Reverse search getNameByAddress.

Implemented with the help of boost bimap.



git-svn-id: https://www4.informatik.uni-erlangen.de/i4svn/danceos/trunk/devel/fail@1983 8c4709b5-6ec9-48aa-a5cd-a96041d1645a
This commit is contained in:
hoffmann
2012-12-20 22:06:46 +00:00
parent f8aa1237e9
commit 5a3a66da25
2 changed files with 169 additions and 137 deletions

View File

@ -6,7 +6,7 @@
namespace fail { namespace fail {
ElfReader::ElfReader(const char* path) : m_log("Fail*Elfinfo", false){ ElfReader::ElfReader(const char* path) : m_log("Fail*Elfinfo", false){
// Try to open the ELF file // Try to open the ELF file
FILE * fp = fopen(path, "r"); FILE * fp = fopen(path, "r");
if (!fp) { if (!fp) {
@ -44,9 +44,9 @@ ElfReader::ElfReader(const char* path) : m_log("Fail*Elfinfo", false){
} }
fclose(fp); fclose(fp);
} }
int ElfReader::process_symboltable(int sect_num, FILE* fp){ int ElfReader::process_symboltable(int sect_num, FILE* fp){
Elf32_Shdr sect_hdr; Elf32_Shdr sect_hdr;
Elf32_Sym mysym; Elf32_Sym mysym;
@ -89,28 +89,43 @@ int ElfReader::process_symboltable(int sect_num, FILE* fp){
int type = ELF32_ST_TYPE(mysym.st_info); int type = ELF32_ST_TYPE(mysym.st_info);
if((type != STT_SECTION) && (type != STT_FILE)){ if((type != STT_SECTION) && (type != STT_FILE)){
//m_log << " " << (i) << " " << name_buf+idx << " @ " << mysym.st_value << std::endl; m_log << " " << (i) << " " << name_buf+idx << " @ " << mysym.st_value << std::endl;
m_map[name_buf+idx] = mysym.st_value; #ifndef __puma
m_bimap.insert( entry(name_buf+idx, mysym.st_value) );
#endif
} }
} }
free (name_buf); free (name_buf);
return 0; return 0;
} }
guest_address_t ElfReader::getAddressByName(const std::string& name) { guest_address_t ElfReader::getAddressByName(const std::string& name) {
if( m_map.find(name) == m_map.end() ) { #ifndef __puma
typedef bimap_t::left_map::const_iterator const_iterator_t;
const_iterator_t iterator = m_bimap.left.find(name);
if(iterator == m_bimap.left.end()){
return ADDR_INV; return ADDR_INV;
}else{ }else{
return m_map[name]; return iterator->second;
}
#endif
} }
}
std::string getNameByAddress(guest_address_t address) { std::string ElfReader::getNameByAddress(guest_address_t address) {
return "Test"; #ifndef __puma
} // .right switches key/value
typedef bimap_t::right_map::const_iterator const_iterator_t;
const_iterator_t iterator = m_bimap.right.find(address);
if(iterator == m_bimap.right.end()){
return "[ElfReader] FUNCTION NOT FOUND";
}else{
return iterator->second;
}
#endif
}
} // end-of-namespace fail } // end-of-namespace fail

View File

@ -1,23 +1,36 @@
#ifndef __ELFREADER_HPP__ #ifndef __ELFREADER_HPP__
#define __ELFREADER_HPP__ #define __ELFREADER_HPP__
#include <string> #include <string>
#include <map> #ifndef __puma
#include <boost/bimap.hpp>
#endif
#include "sal/SALConfig.hpp" #include "sal/SALConfig.hpp"
#include "Logger.hpp" #include "Logger.hpp"
template< class MapType >
void print_map(const MapType & map)
{
typedef typename MapType::const_iterator const_iterator;
for( const_iterator i = map.begin(), iend = map.end(); i != iend; ++i )
{
std::cout << i->first << " -- " << i->second << std::endl;
}
}
namespace fail { namespace fail {
/** /**
* \class ElfReader * \class ElfReader
* Parses an ELF file and provides a list of symbol names * Parses an ELF file and provides a list of symbol names
* and corresponding addresses * and corresponding addresses
*/ */
class ElfReader { class ElfReader {
public: public:
/** /**
* Constructor. * Constructor.
@ -45,12 +58,16 @@ public:
std::string getNameByAddress(guest_address_t address) ; std::string getNameByAddress(guest_address_t address) ;
private: private:
Logger m_log; Logger m_log;
std::map<std::string, guest_address_t> m_map;
#ifndef __puma
typedef boost::bimap< std::string, guest_address_t > bimap_t;
typedef bimap_t::value_type entry;
bimap_t m_bimap;
#endif
int process_symboltable(int sect_num, FILE* fp); int process_symboltable(int sect_num, FILE* fp);
}; };
} // end-of-namespace fail } // end-of-namespace fail