1 //===- ELF.h - ELF object file implementation -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the ELFObjectFile template class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_OBJECT_ELF_H
15 #define LLVM_OBJECT_ELF_H
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/PointerIntPair.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/Support/Casting.h"
24 #include "llvm/Support/ELF.h"
25 #include "llvm/Support/Endian.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Support/raw_ostream.h"
36 using support::endianness;
38 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
40 static const endianness TargetEndianness = target_endianness;
41 static const std::size_t MaxAlignment = max_alignment;
42 static const bool Is64Bits = is64Bits;
45 template<typename T, int max_align>
46 struct MaximumAlignment {
47 enum {value = AlignOf<T>::Alignment > max_align ? max_align
48 : AlignOf<T>::Alignment};
51 // Subclasses of ELFObjectFile may need this for template instantiation
52 inline std::pair<unsigned char, unsigned char>
53 getElfArchType(MemoryBuffer *Object) {
54 if (Object->getBufferSize() < ELF::EI_NIDENT)
55 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE);
56 return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS]
57 , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]);
60 // Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
61 template<endianness target_endianness, std::size_t max_alignment>
62 struct ELFDataTypeTypedefHelperCommon {
63 typedef support::detail::packed_endian_specific_integral
64 <uint16_t, target_endianness,
65 MaximumAlignment<uint16_t, max_alignment>::value> Elf_Half;
66 typedef support::detail::packed_endian_specific_integral
67 <uint32_t, target_endianness,
68 MaximumAlignment<uint32_t, max_alignment>::value> Elf_Word;
69 typedef support::detail::packed_endian_specific_integral
70 <int32_t, target_endianness,
71 MaximumAlignment<int32_t, max_alignment>::value> Elf_Sword;
72 typedef support::detail::packed_endian_specific_integral
73 <uint64_t, target_endianness,
74 MaximumAlignment<uint64_t, max_alignment>::value> Elf_Xword;
75 typedef support::detail::packed_endian_specific_integral
76 <int64_t, target_endianness,
77 MaximumAlignment<int64_t, max_alignment>::value> Elf_Sxword;
81 struct ELFDataTypeTypedefHelper;
84 template<template<endianness, std::size_t, bool> class ELFT,
85 endianness TargetEndianness, std::size_t MaxAlign>
86 struct ELFDataTypeTypedefHelper<ELFT<TargetEndianness, MaxAlign, false> >
87 : ELFDataTypeTypedefHelperCommon<TargetEndianness, MaxAlign> {
88 typedef uint32_t value_type;
89 typedef support::detail::packed_endian_specific_integral
90 <value_type, TargetEndianness,
91 MaximumAlignment<value_type, MaxAlign>::value> Elf_Addr;
92 typedef support::detail::packed_endian_specific_integral
93 <value_type, TargetEndianness,
94 MaximumAlignment<value_type, MaxAlign>::value> Elf_Off;
98 template<template<endianness, std::size_t, bool> class ELFT,
99 endianness TargetEndianness, std::size_t MaxAlign>
100 struct ELFDataTypeTypedefHelper<ELFT<TargetEndianness, MaxAlign, true> >
101 : ELFDataTypeTypedefHelperCommon<TargetEndianness, MaxAlign> {
102 typedef uint64_t value_type;
103 typedef support::detail::packed_endian_specific_integral
104 <value_type, TargetEndianness,
105 MaximumAlignment<value_type, MaxAlign>::value> Elf_Addr;
106 typedef support::detail::packed_endian_specific_integral
107 <value_type, TargetEndianness,
108 MaximumAlignment<value_type, MaxAlign>::value> Elf_Off;
111 // I really don't like doing this, but the alternative is copypasta.
112 #define LLVM_ELF_IMPORT_TYPES(ELFT) \
113 typedef typename ELFDataTypeTypedefHelper <ELFT>::Elf_Addr Elf_Addr; \
114 typedef typename ELFDataTypeTypedefHelper <ELFT>::Elf_Off Elf_Off; \
115 typedef typename ELFDataTypeTypedefHelper <ELFT>::Elf_Half Elf_Half; \
116 typedef typename ELFDataTypeTypedefHelper <ELFT>::Elf_Word Elf_Word; \
117 typedef typename ELFDataTypeTypedefHelper <ELFT>::Elf_Sword Elf_Sword; \
118 typedef typename ELFDataTypeTypedefHelper <ELFT>::Elf_Xword Elf_Xword; \
119 typedef typename ELFDataTypeTypedefHelper <ELFT>::Elf_Sxword Elf_Sxword;
121 // This is required to get template types into a macro :(
122 #define LLVM_ELF_COMMA ,
126 struct Elf_Shdr_Base;
128 template<template<endianness, std::size_t, bool> class ELFT,
129 endianness TargetEndianness, std::size_t MaxAlign>
130 struct Elf_Shdr_Base<ELFT<TargetEndianness, MaxAlign, false> > {
131 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
132 MaxAlign LLVM_ELF_COMMA false>)
133 Elf_Word sh_name; // Section name (index into string table)
134 Elf_Word sh_type; // Section type (SHT_*)
135 Elf_Word sh_flags; // Section flags (SHF_*)
136 Elf_Addr sh_addr; // Address where section is to be loaded
137 Elf_Off sh_offset; // File offset of section data, in bytes
138 Elf_Word sh_size; // Size of section, in bytes
139 Elf_Word sh_link; // Section type-specific header table index link
140 Elf_Word sh_info; // Section type-specific extra information
141 Elf_Word sh_addralign;// Section address alignment
142 Elf_Word sh_entsize; // Size of records contained within the section
145 template<template<endianness, std::size_t, bool> class ELFT,
146 endianness TargetEndianness, std::size_t MaxAlign>
147 struct Elf_Shdr_Base<ELFT<TargetEndianness, MaxAlign, true> > {
148 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
149 MaxAlign LLVM_ELF_COMMA true>)
150 Elf_Word sh_name; // Section name (index into string table)
151 Elf_Word sh_type; // Section type (SHT_*)
152 Elf_Xword sh_flags; // Section flags (SHF_*)
153 Elf_Addr sh_addr; // Address where section is to be loaded
154 Elf_Off sh_offset; // File offset of section data, in bytes
155 Elf_Xword sh_size; // Size of section, in bytes
156 Elf_Word sh_link; // Section type-specific header table index link
157 Elf_Word sh_info; // Section type-specific extra information
158 Elf_Xword sh_addralign;// Section address alignment
159 Elf_Xword sh_entsize; // Size of records contained within the section
163 struct Elf_Shdr_Impl : Elf_Shdr_Base<ELFT> {
164 using Elf_Shdr_Base<ELFT>::sh_entsize;
165 using Elf_Shdr_Base<ELFT>::sh_size;
167 /// @brief Get the number of entities this section contains if it has any.
168 unsigned getEntityCount() const {
171 return sh_size / sh_entsize;
178 template<template<endianness, std::size_t, bool> class ELFT,
179 endianness TargetEndianness, std::size_t MaxAlign>
180 struct Elf_Sym_Base<ELFT<TargetEndianness, MaxAlign, false> > {
181 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
182 MaxAlign LLVM_ELF_COMMA false>)
183 Elf_Word st_name; // Symbol name (index into string table)
184 Elf_Addr st_value; // Value or address associated with the symbol
185 Elf_Word st_size; // Size of the symbol
186 unsigned char st_info; // Symbol's type and binding attributes
187 unsigned char st_other; // Must be zero; reserved
188 Elf_Half st_shndx; // Which section (header table index) it's defined in
191 template<template<endianness, std::size_t, bool> class ELFT,
192 endianness TargetEndianness, std::size_t MaxAlign>
193 struct Elf_Sym_Base<ELFT<TargetEndianness, MaxAlign, true> > {
194 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
195 MaxAlign LLVM_ELF_COMMA true>)
196 Elf_Word st_name; // Symbol name (index into string table)
197 unsigned char st_info; // Symbol's type and binding attributes
198 unsigned char st_other; // Must be zero; reserved
199 Elf_Half st_shndx; // Which section (header table index) it's defined in
200 Elf_Addr st_value; // Value or address associated with the symbol
201 Elf_Xword st_size; // Size of the symbol
205 struct Elf_Sym_Impl : Elf_Sym_Base<ELFT> {
206 using Elf_Sym_Base<ELFT>::st_info;
208 // These accessors and mutators correspond to the ELF32_ST_BIND,
209 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
210 unsigned char getBinding() const { return st_info >> 4; }
211 unsigned char getType() const { return st_info & 0x0f; }
212 void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
213 void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
214 void setBindingAndType(unsigned char b, unsigned char t) {
215 st_info = (b << 4) + (t & 0x0f);
219 /// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section
220 /// (.gnu.version). This structure is identical for ELF32 and ELF64.
222 struct Elf_Versym_Impl {
223 LLVM_ELF_IMPORT_TYPES(ELFT)
224 Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN)
228 struct Elf_Verdaux_Impl;
230 /// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section
231 /// (.gnu.version_d). This structure is identical for ELF32 and ELF64.
233 struct Elf_Verdef_Impl {
234 LLVM_ELF_IMPORT_TYPES(ELFT)
235 typedef Elf_Verdaux_Impl<ELFT> Elf_Verdaux;
236 Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT)
237 Elf_Half vd_flags; // Bitwise flags (VER_DEF_*)
238 Elf_Half vd_ndx; // Version index, used in .gnu.version entries
239 Elf_Half vd_cnt; // Number of Verdaux entries
240 Elf_Word vd_hash; // Hash of name
241 Elf_Word vd_aux; // Offset to the first Verdaux entry (in bytes)
242 Elf_Word vd_next; // Offset to the next Verdef entry (in bytes)
244 /// Get the first Verdaux entry for this Verdef.
245 const Elf_Verdaux *getAux() const {
246 return reinterpret_cast<const Elf_Verdaux*>((const char*)this + vd_aux);
250 /// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef
251 /// section (.gnu.version_d). This structure is identical for ELF32 and ELF64.
253 struct Elf_Verdaux_Impl {
254 LLVM_ELF_IMPORT_TYPES(ELFT)
255 Elf_Word vda_name; // Version name (offset in string table)
256 Elf_Word vda_next; // Offset to next Verdaux entry (in bytes)
259 /// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed
260 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
262 struct Elf_Verneed_Impl {
263 LLVM_ELF_IMPORT_TYPES(ELFT)
264 Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT)
265 Elf_Half vn_cnt; // Number of associated Vernaux entries
266 Elf_Word vn_file; // Library name (string table offset)
267 Elf_Word vn_aux; // Offset to first Vernaux entry (in bytes)
268 Elf_Word vn_next; // Offset to next Verneed entry (in bytes)
271 /// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed
272 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
274 struct Elf_Vernaux_Impl {
275 LLVM_ELF_IMPORT_TYPES(ELFT)
276 Elf_Word vna_hash; // Hash of dependency name
277 Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*)
278 Elf_Half vna_other; // Version index, used in .gnu.version entries
279 Elf_Word vna_name; // Dependency name
280 Elf_Word vna_next; // Offset to next Vernaux entry (in bytes)
283 /// Elf_Dyn_Base: This structure matches the form of entries in the dynamic
284 /// table section (.dynamic) look like.
288 template<template<endianness, std::size_t, bool> class ELFT,
289 endianness TargetEndianness, std::size_t MaxAlign>
290 struct Elf_Dyn_Base<ELFT<TargetEndianness, MaxAlign, false> > {
291 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
292 MaxAlign LLVM_ELF_COMMA false>)
300 template<template<endianness, std::size_t, bool> class ELFT,
301 endianness TargetEndianness, std::size_t MaxAlign>
302 struct Elf_Dyn_Base<ELFT<TargetEndianness, MaxAlign, true> > {
303 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
304 MaxAlign LLVM_ELF_COMMA true>)
312 /// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters.
314 struct Elf_Dyn_Impl : Elf_Dyn_Base<ELFT> {
315 using Elf_Dyn_Base<ELFT>::d_tag;
316 using Elf_Dyn_Base<ELFT>::d_un;
317 int64_t getTag() const { return d_tag; }
318 uint64_t getVal() const { return d_un.d_val; }
319 uint64_t getPtr() const { return d_un.ptr; }
322 // Elf_Rel: Elf Relocation
323 template<class ELFT, bool isRela>
326 template<template<endianness, std::size_t, bool> class ELFT,
327 endianness TargetEndianness, std::size_t MaxAlign>
328 struct Elf_Rel_Base<ELFT<TargetEndianness, MaxAlign, false>, false> {
329 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
330 MaxAlign LLVM_ELF_COMMA 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
335 template<template<endianness, std::size_t, bool> class ELFT,
336 endianness TargetEndianness, std::size_t MaxAlign>
337 struct Elf_Rel_Base<ELFT<TargetEndianness, MaxAlign, true>, false> {
338 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
339 MaxAlign LLVM_ELF_COMMA true>)
340 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
341 Elf_Xword r_info; // Symbol table index and type of relocation to apply
344 template<template<endianness, std::size_t, bool> class ELFT,
345 endianness TargetEndianness, std::size_t MaxAlign>
346 struct Elf_Rel_Base<ELFT<TargetEndianness, MaxAlign, false>, true> {
347 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
348 MaxAlign LLVM_ELF_COMMA false>)
349 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
350 Elf_Word r_info; // Symbol table index and type of relocation to apply
351 Elf_Sword r_addend; // Compute value for relocatable field by adding this
354 template<template<endianness, std::size_t, bool> class ELFT,
355 endianness TargetEndianness, std::size_t MaxAlign>
356 struct Elf_Rel_Base<ELFT<TargetEndianness, MaxAlign, true>, true> {
357 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
358 MaxAlign LLVM_ELF_COMMA true>)
359 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
360 Elf_Xword r_info; // Symbol table index and type of relocation to apply
361 Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
364 template<class ELFT, bool isRela>
367 template<template<endianness, std::size_t, bool> class ELFT,
368 endianness TargetEndianness, std::size_t MaxAlign, bool isRela>
369 struct Elf_Rel_Impl<ELFT<TargetEndianness, MaxAlign, true>, isRela>
370 : Elf_Rel_Base<ELFT<TargetEndianness, MaxAlign, true>, isRela> {
371 using Elf_Rel_Base<ELFT<TargetEndianness, MaxAlign, true>, isRela>::r_info;
372 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
373 MaxAlign LLVM_ELF_COMMA true>)
375 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
376 // and ELF64_R_INFO macros defined in the ELF specification:
377 uint32_t getSymbol() const { return (uint32_t) (r_info >> 32); }
378 uint32_t getType() const {
379 return (uint32_t) (r_info & 0xffffffffL);
381 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); }
382 void setType(uint32_t t) { setSymbolAndType(getSymbol(), t); }
383 void setSymbolAndType(uint32_t s, uint32_t t) {
384 r_info = ((uint64_t)s << 32) + (t&0xffffffffL);
388 template<template<endianness, std::size_t, bool> class ELFT,
389 endianness TargetEndianness, std::size_t MaxAlign, bool isRela>
390 struct Elf_Rel_Impl<ELFT<TargetEndianness, MaxAlign, false>, isRela>
391 : Elf_Rel_Base<ELFT<TargetEndianness, MaxAlign, false>, isRela> {
392 using Elf_Rel_Base<ELFT<TargetEndianness, MaxAlign, false>, isRela>::r_info;
393 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
394 MaxAlign LLVM_ELF_COMMA false>)
396 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
397 // and ELF32_R_INFO macros defined in the ELF specification:
398 uint32_t getSymbol() const { return (r_info >> 8); }
399 unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); }
400 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); }
401 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
402 void setSymbolAndType(uint32_t s, unsigned char t) {
403 r_info = (s << 8) + t;
408 struct Elf_Ehdr_Impl {
409 LLVM_ELF_IMPORT_TYPES(ELFT)
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]; }
433 struct Elf_Phdr_Impl;
435 template<template<endianness, std::size_t, bool> class ELFT,
436 endianness TargetEndianness, std::size_t MaxAlign>
437 struct Elf_Phdr_Impl<ELFT<TargetEndianness, MaxAlign, false> > {
438 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
439 MaxAlign LLVM_ELF_COMMA false>)
440 Elf_Word p_type; // Type of segment
441 Elf_Off p_offset; // FileOffset where segment is located, in bytes
442 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
443 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
444 Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
445 Elf_Word p_memsz; // Num. of bytes in mem image of segment (may be zero)
446 Elf_Word p_flags; // Segment flags
447 Elf_Word p_align; // Segment alignment constraint
450 template<template<endianness, std::size_t, bool> class ELFT,
451 endianness TargetEndianness, std::size_t MaxAlign>
452 struct Elf_Phdr_Impl<ELFT<TargetEndianness, MaxAlign, true> > {
453 LLVM_ELF_IMPORT_TYPES(ELFT<TargetEndianness LLVM_ELF_COMMA
454 MaxAlign LLVM_ELF_COMMA true>)
455 Elf_Word p_type; // Type of segment
456 Elf_Word p_flags; // Segment flags
457 Elf_Off p_offset; // FileOffset where segment is located, in bytes
458 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
459 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
460 Elf_Xword p_filesz; // Num. of bytes in file image of segment (may be zero)
461 Elf_Xword p_memsz; // Num. of bytes in mem image of segment (may be zero)
462 Elf_Xword p_align; // Segment alignment constraint
466 class ELFObjectFile : public ObjectFile {
467 LLVM_ELF_IMPORT_TYPES(ELFT)
470 /// \brief Iterate over constant sized entities.
472 class ELFEntityIterator {
474 typedef void difference_type;
475 typedef EntT value_type;
476 typedef std::forward_iterator_tag iterator_category;
477 typedef value_type &reference;
478 typedef value_type *pointer;
480 /// \brief Default construct iterator.
481 ELFEntityIterator() : EntitySize(0), Current(0) {}
482 ELFEntityIterator(uint64_t EntSize, const char *Start)
483 : EntitySize(EntSize)
486 reference operator *() {
487 assert(Current && "Attempted to dereference an invalid iterator!");
488 return *reinterpret_cast<pointer>(Current);
491 pointer operator ->() {
492 assert(Current && "Attempted to dereference an invalid iterator!");
493 return reinterpret_cast<pointer>(Current);
496 bool operator ==(const ELFEntityIterator &Other) {
497 return Current == Other.Current;
500 bool operator !=(const ELFEntityIterator &Other) {
501 return !(*this == Other);
504 ELFEntityIterator &operator ++() {
505 assert(Current && "Attempted to increment an invalid iterator!");
506 Current += EntitySize;
510 ELFEntityIterator operator ++(int) {
511 ELFEntityIterator Tmp = *this;
516 const char *get() const { return Current; }
519 const uint64_t EntitySize;
524 typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr;
525 typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
526 typedef Elf_Sym_Impl<ELFT> Elf_Sym;
527 typedef Elf_Dyn_Impl<ELFT> Elf_Dyn;
528 typedef Elf_Phdr_Impl<ELFT> Elf_Phdr;
529 typedef Elf_Rel_Impl<ELFT, false> Elf_Rel;
530 typedef Elf_Rel_Impl<ELFT, true> Elf_Rela;
531 typedef Elf_Verdef_Impl<ELFT> Elf_Verdef;
532 typedef Elf_Verdaux_Impl<ELFT> Elf_Verdaux;
533 typedef Elf_Verneed_Impl<ELFT> Elf_Verneed;
534 typedef Elf_Vernaux_Impl<ELFT> Elf_Vernaux;
535 typedef Elf_Versym_Impl<ELFT> Elf_Versym;
536 typedef ELFEntityIterator<const Elf_Dyn> dyn_iterator;
539 // This flag is used for classof, to distinguish ELFObjectFile from
540 // its subclass. If more subclasses will be created, this flag will
541 // have to become an enum.
542 bool isDyldELFObject;
545 typedef SmallVector<const Elf_Shdr*, 1> Sections_t;
546 typedef DenseMap<unsigned, unsigned> IndexMap_t;
547 typedef DenseMap<const Elf_Shdr*, SmallVector<uint32_t, 1> > RelocMap_t;
549 const Elf_Ehdr *Header;
550 const Elf_Shdr *SectionHeaderTable;
551 const Elf_Shdr *dot_shstrtab_sec; // Section header string table.
552 const Elf_Shdr *dot_strtab_sec; // Symbol header string table.
553 const Elf_Shdr *dot_dynstr_sec; // Dynamic symbol string table.
555 // SymbolTableSections[0] always points to the dynamic string table section
556 // header, or NULL if there is no dynamic string table.
557 Sections_t SymbolTableSections;
558 IndexMap_t SymbolTableSectionsIndexMap;
559 DenseMap<const Elf_Sym*, ELF::Elf64_Word> ExtendedSymbolTable;
561 const Elf_Shdr *dot_dynamic_sec; // .dynamic
562 const Elf_Shdr *dot_gnu_version_sec; // .gnu.version
563 const Elf_Shdr *dot_gnu_version_r_sec; // .gnu.version_r
564 const Elf_Shdr *dot_gnu_version_d_sec; // .gnu.version_d
566 // Pointer to SONAME entry in dynamic string table
567 // This is set the first time getLoadName is called.
568 mutable const char *dt_soname;
571 // Records for each version index the corresponding Verdef or Vernaux entry.
572 // This is filled the first time LoadVersionMap() is called.
573 class VersionMapEntry : public PointerIntPair<const void*, 1> {
575 // If the integer is 0, this is an Elf_Verdef*.
576 // If the integer is 1, this is an Elf_Vernaux*.
577 VersionMapEntry() : PointerIntPair<const void*, 1>(NULL, 0) { }
578 VersionMapEntry(const Elf_Verdef *verdef)
579 : PointerIntPair<const void*, 1>(verdef, 0) { }
580 VersionMapEntry(const Elf_Vernaux *vernaux)
581 : PointerIntPair<const void*, 1>(vernaux, 1) { }
582 bool isNull() const { return getPointer() == NULL; }
583 bool isVerdef() const { return !isNull() && getInt() == 0; }
584 bool isVernaux() const { return !isNull() && getInt() == 1; }
585 const Elf_Verdef *getVerdef() const {
586 return isVerdef() ? (const Elf_Verdef*)getPointer() : NULL;
588 const Elf_Vernaux *getVernaux() const {
589 return isVernaux() ? (const Elf_Vernaux*)getPointer() : NULL;
592 mutable SmallVector<VersionMapEntry, 16> VersionMap;
593 void LoadVersionDefs(const Elf_Shdr *sec) const;
594 void LoadVersionNeeds(const Elf_Shdr *ec) const;
595 void LoadVersionMap() const;
597 /// @brief Map sections to an array of relocation sections that reference
598 /// them sorted by section index.
599 RelocMap_t SectionRelocMap;
601 /// @brief Get the relocation section that contains \a Rel.
602 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
603 return getSection(Rel.w.b);
606 bool isRelocationHasAddend(DataRefImpl Rel) const;
608 const T *getEntry(uint16_t Section, uint32_t Entry) const;
610 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
611 const Elf_Shdr *getSection(DataRefImpl index) const;
612 const Elf_Shdr *getSection(uint32_t index) const;
613 const Elf_Rel *getRel(DataRefImpl Rel) const;
614 const Elf_Rela *getRela(DataRefImpl Rela) const;
615 const char *getString(uint32_t section, uint32_t offset) const;
616 const char *getString(const Elf_Shdr *section, uint32_t offset) const;
617 error_code getSymbolVersion(const Elf_Shdr *section,
620 bool &IsDefault) const;
621 void VerifyStrTab(const Elf_Shdr *sh) const;
624 const Elf_Sym *getSymbol(DataRefImpl Symb) const; // FIXME: Should be private?
625 void validateSymbol(DataRefImpl Symb) const;
628 error_code getSymbolName(const Elf_Shdr *section,
630 StringRef &Res) const;
631 error_code getSectionName(const Elf_Shdr *section,
632 StringRef &Res) const;
633 const Elf_Dyn *getDyn(DataRefImpl DynData) const;
634 error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
635 bool &IsDefault) const;
636 uint64_t getSymbolIndex(const Elf_Sym *sym) const;
638 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
639 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
640 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
641 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
642 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
643 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
644 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
645 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
646 virtual error_code getSymbolSection(DataRefImpl Symb,
647 section_iterator &Res) const;
648 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
650 virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const;
651 virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const;
653 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
654 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
655 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
656 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
657 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
658 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
659 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
660 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
661 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
662 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
664 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
665 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
666 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
667 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
669 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
670 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
672 virtual error_code getRelocationNext(DataRefImpl Rel,
673 RelocationRef &Res) const;
674 virtual error_code getRelocationAddress(DataRefImpl Rel,
675 uint64_t &Res) const;
676 virtual error_code getRelocationOffset(DataRefImpl Rel,
677 uint64_t &Res) const;
678 virtual error_code getRelocationSymbol(DataRefImpl Rel,
679 SymbolRef &Res) const;
680 virtual error_code getRelocationType(DataRefImpl Rel,
681 uint64_t &Res) const;
682 virtual error_code getRelocationTypeName(DataRefImpl Rel,
683 SmallVectorImpl<char> &Result) const;
684 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
686 virtual error_code getRelocationValueString(DataRefImpl Rel,
687 SmallVectorImpl<char> &Result) const;
690 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
691 virtual symbol_iterator begin_symbols() const;
692 virtual symbol_iterator end_symbols() const;
694 virtual symbol_iterator begin_dynamic_symbols() const;
695 virtual symbol_iterator end_dynamic_symbols() const;
697 virtual section_iterator begin_sections() const;
698 virtual section_iterator end_sections() const;
700 virtual library_iterator begin_libraries_needed() const;
701 virtual library_iterator end_libraries_needed() const;
703 dyn_iterator begin_dynamic_table() const;
704 dyn_iterator end_dynamic_table() const;
706 typedef ELFEntityIterator<const Elf_Rela> Elf_Rela_Iter;
707 typedef ELFEntityIterator<const Elf_Rel> Elf_Rel_Iter;
709 Elf_Rela_Iter beginELFRela(const Elf_Shdr *sec) const {
710 return Elf_Rela_Iter(sec->sh_entsize,
711 (const char *)(base() + sec->sh_offset));
714 Elf_Rela_Iter endELFRela(const Elf_Shdr *sec) const {
715 return Elf_Rela_Iter(sec->sh_entsize, (const char *)
716 (base() + sec->sh_offset + sec->sh_size));
719 Elf_Rel_Iter beginELFRel(const Elf_Shdr *sec) const {
720 return Elf_Rel_Iter(sec->sh_entsize,
721 (const char *)(base() + sec->sh_offset));
724 Elf_Rel_Iter endELFRel(const Elf_Shdr *sec) const {
725 return Elf_Rel_Iter(sec->sh_entsize, (const char *)
726 (base() + sec->sh_offset + sec->sh_size));
729 /// \brief Iterate over program header table.
730 typedef ELFEntityIterator<const Elf_Phdr> Elf_Phdr_Iter;
732 Elf_Phdr_Iter begin_program_headers() const {
733 return Elf_Phdr_Iter(Header->e_phentsize,
734 (const char*)base() + Header->e_phoff);
737 Elf_Phdr_Iter end_program_headers() const {
738 return Elf_Phdr_Iter(Header->e_phentsize,
739 (const char*)base() +
741 (Header->e_phnum * Header->e_phentsize));
744 virtual uint8_t getBytesInAddress() const;
745 virtual StringRef getFileFormatName() const;
746 virtual StringRef getObjectType() const { return "ELF"; }
747 virtual unsigned getArch() const;
748 virtual StringRef getLoadName() const;
749 virtual error_code getSectionContents(const Elf_Shdr *sec,
750 StringRef &Res) const;
752 uint64_t getNumSections() const;
753 uint64_t getStringTableIndex() const;
754 ELF::Elf64_Word getSymbolTableIndex(const Elf_Sym *symb) const;
755 const Elf_Shdr *getSection(const Elf_Sym *symb) const;
756 const Elf_Shdr *getElfSection(section_iterator &It) const;
757 const Elf_Sym *getElfSymbol(symbol_iterator &It) const;
758 const Elf_Sym *getElfSymbol(uint32_t index) const;
760 // Methods for type inquiry through isa, cast, and dyn_cast
761 bool isDyldType() const { return isDyldELFObject; }
762 static inline bool classof(const Binary *v) {
763 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
768 // Iterate through the version definitions, and place each Elf_Verdef
769 // in the VersionMap according to its index.
771 void ELFObjectFile<ELFT>::LoadVersionDefs(const Elf_Shdr *sec) const {
772 unsigned vd_size = sec->sh_size; // Size of section in bytes
773 unsigned vd_count = sec->sh_info; // Number of Verdef entries
774 const char *sec_start = (const char*)base() + sec->sh_offset;
775 const char *sec_end = sec_start + vd_size;
776 // The first Verdef entry is at the start of the section.
777 const char *p = sec_start;
778 for (unsigned i = 0; i < vd_count; i++) {
779 if (p + sizeof(Elf_Verdef) > sec_end)
780 report_fatal_error("Section ended unexpectedly while scanning "
781 "version definitions.");
782 const Elf_Verdef *vd = reinterpret_cast<const Elf_Verdef *>(p);
783 if (vd->vd_version != ELF::VER_DEF_CURRENT)
784 report_fatal_error("Unexpected verdef version");
785 size_t index = vd->vd_ndx & ELF::VERSYM_VERSION;
786 if (index >= VersionMap.size())
787 VersionMap.resize(index+1);
788 VersionMap[index] = VersionMapEntry(vd);
793 // Iterate through the versions needed section, and place each Elf_Vernaux
794 // in the VersionMap according to its index.
796 void ELFObjectFile<ELFT>::LoadVersionNeeds(const Elf_Shdr *sec) const {
797 unsigned vn_size = sec->sh_size; // Size of section in bytes
798 unsigned vn_count = sec->sh_info; // Number of Verneed entries
799 const char *sec_start = (const char*)base() + sec->sh_offset;
800 const char *sec_end = sec_start + vn_size;
801 // The first Verneed entry is at the start of the section.
802 const char *p = sec_start;
803 for (unsigned i = 0; i < vn_count; i++) {
804 if (p + sizeof(Elf_Verneed) > sec_end)
805 report_fatal_error("Section ended unexpectedly while scanning "
806 "version needed records.");
807 const Elf_Verneed *vn = reinterpret_cast<const Elf_Verneed *>(p);
808 if (vn->vn_version != ELF::VER_NEED_CURRENT)
809 report_fatal_error("Unexpected verneed version");
810 // Iterate through the Vernaux entries
811 const char *paux = p + vn->vn_aux;
812 for (unsigned j = 0; j < vn->vn_cnt; j++) {
813 if (paux + sizeof(Elf_Vernaux) > sec_end)
814 report_fatal_error("Section ended unexpected while scanning auxiliary "
815 "version needed records.");
816 const Elf_Vernaux *vna = reinterpret_cast<const Elf_Vernaux *>(paux);
817 size_t index = vna->vna_other & ELF::VERSYM_VERSION;
818 if (index >= VersionMap.size())
819 VersionMap.resize(index+1);
820 VersionMap[index] = VersionMapEntry(vna);
821 paux += vna->vna_next;
828 void ELFObjectFile<ELFT>::LoadVersionMap() const {
829 // If there is no dynamic symtab or version table, there is nothing to do.
830 if (SymbolTableSections[0] == NULL || dot_gnu_version_sec == NULL)
833 // Has the VersionMap already been loaded?
834 if (VersionMap.size() > 0)
837 // The first two version indexes are reserved.
838 // Index 0 is LOCAL, index 1 is GLOBAL.
839 VersionMap.push_back(VersionMapEntry());
840 VersionMap.push_back(VersionMapEntry());
842 if (dot_gnu_version_d_sec)
843 LoadVersionDefs(dot_gnu_version_d_sec);
845 if (dot_gnu_version_r_sec)
846 LoadVersionNeeds(dot_gnu_version_r_sec);
850 void ELFObjectFile<ELFT>::validateSymbol(DataRefImpl Symb) const {
851 const Elf_Sym *symb = getSymbol(Symb);
852 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
853 // FIXME: We really need to do proper error handling in the case of an invalid
854 // input file. Because we don't use exceptions, I think we'll just pass
855 // an error object around.
857 && SymbolTableSection
858 && symb >= (const Elf_Sym*)(base()
859 + SymbolTableSection->sh_offset)
860 && symb < (const Elf_Sym*)(base()
861 + SymbolTableSection->sh_offset
862 + SymbolTableSection->sh_size)))
863 // FIXME: Proper error handling.
864 report_fatal_error("Symb must point to a valid symbol!");
868 error_code ELFObjectFile<ELFT>::getSymbolNext(DataRefImpl Symb,
869 SymbolRef &Result) const {
870 validateSymbol(Symb);
871 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
874 // Check to see if we are at the end of this symbol table.
875 if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
876 // We are at the end. If there are other symbol tables, jump to them.
877 // If the symbol table is .dynsym, we are iterating dynamic symbols,
878 // and there is only one table of these.
881 Symb.d.a = 1; // The 0th symbol in ELF is fake.
883 // Otherwise return the terminator.
884 if (Symb.d.b == 0 || Symb.d.b >= SymbolTableSections.size()) {
885 Symb.d.a = std::numeric_limits<uint32_t>::max();
886 Symb.d.b = std::numeric_limits<uint32_t>::max();
890 Result = SymbolRef(Symb, this);
891 return object_error::success;
895 error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
896 StringRef &Result) const {
897 validateSymbol(Symb);
898 const Elf_Sym *symb = getSymbol(Symb);
899 return getSymbolName(SymbolTableSections[Symb.d.b], symb, Result);
903 error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
905 bool &IsDefault) const {
906 DataRefImpl Symb = SymRef.getRawDataRefImpl();
907 validateSymbol(Symb);
908 const Elf_Sym *symb = getSymbol(Symb);
909 return getSymbolVersion(SymbolTableSections[Symb.d.b], symb,
914 ELF::Elf64_Word ELFObjectFile<ELFT>
915 ::getSymbolTableIndex(const Elf_Sym *symb) const {
916 if (symb->st_shndx == ELF::SHN_XINDEX)
917 return ExtendedSymbolTable.lookup(symb);
918 return symb->st_shndx;
922 const typename ELFObjectFile<ELFT>::Elf_Shdr *
923 ELFObjectFile<ELFT>::getSection(const Elf_Sym *symb) const {
924 if (symb->st_shndx == ELF::SHN_XINDEX)
925 return getSection(ExtendedSymbolTable.lookup(symb));
926 if (symb->st_shndx >= ELF::SHN_LORESERVE)
928 return getSection(symb->st_shndx);
932 const typename ELFObjectFile<ELFT>::Elf_Shdr *
933 ELFObjectFile<ELFT>::getElfSection(section_iterator &It) const {
934 llvm::object::DataRefImpl ShdrRef = It->getRawDataRefImpl();
935 return reinterpret_cast<const Elf_Shdr *>(ShdrRef.p);
939 const typename ELFObjectFile<ELFT>::Elf_Sym *
940 ELFObjectFile<ELFT>::getElfSymbol(symbol_iterator &It) const {
941 return getSymbol(It->getRawDataRefImpl());
945 const typename ELFObjectFile<ELFT>::Elf_Sym *
946 ELFObjectFile<ELFT>::getElfSymbol(uint32_t index) const {
947 DataRefImpl SymbolData;
948 SymbolData.d.a = index;
950 return getSymbol(SymbolData);
954 error_code ELFObjectFile<ELFT>::getSymbolFileOffset(DataRefImpl Symb,
955 uint64_t &Result) const {
956 validateSymbol(Symb);
957 const Elf_Sym *symb = getSymbol(Symb);
958 const Elf_Shdr *Section;
959 switch (getSymbolTableIndex(symb)) {
960 case ELF::SHN_COMMON:
961 // Unintialized symbols have no offset in the object file
963 Result = UnknownAddressOrSize;
964 return object_error::success;
966 Result = symb->st_value;
967 return object_error::success;
968 default: Section = getSection(symb);
971 switch (symb->getType()) {
972 case ELF::STT_SECTION:
973 Result = Section ? Section->sh_offset : UnknownAddressOrSize;
974 return object_error::success;
976 case ELF::STT_OBJECT:
977 case ELF::STT_NOTYPE:
978 Result = symb->st_value +
979 (Section ? Section->sh_offset : 0);
980 return object_error::success;
982 Result = UnknownAddressOrSize;
983 return object_error::success;
988 error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
989 uint64_t &Result) const {
990 validateSymbol(Symb);
991 const Elf_Sym *symb = getSymbol(Symb);
992 const Elf_Shdr *Section;
993 switch (getSymbolTableIndex(symb)) {
994 case ELF::SHN_COMMON:
996 Result = UnknownAddressOrSize;
997 return object_error::success;
999 Result = symb->st_value;
1000 return object_error::success;
1001 default: Section = getSection(symb);
1004 switch (symb->getType()) {
1005 case ELF::STT_SECTION:
1006 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
1007 return object_error::success;
1009 case ELF::STT_OBJECT:
1010 case ELF::STT_NOTYPE:
1012 switch(Header->e_type) {
1015 IsRelocatable = false;
1018 IsRelocatable = true;
1020 Result = symb->st_value;
1021 if (IsRelocatable && Section != 0)
1022 Result += Section->sh_addr;
1023 return object_error::success;
1025 Result = UnknownAddressOrSize;
1026 return object_error::success;
1030 template<class ELFT>
1031 error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
1032 uint64_t &Result) const {
1033 validateSymbol(Symb);
1034 const Elf_Sym *symb = getSymbol(Symb);
1035 if (symb->st_size == 0)
1036 Result = UnknownAddressOrSize;
1037 Result = symb->st_size;
1038 return object_error::success;
1041 template<class ELFT>
1042 error_code ELFObjectFile<ELFT>::getSymbolNMTypeChar(DataRefImpl Symb,
1043 char &Result) const {
1044 validateSymbol(Symb);
1045 const Elf_Sym *symb = getSymbol(Symb);
1046 const Elf_Shdr *Section = getSection(symb);
1051 switch (Section->sh_type) {
1052 case ELF::SHT_PROGBITS:
1053 case ELF::SHT_DYNAMIC:
1054 switch (Section->sh_flags) {
1055 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
1057 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
1059 case ELF::SHF_ALLOC:
1060 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
1061 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
1065 case ELF::SHT_NOBITS: ret = 'b';
1069 switch (getSymbolTableIndex(symb)) {
1070 case ELF::SHN_UNDEF:
1074 case ELF::SHN_ABS: ret = 'a'; break;
1075 case ELF::SHN_COMMON: ret = 'c'; break;
1078 switch (symb->getBinding()) {
1079 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
1081 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1084 if (symb->getType() == ELF::STT_OBJECT)
1090 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
1092 if (error_code ec = getSymbolName(Symb, name))
1094 Result = StringSwitch<char>(name)
1095 .StartsWith(".debug", 'N')
1096 .StartsWith(".note", 'n')
1098 return object_error::success;
1102 return object_error::success;
1105 template<class ELFT>
1106 error_code ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
1107 SymbolRef::Type &Result) const {
1108 validateSymbol(Symb);
1109 const Elf_Sym *symb = getSymbol(Symb);
1111 switch (symb->getType()) {
1112 case ELF::STT_NOTYPE:
1113 Result = SymbolRef::ST_Unknown;
1115 case ELF::STT_SECTION:
1116 Result = SymbolRef::ST_Debug;
1119 Result = SymbolRef::ST_File;
1122 Result = SymbolRef::ST_Function;
1124 case ELF::STT_OBJECT:
1125 case ELF::STT_COMMON:
1127 Result = SymbolRef::ST_Data;
1130 Result = SymbolRef::ST_Other;
1133 return object_error::success;
1136 template<class ELFT>
1137 error_code ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb,
1138 uint32_t &Result) const {
1139 validateSymbol(Symb);
1140 const Elf_Sym *symb = getSymbol(Symb);
1142 Result = SymbolRef::SF_None;
1144 if (symb->getBinding() != ELF::STB_LOCAL)
1145 Result |= SymbolRef::SF_Global;
1147 if (symb->getBinding() == ELF::STB_WEAK)
1148 Result |= SymbolRef::SF_Weak;
1150 if (symb->st_shndx == ELF::SHN_ABS)
1151 Result |= SymbolRef::SF_Absolute;
1153 if (symb->getType() == ELF::STT_FILE ||
1154 symb->getType() == ELF::STT_SECTION)
1155 Result |= SymbolRef::SF_FormatSpecific;
1157 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1158 Result |= SymbolRef::SF_Undefined;
1160 if (symb->getType() == ELF::STT_COMMON ||
1161 getSymbolTableIndex(symb) == ELF::SHN_COMMON)
1162 Result |= SymbolRef::SF_Common;
1164 if (symb->getType() == ELF::STT_TLS)
1165 Result |= SymbolRef::SF_ThreadLocal;
1167 return object_error::success;
1170 template<class ELFT>
1171 error_code ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
1172 section_iterator &Res) const {
1173 validateSymbol(Symb);
1174 const Elf_Sym *symb = getSymbol(Symb);
1175 const Elf_Shdr *sec = getSection(symb);
1177 Res = end_sections();
1180 Sec.p = reinterpret_cast<intptr_t>(sec);
1181 Res = section_iterator(SectionRef(Sec, this));
1183 return object_error::success;
1186 template<class ELFT>
1187 error_code ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb,
1188 uint64_t &Val) const {
1189 validateSymbol(Symb);
1190 const Elf_Sym *symb = getSymbol(Symb);
1191 Val = symb->st_value;
1192 return object_error::success;
1195 template<class ELFT>
1196 error_code ELFObjectFile<ELFT>::getSectionNext(DataRefImpl Sec,
1197 SectionRef &Result) const {
1198 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
1199 sec += Header->e_shentsize;
1200 Sec.p = reinterpret_cast<intptr_t>(sec);
1201 Result = SectionRef(Sec, this);
1202 return object_error::success;
1205 template<class ELFT>
1206 error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
1207 StringRef &Result) const {
1208 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1209 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
1210 return object_error::success;
1213 template<class ELFT>
1214 error_code ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec,
1215 uint64_t &Result) const {
1216 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1217 Result = sec->sh_addr;
1218 return object_error::success;
1221 template<class ELFT>
1222 error_code ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec,
1223 uint64_t &Result) const {
1224 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1225 Result = sec->sh_size;
1226 return object_error::success;
1229 template<class ELFT>
1230 error_code ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
1231 StringRef &Result) const {
1232 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1233 const char *start = (const char*)base() + sec->sh_offset;
1234 Result = StringRef(start, sec->sh_size);
1235 return object_error::success;
1238 template<class ELFT>
1239 error_code ELFObjectFile<ELFT>::getSectionContents(const Elf_Shdr *Sec,
1240 StringRef &Result) const {
1241 const char *start = (const char*)base() + Sec->sh_offset;
1242 Result = StringRef(start, Sec->sh_size);
1243 return object_error::success;
1246 template<class ELFT>
1247 error_code ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec,
1248 uint64_t &Result) const {
1249 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1250 Result = sec->sh_addralign;
1251 return object_error::success;
1254 template<class ELFT>
1255 error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec,
1256 bool &Result) const {
1257 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1258 if (sec->sh_flags & ELF::SHF_EXECINSTR)
1262 return object_error::success;
1265 template<class ELFT>
1266 error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec,
1267 bool &Result) const {
1268 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1269 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1270 && sec->sh_type == ELF::SHT_PROGBITS)
1274 return object_error::success;
1277 template<class ELFT>
1278 error_code ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec,
1279 bool &Result) const {
1280 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1281 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1282 && sec->sh_type == ELF::SHT_NOBITS)
1286 return object_error::success;
1289 template<class ELFT>
1290 error_code ELFObjectFile<ELFT>::isSectionRequiredForExecution(
1291 DataRefImpl Sec, bool &Result) const {
1292 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1293 if (sec->sh_flags & ELF::SHF_ALLOC)
1297 return object_error::success;
1300 template<class ELFT>
1301 error_code ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec,
1302 bool &Result) const {
1303 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1304 if (sec->sh_type == ELF::SHT_NOBITS)
1308 return object_error::success;
1311 template<class ELFT>
1312 error_code ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec,
1313 bool &Result) const {
1314 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1315 // For ELF, all zero-init sections are virtual (that is, they occupy no space
1316 // in the object image) and vice versa.
1317 Result = sec->sh_type == ELF::SHT_NOBITS;
1318 return object_error::success;
1321 template<class ELFT>
1322 error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec,
1323 bool &Result) const {
1324 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1325 if (sec->sh_flags & ELF::SHF_WRITE || sec->sh_flags & ELF::SHF_EXECINSTR)
1329 return object_error::success;
1332 template<class ELFT>
1333 error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
1335 bool &Result) const {
1336 // FIXME: Unimplemented.
1338 return object_error::success;
1341 template<class ELFT>
1343 ELFObjectFile<ELFT>::getSectionRelBegin(DataRefImpl Sec) const {
1344 DataRefImpl RelData;
1345 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1346 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1347 if (sec != 0 && ittr != SectionRelocMap.end()) {
1348 RelData.w.a = getSection(ittr->second[0])->sh_info;
1349 RelData.w.b = ittr->second[0];
1352 return relocation_iterator(RelocationRef(RelData, this));
1355 template<class ELFT>
1357 ELFObjectFile<ELFT>::getSectionRelEnd(DataRefImpl Sec) const {
1358 DataRefImpl RelData;
1359 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1360 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1361 if (sec != 0 && ittr != SectionRelocMap.end()) {
1362 // Get the index of the last relocation section for this section.
1363 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
1364 const Elf_Shdr *relocsec = getSection(relocsecindex);
1365 RelData.w.a = relocsec->sh_info;
1366 RelData.w.b = relocsecindex;
1367 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
1369 return relocation_iterator(RelocationRef(RelData, this));
1373 template<class ELFT>
1374 error_code ELFObjectFile<ELFT>::getRelocationNext(DataRefImpl Rel,
1375 RelocationRef &Result) const {
1377 const Elf_Shdr *relocsec = getSection(Rel.w.b);
1378 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
1379 // We have reached the end of the relocations for this section. See if there
1380 // is another relocation section.
1381 typename RelocMap_t::mapped_type relocseclist =
1382 SectionRelocMap.lookup(getSection(Rel.w.a));
1384 // Do a binary search for the current reloc section index (which must be
1385 // present). Then get the next one.
1386 typename RelocMap_t::mapped_type::const_iterator loc =
1387 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
1390 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
1391 // to the end iterator.
1392 if (loc != relocseclist.end()) {
1397 Result = RelocationRef(Rel, this);
1398 return object_error::success;
1401 template<class ELFT>
1402 error_code ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel,
1403 SymbolRef &Result) const {
1405 const Elf_Shdr *sec = getSection(Rel.w.b);
1406 switch (sec->sh_type) {
1408 report_fatal_error("Invalid section type in Rel!");
1409 case ELF::SHT_REL : {
1410 symbolIdx = getRel(Rel)->getSymbol();
1413 case ELF::SHT_RELA : {
1414 symbolIdx = getRela(Rel)->getSymbol();
1418 DataRefImpl SymbolData;
1419 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
1420 if (it == SymbolTableSectionsIndexMap.end())
1421 report_fatal_error("Relocation symbol table not found!");
1422 SymbolData.d.a = symbolIdx;
1423 SymbolData.d.b = it->second;
1424 Result = SymbolRef(SymbolData, this);
1425 return object_error::success;
1428 template<class ELFT>
1429 error_code ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
1430 uint64_t &Result) const {
1432 const Elf_Shdr *sec = getSection(Rel.w.b);
1433 switch (sec->sh_type) {
1435 report_fatal_error("Invalid section type in Rel!");
1436 case ELF::SHT_REL : {
1437 offset = getRel(Rel)->r_offset;
1440 case ELF::SHT_RELA : {
1441 offset = getRela(Rel)->r_offset;
1447 return object_error::success;
1450 template<class ELFT>
1451 error_code ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
1452 uint64_t &Result) const {
1454 const Elf_Shdr *sec = getSection(Rel.w.b);
1455 switch (sec->sh_type) {
1457 report_fatal_error("Invalid section type in Rel!");
1458 case ELF::SHT_REL : {
1459 offset = getRel(Rel)->r_offset;
1462 case ELF::SHT_RELA : {
1463 offset = getRela(Rel)->r_offset;
1468 Result = offset - sec->sh_addr;
1469 return object_error::success;
1472 template<class ELFT>
1473 error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
1474 uint64_t &Result) const {
1475 const Elf_Shdr *sec = getSection(Rel.w.b);
1476 switch (sec->sh_type) {
1478 report_fatal_error("Invalid section type in Rel!");
1479 case ELF::SHT_REL : {
1480 Result = getRel(Rel)->getType();
1483 case ELF::SHT_RELA : {
1484 Result = getRela(Rel)->getType();
1488 return object_error::success;
1491 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
1492 case ELF::enum: res = #enum; break;
1494 template<class ELFT>
1495 error_code ELFObjectFile<ELFT>::getRelocationTypeName(
1496 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1497 const Elf_Shdr *sec = getSection(Rel.w.b);
1500 switch (sec->sh_type) {
1502 return object_error::parse_failed;
1503 case ELF::SHT_REL : {
1504 type = getRel(Rel)->getType();
1507 case ELF::SHT_RELA : {
1508 type = getRela(Rel)->getType();
1512 switch (Header->e_machine) {
1513 case ELF::EM_X86_64:
1515 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
1516 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
1517 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
1518 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
1519 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
1520 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
1521 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
1522 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
1523 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
1524 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
1525 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
1526 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
1527 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
1528 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
1529 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
1530 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
1531 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
1532 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
1533 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
1534 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
1535 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
1536 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
1537 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
1538 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
1539 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
1540 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
1541 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
1542 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
1543 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
1544 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
1545 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
1546 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
1553 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
1554 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
1555 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
1556 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
1557 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
1558 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
1559 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
1560 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
1561 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
1562 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
1563 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
1564 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
1565 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
1566 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
1567 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
1568 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
1569 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
1570 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
1571 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
1572 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
1573 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
1574 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
1575 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
1576 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
1577 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
1578 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
1579 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
1580 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
1581 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
1582 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
1583 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
1584 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
1585 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
1586 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
1587 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
1588 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
1589 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
1590 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
1591 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
1592 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
1597 case ELF::EM_AARCH64:
1599 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_NONE);
1600 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ABS64);
1601 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ABS32);
1602 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ABS16);
1603 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_PREL64);
1604 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_PREL32);
1605 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_PREL16);
1606 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G0);
1607 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G0_NC);
1608 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G1);
1609 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G1_NC);
1610 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G2);
1611 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G2_NC);
1612 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G3);
1613 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_SABS_G0);
1614 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_SABS_G1);
1615 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_SABS_G2);
1616 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LD_PREL_LO19);
1617 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ADR_PREL_LO21);
1618 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ADR_PREL_PG_HI21);
1619 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ADD_ABS_LO12_NC);
1620 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LDST8_ABS_LO12_NC);
1621 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TSTBR14);
1622 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_CONDBR19);
1623 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_JUMP26);
1624 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_CALL26);
1625 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LDST16_ABS_LO12_NC);
1626 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LDST32_ABS_LO12_NC);
1627 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LDST64_ABS_LO12_NC);
1628 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LDST128_ABS_LO12_NC);
1629 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ADR_GOT_PAGE);
1630 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LD64_GOT_LO12_NC);
1631 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_MOVW_DTPREL_G2);
1632 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_MOVW_DTPREL_G1);
1633 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_MOVW_DTPREL_G1_NC);
1634 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_MOVW_DTPREL_G0);
1635 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC);
1636 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_ADD_DTPREL_HI12);
1637 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_ADD_DTPREL_LO12);
1638 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC);
1639 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST8_DTPREL_LO12);
1640 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC);
1641 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST16_DTPREL_LO12);
1642 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC);
1643 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST32_DTPREL_LO12);
1644 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC);
1645 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST64_DTPREL_LO12);
1646 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC);
1647 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSIE_MOVW_GOTTPREL_G1);
1648 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC);
1649 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21);
1650 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
1651 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSIE_LD_GOTTPREL_PREL19);
1652 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_MOVW_TPREL_G2);
1653 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_MOVW_TPREL_G1);
1654 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_MOVW_TPREL_G1_NC);
1655 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_MOVW_TPREL_G0);
1656 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_MOVW_TPREL_G0_NC);
1657 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_ADD_TPREL_HI12);
1658 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_ADD_TPREL_LO12);
1659 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_ADD_TPREL_LO12_NC);
1660 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST8_TPREL_LO12);
1661 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC);
1662 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST16_TPREL_LO12);
1663 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC);
1664 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST32_TPREL_LO12);
1665 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC);
1666 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST64_TPREL_LO12);
1667 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC);
1668 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSDESC_ADR_PAGE);
1669 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSDESC_LD64_LO12_NC);
1670 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSDESC_ADD_LO12_NC);
1671 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSDESC_CALL);
1679 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_NONE);
1680 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PC24);
1681 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32);
1682 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32);
1683 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G0);
1684 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS16);
1685 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS12);
1686 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ABS5);
1687 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS8);
1688 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL32);
1689 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_CALL);
1690 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC8);
1691 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BREL_ADJ);
1692 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESC);
1693 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_SWI8);
1694 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_XPC25);
1695 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_XPC22);
1696 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPMOD32);
1697 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPOFF32);
1698 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_TPOFF32);
1699 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_COPY);
1700 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GLOB_DAT);
1701 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP_SLOT);
1702 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_RELATIVE);
1703 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF32);
1704 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_PREL);
1705 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL);
1706 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32);
1707 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_CALL);
1708 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP24);
1709 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP24);
1710 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_ABS);
1711 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_7_0);
1712 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_15_8);
1713 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_23_15);
1714 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SBREL_11_0_NC);
1715 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_19_12_NC);
1716 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_27_20_CK);
1717 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET1);
1718 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL31);
1719 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_V4BX);
1720 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET2);
1721 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PREL31);
1722 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_ABS_NC);
1723 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_ABS);
1724 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_PREL_NC);
1725 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_PREL);
1726 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_ABS_NC);
1727 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_ABS);
1728 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_PREL_NC);
1729 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_PREL);
1730 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP19);
1731 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP6);
1732 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ALU_PREL_11_0);
1733 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC12);
1734 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32_NOI);
1735 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32_NOI);
1736 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0_NC);
1737 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0);
1738 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1_NC);
1739 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1);
1740 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G2);
1741 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G1);
1742 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G2);
1743 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G0);
1744 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G1);
1745 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G2);
1746 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G0);
1747 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G1);
1748 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G2);
1749 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0_NC);
1750 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0);
1751 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1_NC);
1752 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1);
1753 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G2);
1754 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G0);
1755 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G1);
1756 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G2);
1757 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G0);
1758 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G1);
1759 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G2);
1760 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G0);
1761 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G1);
1762 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G2);
1763 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL_NC);
1764 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_BREL);
1765 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL);
1766 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL_NC);
1767 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_BREL);
1768 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL);
1769 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GOTDESC);
1770 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_CALL);
1771 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESCSEQ);
1772 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_CALL);
1773 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32_ABS);
1774 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_ABS);
1775 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_PREL);
1776 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL12);
1777 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF12);
1778 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTRELAX);
1779 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTENTRY);
1780 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTINHERIT);
1781 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP11);
1782 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP8);
1783 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GD32);
1784 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDM32);
1785 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO32);
1786 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE32);
1787 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE32);
1788 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO12);
1789 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE12);
1790 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE12GP);
1791 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_0);
1792 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_1);
1793 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_2);
1794 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_3);
1795 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_4);
1796 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_5);
1797 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_6);
1798 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_7);
1799 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_8);
1800 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_9);
1801 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_10);
1802 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_11);
1803 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_12);
1804 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_13);
1805 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_14);
1806 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_15);
1807 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ME_TOO);
1808 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ16);
1809 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ32);
1814 case ELF::EM_HEXAGON:
1816 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_NONE);
1817 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL);
1818 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL);
1819 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL);
1820 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_LO16);
1821 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HI16);
1822 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32);
1823 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16);
1824 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8);
1825 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_0);
1826 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_1);
1827 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_2);
1828 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_3);
1829 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HL16);
1830 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL);
1831 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL);
1832 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B32_PCREL_X);
1833 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_6_X);
1834 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL_X);
1835 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL_X);
1836 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL_X);
1837 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL_X);
1838 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL_X);
1839 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16_X);
1840 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_12_X);
1841 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_11_X);
1842 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_10_X);
1843 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_9_X);
1844 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8_X);
1845 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_7_X);
1846 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_X);
1847 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_PCREL);
1848 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_COPY);
1849 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GLOB_DAT);
1850 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_JMP_SLOT);
1851 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_RELATIVE);
1852 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_PLT_B22_PCREL);
1853 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_LO16);
1854 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_HI16);
1855 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32);
1856 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_LO16);
1857 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_HI16);
1858 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32);
1859 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16);
1860 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPMOD_32);
1861 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_LO16);
1862 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_HI16);
1863 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32);
1864 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16);
1865 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_PLT_B22_PCREL);
1866 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_LO16);
1867 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_HI16);
1868 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32);
1869 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16);
1870 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_LO16);
1871 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_HI16);
1872 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32);
1873 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_LO16);
1874 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_HI16);
1875 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32);
1876 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16);
1877 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_LO16);
1878 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_HI16);
1879 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32);
1880 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16);
1881 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_PCREL_X);
1882 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32_6_X);
1883 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_16_X);
1884 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_11_X);
1885 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32_6_X);
1886 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16_X);
1887 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_11_X);
1888 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32_6_X);
1889 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16_X);
1890 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_11_X);
1891 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32_6_X);
1892 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16_X);
1893 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_11_X);
1894 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32_6_X);
1895 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_16_X);
1896 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32_6_X);
1897 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16_X);
1898 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_11_X);
1899 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32_6_X);
1900 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16_X);
1901 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_11_X);
1909 Result.append(res.begin(), res.end());
1910 return object_error::success;
1913 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
1915 template<class ELFT>
1916 error_code ELFObjectFile<ELFT>::getRelocationAdditionalInfo(
1917 DataRefImpl Rel, int64_t &Result) const {
1918 const Elf_Shdr *sec = getSection(Rel.w.b);
1919 switch (sec->sh_type) {
1921 report_fatal_error("Invalid section type in Rel!");
1922 case ELF::SHT_REL : {
1924 return object_error::success;
1926 case ELF::SHT_RELA : {
1927 Result = getRela(Rel)->r_addend;
1928 return object_error::success;
1933 template<class ELFT>
1934 error_code ELFObjectFile<ELFT>::getRelocationValueString(
1935 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1936 const Elf_Shdr *sec = getSection(Rel.w.b);
1940 uint16_t symbol_index = 0;
1941 switch (sec->sh_type) {
1943 return object_error::parse_failed;
1944 case ELF::SHT_REL: {
1945 type = getRel(Rel)->getType();
1946 symbol_index = getRel(Rel)->getSymbol();
1947 // TODO: Read implicit addend from section data.
1950 case ELF::SHT_RELA: {
1951 type = getRela(Rel)->getType();
1952 symbol_index = getRela(Rel)->getSymbol();
1953 addend = getRela(Rel)->r_addend;
1957 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1959 if (error_code ec = getSymbolName(getSection(sec->sh_link), symb, symname))
1961 switch (Header->e_machine) {
1962 case ELF::EM_X86_64:
1964 case ELF::R_X86_64_PC8:
1965 case ELF::R_X86_64_PC16:
1966 case ELF::R_X86_64_PC32: {
1968 raw_string_ostream fmt(fmtbuf);
1969 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1971 Result.append(fmtbuf.begin(), fmtbuf.end());
1974 case ELF::R_X86_64_8:
1975 case ELF::R_X86_64_16:
1976 case ELF::R_X86_64_32:
1977 case ELF::R_X86_64_32S:
1978 case ELF::R_X86_64_64: {
1980 raw_string_ostream fmt(fmtbuf);
1981 fmt << symname << (addend < 0 ? "" : "+") << addend;
1983 Result.append(fmtbuf.begin(), fmtbuf.end());
1990 case ELF::EM_AARCH64:
1992 case ELF::EM_HEXAGON:
1999 Result.append(res.begin(), res.end());
2000 return object_error::success;
2003 // Verify that the last byte in the string table in a null.
2004 template<class ELFT>
2005 void ELFObjectFile<ELFT>::VerifyStrTab(const Elf_Shdr *sh) const {
2006 const char *strtab = (const char*)base() + sh->sh_offset;
2007 if (strtab[sh->sh_size - 1] != 0)
2008 // FIXME: Proper error handling.
2009 report_fatal_error("String table must end with a null terminator!");
2012 template<class ELFT>
2013 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBuffer *Object, error_code &ec)
2014 : ObjectFile(getELFType(
2015 static_cast<endianness>(ELFT::TargetEndianness) == support::little,
2019 , isDyldELFObject(false)
2020 , SectionHeaderTable(0)
2021 , dot_shstrtab_sec(0)
2024 , dot_dynamic_sec(0)
2025 , dot_gnu_version_sec(0)
2026 , dot_gnu_version_r_sec(0)
2027 , dot_gnu_version_d_sec(0)
2031 const uint64_t FileSize = Data->getBufferSize();
2033 if (sizeof(Elf_Ehdr) > FileSize)
2034 // FIXME: Proper error handling.
2035 report_fatal_error("File too short!");
2037 Header = reinterpret_cast<const Elf_Ehdr *>(base());
2039 if (Header->e_shoff == 0)
2042 const uint64_t SectionTableOffset = Header->e_shoff;
2044 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize)
2045 // FIXME: Proper error handling.
2046 report_fatal_error("Section header table goes past end of file!");
2048 // The getNumSections() call below depends on SectionHeaderTable being set.
2049 SectionHeaderTable =
2050 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
2051 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
2053 if (SectionTableOffset + SectionTableSize > FileSize)
2054 // FIXME: Proper error handling.
2055 report_fatal_error("Section table goes past end of file!");
2057 // To find the symbol tables we walk the section table to find SHT_SYMTAB.
2058 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
2059 const Elf_Shdr* sh = SectionHeaderTable;
2061 // Reserve SymbolTableSections[0] for .dynsym
2062 SymbolTableSections.push_back(NULL);
2064 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
2065 switch (sh->sh_type) {
2066 case ELF::SHT_SYMTAB_SHNDX: {
2067 if (SymbolTableSectionHeaderIndex)
2068 // FIXME: Proper error handling.
2069 report_fatal_error("More than one .symtab_shndx!");
2070 SymbolTableSectionHeaderIndex = sh;
2073 case ELF::SHT_SYMTAB: {
2074 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
2075 SymbolTableSections.push_back(sh);
2078 case ELF::SHT_DYNSYM: {
2079 if (SymbolTableSections[0] != NULL)
2080 // FIXME: Proper error handling.
2081 report_fatal_error("More than one .dynsym!");
2082 SymbolTableSectionsIndexMap[i] = 0;
2083 SymbolTableSections[0] = sh;
2087 case ELF::SHT_RELA: {
2088 SectionRelocMap[getSection(sh->sh_info)].push_back(i);
2091 case ELF::SHT_DYNAMIC: {
2092 if (dot_dynamic_sec != NULL)
2093 // FIXME: Proper error handling.
2094 report_fatal_error("More than one .dynamic!");
2095 dot_dynamic_sec = sh;
2098 case ELF::SHT_GNU_versym: {
2099 if (dot_gnu_version_sec != NULL)
2100 // FIXME: Proper error handling.
2101 report_fatal_error("More than one .gnu.version section!");
2102 dot_gnu_version_sec = sh;
2105 case ELF::SHT_GNU_verdef: {
2106 if (dot_gnu_version_d_sec != NULL)
2107 // FIXME: Proper error handling.
2108 report_fatal_error("More than one .gnu.version_d section!");
2109 dot_gnu_version_d_sec = sh;
2112 case ELF::SHT_GNU_verneed: {
2113 if (dot_gnu_version_r_sec != NULL)
2114 // FIXME: Proper error handling.
2115 report_fatal_error("More than one .gnu.version_r section!");
2116 dot_gnu_version_r_sec = sh;
2123 // Sort section relocation lists by index.
2124 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
2125 e = SectionRelocMap.end(); i != e; ++i) {
2126 std::sort(i->second.begin(), i->second.end());
2129 // Get string table sections.
2130 dot_shstrtab_sec = getSection(getStringTableIndex());
2131 if (dot_shstrtab_sec) {
2132 // Verify that the last byte in the string table in a null.
2133 VerifyStrTab(dot_shstrtab_sec);
2136 // Merge this into the above loop.
2137 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
2138 *e = i + getNumSections() * Header->e_shentsize;
2139 i != e; i += Header->e_shentsize) {
2140 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
2141 if (sh->sh_type == ELF::SHT_STRTAB) {
2142 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
2143 if (SectionName == ".strtab") {
2144 if (dot_strtab_sec != 0)
2145 // FIXME: Proper error handling.
2146 report_fatal_error("Already found section named .strtab!");
2147 dot_strtab_sec = sh;
2148 VerifyStrTab(dot_strtab_sec);
2149 } else if (SectionName == ".dynstr") {
2150 if (dot_dynstr_sec != 0)
2151 // FIXME: Proper error handling.
2152 report_fatal_error("Already found section named .dynstr!");
2153 dot_dynstr_sec = sh;
2154 VerifyStrTab(dot_dynstr_sec);
2159 // Build symbol name side-mapping if there is one.
2160 if (SymbolTableSectionHeaderIndex) {
2161 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
2162 SymbolTableSectionHeaderIndex->sh_offset);
2164 for (symbol_iterator si = begin_symbols(),
2165 se = end_symbols(); si != se; si.increment(ec)) {
2167 report_fatal_error("Fewer extended symbol table entries than symbols!");
2168 if (*ShndxTable != ELF::SHN_UNDEF)
2169 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
2175 // Get the symbol table index in the symtab section given a symbol
2176 template<class ELFT>
2177 uint64_t ELFObjectFile<ELFT>::getSymbolIndex(const Elf_Sym *Sym) const {
2178 assert(SymbolTableSections.size() == 1 && "Only one symbol table supported!");
2179 const Elf_Shdr *SymTab = *SymbolTableSections.begin();
2180 uintptr_t SymLoc = uintptr_t(Sym);
2181 uintptr_t SymTabLoc = uintptr_t(base() + SymTab->sh_offset);
2182 assert(SymLoc > SymTabLoc && "Symbol not in symbol table!");
2183 uint64_t SymOffset = SymLoc - SymTabLoc;
2184 assert(SymOffset % SymTab->sh_entsize == 0 &&
2185 "Symbol not multiple of symbol size!");
2186 return SymOffset / SymTab->sh_entsize;
2189 template<class ELFT>
2190 symbol_iterator ELFObjectFile<ELFT>::begin_symbols() const {
2191 DataRefImpl SymbolData;
2192 if (SymbolTableSections.size() <= 1) {
2193 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2194 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2196 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2197 SymbolData.d.b = 1; // The 0th table is .dynsym
2199 return symbol_iterator(SymbolRef(SymbolData, this));
2202 template<class ELFT>
2203 symbol_iterator ELFObjectFile<ELFT>::end_symbols() const {
2204 DataRefImpl SymbolData;
2205 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2206 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2207 return symbol_iterator(SymbolRef(SymbolData, this));
2210 template<class ELFT>
2211 symbol_iterator ELFObjectFile<ELFT>::begin_dynamic_symbols() const {
2212 DataRefImpl SymbolData;
2213 if (SymbolTableSections[0] == NULL) {
2214 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2215 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2217 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2218 SymbolData.d.b = 0; // The 0th table is .dynsym
2220 return symbol_iterator(SymbolRef(SymbolData, this));
2223 template<class ELFT>
2224 symbol_iterator ELFObjectFile<ELFT>::end_dynamic_symbols() const {
2225 DataRefImpl SymbolData;
2226 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2227 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2228 return symbol_iterator(SymbolRef(SymbolData, this));
2231 template<class ELFT>
2232 section_iterator ELFObjectFile<ELFT>::begin_sections() const {
2234 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
2235 return section_iterator(SectionRef(ret, this));
2238 template<class ELFT>
2239 section_iterator ELFObjectFile<ELFT>::end_sections() const {
2241 ret.p = reinterpret_cast<intptr_t>(base()
2243 + (Header->e_shentsize*getNumSections()));
2244 return section_iterator(SectionRef(ret, this));
2247 template<class ELFT>
2248 typename ELFObjectFile<ELFT>::dyn_iterator
2249 ELFObjectFile<ELFT>::begin_dynamic_table() const {
2250 if (dot_dynamic_sec)
2251 return dyn_iterator(dot_dynamic_sec->sh_entsize,
2252 (const char *)base() + dot_dynamic_sec->sh_offset);
2253 return dyn_iterator(0, 0);
2256 template<class ELFT>
2257 typename ELFObjectFile<ELFT>::dyn_iterator
2258 ELFObjectFile<ELFT>::end_dynamic_table() const {
2259 if (dot_dynamic_sec)
2260 return dyn_iterator(dot_dynamic_sec->sh_entsize, (const char *)base() +
2261 dot_dynamic_sec->sh_offset + dot_dynamic_sec->sh_size);
2262 return dyn_iterator(0, 0);
2265 template<class ELFT>
2266 StringRef ELFObjectFile<ELFT>::getLoadName() const {
2268 // Find the DT_SONAME entry
2269 dyn_iterator it = begin_dynamic_table();
2270 dyn_iterator ie = end_dynamic_table();
2271 for (; it != ie; ++it) {
2272 if (it->getTag() == ELF::DT_SONAME)
2276 if (dot_dynstr_sec == NULL)
2277 report_fatal_error("Dynamic string table is missing");
2278 dt_soname = getString(dot_dynstr_sec, it->getVal());
2286 template<class ELFT>
2287 library_iterator ELFObjectFile<ELFT>::begin_libraries_needed() const {
2288 // Find the first DT_NEEDED entry
2289 dyn_iterator i = begin_dynamic_table();
2290 dyn_iterator e = end_dynamic_table();
2292 if (i->getTag() == ELF::DT_NEEDED)
2297 DRI.p = reinterpret_cast<uintptr_t>(i.get());
2298 return library_iterator(LibraryRef(DRI, this));
2301 template<class ELFT>
2302 error_code ELFObjectFile<ELFT>::getLibraryNext(DataRefImpl Data,
2303 LibraryRef &Result) const {
2304 // Use the same DataRefImpl format as DynRef.
2305 dyn_iterator i = dyn_iterator(dot_dynamic_sec->sh_entsize,
2306 reinterpret_cast<const char *>(Data.p));
2307 dyn_iterator e = end_dynamic_table();
2309 // Skip the current dynamic table entry.
2312 // Find the next DT_NEEDED entry.
2313 for (; i != e && i->getTag() != ELF::DT_NEEDED; ++i)
2317 DRI.p = reinterpret_cast<uintptr_t>(i.get());
2318 Result = LibraryRef(DRI, this);
2319 return object_error::success;
2322 template<class ELFT>
2323 error_code ELFObjectFile<ELFT>::getLibraryPath(DataRefImpl Data,
2324 StringRef &Res) const {
2325 dyn_iterator i = dyn_iterator(dot_dynamic_sec->sh_entsize,
2326 reinterpret_cast<const char *>(Data.p));
2327 if (i == end_dynamic_table())
2328 report_fatal_error("getLibraryPath() called on iterator end");
2330 if (i->getTag() != ELF::DT_NEEDED)
2331 report_fatal_error("Invalid library_iterator");
2333 // This uses .dynstr to lookup the name of the DT_NEEDED entry.
2334 // THis works as long as DT_STRTAB == .dynstr. This is true most of
2335 // the time, but the specification allows exceptions.
2336 // TODO: This should really use DT_STRTAB instead. Doing this requires
2337 // reading the program headers.
2338 if (dot_dynstr_sec == NULL)
2339 report_fatal_error("Dynamic string table is missing");
2340 Res = getString(dot_dynstr_sec, i->getVal());
2341 return object_error::success;
2344 template<class ELFT>
2345 library_iterator ELFObjectFile<ELFT>::end_libraries_needed() const {
2346 dyn_iterator e = end_dynamic_table();
2348 DRI.p = reinterpret_cast<uintptr_t>(e.get());
2349 return library_iterator(LibraryRef(DRI, this));
2352 template<class ELFT>
2353 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
2354 return ELFT::Is64Bits ? 8 : 4;
2357 template<class ELFT>
2358 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
2359 switch(Header->e_ident[ELF::EI_CLASS]) {
2360 case ELF::ELFCLASS32:
2361 switch(Header->e_machine) {
2363 return "ELF32-i386";
2364 case ELF::EM_X86_64:
2365 return "ELF32-x86-64";
2368 case ELF::EM_HEXAGON:
2369 return "ELF32-hexagon";
2371 return "ELF32-mips";
2373 return "ELF32-unknown";
2375 case ELF::ELFCLASS64:
2376 switch(Header->e_machine) {
2378 return "ELF64-i386";
2379 case ELF::EM_X86_64:
2380 return "ELF64-x86-64";
2381 case ELF::EM_AARCH64:
2382 return "ELF64-aarch64";
2384 return "ELF64-ppc64";
2386 return "ELF64-unknown";
2389 // FIXME: Proper error handling.
2390 report_fatal_error("Invalid ELFCLASS!");
2394 template<class ELFT>
2395 unsigned ELFObjectFile<ELFT>::getArch() const {
2396 switch(Header->e_machine) {
2399 case ELF::EM_X86_64:
2400 return Triple::x86_64;
2401 case ELF::EM_AARCH64:
2402 return Triple::aarch64;
2405 case ELF::EM_HEXAGON:
2406 return Triple::hexagon;
2408 return (ELFT::TargetEndianness == support::little) ?
2409 Triple::mipsel : Triple::mips;
2411 return Triple::ppc64;
2413 return Triple::UnknownArch;
2417 template<class ELFT>
2418 uint64_t ELFObjectFile<ELFT>::getNumSections() const {
2419 assert(Header && "Header not initialized!");
2420 if (Header->e_shnum == ELF::SHN_UNDEF) {
2421 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
2422 return SectionHeaderTable->sh_size;
2424 return Header->e_shnum;
2427 template<class ELFT>
2429 ELFObjectFile<ELFT>::getStringTableIndex() const {
2430 if (Header->e_shnum == ELF::SHN_UNDEF) {
2431 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
2432 return SectionHeaderTable->sh_link;
2433 if (Header->e_shstrndx >= getNumSections())
2436 return Header->e_shstrndx;
2439 template<class ELFT>
2440 template<typename T>
2442 ELFObjectFile<ELFT>::getEntry(uint16_t Section, uint32_t Entry) const {
2443 return getEntry<T>(getSection(Section), Entry);
2446 template<class ELFT>
2447 template<typename T>
2449 ELFObjectFile<ELFT>::getEntry(const Elf_Shdr * Section, uint32_t Entry) const {
2450 return reinterpret_cast<const T *>(
2452 + Section->sh_offset
2453 + (Entry * Section->sh_entsize));
2456 template<class ELFT>
2457 const typename ELFObjectFile<ELFT>::Elf_Sym *
2458 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
2459 return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
2462 template<class ELFT>
2463 const typename ELFObjectFile<ELFT>::Elf_Rel *
2464 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
2465 return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
2468 template<class ELFT>
2469 const typename ELFObjectFile<ELFT>::Elf_Rela *
2470 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
2471 return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
2474 template<class ELFT>
2475 const typename ELFObjectFile<ELFT>::Elf_Shdr *
2476 ELFObjectFile<ELFT>::getSection(DataRefImpl Symb) const {
2477 const Elf_Shdr *sec = getSection(Symb.d.b);
2478 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
2479 // FIXME: Proper error handling.
2480 report_fatal_error("Invalid symbol table section!");
2484 template<class ELFT>
2485 const typename ELFObjectFile<ELFT>::Elf_Shdr *
2486 ELFObjectFile<ELFT>::getSection(uint32_t index) const {
2489 if (!SectionHeaderTable || index >= getNumSections())
2490 // FIXME: Proper error handling.
2491 report_fatal_error("Invalid section index!");
2493 return reinterpret_cast<const Elf_Shdr *>(
2494 reinterpret_cast<const char *>(SectionHeaderTable)
2495 + (index * Header->e_shentsize));
2498 template<class ELFT>
2499 const char *ELFObjectFile<ELFT>::getString(uint32_t section,
2500 ELF::Elf32_Word offset) const {
2501 return getString(getSection(section), offset);
2504 template<class ELFT>
2505 const char *ELFObjectFile<ELFT>::getString(const Elf_Shdr *section,
2506 ELF::Elf32_Word offset) const {
2507 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
2508 if (offset >= section->sh_size)
2509 // FIXME: Proper error handling.
2510 report_fatal_error("Symbol name offset outside of string table!");
2511 return (const char *)base() + section->sh_offset + offset;
2514 template<class ELFT>
2515 error_code ELFObjectFile<ELFT>::getSymbolName(const Elf_Shdr *section,
2516 const Elf_Sym *symb,
2517 StringRef &Result) const {
2518 if (symb->st_name == 0) {
2519 const Elf_Shdr *section = getSection(symb);
2523 Result = getString(dot_shstrtab_sec, section->sh_name);
2524 return object_error::success;
2527 if (section == SymbolTableSections[0]) {
2528 // Symbol is in .dynsym, use .dynstr string table
2529 Result = getString(dot_dynstr_sec, symb->st_name);
2531 // Use the default symbol table name section.
2532 Result = getString(dot_strtab_sec, symb->st_name);
2534 return object_error::success;
2537 template<class ELFT>
2538 error_code ELFObjectFile<ELFT>::getSectionName(const Elf_Shdr *section,
2539 StringRef &Result) const {
2540 Result = StringRef(getString(dot_shstrtab_sec, section->sh_name));
2541 return object_error::success;
2544 template<class ELFT>
2545 error_code ELFObjectFile<ELFT>::getSymbolVersion(const Elf_Shdr *section,
2546 const Elf_Sym *symb,
2548 bool &IsDefault) const {
2549 // Handle non-dynamic symbols.
2550 if (section != SymbolTableSections[0]) {
2551 // Non-dynamic symbols can have versions in their names
2552 // A name of the form 'foo@V1' indicates version 'V1', non-default.
2553 // A name of the form 'foo@@V2' indicates version 'V2', default version.
2555 error_code ec = getSymbolName(section, symb, Name);
2556 if (ec != object_error::success)
2558 size_t atpos = Name.find('@');
2559 if (atpos == StringRef::npos) {
2562 return object_error::success;
2565 if (atpos < Name.size() && Name[atpos] == '@') {
2571 Version = Name.substr(atpos);
2572 return object_error::success;
2575 // This is a dynamic symbol. Look in the GNU symbol version table.
2576 if (dot_gnu_version_sec == NULL) {
2577 // No version table.
2580 return object_error::success;
2583 // Determine the position in the symbol table of this entry.
2584 const char *sec_start = (const char*)base() + section->sh_offset;
2585 size_t entry_index = ((const char*)symb - sec_start)/section->sh_entsize;
2587 // Get the corresponding version index entry
2588 const Elf_Versym *vs = getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index);
2589 size_t version_index = vs->vs_index & ELF::VERSYM_VERSION;
2591 // Special markers for unversioned symbols.
2592 if (version_index == ELF::VER_NDX_LOCAL ||
2593 version_index == ELF::VER_NDX_GLOBAL) {
2596 return object_error::success;
2599 // Lookup this symbol in the version table
2601 if (version_index >= VersionMap.size() || VersionMap[version_index].isNull())
2602 report_fatal_error("Symbol has version index without corresponding "
2603 "define or reference entry");
2604 const VersionMapEntry &entry = VersionMap[version_index];
2606 // Get the version name string
2608 if (entry.isVerdef()) {
2609 // The first Verdaux entry holds the name.
2610 name_offset = entry.getVerdef()->getAux()->vda_name;
2612 name_offset = entry.getVernaux()->vna_name;
2614 Version = getString(dot_dynstr_sec, name_offset);
2617 if (entry.isVerdef()) {
2618 IsDefault = !(vs->vs_index & ELF::VERSYM_HIDDEN);
2623 return object_error::success;
2626 /// This is a generic interface for retrieving GNU symbol version
2627 /// information from an ELFObjectFile.
2628 static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
2629 const SymbolRef &Sym,
2632 // Little-endian 32-bit
2633 if (const ELFObjectFile<ELFType<support::little, 4, false> > *ELFObj =
2634 dyn_cast<ELFObjectFile<ELFType<support::little, 4, false> > >(Obj))
2635 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2637 // Big-endian 32-bit
2638 if (const ELFObjectFile<ELFType<support::big, 4, false> > *ELFObj =
2639 dyn_cast<ELFObjectFile<ELFType<support::big, 4, false> > >(Obj))
2640 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2642 // Little-endian 64-bit
2643 if (const ELFObjectFile<ELFType<support::little, 8, true> > *ELFObj =
2644 dyn_cast<ELFObjectFile<ELFType<support::little, 8, true> > >(Obj))
2645 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2647 // Big-endian 64-bit
2648 if (const ELFObjectFile<ELFType<support::big, 8, true> > *ELFObj =
2649 dyn_cast<ELFObjectFile<ELFType<support::big, 8, true> > >(Obj))
2650 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2652 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");