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>
444 struct Elf_Phdr_Impl;
446 template<endianness target_endianness, std::size_t max_alignment>
447 struct Elf_Phdr_Impl<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_Impl<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;
480 typedef Elf_Phdr_Impl<target_endianness, max_alignment, is64Bits> Elf_Phdr;
482 Elf_Rel_Impl<target_endianness, max_alignment, is64Bits, false> Elf_Rel;
484 Elf_Rel_Impl<target_endianness, max_alignment, is64Bits, true> Elf_Rela;
486 Elf_Verdef_Impl<target_endianness, max_alignment, is64Bits> Elf_Verdef;
488 Elf_Verdaux_Impl<target_endianness, max_alignment, is64Bits> Elf_Verdaux;
490 Elf_Verneed_Impl<target_endianness, max_alignment, is64Bits> Elf_Verneed;
492 Elf_Vernaux_Impl<target_endianness, max_alignment, is64Bits> Elf_Vernaux;
494 Elf_Versym_Impl<target_endianness, max_alignment, is64Bits> Elf_Versym;
495 typedef DynRefImpl<target_endianness, max_alignment, is64Bits> DynRef;
496 typedef content_iterator<DynRef> dyn_iterator;
499 // This flag is used for classof, to distinguish ELFObjectFile from
500 // its subclass. If more subclasses will be created, this flag will
501 // have to become an enum.
502 bool isDyldELFObject;
505 typedef SmallVector<const Elf_Shdr*, 1> Sections_t;
506 typedef DenseMap<unsigned, unsigned> IndexMap_t;
507 typedef DenseMap<const Elf_Shdr*, SmallVector<uint32_t, 1> > RelocMap_t;
509 const Elf_Ehdr *Header;
510 const Elf_Shdr *SectionHeaderTable;
511 const Elf_Shdr *dot_shstrtab_sec; // Section header string table.
512 const Elf_Shdr *dot_strtab_sec; // Symbol header string table.
513 const Elf_Shdr *dot_dynstr_sec; // Dynamic symbol string table.
515 // SymbolTableSections[0] always points to the dynamic string table section
516 // header, or NULL if there is no dynamic string table.
517 Sections_t SymbolTableSections;
518 IndexMap_t SymbolTableSectionsIndexMap;
519 DenseMap<const Elf_Sym*, ELF::Elf64_Word> ExtendedSymbolTable;
521 const Elf_Shdr *dot_dynamic_sec; // .dynamic
522 const Elf_Shdr *dot_gnu_version_sec; // .gnu.version
523 const Elf_Shdr *dot_gnu_version_r_sec; // .gnu.version_r
524 const Elf_Shdr *dot_gnu_version_d_sec; // .gnu.version_d
526 // Pointer to SONAME entry in dynamic string table
527 // This is set the first time getLoadName is called.
528 mutable const char *dt_soname;
531 /// \brief Iterate over constant sized entities.
533 class ELFEntityIterator {
535 typedef void difference_type;
536 typedef EntT value_type;
537 typedef std::forward_iterator_tag iterator_category;
538 typedef value_type &reference;
539 typedef value_type *pointer;
541 /// \brief Default construct iterator.
542 ELFEntityIterator() : EntitySize(0), Current(0) {}
543 ELFEntityIterator(uint64_t EntSize, const char *Start)
544 : EntitySize(EntSize)
547 reference operator *() {
548 assert(Current && "Attempted to dereference an invalid iterator!");
549 return *reinterpret_cast<pointer>(Current);
552 pointer operator ->() {
553 assert(Current && "Attempted to dereference an invalid iterator!");
554 return reinterpret_cast<pointer>(Current);
557 bool operator ==(const ELFEntityIterator &Other) {
558 return Current == Other.Current;
561 bool operator !=(const ELFEntityIterator &Other) {
562 return !(*this == Other);
565 ELFEntityIterator &operator ++() {
566 assert(Current && "Attempted to increment an invalid iterator!");
567 Current += EntitySize;
571 ELFEntityIterator operator ++(int) {
572 ELFEntityIterator Tmp = *this;
578 const uint64_t EntitySize;
583 // Records for each version index the corresponding Verdef or Vernaux entry.
584 // This is filled the first time LoadVersionMap() is called.
585 class VersionMapEntry : public PointerIntPair<const void*, 1> {
587 // If the integer is 0, this is an Elf_Verdef*.
588 // If the integer is 1, this is an Elf_Vernaux*.
589 VersionMapEntry() : PointerIntPair<const void*, 1>(NULL, 0) { }
590 VersionMapEntry(const Elf_Verdef *verdef)
591 : PointerIntPair<const void*, 1>(verdef, 0) { }
592 VersionMapEntry(const Elf_Vernaux *vernaux)
593 : PointerIntPair<const void*, 1>(vernaux, 1) { }
594 bool isNull() const { return getPointer() == NULL; }
595 bool isVerdef() const { return !isNull() && getInt() == 0; }
596 bool isVernaux() const { return !isNull() && getInt() == 1; }
597 const Elf_Verdef *getVerdef() const {
598 return isVerdef() ? (const Elf_Verdef*)getPointer() : NULL;
600 const Elf_Vernaux *getVernaux() const {
601 return isVernaux() ? (const Elf_Vernaux*)getPointer() : NULL;
604 mutable SmallVector<VersionMapEntry, 16> VersionMap;
605 void LoadVersionDefs(const Elf_Shdr *sec) const;
606 void LoadVersionNeeds(const Elf_Shdr *ec) const;
607 void LoadVersionMap() const;
609 /// @brief Map sections to an array of relocation sections that reference
610 /// them sorted by section index.
611 RelocMap_t SectionRelocMap;
613 /// @brief Get the relocation section that contains \a Rel.
614 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
615 return getSection(Rel.w.b);
618 bool isRelocationHasAddend(DataRefImpl Rel) const;
620 const T *getEntry(uint16_t Section, uint32_t Entry) const;
622 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
623 const Elf_Shdr *getSection(DataRefImpl index) const;
624 const Elf_Shdr *getSection(uint32_t index) const;
625 const Elf_Rel *getRel(DataRefImpl Rel) const;
626 const Elf_Rela *getRela(DataRefImpl Rela) const;
627 const char *getString(uint32_t section, uint32_t offset) const;
628 const char *getString(const Elf_Shdr *section, uint32_t offset) const;
629 error_code getSymbolVersion(const Elf_Shdr *section,
632 bool &IsDefault) const;
633 void VerifyStrTab(const Elf_Shdr *sh) const;
636 const Elf_Sym *getSymbol(DataRefImpl Symb) const; // FIXME: Should be private?
637 void validateSymbol(DataRefImpl Symb) const;
640 error_code getSymbolName(const Elf_Shdr *section,
642 StringRef &Res) const;
643 error_code getSectionName(const Elf_Shdr *section,
644 StringRef &Res) const;
645 const Elf_Dyn *getDyn(DataRefImpl DynData) const;
646 error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
647 bool &IsDefault) const;
648 uint64_t getSymbolIndex(const Elf_Sym *sym) const;
650 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
651 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
652 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
653 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
654 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
655 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
656 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
657 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
658 virtual error_code getSymbolSection(DataRefImpl Symb,
659 section_iterator &Res) const;
660 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
662 friend class DynRefImpl<target_endianness, max_alignment, is64Bits>;
663 virtual error_code getDynNext(DataRefImpl DynData, DynRef &Result) const;
665 virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const;
666 virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const;
668 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
669 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
670 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
671 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
672 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
673 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
674 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
675 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
676 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
677 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
679 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
680 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
681 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
682 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
684 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
685 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
687 virtual error_code getRelocationNext(DataRefImpl Rel,
688 RelocationRef &Res) const;
689 virtual error_code getRelocationAddress(DataRefImpl Rel,
690 uint64_t &Res) const;
691 virtual error_code getRelocationOffset(DataRefImpl Rel,
692 uint64_t &Res) const;
693 virtual error_code getRelocationSymbol(DataRefImpl Rel,
694 SymbolRef &Res) const;
695 virtual error_code getRelocationType(DataRefImpl Rel,
696 uint64_t &Res) const;
697 virtual error_code getRelocationTypeName(DataRefImpl Rel,
698 SmallVectorImpl<char> &Result) const;
699 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
701 virtual error_code getRelocationValueString(DataRefImpl Rel,
702 SmallVectorImpl<char> &Result) const;
705 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
706 virtual symbol_iterator begin_symbols() const;
707 virtual symbol_iterator end_symbols() const;
709 virtual symbol_iterator begin_dynamic_symbols() const;
710 virtual symbol_iterator end_dynamic_symbols() const;
712 virtual section_iterator begin_sections() const;
713 virtual section_iterator end_sections() const;
715 virtual library_iterator begin_libraries_needed() const;
716 virtual library_iterator end_libraries_needed() const;
718 virtual dyn_iterator begin_dynamic_table() const;
719 virtual dyn_iterator end_dynamic_table() const;
721 typedef ELFEntityIterator<const Elf_Rela> Elf_Rela_Iter;
722 typedef ELFEntityIterator<const Elf_Rel> Elf_Rel_Iter;
724 Elf_Rela_Iter beginELFRela(const Elf_Shdr *sec) const {
725 return Elf_Rela_Iter(sec->sh_entsize,
726 (const char *)(base() + sec->sh_offset));
729 Elf_Rela_Iter endELFRela(const Elf_Shdr *sec) const {
730 return Elf_Rela_Iter(sec->sh_entsize, (const char *)
731 (base() + sec->sh_offset + sec->sh_size));
734 Elf_Rel_Iter beginELFRel(const Elf_Shdr *sec) const {
735 return Elf_Rel_Iter(sec->sh_entsize,
736 (const char *)(base() + sec->sh_offset));
739 Elf_Rel_Iter endELFRel(const Elf_Shdr *sec) const {
740 return Elf_Rel_Iter(sec->sh_entsize, (const char *)
741 (base() + sec->sh_offset + sec->sh_size));
744 /// \brief Iterate over program header table.
745 typedef ELFEntityIterator<const Elf_Phdr> Elf_Phdr_Iter;
747 Elf_Phdr_Iter begin_program_headers() const {
748 return Elf_Phdr_Iter(Header->e_phentsize,
749 (const char*)base() + Header->e_phoff);
752 Elf_Phdr_Iter end_program_headers() const {
753 return Elf_Phdr_Iter(Header->e_phentsize,
754 (const char*)base() +
756 (Header->e_phnum * Header->e_phentsize));
759 virtual uint8_t getBytesInAddress() const;
760 virtual StringRef getFileFormatName() const;
761 virtual StringRef getObjectType() const { return "ELF"; }
762 virtual unsigned getArch() const;
763 virtual StringRef getLoadName() const;
764 virtual error_code getSectionContents(const Elf_Shdr *sec,
765 StringRef &Res) const;
767 uint64_t getNumSections() const;
768 uint64_t getStringTableIndex() const;
769 ELF::Elf64_Word getSymbolTableIndex(const Elf_Sym *symb) const;
770 const Elf_Shdr *getSection(const Elf_Sym *symb) const;
771 const Elf_Shdr *getElfSection(section_iterator &It) const;
772 const Elf_Sym *getElfSymbol(symbol_iterator &It) const;
773 const Elf_Sym *getElfSymbol(uint32_t index) const;
775 // Methods for type inquiry through isa, cast, and dyn_cast
776 bool isDyldType() const { return isDyldELFObject; }
777 static inline bool classof(const Binary *v) {
778 return v->getType() == getELFType(target_endianness == support::little,
783 // Iterate through the version definitions, and place each Elf_Verdef
784 // in the VersionMap according to its index.
785 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
786 void ELFObjectFile<target_endianness, max_alignment, is64Bits>::
787 LoadVersionDefs(const Elf_Shdr *sec) const {
788 unsigned vd_size = sec->sh_size; // Size of section in bytes
789 unsigned vd_count = sec->sh_info; // Number of Verdef entries
790 const char *sec_start = (const char*)base() + sec->sh_offset;
791 const char *sec_end = sec_start + vd_size;
792 // The first Verdef entry is at the start of the section.
793 const char *p = sec_start;
794 for (unsigned i = 0; i < vd_count; i++) {
795 if (p + sizeof(Elf_Verdef) > sec_end)
796 report_fatal_error("Section ended unexpectedly while scanning "
797 "version definitions.");
798 const Elf_Verdef *vd = reinterpret_cast<const Elf_Verdef *>(p);
799 if (vd->vd_version != ELF::VER_DEF_CURRENT)
800 report_fatal_error("Unexpected verdef version");
801 size_t index = vd->vd_ndx & ELF::VERSYM_VERSION;
802 if (index >= VersionMap.size())
803 VersionMap.resize(index+1);
804 VersionMap[index] = VersionMapEntry(vd);
809 // Iterate through the versions needed section, and place each Elf_Vernaux
810 // in the VersionMap according to its index.
811 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
812 void ELFObjectFile<target_endianness, max_alignment, is64Bits>::
813 LoadVersionNeeds(const Elf_Shdr *sec) const {
814 unsigned vn_size = sec->sh_size; // Size of section in bytes
815 unsigned vn_count = sec->sh_info; // Number of Verneed entries
816 const char *sec_start = (const char*)base() + sec->sh_offset;
817 const char *sec_end = sec_start + vn_size;
818 // The first Verneed entry is at the start of the section.
819 const char *p = sec_start;
820 for (unsigned i = 0; i < vn_count; i++) {
821 if (p + sizeof(Elf_Verneed) > sec_end)
822 report_fatal_error("Section ended unexpectedly while scanning "
823 "version needed records.");
824 const Elf_Verneed *vn = reinterpret_cast<const Elf_Verneed *>(p);
825 if (vn->vn_version != ELF::VER_NEED_CURRENT)
826 report_fatal_error("Unexpected verneed version");
827 // Iterate through the Vernaux entries
828 const char *paux = p + vn->vn_aux;
829 for (unsigned j = 0; j < vn->vn_cnt; j++) {
830 if (paux + sizeof(Elf_Vernaux) > sec_end)
831 report_fatal_error("Section ended unexpected while scanning auxiliary "
832 "version needed records.");
833 const Elf_Vernaux *vna = reinterpret_cast<const Elf_Vernaux *>(paux);
834 size_t index = vna->vna_other & ELF::VERSYM_VERSION;
835 if (index >= VersionMap.size())
836 VersionMap.resize(index+1);
837 VersionMap[index] = VersionMapEntry(vna);
838 paux += vna->vna_next;
844 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
845 void ELFObjectFile<target_endianness, max_alignment, is64Bits>
846 ::LoadVersionMap() const {
847 // If there is no dynamic symtab or version table, there is nothing to do.
848 if (SymbolTableSections[0] == NULL || dot_gnu_version_sec == NULL)
851 // Has the VersionMap already been loaded?
852 if (VersionMap.size() > 0)
855 // The first two version indexes are reserved.
856 // Index 0 is LOCAL, index 1 is GLOBAL.
857 VersionMap.push_back(VersionMapEntry());
858 VersionMap.push_back(VersionMapEntry());
860 if (dot_gnu_version_d_sec)
861 LoadVersionDefs(dot_gnu_version_d_sec);
863 if (dot_gnu_version_r_sec)
864 LoadVersionNeeds(dot_gnu_version_r_sec);
867 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
868 void ELFObjectFile<target_endianness, max_alignment, is64Bits>
869 ::validateSymbol(DataRefImpl Symb) const {
870 const Elf_Sym *symb = getSymbol(Symb);
871 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
872 // FIXME: We really need to do proper error handling in the case of an invalid
873 // input file. Because we don't use exceptions, I think we'll just pass
874 // an error object around.
876 && SymbolTableSection
877 && symb >= (const Elf_Sym*)(base()
878 + SymbolTableSection->sh_offset)
879 && symb < (const Elf_Sym*)(base()
880 + SymbolTableSection->sh_offset
881 + SymbolTableSection->sh_size)))
882 // FIXME: Proper error handling.
883 report_fatal_error("Symb must point to a valid symbol!");
886 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
887 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
888 ::getSymbolNext(DataRefImpl Symb,
889 SymbolRef &Result) const {
890 validateSymbol(Symb);
891 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
894 // Check to see if we are at the end of this symbol table.
895 if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
896 // We are at the end. If there are other symbol tables, jump to them.
897 // If the symbol table is .dynsym, we are iterating dynamic symbols,
898 // and there is only one table of these.
901 Symb.d.a = 1; // The 0th symbol in ELF is fake.
903 // Otherwise return the terminator.
904 if (Symb.d.b == 0 || Symb.d.b >= SymbolTableSections.size()) {
905 Symb.d.a = std::numeric_limits<uint32_t>::max();
906 Symb.d.b = std::numeric_limits<uint32_t>::max();
910 Result = SymbolRef(Symb, this);
911 return object_error::success;
914 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
915 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
916 ::getSymbolName(DataRefImpl Symb,
917 StringRef &Result) const {
918 validateSymbol(Symb);
919 const Elf_Sym *symb = getSymbol(Symb);
920 return getSymbolName(SymbolTableSections[Symb.d.b], symb, Result);
923 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
924 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
925 ::getSymbolVersion(SymbolRef SymRef,
927 bool &IsDefault) const {
928 DataRefImpl Symb = SymRef.getRawDataRefImpl();
929 validateSymbol(Symb);
930 const Elf_Sym *symb = getSymbol(Symb);
931 return getSymbolVersion(SymbolTableSections[Symb.d.b], symb,
935 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
936 ELF::Elf64_Word ELFObjectFile<target_endianness, max_alignment, is64Bits>
937 ::getSymbolTableIndex(const Elf_Sym *symb) const {
938 if (symb->st_shndx == ELF::SHN_XINDEX)
939 return ExtendedSymbolTable.lookup(symb);
940 return symb->st_shndx;
943 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
944 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
946 ELFObjectFile<target_endianness, max_alignment, is64Bits>
947 ::getSection(const Elf_Sym *symb) const {
948 if (symb->st_shndx == ELF::SHN_XINDEX)
949 return getSection(ExtendedSymbolTable.lookup(symb));
950 if (symb->st_shndx >= ELF::SHN_LORESERVE)
952 return getSection(symb->st_shndx);
955 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
956 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
958 ELFObjectFile<target_endianness, max_alignment, is64Bits>
959 ::getElfSection(section_iterator &It) const {
960 llvm::object::DataRefImpl ShdrRef = It->getRawDataRefImpl();
961 return reinterpret_cast<const Elf_Shdr *>(ShdrRef.p);
964 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
965 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
967 ELFObjectFile<target_endianness, max_alignment, is64Bits>
968 ::getElfSymbol(symbol_iterator &It) const {
969 return getSymbol(It->getRawDataRefImpl());
972 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
973 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
975 ELFObjectFile<target_endianness, max_alignment, is64Bits>
976 ::getElfSymbol(uint32_t index) const {
977 DataRefImpl SymbolData;
978 SymbolData.d.a = index;
980 return getSymbol(SymbolData);
983 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
984 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
985 ::getSymbolFileOffset(DataRefImpl Symb,
986 uint64_t &Result) const {
987 validateSymbol(Symb);
988 const Elf_Sym *symb = getSymbol(Symb);
989 const Elf_Shdr *Section;
990 switch (getSymbolTableIndex(symb)) {
991 case ELF::SHN_COMMON:
992 // Unintialized symbols have no offset in the object file
994 Result = UnknownAddressOrSize;
995 return object_error::success;
997 Result = symb->st_value;
998 return object_error::success;
999 default: Section = getSection(symb);
1002 switch (symb->getType()) {
1003 case ELF::STT_SECTION:
1004 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
1005 return object_error::success;
1007 case ELF::STT_OBJECT:
1008 case ELF::STT_NOTYPE:
1009 Result = symb->st_value +
1010 (Section ? Section->sh_offset : 0);
1011 return object_error::success;
1013 Result = UnknownAddressOrSize;
1014 return object_error::success;
1018 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1019 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1020 ::getSymbolAddress(DataRefImpl Symb,
1021 uint64_t &Result) const {
1022 validateSymbol(Symb);
1023 const Elf_Sym *symb = getSymbol(Symb);
1024 const Elf_Shdr *Section;
1025 switch (getSymbolTableIndex(symb)) {
1026 case ELF::SHN_COMMON:
1027 case ELF::SHN_UNDEF:
1028 Result = UnknownAddressOrSize;
1029 return object_error::success;
1031 Result = symb->st_value;
1032 return object_error::success;
1033 default: Section = getSection(symb);
1036 switch (symb->getType()) {
1037 case ELF::STT_SECTION:
1038 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
1039 return object_error::success;
1041 case ELF::STT_OBJECT:
1042 case ELF::STT_NOTYPE:
1044 switch(Header->e_type) {
1047 IsRelocatable = false;
1050 IsRelocatable = true;
1052 Result = symb->st_value;
1053 if (IsRelocatable && Section != 0)
1054 Result += Section->sh_addr;
1055 return object_error::success;
1057 Result = UnknownAddressOrSize;
1058 return object_error::success;
1062 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1063 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1064 ::getSymbolSize(DataRefImpl Symb,
1065 uint64_t &Result) const {
1066 validateSymbol(Symb);
1067 const Elf_Sym *symb = getSymbol(Symb);
1068 if (symb->st_size == 0)
1069 Result = UnknownAddressOrSize;
1070 Result = symb->st_size;
1071 return object_error::success;
1074 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1075 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1076 ::getSymbolNMTypeChar(DataRefImpl Symb,
1077 char &Result) const {
1078 validateSymbol(Symb);
1079 const Elf_Sym *symb = getSymbol(Symb);
1080 const Elf_Shdr *Section = getSection(symb);
1085 switch (Section->sh_type) {
1086 case ELF::SHT_PROGBITS:
1087 case ELF::SHT_DYNAMIC:
1088 switch (Section->sh_flags) {
1089 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
1091 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
1093 case ELF::SHF_ALLOC:
1094 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
1095 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
1099 case ELF::SHT_NOBITS: ret = 'b';
1103 switch (getSymbolTableIndex(symb)) {
1104 case ELF::SHN_UNDEF:
1108 case ELF::SHN_ABS: ret = 'a'; break;
1109 case ELF::SHN_COMMON: ret = 'c'; break;
1112 switch (symb->getBinding()) {
1113 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
1115 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1118 if (symb->getType() == ELF::STT_OBJECT)
1124 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
1126 if (error_code ec = getSymbolName(Symb, name))
1128 Result = StringSwitch<char>(name)
1129 .StartsWith(".debug", 'N')
1130 .StartsWith(".note", 'n')
1132 return object_error::success;
1136 return object_error::success;
1139 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1140 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1141 ::getSymbolType(DataRefImpl Symb,
1142 SymbolRef::Type &Result) const {
1143 validateSymbol(Symb);
1144 const Elf_Sym *symb = getSymbol(Symb);
1146 switch (symb->getType()) {
1147 case ELF::STT_NOTYPE:
1148 Result = SymbolRef::ST_Unknown;
1150 case ELF::STT_SECTION:
1151 Result = SymbolRef::ST_Debug;
1154 Result = SymbolRef::ST_File;
1157 Result = SymbolRef::ST_Function;
1159 case ELF::STT_OBJECT:
1160 case ELF::STT_COMMON:
1162 Result = SymbolRef::ST_Data;
1165 Result = SymbolRef::ST_Other;
1168 return object_error::success;
1171 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1172 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1173 ::getSymbolFlags(DataRefImpl Symb,
1174 uint32_t &Result) const {
1175 validateSymbol(Symb);
1176 const Elf_Sym *symb = getSymbol(Symb);
1178 Result = SymbolRef::SF_None;
1180 if (symb->getBinding() != ELF::STB_LOCAL)
1181 Result |= SymbolRef::SF_Global;
1183 if (symb->getBinding() == ELF::STB_WEAK)
1184 Result |= SymbolRef::SF_Weak;
1186 if (symb->st_shndx == ELF::SHN_ABS)
1187 Result |= SymbolRef::SF_Absolute;
1189 if (symb->getType() == ELF::STT_FILE ||
1190 symb->getType() == ELF::STT_SECTION)
1191 Result |= SymbolRef::SF_FormatSpecific;
1193 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1194 Result |= SymbolRef::SF_Undefined;
1196 if (symb->getType() == ELF::STT_COMMON ||
1197 getSymbolTableIndex(symb) == ELF::SHN_COMMON)
1198 Result |= SymbolRef::SF_Common;
1200 if (symb->getType() == ELF::STT_TLS)
1201 Result |= SymbolRef::SF_ThreadLocal;
1203 return object_error::success;
1206 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1207 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1208 ::getSymbolSection(DataRefImpl Symb,
1209 section_iterator &Res) const {
1210 validateSymbol(Symb);
1211 const Elf_Sym *symb = getSymbol(Symb);
1212 const Elf_Shdr *sec = getSection(symb);
1214 Res = end_sections();
1217 Sec.p = reinterpret_cast<intptr_t>(sec);
1218 Res = section_iterator(SectionRef(Sec, this));
1220 return object_error::success;
1223 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1224 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1225 ::getSymbolValue(DataRefImpl Symb,
1226 uint64_t &Val) const {
1227 validateSymbol(Symb);
1228 const Elf_Sym *symb = getSymbol(Symb);
1229 Val = symb->st_value;
1230 return object_error::success;
1233 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1234 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1235 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
1236 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
1237 sec += Header->e_shentsize;
1238 Sec.p = reinterpret_cast<intptr_t>(sec);
1239 Result = SectionRef(Sec, this);
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 ::getSectionName(DataRefImpl Sec,
1246 StringRef &Result) const {
1247 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1248 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
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 ::getSectionAddress(DataRefImpl Sec,
1255 uint64_t &Result) const {
1256 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1257 Result = sec->sh_addr;
1258 return object_error::success;
1261 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1262 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1263 ::getSectionSize(DataRefImpl Sec,
1264 uint64_t &Result) const {
1265 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1266 Result = sec->sh_size;
1267 return object_error::success;
1270 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1271 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1272 ::getSectionContents(DataRefImpl Sec,
1273 StringRef &Result) const {
1274 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1275 const char *start = (const char*)base() + sec->sh_offset;
1276 Result = StringRef(start, sec->sh_size);
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 ::getSectionContents(const Elf_Shdr *Sec,
1283 StringRef &Result) const {
1284 const char *start = (const char*)base() + Sec->sh_offset;
1285 Result = StringRef(start, Sec->sh_size);
1286 return object_error::success;
1289 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1290 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1291 ::getSectionAlignment(DataRefImpl Sec,
1292 uint64_t &Result) const {
1293 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1294 Result = sec->sh_addralign;
1295 return object_error::success;
1298 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1299 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1300 ::isSectionText(DataRefImpl Sec,
1301 bool &Result) const {
1302 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1303 if (sec->sh_flags & ELF::SHF_EXECINSTR)
1307 return object_error::success;
1310 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1311 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1312 ::isSectionData(DataRefImpl Sec,
1313 bool &Result) const {
1314 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1315 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1316 && sec->sh_type == ELF::SHT_PROGBITS)
1320 return object_error::success;
1323 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1324 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1325 ::isSectionBSS(DataRefImpl Sec,
1326 bool &Result) const {
1327 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1328 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1329 && sec->sh_type == ELF::SHT_NOBITS)
1333 return object_error::success;
1336 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1337 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1338 ::isSectionRequiredForExecution(DataRefImpl Sec,
1339 bool &Result) const {
1340 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1341 if (sec->sh_flags & ELF::SHF_ALLOC)
1345 return object_error::success;
1348 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1349 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1350 ::isSectionVirtual(DataRefImpl Sec,
1351 bool &Result) const {
1352 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1353 if (sec->sh_type == ELF::SHT_NOBITS)
1357 return object_error::success;
1360 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1361 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1362 ::isSectionZeroInit(DataRefImpl Sec,
1363 bool &Result) const {
1364 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1365 // For ELF, all zero-init sections are virtual (that is, they occupy no space
1366 // in the object image) and vice versa.
1367 Result = sec->sh_type == ELF::SHT_NOBITS;
1368 return object_error::success;
1371 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1372 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1373 ::isSectionReadOnlyData(DataRefImpl Sec,
1374 bool &Result) const {
1375 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1376 if (sec->sh_flags & ELF::SHF_WRITE || sec->sh_flags & ELF::SHF_EXECINSTR)
1380 return object_error::success;
1383 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1384 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1385 ::sectionContainsSymbol(DataRefImpl Sec,
1387 bool &Result) const {
1388 // FIXME: Unimplemented.
1390 return object_error::success;
1393 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1394 relocation_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
1395 ::getSectionRelBegin(DataRefImpl Sec) const {
1396 DataRefImpl RelData;
1397 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1398 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1399 if (sec != 0 && ittr != SectionRelocMap.end()) {
1400 RelData.w.a = getSection(ittr->second[0])->sh_info;
1401 RelData.w.b = ittr->second[0];
1404 return relocation_iterator(RelocationRef(RelData, this));
1407 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1408 relocation_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
1409 ::getSectionRelEnd(DataRefImpl Sec) const {
1410 DataRefImpl RelData;
1411 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1412 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1413 if (sec != 0 && ittr != SectionRelocMap.end()) {
1414 // Get the index of the last relocation section for this section.
1415 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
1416 const Elf_Shdr *relocsec = getSection(relocsecindex);
1417 RelData.w.a = relocsec->sh_info;
1418 RelData.w.b = relocsecindex;
1419 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
1421 return relocation_iterator(RelocationRef(RelData, this));
1425 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1426 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1427 ::getRelocationNext(DataRefImpl Rel,
1428 RelocationRef &Result) const {
1430 const Elf_Shdr *relocsec = getSection(Rel.w.b);
1431 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
1432 // We have reached the end of the relocations for this section. See if there
1433 // is another relocation section.
1434 typename RelocMap_t::mapped_type relocseclist =
1435 SectionRelocMap.lookup(getSection(Rel.w.a));
1437 // Do a binary search for the current reloc section index (which must be
1438 // present). Then get the next one.
1439 typename RelocMap_t::mapped_type::const_iterator loc =
1440 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
1443 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
1444 // to the end iterator.
1445 if (loc != relocseclist.end()) {
1450 Result = RelocationRef(Rel, this);
1451 return object_error::success;
1454 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1455 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1456 ::getRelocationSymbol(DataRefImpl Rel,
1457 SymbolRef &Result) const {
1459 const Elf_Shdr *sec = getSection(Rel.w.b);
1460 switch (sec->sh_type) {
1462 report_fatal_error("Invalid section type in Rel!");
1463 case ELF::SHT_REL : {
1464 symbolIdx = getRel(Rel)->getSymbol();
1467 case ELF::SHT_RELA : {
1468 symbolIdx = getRela(Rel)->getSymbol();
1472 DataRefImpl SymbolData;
1473 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
1474 if (it == SymbolTableSectionsIndexMap.end())
1475 report_fatal_error("Relocation symbol table not found!");
1476 SymbolData.d.a = symbolIdx;
1477 SymbolData.d.b = it->second;
1478 Result = SymbolRef(SymbolData, this);
1479 return object_error::success;
1482 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1483 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1484 ::getRelocationAddress(DataRefImpl Rel,
1485 uint64_t &Result) const {
1487 const Elf_Shdr *sec = getSection(Rel.w.b);
1488 switch (sec->sh_type) {
1490 report_fatal_error("Invalid section type in Rel!");
1491 case ELF::SHT_REL : {
1492 offset = getRel(Rel)->r_offset;
1495 case ELF::SHT_RELA : {
1496 offset = getRela(Rel)->r_offset;
1502 return object_error::success;
1505 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1506 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1507 ::getRelocationOffset(DataRefImpl Rel,
1508 uint64_t &Result) const {
1510 const Elf_Shdr *sec = getSection(Rel.w.b);
1511 switch (sec->sh_type) {
1513 report_fatal_error("Invalid section type in Rel!");
1514 case ELF::SHT_REL : {
1515 offset = getRel(Rel)->r_offset;
1518 case ELF::SHT_RELA : {
1519 offset = getRela(Rel)->r_offset;
1524 Result = offset - sec->sh_addr;
1525 return object_error::success;
1528 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1529 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1530 ::getRelocationType(DataRefImpl Rel,
1531 uint64_t &Result) const {
1532 const Elf_Shdr *sec = getSection(Rel.w.b);
1533 switch (sec->sh_type) {
1535 report_fatal_error("Invalid section type in Rel!");
1536 case ELF::SHT_REL : {
1537 Result = getRel(Rel)->getType();
1540 case ELF::SHT_RELA : {
1541 Result = getRela(Rel)->getType();
1545 return object_error::success;
1548 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
1549 case ELF::enum: res = #enum; break;
1551 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1552 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1553 ::getRelocationTypeName(DataRefImpl Rel,
1554 SmallVectorImpl<char> &Result) const {
1555 const Elf_Shdr *sec = getSection(Rel.w.b);
1558 switch (sec->sh_type) {
1560 return object_error::parse_failed;
1561 case ELF::SHT_REL : {
1562 type = getRel(Rel)->getType();
1565 case ELF::SHT_RELA : {
1566 type = getRela(Rel)->getType();
1570 switch (Header->e_machine) {
1571 case ELF::EM_X86_64:
1573 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
1574 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
1575 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
1576 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
1577 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
1578 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
1579 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
1580 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
1581 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
1582 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
1583 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
1584 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
1585 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
1586 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
1587 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
1588 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
1589 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
1590 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
1591 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
1592 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
1593 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
1594 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
1595 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
1596 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
1597 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
1598 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
1599 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
1600 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
1601 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
1602 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
1603 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
1604 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
1611 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
1612 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
1613 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
1614 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
1615 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
1616 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
1617 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
1618 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
1619 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
1620 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
1621 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
1622 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
1623 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
1624 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
1625 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
1626 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
1627 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
1628 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
1629 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
1630 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
1631 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
1632 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
1633 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
1634 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
1635 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
1636 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
1637 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
1638 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
1639 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
1640 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
1641 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
1642 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
1643 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
1644 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
1645 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
1646 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
1647 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
1648 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
1649 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
1650 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
1657 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_NONE);
1658 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PC24);
1659 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32);
1660 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32);
1661 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G0);
1662 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS16);
1663 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS12);
1664 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ABS5);
1665 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS8);
1666 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL32);
1667 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_CALL);
1668 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC8);
1669 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BREL_ADJ);
1670 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESC);
1671 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_SWI8);
1672 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_XPC25);
1673 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_XPC22);
1674 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPMOD32);
1675 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPOFF32);
1676 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_TPOFF32);
1677 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_COPY);
1678 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GLOB_DAT);
1679 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP_SLOT);
1680 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_RELATIVE);
1681 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF32);
1682 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_PREL);
1683 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL);
1684 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32);
1685 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_CALL);
1686 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP24);
1687 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP24);
1688 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_ABS);
1689 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_7_0);
1690 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_15_8);
1691 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_23_15);
1692 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SBREL_11_0_NC);
1693 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_19_12_NC);
1694 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_27_20_CK);
1695 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET1);
1696 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL31);
1697 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_V4BX);
1698 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET2);
1699 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PREL31);
1700 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_ABS_NC);
1701 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_ABS);
1702 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_PREL_NC);
1703 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_PREL);
1704 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_ABS_NC);
1705 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_ABS);
1706 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_PREL_NC);
1707 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_PREL);
1708 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP19);
1709 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP6);
1710 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ALU_PREL_11_0);
1711 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC12);
1712 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32_NOI);
1713 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32_NOI);
1714 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0_NC);
1715 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0);
1716 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1_NC);
1717 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1);
1718 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G2);
1719 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G1);
1720 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G2);
1721 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G0);
1722 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G1);
1723 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G2);
1724 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G0);
1725 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G1);
1726 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G2);
1727 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0_NC);
1728 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0);
1729 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1_NC);
1730 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1);
1731 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G2);
1732 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G0);
1733 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G1);
1734 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G2);
1735 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G0);
1736 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G1);
1737 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G2);
1738 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G0);
1739 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G1);
1740 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G2);
1741 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL_NC);
1742 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_BREL);
1743 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL);
1744 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL_NC);
1745 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_BREL);
1746 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL);
1747 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GOTDESC);
1748 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_CALL);
1749 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESCSEQ);
1750 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_CALL);
1751 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32_ABS);
1752 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_ABS);
1753 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_PREL);
1754 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL12);
1755 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF12);
1756 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTRELAX);
1757 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTENTRY);
1758 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTINHERIT);
1759 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP11);
1760 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP8);
1761 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GD32);
1762 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDM32);
1763 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO32);
1764 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE32);
1765 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE32);
1766 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO12);
1767 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE12);
1768 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE12GP);
1769 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_0);
1770 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_1);
1771 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_2);
1772 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_3);
1773 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_4);
1774 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_5);
1775 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_6);
1776 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_7);
1777 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_8);
1778 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_9);
1779 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_10);
1780 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_11);
1781 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_12);
1782 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_13);
1783 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_14);
1784 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_15);
1785 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ME_TOO);
1786 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ16);
1787 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ32);
1792 case ELF::EM_HEXAGON:
1794 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_NONE);
1795 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL);
1796 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL);
1797 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL);
1798 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_LO16);
1799 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HI16);
1800 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32);
1801 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16);
1802 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8);
1803 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_0);
1804 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_1);
1805 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_2);
1806 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_3);
1807 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HL16);
1808 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL);
1809 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL);
1810 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B32_PCREL_X);
1811 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_6_X);
1812 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL_X);
1813 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL_X);
1814 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL_X);
1815 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL_X);
1816 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL_X);
1817 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16_X);
1818 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_12_X);
1819 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_11_X);
1820 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_10_X);
1821 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_9_X);
1822 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8_X);
1823 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_7_X);
1824 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_X);
1825 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_PCREL);
1826 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_COPY);
1827 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GLOB_DAT);
1828 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_JMP_SLOT);
1829 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_RELATIVE);
1830 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_PLT_B22_PCREL);
1831 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_LO16);
1832 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_HI16);
1833 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32);
1834 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_LO16);
1835 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_HI16);
1836 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32);
1837 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16);
1838 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPMOD_32);
1839 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_LO16);
1840 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_HI16);
1841 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32);
1842 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16);
1843 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_PLT_B22_PCREL);
1844 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_LO16);
1845 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_HI16);
1846 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32);
1847 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16);
1848 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_LO16);
1849 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_HI16);
1850 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32);
1851 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_LO16);
1852 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_HI16);
1853 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32);
1854 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16);
1855 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_LO16);
1856 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_HI16);
1857 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32);
1858 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16);
1859 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_PCREL_X);
1860 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32_6_X);
1861 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_16_X);
1862 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_11_X);
1863 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32_6_X);
1864 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16_X);
1865 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_11_X);
1866 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32_6_X);
1867 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16_X);
1868 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_11_X);
1869 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32_6_X);
1870 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16_X);
1871 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_11_X);
1872 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32_6_X);
1873 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_16_X);
1874 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32_6_X);
1875 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16_X);
1876 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_11_X);
1877 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32_6_X);
1878 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16_X);
1879 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_11_X);
1887 Result.append(res.begin(), res.end());
1888 return object_error::success;
1891 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
1893 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1894 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1895 ::getRelocationAdditionalInfo(DataRefImpl Rel,
1896 int64_t &Result) const {
1897 const Elf_Shdr *sec = getSection(Rel.w.b);
1898 switch (sec->sh_type) {
1900 report_fatal_error("Invalid section type in Rel!");
1901 case ELF::SHT_REL : {
1903 return object_error::success;
1905 case ELF::SHT_RELA : {
1906 Result = getRela(Rel)->r_addend;
1907 return object_error::success;
1912 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1913 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1914 ::getRelocationValueString(DataRefImpl Rel,
1915 SmallVectorImpl<char> &Result) const {
1916 const Elf_Shdr *sec = getSection(Rel.w.b);
1920 uint16_t symbol_index = 0;
1921 switch (sec->sh_type) {
1923 return object_error::parse_failed;
1924 case ELF::SHT_REL: {
1925 type = getRel(Rel)->getType();
1926 symbol_index = getRel(Rel)->getSymbol();
1927 // TODO: Read implicit addend from section data.
1930 case ELF::SHT_RELA: {
1931 type = getRela(Rel)->getType();
1932 symbol_index = getRela(Rel)->getSymbol();
1933 addend = getRela(Rel)->r_addend;
1937 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1939 if (error_code ec = getSymbolName(getSection(sec->sh_link), symb, symname))
1941 switch (Header->e_machine) {
1942 case ELF::EM_X86_64:
1944 case ELF::R_X86_64_PC8:
1945 case ELF::R_X86_64_PC16:
1946 case ELF::R_X86_64_PC32: {
1948 raw_string_ostream fmt(fmtbuf);
1949 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1951 Result.append(fmtbuf.begin(), fmtbuf.end());
1954 case ELF::R_X86_64_8:
1955 case ELF::R_X86_64_16:
1956 case ELF::R_X86_64_32:
1957 case ELF::R_X86_64_32S:
1958 case ELF::R_X86_64_64: {
1960 raw_string_ostream fmt(fmtbuf);
1961 fmt << symname << (addend < 0 ? "" : "+") << addend;
1963 Result.append(fmtbuf.begin(), fmtbuf.end());
1971 case ELF::EM_HEXAGON:
1978 Result.append(res.begin(), res.end());
1979 return object_error::success;
1982 // Verify that the last byte in the string table in a null.
1983 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1984 void ELFObjectFile<target_endianness, max_alignment, is64Bits>
1985 ::VerifyStrTab(const Elf_Shdr *sh) const {
1986 const char *strtab = (const char*)base() + sh->sh_offset;
1987 if (strtab[sh->sh_size - 1] != 0)
1988 // FIXME: Proper error handling.
1989 report_fatal_error("String table must end with a null terminator!");
1992 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1993 ELFObjectFile<target_endianness, max_alignment, is64Bits>
1994 ::ELFObjectFile(MemoryBuffer *Object, error_code &ec)
1995 : ObjectFile(getELFType(target_endianness == support::little, is64Bits),
1997 , isDyldELFObject(false)
1998 , SectionHeaderTable(0)
1999 , dot_shstrtab_sec(0)
2002 , dot_dynamic_sec(0)
2003 , dot_gnu_version_sec(0)
2004 , dot_gnu_version_r_sec(0)
2005 , dot_gnu_version_d_sec(0)
2009 const uint64_t FileSize = Data->getBufferSize();
2011 if (sizeof(Elf_Ehdr) > FileSize)
2012 // FIXME: Proper error handling.
2013 report_fatal_error("File too short!");
2015 Header = reinterpret_cast<const Elf_Ehdr *>(base());
2017 if (Header->e_shoff == 0)
2020 const uint64_t SectionTableOffset = Header->e_shoff;
2022 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize)
2023 // FIXME: Proper error handling.
2024 report_fatal_error("Section header table goes past end of file!");
2026 // The getNumSections() call below depends on SectionHeaderTable being set.
2027 SectionHeaderTable =
2028 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
2029 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
2031 if (SectionTableOffset + SectionTableSize > FileSize)
2032 // FIXME: Proper error handling.
2033 report_fatal_error("Section table goes past end of file!");
2035 // To find the symbol tables we walk the section table to find SHT_SYMTAB.
2036 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
2037 const Elf_Shdr* sh = SectionHeaderTable;
2039 // Reserve SymbolTableSections[0] for .dynsym
2040 SymbolTableSections.push_back(NULL);
2042 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
2043 switch (sh->sh_type) {
2044 case ELF::SHT_SYMTAB_SHNDX: {
2045 if (SymbolTableSectionHeaderIndex)
2046 // FIXME: Proper error handling.
2047 report_fatal_error("More than one .symtab_shndx!");
2048 SymbolTableSectionHeaderIndex = sh;
2051 case ELF::SHT_SYMTAB: {
2052 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
2053 SymbolTableSections.push_back(sh);
2056 case ELF::SHT_DYNSYM: {
2057 if (SymbolTableSections[0] != NULL)
2058 // FIXME: Proper error handling.
2059 report_fatal_error("More than one .dynsym!");
2060 SymbolTableSectionsIndexMap[i] = 0;
2061 SymbolTableSections[0] = sh;
2065 case ELF::SHT_RELA: {
2066 SectionRelocMap[getSection(sh->sh_info)].push_back(i);
2069 case ELF::SHT_DYNAMIC: {
2070 if (dot_dynamic_sec != NULL)
2071 // FIXME: Proper error handling.
2072 report_fatal_error("More than one .dynamic!");
2073 dot_dynamic_sec = sh;
2076 case ELF::SHT_GNU_versym: {
2077 if (dot_gnu_version_sec != NULL)
2078 // FIXME: Proper error handling.
2079 report_fatal_error("More than one .gnu.version section!");
2080 dot_gnu_version_sec = sh;
2083 case ELF::SHT_GNU_verdef: {
2084 if (dot_gnu_version_d_sec != NULL)
2085 // FIXME: Proper error handling.
2086 report_fatal_error("More than one .gnu.version_d section!");
2087 dot_gnu_version_d_sec = sh;
2090 case ELF::SHT_GNU_verneed: {
2091 if (dot_gnu_version_r_sec != NULL)
2092 // FIXME: Proper error handling.
2093 report_fatal_error("More than one .gnu.version_r section!");
2094 dot_gnu_version_r_sec = sh;
2101 // Sort section relocation lists by index.
2102 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
2103 e = SectionRelocMap.end(); i != e; ++i) {
2104 std::sort(i->second.begin(), i->second.end());
2107 // Get string table sections.
2108 dot_shstrtab_sec = getSection(getStringTableIndex());
2109 if (dot_shstrtab_sec) {
2110 // Verify that the last byte in the string table in a null.
2111 VerifyStrTab(dot_shstrtab_sec);
2114 // Merge this into the above loop.
2115 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
2116 *e = i + getNumSections() * Header->e_shentsize;
2117 i != e; i += Header->e_shentsize) {
2118 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
2119 if (sh->sh_type == ELF::SHT_STRTAB) {
2120 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
2121 if (SectionName == ".strtab") {
2122 if (dot_strtab_sec != 0)
2123 // FIXME: Proper error handling.
2124 report_fatal_error("Already found section named .strtab!");
2125 dot_strtab_sec = sh;
2126 VerifyStrTab(dot_strtab_sec);
2127 } else if (SectionName == ".dynstr") {
2128 if (dot_dynstr_sec != 0)
2129 // FIXME: Proper error handling.
2130 report_fatal_error("Already found section named .dynstr!");
2131 dot_dynstr_sec = sh;
2132 VerifyStrTab(dot_dynstr_sec);
2137 // Build symbol name side-mapping if there is one.
2138 if (SymbolTableSectionHeaderIndex) {
2139 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
2140 SymbolTableSectionHeaderIndex->sh_offset);
2142 for (symbol_iterator si = begin_symbols(),
2143 se = end_symbols(); si != se; si.increment(ec)) {
2145 report_fatal_error("Fewer extended symbol table entries than symbols!");
2146 if (*ShndxTable != ELF::SHN_UNDEF)
2147 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
2153 // Get the symbol table index in the symtab section given a symbol
2154 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2155 uint64_t ELFObjectFile<target_endianness, max_alignment, is64Bits>
2156 ::getSymbolIndex(const Elf_Sym *Sym) const {
2157 assert(SymbolTableSections.size() == 1 && "Only one symbol table supported!");
2158 const Elf_Shdr *SymTab = *SymbolTableSections.begin();
2159 uintptr_t SymLoc = uintptr_t(Sym);
2160 uintptr_t SymTabLoc = uintptr_t(base() + SymTab->sh_offset);
2161 assert(SymLoc > SymTabLoc && "Symbol not in symbol table!");
2162 uint64_t SymOffset = SymLoc - SymTabLoc;
2163 assert(SymOffset % SymTab->sh_entsize == 0 &&
2164 "Symbol not multiple of symbol size!");
2165 return SymOffset / SymTab->sh_entsize;
2168 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2169 symbol_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2170 ::begin_symbols() const {
2171 DataRefImpl SymbolData;
2172 if (SymbolTableSections.size() <= 1) {
2173 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2174 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2176 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2177 SymbolData.d.b = 1; // The 0th table is .dynsym
2179 return symbol_iterator(SymbolRef(SymbolData, this));
2182 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2183 symbol_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2184 ::end_symbols() const {
2185 DataRefImpl SymbolData;
2186 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2187 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2188 return symbol_iterator(SymbolRef(SymbolData, this));
2191 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2192 symbol_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2193 ::begin_dynamic_symbols() const {
2194 DataRefImpl SymbolData;
2195 if (SymbolTableSections[0] == NULL) {
2196 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2197 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2199 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2200 SymbolData.d.b = 0; // The 0th table is .dynsym
2202 return symbol_iterator(SymbolRef(SymbolData, this));
2205 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2206 symbol_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2207 ::end_dynamic_symbols() const {
2208 DataRefImpl SymbolData;
2209 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2210 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2211 return symbol_iterator(SymbolRef(SymbolData, this));
2214 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2215 section_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2216 ::begin_sections() const {
2218 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
2219 return section_iterator(SectionRef(ret, this));
2222 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2223 section_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2224 ::end_sections() const {
2226 ret.p = reinterpret_cast<intptr_t>(base()
2228 + (Header->e_shentsize*getNumSections()));
2229 return section_iterator(SectionRef(ret, this));
2232 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2233 typename ELFObjectFile<target_endianness, max_alignment, is64Bits>::dyn_iterator
2234 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2235 ::begin_dynamic_table() const {
2236 DataRefImpl DynData;
2237 if (dot_dynamic_sec == NULL || dot_dynamic_sec->sh_size == 0) {
2238 DynData.d.a = std::numeric_limits<uint32_t>::max();
2242 return dyn_iterator(DynRef(DynData, this));
2245 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2246 typename ELFObjectFile<target_endianness, max_alignment, is64Bits>::dyn_iterator
2247 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2248 ::end_dynamic_table() const {
2249 DataRefImpl DynData;
2250 DynData.d.a = std::numeric_limits<uint32_t>::max();
2251 return dyn_iterator(DynRef(DynData, this));
2254 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2255 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2256 ::getDynNext(DataRefImpl DynData,
2257 DynRef &Result) const {
2260 // Check to see if we are at the end of .dynamic
2261 if (DynData.d.a >= dot_dynamic_sec->getEntityCount()) {
2262 // We are at the end. Return the terminator.
2263 DynData.d.a = std::numeric_limits<uint32_t>::max();
2266 Result = DynRef(DynData, this);
2267 return object_error::success;
2270 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2272 ELFObjectFile<target_endianness, max_alignment, is64Bits>::getLoadName() const {
2274 // Find the DT_SONAME entry
2275 dyn_iterator it = begin_dynamic_table();
2276 dyn_iterator ie = end_dynamic_table();
2279 if (it->getTag() == ELF::DT_SONAME)
2283 report_fatal_error("dynamic table iteration failed");
2286 if (dot_dynstr_sec == NULL)
2287 report_fatal_error("Dynamic string table is missing");
2288 dt_soname = getString(dot_dynstr_sec, it->getVal());
2296 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2297 library_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2298 ::begin_libraries_needed() const {
2299 // Find the first DT_NEEDED entry
2300 dyn_iterator i = begin_dynamic_table();
2301 dyn_iterator e = end_dynamic_table();
2304 if (i->getTag() == ELF::DT_NEEDED)
2308 report_fatal_error("dynamic table iteration failed");
2310 // Use the same DataRefImpl format as DynRef.
2311 return library_iterator(LibraryRef(i->getRawDataRefImpl(), this));
2314 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2315 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2316 ::getLibraryNext(DataRefImpl Data,
2317 LibraryRef &Result) const {
2318 // Use the same DataRefImpl format as DynRef.
2319 dyn_iterator i = dyn_iterator(DynRef(Data, this));
2320 dyn_iterator e = end_dynamic_table();
2322 // Skip the current dynamic table entry.
2326 // TODO: proper error handling
2328 report_fatal_error("dynamic table iteration failed");
2331 // Find the next DT_NEEDED entry.
2333 if (i->getTag() == ELF::DT_NEEDED)
2337 report_fatal_error("dynamic table iteration failed");
2339 Result = LibraryRef(i->getRawDataRefImpl(), this);
2340 return object_error::success;
2343 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2344 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2345 ::getLibraryPath(DataRefImpl Data, StringRef &Res) const {
2346 dyn_iterator i = dyn_iterator(DynRef(Data, this));
2347 if (i == end_dynamic_table())
2348 report_fatal_error("getLibraryPath() called on iterator end");
2350 if (i->getTag() != ELF::DT_NEEDED)
2351 report_fatal_error("Invalid library_iterator");
2353 // This uses .dynstr to lookup the name of the DT_NEEDED entry.
2354 // THis works as long as DT_STRTAB == .dynstr. This is true most of
2355 // the time, but the specification allows exceptions.
2356 // TODO: This should really use DT_STRTAB instead. Doing this requires
2357 // reading the program headers.
2358 if (dot_dynstr_sec == NULL)
2359 report_fatal_error("Dynamic string table is missing");
2360 Res = getString(dot_dynstr_sec, i->getVal());
2361 return object_error::success;
2364 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2365 library_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2366 ::end_libraries_needed() const {
2367 dyn_iterator e = end_dynamic_table();
2368 // Use the same DataRefImpl format as DynRef.
2369 return library_iterator(LibraryRef(e->getRawDataRefImpl(), this));
2372 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2373 uint8_t ELFObjectFile<target_endianness, max_alignment, is64Bits>
2374 ::getBytesInAddress() const {
2375 return is64Bits ? 8 : 4;
2378 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2379 StringRef ELFObjectFile<target_endianness, max_alignment, is64Bits>
2380 ::getFileFormatName() const {
2381 switch(Header->e_ident[ELF::EI_CLASS]) {
2382 case ELF::ELFCLASS32:
2383 switch(Header->e_machine) {
2385 return "ELF32-i386";
2386 case ELF::EM_X86_64:
2387 return "ELF32-x86-64";
2390 case ELF::EM_HEXAGON:
2391 return "ELF32-hexagon";
2393 return "ELF32-unknown";
2395 case ELF::ELFCLASS64:
2396 switch(Header->e_machine) {
2398 return "ELF64-i386";
2399 case ELF::EM_X86_64:
2400 return "ELF64-x86-64";
2402 return "ELF64-ppc64";
2404 return "ELF64-unknown";
2407 // FIXME: Proper error handling.
2408 report_fatal_error("Invalid ELFCLASS!");
2412 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2413 unsigned ELFObjectFile<target_endianness, max_alignment, is64Bits>
2415 switch(Header->e_machine) {
2418 case ELF::EM_X86_64:
2419 return Triple::x86_64;
2422 case ELF::EM_HEXAGON:
2423 return Triple::hexagon;
2425 return (target_endianness == support::little) ?
2426 Triple::mipsel : Triple::mips;
2428 return Triple::ppc64;
2430 return Triple::UnknownArch;
2434 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2435 uint64_t ELFObjectFile<target_endianness, max_alignment, is64Bits>
2436 ::getNumSections() const {
2437 assert(Header && "Header not initialized!");
2438 if (Header->e_shnum == ELF::SHN_UNDEF) {
2439 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
2440 return SectionHeaderTable->sh_size;
2442 return Header->e_shnum;
2445 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2447 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2448 ::getStringTableIndex() const {
2449 if (Header->e_shnum == ELF::SHN_UNDEF) {
2450 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
2451 return SectionHeaderTable->sh_link;
2452 if (Header->e_shstrndx >= getNumSections())
2455 return Header->e_shstrndx;
2458 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2459 template<typename T>
2461 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2462 ::getEntry(uint16_t Section, uint32_t Entry) const {
2463 return getEntry<T>(getSection(Section), Entry);
2466 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2467 template<typename T>
2469 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2470 ::getEntry(const Elf_Shdr * Section, uint32_t Entry) const {
2471 return reinterpret_cast<const T *>(
2473 + Section->sh_offset
2474 + (Entry * Section->sh_entsize));
2477 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2478 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2480 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2481 ::getSymbol(DataRefImpl Symb) const {
2482 return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
2485 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2486 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2488 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2489 ::getDyn(DataRefImpl DynData) const {
2490 return getEntry<Elf_Dyn>(dot_dynamic_sec, DynData.d.a);
2493 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2494 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2496 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2497 ::getRel(DataRefImpl Rel) const {
2498 return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
2501 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2502 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2504 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2505 ::getRela(DataRefImpl Rela) const {
2506 return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
2509 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2510 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2512 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2513 ::getSection(DataRefImpl Symb) const {
2514 const Elf_Shdr *sec = getSection(Symb.d.b);
2515 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
2516 // FIXME: Proper error handling.
2517 report_fatal_error("Invalid symbol table section!");
2521 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2522 const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2524 ELFObjectFile<target_endianness, max_alignment, is64Bits>
2525 ::getSection(uint32_t index) const {
2528 if (!SectionHeaderTable || index >= getNumSections())
2529 // FIXME: Proper error handling.
2530 report_fatal_error("Invalid section index!");
2532 return reinterpret_cast<const Elf_Shdr *>(
2533 reinterpret_cast<const char *>(SectionHeaderTable)
2534 + (index * Header->e_shentsize));
2537 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2538 const char *ELFObjectFile<target_endianness, max_alignment, is64Bits>
2539 ::getString(uint32_t section,
2540 ELF::Elf32_Word offset) const {
2541 return getString(getSection(section), offset);
2544 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2545 const char *ELFObjectFile<target_endianness, max_alignment, is64Bits>
2546 ::getString(const Elf_Shdr *section,
2547 ELF::Elf32_Word offset) const {
2548 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
2549 if (offset >= section->sh_size)
2550 // FIXME: Proper error handling.
2551 report_fatal_error("Symbol name offset outside of string table!");
2552 return (const char *)base() + section->sh_offset + offset;
2555 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2556 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2557 ::getSymbolName(const Elf_Shdr *section,
2558 const Elf_Sym *symb,
2559 StringRef &Result) const {
2560 if (symb->st_name == 0) {
2561 const Elf_Shdr *section = getSection(symb);
2565 Result = getString(dot_shstrtab_sec, section->sh_name);
2566 return object_error::success;
2569 if (section == SymbolTableSections[0]) {
2570 // Symbol is in .dynsym, use .dynstr string table
2571 Result = getString(dot_dynstr_sec, symb->st_name);
2573 // Use the default symbol table name section.
2574 Result = getString(dot_strtab_sec, symb->st_name);
2576 return object_error::success;
2579 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2580 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2581 ::getSectionName(const Elf_Shdr *section,
2582 StringRef &Result) const {
2583 Result = StringRef(getString(dot_shstrtab_sec, section->sh_name));
2584 return object_error::success;
2587 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2588 error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2589 ::getSymbolVersion(const Elf_Shdr *section,
2590 const Elf_Sym *symb,
2592 bool &IsDefault) const {
2593 // Handle non-dynamic symbols.
2594 if (section != SymbolTableSections[0]) {
2595 // Non-dynamic symbols can have versions in their names
2596 // A name of the form 'foo@V1' indicates version 'V1', non-default.
2597 // A name of the form 'foo@@V2' indicates version 'V2', default version.
2599 error_code ec = getSymbolName(section, symb, Name);
2600 if (ec != object_error::success)
2602 size_t atpos = Name.find('@');
2603 if (atpos == StringRef::npos) {
2606 return object_error::success;
2609 if (atpos < Name.size() && Name[atpos] == '@') {
2615 Version = Name.substr(atpos);
2616 return object_error::success;
2619 // This is a dynamic symbol. Look in the GNU symbol version table.
2620 if (dot_gnu_version_sec == NULL) {
2621 // No version table.
2624 return object_error::success;
2627 // Determine the position in the symbol table of this entry.
2628 const char *sec_start = (const char*)base() + section->sh_offset;
2629 size_t entry_index = ((const char*)symb - sec_start)/section->sh_entsize;
2631 // Get the corresponding version index entry
2632 const Elf_Versym *vs = getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index);
2633 size_t version_index = vs->vs_index & ELF::VERSYM_VERSION;
2635 // Special markers for unversioned symbols.
2636 if (version_index == ELF::VER_NDX_LOCAL ||
2637 version_index == ELF::VER_NDX_GLOBAL) {
2640 return object_error::success;
2643 // Lookup this symbol in the version table
2645 if (version_index >= VersionMap.size() || VersionMap[version_index].isNull())
2646 report_fatal_error("Symbol has version index without corresponding "
2647 "define or reference entry");
2648 const VersionMapEntry &entry = VersionMap[version_index];
2650 // Get the version name string
2652 if (entry.isVerdef()) {
2653 // The first Verdaux entry holds the name.
2654 name_offset = entry.getVerdef()->getAux()->vda_name;
2656 name_offset = entry.getVernaux()->vna_name;
2658 Version = getString(dot_dynstr_sec, name_offset);
2661 if (entry.isVerdef()) {
2662 IsDefault = !(vs->vs_index & ELF::VERSYM_HIDDEN);
2667 return object_error::success;
2670 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2671 inline DynRefImpl<target_endianness, max_alignment, is64Bits>
2672 ::DynRefImpl(DataRefImpl DynP, const OwningType *Owner)
2674 , OwningObject(Owner) {}
2676 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2677 inline bool DynRefImpl<target_endianness, max_alignment, is64Bits>
2678 ::operator==(const DynRefImpl &Other) const {
2679 return DynPimpl == Other.DynPimpl;
2682 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2683 inline bool DynRefImpl<target_endianness, max_alignment, is64Bits>
2684 ::operator <(const DynRefImpl &Other) const {
2685 return DynPimpl < Other.DynPimpl;
2688 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2689 inline error_code DynRefImpl<target_endianness, max_alignment, is64Bits>
2690 ::getNext(DynRefImpl &Result) const {
2691 return OwningObject->getDynNext(DynPimpl, Result);
2694 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2695 inline int64_t DynRefImpl<target_endianness, max_alignment, is64Bits>
2697 return OwningObject->getDyn(DynPimpl)->d_tag;
2700 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2701 inline uint64_t DynRefImpl<target_endianness, max_alignment, is64Bits>
2703 return OwningObject->getDyn(DynPimpl)->d_un.d_val;
2706 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2707 inline uint64_t DynRefImpl<target_endianness, max_alignment, is64Bits>
2709 return OwningObject->getDyn(DynPimpl)->d_un.d_ptr;
2712 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2713 inline DataRefImpl DynRefImpl<target_endianness, max_alignment, is64Bits>
2714 ::getRawDataRefImpl() const {
2718 /// This is a generic interface for retrieving GNU symbol version
2719 /// information from an ELFObjectFile.
2720 static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
2721 const SymbolRef &Sym,
2724 // Little-endian 32-bit
2725 if (const ELFObjectFile<support::little, 4, false> *ELFObj =
2726 dyn_cast<ELFObjectFile<support::little, 4, false> >(Obj))
2727 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2729 // Big-endian 32-bit
2730 if (const ELFObjectFile<support::big, 4, false> *ELFObj =
2731 dyn_cast<ELFObjectFile<support::big, 4, false> >(Obj))
2732 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2734 // Little-endian 64-bit
2735 if (const ELFObjectFile<support::little, 8, true> *ELFObj =
2736 dyn_cast<ELFObjectFile<support::little, 8, true> >(Obj))
2737 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2739 // Big-endian 64-bit
2740 if (const ELFObjectFile<support::big, 8, true> *ELFObj =
2741 dyn_cast<ELFObjectFile<support::big, 8, true> >(Obj))
2742 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2744 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");