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