Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
src/blfwk/SourceFile.h
1 /*
2  * Copyright (c) 2013-14, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #if !defined(_SourceFile_h_)
31 #define _SourceFile_h_
32 
33 #include <string>
34 #include <iostream>
35 #include <fstream>
36 #include "smart_ptr.h"
37 #include "DataSource.h"
38 #include "DataTarget.h"
39 #include "StringMatcher.h"
40 #include "OptionContext.h"
41 
42 namespace blfwk
43 {
57 {
58 public:
59  // \brief Factory function that creates the correct subclass of SourceFile.
60  static SourceFile *openFile(const std::string &path);
61 
64  {
70  };
71 
72 public:
74  SourceFile(const std::string &path, source_file_t filetype);
75 
77  virtual ~SourceFile();
78 
83  inline void setOptions(OptionContext *context) { m_options = context; }
85  inline const OptionContext *getOptions() const { return m_options; }
87  inline source_file_t getFileType() const { return m_filetype; }
89  inline const std::string &getPath() const { return m_path; }
91  unsigned getSize() const { return m_size; }
93 
94  virtual void open();
96 
98  virtual void close();
99 
101  virtual bool isOpen() const { return (bool)m_stream && const_cast<std::ifstream *>(m_stream.get())->is_open(); }
103 
105 
106  virtual bool supportsNamedSections() const = 0;
107  virtual bool supportsNamedSymbols() const = 0;
109 
111 
112  virtual DataSource *createDataSource() = 0;
114 
120  virtual DataSource *createDataSource(StringMatcher &matcher) { return NULL; }
122  virtual DataSource *createDataSource(const std::string &section);
124 
126 
127  virtual bool hasEntryPoint() = 0;
129 
131  virtual uint32_t getEntryPointAddress() { return 0; }
133 
135 
136  virtual DataTarget *createDataTargetForSection(const std::string &section) { return NULL; }
137  virtual DataTarget *createDataTargetForSymbol(const std::string &symbol) { return NULL; }
138  virtual DataTarget *createDataTargetForEntryPoint();
140 
142 
143  virtual bool hasSymbol(const std::string &name) { return false; }
146  virtual uint32_t getSymbolValue(const std::string &name) { return 0; }
148  virtual unsigned getSymbolSize(const std::string &name) { return 0; }
150 
151 protected:
152  std::string m_path;
156  unsigned m_size;
157 
159  inline std::ifstream *getStream() { return m_stream; }
160 };
161 
166 {
167 public:
169  BinarySourceFile(const std::string &path, source_file_t sourceFileType = kBinarySourceFile);
170 
172 
173  virtual bool supportsNamedSections() const { return false; }
174  virtual bool supportsNamedSymbols() const { return false; }
176 
178 
179  virtual DataSource *createDataSource();
182 
184 
185  void guessEntryPointAndStackPointer();
188 
190  virtual bool hasEntryPoint() { return m_entry_point != 0xffffffff; }
192  virtual uint32_t getEntryPointAddress() { return m_entry_point; }
194  uint32_t getStackPointer() { return m_stack_pointer; }
196 
197 protected:
198  uint32_t m_entry_point;
199  uint32_t m_stack_pointer;
200 };
201 
202 }; // namespace blfwk
203 
204 #endif // _SourceFile_h_
smart_ptr< OptionContext > m_options
Table of option values.
Definition: src/blfwk/SourceFile.h:154
Definition: src/blfwk/SourceFile.h:68
ptr_type get()
Return the current pointer value.
Definition: apps/elftosb/common/smart_ptr.h:43
Definition: BlfwkErrors.h:16
Definition: src/blfwk/SourceFile.h:67
source_file_t
Set of supported executable image file formats.
Definition: src/blfwk/SourceFile.h:63
virtual uint32_t getSymbolValue(const std::string &name)
Returns the value of a symbol.
Definition: src/blfwk/SourceFile.h:146
virtual bool hasEntryPoint()
Returns true if an entry point was set in the file.
Definition: src/blfwk/SourceFile.h:190
virtual uint32_t getEntryPointAddress()
Returns the entry point address.
Definition: src/blfwk/SourceFile.h:192
Binary data file.
Definition: src/blfwk/SourceFile.h:165
virtual void close()
Closes the file.
Definition: src/blfwk/src/SourceFile.cpp:147
smart_ptr< std::ifstream > m_stream
File stream, or NULL if file is closed.
Definition: src/blfwk/SourceFile.h:153
Abstract base class for the target address or range of data.
Definition: src/blfwk/DataTarget.h:57
Pure abstract interface class to a table of options.
Definition: src/blfwk/OptionContext.h:41
Simple, standard smart pointer class.
Definition: apps/elftosb/common/smart_ptr.h:18
std::string m_path
Path to the file.
Definition: src/blfwk/SourceFile.h:152
static SourceFile * openFile(const std::string &path)
Definition: src/blfwk/src/SourceFile.cpp:62
unsigned m_size
The size in bytes of the file.
Definition: src/blfwk/SourceFile.h:156
unsigned getSize() const
Get the size in bytes of the file.
Definition: src/blfwk/SourceFile.h:91
virtual DataSource * createDataSource()=0
Creates a data source from the entire file.
Abstract base class for a source file containing executable code.
Definition: src/blfwk/SourceFile.h:56
SourceFile(const std::string &path, source_file_t filetype)
Default constructor.
Definition: src/blfwk/src/SourceFile.cpp:113
virtual bool hasEntryPoint()=0
Returns true if an entry point was set in the file.
virtual uint32_t getEntryPointAddress()
Returns the entry point address.
Definition: src/blfwk/SourceFile.h:131
const std::string & getPath() const
Returns the path to the file.
Definition: src/blfwk/SourceFile.h:89
virtual bool hasSymbol(const std::string &name)
Returns whether a symbol exists in the source file.
Definition: src/blfwk/SourceFile.h:144
source_file_t m_filetype
Image file type.
Definition: src/blfwk/SourceFile.h:155
Abstract interface class used to select strings by name.
Definition: src/blfwk/StringMatcher.h:40
virtual void open()
Opens the file.
Definition: src/blfwk/src/SourceFile.cpp:137
virtual ~SourceFile()
Destructor.
Definition: src/blfwk/src/SourceFile.cpp:128
virtual DataSource * createDataSource(StringMatcher &matcher)
Creates a data source out of one or more sections of the file.
Definition: src/blfwk/SourceFile.h:120
source_file_t getFileType() const
Return the file type.
Definition: src/blfwk/SourceFile.h:87
Abstract base class for data sources.
Definition: src/blfwk/DataSource.h:56
const OptionContext * getOptions() const
Return the option context.
Definition: src/blfwk/SourceFile.h:85
std::ifstream * getStream()
Internal access to the input stream object.
Definition: src/blfwk/SourceFile.h:159
Definition: src/blfwk/SourceFile.h:66
uint32_t getStackPointer()
Returns the stack pointer.
Definition: src/blfwk/SourceFile.h:194
virtual bool isOpen() const
Returns whether the file is already open.
Definition: src/blfwk/SourceFile.h:101
Definition: src/blfwk/SourceFile.h:69
virtual unsigned getSymbolSize(const std::string &name)
Returns the size of a symbol.
Definition: src/blfwk/SourceFile.h:148
Definition: src/blfwk/SourceFile.h:65
void setOptions(OptionContext *context)
Set the option context.
Definition: src/blfwk/SourceFile.h:83