Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
apps/elftosb/common/StELFFile.h
1 /*
2  * File: StELFFile.h
3  *
4  * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5  * See included license file for license details.
6  */
7 #if !defined(_StELFFile_h_)
8 #define _StELFFile_h_
9 
10 #include "stdafx.h"
11 #include <string>
12 #include <vector>
13 #include <map>
14 #include <iostream>
15 #include <stdexcept>
16 #include "ELF.h"
17 
19 typedef enum
20 {
21  eARMVariant = 1,
22  eGHSVariant,
23  eGCCVariant
24 } ELFVariant_t;
25 
27 typedef enum
28 {
29  eUnknownSymbol,
30  eARMSymbol,
31  eThumbSymbol,
32  eDataSymbol
33 } ARMSymbolType_t;
34 
42 class StELFFile
43 {
44 public:
45  typedef std::vector<Elf32_Shdr>::const_iterator const_section_iterator;
46  typedef std::vector<Elf32_Phdr>::const_iterator const_segment_iterator;
47 
48 public:
50  StELFFile(std::istream &inStream);
51 
53  virtual ~StELFFile();
54 
56 
57  virtual ELFVariant_t ELFVariant() { return m_elfVariant; }
60  virtual void setELFVariant(ELFVariant_t variant) { m_elfVariant = variant; }
62 
64 
65  virtual void setName(const std::string &inName) { m_name = inName; }
66  virtual std::string getName() const { return m_name; }
68 
70 
71  inline const Elf32_Ehdr &getFileHeader() const { return m_header; }
74 
77 
78  inline unsigned getSectionCount() const { return static_cast<unsigned>(m_sectionHeaders.size()); }
81  const Elf32_Shdr &getSectionAtIndex(unsigned inIndex) const;
82 
83  inline const_section_iterator getSectionBegin() const { return m_sectionHeaders.begin(); }
84  inline const_section_iterator getSectionEnd() const { return m_sectionHeaders.end(); }
86  unsigned getIndexOfSectionWithName(const std::string &inName);
87 
89  uint8_t *getSectionDataAtIndex(unsigned inIndex);
90 
92  uint8_t *getSectionData(const_section_iterator inSection);
94 
97 
98  inline unsigned getSegmentCount() const { return static_cast<unsigned>(m_programHeaders.size()); }
101  const Elf32_Phdr &getSegmentAtIndex(unsigned inIndex) const;
102 
103  inline const_segment_iterator getSegmentBegin() const { return m_programHeaders.begin(); }
104  inline const_segment_iterator getSegmentEnd() const { return m_programHeaders.end(); }
106  uint8_t *getSegmentDataAtIndex(unsigned inIndex);
107 
109  uint8_t *getSegmentData(const_segment_iterator inSegment);
111 
114 
115  std::string getSectionNameAtIndex(unsigned inIndex);
117 
119  std::string getStringAtIndex(unsigned inStringTableSectionIndex, unsigned inStringIndex);
121 
125 
126  unsigned getSymbolCount();
128 
130  const Elf32_Sym &getSymbolAtIndex(unsigned inIndex);
131 
133  unsigned getSymbolNameStringTableIndex() const;
134 
136  std::string getSymbolName(const Elf32_Sym &inSymbol);
137 
138  unsigned getIndexOfSymbolAtAddress(uint32_t symbolAddress, bool strict = true);
139 
140  ARMSymbolType_t getTypeOfSymbolAtIndex(unsigned symbolIndex);
142 
144 
145  void dumpSections();
146  void dumpSymbolTable();
148 
149 protected:
150  std::istream &m_stream;
151  ELFVariant_t m_elfVariant;
152  std::string m_name;
154  std::vector<Elf32_Shdr> m_sectionHeaders;
155  std::vector<Elf32_Phdr> m_programHeaders;
157 
162  {
163  uint8_t *m_data;
164  unsigned m_size;
165  bool m_swapped;
166  };
167  typedef std::map<unsigned, SectionDataInfo> SectionDataMap;
168  SectionDataMap m_sectionDataCache;
169 
171  SectionDataInfo &getCachedSectionData(unsigned inSectionIndex);
172 
174  void readFileHeaders();
175 
176  uint8_t *readSectionData(const Elf32_Shdr &inHeader);
177  uint8_t *readSegmentData(const Elf32_Phdr &inHeader);
178 
180  void byteSwapSymbolTable(const Elf32_Shdr &header, SectionDataInfo &info);
181 };
182 
186 class StELFFileException : public std::runtime_error
187 {
188 public:
190  StELFFileException(const std::string &inMessage)
191  : std::runtime_error(inMessage)
192  {
193  }
194 };
195 
196 #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: apps/elftosb/common/StELFFile.h:60
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: apps/elftosb/common/StELFFile.h:190
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