ELF.h revision fe23da794930e01701ee1ee4fdb2b91db59c2be5
1//===- ELF.h - ELF object file implementation -------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the ELFObjectFile template class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_OBJECT_ELF_H
15#define LLVM_OBJECT_ELF_H
16
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"
29#include <algorithm>
30#include <limits>
31#include <utility>
32
33namespace llvm {
34namespace object {
35
36using support::endianness;
37
38template<typename T, int max_align>
39struct MaximumAlignment {
40  enum {value = AlignOf<T>::Alignment > max_align ? max_align
41                                                  : AlignOf<T>::Alignment};
42};
43
44// Subclasses of ELFObjectFile may need this for template instantiation
45inline std::pair<unsigned char, unsigned char>
46getElfArchType(MemoryBuffer *Object) {
47  if (Object->getBufferSize() < ELF::EI_NIDENT)
48    return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE);
49  return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS]
50                       , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]);
51}
52
53// Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
54template<endianness target_endianness, std::size_t max_alignment>
55struct ELFDataTypeTypedefHelperCommon {
56  typedef support::detail::packed_endian_specific_integral
57    <uint16_t, target_endianness,
58     MaximumAlignment<uint16_t, max_alignment>::value> Elf_Half;
59  typedef support::detail::packed_endian_specific_integral
60    <uint32_t, target_endianness,
61     MaximumAlignment<uint32_t, max_alignment>::value> Elf_Word;
62  typedef support::detail::packed_endian_specific_integral
63    <int32_t, target_endianness,
64     MaximumAlignment<int32_t, max_alignment>::value> Elf_Sword;
65  typedef support::detail::packed_endian_specific_integral
66    <uint64_t, target_endianness,
67     MaximumAlignment<uint64_t, max_alignment>::value> Elf_Xword;
68  typedef support::detail::packed_endian_specific_integral
69    <int64_t, target_endianness,
70     MaximumAlignment<int64_t, max_alignment>::value> Elf_Sxword;
71};
72
73template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
74struct ELFDataTypeTypedefHelper;
75
76/// ELF 32bit types.
77template<endianness target_endianness, std::size_t max_alignment>
78struct ELFDataTypeTypedefHelper<target_endianness, max_alignment, false>
79  : ELFDataTypeTypedefHelperCommon<target_endianness, max_alignment> {
80  typedef uint32_t value_type;
81  typedef support::detail::packed_endian_specific_integral
82    <value_type, target_endianness,
83     MaximumAlignment<value_type, max_alignment>::value> Elf_Addr;
84  typedef support::detail::packed_endian_specific_integral
85    <value_type, target_endianness,
86     MaximumAlignment<value_type, max_alignment>::value> Elf_Off;
87};
88
89/// ELF 64bit types.
90template<endianness target_endianness, std::size_t max_alignment>
91struct ELFDataTypeTypedefHelper<target_endianness, max_alignment, true>
92  : ELFDataTypeTypedefHelperCommon<target_endianness, max_alignment>{
93  typedef uint64_t value_type;
94  typedef support::detail::packed_endian_specific_integral
95    <value_type, target_endianness,
96     MaximumAlignment<value_type, max_alignment>::value> Elf_Addr;
97  typedef support::detail::packed_endian_specific_integral
98    <value_type, target_endianness,
99     MaximumAlignment<value_type, max_alignment>::value> Elf_Off;
100};
101
102// I really don't like doing this, but the alternative is copypasta.
103#define LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits) \
104typedef typename ELFDataTypeTypedefHelper \
105  <target_endianness, max_alignment, is64Bits>::Elf_Addr Elf_Addr; \
106typedef typename ELFDataTypeTypedefHelper \
107  <target_endianness, max_alignment, is64Bits>::Elf_Off Elf_Off; \
108typedef typename ELFDataTypeTypedefHelper \
109  <target_endianness, max_alignment, is64Bits>::Elf_Half Elf_Half; \
110typedef typename ELFDataTypeTypedefHelper \
111  <target_endianness, max_alignment, is64Bits>::Elf_Word Elf_Word; \
112typedef typename ELFDataTypeTypedefHelper \
113  <target_endianness, max_alignment, is64Bits>::Elf_Sword Elf_Sword; \
114typedef typename ELFDataTypeTypedefHelper \
115  <target_endianness, max_alignment, is64Bits>::Elf_Xword Elf_Xword; \
116typedef typename ELFDataTypeTypedefHelper \
117  <target_endianness, max_alignment, is64Bits>::Elf_Sxword Elf_Sxword;
118
119  // Section header.
120template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
121struct Elf_Shdr_Base;
122
123template<endianness target_endianness, std::size_t max_alignment>
124struct Elf_Shdr_Base<target_endianness, max_alignment, false> {
125  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
126  Elf_Word sh_name;     // Section name (index into string table)
127  Elf_Word sh_type;     // Section type (SHT_*)
128  Elf_Word sh_flags;    // Section flags (SHF_*)
129  Elf_Addr sh_addr;     // Address where section is to be loaded
130  Elf_Off  sh_offset;   // File offset of section data, in bytes
131  Elf_Word sh_size;     // Size of section, in bytes
132  Elf_Word sh_link;     // Section type-specific header table index link
133  Elf_Word sh_info;     // Section type-specific extra information
134  Elf_Word sh_addralign;// Section address alignment
135  Elf_Word sh_entsize;  // Size of records contained within the section
136};
137
138template<endianness target_endianness, std::size_t max_alignment>
139struct Elf_Shdr_Base<target_endianness, max_alignment, true> {
140  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
141  Elf_Word  sh_name;     // Section name (index into string table)
142  Elf_Word  sh_type;     // Section type (SHT_*)
143  Elf_Xword sh_flags;    // Section flags (SHF_*)
144  Elf_Addr  sh_addr;     // Address where section is to be loaded
145  Elf_Off   sh_offset;   // File offset of section data, in bytes
146  Elf_Xword sh_size;     // Size of section, in bytes
147  Elf_Word  sh_link;     // Section type-specific header table index link
148  Elf_Word  sh_info;     // Section type-specific extra information
149  Elf_Xword sh_addralign;// Section address alignment
150  Elf_Xword sh_entsize;  // Size of records contained within the section
151};
152
153template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
154struct Elf_Shdr_Impl
155  : Elf_Shdr_Base<target_endianness, max_alignment, is64Bits> {
156  using Elf_Shdr_Base<target_endianness, max_alignment, is64Bits>::sh_entsize;
157  using Elf_Shdr_Base<target_endianness, max_alignment, is64Bits>::sh_size;
158
159  /// @brief Get the number of entities this section contains if it has any.
160  unsigned getEntityCount() const {
161    if (sh_entsize == 0)
162      return 0;
163    return sh_size / sh_entsize;
164  }
165};
166
167template< endianness target_endianness
168        , std::size_t max_alignment
169        , bool is64Bits>
170struct Elf_Sym_Base;
171
172template<endianness target_endianness, std::size_t max_alignment>
173struct Elf_Sym_Base<target_endianness, max_alignment, false> {
174  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
175  Elf_Word      st_name;  // Symbol name (index into string table)
176  Elf_Addr      st_value; // Value or address associated with the symbol
177  Elf_Word      st_size;  // Size of the symbol
178  unsigned char st_info;  // Symbol's type and binding attributes
179  unsigned char st_other; // Must be zero; reserved
180  Elf_Half      st_shndx; // Which section (header table index) it's defined in
181};
182
183template<endianness target_endianness, std::size_t max_alignment>
184struct Elf_Sym_Base<target_endianness, max_alignment, true> {
185  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
186  Elf_Word      st_name;  // Symbol name (index into string table)
187  unsigned char st_info;  // Symbol's type and binding attributes
188  unsigned char st_other; // Must be zero; reserved
189  Elf_Half      st_shndx; // Which section (header table index) it's defined in
190  Elf_Addr      st_value; // Value or address associated with the symbol
191  Elf_Xword     st_size;  // Size of the symbol
192};
193
194template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
195struct Elf_Sym_Impl
196  : Elf_Sym_Base<target_endianness, max_alignment, is64Bits> {
197  using Elf_Sym_Base<target_endianness, max_alignment, is64Bits>::st_info;
198
199  // These accessors and mutators correspond to the ELF32_ST_BIND,
200  // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
201  unsigned char getBinding() const { return st_info >> 4; }
202  unsigned char getType() const { return st_info & 0x0f; }
203  void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
204  void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
205  void setBindingAndType(unsigned char b, unsigned char t) {
206    st_info = (b << 4) + (t & 0x0f);
207  }
208};
209
210/// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section
211/// (.gnu.version). This structure is identical for ELF32 and ELF64.
212template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
213struct Elf_Versym_Impl {
214  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
215  Elf_Half vs_index;   // Version index with flags (e.g. VERSYM_HIDDEN)
216};
217
218template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
219struct Elf_Verdaux_Impl;
220
221/// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section
222/// (.gnu.version_d). This structure is identical for ELF32 and ELF64.
223template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
224struct Elf_Verdef_Impl {
225  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
226  typedef
227    Elf_Verdaux_Impl<target_endianness, max_alignment, is64Bits> Elf_Verdaux;
228  Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT)
229  Elf_Half vd_flags;   // Bitwise flags (VER_DEF_*)
230  Elf_Half vd_ndx;     // Version index, used in .gnu.version entries
231  Elf_Half vd_cnt;     // Number of Verdaux entries
232  Elf_Word vd_hash;    // Hash of name
233  Elf_Word vd_aux;     // Offset to the first Verdaux entry (in bytes)
234  Elf_Word vd_next;    // Offset to the next Verdef entry (in bytes)
235
236  /// Get the first Verdaux entry for this Verdef.
237  const Elf_Verdaux *getAux() const {
238    return reinterpret_cast<const Elf_Verdaux*>((const char*)this + vd_aux);
239  }
240};
241
242/// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef
243/// section (.gnu.version_d). This structure is identical for ELF32 and ELF64.
244template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
245struct Elf_Verdaux_Impl {
246  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
247  Elf_Word vda_name; // Version name (offset in string table)
248  Elf_Word vda_next; // Offset to next Verdaux entry (in bytes)
249};
250
251/// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed
252/// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
253template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
254struct Elf_Verneed_Impl {
255  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
256  Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT)
257  Elf_Half vn_cnt;     // Number of associated Vernaux entries
258  Elf_Word vn_file;    // Library name (string table offset)
259  Elf_Word vn_aux;     // Offset to first Vernaux entry (in bytes)
260  Elf_Word vn_next;    // Offset to next Verneed entry (in bytes)
261};
262
263/// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed
264/// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
265template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
266struct Elf_Vernaux_Impl {
267  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
268  Elf_Word vna_hash;  // Hash of dependency name
269  Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*)
270  Elf_Half vna_other; // Version index, used in .gnu.version entries
271  Elf_Word vna_name;  // Dependency name
272  Elf_Word vna_next;  // Offset to next Vernaux entry (in bytes)
273};
274
275/// Elf_Dyn_Base: This structure matches the form of entries in the dynamic
276///               table section (.dynamic) look like.
277template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
278struct Elf_Dyn_Base;
279
280template<endianness target_endianness, std::size_t max_alignment>
281struct Elf_Dyn_Base<target_endianness, max_alignment, false> {
282  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
283  Elf_Sword d_tag;
284  union {
285    Elf_Word d_val;
286    Elf_Addr d_ptr;
287  } d_un;
288};
289
290template<endianness target_endianness, std::size_t max_alignment>
291struct Elf_Dyn_Base<target_endianness, max_alignment, true> {
292  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
293  Elf_Sxword d_tag;
294  union {
295    Elf_Xword d_val;
296    Elf_Addr d_ptr;
297  } d_un;
298};
299
300/// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters.
301template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
302struct Elf_Dyn_Impl : Elf_Dyn_Base<target_endianness, max_alignment, is64Bits> {
303  using Elf_Dyn_Base<target_endianness, max_alignment, is64Bits>::d_tag;
304  using Elf_Dyn_Base<target_endianness, max_alignment, is64Bits>::d_un;
305  int64_t getTag() const { return d_tag; }
306  uint64_t getVal() const { return d_un.d_val; }
307  uint64_t getPtr() const { return d_un.ptr; }
308};
309
310template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
311class ELFObjectFile;
312
313// DynRefImpl: Reference to an entry in the dynamic table
314// This is an ELF-specific interface.
315template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
316class DynRefImpl {
317  typedef Elf_Dyn_Impl<target_endianness, max_alignment, is64Bits> Elf_Dyn;
318  typedef ELFObjectFile<target_endianness, max_alignment, is64Bits> OwningType;
319
320  DataRefImpl DynPimpl;
321  const OwningType *OwningObject;
322
323public:
324  DynRefImpl() : OwningObject(NULL) { }
325
326  DynRefImpl(DataRefImpl DynP, const OwningType *Owner);
327
328  bool operator==(const DynRefImpl &Other) const;
329  bool operator <(const DynRefImpl &Other) const;
330
331  error_code getNext(DynRefImpl &Result) const;
332  int64_t getTag() const;
333  uint64_t getVal() const;
334  uint64_t getPtr() const;
335
336  DataRefImpl getRawDataRefImpl() const;
337};
338
339// Elf_Rel: Elf Relocation
340template< endianness target_endianness
341        , std::size_t max_alignment
342        , bool is64Bits
343        , bool isRela>
344struct Elf_Rel_Base;
345
346template<endianness target_endianness, std::size_t max_alignment>
347struct Elf_Rel_Base<target_endianness, max_alignment, false, false> {
348  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, 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};
352
353template<endianness target_endianness, std::size_t max_alignment>
354struct Elf_Rel_Base<target_endianness, max_alignment, true, false> {
355  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
356  Elf_Addr      r_offset; // Location (file byte offset, or program virtual addr)
357  Elf_Xword     r_info;   // Symbol table index and type of relocation to apply
358};
359
360template<endianness target_endianness, std::size_t max_alignment>
361struct Elf_Rel_Base<target_endianness, max_alignment, false, true> {
362  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
363  Elf_Addr      r_offset; // Location (file byte offset, or program virtual addr)
364  Elf_Word      r_info;   // Symbol table index and type of relocation to apply
365  Elf_Sword     r_addend; // Compute value for relocatable field by adding this
366};
367
368template<endianness target_endianness, std::size_t max_alignment>
369struct Elf_Rel_Base<target_endianness, max_alignment, true, true> {
370  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
371  Elf_Addr      r_offset; // Location (file byte offset, or program virtual addr)
372  Elf_Xword     r_info;   // Symbol table index and type of relocation to apply
373  Elf_Sxword    r_addend; // Compute value for relocatable field by adding this.
374};
375
376template< endianness target_endianness
377        , std::size_t max_alignment
378        , bool is64Bits
379        , bool isRela>
380struct Elf_Rel_Impl;
381
382template<endianness target_endianness, std::size_t max_alignment, bool isRela>
383struct Elf_Rel_Impl<target_endianness, max_alignment, true, isRela>
384       : Elf_Rel_Base<target_endianness, max_alignment, true, isRela> {
385  using Elf_Rel_Base<target_endianness, max_alignment, true, isRela>::r_info;
386  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
387
388  // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
389  // and ELF64_R_INFO macros defined in the ELF specification:
390  uint64_t getSymbol() const { return (r_info >> 32); }
391  unsigned char getType() const {
392    return (unsigned char) (r_info & 0xffffffffL);
393  }
394  void setSymbol(uint64_t s) { setSymbolAndType(s, getType()); }
395  void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
396  void setSymbolAndType(uint64_t s, unsigned char t) {
397    r_info = (s << 32) + (t&0xffffffffL);
398  }
399};
400
401template<endianness target_endianness, std::size_t max_alignment, bool isRela>
402struct Elf_Rel_Impl<target_endianness, max_alignment, false, isRela>
403       : Elf_Rel_Base<target_endianness, max_alignment, false, isRela> {
404  using Elf_Rel_Base<target_endianness, max_alignment, false, isRela>::r_info;
405  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
406
407  // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
408  // and ELF32_R_INFO macros defined in the ELF specification:
409  uint32_t getSymbol() const { return (r_info >> 8); }
410  unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); }
411  void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); }
412  void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
413  void setSymbolAndType(uint32_t s, unsigned char t) {
414    r_info = (s << 8) + t;
415  }
416};
417
418template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
419struct Elf_Ehdr_Impl {
420  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
421  unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
422  Elf_Half e_type;     // Type of file (see ET_*)
423  Elf_Half e_machine;  // Required architecture for this file (see EM_*)
424  Elf_Word e_version;  // Must be equal to 1
425  Elf_Addr e_entry;    // Address to jump to in order to start program
426  Elf_Off  e_phoff;    // Program header table's file offset, in bytes
427  Elf_Off  e_shoff;    // Section header table's file offset, in bytes
428  Elf_Word e_flags;    // Processor-specific flags
429  Elf_Half e_ehsize;   // Size of ELF header, in bytes
430  Elf_Half e_phentsize;// Size of an entry in the program header table
431  Elf_Half e_phnum;    // Number of entries in the program header table
432  Elf_Half e_shentsize;// Size of an entry in the section header table
433  Elf_Half e_shnum;    // Number of entries in the section header table
434  Elf_Half e_shstrndx; // Section header table index of section name
435                                 // string table
436  bool checkMagic() const {
437    return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
438  }
439   unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
440   unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
441};
442
443template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
444struct Elf_Phdr;
445
446template<endianness target_endianness, std::size_t max_alignment>
447struct Elf_Phdr<target_endianness, max_alignment, false> {
448  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, false)
449  Elf_Word p_type;   // Type of segment
450  Elf_Off  p_offset; // FileOffset where segment is located, in bytes
451  Elf_Addr p_vaddr;  // Virtual Address of beginning of segment
452  Elf_Addr p_paddr;  // Physical address of beginning of segment (OS-specific)
453  Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
454  Elf_Word p_memsz;  // Num. of bytes in mem image of segment (may be zero)
455  Elf_Word p_flags;  // Segment flags
456  Elf_Word p_align;  // Segment alignment constraint
457};
458
459template<endianness target_endianness, std::size_t max_alignment>
460struct Elf_Phdr<target_endianness, max_alignment, true> {
461  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, true)
462  Elf_Word p_type;   // Type of segment
463  Elf_Word p_flags;  // Segment flags
464  Elf_Off  p_offset; // FileOffset where segment is located, in bytes
465  Elf_Addr p_vaddr;  // Virtual Address of beginning of segment
466  Elf_Addr p_paddr;  // Physical address of beginning of segment (OS-specific)
467  Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
468  Elf_Word p_memsz;  // Num. of bytes in mem image of segment (may be zero)
469  Elf_Word p_align;  // Segment alignment constraint
470};
471
472template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
473class ELFObjectFile : public ObjectFile {
474  LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
475
476  typedef Elf_Ehdr_Impl<target_endianness, max_alignment, is64Bits> Elf_Ehdr;
477  typedef Elf_Shdr_Impl<target_endianness, max_alignment, is64Bits> Elf_Shdr;
478  typedef Elf_Sym_Impl<target_endianness, max_alignment, is64Bits> Elf_Sym;
479  typedef Elf_Dyn_Impl<target_endianness, max_alignment, is64Bits> Elf_Dyn;
480  typedef
481    Elf_Rel_Impl<target_endianness, max_alignment, is64Bits, false> Elf_Rel;
482  typedef
483    Elf_Rel_Impl<target_endianness, max_alignment, is64Bits, true> Elf_Rela;
484  typedef
485    Elf_Verdef_Impl<target_endianness, max_alignment, is64Bits> Elf_Verdef;
486  typedef
487    Elf_Verdaux_Impl<target_endianness, max_alignment, is64Bits> Elf_Verdaux;
488  typedef
489    Elf_Verneed_Impl<target_endianness, max_alignment, is64Bits> Elf_Verneed;
490  typedef
491    Elf_Vernaux_Impl<target_endianness, max_alignment, is64Bits> Elf_Vernaux;
492  typedef
493    Elf_Versym_Impl<target_endianness, max_alignment, is64Bits> Elf_Versym;
494  typedef DynRefImpl<target_endianness, max_alignment, is64Bits> DynRef;
495  typedef content_iterator<DynRef> dyn_iterator;
496
497protected:
498  // This flag is used for classof, to distinguish ELFObjectFile from
499  // its subclass. If more subclasses will be created, this flag will
500  // have to become an enum.
501  bool isDyldELFObject;
502
503private:
504  typedef SmallVector<const Elf_Shdr*, 1> Sections_t;
505  typedef DenseMap<unsigned, unsigned> IndexMap_t;
506  typedef DenseMap<const Elf_Shdr*, SmallVector<uint32_t, 1> > RelocMap_t;
507
508  const Elf_Ehdr *Header;
509  const Elf_Shdr *SectionHeaderTable;
510  const Elf_Shdr *dot_shstrtab_sec; // Section header string table.
511  const Elf_Shdr *dot_strtab_sec;   // Symbol header string table.
512  const Elf_Shdr *dot_dynstr_sec;   // Dynamic symbol string table.
513
514  // SymbolTableSections[0] always points to the dynamic string table section
515  // header, or NULL if there is no dynamic string table.
516  Sections_t SymbolTableSections;
517  IndexMap_t SymbolTableSectionsIndexMap;
518  DenseMap<const Elf_Sym*, ELF::Elf64_Word> ExtendedSymbolTable;
519
520  const Elf_Shdr *dot_dynamic_sec;       // .dynamic
521  const Elf_Shdr *dot_gnu_version_sec;   // .gnu.version
522  const Elf_Shdr *dot_gnu_version_r_sec; // .gnu.version_r
523  const Elf_Shdr *dot_gnu_version_d_sec; // .gnu.version_d
524
525  // Pointer to SONAME entry in dynamic string table
526  // This is set the first time getLoadName is called.
527  mutable const char *dt_soname;
528
529public:
530  /// \brief Iterate over constant sized entities.
531  template<class EntT>
532  class ELFEntityIterator {
533  public:
534    typedef void difference_type;
535    typedef EntT value_type;
536    typedef std::forward_iterator_tag iterator_category;
537    typedef value_type &reference;
538    typedef value_type *pointer;
539
540    /// \brief Default construct iterator.
541    ELFEntityIterator() : EntitySize(0), Current(0) {}
542    ELFEntityIterator(uint64_t EntSize, const char *Start)
543      : EntitySize(EntSize)
544      , Current(Start) {}
545
546    reference operator *() {
547      assert(Current && "Attempted to dereference an invalid iterator!");
548      return *reinterpret_cast<pointer>(Current);
549    }
550
551    pointer operator ->() {
552      assert(Current && "Attempted to dereference an invalid iterator!");
553      return reinterpret_cast<pointer>(Current);
554    }
555
556    bool operator ==(const ELFEntityIterator &Other) {
557      return Current == Other.Current;
558    }
559
560    bool operator !=(const ELFEntityIterator &Other) {
561      return !(*this == Other);
562    }
563
564    ELFEntityIterator &operator ++() {
565      assert(Current && "Attempted to increment an invalid iterator!");
566      Current += EntitySize;
567      return *this;
568    }
569
570    ELFEntityIterator operator ++(int) {
571      ELFEntityIterator Tmp = *this;
572      ++*this;
573      return Tmp;
574    }
575
576  private:
577    const uint64_t EntitySize;
578    const char *Current;
579  };
580
581private:
582  // Records for each version index the corresponding Verdef or Vernaux entry.
583  // This is filled the first time LoadVersionMap() is called.
584  class VersionMapEntry : public PointerIntPair<const void*, 1> {
585    public:
586    // If the integer is 0, this is an Elf_Verdef*.
587    // If the integer is 1, this is an Elf_Vernaux*.
588    VersionMapEntry() : PointerIntPair<const void*, 1>(NULL, 0) { }
589    VersionMapEntry(const Elf_Verdef *verdef)
590        : PointerIntPair<const void*, 1>(verdef, 0) { }
591    VersionMapEntry(const Elf_Vernaux *vernaux)
592        : PointerIntPair<const void*, 1>(vernaux, 1) { }
593    bool isNull() const { return getPointer() == NULL; }
594    bool isVerdef() const { return !isNull() && getInt() == 0; }
595    bool isVernaux() const { return !isNull() && getInt() == 1; }
596    const Elf_Verdef *getVerdef() const {
597      return isVerdef() ? (const Elf_Verdef*)getPointer() : NULL;
598    }
599    const Elf_Vernaux *getVernaux() const {
600      return isVernaux() ? (const Elf_Vernaux*)getPointer() : NULL;
601    }
602  };
603  mutable SmallVector<VersionMapEntry, 16> VersionMap;
604  void LoadVersionDefs(const Elf_Shdr *sec) const;
605  void LoadVersionNeeds(const Elf_Shdr *ec) const;
606  void LoadVersionMap() const;
607
608  /// @brief Map sections to an array of relocation sections that reference
609  ///        them sorted by section index.
610  RelocMap_t SectionRelocMap;
611
612  /// @brief Get the relocation section that contains \a Rel.
613  const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
614    return getSection(Rel.w.b);
615  }
616
617  bool            isRelocationHasAddend(DataRefImpl Rel) const;
618  template<typename T>
619  const T        *getEntry(uint16_t Section, uint32_t Entry) const;
620  template<typename T>
621  const T        *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
622  const Elf_Shdr *getSection(DataRefImpl index) const;
623  const Elf_Shdr *getSection(uint32_t index) const;
624  const Elf_Rel  *getRel(DataRefImpl Rel) const;
625  const Elf_Rela *getRela(DataRefImpl Rela) const;
626  const char     *getString(uint32_t section, uint32_t offset) const;
627  const char     *getString(const Elf_Shdr *section, uint32_t offset) const;
628  error_code      getSymbolVersion(const Elf_Shdr *section,
629                                   const Elf_Sym *Symb,
630                                   StringRef &Version,
631                                   bool &IsDefault) const;
632  void VerifyStrTab(const Elf_Shdr *sh) const;
633
634protected:
635  const Elf_Sym  *getSymbol(DataRefImpl Symb) const; // FIXME: Should be private?
636  void            validateSymbol(DataRefImpl Symb) const;
637
638public:
639  error_code      getSymbolName(const Elf_Shdr *section,
640                                const Elf_Sym *Symb,
641                                StringRef &Res) const;
642  error_code      getSectionName(const Elf_Shdr *section,
643                                 StringRef &Res) const;
644  const Elf_Dyn  *getDyn(DataRefImpl DynData) const;
645  error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
646                              bool &IsDefault) const;
647  uint64_t getSymbolIndex(const Elf_Sym *sym) const;
648protected:
649  virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
650  virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
651  virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
652  virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
653  virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
654  virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
655  virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
656  virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
657  virtual error_code getSymbolSection(DataRefImpl Symb,
658                                      section_iterator &Res) const;
659  virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
660
661  friend class DynRefImpl<target_endianness, max_alignment, is64Bits>;
662  virtual error_code getDynNext(DataRefImpl DynData, DynRef &Result) const;
663
664  virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const;
665  virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const;
666
667  virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
668  virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
669  virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
670  virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
671  virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
672  virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
673  virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
674  virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
675  virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
676  virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
677                                                   bool &Res) const;
678  virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
679  virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
680  virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
681  virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
682                                           bool &Result) const;
683  virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
684  virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
685
686  virtual error_code getRelocationNext(DataRefImpl Rel,
687                                       RelocationRef &Res) const;
688  virtual error_code getRelocationAddress(DataRefImpl Rel,
689                                          uint64_t &Res) const;
690  virtual error_code getRelocationOffset(DataRefImpl Rel,
691                                         uint64_t &Res) const;
692  virtual error_code getRelocationSymbol(DataRefImpl Rel,
693                                         SymbolRef &Res) const;
694  virtual error_code getRelocationType(DataRefImpl Rel,
695                                       uint64_t &Res) const;
696  virtual error_code getRelocationTypeName(DataRefImpl Rel,
697                                           SmallVectorImpl<char> &Result) const;
698  virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
699                                                 int64_t &Res) const;
700  virtual error_code getRelocationValueString(DataRefImpl Rel,
701                                           SmallVectorImpl<char> &Result) const;
702
703public:
704  ELFObjectFile(MemoryBuffer *Object, error_code &ec);
705  virtual symbol_iterator begin_symbols() const;
706  virtual symbol_iterator end_symbols() const;
707
708  virtual symbol_iterator begin_dynamic_symbols() const;
709  virtual symbol_iterator end_dynamic_symbols() const;
710
711  virtual section_iterator begin_sections() const;
712  virtual section_iterator end_sections() const;
713
714  virtual library_iterator begin_libraries_needed() const;
715  virtual library_iterator end_libraries_needed() const;
716
717  virtual dyn_iterator begin_dynamic_table() const;
718  virtual dyn_iterator end_dynamic_table() const;
719
720  typedef ELFEntityIterator<const Elf_Rela> Elf_Rela_Iter;
721  typedef ELFEntityIterator<const Elf_Rel> Elf_Rel_Iter;
722
723  Elf_Rela_Iter beginELFRela(const Elf_Shdr *sec) const {
724    return Elf_Rela_Iter(sec->sh_entsize,
725                         (const char *)(base() + sec->sh_offset));
726  }
727
728  Elf_Rela_Iter endELFRela(const Elf_Shdr *sec) const {
729    return Elf_Rela_Iter(sec->sh_entsize, (const char *)
730                         (base() + sec->sh_offset + sec->sh_size));
731  }
732
733  Elf_Rel_Iter beginELFRel(const Elf_Shdr *sec) const {
734    return Elf_Rel_Iter(sec->sh_entsize,
735                        (const char *)(base() + sec->sh_offset));
736  }
737
738  Elf_Rel_Iter endELFRel(const Elf_Shdr *sec) const {
739    return Elf_Rel_Iter(sec->sh_entsize, (const char *)
740                        (base() + sec->sh_offset + sec->sh_size));
741  }
742
743  virtual uint8_t getBytesInAddress() const;
744  virtual StringRef getFileFormatName() const;
745  virtual StringRef getObjectType() const { return "ELF"; }
746  virtual unsigned getArch() const;
747  virtual StringRef getLoadName() const;
748  virtual error_code getSectionContents(const Elf_Shdr *sec,
749                                        StringRef &Res) const;
750
751  uint64_t getNumSections() const;
752  uint64_t getStringTableIndex() const;
753  ELF::Elf64_Word getSymbolTableIndex(const Elf_Sym *symb) const;
754  const Elf_Shdr *getSection(const Elf_Sym *symb) const;
755  const Elf_Shdr *getElfSection(section_iterator &It) const;
756  const Elf_Sym *getElfSymbol(symbol_iterator &It) const;
757  const Elf_Sym *getElfSymbol(uint32_t index) const;
758
759  // Methods for type inquiry through isa, cast, and dyn_cast
760  bool isDyldType() const { return isDyldELFObject; }
761  static inline bool classof(const Binary *v) {
762    return v->getType() == getELFType(target_endianness == support::little,
763                                      is64Bits);
764  }
765};
766
767// Iterate through the version definitions, and place each Elf_Verdef
768// in the VersionMap according to its index.
769template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
770void ELFObjectFile<target_endianness, max_alignment, is64Bits>::
771                  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);
789    p += vd->vd_next;
790  }
791}
792
793// Iterate through the versions needed section, and place each Elf_Vernaux
794// in the VersionMap according to its index.
795template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
796void ELFObjectFile<target_endianness, max_alignment, is64Bits>::
797                  LoadVersionNeeds(const Elf_Shdr *sec) const {
798  unsigned vn_size = sec->sh_size; // Size of section in bytes
799  unsigned vn_count = sec->sh_info; // Number of Verneed entries
800  const char *sec_start = (const char*)base() + sec->sh_offset;
801  const char *sec_end = sec_start + vn_size;
802  // The first Verneed entry is at the start of the section.
803  const char *p = sec_start;
804  for (unsigned i = 0; i < vn_count; i++) {
805    if (p + sizeof(Elf_Verneed) > sec_end)
806      report_fatal_error("Section ended unexpectedly while scanning "
807                         "version needed records.");
808    const Elf_Verneed *vn = reinterpret_cast<const Elf_Verneed *>(p);
809    if (vn->vn_version != ELF::VER_NEED_CURRENT)
810      report_fatal_error("Unexpected verneed version");
811    // Iterate through the Vernaux entries
812    const char *paux = p + vn->vn_aux;
813    for (unsigned j = 0; j < vn->vn_cnt; j++) {
814      if (paux + sizeof(Elf_Vernaux) > sec_end)
815        report_fatal_error("Section ended unexpected while scanning auxiliary "
816                           "version needed records.");
817      const Elf_Vernaux *vna = reinterpret_cast<const Elf_Vernaux *>(paux);
818      size_t index = vna->vna_other & ELF::VERSYM_VERSION;
819      if (index >= VersionMap.size())
820        VersionMap.resize(index+1);
821      VersionMap[index] = VersionMapEntry(vna);
822      paux += vna->vna_next;
823    }
824    p += vn->vn_next;
825  }
826}
827
828template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
829void ELFObjectFile<target_endianness, max_alignment, is64Bits>
830                  ::LoadVersionMap() const {
831  // If there is no dynamic symtab or version table, there is nothing to do.
832  if (SymbolTableSections[0] == NULL || dot_gnu_version_sec == NULL)
833    return;
834
835  // Has the VersionMap already been loaded?
836  if (VersionMap.size() > 0)
837    return;
838
839  // The first two version indexes are reserved.
840  // Index 0 is LOCAL, index 1 is GLOBAL.
841  VersionMap.push_back(VersionMapEntry());
842  VersionMap.push_back(VersionMapEntry());
843
844  if (dot_gnu_version_d_sec)
845    LoadVersionDefs(dot_gnu_version_d_sec);
846
847  if (dot_gnu_version_r_sec)
848    LoadVersionNeeds(dot_gnu_version_r_sec);
849}
850
851template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
852void ELFObjectFile<target_endianness, max_alignment, is64Bits>
853                  ::validateSymbol(DataRefImpl Symb) const {
854  const Elf_Sym  *symb = getSymbol(Symb);
855  const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
856  // FIXME: We really need to do proper error handling in the case of an invalid
857  //        input file. Because we don't use exceptions, I think we'll just pass
858  //        an error object around.
859  if (!(  symb
860        && SymbolTableSection
861        && symb >= (const Elf_Sym*)(base()
862                   + SymbolTableSection->sh_offset)
863        && symb <  (const Elf_Sym*)(base()
864                   + SymbolTableSection->sh_offset
865                   + SymbolTableSection->sh_size)))
866    // FIXME: Proper error handling.
867    report_fatal_error("Symb must point to a valid symbol!");
868}
869
870template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
871error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
872                        ::getSymbolNext(DataRefImpl Symb,
873                                        SymbolRef &Result) const {
874  validateSymbol(Symb);
875  const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
876
877  ++Symb.d.a;
878  // Check to see if we are at the end of this symbol table.
879  if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
880    // We are at the end. If there are other symbol tables, jump to them.
881    // If the symbol table is .dynsym, we are iterating dynamic symbols,
882    // and there is only one table of these.
883    if (Symb.d.b != 0) {
884      ++Symb.d.b;
885      Symb.d.a = 1; // The 0th symbol in ELF is fake.
886    }
887    // Otherwise return the terminator.
888    if (Symb.d.b == 0 || Symb.d.b >= SymbolTableSections.size()) {
889      Symb.d.a = std::numeric_limits<uint32_t>::max();
890      Symb.d.b = std::numeric_limits<uint32_t>::max();
891    }
892  }
893
894  Result = SymbolRef(Symb, this);
895  return object_error::success;
896}
897
898template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
899error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
900                        ::getSymbolName(DataRefImpl Symb,
901                                        StringRef &Result) const {
902  validateSymbol(Symb);
903  const Elf_Sym *symb = getSymbol(Symb);
904  return getSymbolName(SymbolTableSections[Symb.d.b], symb, Result);
905}
906
907template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
908error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
909                        ::getSymbolVersion(SymbolRef SymRef,
910                                           StringRef &Version,
911                                           bool &IsDefault) const {
912  DataRefImpl Symb = SymRef.getRawDataRefImpl();
913  validateSymbol(Symb);
914  const Elf_Sym *symb = getSymbol(Symb);
915  return getSymbolVersion(SymbolTableSections[Symb.d.b], symb,
916                          Version, IsDefault);
917}
918
919template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
920ELF::Elf64_Word ELFObjectFile<target_endianness, max_alignment, is64Bits>
921                      ::getSymbolTableIndex(const Elf_Sym *symb) const {
922  if (symb->st_shndx == ELF::SHN_XINDEX)
923    return ExtendedSymbolTable.lookup(symb);
924  return symb->st_shndx;
925}
926
927template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
928const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
929                            ::Elf_Shdr *
930ELFObjectFile<target_endianness, max_alignment, is64Bits>
931                             ::getSection(const Elf_Sym *symb) const {
932  if (symb->st_shndx == ELF::SHN_XINDEX)
933    return getSection(ExtendedSymbolTable.lookup(symb));
934  if (symb->st_shndx >= ELF::SHN_LORESERVE)
935    return 0;
936  return getSection(symb->st_shndx);
937}
938
939template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
940const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
941                            ::Elf_Shdr *
942ELFObjectFile<target_endianness, max_alignment, is64Bits>
943                             ::getElfSection(section_iterator &It) const {
944  llvm::object::DataRefImpl ShdrRef = It->getRawDataRefImpl();
945  return reinterpret_cast<const Elf_Shdr *>(ShdrRef.p);
946}
947
948template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
949const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
950                            ::Elf_Sym *
951ELFObjectFile<target_endianness, max_alignment, is64Bits>
952                             ::getElfSymbol(symbol_iterator &It) const {
953  return getSymbol(It->getRawDataRefImpl());
954}
955
956template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
957const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
958                            ::Elf_Sym *
959ELFObjectFile<target_endianness, max_alignment, is64Bits>
960                             ::getElfSymbol(uint32_t index) const {
961  DataRefImpl SymbolData;
962  SymbolData.d.a = index;
963  SymbolData.d.b = 1;
964  return getSymbol(SymbolData);
965}
966
967template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
968error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
969                        ::getSymbolFileOffset(DataRefImpl Symb,
970                                          uint64_t &Result) const {
971  validateSymbol(Symb);
972  const Elf_Sym  *symb = getSymbol(Symb);
973  const Elf_Shdr *Section;
974  switch (getSymbolTableIndex(symb)) {
975  case ELF::SHN_COMMON:
976   // Unintialized symbols have no offset in the object file
977  case ELF::SHN_UNDEF:
978    Result = UnknownAddressOrSize;
979    return object_error::success;
980  case ELF::SHN_ABS:
981    Result = symb->st_value;
982    return object_error::success;
983  default: Section = getSection(symb);
984  }
985
986  switch (symb->getType()) {
987  case ELF::STT_SECTION:
988    Result = Section ? Section->sh_addr : UnknownAddressOrSize;
989    return object_error::success;
990  case ELF::STT_FUNC:
991  case ELF::STT_OBJECT:
992  case ELF::STT_NOTYPE:
993    Result = symb->st_value +
994             (Section ? Section->sh_offset : 0);
995    return object_error::success;
996  default:
997    Result = UnknownAddressOrSize;
998    return object_error::success;
999  }
1000}
1001
1002template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1003error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1004                        ::getSymbolAddress(DataRefImpl Symb,
1005                                           uint64_t &Result) const {
1006  validateSymbol(Symb);
1007  const Elf_Sym  *symb = getSymbol(Symb);
1008  const Elf_Shdr *Section;
1009  switch (getSymbolTableIndex(symb)) {
1010  case ELF::SHN_COMMON:
1011  case ELF::SHN_UNDEF:
1012    Result = UnknownAddressOrSize;
1013    return object_error::success;
1014  case ELF::SHN_ABS:
1015    Result = symb->st_value;
1016    return object_error::success;
1017  default: Section = getSection(symb);
1018  }
1019
1020  switch (symb->getType()) {
1021  case ELF::STT_SECTION:
1022    Result = Section ? Section->sh_addr : UnknownAddressOrSize;
1023    return object_error::success;
1024  case ELF::STT_FUNC:
1025  case ELF::STT_OBJECT:
1026  case ELF::STT_NOTYPE:
1027    bool IsRelocatable;
1028    switch(Header->e_type) {
1029    case ELF::ET_EXEC:
1030    case ELF::ET_DYN:
1031      IsRelocatable = false;
1032      break;
1033    default:
1034      IsRelocatable = true;
1035    }
1036    Result = symb->st_value;
1037    if (IsRelocatable && Section != 0)
1038      Result += Section->sh_addr;
1039    return object_error::success;
1040  default:
1041    Result = UnknownAddressOrSize;
1042    return object_error::success;
1043  }
1044}
1045
1046template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1047error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1048                        ::getSymbolSize(DataRefImpl Symb,
1049                                        uint64_t &Result) const {
1050  validateSymbol(Symb);
1051  const Elf_Sym  *symb = getSymbol(Symb);
1052  if (symb->st_size == 0)
1053    Result = UnknownAddressOrSize;
1054  Result = symb->st_size;
1055  return object_error::success;
1056}
1057
1058template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1059error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1060                        ::getSymbolNMTypeChar(DataRefImpl Symb,
1061                                              char &Result) const {
1062  validateSymbol(Symb);
1063  const Elf_Sym  *symb = getSymbol(Symb);
1064  const Elf_Shdr *Section = getSection(symb);
1065
1066  char ret = '?';
1067
1068  if (Section) {
1069    switch (Section->sh_type) {
1070    case ELF::SHT_PROGBITS:
1071    case ELF::SHT_DYNAMIC:
1072      switch (Section->sh_flags) {
1073      case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
1074        ret = 't'; break;
1075      case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
1076        ret = 'd'; break;
1077      case ELF::SHF_ALLOC:
1078      case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
1079      case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
1080        ret = 'r'; break;
1081      }
1082      break;
1083    case ELF::SHT_NOBITS: ret = 'b';
1084    }
1085  }
1086
1087  switch (getSymbolTableIndex(symb)) {
1088  case ELF::SHN_UNDEF:
1089    if (ret == '?')
1090      ret = 'U';
1091    break;
1092  case ELF::SHN_ABS: ret = 'a'; break;
1093  case ELF::SHN_COMMON: ret = 'c'; break;
1094  }
1095
1096  switch (symb->getBinding()) {
1097  case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
1098  case ELF::STB_WEAK:
1099    if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1100      ret = 'w';
1101    else
1102      if (symb->getType() == ELF::STT_OBJECT)
1103        ret = 'V';
1104      else
1105        ret = 'W';
1106  }
1107
1108  if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
1109    StringRef name;
1110    if (error_code ec = getSymbolName(Symb, name))
1111      return ec;
1112    Result = StringSwitch<char>(name)
1113      .StartsWith(".debug", 'N')
1114      .StartsWith(".note", 'n')
1115      .Default('?');
1116    return object_error::success;
1117  }
1118
1119  Result = ret;
1120  return object_error::success;
1121}
1122
1123template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1124error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1125                        ::getSymbolType(DataRefImpl Symb,
1126                                        SymbolRef::Type &Result) const {
1127  validateSymbol(Symb);
1128  const Elf_Sym  *symb = getSymbol(Symb);
1129
1130  switch (symb->getType()) {
1131  case ELF::STT_NOTYPE:
1132    Result = SymbolRef::ST_Unknown;
1133    break;
1134  case ELF::STT_SECTION:
1135    Result = SymbolRef::ST_Debug;
1136    break;
1137  case ELF::STT_FILE:
1138    Result = SymbolRef::ST_File;
1139    break;
1140  case ELF::STT_FUNC:
1141    Result = SymbolRef::ST_Function;
1142    break;
1143  case ELF::STT_OBJECT:
1144  case ELF::STT_COMMON:
1145  case ELF::STT_TLS:
1146    Result = SymbolRef::ST_Data;
1147    break;
1148  default:
1149    Result = SymbolRef::ST_Other;
1150    break;
1151  }
1152  return object_error::success;
1153}
1154
1155template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1156error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1157                        ::getSymbolFlags(DataRefImpl Symb,
1158                                         uint32_t &Result) const {
1159  validateSymbol(Symb);
1160  const Elf_Sym  *symb = getSymbol(Symb);
1161
1162  Result = SymbolRef::SF_None;
1163
1164  if (symb->getBinding() != ELF::STB_LOCAL)
1165    Result |= SymbolRef::SF_Global;
1166
1167  if (symb->getBinding() == ELF::STB_WEAK)
1168    Result |= SymbolRef::SF_Weak;
1169
1170  if (symb->st_shndx == ELF::SHN_ABS)
1171    Result |= SymbolRef::SF_Absolute;
1172
1173  if (symb->getType() == ELF::STT_FILE ||
1174      symb->getType() == ELF::STT_SECTION)
1175    Result |= SymbolRef::SF_FormatSpecific;
1176
1177  if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1178    Result |= SymbolRef::SF_Undefined;
1179
1180  if (symb->getType() == ELF::STT_COMMON ||
1181      getSymbolTableIndex(symb) == ELF::SHN_COMMON)
1182    Result |= SymbolRef::SF_Common;
1183
1184  if (symb->getType() == ELF::STT_TLS)
1185    Result |= SymbolRef::SF_ThreadLocal;
1186
1187  return object_error::success;
1188}
1189
1190template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1191error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1192                        ::getSymbolSection(DataRefImpl Symb,
1193                                           section_iterator &Res) const {
1194  validateSymbol(Symb);
1195  const Elf_Sym  *symb = getSymbol(Symb);
1196  const Elf_Shdr *sec = getSection(symb);
1197  if (!sec)
1198    Res = end_sections();
1199  else {
1200    DataRefImpl Sec;
1201    Sec.p = reinterpret_cast<intptr_t>(sec);
1202    Res = section_iterator(SectionRef(Sec, this));
1203  }
1204  return object_error::success;
1205}
1206
1207template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1208error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1209                        ::getSymbolValue(DataRefImpl Symb,
1210                                         uint64_t &Val) const {
1211  validateSymbol(Symb);
1212  const Elf_Sym *symb = getSymbol(Symb);
1213  Val = symb->st_value;
1214  return object_error::success;
1215}
1216
1217template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1218error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1219                        ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
1220  const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
1221  sec += Header->e_shentsize;
1222  Sec.p = reinterpret_cast<intptr_t>(sec);
1223  Result = SectionRef(Sec, this);
1224  return object_error::success;
1225}
1226
1227template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1228error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1229                        ::getSectionName(DataRefImpl Sec,
1230                                         StringRef &Result) const {
1231  const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1232  Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
1233  return object_error::success;
1234}
1235
1236template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1237error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1238                        ::getSectionAddress(DataRefImpl Sec,
1239                                            uint64_t &Result) const {
1240  const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1241  Result = sec->sh_addr;
1242  return object_error::success;
1243}
1244
1245template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1246error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1247                        ::getSectionSize(DataRefImpl Sec,
1248                                         uint64_t &Result) const {
1249  const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1250  Result = sec->sh_size;
1251  return object_error::success;
1252}
1253
1254template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1255error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1256                        ::getSectionContents(DataRefImpl Sec,
1257                                             StringRef &Result) const {
1258  const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1259  const char *start = (const char*)base() + sec->sh_offset;
1260  Result = StringRef(start, sec->sh_size);
1261  return object_error::success;
1262}
1263
1264template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1265error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1266                        ::getSectionContents(const Elf_Shdr *Sec,
1267                                             StringRef &Result) const {
1268  const char *start = (const char*)base() + Sec->sh_offset;
1269  Result = StringRef(start, Sec->sh_size);
1270  return object_error::success;
1271}
1272
1273template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1274error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1275                        ::getSectionAlignment(DataRefImpl Sec,
1276                                              uint64_t &Result) const {
1277  const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1278  Result = sec->sh_addralign;
1279  return object_error::success;
1280}
1281
1282template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1283error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1284                        ::isSectionText(DataRefImpl Sec,
1285                                        bool &Result) const {
1286  const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1287  if (sec->sh_flags & ELF::SHF_EXECINSTR)
1288    Result = true;
1289  else
1290    Result = false;
1291  return object_error::success;
1292}
1293
1294template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1295error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1296                        ::isSectionData(DataRefImpl Sec,
1297                                        bool &Result) const {
1298  const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1299  if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1300      && sec->sh_type == ELF::SHT_PROGBITS)
1301    Result = true;
1302  else
1303    Result = false;
1304  return object_error::success;
1305}
1306
1307template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1308error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1309                        ::isSectionBSS(DataRefImpl Sec,
1310                                       bool &Result) const {
1311  const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1312  if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1313      && sec->sh_type == ELF::SHT_NOBITS)
1314    Result = true;
1315  else
1316    Result = false;
1317  return object_error::success;
1318}
1319
1320template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1321error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1322                        ::isSectionRequiredForExecution(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_ALLOC)
1326    Result = true;
1327  else
1328    Result = false;
1329  return object_error::success;
1330}
1331
1332template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1333error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1334                        ::isSectionVirtual(DataRefImpl Sec,
1335                                           bool &Result) const {
1336  const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1337  if (sec->sh_type == ELF::SHT_NOBITS)
1338    Result = true;
1339  else
1340    Result = false;
1341  return object_error::success;
1342}
1343
1344template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1345error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1346                        ::isSectionZeroInit(DataRefImpl Sec,
1347                                            bool &Result) const {
1348  const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1349  // For ELF, all zero-init sections are virtual (that is, they occupy no space
1350  //   in the object image) and vice versa.
1351  Result = sec->sh_type == ELF::SHT_NOBITS;
1352  return object_error::success;
1353}
1354
1355template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1356error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1357                       ::isSectionReadOnlyData(DataRefImpl Sec,
1358                                               bool &Result) const {
1359  const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1360  if (sec->sh_flags & ELF::SHF_WRITE || sec->sh_flags & ELF::SHF_EXECINSTR)
1361    Result = false;
1362  else
1363    Result = true;
1364  return object_error::success;
1365}
1366
1367template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1368error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1369                          ::sectionContainsSymbol(DataRefImpl Sec,
1370                                                  DataRefImpl Symb,
1371                                                  bool &Result) const {
1372  // FIXME: Unimplemented.
1373  Result = false;
1374  return object_error::success;
1375}
1376
1377template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1378relocation_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
1379                                 ::getSectionRelBegin(DataRefImpl Sec) const {
1380  DataRefImpl RelData;
1381  const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1382  typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1383  if (sec != 0 && ittr != SectionRelocMap.end()) {
1384    RelData.w.a = getSection(ittr->second[0])->sh_info;
1385    RelData.w.b = ittr->second[0];
1386    RelData.w.c = 0;
1387  }
1388  return relocation_iterator(RelocationRef(RelData, this));
1389}
1390
1391template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1392relocation_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
1393                                 ::getSectionRelEnd(DataRefImpl Sec) const {
1394  DataRefImpl RelData;
1395  const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1396  typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1397  if (sec != 0 && ittr != SectionRelocMap.end()) {
1398    // Get the index of the last relocation section for this section.
1399    std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
1400    const Elf_Shdr *relocsec = getSection(relocsecindex);
1401    RelData.w.a = relocsec->sh_info;
1402    RelData.w.b = relocsecindex;
1403    RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
1404  }
1405  return relocation_iterator(RelocationRef(RelData, this));
1406}
1407
1408// Relocations
1409template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1410error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1411                        ::getRelocationNext(DataRefImpl Rel,
1412                                            RelocationRef &Result) const {
1413  ++Rel.w.c;
1414  const Elf_Shdr *relocsec = getSection(Rel.w.b);
1415  if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
1416    // We have reached the end of the relocations for this section. See if there
1417    // is another relocation section.
1418    typename RelocMap_t::mapped_type relocseclist =
1419      SectionRelocMap.lookup(getSection(Rel.w.a));
1420
1421    // Do a binary search for the current reloc section index (which must be
1422    // present). Then get the next one.
1423    typename RelocMap_t::mapped_type::const_iterator loc =
1424      std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
1425    ++loc;
1426
1427    // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
1428    // to the end iterator.
1429    if (loc != relocseclist.end()) {
1430      Rel.w.b = *loc;
1431      Rel.w.a = 0;
1432    }
1433  }
1434  Result = RelocationRef(Rel, this);
1435  return object_error::success;
1436}
1437
1438template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1439error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1440                        ::getRelocationSymbol(DataRefImpl Rel,
1441                                              SymbolRef &Result) const {
1442  uint32_t symbolIdx;
1443  const Elf_Shdr *sec = getSection(Rel.w.b);
1444  switch (sec->sh_type) {
1445    default :
1446      report_fatal_error("Invalid section type in Rel!");
1447    case ELF::SHT_REL : {
1448      symbolIdx = getRel(Rel)->getSymbol();
1449      break;
1450    }
1451    case ELF::SHT_RELA : {
1452      symbolIdx = getRela(Rel)->getSymbol();
1453      break;
1454    }
1455  }
1456  DataRefImpl SymbolData;
1457  IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
1458  if (it == SymbolTableSectionsIndexMap.end())
1459    report_fatal_error("Relocation symbol table not found!");
1460  SymbolData.d.a = symbolIdx;
1461  SymbolData.d.b = it->second;
1462  Result = SymbolRef(SymbolData, this);
1463  return object_error::success;
1464}
1465
1466template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1467error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1468                        ::getRelocationAddress(DataRefImpl Rel,
1469                                               uint64_t &Result) const {
1470  uint64_t offset;
1471  const Elf_Shdr *sec = getSection(Rel.w.b);
1472  switch (sec->sh_type) {
1473    default :
1474      report_fatal_error("Invalid section type in Rel!");
1475    case ELF::SHT_REL : {
1476      offset = getRel(Rel)->r_offset;
1477      break;
1478    }
1479    case ELF::SHT_RELA : {
1480      offset = getRela(Rel)->r_offset;
1481      break;
1482    }
1483  }
1484
1485  Result = offset;
1486  return object_error::success;
1487}
1488
1489template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1490error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1491                        ::getRelocationOffset(DataRefImpl Rel,
1492                                              uint64_t &Result) const {
1493  uint64_t offset;
1494  const Elf_Shdr *sec = getSection(Rel.w.b);
1495  switch (sec->sh_type) {
1496    default :
1497      report_fatal_error("Invalid section type in Rel!");
1498    case ELF::SHT_REL : {
1499      offset = getRel(Rel)->r_offset;
1500      break;
1501    }
1502    case ELF::SHT_RELA : {
1503      offset = getRela(Rel)->r_offset;
1504      break;
1505    }
1506  }
1507
1508  Result = offset - sec->sh_addr;
1509  return object_error::success;
1510}
1511
1512template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1513error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1514                        ::getRelocationType(DataRefImpl Rel,
1515                                            uint64_t &Result) const {
1516  const Elf_Shdr *sec = getSection(Rel.w.b);
1517  switch (sec->sh_type) {
1518    default :
1519      report_fatal_error("Invalid section type in Rel!");
1520    case ELF::SHT_REL : {
1521      Result = getRel(Rel)->getType();
1522      break;
1523    }
1524    case ELF::SHT_RELA : {
1525      Result = getRela(Rel)->getType();
1526      break;
1527    }
1528  }
1529  return object_error::success;
1530}
1531
1532#define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
1533  case ELF::enum: res = #enum; break;
1534
1535template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1536error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1537                        ::getRelocationTypeName(DataRefImpl Rel,
1538                                          SmallVectorImpl<char> &Result) const {
1539  const Elf_Shdr *sec = getSection(Rel.w.b);
1540  uint8_t type;
1541  StringRef res;
1542  switch (sec->sh_type) {
1543    default :
1544      return object_error::parse_failed;
1545    case ELF::SHT_REL : {
1546      type = getRel(Rel)->getType();
1547      break;
1548    }
1549    case ELF::SHT_RELA : {
1550      type = getRela(Rel)->getType();
1551      break;
1552    }
1553  }
1554  switch (Header->e_machine) {
1555  case ELF::EM_X86_64:
1556    switch (type) {
1557      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
1558      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
1559      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
1560      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
1561      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
1562      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
1563      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
1564      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
1565      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
1566      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
1567      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
1568      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
1569      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
1570      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
1571      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
1572      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
1573      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
1574      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
1575      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
1576      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
1577      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
1578      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
1579      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
1580      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
1581      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
1582      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
1583      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
1584      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
1585      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
1586      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
1587      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
1588      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
1589    default:
1590      res = "Unknown";
1591    }
1592    break;
1593  case ELF::EM_386:
1594    switch (type) {
1595      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
1596      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
1597      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
1598      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
1599      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
1600      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
1601      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
1602      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
1603      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
1604      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
1605      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
1606      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
1607      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
1608      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
1609      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
1610      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
1611      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
1612      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
1613      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
1614      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
1615      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
1616      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
1617      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
1618      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
1619      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
1620      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
1621      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
1622      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
1623      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
1624      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
1625      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
1626      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
1627      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
1628      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
1629      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
1630      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
1631      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
1632      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
1633      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
1634      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
1635    default:
1636      res = "Unknown";
1637    }
1638    break;
1639  case ELF::EM_ARM:
1640    switch (type) {
1641      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_NONE);
1642      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PC24);
1643      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32);
1644      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32);
1645      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G0);
1646      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS16);
1647      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS12);
1648      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ABS5);
1649      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS8);
1650      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL32);
1651      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_CALL);
1652      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC8);
1653      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BREL_ADJ);
1654      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESC);
1655      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_SWI8);
1656      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_XPC25);
1657      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_XPC22);
1658      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPMOD32);
1659      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPOFF32);
1660      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_TPOFF32);
1661      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_COPY);
1662      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GLOB_DAT);
1663      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP_SLOT);
1664      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_RELATIVE);
1665      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF32);
1666      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_PREL);
1667      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL);
1668      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32);
1669      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_CALL);
1670      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP24);
1671      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP24);
1672      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_ABS);
1673      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_7_0);
1674      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_15_8);
1675      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_23_15);
1676      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SBREL_11_0_NC);
1677      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_19_12_NC);
1678      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_27_20_CK);
1679      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET1);
1680      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL31);
1681      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_V4BX);
1682      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET2);
1683      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PREL31);
1684      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_ABS_NC);
1685      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_ABS);
1686      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_PREL_NC);
1687      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_PREL);
1688      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_ABS_NC);
1689      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_ABS);
1690      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_PREL_NC);
1691      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_PREL);
1692      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP19);
1693      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP6);
1694      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ALU_PREL_11_0);
1695      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC12);
1696      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32_NOI);
1697      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32_NOI);
1698      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0_NC);
1699      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0);
1700      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1_NC);
1701      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1);
1702      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G2);
1703      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G1);
1704      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G2);
1705      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G0);
1706      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G1);
1707      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G2);
1708      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G0);
1709      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G1);
1710      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G2);
1711      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0_NC);
1712      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0);
1713      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1_NC);
1714      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1);
1715      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G2);
1716      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G0);
1717      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G1);
1718      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G2);
1719      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G0);
1720      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G1);
1721      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G2);
1722      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G0);
1723      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G1);
1724      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G2);
1725      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL_NC);
1726      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_BREL);
1727      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL);
1728      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL_NC);
1729      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_BREL);
1730      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL);
1731      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GOTDESC);
1732      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_CALL);
1733      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESCSEQ);
1734      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_CALL);
1735      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32_ABS);
1736      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_ABS);
1737      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_PREL);
1738      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL12);
1739      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF12);
1740      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTRELAX);
1741      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTENTRY);
1742      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTINHERIT);
1743      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP11);
1744      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP8);
1745      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GD32);
1746      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDM32);
1747      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO32);
1748      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE32);
1749      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE32);
1750      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO12);
1751      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE12);
1752      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE12GP);
1753      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_0);
1754      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_1);
1755      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_2);
1756      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_3);
1757      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_4);
1758      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_5);
1759      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_6);
1760      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_7);
1761      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_8);
1762      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_9);
1763      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_10);
1764      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_11);
1765      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_12);
1766      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_13);
1767      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_14);
1768      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_15);
1769      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ME_TOO);
1770      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ16);
1771      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ32);
1772    default:
1773      res = "Unknown";
1774    }
1775    break;
1776  case ELF::EM_HEXAGON:
1777    switch (type) {
1778      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_NONE);
1779      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL);
1780      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL);
1781      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL);
1782      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_LO16);
1783      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HI16);
1784      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32);
1785      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16);
1786      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8);
1787      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_0);
1788      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_1);
1789      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_2);
1790      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_3);
1791      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HL16);
1792      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL);
1793      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL);
1794      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B32_PCREL_X);
1795      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_6_X);
1796      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL_X);
1797      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL_X);
1798      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL_X);
1799      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL_X);
1800      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL_X);
1801      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16_X);
1802      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_12_X);
1803      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_11_X);
1804      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_10_X);
1805      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_9_X);
1806      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8_X);
1807      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_7_X);
1808      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_X);
1809      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_PCREL);
1810      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_COPY);
1811      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GLOB_DAT);
1812      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_JMP_SLOT);
1813      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_RELATIVE);
1814      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_PLT_B22_PCREL);
1815      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_LO16);
1816      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_HI16);
1817      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32);
1818      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_LO16);
1819      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_HI16);
1820      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32);
1821      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16);
1822      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPMOD_32);
1823      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_LO16);
1824      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_HI16);
1825      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32);
1826      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16);
1827      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_PLT_B22_PCREL);
1828      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_LO16);
1829      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_HI16);
1830      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32);
1831      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16);
1832      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_LO16);
1833      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_HI16);
1834      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32);
1835      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_LO16);
1836      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_HI16);
1837      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32);
1838      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16);
1839      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_LO16);
1840      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_HI16);
1841      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32);
1842      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16);
1843      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_PCREL_X);
1844      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32_6_X);
1845      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_16_X);
1846      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_11_X);
1847      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32_6_X);
1848      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16_X);
1849      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_11_X);
1850      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32_6_X);
1851      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16_X);
1852      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_11_X);
1853      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32_6_X);
1854      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16_X);
1855      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_11_X);
1856      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32_6_X);
1857      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_16_X);
1858      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32_6_X);
1859      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16_X);
1860      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_11_X);
1861      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32_6_X);
1862      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16_X);
1863      LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_11_X);
1864    default:
1865      res = "Unknown";
1866    }
1867    break;
1868  default:
1869    res = "Unknown";
1870  }
1871  Result.append(res.begin(), res.end());
1872  return object_error::success;
1873}
1874
1875#undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
1876
1877template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1878error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1879                        ::getRelocationAdditionalInfo(DataRefImpl Rel,
1880                                                      int64_t &Result) const {
1881  const Elf_Shdr *sec = getSection(Rel.w.b);
1882  switch (sec->sh_type) {
1883    default :
1884      report_fatal_error("Invalid section type in Rel!");
1885    case ELF::SHT_REL : {
1886      Result = 0;
1887      return object_error::success;
1888    }
1889    case ELF::SHT_RELA : {
1890      Result = getRela(Rel)->r_addend;
1891      return object_error::success;
1892    }
1893  }
1894}
1895
1896template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1897error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
1898                        ::getRelocationValueString(DataRefImpl Rel,
1899                                          SmallVectorImpl<char> &Result) const {
1900  const Elf_Shdr *sec = getSection(Rel.w.b);
1901  uint8_t type;
1902  StringRef res;
1903  int64_t addend = 0;
1904  uint16_t symbol_index = 0;
1905  switch (sec->sh_type) {
1906    default:
1907      return object_error::parse_failed;
1908    case ELF::SHT_REL: {
1909      type = getRel(Rel)->getType();
1910      symbol_index = getRel(Rel)->getSymbol();
1911      // TODO: Read implicit addend from section data.
1912      break;
1913    }
1914    case ELF::SHT_RELA: {
1915      type = getRela(Rel)->getType();
1916      symbol_index = getRela(Rel)->getSymbol();
1917      addend = getRela(Rel)->r_addend;
1918      break;
1919    }
1920  }
1921  const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1922  StringRef symname;
1923  if (error_code ec = getSymbolName(getSection(sec->sh_link), symb, symname))
1924    return ec;
1925  switch (Header->e_machine) {
1926  case ELF::EM_X86_64:
1927    switch (type) {
1928    case ELF::R_X86_64_PC8:
1929    case ELF::R_X86_64_PC16:
1930    case ELF::R_X86_64_PC32: {
1931        std::string fmtbuf;
1932        raw_string_ostream fmt(fmtbuf);
1933        fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1934        fmt.flush();
1935        Result.append(fmtbuf.begin(), fmtbuf.end());
1936      }
1937      break;
1938    case ELF::R_X86_64_8:
1939    case ELF::R_X86_64_16:
1940    case ELF::R_X86_64_32:
1941    case ELF::R_X86_64_32S:
1942    case ELF::R_X86_64_64: {
1943        std::string fmtbuf;
1944        raw_string_ostream fmt(fmtbuf);
1945        fmt << symname << (addend < 0 ? "" : "+") << addend;
1946        fmt.flush();
1947        Result.append(fmtbuf.begin(), fmtbuf.end());
1948      }
1949      break;
1950    default:
1951      res = "Unknown";
1952    }
1953    break;
1954  case ELF::EM_ARM:
1955  case ELF::EM_HEXAGON:
1956    res = symname;
1957    break;
1958  default:
1959    res = "Unknown";
1960  }
1961  if (Result.empty())
1962    Result.append(res.begin(), res.end());
1963  return object_error::success;
1964}
1965
1966// Verify that the last byte in the string table in a null.
1967template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1968void ELFObjectFile<target_endianness, max_alignment, is64Bits>
1969                  ::VerifyStrTab(const Elf_Shdr *sh) const {
1970  const char *strtab = (const char*)base() + sh->sh_offset;
1971  if (strtab[sh->sh_size - 1] != 0)
1972    // FIXME: Proper error handling.
1973    report_fatal_error("String table must end with a null terminator!");
1974}
1975
1976template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
1977ELFObjectFile<target_endianness, max_alignment, is64Bits>
1978             ::ELFObjectFile(MemoryBuffer *Object, error_code &ec)
1979  : ObjectFile(getELFType(target_endianness == support::little, is64Bits),
1980               Object, ec)
1981  , isDyldELFObject(false)
1982  , SectionHeaderTable(0)
1983  , dot_shstrtab_sec(0)
1984  , dot_strtab_sec(0)
1985  , dot_dynstr_sec(0)
1986  , dot_dynamic_sec(0)
1987  , dot_gnu_version_sec(0)
1988  , dot_gnu_version_r_sec(0)
1989  , dot_gnu_version_d_sec(0)
1990  , dt_soname(0)
1991 {
1992
1993  const uint64_t FileSize = Data->getBufferSize();
1994
1995  if (sizeof(Elf_Ehdr) > FileSize)
1996    // FIXME: Proper error handling.
1997    report_fatal_error("File too short!");
1998
1999  Header = reinterpret_cast<const Elf_Ehdr *>(base());
2000
2001  if (Header->e_shoff == 0)
2002    return;
2003
2004  const uint64_t SectionTableOffset = Header->e_shoff;
2005
2006  if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize)
2007    // FIXME: Proper error handling.
2008    report_fatal_error("Section header table goes past end of file!");
2009
2010  // The getNumSections() call below depends on SectionHeaderTable being set.
2011  SectionHeaderTable =
2012    reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
2013  const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
2014
2015  if (SectionTableOffset + SectionTableSize > FileSize)
2016    // FIXME: Proper error handling.
2017    report_fatal_error("Section table goes past end of file!");
2018
2019  // To find the symbol tables we walk the section table to find SHT_SYMTAB.
2020  const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
2021  const Elf_Shdr* sh = SectionHeaderTable;
2022
2023  // Reserve SymbolTableSections[0] for .dynsym
2024  SymbolTableSections.push_back(NULL);
2025
2026  for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
2027    switch (sh->sh_type) {
2028    case ELF::SHT_SYMTAB_SHNDX: {
2029      if (SymbolTableSectionHeaderIndex)
2030        // FIXME: Proper error handling.
2031        report_fatal_error("More than one .symtab_shndx!");
2032      SymbolTableSectionHeaderIndex = sh;
2033      break;
2034    }
2035    case ELF::SHT_SYMTAB: {
2036      SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
2037      SymbolTableSections.push_back(sh);
2038      break;
2039    }
2040    case ELF::SHT_DYNSYM: {
2041      if (SymbolTableSections[0] != NULL)
2042        // FIXME: Proper error handling.
2043        report_fatal_error("More than one .dynsym!");
2044      SymbolTableSectionsIndexMap[i] = 0;
2045      SymbolTableSections[0] = sh;
2046      break;
2047    }
2048    case ELF::SHT_REL:
2049    case ELF::SHT_RELA: {
2050      SectionRelocMap[getSection(sh->sh_info)].push_back(i);
2051      break;
2052    }
2053    case ELF::SHT_DYNAMIC: {
2054      if (dot_dynamic_sec != NULL)
2055        // FIXME: Proper error handling.
2056        report_fatal_error("More than one .dynamic!");
2057      dot_dynamic_sec = sh;
2058      break;
2059    }
2060    case ELF::SHT_GNU_versym: {
2061      if (dot_gnu_version_sec != NULL)
2062        // FIXME: Proper error handling.
2063        report_fatal_error("More than one .gnu.version section!");
2064      dot_gnu_version_sec = sh;
2065      break;
2066    }
2067    case ELF::SHT_GNU_verdef: {
2068      if (dot_gnu_version_d_sec != NULL)
2069        // FIXME: Proper error handling.
2070        report_fatal_error("More than one .gnu.version_d section!");
2071      dot_gnu_version_d_sec = sh;
2072      break;
2073    }
2074    case ELF::SHT_GNU_verneed: {
2075      if (dot_gnu_version_r_sec != NULL)
2076        // FIXME: Proper error handling.
2077        report_fatal_error("More than one .gnu.version_r section!");
2078      dot_gnu_version_r_sec = sh;
2079      break;
2080    }
2081    }
2082    ++sh;
2083  }
2084
2085  // Sort section relocation lists by index.
2086  for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
2087                                     e = SectionRelocMap.end(); i != e; ++i) {
2088    std::sort(i->second.begin(), i->second.end());
2089  }
2090
2091  // Get string table sections.
2092  dot_shstrtab_sec = getSection(getStringTableIndex());
2093  if (dot_shstrtab_sec) {
2094    // Verify that the last byte in the string table in a null.
2095    VerifyStrTab(dot_shstrtab_sec);
2096  }
2097
2098  // Merge this into the above loop.
2099  for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
2100                  *e = i + getNumSections() * Header->e_shentsize;
2101                   i != e; i += Header->e_shentsize) {
2102    const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
2103    if (sh->sh_type == ELF::SHT_STRTAB) {
2104      StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
2105      if (SectionName == ".strtab") {
2106        if (dot_strtab_sec != 0)
2107          // FIXME: Proper error handling.
2108          report_fatal_error("Already found section named .strtab!");
2109        dot_strtab_sec = sh;
2110        VerifyStrTab(dot_strtab_sec);
2111      } else if (SectionName == ".dynstr") {
2112        if (dot_dynstr_sec != 0)
2113          // FIXME: Proper error handling.
2114          report_fatal_error("Already found section named .dynstr!");
2115        dot_dynstr_sec = sh;
2116        VerifyStrTab(dot_dynstr_sec);
2117      }
2118    }
2119  }
2120
2121  // Build symbol name side-mapping if there is one.
2122  if (SymbolTableSectionHeaderIndex) {
2123    const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
2124                                      SymbolTableSectionHeaderIndex->sh_offset);
2125    error_code ec;
2126    for (symbol_iterator si = begin_symbols(),
2127                         se = end_symbols(); si != se; si.increment(ec)) {
2128      if (ec)
2129        report_fatal_error("Fewer extended symbol table entries than symbols!");
2130      if (*ShndxTable != ELF::SHN_UNDEF)
2131        ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
2132      ++ShndxTable;
2133    }
2134  }
2135}
2136
2137// Get the symbol table index in the symtab section given a symbol
2138template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2139uint64_t ELFObjectFile<target_endianness, max_alignment, is64Bits>
2140                      ::getSymbolIndex(const Elf_Sym *Sym) const {
2141  assert(SymbolTableSections.size() == 1 && "Only one symbol table supported!");
2142  const Elf_Shdr *SymTab = *SymbolTableSections.begin();
2143  uintptr_t SymLoc = uintptr_t(Sym);
2144  uintptr_t SymTabLoc = uintptr_t(base() + SymTab->sh_offset);
2145  assert(SymLoc > SymTabLoc && "Symbol not in symbol table!");
2146  uint64_t SymOffset = SymLoc - SymTabLoc;
2147  assert(SymOffset % SymTab->sh_entsize == 0 &&
2148         "Symbol not multiple of symbol size!");
2149  return SymOffset / SymTab->sh_entsize;
2150}
2151
2152template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2153symbol_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2154                             ::begin_symbols() const {
2155  DataRefImpl SymbolData;
2156  if (SymbolTableSections.size() <= 1) {
2157    SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2158    SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2159  } else {
2160    SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2161    SymbolData.d.b = 1; // The 0th table is .dynsym
2162  }
2163  return symbol_iterator(SymbolRef(SymbolData, this));
2164}
2165
2166template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2167symbol_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2168                             ::end_symbols() const {
2169  DataRefImpl SymbolData;
2170  SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2171  SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2172  return symbol_iterator(SymbolRef(SymbolData, this));
2173}
2174
2175template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2176symbol_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2177                             ::begin_dynamic_symbols() const {
2178  DataRefImpl SymbolData;
2179  if (SymbolTableSections[0] == NULL) {
2180    SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2181    SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2182  } else {
2183    SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2184    SymbolData.d.b = 0; // The 0th table is .dynsym
2185  }
2186  return symbol_iterator(SymbolRef(SymbolData, this));
2187}
2188
2189template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2190symbol_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2191                             ::end_dynamic_symbols() const {
2192  DataRefImpl SymbolData;
2193  SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2194  SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2195  return symbol_iterator(SymbolRef(SymbolData, this));
2196}
2197
2198template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2199section_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2200                              ::begin_sections() const {
2201  DataRefImpl ret;
2202  ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
2203  return section_iterator(SectionRef(ret, this));
2204}
2205
2206template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2207section_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2208                              ::end_sections() const {
2209  DataRefImpl ret;
2210  ret.p = reinterpret_cast<intptr_t>(base()
2211                                     + Header->e_shoff
2212                                     + (Header->e_shentsize*getNumSections()));
2213  return section_iterator(SectionRef(ret, this));
2214}
2215
2216template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2217typename ELFObjectFile<target_endianness, max_alignment, is64Bits>::dyn_iterator
2218ELFObjectFile<target_endianness, max_alignment, is64Bits>
2219             ::begin_dynamic_table() const {
2220  DataRefImpl DynData;
2221  if (dot_dynamic_sec == NULL || dot_dynamic_sec->sh_size == 0) {
2222    DynData.d.a = std::numeric_limits<uint32_t>::max();
2223  } else {
2224    DynData.d.a = 0;
2225  }
2226  return dyn_iterator(DynRef(DynData, this));
2227}
2228
2229template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2230typename ELFObjectFile<target_endianness, max_alignment, is64Bits>::dyn_iterator
2231ELFObjectFile<target_endianness, max_alignment, is64Bits>
2232                          ::end_dynamic_table() const {
2233  DataRefImpl DynData;
2234  DynData.d.a = std::numeric_limits<uint32_t>::max();
2235  return dyn_iterator(DynRef(DynData, this));
2236}
2237
2238template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2239error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2240                        ::getDynNext(DataRefImpl DynData,
2241                                     DynRef &Result) const {
2242  ++DynData.d.a;
2243
2244  // Check to see if we are at the end of .dynamic
2245  if (DynData.d.a >= dot_dynamic_sec->getEntityCount()) {
2246    // We are at the end. Return the terminator.
2247    DynData.d.a = std::numeric_limits<uint32_t>::max();
2248  }
2249
2250  Result = DynRef(DynData, this);
2251  return object_error::success;
2252}
2253
2254template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2255StringRef
2256ELFObjectFile<target_endianness, max_alignment, is64Bits>::getLoadName() const {
2257  if (!dt_soname) {
2258    // Find the DT_SONAME entry
2259    dyn_iterator it = begin_dynamic_table();
2260    dyn_iterator ie = end_dynamic_table();
2261    error_code ec;
2262    while (it != ie) {
2263      if (it->getTag() == ELF::DT_SONAME)
2264        break;
2265      it.increment(ec);
2266      if (ec)
2267        report_fatal_error("dynamic table iteration failed");
2268    }
2269    if (it != ie) {
2270      if (dot_dynstr_sec == NULL)
2271        report_fatal_error("Dynamic string table is missing");
2272      dt_soname = getString(dot_dynstr_sec, it->getVal());
2273    } else {
2274      dt_soname = "";
2275    }
2276  }
2277  return dt_soname;
2278}
2279
2280template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2281library_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2282                             ::begin_libraries_needed() const {
2283  // Find the first DT_NEEDED entry
2284  dyn_iterator i = begin_dynamic_table();
2285  dyn_iterator e = end_dynamic_table();
2286  error_code ec;
2287  while (i != e) {
2288    if (i->getTag() == ELF::DT_NEEDED)
2289      break;
2290    i.increment(ec);
2291    if (ec)
2292      report_fatal_error("dynamic table iteration failed");
2293  }
2294  // Use the same DataRefImpl format as DynRef.
2295  return library_iterator(LibraryRef(i->getRawDataRefImpl(), this));
2296}
2297
2298template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2299error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2300                        ::getLibraryNext(DataRefImpl Data,
2301                                         LibraryRef &Result) const {
2302  // Use the same DataRefImpl format as DynRef.
2303  dyn_iterator i = dyn_iterator(DynRef(Data, this));
2304  dyn_iterator e = end_dynamic_table();
2305
2306  // Skip the current dynamic table entry.
2307  error_code ec;
2308  if (i != e) {
2309    i.increment(ec);
2310    // TODO: proper error handling
2311    if (ec)
2312      report_fatal_error("dynamic table iteration failed");
2313  }
2314
2315  // Find the next DT_NEEDED entry.
2316  while (i != e) {
2317    if (i->getTag() == ELF::DT_NEEDED)
2318      break;
2319    i.increment(ec);
2320    if (ec)
2321      report_fatal_error("dynamic table iteration failed");
2322  }
2323  Result = LibraryRef(i->getRawDataRefImpl(), this);
2324  return object_error::success;
2325}
2326
2327template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2328error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2329         ::getLibraryPath(DataRefImpl Data, StringRef &Res) const {
2330  dyn_iterator i = dyn_iterator(DynRef(Data, this));
2331  if (i == end_dynamic_table())
2332    report_fatal_error("getLibraryPath() called on iterator end");
2333
2334  if (i->getTag() != ELF::DT_NEEDED)
2335    report_fatal_error("Invalid library_iterator");
2336
2337  // This uses .dynstr to lookup the name of the DT_NEEDED entry.
2338  // THis works as long as DT_STRTAB == .dynstr. This is true most of
2339  // the time, but the specification allows exceptions.
2340  // TODO: This should really use DT_STRTAB instead. Doing this requires
2341  // reading the program headers.
2342  if (dot_dynstr_sec == NULL)
2343    report_fatal_error("Dynamic string table is missing");
2344  Res = getString(dot_dynstr_sec, i->getVal());
2345  return object_error::success;
2346}
2347
2348template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2349library_iterator ELFObjectFile<target_endianness, max_alignment, is64Bits>
2350                             ::end_libraries_needed() const {
2351  dyn_iterator e = end_dynamic_table();
2352  // Use the same DataRefImpl format as DynRef.
2353  return library_iterator(LibraryRef(e->getRawDataRefImpl(), this));
2354}
2355
2356template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2357uint8_t ELFObjectFile<target_endianness, max_alignment, is64Bits>
2358                     ::getBytesInAddress() const {
2359  return is64Bits ? 8 : 4;
2360}
2361
2362template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2363StringRef ELFObjectFile<target_endianness, max_alignment, is64Bits>
2364                       ::getFileFormatName() const {
2365  switch(Header->e_ident[ELF::EI_CLASS]) {
2366  case ELF::ELFCLASS32:
2367    switch(Header->e_machine) {
2368    case ELF::EM_386:
2369      return "ELF32-i386";
2370    case ELF::EM_X86_64:
2371      return "ELF32-x86-64";
2372    case ELF::EM_ARM:
2373      return "ELF32-arm";
2374    case ELF::EM_HEXAGON:
2375      return "ELF32-hexagon";
2376    default:
2377      return "ELF32-unknown";
2378    }
2379  case ELF::ELFCLASS64:
2380    switch(Header->e_machine) {
2381    case ELF::EM_386:
2382      return "ELF64-i386";
2383    case ELF::EM_X86_64:
2384      return "ELF64-x86-64";
2385    case ELF::EM_PPC64:
2386      return "ELF64-ppc64";
2387    default:
2388      return "ELF64-unknown";
2389    }
2390  default:
2391    // FIXME: Proper error handling.
2392    report_fatal_error("Invalid ELFCLASS!");
2393  }
2394}
2395
2396template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2397unsigned ELFObjectFile<target_endianness, max_alignment, is64Bits>
2398                      ::getArch() const {
2399  switch(Header->e_machine) {
2400  case ELF::EM_386:
2401    return Triple::x86;
2402  case ELF::EM_X86_64:
2403    return Triple::x86_64;
2404  case ELF::EM_ARM:
2405    return Triple::arm;
2406  case ELF::EM_HEXAGON:
2407    return Triple::hexagon;
2408  case ELF::EM_MIPS:
2409    return (target_endianness == support::little) ?
2410           Triple::mipsel : Triple::mips;
2411  case ELF::EM_PPC64:
2412    return Triple::ppc64;
2413  default:
2414    return Triple::UnknownArch;
2415  }
2416}
2417
2418template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2419uint64_t ELFObjectFile<target_endianness, max_alignment, is64Bits>
2420                      ::getNumSections() const {
2421  assert(Header && "Header not initialized!");
2422  if (Header->e_shnum == ELF::SHN_UNDEF) {
2423    assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
2424    return SectionHeaderTable->sh_size;
2425  }
2426  return Header->e_shnum;
2427}
2428
2429template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2430uint64_t
2431ELFObjectFile<target_endianness, max_alignment, is64Bits>
2432             ::getStringTableIndex() const {
2433  if (Header->e_shnum == ELF::SHN_UNDEF) {
2434    if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
2435      return SectionHeaderTable->sh_link;
2436    if (Header->e_shstrndx >= getNumSections())
2437      return 0;
2438  }
2439  return Header->e_shstrndx;
2440}
2441
2442template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2443template<typename T>
2444inline const T *
2445ELFObjectFile<target_endianness, max_alignment, is64Bits>
2446             ::getEntry(uint16_t Section, uint32_t Entry) const {
2447  return getEntry<T>(getSection(Section), Entry);
2448}
2449
2450template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2451template<typename T>
2452inline const T *
2453ELFObjectFile<target_endianness, max_alignment, is64Bits>
2454             ::getEntry(const Elf_Shdr * Section, uint32_t Entry) const {
2455  return reinterpret_cast<const T *>(
2456           base()
2457           + Section->sh_offset
2458           + (Entry * Section->sh_entsize));
2459}
2460
2461template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2462const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2463                            ::Elf_Sym *
2464ELFObjectFile<target_endianness, max_alignment, is64Bits>
2465             ::getSymbol(DataRefImpl Symb) const {
2466  return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
2467}
2468
2469template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2470const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2471                            ::Elf_Dyn *
2472ELFObjectFile<target_endianness, max_alignment, is64Bits>
2473             ::getDyn(DataRefImpl DynData) const {
2474  return getEntry<Elf_Dyn>(dot_dynamic_sec, DynData.d.a);
2475}
2476
2477template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2478const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2479                            ::Elf_Rel *
2480ELFObjectFile<target_endianness, max_alignment, is64Bits>
2481             ::getRel(DataRefImpl Rel) const {
2482  return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
2483}
2484
2485template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2486const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2487                            ::Elf_Rela *
2488ELFObjectFile<target_endianness, max_alignment, is64Bits>
2489             ::getRela(DataRefImpl Rela) const {
2490  return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
2491}
2492
2493template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2494const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2495                            ::Elf_Shdr *
2496ELFObjectFile<target_endianness, max_alignment, is64Bits>
2497             ::getSection(DataRefImpl Symb) const {
2498  const Elf_Shdr *sec = getSection(Symb.d.b);
2499  if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
2500    // FIXME: Proper error handling.
2501    report_fatal_error("Invalid symbol table section!");
2502  return sec;
2503}
2504
2505template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2506const typename ELFObjectFile<target_endianness, max_alignment, is64Bits>
2507                            ::Elf_Shdr *
2508ELFObjectFile<target_endianness, max_alignment, is64Bits>
2509             ::getSection(uint32_t index) const {
2510  if (index == 0)
2511    return 0;
2512  if (!SectionHeaderTable || index >= getNumSections())
2513    // FIXME: Proper error handling.
2514    report_fatal_error("Invalid section index!");
2515
2516  return reinterpret_cast<const Elf_Shdr *>(
2517         reinterpret_cast<const char *>(SectionHeaderTable)
2518         + (index * Header->e_shentsize));
2519}
2520
2521template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2522const char *ELFObjectFile<target_endianness, max_alignment, is64Bits>
2523                         ::getString(uint32_t section,
2524                                     ELF::Elf32_Word offset) const {
2525  return getString(getSection(section), offset);
2526}
2527
2528template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2529const char *ELFObjectFile<target_endianness, max_alignment, is64Bits>
2530                         ::getString(const Elf_Shdr *section,
2531                                     ELF::Elf32_Word offset) const {
2532  assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
2533  if (offset >= section->sh_size)
2534    // FIXME: Proper error handling.
2535    report_fatal_error("Symbol name offset outside of string table!");
2536  return (const char *)base() + section->sh_offset + offset;
2537}
2538
2539template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2540error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2541                        ::getSymbolName(const Elf_Shdr *section,
2542                                        const Elf_Sym *symb,
2543                                        StringRef &Result) const {
2544  if (symb->st_name == 0) {
2545    const Elf_Shdr *section = getSection(symb);
2546    if (!section)
2547      Result = "";
2548    else
2549      Result = getString(dot_shstrtab_sec, section->sh_name);
2550    return object_error::success;
2551  }
2552
2553  if (section == SymbolTableSections[0]) {
2554    // Symbol is in .dynsym, use .dynstr string table
2555    Result = getString(dot_dynstr_sec, symb->st_name);
2556  } else {
2557    // Use the default symbol table name section.
2558    Result = getString(dot_strtab_sec, symb->st_name);
2559  }
2560  return object_error::success;
2561}
2562
2563template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2564error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2565                        ::getSectionName(const Elf_Shdr *section,
2566                                        StringRef &Result) const {
2567  Result = StringRef(getString(dot_shstrtab_sec, section->sh_name));
2568  return object_error::success;
2569}
2570
2571template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2572error_code ELFObjectFile<target_endianness, max_alignment, is64Bits>
2573                        ::getSymbolVersion(const Elf_Shdr *section,
2574                                           const Elf_Sym *symb,
2575                                           StringRef &Version,
2576                                           bool &IsDefault) const {
2577  // Handle non-dynamic symbols.
2578  if (section != SymbolTableSections[0]) {
2579    // Non-dynamic symbols can have versions in their names
2580    // A name of the form 'foo@V1' indicates version 'V1', non-default.
2581    // A name of the form 'foo@@V2' indicates version 'V2', default version.
2582    StringRef Name;
2583    error_code ec = getSymbolName(section, symb, Name);
2584    if (ec != object_error::success)
2585      return ec;
2586    size_t atpos = Name.find('@');
2587    if (atpos == StringRef::npos) {
2588      Version = "";
2589      IsDefault = false;
2590      return object_error::success;
2591    }
2592    ++atpos;
2593    if (atpos < Name.size() && Name[atpos] == '@') {
2594      IsDefault = true;
2595      ++atpos;
2596    } else {
2597      IsDefault = false;
2598    }
2599    Version = Name.substr(atpos);
2600    return object_error::success;
2601  }
2602
2603  // This is a dynamic symbol. Look in the GNU symbol version table.
2604  if (dot_gnu_version_sec == NULL) {
2605    // No version table.
2606    Version = "";
2607    IsDefault = false;
2608    return object_error::success;
2609  }
2610
2611  // Determine the position in the symbol table of this entry.
2612  const char *sec_start = (const char*)base() + section->sh_offset;
2613  size_t entry_index = ((const char*)symb - sec_start)/section->sh_entsize;
2614
2615  // Get the corresponding version index entry
2616  const Elf_Versym *vs = getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index);
2617  size_t version_index = vs->vs_index & ELF::VERSYM_VERSION;
2618
2619  // Special markers for unversioned symbols.
2620  if (version_index == ELF::VER_NDX_LOCAL ||
2621      version_index == ELF::VER_NDX_GLOBAL) {
2622    Version = "";
2623    IsDefault = false;
2624    return object_error::success;
2625  }
2626
2627  // Lookup this symbol in the version table
2628  LoadVersionMap();
2629  if (version_index >= VersionMap.size() || VersionMap[version_index].isNull())
2630    report_fatal_error("Symbol has version index without corresponding "
2631                       "define or reference entry");
2632  const VersionMapEntry &entry = VersionMap[version_index];
2633
2634  // Get the version name string
2635  size_t name_offset;
2636  if (entry.isVerdef()) {
2637    // The first Verdaux entry holds the name.
2638    name_offset = entry.getVerdef()->getAux()->vda_name;
2639  } else {
2640    name_offset = entry.getVernaux()->vna_name;
2641  }
2642  Version = getString(dot_dynstr_sec, name_offset);
2643
2644  // Set IsDefault
2645  if (entry.isVerdef()) {
2646    IsDefault = !(vs->vs_index & ELF::VERSYM_HIDDEN);
2647  } else {
2648    IsDefault = false;
2649  }
2650
2651  return object_error::success;
2652}
2653
2654template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2655inline DynRefImpl<target_endianness, max_alignment, is64Bits>
2656                 ::DynRefImpl(DataRefImpl DynP, const OwningType *Owner)
2657  : DynPimpl(DynP)
2658  , OwningObject(Owner) {}
2659
2660template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2661inline bool DynRefImpl<target_endianness, max_alignment, is64Bits>
2662                      ::operator==(const DynRefImpl &Other) const {
2663  return DynPimpl == Other.DynPimpl;
2664}
2665
2666template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2667inline bool DynRefImpl<target_endianness, max_alignment, is64Bits>
2668                      ::operator <(const DynRefImpl &Other) const {
2669  return DynPimpl < Other.DynPimpl;
2670}
2671
2672template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2673inline error_code DynRefImpl<target_endianness, max_alignment, is64Bits>
2674                            ::getNext(DynRefImpl &Result) const {
2675  return OwningObject->getDynNext(DynPimpl, Result);
2676}
2677
2678template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2679inline int64_t DynRefImpl<target_endianness, max_alignment, is64Bits>
2680                            ::getTag() const {
2681  return OwningObject->getDyn(DynPimpl)->d_tag;
2682}
2683
2684template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2685inline uint64_t DynRefImpl<target_endianness, max_alignment, is64Bits>
2686                            ::getVal() const {
2687  return OwningObject->getDyn(DynPimpl)->d_un.d_val;
2688}
2689
2690template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2691inline uint64_t DynRefImpl<target_endianness, max_alignment, is64Bits>
2692                            ::getPtr() const {
2693  return OwningObject->getDyn(DynPimpl)->d_un.d_ptr;
2694}
2695
2696template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
2697inline DataRefImpl DynRefImpl<target_endianness, max_alignment, is64Bits>
2698                             ::getRawDataRefImpl() const {
2699  return DynPimpl;
2700}
2701
2702/// This is a generic interface for retrieving GNU symbol version
2703/// information from an ELFObjectFile.
2704static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
2705                                             const SymbolRef &Sym,
2706                                             StringRef &Version,
2707                                             bool &IsDefault) {
2708  // Little-endian 32-bit
2709  if (const ELFObjectFile<support::little, 4, false> *ELFObj =
2710          dyn_cast<ELFObjectFile<support::little, 4, false> >(Obj))
2711    return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2712
2713  // Big-endian 32-bit
2714  if (const ELFObjectFile<support::big, 4, false> *ELFObj =
2715          dyn_cast<ELFObjectFile<support::big, 4, false> >(Obj))
2716    return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2717
2718  // Little-endian 64-bit
2719  if (const ELFObjectFile<support::little, 8, true> *ELFObj =
2720          dyn_cast<ELFObjectFile<support::little, 8, true> >(Obj))
2721    return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2722
2723  // Big-endian 64-bit
2724  if (const ELFObjectFile<support::big, 8, true> *ELFObj =
2725          dyn_cast<ELFObjectFile<support::big, 8, true> >(Obj))
2726    return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2727
2728  llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");
2729}
2730
2731}
2732}
2733
2734#endif
2735