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