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 // Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
37 template<support::endianness target_endianness>
38 struct ELFDataTypeTypedefHelperCommon {
39 typedef support::detail::packed_endian_specific_integral
40 <uint16_t, target_endianness, support::aligned> Elf_Half;
41 typedef support::detail::packed_endian_specific_integral
42 <uint32_t, target_endianness, support::aligned> Elf_Word;
43 typedef support::detail::packed_endian_specific_integral
44 <int32_t, target_endianness, support::aligned> Elf_Sword;
45 typedef support::detail::packed_endian_specific_integral
46 <uint64_t, target_endianness, support::aligned> Elf_Xword;
47 typedef support::detail::packed_endian_specific_integral
48 <int64_t, target_endianness, support::aligned> Elf_Sxword;
51 template<support::endianness target_endianness, bool is64Bits>
52 struct ELFDataTypeTypedefHelper;
55 template<support::endianness target_endianness>
56 struct ELFDataTypeTypedefHelper<target_endianness, false>
57 : ELFDataTypeTypedefHelperCommon<target_endianness> {
58 typedef uint32_t value_type;
59 typedef support::detail::packed_endian_specific_integral
60 <value_type, target_endianness, support::aligned> Elf_Addr;
61 typedef support::detail::packed_endian_specific_integral
62 <value_type, target_endianness, support::aligned> Elf_Off;
66 template<support::endianness target_endianness>
67 struct ELFDataTypeTypedefHelper<target_endianness, true>
68 : ELFDataTypeTypedefHelperCommon<target_endianness>{
69 typedef uint64_t value_type;
70 typedef support::detail::packed_endian_specific_integral
71 <value_type, target_endianness, support::aligned> Elf_Addr;
72 typedef support::detail::packed_endian_specific_integral
73 <value_type, target_endianness, support::aligned> Elf_Off;
76 // I really don't like doing this, but the alternative is copypasta.
77 #define LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) \
79 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Addr Elf_Addr; \
81 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Off Elf_Off; \
83 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Half Elf_Half; \
85 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Word Elf_Word; \
87 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sword Elf_Sword; \
89 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Xword Elf_Xword; \
91 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sxword Elf_Sxword;
94 template<support::endianness target_endianness, bool is64Bits>
97 template<support::endianness target_endianness>
98 struct Elf_Shdr_Base<target_endianness, false> {
99 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
100 Elf_Word sh_name; // Section name (index into string table)
101 Elf_Word sh_type; // Section type (SHT_*)
102 Elf_Word sh_flags; // Section flags (SHF_*)
103 Elf_Addr sh_addr; // Address where section is to be loaded
104 Elf_Off sh_offset; // File offset of section data, in bytes
105 Elf_Word sh_size; // Size of section, in bytes
106 Elf_Word sh_link; // Section type-specific header table index link
107 Elf_Word sh_info; // Section type-specific extra information
108 Elf_Word sh_addralign;// Section address alignment
109 Elf_Word sh_entsize; // Size of records contained within the section
112 template<support::endianness target_endianness>
113 struct Elf_Shdr_Base<target_endianness, true> {
114 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
115 Elf_Word sh_name; // Section name (index into string table)
116 Elf_Word sh_type; // Section type (SHT_*)
117 Elf_Xword sh_flags; // Section flags (SHF_*)
118 Elf_Addr sh_addr; // Address where section is to be loaded
119 Elf_Off sh_offset; // File offset of section data, in bytes
120 Elf_Xword sh_size; // Size of section, in bytes
121 Elf_Word sh_link; // Section type-specific header table index link
122 Elf_Word sh_info; // Section type-specific extra information
123 Elf_Xword sh_addralign;// Section address alignment
124 Elf_Xword sh_entsize; // Size of records contained within the section
127 template<support::endianness target_endianness, bool is64Bits>
128 struct Elf_Shdr_Impl : Elf_Shdr_Base<target_endianness, is64Bits> {
129 using Elf_Shdr_Base<target_endianness, is64Bits>::sh_entsize;
130 using Elf_Shdr_Base<target_endianness, is64Bits>::sh_size;
132 /// @brief Get the number of entities this section contains if it has any.
133 unsigned getEntityCount() const {
136 return sh_size / sh_entsize;
140 template<support::endianness target_endianness, bool is64Bits>
143 template<support::endianness target_endianness>
144 struct Elf_Sym_Base<target_endianness, false> {
145 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
146 Elf_Word st_name; // Symbol name (index into string table)
147 Elf_Addr st_value; // Value or address associated with the symbol
148 Elf_Word st_size; // Size of the symbol
149 unsigned char st_info; // Symbol's type and binding attributes
150 unsigned char st_other; // Must be zero; reserved
151 Elf_Half st_shndx; // Which section (header table index) it's defined in
154 template<support::endianness target_endianness>
155 struct Elf_Sym_Base<target_endianness, true> {
156 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
157 Elf_Word st_name; // Symbol name (index into string table)
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
161 Elf_Addr st_value; // Value or address associated with the symbol
162 Elf_Xword st_size; // Size of the symbol
165 template<support::endianness target_endianness, bool is64Bits>
166 struct Elf_Sym_Impl : Elf_Sym_Base<target_endianness, is64Bits> {
167 using Elf_Sym_Base<target_endianness, is64Bits>::st_info;
169 // These accessors and mutators correspond to the ELF32_ST_BIND,
170 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
171 unsigned char getBinding() const { return st_info >> 4; }
172 unsigned char getType() const { return st_info & 0x0f; }
173 void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
174 void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
175 void setBindingAndType(unsigned char b, unsigned char t) {
176 st_info = (b << 4) + (t & 0x0f);
180 /// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section
181 /// (.gnu.version). This structure is identical for ELF32 and ELF64.
182 template<support::endianness target_endianness, bool is64Bits>
183 struct Elf_Versym_Impl {
184 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
185 Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN)
188 template<support::endianness target_endianness, bool is64Bits>
189 struct Elf_Verdaux_Impl;
191 /// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section
192 /// (.gnu.version_d). This structure is identical for ELF32 and ELF64.
193 template<support::endianness target_endianness, bool is64Bits>
194 struct Elf_Verdef_Impl {
195 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
196 typedef Elf_Verdaux_Impl<target_endianness, is64Bits> Elf_Verdaux;
197 Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT)
198 Elf_Half vd_flags; // Bitwise flags (VER_DEF_*)
199 Elf_Half vd_ndx; // Version index, used in .gnu.version entries
200 Elf_Half vd_cnt; // Number of Verdaux entries
201 Elf_Word vd_hash; // Hash of name
202 Elf_Word vd_aux; // Offset to the first Verdaux entry (in bytes)
203 Elf_Word vd_next; // Offset to the next Verdef entry (in bytes)
205 /// Get the first Verdaux entry for this Verdef.
206 const Elf_Verdaux *getAux() const {
207 return reinterpret_cast<const Elf_Verdaux*>((const char*)this + vd_aux);
211 /// Elf_Verdaux: This is the structure of auxilary data in the SHT_GNU_verdef
212 /// section (.gnu.version_d). This structure is identical for ELF32 and ELF64.
213 template<support::endianness target_endianness, bool is64Bits>
214 struct Elf_Verdaux_Impl {
215 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
216 Elf_Word vda_name; // Version name (offset in string table)
217 Elf_Word vda_next; // Offset to next Verdaux entry (in bytes)
220 /// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed
221 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
222 template<support::endianness target_endianness, bool is64Bits>
223 struct Elf_Verneed_Impl {
224 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
225 Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT)
226 Elf_Half vn_cnt; // Number of associated Vernaux entries
227 Elf_Word vn_file; // Library name (string table offset)
228 Elf_Word vn_aux; // Offset to first Vernaux entry (in bytes)
229 Elf_Word vn_next; // Offset to next Verneed entry (in bytes)
232 /// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed
233 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
234 template<support::endianness target_endianness, bool is64Bits>
235 struct Elf_Vernaux_Impl {
236 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
237 Elf_Word vna_hash; // Hash of dependency name
238 Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*)
239 Elf_Half vna_other; // Version index, used in .gnu.version entries
240 Elf_Word vna_name; // Dependency name
241 Elf_Word vna_next; // Offset to next Vernaux entry (in bytes)
244 /// Elf_Dyn_Base: This structure matches the form of entries in the dynamic
245 /// table section (.dynamic) look like.
246 template<support::endianness target_endianness, bool is64Bits>
249 template<support::endianness target_endianness>
250 struct Elf_Dyn_Base<target_endianness, false> {
251 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
259 template<support::endianness target_endianness>
260 struct Elf_Dyn_Base<target_endianness, true> {
261 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
269 /// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters.
270 template<support::endianness target_endianness, bool is64Bits>
271 struct Elf_Dyn_Impl : Elf_Dyn_Base<target_endianness, is64Bits> {
272 using Elf_Dyn_Base<target_endianness, is64Bits>::d_tag;
273 using Elf_Dyn_Base<target_endianness, is64Bits>::d_un;
274 int64_t getTag() const { return d_tag; }
275 uint64_t getVal() const { return d_un.d_val; }
276 uint64_t getPtr() const { return d_un.ptr; }
279 template<support::endianness target_endianness, bool is64Bits>
282 // DynRefImpl: Reference to an entry in the dynamic table
283 // This is an ELF-specific interface.
284 template<support::endianness target_endianness, bool is64Bits>
286 typedef Elf_Dyn_Impl<target_endianness, is64Bits> Elf_Dyn;
287 typedef ELFObjectFile<target_endianness, is64Bits> OwningType;
289 DataRefImpl DynPimpl;
290 const OwningType *OwningObject;
293 DynRefImpl() : OwningObject(NULL) {
294 std::memset(&DynPimpl, 0, sizeof(DynPimpl));
297 DynRefImpl(DataRefImpl DynP, const OwningType *Owner);
299 bool operator==(const DynRefImpl &Other) const;
300 bool operator <(const DynRefImpl &Other) const;
302 error_code getNext(DynRefImpl &Result) const;
303 int64_t getTag() const;
304 uint64_t getVal() const;
305 uint64_t getPtr() const;
307 DataRefImpl getRawDataRefImpl() const;
310 // Elf_Rel: Elf Relocation
311 template<support::endianness target_endianness, bool is64Bits, bool isRela>
314 template<support::endianness target_endianness>
315 struct Elf_Rel_Base<target_endianness, false, false> {
316 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
317 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
318 Elf_Word r_info; // Symbol table index and type of relocation to apply
321 template<support::endianness target_endianness>
322 struct Elf_Rel_Base<target_endianness, true, false> {
323 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
324 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
325 Elf_Xword r_info; // Symbol table index and type of relocation to apply
328 template<support::endianness target_endianness>
329 struct Elf_Rel_Base<target_endianness, false, true> {
330 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
331 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
332 Elf_Word r_info; // Symbol table index and type of relocation to apply
333 Elf_Sword r_addend; // Compute value for relocatable field by adding this
336 template<support::endianness target_endianness>
337 struct Elf_Rel_Base<target_endianness, true, true> {
338 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
339 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
340 Elf_Xword r_info; // Symbol table index and type of relocation to apply
341 Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
344 template<support::endianness target_endianness, bool is64Bits, bool isRela>
347 template<support::endianness target_endianness, bool isRela>
348 struct Elf_Rel_Impl<target_endianness, true, isRela>
349 : Elf_Rel_Base<target_endianness, true, isRela> {
350 using Elf_Rel_Base<target_endianness, true, isRela>::r_info;
351 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
353 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
354 // and ELF64_R_INFO macros defined in the ELF specification:
355 uint64_t getSymbol() const { return (r_info >> 32); }
356 unsigned char getType() const {
357 return (unsigned char) (r_info & 0xffffffffL);
359 void setSymbol(uint64_t s) { setSymbolAndType(s, getType()); }
360 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
361 void setSymbolAndType(uint64_t s, unsigned char t) {
362 r_info = (s << 32) + (t&0xffffffffL);
366 template<support::endianness target_endianness, bool isRela>
367 struct Elf_Rel_Impl<target_endianness, false, isRela>
368 : Elf_Rel_Base<target_endianness, false, isRela> {
369 using Elf_Rel_Base<target_endianness, false, isRela>::r_info;
370 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
372 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
373 // and ELF32_R_INFO macros defined in the ELF specification:
374 uint32_t getSymbol() const { return (r_info >> 8); }
375 unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); }
376 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); }
377 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
378 void setSymbolAndType(uint32_t s, unsigned char t) {
379 r_info = (s << 8) + t;
384 template<support::endianness target_endianness, bool is64Bits>
385 class ELFObjectFile : public ObjectFile {
386 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
388 typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
389 typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
390 typedef Elf_Dyn_Impl<target_endianness, is64Bits> Elf_Dyn;
391 typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel;
392 typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela;
393 typedef Elf_Verdef_Impl<target_endianness, is64Bits> Elf_Verdef;
394 typedef Elf_Verdaux_Impl<target_endianness, is64Bits> Elf_Verdaux;
395 typedef Elf_Verneed_Impl<target_endianness, is64Bits> Elf_Verneed;
396 typedef Elf_Vernaux_Impl<target_endianness, is64Bits> Elf_Vernaux;
397 typedef Elf_Versym_Impl<target_endianness, is64Bits> Elf_Versym;
398 typedef DynRefImpl<target_endianness, is64Bits> DynRef;
399 typedef content_iterator<DynRef> dyn_iterator;
403 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
404 Elf_Half e_type; // Type of file (see ET_*)
405 Elf_Half e_machine; // Required architecture for this file (see EM_*)
406 Elf_Word e_version; // Must be equal to 1
407 Elf_Addr e_entry; // Address to jump to in order to start program
408 Elf_Off e_phoff; // Program header table's file offset, in bytes
409 Elf_Off e_shoff; // Section header table's file offset, in bytes
410 Elf_Word e_flags; // Processor-specific flags
411 Elf_Half e_ehsize; // Size of ELF header, in bytes
412 Elf_Half e_phentsize;// Size of an entry in the program header table
413 Elf_Half e_phnum; // Number of entries in the program header table
414 Elf_Half e_shentsize;// Size of an entry in the section header table
415 Elf_Half e_shnum; // Number of entries in the section header table
416 Elf_Half e_shstrndx; // Section header table index of section name
418 bool checkMagic() const {
419 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
421 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
422 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
424 // This flag is used for classof, to distinguish ELFObjectFile from
425 // its subclass. If more subclasses will be created, this flag will
426 // have to become an enum.
427 bool isDyldELFObject;
430 typedef SmallVector<const Elf_Shdr*, 1> Sections_t;
431 typedef DenseMap<unsigned, unsigned> IndexMap_t;
432 typedef DenseMap<const Elf_Shdr*, SmallVector<uint32_t, 1> > RelocMap_t;
434 const Elf_Ehdr *Header;
435 const Elf_Shdr *SectionHeaderTable;
436 const Elf_Shdr *dot_shstrtab_sec; // Section header string table.
437 const Elf_Shdr *dot_strtab_sec; // Symbol header string table.
438 const Elf_Shdr *dot_dynstr_sec; // Dynamic symbol string table.
440 // SymbolTableSections[0] always points to the dynamic string table section
441 // header, or NULL if there is no dynamic string table.
442 Sections_t SymbolTableSections;
443 IndexMap_t SymbolTableSectionsIndexMap;
444 DenseMap<const Elf_Sym*, ELF::Elf64_Word> ExtendedSymbolTable;
446 const Elf_Shdr *dot_dynamic_sec; // .dynamic
447 const Elf_Shdr *dot_gnu_version_sec; // .gnu.version
448 const Elf_Shdr *dot_gnu_version_r_sec; // .gnu.version_r
449 const Elf_Shdr *dot_gnu_version_d_sec; // .gnu.version_d
451 // Pointer to SONAME entry in dynamic string table
452 // This is set the first time getLoadName is called.
453 mutable const char *dt_soname;
455 // Records for each version index the corresponding Verdef or Vernaux entry.
456 // This is filled the first time LoadVersionMap() is called.
457 class VersionMapEntry : public PointerIntPair<const void*, 1> {
459 // If the integer is 0, this is an Elf_Verdef*.
460 // If the integer is 1, this is an Elf_Vernaux*.
461 VersionMapEntry() : PointerIntPair<const void*, 1>(NULL, 0) { }
462 VersionMapEntry(const Elf_Verdef *verdef)
463 : PointerIntPair<const void*, 1>(verdef, 0) { }
464 VersionMapEntry(const Elf_Vernaux *vernaux)
465 : PointerIntPair<const void*, 1>(vernaux, 1) { }
466 bool isNull() const { return getPointer() == NULL; }
467 bool isVerdef() const { return !isNull() && getInt() == 0; }
468 bool isVernaux() const { return !isNull() && getInt() == 1; }
469 const Elf_Verdef *getVerdef() const {
470 return isVerdef() ? (const Elf_Verdef*)getPointer() : NULL;
472 const Elf_Vernaux *getVernaux() const {
473 return isVernaux() ? (const Elf_Vernaux*)getPointer() : NULL;
476 mutable SmallVector<VersionMapEntry, 16> VersionMap;
477 void LoadVersionDefs(const Elf_Shdr *sec) const;
478 void LoadVersionNeeds(const Elf_Shdr *ec) const;
479 void LoadVersionMap() const;
481 /// @brief Map sections to an array of relocation sections that reference
482 /// them sorted by section index.
483 RelocMap_t SectionRelocMap;
485 /// @brief Get the relocation section that contains \a Rel.
486 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
487 return getSection(Rel.w.b);
490 bool isRelocationHasAddend(DataRefImpl Rel) const;
492 const T *getEntry(uint16_t Section, uint32_t Entry) const;
494 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
495 const Elf_Shdr *getSection(DataRefImpl index) const;
496 const Elf_Shdr *getSection(uint32_t index) const;
497 const Elf_Rel *getRel(DataRefImpl Rel) const;
498 const Elf_Rela *getRela(DataRefImpl Rela) const;
499 const char *getString(uint32_t section, uint32_t offset) const;
500 const char *getString(const Elf_Shdr *section, uint32_t offset) const;
501 error_code getSymbolName(const Elf_Shdr *section,
503 StringRef &Res) const;
504 error_code getSymbolVersion(const Elf_Shdr *section,
507 bool &IsDefault) const;
508 void VerifyStrTab(const Elf_Shdr *sh) const;
511 const Elf_Sym *getSymbol(DataRefImpl Symb) const; // FIXME: Should be private?
512 void validateSymbol(DataRefImpl Symb) const;
515 const Elf_Dyn *getDyn(DataRefImpl DynData) const;
516 error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
517 bool &IsDefault) const;
519 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
520 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
521 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
522 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
523 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
524 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
525 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
526 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
527 virtual error_code getSymbolSection(DataRefImpl Symb,
528 section_iterator &Res) const;
530 friend class DynRefImpl<target_endianness, is64Bits>;
531 virtual error_code getDynNext(DataRefImpl DynData, DynRef &Result) const;
533 virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const;
534 virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const;
536 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
537 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
538 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
539 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
540 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
541 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
542 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
543 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
544 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
545 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
547 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
548 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
550 virtual error_code getRelocationNext(DataRefImpl Rel,
551 RelocationRef &Res) const;
552 virtual error_code getRelocationAddress(DataRefImpl Rel,
553 uint64_t &Res) const;
554 virtual error_code getRelocationOffset(DataRefImpl Rel,
555 uint64_t &Res) const;
556 virtual error_code getRelocationSymbol(DataRefImpl Rel,
557 SymbolRef &Res) const;
558 virtual error_code getRelocationType(DataRefImpl Rel,
559 uint64_t &Res) const;
560 virtual error_code getRelocationTypeName(DataRefImpl Rel,
561 SmallVectorImpl<char> &Result) const;
562 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
564 virtual error_code getRelocationValueString(DataRefImpl Rel,
565 SmallVectorImpl<char> &Result) const;
568 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
569 virtual symbol_iterator begin_symbols() const;
570 virtual symbol_iterator end_symbols() const;
572 virtual symbol_iterator begin_dynamic_symbols() const;
573 virtual symbol_iterator end_dynamic_symbols() const;
575 virtual section_iterator begin_sections() const;
576 virtual section_iterator end_sections() const;
578 virtual library_iterator begin_libraries_needed() const;
579 virtual library_iterator end_libraries_needed() const;
581 virtual dyn_iterator begin_dynamic_table() const;
582 virtual dyn_iterator end_dynamic_table() const;
584 virtual uint8_t getBytesInAddress() const;
585 virtual StringRef getFileFormatName() const;
586 virtual StringRef getObjectType() const { return "ELF"; }
587 virtual unsigned getArch() const;
588 virtual StringRef getLoadName() const;
590 uint64_t getNumSections() const;
591 uint64_t getStringTableIndex() const;
592 ELF::Elf64_Word getSymbolTableIndex(const Elf_Sym *symb) const;
593 const Elf_Shdr *getSection(const Elf_Sym *symb) const;
595 // Methods for type inquiry through isa, cast, and dyn_cast
596 bool isDyldType() const { return isDyldELFObject; }
597 static inline bool classof(const Binary *v) {
598 return v->getType() == getELFType(target_endianness == support::little,
601 static inline bool classof(const ELFObjectFile *v) { return true; }
604 // Iterate through the version definitions, and place each Elf_Verdef
605 // in the VersionMap according to its index.
606 template<support::endianness target_endianness, bool is64Bits>
607 void ELFObjectFile<target_endianness, is64Bits>::
608 LoadVersionDefs(const Elf_Shdr *sec) const {
609 unsigned vd_size = sec->sh_size; // Size of section in bytes
610 unsigned vd_count = sec->sh_info; // Number of Verdef entries
611 const char *sec_start = (const char*)base() + sec->sh_offset;
612 const char *sec_end = sec_start + vd_size;
613 // The first Verdef entry is at the start of the section.
614 const char *p = sec_start;
615 for (unsigned i = 0; i < vd_count; i++) {
616 if (p + sizeof(Elf_Verdef) > sec_end)
617 report_fatal_error("Section ended unexpectedly while scanning "
618 "version definitions.");
619 const Elf_Verdef *vd = reinterpret_cast<const Elf_Verdef *>(p);
620 if (vd->vd_version != ELF::VER_DEF_CURRENT)
621 report_fatal_error("Unexpected verdef version");
622 size_t index = vd->vd_ndx & ELF::VERSYM_VERSION;
623 if (index >= VersionMap.size())
624 VersionMap.resize(index+1);
625 VersionMap[index] = VersionMapEntry(vd);
630 // Iterate through the versions needed section, and place each Elf_Vernaux
631 // in the VersionMap according to its index.
632 template<support::endianness target_endianness, bool is64Bits>
633 void ELFObjectFile<target_endianness, is64Bits>::
634 LoadVersionNeeds(const Elf_Shdr *sec) const {
635 unsigned vn_size = sec->sh_size; // Size of section in bytes
636 unsigned vn_count = sec->sh_info; // Number of Verneed entries
637 const char *sec_start = (const char*)base() + sec->sh_offset;
638 const char *sec_end = sec_start + vn_size;
639 // The first Verneed entry is at the start of the section.
640 const char *p = sec_start;
641 for (unsigned i = 0; i < vn_count; i++) {
642 if (p + sizeof(Elf_Verneed) > sec_end)
643 report_fatal_error("Section ended unexpectedly while scanning "
644 "version needed records.");
645 const Elf_Verneed *vn = reinterpret_cast<const Elf_Verneed *>(p);
646 if (vn->vn_version != ELF::VER_NEED_CURRENT)
647 report_fatal_error("Unexpected verneed version");
648 // Iterate through the Vernaux entries
649 const char *paux = p + vn->vn_aux;
650 for (unsigned j = 0; j < vn->vn_cnt; j++) {
651 if (paux + sizeof(Elf_Vernaux) > sec_end)
652 report_fatal_error("Section ended unexpected while scanning auxiliary "
653 "version needed records.");
654 const Elf_Vernaux *vna = reinterpret_cast<const Elf_Vernaux *>(paux);
655 size_t index = vna->vna_other & ELF::VERSYM_VERSION;
656 if (index >= VersionMap.size())
657 VersionMap.resize(index+1);
658 VersionMap[index] = VersionMapEntry(vna);
659 paux += vna->vna_next;
665 template<support::endianness target_endianness, bool is64Bits>
666 void ELFObjectFile<target_endianness, is64Bits>::LoadVersionMap() const {
667 // If there is no dynamic symtab or version table, there is nothing to do.
668 if (SymbolTableSections[0] == NULL || dot_gnu_version_sec == NULL)
671 // Has the VersionMap already been loaded?
672 if (VersionMap.size() > 0)
675 // The first two version indexes are reserved.
676 // Index 0 is LOCAL, index 1 is GLOBAL.
677 VersionMap.push_back(VersionMapEntry());
678 VersionMap.push_back(VersionMapEntry());
680 if (dot_gnu_version_d_sec)
681 LoadVersionDefs(dot_gnu_version_d_sec);
683 if (dot_gnu_version_r_sec)
684 LoadVersionNeeds(dot_gnu_version_r_sec);
687 template<support::endianness target_endianness, bool is64Bits>
688 void ELFObjectFile<target_endianness, is64Bits>
689 ::validateSymbol(DataRefImpl Symb) const {
690 const Elf_Sym *symb = getSymbol(Symb);
691 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
692 // FIXME: We really need to do proper error handling in the case of an invalid
693 // input file. Because we don't use exceptions, I think we'll just pass
694 // an error object around.
696 && SymbolTableSection
697 && symb >= (const Elf_Sym*)(base()
698 + SymbolTableSection->sh_offset)
699 && symb < (const Elf_Sym*)(base()
700 + SymbolTableSection->sh_offset
701 + SymbolTableSection->sh_size)))
702 // FIXME: Proper error handling.
703 report_fatal_error("Symb must point to a valid symbol!");
706 template<support::endianness target_endianness, bool is64Bits>
707 error_code ELFObjectFile<target_endianness, is64Bits>
708 ::getSymbolNext(DataRefImpl Symb,
709 SymbolRef &Result) const {
710 validateSymbol(Symb);
711 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
714 // Check to see if we are at the end of this symbol table.
715 if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
716 // We are at the end. If there are other symbol tables, jump to them.
717 // If the symbol table is .dynsym, we are iterating dynamic symbols,
718 // and there is only one table of these.
721 Symb.d.a = 1; // The 0th symbol in ELF is fake.
723 // Otherwise return the terminator.
724 if (Symb.d.b == 0 || Symb.d.b >= SymbolTableSections.size()) {
725 Symb.d.a = std::numeric_limits<uint32_t>::max();
726 Symb.d.b = std::numeric_limits<uint32_t>::max();
730 Result = SymbolRef(Symb, this);
731 return object_error::success;
734 template<support::endianness target_endianness, bool is64Bits>
735 error_code ELFObjectFile<target_endianness, is64Bits>
736 ::getSymbolName(DataRefImpl Symb,
737 StringRef &Result) const {
738 validateSymbol(Symb);
739 const Elf_Sym *symb = getSymbol(Symb);
740 return getSymbolName(SymbolTableSections[Symb.d.b], symb, Result);
743 template<support::endianness target_endianness, bool is64Bits>
744 error_code ELFObjectFile<target_endianness, is64Bits>
745 ::getSymbolVersion(SymbolRef SymRef,
747 bool &IsDefault) const {
748 DataRefImpl Symb = SymRef.getRawDataRefImpl();
749 validateSymbol(Symb);
750 const Elf_Sym *symb = getSymbol(Symb);
751 return getSymbolVersion(SymbolTableSections[Symb.d.b], symb,
755 template<support::endianness target_endianness, bool is64Bits>
756 ELF::Elf64_Word ELFObjectFile<target_endianness, is64Bits>
757 ::getSymbolTableIndex(const Elf_Sym *symb) const {
758 if (symb->st_shndx == ELF::SHN_XINDEX)
759 return ExtendedSymbolTable.lookup(symb);
760 return symb->st_shndx;
763 template<support::endianness target_endianness, bool is64Bits>
764 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
765 ELFObjectFile<target_endianness, is64Bits>
766 ::getSection(const Elf_Sym *symb) const {
767 if (symb->st_shndx == ELF::SHN_XINDEX)
768 return getSection(ExtendedSymbolTable.lookup(symb));
769 if (symb->st_shndx >= ELF::SHN_LORESERVE)
771 return getSection(symb->st_shndx);
774 template<support::endianness target_endianness, bool is64Bits>
775 error_code ELFObjectFile<target_endianness, is64Bits>
776 ::getSymbolFileOffset(DataRefImpl Symb,
777 uint64_t &Result) const {
778 validateSymbol(Symb);
779 const Elf_Sym *symb = getSymbol(Symb);
780 const Elf_Shdr *Section;
781 switch (getSymbolTableIndex(symb)) {
782 case ELF::SHN_COMMON:
783 // Unintialized symbols have no offset in the object file
785 Result = UnknownAddressOrSize;
786 return object_error::success;
788 Result = symb->st_value;
789 return object_error::success;
790 default: Section = getSection(symb);
793 switch (symb->getType()) {
794 case ELF::STT_SECTION:
795 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
796 return object_error::success;
798 case ELF::STT_OBJECT:
799 case ELF::STT_NOTYPE:
800 Result = symb->st_value +
801 (Section ? Section->sh_offset : 0);
802 return object_error::success;
804 Result = UnknownAddressOrSize;
805 return object_error::success;
809 template<support::endianness target_endianness, bool is64Bits>
810 error_code ELFObjectFile<target_endianness, is64Bits>
811 ::getSymbolAddress(DataRefImpl Symb,
812 uint64_t &Result) const {
813 validateSymbol(Symb);
814 const Elf_Sym *symb = getSymbol(Symb);
815 const Elf_Shdr *Section;
816 switch (getSymbolTableIndex(symb)) {
817 case ELF::SHN_COMMON:
819 Result = UnknownAddressOrSize;
820 return object_error::success;
822 Result = symb->st_value;
823 return object_error::success;
824 default: Section = getSection(symb);
827 switch (symb->getType()) {
828 case ELF::STT_SECTION:
829 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
830 return object_error::success;
832 case ELF::STT_OBJECT:
833 case ELF::STT_NOTYPE:
834 Result = symb->st_value + (Section ? Section->sh_addr : 0);
835 return object_error::success;
837 Result = UnknownAddressOrSize;
838 return object_error::success;
842 template<support::endianness target_endianness, bool is64Bits>
843 error_code ELFObjectFile<target_endianness, is64Bits>
844 ::getSymbolSize(DataRefImpl Symb,
845 uint64_t &Result) const {
846 validateSymbol(Symb);
847 const Elf_Sym *symb = getSymbol(Symb);
848 if (symb->st_size == 0)
849 Result = UnknownAddressOrSize;
850 Result = symb->st_size;
851 return object_error::success;
854 template<support::endianness target_endianness, bool is64Bits>
855 error_code ELFObjectFile<target_endianness, is64Bits>
856 ::getSymbolNMTypeChar(DataRefImpl Symb,
857 char &Result) const {
858 validateSymbol(Symb);
859 const Elf_Sym *symb = getSymbol(Symb);
860 const Elf_Shdr *Section = getSection(symb);
865 switch (Section->sh_type) {
866 case ELF::SHT_PROGBITS:
867 case ELF::SHT_DYNAMIC:
868 switch (Section->sh_flags) {
869 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
871 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
874 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
875 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
879 case ELF::SHT_NOBITS: ret = 'b';
883 switch (getSymbolTableIndex(symb)) {
888 case ELF::SHN_ABS: ret = 'a'; break;
889 case ELF::SHN_COMMON: ret = 'c'; break;
892 switch (symb->getBinding()) {
893 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
895 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
898 if (symb->getType() == ELF::STT_OBJECT)
904 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
906 if (error_code ec = getSymbolName(Symb, name))
908 Result = StringSwitch<char>(name)
909 .StartsWith(".debug", 'N')
910 .StartsWith(".note", 'n')
912 return object_error::success;
916 return object_error::success;
919 template<support::endianness target_endianness, bool is64Bits>
920 error_code ELFObjectFile<target_endianness, is64Bits>
921 ::getSymbolType(DataRefImpl Symb,
922 SymbolRef::Type &Result) const {
923 validateSymbol(Symb);
924 const Elf_Sym *symb = getSymbol(Symb);
926 switch (symb->getType()) {
927 case ELF::STT_NOTYPE:
928 Result = SymbolRef::ST_Unknown;
930 case ELF::STT_SECTION:
931 Result = SymbolRef::ST_Debug;
934 Result = SymbolRef::ST_File;
937 Result = SymbolRef::ST_Function;
939 case ELF::STT_OBJECT:
940 case ELF::STT_COMMON:
942 Result = SymbolRef::ST_Data;
945 Result = SymbolRef::ST_Other;
948 return object_error::success;
951 template<support::endianness target_endianness, bool is64Bits>
952 error_code ELFObjectFile<target_endianness, is64Bits>
953 ::getSymbolFlags(DataRefImpl Symb,
954 uint32_t &Result) const {
955 validateSymbol(Symb);
956 const Elf_Sym *symb = getSymbol(Symb);
958 Result = SymbolRef::SF_None;
960 if (symb->getBinding() != ELF::STB_LOCAL)
961 Result |= SymbolRef::SF_Global;
963 if (symb->getBinding() == ELF::STB_WEAK)
964 Result |= SymbolRef::SF_Weak;
966 if (symb->st_shndx == ELF::SHN_ABS)
967 Result |= SymbolRef::SF_Absolute;
969 if (symb->getType() == ELF::STT_FILE ||
970 symb->getType() == ELF::STT_SECTION)
971 Result |= SymbolRef::SF_FormatSpecific;
973 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
974 Result |= SymbolRef::SF_Undefined;
976 if (symb->getType() == ELF::STT_COMMON ||
977 getSymbolTableIndex(symb) == ELF::SHN_COMMON)
978 Result |= SymbolRef::SF_Common;
980 if (symb->getType() == ELF::STT_TLS)
981 Result |= SymbolRef::SF_ThreadLocal;
983 return object_error::success;
986 template<support::endianness target_endianness, bool is64Bits>
987 error_code ELFObjectFile<target_endianness, is64Bits>
988 ::getSymbolSection(DataRefImpl Symb,
989 section_iterator &Res) const {
990 validateSymbol(Symb);
991 const Elf_Sym *symb = getSymbol(Symb);
992 const Elf_Shdr *sec = getSection(symb);
994 Res = end_sections();
997 Sec.p = reinterpret_cast<intptr_t>(sec);
998 Res = section_iterator(SectionRef(Sec, this));
1000 return object_error::success;
1003 template<support::endianness target_endianness, bool is64Bits>
1004 error_code ELFObjectFile<target_endianness, is64Bits>
1005 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
1006 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
1007 sec += Header->e_shentsize;
1008 Sec.p = reinterpret_cast<intptr_t>(sec);
1009 Result = SectionRef(Sec, this);
1010 return object_error::success;
1013 template<support::endianness target_endianness, bool is64Bits>
1014 error_code ELFObjectFile<target_endianness, is64Bits>
1015 ::getSectionName(DataRefImpl Sec,
1016 StringRef &Result) const {
1017 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1018 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
1019 return object_error::success;
1022 template<support::endianness target_endianness, bool is64Bits>
1023 error_code ELFObjectFile<target_endianness, is64Bits>
1024 ::getSectionAddress(DataRefImpl Sec,
1025 uint64_t &Result) const {
1026 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1027 Result = sec->sh_addr;
1028 return object_error::success;
1031 template<support::endianness target_endianness, bool is64Bits>
1032 error_code ELFObjectFile<target_endianness, is64Bits>
1033 ::getSectionSize(DataRefImpl Sec,
1034 uint64_t &Result) const {
1035 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1036 Result = sec->sh_size;
1037 return object_error::success;
1040 template<support::endianness target_endianness, bool is64Bits>
1041 error_code ELFObjectFile<target_endianness, is64Bits>
1042 ::getSectionContents(DataRefImpl Sec,
1043 StringRef &Result) const {
1044 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1045 const char *start = (const char*)base() + sec->sh_offset;
1046 Result = StringRef(start, sec->sh_size);
1047 return object_error::success;
1050 template<support::endianness target_endianness, bool is64Bits>
1051 error_code ELFObjectFile<target_endianness, is64Bits>
1052 ::getSectionAlignment(DataRefImpl Sec,
1053 uint64_t &Result) const {
1054 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1055 Result = sec->sh_addralign;
1056 return object_error::success;
1059 template<support::endianness target_endianness, bool is64Bits>
1060 error_code ELFObjectFile<target_endianness, is64Bits>
1061 ::isSectionText(DataRefImpl Sec,
1062 bool &Result) const {
1063 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1064 if (sec->sh_flags & ELF::SHF_EXECINSTR)
1068 return object_error::success;
1071 template<support::endianness target_endianness, bool is64Bits>
1072 error_code ELFObjectFile<target_endianness, is64Bits>
1073 ::isSectionData(DataRefImpl Sec,
1074 bool &Result) const {
1075 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1076 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1077 && sec->sh_type == ELF::SHT_PROGBITS)
1081 return object_error::success;
1084 template<support::endianness target_endianness, bool is64Bits>
1085 error_code ELFObjectFile<target_endianness, is64Bits>
1086 ::isSectionBSS(DataRefImpl Sec,
1087 bool &Result) const {
1088 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1089 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1090 && sec->sh_type == ELF::SHT_NOBITS)
1094 return object_error::success;
1097 template<support::endianness target_endianness, bool is64Bits>
1098 error_code ELFObjectFile<target_endianness, is64Bits>
1099 ::sectionContainsSymbol(DataRefImpl Sec,
1101 bool &Result) const {
1102 // FIXME: Unimplemented.
1104 return object_error::success;
1107 template<support::endianness target_endianness, bool is64Bits>
1108 relocation_iterator ELFObjectFile<target_endianness, is64Bits>
1109 ::getSectionRelBegin(DataRefImpl Sec) const {
1110 DataRefImpl RelData;
1111 memset(&RelData, 0, sizeof(RelData));
1112 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1113 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1114 if (sec != 0 && ittr != SectionRelocMap.end()) {
1115 RelData.w.a = getSection(ittr->second[0])->sh_info;
1116 RelData.w.b = ittr->second[0];
1119 return relocation_iterator(RelocationRef(RelData, this));
1122 template<support::endianness target_endianness, bool is64Bits>
1123 relocation_iterator ELFObjectFile<target_endianness, is64Bits>
1124 ::getSectionRelEnd(DataRefImpl Sec) const {
1125 DataRefImpl RelData;
1126 memset(&RelData, 0, sizeof(RelData));
1127 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1128 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1129 if (sec != 0 && ittr != SectionRelocMap.end()) {
1130 // Get the index of the last relocation section for this section.
1131 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
1132 const Elf_Shdr *relocsec = getSection(relocsecindex);
1133 RelData.w.a = relocsec->sh_info;
1134 RelData.w.b = relocsecindex;
1135 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
1137 return relocation_iterator(RelocationRef(RelData, this));
1141 template<support::endianness target_endianness, bool is64Bits>
1142 error_code ELFObjectFile<target_endianness, is64Bits>
1143 ::getRelocationNext(DataRefImpl Rel,
1144 RelocationRef &Result) const {
1146 const Elf_Shdr *relocsec = getSection(Rel.w.b);
1147 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
1148 // We have reached the end of the relocations for this section. See if there
1149 // is another relocation section.
1150 typename RelocMap_t::mapped_type relocseclist =
1151 SectionRelocMap.lookup(getSection(Rel.w.a));
1153 // Do a binary search for the current reloc section index (which must be
1154 // present). Then get the next one.
1155 typename RelocMap_t::mapped_type::const_iterator loc =
1156 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
1159 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
1160 // to the end iterator.
1161 if (loc != relocseclist.end()) {
1166 Result = RelocationRef(Rel, this);
1167 return object_error::success;
1170 template<support::endianness target_endianness, bool is64Bits>
1171 error_code ELFObjectFile<target_endianness, is64Bits>
1172 ::getRelocationSymbol(DataRefImpl Rel,
1173 SymbolRef &Result) const {
1175 const Elf_Shdr *sec = getSection(Rel.w.b);
1176 switch (sec->sh_type) {
1178 report_fatal_error("Invalid section type in Rel!");
1179 case ELF::SHT_REL : {
1180 symbolIdx = getRel(Rel)->getSymbol();
1183 case ELF::SHT_RELA : {
1184 symbolIdx = getRela(Rel)->getSymbol();
1188 DataRefImpl SymbolData;
1189 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
1190 if (it == SymbolTableSectionsIndexMap.end())
1191 report_fatal_error("Relocation symbol table not found!");
1192 SymbolData.d.a = symbolIdx;
1193 SymbolData.d.b = it->second;
1194 Result = SymbolRef(SymbolData, this);
1195 return object_error::success;
1198 template<support::endianness target_endianness, bool is64Bits>
1199 error_code ELFObjectFile<target_endianness, is64Bits>
1200 ::getRelocationAddress(DataRefImpl Rel,
1201 uint64_t &Result) const {
1203 const Elf_Shdr *sec = getSection(Rel.w.b);
1204 switch (sec->sh_type) {
1206 report_fatal_error("Invalid section type in Rel!");
1207 case ELF::SHT_REL : {
1208 offset = getRel(Rel)->r_offset;
1211 case ELF::SHT_RELA : {
1212 offset = getRela(Rel)->r_offset;
1218 return object_error::success;
1221 template<support::endianness target_endianness, bool is64Bits>
1222 error_code ELFObjectFile<target_endianness, is64Bits>
1223 ::getRelocationOffset(DataRefImpl Rel,
1224 uint64_t &Result) const {
1226 const Elf_Shdr *sec = getSection(Rel.w.b);
1227 switch (sec->sh_type) {
1229 report_fatal_error("Invalid section type in Rel!");
1230 case ELF::SHT_REL : {
1231 offset = getRel(Rel)->r_offset;
1234 case ELF::SHT_RELA : {
1235 offset = getRela(Rel)->r_offset;
1240 Result = offset - sec->sh_addr;
1241 return object_error::success;
1244 template<support::endianness target_endianness, bool is64Bits>
1245 error_code ELFObjectFile<target_endianness, is64Bits>
1246 ::getRelocationType(DataRefImpl Rel,
1247 uint64_t &Result) const {
1248 const Elf_Shdr *sec = getSection(Rel.w.b);
1249 switch (sec->sh_type) {
1251 report_fatal_error("Invalid section type in Rel!");
1252 case ELF::SHT_REL : {
1253 Result = getRel(Rel)->getType();
1256 case ELF::SHT_RELA : {
1257 Result = getRela(Rel)->getType();
1261 return object_error::success;
1264 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
1265 case ELF::enum: res = #enum; break;
1267 template<support::endianness target_endianness, bool is64Bits>
1268 error_code ELFObjectFile<target_endianness, is64Bits>
1269 ::getRelocationTypeName(DataRefImpl Rel,
1270 SmallVectorImpl<char> &Result) const {
1271 const Elf_Shdr *sec = getSection(Rel.w.b);
1274 switch (sec->sh_type) {
1276 return object_error::parse_failed;
1277 case ELF::SHT_REL : {
1278 type = getRel(Rel)->getType();
1281 case ELF::SHT_RELA : {
1282 type = getRela(Rel)->getType();
1286 switch (Header->e_machine) {
1287 case ELF::EM_X86_64:
1289 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
1290 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
1291 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
1292 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
1293 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
1294 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
1295 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
1296 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
1297 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
1298 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
1299 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
1300 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
1301 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
1302 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
1303 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
1304 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
1305 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
1306 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
1307 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
1308 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
1309 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
1310 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
1311 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
1312 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
1313 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
1314 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
1315 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
1316 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
1317 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
1318 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
1319 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
1320 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
1327 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
1328 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
1329 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
1330 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
1331 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
1332 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
1333 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
1334 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
1335 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
1336 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
1337 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
1338 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
1339 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
1340 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
1341 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
1342 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
1343 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
1344 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
1345 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
1346 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
1347 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
1348 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
1349 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
1350 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
1351 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
1352 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
1353 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
1354 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
1355 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
1356 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
1357 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
1358 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
1359 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
1360 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
1361 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
1362 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
1363 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
1364 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
1365 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
1366 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
1374 Result.append(res.begin(), res.end());
1375 return object_error::success;
1378 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
1380 template<support::endianness target_endianness, bool is64Bits>
1381 error_code ELFObjectFile<target_endianness, is64Bits>
1382 ::getRelocationAdditionalInfo(DataRefImpl Rel,
1383 int64_t &Result) const {
1384 const Elf_Shdr *sec = getSection(Rel.w.b);
1385 switch (sec->sh_type) {
1387 report_fatal_error("Invalid section type in Rel!");
1388 case ELF::SHT_REL : {
1390 return object_error::success;
1392 case ELF::SHT_RELA : {
1393 Result = getRela(Rel)->r_addend;
1394 return object_error::success;
1399 template<support::endianness target_endianness, bool is64Bits>
1400 error_code ELFObjectFile<target_endianness, is64Bits>
1401 ::getRelocationValueString(DataRefImpl Rel,
1402 SmallVectorImpl<char> &Result) const {
1403 const Elf_Shdr *sec = getSection(Rel.w.b);
1407 uint16_t symbol_index = 0;
1408 switch (sec->sh_type) {
1410 return object_error::parse_failed;
1411 case ELF::SHT_REL : {
1412 type = getRel(Rel)->getType();
1413 symbol_index = getRel(Rel)->getSymbol();
1414 // TODO: Read implicit addend from section data.
1417 case ELF::SHT_RELA : {
1418 type = getRela(Rel)->getType();
1419 symbol_index = getRela(Rel)->getSymbol();
1420 addend = getRela(Rel)->r_addend;
1424 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1426 if (error_code ec = getSymbolName(getSection(sec->sh_link), symb, symname))
1428 switch (Header->e_machine) {
1429 case ELF::EM_X86_64:
1431 case ELF::R_X86_64_32S:
1434 case ELF::R_X86_64_PC32: {
1436 raw_string_ostream fmt(fmtbuf);
1437 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1439 Result.append(fmtbuf.begin(), fmtbuf.end());
1450 Result.append(res.begin(), res.end());
1451 return object_error::success;
1454 // Verify that the last byte in the string table in a null.
1455 template<support::endianness target_endianness, bool is64Bits>
1456 void ELFObjectFile<target_endianness, is64Bits>
1457 ::VerifyStrTab(const Elf_Shdr *sh) const {
1458 const char *strtab = (const char*)base() + sh->sh_offset;
1459 if (strtab[sh->sh_size - 1] != 0)
1460 // FIXME: Proper error handling.
1461 report_fatal_error("String table must end with a null terminator!");
1464 template<support::endianness target_endianness, bool is64Bits>
1465 ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object
1467 : ObjectFile(getELFType(target_endianness == support::little, is64Bits),
1469 , isDyldELFObject(false)
1470 , SectionHeaderTable(0)
1471 , dot_shstrtab_sec(0)
1474 , dot_dynamic_sec(0)
1475 , dot_gnu_version_sec(0)
1476 , dot_gnu_version_r_sec(0)
1477 , dot_gnu_version_d_sec(0)
1481 const uint64_t FileSize = Data->getBufferSize();
1483 if (sizeof(Elf_Ehdr) > FileSize)
1484 // FIXME: Proper error handling.
1485 report_fatal_error("File too short!");
1487 Header = reinterpret_cast<const Elf_Ehdr *>(base());
1489 if (Header->e_shoff == 0)
1492 const uint64_t SectionTableOffset = Header->e_shoff;
1494 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize)
1495 // FIXME: Proper error handling.
1496 report_fatal_error("Section header table goes past end of file!");
1498 // The getNumSections() call below depends on SectionHeaderTable being set.
1499 SectionHeaderTable =
1500 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
1501 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
1503 if (SectionTableOffset + SectionTableSize > FileSize)
1504 // FIXME: Proper error handling.
1505 report_fatal_error("Section table goes past end of file!");
1507 // To find the symbol tables we walk the section table to find SHT_SYMTAB.
1508 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
1509 const Elf_Shdr* sh = SectionHeaderTable;
1511 // Reserve SymbolTableSections[0] for .dynsym
1512 SymbolTableSections.push_back(NULL);
1514 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
1515 switch (sh->sh_type) {
1516 case ELF::SHT_SYMTAB_SHNDX: {
1517 if (SymbolTableSectionHeaderIndex)
1518 // FIXME: Proper error handling.
1519 report_fatal_error("More than one .symtab_shndx!");
1520 SymbolTableSectionHeaderIndex = sh;
1523 case ELF::SHT_SYMTAB: {
1524 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
1525 SymbolTableSections.push_back(sh);
1528 case ELF::SHT_DYNSYM: {
1529 if (SymbolTableSections[0] != NULL)
1530 // FIXME: Proper error handling.
1531 report_fatal_error("More than one .dynsym!");
1532 SymbolTableSectionsIndexMap[i] = 0;
1533 SymbolTableSections[0] = sh;
1537 case ELF::SHT_RELA: {
1538 SectionRelocMap[getSection(sh->sh_info)].push_back(i);
1541 case ELF::SHT_DYNAMIC: {
1542 if (dot_dynamic_sec != NULL)
1543 // FIXME: Proper error handling.
1544 report_fatal_error("More than one .dynamic!");
1545 dot_dynamic_sec = sh;
1548 case ELF::SHT_GNU_versym: {
1549 if (dot_gnu_version_sec != NULL)
1550 // FIXME: Proper error handling.
1551 report_fatal_error("More than one .gnu.version section!");
1552 dot_gnu_version_sec = sh;
1555 case ELF::SHT_GNU_verdef: {
1556 if (dot_gnu_version_d_sec != NULL)
1557 // FIXME: Proper error handling.
1558 report_fatal_error("More than one .gnu.version_d section!");
1559 dot_gnu_version_d_sec = sh;
1562 case ELF::SHT_GNU_verneed: {
1563 if (dot_gnu_version_r_sec != NULL)
1564 // FIXME: Proper error handling.
1565 report_fatal_error("More than one .gnu.version_r section!");
1566 dot_gnu_version_r_sec = sh;
1573 // Sort section relocation lists by index.
1574 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
1575 e = SectionRelocMap.end(); i != e; ++i) {
1576 std::sort(i->second.begin(), i->second.end());
1579 // Get string table sections.
1580 dot_shstrtab_sec = getSection(getStringTableIndex());
1581 if (dot_shstrtab_sec) {
1582 // Verify that the last byte in the string table in a null.
1583 VerifyStrTab(dot_shstrtab_sec);
1586 // Merge this into the above loop.
1587 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
1588 *e = i + getNumSections() * Header->e_shentsize;
1589 i != e; i += Header->e_shentsize) {
1590 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
1591 if (sh->sh_type == ELF::SHT_STRTAB) {
1592 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
1593 if (SectionName == ".strtab") {
1594 if (dot_strtab_sec != 0)
1595 // FIXME: Proper error handling.
1596 report_fatal_error("Already found section named .strtab!");
1597 dot_strtab_sec = sh;
1598 VerifyStrTab(dot_strtab_sec);
1599 } else if (SectionName == ".dynstr") {
1600 if (dot_dynstr_sec != 0)
1601 // FIXME: Proper error handling.
1602 report_fatal_error("Already found section named .dynstr!");
1603 dot_dynstr_sec = sh;
1604 VerifyStrTab(dot_dynstr_sec);
1609 // Build symbol name side-mapping if there is one.
1610 if (SymbolTableSectionHeaderIndex) {
1611 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
1612 SymbolTableSectionHeaderIndex->sh_offset);
1614 for (symbol_iterator si = begin_symbols(),
1615 se = end_symbols(); si != se; si.increment(ec)) {
1617 report_fatal_error("Fewer extended symbol table entries than symbols!");
1618 if (*ShndxTable != ELF::SHN_UNDEF)
1619 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
1625 template<support::endianness target_endianness, bool is64Bits>
1626 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1627 ::begin_symbols() const {
1628 DataRefImpl SymbolData;
1629 memset(&SymbolData, 0, sizeof(SymbolData));
1630 if (SymbolTableSections.size() <= 1) {
1631 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1632 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1634 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
1635 SymbolData.d.b = 1; // The 0th table is .dynsym
1637 return symbol_iterator(SymbolRef(SymbolData, this));
1640 template<support::endianness target_endianness, bool is64Bits>
1641 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1642 ::end_symbols() const {
1643 DataRefImpl SymbolData;
1644 memset(&SymbolData, 0, sizeof(SymbolData));
1645 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1646 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1647 return symbol_iterator(SymbolRef(SymbolData, this));
1650 template<support::endianness target_endianness, bool is64Bits>
1651 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1652 ::begin_dynamic_symbols() const {
1653 DataRefImpl SymbolData;
1654 memset(&SymbolData, 0, sizeof(SymbolData));
1655 if (SymbolTableSections[0] == NULL) {
1656 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1657 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1659 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
1660 SymbolData.d.b = 0; // The 0th table is .dynsym
1662 return symbol_iterator(SymbolRef(SymbolData, this));
1665 template<support::endianness target_endianness, bool is64Bits>
1666 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1667 ::end_dynamic_symbols() const {
1668 DataRefImpl SymbolData;
1669 memset(&SymbolData, 0, sizeof(SymbolData));
1670 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1671 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1672 return symbol_iterator(SymbolRef(SymbolData, this));
1675 template<support::endianness target_endianness, bool is64Bits>
1676 section_iterator ELFObjectFile<target_endianness, is64Bits>
1677 ::begin_sections() const {
1679 memset(&ret, 0, sizeof(DataRefImpl));
1680 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
1681 return section_iterator(SectionRef(ret, this));
1684 template<support::endianness target_endianness, bool is64Bits>
1685 section_iterator ELFObjectFile<target_endianness, is64Bits>
1686 ::end_sections() const {
1688 memset(&ret, 0, sizeof(DataRefImpl));
1689 ret.p = reinterpret_cast<intptr_t>(base()
1691 + (Header->e_shentsize*getNumSections()));
1692 return section_iterator(SectionRef(ret, this));
1695 template<support::endianness target_endianness, bool is64Bits>
1696 typename ELFObjectFile<target_endianness, is64Bits>::dyn_iterator
1697 ELFObjectFile<target_endianness, is64Bits>::begin_dynamic_table() const {
1698 DataRefImpl DynData;
1699 memset(&DynData, 0, sizeof(DynData));
1700 if (dot_dynamic_sec == NULL || dot_dynamic_sec->sh_size == 0) {
1701 DynData.d.a = std::numeric_limits<uint32_t>::max();
1705 return dyn_iterator(DynRef(DynData, this));
1708 template<support::endianness target_endianness, bool is64Bits>
1709 typename ELFObjectFile<target_endianness, is64Bits>::dyn_iterator
1710 ELFObjectFile<target_endianness, is64Bits>
1711 ::end_dynamic_table() const {
1712 DataRefImpl DynData;
1713 memset(&DynData, 0, sizeof(DynData));
1714 DynData.d.a = std::numeric_limits<uint32_t>::max();
1715 return dyn_iterator(DynRef(DynData, this));
1718 template<support::endianness target_endianness, bool is64Bits>
1719 error_code ELFObjectFile<target_endianness, is64Bits>
1720 ::getDynNext(DataRefImpl DynData,
1721 DynRef &Result) const {
1724 // Check to see if we are at the end of .dynamic
1725 if (DynData.d.a >= dot_dynamic_sec->getEntityCount()) {
1726 // We are at the end. Return the terminator.
1727 DynData.d.a = std::numeric_limits<uint32_t>::max();
1730 Result = DynRef(DynData, this);
1731 return object_error::success;
1734 template<support::endianness target_endianness, bool is64Bits>
1736 ELFObjectFile<target_endianness, is64Bits>::getLoadName() const {
1738 // Find the DT_SONAME entry
1739 dyn_iterator it = begin_dynamic_table();
1740 dyn_iterator ie = end_dynamic_table();
1743 if (it->getTag() == ELF::DT_SONAME)
1747 report_fatal_error("dynamic table iteration failed");
1750 if (dot_dynstr_sec == NULL)
1751 report_fatal_error("Dynamic string table is missing");
1752 dt_soname = getString(dot_dynstr_sec, it->getVal());
1760 template<support::endianness target_endianness, bool is64Bits>
1761 library_iterator ELFObjectFile<target_endianness, is64Bits>
1762 ::begin_libraries_needed() const {
1763 // Find the first DT_NEEDED entry
1764 dyn_iterator i = begin_dynamic_table();
1765 dyn_iterator e = end_dynamic_table();
1768 if (i->getTag() == ELF::DT_NEEDED)
1772 report_fatal_error("dynamic table iteration failed");
1774 // Use the same DataRefImpl format as DynRef.
1775 return library_iterator(LibraryRef(i->getRawDataRefImpl(), this));
1778 template<support::endianness target_endianness, bool is64Bits>
1779 error_code ELFObjectFile<target_endianness, is64Bits>
1780 ::getLibraryNext(DataRefImpl Data,
1781 LibraryRef &Result) const {
1782 // Use the same DataRefImpl format as DynRef.
1783 dyn_iterator i = dyn_iterator(DynRef(Data, this));
1784 dyn_iterator e = end_dynamic_table();
1786 // Skip the current dynamic table entry.
1790 // TODO: proper error handling
1792 report_fatal_error("dynamic table iteration failed");
1795 // Find the next DT_NEEDED entry.
1797 if (i->getTag() == ELF::DT_NEEDED)
1801 report_fatal_error("dynamic table iteration failed");
1803 Result = LibraryRef(i->getRawDataRefImpl(), this);
1804 return object_error::success;
1807 template<support::endianness target_endianness, bool is64Bits>
1808 error_code ELFObjectFile<target_endianness, is64Bits>
1809 ::getLibraryPath(DataRefImpl Data, StringRef &Res) const {
1810 dyn_iterator i = dyn_iterator(DynRef(Data, this));
1811 if (i == end_dynamic_table())
1812 report_fatal_error("getLibraryPath() called on iterator end");
1814 if (i->getTag() != ELF::DT_NEEDED)
1815 report_fatal_error("Invalid library_iterator");
1817 // This uses .dynstr to lookup the name of the DT_NEEDED entry.
1818 // THis works as long as DT_STRTAB == .dynstr. This is true most of
1819 // the time, but the specification allows exceptions.
1820 // TODO: This should really use DT_STRTAB instead. Doing this requires
1821 // reading the program headers.
1822 if (dot_dynstr_sec == NULL)
1823 report_fatal_error("Dynamic string table is missing");
1824 Res = getString(dot_dynstr_sec, i->getVal());
1825 return object_error::success;
1828 template<support::endianness target_endianness, bool is64Bits>
1829 library_iterator ELFObjectFile<target_endianness, is64Bits>
1830 ::end_libraries_needed() const {
1831 dyn_iterator e = end_dynamic_table();
1832 // Use the same DataRefImpl format as DynRef.
1833 return library_iterator(LibraryRef(e->getRawDataRefImpl(), this));
1836 template<support::endianness target_endianness, bool is64Bits>
1837 uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const {
1838 return is64Bits ? 8 : 4;
1841 template<support::endianness target_endianness, bool is64Bits>
1842 StringRef ELFObjectFile<target_endianness, is64Bits>
1843 ::getFileFormatName() const {
1844 switch(Header->e_ident[ELF::EI_CLASS]) {
1845 case ELF::ELFCLASS32:
1846 switch(Header->e_machine) {
1848 return "ELF32-i386";
1849 case ELF::EM_X86_64:
1850 return "ELF32-x86-64";
1854 return "ELF32-unknown";
1856 case ELF::ELFCLASS64:
1857 switch(Header->e_machine) {
1859 return "ELF64-i386";
1860 case ELF::EM_X86_64:
1861 return "ELF64-x86-64";
1863 return "ELF64-unknown";
1866 // FIXME: Proper error handling.
1867 report_fatal_error("Invalid ELFCLASS!");
1871 template<support::endianness target_endianness, bool is64Bits>
1872 unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const {
1873 switch(Header->e_machine) {
1876 case ELF::EM_X86_64:
1877 return Triple::x86_64;
1881 return Triple::UnknownArch;
1885 template<support::endianness target_endianness, bool is64Bits>
1886 uint64_t ELFObjectFile<target_endianness, is64Bits>::getNumSections() const {
1887 assert(Header && "Header not initialized!");
1888 if (Header->e_shnum == ELF::SHN_UNDEF) {
1889 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
1890 return SectionHeaderTable->sh_size;
1892 return Header->e_shnum;
1895 template<support::endianness target_endianness, bool is64Bits>
1897 ELFObjectFile<target_endianness, is64Bits>::getStringTableIndex() const {
1898 if (Header->e_shnum == ELF::SHN_UNDEF) {
1899 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
1900 return SectionHeaderTable->sh_link;
1901 if (Header->e_shstrndx >= getNumSections())
1904 return Header->e_shstrndx;
1908 template<support::endianness target_endianness, bool is64Bits>
1909 template<typename T>
1911 ELFObjectFile<target_endianness, is64Bits>::getEntry(uint16_t Section,
1912 uint32_t Entry) const {
1913 return getEntry<T>(getSection(Section), Entry);
1916 template<support::endianness target_endianness, bool is64Bits>
1917 template<typename T>
1919 ELFObjectFile<target_endianness, is64Bits>::getEntry(const Elf_Shdr * Section,
1920 uint32_t Entry) const {
1921 return reinterpret_cast<const T *>(
1923 + Section->sh_offset
1924 + (Entry * Section->sh_entsize));
1927 template<support::endianness target_endianness, bool is64Bits>
1928 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym *
1929 ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const {
1930 return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
1933 template<support::endianness target_endianness, bool is64Bits>
1934 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Dyn *
1935 ELFObjectFile<target_endianness, is64Bits>::getDyn(DataRefImpl DynData) const {
1936 return getEntry<Elf_Dyn>(dot_dynamic_sec, DynData.d.a);
1939 template<support::endianness target_endianness, bool is64Bits>
1940 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel *
1941 ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const {
1942 return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
1945 template<support::endianness target_endianness, bool is64Bits>
1946 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela *
1947 ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const {
1948 return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
1951 template<support::endianness target_endianness, bool is64Bits>
1952 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
1953 ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const {
1954 const Elf_Shdr *sec = getSection(Symb.d.b);
1955 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
1956 // FIXME: Proper error handling.
1957 report_fatal_error("Invalid symbol table section!");
1961 template<support::endianness target_endianness, bool is64Bits>
1962 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
1963 ELFObjectFile<target_endianness, is64Bits>::getSection(uint32_t index) const {
1966 if (!SectionHeaderTable || index >= getNumSections())
1967 // FIXME: Proper error handling.
1968 report_fatal_error("Invalid section index!");
1970 return reinterpret_cast<const Elf_Shdr *>(
1971 reinterpret_cast<const char *>(SectionHeaderTable)
1972 + (index * Header->e_shentsize));
1975 template<support::endianness target_endianness, bool is64Bits>
1976 const char *ELFObjectFile<target_endianness, is64Bits>
1977 ::getString(uint32_t section,
1978 ELF::Elf32_Word offset) const {
1979 return getString(getSection(section), offset);
1982 template<support::endianness target_endianness, bool is64Bits>
1983 const char *ELFObjectFile<target_endianness, is64Bits>
1984 ::getString(const Elf_Shdr *section,
1985 ELF::Elf32_Word offset) const {
1986 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
1987 if (offset >= section->sh_size)
1988 // FIXME: Proper error handling.
1989 report_fatal_error("Symbol name offset outside of string table!");
1990 return (const char *)base() + section->sh_offset + offset;
1993 template<support::endianness target_endianness, bool is64Bits>
1994 error_code ELFObjectFile<target_endianness, is64Bits>
1995 ::getSymbolName(const Elf_Shdr *section,
1996 const Elf_Sym *symb,
1997 StringRef &Result) const {
1998 if (symb->st_name == 0) {
1999 const Elf_Shdr *section = getSection(symb);
2003 Result = getString(dot_shstrtab_sec, section->sh_name);
2004 return object_error::success;
2007 if (section == SymbolTableSections[0]) {
2008 // Symbol is in .dynsym, use .dynstr string table
2009 Result = getString(dot_dynstr_sec, symb->st_name);
2011 // Use the default symbol table name section.
2012 Result = getString(dot_strtab_sec, symb->st_name);
2014 return object_error::success;
2017 template<support::endianness target_endianness, bool is64Bits>
2018 error_code ELFObjectFile<target_endianness, is64Bits>
2019 ::getSymbolVersion(const Elf_Shdr *section,
2020 const Elf_Sym *symb,
2022 bool &IsDefault) const {
2023 // Handle non-dynamic symbols.
2024 if (section != SymbolTableSections[0]) {
2025 // Non-dynamic symbols can have versions in their names
2026 // A name of the form 'foo@V1' indicates version 'V1', non-default.
2027 // A name of the form 'foo@@V2' indicates version 'V2', default version.
2029 error_code ec = getSymbolName(section, symb, Name);
2030 if (ec != object_error::success)
2032 size_t atpos = Name.find('@');
2033 if (atpos == StringRef::npos) {
2036 return object_error::success;
2039 if (atpos < Name.size() && Name[atpos] == '@') {
2045 Version = Name.substr(atpos);
2046 return object_error::success;
2049 // This is a dynamic symbol. Look in the GNU symbol version table.
2050 if (dot_gnu_version_sec == NULL) {
2051 // No version table.
2054 return object_error::success;
2057 // Determine the position in the symbol table of this entry.
2058 const char *sec_start = (const char*)base() + section->sh_offset;
2059 size_t entry_index = ((const char*)symb - sec_start)/section->sh_entsize;
2061 // Get the corresponding version index entry
2062 const Elf_Versym *vs = getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index);
2063 size_t version_index = vs->vs_index & ELF::VERSYM_VERSION;
2065 // Special markers for unversioned symbols.
2066 if (version_index == ELF::VER_NDX_LOCAL ||
2067 version_index == ELF::VER_NDX_GLOBAL) {
2070 return object_error::success;
2073 // Lookup this symbol in the version table
2075 if (version_index >= VersionMap.size() || VersionMap[version_index].isNull())
2076 report_fatal_error("Symbol has version index without corresponding "
2077 "define or reference entry");
2078 const VersionMapEntry &entry = VersionMap[version_index];
2080 // Get the version name string
2082 if (entry.isVerdef()) {
2083 // The first Verdaux entry holds the name.
2084 name_offset = entry.getVerdef()->getAux()->vda_name;
2086 name_offset = entry.getVernaux()->vna_name;
2088 Version = getString(dot_dynstr_sec, name_offset);
2091 if (entry.isVerdef()) {
2092 IsDefault = !(vs->vs_index & ELF::VERSYM_HIDDEN);
2097 return object_error::success;
2100 template<support::endianness target_endianness, bool is64Bits>
2101 inline DynRefImpl<target_endianness, is64Bits>
2102 ::DynRefImpl(DataRefImpl DynP, const OwningType *Owner)
2104 , OwningObject(Owner) {}
2106 template<support::endianness target_endianness, bool is64Bits>
2107 inline bool DynRefImpl<target_endianness, is64Bits>
2108 ::operator==(const DynRefImpl &Other) const {
2109 return DynPimpl == Other.DynPimpl;
2112 template<support::endianness target_endianness, bool is64Bits>
2113 inline bool DynRefImpl<target_endianness, is64Bits>
2114 ::operator <(const DynRefImpl &Other) const {
2115 return DynPimpl < Other.DynPimpl;
2118 template<support::endianness target_endianness, bool is64Bits>
2119 inline error_code DynRefImpl<target_endianness, is64Bits>
2120 ::getNext(DynRefImpl &Result) const {
2121 return OwningObject->getDynNext(DynPimpl, Result);
2124 template<support::endianness target_endianness, bool is64Bits>
2125 inline int64_t DynRefImpl<target_endianness, is64Bits>
2127 return OwningObject->getDyn(DynPimpl)->d_tag;
2130 template<support::endianness target_endianness, bool is64Bits>
2131 inline uint64_t DynRefImpl<target_endianness, is64Bits>
2133 return OwningObject->getDyn(DynPimpl)->d_un.d_val;
2136 template<support::endianness target_endianness, bool is64Bits>
2137 inline uint64_t DynRefImpl<target_endianness, is64Bits>
2139 return OwningObject->getDyn(DynPimpl)->d_un.d_ptr;
2142 template<support::endianness target_endianness, bool is64Bits>
2143 inline DataRefImpl DynRefImpl<target_endianness, is64Bits>
2144 ::getRawDataRefImpl() const {
2148 /// This is a generic interface for retrieving GNU symbol version
2149 /// information from an ELFObjectFile.
2150 static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
2151 const SymbolRef &Sym,
2154 // Little-endian 32-bit
2155 if (const ELFObjectFile<support::little, false> *ELFObj =
2156 dyn_cast<ELFObjectFile<support::little, false> >(Obj))
2157 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2159 // Big-endian 32-bit
2160 if (const ELFObjectFile<support::big, false> *ELFObj =
2161 dyn_cast<ELFObjectFile<support::big, false> >(Obj))
2162 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2164 // Little-endian 64-bit
2165 if (const ELFObjectFile<support::little, true> *ELFObj =
2166 dyn_cast<ELFObjectFile<support::little, true> >(Obj))
2167 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2169 // Big-endian 64-bit
2170 if (const ELFObjectFile<support::big, true> *ELFObj =
2171 dyn_cast<ELFObjectFile<support::big, true> >(Obj))
2172 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2174 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");