ElfReader: add some convenience functions

This commit is contained in:
Christian Dietrich
2013-03-06 17:15:32 +01:00
parent 532ec87b27
commit c1f32f5a98
3 changed files with 39 additions and 7 deletions

View File

@ -11,6 +11,8 @@
#include "Event.hpp"
#include "ConcreteCPU.hpp"
#include "perf/BufferInterface.hpp"
#include "util/ElfReader.hpp"
namespace fail {
@ -301,6 +303,11 @@ public:
MemAccessEvent::access_type_t type = MemAccessEvent::MEM_READWRITE,
ConcreteCPU* cpu = NULL)
: BaseListener(cpu), m_WatchAddr(addr), m_WatchWidth(1), m_WatchType(type) { }
MemAccessListener(const ElfSymbol &symbol,
MemAccessEvent::access_type_t type = MemAccessEvent::MEM_READWRITE,
ConcreteCPU* cpu = NULL)
: BaseListener(cpu), m_WatchAddr(symbol.getAddress()), m_WatchWidth(symbol.getSize()), m_WatchType(type) { }
/**
* Returns the physical memory address to be observed.
*/

View File

@ -18,6 +18,13 @@ bool operator==(guest_address_t address, const ElfSymbol & sym) {
return sym.getAddress() == address;
}
std::ostream& operator<< (std::ostream &out, const ElfSymbol &symbol) {
return (out << symbol.getName()
<< " @ 0x" << std::hex << symbol.getAddress()
<< " size " << std::dec << symbol.getSize());
}
ElfReader::ElfReader() : m_log("Fail*Elfinfo", false){
// try to open elf file from environment variable
@ -157,7 +164,8 @@ int ElfReader::process_symboltable(int sect_num, FILE* fp){
int type = ELF32_ST_TYPE(mysym.st_info);
if((type != STT_SECTION) && (type != STT_FILE)){
m_symboltable.push_back( ElfSymbol(name_buf+idx, mysym.st_value, mysym.st_size, ElfSymbol::SYMBOL) );
m_symboltable.push_back( ElfSymbol(name_buf+idx, mysym.st_value, mysym.st_size, ElfSymbol::SYMBOL,
type) );
}
}
free (name_buf);

View File

@ -21,21 +21,26 @@ namespace fail {
guest_address_t address;
size_t size;
int m_type;
int m_symbol_type;
public:
enum { SECTION = 1, SYMBOL = 2, UNDEFINED = 3, };
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) {};
ElfSymbol(const std::string & name = ELF::NOTFOUND, guest_address_t addr = ADDR_INV, size_t size = -1, int type = UNDEFINED,
int symbol_type = 0)
: name(name), address(addr), size(size), m_type(type), m_symbol_type(symbol_type) {};
const std::string& getName() const { return name; };
std::string getDemangledName() const { return Demangler::demangle(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; };
int getSymbolType() const { return m_symbol_type; };
bool isSection() const { return m_type == SECTION; };
bool isSymbol() const { return m_type == SYMBOL; };
bool isValid() const { return name != ELF::NOTFOUND; };
bool operator==(const std::string& rhs) const {
if(rhs == name){
@ -56,7 +61,12 @@ namespace fail {
return (ad >= address) && (ad < address+size);
}
};
/**
* \fn
* \relates ElfSymbol
* overloaded stream operator for printing ElfSymbol
*/
std::ostream& operator<< (std::ostream &out, const ElfSymbol &symbol);
/**
* \class ElfReader
@ -67,6 +77,9 @@ namespace fail {
class ElfReader {
public:
typedef ElfSymbol entry_t;
typedef std::vector<entry_t> container_t;
typedef container_t::const_iterator symbol_iterator;
/**
* Constructor.
@ -124,6 +137,13 @@ namespace fail {
*/
const ElfSymbol& getSection( const std::string& name );
/**
* Get symboltable iterator. Derefences to a ElfSymbol
* @return iterator
*/
container_t::const_iterator sym_begin() { return m_symboltable.begin(); }
container_t::const_iterator sym_end() { return m_symboltable.end(); }
private:
Logger m_log;
@ -131,9 +151,6 @@ namespace fail {
int process_symboltable(int sect_num, FILE* fp);
int process_section(Elf32_Shdr *sect_hdr, char* sect_name_buff);
typedef ElfSymbol entry_t;
typedef std::vector<entry_t> container_t;
container_t m_symboltable;
container_t m_sectiontable;