1 //===- ELFTypes.h - Endian specific types for ELF ---------------*- 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 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_OBJECT_ELFTYPES_H
11 #define LLVM_OBJECT_ELFTYPES_H
13 #include "llvm/Support/AlignOf.h"
14 #include "llvm/Support/DataTypes.h"
15 #include "llvm/Support/ELF.h"
16 #include "llvm/Support/Endian.h"
21 using support::endianness;
23 template <endianness target_endianness, std::size_t max_alignment,
26 static const endianness TargetEndianness = target_endianness;
27 static const std::size_t MaxAlignment = max_alignment;
28 static const bool Is64Bits = is64Bits;
31 template <typename T, int max_align> struct MaximumAlignment {
32 enum { value = AlignOf<T>::Alignment > max_align ? max_align
33 : AlignOf<T>::Alignment
37 // Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
38 template <endianness target_endianness, std::size_t max_alignment>
39 struct ELFDataTypeTypedefHelperCommon {
40 typedef support::detail::packed_endian_specific_integral<
41 uint16_t, target_endianness,
42 MaximumAlignment<uint16_t, max_alignment>::value> Elf_Half;
43 typedef support::detail::packed_endian_specific_integral<
44 uint32_t, target_endianness,
45 MaximumAlignment<uint32_t, max_alignment>::value> Elf_Word;
46 typedef support::detail::packed_endian_specific_integral<
47 int32_t, target_endianness,
48 MaximumAlignment<int32_t, max_alignment>::value> Elf_Sword;
49 typedef support::detail::packed_endian_specific_integral<
50 uint64_t, target_endianness,
51 MaximumAlignment<uint64_t, max_alignment>::value> Elf_Xword;
52 typedef support::detail::packed_endian_specific_integral<
53 int64_t, target_endianness,
54 MaximumAlignment<int64_t, max_alignment>::value> Elf_Sxword;
57 template <class ELFT> struct ELFDataTypeTypedefHelper;
60 template <endianness TargetEndianness, std::size_t MaxAlign>
61 struct ELFDataTypeTypedefHelper<ELFType<TargetEndianness, MaxAlign, false> >
62 : ELFDataTypeTypedefHelperCommon<TargetEndianness, MaxAlign> {
63 typedef uint32_t value_type;
64 typedef support::detail::packed_endian_specific_integral<
65 value_type, TargetEndianness,
66 MaximumAlignment<value_type, MaxAlign>::value> Elf_Addr;
67 typedef support::detail::packed_endian_specific_integral<
68 value_type, TargetEndianness,
69 MaximumAlignment<value_type, MaxAlign>::value> Elf_Off;
73 template <endianness TargetEndianness, std::size_t MaxAlign>
74 struct ELFDataTypeTypedefHelper<ELFType<TargetEndianness, MaxAlign, true> >
75 : ELFDataTypeTypedefHelperCommon<TargetEndianness, MaxAlign> {
76 typedef uint64_t value_type;
77 typedef support::detail::packed_endian_specific_integral<
78 value_type, TargetEndianness,
79 MaximumAlignment<value_type, MaxAlign>::value> Elf_Addr;
80 typedef support::detail::packed_endian_specific_integral<
81 value_type, TargetEndianness,
82 MaximumAlignment<value_type, MaxAlign>::value> Elf_Off;
85 // I really don't like doing this, but the alternative is copypasta.
86 #define LLVM_ELF_IMPORT_TYPES(E, M, W) \
87 typedef typename ELFDataTypeTypedefHelper<ELFType<E, M, W> >::Elf_Addr \
89 typedef typename ELFDataTypeTypedefHelper<ELFType<E, M, W> >::Elf_Off \
91 typedef typename ELFDataTypeTypedefHelper<ELFType<E, M, W> >::Elf_Half \
93 typedef typename ELFDataTypeTypedefHelper<ELFType<E, M, W> >::Elf_Word \
95 typedef typename ELFDataTypeTypedefHelper<ELFType<E, M, W> >::Elf_Sword \
97 typedef typename ELFDataTypeTypedefHelper<ELFType<E, M, W> >::Elf_Xword \
99 typedef typename ELFDataTypeTypedefHelper<ELFType<E, M, W> >::Elf_Sxword \
102 #define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) \
103 LLVM_ELF_IMPORT_TYPES(ELFT::TargetEndianness, ELFT::MaxAlignment, \
107 template <class ELFT> struct Elf_Shdr_Base;
109 template <endianness TargetEndianness, std::size_t MaxAlign>
110 struct Elf_Shdr_Base<ELFType<TargetEndianness, MaxAlign, false> > {
111 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
112 Elf_Word sh_name; // Section name (index into string table)
113 Elf_Word sh_type; // Section type (SHT_*)
114 Elf_Word sh_flags; // Section flags (SHF_*)
115 Elf_Addr sh_addr; // Address where section is to be loaded
116 Elf_Off sh_offset; // File offset of section data, in bytes
117 Elf_Word sh_size; // Size of section, in bytes
118 Elf_Word sh_link; // Section type-specific header table index link
119 Elf_Word sh_info; // Section type-specific extra information
120 Elf_Word sh_addralign; // Section address alignment
121 Elf_Word sh_entsize; // Size of records contained within the section
124 template <endianness TargetEndianness, std::size_t MaxAlign>
125 struct Elf_Shdr_Base<ELFType<TargetEndianness, MaxAlign, true> > {
126 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
127 Elf_Word sh_name; // Section name (index into string table)
128 Elf_Word sh_type; // Section type (SHT_*)
129 Elf_Xword sh_flags; // Section flags (SHF_*)
130 Elf_Addr sh_addr; // Address where section is to be loaded
131 Elf_Off sh_offset; // File offset of section data, in bytes
132 Elf_Xword sh_size; // Size of section, in bytes
133 Elf_Word sh_link; // Section type-specific header table index link
134 Elf_Word sh_info; // Section type-specific extra information
135 Elf_Xword sh_addralign; // Section address alignment
136 Elf_Xword sh_entsize; // Size of records contained within the section
139 template <class ELFT>
140 struct Elf_Shdr_Impl : Elf_Shdr_Base<ELFT> {
141 using Elf_Shdr_Base<ELFT>::sh_entsize;
142 using Elf_Shdr_Base<ELFT>::sh_size;
144 /// @brief Get the number of entities this section contains if it has any.
145 unsigned getEntityCount() const {
148 return sh_size / sh_entsize;
152 template <class ELFT> struct Elf_Sym_Base;
154 template <endianness TargetEndianness, std::size_t MaxAlign>
155 struct Elf_Sym_Base<ELFType<TargetEndianness, MaxAlign, false> > {
156 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
157 Elf_Word st_name; // Symbol name (index into string table)
158 Elf_Addr st_value; // Value or address associated with the symbol
159 Elf_Word st_size; // Size of the symbol
160 unsigned char st_info; // Symbol's type and binding attributes
161 unsigned char st_other; // Must be zero; reserved
162 Elf_Half st_shndx; // Which section (header table index) it's defined in
165 template <endianness TargetEndianness, std::size_t MaxAlign>
166 struct Elf_Sym_Base<ELFType<TargetEndianness, MaxAlign, true> > {
167 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
168 Elf_Word st_name; // Symbol name (index into string table)
169 unsigned char st_info; // Symbol's type and binding attributes
170 unsigned char st_other; // Must be zero; reserved
171 Elf_Half st_shndx; // Which section (header table index) it's defined in
172 Elf_Addr st_value; // Value or address associated with the symbol
173 Elf_Xword st_size; // Size of the symbol
176 template <class ELFT>
177 struct Elf_Sym_Impl : Elf_Sym_Base<ELFT> {
178 using Elf_Sym_Base<ELFT>::st_info;
179 using Elf_Sym_Base<ELFT>::st_other;
181 // These accessors and mutators correspond to the ELF32_ST_BIND,
182 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
183 unsigned char getBinding() const { return st_info >> 4; }
184 unsigned char getType() const { return st_info & 0x0f; }
185 void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
186 void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
187 void setBindingAndType(unsigned char b, unsigned char t) {
188 st_info = (b << 4) + (t & 0x0f);
191 /// Access to the STV_xxx flag stored in the first two bits of st_other.
196 unsigned char getVisibility() const { return st_other & 0x3; }
197 void setVisibility(unsigned char v) {
198 assert(v < 4 && "Invalid value for visibility");
199 st_other = (st_other & ~0x3) | v;
203 /// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section
204 /// (.gnu.version). This structure is identical for ELF32 and ELF64.
205 template <class ELFT>
206 struct Elf_Versym_Impl {
207 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
208 Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN)
211 template <class ELFT> struct Elf_Verdaux_Impl;
213 /// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section
214 /// (.gnu.version_d). This structure is identical for ELF32 and ELF64.
215 template <class ELFT>
216 struct Elf_Verdef_Impl {
217 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
218 typedef Elf_Verdaux_Impl<ELFT> Elf_Verdaux;
219 Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT)
220 Elf_Half vd_flags; // Bitwise flags (VER_DEF_*)
221 Elf_Half vd_ndx; // Version index, used in .gnu.version entries
222 Elf_Half vd_cnt; // Number of Verdaux entries
223 Elf_Word vd_hash; // Hash of name
224 Elf_Word vd_aux; // Offset to the first Verdaux entry (in bytes)
225 Elf_Word vd_next; // Offset to the next Verdef entry (in bytes)
227 /// Get the first Verdaux entry for this Verdef.
228 const Elf_Verdaux *getAux() const {
229 return reinterpret_cast<const Elf_Verdaux *>((const char *)this + vd_aux);
233 /// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef
234 /// section (.gnu.version_d). This structure is identical for ELF32 and ELF64.
235 template <class ELFT>
236 struct Elf_Verdaux_Impl {
237 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
238 Elf_Word vda_name; // Version name (offset in string table)
239 Elf_Word vda_next; // Offset to next Verdaux entry (in bytes)
242 /// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed
243 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
244 template <class ELFT>
245 struct Elf_Verneed_Impl {
246 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
247 Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT)
248 Elf_Half vn_cnt; // Number of associated Vernaux entries
249 Elf_Word vn_file; // Library name (string table offset)
250 Elf_Word vn_aux; // Offset to first Vernaux entry (in bytes)
251 Elf_Word vn_next; // Offset to next Verneed entry (in bytes)
254 /// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed
255 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
256 template <class ELFT>
257 struct Elf_Vernaux_Impl {
258 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
259 Elf_Word vna_hash; // Hash of dependency name
260 Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*)
261 Elf_Half vna_other; // Version index, used in .gnu.version entries
262 Elf_Word vna_name; // Dependency name
263 Elf_Word vna_next; // Offset to next Vernaux entry (in bytes)
266 /// Elf_Dyn_Base: This structure matches the form of entries in the dynamic
267 /// table section (.dynamic) look like.
268 template <class ELFT> struct Elf_Dyn_Base;
270 template <endianness TargetEndianness, std::size_t MaxAlign>
271 struct Elf_Dyn_Base<ELFType<TargetEndianness, MaxAlign, false> > {
272 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
280 template <endianness TargetEndianness, std::size_t MaxAlign>
281 struct Elf_Dyn_Base<ELFType<TargetEndianness, MaxAlign, true> > {
282 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
290 /// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters.
291 template <class ELFT>
292 struct Elf_Dyn_Impl : Elf_Dyn_Base<ELFT> {
293 using Elf_Dyn_Base<ELFT>::d_tag;
294 using Elf_Dyn_Base<ELFT>::d_un;
295 int64_t getTag() const { return d_tag; }
296 uint64_t getVal() const { return d_un.d_val; }
297 uint64_t getPtr() const { return d_un.ptr; }
300 // Elf_Rel: Elf Relocation
301 template <class ELFT, bool isRela> struct Elf_Rel_Base;
303 template <endianness TargetEndianness, std::size_t MaxAlign>
304 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, false>, false> {
305 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
306 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
307 Elf_Word r_info; // Symbol table index and type of relocation to apply
309 uint32_t getRInfo(bool isMips64EL) const {
313 void setRInfo(uint32_t R, bool IsMips64EL) {
319 template <endianness TargetEndianness, std::size_t MaxAlign>
320 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, true>, false> {
321 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
322 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
323 Elf_Xword r_info; // Symbol table index and type of relocation to apply
325 uint64_t getRInfo(bool isMips64EL) const {
329 // Mips64 little endian has a "special" encoding of r_info. Instead of one
330 // 64 bit little endian number, it is a little endian 32 bit number followed
331 // by a 32 bit big endian number.
332 return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) |
333 ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff);
335 void setRInfo(uint64_t R, bool IsMips64EL) {
337 r_info = (R >> 32) | ((R & 0xff000000) << 8) | ((R & 0x00ff0000) << 24) |
338 ((R & 0x0000ff00) << 40) | ((R & 0x000000ff) << 56);
344 template <endianness TargetEndianness, std::size_t MaxAlign>
345 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, false>, true> {
346 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
347 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
348 Elf_Word r_info; // Symbol table index and type of relocation to apply
349 Elf_Sword r_addend; // Compute value for relocatable field by adding this
351 uint32_t getRInfo(bool isMips64EL) const {
355 void setRInfo(uint32_t R, bool IsMips64EL) {
361 template <endianness TargetEndianness, std::size_t MaxAlign>
362 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, true>, true> {
363 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
364 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
365 Elf_Xword r_info; // Symbol table index and type of relocation to apply
366 Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
368 uint64_t getRInfo(bool isMips64EL) const {
369 // Mips64 little endian has a "special" encoding of r_info. Instead of one
370 // 64 bit little endian number, it is a little endian 32 bit number followed
371 // by a 32 bit big endian number.
375 return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) |
376 ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff);
378 void setRInfo(uint64_t R, bool IsMips64EL) {
380 r_info = (R >> 32) | ((R & 0xff000000) << 8) | ((R & 0x00ff0000) << 24) |
381 ((R & 0x0000ff00) << 40) | ((R & 0x000000ff) << 56);
387 template <class ELFT, bool isRela> struct Elf_Rel_Impl;
389 template <endianness TargetEndianness, std::size_t MaxAlign, bool isRela>
390 struct Elf_Rel_Impl<ELFType<TargetEndianness, MaxAlign, true>,
391 isRela> : Elf_Rel_Base<
392 ELFType<TargetEndianness, MaxAlign, true>, isRela> {
393 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
395 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
396 // and ELF64_R_INFO macros defined in the ELF specification:
397 uint32_t getSymbol(bool isMips64EL) const {
398 return (uint32_t)(this->getRInfo(isMips64EL) >> 32);
400 uint32_t getType(bool isMips64EL) const {
401 return (uint32_t)(this->getRInfo(isMips64EL) & 0xffffffffL);
403 void setSymbol(uint32_t s, bool IsMips64EL) {
404 setSymbolAndType(s, getType(), IsMips64EL);
406 void setType(uint32_t t, bool IsMips64EL) {
407 setSymbolAndType(getSymbol(), t, IsMips64EL);
409 void setSymbolAndType(uint32_t s, uint32_t t, bool IsMips64EL) {
410 this->setRInfo(((uint64_t)s << 32) + (t & 0xffffffffL), IsMips64EL);
414 template <endianness TargetEndianness, std::size_t MaxAlign, bool isRela>
415 struct Elf_Rel_Impl<ELFType<TargetEndianness, MaxAlign, false>,
416 isRela> : Elf_Rel_Base<
417 ELFType<TargetEndianness, MaxAlign, false>, isRela> {
418 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
420 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
421 // and ELF32_R_INFO macros defined in the ELF specification:
422 uint32_t getSymbol(bool isMips64EL) const {
423 return this->getRInfo(isMips64EL) >> 8;
425 unsigned char getType(bool isMips64EL) const {
426 return (unsigned char)(this->getRInfo(isMips64EL) & 0x0ff);
428 void setSymbol(uint32_t s, bool IsMips64EL) {
429 setSymbolAndType(s, getType(), IsMips64EL);
431 void setType(unsigned char t, bool IsMips64EL) {
432 setSymbolAndType(getSymbol(), t, IsMips64EL);
434 void setSymbolAndType(uint32_t s, unsigned char t, bool IsMips64EL) {
435 this->setRInfo((s << 8) + t, IsMips64EL);
439 template <class ELFT>
440 struct Elf_Ehdr_Impl {
441 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
442 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
443 Elf_Half e_type; // Type of file (see ET_*)
444 Elf_Half e_machine; // Required architecture for this file (see EM_*)
445 Elf_Word e_version; // Must be equal to 1
446 Elf_Addr e_entry; // Address to jump to in order to start program
447 Elf_Off e_phoff; // Program header table's file offset, in bytes
448 Elf_Off e_shoff; // Section header table's file offset, in bytes
449 Elf_Word e_flags; // Processor-specific flags
450 Elf_Half e_ehsize; // Size of ELF header, in bytes
451 Elf_Half e_phentsize; // Size of an entry in the program header table
452 Elf_Half e_phnum; // Number of entries in the program header table
453 Elf_Half e_shentsize; // Size of an entry in the section header table
454 Elf_Half e_shnum; // Number of entries in the section header table
455 Elf_Half e_shstrndx; // Section header table index of section name
457 bool checkMagic() const {
458 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
460 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
461 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
464 template <class ELFT> struct Elf_Phdr_Impl;
466 template <endianness TargetEndianness, std::size_t MaxAlign>
467 struct Elf_Phdr_Impl<ELFType<TargetEndianness, MaxAlign, false> > {
468 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
469 Elf_Word p_type; // Type of segment
470 Elf_Off p_offset; // FileOffset where segment is located, in bytes
471 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
472 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
473 Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
474 Elf_Word p_memsz; // Num. of bytes in mem image of segment (may be zero)
475 Elf_Word p_flags; // Segment flags
476 Elf_Word p_align; // Segment alignment constraint
479 template <endianness TargetEndianness, std::size_t MaxAlign>
480 struct Elf_Phdr_Impl<ELFType<TargetEndianness, MaxAlign, true> > {
481 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
482 Elf_Word p_type; // Type of segment
483 Elf_Word p_flags; // Segment flags
484 Elf_Off p_offset; // FileOffset where segment is located, in bytes
485 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
486 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
487 Elf_Xword p_filesz; // Num. of bytes in file image of segment (may be zero)
488 Elf_Xword p_memsz; // Num. of bytes in mem image of segment (may be zero)
489 Elf_Xword p_align; // Segment alignment constraint
492 // .MIPS.abiflags section content
493 template <class ELFT> struct Elf_Mips_ABIFlags {
494 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
495 Elf_Half version; // Version of the structure
496 uint8_t isa_level; // ISA level: 1-5, 32, and 64
497 uint8_t isa_rev; // ISA revision (0 for MIPS I - MIPS V)
498 uint8_t gpr_size; // General purpose registers size
499 uint8_t cpr1_size; // Co-processor 1 registers size
500 uint8_t cpr2_size; // Co-processor 2 registers size
501 uint8_t fp_abi; // Floating-point ABI flag
502 Elf_Word isa_ext; // Processor-specific extension
503 Elf_Word ases; // ASEs flags
504 Elf_Word flags1; // General flags
505 Elf_Word flags2; // General flags
508 } // end namespace object.
509 } // end namespace llvm.