4 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5 * See included license file for license details.
7 #if !defined(_SourceFile_h_)
13 #include "smart_ptr.h"
14 #include "DataSource.h"
15 #include "DataTarget.h"
16 #include "StringMatcher.h"
17 #include "OptionContext.h"
23 * \brief Abstract base class for a source file containing executable code.
25 * The purpose of this class cluster is to provide a common interface for
26 * accessing the contents of different file formats. This is accomplished
27 * through several small sets of methods along with the DataSource and
30 * The primary interface for creating instances of SourceFile is the static
31 * SourceFile::openFile() function. It will create the correct subclass of
32 * SourceFile by inspecting the file to determine its type.
37 // \brief Factory function that creates the correct subclass of SourceFile.
38 static SourceFile * openFile(const std::string & path);
41 //! \brief Default constructor.
42 SourceFile(const std::string & path);
44 //! \brief Destructor.
45 virtual ~SourceFile();
47 //! \brief Set the option context.
49 //! The source file will take ownership of the @a context and delete it
50 //! when the source file is itself deleted.
51 inline void setOptions(OptionContext * context) { m_options = context; }
53 //! \brief Return the option context.
54 inline const OptionContext * getOptions() const { return m_options; }
56 //! \brief Returns the path to the file.
57 inline const std::string & getPath() const { return m_path; }
59 //! \brief Get the size in bytes of the file.
62 //! \name Opening and closing
64 //! \brief Opens the file.
67 //! \brief Closes the file.
70 //! \brief Returns whether the file is already open.
71 virtual bool isOpen() const { return (bool)m_stream && const_cast<std::ifstream*>(m_stream.get())->is_open(); }
74 //! \name Format capabilities
76 virtual bool supportsNamedSections() const=0;
77 virtual bool supportsNamedSymbols() const=0;
80 //! \name Data source creation
82 //! \brief Creates a data source from the entire file.
83 virtual DataSource * createDataSource()=0;
85 //! \brief Creates a data source out of one or more sections of the file.
87 //! The \a selector object is used to perform the section name comparison.
88 //! If the file does not support named sections, or if there is not a
89 //! section with the given name, this method may return NULL.
90 virtual DataSource * createDataSource(StringMatcher & matcher) { return NULL; }
92 //! \brief Creates a data source out of one section of the file.
93 virtual DataSource * createDataSource(const std::string & section);
98 //! \brief Returns true if an entry point was set in the file.
99 virtual bool hasEntryPoint()=0;
101 //! \brief Returns the entry point address.
102 virtual uint32_t getEntryPointAddress() { return 0; }
105 //! \name Data target creation
107 virtual DataTarget * createDataTargetForSection(const std::string & section) { return NULL; }
108 virtual DataTarget * createDataTargetForSymbol(const std::string & symbol) { return NULL; }
109 virtual DataTarget * createDataTargetForEntryPoint();
114 //! \brief Returns whether a symbol exists in the source file.
115 virtual bool hasSymbol(const std::string & name) { return false; }
117 //! \brief Returns the value of a symbol.
118 virtual uint32_t getSymbolValue(const std::string & name) { return 0; }
120 //! \brief Returns the size of a symbol.
121 virtual unsigned getSymbolSize(const std::string & name) { return 0; }
125 std::string m_path; //!< Path to the file.
126 smart_ptr<std::ifstream> m_stream; //!< File stream, or NULL if file is closed.
127 smart_ptr<OptionContext> m_options; //!< Table of option values.
129 //! \brief Internal access to the input stream object.
130 inline std::ifstream * getStream() { return m_stream; }
134 * \brief Binary data file.
136 class BinarySourceFile : public SourceFile
139 //! \brief Default constructor.
140 BinarySourceFile(const std::string & path) : SourceFile(path) {}
142 //! \name Format capabilities
144 virtual bool supportsNamedSections() const { return false; }
145 virtual bool supportsNamedSymbols() const { return false; }
148 //! \brief Creates an unmapped data source from the entire file.
149 virtual DataSource * createDataSource();
151 virtual bool hasEntryPoint() { return false; }
154 }; // namespace elftosb
156 #endif // _SourceFile_h_