From 4e98bcf99d499fdbf7a4c3ac341fedbe1c1292a6 Mon Sep 17 00:00:00 2001 From: Qwinci <32550582+Qwinci@users.noreply.github.com> Date: Mon, 13 Jan 2025 16:15:58 +0200 Subject: [PATCH] elf: support loading PIE executables --- kernel-src/sys/elf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kernel-src/sys/elf.c b/kernel-src/sys/elf.c index 91cf57a4..3b60dd84 100644 --- a/kernel-src/sys/elf.c +++ b/kernel-src/sys/elf.c @@ -153,6 +153,10 @@ int elf_load(vnode_t *vnode, void *base, void **entry, char **interpreter, auxv6 if (headerok(&header) == false) return ENOEXEC; + if (!base && header.type == ELF_SHARED) { + base = (void *)0x400000; + } + auxv64->null.type = AT_NULL; auxv64->phdr.type = AT_PHDR; auxv64->phnum.type = AT_PHNUM; @@ -161,7 +165,7 @@ int elf_load(vnode_t *vnode, void *base, void **entry, char **interpreter, auxv6 auxv64->phnum.val = header.phcount; auxv64->phent.val = header.phsize; - auxv64->entry.val = header.entry; + auxv64->entry.val = header.entry + (uintptr_t)base; *entry = (void *)(header.entry + (uintptr_t)base); __assert(header.phsize == sizeof(elfph64_t));