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