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 std::error_code getRelocationType(DataRefImpl Rel,
116 uint64_t &Res) const override;
118 getRelocationTypeName(DataRefImpl Rel,
119 SmallVectorImpl<char> &Result) const override;
121 uint64_t getROffset(DataRefImpl Rel) const;
122 StringRef getRelocationTypeName(uint32_t Type) const;
124 /// \brief Get the relocation section that contains \a Rel.
125 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
126 return EF.getSection(Rel.d.a);
129 const Elf_Rel *getRel(DataRefImpl Rel) const;
130 const Elf_Rela *getRela(DataRefImpl Rela) const;
132 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
133 bool IsDynamic = Symb.p & 1;
136 EF.begin_dynamic_symbols().getEntSize(),
137 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
138 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
139 reinterpret_cast<const char *>(Symb.p), IsDynamic);
142 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
144 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
145 static_cast<uintptr_t>(Symb.isDynamic());
149 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
150 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
151 reinterpret_cast<const char *>(Sec.p));
154 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
156 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
160 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
162 DRI.p = reinterpret_cast<uintptr_t>(Sec);
166 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
167 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
168 reinterpret_cast<const char *>(Dyn.p));
171 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
173 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
177 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
178 unsigned char Binding = ESym->getBinding();
179 unsigned char Visibility = ESym->getVisibility();
181 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
182 // visibility is either DEFAULT or PROTECTED. All other symbols are not
184 if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
185 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
191 // This flag is used for classof, to distinguish ELFObjectFile from
192 // its subclass. If more subclasses will be created, this flag will
193 // have to become an enum.
194 bool isDyldELFObject;
197 ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
199 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
201 basic_symbol_iterator symbol_begin_impl() const override;
202 basic_symbol_iterator symbol_end_impl() const override;
204 symbol_iterator dynamic_symbol_begin() const;
205 symbol_iterator dynamic_symbol_end() const;
207 section_iterator section_begin() const override;
208 section_iterator section_end() const override;
210 std::error_code getRelocationAddend(DataRefImpl Rel,
211 int64_t &Res) const override;
212 std::error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
213 bool &IsDefault) const override;
215 uint64_t getSectionFlags(SectionRef Sec) const override;
216 uint32_t getSectionType(SectionRef Sec) const override;
218 uint8_t getBytesInAddress() const override;
219 StringRef getFileFormatName() const override;
220 unsigned getArch() const override;
221 StringRef getLoadName() const;
223 std::error_code getPlatformFlags(unsigned &Result) const override {
224 Result = EF.getHeader()->e_flags;
225 return std::error_code();
228 const ELFFile<ELFT> *getELFFile() const { return &EF; }
230 bool isDyldType() const { return isDyldELFObject; }
231 static inline bool classof(const Binary *v) {
232 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
236 std::pair<symbol_iterator, symbol_iterator>
237 getELFDynamicSymbolIterators() const override;
239 bool isRelocatableObject() const override;
242 typedef ELFObjectFile<ELFType<support::little, false>> ELF32LEObjectFile;
243 typedef ELFObjectFile<ELFType<support::little, true>> ELF64LEObjectFile;
244 typedef ELFObjectFile<ELFType<support::big, false>> ELF32BEObjectFile;
245 typedef ELFObjectFile<ELFType<support::big, true>> ELF64BEObjectFile;
247 template <class ELFT>
248 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const {
249 Symb = toDRI(++toELFSymIter(Symb));
252 template <class ELFT>
253 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
254 StringRef &Result) const {
255 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
257 return Name.getError();
259 return std::error_code();
262 template <class ELFT>
263 std::error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
265 bool &IsDefault) const {
266 DataRefImpl Symb = SymRef.getRawDataRefImpl();
267 const Elf_Sym *symb = getSymbol(Symb);
268 ErrorOr<StringRef> Ver =
269 EF.getSymbolVersion(EF.getSection(Symb.d.b), symb, IsDefault);
271 return Ver.getError();
273 return std::error_code();
276 template <class ELFT>
277 uint64_t ELFObjectFile<ELFT>::getSectionFlags(SectionRef Sec) const {
278 DataRefImpl DRI = Sec.getRawDataRefImpl();
279 return toELFShdrIter(DRI)->sh_flags;
282 template <class ELFT>
283 uint32_t ELFObjectFile<ELFT>::getSectionType(SectionRef Sec) const {
284 DataRefImpl DRI = Sec.getRawDataRefImpl();
285 return toELFShdrIter(DRI)->sh_type;
288 template <class ELFT>
289 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
290 uint64_t &Result) const {
291 const Elf_Sym *ESym = getSymbol(Symb);
292 switch (EF.getSymbolTableIndex(ESym)) {
293 case ELF::SHN_COMMON:
295 Result = UnknownAddressOrSize;
296 return std::error_code();
298 Result = ESym->st_value;
299 return std::error_code();
304 const Elf_Ehdr *Header = EF.getHeader();
305 Result = ESym->st_value;
307 // Clear the ARM/Thumb or microMIPS indicator flag.
308 if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
309 ESym->getType() == ELF::STT_FUNC)
312 if (Header->e_type == ELF::ET_REL) {
313 const typename ELFFile<ELFT>::Elf_Shdr * Section = EF.getSection(ESym);
314 if (Section != nullptr)
315 Result += Section->sh_addr;
318 return std::error_code();
321 template <class ELFT>
322 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
323 Elf_Sym_Iter Sym = toELFSymIter(Symb);
324 if (Sym->st_shndx == ELF::SHN_COMMON)
325 return Sym->st_value;
329 template <class ELFT>
330 uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb) const {
331 return toELFSymIter(Symb)->st_size;
334 template <class ELFT>
335 std::error_code ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb,
336 uint8_t &Result) const {
337 Result = toELFSymIter(Symb)->st_other;
338 return std::error_code();
341 template <class ELFT>
343 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
344 SymbolRef::Type &Result) const {
345 const Elf_Sym *ESym = getSymbol(Symb);
347 switch (ESym->getType()) {
348 case ELF::STT_NOTYPE:
349 Result = SymbolRef::ST_Unknown;
351 case ELF::STT_SECTION:
352 Result = SymbolRef::ST_Debug;
355 Result = SymbolRef::ST_File;
358 Result = SymbolRef::ST_Function;
360 case ELF::STT_OBJECT:
361 case ELF::STT_COMMON:
363 Result = SymbolRef::ST_Data;
366 Result = SymbolRef::ST_Other;
369 return std::error_code();
372 template <class ELFT>
373 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const {
374 Elf_Sym_Iter EIter = toELFSymIter(Symb);
375 const Elf_Sym *ESym = &*EIter;
377 uint32_t Result = SymbolRef::SF_None;
379 if (ESym->getBinding() != ELF::STB_LOCAL)
380 Result |= SymbolRef::SF_Global;
382 if (ESym->getBinding() == ELF::STB_WEAK)
383 Result |= SymbolRef::SF_Weak;
385 if (ESym->st_shndx == ELF::SHN_ABS)
386 Result |= SymbolRef::SF_Absolute;
388 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
389 EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols())
390 Result |= SymbolRef::SF_FormatSpecific;
392 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
393 Result |= SymbolRef::SF_Undefined;
395 if (ESym->getType() == ELF::STT_COMMON ||
396 EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
397 Result |= SymbolRef::SF_Common;
399 if (isExportedToOtherDSO(ESym))
400 Result |= SymbolRef::SF_Exported;
402 if (ESym->getVisibility() == ELF::STV_HIDDEN)
403 Result |= SymbolRef::SF_Hidden;
408 template <class ELFT>
410 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
411 const Elf_Shdr *ESec = EF.getSection(ESym);
413 return section_end();
416 Sec.p = reinterpret_cast<intptr_t>(ESec);
417 return section_iterator(SectionRef(Sec, this));
421 template <class ELFT>
423 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
424 section_iterator &Res) const {
425 Res = getSymbolSection(getSymbol(Symb));
426 return std::error_code();
429 template <class ELFT>
430 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
431 Sec = toDRI(++toELFShdrIter(Sec));
434 template <class ELFT>
435 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
436 StringRef &Result) const {
437 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
439 return Name.getError();
441 return std::error_code();
444 template <class ELFT>
445 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
446 return toELFShdrIter(Sec)->sh_addr;
449 template <class ELFT>
450 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
451 return toELFShdrIter(Sec)->sh_size;
454 template <class ELFT>
456 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
457 StringRef &Result) const {
458 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
459 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
460 return std::error_code();
463 template <class ELFT>
464 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
465 return toELFShdrIter(Sec)->sh_addralign;
468 template <class ELFT>
469 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
470 return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
473 template <class ELFT>
474 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
475 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
476 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
477 EShdr->sh_type == ELF::SHT_PROGBITS;
480 template <class ELFT>
481 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
482 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
483 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
484 EShdr->sh_type == ELF::SHT_NOBITS;
487 template <class ELFT>
488 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
489 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
492 template <class ELFT>
493 bool ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
494 DataRefImpl Symb) const {
495 Elf_Sym_Iter ESym = toELFSymIter(Symb);
497 uintX_t Index = ESym->st_shndx;
498 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
500 return !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
503 template <class ELFT>
505 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
507 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
508 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
510 return relocation_iterator(RelocationRef(RelData, this));
513 template <class ELFT>
515 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
517 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
518 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
519 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
520 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
523 RelData.d.b = S->sh_size / S->sh_entsize;
525 return relocation_iterator(RelocationRef(RelData, this));
528 template <class ELFT>
530 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
531 if (EF.getHeader()->e_type != ELF::ET_REL)
532 return section_end();
534 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
535 uintX_t Type = EShdr->sh_type;
536 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
537 return section_end();
539 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
540 return section_iterator(SectionRef(toDRI(R), this));
544 template <class ELFT>
545 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
549 template <class ELFT>
551 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
553 const Elf_Shdr *sec = getRelSection(Rel);
554 switch (sec->sh_type) {
556 report_fatal_error("Invalid section type in Rel!");
558 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
561 case ELF::SHT_RELA: {
562 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
569 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
571 DataRefImpl SymbolData;
572 switch (SymSec->sh_type) {
574 report_fatal_error("Invalid symbol table section type!");
575 case ELF::SHT_SYMTAB:
576 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
578 case ELF::SHT_DYNSYM:
579 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
583 return symbol_iterator(SymbolRef(SymbolData, this));
586 template <class ELFT>
588 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
589 uint64_t &Result) const {
590 uint64_t ROffset = getROffset(Rel);
591 const Elf_Ehdr *Header = EF.getHeader();
593 if (Header->e_type == ELF::ET_REL) {
594 const Elf_Shdr *RelocationSec = getRelSection(Rel);
595 const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info);
596 Result = ROffset + RelocatedSec->sh_addr;
601 return std::error_code();
604 template <class ELFT>
606 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
607 uint64_t &Result) const {
608 assert(EF.getHeader()->e_type == ELF::ET_REL &&
609 "Only relocatable object files have relocation offsets");
610 Result = getROffset(Rel);
611 return std::error_code();
614 template <class ELFT>
615 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
616 const Elf_Shdr *sec = getRelSection(Rel);
617 switch (sec->sh_type) {
619 report_fatal_error("Invalid section type in Rel!");
621 return getRel(Rel)->r_offset;
623 return getRela(Rel)->r_offset;
627 template <class ELFT>
628 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
629 uint64_t &Result) const {
630 const Elf_Shdr *sec = getRelSection(Rel);
631 switch (sec->sh_type) {
633 report_fatal_error("Invalid section type in Rel!");
635 Result = getRel(Rel)->getType(EF.isMips64EL());
638 case ELF::SHT_RELA: {
639 Result = getRela(Rel)->getType(EF.isMips64EL());
643 return std::error_code();
646 template <class ELFT>
647 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
648 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
651 template <class ELFT>
652 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName(
653 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
654 const Elf_Shdr *sec = getRelSection(Rel);
656 switch (sec->sh_type) {
658 return object_error::parse_failed;
660 type = getRel(Rel)->getType(EF.isMips64EL());
663 case ELF::SHT_RELA: {
664 type = getRela(Rel)->getType(EF.isMips64EL());
669 EF.getRelocationTypeName(type, Result);
670 return std::error_code();
673 template <class ELFT>
675 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
676 int64_t &Result) const {
677 const Elf_Shdr *sec = getRelSection(Rel);
678 switch (sec->sh_type) {
680 report_fatal_error("Invalid section type in Rel!");
683 return std::error_code();
685 case ELF::SHT_RELA: {
686 Result = getRela(Rel)->r_addend;
687 return std::error_code();
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::error_code getELFRelocationAddend(const RelocationRef R,
869 const ObjectFile *Obj = R.getObjectFile();
870 DataRefImpl DRI = R.getRawDataRefImpl();
871 return cast<ELFObjectFileBase>(Obj)->getRelocationAddend(DRI, Addend);
874 inline std::pair<symbol_iterator, symbol_iterator>
875 getELFDynamicSymbolIterators(const SymbolicFile *Obj) {
876 return cast<ELFObjectFileBase>(Obj)->getELFDynamicSymbolIterators();
879 inline std::error_code GetELFSymbolVersion(const ObjectFile *Obj,
880 const SymbolRef &Sym,
883 return cast<ELFObjectFileBase>(Obj)
884 ->getSymbolVersion(Sym, Version, IsDefault);
886 } // namespace object