Began removal of platform/. The Monitor:: namespace is completely converted.
[aesalon.git] / src / monitor / elf / SymbolParser.cpp
blob760b28a737d401e852557633229b8f6cffc80229
1 #include <iostream>
2 #include <cstring>
4 #include "SymbolParser.h"
5 #include "Parser.h"
7 namespace Aesalon {
8 namespace Monitor {
9 namespace ELF {
11 SymbolParser::SymbolParser(Misc::SmartPointer<Parser> elf_parser) : elf_parser(elf_parser) {
12 std::cout << std::hex;
13 /* Start off with the static symbols . . . */
14 Misc::SmartPointer<Section> symbol_table = elf_parser->get_section(".symtab");
15 if(symbol_table.is_valid()) { /* Only try to parse the symbols if the .symtab section exists */
16 Misc::SmartPointer<Block> symbol_block = symbol_table->get_content();
17 while(symbol_block->get_size()) {
18 Elf64_Sym sym;
19 symbol_block->read(&sym, sizeof(sym));
21 char *name = (char *)elf_parser->get_section(".strtab")->get_content()->get_data(sym.st_name);
22 if(sym.st_value) symbol_vector.push_back(new Symbol(name, sym.st_value, sym.st_size));
25 symbol_table = elf_parser->get_section(".rela.plt");
26 if(symbol_table.is_valid()) {
27 symbol_table->read_content();
28 /*Misc::SmartPointer<Block> address_table = elf_parser->get_section(".rela.plt")->get_content();
29 while(address_table->get_size()) {
30 Elf64_Rela rela;
31 address_table->read(&rela, sizeof(rela));
32 Word symbol = ELF64_R_SYM(rela.r_info);
33 if(symbol > dynamic_offsets.size()) dynamic_offsets.resize(symbol * 2);
34 dynamic_offsets[symbol] = rela.r_offset;
35 std::cout << "Dynamic offset parsed:" << std::hex << std::endl;
36 std::cout << "\tsymbol: " << symbol << std::endl;
37 std::cout << "\tr_offset: " << rela.r_offset << std::endl;
38 std::cout << "\tr_addend: " << rela.r_addend << std::endl;
39 }*/
41 Misc::SmartPointer<Block> symbol_block;
42 /* Now for the dynamic symbols. Otherwise known as the tricky ones. */
43 symbol_block = elf_parser->get_section(".dynsym")->get_content();
44 std::size_t index = 0;
45 while(symbol_block->get_size()) {
46 Elf64_Sym sym;
47 symbol_block->read(&sym, sizeof(sym));
49 char *name = (char *)elf_parser->get_section(".dynstr")->get_content()->get_data(sym.st_name);
50 /*if(!std::strcmp(name, "malloc")) {
51 std::cout << "malloc symbol:" << std::endl;
52 std::cout << "\tdynamic_offset is: " << dynamic_offsets[index] << std::endl;
53 std::cout << "\tvalue is " << sym.st_value << std::endl;
54 }*/
55 /*std::cout << "Dynamic symbol: name is \"" << name << "\", address is " << sym.st_value << std::endl;
56 std::cout << "Dynamic offset for this symbol is: " << dynamic_offsets[index] << std::endl;*/
57 if(sym.st_name) symbol_vector.push_back(new Symbol(name, sym.st_value/* + dynamic_offsets[index]*/, sym.st_size));
58 index ++;
61 std::cout << std::dec;
64 Misc::SmartPointer<Symbol> SymbolParser::get_symbol(std::string name) const {
65 for(symbol_vector_t::const_iterator i = symbol_vector.begin(); i != symbol_vector.end(); i ++) {
66 if((*i)->get_symbol_name() == name) return *i;
68 return NULL;
71 void SymbolParser::dump_symbols() const {
72 std::cout << "Symbol dump: \n";
73 for(symbol_vector_t::const_iterator i = symbol_vector.begin(); i != symbol_vector.end(); i ++) {
74 std::cout << "\tSymbol: name is \"" << (*i)->get_symbol_name() << "\", address is 0x" << std::hex << (*i)->get_address() << std::dec << std::endl;
78 } // namespace ELF
79 } // namespace Monitor
80 } // namespace Aesalon