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