LLVM 24.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 SubtargetFeatures getSparcFeatures() const;
69
70 StringRef getAMDGPUCPUName() const;
71 StringRef getNVPTXCPUName() const;
72
73protected:
74 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
75
76 virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
77 virtual uint8_t getSymbolBinding(DataRefImpl Symb) const = 0;
78 virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
79 virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
80
81 virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
82 virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
83 virtual uint64_t getSectionOffset(DataRefImpl Sec) const = 0;
84
86 virtual Error getBuildAttributes(ELFAttributeParser &Attributes) const = 0;
87
88public:
90
92
93 /// Returns platform-specific object flags, if any.
94 virtual unsigned getPlatformFlags() const = 0;
95
97
98 static bool classof(const Binary *v) { return v->isELF(); }
99
101
102 std::optional<StringRef> tryGetCPUName() const override;
103
104 void setARMSubArch(Triple &TheTriple) const override;
105
106 virtual uint16_t getEType() const = 0;
107
108 virtual uint16_t getEMachine() const = 0;
109
110 virtual uint8_t getEIdentABIVersion() const = 0;
111
112 virtual uint8_t getEIdentOSABI() const = 0;
113
114 std::vector<ELFPltEntry> getPltEntries(const MCSubtargetInfo &STI) const;
115
116 /// Returns a vector containing a symbol version for each dynamic symbol.
117 /// Returns an empty vector if version sections do not exist.
119
120 /// Returns a vector of all BB address maps in the object file. When
121 /// `TextSectionIndex` is specified, only returns the BB address maps
122 /// corresponding to the section with that index. When `PGOAnalyses`is
123 /// specified (PGOAnalyses is not nullptr), the vector is cleared then filled
124 /// with extra PGO data. `PGOAnalyses` will always be the same length as the
125 /// return value when it is requested assuming no error occurs. Upon failure,
126 /// `PGOAnalyses` will be emptied.
128 readBBAddrMap(std::optional<unsigned> TextSectionIndex = std::nullopt,
129 std::vector<PGOAnalysisMap> *PGOAnalyses = nullptr) const;
130
132};
133
156
158public:
162
163 const ELFSectionRef *operator->() const {
164 return static_cast<const ELFSectionRef *>(section_iterator::operator->());
165 }
166
167 const ELFSectionRef &operator*() const {
168 return static_cast<const ELFSectionRef &>(section_iterator::operator*());
169 }
170};
171
172class ELFSymbolRef : public SymbolRef {
173public:
177
181
184 }
185
189
192 }
193
197
199 return getElfSymbolTypes().toString(getELFType(), 1);
200 }
201};
202
203inline bool operator<(const ELFSymbolRef &A, const ELFSymbolRef &B) {
204 const DataRefImpl &DRIA = A.getRawDataRefImpl();
205 const DataRefImpl &DRIB = B.getRawDataRefImpl();
206 if (DRIA.d.a == DRIB.d.a)
207 return DRIA.d.b < DRIB.d.b;
208 return DRIA.d.a < DRIB.d.a;
209}
210
212public:
216
217 const ELFSymbolRef *operator->() const {
218 return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
219 }
220
221 const ELFSymbolRef &operator*() const {
222 return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
223 }
224};
225
240
242public:
246
248 return static_cast<const ELFRelocationRef *>(
250 }
251
253 return static_cast<const ELFRelocationRef &>(
255 }
256};
257
262
263template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
264 uint16_t getEMachine() const override;
265 uint16_t getEType() const override;
266 uint8_t getEIdentABIVersion() const override;
267 uint8_t getEIdentOSABI() const override;
268 uint64_t getSymbolSize(DataRefImpl Sym) const override;
269
270public:
272
273 SectionRef toSectionRef(const Elf_Shdr *Sec) const {
274 return SectionRef(toDRI(Sec), this);
275 }
276
277 ELFSymbolRef toSymbolRef(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
278 return ELFSymbolRef({toDRI(SymTable, SymbolNum), this});
279 }
280
281 bool IsContentValid() const { return ContentValid; }
282
283private:
285 const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,
286 const Elf_Shdr *DotSymtabShndxSec);
287
288 bool ContentValid = false;
289
290protected:
292
293 const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
294 const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
295 const Elf_Shdr *DotSymtabShndxSec = nullptr; // SHT_SYMTAB_SHNDX section.
296
297 // Hold CREL relocations for SectionRef::relocations().
300
301 Error initContent() override;
302
303 void moveSymbolNext(DataRefImpl &Symb) const override;
311 uint8_t getSymbolOther(DataRefImpl Symb) const override;
315 const Elf_Shdr *SymTab) const;
317
318 void moveSectionNext(DataRefImpl &Sec) const override;
324 getSectionContents(DataRefImpl Sec) const override;
326 bool isSectionCompressed(DataRefImpl Sec) const override;
327 bool isSectionText(DataRefImpl Sec) const override;
328 bool isSectionData(DataRefImpl Sec) const override;
329 bool isSectionBSS(DataRefImpl Sec) const override;
330 bool isSectionVirtual(DataRefImpl Sec) const override;
331 bool isBerkeleyText(DataRefImpl Sec) const override;
332 bool isBerkeleyData(DataRefImpl Sec) const override;
333 bool isDebugSection(DataRefImpl Sec) const override;
336 std::vector<SectionRef> dynamic_relocation_sections() const override;
338 getRelocatedSection(DataRefImpl Sec) const override;
339
340 void moveRelocationNext(DataRefImpl &Rel) const override;
345 SmallVectorImpl<char> &Result) const override;
346
351
352 DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
353 DataRefImpl DRI;
354 if (!SymTable) {
355 DRI.d.a = 0;
356 DRI.d.b = 0;
357 return DRI;
358 }
359 assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
360 SymTable->sh_type == ELF::SHT_DYNSYM);
361
362 auto SectionsOrErr = EF.sections();
363 if (!SectionsOrErr) {
364 DRI.d.a = 0;
365 DRI.d.b = 0;
366 return DRI;
367 }
368 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
369 unsigned SymTableIndex =
370 (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
371
372 DRI.d.a = SymTableIndex;
373 DRI.d.b = SymbolNum;
374 return DRI;
375 }
376
377 const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
378 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
379 }
380
381 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
382 DataRefImpl DRI;
383 DRI.p = reinterpret_cast<uintptr_t>(Sec);
384 return DRI;
385 }
386
387 DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
388 DataRefImpl DRI;
389 DRI.p = reinterpret_cast<uintptr_t>(Dyn);
390 return DRI;
391 }
392
393 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
394 unsigned char Binding = ESym->getBinding();
395 unsigned char Visibility = ESym->getVisibility();
396
397 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
398 // visibility is either DEFAULT or PROTECTED. All other symbols are not
399 // exported.
400 return (
403 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED));
404 }
405
406 Error getBuildAttributes(ELFAttributeParser &Attributes) const override {
408 switch (getEMachine()) {
409 case ELF::EM_ARM:
411 break;
412 case ELF::EM_AARCH64:
414 break;
415 case ELF::EM_RISCV:
417 break;
418 case ELF::EM_HEXAGON:
420 break;
421 default:
422 return Error::success();
423 }
424
425 auto SectionsOrErr = EF.sections();
426 if (!SectionsOrErr)
427 return SectionsOrErr.takeError();
428 for (const Elf_Shdr &Sec : *SectionsOrErr) {
429 if (Sec.sh_type != Type)
430 continue;
431 auto ErrorOrContents = EF.getSectionContents(Sec);
432 if (!ErrorOrContents)
433 return ErrorOrContents.takeError();
434
435 auto Contents = ErrorOrContents.get();
436 if (Contents[0] != ELFAttrs::Format_Version || Contents.size() == 1)
437 return Error::success();
438
439 if (Error E = Attributes.parse(Contents, ELFT::Endianness))
440 return E;
441 break;
442 }
443 return Error::success();
444 }
445
446 // This flag is used for classof, to distinguish ELFObjectFile from
447 // its subclass. If more subclasses will be created, this flag will
448 // have to become an enum.
449 bool isDyldELFObject = false;
450
451public:
452 ELFObjectFile(ELFObjectFile<ELFT> &&Other);
454 bool InitContent = true);
455
456 const Elf_Rel *getRel(DataRefImpl Rel) const;
457 const Elf_Rela *getRela(DataRefImpl Rela) const;
458 Elf_Crel getCrel(DataRefImpl Crel) const;
459
461 return EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
462 }
463
464 /// Get the relocation section that contains \a Rel.
465 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
466 auto RelSecOrErr = EF.getSection(Rel.d.a);
467 if (!RelSecOrErr)
469 Twine(errorToErrorCode(RelSecOrErr.takeError()).message()));
470 return *RelSecOrErr;
471 }
472
473 const Elf_Shdr *getSection(DataRefImpl Sec) const {
474 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
475 }
476
479
480 bool is64Bit() const override { return getBytesInAddress() == 8; }
481
484
487
489
490 uint8_t getBytesInAddress() const override;
491 StringRef getFileFormatName() const override;
492 Triple::ArchType getArch() const override;
493 Triple::OSType getOS() const override;
495
496 unsigned getPlatformFlags() const override { return EF.getHeader().e_flags; }
497
498 const ELFFile<ELFT> &getELFFile() const { return EF; }
499
500 bool isDyldType() const { return isDyldELFObject; }
501 static bool classof(const Binary *v) {
502 return v->getType() ==
503 getELFType(ELFT::Endianness == llvm::endianness::little,
504 ELFT::Is64Bits);
505 }
506
508
509 bool isRelocatableObject() const override;
510
511 void createFakeSections() { EF.createFakeSections(); }
512
514};
515
520
521template <class ELFT>
523 ++Sym.d.b;
524}
525
527 auto SectionsOrErr = EF.sections();
528 if (!SectionsOrErr)
529 return SectionsOrErr.takeError();
530
531 for (const Elf_Shdr &Sec : *SectionsOrErr) {
532 switch (Sec.sh_type) {
533 case ELF::SHT_DYNSYM: {
534 if (!DotDynSymSec)
535 DotDynSymSec = &Sec;
536 break;
537 }
538 case ELF::SHT_SYMTAB: {
539 if (!DotSymtabSec)
540 DotSymtabSec = &Sec;
541 break;
542 }
545 DotSymtabShndxSec = &Sec;
546 break;
547 }
548 }
549 }
550
551 ContentValid = true;
552 return Error::success();
553}
554
555template <class ELFT>
557 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
558 if (!SymOrErr)
559 return SymOrErr.takeError();
560 auto SymTabOrErr = EF.getSection(Sym.d.a);
561 if (!SymTabOrErr)
562 return SymTabOrErr.takeError();
563 const Elf_Shdr *SymTableSec = *SymTabOrErr;
564 auto StrTabOrErr = EF.getSection(SymTableSec->sh_link);
565 if (!StrTabOrErr)
566 return StrTabOrErr.takeError();
567 const Elf_Shdr *StringTableSec = *StrTabOrErr;
568 auto SymStrTabOrErr = EF.getStringTable(*StringTableSec);
569 if (!SymStrTabOrErr)
570 return SymStrTabOrErr.takeError();
571 Expected<StringRef> Name = (*SymOrErr)->getName(*SymStrTabOrErr);
572 if (Name && !Name->empty())
573 return Name;
574
575 // If the symbol name is empty use the section name.
576 if ((*SymOrErr)->getType() == ELF::STT_SECTION) {
578 if (SecOrErr)
579 return (*SecOrErr)->getName();
580 return SecOrErr.takeError();
581 }
582 return Name;
583}
584
585template <class ELFT>
587 return getSection(Sec)->sh_flags;
588}
589
590template <class ELFT>
592 return getSection(Sec)->sh_type;
593}
594
595template <class ELFT>
597 return getSection(Sec)->sh_offset;
598}
599
600template <class ELFT>
602 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
603 if (!SymOrErr)
604 report_fatal_error(SymOrErr.takeError());
605
606 uint64_t Ret = (*SymOrErr)->st_value;
607 if ((*SymOrErr)->st_shndx == ELF::SHN_ABS)
608 return Ret;
609
610 const Elf_Ehdr &Header = EF.getHeader();
611 // Clear the ARM/Thumb or microMIPS indicator flag.
612 if ((Header.e_machine == ELF::EM_ARM || Header.e_machine == ELF::EM_MIPS) &&
613 (*SymOrErr)->getType() == ELF::STT_FUNC)
614 Ret &= ~1;
615
616 return Ret;
617}
618
619template <class ELFT>
622 Expected<uint64_t> SymbolValueOrErr = getSymbolValue(Symb);
623 if (!SymbolValueOrErr)
624 // TODO: Test this error.
625 return SymbolValueOrErr.takeError();
626
627 uint64_t Result = *SymbolValueOrErr;
628 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
629 if (!SymOrErr)
630 return SymOrErr.takeError();
631
632 switch ((*SymOrErr)->st_shndx) {
633 case ELF::SHN_COMMON:
634 case ELF::SHN_UNDEF:
635 case ELF::SHN_ABS:
636 return Result;
637 }
638
639 auto SymTabOrErr = EF.getSection(Symb.d.a);
640 if (!SymTabOrErr)
641 return SymTabOrErr.takeError();
642
643 if (EF.getHeader().e_type == ELF::ET_REL) {
644 ArrayRef<Elf_Word> ShndxTable;
645 if (DotSymtabShndxSec) {
646 // TODO: Test this error.
647 if (Expected<ArrayRef<Elf_Word>> ShndxTableOrErr =
648 EF.getSHNDXTable(*DotSymtabShndxSec))
649 ShndxTable = *ShndxTableOrErr;
650 else
651 return ShndxTableOrErr.takeError();
652 }
653
654 Expected<const Elf_Shdr *> SectionOrErr =
655 EF.getSection(**SymOrErr, *SymTabOrErr, ShndxTable);
656 if (!SectionOrErr)
657 return SectionOrErr.takeError();
658 const Elf_Shdr *Section = *SectionOrErr;
659 if (Section)
660 Result += Section->sh_addr;
661 }
662
663 return Result;
664}
665
666template <class ELFT>
668 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
669 if (!SymOrErr)
670 report_fatal_error(SymOrErr.takeError());
671 if ((*SymOrErr)->st_shndx == ELF::SHN_COMMON)
672 return (*SymOrErr)->st_value;
673 return 0;
674}
675
676template <class ELFT>
678 return EF.getHeader().e_machine;
679}
680
681template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const {
682 return EF.getHeader().e_type;
683}
684
685template <class ELFT> uint8_t ELFObjectFile<ELFT>::getEIdentABIVersion() const {
686 return EF.getHeader().e_ident[ELF::EI_ABIVERSION];
687}
688
689template <class ELFT> uint8_t ELFObjectFile<ELFT>::getEIdentOSABI() const {
690 return EF.getHeader().e_ident[ELF::EI_OSABI];
691}
692
693template <class ELFT>
694uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
695 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
696 if (!SymOrErr)
697 report_fatal_error(SymOrErr.takeError());
698 return (*SymOrErr)->st_size;
699}
700
701template <class ELFT>
703 return getSymbolSize(Symb);
704}
705
706template <class ELFT>
708 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
709 if (!SymOrErr)
710 report_fatal_error(SymOrErr.takeError());
711 return (*SymOrErr)->getBinding();
712}
713
714template <class ELFT>
716 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
717 if (!SymOrErr)
718 report_fatal_error(SymOrErr.takeError());
719 return (*SymOrErr)->st_other;
720}
721
722template <class ELFT>
724 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
725 if (!SymOrErr)
726 report_fatal_error(SymOrErr.takeError());
727 return (*SymOrErr)->getType();
728}
729
730template <class ELFT>
733 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
734 if (!SymOrErr)
735 return SymOrErr.takeError();
736
737 switch ((*SymOrErr)->getType()) {
738 case ELF::STT_NOTYPE:
740 case ELF::STT_SECTION:
741 return SymbolRef::ST_Debug;
742 case ELF::STT_FILE:
743 return SymbolRef::ST_File;
744 case ELF::STT_FUNC:
746 case ELF::STT_OBJECT:
747 case ELF::STT_COMMON:
748 return SymbolRef::ST_Data;
749 case ELF::STT_TLS:
750 default:
751 return SymbolRef::ST_Other;
752 }
753}
754
755template <class ELFT>
757 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
758 if (!SymOrErr)
759 return SymOrErr.takeError();
760
761 const Elf_Sym *ESym = *SymOrErr;
763
764 if (ESym->getBinding() != ELF::STB_LOCAL)
765 Result |= SymbolRef::SF_Global;
766
767 if (ESym->getBinding() == ELF::STB_WEAK)
768 Result |= SymbolRef::SF_Weak;
769
770 if (ESym->st_shndx == ELF::SHN_ABS)
771 Result |= SymbolRef::SF_Absolute;
772
773 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION)
775
776 if (Expected<typename ELFT::SymRange> SymbolsOrErr =
777 EF.symbols(DotSymtabSec)) {
778 // Set the SF_FormatSpecific flag for the 0-index null symbol.
779 if (ESym == SymbolsOrErr->begin())
781 } else
782 // TODO: Test this error.
783 return SymbolsOrErr.takeError();
784
785 if (Expected<typename ELFT::SymRange> SymbolsOrErr =
786 EF.symbols(DotDynSymSec)) {
787 // Set the SF_FormatSpecific flag for the 0-index null symbol.
788 if (ESym == SymbolsOrErr->begin())
790 } else
791 // TODO: Test this error.
792 return SymbolsOrErr.takeError();
793
794 if (EF.getHeader().e_machine == ELF::EM_AARCH64) {
795 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
796 StringRef Name = *NameOrErr;
797 if (Name.starts_with("$d") || Name.starts_with("$x"))
799 } else {
800 // TODO: Actually report errors helpfully.
801 consumeError(NameOrErr.takeError());
802 }
803 } else if (EF.getHeader().e_machine == ELF::EM_ARM) {
804 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
805 StringRef Name = *NameOrErr;
806 // TODO Investigate why empty name symbols need to be marked.
807 if (Name.empty() || Name.starts_with("$d") || Name.starts_with("$t") ||
808 Name.starts_with("$a"))
810 } else {
811 // TODO: Actually report errors helpfully.
812 consumeError(NameOrErr.takeError());
813 }
814 if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1)
815 Result |= SymbolRef::SF_Thumb;
816 } else if (EF.getHeader().e_machine == ELF::EM_CSKY) {
817 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
818 StringRef Name = *NameOrErr;
819 if (Name.starts_with("$d") || Name.starts_with("$t"))
821 } else {
822 // TODO: Actually report errors helpfully.
823 consumeError(NameOrErr.takeError());
824 }
825 } else if (EF.getHeader().e_machine == ELF::EM_RISCV) {
826 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
827 StringRef Name = *NameOrErr;
828 // Mark fake labels (used for label differences) and mapping symbols.
829 if (Name == ".L0 " || Name.starts_with("$d") || Name.starts_with("$x"))
831 } else {
832 // TODO: Actually report errors helpfully.
833 consumeError(NameOrErr.takeError());
834 }
835 }
836
837 if (ESym->st_shndx == ELF::SHN_UNDEF)
838 Result |= SymbolRef::SF_Undefined;
839
840 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
841 Result |= SymbolRef::SF_Common;
842
843 if (isExportedToOtherDSO(ESym))
844 Result |= SymbolRef::SF_Exported;
845
846 if (ESym->getType() == ELF::STT_GNU_IFUNC)
847 Result |= SymbolRef::SF_Indirect;
848
849 if (ESym->getVisibility() == ELF::STV_HIDDEN)
850 Result |= SymbolRef::SF_Hidden;
851
852 return Result;
853}
854
855template <class ELFT>
858 const Elf_Shdr *SymTab) const {
859 ArrayRef<Elf_Word> ShndxTable;
860 if (DotSymtabShndxSec) {
861 // TODO: Test this error.
862 Expected<ArrayRef<Elf_Word>> ShndxTableOrErr =
863 EF.getSHNDXTable(*DotSymtabShndxSec);
864 if (!ShndxTableOrErr)
865 return ShndxTableOrErr.takeError();
866 ShndxTable = *ShndxTableOrErr;
867 }
868
869 auto ESecOrErr = EF.getSection(*ESym, SymTab, ShndxTable);
870 if (!ESecOrErr)
871 return ESecOrErr.takeError();
872
873 const Elf_Shdr *ESec = *ESecOrErr;
874 if (!ESec)
875 return section_end();
876
877 DataRefImpl Sec;
878 Sec.p = reinterpret_cast<intptr_t>(ESec);
879 return section_iterator(SectionRef(Sec, this));
880}
881
882template <class ELFT>
885 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
886 if (!SymOrErr)
887 return SymOrErr.takeError();
888
889 auto SymTabOrErr = EF.getSection(Symb.d.a);
890 if (!SymTabOrErr)
891 return SymTabOrErr.takeError();
892 return getSymbolSection(*SymOrErr, *SymTabOrErr);
893}
894
895template <class ELFT>
897 const Elf_Shdr *ESec = getSection(Sec);
898 Sec = toDRI(++ESec);
899}
900
901template <class ELFT>
903 return EF.getSectionName(*getSection(Sec));
904}
905
906template <class ELFT>
908 return getSection(Sec)->sh_addr;
909}
910
911template <class ELFT>
913 auto SectionsOrErr = EF.sections();
914 handleAllErrors(std::move(SectionsOrErr.takeError()),
915 [](const ErrorInfoBase &) {
916 llvm_unreachable("unable to get section index");
917 });
918 const Elf_Shdr *First = SectionsOrErr->begin();
919 return getSection(Sec) - First;
920}
921
922template <class ELFT>
924 return getSection(Sec)->sh_size;
925}
926
927template <class ELFT>
930 const Elf_Shdr *EShdr = getSection(Sec);
931 if (EShdr->sh_type == ELF::SHT_NOBITS)
932 return ArrayRef((const uint8_t *)base(), (size_t)0);
933 if (Error E =
935 (uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
936 return std::move(E);
937 return ArrayRef((const uint8_t *)base() + EShdr->sh_offset, EShdr->sh_size);
938}
939
940template <class ELFT>
942 return getSection(Sec)->sh_addralign;
943}
944
945template <class ELFT>
947 return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED;
948}
949
950template <class ELFT>
952 return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR;
953}
954
955template <class ELFT>
957 const Elf_Shdr *EShdr = getSection(Sec);
958 return (EShdr->sh_flags & ELF::SHF_ALLOC) &&
959 !(EShdr->sh_flags & ELF::SHF_EXECINSTR) &&
960 EShdr->sh_type != ELF::SHT_NOBITS;
961}
962
963template <class ELFT>
965 const Elf_Shdr *EShdr = getSection(Sec);
966 return EShdr->sh_flags & ELF::SHF_ALLOC && EShdr->sh_type == ELF::SHT_NOBITS;
967}
968
969template <class ELFT>
970std::vector<SectionRef>
972 std::vector<SectionRef> Res;
973 std::vector<uintptr_t> Offsets;
974
975 auto SectionsOrErr = EF.sections();
976 if (!SectionsOrErr)
977 return Res;
978
979 for (const Elf_Shdr &Sec : *SectionsOrErr) {
980 if (Sec.sh_type != ELF::SHT_DYNAMIC)
981 continue;
982 Elf_Dyn *Dynamic =
983 reinterpret_cast<Elf_Dyn *>((uintptr_t)base() + Sec.sh_offset);
984 for (; Dynamic->d_tag != ELF::DT_NULL; Dynamic++) {
985 if (Dynamic->d_tag == ELF::DT_REL || Dynamic->d_tag == ELF::DT_RELA ||
986 Dynamic->d_tag == ELF::DT_JMPREL) {
987 Offsets.push_back(Dynamic->d_un.d_val);
988 }
989 }
990 }
991 for (const Elf_Shdr &Sec : *SectionsOrErr) {
992 if (is_contained(Offsets, Sec.sh_addr))
993 Res.emplace_back(toDRI(&Sec), this);
994 }
995 return Res;
996}
997
998template <class ELFT>
1000 return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
1001}
1002
1003template <class ELFT>
1005 return getSection(Sec)->sh_flags & ELF::SHF_ALLOC &&
1006 (getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR ||
1007 !(getSection(Sec)->sh_flags & ELF::SHF_WRITE));
1008}
1009
1010template <class ELFT>
1012 const Elf_Shdr *EShdr = getSection(Sec);
1013 return !isBerkeleyText(Sec) && EShdr->sh_type != ELF::SHT_NOBITS &&
1014 EShdr->sh_flags & ELF::SHF_ALLOC;
1015}
1016
1017template <class ELFT>
1019 Expected<StringRef> SectionNameOrErr = getSectionName(Sec);
1020 if (!SectionNameOrErr) {
1021 // TODO: Report the error message properly.
1022 consumeError(SectionNameOrErr.takeError());
1023 return false;
1024 }
1025 StringRef SectionName = SectionNameOrErr.get();
1026 return SectionName.starts_with(".debug") ||
1027 SectionName.starts_with(".zdebug") || SectionName == ".gdb_index";
1028}
1029
1030template <class ELFT>
1033 DataRefImpl RelData;
1034 auto SectionsOrErr = EF.sections();
1035 if (!SectionsOrErr)
1037 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
1038 RelData.d.a = (Sec.p - SHT) / EF.getHeader().e_shentsize;
1039 RelData.d.b = 0;
1040 if (reinterpret_cast<const Elf_Shdr *>(Sec.p)->sh_type == ELF::SHT_CREL) {
1041 if (RelData.d.a + 1 > Crels.size())
1042 Crels.resize(RelData.d.a + 1);
1043 auto &Crel = Crels[RelData.d.a];
1044 if (Crel.empty()) {
1046 size_t I = 0;
1048 Content, [&](uint64_t Count, bool) { Crel.resize(Count); },
1049 [&](Elf_Crel Crel) { Crels[RelData.d.a][I++] = Crel; });
1050 if (Err) {
1051 Crel.assign(1, Elf_Crel{0, 0, 0, 0});
1052 if (RelData.d.a + 1 > CrelDecodeProblems.size())
1053 CrelDecodeProblems.resize(RelData.d.a + 1);
1054 CrelDecodeProblems[RelData.d.a] = toString(std::move(Err));
1055 }
1056 }
1057 }
1058 return relocation_iterator(RelocationRef(RelData, this));
1059}
1060
1061template <class ELFT>
1064 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1066 DataRefImpl RelData = Begin->getRawDataRefImpl();
1067 if (S->sh_type == ELF::SHT_CREL) {
1068 RelData.d.b = Crels[RelData.d.a].size();
1069 return relocation_iterator(RelocationRef(RelData, this));
1070 }
1071 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
1072 return Begin;
1073 const Elf_Shdr *RelSec = getRelSection(RelData);
1074
1075 // Error check sh_link here so that getRelocationSymbol can just use it.
1076 auto SymSecOrErr = EF.getSection(RelSec->sh_link);
1077 if (!SymSecOrErr)
1079 Twine(errorToErrorCode(SymSecOrErr.takeError()).message()));
1080
1081 RelData.d.b += S->sh_size / S->sh_entsize;
1082 return relocation_iterator(RelocationRef(RelData, this));
1083}
1084
1085template <class ELFT>
1088 const Elf_Shdr *EShdr = getSection(Sec);
1089 uintX_t Type = EShdr->sh_type;
1091 return section_end();
1092
1093 Expected<const Elf_Shdr *> SecOrErr = EF.getSection(EShdr->sh_info);
1094 if (!SecOrErr)
1095 return SecOrErr.takeError();
1096 return section_iterator(SectionRef(toDRI(*SecOrErr), this));
1097}
1098
1099// Relocations
1100template <class ELFT>
1102 ++Rel.d.b;
1103}
1104
1105template <class ELFT>
1108 uint32_t symbolIdx;
1109 const Elf_Shdr *sec = getRelSection(Rel);
1110 if (sec->sh_type == ELF::SHT_CREL)
1111 symbolIdx = getCrel(Rel).r_symidx;
1112 else if (sec->sh_type == ELF::SHT_REL)
1113 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
1114 else
1115 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
1116 if (!symbolIdx)
1117 return symbol_end();
1118
1119 // FIXME: error check symbolIdx
1120 DataRefImpl SymbolData;
1121 SymbolData.d.a = sec->sh_link;
1122 SymbolData.d.b = symbolIdx;
1123 return symbol_iterator(SymbolRef(SymbolData, this));
1124}
1125
1126template <class ELFT>
1128 const Elf_Shdr *sec = getRelSection(Rel);
1129 if (sec->sh_type == ELF::SHT_CREL)
1130 return getCrel(Rel).r_offset;
1131 if (sec->sh_type == ELF::SHT_REL)
1132 return getRel(Rel)->r_offset;
1133
1134 return getRela(Rel)->r_offset;
1135}
1136
1137template <class ELFT>
1139 const Elf_Shdr *sec = getRelSection(Rel);
1140 if (sec->sh_type == ELF::SHT_CREL)
1141 return getCrel(Rel).r_type;
1142 if (sec->sh_type == ELF::SHT_REL)
1143 return getRel(Rel)->getType(EF.isMips64EL());
1144 else
1145 return getRela(Rel)->getType(EF.isMips64EL());
1146}
1147
1148template <class ELFT>
1152
1153template <class ELFT>
1155 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1156 uint32_t type = getRelocationType(Rel);
1157 EF.getRelocationTypeName(type, Result);
1158}
1159
1160template <class ELFT>
1163 if (getRelSection(Rel)->sh_type == ELF::SHT_RELA)
1164 return (int64_t)getRela(Rel)->r_addend;
1165 if (getRelSection(Rel)->sh_type == ELF::SHT_CREL)
1166 return (int64_t)getCrel(Rel).r_addend;
1167 return createError("Relocation section does not have addends");
1168}
1169
1170template <class ELFT>
1171const typename ELFObjectFile<ELFT>::Elf_Rel *
1173 assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
1174 auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
1175 if (!Ret)
1176 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message()));
1177 return *Ret;
1178}
1179
1180template <class ELFT>
1181const typename ELFObjectFile<ELFT>::Elf_Rela *
1183 assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
1184 auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
1185 if (!Ret)
1186 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message()));
1187 return *Ret;
1188}
1189
1190template <class ELFT>
1193 assert(getRelSection(Crel)->sh_type == ELF::SHT_CREL);
1194 assert(Crel.d.a < Crels.size());
1195 return Crels[Crel.d.a][Crel.d.b];
1196}
1197
1198template <class ELFT>
1201 auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer());
1202 if (Error E = EFOrErr.takeError())
1203 return std::move(E);
1204
1205 ELFObjectFile<ELFT> Obj = {Object, std::move(*EFOrErr), nullptr, nullptr,
1206 nullptr};
1207 if (InitContent)
1208 if (Error E = Obj.initContent())
1209 return std::move(E);
1210 return std::move(Obj);
1211}
1212
1213template <class ELFT>
1214ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
1215 const Elf_Shdr *DotDynSymSec,
1216 const Elf_Shdr *DotSymtabSec,
1217 const Elf_Shdr *DotSymtabShndx)
1218 : ELFObjectFileBase(getELFType(ELFT::Endianness == llvm::endianness::little,
1219 ELFT::Is64Bits),
1220 Object),
1221 EF(std::move(EF)), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
1222 DotSymtabShndxSec(DotSymtabShndx) {}
1223
1224template <class ELFT>
1225ELFObjectFile<ELFT>::ELFObjectFile(ELFObjectFile<ELFT> &&Other)
1226 : ELFObjectFile(Other.Data, std::move(Other.EF), Other.DotDynSymSec,
1228
1229template <class ELFT>
1231 DataRefImpl Sym =
1233 DotSymtabSec && DotSymtabSec->sh_size >= sizeof(Elf_Sym) ? 1 : 0);
1234 return basic_symbol_iterator(SymbolRef(Sym, this));
1235}
1236
1237template <class ELFT>
1239 const Elf_Shdr *SymTab = DotSymtabSec;
1240 if (!SymTab)
1241 return symbol_begin();
1242 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1243 return basic_symbol_iterator(SymbolRef(Sym, this));
1244}
1245
1246template <class ELFT>
1248 if (!DotDynSymSec || DotDynSymSec->sh_size < sizeof(Elf_Sym))
1249 // Ignore errors here where the dynsym is empty or sh_size less than the
1250 // size of one symbol. These should be handled elsewhere.
1251 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 0), this));
1252 // Skip 0-index NULL symbol.
1253 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 1), this));
1254}
1255
1256template <class ELFT>
1258 const Elf_Shdr *SymTab = DotDynSymSec;
1259 if (!SymTab)
1260 return dynamic_symbol_begin();
1261 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1262 return basic_symbol_iterator(SymbolRef(Sym, this));
1263}
1264
1265template <class ELFT>
1267 auto SectionsOrErr = EF.sections();
1268 if (!SectionsOrErr)
1269 return section_iterator(SectionRef());
1270 return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this));
1271}
1272
1273template <class ELFT>
1275 auto SectionsOrErr = EF.sections();
1276 if (!SectionsOrErr)
1277 return section_iterator(SectionRef());
1278 return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
1279}
1280
1281template <class ELFT>
1283 return ELFT::Is64Bits ? 8 : 4;
1284}
1285
1286template <class ELFT>
1288 constexpr bool IsLittleEndian = ELFT::Endianness == llvm::endianness::little;
1289 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1290 case ELF::ELFCLASS32:
1291 switch (EF.getHeader().e_machine) {
1292 case ELF::EM_68K:
1293 return "elf32-m68k";
1294 case ELF::EM_386:
1295 return "elf32-i386";
1296 case ELF::EM_IAMCU:
1297 return "elf32-iamcu";
1298 case ELF::EM_X86_64:
1299 return "elf32-x86-64";
1300 case ELF::EM_ARM:
1301 return (IsLittleEndian ? "elf32-littlearm" : "elf32-bigarm");
1302 case ELF::EM_AVR:
1303 return "elf32-avr";
1304 case ELF::EM_HEXAGON:
1305 return "elf32-hexagon";
1306 case ELF::EM_LANAI:
1307 return "elf32-lanai";
1308 case ELF::EM_MIPS:
1309 return "elf32-mips";
1310 case ELF::EM_MSP430:
1311 return "elf32-msp430";
1312 case ELF::EM_PPC:
1313 return (IsLittleEndian ? "elf32-powerpcle" : "elf32-powerpc");
1314 case ELF::EM_RISCV:
1315 return (IsLittleEndian ? "elf32-littleriscv" : "elf32-bigriscv");
1316 case ELF::EM_CSKY:
1317 return "elf32-csky";
1318 case ELF::EM_SPARC:
1319 case ELF::EM_SPARC32PLUS:
1320 return "elf32-sparc";
1321 case ELF::EM_AMDGPU:
1322 return "elf32-amdgpu";
1323 case ELF::EM_LOONGARCH:
1324 return "elf32-loongarch";
1325 case ELF::EM_XTENSA:
1326 return "elf32-xtensa";
1327 case ELF::EM_NANOMIPS:
1328 return "elf32-nanomips";
1329 default:
1330 return "elf32-unknown";
1331 }
1332 case ELF::ELFCLASS64:
1333 switch (EF.getHeader().e_machine) {
1334 case ELF::EM_386:
1335 return "elf64-i386";
1336 case ELF::EM_X86_64:
1337 return "elf64-x86-64";
1338 case ELF::EM_AARCH64:
1339 return (IsLittleEndian ? "elf64-littleaarch64" : "elf64-bigaarch64");
1340 case ELF::EM_PPC64:
1341 return (IsLittleEndian ? "elf64-powerpcle" : "elf64-powerpc");
1342 case ELF::EM_RISCV:
1343 return (IsLittleEndian ? "elf64-littleriscv" : "elf64-bigriscv");
1344 case ELF::EM_S390:
1345 return "elf64-s390";
1346 case ELF::EM_SPARCV9:
1347 return "elf64-sparc";
1348 case ELF::EM_MIPS:
1349 return "elf64-mips";
1350 case ELF::EM_AMDGPU:
1351 return "elf64-amdgpu";
1352 case ELF::EM_BPF:
1353 return "elf64-bpf";
1354 case ELF::EM_VE:
1355 return "elf64-ve";
1356 case ELF::EM_LOONGARCH:
1357 return "elf64-loongarch";
1358 default:
1359 return "elf64-unknown";
1360 }
1361 default:
1362 // FIXME: Proper error handling.
1363 report_fatal_error("Invalid ELFCLASS!");
1364 }
1365}
1366
1367template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
1368 bool IsLittleEndian = ELFT::Endianness == llvm::endianness::little;
1369 switch (EF.getHeader().e_machine) {
1370 case ELF::EM_68K:
1371 return Triple::m68k;
1372 case ELF::EM_386:
1373 case ELF::EM_IAMCU:
1374 return Triple::x86;
1375 case ELF::EM_X86_64:
1376 return Triple::x86_64;
1377 case ELF::EM_AARCH64:
1378 return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be;
1379 case ELF::EM_ARM:
1380 return Triple::arm;
1381 case ELF::EM_AVR:
1382 return Triple::avr;
1383 case ELF::EM_HEXAGON:
1384 return Triple::hexagon;
1385 case ELF::EM_LANAI:
1386 return Triple::lanai;
1387 case ELF::EM_MIPS:
1388 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1389 case ELF::ELFCLASS32:
1390 return IsLittleEndian ? Triple::mipsel : Triple::mips;
1391 case ELF::ELFCLASS64:
1392 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
1393 default:
1394 report_fatal_error("Invalid ELFCLASS!");
1395 }
1396 case ELF::EM_MSP430:
1397 return Triple::msp430;
1398 case ELF::EM_PPC:
1399 return IsLittleEndian ? Triple::ppcle : Triple::ppc;
1400 case ELF::EM_PPC64:
1401 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
1402 case ELF::EM_RISCV:
1403 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1404 case ELF::ELFCLASS32:
1405 return IsLittleEndian ? Triple::riscv32 : Triple::riscv32be;
1406 case ELF::ELFCLASS64:
1407 return IsLittleEndian ? Triple::riscv64 : Triple::riscv64be;
1408 default:
1409 report_fatal_error("Invalid ELFCLASS!");
1410 }
1411 case ELF::EM_S390:
1412 return Triple::systemz;
1413
1414 case ELF::EM_SPARC:
1415 case ELF::EM_SPARC32PLUS:
1416 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
1417 case ELF::EM_SPARCV9:
1418 return Triple::sparcv9;
1419
1420 case ELF::EM_AMDGPU: {
1421 if (!IsLittleEndian)
1422 return Triple::UnknownArch;
1423
1424 unsigned MACH = EF.getHeader().e_flags & ELF::EF_AMDGPU_MACH;
1425 if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST &&
1427 return Triple::r600;
1430 return Triple::amdgpu;
1431
1432 return Triple::UnknownArch;
1433 }
1434
1435 case ELF::EM_CUDA: {
1436 if (EF.getHeader().e_ident[ELF::EI_CLASS] == ELF::ELFCLASS32)
1437 return Triple::nvptx;
1438 return Triple::nvptx64;
1439 }
1440
1441 case ELF::EM_BPF:
1442 return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
1443
1444 case ELF::EM_VE:
1445 return Triple::ve;
1446 case ELF::EM_CSKY:
1447 return Triple::csky;
1448
1449 case ELF::EM_LOONGARCH:
1450 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1451 case ELF::ELFCLASS32:
1452 return Triple::loongarch32;
1453 case ELF::ELFCLASS64:
1454 return Triple::loongarch64;
1455 default:
1456 report_fatal_error("Invalid ELFCLASS!");
1457 }
1458
1459 case ELF::EM_XTENSA:
1460 return Triple::xtensa;
1461
1462 case ELF::EM_NANOMIPS:
1463 return Triple::nanomips;
1464
1465 default:
1466 return Triple::UnknownArch;
1467 }
1468}
1469
1470template <class ELFT> Triple::OSType ELFObjectFile<ELFT>::getOS() const {
1471 switch (EF.getHeader().e_ident[ELF::EI_OSABI]) {
1473 return Triple::NetBSD;
1475 return Triple::Linux;
1476 case ELF::ELFOSABI_HURD:
1477 return Triple::Hurd;
1479 return Triple::Solaris;
1480 case ELF::ELFOSABI_AIX:
1481 return Triple::AIX;
1483 return Triple::FreeBSD;
1485 return Triple::OpenBSD;
1486 case ELF::ELFOSABI_CUDA:
1488 return Triple::CUDA;
1490 return Triple::AMDHSA;
1492 return Triple::AMDPAL;
1494 return Triple::Mesa3D;
1495 default:
1496 return Triple::UnknownOS;
1497 }
1498}
1499
1500template <class ELFT>
1502 return EF.getHeader().e_entry;
1503}
1504
1505template <class ELFT>
1510
1511template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
1512 return EF.getHeader().e_type == ELF::ET_REL;
1513}
1514
1515template <class ELFT>
1517 uintptr_t SHT = reinterpret_cast<uintptr_t>(cantFail(EF.sections()).begin());
1518 auto I = (Sec.p - SHT) / EF.getHeader().e_shentsize;
1519 if (I < CrelDecodeProblems.size())
1520 return CrelDecodeProblems[I];
1521 return "";
1522}
1523
1524} // end namespace object
1525} // end namespace llvm
1526
1527#endif // LLVM_OBJECT_ELFOBJECTFILE_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
uint64_t uint64_t
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:211
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, StringTable 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:48
@ loongarch32
Definition Triple.h:65
@ UnknownArch
Definition Triple.h:51
@ loongarch64
Definition Triple.h:66
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:27
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.
@ EI_ABIVERSION
Definition ELF.h:59
@ EI_CLASS
Definition ELF.h:55
@ EI_OSABI
Definition ELF.h:58
@ STT_FUNC
Definition ELF.h:1274
@ STT_NOTYPE
Definition ELF.h:1272
@ STT_SECTION
Definition ELF.h:1275
@ STT_FILE
Definition ELF.h:1276
@ STT_COMMON
Definition ELF.h:1277
@ STT_GNU_IFUNC
Definition ELF.h:1279
@ STT_OBJECT
Definition ELF.h:1273
@ STT_TLS
Definition ELF.h:1278
@ ELFOSABI_HURD
Definition ELF.h:159
@ ELFOSABI_CUDA
Definition ELF.h:172
@ ELFOSABI_OPENBSD
Definition ELF.h:166
@ ELFOSABI_CUDA_V2
Definition ELF.h:173
@ ELFOSABI_NETBSD
Definition ELF.h:156
@ ELFOSABI_AMDGPU_HSA
Definition ELF.h:175
@ ELFOSABI_SOLARIS
Definition ELF.h:160
@ ELFOSABI_FREEBSD
Definition ELF.h:163
@ ELFOSABI_LINUX
Definition ELF.h:158
@ ELFOSABI_AIX
Definition ELF.h:161
@ ELFOSABI_AMDGPU_MESA3D
Definition ELF.h:177
@ ELFOSABI_AMDGPU_PAL
Definition ELF.h:176
@ STV_HIDDEN
Definition ELF.h:1292
@ STV_PROTECTED
Definition ELF.h:1293
@ STV_DEFAULT
Definition ELF.h:1290
@ SHF_ALLOC
Definition ELF.h:1098
@ SHF_COMPRESSED
Definition ELF.h:1126
@ SHF_WRITE
Definition ELF.h:1095
@ SHF_EXECINSTR
Definition ELF.h:1101
@ ELFCLASS64
Definition ELF.h:142
@ ELFCLASS32
Definition ELF.h:141
@ ET_REL
Definition ELF.h:119
@ EF_AMDGPU_MACH_AMDGCN_LAST
Definition ELF.h:687
@ EF_AMDGPU_MACH_R600_LAST
Definition ELF.h:674
@ EF_AMDGPU_MACH_AMDGCN_FIRST
Definition ELF.h:686
@ EF_AMDGPU_MACH
Definition ELF.h:660
@ EF_AMDGPU_MACH_R600_FIRST
Definition ELF.h:673
@ SHN_ABS
Definition ELF.h:987
@ SHN_COMMON
Definition ELF.h:988
@ SHN_UNDEF
Definition ELF.h:981
@ SHT_REL
Definition ELF.h:1004
@ SHT_ARM_ATTRIBUTES
Definition ELF.h:1058
@ SHT_NOBITS
Definition ELF.h:1003
@ SHT_SYMTAB
Definition ELF.h:997
@ SHT_HEXAGON_ATTRIBUTES
Definition ELF.h:1085
@ SHT_CREL
Definition ELF.h:1017
@ SHT_DYNAMIC
Definition ELF.h:1001
@ SHT_SYMTAB_SHNDX
Definition ELF.h:1011
@ SHT_AARCH64_ATTRIBUTES
Definition ELF.h:1062
@ SHT_RISCV_ATTRIBUTES
Definition ELF.h:1081
@ SHT_RELA
Definition ELF.h:999
@ SHT_DYNSYM
Definition ELF.h:1006
@ STB_GLOBAL
Definition ELF.h:1261
@ STB_LOCAL
Definition ELF.h:1260
@ STB_GNU_UNIQUE
Definition ELF.h:1263
@ STB_WEAK
Definition ELF.h:1262
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:1933
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:1963
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:879
std::optional< DataRefImpl > Symbol
struct llvm::object::DataRefImpl::@005117267142344013370254144343227032034000327225 d