diff options
author | dzwdz | 2022-07-23 19:28:43 +0200 |
---|---|---|
committer | dzwdz | 2022-07-23 19:28:43 +0200 |
commit | 20c7b5be3bbb5f56a31536c018e20706100f57ae (patch) | |
tree | 47dde902a7d2a2ff464c600d76b4743fe8153e63 /src/user/lib | |
parent | 3badc5ac30fcc0836a2604146788158b16b64f2d (diff) |
user_bootstrap: pass the initrd in an argument to init's main
Diffstat (limited to 'src/user/lib')
-rw-r--r-- | src/user/lib/elfload.c | 51 | ||||
-rw-r--r-- | src/user/lib/elfload.h | 1 |
2 files changed, 35 insertions, 17 deletions
diff --git a/src/user/lib/elfload.c b/src/user/lib/elfload.c index 6c75268..ef14388 100644 --- a/src/user/lib/elfload.c +++ b/src/user/lib/elfload.c @@ -62,41 +62,58 @@ static size_t elf_spread(const void *elf) { return high - low; } -/* frees memory outside of [low; high] and jumps to *entry */ -static void freejmp(void *entry, void *low, void *high) { +/* frees memory outside of [low; low + len] and jumps to *entry */ +static void freejmp(void *entry, void *low, size_t len) { + uintptr_t high = (uintptr_t)low + len; uint64_t buf[] = { EXECBUF_SYSCALL, _SYSCALL_MEMFLAG, 0, (uintptr_t)low, 0, 0, - EXECBUF_SYSCALL, _SYSCALL_MEMFLAG, (uintptr_t)high, ~0 - 0xF000 - (uintptr_t)high, 0, 0, + EXECBUF_SYSCALL, _SYSCALL_MEMFLAG, high, ~0 - 0xF000 - high, 0, 0, EXECBUF_JMP, (uintptr_t)entry, }; _syscall_execbuf(buf, sizeof buf); + // should never return } -void elf_exec(void *base) { - struct Elf64_Ehdr *ehdr = base; +static void *elf_loadmem(struct Elf64_Ehdr *ehdr) { void *exebase; - if (!valid_ehdr(ehdr)) return; - _syscall_debug_klog("here", 4); - size_t spread = elf_spread(base); + size_t spread = elf_spread(ehdr); switch (ehdr->e_type) { case ET_EXEC: exebase = (void*)0; break; case ET_DYN: exebase = _syscall_memflag((void*)0x1000, spread, MEMFLAG_FINDFREE); - if (!exebase) { - printf("elf: out of memory\n"); - _syscall_exit(1); - } + if (!exebase) + return NULL; break; default: - return; + return NULL; } for (size_t phi = 0; phi < ehdr->e_phnum; phi++) { - if (!load_phdr(base, exebase, phi)) - return; + if (!load_phdr((void*)ehdr, exebase, phi)) { + _syscall_memflag(exebase, spread, 0); + return NULL; + } } + return exebase; +} + +void elf_exec(void *base) { + struct Elf64_Ehdr *ehdr = base; + if (!valid_ehdr(ehdr)) return; + + void *exebase = elf_loadmem(ehdr); + if (!exebase) return; + + freejmp(exebase + ehdr->e_entry, exebase, elf_spread(ehdr) + 0x1000); +} + +void *elf_partialexec(void *base) { + struct Elf64_Ehdr *ehdr = base; + if (!valid_ehdr(ehdr)) return NULL; + + void *exebase = elf_loadmem(ehdr); + if (!exebase) return NULL; - freejmp(exebase + ehdr->e_entry, exebase, exebase + spread + 0x1000); - printf("elf: execbuf failed?"); + return exebase + ehdr->e_entry; } diff --git a/src/user/lib/elfload.h b/src/user/lib/elfload.h index f79be6e..40c4874 100644 --- a/src/user/lib/elfload.h +++ b/src/user/lib/elfload.h @@ -3,5 +3,6 @@ void elf_execf(libc_file *f); void elf_exec(void *elf); +void *elf_partialexec(void *elf); /* returns pointer to entry point */ void elf_selfreloc(void); // elfreloc.c |