apps/elftosb/common/ELFSourceFile.h
/*
* File: ELFSourceFile.h
*
* Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
* See included license file for license details.
*/
#if !defined(_ELFSourceFile_h_)
#define _ELFSourceFile_h_
#include "SourceFile.h"
#include "StELFFile.h"
#include "smart_ptr.h"
#include "DataSource.h"
#include "DataTarget.h"
#include "ELF.h"
namespace elftosb
{
enum elf_toolset_t
{
kUnknownToolset,
kGHSToolset,
kGCCToolset,
kADSToolset,
kIARToolset,
kKDSToolset,
kKeilToolset
};
enum secinfo_clear_t
{
// Default value for the .secinfo action.
kSecinfoDefault,
kSecinfoIgnore,
kSecinfoROMClear,
kSecinfoCStartupClear
};
class ELFSourceFile : public SourceFile
{
public:
ELFSourceFile(const std::string &path);
virtual ~ELFSourceFile();
static bool isELFFile(std::istream &stream);
virtual void open();
virtual void close();
virtual bool supportsNamedSections() const { return true; }
virtual bool supportsNamedSymbols() const { return true; }
virtual DataSource *createDataSource();
virtual DataSource *createDataSource(StringMatcher &matcher);
virtual bool hasEntryPoint();
virtual uint32_t getEntryPointAddress();
virtual DataTarget *createDataTargetForSection(const std::string &section);
virtual DataTarget *createDataTargetForSymbol(const std::string &symbol);
virtual bool hasSymbol(const std::string &name);
virtual uint32_t getSymbolValue(const std::string &name);
virtual unsigned getSymbolSize(const std::string &name);
StELFFile *getELFFile() { return m_file; }
bool lookupSymbol(const std::string &name, Elf32_Sym &info);
protected:
smart_ptr<StELFFile> m_file;
elf_toolset_t m_toolset;
secinfo_clear_t m_secinfoOption;
protected:
elf_toolset_t readToolsetOption();
secinfo_clear_t readSecinfoClearOption();
protected:
class ELFDataSource : public DataSource
{
public:
class ProgBitsSegment : public DataSource::Segment
{
public:
ProgBitsSegment(ELFDataSource &source, StELFFile *elf, unsigned index);
virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer);
virtual unsigned getLength();
virtual bool hasNaturalLocation() { return true; }
virtual uint32_t getBaseAddress();
protected:
StELFFile *m_elf;
unsigned m_sectionIndex;
};
class NoBitsSegment : public DataSource::PatternSegment
{
public:
NoBitsSegment(ELFDataSource &source, StELFFile *elf, unsigned index);
virtual unsigned getLength();
virtual bool hasNaturalLocation() { return true; }
virtual uint32_t getBaseAddress();
protected:
StELFFile *m_elf;
unsigned m_sectionIndex;
};
public:
ELFDataSource(StELFFile *elf)
: DataSource()
, m_elf(elf)
{
}
virtual ~ELFDataSource();
inline void setSecinfoOption(secinfo_clear_t option) { m_secinfoOption = option; }
void addSection(unsigned sectionIndex);
virtual unsigned getSegmentCount() { return (unsigned)m_segments.size(); }
virtual DataSource::Segment *getSegmentAt(unsigned index) { return m_segments[index]; }
protected:
StELFFile *m_elf;
secinfo_clear_t m_secinfoOption;
typedef std::vector<DataSource::Segment *> segment_vector_t;
segment_vector_t m_segments;
};
};
}; // namespace elftosb
#endif // _ELFSourceFile_h_
elftosb::ELFSourceFile::m_file
smart_ptr< StELFFile > m_file
Parser for the ELF file.
Definition: apps/elftosb/common/ELFSourceFile.h:124
elftosb::ELFSourceFile::ELFDataSource::NoBitsSegment
Represents one named #SHT_NOBITS section within the ELF file.
Definition: apps/elftosb/common/ELFSourceFile.h:179
elftosb::ELFSourceFile::ELFDataSource::ProgBitsSegment::m_sectionIndex
unsigned m_sectionIndex
The index of the section this segment represents.
Definition: apps/elftosb/common/ELFSourceFile.h:170
elftosb::ELFSourceFile::getELFFile
StELFFile * getELFFile()
Returns the underlying StELFFile object.
Definition: apps/elftosb/common/ELFSourceFile.h:118
elftosb::ELFSourceFile::ELFDataSource::ProgBitsSegment::getLength
virtual unsigned getLength()
Gets the length of the segment&#39;s data.
Definition: apps/elftosb/common/ELFSourceFile.cpp:517
elftosb::DataSource
Abstract base class for data sources.
Definition: apps/elftosb/common/DataSource.h:33
elftosb::ELFSourceFile::readToolsetOption
elf_toolset_t readToolsetOption()
Parses the toolset option value.
Definition: apps/elftosb/common/ELFSourceFile.cpp:103
elftosb::ELFSourceFile::createDataSource
virtual DataSource * createDataSource()
Creates a data source from the entire file.
Definition: apps/elftosb/common/ELFSourceFile.cpp:202
elftosb::ELFSourceFile::hasSymbol
virtual bool hasSymbol(const std::string &name)
Returns whether a symbol exists in the source file.
Definition: apps/elftosb/common/ELFSourceFile.cpp:335
elftosb::ELFSourceFile::createDataTargetForSection
virtual DataTarget * createDataTargetForSection(const std::string &section)
Definition: apps/elftosb/common/ELFSourceFile.cpp:291
elftosb::ELFSourceFile::ELFSourceFile
ELFSourceFile(const std::string &path)
Default constructor.
Definition: apps/elftosb/common/ELFSourceFile.cpp:34
elftosb::ELFSourceFile::ELFDataSource::m_segments
segment_vector_t m_segments
The segments of this data source.
Definition: apps/elftosb/common/ELFSourceFile.h:219
elftosb::ELFSourceFile::ELFDataSource::segment_vector_t
std::vector< DataSource::Segment * > segment_vector_t
A list of segment instances.
Definition: apps/elftosb/common/ELFSourceFile.h:218
elftosb::ELFSourceFile::ELFDataSource::ELFDataSource
ELFDataSource(StELFFile *elf)
Default constructor.
Definition: apps/elftosb/common/ELFSourceFile.h:196
elftosb::ELFSourceFile::ELFDataSource::m_elf
StELFFile * m_elf
The ELF file parser.
Definition: apps/elftosb/common/ELFSourceFile.h:215
elftosb::ELFSourceFile::createDataTargetForSymbol
virtual DataTarget * createDataTargetForSymbol(const std::string &symbol)
Definition: apps/elftosb/common/ELFSourceFile.cpp:309
smart_ptr< StELFFile >
elftosb::ELFSourceFile::m_secinfoOption
secinfo_clear_t m_secinfoOption
How to deal with the .secinfo section. Ignored if the toolset is not GHS.
Definition: apps/elftosb/common/ELFSourceFile.h:126
elftosb::ELFSourceFile::hasEntryPoint
virtual bool hasEntryPoint()
Returns true if an entry point was set in the file.
Definition: apps/elftosb/common/ELFSourceFile.cpp:251
elftosb::ELFSourceFile::ELFDataSource::ProgBitsSegment::getBaseAddress
virtual uint32_t getBaseAddress()
Returns the address associated with the segment.
Definition: apps/elftosb/common/ELFSourceFile.cpp:523
elftosb::ELFSourceFile::readSecinfoClearOption
secinfo_clear_t readSecinfoClearOption()
Reads the secinfoClear option.
Definition: apps/elftosb/common/ELFSourceFile.cpp:151
elftosb::ELFSourceFile::getSymbolSize
virtual unsigned getSymbolSize(const std::string &name)
Returns the size of a symbol.
Definition: apps/elftosb/common/ELFSourceFile.cpp:370
Elf32_Sym
ELF symbol table entry.
Definition: apps/elftosb/common/ELF.h:277
elftosb::ELFSourceFile::getEntryPointAddress
virtual uint32_t getEntryPointAddress()
Returns the entry point address.
Definition: apps/elftosb/common/ELFSourceFile.cpp:258
elftosb::ELFSourceFile::lookupSymbol
bool lookupSymbol(const std::string &name, Elf32_Sym &info)
Gets information about a symbol in the ELF file.
Definition: apps/elftosb/common/ELFSourceFile.cpp:386
elftosb
Definition: BootImage.h:13
elftosb::SourceFile
Abstract base class for a source file containing executable code.
Definition: apps/elftosb/common/SourceFile.h:33
elftosb::DataTarget
Abstract base class for the target address or range of data.
Definition: apps/elftosb/common/DataTarget.h:34
elftosb::DataSource::PatternSegment
This is a special type of segment containing a repeating pattern.
Definition: apps/elftosb/common/DataSource.h:87
elftosb::ELFSourceFile::ELFDataSource::ProgBitsSegment
Represents one named #SHT_PROGBITS section within the ELF file.
Definition: apps/elftosb/common/ELFSourceFile.h:157
elftosb::ELFSourceFile::ELFDataSource::~ELFDataSource
virtual ~ELFDataSource()
Destructor.
Definition: apps/elftosb/common/ELFSourceFile.cpp:409
elftosb::ELFSourceFile::ELFDataSource::getSegmentCount
virtual unsigned getSegmentCount()
Returns the number of segments in the source.
Definition: apps/elftosb/common/ELFSourceFile.h:211
elftosb::ELFSourceFile::ELFDataSource
A data source with ELF file sections as the contents.
Definition: apps/elftosb/common/ELFSourceFile.h:151
elftosb::ELFSourceFile::ELFDataSource::addSection
void addSection(unsigned sectionIndex)
Adds the ELF section at position sectionIndex to the data source.
Definition: apps/elftosb/common/ELFSourceFile.cpp:428
elftosb::ELFSourceFile::isELFFile
static bool isELFFile(std::istream &stream)
Identifies whether the stream contains an ELF file.
Definition: apps/elftosb/common/ELFSourceFile.cpp:45
elftosb::ELFSourceFile::m_toolset
elf_toolset_t m_toolset
Toolset that produced the ELF file.
Definition: apps/elftosb/common/ELFSourceFile.h:125
elftosb::DataSource::Segment
Discrete, contiguous part of the source&#39;s data.
Definition: apps/elftosb/common/DataSource.h:42
elftosb::ELFSourceFile::ELFDataSource::NoBitsSegment::m_sectionIndex
unsigned m_sectionIndex
The index of the section this segment represents.
Definition: apps/elftosb/common/ELFSourceFile.h:191
elftosb::ELFSourceFile::~ELFSourceFile
virtual ~ELFSourceFile()
Destructor.
Definition: apps/elftosb/common/ELFSourceFile.cpp:41
elftosb::ELFSourceFile::ELFDataSource::NoBitsSegment::m_elf
StELFFile * m_elf
The format parser instance for this ELF file.
Definition: apps/elftosb/common/ELFSourceFile.h:190
elftosb::ELFSourceFile::ELFDataSource::ProgBitsSegment::hasNaturalLocation
virtual bool hasNaturalLocation()
Returns whether the segment has an associated address.
Definition: apps/elftosb/common/ELFSourceFile.h:165
StELFFile
Parser for Executable and Linking Format (ELF) files.
Definition: apps/elftosb/common/StELFFile.h:42
elftosb::ELFSourceFile
Executable and Loading Format (ELF) source file.
Definition: apps/elftosb/common/ELFSourceFile.h:52
elftosb::ELFSourceFile::getSymbolValue
virtual uint32_t getSymbolValue(const std::string &name)
Returns the value of a symbol.
Definition: apps/elftosb/common/ELFSourceFile.cpp:341
elftosb::ELFSourceFile::ELFDataSource::ProgBitsSegment::getData
virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer)
Gets all or a portion of the segment&#39;s data.
Definition: apps/elftosb/common/ELFSourceFile.cpp:501
elftosb::ELFSourceFile::ELFDataSource::ProgBitsSegment::m_elf
StELFFile * m_elf
The format parser instance for this ELF file.
Definition: apps/elftosb/common/ELFSourceFile.h:169
elftosb::StringMatcher
Abstract interface class used to select strings by name.
Definition: apps/elftosb/common/StringMatcher.h:17
elftosb::ELFSourceFile::ELFDataSource::setSecinfoOption
void setSecinfoOption(secinfo_clear_t option)
Set the option to control .secinfo usage.
Definition: apps/elftosb/common/ELFSourceFile.h:206
elftosb::ELFSourceFile::ELFDataSource::getSegmentAt
virtual DataSource::Segment * getSegmentAt(unsigned index)
Returns the segment at position index.
Definition: apps/elftosb/common/ELFSourceFile.h:213
elftosb::ELFSourceFile::ELFDataSource::m_secinfoOption
secinfo_clear_t m_secinfoOption
How to deal with the .secinfo section. Ignored if the toolset is not GHS.
Definition: apps/elftosb/common/ELFSourceFile.h:216
elftosb::ELFSourceFile::ELFDataSource::NoBitsSegment::hasNaturalLocation
virtual bool hasNaturalLocation()
Pattern segments have no natural address.
Definition: apps/elftosb/common/ELFSourceFile.h:186
elftosb::ELFSourceFile::close
virtual void close()
Closes the file.
Definition: apps/elftosb/common/ELFSourceFile.cpp:96
elftosb::ELFSourceFile::open
virtual void open()
Opens the file.
Definition: apps/elftosb/common/ELFSourceFile.cpp:58