diff --git a/kernel/include/elf.hpp b/kernel/include/elf.hpp new file mode 100644 index 00000000..ce40adaa --- /dev/null +++ b/kernel/include/elf.hpp @@ -0,0 +1,61 @@ +//======================================================================= +// Copyright Baptiste Wicht 2013. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +//======================================================================= + +#ifndef ELF_H +#define ELF_H + +#include "stl/types.hpp" +#include "stl/string.hpp" + +namespace elf { + +struct elf_header { + char e_ident[16]; + uint16_t e_type; + uint16_t e_machine; + uint32_t e_version; + uint64_t e_entry; + uint64_t e_phoff; + uint64_t e_shoff; + uint32_t e_flags; + uint16_t e_ehsize; + uint16_t e_phentsize; + uint16_t e_phnum; + uint16_t e_shentsize; + uint16_t e_shnum; + uint16_t e_shstrndx; +}__attribute__((packed)); + +struct program_header { + uint32_t p_type; + uint32_t p_flags; + uint64_t p_offset; + uint64_t p_vaddr; + uint64_t p_paddr; + uint64_t p_filesize; + uint64_t p_memsz; + uint64_t p_align; +}__attribute__((packed)); + +struct section_header { + uint32_t sh_name; + uint32_t sh_type; + uint64_t sh_flags; + uint64_t sh_addr; + uint64_t sh_offset; + uint64_t sh_size; + uint32_t sh_link; + uint32_t sh_info; + uint64_t sh_addralign; + uint64_t sh_entsize; +}__attribute__((packed)); + +bool is_valid(const std::string& content); + +} //end of namespace elf + +#endif diff --git a/kernel/src/elf.cpp b/kernel/src/elf.cpp new file mode 100644 index 00000000..afb5b85e --- /dev/null +++ b/kernel/src/elf.cpp @@ -0,0 +1,25 @@ +//======================================================================= +// Copyright Baptiste Wicht 2013. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +//======================================================================= + +#include "elf.hpp" + +bool elf::is_valid(const std::string& content){ + auto buffer = content.c_str(); + auto header = reinterpret_cast(buffer); + + //Test if ELF file + if(header->e_ident[0] == 0x7F && header->e_ident[1] == 'E' && + header->e_ident[2] == 'L' && header->e_ident[3] == 'F'){ + + //Test if ELF64 + if(header->e_ident[4] == 2){ + return true; + } + } + + return false; +} diff --git a/kernel/src/shell.cpp b/kernel/src/shell.cpp index 135544ba..d9eb151e 100644 --- a/kernel/src/shell.cpp +++ b/kernel/src/shell.cpp @@ -15,6 +15,7 @@ #include "acpi.hpp" #include "e820.hpp" #include "rtc.hpp" +#include "elf.hpp" //Commands #include "sysinfo.hpp" @@ -635,63 +636,6 @@ void rm_command(const std::vector& params){ } } -struct elf_header { - char e_ident[16]; - uint16_t e_type; - uint16_t e_machine; - uint32_t e_version; - uint64_t e_entry; - uint64_t e_phoff; - uint64_t e_shoff; - uint32_t e_flags; - uint16_t e_ehsize; - uint16_t e_phentsize; - uint16_t e_phnum; - uint16_t e_shentsize; - uint16_t e_shnum; - uint16_t e_shstrndx; -}__attribute__((packed)); - -struct program_header { - uint32_t p_type; - uint32_t p_flags; - uint64_t p_offset; - uint64_t p_vaddr; - uint64_t p_paddr; - uint64_t p_filesize; - uint64_t p_memsz; - uint64_t p_align; -}__attribute__((packed)); - -struct section_header { - uint32_t sh_name; - uint32_t sh_type; - uint64_t sh_flags; - uint64_t sh_addr; - uint64_t sh_offset; - uint64_t sh_size; - uint32_t sh_link; - uint32_t sh_info; - uint64_t sh_addralign; - uint64_t sh_entsize; -}__attribute__((packed)); - -bool is_valid_elf_file(const std::string& content){ - auto buffer = content.c_str(); - - //Test if ELF file - if(header->e_ident[0] == 0x7F && header->e_ident[1] == 'E' && - header->e_ident[2] == 'L' && header->e_ident[3] == 'F'){ - - //Test if ELF64 - if(header->e_ident[4] == 2){ - return true; - } - } - - return false; -} - void readelf_command(const std::vector& params){ if(params.size() < 2){ k_print_line("readelf: Need the name of the executable to read"); @@ -713,20 +657,20 @@ void readelf_command(const std::vector& params){ return; } - if(!is_valid_elf_file(content)){ + if(!elf::is_valid(content)){ k_print_line("readelf: This file is not an ELF file or not in ELF64 format"); return; } auto buffer = content.c_str(); - auto header = reinterpret_cast(buffer); + auto header = reinterpret_cast(buffer); k_printf("Number of Program Headers: %u\n", static_cast(header->e_phnum)); k_printf("Number of Section Headers: %u\n", static_cast(header->e_shnum)); - auto program_header_table = reinterpret_cast(buffer + header->e_phoff); - auto section_header_table = reinterpret_cast(buffer + header->e_shoff); + auto program_header_table = reinterpret_cast(buffer + header->e_phoff); + auto section_header_table = reinterpret_cast(buffer + header->e_shoff); auto& string_table_header = section_header_table[header->e_shstrndx]; auto string_table = buffer + string_table_header.sh_offset; diff --git a/programs/one/a.out b/programs/one/a.out index e3f2b791..d5281b90 100755 Binary files a/programs/one/a.out and b/programs/one/a.out differ