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 return getSectionContents(sec, Result);
1266 template<class ELFT>
1267 error_code ELFObjectFile<ELFT>::getSectionContents(const Elf_Shdr *Sec,
1268 StringRef &Result) const {
1269 if (Sec->sh_type == ELF::SHT_NOBITS)
1270 Result = StringRef();
1272 const char *start = (const char*)base() + Sec->sh_offset;
1273 Result = StringRef(start, Sec->sh_size);
1275 return object_error::success;
1278 template<class ELFT>
1279 error_code ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec,
1280 uint64_t &Result) const {
1281 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1282 Result = sec->sh_addralign;
1283 return object_error::success;
1286 template<class ELFT>
1287 error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec,
1288 bool &Result) const {
1289 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1290 if (sec->sh_flags & ELF::SHF_EXECINSTR)
1294 return object_error::success;
1297 template<class ELFT>
1298 error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec,
1299 bool &Result) const {
1300 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1301 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1302 && sec->sh_type == ELF::SHT_PROGBITS)
1306 return object_error::success;
1309 template<class ELFT>
1310 error_code ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec,
1311 bool &Result) const {
1312 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1313 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1314 && sec->sh_type == ELF::SHT_NOBITS)
1318 return object_error::success;
1321 template<class ELFT>
1322 error_code ELFObjectFile<ELFT>::isSectionRequiredForExecution(
1323 DataRefImpl Sec, bool &Result) const {
1324 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1325 if (sec->sh_flags & ELF::SHF_ALLOC)
1329 return object_error::success;
1332 template<class ELFT>
1333 error_code ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec,
1334 bool &Result) const {
1335 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1336 if (sec->sh_type == ELF::SHT_NOBITS)
1340 return object_error::success;
1343 template<class ELFT>
1344 error_code ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec,
1345 bool &Result) const {
1346 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1347 // For ELF, all zero-init sections are virtual (that is, they occupy no space
1348 // in the object image) and vice versa.
1349 Result = sec->sh_type == ELF::SHT_NOBITS;
1350 return object_error::success;
1353 template<class ELFT>
1354 error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec,
1355 bool &Result) const {
1356 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1357 if (sec->sh_flags & ELF::SHF_WRITE || sec->sh_flags & ELF::SHF_EXECINSTR)
1361 return object_error::success;
1364 template<class ELFT>
1365 error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
1367 bool &Result) const {
1368 // FIXME: Unimplemented.
1370 return object_error::success;
1373 template<class ELFT>
1375 ELFObjectFile<ELFT>::getSectionRelBegin(DataRefImpl Sec) const {
1376 DataRefImpl RelData;
1377 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1378 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1379 if (sec != 0 && ittr != SectionRelocMap.end()) {
1380 RelData.w.a = getSection(ittr->second[0])->sh_info;
1381 RelData.w.b = ittr->second[0];
1384 return relocation_iterator(RelocationRef(RelData, this));
1387 template<class ELFT>
1389 ELFObjectFile<ELFT>::getSectionRelEnd(DataRefImpl Sec) const {
1390 DataRefImpl RelData;
1391 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1392 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1393 if (sec != 0 && ittr != SectionRelocMap.end()) {
1394 // Get the index of the last relocation section for this section.
1395 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
1396 const Elf_Shdr *relocsec = getSection(relocsecindex);
1397 RelData.w.a = relocsec->sh_info;
1398 RelData.w.b = relocsecindex;
1399 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
1401 return relocation_iterator(RelocationRef(RelData, this));
1405 template<class ELFT>
1406 error_code ELFObjectFile<ELFT>::getRelocationNext(DataRefImpl Rel,
1407 RelocationRef &Result) const {
1409 const Elf_Shdr *relocsec = getSection(Rel.w.b);
1410 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
1411 // We have reached the end of the relocations for this section. See if there
1412 // is another relocation section.
1413 typename RelocMap_t::mapped_type relocseclist =
1414 SectionRelocMap.lookup(getSection(Rel.w.a));
1416 // Do a binary search for the current reloc section index (which must be
1417 // present). Then get the next one.
1418 typename RelocMap_t::mapped_type::const_iterator loc =
1419 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
1422 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
1423 // to the end iterator.
1424 if (loc != relocseclist.end()) {
1429 Result = RelocationRef(Rel, this);
1430 return object_error::success;
1433 template<class ELFT>
1434 error_code ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel,
1435 SymbolRef &Result) const {
1437 const Elf_Shdr *sec = getSection(Rel.w.b);
1438 switch (sec->sh_type) {
1440 report_fatal_error("Invalid section type in Rel!");
1441 case ELF::SHT_REL : {
1442 symbolIdx = getRel(Rel)->getSymbol();
1445 case ELF::SHT_RELA : {
1446 symbolIdx = getRela(Rel)->getSymbol();
1450 DataRefImpl SymbolData;
1451 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
1452 if (it == SymbolTableSectionsIndexMap.end())
1453 report_fatal_error("Relocation symbol table not found!");
1454 SymbolData.d.a = symbolIdx;
1455 SymbolData.d.b = it->second;
1456 Result = SymbolRef(SymbolData, this);
1457 return object_error::success;
1460 template<class ELFT>
1461 error_code ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
1462 uint64_t &Result) const {
1464 const Elf_Shdr *sec = getSection(Rel.w.b);
1465 switch (sec->sh_type) {
1467 report_fatal_error("Invalid section type in Rel!");
1468 case ELF::SHT_REL : {
1469 offset = getRel(Rel)->r_offset;
1472 case ELF::SHT_RELA : {
1473 offset = getRela(Rel)->r_offset;
1479 return object_error::success;
1482 template<class ELFT>
1483 error_code ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
1484 uint64_t &Result) const {
1486 const Elf_Shdr *sec = getSection(Rel.w.b);
1487 switch (sec->sh_type) {
1489 report_fatal_error("Invalid section type in Rel!");
1490 case ELF::SHT_REL : {
1491 offset = getRel(Rel)->r_offset;
1494 case ELF::SHT_RELA : {
1495 offset = getRela(Rel)->r_offset;
1500 Result = offset - sec->sh_addr;
1501 return object_error::success;
1504 template<class ELFT>
1505 error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
1506 uint64_t &Result) const {
1507 const Elf_Shdr *sec = getSection(Rel.w.b);
1508 switch (sec->sh_type) {
1510 report_fatal_error("Invalid section type in Rel!");
1511 case ELF::SHT_REL : {
1512 Result = getRel(Rel)->getType();
1515 case ELF::SHT_RELA : {
1516 Result = getRela(Rel)->getType();
1520 return object_error::success;
1523 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
1524 case ELF::enum: res = #enum; break;
1526 template<class ELFT>
1527 error_code ELFObjectFile<ELFT>::getRelocationTypeName(
1528 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1529 const Elf_Shdr *sec = getSection(Rel.w.b);
1532 switch (sec->sh_type) {
1534 return object_error::parse_failed;
1535 case ELF::SHT_REL : {
1536 type = getRel(Rel)->getType();
1539 case ELF::SHT_RELA : {
1540 type = getRela(Rel)->getType();
1544 switch (Header->e_machine) {
1545 case ELF::EM_X86_64:
1547 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
1548 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
1549 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
1550 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
1551 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
1552 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
1553 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
1554 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
1555 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
1556 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
1557 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
1558 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
1559 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
1560 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
1561 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
1562 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
1563 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
1564 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
1565 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
1566 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
1567 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
1568 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
1569 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
1570 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
1571 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
1572 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
1573 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
1574 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
1575 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
1576 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
1577 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
1578 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
1585 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
1586 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
1587 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
1588 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
1589 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
1590 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
1591 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
1592 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
1593 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
1594 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
1595 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
1596 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
1597 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
1598 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
1599 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
1600 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
1601 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
1602 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
1603 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
1604 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
1605 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
1606 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
1607 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
1608 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
1609 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
1610 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
1611 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
1612 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
1613 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
1614 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
1615 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
1616 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
1617 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
1618 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
1619 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
1620 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
1621 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
1622 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
1623 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
1624 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
1631 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_NONE);
1632 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PC24);
1633 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32);
1634 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32);
1635 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G0);
1636 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS16);
1637 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS12);
1638 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ABS5);
1639 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS8);
1640 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL32);
1641 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_CALL);
1642 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC8);
1643 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BREL_ADJ);
1644 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESC);
1645 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_SWI8);
1646 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_XPC25);
1647 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_XPC22);
1648 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPMOD32);
1649 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPOFF32);
1650 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_TPOFF32);
1651 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_COPY);
1652 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GLOB_DAT);
1653 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP_SLOT);
1654 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_RELATIVE);
1655 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF32);
1656 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_PREL);
1657 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL);
1658 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32);
1659 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_CALL);
1660 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP24);
1661 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP24);
1662 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_ABS);
1663 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_7_0);
1664 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_15_8);
1665 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_23_15);
1666 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SBREL_11_0_NC);
1667 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_19_12_NC);
1668 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_27_20_CK);
1669 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET1);
1670 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL31);
1671 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_V4BX);
1672 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET2);
1673 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PREL31);
1674 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_ABS_NC);
1675 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_ABS);
1676 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_PREL_NC);
1677 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_PREL);
1678 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_ABS_NC);
1679 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_ABS);
1680 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_PREL_NC);
1681 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_PREL);
1682 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP19);
1683 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP6);
1684 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ALU_PREL_11_0);
1685 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC12);
1686 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32_NOI);
1687 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32_NOI);
1688 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0_NC);
1689 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0);
1690 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1_NC);
1691 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1);
1692 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G2);
1693 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G1);
1694 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G2);
1695 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G0);
1696 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G1);
1697 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G2);
1698 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G0);
1699 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G1);
1700 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G2);
1701 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0_NC);
1702 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0);
1703 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1_NC);
1704 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1);
1705 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G2);
1706 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G0);
1707 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G1);
1708 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G2);
1709 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G0);
1710 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G1);
1711 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G2);
1712 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G0);
1713 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G1);
1714 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G2);
1715 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL_NC);
1716 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_BREL);
1717 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL);
1718 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL_NC);
1719 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_BREL);
1720 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL);
1721 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GOTDESC);
1722 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_CALL);
1723 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESCSEQ);
1724 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_CALL);
1725 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32_ABS);
1726 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_ABS);
1727 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_PREL);
1728 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL12);
1729 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF12);
1730 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTRELAX);
1731 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTENTRY);
1732 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTINHERIT);
1733 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP11);
1734 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP8);
1735 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GD32);
1736 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDM32);
1737 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO32);
1738 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE32);
1739 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE32);
1740 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO12);
1741 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE12);
1742 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE12GP);
1743 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_0);
1744 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_1);
1745 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_2);
1746 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_3);
1747 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_4);
1748 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_5);
1749 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_6);
1750 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_7);
1751 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_8);
1752 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_9);
1753 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_10);
1754 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_11);
1755 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_12);
1756 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_13);
1757 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_14);
1758 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_15);
1759 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ME_TOO);
1760 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ16);
1761 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ32);
1766 case ELF::EM_HEXAGON:
1768 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_NONE);
1769 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL);
1770 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL);
1771 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL);
1772 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_LO16);
1773 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HI16);
1774 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32);
1775 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16);
1776 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8);
1777 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_0);
1778 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_1);
1779 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_2);
1780 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_3);
1781 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HL16);
1782 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL);
1783 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL);
1784 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B32_PCREL_X);
1785 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_6_X);
1786 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL_X);
1787 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL_X);
1788 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL_X);
1789 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL_X);
1790 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL_X);
1791 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16_X);
1792 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_12_X);
1793 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_11_X);
1794 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_10_X);
1795 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_9_X);
1796 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8_X);
1797 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_7_X);
1798 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_X);
1799 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_PCREL);
1800 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_COPY);
1801 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GLOB_DAT);
1802 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_JMP_SLOT);
1803 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_RELATIVE);
1804 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_PLT_B22_PCREL);
1805 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_LO16);
1806 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_HI16);
1807 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32);
1808 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_LO16);
1809 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_HI16);
1810 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32);
1811 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16);
1812 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPMOD_32);
1813 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_LO16);
1814 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_HI16);
1815 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32);
1816 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16);
1817 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_PLT_B22_PCREL);
1818 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_LO16);
1819 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_HI16);
1820 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32);
1821 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16);
1822 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_LO16);
1823 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_HI16);
1824 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32);
1825 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_LO16);
1826 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_HI16);
1827 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32);
1828 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16);
1829 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_LO16);
1830 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_HI16);
1831 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32);
1832 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16);
1833 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_PCREL_X);
1834 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32_6_X);
1835 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_16_X);
1836 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_11_X);
1837 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32_6_X);
1838 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16_X);
1839 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_11_X);
1840 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32_6_X);
1841 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16_X);
1842 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_11_X);
1843 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32_6_X);
1844 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16_X);
1845 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_11_X);
1846 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32_6_X);
1847 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_16_X);
1848 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32_6_X);
1849 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16_X);
1850 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_11_X);
1851 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32_6_X);
1852 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16_X);
1853 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_11_X);
1861 Result.append(res.begin(), res.end());
1862 return object_error::success;
1865 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
1867 template<class ELFT>
1868 error_code ELFObjectFile<ELFT>::getRelocationAdditionalInfo(
1869 DataRefImpl Rel, int64_t &Result) const {
1870 const Elf_Shdr *sec = getSection(Rel.w.b);
1871 switch (sec->sh_type) {
1873 report_fatal_error("Invalid section type in Rel!");
1874 case ELF::SHT_REL : {
1876 return object_error::success;
1878 case ELF::SHT_RELA : {
1879 Result = getRela(Rel)->r_addend;
1880 return object_error::success;
1885 template<class ELFT>
1886 error_code ELFObjectFile<ELFT>::getRelocationValueString(
1887 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1888 const Elf_Shdr *sec = getSection(Rel.w.b);
1892 uint16_t symbol_index = 0;
1893 switch (sec->sh_type) {
1895 return object_error::parse_failed;
1896 case ELF::SHT_REL: {
1897 type = getRel(Rel)->getType();
1898 symbol_index = getRel(Rel)->getSymbol();
1899 // TODO: Read implicit addend from section data.
1902 case ELF::SHT_RELA: {
1903 type = getRela(Rel)->getType();
1904 symbol_index = getRela(Rel)->getSymbol();
1905 addend = getRela(Rel)->r_addend;
1909 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1911 if (error_code ec = getSymbolName(getSection(sec->sh_link), symb, symname))
1913 switch (Header->e_machine) {
1914 case ELF::EM_X86_64:
1916 case ELF::R_X86_64_PC8:
1917 case ELF::R_X86_64_PC16:
1918 case ELF::R_X86_64_PC32: {
1920 raw_string_ostream fmt(fmtbuf);
1921 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1923 Result.append(fmtbuf.begin(), fmtbuf.end());
1926 case ELF::R_X86_64_8:
1927 case ELF::R_X86_64_16:
1928 case ELF::R_X86_64_32:
1929 case ELF::R_X86_64_32S:
1930 case ELF::R_X86_64_64: {
1932 raw_string_ostream fmt(fmtbuf);
1933 fmt << symname << (addend < 0 ? "" : "+") << addend;
1935 Result.append(fmtbuf.begin(), fmtbuf.end());
1943 case ELF::EM_HEXAGON:
1950 Result.append(res.begin(), res.end());
1951 return object_error::success;
1954 // Verify that the last byte in the string table in a null.
1955 template<class ELFT>
1956 void ELFObjectFile<ELFT>::VerifyStrTab(const Elf_Shdr *sh) const {
1957 const char *strtab = (const char*)base() + sh->sh_offset;
1958 if (strtab[sh->sh_size - 1] != 0)
1959 // FIXME: Proper error handling.
1960 report_fatal_error("String table must end with a null terminator!");
1963 template<class ELFT>
1964 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBuffer *Object, error_code &ec)
1965 : ObjectFile(getELFType(
1966 static_cast<endianness>(ELFT::TargetEndianness) == support::little,
1970 , isDyldELFObject(false)
1971 , SectionHeaderTable(0)
1972 , dot_shstrtab_sec(0)
1975 , dot_dynamic_sec(0)
1976 , dot_gnu_version_sec(0)
1977 , dot_gnu_version_r_sec(0)
1978 , dot_gnu_version_d_sec(0)
1982 const uint64_t FileSize = Data->getBufferSize();
1984 if (sizeof(Elf_Ehdr) > FileSize)
1985 // FIXME: Proper error handling.
1986 report_fatal_error("File too short!");
1988 Header = reinterpret_cast<const Elf_Ehdr *>(base());
1990 if (Header->e_shoff == 0)
1993 const uint64_t SectionTableOffset = Header->e_shoff;
1995 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize)
1996 // FIXME: Proper error handling.
1997 report_fatal_error("Section header table goes past end of file!");
1999 // The getNumSections() call below depends on SectionHeaderTable being set.
2000 SectionHeaderTable =
2001 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
2002 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
2004 if (SectionTableOffset + SectionTableSize > FileSize)
2005 // FIXME: Proper error handling.
2006 report_fatal_error("Section table goes past end of file!");
2008 // To find the symbol tables we walk the section table to find SHT_SYMTAB.
2009 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
2010 const Elf_Shdr* sh = SectionHeaderTable;
2012 // Reserve SymbolTableSections[0] for .dynsym
2013 SymbolTableSections.push_back(NULL);
2015 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
2016 switch (sh->sh_type) {
2017 case ELF::SHT_SYMTAB_SHNDX: {
2018 if (SymbolTableSectionHeaderIndex)
2019 // FIXME: Proper error handling.
2020 report_fatal_error("More than one .symtab_shndx!");
2021 SymbolTableSectionHeaderIndex = sh;
2024 case ELF::SHT_SYMTAB: {
2025 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
2026 SymbolTableSections.push_back(sh);
2029 case ELF::SHT_DYNSYM: {
2030 if (SymbolTableSections[0] != NULL)
2031 // FIXME: Proper error handling.
2032 report_fatal_error("More than one .dynsym!");
2033 SymbolTableSectionsIndexMap[i] = 0;
2034 SymbolTableSections[0] = sh;
2038 case ELF::SHT_RELA: {
2039 SectionRelocMap[getSection(sh->sh_info)].push_back(i);
2042 case ELF::SHT_DYNAMIC: {
2043 if (dot_dynamic_sec != NULL)
2044 // FIXME: Proper error handling.
2045 report_fatal_error("More than one .dynamic!");
2046 dot_dynamic_sec = sh;
2049 case ELF::SHT_GNU_versym: {
2050 if (dot_gnu_version_sec != NULL)
2051 // FIXME: Proper error handling.
2052 report_fatal_error("More than one .gnu.version section!");
2053 dot_gnu_version_sec = sh;
2056 case ELF::SHT_GNU_verdef: {
2057 if (dot_gnu_version_d_sec != NULL)
2058 // FIXME: Proper error handling.
2059 report_fatal_error("More than one .gnu.version_d section!");
2060 dot_gnu_version_d_sec = sh;
2063 case ELF::SHT_GNU_verneed: {
2064 if (dot_gnu_version_r_sec != NULL)
2065 // FIXME: Proper error handling.
2066 report_fatal_error("More than one .gnu.version_r section!");
2067 dot_gnu_version_r_sec = sh;
2074 // Sort section relocation lists by index.
2075 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
2076 e = SectionRelocMap.end(); i != e; ++i) {
2077 std::sort(i->second.begin(), i->second.end());
2080 // Get string table sections.
2081 dot_shstrtab_sec = getSection(getStringTableIndex());
2082 if (dot_shstrtab_sec) {
2083 // Verify that the last byte in the string table in a null.
2084 VerifyStrTab(dot_shstrtab_sec);
2087 // Merge this into the above loop.
2088 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
2089 *e = i + getNumSections() * Header->e_shentsize;
2090 i != e; i += Header->e_shentsize) {
2091 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
2092 if (sh->sh_type == ELF::SHT_STRTAB) {
2093 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
2094 if (SectionName == ".strtab") {
2095 if (dot_strtab_sec != 0)
2096 // FIXME: Proper error handling.
2097 report_fatal_error("Already found section named .strtab!");
2098 dot_strtab_sec = sh;
2099 VerifyStrTab(dot_strtab_sec);
2100 } else if (SectionName == ".dynstr") {
2101 if (dot_dynstr_sec != 0)
2102 // FIXME: Proper error handling.
2103 report_fatal_error("Already found section named .dynstr!");
2104 dot_dynstr_sec = sh;
2105 VerifyStrTab(dot_dynstr_sec);
2110 // Build symbol name side-mapping if there is one.
2111 if (SymbolTableSectionHeaderIndex) {
2112 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
2113 SymbolTableSectionHeaderIndex->sh_offset);
2115 for (symbol_iterator si = begin_symbols(),
2116 se = end_symbols(); si != se; si.increment(ec)) {
2118 report_fatal_error("Fewer extended symbol table entries than symbols!");
2119 if (*ShndxTable != ELF::SHN_UNDEF)
2120 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
2126 // Get the symbol table index in the symtab section given a symbol
2127 template<class ELFT>
2128 uint64_t ELFObjectFile<ELFT>::getSymbolIndex(const Elf_Sym *Sym) const {
2129 assert(SymbolTableSections.size() == 1 && "Only one symbol table supported!");
2130 const Elf_Shdr *SymTab = *SymbolTableSections.begin();
2131 uintptr_t SymLoc = uintptr_t(Sym);
2132 uintptr_t SymTabLoc = uintptr_t(base() + SymTab->sh_offset);
2133 assert(SymLoc > SymTabLoc && "Symbol not in symbol table!");
2134 uint64_t SymOffset = SymLoc - SymTabLoc;
2135 assert(SymOffset % SymTab->sh_entsize == 0 &&
2136 "Symbol not multiple of symbol size!");
2137 return SymOffset / SymTab->sh_entsize;
2140 template<class ELFT>
2141 symbol_iterator ELFObjectFile<ELFT>::begin_symbols() const {
2142 DataRefImpl SymbolData;
2143 if (SymbolTableSections.size() <= 1) {
2144 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2145 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2147 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2148 SymbolData.d.b = 1; // The 0th table is .dynsym
2150 return symbol_iterator(SymbolRef(SymbolData, this));
2153 template<class ELFT>
2154 symbol_iterator ELFObjectFile<ELFT>::end_symbols() const {
2155 DataRefImpl SymbolData;
2156 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2157 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2158 return symbol_iterator(SymbolRef(SymbolData, this));
2161 template<class ELFT>
2162 symbol_iterator ELFObjectFile<ELFT>::begin_dynamic_symbols() const {
2163 DataRefImpl SymbolData;
2164 if (SymbolTableSections[0] == NULL) {
2165 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2166 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2168 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2169 SymbolData.d.b = 0; // The 0th table is .dynsym
2171 return symbol_iterator(SymbolRef(SymbolData, this));
2174 template<class ELFT>
2175 symbol_iterator ELFObjectFile<ELFT>::end_dynamic_symbols() const {
2176 DataRefImpl SymbolData;
2177 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2178 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2179 return symbol_iterator(SymbolRef(SymbolData, this));
2182 template<class ELFT>
2183 section_iterator ELFObjectFile<ELFT>::begin_sections() const {
2185 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
2186 return section_iterator(SectionRef(ret, this));
2189 template<class ELFT>
2190 section_iterator ELFObjectFile<ELFT>::end_sections() const {
2192 ret.p = reinterpret_cast<intptr_t>(base()
2194 + (Header->e_shentsize*getNumSections()));
2195 return section_iterator(SectionRef(ret, this));
2198 template<class ELFT>
2199 typename ELFObjectFile<ELFT>::dyn_iterator
2200 ELFObjectFile<ELFT>::begin_dynamic_table() const {
2201 DataRefImpl DynData;
2202 if (dot_dynamic_sec == NULL || dot_dynamic_sec->sh_size == 0) {
2203 DynData.d.a = std::numeric_limits<uint32_t>::max();
2207 return dyn_iterator(DynRef(DynData, this));
2210 template<class ELFT>
2211 typename ELFObjectFile<ELFT>::dyn_iterator
2212 ELFObjectFile<ELFT>::end_dynamic_table() const {
2213 DataRefImpl DynData;
2214 DynData.d.a = std::numeric_limits<uint32_t>::max();
2215 return dyn_iterator(DynRef(DynData, this));
2218 template<class ELFT>
2219 error_code ELFObjectFile<ELFT>::getDynNext(DataRefImpl DynData,
2220 DynRef &Result) const {
2223 // Check to see if we are at the end of .dynamic
2224 if (DynData.d.a >= dot_dynamic_sec->getEntityCount()) {
2225 // We are at the end. Return the terminator.
2226 DynData.d.a = std::numeric_limits<uint32_t>::max();
2229 Result = DynRef(DynData, this);
2230 return object_error::success;
2233 template<class ELFT>
2235 ELFObjectFile<ELFT>::getLoadName() const {
2237 // Find the DT_SONAME entry
2238 dyn_iterator it = begin_dynamic_table();
2239 dyn_iterator ie = end_dynamic_table();
2242 if (it->getTag() == ELF::DT_SONAME)
2246 report_fatal_error("dynamic table iteration failed");
2249 if (dot_dynstr_sec == NULL)
2250 report_fatal_error("Dynamic string table is missing");
2251 dt_soname = getString(dot_dynstr_sec, it->getVal());
2259 template<class ELFT>
2260 library_iterator ELFObjectFile<ELFT>::begin_libraries_needed() const {
2261 // Find the first DT_NEEDED entry
2262 dyn_iterator i = begin_dynamic_table();
2263 dyn_iterator e = end_dynamic_table();
2266 if (i->getTag() == ELF::DT_NEEDED)
2270 report_fatal_error("dynamic table iteration failed");
2272 // Use the same DataRefImpl format as DynRef.
2273 return library_iterator(LibraryRef(i->getRawDataRefImpl(), this));
2276 template<class ELFT>
2277 error_code ELFObjectFile<ELFT>::getLibraryNext(DataRefImpl Data,
2278 LibraryRef &Result) const {
2279 // Use the same DataRefImpl format as DynRef.
2280 dyn_iterator i = dyn_iterator(DynRef(Data, this));
2281 dyn_iterator e = end_dynamic_table();
2283 // Skip the current dynamic table entry.
2287 // TODO: proper error handling
2289 report_fatal_error("dynamic table iteration failed");
2292 // Find the next DT_NEEDED entry.
2294 if (i->getTag() == ELF::DT_NEEDED)
2298 report_fatal_error("dynamic table iteration failed");
2300 Result = LibraryRef(i->getRawDataRefImpl(), this);
2301 return object_error::success;
2304 template<class ELFT>
2305 error_code ELFObjectFile<ELFT>::getLibraryPath(DataRefImpl Data,
2306 StringRef &Res) const {
2307 dyn_iterator i = dyn_iterator(DynRef(Data, this));
2308 if (i == end_dynamic_table())
2309 report_fatal_error("getLibraryPath() called on iterator end");
2311 if (i->getTag() != ELF::DT_NEEDED)
2312 report_fatal_error("Invalid library_iterator");
2314 // This uses .dynstr to lookup the name of the DT_NEEDED entry.
2315 // THis works as long as DT_STRTAB == .dynstr. This is true most of
2316 // the time, but the specification allows exceptions.
2317 // TODO: This should really use DT_STRTAB instead. Doing this requires
2318 // reading the program headers.
2319 if (dot_dynstr_sec == NULL)
2320 report_fatal_error("Dynamic string table is missing");
2321 Res = getString(dot_dynstr_sec, i->getVal());
2322 return object_error::success;
2325 template<class ELFT>
2326 library_iterator ELFObjectFile<ELFT>::end_libraries_needed() const {
2327 dyn_iterator e = end_dynamic_table();
2328 // Use the same DataRefImpl format as DynRef.
2329 return library_iterator(LibraryRef(e->getRawDataRefImpl(), this));
2332 template<class ELFT>
2333 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
2334 return ELFT::Is64Bits ? 8 : 4;
2337 template<class ELFT>
2338 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
2339 switch(Header->e_ident[ELF::EI_CLASS]) {
2340 case ELF::ELFCLASS32:
2341 switch(Header->e_machine) {
2343 return "ELF32-i386";
2344 case ELF::EM_X86_64:
2345 return "ELF32-x86-64";
2348 case ELF::EM_HEXAGON:
2349 return "ELF32-hexagon";
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");