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 relocations in a .rel or .rela section.
531 template<class RelocT>
532 class ELFRelocationIterator {
534 typedef void difference_type;
535 typedef const RelocT 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 ELFRelocationIterator() : Section(0), Current(0) {}
542 ELFRelocationIterator(const Elf_Shdr *Sec, const char *Start)
546 reference operator *() {
547 assert(Current && "Attempted to dereference an invalid iterator!");
548 return *reinterpret_cast<const RelocT*>(Current);
551 pointer operator ->() {
552 assert(Current && "Attempted to dereference an invalid iterator!");
553 return reinterpret_cast<const RelocT*>(Current);
556 bool operator ==(const ELFRelocationIterator &Other) {
557 return Section == Other.Section && Current == Other.Current;
560 bool operator !=(const ELFRelocationIterator &Other) {
561 return !(*this == Other);
564 ELFRelocationIterator &operator ++(int) {
565 assert(Current && "Attempted to increment an invalid iterator!");
566 Current += Section->sh_entsize;
570 ELFRelocationIterator operator ++() {
571 ELFRelocationIterator Tmp = *this;
577 const Elf_Shdr *Section;
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 ELFRelocationIterator<Elf_Rela> Elf_Rela_Iter;
721 typedef ELFRelocationIterator<Elf_Rel> Elf_Rel_Iter;
723 virtual Elf_Rela_Iter beginELFRela(const Elf_Shdr *sec) const {
724 return Elf_Rela_Iter(sec, (const char *)(base() + sec->sh_offset));
727 virtual Elf_Rela_Iter endELFRela(const Elf_Shdr *sec) const {
728 return Elf_Rela_Iter(sec, (const char *)
729 (base() + sec->sh_offset + sec->sh_size));
732 virtual Elf_Rel_Iter beginELFRel(const Elf_Shdr *sec) const {
733 return Elf_Rel_Iter(sec, (const char *)(base() + sec->sh_offset));
736 virtual Elf_Rel_Iter endELFRel(const Elf_Shdr *sec) const {
737 return Elf_Rel_Iter(sec, (const char *)
738 (base() + sec->sh_offset + sec->sh_size));
741 virtual uint8_t getBytesInAddress() const;
742 virtual StringRef getFileFormatName() const;
743 virtual StringRef getObjectType() const { return "ELF"; }
744 virtual unsigned getArch() const;
745 virtual StringRef getLoadName() const;
746 virtual error_code getSectionContents(const Elf_Shdr *sec,
747 StringRef &Res) const;
749 uint64_t getNumSections() const;
750 uint64_t getStringTableIndex() const;
751 ELF::Elf64_Word getSymbolTableIndex(const Elf_Sym *symb) const;
752 const Elf_Shdr *getSection(const Elf_Sym *symb) const;
753 const Elf_Shdr *getElfSection(section_iterator &It) const;
754 const Elf_Sym *getElfSymbol(symbol_iterator &It) const;
755 const Elf_Sym *getElfSymbol(uint32_t index) const;
757 // Methods for type inquiry through isa, cast, and dyn_cast
758 bool isDyldType() const { return isDyldELFObject; }
759 static inline bool classof(const Binary *v) {
760 return v->getType() == getELFType(target_endianness == support::little,
765 // Iterate through the version definitions, and place each Elf_Verdef
766 // in the VersionMap according to its index.
767 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
768 void ELFObjectFile<target_endianness, max_alignment, is64Bits>::
769 LoadVersionDefs(const Elf_Shdr *sec) const {
770 unsigned vd_size = sec->sh_size; // Size of section in bytes
771 unsigned vd_count = sec->sh_info; // Number of Verdef entries
772 const char *sec_start = (const char*)base() + sec->sh_offset;
773 const char *sec_end = sec_start + vd_size;
774 // The first Verdef entry is at the start of the section.
775 const char *p = sec_start;
776 for (unsigned i = 0; i < vd_count; i++) {
777 if (p + sizeof(Elf_Verdef) > sec_end)
778 report_fatal_error("Section ended unexpectedly while scanning "
779 "version definitions.");
780 const Elf_Verdef *vd = reinterpret_cast<const Elf_Verdef *>(p);
781 if (vd->vd_version != ELF::VER_DEF_CURRENT)
782 report_fatal_error("Unexpected verdef version");
783 size_t index = vd->vd_ndx & ELF::VERSYM_VERSION;
784 if (index >= VersionMap.size())
785 VersionMap.resize(index+1);
786 VersionMap[index] = VersionMapEntry(vd);
791 // Iterate through the versions needed section, and place each Elf_Vernaux
792 // in the VersionMap according to its index.
793 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
794 void ELFObjectFile<target_endianness, max_alignment, is64Bits>::
795 LoadVersionNeeds(const Elf_Shdr *sec) const {
796 unsigned vn_size = sec->sh_size; // Size of section in bytes
797 unsigned vn_count = sec->sh_info; // Number of Verneed entries
798 const char *sec_start = (const char*)base() + sec->sh_offset;
799 const char *sec_end = sec_start + vn_size;
800 // The first Verneed entry is at the start of the section.
801 const char *p = sec_start;
802 for (unsigned i = 0; i < vn_count; i++) {
803 if (p + sizeof(Elf_Verneed) > sec_end)
804 report_fatal_error("Section ended unexpectedly while scanning "
805 "version needed records.");
806 const Elf_Verneed *vn = reinterpret_cast<const Elf_Verneed *>(p);
807 if (vn->vn_version != ELF::VER_NEED_CURRENT)
808 report_fatal_error("Unexpected verneed version");
809 // Iterate through the Vernaux entries
810 const char *paux = p + vn->vn_aux;
811 for (unsigned j = 0; j < vn->vn_cnt; j++) {
812 if (paux + sizeof(Elf_Vernaux) > sec_end)
813 report_fatal_error("Section ended unexpected while scanning auxiliary "
814 "version needed records.");
815 const Elf_Vernaux *vna = reinterpret_cast<const Elf_Vernaux *>(paux);
816 size_t index = vna->vna_other & ELF::VERSYM_VERSION;
817 if (index >= VersionMap.size())
818 VersionMap.resize(index+1);
819 VersionMap[index] = VersionMapEntry(vna);
820 paux += vna->vna_next;
826 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
827 void ELFObjectFile<target_endianness, max_alignment, is64Bits>
828 ::LoadVersionMap() const {
829 // If there is no dynamic symtab or version table, there is nothing to do.
830 if (SymbolTableSections[0] == NULL || dot_gnu_version_sec == NULL)
833 // Has the VersionMap already been loaded?
834 if (VersionMap.size() > 0)
837 // The first two version indexes are reserved.
838 // Index 0 is LOCAL, index 1 is GLOBAL.
839 VersionMap.push_back(VersionMapEntry());
840 VersionMap.push_back(VersionMapEntry());
842 if (dot_gnu_version_d_sec)
843 LoadVersionDefs(dot_gnu_version_d_sec);
845 if (dot_gnu_version_r_sec)
846 LoadVersionNeeds(dot_gnu_version_r_sec);
849 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
850 void ELFObjectFile<target_endianness, max_alignment, is64Bits>
851 ::validateSymbol(DataRefImpl Symb) const {
852 const Elf_Sym *symb = getSymbol(Symb);
853 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
854 // FIXME: We really need to do proper error handling in the case of an invalid
855 // input file. Because we don't use exceptions, I think we'll just pass
856 // an error object around.
858 && SymbolTableSection
859 && symb >= (const Elf_Sym*)(base()
860 + SymbolTableSection->sh_offset)
861 && symb < (const Elf_Sym*)(base()
862 + SymbolTableSection->sh_offset
863 + SymbolTableSection->sh_size)))
864 // FIXME: Proper error handling.
865 report_fatal_error("Symb must point to a valid symbol!");
868 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
869 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
870 ::getSymbolNext(DataRefImpl Symb,
871 SymbolRef &Result) const {
872 validateSymbol(Symb);
873 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
876 // Check to see if we are at the end of this symbol table.
877 if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
878 // We are at the end. If there are other symbol tables, jump to them.
879 // If the symbol table is .dynsym, we are iterating dynamic symbols,
880 // and there is only one table of these.
883 Symb.d.a = 1; // The 0th symbol in ELF is fake.
885 // Otherwise return the terminator.
886 if (Symb.d.b == 0 || Symb.d.b >= SymbolTableSections.size()) {
887 Symb.d.a = std::numeric_limits<uint32_t>::max();
888 Symb.d.b = std::numeric_limits<uint32_t>::max();
892 Result = SymbolRef(Symb, this);
893 return object_error::success;
896 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
897 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
898 ::getSymbolName(DataRefImpl Symb,
899 StringRef &Result) const {
900 validateSymbol(Symb);
901 const Elf_Sym *symb = getSymbol(Symb);
902 return getSymbolName(SymbolTableSections[Symb.d.b], symb, Result);
905 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
906 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
907 ::getSymbolVersion(SymbolRef SymRef,
909 bool &IsDefault) const {
910 DataRefImpl Symb = SymRef.getRawDataRefImpl();
911 validateSymbol(Symb);
912 const Elf_Sym *symb = getSymbol(Symb);
913 return getSymbolVersion(SymbolTableSections[Symb.d.b], symb,
917 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
918 ELF::Elf64_Word ELFObjectFile<target_endianness, max_alignment, is64Bits>
919 ::getSymbolTableIndex(const Elf_Sym *symb) const {
920 if (symb->st_shndx == ELF::SHN_XINDEX)
921 return ExtendedSymbolTable.lookup(symb);
922 return symb->st_shndx;
925 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
926 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
928 ELFObjectFile<target_endianness, max_alignment, is64Bits>
929 ::getSection(const Elf_Sym *symb) const {
930 if (symb->st_shndx == ELF::SHN_XINDEX)
931 return getSection(ExtendedSymbolTable.lookup(symb));
932 if (symb->st_shndx >= ELF::SHN_LORESERVE)
934 return getSection(symb->st_shndx);
937 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
938 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
940 ELFObjectFile<target_endianness, max_alignment, is64Bits>
941 ::getElfSection(section_iterator &It) const {
942 llvm::object::DataRefImpl ShdrRef = It->getRawDataRefImpl();
943 return reinterpret_cast<const Elf_Shdr *>(ShdrRef.p);
946 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
947 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
949 ELFObjectFile<target_endianness, max_alignment, is64Bits>
950 ::getElfSymbol(symbol_iterator &It) const {
951 return getSymbol(It->getRawDataRefImpl());
954 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
955 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
957 ELFObjectFile<target_endianness, max_alignment, is64Bits>
958 ::getElfSymbol(uint32_t index) const {
959 DataRefImpl SymbolData;
960 SymbolData.d.a = index;
962 return getSymbol(SymbolData);
965 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
966 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
967 ::getSymbolFileOffset(DataRefImpl Symb,
968 uint64_t &Result) const {
969 validateSymbol(Symb);
970 const Elf_Sym *symb = getSymbol(Symb);
971 const Elf_Shdr *Section;
972 switch (getSymbolTableIndex(symb)) {
973 case ELF::SHN_COMMON:
974 // Unintialized symbols have no offset in the object file
976 Result = UnknownAddressOrSize;
977 return object_error::success;
979 Result = symb->st_value;
980 return object_error::success;
981 default: Section = getSection(symb);
984 switch (symb->getType()) {
985 case ELF::STT_SECTION:
986 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
987 return object_error::success;
989 case ELF::STT_OBJECT:
990 case ELF::STT_NOTYPE:
991 Result = symb->st_value +
992 (Section ? Section->sh_offset : 0);
993 return object_error::success;
995 Result = UnknownAddressOrSize;
996 return object_error::success;
1000 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1001 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1002 ::getSymbolAddress(DataRefImpl Symb,
1003 uint64_t &Result) const {
1004 validateSymbol(Symb);
1005 const Elf_Sym *symb = getSymbol(Symb);
1006 const Elf_Shdr *Section;
1007 switch (getSymbolTableIndex(symb)) {
1008 case ELF::SHN_COMMON:
1009 case ELF::SHN_UNDEF:
1010 Result = UnknownAddressOrSize;
1011 return object_error::success;
1013 Result = symb->st_value;
1014 return object_error::success;
1015 default: Section = getSection(symb);
1018 switch (symb->getType()) {
1019 case ELF::STT_SECTION:
1020 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
1021 return object_error::success;
1023 case ELF::STT_OBJECT:
1024 case ELF::STT_NOTYPE:
1026 switch(Header->e_type) {
1029 IsRelocatable = false;
1032 IsRelocatable = true;
1034 Result = symb->st_value;
1035 if (IsRelocatable && Section != 0)
1036 Result += Section->sh_addr;
1037 return object_error::success;
1039 Result = UnknownAddressOrSize;
1040 return object_error::success;
1044 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1045 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1046 ::getSymbolSize(DataRefImpl Symb,
1047 uint64_t &Result) const {
1048 validateSymbol(Symb);
1049 const Elf_Sym *symb = getSymbol(Symb);
1050 if (symb->st_size == 0)
1051 Result = UnknownAddressOrSize;
1052 Result = symb->st_size;
1053 return object_error::success;
1056 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1057 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1058 ::getSymbolNMTypeChar(DataRefImpl Symb,
1059 char &Result) const {
1060 validateSymbol(Symb);
1061 const Elf_Sym *symb = getSymbol(Symb);
1062 const Elf_Shdr *Section = getSection(symb);
1067 switch (Section->sh_type) {
1068 case ELF::SHT_PROGBITS:
1069 case ELF::SHT_DYNAMIC:
1070 switch (Section->sh_flags) {
1071 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
1073 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
1075 case ELF::SHF_ALLOC:
1076 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
1077 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
1081 case ELF::SHT_NOBITS: ret = 'b';
1085 switch (getSymbolTableIndex(symb)) {
1086 case ELF::SHN_UNDEF:
1090 case ELF::SHN_ABS: ret = 'a'; break;
1091 case ELF::SHN_COMMON: ret = 'c'; break;
1094 switch (symb->getBinding()) {
1095 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
1097 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1100 if (symb->getType() == ELF::STT_OBJECT)
1106 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
1108 if (error_code ec = getSymbolName(Symb, name))
1110 Result = StringSwitch<char>(name)
1111 .StartsWith(".debug", 'N')
1112 .StartsWith(".note", 'n')
1114 return object_error::success;
1118 return object_error::success;
1121 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1122 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1123 ::getSymbolType(DataRefImpl Symb,
1124 SymbolRef::Type &Result) const {
1125 validateSymbol(Symb);
1126 const Elf_Sym *symb = getSymbol(Symb);
1128 switch (symb->getType()) {
1129 case ELF::STT_NOTYPE:
1130 Result = SymbolRef::ST_Unknown;
1132 case ELF::STT_SECTION:
1133 Result = SymbolRef::ST_Debug;
1136 Result = SymbolRef::ST_File;
1139 Result = SymbolRef::ST_Function;
1141 case ELF::STT_OBJECT:
1142 case ELF::STT_COMMON:
1144 Result = SymbolRef::ST_Data;
1147 Result = SymbolRef::ST_Other;
1150 return object_error::success;
1153 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1154 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1155 ::getSymbolFlags(DataRefImpl Symb,
1156 uint32_t &Result) const {
1157 validateSymbol(Symb);
1158 const Elf_Sym *symb = getSymbol(Symb);
1160 Result = SymbolRef::SF_None;
1162 if (symb->getBinding() != ELF::STB_LOCAL)
1163 Result |= SymbolRef::SF_Global;
1165 if (symb->getBinding() == ELF::STB_WEAK)
1166 Result |= SymbolRef::SF_Weak;
1168 if (symb->st_shndx == ELF::SHN_ABS)
1169 Result |= SymbolRef::SF_Absolute;
1171 if (symb->getType() == ELF::STT_FILE ||
1172 symb->getType() == ELF::STT_SECTION)
1173 Result |= SymbolRef::SF_FormatSpecific;
1175 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1176 Result |= SymbolRef::SF_Undefined;
1178 if (symb->getType() == ELF::STT_COMMON ||
1179 getSymbolTableIndex(symb) == ELF::SHN_COMMON)
1180 Result |= SymbolRef::SF_Common;
1182 if (symb->getType() == ELF::STT_TLS)
1183 Result |= SymbolRef::SF_ThreadLocal;
1185 return object_error::success;
1188 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1189 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1190 ::getSymbolSection(DataRefImpl Symb,
1191 section_iterator &Res) const {
1192 validateSymbol(Symb);
1193 const Elf_Sym *symb = getSymbol(Symb);
1194 const Elf_Shdr *sec = getSection(symb);
1196 Res = end_sections();
1199 Sec.p = reinterpret_cast<intptr_t>(sec);
1200 Res = section_iterator(SectionRef(Sec, this));
1202 return object_error::success;
1205 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1206 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1207 ::getSymbolValue(DataRefImpl Symb,
1208 uint64_t &Val) const {
1209 validateSymbol(Symb);
1210 const Elf_Sym *symb = getSymbol(Symb);
1211 Val = symb->st_value;
1212 return object_error::success;
1215 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1216 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1217 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
1218 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
1219 sec += Header->e_shentsize;
1220 Sec.p = reinterpret_cast<intptr_t>(sec);
1221 Result = SectionRef(Sec, this);
1222 return object_error::success;
1225 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1226 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1227 ::getSectionName(DataRefImpl Sec,
1228 StringRef &Result) const {
1229 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1230 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
1231 return object_error::success;
1234 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1235 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1236 ::getSectionAddress(DataRefImpl Sec,
1237 uint64_t &Result) const {
1238 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1239 Result = sec->sh_addr;
1240 return object_error::success;
1243 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1244 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1245 ::getSectionSize(DataRefImpl Sec,
1246 uint64_t &Result) const {
1247 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1248 Result = sec->sh_size;
1249 return object_error::success;
1252 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1253 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1254 ::getSectionContents(DataRefImpl Sec,
1255 StringRef &Result) const {
1256 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1257 const char *start = (const char*)base() + sec->sh_offset;
1258 Result = StringRef(start, sec->sh_size);
1259 return object_error::success;
1262 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1263 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1264 ::getSectionContents(const Elf_Shdr *Sec,
1265 StringRef &Result) const {
1266 const char *start = (const char*)base() + Sec->sh_offset;
1267 Result = StringRef(start, Sec->sh_size);
1268 return object_error::success;
1271 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1272 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1273 ::getSectionAlignment(DataRefImpl Sec,
1274 uint64_t &Result) const {
1275 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1276 Result = sec->sh_addralign;
1277 return object_error::success;
1280 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1281 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1282 ::isSectionText(DataRefImpl Sec,
1283 bool &Result) const {
1284 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1285 if (sec->sh_flags & ELF::SHF_EXECINSTR)
1289 return object_error::success;
1292 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1293 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1294 ::isSectionData(DataRefImpl Sec,
1295 bool &Result) const {
1296 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1297 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1298 && sec->sh_type == ELF::SHT_PROGBITS)
1302 return object_error::success;
1305 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1306 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1307 ::isSectionBSS(DataRefImpl Sec,
1308 bool &Result) const {
1309 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1310 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1311 && sec->sh_type == ELF::SHT_NOBITS)
1315 return object_error::success;
1318 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1319 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1320 ::isSectionRequiredForExecution(DataRefImpl Sec,
1321 bool &Result) const {
1322 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1323 if (sec->sh_flags & ELF::SHF_ALLOC)
1327 return object_error::success;
1330 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1331 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1332 ::isSectionVirtual(DataRefImpl Sec,
1333 bool &Result) const {
1334 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1335 if (sec->sh_type == ELF::SHT_NOBITS)
1339 return object_error::success;
1342 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1343 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1344 ::isSectionZeroInit(DataRefImpl Sec,
1345 bool &Result) const {
1346 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1347 // For ELF, all zero-init sections are virtual (that is, they occupy no space
1348 // in the object image) and vice versa.
1349 Result = sec->sh_type == ELF::SHT_NOBITS;
1350 return object_error::success;
1353 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1354 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1355 ::isSectionReadOnlyData(DataRefImpl Sec,
1356 bool &Result) const {
1357 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1358 if (sec->sh_flags & ELF::SHF_WRITE || sec->sh_flags & ELF::SHF_EXECINSTR)
1362 return object_error::success;
1365 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1366 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1367 ::sectionContainsSymbol(DataRefImpl Sec,
1369 bool &Result) const {
1370 // FIXME: Unimplemented.
1372 return object_error::success;
1375 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1376 relocation_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
1377 ::getSectionRelBegin(DataRefImpl Sec) const {
1378 DataRefImpl RelData;
1379 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1380 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1381 if (sec != 0 && ittr != SectionRelocMap.end()) {
1382 RelData.w.a = getSection(ittr->second[0])->sh_info;
1383 RelData.w.b = ittr->second[0];
1386 return relocation_iterator(RelocationRef(RelData, this));
1389 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1390 relocation_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
1391 ::getSectionRelEnd(DataRefImpl Sec) const {
1392 DataRefImpl RelData;
1393 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1394 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1395 if (sec != 0 && ittr != SectionRelocMap.end()) {
1396 // Get the index of the last relocation section for this section.
1397 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
1398 const Elf_Shdr *relocsec = getSection(relocsecindex);
1399 RelData.w.a = relocsec->sh_info;
1400 RelData.w.b = relocsecindex;
1401 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
1403 return relocation_iterator(RelocationRef(RelData, this));
1407 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1408 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1409 ::getRelocationNext(DataRefImpl Rel,
1410 RelocationRef &Result) const {
1412 const Elf_Shdr *relocsec = getSection(Rel.w.b);
1413 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
1414 // We have reached the end of the relocations for this section. See if there
1415 // is another relocation section.
1416 typename RelocMap_t::mapped_type relocseclist =
1417 SectionRelocMap.lookup(getSection(Rel.w.a));
1419 // Do a binary search for the current reloc section index (which must be
1420 // present). Then get the next one.
1421 typename RelocMap_t::mapped_type::const_iterator loc =
1422 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
1425 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
1426 // to the end iterator.
1427 if (loc != relocseclist.end()) {
1432 Result = RelocationRef(Rel, this);
1433 return object_error::success;
1436 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1437 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1438 ::getRelocationSymbol(DataRefImpl Rel,
1439 SymbolRef &Result) const {
1441 const Elf_Shdr *sec = getSection(Rel.w.b);
1442 switch (sec->sh_type) {
1444 report_fatal_error("Invalid section type in Rel!");
1445 case ELF::SHT_REL : {
1446 symbolIdx = getRel(Rel)->getSymbol();
1449 case ELF::SHT_RELA : {
1450 symbolIdx = getRela(Rel)->getSymbol();
1454 DataRefImpl SymbolData;
1455 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
1456 if (it == SymbolTableSectionsIndexMap.end())
1457 report_fatal_error("Relocation symbol table not found!");
1458 SymbolData.d.a = symbolIdx;
1459 SymbolData.d.b = it->second;
1460 Result = SymbolRef(SymbolData, this);
1461 return object_error::success;
1464 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1465 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1466 ::getRelocationAddress(DataRefImpl Rel,
1467 uint64_t &Result) const {
1469 const Elf_Shdr *sec = getSection(Rel.w.b);
1470 switch (sec->sh_type) {
1472 report_fatal_error("Invalid section type in Rel!");
1473 case ELF::SHT_REL : {
1474 offset = getRel(Rel)->r_offset;
1477 case ELF::SHT_RELA : {
1478 offset = getRela(Rel)->r_offset;
1484 return object_error::success;
1487 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1488 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1489 ::getRelocationOffset(DataRefImpl Rel,
1490 uint64_t &Result) const {
1492 const Elf_Shdr *sec = getSection(Rel.w.b);
1493 switch (sec->sh_type) {
1495 report_fatal_error("Invalid section type in Rel!");
1496 case ELF::SHT_REL : {
1497 offset = getRel(Rel)->r_offset;
1500 case ELF::SHT_RELA : {
1501 offset = getRela(Rel)->r_offset;
1506 Result = offset - sec->sh_addr;
1507 return object_error::success;
1510 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1511 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1512 ::getRelocationType(DataRefImpl Rel,
1513 uint64_t &Result) const {
1514 const Elf_Shdr *sec = getSection(Rel.w.b);
1515 switch (sec->sh_type) {
1517 report_fatal_error("Invalid section type in Rel!");
1518 case ELF::SHT_REL : {
1519 Result = getRel(Rel)->getType();
1522 case ELF::SHT_RELA : {
1523 Result = getRela(Rel)->getType();
1527 return object_error::success;
1530 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
1531 case ELF::enum: res = #enum; break;
1533 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1534 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1535 ::getRelocationTypeName(DataRefImpl Rel,
1536 SmallVectorImpl<char> &Result) const {
1537 const Elf_Shdr *sec = getSection(Rel.w.b);
1540 switch (sec->sh_type) {
1542 return object_error::parse_failed;
1543 case ELF::SHT_REL : {
1544 type = getRel(Rel)->getType();
1547 case ELF::SHT_RELA : {
1548 type = getRela(Rel)->getType();
1552 switch (Header->e_machine) {
1553 case ELF::EM_X86_64:
1555 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
1556 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
1557 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
1558 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
1559 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
1560 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
1561 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
1562 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
1563 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
1564 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
1565 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
1566 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
1567 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
1568 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
1569 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
1570 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
1571 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
1572 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
1573 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
1574 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
1575 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
1576 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
1577 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
1578 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
1579 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
1580 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
1581 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
1582 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
1583 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
1584 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
1585 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
1586 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
1593 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
1594 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
1595 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
1596 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
1597 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
1598 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
1599 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
1600 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
1601 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
1602 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
1603 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
1604 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
1605 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
1606 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
1607 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
1608 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
1609 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
1610 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
1611 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
1612 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
1613 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
1614 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
1615 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
1616 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
1617 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
1618 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
1619 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
1620 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
1621 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
1622 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
1623 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
1624 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
1625 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
1626 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
1627 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
1628 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
1629 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
1630 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
1631 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
1632 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
1639 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_NONE);
1640 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PC24);
1641 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32);
1642 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32);
1643 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G0);
1644 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS16);
1645 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS12);
1646 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ABS5);
1647 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS8);
1648 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL32);
1649 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_CALL);
1650 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC8);
1651 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BREL_ADJ);
1652 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESC);
1653 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_SWI8);
1654 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_XPC25);
1655 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_XPC22);
1656 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPMOD32);
1657 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPOFF32);
1658 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_TPOFF32);
1659 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_COPY);
1660 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GLOB_DAT);
1661 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP_SLOT);
1662 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_RELATIVE);
1663 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF32);
1664 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_PREL);
1665 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL);
1666 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32);
1667 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_CALL);
1668 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP24);
1669 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP24);
1670 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_ABS);
1671 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_7_0);
1672 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_15_8);
1673 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_23_15);
1674 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SBREL_11_0_NC);
1675 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_19_12_NC);
1676 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_27_20_CK);
1677 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET1);
1678 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL31);
1679 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_V4BX);
1680 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET2);
1681 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PREL31);
1682 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_ABS_NC);
1683 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_ABS);
1684 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_PREL_NC);
1685 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_PREL);
1686 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_ABS_NC);
1687 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_ABS);
1688 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_PREL_NC);
1689 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_PREL);
1690 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP19);
1691 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP6);
1692 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ALU_PREL_11_0);
1693 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC12);
1694 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32_NOI);
1695 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32_NOI);
1696 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0_NC);
1697 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0);
1698 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1_NC);
1699 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1);
1700 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G2);
1701 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G1);
1702 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G2);
1703 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G0);
1704 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G1);
1705 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G2);
1706 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G0);
1707 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G1);
1708 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G2);
1709 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0_NC);
1710 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0);
1711 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1_NC);
1712 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1);
1713 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G2);
1714 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G0);
1715 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G1);
1716 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G2);
1717 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G0);
1718 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G1);
1719 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G2);
1720 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G0);
1721 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G1);
1722 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G2);
1723 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL_NC);
1724 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_BREL);
1725 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL);
1726 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL_NC);
1727 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_BREL);
1728 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL);
1729 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GOTDESC);
1730 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_CALL);
1731 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESCSEQ);
1732 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_CALL);
1733 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32_ABS);
1734 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_ABS);
1735 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_PREL);
1736 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL12);
1737 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF12);
1738 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTRELAX);
1739 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTENTRY);
1740 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTINHERIT);
1741 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP11);
1742 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP8);
1743 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GD32);
1744 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDM32);
1745 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO32);
1746 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE32);
1747 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE32);
1748 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO12);
1749 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE12);
1750 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE12GP);
1751 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_0);
1752 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_1);
1753 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_2);
1754 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_3);
1755 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_4);
1756 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_5);
1757 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_6);
1758 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_7);
1759 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_8);
1760 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_9);
1761 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_10);
1762 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_11);
1763 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_12);
1764 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_13);
1765 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_14);
1766 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_15);
1767 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ME_TOO);
1768 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ16);
1769 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ32);
1774 case ELF::EM_HEXAGON:
1776 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_NONE);
1777 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL);
1778 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL);
1779 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL);
1780 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_LO16);
1781 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HI16);
1782 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32);
1783 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16);
1784 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8);
1785 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_0);
1786 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_1);
1787 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_2);
1788 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_3);
1789 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HL16);
1790 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL);
1791 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL);
1792 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B32_PCREL_X);
1793 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_6_X);
1794 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL_X);
1795 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL_X);
1796 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL_X);
1797 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL_X);
1798 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL_X);
1799 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16_X);
1800 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_12_X);
1801 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_11_X);
1802 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_10_X);
1803 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_9_X);
1804 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8_X);
1805 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_7_X);
1806 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_X);
1807 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_PCREL);
1808 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_COPY);
1809 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GLOB_DAT);
1810 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_JMP_SLOT);
1811 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_RELATIVE);
1812 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_PLT_B22_PCREL);
1813 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_LO16);
1814 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_HI16);
1815 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32);
1816 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_LO16);
1817 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_HI16);
1818 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32);
1819 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16);
1820 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPMOD_32);
1821 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_LO16);
1822 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_HI16);
1823 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32);
1824 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16);
1825 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_PLT_B22_PCREL);
1826 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_LO16);
1827 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_HI16);
1828 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32);
1829 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16);
1830 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_LO16);
1831 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_HI16);
1832 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32);
1833 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_LO16);
1834 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_HI16);
1835 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32);
1836 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16);
1837 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_LO16);
1838 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_HI16);
1839 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32);
1840 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16);
1841 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_PCREL_X);
1842 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32_6_X);
1843 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_16_X);
1844 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_11_X);
1845 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32_6_X);
1846 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16_X);
1847 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_11_X);
1848 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32_6_X);
1849 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16_X);
1850 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_11_X);
1851 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32_6_X);
1852 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16_X);
1853 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_11_X);
1854 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32_6_X);
1855 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_16_X);
1856 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32_6_X);
1857 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16_X);
1858 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_11_X);
1859 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32_6_X);
1860 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16_X);
1861 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_11_X);
1869 Result.append(res.begin(), res.end());
1870 return object_error::success;
1873 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
1875 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1876 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1877 ::getRelocationAdditionalInfo(DataRefImpl Rel,
1878 int64_t &Result) const {
1879 const Elf_Shdr *sec = getSection(Rel.w.b);
1880 switch (sec->sh_type) {
1882 report_fatal_error("Invalid section type in Rel!");
1883 case ELF::SHT_REL : {
1885 return object_error::success;
1887 case ELF::SHT_RELA : {
1888 Result = getRela(Rel)->r_addend;
1889 return object_error::success;
1894 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1895 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1896 ::getRelocationValueString(DataRefImpl Rel,
1897 SmallVectorImpl<char> &Result) const {
1898 const Elf_Shdr *sec = getSection(Rel.w.b);
1902 uint16_t symbol_index = 0;
1903 switch (sec->sh_type) {
1905 return object_error::parse_failed;
1906 case ELF::SHT_REL: {
1907 type = getRel(Rel)->getType();
1908 symbol_index = getRel(Rel)->getSymbol();
1909 // TODO: Read implicit addend from section data.
1912 case ELF::SHT_RELA: {
1913 type = getRela(Rel)->getType();
1914 symbol_index = getRela(Rel)->getSymbol();
1915 addend = getRela(Rel)->r_addend;
1919 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1921 if (error_code ec = getSymbolName(getSection(sec->sh_link), symb, symname))
1923 switch (Header->e_machine) {
1924 case ELF::EM_X86_64:
1926 case ELF::R_X86_64_PC8:
1927 case ELF::R_X86_64_PC16:
1928 case ELF::R_X86_64_PC32: {
1930 raw_string_ostream fmt(fmtbuf);
1931 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1933 Result.append(fmtbuf.begin(), fmtbuf.end());
1936 case ELF::R_X86_64_8:
1937 case ELF::R_X86_64_16:
1938 case ELF::R_X86_64_32:
1939 case ELF::R_X86_64_32S:
1940 case ELF::R_X86_64_64: {
1942 raw_string_ostream fmt(fmtbuf);
1943 fmt << symname << (addend < 0 ? "" : "+") << addend;
1945 Result.append(fmtbuf.begin(), fmtbuf.end());
1953 case ELF::EM_HEXAGON:
1960 Result.append(res.begin(), res.end());
1961 return object_error::success;
1964 // Verify that the last byte in the string table in a null.
1965 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1966 void ELFObjectFile<target_endianness, max_alignment, is64Bits>
1967 ::VerifyStrTab(const Elf_Shdr *sh) const {
1968 const char *strtab = (const char*)base() + sh->sh_offset;
1969 if (strtab[sh->sh_size - 1] != 0)
1970 // FIXME: Proper error handling.
1971 report_fatal_error("String table must end with a null terminator!");
1974 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1975 ELFObjectFile<target_endianness, max_alignment, is64Bits>
1976 ::ELFObjectFile(MemoryBuffer *Object, error_code &ec)
1977 : ObjectFile(getELFType(target_endianness == support::little, is64Bits),
1979 , isDyldELFObject(false)
1980 , SectionHeaderTable(0)
1981 , dot_shstrtab_sec(0)
1984 , dot_dynamic_sec(0)
1985 , dot_gnu_version_sec(0)
1986 , dot_gnu_version_r_sec(0)
1987 , dot_gnu_version_d_sec(0)
1991 const uint64_t FileSize = Data->getBufferSize();
1993 if (sizeof(Elf_Ehdr) > FileSize)
1994 // FIXME: Proper error handling.
1995 report_fatal_error("File too short!");
1997 Header = reinterpret_cast<const Elf_Ehdr *>(base());
1999 if (Header->e_shoff == 0)
2002 const uint64_t SectionTableOffset = Header->e_shoff;
2004 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize)
2005 // FIXME: Proper error handling.
2006 report_fatal_error("Section header table goes past end of file!");
2008 // The getNumSections() call below depends on SectionHeaderTable being set.
2009 SectionHeaderTable =
2010 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
2011 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
2013 if (SectionTableOffset + SectionTableSize > FileSize)
2014 // FIXME: Proper error handling.
2015 report_fatal_error("Section table goes past end of file!");
2017 // To find the symbol tables we walk the section table to find SHT_SYMTAB.
2018 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
2019 const Elf_Shdr* sh = SectionHeaderTable;
2021 // Reserve SymbolTableSections[0] for .dynsym
2022 SymbolTableSections.push_back(NULL);
2024 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
2025 switch (sh->sh_type) {
2026 case ELF::SHT_SYMTAB_SHNDX: {
2027 if (SymbolTableSectionHeaderIndex)
2028 // FIXME: Proper error handling.
2029 report_fatal_error("More than one .symtab_shndx!");
2030 SymbolTableSectionHeaderIndex = sh;
2033 case ELF::SHT_SYMTAB: {
2034 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
2035 SymbolTableSections.push_back(sh);
2038 case ELF::SHT_DYNSYM: {
2039 if (SymbolTableSections[0] != NULL)
2040 // FIXME: Proper error handling.
2041 report_fatal_error("More than one .dynsym!");
2042 SymbolTableSectionsIndexMap[i] = 0;
2043 SymbolTableSections[0] = sh;
2047 case ELF::SHT_RELA: {
2048 SectionRelocMap[getSection(sh->sh_info)].push_back(i);
2051 case ELF::SHT_DYNAMIC: {
2052 if (dot_dynamic_sec != NULL)
2053 // FIXME: Proper error handling.
2054 report_fatal_error("More than one .dynamic!");
2055 dot_dynamic_sec = sh;
2058 case ELF::SHT_GNU_versym: {
2059 if (dot_gnu_version_sec != NULL)
2060 // FIXME: Proper error handling.
2061 report_fatal_error("More than one .gnu.version section!");
2062 dot_gnu_version_sec = sh;
2065 case ELF::SHT_GNU_verdef: {
2066 if (dot_gnu_version_d_sec != NULL)
2067 // FIXME: Proper error handling.
2068 report_fatal_error("More than one .gnu.version_d section!");
2069 dot_gnu_version_d_sec = sh;
2072 case ELF::SHT_GNU_verneed: {
2073 if (dot_gnu_version_r_sec != NULL)
2074 // FIXME: Proper error handling.
2075 report_fatal_error("More than one .gnu.version_r section!");
2076 dot_gnu_version_r_sec = sh;
2083 // Sort section relocation lists by index.
2084 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
2085 e = SectionRelocMap.end(); i != e; ++i) {
2086 std::sort(i->second.begin(), i->second.end());
2089 // Get string table sections.
2090 dot_shstrtab_sec = getSection(getStringTableIndex());
2091 if (dot_shstrtab_sec) {
2092 // Verify that the last byte in the string table in a null.
2093 VerifyStrTab(dot_shstrtab_sec);
2096 // Merge this into the above loop.
2097 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
2098 *e = i + getNumSections() * Header->e_shentsize;
2099 i != e; i += Header->e_shentsize) {
2100 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
2101 if (sh->sh_type == ELF::SHT_STRTAB) {
2102 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
2103 if (SectionName == ".strtab") {
2104 if (dot_strtab_sec != 0)
2105 // FIXME: Proper error handling.
2106 report_fatal_error("Already found section named .strtab!");
2107 dot_strtab_sec = sh;
2108 VerifyStrTab(dot_strtab_sec);
2109 } else if (SectionName == ".dynstr") {
2110 if (dot_dynstr_sec != 0)
2111 // FIXME: Proper error handling.
2112 report_fatal_error("Already found section named .dynstr!");
2113 dot_dynstr_sec = sh;
2114 VerifyStrTab(dot_dynstr_sec);
2119 // Build symbol name side-mapping if there is one.
2120 if (SymbolTableSectionHeaderIndex) {
2121 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
2122 SymbolTableSectionHeaderIndex->sh_offset);
2124 for (symbol_iterator si = begin_symbols(),
2125 se = end_symbols(); si != se; si.increment(ec)) {
2127 report_fatal_error("Fewer extended symbol table entries than symbols!");
2128 if (*ShndxTable != ELF::SHN_UNDEF)
2129 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
2135 // Get the symbol table index in the symtab section given a symbol
2136 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2137 uint64_t ELFObjectFile<target_endianness, max_alignment, is64Bits>
2138 ::getSymbolIndex(const Elf_Sym *Sym) const {
2139 assert(SymbolTableSections.size() == 1 && "Only one symbol table supported!");
2140 const Elf_Shdr *SymTab = *SymbolTableSections.begin();
2141 uintptr_t SymLoc = uintptr_t(Sym);
2142 uintptr_t SymTabLoc = uintptr_t(base() + SymTab->sh_offset);
2143 assert(SymLoc > SymTabLoc && "Symbol not in symbol table!");
2144 uint64_t SymOffset = SymLoc - SymTabLoc;
2145 assert(SymOffset % SymTab->sh_entsize == 0 &&
2146 "Symbol not multiple of symbol size!");
2147 return SymOffset / SymTab->sh_entsize;
2150 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2151 symbol_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2152 ::begin_symbols() const {
2153 DataRefImpl SymbolData;
2154 if (SymbolTableSections.size() <= 1) {
2155 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2156 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2158 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2159 SymbolData.d.b = 1; // The 0th table is .dynsym
2161 return symbol_iterator(SymbolRef(SymbolData, this));
2164 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2165 symbol_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2166 ::end_symbols() const {
2167 DataRefImpl SymbolData;
2168 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2169 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2170 return symbol_iterator(SymbolRef(SymbolData, this));
2173 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2174 symbol_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2175 ::begin_dynamic_symbols() const {
2176 DataRefImpl SymbolData;
2177 if (SymbolTableSections[0] == NULL) {
2178 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2179 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2181 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2182 SymbolData.d.b = 0; // The 0th table is .dynsym
2184 return symbol_iterator(SymbolRef(SymbolData, this));
2187 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2188 symbol_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2189 ::end_dynamic_symbols() const {
2190 DataRefImpl SymbolData;
2191 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2192 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2193 return symbol_iterator(SymbolRef(SymbolData, this));
2196 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2197 section_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2198 ::begin_sections() const {
2200 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
2201 return section_iterator(SectionRef(ret, this));
2204 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2205 section_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2206 ::end_sections() const {
2208 ret.p = reinterpret_cast<intptr_t>(base()
2210 + (Header->e_shentsize*getNumSections()));
2211 return section_iterator(SectionRef(ret, this));
2214 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2215 typename ELFObjectFile<target_endianness, max_alignment, is64Bits>::dyn_iterator
2216 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2217 ::begin_dynamic_table() const {
2218 DataRefImpl DynData;
2219 if (dot_dynamic_sec == NULL || dot_dynamic_sec->sh_size == 0) {
2220 DynData.d.a = std::numeric_limits<uint32_t>::max();
2224 return dyn_iterator(DynRef(DynData, this));
2227 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2228 typename ELFObjectFile<target_endianness, max_alignment, is64Bits>::dyn_iterator
2229 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2230 ::end_dynamic_table() const {
2231 DataRefImpl DynData;
2232 DynData.d.a = std::numeric_limits<uint32_t>::max();
2233 return dyn_iterator(DynRef(DynData, this));
2236 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2237 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2238 ::getDynNext(DataRefImpl DynData,
2239 DynRef &Result) const {
2242 // Check to see if we are at the end of .dynamic
2243 if (DynData.d.a >= dot_dynamic_sec->getEntityCount()) {
2244 // We are at the end. Return the terminator.
2245 DynData.d.a = std::numeric_limits<uint32_t>::max();
2248 Result = DynRef(DynData, this);
2249 return object_error::success;
2252 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2254 ELFObjectFile<target_endianness, max_alignment, is64Bits>::getLoadName() const {
2256 // Find the DT_SONAME entry
2257 dyn_iterator it = begin_dynamic_table();
2258 dyn_iterator ie = end_dynamic_table();
2261 if (it->getTag() == ELF::DT_SONAME)
2265 report_fatal_error("dynamic table iteration failed");
2268 if (dot_dynstr_sec == NULL)
2269 report_fatal_error("Dynamic string table is missing");
2270 dt_soname = getString(dot_dynstr_sec, it->getVal());
2278 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2279 library_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2280 ::begin_libraries_needed() const {
2281 // Find the first DT_NEEDED entry
2282 dyn_iterator i = begin_dynamic_table();
2283 dyn_iterator e = end_dynamic_table();
2286 if (i->getTag() == ELF::DT_NEEDED)
2290 report_fatal_error("dynamic table iteration failed");
2292 // Use the same DataRefImpl format as DynRef.
2293 return library_iterator(LibraryRef(i->getRawDataRefImpl(), this));
2296 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2297 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2298 ::getLibraryNext(DataRefImpl Data,
2299 LibraryRef &Result) const {
2300 // Use the same DataRefImpl format as DynRef.
2301 dyn_iterator i = dyn_iterator(DynRef(Data, this));
2302 dyn_iterator e = end_dynamic_table();
2304 // Skip the current dynamic table entry.
2308 // TODO: proper error handling
2310 report_fatal_error("dynamic table iteration failed");
2313 // Find the next DT_NEEDED entry.
2315 if (i->getTag() == ELF::DT_NEEDED)
2319 report_fatal_error("dynamic table iteration failed");
2321 Result = LibraryRef(i->getRawDataRefImpl(), this);
2322 return object_error::success;
2325 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2326 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2327 ::getLibraryPath(DataRefImpl Data, StringRef &Res) const {
2328 dyn_iterator i = dyn_iterator(DynRef(Data, this));
2329 if (i == end_dynamic_table())
2330 report_fatal_error("getLibraryPath() called on iterator end");
2332 if (i->getTag() != ELF::DT_NEEDED)
2333 report_fatal_error("Invalid library_iterator");
2335 // This uses .dynstr to lookup the name of the DT_NEEDED entry.
2336 // THis works as long as DT_STRTAB == .dynstr. This is true most of
2337 // the time, but the specification allows exceptions.
2338 // TODO: This should really use DT_STRTAB instead. Doing this requires
2339 // reading the program headers.
2340 if (dot_dynstr_sec == NULL)
2341 report_fatal_error("Dynamic string table is missing");
2342 Res = getString(dot_dynstr_sec, i->getVal());
2343 return object_error::success;
2346 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2347 library_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2348 ::end_libraries_needed() const {
2349 dyn_iterator e = end_dynamic_table();
2350 // Use the same DataRefImpl format as DynRef.
2351 return library_iterator(LibraryRef(e->getRawDataRefImpl(), this));
2354 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2355 uint8_t ELFObjectFile<target_endianness, max_alignment, is64Bits>
2356 ::getBytesInAddress() const {
2357 return is64Bits ? 8 : 4;
2360 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2361 StringRef ELFObjectFile<target_endianness, max_alignment, is64Bits>
2362 ::getFileFormatName() const {
2363 switch(Header->e_ident[ELF::EI_CLASS]) {
2364 case ELF::ELFCLASS32:
2365 switch(Header->e_machine) {
2367 return "ELF32-i386";
2368 case ELF::EM_X86_64:
2369 return "ELF32-x86-64";
2372 case ELF::EM_HEXAGON:
2373 return "ELF32-hexagon";
2375 return "ELF32-unknown";
2377 case ELF::ELFCLASS64:
2378 switch(Header->e_machine) {
2380 return "ELF64-i386";
2381 case ELF::EM_X86_64:
2382 return "ELF64-x86-64";
2384 return "ELF64-ppc64";
2386 return "ELF64-unknown";
2389 // FIXME: Proper error handling.
2390 report_fatal_error("Invalid ELFCLASS!");
2394 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2395 unsigned ELFObjectFile<target_endianness, max_alignment, is64Bits>
2397 switch(Header->e_machine) {
2400 case ELF::EM_X86_64:
2401 return Triple::x86_64;
2404 case ELF::EM_HEXAGON:
2405 return Triple::hexagon;
2407 return (target_endianness == support::little) ?
2408 Triple::mipsel : Triple::mips;
2410 return Triple::ppc64;
2412 return Triple::UnknownArch;
2416 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2417 uint64_t ELFObjectFile<target_endianness, max_alignment, is64Bits>
2418 ::getNumSections() const {
2419 assert(Header && "Header not initialized!");
2420 if (Header->e_shnum == ELF::SHN_UNDEF) {
2421 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
2422 return SectionHeaderTable->sh_size;
2424 return Header->e_shnum;
2427 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2429 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2430 ::getStringTableIndex() const {
2431 if (Header->e_shnum == ELF::SHN_UNDEF) {
2432 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
2433 return SectionHeaderTable->sh_link;
2434 if (Header->e_shstrndx >= getNumSections())
2437 return Header->e_shstrndx;
2440 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2441 template<typename T>
2443 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2444 ::getEntry(uint16_t Section, uint32_t Entry) const {
2445 return getEntry<T>(getSection(Section), Entry);
2448 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2449 template<typename T>
2451 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2452 ::getEntry(const Elf_Shdr * Section, uint32_t Entry) const {
2453 return reinterpret_cast<const T *>(
2455 + Section->sh_offset
2456 + (Entry * Section->sh_entsize));
2459 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2460 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2462 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2463 ::getSymbol(DataRefImpl Symb) const {
2464 return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
2467 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2468 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2470 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2471 ::getDyn(DataRefImpl DynData) const {
2472 return getEntry<Elf_Dyn>(dot_dynamic_sec, DynData.d.a);
2475 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2476 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2478 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2479 ::getRel(DataRefImpl Rel) const {
2480 return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
2483 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2484 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2486 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2487 ::getRela(DataRefImpl Rela) const {
2488 return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
2491 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2492 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2494 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2495 ::getSection(DataRefImpl Symb) const {
2496 const Elf_Shdr *sec = getSection(Symb.d.b);
2497 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
2498 // FIXME: Proper error handling.
2499 report_fatal_error("Invalid symbol table section!");
2503 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2504 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2506 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2507 ::getSection(uint32_t index) const {
2510 if (!SectionHeaderTable || index >= getNumSections())
2511 // FIXME: Proper error handling.
2512 report_fatal_error("Invalid section index!");
2514 return reinterpret_cast<const Elf_Shdr *>(
2515 reinterpret_cast<const char *>(SectionHeaderTable)
2516 + (index * Header->e_shentsize));
2519 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2520 const char *ELFObjectFile<target_endianness, max_alignment, is64Bits>
2521 ::getString(uint32_t section,
2522 ELF::Elf32_Word offset) const {
2523 return getString(getSection(section), offset);
2526 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2527 const char *ELFObjectFile<target_endianness, max_alignment, is64Bits>
2528 ::getString(const Elf_Shdr *section,
2529 ELF::Elf32_Word offset) const {
2530 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
2531 if (offset >= section->sh_size)
2532 // FIXME: Proper error handling.
2533 report_fatal_error("Symbol name offset outside of string table!");
2534 return (const char *)base() + section->sh_offset + offset;
2537 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2538 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2539 ::getSymbolName(const Elf_Shdr *section,
2540 const Elf_Sym *symb,
2541 StringRef &Result) const {
2542 if (symb->st_name == 0) {
2543 const Elf_Shdr *section = getSection(symb);
2547 Result = getString(dot_shstrtab_sec, section->sh_name);
2548 return object_error::success;
2551 if (section == SymbolTableSections[0]) {
2552 // Symbol is in .dynsym, use .dynstr string table
2553 Result = getString(dot_dynstr_sec, symb->st_name);
2555 // Use the default symbol table name section.
2556 Result = getString(dot_strtab_sec, symb->st_name);
2558 return object_error::success;
2561 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2562 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2563 ::getSectionName(const Elf_Shdr *section,
2564 StringRef &Result) const {
2565 Result = StringRef(getString(dot_shstrtab_sec, section->sh_name));
2566 return object_error::success;
2569 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2570 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2571 ::getSymbolVersion(const Elf_Shdr *section,
2572 const Elf_Sym *symb,
2574 bool &IsDefault) const {
2575 // Handle non-dynamic symbols.
2576 if (section != SymbolTableSections[0]) {
2577 // Non-dynamic symbols can have versions in their names
2578 // A name of the form 'foo@V1' indicates version 'V1', non-default.
2579 // A name of the form 'foo@@V2' indicates version 'V2', default version.
2581 error_code ec = getSymbolName(section, symb, Name);
2582 if (ec != object_error::success)
2584 size_t atpos = Name.find('@');
2585 if (atpos == StringRef::npos) {
2588 return object_error::success;
2591 if (atpos < Name.size() && Name[atpos] == '@') {
2597 Version = Name.substr(atpos);
2598 return object_error::success;
2601 // This is a dynamic symbol. Look in the GNU symbol version table.
2602 if (dot_gnu_version_sec == NULL) {
2603 // No version table.
2606 return object_error::success;
2609 // Determine the position in the symbol table of this entry.
2610 const char *sec_start = (const char*)base() + section->sh_offset;
2611 size_t entry_index = ((const char*)symb - sec_start)/section->sh_entsize;
2613 // Get the corresponding version index entry
2614 const Elf_Versym *vs = getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index);
2615 size_t version_index = vs->vs_index & ELF::VERSYM_VERSION;
2617 // Special markers for unversioned symbols.
2618 if (version_index == ELF::VER_NDX_LOCAL ||
2619 version_index == ELF::VER_NDX_GLOBAL) {
2622 return object_error::success;
2625 // Lookup this symbol in the version table
2627 if (version_index >= VersionMap.size() || VersionMap[version_index].isNull())
2628 report_fatal_error("Symbol has version index without corresponding "
2629 "define or reference entry");
2630 const VersionMapEntry &entry = VersionMap[version_index];
2632 // Get the version name string
2634 if (entry.isVerdef()) {
2635 // The first Verdaux entry holds the name.
2636 name_offset = entry.getVerdef()->getAux()->vda_name;
2638 name_offset = entry.getVernaux()->vna_name;
2640 Version = getString(dot_dynstr_sec, name_offset);
2643 if (entry.isVerdef()) {
2644 IsDefault = !(vs->vs_index & ELF::VERSYM_HIDDEN);
2649 return object_error::success;
2652 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2653 inline DynRefImpl<target_endianness, max_alignment, is64Bits>
2654 ::DynRefImpl(DataRefImpl DynP, const OwningType *Owner)
2656 , OwningObject(Owner) {}
2658 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2659 inline bool DynRefImpl<target_endianness, max_alignment, is64Bits>
2660 ::operator==(const DynRefImpl &Other) const {
2661 return DynPimpl == Other.DynPimpl;
2664 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2665 inline bool DynRefImpl<target_endianness, max_alignment, is64Bits>
2666 ::operator <(const DynRefImpl &Other) const {
2667 return DynPimpl < Other.DynPimpl;
2670 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2671 inline error_code DynRefImpl<target_endianness, max_alignment, is64Bits>
2672 ::getNext(DynRefImpl &Result) const {
2673 return OwningObject->getDynNext(DynPimpl, Result);
2676 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2677 inline int64_t DynRefImpl<target_endianness, max_alignment, is64Bits>
2679 return OwningObject->getDyn(DynPimpl)->d_tag;
2682 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2683 inline uint64_t DynRefImpl<target_endianness, max_alignment, is64Bits>
2685 return OwningObject->getDyn(DynPimpl)->d_un.d_val;
2688 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2689 inline uint64_t DynRefImpl<target_endianness, max_alignment, is64Bits>
2691 return OwningObject->getDyn(DynPimpl)->d_un.d_ptr;
2694 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2695 inline DataRefImpl DynRefImpl<target_endianness, max_alignment, is64Bits>
2696 ::getRawDataRefImpl() const {
2700 /// This is a generic interface for retrieving GNU symbol version
2701 /// information from an ELFObjectFile.
2702 static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
2703 const SymbolRef &Sym,
2706 // Little-endian 32-bit
2707 if (const ELFObjectFile<support::little, 4, false> *ELFObj =
2708 dyn_cast<ELFObjectFile<support::little, 4, false> >(Obj))
2709 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2711 // Big-endian 32-bit
2712 if (const ELFObjectFile<support::big, 4, false> *ELFObj =
2713 dyn_cast<ELFObjectFile<support::big, 4, false> >(Obj))
2714 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2716 // Little-endian 64-bit
2717 if (const ELFObjectFile<support::little, 8, true> *ELFObj =
2718 dyn_cast<ELFObjectFile<support::little, 8, true> >(Obj))
2719 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2721 // Big-endian 64-bit
2722 if (const ELFObjectFile<support::big, 8, true> *ELFObj =
2723 dyn_cast<ELFObjectFile<support::big, 8, true> >(Obj))
2724 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2726 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");