Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
src/blfwk/ELFSourceFile.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(_ELFSourceFile_h_)
31 #define _ELFSourceFile_h_
32 
33 #include "SourceFile.h"
34 #include "StELFFile.h"
35 #include "smart_ptr.h"
36 #include "DataSource.h"
37 #include "DataTarget.h"
38 #include "ELF.h"
39 
40 namespace blfwk
41 {
43 enum elf_toolset_t
44 {
45  kUnknownToolset,
46  kGHSToolset,
47  kGCCToolset,
48  kADSToolset
49 };
50 
52 enum secinfo_clear_t
53 {
54  // Default value for the .secinfo action.
55  kSecinfoDefault,
56 
59  kSecinfoIgnore,
60 
62  kSecinfoROMClear,
63 
66  kSecinfoCStartupClear
67 };
68 
72 class ELFSourceFile : public SourceFile
73 {
74 public:
76  ELFSourceFile(const std::string &path);
77 
79  virtual ~ELFSourceFile();
80 
82  static bool isELFFile(std::istream &stream);
83 
85 
86  virtual void open();
88 
90  virtual void close();
92 
94 
95  virtual bool supportsNamedSections() const { return true; }
96  virtual bool supportsNamedSymbols() const { return true; }
98 
100 
101  virtual DataSource *createDataSource();
103 
105  virtual DataSource *createDataSource(StringMatcher &matcher);
107 
109  // baseAddress
110  virtual DataSource *createDataSource(const std::vector<uint32_t> &baseAddress, bool match);
112 
114 
115  virtual bool hasEntryPoint();
117 
119  virtual uint32_t getEntryPointAddress();
121 
123 
124  virtual DataTarget *createDataTargetForSection(const std::string &section);
125  virtual DataTarget *createDataTargetForSymbol(const std::string &symbol);
127 
129 
130  virtual bool hasSymbol(const std::string &name);
132 
134  virtual uint32_t getSymbolValue(const std::string &name);
135 
137  virtual unsigned getSymbolSize(const std::string &name);
139 
141 
142  StELFFile *getELFFile() { return m_file; }
145  bool lookupSymbol(const std::string &name, Elf32_Sym &info);
147 
148 protected:
150  elf_toolset_t m_toolset;
151  secinfo_clear_t m_secinfoOption;
152 
153 protected:
155  elf_toolset_t readToolsetOption();
156 
158  secinfo_clear_t readSecinfoClearOption();
159 
160 protected:
176  class ELFDataSource : public DataSource
177  {
178  public:
183  {
184  public:
185  ProgBitsSegment(ELFDataSource &source, StELFFile *elf, unsigned index);
186 
187  virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer);
188  virtual unsigned getLength();
189 
190  virtual bool hasNaturalLocation() { return true; }
191  virtual uint32_t getBaseAddress();
192 
193  protected:
195  unsigned m_sectionIndex;
196  };
197 
205  {
206  public:
207  NoBitsSegment(ELFDataSource &source, StELFFile *elf, unsigned index);
208 
209  virtual unsigned getLength();
210 
211  virtual bool hasNaturalLocation() { return true; }
212  virtual uint32_t getBaseAddress();
213 
214  protected:
216  unsigned m_sectionIndex;
217  };
218 
219  public:
222  : DataSource()
223  , m_elf(elf)
224  {
225  }
226 
228  virtual ~ELFDataSource();
229 
231  inline void setSecinfoOption(secinfo_clear_t option) { m_secinfoOption = option; }
233  void addSection(unsigned sectionIndex);
234 
236  virtual unsigned getSegmentCount() { return (unsigned)m_segments.size(); }
238  virtual DataSource::Segment *getSegmentAt(unsigned index) { return m_segments[index]; }
239  protected:
241  secinfo_clear_t m_secinfoOption;
242 
243  typedef std::vector<DataSource::Segment *> segment_vector_t;
244  segment_vector_t m_segments;
245  };
246 };
247 
248 }; // namespace blfwk
249 
250 #endif // _ELFSourceFile_h_
virtual ~ELFDataSource()
Destructor.
Definition: src/blfwk/src/ELFSourceFile.cpp:493
Represents one named #SHT_NOBITS section within the ELF file.
Definition: src/blfwk/ELFSourceFile.h:204
StELFFile * getELFFile()
Returns the underlying StELFFile object.
Definition: src/blfwk/ELFSourceFile.h:143
StELFFile * m_elf
The ELF file parser.
Definition: src/blfwk/ELFSourceFile.h:240
A data source with ELF file sections as the contents.
Definition: src/blfwk/ELFSourceFile.h:176
virtual bool hasNaturalLocation()
Returns whether the segment has an associated address.
Definition: src/blfwk/ELFSourceFile.h:190
Definition: BlfwkErrors.h:16
elf_toolset_t m_toolset
Toolset that produced the ELF file.
Definition: src/blfwk/ELFSourceFile.h:150
virtual unsigned getSegmentCount()
Returns the number of segments in the source.
Definition: src/blfwk/ELFSourceFile.h:236
elf_toolset_t readToolsetOption()
Parses the toolset option value.
Definition: src/blfwk/src/ELFSourceFile.cpp:122
void addSection(unsigned sectionIndex)
Adds the ELF section at position sectionIndex to the data source.
Definition: src/blfwk/src/ELFSourceFile.cpp:512
unsigned m_sectionIndex
The index of the section this segment represents.
Definition: src/blfwk/ELFSourceFile.h:216
secinfo_clear_t m_secinfoOption
How to deal with the .secinfo section. Ignored if the toolset is not GHS.
Definition: src/blfwk/ELFSourceFile.h:151
void setSecinfoOption(secinfo_clear_t option)
Set the option to control .secinfo usage.
Definition: src/blfwk/ELFSourceFile.h:231
StELFFile * m_elf
The format parser instance for this ELF file.
Definition: src/blfwk/ELFSourceFile.h:215
ELFDataSource(StELFFile *elf)
Default constructor.
Definition: src/blfwk/ELFSourceFile.h:221
Abstract base class for the target address or range of data.
Definition: src/blfwk/DataTarget.h:57
ELF symbol table entry.
Definition: apps/elftosb/common/ELF.h:277
virtual unsigned getData(unsigned offset, unsigned maxBytes, uint8_t *buffer)
Gets all or a portion of the segment&#39;s data.
Definition: src/blfwk/src/ELFSourceFile.cpp:587
virtual void close()
Closes the file.
Definition: src/blfwk/src/ELFSourceFile.cpp:115
smart_ptr< StELFFile > m_file
Parser for the ELF file.
Definition: src/blfwk/ELFSourceFile.h:149
std::vector< DataSource::Segment * > segment_vector_t
A list of segment instances.
Definition: src/blfwk/ELFSourceFile.h:243
virtual ~ELFSourceFile()
Destructor.
Definition: src/blfwk/src/ELFSourceFile.cpp:63
Abstract base class for a source file containing executable code.
Definition: src/blfwk/SourceFile.h:56
virtual uint32_t getBaseAddress()
Returns the address associated with the segment.
Definition: src/blfwk/src/ELFSourceFile.cpp:609
virtual bool hasSymbol(const std::string &name)
Returns whether a symbol exists in the source file.
Definition: src/blfwk/src/ELFSourceFile.cpp:419
virtual DataSource::Segment * getSegmentAt(unsigned index)
Returns the segment at position index.
Definition: src/blfwk/ELFSourceFile.h:238
bool lookupSymbol(const std::string &name, Elf32_Sym &info)
Gets information about a symbol in the ELF file.
Definition: src/blfwk/src/ELFSourceFile.cpp:470
virtual unsigned getLength()
Gets the length of the segment&#39;s data.
Definition: src/blfwk/src/ELFSourceFile.cpp:603
segment_vector_t m_segments
The segments of this data source.
Definition: src/blfwk/ELFSourceFile.h:244
ELFSourceFile(const std::string &path)
Default constructor.
Definition: src/blfwk/src/ELFSourceFile.cpp:56
Abstract interface class used to select strings by name.
Definition: src/blfwk/StringMatcher.h:40
Discrete, contiguous part of the source&#39;s data.
Definition: src/blfwk/DataSource.h:65
Represents one named #SHT_PROGBITS section within the ELF file.
Definition: src/blfwk/ELFSourceFile.h:182
secinfo_clear_t readSecinfoClearOption()
Reads the secinfoClear option.
Definition: src/blfwk/src/ELFSourceFile.cpp:169
virtual unsigned getSymbolSize(const std::string &name)
Returns the size of a symbol.
Definition: src/blfwk/src/ELFSourceFile.cpp:454
virtual uint32_t getEntryPointAddress()
Returns the entry point address.
Definition: src/blfwk/src/ELFSourceFile.cpp:342
StELFFile * m_elf
The format parser instance for this ELF file.
Definition: src/blfwk/ELFSourceFile.h:194
Parser for Executable and Linking Format (ELF) files.
Definition: apps/elftosb/common/StELFFile.h:42
Executable and Loading Format (ELF) source file.
Definition: src/blfwk/ELFSourceFile.h:72
static bool isELFFile(std::istream &stream)
Identifies whether the stream contains an ELF file.
Definition: src/blfwk/src/ELFSourceFile.cpp:67
Abstract base class for data sources.
Definition: src/blfwk/DataSource.h:56
secinfo_clear_t m_secinfoOption
How to deal with the .secinfo section. Ignored if the toolset is not GHS.
Definition: src/blfwk/ELFSourceFile.h:241
virtual DataTarget * createDataTargetForSection(const std::string &section)
Definition: src/blfwk/src/ELFSourceFile.cpp:375
unsigned m_sectionIndex
The index of the section this segment represents.
Definition: src/blfwk/ELFSourceFile.h:195
virtual bool hasNaturalLocation()
Pattern segments have no natural address.
Definition: src/blfwk/ELFSourceFile.h:211
virtual bool hasEntryPoint()
Returns true if an entry point was set in the file.
Definition: src/blfwk/src/ELFSourceFile.cpp:335
virtual DataTarget * createDataTargetForSymbol(const std::string &symbol)
Definition: src/blfwk/src/ELFSourceFile.cpp:393
virtual void open()
Opens the file.
Definition: src/blfwk/src/ELFSourceFile.cpp:80
virtual uint32_t getSymbolValue(const std::string &name)
Returns the value of a symbol.
Definition: src/blfwk/src/ELFSourceFile.cpp:425
virtual DataSource * createDataSource()
Creates a data source from the entire file.
Definition: src/blfwk/src/ELFSourceFile.cpp:220
This is a special type of segment containing a repeating pattern.
Definition: src/blfwk/DataSource.h:110