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