From 277e4af4fa9e80816c809542d792ee6bebb7f202 Mon Sep 17 00:00:00 2001 From: Alex AUVOLAT Date: Fri, 4 May 2012 20:06:37 +0200 Subject: Migration to C++! --- src/kernel/linker/elf.c | 57 ----------------------------------------------- src/kernel/linker/elf.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++ src/kernel/linker/elf.h | 6 ++--- 3 files changed, 60 insertions(+), 60 deletions(-) delete mode 100644 src/kernel/linker/elf.c create mode 100644 src/kernel/linker/elf.cpp (limited to 'src/kernel/linker') diff --git a/src/kernel/linker/elf.c b/src/kernel/linker/elf.c deleted file mode 100644 index af6d057..0000000 --- a/src/kernel/linker/elf.c +++ /dev/null @@ -1,57 +0,0 @@ -#include "elf.h" -#include -#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; - size_t dataseg = 0; - if (elf_check(data)) return 0; - - struct page_directory *r = current_pagedir; - cli(); - 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, 0); - 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); - } - if (phdr[i].p_vaddr + phdr[i].p_memsz > dataseg) { - dataseg = phdr[i].p_vaddr + phdr[i].p_memsz; - dataseg = (dataseg & 0xFFFFF000) + 0x1000; - } - } - } - - process->data = dataseg; - - pagedir_switch(r); - sti(); - - return (thread_entry)ehdr->e_entry; -} - -struct process* elf_exec(uint8_t *data, int privilege) { - if (elf_check(data)) return 0; - - struct process* p = process_new(0, 0, privilege); - - thread_entry e = elf_load(data, p); - - thread_new(p, e, 0, 0); - - return p; -} diff --git a/src/kernel/linker/elf.cpp b/src/kernel/linker/elf.cpp new file mode 100644 index 0000000..ab8e349 --- /dev/null +++ b/src/kernel/linker/elf.cpp @@ -0,0 +1,57 @@ +#include "elf.h" +#include +#include +#include +#include + +int elf_check(uint8_t *data) { + elf_ehdr *h = (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, process* process) { + elf_ehdr *ehdr = (elf_ehdr*)data; + elf_phdr *phdr; + int i; + size_t dataseg = 0; + if (elf_check(data)) return 0; + + page_directory *r = current_pagedir; + cli(); + pagedir_switch(process->pagedir); + + phdr = (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, 0); + 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); + } + if (phdr[i].p_vaddr + phdr[i].p_memsz > dataseg) { + dataseg = phdr[i].p_vaddr + phdr[i].p_memsz; + dataseg = (dataseg & 0xFFFFF000) + 0x1000; + } + } + } + + process->data = dataseg; + + pagedir_switch(r); + sti(); + + return (thread_entry)ehdr->e_entry; +} + +process* elf_exec(uint8_t *data, int privilege) { + if (elf_check(data)) return 0; + + process* p = process_new(0, 0, privilege); + + thread_entry e = elf_load(data, p); + + thread_new(p, e, 0, 0); + + return p; +} diff --git a/src/kernel/linker/elf.h b/src/kernel/linker/elf.h index c32cce0..bb61795 100644 --- a/src/kernel/linker/elf.h +++ b/src/kernel/linker/elf.h @@ -52,12 +52,12 @@ struct elf_phdr { }; struct phdr { - struct elf_phdr h; + 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, int privilege); //Creates a new process and a thread for running ELF file +thread_entry elf_load(uint8_t *data, process* process); //Load an ELF to a process, return entry point +process* elf_exec(uint8_t *data, int privilege); //Creates a new process and a thread for running ELF file #endif -- cgit v1.2.3