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/mm/valloc.h>
6 #include <lunaix/spike.h>
9 elf32_smap(struct load_context* ldctx,
10 const struct elf32* elf,
11 struct elf32_phdr* phdre,
14 struct v_file* elfile = (struct v_file*)elf->elf_file;
16 assert(PG_ALIGNED(phdre->p_offset));
19 if ((phdre->p_flags & PF_R)) {
22 if ((phdre->p_flags & PF_W)) {
25 if ((phdre->p_flags & PF_X)) {
29 uintptr_t va = phdre->p_va + base_va;
30 struct exec_container* container = ldctx->container;
31 struct mmap_param param = { .vms_mnt = container->vms_mnt,
32 .pvms = &container->proc->mm,
34 .offset = PG_ALIGN(phdre->p_offset),
35 .mlen = ROUNDUP(phdre->p_memsz, PG_SIZE),
36 .flen = phdre->p_filesz,
37 .flags = MAP_FIXED | MAP_PRIVATE,
38 .type = REGION_TYPE_CODE };
40 struct mm_region* seg_reg;
41 int status = mem_map(NULL, &seg_reg, PG_ALIGN(va), elfile, ¶m);
44 size_t next_addr = phdre->p_memsz + va;
45 ldctx->end = MAX(ldctx->end, ROUNDUP(next_addr, PG_SIZE));
46 ldctx->mem_sz += phdre->p_memsz;
48 // we probably fucked up our process
56 load_executable(struct load_context* context, const struct v_file* exefile)
62 struct exec_container* container = context->container;
64 if ((errno = elf32_openat(&elf, exefile))) {
68 if (!elf32_check_arch(&elf)) {
73 if (!(elf32_check_exec(&elf, ET_EXEC) || elf32_check_exec(&elf, ET_DYN))) {
79 errno = elf32_find_loader(&elf, ldpath, 512);
80 uintptr_t load_base = 0;
86 if (errno != NO_LOADER) {
87 container->argv_pp[1] = ldpath;
90 if ((errno = elf32_close(&elf))) {
94 // open the loader instead
95 if ((errno = elf32_open(&elf, ldpath))) {
99 // Is this the valid loader?
100 if (!elf32_static_linked(&elf) || !elf32_check_exec(&elf, ET_DYN)) {
102 goto done_close_elf32;
105 load_base = UMMAP_START;
108 context->entry = elf.eheader.e_entry + load_base;
110 struct v_file* elfile = (struct v_file*)elf.elf_file;
112 for (size_t i = 0; i < elf.eheader.e_phnum && !errno; i++) {
113 struct elf32_phdr* phdr = &elf.pheaders[i];
115 if (phdr->p_type != PT_LOAD) {
119 if (phdr->p_align != PG_SIZE) {
120 // surprising alignment!
125 errno = elf32_smap(context, &elf, phdr, load_base);