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:
@ -6,111 +6,126 @@
|
|||||||
|
|
||||||
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) {
|
||||||
m_log << "Error: Could not open " << path << std::endl;
|
m_log << "Error: Could not open " << path << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Evaluate headers
|
// Evaluate headers
|
||||||
Elf32_Ehdr ehdr;
|
Elf32_Ehdr ehdr;
|
||||||
Elf32_Shdr sec_hdr;
|
Elf32_Shdr sec_hdr;
|
||||||
int num_hdrs,i;
|
int num_hdrs,i;
|
||||||
fseek(fp,(off_t)0,SEEK_SET);
|
fseek(fp,(off_t)0,SEEK_SET);
|
||||||
read_ELF_file_header(fp, &ehdr);
|
read_ELF_file_header(fp, &ehdr);
|
||||||
num_hdrs=ehdr.e_shnum;
|
num_hdrs=ehdr.e_shnum;
|
||||||
m_log << "Evaluating ELF File: " << path << std::endl;
|
m_log << "Evaluating ELF File: " << path << std::endl;
|
||||||
// Parse symbol table and generate internal map
|
// Parse symbol table and generate internal map
|
||||||
for(i=0;i<num_hdrs;i++)
|
for(i=0;i<num_hdrs;i++)
|
||||||
{
|
{
|
||||||
if(read_ELF_section_header(i,&sec_hdr,fp)==-1)
|
if(read_ELF_section_header(i,&sec_hdr,fp)==-1)
|
||||||
{
|
{
|
||||||
m_log << "Wrong Section to read" << std::endl;
|
m_log << "Wrong Section to read" << std::endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if((sec_hdr.sh_type==SHT_SYMTAB)||(sec_hdr.sh_type==SHT_DYNSYM))
|
if((sec_hdr.sh_type==SHT_SYMTAB)||(sec_hdr.sh_type==SHT_DYNSYM))
|
||||||
{
|
{
|
||||||
process_symboltable(i,fp);
|
process_symboltable(i,fp);
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(fp);
|
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,§_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
|
int ElfReader::process_symboltable(int sect_num, FILE* fp){
|
||||||
if(read_ELF_section_header(link,§_hdr,fp)==-1)
|
|
||||||
{
|
Elf32_Shdr sect_hdr;
|
||||||
return -1;
|
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,§_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,§_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) {
|
guest_address_t ElfReader::getAddressByName(const std::string& name) {
|
||||||
if( m_map.find(name) == m_map.end() ) {
|
#ifndef __puma
|
||||||
return ADDR_INV;
|
typedef bimap_t::left_map::const_iterator const_iterator_t;
|
||||||
}else{
|
|
||||||
return m_map[name];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string getNameByAddress(guest_address_t address) {
|
const_iterator_t iterator = m_bimap.left.find(name);
|
||||||
return "Test";
|
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
|
} // end-of-namespace fail
|
||||||
|
|
||||||
|
|||||||
@ -1,56 +1,73 @@
|
|||||||
#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
|
|
||||||
* Parses an ELF file and provides a list of symbol names
|
|
||||||
* and corresponding addresses
|
|
||||||
*/
|
|
||||||
|
|
||||||
class ElfReader {
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* \class ElfReader
|
||||||
* @param path Path to the ELF file.
|
* Parses an ELF file and provides a list of symbol names
|
||||||
|
* and corresponding addresses
|
||||||
*/
|
*/
|
||||||
ElfReader(const char* path);
|
|
||||||
|
|
||||||
/**
|
class ElfReader {
|
||||||
* Get guest address by symbol name
|
|
||||||
* @param name The symbol name as string
|
public:
|
||||||
* @return The according addres if found, else -1
|
|
||||||
*/
|
/**
|
||||||
guest_address_t getAddressByName(const std::string& name) ;
|
* 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
|
* Get symbol name associated to an address
|
||||||
* This is interesting when checking instruction pointers.
|
* This is interesting when checking instruction pointers.
|
||||||
* @param name The address of a symbol (or around a symbol -> instruction pointer)
|
* @param name The address of a symbol (or around a symbol -> instruction pointer)
|
||||||
* @return The according address if found, else -1
|
* @return The according address if found, else -1
|
||||||
*
|
*
|
||||||
* \todo multimap sorted by addresses
|
* \todo multimap sorted by addresses
|
||||||
* Name is at first key <= address
|
* Name is at first key <= address
|
||||||
*/
|
*/
|
||||||
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;
|
|
||||||
|
|
||||||
int process_symboltable(int sect_num, FILE* fp);
|
#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
|
} // end-of-namespace fail
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user