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