blob: faaf8be99d687cc5d75987bb1f1d9da8e07d1cc6 [file] [log] [blame]
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001//===-- ELFHeader.h ------------------------------------------- -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// @file
Adrian Prantld8f460e2018-05-02 16:55:16 +000011/// Generic structures and typedefs for ELF files.
Stephen Wilsonf325ba92010-07-13 23:07:23 +000012///
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
Adrian Prantld8f460e2018-05-02 16:55:16 +000017/// entity provides a \c Parse method which is capable of transparently
18/// reading both 32 and 64 bit instances of the object.
Stephen Wilsonf325ba92010-07-13 23:07:23 +000019//===----------------------------------------------------------------------===//
20
21#ifndef liblldb_ELFHeader_h_
22#define liblldb_ELFHeader_h_
23
Zachary Turner264b5d92017-06-07 03:48:56 +000024#include "llvm/BinaryFormat/ELF.h"
Stephen Wilsonf325ba92010-07-13 23:07:23 +000025
26#include "lldb/lldb-enumerations.h"
Pavel Labath23ccc292017-01-31 23:09:46 +000027#include "lldb/lldb-types.h"
Stephen Wilsonf325ba92010-07-13 23:07:23 +000028
Kate Stoneb9c1b512016-09-06 20:57:50 +000029namespace lldb_private {
Stephen Wilsonf325ba92010-07-13 23:07:23 +000030class DataExtractor;
31} // End namespace lldb_private.
32
Kate Stoneb9c1b512016-09-06 20:57:50 +000033namespace elf {
Stephen Wilsonf325ba92010-07-13 23:07:23 +000034
35//------------------------------------------------------------------------------
36/// @name ELF type definitions.
37///
Adrian Prantld8f460e2018-05-02 16:55:16 +000038/// Types used to represent the various components of ELF structures. All
39/// types are signed or unsigned integral types wide enough to hold values
40/// from both
Stephen Wilsonf325ba92010-07-13 23:07:23 +000041/// 32 and 64 bit ELF variants.
42//@{
43typedef uint64_t elf_addr;
44typedef uint64_t elf_off;
45typedef uint16_t elf_half;
46typedef uint32_t elf_word;
Kate Stoneb9c1b512016-09-06 20:57:50 +000047typedef int32_t elf_sword;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000048typedef uint64_t elf_size;
49typedef uint64_t elf_xword;
Kate Stoneb9c1b512016-09-06 20:57:50 +000050typedef int64_t elf_sxword;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000051//@}
52
53//------------------------------------------------------------------------------
54/// @class ELFHeader
Adrian Prantld8f460e2018-05-02 16:55:16 +000055/// Generic representation of an ELF file header.
Stephen Wilsonf325ba92010-07-13 23:07:23 +000056///
Adrian Prantld8f460e2018-05-02 16:55:16 +000057/// This object is used to identify the general attributes on an ELF file and
58/// to locate additional sections within the file.
Kate Stoneb9c1b512016-09-06 20:57:50 +000059struct ELFHeader {
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.
Pavel Labath23ccc292017-01-31 23:09:46 +000070 elf_half e_phnum_hdr; ///< Number of program header entries.
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 elf_half e_shentsize; ///< Size of a section header table entry.
Pavel Labath23ccc292017-01-31 23:09:46 +000072 elf_half e_shnum_hdr; ///< Number of section header entries.
73 elf_half e_shstrndx_hdr; ///< String table section index.
74
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
77 // values.
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.
Stephen Wilsonf325ba92010-07-13 23:07:23 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 ELFHeader();
Stephen Wilsonf325ba92010-07-13 23:07:23 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 //--------------------------------------------------------------------------
85 /// Returns true if this is a 32 bit ELF file header.
86 ///
87 /// @return
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;
91 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +000092
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 //--------------------------------------------------------------------------
94 /// Returns true if this is a 64 bit ELF file header.
95 ///
96 /// @return
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;
100 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 //--------------------------------------------------------------------------
103 /// The byte order of this ELF file header.
104 ///
105 /// @return
106 /// The byte order of this ELF file as described by the header.
107 lldb::ByteOrder GetByteOrder() const;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 //--------------------------------------------------------------------------
110 /// The jump slot relocation type of this ELF.
111 unsigned GetRelocationJumpSlotType() const;
Stephen Wilson43fe6452011-03-30 15:59:12 +0000112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 //--------------------------------------------------------------------------
Pavel Labath23ccc292017-01-31 23:09:46 +0000114 /// Check if there should be header extension in section header #0
115 ///
116 /// @return
117 /// True if parsing the ELFHeader requires reading header extension
118 /// and false otherwise.
119 bool HasHeaderExtension() const;
120
121 //--------------------------------------------------------------------------
Adrian Prantld8f460e2018-05-02 16:55:16 +0000122 /// 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125 ///
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.
129 ///
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.
133 ///
134 /// @return
135 /// True if the ELFHeader was successfully read and false
136 /// otherwise.
137 bool Parse(lldb_private::DataExtractor &data, lldb::offset_t *offset);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 //--------------------------------------------------------------------------
140 /// Examines at most EI_NIDENT bytes starting from the given pointer and
141 /// determines if the magic ELF identification exists.
142 ///
143 /// @return
144 /// True if the given sequence of bytes identifies an ELF file.
145 static bool MagicBytesMatch(const uint8_t *magic);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000146
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147 //--------------------------------------------------------------------------
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
151 /// true.
152 ///
153 /// @return
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);
Pavel Labath23ccc292017-01-31 23:09:46 +0000157
158private:
159
160 //--------------------------------------------------------------------------
Adrian Prantld8f460e2018-05-02 16:55:16 +0000161 /// Parse an ELFHeader header extension entry. This method is called by
162 /// Parse().
Pavel Labath23ccc292017-01-31 23:09:46 +0000163 ///
164 /// @param[in] data
165 /// The DataExtractor to read from.
166 void ParseHeaderExtension(lldb_private::DataExtractor &data);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000167};
168
169//------------------------------------------------------------------------------
170/// @class ELFSectionHeader
Adrian Prantld8f460e2018-05-02 16:55:16 +0000171/// Generic representation of an ELF section header.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172struct 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.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 ELFSectionHeader();
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000185
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 //--------------------------------------------------------------------------
187 /// Parse an ELFSectionHeader entry from the given DataExtracter starting at
188 /// position \p offset.
189 ///
190 /// @param[in] data
191 /// The DataExtractor to read from. The address size of the extractor
192 /// determines if a 32 or 64 bit object should be read.
193 ///
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.
197 ///
198 /// @return
199 /// True if the ELFSectionHeader was successfully read and false
200 /// otherwise.
201 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000202};
203
204//------------------------------------------------------------------------------
205/// @class ELFProgramHeader
Adrian Prantld8f460e2018-05-02 16:55:16 +0000206/// Generic representation of an ELF program header.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207struct 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.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 ELFProgramHeader();
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000218
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219 /// 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.
222 ///
223 /// @param[in] data
224 /// The DataExtractor to read from. The address size of the extractor
225 /// determines if a 32 or 64 bit object should be read.
226 ///
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.
230 ///
231 /// @return
232 /// True if the ELFProgramHeader was successfully read and false
233 /// otherwise.
234 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000235};
236
237//------------------------------------------------------------------------------
238/// @class ELFSymbol
Adrian Prantld8f460e2018-05-02 16:55:16 +0000239/// Represents a symbol within an ELF symbol table.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240struct ELFSymbol {
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.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000247
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248 ELFSymbol();
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000249
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 /// Returns the binding attribute of the st_info member.
251 unsigned char getBinding() const { return st_info >> 4; }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 /// Returns the type attribute of the st_info member.
254 unsigned char getType() const { return st_info & 0x0F; }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000255
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 /// 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);
259 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 static const char *bindingToCString(unsigned char binding);
Greg Clayton9594f4c2013-04-13 23:17:23 +0000262
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263 static const char *typeToCString(unsigned char type);
Greg Clayton9594f4c2013-04-13 23:17:23 +0000264
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 static const char *
266 sectionIndexToCString(elf_half shndx,
267 const lldb_private::SectionList *section_list);
Greg Clayton9594f4c2013-04-13 23:17:23 +0000268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 /// 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.
272 ///
273 /// @param[in] data
274 /// The DataExtractor to read from. The address size of the extractor
275 /// determines if a 32 or 64 bit object should be read.
276 ///
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.
280 ///
281 /// @return
282 /// True if the ELFSymbol was successfully read and false otherwise.
283 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
284
285 void Dump(lldb_private::Stream *s, uint32_t idx,
286 const lldb_private::DataExtractor *strtab_data,
287 const lldb_private::SectionList *section_list);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000288};
289
290//------------------------------------------------------------------------------
291/// @class ELFDynamic
Adrian Prantld8f460e2018-05-02 16:55:16 +0000292/// Represents an entry in an ELF dynamic table.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000293struct ELFDynamic {
294 elf_sxword d_tag; ///< Type of dynamic table entry.
295 union {
296 elf_xword d_val; ///< Integer value of the table entry.
297 elf_addr d_ptr; ///< Pointer value of the table entry.
298 };
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000299
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300 ELFDynamic();
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000301
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302 /// 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.
305 ///
306 /// @param[in] data
307 /// The DataExtractor to read from. The address size of the extractor
308 /// determines if a 32 or 64 bit object should be read.
309 ///
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.
313 ///
314 /// @return
315 /// True if the ELFDynamic entry was successfully read and false
316 /// otherwise.
317 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000318};
319
Stephen Wilson43fe6452011-03-30 15:59:12 +0000320//------------------------------------------------------------------------------
321/// @class ELFRel
Adrian Prantld8f460e2018-05-02 16:55:16 +0000322/// Represents a relocation entry with an implicit addend.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323struct ELFRel {
324 elf_addr r_offset; ///< Address of reference.
325 elf_xword r_info; ///< symbol index and type of relocation.
Stephen Wilson43fe6452011-03-30 15:59:12 +0000326
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327 ELFRel();
Stephen Wilson43fe6452011-03-30 15:59:12 +0000328
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329 /// 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.
332 ///
333 /// @param[in] data
334 /// The DataExtractor to read from. The address size of the extractor
335 /// determines if a 32 or 64 bit object should be read.
336 ///
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.
340 ///
341 /// @return
342 /// True if the ELFRel entry was successfully read and false otherwise.
343 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
Stephen Wilson43fe6452011-03-30 15:59:12 +0000344
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345 /// Returns the type when the given entry represents a 32-bit relocation.
346 static unsigned RelocType32(const ELFRel &rel) { return rel.r_info & 0x0ff; }
Stephen Wilson43fe6452011-03-30 15:59:12 +0000347
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 /// 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;
351 }
Stephen Wilson43fe6452011-03-30 15:59:12 +0000352
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 /// Returns the symbol index when the given entry represents a 32-bit
354 /// relocation.
355 static unsigned RelocSymbol32(const ELFRel &rel) { return rel.r_info >> 8; }
Stephen Wilson43fe6452011-03-30 15:59:12 +0000356
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357 /// Returns the symbol index when the given entry represents a 64-bit
358 /// relocation.
359 static unsigned RelocSymbol64(const ELFRel &rel) { return rel.r_info >> 32; }
Stephen Wilson43fe6452011-03-30 15:59:12 +0000360};
361
362//------------------------------------------------------------------------------
363/// @class ELFRela
Adrian Prantld8f460e2018-05-02 16:55:16 +0000364/// Represents a relocation entry with an explicit addend.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365struct ELFRela {
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.
Stephen Wilson43fe6452011-03-30 15:59:12 +0000369
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 ELFRela();
Stephen Wilson43fe6452011-03-30 15:59:12 +0000371
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372 /// 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.
375 ///
376 /// @param[in] data
377 /// The DataExtractor to read from. The address size of the extractor
378 /// determines if a 32 or 64 bit object should be read.
379 ///
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.
383 ///
384 /// @return
385 /// True if the ELFRela entry was successfully read and false otherwise.
386 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
Stephen Wilson43fe6452011-03-30 15:59:12 +0000387
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388 /// 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;
391 }
Stephen Wilson43fe6452011-03-30 15:59:12 +0000392
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393 /// 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;
396 }
Stephen Wilson43fe6452011-03-30 15:59:12 +0000397
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398 /// Returns the symbol index when the given entry represents a 32-bit
399 /// relocation.
400 static unsigned RelocSymbol32(const ELFRela &rela) {
401 return rela.r_info >> 8;
402 }
Stephen Wilson43fe6452011-03-30 15:59:12 +0000403
Kate Stoneb9c1b512016-09-06 20:57:50 +0000404 /// Returns the symbol index when the given entry represents a 64-bit
405 /// relocation.
406 static unsigned RelocSymbol64(const ELFRela &rela) {
407 return rela.r_info >> 32;
408 }
Stephen Wilson43fe6452011-03-30 15:59:12 +0000409};
410
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000411} // End namespace elf.
412
413#endif // #ifndef liblldb_ELFHeader_h_