diff options
author | Jon Santmyer <jon@jonsantmyer.com> | 2024-03-11 21:30:31 -0400 |
---|---|---|
committer | Jon Santmyer <jon@jonsantmyer.com> | 2024-03-11 21:30:31 -0400 |
commit | d1ff7bcc91886626dc9060ec5fb67ee102ab7c1d (patch) | |
tree | 8f0b5cd8aad31089131785dc6e37b659490f9955 /arch/x86_64/elf.c | |
download | jove-kernel-d1ff7bcc91886626dc9060ec5fb67ee102ab7c1d.tar.gz jove-kernel-d1ff7bcc91886626dc9060ec5fb67ee102ab7c1d.tar.bz2 jove-kernel-d1ff7bcc91886626dc9060ec5fb67ee102ab7c1d.zip |
usermode capable kernel with logging syscall
Diffstat (limited to 'arch/x86_64/elf.c')
-rw-r--r-- | arch/x86_64/elf.c | 47 |
1 files changed, 47 insertions, 0 deletions
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; +} |