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;
391 template<support::endianness target_endianness, bool is64Bits>
392 class ELFObjectFile : public ObjectFile {
393 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
395 typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
396 typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
397 typedef Elf_Dyn_Impl<target_endianness, is64Bits> Elf_Dyn;
398 typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel;
399 typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela;
400 typedef Elf_Verdef_Impl<target_endianness, is64Bits> Elf_Verdef;
401 typedef Elf_Verdaux_Impl<target_endianness, is64Bits> Elf_Verdaux;
402 typedef Elf_Verneed_Impl<target_endianness, is64Bits> Elf_Verneed;
403 typedef Elf_Vernaux_Impl<target_endianness, is64Bits> Elf_Vernaux;
404 typedef Elf_Versym_Impl<target_endianness, is64Bits> Elf_Versym;
405 typedef DynRefImpl<target_endianness, is64Bits> DynRef;
406 typedef content_iterator<DynRef> dyn_iterator;
410 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
411 Elf_Half e_type; // Type of file (see ET_*)
412 Elf_Half e_machine; // Required architecture for this file (see EM_*)
413 Elf_Word e_version; // Must be equal to 1
414 Elf_Addr e_entry; // Address to jump to in order to start program
415 Elf_Off e_phoff; // Program header table's file offset, in bytes
416 Elf_Off e_shoff; // Section header table's file offset, in bytes
417 Elf_Word e_flags; // Processor-specific flags
418 Elf_Half e_ehsize; // Size of ELF header, in bytes
419 Elf_Half e_phentsize;// Size of an entry in the program header table
420 Elf_Half e_phnum; // Number of entries in the program header table
421 Elf_Half e_shentsize;// Size of an entry in the section header table
422 Elf_Half e_shnum; // Number of entries in the section header table
423 Elf_Half e_shstrndx; // Section header table index of section name
425 bool checkMagic() const {
426 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
428 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
429 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
431 // This flag is used for classof, to distinguish ELFObjectFile from
432 // its subclass. If more subclasses will be created, this flag will
433 // have to become an enum.
434 bool isDyldELFObject;
437 typedef SmallVector<const Elf_Shdr*, 1> Sections_t;
438 typedef DenseMap<unsigned, unsigned> IndexMap_t;
439 typedef DenseMap<const Elf_Shdr*, SmallVector<uint32_t, 1> > RelocMap_t;
441 const Elf_Ehdr *Header;
442 const Elf_Shdr *SectionHeaderTable;
443 const Elf_Shdr *dot_shstrtab_sec; // Section header string table.
444 const Elf_Shdr *dot_strtab_sec; // Symbol header string table.
445 const Elf_Shdr *dot_dynstr_sec; // Dynamic symbol string table.
447 // SymbolTableSections[0] always points to the dynamic string table section
448 // header, or NULL if there is no dynamic string table.
449 Sections_t SymbolTableSections;
450 IndexMap_t SymbolTableSectionsIndexMap;
451 DenseMap<const Elf_Sym*, ELF::Elf64_Word> ExtendedSymbolTable;
453 const Elf_Shdr *dot_dynamic_sec; // .dynamic
454 const Elf_Shdr *dot_gnu_version_sec; // .gnu.version
455 const Elf_Shdr *dot_gnu_version_r_sec; // .gnu.version_r
456 const Elf_Shdr *dot_gnu_version_d_sec; // .gnu.version_d
458 // Pointer to SONAME entry in dynamic string table
459 // This is set the first time getLoadName is called.
460 mutable const char *dt_soname;
462 // Records for each version index the corresponding Verdef or Vernaux entry.
463 // This is filled the first time LoadVersionMap() is called.
464 class VersionMapEntry : public PointerIntPair<const void*, 1> {
466 // If the integer is 0, this is an Elf_Verdef*.
467 // If the integer is 1, this is an Elf_Vernaux*.
468 VersionMapEntry() : PointerIntPair<const void*, 1>(NULL, 0) { }
469 VersionMapEntry(const Elf_Verdef *verdef)
470 : PointerIntPair<const void*, 1>(verdef, 0) { }
471 VersionMapEntry(const Elf_Vernaux *vernaux)
472 : PointerIntPair<const void*, 1>(vernaux, 1) { }
473 bool isNull() const { return getPointer() == NULL; }
474 bool isVerdef() const { return !isNull() && getInt() == 0; }
475 bool isVernaux() const { return !isNull() && getInt() == 1; }
476 const Elf_Verdef *getVerdef() const {
477 return isVerdef() ? (const Elf_Verdef*)getPointer() : NULL;
479 const Elf_Vernaux *getVernaux() const {
480 return isVernaux() ? (const Elf_Vernaux*)getPointer() : NULL;
483 mutable SmallVector<VersionMapEntry, 16> VersionMap;
484 void LoadVersionDefs(const Elf_Shdr *sec) const;
485 void LoadVersionNeeds(const Elf_Shdr *ec) const;
486 void LoadVersionMap() const;
488 /// @brief Map sections to an array of relocation sections that reference
489 /// them sorted by section index.
490 RelocMap_t SectionRelocMap;
492 /// @brief Get the relocation section that contains \a Rel.
493 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
494 return getSection(Rel.w.b);
497 bool isRelocationHasAddend(DataRefImpl Rel) const;
499 const T *getEntry(uint16_t Section, uint32_t Entry) const;
501 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
502 const Elf_Shdr *getSection(DataRefImpl index) const;
503 const Elf_Shdr *getSection(uint32_t index) const;
504 const Elf_Rel *getRel(DataRefImpl Rel) const;
505 const Elf_Rela *getRela(DataRefImpl Rela) const;
506 const char *getString(uint32_t section, uint32_t offset) const;
507 const char *getString(const Elf_Shdr *section, uint32_t offset) const;
508 error_code getSymbolVersion(const Elf_Shdr *section,
511 bool &IsDefault) const;
512 void VerifyStrTab(const Elf_Shdr *sh) const;
515 const Elf_Sym *getSymbol(DataRefImpl Symb) const; // FIXME: Should be private?
516 void validateSymbol(DataRefImpl Symb) const;
519 error_code getSymbolName(const Elf_Shdr *section,
521 StringRef &Res) const;
522 error_code getSectionName(const Elf_Shdr *section,
523 StringRef &Res) const;
524 const Elf_Dyn *getDyn(DataRefImpl DynData) const;
525 error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
526 bool &IsDefault) const;
528 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
529 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
530 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
531 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
532 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
533 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
534 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
535 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
536 virtual error_code getSymbolSection(DataRefImpl Symb,
537 section_iterator &Res) const;
539 friend class DynRefImpl<target_endianness, is64Bits>;
540 virtual error_code getDynNext(DataRefImpl DynData, DynRef &Result) const;
542 virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const;
543 virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const;
545 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
546 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
547 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
548 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
549 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
550 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
551 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
552 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
553 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
554 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
556 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
557 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
558 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
560 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
561 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
563 virtual error_code getRelocationNext(DataRefImpl Rel,
564 RelocationRef &Res) const;
565 virtual error_code getRelocationAddress(DataRefImpl Rel,
566 uint64_t &Res) const;
567 virtual error_code getRelocationOffset(DataRefImpl Rel,
568 uint64_t &Res) const;
569 virtual error_code getRelocationSymbol(DataRefImpl Rel,
570 SymbolRef &Res) const;
571 virtual error_code getRelocationType(DataRefImpl Rel,
572 uint64_t &Res) const;
573 virtual error_code getRelocationTypeName(DataRefImpl Rel,
574 SmallVectorImpl<char> &Result) const;
575 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
577 virtual error_code getRelocationValueString(DataRefImpl Rel,
578 SmallVectorImpl<char> &Result) const;
581 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
582 virtual symbol_iterator begin_symbols() const;
583 virtual symbol_iterator end_symbols() const;
585 virtual symbol_iterator begin_dynamic_symbols() const;
586 virtual symbol_iterator end_dynamic_symbols() const;
588 virtual section_iterator begin_sections() const;
589 virtual section_iterator end_sections() const;
591 virtual library_iterator begin_libraries_needed() const;
592 virtual library_iterator end_libraries_needed() const;
594 virtual dyn_iterator begin_dynamic_table() const;
595 virtual dyn_iterator end_dynamic_table() const;
597 virtual uint8_t getBytesInAddress() const;
598 virtual StringRef getFileFormatName() const;
599 virtual StringRef getObjectType() const { return "ELF"; }
600 virtual unsigned getArch() const;
601 virtual StringRef getLoadName() const;
602 virtual error_code getSectionContents(const Elf_Shdr *sec,
603 StringRef &Res) const;
605 uint64_t getNumSections() const;
606 uint64_t getStringTableIndex() const;
607 ELF::Elf64_Word getSymbolTableIndex(const Elf_Sym *symb) const;
608 const Elf_Shdr *getSection(const Elf_Sym *symb) const;
609 const Elf_Shdr *getElfSection(section_iterator &It) const;
610 const Elf_Sym *getElfSymbol(symbol_iterator &It) const;
612 // Methods for type inquiry through isa, cast, and dyn_cast
613 bool isDyldType() const { return isDyldELFObject; }
614 static inline bool classof(const Binary *v) {
615 return v->getType() == getELFType(target_endianness == support::little,
618 static inline bool classof(const ELFObjectFile *v) { return true; }
621 // Iterate through the version definitions, and place each Elf_Verdef
622 // in the VersionMap according to its index.
623 template<support::endianness target_endianness, bool is64Bits>
624 void ELFObjectFile<target_endianness, is64Bits>::
625 LoadVersionDefs(const Elf_Shdr *sec) const {
626 unsigned vd_size = sec->sh_size; // Size of section in bytes
627 unsigned vd_count = sec->sh_info; // Number of Verdef entries
628 const char *sec_start = (const char*)base() + sec->sh_offset;
629 const char *sec_end = sec_start + vd_size;
630 // The first Verdef entry is at the start of the section.
631 const char *p = sec_start;
632 for (unsigned i = 0; i < vd_count; i++) {
633 if (p + sizeof(Elf_Verdef) > sec_end)
634 report_fatal_error("Section ended unexpectedly while scanning "
635 "version definitions.");
636 const Elf_Verdef *vd = reinterpret_cast<const Elf_Verdef *>(p);
637 if (vd->vd_version != ELF::VER_DEF_CURRENT)
638 report_fatal_error("Unexpected verdef version");
639 size_t index = vd->vd_ndx & ELF::VERSYM_VERSION;
640 if (index >= VersionMap.size())
641 VersionMap.resize(index+1);
642 VersionMap[index] = VersionMapEntry(vd);
647 // Iterate through the versions needed section, and place each Elf_Vernaux
648 // in the VersionMap according to its index.
649 template<support::endianness target_endianness, bool is64Bits>
650 void ELFObjectFile<target_endianness, is64Bits>::
651 LoadVersionNeeds(const Elf_Shdr *sec) const {
652 unsigned vn_size = sec->sh_size; // Size of section in bytes
653 unsigned vn_count = sec->sh_info; // Number of Verneed entries
654 const char *sec_start = (const char*)base() + sec->sh_offset;
655 const char *sec_end = sec_start + vn_size;
656 // The first Verneed entry is at the start of the section.
657 const char *p = sec_start;
658 for (unsigned i = 0; i < vn_count; i++) {
659 if (p + sizeof(Elf_Verneed) > sec_end)
660 report_fatal_error("Section ended unexpectedly while scanning "
661 "version needed records.");
662 const Elf_Verneed *vn = reinterpret_cast<const Elf_Verneed *>(p);
663 if (vn->vn_version != ELF::VER_NEED_CURRENT)
664 report_fatal_error("Unexpected verneed version");
665 // Iterate through the Vernaux entries
666 const char *paux = p + vn->vn_aux;
667 for (unsigned j = 0; j < vn->vn_cnt; j++) {
668 if (paux + sizeof(Elf_Vernaux) > sec_end)
669 report_fatal_error("Section ended unexpected while scanning auxiliary "
670 "version needed records.");
671 const Elf_Vernaux *vna = reinterpret_cast<const Elf_Vernaux *>(paux);
672 size_t index = vna->vna_other & ELF::VERSYM_VERSION;
673 if (index >= VersionMap.size())
674 VersionMap.resize(index+1);
675 VersionMap[index] = VersionMapEntry(vna);
676 paux += vna->vna_next;
682 template<support::endianness target_endianness, bool is64Bits>
683 void ELFObjectFile<target_endianness, is64Bits>::LoadVersionMap() const {
684 // If there is no dynamic symtab or version table, there is nothing to do.
685 if (SymbolTableSections[0] == NULL || dot_gnu_version_sec == NULL)
688 // Has the VersionMap already been loaded?
689 if (VersionMap.size() > 0)
692 // The first two version indexes are reserved.
693 // Index 0 is LOCAL, index 1 is GLOBAL.
694 VersionMap.push_back(VersionMapEntry());
695 VersionMap.push_back(VersionMapEntry());
697 if (dot_gnu_version_d_sec)
698 LoadVersionDefs(dot_gnu_version_d_sec);
700 if (dot_gnu_version_r_sec)
701 LoadVersionNeeds(dot_gnu_version_r_sec);
704 template<support::endianness target_endianness, bool is64Bits>
705 void ELFObjectFile<target_endianness, is64Bits>
706 ::validateSymbol(DataRefImpl Symb) const {
707 const Elf_Sym *symb = getSymbol(Symb);
708 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
709 // FIXME: We really need to do proper error handling in the case of an invalid
710 // input file. Because we don't use exceptions, I think we'll just pass
711 // an error object around.
713 && SymbolTableSection
714 && symb >= (const Elf_Sym*)(base()
715 + SymbolTableSection->sh_offset)
716 && symb < (const Elf_Sym*)(base()
717 + SymbolTableSection->sh_offset
718 + SymbolTableSection->sh_size)))
719 // FIXME: Proper error handling.
720 report_fatal_error("Symb must point to a valid symbol!");
723 template<support::endianness target_endianness, bool is64Bits>
724 error_code ELFObjectFile<target_endianness, is64Bits>
725 ::getSymbolNext(DataRefImpl Symb,
726 SymbolRef &Result) const {
727 validateSymbol(Symb);
728 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
731 // Check to see if we are at the end of this symbol table.
732 if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
733 // We are at the end. If there are other symbol tables, jump to them.
734 // If the symbol table is .dynsym, we are iterating dynamic symbols,
735 // and there is only one table of these.
738 Symb.d.a = 1; // The 0th symbol in ELF is fake.
740 // Otherwise return the terminator.
741 if (Symb.d.b == 0 || Symb.d.b >= SymbolTableSections.size()) {
742 Symb.d.a = std::numeric_limits<uint32_t>::max();
743 Symb.d.b = std::numeric_limits<uint32_t>::max();
747 Result = SymbolRef(Symb, this);
748 return object_error::success;
751 template<support::endianness target_endianness, bool is64Bits>
752 error_code ELFObjectFile<target_endianness, is64Bits>
753 ::getSymbolName(DataRefImpl Symb,
754 StringRef &Result) const {
755 validateSymbol(Symb);
756 const Elf_Sym *symb = getSymbol(Symb);
757 return getSymbolName(SymbolTableSections[Symb.d.b], symb, Result);
760 template<support::endianness target_endianness, bool is64Bits>
761 error_code ELFObjectFile<target_endianness, is64Bits>
762 ::getSymbolVersion(SymbolRef SymRef,
764 bool &IsDefault) const {
765 DataRefImpl Symb = SymRef.getRawDataRefImpl();
766 validateSymbol(Symb);
767 const Elf_Sym *symb = getSymbol(Symb);
768 return getSymbolVersion(SymbolTableSections[Symb.d.b], symb,
772 template<support::endianness target_endianness, bool is64Bits>
773 ELF::Elf64_Word ELFObjectFile<target_endianness, is64Bits>
774 ::getSymbolTableIndex(const Elf_Sym *symb) const {
775 if (symb->st_shndx == ELF::SHN_XINDEX)
776 return ExtendedSymbolTable.lookup(symb);
777 return symb->st_shndx;
780 template<support::endianness target_endianness, bool is64Bits>
781 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
782 ELFObjectFile<target_endianness, is64Bits>
783 ::getSection(const Elf_Sym *symb) const {
784 if (symb->st_shndx == ELF::SHN_XINDEX)
785 return getSection(ExtendedSymbolTable.lookup(symb));
786 if (symb->st_shndx >= ELF::SHN_LORESERVE)
788 return getSection(symb->st_shndx);
791 template<support::endianness target_endianness, bool is64Bits>
792 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
793 ELFObjectFile<target_endianness, is64Bits>
794 ::getElfSection(section_iterator &It) const {
795 llvm::object::DataRefImpl ShdrRef = It->getRawDataRefImpl();
796 return reinterpret_cast<const Elf_Shdr *>(ShdrRef.p);
799 template<support::endianness target_endianness, bool is64Bits>
800 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym *
801 ELFObjectFile<target_endianness, is64Bits>
802 ::getElfSymbol(symbol_iterator &It) const {
803 return getSymbol(It->getRawDataRefImpl());
806 template<support::endianness target_endianness, bool is64Bits>
807 error_code ELFObjectFile<target_endianness, is64Bits>
808 ::getSymbolFileOffset(DataRefImpl Symb,
809 uint64_t &Result) const {
810 validateSymbol(Symb);
811 const Elf_Sym *symb = getSymbol(Symb);
812 const Elf_Shdr *Section;
813 switch (getSymbolTableIndex(symb)) {
814 case ELF::SHN_COMMON:
815 // Unintialized symbols have no offset in the object file
817 Result = UnknownAddressOrSize;
818 return object_error::success;
820 Result = symb->st_value;
821 return object_error::success;
822 default: Section = getSection(symb);
825 switch (symb->getType()) {
826 case ELF::STT_SECTION:
827 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
828 return object_error::success;
830 case ELF::STT_OBJECT:
831 case ELF::STT_NOTYPE:
832 Result = symb->st_value +
833 (Section ? Section->sh_offset : 0);
834 return object_error::success;
836 Result = UnknownAddressOrSize;
837 return object_error::success;
841 template<support::endianness target_endianness, bool is64Bits>
842 error_code ELFObjectFile<target_endianness, is64Bits>
843 ::getSymbolAddress(DataRefImpl Symb,
844 uint64_t &Result) const {
845 validateSymbol(Symb);
846 const Elf_Sym *symb = getSymbol(Symb);
847 const Elf_Shdr *Section;
848 switch (getSymbolTableIndex(symb)) {
849 case ELF::SHN_COMMON:
851 Result = UnknownAddressOrSize;
852 return object_error::success;
854 Result = symb->st_value;
855 return object_error::success;
856 default: Section = getSection(symb);
859 switch (symb->getType()) {
860 case ELF::STT_SECTION:
861 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
862 return object_error::success;
864 case ELF::STT_OBJECT:
865 case ELF::STT_NOTYPE:
866 Result = symb->st_value + (Section ? Section->sh_addr : 0);
867 return object_error::success;
869 Result = UnknownAddressOrSize;
870 return object_error::success;
874 template<support::endianness target_endianness, bool is64Bits>
875 error_code ELFObjectFile<target_endianness, is64Bits>
876 ::getSymbolSize(DataRefImpl Symb,
877 uint64_t &Result) const {
878 validateSymbol(Symb);
879 const Elf_Sym *symb = getSymbol(Symb);
880 if (symb->st_size == 0)
881 Result = UnknownAddressOrSize;
882 Result = symb->st_size;
883 return object_error::success;
886 template<support::endianness target_endianness, bool is64Bits>
887 error_code ELFObjectFile<target_endianness, is64Bits>
888 ::getSymbolNMTypeChar(DataRefImpl Symb,
889 char &Result) const {
890 validateSymbol(Symb);
891 const Elf_Sym *symb = getSymbol(Symb);
892 const Elf_Shdr *Section = getSection(symb);
897 switch (Section->sh_type) {
898 case ELF::SHT_PROGBITS:
899 case ELF::SHT_DYNAMIC:
900 switch (Section->sh_flags) {
901 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
903 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
906 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
907 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
911 case ELF::SHT_NOBITS: ret = 'b';
915 switch (getSymbolTableIndex(symb)) {
920 case ELF::SHN_ABS: ret = 'a'; break;
921 case ELF::SHN_COMMON: ret = 'c'; break;
924 switch (symb->getBinding()) {
925 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
927 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
930 if (symb->getType() == ELF::STT_OBJECT)
936 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
938 if (error_code ec = getSymbolName(Symb, name))
940 Result = StringSwitch<char>(name)
941 .StartsWith(".debug", 'N')
942 .StartsWith(".note", 'n')
944 return object_error::success;
948 return object_error::success;
951 template<support::endianness target_endianness, bool is64Bits>
952 error_code ELFObjectFile<target_endianness, is64Bits>
953 ::getSymbolType(DataRefImpl Symb,
954 SymbolRef::Type &Result) const {
955 validateSymbol(Symb);
956 const Elf_Sym *symb = getSymbol(Symb);
958 switch (symb->getType()) {
959 case ELF::STT_NOTYPE:
960 Result = SymbolRef::ST_Unknown;
962 case ELF::STT_SECTION:
963 Result = SymbolRef::ST_Debug;
966 Result = SymbolRef::ST_File;
969 Result = SymbolRef::ST_Function;
971 case ELF::STT_OBJECT:
972 case ELF::STT_COMMON:
974 Result = SymbolRef::ST_Data;
977 Result = SymbolRef::ST_Other;
980 return object_error::success;
983 template<support::endianness target_endianness, bool is64Bits>
984 error_code ELFObjectFile<target_endianness, is64Bits>
985 ::getSymbolFlags(DataRefImpl Symb,
986 uint32_t &Result) const {
987 validateSymbol(Symb);
988 const Elf_Sym *symb = getSymbol(Symb);
990 Result = SymbolRef::SF_None;
992 if (symb->getBinding() != ELF::STB_LOCAL)
993 Result |= SymbolRef::SF_Global;
995 if (symb->getBinding() == ELF::STB_WEAK)
996 Result |= SymbolRef::SF_Weak;
998 if (symb->st_shndx == ELF::SHN_ABS)
999 Result |= SymbolRef::SF_Absolute;
1001 if (symb->getType() == ELF::STT_FILE ||
1002 symb->getType() == ELF::STT_SECTION)
1003 Result |= SymbolRef::SF_FormatSpecific;
1005 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1006 Result |= SymbolRef::SF_Undefined;
1008 if (symb->getType() == ELF::STT_COMMON ||
1009 getSymbolTableIndex(symb) == ELF::SHN_COMMON)
1010 Result |= SymbolRef::SF_Common;
1012 if (symb->getType() == ELF::STT_TLS)
1013 Result |= SymbolRef::SF_ThreadLocal;
1015 return object_error::success;
1018 template<support::endianness target_endianness, bool is64Bits>
1019 error_code ELFObjectFile<target_endianness, is64Bits>
1020 ::getSymbolSection(DataRefImpl Symb,
1021 section_iterator &Res) const {
1022 validateSymbol(Symb);
1023 const Elf_Sym *symb = getSymbol(Symb);
1024 const Elf_Shdr *sec = getSection(symb);
1026 Res = end_sections();
1029 Sec.p = reinterpret_cast<intptr_t>(sec);
1030 Res = section_iterator(SectionRef(Sec, this));
1032 return object_error::success;
1035 template<support::endianness target_endianness, bool is64Bits>
1036 error_code ELFObjectFile<target_endianness, is64Bits>
1037 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
1038 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
1039 sec += Header->e_shentsize;
1040 Sec.p = reinterpret_cast<intptr_t>(sec);
1041 Result = SectionRef(Sec, this);
1042 return object_error::success;
1045 template<support::endianness target_endianness, bool is64Bits>
1046 error_code ELFObjectFile<target_endianness, is64Bits>
1047 ::getSectionName(DataRefImpl Sec,
1048 StringRef &Result) const {
1049 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1050 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
1051 return object_error::success;
1054 template<support::endianness target_endianness, bool is64Bits>
1055 error_code ELFObjectFile<target_endianness, is64Bits>
1056 ::getSectionAddress(DataRefImpl Sec,
1057 uint64_t &Result) const {
1058 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1059 Result = sec->sh_addr;
1060 return object_error::success;
1063 template<support::endianness target_endianness, bool is64Bits>
1064 error_code ELFObjectFile<target_endianness, is64Bits>
1065 ::getSectionSize(DataRefImpl Sec,
1066 uint64_t &Result) const {
1067 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1068 Result = sec->sh_size;
1069 return object_error::success;
1072 template<support::endianness target_endianness, bool is64Bits>
1073 error_code ELFObjectFile<target_endianness, is64Bits>
1074 ::getSectionContents(DataRefImpl Sec,
1075 StringRef &Result) const {
1076 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1077 const char *start = (const char*)base() + sec->sh_offset;
1078 Result = StringRef(start, sec->sh_size);
1079 return object_error::success;
1082 template<support::endianness target_endianness, bool is64Bits>
1083 error_code ELFObjectFile<target_endianness, is64Bits>
1084 ::getSectionContents(const Elf_Shdr *Sec,
1085 StringRef &Result) const {
1086 const char *start = (const char*)base() + Sec->sh_offset;
1087 Result = StringRef(start, Sec->sh_size);
1088 return object_error::success;
1091 template<support::endianness target_endianness, bool is64Bits>
1092 error_code ELFObjectFile<target_endianness, is64Bits>
1093 ::getSectionAlignment(DataRefImpl Sec,
1094 uint64_t &Result) const {
1095 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1096 Result = sec->sh_addralign;
1097 return object_error::success;
1100 template<support::endianness target_endianness, bool is64Bits>
1101 error_code ELFObjectFile<target_endianness, is64Bits>
1102 ::isSectionText(DataRefImpl Sec,
1103 bool &Result) const {
1104 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1105 if (sec->sh_flags & ELF::SHF_EXECINSTR)
1109 return object_error::success;
1112 template<support::endianness target_endianness, bool is64Bits>
1113 error_code ELFObjectFile<target_endianness, is64Bits>
1114 ::isSectionData(DataRefImpl Sec,
1115 bool &Result) const {
1116 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1117 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1118 && sec->sh_type == ELF::SHT_PROGBITS)
1122 return object_error::success;
1125 template<support::endianness target_endianness, bool is64Bits>
1126 error_code ELFObjectFile<target_endianness, is64Bits>
1127 ::isSectionBSS(DataRefImpl Sec,
1128 bool &Result) const {
1129 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1130 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1131 && sec->sh_type == ELF::SHT_NOBITS)
1135 return object_error::success;
1138 template<support::endianness target_endianness, bool is64Bits>
1139 error_code ELFObjectFile<target_endianness, is64Bits>
1140 ::isSectionRequiredForExecution(DataRefImpl Sec,
1141 bool &Result) const {
1142 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1143 if (sec->sh_flags & ELF::SHF_ALLOC)
1147 return object_error::success;
1150 template<support::endianness target_endianness, bool is64Bits>
1151 error_code ELFObjectFile<target_endianness, is64Bits>
1152 ::isSectionVirtual(DataRefImpl Sec,
1153 bool &Result) const {
1154 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1155 if (sec->sh_type == ELF::SHT_NOBITS)
1159 return object_error::success;
1162 template<support::endianness target_endianness, bool is64Bits>
1163 error_code ELFObjectFile<target_endianness, is64Bits>::isSectionZeroInit(DataRefImpl Sec,
1164 bool &Result) const {
1165 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1166 // For ELF, all zero-init sections are virtual (that is, they occupy no space
1167 // in the object image) and vice versa.
1168 if (sec->sh_flags & ELF::SHT_NOBITS)
1172 return object_error::success;
1175 template<support::endianness target_endianness, bool is64Bits>
1176 error_code ELFObjectFile<target_endianness, is64Bits>
1177 ::sectionContainsSymbol(DataRefImpl Sec,
1179 bool &Result) const {
1180 // FIXME: Unimplemented.
1182 return object_error::success;
1185 template<support::endianness target_endianness, bool is64Bits>
1186 relocation_iterator ELFObjectFile<target_endianness, is64Bits>
1187 ::getSectionRelBegin(DataRefImpl Sec) const {
1188 DataRefImpl RelData;
1189 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1190 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1191 if (sec != 0 && ittr != SectionRelocMap.end()) {
1192 RelData.w.a = getSection(ittr->second[0])->sh_info;
1193 RelData.w.b = ittr->second[0];
1196 return relocation_iterator(RelocationRef(RelData, this));
1199 template<support::endianness target_endianness, bool is64Bits>
1200 relocation_iterator ELFObjectFile<target_endianness, is64Bits>
1201 ::getSectionRelEnd(DataRefImpl Sec) const {
1202 DataRefImpl RelData;
1203 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1204 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1205 if (sec != 0 && ittr != SectionRelocMap.end()) {
1206 // Get the index of the last relocation section for this section.
1207 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
1208 const Elf_Shdr *relocsec = getSection(relocsecindex);
1209 RelData.w.a = relocsec->sh_info;
1210 RelData.w.b = relocsecindex;
1211 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
1213 return relocation_iterator(RelocationRef(RelData, this));
1217 template<support::endianness target_endianness, bool is64Bits>
1218 error_code ELFObjectFile<target_endianness, is64Bits>
1219 ::getRelocationNext(DataRefImpl Rel,
1220 RelocationRef &Result) const {
1222 const Elf_Shdr *relocsec = getSection(Rel.w.b);
1223 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
1224 // We have reached the end of the relocations for this section. See if there
1225 // is another relocation section.
1226 typename RelocMap_t::mapped_type relocseclist =
1227 SectionRelocMap.lookup(getSection(Rel.w.a));
1229 // Do a binary search for the current reloc section index (which must be
1230 // present). Then get the next one.
1231 typename RelocMap_t::mapped_type::const_iterator loc =
1232 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
1235 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
1236 // to the end iterator.
1237 if (loc != relocseclist.end()) {
1242 Result = RelocationRef(Rel, this);
1243 return object_error::success;
1246 template<support::endianness target_endianness, bool is64Bits>
1247 error_code ELFObjectFile<target_endianness, is64Bits>
1248 ::getRelocationSymbol(DataRefImpl Rel,
1249 SymbolRef &Result) const {
1251 const Elf_Shdr *sec = getSection(Rel.w.b);
1252 switch (sec->sh_type) {
1254 report_fatal_error("Invalid section type in Rel!");
1255 case ELF::SHT_REL : {
1256 symbolIdx = getRel(Rel)->getSymbol();
1259 case ELF::SHT_RELA : {
1260 symbolIdx = getRela(Rel)->getSymbol();
1264 DataRefImpl SymbolData;
1265 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
1266 if (it == SymbolTableSectionsIndexMap.end())
1267 report_fatal_error("Relocation symbol table not found!");
1268 SymbolData.d.a = symbolIdx;
1269 SymbolData.d.b = it->second;
1270 Result = SymbolRef(SymbolData, this);
1271 return object_error::success;
1274 template<support::endianness target_endianness, bool is64Bits>
1275 error_code ELFObjectFile<target_endianness, is64Bits>
1276 ::getRelocationAddress(DataRefImpl Rel,
1277 uint64_t &Result) const {
1279 const Elf_Shdr *sec = getSection(Rel.w.b);
1280 switch (sec->sh_type) {
1282 report_fatal_error("Invalid section type in Rel!");
1283 case ELF::SHT_REL : {
1284 offset = getRel(Rel)->r_offset;
1287 case ELF::SHT_RELA : {
1288 offset = getRela(Rel)->r_offset;
1294 return object_error::success;
1297 template<support::endianness target_endianness, bool is64Bits>
1298 error_code ELFObjectFile<target_endianness, is64Bits>
1299 ::getRelocationOffset(DataRefImpl Rel,
1300 uint64_t &Result) const {
1302 const Elf_Shdr *sec = getSection(Rel.w.b);
1303 switch (sec->sh_type) {
1305 report_fatal_error("Invalid section type in Rel!");
1306 case ELF::SHT_REL : {
1307 offset = getRel(Rel)->r_offset;
1310 case ELF::SHT_RELA : {
1311 offset = getRela(Rel)->r_offset;
1316 Result = offset - sec->sh_addr;
1317 return object_error::success;
1320 template<support::endianness target_endianness, bool is64Bits>
1321 error_code ELFObjectFile<target_endianness, is64Bits>
1322 ::getRelocationType(DataRefImpl Rel,
1323 uint64_t &Result) const {
1324 const Elf_Shdr *sec = getSection(Rel.w.b);
1325 switch (sec->sh_type) {
1327 report_fatal_error("Invalid section type in Rel!");
1328 case ELF::SHT_REL : {
1329 Result = getRel(Rel)->getType();
1332 case ELF::SHT_RELA : {
1333 Result = getRela(Rel)->getType();
1337 return object_error::success;
1340 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
1341 case ELF::enum: res = #enum; break;
1343 template<support::endianness target_endianness, bool is64Bits>
1344 error_code ELFObjectFile<target_endianness, is64Bits>
1345 ::getRelocationTypeName(DataRefImpl Rel,
1346 SmallVectorImpl<char> &Result) const {
1347 const Elf_Shdr *sec = getSection(Rel.w.b);
1350 switch (sec->sh_type) {
1352 return object_error::parse_failed;
1353 case ELF::SHT_REL : {
1354 type = getRel(Rel)->getType();
1357 case ELF::SHT_RELA : {
1358 type = getRela(Rel)->getType();
1362 switch (Header->e_machine) {
1363 case ELF::EM_X86_64:
1365 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
1366 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
1367 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
1368 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
1369 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
1370 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
1371 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
1372 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
1373 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
1374 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
1375 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
1376 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
1377 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
1378 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
1379 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
1380 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
1381 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
1382 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
1383 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
1384 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
1385 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
1386 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
1387 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
1388 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
1389 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
1390 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
1391 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
1392 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
1393 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
1394 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
1395 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
1396 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
1403 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
1404 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
1405 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
1406 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
1407 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
1408 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
1409 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
1410 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
1411 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
1412 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
1413 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
1414 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
1415 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
1416 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
1417 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
1418 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
1419 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
1420 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
1421 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
1422 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
1423 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
1424 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
1425 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
1426 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
1427 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
1428 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
1429 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
1430 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
1431 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
1432 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
1433 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
1434 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
1435 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
1436 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
1437 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
1438 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
1439 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
1440 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
1441 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
1442 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
1449 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_NONE);
1450 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PC24);
1451 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32);
1452 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32);
1453 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G0);
1454 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS16);
1455 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS12);
1456 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ABS5);
1457 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS8);
1458 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL32);
1459 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_CALL);
1460 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC8);
1461 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BREL_ADJ);
1462 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESC);
1463 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_SWI8);
1464 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_XPC25);
1465 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_XPC22);
1466 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPMOD32);
1467 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPOFF32);
1468 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_TPOFF32);
1469 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_COPY);
1470 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GLOB_DAT);
1471 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP_SLOT);
1472 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_RELATIVE);
1473 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF32);
1474 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_PREL);
1475 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL);
1476 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32);
1477 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_CALL);
1478 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP24);
1479 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP24);
1480 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_ABS);
1481 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_7_0);
1482 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_15_8);
1483 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_23_15);
1484 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SBREL_11_0_NC);
1485 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_19_12_NC);
1486 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_27_20_CK);
1487 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET1);
1488 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL31);
1489 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_V4BX);
1490 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET2);
1491 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PREL31);
1492 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_ABS_NC);
1493 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_ABS);
1494 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_PREL_NC);
1495 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_PREL);
1496 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_ABS_NC);
1497 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_ABS);
1498 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_PREL_NC);
1499 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_PREL);
1500 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP19);
1501 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP6);
1502 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ALU_PREL_11_0);
1503 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC12);
1504 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32_NOI);
1505 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32_NOI);
1506 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0_NC);
1507 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0);
1508 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1_NC);
1509 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1);
1510 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G2);
1511 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G1);
1512 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G2);
1513 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G0);
1514 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G1);
1515 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G2);
1516 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G0);
1517 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G1);
1518 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G2);
1519 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0_NC);
1520 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0);
1521 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1_NC);
1522 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1);
1523 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G2);
1524 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G0);
1525 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G1);
1526 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G2);
1527 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G0);
1528 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G1);
1529 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G2);
1530 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G0);
1531 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G1);
1532 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G2);
1533 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL_NC);
1534 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_BREL);
1535 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL);
1536 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL_NC);
1537 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_BREL);
1538 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL);
1539 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GOTDESC);
1540 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_CALL);
1541 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESCSEQ);
1542 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_CALL);
1543 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32_ABS);
1544 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_ABS);
1545 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_PREL);
1546 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL12);
1547 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF12);
1548 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTRELAX);
1549 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTENTRY);
1550 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTINHERIT);
1551 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP11);
1552 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP8);
1553 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GD32);
1554 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDM32);
1555 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO32);
1556 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE32);
1557 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE32);
1558 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO12);
1559 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE12);
1560 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE12GP);
1561 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_0);
1562 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_1);
1563 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_2);
1564 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_3);
1565 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_4);
1566 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_5);
1567 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_6);
1568 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_7);
1569 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_8);
1570 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_9);
1571 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_10);
1572 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_11);
1573 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_12);
1574 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_13);
1575 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_14);
1576 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_15);
1577 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ME_TOO);
1578 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ16);
1579 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ32);
1584 case ELF::EM_HEXAGON:
1586 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_NONE);
1587 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL);
1588 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL);
1589 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL);
1590 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_LO16);
1591 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HI16);
1592 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32);
1593 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16);
1594 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8);
1595 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_0);
1596 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_1);
1597 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_2);
1598 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_3);
1599 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HL16);
1600 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL);
1601 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL);
1602 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B32_PCREL_X);
1603 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_6_X);
1604 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL_X);
1605 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL_X);
1606 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL_X);
1607 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL_X);
1608 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL_X);
1609 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16_X);
1610 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_12_X);
1611 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_11_X);
1612 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_10_X);
1613 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_9_X);
1614 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8_X);
1615 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_7_X);
1616 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_X);
1617 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_PCREL);
1618 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_COPY);
1619 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GLOB_DAT);
1620 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_JMP_SLOT);
1621 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_RELATIVE);
1622 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_PLT_B22_PCREL);
1623 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_LO16);
1624 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_HI16);
1625 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32);
1626 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_LO16);
1627 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_HI16);
1628 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32);
1629 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16);
1630 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPMOD_32);
1631 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_LO16);
1632 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_HI16);
1633 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32);
1634 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16);
1635 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_PLT_B22_PCREL);
1636 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_LO16);
1637 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_HI16);
1638 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32);
1639 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16);
1640 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_LO16);
1641 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_HI16);
1642 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32);
1643 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_LO16);
1644 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_HI16);
1645 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32);
1646 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16);
1647 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_LO16);
1648 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_HI16);
1649 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32);
1650 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16);
1651 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_PCREL_X);
1652 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32_6_X);
1653 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_16_X);
1654 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_11_X);
1655 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32_6_X);
1656 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16_X);
1657 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_11_X);
1658 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32_6_X);
1659 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16_X);
1660 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_11_X);
1661 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32_6_X);
1662 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16_X);
1663 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_11_X);
1664 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32_6_X);
1665 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_16_X);
1666 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32_6_X);
1667 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16_X);
1668 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_11_X);
1669 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32_6_X);
1670 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16_X);
1671 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_11_X);
1679 Result.append(res.begin(), res.end());
1680 return object_error::success;
1683 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
1685 template<support::endianness target_endianness, bool is64Bits>
1686 error_code ELFObjectFile<target_endianness, is64Bits>
1687 ::getRelocationAdditionalInfo(DataRefImpl Rel,
1688 int64_t &Result) const {
1689 const Elf_Shdr *sec = getSection(Rel.w.b);
1690 switch (sec->sh_type) {
1692 report_fatal_error("Invalid section type in Rel!");
1693 case ELF::SHT_REL : {
1695 return object_error::success;
1697 case ELF::SHT_RELA : {
1698 Result = getRela(Rel)->r_addend;
1699 return object_error::success;
1704 template<support::endianness target_endianness, bool is64Bits>
1705 error_code ELFObjectFile<target_endianness, is64Bits>
1706 ::getRelocationValueString(DataRefImpl Rel,
1707 SmallVectorImpl<char> &Result) const {
1708 const Elf_Shdr *sec = getSection(Rel.w.b);
1712 uint16_t symbol_index = 0;
1713 switch (sec->sh_type) {
1715 return object_error::parse_failed;
1716 case ELF::SHT_REL : {
1717 type = getRel(Rel)->getType();
1718 symbol_index = getRel(Rel)->getSymbol();
1719 // TODO: Read implicit addend from section data.
1722 case ELF::SHT_RELA : {
1723 type = getRela(Rel)->getType();
1724 symbol_index = getRela(Rel)->getSymbol();
1725 addend = getRela(Rel)->r_addend;
1729 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1731 if (error_code ec = getSymbolName(getSection(sec->sh_link), symb, symname))
1733 switch (Header->e_machine) {
1734 case ELF::EM_X86_64:
1736 case ELF::R_X86_64_32S:
1739 case ELF::R_X86_64_PC32: {
1741 raw_string_ostream fmt(fmtbuf);
1742 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1744 Result.append(fmtbuf.begin(), fmtbuf.end());
1752 case ELF::EM_HEXAGON:
1759 Result.append(res.begin(), res.end());
1760 return object_error::success;
1763 // Verify that the last byte in the string table in a null.
1764 template<support::endianness target_endianness, bool is64Bits>
1765 void ELFObjectFile<target_endianness, is64Bits>
1766 ::VerifyStrTab(const Elf_Shdr *sh) const {
1767 const char *strtab = (const char*)base() + sh->sh_offset;
1768 if (strtab[sh->sh_size - 1] != 0)
1769 // FIXME: Proper error handling.
1770 report_fatal_error("String table must end with a null terminator!");
1773 template<support::endianness target_endianness, bool is64Bits>
1774 ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object
1776 : ObjectFile(getELFType(target_endianness == support::little, is64Bits),
1778 , isDyldELFObject(false)
1779 , SectionHeaderTable(0)
1780 , dot_shstrtab_sec(0)
1783 , dot_dynamic_sec(0)
1784 , dot_gnu_version_sec(0)
1785 , dot_gnu_version_r_sec(0)
1786 , dot_gnu_version_d_sec(0)
1790 const uint64_t FileSize = Data->getBufferSize();
1792 if (sizeof(Elf_Ehdr) > FileSize)
1793 // FIXME: Proper error handling.
1794 report_fatal_error("File too short!");
1796 Header = reinterpret_cast<const Elf_Ehdr *>(base());
1798 if (Header->e_shoff == 0)
1801 const uint64_t SectionTableOffset = Header->e_shoff;
1803 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize)
1804 // FIXME: Proper error handling.
1805 report_fatal_error("Section header table goes past end of file!");
1807 // The getNumSections() call below depends on SectionHeaderTable being set.
1808 SectionHeaderTable =
1809 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
1810 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
1812 if (SectionTableOffset + SectionTableSize > FileSize)
1813 // FIXME: Proper error handling.
1814 report_fatal_error("Section table goes past end of file!");
1816 // To find the symbol tables we walk the section table to find SHT_SYMTAB.
1817 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
1818 const Elf_Shdr* sh = SectionHeaderTable;
1820 // Reserve SymbolTableSections[0] for .dynsym
1821 SymbolTableSections.push_back(NULL);
1823 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
1824 switch (sh->sh_type) {
1825 case ELF::SHT_SYMTAB_SHNDX: {
1826 if (SymbolTableSectionHeaderIndex)
1827 // FIXME: Proper error handling.
1828 report_fatal_error("More than one .symtab_shndx!");
1829 SymbolTableSectionHeaderIndex = sh;
1832 case ELF::SHT_SYMTAB: {
1833 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
1834 SymbolTableSections.push_back(sh);
1837 case ELF::SHT_DYNSYM: {
1838 if (SymbolTableSections[0] != NULL)
1839 // FIXME: Proper error handling.
1840 report_fatal_error("More than one .dynsym!");
1841 SymbolTableSectionsIndexMap[i] = 0;
1842 SymbolTableSections[0] = sh;
1846 case ELF::SHT_RELA: {
1847 SectionRelocMap[getSection(sh->sh_info)].push_back(i);
1850 case ELF::SHT_DYNAMIC: {
1851 if (dot_dynamic_sec != NULL)
1852 // FIXME: Proper error handling.
1853 report_fatal_error("More than one .dynamic!");
1854 dot_dynamic_sec = sh;
1857 case ELF::SHT_GNU_versym: {
1858 if (dot_gnu_version_sec != NULL)
1859 // FIXME: Proper error handling.
1860 report_fatal_error("More than one .gnu.version section!");
1861 dot_gnu_version_sec = sh;
1864 case ELF::SHT_GNU_verdef: {
1865 if (dot_gnu_version_d_sec != NULL)
1866 // FIXME: Proper error handling.
1867 report_fatal_error("More than one .gnu.version_d section!");
1868 dot_gnu_version_d_sec = sh;
1871 case ELF::SHT_GNU_verneed: {
1872 if (dot_gnu_version_r_sec != NULL)
1873 // FIXME: Proper error handling.
1874 report_fatal_error("More than one .gnu.version_r section!");
1875 dot_gnu_version_r_sec = sh;
1882 // Sort section relocation lists by index.
1883 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
1884 e = SectionRelocMap.end(); i != e; ++i) {
1885 std::sort(i->second.begin(), i->second.end());
1888 // Get string table sections.
1889 dot_shstrtab_sec = getSection(getStringTableIndex());
1890 if (dot_shstrtab_sec) {
1891 // Verify that the last byte in the string table in a null.
1892 VerifyStrTab(dot_shstrtab_sec);
1895 // Merge this into the above loop.
1896 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
1897 *e = i + getNumSections() * Header->e_shentsize;
1898 i != e; i += Header->e_shentsize) {
1899 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
1900 if (sh->sh_type == ELF::SHT_STRTAB) {
1901 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
1902 if (SectionName == ".strtab") {
1903 if (dot_strtab_sec != 0)
1904 // FIXME: Proper error handling.
1905 report_fatal_error("Already found section named .strtab!");
1906 dot_strtab_sec = sh;
1907 VerifyStrTab(dot_strtab_sec);
1908 } else if (SectionName == ".dynstr") {
1909 if (dot_dynstr_sec != 0)
1910 // FIXME: Proper error handling.
1911 report_fatal_error("Already found section named .dynstr!");
1912 dot_dynstr_sec = sh;
1913 VerifyStrTab(dot_dynstr_sec);
1918 // Build symbol name side-mapping if there is one.
1919 if (SymbolTableSectionHeaderIndex) {
1920 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
1921 SymbolTableSectionHeaderIndex->sh_offset);
1923 for (symbol_iterator si = begin_symbols(),
1924 se = end_symbols(); si != se; si.increment(ec)) {
1926 report_fatal_error("Fewer extended symbol table entries than symbols!");
1927 if (*ShndxTable != ELF::SHN_UNDEF)
1928 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
1934 template<support::endianness target_endianness, bool is64Bits>
1935 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1936 ::begin_symbols() const {
1937 DataRefImpl SymbolData;
1938 if (SymbolTableSections.size() <= 1) {
1939 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1940 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1942 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
1943 SymbolData.d.b = 1; // The 0th table is .dynsym
1945 return symbol_iterator(SymbolRef(SymbolData, this));
1948 template<support::endianness target_endianness, bool is64Bits>
1949 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1950 ::end_symbols() const {
1951 DataRefImpl SymbolData;
1952 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1953 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1954 return symbol_iterator(SymbolRef(SymbolData, this));
1957 template<support::endianness target_endianness, bool is64Bits>
1958 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1959 ::begin_dynamic_symbols() const {
1960 DataRefImpl SymbolData;
1961 if (SymbolTableSections[0] == NULL) {
1962 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1963 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1965 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
1966 SymbolData.d.b = 0; // The 0th table is .dynsym
1968 return symbol_iterator(SymbolRef(SymbolData, this));
1971 template<support::endianness target_endianness, bool is64Bits>
1972 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1973 ::end_dynamic_symbols() const {
1974 DataRefImpl SymbolData;
1975 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1976 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1977 return symbol_iterator(SymbolRef(SymbolData, this));
1980 template<support::endianness target_endianness, bool is64Bits>
1981 section_iterator ELFObjectFile<target_endianness, is64Bits>
1982 ::begin_sections() const {
1984 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
1985 return section_iterator(SectionRef(ret, this));
1988 template<support::endianness target_endianness, bool is64Bits>
1989 section_iterator ELFObjectFile<target_endianness, is64Bits>
1990 ::end_sections() const {
1992 ret.p = reinterpret_cast<intptr_t>(base()
1994 + (Header->e_shentsize*getNumSections()));
1995 return section_iterator(SectionRef(ret, this));
1998 template<support::endianness target_endianness, bool is64Bits>
1999 typename ELFObjectFile<target_endianness, is64Bits>::dyn_iterator
2000 ELFObjectFile<target_endianness, is64Bits>::begin_dynamic_table() const {
2001 DataRefImpl DynData;
2002 if (dot_dynamic_sec == NULL || dot_dynamic_sec->sh_size == 0) {
2003 DynData.d.a = std::numeric_limits<uint32_t>::max();
2007 return dyn_iterator(DynRef(DynData, this));
2010 template<support::endianness target_endianness, bool is64Bits>
2011 typename ELFObjectFile<target_endianness, is64Bits>::dyn_iterator
2012 ELFObjectFile<target_endianness, is64Bits>
2013 ::end_dynamic_table() const {
2014 DataRefImpl DynData;
2015 DynData.d.a = std::numeric_limits<uint32_t>::max();
2016 return dyn_iterator(DynRef(DynData, this));
2019 template<support::endianness target_endianness, bool is64Bits>
2020 error_code ELFObjectFile<target_endianness, is64Bits>
2021 ::getDynNext(DataRefImpl DynData,
2022 DynRef &Result) const {
2025 // Check to see if we are at the end of .dynamic
2026 if (DynData.d.a >= dot_dynamic_sec->getEntityCount()) {
2027 // We are at the end. Return the terminator.
2028 DynData.d.a = std::numeric_limits<uint32_t>::max();
2031 Result = DynRef(DynData, this);
2032 return object_error::success;
2035 template<support::endianness target_endianness, bool is64Bits>
2037 ELFObjectFile<target_endianness, is64Bits>::getLoadName() const {
2039 // Find the DT_SONAME entry
2040 dyn_iterator it = begin_dynamic_table();
2041 dyn_iterator ie = end_dynamic_table();
2044 if (it->getTag() == ELF::DT_SONAME)
2048 report_fatal_error("dynamic table iteration failed");
2051 if (dot_dynstr_sec == NULL)
2052 report_fatal_error("Dynamic string table is missing");
2053 dt_soname = getString(dot_dynstr_sec, it->getVal());
2061 template<support::endianness target_endianness, bool is64Bits>
2062 library_iterator ELFObjectFile<target_endianness, is64Bits>
2063 ::begin_libraries_needed() const {
2064 // Find the first DT_NEEDED entry
2065 dyn_iterator i = begin_dynamic_table();
2066 dyn_iterator e = end_dynamic_table();
2069 if (i->getTag() == ELF::DT_NEEDED)
2073 report_fatal_error("dynamic table iteration failed");
2075 // Use the same DataRefImpl format as DynRef.
2076 return library_iterator(LibraryRef(i->getRawDataRefImpl(), this));
2079 template<support::endianness target_endianness, bool is64Bits>
2080 error_code ELFObjectFile<target_endianness, is64Bits>
2081 ::getLibraryNext(DataRefImpl Data,
2082 LibraryRef &Result) const {
2083 // Use the same DataRefImpl format as DynRef.
2084 dyn_iterator i = dyn_iterator(DynRef(Data, this));
2085 dyn_iterator e = end_dynamic_table();
2087 // Skip the current dynamic table entry.
2091 // TODO: proper error handling
2093 report_fatal_error("dynamic table iteration failed");
2096 // Find the next DT_NEEDED entry.
2098 if (i->getTag() == ELF::DT_NEEDED)
2102 report_fatal_error("dynamic table iteration failed");
2104 Result = LibraryRef(i->getRawDataRefImpl(), this);
2105 return object_error::success;
2108 template<support::endianness target_endianness, bool is64Bits>
2109 error_code ELFObjectFile<target_endianness, is64Bits>
2110 ::getLibraryPath(DataRefImpl Data, StringRef &Res) const {
2111 dyn_iterator i = dyn_iterator(DynRef(Data, this));
2112 if (i == end_dynamic_table())
2113 report_fatal_error("getLibraryPath() called on iterator end");
2115 if (i->getTag() != ELF::DT_NEEDED)
2116 report_fatal_error("Invalid library_iterator");
2118 // This uses .dynstr to lookup the name of the DT_NEEDED entry.
2119 // THis works as long as DT_STRTAB == .dynstr. This is true most of
2120 // the time, but the specification allows exceptions.
2121 // TODO: This should really use DT_STRTAB instead. Doing this requires
2122 // reading the program headers.
2123 if (dot_dynstr_sec == NULL)
2124 report_fatal_error("Dynamic string table is missing");
2125 Res = getString(dot_dynstr_sec, i->getVal());
2126 return object_error::success;
2129 template<support::endianness target_endianness, bool is64Bits>
2130 library_iterator ELFObjectFile<target_endianness, is64Bits>
2131 ::end_libraries_needed() const {
2132 dyn_iterator e = end_dynamic_table();
2133 // Use the same DataRefImpl format as DynRef.
2134 return library_iterator(LibraryRef(e->getRawDataRefImpl(), this));
2137 template<support::endianness target_endianness, bool is64Bits>
2138 uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const {
2139 return is64Bits ? 8 : 4;
2142 template<support::endianness target_endianness, bool is64Bits>
2143 StringRef ELFObjectFile<target_endianness, is64Bits>
2144 ::getFileFormatName() const {
2145 switch(Header->e_ident[ELF::EI_CLASS]) {
2146 case ELF::ELFCLASS32:
2147 switch(Header->e_machine) {
2149 return "ELF32-i386";
2150 case ELF::EM_X86_64:
2151 return "ELF32-x86-64";
2154 case ELF::EM_HEXAGON:
2155 return "ELF32-hexagon";
2157 return "ELF32-unknown";
2159 case ELF::ELFCLASS64:
2160 switch(Header->e_machine) {
2162 return "ELF64-i386";
2163 case ELF::EM_X86_64:
2164 return "ELF64-x86-64";
2166 return "ELF64-unknown";
2169 // FIXME: Proper error handling.
2170 report_fatal_error("Invalid ELFCLASS!");
2174 template<support::endianness target_endianness, bool is64Bits>
2175 unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const {
2176 switch(Header->e_machine) {
2179 case ELF::EM_X86_64:
2180 return Triple::x86_64;
2183 case ELF::EM_HEXAGON:
2184 return Triple::hexagon;
2186 return (target_endianness == support::little) ?
2187 Triple::mipsel : Triple::mips;
2189 return Triple::UnknownArch;
2193 template<support::endianness target_endianness, bool is64Bits>
2194 uint64_t ELFObjectFile<target_endianness, is64Bits>::getNumSections() const {
2195 assert(Header && "Header not initialized!");
2196 if (Header->e_shnum == ELF::SHN_UNDEF) {
2197 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
2198 return SectionHeaderTable->sh_size;
2200 return Header->e_shnum;
2203 template<support::endianness target_endianness, bool is64Bits>
2205 ELFObjectFile<target_endianness, is64Bits>::getStringTableIndex() const {
2206 if (Header->e_shnum == ELF::SHN_UNDEF) {
2207 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
2208 return SectionHeaderTable->sh_link;
2209 if (Header->e_shstrndx >= getNumSections())
2212 return Header->e_shstrndx;
2216 template<support::endianness target_endianness, bool is64Bits>
2217 template<typename T>
2219 ELFObjectFile<target_endianness, is64Bits>::getEntry(uint16_t Section,
2220 uint32_t Entry) const {
2221 return getEntry<T>(getSection(Section), Entry);
2224 template<support::endianness target_endianness, bool is64Bits>
2225 template<typename T>
2227 ELFObjectFile<target_endianness, is64Bits>::getEntry(const Elf_Shdr * Section,
2228 uint32_t Entry) const {
2229 return reinterpret_cast<const T *>(
2231 + Section->sh_offset
2232 + (Entry * Section->sh_entsize));
2235 template<support::endianness target_endianness, bool is64Bits>
2236 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym *
2237 ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const {
2238 return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
2241 template<support::endianness target_endianness, bool is64Bits>
2242 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Dyn *
2243 ELFObjectFile<target_endianness, is64Bits>::getDyn(DataRefImpl DynData) const {
2244 return getEntry<Elf_Dyn>(dot_dynamic_sec, DynData.d.a);
2247 template<support::endianness target_endianness, bool is64Bits>
2248 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel *
2249 ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const {
2250 return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
2253 template<support::endianness target_endianness, bool is64Bits>
2254 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela *
2255 ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const {
2256 return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
2259 template<support::endianness target_endianness, bool is64Bits>
2260 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
2261 ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const {
2262 const Elf_Shdr *sec = getSection(Symb.d.b);
2263 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
2264 // FIXME: Proper error handling.
2265 report_fatal_error("Invalid symbol table section!");
2269 template<support::endianness target_endianness, bool is64Bits>
2270 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
2271 ELFObjectFile<target_endianness, is64Bits>::getSection(uint32_t index) const {
2274 if (!SectionHeaderTable || index >= getNumSections())
2275 // FIXME: Proper error handling.
2276 report_fatal_error("Invalid section index!");
2278 return reinterpret_cast<const Elf_Shdr *>(
2279 reinterpret_cast<const char *>(SectionHeaderTable)
2280 + (index * Header->e_shentsize));
2283 template<support::endianness target_endianness, bool is64Bits>
2284 const char *ELFObjectFile<target_endianness, is64Bits>
2285 ::getString(uint32_t section,
2286 ELF::Elf32_Word offset) const {
2287 return getString(getSection(section), offset);
2290 template<support::endianness target_endianness, bool is64Bits>
2291 const char *ELFObjectFile<target_endianness, is64Bits>
2292 ::getString(const Elf_Shdr *section,
2293 ELF::Elf32_Word offset) const {
2294 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
2295 if (offset >= section->sh_size)
2296 // FIXME: Proper error handling.
2297 report_fatal_error("Symbol name offset outside of string table!");
2298 return (const char *)base() + section->sh_offset + offset;
2301 template<support::endianness target_endianness, bool is64Bits>
2302 error_code ELFObjectFile<target_endianness, is64Bits>
2303 ::getSymbolName(const Elf_Shdr *section,
2304 const Elf_Sym *symb,
2305 StringRef &Result) const {
2306 if (symb->st_name == 0) {
2307 const Elf_Shdr *section = getSection(symb);
2311 Result = getString(dot_shstrtab_sec, section->sh_name);
2312 return object_error::success;
2315 if (section == SymbolTableSections[0]) {
2316 // Symbol is in .dynsym, use .dynstr string table
2317 Result = getString(dot_dynstr_sec, symb->st_name);
2319 // Use the default symbol table name section.
2320 Result = getString(dot_strtab_sec, symb->st_name);
2322 return object_error::success;
2325 template<support::endianness target_endianness, bool is64Bits>
2326 error_code ELFObjectFile<target_endianness, is64Bits>
2327 ::getSectionName(const Elf_Shdr *section,
2328 StringRef &Result) const {
2329 Result = StringRef(getString(dot_shstrtab_sec, section->sh_name));
2330 return object_error::success;
2333 template<support::endianness target_endianness, bool is64Bits>
2334 error_code ELFObjectFile<target_endianness, is64Bits>
2335 ::getSymbolVersion(const Elf_Shdr *section,
2336 const Elf_Sym *symb,
2338 bool &IsDefault) const {
2339 // Handle non-dynamic symbols.
2340 if (section != SymbolTableSections[0]) {
2341 // Non-dynamic symbols can have versions in their names
2342 // A name of the form 'foo@V1' indicates version 'V1', non-default.
2343 // A name of the form 'foo@@V2' indicates version 'V2', default version.
2345 error_code ec = getSymbolName(section, symb, Name);
2346 if (ec != object_error::success)
2348 size_t atpos = Name.find('@');
2349 if (atpos == StringRef::npos) {
2352 return object_error::success;
2355 if (atpos < Name.size() && Name[atpos] == '@') {
2361 Version = Name.substr(atpos);
2362 return object_error::success;
2365 // This is a dynamic symbol. Look in the GNU symbol version table.
2366 if (dot_gnu_version_sec == NULL) {
2367 // No version table.
2370 return object_error::success;
2373 // Determine the position in the symbol table of this entry.
2374 const char *sec_start = (const char*)base() + section->sh_offset;
2375 size_t entry_index = ((const char*)symb - sec_start)/section->sh_entsize;
2377 // Get the corresponding version index entry
2378 const Elf_Versym *vs = getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index);
2379 size_t version_index = vs->vs_index & ELF::VERSYM_VERSION;
2381 // Special markers for unversioned symbols.
2382 if (version_index == ELF::VER_NDX_LOCAL ||
2383 version_index == ELF::VER_NDX_GLOBAL) {
2386 return object_error::success;
2389 // Lookup this symbol in the version table
2391 if (version_index >= VersionMap.size() || VersionMap[version_index].isNull())
2392 report_fatal_error("Symbol has version index without corresponding "
2393 "define or reference entry");
2394 const VersionMapEntry &entry = VersionMap[version_index];
2396 // Get the version name string
2398 if (entry.isVerdef()) {
2399 // The first Verdaux entry holds the name.
2400 name_offset = entry.getVerdef()->getAux()->vda_name;
2402 name_offset = entry.getVernaux()->vna_name;
2404 Version = getString(dot_dynstr_sec, name_offset);
2407 if (entry.isVerdef()) {
2408 IsDefault = !(vs->vs_index & ELF::VERSYM_HIDDEN);
2413 return object_error::success;
2416 template<support::endianness target_endianness, bool is64Bits>
2417 inline DynRefImpl<target_endianness, is64Bits>
2418 ::DynRefImpl(DataRefImpl DynP, const OwningType *Owner)
2420 , OwningObject(Owner) {}
2422 template<support::endianness target_endianness, bool is64Bits>
2423 inline bool DynRefImpl<target_endianness, is64Bits>
2424 ::operator==(const DynRefImpl &Other) const {
2425 return DynPimpl == Other.DynPimpl;
2428 template<support::endianness target_endianness, bool is64Bits>
2429 inline bool DynRefImpl<target_endianness, is64Bits>
2430 ::operator <(const DynRefImpl &Other) const {
2431 return DynPimpl < Other.DynPimpl;
2434 template<support::endianness target_endianness, bool is64Bits>
2435 inline error_code DynRefImpl<target_endianness, is64Bits>
2436 ::getNext(DynRefImpl &Result) const {
2437 return OwningObject->getDynNext(DynPimpl, Result);
2440 template<support::endianness target_endianness, bool is64Bits>
2441 inline int64_t DynRefImpl<target_endianness, is64Bits>
2443 return OwningObject->getDyn(DynPimpl)->d_tag;
2446 template<support::endianness target_endianness, bool is64Bits>
2447 inline uint64_t DynRefImpl<target_endianness, is64Bits>
2449 return OwningObject->getDyn(DynPimpl)->d_un.d_val;
2452 template<support::endianness target_endianness, bool is64Bits>
2453 inline uint64_t DynRefImpl<target_endianness, is64Bits>
2455 return OwningObject->getDyn(DynPimpl)->d_un.d_ptr;
2458 template<support::endianness target_endianness, bool is64Bits>
2459 inline DataRefImpl DynRefImpl<target_endianness, is64Bits>
2460 ::getRawDataRefImpl() const {
2464 /// This is a generic interface for retrieving GNU symbol version
2465 /// information from an ELFObjectFile.
2466 static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
2467 const SymbolRef &Sym,
2470 // Little-endian 32-bit
2471 if (const ELFObjectFile<support::little, false> *ELFObj =
2472 dyn_cast<ELFObjectFile<support::little, false> >(Obj))
2473 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2475 // Big-endian 32-bit
2476 if (const ELFObjectFile<support::big, false> *ELFObj =
2477 dyn_cast<ELFObjectFile<support::big, false> >(Obj))
2478 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2480 // Little-endian 64-bit
2481 if (const ELFObjectFile<support::little, true> *ELFObj =
2482 dyn_cast<ELFObjectFile<support::little, true> >(Obj))
2483 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2485 // Big-endian 64-bit
2486 if (const ELFObjectFile<support::big, true> *ELFObj =
2487 dyn_cast<ELFObjectFile<support::big, true> >(Obj))
2488 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2490 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");