ElfReader: Support for Section and Symbol size.

- getSection/getSymbol now returns an ElfSymbol reference.

Searching by address now searches if address is within
symbol address and symbol address + size.
So we can test, if we are *within* a function, object or
section and not only at the start address.
This commit is contained in:
Martin Hoffmann
2013-03-04 15:18:52 +01:00
parent 3501050548
commit 4686c27d3d
10 changed files with 185 additions and 218 deletions

View File

@ -5,7 +5,7 @@
#include <stdio.h>
namespace fail {
const std::string Demangler::DEMANGLE_FAILED = "[Demangler] Demangle failed.";
std::string Demangler::demangle(const std::string& name){
@ -13,7 +13,7 @@ namespace fail {
if(res != NULL){
return std::string(res);
}else{
return DEMANGLE_FAILED;
return Demangler::DEMANGLE_FAILED;
}
}

View File

@ -1,10 +1,10 @@
#ifndef __DEMANGLER_HPP
#define __DEMANGLER_HPP
#define __DEMANGLER_HPP
#include <string>
namespace fail {
class Demangler {
public:

View File

@ -2,12 +2,21 @@
#include "sal/SALConfig.hpp"
#include <stdio.h>
#include <cstdlib>
#include <algorithm>
#include "Demangler.hpp"
namespace fail {
const std::string ElfReader::NOTFOUND = "[ELFReader] Function not found.";
const std::string ELF::NOTFOUND = "[ELFReader] Function not found.";
static const ElfSymbol g_SymbolNotFound;
bool operator==(const std::string & str, const ElfSymbol & sym) {
return sym.getName() == str;
}
bool operator==(guest_address_t address, const ElfSymbol & sym) {
return sym.getAddress() == address;
}
ElfReader::ElfReader() : m_log("Fail*Elfinfo", false){
@ -91,13 +100,17 @@ void ElfReader::setup(const char* path) {
free(buff);
fclose(fp);
// printDemangled();
// printSections();
}
int ElfReader::process_section(Elf32_Shdr *sect_hdr, char* sect_name_buff){
// Add section name, start address and size to list
int idx=sect_hdr->sh_name;
m_sections_map.push_back( sect_hdr->sh_addr, sect_hdr->sh_size, sect_name_buff+idx );
// m_sections_map.push_back( sect_hdr->sh_addr, sect_hdr->sh_size, sect_name_buff+idx );
m_sectiontable.push_back( ElfSymbol(sect_name_buff+idx, sect_hdr->sh_addr, sect_hdr->sh_size, ElfSymbol::SECTION) );
return 0;
}
@ -144,103 +157,89 @@ int ElfReader::process_symboltable(int sect_num, FILE* fp){
int type = ELF32_ST_TYPE(mysym.st_info);
if((type != STT_SECTION) && (type != STT_FILE)){
#ifndef __puma
m_bimap_mangled.insert( entry(name_buf+idx, mysym.st_value) );
m_bimap_demangled.insert( entry ( Demangler::demangle(name_buf+idx), mysym.st_value) );
#endif
m_symboltable.push_back( ElfSymbol(name_buf+idx, mysym.st_value, mysym.st_size, ElfSymbol::SYMBOL) );
}
}
free (name_buf);
return 0;
}
guest_address_t ElfReader::getAddressByName(const std::string& name) {
#ifndef __puma
guest_address_t res = getAddress(m_bimap_demangled, name);
if(res == ADDR_INV){
res = getAddress(m_bimap_mangled, name);
const ElfSymbol& ElfReader::getSymbol(guest_address_t address){
for(container_t::const_iterator it = m_symboltable.begin(); it !=m_symboltable.end(); ++it){
if(it->contains(address)){
return *it;
}
}
return res;
#endif
return g_SymbolNotFound;
}
#ifndef __puma
guest_address_t ElfReader::getAddress(const bimap_t& map, const std::string& name){
typedef bimap_t::left_map::const_iterator const_iterator_t;
const_iterator_t iterator = map.left.find(name);
if(iterator == map.left.end()){
return ADDR_INV;
}else{
return iterator->second;
// Symbol search
const ElfSymbol& ElfReader::getSymbol( const std::string& name ){
container_t::const_iterator it;
// Fist, try to find as mangled symbol
it = std::find(m_symboltable.begin(), m_symboltable.end(), name);
if(it != m_symboltable.end()){
return *it;
}
}
#endif
#ifndef __puma
std::string ElfReader::getName(const bimap_t& map, guest_address_t address){
// .right switches key/value
typedef bimap_t::right_map::const_iterator const_iterator_t;
const_iterator_t iterator = map.right.find(address);
if(iterator != map.right.end()){
return iterator->second;
// Then, try to find as demangled symbol
std::string dname = Demangler::demangle(name);
if(dname == Demangler::DEMANGLE_FAILED){
return g_SymbolNotFound;
}
return NOTFOUND;
}
std::string ElfReader::getNameByAddress(guest_address_t address) {
std::string res = getName(m_bimap_demangled, address);
if(res == NOTFOUND){
return getName(m_bimap_mangled, address);
it = std::find(m_symboltable.begin(), m_symboltable.end(), dname);
if(it != m_symboltable.end()){
return *it;
}
return res;
return g_SymbolNotFound;
}
std::string ElfReader::getMangledNameByAddress(guest_address_t address) {
return getName(m_bimap_mangled, address);
// Section search
const ElfSymbol& ElfReader::getSection(guest_address_t address){
for(container_t::const_iterator it = m_sectiontable.begin(); it != m_sectiontable.end(); ++it){
if(it->contains(address)){
return *it;
}
}
return g_SymbolNotFound;
}
std::string ElfReader::getDemangledNameByAddress(guest_address_t address) {
return getName(m_bimap_demangled, address);
const ElfSymbol& ElfReader::getSection( const std::string& name ){
for(container_t::const_iterator it = m_sectiontable.begin(); it !=m_sectiontable.end(); ++it){
if(it->getName() == name){
return *it;
}
}
return g_SymbolNotFound;
}
// "Pretty" Print
void ElfReader::printDemangled(){
print_map(m_bimap_demangled.right); // print Address as first element
m_log << "Demangled: " << std::endl;
for(container_t::const_iterator it = m_symboltable.begin(); it !=m_symboltable.end(); ++it){
std::string str = Demangler::demangle(it->getName());
if(str == Demangler::DEMANGLE_FAILED){
str = it->getName();
}
m_log << "0x" << std::hex << it->getAddress() << "\t" << str.c_str() << "\t" << it->getSize() << std::endl;
}
}
void ElfReader::printMangled(){
print_map(m_bimap_mangled.right); // print Address as first element
}
#endif
#ifndef __puma
std::string ElfReader::getSection(guest_address_t address) {
return m_sections_map.find_name_by(address);
}
guest_address_t ElfReader::getSectionStart(const std::string& sectionname) {
SectionsMap::address_pair_t pair;
pair = m_sections_map.find_range_by(sectionname);
return pair.first;
}
guest_address_t ElfReader::getSectionEnd(const std::string& sectionname) {
SectionsMap::address_pair_t pair = m_sections_map.find_range_by(sectionname);
if ( pair.first == ADDR_INV ) {
return ADDR_INV;
for(container_t::const_iterator it = m_symboltable.begin(); it !=m_symboltable.end(); ++it){
m_log << "0x" << it->getAddress() << "\t" << it->getName().c_str() << "\t" << it->getSize() << std::endl;
}
return pair.first + pair.second;
}
guest_address_t ElfReader::getSectionSize(const std::string& sectionname) {
SectionsMap::address_pair_t pair = m_sections_map.find_range_by(sectionname);
return pair.second;
void ElfReader::printSections() {
for(container_t::const_iterator it = m_sectiontable.begin(); it !=m_sectiontable.end(); ++it){
m_log << "0x" << it->getAddress() << "\t" << it->getName().c_str() << "\t" << it->getSize() << std::endl;
}
}
#endif
} // end-of-namespace fail

View File

@ -2,58 +2,59 @@
#define __ELFREADER_HPP__
#include <string>
#ifndef __puma
#include <boost/bimap.hpp>
#endif
#include <ostream>
#include "sal/SALConfig.hpp" // for ADDR_INV
#include "Logger.hpp"
#include "elfinfo/elfinfo.h"
#include <vector>
#include <map>
#include "Demangler.hpp"
namespace fail {
/*
* Helper struct for section information
*/
struct SectionsMap {
typedef std::pair<guest_address_t, size_t> address_pair_t;
typedef std::vector< std::pair< address_pair_t, std::string> > container ;
container section_to_name;
struct ELF {
static const std::string NOTFOUND;
};
void push_back(guest_address_t start, size_t size, std::string name)
{
section_to_name.push_back(std::make_pair(std::make_pair(start, size), name));
}
class ElfSymbol {
std::string name;
guest_address_t address;
size_t size;
int m_type;
address_pair_t find_range_by(std::string name){
for(container::iterator it = section_to_name.begin(), end = section_to_name.end(); it != end; ++it){
container::value_type pair_pair_string = *it;
typedef container::value_type::first_type section_type;
public:
enum { SECTION = 1, SYMBOL = 2, UNDEFINED = 3, };
if(pair_pair_string.second == name){
return pair_pair_string.first;
}
ElfSymbol(const std::string & name = ELF::NOTFOUND, guest_address_t addr = ADDR_INV, size_t size = -1, int type = UNDEFINED)
: name(name), address(addr), size(size), m_type(type) {};
const std::string& getName() const { return name; };
guest_address_t getAddress() const { return address; };
size_t getSize() const { return size; };
guest_address_t getStart() const { return getAddress(); }; // alias
guest_address_t getEnd() const { return address + size; };
bool isSection() const { return m_type == SECTION; };
bool isSymbol() const { return m_type == SYMBOL; };
bool operator==(const std::string& rhs) const {
if(rhs == name){
return true;
}
return std::make_pair(ADDR_INV, 0);
}
std::string find_name_by(guest_address_t address){
for(container::iterator it = section_to_name.begin(), end = section_to_name.end(); it != end; ++it){
container::value_type pair_pair_string = *it;
typedef container::value_type::first_type section_type;
section_type section = pair_pair_string.first;
if(address >= section.first && address < section.first + section.second){
return pair_pair_string.second;
}
if( rhs == Demangler::demangle(name) ){
return true;
}
return std::string("SECTION_NOT_FOUND");
return false;
}
bool operator==(const guest_address_t rhs) const {
return rhs == address;
}
bool contains(guest_address_t ad) const {
return (ad >= address) && (ad < address+size);
}
};
@ -79,38 +80,6 @@ namespace fail {
*/
ElfReader();
/**
* Get guest address by symbol name.
* Both mangled an demangled symbols are searched.
* @param name The symbol name as string
* @return The according address if found, else ADDR_INV
*/
guest_address_t getAddressByName(const std::string& name) ;
/**
* Get demangled symbol name associated to an address
* This is interesting when checking instruction pointers.
* @param name The address of a symbol (or around a symbol -> instruction pointer)
* @return The according address if found, else ElfReader::NOTFOUND
*/
std::string getNameByAddress(guest_address_t address) ;
/**
* Get the mangled symbol name associated to an address
* @param name The address of a symbol (or around a symbol -> instruction pointer)
* @return The according address if found, else ElfReader::NOTFOUND
*/
std::string getMangledNameByAddress(guest_address_t address) ;
/**
* Get the demangled symbol name associated to an address
* Note the the demangled name is simplified, not showing any types!
* @param name The address of a symbol (or around a symbol -> instruction pointer)
* @return The according address if found, else ElfReader::NOTFOUND
*/
std::string getDemangledNameByAddress(guest_address_t address) ;
/**
* Print the list of available mangled symbols
* @note This includes both C and C++ symbols
@ -118,41 +87,42 @@ namespace fail {
void printMangled();
/**
* Print the list of all available demangled symbols
* @note These are only C++ symbols.
* Print a list of demangled symbols.
*/
void printDemangled();
//! Default string, if symbol is not found
static const std::string NOTFOUND;
/**
* Get the name of a section
* @param address The address of the section
* @return The according section name if section was found, else SECTION_NOT_FOUND
* Print the list of all available sections.
*/
std::string getSection(guest_address_t address);
void printSections();
/**
* Get the start address of a section
* Get symbol by address
* @param address Address within range of the symbol
* @return The according symbol name if symbol.address <= address < symbol.address + symbol.size , else g_SymbolNotFound
*/
const ElfSymbol& getSymbol(guest_address_t address);
/**
* Get symbol by name
* @param address Name of the symbol
* @return The according symbol name if section was found, else g_SymbolNotFound
*/
const ElfSymbol& getSymbol( const std::string& name );
/**
* Get section by address
* @param address An address to search for a section containing that address.
* @return The according section name if section was found, else g_SymbolNotFound
*/
const ElfSymbol& getSection(guest_address_t address);
/**
* Get section by name
* @param name The name of the section
* @return The according section start if section was found, else ADDR_INV
* @return The according section if section was found, else g_SymbolNotFound
*/
guest_address_t getSectionStart(const std::string& name);
/**
* Get the end address of a section
* @param name The name of the section
* @return The according section end if section was found, else ADDR_INV
*/
guest_address_t getSectionEnd(const std::string& name);
/**
* Get the size of a section
* @param name The name of the section
* @return The according section sizh if section was found, else ADDR_INV
*/
guest_address_t getSectionSize(const std::string& name);
const ElfSymbol& getSection( const std::string& name );
private:
Logger m_log;
@ -161,27 +131,14 @@ namespace fail {
int process_symboltable(int sect_num, FILE* fp);
int process_section(Elf32_Shdr *sect_hdr, char* sect_name_buff);
fail::SectionsMap m_sections_map;
typedef ElfSymbol entry_t;
typedef std::vector<entry_t> container_t;
#ifndef __puma
typedef boost::bimap< std::string, guest_address_t > bimap_t;
typedef bimap_t::value_type entry;
container_t m_symboltable;
container_t m_sectiontable;
bimap_t m_bimap_mangled;
bimap_t m_bimap_demangled;
template < typename MapType >
void print_map(const MapType & m){
typedef typename MapType::const_iterator const_iterator;
for( const_iterator iter = m.begin(), iend = m.end(); iter != iend; ++iter )
{
m_log << std::hex << iter->first << " \t "<< std::hex << iter->second << std::endl;
}
}
guest_address_t getAddress(const bimap_t& map, const std::string& name);
std::string getName(const bimap_t& map, guest_address_t address);
#endif
guest_address_t getAddress(const std::string& name);
std::string getName(guest_address_t address);
};
} // end-of-namespace fail

View File

@ -25,7 +25,7 @@ public:
* @param dest Stream to log into.
*/
Logger(const std::string& description = "Fail*", bool show_time = true,
std::ostream& dest = std::cout)
std::ostream& dest = std::cout)
: m_pDest(&dest), m_description(description), m_showTime(show_time) { }
/**
* Change the default description which is shown alongside each log