1 //===-- ELFHeader.h ------------------------------------------- -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// @brief Generic structures and typedefs for ELF files.
13 /// This file provides definitions for the various entities comprising an ELF
14 /// file. The structures are generic in the sense that they do not correspond
15 /// to the exact binary layout of an ELF, but can be used to hold the
16 /// information present in both 32 and 64 bit variants of the format. Each
17 /// entity provides a \c Parse method which is capable of transparently reading
18 /// both 32 and 64 bit instances of the object.
19 //===----------------------------------------------------------------------===//
21 #ifndef liblldb_ELFHeader_h_
22 #define liblldb_ELFHeader_h_
24 #include "llvm/Support/ELF.h"
26 #include "lldb/lldb-enumerations.h"
28 namespace lldb_private {
30 } // End namespace lldb_private.
34 //------------------------------------------------------------------------------
35 /// @name ELF type definitions.
37 /// Types used to represent the various components of ELF structures. All types
38 /// are signed or unsigned integral types wide enough to hold values from both
39 /// 32 and 64 bit ELF variants.
41 typedef uint64_t elf_addr;
42 typedef uint64_t elf_off;
43 typedef uint16_t elf_half;
44 typedef uint32_t elf_word;
45 typedef int32_t elf_sword;
46 typedef uint64_t elf_size;
47 typedef uint64_t elf_xword;
48 typedef int64_t elf_sxword;
51 //------------------------------------------------------------------------------
53 /// @brief Generic representation of an ELF file header.
55 /// This object is used to identify the general attributes on an ELF file and to
56 /// locate additional sections within the file.
58 unsigned char e_ident[llvm::ELF::EI_NIDENT]; ///< ELF file identification.
59 elf_addr e_entry; ///< Virtual address program entry point.
60 elf_off e_phoff; ///< File offset of program header table.
61 elf_off e_shoff; ///< File offset of section header table.
62 elf_word e_flags; ///< Processor specific flags.
63 elf_word e_version; ///< Version of object file (always 1).
64 elf_half e_type; ///< Object file type.
65 elf_half e_machine; ///< Target architecture.
66 elf_half e_ehsize; ///< Byte size of the ELF header.
67 elf_half e_phentsize; ///< Size of a program header table entry.
68 elf_half e_phnum; ///< Number of program header entries.
69 elf_half e_shentsize; ///< Size of a section header table entry.
70 elf_half e_shnum; ///< Number of section header entries.
71 elf_half e_shstrndx; ///< String table section index.
75 //--------------------------------------------------------------------------
76 /// Returns true if this is a 32 bit ELF file header.
79 /// True if this is a 32 bit ELF file header.
80 bool Is32Bit() const {
81 return e_ident[llvm::ELF::EI_CLASS] == llvm::ELF::ELFCLASS32;
84 //--------------------------------------------------------------------------
85 /// Returns true if this is a 64 bit ELF file header.
88 /// True if this is a 64 bit ELF file header.
89 bool Is64Bit() const {
90 return e_ident[llvm::ELF::EI_CLASS] == llvm::ELF::ELFCLASS64;
93 //--------------------------------------------------------------------------
94 /// The byte order of this ELF file header.
97 /// The byte order of this ELF file as described by the header.
98 lldb::ByteOrder GetByteOrder() const;
100 //--------------------------------------------------------------------------
101 /// The jump slot relocation type of this ELF.
102 unsigned GetRelocationJumpSlotType() const;
104 //--------------------------------------------------------------------------
105 /// Parse an ELFHeader entry starting at position \p offset and
106 /// update the data extractor with the address size and byte order
107 /// attributes as defined by the header.
109 /// @param[in,out] data
110 /// The DataExtractor to read from. Updated with the address size and
111 /// byte order attributes appropriate to this header.
113 /// @param[in,out] offset
114 /// Pointer to an offset in the data. On return the offset will be
115 /// advanced by the number of bytes read.
118 /// True if the ELFHeader was successfully read and false
120 bool Parse(lldb_private::DataExtractor &data, lldb::offset_t *offset);
122 //--------------------------------------------------------------------------
123 /// Examines at most EI_NIDENT bytes starting from the given pointer and
124 /// determines if the magic ELF identification exists.
127 /// True if the given sequence of bytes identifies an ELF file.
128 static bool MagicBytesMatch(const uint8_t *magic);
130 //--------------------------------------------------------------------------
131 /// Examines at most EI_NIDENT bytes starting from the given address and
132 /// determines the address size of the underlying ELF file. This function
133 /// should only be called on an pointer for which MagicBytesMatch returns
137 /// The number of bytes forming an address in the ELF file (either 4 or
138 /// 8), else zero if the address size could not be determined.
139 static unsigned AddressSizeInBytes(const uint8_t *magic);
142 //------------------------------------------------------------------------------
143 /// @class ELFSectionHeader
144 /// @brief Generic representation of an ELF section header.
145 struct ELFSectionHeader {
146 elf_word sh_name; ///< Section name string index.
147 elf_word sh_type; ///< Section type.
148 elf_xword sh_flags; ///< Section attributes.
149 elf_addr sh_addr; ///< Virtual address of the section in memory.
150 elf_off sh_offset; ///< Start of section from beginning of file.
151 elf_xword sh_size; ///< Number of bytes occupied in the file.
152 elf_word sh_link; ///< Index of associated section.
153 elf_word sh_info; ///< Extra section info (overloaded).
154 elf_xword sh_addralign; ///< Power of two alignment constraint.
155 elf_xword sh_entsize; ///< Byte size of each section entry.
159 //--------------------------------------------------------------------------
160 /// Parse an ELFSectionHeader entry from the given DataExtracter starting at
161 /// position \p offset.
164 /// The DataExtractor to read from. The address size of the extractor
165 /// determines if a 32 or 64 bit object should be read.
167 /// @param[in,out] offset
168 /// Pointer to an offset in the data. On return the offset will be
169 /// advanced by the number of bytes read.
172 /// True if the ELFSectionHeader was successfully read and false
174 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
177 //------------------------------------------------------------------------------
178 /// @class ELFProgramHeader
179 /// @brief Generic representation of an ELF program header.
180 struct ELFProgramHeader {
181 elf_word p_type; ///< Type of program segment.
182 elf_word p_flags; ///< Segment attributes.
183 elf_off p_offset; ///< Start of segment from beginning of file.
184 elf_addr p_vaddr; ///< Virtual address of segment in memory.
185 elf_addr p_paddr; ///< Physical address (for non-VM systems).
186 elf_xword p_filesz; ///< Byte size of the segment in file.
187 elf_xword p_memsz; ///< Byte size of the segment in memory.
188 elf_xword p_align; ///< Segment alignment constraint.
192 /// Parse an ELFProgramHeader entry from the given DataExtractor starting at
193 /// position \p offset. The address size of the DataExtractor determines if
194 /// a 32 or 64 bit object is to be parsed.
197 /// The DataExtractor to read from. The address size of the extractor
198 /// determines if a 32 or 64 bit object should be read.
200 /// @param[in,out] offset
201 /// Pointer to an offset in the data. On return the offset will be
202 /// advanced by the number of bytes read.
205 /// True if the ELFProgramHeader was successfully read and false
207 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
210 //------------------------------------------------------------------------------
212 /// @brief Represents a symbol within an ELF symbol table.
214 elf_addr st_value; ///< Absolute or relocatable address.
215 elf_xword st_size; ///< Size of the symbol or zero.
216 elf_word st_name; ///< Symbol name string index.
217 unsigned char st_info; ///< Symbol type and binding attributes.
218 unsigned char st_other; ///< Reserved for future use.
219 elf_half st_shndx; ///< Section to which this symbol applies.
223 /// Returns the binding attribute of the st_info member.
224 unsigned char getBinding() const { return st_info >> 4; }
226 /// Returns the type attribute of the st_info member.
227 unsigned char getType() const { return st_info & 0x0F; }
229 /// Sets the binding and type of the st_info member.
230 void setBindingAndType(unsigned char binding, unsigned char type) {
231 st_info = (binding << 4) + (type & 0x0F);
234 static const char *bindingToCString(unsigned char binding);
236 static const char *typeToCString(unsigned char type);
239 sectionIndexToCString(elf_half shndx,
240 const lldb_private::SectionList *section_list);
242 /// Parse an ELFSymbol entry from the given DataExtractor starting at
243 /// position \p offset. The address size of the DataExtractor determines if
244 /// a 32 or 64 bit object is to be parsed.
247 /// The DataExtractor to read from. The address size of the extractor
248 /// determines if a 32 or 64 bit object should be read.
250 /// @param[in,out] offset
251 /// Pointer to an offset in the data. On return the offset will be
252 /// advanced by the number of bytes read.
255 /// True if the ELFSymbol was successfully read and false otherwise.
256 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
258 void Dump(lldb_private::Stream *s, uint32_t idx,
259 const lldb_private::DataExtractor *strtab_data,
260 const lldb_private::SectionList *section_list);
263 //------------------------------------------------------------------------------
264 /// @class ELFDynamic
265 /// @brief Represents an entry in an ELF dynamic table.
267 elf_sxword d_tag; ///< Type of dynamic table entry.
269 elf_xword d_val; ///< Integer value of the table entry.
270 elf_addr d_ptr; ///< Pointer value of the table entry.
275 /// Parse an ELFDynamic entry from the given DataExtractor starting at
276 /// position \p offset. The address size of the DataExtractor determines if
277 /// a 32 or 64 bit object is to be parsed.
280 /// The DataExtractor to read from. The address size of the extractor
281 /// determines if a 32 or 64 bit object should be read.
283 /// @param[in,out] offset
284 /// Pointer to an offset in the data. On return the offset will be
285 /// advanced by the number of bytes read.
288 /// True if the ELFDynamic entry was successfully read and false
290 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
293 //------------------------------------------------------------------------------
295 /// @brief Represents a relocation entry with an implicit addend.
297 elf_addr r_offset; ///< Address of reference.
298 elf_xword r_info; ///< symbol index and type of relocation.
302 /// Parse an ELFRel entry from the given DataExtractor starting at position
303 /// \p offset. The address size of the DataExtractor determines if a 32 or
304 /// 64 bit object is to be parsed.
307 /// The DataExtractor to read from. The address size of the extractor
308 /// determines if a 32 or 64 bit object should be read.
310 /// @param[in,out] offset
311 /// Pointer to an offset in the data. On return the offset will be
312 /// advanced by the number of bytes read.
315 /// True if the ELFRel entry was successfully read and false otherwise.
316 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
318 /// Returns the type when the given entry represents a 32-bit relocation.
319 static unsigned RelocType32(const ELFRel &rel) { return rel.r_info & 0x0ff; }
321 /// Returns the type when the given entry represents a 64-bit relocation.
322 static unsigned RelocType64(const ELFRel &rel) {
323 return rel.r_info & 0xffffffff;
326 /// Returns the symbol index when the given entry represents a 32-bit
328 static unsigned RelocSymbol32(const ELFRel &rel) { return rel.r_info >> 8; }
330 /// Returns the symbol index when the given entry represents a 64-bit
332 static unsigned RelocSymbol64(const ELFRel &rel) { return rel.r_info >> 32; }
335 //------------------------------------------------------------------------------
337 /// @brief Represents a relocation entry with an explicit addend.
339 elf_addr r_offset; ///< Address of reference.
340 elf_xword r_info; ///< Symbol index and type of relocation.
341 elf_sxword r_addend; ///< Constant part of expression.
345 /// Parse an ELFRela entry from the given DataExtractor starting at position
346 /// \p offset. The address size of the DataExtractor determines if a 32 or
347 /// 64 bit object is to be parsed.
350 /// The DataExtractor to read from. The address size of the extractor
351 /// determines if a 32 or 64 bit object should be read.
353 /// @param[in,out] offset
354 /// Pointer to an offset in the data. On return the offset will be
355 /// advanced by the number of bytes read.
358 /// True if the ELFRela entry was successfully read and false otherwise.
359 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
361 /// Returns the type when the given entry represents a 32-bit relocation.
362 static unsigned RelocType32(const ELFRela &rela) {
363 return rela.r_info & 0x0ff;
366 /// Returns the type when the given entry represents a 64-bit relocation.
367 static unsigned RelocType64(const ELFRela &rela) {
368 return rela.r_info & 0xffffffff;
371 /// Returns the symbol index when the given entry represents a 32-bit
373 static unsigned RelocSymbol32(const ELFRela &rela) {
374 return rela.r_info >> 8;
377 /// Returns the symbol index when the given entry represents a 64-bit
379 static unsigned RelocSymbol64(const ELFRela &rela) {
380 return rela.r_info >> 32;
384 } // End namespace elf.
386 #endif // #ifndef liblldb_ELFHeader_h_