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_ELF_TYPES_H
11 #define LLVM_OBJECT_ELF_TYPES_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;
180 // These accessors and mutators correspond to the ELF32_ST_BIND,
181 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
182 unsigned char getBinding() const { return st_info >> 4; }
183 unsigned char getType() const { return st_info & 0x0f; }
184 void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
185 void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
186 void setBindingAndType(unsigned char b, unsigned char t) {
187 st_info = (b << 4) + (t & 0x0f);
191 /// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section
192 /// (.gnu.version). This structure is identical for ELF32 and ELF64.
193 template <class ELFT>
194 struct Elf_Versym_Impl {
195 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
196 Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN)
199 template <class ELFT> struct Elf_Verdaux_Impl;
201 /// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section
202 /// (.gnu.version_d). This structure is identical for ELF32 and ELF64.
203 template <class ELFT>
204 struct Elf_Verdef_Impl {
205 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
206 typedef Elf_Verdaux_Impl<ELFT> Elf_Verdaux;
207 Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT)
208 Elf_Half vd_flags; // Bitwise flags (VER_DEF_*)
209 Elf_Half vd_ndx; // Version index, used in .gnu.version entries
210 Elf_Half vd_cnt; // Number of Verdaux entries
211 Elf_Word vd_hash; // Hash of name
212 Elf_Word vd_aux; // Offset to the first Verdaux entry (in bytes)
213 Elf_Word vd_next; // Offset to the next Verdef entry (in bytes)
215 /// Get the first Verdaux entry for this Verdef.
216 const Elf_Verdaux *getAux() const {
217 return reinterpret_cast<const Elf_Verdaux *>((const char *)this + vd_aux);
221 /// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef
222 /// section (.gnu.version_d). This structure is identical for ELF32 and ELF64.
223 template <class ELFT>
224 struct Elf_Verdaux_Impl {
225 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
226 Elf_Word vda_name; // Version name (offset in string table)
227 Elf_Word vda_next; // Offset to next Verdaux entry (in bytes)
230 /// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed
231 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
232 template <class ELFT>
233 struct Elf_Verneed_Impl {
234 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
235 Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT)
236 Elf_Half vn_cnt; // Number of associated Vernaux entries
237 Elf_Word vn_file; // Library name (string table offset)
238 Elf_Word vn_aux; // Offset to first Vernaux entry (in bytes)
239 Elf_Word vn_next; // Offset to next Verneed entry (in bytes)
242 /// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed
243 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
244 template <class ELFT>
245 struct Elf_Vernaux_Impl {
246 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
247 Elf_Word vna_hash; // Hash of dependency name
248 Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*)
249 Elf_Half vna_other; // Version index, used in .gnu.version entries
250 Elf_Word vna_name; // Dependency name
251 Elf_Word vna_next; // Offset to next Vernaux entry (in bytes)
254 /// Elf_Dyn_Base: This structure matches the form of entries in the dynamic
255 /// table section (.dynamic) look like.
256 template <class ELFT> struct Elf_Dyn_Base;
258 template <endianness TargetEndianness, std::size_t MaxAlign>
259 struct Elf_Dyn_Base<ELFType<TargetEndianness, MaxAlign, false> > {
260 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
268 template <endianness TargetEndianness, std::size_t MaxAlign>
269 struct Elf_Dyn_Base<ELFType<TargetEndianness, MaxAlign, true> > {
270 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
278 /// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters.
279 template <class ELFT>
280 struct Elf_Dyn_Impl : Elf_Dyn_Base<ELFT> {
281 using Elf_Dyn_Base<ELFT>::d_tag;
282 using Elf_Dyn_Base<ELFT>::d_un;
283 int64_t getTag() const { return d_tag; }
284 uint64_t getVal() const { return d_un.d_val; }
285 uint64_t getPtr() const { return d_un.ptr; }
288 // Elf_Rel: Elf Relocation
289 template <class ELFT, bool isRela> struct Elf_Rel_Base;
291 template <endianness TargetEndianness, std::size_t MaxAlign>
292 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, false>, false> {
293 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
294 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
295 Elf_Word r_info; // Symbol table index and type of relocation to apply
297 uint32_t getRInfo(bool isMips64EL) const {
301 void setRInfo(uint32_t R) { r_info = R; }
304 template <endianness TargetEndianness, std::size_t MaxAlign>
305 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, true>, false> {
306 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
307 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
308 Elf_Xword r_info; // Symbol table index and type of relocation to apply
310 uint64_t getRInfo(bool isMips64EL) const {
314 // Mips64 little endian has a "special" encoding of r_info. Instead of one
315 // 64 bit little endian number, it is a little endian 32 bit number followed
316 // by a 32 bit big endian number.
317 return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) |
318 ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff);
320 void setRInfo(uint64_t R) {
321 // FIXME: Add mips64el support.
326 template <endianness TargetEndianness, std::size_t MaxAlign>
327 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, false>, true> {
328 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
329 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
330 Elf_Word r_info; // Symbol table index and type of relocation to apply
331 Elf_Sword r_addend; // Compute value for relocatable field by adding this
333 uint32_t getRInfo(bool isMips64EL) const {
337 void setRInfo(uint32_t R) { r_info = R; }
340 template <endianness TargetEndianness, std::size_t MaxAlign>
341 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, true>, true> {
342 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
343 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
344 Elf_Xword r_info; // Symbol table index and type of relocation to apply
345 Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
347 uint64_t getRInfo(bool isMips64EL) const {
348 // Mips64 little endian has a "special" encoding of r_info. Instead of one
349 // 64 bit little endian number, it is a little endian 32 bit number followed
350 // by a 32 bit big endian number.
354 return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) |
355 ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff);
357 void setRInfo(uint64_t R) {
358 // FIXME: Add mips64el support.
363 template <class ELFT, bool isRela> struct Elf_Rel_Impl;
365 template <endianness TargetEndianness, std::size_t MaxAlign, bool isRela>
366 struct Elf_Rel_Impl<ELFType<TargetEndianness, MaxAlign, true>,
367 isRela> : Elf_Rel_Base<
368 ELFType<TargetEndianness, MaxAlign, true>, isRela> {
369 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
371 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
372 // and ELF64_R_INFO macros defined in the ELF specification:
373 uint32_t getSymbol(bool isMips64EL) const {
374 return (uint32_t)(this->getRInfo(isMips64EL) >> 32);
376 uint32_t getType(bool isMips64EL) const {
377 return (uint32_t)(this->getRInfo(isMips64EL) & 0xffffffffL);
379 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); }
380 void setType(uint32_t t) { setSymbolAndType(getSymbol(), t); }
381 void setSymbolAndType(uint32_t s, uint32_t t) {
382 this->setRInfo(((uint64_t)s << 32) + (t & 0xffffffffL));
386 template <endianness TargetEndianness, std::size_t MaxAlign, bool isRela>
387 struct Elf_Rel_Impl<ELFType<TargetEndianness, MaxAlign, false>,
388 isRela> : Elf_Rel_Base<
389 ELFType<TargetEndianness, MaxAlign, false>, isRela> {
390 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
392 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
393 // and ELF32_R_INFO macros defined in the ELF specification:
394 uint32_t getSymbol(bool isMips64EL) const {
395 return this->getRInfo(isMips64EL) >> 8;
397 unsigned char getType(bool isMips64EL) const {
398 return (unsigned char)(this->getRInfo(isMips64EL) & 0x0ff);
400 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); }
401 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
402 void setSymbolAndType(uint32_t s, unsigned char t) {
403 this->setRInfo((s << 8) + t);
407 template <class ELFT>
408 struct Elf_Ehdr_Impl {
409 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
410 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
411 Elf_Half e_type; // Type of file (see ET_*)
412 Elf_Half e_machine; // Required architecture for this file (see EM_*)
413 Elf_Word e_version; // Must be equal to 1
414 Elf_Addr e_entry; // Address to jump to in order to start program
415 Elf_Off e_phoff; // Program header table's file offset, in bytes
416 Elf_Off e_shoff; // Section header table's file offset, in bytes
417 Elf_Word e_flags; // Processor-specific flags
418 Elf_Half e_ehsize; // Size of ELF header, in bytes
419 Elf_Half e_phentsize; // Size of an entry in the program header table
420 Elf_Half e_phnum; // Number of entries in the program header table
421 Elf_Half e_shentsize; // Size of an entry in the section header table
422 Elf_Half e_shnum; // Number of entries in the section header table
423 Elf_Half e_shstrndx; // Section header table index of section name
425 bool checkMagic() const {
426 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
428 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
429 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
432 template <class ELFT> struct Elf_Phdr_Impl;
434 template <endianness TargetEndianness, std::size_t MaxAlign>
435 struct Elf_Phdr_Impl<ELFType<TargetEndianness, MaxAlign, false> > {
436 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false)
437 Elf_Word p_type; // Type of segment
438 Elf_Off p_offset; // FileOffset where segment is located, in bytes
439 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
440 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
441 Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
442 Elf_Word p_memsz; // Num. of bytes in mem image of segment (may be zero)
443 Elf_Word p_flags; // Segment flags
444 Elf_Word p_align; // Segment alignment constraint
447 template <endianness TargetEndianness, std::size_t MaxAlign>
448 struct Elf_Phdr_Impl<ELFType<TargetEndianness, MaxAlign, true> > {
449 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true)
450 Elf_Word p_type; // Type of segment
451 Elf_Word p_flags; // Segment flags
452 Elf_Off p_offset; // FileOffset where segment is located, in bytes
453 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
454 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
455 Elf_Xword p_filesz; // Num. of bytes in file image of segment (may be zero)
456 Elf_Xword p_memsz; // Num. of bytes in mem image of segment (may be zero)
457 Elf_Xword p_align; // Segment alignment constraint
460 } // end namespace object.
461 } // end namespace llvm.