ElfReader now supports section information.
ElfReader can now get Section start address, end address and size by its name. Or the according section name to any address within a section See ElfReader.hpp for API. git-svn-id: https://www4.informatik.uni-erlangen.de/i4svn/danceos/trunk/devel/fail@2070 8c4709b5-6ec9-48aa-a5cd-a96041d1645a
This commit is contained in:
@ -1,5 +1,4 @@
|
|||||||
#include "ElfReader.hpp"
|
#include "ElfReader.hpp"
|
||||||
#include "elfinfo/elfinfo.h"
|
|
||||||
#include "sal/SALConfig.hpp"
|
#include "sal/SALConfig.hpp"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
@ -11,6 +10,20 @@ namespace fail {
|
|||||||
const std::string ElfReader::NOTFOUND = "[ELFReader] Function not found.";
|
const std::string ElfReader::NOTFOUND = "[ELFReader] Function not found.";
|
||||||
|
|
||||||
|
|
||||||
|
ElfReader::ElfReader() : m_log("Fail*Elfinfo", false){
|
||||||
|
// try to open elf file from environment variable
|
||||||
|
char * elfpath = getenv("FAIL_ELF_PATH");
|
||||||
|
if(elfpath == NULL){
|
||||||
|
m_log << "FAIL_ELF_PATH not set :(" << std::endl;
|
||||||
|
}else{
|
||||||
|
setup(elfpath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ElfReader::ElfReader(const char* path) : m_log("Fail*Elfinfo", false){
|
||||||
|
setup(path);
|
||||||
|
}
|
||||||
|
|
||||||
void ElfReader::setup(const char* path) {
|
void ElfReader::setup(const char* path) {
|
||||||
// Try to open the ELF file
|
// Try to open the ELF file
|
||||||
FILE * fp = fopen(path, "r");
|
FILE * fp = fopen(path, "r");
|
||||||
@ -46,22 +59,47 @@ void ElfReader::setup(const char* path) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Parse section information
|
||||||
|
if(read_ELF_section_header(ehdr.e_shstrndx,&sec_hdr,fp)==-1)
|
||||||
|
{
|
||||||
|
m_log << "Error: reading section string table sect_num = " << ehdr.e_shstrndx << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* buff=(char*)malloc(sec_hdr.sh_size);
|
||||||
|
if (!buff)
|
||||||
|
{
|
||||||
|
m_log << "Malloc failed to allocate buffer for shstrtab" << std::endl;
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
//seek to the offset in the file,
|
||||||
|
fseek(fp,(off_t)sec_hdr.sh_offset,SEEK_SET);
|
||||||
|
fread(buff,sec_hdr.sh_size,1,fp);
|
||||||
|
m_log << "Total number of sections: " << num_hdrs << std::endl;
|
||||||
|
|
||||||
|
for(i=0;i<num_hdrs;i++)
|
||||||
|
{
|
||||||
|
if(read_ELF_section_header(i,&sec_hdr,fp)==-1)
|
||||||
|
{
|
||||||
|
m_log << "Wrong Section to read\n" << std::endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
process_section(&sec_hdr, buff);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(buff)
|
||||||
|
free(buff);
|
||||||
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
ElfReader::ElfReader() : m_log("Fail*Elfinfo", false){
|
|
||||||
// try to open elf file from environment variable
|
|
||||||
char * elfpath = getenv("FAIL_ELF_PATH");
|
|
||||||
if(elfpath == NULL){
|
|
||||||
m_log << "FAIL_ELF_PATH not set :(" << std::endl;
|
|
||||||
}else{
|
|
||||||
setup(elfpath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ElfReader::ElfReader(const char* path) : m_log("Fail*Elfinfo", false){
|
int ElfReader::process_section(Elf32_Shdr *sect_hdr, char* sect_name_buff){
|
||||||
setup(path);
|
// Add section name, start address and size to list
|
||||||
|
int idx=sect_hdr->sh_name;
|
||||||
|
m_sections_map.push_back( sect_hdr->sh_addr, sect_hdr->sh_size, sect_name_buff+idx );
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ElfReader::process_symboltable(int sect_num, FILE* fp){
|
int ElfReader::process_symboltable(int sect_num, FILE* fp){
|
||||||
@ -177,6 +215,32 @@ void ElfReader::printMangled(){
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef __puma
|
||||||
|
std::string ElfReader::getSection(guest_address_t address) {
|
||||||
|
return m_sections_map.find_name_by(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
guest_address_t ElfReader::getSectionStart(const std::string& sectionname) {
|
||||||
|
SectionsMap::address_pair_t pair;
|
||||||
|
pair = m_sections_map.find_range_by(sectionname);
|
||||||
|
|
||||||
|
return pair.first;
|
||||||
|
}
|
||||||
|
|
||||||
|
guest_address_t ElfReader::getSectionEnd(const std::string& sectionname) {
|
||||||
|
SectionsMap::address_pair_t pair = m_sections_map.find_range_by(sectionname);
|
||||||
|
if ( pair.first == ADDR_INV ) {
|
||||||
|
return ADDR_INV;
|
||||||
|
}
|
||||||
|
return pair.first + pair.second;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t ElfReader::getSectionSize(const std::string& sectionname) {
|
||||||
|
SectionsMap::address_pair_t pair = m_sections_map.find_range_by(sectionname);
|
||||||
|
return pair.second;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
} // end-of-namespace fail
|
} // end-of-namespace fail
|
||||||
|
|
||||||
|
|||||||
@ -7,11 +7,56 @@
|
|||||||
#include <boost/bimap.hpp>
|
#include <boost/bimap.hpp>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <ostream>
|
||||||
#include "sal/SALConfig.hpp" // for ADDR_INV
|
#include "sal/SALConfig.hpp" // for ADDR_INV
|
||||||
#include "Logger.hpp"
|
#include "Logger.hpp"
|
||||||
|
#include "elfinfo/elfinfo.h"
|
||||||
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
namespace fail {
|
namespace fail {
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Helper struct for section information
|
||||||
|
*/
|
||||||
|
struct SectionsMap {
|
||||||
|
typedef std::pair<guest_address_t, size_t> address_pair_t;
|
||||||
|
typedef std::vector< std::pair< address_pair_t, std::string> > container ;
|
||||||
|
container section_to_name;
|
||||||
|
|
||||||
|
void push_back(guest_address_t start, size_t size, std::string name)
|
||||||
|
{
|
||||||
|
section_to_name.push_back(std::make_pair(std::make_pair(start, size), name));
|
||||||
|
}
|
||||||
|
|
||||||
|
address_pair_t find_range_by(std::string name){
|
||||||
|
for(container::iterator it = section_to_name.begin(), end = section_to_name.end(); it != end; ++it){
|
||||||
|
container::value_type pair_pair_string = *it;
|
||||||
|
typedef container::value_type::first_type section_type;
|
||||||
|
|
||||||
|
if(pair_pair_string.second == name){
|
||||||
|
return pair_pair_string.first;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return std::make_pair(ADDR_INV, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string find_name_by(guest_address_t address){
|
||||||
|
for(container::iterator it = section_to_name.begin(), end = section_to_name.end(); it != end; ++it){
|
||||||
|
container::value_type pair_pair_string = *it;
|
||||||
|
typedef container::value_type::first_type section_type;
|
||||||
|
section_type section = pair_pair_string.first;
|
||||||
|
if(address >= section.first && address < section.first + section.second){
|
||||||
|
return pair_pair_string.second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return std::string("SECTION_NOT_FOUND");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \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
|
||||||
@ -81,12 +126,43 @@ namespace fail {
|
|||||||
//! Default string, if symbol is not found
|
//! Default string, if symbol is not found
|
||||||
static const std::string NOTFOUND;
|
static const std::string NOTFOUND;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the name of a section
|
||||||
|
* @param address The address of the section
|
||||||
|
* @return The according section name if section was found, else SECTION_NOT_FOUND
|
||||||
|
*/
|
||||||
|
std::string getSection(guest_address_t address);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the start address of a section
|
||||||
|
* @param name The name of the section
|
||||||
|
* @return The according section start if section was found, else ADDR_INV
|
||||||
|
*/
|
||||||
|
guest_address_t getSectionStart(const std::string& name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the end address of a section
|
||||||
|
* @param name The name of the section
|
||||||
|
* @return The according section end if section was found, else ADDR_INV
|
||||||
|
*/
|
||||||
|
guest_address_t getSectionEnd(const std::string& name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the size of a section
|
||||||
|
* @param name The name of the section
|
||||||
|
* @return The according section sizh if section was found, else ADDR_INV
|
||||||
|
*/
|
||||||
|
guest_address_t getSectionSize(const std::string& name);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Logger m_log;
|
Logger m_log;
|
||||||
|
|
||||||
|
|
||||||
void setup(const char*);
|
void setup(const char*);
|
||||||
int process_symboltable(int sect_num, FILE* fp);
|
int process_symboltable(int sect_num, FILE* fp);
|
||||||
|
int process_section(Elf32_Shdr *sect_hdr, char* sect_name_buff);
|
||||||
|
|
||||||
|
fail::SectionsMap m_sections_map;
|
||||||
|
|
||||||
#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;
|
||||||
@ -99,7 +175,7 @@ namespace fail {
|
|||||||
typedef typename MapType::const_iterator const_iterator;
|
typedef typename MapType::const_iterator const_iterator;
|
||||||
for( const_iterator iter = m.begin(), iend = m.end(); iter != iend; ++iter )
|
for( const_iterator iter = m.begin(), iend = m.end(); iter != iend; ++iter )
|
||||||
{
|
{
|
||||||
m_log << std::hex << iter->first << " - "<< std::hex << iter->second << std::endl;
|
m_log << std::hex << iter->first << " \t "<< std::hex << iter->second << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -28,9 +28,9 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <elf.h>
|
#include <elf.h>
|
||||||
|
|
||||||
|
|
||||||
void read_ELF_file_header(FILE* fp,Elf32_Ehdr *filehdr)
|
void read_ELF_file_header(FILE* fp,Elf32_Ehdr *filehdr)
|
||||||
{
|
{
|
||||||
|
|
||||||
//rewind(fp);
|
//rewind(fp);
|
||||||
fread(filehdr,sizeof(Elf32_Ehdr),1,fp);
|
fread(filehdr,sizeof(Elf32_Ehdr),1,fp);
|
||||||
}
|
}
|
||||||
@ -59,6 +59,7 @@ int read_ELF_section_header(int sect_num,Elf32_Shdr *sect_hdr,FILE *fp)
|
|||||||
sect_hdr_off+=elfhdr.e_shentsize*sect_num;
|
sect_hdr_off+=elfhdr.e_shentsize*sect_num;
|
||||||
fseek(fp,(off_t)sect_hdr_off,SEEK_SET);
|
fseek(fp,(off_t)sect_hdr_off,SEEK_SET);
|
||||||
fread(sect_hdr,sizeof(Elf32_Shdr),1,fp);
|
fread(sect_hdr,sizeof(Elf32_Shdr),1,fp);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,7 +67,7 @@ void process_sect_hdr(Elf32_Shdr *sect_hdr,char *sect_name_buff)
|
|||||||
{
|
{
|
||||||
int idx;
|
int idx;
|
||||||
idx=sect_hdr->sh_name;
|
idx=sect_hdr->sh_name;
|
||||||
printf(" %-20s ",sect_name_buff+idx);
|
printf(" %-20s (0x%x++0x%x) ",sect_name_buff+idx, sect_hdr->sh_addr, sect_hdr->sh_size);
|
||||||
if(sect_hdr->sh_entsize)
|
if(sect_hdr->sh_entsize)
|
||||||
printf(" %c ",'*');
|
printf(" %c ",'*');
|
||||||
else
|
else
|
||||||
@ -341,3 +342,4 @@ void dump_symbols(FILE *fp)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -17,5 +17,6 @@
|
|||||||
|
|
||||||
void read_ELF_file_header(FILE* fp,Elf32_Ehdr *filehdr);
|
void read_ELF_file_header(FILE* fp,Elf32_Ehdr *filehdr);
|
||||||
int read_ELF_section_header(int sect_num,Elf32_Shdr *sect_hdr,FILE *fp);
|
int read_ELF_section_header(int sect_num,Elf32_Shdr *sect_hdr,FILE *fp);
|
||||||
|
void display_sections(FILE *fp);
|
||||||
|
|
||||||
#endif // ELFINFO_H
|
#endif // ELFINFO_H
|
||||||
|
|||||||
Reference in New Issue
Block a user