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