1 //===- ELF.h - ELF object file implementation -------------------*- 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 // This file declares the ELFObjectFile template class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_OBJECT_ELF_H
15 #define LLVM_OBJECT_ELF_H
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/PointerIntPair.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/Support/Casting.h"
24 #include "llvm/Support/ELF.h"
25 #include "llvm/Support/Endian.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Support/raw_ostream.h"
36 using support::endianness;
38 template<typename T, int max_align>
39 struct MaximumAlignment {
40 enum {value = AlignOf<T>::Alignment > max_align ? max_align
41 : AlignOf<T>::Alignment};
44 // Subclasses of ELFObjectFile may need this for template instantiation
45 inline std::pair<unsigned char, unsigned char>
46 getElfArchType(MemoryBuffer *Object) {
47 if (Object->getBufferSize() < ELF::EI_NIDENT)
48 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE);
49 return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS]
50 , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]);
53 // Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
54 template<endianness target_endianness, std::size_t max_alignment>
55 struct ELFDataTypeTypedefHelperCommon {
56 typedef support::detail::packed_endian_specific_integral
57 <uint16_t, target_endianness,
58 MaximumAlignment<uint16_t, max_alignment>::value> Elf_Half;
59 typedef support::detail::packed_endian_specific_integral
60 <uint32_t, target_endianness,
61 MaximumAlignment<uint32_t, max_alignment>::value> Elf_Word;
62 typedef support::detail::packed_endian_specific_integral
63 <int32_t, target_endianness,
64 MaximumAlignment<int32_t, max_alignment>::value> Elf_Sword;
65 typedef support::detail::packed_endian_specific_integral
66 <uint64_t, target_endianness,
67 MaximumAlignment<uint64_t, max_alignment>::value> Elf_Xword;
68 typedef support::detail::packed_endian_specific_integral
69 <int64_t, target_endianness,
70 MaximumAlignment<int64_t, max_alignment>::value> Elf_Sxword;
73 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
74 struct ELFDataTypeTypedefHelper;
77 template<endianness target_endianness, std::size_t max_alignment>
78 struct ELFDataTypeTypedefHelper<target_endianness, max_alignment, false>
79 : ELFDataTypeTypedefHelperCommon<target_endianness, max_alignment> {
80 typedef uint32_t value_type;
81 typedef support::detail::packed_endian_specific_integral
82 <value_type, target_endianness,
83 MaximumAlignment<value_type, max_alignment>::value> Elf_Addr;
84 typedef support::detail::packed_endian_specific_integral
85 <value_type, target_endianness,
86 MaximumAlignment<value_type, max_alignment>::value> Elf_Off;
90 template<endianness target_endianness, std::size_t max_alignment>
91 struct ELFDataTypeTypedefHelper<target_endianness, max_alignment, true>
92 : ELFDataTypeTypedefHelperCommon<target_endianness, max_alignment>{
93 typedef uint64_t value_type;
94 typedef support::detail::packed_endian_specific_integral
95 <value_type, target_endianness,
96 MaximumAlignment<value_type, max_alignment>::value> Elf_Addr;
97 typedef support::detail::packed_endian_specific_integral
98 <value_type, target_endianness,
99 MaximumAlignment<value_type, max_alignment>::value> Elf_Off;
102 // I really don't like doing this, but the alternative is copypasta.
103 #define LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits) \
104 typedef typename ELFDataTypeTypedefHelper \
105 <target_endianness, max_alignment, is64Bits>::Elf_Addr Elf_Addr; \
106 typedef typename ELFDataTypeTypedefHelper \
107 <target_endianness, max_alignment, is64Bits>::Elf_Off Elf_Off; \
108 typedef typename ELFDataTypeTypedefHelper \
109 <target_endianness, max_alignment, is64Bits>::Elf_Half Elf_Half; \
110 typedef typename ELFDataTypeTypedefHelper \
111 <target_endianness, max_alignment, is64Bits>::Elf_Word Elf_Word; \
112 typedef typename ELFDataTypeTypedefHelper \
113 <target_endianness, max_alignment, is64Bits>::Elf_Sword Elf_Sword; \
114 typedef typename ELFDataTypeTypedefHelper \
115 <target_endianness, max_alignment, is64Bits>::Elf_Xword Elf_Xword; \
116 typedef typename ELFDataTypeTypedefHelper \
117 <target_endianness, max_alignment, is64Bits>::Elf_Sxword Elf_Sxword;
120 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
121 struct Elf_Shdr_Base;
123 template<endianness target_endianness, std::size_t max_alignment>
124 struct Elf_Shdr_Base<target_endianness, max_alignment, false> {
125 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
126 Elf_Word sh_name; // Section name (index into string table)
127 Elf_Word sh_type; // Section type (SHT_*)
128 Elf_Word sh_flags; // Section flags (SHF_*)
129 Elf_Addr sh_addr; // Address where section is to be loaded
130 Elf_Off sh_offset; // File offset of section data, in bytes
131 Elf_Word sh_size; // Size of section, in bytes
132 Elf_Word sh_link; // Section type-specific header table index link
133 Elf_Word sh_info; // Section type-specific extra information
134 Elf_Word sh_addralign;// Section address alignment
135 Elf_Word sh_entsize; // Size of records contained within the section
138 template<endianness target_endianness, std::size_t max_alignment>
139 struct Elf_Shdr_Base<target_endianness, max_alignment, true> {
140 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
141 Elf_Word sh_name; // Section name (index into string table)
142 Elf_Word sh_type; // Section type (SHT_*)
143 Elf_Xword sh_flags; // Section flags (SHF_*)
144 Elf_Addr sh_addr; // Address where section is to be loaded
145 Elf_Off sh_offset; // File offset of section data, in bytes
146 Elf_Xword sh_size; // Size of section, in bytes
147 Elf_Word sh_link; // Section type-specific header table index link
148 Elf_Word sh_info; // Section type-specific extra information
149 Elf_Xword sh_addralign;// Section address alignment
150 Elf_Xword sh_entsize; // Size of records contained within the section
153 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
155 : Elf_Shdr_Base<target_endianness, max_alignment, is64Bits> {
156 using Elf_Shdr_Base<target_endianness, max_alignment, is64Bits>::sh_entsize;
157 using Elf_Shdr_Base<target_endianness, max_alignment, is64Bits>::sh_size;
159 /// @brief Get the number of entities this section contains if it has any.
160 unsigned getEntityCount() const {
163 return sh_size / sh_entsize;
167 template< endianness target_endianness
168 , std::size_t max_alignment
172 template<endianness target_endianness, std::size_t max_alignment>
173 struct Elf_Sym_Base<target_endianness, max_alignment, false> {
174 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
175 Elf_Word st_name; // Symbol name (index into string table)
176 Elf_Addr st_value; // Value or address associated with the symbol
177 Elf_Word st_size; // Size of the symbol
178 unsigned char st_info; // Symbol's type and binding attributes
179 unsigned char st_other; // Must be zero; reserved
180 Elf_Half st_shndx; // Which section (header table index) it's defined in
183 template<endianness target_endianness, std::size_t max_alignment>
184 struct Elf_Sym_Base<target_endianness, max_alignment, true> {
185 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
186 Elf_Word st_name; // Symbol name (index into string table)
187 unsigned char st_info; // Symbol's type and binding attributes
188 unsigned char st_other; // Must be zero; reserved
189 Elf_Half st_shndx; // Which section (header table index) it's defined in
190 Elf_Addr st_value; // Value or address associated with the symbol
191 Elf_Xword st_size; // Size of the symbol
194 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
196 : Elf_Sym_Base<target_endianness, max_alignment, is64Bits> {
197 using Elf_Sym_Base<target_endianness, max_alignment, is64Bits>::st_info;
199 // These accessors and mutators correspond to the ELF32_ST_BIND,
200 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
201 unsigned char getBinding() const { return st_info >> 4; }
202 unsigned char getType() const { return st_info & 0x0f; }
203 void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
204 void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
205 void setBindingAndType(unsigned char b, unsigned char t) {
206 st_info = (b << 4) + (t & 0x0f);
210 /// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section
211 /// (.gnu.version). This structure is identical for ELF32 and ELF64.
212 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
213 struct Elf_Versym_Impl {
214 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
215 Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN)
218 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
219 struct Elf_Verdaux_Impl;
221 /// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section
222 /// (.gnu.version_d). This structure is identical for ELF32 and ELF64.
223 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
224 struct Elf_Verdef_Impl {
225 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
227 Elf_Verdaux_Impl<target_endianness, max_alignment, is64Bits> Elf_Verdaux;
228 Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT)
229 Elf_Half vd_flags; // Bitwise flags (VER_DEF_*)
230 Elf_Half vd_ndx; // Version index, used in .gnu.version entries
231 Elf_Half vd_cnt; // Number of Verdaux entries
232 Elf_Word vd_hash; // Hash of name
233 Elf_Word vd_aux; // Offset to the first Verdaux entry (in bytes)
234 Elf_Word vd_next; // Offset to the next Verdef entry (in bytes)
236 /// Get the first Verdaux entry for this Verdef.
237 const Elf_Verdaux *getAux() const {
238 return reinterpret_cast<const Elf_Verdaux*>((const char*)this + vd_aux);
242 /// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef
243 /// section (.gnu.version_d). This structure is identical for ELF32 and ELF64.
244 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
245 struct Elf_Verdaux_Impl {
246 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
247 Elf_Word vda_name; // Version name (offset in string table)
248 Elf_Word vda_next; // Offset to next Verdaux entry (in bytes)
251 /// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed
252 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
253 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
254 struct Elf_Verneed_Impl {
255 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
256 Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT)
257 Elf_Half vn_cnt; // Number of associated Vernaux entries
258 Elf_Word vn_file; // Library name (string table offset)
259 Elf_Word vn_aux; // Offset to first Vernaux entry (in bytes)
260 Elf_Word vn_next; // Offset to next Verneed entry (in bytes)
263 /// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed
264 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
265 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
266 struct Elf_Vernaux_Impl {
267 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
268 Elf_Word vna_hash; // Hash of dependency name
269 Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*)
270 Elf_Half vna_other; // Version index, used in .gnu.version entries
271 Elf_Word vna_name; // Dependency name
272 Elf_Word vna_next; // Offset to next Vernaux entry (in bytes)
275 /// Elf_Dyn_Base: This structure matches the form of entries in the dynamic
276 /// table section (.dynamic) look like.
277 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
280 template<endianness target_endianness, std::size_t max_alignment>
281 struct Elf_Dyn_Base<target_endianness, max_alignment, false> {
282 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
290 template<endianness target_endianness, std::size_t max_alignment>
291 struct Elf_Dyn_Base<target_endianness, max_alignment, true> {
292 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
300 /// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters.
301 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
302 struct Elf_Dyn_Impl : Elf_Dyn_Base<target_endianness, max_alignment, is64Bits> {
303 using Elf_Dyn_Base<target_endianness, max_alignment, is64Bits>::d_tag;
304 using Elf_Dyn_Base<target_endianness, max_alignment, is64Bits>::d_un;
305 int64_t getTag() const { return d_tag; }
306 uint64_t getVal() const { return d_un.d_val; }
307 uint64_t getPtr() const { return d_un.ptr; }
310 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
313 // DynRefImpl: Reference to an entry in the dynamic table
314 // This is an ELF-specific interface.
315 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
317 typedef Elf_Dyn_Impl<target_endianness, max_alignment, is64Bits> Elf_Dyn;
318 typedef ELFObjectFile<target_endianness, max_alignment, is64Bits> OwningType;
320 DataRefImpl DynPimpl;
321 const OwningType *OwningObject;
324 DynRefImpl() : OwningObject(NULL) { }
326 DynRefImpl(DataRefImpl DynP, const OwningType *Owner);
328 bool operator==(const DynRefImpl &Other) const;
329 bool operator <(const DynRefImpl &Other) const;
331 error_code getNext(DynRefImpl &Result) const;
332 int64_t getTag() const;
333 uint64_t getVal() const;
334 uint64_t getPtr() const;
336 DataRefImpl getRawDataRefImpl() const;
339 // Elf_Rel: Elf Relocation
340 template< endianness target_endianness
341 , std::size_t max_alignment
346 template<endianness target_endianness, std::size_t max_alignment>
347 struct Elf_Rel_Base<target_endianness, max_alignment, false, false> {
348 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
349 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
350 Elf_Word r_info; // Symbol table index and type of relocation to apply
353 template<endianness target_endianness, std::size_t max_alignment>
354 struct Elf_Rel_Base<target_endianness, max_alignment, true, false> {
355 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, 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
360 template<endianness target_endianness, std::size_t max_alignment>
361 struct Elf_Rel_Base<target_endianness, max_alignment, false, true> {
362 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
363 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
364 Elf_Word r_info; // Symbol table index and type of relocation to apply
365 Elf_Sword r_addend; // Compute value for relocatable field by adding this
368 template<endianness target_endianness, std::size_t max_alignment>
369 struct Elf_Rel_Base<target_endianness, max_alignment, true, true> {
370 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
371 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
372 Elf_Xword r_info; // Symbol table index and type of relocation to apply
373 Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
376 template< endianness target_endianness
377 , std::size_t max_alignment
382 template<endianness target_endianness, std::size_t max_alignment, bool isRela>
383 struct Elf_Rel_Impl<target_endianness, max_alignment, true, isRela>
384 : Elf_Rel_Base<target_endianness, max_alignment, true, isRela> {
385 using Elf_Rel_Base<target_endianness, max_alignment, true, isRela>::r_info;
386 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
388 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
389 // and ELF64_R_INFO macros defined in the ELF specification:
390 uint64_t getSymbol() const { return (r_info >> 32); }
391 unsigned char getType() const {
392 return (unsigned char) (r_info & 0xffffffffL);
394 void setSymbol(uint64_t s) { setSymbolAndType(s, getType()); }
395 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
396 void setSymbolAndType(uint64_t s, unsigned char t) {
397 r_info = (s << 32) + (t&0xffffffffL);
401 template<endianness target_endianness, std::size_t max_alignment, bool isRela>
402 struct Elf_Rel_Impl<target_endianness, max_alignment, false, isRela>
403 : Elf_Rel_Base<target_endianness, max_alignment, false, isRela> {
404 using Elf_Rel_Base<target_endianness, max_alignment, false, isRela>::r_info;
405 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
407 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
408 // and ELF32_R_INFO macros defined in the ELF specification:
409 uint32_t getSymbol() const { return (r_info >> 8); }
410 unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); }
411 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); }
412 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
413 void setSymbolAndType(uint32_t s, unsigned char t) {
414 r_info = (s << 8) + t;
418 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
419 struct Elf_Ehdr_Impl {
420 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
421 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
422 Elf_Half e_type; // Type of file (see ET_*)
423 Elf_Half e_machine; // Required architecture for this file (see EM_*)
424 Elf_Word e_version; // Must be equal to 1
425 Elf_Addr e_entry; // Address to jump to in order to start program
426 Elf_Off e_phoff; // Program header table's file offset, in bytes
427 Elf_Off e_shoff; // Section header table's file offset, in bytes
428 Elf_Word e_flags; // Processor-specific flags
429 Elf_Half e_ehsize; // Size of ELF header, in bytes
430 Elf_Half e_phentsize;// Size of an entry in the program header table
431 Elf_Half e_phnum; // Number of entries in the program header table
432 Elf_Half e_shentsize;// Size of an entry in the section header table
433 Elf_Half e_shnum; // Number of entries in the section header table
434 Elf_Half e_shstrndx; // Section header table index of section name
436 bool checkMagic() const {
437 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
439 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
440 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
443 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
446 template<endianness target_endianness, std::size_t max_alignment>
447 struct Elf_Phdr<target_endianness, max_alignment, false> {
448 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
449 Elf_Word p_type; // Type of segment
450 Elf_Off p_offset; // FileOffset where segment is located, in bytes
451 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
452 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
453 Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
454 Elf_Word p_memsz; // Num. of bytes in mem image of segment (may be zero)
455 Elf_Word p_flags; // Segment flags
456 Elf_Word p_align; // Segment alignment constraint
459 template<endianness target_endianness, std::size_t max_alignment>
460 struct Elf_Phdr<target_endianness, max_alignment, true> {
461 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
462 Elf_Word p_type; // Type of segment
463 Elf_Word p_flags; // Segment flags
464 Elf_Off p_offset; // FileOffset where segment is located, in bytes
465 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
466 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
467 Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
468 Elf_Word p_memsz; // Num. of bytes in mem image of segment (may be zero)
469 Elf_Word p_align; // Segment alignment constraint
472 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
473 class ELFObjectFile : public ObjectFile {
474 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
476 typedef Elf_Ehdr_Impl<target_endianness, max_alignment, is64Bits> Elf_Ehdr;
477 typedef Elf_Shdr_Impl<target_endianness, max_alignment, is64Bits> Elf_Shdr;
478 typedef Elf_Sym_Impl<target_endianness, max_alignment, is64Bits> Elf_Sym;
479 typedef Elf_Dyn_Impl<target_endianness, max_alignment, is64Bits> Elf_Dyn;
481 Elf_Rel_Impl<target_endianness, max_alignment, is64Bits, false> Elf_Rel;
483 Elf_Rel_Impl<target_endianness, max_alignment, is64Bits, true> Elf_Rela;
485 Elf_Verdef_Impl<target_endianness, max_alignment, is64Bits> Elf_Verdef;
487 Elf_Verdaux_Impl<target_endianness, max_alignment, is64Bits> Elf_Verdaux;
489 Elf_Verneed_Impl<target_endianness, max_alignment, is64Bits> Elf_Verneed;
491 Elf_Vernaux_Impl<target_endianness, max_alignment, is64Bits> Elf_Vernaux;
493 Elf_Versym_Impl<target_endianness, max_alignment, is64Bits> Elf_Versym;
494 typedef DynRefImpl<target_endianness, max_alignment, is64Bits> DynRef;
495 typedef content_iterator<DynRef> dyn_iterator;
498 // This flag is used for classof, to distinguish ELFObjectFile from
499 // its subclass. If more subclasses will be created, this flag will
500 // have to become an enum.
501 bool isDyldELFObject;
504 typedef SmallVector<const Elf_Shdr*, 1> Sections_t;
505 typedef DenseMap<unsigned, unsigned> IndexMap_t;
506 typedef DenseMap<const Elf_Shdr*, SmallVector<uint32_t, 1> > RelocMap_t;
508 const Elf_Ehdr *Header;
509 const Elf_Shdr *SectionHeaderTable;
510 const Elf_Shdr *dot_shstrtab_sec; // Section header string table.
511 const Elf_Shdr *dot_strtab_sec; // Symbol header string table.
512 const Elf_Shdr *dot_dynstr_sec; // Dynamic symbol string table.
514 // SymbolTableSections[0] always points to the dynamic string table section
515 // header, or NULL if there is no dynamic string table.
516 Sections_t SymbolTableSections;
517 IndexMap_t SymbolTableSectionsIndexMap;
518 DenseMap<const Elf_Sym*, ELF::Elf64_Word> ExtendedSymbolTable;
520 const Elf_Shdr *dot_dynamic_sec; // .dynamic
521 const Elf_Shdr *dot_gnu_version_sec; // .gnu.version
522 const Elf_Shdr *dot_gnu_version_r_sec; // .gnu.version_r
523 const Elf_Shdr *dot_gnu_version_d_sec; // .gnu.version_d
525 // Pointer to SONAME entry in dynamic string table
526 // This is set the first time getLoadName is called.
527 mutable const char *dt_soname;
530 /// \brief Iterate over constant sized entities.
532 class ELFEntityIterator {
534 typedef void difference_type;
535 typedef EntT value_type;
536 typedef std::forward_iterator_tag iterator_category;
537 typedef value_type &reference;
538 typedef value_type *pointer;
540 /// \brief Default construct iterator.
541 ELFEntityIterator() : EntitySize(0), Current(0) {}
542 ELFEntityIterator(uint64_t EntSize, const char *Start)
543 : EntitySize(EntSize)
546 reference operator *() {
547 assert(Current && "Attempted to dereference an invalid iterator!");
548 return *reinterpret_cast<pointer>(Current);
551 pointer operator ->() {
552 assert(Current && "Attempted to dereference an invalid iterator!");
553 return reinterpret_cast<pointer>(Current);
556 bool operator ==(const ELFEntityIterator &Other) {
557 return Current == Other.Current;
560 bool operator !=(const ELFEntityIterator &Other) {
561 return !(*this == Other);
564 ELFEntityIterator &operator ++() {
565 assert(Current && "Attempted to increment an invalid iterator!");
566 Current += EntitySize;
570 ELFEntityIterator operator ++(int) {
571 ELFEntityIterator Tmp = *this;
577 const uint64_t EntitySize;
582 // Records for each version index the corresponding Verdef or Vernaux entry.
583 // This is filled the first time LoadVersionMap() is called.
584 class VersionMapEntry : public PointerIntPair<const void*, 1> {
586 // If the integer is 0, this is an Elf_Verdef*.
587 // If the integer is 1, this is an Elf_Vernaux*.
588 VersionMapEntry() : PointerIntPair<const void*, 1>(NULL, 0) { }
589 VersionMapEntry(const Elf_Verdef *verdef)
590 : PointerIntPair<const void*, 1>(verdef, 0) { }
591 VersionMapEntry(const Elf_Vernaux *vernaux)
592 : PointerIntPair<const void*, 1>(vernaux, 1) { }
593 bool isNull() const { return getPointer() == NULL; }
594 bool isVerdef() const { return !isNull() && getInt() == 0; }
595 bool isVernaux() const { return !isNull() && getInt() == 1; }
596 const Elf_Verdef *getVerdef() const {
597 return isVerdef() ? (const Elf_Verdef*)getPointer() : NULL;
599 const Elf_Vernaux *getVernaux() const {
600 return isVernaux() ? (const Elf_Vernaux*)getPointer() : NULL;
603 mutable SmallVector<VersionMapEntry, 16> VersionMap;
604 void LoadVersionDefs(const Elf_Shdr *sec) const;
605 void LoadVersionNeeds(const Elf_Shdr *ec) const;
606 void LoadVersionMap() const;
608 /// @brief Map sections to an array of relocation sections that reference
609 /// them sorted by section index.
610 RelocMap_t SectionRelocMap;
612 /// @brief Get the relocation section that contains \a Rel.
613 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
614 return getSection(Rel.w.b);
617 bool isRelocationHasAddend(DataRefImpl Rel) const;
619 const T *getEntry(uint16_t Section, uint32_t Entry) const;
621 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
622 const Elf_Shdr *getSection(DataRefImpl index) const;
623 const Elf_Shdr *getSection(uint32_t index) const;
624 const Elf_Rel *getRel(DataRefImpl Rel) const;
625 const Elf_Rela *getRela(DataRefImpl Rela) const;
626 const char *getString(uint32_t section, uint32_t offset) const;
627 const char *getString(const Elf_Shdr *section, uint32_t offset) const;
628 error_code getSymbolVersion(const Elf_Shdr *section,
631 bool &IsDefault) const;
632 void VerifyStrTab(const Elf_Shdr *sh) const;
635 const Elf_Sym *getSymbol(DataRefImpl Symb) const; // FIXME: Should be private?
636 void validateSymbol(DataRefImpl Symb) const;
639 error_code getSymbolName(const Elf_Shdr *section,
641 StringRef &Res) const;
642 error_code getSectionName(const Elf_Shdr *section,
643 StringRef &Res) const;
644 const Elf_Dyn *getDyn(DataRefImpl DynData) const;
645 error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
646 bool &IsDefault) const;
647 uint64_t getSymbolIndex(const Elf_Sym *sym) const;
649 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
650 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
651 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
652 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
653 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
654 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
655 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
656 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
657 virtual error_code getSymbolSection(DataRefImpl Symb,
658 section_iterator &Res) const;
659 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
661 friend class DynRefImpl<target_endianness, max_alignment, is64Bits>;
662 virtual error_code getDynNext(DataRefImpl DynData, DynRef &Result) const;
664 virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const;
665 virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const;
667 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
668 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
669 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
670 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
671 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
672 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
673 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
674 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
675 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
676 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
678 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
679 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
680 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
681 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
683 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
684 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
686 virtual error_code getRelocationNext(DataRefImpl Rel,
687 RelocationRef &Res) const;
688 virtual error_code getRelocationAddress(DataRefImpl Rel,
689 uint64_t &Res) const;
690 virtual error_code getRelocationOffset(DataRefImpl Rel,
691 uint64_t &Res) const;
692 virtual error_code getRelocationSymbol(DataRefImpl Rel,
693 SymbolRef &Res) const;
694 virtual error_code getRelocationType(DataRefImpl Rel,
695 uint64_t &Res) const;
696 virtual error_code getRelocationTypeName(DataRefImpl Rel,
697 SmallVectorImpl<char> &Result) const;
698 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
700 virtual error_code getRelocationValueString(DataRefImpl Rel,
701 SmallVectorImpl<char> &Result) const;
704 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
705 virtual symbol_iterator begin_symbols() const;
706 virtual symbol_iterator end_symbols() const;
708 virtual symbol_iterator begin_dynamic_symbols() const;
709 virtual symbol_iterator end_dynamic_symbols() const;
711 virtual section_iterator begin_sections() const;
712 virtual section_iterator end_sections() const;
714 virtual library_iterator begin_libraries_needed() const;
715 virtual library_iterator end_libraries_needed() const;
717 virtual dyn_iterator begin_dynamic_table() const;
718 virtual dyn_iterator end_dynamic_table() const;
720 typedef ELFEntityIterator<const Elf_Rela> Elf_Rela_Iter;
721 typedef ELFEntityIterator<const Elf_Rel> Elf_Rel_Iter;
723 Elf_Rela_Iter beginELFRela(const Elf_Shdr *sec) const {
724 return Elf_Rela_Iter(sec->sh_entsize,
725 (const char *)(base() + sec->sh_offset));
728 Elf_Rela_Iter endELFRela(const Elf_Shdr *sec) const {
729 return Elf_Rela_Iter(sec->sh_entsize, (const char *)
730 (base() + sec->sh_offset + sec->sh_size));
733 Elf_Rel_Iter beginELFRel(const Elf_Shdr *sec) const {
734 return Elf_Rel_Iter(sec->sh_entsize,
735 (const char *)(base() + sec->sh_offset));
738 Elf_Rel_Iter endELFRel(const Elf_Shdr *sec) const {
739 return Elf_Rel_Iter(sec->sh_entsize, (const char *)
740 (base() + sec->sh_offset + sec->sh_size));
743 virtual uint8_t getBytesInAddress() const;
744 virtual StringRef getFileFormatName() const;
745 virtual StringRef getObjectType() const { return "ELF"; }
746 virtual unsigned getArch() const;
747 virtual StringRef getLoadName() const;
748 virtual error_code getSectionContents(const Elf_Shdr *sec,
749 StringRef &Res) const;
751 uint64_t getNumSections() const;
752 uint64_t getStringTableIndex() const;
753 ELF::Elf64_Word getSymbolTableIndex(const Elf_Sym *symb) const;
754 const Elf_Shdr *getSection(const Elf_Sym *symb) const;
755 const Elf_Shdr *getElfSection(section_iterator &It) const;
756 const Elf_Sym *getElfSymbol(symbol_iterator &It) const;
757 const Elf_Sym *getElfSymbol(uint32_t index) const;
759 // Methods for type inquiry through isa, cast, and dyn_cast
760 bool isDyldType() const { return isDyldELFObject; }
761 static inline bool classof(const Binary *v) {
762 return v->getType() == getELFType(target_endianness == support::little,
767 // Iterate through the version definitions, and place each Elf_Verdef
768 // in the VersionMap according to its index.
769 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
770 void ELFObjectFile<target_endianness, max_alignment, is64Bits>::
771 LoadVersionDefs(const Elf_Shdr *sec) const {
772 unsigned vd_size = sec->sh_size; // Size of section in bytes
773 unsigned vd_count = sec->sh_info; // Number of Verdef entries
774 const char *sec_start = (const char*)base() + sec->sh_offset;
775 const char *sec_end = sec_start + vd_size;
776 // The first Verdef entry is at the start of the section.
777 const char *p = sec_start;
778 for (unsigned i = 0; i < vd_count; i++) {
779 if (p + sizeof(Elf_Verdef) > sec_end)
780 report_fatal_error("Section ended unexpectedly while scanning "
781 "version definitions.");
782 const Elf_Verdef *vd = reinterpret_cast<const Elf_Verdef *>(p);
783 if (vd->vd_version != ELF::VER_DEF_CURRENT)
784 report_fatal_error("Unexpected verdef version");
785 size_t index = vd->vd_ndx & ELF::VERSYM_VERSION;
786 if (index >= VersionMap.size())
787 VersionMap.resize(index+1);
788 VersionMap[index] = VersionMapEntry(vd);
793 // Iterate through the versions needed section, and place each Elf_Vernaux
794 // in the VersionMap according to its index.
795 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
796 void ELFObjectFile<target_endianness, max_alignment, is64Bits>::
797 LoadVersionNeeds(const Elf_Shdr *sec) const {
798 unsigned vn_size = sec->sh_size; // Size of section in bytes
799 unsigned vn_count = sec->sh_info; // Number of Verneed entries
800 const char *sec_start = (const char*)base() + sec->sh_offset;
801 const char *sec_end = sec_start + vn_size;
802 // The first Verneed entry is at the start of the section.
803 const char *p = sec_start;
804 for (unsigned i = 0; i < vn_count; i++) {
805 if (p + sizeof(Elf_Verneed) > sec_end)
806 report_fatal_error("Section ended unexpectedly while scanning "
807 "version needed records.");
808 const Elf_Verneed *vn = reinterpret_cast<const Elf_Verneed *>(p);
809 if (vn->vn_version != ELF::VER_NEED_CURRENT)
810 report_fatal_error("Unexpected verneed version");
811 // Iterate through the Vernaux entries
812 const char *paux = p + vn->vn_aux;
813 for (unsigned j = 0; j < vn->vn_cnt; j++) {
814 if (paux + sizeof(Elf_Vernaux) > sec_end)
815 report_fatal_error("Section ended unexpected while scanning auxiliary "
816 "version needed records.");
817 const Elf_Vernaux *vna = reinterpret_cast<const Elf_Vernaux *>(paux);
818 size_t index = vna->vna_other & ELF::VERSYM_VERSION;
819 if (index >= VersionMap.size())
820 VersionMap.resize(index+1);
821 VersionMap[index] = VersionMapEntry(vna);
822 paux += vna->vna_next;
828 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
829 void ELFObjectFile<target_endianness, max_alignment, is64Bits>
830 ::LoadVersionMap() const {
831 // If there is no dynamic symtab or version table, there is nothing to do.
832 if (SymbolTableSections[0] == NULL || dot_gnu_version_sec == NULL)
835 // Has the VersionMap already been loaded?
836 if (VersionMap.size() > 0)
839 // The first two version indexes are reserved.
840 // Index 0 is LOCAL, index 1 is GLOBAL.
841 VersionMap.push_back(VersionMapEntry());
842 VersionMap.push_back(VersionMapEntry());
844 if (dot_gnu_version_d_sec)
845 LoadVersionDefs(dot_gnu_version_d_sec);
847 if (dot_gnu_version_r_sec)
848 LoadVersionNeeds(dot_gnu_version_r_sec);
851 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
852 void ELFObjectFile<target_endianness, max_alignment, is64Bits>
853 ::validateSymbol(DataRefImpl Symb) const {
854 const Elf_Sym *symb = getSymbol(Symb);
855 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
856 // FIXME: We really need to do proper error handling in the case of an invalid
857 // input file. Because we don't use exceptions, I think we'll just pass
858 // an error object around.
860 && SymbolTableSection
861 && symb >= (const Elf_Sym*)(base()
862 + SymbolTableSection->sh_offset)
863 && symb < (const Elf_Sym*)(base()
864 + SymbolTableSection->sh_offset
865 + SymbolTableSection->sh_size)))
866 // FIXME: Proper error handling.
867 report_fatal_error("Symb must point to a valid symbol!");
870 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
871 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
872 ::getSymbolNext(DataRefImpl Symb,
873 SymbolRef &Result) const {
874 validateSymbol(Symb);
875 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
878 // Check to see if we are at the end of this symbol table.
879 if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
880 // We are at the end. If there are other symbol tables, jump to them.
881 // If the symbol table is .dynsym, we are iterating dynamic symbols,
882 // and there is only one table of these.
885 Symb.d.a = 1; // The 0th symbol in ELF is fake.
887 // Otherwise return the terminator.
888 if (Symb.d.b == 0 || Symb.d.b >= SymbolTableSections.size()) {
889 Symb.d.a = std::numeric_limits<uint32_t>::max();
890 Symb.d.b = std::numeric_limits<uint32_t>::max();
894 Result = SymbolRef(Symb, this);
895 return object_error::success;
898 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
899 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
900 ::getSymbolName(DataRefImpl Symb,
901 StringRef &Result) const {
902 validateSymbol(Symb);
903 const Elf_Sym *symb = getSymbol(Symb);
904 return getSymbolName(SymbolTableSections[Symb.d.b], symb, Result);
907 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
908 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
909 ::getSymbolVersion(SymbolRef SymRef,
911 bool &IsDefault) const {
912 DataRefImpl Symb = SymRef.getRawDataRefImpl();
913 validateSymbol(Symb);
914 const Elf_Sym *symb = getSymbol(Symb);
915 return getSymbolVersion(SymbolTableSections[Symb.d.b], symb,
919 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
920 ELF::Elf64_Word ELFObjectFile<target_endianness, max_alignment, is64Bits>
921 ::getSymbolTableIndex(const Elf_Sym *symb) const {
922 if (symb->st_shndx == ELF::SHN_XINDEX)
923 return ExtendedSymbolTable.lookup(symb);
924 return symb->st_shndx;
927 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
928 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
930 ELFObjectFile<target_endianness, max_alignment, is64Bits>
931 ::getSection(const Elf_Sym *symb) const {
932 if (symb->st_shndx == ELF::SHN_XINDEX)
933 return getSection(ExtendedSymbolTable.lookup(symb));
934 if (symb->st_shndx >= ELF::SHN_LORESERVE)
936 return getSection(symb->st_shndx);
939 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
940 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
942 ELFObjectFile<target_endianness, max_alignment, is64Bits>
943 ::getElfSection(section_iterator &It) const {
944 llvm::object::DataRefImpl ShdrRef = It->getRawDataRefImpl();
945 return reinterpret_cast<const Elf_Shdr *>(ShdrRef.p);
948 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
949 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
951 ELFObjectFile<target_endianness, max_alignment, is64Bits>
952 ::getElfSymbol(symbol_iterator &It) const {
953 return getSymbol(It->getRawDataRefImpl());
956 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
957 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
959 ELFObjectFile<target_endianness, max_alignment, is64Bits>
960 ::getElfSymbol(uint32_t index) const {
961 DataRefImpl SymbolData;
962 SymbolData.d.a = index;
964 return getSymbol(SymbolData);
967 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
968 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
969 ::getSymbolFileOffset(DataRefImpl Symb,
970 uint64_t &Result) const {
971 validateSymbol(Symb);
972 const Elf_Sym *symb = getSymbol(Symb);
973 const Elf_Shdr *Section;
974 switch (getSymbolTableIndex(symb)) {
975 case ELF::SHN_COMMON:
976 // Unintialized symbols have no offset in the object file
978 Result = UnknownAddressOrSize;
979 return object_error::success;
981 Result = symb->st_value;
982 return object_error::success;
983 default: Section = getSection(symb);
986 switch (symb->getType()) {
987 case ELF::STT_SECTION:
988 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
989 return object_error::success;
991 case ELF::STT_OBJECT:
992 case ELF::STT_NOTYPE:
993 Result = symb->st_value +
994 (Section ? Section->sh_offset : 0);
995 return object_error::success;
997 Result = UnknownAddressOrSize;
998 return object_error::success;
1002 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1003 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1004 ::getSymbolAddress(DataRefImpl Symb,
1005 uint64_t &Result) const {
1006 validateSymbol(Symb);
1007 const Elf_Sym *symb = getSymbol(Symb);
1008 const Elf_Shdr *Section;
1009 switch (getSymbolTableIndex(symb)) {
1010 case ELF::SHN_COMMON:
1011 case ELF::SHN_UNDEF:
1012 Result = UnknownAddressOrSize;
1013 return object_error::success;
1015 Result = symb->st_value;
1016 return object_error::success;
1017 default: Section = getSection(symb);
1020 switch (symb->getType()) {
1021 case ELF::STT_SECTION:
1022 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
1023 return object_error::success;
1025 case ELF::STT_OBJECT:
1026 case ELF::STT_NOTYPE:
1028 switch(Header->e_type) {
1031 IsRelocatable = false;
1034 IsRelocatable = true;
1036 Result = symb->st_value;
1037 if (IsRelocatable && Section != 0)
1038 Result += Section->sh_addr;
1039 return object_error::success;
1041 Result = UnknownAddressOrSize;
1042 return object_error::success;
1046 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1047 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1048 ::getSymbolSize(DataRefImpl Symb,
1049 uint64_t &Result) const {
1050 validateSymbol(Symb);
1051 const Elf_Sym *symb = getSymbol(Symb);
1052 if (symb->st_size == 0)
1053 Result = UnknownAddressOrSize;
1054 Result = symb->st_size;
1055 return object_error::success;
1058 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1059 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1060 ::getSymbolNMTypeChar(DataRefImpl Symb,
1061 char &Result) const {
1062 validateSymbol(Symb);
1063 const Elf_Sym *symb = getSymbol(Symb);
1064 const Elf_Shdr *Section = getSection(symb);
1069 switch (Section->sh_type) {
1070 case ELF::SHT_PROGBITS:
1071 case ELF::SHT_DYNAMIC:
1072 switch (Section->sh_flags) {
1073 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
1075 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
1077 case ELF::SHF_ALLOC:
1078 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
1079 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
1083 case ELF::SHT_NOBITS: ret = 'b';
1087 switch (getSymbolTableIndex(symb)) {
1088 case ELF::SHN_UNDEF:
1092 case ELF::SHN_ABS: ret = 'a'; break;
1093 case ELF::SHN_COMMON: ret = 'c'; break;
1096 switch (symb->getBinding()) {
1097 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
1099 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1102 if (symb->getType() == ELF::STT_OBJECT)
1108 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
1110 if (error_code ec = getSymbolName(Symb, name))
1112 Result = StringSwitch<char>(name)
1113 .StartsWith(".debug", 'N')
1114 .StartsWith(".note", 'n')
1116 return object_error::success;
1120 return object_error::success;
1123 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1124 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1125 ::getSymbolType(DataRefImpl Symb,
1126 SymbolRef::Type &Result) const {
1127 validateSymbol(Symb);
1128 const Elf_Sym *symb = getSymbol(Symb);
1130 switch (symb->getType()) {
1131 case ELF::STT_NOTYPE:
1132 Result = SymbolRef::ST_Unknown;
1134 case ELF::STT_SECTION:
1135 Result = SymbolRef::ST_Debug;
1138 Result = SymbolRef::ST_File;
1141 Result = SymbolRef::ST_Function;
1143 case ELF::STT_OBJECT:
1144 case ELF::STT_COMMON:
1146 Result = SymbolRef::ST_Data;
1149 Result = SymbolRef::ST_Other;
1152 return object_error::success;
1155 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1156 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1157 ::getSymbolFlags(DataRefImpl Symb,
1158 uint32_t &Result) const {
1159 validateSymbol(Symb);
1160 const Elf_Sym *symb = getSymbol(Symb);
1162 Result = SymbolRef::SF_None;
1164 if (symb->getBinding() != ELF::STB_LOCAL)
1165 Result |= SymbolRef::SF_Global;
1167 if (symb->getBinding() == ELF::STB_WEAK)
1168 Result |= SymbolRef::SF_Weak;
1170 if (symb->st_shndx == ELF::SHN_ABS)
1171 Result |= SymbolRef::SF_Absolute;
1173 if (symb->getType() == ELF::STT_FILE ||
1174 symb->getType() == ELF::STT_SECTION)
1175 Result |= SymbolRef::SF_FormatSpecific;
1177 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1178 Result |= SymbolRef::SF_Undefined;
1180 if (symb->getType() == ELF::STT_COMMON ||
1181 getSymbolTableIndex(symb) == ELF::SHN_COMMON)
1182 Result |= SymbolRef::SF_Common;
1184 if (symb->getType() == ELF::STT_TLS)
1185 Result |= SymbolRef::SF_ThreadLocal;
1187 return object_error::success;
1190 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1191 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1192 ::getSymbolSection(DataRefImpl Symb,
1193 section_iterator &Res) const {
1194 validateSymbol(Symb);
1195 const Elf_Sym *symb = getSymbol(Symb);
1196 const Elf_Shdr *sec = getSection(symb);
1198 Res = end_sections();
1201 Sec.p = reinterpret_cast<intptr_t>(sec);
1202 Res = section_iterator(SectionRef(Sec, this));
1204 return object_error::success;
1207 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1208 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1209 ::getSymbolValue(DataRefImpl Symb,
1210 uint64_t &Val) const {
1211 validateSymbol(Symb);
1212 const Elf_Sym *symb = getSymbol(Symb);
1213 Val = symb->st_value;
1214 return object_error::success;
1217 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1218 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1219 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
1220 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
1221 sec += Header->e_shentsize;
1222 Sec.p = reinterpret_cast<intptr_t>(sec);
1223 Result = SectionRef(Sec, this);
1224 return object_error::success;
1227 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1228 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1229 ::getSectionName(DataRefImpl Sec,
1230 StringRef &Result) const {
1231 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1232 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
1233 return object_error::success;
1236 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1237 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1238 ::getSectionAddress(DataRefImpl Sec,
1239 uint64_t &Result) const {
1240 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1241 Result = sec->sh_addr;
1242 return object_error::success;
1245 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1246 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1247 ::getSectionSize(DataRefImpl Sec,
1248 uint64_t &Result) const {
1249 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1250 Result = sec->sh_size;
1251 return object_error::success;
1254 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1255 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1256 ::getSectionContents(DataRefImpl Sec,
1257 StringRef &Result) const {
1258 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1259 const char *start = (const char*)base() + sec->sh_offset;
1260 Result = StringRef(start, sec->sh_size);
1261 return object_error::success;
1264 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1265 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1266 ::getSectionContents(const Elf_Shdr *Sec,
1267 StringRef &Result) const {
1268 const char *start = (const char*)base() + Sec->sh_offset;
1269 Result = StringRef(start, Sec->sh_size);
1270 return object_error::success;
1273 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1274 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1275 ::getSectionAlignment(DataRefImpl Sec,
1276 uint64_t &Result) const {
1277 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1278 Result = sec->sh_addralign;
1279 return object_error::success;
1282 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1283 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1284 ::isSectionText(DataRefImpl Sec,
1285 bool &Result) const {
1286 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1287 if (sec->sh_flags & ELF::SHF_EXECINSTR)
1291 return object_error::success;
1294 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1295 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1296 ::isSectionData(DataRefImpl Sec,
1297 bool &Result) const {
1298 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1299 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1300 && sec->sh_type == ELF::SHT_PROGBITS)
1304 return object_error::success;
1307 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1308 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1309 ::isSectionBSS(DataRefImpl Sec,
1310 bool &Result) const {
1311 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1312 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1313 && sec->sh_type == ELF::SHT_NOBITS)
1317 return object_error::success;
1320 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1321 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1322 ::isSectionRequiredForExecution(DataRefImpl Sec,
1323 bool &Result) const {
1324 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1325 if (sec->sh_flags & ELF::SHF_ALLOC)
1329 return object_error::success;
1332 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1333 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1334 ::isSectionVirtual(DataRefImpl Sec,
1335 bool &Result) const {
1336 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1337 if (sec->sh_type == ELF::SHT_NOBITS)
1341 return object_error::success;
1344 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1345 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1346 ::isSectionZeroInit(DataRefImpl Sec,
1347 bool &Result) const {
1348 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1349 // For ELF, all zero-init sections are virtual (that is, they occupy no space
1350 // in the object image) and vice versa.
1351 Result = sec->sh_type == ELF::SHT_NOBITS;
1352 return object_error::success;
1355 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1356 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1357 ::isSectionReadOnlyData(DataRefImpl Sec,
1358 bool &Result) const {
1359 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1360 if (sec->sh_flags & ELF::SHF_WRITE || sec->sh_flags & ELF::SHF_EXECINSTR)
1364 return object_error::success;
1367 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1368 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1369 ::sectionContainsSymbol(DataRefImpl Sec,
1371 bool &Result) const {
1372 // FIXME: Unimplemented.
1374 return object_error::success;
1377 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1378 relocation_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
1379 ::getSectionRelBegin(DataRefImpl Sec) const {
1380 DataRefImpl RelData;
1381 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1382 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1383 if (sec != 0 && ittr != SectionRelocMap.end()) {
1384 RelData.w.a = getSection(ittr->second[0])->sh_info;
1385 RelData.w.b = ittr->second[0];
1388 return relocation_iterator(RelocationRef(RelData, this));
1391 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1392 relocation_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
1393 ::getSectionRelEnd(DataRefImpl Sec) const {
1394 DataRefImpl RelData;
1395 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1396 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1397 if (sec != 0 && ittr != SectionRelocMap.end()) {
1398 // Get the index of the last relocation section for this section.
1399 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
1400 const Elf_Shdr *relocsec = getSection(relocsecindex);
1401 RelData.w.a = relocsec->sh_info;
1402 RelData.w.b = relocsecindex;
1403 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
1405 return relocation_iterator(RelocationRef(RelData, this));
1409 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1410 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1411 ::getRelocationNext(DataRefImpl Rel,
1412 RelocationRef &Result) const {
1414 const Elf_Shdr *relocsec = getSection(Rel.w.b);
1415 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
1416 // We have reached the end of the relocations for this section. See if there
1417 // is another relocation section.
1418 typename RelocMap_t::mapped_type relocseclist =
1419 SectionRelocMap.lookup(getSection(Rel.w.a));
1421 // Do a binary search for the current reloc section index (which must be
1422 // present). Then get the next one.
1423 typename RelocMap_t::mapped_type::const_iterator loc =
1424 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
1427 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
1428 // to the end iterator.
1429 if (loc != relocseclist.end()) {
1434 Result = RelocationRef(Rel, this);
1435 return object_error::success;
1438 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1439 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1440 ::getRelocationSymbol(DataRefImpl Rel,
1441 SymbolRef &Result) const {
1443 const Elf_Shdr *sec = getSection(Rel.w.b);
1444 switch (sec->sh_type) {
1446 report_fatal_error("Invalid section type in Rel!");
1447 case ELF::SHT_REL : {
1448 symbolIdx = getRel(Rel)->getSymbol();
1451 case ELF::SHT_RELA : {
1452 symbolIdx = getRela(Rel)->getSymbol();
1456 DataRefImpl SymbolData;
1457 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
1458 if (it == SymbolTableSectionsIndexMap.end())
1459 report_fatal_error("Relocation symbol table not found!");
1460 SymbolData.d.a = symbolIdx;
1461 SymbolData.d.b = it->second;
1462 Result = SymbolRef(SymbolData, this);
1463 return object_error::success;
1466 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1467 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1468 ::getRelocationAddress(DataRefImpl Rel,
1469 uint64_t &Result) const {
1471 const Elf_Shdr *sec = getSection(Rel.w.b);
1472 switch (sec->sh_type) {
1474 report_fatal_error("Invalid section type in Rel!");
1475 case ELF::SHT_REL : {
1476 offset = getRel(Rel)->r_offset;
1479 case ELF::SHT_RELA : {
1480 offset = getRela(Rel)->r_offset;
1486 return object_error::success;
1489 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1490 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1491 ::getRelocationOffset(DataRefImpl Rel,
1492 uint64_t &Result) const {
1494 const Elf_Shdr *sec = getSection(Rel.w.b);
1495 switch (sec->sh_type) {
1497 report_fatal_error("Invalid section type in Rel!");
1498 case ELF::SHT_REL : {
1499 offset = getRel(Rel)->r_offset;
1502 case ELF::SHT_RELA : {
1503 offset = getRela(Rel)->r_offset;
1508 Result = offset - sec->sh_addr;
1509 return object_error::success;
1512 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1513 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1514 ::getRelocationType(DataRefImpl Rel,
1515 uint64_t &Result) const {
1516 const Elf_Shdr *sec = getSection(Rel.w.b);
1517 switch (sec->sh_type) {
1519 report_fatal_error("Invalid section type in Rel!");
1520 case ELF::SHT_REL : {
1521 Result = getRel(Rel)->getType();
1524 case ELF::SHT_RELA : {
1525 Result = getRela(Rel)->getType();
1529 return object_error::success;
1532 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
1533 case ELF::enum: res = #enum; break;
1535 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1536 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1537 ::getRelocationTypeName(DataRefImpl Rel,
1538 SmallVectorImpl<char> &Result) const {
1539 const Elf_Shdr *sec = getSection(Rel.w.b);
1542 switch (sec->sh_type) {
1544 return object_error::parse_failed;
1545 case ELF::SHT_REL : {
1546 type = getRel(Rel)->getType();
1549 case ELF::SHT_RELA : {
1550 type = getRela(Rel)->getType();
1554 switch (Header->e_machine) {
1555 case ELF::EM_X86_64:
1557 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
1558 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
1559 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
1560 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
1561 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
1562 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
1563 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
1564 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
1565 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
1566 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
1567 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
1568 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
1569 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
1570 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
1571 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
1572 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
1573 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
1574 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
1575 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
1576 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
1577 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
1578 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
1579 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
1580 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
1581 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
1582 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
1583 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
1584 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
1585 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
1586 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
1587 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
1588 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
1595 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
1596 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
1597 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
1598 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
1599 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
1600 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
1601 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
1602 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
1603 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
1604 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
1605 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
1606 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
1607 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
1608 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
1609 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
1610 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
1611 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
1612 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
1613 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
1614 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
1615 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
1616 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
1617 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
1618 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
1619 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
1620 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
1621 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
1622 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
1623 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
1624 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
1625 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
1626 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
1627 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
1628 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
1629 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
1630 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
1631 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
1632 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
1633 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
1634 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
1641 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_NONE);
1642 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PC24);
1643 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32);
1644 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32);
1645 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G0);
1646 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS16);
1647 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS12);
1648 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ABS5);
1649 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS8);
1650 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL32);
1651 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_CALL);
1652 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC8);
1653 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BREL_ADJ);
1654 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESC);
1655 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_SWI8);
1656 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_XPC25);
1657 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_XPC22);
1658 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPMOD32);
1659 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPOFF32);
1660 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_TPOFF32);
1661 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_COPY);
1662 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GLOB_DAT);
1663 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP_SLOT);
1664 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_RELATIVE);
1665 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF32);
1666 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_PREL);
1667 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL);
1668 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32);
1669 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_CALL);
1670 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP24);
1671 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP24);
1672 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_ABS);
1673 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_7_0);
1674 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_15_8);
1675 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_23_15);
1676 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SBREL_11_0_NC);
1677 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_19_12_NC);
1678 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_27_20_CK);
1679 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET1);
1680 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL31);
1681 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_V4BX);
1682 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET2);
1683 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PREL31);
1684 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_ABS_NC);
1685 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_ABS);
1686 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_PREL_NC);
1687 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_PREL);
1688 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_ABS_NC);
1689 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_ABS);
1690 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_PREL_NC);
1691 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_PREL);
1692 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP19);
1693 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP6);
1694 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ALU_PREL_11_0);
1695 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC12);
1696 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32_NOI);
1697 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32_NOI);
1698 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0_NC);
1699 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0);
1700 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1_NC);
1701 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1);
1702 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G2);
1703 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G1);
1704 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G2);
1705 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G0);
1706 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G1);
1707 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G2);
1708 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G0);
1709 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G1);
1710 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G2);
1711 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0_NC);
1712 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0);
1713 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1_NC);
1714 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1);
1715 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G2);
1716 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G0);
1717 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G1);
1718 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G2);
1719 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G0);
1720 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G1);
1721 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G2);
1722 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G0);
1723 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G1);
1724 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G2);
1725 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL_NC);
1726 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_BREL);
1727 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL);
1728 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL_NC);
1729 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_BREL);
1730 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL);
1731 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GOTDESC);
1732 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_CALL);
1733 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESCSEQ);
1734 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_CALL);
1735 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32_ABS);
1736 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_ABS);
1737 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_PREL);
1738 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL12);
1739 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF12);
1740 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTRELAX);
1741 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTENTRY);
1742 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTINHERIT);
1743 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP11);
1744 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP8);
1745 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GD32);
1746 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDM32);
1747 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO32);
1748 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE32);
1749 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE32);
1750 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO12);
1751 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE12);
1752 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE12GP);
1753 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_0);
1754 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_1);
1755 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_2);
1756 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_3);
1757 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_4);
1758 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_5);
1759 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_6);
1760 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_7);
1761 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_8);
1762 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_9);
1763 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_10);
1764 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_11);
1765 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_12);
1766 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_13);
1767 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_14);
1768 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_15);
1769 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ME_TOO);
1770 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ16);
1771 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ32);
1776 case ELF::EM_HEXAGON:
1778 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_NONE);
1779 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL);
1780 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL);
1781 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL);
1782 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_LO16);
1783 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HI16);
1784 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32);
1785 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16);
1786 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8);
1787 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_0);
1788 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_1);
1789 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_2);
1790 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_3);
1791 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HL16);
1792 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL);
1793 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL);
1794 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B32_PCREL_X);
1795 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_6_X);
1796 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL_X);
1797 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL_X);
1798 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL_X);
1799 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL_X);
1800 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL_X);
1801 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16_X);
1802 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_12_X);
1803 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_11_X);
1804 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_10_X);
1805 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_9_X);
1806 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8_X);
1807 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_7_X);
1808 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_X);
1809 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_PCREL);
1810 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_COPY);
1811 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GLOB_DAT);
1812 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_JMP_SLOT);
1813 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_RELATIVE);
1814 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_PLT_B22_PCREL);
1815 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_LO16);
1816 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_HI16);
1817 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32);
1818 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_LO16);
1819 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_HI16);
1820 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32);
1821 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16);
1822 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPMOD_32);
1823 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_LO16);
1824 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_HI16);
1825 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32);
1826 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16);
1827 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_PLT_B22_PCREL);
1828 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_LO16);
1829 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_HI16);
1830 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32);
1831 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16);
1832 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_LO16);
1833 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_HI16);
1834 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32);
1835 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_LO16);
1836 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_HI16);
1837 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32);
1838 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16);
1839 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_LO16);
1840 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_HI16);
1841 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32);
1842 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16);
1843 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_PCREL_X);
1844 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32_6_X);
1845 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_16_X);
1846 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_11_X);
1847 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32_6_X);
1848 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16_X);
1849 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_11_X);
1850 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32_6_X);
1851 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16_X);
1852 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_11_X);
1853 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32_6_X);
1854 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16_X);
1855 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_11_X);
1856 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32_6_X);
1857 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_16_X);
1858 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32_6_X);
1859 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16_X);
1860 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_11_X);
1861 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32_6_X);
1862 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16_X);
1863 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_11_X);
1871 Result.append(res.begin(), res.end());
1872 return object_error::success;
1875 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
1877 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1878 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1879 ::getRelocationAdditionalInfo(DataRefImpl Rel,
1880 int64_t &Result) const {
1881 const Elf_Shdr *sec = getSection(Rel.w.b);
1882 switch (sec->sh_type) {
1884 report_fatal_error("Invalid section type in Rel!");
1885 case ELF::SHT_REL : {
1887 return object_error::success;
1889 case ELF::SHT_RELA : {
1890 Result = getRela(Rel)->r_addend;
1891 return object_error::success;
1896 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1897 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1898 ::getRelocationValueString(DataRefImpl Rel,
1899 SmallVectorImpl<char> &Result) const {
1900 const Elf_Shdr *sec = getSection(Rel.w.b);
1904 uint16_t symbol_index = 0;
1905 switch (sec->sh_type) {
1907 return object_error::parse_failed;
1908 case ELF::SHT_REL: {
1909 type = getRel(Rel)->getType();
1910 symbol_index = getRel(Rel)->getSymbol();
1911 // TODO: Read implicit addend from section data.
1914 case ELF::SHT_RELA: {
1915 type = getRela(Rel)->getType();
1916 symbol_index = getRela(Rel)->getSymbol();
1917 addend = getRela(Rel)->r_addend;
1921 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1923 if (error_code ec = getSymbolName(getSection(sec->sh_link), symb, symname))
1925 switch (Header->e_machine) {
1926 case ELF::EM_X86_64:
1928 case ELF::R_X86_64_PC8:
1929 case ELF::R_X86_64_PC16:
1930 case ELF::R_X86_64_PC32: {
1932 raw_string_ostream fmt(fmtbuf);
1933 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1935 Result.append(fmtbuf.begin(), fmtbuf.end());
1938 case ELF::R_X86_64_8:
1939 case ELF::R_X86_64_16:
1940 case ELF::R_X86_64_32:
1941 case ELF::R_X86_64_32S:
1942 case ELF::R_X86_64_64: {
1944 raw_string_ostream fmt(fmtbuf);
1945 fmt << symname << (addend < 0 ? "" : "+") << addend;
1947 Result.append(fmtbuf.begin(), fmtbuf.end());
1955 case ELF::EM_HEXAGON:
1962 Result.append(res.begin(), res.end());
1963 return object_error::success;
1966 // Verify that the last byte in the string table in a null.
1967 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1968 void ELFObjectFile<target_endianness, max_alignment, is64Bits>
1969 ::VerifyStrTab(const Elf_Shdr *sh) const {
1970 const char *strtab = (const char*)base() + sh->sh_offset;
1971 if (strtab[sh->sh_size - 1] != 0)
1972 // FIXME: Proper error handling.
1973 report_fatal_error("String table must end with a null terminator!");
1976 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1977 ELFObjectFile<target_endianness, max_alignment, is64Bits>
1978 ::ELFObjectFile(MemoryBuffer *Object, error_code &ec)
1979 : ObjectFile(getELFType(target_endianness == support::little, is64Bits),
1981 , isDyldELFObject(false)
1982 , SectionHeaderTable(0)
1983 , dot_shstrtab_sec(0)
1986 , dot_dynamic_sec(0)
1987 , dot_gnu_version_sec(0)
1988 , dot_gnu_version_r_sec(0)
1989 , dot_gnu_version_d_sec(0)
1993 const uint64_t FileSize = Data->getBufferSize();
1995 if (sizeof(Elf_Ehdr) > FileSize)
1996 // FIXME: Proper error handling.
1997 report_fatal_error("File too short!");
1999 Header = reinterpret_cast<const Elf_Ehdr *>(base());
2001 if (Header->e_shoff == 0)
2004 const uint64_t SectionTableOffset = Header->e_shoff;
2006 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize)
2007 // FIXME: Proper error handling.
2008 report_fatal_error("Section header table goes past end of file!");
2010 // The getNumSections() call below depends on SectionHeaderTable being set.
2011 SectionHeaderTable =
2012 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
2013 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
2015 if (SectionTableOffset + SectionTableSize > FileSize)
2016 // FIXME: Proper error handling.
2017 report_fatal_error("Section table goes past end of file!");
2019 // To find the symbol tables we walk the section table to find SHT_SYMTAB.
2020 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
2021 const Elf_Shdr* sh = SectionHeaderTable;
2023 // Reserve SymbolTableSections[0] for .dynsym
2024 SymbolTableSections.push_back(NULL);
2026 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
2027 switch (sh->sh_type) {
2028 case ELF::SHT_SYMTAB_SHNDX: {
2029 if (SymbolTableSectionHeaderIndex)
2030 // FIXME: Proper error handling.
2031 report_fatal_error("More than one .symtab_shndx!");
2032 SymbolTableSectionHeaderIndex = sh;
2035 case ELF::SHT_SYMTAB: {
2036 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
2037 SymbolTableSections.push_back(sh);
2040 case ELF::SHT_DYNSYM: {
2041 if (SymbolTableSections[0] != NULL)
2042 // FIXME: Proper error handling.
2043 report_fatal_error("More than one .dynsym!");
2044 SymbolTableSectionsIndexMap[i] = 0;
2045 SymbolTableSections[0] = sh;
2049 case ELF::SHT_RELA: {
2050 SectionRelocMap[getSection(sh->sh_info)].push_back(i);
2053 case ELF::SHT_DYNAMIC: {
2054 if (dot_dynamic_sec != NULL)
2055 // FIXME: Proper error handling.
2056 report_fatal_error("More than one .dynamic!");
2057 dot_dynamic_sec = sh;
2060 case ELF::SHT_GNU_versym: {
2061 if (dot_gnu_version_sec != NULL)
2062 // FIXME: Proper error handling.
2063 report_fatal_error("More than one .gnu.version section!");
2064 dot_gnu_version_sec = sh;
2067 case ELF::SHT_GNU_verdef: {
2068 if (dot_gnu_version_d_sec != NULL)
2069 // FIXME: Proper error handling.
2070 report_fatal_error("More than one .gnu.version_d section!");
2071 dot_gnu_version_d_sec = sh;
2074 case ELF::SHT_GNU_verneed: {
2075 if (dot_gnu_version_r_sec != NULL)
2076 // FIXME: Proper error handling.
2077 report_fatal_error("More than one .gnu.version_r section!");
2078 dot_gnu_version_r_sec = sh;
2085 // Sort section relocation lists by index.
2086 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
2087 e = SectionRelocMap.end(); i != e; ++i) {
2088 std::sort(i->second.begin(), i->second.end());
2091 // Get string table sections.
2092 dot_shstrtab_sec = getSection(getStringTableIndex());
2093 if (dot_shstrtab_sec) {
2094 // Verify that the last byte in the string table in a null.
2095 VerifyStrTab(dot_shstrtab_sec);
2098 // Merge this into the above loop.
2099 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
2100 *e = i + getNumSections() * Header->e_shentsize;
2101 i != e; i += Header->e_shentsize) {
2102 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
2103 if (sh->sh_type == ELF::SHT_STRTAB) {
2104 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
2105 if (SectionName == ".strtab") {
2106 if (dot_strtab_sec != 0)
2107 // FIXME: Proper error handling.
2108 report_fatal_error("Already found section named .strtab!");
2109 dot_strtab_sec = sh;
2110 VerifyStrTab(dot_strtab_sec);
2111 } else if (SectionName == ".dynstr") {
2112 if (dot_dynstr_sec != 0)
2113 // FIXME: Proper error handling.
2114 report_fatal_error("Already found section named .dynstr!");
2115 dot_dynstr_sec = sh;
2116 VerifyStrTab(dot_dynstr_sec);
2121 // Build symbol name side-mapping if there is one.
2122 if (SymbolTableSectionHeaderIndex) {
2123 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
2124 SymbolTableSectionHeaderIndex->sh_offset);
2126 for (symbol_iterator si = begin_symbols(),
2127 se = end_symbols(); si != se; si.increment(ec)) {
2129 report_fatal_error("Fewer extended symbol table entries than symbols!");
2130 if (*ShndxTable != ELF::SHN_UNDEF)
2131 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
2137 // Get the symbol table index in the symtab section given a symbol
2138 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2139 uint64_t ELFObjectFile<target_endianness, max_alignment, is64Bits>
2140 ::getSymbolIndex(const Elf_Sym *Sym) const {
2141 assert(SymbolTableSections.size() == 1 && "Only one symbol table supported!");
2142 const Elf_Shdr *SymTab = *SymbolTableSections.begin();
2143 uintptr_t SymLoc = uintptr_t(Sym);
2144 uintptr_t SymTabLoc = uintptr_t(base() + SymTab->sh_offset);
2145 assert(SymLoc > SymTabLoc && "Symbol not in symbol table!");
2146 uint64_t SymOffset = SymLoc - SymTabLoc;
2147 assert(SymOffset % SymTab->sh_entsize == 0 &&
2148 "Symbol not multiple of symbol size!");
2149 return SymOffset / SymTab->sh_entsize;
2152 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2153 symbol_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2154 ::begin_symbols() const {
2155 DataRefImpl SymbolData;
2156 if (SymbolTableSections.size() <= 1) {
2157 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2158 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2160 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2161 SymbolData.d.b = 1; // The 0th table is .dynsym
2163 return symbol_iterator(SymbolRef(SymbolData, this));
2166 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2167 symbol_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2168 ::end_symbols() const {
2169 DataRefImpl SymbolData;
2170 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2171 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2172 return symbol_iterator(SymbolRef(SymbolData, this));
2175 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2176 symbol_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2177 ::begin_dynamic_symbols() const {
2178 DataRefImpl SymbolData;
2179 if (SymbolTableSections[0] == NULL) {
2180 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2181 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2183 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2184 SymbolData.d.b = 0; // The 0th table is .dynsym
2186 return symbol_iterator(SymbolRef(SymbolData, this));
2189 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2190 symbol_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2191 ::end_dynamic_symbols() const {
2192 DataRefImpl SymbolData;
2193 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2194 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2195 return symbol_iterator(SymbolRef(SymbolData, this));
2198 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2199 section_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2200 ::begin_sections() const {
2202 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
2203 return section_iterator(SectionRef(ret, this));
2206 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2207 section_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2208 ::end_sections() const {
2210 ret.p = reinterpret_cast<intptr_t>(base()
2212 + (Header->e_shentsize*getNumSections()));
2213 return section_iterator(SectionRef(ret, this));
2216 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2217 typename ELFObjectFile<target_endianness, max_alignment, is64Bits>::dyn_iterator
2218 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2219 ::begin_dynamic_table() const {
2220 DataRefImpl DynData;
2221 if (dot_dynamic_sec == NULL || dot_dynamic_sec->sh_size == 0) {
2222 DynData.d.a = std::numeric_limits<uint32_t>::max();
2226 return dyn_iterator(DynRef(DynData, this));
2229 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2230 typename ELFObjectFile<target_endianness, max_alignment, is64Bits>::dyn_iterator
2231 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2232 ::end_dynamic_table() const {
2233 DataRefImpl DynData;
2234 DynData.d.a = std::numeric_limits<uint32_t>::max();
2235 return dyn_iterator(DynRef(DynData, this));
2238 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2239 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2240 ::getDynNext(DataRefImpl DynData,
2241 DynRef &Result) const {
2244 // Check to see if we are at the end of .dynamic
2245 if (DynData.d.a >= dot_dynamic_sec->getEntityCount()) {
2246 // We are at the end. Return the terminator.
2247 DynData.d.a = std::numeric_limits<uint32_t>::max();
2250 Result = DynRef(DynData, this);
2251 return object_error::success;
2254 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2256 ELFObjectFile<target_endianness, max_alignment, is64Bits>::getLoadName() const {
2258 // Find the DT_SONAME entry
2259 dyn_iterator it = begin_dynamic_table();
2260 dyn_iterator ie = end_dynamic_table();
2263 if (it->getTag() == ELF::DT_SONAME)
2267 report_fatal_error("dynamic table iteration failed");
2270 if (dot_dynstr_sec == NULL)
2271 report_fatal_error("Dynamic string table is missing");
2272 dt_soname = getString(dot_dynstr_sec, it->getVal());
2280 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2281 library_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2282 ::begin_libraries_needed() const {
2283 // Find the first DT_NEEDED entry
2284 dyn_iterator i = begin_dynamic_table();
2285 dyn_iterator e = end_dynamic_table();
2288 if (i->getTag() == ELF::DT_NEEDED)
2292 report_fatal_error("dynamic table iteration failed");
2294 // Use the same DataRefImpl format as DynRef.
2295 return library_iterator(LibraryRef(i->getRawDataRefImpl(), this));
2298 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2299 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2300 ::getLibraryNext(DataRefImpl Data,
2301 LibraryRef &Result) const {
2302 // Use the same DataRefImpl format as DynRef.
2303 dyn_iterator i = dyn_iterator(DynRef(Data, this));
2304 dyn_iterator e = end_dynamic_table();
2306 // Skip the current dynamic table entry.
2310 // TODO: proper error handling
2312 report_fatal_error("dynamic table iteration failed");
2315 // Find the next DT_NEEDED entry.
2317 if (i->getTag() == ELF::DT_NEEDED)
2321 report_fatal_error("dynamic table iteration failed");
2323 Result = LibraryRef(i->getRawDataRefImpl(), this);
2324 return object_error::success;
2327 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2328 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2329 ::getLibraryPath(DataRefImpl Data, StringRef &Res) const {
2330 dyn_iterator i = dyn_iterator(DynRef(Data, this));
2331 if (i == end_dynamic_table())
2332 report_fatal_error("getLibraryPath() called on iterator end");
2334 if (i->getTag() != ELF::DT_NEEDED)
2335 report_fatal_error("Invalid library_iterator");
2337 // This uses .dynstr to lookup the name of the DT_NEEDED entry.
2338 // THis works as long as DT_STRTAB == .dynstr. This is true most of
2339 // the time, but the specification allows exceptions.
2340 // TODO: This should really use DT_STRTAB instead. Doing this requires
2341 // reading the program headers.
2342 if (dot_dynstr_sec == NULL)
2343 report_fatal_error("Dynamic string table is missing");
2344 Res = getString(dot_dynstr_sec, i->getVal());
2345 return object_error::success;
2348 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2349 library_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2350 ::end_libraries_needed() const {
2351 dyn_iterator e = end_dynamic_table();
2352 // Use the same DataRefImpl format as DynRef.
2353 return library_iterator(LibraryRef(e->getRawDataRefImpl(), this));
2356 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2357 uint8_t ELFObjectFile<target_endianness, max_alignment, is64Bits>
2358 ::getBytesInAddress() const {
2359 return is64Bits ? 8 : 4;
2362 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2363 StringRef ELFObjectFile<target_endianness, max_alignment, is64Bits>
2364 ::getFileFormatName() const {
2365 switch(Header->e_ident[ELF::EI_CLASS]) {
2366 case ELF::ELFCLASS32:
2367 switch(Header->e_machine) {
2369 return "ELF32-i386";
2370 case ELF::EM_X86_64:
2371 return "ELF32-x86-64";
2374 case ELF::EM_HEXAGON:
2375 return "ELF32-hexagon";
2377 return "ELF32-unknown";
2379 case ELF::ELFCLASS64:
2380 switch(Header->e_machine) {
2382 return "ELF64-i386";
2383 case ELF::EM_X86_64:
2384 return "ELF64-x86-64";
2386 return "ELF64-ppc64";
2388 return "ELF64-unknown";
2391 // FIXME: Proper error handling.
2392 report_fatal_error("Invalid ELFCLASS!");
2396 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2397 unsigned ELFObjectFile<target_endianness, max_alignment, is64Bits>
2399 switch(Header->e_machine) {
2402 case ELF::EM_X86_64:
2403 return Triple::x86_64;
2406 case ELF::EM_HEXAGON:
2407 return Triple::hexagon;
2409 return (target_endianness == support::little) ?
2410 Triple::mipsel : Triple::mips;
2412 return Triple::ppc64;
2414 return Triple::UnknownArch;
2418 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2419 uint64_t ELFObjectFile<target_endianness, max_alignment, is64Bits>
2420 ::getNumSections() const {
2421 assert(Header && "Header not initialized!");
2422 if (Header->e_shnum == ELF::SHN_UNDEF) {
2423 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
2424 return SectionHeaderTable->sh_size;
2426 return Header->e_shnum;
2429 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2431 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2432 ::getStringTableIndex() const {
2433 if (Header->e_shnum == ELF::SHN_UNDEF) {
2434 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
2435 return SectionHeaderTable->sh_link;
2436 if (Header->e_shstrndx >= getNumSections())
2439 return Header->e_shstrndx;
2442 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2443 template<typename T>
2445 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2446 ::getEntry(uint16_t Section, uint32_t Entry) const {
2447 return getEntry<T>(getSection(Section), Entry);
2450 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2451 template<typename T>
2453 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2454 ::getEntry(const Elf_Shdr * Section, uint32_t Entry) const {
2455 return reinterpret_cast<const T *>(
2457 + Section->sh_offset
2458 + (Entry * Section->sh_entsize));
2461 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2462 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2464 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2465 ::getSymbol(DataRefImpl Symb) const {
2466 return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
2469 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2470 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2472 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2473 ::getDyn(DataRefImpl DynData) const {
2474 return getEntry<Elf_Dyn>(dot_dynamic_sec, DynData.d.a);
2477 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2478 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2480 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2481 ::getRel(DataRefImpl Rel) const {
2482 return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
2485 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2486 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2488 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2489 ::getRela(DataRefImpl Rela) const {
2490 return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
2493 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2494 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2496 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2497 ::getSection(DataRefImpl Symb) const {
2498 const Elf_Shdr *sec = getSection(Symb.d.b);
2499 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
2500 // FIXME: Proper error handling.
2501 report_fatal_error("Invalid symbol table section!");
2505 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2506 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2508 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2509 ::getSection(uint32_t index) const {
2512 if (!SectionHeaderTable || index >= getNumSections())
2513 // FIXME: Proper error handling.
2514 report_fatal_error("Invalid section index!");
2516 return reinterpret_cast<const Elf_Shdr *>(
2517 reinterpret_cast<const char *>(SectionHeaderTable)
2518 + (index * Header->e_shentsize));
2521 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2522 const char *ELFObjectFile<target_endianness, max_alignment, is64Bits>
2523 ::getString(uint32_t section,
2524 ELF::Elf32_Word offset) const {
2525 return getString(getSection(section), offset);
2528 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2529 const char *ELFObjectFile<target_endianness, max_alignment, is64Bits>
2530 ::getString(const Elf_Shdr *section,
2531 ELF::Elf32_Word offset) const {
2532 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
2533 if (offset >= section->sh_size)
2534 // FIXME: Proper error handling.
2535 report_fatal_error("Symbol name offset outside of string table!");
2536 return (const char *)base() + section->sh_offset + offset;
2539 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2540 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2541 ::getSymbolName(const Elf_Shdr *section,
2542 const Elf_Sym *symb,
2543 StringRef &Result) const {
2544 if (symb->st_name == 0) {
2545 const Elf_Shdr *section = getSection(symb);
2549 Result = getString(dot_shstrtab_sec, section->sh_name);
2550 return object_error::success;
2553 if (section == SymbolTableSections[0]) {
2554 // Symbol is in .dynsym, use .dynstr string table
2555 Result = getString(dot_dynstr_sec, symb->st_name);
2557 // Use the default symbol table name section.
2558 Result = getString(dot_strtab_sec, symb->st_name);
2560 return object_error::success;
2563 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2564 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2565 ::getSectionName(const Elf_Shdr *section,
2566 StringRef &Result) const {
2567 Result = StringRef(getString(dot_shstrtab_sec, section->sh_name));
2568 return object_error::success;
2571 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2572 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2573 ::getSymbolVersion(const Elf_Shdr *section,
2574 const Elf_Sym *symb,
2576 bool &IsDefault) const {
2577 // Handle non-dynamic symbols.
2578 if (section != SymbolTableSections[0]) {
2579 // Non-dynamic symbols can have versions in their names
2580 // A name of the form 'foo@V1' indicates version 'V1', non-default.
2581 // A name of the form 'foo@@V2' indicates version 'V2', default version.
2583 error_code ec = getSymbolName(section, symb, Name);
2584 if (ec != object_error::success)
2586 size_t atpos = Name.find('@');
2587 if (atpos == StringRef::npos) {
2590 return object_error::success;
2593 if (atpos < Name.size() && Name[atpos] == '@') {
2599 Version = Name.substr(atpos);
2600 return object_error::success;
2603 // This is a dynamic symbol. Look in the GNU symbol version table.
2604 if (dot_gnu_version_sec == NULL) {
2605 // No version table.
2608 return object_error::success;
2611 // Determine the position in the symbol table of this entry.
2612 const char *sec_start = (const char*)base() + section->sh_offset;
2613 size_t entry_index = ((const char*)symb - sec_start)/section->sh_entsize;
2615 // Get the corresponding version index entry
2616 const Elf_Versym *vs = getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index);
2617 size_t version_index = vs->vs_index & ELF::VERSYM_VERSION;
2619 // Special markers for unversioned symbols.
2620 if (version_index == ELF::VER_NDX_LOCAL ||
2621 version_index == ELF::VER_NDX_GLOBAL) {
2624 return object_error::success;
2627 // Lookup this symbol in the version table
2629 if (version_index >= VersionMap.size() || VersionMap[version_index].isNull())
2630 report_fatal_error("Symbol has version index without corresponding "
2631 "define or reference entry");
2632 const VersionMapEntry &entry = VersionMap[version_index];
2634 // Get the version name string
2636 if (entry.isVerdef()) {
2637 // The first Verdaux entry holds the name.
2638 name_offset = entry.getVerdef()->getAux()->vda_name;
2640 name_offset = entry.getVernaux()->vna_name;
2642 Version = getString(dot_dynstr_sec, name_offset);
2645 if (entry.isVerdef()) {
2646 IsDefault = !(vs->vs_index & ELF::VERSYM_HIDDEN);
2651 return object_error::success;
2654 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2655 inline DynRefImpl<target_endianness, max_alignment, is64Bits>
2656 ::DynRefImpl(DataRefImpl DynP, const OwningType *Owner)
2658 , OwningObject(Owner) {}
2660 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2661 inline bool DynRefImpl<target_endianness, max_alignment, is64Bits>
2662 ::operator==(const DynRefImpl &Other) const {
2663 return DynPimpl == Other.DynPimpl;
2666 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2667 inline bool DynRefImpl<target_endianness, max_alignment, is64Bits>
2668 ::operator <(const DynRefImpl &Other) const {
2669 return DynPimpl < Other.DynPimpl;
2672 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2673 inline error_code DynRefImpl<target_endianness, max_alignment, is64Bits>
2674 ::getNext(DynRefImpl &Result) const {
2675 return OwningObject->getDynNext(DynPimpl, Result);
2678 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2679 inline int64_t DynRefImpl<target_endianness, max_alignment, is64Bits>
2681 return OwningObject->getDyn(DynPimpl)->d_tag;
2684 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2685 inline uint64_t DynRefImpl<target_endianness, max_alignment, is64Bits>
2687 return OwningObject->getDyn(DynPimpl)->d_un.d_val;
2690 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2691 inline uint64_t DynRefImpl<target_endianness, max_alignment, is64Bits>
2693 return OwningObject->getDyn(DynPimpl)->d_un.d_ptr;
2696 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2697 inline DataRefImpl DynRefImpl<target_endianness, max_alignment, is64Bits>
2698 ::getRawDataRefImpl() const {
2702 /// This is a generic interface for retrieving GNU symbol version
2703 /// information from an ELFObjectFile.
2704 static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
2705 const SymbolRef &Sym,
2708 // Little-endian 32-bit
2709 if (const ELFObjectFile<support::little, 4, false> *ELFObj =
2710 dyn_cast<ELFObjectFile<support::little, 4, false> >(Obj))
2711 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2713 // Big-endian 32-bit
2714 if (const ELFObjectFile<support::big, 4, false> *ELFObj =
2715 dyn_cast<ELFObjectFile<support::big, 4, false> >(Obj))
2716 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2718 // Little-endian 64-bit
2719 if (const ELFObjectFile<support::little, 8, true> *ELFObj =
2720 dyn_cast<ELFObjectFile<support::little, 8, true> >(Obj))
2721 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2723 // Big-endian 64-bit
2724 if (const ELFObjectFile<support::big, 8, true> *ELFObj =
2725 dyn_cast<ELFObjectFile<support::big, 8, true> >(Obj))
2726 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2728 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");