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,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;i<num_hdrs;i++)
{
if(read_ELF_section_header(i,&sec_hdr,fp)==-1)
{
m_log << "Wrong Section to read" << std::endl;
}
else
{
if((sec_hdr.sh_type==SHT_SYMTAB)||(sec_hdr.sh_type==SHT_DYNSYM))
{
process_symboltable(i,fp);
// 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;i<num_hdrs;i++)
{
if(read_ELF_section_header(i,&sec_hdr,fp)==-1)
{
m_log << "Wrong Section to read" << std::endl;
}
else
{
if((sec_hdr.sh_type==SHT_SYMTAB)||(sec_hdr.sh_type==SHT_DYNSYM))
{
process_symboltable(i,fp);
}
else
{
continue;
}
}
}
}
else
{
continue;
}
}
}
fclose(fp);
}
int ElfReader::process_symboltable(int sect_num, FILE* fp){
Elf32_Shdr sect_hdr;
Elf32_Sym mysym;
char *name_buf=NULL;
int num_sym,link,i,idx;
off_t sym_data_offset;
int sym_data_size;
if(read_ELF_section_header(sect_num,&sect_hdr,fp)==-1)
{
return -1;
fclose(fp);
}
//we have to check to which strtab it is linked
link=sect_hdr.sh_link;
sym_data_offset=sect_hdr.sh_offset;
sym_data_size=sect_hdr.sh_size;
num_sym=sym_data_size/sizeof(Elf32_Sym);
//read the coresponding strtab
if(read_ELF_section_header(link,&sect_hdr,fp)==-1)
{
return -1;
int ElfReader::process_symboltable(int sect_num, FILE* fp){
Elf32_Shdr sect_hdr;
Elf32_Sym mysym;
char *name_buf=NULL;
int num_sym,link,i,idx;
off_t sym_data_offset;
int sym_data_size;
if(read_ELF_section_header(sect_num,&sect_hdr,fp)==-1)
{
return -1;
}
//we have to check to which strtab it is linked
link=sect_hdr.sh_link;
sym_data_offset=sect_hdr.sh_offset;
sym_data_size=sect_hdr.sh_size;
num_sym=sym_data_size/sizeof(Elf32_Sym);
//read the coresponding strtab
if(read_ELF_section_header(link,&sect_hdr,fp)==-1)
{
return -1;
}
//get the size of strtab in file and allocate a buffer
name_buf=(char*)malloc(sect_hdr.sh_size);
if(!name_buf)
return -1;
//get the offset of strtab in file and seek to it
fseek(fp,sect_hdr.sh_offset,SEEK_SET);
//read all data from the section to the buffer.
fread(name_buf,sect_hdr.sh_size,1,fp);
//so we have the namebuf now seek to symtab data
fseek(fp,sym_data_offset,SEEK_SET);
//m_log << "[section " << sect_num << "] contains " << num_sym << " symbols." << std::endl;
for(i=0;i<num_sym;i++)
{
fread(&mysym,sizeof(Elf32_Sym),1,fp);
idx=mysym.st_name;
int type = ELF32_ST_TYPE(mysym.st_info);
if((type != STT_SECTION) && (type != STT_FILE)){
m_log << " " << (i) << " " << name_buf+idx << " @ " << mysym.st_value << std::endl;
#ifndef __puma
m_bimap.insert( entry(name_buf+idx, mysym.st_value) );
#endif
}
}
free (name_buf);
return 0;
}
//get the size of strtab in file and allocate a buffer
name_buf=(char*)malloc(sect_hdr.sh_size);
if(!name_buf)
return -1;
//get the offset of strtab in file and seek to it
fseek(fp,sect_hdr.sh_offset,SEEK_SET);
//read all data from the section to the buffer.
fread(name_buf,sect_hdr.sh_size,1,fp);
//so we have the namebuf now seek to symtab data
fseek(fp,sym_data_offset,SEEK_SET);
//m_log << "[section " << sect_num << "] contains " << num_sym << " symbols." << std::endl;
for(i=0;i<num_sym;i++)
{
fread(&mysym,sizeof(Elf32_Sym),1,fp);
idx=mysym.st_name;
int type = ELF32_ST_TYPE(mysym.st_info);
if((type != STT_SECTION) && (type != STT_FILE)){
//m_log << " " << (i) << " " << name_buf+idx << " @ " << mysym.st_value << std::endl;
m_map[name_buf+idx] = mysym.st_value;
}
}
free (name_buf);
return 0;
}
guest_address_t ElfReader::getAddressByName(const std::string& name) {
if( m_map.find(name) == m_map.end() ) {
return ADDR_INV;
}else{
return m_map[name];
}
}
guest_address_t ElfReader::getAddressByName(const std::string& name) {
#ifndef __puma
typedef bimap_t::left_map::const_iterator const_iterator_t;
std::string getNameByAddress(guest_address_t address) {
return "Test";
}
const_iterator_t iterator = m_bimap.left.find(name);
if(iterator == m_bimap.left.end()){
return ADDR_INV;
}else{
return iterator->second;
}
#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

View File

@ -1,56 +1,73 @@
#ifndef __ELFREADER_HPP__
#define __ELFREADER_HPP__
#define __ELFREADER_HPP__
#include <string>
#include <map>
#ifndef __puma
#include <boost/bimap.hpp>
#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<std::string, guest_address_t> 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