1 #include <lunaix/exebi/elf32.h>
2 #include <lunaix/load.h>
3 #include <lunaix/mm/mmap.h>
4 #include <lunaix/mm/page.h>
5 #include <lunaix/spike.h>
8 elf32_smap(struct load_context* ldctx,
9 const struct elf32* elf,
10 struct elf32_phdr* phdre)
12 struct v_file* elfile = (struct v_file*)elf->elf_file;
14 assert(PG_ALIGNED(phdre->p_offset));
17 if ((phdre->p_flags & PF_R)) {
20 if ((phdre->p_flags & PF_W)) {
23 if ((phdre->p_flags & PF_X)) {
27 struct exec_container* container = ldctx->container;
28 struct mmap_param param = { .vms_mnt = container->vms_mnt,
29 .pvms = &container->proc->mm,
31 .offset = PG_ALIGN(phdre->p_offset),
32 .mlen = ROUNDUP(phdre->p_memsz, PG_SIZE),
33 .flen = phdre->p_filesz + PG_MOD(phdre->p_va),
34 .flags = MAP_FIXED | MAP_PRIVATE,
35 .type = REGION_TYPE_CODE };
37 struct mm_region* seg_reg;
38 int status = mem_map(NULL, &seg_reg, PG_ALIGN(phdre->p_va), elfile, ¶m);
41 size_t next_addr = phdre->p_memsz + phdre->p_va;
42 ldctx->end = MAX(ldctx->end, ROUNDUP(next_addr, PG_SIZE));
43 ldctx->mem_sz += phdre->p_memsz;
45 // we probably fucked up our process
53 load_executable(struct load_context* context, const struct v_file* exefile)
59 struct exec_container* container = context->container;
61 if ((errno = elf32_openat(&elf, exefile))) {
65 if (!elf32_check_exec(&elf)) {
71 errno = elf32_find_loader(&elf, ldpath, 512);
77 if (errno != NO_LOADER) {
78 container->argv_pp[1] = ldpath;
81 if ((errno = elf32_close(&elf))) {
85 // open the loader instead
86 if ((errno = elf32_open(&elf, ldpath))) {
90 // Is this the valid loader?
91 if (!elf32_static_linked(&elf) || !elf32_check_exec(&elf)) {
93 goto done_close_elf32;
96 // TODO: relocate loader
98 context->entry = elf.eheader.e_entry;
101 struct v_file* elfile = (struct v_file*)elf.elf_file;
103 for (size_t i = 0; i < elf.eheader.e_phnum && !errno; i++) {
104 struct elf32_phdr* phdr = &elf.pheaders[i];
106 if (phdr->p_type == PT_LOAD) {
107 if (phdr->p_align != PG_SIZE) {
108 // surprising alignment!
113 errno = elf32_smap(context, &elf, phdr);
115 // TODO Handle relocation