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/ArrayRef.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/MC/SubtargetFeature.h"
23 #include "llvm/Object/Binary.h"
24 #include "llvm/Object/ELF.h"
25 #include "llvm/Object/ELFTypes.h"
26 #include "llvm/Object/Error.h"
27 #include "llvm/Object/ObjectFile.h"
28 #include "llvm/Object/SymbolicFile.h"
29 #include "llvm/Support/Casting.h"
30 #include "llvm/Support/ELF.h"
31 #include "llvm/Support/Endian.h"
32 #include "llvm/Support/Error.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/ErrorOr.h"
35 #include "llvm/Support/MemoryBuffer.h"
38 #include <system_error>
43 class elf_symbol_iterator;
45 class ELFRelocationRef;
47 class ELFObjectFileBase : public ObjectFile {
48 friend class ELFSymbolRef;
49 friend class ELFSectionRef;
50 friend class ELFRelocationRef;
53 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
55 virtual uint16_t getEMachine() const = 0;
56 virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
57 virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
58 virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
60 virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
61 virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
63 virtual ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0;
66 typedef iterator_range<elf_symbol_iterator> elf_symbol_iterator_range;
67 virtual elf_symbol_iterator_range getDynamicSymbolIterators() const = 0;
69 elf_symbol_iterator_range symbols() const;
71 static inline bool classof(const Binary *v) { return v->isELF(); }
73 SubtargetFeatures getFeatures() const override;
76 class ELFSectionRef : public SectionRef {
78 ELFSectionRef(const SectionRef &B) : SectionRef(B) {
79 assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
82 const ELFObjectFileBase *getObject() const {
83 return cast<ELFObjectFileBase>(SectionRef::getObject());
86 uint32_t getType() const {
87 return getObject()->getSectionType(getRawDataRefImpl());
90 uint64_t getFlags() const {
91 return getObject()->getSectionFlags(getRawDataRefImpl());
95 class elf_section_iterator : public section_iterator {
97 elf_section_iterator(const section_iterator &B) : section_iterator(B) {
98 assert(isa<ELFObjectFileBase>(B->getObject()));
101 const ELFSectionRef *operator->() const {
102 return static_cast<const ELFSectionRef *>(section_iterator::operator->());
105 const ELFSectionRef &operator*() const {
106 return static_cast<const ELFSectionRef &>(section_iterator::operator*());
110 class ELFSymbolRef : public SymbolRef {
112 ELFSymbolRef(const SymbolRef &B) : SymbolRef(B) {
113 assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
116 const ELFObjectFileBase *getObject() const {
117 return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
120 uint64_t getSize() const {
121 return getObject()->getSymbolSize(getRawDataRefImpl());
124 uint8_t getOther() const {
125 return getObject()->getSymbolOther(getRawDataRefImpl());
128 uint8_t getELFType() const {
129 return getObject()->getSymbolELFType(getRawDataRefImpl());
133 class elf_symbol_iterator : public symbol_iterator {
135 elf_symbol_iterator(const basic_symbol_iterator &B)
136 : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
137 cast<ELFObjectFileBase>(B->getObject()))) {}
139 const ELFSymbolRef *operator->() const {
140 return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
143 const ELFSymbolRef &operator*() const {
144 return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
148 class ELFRelocationRef : public RelocationRef {
150 ELFRelocationRef(const RelocationRef &B) : RelocationRef(B) {
151 assert(isa<ELFObjectFileBase>(RelocationRef::getObject()));
154 const ELFObjectFileBase *getObject() const {
155 return cast<ELFObjectFileBase>(RelocationRef::getObject());
158 ErrorOr<int64_t> getAddend() const {
159 return getObject()->getRelocationAddend(getRawDataRefImpl());
163 class elf_relocation_iterator : public relocation_iterator {
165 elf_relocation_iterator(const relocation_iterator &B)
166 : relocation_iterator(RelocationRef(
167 B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {}
169 const ELFRelocationRef *operator->() const {
170 return static_cast<const ELFRelocationRef *>(
171 relocation_iterator::operator->());
174 const ELFRelocationRef &operator*() const {
175 return static_cast<const ELFRelocationRef &>(
176 relocation_iterator::operator*());
180 inline ELFObjectFileBase::elf_symbol_iterator_range
181 ELFObjectFileBase::symbols() const {
182 return elf_symbol_iterator_range(symbol_begin(), symbol_end());
185 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
186 uint16_t getEMachine() const override;
187 uint64_t getSymbolSize(DataRefImpl Sym) const override;
190 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
192 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
194 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
195 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
196 typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
197 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
198 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
199 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
204 const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
205 const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
206 ArrayRef<Elf_Word> ShndxTable;
208 void moveSymbolNext(DataRefImpl &Symb) const override;
209 Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
210 Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
211 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
212 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
213 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
214 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
215 uint8_t getSymbolOther(DataRefImpl Symb) const override;
216 uint8_t getSymbolELFType(DataRefImpl Symb) const override;
217 Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
218 Expected<section_iterator> getSymbolSection(const Elf_Sym *Symb,
219 const Elf_Shdr *SymTab) const;
220 Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
222 void moveSectionNext(DataRefImpl &Sec) const override;
223 std::error_code getSectionName(DataRefImpl Sec,
224 StringRef &Res) const override;
225 uint64_t getSectionAddress(DataRefImpl Sec) const override;
226 uint64_t getSectionSize(DataRefImpl Sec) const override;
227 std::error_code getSectionContents(DataRefImpl Sec,
228 StringRef &Res) const override;
229 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
230 bool isSectionCompressed(DataRefImpl Sec) const override;
231 bool isSectionText(DataRefImpl Sec) const override;
232 bool isSectionData(DataRefImpl Sec) const override;
233 bool isSectionBSS(DataRefImpl Sec) const override;
234 bool isSectionVirtual(DataRefImpl Sec) const override;
235 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
236 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
237 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
239 void moveRelocationNext(DataRefImpl &Rel) const override;
240 uint64_t getRelocationOffset(DataRefImpl Rel) const override;
241 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
242 uint64_t getRelocationType(DataRefImpl Rel) const override;
243 void getRelocationTypeName(DataRefImpl Rel,
244 SmallVectorImpl<char> &Result) const override;
246 uint32_t getSectionType(DataRefImpl Sec) const override;
247 uint64_t getSectionFlags(DataRefImpl Sec) const override;
248 StringRef getRelocationTypeName(uint32_t Type) const;
250 /// \brief Get the relocation section that contains \a Rel.
251 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
252 return *EF.getSection(Rel.d.a);
255 DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
262 assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
263 SymTable->sh_type == ELF::SHT_DYNSYM);
265 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin());
266 unsigned SymTableIndex =
267 (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
269 DRI.d.a = SymTableIndex;
274 const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
275 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
278 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
280 DRI.p = reinterpret_cast<uintptr_t>(Sec);
284 DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
286 DRI.p = reinterpret_cast<uintptr_t>(Dyn);
290 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
291 unsigned char Binding = ESym->getBinding();
292 unsigned char Visibility = ESym->getVisibility();
294 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
295 // visibility is either DEFAULT or PROTECTED. All other symbols are not
297 return ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
298 (Visibility == ELF::STV_DEFAULT ||
299 Visibility == ELF::STV_PROTECTED));
302 // This flag is used for classof, to distinguish ELFObjectFile from
303 // its subclass. If more subclasses will be created, this flag will
304 // have to become an enum.
305 bool isDyldELFObject;
308 ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
310 const Elf_Rel *getRel(DataRefImpl Rel) const;
311 const Elf_Rela *getRela(DataRefImpl Rela) const;
313 const Elf_Sym *getSymbol(DataRefImpl Sym) const {
314 return EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
317 const Elf_Shdr *getSection(DataRefImpl Sec) const {
318 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
321 basic_symbol_iterator symbol_begin_impl() const override;
322 basic_symbol_iterator symbol_end_impl() const override;
324 elf_symbol_iterator dynamic_symbol_begin() const;
325 elf_symbol_iterator dynamic_symbol_end() const;
327 section_iterator section_begin() const override;
328 section_iterator section_end() const override;
330 ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const override;
332 uint8_t getBytesInAddress() const override;
333 StringRef getFileFormatName() const override;
334 unsigned getArch() const override;
336 std::error_code getPlatformFlags(unsigned &Result) const override {
337 Result = EF.getHeader()->e_flags;
338 return std::error_code();
341 const ELFFile<ELFT> *getELFFile() const { return &EF; }
343 bool isDyldType() const { return isDyldELFObject; }
344 static inline bool classof(const Binary *v) {
345 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
349 elf_symbol_iterator_range getDynamicSymbolIterators() const override;
351 bool isRelocatableObject() const override;
354 typedef ELFObjectFile<ELFType<support::little, false>> ELF32LEObjectFile;
355 typedef ELFObjectFile<ELFType<support::little, true>> ELF64LEObjectFile;
356 typedef ELFObjectFile<ELFType<support::big, false>> ELF32BEObjectFile;
357 typedef ELFObjectFile<ELFType<support::big, true>> ELF64BEObjectFile;
359 template <class ELFT>
360 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Sym) const {
364 template <class ELFT>
365 Expected<StringRef> ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Sym) const {
366 const Elf_Sym *ESym = getSymbol(Sym);
367 const Elf_Shdr *SymTableSec = *EF.getSection(Sym.d.a);
368 const Elf_Shdr *StringTableSec = *EF.getSection(SymTableSec->sh_link);
369 StringRef SymTable = *EF.getStringTable(StringTableSec);
370 return ESym->getName(SymTable);
373 template <class ELFT>
374 uint64_t ELFObjectFile<ELFT>::getSectionFlags(DataRefImpl Sec) const {
375 return getSection(Sec)->sh_flags;
378 template <class ELFT>
379 uint32_t ELFObjectFile<ELFT>::getSectionType(DataRefImpl Sec) const {
380 return getSection(Sec)->sh_type;
383 template <class ELFT>
384 uint64_t ELFObjectFile<ELFT>::getSymbolValueImpl(DataRefImpl Symb) const {
385 const Elf_Sym *ESym = getSymbol(Symb);
386 uint64_t Ret = ESym->st_value;
387 if (ESym->st_shndx == ELF::SHN_ABS)
390 const Elf_Ehdr *Header = EF.getHeader();
391 // Clear the ARM/Thumb or microMIPS indicator flag.
392 if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
393 ESym->getType() == ELF::STT_FUNC)
399 template <class ELFT>
401 ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb) const {
402 uint64_t Result = getSymbolValue(Symb);
403 const Elf_Sym *ESym = getSymbol(Symb);
404 switch (ESym->st_shndx) {
405 case ELF::SHN_COMMON:
411 const Elf_Ehdr *Header = EF.getHeader();
412 const Elf_Shdr *SymTab = *EF.getSection(Symb.d.a);
414 if (Header->e_type == ELF::ET_REL) {
415 ErrorOr<const Elf_Shdr *> SectionOrErr =
416 EF.getSection(ESym, SymTab, ShndxTable);
417 if (std::error_code EC = SectionOrErr.getError())
418 return errorCodeToError(EC);
419 const Elf_Shdr *Section = *SectionOrErr;
421 Result += Section->sh_addr;
427 template <class ELFT>
428 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
429 const Elf_Sym *Sym = getSymbol(Symb);
430 if (Sym->st_shndx == ELF::SHN_COMMON)
431 return Sym->st_value;
435 template <class ELFT>
436 uint16_t ELFObjectFile<ELFT>::getEMachine() const {
437 return EF.getHeader()->e_machine;
440 template <class ELFT>
441 uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
442 return getSymbol(Sym)->st_size;
445 template <class ELFT>
446 uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
447 return getSymbol(Symb)->st_size;
450 template <class ELFT>
451 uint8_t ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb) const {
452 return getSymbol(Symb)->st_other;
455 template <class ELFT>
456 uint8_t ELFObjectFile<ELFT>::getSymbolELFType(DataRefImpl Symb) const {
457 return getSymbol(Symb)->getType();
460 template <class ELFT>
461 Expected<SymbolRef::Type>
462 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const {
463 const Elf_Sym *ESym = getSymbol(Symb);
465 switch (ESym->getType()) {
466 case ELF::STT_NOTYPE:
467 return SymbolRef::ST_Unknown;
468 case ELF::STT_SECTION:
469 return SymbolRef::ST_Debug;
471 return SymbolRef::ST_File;
473 return SymbolRef::ST_Function;
474 case ELF::STT_OBJECT:
475 case ELF::STT_COMMON:
477 return SymbolRef::ST_Data;
479 return SymbolRef::ST_Other;
483 template <class ELFT>
484 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const {
485 const Elf_Sym *ESym = getSymbol(Sym);
487 uint32_t Result = SymbolRef::SF_None;
489 if (ESym->getBinding() != ELF::STB_LOCAL)
490 Result |= SymbolRef::SF_Global;
492 if (ESym->getBinding() == ELF::STB_WEAK)
493 Result |= SymbolRef::SF_Weak;
495 if (ESym->st_shndx == ELF::SHN_ABS)
496 Result |= SymbolRef::SF_Absolute;
498 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
499 ESym == EF.symbol_begin(DotSymtabSec) ||
500 ESym == EF.symbol_begin(DotDynSymSec))
501 Result |= SymbolRef::SF_FormatSpecific;
503 if (EF.getHeader()->e_machine == ELF::EM_ARM) {
504 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
505 StringRef Name = *NameOrErr;
506 if (Name.startswith("$d") || Name.startswith("$t") ||
507 Name.startswith("$a"))
508 Result |= SymbolRef::SF_FormatSpecific;
510 // TODO: Actually report errors helpfully.
511 consumeError(NameOrErr.takeError());
513 if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1)
514 Result |= SymbolRef::SF_Thumb;
517 if (ESym->st_shndx == ELF::SHN_UNDEF)
518 Result |= SymbolRef::SF_Undefined;
520 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
521 Result |= SymbolRef::SF_Common;
523 if (isExportedToOtherDSO(ESym))
524 Result |= SymbolRef::SF_Exported;
526 if (ESym->getVisibility() == ELF::STV_HIDDEN)
527 Result |= SymbolRef::SF_Hidden;
532 template <class ELFT>
533 Expected<section_iterator>
534 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym,
535 const Elf_Shdr *SymTab) const {
536 ErrorOr<const Elf_Shdr *> ESecOrErr = EF.getSection(ESym, SymTab, ShndxTable);
537 if (std::error_code EC = ESecOrErr.getError())
538 return errorCodeToError(EC);
540 const Elf_Shdr *ESec = *ESecOrErr;
542 return section_end();
545 Sec.p = reinterpret_cast<intptr_t>(ESec);
546 return section_iterator(SectionRef(Sec, this));
549 template <class ELFT>
550 Expected<section_iterator>
551 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb) const {
552 const Elf_Sym *Sym = getSymbol(Symb);
553 const Elf_Shdr *SymTab = *EF.getSection(Symb.d.a);
554 return getSymbolSection(Sym, SymTab);
557 template <class ELFT>
558 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
559 const Elf_Shdr *ESec = getSection(Sec);
563 template <class ELFT>
564 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
565 StringRef &Result) const {
566 ErrorOr<StringRef> Name = EF.getSectionName(&*getSection(Sec));
568 return Name.getError();
570 return std::error_code();
573 template <class ELFT>
574 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
575 return getSection(Sec)->sh_addr;
578 template <class ELFT>
579 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
580 return getSection(Sec)->sh_size;
583 template <class ELFT>
585 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
586 StringRef &Result) const {
587 const Elf_Shdr *EShdr = getSection(Sec);
588 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
589 return std::error_code();
592 template <class ELFT>
593 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
594 return getSection(Sec)->sh_addralign;
597 template <class ELFT>
598 bool ELFObjectFile<ELFT>::isSectionCompressed(DataRefImpl Sec) const {
599 return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED;
602 template <class ELFT>
603 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
604 return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR;
607 template <class ELFT>
608 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
609 const Elf_Shdr *EShdr = getSection(Sec);
610 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
611 EShdr->sh_type == ELF::SHT_PROGBITS;
614 template <class ELFT>
615 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
616 const Elf_Shdr *EShdr = getSection(Sec);
617 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
618 EShdr->sh_type == ELF::SHT_NOBITS;
621 template <class ELFT>
622 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
623 return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
626 template <class ELFT>
628 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
630 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin());
631 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
633 return relocation_iterator(RelocationRef(RelData, this));
636 template <class ELFT>
638 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
639 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
640 relocation_iterator Begin = section_rel_begin(Sec);
641 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
643 DataRefImpl RelData = Begin->getRawDataRefImpl();
644 const Elf_Shdr *RelSec = getRelSection(RelData);
646 // Error check sh_link here so that getRelocationSymbol can just use it.
647 ErrorOr<const Elf_Shdr *> SymSecOrErr = EF.getSection(RelSec->sh_link);
648 if (std::error_code EC = SymSecOrErr.getError())
649 report_fatal_error(EC.message());
651 RelData.d.b += S->sh_size / S->sh_entsize;
652 return relocation_iterator(RelocationRef(RelData, this));
655 template <class ELFT>
657 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
658 if (EF.getHeader()->e_type != ELF::ET_REL)
659 return section_end();
661 const Elf_Shdr *EShdr = getSection(Sec);
662 uintX_t Type = EShdr->sh_type;
663 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
664 return section_end();
666 ErrorOr<const Elf_Shdr *> R = EF.getSection(EShdr->sh_info);
667 if (std::error_code EC = R.getError())
668 report_fatal_error(EC.message());
669 return section_iterator(SectionRef(toDRI(*R), this));
673 template <class ELFT>
674 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
678 template <class ELFT>
680 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
682 const Elf_Shdr *sec = getRelSection(Rel);
683 if (sec->sh_type == ELF::SHT_REL)
684 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
686 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
690 // FIXME: error check symbolIdx
691 DataRefImpl SymbolData;
692 SymbolData.d.a = sec->sh_link;
693 SymbolData.d.b = symbolIdx;
694 return symbol_iterator(SymbolRef(SymbolData, this));
697 template <class ELFT>
698 uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const {
699 assert(EF.getHeader()->e_type == ELF::ET_REL &&
700 "Only relocatable object files have relocation offsets");
701 const Elf_Shdr *sec = getRelSection(Rel);
702 if (sec->sh_type == ELF::SHT_REL)
703 return getRel(Rel)->r_offset;
705 return getRela(Rel)->r_offset;
708 template <class ELFT>
709 uint64_t ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel) const {
710 const Elf_Shdr *sec = getRelSection(Rel);
711 if (sec->sh_type == ELF::SHT_REL)
712 return getRel(Rel)->getType(EF.isMips64EL());
714 return getRela(Rel)->getType(EF.isMips64EL());
717 template <class ELFT>
718 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
719 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
722 template <class ELFT>
723 void ELFObjectFile<ELFT>::getRelocationTypeName(
724 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
725 uint32_t type = getRelocationType(Rel);
726 EF.getRelocationTypeName(type, Result);
729 template <class ELFT>
731 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
732 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
733 return object_error::parse_failed;
734 return (int64_t)getRela(Rel)->r_addend;
737 template <class ELFT>
738 const typename ELFObjectFile<ELFT>::Elf_Rel *
739 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
740 assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
741 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
744 template <class ELFT>
745 const typename ELFObjectFile<ELFT>::Elf_Rela *
746 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
747 assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
748 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
751 template <class ELFT>
752 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
754 getELFType(ELFT::TargetEndianness == support::little, ELFT::Is64Bits),
756 EF(Data.getBuffer(), EC) {
759 for (const Elf_Shdr &Sec : EF.sections()) {
760 switch (Sec.sh_type) {
761 case ELF::SHT_DYNSYM: {
763 // More than one .dynsym!
764 EC = object_error::parse_failed;
770 case ELF::SHT_SYMTAB: {
772 // More than one .dynsym!
773 EC = object_error::parse_failed;
779 case ELF::SHT_SYMTAB_SHNDX: {
780 ErrorOr<ArrayRef<Elf_Word>> TableOrErr = EF.getSHNDXTable(Sec);
781 if ((EC = TableOrErr.getError()))
783 ShndxTable = *TableOrErr;
790 template <class ELFT>
791 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
792 DataRefImpl Sym = toDRI(DotSymtabSec, 0);
793 return basic_symbol_iterator(SymbolRef(Sym, this));
796 template <class ELFT>
797 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
798 const Elf_Shdr *SymTab = DotSymtabSec;
800 return symbol_begin_impl();
801 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
802 return basic_symbol_iterator(SymbolRef(Sym, this));
805 template <class ELFT>
806 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
807 DataRefImpl Sym = toDRI(DotDynSymSec, 0);
808 return symbol_iterator(SymbolRef(Sym, this));
811 template <class ELFT>
812 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
813 const Elf_Shdr *SymTab = DotDynSymSec;
814 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
815 return basic_symbol_iterator(SymbolRef(Sym, this));
818 template <class ELFT>
819 section_iterator ELFObjectFile<ELFT>::section_begin() const {
820 return section_iterator(SectionRef(toDRI(EF.section_begin()), this));
823 template <class ELFT>
824 section_iterator ELFObjectFile<ELFT>::section_end() const {
825 return section_iterator(SectionRef(toDRI(EF.section_end()), this));
828 template <class ELFT>
829 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
830 return ELFT::Is64Bits ? 8 : 4;
833 template <class ELFT>
834 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
835 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
836 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
837 case ELF::ELFCLASS32:
838 switch (EF.getHeader()->e_machine) {
842 return "ELF32-iamcu";
844 return "ELF32-x86-64";
846 return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
849 case ELF::EM_HEXAGON:
850 return "ELF32-hexagon";
852 return "ELF32-lanai";
858 case ELF::EM_SPARC32PLUS:
859 return "ELF32-sparc";
860 case ELF::EM_WEBASSEMBLY:
863 return "ELF32-amdgpu";
865 return "ELF32-unknown";
867 case ELF::ELFCLASS64:
868 switch (EF.getHeader()->e_machine) {
872 return "ELF64-x86-64";
873 case ELF::EM_AARCH64:
874 return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
876 return "ELF64-ppc64";
879 case ELF::EM_SPARCV9:
880 return "ELF64-sparc";
883 case ELF::EM_WEBASSEMBLY:
886 return (EF.getHeader()->e_ident[ELF::EI_OSABI] == ELF::ELFOSABI_AMDGPU_HSA
888 "ELF64-amdgpu-hsacobj" : "ELF64-amdgpu";
892 return "ELF64-unknown";
895 // FIXME: Proper error handling.
896 report_fatal_error("Invalid ELFCLASS!");
900 template <class ELFT>
901 unsigned ELFObjectFile<ELFT>::getArch() const {
902 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
903 switch (EF.getHeader()->e_machine) {
908 return Triple::x86_64;
909 case ELF::EM_AARCH64:
910 return Triple::aarch64;
915 case ELF::EM_HEXAGON:
916 return Triple::hexagon;
918 return Triple::lanai;
920 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
921 case ELF::ELFCLASS32:
922 return IsLittleEndian ? Triple::mipsel : Triple::mips;
923 case ELF::ELFCLASS64:
924 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
926 report_fatal_error("Invalid ELFCLASS!");
931 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
933 return Triple::systemz;
936 case ELF::EM_SPARC32PLUS:
937 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
938 case ELF::EM_SPARCV9:
939 return Triple::sparcv9;
940 case ELF::EM_WEBASSEMBLY:
941 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
942 case ELF::ELFCLASS32: return Triple::wasm32;
943 case ELF::ELFCLASS64: return Triple::wasm64;
944 default: return Triple::UnknownArch;
948 return (EF.getHeader()->e_ident[ELF::EI_CLASS] == ELF::ELFCLASS64
949 && EF.getHeader()->e_ident[ELF::EI_OSABI] == ELF::ELFOSABI_AMDGPU_HSA
951 Triple::amdgcn : Triple::UnknownArch;
954 return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
957 return Triple::UnknownArch;
961 template <class ELFT>
962 ELFObjectFileBase::elf_symbol_iterator_range
963 ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
964 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
967 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
968 return EF.getHeader()->e_type == ELF::ET_REL;
971 } // end namespace object
972 } // end namespace llvm
974 #endif // LLVM_OBJECT_ELFOBJECTFILE_H