apps/elftosb/common/SourceFile.h
/*
* File: SourceFile.h
*
* Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
* See included license file for license details.
*/
#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 elftosb
{
class SourceFile
{
public:
// \brief Factory function that creates the correct subclass of SourceFile.
static SourceFile *openFile(const std::string &path);
public:
SourceFile(const std::string &path);
virtual ~SourceFile();
inline void setOptions(OptionContext *context) { m_options = context; }
inline const OptionContext *getOptions() const { return m_options; }
inline const std::string &getPath() const { return m_path; }
unsigned getSize();
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;
inline std::ifstream *getStream() { return m_stream; }
};
class BinarySourceFile : public SourceFile
{
public:
BinarySourceFile(const std::string &path)
: SourceFile(path)
{
}
virtual bool supportsNamedSections() const { return false; }
virtual bool supportsNamedSymbols() const { return false; }
virtual DataSource *createDataSource();
virtual bool hasEntryPoint() { return false; }
};
}; // namespace elftosb
#endif // _SourceFile_h_
elftosb::SourceFile::close
virtual void close()
Closes the file.
Definition: apps/elftosb/common/SourceFile.cpp:100
elftosb::DataSource
Abstract base class for data sources.
Definition: apps/elftosb/common/DataSource.h:33
smart_ptr::get
ptr_type get()
Return the current pointer value.
Definition: apps/elftosb/common/smart_ptr.h:43
smart_ptr< std::ifstream >
elftosb::SourceFile::createDataSource
virtual DataSource * createDataSource(StringMatcher &matcher)
Creates a data source out of one or more sections of the file.
Definition: apps/elftosb/common/SourceFile.h:86
elftosb::SourceFile::getOptions
const OptionContext * getOptions() const
Return the option context.
Definition: apps/elftosb/common/SourceFile.h:52
smart_ptr
Simple, standard smart pointer class.
Definition: apps/elftosb/common/smart_ptr.h:18
elftosb::SourceFile::getSymbolSize
virtual unsigned getSymbolSize(const std::string &name)
Returns the size of a symbol.
Definition: apps/elftosb/common/SourceFile.h:114
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::SourceFile::createDataSource
virtual DataSource * createDataSource()=0
Creates a data source from the entire file.
elftosb::SourceFile::~SourceFile
virtual ~SourceFile()
Destructor.
Definition: apps/elftosb/common/SourceFile.cpp:81
elftosb::SourceFile::isOpen
virtual bool isOpen() const
Returns whether the file is already open.
Definition: apps/elftosb/common/SourceFile.h:67
elftosb::SourceFile::hasEntryPoint
virtual bool hasEntryPoint()=0
Returns true if an entry point was set in the file.
elftosb::SourceFile::getPath
const std::string & getPath() const
Returns the path to the file.
Definition: apps/elftosb/common/SourceFile.h:54
elftosb::OptionContext
Pure abstract interface class to a table of options.
Definition: apps/elftosb/common/OptionContext.h:18
elftosb::SourceFile::hasSymbol
virtual bool hasSymbol(const std::string &name)
Returns whether a symbol exists in the source file.
Definition: apps/elftosb/common/SourceFile.h:110
elftosb::SourceFile::m_path
std::string m_path
Path to the file.
Definition: apps/elftosb/common/SourceFile.h:118
elftosb::SourceFile::getSymbolValue
virtual uint32_t getSymbolValue(const std::string &name)
Returns the value of a symbol.
Definition: apps/elftosb/common/SourceFile.h:112
elftosb::SourceFile::open
virtual void open()
Opens the file.
Definition: apps/elftosb/common/SourceFile.cpp:90
elftosb::SourceFile::getEntryPointAddress
virtual uint32_t getEntryPointAddress()
Returns the entry point address.
Definition: apps/elftosb/common/SourceFile.h:97
elftosb::SourceFile::setOptions
void setOptions(OptionContext *context)
Set the option context.
Definition: apps/elftosb/common/SourceFile.h:50
elftosb::BinarySourceFile::hasEntryPoint
virtual bool hasEntryPoint()
Returns true if an entry point was set in the file.
Definition: apps/elftosb/common/SourceFile.h:147
elftosb::BinarySourceFile
Binary data file.
Definition: apps/elftosb/common/SourceFile.h:129
elftosb::SourceFile::m_options
smart_ptr< OptionContext > m_options
Table of option values.
Definition: apps/elftosb/common/SourceFile.h:120
elftosb::SourceFile::openFile
static SourceFile * openFile(const std::string &path)
Definition: apps/elftosb/common/SourceFile.cpp:32
elftosb::StringMatcher
Abstract interface class used to select strings by name.
Definition: apps/elftosb/common/StringMatcher.h:17
elftosb::SourceFile::m_stream
smart_ptr< std::ifstream > m_stream
File stream, or NULL if file is closed.
Definition: apps/elftosb/common/SourceFile.h:119
elftosb::SourceFile::getStream
std::ifstream * getStream()
Internal access to the input stream object.
Definition: apps/elftosb/common/SourceFile.h:123
elftosb::SourceFile::getSize
unsigned getSize()
Get the size in bytes of the file.
Definition: apps/elftosb/common/SourceFile.cpp:108
elftosb::SourceFile::SourceFile
SourceFile(const std::string &path)
Default constructor.
Definition: apps/elftosb/common/SourceFile.cpp:73
elftosb::BinarySourceFile::BinarySourceFile
BinarySourceFile(const std::string &path)
Default constructor.
Definition: apps/elftosb/common/SourceFile.h:133