Avoid Enum Compare Warnings
[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(static_cast<int>(ELFT::TargetEndianness)
796                                       == static_cast<int>(support::little),
797                                       ELFT::Is64Bits);
798   }
799 };
800
801 // Iterate through the version definitions, and place each Elf_Verdef
802 // in the VersionMap according to its index.
803 template<class ELFT>
804 void ELFObjectFile<ELFT>::LoadVersionDefs(const Elf_Shdr *sec) const {
805   unsigned vd_size = sec->sh_size; // Size of section in bytes
806   unsigned vd_count = sec->sh_info; // Number of Verdef entries
807   const char *sec_start = (const char*)base() + sec->sh_offset;
808   const char *sec_end = sec_start + vd_size;
809   // The first Verdef entry is at the start of the section.
810   const char *p = sec_start;
811   for (unsigned i = 0; i < vd_count; i++) {
812     if (p + sizeof(Elf_Verdef) > sec_end)
813       report_fatal_error("Section ended unexpectedly while scanning "
814                          "version definitions.");
815     const Elf_Verdef *vd = reinterpret_cast<const Elf_Verdef *>(p);
816     if (vd->vd_version != ELF::VER_DEF_CURRENT)
817       report_fatal_error("Unexpected verdef version");
818     size_t index = vd->vd_ndx & ELF::VERSYM_VERSION;
819     if (index >= VersionMap.size())
820       VersionMap.resize(index+1);
821     VersionMap[index] = VersionMapEntry(vd);
822     p += vd->vd_next;
823   }
824 }
825
826 // Iterate through the versions needed section, and place each Elf_Vernaux
827 // in the VersionMap according to its index.
828 template<class ELFT>
829 void ELFObjectFile<ELFT>::LoadVersionNeeds(const Elf_Shdr *sec) const {
830   unsigned vn_size = sec->sh_size; // Size of section in bytes
831   unsigned vn_count = sec->sh_info; // Number of Verneed entries
832   const char *sec_start = (const char*)base() + sec->sh_offset;
833   const char *sec_end = sec_start + vn_size;
834   // The first Verneed entry is at the start of the section.
835   const char *p = sec_start;
836   for (unsigned i = 0; i < vn_count; i++) {
837     if (p + sizeof(Elf_Verneed) > sec_end)
838       report_fatal_error("Section ended unexpectedly while scanning "
839                          "version needed records.");
840     const Elf_Verneed *vn = reinterpret_cast<const Elf_Verneed *>(p);
841     if (vn->vn_version != ELF::VER_NEED_CURRENT)
842       report_fatal_error("Unexpected verneed version");
843     // Iterate through the Vernaux entries
844     const char *paux = p + vn->vn_aux;
845     for (unsigned j = 0; j < vn->vn_cnt; j++) {
846       if (paux + sizeof(Elf_Vernaux) > sec_end)
847         report_fatal_error("Section ended unexpected while scanning auxiliary "
848                            "version needed records.");
849       const Elf_Vernaux *vna = reinterpret_cast<const Elf_Vernaux *>(paux);
850       size_t index = vna->vna_other & ELF::VERSYM_VERSION;
851       if (index >= VersionMap.size())
852         VersionMap.resize(index+1);
853       VersionMap[index] = VersionMapEntry(vna);
854       paux += vna->vna_next;
855     }
856     p += vn->vn_next;
857   }
858 }
859
860 template<class ELFT>
861 void ELFObjectFile<ELFT>::LoadVersionMap() const {
862   // If there is no dynamic symtab or version table, there is nothing to do.
863   if (SymbolTableSections[0] == NULL || dot_gnu_version_sec == NULL)
864     return;
865
866   // Has the VersionMap already been loaded?
867   if (VersionMap.size() > 0)
868     return;
869
870   // The first two version indexes are reserved.
871   // Index 0 is LOCAL, index 1 is GLOBAL.
872   VersionMap.push_back(VersionMapEntry());
873   VersionMap.push_back(VersionMapEntry());
874
875   if (dot_gnu_version_d_sec)
876     LoadVersionDefs(dot_gnu_version_d_sec);
877
878   if (dot_gnu_version_r_sec)
879     LoadVersionNeeds(dot_gnu_version_r_sec);
880 }
881
882 template<class ELFT>
883 void ELFObjectFile<ELFT>::validateSymbol(DataRefImpl Symb) const {
884   const Elf_Sym  *symb = getSymbol(Symb);
885   const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
886   // FIXME: We really need to do proper error handling in the case of an invalid
887   //        input file. Because we don't use exceptions, I think we'll just pass
888   //        an error object around.
889   if (!(  symb
890         && SymbolTableSection
891         && symb >= (const Elf_Sym*)(base()
892                    + SymbolTableSection->sh_offset)
893         && symb <  (const Elf_Sym*)(base()
894                    + SymbolTableSection->sh_offset
895                    + SymbolTableSection->sh_size)))
896     // FIXME: Proper error handling.
897     report_fatal_error("Symb must point to a valid symbol!");
898 }
899
900 template<class ELFT>
901 error_code ELFObjectFile<ELFT>::getSymbolNext(DataRefImpl Symb,
902                                               SymbolRef &Result) const {
903   validateSymbol(Symb);
904   const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
905
906   ++Symb.d.a;
907   // Check to see if we are at the end of this symbol table.
908   if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
909     // We are at the end. If there are other symbol tables, jump to them.
910     // If the symbol table is .dynsym, we are iterating dynamic symbols,
911     // and there is only one table of these.
912     if (Symb.d.b != 0) {
913       ++Symb.d.b;
914       Symb.d.a = 1; // The 0th symbol in ELF is fake.
915     }
916     // Otherwise return the terminator.
917     if (Symb.d.b == 0 || Symb.d.b >= SymbolTableSections.size()) {
918       Symb.d.a = std::numeric_limits<uint32_t>::max();
919       Symb.d.b = std::numeric_limits<uint32_t>::max();
920     }
921   }
922
923   Result = SymbolRef(Symb, this);
924   return object_error::success;
925 }
926
927 template<class ELFT>
928 error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
929                                               StringRef &Result) const {
930   validateSymbol(Symb);
931   const Elf_Sym *symb = getSymbol(Symb);
932   return getSymbolName(SymbolTableSections[Symb.d.b], symb, Result);
933 }
934
935 template<class ELFT>
936 error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
937                                                  StringRef &Version,
938                                                  bool &IsDefault) const {
939   DataRefImpl Symb = SymRef.getRawDataRefImpl();
940   validateSymbol(Symb);
941   const Elf_Sym *symb = getSymbol(Symb);
942   return getSymbolVersion(SymbolTableSections[Symb.d.b], symb,
943                           Version, IsDefault);
944 }
945
946 template<class ELFT>
947 ELF::Elf64_Word ELFObjectFile<ELFT>
948                              ::getSymbolTableIndex(const Elf_Sym *symb) const {
949   if (symb->st_shndx == ELF::SHN_XINDEX)
950     return ExtendedSymbolTable.lookup(symb);
951   return symb->st_shndx;
952 }
953
954 template<class ELFT>
955 const typename ELFObjectFile<ELFT>::Elf_Shdr *
956 ELFObjectFile<ELFT>::getSection(const Elf_Sym *symb) const {
957   if (symb->st_shndx == ELF::SHN_XINDEX)
958     return getSection(ExtendedSymbolTable.lookup(symb));
959   if (symb->st_shndx >= ELF::SHN_LORESERVE)
960     return 0;
961   return getSection(symb->st_shndx);
962 }
963
964 template<class ELFT>
965 const typename ELFObjectFile<ELFT>::Elf_Shdr *
966 ELFObjectFile<ELFT>::getElfSection(section_iterator &It) const {
967   llvm::object::DataRefImpl ShdrRef = It->getRawDataRefImpl();
968   return reinterpret_cast<const Elf_Shdr *>(ShdrRef.p);
969 }
970
971 template<class ELFT>
972 const typename ELFObjectFile<ELFT>::Elf_Sym *
973 ELFObjectFile<ELFT>::getElfSymbol(symbol_iterator &It) const {
974   return getSymbol(It->getRawDataRefImpl());
975 }
976
977 template<class ELFT>
978 const typename ELFObjectFile<ELFT>::Elf_Sym *
979 ELFObjectFile<ELFT>::getElfSymbol(uint32_t index) const {
980   DataRefImpl SymbolData;
981   SymbolData.d.a = index;
982   SymbolData.d.b = 1;
983   return getSymbol(SymbolData);
984 }
985
986 template<class ELFT>
987 error_code ELFObjectFile<ELFT>::getSymbolFileOffset(DataRefImpl Symb,
988                                                     uint64_t &Result) const {
989   validateSymbol(Symb);
990   const Elf_Sym  *symb = getSymbol(Symb);
991   const Elf_Shdr *Section;
992   switch (getSymbolTableIndex(symb)) {
993   case ELF::SHN_COMMON:
994    // Unintialized symbols have no offset in the object file
995   case ELF::SHN_UNDEF:
996     Result = UnknownAddressOrSize;
997     return object_error::success;
998   case ELF::SHN_ABS:
999     Result = symb->st_value;
1000     return object_error::success;
1001   default: Section = getSection(symb);
1002   }
1003
1004   switch (symb->getType()) {
1005   case ELF::STT_SECTION:
1006     Result = Section ? Section->sh_addr : UnknownAddressOrSize;
1007     return object_error::success;
1008   case ELF::STT_FUNC:
1009   case ELF::STT_OBJECT:
1010   case ELF::STT_NOTYPE:
1011     Result = symb->st_value +
1012              (Section ? Section->sh_offset : 0);
1013     return object_error::success;
1014   default:
1015     Result = UnknownAddressOrSize;
1016     return object_error::success;
1017   }
1018 }
1019
1020 template<class ELFT>
1021 error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
1022                                                  uint64_t &Result) const {
1023   validateSymbol(Symb);
1024   const Elf_Sym  *symb = getSymbol(Symb);
1025   const Elf_Shdr *Section;
1026   switch (getSymbolTableIndex(symb)) {
1027   case ELF::SHN_COMMON:
1028   case ELF::SHN_UNDEF:
1029     Result = UnknownAddressOrSize;
1030     return object_error::success;
1031   case ELF::SHN_ABS:
1032     Result = symb->st_value;
1033     return object_error::success;
1034   default: Section = getSection(symb);
1035   }
1036
1037   switch (symb->getType()) {
1038   case ELF::STT_SECTION:
1039     Result = Section ? Section->sh_addr : UnknownAddressOrSize;
1040     return object_error::success;
1041   case ELF::STT_FUNC:
1042   case ELF::STT_OBJECT:
1043   case ELF::STT_NOTYPE:
1044     bool IsRelocatable;
1045     switch(Header->e_type) {
1046     case ELF::ET_EXEC:
1047     case ELF::ET_DYN:
1048       IsRelocatable = false;
1049       break;
1050     default:
1051       IsRelocatable = true;
1052     }
1053     Result = symb->st_value;
1054     if (IsRelocatable && Section != 0)
1055       Result += Section->sh_addr;
1056     return object_error::success;
1057   default:
1058     Result = UnknownAddressOrSize;
1059     return object_error::success;
1060   }
1061 }
1062
1063 template<class ELFT>
1064 error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
1065                                               uint64_t &Result) const {
1066   validateSymbol(Symb);
1067   const Elf_Sym  *symb = getSymbol(Symb);
1068   if (symb->st_size == 0)
1069     Result = UnknownAddressOrSize;
1070   Result = symb->st_size;
1071   return object_error::success;
1072 }
1073
1074 template<class ELFT>
1075 error_code ELFObjectFile<ELFT>::getSymbolNMTypeChar(DataRefImpl Symb,
1076                                                     char &Result) const {
1077   validateSymbol(Symb);
1078   const Elf_Sym  *symb = getSymbol(Symb);
1079   const Elf_Shdr *Section = getSection(symb);
1080
1081   char ret = '?';
1082
1083   if (Section) {
1084     switch (Section->sh_type) {
1085     case ELF::SHT_PROGBITS:
1086     case ELF::SHT_DYNAMIC:
1087       switch (Section->sh_flags) {
1088       case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
1089         ret = 't'; break;
1090       case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
1091         ret = 'd'; break;
1092       case ELF::SHF_ALLOC:
1093       case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
1094       case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
1095         ret = 'r'; break;
1096       }
1097       break;
1098     case ELF::SHT_NOBITS: ret = 'b';
1099     }
1100   }
1101
1102   switch (getSymbolTableIndex(symb)) {
1103   case ELF::SHN_UNDEF:
1104     if (ret == '?')
1105       ret = 'U';
1106     break;
1107   case ELF::SHN_ABS: ret = 'a'; break;
1108   case ELF::SHN_COMMON: ret = 'c'; break;
1109   }
1110
1111   switch (symb->getBinding()) {
1112   case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
1113   case ELF::STB_WEAK:
1114     if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1115       ret = 'w';
1116     else
1117       if (symb->getType() == ELF::STT_OBJECT)
1118         ret = 'V';
1119       else
1120         ret = 'W';
1121   }
1122
1123   if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
1124     StringRef name;
1125     if (error_code ec = getSymbolName(Symb, name))
1126       return ec;
1127     Result = StringSwitch<char>(name)
1128       .StartsWith(".debug", 'N')
1129       .StartsWith(".note", 'n')
1130       .Default('?');
1131     return object_error::success;
1132   }
1133
1134   Result = ret;
1135   return object_error::success;
1136 }
1137
1138 template<class ELFT>
1139 error_code ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
1140                                               SymbolRef::Type &Result) const {
1141   validateSymbol(Symb);
1142   const Elf_Sym  *symb = getSymbol(Symb);
1143
1144   switch (symb->getType()) {
1145   case ELF::STT_NOTYPE:
1146     Result = SymbolRef::ST_Unknown;
1147     break;
1148   case ELF::STT_SECTION:
1149     Result = SymbolRef::ST_Debug;
1150     break;
1151   case ELF::STT_FILE:
1152     Result = SymbolRef::ST_File;
1153     break;
1154   case ELF::STT_FUNC:
1155     Result = SymbolRef::ST_Function;
1156     break;
1157   case ELF::STT_OBJECT:
1158   case ELF::STT_COMMON:
1159   case ELF::STT_TLS:
1160     Result = SymbolRef::ST_Data;
1161     break;
1162   default:
1163     Result = SymbolRef::ST_Other;
1164     break;
1165   }
1166   return object_error::success;
1167 }
1168
1169 template<class ELFT>
1170 error_code ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb,
1171                                                uint32_t &Result) const {
1172   validateSymbol(Symb);
1173   const Elf_Sym  *symb = getSymbol(Symb);
1174
1175   Result = SymbolRef::SF_None;
1176
1177   if (symb->getBinding() != ELF::STB_LOCAL)
1178     Result |= SymbolRef::SF_Global;
1179
1180   if (symb->getBinding() == ELF::STB_WEAK)
1181     Result |= SymbolRef::SF_Weak;
1182
1183   if (symb->st_shndx == ELF::SHN_ABS)
1184     Result |= SymbolRef::SF_Absolute;
1185
1186   if (symb->getType() == ELF::STT_FILE ||
1187       symb->getType() == ELF::STT_SECTION)
1188     Result |= SymbolRef::SF_FormatSpecific;
1189
1190   if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
1191     Result |= SymbolRef::SF_Undefined;
1192
1193   if (symb->getType() == ELF::STT_COMMON ||
1194       getSymbolTableIndex(symb) == ELF::SHN_COMMON)
1195     Result |= SymbolRef::SF_Common;
1196
1197   if (symb->getType() == ELF::STT_TLS)
1198     Result |= SymbolRef::SF_ThreadLocal;
1199
1200   return object_error::success;
1201 }
1202
1203 template<class ELFT>
1204 error_code ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
1205                                                  section_iterator &Res) const {
1206   validateSymbol(Symb);
1207   const Elf_Sym  *symb = getSymbol(Symb);
1208   const Elf_Shdr *sec = getSection(symb);
1209   if (!sec)
1210     Res = end_sections();
1211   else {
1212     DataRefImpl Sec;
1213     Sec.p = reinterpret_cast<intptr_t>(sec);
1214     Res = section_iterator(SectionRef(Sec, this));
1215   }
1216   return object_error::success;
1217 }
1218
1219 template<class ELFT>
1220 error_code ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb,
1221                                                uint64_t &Val) const {
1222   validateSymbol(Symb);
1223   const Elf_Sym *symb = getSymbol(Symb);
1224   Val = symb->st_value;
1225   return object_error::success;
1226 }
1227
1228 template<class ELFT>
1229 error_code ELFObjectFile<ELFT>::getSectionNext(DataRefImpl Sec,
1230                                                SectionRef &Result) const {
1231   const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
1232   sec += Header->e_shentsize;
1233   Sec.p = reinterpret_cast<intptr_t>(sec);
1234   Result = SectionRef(Sec, this);
1235   return object_error::success;
1236 }
1237
1238 template<class ELFT>
1239 error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
1240                                                StringRef &Result) const {
1241   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1242   Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
1243   return object_error::success;
1244 }
1245
1246 template<class ELFT>
1247 error_code ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec,
1248                                                   uint64_t &Result) const {
1249   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1250   Result = sec->sh_addr;
1251   return object_error::success;
1252 }
1253
1254 template<class ELFT>
1255 error_code ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec,
1256                                                uint64_t &Result) const {
1257   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1258   Result = sec->sh_size;
1259   return object_error::success;
1260 }
1261
1262 template<class ELFT>
1263 error_code ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
1264                                                    StringRef &Result) const {
1265   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1266   const char *start = (const char*)base() + sec->sh_offset;
1267   Result = StringRef(start, sec->sh_size);
1268   return object_error::success;
1269 }
1270
1271 template<class ELFT>
1272 error_code ELFObjectFile<ELFT>::getSectionContents(const Elf_Shdr *Sec,
1273                                                    StringRef &Result) const {
1274   const char *start = (const char*)base() + Sec->sh_offset;
1275   Result = StringRef(start, Sec->sh_size);
1276   return object_error::success;
1277 }
1278
1279 template<class ELFT>
1280 error_code ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec,
1281                                                     uint64_t &Result) const {
1282   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1283   Result = sec->sh_addralign;
1284   return object_error::success;
1285 }
1286
1287 template<class ELFT>
1288 error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec,
1289                                               bool &Result) const {
1290   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1291   if (sec->sh_flags & ELF::SHF_EXECINSTR)
1292     Result = true;
1293   else
1294     Result = false;
1295   return object_error::success;
1296 }
1297
1298 template<class ELFT>
1299 error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec,
1300                                               bool &Result) const {
1301   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1302   if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1303       && sec->sh_type == ELF::SHT_PROGBITS)
1304     Result = true;
1305   else
1306     Result = false;
1307   return object_error::success;
1308 }
1309
1310 template<class ELFT>
1311 error_code ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec,
1312                                              bool &Result) const {
1313   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1314   if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
1315       && sec->sh_type == ELF::SHT_NOBITS)
1316     Result = true;
1317   else
1318     Result = false;
1319   return object_error::success;
1320 }
1321
1322 template<class ELFT>
1323 error_code ELFObjectFile<ELFT>::isSectionRequiredForExecution(
1324     DataRefImpl Sec, bool &Result) const {
1325   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1326   if (sec->sh_flags & ELF::SHF_ALLOC)
1327     Result = true;
1328   else
1329     Result = false;
1330   return object_error::success;
1331 }
1332
1333 template<class ELFT>
1334 error_code ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec,
1335                                                  bool &Result) const {
1336   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1337   if (sec->sh_type == ELF::SHT_NOBITS)
1338     Result = true;
1339   else
1340     Result = false;
1341   return object_error::success;
1342 }
1343
1344 template<class ELFT>
1345 error_code ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec,
1346                                                   bool &Result) const {
1347   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1348   // For ELF, all zero-init sections are virtual (that is, they occupy no space
1349   //   in the object image) and vice versa.
1350   Result = sec->sh_type == ELF::SHT_NOBITS;
1351   return object_error::success;
1352 }
1353
1354 template<class ELFT>
1355 error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec,
1356                                                       bool &Result) const {
1357   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1358   if (sec->sh_flags & ELF::SHF_WRITE || sec->sh_flags & ELF::SHF_EXECINSTR)
1359     Result = false;
1360   else
1361     Result = true;
1362   return object_error::success;
1363 }
1364
1365 template<class ELFT>
1366 error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
1367                                                       DataRefImpl Symb,
1368                                                       bool &Result) const {
1369   // FIXME: Unimplemented.
1370   Result = false;
1371   return object_error::success;
1372 }
1373
1374 template<class ELFT>
1375 relocation_iterator
1376 ELFObjectFile<ELFT>::getSectionRelBegin(DataRefImpl Sec) const {
1377   DataRefImpl RelData;
1378   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1379   typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1380   if (sec != 0 && ittr != SectionRelocMap.end()) {
1381     RelData.w.a = getSection(ittr->second[0])->sh_info;
1382     RelData.w.b = ittr->second[0];
1383     RelData.w.c = 0;
1384   }
1385   return relocation_iterator(RelocationRef(RelData, this));
1386 }
1387
1388 template<class ELFT>
1389 relocation_iterator
1390 ELFObjectFile<ELFT>::getSectionRelEnd(DataRefImpl Sec) const {
1391   DataRefImpl RelData;
1392   const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1393   typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
1394   if (sec != 0 && ittr != SectionRelocMap.end()) {
1395     // Get the index of the last relocation section for this section.
1396     std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
1397     const Elf_Shdr *relocsec = getSection(relocsecindex);
1398     RelData.w.a = relocsec->sh_info;
1399     RelData.w.b = relocsecindex;
1400     RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
1401   }
1402   return relocation_iterator(RelocationRef(RelData, this));
1403 }
1404
1405 // Relocations
1406 template<class ELFT>
1407 error_code ELFObjectFile<ELFT>::getRelocationNext(DataRefImpl Rel,
1408                                                   RelocationRef &Result) const {
1409   ++Rel.w.c;
1410   const Elf_Shdr *relocsec = getSection(Rel.w.b);
1411   if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
1412     // We have reached the end of the relocations for this section. See if there
1413     // is another relocation section.
1414     typename RelocMap_t::mapped_type relocseclist =
1415       SectionRelocMap.lookup(getSection(Rel.w.a));
1416
1417     // Do a binary search for the current reloc section index (which must be
1418     // present). Then get the next one.
1419     typename RelocMap_t::mapped_type::const_iterator loc =
1420       std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
1421     ++loc;
1422
1423     // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
1424     // to the end iterator.
1425     if (loc != relocseclist.end()) {
1426       Rel.w.b = *loc;
1427       Rel.w.a = 0;
1428     }
1429   }
1430   Result = RelocationRef(Rel, this);
1431   return object_error::success;
1432 }
1433
1434 template<class ELFT>
1435 error_code ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel,
1436                                                     SymbolRef &Result) const {
1437   uint32_t symbolIdx;
1438   const Elf_Shdr *sec = getSection(Rel.w.b);
1439   switch (sec->sh_type) {
1440     default :
1441       report_fatal_error("Invalid section type in Rel!");
1442     case ELF::SHT_REL : {
1443       symbolIdx = getRel(Rel)->getSymbol();
1444       break;
1445     }
1446     case ELF::SHT_RELA : {
1447       symbolIdx = getRela(Rel)->getSymbol();
1448       break;
1449     }
1450   }
1451   DataRefImpl SymbolData;
1452   IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
1453   if (it == SymbolTableSectionsIndexMap.end())
1454     report_fatal_error("Relocation symbol table not found!");
1455   SymbolData.d.a = symbolIdx;
1456   SymbolData.d.b = it->second;
1457   Result = SymbolRef(SymbolData, this);
1458   return object_error::success;
1459 }
1460
1461 template<class ELFT>
1462 error_code ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
1463                                                      uint64_t &Result) const {
1464   uint64_t offset;
1465   const Elf_Shdr *sec = getSection(Rel.w.b);
1466   switch (sec->sh_type) {
1467     default :
1468       report_fatal_error("Invalid section type in Rel!");
1469     case ELF::SHT_REL : {
1470       offset = getRel(Rel)->r_offset;
1471       break;
1472     }
1473     case ELF::SHT_RELA : {
1474       offset = getRela(Rel)->r_offset;
1475       break;
1476     }
1477   }
1478
1479   Result = offset;
1480   return object_error::success;
1481 }
1482
1483 template<class ELFT>
1484 error_code ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
1485                                                     uint64_t &Result) const {
1486   uint64_t offset;
1487   const Elf_Shdr *sec = getSection(Rel.w.b);
1488   switch (sec->sh_type) {
1489     default :
1490       report_fatal_error("Invalid section type in Rel!");
1491     case ELF::SHT_REL : {
1492       offset = getRel(Rel)->r_offset;
1493       break;
1494     }
1495     case ELF::SHT_RELA : {
1496       offset = getRela(Rel)->r_offset;
1497       break;
1498     }
1499   }
1500
1501   Result = offset - sec->sh_addr;
1502   return object_error::success;
1503 }
1504
1505 template<class ELFT>
1506 error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
1507                                                   uint64_t &Result) const {
1508   const Elf_Shdr *sec = getSection(Rel.w.b);
1509   switch (sec->sh_type) {
1510     default :
1511       report_fatal_error("Invalid section type in Rel!");
1512     case ELF::SHT_REL : {
1513       Result = getRel(Rel)->getType();
1514       break;
1515     }
1516     case ELF::SHT_RELA : {
1517       Result = getRela(Rel)->getType();
1518       break;
1519     }
1520   }
1521   return object_error::success;
1522 }
1523
1524 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
1525   case ELF::enum: res = #enum; break;
1526
1527 template<class ELFT>
1528 error_code ELFObjectFile<ELFT>::getRelocationTypeName(
1529     DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1530   const Elf_Shdr *sec = getSection(Rel.w.b);
1531   uint32_t type;
1532   StringRef res;
1533   switch (sec->sh_type) {
1534     default :
1535       return object_error::parse_failed;
1536     case ELF::SHT_REL : {
1537       type = getRel(Rel)->getType();
1538       break;
1539     }
1540     case ELF::SHT_RELA : {
1541       type = getRela(Rel)->getType();
1542       break;
1543     }
1544   }
1545   switch (Header->e_machine) {
1546   case ELF::EM_X86_64:
1547     switch (type) {
1548       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
1549       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
1550       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
1551       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
1552       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
1553       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
1554       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
1555       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
1556       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
1557       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
1558       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
1559       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
1560       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
1561       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
1562       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
1563       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
1564       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
1565       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
1566       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
1567       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
1568       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
1569       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
1570       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
1571       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
1572       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
1573       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
1574       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
1575       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
1576       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
1577       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
1578       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
1579       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
1580     default:
1581       res = "Unknown";
1582     }
1583     break;
1584   case ELF::EM_386:
1585     switch (type) {
1586       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
1587       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
1588       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
1589       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
1590       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
1591       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
1592       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
1593       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
1594       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
1595       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
1596       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
1597       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
1598       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
1599       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
1600       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
1601       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
1602       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
1603       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
1604       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
1605       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
1606       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
1607       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
1608       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
1609       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
1610       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
1611       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
1612       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
1613       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
1614       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
1615       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
1616       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
1617       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
1618       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
1619       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
1620       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
1621       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
1622       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
1623       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
1624       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
1625       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
1626     default:
1627       res = "Unknown";
1628     }
1629     break;
1630   case ELF::EM_ARM:
1631     switch (type) {
1632       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_NONE);
1633       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PC24);
1634       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32);
1635       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32);
1636       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G0);
1637       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS16);
1638       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS12);
1639       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ABS5);
1640       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS8);
1641       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL32);
1642       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_CALL);
1643       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC8);
1644       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BREL_ADJ);
1645       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESC);
1646       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_SWI8);
1647       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_XPC25);
1648       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_XPC22);
1649       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPMOD32);
1650       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPOFF32);
1651       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_TPOFF32);
1652       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_COPY);
1653       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GLOB_DAT);
1654       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP_SLOT);
1655       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_RELATIVE);
1656       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF32);
1657       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_PREL);
1658       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL);
1659       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32);
1660       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_CALL);
1661       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP24);
1662       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP24);
1663       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_ABS);
1664       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_7_0);
1665       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_15_8);
1666       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_23_15);
1667       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SBREL_11_0_NC);
1668       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_19_12_NC);
1669       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_27_20_CK);
1670       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET1);
1671       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL31);
1672       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_V4BX);
1673       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET2);
1674       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PREL31);
1675       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_ABS_NC);
1676       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_ABS);
1677       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_PREL_NC);
1678       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_PREL);
1679       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_ABS_NC);
1680       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_ABS);
1681       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_PREL_NC);
1682       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_PREL);
1683       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP19);
1684       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP6);
1685       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ALU_PREL_11_0);
1686       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC12);
1687       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32_NOI);
1688       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32_NOI);
1689       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0_NC);
1690       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0);
1691       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1_NC);
1692       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1);
1693       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G2);
1694       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G1);
1695       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G2);
1696       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G0);
1697       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G1);
1698       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G2);
1699       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G0);
1700       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G1);
1701       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G2);
1702       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0_NC);
1703       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0);
1704       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1_NC);
1705       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1);
1706       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G2);
1707       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G0);
1708       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G1);
1709       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G2);
1710       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G0);
1711       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G1);
1712       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G2);
1713       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G0);
1714       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G1);
1715       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G2);
1716       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL_NC);
1717       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_BREL);
1718       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL);
1719       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL_NC);
1720       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_BREL);
1721       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL);
1722       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GOTDESC);
1723       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_CALL);
1724       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESCSEQ);
1725       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_CALL);
1726       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32_ABS);
1727       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_ABS);
1728       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_PREL);
1729       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL12);
1730       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF12);
1731       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTRELAX);
1732       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTENTRY);
1733       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTINHERIT);
1734       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP11);
1735       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP8);
1736       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GD32);
1737       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDM32);
1738       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO32);
1739       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE32);
1740       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE32);
1741       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO12);
1742       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE12);
1743       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE12GP);
1744       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_0);
1745       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_1);
1746       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_2);
1747       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_3);
1748       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_4);
1749       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_5);
1750       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_6);
1751       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_7);
1752       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_8);
1753       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_9);
1754       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_10);
1755       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_11);
1756       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_12);
1757       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_13);
1758       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_14);
1759       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_15);
1760       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ME_TOO);
1761       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ16);
1762       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ32);
1763     default:
1764       res = "Unknown";
1765     }
1766     break;
1767   case ELF::EM_HEXAGON:
1768     switch (type) {
1769       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_NONE);
1770       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL);
1771       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL);
1772       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL);
1773       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_LO16);
1774       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HI16);
1775       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32);
1776       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16);
1777       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8);
1778       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_0);
1779       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_1);
1780       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_2);
1781       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_3);
1782       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HL16);
1783       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL);
1784       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL);
1785       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B32_PCREL_X);
1786       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_6_X);
1787       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL_X);
1788       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL_X);
1789       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL_X);
1790       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL_X);
1791       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL_X);
1792       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16_X);
1793       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_12_X);
1794       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_11_X);
1795       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_10_X);
1796       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_9_X);
1797       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8_X);
1798       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_7_X);
1799       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_X);
1800       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_PCREL);
1801       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_COPY);
1802       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GLOB_DAT);
1803       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_JMP_SLOT);
1804       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_RELATIVE);
1805       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_PLT_B22_PCREL);
1806       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_LO16);
1807       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_HI16);
1808       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32);
1809       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_LO16);
1810       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_HI16);
1811       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32);
1812       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16);
1813       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPMOD_32);
1814       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_LO16);
1815       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_HI16);
1816       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32);
1817       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16);
1818       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_PLT_B22_PCREL);
1819       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_LO16);
1820       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_HI16);
1821       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32);
1822       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16);
1823       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_LO16);
1824       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_HI16);
1825       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32);
1826       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_LO16);
1827       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_HI16);
1828       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32);
1829       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16);
1830       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_LO16);
1831       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_HI16);
1832       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32);
1833       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16);
1834       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_PCREL_X);
1835       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32_6_X);
1836       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_16_X);
1837       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_11_X);
1838       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32_6_X);
1839       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16_X);
1840       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_11_X);
1841       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32_6_X);
1842       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16_X);
1843       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_11_X);
1844       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32_6_X);
1845       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16_X);
1846       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_11_X);
1847       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32_6_X);
1848       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_16_X);
1849       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32_6_X);
1850       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16_X);
1851       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_11_X);
1852       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32_6_X);
1853       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16_X);
1854       LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_11_X);
1855     default:
1856       res = "Unknown";
1857     }
1858     break;
1859   default:
1860     res = "Unknown";
1861   }
1862   Result.append(res.begin(), res.end());
1863   return object_error::success;
1864 }
1865
1866 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
1867
1868 template<class ELFT>
1869 error_code ELFObjectFile<ELFT>::getRelocationAdditionalInfo(
1870     DataRefImpl Rel, int64_t &Result) const {
1871   const Elf_Shdr *sec = getSection(Rel.w.b);
1872   switch (sec->sh_type) {
1873     default :
1874       report_fatal_error("Invalid section type in Rel!");
1875     case ELF::SHT_REL : {
1876       Result = 0;
1877       return object_error::success;
1878     }
1879     case ELF::SHT_RELA : {
1880       Result = getRela(Rel)->r_addend;
1881       return object_error::success;
1882     }
1883   }
1884 }
1885
1886 template<class ELFT>
1887 error_code ELFObjectFile<ELFT>::getRelocationValueString(
1888     DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1889   const Elf_Shdr *sec = getSection(Rel.w.b);
1890   uint8_t type;
1891   StringRef res;
1892   int64_t addend = 0;
1893   uint16_t symbol_index = 0;
1894   switch (sec->sh_type) {
1895     default:
1896       return object_error::parse_failed;
1897     case ELF::SHT_REL: {
1898       type = getRel(Rel)->getType();
1899       symbol_index = getRel(Rel)->getSymbol();
1900       // TODO: Read implicit addend from section data.
1901       break;
1902     }
1903     case ELF::SHT_RELA: {
1904       type = getRela(Rel)->getType();
1905       symbol_index = getRela(Rel)->getSymbol();
1906       addend = getRela(Rel)->r_addend;
1907       break;
1908     }
1909   }
1910   const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1911   StringRef symname;
1912   if (error_code ec = getSymbolName(getSection(sec->sh_link), symb, symname))
1913     return ec;
1914   switch (Header->e_machine) {
1915   case ELF::EM_X86_64:
1916     switch (type) {
1917     case ELF::R_X86_64_PC8:
1918     case ELF::R_X86_64_PC16:
1919     case ELF::R_X86_64_PC32: {
1920         std::string fmtbuf;
1921         raw_string_ostream fmt(fmtbuf);
1922         fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1923         fmt.flush();
1924         Result.append(fmtbuf.begin(), fmtbuf.end());
1925       }
1926       break;
1927     case ELF::R_X86_64_8:
1928     case ELF::R_X86_64_16:
1929     case ELF::R_X86_64_32:
1930     case ELF::R_X86_64_32S:
1931     case ELF::R_X86_64_64: {
1932         std::string fmtbuf;
1933         raw_string_ostream fmt(fmtbuf);
1934         fmt << symname << (addend < 0 ? "" : "+") << addend;
1935         fmt.flush();
1936         Result.append(fmtbuf.begin(), fmtbuf.end());
1937       }
1938       break;
1939     default:
1940       res = "Unknown";
1941     }
1942     break;
1943   case ELF::EM_ARM:
1944   case ELF::EM_HEXAGON:
1945     res = symname;
1946     break;
1947   default:
1948     res = "Unknown";
1949   }
1950   if (Result.empty())
1951     Result.append(res.begin(), res.end());
1952   return object_error::success;
1953 }
1954
1955 // Verify that the last byte in the string table in a null.
1956 template<class ELFT>
1957 void ELFObjectFile<ELFT>::VerifyStrTab(const Elf_Shdr *sh) const {
1958   const char *strtab = (const char*)base() + sh->sh_offset;
1959   if (strtab[sh->sh_size - 1] != 0)
1960     // FIXME: Proper error handling.
1961     report_fatal_error("String table must end with a null terminator!");
1962 }
1963
1964 template<class ELFT>
1965 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBuffer *Object, error_code &ec)
1966   : ObjectFile(getELFType(
1967       static_cast<endianness>(ELFT::TargetEndianness) == support::little,
1968       ELFT::Is64Bits),
1969       Object,
1970       ec)
1971   , isDyldELFObject(false)
1972   , SectionHeaderTable(0)
1973   , dot_shstrtab_sec(0)
1974   , dot_strtab_sec(0)
1975   , dot_dynstr_sec(0)
1976   , dot_dynamic_sec(0)
1977   , dot_gnu_version_sec(0)
1978   , dot_gnu_version_r_sec(0)
1979   , dot_gnu_version_d_sec(0)
1980   , dt_soname(0)
1981  {
1982
1983   const uint64_t FileSize = Data->getBufferSize();
1984
1985   if (sizeof(Elf_Ehdr) > FileSize)
1986     // FIXME: Proper error handling.
1987     report_fatal_error("File too short!");
1988
1989   Header = reinterpret_cast<const Elf_Ehdr *>(base());
1990
1991   if (Header->e_shoff == 0)
1992     return;
1993
1994   const uint64_t SectionTableOffset = Header->e_shoff;
1995
1996   if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize)
1997     // FIXME: Proper error handling.
1998     report_fatal_error("Section header table goes past end of file!");
1999
2000   // The getNumSections() call below depends on SectionHeaderTable being set.
2001   SectionHeaderTable =
2002     reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
2003   const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
2004
2005   if (SectionTableOffset + SectionTableSize > FileSize)
2006     // FIXME: Proper error handling.
2007     report_fatal_error("Section table goes past end of file!");
2008
2009   // To find the symbol tables we walk the section table to find SHT_SYMTAB.
2010   const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
2011   const Elf_Shdr* sh = SectionHeaderTable;
2012
2013   // Reserve SymbolTableSections[0] for .dynsym
2014   SymbolTableSections.push_back(NULL);
2015
2016   for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
2017     switch (sh->sh_type) {
2018     case ELF::SHT_SYMTAB_SHNDX: {
2019       if (SymbolTableSectionHeaderIndex)
2020         // FIXME: Proper error handling.
2021         report_fatal_error("More than one .symtab_shndx!");
2022       SymbolTableSectionHeaderIndex = sh;
2023       break;
2024     }
2025     case ELF::SHT_SYMTAB: {
2026       SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
2027       SymbolTableSections.push_back(sh);
2028       break;
2029     }
2030     case ELF::SHT_DYNSYM: {
2031       if (SymbolTableSections[0] != NULL)
2032         // FIXME: Proper error handling.
2033         report_fatal_error("More than one .dynsym!");
2034       SymbolTableSectionsIndexMap[i] = 0;
2035       SymbolTableSections[0] = sh;
2036       break;
2037     }
2038     case ELF::SHT_REL:
2039     case ELF::SHT_RELA: {
2040       SectionRelocMap[getSection(sh->sh_info)].push_back(i);
2041       break;
2042     }
2043     case ELF::SHT_DYNAMIC: {
2044       if (dot_dynamic_sec != NULL)
2045         // FIXME: Proper error handling.
2046         report_fatal_error("More than one .dynamic!");
2047       dot_dynamic_sec = sh;
2048       break;
2049     }
2050     case ELF::SHT_GNU_versym: {
2051       if (dot_gnu_version_sec != NULL)
2052         // FIXME: Proper error handling.
2053         report_fatal_error("More than one .gnu.version section!");
2054       dot_gnu_version_sec = sh;
2055       break;
2056     }
2057     case ELF::SHT_GNU_verdef: {
2058       if (dot_gnu_version_d_sec != NULL)
2059         // FIXME: Proper error handling.
2060         report_fatal_error("More than one .gnu.version_d section!");
2061       dot_gnu_version_d_sec = sh;
2062       break;
2063     }
2064     case ELF::SHT_GNU_verneed: {
2065       if (dot_gnu_version_r_sec != NULL)
2066         // FIXME: Proper error handling.
2067         report_fatal_error("More than one .gnu.version_r section!");
2068       dot_gnu_version_r_sec = sh;
2069       break;
2070     }
2071     }
2072     ++sh;
2073   }
2074
2075   // Sort section relocation lists by index.
2076   for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
2077                                      e = SectionRelocMap.end(); i != e; ++i) {
2078     std::sort(i->second.begin(), i->second.end());
2079   }
2080
2081   // Get string table sections.
2082   dot_shstrtab_sec = getSection(getStringTableIndex());
2083   if (dot_shstrtab_sec) {
2084     // Verify that the last byte in the string table in a null.
2085     VerifyStrTab(dot_shstrtab_sec);
2086   }
2087
2088   // Merge this into the above loop.
2089   for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
2090                   *e = i + getNumSections() * Header->e_shentsize;
2091                    i != e; i += Header->e_shentsize) {
2092     const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
2093     if (sh->sh_type == ELF::SHT_STRTAB) {
2094       StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
2095       if (SectionName == ".strtab") {
2096         if (dot_strtab_sec != 0)
2097           // FIXME: Proper error handling.
2098           report_fatal_error("Already found section named .strtab!");
2099         dot_strtab_sec = sh;
2100         VerifyStrTab(dot_strtab_sec);
2101       } else if (SectionName == ".dynstr") {
2102         if (dot_dynstr_sec != 0)
2103           // FIXME: Proper error handling.
2104           report_fatal_error("Already found section named .dynstr!");
2105         dot_dynstr_sec = sh;
2106         VerifyStrTab(dot_dynstr_sec);
2107       }
2108     }
2109   }
2110
2111   // Build symbol name side-mapping if there is one.
2112   if (SymbolTableSectionHeaderIndex) {
2113     const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
2114                                       SymbolTableSectionHeaderIndex->sh_offset);
2115     error_code ec;
2116     for (symbol_iterator si = begin_symbols(),
2117                          se = end_symbols(); si != se; si.increment(ec)) {
2118       if (ec)
2119         report_fatal_error("Fewer extended symbol table entries than symbols!");
2120       if (*ShndxTable != ELF::SHN_UNDEF)
2121         ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
2122       ++ShndxTable;
2123     }
2124   }
2125 }
2126
2127 // Get the symbol table index in the symtab section given a symbol
2128 template<class ELFT>
2129 uint64_t ELFObjectFile<ELFT>::getSymbolIndex(const Elf_Sym *Sym) const {
2130   assert(SymbolTableSections.size() == 1 && "Only one symbol table supported!");
2131   const Elf_Shdr *SymTab = *SymbolTableSections.begin();
2132   uintptr_t SymLoc = uintptr_t(Sym);
2133   uintptr_t SymTabLoc = uintptr_t(base() + SymTab->sh_offset);
2134   assert(SymLoc > SymTabLoc && "Symbol not in symbol table!");
2135   uint64_t SymOffset = SymLoc - SymTabLoc;
2136   assert(SymOffset % SymTab->sh_entsize == 0 &&
2137          "Symbol not multiple of symbol size!");
2138   return SymOffset / SymTab->sh_entsize;
2139 }
2140
2141 template<class ELFT>
2142 symbol_iterator ELFObjectFile<ELFT>::begin_symbols() const {
2143   DataRefImpl SymbolData;
2144   if (SymbolTableSections.size() <= 1) {
2145     SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2146     SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2147   } else {
2148     SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2149     SymbolData.d.b = 1; // The 0th table is .dynsym
2150   }
2151   return symbol_iterator(SymbolRef(SymbolData, this));
2152 }
2153
2154 template<class ELFT>
2155 symbol_iterator ELFObjectFile<ELFT>::end_symbols() const {
2156   DataRefImpl SymbolData;
2157   SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2158   SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2159   return symbol_iterator(SymbolRef(SymbolData, this));
2160 }
2161
2162 template<class ELFT>
2163 symbol_iterator ELFObjectFile<ELFT>::begin_dynamic_symbols() const {
2164   DataRefImpl SymbolData;
2165   if (SymbolTableSections[0] == NULL) {
2166     SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2167     SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2168   } else {
2169     SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2170     SymbolData.d.b = 0; // The 0th table is .dynsym
2171   }
2172   return symbol_iterator(SymbolRef(SymbolData, this));
2173 }
2174
2175 template<class ELFT>
2176 symbol_iterator ELFObjectFile<ELFT>::end_dynamic_symbols() const {
2177   DataRefImpl SymbolData;
2178   SymbolData.d.a = std::numeric_limits<uint32_t>::max();
2179   SymbolData.d.b = std::numeric_limits<uint32_t>::max();
2180   return symbol_iterator(SymbolRef(SymbolData, this));
2181 }
2182
2183 template<class ELFT>
2184 section_iterator ELFObjectFile<ELFT>::begin_sections() const {
2185   DataRefImpl ret;
2186   ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
2187   return section_iterator(SectionRef(ret, this));
2188 }
2189
2190 template<class ELFT>
2191 section_iterator ELFObjectFile<ELFT>::end_sections() const {
2192   DataRefImpl ret;
2193   ret.p = reinterpret_cast<intptr_t>(base()
2194                                      + Header->e_shoff
2195                                      + (Header->e_shentsize*getNumSections()));
2196   return section_iterator(SectionRef(ret, this));
2197 }
2198
2199 template<class ELFT>
2200 typename ELFObjectFile<ELFT>::dyn_iterator
2201 ELFObjectFile<ELFT>::begin_dynamic_table() const {
2202   DataRefImpl DynData;
2203   if (dot_dynamic_sec == NULL || dot_dynamic_sec->sh_size == 0) {
2204     DynData.d.a = std::numeric_limits<uint32_t>::max();
2205   } else {
2206     DynData.d.a = 0;
2207   }
2208   return dyn_iterator(DynRef(DynData, this));
2209 }
2210
2211 template<class ELFT>
2212 typename ELFObjectFile<ELFT>::dyn_iterator
2213 ELFObjectFile<ELFT>::end_dynamic_table() const {
2214   DataRefImpl DynData;
2215   DynData.d.a = std::numeric_limits<uint32_t>::max();
2216   return dyn_iterator(DynRef(DynData, this));
2217 }
2218
2219 template<class ELFT>
2220 error_code ELFObjectFile<ELFT>::getDynNext(DataRefImpl DynData,
2221                                            DynRef &Result) const {
2222   ++DynData.d.a;
2223
2224   // Check to see if we are at the end of .dynamic
2225   if (DynData.d.a >= dot_dynamic_sec->getEntityCount()) {
2226     // We are at the end. Return the terminator.
2227     DynData.d.a = std::numeric_limits<uint32_t>::max();
2228   }
2229
2230   Result = DynRef(DynData, this);
2231   return object_error::success;
2232 }
2233
2234 template<class ELFT>
2235 StringRef
2236 ELFObjectFile<ELFT>::getLoadName() const {
2237   if (!dt_soname) {
2238     // Find the DT_SONAME entry
2239     dyn_iterator it = begin_dynamic_table();
2240     dyn_iterator ie = end_dynamic_table();
2241     error_code ec;
2242     while (it != ie) {
2243       if (it->getTag() == ELF::DT_SONAME)
2244         break;
2245       it.increment(ec);
2246       if (ec)
2247         report_fatal_error("dynamic table iteration failed");
2248     }
2249     if (it != ie) {
2250       if (dot_dynstr_sec == NULL)
2251         report_fatal_error("Dynamic string table is missing");
2252       dt_soname = getString(dot_dynstr_sec, it->getVal());
2253     } else {
2254       dt_soname = "";
2255     }
2256   }
2257   return dt_soname;
2258 }
2259
2260 template<class ELFT>
2261 library_iterator ELFObjectFile<ELFT>::begin_libraries_needed() const {
2262   // Find the first DT_NEEDED entry
2263   dyn_iterator i = begin_dynamic_table();
2264   dyn_iterator e = end_dynamic_table();
2265   error_code ec;
2266   while (i != e) {
2267     if (i->getTag() == ELF::DT_NEEDED)
2268       break;
2269     i.increment(ec);
2270     if (ec)
2271       report_fatal_error("dynamic table iteration failed");
2272   }
2273   // Use the same DataRefImpl format as DynRef.
2274   return library_iterator(LibraryRef(i->getRawDataRefImpl(), this));
2275 }
2276
2277 template<class ELFT>
2278 error_code ELFObjectFile<ELFT>::getLibraryNext(DataRefImpl Data,
2279                                                LibraryRef &Result) const {
2280   // Use the same DataRefImpl format as DynRef.
2281   dyn_iterator i = dyn_iterator(DynRef(Data, this));
2282   dyn_iterator e = end_dynamic_table();
2283
2284   // Skip the current dynamic table entry.
2285   error_code ec;
2286   if (i != e) {
2287     i.increment(ec);
2288     // TODO: proper error handling
2289     if (ec)
2290       report_fatal_error("dynamic table iteration failed");
2291   }
2292
2293   // Find the next DT_NEEDED entry.
2294   while (i != e) {
2295     if (i->getTag() == ELF::DT_NEEDED)
2296       break;
2297     i.increment(ec);
2298     if (ec)
2299       report_fatal_error("dynamic table iteration failed");
2300   }
2301   Result = LibraryRef(i->getRawDataRefImpl(), this);
2302   return object_error::success;
2303 }
2304
2305 template<class ELFT>
2306 error_code ELFObjectFile<ELFT>::getLibraryPath(DataRefImpl Data,
2307                                                StringRef &Res) const {
2308   dyn_iterator i = dyn_iterator(DynRef(Data, this));
2309   if (i == end_dynamic_table())
2310     report_fatal_error("getLibraryPath() called on iterator end");
2311
2312   if (i->getTag() != ELF::DT_NEEDED)
2313     report_fatal_error("Invalid library_iterator");
2314
2315   // This uses .dynstr to lookup the name of the DT_NEEDED entry.
2316   // THis works as long as DT_STRTAB == .dynstr. This is true most of
2317   // the time, but the specification allows exceptions.
2318   // TODO: This should really use DT_STRTAB instead. Doing this requires
2319   // reading the program headers.
2320   if (dot_dynstr_sec == NULL)
2321     report_fatal_error("Dynamic string table is missing");
2322   Res = getString(dot_dynstr_sec, i->getVal());
2323   return object_error::success;
2324 }
2325
2326 template<class ELFT>
2327 library_iterator ELFObjectFile<ELFT>::end_libraries_needed() const {
2328   dyn_iterator e = end_dynamic_table();
2329   // Use the same DataRefImpl format as DynRef.
2330   return library_iterator(LibraryRef(e->getRawDataRefImpl(), this));
2331 }
2332
2333 template<class ELFT>
2334 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
2335   return ELFT::Is64Bits ? 8 : 4;
2336 }
2337
2338 template<class ELFT>
2339 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
2340   switch(Header->e_ident[ELF::EI_CLASS]) {
2341   case ELF::ELFCLASS32:
2342     switch(Header->e_machine) {
2343     case ELF::EM_386:
2344       return "ELF32-i386";
2345     case ELF::EM_X86_64:
2346       return "ELF32-x86-64";
2347     case ELF::EM_ARM:
2348       return "ELF32-arm";
2349     case ELF::EM_HEXAGON:
2350       return "ELF32-hexagon";
2351     default:
2352       return "ELF32-unknown";
2353     }
2354   case ELF::ELFCLASS64:
2355     switch(Header->e_machine) {
2356     case ELF::EM_386:
2357       return "ELF64-i386";
2358     case ELF::EM_X86_64:
2359       return "ELF64-x86-64";
2360     case ELF::EM_PPC64:
2361       return "ELF64-ppc64";
2362     default:
2363       return "ELF64-unknown";
2364     }
2365   default:
2366     // FIXME: Proper error handling.
2367     report_fatal_error("Invalid ELFCLASS!");
2368   }
2369 }
2370
2371 template<class ELFT>
2372 unsigned ELFObjectFile<ELFT>::getArch() const {
2373   switch(Header->e_machine) {
2374   case ELF::EM_386:
2375     return Triple::x86;
2376   case ELF::EM_X86_64:
2377     return Triple::x86_64;
2378   case ELF::EM_ARM:
2379     return Triple::arm;
2380   case ELF::EM_HEXAGON:
2381     return Triple::hexagon;
2382   case ELF::EM_MIPS:
2383     return (static_cast<int>(ELFT::TargetEndianness)
2384             == static_cast<int>(support::little)) ?
2385            Triple::mipsel : Triple::mips;
2386   case ELF::EM_PPC64:
2387     return Triple::ppc64;
2388   default:
2389     return Triple::UnknownArch;
2390   }
2391 }
2392
2393 template<class ELFT>
2394 uint64_t ELFObjectFile<ELFT>::getNumSections() const {
2395   assert(Header && "Header not initialized!");
2396   if (Header->e_shnum == ELF::SHN_UNDEF) {
2397     assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
2398     return SectionHeaderTable->sh_size;
2399   }
2400   return Header->e_shnum;
2401 }
2402
2403 template<class ELFT>
2404 uint64_t
2405 ELFObjectFile<ELFT>::getStringTableIndex() const {
2406   if (Header->e_shnum == ELF::SHN_UNDEF) {
2407     if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
2408       return SectionHeaderTable->sh_link;
2409     if (Header->e_shstrndx >= getNumSections())
2410       return 0;
2411   }
2412   return Header->e_shstrndx;
2413 }
2414
2415 template<class ELFT>
2416 template<typename T>
2417 inline const T *
2418 ELFObjectFile<ELFT>::getEntry(uint16_t Section, uint32_t Entry) const {
2419   return getEntry<T>(getSection(Section), Entry);
2420 }
2421
2422 template<class ELFT>
2423 template<typename T>
2424 inline const T *
2425 ELFObjectFile<ELFT>::getEntry(const Elf_Shdr * Section, uint32_t Entry) const {
2426   return reinterpret_cast<const T *>(
2427            base()
2428            + Section->sh_offset
2429            + (Entry * Section->sh_entsize));
2430 }
2431
2432 template<class ELFT>
2433 const typename ELFObjectFile<ELFT>::Elf_Sym *
2434 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
2435   return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
2436 }
2437
2438 template<class ELFT>
2439 const typename ELFObjectFile<ELFT>::Elf_Dyn *
2440 ELFObjectFile<ELFT>::getDyn(DataRefImpl DynData) const {
2441   return getEntry<Elf_Dyn>(dot_dynamic_sec, DynData.d.a);
2442 }
2443
2444 template<class ELFT>
2445 const typename ELFObjectFile<ELFT>::Elf_Rel *
2446 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
2447   return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
2448 }
2449
2450 template<class ELFT>
2451 const typename ELFObjectFile<ELFT>::Elf_Rela *
2452 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
2453   return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
2454 }
2455
2456 template<class ELFT>
2457 const typename ELFObjectFile<ELFT>::Elf_Shdr *
2458 ELFObjectFile<ELFT>::getSection(DataRefImpl Symb) const {
2459   const Elf_Shdr *sec = getSection(Symb.d.b);
2460   if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
2461     // FIXME: Proper error handling.
2462     report_fatal_error("Invalid symbol table section!");
2463   return sec;
2464 }
2465
2466 template<class ELFT>
2467 const typename ELFObjectFile<ELFT>::Elf_Shdr *
2468 ELFObjectFile<ELFT>::getSection(uint32_t index) const {
2469   if (index == 0)
2470     return 0;
2471   if (!SectionHeaderTable || index >= getNumSections())
2472     // FIXME: Proper error handling.
2473     report_fatal_error("Invalid section index!");
2474
2475   return reinterpret_cast<const Elf_Shdr *>(
2476          reinterpret_cast<const char *>(SectionHeaderTable)
2477          + (index * Header->e_shentsize));
2478 }
2479
2480 template<class ELFT>
2481 const char *ELFObjectFile<ELFT>::getString(uint32_t section,
2482                                            ELF::Elf32_Word offset) const {
2483   return getString(getSection(section), offset);
2484 }
2485
2486 template<class ELFT>
2487 const char *ELFObjectFile<ELFT>::getString(const Elf_Shdr *section,
2488                                            ELF::Elf32_Word offset) const {
2489   assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
2490   if (offset >= section->sh_size)
2491     // FIXME: Proper error handling.
2492     report_fatal_error("Symbol name offset outside of string table!");
2493   return (const char *)base() + section->sh_offset + offset;
2494 }
2495
2496 template<class ELFT>
2497 error_code ELFObjectFile<ELFT>::getSymbolName(const Elf_Shdr *section,
2498                                               const Elf_Sym *symb,
2499                                               StringRef &Result) const {
2500   if (symb->st_name == 0) {
2501     const Elf_Shdr *section = getSection(symb);
2502     if (!section)
2503       Result = "";
2504     else
2505       Result = getString(dot_shstrtab_sec, section->sh_name);
2506     return object_error::success;
2507   }
2508
2509   if (section == SymbolTableSections[0]) {
2510     // Symbol is in .dynsym, use .dynstr string table
2511     Result = getString(dot_dynstr_sec, symb->st_name);
2512   } else {
2513     // Use the default symbol table name section.
2514     Result = getString(dot_strtab_sec, symb->st_name);
2515   }
2516   return object_error::success;
2517 }
2518
2519 template<class ELFT>
2520 error_code ELFObjectFile<ELFT>::getSectionName(const Elf_Shdr *section,
2521                                                StringRef &Result) const {
2522   Result = StringRef(getString(dot_shstrtab_sec, section->sh_name));
2523   return object_error::success;
2524 }
2525
2526 template<class ELFT>
2527 error_code ELFObjectFile<ELFT>::getSymbolVersion(const Elf_Shdr *section,
2528                                                  const Elf_Sym *symb,
2529                                                  StringRef &Version,
2530                                                  bool &IsDefault) const {
2531   // Handle non-dynamic symbols.
2532   if (section != SymbolTableSections[0]) {
2533     // Non-dynamic symbols can have versions in their names
2534     // A name of the form 'foo@V1' indicates version 'V1', non-default.
2535     // A name of the form 'foo@@V2' indicates version 'V2', default version.
2536     StringRef Name;
2537     error_code ec = getSymbolName(section, symb, Name);
2538     if (ec != object_error::success)
2539       return ec;
2540     size_t atpos = Name.find('@');
2541     if (atpos == StringRef::npos) {
2542       Version = "";
2543       IsDefault = false;
2544       return object_error::success;
2545     }
2546     ++atpos;
2547     if (atpos < Name.size() && Name[atpos] == '@') {
2548       IsDefault = true;
2549       ++atpos;
2550     } else {
2551       IsDefault = false;
2552     }
2553     Version = Name.substr(atpos);
2554     return object_error::success;
2555   }
2556
2557   // This is a dynamic symbol. Look in the GNU symbol version table.
2558   if (dot_gnu_version_sec == NULL) {
2559     // No version table.
2560     Version = "";
2561     IsDefault = false;
2562     return object_error::success;
2563   }
2564
2565   // Determine the position in the symbol table of this entry.
2566   const char *sec_start = (const char*)base() + section->sh_offset;
2567   size_t entry_index = ((const char*)symb - sec_start)/section->sh_entsize;
2568
2569   // Get the corresponding version index entry
2570   const Elf_Versym *vs = getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index);
2571   size_t version_index = vs->vs_index & ELF::VERSYM_VERSION;
2572
2573   // Special markers for unversioned symbols.
2574   if (version_index == ELF::VER_NDX_LOCAL ||
2575       version_index == ELF::VER_NDX_GLOBAL) {
2576     Version = "";
2577     IsDefault = false;
2578     return object_error::success;
2579   }
2580
2581   // Lookup this symbol in the version table
2582   LoadVersionMap();
2583   if (version_index >= VersionMap.size() || VersionMap[version_index].isNull())
2584     report_fatal_error("Symbol has version index without corresponding "
2585                        "define or reference entry");
2586   const VersionMapEntry &entry = VersionMap[version_index];
2587
2588   // Get the version name string
2589   size_t name_offset;
2590   if (entry.isVerdef()) {
2591     // The first Verdaux entry holds the name.
2592     name_offset = entry.getVerdef()->getAux()->vda_name;
2593   } else {
2594     name_offset = entry.getVernaux()->vna_name;
2595   }
2596   Version = getString(dot_dynstr_sec, name_offset);
2597
2598   // Set IsDefault
2599   if (entry.isVerdef()) {
2600     IsDefault = !(vs->vs_index & ELF::VERSYM_HIDDEN);
2601   } else {
2602     IsDefault = false;
2603   }
2604
2605   return object_error::success;
2606 }
2607
2608 template<class ELFT>
2609 inline DynRefImpl<ELFT>::DynRefImpl(DataRefImpl DynP, const OwningType *Owner)
2610   : DynPimpl(DynP)
2611   , OwningObject(Owner) {}
2612
2613 template<class ELFT>
2614 inline bool DynRefImpl<ELFT>::operator==(const DynRefImpl &Other) const {
2615   return DynPimpl == Other.DynPimpl;
2616 }
2617
2618 template<class ELFT>
2619 inline bool DynRefImpl<ELFT>::operator <(const DynRefImpl &Other) const {
2620   return DynPimpl < Other.DynPimpl;
2621 }
2622
2623 template<class ELFT>
2624 inline error_code DynRefImpl<ELFT>::getNext(DynRefImpl &Result) const {
2625   return OwningObject->getDynNext(DynPimpl, Result);
2626 }
2627
2628 template<class ELFT>
2629 inline int64_t DynRefImpl<ELFT>::getTag() const {
2630   return OwningObject->getDyn(DynPimpl)->d_tag;
2631 }
2632
2633 template<class ELFT>
2634 inline uint64_t DynRefImpl<ELFT>::getVal() const {
2635   return OwningObject->getDyn(DynPimpl)->d_un.d_val;
2636 }
2637
2638 template<class ELFT>
2639 inline uint64_t DynRefImpl<ELFT>::getPtr() const {
2640   return OwningObject->getDyn(DynPimpl)->d_un.d_ptr;
2641 }
2642
2643 template<class ELFT>
2644 inline DataRefImpl DynRefImpl<ELFT>::getRawDataRefImpl() const {
2645   return DynPimpl;
2646 }
2647
2648 /// This is a generic interface for retrieving GNU symbol version
2649 /// information from an ELFObjectFile.
2650 static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
2651                                              const SymbolRef &Sym,
2652                                              StringRef &Version,
2653                                              bool &IsDefault) {
2654   // Little-endian 32-bit
2655   if (const ELFObjectFile<ELFType<support::little, 4, false> > *ELFObj =
2656           dyn_cast<ELFObjectFile<ELFType<support::little, 4, false> > >(Obj))
2657     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2658
2659   // Big-endian 32-bit
2660   if (const ELFObjectFile<ELFType<support::big, 4, false> > *ELFObj =
2661           dyn_cast<ELFObjectFile<ELFType<support::big, 4, false> > >(Obj))
2662     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2663
2664   // Little-endian 64-bit
2665   if (const ELFObjectFile<ELFType<support::little, 8, true> > *ELFObj =
2666           dyn_cast<ELFObjectFile<ELFType<support::little, 8, true> > >(Obj))
2667     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2668
2669   // Big-endian 64-bit
2670   if (const ELFObjectFile<ELFType<support::big, 8, true> > *ELFObj =
2671           dyn_cast<ELFObjectFile<ELFType<support::big, 8, true> > >(Obj))
2672     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
2673
2674   llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");
2675 }
2676
2677 }
2678 }
2679
2680 #endif