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