Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
src/blfwk/StELFFile.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(_StELFFile_h_)
31 #define _StELFFile_h_
32 
33 #include "blfwk/stdafx.h"
34 #include <string>
35 #include <vector>
36 #include <map>
37 #include <iostream>
38 #include <stdexcept>
39 #include "blfwk/ELF.h"
40 
42 typedef enum
43 {
44  eARMVariant = 1,
45  eGHSVariant,
46  eGCCVariant
47 } ELFVariant_t;
48 
50 typedef enum
51 {
52  eUnknownSymbol,
53  eARMSymbol,
54  eThumbSymbol,
55  eDataSymbol
56 } ARMSymbolType_t;
57 
65 class StELFFile
66 {
67 public:
68  typedef std::vector<Elf32_Shdr>::const_iterator const_section_iterator;
69  typedef std::vector<Elf32_Phdr>::const_iterator const_segment_iterator;
70 
71 public:
73  StELFFile(std::istream &inStream);
74 
76  virtual ~StELFFile();
77 
79 
80  virtual ELFVariant_t ELFVariant() { return m_elfVariant; }
83  virtual void setELFVariant(ELFVariant_t variant) { m_elfVariant = variant; }
85 
87 
88  virtual void setName(const std::string &inName) { m_name = inName; }
89  virtual std::string getName() const { return m_name; }
91 
93 
94  inline const Elf32_Ehdr &getFileHeader() const { return m_header; }
97 
100 
101  inline unsigned getSectionCount() const { return static_cast<unsigned>(m_sectionHeaders.size()); }
104  const Elf32_Shdr &getSectionAtIndex(unsigned inIndex) const;
105 
106  inline const_section_iterator getSectionBegin() const { return m_sectionHeaders.begin(); }
107  inline const_section_iterator getSectionEnd() const { return m_sectionHeaders.end(); }
109  unsigned getIndexOfSectionWithName(const std::string &inName);
110 
112  uint8_t *getSectionDataAtIndex(unsigned inIndex);
113 
115  uint8_t *getSectionData(const_section_iterator inSection);
117 
120 
121  inline unsigned getSegmentCount() const { return static_cast<unsigned>(m_programHeaders.size()); }
124  const Elf32_Phdr &getSegmentAtIndex(unsigned inIndex) const;
125 
126  inline const_segment_iterator getSegmentBegin() const { return m_programHeaders.begin(); }
127  inline const_segment_iterator getSegmentEnd() const { return m_programHeaders.end(); }
129  uint8_t *getSegmentDataAtIndex(unsigned inIndex);
130 
132  uint8_t *getSegmentData(const_segment_iterator inSegment);
134 
137 
138  std::string getSectionNameAtIndex(unsigned inIndex);
140 
142  std::string getStringAtIndex(unsigned inStringTableSectionIndex, unsigned inStringIndex);
144 
148 
149  unsigned getSymbolCount();
151 
153  const Elf32_Sym &getSymbolAtIndex(unsigned inIndex);
154 
156  unsigned getSymbolNameStringTableIndex() const;
157 
159  std::string getSymbolName(const Elf32_Sym &inSymbol);
160 
161  unsigned getIndexOfSymbolAtAddress(uint32_t symbolAddress, bool strict = true);
162 
163  ARMSymbolType_t getTypeOfSymbolAtIndex(unsigned symbolIndex);
165 
167 
168  void dumpSections();
169  void dumpSymbolTable();
171 
172 protected:
173  std::istream &m_stream;
174  ELFVariant_t m_elfVariant;
175  std::string m_name;
177  std::vector<Elf32_Shdr> m_sectionHeaders;
178  std::vector<Elf32_Phdr> m_programHeaders;
179  unsigned m_symbolTableIndex;
180 
184  struct SectionDataInfo
185  {
186  uint8_t *m_data;
187  unsigned m_size;
188  bool m_swapped;
189  };
190  typedef std::map<unsigned, SectionDataInfo> SectionDataMap;
191  SectionDataMap m_sectionDataCache;
192 
194  SectionDataInfo &getCachedSectionData(unsigned inSectionIndex);
195 
197  void readFileHeaders();
198 
199  uint8_t *readSectionData(const Elf32_Shdr &inHeader);
200  uint8_t *readSegmentData(const Elf32_Phdr &inHeader);
201 
203  void byteSwapSymbolTable(const Elf32_Shdr &header, SectionDataInfo &info);
204 };
205 
209 class StELFFileException : public std::runtime_error
210 {
211 public:
213  StELFFileException(const std::string &inMessage)
214  : std::runtime_error(inMessage)
215  {
216  }
217 };
218 
219 #endif // _StELFFile_h_
Elf32_Ehdr m_header
The ELF file header.
Definition: apps/elftosb/common/StELFFile.h:153
ELF section header.
Definition: apps/elftosb/common/ELF.h:135
uint8_t * getSectionDataAtIndex(unsigned inIndex)
Returns the data for the section.
Definition: apps/elftosb/common/StELFFile.cpp:182
Definition: apps/elftosb/common/StELFFile.h:161
SectionDataMap m_sectionDataCache
Cached data of sections.
Definition: apps/elftosb/common/StELFFile.h:168
ELFVariant_t m_elfVariant
Variant of the ARM ELF format specification.
Definition: apps/elftosb/common/StELFFile.h:151
unsigned getSymbolCount()
Returns the number of symbols in the default ".symtab" symbol table.
Definition: apps/elftosb/common/StELFFile.cpp:348
unsigned getIndexOfSectionWithName(const std::string &inName)
Returns the index of the section with the name inName.
Definition: apps/elftosb/common/StELFFile.cpp:154
SectionDataInfo & getCachedSectionData(unsigned inSectionIndex)
Reads a section&#39;s data either from cache or from disk.
Definition: apps/elftosb/common/StELFFile.cpp:326
void byteSwapSymbolTable(const Elf32_Shdr &header, SectionDataInfo &info)
Byte swaps the symbol table data into host endianness.
Definition: apps/elftosb/common/StELFFile.cpp:381
void readFileHeaders()
Reads the file, section, and program headers into memory.
Definition: apps/elftosb/common/StELFFile.cpp:38
std::vector< Elf32_Shdr > m_sectionHeaders
All of the section headers.
Definition: apps/elftosb/common/StELFFile.h:154
uint8_t * getSectionData(const_section_iterator inSection)
Returns the data for the section.
Definition: apps/elftosb/common/StELFFile.cpp:196
const Elf32_Phdr & getSegmentAtIndex(unsigned inIndex) const
Returns a reference to the given segment.
Definition: apps/elftosb/common/StELFFile.cpp:230
uint8_t * readSectionData(const Elf32_Shdr &inHeader)
Definition: apps/elftosb/common/StELFFile.cpp:203
STL namespace.
virtual void setELFVariant(ELFVariant_t variant)
Set the ELF format variation to either #eARMVariant or #eGHSVariant.
Definition: src/blfwk/StELFFile.h:83
virtual ELFVariant_t ELFVariant()
Return the ELF format variant to which this file is set.
Definition: apps/elftosb/common/StELFFile.h:58
unsigned getSymbolNameStringTableIndex() const
Returns the section index of the string table containing symbol names.
Definition: apps/elftosb/common/StELFFile.cpp:400
std::istream & m_stream
The source stream for the ELF file.
Definition: apps/elftosb/common/StELFFile.h:150
ELF program header.
Definition: apps/elftosb/common/ELF.h:223
unsigned m_symbolTableIndex
Index of ".symtab" section, or #SHN_UNDEF if not present.
Definition: apps/elftosb/common/StELFFile.h:156
ELF symbol table entry.
Definition: apps/elftosb/common/ELF.h:277
StELFFile(std::istream &inStream)
Constructor.
Definition: apps/elftosb/common/StELFFile.cpp:16
uint8_t * getSegmentData(const_segment_iterator inSegment)
Returns the data of the specified segment.
Definition: apps/elftosb/common/StELFFile.cpp:261
virtual ~StELFFile()
Destructor.
Definition: apps/elftosb/common/StELFFile.cpp:23
uint8_t * readSegmentData(const Elf32_Phdr &inHeader)
Definition: apps/elftosb/common/StELFFile.cpp:268
const Elf32_Ehdr & getFileHeader() const
Returns the ELF file header.
Definition: apps/elftosb/common/StELFFile.h:72
const Elf32_Shdr & getSectionAtIndex(unsigned inIndex) const
Returns a reference to section number inIndex.
Definition: apps/elftosb/common/StELFFile.cpp:144
bool m_swapped
Has this section been byte swapped yet? Used for symbol table.
Definition: apps/elftosb/common/StELFFile.h:165
uint8_t * getSegmentDataAtIndex(unsigned inIndex)
Returns the data of the specified segment.
Definition: apps/elftosb/common/StELFFile.cpp:247
ELF file header.
Definition: apps/elftosb/common/ELF.h:61
std::string getSectionNameAtIndex(unsigned inIndex)
Returns a string from the file&#39;s section name string table.
Definition: apps/elftosb/common/StELFFile.cpp:299
unsigned getSectionCount() const
Returns the number of sections in the file.
Definition: apps/elftosb/common/StELFFile.h:79
Simple exception thrown to indicate an error in the input ELF file format.
Definition: apps/elftosb/common/StELFFile.h:186
std::string m_name
File name. (optional)
Definition: apps/elftosb/common/StELFFile.h:152
Parser for Executable and Linking Format (ELF) files.
Definition: apps/elftosb/common/StELFFile.h:42
std::string getStringAtIndex(unsigned inStringTableSectionIndex, unsigned inStringIndex)
Returns a string from any string table in the object file.
Definition: apps/elftosb/common/StELFFile.cpp:311
StELFFileException(const std::string &inMessage)
Default constructor.
Definition: src/blfwk/StELFFile.h:213
unsigned m_size
Section data size in bytes.
Definition: apps/elftosb/common/StELFFile.h:164
unsigned getIndexOfSymbolAtAddress(uint32_t symbolAddress, bool strict=true)
Returns STN_UNDEF if it cannot find a symbol at the given symbolAddress.
Definition: apps/elftosb/common/StELFFile.cpp:413
std::vector< Elf32_Phdr > m_programHeaders
All of the program headers.
Definition: apps/elftosb/common/StELFFile.h:155
unsigned getSegmentCount() const
Returns the number of segments, or program headers, in the file.
Definition: apps/elftosb/common/StELFFile.h:99
const Elf32_Sym & getSymbolAtIndex(unsigned inIndex)
Returns the symbol with index inIndex.
Definition: apps/elftosb/common/StELFFile.cpp:359
std::string getSymbolName(const Elf32_Sym &inSymbol)
Returns the name of the symbol described by inSymbol.
Definition: apps/elftosb/common/StELFFile.cpp:406
uint8_t * m_data
Pointer to section data.
Definition: apps/elftosb/common/StELFFile.h:163