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