Aesalon now parses libc to get the correct address to use for malloc-alikes.
[aesalon.git] / src / monitor / elf / SymbolParser.cpp
blob86e88b4e566edb8a904283b9f28de2b1eaac7723
1 #include <iostream>
3 #include "SymbolParser.h"
4 #include "Parser.h"
6 namespace Aesalon {
7 namespace Monitor {
8 namespace ELF {
10 SymbolParser::SymbolParser(Misc::SmartPointer<Parser> elf_parser) : elf_parser(elf_parser) {
11 std::cout << std::hex;
12 /* Start off with the static symbols . . . */
13 Misc::SmartPointer<Section> symbol_table = elf_parser->get_section(".symtab");
14 if(symbol_table.is_valid()) { /* Only try to parse the symbols if the .symtab section exists */
15 Misc::SmartPointer<Block> symbol_block = symbol_table->get_content();
16 while(symbol_block->get_size()) {
17 Elf64_Sym sym;
18 symbol_block->read(&sym, sizeof(sym));
20 char *name = (char *)elf_parser->get_section(".strtab")->get_content()->get_data(sym.st_name);
21 if(sym.st_value) symbol_vector.push_back(new Symbol(name, sym.st_value, sym.st_size));
24 symbol_table = elf_parser->get_section(".rela.plt");
25 if(symbol_table.is_valid()) {
26 symbol_table->read_content();
27 Misc::SmartPointer<Block> address_table = elf_parser->get_section(".rela.plt")->get_content();
28 while(address_table->get_size()) {
29 Elf64_Rela rela;
30 address_table->read(&rela, sizeof(rela));
31 Word symbol = ELF64_R_SYM(rela.r_info);
32 if(symbol > dynamic_offsets.size()) dynamic_offsets.resize(symbol * 2);
33 dynamic_offsets[symbol] = rela.r_offset;
34 /*std::cout << "Dynamic offset parsed:" << std::hex << std::endl;
35 std::cout << "\tsymbol: " << symbol << std::endl;
36 std::cout << "\tr_offset: " << rela.r_offset << std::endl;
37 std::cout << "\tr_addend: " << rela.r_addend << std::endl;*/
40 Misc::SmartPointer<Block> symbol_block;
41 /* Now for the dynamic symbols. Otherwise known as the tricky ones. */
42 symbol_block = elf_parser->get_section(".dynsym")->get_content();
43 std::size_t index = 0;
44 while(symbol_block->get_size()) {
45 Elf64_Sym sym;
46 symbol_block->read(&sym, sizeof(sym));
48 char *name = (char *)elf_parser->get_section(".dynstr")->get_content()->get_data(sym.st_name);
49 /*std::cout << "Dynamic symbol: name is \"" << name << "\", address is " << sym.st_value << std::endl;
50 std::cout << "Dynamic offset for this symbol is: " << dynamic_offsets[index] << std::endl;*/
51 if(sym.st_name) symbol_vector.push_back(new Symbol(name, dynamic_offsets[index], sym.st_size));
52 index ++;
55 std::cout << std::dec;
58 Misc::SmartPointer<Symbol> SymbolParser::get_symbol(std::string name) const {
59 for(symbol_vector_t::const_iterator i = symbol_vector.begin(); i != symbol_vector.end(); i ++) {
60 if((*i)->get_symbol_name() == name) return *i;
62 return NULL;
65 void SymbolParser::dump_symbols() const {
66 std::cout << "Symbol dump: \n";
67 for(symbol_vector_t::const_iterator i = symbol_vector.begin(); i != symbol_vector.end(); i ++) {
68 std::cout << "\tSymbol: name is \"" << (*i)->get_symbol_name() << "\", address is 0x" << std::hex << (*i)->get_address() << std::dec << std::endl;
72 } // namespace ELF
73 } // namespace Monitor
74 } // namespace Aesalon