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 /// 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
18 /// reading both 32 and 64 bit instances of the object.
19 //===----------------------------------------------------------------------===//
21 #ifndef liblldb_ELFHeader_h_
22 #define liblldb_ELFHeader_h_
24 #include "llvm/BinaryFormat/ELF.h"
26 #include "lldb/lldb-enumerations.h"
27 #include "lldb/lldb-types.h"
29 namespace lldb_private {
31 } // End namespace lldb_private.
35 //------------------------------------------------------------------------------
36 /// @name ELF type definitions.
38 /// Types used to represent the various components of ELF structures. All
39 /// types are signed or unsigned integral types wide enough to hold values
41 /// 32 and 64 bit ELF variants.
43 typedef uint64_t elf_addr;
44 typedef uint64_t elf_off;
45 typedef uint16_t elf_half;
46 typedef uint32_t elf_word;
47 typedef int32_t elf_sword;
48 typedef uint64_t elf_size;
49 typedef uint64_t elf_xword;
50 typedef int64_t elf_sxword;
53 //------------------------------------------------------------------------------
55 /// Generic representation of an ELF file header.
57 /// This object is used to identify the general attributes on an ELF file and
58 /// to locate additional sections within the file.
60 unsigned char e_ident[llvm::ELF::EI_NIDENT]; ///< ELF file identification.
61 elf_addr e_entry; ///< Virtual address program entry point.
62 elf_off e_phoff; ///< File offset of program header table.
63 elf_off e_shoff; ///< File offset of section header table.
64 elf_word e_flags; ///< Processor specific flags.
65 elf_word e_version; ///< Version of object file (always 1).
66 elf_half e_type; ///< Object file type.
67 elf_half e_machine; ///< Target architecture.
68 elf_half e_ehsize; ///< Byte size of the ELF header.
69 elf_half e_phentsize; ///< Size of a program header table entry.
70 elf_half e_phnum_hdr; ///< Number of program header entries.
71 elf_half e_shentsize; ///< Size of a section header table entry.
72 elf_half e_shnum_hdr; ///< Number of section header entries.
73 elf_half e_shstrndx_hdr; ///< String table section index.
75 // In some cases these numbers do not fit in 16 bits and they are
76 // stored outside of the header in section #0. Here are the actual
78 elf_word e_phnum; ///< Number of program header entries.
79 elf_word e_shnum; ///< Number of section header entries.
80 elf_word e_shstrndx; ///< String table section index.
84 //--------------------------------------------------------------------------
85 /// Returns true if this is a 32 bit ELF file header.
88 /// True if this is a 32 bit ELF file header.
89 bool Is32Bit() const {
90 return e_ident[llvm::ELF::EI_CLASS] == llvm::ELF::ELFCLASS32;
93 //--------------------------------------------------------------------------
94 /// Returns true if this is a 64 bit ELF file header.
97 /// True if this is a 64 bit ELF file header.
98 bool Is64Bit() const {
99 return e_ident[llvm::ELF::EI_CLASS] == llvm::ELF::ELFCLASS64;
102 //--------------------------------------------------------------------------
103 /// The byte order of this ELF file header.
106 /// The byte order of this ELF file as described by the header.
107 lldb::ByteOrder GetByteOrder() const;
109 //--------------------------------------------------------------------------
110 /// The jump slot relocation type of this ELF.
111 unsigned GetRelocationJumpSlotType() const;
113 //--------------------------------------------------------------------------
114 /// Check if there should be header extension in section header #0
117 /// True if parsing the ELFHeader requires reading header extension
118 /// and false otherwise.
119 bool HasHeaderExtension() const;
121 //--------------------------------------------------------------------------
122 /// Parse an ELFHeader entry starting at position \p offset and update the
123 /// data extractor with the address size and byte order attributes as
124 /// defined by the header.
126 /// @param[in,out] data
127 /// The DataExtractor to read from. Updated with the address size and
128 /// byte order attributes appropriate to this header.
130 /// @param[in,out] offset
131 /// Pointer to an offset in the data. On return the offset will be
132 /// advanced by the number of bytes read.
135 /// True if the ELFHeader was successfully read and false
137 bool Parse(lldb_private::DataExtractor &data, lldb::offset_t *offset);
139 //--------------------------------------------------------------------------
140 /// Examines at most EI_NIDENT bytes starting from the given pointer and
141 /// determines if the magic ELF identification exists.
144 /// True if the given sequence of bytes identifies an ELF file.
145 static bool MagicBytesMatch(const uint8_t *magic);
147 //--------------------------------------------------------------------------
148 /// Examines at most EI_NIDENT bytes starting from the given address and
149 /// determines the address size of the underlying ELF file. This function
150 /// should only be called on an pointer for which MagicBytesMatch returns
154 /// The number of bytes forming an address in the ELF file (either 4 or
155 /// 8), else zero if the address size could not be determined.
156 static unsigned AddressSizeInBytes(const uint8_t *magic);
160 //--------------------------------------------------------------------------
161 /// Parse an ELFHeader header extension entry. This method is called by
165 /// The DataExtractor to read from.
166 void ParseHeaderExtension(lldb_private::DataExtractor &data);
169 //------------------------------------------------------------------------------
170 /// @class ELFSectionHeader
171 /// Generic representation of an ELF section header.
172 struct ELFSectionHeader {
173 elf_word sh_name; ///< Section name string index.
174 elf_word sh_type; ///< Section type.
175 elf_xword sh_flags; ///< Section attributes.
176 elf_addr sh_addr; ///< Virtual address of the section in memory.
177 elf_off sh_offset; ///< Start of section from beginning of file.
178 elf_xword sh_size; ///< Number of bytes occupied in the file.
179 elf_word sh_link; ///< Index of associated section.
180 elf_word sh_info; ///< Extra section info (overloaded).
181 elf_xword sh_addralign; ///< Power of two alignment constraint.
182 elf_xword sh_entsize; ///< Byte size of each section entry.
186 //--------------------------------------------------------------------------
187 /// Parse an ELFSectionHeader entry from the given DataExtracter starting at
188 /// position \p offset.
191 /// The DataExtractor to read from. The address size of the extractor
192 /// determines if a 32 or 64 bit object should be read.
194 /// @param[in,out] offset
195 /// Pointer to an offset in the data. On return the offset will be
196 /// advanced by the number of bytes read.
199 /// True if the ELFSectionHeader was successfully read and false
201 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
204 //------------------------------------------------------------------------------
205 /// @class ELFProgramHeader
206 /// Generic representation of an ELF program header.
207 struct ELFProgramHeader {
208 elf_word p_type; ///< Type of program segment.
209 elf_word p_flags; ///< Segment attributes.
210 elf_off p_offset; ///< Start of segment from beginning of file.
211 elf_addr p_vaddr; ///< Virtual address of segment in memory.
212 elf_addr p_paddr; ///< Physical address (for non-VM systems).
213 elf_xword p_filesz; ///< Byte size of the segment in file.
214 elf_xword p_memsz; ///< Byte size of the segment in memory.
215 elf_xword p_align; ///< Segment alignment constraint.
219 /// Parse an ELFProgramHeader entry from the given DataExtractor starting at
220 /// position \p offset. The address size of the DataExtractor determines if
221 /// a 32 or 64 bit object is to be parsed.
224 /// The DataExtractor to read from. The address size of the extractor
225 /// determines if a 32 or 64 bit object should be read.
227 /// @param[in,out] offset
228 /// Pointer to an offset in the data. On return the offset will be
229 /// advanced by the number of bytes read.
232 /// True if the ELFProgramHeader was successfully read and false
234 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
237 //------------------------------------------------------------------------------
239 /// Represents a symbol within an ELF symbol table.
241 elf_addr st_value; ///< Absolute or relocatable address.
242 elf_xword st_size; ///< Size of the symbol or zero.
243 elf_word st_name; ///< Symbol name string index.
244 unsigned char st_info; ///< Symbol type and binding attributes.
245 unsigned char st_other; ///< Reserved for future use.
246 elf_half st_shndx; ///< Section to which this symbol applies.
250 /// Returns the binding attribute of the st_info member.
251 unsigned char getBinding() const { return st_info >> 4; }
253 /// Returns the type attribute of the st_info member.
254 unsigned char getType() const { return st_info & 0x0F; }
256 /// Sets the binding and type of the st_info member.
257 void setBindingAndType(unsigned char binding, unsigned char type) {
258 st_info = (binding << 4) + (type & 0x0F);
261 static const char *bindingToCString(unsigned char binding);
263 static const char *typeToCString(unsigned char type);
266 sectionIndexToCString(elf_half shndx,
267 const lldb_private::SectionList *section_list);
269 /// Parse an ELFSymbol entry from the given DataExtractor starting at
270 /// position \p offset. The address size of the DataExtractor determines if
271 /// a 32 or 64 bit object is to be parsed.
274 /// The DataExtractor to read from. The address size of the extractor
275 /// determines if a 32 or 64 bit object should be read.
277 /// @param[in,out] offset
278 /// Pointer to an offset in the data. On return the offset will be
279 /// advanced by the number of bytes read.
282 /// True if the ELFSymbol was successfully read and false otherwise.
283 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
285 void Dump(lldb_private::Stream *s, uint32_t idx,
286 const lldb_private::DataExtractor *strtab_data,
287 const lldb_private::SectionList *section_list);
290 //------------------------------------------------------------------------------
291 /// @class ELFDynamic
292 /// Represents an entry in an ELF dynamic table.
294 elf_sxword d_tag; ///< Type of dynamic table entry.
296 elf_xword d_val; ///< Integer value of the table entry.
297 elf_addr d_ptr; ///< Pointer value of the table entry.
302 /// Parse an ELFDynamic entry from the given DataExtractor starting at
303 /// position \p offset. The address size of the DataExtractor determines if
304 /// a 32 or 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 ELFDynamic entry was successfully read and false
317 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
320 //------------------------------------------------------------------------------
322 /// Represents a relocation entry with an implicit addend.
324 elf_addr r_offset; ///< Address of reference.
325 elf_xword r_info; ///< symbol index and type of relocation.
329 /// Parse an ELFRel entry from the given DataExtractor starting at position
330 /// \p offset. The address size of the DataExtractor determines if a 32 or
331 /// 64 bit object is to be parsed.
334 /// The DataExtractor to read from. The address size of the extractor
335 /// determines if a 32 or 64 bit object should be read.
337 /// @param[in,out] offset
338 /// Pointer to an offset in the data. On return the offset will be
339 /// advanced by the number of bytes read.
342 /// True if the ELFRel entry was successfully read and false otherwise.
343 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
345 /// Returns the type when the given entry represents a 32-bit relocation.
346 static unsigned RelocType32(const ELFRel &rel) { return rel.r_info & 0x0ff; }
348 /// Returns the type when the given entry represents a 64-bit relocation.
349 static unsigned RelocType64(const ELFRel &rel) {
350 return rel.r_info & 0xffffffff;
353 /// Returns the symbol index when the given entry represents a 32-bit
355 static unsigned RelocSymbol32(const ELFRel &rel) { return rel.r_info >> 8; }
357 /// Returns the symbol index when the given entry represents a 64-bit
359 static unsigned RelocSymbol64(const ELFRel &rel) { return rel.r_info >> 32; }
362 //------------------------------------------------------------------------------
364 /// Represents a relocation entry with an explicit addend.
366 elf_addr r_offset; ///< Address of reference.
367 elf_xword r_info; ///< Symbol index and type of relocation.
368 elf_sxword r_addend; ///< Constant part of expression.
372 /// Parse an ELFRela entry from the given DataExtractor starting at position
373 /// \p offset. The address size of the DataExtractor determines if a 32 or
374 /// 64 bit object is to be parsed.
377 /// The DataExtractor to read from. The address size of the extractor
378 /// determines if a 32 or 64 bit object should be read.
380 /// @param[in,out] offset
381 /// Pointer to an offset in the data. On return the offset will be
382 /// advanced by the number of bytes read.
385 /// True if the ELFRela entry was successfully read and false otherwise.
386 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
388 /// Returns the type when the given entry represents a 32-bit relocation.
389 static unsigned RelocType32(const ELFRela &rela) {
390 return rela.r_info & 0x0ff;
393 /// Returns the type when the given entry represents a 64-bit relocation.
394 static unsigned RelocType64(const ELFRela &rela) {
395 return rela.r_info & 0xffffffff;
398 /// Returns the symbol index when the given entry represents a 32-bit
400 static unsigned RelocSymbol32(const ELFRela &rela) {
401 return rela.r_info >> 8;
404 /// Returns the symbol index when the given entry represents a 64-bit
406 static unsigned RelocSymbol64(const ELFRela &rela) {
407 return rela.r_info >> 32;
411 } // End namespace elf.
413 #endif // #ifndef liblldb_ELFHeader_h_