Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
apps/elftosb/common/ELFSourceFile.h
1 /*
2  * File: ELFSourceFile.h
3  *
4  * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5  * See included license file for license details.
6  */
7 #if !defined(_ELFSourceFile_h_)
8 #define _ELFSourceFile_h_
9 
10 #include "SourceFile.h"
11 #include "StELFFile.h"
12 #include "smart_ptr.h"
13 #include "DataSource.h"
14 #include "DataTarget.h"
15 #include "ELF.h"
16 
17 namespace elftosb
18 {
20 enum elf_toolset_t
21 {
22  kUnknownToolset,
23  kGHSToolset,
24  kGCCToolset,
25  kADSToolset,
26  kIARToolset,
27  kKDSToolset,
28  kKeilToolset
29 };
30 
32 enum secinfo_clear_t
33 {
34  // Default value for the .secinfo action.
35  kSecinfoDefault,
36 
39  kSecinfoIgnore,
40 
42  kSecinfoROMClear,
43 
46  kSecinfoCStartupClear
47 };
48 
52 class ELFSourceFile : public SourceFile
53 {
54 public:
56  ELFSourceFile(const std::string &path);
57 
59  virtual ~ELFSourceFile();
60 
62  static bool isELFFile(std::istream &stream);
63 
65 
66  virtual void open();
68 
70  virtual void close();
72 
74 
75  virtual bool supportsNamedSections() const { return true; }
76  virtual bool supportsNamedSymbols() const { return true; }
78 
80 
81  virtual DataSource *createDataSource();
83 
85  virtual DataSource *createDataSource(StringMatcher &matcher);
87 
89 
90  virtual bool hasEntryPoint();
92 
94  virtual uint32_t getEntryPointAddress();
96 
98 
99  virtual DataTarget *createDataTargetForSection(const std::string &section);
100  virtual DataTarget *createDataTargetForSymbol(const std::string &symbol);
102 
104 
105  virtual bool hasSymbol(const std::string &name);
107 
109  virtual uint32_t getSymbolValue(const std::string &name);
110 
112  virtual unsigned getSymbolSize(const std::string &name);
114 
116 
117  StELFFile *getELFFile() { return m_file; }
120  bool lookupSymbol(const std::string &name, Elf32_Sym &info);
122 
123 protected:
125  elf_toolset_t m_toolset;
126  secinfo_clear_t m_secinfoOption;
127 
128 protected:
130  elf_toolset_t readToolsetOption();
131 
133  secinfo_clear_t readSecinfoClearOption();
134 
135 protected:
151  class ELFDataSource : public DataSource
152  {
153  public:
158  {
159  public:
160  ProgBitsSegment(ELFDataSource &source, StELFFile *elf, unsigned index);
161 
162  virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer);
163  virtual unsigned getLength();
164 
165  virtual bool hasNaturalLocation() { return true; }
166  virtual uint32_t getBaseAddress();
167 
168  protected:
170  unsigned m_sectionIndex;
171  };
172 
180  {
181  public:
182  NoBitsSegment(ELFDataSource &source, StELFFile *elf, unsigned index);
183 
184  virtual unsigned getLength();
185 
186  virtual bool hasNaturalLocation() { return true; }
187  virtual uint32_t getBaseAddress();
188 
189  protected:
191  unsigned m_sectionIndex;
192  };
193 
194  public:
197  : DataSource()
198  , m_elf(elf)
199  {
200  }
201 
203  virtual ~ELFDataSource();
204 
206  inline void setSecinfoOption(secinfo_clear_t option) { m_secinfoOption = option; }
208  void addSection(unsigned sectionIndex);
209 
211  virtual unsigned getSegmentCount() { return (unsigned)m_segments.size(); }
213  virtual DataSource::Segment *getSegmentAt(unsigned index) { return m_segments[index]; }
214  protected:
216  secinfo_clear_t m_secinfoOption;
217 
218  typedef std::vector<DataSource::Segment *> segment_vector_t;
219  segment_vector_t m_segments;
220  };
221 };
222 
223 }; // namespace elftosb
224 
225 #endif // _ELFSourceFile_h_
smart_ptr< StELFFile > m_file
Parser for the ELF file.
Definition: apps/elftosb/common/ELFSourceFile.h:124
Represents one named #SHT_NOBITS section within the ELF file.
Definition: apps/elftosb/common/ELFSourceFile.h:179
unsigned m_sectionIndex
The index of the section this segment represents.
Definition: apps/elftosb/common/ELFSourceFile.h:170
StELFFile * getELFFile()
Returns the underlying StELFFile object.
Definition: apps/elftosb/common/ELFSourceFile.h:118
virtual unsigned getLength()
Gets the length of the segment&#39;s data.
Definition: apps/elftosb/common/ELFSourceFile.cpp:517
Abstract base class for data sources.
Definition: apps/elftosb/common/DataSource.h:33
elf_toolset_t readToolsetOption()
Parses the toolset option value.
Definition: apps/elftosb/common/ELFSourceFile.cpp:103
virtual DataSource * createDataSource()
Creates a data source from the entire file.
Definition: apps/elftosb/common/ELFSourceFile.cpp:202
virtual bool hasSymbol(const std::string &name)
Returns whether a symbol exists in the source file.
Definition: apps/elftosb/common/ELFSourceFile.cpp:335
virtual DataTarget * createDataTargetForSection(const std::string &section)
Definition: apps/elftosb/common/ELFSourceFile.cpp:291
ELFSourceFile(const std::string &path)
Default constructor.
Definition: apps/elftosb/common/ELFSourceFile.cpp:34
segment_vector_t m_segments
The segments of this data source.
Definition: apps/elftosb/common/ELFSourceFile.h:219
std::vector< DataSource::Segment * > segment_vector_t
A list of segment instances.
Definition: apps/elftosb/common/ELFSourceFile.h:218
ELFDataSource(StELFFile *elf)
Default constructor.
Definition: apps/elftosb/common/ELFSourceFile.h:196
StELFFile * m_elf
The ELF file parser.
Definition: apps/elftosb/common/ELFSourceFile.h:215
virtual DataTarget * createDataTargetForSymbol(const std::string &symbol)
Definition: apps/elftosb/common/ELFSourceFile.cpp:309
secinfo_clear_t m_secinfoOption
How to deal with the .secinfo section. Ignored if the toolset is not GHS.
Definition: apps/elftosb/common/ELFSourceFile.h:126
virtual bool hasEntryPoint()
Returns true if an entry point was set in the file.
Definition: apps/elftosb/common/ELFSourceFile.cpp:251
virtual uint32_t getBaseAddress()
Returns the address associated with the segment.
Definition: apps/elftosb/common/ELFSourceFile.cpp:523
secinfo_clear_t readSecinfoClearOption()
Reads the secinfoClear option.
Definition: apps/elftosb/common/ELFSourceFile.cpp:151
virtual unsigned getSymbolSize(const std::string &name)
Returns the size of a symbol.
Definition: apps/elftosb/common/ELFSourceFile.cpp:370
ELF symbol table entry.
Definition: apps/elftosb/common/ELF.h:277
virtual uint32_t getEntryPointAddress()
Returns the entry point address.
Definition: apps/elftosb/common/ELFSourceFile.cpp:258
bool lookupSymbol(const std::string &name, Elf32_Sym &info)
Gets information about a symbol in the ELF file.
Definition: apps/elftosb/common/ELFSourceFile.cpp:386
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
This is a special type of segment containing a repeating pattern.
Definition: apps/elftosb/common/DataSource.h:87
Represents one named #SHT_PROGBITS section within the ELF file.
Definition: apps/elftosb/common/ELFSourceFile.h:157
virtual ~ELFDataSource()
Destructor.
Definition: apps/elftosb/common/ELFSourceFile.cpp:409
virtual unsigned getSegmentCount()
Returns the number of segments in the source.
Definition: apps/elftosb/common/ELFSourceFile.h:211
A data source with ELF file sections as the contents.
Definition: apps/elftosb/common/ELFSourceFile.h:151
void addSection(unsigned sectionIndex)
Adds the ELF section at position sectionIndex to the data source.
Definition: apps/elftosb/common/ELFSourceFile.cpp:428
static bool isELFFile(std::istream &stream)
Identifies whether the stream contains an ELF file.
Definition: apps/elftosb/common/ELFSourceFile.cpp:45
elf_toolset_t m_toolset
Toolset that produced the ELF file.
Definition: apps/elftosb/common/ELFSourceFile.h:125
Discrete, contiguous part of the source&#39;s data.
Definition: apps/elftosb/common/DataSource.h:42
unsigned m_sectionIndex
The index of the section this segment represents.
Definition: apps/elftosb/common/ELFSourceFile.h:191
virtual ~ELFSourceFile()
Destructor.
Definition: apps/elftosb/common/ELFSourceFile.cpp:41
StELFFile * m_elf
The format parser instance for this ELF file.
Definition: apps/elftosb/common/ELFSourceFile.h:190
virtual bool hasNaturalLocation()
Returns whether the segment has an associated address.
Definition: apps/elftosb/common/ELFSourceFile.h:165
Parser for Executable and Linking Format (ELF) files.
Definition: apps/elftosb/common/StELFFile.h:42
Executable and Loading Format (ELF) source file.
Definition: apps/elftosb/common/ELFSourceFile.h:52
virtual uint32_t getSymbolValue(const std::string &name)
Returns the value of a symbol.
Definition: apps/elftosb/common/ELFSourceFile.cpp:341
virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer)
Gets all or a portion of the segment&#39;s data.
Definition: apps/elftosb/common/ELFSourceFile.cpp:501
StELFFile * m_elf
The format parser instance for this ELF file.
Definition: apps/elftosb/common/ELFSourceFile.h:169
Abstract interface class used to select strings by name.
Definition: apps/elftosb/common/StringMatcher.h:17
void setSecinfoOption(secinfo_clear_t option)
Set the option to control .secinfo usage.
Definition: apps/elftosb/common/ELFSourceFile.h:206
virtual DataSource::Segment * getSegmentAt(unsigned index)
Returns the segment at position index.
Definition: apps/elftosb/common/ELFSourceFile.h:213
secinfo_clear_t m_secinfoOption
How to deal with the .secinfo section. Ignored if the toolset is not GHS.
Definition: apps/elftosb/common/ELFSourceFile.h:216
virtual bool hasNaturalLocation()
Pattern segments have no natural address.
Definition: apps/elftosb/common/ELFSourceFile.h:186
virtual void close()
Closes the file.
Definition: apps/elftosb/common/ELFSourceFile.cpp:96
virtual void open()
Opens the file.
Definition: apps/elftosb/common/ELFSourceFile.cpp:58