a85fbd8701c79753e789a5d33dbcae4770b125d8
[oota-llvm.git] / include / llvm / Object / ELFObjectFile.h
1 //===- ELFObjectFile.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_ELFOBJECTFILE_H
15 #define LLVM_OBJECT_ELFOBJECTFILE_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/ELF.h"
23 #include "llvm/Object/ObjectFile.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/ELF.h"
26 #include "llvm/Support/Endian.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include <algorithm>
31 #include <cctype>
32 #include <limits>
33 #include <utility>
34
35 namespace llvm {
36 namespace object {
37
38 class ELFObjectFileBase : public ObjectFile {
39 protected:
40   ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
41
42 public:
43   virtual std::error_code getRelocationAddend(DataRefImpl Rel,
44                                               int64_t &Res) const = 0;
45   virtual std::pair<symbol_iterator, symbol_iterator>
46   getELFDynamicSymbolIterators() const = 0;
47
48   virtual std::error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
49                                            bool &IsDefault) const = 0;
50
51   virtual uint64_t getSectionFlags(SectionRef Sec) const = 0;
52   virtual uint32_t getSectionType(SectionRef Sec) const = 0;
53
54   static inline bool classof(const Binary *v) { return v->isELF(); }
55 };
56
57 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
58 public:
59   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
60
61   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
62
63   typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
64   typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
65   typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
66   typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
67   typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
68   typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
69
70   typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
71   typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
72   typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
73
74 protected:
75   ELFFile<ELFT> EF;
76
77   void moveSymbolNext(DataRefImpl &Symb) const override;
78   std::error_code getSymbolName(DataRefImpl Symb,
79                                 StringRef &Res) const override;
80   std::error_code getSymbolAddress(DataRefImpl Symb,
81                                    uint64_t &Res) const override;
82   uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
83   uint64_t getSymbolSize(DataRefImpl Symb) const override;
84   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
85   std::error_code getSymbolOther(DataRefImpl Symb, uint8_t &Res) const override;
86   std::error_code getSymbolType(DataRefImpl Symb,
87                                 SymbolRef::Type &Res) const override;
88   section_iterator getSymbolSection(const Elf_Sym *Symb) const;
89   std::error_code getSymbolSection(DataRefImpl Symb,
90                                    section_iterator &Res) const override;
91
92   void moveSectionNext(DataRefImpl &Sec) const override;
93   std::error_code getSectionName(DataRefImpl Sec,
94                                  StringRef &Res) const override;
95   uint64_t getSectionAddress(DataRefImpl Sec) const override;
96   uint64_t getSectionSize(DataRefImpl Sec) const override;
97   std::error_code getSectionContents(DataRefImpl Sec,
98                                      StringRef &Res) const override;
99   uint64_t getSectionAlignment(DataRefImpl Sec) const override;
100   bool isSectionText(DataRefImpl Sec) const override;
101   bool isSectionData(DataRefImpl Sec) const override;
102   bool isSectionBSS(DataRefImpl Sec) const override;
103   bool isSectionVirtual(DataRefImpl Sec) const override;
104   bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
105   relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
106   relocation_iterator section_rel_end(DataRefImpl Sec) const override;
107   section_iterator getRelocatedSection(DataRefImpl Sec) const override;
108
109   void moveRelocationNext(DataRefImpl &Rel) const override;
110   std::error_code getRelocationAddress(DataRefImpl Rel,
111                                        uint64_t &Res) const override;
112   std::error_code getRelocationOffset(DataRefImpl Rel,
113                                       uint64_t &Res) const override;
114   symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
115   std::error_code getRelocationType(DataRefImpl Rel,
116                                     uint64_t &Res) const override;
117   std::error_code
118   getRelocationTypeName(DataRefImpl Rel,
119                         SmallVectorImpl<char> &Result) const override;
120
121   uint64_t getROffset(DataRefImpl Rel) const;
122   StringRef getRelocationTypeName(uint32_t Type) const;
123
124   /// \brief Get the relocation section that contains \a Rel.
125   const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
126     return EF.getSection(Rel.d.a);
127   }
128
129   const Elf_Rel *getRel(DataRefImpl Rel) const;
130   const Elf_Rela *getRela(DataRefImpl Rela) const;
131
132   Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
133     bool IsDynamic = Symb.p & 1;
134     if (IsDynamic)
135       return Elf_Sym_Iter(
136           EF.begin_dynamic_symbols().getEntSize(),
137           reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
138     return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
139                         reinterpret_cast<const char *>(Symb.p), IsDynamic);
140   }
141
142   DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
143     DataRefImpl DRI;
144     DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
145       static_cast<uintptr_t>(Symb.isDynamic());
146     return DRI;
147   }
148
149   Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
150     return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
151                          reinterpret_cast<const char *>(Sec.p));
152   }
153
154   DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
155     DataRefImpl DRI;
156     DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
157     return DRI;
158   }
159
160   DataRefImpl toDRI(const Elf_Shdr *Sec) const {
161     DataRefImpl DRI;
162     DRI.p = reinterpret_cast<uintptr_t>(Sec);
163     return DRI;
164   }
165
166   Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
167     return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
168                         reinterpret_cast<const char *>(Dyn.p));
169   }
170
171   DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
172     DataRefImpl DRI;
173     DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
174     return DRI;
175   }
176
177   bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
178     unsigned char Binding = ESym->getBinding();
179     unsigned char Visibility = ESym->getVisibility();
180
181     // A symbol is exported if its binding is either GLOBAL or WEAK, and its
182     // visibility is either DEFAULT or PROTECTED. All other symbols are not
183     // exported.
184     if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
185         (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
186       return true;
187
188     return false;
189   }
190
191   // This flag is used for classof, to distinguish ELFObjectFile from
192   // its subclass. If more subclasses will be created, this flag will
193   // have to become an enum.
194   bool isDyldELFObject;
195
196 public:
197   ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
198
199   const Elf_Sym *getSymbol(DataRefImpl Symb) const;
200
201   basic_symbol_iterator symbol_begin_impl() const override;
202   basic_symbol_iterator symbol_end_impl() const override;
203
204   symbol_iterator dynamic_symbol_begin() const;
205   symbol_iterator dynamic_symbol_end() const;
206
207   section_iterator section_begin() const override;
208   section_iterator section_end() const override;
209
210   std::error_code getRelocationAddend(DataRefImpl Rel,
211                                       int64_t &Res) const override;
212   std::error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
213                                    bool &IsDefault) const override;
214
215   uint64_t getSectionFlags(SectionRef Sec) const override;
216   uint32_t getSectionType(SectionRef Sec) const override;
217
218   uint8_t getBytesInAddress() const override;
219   StringRef getFileFormatName() const override;
220   unsigned getArch() const override;
221   StringRef getLoadName() const;
222
223   std::error_code getPlatformFlags(unsigned &Result) const override {
224     Result = EF.getHeader()->e_flags;
225     return std::error_code();
226   }
227
228   const ELFFile<ELFT> *getELFFile() const { return &EF; }
229
230   bool isDyldType() const { return isDyldELFObject; }
231   static inline bool classof(const Binary *v) {
232     return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
233                                       ELFT::Is64Bits);
234   }
235
236   std::pair<symbol_iterator, symbol_iterator>
237   getELFDynamicSymbolIterators() const override;
238
239   bool isRelocatableObject() const override;
240 };
241
242 typedef ELFObjectFile<ELFType<support::little, false>> ELF32LEObjectFile;
243 typedef ELFObjectFile<ELFType<support::little, true>> ELF64LEObjectFile;
244 typedef ELFObjectFile<ELFType<support::big, false>> ELF32BEObjectFile;
245 typedef ELFObjectFile<ELFType<support::big, true>> ELF64BEObjectFile;
246
247 template <class ELFT>
248 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
249   Symb = toDRI(++toELFSymIter(Symb));
250 }
251
252 template <class ELFT>
253 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
254                                                    StringRef &Result) const {
255   ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
256   if (!Name)
257     return Name.getError();
258   Result = *Name;
259   return std::error_code();
260 }
261
262 template <class ELFT>
263 std::error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
264                                                       StringRef &Version,
265                                                       bool &IsDefault) const {
266   DataRefImpl Symb = SymRef.getRawDataRefImpl();
267   const Elf_Sym *symb = getSymbol(Symb);
268   ErrorOr<StringRef> Ver =
269       EF.getSymbolVersion(EF.getSection(Symb.d.b), symb, IsDefault);
270   if (!Ver)
271     return Ver.getError();
272   Version = *Ver;
273   return std::error_code();
274 }
275
276 template <class ELFT>
277 uint64_t ELFObjectFile<ELFT>::getSectionFlags(SectionRef Sec) const {
278   DataRefImpl DRI = Sec.getRawDataRefImpl();
279   return toELFShdrIter(DRI)->sh_flags;
280 }
281
282 template <class ELFT>
283 uint32_t ELFObjectFile<ELFT>::getSectionType(SectionRef Sec) const {
284   DataRefImpl DRI = Sec.getRawDataRefImpl();
285   return toELFShdrIter(DRI)->sh_type;
286 }
287
288 template <class ELFT>
289 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
290                                                       uint64_t &Result) const {
291   const Elf_Sym *ESym = getSymbol(Symb);
292   switch (EF.getSymbolTableIndex(ESym)) {
293   case ELF::SHN_COMMON:
294   case ELF::SHN_UNDEF:
295     Result = UnknownAddressOrSize;
296     return std::error_code();
297   case ELF::SHN_ABS:
298     Result = ESym->st_value;
299     return std::error_code();
300   default:
301     break;
302   }
303
304   const Elf_Ehdr *Header = EF.getHeader();
305   Result = ESym->st_value;
306
307   // Clear the ARM/Thumb or microMIPS indicator flag.
308   if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
309       ESym->getType() == ELF::STT_FUNC)
310     Result &= ~1;
311
312   if (Header->e_type == ELF::ET_REL) {
313     const typename ELFFile<ELFT>::Elf_Shdr * Section = EF.getSection(ESym);
314     if (Section != nullptr)
315       Result += Section->sh_addr;
316   }
317
318   return std::error_code();
319 }
320
321 template <class ELFT>
322 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
323   Elf_Sym_Iter Sym = toELFSymIter(Symb);
324   if (Sym->st_shndx == ELF::SHN_COMMON)
325     return Sym->st_value;
326   return 0;
327 }
328
329 template <class ELFT>
330 uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb) const {
331   return toELFSymIter(Symb)->st_size;
332 }
333
334 template <class ELFT>
335 std::error_code ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb,
336                                                     uint8_t &Result) const {
337   Result = toELFSymIter(Symb)->st_other;
338   return std::error_code();
339 }
340
341 template <class ELFT>
342 std::error_code
343 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
344                                    SymbolRef::Type &Result) const {
345   const Elf_Sym *ESym = getSymbol(Symb);
346
347   switch (ESym->getType()) {
348   case ELF::STT_NOTYPE:
349     Result = SymbolRef::ST_Unknown;
350     break;
351   case ELF::STT_SECTION:
352     Result = SymbolRef::ST_Debug;
353     break;
354   case ELF::STT_FILE:
355     Result = SymbolRef::ST_File;
356     break;
357   case ELF::STT_FUNC:
358     Result = SymbolRef::ST_Function;
359     break;
360   case ELF::STT_OBJECT:
361   case ELF::STT_COMMON:
362   case ELF::STT_TLS:
363     Result = SymbolRef::ST_Data;
364     break;
365   default:
366     Result = SymbolRef::ST_Other;
367     break;
368   }
369   return std::error_code();
370 }
371
372 template <class ELFT>
373 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
374   Elf_Sym_Iter EIter = toELFSymIter(Symb);
375   const Elf_Sym *ESym = &*EIter;
376
377   uint32_t Result = SymbolRef::SF_None;
378
379   if (ESym->getBinding() != ELF::STB_LOCAL)
380     Result |= SymbolRef::SF_Global;
381
382   if (ESym->getBinding() == ELF::STB_WEAK)
383     Result |= SymbolRef::SF_Weak;
384
385   if (ESym->st_shndx == ELF::SHN_ABS)
386     Result |= SymbolRef::SF_Absolute;
387
388   if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
389       EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
390     Result |= SymbolRef::SF_FormatSpecific;
391
392   if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
393     Result |= SymbolRef::SF_Undefined;
394
395   if (ESym->getType() == ELF::STT_COMMON ||
396       EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
397     Result |= SymbolRef::SF_Common;
398
399   if (isExportedToOtherDSO(ESym))
400     Result |= SymbolRef::SF_Exported;
401
402   if (ESym->getVisibility() == ELF::STV_HIDDEN)
403     Result |= SymbolRef::SF_Hidden;
404
405   return Result;
406 }
407
408 template <class ELFT>
409 section_iterator
410 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
411   const Elf_Shdr *ESec = EF.getSection(ESym);
412   if (!ESec)
413     return section_end();
414   else {
415     DataRefImpl Sec;
416     Sec.p = reinterpret_cast<intptr_t>(ESec);
417     return section_iterator(SectionRef(Sec, this));
418   }
419 }
420
421 template <class ELFT>
422 std::error_code
423 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
424                                       section_iterator &Res) const {
425   Res = getSymbolSection(getSymbol(Symb));
426   return std::error_code();
427 }
428
429 template <class ELFT>
430 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
431   Sec = toDRI(++toELFShdrIter(Sec));
432 }
433
434 template <class ELFT>
435 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
436                                                     StringRef &Result) const {
437   ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
438   if (!Name)
439     return Name.getError();
440   Result = *Name;
441   return std::error_code();
442 }
443
444 template <class ELFT>
445 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
446   return toELFShdrIter(Sec)->sh_addr;
447 }
448
449 template <class ELFT>
450 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
451   return toELFShdrIter(Sec)->sh_size;
452 }
453
454 template <class ELFT>
455 std::error_code
456 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
457                                         StringRef &Result) const {
458   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
459   Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
460   return std::error_code();
461 }
462
463 template <class ELFT>
464 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
465   return toELFShdrIter(Sec)->sh_addralign;
466 }
467
468 template <class ELFT>
469 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
470   return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
471 }
472
473 template <class ELFT>
474 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
475   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
476   return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
477          EShdr->sh_type == ELF::SHT_PROGBITS;
478 }
479
480 template <class ELFT>
481 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
482   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
483   return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
484          EShdr->sh_type == ELF::SHT_NOBITS;
485 }
486
487 template <class ELFT>
488 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
489   return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
490 }
491
492 template <class ELFT>
493 bool ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
494                                                 DataRefImpl Symb) const {
495   Elf_Sym_Iter ESym = toELFSymIter(Symb);
496
497   uintX_t Index = ESym->st_shndx;
498   bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
499
500   return !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
501 }
502
503 template <class ELFT>
504 relocation_iterator
505 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
506   DataRefImpl RelData;
507   uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
508   RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
509   RelData.d.b = 0;
510   return relocation_iterator(RelocationRef(RelData, this));
511 }
512
513 template <class ELFT>
514 relocation_iterator
515 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
516   DataRefImpl RelData;
517   uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
518   const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
519   RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
520   if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
521     RelData.d.b = 0;
522   else
523     RelData.d.b = S->sh_size / S->sh_entsize;
524
525   return relocation_iterator(RelocationRef(RelData, this));
526 }
527
528 template <class ELFT>
529 section_iterator
530 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
531   if (EF.getHeader()->e_type != ELF::ET_REL)
532     return section_end();
533
534   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
535   uintX_t Type = EShdr->sh_type;
536   if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
537     return section_end();
538
539   const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
540   return section_iterator(SectionRef(toDRI(R), this));
541 }
542
543 // Relocations
544 template <class ELFT>
545 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
546   ++Rel.d.b;
547 }
548
549 template <class ELFT>
550 symbol_iterator
551 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
552   uint32_t symbolIdx;
553   const Elf_Shdr *sec = getRelSection(Rel);
554   switch (sec->sh_type) {
555   default:
556     report_fatal_error("Invalid section type in Rel!");
557   case ELF::SHT_REL: {
558     symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
559     break;
560   }
561   case ELF::SHT_RELA: {
562     symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
563     break;
564   }
565   }
566   if (!symbolIdx)
567     return symbol_end();
568
569   const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
570
571   DataRefImpl SymbolData;
572   switch (SymSec->sh_type) {
573   default:
574     report_fatal_error("Invalid symbol table section type!");
575   case ELF::SHT_SYMTAB:
576     SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
577     break;
578   case ELF::SHT_DYNSYM:
579     SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
580     break;
581   }
582
583   return symbol_iterator(SymbolRef(SymbolData, this));
584 }
585
586 template <class ELFT>
587 std::error_code
588 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
589                                           uint64_t &Result) const {
590   uint64_t ROffset = getROffset(Rel);
591   const Elf_Ehdr *Header = EF.getHeader();
592
593   if (Header->e_type == ELF::ET_REL) {
594     const Elf_Shdr *RelocationSec = getRelSection(Rel);
595     const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
596     Result = ROffset + RelocatedSec->sh_addr;
597   } else {
598     Result = ROffset;
599   }
600
601   return std::error_code();
602 }
603
604 template <class ELFT>
605 std::error_code
606 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
607                                          uint64_t &Result) const {
608   assert(EF.getHeader()->e_type == ELF::ET_REL &&
609          "Only relocatable object files have relocation offsets");
610   Result = getROffset(Rel);
611   return std::error_code();
612 }
613
614 template <class ELFT>
615 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
616   const Elf_Shdr *sec = getRelSection(Rel);
617   switch (sec->sh_type) {
618   default:
619     report_fatal_error("Invalid section type in Rel!");
620   case ELF::SHT_REL:
621     return getRel(Rel)->r_offset;
622   case ELF::SHT_RELA:
623     return getRela(Rel)->r_offset;
624   }
625 }
626
627 template <class ELFT>
628 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
629                                                        uint64_t &Result) const {
630   const Elf_Shdr *sec = getRelSection(Rel);
631   switch (sec->sh_type) {
632   default:
633     report_fatal_error("Invalid section type in Rel!");
634   case ELF::SHT_REL: {
635     Result = getRel(Rel)->getType(EF.isMips64EL());
636     break;
637   }
638   case ELF::SHT_RELA: {
639     Result = getRela(Rel)->getType(EF.isMips64EL());
640     break;
641   }
642   }
643   return std::error_code();
644 }
645
646 template <class ELFT>
647 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
648   return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
649 }
650
651 template <class ELFT>
652 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
653     DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
654   const Elf_Shdr *sec = getRelSection(Rel);
655   uint32_t type;
656   switch (sec->sh_type) {
657   default:
658     return object_error::parse_failed;
659   case ELF::SHT_REL: {
660     type = getRel(Rel)->getType(EF.isMips64EL());
661     break;
662   }
663   case ELF::SHT_RELA: {
664     type = getRela(Rel)->getType(EF.isMips64EL());
665     break;
666   }
667   }
668
669   EF.getRelocationTypeName(type, Result);
670   return std::error_code();
671 }
672
673 template <class ELFT>
674 std::error_code
675 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
676                                          int64_t &Result) const {
677   const Elf_Shdr *sec = getRelSection(Rel);
678   switch (sec->sh_type) {
679   default:
680     report_fatal_error("Invalid section type in Rel!");
681   case ELF::SHT_REL: {
682     Result = 0;
683     return std::error_code();
684   }
685   case ELF::SHT_RELA: {
686     Result = getRela(Rel)->r_addend;
687     return std::error_code();
688   }
689   }
690 }
691
692 template <class ELFT>
693 const typename ELFFile<ELFT>::Elf_Sym *
694 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
695   return &*toELFSymIter(Symb);
696 }
697
698 template <class ELFT>
699 const typename ELFObjectFile<ELFT>::Elf_Rel *
700 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
701   return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
702 }
703
704 template <class ELFT>
705 const typename ELFObjectFile<ELFT>::Elf_Rela *
706 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
707   return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
708 }
709
710 template <class ELFT>
711 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
712     : ELFObjectFileBase(
713           getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
714                          support::little,
715                      ELFT::Is64Bits),
716           Object),
717       EF(Data.getBuffer(), EC) {}
718
719 template <class ELFT>
720 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
721   return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
722 }
723
724 template <class ELFT>
725 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
726   return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
727 }
728
729 template <class ELFT>
730 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
731   return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
732 }
733
734 template <class ELFT>
735 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
736   return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
737 }
738
739 template <class ELFT>
740 section_iterator ELFObjectFile<ELFT>::section_begin() const {
741   return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
742 }
743
744 template <class ELFT>
745 section_iterator ELFObjectFile<ELFT>::section_end() const {
746   return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
747 }
748
749 template <class ELFT>
750 StringRef ELFObjectFile<ELFT>::getLoadName() const {
751   Elf_Dyn_Iter DI = EF.begin_dynamic_table();
752   Elf_Dyn_Iter DE = EF.end_dynamic_table();
753
754   while (DI != DE && DI->getTag() != ELF::DT_SONAME)
755     ++DI;
756
757   if (DI != DE)
758     return EF.getDynamicString(DI->getVal());
759   return "";
760 }
761
762 template <class ELFT>
763 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
764   return ELFT::Is64Bits ? 8 : 4;
765 }
766
767 template <class ELFT>
768 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
769   bool IsLittleEndian = ELFT::TargetEndianness == support::little;
770   switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
771   case ELF::ELFCLASS32:
772     switch (EF.getHeader()->e_machine) {
773     case ELF::EM_386:
774       return "ELF32-i386";
775     case ELF::EM_X86_64:
776       return "ELF32-x86-64";
777     case ELF::EM_ARM:
778       return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
779     case ELF::EM_HEXAGON:
780       return "ELF32-hexagon";
781     case ELF::EM_MIPS:
782       return "ELF32-mips";
783     case ELF::EM_PPC:
784       return "ELF32-ppc";
785     case ELF::EM_SPARC:
786     case ELF::EM_SPARC32PLUS:
787       return "ELF32-sparc";
788     default:
789       return "ELF32-unknown";
790     }
791   case ELF::ELFCLASS64:
792     switch (EF.getHeader()->e_machine) {
793     case ELF::EM_386:
794       return "ELF64-i386";
795     case ELF::EM_X86_64:
796       return "ELF64-x86-64";
797     case ELF::EM_AARCH64:
798       return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
799     case ELF::EM_PPC64:
800       return "ELF64-ppc64";
801     case ELF::EM_S390:
802       return "ELF64-s390";
803     case ELF::EM_SPARCV9:
804       return "ELF64-sparc";
805     case ELF::EM_MIPS:
806       return "ELF64-mips";
807     default:
808       return "ELF64-unknown";
809     }
810   default:
811     // FIXME: Proper error handling.
812     report_fatal_error("Invalid ELFCLASS!");
813   }
814 }
815
816 template <class ELFT>
817 unsigned ELFObjectFile<ELFT>::getArch() const {
818   bool IsLittleEndian = ELFT::TargetEndianness == support::little;
819   switch (EF.getHeader()->e_machine) {
820   case ELF::EM_386:
821     return Triple::x86;
822   case ELF::EM_X86_64:
823     return Triple::x86_64;
824   case ELF::EM_AARCH64:
825     return Triple::aarch64;
826   case ELF::EM_ARM:
827     return Triple::arm;
828   case ELF::EM_HEXAGON:
829     return Triple::hexagon;
830   case ELF::EM_MIPS:
831     switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
832     case ELF::ELFCLASS32:
833       return IsLittleEndian ? Triple::mipsel : Triple::mips;
834     case ELF::ELFCLASS64:
835       return IsLittleEndian ? Triple::mips64el : Triple::mips64;
836     default:
837       report_fatal_error("Invalid ELFCLASS!");
838     }
839   case ELF::EM_PPC:
840     return Triple::ppc;
841   case ELF::EM_PPC64:
842     return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
843   case ELF::EM_S390:
844     return Triple::systemz;
845
846   case ELF::EM_SPARC:
847   case ELF::EM_SPARC32PLUS:
848     return IsLittleEndian ? Triple::sparcel : Triple::sparc;
849   case ELF::EM_SPARCV9:
850     return Triple::sparcv9;
851
852   default:
853     return Triple::UnknownArch;
854   }
855 }
856
857 template <class ELFT>
858 std::pair<symbol_iterator, symbol_iterator>
859 ELFObjectFile<ELFT>::getELFDynamicSymbolIterators() const {
860   return std::make_pair(dynamic_symbol_begin(), dynamic_symbol_end());
861 }
862
863 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
864   return EF.getHeader()->e_type == ELF::ET_REL;
865 }
866
867 inline std::error_code getELFRelocationAddend(const RelocationRef R,
868                                               int64_t &Addend) {
869   const ObjectFile *Obj = R.getObjectFile();
870   DataRefImpl DRI = R.getRawDataRefImpl();
871   return cast<ELFObjectFileBase>(Obj)->getRelocationAddend(DRI, Addend);
872 }
873
874 inline std::pair<symbol_iterator, symbol_iterator>
875 getELFDynamicSymbolIterators(const SymbolicFile *Obj) {
876   return cast<ELFObjectFileBase>(Obj)->getELFDynamicSymbolIterators();
877 }
878
879 inline std::error_code GetELFSymbolVersion(const ObjectFile *Obj,
880                                            const SymbolRef &Sym,
881                                            StringRef &Version,
882                                            bool &IsDefault) {
883   return cast<ELFObjectFileBase>(Obj)
884       ->getSymbolVersion(Sym, Version, IsDefault);
885 }
886 } // namespace object
887 } // namespace llvm
888
889 #endif