src/blfwk/StELFFile.h
/*
* Copyright (c) 2013-14, Freescale Semiconductor, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* o Neither the name of Freescale Semiconductor, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if !defined(_StELFFile_h_)
#define _StELFFile_h_
#include "blfwk/stdafx.h"
#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <stdexcept>
#include "blfwk/ELF.h"
typedef enum
{
eARMVariant = 1,
eGHSVariant,
eGCCVariant
} ELFVariant_t;
typedef enum
{
eUnknownSymbol,
eARMSymbol,
eThumbSymbol,
eDataSymbol
} ARMSymbolType_t;
class StELFFile
{
public:
typedef std::vector<Elf32_Shdr>::const_iterator const_section_iterator;
typedef std::vector<Elf32_Phdr>::const_iterator const_segment_iterator;
public:
StELFFile(std::istream &inStream);
virtual ~StELFFile();
virtual ELFVariant_t ELFVariant() { return m_elfVariant; }
virtual void setELFVariant(ELFVariant_t variant) { m_elfVariant = variant; }
virtual void setName(const std::string &inName) { m_name = inName; }
virtual std::string getName() const { return m_name; }
inline const Elf32_Ehdr &getFileHeader() const { return m_header; }
inline unsigned getSectionCount() const { return static_cast<unsigned>(m_sectionHeaders.size()); }
const Elf32_Shdr &getSectionAtIndex(unsigned inIndex) const;
inline const_section_iterator getSectionBegin() const { return m_sectionHeaders.begin(); }
inline const_section_iterator getSectionEnd() const { return m_sectionHeaders.end(); }
unsigned getIndexOfSectionWithName(const std::string &inName);
uint8_t *getSectionDataAtIndex(unsigned inIndex);
uint8_t *getSectionData(const_section_iterator inSection);
inline unsigned getSegmentCount() const { return static_cast<unsigned>(m_programHeaders.size()); }
const Elf32_Phdr &getSegmentAtIndex(unsigned inIndex) const;
inline const_segment_iterator getSegmentBegin() const { return m_programHeaders.begin(); }
inline const_segment_iterator getSegmentEnd() const { return m_programHeaders.end(); }
uint8_t *getSegmentDataAtIndex(unsigned inIndex);
uint8_t *getSegmentData(const_segment_iterator inSegment);
std::string getSectionNameAtIndex(unsigned inIndex);
std::string getStringAtIndex(unsigned inStringTableSectionIndex, unsigned inStringIndex);
unsigned getSymbolCount();
const Elf32_Sym &getSymbolAtIndex(unsigned inIndex);
unsigned getSymbolNameStringTableIndex() const;
std::string getSymbolName(const Elf32_Sym &inSymbol);
unsigned getIndexOfSymbolAtAddress(uint32_t symbolAddress, bool strict = true);
ARMSymbolType_t getTypeOfSymbolAtIndex(unsigned symbolIndex);
void dumpSections();
void dumpSymbolTable();
protected:
std::istream &m_stream;
ELFVariant_t m_elfVariant;
std::string m_name;
Elf32_Ehdr m_header;
std::vector<Elf32_Shdr> m_sectionHeaders;
std::vector<Elf32_Phdr> m_programHeaders;
unsigned m_symbolTableIndex;
struct SectionDataInfo
{
uint8_t *m_data;
unsigned m_size;
bool m_swapped;
};
typedef std::map<unsigned, SectionDataInfo> SectionDataMap;
SectionDataMap m_sectionDataCache;
SectionDataInfo &getCachedSectionData(unsigned inSectionIndex);
void readFileHeaders();
uint8_t *readSectionData(const Elf32_Shdr &inHeader);
uint8_t *readSegmentData(const Elf32_Phdr &inHeader);
void byteSwapSymbolTable(const Elf32_Shdr &header, SectionDataInfo &info);
};
class StELFFileException : public std::runtime_error
{
public:
StELFFileException(const std::string &inMessage)
: std::runtime_error(inMessage)
{
}
};
#endif // _StELFFile_h_
StELFFile::m_header
Elf32_Ehdr m_header
The ELF file header.
Definition: apps/elftosb/common/StELFFile.h:153
Elf32_Shdr
ELF section header.
Definition: apps/elftosb/common/ELF.h:135
StELFFile::getSectionDataAtIndex
uint8_t * getSectionDataAtIndex(unsigned inIndex)
Returns the data for the section.
Definition: apps/elftosb/common/StELFFile.cpp:182
StELFFile::SectionDataInfo
Definition: apps/elftosb/common/StELFFile.h:161
StELFFile::m_sectionDataCache
SectionDataMap m_sectionDataCache
Cached data of sections.
Definition: apps/elftosb/common/StELFFile.h:168
StELFFile::m_elfVariant
ELFVariant_t m_elfVariant
Variant of the ARM ELF format specification.
Definition: apps/elftosb/common/StELFFile.h:151
StELFFile::getSymbolCount
unsigned getSymbolCount()
Returns the number of symbols in the default ".symtab" symbol table.
Definition: apps/elftosb/common/StELFFile.cpp:348
StELFFile::getIndexOfSectionWithName
unsigned getIndexOfSectionWithName(const std::string &inName)
Returns the index of the section with the name inName.
Definition: apps/elftosb/common/StELFFile.cpp:154
StELFFile::getCachedSectionData
SectionDataInfo & getCachedSectionData(unsigned inSectionIndex)
Reads a section&#39;s data either from cache or from disk.
Definition: apps/elftosb/common/StELFFile.cpp:326
StELFFile::byteSwapSymbolTable
void byteSwapSymbolTable(const Elf32_Shdr &header, SectionDataInfo &info)
Byte swaps the symbol table data into host endianness.
Definition: apps/elftosb/common/StELFFile.cpp:381
StELFFile::readFileHeaders
void readFileHeaders()
Reads the file, section, and program headers into memory.
Definition: apps/elftosb/common/StELFFile.cpp:38
StELFFile::m_sectionHeaders
std::vector< Elf32_Shdr > m_sectionHeaders
All of the section headers.
Definition: apps/elftosb/common/StELFFile.h:154
StELFFile::getSectionData
uint8_t * getSectionData(const_section_iterator inSection)
Returns the data for the section.
Definition: apps/elftosb/common/StELFFile.cpp:196
StELFFile::getSegmentAtIndex
const Elf32_Phdr & getSegmentAtIndex(unsigned inIndex) const
Returns a reference to the given segment.
Definition: apps/elftosb/common/StELFFile.cpp:230
StELFFile::readSectionData
uint8_t * readSectionData(const Elf32_Shdr &inHeader)
Definition: apps/elftosb/common/StELFFile.cpp:203
std
STL namespace.
StELFFile::setELFVariant
virtual void setELFVariant(ELFVariant_t variant)
Set the ELF format variation to either #eARMVariant or #eGHSVariant.
Definition: src/blfwk/StELFFile.h:83
StELFFile::ELFVariant
virtual ELFVariant_t ELFVariant()
Return the ELF format variant to which this file is set.
Definition: apps/elftosb/common/StELFFile.h:58
StELFFile::getSymbolNameStringTableIndex
unsigned getSymbolNameStringTableIndex() const
Returns the section index of the string table containing symbol names.
Definition: apps/elftosb/common/StELFFile.cpp:400
StELFFile::m_stream
std::istream & m_stream
The source stream for the ELF file.
Definition: apps/elftosb/common/StELFFile.h:150
Elf32_Phdr
ELF program header.
Definition: apps/elftosb/common/ELF.h:223
StELFFile::m_symbolTableIndex
unsigned m_symbolTableIndex
Index of ".symtab" section, or #SHN_UNDEF if not present.
Definition: apps/elftosb/common/StELFFile.h:156
Elf32_Sym
ELF symbol table entry.
Definition: apps/elftosb/common/ELF.h:277
StELFFile::StELFFile
StELFFile(std::istream &inStream)
Constructor.
Definition: apps/elftosb/common/StELFFile.cpp:16
StELFFile::getSegmentData
uint8_t * getSegmentData(const_segment_iterator inSegment)
Returns the data of the specified segment.
Definition: apps/elftosb/common/StELFFile.cpp:261
StELFFile::~StELFFile
virtual ~StELFFile()
Destructor.
Definition: apps/elftosb/common/StELFFile.cpp:23
StELFFile::readSegmentData
uint8_t * readSegmentData(const Elf32_Phdr &inHeader)
Definition: apps/elftosb/common/StELFFile.cpp:268
StELFFile::getFileHeader
const Elf32_Ehdr & getFileHeader() const
Returns the ELF file header.
Definition: apps/elftosb/common/StELFFile.h:72
StELFFile::getSectionAtIndex
const Elf32_Shdr & getSectionAtIndex(unsigned inIndex) const
Returns a reference to section number inIndex.
Definition: apps/elftosb/common/StELFFile.cpp:144
StELFFile::SectionDataInfo::m_swapped
bool m_swapped
Has this section been byte swapped yet? Used for symbol table.
Definition: apps/elftosb/common/StELFFile.h:165
StELFFile::getSegmentDataAtIndex
uint8_t * getSegmentDataAtIndex(unsigned inIndex)
Returns the data of the specified segment.
Definition: apps/elftosb/common/StELFFile.cpp:247
Elf32_Ehdr
ELF file header.
Definition: apps/elftosb/common/ELF.h:61
StELFFile::getSectionNameAtIndex
std::string getSectionNameAtIndex(unsigned inIndex)
Returns a string from the file&#39;s section name string table.
Definition: apps/elftosb/common/StELFFile.cpp:299
StELFFile::getSectionCount
unsigned getSectionCount() const
Returns the number of sections in the file.
Definition: apps/elftosb/common/StELFFile.h:79
StELFFileException
Simple exception thrown to indicate an error in the input ELF file format.
Definition: apps/elftosb/common/StELFFile.h:186
StELFFile::m_name
std::string m_name
File name. (optional)
Definition: apps/elftosb/common/StELFFile.h:152
StELFFile
Parser for Executable and Linking Format (ELF) files.
Definition: apps/elftosb/common/StELFFile.h:42
StELFFile::getStringAtIndex
std::string getStringAtIndex(unsigned inStringTableSectionIndex, unsigned inStringIndex)
Returns a string from any string table in the object file.
Definition: apps/elftosb/common/StELFFile.cpp:311
StELFFileException::StELFFileException
StELFFileException(const std::string &inMessage)
Default constructor.
Definition: src/blfwk/StELFFile.h:213
StELFFile::SectionDataInfo::m_size
unsigned m_size
Section data size in bytes.
Definition: apps/elftosb/common/StELFFile.h:164
StELFFile::getIndexOfSymbolAtAddress
unsigned getIndexOfSymbolAtAddress(uint32_t symbolAddress, bool strict=true)
Returns STN_UNDEF if it cannot find a symbol at the given symbolAddress.
Definition: apps/elftosb/common/StELFFile.cpp:413
StELFFile::m_programHeaders
std::vector< Elf32_Phdr > m_programHeaders
All of the program headers.
Definition: apps/elftosb/common/StELFFile.h:155
StELFFile::getSegmentCount
unsigned getSegmentCount() const
Returns the number of segments, or program headers, in the file.
Definition: apps/elftosb/common/StELFFile.h:99
StELFFile::getSymbolAtIndex
const Elf32_Sym & getSymbolAtIndex(unsigned inIndex)
Returns the symbol with index inIndex.
Definition: apps/elftosb/common/StELFFile.cpp:359
StELFFile::getSymbolName
std::string getSymbolName(const Elf32_Sym &inSymbol)
Returns the name of the symbol described by inSymbol.
Definition: apps/elftosb/common/StELFFile.cpp:406
StELFFile::SectionDataInfo::m_data
uint8_t * m_data
Pointer to section data.
Definition: apps/elftosb/common/StELFFile.h:163