From f9fd53ccbb1bb38ed21360179f44b27e2a729701 Mon Sep 17 00:00:00 2001 From: Alexis211 Date: Fri, 5 Feb 2010 19:56:55 +0100 Subject: Elf loader added --- src/stem/Makefile | 3 ++- src/stem/linker/elf.c | 31 +++++++++++++++++++++++++ src/stem/linker/elf.h | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/stem/stem.map | 14 ++++++++++-- 4 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 src/stem/linker/elf.c create mode 100644 src/stem/linker/elf.h diff --git a/src/stem/Makefile b/src/stem/Makefile index d212216..41b5368 100644 --- a/src/stem/Makefile +++ b/src/stem/Makefile @@ -12,7 +12,8 @@ OBJECTS = core/loader_.o core/kmain.o core/sys.o \ core/monitor.o task/timer.o \ task/idt.o task/idt_.o task/task.o task/task_.o task/syscall.o \ lib/stdlib.o lib/bitset.o \ - mem/mem.o mem/paging.o mem/gdt.o mem/heap.o mem/seg.o + mem/mem.o mem/paging.o mem/gdt.o mem/heap.o mem/seg.o \ + linker/elf.o OUT = stem.elf all: $(OBJECTS) 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 +#include +#include + +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; +} diff --git a/src/stem/linker/elf.h b/src/stem/linker/elf.h new file mode 100644 index 0000000..f84bb62 --- /dev/null +++ b/src/stem/linker/elf.h @@ -0,0 +1,63 @@ +#ifndef DEF_ELF_H +#define DEF_ELF_H + +#include +#include + +/* elf_phdr_t :: p_type : program header entries types */ +#define PT_NULL 0 +#define PT_LOAD 1 +#define PT_DYNAMIC 2 +#define PT_INTERP 3 +#define PT_NOTE 4 +#define PT_SHLIB 5 +#define PT_PHDR 6 +#define PT_LOPROC 0x70000000 +#define PT_HIPROC 0x7fffffff + +/* elf_phdr_t :: p_flags : program header entries flags */ +#define PF_X (1 << 0) +#define PF_W (1 << 1) +#define PF_R (1 << 2) + +struct elf_ehdr { + uint8_t e_ident[16]; /* ELF identification */ + uint16_t e_type; /* 2 (exec file) */ + uint16_t e_machine; /* 3 (intel architecture) */ + uint32_t e_version; /* 1 */ + uint32_t e_entry; /* starting point */ + uint32_t e_phoff; /* program header table offset */ + uint32_t e_shoff; /* section header table offset */ + uint32_t e_flags; /* various flags */ + uint16_t e_ehsize; /* ELF header (this) size */ + + uint16_t e_phentsize; /* program header table entry size */ + uint16_t e_phnum; /* number of entries */ + + uint16_t e_shentsize; /* section header table entry size */ + uint16_t e_shnum; /* number of entries */ + + uint16_t e_shstrndx; /* index of the section name string table */ +}; + +struct elf_phdr { + uint32_t p_type; /* type of segment */ + uint32_t p_offset; + uint32_t p_vaddr; + uint32_t p_paddr; + uint32_t p_filesz; + uint32_t p_memsz; + uint32_t p_flags; + uint32_t p_align; +}; + +struct phdr { + struct elf_phdr h; + uint8_t* data; +}; + +int elf_check(uint8_t *data); //0 if ok, 1 if not a valid ELF +thread_entry elf_load(uint8_t *data, struct process* process); //Load an ELF to a process, return entry point +struct process* elf_exec(uint8_t *data); //Creates a new process and a thread for running ELF file + +#endif diff --git a/src/stem/stem.map b/src/stem/stem.map index c35b1ee..a919c01 100644 --- a/src/stem/stem.map +++ b/src/stem/stem.map @@ -26,7 +26,7 @@ Linker script and memory map 0x00100000 loader 0xe010003a . = (. + 0xe0000000) -.text 0xe0100040 0x3395 load address 0x00100040 +.text 0xe0100040 0x355a load address 0x00100040 *(.text) .text 0xe0100040 0x25 core/loader_.o *fill* 0xe0100065 0x3 00 @@ -178,8 +178,12 @@ Linker script and memory map 0xe01032e8 simpleseg_unmap 0xe010333b simpleseg_handleFault 0xe01033d0 simpleseg_delete + *fill* 0xe01033d5 0x3 00 + .text 0xe01033d8 0x1c2 linker/elf.o + 0xe01033d8 elf_check + 0xe010341d elf_load -.iplt 0xe01033d8 0x0 load address 0x001033d5 +.iplt 0xe010359c 0x0 load address 0x0010359a .iplt 0x00000000 0x0 core/loader_.o .rodata 0xe0104000 0x395 load address 0x00104000 @@ -216,6 +220,7 @@ Linker script and memory map .data 0xe0105024 0x0 mem/gdt.o .data 0xe0105024 0x0 mem/heap.o .data 0xe0105024 0x0 mem/seg.o + .data 0xe0105024 0x0 linker/elf.o .igot.plt 0xe0105024 0x0 load address 0x00105024 .igot.plt 0x00000000 0x0 core/loader_.o @@ -258,6 +263,7 @@ Linker script and memory map *fill* 0xe01159ae 0x2 00 .bss 0xe01159b0 0x0 mem/heap.o .bss 0xe01159b0 0x0 mem/seg.o + .bss 0xe01159b0 0x0 linker/elf.o 0xe01159b0 ebss = . 0xe01159b0 end = . 0xe01159b0 _end = . @@ -279,6 +285,7 @@ LOAD mem/paging.o LOAD mem/gdt.o LOAD mem/heap.o LOAD mem/seg.o +LOAD linker/elf.o OUTPUT(stem.elf elf32-i386) .comment 0x00000000 0x11 @@ -297,6 +304,7 @@ OUTPUT(stem.elf elf32-i386) .comment 0x00000000 0x12 mem/gdt.o .comment 0x00000000 0x12 mem/heap.o .comment 0x00000000 0x12 mem/seg.o + .comment 0x00000000 0x12 linker/elf.o .note.GNU-stack 0x00000000 0x0 @@ -328,3 +336,5 @@ OUTPUT(stem.elf elf32-i386) 0x00000000 0x0 mem/heap.o .note.GNU-stack 0x00000000 0x0 mem/seg.o + .note.GNU-stack + 0x00000000 0x0 linker/elf.o -- cgit v1.2.3