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/STLExtras.h"
18#include "llvm/ADT/StringRef.h"
22#include "llvm/Object/Binary.h"
23#include "llvm/Object/ELF.h"
25#include "llvm/Object/Error.h"
32#include "llvm/Support/Error.h"
34#include "llvm/Support/LEB128.h"
39#include <cassert>
40#include <cstdint>
41
42namespace llvm {
43
44template <typename T> class SmallVectorImpl;
45
46namespace object {
47
48constexpr int NumElfSymbolTypes = 16;
51
53
56 std::optional<DataRefImpl> Symbol;
58};
59
61 friend class ELFRelocationRef;
62 friend class ELFSectionRef;
63 friend class ELFSymbolRef;
64
65 SubtargetFeatures getMIPSFeatures() const;
66 SubtargetFeatures getARMFeatures() const;
67 SubtargetFeatures getHexagonFeatures() const;
68 Expected<SubtargetFeatures> getRISCVFeatures() const;
69 SubtargetFeatures getLoongArchFeatures() const;
70
71 StringRef getAMDGPUCPUName() const;
72 StringRef getNVPTXCPUName() const;
73
74protected:
75 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
76
77 virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
78 virtual uint8_t getSymbolBinding(DataRefImpl Symb) const = 0;
79 virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
80 virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
81
82 virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
83 virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
84 virtual uint64_t getSectionOffset(DataRefImpl Sec) const = 0;
85
87 virtual Error getBuildAttributes(ELFAttributeParser &Attributes) const = 0;
88
89public:
91
93
94 /// Returns platform-specific object flags, if any.
95 virtual unsigned getPlatformFlags() const = 0;
96
98
99 static bool classof(const Binary *v) { return v->isELF(); }
100
102
103 std::optional<StringRef> tryGetCPUName() const override;
104
105 void setARMSubArch(Triple &TheTriple) const override;
106
107 virtual uint16_t getEType() const = 0;
108
109 virtual uint16_t getEMachine() const = 0;
110
111 virtual uint8_t getEIdentABIVersion() const = 0;
112
113 virtual uint8_t getEIdentOSABI() const = 0;
114
115 std::vector<ELFPltEntry> getPltEntries(const MCSubtargetInfo &STI) const;
116
117 /// Returns a vector containing a symbol version for each dynamic symbol.
118 /// Returns an empty vector if version sections do not exist.
120
121 /// Returns a vector of all BB address maps in the object file. When
122 /// `TextSectionIndex` is specified, only returns the BB address maps
123 /// corresponding to the section with that index. When `PGOAnalyses`is
124 /// specified (PGOAnalyses is not nullptr), the vector is cleared then filled
125 /// with extra PGO data. `PGOAnalyses` will always be the same length as the
126 /// return value when it is requested assuming no error occurs. Upon failure,
127 /// `PGOAnalyses` will be emptied.
129 readBBAddrMap(std::optional<unsigned> TextSectionIndex = std::nullopt,
130 std::vector<PGOAnalysisMap> *PGOAnalyses = nullptr) const;
131
133};
134
157
159public:
163
164 const ELFSectionRef *operator->() const {
165 return static_cast<const ELFSectionRef *>(section_iterator::operator->());
166 }
167
168 const ELFSectionRef &operator*() const {
169 return static_cast<const ELFSectionRef &>(section_iterator::operator*());
170 }
171};
172
173class ELFSymbolRef : public SymbolRef {
174public:
178
182
185 }
186
190
193 }
194
198
201 for (const auto &EE : ElfSymbolTypes) {
202 if (EE.Value == Type) {
203 return EE.AltName;
204 }
205 }
206 return "";
207 }
208};
209
210inline bool operator<(const ELFSymbolRef &A, const ELFSymbolRef &B) {
211 const DataRefImpl &DRIA = A.getRawDataRefImpl();
212 const DataRefImpl &DRIB = B.getRawDataRefImpl();
213 if (DRIA.d.a == DRIB.d.a)
214 return DRIA.d.b < DRIB.d.b;
215 return DRIA.d.a < DRIB.d.a;
216}
217
219public:
223
224 const ELFSymbolRef *operator->() const {
225 return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
226 }
227
228 const ELFSymbolRef &operator*() const {
229 return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
230 }
231};
232
247
249public:
253
255 return static_cast<const ELFRelocationRef *>(
257 }
258
260 return static_cast<const ELFRelocationRef &>(
262 }
263};
264
269
270template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
271 uint16_t getEMachine() const override;
272 uint16_t getEType() const override;
273 uint8_t getEIdentABIVersion() const override;
274 uint8_t getEIdentOSABI() const override;
275 uint64_t getSymbolSize(DataRefImpl Sym) const override;
276
277public:
279
280 SectionRef toSectionRef(const Elf_Shdr *Sec) const {
281 return SectionRef(toDRI(Sec), this);
282 }
283
284 ELFSymbolRef toSymbolRef(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
285 return ELFSymbolRef({toDRI(SymTable, SymbolNum), this});
286 }
287
288 bool IsContentValid() const { return ContentValid; }
289
290private:
292 const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,
293 const Elf_Shdr *DotSymtabShndxSec);
294
295 bool ContentValid = false;
296
297protected:
299
300 const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
301 const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
302 const Elf_Shdr *DotSymtabShndxSec = nullptr; // SHT_SYMTAB_SHNDX section.
303
304 // Hold CREL relocations for SectionRef::relocations().
307
308 Error initContent() override;
309
310 void moveSymbolNext(DataRefImpl &Symb) const override;
318 uint8_t getSymbolOther(DataRefImpl Symb) const override;
322 const Elf_Shdr *SymTab) const;
324
325 void moveSectionNext(DataRefImpl &Sec) const override;
331 getSectionContents(DataRefImpl Sec) const override;
333 bool isSectionCompressed(DataRefImpl Sec) const override;
334 bool isSectionText(DataRefImpl Sec) const override;
335 bool isSectionData(DataRefImpl Sec) const override;
336 bool isSectionBSS(DataRefImpl Sec) const override;
337 bool isSectionVirtual(DataRefImpl Sec) const override;
338 bool isBerkeleyText(DataRefImpl Sec) const override;
339 bool isBerkeleyData(DataRefImpl Sec) const override;
340 bool isDebugSection(DataRefImpl Sec) const override;
343 std::vector<SectionRef> dynamic_relocation_sections() const override;
345 getRelocatedSection(DataRefImpl Sec) const override;
346
347 void moveRelocationNext(DataRefImpl &Rel) const override;
352 SmallVectorImpl<char> &Result) const override;
353
358
359 DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
360 DataRefImpl DRI;
361 if (!SymTable) {
362 DRI.d.a = 0;
363 DRI.d.b = 0;
364 return DRI;
365 }
366 assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
367 SymTable->sh_type == ELF::SHT_DYNSYM);
368
369 auto SectionsOrErr = EF.sections();
370 if (!SectionsOrErr) {
371 DRI.d.a = 0;
372 DRI.d.b = 0;
373 return DRI;
374 }
375 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
376 unsigned SymTableIndex =
377 (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
378
379 DRI.d.a = SymTableIndex;
380 DRI.d.b = SymbolNum;
381 return DRI;
382 }
383
384 const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
385 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
386 }
387
388 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
389 DataRefImpl DRI;
390 DRI.p = reinterpret_cast<uintptr_t>(Sec);
391 return DRI;
392 }
393
394 DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
395 DataRefImpl DRI;
396 DRI.p = reinterpret_cast<uintptr_t>(Dyn);
397 return DRI;
398 }
399
400 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
401 unsigned char Binding = ESym->getBinding();
402 unsigned char Visibility = ESym->getVisibility();
403
404 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
405 // visibility is either DEFAULT or PROTECTED. All other symbols are not
406 // exported.
407 return (
410 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED));
411 }
412
413 Error getBuildAttributes(ELFAttributeParser &Attributes) const override {
415 switch (getEMachine()) {
416 case ELF::EM_ARM:
418 break;
419 case ELF::EM_AARCH64:
421 break;
422 case ELF::EM_RISCV:
424 break;
425 case ELF::EM_HEXAGON:
427 break;
428 default:
429 return Error::success();
430 }
431
432 auto SectionsOrErr = EF.sections();
433 if (!SectionsOrErr)
434 return SectionsOrErr.takeError();
435 for (const Elf_Shdr &Sec : *SectionsOrErr) {
436 if (Sec.sh_type != Type)
437 continue;
438 auto ErrorOrContents = EF.getSectionContents(Sec);
439 if (!ErrorOrContents)
440 return ErrorOrContents.takeError();
441
442 auto Contents = ErrorOrContents.get();
443 if (Contents[0] != ELFAttrs::Format_Version || Contents.size() == 1)
444 return Error::success();
445
446 if (Error E = Attributes.parse(Contents, ELFT::Endianness))
447 return E;
448 break;
449 }
450 return Error::success();
451 }
452
453 // This flag is used for classof, to distinguish ELFObjectFile from
454 // its subclass. If more subclasses will be created, this flag will
455 // have to become an enum.
456 bool isDyldELFObject = false;
457
458public:
459 ELFObjectFile(ELFObjectFile<ELFT> &&Other);
461 bool InitContent = true);
462
463 const Elf_Rel *getRel(DataRefImpl Rel) const;
464 const Elf_Rela *getRela(DataRefImpl Rela) const;
465 Elf_Crel getCrel(DataRefImpl Crel) const;
466
468 return EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
469 }
470
471 /// Get the relocation section that contains \a Rel.
472 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
473 auto RelSecOrErr = EF.getSection(Rel.d.a);
474 if (!RelSecOrErr)
476 Twine(errorToErrorCode(RelSecOrErr.takeError()).message()));
477 return *RelSecOrErr;
478 }
479
480 const Elf_Shdr *getSection(DataRefImpl Sec) const {
481 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
482 }
483
486
487 bool is64Bit() const override { return getBytesInAddress() == 8; }
488
491
494
496
497 uint8_t getBytesInAddress() const override;
498 StringRef getFileFormatName() const override;
499 Triple::ArchType getArch() const override;
500 Triple::OSType getOS() const override;
502
503 unsigned getPlatformFlags() const override { return EF.getHeader().e_flags; }
504
505 const ELFFile<ELFT> &getELFFile() const { return EF; }
506
507 bool isDyldType() const { return isDyldELFObject; }
508 static bool classof(const Binary *v) {
509 return v->getType() ==
510 getELFType(ELFT::Endianness == llvm::endianness::little,
511 ELFT::Is64Bits);
512 }
513
515
516 bool isRelocatableObject() const override;
517
518 void createFakeSections() { EF.createFakeSections(); }
519
521};
522
527
528template <class ELFT>
530 ++Sym.d.b;
531}
532
534 auto SectionsOrErr = EF.sections();
535 if (!SectionsOrErr)
536 return SectionsOrErr.takeError();
537
538 for (const Elf_Shdr &Sec : *SectionsOrErr) {
539 switch (Sec.sh_type) {
540 case ELF::SHT_DYNSYM: {
541 if (!DotDynSymSec)
542 DotDynSymSec = &Sec;
543 break;
544 }
545 case ELF::SHT_SYMTAB: {
546 if (!DotSymtabSec)
547 DotSymtabSec = &Sec;
548 break;
549 }
552 DotSymtabShndxSec = &Sec;
553 break;
554 }
555 }
556 }
557
558 ContentValid = true;
559 return Error::success();
560}
561
562template <class ELFT>
564 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
565 if (!SymOrErr)
566 return SymOrErr.takeError();
567 auto SymTabOrErr = EF.getSection(Sym.d.a);
568 if (!SymTabOrErr)
569 return SymTabOrErr.takeError();
570 const Elf_Shdr *SymTableSec = *SymTabOrErr;
571 auto StrTabOrErr = EF.getSection(SymTableSec->sh_link);
572 if (!StrTabOrErr)
573 return StrTabOrErr.takeError();
574 const Elf_Shdr *StringTableSec = *StrTabOrErr;
575 auto SymStrTabOrErr = EF.getStringTable(*StringTableSec);
576 if (!SymStrTabOrErr)
577 return SymStrTabOrErr.takeError();
578 Expected<StringRef> Name = (*SymOrErr)->getName(*SymStrTabOrErr);
579 if (Name && !Name->empty())
580 return Name;
581
582 // If the symbol name is empty use the section name.
583 if ((*SymOrErr)->getType() == ELF::STT_SECTION) {
585 if (SecOrErr)
586 return (*SecOrErr)->getName();
587 return SecOrErr.takeError();
588 }
589 return Name;
590}
591
592template <class ELFT>
594 return getSection(Sec)->sh_flags;
595}
596
597template <class ELFT>
599 return getSection(Sec)->sh_type;
600}
601
602template <class ELFT>
604 return getSection(Sec)->sh_offset;
605}
606
607template <class ELFT>
609 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
610 if (!SymOrErr)
611 report_fatal_error(SymOrErr.takeError());
612
613 uint64_t Ret = (*SymOrErr)->st_value;
614 if ((*SymOrErr)->st_shndx == ELF::SHN_ABS)
615 return Ret;
616
617 const Elf_Ehdr &Header = EF.getHeader();
618 // Clear the ARM/Thumb or microMIPS indicator flag.
619 if ((Header.e_machine == ELF::EM_ARM || Header.e_machine == ELF::EM_MIPS) &&
620 (*SymOrErr)->getType() == ELF::STT_FUNC)
621 Ret &= ~1;
622
623 return Ret;
624}
625
626template <class ELFT>
629 Expected<uint64_t> SymbolValueOrErr = getSymbolValue(Symb);
630 if (!SymbolValueOrErr)
631 // TODO: Test this error.
632 return SymbolValueOrErr.takeError();
633
634 uint64_t Result = *SymbolValueOrErr;
635 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
636 if (!SymOrErr)
637 return SymOrErr.takeError();
638
639 switch ((*SymOrErr)->st_shndx) {
640 case ELF::SHN_COMMON:
641 case ELF::SHN_UNDEF:
642 case ELF::SHN_ABS:
643 return Result;
644 }
645
646 auto SymTabOrErr = EF.getSection(Symb.d.a);
647 if (!SymTabOrErr)
648 return SymTabOrErr.takeError();
649
650 if (EF.getHeader().e_type == ELF::ET_REL) {
651 ArrayRef<Elf_Word> ShndxTable;
652 if (DotSymtabShndxSec) {
653 // TODO: Test this error.
654 if (Expected<ArrayRef<Elf_Word>> ShndxTableOrErr =
655 EF.getSHNDXTable(*DotSymtabShndxSec))
656 ShndxTable = *ShndxTableOrErr;
657 else
658 return ShndxTableOrErr.takeError();
659 }
660
661 Expected<const Elf_Shdr *> SectionOrErr =
662 EF.getSection(**SymOrErr, *SymTabOrErr, ShndxTable);
663 if (!SectionOrErr)
664 return SectionOrErr.takeError();
665 const Elf_Shdr *Section = *SectionOrErr;
666 if (Section)
667 Result += Section->sh_addr;
668 }
669
670 return Result;
671}
672
673template <class ELFT>
675 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
676 if (!SymOrErr)
677 report_fatal_error(SymOrErr.takeError());
678 if ((*SymOrErr)->st_shndx == ELF::SHN_COMMON)
679 return (*SymOrErr)->st_value;
680 return 0;
681}
682
683template <class ELFT>
685 return EF.getHeader().e_machine;
686}
687
688template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const {
689 return EF.getHeader().e_type;
690}
691
692template <class ELFT> uint8_t ELFObjectFile<ELFT>::getEIdentABIVersion() const {
693 return EF.getHeader().e_ident[ELF::EI_ABIVERSION];
694}
695
696template <class ELFT> uint8_t ELFObjectFile<ELFT>::getEIdentOSABI() const {
697 return EF.getHeader().e_ident[ELF::EI_OSABI];
698}
699
700template <class ELFT>
701uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
702 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
703 if (!SymOrErr)
704 report_fatal_error(SymOrErr.takeError());
705 return (*SymOrErr)->st_size;
706}
707
708template <class ELFT>
710 return getSymbolSize(Symb);
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)->getBinding();
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)->st_other;
727}
728
729template <class ELFT>
731 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
732 if (!SymOrErr)
733 report_fatal_error(SymOrErr.takeError());
734 return (*SymOrErr)->getType();
735}
736
737template <class ELFT>
740 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
741 if (!SymOrErr)
742 return SymOrErr.takeError();
743
744 switch ((*SymOrErr)->getType()) {
745 case ELF::STT_NOTYPE:
747 case ELF::STT_SECTION:
748 return SymbolRef::ST_Debug;
749 case ELF::STT_FILE:
750 return SymbolRef::ST_File;
751 case ELF::STT_FUNC:
753 case ELF::STT_OBJECT:
754 case ELF::STT_COMMON:
755 return SymbolRef::ST_Data;
756 case ELF::STT_TLS:
757 default:
758 return SymbolRef::ST_Other;
759 }
760}
761
762template <class ELFT>
764 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
765 if (!SymOrErr)
766 return SymOrErr.takeError();
767
768 const Elf_Sym *ESym = *SymOrErr;
770
771 if (ESym->getBinding() != ELF::STB_LOCAL)
772 Result |= SymbolRef::SF_Global;
773
774 if (ESym->getBinding() == ELF::STB_WEAK)
775 Result |= SymbolRef::SF_Weak;
776
777 if (ESym->st_shndx == ELF::SHN_ABS)
778 Result |= SymbolRef::SF_Absolute;
779
780 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION)
782
783 if (Expected<typename ELFT::SymRange> SymbolsOrErr =
784 EF.symbols(DotSymtabSec)) {
785 // Set the SF_FormatSpecific flag for the 0-index null symbol.
786 if (ESym == SymbolsOrErr->begin())
788 } else
789 // TODO: Test this error.
790 return SymbolsOrErr.takeError();
791
792 if (Expected<typename ELFT::SymRange> SymbolsOrErr =
793 EF.symbols(DotDynSymSec)) {
794 // Set the SF_FormatSpecific flag for the 0-index null symbol.
795 if (ESym == SymbolsOrErr->begin())
797 } else
798 // TODO: Test this error.
799 return SymbolsOrErr.takeError();
800
801 if (EF.getHeader().e_machine == ELF::EM_AARCH64) {
802 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
803 StringRef Name = *NameOrErr;
804 if (Name.starts_with("$d") || Name.starts_with("$x"))
806 } else {
807 // TODO: Actually report errors helpfully.
808 consumeError(NameOrErr.takeError());
809 }
810 } else if (EF.getHeader().e_machine == ELF::EM_ARM) {
811 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
812 StringRef Name = *NameOrErr;
813 // TODO Investigate why empty name symbols need to be marked.
814 if (Name.empty() || Name.starts_with("$d") || Name.starts_with("$t") ||
815 Name.starts_with("$a"))
817 } else {
818 // TODO: Actually report errors helpfully.
819 consumeError(NameOrErr.takeError());
820 }
821 if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1)
822 Result |= SymbolRef::SF_Thumb;
823 } else if (EF.getHeader().e_machine == ELF::EM_CSKY) {
824 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
825 StringRef Name = *NameOrErr;
826 if (Name.starts_with("$d") || Name.starts_with("$t"))
828 } else {
829 // TODO: Actually report errors helpfully.
830 consumeError(NameOrErr.takeError());
831 }
832 } else if (EF.getHeader().e_machine == ELF::EM_RISCV) {
833 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
834 StringRef Name = *NameOrErr;
835 // Mark fake labels (used for label differences) and mapping symbols.
836 if (Name == ".L0 " || Name.starts_with("$d") || Name.starts_with("$x"))
838 } else {
839 // TODO: Actually report errors helpfully.
840 consumeError(NameOrErr.takeError());
841 }
842 }
843
844 if (ESym->st_shndx == ELF::SHN_UNDEF)
845 Result |= SymbolRef::SF_Undefined;
846
847 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
848 Result |= SymbolRef::SF_Common;
849
850 if (isExportedToOtherDSO(ESym))
851 Result |= SymbolRef::SF_Exported;
852
853 if (ESym->getType() == ELF::STT_GNU_IFUNC)
854 Result |= SymbolRef::SF_Indirect;
855
856 if (ESym->getVisibility() == ELF::STV_HIDDEN)
857 Result |= SymbolRef::SF_Hidden;
858
859 return Result;
860}
861
862template <class ELFT>
865 const Elf_Shdr *SymTab) const {
866 ArrayRef<Elf_Word> ShndxTable;
867 if (DotSymtabShndxSec) {
868 // TODO: Test this error.
869 Expected<ArrayRef<Elf_Word>> ShndxTableOrErr =
870 EF.getSHNDXTable(*DotSymtabShndxSec);
871 if (!ShndxTableOrErr)
872 return ShndxTableOrErr.takeError();
873 ShndxTable = *ShndxTableOrErr;
874 }
875
876 auto ESecOrErr = EF.getSection(*ESym, SymTab, ShndxTable);
877 if (!ESecOrErr)
878 return ESecOrErr.takeError();
879
880 const Elf_Shdr *ESec = *ESecOrErr;
881 if (!ESec)
882 return section_end();
883
884 DataRefImpl Sec;
885 Sec.p = reinterpret_cast<intptr_t>(ESec);
886 return section_iterator(SectionRef(Sec, this));
887}
888
889template <class ELFT>
892 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
893 if (!SymOrErr)
894 return SymOrErr.takeError();
895
896 auto SymTabOrErr = EF.getSection(Symb.d.a);
897 if (!SymTabOrErr)
898 return SymTabOrErr.takeError();
899 return getSymbolSection(*SymOrErr, *SymTabOrErr);
900}
901
902template <class ELFT>
904 const Elf_Shdr *ESec = getSection(Sec);
905 Sec = toDRI(++ESec);
906}
907
908template <class ELFT>
910 return EF.getSectionName(*getSection(Sec));
911}
912
913template <class ELFT>
915 return getSection(Sec)->sh_addr;
916}
917
918template <class ELFT>
920 auto SectionsOrErr = EF.sections();
921 handleAllErrors(std::move(SectionsOrErr.takeError()),
922 [](const ErrorInfoBase &) {
923 llvm_unreachable("unable to get section index");
924 });
925 const Elf_Shdr *First = SectionsOrErr->begin();
926 return getSection(Sec) - First;
927}
928
929template <class ELFT>
931 return getSection(Sec)->sh_size;
932}
933
934template <class ELFT>
937 const Elf_Shdr *EShdr = getSection(Sec);
938 if (EShdr->sh_type == ELF::SHT_NOBITS)
939 return ArrayRef((const uint8_t *)base(), (size_t)0);
940 if (Error E =
942 (uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
943 return std::move(E);
944 return ArrayRef((const uint8_t *)base() + EShdr->sh_offset, EShdr->sh_size);
945}
946
947template <class ELFT>
949 return getSection(Sec)->sh_addralign;
950}
951
952template <class ELFT>
954 return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED;
955}
956
957template <class ELFT>
959 return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR;
960}
961
962template <class ELFT>
964 const Elf_Shdr *EShdr = getSection(Sec);
965 return (EShdr->sh_flags & ELF::SHF_ALLOC) &&
966 !(EShdr->sh_flags & ELF::SHF_EXECINSTR) &&
967 EShdr->sh_type != ELF::SHT_NOBITS;
968}
969
970template <class ELFT>
972 const Elf_Shdr *EShdr = getSection(Sec);
973 return EShdr->sh_flags & ELF::SHF_ALLOC && EShdr->sh_type == ELF::SHT_NOBITS;
974}
975
976template <class ELFT>
977std::vector<SectionRef>
979 std::vector<SectionRef> Res;
980 std::vector<uintptr_t> Offsets;
981
982 auto SectionsOrErr = EF.sections();
983 if (!SectionsOrErr)
984 return Res;
985
986 for (const Elf_Shdr &Sec : *SectionsOrErr) {
987 if (Sec.sh_type != ELF::SHT_DYNAMIC)
988 continue;
989 Elf_Dyn *Dynamic =
990 reinterpret_cast<Elf_Dyn *>((uintptr_t)base() + Sec.sh_offset);
991 for (; Dynamic->d_tag != ELF::DT_NULL; Dynamic++) {
992 if (Dynamic->d_tag == ELF::DT_REL || Dynamic->d_tag == ELF::DT_RELA ||
993 Dynamic->d_tag == ELF::DT_JMPREL) {
994 Offsets.push_back(Dynamic->d_un.d_val);
995 }
996 }
997 }
998 for (const Elf_Shdr &Sec : *SectionsOrErr) {
999 if (is_contained(Offsets, Sec.sh_addr))
1000 Res.emplace_back(toDRI(&Sec), this);
1001 }
1002 return Res;
1003}
1004
1005template <class ELFT>
1007 return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
1008}
1009
1010template <class ELFT>
1012 return getSection(Sec)->sh_flags & ELF::SHF_ALLOC &&
1013 (getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR ||
1014 !(getSection(Sec)->sh_flags & ELF::SHF_WRITE));
1015}
1016
1017template <class ELFT>
1019 const Elf_Shdr *EShdr = getSection(Sec);
1020 return !isBerkeleyText(Sec) && EShdr->sh_type != ELF::SHT_NOBITS &&
1021 EShdr->sh_flags & ELF::SHF_ALLOC;
1022}
1023
1024template <class ELFT>
1026 Expected<StringRef> SectionNameOrErr = getSectionName(Sec);
1027 if (!SectionNameOrErr) {
1028 // TODO: Report the error message properly.
1029 consumeError(SectionNameOrErr.takeError());
1030 return false;
1031 }
1032 StringRef SectionName = SectionNameOrErr.get();
1033 return SectionName.starts_with(".debug") ||
1034 SectionName.starts_with(".zdebug") || SectionName == ".gdb_index";
1035}
1036
1037template <class ELFT>
1040 DataRefImpl RelData;
1041 auto SectionsOrErr = EF.sections();
1042 if (!SectionsOrErr)
1044 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
1045 RelData.d.a = (Sec.p - SHT) / EF.getHeader().e_shentsize;
1046 RelData.d.b = 0;
1047 if (reinterpret_cast<const Elf_Shdr *>(Sec.p)->sh_type == ELF::SHT_CREL) {
1048 if (RelData.d.a + 1 > Crels.size())
1049 Crels.resize(RelData.d.a + 1);
1050 auto &Crel = Crels[RelData.d.a];
1051 if (Crel.empty()) {
1053 size_t I = 0;
1055 Content, [&](uint64_t Count, bool) { Crel.resize(Count); },
1056 [&](Elf_Crel Crel) { Crels[RelData.d.a][I++] = Crel; });
1057 if (Err) {
1058 Crel.assign(1, Elf_Crel{0, 0, 0, 0});
1059 if (RelData.d.a + 1 > CrelDecodeProblems.size())
1060 CrelDecodeProblems.resize(RelData.d.a + 1);
1061 CrelDecodeProblems[RelData.d.a] = toString(std::move(Err));
1062 }
1063 }
1064 }
1065 return relocation_iterator(RelocationRef(RelData, this));
1066}
1067
1068template <class ELFT>
1071 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1073 DataRefImpl RelData = Begin->getRawDataRefImpl();
1074 if (S->sh_type == ELF::SHT_CREL) {
1075 RelData.d.b = Crels[RelData.d.a].size();
1076 return relocation_iterator(RelocationRef(RelData, this));
1077 }
1078 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
1079 return Begin;
1080 const Elf_Shdr *RelSec = getRelSection(RelData);
1081
1082 // Error check sh_link here so that getRelocationSymbol can just use it.
1083 auto SymSecOrErr = EF.getSection(RelSec->sh_link);
1084 if (!SymSecOrErr)
1086 Twine(errorToErrorCode(SymSecOrErr.takeError()).message()));
1087
1088 RelData.d.b += S->sh_size / S->sh_entsize;
1089 return relocation_iterator(RelocationRef(RelData, this));
1090}
1091
1092template <class ELFT>
1095 const Elf_Shdr *EShdr = getSection(Sec);
1096 uintX_t Type = EShdr->sh_type;
1098 return section_end();
1099
1100 Expected<const Elf_Shdr *> SecOrErr = EF.getSection(EShdr->sh_info);
1101 if (!SecOrErr)
1102 return SecOrErr.takeError();
1103 return section_iterator(SectionRef(toDRI(*SecOrErr), this));
1104}
1105
1106// Relocations
1107template <class ELFT>
1109 ++Rel.d.b;
1110}
1111
1112template <class ELFT>
1115 uint32_t symbolIdx;
1116 const Elf_Shdr *sec = getRelSection(Rel);
1117 if (sec->sh_type == ELF::SHT_CREL)
1118 symbolIdx = getCrel(Rel).r_symidx;
1119 else if (sec->sh_type == ELF::SHT_REL)
1120 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
1121 else
1122 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
1123 if (!symbolIdx)
1124 return symbol_end();
1125
1126 // FIXME: error check symbolIdx
1127 DataRefImpl SymbolData;
1128 SymbolData.d.a = sec->sh_link;
1129 SymbolData.d.b = symbolIdx;
1130 return symbol_iterator(SymbolRef(SymbolData, this));
1131}
1132
1133template <class ELFT>
1135 const Elf_Shdr *sec = getRelSection(Rel);
1136 if (sec->sh_type == ELF::SHT_CREL)
1137 return getCrel(Rel).r_offset;
1138 if (sec->sh_type == ELF::SHT_REL)
1139 return getRel(Rel)->r_offset;
1140
1141 return getRela(Rel)->r_offset;
1142}
1143
1144template <class ELFT>
1146 const Elf_Shdr *sec = getRelSection(Rel);
1147 if (sec->sh_type == ELF::SHT_CREL)
1148 return getCrel(Rel).r_type;
1149 if (sec->sh_type == ELF::SHT_REL)
1150 return getRel(Rel)->getType(EF.isMips64EL());
1151 else
1152 return getRela(Rel)->getType(EF.isMips64EL());
1153}
1154
1155template <class ELFT>
1159
1160template <class ELFT>
1162 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1163 uint32_t type = getRelocationType(Rel);
1164 EF.getRelocationTypeName(type, Result);
1165}
1166
1167template <class ELFT>
1170 if (getRelSection(Rel)->sh_type == ELF::SHT_RELA)
1171 return (int64_t)getRela(Rel)->r_addend;
1172 if (getRelSection(Rel)->sh_type == ELF::SHT_CREL)
1173 return (int64_t)getCrel(Rel).r_addend;
1174 return createError("Relocation section does not have addends");
1175}
1176
1177template <class ELFT>
1178const typename ELFObjectFile<ELFT>::Elf_Rel *
1180 assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
1181 auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
1182 if (!Ret)
1183 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message()));
1184 return *Ret;
1185}
1186
1187template <class ELFT>
1188const typename ELFObjectFile<ELFT>::Elf_Rela *
1190 assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
1191 auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
1192 if (!Ret)
1193 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message()));
1194 return *Ret;
1195}
1196
1197template <class ELFT>
1200 assert(getRelSection(Crel)->sh_type == ELF::SHT_CREL);
1201 assert(Crel.d.a < Crels.size());
1202 return Crels[Crel.d.a][Crel.d.b];
1203}
1204
1205template <class ELFT>
1208 auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer());
1209 if (Error E = EFOrErr.takeError())
1210 return std::move(E);
1211
1212 ELFObjectFile<ELFT> Obj = {Object, std::move(*EFOrErr), nullptr, nullptr,
1213 nullptr};
1214 if (InitContent)
1215 if (Error E = Obj.initContent())
1216 return std::move(E);
1217 return std::move(Obj);
1218}
1219
1220template <class ELFT>
1221ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
1222 const Elf_Shdr *DotDynSymSec,
1223 const Elf_Shdr *DotSymtabSec,
1224 const Elf_Shdr *DotSymtabShndx)
1225 : ELFObjectFileBase(getELFType(ELFT::Endianness == llvm::endianness::little,
1226 ELFT::Is64Bits),
1227 Object),
1228 EF(std::move(EF)), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
1229 DotSymtabShndxSec(DotSymtabShndx) {}
1230
1231template <class ELFT>
1232ELFObjectFile<ELFT>::ELFObjectFile(ELFObjectFile<ELFT> &&Other)
1233 : ELFObjectFile(Other.Data, std::move(Other.EF), Other.DotDynSymSec,
1235
1236template <class ELFT>
1238 DataRefImpl Sym =
1240 DotSymtabSec && DotSymtabSec->sh_size >= sizeof(Elf_Sym) ? 1 : 0);
1241 return basic_symbol_iterator(SymbolRef(Sym, this));
1242}
1243
1244template <class ELFT>
1246 const Elf_Shdr *SymTab = DotSymtabSec;
1247 if (!SymTab)
1248 return symbol_begin();
1249 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1250 return basic_symbol_iterator(SymbolRef(Sym, this));
1251}
1252
1253template <class ELFT>
1255 if (!DotDynSymSec || DotDynSymSec->sh_size < sizeof(Elf_Sym))
1256 // Ignore errors here where the dynsym is empty or sh_size less than the
1257 // size of one symbol. These should be handled elsewhere.
1258 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 0), this));
1259 // Skip 0-index NULL symbol.
1260 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 1), this));
1261}
1262
1263template <class ELFT>
1265 const Elf_Shdr *SymTab = DotDynSymSec;
1266 if (!SymTab)
1267 return dynamic_symbol_begin();
1268 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1269 return basic_symbol_iterator(SymbolRef(Sym, 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).begin()), this));
1278}
1279
1280template <class ELFT>
1282 auto SectionsOrErr = EF.sections();
1283 if (!SectionsOrErr)
1284 return section_iterator(SectionRef());
1285 return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
1286}
1287
1288template <class ELFT>
1290 return ELFT::Is64Bits ? 8 : 4;
1291}
1292
1293template <class ELFT>
1295 constexpr bool IsLittleEndian = ELFT::Endianness == llvm::endianness::little;
1296 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1297 case ELF::ELFCLASS32:
1298 switch (EF.getHeader().e_machine) {
1299 case ELF::EM_68K:
1300 return "elf32-m68k";
1301 case ELF::EM_386:
1302 return "elf32-i386";
1303 case ELF::EM_IAMCU:
1304 return "elf32-iamcu";
1305 case ELF::EM_X86_64:
1306 return "elf32-x86-64";
1307 case ELF::EM_ARM:
1308 return (IsLittleEndian ? "elf32-littlearm" : "elf32-bigarm");
1309 case ELF::EM_AVR:
1310 return "elf32-avr";
1311 case ELF::EM_HEXAGON:
1312 return "elf32-hexagon";
1313 case ELF::EM_LANAI:
1314 return "elf32-lanai";
1315 case ELF::EM_MIPS:
1316 return "elf32-mips";
1317 case ELF::EM_MSP430:
1318 return "elf32-msp430";
1319 case ELF::EM_PPC:
1320 return (IsLittleEndian ? "elf32-powerpcle" : "elf32-powerpc");
1321 case ELF::EM_RISCV:
1322 return (IsLittleEndian ? "elf32-littleriscv" : "elf32-bigriscv");
1323 case ELF::EM_CSKY:
1324 return "elf32-csky";
1325 case ELF::EM_SPARC:
1327 return "elf32-sparc";
1328 case ELF::EM_AMDGPU:
1329 return "elf32-amdgpu";
1330 case ELF::EM_LOONGARCH:
1331 return "elf32-loongarch";
1332 case ELF::EM_XTENSA:
1333 return "elf32-xtensa";
1334 default:
1335 return "elf32-unknown";
1336 }
1337 case ELF::ELFCLASS64:
1338 switch (EF.getHeader().e_machine) {
1339 case ELF::EM_386:
1340 return "elf64-i386";
1341 case ELF::EM_X86_64:
1342 return "elf64-x86-64";
1343 case ELF::EM_AARCH64:
1344 return (IsLittleEndian ? "elf64-littleaarch64" : "elf64-bigaarch64");
1345 case ELF::EM_PPC64:
1346 return (IsLittleEndian ? "elf64-powerpcle" : "elf64-powerpc");
1347 case ELF::EM_RISCV:
1348 return (IsLittleEndian ? "elf64-littleriscv" : "elf64-bigriscv");
1349 case ELF::EM_S390:
1350 return "elf64-s390";
1351 case ELF::EM_SPARCV9:
1352 return "elf64-sparc";
1353 case ELF::EM_MIPS:
1354 return "elf64-mips";
1355 case ELF::EM_AMDGPU:
1356 return "elf64-amdgpu";
1357 case ELF::EM_BPF:
1358 return "elf64-bpf";
1359 case ELF::EM_VE:
1360 return "elf64-ve";
1361 case ELF::EM_LOONGARCH:
1362 return "elf64-loongarch";
1363 default:
1364 return "elf64-unknown";
1365 }
1366 default:
1367 // FIXME: Proper error handling.
1368 report_fatal_error("Invalid ELFCLASS!");
1369 }
1370}
1371
1372template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
1373 bool IsLittleEndian = ELFT::Endianness == llvm::endianness::little;
1374 switch (EF.getHeader().e_machine) {
1375 case ELF::EM_68K:
1376 return Triple::m68k;
1377 case ELF::EM_386:
1378 case ELF::EM_IAMCU:
1379 return Triple::x86;
1380 case ELF::EM_X86_64:
1381 return Triple::x86_64;
1382 case ELF::EM_AARCH64:
1383 return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be;
1384 case ELF::EM_ARM:
1385 return Triple::arm;
1386 case ELF::EM_AVR:
1387 return Triple::avr;
1388 case ELF::EM_HEXAGON:
1389 return Triple::hexagon;
1390 case ELF::EM_LANAI:
1391 return Triple::lanai;
1392 case ELF::EM_MIPS:
1393 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1394 case ELF::ELFCLASS32:
1395 return IsLittleEndian ? Triple::mipsel : Triple::mips;
1396 case ELF::ELFCLASS64:
1397 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
1398 default:
1399 report_fatal_error("Invalid ELFCLASS!");
1400 }
1401 case ELF::EM_MSP430:
1402 return Triple::msp430;
1403 case ELF::EM_PPC:
1404 return IsLittleEndian ? Triple::ppcle : Triple::ppc;
1405 case ELF::EM_PPC64:
1406 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
1407 case ELF::EM_RISCV:
1408 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1409 case ELF::ELFCLASS32:
1410 return IsLittleEndian ? Triple::riscv32 : Triple::riscv32be;
1411 case ELF::ELFCLASS64:
1412 return IsLittleEndian ? Triple::riscv64 : Triple::riscv64be;
1413 default:
1414 report_fatal_error("Invalid ELFCLASS!");
1415 }
1416 case ELF::EM_S390:
1417 return Triple::systemz;
1418
1419 case ELF::EM_SPARC:
1421 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
1422 case ELF::EM_SPARCV9:
1423 return Triple::sparcv9;
1424
1425 case ELF::EM_AMDGPU: {
1426 if (!IsLittleEndian)
1427 return Triple::UnknownArch;
1428
1429 unsigned MACH = EF.getHeader().e_flags & ELF::EF_AMDGPU_MACH;
1430 if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST &&
1432 return Triple::r600;
1435 return Triple::amdgcn;
1436
1437 return Triple::UnknownArch;
1438 }
1439
1440 case ELF::EM_CUDA: {
1441 if (EF.getHeader().e_ident[ELF::EI_CLASS] == ELF::ELFCLASS32)
1442 return Triple::nvptx;
1443 return Triple::nvptx64;
1444 }
1445
1446 case ELF::EM_BPF:
1447 return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
1448
1449 case ELF::EM_VE:
1450 return Triple::ve;
1451 case ELF::EM_CSKY:
1452 return Triple::csky;
1453
1454 case ELF::EM_LOONGARCH:
1455 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1456 case ELF::ELFCLASS32:
1457 return Triple::loongarch32;
1458 case ELF::ELFCLASS64:
1459 return Triple::loongarch64;
1460 default:
1461 report_fatal_error("Invalid ELFCLASS!");
1462 }
1463
1464 case ELF::EM_XTENSA:
1465 return Triple::xtensa;
1466
1467 default:
1468 return Triple::UnknownArch;
1469 }
1470}
1471
1472template <class ELFT> Triple::OSType ELFObjectFile<ELFT>::getOS() const {
1473 switch (EF.getHeader().e_ident[ELF::EI_OSABI]) {
1475 return Triple::NetBSD;
1477 return Triple::Linux;
1478 case ELF::ELFOSABI_HURD:
1479 return Triple::Hurd;
1481 return Triple::Solaris;
1482 case ELF::ELFOSABI_AIX:
1483 return Triple::AIX;
1485 return Triple::FreeBSD;
1487 return Triple::OpenBSD;
1488 case ELF::ELFOSABI_CUDA:
1490 return Triple::CUDA;
1492 return Triple::AMDHSA;
1494 return Triple::AMDPAL;
1496 return Triple::Mesa3D;
1497 default:
1498 return Triple::UnknownOS;
1499 }
1500}
1501
1502template <class ELFT>
1504 return EF.getHeader().e_entry;
1505}
1506
1507template <class ELFT>
1512
1513template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
1514 return EF.getHeader().e_type == ELF::ET_REL;
1515}
1516
1517template <class ELFT>
1519 uintptr_t SHT = reinterpret_cast<uintptr_t>(cantFail(EF.sections()).begin());
1520 auto I = (Sec.p - SHT) / EF.getHeader().e_shentsize;
1521 if (I < CrelDecodeProblems.size())
1522 return CrelDecodeProblems[I];
1523 return "";
1524}
1525
1526} // end namespace object
1527} // end namespace llvm
1528
1529#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")
static StringRef getSymbolName(SymbolKind SymKind)
#define LLVM_ABI
Definition Compiler.h:213
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:1001
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:1141
@ SHN_COMMON
Definition ELF.h:1142
@ SHN_UNDEF
Definition ELF.h:1135
@ SHF_ALLOC
Definition ELF.h:1251
@ SHF_COMPRESSED
Definition ELF.h:1279
@ SHF_WRITE
Definition ELF.h:1248
@ SHF_EXECINSTR
Definition ELF.h:1254
@ 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:1158
@ SHT_ARM_ATTRIBUTES
Definition ELF.h:1211
@ SHT_NOBITS
Definition ELF.h:1157
@ SHT_SYMTAB
Definition ELF.h:1151
@ SHT_HEXAGON_ATTRIBUTES
Definition ELF.h:1238
@ SHT_CREL
Definition ELF.h:1171
@ SHT_DYNAMIC
Definition ELF.h:1155
@ SHT_SYMTAB_SHNDX
Definition ELF.h:1165
@ SHT_AARCH64_ATTRIBUTES
Definition ELF.h:1215
@ SHT_RISCV_ATTRIBUTES
Definition ELF.h:1234
@ SHT_RELA
Definition ELF.h:1153
@ SHT_DYNSYM
Definition ELF.h:1160
@ 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:1408
@ STB_LOCAL
Definition ELF.h:1407
@ STB_GNU_UNIQUE
Definition ELF.h:1410
@ STB_WEAK
Definition ELF.h:1409
@ STT_FUNC
Definition ELF.h:1421
@ STT_NOTYPE
Definition ELF.h:1419
@ STT_SECTION
Definition ELF.h:1422
@ STT_FILE
Definition ELF.h:1423
@ STT_COMMON
Definition ELF.h:1424
@ STT_GNU_IFUNC
Definition ELF.h:1426
@ STT_OBJECT
Definition ELF.h:1420
@ STT_TLS
Definition ELF.h:1425
@ ELFCLASS64
Definition ELF.h:334
@ ELFCLASS32
Definition ELF.h:333
@ ET_REL
Definition ELF.h:119
@ EF_AMDGPU_MACH_AMDGCN_LAST
Definition ELF.h:873
@ EF_AMDGPU_MACH_R600_LAST
Definition ELF.h:862
@ EF_AMDGPU_MACH_AMDGCN_FIRST
Definition ELF.h:872
@ EF_AMDGPU_MACH
Definition ELF.h:848
@ EF_AMDGPU_MACH_R600_FIRST
Definition ELF.h:861
@ STV_HIDDEN
Definition ELF.h:1439
@ STV_PROTECTED
Definition ELF.h:1440
@ STV_DEFAULT
Definition ELF.h:1437
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))
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Definition ELF.h:608
bool operator<(const ELFSymbolRef &A, const ELFSymbolRef &B)
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
constexpr int NumElfSymbolTypes
content_iterator< BasicSymbolRef > basic_symbol_iterator
ELFObjectFile< ELF64LE > ELF64LEObjectFile
ELFObjectFile< ELF32LE > ELF32LEObjectFile
static Error decodeCrel(ArrayRef< uint8_t > Content, function_ref< void(uint64_t, bool)> HdrHandler, function_ref< void(Elf_Crel_Impl< Is64 >)> EntryHandler)
Definition ELF.h:218
LLVM_ABI const llvm::EnumEntry< unsigned > ElfSymbolTypes[NumElfSymbolTypes]
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.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
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
@ 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:1916
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:1946
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:874
std::optional< DataRefImpl > Symbol
struct llvm::object::DataRefImpl::@005117267142344013370254144343227032034000327225 d