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/SmallVector.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/PointerIntPair.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 // Subclasses of ELFObjectFile may need this for template instantiation
37 inline std::pair<unsigned char, unsigned char>
38 getElfArchType(MemoryBuffer *Object) {
39 if (Object->getBufferSize() < ELF::EI_NIDENT)
40 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE);
41 return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS]
42 , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]);
45 // Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
46 template<support::endianness target_endianness>
47 struct ELFDataTypeTypedefHelperCommon {
48 typedef support::detail::packed_endian_specific_integral
49 <uint16_t, target_endianness, support::aligned> Elf_Half;
50 typedef support::detail::packed_endian_specific_integral
51 <uint32_t, target_endianness, support::aligned> Elf_Word;
52 typedef support::detail::packed_endian_specific_integral
53 <int32_t, target_endianness, support::aligned> Elf_Sword;
54 typedef support::detail::packed_endian_specific_integral
55 <uint64_t, target_endianness, support::aligned> Elf_Xword;
56 typedef support::detail::packed_endian_specific_integral
57 <int64_t, target_endianness, support::aligned> Elf_Sxword;
60 template<support::endianness target_endianness, bool is64Bits>
61 struct ELFDataTypeTypedefHelper;
64 template<support::endianness target_endianness>
65 struct ELFDataTypeTypedefHelper<target_endianness, false>
66 : ELFDataTypeTypedefHelperCommon<target_endianness> {
67 typedef uint32_t value_type;
68 typedef support::detail::packed_endian_specific_integral
69 <value_type, target_endianness, support::aligned> Elf_Addr;
70 typedef support::detail::packed_endian_specific_integral
71 <value_type, target_endianness, support::aligned> Elf_Off;
75 template<support::endianness target_endianness>
76 struct ELFDataTypeTypedefHelper<target_endianness, true>
77 : ELFDataTypeTypedefHelperCommon<target_endianness>{
78 typedef uint64_t value_type;
79 typedef support::detail::packed_endian_specific_integral
80 <value_type, target_endianness, support::aligned> Elf_Addr;
81 typedef support::detail::packed_endian_specific_integral
82 <value_type, target_endianness, support::aligned> Elf_Off;
85 // I really don't like doing this, but the alternative is copypasta.
86 #define LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) \
88 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Addr Elf_Addr; \
90 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Off Elf_Off; \
92 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Half Elf_Half; \
94 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Word Elf_Word; \
96 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sword Elf_Sword; \
98 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Xword Elf_Xword; \
100 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sxword Elf_Sxword;
103 template<support::endianness target_endianness, bool is64Bits>
104 struct Elf_Shdr_Base;
106 template<support::endianness target_endianness>
107 struct Elf_Shdr_Base<target_endianness, false> {
108 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
109 Elf_Word sh_name; // Section name (index into string table)
110 Elf_Word sh_type; // Section type (SHT_*)
111 Elf_Word sh_flags; // Section flags (SHF_*)
112 Elf_Addr sh_addr; // Address where section is to be loaded
113 Elf_Off sh_offset; // File offset of section data, in bytes
114 Elf_Word sh_size; // Size of section, in bytes
115 Elf_Word sh_link; // Section type-specific header table index link
116 Elf_Word sh_info; // Section type-specific extra information
117 Elf_Word sh_addralign;// Section address alignment
118 Elf_Word sh_entsize; // Size of records contained within the section
121 template<support::endianness target_endianness>
122 struct Elf_Shdr_Base<target_endianness, true> {
123 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
124 Elf_Word sh_name; // Section name (index into string table)
125 Elf_Word sh_type; // Section type (SHT_*)
126 Elf_Xword sh_flags; // Section flags (SHF_*)
127 Elf_Addr sh_addr; // Address where section is to be loaded
128 Elf_Off sh_offset; // File offset of section data, in bytes
129 Elf_Xword sh_size; // Size of section, in bytes
130 Elf_Word sh_link; // Section type-specific header table index link
131 Elf_Word sh_info; // Section type-specific extra information
132 Elf_Xword sh_addralign;// Section address alignment
133 Elf_Xword sh_entsize; // Size of records contained within the section
136 template<support::endianness target_endianness, bool is64Bits>
137 struct Elf_Shdr_Impl : Elf_Shdr_Base<target_endianness, is64Bits> {
138 using Elf_Shdr_Base<target_endianness, is64Bits>::sh_entsize;
139 using Elf_Shdr_Base<target_endianness, is64Bits>::sh_size;
141 /// @brief Get the number of entities this section contains if it has any.
142 unsigned getEntityCount() const {
145 return sh_size / sh_entsize;
149 template<support::endianness target_endianness, bool is64Bits>
152 template<support::endianness target_endianness>
153 struct Elf_Sym_Base<target_endianness, false> {
154 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
155 Elf_Word st_name; // Symbol name (index into string table)
156 Elf_Addr st_value; // Value or address associated with the symbol
157 Elf_Word st_size; // Size of the symbol
158 unsigned char st_info; // Symbol's type and binding attributes
159 unsigned char st_other; // Must be zero; reserved
160 Elf_Half st_shndx; // Which section (header table index) it's defined in
163 template<support::endianness target_endianness>
164 struct Elf_Sym_Base<target_endianness, true> {
165 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
166 Elf_Word st_name; // Symbol name (index into string table)
167 unsigned char st_info; // Symbol's type and binding attributes
168 unsigned char st_other; // Must be zero; reserved
169 Elf_Half st_shndx; // Which section (header table index) it's defined in
170 Elf_Addr st_value; // Value or address associated with the symbol
171 Elf_Xword st_size; // Size of the symbol
174 template<support::endianness target_endianness, bool is64Bits>
175 struct Elf_Sym_Impl : Elf_Sym_Base<target_endianness, is64Bits> {
176 using Elf_Sym_Base<target_endianness, is64Bits>::st_info;
178 // These accessors and mutators correspond to the ELF32_ST_BIND,
179 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
180 unsigned char getBinding() const { return st_info >> 4; }
181 unsigned char getType() const { return st_info & 0x0f; }
182 void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
183 void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
184 void setBindingAndType(unsigned char b, unsigned char t) {
185 st_info = (b << 4) + (t & 0x0f);
189 /// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section
190 /// (.gnu.version). This structure is identical for ELF32 and ELF64.
191 template<support::endianness target_endianness, bool is64Bits>
192 struct Elf_Versym_Impl {
193 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
194 Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN)
197 template<support::endianness target_endianness, bool is64Bits>
198 struct Elf_Verdaux_Impl;
200 /// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section
201 /// (.gnu.version_d). This structure is identical for ELF32 and ELF64.
202 template<support::endianness target_endianness, bool is64Bits>
203 struct Elf_Verdef_Impl {
204 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
205 typedef Elf_Verdaux_Impl<target_endianness, is64Bits> Elf_Verdaux;
206 Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT)
207 Elf_Half vd_flags; // Bitwise flags (VER_DEF_*)
208 Elf_Half vd_ndx; // Version index, used in .gnu.version entries
209 Elf_Half vd_cnt; // Number of Verdaux entries
210 Elf_Word vd_hash; // Hash of name
211 Elf_Word vd_aux; // Offset to the first Verdaux entry (in bytes)
212 Elf_Word vd_next; // Offset to the next Verdef entry (in bytes)
214 /// Get the first Verdaux entry for this Verdef.
215 const Elf_Verdaux *getAux() const {
216 return reinterpret_cast<const Elf_Verdaux*>((const char*)this + vd_aux);
220 /// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef
221 /// section (.gnu.version_d). This structure is identical for ELF32 and ELF64.
222 template<support::endianness target_endianness, bool is64Bits>
223 struct Elf_Verdaux_Impl {
224 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
225 Elf_Word vda_name; // Version name (offset in string table)
226 Elf_Word vda_next; // Offset to next Verdaux entry (in bytes)
229 /// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed
230 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
231 template<support::endianness target_endianness, bool is64Bits>
232 struct Elf_Verneed_Impl {
233 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
234 Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT)
235 Elf_Half vn_cnt; // Number of associated Vernaux entries
236 Elf_Word vn_file; // Library name (string table offset)
237 Elf_Word vn_aux; // Offset to first Vernaux entry (in bytes)
238 Elf_Word vn_next; // Offset to next Verneed entry (in bytes)
241 /// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed
242 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
243 template<support::endianness target_endianness, bool is64Bits>
244 struct Elf_Vernaux_Impl {
245 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
246 Elf_Word vna_hash; // Hash of dependency name
247 Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*)
248 Elf_Half vna_other; // Version index, used in .gnu.version entries
249 Elf_Word vna_name; // Dependency name
250 Elf_Word vna_next; // Offset to next Vernaux entry (in bytes)
253 /// Elf_Dyn_Base: This structure matches the form of entries in the dynamic
254 /// table section (.dynamic) look like.
255 template<support::endianness target_endianness, bool is64Bits>
258 template<support::endianness target_endianness>
259 struct Elf_Dyn_Base<target_endianness, false> {
260 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
268 template<support::endianness target_endianness>
269 struct Elf_Dyn_Base<target_endianness, true> {
270 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
278 /// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters.
279 template<support::endianness target_endianness, bool is64Bits>
280 struct Elf_Dyn_Impl : Elf_Dyn_Base<target_endianness, is64Bits> {
281 using Elf_Dyn_Base<target_endianness, is64Bits>::d_tag;
282 using Elf_Dyn_Base<target_endianness, is64Bits>::d_un;
283 int64_t getTag() const { return d_tag; }
284 uint64_t getVal() const { return d_un.d_val; }
285 uint64_t getPtr() const { return d_un.ptr; }
288 template<support::endianness target_endianness, bool is64Bits>
291 // DynRefImpl: Reference to an entry in the dynamic table
292 // This is an ELF-specific interface.
293 template<support::endianness target_endianness, bool is64Bits>
295 typedef Elf_Dyn_Impl<target_endianness, is64Bits> Elf_Dyn;
296 typedef ELFObjectFile<target_endianness, is64Bits> OwningType;
298 DataRefImpl DynPimpl;
299 const OwningType *OwningObject;
302 DynRefImpl() : OwningObject(NULL) { }
304 DynRefImpl(DataRefImpl DynP, const OwningType *Owner);
306 bool operator==(const DynRefImpl &Other) const;
307 bool operator <(const DynRefImpl &Other) const;
309 error_code getNext(DynRefImpl &Result) const;
310 int64_t getTag() const;
311 uint64_t getVal() const;
312 uint64_t getPtr() const;
314 DataRefImpl getRawDataRefImpl() const;
317 // Elf_Rel: Elf Relocation
318 template<support::endianness target_endianness, bool is64Bits, bool isRela>
321 template<support::endianness target_endianness>
322 struct Elf_Rel_Base<target_endianness, false, false> {
323 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
324 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
325 Elf_Word r_info; // Symbol table index and type of relocation to apply
328 template<support::endianness target_endianness>
329 struct Elf_Rel_Base<target_endianness, true, false> {
330 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
331 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
332 Elf_Xword r_info; // Symbol table index and type of relocation to apply
335 template<support::endianness target_endianness>
336 struct Elf_Rel_Base<target_endianness, false, true> {
337 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
338 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
339 Elf_Word r_info; // Symbol table index and type of relocation to apply
340 Elf_Sword r_addend; // Compute value for relocatable field by adding this
343 template<support::endianness target_endianness>
344 struct Elf_Rel_Base<target_endianness, true, true> {
345 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
346 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
347 Elf_Xword r_info; // Symbol table index and type of relocation to apply
348 Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
351 template<support::endianness target_endianness, bool is64Bits, bool isRela>
354 template<support::endianness target_endianness, bool isRela>
355 struct Elf_Rel_Impl<target_endianness, true, isRela>
356 : Elf_Rel_Base<target_endianness, true, isRela> {
357 using Elf_Rel_Base<target_endianness, true, isRela>::r_info;
358 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
360 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
361 // and ELF64_R_INFO macros defined in the ELF specification:
362 uint64_t getSymbol() const { return (r_info >> 32); }
363 unsigned char getType() const {
364 return (unsigned char) (r_info & 0xffffffffL);
366 void setSymbol(uint64_t s) { setSymbolAndType(s, getType()); }
367 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
368 void setSymbolAndType(uint64_t s, unsigned char t) {
369 r_info = (s << 32) + (t&0xffffffffL);
373 template<support::endianness target_endianness, bool isRela>
374 struct Elf_Rel_Impl<target_endianness, false, isRela>
375 : Elf_Rel_Base<target_endianness, false, isRela> {
376 using Elf_Rel_Base<target_endianness, false, isRela>::r_info;
377 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
379 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
380 // and ELF32_R_INFO macros defined in the ELF specification:
381 uint32_t getSymbol() const { return (r_info >> 8); }
382 unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); }
383 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); }
384 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
385 void setSymbolAndType(uint32_t s, unsigned char t) {
386 r_info = (s << 8) + t;
390 template<support::endianness target_endianness, bool is64Bits>
391 struct Elf_Ehdr_Impl {
392 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
393 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
394 Elf_Half e_type; // Type of file (see ET_*)
395 Elf_Half e_machine; // Required architecture for this file (see EM_*)
396 Elf_Word e_version; // Must be equal to 1
397 Elf_Addr e_entry; // Address to jump to in order to start program
398 Elf_Off e_phoff; // Program header table's file offset, in bytes
399 Elf_Off e_shoff; // Section header table's file offset, in bytes
400 Elf_Word e_flags; // Processor-specific flags
401 Elf_Half e_ehsize; // Size of ELF header, in bytes
402 Elf_Half e_phentsize;// Size of an entry in the program header table
403 Elf_Half e_phnum; // Number of entries in the program header table
404 Elf_Half e_shentsize;// Size of an entry in the section header table
405 Elf_Half e_shnum; // Number of entries in the section header table
406 Elf_Half e_shstrndx; // Section header table index of section name
408 bool checkMagic() const {
409 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
411 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
412 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
415 template<support::endianness target_endianness, bool is64Bits>
416 class ELFObjectFile : public ObjectFile {
417 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
419 typedef Elf_Ehdr_Impl<target_endianness, is64Bits> Elf_Ehdr;
420 typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
421 typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
422 typedef Elf_Dyn_Impl<target_endianness, is64Bits> Elf_Dyn;
423 typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel;
424 typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela;
425 typedef Elf_Verdef_Impl<target_endianness, is64Bits> Elf_Verdef;
426 typedef Elf_Verdaux_Impl<target_endianness, is64Bits> Elf_Verdaux;
427 typedef Elf_Verneed_Impl<target_endianness, is64Bits> Elf_Verneed;
428 typedef Elf_Vernaux_Impl<target_endianness, is64Bits> Elf_Vernaux;
429 typedef Elf_Versym_Impl<target_endianness, is64Bits> Elf_Versym;
430 typedef DynRefImpl<target_endianness, is64Bits> DynRef;
431 typedef content_iterator<DynRef> dyn_iterator;
434 // This flag is used for classof, to distinguish ELFObjectFile from
435 // its subclass. If more subclasses will be created, this flag will
436 // have to become an enum.
437 bool isDyldELFObject;
440 typedef SmallVector<const Elf_Shdr*, 1> Sections_t;
441 typedef DenseMap<unsigned, unsigned> IndexMap_t;
442 typedef DenseMap<const Elf_Shdr*, SmallVector<uint32_t, 1> > RelocMap_t;
444 const Elf_Ehdr *Header;
445 const Elf_Shdr *SectionHeaderTable;
446 const Elf_Shdr *dot_shstrtab_sec; // Section header string table.
447 const Elf_Shdr *dot_strtab_sec; // Symbol header string table.
448 const Elf_Shdr *dot_dynstr_sec; // Dynamic symbol string table.
450 // SymbolTableSections[0] always points to the dynamic string table section
451 // header, or NULL if there is no dynamic string table.
452 Sections_t SymbolTableSections;
453 IndexMap_t SymbolTableSectionsIndexMap;
454 DenseMap<const Elf_Sym*, ELF::Elf64_Word> ExtendedSymbolTable;
456 const Elf_Shdr *dot_dynamic_sec; // .dynamic
457 const Elf_Shdr *dot_gnu_version_sec; // .gnu.version
458 const Elf_Shdr *dot_gnu_version_r_sec; // .gnu.version_r
459 const Elf_Shdr *dot_gnu_version_d_sec; // .gnu.version_d
461 // Pointer to SONAME entry in dynamic string table
462 // This is set the first time getLoadName is called.
463 mutable const char *dt_soname;
466 /// \brief Iterate over relocations in a .rel or .rela section.
467 template<class RelocT>
468 class ELFRelocationIterator {
470 typedef void difference_type;
471 typedef const RelocT value_type;
472 typedef std::forward_iterator_tag iterator_category;
473 typedef value_type &reference;
474 typedef value_type *pointer;
476 /// \brief Default construct iterator.
477 ELFRelocationIterator() : Section(0), Current(0) {}
478 ELFRelocationIterator(const Elf_Shdr *Sec, const char *Start)
482 reference operator *() {
483 assert(Current && "Attempted to dereference an invalid iterator!");
484 return *reinterpret_cast<const RelocT*>(Current);
487 pointer operator ->() {
488 assert(Current && "Attempted to dereference an invalid iterator!");
489 return reinterpret_cast<const RelocT*>(Current);
492 bool operator ==(const ELFRelocationIterator &Other) {
493 return Section == Other.Section && Current == Other.Current;
496 bool operator !=(const ELFRelocationIterator &Other) {
497 return !(*this == Other);
500 ELFRelocationIterator &operator ++(int) {
501 assert(Current && "Attempted to increment an invalid iterator!");
502 Current += Section->sh_entsize;
506 ELFRelocationIterator operator ++() {
507 ELFRelocationIterator Tmp = *this;
513 const Elf_Shdr *Section;
518 // Records for each version index the corresponding Verdef or Vernaux entry.
519 // This is filled the first time LoadVersionMap() is called.
520 class VersionMapEntry : public PointerIntPair<const void*, 1> {
522 // If the integer is 0, this is an Elf_Verdef*.
523 // If the integer is 1, this is an Elf_Vernaux*.
524 VersionMapEntry() : PointerIntPair<const void*, 1>(NULL, 0) { }
525 VersionMapEntry(const Elf_Verdef *verdef)
526 : PointerIntPair<const void*, 1>(verdef, 0) { }
527 VersionMapEntry(const Elf_Vernaux *vernaux)
528 : PointerIntPair<const void*, 1>(vernaux, 1) { }
529 bool isNull() const { return getPointer() == NULL; }
530 bool isVerdef() const { return !isNull() && getInt() == 0; }
531 bool isVernaux() const { return !isNull() && getInt() == 1; }
532 const Elf_Verdef *getVerdef() const {
533 return isVerdef() ? (const Elf_Verdef*)getPointer() : NULL;
535 const Elf_Vernaux *getVernaux() const {
536 return isVernaux() ? (const Elf_Vernaux*)getPointer() : NULL;
539 mutable SmallVector<VersionMapEntry, 16> VersionMap;
540 void LoadVersionDefs(const Elf_Shdr *sec) const;
541 void LoadVersionNeeds(const Elf_Shdr *ec) const;
542 void LoadVersionMap() const;
544 /// @brief Map sections to an array of relocation sections that reference
545 /// them sorted by section index.
546 RelocMap_t SectionRelocMap;
548 /// @brief Get the relocation section that contains \a Rel.
549 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
550 return getSection(Rel.w.b);
553 bool isRelocationHasAddend(DataRefImpl Rel) const;
555 const T *getEntry(uint16_t Section, uint32_t Entry) const;
557 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
558 const Elf_Shdr *getSection(DataRefImpl index) const;
559 const Elf_Shdr *getSection(uint32_t index) const;
560 const Elf_Rel *getRel(DataRefImpl Rel) const;
561 const Elf_Rela *getRela(DataRefImpl Rela) const;
562 const char *getString(uint32_t section, uint32_t offset) const;
563 const char *getString(const Elf_Shdr *section, uint32_t offset) const;
564 error_code getSymbolVersion(const Elf_Shdr *section,
567 bool &IsDefault) const;
568 void VerifyStrTab(const Elf_Shdr *sh) const;
571 const Elf_Sym *getSymbol(DataRefImpl Symb) const; // FIXME: Should be private?
572 void validateSymbol(DataRefImpl Symb) const;
575 error_code getSymbolName(const Elf_Shdr *section,
577 StringRef &Res) const;
578 error_code getSectionName(const Elf_Shdr *section,
579 StringRef &Res) const;
580 const Elf_Dyn *getDyn(DataRefImpl DynData) const;
581 error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
582 bool &IsDefault) const;
584 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
585 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
586 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
587 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
588 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
589 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
590 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
591 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
592 virtual error_code getSymbolSection(DataRefImpl Symb,
593 section_iterator &Res) const;
595 friend class DynRefImpl<target_endianness, is64Bits>;
596 virtual error_code getDynNext(DataRefImpl DynData, DynRef &Result) const;
598 virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const;
599 virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const;
601 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
602 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
603 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
604 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
605 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
606 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
607 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
608 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
609 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
610 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
612 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
613 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
614 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
616 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
617 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
619 virtual error_code getRelocationNext(DataRefImpl Rel,
620 RelocationRef &Res) const;
621 virtual error_code getRelocationAddress(DataRefImpl Rel,
622 uint64_t &Res) const;
623 virtual error_code getRelocationOffset(DataRefImpl Rel,
624 uint64_t &Res) const;
625 virtual error_code getRelocationSymbol(DataRefImpl Rel,
626 SymbolRef &Res) const;
627 virtual error_code getRelocationType(DataRefImpl Rel,
628 uint64_t &Res) const;
629 virtual error_code getRelocationTypeName(DataRefImpl Rel,
630 SmallVectorImpl<char> &Result) const;
631 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
633 virtual error_code getRelocationValueString(DataRefImpl Rel,
634 SmallVectorImpl<char> &Result) const;
637 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
638 virtual symbol_iterator begin_symbols() const;
639 virtual symbol_iterator end_symbols() const;
641 virtual symbol_iterator begin_dynamic_symbols() const;
642 virtual symbol_iterator end_dynamic_symbols() const;
644 virtual section_iterator begin_sections() const;
645 virtual section_iterator end_sections() const;
647 virtual library_iterator begin_libraries_needed() const;
648 virtual library_iterator end_libraries_needed() const;
650 virtual dyn_iterator begin_dynamic_table() const;
651 virtual dyn_iterator end_dynamic_table() const;
653 typedef ELFRelocationIterator<Elf_Rela> Elf_Rela_Iter;
654 typedef ELFRelocationIterator<Elf_Rel> Elf_Rel_Iter;
656 virtual Elf_Rela_Iter beginELFRela(const Elf_Shdr *sec) const {
657 return Elf_Rela_Iter(sec, (const char *)(base() + sec->sh_offset));
660 virtual Elf_Rela_Iter endELFRela(const Elf_Shdr *sec) const {
661 return Elf_Rela_Iter(sec, (const char *)
662 (base() + sec->sh_offset + sec->sh_size));
665 virtual Elf_Rel_Iter beginELFRel(const Elf_Shdr *sec) const {
666 return Elf_Rel_Iter(sec, (const char *)(base() + sec->sh_offset));
669 virtual Elf_Rel_Iter endELFRel(const Elf_Shdr *sec) const {
670 return Elf_Rel_Iter(sec, (const char *)
671 (base() + sec->sh_offset + sec->sh_size));
674 virtual uint8_t getBytesInAddress() const;
675 virtual StringRef getFileFormatName() const;
676 virtual StringRef getObjectType() const { return "ELF"; }
677 virtual unsigned getArch() const;
678 virtual StringRef getLoadName() const;
679 virtual error_code getSectionContents(const Elf_Shdr *sec,
680 StringRef &Res) const;
682 uint64_t getNumSections() const;
683 uint64_t getStringTableIndex() const;
684 ELF::Elf64_Word getSymbolTableIndex(const Elf_Sym *symb) const;
685 const Elf_Shdr *getSection(const Elf_Sym *symb) const;
686 const Elf_Shdr *getElfSection(section_iterator &It) const;
687 const Elf_Sym *getElfSymbol(symbol_iterator &It) const;
688 const Elf_Sym *getElfSymbol(uint32_t index) const;
690 // Methods for type inquiry through isa, cast, and dyn_cast
691 bool isDyldType() const { return isDyldELFObject; }
692 static inline bool classof(const Binary *v) {
693 return v->getType() == getELFType(target_endianness == support::little,
696 static inline bool classof(const ELFObjectFile *v) { return true; }
699 // Iterate through the version definitions, and place each Elf_Verdef
700 // in the VersionMap according to its index.
701 template<support::endianness target_endianness, bool is64Bits>
702 void ELFObjectFile<target_endianness, is64Bits>::
703 LoadVersionDefs(const Elf_Shdr *sec) const {
704 unsigned vd_size = sec->sh_size; // Size of section in bytes
705 unsigned vd_count = sec->sh_info; // Number of Verdef entries
706 const char *sec_start = (const char*)base() + sec->sh_offset;
707 const char *sec_end = sec_start + vd_size;
708 // The first Verdef entry is at the start of the section.
709 const char *p = sec_start;
710 for (unsigned i = 0; i < vd_count; i++) {
711 if (p + sizeof(Elf_Verdef) > sec_end)
712 report_fatal_error("Section ended unexpectedly while scanning "
713 "version definitions.");
714 const Elf_Verdef *vd = reinterpret_cast<const Elf_Verdef *>(p);
715 if (vd->vd_version != ELF::VER_DEF_CURRENT)
716 report_fatal_error("Unexpected verdef version");
717 size_t index = vd->vd_ndx & ELF::VERSYM_VERSION;
718 if (index >= VersionMap.size())
719 VersionMap.resize(index+1);
720 VersionMap[index] = VersionMapEntry(vd);
725 // Iterate through the versions needed section, and place each Elf_Vernaux
726 // in the VersionMap according to its index.
727 template<support::endianness target_endianness, bool is64Bits>
728 void ELFObjectFile<target_endianness, is64Bits>::
729 LoadVersionNeeds(const Elf_Shdr *sec) const {
730 unsigned vn_size = sec->sh_size; // Size of section in bytes
731 unsigned vn_count = sec->sh_info; // Number of Verneed entries
732 const char *sec_start = (const char*)base() + sec->sh_offset;
733 const char *sec_end = sec_start + vn_size;
734 // The first Verneed entry is at the start of the section.
735 const char *p = sec_start;
736 for (unsigned i = 0; i < vn_count; i++) {
737 if (p + sizeof(Elf_Verneed) > sec_end)
738 report_fatal_error("Section ended unexpectedly while scanning "
739 "version needed records.");
740 const Elf_Verneed *vn = reinterpret_cast<const Elf_Verneed *>(p);
741 if (vn->vn_version != ELF::VER_NEED_CURRENT)
742 report_fatal_error("Unexpected verneed version");
743 // Iterate through the Vernaux entries
744 const char *paux = p + vn->vn_aux;
745 for (unsigned j = 0; j < vn->vn_cnt; j++) {
746 if (paux + sizeof(Elf_Vernaux) > sec_end)
747 report_fatal_error("Section ended unexpected while scanning auxiliary "
748 "version needed records.");
749 const Elf_Vernaux *vna = reinterpret_cast<const Elf_Vernaux *>(paux);
750 size_t index = vna->vna_other & ELF::VERSYM_VERSION;
751 if (index >= VersionMap.size())
752 VersionMap.resize(index+1);
753 VersionMap[index] = VersionMapEntry(vna);
754 paux += vna->vna_next;
760 template<support::endianness target_endianness, bool is64Bits>
761 void ELFObjectFile<target_endianness, is64Bits>::LoadVersionMap() const {
762 // If there is no dynamic symtab or version table, there is nothing to do.
763 if (SymbolTableSections[0] == NULL || dot_gnu_version_sec == NULL)
766 // Has the VersionMap already been loaded?
767 if (VersionMap.size() > 0)
770 // The first two version indexes are reserved.
771 // Index 0 is LOCAL, index 1 is GLOBAL.
772 VersionMap.push_back(VersionMapEntry());
773 VersionMap.push_back(VersionMapEntry());
775 if (dot_gnu_version_d_sec)
776 LoadVersionDefs(dot_gnu_version_d_sec);
778 if (dot_gnu_version_r_sec)
779 LoadVersionNeeds(dot_gnu_version_r_sec);
782 template<support::endianness target_endianness, bool is64Bits>
783 void ELFObjectFile<target_endianness, is64Bits>
784 ::validateSymbol(DataRefImpl Symb) const {
785 const Elf_Sym *symb = getSymbol(Symb);
786 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
787 // FIXME: We really need to do proper error handling in the case of an invalid
788 // input file. Because we don't use exceptions, I think we'll just pass
789 // an error object around.
791 && SymbolTableSection
792 && symb >= (const Elf_Sym*)(base()
793 + SymbolTableSection->sh_offset)
794 && symb < (const Elf_Sym*)(base()
795 + SymbolTableSection->sh_offset
796 + SymbolTableSection->sh_size)))
797 // FIXME: Proper error handling.
798 report_fatal_error("Symb must point to a valid symbol!");
801 template<support::endianness target_endianness, bool is64Bits>
802 error_code ELFObjectFile<target_endianness, is64Bits>
803 ::getSymbolNext(DataRefImpl Symb,
804 SymbolRef &Result) const {
805 validateSymbol(Symb);
806 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
809 // Check to see if we are at the end of this symbol table.
810 if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
811 // We are at the end. If there are other symbol tables, jump to them.
812 // If the symbol table is .dynsym, we are iterating dynamic symbols,
813 // and there is only one table of these.
816 Symb.d.a = 1; // The 0th symbol in ELF is fake.
818 // Otherwise return the terminator.
819 if (Symb.d.b == 0 || Symb.d.b >= SymbolTableSections.size()) {
820 Symb.d.a = std::numeric_limits<uint32_t>::max();
821 Symb.d.b = std::numeric_limits<uint32_t>::max();
825 Result = SymbolRef(Symb, this);
826 return object_error::success;
829 template<support::endianness target_endianness, bool is64Bits>
830 error_code ELFObjectFile<target_endianness, is64Bits>
831 ::getSymbolName(DataRefImpl Symb,
832 StringRef &Result) const {
833 validateSymbol(Symb);
834 const Elf_Sym *symb = getSymbol(Symb);
835 return getSymbolName(SymbolTableSections[Symb.d.b], symb, Result);
838 template<support::endianness target_endianness, bool is64Bits>
839 error_code ELFObjectFile<target_endianness, is64Bits>
840 ::getSymbolVersion(SymbolRef SymRef,
842 bool &IsDefault) const {
843 DataRefImpl Symb = SymRef.getRawDataRefImpl();
844 validateSymbol(Symb);
845 const Elf_Sym *symb = getSymbol(Symb);
846 return getSymbolVersion(SymbolTableSections[Symb.d.b], symb,
850 template<support::endianness target_endianness, bool is64Bits>
851 ELF::Elf64_Word ELFObjectFile<target_endianness, is64Bits>
852 ::getSymbolTableIndex(const Elf_Sym *symb) const {
853 if (symb->st_shndx == ELF::SHN_XINDEX)
854 return ExtendedSymbolTable.lookup(symb);
855 return symb->st_shndx;
858 template<support::endianness target_endianness, bool is64Bits>
859 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
860 ELFObjectFile<target_endianness, is64Bits>
861 ::getSection(const Elf_Sym *symb) const {
862 if (symb->st_shndx == ELF::SHN_XINDEX)
863 return getSection(ExtendedSymbolTable.lookup(symb));
864 if (symb->st_shndx >= ELF::SHN_LORESERVE)
866 return getSection(symb->st_shndx);
869 template<support::endianness target_endianness, bool is64Bits>
870 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
871 ELFObjectFile<target_endianness, is64Bits>
872 ::getElfSection(section_iterator &It) const {
873 llvm::object::DataRefImpl ShdrRef = It->getRawDataRefImpl();
874 return reinterpret_cast<const Elf_Shdr *>(ShdrRef.p);
877 template<support::endianness target_endianness, bool is64Bits>
878 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym *
879 ELFObjectFile<target_endianness, is64Bits>
880 ::getElfSymbol(symbol_iterator &It) const {
881 return getSymbol(It->getRawDataRefImpl());
884 template<support::endianness target_endianness, bool is64Bits>
885 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym *
886 ELFObjectFile<target_endianness, is64Bits>
887 ::getElfSymbol(uint32_t index) const {
888 DataRefImpl SymbolData;
889 SymbolData.d.a = index;
891 return getSymbol(SymbolData);
894 template<support::endianness target_endianness, bool is64Bits>
895 error_code ELFObjectFile<target_endianness, is64Bits>
896 ::getSymbolFileOffset(DataRefImpl Symb,
897 uint64_t &Result) const {
898 validateSymbol(Symb);
899 const Elf_Sym *symb = getSymbol(Symb);
900 const Elf_Shdr *Section;
901 switch (getSymbolTableIndex(symb)) {
902 case ELF::SHN_COMMON:
903 // Unintialized symbols have no offset in the object file
905 Result = UnknownAddressOrSize;
906 return object_error::success;
908 Result = symb->st_value;
909 return object_error::success;
910 default: Section = getSection(symb);
913 switch (symb->getType()) {
914 case ELF::STT_SECTION:
915 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
916 return object_error::success;
918 case ELF::STT_OBJECT:
919 case ELF::STT_NOTYPE:
920 Result = symb->st_value +
921 (Section ? Section->sh_offset : 0);
922 return object_error::success;
924 Result = UnknownAddressOrSize;
925 return object_error::success;
929 template<support::endianness target_endianness, bool is64Bits>
930 error_code ELFObjectFile<target_endianness, is64Bits>
931 ::getSymbolAddress(DataRefImpl Symb,
932 uint64_t &Result) const {
933 validateSymbol(Symb);
934 const Elf_Sym *symb = getSymbol(Symb);
935 const Elf_Shdr *Section;
936 switch (getSymbolTableIndex(symb)) {
937 case ELF::SHN_COMMON:
939 Result = UnknownAddressOrSize;
940 return object_error::success;
942 Result = symb->st_value;
943 return object_error::success;
944 default: Section = getSection(symb);
947 switch (symb->getType()) {
948 case ELF::STT_SECTION:
949 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
950 return object_error::success;
952 case ELF::STT_OBJECT:
953 case ELF::STT_NOTYPE:
955 switch(Header->e_type) {
958 IsRelocatable = false;
961 IsRelocatable = true;
963 Result = symb->st_value;
964 if (IsRelocatable && Section != 0)
965 Result += Section->sh_addr;
966 return object_error::success;
968 Result = UnknownAddressOrSize;
969 return object_error::success;
973 template<support::endianness target_endianness, bool is64Bits>
974 error_code ELFObjectFile<target_endianness, is64Bits>
975 ::getSymbolSize(DataRefImpl Symb,
976 uint64_t &Result) const {
977 validateSymbol(Symb);
978 const Elf_Sym *symb = getSymbol(Symb);
979 if (symb->st_size == 0)
980 Result = UnknownAddressOrSize;
981 Result = symb->st_size;
982 return object_error::success;
985 template<support::endianness target_endianness, bool is64Bits>
986 error_code ELFObjectFile<target_endianness, is64Bits>
987 ::getSymbolNMTypeChar(DataRefImpl Symb,
988 char &Result) const {
989 validateSymbol(Symb);
990 const Elf_Sym *symb = getSymbol(Symb);
991 const Elf_Shdr *Section = getSection(symb);
996 switch (Section->sh_type) {
997 case ELF::SHT_PROGBITS:
998 case ELF::SHT_DYNAMIC:
999 switch (Section->sh_flags) {
1000 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
1002 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
1004 case ELF::SHF_ALLOC:
1005 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
1006 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
1010 case ELF::SHT_NOBITS: ret = 'b';
1014 switch (getSymbolTableIndex(symb)) {
1015 case ELF::SHN_UNDEF:
1019 case ELF::SHN_ABS: ret = 'a'; break;
1020 case ELF::SHN_COMMON: ret = 'c'; break;
1023 switch (symb->getBinding()) {
1024 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
1026 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1029 if (symb->getType() == ELF::STT_OBJECT)
1035 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
1037 if (error_code ec = getSymbolName(Symb, name))
1039 Result = StringSwitch<char>(name)
1040 .StartsWith(".debug", 'N')
1041 .StartsWith(".note", 'n')
1043 return object_error::success;
1047 return object_error::success;
1050 template<support::endianness target_endianness, bool is64Bits>
1051 error_code ELFObjectFile<target_endianness, is64Bits>
1052 ::getSymbolType(DataRefImpl Symb,
1053 SymbolRef::Type &Result) const {
1054 validateSymbol(Symb);
1055 const Elf_Sym *symb = getSymbol(Symb);
1057 switch (symb->getType()) {
1058 case ELF::STT_NOTYPE:
1059 Result = SymbolRef::ST_Unknown;
1061 case ELF::STT_SECTION:
1062 Result = SymbolRef::ST_Debug;
1065 Result = SymbolRef::ST_File;
1068 Result = SymbolRef::ST_Function;
1070 case ELF::STT_OBJECT:
1071 case ELF::STT_COMMON:
1073 Result = SymbolRef::ST_Data;
1076 Result = SymbolRef::ST_Other;
1079 return object_error::success;
1082 template<support::endianness target_endianness, bool is64Bits>
1083 error_code ELFObjectFile<target_endianness, is64Bits>
1084 ::getSymbolFlags(DataRefImpl Symb,
1085 uint32_t &Result) const {
1086 validateSymbol(Symb);
1087 const Elf_Sym *symb = getSymbol(Symb);
1089 Result = SymbolRef::SF_None;
1091 if (symb->getBinding() != ELF::STB_LOCAL)
1092 Result |= SymbolRef::SF_Global;
1094 if (symb->getBinding() == ELF::STB_WEAK)
1095 Result |= SymbolRef::SF_Weak;
1097 if (symb->st_shndx == ELF::SHN_ABS)
1098 Result |= SymbolRef::SF_Absolute;
1100 if (symb->getType() == ELF::STT_FILE ||
1101 symb->getType() == ELF::STT_SECTION)
1102 Result |= SymbolRef::SF_FormatSpecific;
1104 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1105 Result |= SymbolRef::SF_Undefined;
1107 if (symb->getType() == ELF::STT_COMMON ||
1108 getSymbolTableIndex(symb) == ELF::SHN_COMMON)
1109 Result |= SymbolRef::SF_Common;
1111 if (symb->getType() == ELF::STT_TLS)
1112 Result |= SymbolRef::SF_ThreadLocal;
1114 return object_error::success;
1117 template<support::endianness target_endianness, bool is64Bits>
1118 error_code ELFObjectFile<target_endianness, is64Bits>
1119 ::getSymbolSection(DataRefImpl Symb,
1120 section_iterator &Res) const {
1121 validateSymbol(Symb);
1122 const Elf_Sym *symb = getSymbol(Symb);
1123 const Elf_Shdr *sec = getSection(symb);
1125 Res = end_sections();
1128 Sec.p = reinterpret_cast<intptr_t>(sec);
1129 Res = section_iterator(SectionRef(Sec, this));
1131 return object_error::success;
1134 template<support::endianness target_endianness, bool is64Bits>
1135 error_code ELFObjectFile<target_endianness, is64Bits>
1136 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
1137 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
1138 sec += Header->e_shentsize;
1139 Sec.p = reinterpret_cast<intptr_t>(sec);
1140 Result = SectionRef(Sec, this);
1141 return object_error::success;
1144 template<support::endianness target_endianness, bool is64Bits>
1145 error_code ELFObjectFile<target_endianness, is64Bits>
1146 ::getSectionName(DataRefImpl Sec,
1147 StringRef &Result) const {
1148 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1149 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
1150 return object_error::success;
1153 template<support::endianness target_endianness, bool is64Bits>
1154 error_code ELFObjectFile<target_endianness, is64Bits>
1155 ::getSectionAddress(DataRefImpl Sec,
1156 uint64_t &Result) const {
1157 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1158 Result = sec->sh_addr;
1159 return object_error::success;
1162 template<support::endianness target_endianness, bool is64Bits>
1163 error_code ELFObjectFile<target_endianness, is64Bits>
1164 ::getSectionSize(DataRefImpl Sec,
1165 uint64_t &Result) const {
1166 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1167 Result = sec->sh_size;
1168 return object_error::success;
1171 template<support::endianness target_endianness, bool is64Bits>
1172 error_code ELFObjectFile<target_endianness, is64Bits>
1173 ::getSectionContents(DataRefImpl Sec,
1174 StringRef &Result) const {
1175 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1176 const char *start = (const char*)base() + sec->sh_offset;
1177 Result = StringRef(start, sec->sh_size);
1178 return object_error::success;
1181 template<support::endianness target_endianness, bool is64Bits>
1182 error_code ELFObjectFile<target_endianness, is64Bits>
1183 ::getSectionContents(const Elf_Shdr *Sec,
1184 StringRef &Result) const {
1185 const char *start = (const char*)base() + Sec->sh_offset;
1186 Result = StringRef(start, Sec->sh_size);
1187 return object_error::success;
1190 template<support::endianness target_endianness, bool is64Bits>
1191 error_code ELFObjectFile<target_endianness, is64Bits>
1192 ::getSectionAlignment(DataRefImpl Sec,
1193 uint64_t &Result) const {
1194 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1195 Result = sec->sh_addralign;
1196 return object_error::success;
1199 template<support::endianness target_endianness, bool is64Bits>
1200 error_code ELFObjectFile<target_endianness, is64Bits>
1201 ::isSectionText(DataRefImpl Sec,
1202 bool &Result) const {
1203 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1204 if (sec->sh_flags & ELF::SHF_EXECINSTR)
1208 return object_error::success;
1211 template<support::endianness target_endianness, bool is64Bits>
1212 error_code ELFObjectFile<target_endianness, is64Bits>
1213 ::isSectionData(DataRefImpl Sec,
1214 bool &Result) const {
1215 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1216 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1217 && sec->sh_type == ELF::SHT_PROGBITS)
1221 return object_error::success;
1224 template<support::endianness target_endianness, bool is64Bits>
1225 error_code ELFObjectFile<target_endianness, is64Bits>
1226 ::isSectionBSS(DataRefImpl Sec,
1227 bool &Result) const {
1228 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1229 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1230 && sec->sh_type == ELF::SHT_NOBITS)
1234 return object_error::success;
1237 template<support::endianness target_endianness, bool is64Bits>
1238 error_code ELFObjectFile<target_endianness, is64Bits>
1239 ::isSectionRequiredForExecution(DataRefImpl Sec,
1240 bool &Result) const {
1241 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1242 if (sec->sh_flags & ELF::SHF_ALLOC)
1246 return object_error::success;
1249 template<support::endianness target_endianness, bool is64Bits>
1250 error_code ELFObjectFile<target_endianness, is64Bits>
1251 ::isSectionVirtual(DataRefImpl Sec,
1252 bool &Result) const {
1253 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1254 if (sec->sh_type == ELF::SHT_NOBITS)
1258 return object_error::success;
1261 template<support::endianness target_endianness, bool is64Bits>
1262 error_code ELFObjectFile<target_endianness, is64Bits>::isSectionZeroInit(DataRefImpl Sec,
1263 bool &Result) const {
1264 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1265 // For ELF, all zero-init sections are virtual (that is, they occupy no space
1266 // in the object image) and vice versa.
1267 if (sec->sh_flags & ELF::SHT_NOBITS)
1271 return object_error::success;
1274 template<support::endianness target_endianness, bool is64Bits>
1275 error_code ELFObjectFile<target_endianness, is64Bits>
1276 ::sectionContainsSymbol(DataRefImpl Sec,
1278 bool &Result) const {
1279 // FIXME: Unimplemented.
1281 return object_error::success;
1284 template<support::endianness target_endianness, bool is64Bits>
1285 relocation_iterator ELFObjectFile<target_endianness, is64Bits>
1286 ::getSectionRelBegin(DataRefImpl Sec) const {
1287 DataRefImpl RelData;
1288 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1289 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1290 if (sec != 0 && ittr != SectionRelocMap.end()) {
1291 RelData.w.a = getSection(ittr->second[0])->sh_info;
1292 RelData.w.b = ittr->second[0];
1295 return relocation_iterator(RelocationRef(RelData, this));
1298 template<support::endianness target_endianness, bool is64Bits>
1299 relocation_iterator ELFObjectFile<target_endianness, is64Bits>
1300 ::getSectionRelEnd(DataRefImpl Sec) const {
1301 DataRefImpl RelData;
1302 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1303 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1304 if (sec != 0 && ittr != SectionRelocMap.end()) {
1305 // Get the index of the last relocation section for this section.
1306 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
1307 const Elf_Shdr *relocsec = getSection(relocsecindex);
1308 RelData.w.a = relocsec->sh_info;
1309 RelData.w.b = relocsecindex;
1310 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
1312 return relocation_iterator(RelocationRef(RelData, this));
1316 template<support::endianness target_endianness, bool is64Bits>
1317 error_code ELFObjectFile<target_endianness, is64Bits>
1318 ::getRelocationNext(DataRefImpl Rel,
1319 RelocationRef &Result) const {
1321 const Elf_Shdr *relocsec = getSection(Rel.w.b);
1322 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
1323 // We have reached the end of the relocations for this section. See if there
1324 // is another relocation section.
1325 typename RelocMap_t::mapped_type relocseclist =
1326 SectionRelocMap.lookup(getSection(Rel.w.a));
1328 // Do a binary search for the current reloc section index (which must be
1329 // present). Then get the next one.
1330 typename RelocMap_t::mapped_type::const_iterator loc =
1331 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
1334 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
1335 // to the end iterator.
1336 if (loc != relocseclist.end()) {
1341 Result = RelocationRef(Rel, this);
1342 return object_error::success;
1345 template<support::endianness target_endianness, bool is64Bits>
1346 error_code ELFObjectFile<target_endianness, is64Bits>
1347 ::getRelocationSymbol(DataRefImpl Rel,
1348 SymbolRef &Result) const {
1350 const Elf_Shdr *sec = getSection(Rel.w.b);
1351 switch (sec->sh_type) {
1353 report_fatal_error("Invalid section type in Rel!");
1354 case ELF::SHT_REL : {
1355 symbolIdx = getRel(Rel)->getSymbol();
1358 case ELF::SHT_RELA : {
1359 symbolIdx = getRela(Rel)->getSymbol();
1363 DataRefImpl SymbolData;
1364 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
1365 if (it == SymbolTableSectionsIndexMap.end())
1366 report_fatal_error("Relocation symbol table not found!");
1367 SymbolData.d.a = symbolIdx;
1368 SymbolData.d.b = it->second;
1369 Result = SymbolRef(SymbolData, this);
1370 return object_error::success;
1373 template<support::endianness target_endianness, bool is64Bits>
1374 error_code ELFObjectFile<target_endianness, is64Bits>
1375 ::getRelocationAddress(DataRefImpl Rel,
1376 uint64_t &Result) const {
1378 const Elf_Shdr *sec = getSection(Rel.w.b);
1379 switch (sec->sh_type) {
1381 report_fatal_error("Invalid section type in Rel!");
1382 case ELF::SHT_REL : {
1383 offset = getRel(Rel)->r_offset;
1386 case ELF::SHT_RELA : {
1387 offset = getRela(Rel)->r_offset;
1393 return object_error::success;
1396 template<support::endianness target_endianness, bool is64Bits>
1397 error_code ELFObjectFile<target_endianness, is64Bits>
1398 ::getRelocationOffset(DataRefImpl Rel,
1399 uint64_t &Result) const {
1401 const Elf_Shdr *sec = getSection(Rel.w.b);
1402 switch (sec->sh_type) {
1404 report_fatal_error("Invalid section type in Rel!");
1405 case ELF::SHT_REL : {
1406 offset = getRel(Rel)->r_offset;
1409 case ELF::SHT_RELA : {
1410 offset = getRela(Rel)->r_offset;
1415 Result = offset - sec->sh_addr;
1416 return object_error::success;
1419 template<support::endianness target_endianness, bool is64Bits>
1420 error_code ELFObjectFile<target_endianness, is64Bits>
1421 ::getRelocationType(DataRefImpl Rel,
1422 uint64_t &Result) const {
1423 const Elf_Shdr *sec = getSection(Rel.w.b);
1424 switch (sec->sh_type) {
1426 report_fatal_error("Invalid section type in Rel!");
1427 case ELF::SHT_REL : {
1428 Result = getRel(Rel)->getType();
1431 case ELF::SHT_RELA : {
1432 Result = getRela(Rel)->getType();
1436 return object_error::success;
1439 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
1440 case ELF::enum: res = #enum; break;
1442 template<support::endianness target_endianness, bool is64Bits>
1443 error_code ELFObjectFile<target_endianness, is64Bits>
1444 ::getRelocationTypeName(DataRefImpl Rel,
1445 SmallVectorImpl<char> &Result) const {
1446 const Elf_Shdr *sec = getSection(Rel.w.b);
1449 switch (sec->sh_type) {
1451 return object_error::parse_failed;
1452 case ELF::SHT_REL : {
1453 type = getRel(Rel)->getType();
1456 case ELF::SHT_RELA : {
1457 type = getRela(Rel)->getType();
1461 switch (Header->e_machine) {
1462 case ELF::EM_X86_64:
1464 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
1465 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
1466 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
1467 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
1468 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
1469 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
1470 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
1471 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
1472 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
1473 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
1474 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
1475 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
1476 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
1477 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
1478 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
1479 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
1480 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
1481 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
1482 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
1483 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
1484 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
1485 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
1486 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
1487 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
1488 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
1489 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
1490 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
1491 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
1492 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
1493 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
1494 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
1495 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
1502 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
1503 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
1504 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
1505 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
1506 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
1507 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
1508 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
1509 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
1510 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
1511 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
1512 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
1513 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
1514 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
1515 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
1516 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
1517 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
1518 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
1519 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
1520 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
1521 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
1522 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
1523 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
1524 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
1525 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
1526 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
1527 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
1528 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
1529 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
1530 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
1531 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
1532 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
1533 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
1534 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
1535 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
1536 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
1537 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
1538 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
1539 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
1540 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
1541 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
1548 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_NONE);
1549 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PC24);
1550 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32);
1551 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32);
1552 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G0);
1553 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS16);
1554 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS12);
1555 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ABS5);
1556 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS8);
1557 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL32);
1558 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_CALL);
1559 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC8);
1560 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BREL_ADJ);
1561 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESC);
1562 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_SWI8);
1563 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_XPC25);
1564 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_XPC22);
1565 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPMOD32);
1566 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPOFF32);
1567 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_TPOFF32);
1568 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_COPY);
1569 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GLOB_DAT);
1570 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP_SLOT);
1571 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_RELATIVE);
1572 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF32);
1573 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_PREL);
1574 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL);
1575 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32);
1576 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_CALL);
1577 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP24);
1578 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP24);
1579 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_ABS);
1580 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_7_0);
1581 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_15_8);
1582 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_23_15);
1583 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SBREL_11_0_NC);
1584 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_19_12_NC);
1585 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_27_20_CK);
1586 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET1);
1587 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL31);
1588 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_V4BX);
1589 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET2);
1590 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PREL31);
1591 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_ABS_NC);
1592 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_ABS);
1593 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_PREL_NC);
1594 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_PREL);
1595 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_ABS_NC);
1596 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_ABS);
1597 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_PREL_NC);
1598 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_PREL);
1599 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP19);
1600 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP6);
1601 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ALU_PREL_11_0);
1602 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC12);
1603 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32_NOI);
1604 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32_NOI);
1605 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0_NC);
1606 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0);
1607 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1_NC);
1608 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1);
1609 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G2);
1610 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G1);
1611 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G2);
1612 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G0);
1613 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G1);
1614 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G2);
1615 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G0);
1616 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G1);
1617 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G2);
1618 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0_NC);
1619 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0);
1620 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1_NC);
1621 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1);
1622 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G2);
1623 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G0);
1624 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G1);
1625 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G2);
1626 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G0);
1627 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G1);
1628 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G2);
1629 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G0);
1630 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G1);
1631 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G2);
1632 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL_NC);
1633 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_BREL);
1634 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL);
1635 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL_NC);
1636 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_BREL);
1637 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL);
1638 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GOTDESC);
1639 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_CALL);
1640 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESCSEQ);
1641 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_CALL);
1642 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32_ABS);
1643 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_ABS);
1644 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_PREL);
1645 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL12);
1646 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF12);
1647 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTRELAX);
1648 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTENTRY);
1649 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTINHERIT);
1650 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP11);
1651 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP8);
1652 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GD32);
1653 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDM32);
1654 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO32);
1655 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE32);
1656 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE32);
1657 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO12);
1658 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE12);
1659 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE12GP);
1660 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_0);
1661 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_1);
1662 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_2);
1663 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_3);
1664 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_4);
1665 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_5);
1666 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_6);
1667 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_7);
1668 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_8);
1669 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_9);
1670 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_10);
1671 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_11);
1672 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_12);
1673 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_13);
1674 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_14);
1675 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_15);
1676 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ME_TOO);
1677 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ16);
1678 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ32);
1683 case ELF::EM_HEXAGON:
1685 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_NONE);
1686 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL);
1687 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL);
1688 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL);
1689 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_LO16);
1690 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HI16);
1691 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32);
1692 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16);
1693 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8);
1694 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_0);
1695 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_1);
1696 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_2);
1697 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_3);
1698 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HL16);
1699 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL);
1700 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL);
1701 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B32_PCREL_X);
1702 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_6_X);
1703 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL_X);
1704 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL_X);
1705 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL_X);
1706 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL_X);
1707 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL_X);
1708 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16_X);
1709 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_12_X);
1710 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_11_X);
1711 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_10_X);
1712 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_9_X);
1713 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8_X);
1714 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_7_X);
1715 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_X);
1716 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_PCREL);
1717 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_COPY);
1718 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GLOB_DAT);
1719 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_JMP_SLOT);
1720 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_RELATIVE);
1721 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_PLT_B22_PCREL);
1722 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_LO16);
1723 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_HI16);
1724 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32);
1725 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_LO16);
1726 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_HI16);
1727 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32);
1728 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16);
1729 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPMOD_32);
1730 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_LO16);
1731 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_HI16);
1732 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32);
1733 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16);
1734 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_PLT_B22_PCREL);
1735 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_LO16);
1736 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_HI16);
1737 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32);
1738 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16);
1739 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_LO16);
1740 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_HI16);
1741 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32);
1742 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_LO16);
1743 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_HI16);
1744 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32);
1745 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16);
1746 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_LO16);
1747 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_HI16);
1748 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32);
1749 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16);
1750 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_PCREL_X);
1751 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32_6_X);
1752 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_16_X);
1753 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_11_X);
1754 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32_6_X);
1755 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16_X);
1756 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_11_X);
1757 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32_6_X);
1758 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16_X);
1759 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_11_X);
1760 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32_6_X);
1761 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16_X);
1762 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_11_X);
1763 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32_6_X);
1764 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_16_X);
1765 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32_6_X);
1766 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16_X);
1767 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_11_X);
1768 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32_6_X);
1769 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16_X);
1770 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_11_X);
1778 Result.append(res.begin(), res.end());
1779 return object_error::success;
1782 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
1784 template<support::endianness target_endianness, bool is64Bits>
1785 error_code ELFObjectFile<target_endianness, is64Bits>
1786 ::getRelocationAdditionalInfo(DataRefImpl Rel,
1787 int64_t &Result) const {
1788 const Elf_Shdr *sec = getSection(Rel.w.b);
1789 switch (sec->sh_type) {
1791 report_fatal_error("Invalid section type in Rel!");
1792 case ELF::SHT_REL : {
1794 return object_error::success;
1796 case ELF::SHT_RELA : {
1797 Result = getRela(Rel)->r_addend;
1798 return object_error::success;
1803 template<support::endianness target_endianness, bool is64Bits>
1804 error_code ELFObjectFile<target_endianness, is64Bits>
1805 ::getRelocationValueString(DataRefImpl Rel,
1806 SmallVectorImpl<char> &Result) const {
1807 const Elf_Shdr *sec = getSection(Rel.w.b);
1811 uint16_t symbol_index = 0;
1812 switch (sec->sh_type) {
1814 return object_error::parse_failed;
1815 case ELF::SHT_REL: {
1816 type = getRel(Rel)->getType();
1817 symbol_index = getRel(Rel)->getSymbol();
1818 // TODO: Read implicit addend from section data.
1821 case ELF::SHT_RELA: {
1822 type = getRela(Rel)->getType();
1823 symbol_index = getRela(Rel)->getSymbol();
1824 addend = getRela(Rel)->r_addend;
1828 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1830 if (error_code ec = getSymbolName(getSection(sec->sh_link), symb, symname))
1832 switch (Header->e_machine) {
1833 case ELF::EM_X86_64:
1835 case ELF::R_X86_64_PC8:
1836 case ELF::R_X86_64_PC16:
1837 case ELF::R_X86_64_PC32: {
1839 raw_string_ostream fmt(fmtbuf);
1840 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1842 Result.append(fmtbuf.begin(), fmtbuf.end());
1845 case ELF::R_X86_64_8:
1846 case ELF::R_X86_64_16:
1847 case ELF::R_X86_64_32:
1848 case ELF::R_X86_64_32S:
1849 case ELF::R_X86_64_64: {
1851 raw_string_ostream fmt(fmtbuf);
1852 fmt << symname << (addend < 0 ? "" : "+") << addend;
1854 Result.append(fmtbuf.begin(), fmtbuf.end());
1862 case ELF::EM_HEXAGON:
1869 Result.append(res.begin(), res.end());
1870 return object_error::success;
1873 // Verify that the last byte in the string table in a null.
1874 template<support::endianness target_endianness, bool is64Bits>
1875 void ELFObjectFile<target_endianness, is64Bits>
1876 ::VerifyStrTab(const Elf_Shdr *sh) const {
1877 const char *strtab = (const char*)base() + sh->sh_offset;
1878 if (strtab[sh->sh_size - 1] != 0)
1879 // FIXME: Proper error handling.
1880 report_fatal_error("String table must end with a null terminator!");
1883 template<support::endianness target_endianness, bool is64Bits>
1884 ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object
1886 : ObjectFile(getELFType(target_endianness == support::little, is64Bits),
1888 , isDyldELFObject(false)
1889 , SectionHeaderTable(0)
1890 , dot_shstrtab_sec(0)
1893 , dot_dynamic_sec(0)
1894 , dot_gnu_version_sec(0)
1895 , dot_gnu_version_r_sec(0)
1896 , dot_gnu_version_d_sec(0)
1900 const uint64_t FileSize = Data->getBufferSize();
1902 if (sizeof(Elf_Ehdr) > FileSize)
1903 // FIXME: Proper error handling.
1904 report_fatal_error("File too short!");
1906 Header = reinterpret_cast<const Elf_Ehdr *>(base());
1908 if (Header->e_shoff == 0)
1911 const uint64_t SectionTableOffset = Header->e_shoff;
1913 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize)
1914 // FIXME: Proper error handling.
1915 report_fatal_error("Section header table goes past end of file!");
1917 // The getNumSections() call below depends on SectionHeaderTable being set.
1918 SectionHeaderTable =
1919 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
1920 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
1922 if (SectionTableOffset + SectionTableSize > FileSize)
1923 // FIXME: Proper error handling.
1924 report_fatal_error("Section table goes past end of file!");
1926 // To find the symbol tables we walk the section table to find SHT_SYMTAB.
1927 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
1928 const Elf_Shdr* sh = SectionHeaderTable;
1930 // Reserve SymbolTableSections[0] for .dynsym
1931 SymbolTableSections.push_back(NULL);
1933 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
1934 switch (sh->sh_type) {
1935 case ELF::SHT_SYMTAB_SHNDX: {
1936 if (SymbolTableSectionHeaderIndex)
1937 // FIXME: Proper error handling.
1938 report_fatal_error("More than one .symtab_shndx!");
1939 SymbolTableSectionHeaderIndex = sh;
1942 case ELF::SHT_SYMTAB: {
1943 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
1944 SymbolTableSections.push_back(sh);
1947 case ELF::SHT_DYNSYM: {
1948 if (SymbolTableSections[0] != NULL)
1949 // FIXME: Proper error handling.
1950 report_fatal_error("More than one .dynsym!");
1951 SymbolTableSectionsIndexMap[i] = 0;
1952 SymbolTableSections[0] = sh;
1956 case ELF::SHT_RELA: {
1957 SectionRelocMap[getSection(sh->sh_info)].push_back(i);
1960 case ELF::SHT_DYNAMIC: {
1961 if (dot_dynamic_sec != NULL)
1962 // FIXME: Proper error handling.
1963 report_fatal_error("More than one .dynamic!");
1964 dot_dynamic_sec = sh;
1967 case ELF::SHT_GNU_versym: {
1968 if (dot_gnu_version_sec != NULL)
1969 // FIXME: Proper error handling.
1970 report_fatal_error("More than one .gnu.version section!");
1971 dot_gnu_version_sec = sh;
1974 case ELF::SHT_GNU_verdef: {
1975 if (dot_gnu_version_d_sec != NULL)
1976 // FIXME: Proper error handling.
1977 report_fatal_error("More than one .gnu.version_d section!");
1978 dot_gnu_version_d_sec = sh;
1981 case ELF::SHT_GNU_verneed: {
1982 if (dot_gnu_version_r_sec != NULL)
1983 // FIXME: Proper error handling.
1984 report_fatal_error("More than one .gnu.version_r section!");
1985 dot_gnu_version_r_sec = sh;
1992 // Sort section relocation lists by index.
1993 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
1994 e = SectionRelocMap.end(); i != e; ++i) {
1995 std::sort(i->second.begin(), i->second.end());
1998 // Get string table sections.
1999 dot_shstrtab_sec = getSection(getStringTableIndex());
2000 if (dot_shstrtab_sec) {
2001 // Verify that the last byte in the string table in a null.
2002 VerifyStrTab(dot_shstrtab_sec);
2005 // Merge this into the above loop.
2006 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
2007 *e = i + getNumSections() * Header->e_shentsize;
2008 i != e; i += Header->e_shentsize) {
2009 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
2010 if (sh->sh_type == ELF::SHT_STRTAB) {
2011 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
2012 if (SectionName == ".strtab") {
2013 if (dot_strtab_sec != 0)
2014 // FIXME: Proper error handling.
2015 report_fatal_error("Already found section named .strtab!");
2016 dot_strtab_sec = sh;
2017 VerifyStrTab(dot_strtab_sec);
2018 } else if (SectionName == ".dynstr") {
2019 if (dot_dynstr_sec != 0)
2020 // FIXME: Proper error handling.
2021 report_fatal_error("Already found section named .dynstr!");
2022 dot_dynstr_sec = sh;
2023 VerifyStrTab(dot_dynstr_sec);
2028 // Build symbol name side-mapping if there is one.
2029 if (SymbolTableSectionHeaderIndex) {
2030 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
2031 SymbolTableSectionHeaderIndex->sh_offset);
2033 for (symbol_iterator si = begin_symbols(),
2034 se = end_symbols(); si != se; si.increment(ec)) {
2036 report_fatal_error("Fewer extended symbol table entries than symbols!");
2037 if (*ShndxTable != ELF::SHN_UNDEF)
2038 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
2044 template<support::endianness target_endianness, bool is64Bits>
2045 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
2046 ::begin_symbols() const {
2047 DataRefImpl SymbolData;
2048 if (SymbolTableSections.size() <= 1) {
2049 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2050 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2052 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2053 SymbolData.d.b = 1; // The 0th table is .dynsym
2055 return symbol_iterator(SymbolRef(SymbolData, this));
2058 template<support::endianness target_endianness, bool is64Bits>
2059 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
2060 ::end_symbols() const {
2061 DataRefImpl SymbolData;
2062 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2063 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2064 return symbol_iterator(SymbolRef(SymbolData, this));
2067 template<support::endianness target_endianness, bool is64Bits>
2068 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
2069 ::begin_dynamic_symbols() const {
2070 DataRefImpl SymbolData;
2071 if (SymbolTableSections[0] == NULL) {
2072 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2073 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2075 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2076 SymbolData.d.b = 0; // The 0th table is .dynsym
2078 return symbol_iterator(SymbolRef(SymbolData, this));
2081 template<support::endianness target_endianness, bool is64Bits>
2082 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
2083 ::end_dynamic_symbols() const {
2084 DataRefImpl SymbolData;
2085 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2086 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2087 return symbol_iterator(SymbolRef(SymbolData, this));
2090 template<support::endianness target_endianness, bool is64Bits>
2091 section_iterator ELFObjectFile<target_endianness, is64Bits>
2092 ::begin_sections() const {
2094 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
2095 return section_iterator(SectionRef(ret, this));
2098 template<support::endianness target_endianness, bool is64Bits>
2099 section_iterator ELFObjectFile<target_endianness, is64Bits>
2100 ::end_sections() const {
2102 ret.p = reinterpret_cast<intptr_t>(base()
2104 + (Header->e_shentsize*getNumSections()));
2105 return section_iterator(SectionRef(ret, this));
2108 template<support::endianness target_endianness, bool is64Bits>
2109 typename ELFObjectFile<target_endianness, is64Bits>::dyn_iterator
2110 ELFObjectFile<target_endianness, is64Bits>::begin_dynamic_table() const {
2111 DataRefImpl DynData;
2112 if (dot_dynamic_sec == NULL || dot_dynamic_sec->sh_size == 0) {
2113 DynData.d.a = std::numeric_limits<uint32_t>::max();
2117 return dyn_iterator(DynRef(DynData, this));
2120 template<support::endianness target_endianness, bool is64Bits>
2121 typename ELFObjectFile<target_endianness, is64Bits>::dyn_iterator
2122 ELFObjectFile<target_endianness, is64Bits>
2123 ::end_dynamic_table() const {
2124 DataRefImpl DynData;
2125 DynData.d.a = std::numeric_limits<uint32_t>::max();
2126 return dyn_iterator(DynRef(DynData, this));
2129 template<support::endianness target_endianness, bool is64Bits>
2130 error_code ELFObjectFile<target_endianness, is64Bits>
2131 ::getDynNext(DataRefImpl DynData,
2132 DynRef &Result) const {
2135 // Check to see if we are at the end of .dynamic
2136 if (DynData.d.a >= dot_dynamic_sec->getEntityCount()) {
2137 // We are at the end. Return the terminator.
2138 DynData.d.a = std::numeric_limits<uint32_t>::max();
2141 Result = DynRef(DynData, this);
2142 return object_error::success;
2145 template<support::endianness target_endianness, bool is64Bits>
2147 ELFObjectFile<target_endianness, is64Bits>::getLoadName() const {
2149 // Find the DT_SONAME entry
2150 dyn_iterator it = begin_dynamic_table();
2151 dyn_iterator ie = end_dynamic_table();
2154 if (it->getTag() == ELF::DT_SONAME)
2158 report_fatal_error("dynamic table iteration failed");
2161 if (dot_dynstr_sec == NULL)
2162 report_fatal_error("Dynamic string table is missing");
2163 dt_soname = getString(dot_dynstr_sec, it->getVal());
2171 template<support::endianness target_endianness, bool is64Bits>
2172 library_iterator ELFObjectFile<target_endianness, is64Bits>
2173 ::begin_libraries_needed() const {
2174 // Find the first DT_NEEDED entry
2175 dyn_iterator i = begin_dynamic_table();
2176 dyn_iterator e = end_dynamic_table();
2179 if (i->getTag() == ELF::DT_NEEDED)
2183 report_fatal_error("dynamic table iteration failed");
2185 // Use the same DataRefImpl format as DynRef.
2186 return library_iterator(LibraryRef(i->getRawDataRefImpl(), this));
2189 template<support::endianness target_endianness, bool is64Bits>
2190 error_code ELFObjectFile<target_endianness, is64Bits>
2191 ::getLibraryNext(DataRefImpl Data,
2192 LibraryRef &Result) const {
2193 // Use the same DataRefImpl format as DynRef.
2194 dyn_iterator i = dyn_iterator(DynRef(Data, this));
2195 dyn_iterator e = end_dynamic_table();
2197 // Skip the current dynamic table entry.
2201 // TODO: proper error handling
2203 report_fatal_error("dynamic table iteration failed");
2206 // Find the next DT_NEEDED entry.
2208 if (i->getTag() == ELF::DT_NEEDED)
2212 report_fatal_error("dynamic table iteration failed");
2214 Result = LibraryRef(i->getRawDataRefImpl(), this);
2215 return object_error::success;
2218 template<support::endianness target_endianness, bool is64Bits>
2219 error_code ELFObjectFile<target_endianness, is64Bits>
2220 ::getLibraryPath(DataRefImpl Data, StringRef &Res) const {
2221 dyn_iterator i = dyn_iterator(DynRef(Data, this));
2222 if (i == end_dynamic_table())
2223 report_fatal_error("getLibraryPath() called on iterator end");
2225 if (i->getTag() != ELF::DT_NEEDED)
2226 report_fatal_error("Invalid library_iterator");
2228 // This uses .dynstr to lookup the name of the DT_NEEDED entry.
2229 // THis works as long as DT_STRTAB == .dynstr. This is true most of
2230 // the time, but the specification allows exceptions.
2231 // TODO: This should really use DT_STRTAB instead. Doing this requires
2232 // reading the program headers.
2233 if (dot_dynstr_sec == NULL)
2234 report_fatal_error("Dynamic string table is missing");
2235 Res = getString(dot_dynstr_sec, i->getVal());
2236 return object_error::success;
2239 template<support::endianness target_endianness, bool is64Bits>
2240 library_iterator ELFObjectFile<target_endianness, is64Bits>
2241 ::end_libraries_needed() const {
2242 dyn_iterator e = end_dynamic_table();
2243 // Use the same DataRefImpl format as DynRef.
2244 return library_iterator(LibraryRef(e->getRawDataRefImpl(), this));
2247 template<support::endianness target_endianness, bool is64Bits>
2248 uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const {
2249 return is64Bits ? 8 : 4;
2252 template<support::endianness target_endianness, bool is64Bits>
2253 StringRef ELFObjectFile<target_endianness, is64Bits>
2254 ::getFileFormatName() const {
2255 switch(Header->e_ident[ELF::EI_CLASS]) {
2256 case ELF::ELFCLASS32:
2257 switch(Header->e_machine) {
2259 return "ELF32-i386";
2260 case ELF::EM_X86_64:
2261 return "ELF32-x86-64";
2264 case ELF::EM_HEXAGON:
2265 return "ELF32-hexagon";
2267 return "ELF32-unknown";
2269 case ELF::ELFCLASS64:
2270 switch(Header->e_machine) {
2272 return "ELF64-i386";
2273 case ELF::EM_X86_64:
2274 return "ELF64-x86-64";
2276 return "ELF64-unknown";
2279 // FIXME: Proper error handling.
2280 report_fatal_error("Invalid ELFCLASS!");
2284 template<support::endianness target_endianness, bool is64Bits>
2285 unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const {
2286 switch(Header->e_machine) {
2289 case ELF::EM_X86_64:
2290 return Triple::x86_64;
2293 case ELF::EM_HEXAGON:
2294 return Triple::hexagon;
2296 return (target_endianness == support::little) ?
2297 Triple::mipsel : Triple::mips;
2299 return Triple::UnknownArch;
2303 template<support::endianness target_endianness, bool is64Bits>
2304 uint64_t ELFObjectFile<target_endianness, is64Bits>::getNumSections() const {
2305 assert(Header && "Header not initialized!");
2306 if (Header->e_shnum == ELF::SHN_UNDEF) {
2307 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
2308 return SectionHeaderTable->sh_size;
2310 return Header->e_shnum;
2313 template<support::endianness target_endianness, bool is64Bits>
2315 ELFObjectFile<target_endianness, is64Bits>::getStringTableIndex() const {
2316 if (Header->e_shnum == ELF::SHN_UNDEF) {
2317 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
2318 return SectionHeaderTable->sh_link;
2319 if (Header->e_shstrndx >= getNumSections())
2322 return Header->e_shstrndx;
2326 template<support::endianness target_endianness, bool is64Bits>
2327 template<typename T>
2329 ELFObjectFile<target_endianness, is64Bits>::getEntry(uint16_t Section,
2330 uint32_t Entry) const {
2331 return getEntry<T>(getSection(Section), Entry);
2334 template<support::endianness target_endianness, bool is64Bits>
2335 template<typename T>
2337 ELFObjectFile<target_endianness, is64Bits>::getEntry(const Elf_Shdr * Section,
2338 uint32_t Entry) const {
2339 return reinterpret_cast<const T *>(
2341 + Section->sh_offset
2342 + (Entry * Section->sh_entsize));
2345 template<support::endianness target_endianness, bool is64Bits>
2346 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym *
2347 ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const {
2348 return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
2351 template<support::endianness target_endianness, bool is64Bits>
2352 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Dyn *
2353 ELFObjectFile<target_endianness, is64Bits>::getDyn(DataRefImpl DynData) const {
2354 return getEntry<Elf_Dyn>(dot_dynamic_sec, DynData.d.a);
2357 template<support::endianness target_endianness, bool is64Bits>
2358 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel *
2359 ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const {
2360 return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
2363 template<support::endianness target_endianness, bool is64Bits>
2364 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela *
2365 ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const {
2366 return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
2369 template<support::endianness target_endianness, bool is64Bits>
2370 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
2371 ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const {
2372 const Elf_Shdr *sec = getSection(Symb.d.b);
2373 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
2374 // FIXME: Proper error handling.
2375 report_fatal_error("Invalid symbol table section!");
2379 template<support::endianness target_endianness, bool is64Bits>
2380 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
2381 ELFObjectFile<target_endianness, is64Bits>::getSection(uint32_t index) const {
2384 if (!SectionHeaderTable || index >= getNumSections())
2385 // FIXME: Proper error handling.
2386 report_fatal_error("Invalid section index!");
2388 return reinterpret_cast<const Elf_Shdr *>(
2389 reinterpret_cast<const char *>(SectionHeaderTable)
2390 + (index * Header->e_shentsize));
2393 template<support::endianness target_endianness, bool is64Bits>
2394 const char *ELFObjectFile<target_endianness, is64Bits>
2395 ::getString(uint32_t section,
2396 ELF::Elf32_Word offset) const {
2397 return getString(getSection(section), offset);
2400 template<support::endianness target_endianness, bool is64Bits>
2401 const char *ELFObjectFile<target_endianness, is64Bits>
2402 ::getString(const Elf_Shdr *section,
2403 ELF::Elf32_Word offset) const {
2404 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
2405 if (offset >= section->sh_size)
2406 // FIXME: Proper error handling.
2407 report_fatal_error("Symbol name offset outside of string table!");
2408 return (const char *)base() + section->sh_offset + offset;
2411 template<support::endianness target_endianness, bool is64Bits>
2412 error_code ELFObjectFile<target_endianness, is64Bits>
2413 ::getSymbolName(const Elf_Shdr *section,
2414 const Elf_Sym *symb,
2415 StringRef &Result) const {
2416 if (symb->st_name == 0) {
2417 const Elf_Shdr *section = getSection(symb);
2421 Result = getString(dot_shstrtab_sec, section->sh_name);
2422 return object_error::success;
2425 if (section == SymbolTableSections[0]) {
2426 // Symbol is in .dynsym, use .dynstr string table
2427 Result = getString(dot_dynstr_sec, symb->st_name);
2429 // Use the default symbol table name section.
2430 Result = getString(dot_strtab_sec, symb->st_name);
2432 return object_error::success;
2435 template<support::endianness target_endianness, bool is64Bits>
2436 error_code ELFObjectFile<target_endianness, is64Bits>
2437 ::getSectionName(const Elf_Shdr *section,
2438 StringRef &Result) const {
2439 Result = StringRef(getString(dot_shstrtab_sec, section->sh_name));
2440 return object_error::success;
2443 template<support::endianness target_endianness, bool is64Bits>
2444 error_code ELFObjectFile<target_endianness, is64Bits>
2445 ::getSymbolVersion(const Elf_Shdr *section,
2446 const Elf_Sym *symb,
2448 bool &IsDefault) const {
2449 // Handle non-dynamic symbols.
2450 if (section != SymbolTableSections[0]) {
2451 // Non-dynamic symbols can have versions in their names
2452 // A name of the form 'foo@V1' indicates version 'V1', non-default.
2453 // A name of the form 'foo@@V2' indicates version 'V2', default version.
2455 error_code ec = getSymbolName(section, symb, Name);
2456 if (ec != object_error::success)
2458 size_t atpos = Name.find('@');
2459 if (atpos == StringRef::npos) {
2462 return object_error::success;
2465 if (atpos < Name.size() && Name[atpos] == '@') {
2471 Version = Name.substr(atpos);
2472 return object_error::success;
2475 // This is a dynamic symbol. Look in the GNU symbol version table.
2476 if (dot_gnu_version_sec == NULL) {
2477 // No version table.
2480 return object_error::success;
2483 // Determine the position in the symbol table of this entry.
2484 const char *sec_start = (const char*)base() + section->sh_offset;
2485 size_t entry_index = ((const char*)symb - sec_start)/section->sh_entsize;
2487 // Get the corresponding version index entry
2488 const Elf_Versym *vs = getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index);
2489 size_t version_index = vs->vs_index & ELF::VERSYM_VERSION;
2491 // Special markers for unversioned symbols.
2492 if (version_index == ELF::VER_NDX_LOCAL ||
2493 version_index == ELF::VER_NDX_GLOBAL) {
2496 return object_error::success;
2499 // Lookup this symbol in the version table
2501 if (version_index >= VersionMap.size() || VersionMap[version_index].isNull())
2502 report_fatal_error("Symbol has version index without corresponding "
2503 "define or reference entry");
2504 const VersionMapEntry &entry = VersionMap[version_index];
2506 // Get the version name string
2508 if (entry.isVerdef()) {
2509 // The first Verdaux entry holds the name.
2510 name_offset = entry.getVerdef()->getAux()->vda_name;
2512 name_offset = entry.getVernaux()->vna_name;
2514 Version = getString(dot_dynstr_sec, name_offset);
2517 if (entry.isVerdef()) {
2518 IsDefault = !(vs->vs_index & ELF::VERSYM_HIDDEN);
2523 return object_error::success;
2526 template<support::endianness target_endianness, bool is64Bits>
2527 inline DynRefImpl<target_endianness, is64Bits>
2528 ::DynRefImpl(DataRefImpl DynP, const OwningType *Owner)
2530 , OwningObject(Owner) {}
2532 template<support::endianness target_endianness, bool is64Bits>
2533 inline bool DynRefImpl<target_endianness, is64Bits>
2534 ::operator==(const DynRefImpl &Other) const {
2535 return DynPimpl == Other.DynPimpl;
2538 template<support::endianness target_endianness, bool is64Bits>
2539 inline bool DynRefImpl<target_endianness, is64Bits>
2540 ::operator <(const DynRefImpl &Other) const {
2541 return DynPimpl < Other.DynPimpl;
2544 template<support::endianness target_endianness, bool is64Bits>
2545 inline error_code DynRefImpl<target_endianness, is64Bits>
2546 ::getNext(DynRefImpl &Result) const {
2547 return OwningObject->getDynNext(DynPimpl, Result);
2550 template<support::endianness target_endianness, bool is64Bits>
2551 inline int64_t DynRefImpl<target_endianness, is64Bits>
2553 return OwningObject->getDyn(DynPimpl)->d_tag;
2556 template<support::endianness target_endianness, bool is64Bits>
2557 inline uint64_t DynRefImpl<target_endianness, is64Bits>
2559 return OwningObject->getDyn(DynPimpl)->d_un.d_val;
2562 template<support::endianness target_endianness, bool is64Bits>
2563 inline uint64_t DynRefImpl<target_endianness, is64Bits>
2565 return OwningObject->getDyn(DynPimpl)->d_un.d_ptr;
2568 template<support::endianness target_endianness, bool is64Bits>
2569 inline DataRefImpl DynRefImpl<target_endianness, is64Bits>
2570 ::getRawDataRefImpl() const {
2574 /// This is a generic interface for retrieving GNU symbol version
2575 /// information from an ELFObjectFile.
2576 static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
2577 const SymbolRef &Sym,
2580 // Little-endian 32-bit
2581 if (const ELFObjectFile<support::little, false> *ELFObj =
2582 dyn_cast<ELFObjectFile<support::little, false> >(Obj))
2583 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2585 // Big-endian 32-bit
2586 if (const ELFObjectFile<support::big, false> *ELFObj =
2587 dyn_cast<ELFObjectFile<support::big, false> >(Obj))
2588 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2590 // Little-endian 64-bit
2591 if (const ELFObjectFile<support::little, true> *ELFObj =
2592 dyn_cast<ELFObjectFile<support::little, true> >(Obj))
2593 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2595 // Big-endian 64-bit
2596 if (const ELFObjectFile<support::big, true> *ELFObj =
2597 dyn_cast<ELFObjectFile<support::big, true> >(Obj))
2598 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2600 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");