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