Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
apps/elftosb/common/SourceFile.h
1 /*
2  * File: SourceFile.h
3  *
4  * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5  * See included license file for license details.
6  */
7 #if !defined(_SourceFile_h_)
8 #define _SourceFile_h_
9 
10 #include <string>
11 #include <iostream>
12 #include <fstream>
13 #include "smart_ptr.h"
14 #include "DataSource.h"
15 #include "DataTarget.h"
16 #include "StringMatcher.h"
17 #include "OptionContext.h"
18 
19 namespace elftosb
20 {
34 {
35 public:
36  // \brief Factory function that creates the correct subclass of SourceFile.
37  static SourceFile *openFile(const std::string &path);
38 
39 public:
41  SourceFile(const std::string &path);
42 
44  virtual ~SourceFile();
45 
50  inline void setOptions(OptionContext *context) { m_options = context; }
52  inline const OptionContext *getOptions() const { return m_options; }
54  inline const std::string &getPath() const { return m_path; }
56  unsigned getSize();
57 
59 
60  virtual void open();
62 
64  virtual void close();
65 
67  virtual bool isOpen() const { return (bool)m_stream && const_cast<std::ifstream *>(m_stream.get())->is_open(); }
69 
71 
72  virtual bool supportsNamedSections() const = 0;
73  virtual bool supportsNamedSymbols() const = 0;
75 
77 
78  virtual DataSource *createDataSource() = 0;
80 
86  virtual DataSource *createDataSource(StringMatcher &matcher) { return NULL; }
88  virtual DataSource *createDataSource(const std::string &section);
90 
92 
93  virtual bool hasEntryPoint() = 0;
95 
97  virtual uint32_t getEntryPointAddress() { return 0; }
99 
101 
102  virtual DataTarget *createDataTargetForSection(const std::string &section) { return NULL; }
103  virtual DataTarget *createDataTargetForSymbol(const std::string &symbol) { return NULL; }
104  virtual DataTarget *createDataTargetForEntryPoint();
106 
108 
109  virtual bool hasSymbol(const std::string &name) { return false; }
112  virtual uint32_t getSymbolValue(const std::string &name) { return 0; }
114  virtual unsigned getSymbolSize(const std::string &name) { return 0; }
116 
117 protected:
118  std::string m_path;
121 
123  inline std::ifstream *getStream() { return m_stream; }
124 };
125 
130 {
131 public:
133  BinarySourceFile(const std::string &path)
134  : SourceFile(path)
135  {
136  }
137 
139 
140  virtual bool supportsNamedSections() const { return false; }
141  virtual bool supportsNamedSymbols() const { return false; }
143 
145  virtual DataSource *createDataSource();
146 
147  virtual bool hasEntryPoint() { return false; }
148 };
149 
150 }; // namespace elftosb
151 
152 #endif // _SourceFile_h_
virtual void close()
Closes the file.
Definition: apps/elftosb/common/SourceFile.cpp:100
Abstract base class for data sources.
Definition: apps/elftosb/common/DataSource.h:33
ptr_type get()
Return the current pointer value.
Definition: apps/elftosb/common/smart_ptr.h:43
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
const OptionContext * getOptions() const
Return the option context.
Definition: apps/elftosb/common/SourceFile.h:52
Simple, standard smart pointer class.
Definition: apps/elftosb/common/smart_ptr.h:18
virtual unsigned getSymbolSize(const std::string &name)
Returns the size of a symbol.
Definition: apps/elftosb/common/SourceFile.h:114
Definition: BootImage.h:13
Abstract base class for a source file containing executable code.
Definition: apps/elftosb/common/SourceFile.h:33
Abstract base class for the target address or range of data.
Definition: apps/elftosb/common/DataTarget.h:34
virtual DataSource * createDataSource()=0
Creates a data source from the entire file.
virtual ~SourceFile()
Destructor.
Definition: apps/elftosb/common/SourceFile.cpp:81
virtual bool isOpen() const
Returns whether the file is already open.
Definition: apps/elftosb/common/SourceFile.h:67
virtual bool hasEntryPoint()=0
Returns true if an entry point was set in the file.
const std::string & getPath() const
Returns the path to the file.
Definition: apps/elftosb/common/SourceFile.h:54
Pure abstract interface class to a table of options.
Definition: apps/elftosb/common/OptionContext.h:18
virtual bool hasSymbol(const std::string &name)
Returns whether a symbol exists in the source file.
Definition: apps/elftosb/common/SourceFile.h:110
std::string m_path
Path to the file.
Definition: apps/elftosb/common/SourceFile.h:118
virtual uint32_t getSymbolValue(const std::string &name)
Returns the value of a symbol.
Definition: apps/elftosb/common/SourceFile.h:112
virtual void open()
Opens the file.
Definition: apps/elftosb/common/SourceFile.cpp:90
virtual uint32_t getEntryPointAddress()
Returns the entry point address.
Definition: apps/elftosb/common/SourceFile.h:97
void setOptions(OptionContext *context)
Set the option context.
Definition: apps/elftosb/common/SourceFile.h:50
virtual bool hasEntryPoint()
Returns true if an entry point was set in the file.
Definition: apps/elftosb/common/SourceFile.h:147
Binary data file.
Definition: apps/elftosb/common/SourceFile.h:129
smart_ptr< OptionContext > m_options
Table of option values.
Definition: apps/elftosb/common/SourceFile.h:120
static SourceFile * openFile(const std::string &path)
Definition: apps/elftosb/common/SourceFile.cpp:32
Abstract interface class used to select strings by name.
Definition: apps/elftosb/common/StringMatcher.h:17
smart_ptr< std::ifstream > m_stream
File stream, or NULL if file is closed.
Definition: apps/elftosb/common/SourceFile.h:119
std::ifstream * getStream()
Internal access to the input stream object.
Definition: apps/elftosb/common/SourceFile.h:123
unsigned getSize()
Get the size in bytes of the file.
Definition: apps/elftosb/common/SourceFile.cpp:108
SourceFile(const std::string &path)
Default constructor.
Definition: apps/elftosb/common/SourceFile.cpp:73
BinarySourceFile(const std::string &path)
Default constructor.
Definition: apps/elftosb/common/SourceFile.h:133