1 #include <lunaix/exebi/elf.h>
2 #include <lunaix/load.h>
3 #include <lunaix/mm/mmap.h>
4 #include <lunaix/mm/valloc.h>
5 #include <lunaix/spike.h>
7 #include <asm/mempart.h>
10 elf_smap(struct load_context* ldctx,
11 const struct elf* elf,
12 struct elf_phdr* phdre,
15 struct v_file* elfile = (struct v_file*)elf->elf_file;
17 assert(!va_offset(phdre->p_offset));
20 if ((phdre->p_flags & PF_R)) {
23 if ((phdre->p_flags & PF_W)) {
26 if ((phdre->p_flags & PF_X)) {
30 uintptr_t va = phdre->p_va + base_va;
31 struct exec_host* container = ldctx->container;
32 struct mmap_param param = { .vms_mnt = container->vms_mnt,
33 .pvms = vmspace(container->proc),
35 .offset = page_aligned(phdre->p_offset),
36 .mlen = page_upaligned(phdre->p_memsz),
37 .flen = phdre->p_filesz,
38 .flags = MAP_FIXED | MAP_PRIVATE,
39 .type = REGION_TYPE_CODE };
41 struct mm_region* seg_reg;
42 int status = mmap_user(NULL, &seg_reg, page_aligned(va), elfile, ¶m);
45 size_t next_addr = phdre->p_memsz + va;
46 ldctx->end = MAX(ldctx->end, page_upaligned(next_addr));
47 ldctx->mem_sz += phdre->p_memsz;
49 // we probably fucked up our process
50 terminate_current(-1);
57 load_executable(struct load_context* context, const struct v_file* exefile)
63 struct exec_host* container = context->container;
65 if ((errno = elf_openat(&elf, exefile))) {
69 if (!elf_check_arch(&elf)) {
74 if (!(elf_check_exec(&elf, ET_EXEC) || elf_check_exec(&elf, ET_DYN))) {
80 errno = elf_find_loader(&elf, ldpath, 256);
81 uintptr_t load_base = 0;
87 if (errno != NO_LOADER) {
89 if ((errno = elf_close(&elf))) {
93 // open the loader instead
94 if ((errno = elf_open(&elf, ldpath))) {
98 // Is this the valid loader?
99 if (!elf_static_linked(&elf) || !elf_check_exec(&elf, ET_DYN)) {
101 goto done_close_elf32;
104 load_base = USR_MMAP;
107 context->entry = elf.eheader.e_entry + load_base;
109 struct v_file* elfile = (struct v_file*)elf.elf_file;
111 for (size_t i = 0; i < elf.eheader.e_phnum && !errno; i++) {
112 struct elf_phdr* phdr = &elf.pheaders[i];
114 if (phdr->p_type != PT_LOAD) {
118 if (phdr->p_align != PAGE_SIZE) {
119 // surprising alignment!
124 errno = elf_smap(context, &elf, phdr, load_base);