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