diff --git a/include/elf.h b/include/elf.h index 6605add..24146ea 100644 --- a/include/elf.h +++ b/include/elf.h @@ -1,10 +1,9 @@ #pragma once +#include "pageallocator.h" #include "types/physaddr.h" #include -const uint32_t elf_magic_number = 0x464c457f; - enum elf_endianness_t { ELF_LITTLE_ENDIAN = 1, @@ -107,11 +106,13 @@ struct elf_section_header_t }; #if defined __i386__ - static const elf_isa_t HOST_ISA = ELF_ISA_x86; + static const enum elf_isa_t HOST_ISA = ELF_ISA_x86; #elif defined __x86_64__ - static const elf_isa_t HOST_ISA = ELF_ISA_x86_64; + static const enum elf_isa_t HOST_ISA = ELF_ISA_x86_64; #elif defined __arm__ - static const elf_isa_t HOST_ISA = ELF_ISA_ARM; + static const enum elf_isa_t HOST_ISA = ELF_ISA_ARM; #elif defined __aarch64__ - static const elf_isa_t HOST_ISA = ELF_ISA_AARCH64; + static const enum elf_isa_t HOST_ISA = ELF_ISA_AARCH64; #endif + +int load_program(struct elf_file_header_t *elf, struct page_stack_t *page_stack); diff --git a/src/elf.c b/src/elf.c new file mode 100644 index 0000000..586fb21 --- /dev/null +++ b/src/elf.c @@ -0,0 +1,40 @@ +#include "elf.h" +#include "pageallocator.h" +#include "mmgr.h" +#include "string.h" +#include "types/status.h" + +const uint32_t elf_magic_number = 0x464c457f; + +int load_program(struct elf_file_header_t *elf, struct page_stack_t *page_stack) +{ + struct elf_program_header_t *program_header = (struct elf_program_header_t*)((void*)elf + elf->phoffset); + unsigned int count = elf->phcount; + while(count > 0) + { + if(program_header->type == ELF_LOAD) + { + void *d = program_header->vaddr, *s = (void*)elf + program_header->offset; + for(size_t n = 0; n < program_header->memsize; n += page_size) + { + physaddr_t page = reserve_page(page_stack); + if(page == S_OUT_OF_MEMORY) + { + return S_OUT_OF_MEMORY; + } + int status = map_page(page_stack, d, page, PAGE_RW | PAGE_USERMODE | PAGE_EXECUTABLE); + switch(status) + { + case S_OUT_OF_MEMORY: + return status; + case S_OUT_OF_BOUNDS: + return status; + case S_OK: + memcpy(d, s, page_size); + } + } + } + count--; + program_header = (struct elf_program_header_t*)((void*)program_header + elf->phsize); + } +} \ No newline at end of file