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