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