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>
8 #include <sys/mm/mempart.h>
11 elf32_smap(struct load_context* ldctx,
12 const struct elf32* elf,
13 struct elf32_phdr* phdre,
16 struct v_file* elfile = (struct v_file*)elf->elf_file;
18 assert(PG_ALIGNED(phdre->p_offset));
21 if ((phdre->p_flags & PF_R)) {
24 if ((phdre->p_flags & PF_W)) {
27 if ((phdre->p_flags & PF_X)) {
31 uintptr_t va = phdre->p_va + base_va;
32 struct exec_container* container = ldctx->container;
33 struct mmap_param param = { .vms_mnt = container->vms_mnt,
34 .pvms = &container->proc->mm,
36 .offset = PG_ALIGN(phdre->p_offset),
37 .mlen = ROUNDUP(phdre->p_memsz, PG_SIZE),
38 .flen = phdre->p_filesz,
39 .flags = MAP_FIXED | MAP_PRIVATE,
40 .type = REGION_TYPE_CODE };
42 struct mm_region* seg_reg;
43 int status = mem_map(NULL, &seg_reg, PG_ALIGN(va), elfile, ¶m);
46 size_t next_addr = phdre->p_memsz + va;
47 ldctx->end = MAX(ldctx->end, ROUNDUP(next_addr, PG_SIZE));
48 ldctx->mem_sz += phdre->p_memsz;
50 // we probably fucked up our process
58 load_executable(struct load_context* context, const struct v_file* exefile)
64 struct exec_container* container = context->container;
66 if ((errno = elf32_openat(&elf, exefile))) {
70 if (!elf32_check_arch(&elf)) {
75 if (!(elf32_check_exec(&elf, ET_EXEC) || elf32_check_exec(&elf, ET_DYN))) {
81 errno = elf32_find_loader(&elf, ldpath, 512);
82 uintptr_t load_base = 0;
88 if (errno != NO_LOADER) {
89 container->argv_pp[1] = ldpath;
92 if ((errno = elf32_close(&elf))) {
96 // open the loader instead
97 if ((errno = elf32_open(&elf, ldpath))) {
101 // Is this the valid loader?
102 if (!elf32_static_linked(&elf) || !elf32_check_exec(&elf, ET_DYN)) {
104 goto done_close_elf32;
107 load_base = USR_MMAP;
110 context->entry = elf.eheader.e_entry + load_base;
112 struct v_file* elfile = (struct v_file*)elf.elf_file;
114 for (size_t i = 0; i < elf.eheader.e_phnum && !errno; i++) {
115 struct elf32_phdr* phdr = &elf.pheaders[i];
117 if (phdr->p_type != PT_LOAD) {
121 if (phdr->p_align != PG_SIZE) {
122 // surprising alignment!
127 errno = elf32_smap(context, &elf, phdr, load_base);