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.
192 unsigned char getVisibility() const { return st_other & 0x3; }
195 /// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section
196 /// (.gnu.version). This structure is identical for ELF32 and ELF64.
197 template <class ELFT>
198 struct Elf_Versym_Impl {
199 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
200 Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN)
203 template <class ELFT> struct Elf_Verdaux_Impl;
205 /// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section
206 /// (.gnu.version_d). This structure is identical for ELF32 and ELF64.
207 template <class ELFT>
208 struct Elf_Verdef_Impl {
209 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
210 typedef Elf_Verdaux_Impl<ELFT> Elf_Verdaux;
211 Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT)
212 Elf_Half vd_flags; // Bitwise flags (VER_DEF_*)
213 Elf_Half vd_ndx; // Version index, used in .gnu.version entries
214 Elf_Half vd_cnt; // Number of Verdaux entries
215 Elf_Word vd_hash; // Hash of name
216 Elf_Word vd_aux; // Offset to the first Verdaux entry (in bytes)
217 Elf_Word vd_next; // Offset to the next Verdef entry (in bytes)
219 /// Get the first Verdaux entry for this Verdef.
220 const Elf_Verdaux *getAux() const {
221 return reinterpret_cast<const Elf_Verdaux *>((const char *)this + vd_aux);
225 /// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef
226 /// section (.gnu.version_d). This structure is identical for ELF32 and ELF64.
227 template <class ELFT>
228 struct Elf_Verdaux_Impl {
229 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
230 Elf_Word vda_name; // Version name (offset in string table)
231 Elf_Word vda_next; // Offset to next Verdaux entry (in bytes)
234 /// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed
235 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
236 template <class ELFT>
237 struct Elf_Verneed_Impl {
238 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
239 Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT)
240 Elf_Half vn_cnt; // Number of associated Vernaux entries
241 Elf_Word vn_file; // Library name (string table offset)
242 Elf_Word vn_aux; // Offset to first Vernaux entry (in bytes)
243 Elf_Word vn_next; // Offset to next Verneed entry (in bytes)
246 /// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed
247 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
248 template <class ELFT>
249 struct Elf_Vernaux_Impl {
250 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
251 Elf_Word vna_hash; // Hash of dependency name
252 Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*)
253 Elf_Half vna_other; // Version index, used in .gnu.version entries
254 Elf_Word vna_name; // Dependency name
255 Elf_Word vna_next; // Offset to next Vernaux entry (in bytes)
258 /// Elf_Dyn_Base: This structure matches the form of entries in the dynamic
259 /// table section (.dynamic) look like.
260 template <class ELFT> struct Elf_Dyn_Base;
262 template <endianness TargetEndianness, std::size_t MaxAlign>
263 struct Elf_Dyn_Base<ELFType<TargetEndianness, MaxAlign, false> > {
264 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
272 template <endianness TargetEndianness, std::size_t MaxAlign>
273 struct Elf_Dyn_Base<ELFType<TargetEndianness, MaxAlign, true> > {
274 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
282 /// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters.
283 template <class ELFT>
284 struct Elf_Dyn_Impl : Elf_Dyn_Base<ELFT> {
285 using Elf_Dyn_Base<ELFT>::d_tag;
286 using Elf_Dyn_Base<ELFT>::d_un;
287 int64_t getTag() const { return d_tag; }
288 uint64_t getVal() const { return d_un.d_val; }
289 uint64_t getPtr() const { return d_un.ptr; }
292 // Elf_Rel: Elf Relocation
293 template <class ELFT, bool isRela> struct Elf_Rel_Base;
295 template <endianness TargetEndianness, std::size_t MaxAlign>
296 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, false>, false> {
297 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
298 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
299 Elf_Word r_info; // Symbol table index and type of relocation to apply
301 uint32_t getRInfo(bool isMips64EL) const {
305 void setRInfo(uint32_t R, bool IsMips64EL) {
311 template <endianness TargetEndianness, std::size_t MaxAlign>
312 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, true>, false> {
313 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
314 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
315 Elf_Xword r_info; // Symbol table index and type of relocation to apply
317 uint64_t getRInfo(bool isMips64EL) const {
321 // Mips64 little endian has a "special" encoding of r_info. Instead of one
322 // 64 bit little endian number, it is a little endian 32 bit number followed
323 // by a 32 bit big endian number.
324 return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) |
325 ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff);
327 void setRInfo(uint64_t R, bool IsMips64EL) {
329 r_info = (R >> 32) | ((R & 0xff000000) << 8) | ((R & 0x00ff0000) << 24) |
330 ((R & 0x0000ff00) << 40) | ((R & 0x000000ff) << 56);
336 template <endianness TargetEndianness, std::size_t MaxAlign>
337 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, false>, true> {
338 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
339 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
340 Elf_Word r_info; // Symbol table index and type of relocation to apply
341 Elf_Sword r_addend; // Compute value for relocatable field by adding this
343 uint32_t getRInfo(bool isMips64EL) const {
347 void setRInfo(uint32_t R, bool IsMips64EL) {
353 template <endianness TargetEndianness, std::size_t MaxAlign>
354 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, true>, true> {
355 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
356 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
357 Elf_Xword r_info; // Symbol table index and type of relocation to apply
358 Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
360 uint64_t getRInfo(bool isMips64EL) const {
361 // Mips64 little endian has a "special" encoding of r_info. Instead of one
362 // 64 bit little endian number, it is a little endian 32 bit number followed
363 // by a 32 bit big endian number.
367 return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) |
368 ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff);
370 void setRInfo(uint64_t R, bool IsMips64EL) {
372 r_info = (R >> 32) | ((R & 0xff000000) << 8) | ((R & 0x00ff0000) << 24) |
373 ((R & 0x0000ff00) << 40) | ((R & 0x000000ff) << 56);
379 template <class ELFT, bool isRela> struct Elf_Rel_Impl;
381 template <endianness TargetEndianness, std::size_t MaxAlign, bool isRela>
382 struct Elf_Rel_Impl<ELFType<TargetEndianness, MaxAlign, true>,
383 isRela> : Elf_Rel_Base<
384 ELFType<TargetEndianness, MaxAlign, true>, isRela> {
385 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
387 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
388 // and ELF64_R_INFO macros defined in the ELF specification:
389 uint32_t getSymbol(bool isMips64EL) const {
390 return (uint32_t)(this->getRInfo(isMips64EL) >> 32);
392 uint32_t getType(bool isMips64EL) const {
393 return (uint32_t)(this->getRInfo(isMips64EL) & 0xffffffffL);
395 void setSymbol(uint32_t s, bool IsMips64EL) {
396 setSymbolAndType(s, getType(), IsMips64EL);
398 void setType(uint32_t t, bool IsMips64EL) {
399 setSymbolAndType(getSymbol(), t, IsMips64EL);
401 void setSymbolAndType(uint32_t s, uint32_t t, bool IsMips64EL) {
402 this->setRInfo(((uint64_t)s << 32) + (t & 0xffffffffL), IsMips64EL);
406 template <endianness TargetEndianness, std::size_t MaxAlign, bool isRela>
407 struct Elf_Rel_Impl<ELFType<TargetEndianness, MaxAlign, false>,
408 isRela> : Elf_Rel_Base<
409 ELFType<TargetEndianness, MaxAlign, false>, isRela> {
410 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
412 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
413 // and ELF32_R_INFO macros defined in the ELF specification:
414 uint32_t getSymbol(bool isMips64EL) const {
415 return this->getRInfo(isMips64EL) >> 8;
417 unsigned char getType(bool isMips64EL) const {
418 return (unsigned char)(this->getRInfo(isMips64EL) & 0x0ff);
420 void setSymbol(uint32_t s, bool IsMips64EL) {
421 setSymbolAndType(s, getType(), IsMips64EL);
423 void setType(unsigned char t, bool IsMips64EL) {
424 setSymbolAndType(getSymbol(), t, IsMips64EL);
426 void setSymbolAndType(uint32_t s, unsigned char t, bool IsMips64EL) {
427 this->setRInfo((s << 8) + t, IsMips64EL);
431 template <class ELFT>
432 struct Elf_Ehdr_Impl {
433 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
434 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
435 Elf_Half e_type; // Type of file (see ET_*)
436 Elf_Half e_machine; // Required architecture for this file (see EM_*)
437 Elf_Word e_version; // Must be equal to 1
438 Elf_Addr e_entry; // Address to jump to in order to start program
439 Elf_Off e_phoff; // Program header table's file offset, in bytes
440 Elf_Off e_shoff; // Section header table's file offset, in bytes
441 Elf_Word e_flags; // Processor-specific flags
442 Elf_Half e_ehsize; // Size of ELF header, in bytes
443 Elf_Half e_phentsize; // Size of an entry in the program header table
444 Elf_Half e_phnum; // Number of entries in the program header table
445 Elf_Half e_shentsize; // Size of an entry in the section header table
446 Elf_Half e_shnum; // Number of entries in the section header table
447 Elf_Half e_shstrndx; // Section header table index of section name
449 bool checkMagic() const {
450 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
452 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
453 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
456 template <class ELFT> struct Elf_Phdr_Impl;
458 template <endianness TargetEndianness, std::size_t MaxAlign>
459 struct Elf_Phdr_Impl<ELFType<TargetEndianness, MaxAlign, false> > {
460 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
461 Elf_Word p_type; // Type of segment
462 Elf_Off p_offset; // FileOffset where segment is located, in bytes
463 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
464 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
465 Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
466 Elf_Word p_memsz; // Num. of bytes in mem image of segment (may be zero)
467 Elf_Word p_flags; // Segment flags
468 Elf_Word p_align; // Segment alignment constraint
471 template <endianness TargetEndianness, std::size_t MaxAlign>
472 struct Elf_Phdr_Impl<ELFType<TargetEndianness, MaxAlign, true> > {
473 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
474 Elf_Word p_type; // Type of segment
475 Elf_Word p_flags; // Segment flags
476 Elf_Off p_offset; // FileOffset where segment is located, in bytes
477 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
478 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
479 Elf_Xword p_filesz; // Num. of bytes in file image of segment (may be zero)
480 Elf_Xword p_memsz; // Num. of bytes in mem image of segment (may be zero)
481 Elf_Xword p_align; // Segment alignment constraint
484 } // end namespace object.
485 } // end namespace llvm.