summaryrefslogtreecommitdiff
path: root/src/stem/linker/elf.c
diff options
context:
space:
mode:
authorAlexis211 <alexis211@gmail.com>2010-02-05 19:56:55 +0100
committerAlexis211 <alexis211@gmail.com>2010-02-05 19:56:55 +0100
commitf9fd53ccbb1bb38ed21360179f44b27e2a729701 (patch)
treed61b0a3e9f347493748053685c1185cfab0961f6 /src/stem/linker/elf.c
parentc0018a3607947922a51597df12fd2a6528af2fa2 (diff)
downloadTCE-f9fd53ccbb1bb38ed21360179f44b27e2a729701.tar.gz
TCE-f9fd53ccbb1bb38ed21360179f44b27e2a729701.zip
Elf loader added
Diffstat (limited to 'src/stem/linker/elf.c')
-rw-r--r--src/stem/linker/elf.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/stem/linker/elf.c b/src/stem/linker/elf.c
new file mode 100644
index 0000000..62030ee
--- /dev/null
+++ b/src/stem/linker/elf.c
@@ -0,0 +1,31 @@
+#include "elf.h"
+#include <mem/paging.h>
+#include <mem/seg.h>
+#include <stdlib.h>
+
+int elf_check(uint8_t *data) {
+ struct elf_ehdr *h = (struct elf_ehdr*)data;
+ if (h->e_ident[0] == 0x7F && h->e_ident[1] == 'E' && h->e_ident[2] == 'L' && h->e_ident[3] == 'F') return 0;
+ return 1;
+}
+
+thread_entry elf_load(uint8_t *data, struct process* process) {
+ struct elf_ehdr *ehdr = (struct elf_ehdr*)data;
+ struct elf_phdr *phdr;
+ int i;
+ if (elf_check(data)) return 0;
+
+ pagedir_switch(process->pagedir);
+
+ phdr = (struct elf_phdr*)((uint8_t*)(data + ehdr->e_phoff));
+ for (i = 0; i < ehdr->e_phnum; i++) {
+ if (phdr[i].p_type == PT_LOAD) {
+ seg_map(simpleseg_make(phdr[i].p_vaddr, phdr[i].p_memsz, (phdr[i].p_flags & PF_W) != 0), process->pagedir);
+ memcpy((uint8_t*)phdr[i].p_vaddr, data + phdr[i].p_offset, phdr[i].p_filesz);
+ if (phdr[i].p_memsz > phdr[i].p_filesz) {
+ memset((uint8_t*)phdr[i].p_vaddr + phdr[i].p_memsz, 0, phdr[i].p_memsz - phdr[i].p_filesz);
+ }
+ }
+ }
+ return (thread_entry)ehdr->e_entry;
+}