src/blfwk/ELFSourceFile.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(_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 blfwk
{
enum elf_toolset_t
{
kUnknownToolset,
kGHSToolset,
kGCCToolset,
kADSToolset
};
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);
// baseAddress
virtual DataSource *createDataSource(const std::vector<uint32_t> &baseAddress, bool match);
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 blfwk
#endif // _ELFSourceFile_h_
blfwk::ELFSourceFile::ELFDataSource::~ELFDataSource
virtual ~ELFDataSource()
Destructor.
Definition: src/blfwk/src/ELFSourceFile.cpp:493
blfwk::ELFSourceFile::ELFDataSource::NoBitsSegment
Represents one named #SHT_NOBITS section within the ELF file.
Definition: src/blfwk/ELFSourceFile.h:204
blfwk::ELFSourceFile::getELFFile
StELFFile * getELFFile()
Returns the underlying StELFFile object.
Definition: src/blfwk/ELFSourceFile.h:143
blfwk::ELFSourceFile::ELFDataSource::m_elf
StELFFile * m_elf
The ELF file parser.
Definition: src/blfwk/ELFSourceFile.h:240
blfwk::ELFSourceFile::ELFDataSource
A data source with ELF file sections as the contents.
Definition: src/blfwk/ELFSourceFile.h:176
blfwk::ELFSourceFile::ELFDataSource::ProgBitsSegment::hasNaturalLocation
virtual bool hasNaturalLocation()
Returns whether the segment has an associated address.
Definition: src/blfwk/ELFSourceFile.h:190
blfwk
Definition: BlfwkErrors.h:16
blfwk::ELFSourceFile::m_toolset
elf_toolset_t m_toolset
Toolset that produced the ELF file.
Definition: src/blfwk/ELFSourceFile.h:150
blfwk::ELFSourceFile::ELFDataSource::getSegmentCount
virtual unsigned getSegmentCount()
Returns the number of segments in the source.
Definition: src/blfwk/ELFSourceFile.h:236
blfwk::ELFSourceFile::readToolsetOption
elf_toolset_t readToolsetOption()
Parses the toolset option value.
Definition: src/blfwk/src/ELFSourceFile.cpp:122
blfwk::ELFSourceFile::ELFDataSource::addSection
void addSection(unsigned sectionIndex)
Adds the ELF section at position sectionIndex to the data source.
Definition: src/blfwk/src/ELFSourceFile.cpp:512
smart_ptr< StELFFile >
blfwk::ELFSourceFile::ELFDataSource::NoBitsSegment::m_sectionIndex
unsigned m_sectionIndex
The index of the section this segment represents.
Definition: src/blfwk/ELFSourceFile.h:216
blfwk::ELFSourceFile::m_secinfoOption
secinfo_clear_t m_secinfoOption
How to deal with the .secinfo section. Ignored if the toolset is not GHS.
Definition: src/blfwk/ELFSourceFile.h:151
blfwk::ELFSourceFile::ELFDataSource::setSecinfoOption
void setSecinfoOption(secinfo_clear_t option)
Set the option to control .secinfo usage.
Definition: src/blfwk/ELFSourceFile.h:231
blfwk::ELFSourceFile::ELFDataSource::NoBitsSegment::m_elf
StELFFile * m_elf
The format parser instance for this ELF file.
Definition: src/blfwk/ELFSourceFile.h:215
blfwk::ELFSourceFile::ELFDataSource::ELFDataSource
ELFDataSource(StELFFile *elf)
Default constructor.
Definition: src/blfwk/ELFSourceFile.h:221
blfwk::DataTarget
Abstract base class for the target address or range of data.
Definition: src/blfwk/DataTarget.h:57
Elf32_Sym
ELF symbol table entry.
Definition: apps/elftosb/common/ELF.h:277
blfwk::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: src/blfwk/src/ELFSourceFile.cpp:587
blfwk::ELFSourceFile::close
virtual void close()
Closes the file.
Definition: src/blfwk/src/ELFSourceFile.cpp:115
blfwk::ELFSourceFile::m_file
smart_ptr< StELFFile > m_file
Parser for the ELF file.
Definition: src/blfwk/ELFSourceFile.h:149
blfwk::ELFSourceFile::ELFDataSource::segment_vector_t
std::vector< DataSource::Segment * > segment_vector_t
A list of segment instances.
Definition: src/blfwk/ELFSourceFile.h:243
blfwk::ELFSourceFile::~ELFSourceFile
virtual ~ELFSourceFile()
Destructor.
Definition: src/blfwk/src/ELFSourceFile.cpp:63
blfwk::SourceFile
Abstract base class for a source file containing executable code.
Definition: src/blfwk/SourceFile.h:56
blfwk::ELFSourceFile::ELFDataSource::ProgBitsSegment::getBaseAddress
virtual uint32_t getBaseAddress()
Returns the address associated with the segment.
Definition: src/blfwk/src/ELFSourceFile.cpp:609
blfwk::ELFSourceFile::hasSymbol
virtual bool hasSymbol(const std::string &name)
Returns whether a symbol exists in the source file.
Definition: src/blfwk/src/ELFSourceFile.cpp:419
blfwk::ELFSourceFile::ELFDataSource::getSegmentAt
virtual DataSource::Segment * getSegmentAt(unsigned index)
Returns the segment at position index.
Definition: src/blfwk/ELFSourceFile.h:238
blfwk::ELFSourceFile::lookupSymbol
bool lookupSymbol(const std::string &name, Elf32_Sym &info)
Gets information about a symbol in the ELF file.
Definition: src/blfwk/src/ELFSourceFile.cpp:470
blfwk::ELFSourceFile::ELFDataSource::ProgBitsSegment::getLength
virtual unsigned getLength()
Gets the length of the segment&#39;s data.
Definition: src/blfwk/src/ELFSourceFile.cpp:603
blfwk::ELFSourceFile::ELFDataSource::m_segments
segment_vector_t m_segments
The segments of this data source.
Definition: src/blfwk/ELFSourceFile.h:244
blfwk::ELFSourceFile::ELFSourceFile
ELFSourceFile(const std::string &path)
Default constructor.
Definition: src/blfwk/src/ELFSourceFile.cpp:56
blfwk::StringMatcher
Abstract interface class used to select strings by name.
Definition: src/blfwk/StringMatcher.h:40
blfwk::DataSource::Segment
Discrete, contiguous part of the source&#39;s data.
Definition: src/blfwk/DataSource.h:65
blfwk::ELFSourceFile::ELFDataSource::ProgBitsSegment
Represents one named #SHT_PROGBITS section within the ELF file.
Definition: src/blfwk/ELFSourceFile.h:182
blfwk::ELFSourceFile::readSecinfoClearOption
secinfo_clear_t readSecinfoClearOption()
Reads the secinfoClear option.
Definition: src/blfwk/src/ELFSourceFile.cpp:169
blfwk::ELFSourceFile::getSymbolSize
virtual unsigned getSymbolSize(const std::string &name)
Returns the size of a symbol.
Definition: src/blfwk/src/ELFSourceFile.cpp:454
blfwk::ELFSourceFile::getEntryPointAddress
virtual uint32_t getEntryPointAddress()
Returns the entry point address.
Definition: src/blfwk/src/ELFSourceFile.cpp:342
blfwk::ELFSourceFile::ELFDataSource::ProgBitsSegment::m_elf
StELFFile * m_elf
The format parser instance for this ELF file.
Definition: src/blfwk/ELFSourceFile.h:194
StELFFile
Parser for Executable and Linking Format (ELF) files.
Definition: apps/elftosb/common/StELFFile.h:42
blfwk::ELFSourceFile
Executable and Loading Format (ELF) source file.
Definition: src/blfwk/ELFSourceFile.h:72
blfwk::ELFSourceFile::isELFFile
static bool isELFFile(std::istream &stream)
Identifies whether the stream contains an ELF file.
Definition: src/blfwk/src/ELFSourceFile.cpp:67
blfwk::DataSource
Abstract base class for data sources.
Definition: src/blfwk/DataSource.h:56
blfwk::ELFSourceFile::ELFDataSource::m_secinfoOption
secinfo_clear_t m_secinfoOption
How to deal with the .secinfo section. Ignored if the toolset is not GHS.
Definition: src/blfwk/ELFSourceFile.h:241
blfwk::ELFSourceFile::createDataTargetForSection
virtual DataTarget * createDataTargetForSection(const std::string &section)
Definition: src/blfwk/src/ELFSourceFile.cpp:375
blfwk::ELFSourceFile::ELFDataSource::ProgBitsSegment::m_sectionIndex
unsigned m_sectionIndex
The index of the section this segment represents.
Definition: src/blfwk/ELFSourceFile.h:195
blfwk::ELFSourceFile::ELFDataSource::NoBitsSegment::hasNaturalLocation
virtual bool hasNaturalLocation()
Pattern segments have no natural address.
Definition: src/blfwk/ELFSourceFile.h:211
blfwk::ELFSourceFile::hasEntryPoint
virtual bool hasEntryPoint()
Returns true if an entry point was set in the file.
Definition: src/blfwk/src/ELFSourceFile.cpp:335
blfwk::ELFSourceFile::createDataTargetForSymbol
virtual DataTarget * createDataTargetForSymbol(const std::string &symbol)
Definition: src/blfwk/src/ELFSourceFile.cpp:393
blfwk::ELFSourceFile::open
virtual void open()
Opens the file.
Definition: src/blfwk/src/ELFSourceFile.cpp:80
blfwk::ELFSourceFile::getSymbolValue
virtual uint32_t getSymbolValue(const std::string &name)
Returns the value of a symbol.
Definition: src/blfwk/src/ELFSourceFile.cpp:425
blfwk::ELFSourceFile::createDataSource
virtual DataSource * createDataSource()
Creates a data source from the entire file.
Definition: src/blfwk/src/ELFSourceFile.cpp:220
blfwk::DataSource::PatternSegment
This is a special type of segment containing a repeating pattern.
Definition: src/blfwk/DataSource.h:110