Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
StELFFile Class Reference

Parser for Executable and Linking Format (ELF) files. More...

#include <StELFFile.h>

+ Collaboration diagram for StELFFile:

Classes

struct  SectionDataInfo
 

Public Types

typedef std::vector< Elf32_Shdr >::const_iterator const_section_iterator
 
typedef std::vector< Elf32_Phdr >::const_iterator const_segment_iterator
 
typedef std::vector< Elf32_Shdr >::const_iterator const_section_iterator
 
typedef std::vector< Elf32_Phdr >::const_iterator const_segment_iterator
 

Public Member Functions

 StELFFile (std::istream &inStream)
 Constructor. More...
 
virtual ~StELFFile ()
 Destructor. More...
 
 StELFFile (std::istream &inStream)
 Constructor.
 
virtual ~StELFFile ()
 Destructor.
 
File format variant
virtual ELFVariant_t ELFVariant ()
 Return the ELF format variant to which this file is set.
 
virtual void setELFVariant (ELFVariant_t variant)
 Set the ELF format variation to either #eARMVariant or #eGHSVariant.
 
virtual ELFVariant_t ELFVariant ()
 Return the ELF format variant to which this file is set.
 
virtual void setELFVariant (ELFVariant_t variant)
 Set the ELF format variation to either #eARMVariant or #eGHSVariant.
 
File name
virtual void setName (const std::string &inName)
 
virtual std::string getName () const
 
virtual void setName (const std::string &inName)
 
virtual std::string getName () const
 
ELF header
const Elf32_EhdrgetFileHeader () const
 Returns the ELF file header.
 
const Elf32_EhdrgetFileHeader () const
 Returns the ELF file header.
 
Sections

Methods pertaining to the object file's sections.

unsigned getSectionCount () const
 Returns the number of sections in the file.
 
const Elf32_ShdrgetSectionAtIndex (unsigned inIndex) const
 Returns a reference to section number inIndex.
 
const_section_iterator getSectionBegin () const
 
const_section_iterator getSectionEnd () const
 
unsigned getIndexOfSectionWithName (const std::string &inName)
 Returns the index of the section with the name inName. More...
 
uint8_t * getSectionDataAtIndex (unsigned inIndex)
 Returns the data for the section. More...
 
uint8_t * getSectionData (const_section_iterator inSection)
 Returns the data for the section. More...
 
unsigned getSectionCount () const
 Returns the number of sections in the file.
 
const Elf32_ShdrgetSectionAtIndex (unsigned inIndex) const
 Returns a reference to section number inIndex.
 
const_section_iterator getSectionBegin () const
 
const_section_iterator getSectionEnd () const
 
unsigned getIndexOfSectionWithName (const std::string &inName)
 Returns the index of the section with the name inName.
 
uint8_t * getSectionDataAtIndex (unsigned inIndex)
 Returns the data for the section.
 
uint8_t * getSectionData (const_section_iterator inSection)
 Returns the data for the section.
 
Segments

Methods for accessing the file's program headers for segments.

unsigned getSegmentCount () const
 Returns the number of segments, or program headers, in the file.
 
const Elf32_PhdrgetSegmentAtIndex (unsigned inIndex) const
 Returns a reference to the given segment.
 
const_segment_iterator getSegmentBegin () const
 
const_segment_iterator getSegmentEnd () const
 
uint8_t * getSegmentDataAtIndex (unsigned inIndex)
 Returns the data of the specified segment. More...
 
uint8_t * getSegmentData (const_segment_iterator inSegment)
 Returns the data of the specified segment. More...
 
unsigned getSegmentCount () const
 Returns the number of segments, or program headers, in the file.
 
const Elf32_PhdrgetSegmentAtIndex (unsigned inIndex) const
 Returns a reference to the given segment.
 
const_segment_iterator getSegmentBegin () const
 
const_segment_iterator getSegmentEnd () const
 
uint8_t * getSegmentDataAtIndex (unsigned inIndex)
 Returns the data of the specified segment.
 
uint8_t * getSegmentData (const_segment_iterator inSegment)
 Returns the data of the specified segment.
 
String table

Methods for accessing the string tables.

std::string getSectionNameAtIndex (unsigned inIndex)
 Returns a string from the file's section name string table. More...
 
std::string getStringAtIndex (unsigned inStringTableSectionIndex, unsigned inStringIndex)
 Returns a string from any string table in the object file. More...
 
std::string getSectionNameAtIndex (unsigned inIndex)
 Returns a string from the file's section name string table.
 
std::string getStringAtIndex (unsigned inStringTableSectionIndex, unsigned inStringIndex)
 Returns a string from any string table in the object file.
 
Symbol table

Methods for accessing the object file's symbol table. Currently only a single symbol table with the section name ".symtab" is supported.

unsigned getSymbolCount ()
 Returns the number of symbols in the default ".symtab" symbol table. More...
 
const Elf32_SymgetSymbolAtIndex (unsigned inIndex)
 Returns the symbol with index inIndex. More...
 
unsigned getSymbolNameStringTableIndex () const
 Returns the section index of the string table containing symbol names.
 
std::string getSymbolName (const Elf32_Sym &inSymbol)
 Returns the name of the symbol described by inSymbol.
 
unsigned getIndexOfSymbolAtAddress (uint32_t symbolAddress, bool strict=true)
 Returns STN_UNDEF if it cannot find a symbol at the given symbolAddress.
 
ARMSymbolType_t getTypeOfSymbolAtIndex (unsigned symbolIndex)
 
unsigned getSymbolCount ()
 Returns the number of symbols in the default ".symtab" symbol table.
 
const Elf32_SymgetSymbolAtIndex (unsigned inIndex)
 Returns the symbol with index inIndex.
 
unsigned getSymbolNameStringTableIndex () const
 Returns the section index of the string table containing symbol names.
 
std::string getSymbolName (const Elf32_Sym &inSymbol)
 Returns the name of the symbol described by inSymbol.
 
unsigned getIndexOfSymbolAtAddress (uint32_t symbolAddress, bool strict=true)
 
ARMSymbolType_t getTypeOfSymbolAtIndex (unsigned symbolIndex)
 
Debugging
void dumpSections ()
 
void dumpSymbolTable ()
 
void dumpSections ()
 
void dumpSymbolTable ()
 

Protected Types

typedef std::map< unsigned, SectionDataInfoSectionDataMap
 
typedef std::map< unsigned, SectionDataInfoSectionDataMap
 

Protected Member Functions

SectionDataInfogetCachedSectionData (unsigned inSectionIndex)
 Reads a section's data either from cache or from disk.
 
void readFileHeaders ()
 Reads the file, section, and program headers into memory. More...
 
uint8_t * readSectionData (const Elf32_Shdr &inHeader)
 
uint8_t * readSegmentData (const Elf32_Phdr &inHeader)
 
void byteSwapSymbolTable (const Elf32_Shdr &header, SectionDataInfo &info)
 Byte swaps the symbol table data into host endianness.
 
SectionDataInfogetCachedSectionData (unsigned inSectionIndex)
 Reads a section's data either from cache or from disk.
 
void readFileHeaders ()
 Reads the file, section, and program headers into memory.
 
uint8_t * readSectionData (const Elf32_Shdr &inHeader)
 
uint8_t * readSegmentData (const Elf32_Phdr &inHeader)
 
void byteSwapSymbolTable (const Elf32_Shdr &header, SectionDataInfo &info)
 Byte swaps the symbol table data into host endianness.
 

Protected Attributes

std::istream & m_stream
 The source stream for the ELF file.
 
ELFVariant_t m_elfVariant
 Variant of the ARM ELF format specification.
 
std::string m_name
 File name. (optional)
 
Elf32_Ehdr m_header
 The ELF file header.
 
std::vector< Elf32_Shdrm_sectionHeaders
 All of the section headers.
 
std::vector< Elf32_Phdrm_programHeaders
 All of the program headers.
 
unsigned m_symbolTableIndex
 Index of ".symtab" section, or #SHN_UNDEF if not present.
 
SectionDataMap m_sectionDataCache
 Cached data of sections.
 

Detailed Description

Parser for Executable and Linking Format (ELF) files.

The stream passed into the constructor needs to stay open for the life of the object. This is because calls to getSectionDataAtIndex() and getSegmentDataAtIndex() read the data directly from the input stream.


Class Documentation

struct StELFFile::SectionDataInfo

Little structure containing information about cached section data.

Class Members
uint8_t * m_data Pointer to section data.
unsigned m_size Section data size in bytes.
bool m_swapped Has this section been byte swapped yet? Used for symbol table.

Constructor & Destructor Documentation

StELFFile::StELFFile ( std::istream &  inStream)

Constructor.

Exceptions
StELFFileExceptionis thrown if there is a problem with the file format.
StELFFile::~StELFFile ( )
virtual

Destructor.

Disposes of the string table data.

Member Function Documentation

unsigned StELFFile::getIndexOfSectionWithName ( const std::string &  inName)

Returns the index of the section with the name inName.

If there is not a matching section, then #SHN_UNDEF is returned instead.

uint8_t * StELFFile::getSectionData ( const_section_iterator  inSection)

Returns the data for the section.

The pointer returned from this method must be freed with the delete array operator (i.e., delete []). If either the section data offset (sh_offset) or the section size (sh_size) are 0, then NULL will be returned instead.

The data is read directly from the input stream passed into the constructor. The stream must still be open, or an exception will be thrown.

Exceptions
StELFFileExceptionis thrown if an error occurs while reading the file.
std::bad_allocis thrown if memory for the data cannot be allocated.
uint8_t * StELFFile::getSectionDataAtIndex ( unsigned  inIndex)

Returns the data for the section.

The pointer returned from this method must be freed with the delete array operator (i.e., delete []). If either the section data offset (sh_offset) or the section size (sh_size) are 0, then NULL will be returned instead.

The data is read directly from the input stream passed into the constructor. The stream must still be open, or an exception will be thrown.

Exceptions
StELFFileExceptionis thrown if an error occurs while reading the file.
std::bad_allocis thrown if memory for the data cannot be allocated.
std::string StELFFile::getSectionNameAtIndex ( unsigned  inIndex)

Returns a string from the file's section name string table.

If the index is out of range, or if there is no string table in the file, then an empty string will be returned instead. This will also happen when the index is either 0 or the last byte in the table, since the table begins and ends with zero bytes.

uint8_t * StELFFile::getSegmentData ( const_segment_iterator  inSegment)

Returns the data of the specified segment.

The pointer returned from this method must be freed with the delete array operator (i.e., delete []). If either the segment offset (p_offset) or the segment file size (p_filesz) are 0, then NULL will be returned instead.

The data is read directly from the input stream passed into the constructor. The stream must still be open, or an exception will be thrown.

Exceptions
StELFFileExceptionis thrown if an error occurs while reading the file.
std::bad_allocis thrown if memory for the data cannot be allocated.
uint8_t * StELFFile::getSegmentDataAtIndex ( unsigned  inIndex)

Returns the data of the specified segment.

The pointer returned from this method must be freed with the delete array operator (i.e., delete []). If either the segment offset (p_offset) or the segment file size (p_filesz) are 0, then NULL will be returned instead.

The data is read directly from the input stream passed into the constructor. The stream must still be open, or an exception will be thrown.

Exceptions
StELFFileExceptionis thrown if an error occurs while reading the file.
std::bad_allocis thrown if memory for the data cannot be allocated.
std::string StELFFile::getStringAtIndex ( unsigned  inStringTableSectionIndex,
unsigned  inStringIndex 
)

Returns a string from any string table in the object file.

Exceptions
std::invalid_argumentis thrown if the section identified by inStringTableSectionIndex is not actually a string table, or if inStringIndex is out of range for the string table.
const Elf32_Sym & StELFFile::getSymbolAtIndex ( unsigned  inIndex)

Returns the symbol with index inIndex.

Exceptions
std::invalid_argumentis thrown if inIndex is out of range.]
unsigned StELFFile::getSymbolCount ( )

Returns the number of symbols in the default ".symtab" symbol table.

The number of entries in the symbol table is the symbol table section size divided by the size of each symbol entry (the Elf32_Shdr::sh_entsize field of the symbol table section header).

void StELFFile::readFileHeaders ( )
protected

Reads the file, section, and program headers into memory.

Exceptions
StELFFileExceptionis thrown if the file is not an ELF file.
uint8_t * StELFFile::readSectionData ( const Elf32_Shdr inHeader)
protected
Exceptions
StELFFileExceptionis thrown if an error occurs while reading the file.
std::bad_allocis thrown if memory for the data cannot be allocated.
uint8_t * StELFFile::readSegmentData ( const Elf32_Phdr inHeader)
protected
Exceptions
StELFFileExceptionis thrown if an error occurs while reading the file.
std::bad_allocis thrown if memory for the data cannot be allocated.

The documentation for this class was generated from the following files: