summaryrefslogtreecommitdiffstats
path: root/arch/x86_64/elf_load.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86_64/elf_load.c')
-rw-r--r--arch/x86_64/elf_load.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/arch/x86_64/elf_load.c b/arch/x86_64/elf_load.c
new file mode 100644
index 0000000..d7d1b29
--- /dev/null
+++ b/arch/x86_64/elf_load.c
@@ -0,0 +1,58 @@
+#include "arch/elf.h"
+#include "string.h"
+#include "memory.h"
+#include "print.h"
+
+uintptr_t
+elf_load(const void *data, size_t len)
+{
+ if(data == NULL) return 0;
+ if(len < 4) return 0;
+
+ elf_header_t *ehdr = (elf_header_t*)data;
+ if(ehdr->ei_mag[0] != EI_MAG0 ||
+ ehdr->ei_mag[1] != EI_MAG1 ||
+ ehdr->ei_mag[2] != EI_MAG2 ||
+ ehdr->ei_mag[3] != EI_MAG3) return 0;
+
+ if(ehdr->ei_class != EI_CLASS_64) {
+ kerrf("Jove does not support ELF files of class CLASS32\n");
+ return 0;
+ }
+
+ if(ehdr->e_type != ET_EXEC) {
+ kerrf("Jove does not support ELF files other than ET_EXEC\n");
+ return 0;
+ }
+
+ uint64_t entry = ehdr->e_entry;
+
+ size_t phdrc = ehdr->e_phnum;
+ elf_phdr_t *phdrs = (elf_phdr_t*)((uintptr_t)data + ehdr->e_phoff);
+
+ for(size_t phdri = 0; phdri < phdrc; phdri++)
+ {
+ elf_phdr_t *phdr = &phdrs[phdri];
+ void *pdata = (void*)phdr->p_vaddr;
+
+ vm_ensure(
+ phdr->p_vaddr,
+ phdr->p_vaddr + phdr->p_memsz,
+ (page_flags_t) {
+ .present = true,
+ .writeable = true,
+ .useraccess = true,
+ .executable = true
+ });
+
+ if(phdr->p_type == PT_LOAD)
+ {
+ kdbgf("PT_LOAD %#016X [%#016X] -> %#016X\n",
+ (uintptr_t)data + phdr->p_offset,
+ phdr->p_filesz,
+ pdata);
+ memcpy(pdata, (void*)((uintptr_t)data + phdr->p_offset), phdr->p_filesz);
+ }
+ }
+ return entry;
+}