Cleanup a bit

This commit is contained in:
Baptiste Wicht 2014-01-05 21:14:39 +01:00
parent 9e9175ca6b
commit 4d33c3eea7
4 changed files with 91 additions and 61 deletions

61
kernel/include/elf.hpp Normal file
View File

@ -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

25
kernel/src/elf.cpp Normal file
View File

@ -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<const elf::elf_header*>(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;
}

View File

@ -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<std::string>& 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<std::string>& 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<std::string>& 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<elf_header*>(buffer);
auto header = reinterpret_cast<elf::elf_header*>(buffer);
k_printf("Number of Program Headers: %u\n", static_cast<uint64_t>(header->e_phnum));
k_printf("Number of Section Headers: %u\n", static_cast<uint64_t>(header->e_shnum));
auto program_header_table = reinterpret_cast<program_header*>(buffer + header->e_phoff);
auto section_header_table = reinterpret_cast<section_header*>(buffer + header->e_shoff);
auto program_header_table = reinterpret_cast<elf::program_header*>(buffer + header->e_phoff);
auto section_header_table = reinterpret_cast<elf::section_header*>(buffer + header->e_shoff);
auto& string_table_header = section_header_table[header->e_shstrndx];
auto string_table = buffer + string_table_header.sh_offset;

Binary file not shown.