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<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
40 static const endianness TargetEndianness = target_endianness;
41 static const std::size_t MaxAlignment = max_alignment;
42 static const bool Is64Bits = is64Bits;
45 template<typename T, int max_align>
46 struct MaximumAlignment {
47 enum {value = AlignOf<T>::Alignment > max_align ? max_align
48 : AlignOf<T>::Alignment};
51 // Subclasses of ELFObjectFile may need this for template instantiation
52 inline std::pair<unsigned char, unsigned char>
53 getElfArchType(MemoryBuffer *Object) {
54 if (Object->getBufferSize() < ELF::EI_NIDENT)
55 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE);
56 return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS]
57 , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]);
60 // Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
61 template<endianness target_endianness, std::size_t max_alignment>
62 struct ELFDataTypeTypedefHelperCommon {
63 typedef support::detail::packed_endian_specific_integral
64 <uint16_t, target_endianness,
65 MaximumAlignment<uint16_t, max_alignment>::value> Elf_Half;
66 typedef support::detail::packed_endian_specific_integral
67 <uint32_t, target_endianness,
68 MaximumAlignment<uint32_t, max_alignment>::value> Elf_Word;
69 typedef support::detail::packed_endian_specific_integral
70 <int32_t, target_endianness,
71 MaximumAlignment<int32_t, max_alignment>::value> Elf_Sword;
72 typedef support::detail::packed_endian_specific_integral
73 <uint64_t, target_endianness,
74 MaximumAlignment<uint64_t, max_alignment>::value> Elf_Xword;
75 typedef support::detail::packed_endian_specific_integral
76 <int64_t, target_endianness,
77 MaximumAlignment<int64_t, max_alignment>::value> Elf_Sxword;
81 struct ELFDataTypeTypedefHelper;
84 template<template<endianness, std::size_t, bool> class ELFT,
85 endianness TargetEndianness, std::size_t MaxAlign>
86 struct ELFDataTypeTypedefHelper<ELFT<TargetEndianness, MaxAlign, false> >
87 : ELFDataTypeTypedefHelperCommon<TargetEndianness, MaxAlign> {
88 typedef uint32_t value_type;
89 typedef support::detail::packed_endian_specific_integral
90 <value_type, TargetEndianness,
91 MaximumAlignment<value_type, MaxAlign>::value> Elf_Addr;
92 typedef support::detail::packed_endian_specific_integral
93 <value_type, TargetEndianness,
94 MaximumAlignment<value_type, MaxAlign>::value> Elf_Off;
98 template<template<endianness, std::size_t, bool> class ELFT,
99 endianness TargetEndianness, std::size_t MaxAlign>
100 struct ELFDataTypeTypedefHelper<ELFT<TargetEndianness, MaxAlign, true> >
101 : ELFDataTypeTypedefHelperCommon<TargetEndianness, MaxAlign> {
102 typedef uint64_t value_type;
103 typedef support::detail::packed_endian_specific_integral
104 <value_type, TargetEndianness,
105 MaximumAlignment<value_type, MaxAlign>::value> Elf_Addr;
106 typedef support::detail::packed_endian_specific_integral
107 <value_type, TargetEndianness,
108 MaximumAlignment<value_type, MaxAlign>::value> Elf_Off;
111 // I really don't like doing this, but the alternative is copypasta.
112 #define LLVM_ELF_IMPORT_TYPES(ELFT) \
113 typedef typename ELFDataTypeTypedefHelper <ELFT>::Elf_Addr Elf_Addr; \
114 typedef typename ELFDataTypeTypedefHelper <ELFT>::Elf_Off Elf_Off; \
115 typedef typename ELFDataTypeTypedefHelper <ELFT>::Elf_Half Elf_Half; \
116 typedef typename ELFDataTypeTypedefHelper <ELFT>::Elf_Word Elf_Word; \
117 typedef typename ELFDataTypeTypedefHelper <ELFT>::Elf_Sword Elf_Sword; \
118 typedef typename ELFDataTypeTypedefHelper <ELFT>::Elf_Xword Elf_Xword; \
119 typedef typename ELFDataTypeTypedefHelper <ELFT>::Elf_Sxword Elf_Sxword;
121 // This is required to get template types into a macro :(
122 #define LLVM_ELF_COMMA ,
126 struct Elf_Shdr_Base;
128 template<template<endianness, std::size_t, bool> class ELFT,
129 endianness TargetEndianness, std::size_t MaxAlign>
130 struct Elf_Shdr_Base<ELFT<TargetEndianness, MaxAlign, false> > {
131 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
132 MaxAlign LLVM_ELF_COMMA false>)
133 Elf_Word sh_name; // Section name (index into string table)
134 Elf_Word sh_type; // Section type (SHT_*)
135 Elf_Word sh_flags; // Section flags (SHF_*)
136 Elf_Addr sh_addr; // Address where section is to be loaded
137 Elf_Off sh_offset; // File offset of section data, in bytes
138 Elf_Word sh_size; // Size of section, in bytes
139 Elf_Word sh_link; // Section type-specific header table index link
140 Elf_Word sh_info; // Section type-specific extra information
141 Elf_Word sh_addralign;// Section address alignment
142 Elf_Word sh_entsize; // Size of records contained within the section
145 template<template<endianness, std::size_t, bool> class ELFT,
146 endianness TargetEndianness, std::size_t MaxAlign>
147 struct Elf_Shdr_Base<ELFT<TargetEndianness, MaxAlign, true> > {
148 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
149 MaxAlign LLVM_ELF_COMMA true>)
150 Elf_Word sh_name; // Section name (index into string table)
151 Elf_Word sh_type; // Section type (SHT_*)
152 Elf_Xword sh_flags; // Section flags (SHF_*)
153 Elf_Addr sh_addr; // Address where section is to be loaded
154 Elf_Off sh_offset; // File offset of section data, in bytes
155 Elf_Xword sh_size; // Size of section, in bytes
156 Elf_Word sh_link; // Section type-specific header table index link
157 Elf_Word sh_info; // Section type-specific extra information
158 Elf_Xword sh_addralign;// Section address alignment
159 Elf_Xword sh_entsize; // Size of records contained within the section
163 struct Elf_Shdr_Impl : Elf_Shdr_Base<ELFT> {
164 using Elf_Shdr_Base<ELFT>::sh_entsize;
165 using Elf_Shdr_Base<ELFT>::sh_size;
167 /// @brief Get the number of entities this section contains if it has any.
168 unsigned getEntityCount() const {
171 return sh_size / sh_entsize;
178 template<template<endianness, std::size_t, bool> class ELFT,
179 endianness TargetEndianness, std::size_t MaxAlign>
180 struct Elf_Sym_Base<ELFT<TargetEndianness, MaxAlign, false> > {
181 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
182 MaxAlign LLVM_ELF_COMMA false>)
183 Elf_Word st_name; // Symbol name (index into string table)
184 Elf_Addr st_value; // Value or address associated with the symbol
185 Elf_Word st_size; // Size of the symbol
186 unsigned char st_info; // Symbol's type and binding attributes
187 unsigned char st_other; // Must be zero; reserved
188 Elf_Half st_shndx; // Which section (header table index) it's defined in
191 template<template<endianness, std::size_t, bool> class ELFT,
192 endianness TargetEndianness, std::size_t MaxAlign>
193 struct Elf_Sym_Base<ELFT<TargetEndianness, MaxAlign, true> > {
194 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
195 MaxAlign LLVM_ELF_COMMA true>)
196 Elf_Word st_name; // Symbol name (index into string table)
197 unsigned char st_info; // Symbol's type and binding attributes
198 unsigned char st_other; // Must be zero; reserved
199 Elf_Half st_shndx; // Which section (header table index) it's defined in
200 Elf_Addr st_value; // Value or address associated with the symbol
201 Elf_Xword st_size; // Size of the symbol
205 struct Elf_Sym_Impl : Elf_Sym_Base<ELFT> {
206 using Elf_Sym_Base<ELFT>::st_info;
208 // These accessors and mutators correspond to the ELF32_ST_BIND,
209 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
210 unsigned char getBinding() const { return st_info >> 4; }
211 unsigned char getType() const { return st_info & 0x0f; }
212 void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
213 void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
214 void setBindingAndType(unsigned char b, unsigned char t) {
215 st_info = (b << 4) + (t & 0x0f);
219 /// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section
220 /// (.gnu.version). This structure is identical for ELF32 and ELF64.
222 struct Elf_Versym_Impl {
223 LLVM_ELF_IMPORT_TYPES(ELFT)
224 Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN)
228 struct Elf_Verdaux_Impl;
230 /// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section
231 /// (.gnu.version_d). This structure is identical for ELF32 and ELF64.
233 struct Elf_Verdef_Impl {
234 LLVM_ELF_IMPORT_TYPES(ELFT)
235 typedef Elf_Verdaux_Impl<ELFT> Elf_Verdaux;
236 Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT)
237 Elf_Half vd_flags; // Bitwise flags (VER_DEF_*)
238 Elf_Half vd_ndx; // Version index, used in .gnu.version entries
239 Elf_Half vd_cnt; // Number of Verdaux entries
240 Elf_Word vd_hash; // Hash of name
241 Elf_Word vd_aux; // Offset to the first Verdaux entry (in bytes)
242 Elf_Word vd_next; // Offset to the next Verdef entry (in bytes)
244 /// Get the first Verdaux entry for this Verdef.
245 const Elf_Verdaux *getAux() const {
246 return reinterpret_cast<const Elf_Verdaux*>((const char*)this + vd_aux);
250 /// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef
251 /// section (.gnu.version_d). This structure is identical for ELF32 and ELF64.
253 struct Elf_Verdaux_Impl {
254 LLVM_ELF_IMPORT_TYPES(ELFT)
255 Elf_Word vda_name; // Version name (offset in string table)
256 Elf_Word vda_next; // Offset to next Verdaux entry (in bytes)
259 /// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed
260 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
262 struct Elf_Verneed_Impl {
263 LLVM_ELF_IMPORT_TYPES(ELFT)
264 Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT)
265 Elf_Half vn_cnt; // Number of associated Vernaux entries
266 Elf_Word vn_file; // Library name (string table offset)
267 Elf_Word vn_aux; // Offset to first Vernaux entry (in bytes)
268 Elf_Word vn_next; // Offset to next Verneed entry (in bytes)
271 /// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed
272 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
274 struct Elf_Vernaux_Impl {
275 LLVM_ELF_IMPORT_TYPES(ELFT)
276 Elf_Word vna_hash; // Hash of dependency name
277 Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*)
278 Elf_Half vna_other; // Version index, used in .gnu.version entries
279 Elf_Word vna_name; // Dependency name
280 Elf_Word vna_next; // Offset to next Vernaux entry (in bytes)
283 /// Elf_Dyn_Base: This structure matches the form of entries in the dynamic
284 /// table section (.dynamic) look like.
288 template<template<endianness, std::size_t, bool> class ELFT,
289 endianness TargetEndianness, std::size_t MaxAlign>
290 struct Elf_Dyn_Base<ELFT<TargetEndianness, MaxAlign, false> > {
291 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
292 MaxAlign LLVM_ELF_COMMA false>)
300 template<template<endianness, std::size_t, bool> class ELFT,
301 endianness TargetEndianness, std::size_t MaxAlign>
302 struct Elf_Dyn_Base<ELFT<TargetEndianness, MaxAlign, true> > {
303 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
304 MaxAlign LLVM_ELF_COMMA true>)
312 /// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters.
314 struct Elf_Dyn_Impl : Elf_Dyn_Base<ELFT> {
315 using Elf_Dyn_Base<ELFT>::d_tag;
316 using Elf_Dyn_Base<ELFT>::d_un;
317 int64_t getTag() const { return d_tag; }
318 uint64_t getVal() const { return d_un.d_val; }
319 uint64_t getPtr() const { return d_un.ptr; }
325 // DynRefImpl: Reference to an entry in the dynamic table
326 // This is an ELF-specific interface.
329 typedef Elf_Dyn_Impl<ELFT> Elf_Dyn;
330 typedef ELFObjectFile<ELFT> OwningType;
332 DataRefImpl DynPimpl;
333 const OwningType *OwningObject;
336 DynRefImpl() : OwningObject(NULL) { }
338 DynRefImpl(DataRefImpl DynP, const OwningType *Owner);
340 bool operator==(const DynRefImpl &Other) const;
341 bool operator <(const DynRefImpl &Other) const;
343 error_code getNext(DynRefImpl &Result) const;
344 int64_t getTag() const;
345 uint64_t getVal() const;
346 uint64_t getPtr() const;
348 DataRefImpl getRawDataRefImpl() const;
351 // Elf_Rel: Elf Relocation
352 template<class ELFT, bool isRela>
355 template<template<endianness, std::size_t, bool> class ELFT,
356 endianness TargetEndianness, std::size_t MaxAlign>
357 struct Elf_Rel_Base<ELFT<TargetEndianness, MaxAlign, false>, false> {
358 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
359 MaxAlign LLVM_ELF_COMMA false>)
360 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
361 Elf_Word r_info; // Symbol table index and type of relocation to apply
364 template<template<endianness, std::size_t, bool> class ELFT,
365 endianness TargetEndianness, std::size_t MaxAlign>
366 struct Elf_Rel_Base<ELFT<TargetEndianness, MaxAlign, true>, false> {
367 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
368 MaxAlign LLVM_ELF_COMMA true>)
369 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
370 Elf_Xword r_info; // Symbol table index and type of relocation to apply
373 template<template<endianness, std::size_t, bool> class ELFT,
374 endianness TargetEndianness, std::size_t MaxAlign>
375 struct Elf_Rel_Base<ELFT<TargetEndianness, MaxAlign, false>, true> {
376 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
377 MaxAlign LLVM_ELF_COMMA false>)
378 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
379 Elf_Word r_info; // Symbol table index and type of relocation to apply
380 Elf_Sword r_addend; // Compute value for relocatable field by adding this
383 template<template<endianness, std::size_t, bool> class ELFT,
384 endianness TargetEndianness, std::size_t MaxAlign>
385 struct Elf_Rel_Base<ELFT<TargetEndianness, MaxAlign, true>, true> {
386 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
387 MaxAlign LLVM_ELF_COMMA true>)
388 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
389 Elf_Xword r_info; // Symbol table index and type of relocation to apply
390 Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
393 template<class ELFT, bool isRela>
396 template<template<endianness, std::size_t, bool> class ELFT,
397 endianness TargetEndianness, std::size_t MaxAlign, bool isRela>
398 struct Elf_Rel_Impl<ELFT<TargetEndianness, MaxAlign, true>, isRela>
399 : Elf_Rel_Base<ELFT<TargetEndianness, MaxAlign, true>, isRela> {
400 using Elf_Rel_Base<ELFT<TargetEndianness, MaxAlign, true>, isRela>::r_info;
401 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
402 MaxAlign LLVM_ELF_COMMA true>)
404 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
405 // and ELF64_R_INFO macros defined in the ELF specification:
406 uint32_t getSymbol() const { return (uint32_t) (r_info >> 32); }
407 uint32_t getType() const {
408 return (uint32_t) (r_info & 0xffffffffL);
410 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); }
411 void setType(uint32_t t) { setSymbolAndType(getSymbol(), t); }
412 void setSymbolAndType(uint32_t s, uint32_t t) {
413 r_info = ((uint64_t)s << 32) + (t&0xffffffffL);
417 template<template<endianness, std::size_t, bool> class ELFT,
418 endianness TargetEndianness, std::size_t MaxAlign, bool isRela>
419 struct Elf_Rel_Impl<ELFT<TargetEndianness, MaxAlign, false>, isRela>
420 : Elf_Rel_Base<ELFT<TargetEndianness, MaxAlign, false>, isRela> {
421 using Elf_Rel_Base<ELFT<TargetEndianness, MaxAlign, false>, isRela>::r_info;
422 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
423 MaxAlign LLVM_ELF_COMMA false>)
425 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
426 // and ELF32_R_INFO macros defined in the ELF specification:
427 uint32_t getSymbol() const { return (r_info >> 8); }
428 unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); }
429 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); }
430 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
431 void setSymbolAndType(uint32_t s, unsigned char t) {
432 r_info = (s << 8) + t;
437 struct Elf_Ehdr_Impl {
438 LLVM_ELF_IMPORT_TYPES(ELFT)
439 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
440 Elf_Half e_type; // Type of file (see ET_*)
441 Elf_Half e_machine; // Required architecture for this file (see EM_*)
442 Elf_Word e_version; // Must be equal to 1
443 Elf_Addr e_entry; // Address to jump to in order to start program
444 Elf_Off e_phoff; // Program header table's file offset, in bytes
445 Elf_Off e_shoff; // Section header table's file offset, in bytes
446 Elf_Word e_flags; // Processor-specific flags
447 Elf_Half e_ehsize; // Size of ELF header, in bytes
448 Elf_Half e_phentsize;// Size of an entry in the program header table
449 Elf_Half e_phnum; // Number of entries in the program header table
450 Elf_Half e_shentsize;// Size of an entry in the section header table
451 Elf_Half e_shnum; // Number of entries in the section header table
452 Elf_Half e_shstrndx; // Section header table index of section name
454 bool checkMagic() const {
455 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
457 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
458 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
462 struct Elf_Phdr_Impl;
464 template<template<endianness, std::size_t, bool> class ELFT,
465 endianness TargetEndianness, std::size_t MaxAlign>
466 struct Elf_Phdr_Impl<ELFT<TargetEndianness, MaxAlign, false> > {
467 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
468 MaxAlign LLVM_ELF_COMMA false>)
469 Elf_Word p_type; // Type of segment
470 Elf_Off p_offset; // FileOffset where segment is located, in bytes
471 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
472 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
473 Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
474 Elf_Word p_memsz; // Num. of bytes in mem image of segment (may be zero)
475 Elf_Word p_flags; // Segment flags
476 Elf_Word p_align; // Segment alignment constraint
479 template<template<endianness, std::size_t, bool> class ELFT,
480 endianness TargetEndianness, std::size_t MaxAlign>
481 struct Elf_Phdr_Impl<ELFT<TargetEndianness, MaxAlign, true> > {
482 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
483 MaxAlign LLVM_ELF_COMMA true>)
484 Elf_Word p_type; // Type of segment
485 Elf_Word p_flags; // Segment flags
486 Elf_Off p_offset; // FileOffset where segment is located, in bytes
487 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
488 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
489 Elf_Xword p_filesz; // Num. of bytes in file image of segment (may be zero)
490 Elf_Xword p_memsz; // Num. of bytes in mem image of segment (may be zero)
491 Elf_Xword p_align; // Segment alignment constraint
495 class ELFObjectFile : public ObjectFile {
496 LLVM_ELF_IMPORT_TYPES(ELFT)
498 typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
499 typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
500 typedef Elf_Sym_Impl<ELFT> Elf_Sym;
501 typedef Elf_Dyn_Impl<ELFT> Elf_Dyn;
502 typedef Elf_Phdr_Impl<ELFT> Elf_Phdr;
503 typedef Elf_Rel_Impl<ELFT, false> Elf_Rel;
504 typedef Elf_Rel_Impl<ELFT, true> Elf_Rela;
505 typedef Elf_Verdef_Impl<ELFT> Elf_Verdef;
506 typedef Elf_Verdaux_Impl<ELFT> Elf_Verdaux;
507 typedef Elf_Verneed_Impl<ELFT> Elf_Verneed;
508 typedef Elf_Vernaux_Impl<ELFT> Elf_Vernaux;
509 typedef Elf_Versym_Impl<ELFT> Elf_Versym;
510 typedef DynRefImpl<ELFT> DynRef;
511 typedef content_iterator<DynRef> dyn_iterator;
514 // This flag is used for classof, to distinguish ELFObjectFile from
515 // its subclass. If more subclasses will be created, this flag will
516 // have to become an enum.
517 bool isDyldELFObject;
520 typedef SmallVector<const Elf_Shdr*, 1> Sections_t;
521 typedef DenseMap<unsigned, unsigned> IndexMap_t;
522 typedef DenseMap<const Elf_Shdr*, SmallVector<uint32_t, 1> > RelocMap_t;
524 const Elf_Ehdr *Header;
525 const Elf_Shdr *SectionHeaderTable;
526 const Elf_Shdr *dot_shstrtab_sec; // Section header string table.
527 const Elf_Shdr *dot_strtab_sec; // Symbol header string table.
528 const Elf_Shdr *dot_dynstr_sec; // Dynamic symbol string table.
530 // SymbolTableSections[0] always points to the dynamic string table section
531 // header, or NULL if there is no dynamic string table.
532 Sections_t SymbolTableSections;
533 IndexMap_t SymbolTableSectionsIndexMap;
534 DenseMap<const Elf_Sym*, ELF::Elf64_Word> ExtendedSymbolTable;
536 const Elf_Shdr *dot_dynamic_sec; // .dynamic
537 const Elf_Shdr *dot_gnu_version_sec; // .gnu.version
538 const Elf_Shdr *dot_gnu_version_r_sec; // .gnu.version_r
539 const Elf_Shdr *dot_gnu_version_d_sec; // .gnu.version_d
541 // Pointer to SONAME entry in dynamic string table
542 // This is set the first time getLoadName is called.
543 mutable const char *dt_soname;
546 /// \brief Iterate over constant sized entities.
548 class ELFEntityIterator {
550 typedef void difference_type;
551 typedef EntT value_type;
552 typedef std::forward_iterator_tag iterator_category;
553 typedef value_type &reference;
554 typedef value_type *pointer;
556 /// \brief Default construct iterator.
557 ELFEntityIterator() : EntitySize(0), Current(0) {}
558 ELFEntityIterator(uint64_t EntSize, const char *Start)
559 : EntitySize(EntSize)
562 reference operator *() {
563 assert(Current && "Attempted to dereference an invalid iterator!");
564 return *reinterpret_cast<pointer>(Current);
567 pointer operator ->() {
568 assert(Current && "Attempted to dereference an invalid iterator!");
569 return reinterpret_cast<pointer>(Current);
572 bool operator ==(const ELFEntityIterator &Other) {
573 return Current == Other.Current;
576 bool operator !=(const ELFEntityIterator &Other) {
577 return !(*this == Other);
580 ELFEntityIterator &operator ++() {
581 assert(Current && "Attempted to increment an invalid iterator!");
582 Current += EntitySize;
586 ELFEntityIterator operator ++(int) {
587 ELFEntityIterator Tmp = *this;
593 const uint64_t EntitySize;
598 // Records for each version index the corresponding Verdef or Vernaux entry.
599 // This is filled the first time LoadVersionMap() is called.
600 class VersionMapEntry : public PointerIntPair<const void*, 1> {
602 // If the integer is 0, this is an Elf_Verdef*.
603 // If the integer is 1, this is an Elf_Vernaux*.
604 VersionMapEntry() : PointerIntPair<const void*, 1>(NULL, 0) { }
605 VersionMapEntry(const Elf_Verdef *verdef)
606 : PointerIntPair<const void*, 1>(verdef, 0) { }
607 VersionMapEntry(const Elf_Vernaux *vernaux)
608 : PointerIntPair<const void*, 1>(vernaux, 1) { }
609 bool isNull() const { return getPointer() == NULL; }
610 bool isVerdef() const { return !isNull() && getInt() == 0; }
611 bool isVernaux() const { return !isNull() && getInt() == 1; }
612 const Elf_Verdef *getVerdef() const {
613 return isVerdef() ? (const Elf_Verdef*)getPointer() : NULL;
615 const Elf_Vernaux *getVernaux() const {
616 return isVernaux() ? (const Elf_Vernaux*)getPointer() : NULL;
619 mutable SmallVector<VersionMapEntry, 16> VersionMap;
620 void LoadVersionDefs(const Elf_Shdr *sec) const;
621 void LoadVersionNeeds(const Elf_Shdr *ec) const;
622 void LoadVersionMap() const;
624 /// @brief Map sections to an array of relocation sections that reference
625 /// them sorted by section index.
626 RelocMap_t SectionRelocMap;
628 /// @brief Get the relocation section that contains \a Rel.
629 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
630 return getSection(Rel.w.b);
633 bool isRelocationHasAddend(DataRefImpl Rel) const;
635 const T *getEntry(uint16_t Section, uint32_t Entry) const;
637 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
638 const Elf_Shdr *getSection(DataRefImpl index) const;
639 const Elf_Shdr *getSection(uint32_t index) const;
640 const Elf_Rel *getRel(DataRefImpl Rel) const;
641 const Elf_Rela *getRela(DataRefImpl Rela) const;
642 const char *getString(uint32_t section, uint32_t offset) const;
643 const char *getString(const Elf_Shdr *section, uint32_t offset) const;
644 error_code getSymbolVersion(const Elf_Shdr *section,
647 bool &IsDefault) const;
648 void VerifyStrTab(const Elf_Shdr *sh) const;
651 const Elf_Sym *getSymbol(DataRefImpl Symb) const; // FIXME: Should be private?
652 void validateSymbol(DataRefImpl Symb) const;
655 error_code getSymbolName(const Elf_Shdr *section,
657 StringRef &Res) const;
658 error_code getSectionName(const Elf_Shdr *section,
659 StringRef &Res) const;
660 const Elf_Dyn *getDyn(DataRefImpl DynData) const;
661 error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
662 bool &IsDefault) const;
663 uint64_t getSymbolIndex(const Elf_Sym *sym) const;
665 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
666 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
667 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
668 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
669 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
670 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
671 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
672 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
673 virtual error_code getSymbolSection(DataRefImpl Symb,
674 section_iterator &Res) const;
675 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
677 friend class DynRefImpl<ELFT>;
678 virtual error_code getDynNext(DataRefImpl DynData, DynRef &Result) const;
680 virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const;
681 virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const;
683 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
684 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
685 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
686 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
687 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
688 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
689 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
690 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
691 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
692 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
694 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
695 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
696 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
697 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
699 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
700 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
702 virtual error_code getRelocationNext(DataRefImpl Rel,
703 RelocationRef &Res) const;
704 virtual error_code getRelocationAddress(DataRefImpl Rel,
705 uint64_t &Res) const;
706 virtual error_code getRelocationOffset(DataRefImpl Rel,
707 uint64_t &Res) const;
708 virtual error_code getRelocationSymbol(DataRefImpl Rel,
709 SymbolRef &Res) const;
710 virtual error_code getRelocationType(DataRefImpl Rel,
711 uint64_t &Res) const;
712 virtual error_code getRelocationTypeName(DataRefImpl Rel,
713 SmallVectorImpl<char> &Result) const;
714 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
716 virtual error_code getRelocationValueString(DataRefImpl Rel,
717 SmallVectorImpl<char> &Result) const;
720 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
721 virtual symbol_iterator begin_symbols() const;
722 virtual symbol_iterator end_symbols() const;
724 virtual symbol_iterator begin_dynamic_symbols() const;
725 virtual symbol_iterator end_dynamic_symbols() const;
727 virtual section_iterator begin_sections() const;
728 virtual section_iterator end_sections() const;
730 virtual library_iterator begin_libraries_needed() const;
731 virtual library_iterator end_libraries_needed() const;
733 virtual dyn_iterator begin_dynamic_table() const;
734 virtual dyn_iterator end_dynamic_table() const;
736 typedef ELFEntityIterator<const Elf_Rela> Elf_Rela_Iter;
737 typedef ELFEntityIterator<const Elf_Rel> Elf_Rel_Iter;
739 Elf_Rela_Iter beginELFRela(const Elf_Shdr *sec) const {
740 return Elf_Rela_Iter(sec->sh_entsize,
741 (const char *)(base() + sec->sh_offset));
744 Elf_Rela_Iter endELFRela(const Elf_Shdr *sec) const {
745 return Elf_Rela_Iter(sec->sh_entsize, (const char *)
746 (base() + sec->sh_offset + sec->sh_size));
749 Elf_Rel_Iter beginELFRel(const Elf_Shdr *sec) const {
750 return Elf_Rel_Iter(sec->sh_entsize,
751 (const char *)(base() + sec->sh_offset));
754 Elf_Rel_Iter endELFRel(const Elf_Shdr *sec) const {
755 return Elf_Rel_Iter(sec->sh_entsize, (const char *)
756 (base() + sec->sh_offset + sec->sh_size));
759 /// \brief Iterate over program header table.
760 typedef ELFEntityIterator<const Elf_Phdr> Elf_Phdr_Iter;
762 Elf_Phdr_Iter begin_program_headers() const {
763 return Elf_Phdr_Iter(Header->e_phentsize,
764 (const char*)base() + Header->e_phoff);
767 Elf_Phdr_Iter end_program_headers() const {
768 return Elf_Phdr_Iter(Header->e_phentsize,
769 (const char*)base() +
771 (Header->e_phnum * Header->e_phentsize));
774 virtual uint8_t getBytesInAddress() const;
775 virtual StringRef getFileFormatName() const;
776 virtual StringRef getObjectType() const { return "ELF"; }
777 virtual unsigned getArch() const;
778 virtual StringRef getLoadName() const;
779 virtual error_code getSectionContents(const Elf_Shdr *sec,
780 StringRef &Res) const;
782 uint64_t getNumSections() const;
783 uint64_t getStringTableIndex() const;
784 ELF::Elf64_Word getSymbolTableIndex(const Elf_Sym *symb) const;
785 const Elf_Shdr *getSection(const Elf_Sym *symb) const;
786 const Elf_Shdr *getElfSection(section_iterator &It) const;
787 const Elf_Sym *getElfSymbol(symbol_iterator &It) const;
788 const Elf_Sym *getElfSymbol(uint32_t index) const;
790 // Methods for type inquiry through isa, cast, and dyn_cast
791 bool isDyldType() const { return isDyldELFObject; }
792 static inline bool classof(const Binary *v) {
793 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
798 // Iterate through the version definitions, and place each Elf_Verdef
799 // in the VersionMap according to its index.
801 void ELFObjectFile<ELFT>::LoadVersionDefs(const Elf_Shdr *sec) const {
802 unsigned vd_size = sec->sh_size; // Size of section in bytes
803 unsigned vd_count = sec->sh_info; // Number of Verdef entries
804 const char *sec_start = (const char*)base() + sec->sh_offset;
805 const char *sec_end = sec_start + vd_size;
806 // The first Verdef entry is at the start of the section.
807 const char *p = sec_start;
808 for (unsigned i = 0; i < vd_count; i++) {
809 if (p + sizeof(Elf_Verdef) > sec_end)
810 report_fatal_error("Section ended unexpectedly while scanning "
811 "version definitions.");
812 const Elf_Verdef *vd = reinterpret_cast<const Elf_Verdef *>(p);
813 if (vd->vd_version != ELF::VER_DEF_CURRENT)
814 report_fatal_error("Unexpected verdef version");
815 size_t index = vd->vd_ndx & ELF::VERSYM_VERSION;
816 if (index >= VersionMap.size())
817 VersionMap.resize(index+1);
818 VersionMap[index] = VersionMapEntry(vd);
823 // Iterate through the versions needed section, and place each Elf_Vernaux
824 // in the VersionMap according to its index.
826 void ELFObjectFile<ELFT>::LoadVersionNeeds(const Elf_Shdr *sec) const {
827 unsigned vn_size = sec->sh_size; // Size of section in bytes
828 unsigned vn_count = sec->sh_info; // Number of Verneed entries
829 const char *sec_start = (const char*)base() + sec->sh_offset;
830 const char *sec_end = sec_start + vn_size;
831 // The first Verneed entry is at the start of the section.
832 const char *p = sec_start;
833 for (unsigned i = 0; i < vn_count; i++) {
834 if (p + sizeof(Elf_Verneed) > sec_end)
835 report_fatal_error("Section ended unexpectedly while scanning "
836 "version needed records.");
837 const Elf_Verneed *vn = reinterpret_cast<const Elf_Verneed *>(p);
838 if (vn->vn_version != ELF::VER_NEED_CURRENT)
839 report_fatal_error("Unexpected verneed version");
840 // Iterate through the Vernaux entries
841 const char *paux = p + vn->vn_aux;
842 for (unsigned j = 0; j < vn->vn_cnt; j++) {
843 if (paux + sizeof(Elf_Vernaux) > sec_end)
844 report_fatal_error("Section ended unexpected while scanning auxiliary "
845 "version needed records.");
846 const Elf_Vernaux *vna = reinterpret_cast<const Elf_Vernaux *>(paux);
847 size_t index = vna->vna_other & ELF::VERSYM_VERSION;
848 if (index >= VersionMap.size())
849 VersionMap.resize(index+1);
850 VersionMap[index] = VersionMapEntry(vna);
851 paux += vna->vna_next;
858 void ELFObjectFile<ELFT>::LoadVersionMap() const {
859 // If there is no dynamic symtab or version table, there is nothing to do.
860 if (SymbolTableSections[0] == NULL || dot_gnu_version_sec == NULL)
863 // Has the VersionMap already been loaded?
864 if (VersionMap.size() > 0)
867 // The first two version indexes are reserved.
868 // Index 0 is LOCAL, index 1 is GLOBAL.
869 VersionMap.push_back(VersionMapEntry());
870 VersionMap.push_back(VersionMapEntry());
872 if (dot_gnu_version_d_sec)
873 LoadVersionDefs(dot_gnu_version_d_sec);
875 if (dot_gnu_version_r_sec)
876 LoadVersionNeeds(dot_gnu_version_r_sec);
880 void ELFObjectFile<ELFT>::validateSymbol(DataRefImpl Symb) const {
881 const Elf_Sym *symb = getSymbol(Symb);
882 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
883 // FIXME: We really need to do proper error handling in the case of an invalid
884 // input file. Because we don't use exceptions, I think we'll just pass
885 // an error object around.
887 && SymbolTableSection
888 && symb >= (const Elf_Sym*)(base()
889 + SymbolTableSection->sh_offset)
890 && symb < (const Elf_Sym*)(base()
891 + SymbolTableSection->sh_offset
892 + SymbolTableSection->sh_size)))
893 // FIXME: Proper error handling.
894 report_fatal_error("Symb must point to a valid symbol!");
898 error_code ELFObjectFile<ELFT>::getSymbolNext(DataRefImpl Symb,
899 SymbolRef &Result) const {
900 validateSymbol(Symb);
901 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
904 // Check to see if we are at the end of this symbol table.
905 if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
906 // We are at the end. If there are other symbol tables, jump to them.
907 // If the symbol table is .dynsym, we are iterating dynamic symbols,
908 // and there is only one table of these.
911 Symb.d.a = 1; // The 0th symbol in ELF is fake.
913 // Otherwise return the terminator.
914 if (Symb.d.b == 0 || Symb.d.b >= SymbolTableSections.size()) {
915 Symb.d.a = std::numeric_limits<uint32_t>::max();
916 Symb.d.b = std::numeric_limits<uint32_t>::max();
920 Result = SymbolRef(Symb, this);
921 return object_error::success;
925 error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
926 StringRef &Result) const {
927 validateSymbol(Symb);
928 const Elf_Sym *symb = getSymbol(Symb);
929 return getSymbolName(SymbolTableSections[Symb.d.b], symb, Result);
933 error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
935 bool &IsDefault) const {
936 DataRefImpl Symb = SymRef.getRawDataRefImpl();
937 validateSymbol(Symb);
938 const Elf_Sym *symb = getSymbol(Symb);
939 return getSymbolVersion(SymbolTableSections[Symb.d.b], symb,
944 ELF::Elf64_Word ELFObjectFile<ELFT>
945 ::getSymbolTableIndex(const Elf_Sym *symb) const {
946 if (symb->st_shndx == ELF::SHN_XINDEX)
947 return ExtendedSymbolTable.lookup(symb);
948 return symb->st_shndx;
952 const typename ELFObjectFile<ELFT>::Elf_Shdr *
953 ELFObjectFile<ELFT>::getSection(const Elf_Sym *symb) const {
954 if (symb->st_shndx == ELF::SHN_XINDEX)
955 return getSection(ExtendedSymbolTable.lookup(symb));
956 if (symb->st_shndx >= ELF::SHN_LORESERVE)
958 return getSection(symb->st_shndx);
962 const typename ELFObjectFile<ELFT>::Elf_Shdr *
963 ELFObjectFile<ELFT>::getElfSection(section_iterator &It) const {
964 llvm::object::DataRefImpl ShdrRef = It->getRawDataRefImpl();
965 return reinterpret_cast<const Elf_Shdr *>(ShdrRef.p);
969 const typename ELFObjectFile<ELFT>::Elf_Sym *
970 ELFObjectFile<ELFT>::getElfSymbol(symbol_iterator &It) const {
971 return getSymbol(It->getRawDataRefImpl());
975 const typename ELFObjectFile<ELFT>::Elf_Sym *
976 ELFObjectFile<ELFT>::getElfSymbol(uint32_t index) const {
977 DataRefImpl SymbolData;
978 SymbolData.d.a = index;
980 return getSymbol(SymbolData);
984 error_code ELFObjectFile<ELFT>::getSymbolFileOffset(DataRefImpl Symb,
985 uint64_t &Result) const {
986 validateSymbol(Symb);
987 const Elf_Sym *symb = getSymbol(Symb);
988 const Elf_Shdr *Section;
989 switch (getSymbolTableIndex(symb)) {
990 case ELF::SHN_COMMON:
991 // Unintialized symbols have no offset in the object file
993 Result = UnknownAddressOrSize;
994 return object_error::success;
996 Result = symb->st_value;
997 return object_error::success;
998 default: Section = getSection(symb);
1001 switch (symb->getType()) {
1002 case ELF::STT_SECTION:
1003 Result = Section ? Section->sh_offset : UnknownAddressOrSize;
1004 return object_error::success;
1006 case ELF::STT_OBJECT:
1007 case ELF::STT_NOTYPE:
1008 Result = symb->st_value +
1009 (Section ? Section->sh_offset : 0);
1010 return object_error::success;
1012 Result = UnknownAddressOrSize;
1013 return object_error::success;
1017 template<class ELFT>
1018 error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
1019 uint64_t &Result) const {
1020 validateSymbol(Symb);
1021 const Elf_Sym *symb = getSymbol(Symb);
1022 const Elf_Shdr *Section;
1023 switch (getSymbolTableIndex(symb)) {
1024 case ELF::SHN_COMMON:
1025 case ELF::SHN_UNDEF:
1026 Result = UnknownAddressOrSize;
1027 return object_error::success;
1029 Result = symb->st_value;
1030 return object_error::success;
1031 default: Section = getSection(symb);
1034 switch (symb->getType()) {
1035 case ELF::STT_SECTION:
1036 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
1037 return object_error::success;
1039 case ELF::STT_OBJECT:
1040 case ELF::STT_NOTYPE:
1042 switch(Header->e_type) {
1045 IsRelocatable = false;
1048 IsRelocatable = true;
1050 Result = symb->st_value;
1051 if (IsRelocatable && Section != 0)
1052 Result += Section->sh_addr;
1053 return object_error::success;
1055 Result = UnknownAddressOrSize;
1056 return object_error::success;
1060 template<class ELFT>
1061 error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
1062 uint64_t &Result) const {
1063 validateSymbol(Symb);
1064 const Elf_Sym *symb = getSymbol(Symb);
1065 if (symb->st_size == 0)
1066 Result = UnknownAddressOrSize;
1067 Result = symb->st_size;
1068 return object_error::success;
1071 template<class ELFT>
1072 error_code ELFObjectFile<ELFT>::getSymbolNMTypeChar(DataRefImpl Symb,
1073 char &Result) const {
1074 validateSymbol(Symb);
1075 const Elf_Sym *symb = getSymbol(Symb);
1076 const Elf_Shdr *Section = getSection(symb);
1081 switch (Section->sh_type) {
1082 case ELF::SHT_PROGBITS:
1083 case ELF::SHT_DYNAMIC:
1084 switch (Section->sh_flags) {
1085 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
1087 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
1089 case ELF::SHF_ALLOC:
1090 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
1091 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
1095 case ELF::SHT_NOBITS: ret = 'b';
1099 switch (getSymbolTableIndex(symb)) {
1100 case ELF::SHN_UNDEF:
1104 case ELF::SHN_ABS: ret = 'a'; break;
1105 case ELF::SHN_COMMON: ret = 'c'; break;
1108 switch (symb->getBinding()) {
1109 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
1111 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1114 if (symb->getType() == ELF::STT_OBJECT)
1120 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
1122 if (error_code ec = getSymbolName(Symb, name))
1124 Result = StringSwitch<char>(name)
1125 .StartsWith(".debug", 'N')
1126 .StartsWith(".note", 'n')
1128 return object_error::success;
1132 return object_error::success;
1135 template<class ELFT>
1136 error_code ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
1137 SymbolRef::Type &Result) const {
1138 validateSymbol(Symb);
1139 const Elf_Sym *symb = getSymbol(Symb);
1141 switch (symb->getType()) {
1142 case ELF::STT_NOTYPE:
1143 Result = SymbolRef::ST_Unknown;
1145 case ELF::STT_SECTION:
1146 Result = SymbolRef::ST_Debug;
1149 Result = SymbolRef::ST_File;
1152 Result = SymbolRef::ST_Function;
1154 case ELF::STT_OBJECT:
1155 case ELF::STT_COMMON:
1157 Result = SymbolRef::ST_Data;
1160 Result = SymbolRef::ST_Other;
1163 return object_error::success;
1166 template<class ELFT>
1167 error_code ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb,
1168 uint32_t &Result) const {
1169 validateSymbol(Symb);
1170 const Elf_Sym *symb = getSymbol(Symb);
1172 Result = SymbolRef::SF_None;
1174 if (symb->getBinding() != ELF::STB_LOCAL)
1175 Result |= SymbolRef::SF_Global;
1177 if (symb->getBinding() == ELF::STB_WEAK)
1178 Result |= SymbolRef::SF_Weak;
1180 if (symb->st_shndx == ELF::SHN_ABS)
1181 Result |= SymbolRef::SF_Absolute;
1183 if (symb->getType() == ELF::STT_FILE ||
1184 symb->getType() == ELF::STT_SECTION)
1185 Result |= SymbolRef::SF_FormatSpecific;
1187 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1188 Result |= SymbolRef::SF_Undefined;
1190 if (symb->getType() == ELF::STT_COMMON ||
1191 getSymbolTableIndex(symb) == ELF::SHN_COMMON)
1192 Result |= SymbolRef::SF_Common;
1194 if (symb->getType() == ELF::STT_TLS)
1195 Result |= SymbolRef::SF_ThreadLocal;
1197 return object_error::success;
1200 template<class ELFT>
1201 error_code ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
1202 section_iterator &Res) const {
1203 validateSymbol(Symb);
1204 const Elf_Sym *symb = getSymbol(Symb);
1205 const Elf_Shdr *sec = getSection(symb);
1207 Res = end_sections();
1210 Sec.p = reinterpret_cast<intptr_t>(sec);
1211 Res = section_iterator(SectionRef(Sec, this));
1213 return object_error::success;
1216 template<class ELFT>
1217 error_code ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb,
1218 uint64_t &Val) const {
1219 validateSymbol(Symb);
1220 const Elf_Sym *symb = getSymbol(Symb);
1221 Val = symb->st_value;
1222 return object_error::success;
1225 template<class ELFT>
1226 error_code ELFObjectFile<ELFT>::getSectionNext(DataRefImpl Sec,
1227 SectionRef &Result) const {
1228 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
1229 sec += Header->e_shentsize;
1230 Sec.p = reinterpret_cast<intptr_t>(sec);
1231 Result = SectionRef(Sec, this);
1232 return object_error::success;
1235 template<class ELFT>
1236 error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
1237 StringRef &Result) const {
1238 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1239 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
1240 return object_error::success;
1243 template<class ELFT>
1244 error_code ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec,
1245 uint64_t &Result) const {
1246 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1247 Result = sec->sh_addr;
1248 return object_error::success;
1251 template<class ELFT>
1252 error_code ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec,
1253 uint64_t &Result) const {
1254 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1255 Result = sec->sh_size;
1256 return object_error::success;
1259 template<class ELFT>
1260 error_code ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
1261 StringRef &Result) const {
1262 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1263 const char *start = (const char*)base() + sec->sh_offset;
1264 Result = StringRef(start, sec->sh_size);
1265 return object_error::success;
1268 template<class ELFT>
1269 error_code ELFObjectFile<ELFT>::getSectionContents(const Elf_Shdr *Sec,
1270 StringRef &Result) const {
1271 const char *start = (const char*)base() + Sec->sh_offset;
1272 Result = StringRef(start, Sec->sh_size);
1273 return object_error::success;
1276 template<class ELFT>
1277 error_code ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec,
1278 uint64_t &Result) const {
1279 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1280 Result = sec->sh_addralign;
1281 return object_error::success;
1284 template<class ELFT>
1285 error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec,
1286 bool &Result) const {
1287 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1288 if (sec->sh_flags & ELF::SHF_EXECINSTR)
1292 return object_error::success;
1295 template<class ELFT>
1296 error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec,
1297 bool &Result) const {
1298 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1299 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1300 && sec->sh_type == ELF::SHT_PROGBITS)
1304 return object_error::success;
1307 template<class ELFT>
1308 error_code ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec,
1309 bool &Result) const {
1310 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1311 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1312 && sec->sh_type == ELF::SHT_NOBITS)
1316 return object_error::success;
1319 template<class ELFT>
1320 error_code ELFObjectFile<ELFT>::isSectionRequiredForExecution(
1321 DataRefImpl Sec, bool &Result) const {
1322 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1323 if (sec->sh_flags & ELF::SHF_ALLOC)
1327 return object_error::success;
1330 template<class ELFT>
1331 error_code ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec,
1332 bool &Result) const {
1333 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1334 if (sec->sh_type == ELF::SHT_NOBITS)
1338 return object_error::success;
1341 template<class ELFT>
1342 error_code ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec,
1343 bool &Result) const {
1344 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1345 // For ELF, all zero-init sections are virtual (that is, they occupy no space
1346 // in the object image) and vice versa.
1347 Result = sec->sh_type == ELF::SHT_NOBITS;
1348 return object_error::success;
1351 template<class ELFT>
1352 error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec,
1353 bool &Result) const {
1354 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1355 if (sec->sh_flags & ELF::SHF_WRITE || sec->sh_flags & ELF::SHF_EXECINSTR)
1359 return object_error::success;
1362 template<class ELFT>
1363 error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
1365 bool &Result) const {
1366 // FIXME: Unimplemented.
1368 return object_error::success;
1371 template<class ELFT>
1373 ELFObjectFile<ELFT>::getSectionRelBegin(DataRefImpl Sec) const {
1374 DataRefImpl RelData;
1375 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1376 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1377 if (sec != 0 && ittr != SectionRelocMap.end()) {
1378 RelData.w.a = getSection(ittr->second[0])->sh_info;
1379 RelData.w.b = ittr->second[0];
1382 return relocation_iterator(RelocationRef(RelData, this));
1385 template<class ELFT>
1387 ELFObjectFile<ELFT>::getSectionRelEnd(DataRefImpl Sec) const {
1388 DataRefImpl RelData;
1389 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1390 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1391 if (sec != 0 && ittr != SectionRelocMap.end()) {
1392 // Get the index of the last relocation section for this section.
1393 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
1394 const Elf_Shdr *relocsec = getSection(relocsecindex);
1395 RelData.w.a = relocsec->sh_info;
1396 RelData.w.b = relocsecindex;
1397 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
1399 return relocation_iterator(RelocationRef(RelData, this));
1403 template<class ELFT>
1404 error_code ELFObjectFile<ELFT>::getRelocationNext(DataRefImpl Rel,
1405 RelocationRef &Result) const {
1407 const Elf_Shdr *relocsec = getSection(Rel.w.b);
1408 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
1409 // We have reached the end of the relocations for this section. See if there
1410 // is another relocation section.
1411 typename RelocMap_t::mapped_type relocseclist =
1412 SectionRelocMap.lookup(getSection(Rel.w.a));
1414 // Do a binary search for the current reloc section index (which must be
1415 // present). Then get the next one.
1416 typename RelocMap_t::mapped_type::const_iterator loc =
1417 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
1420 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
1421 // to the end iterator.
1422 if (loc != relocseclist.end()) {
1427 Result = RelocationRef(Rel, this);
1428 return object_error::success;
1431 template<class ELFT>
1432 error_code ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel,
1433 SymbolRef &Result) const {
1435 const Elf_Shdr *sec = getSection(Rel.w.b);
1436 switch (sec->sh_type) {
1438 report_fatal_error("Invalid section type in Rel!");
1439 case ELF::SHT_REL : {
1440 symbolIdx = getRel(Rel)->getSymbol();
1443 case ELF::SHT_RELA : {
1444 symbolIdx = getRela(Rel)->getSymbol();
1448 DataRefImpl SymbolData;
1449 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
1450 if (it == SymbolTableSectionsIndexMap.end())
1451 report_fatal_error("Relocation symbol table not found!");
1452 SymbolData.d.a = symbolIdx;
1453 SymbolData.d.b = it->second;
1454 Result = SymbolRef(SymbolData, this);
1455 return object_error::success;
1458 template<class ELFT>
1459 error_code ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
1460 uint64_t &Result) const {
1462 const Elf_Shdr *sec = getSection(Rel.w.b);
1463 switch (sec->sh_type) {
1465 report_fatal_error("Invalid section type in Rel!");
1466 case ELF::SHT_REL : {
1467 offset = getRel(Rel)->r_offset;
1470 case ELF::SHT_RELA : {
1471 offset = getRela(Rel)->r_offset;
1477 return object_error::success;
1480 template<class ELFT>
1481 error_code ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
1482 uint64_t &Result) const {
1484 const Elf_Shdr *sec = getSection(Rel.w.b);
1485 switch (sec->sh_type) {
1487 report_fatal_error("Invalid section type in Rel!");
1488 case ELF::SHT_REL : {
1489 offset = getRel(Rel)->r_offset;
1492 case ELF::SHT_RELA : {
1493 offset = getRela(Rel)->r_offset;
1498 Result = offset - sec->sh_addr;
1499 return object_error::success;
1502 template<class ELFT>
1503 error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
1504 uint64_t &Result) const {
1505 const Elf_Shdr *sec = getSection(Rel.w.b);
1506 switch (sec->sh_type) {
1508 report_fatal_error("Invalid section type in Rel!");
1509 case ELF::SHT_REL : {
1510 Result = getRel(Rel)->getType();
1513 case ELF::SHT_RELA : {
1514 Result = getRela(Rel)->getType();
1518 return object_error::success;
1521 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
1522 case ELF::enum: res = #enum; break;
1524 template<class ELFT>
1525 error_code ELFObjectFile<ELFT>::getRelocationTypeName(
1526 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1527 const Elf_Shdr *sec = getSection(Rel.w.b);
1530 switch (sec->sh_type) {
1532 return object_error::parse_failed;
1533 case ELF::SHT_REL : {
1534 type = getRel(Rel)->getType();
1537 case ELF::SHT_RELA : {
1538 type = getRela(Rel)->getType();
1542 switch (Header->e_machine) {
1543 case ELF::EM_X86_64:
1545 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
1546 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
1547 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
1548 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
1549 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
1550 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
1551 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
1552 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
1553 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
1554 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
1555 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
1556 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
1557 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
1558 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
1559 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
1560 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
1561 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
1562 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
1563 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
1564 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
1565 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
1566 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
1567 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
1568 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
1569 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
1570 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
1571 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
1572 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
1573 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
1574 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
1575 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
1576 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
1583 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
1584 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
1585 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
1586 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
1587 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
1588 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
1589 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
1590 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
1591 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
1592 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
1593 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
1594 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
1595 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
1596 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
1597 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
1598 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
1599 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
1600 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
1601 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
1602 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
1603 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
1604 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
1605 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
1606 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
1607 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
1608 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
1609 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
1610 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
1611 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
1612 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
1613 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
1614 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
1615 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
1616 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
1617 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
1618 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
1619 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
1620 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
1621 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
1622 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
1629 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_NONE);
1630 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PC24);
1631 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32);
1632 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32);
1633 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G0);
1634 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS16);
1635 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS12);
1636 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ABS5);
1637 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS8);
1638 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL32);
1639 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_CALL);
1640 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC8);
1641 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BREL_ADJ);
1642 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESC);
1643 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_SWI8);
1644 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_XPC25);
1645 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_XPC22);
1646 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPMOD32);
1647 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPOFF32);
1648 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_TPOFF32);
1649 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_COPY);
1650 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GLOB_DAT);
1651 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP_SLOT);
1652 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_RELATIVE);
1653 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF32);
1654 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_PREL);
1655 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL);
1656 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32);
1657 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_CALL);
1658 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP24);
1659 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP24);
1660 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_ABS);
1661 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_7_0);
1662 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_15_8);
1663 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_23_15);
1664 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SBREL_11_0_NC);
1665 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_19_12_NC);
1666 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_27_20_CK);
1667 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET1);
1668 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL31);
1669 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_V4BX);
1670 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET2);
1671 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PREL31);
1672 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_ABS_NC);
1673 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_ABS);
1674 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_PREL_NC);
1675 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_PREL);
1676 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_ABS_NC);
1677 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_ABS);
1678 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_PREL_NC);
1679 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_PREL);
1680 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP19);
1681 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP6);
1682 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ALU_PREL_11_0);
1683 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC12);
1684 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32_NOI);
1685 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32_NOI);
1686 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0_NC);
1687 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0);
1688 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1_NC);
1689 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1);
1690 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G2);
1691 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G1);
1692 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G2);
1693 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G0);
1694 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G1);
1695 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G2);
1696 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G0);
1697 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G1);
1698 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G2);
1699 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0_NC);
1700 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0);
1701 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1_NC);
1702 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1);
1703 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G2);
1704 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G0);
1705 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G1);
1706 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G2);
1707 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G0);
1708 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G1);
1709 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G2);
1710 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G0);
1711 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G1);
1712 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G2);
1713 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL_NC);
1714 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_BREL);
1715 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL);
1716 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL_NC);
1717 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_BREL);
1718 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL);
1719 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GOTDESC);
1720 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_CALL);
1721 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESCSEQ);
1722 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_CALL);
1723 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32_ABS);
1724 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_ABS);
1725 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_PREL);
1726 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL12);
1727 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF12);
1728 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTRELAX);
1729 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTENTRY);
1730 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTINHERIT);
1731 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP11);
1732 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP8);
1733 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GD32);
1734 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDM32);
1735 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO32);
1736 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE32);
1737 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE32);
1738 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO12);
1739 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE12);
1740 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE12GP);
1741 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_0);
1742 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_1);
1743 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_2);
1744 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_3);
1745 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_4);
1746 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_5);
1747 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_6);
1748 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_7);
1749 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_8);
1750 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_9);
1751 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_10);
1752 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_11);
1753 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_12);
1754 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_13);
1755 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_14);
1756 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_15);
1757 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ME_TOO);
1758 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ16);
1759 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ32);
1764 case ELF::EM_HEXAGON:
1766 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_NONE);
1767 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL);
1768 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL);
1769 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL);
1770 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_LO16);
1771 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HI16);
1772 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32);
1773 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16);
1774 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8);
1775 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_0);
1776 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_1);
1777 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_2);
1778 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_3);
1779 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HL16);
1780 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL);
1781 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL);
1782 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B32_PCREL_X);
1783 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_6_X);
1784 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL_X);
1785 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL_X);
1786 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL_X);
1787 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL_X);
1788 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL_X);
1789 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16_X);
1790 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_12_X);
1791 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_11_X);
1792 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_10_X);
1793 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_9_X);
1794 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8_X);
1795 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_7_X);
1796 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_X);
1797 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_PCREL);
1798 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_COPY);
1799 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GLOB_DAT);
1800 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_JMP_SLOT);
1801 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_RELATIVE);
1802 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_PLT_B22_PCREL);
1803 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_LO16);
1804 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_HI16);
1805 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32);
1806 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_LO16);
1807 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_HI16);
1808 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32);
1809 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16);
1810 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPMOD_32);
1811 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_LO16);
1812 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_HI16);
1813 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32);
1814 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16);
1815 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_PLT_B22_PCREL);
1816 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_LO16);
1817 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_HI16);
1818 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32);
1819 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16);
1820 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_LO16);
1821 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_HI16);
1822 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32);
1823 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_LO16);
1824 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_HI16);
1825 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32);
1826 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16);
1827 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_LO16);
1828 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_HI16);
1829 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32);
1830 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16);
1831 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_PCREL_X);
1832 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32_6_X);
1833 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_16_X);
1834 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_11_X);
1835 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32_6_X);
1836 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16_X);
1837 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_11_X);
1838 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32_6_X);
1839 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16_X);
1840 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_11_X);
1841 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32_6_X);
1842 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16_X);
1843 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_11_X);
1844 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32_6_X);
1845 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_16_X);
1846 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32_6_X);
1847 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16_X);
1848 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_11_X);
1849 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32_6_X);
1850 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16_X);
1851 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_11_X);
1859 Result.append(res.begin(), res.end());
1860 return object_error::success;
1863 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
1865 template<class ELFT>
1866 error_code ELFObjectFile<ELFT>::getRelocationAdditionalInfo(
1867 DataRefImpl Rel, int64_t &Result) const {
1868 const Elf_Shdr *sec = getSection(Rel.w.b);
1869 switch (sec->sh_type) {
1871 report_fatal_error("Invalid section type in Rel!");
1872 case ELF::SHT_REL : {
1874 return object_error::success;
1876 case ELF::SHT_RELA : {
1877 Result = getRela(Rel)->r_addend;
1878 return object_error::success;
1883 template<class ELFT>
1884 error_code ELFObjectFile<ELFT>::getRelocationValueString(
1885 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1886 const Elf_Shdr *sec = getSection(Rel.w.b);
1890 uint16_t symbol_index = 0;
1891 switch (sec->sh_type) {
1893 return object_error::parse_failed;
1894 case ELF::SHT_REL: {
1895 type = getRel(Rel)->getType();
1896 symbol_index = getRel(Rel)->getSymbol();
1897 // TODO: Read implicit addend from section data.
1900 case ELF::SHT_RELA: {
1901 type = getRela(Rel)->getType();
1902 symbol_index = getRela(Rel)->getSymbol();
1903 addend = getRela(Rel)->r_addend;
1907 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1909 if (error_code ec = getSymbolName(getSection(sec->sh_link), symb, symname))
1911 switch (Header->e_machine) {
1912 case ELF::EM_X86_64:
1914 case ELF::R_X86_64_PC8:
1915 case ELF::R_X86_64_PC16:
1916 case ELF::R_X86_64_PC32: {
1918 raw_string_ostream fmt(fmtbuf);
1919 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1921 Result.append(fmtbuf.begin(), fmtbuf.end());
1924 case ELF::R_X86_64_8:
1925 case ELF::R_X86_64_16:
1926 case ELF::R_X86_64_32:
1927 case ELF::R_X86_64_32S:
1928 case ELF::R_X86_64_64: {
1930 raw_string_ostream fmt(fmtbuf);
1931 fmt << symname << (addend < 0 ? "" : "+") << addend;
1933 Result.append(fmtbuf.begin(), fmtbuf.end());
1941 case ELF::EM_HEXAGON:
1948 Result.append(res.begin(), res.end());
1949 return object_error::success;
1952 // Verify that the last byte in the string table in a null.
1953 template<class ELFT>
1954 void ELFObjectFile<ELFT>::VerifyStrTab(const Elf_Shdr *sh) const {
1955 const char *strtab = (const char*)base() + sh->sh_offset;
1956 if (strtab[sh->sh_size - 1] != 0)
1957 // FIXME: Proper error handling.
1958 report_fatal_error("String table must end with a null terminator!");
1961 template<class ELFT>
1962 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBuffer *Object, error_code &ec)
1963 : ObjectFile(getELFType(
1964 static_cast<endianness>(ELFT::TargetEndianness) == support::little,
1968 , isDyldELFObject(false)
1969 , SectionHeaderTable(0)
1970 , dot_shstrtab_sec(0)
1973 , dot_dynamic_sec(0)
1974 , dot_gnu_version_sec(0)
1975 , dot_gnu_version_r_sec(0)
1976 , dot_gnu_version_d_sec(0)
1980 const uint64_t FileSize = Data->getBufferSize();
1982 if (sizeof(Elf_Ehdr) > FileSize)
1983 // FIXME: Proper error handling.
1984 report_fatal_error("File too short!");
1986 Header = reinterpret_cast<const Elf_Ehdr *>(base());
1988 if (Header->e_shoff == 0)
1991 const uint64_t SectionTableOffset = Header->e_shoff;
1993 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize)
1994 // FIXME: Proper error handling.
1995 report_fatal_error("Section header table goes past end of file!");
1997 // The getNumSections() call below depends on SectionHeaderTable being set.
1998 SectionHeaderTable =
1999 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
2000 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
2002 if (SectionTableOffset + SectionTableSize > FileSize)
2003 // FIXME: Proper error handling.
2004 report_fatal_error("Section table goes past end of file!");
2006 // To find the symbol tables we walk the section table to find SHT_SYMTAB.
2007 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
2008 const Elf_Shdr* sh = SectionHeaderTable;
2010 // Reserve SymbolTableSections[0] for .dynsym
2011 SymbolTableSections.push_back(NULL);
2013 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
2014 switch (sh->sh_type) {
2015 case ELF::SHT_SYMTAB_SHNDX: {
2016 if (SymbolTableSectionHeaderIndex)
2017 // FIXME: Proper error handling.
2018 report_fatal_error("More than one .symtab_shndx!");
2019 SymbolTableSectionHeaderIndex = sh;
2022 case ELF::SHT_SYMTAB: {
2023 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
2024 SymbolTableSections.push_back(sh);
2027 case ELF::SHT_DYNSYM: {
2028 if (SymbolTableSections[0] != NULL)
2029 // FIXME: Proper error handling.
2030 report_fatal_error("More than one .dynsym!");
2031 SymbolTableSectionsIndexMap[i] = 0;
2032 SymbolTableSections[0] = sh;
2036 case ELF::SHT_RELA: {
2037 SectionRelocMap[getSection(sh->sh_info)].push_back(i);
2040 case ELF::SHT_DYNAMIC: {
2041 if (dot_dynamic_sec != NULL)
2042 // FIXME: Proper error handling.
2043 report_fatal_error("More than one .dynamic!");
2044 dot_dynamic_sec = sh;
2047 case ELF::SHT_GNU_versym: {
2048 if (dot_gnu_version_sec != NULL)
2049 // FIXME: Proper error handling.
2050 report_fatal_error("More than one .gnu.version section!");
2051 dot_gnu_version_sec = sh;
2054 case ELF::SHT_GNU_verdef: {
2055 if (dot_gnu_version_d_sec != NULL)
2056 // FIXME: Proper error handling.
2057 report_fatal_error("More than one .gnu.version_d section!");
2058 dot_gnu_version_d_sec = sh;
2061 case ELF::SHT_GNU_verneed: {
2062 if (dot_gnu_version_r_sec != NULL)
2063 // FIXME: Proper error handling.
2064 report_fatal_error("More than one .gnu.version_r section!");
2065 dot_gnu_version_r_sec = sh;
2072 // Sort section relocation lists by index.
2073 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
2074 e = SectionRelocMap.end(); i != e; ++i) {
2075 std::sort(i->second.begin(), i->second.end());
2078 // Get string table sections.
2079 dot_shstrtab_sec = getSection(getStringTableIndex());
2080 if (dot_shstrtab_sec) {
2081 // Verify that the last byte in the string table in a null.
2082 VerifyStrTab(dot_shstrtab_sec);
2085 // Merge this into the above loop.
2086 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
2087 *e = i + getNumSections() * Header->e_shentsize;
2088 i != e; i += Header->e_shentsize) {
2089 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
2090 if (sh->sh_type == ELF::SHT_STRTAB) {
2091 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
2092 if (SectionName == ".strtab") {
2093 if (dot_strtab_sec != 0)
2094 // FIXME: Proper error handling.
2095 report_fatal_error("Already found section named .strtab!");
2096 dot_strtab_sec = sh;
2097 VerifyStrTab(dot_strtab_sec);
2098 } else if (SectionName == ".dynstr") {
2099 if (dot_dynstr_sec != 0)
2100 // FIXME: Proper error handling.
2101 report_fatal_error("Already found section named .dynstr!");
2102 dot_dynstr_sec = sh;
2103 VerifyStrTab(dot_dynstr_sec);
2108 // Build symbol name side-mapping if there is one.
2109 if (SymbolTableSectionHeaderIndex) {
2110 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
2111 SymbolTableSectionHeaderIndex->sh_offset);
2113 for (symbol_iterator si = begin_symbols(),
2114 se = end_symbols(); si != se; si.increment(ec)) {
2116 report_fatal_error("Fewer extended symbol table entries than symbols!");
2117 if (*ShndxTable != ELF::SHN_UNDEF)
2118 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
2124 // Get the symbol table index in the symtab section given a symbol
2125 template<class ELFT>
2126 uint64_t ELFObjectFile<ELFT>::getSymbolIndex(const Elf_Sym *Sym) const {
2127 assert(SymbolTableSections.size() == 1 && "Only one symbol table supported!");
2128 const Elf_Shdr *SymTab = *SymbolTableSections.begin();
2129 uintptr_t SymLoc = uintptr_t(Sym);
2130 uintptr_t SymTabLoc = uintptr_t(base() + SymTab->sh_offset);
2131 assert(SymLoc > SymTabLoc && "Symbol not in symbol table!");
2132 uint64_t SymOffset = SymLoc - SymTabLoc;
2133 assert(SymOffset % SymTab->sh_entsize == 0 &&
2134 "Symbol not multiple of symbol size!");
2135 return SymOffset / SymTab->sh_entsize;
2138 template<class ELFT>
2139 symbol_iterator ELFObjectFile<ELFT>::begin_symbols() const {
2140 DataRefImpl SymbolData;
2141 if (SymbolTableSections.size() <= 1) {
2142 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2143 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2145 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2146 SymbolData.d.b = 1; // The 0th table is .dynsym
2148 return symbol_iterator(SymbolRef(SymbolData, this));
2151 template<class ELFT>
2152 symbol_iterator ELFObjectFile<ELFT>::end_symbols() const {
2153 DataRefImpl SymbolData;
2154 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2155 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2156 return symbol_iterator(SymbolRef(SymbolData, this));
2159 template<class ELFT>
2160 symbol_iterator ELFObjectFile<ELFT>::begin_dynamic_symbols() const {
2161 DataRefImpl SymbolData;
2162 if (SymbolTableSections[0] == NULL) {
2163 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2164 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2166 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2167 SymbolData.d.b = 0; // The 0th table is .dynsym
2169 return symbol_iterator(SymbolRef(SymbolData, this));
2172 template<class ELFT>
2173 symbol_iterator ELFObjectFile<ELFT>::end_dynamic_symbols() const {
2174 DataRefImpl SymbolData;
2175 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2176 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2177 return symbol_iterator(SymbolRef(SymbolData, this));
2180 template<class ELFT>
2181 section_iterator ELFObjectFile<ELFT>::begin_sections() const {
2183 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
2184 return section_iterator(SectionRef(ret, this));
2187 template<class ELFT>
2188 section_iterator ELFObjectFile<ELFT>::end_sections() const {
2190 ret.p = reinterpret_cast<intptr_t>(base()
2192 + (Header->e_shentsize*getNumSections()));
2193 return section_iterator(SectionRef(ret, this));
2196 template<class ELFT>
2197 typename ELFObjectFile<ELFT>::dyn_iterator
2198 ELFObjectFile<ELFT>::begin_dynamic_table() const {
2199 DataRefImpl DynData;
2200 if (dot_dynamic_sec == NULL || dot_dynamic_sec->sh_size == 0) {
2201 DynData.d.a = std::numeric_limits<uint32_t>::max();
2205 return dyn_iterator(DynRef(DynData, this));
2208 template<class ELFT>
2209 typename ELFObjectFile<ELFT>::dyn_iterator
2210 ELFObjectFile<ELFT>::end_dynamic_table() const {
2211 DataRefImpl DynData;
2212 DynData.d.a = std::numeric_limits<uint32_t>::max();
2213 return dyn_iterator(DynRef(DynData, this));
2216 template<class ELFT>
2217 error_code ELFObjectFile<ELFT>::getDynNext(DataRefImpl DynData,
2218 DynRef &Result) const {
2221 // Check to see if we are at the end of .dynamic
2222 if (DynData.d.a >= dot_dynamic_sec->getEntityCount()) {
2223 // We are at the end. Return the terminator.
2224 DynData.d.a = std::numeric_limits<uint32_t>::max();
2227 Result = DynRef(DynData, this);
2228 return object_error::success;
2231 template<class ELFT>
2233 ELFObjectFile<ELFT>::getLoadName() const {
2235 // Find the DT_SONAME entry
2236 dyn_iterator it = begin_dynamic_table();
2237 dyn_iterator ie = end_dynamic_table();
2240 if (it->getTag() == ELF::DT_SONAME)
2244 report_fatal_error("dynamic table iteration failed");
2247 if (dot_dynstr_sec == NULL)
2248 report_fatal_error("Dynamic string table is missing");
2249 dt_soname = getString(dot_dynstr_sec, it->getVal());
2257 template<class ELFT>
2258 library_iterator ELFObjectFile<ELFT>::begin_libraries_needed() const {
2259 // Find the first DT_NEEDED entry
2260 dyn_iterator i = begin_dynamic_table();
2261 dyn_iterator e = end_dynamic_table();
2264 if (i->getTag() == ELF::DT_NEEDED)
2268 report_fatal_error("dynamic table iteration failed");
2270 // Use the same DataRefImpl format as DynRef.
2271 return library_iterator(LibraryRef(i->getRawDataRefImpl(), this));
2274 template<class ELFT>
2275 error_code ELFObjectFile<ELFT>::getLibraryNext(DataRefImpl Data,
2276 LibraryRef &Result) const {
2277 // Use the same DataRefImpl format as DynRef.
2278 dyn_iterator i = dyn_iterator(DynRef(Data, this));
2279 dyn_iterator e = end_dynamic_table();
2281 // Skip the current dynamic table entry.
2285 // TODO: proper error handling
2287 report_fatal_error("dynamic table iteration failed");
2290 // Find the next DT_NEEDED entry.
2292 if (i->getTag() == ELF::DT_NEEDED)
2296 report_fatal_error("dynamic table iteration failed");
2298 Result = LibraryRef(i->getRawDataRefImpl(), this);
2299 return object_error::success;
2302 template<class ELFT>
2303 error_code ELFObjectFile<ELFT>::getLibraryPath(DataRefImpl Data,
2304 StringRef &Res) const {
2305 dyn_iterator i = dyn_iterator(DynRef(Data, this));
2306 if (i == end_dynamic_table())
2307 report_fatal_error("getLibraryPath() called on iterator end");
2309 if (i->getTag() != ELF::DT_NEEDED)
2310 report_fatal_error("Invalid library_iterator");
2312 // This uses .dynstr to lookup the name of the DT_NEEDED entry.
2313 // THis works as long as DT_STRTAB == .dynstr. This is true most of
2314 // the time, but the specification allows exceptions.
2315 // TODO: This should really use DT_STRTAB instead. Doing this requires
2316 // reading the program headers.
2317 if (dot_dynstr_sec == NULL)
2318 report_fatal_error("Dynamic string table is missing");
2319 Res = getString(dot_dynstr_sec, i->getVal());
2320 return object_error::success;
2323 template<class ELFT>
2324 library_iterator ELFObjectFile<ELFT>::end_libraries_needed() const {
2325 dyn_iterator e = end_dynamic_table();
2326 // Use the same DataRefImpl format as DynRef.
2327 return library_iterator(LibraryRef(e->getRawDataRefImpl(), this));
2330 template<class ELFT>
2331 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
2332 return ELFT::Is64Bits ? 8 : 4;
2335 template<class ELFT>
2336 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
2337 switch(Header->e_ident[ELF::EI_CLASS]) {
2338 case ELF::ELFCLASS32:
2339 switch(Header->e_machine) {
2341 return "ELF32-i386";
2342 case ELF::EM_X86_64:
2343 return "ELF32-x86-64";
2346 case ELF::EM_HEXAGON:
2347 return "ELF32-hexagon";
2349 return "ELF32-mips";
2351 return "ELF32-unknown";
2353 case ELF::ELFCLASS64:
2354 switch(Header->e_machine) {
2356 return "ELF64-i386";
2357 case ELF::EM_X86_64:
2358 return "ELF64-x86-64";
2360 return "ELF64-ppc64";
2362 return "ELF64-unknown";
2365 // FIXME: Proper error handling.
2366 report_fatal_error("Invalid ELFCLASS!");
2370 template<class ELFT>
2371 unsigned ELFObjectFile<ELFT>::getArch() const {
2372 switch(Header->e_machine) {
2375 case ELF::EM_X86_64:
2376 return Triple::x86_64;
2379 case ELF::EM_HEXAGON:
2380 return Triple::hexagon;
2382 return (ELFT::TargetEndianness == support::little) ?
2383 Triple::mipsel : Triple::mips;
2385 return Triple::ppc64;
2387 return Triple::UnknownArch;
2391 template<class ELFT>
2392 uint64_t ELFObjectFile<ELFT>::getNumSections() const {
2393 assert(Header && "Header not initialized!");
2394 if (Header->e_shnum == ELF::SHN_UNDEF) {
2395 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
2396 return SectionHeaderTable->sh_size;
2398 return Header->e_shnum;
2401 template<class ELFT>
2403 ELFObjectFile<ELFT>::getStringTableIndex() const {
2404 if (Header->e_shnum == ELF::SHN_UNDEF) {
2405 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
2406 return SectionHeaderTable->sh_link;
2407 if (Header->e_shstrndx >= getNumSections())
2410 return Header->e_shstrndx;
2413 template<class ELFT>
2414 template<typename T>
2416 ELFObjectFile<ELFT>::getEntry(uint16_t Section, uint32_t Entry) const {
2417 return getEntry<T>(getSection(Section), Entry);
2420 template<class ELFT>
2421 template<typename T>
2423 ELFObjectFile<ELFT>::getEntry(const Elf_Shdr * Section, uint32_t Entry) const {
2424 return reinterpret_cast<const T *>(
2426 + Section->sh_offset
2427 + (Entry * Section->sh_entsize));
2430 template<class ELFT>
2431 const typename ELFObjectFile<ELFT>::Elf_Sym *
2432 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
2433 return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
2436 template<class ELFT>
2437 const typename ELFObjectFile<ELFT>::Elf_Dyn *
2438 ELFObjectFile<ELFT>::getDyn(DataRefImpl DynData) const {
2439 return getEntry<Elf_Dyn>(dot_dynamic_sec, DynData.d.a);
2442 template<class ELFT>
2443 const typename ELFObjectFile<ELFT>::Elf_Rel *
2444 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
2445 return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
2448 template<class ELFT>
2449 const typename ELFObjectFile<ELFT>::Elf_Rela *
2450 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
2451 return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
2454 template<class ELFT>
2455 const typename ELFObjectFile<ELFT>::Elf_Shdr *
2456 ELFObjectFile<ELFT>::getSection(DataRefImpl Symb) const {
2457 const Elf_Shdr *sec = getSection(Symb.d.b);
2458 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
2459 // FIXME: Proper error handling.
2460 report_fatal_error("Invalid symbol table section!");
2464 template<class ELFT>
2465 const typename ELFObjectFile<ELFT>::Elf_Shdr *
2466 ELFObjectFile<ELFT>::getSection(uint32_t index) const {
2469 if (!SectionHeaderTable || index >= getNumSections())
2470 // FIXME: Proper error handling.
2471 report_fatal_error("Invalid section index!");
2473 return reinterpret_cast<const Elf_Shdr *>(
2474 reinterpret_cast<const char *>(SectionHeaderTable)
2475 + (index * Header->e_shentsize));
2478 template<class ELFT>
2479 const char *ELFObjectFile<ELFT>::getString(uint32_t section,
2480 ELF::Elf32_Word offset) const {
2481 return getString(getSection(section), offset);
2484 template<class ELFT>
2485 const char *ELFObjectFile<ELFT>::getString(const Elf_Shdr *section,
2486 ELF::Elf32_Word offset) const {
2487 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
2488 if (offset >= section->sh_size)
2489 // FIXME: Proper error handling.
2490 report_fatal_error("Symbol name offset outside of string table!");
2491 return (const char *)base() + section->sh_offset + offset;
2494 template<class ELFT>
2495 error_code ELFObjectFile<ELFT>::getSymbolName(const Elf_Shdr *section,
2496 const Elf_Sym *symb,
2497 StringRef &Result) const {
2498 if (symb->st_name == 0) {
2499 const Elf_Shdr *section = getSection(symb);
2503 Result = getString(dot_shstrtab_sec, section->sh_name);
2504 return object_error::success;
2507 if (section == SymbolTableSections[0]) {
2508 // Symbol is in .dynsym, use .dynstr string table
2509 Result = getString(dot_dynstr_sec, symb->st_name);
2511 // Use the default symbol table name section.
2512 Result = getString(dot_strtab_sec, symb->st_name);
2514 return object_error::success;
2517 template<class ELFT>
2518 error_code ELFObjectFile<ELFT>::getSectionName(const Elf_Shdr *section,
2519 StringRef &Result) const {
2520 Result = StringRef(getString(dot_shstrtab_sec, section->sh_name));
2521 return object_error::success;
2524 template<class ELFT>
2525 error_code ELFObjectFile<ELFT>::getSymbolVersion(const Elf_Shdr *section,
2526 const Elf_Sym *symb,
2528 bool &IsDefault) const {
2529 // Handle non-dynamic symbols.
2530 if (section != SymbolTableSections[0]) {
2531 // Non-dynamic symbols can have versions in their names
2532 // A name of the form 'foo@V1' indicates version 'V1', non-default.
2533 // A name of the form 'foo@@V2' indicates version 'V2', default version.
2535 error_code ec = getSymbolName(section, symb, Name);
2536 if (ec != object_error::success)
2538 size_t atpos = Name.find('@');
2539 if (atpos == StringRef::npos) {
2542 return object_error::success;
2545 if (atpos < Name.size() && Name[atpos] == '@') {
2551 Version = Name.substr(atpos);
2552 return object_error::success;
2555 // This is a dynamic symbol. Look in the GNU symbol version table.
2556 if (dot_gnu_version_sec == NULL) {
2557 // No version table.
2560 return object_error::success;
2563 // Determine the position in the symbol table of this entry.
2564 const char *sec_start = (const char*)base() + section->sh_offset;
2565 size_t entry_index = ((const char*)symb - sec_start)/section->sh_entsize;
2567 // Get the corresponding version index entry
2568 const Elf_Versym *vs = getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index);
2569 size_t version_index = vs->vs_index & ELF::VERSYM_VERSION;
2571 // Special markers for unversioned symbols.
2572 if (version_index == ELF::VER_NDX_LOCAL ||
2573 version_index == ELF::VER_NDX_GLOBAL) {
2576 return object_error::success;
2579 // Lookup this symbol in the version table
2581 if (version_index >= VersionMap.size() || VersionMap[version_index].isNull())
2582 report_fatal_error("Symbol has version index without corresponding "
2583 "define or reference entry");
2584 const VersionMapEntry &entry = VersionMap[version_index];
2586 // Get the version name string
2588 if (entry.isVerdef()) {
2589 // The first Verdaux entry holds the name.
2590 name_offset = entry.getVerdef()->getAux()->vda_name;
2592 name_offset = entry.getVernaux()->vna_name;
2594 Version = getString(dot_dynstr_sec, name_offset);
2597 if (entry.isVerdef()) {
2598 IsDefault = !(vs->vs_index & ELF::VERSYM_HIDDEN);
2603 return object_error::success;
2606 template<class ELFT>
2607 inline DynRefImpl<ELFT>::DynRefImpl(DataRefImpl DynP, const OwningType *Owner)
2609 , OwningObject(Owner) {}
2611 template<class ELFT>
2612 inline bool DynRefImpl<ELFT>::operator==(const DynRefImpl &Other) const {
2613 return DynPimpl == Other.DynPimpl;
2616 template<class ELFT>
2617 inline bool DynRefImpl<ELFT>::operator <(const DynRefImpl &Other) const {
2618 return DynPimpl < Other.DynPimpl;
2621 template<class ELFT>
2622 inline error_code DynRefImpl<ELFT>::getNext(DynRefImpl &Result) const {
2623 return OwningObject->getDynNext(DynPimpl, Result);
2626 template<class ELFT>
2627 inline int64_t DynRefImpl<ELFT>::getTag() const {
2628 return OwningObject->getDyn(DynPimpl)->d_tag;
2631 template<class ELFT>
2632 inline uint64_t DynRefImpl<ELFT>::getVal() const {
2633 return OwningObject->getDyn(DynPimpl)->d_un.d_val;
2636 template<class ELFT>
2637 inline uint64_t DynRefImpl<ELFT>::getPtr() const {
2638 return OwningObject->getDyn(DynPimpl)->d_un.d_ptr;
2641 template<class ELFT>
2642 inline DataRefImpl DynRefImpl<ELFT>::getRawDataRefImpl() const {
2646 /// This is a generic interface for retrieving GNU symbol version
2647 /// information from an ELFObjectFile.
2648 static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
2649 const SymbolRef &Sym,
2652 // Little-endian 32-bit
2653 if (const ELFObjectFile<ELFType<support::little, 4, false> > *ELFObj =
2654 dyn_cast<ELFObjectFile<ELFType<support::little, 4, false> > >(Obj))
2655 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2657 // Big-endian 32-bit
2658 if (const ELFObjectFile<ELFType<support::big, 4, false> > *ELFObj =
2659 dyn_cast<ELFObjectFile<ELFType<support::big, 4, false> > >(Obj))
2660 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2662 // Little-endian 64-bit
2663 if (const ELFObjectFile<ELFType<support::little, 8, true> > *ELFObj =
2664 dyn_cast<ELFObjectFile<ELFType<support::little, 8, true> > >(Obj))
2665 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2667 // Big-endian 64-bit
2668 if (const ELFObjectFile<ELFType<support::big, 8, true> > *ELFObj =
2669 dyn_cast<ELFObjectFile<ELFType<support::big, 8, true> > >(Obj))
2670 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2672 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");