5-malloc.md (#25)
[lunaix-os.git] / lunaix-os / kernel / exe / elf32 / ldelf32.c
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>
7
8 #include <sys/mm/mempart.h>
9
10 int
11 elf32_smap(struct load_context* ldctx,
12            const struct elf32* elf,
13            struct elf32_phdr* phdre,
14            uintptr_t base_va)
15 {
16     struct v_file* elfile = (struct v_file*)elf->elf_file;
17
18     assert(PG_ALIGNED(phdre->p_offset));
19
20     int proct = 0;
21     if ((phdre->p_flags & PF_R)) {
22         proct |= PROT_READ;
23     }
24     if ((phdre->p_flags & PF_W)) {
25         proct |= PROT_WRITE;
26     }
27     if ((phdre->p_flags & PF_X)) {
28         proct |= PROT_EXEC;
29     }
30
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),
35                                 .proct = proct,
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 };
40
41     struct mm_region* seg_reg;
42     int status = mmap_user(NULL, &seg_reg, PG_ALIGN(va), elfile, &param);
43
44     if (!status) {
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;
48     } else {
49         // we probably fucked up our process
50         terminate_current(-1);
51     }
52
53     return status;
54 }
55
56 int
57 load_executable(struct load_context* context, const struct v_file* exefile)
58 {
59     int errno = 0;
60
61     char* ldpath = NULL;
62     struct elf32 elf;
63     struct exec_container* container = context->container;
64
65     if ((errno = elf32_openat(&elf, exefile))) {
66         goto done;
67     }
68
69     if (!elf32_check_arch(&elf)) {
70         errno = EINVAL;
71         goto done;
72     }
73
74     if (!(elf32_check_exec(&elf, ET_EXEC) || elf32_check_exec(&elf, ET_DYN))) {
75         errno = ENOEXEC;
76         goto done;
77     }
78
79     ldpath = valloc(256);
80     errno = elf32_find_loader(&elf, ldpath, 256);
81     uintptr_t load_base = 0;
82
83     if (errno < 0) {
84         goto done;
85     }
86
87     if (errno != NO_LOADER) {
88         container->argv_pp[1] = ldpath;
89
90         // close old elf
91         if ((errno = elf32_close(&elf))) {
92             goto done;
93         }
94
95         // open the loader instead
96         if ((errno = elf32_open(&elf, ldpath))) {
97             goto done;
98         }
99
100         // Is this the valid loader?
101         if (!elf32_static_linked(&elf) || !elf32_check_exec(&elf, ET_DYN)) {
102             errno = ELIBBAD;
103             goto done_close_elf32;
104         }
105
106         load_base = USR_MMAP;
107     }
108
109     context->entry = elf.eheader.e_entry + load_base;
110
111     struct v_file* elfile = (struct v_file*)elf.elf_file;
112
113     for (size_t i = 0; i < elf.eheader.e_phnum && !errno; i++) {
114         struct elf32_phdr* phdr = &elf.pheaders[i];
115
116         if (phdr->p_type != PT_LOAD) {
117             continue;
118         }
119
120         if (phdr->p_align != PG_SIZE) {
121             // surprising alignment!
122             errno = ENOEXEC;
123             break;
124         }
125
126         errno = elf32_smap(context, &elf, phdr, load_base);
127     }
128
129 done_close_elf32:
130     elf32_close(&elf);
131
132 done:
133     if (!container->argv_pp[1]) {
134         vfree_safe(ldpath);
135     }
136     return errno;
137 }