LLVM 23.0.0git
ELFObjectFile.h
Go to the documentation of this file.
1//===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the ELFObjectFile template class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_OBJECT_ELFOBJECTFILE_H
14#define LLVM_OBJECT_ELFOBJECTFILE_H
15
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/Enum.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/StringRef.h"
23#include "llvm/Object/Binary.h"
24#include "llvm/Object/ELF.h"
26#include "llvm/Object/Error.h"
33#include "llvm/Support/Error.h"
35#include "llvm/Support/LEB128.h"
39#include <cassert>
40#include <cstdint>
41
42namespace llvm {
43
44template <typename T> class SmallVectorImpl;
45
46namespace object {
47
49
51
54 std::optional<DataRefImpl> Symbol;
56};
57
59 friend class ELFRelocationRef;
60 friend class ELFSectionRef;
61 friend class ELFSymbolRef;
62
63 SubtargetFeatures getMIPSFeatures() const;
64 SubtargetFeatures getARMFeatures() const;
65 SubtargetFeatures getHexagonFeatures() const;
66 Expected<SubtargetFeatures> getRISCVFeatures() const;
67 SubtargetFeatures getLoongArchFeatures() const;
68
69 StringRef getAMDGPUCPUName() const;
70 StringRef getNVPTXCPUName() const;
71
72protected:
73 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
74
75 virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
76 virtual uint8_t getSymbolBinding(DataRefImpl Symb) const = 0;
77 virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
78 virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
79
80 virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
81 virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
82 virtual uint64_t getSectionOffset(DataRefImpl Sec) const = 0;
83
85 virtual Error getBuildAttributes(ELFAttributeParser &Attributes) const = 0;
86
87public:
89
91
92 /// Returns platform-specific object flags, if any.
93 virtual unsigned getPlatformFlags() const = 0;
94
96
97 static bool classof(const Binary *v) { return v->isELF(); }
98
100
101 std::optional<StringRef> tryGetCPUName() const override;
102
103 void setARMSubArch(Triple &TheTriple) const override;
104
105 virtual uint16_t getEType() const = 0;
106
107 virtual uint16_t getEMachine() const = 0;
108
109 virtual uint8_t getEIdentABIVersion() const = 0;
110
111 virtual uint8_t getEIdentOSABI() const = 0;
112
113 std::vector<ELFPltEntry> getPltEntries(const MCSubtargetInfo &STI) const;
114
115 /// Returns a vector containing a symbol version for each dynamic symbol.
116 /// Returns an empty vector if version sections do not exist.
118
119 /// Returns a vector of all BB address maps in the object file. When
120 /// `TextSectionIndex` is specified, only returns the BB address maps
121 /// corresponding to the section with that index. When `PGOAnalyses`is
122 /// specified (PGOAnalyses is not nullptr), the vector is cleared then filled
123 /// with extra PGO data. `PGOAnalyses` will always be the same length as the
124 /// return value when it is requested assuming no error occurs. Upon failure,
125 /// `PGOAnalyses` will be emptied.
127 readBBAddrMap(std::optional<unsigned> TextSectionIndex = std::nullopt,
128 std::vector<PGOAnalysisMap> *PGOAnalyses = nullptr) const;
129
131};
132
155
157public:
161
162 const ELFSectionRef *operator->() const {
163 return static_cast<const ELFSectionRef *>(section_iterator::operator->());
164 }
165
166 const ELFSectionRef &operator*() const {
167 return static_cast<const ELFSectionRef &>(section_iterator::operator*());
168 }
169};
170
171class ELFSymbolRef : public SymbolRef {
172public:
176
180
183 }
184
188
191 }
192
196
198 return getElfSymbolTypes().toString(getELFType(), 1);
199 }
200};
201
202inline bool operator<(const ELFSymbolRef &A, const ELFSymbolRef &B) {
203 const DataRefImpl &DRIA = A.getRawDataRefImpl();
204 const DataRefImpl &DRIB = B.getRawDataRefImpl();
205 if (DRIA.d.a == DRIB.d.a)
206 return DRIA.d.b < DRIB.d.b;
207 return DRIA.d.a < DRIB.d.a;
208}
209
211public:
215
216 const ELFSymbolRef *operator->() const {
217 return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
218 }
219
220 const ELFSymbolRef &operator*() const {
221 return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
222 }
223};
224
239
241public:
245
247 return static_cast<const ELFRelocationRef *>(
249 }
250
252 return static_cast<const ELFRelocationRef &>(
254 }
255};
256
261
262template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
263 uint16_t getEMachine() const override;
264 uint16_t getEType() const override;
265 uint8_t getEIdentABIVersion() const override;
266 uint8_t getEIdentOSABI() const override;
267 uint64_t getSymbolSize(DataRefImpl Sym) const override;
268
269public:
271
272 SectionRef toSectionRef(const Elf_Shdr *Sec) const {
273 return SectionRef(toDRI(Sec), this);
274 }
275
276 ELFSymbolRef toSymbolRef(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
277 return ELFSymbolRef({toDRI(SymTable, SymbolNum), this});
278 }
279
280 bool IsContentValid() const { return ContentValid; }
281
282private:
284 const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,
285 const Elf_Shdr *DotSymtabShndxSec);
286
287 bool ContentValid = false;
288
289protected:
291
292 const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
293 const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
294 const Elf_Shdr *DotSymtabShndxSec = nullptr; // SHT_SYMTAB_SHNDX section.
295
296 // Hold CREL relocations for SectionRef::relocations().
299
300 Error initContent() override;
301
302 void moveSymbolNext(DataRefImpl &Symb) const override;
310 uint8_t getSymbolOther(DataRefImpl Symb) const override;
314 const Elf_Shdr *SymTab) const;
316
317 void moveSectionNext(DataRefImpl &Sec) const override;
323 getSectionContents(DataRefImpl Sec) const override;
325 bool isSectionCompressed(DataRefImpl Sec) const override;
326 bool isSectionText(DataRefImpl Sec) const override;
327 bool isSectionData(DataRefImpl Sec) const override;
328 bool isSectionBSS(DataRefImpl Sec) const override;
329 bool isSectionVirtual(DataRefImpl Sec) const override;
330 bool isBerkeleyText(DataRefImpl Sec) const override;
331 bool isBerkeleyData(DataRefImpl Sec) const override;
332 bool isDebugSection(DataRefImpl Sec) const override;
335 std::vector<SectionRef> dynamic_relocation_sections() const override;
337 getRelocatedSection(DataRefImpl Sec) const override;
338
339 void moveRelocationNext(DataRefImpl &Rel) const override;
344 SmallVectorImpl<char> &Result) const override;
345
350
351 DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
352 DataRefImpl DRI;
353 if (!SymTable) {
354 DRI.d.a = 0;
355 DRI.d.b = 0;
356 return DRI;
357 }
358 assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
359 SymTable->sh_type == ELF::SHT_DYNSYM);
360
361 auto SectionsOrErr = EF.sections();
362 if (!SectionsOrErr) {
363 DRI.d.a = 0;
364 DRI.d.b = 0;
365 return DRI;
366 }
367 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
368 unsigned SymTableIndex =
369 (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
370
371 DRI.d.a = SymTableIndex;
372 DRI.d.b = SymbolNum;
373 return DRI;
374 }
375
376 const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
377 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
378 }
379
380 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
381 DataRefImpl DRI;
382 DRI.p = reinterpret_cast<uintptr_t>(Sec);
383 return DRI;
384 }
385
386 DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
387 DataRefImpl DRI;
388 DRI.p = reinterpret_cast<uintptr_t>(Dyn);
389 return DRI;
390 }
391
392 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
393 unsigned char Binding = ESym->getBinding();
394 unsigned char Visibility = ESym->getVisibility();
395
396 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
397 // visibility is either DEFAULT or PROTECTED. All other symbols are not
398 // exported.
399 return (
402 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED));
403 }
404
405 Error getBuildAttributes(ELFAttributeParser &Attributes) const override {
407 switch (getEMachine()) {
408 case ELF::EM_ARM:
410 break;
411 case ELF::EM_AARCH64:
413 break;
414 case ELF::EM_RISCV:
416 break;
417 case ELF::EM_HEXAGON:
419 break;
420 default:
421 return Error::success();
422 }
423
424 auto SectionsOrErr = EF.sections();
425 if (!SectionsOrErr)
426 return SectionsOrErr.takeError();
427 for (const Elf_Shdr &Sec : *SectionsOrErr) {
428 if (Sec.sh_type != Type)
429 continue;
430 auto ErrorOrContents = EF.getSectionContents(Sec);
431 if (!ErrorOrContents)
432 return ErrorOrContents.takeError();
433
434 auto Contents = ErrorOrContents.get();
435 if (Contents[0] != ELFAttrs::Format_Version || Contents.size() == 1)
436 return Error::success();
437
438 if (Error E = Attributes.parse(Contents, ELFT::Endianness))
439 return E;
440 break;
441 }
442 return Error::success();
443 }
444
445 // This flag is used for classof, to distinguish ELFObjectFile from
446 // its subclass. If more subclasses will be created, this flag will
447 // have to become an enum.
448 bool isDyldELFObject = false;
449
450public:
451 ELFObjectFile(ELFObjectFile<ELFT> &&Other);
453 bool InitContent = true);
454
455 const Elf_Rel *getRel(DataRefImpl Rel) const;
456 const Elf_Rela *getRela(DataRefImpl Rela) const;
457 Elf_Crel getCrel(DataRefImpl Crel) const;
458
460 return EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
461 }
462
463 /// Get the relocation section that contains \a Rel.
464 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
465 auto RelSecOrErr = EF.getSection(Rel.d.a);
466 if (!RelSecOrErr)
468 Twine(errorToErrorCode(RelSecOrErr.takeError()).message()));
469 return *RelSecOrErr;
470 }
471
472 const Elf_Shdr *getSection(DataRefImpl Sec) const {
473 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
474 }
475
478
479 bool is64Bit() const override { return getBytesInAddress() == 8; }
480
483
486
488
489 uint8_t getBytesInAddress() const override;
490 StringRef getFileFormatName() const override;
491 Triple::ArchType getArch() const override;
492 Triple::OSType getOS() const override;
494
495 unsigned getPlatformFlags() const override { return EF.getHeader().e_flags; }
496
497 const ELFFile<ELFT> &getELFFile() const { return EF; }
498
499 bool isDyldType() const { return isDyldELFObject; }
500 static bool classof(const Binary *v) {
501 return v->getType() ==
502 getELFType(ELFT::Endianness == llvm::endianness::little,
503 ELFT::Is64Bits);
504 }
505
507
508 bool isRelocatableObject() const override;
509
510 void createFakeSections() { EF.createFakeSections(); }
511
513};
514
519
520template <class ELFT>
522 ++Sym.d.b;
523}
524
526 auto SectionsOrErr = EF.sections();
527 if (!SectionsOrErr)
528 return SectionsOrErr.takeError();
529
530 for (const Elf_Shdr &Sec : *SectionsOrErr) {
531 switch (Sec.sh_type) {
532 case ELF::SHT_DYNSYM: {
533 if (!DotDynSymSec)
534 DotDynSymSec = &Sec;
535 break;
536 }
537 case ELF::SHT_SYMTAB: {
538 if (!DotSymtabSec)
539 DotSymtabSec = &Sec;
540 break;
541 }
544 DotSymtabShndxSec = &Sec;
545 break;
546 }
547 }
548 }
549
550 ContentValid = true;
551 return Error::success();
552}
553
554template <class ELFT>
556 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
557 if (!SymOrErr)
558 return SymOrErr.takeError();
559 auto SymTabOrErr = EF.getSection(Sym.d.a);
560 if (!SymTabOrErr)
561 return SymTabOrErr.takeError();
562 const Elf_Shdr *SymTableSec = *SymTabOrErr;
563 auto StrTabOrErr = EF.getSection(SymTableSec->sh_link);
564 if (!StrTabOrErr)
565 return StrTabOrErr.takeError();
566 const Elf_Shdr *StringTableSec = *StrTabOrErr;
567 auto SymStrTabOrErr = EF.getStringTable(*StringTableSec);
568 if (!SymStrTabOrErr)
569 return SymStrTabOrErr.takeError();
570 Expected<StringRef> Name = (*SymOrErr)->getName(*SymStrTabOrErr);
571 if (Name && !Name->empty())
572 return Name;
573
574 // If the symbol name is empty use the section name.
575 if ((*SymOrErr)->getType() == ELF::STT_SECTION) {
577 if (SecOrErr)
578 return (*SecOrErr)->getName();
579 return SecOrErr.takeError();
580 }
581 return Name;
582}
583
584template <class ELFT>
586 return getSection(Sec)->sh_flags;
587}
588
589template <class ELFT>
591 return getSection(Sec)->sh_type;
592}
593
594template <class ELFT>
596 return getSection(Sec)->sh_offset;
597}
598
599template <class ELFT>
601 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
602 if (!SymOrErr)
603 report_fatal_error(SymOrErr.takeError());
604
605 uint64_t Ret = (*SymOrErr)->st_value;
606 if ((*SymOrErr)->st_shndx == ELF::SHN_ABS)
607 return Ret;
608
609 const Elf_Ehdr &Header = EF.getHeader();
610 // Clear the ARM/Thumb or microMIPS indicator flag.
611 if ((Header.e_machine == ELF::EM_ARM || Header.e_machine == ELF::EM_MIPS) &&
612 (*SymOrErr)->getType() == ELF::STT_FUNC)
613 Ret &= ~1;
614
615 return Ret;
616}
617
618template <class ELFT>
621 Expected<uint64_t> SymbolValueOrErr = getSymbolValue(Symb);
622 if (!SymbolValueOrErr)
623 // TODO: Test this error.
624 return SymbolValueOrErr.takeError();
625
626 uint64_t Result = *SymbolValueOrErr;
627 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
628 if (!SymOrErr)
629 return SymOrErr.takeError();
630
631 switch ((*SymOrErr)->st_shndx) {
632 case ELF::SHN_COMMON:
633 case ELF::SHN_UNDEF:
634 case ELF::SHN_ABS:
635 return Result;
636 }
637
638 auto SymTabOrErr = EF.getSection(Symb.d.a);
639 if (!SymTabOrErr)
640 return SymTabOrErr.takeError();
641
642 if (EF.getHeader().e_type == ELF::ET_REL) {
643 ArrayRef<Elf_Word> ShndxTable;
644 if (DotSymtabShndxSec) {
645 // TODO: Test this error.
646 if (Expected<ArrayRef<Elf_Word>> ShndxTableOrErr =
647 EF.getSHNDXTable(*DotSymtabShndxSec))
648 ShndxTable = *ShndxTableOrErr;
649 else
650 return ShndxTableOrErr.takeError();
651 }
652
653 Expected<const Elf_Shdr *> SectionOrErr =
654 EF.getSection(**SymOrErr, *SymTabOrErr, ShndxTable);
655 if (!SectionOrErr)
656 return SectionOrErr.takeError();
657 const Elf_Shdr *Section = *SectionOrErr;
658 if (Section)
659 Result += Section->sh_addr;
660 }
661
662 return Result;
663}
664
665template <class ELFT>
667 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
668 if (!SymOrErr)
669 report_fatal_error(SymOrErr.takeError());
670 if ((*SymOrErr)->st_shndx == ELF::SHN_COMMON)
671 return (*SymOrErr)->st_value;
672 return 0;
673}
674
675template <class ELFT>
677 return EF.getHeader().e_machine;
678}
679
680template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const {
681 return EF.getHeader().e_type;
682}
683
684template <class ELFT> uint8_t ELFObjectFile<ELFT>::getEIdentABIVersion() const {
685 return EF.getHeader().e_ident[ELF::EI_ABIVERSION];
686}
687
688template <class ELFT> uint8_t ELFObjectFile<ELFT>::getEIdentOSABI() const {
689 return EF.getHeader().e_ident[ELF::EI_OSABI];
690}
691
692template <class ELFT>
693uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
694 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
695 if (!SymOrErr)
696 report_fatal_error(SymOrErr.takeError());
697 return (*SymOrErr)->st_size;
698}
699
700template <class ELFT>
702 return getSymbolSize(Symb);
703}
704
705template <class ELFT>
707 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
708 if (!SymOrErr)
709 report_fatal_error(SymOrErr.takeError());
710 return (*SymOrErr)->getBinding();
711}
712
713template <class ELFT>
715 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
716 if (!SymOrErr)
717 report_fatal_error(SymOrErr.takeError());
718 return (*SymOrErr)->st_other;
719}
720
721template <class ELFT>
723 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
724 if (!SymOrErr)
725 report_fatal_error(SymOrErr.takeError());
726 return (*SymOrErr)->getType();
727}
728
729template <class ELFT>
732 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
733 if (!SymOrErr)
734 return SymOrErr.takeError();
735
736 switch ((*SymOrErr)->getType()) {
737 case ELF::STT_NOTYPE:
739 case ELF::STT_SECTION:
740 return SymbolRef::ST_Debug;
741 case ELF::STT_FILE:
742 return SymbolRef::ST_File;
743 case ELF::STT_FUNC:
745 case ELF::STT_OBJECT:
746 case ELF::STT_COMMON:
747 return SymbolRef::ST_Data;
748 case ELF::STT_TLS:
749 default:
750 return SymbolRef::ST_Other;
751 }
752}
753
754template <class ELFT>
756 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
757 if (!SymOrErr)
758 return SymOrErr.takeError();
759
760 const Elf_Sym *ESym = *SymOrErr;
762
763 if (ESym->getBinding() != ELF::STB_LOCAL)
764 Result |= SymbolRef::SF_Global;
765
766 if (ESym->getBinding() == ELF::STB_WEAK)
767 Result |= SymbolRef::SF_Weak;
768
769 if (ESym->st_shndx == ELF::SHN_ABS)
770 Result |= SymbolRef::SF_Absolute;
771
772 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION)
774
775 if (Expected<typename ELFT::SymRange> SymbolsOrErr =
776 EF.symbols(DotSymtabSec)) {
777 // Set the SF_FormatSpecific flag for the 0-index null symbol.
778 if (ESym == SymbolsOrErr->begin())
780 } else
781 // TODO: Test this error.
782 return SymbolsOrErr.takeError();
783
784 if (Expected<typename ELFT::SymRange> SymbolsOrErr =
785 EF.symbols(DotDynSymSec)) {
786 // Set the SF_FormatSpecific flag for the 0-index null symbol.
787 if (ESym == SymbolsOrErr->begin())
789 } else
790 // TODO: Test this error.
791 return SymbolsOrErr.takeError();
792
793 if (EF.getHeader().e_machine == ELF::EM_AARCH64) {
794 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
795 StringRef Name = *NameOrErr;
796 if (Name.starts_with("$d") || Name.starts_with("$x"))
798 } else {
799 // TODO: Actually report errors helpfully.
800 consumeError(NameOrErr.takeError());
801 }
802 } else if (EF.getHeader().e_machine == ELF::EM_ARM) {
803 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
804 StringRef Name = *NameOrErr;
805 // TODO Investigate why empty name symbols need to be marked.
806 if (Name.empty() || Name.starts_with("$d") || Name.starts_with("$t") ||
807 Name.starts_with("$a"))
809 } else {
810 // TODO: Actually report errors helpfully.
811 consumeError(NameOrErr.takeError());
812 }
813 if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1)
814 Result |= SymbolRef::SF_Thumb;
815 } else if (EF.getHeader().e_machine == ELF::EM_CSKY) {
816 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
817 StringRef Name = *NameOrErr;
818 if (Name.starts_with("$d") || Name.starts_with("$t"))
820 } else {
821 // TODO: Actually report errors helpfully.
822 consumeError(NameOrErr.takeError());
823 }
824 } else if (EF.getHeader().e_machine == ELF::EM_RISCV) {
825 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
826 StringRef Name = *NameOrErr;
827 // Mark fake labels (used for label differences) and mapping symbols.
828 if (Name == ".L0 " || Name.starts_with("$d") || Name.starts_with("$x"))
830 } else {
831 // TODO: Actually report errors helpfully.
832 consumeError(NameOrErr.takeError());
833 }
834 }
835
836 if (ESym->st_shndx == ELF::SHN_UNDEF)
837 Result |= SymbolRef::SF_Undefined;
838
839 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
840 Result |= SymbolRef::SF_Common;
841
842 if (isExportedToOtherDSO(ESym))
843 Result |= SymbolRef::SF_Exported;
844
845 if (ESym->getType() == ELF::STT_GNU_IFUNC)
846 Result |= SymbolRef::SF_Indirect;
847
848 if (ESym->getVisibility() == ELF::STV_HIDDEN)
849 Result |= SymbolRef::SF_Hidden;
850
851 return Result;
852}
853
854template <class ELFT>
857 const Elf_Shdr *SymTab) const {
858 ArrayRef<Elf_Word> ShndxTable;
859 if (DotSymtabShndxSec) {
860 // TODO: Test this error.
861 Expected<ArrayRef<Elf_Word>> ShndxTableOrErr =
862 EF.getSHNDXTable(*DotSymtabShndxSec);
863 if (!ShndxTableOrErr)
864 return ShndxTableOrErr.takeError();
865 ShndxTable = *ShndxTableOrErr;
866 }
867
868 auto ESecOrErr = EF.getSection(*ESym, SymTab, ShndxTable);
869 if (!ESecOrErr)
870 return ESecOrErr.takeError();
871
872 const Elf_Shdr *ESec = *ESecOrErr;
873 if (!ESec)
874 return section_end();
875
876 DataRefImpl Sec;
877 Sec.p = reinterpret_cast<intptr_t>(ESec);
878 return section_iterator(SectionRef(Sec, this));
879}
880
881template <class ELFT>
884 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
885 if (!SymOrErr)
886 return SymOrErr.takeError();
887
888 auto SymTabOrErr = EF.getSection(Symb.d.a);
889 if (!SymTabOrErr)
890 return SymTabOrErr.takeError();
891 return getSymbolSection(*SymOrErr, *SymTabOrErr);
892}
893
894template <class ELFT>
896 const Elf_Shdr *ESec = getSection(Sec);
897 Sec = toDRI(++ESec);
898}
899
900template <class ELFT>
902 return EF.getSectionName(*getSection(Sec));
903}
904
905template <class ELFT>
907 return getSection(Sec)->sh_addr;
908}
909
910template <class ELFT>
912 auto SectionsOrErr = EF.sections();
913 handleAllErrors(std::move(SectionsOrErr.takeError()),
914 [](const ErrorInfoBase &) {
915 llvm_unreachable("unable to get section index");
916 });
917 const Elf_Shdr *First = SectionsOrErr->begin();
918 return getSection(Sec) - First;
919}
920
921template <class ELFT>
923 return getSection(Sec)->sh_size;
924}
925
926template <class ELFT>
929 const Elf_Shdr *EShdr = getSection(Sec);
930 if (EShdr->sh_type == ELF::SHT_NOBITS)
931 return ArrayRef((const uint8_t *)base(), (size_t)0);
932 if (Error E =
934 (uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
935 return std::move(E);
936 return ArrayRef((const uint8_t *)base() + EShdr->sh_offset, EShdr->sh_size);
937}
938
939template <class ELFT>
941 return getSection(Sec)->sh_addralign;
942}
943
944template <class ELFT>
946 return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED;
947}
948
949template <class ELFT>
951 return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR;
952}
953
954template <class ELFT>
956 const Elf_Shdr *EShdr = getSection(Sec);
957 return (EShdr->sh_flags & ELF::SHF_ALLOC) &&
958 !(EShdr->sh_flags & ELF::SHF_EXECINSTR) &&
959 EShdr->sh_type != ELF::SHT_NOBITS;
960}
961
962template <class ELFT>
964 const Elf_Shdr *EShdr = getSection(Sec);
965 return EShdr->sh_flags & ELF::SHF_ALLOC && EShdr->sh_type == ELF::SHT_NOBITS;
966}
967
968template <class ELFT>
969std::vector<SectionRef>
971 std::vector<SectionRef> Res;
972 std::vector<uintptr_t> Offsets;
973
974 auto SectionsOrErr = EF.sections();
975 if (!SectionsOrErr)
976 return Res;
977
978 for (const Elf_Shdr &Sec : *SectionsOrErr) {
979 if (Sec.sh_type != ELF::SHT_DYNAMIC)
980 continue;
981 Elf_Dyn *Dynamic =
982 reinterpret_cast<Elf_Dyn *>((uintptr_t)base() + Sec.sh_offset);
983 for (; Dynamic->d_tag != ELF::DT_NULL; Dynamic++) {
984 if (Dynamic->d_tag == ELF::DT_REL || Dynamic->d_tag == ELF::DT_RELA ||
985 Dynamic->d_tag == ELF::DT_JMPREL) {
986 Offsets.push_back(Dynamic->d_un.d_val);
987 }
988 }
989 }
990 for (const Elf_Shdr &Sec : *SectionsOrErr) {
991 if (is_contained(Offsets, Sec.sh_addr))
992 Res.emplace_back(toDRI(&Sec), this);
993 }
994 return Res;
995}
996
997template <class ELFT>
999 return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
1000}
1001
1002template <class ELFT>
1004 return getSection(Sec)->sh_flags & ELF::SHF_ALLOC &&
1005 (getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR ||
1006 !(getSection(Sec)->sh_flags & ELF::SHF_WRITE));
1007}
1008
1009template <class ELFT>
1011 const Elf_Shdr *EShdr = getSection(Sec);
1012 return !isBerkeleyText(Sec) && EShdr->sh_type != ELF::SHT_NOBITS &&
1013 EShdr->sh_flags & ELF::SHF_ALLOC;
1014}
1015
1016template <class ELFT>
1018 Expected<StringRef> SectionNameOrErr = getSectionName(Sec);
1019 if (!SectionNameOrErr) {
1020 // TODO: Report the error message properly.
1021 consumeError(SectionNameOrErr.takeError());
1022 return false;
1023 }
1024 StringRef SectionName = SectionNameOrErr.get();
1025 return SectionName.starts_with(".debug") ||
1026 SectionName.starts_with(".zdebug") || SectionName == ".gdb_index";
1027}
1028
1029template <class ELFT>
1032 DataRefImpl RelData;
1033 auto SectionsOrErr = EF.sections();
1034 if (!SectionsOrErr)
1036 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
1037 RelData.d.a = (Sec.p - SHT) / EF.getHeader().e_shentsize;
1038 RelData.d.b = 0;
1039 if (reinterpret_cast<const Elf_Shdr *>(Sec.p)->sh_type == ELF::SHT_CREL) {
1040 if (RelData.d.a + 1 > Crels.size())
1041 Crels.resize(RelData.d.a + 1);
1042 auto &Crel = Crels[RelData.d.a];
1043 if (Crel.empty()) {
1045 size_t I = 0;
1047 Content, [&](uint64_t Count, bool) { Crel.resize(Count); },
1048 [&](Elf_Crel Crel) { Crels[RelData.d.a][I++] = Crel; });
1049 if (Err) {
1050 Crel.assign(1, Elf_Crel{0, 0, 0, 0});
1051 if (RelData.d.a + 1 > CrelDecodeProblems.size())
1052 CrelDecodeProblems.resize(RelData.d.a + 1);
1053 CrelDecodeProblems[RelData.d.a] = toString(std::move(Err));
1054 }
1055 }
1056 }
1057 return relocation_iterator(RelocationRef(RelData, this));
1058}
1059
1060template <class ELFT>
1063 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1065 DataRefImpl RelData = Begin->getRawDataRefImpl();
1066 if (S->sh_type == ELF::SHT_CREL) {
1067 RelData.d.b = Crels[RelData.d.a].size();
1068 return relocation_iterator(RelocationRef(RelData, this));
1069 }
1070 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
1071 return Begin;
1072 const Elf_Shdr *RelSec = getRelSection(RelData);
1073
1074 // Error check sh_link here so that getRelocationSymbol can just use it.
1075 auto SymSecOrErr = EF.getSection(RelSec->sh_link);
1076 if (!SymSecOrErr)
1078 Twine(errorToErrorCode(SymSecOrErr.takeError()).message()));
1079
1080 RelData.d.b += S->sh_size / S->sh_entsize;
1081 return relocation_iterator(RelocationRef(RelData, this));
1082}
1083
1084template <class ELFT>
1087 const Elf_Shdr *EShdr = getSection(Sec);
1088 uintX_t Type = EShdr->sh_type;
1090 return section_end();
1091
1092 Expected<const Elf_Shdr *> SecOrErr = EF.getSection(EShdr->sh_info);
1093 if (!SecOrErr)
1094 return SecOrErr.takeError();
1095 return section_iterator(SectionRef(toDRI(*SecOrErr), this));
1096}
1097
1098// Relocations
1099template <class ELFT>
1101 ++Rel.d.b;
1102}
1103
1104template <class ELFT>
1107 uint32_t symbolIdx;
1108 const Elf_Shdr *sec = getRelSection(Rel);
1109 if (sec->sh_type == ELF::SHT_CREL)
1110 symbolIdx = getCrel(Rel).r_symidx;
1111 else if (sec->sh_type == ELF::SHT_REL)
1112 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
1113 else
1114 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
1115 if (!symbolIdx)
1116 return symbol_end();
1117
1118 // FIXME: error check symbolIdx
1119 DataRefImpl SymbolData;
1120 SymbolData.d.a = sec->sh_link;
1121 SymbolData.d.b = symbolIdx;
1122 return symbol_iterator(SymbolRef(SymbolData, this));
1123}
1124
1125template <class ELFT>
1127 const Elf_Shdr *sec = getRelSection(Rel);
1128 if (sec->sh_type == ELF::SHT_CREL)
1129 return getCrel(Rel).r_offset;
1130 if (sec->sh_type == ELF::SHT_REL)
1131 return getRel(Rel)->r_offset;
1132
1133 return getRela(Rel)->r_offset;
1134}
1135
1136template <class ELFT>
1138 const Elf_Shdr *sec = getRelSection(Rel);
1139 if (sec->sh_type == ELF::SHT_CREL)
1140 return getCrel(Rel).r_type;
1141 if (sec->sh_type == ELF::SHT_REL)
1142 return getRel(Rel)->getType(EF.isMips64EL());
1143 else
1144 return getRela(Rel)->getType(EF.isMips64EL());
1145}
1146
1147template <class ELFT>
1151
1152template <class ELFT>
1154 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1155 uint32_t type = getRelocationType(Rel);
1156 EF.getRelocationTypeName(type, Result);
1157}
1158
1159template <class ELFT>
1162 if (getRelSection(Rel)->sh_type == ELF::SHT_RELA)
1163 return (int64_t)getRela(Rel)->r_addend;
1164 if (getRelSection(Rel)->sh_type == ELF::SHT_CREL)
1165 return (int64_t)getCrel(Rel).r_addend;
1166 return createError("Relocation section does not have addends");
1167}
1168
1169template <class ELFT>
1170const typename ELFObjectFile<ELFT>::Elf_Rel *
1172 assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
1173 auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
1174 if (!Ret)
1175 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message()));
1176 return *Ret;
1177}
1178
1179template <class ELFT>
1180const typename ELFObjectFile<ELFT>::Elf_Rela *
1182 assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
1183 auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
1184 if (!Ret)
1185 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message()));
1186 return *Ret;
1187}
1188
1189template <class ELFT>
1192 assert(getRelSection(Crel)->sh_type == ELF::SHT_CREL);
1193 assert(Crel.d.a < Crels.size());
1194 return Crels[Crel.d.a][Crel.d.b];
1195}
1196
1197template <class ELFT>
1200 auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer());
1201 if (Error E = EFOrErr.takeError())
1202 return std::move(E);
1203
1204 ELFObjectFile<ELFT> Obj = {Object, std::move(*EFOrErr), nullptr, nullptr,
1205 nullptr};
1206 if (InitContent)
1207 if (Error E = Obj.initContent())
1208 return std::move(E);
1209 return std::move(Obj);
1210}
1211
1212template <class ELFT>
1213ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
1214 const Elf_Shdr *DotDynSymSec,
1215 const Elf_Shdr *DotSymtabSec,
1216 const Elf_Shdr *DotSymtabShndx)
1217 : ELFObjectFileBase(getELFType(ELFT::Endianness == llvm::endianness::little,
1218 ELFT::Is64Bits),
1219 Object),
1220 EF(std::move(EF)), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
1221 DotSymtabShndxSec(DotSymtabShndx) {}
1222
1223template <class ELFT>
1224ELFObjectFile<ELFT>::ELFObjectFile(ELFObjectFile<ELFT> &&Other)
1225 : ELFObjectFile(Other.Data, std::move(Other.EF), Other.DotDynSymSec,
1227
1228template <class ELFT>
1230 DataRefImpl Sym =
1232 DotSymtabSec && DotSymtabSec->sh_size >= sizeof(Elf_Sym) ? 1 : 0);
1233 return basic_symbol_iterator(SymbolRef(Sym, this));
1234}
1235
1236template <class ELFT>
1238 const Elf_Shdr *SymTab = DotSymtabSec;
1239 if (!SymTab)
1240 return symbol_begin();
1241 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1242 return basic_symbol_iterator(SymbolRef(Sym, this));
1243}
1244
1245template <class ELFT>
1247 if (!DotDynSymSec || DotDynSymSec->sh_size < sizeof(Elf_Sym))
1248 // Ignore errors here where the dynsym is empty or sh_size less than the
1249 // size of one symbol. These should be handled elsewhere.
1250 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 0), this));
1251 // Skip 0-index NULL symbol.
1252 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 1), this));
1253}
1254
1255template <class ELFT>
1257 const Elf_Shdr *SymTab = DotDynSymSec;
1258 if (!SymTab)
1259 return dynamic_symbol_begin();
1260 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1261 return basic_symbol_iterator(SymbolRef(Sym, this));
1262}
1263
1264template <class ELFT>
1266 auto SectionsOrErr = EF.sections();
1267 if (!SectionsOrErr)
1268 return section_iterator(SectionRef());
1269 return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this));
1270}
1271
1272template <class ELFT>
1274 auto SectionsOrErr = EF.sections();
1275 if (!SectionsOrErr)
1276 return section_iterator(SectionRef());
1277 return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
1278}
1279
1280template <class ELFT>
1282 return ELFT::Is64Bits ? 8 : 4;
1283}
1284
1285template <class ELFT>
1287 constexpr bool IsLittleEndian = ELFT::Endianness == llvm::endianness::little;
1288 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1289 case ELF::ELFCLASS32:
1290 switch (EF.getHeader().e_machine) {
1291 case ELF::EM_68K:
1292 return "elf32-m68k";
1293 case ELF::EM_386:
1294 return "elf32-i386";
1295 case ELF::EM_IAMCU:
1296 return "elf32-iamcu";
1297 case ELF::EM_X86_64:
1298 return "elf32-x86-64";
1299 case ELF::EM_ARM:
1300 return (IsLittleEndian ? "elf32-littlearm" : "elf32-bigarm");
1301 case ELF::EM_AVR:
1302 return "elf32-avr";
1303 case ELF::EM_HEXAGON:
1304 return "elf32-hexagon";
1305 case ELF::EM_LANAI:
1306 return "elf32-lanai";
1307 case ELF::EM_MIPS:
1308 return "elf32-mips";
1309 case ELF::EM_MSP430:
1310 return "elf32-msp430";
1311 case ELF::EM_PPC:
1312 return (IsLittleEndian ? "elf32-powerpcle" : "elf32-powerpc");
1313 case ELF::EM_RISCV:
1314 return (IsLittleEndian ? "elf32-littleriscv" : "elf32-bigriscv");
1315 case ELF::EM_CSKY:
1316 return "elf32-csky";
1317 case ELF::EM_SPARC:
1319 return "elf32-sparc";
1320 case ELF::EM_AMDGPU:
1321 return "elf32-amdgpu";
1322 case ELF::EM_LOONGARCH:
1323 return "elf32-loongarch";
1324 case ELF::EM_XTENSA:
1325 return "elf32-xtensa";
1326 default:
1327 return "elf32-unknown";
1328 }
1329 case ELF::ELFCLASS64:
1330 switch (EF.getHeader().e_machine) {
1331 case ELF::EM_386:
1332 return "elf64-i386";
1333 case ELF::EM_X86_64:
1334 return "elf64-x86-64";
1335 case ELF::EM_AARCH64:
1336 return (IsLittleEndian ? "elf64-littleaarch64" : "elf64-bigaarch64");
1337 case ELF::EM_PPC64:
1338 return (IsLittleEndian ? "elf64-powerpcle" : "elf64-powerpc");
1339 case ELF::EM_RISCV:
1340 return (IsLittleEndian ? "elf64-littleriscv" : "elf64-bigriscv");
1341 case ELF::EM_S390:
1342 return "elf64-s390";
1343 case ELF::EM_SPARCV9:
1344 return "elf64-sparc";
1345 case ELF::EM_MIPS:
1346 return "elf64-mips";
1347 case ELF::EM_AMDGPU:
1348 return "elf64-amdgpu";
1349 case ELF::EM_BPF:
1350 return "elf64-bpf";
1351 case ELF::EM_VE:
1352 return "elf64-ve";
1353 case ELF::EM_LOONGARCH:
1354 return "elf64-loongarch";
1355 default:
1356 return "elf64-unknown";
1357 }
1358 default:
1359 // FIXME: Proper error handling.
1360 report_fatal_error("Invalid ELFCLASS!");
1361 }
1362}
1363
1364template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
1365 bool IsLittleEndian = ELFT::Endianness == llvm::endianness::little;
1366 switch (EF.getHeader().e_machine) {
1367 case ELF::EM_68K:
1368 return Triple::m68k;
1369 case ELF::EM_386:
1370 case ELF::EM_IAMCU:
1371 return Triple::x86;
1372 case ELF::EM_X86_64:
1373 return Triple::x86_64;
1374 case ELF::EM_AARCH64:
1375 return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be;
1376 case ELF::EM_ARM:
1377 return Triple::arm;
1378 case ELF::EM_AVR:
1379 return Triple::avr;
1380 case ELF::EM_HEXAGON:
1381 return Triple::hexagon;
1382 case ELF::EM_LANAI:
1383 return Triple::lanai;
1384 case ELF::EM_MIPS:
1385 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1386 case ELF::ELFCLASS32:
1387 return IsLittleEndian ? Triple::mipsel : Triple::mips;
1388 case ELF::ELFCLASS64:
1389 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
1390 default:
1391 report_fatal_error("Invalid ELFCLASS!");
1392 }
1393 case ELF::EM_MSP430:
1394 return Triple::msp430;
1395 case ELF::EM_PPC:
1396 return IsLittleEndian ? Triple::ppcle : Triple::ppc;
1397 case ELF::EM_PPC64:
1398 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
1399 case ELF::EM_RISCV:
1400 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1401 case ELF::ELFCLASS32:
1402 return IsLittleEndian ? Triple::riscv32 : Triple::riscv32be;
1403 case ELF::ELFCLASS64:
1404 return IsLittleEndian ? Triple::riscv64 : Triple::riscv64be;
1405 default:
1406 report_fatal_error("Invalid ELFCLASS!");
1407 }
1408 case ELF::EM_S390:
1409 return Triple::systemz;
1410
1411 case ELF::EM_SPARC:
1413 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
1414 case ELF::EM_SPARCV9:
1415 return Triple::sparcv9;
1416
1417 case ELF::EM_AMDGPU: {
1418 if (!IsLittleEndian)
1419 return Triple::UnknownArch;
1420
1421 unsigned MACH = EF.getHeader().e_flags & ELF::EF_AMDGPU_MACH;
1422 if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST &&
1424 return Triple::r600;
1427 return Triple::amdgcn;
1428
1429 return Triple::UnknownArch;
1430 }
1431
1432 case ELF::EM_CUDA: {
1433 if (EF.getHeader().e_ident[ELF::EI_CLASS] == ELF::ELFCLASS32)
1434 return Triple::nvptx;
1435 return Triple::nvptx64;
1436 }
1437
1438 case ELF::EM_BPF:
1439 return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
1440
1441 case ELF::EM_VE:
1442 return Triple::ve;
1443 case ELF::EM_CSKY:
1444 return Triple::csky;
1445
1446 case ELF::EM_LOONGARCH:
1447 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1448 case ELF::ELFCLASS32:
1449 return Triple::loongarch32;
1450 case ELF::ELFCLASS64:
1451 return Triple::loongarch64;
1452 default:
1453 report_fatal_error("Invalid ELFCLASS!");
1454 }
1455
1456 case ELF::EM_XTENSA:
1457 return Triple::xtensa;
1458
1459 default:
1460 return Triple::UnknownArch;
1461 }
1462}
1463
1464template <class ELFT> Triple::OSType ELFObjectFile<ELFT>::getOS() const {
1465 switch (EF.getHeader().e_ident[ELF::EI_OSABI]) {
1467 return Triple::NetBSD;
1469 return Triple::Linux;
1470 case ELF::ELFOSABI_HURD:
1471 return Triple::Hurd;
1473 return Triple::Solaris;
1474 case ELF::ELFOSABI_AIX:
1475 return Triple::AIX;
1477 return Triple::FreeBSD;
1479 return Triple::OpenBSD;
1480 case ELF::ELFOSABI_CUDA:
1482 return Triple::CUDA;
1484 return Triple::AMDHSA;
1486 return Triple::AMDPAL;
1488 return Triple::Mesa3D;
1489 default:
1490 return Triple::UnknownOS;
1491 }
1492}
1493
1494template <class ELFT>
1496 return EF.getHeader().e_entry;
1497}
1498
1499template <class ELFT>
1504
1505template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
1506 return EF.getHeader().e_type == ELF::ET_REL;
1507}
1508
1509template <class ELFT>
1511 uintptr_t SHT = reinterpret_cast<uintptr_t>(cantFail(EF.sections()).begin());
1512 auto I = (Sec.p - SHT) / EF.getHeader().e_shentsize;
1513 if (I < CrelDecodeProblems.size())
1514 return CrelDecodeProblems[I];
1515 return "";
1516}
1517
1518} // end namespace object
1519} // end namespace llvm
1520
1521#endif // LLVM_OBJECT_ELFOBJECTFILE_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:215
DXIL Resource Implicit Binding
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Definition ELFTypes.h:119
static FeatureBitset getFeatures(MCSubtargetInfo &STI, StringRef CPU, StringRef TuneCPU, StringRef FS, ArrayRef< StringRef > ProcNames, ArrayRef< SubtargetSubTypeKV > ProcDesc, ArrayRef< SubtargetFeatureKV > ProcFeatures)
#define I(x, y, z)
Definition MD5.cpp:57
This file contains some templates that are useful if you are working with the STL at all.
static uint64_t getSymbolValue(const MCSymbolCOFF &Symbol, const MCAssembler &Asm)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Base class for error info classes.
Definition Error.h:44
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
reference get()
Returns a reference to the stored T value.
Definition Error.h:582
Generic base class for all target subtargets.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Manages the enabling and disabling of subtarget specific features.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
@ loongarch32
Definition Triple.h:64
@ UnknownArch
Definition Triple.h:50
@ loongarch64
Definition Triple.h:65
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
A range adaptor for a pair of iterators.
const SymbolicFile * getObject() const
DataRefImpl getRawDataRefImpl() const
MemoryBufferRef Data
Definition Binary.h:38
static unsigned int getELFType(bool isLE, bool is64Bits)
Definition Binary.h:80
MemoryBufferRef getMemoryBufferRef() const
Definition Binary.cpp:43
static Error checkOffset(MemoryBufferRef M, uintptr_t Addr, const uint64_t Size)
Definition Binary.h:179
static Expected< ELFFile > create(StringRef Object)
Definition ELF.h:1000
virtual uint64_t getSymbolSize(DataRefImpl Symb) const =0
virtual uint8_t getEIdentABIVersion() const =0
virtual Error getBuildAttributes(ELFAttributeParser &Attributes) const =0
std::vector< ELFPltEntry > getPltEntries(const MCSubtargetInfo &STI) const
virtual uint64_t getSectionFlags(DataRefImpl Sec) const =0
virtual uint16_t getEType() const =0
virtual uint8_t getSymbolELFType(DataRefImpl Symb) const =0
virtual uint8_t getEIdentOSABI() const =0
Expected< std::vector< VersionEntry > > readDynsymVersions() const
Returns a vector containing a symbol version for each dynamic symbol.
virtual uint8_t getSymbolOther(DataRefImpl Symb) const =0
virtual elf_symbol_iterator_range getDynamicSymbolIterators() const =0
virtual uint32_t getSectionType(DataRefImpl Sec) const =0
StringRef getCrelDecodeProblem(SectionRef Sec) const
elf_symbol_iterator_range symbols() const
virtual Expected< int64_t > getRelocationAddend(DataRefImpl Rel) const =0
virtual uint8_t getSymbolBinding(DataRefImpl Symb) const =0
iterator_range< elf_symbol_iterator > elf_symbol_iterator_range
virtual uint16_t getEMachine() const =0
static bool classof(const Binary *v)
virtual unsigned getPlatformFlags() const =0
Returns platform-specific object flags, if any.
ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
virtual uint64_t getSectionOffset(DataRefImpl Sec) const =0
Expected< std::vector< BBAddrMap > > readBBAddrMap(std::optional< unsigned > TextSectionIndex=std::nullopt, std::vector< PGOAnalysisMap > *PGOAnalyses=nullptr) const
Returns a vector of all BB address maps in the object file.
static bool classof(const Binary *v)
const ELFFile< ELFT > & getELFFile() const
Expected< StringRef > getSectionName(DataRefImpl Sec) const override
std::vector< SectionRef > dynamic_relocation_sections() const override
uint64_t getRelocationType(DataRefImpl Rel) const override
bool isSectionText(DataRefImpl Sec) const override
uint8_t getSymbolELFType(DataRefImpl Symb) const override
uint64_t getSectionAlignment(DataRefImpl Sec) const override
bool is64Bit() const override
DataRefImpl toDRI(const Elf_Dyn *Dyn) const
bool isSectionVirtual(DataRefImpl Sec) const override
Triple::OSType getOS() const override
SectionRef toSectionRef(const Elf_Shdr *Sec) const
uint32_t getSectionType(DataRefImpl Sec) const override
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
elf_symbol_iterator_range getDynamicSymbolIterators() const override
Expected< const Elf_Sym * > getSymbol(DataRefImpl Sym) const
Expected< section_iterator > getSymbolSection(DataRefImpl Symb) const override
StringRef getRelocationTypeName(uint32_t Type) const
const Elf_Rel * getRel(DataRefImpl Rel) const
Elf_Crel getCrel(DataRefImpl Crel) const
ELFObjectFile(ELFObjectFile< ELFT > &&Other)
Expected< section_iterator > getSymbolSection(const Elf_Sym *Symb, const Elf_Shdr *SymTab) const
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override
basic_symbol_iterator symbol_begin() const override
Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
ELFSymbolRef toSymbolRef(const Elf_Shdr *SymTable, unsigned SymbolNum) const
SmallVector< std::string, 0 > CrelDecodeProblems
const Elf_Rela * getRela(DataRefImpl Rela) const
static Expected< ELFObjectFile< ELFT > > create(MemoryBufferRef Object, bool InitContent=true)
bool isExportedToOtherDSO(const Elf_Sym *ESym) const
uint64_t getSectionAddress(DataRefImpl Sec) const override
Expected< ArrayRef< uint8_t > > getSectionContents(DataRefImpl Sec) const override
void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const override
uint64_t getSectionIndex(DataRefImpl Sec) const override
Expected< uint32_t > getSymbolFlags(DataRefImpl Symb) const override
bool isSectionData(DataRefImpl Sec) const override
const Elf_Shdr * DotSymtabSec
const Elf_Shdr * DotDynSymSec
uint32_t getSymbolAlignment(DataRefImpl Symb) const override
const Elf_Shdr * toELFShdrIter(DataRefImpl Sec) const
Triple::ArchType getArch() const override
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
uint64_t getSectionSize(DataRefImpl Sec) const override
bool isBerkeleyData(DataRefImpl Sec) const override
StringRef getFileFormatName() const override
bool isRelocatableObject() const override
True if this is a relocatable object (.o/.obj).
void moveSymbolNext(DataRefImpl &Symb) const override
uint8_t getSymbolOther(DataRefImpl Symb) const override
section_iterator section_end() const override
const Elf_Shdr * getRelSection(DataRefImpl Rel) const
Get the relocation section that contains Rel.
Expected< int64_t > getRelocationAddend(DataRefImpl Rel) const override
uint64_t getRelocationOffset(DataRefImpl Rel) const override
Expected< section_iterator > getRelocatedSection(DataRefImpl Sec) const override
void moveSectionNext(DataRefImpl &Sec) const override
bool isSectionBSS(DataRefImpl Sec) const override
unsigned getPlatformFlags() const override
Returns platform-specific object flags, if any.
uint64_t getSectionFlags(DataRefImpl Sec) const override
void moveRelocationNext(DataRefImpl &Rel) const override
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
Expected< StringRef > getSymbolName(DataRefImpl Symb) const override
basic_symbol_iterator symbol_end() const override
uint8_t getSymbolBinding(DataRefImpl Symb) const override
uint64_t getSectionOffset(DataRefImpl Sec) const override
relocation_iterator section_rel_end(DataRefImpl Sec) const override
Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const override
const Elf_Shdr * getSection(DataRefImpl Sec) const
Error getBuildAttributes(ELFAttributeParser &Attributes) const override
DataRefImpl toDRI(const Elf_Shdr *Sec) const
elf_symbol_iterator dynamic_symbol_begin() const
const Elf_Shdr * DotSymtabShndxSec
elf_symbol_iterator dynamic_symbol_end() const
DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const
bool isDebugSection(DataRefImpl Sec) const override
section_iterator section_begin() const override
bool isSectionCompressed(DataRefImpl Sec) const override
SmallVector< SmallVector< Elf_Crel, 0 >, 0 > Crels
bool isBerkeleyText(DataRefImpl Sec) const override
uint8_t getBytesInAddress() const override
The number of bytes used to represent an address in this object file format.
Expected< uint64_t > getStartAddress() const override
StringRef getCrelDecodeProblem(DataRefImpl Sec) const
Expected< int64_t > getAddend() const
const ELFObjectFileBase * getObject() const
ELFRelocationRef(const RelocationRef &B)
const ELFObjectFileBase * getObject() const
ELFSectionRef(const SectionRef &B)
const ELFObjectFileBase * getObject() const
ELFSymbolRef(const SymbolRef &B)
StringRef getELFTypeName() const
const uint8_t * base() const
Definition ObjectFile.h:237
ObjectFile(unsigned int Type, MemoryBufferRef Source)
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition ObjectFile.h:54
const ObjectFile * getObject() const
Definition ObjectFile.h:645
DataRefImpl getRawDataRefImpl() const
Definition ObjectFile.h:641
This is a value type class that represents a single section in the list of sections in the object fil...
Definition ObjectFile.h:83
DataRefImpl getRawDataRefImpl() const
Definition ObjectFile.h:603
const ObjectFile * getObject() const
Definition ObjectFile.h:607
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition ObjectFile.h:170
const ObjectFile * getObject() const
Definition ObjectFile.h:493
virtual basic_symbol_iterator symbol_begin() const =0
virtual basic_symbol_iterator symbol_end() const =0
const ELFRelocationRef & operator*() const
elf_relocation_iterator(const relocation_iterator &B)
const ELFRelocationRef * operator->() const
elf_section_iterator(const section_iterator &B)
const ELFSectionRef * operator->() const
const ELFSectionRef & operator*() const
const ELFSymbolRef & operator*() const
elf_symbol_iterator(const basic_symbol_iterator &B)
const ELFSymbolRef * operator->() const
const SymbolRef * operator->() const
Definition ObjectFile.h:217
const SymbolRef & operator*() const
Definition ObjectFile.h:222
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
@ SHN_ABS
Definition ELF.h:1146
@ SHN_COMMON
Definition ELF.h:1147
@ SHN_UNDEF
Definition ELF.h:1140
@ SHF_ALLOC
Definition ELF.h:1256
@ SHF_COMPRESSED
Definition ELF.h:1284
@ SHF_WRITE
Definition ELF.h:1253
@ SHF_EXECINSTR
Definition ELF.h:1259
@ EI_ABIVERSION
Definition ELF.h:59
@ EI_CLASS
Definition ELF.h:55
@ EI_OSABI
Definition ELF.h:58
@ EM_MSP430
Definition ELF.h:227
@ EM_S390
Definition ELF.h:155
@ EM_PPC64
Definition ELF.h:154
@ EM_SPARC
Definition ELF.h:140
@ EM_CSKY
Definition ELF.h:326
@ EM_SPARC32PLUS
Definition ELF.h:151
@ EM_68K
Definition ELF.h:142
@ EM_386
Definition ELF.h:141
@ EM_CUDA
Definition ELF.h:291
@ EM_LOONGARCH
Definition ELF.h:327
@ EM_BPF
Definition ELF.h:324
@ EM_PPC
Definition ELF.h:153
@ EM_X86_64
Definition ELF.h:183
@ EM_HEXAGON
Definition ELF.h:262
@ EM_LANAI
Definition ELF.h:323
@ EM_MIPS
Definition ELF.h:146
@ EM_SPARCV9
Definition ELF.h:164
@ EM_AARCH64
Definition ELF.h:285
@ EM_XTENSA
Definition ELF.h:216
@ EM_RISCV
Definition ELF.h:322
@ EM_ARM
Definition ELF.h:161
@ EM_VE
Definition ELF.h:325
@ EM_IAMCU
Definition ELF.h:144
@ EM_AMDGPU
Definition ELF.h:321
@ EM_AVR
Definition ELF.h:204
@ SHT_REL
Definition ELF.h:1163
@ SHT_ARM_ATTRIBUTES
Definition ELF.h:1216
@ SHT_NOBITS
Definition ELF.h:1162
@ SHT_SYMTAB
Definition ELF.h:1156
@ SHT_HEXAGON_ATTRIBUTES
Definition ELF.h:1243
@ SHT_CREL
Definition ELF.h:1176
@ SHT_DYNAMIC
Definition ELF.h:1160
@ SHT_SYMTAB_SHNDX
Definition ELF.h:1170
@ SHT_AARCH64_ATTRIBUTES
Definition ELF.h:1220
@ SHT_RISCV_ATTRIBUTES
Definition ELF.h:1239
@ SHT_RELA
Definition ELF.h:1158
@ SHT_DYNSYM
Definition ELF.h:1165
@ ELFOSABI_HURD
Definition ELF.h:351
@ ELFOSABI_CUDA
Definition ELF.h:364
@ ELFOSABI_OPENBSD
Definition ELF.h:358
@ ELFOSABI_CUDA_V2
Definition ELF.h:365
@ ELFOSABI_NETBSD
Definition ELF.h:348
@ ELFOSABI_AMDGPU_HSA
Definition ELF.h:367
@ ELFOSABI_SOLARIS
Definition ELF.h:352
@ ELFOSABI_FREEBSD
Definition ELF.h:355
@ ELFOSABI_LINUX
Definition ELF.h:350
@ ELFOSABI_AIX
Definition ELF.h:353
@ ELFOSABI_AMDGPU_MESA3D
Definition ELF.h:369
@ ELFOSABI_AMDGPU_PAL
Definition ELF.h:368
@ STB_GLOBAL
Definition ELF.h:1413
@ STB_LOCAL
Definition ELF.h:1412
@ STB_GNU_UNIQUE
Definition ELF.h:1415
@ STB_WEAK
Definition ELF.h:1414
@ STT_FUNC
Definition ELF.h:1426
@ STT_NOTYPE
Definition ELF.h:1424
@ STT_SECTION
Definition ELF.h:1427
@ STT_FILE
Definition ELF.h:1428
@ STT_COMMON
Definition ELF.h:1429
@ STT_GNU_IFUNC
Definition ELF.h:1431
@ STT_OBJECT
Definition ELF.h:1425
@ STT_TLS
Definition ELF.h:1430
@ ELFCLASS64
Definition ELF.h:334
@ ELFCLASS32
Definition ELF.h:333
@ ET_REL
Definition ELF.h:119
@ EF_AMDGPU_MACH_AMDGCN_LAST
Definition ELF.h:878
@ EF_AMDGPU_MACH_R600_LAST
Definition ELF.h:865
@ EF_AMDGPU_MACH_AMDGCN_FIRST
Definition ELF.h:877
@ EF_AMDGPU_MACH
Definition ELF.h:851
@ EF_AMDGPU_MACH_R600_FIRST
Definition ELF.h:864
@ STV_HIDDEN
Definition ELF.h:1444
@ STV_PROTECTED
Definition ELF.h:1445
@ STV_DEFAULT
Definition ELF.h:1442
static constexpr const StringLiteral & getSectionName(DebugSectionKind SectionKind)
Return the name of the section.
static Expected< const T * > getObject(MemoryBufferRef M, const void *Ptr, const uint64_t Size=sizeof(T))
Error decodeCrel(ArrayRef< uint8_t > Content, function_ref< void(uint64_t, bool)> HdrHandler, function_ref< void(Elf_Crel_Impl< Is64 >)> EntryHandler)
Definition ELF.h:217
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Definition ELF.h:607
bool operator<(const ELFSymbolRef &A, const ELFSymbolRef &B)
LLVM_ABI EnumStrings< uint8_t, 2 > getElfSymbolTypes()
content_iterator< SectionRef > section_iterator
Definition ObjectFile.h:49
Error createError(const Twine &Err)
Definition Error.h:86
LLVM_ABI StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
Definition ELF.cpp:25
ELFObjectFile< ELF32BE > ELF32BEObjectFile
content_iterator< BasicSymbolRef > basic_symbol_iterator
ELFObjectFile< ELF64LE > ELF64LEObjectFile
ELFObjectFile< ELF32LE > ELF32LEObjectFile
content_iterator< RelocationRef > relocation_iterator
Definition ObjectFile.h:79
ELFObjectFile< ELF64BE > ELF64BEObjectFile
This is an optimization pass for GlobalISel generic memory operations.
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition Error.h:1013
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
EnumStrings(const EnumStringsStorage< T, NumStrs, N, StrLen > &) -> EnumStrings< T, NumStrs >
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
@ Dynamic
Denotes mode unknown at compile time.
ArrayRef(const T &OneElt) -> ArrayRef< T >
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
endianness
Definition bit.h:71
LLVM_ABI std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition Error.cpp:113
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:860
std::optional< DataRefImpl > Symbol
struct llvm::object::DataRefImpl::@005117267142344013370254144343227032034000327225 d