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

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