2#include <elfio/elfio.hpp>
7uint64_t load_elf_file(std::string
const& name, std::function<
bool(uint64_t, uint64_t,
const uint8_t*)> cb, uint8_t expected_elf_class,
8 uint16_t expected_elf_machine) {
12 if(!reader.load(name))
13 throw std::runtime_error(
"Could not load file");
15 if(reader.get_class() != expected_elf_class)
16 throw std::runtime_error(
"ELF Class missmatch");
17 if(reader.get_type() != ELFIO::ET_EXEC && reader.get_type() != ELFIO::ET_DYN)
18 throw std::runtime_error(
"Input is neither an executable nor a pie executable (dyn)");
19 if(reader.get_machine() != expected_elf_machine)
20 throw std::runtime_error(
"ELF Machine type missmatch");
21 auto entry_address = reader.get_entry();
22 for(
const auto& pseg : reader.segments) {
23 const auto fsize = pseg->get_file_size();
24 const auto seg_data = pseg->get_data();
25 const auto type = pseg->get_type();
26 if(type == ELFIO::PT_LOAD && fsize > 0) {
27 if(cb(pseg->get_physical_address(), fsize,
reinterpret_cast<const uint8_t* const
>(seg_data))) {
28 std::ostringstream oss;
29 oss <<
"Problem writing " << fsize <<
" bytes to 0x" << std::hex << pseg->get_physical_address();
30 throw std::runtime_error(oss.str());
37std::unordered_map<std::string, uint64_t> read_elf_symbols(std::string
const& name, uint8_t expected_elf_class,
38 uint16_t expected_elf_machine) {
42 if(!reader.load(name))
43 throw std::runtime_error(
"Could not load file");
45 if(reader.get_class() != expected_elf_class)
46 throw std::runtime_error(
"ELF Class missmatch");
47 if(reader.get_type() != ELFIO::ET_EXEC && reader.get_type() != ELFIO::ET_DYN)
48 throw std::runtime_error(
"Input is neither an executable nor a pie executable (dyn)");
49 if(reader.get_machine() != expected_elf_machine)
50 throw std::runtime_error(
"ELF Machine type missmatch");
51 std::unordered_map<std::string, uint64_t> symbol_table;
52 const auto sym_sec = reader.sections[
".symtab"];
53 if(ELFIO::SHT_SYMTAB == sym_sec->get_type() || ELFIO::SHT_DYNSYM == sym_sec->get_type()) {
54 ELFIO::symbol_section_accessor symbols(reader, sym_sec);
55 auto sym_no = symbols.get_symbols_num();
57 ELFIO::Elf64_Addr value = 0;
58 ELFIO::Elf_Xword size = 0;
59 unsigned char bind = 0;
60 unsigned char type = 0;
61 ELFIO::Elf_Half section = 0;
62 unsigned char other = 0;
63 for(
auto i = 0U; i < sym_no; ++i) {
64 symbols.get_symbol(i, name, value, size, bind, type, section, other);
66 symbol_table[name] = value;
70 return std::move(symbol_table);