src/blfwk/SourceFile.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(_SourceFile_h_)
#define _SourceFile_h_
#include <string>
#include <iostream>
#include <fstream>
#include "smart_ptr.h"
#include "DataSource.h"
#include "DataTarget.h"
#include "StringMatcher.h"
#include "OptionContext.h"
namespace blfwk
{
class SourceFile
{
public:
// \brief Factory function that creates the correct subclass of SourceFile.
static SourceFile *openFile(const std::string &path);
enum source_file_t
{
kBinarySourceFile,
kELFSourceFile,
kIntelHexSourceFile,
kSBSourceFile,
kSRecordSourceFile
};
public:
SourceFile(const std::string &path, source_file_t filetype);
virtual ~SourceFile();
inline void setOptions(OptionContext *context) { m_options = context; }
inline const OptionContext *getOptions() const { return m_options; }
inline source_file_t getFileType() const { return m_filetype; }
inline const std::string &getPath() const { return m_path; }
unsigned getSize() const { return m_size; }
virtual void open();
virtual void close();
virtual bool isOpen() const { return (bool)m_stream && const_cast<std::ifstream *>(m_stream.get())->is_open(); }
virtual bool supportsNamedSections() const = 0;
virtual bool supportsNamedSymbols() const = 0;
virtual DataSource *createDataSource() = 0;
virtual DataSource *createDataSource(StringMatcher &matcher) { return NULL; }
virtual DataSource *createDataSource(const std::string &section);
virtual bool hasEntryPoint() = 0;
virtual uint32_t getEntryPointAddress() { return 0; }
virtual DataTarget *createDataTargetForSection(const std::string &section) { return NULL; }
virtual DataTarget *createDataTargetForSymbol(const std::string &symbol) { return NULL; }
virtual DataTarget *createDataTargetForEntryPoint();
virtual bool hasSymbol(const std::string &name) { return false; }
virtual uint32_t getSymbolValue(const std::string &name) { return 0; }
virtual unsigned getSymbolSize(const std::string &name) { return 0; }
protected:
std::string m_path;
smart_ptr<std::ifstream> m_stream;
smart_ptr<OptionContext> m_options;
source_file_t m_filetype;
unsigned m_size;
inline std::ifstream *getStream() { return m_stream; }
};
class BinarySourceFile : public SourceFile
{
public:
BinarySourceFile(const std::string &path, source_file_t sourceFileType = kBinarySourceFile);
virtual bool supportsNamedSections() const { return false; }
virtual bool supportsNamedSymbols() const { return false; }
virtual DataSource *createDataSource();
void guessEntryPointAndStackPointer();
virtual bool hasEntryPoint() { return m_entry_point != 0xffffffff; }
virtual uint32_t getEntryPointAddress() { return m_entry_point; }
uint32_t getStackPointer() { return m_stack_pointer; }
protected:
uint32_t m_entry_point;
uint32_t m_stack_pointer;
};
}; // namespace blfwk
#endif // _SourceFile_h_
blfwk::SourceFile::m_options
smart_ptr< OptionContext > m_options
Table of option values.
Definition: src/blfwk/SourceFile.h:154
blfwk::SourceFile::kSBSourceFile
Definition: src/blfwk/SourceFile.h:68
smart_ptr::get
ptr_type get()
Return the current pointer value.
Definition: apps/elftosb/common/smart_ptr.h:43
blfwk
Definition: BlfwkErrors.h:16
blfwk::SourceFile::kIntelHexSourceFile
Definition: src/blfwk/SourceFile.h:67
blfwk::SourceFile::source_file_t
source_file_t
Set of supported executable image file formats.
Definition: src/blfwk/SourceFile.h:63
blfwk::SourceFile::getSymbolValue
virtual uint32_t getSymbolValue(const std::string &name)
Returns the value of a symbol.
Definition: src/blfwk/SourceFile.h:146
blfwk::BinarySourceFile::hasEntryPoint
virtual bool hasEntryPoint()
Returns true if an entry point was set in the file.
Definition: src/blfwk/SourceFile.h:190
blfwk::BinarySourceFile::getEntryPointAddress
virtual uint32_t getEntryPointAddress()
Returns the entry point address.
Definition: src/blfwk/SourceFile.h:192
smart_ptr< std::ifstream >
blfwk::BinarySourceFile
Binary data file.
Definition: src/blfwk/SourceFile.h:165
blfwk::SourceFile::close
virtual void close()
Closes the file.
Definition: src/blfwk/src/SourceFile.cpp:147
blfwk::SourceFile::m_stream
smart_ptr< std::ifstream > m_stream
File stream, or NULL if file is closed.
Definition: src/blfwk/SourceFile.h:153
blfwk::DataTarget
Abstract base class for the target address or range of data.
Definition: src/blfwk/DataTarget.h:57
blfwk::OptionContext
Pure abstract interface class to a table of options.
Definition: src/blfwk/OptionContext.h:41
smart_ptr
Simple, standard smart pointer class.
Definition: apps/elftosb/common/smart_ptr.h:18
blfwk::SourceFile::m_path
std::string m_path
Path to the file.
Definition: src/blfwk/SourceFile.h:152
blfwk::SourceFile::openFile
static SourceFile * openFile(const std::string &path)
Definition: src/blfwk/src/SourceFile.cpp:62
blfwk::SourceFile::m_size
unsigned m_size
The size in bytes of the file.
Definition: src/blfwk/SourceFile.h:156
blfwk::SourceFile::getSize
unsigned getSize() const
Get the size in bytes of the file.
Definition: src/blfwk/SourceFile.h:91
blfwk::SourceFile::createDataSource
virtual DataSource * createDataSource()=0
Creates a data source from the entire file.
blfwk::SourceFile
Abstract base class for a source file containing executable code.
Definition: src/blfwk/SourceFile.h:56
blfwk::SourceFile::SourceFile
SourceFile(const std::string &path, source_file_t filetype)
Default constructor.
Definition: src/blfwk/src/SourceFile.cpp:113
blfwk::SourceFile::hasEntryPoint
virtual bool hasEntryPoint()=0
Returns true if an entry point was set in the file.
blfwk::SourceFile::getEntryPointAddress
virtual uint32_t getEntryPointAddress()
Returns the entry point address.
Definition: src/blfwk/SourceFile.h:131
blfwk::SourceFile::getPath
const std::string & getPath() const
Returns the path to the file.
Definition: src/blfwk/SourceFile.h:89
blfwk::SourceFile::hasSymbol
virtual bool hasSymbol(const std::string &name)
Returns whether a symbol exists in the source file.
Definition: src/blfwk/SourceFile.h:144
blfwk::SourceFile::m_filetype
source_file_t m_filetype
Image file type.
Definition: src/blfwk/SourceFile.h:155
blfwk::StringMatcher
Abstract interface class used to select strings by name.
Definition: src/blfwk/StringMatcher.h:40
blfwk::SourceFile::open
virtual void open()
Opens the file.
Definition: src/blfwk/src/SourceFile.cpp:137
blfwk::SourceFile::~SourceFile
virtual ~SourceFile()
Destructor.
Definition: src/blfwk/src/SourceFile.cpp:128
blfwk::SourceFile::createDataSource
virtual DataSource * createDataSource(StringMatcher &matcher)
Creates a data source out of one or more sections of the file.
Definition: src/blfwk/SourceFile.h:120
blfwk::SourceFile::getFileType
source_file_t getFileType() const
Return the file type.
Definition: src/blfwk/SourceFile.h:87
blfwk::DataSource
Abstract base class for data sources.
Definition: src/blfwk/DataSource.h:56
blfwk::SourceFile::getOptions
const OptionContext * getOptions() const
Return the option context.
Definition: src/blfwk/SourceFile.h:85
blfwk::SourceFile::getStream
std::ifstream * getStream()
Internal access to the input stream object.
Definition: src/blfwk/SourceFile.h:159
blfwk::SourceFile::kELFSourceFile
Definition: src/blfwk/SourceFile.h:66
blfwk::BinarySourceFile::getStackPointer
uint32_t getStackPointer()
Returns the stack pointer.
Definition: src/blfwk/SourceFile.h:194
blfwk::SourceFile::isOpen
virtual bool isOpen() const
Returns whether the file is already open.
Definition: src/blfwk/SourceFile.h:101
blfwk::SourceFile::kSRecordSourceFile
Definition: src/blfwk/SourceFile.h:69
blfwk::SourceFile::getSymbolSize
virtual unsigned getSymbolSize(const std::string &name)
Returns the size of a symbol.
Definition: src/blfwk/SourceFile.h:148
blfwk::SourceFile::kBinarySourceFile
Definition: src/blfwk/SourceFile.h:65
blfwk::SourceFile::setOptions
void setOptions(OptionContext *context)
Set the option context.
Definition: src/blfwk/SourceFile.h:83