ElfReader: Added support for de/mangled symbols
See vezs-example experiment for usage. git-svn-id: https://www4.informatik.uni-erlangen.de/i4svn/danceos/trunk/devel/fail@1985 8c4709b5-6ec9-48aa-a5cd-a96041d1645a
This commit is contained in:
@ -1,6 +1,8 @@
|
|||||||
set(SRCS
|
set(SRCS
|
||||||
ElfReader.cc
|
ElfReader.cc
|
||||||
ElfReader.hpp
|
ElfReader.hpp
|
||||||
|
Demangler.hpp
|
||||||
|
Demangler.cc
|
||||||
elfinfo/elfinfo.cc
|
elfinfo/elfinfo.cc
|
||||||
elfinfo/elfinfo.h
|
elfinfo/elfinfo.h
|
||||||
gzstream/gzstream.C
|
gzstream/gzstream.C
|
||||||
@ -28,5 +30,8 @@ find_package(Boost 1.42 COMPONENTS thread REQUIRED)
|
|||||||
include_directories(${Boost_INCLUDE_DIRS})
|
include_directories(${Boost_INCLUDE_DIRS})
|
||||||
link_directories(${Boost_LIBRARY_DIRS})
|
link_directories(${Boost_LIBRARY_DIRS})
|
||||||
|
|
||||||
|
# libiberty required by Demangler.cc:
|
||||||
|
find_library(LIB_IBERTY iberty)
|
||||||
|
|
||||||
add_library(fail-util ${SRCS})
|
add_library(fail-util ${SRCS})
|
||||||
target_link_libraries(fail-util ${PROTOBUF_LIBRARY} ${Boost_THREAD_LIBRARY})
|
target_link_libraries(fail-util ${PROTOBUF_LIBRARY} ${Boost_THREAD_LIBRARY} ${LIB_IBERTY})
|
||||||
|
|||||||
20
src/core/util/Demangler.cc
Normal file
20
src/core/util/Demangler.cc
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#include "Demangler.hpp"
|
||||||
|
|
||||||
|
#define HAVE_DECL_BASENAME 1
|
||||||
|
#include <demangle.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
namespace fail {
|
||||||
|
|
||||||
|
const std::string Demangler::DEMANGLE_FAILED = "[Demangler] Demangle failed.";
|
||||||
|
|
||||||
|
std::string Demangler::demangle(const std::string& name){
|
||||||
|
const char* res = cplus_demangle(name.c_str(), 0);
|
||||||
|
if(res != NULL){
|
||||||
|
return std::string(res);
|
||||||
|
}else{
|
||||||
|
return DEMANGLE_FAILED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // end of namespace
|
||||||
24
src/core/util/Demangler.hpp
Normal file
24
src/core/util/Demangler.hpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#ifndef __DEMANGLER_HPP
|
||||||
|
#define __DEMANGLER_HPP
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace fail {
|
||||||
|
|
||||||
|
class Demangler {
|
||||||
|
public:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the demangled symbol name of a mangled string.
|
||||||
|
* @param name The mangled symbol
|
||||||
|
* @return The according demangled name if found, else Demangler:::DEMANGLE_FAILED
|
||||||
|
*/
|
||||||
|
static std::string demangle(const std::string & name);
|
||||||
|
|
||||||
|
//! Inform about failed demangling.
|
||||||
|
static const std::string DEMANGLE_FAILED;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // end of namespace
|
||||||
|
|
||||||
|
#endif // DEMANGLER_HPP
|
||||||
@ -4,8 +4,12 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
|
#include "Demangler.hpp"
|
||||||
|
|
||||||
namespace fail {
|
namespace fail {
|
||||||
|
|
||||||
|
const std::string ElfReader::NOTFOUND = "[ELFReader] Function not found.";
|
||||||
|
|
||||||
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");
|
||||||
@ -80,7 +84,6 @@ namespace fail {
|
|||||||
//so we have the namebuf now seek to symtab data
|
//so we have the namebuf now seek to symtab data
|
||||||
fseek(fp,sym_data_offset,SEEK_SET);
|
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++)
|
for(i=0;i<num_sym;i++)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -89,9 +92,10 @@ namespace fail {
|
|||||||
|
|
||||||
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;
|
|
||||||
#ifndef __puma
|
#ifndef __puma
|
||||||
m_bimap.insert( entry(name_buf+idx, mysym.st_value) );
|
m_bimap_mangled.insert( entry(name_buf+idx, mysym.st_value) );
|
||||||
|
m_bimap_demangled.insert( entry ( Demangler::demangle(name_buf+idx), mysym.st_value) );
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -99,33 +103,66 @@ namespace fail {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
guest_address_t ElfReader::getAddressByName(const std::string& name) {
|
guest_address_t ElfReader::getAddressByName(const std::string& name) {
|
||||||
#ifndef __puma
|
#ifndef __puma
|
||||||
typedef bimap_t::left_map::const_iterator const_iterator_t;
|
guest_address_t res = getAddress(m_bimap_demangled, name);
|
||||||
|
if(res == ADDR_INV){
|
||||||
|
res = getAddress(m_bimap_mangled, name);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
const_iterator_t iterator = m_bimap.left.find(name);
|
#ifndef __puma
|
||||||
if(iterator == m_bimap.left.end()){
|
guest_address_t ElfReader::getAddress(const bimap_t& map, const std::string& name){
|
||||||
return ADDR_INV;
|
typedef bimap_t::left_map::const_iterator const_iterator_t;
|
||||||
}else{
|
|
||||||
return iterator->second;
|
const_iterator_t iterator = map.left.find(name);
|
||||||
|
if(iterator == map.left.end()){
|
||||||
|
return ADDR_INV;
|
||||||
|
}else{
|
||||||
|
return iterator->second;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef __puma
|
||||||
|
std::string ElfReader::getName(const bimap_t& map, guest_address_t address){
|
||||||
|
// .right switches key/value
|
||||||
|
typedef bimap_t::right_map::const_iterator const_iterator_t;
|
||||||
|
|
||||||
|
const_iterator_t iterator = map.right.find(address);
|
||||||
|
if(iterator != map.right.end()){
|
||||||
|
return iterator->second;
|
||||||
|
}
|
||||||
|
return NOTFOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ElfReader::getNameByAddress(guest_address_t address) {
|
std::string ElfReader::getNameByAddress(guest_address_t address) {
|
||||||
#ifndef __puma
|
std::string res = getName(m_bimap_demangled, address);
|
||||||
// .right switches key/value
|
if(res == NOTFOUND){
|
||||||
typedef bimap_t::right_map::const_iterator const_iterator_t;
|
return getName(m_bimap_mangled, address);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
const_iterator_t iterator = m_bimap.right.find(address);
|
std::string ElfReader::getMangledNameByAddress(guest_address_t address) {
|
||||||
if(iterator == m_bimap.right.end()){
|
return getName(m_bimap_mangled, address);
|
||||||
return "[ElfReader] FUNCTION NOT FOUND";
|
}
|
||||||
}else{
|
|
||||||
return iterator->second;
|
std::string ElfReader::getDemangledNameByAddress(guest_address_t address) {
|
||||||
|
return getName(m_bimap_demangled, address);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ElfReader::printDemangled(){
|
||||||
|
print_map(m_bimap_demangled.right); // print Address as first element
|
||||||
|
}
|
||||||
|
|
||||||
|
void ElfReader::printMangled(){
|
||||||
|
print_map(m_bimap_mangled.right); // print Address as first element
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
|
|
||||||
} // end-of-namespace fail
|
} // end-of-namespace fail
|
||||||
|
|
||||||
|
|||||||
@ -2,24 +2,14 @@
|
|||||||
#define __ELFREADER_HPP__
|
#define __ELFREADER_HPP__
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#ifndef __puma
|
#ifndef __puma
|
||||||
#include <boost/bimap.hpp>
|
#include <boost/bimap.hpp>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "sal/SALConfig.hpp"
|
#include "sal/SALConfig.hpp" // for ADDR_INV
|
||||||
#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 {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,34 +29,76 @@ namespace fail {
|
|||||||
ElfReader(const char* path);
|
ElfReader(const char* path);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get guest address by symbol name
|
* Get guest address by symbol name.
|
||||||
|
* Both mangled an demangled symbols are searched.
|
||||||
* @param name The symbol name as string
|
* @param name The symbol name as string
|
||||||
* @return The according addres if found, else -1
|
* @return The according address if found, else ADDR_INV
|
||||||
*/
|
*/
|
||||||
guest_address_t getAddressByName(const std::string& name) ;
|
guest_address_t getAddressByName(const std::string& name) ;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get symbol name associated to an address
|
* Get demangled 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 ElfReader::NOTFOUND
|
||||||
*
|
|
||||||
* \todo multimap sorted by addresses
|
|
||||||
* Name is at first key <= address
|
|
||||||
*/
|
*/
|
||||||
std::string getNameByAddress(guest_address_t address) ;
|
std::string getNameByAddress(guest_address_t address) ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the mangled symbol name associated to an address
|
||||||
|
|
||||||
|
* @param name The address of a symbol (or around a symbol -> instruction pointer)
|
||||||
|
* @return The according address if found, else ElfReader::NOTFOUND
|
||||||
|
*/
|
||||||
|
std::string getMangledNameByAddress(guest_address_t address) ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the demangled symbol name associated to an address
|
||||||
|
* Note the the demangled name is simplified, not showing any types!
|
||||||
|
* @param name The address of a symbol (or around a symbol -> instruction pointer)
|
||||||
|
* @return The according address if found, else ElfReader::NOTFOUND
|
||||||
|
*/
|
||||||
|
std::string getDemangledNameByAddress(guest_address_t address) ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print the list of available mangled symbols
|
||||||
|
* @note This includes both C and C++ symbols
|
||||||
|
*/
|
||||||
|
void printMangled();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print the list of all available demangled symbols
|
||||||
|
* @note These are only C++ symbols.
|
||||||
|
*/
|
||||||
|
void printDemangled();
|
||||||
|
|
||||||
|
//! Default string, if symbol is not found
|
||||||
|
static const std::string NOTFOUND;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Logger m_log;
|
Logger m_log;
|
||||||
|
|
||||||
|
|
||||||
|
int process_symboltable(int sect_num, FILE* fp);
|
||||||
#ifndef __puma
|
#ifndef __puma
|
||||||
typedef boost::bimap< std::string, guest_address_t > bimap_t;
|
typedef boost::bimap< std::string, guest_address_t > bimap_t;
|
||||||
typedef bimap_t::value_type entry;
|
typedef bimap_t::value_type entry;
|
||||||
bimap_t m_bimap;
|
|
||||||
|
bimap_t m_bimap_mangled;
|
||||||
|
bimap_t m_bimap_demangled;
|
||||||
|
|
||||||
|
template < typename MapType >
|
||||||
|
void print_map(const MapType & m){
|
||||||
|
typedef typename MapType::const_iterator const_iterator;
|
||||||
|
for( const_iterator iter = m.begin(), iend = m.end(); iter != iend; ++iter )
|
||||||
|
{
|
||||||
|
m_log << std::hex << iter->first << " - "<< std::hex << iter->second << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
guest_address_t getAddress(const bimap_t& map, const std::string& name);
|
||||||
|
std::string getName(const bimap_t& map, guest_address_t address);
|
||||||
#endif
|
#endif
|
||||||
int process_symboltable(int sect_num, FILE* fp);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // end-of-namespace fail
|
} // end-of-namespace fail
|
||||||
|
|||||||
@ -34,6 +34,8 @@ bool VEZSExperiment::run()
|
|||||||
ElfReader elf("./x86_bare_test");
|
ElfReader elf("./x86_bare_test");
|
||||||
log << "STARTING EXPERIMENT" << endl;
|
log << "STARTING EXPERIMENT" << endl;
|
||||||
log << "main() address: " << elf.getAddressByName("main") << endl;
|
log << "main() address: " << elf.getAddressByName("main") << endl;
|
||||||
|
elf.printMangled();
|
||||||
|
elf.printDemangled();
|
||||||
|
|
||||||
BPSingleListener bp;
|
BPSingleListener bp;
|
||||||
#if 0
|
#if 0
|
||||||
@ -57,23 +59,22 @@ bool VEZSExperiment::run()
|
|||||||
simulator.restore("vezs.state");
|
simulator.restore("vezs.state");
|
||||||
|
|
||||||
log << " current EIP = " << simulator.getCPU(0).getInstructionPointer() << endl;
|
log << " current EIP = " << simulator.getCPU(0).getInstructionPointer() << endl;
|
||||||
log << " Task0 start: " << elf.getAddressByName("_ZN5Alpha17functionTaskTask0Ev") << endl;
|
|
||||||
BPSingleListener bpt0;
|
BPSingleListener bpt0;
|
||||||
BPSingleListener bpt1;
|
BPSingleListener bpt1;
|
||||||
bpt0.setWatchInstructionPointer(elf.getAddressByName("_ZN5Alpha17functionTaskTask0Ev"));
|
bpt0.setWatchInstructionPointer(elf.getAddressByName("Alpha::functionTaskTask0"));
|
||||||
bpt1.setWatchInstructionPointer(elf.getAddressByName("_ZN4Beta17functionTaskTask1Ev"));
|
bpt1.setWatchInstructionPointer(elf.getAddressByName("_ZN4Beta17functionTaskTask1Ev")); // both mangled and demangled name a working.
|
||||||
|
|
||||||
simulator.addListener(&bpt1);
|
simulator.addListener(&bpt1);
|
||||||
simulator.addListenerAndResume(&bpt0);
|
simulator.addListenerAndResume(&bpt0);
|
||||||
log << "EIP = " << simulator.getCPU(0).getInstructionPointer() <<" "<<elf.getNameByAddress(simulator.getCPU(0).getInstructionPointer()) << endl;
|
log << "EIP = " << simulator.getCPU(0).getInstructionPointer() <<" "<<elf.getMangledNameByAddress(simulator.getCPU(0).getInstructionPointer()) << endl;
|
||||||
simulator.resume();
|
simulator.resume();
|
||||||
log << "EIP = " << simulator.getCPU(0).getInstructionPointer() <<" "<<elf.getNameByAddress(simulator.getCPU(0).getInstructionPointer()) << endl;
|
log << "EIP = " << simulator.getCPU(0).getInstructionPointer() <<" "<<elf.getNameByAddress(simulator.getCPU(0).getInstructionPointer()) << endl;
|
||||||
|
|
||||||
simulator.clearListeners();
|
simulator.clearListeners();
|
||||||
bpt1.setWatchInstructionPointer(elf.getAddressByName("_ZN2os3krn9SchedImpl18superDispatch_implEPS1_hh"));
|
bpt1.setWatchInstructionPointer(elf.getAddressByName("os::krn::SchedImpl::superDispatch_impl"));
|
||||||
for(int i = 0; i < 10; i++){
|
for(int i = 0; i < 10; i++){
|
||||||
simulator.addListenerAndResume(&bpt1);
|
simulator.addListenerAndResume(&bpt1);
|
||||||
log << "EIP = " << simulator.getCPU(0).getInstructionPointer() <<" "<<elf.getNameByAddress(simulator.getCPU(0).getInstructionPointer()) << endl;
|
log << "EIP = " << simulator.getCPU(0).getInstructionPointer() <<" "<< elf.getNameByAddress(simulator.getCPU(0).getInstructionPointer()) << endl;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#if 0
|
#if 0
|
||||||
|
|||||||
Reference in New Issue
Block a user