From 5a3a66da25893377db818de090ae6478618761e8 Mon Sep 17 00:00:00 2001 From: hoffmann Date: Thu, 20 Dec 2012 22:06:46 +0000 Subject: [PATCH] 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 --- src/core/util/ElfReader.cc | 209 +++++++++++++++++++----------------- src/core/util/ElfReader.hpp | 97 ++++++++++------- 2 files changed, 169 insertions(+), 137 deletions(-) diff --git a/src/core/util/ElfReader.cc b/src/core/util/ElfReader.cc index a9a0970f..8a7b8fa8 100644 --- a/src/core/util/ElfReader.cc +++ b/src/core/util/ElfReader.cc @@ -6,111 +6,126 @@ namespace fail { -ElfReader::ElfReader(const char* path) : m_log("Fail*Elfinfo", false){ - // Try to open the ELF file - FILE * fp = fopen(path, "r"); - if (!fp) { - m_log << "Error: Could not open " << path << std::endl; - return; - } + ElfReader::ElfReader(const char* path) : m_log("Fail*Elfinfo", false){ + // Try to open the ELF file + FILE * fp = fopen(path, "r"); + if (!fp) { + m_log << "Error: Could not open " << path << std::endl; + return; + } - // Evaluate headers - Elf32_Ehdr ehdr; - Elf32_Shdr sec_hdr; - int num_hdrs,i; - fseek(fp,(off_t)0,SEEK_SET); - read_ELF_file_header(fp, &ehdr); - num_hdrs=ehdr.e_shnum; - m_log << "Evaluating ELF File: " << path << std::endl; - // Parse symbol table and generate internal map - for(i=0;isecond; + } +#endif + } + + std::string ElfReader::getNameByAddress(guest_address_t address) { +#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 diff --git a/src/core/util/ElfReader.hpp b/src/core/util/ElfReader.hpp index eeb1eb9f..0c9e9374 100644 --- a/src/core/util/ElfReader.hpp +++ b/src/core/util/ElfReader.hpp @@ -1,56 +1,73 @@ #ifndef __ELFREADER_HPP__ - #define __ELFREADER_HPP__ +#define __ELFREADER_HPP__ #include -#include +#ifndef __puma +#include +#endif #include "sal/SALConfig.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 { -/** - * \class ElfReader - * Parses an ELF file and provides a list of symbol names - * and corresponding addresses - */ - -class ElfReader { - -public: - - /** - * Constructor. - * @param path Path to the ELF file. - */ - ElfReader(const char* path); - /** - * Get guest address by symbol name - * @param name The symbol name as string - * @return The according addres if found, else -1 - */ - guest_address_t getAddressByName(const std::string& name) ; - - - /** - * Get 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 -1 - * - * \todo multimap sorted by addresses - * Name is at first key <= address + * \class ElfReader + * Parses an ELF file and provides a list of symbol names + * and corresponding addresses */ - std::string getNameByAddress(guest_address_t address) ; - -private: - Logger m_log; - std::map m_map; + class ElfReader { - int process_symboltable(int sect_num, FILE* fp); -}; + public: + + /** + * Constructor. + * @param path Path to the ELF file. + */ + ElfReader(const char* path); + + /** + * Get guest address by symbol name + * @param name The symbol name as string + * @return The according addres if found, else -1 + */ + guest_address_t getAddressByName(const std::string& name) ; + + + /** + * Get 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 -1 + * + * \todo multimap sorted by addresses + * Name is at first key <= address + */ + std::string getNameByAddress(guest_address_t address) ; + + + private: + Logger m_log; + +#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); + }; } // end-of-namespace fail