From d1ff7bcc91886626dc9060ec5fb67ee102ab7c1d Mon Sep 17 00:00:00 2001 From: Jon Santmyer Date: Mon, 11 Mar 2024 21:30:31 -0400 Subject: usermode capable kernel with logging syscall --- arch/x86_64/elf.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 arch/x86_64/elf.c (limited to 'arch/x86_64/elf.c') diff --git a/arch/x86_64/elf.c b/arch/x86_64/elf.c new file mode 100644 index 0000000..60b8f13 --- /dev/null +++ b/arch/x86_64/elf.c @@ -0,0 +1,47 @@ +#include "elf.h" +#include "lib/string.h" +#include "usr/elf.h" +#include "io/log.h" +#include "mem/memory.h" + +void* +elf_load(const void *data, size_t len) +{ + if(data == NULL) return NULL; + if(len < 4) return NULL; + + struct ELF_header *ehdr = (struct ELF_header*)data; + if(ehdr->e_ident[EI_MAG0] != E_IDENT_MAG0 && + ehdr->e_ident[EI_MAG1] != E_IDENT_MAG1 && + ehdr->e_ident[EI_MAG2] != E_IDENT_MAG2 && + ehdr->e_ident[EI_MAG3] != E_IDENT_MAG3) + return NULL; + + if(ehdr->e_ident[EI_CLASS] != E_IDENT_CLASS64) { + klogf("Jove does not support ELF files of class CLASS32\n"); + return NULL; + } + + if(ehdr->e_type != ET_EXEC) { + klogf("Jove does not support ELF files other than ET_EXEC\n"); + return NULL; + } + + uint64_t entry = ehdr->e_entry; + + size_t phdrc = ehdr->e_phnum; + struct ELF_phdr *phdrs = (struct ELF_phdr*)((uintptr_t)data + ehdr->e_phoff); + + for(size_t phdri = 0; phdri < phdrc; phdri++) + { + struct ELF_phdr *phdr = &phdrs[phdri]; + void *pdata = (void*)phdr->p_vaddr; + + mem_ensure_range(phdr->p_vaddr, phdr->p_vaddr + phdr->p_memsz, true, true); + if(phdr->p_type == PT_LOAD) + { + memcpy(pdata, (void*)((uintptr_t)data + phdr->p_offset), phdr->p_filesz); + } + } + return (void*)entry; +} -- cgit v1.2.1