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 = vmspace(container->proc),
36 .offset = PG_ALIGN(phdre->p_offset),
37 .mlen = ROUNDUP(phdre->p_memsz, PG_SIZE),
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, PG_ALIGN(va), elfile, ¶m);
45 size_t next_addr = phdre->p_memsz + va;
46 ldctx->end = MAX(ldctx->end, ROUNDUP(next_addr, PG_SIZE));
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_container* container = context->container;
65 if ((errno = elf32_openat(&elf, exefile))) {
69 if (!elf32_check_arch(&elf)) {
74 if (!(elf32_check_exec(&elf, ET_EXEC) || elf32_check_exec(&elf, ET_DYN))) {
80 errno = elf32_find_loader(&elf, ldpath, 256);
81 uintptr_t load_base = 0;
87 if (errno != NO_LOADER) {
88 container->argv_pp[1] = ldpath;
91 if ((errno = elf32_close(&elf))) {
95 // open the loader instead
96 if ((errno = elf32_open(&elf, ldpath))) {
100 // Is this the valid loader?
101 if (!elf32_static_linked(&elf) || !elf32_check_exec(&elf, ET_DYN)) {
103 goto done_close_elf32;
106 load_base = USR_MMAP;
109 context->entry = elf.eheader.e_entry + load_base;
111 struct v_file* elfile = (struct v_file*)elf.elf_file;
113 for (size_t i = 0; i < elf.eheader.e_phnum && !errno; i++) {
114 struct elf32_phdr* phdr = &elf.pheaders[i];
116 if (phdr->p_type != PT_LOAD) {
120 if (phdr->p_align != PG_SIZE) {
121 // surprising alignment!
126 errno = elf32_smap(context, &elf, phdr, load_base);
133 if (!container->argv_pp[1]) {