4 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5 * See included license file for license details.
7 #if !defined(_StELFFile_h_)
18 //! Variations of the ARM ELF format.
20 eARMVariant = 1, //!< Standard ARM ELF specification.
21 eGHSVariant, //!< Green Hills Software variant.
22 eGCCVariant //!< GNU Compiler Collection variant.
25 //! Possible ARM ELF symbol types.
34 * \brief Parser for Executable and Linking Format (ELF) files.
36 * The stream passed into the constructor needs to stay open for the life
37 * of the object. This is because calls to getSectionDataAtIndex() and
38 * getSegmentDataAtIndex() read the data directly from the input stream.
43 typedef std::vector<Elf32_Shdr>::const_iterator const_section_iterator;
44 typedef std::vector<Elf32_Phdr>::const_iterator const_segment_iterator;
47 //! \brief Constructor.
48 StELFFile(std::istream & inStream);
50 //! \brief Destructor.
53 //! \name File format variant
55 //! \brief Return the ELF format variant to which this file is set.
56 virtual ELFVariant_t ELFVariant() { return m_elfVariant; }
58 //! \brief Set the ELF format variation to either #eARMVariant or #eGHSVariant.
59 virtual void setELFVariant(ELFVariant_t variant) { m_elfVariant = variant; }
64 virtual void setName(const std::string & inName) { m_name = inName; }
65 virtual std::string getName() const { return m_name; }
70 //! \brief Returns the ELF file header.
71 inline const Elf32_Ehdr & getFileHeader() const { return m_header; }
75 //! Methods pertaining to the object file's sections.
77 //! \brief Returns the number of sections in the file.
78 inline unsigned getSectionCount() const { return static_cast<unsigned>(m_sectionHeaders.size()); }
80 //! \brief Returns a reference to section number \a inIndex.
81 const Elf32_Shdr & getSectionAtIndex(unsigned inIndex) const;
83 inline const_section_iterator getSectionBegin() const { return m_sectionHeaders.begin(); }
84 inline const_section_iterator getSectionEnd() const { return m_sectionHeaders.end(); }
86 //! \brief Returns the index of the section with the name \a inName.
87 unsigned getIndexOfSectionWithName(const std::string & inName);
89 //! \brief Returns the data for the section.
90 uint8_t * getSectionDataAtIndex(unsigned inIndex);
92 //! \brief Returns the data for the section.
93 uint8_t * getSectionData(const_section_iterator inSection);
97 //! Methods for accessing the file's program headers for segments.
99 //! \brief Returns the number of segments, or program headers, in the file.
100 inline unsigned getSegmentCount() const { return static_cast<unsigned>(m_programHeaders.size()); }
102 //! \brief Returns a reference to the given segment.
103 const Elf32_Phdr & getSegmentAtIndex(unsigned inIndex) const;
105 inline const_segment_iterator getSegmentBegin() const { return m_programHeaders.begin(); }
106 inline const_segment_iterator getSegmentEnd() const { return m_programHeaders.end(); }
108 //! \brief Returns the data of the specified segment.
109 uint8_t * getSegmentDataAtIndex(unsigned inIndex);
111 //! \brief Returns the data of the specified segment.
112 uint8_t * getSegmentData(const_segment_iterator inSegment);
115 //! \name String table
116 //! Methods for accessing the string tables.
118 //! \brief Returns a string from the file's section name string table.
119 std::string getSectionNameAtIndex(unsigned inIndex);
121 //! \brief Returns a string from any string table in the object file.
122 std::string getStringAtIndex(unsigned inStringTableSectionIndex, unsigned inStringIndex);
125 //! \name Symbol table
126 //! Methods for accessing the object file's symbol table. Currently only
127 //! a single symbol table with the section name ".symtab" is supported.
129 //! \brief Returns the number of symbols in the default ".symtab" symbol table.
130 unsigned getSymbolCount();
132 //! \brief Returns the symbol with index \a inIndex.
133 const Elf32_Sym & getSymbolAtIndex(unsigned inIndex);
135 //! \brief Returns the section index of the string table containing symbol names.
136 unsigned getSymbolNameStringTableIndex() const;
138 //! \brief Returns the name of the symbol described by \a inSymbol.
139 std::string getSymbolName(const Elf32_Sym & inSymbol);
141 unsigned getIndexOfSymbolAtAddress(uint32_t symbolAddress, bool strict=true);
143 ARMSymbolType_t getTypeOfSymbolAtIndex(unsigned symbolIndex);
149 void dumpSymbolTable();
153 std::istream & m_stream; //!< The source stream for the ELF file.
154 ELFVariant_t m_elfVariant; //!< Variant of the ARM ELF format specification.
155 std::string m_name; //!< File name. (optional)
156 Elf32_Ehdr m_header; //!< The ELF file header.
157 std::vector<Elf32_Shdr> m_sectionHeaders; //!< All of the section headers.
158 std::vector<Elf32_Phdr> m_programHeaders; //!< All of the program headers.
159 unsigned m_symbolTableIndex; //!< Index of ".symtab" section, or #SHN_UNDEF if not present.
162 * Little structure containing information about cached section data.
164 struct SectionDataInfo
166 uint8_t * m_data; //!< Pointer to section data.
167 unsigned m_size; //!< Section data size in bytes.
168 bool m_swapped; //!< Has this section been byte swapped yet? Used for symbol table.
170 typedef std::map<unsigned, SectionDataInfo> SectionDataMap;
171 SectionDataMap m_sectionDataCache; //!< Cached data of sections.
173 //! \brief Reads a section's data either from cache or from disk.
174 SectionDataInfo & getCachedSectionData(unsigned inSectionIndex);
176 //! \brief Reads the file, section, and program headers into memory.
177 void readFileHeaders();
179 uint8_t * readSectionData(const Elf32_Shdr & inHeader);
180 uint8_t * readSegmentData(const Elf32_Phdr & inHeader);
182 //! \brief Byte swaps the symbol table data into host endianness.
183 void byteSwapSymbolTable(const Elf32_Shdr & header, SectionDataInfo & info);
187 * \brief Simple exception thrown to indicate an error in the input ELF file format.
189 class StELFFileException : public std::runtime_error
192 //! \brief Default constructor.
193 StELFFileException(const std::string & inMessage) : std::runtime_error(inMessage) {}
196 #endif // _StELFFile_h_