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