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 std::error_code getRelocationAddend(DataRefImpl Rel,
44 int64_t &Res) const = 0;
45 virtual std::pair<symbol_iterator, symbol_iterator>
46 getELFDynamicSymbolIterators() const = 0;
48 virtual std::error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
49 bool &IsDefault) const = 0;
51 virtual uint64_t getSectionFlags(SectionRef Sec) const = 0;
52 virtual uint32_t getSectionType(SectionRef Sec) const = 0;
54 static inline bool classof(const Binary *v) { return v->isELF(); }
57 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
59 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
61 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
63 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
64 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
65 typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
66 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
67 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
68 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
70 typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
71 typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
72 typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
77 void moveSymbolNext(DataRefImpl &Symb) const override;
78 std::error_code getSymbolName(DataRefImpl Symb,
79 StringRef &Res) const override;
80 std::error_code getSymbolAddress(DataRefImpl Symb,
81 uint64_t &Res) const override;
82 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
83 uint64_t getSymbolSize(DataRefImpl Symb) const override;
84 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
85 std::error_code getSymbolOther(DataRefImpl Symb, uint8_t &Res) const override;
86 std::error_code getSymbolType(DataRefImpl Symb,
87 SymbolRef::Type &Res) const override;
88 section_iterator getSymbolSection(const Elf_Sym *Symb) const;
89 std::error_code getSymbolSection(DataRefImpl Symb,
90 section_iterator &Res) const override;
92 void moveSectionNext(DataRefImpl &Sec) const override;
93 std::error_code getSectionName(DataRefImpl Sec,
94 StringRef &Res) const override;
95 uint64_t getSectionAddress(DataRefImpl Sec) const override;
96 uint64_t getSectionSize(DataRefImpl Sec) const override;
97 std::error_code getSectionContents(DataRefImpl Sec,
98 StringRef &Res) const override;
99 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
100 bool isSectionText(DataRefImpl Sec) const override;
101 bool isSectionData(DataRefImpl Sec) const override;
102 bool isSectionBSS(DataRefImpl Sec) const override;
103 bool isSectionVirtual(DataRefImpl Sec) const override;
104 bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
105 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
106 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
107 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
109 void moveRelocationNext(DataRefImpl &Rel) const override;
110 std::error_code getRelocationAddress(DataRefImpl Rel,
111 uint64_t &Res) const override;
112 std::error_code getRelocationOffset(DataRefImpl Rel,
113 uint64_t &Res) const override;
114 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
115 section_iterator getRelocationSection(DataRefImpl Rel) const override;
116 std::error_code getRelocationType(DataRefImpl Rel,
117 uint64_t &Res) const override;
119 getRelocationTypeName(DataRefImpl Rel,
120 SmallVectorImpl<char> &Result) const override;
122 uint64_t getROffset(DataRefImpl Rel) const;
123 StringRef getRelocationTypeName(uint32_t Type) const;
125 /// \brief Get the relocation section that contains \a Rel.
126 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
127 return EF.getSection(Rel.d.a);
130 const Elf_Rel *getRel(DataRefImpl Rel) const;
131 const Elf_Rela *getRela(DataRefImpl Rela) const;
133 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
134 bool IsDynamic = Symb.p & 1;
137 EF.begin_dynamic_symbols().getEntSize(),
138 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
139 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
140 reinterpret_cast<const char *>(Symb.p), IsDynamic);
143 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
145 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
146 static_cast<uintptr_t>(Symb.isDynamic());
150 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
151 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
152 reinterpret_cast<const char *>(Sec.p));
155 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
157 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
161 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
163 DRI.p = reinterpret_cast<uintptr_t>(Sec);
167 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
168 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
169 reinterpret_cast<const char *>(Dyn.p));
172 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
174 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
178 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
179 unsigned char Binding = ESym->getBinding();
180 unsigned char Visibility = ESym->getVisibility();
182 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
183 // visibility is either DEFAULT or PROTECTED. All other symbols are not
185 if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
186 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
192 // This flag is used for classof, to distinguish ELFObjectFile from
193 // its subclass. If more subclasses will be created, this flag will
194 // have to become an enum.
195 bool isDyldELFObject;
198 ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
200 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
202 basic_symbol_iterator symbol_begin_impl() const override;
203 basic_symbol_iterator symbol_end_impl() const override;
205 symbol_iterator dynamic_symbol_begin() const;
206 symbol_iterator dynamic_symbol_end() const;
208 section_iterator section_begin() const override;
209 section_iterator section_end() const override;
211 std::error_code getRelocationAddend(DataRefImpl Rel,
212 int64_t &Res) const override;
213 std::error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
214 bool &IsDefault) const override;
216 uint64_t getSectionFlags(SectionRef Sec) const override;
217 uint32_t getSectionType(SectionRef Sec) const override;
219 uint8_t getBytesInAddress() const override;
220 StringRef getFileFormatName() const override;
221 unsigned getArch() const override;
222 StringRef getLoadName() const;
224 std::error_code getPlatformFlags(unsigned &Result) const override {
225 Result = EF.getHeader()->e_flags;
226 return std::error_code();
229 const ELFFile<ELFT> *getELFFile() const { return &EF; }
231 bool isDyldType() const { return isDyldELFObject; }
232 static inline bool classof(const Binary *v) {
233 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
237 std::pair<symbol_iterator, symbol_iterator>
238 getELFDynamicSymbolIterators() const override;
240 bool isRelocatableObject() const override;
243 typedef ELFObjectFile<ELFType<support::little, false>> ELF32LEObjectFile;
244 typedef ELFObjectFile<ELFType<support::little, true>> ELF64LEObjectFile;
245 typedef ELFObjectFile<ELFType<support::big, false>> ELF32BEObjectFile;
246 typedef ELFObjectFile<ELFType<support::big, true>> ELF64BEObjectFile;
248 template <class ELFT>
249 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
250 Symb = toDRI(++toELFSymIter(Symb));
253 template <class ELFT>
254 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
255 StringRef &Result) const {
256 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
258 return Name.getError();
260 return std::error_code();
263 template <class ELFT>
264 std::error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
266 bool &IsDefault) const {
267 DataRefImpl Symb = SymRef.getRawDataRefImpl();
268 const Elf_Sym *symb = getSymbol(Symb);
269 ErrorOr<StringRef> Ver =
270 EF.getSymbolVersion(EF.getSection(Symb.d.b), symb, IsDefault);
272 return Ver.getError();
274 return std::error_code();
277 template <class ELFT>
278 uint64_t ELFObjectFile<ELFT>::getSectionFlags(SectionRef Sec) const {
279 DataRefImpl DRI = Sec.getRawDataRefImpl();
280 return toELFShdrIter(DRI)->sh_flags;
283 template <class ELFT>
284 uint32_t ELFObjectFile<ELFT>::getSectionType(SectionRef Sec) const {
285 DataRefImpl DRI = Sec.getRawDataRefImpl();
286 return toELFShdrIter(DRI)->sh_type;
289 template <class ELFT>
290 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
291 uint64_t &Result) const {
292 const Elf_Sym *ESym = getSymbol(Symb);
293 switch (EF.getSymbolTableIndex(ESym)) {
294 case ELF::SHN_COMMON:
296 Result = UnknownAddressOrSize;
297 return std::error_code();
299 Result = ESym->st_value;
300 return std::error_code();
305 const Elf_Ehdr *Header = EF.getHeader();
306 Result = ESym->st_value;
308 // Clear the ARM/Thumb or microMIPS indicator flag.
309 if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
310 ESym->getType() == ELF::STT_FUNC)
313 if (Header->e_type == ELF::ET_REL) {
314 const typename ELFFile<ELFT>::Elf_Shdr * Section = EF.getSection(ESym);
315 if (Section != nullptr)
316 Result += Section->sh_addr;
319 return std::error_code();
322 template <class ELFT>
323 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
324 Elf_Sym_Iter Sym = toELFSymIter(Symb);
325 if (Sym->st_shndx == ELF::SHN_COMMON)
326 return Sym->st_value;
330 template <class ELFT>
331 uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb) const {
332 return toELFSymIter(Symb)->st_size;
335 template <class ELFT>
336 std::error_code ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb,
337 uint8_t &Result) const {
338 Result = toELFSymIter(Symb)->st_other;
339 return std::error_code();
342 template <class ELFT>
344 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
345 SymbolRef::Type &Result) const {
346 const Elf_Sym *ESym = getSymbol(Symb);
348 switch (ESym->getType()) {
349 case ELF::STT_NOTYPE:
350 Result = SymbolRef::ST_Unknown;
352 case ELF::STT_SECTION:
353 Result = SymbolRef::ST_Debug;
356 Result = SymbolRef::ST_File;
359 Result = SymbolRef::ST_Function;
361 case ELF::STT_OBJECT:
362 case ELF::STT_COMMON:
364 Result = SymbolRef::ST_Data;
367 Result = SymbolRef::ST_Other;
370 return std::error_code();
373 template <class ELFT>
374 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
375 Elf_Sym_Iter EIter = toELFSymIter(Symb);
376 const Elf_Sym *ESym = &*EIter;
378 uint32_t Result = SymbolRef::SF_None;
380 if (ESym->getBinding() != ELF::STB_LOCAL)
381 Result |= SymbolRef::SF_Global;
383 if (ESym->getBinding() == ELF::STB_WEAK)
384 Result |= SymbolRef::SF_Weak;
386 if (ESym->st_shndx == ELF::SHN_ABS)
387 Result |= SymbolRef::SF_Absolute;
389 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
390 EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
391 Result |= SymbolRef::SF_FormatSpecific;
393 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
394 Result |= SymbolRef::SF_Undefined;
396 if (ESym->getType() == ELF::STT_COMMON ||
397 EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
398 Result |= SymbolRef::SF_Common;
400 if (isExportedToOtherDSO(ESym))
401 Result |= SymbolRef::SF_Exported;
403 if (ESym->getVisibility() == ELF::STV_HIDDEN)
404 Result |= SymbolRef::SF_Hidden;
409 template <class ELFT>
411 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
412 const Elf_Shdr *ESec = EF.getSection(ESym);
414 return section_end();
417 Sec.p = reinterpret_cast<intptr_t>(ESec);
418 return section_iterator(SectionRef(Sec, this));
422 template <class ELFT>
424 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
425 section_iterator &Res) const {
426 Res = getSymbolSection(getSymbol(Symb));
427 return std::error_code();
430 template <class ELFT>
431 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
432 Sec = toDRI(++toELFShdrIter(Sec));
435 template <class ELFT>
436 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
437 StringRef &Result) const {
438 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
440 return Name.getError();
442 return std::error_code();
445 template <class ELFT>
446 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
447 return toELFShdrIter(Sec)->sh_addr;
450 template <class ELFT>
451 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
452 return toELFShdrIter(Sec)->sh_size;
455 template <class ELFT>
457 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
458 StringRef &Result) const {
459 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
460 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
461 return std::error_code();
464 template <class ELFT>
465 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
466 return toELFShdrIter(Sec)->sh_addralign;
469 template <class ELFT>
470 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
471 return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
474 template <class ELFT>
475 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
476 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
477 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
478 EShdr->sh_type == ELF::SHT_PROGBITS;
481 template <class ELFT>
482 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
483 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
484 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
485 EShdr->sh_type == ELF::SHT_NOBITS;
488 template <class ELFT>
489 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
490 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
493 template <class ELFT>
494 bool ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
495 DataRefImpl Symb) const {
496 Elf_Sym_Iter ESym = toELFSymIter(Symb);
498 uintX_t Index = ESym->st_shndx;
499 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
501 return !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
504 template <class ELFT>
506 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
508 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
509 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
511 return relocation_iterator(RelocationRef(RelData, this));
514 template <class ELFT>
516 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
518 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
519 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
520 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
521 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
524 RelData.d.b = S->sh_size / S->sh_entsize;
526 return relocation_iterator(RelocationRef(RelData, this));
529 template <class ELFT>
531 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
532 if (EF.getHeader()->e_type != ELF::ET_REL)
533 return section_end();
535 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
536 uintX_t Type = EShdr->sh_type;
537 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
538 return section_end();
540 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
541 return section_iterator(SectionRef(toDRI(R), this));
545 template <class ELFT>
546 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
550 template <class ELFT>
552 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
554 const Elf_Shdr *sec = getRelSection(Rel);
555 switch (sec->sh_type) {
557 report_fatal_error("Invalid section type in Rel!");
559 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
562 case ELF::SHT_RELA: {
563 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
570 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
572 DataRefImpl SymbolData;
573 switch (SymSec->sh_type) {
575 report_fatal_error("Invalid symbol table section type!");
576 case ELF::SHT_SYMTAB:
577 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
579 case ELF::SHT_DYNSYM:
580 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
584 return symbol_iterator(SymbolRef(SymbolData, this));
587 // ELF relocations can target sections, by targetting a symbol of type
589 template <class ELFT>
591 ELFObjectFile<ELFT>::getRelocationSection(DataRefImpl Rel) const {
592 symbol_iterator Sym = getRelocationSymbol(Rel);
593 if (Sym == symbol_end())
594 return section_end();
595 const Elf_Sym *ESym = getSymbol(Sym->getRawDataRefImpl());
596 if (ESym->getType() != ELF::STT_SECTION)
597 return section_end();
598 return getSymbolSection(ESym);
601 template <class ELFT>
603 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
604 uint64_t &Result) const {
605 uint64_t ROffset = getROffset(Rel);
606 const Elf_Ehdr *Header = EF.getHeader();
608 if (Header->e_type == ELF::ET_REL) {
609 const Elf_Shdr *RelocationSec = getRelSection(Rel);
610 const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
611 Result = ROffset + RelocatedSec->sh_addr;
616 return std::error_code();
619 template <class ELFT>
621 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
622 uint64_t &Result) const {
623 assert(EF.getHeader()->e_type == ELF::ET_REL &&
624 "Only relocatable object files have relocation offsets");
625 Result = getROffset(Rel);
626 return std::error_code();
629 template <class ELFT>
630 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
631 const Elf_Shdr *sec = getRelSection(Rel);
632 switch (sec->sh_type) {
634 report_fatal_error("Invalid section type in Rel!");
636 return getRel(Rel)->r_offset;
638 return getRela(Rel)->r_offset;
642 template <class ELFT>
643 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
644 uint64_t &Result) const {
645 const Elf_Shdr *sec = getRelSection(Rel);
646 switch (sec->sh_type) {
648 report_fatal_error("Invalid section type in Rel!");
650 Result = getRel(Rel)->getType(EF.isMips64EL());
653 case ELF::SHT_RELA: {
654 Result = getRela(Rel)->getType(EF.isMips64EL());
658 return std::error_code();
661 template <class ELFT>
662 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
663 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
666 template <class ELFT>
667 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
668 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
669 const Elf_Shdr *sec = getRelSection(Rel);
671 switch (sec->sh_type) {
673 return object_error::parse_failed;
675 type = getRel(Rel)->getType(EF.isMips64EL());
678 case ELF::SHT_RELA: {
679 type = getRela(Rel)->getType(EF.isMips64EL());
684 EF.getRelocationTypeName(type, Result);
685 return std::error_code();
688 template <class ELFT>
690 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
691 int64_t &Result) const {
692 const Elf_Shdr *sec = getRelSection(Rel);
693 switch (sec->sh_type) {
695 report_fatal_error("Invalid section type in Rel!");
698 return std::error_code();
700 case ELF::SHT_RELA: {
701 Result = getRela(Rel)->r_addend;
702 return std::error_code();
707 template <class ELFT>
708 const typename ELFFile<ELFT>::Elf_Sym *
709 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
710 return &*toELFSymIter(Symb);
713 template <class ELFT>
714 const typename ELFObjectFile<ELFT>::Elf_Rel *
715 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
716 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
719 template <class ELFT>
720 const typename ELFObjectFile<ELFT>::Elf_Rela *
721 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
722 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
725 template <class ELFT>
726 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
728 getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
732 EF(Data.getBuffer(), EC) {}
734 template <class ELFT>
735 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
736 return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
739 template <class ELFT>
740 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
741 return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
744 template <class ELFT>
745 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
746 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
749 template <class ELFT>
750 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
751 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
754 template <class ELFT>
755 section_iterator ELFObjectFile<ELFT>::section_begin() const {
756 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
759 template <class ELFT>
760 section_iterator ELFObjectFile<ELFT>::section_end() const {
761 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
764 template <class ELFT>
765 StringRef ELFObjectFile<ELFT>::getLoadName() const {
766 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
767 Elf_Dyn_Iter DE = EF.end_dynamic_table();
769 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
773 return EF.getDynamicString(DI->getVal());
777 template <class ELFT>
778 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
779 return ELFT::Is64Bits ? 8 : 4;
782 template <class ELFT>
783 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
784 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
785 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
786 case ELF::ELFCLASS32:
787 switch (EF.getHeader()->e_machine) {
791 return "ELF32-x86-64";
793 return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
794 case ELF::EM_HEXAGON:
795 return "ELF32-hexagon";
801 case ELF::EM_SPARC32PLUS:
802 return "ELF32-sparc";
804 return "ELF32-unknown";
806 case ELF::ELFCLASS64:
807 switch (EF.getHeader()->e_machine) {
811 return "ELF64-x86-64";
812 case ELF::EM_AARCH64:
813 return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
815 return "ELF64-ppc64";
818 case ELF::EM_SPARCV9:
819 return "ELF64-sparc";
823 return "ELF64-unknown";
826 // FIXME: Proper error handling.
827 report_fatal_error("Invalid ELFCLASS!");
831 template <class ELFT>
832 unsigned ELFObjectFile<ELFT>::getArch() const {
833 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
834 switch (EF.getHeader()->e_machine) {
838 return Triple::x86_64;
839 case ELF::EM_AARCH64:
840 return Triple::aarch64;
843 case ELF::EM_HEXAGON:
844 return Triple::hexagon;
846 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
847 case ELF::ELFCLASS32:
848 return IsLittleEndian ? Triple::mipsel : Triple::mips;
849 case ELF::ELFCLASS64:
850 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
852 report_fatal_error("Invalid ELFCLASS!");
857 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
859 return Triple::systemz;
862 case ELF::EM_SPARC32PLUS:
863 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
864 case ELF::EM_SPARCV9:
865 return Triple::sparcv9;
868 return Triple::UnknownArch;
872 template <class ELFT>
873 std::pair<symbol_iterator, symbol_iterator>
874 ELFObjectFile<ELFT>::getELFDynamicSymbolIterators() const {
875 return std::make_pair(dynamic_symbol_begin(), dynamic_symbol_end());
878 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
879 return EF.getHeader()->e_type == ELF::ET_REL;
882 inline std::error_code getELFRelocationAddend(const RelocationRef R,
884 const ObjectFile *Obj = R.getObjectFile();
885 DataRefImpl DRI = R.getRawDataRefImpl();
886 return cast<ELFObjectFileBase>(Obj)->getRelocationAddend(DRI, Addend);
889 inline std::pair<symbol_iterator, symbol_iterator>
890 getELFDynamicSymbolIterators(const SymbolicFile *Obj) {
891 return cast<ELFObjectFileBase>(Obj)->getELFDynamicSymbolIterators();
894 inline std::error_code GetELFSymbolVersion(const ObjectFile *Obj,
895 const SymbolRef &Sym,
898 return cast<ELFObjectFileBase>(Obj)
899 ->getSymbolVersion(Sym, Version, IsDefault);