Add back an assert that was lost in the ELFObjectFile.h split.
[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_ELF_OBJECT_FILE_H
15 #define LLVM_OBJECT_ELF_OBJECT_FILE_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 template <class ELFT>
39 class ELFObjectFile : public ObjectFile {
40 public:
41   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
42
43   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
44
45   typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
46   typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
47   typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
48   typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
49   typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
50
51   typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
52   typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
53   typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
54
55 protected:
56   ELFFile<ELFT> EF;
57
58   void moveSymbolNext(DataRefImpl &Symb) const override;
59   error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const override;
60   error_code getSymbolFileOffset(DataRefImpl Symb,
61                                  uint64_t &Res) const override;
62   error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const override;
63   error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const override;
64   error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
65   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
66   error_code getSymbolType(DataRefImpl Symb,
67                            SymbolRef::Type &Res) const override;
68   error_code getSymbolSection(DataRefImpl Symb,
69                               section_iterator &Res) const override;
70   error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const override;
71
72   error_code getLibraryNext(DataRefImpl Data,
73                             LibraryRef &Result) const override;
74   error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const override;
75
76   void moveSectionNext(DataRefImpl &Sec) const override;
77   error_code getSectionName(DataRefImpl Sec, StringRef &Res) const override;
78   error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const override;
79   error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const override;
80   error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const override;
81   error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const override;
82   error_code isSectionText(DataRefImpl Sec, bool &Res) const override;
83   error_code isSectionData(DataRefImpl Sec, bool &Res) const override;
84   error_code isSectionBSS(DataRefImpl Sec, bool &Res) const override;
85   error_code isSectionRequiredForExecution(DataRefImpl Sec,
86                                            bool &Res) const override;
87   error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const override;
88   error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const override;
89   error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const override;
90   error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
91                                    bool &Result) const override;
92   relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
93   relocation_iterator section_rel_end(DataRefImpl Sec) const override;
94   bool section_rel_empty(DataRefImpl Sec) const override;
95   section_iterator getRelocatedSection(DataRefImpl Sec) const override;
96
97   void moveRelocationNext(DataRefImpl &Rel) const override;
98   error_code getRelocationAddress(DataRefImpl Rel,
99                                   uint64_t &Res) const override;
100   error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const override;
101   symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
102   error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const override;
103   error_code getRelocationTypeName(DataRefImpl Rel,
104                                   SmallVectorImpl<char> &Result) const override;
105   error_code getRelocationValueString(DataRefImpl Rel,
106                                   SmallVectorImpl<char> &Result) const override;
107
108   uint64_t getROffset(DataRefImpl Rel) const;
109   StringRef getRelocationTypeName(uint32_t Type) const;
110
111   /// \brief Get the relocation section that contains \a Rel.
112   const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
113     return EF.getSection(Rel.d.a);
114   }
115
116   const Elf_Rel *getRel(DataRefImpl Rel) const;
117   const Elf_Rela *getRela(DataRefImpl Rela) const;
118
119   Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
120     bool IsDynamic = Symb.p & 1;
121     if (IsDynamic)
122       return Elf_Sym_Iter(
123           EF.begin_dynamic_symbols().getEntSize(),
124           reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
125     return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
126                         reinterpret_cast<const char *>(Symb.p), IsDynamic);
127   }
128
129   DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
130     DataRefImpl DRI;
131     DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
132       static_cast<uintptr_t>(Symb.isDynamic());
133     return DRI;
134   }
135
136   Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
137     return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
138                          reinterpret_cast<const char *>(Sec.p));
139   }
140
141   DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
142     DataRefImpl DRI;
143     DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
144     return DRI;
145   }
146
147   DataRefImpl toDRI(const Elf_Shdr *Sec) const {
148     DataRefImpl DRI;
149     DRI.p = reinterpret_cast<uintptr_t>(Sec);
150     return DRI;
151   }
152
153   Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
154     return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
155                         reinterpret_cast<const char *>(Dyn.p));
156   }
157
158   DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
159     DataRefImpl DRI;
160     DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
161     return DRI;
162   }
163
164   // This flag is used for classof, to distinguish ELFObjectFile from
165   // its subclass. If more subclasses will be created, this flag will
166   // have to become an enum.
167   bool isDyldELFObject;
168
169 public:
170   ELFObjectFile(MemoryBuffer *Object, error_code &EC, bool BufferOwned = true);
171
172   const Elf_Sym *getSymbol(DataRefImpl Symb) const;
173
174   basic_symbol_iterator symbol_begin_impl() const override;
175   basic_symbol_iterator symbol_end_impl() const override;
176
177   symbol_iterator dynamic_symbol_begin() const;
178   symbol_iterator dynamic_symbol_end() const;
179
180   section_iterator section_begin() const override;
181   section_iterator section_end() const override;
182
183   library_iterator needed_library_begin() const override;
184   library_iterator needed_library_end() const override;
185
186   error_code getRelocationAddend(DataRefImpl Rel, int64_t &Res) const;
187   error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
188                               bool &IsDefault) const;
189
190
191   uint8_t getBytesInAddress() const override;
192   StringRef getFileFormatName() const override;
193   unsigned getArch() const override;
194   StringRef getLoadName() const override;
195
196   const ELFFile<ELFT> *getELFFile() const { return &EF; }
197
198   bool isDyldType() const { return isDyldELFObject; }
199   static inline bool classof(const Binary *v) {
200     return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
201                                       ELFT::Is64Bits);
202   }
203 };
204
205 // Use an alignment of 2 for the typedefs since that is the worst case for
206 // ELF files in archives.
207 typedef ELFObjectFile<ELFType<support::little, 2, false> > ELF32LEObjectFile;
208 typedef ELFObjectFile<ELFType<support::little, 2, true> > ELF64LEObjectFile;
209 typedef ELFObjectFile<ELFType<support::big, 2, false> > ELF32BEObjectFile;
210 typedef ELFObjectFile<ELFType<support::big, 2, true> > ELF64BEObjectFile;
211
212 template <class ELFT>
213 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
214   Symb = toDRI(++toELFSymIter(Symb));
215 }
216
217 template <class ELFT>
218 error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
219                                               StringRef &Result) const {
220   ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
221   if (!Name)
222     return Name.getError();
223   Result = *Name;
224   return object_error::success;
225 }
226
227 template <class ELFT>
228 error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
229                                                  StringRef &Version,
230                                                  bool &IsDefault) const {
231   DataRefImpl Symb = SymRef.getRawDataRefImpl();
232   const Elf_Sym *symb = getSymbol(Symb);
233   ErrorOr<StringRef> Ver =
234       EF.getSymbolVersion(EF.getSection(Symb.d.b), symb, IsDefault);
235   if (!Ver)
236     return Ver.getError();
237   Version = *Ver;
238   return object_error::success;
239 }
240
241 template <class ELFT>
242 error_code ELFObjectFile<ELFT>::getSymbolFileOffset(DataRefImpl Symb,
243                                                     uint64_t &Result) const {
244   const Elf_Sym *ESym = getSymbol(Symb);
245   const Elf_Shdr *ESec;
246   switch (EF.getSymbolTableIndex(ESym)) {
247   case ELF::SHN_COMMON:
248   // Unintialized symbols have no offset in the object file
249   case ELF::SHN_UNDEF:
250     Result = UnknownAddressOrSize;
251     return object_error::success;
252   case ELF::SHN_ABS:
253     Result = ESym->st_value;
254     return object_error::success;
255   default:
256     ESec = EF.getSection(ESym);
257   }
258
259   switch (ESym->getType()) {
260   case ELF::STT_SECTION:
261     Result = ESec ? ESec->sh_offset : UnknownAddressOrSize;
262     return object_error::success;
263   case ELF::STT_FUNC:
264   case ELF::STT_OBJECT:
265   case ELF::STT_NOTYPE:
266     Result = ESym->st_value + (ESec ? ESec->sh_offset : 0);
267     return object_error::success;
268   default:
269     Result = UnknownAddressOrSize;
270     return object_error::success;
271   }
272 }
273
274 template <class ELFT>
275 error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
276                                                  uint64_t &Result) const {
277   const Elf_Sym *ESym = getSymbol(Symb);
278   const Elf_Shdr *ESec;
279   switch (EF.getSymbolTableIndex(ESym)) {
280   case ELF::SHN_COMMON:
281   case ELF::SHN_UNDEF:
282     Result = UnknownAddressOrSize;
283     return object_error::success;
284   case ELF::SHN_ABS:
285     Result = ESym->st_value;
286     return object_error::success;
287   default:
288     ESec = EF.getSection(ESym);
289   }
290
291   switch (ESym->getType()) {
292   case ELF::STT_SECTION:
293     Result = ESec ? ESec->sh_addr : UnknownAddressOrSize;
294     return object_error::success;
295   case ELF::STT_FUNC:
296   case ELF::STT_OBJECT:
297   case ELF::STT_NOTYPE:
298   case ELF::STT_TLS:
299     bool IsRelocatable;
300     switch (EF.getHeader()->e_type) {
301     case ELF::ET_EXEC:
302     case ELF::ET_DYN:
303       IsRelocatable = false;
304       break;
305     default:
306       IsRelocatable = true;
307     }
308     Result = ESym->st_value;
309
310     // Clear the ARM/Thumb indicator flag.
311     if (EF.getHeader()->e_machine == ELF::EM_ARM)
312       Result &= ~1;
313
314     if (IsRelocatable && ESec != 0)
315       Result += ESec->sh_addr;
316     return object_error::success;
317   default:
318     Result = UnknownAddressOrSize;
319     return object_error::success;
320   }
321 }
322
323 template <class ELFT>
324 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 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 error_code ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
343                                               SymbolRef::Type &Result) const {
344   const Elf_Sym *ESym = getSymbol(Symb);
345
346   switch (ESym->getType()) {
347   case ELF::STT_NOTYPE:
348     Result = SymbolRef::ST_Unknown;
349     break;
350   case ELF::STT_SECTION:
351     Result = SymbolRef::ST_Debug;
352     break;
353   case ELF::STT_FILE:
354     Result = SymbolRef::ST_File;
355     break;
356   case ELF::STT_FUNC:
357     Result = SymbolRef::ST_Function;
358     break;
359   case ELF::STT_OBJECT:
360   case ELF::STT_COMMON:
361   case ELF::STT_TLS:
362     Result = SymbolRef::ST_Data;
363     break;
364   default:
365     Result = SymbolRef::ST_Other;
366     break;
367   }
368   return object_error::success;
369 }
370
371 template <class ELFT>
372 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
373   Elf_Sym_Iter EIter = toELFSymIter(Symb);
374   const Elf_Sym *ESym = &*EIter;
375
376   uint32_t Result = SymbolRef::SF_None;
377
378   if (ESym->getBinding() != ELF::STB_LOCAL)
379     Result |= SymbolRef::SF_Global;
380
381   if (ESym->getBinding() == ELF::STB_WEAK)
382     Result |= SymbolRef::SF_Weak;
383
384   if (ESym->st_shndx == ELF::SHN_ABS)
385     Result |= SymbolRef::SF_Absolute;
386
387   if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
388       EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
389     Result |= SymbolRef::SF_FormatSpecific;
390
391   if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
392     Result |= SymbolRef::SF_Undefined;
393
394   if (ESym->getType() == ELF::STT_COMMON ||
395       EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
396     Result |= SymbolRef::SF_Common;
397
398   return Result;
399 }
400
401 template <class ELFT>
402 error_code ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
403                                                  section_iterator &Res) const {
404   const Elf_Sym *ESym = getSymbol(Symb);
405   const Elf_Shdr *ESec = EF.getSection(ESym);
406   if (!ESec)
407     Res = section_end();
408   else {
409     DataRefImpl Sec;
410     Sec.p = reinterpret_cast<intptr_t>(ESec);
411     Res = section_iterator(SectionRef(Sec, this));
412   }
413   return object_error::success;
414 }
415
416 template <class ELFT>
417 error_code ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb,
418                                                uint64_t &Val) const {
419   const Elf_Sym *ESym = getSymbol(Symb);
420   Val = ESym->st_value;
421   return object_error::success;
422 }
423
424 template <class ELFT>
425 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
426   Sec = toDRI(++toELFShdrIter(Sec));
427 }
428
429 template <class ELFT>
430 error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
431                                                StringRef &Result) const {
432   ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
433   if (!Name)
434     return Name.getError();
435   Result = *Name;
436   return object_error::success;
437 }
438
439 template <class ELFT>
440 error_code ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec,
441                                                   uint64_t &Result) const {
442   Result = toELFShdrIter(Sec)->sh_addr;
443   return object_error::success;
444 }
445
446 template <class ELFT>
447 error_code ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec,
448                                                uint64_t &Result) const {
449   Result = toELFShdrIter(Sec)->sh_size;
450   return object_error::success;
451 }
452
453 template <class ELFT>
454 error_code ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
455                                                    StringRef &Result) const {
456   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
457   Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
458   return object_error::success;
459 }
460
461 template <class ELFT>
462 error_code ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec,
463                                                     uint64_t &Result) const {
464   Result = toELFShdrIter(Sec)->sh_addralign;
465   return object_error::success;
466 }
467
468 template <class ELFT>
469 error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec,
470                                               bool &Result) const {
471   Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
472   return object_error::success;
473 }
474
475 template <class ELFT>
476 error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec,
477                                               bool &Result) const {
478   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
479   Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
480            EShdr->sh_type == ELF::SHT_PROGBITS;
481   return object_error::success;
482 }
483
484 template <class ELFT>
485 error_code ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec,
486                                              bool &Result) const {
487   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
488   Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
489            EShdr->sh_type == ELF::SHT_NOBITS;
490   return object_error::success;
491 }
492
493 template <class ELFT>
494 error_code
495 ELFObjectFile<ELFT>::isSectionRequiredForExecution(DataRefImpl Sec,
496                                                    bool &Result) const {
497   Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC;
498   return object_error::success;
499 }
500
501 template <class ELFT>
502 error_code ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec,
503                                                  bool &Result) const {
504   Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
505   return object_error::success;
506 }
507
508 template <class ELFT>
509 error_code ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec,
510                                                   bool &Result) const {
511   Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
512   return object_error::success;
513 }
514
515 template <class ELFT>
516 error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec,
517                                                       bool &Result) const {
518   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
519   Result = !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
520   return object_error::success;
521 }
522
523 template <class ELFT>
524 error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
525                                                       DataRefImpl Symb,
526                                                       bool &Result) const {
527   Elf_Sym_Iter ESym = toELFSymIter(Symb);
528
529   uintX_t Index = ESym->st_shndx;
530   bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
531
532   Result = !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
533   return object_error::success;
534 }
535
536 template <class ELFT>
537 relocation_iterator
538 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
539   DataRefImpl RelData;
540   uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
541   RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
542   RelData.d.b = 0;
543   return relocation_iterator(RelocationRef(RelData, this));
544 }
545
546 template <class ELFT>
547 relocation_iterator
548 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
549   DataRefImpl RelData;
550   uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
551   const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
552   RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
553   if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
554     RelData.d.b = 0;
555   else
556     RelData.d.b = S->sh_size / S->sh_entsize;
557
558   return relocation_iterator(RelocationRef(RelData, this));
559 }
560
561 template <class ELFT>
562 bool ELFObjectFile<ELFT>::section_rel_empty(DataRefImpl Sec) const {
563   const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
564   return S->sh_size == 0;
565 }
566
567 template <class ELFT>
568 section_iterator
569 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
570   if (EF.getHeader()->e_type != ELF::ET_REL)
571     return section_end();
572
573   Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
574   uintX_t Type = EShdr->sh_type;
575   if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
576     return section_end();
577
578   const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
579   return section_iterator(SectionRef(toDRI(R), this));
580 }
581
582 // Relocations
583 template <class ELFT>
584 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
585   ++Rel.d.b;
586 }
587
588 template <class ELFT>
589 symbol_iterator
590 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
591   uint32_t symbolIdx;
592   const Elf_Shdr *sec = getRelSection(Rel);
593   switch (sec->sh_type) {
594   default:
595     report_fatal_error("Invalid section type in Rel!");
596   case ELF::SHT_REL: {
597     symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
598     break;
599   }
600   case ELF::SHT_RELA: {
601     symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
602     break;
603   }
604   }
605   if (!symbolIdx)
606     return symbol_end();
607
608   const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
609
610   DataRefImpl SymbolData;
611   switch (SymSec->sh_type) {
612   default:
613     report_fatal_error("Invalid symbol table section type!");
614   case ELF::SHT_SYMTAB:
615     SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
616     break;
617   case ELF::SHT_DYNSYM:
618     SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
619     break;
620   }
621
622   return symbol_iterator(SymbolRef(SymbolData, this));
623 }
624
625 template <class ELFT>
626 error_code ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
627                                                      uint64_t &Result) const {
628   assert((EF.getHeader()->e_type == ELF::ET_EXEC ||
629           EF.getHeader()->e_type == ELF::ET_DYN) &&
630          "Only executable and shared objects files have relocation addresses");
631   Result = getROffset(Rel);
632   return object_error::success;
633 }
634
635 template <class ELFT>
636 error_code ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
637                                                     uint64_t &Result) const {
638   assert(EF.getHeader()->e_type == ELF::ET_REL &&
639          "Only relocatable object files have relocation offsets");
640   Result = getROffset(Rel);
641   return object_error::success;
642 }
643
644 template <class ELFT>
645 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
646   const Elf_Shdr *sec = getRelSection(Rel);
647   switch (sec->sh_type) {
648   default:
649     report_fatal_error("Invalid section type in Rel!");
650   case ELF::SHT_REL:
651     return getRel(Rel)->r_offset;
652   case ELF::SHT_RELA:
653     return getRela(Rel)->r_offset;
654   }
655 }
656
657 template <class ELFT>
658 error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
659                                                   uint64_t &Result) const {
660   const Elf_Shdr *sec = getRelSection(Rel);
661   switch (sec->sh_type) {
662   default:
663     report_fatal_error("Invalid section type in Rel!");
664   case ELF::SHT_REL: {
665     Result = getRel(Rel)->getType(EF.isMips64EL());
666     break;
667   }
668   case ELF::SHT_RELA: {
669     Result = getRela(Rel)->getType(EF.isMips64EL());
670     break;
671   }
672   }
673   return object_error::success;
674 }
675
676 template <class ELFT>
677 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
678   return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
679 }
680
681 template <class ELFT>
682 error_code ELFObjectFile<ELFT>::getRelocationTypeName(
683     DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
684   const Elf_Shdr *sec = getRelSection(Rel);
685   uint32_t type;
686   switch (sec->sh_type) {
687   default:
688     return object_error::parse_failed;
689   case ELF::SHT_REL: {
690     type = getRel(Rel)->getType(EF.isMips64EL());
691     break;
692   }
693   case ELF::SHT_RELA: {
694     type = getRela(Rel)->getType(EF.isMips64EL());
695     break;
696   }
697   }
698
699   EF.getRelocationTypeName(type, Result);
700   return object_error::success;
701 }
702
703 template <class ELFT>
704 error_code ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
705                                                     int64_t &Result) const {
706   const Elf_Shdr *sec = getRelSection(Rel);
707   switch (sec->sh_type) {
708   default:
709     report_fatal_error("Invalid section type in Rel!");
710   case ELF::SHT_REL: {
711     Result = 0;
712     return object_error::success;
713   }
714   case ELF::SHT_RELA: {
715     Result = getRela(Rel)->r_addend;
716     return object_error::success;
717   }
718   }
719 }
720
721 template <class ELFT>
722 error_code ELFObjectFile<ELFT>::getRelocationValueString(
723     DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
724   const Elf_Shdr *sec = getRelSection(Rel);
725   uint8_t type;
726   StringRef res;
727   int64_t addend = 0;
728   uint16_t symbol_index = 0;
729   switch (sec->sh_type) {
730   default:
731     return object_error::parse_failed;
732   case ELF::SHT_REL: {
733     type = getRel(Rel)->getType(EF.isMips64EL());
734     symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
735     // TODO: Read implicit addend from section data.
736     break;
737   }
738   case ELF::SHT_RELA: {
739     type = getRela(Rel)->getType(EF.isMips64EL());
740     symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
741     addend = getRela(Rel)->r_addend;
742     break;
743   }
744   }
745   const Elf_Sym *symb =
746       EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
747   ErrorOr<StringRef> SymName =
748       EF.getSymbolName(EF.getSection(sec->sh_link), symb);
749   if (!SymName)
750     return SymName.getError();
751   switch (EF.getHeader()->e_machine) {
752   case ELF::EM_X86_64:
753     switch (type) {
754     case ELF::R_X86_64_PC8:
755     case ELF::R_X86_64_PC16:
756     case ELF::R_X86_64_PC32: {
757       std::string fmtbuf;
758       raw_string_ostream fmt(fmtbuf);
759       fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P";
760       fmt.flush();
761       Result.append(fmtbuf.begin(), fmtbuf.end());
762     } break;
763     case ELF::R_X86_64_8:
764     case ELF::R_X86_64_16:
765     case ELF::R_X86_64_32:
766     case ELF::R_X86_64_32S:
767     case ELF::R_X86_64_64: {
768       std::string fmtbuf;
769       raw_string_ostream fmt(fmtbuf);
770       fmt << *SymName << (addend < 0 ? "" : "+") << addend;
771       fmt.flush();
772       Result.append(fmtbuf.begin(), fmtbuf.end());
773     } break;
774     default:
775       res = "Unknown";
776     }
777     break;
778   case ELF::EM_AARCH64: {
779     std::string fmtbuf;
780     raw_string_ostream fmt(fmtbuf);
781     fmt << *SymName;
782     if (addend != 0)
783       fmt << (addend < 0 ? "" : "+") << addend;
784     fmt.flush();
785     Result.append(fmtbuf.begin(), fmtbuf.end());
786     break;
787   }
788   case ELF::EM_ARM:
789   case ELF::EM_HEXAGON:
790   case ELF::EM_MIPS:
791     res = *SymName;
792     break;
793   default:
794     res = "Unknown";
795   }
796   if (Result.empty())
797     Result.append(res.begin(), res.end());
798   return object_error::success;
799 }
800
801 template <class ELFT>
802 const typename ELFFile<ELFT>::Elf_Sym *
803 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
804   return &*toELFSymIter(Symb);
805 }
806
807 template <class ELFT>
808 const typename ELFObjectFile<ELFT>::Elf_Rel *
809 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
810   return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
811 }
812
813 template <class ELFT>
814 const typename ELFObjectFile<ELFT>::Elf_Rela *
815 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
816   return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
817 }
818
819 template <class ELFT>
820 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBuffer *Object, error_code &ec,
821                                    bool BufferOwned)
822     : ObjectFile(getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
823                                 support::little,
824                             ELFT::Is64Bits),
825                  Object, BufferOwned),
826       EF(Object, ec) {}
827
828 template <class ELFT>
829 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
830   return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
831 }
832
833 template <class ELFT>
834 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
835   return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
836 }
837
838 template <class ELFT>
839 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
840   return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
841 }
842
843 template <class ELFT>
844 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
845   return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
846 }
847
848 template <class ELFT>
849 section_iterator ELFObjectFile<ELFT>::section_begin() const {
850   return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
851 }
852
853 template <class ELFT>
854 section_iterator ELFObjectFile<ELFT>::section_end() const {
855   return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
856 }
857
858 template <class ELFT>
859 StringRef ELFObjectFile<ELFT>::getLoadName() const {
860   Elf_Dyn_Iter DI = EF.begin_dynamic_table();
861   Elf_Dyn_Iter DE = EF.end_dynamic_table();
862
863   while (DI != DE && DI->getTag() != ELF::DT_SONAME)
864     ++DI;
865
866   if (DI != DE)
867     return EF.getDynamicString(DI->getVal());
868   return "";
869 }
870
871 template <class ELFT>
872 library_iterator ELFObjectFile<ELFT>::needed_library_begin() const {
873   Elf_Dyn_Iter DI = EF.begin_dynamic_table();
874   Elf_Dyn_Iter DE = EF.end_dynamic_table();
875
876   while (DI != DE && DI->getTag() != ELF::DT_SONAME)
877     ++DI;
878
879   return library_iterator(LibraryRef(toDRI(DI), this));
880 }
881
882 template <class ELFT>
883 error_code ELFObjectFile<ELFT>::getLibraryNext(DataRefImpl Data,
884                                                LibraryRef &Result) const {
885   Elf_Dyn_Iter DI = toELFDynIter(Data);
886   Elf_Dyn_Iter DE = EF.end_dynamic_table();
887
888   // Skip to the next DT_NEEDED entry.
889   do
890     ++DI;
891   while (DI != DE && DI->getTag() != ELF::DT_NEEDED);
892
893   Result = LibraryRef(toDRI(DI), this);
894   return object_error::success;
895 }
896
897 template <class ELFT>
898 error_code ELFObjectFile<ELFT>::getLibraryPath(DataRefImpl Data,
899                                                StringRef &Res) const {
900   Res = EF.getDynamicString(toELFDynIter(Data)->getVal());
901   return object_error::success;
902 }
903
904 template <class ELFT>
905 library_iterator ELFObjectFile<ELFT>::needed_library_end() const {
906   return library_iterator(LibraryRef(toDRI(EF.end_dynamic_table()), this));
907 }
908
909 template <class ELFT>
910 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
911   return ELFT::Is64Bits ? 8 : 4;
912 }
913
914 template <class ELFT>
915 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
916   switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
917   case ELF::ELFCLASS32:
918     switch (EF.getHeader()->e_machine) {
919     case ELF::EM_386:
920       return "ELF32-i386";
921     case ELF::EM_X86_64:
922       return "ELF32-x86-64";
923     case ELF::EM_ARM:
924       return "ELF32-arm";
925     case ELF::EM_HEXAGON:
926       return "ELF32-hexagon";
927     case ELF::EM_MIPS:
928       return "ELF32-mips";
929     case ELF::EM_PPC:
930       return "ELF32-ppc";
931     case ELF::EM_SPARC:
932     case ELF::EM_SPARC32PLUS:
933       return "ELF32-sparc";
934     default:
935       return "ELF32-unknown";
936     }
937   case ELF::ELFCLASS64:
938     switch (EF.getHeader()->e_machine) {
939     case ELF::EM_386:
940       return "ELF64-i386";
941     case ELF::EM_X86_64:
942       return "ELF64-x86-64";
943     case ELF::EM_AARCH64:
944       return "ELF64-aarch64";
945     case ELF::EM_PPC64:
946       return "ELF64-ppc64";
947     case ELF::EM_S390:
948       return "ELF64-s390";
949     case ELF::EM_SPARCV9:
950       return "ELF64-sparc";
951     case ELF::EM_MIPS:
952       return "ELF64-mips";
953     default:
954       return "ELF64-unknown";
955     }
956   default:
957     // FIXME: Proper error handling.
958     report_fatal_error("Invalid ELFCLASS!");
959   }
960 }
961
962 template <class ELFT>
963 unsigned ELFObjectFile<ELFT>::getArch() const {
964   switch (EF.getHeader()->e_machine) {
965   case ELF::EM_386:
966     return Triple::x86;
967   case ELF::EM_X86_64:
968     return Triple::x86_64;
969   case ELF::EM_AARCH64:
970     return Triple::aarch64;
971   case ELF::EM_ARM:
972     return Triple::arm;
973   case ELF::EM_HEXAGON:
974     return Triple::hexagon;
975   case ELF::EM_MIPS:
976     return (ELFT::TargetEndianness == support::little) ? Triple::mipsel
977                                                        : Triple::mips;
978   case ELF::EM_PPC64:
979     return (ELFT::TargetEndianness == support::little) ? Triple::ppc64le
980                                                        : Triple::ppc64;
981   case ELF::EM_S390:
982     return Triple::systemz;
983
984   case ELF::EM_SPARC:
985   case ELF::EM_SPARC32PLUS:
986     return Triple::sparc;
987   case ELF::EM_SPARCV9:
988     return Triple::sparcv9;
989
990   default:
991     return Triple::UnknownArch;
992   }
993 }
994
995 /// FIXME: Maybe we should have a base ElfObjectFile that is not a template
996 /// and make these member functions?
997 inline error_code getELFRelocationAddend(const RelocationRef R,
998                                          int64_t &Addend) {
999   const ObjectFile *Obj = R.getObjectFile();
1000   DataRefImpl DRI = R.getRawDataRefImpl();
1001   // Little-endian 32-bit
1002   if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
1003     return ELFObj->getRelocationAddend(DRI, Addend);
1004
1005   // Big-endian 32-bit
1006   if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
1007     return ELFObj->getRelocationAddend(DRI, Addend);
1008
1009   // Little-endian 64-bit
1010   if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
1011     return ELFObj->getRelocationAddend(DRI, Addend);
1012
1013   // Big-endian 64-bit
1014   if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
1015     return ELFObj->getRelocationAddend(DRI, Addend);
1016
1017   llvm_unreachable("Object passed to getELFRelocationAddend() is not ELF");
1018 }
1019
1020 inline std::pair<symbol_iterator, symbol_iterator>
1021 getELFDynamicSymbolIterators(SymbolicFile *Obj) {
1022   if (const ELF32LEObjectFile *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
1023     return std::make_pair(ELF->dynamic_symbol_begin(),
1024                           ELF->dynamic_symbol_end());
1025   if (const ELF64LEObjectFile *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
1026     return std::make_pair(ELF->dynamic_symbol_begin(),
1027                           ELF->dynamic_symbol_end());
1028   if (const ELF32BEObjectFile *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
1029     return std::make_pair(ELF->dynamic_symbol_begin(),
1030                           ELF->dynamic_symbol_end());
1031   if (const ELF64BEObjectFile *ELF = cast<ELF64BEObjectFile>(Obj))
1032     return std::make_pair(ELF->dynamic_symbol_begin(),
1033                           ELF->dynamic_symbol_end());
1034
1035   llvm_unreachable(
1036       "Object passed to getELFDynamicSymbolIterators() is not ELF");
1037 }
1038
1039 /// This is a generic interface for retrieving GNU symbol version
1040 /// information from an ELFObjectFile.
1041 inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
1042                                       const SymbolRef &Sym, StringRef &Version,
1043                                       bool &IsDefault) {
1044   // Little-endian 32-bit
1045   if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
1046     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1047
1048   // Big-endian 32-bit
1049   if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
1050     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1051
1052   // Little-endian 64-bit
1053   if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
1054     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1055
1056   // Big-endian 64-bit
1057   if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
1058     return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1059
1060   llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");
1061 }
1062 }
1063 }
1064
1065 #endif