From f092bf03baf3b2c13c9dc462bdc697caddccd347 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Wed, 29 Jul 2020 15:55:13 +0200 Subject: Added elf loading and test app --- src/features/elf.c | 39 +++++++++++++++++++++++++++++++++++++++ src/features/gui.c | 2 +- 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/features/elf.c (limited to 'src/features') diff --git a/src/features/elf.c b/src/features/elf.c new file mode 100644 index 0000000..339add0 --- /dev/null +++ b/src/features/elf.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include + +int elf_verify(struct elf_header *header) +{ + if (header->ident[0] == ELF_MAG && header->ident[1] == 'E' && header->ident[2] == 'L' && + header->ident[3] == 'F' && header->ident[4] == ELF_32 && + header->ident[5] == ELF_LITTLE && header->ident[6] == ELF_CURRENT && + header->machine == ELF_386 && (header->type == ET_REL || header->type == ET_EXEC)) { + return 1; + } + return 0; +} + +void elf_load(char *path) +{ + u32 *data = read_file(path); + + struct elf_header *h = (struct elf_header *)data; + assert(elf_verify(h)); + + struct elf_program_header *p = (struct elf_program_header *)((u32)data + h->phoff); + struct elf_program_header *p_end = + (struct elf_program_header *)((u32)p + (h->phentsize * h->phnum)); + + u32 offset = (p->vaddr - p->paddr); + while (p < p_end) { + memcpy((void *)p->paddr, (void *)((u32)data + p->offset), p->filesz); + p++; + } + + void (*entry)(); + entry = (void (*)())(h->entry - offset); + + entry(); +} diff --git a/src/features/gui.c b/src/features/gui.c index fa0641c..d080cb1 100644 --- a/src/features/gui.c +++ b/src/features/gui.c @@ -37,7 +37,7 @@ void gui_write(int x, int y, const u32 c[3], char *text) } // Abstraction -int x, y = 1; +int x, y = 0; const u32 c[3] = { 0xff, 0xff, 0xff }; void gui_term_write_char(char ch) { -- cgit v1.2.3