1 //===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the ELFObjectFile template class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_OBJECT_ELFOBJECTFILE_H
15 #define LLVM_OBJECT_ELFOBJECTFILE_H
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"
38 class ELFObjectFileBase : public ObjectFile {
40 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
43 virtual ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0;
45 // FIXME: This is a bit of a hack. Every caller should know if it expecting
47 virtual bool hasRelocationAddend(DataRefImpl Rel) const = 0;
49 virtual std::pair<symbol_iterator, symbol_iterator>
50 getELFDynamicSymbolIterators() const = 0;
52 virtual uint64_t getSectionFlags(SectionRef Sec) const = 0;
53 virtual uint32_t getSectionType(SectionRef Sec) const = 0;
55 virtual uint64_t getSymbolSize(SymbolRef Symb) const = 0;
57 static inline bool classof(const Binary *v) { return v->isELF(); }
60 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
62 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
64 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
66 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
67 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
68 typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
69 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
70 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
71 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
73 typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
74 typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
75 typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
77 uint64_t getSymbolSize(SymbolRef Symb) const override;
82 void moveSymbolNext(DataRefImpl &Symb) const override;
83 std::error_code getSymbolName(DataRefImpl Symb,
84 StringRef &Res) const override;
85 std::error_code getSymbolAddress(DataRefImpl Symb,
86 uint64_t &Res) const override;
87 uint64_t getSymbolValue(DataRefImpl Symb) const override;
88 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
89 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
90 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
91 std::error_code getSymbolOther(DataRefImpl Symb, uint8_t &Res) const override;
92 std::error_code getSymbolType(DataRefImpl Symb,
93 SymbolRef::Type &Res) const override;
94 section_iterator getSymbolSection(const Elf_Sym *Symb) const;
95 std::error_code getSymbolSection(DataRefImpl Symb,
96 section_iterator &Res) const override;
98 void moveSectionNext(DataRefImpl &Sec) const override;
99 std::error_code getSectionName(DataRefImpl Sec,
100 StringRef &Res) const override;
101 uint64_t getSectionAddress(DataRefImpl Sec) const override;
102 uint64_t getSectionSize(DataRefImpl Sec) const override;
103 std::error_code getSectionContents(DataRefImpl Sec,
104 StringRef &Res) const override;
105 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
106 bool isSectionText(DataRefImpl Sec) const override;
107 bool isSectionData(DataRefImpl Sec) const override;
108 bool isSectionBSS(DataRefImpl Sec) const override;
109 bool isSectionVirtual(DataRefImpl Sec) const override;
110 bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
111 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
112 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
113 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
115 void moveRelocationNext(DataRefImpl &Rel) const override;
116 std::error_code getRelocationAddress(DataRefImpl Rel,
117 uint64_t &Res) const override;
118 std::error_code getRelocationOffset(DataRefImpl Rel,
119 uint64_t &Res) const override;
120 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
121 std::error_code getRelocationType(DataRefImpl Rel,
122 uint64_t &Res) const override;
124 getRelocationTypeName(DataRefImpl Rel,
125 SmallVectorImpl<char> &Result) const override;
127 uint64_t getROffset(DataRefImpl Rel) const;
128 StringRef getRelocationTypeName(uint32_t Type) const;
130 /// \brief Get the relocation section that contains \a Rel.
131 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
132 return EF.getSection(Rel.d.a);
135 const Elf_Rel *getRel(DataRefImpl Rel) const;
136 const Elf_Rela *getRela(DataRefImpl Rela) const;
138 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
139 bool IsDynamic = Symb.p & 1;
142 EF.begin_dynamic_symbols().getEntSize(),
143 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
144 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
145 reinterpret_cast<const char *>(Symb.p), IsDynamic);
148 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
150 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
151 static_cast<uintptr_t>(Symb.isDynamic());
155 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
156 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
157 reinterpret_cast<const char *>(Sec.p));
160 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
162 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
166 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
168 DRI.p = reinterpret_cast<uintptr_t>(Sec);
172 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
173 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
174 reinterpret_cast<const char *>(Dyn.p));
177 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
179 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
183 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
184 unsigned char Binding = ESym->getBinding();
185 unsigned char Visibility = ESym->getVisibility();
187 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
188 // visibility is either DEFAULT or PROTECTED. All other symbols are not
190 if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
191 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
197 // This flag is used for classof, to distinguish ELFObjectFile from
198 // its subclass. If more subclasses will be created, this flag will
199 // have to become an enum.
200 bool isDyldELFObject;
203 ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
205 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
207 basic_symbol_iterator symbol_begin_impl() const override;
208 basic_symbol_iterator symbol_end_impl() const override;
210 symbol_iterator dynamic_symbol_begin() const;
211 symbol_iterator dynamic_symbol_end() const;
213 section_iterator section_begin() const override;
214 section_iterator section_end() const override;
216 ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const override;
217 bool hasRelocationAddend(DataRefImpl Rel) const override;
219 uint64_t getSectionFlags(SectionRef Sec) const override;
220 uint32_t getSectionType(SectionRef Sec) const override;
222 uint8_t getBytesInAddress() const override;
223 StringRef getFileFormatName() const override;
224 unsigned getArch() const override;
225 StringRef getLoadName() const;
227 std::error_code getPlatformFlags(unsigned &Result) const override {
228 Result = EF.getHeader()->e_flags;
229 return std::error_code();
232 const ELFFile<ELFT> *getELFFile() const { return &EF; }
234 bool isDyldType() const { return isDyldELFObject; }
235 static inline bool classof(const Binary *v) {
236 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
240 std::pair<symbol_iterator, symbol_iterator>
241 getELFDynamicSymbolIterators() const override;
243 bool isRelocatableObject() const override;
246 typedef ELFObjectFile<ELFType<support::little, false>> ELF32LEObjectFile;
247 typedef ELFObjectFile<ELFType<support::little, true>> ELF64LEObjectFile;
248 typedef ELFObjectFile<ELFType<support::big, false>> ELF32BEObjectFile;
249 typedef ELFObjectFile<ELFType<support::big, true>> ELF64BEObjectFile;
251 template <class ELFT>
252 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
253 Symb = toDRI(++toELFSymIter(Symb));
256 template <class ELFT>
257 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
258 StringRef &Result) const {
259 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
261 return Name.getError();
263 return std::error_code();
266 template <class ELFT>
267 uint64_t ELFObjectFile<ELFT>::getSectionFlags(SectionRef Sec) const {
268 DataRefImpl DRI = Sec.getRawDataRefImpl();
269 return toELFShdrIter(DRI)->sh_flags;
272 template <class ELFT>
273 uint32_t ELFObjectFile<ELFT>::getSectionType(SectionRef Sec) const {
274 DataRefImpl DRI = Sec.getRawDataRefImpl();
275 return toELFShdrIter(DRI)->sh_type;
278 template <class ELFT>
279 uint64_t ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb) const {
280 const Elf_Sym *ESym = getSymbol(Symb);
281 switch (ESym->st_shndx) {
282 case ELF::SHN_COMMON:
284 return UnknownAddress;
286 return ESym->st_value;
289 const Elf_Ehdr *Header = EF.getHeader();
290 uint64_t Ret = ESym->st_value;
292 // Clear the ARM/Thumb or microMIPS indicator flag.
293 if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
294 ESym->getType() == ELF::STT_FUNC)
300 template <class ELFT>
301 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
302 uint64_t &Result) const {
303 Result = getSymbolValue(Symb);
304 const Elf_Sym *ESym = getSymbol(Symb);
305 switch (ESym->st_shndx) {
306 case ELF::SHN_COMMON:
309 return std::error_code();
312 const Elf_Ehdr *Header = EF.getHeader();
314 if (Header->e_type == ELF::ET_REL) {
315 const typename ELFFile<ELFT>::Elf_Shdr * Section = EF.getSection(ESym);
316 if (Section != nullptr)
317 Result += Section->sh_addr;
320 return std::error_code();
323 template <class ELFT>
324 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
325 Elf_Sym_Iter Sym = toELFSymIter(Symb);
326 if (Sym->st_shndx == ELF::SHN_COMMON)
327 return Sym->st_value;
331 template <class ELFT>
332 uint64_t ELFObjectFile<ELFT>::getSymbolSize(SymbolRef Symb) const {
333 return toELFSymIter(Symb.getRawDataRefImpl())->st_size;
336 template <class ELFT>
337 uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
338 return toELFSymIter(Symb)->st_size;
341 template <class ELFT>
342 std::error_code ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb,
343 uint8_t &Result) const {
344 Result = toELFSymIter(Symb)->st_other;
345 return std::error_code();
348 template <class ELFT>
350 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
351 SymbolRef::Type &Result) const {
352 const Elf_Sym *ESym = getSymbol(Symb);
354 switch (ESym->getType()) {
355 case ELF::STT_NOTYPE:
356 Result = SymbolRef::ST_Unknown;
358 case ELF::STT_SECTION:
359 Result = SymbolRef::ST_Debug;
362 Result = SymbolRef::ST_File;
365 Result = SymbolRef::ST_Function;
367 case ELF::STT_OBJECT:
368 case ELF::STT_COMMON:
370 Result = SymbolRef::ST_Data;
373 Result = SymbolRef::ST_Other;
376 return std::error_code();
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;
384 uint32_t Result = SymbolRef::SF_None;
386 if (ESym->getBinding() != ELF::STB_LOCAL)
387 Result |= SymbolRef::SF_Global;
389 if (ESym->getBinding() == ELF::STB_WEAK)
390 Result |= SymbolRef::SF_Weak;
392 if (ESym->st_shndx == ELF::SHN_ABS)
393 Result |= SymbolRef::SF_Absolute;
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;
399 if (ESym->st_shndx == ELF::SHN_UNDEF)
400 Result |= SymbolRef::SF_Undefined;
402 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
403 Result |= SymbolRef::SF_Common;
405 if (isExportedToOtherDSO(ESym))
406 Result |= SymbolRef::SF_Exported;
408 if (ESym->getVisibility() == ELF::STV_HIDDEN)
409 Result |= SymbolRef::SF_Hidden;
414 template <class ELFT>
416 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
417 const Elf_Shdr *ESec = EF.getSection(ESym);
419 return section_end();
422 Sec.p = reinterpret_cast<intptr_t>(ESec);
423 return section_iterator(SectionRef(Sec, this));
427 template <class ELFT>
429 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
430 section_iterator &Res) const {
431 Res = getSymbolSection(getSymbol(Symb));
432 return std::error_code();
435 template <class ELFT>
436 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
437 Sec = toDRI(++toELFShdrIter(Sec));
440 template <class ELFT>
441 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
442 StringRef &Result) const {
443 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
445 return Name.getError();
447 return std::error_code();
450 template <class ELFT>
451 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
452 return toELFShdrIter(Sec)->sh_addr;
455 template <class ELFT>
456 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
457 return toELFShdrIter(Sec)->sh_size;
460 template <class ELFT>
462 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
463 StringRef &Result) const {
464 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
465 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
466 return std::error_code();
469 template <class ELFT>
470 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
471 return toELFShdrIter(Sec)->sh_addralign;
474 template <class ELFT>
475 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
476 return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
479 template <class ELFT>
480 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
481 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
482 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
483 EShdr->sh_type == ELF::SHT_PROGBITS;
486 template <class ELFT>
487 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
488 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
489 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
490 EShdr->sh_type == ELF::SHT_NOBITS;
493 template <class ELFT>
494 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
495 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
498 template <class ELFT>
499 bool ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
500 DataRefImpl Symb) const {
501 Elf_Sym_Iter ESym = toELFSymIter(Symb);
503 uintX_t Index = ESym->st_shndx;
504 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
506 return !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
509 template <class ELFT>
511 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
513 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
514 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
516 return relocation_iterator(RelocationRef(RelData, this));
519 template <class ELFT>
521 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
523 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
524 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
525 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
526 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
529 RelData.d.b = S->sh_size / S->sh_entsize;
531 return relocation_iterator(RelocationRef(RelData, this));
534 template <class ELFT>
536 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
537 if (EF.getHeader()->e_type != ELF::ET_REL)
538 return section_end();
540 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
541 uintX_t Type = EShdr->sh_type;
542 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
543 return section_end();
545 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
546 return section_iterator(SectionRef(toDRI(R), this));
550 template <class ELFT>
551 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
555 template <class ELFT>
557 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
559 const Elf_Shdr *sec = getRelSection(Rel);
560 switch (sec->sh_type) {
562 report_fatal_error("Invalid section type in Rel!");
564 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
567 case ELF::SHT_RELA: {
568 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
575 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
577 DataRefImpl SymbolData;
578 switch (SymSec->sh_type) {
580 report_fatal_error("Invalid symbol table section type!");
581 case ELF::SHT_SYMTAB:
582 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
584 case ELF::SHT_DYNSYM:
585 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
589 return symbol_iterator(SymbolRef(SymbolData, this));
592 template <class ELFT>
594 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
595 uint64_t &Result) const {
596 uint64_t ROffset = getROffset(Rel);
597 const Elf_Ehdr *Header = EF.getHeader();
599 if (Header->e_type == ELF::ET_REL) {
600 const Elf_Shdr *RelocationSec = getRelSection(Rel);
601 const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
602 Result = ROffset + RelocatedSec->sh_addr;
607 return std::error_code();
610 template <class ELFT>
612 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
613 uint64_t &Result) const {
614 assert(EF.getHeader()->e_type == ELF::ET_REL &&
615 "Only relocatable object files have relocation offsets");
616 Result = getROffset(Rel);
617 return std::error_code();
620 template <class ELFT>
621 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
622 const Elf_Shdr *sec = getRelSection(Rel);
623 switch (sec->sh_type) {
625 report_fatal_error("Invalid section type in Rel!");
627 return getRel(Rel)->r_offset;
629 return getRela(Rel)->r_offset;
633 template <class ELFT>
634 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
635 uint64_t &Result) const {
636 const Elf_Shdr *sec = getRelSection(Rel);
637 switch (sec->sh_type) {
639 report_fatal_error("Invalid section type in Rel!");
641 Result = getRel(Rel)->getType(EF.isMips64EL());
644 case ELF::SHT_RELA: {
645 Result = getRela(Rel)->getType(EF.isMips64EL());
649 return std::error_code();
652 template <class ELFT>
653 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
654 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
657 template <class ELFT>
658 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
659 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
660 const Elf_Shdr *sec = getRelSection(Rel);
662 switch (sec->sh_type) {
664 return object_error::parse_failed;
666 type = getRel(Rel)->getType(EF.isMips64EL());
669 case ELF::SHT_RELA: {
670 type = getRela(Rel)->getType(EF.isMips64EL());
675 EF.getRelocationTypeName(type, Result);
676 return std::error_code();
679 template <class ELFT>
681 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
682 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
683 return object_error::parse_failed;
684 return (int64_t)getRela(Rel)->r_addend;
687 template <class ELFT>
688 bool ELFObjectFile<ELFT>::hasRelocationAddend(DataRefImpl Rel) const {
689 return getRelSection(Rel)->sh_type == ELF::SHT_RELA;
692 template <class ELFT>
693 const typename ELFFile<ELFT>::Elf_Sym *
694 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
695 return &*toELFSymIter(Symb);
698 template <class ELFT>
699 const typename ELFObjectFile<ELFT>::Elf_Rel *
700 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
701 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
704 template <class ELFT>
705 const typename ELFObjectFile<ELFT>::Elf_Rela *
706 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
707 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
710 template <class ELFT>
711 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
713 getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
717 EF(Data.getBuffer(), EC) {}
719 template <class ELFT>
720 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
721 return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
724 template <class ELFT>
725 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
726 return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
729 template <class ELFT>
730 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
731 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
734 template <class ELFT>
735 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
736 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
739 template <class ELFT>
740 section_iterator ELFObjectFile<ELFT>::section_begin() const {
741 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
744 template <class ELFT>
745 section_iterator ELFObjectFile<ELFT>::section_end() const {
746 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
749 template <class ELFT>
750 StringRef ELFObjectFile<ELFT>::getLoadName() const {
751 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
752 Elf_Dyn_Iter DE = EF.end_dynamic_table();
754 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
758 return EF.getDynamicString(DI->getVal());
762 template <class ELFT>
763 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
764 return ELFT::Is64Bits ? 8 : 4;
767 template <class ELFT>
768 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
769 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
770 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
771 case ELF::ELFCLASS32:
772 switch (EF.getHeader()->e_machine) {
776 return "ELF32-x86-64";
778 return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
779 case ELF::EM_HEXAGON:
780 return "ELF32-hexagon";
786 case ELF::EM_SPARC32PLUS:
787 return "ELF32-sparc";
789 return "ELF32-unknown";
791 case ELF::ELFCLASS64:
792 switch (EF.getHeader()->e_machine) {
796 return "ELF64-x86-64";
797 case ELF::EM_AARCH64:
798 return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
800 return "ELF64-ppc64";
803 case ELF::EM_SPARCV9:
804 return "ELF64-sparc";
808 return "ELF64-unknown";
811 // FIXME: Proper error handling.
812 report_fatal_error("Invalid ELFCLASS!");
816 template <class ELFT>
817 unsigned ELFObjectFile<ELFT>::getArch() const {
818 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
819 switch (EF.getHeader()->e_machine) {
823 return Triple::x86_64;
824 case ELF::EM_AARCH64:
825 return Triple::aarch64;
828 case ELF::EM_HEXAGON:
829 return Triple::hexagon;
831 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
832 case ELF::ELFCLASS32:
833 return IsLittleEndian ? Triple::mipsel : Triple::mips;
834 case ELF::ELFCLASS64:
835 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
837 report_fatal_error("Invalid ELFCLASS!");
842 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
844 return Triple::systemz;
847 case ELF::EM_SPARC32PLUS:
848 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
849 case ELF::EM_SPARCV9:
850 return Triple::sparcv9;
853 return Triple::UnknownArch;
857 template <class ELFT>
858 std::pair<symbol_iterator, symbol_iterator>
859 ELFObjectFile<ELFT>::getELFDynamicSymbolIterators() const {
860 return std::make_pair(dynamic_symbol_begin(), dynamic_symbol_end());
863 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
864 return EF.getHeader()->e_type == ELF::ET_REL;
867 inline std::pair<symbol_iterator, symbol_iterator>
868 getELFDynamicSymbolIterators(const SymbolicFile *Obj) {
869 return cast<ELFObjectFileBase>(Obj)->getELFDynamicSymbolIterators();