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