LLVM 19.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"
21#include "llvm/Object/Binary.h"
22#include "llvm/Object/ELF.h"
24#include "llvm/Object/Error.h"
30#include "llvm/Support/Error.h"
36#include <cassert>
37#include <cstdint>
38
39namespace llvm {
40
41template <typename T> class SmallVectorImpl;
42
43namespace object {
44
45constexpr int NumElfSymbolTypes = 16;
47
49
52 std::optional<DataRefImpl> Symbol;
54};
55
57 friend class ELFRelocationRef;
58 friend class ELFSectionRef;
59 friend class ELFSymbolRef;
60
61 SubtargetFeatures getMIPSFeatures() const;
62 SubtargetFeatures getARMFeatures() const;
63 SubtargetFeatures getHexagonFeatures() const;
64 Expected<SubtargetFeatures> getRISCVFeatures() const;
65 SubtargetFeatures getLoongArchFeatures() const;
66
67 StringRef getAMDGPUCPUName() const;
68 StringRef getNVPTXCPUName() const;
69
70protected:
71 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
72
73 virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
74 virtual uint8_t getSymbolBinding(DataRefImpl Symb) const = 0;
75 virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
76 virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
77
78 virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
79 virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
80 virtual uint64_t getSectionOffset(DataRefImpl Sec) const = 0;
81
83 virtual Error getBuildAttributes(ELFAttributeParser &Attributes) const = 0;
84
85public:
87
89
90 /// Returns platform-specific object flags, if any.
91 virtual unsigned getPlatformFlags() const = 0;
92
94
95 static bool classof(const Binary *v) { return v->isELF(); }
96
98
99 std::optional<StringRef> tryGetCPUName() const override;
100
101 void setARMSubArch(Triple &TheTriple) const override;
102
103 virtual uint16_t getEType() const = 0;
104
105 virtual uint16_t getEMachine() const = 0;
106
107 virtual uint8_t getEIdentABIVersion() const = 0;
108
109 std::vector<ELFPltEntry> getPltEntries() const;
110
111 /// Returns a vector containing a symbol version for each dynamic symbol.
112 /// Returns an empty vector if version sections do not exist.
114
115 /// Returns a vector of all BB address maps in the object file. When
116 /// `TextSectionIndex` is specified, only returns the BB address maps
117 /// corresponding to the section with that index. When `PGOAnalyses`is
118 /// specified (PGOAnalyses is not nullptr), the vector is cleared then filled
119 /// with extra PGO data. `PGOAnalyses` will always be the same length as the
120 /// return value when it is requested assuming no error occurs. Upon failure,
121 /// `PGOAnalyses` will be emptied.
123 readBBAddrMap(std::optional<unsigned> TextSectionIndex = std::nullopt,
124 std::vector<PGOAnalysisMap> *PGOAnalyses = nullptr) const;
125};
126
127class ELFSectionRef : public SectionRef {
128public:
130 assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
131 }
132
134 return cast<ELFObjectFileBase>(SectionRef::getObject());
135 }
136
139 }
140
143 }
144
147 }
148};
149
151public:
153 assert(isa<ELFObjectFileBase>(B->getObject()));
154 }
155
156 const ELFSectionRef *operator->() const {
157 return static_cast<const ELFSectionRef *>(section_iterator::operator->());
158 }
159
160 const ELFSectionRef &operator*() const {
161 return static_cast<const ELFSectionRef &>(section_iterator::operator*());
162 }
163};
164
165class ELFSymbolRef : public SymbolRef {
166public:
168 assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
169 }
170
172 return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
173 }
174
177 }
178
179 uint8_t getBinding() const {
181 }
182
183 uint8_t getOther() const {
185 }
186
187 uint8_t getELFType() const {
189 }
190
192 uint8_t Type = getELFType();
193 for (const auto &EE : ElfSymbolTypes) {
194 if (EE.Value == Type) {
195 return EE.AltName;
196 }
197 }
198 return "";
199 }
200};
201
202inline bool operator<(const ELFSymbolRef &A, const ELFSymbolRef &B) {
203 const DataRefImpl &DRIA = A.getRawDataRefImpl();
204 const DataRefImpl &DRIB = B.getRawDataRefImpl();
205 if (DRIA.d.a == DRIB.d.a)
206 return DRIA.d.b < DRIB.d.b;
207 return DRIA.d.a < DRIB.d.a;
208}
209
211public:
213 : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
215
216 const ELFSymbolRef *operator->() const {
217 return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
218 }
219
220 const ELFSymbolRef &operator*() const {
221 return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
222 }
223};
224
226public:
228 assert(isa<ELFObjectFileBase>(RelocationRef::getObject()));
229 }
230
232 return cast<ELFObjectFileBase>(RelocationRef::getObject());
233 }
234
237 }
238};
239
241public:
244 B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {}
245
247 return static_cast<const ELFRelocationRef *>(
249 }
250
252 return static_cast<const ELFRelocationRef &>(
254 }
255};
256
260}
261
262template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
263 uint16_t getEMachine() const override;
264 uint16_t getEType() const override;
265 uint8_t getEIdentABIVersion() const override;
266 uint64_t getSymbolSize(DataRefImpl Sym) const override;
267
268public:
270
271 SectionRef toSectionRef(const Elf_Shdr *Sec) const {
272 return SectionRef(toDRI(Sec), this);
273 }
274
275 ELFSymbolRef toSymbolRef(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
276 return ELFSymbolRef({toDRI(SymTable, SymbolNum), this});
277 }
278
279 bool IsContentValid() const { return ContentValid; }
280
281private:
283 const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,
284 const Elf_Shdr *DotSymtabShndxSec);
285
286 bool ContentValid = false;
287
288protected:
290
291 const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
292 const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
293 const Elf_Shdr *DotSymtabShndxSec = nullptr; // SHT_SYMTAB_SHNDX section.
294
295 Error initContent() override;
296
297 void moveSymbolNext(DataRefImpl &Symb) const override;
298 Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
300 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
301 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
302 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
303 Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override;
304 uint8_t getSymbolBinding(DataRefImpl Symb) const override;
305 uint8_t getSymbolOther(DataRefImpl Symb) const override;
306 uint8_t getSymbolELFType(DataRefImpl Symb) const override;
309 const Elf_Shdr *SymTab) const;
311
312 void moveSectionNext(DataRefImpl &Sec) const override;
314 uint64_t getSectionAddress(DataRefImpl Sec) const override;
315 uint64_t getSectionIndex(DataRefImpl Sec) const override;
316 uint64_t getSectionSize(DataRefImpl Sec) const override;
318 getSectionContents(DataRefImpl Sec) const override;
319 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
320 bool isSectionCompressed(DataRefImpl Sec) const override;
321 bool isSectionText(DataRefImpl Sec) const override;
322 bool isSectionData(DataRefImpl Sec) const override;
323 bool isSectionBSS(DataRefImpl Sec) const override;
324 bool isSectionVirtual(DataRefImpl Sec) const override;
325 bool isBerkeleyText(DataRefImpl Sec) const override;
326 bool isBerkeleyData(DataRefImpl Sec) const override;
327 bool isDebugSection(DataRefImpl Sec) const override;
330 std::vector<SectionRef> dynamic_relocation_sections() const override;
332 getRelocatedSection(DataRefImpl Sec) const override;
333
334 void moveRelocationNext(DataRefImpl &Rel) const override;
335 uint64_t getRelocationOffset(DataRefImpl Rel) const override;
337 uint64_t getRelocationType(DataRefImpl Rel) const override;
339 SmallVectorImpl<char> &Result) const override;
340
341 uint32_t getSectionType(DataRefImpl Sec) const override;
342 uint64_t getSectionFlags(DataRefImpl Sec) const override;
343 uint64_t getSectionOffset(DataRefImpl Sec) const override;
345
346 DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
347 DataRefImpl DRI;
348 if (!SymTable) {
349 DRI.d.a = 0;
350 DRI.d.b = 0;
351 return DRI;
352 }
353 assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
354 SymTable->sh_type == ELF::SHT_DYNSYM);
355
356 auto SectionsOrErr = EF.sections();
357 if (!SectionsOrErr) {
358 DRI.d.a = 0;
359 DRI.d.b = 0;
360 return DRI;
361 }
362 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
363 unsigned SymTableIndex =
364 (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
365
366 DRI.d.a = SymTableIndex;
367 DRI.d.b = SymbolNum;
368 return DRI;
369 }
370
371 const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
372 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
373 }
374
375 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
376 DataRefImpl DRI;
377 DRI.p = reinterpret_cast<uintptr_t>(Sec);
378 return DRI;
379 }
380
381 DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
382 DataRefImpl DRI;
383 DRI.p = reinterpret_cast<uintptr_t>(Dyn);
384 return DRI;
385 }
386
387 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
388 unsigned char Binding = ESym->getBinding();
389 unsigned char Visibility = ESym->getVisibility();
390
391 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
392 // visibility is either DEFAULT or PROTECTED. All other symbols are not
393 // exported.
394 return (
395 (Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK ||
396 Binding == ELF::STB_GNU_UNIQUE) &&
397 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED));
398 }
399
400 Error getBuildAttributes(ELFAttributeParser &Attributes) const override {
402 switch (getEMachine()) {
403 case ELF::EM_ARM:
405 break;
406 case ELF::EM_RISCV:
408 break;
409 case ELF::EM_HEXAGON:
411 break;
412 default:
413 return Error::success();
414 }
415
416 auto SectionsOrErr = EF.sections();
417 if (!SectionsOrErr)
418 return SectionsOrErr.takeError();
419 for (const Elf_Shdr &Sec : *SectionsOrErr) {
420 if (Sec.sh_type != Type)
421 continue;
422 auto ErrorOrContents = EF.getSectionContents(Sec);
423 if (!ErrorOrContents)
424 return ErrorOrContents.takeError();
425
426 auto Contents = ErrorOrContents.get();
427 if (Contents[0] != ELFAttrs::Format_Version || Contents.size() == 1)
428 return Error::success();
429
430 if (Error E = Attributes.parse(Contents, ELFT::Endianness))
431 return E;
432 break;
433 }
434 return Error::success();
435 }
436
437 // This flag is used for classof, to distinguish ELFObjectFile from
438 // its subclass. If more subclasses will be created, this flag will
439 // have to become an enum.
440 bool isDyldELFObject = false;
441
442public:
445 bool InitContent = true);
446
447 const Elf_Rel *getRel(DataRefImpl Rel) const;
448 const Elf_Rela *getRela(DataRefImpl Rela) const;
449
451 return EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
452 }
453
454 /// Get the relocation section that contains \a Rel.
455 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
456 auto RelSecOrErr = EF.getSection(Rel.d.a);
457 if (!RelSecOrErr)
459 Twine(errorToErrorCode(RelSecOrErr.takeError()).message()));
460 return *RelSecOrErr;
461 }
462
463 const Elf_Shdr *getSection(DataRefImpl Sec) const {
464 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
465 }
466
467 basic_symbol_iterator symbol_begin() const override;
468 basic_symbol_iterator symbol_end() const override;
469
470 bool is64Bit() const override { return getBytesInAddress() == 8; }
471
474
475 section_iterator section_begin() const override;
476 section_iterator section_end() const override;
477
479
480 uint8_t getBytesInAddress() const override;
481 StringRef getFileFormatName() const override;
482 Triple::ArchType getArch() const override;
483 Triple::OSType getOS() const override;
484 Expected<uint64_t> getStartAddress() const override;
485
486 unsigned getPlatformFlags() const override { return EF.getHeader().e_flags; }
487
488 const ELFFile<ELFT> &getELFFile() const { return EF; }
489
490 bool isDyldType() const { return isDyldELFObject; }
491 static bool classof(const Binary *v) {
492 return v->getType() ==
493 getELFType(ELFT::Endianness == llvm::endianness::little,
494 ELFT::Is64Bits);
495 }
496
498
499 bool isRelocatableObject() const override;
500
501 void createFakeSections() { EF.createFakeSections(); }
502};
503
508
509template <class ELFT>
511 ++Sym.d.b;
512}
513
515 auto SectionsOrErr = EF.sections();
516 if (!SectionsOrErr)
517 return SectionsOrErr.takeError();
518
519 for (const Elf_Shdr &Sec : *SectionsOrErr) {
520 switch (Sec.sh_type) {
521 case ELF::SHT_DYNSYM: {
522 if (!DotDynSymSec)
523 DotDynSymSec = &Sec;
524 break;
525 }
526 case ELF::SHT_SYMTAB: {
527 if (!DotSymtabSec)
528 DotSymtabSec = &Sec;
529 break;
530 }
532 if (!DotSymtabShndxSec)
533 DotSymtabShndxSec = &Sec;
534 break;
535 }
536 }
537 }
538
539 ContentValid = true;
540 return Error::success();
541}
542
543template <class ELFT>
545 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
546 if (!SymOrErr)
547 return SymOrErr.takeError();
548 auto SymTabOrErr = EF.getSection(Sym.d.a);
549 if (!SymTabOrErr)
550 return SymTabOrErr.takeError();
551 const Elf_Shdr *SymTableSec = *SymTabOrErr;
552 auto StrTabOrErr = EF.getSection(SymTableSec->sh_link);
553 if (!StrTabOrErr)
554 return StrTabOrErr.takeError();
555 const Elf_Shdr *StringTableSec = *StrTabOrErr;
556 auto SymStrTabOrErr = EF.getStringTable(*StringTableSec);
557 if (!SymStrTabOrErr)
558 return SymStrTabOrErr.takeError();
559 Expected<StringRef> Name = (*SymOrErr)->getName(*SymStrTabOrErr);
560 if (Name && !Name->empty())
561 return Name;
562
563 // If the symbol name is empty use the section name.
564 if ((*SymOrErr)->getType() == ELF::STT_SECTION) {
565 Expected<section_iterator> SecOrErr = getSymbolSection(Sym);
566 if (SecOrErr)
567 return (*SecOrErr)->getName();
568 return SecOrErr.takeError();
569 }
570 return Name;
571}
572
573template <class ELFT>
575 return getSection(Sec)->sh_flags;
576}
577
578template <class ELFT>
580 return getSection(Sec)->sh_type;
581}
582
583template <class ELFT>
585 return getSection(Sec)->sh_offset;
586}
587
588template <class ELFT>
590 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
591 if (!SymOrErr)
592 report_fatal_error(SymOrErr.takeError());
593
594 uint64_t Ret = (*SymOrErr)->st_value;
595 if ((*SymOrErr)->st_shndx == ELF::SHN_ABS)
596 return Ret;
597
598 const Elf_Ehdr &Header = EF.getHeader();
599 // Clear the ARM/Thumb or microMIPS indicator flag.
600 if ((Header.e_machine == ELF::EM_ARM || Header.e_machine == ELF::EM_MIPS) &&
601 (*SymOrErr)->getType() == ELF::STT_FUNC)
602 Ret &= ~1;
603
604 return Ret;
605}
606
607template <class ELFT>
610 Expected<uint64_t> SymbolValueOrErr = getSymbolValue(Symb);
611 if (!SymbolValueOrErr)
612 // TODO: Test this error.
613 return SymbolValueOrErr.takeError();
614
615 uint64_t Result = *SymbolValueOrErr;
616 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
617 if (!SymOrErr)
618 return SymOrErr.takeError();
619
620 switch ((*SymOrErr)->st_shndx) {
621 case ELF::SHN_COMMON:
622 case ELF::SHN_UNDEF:
623 case ELF::SHN_ABS:
624 return Result;
625 }
626
627 auto SymTabOrErr = EF.getSection(Symb.d.a);
628 if (!SymTabOrErr)
629 return SymTabOrErr.takeError();
630
631 if (EF.getHeader().e_type == ELF::ET_REL) {
632 ArrayRef<Elf_Word> ShndxTable;
633 if (DotSymtabShndxSec) {
634 // TODO: Test this error.
635 if (Expected<ArrayRef<Elf_Word>> ShndxTableOrErr =
636 EF.getSHNDXTable(*DotSymtabShndxSec))
637 ShndxTable = *ShndxTableOrErr;
638 else
639 return ShndxTableOrErr.takeError();
640 }
641
642 Expected<const Elf_Shdr *> SectionOrErr =
643 EF.getSection(**SymOrErr, *SymTabOrErr, ShndxTable);
644 if (!SectionOrErr)
645 return SectionOrErr.takeError();
646 const Elf_Shdr *Section = *SectionOrErr;
647 if (Section)
648 Result += Section->sh_addr;
649 }
650
651 return Result;
652}
653
654template <class ELFT>
656 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
657 if (!SymOrErr)
658 report_fatal_error(SymOrErr.takeError());
659 if ((*SymOrErr)->st_shndx == ELF::SHN_COMMON)
660 return (*SymOrErr)->st_value;
661 return 0;
662}
663
664template <class ELFT>
666 return EF.getHeader().e_machine;
667}
668
669template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const {
670 return EF.getHeader().e_type;
671}
672
673template <class ELFT> uint8_t ELFObjectFile<ELFT>::getEIdentABIVersion() const {
674 return EF.getHeader().e_ident[ELF::EI_ABIVERSION];
675}
676
677template <class ELFT>
678uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
679 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
680 if (!SymOrErr)
681 report_fatal_error(SymOrErr.takeError());
682 return (*SymOrErr)->st_size;
683}
684
685template <class ELFT>
687 return getSymbolSize(Symb);
688}
689
690template <class ELFT>
692 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
693 if (!SymOrErr)
694 report_fatal_error(SymOrErr.takeError());
695 return (*SymOrErr)->getBinding();
696}
697
698template <class ELFT>
700 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
701 if (!SymOrErr)
702 report_fatal_error(SymOrErr.takeError());
703 return (*SymOrErr)->st_other;
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)->getType();
712}
713
714template <class ELFT>
717 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
718 if (!SymOrErr)
719 return SymOrErr.takeError();
720
721 switch ((*SymOrErr)->getType()) {
722 case ELF::STT_NOTYPE:
724 case ELF::STT_SECTION:
725 return SymbolRef::ST_Debug;
726 case ELF::STT_FILE:
727 return SymbolRef::ST_File;
728 case ELF::STT_FUNC:
730 case ELF::STT_OBJECT:
731 case ELF::STT_COMMON:
732 return SymbolRef::ST_Data;
733 case ELF::STT_TLS:
734 default:
735 return SymbolRef::ST_Other;
736 }
737}
738
739template <class ELFT>
741 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
742 if (!SymOrErr)
743 return SymOrErr.takeError();
744
745 const Elf_Sym *ESym = *SymOrErr;
747
748 if (ESym->getBinding() != ELF::STB_LOCAL)
749 Result |= SymbolRef::SF_Global;
750
751 if (ESym->getBinding() == ELF::STB_WEAK)
752 Result |= SymbolRef::SF_Weak;
753
754 if (ESym->st_shndx == ELF::SHN_ABS)
755 Result |= SymbolRef::SF_Absolute;
756
757 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION)
759
760 if (Expected<typename ELFT::SymRange> SymbolsOrErr =
761 EF.symbols(DotSymtabSec)) {
762 // Set the SF_FormatSpecific flag for the 0-index null symbol.
763 if (ESym == SymbolsOrErr->begin())
765 } else
766 // TODO: Test this error.
767 return SymbolsOrErr.takeError();
768
769 if (Expected<typename ELFT::SymRange> SymbolsOrErr =
770 EF.symbols(DotDynSymSec)) {
771 // Set the SF_FormatSpecific flag for the 0-index null symbol.
772 if (ESym == SymbolsOrErr->begin())
774 } else
775 // TODO: Test this error.
776 return SymbolsOrErr.takeError();
777
778 if (EF.getHeader().e_machine == ELF::EM_AARCH64) {
779 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
780 StringRef Name = *NameOrErr;
781 if (Name.starts_with("$d") || Name.starts_with("$x"))
783 } else {
784 // TODO: Actually report errors helpfully.
785 consumeError(NameOrErr.takeError());
786 }
787 } else if (EF.getHeader().e_machine == ELF::EM_ARM) {
788 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
789 StringRef Name = *NameOrErr;
790 // TODO Investigate why empty name symbols need to be marked.
791 if (Name.empty() || Name.starts_with("$d") || Name.starts_with("$t") ||
792 Name.starts_with("$a"))
794 } else {
795 // TODO: Actually report errors helpfully.
796 consumeError(NameOrErr.takeError());
797 }
798 if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1)
799 Result |= SymbolRef::SF_Thumb;
800 } else if (EF.getHeader().e_machine == ELF::EM_CSKY) {
801 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
802 StringRef Name = *NameOrErr;
803 if (Name.starts_with("$d") || Name.starts_with("$t"))
805 } else {
806 // TODO: Actually report errors helpfully.
807 consumeError(NameOrErr.takeError());
808 }
809 } else if (EF.getHeader().e_machine == ELF::EM_RISCV) {
810 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
811 StringRef Name = *NameOrErr;
812 // Mark fake labels (used for label differences) and mapping symbols.
813 if (Name == ".L0 " || Name.starts_with("$d") || Name.starts_with("$x"))
815 } else {
816 // TODO: Actually report errors helpfully.
817 consumeError(NameOrErr.takeError());
818 }
819 }
820
821 if (ESym->st_shndx == ELF::SHN_UNDEF)
822 Result |= SymbolRef::SF_Undefined;
823
824 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
825 Result |= SymbolRef::SF_Common;
826
827 if (isExportedToOtherDSO(ESym))
828 Result |= SymbolRef::SF_Exported;
829
830 if (ESym->getType() == ELF::STT_GNU_IFUNC)
831 Result |= SymbolRef::SF_Indirect;
832
833 if (ESym->getVisibility() == ELF::STV_HIDDEN)
834 Result |= SymbolRef::SF_Hidden;
835
836 return Result;
837}
838
839template <class ELFT>
842 const Elf_Shdr *SymTab) const {
843 ArrayRef<Elf_Word> ShndxTable;
844 if (DotSymtabShndxSec) {
845 // TODO: Test this error.
846 Expected<ArrayRef<Elf_Word>> ShndxTableOrErr =
847 EF.getSHNDXTable(*DotSymtabShndxSec);
848 if (!ShndxTableOrErr)
849 return ShndxTableOrErr.takeError();
850 ShndxTable = *ShndxTableOrErr;
851 }
852
853 auto ESecOrErr = EF.getSection(*ESym, SymTab, ShndxTable);
854 if (!ESecOrErr)
855 return ESecOrErr.takeError();
856
857 const Elf_Shdr *ESec = *ESecOrErr;
858 if (!ESec)
859 return section_end();
860
861 DataRefImpl Sec;
862 Sec.p = reinterpret_cast<intptr_t>(ESec);
863 return section_iterator(SectionRef(Sec, this));
864}
865
866template <class ELFT>
869 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
870 if (!SymOrErr)
871 return SymOrErr.takeError();
872
873 auto SymTabOrErr = EF.getSection(Symb.d.a);
874 if (!SymTabOrErr)
875 return SymTabOrErr.takeError();
876 return getSymbolSection(*SymOrErr, *SymTabOrErr);
877}
878
879template <class ELFT>
881 const Elf_Shdr *ESec = getSection(Sec);
882 Sec = toDRI(++ESec);
883}
884
885template <class ELFT>
887 return EF.getSectionName(*getSection(Sec));
888}
889
890template <class ELFT>
892 return getSection(Sec)->sh_addr;
893}
894
895template <class ELFT>
897 auto SectionsOrErr = EF.sections();
898 handleAllErrors(std::move(SectionsOrErr.takeError()),
899 [](const ErrorInfoBase &) {
900 llvm_unreachable("unable to get section index");
901 });
902 const Elf_Shdr *First = SectionsOrErr->begin();
903 return getSection(Sec) - First;
904}
905
906template <class ELFT>
908 return getSection(Sec)->sh_size;
909}
910
911template <class ELFT>
914 const Elf_Shdr *EShdr = getSection(Sec);
915 if (EShdr->sh_type == ELF::SHT_NOBITS)
916 return ArrayRef((const uint8_t *)base(), (size_t)0);
917 if (Error E =
918 checkOffset(getMemoryBufferRef(),
919 (uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
920 return std::move(E);
921 return ArrayRef((const uint8_t *)base() + EShdr->sh_offset, EShdr->sh_size);
922}
923
924template <class ELFT>
926 return getSection(Sec)->sh_addralign;
927}
928
929template <class ELFT>
931 return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED;
932}
933
934template <class ELFT>
936 return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR;
937}
938
939template <class ELFT>
941 const Elf_Shdr *EShdr = getSection(Sec);
942 return EShdr->sh_type == ELF::SHT_PROGBITS &&
943 EShdr->sh_flags & ELF::SHF_ALLOC &&
944 !(EShdr->sh_flags & ELF::SHF_EXECINSTR);
945}
946
947template <class ELFT>
949 const Elf_Shdr *EShdr = getSection(Sec);
950 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
951 EShdr->sh_type == ELF::SHT_NOBITS;
952}
953
954template <class ELFT>
955std::vector<SectionRef>
957 std::vector<SectionRef> Res;
958 std::vector<uintptr_t> Offsets;
959
960 auto SectionsOrErr = EF.sections();
961 if (!SectionsOrErr)
962 return Res;
963
964 for (const Elf_Shdr &Sec : *SectionsOrErr) {
965 if (Sec.sh_type != ELF::SHT_DYNAMIC)
966 continue;
967 Elf_Dyn *Dynamic =
968 reinterpret_cast<Elf_Dyn *>((uintptr_t)base() + Sec.sh_offset);
969 for (; Dynamic->d_tag != ELF::DT_NULL; Dynamic++) {
970 if (Dynamic->d_tag == ELF::DT_REL || Dynamic->d_tag == ELF::DT_RELA ||
971 Dynamic->d_tag == ELF::DT_JMPREL) {
972 Offsets.push_back(Dynamic->d_un.d_val);
973 }
974 }
975 }
976 for (const Elf_Shdr &Sec : *SectionsOrErr) {
977 if (is_contained(Offsets, Sec.sh_addr))
978 Res.emplace_back(toDRI(&Sec), this);
979 }
980 return Res;
981}
982
983template <class ELFT>
985 return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
986}
987
988template <class ELFT>
990 return getSection(Sec)->sh_flags & ELF::SHF_ALLOC &&
991 (getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR ||
992 !(getSection(Sec)->sh_flags & ELF::SHF_WRITE));
993}
994
995template <class ELFT>
997 const Elf_Shdr *EShdr = getSection(Sec);
998 return !isBerkeleyText(Sec) && EShdr->sh_type != ELF::SHT_NOBITS &&
999 EShdr->sh_flags & ELF::SHF_ALLOC;
1000}
1001
1002template <class ELFT>
1004 Expected<StringRef> SectionNameOrErr = getSectionName(Sec);
1005 if (!SectionNameOrErr) {
1006 // TODO: Report the error message properly.
1007 consumeError(SectionNameOrErr.takeError());
1008 return false;
1009 }
1010 StringRef SectionName = SectionNameOrErr.get();
1011 return SectionName.starts_with(".debug") ||
1012 SectionName.starts_with(".zdebug") || SectionName == ".gdb_index";
1013}
1014
1015template <class ELFT>
1018 DataRefImpl RelData;
1019 auto SectionsOrErr = EF.sections();
1020 if (!SectionsOrErr)
1022 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
1023 RelData.d.a = (Sec.p - SHT) / EF.getHeader().e_shentsize;
1024 RelData.d.b = 0;
1025 return relocation_iterator(RelocationRef(RelData, this));
1026}
1027
1028template <class ELFT>
1031 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1032 relocation_iterator Begin = section_rel_begin(Sec);
1033 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
1034 return Begin;
1035 DataRefImpl RelData = Begin->getRawDataRefImpl();
1036 const Elf_Shdr *RelSec = getRelSection(RelData);
1037
1038 // Error check sh_link here so that getRelocationSymbol can just use it.
1039 auto SymSecOrErr = EF.getSection(RelSec->sh_link);
1040 if (!SymSecOrErr)
1042 Twine(errorToErrorCode(SymSecOrErr.takeError()).message()));
1043
1044 RelData.d.b += S->sh_size / S->sh_entsize;
1045 return relocation_iterator(RelocationRef(RelData, this));
1046}
1047
1048template <class ELFT>
1051 const Elf_Shdr *EShdr = getSection(Sec);
1052 uintX_t Type = EShdr->sh_type;
1053 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
1054 return section_end();
1055
1056 Expected<const Elf_Shdr *> SecOrErr = EF.getSection(EShdr->sh_info);
1057 if (!SecOrErr)
1058 return SecOrErr.takeError();
1059 return section_iterator(SectionRef(toDRI(*SecOrErr), this));
1060}
1061
1062// Relocations
1063template <class ELFT>
1065 ++Rel.d.b;
1066}
1067
1068template <class ELFT>
1071 uint32_t symbolIdx;
1072 const Elf_Shdr *sec = getRelSection(Rel);
1073 if (sec->sh_type == ELF::SHT_REL)
1074 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
1075 else
1076 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
1077 if (!symbolIdx)
1078 return symbol_end();
1079
1080 // FIXME: error check symbolIdx
1081 DataRefImpl SymbolData;
1082 SymbolData.d.a = sec->sh_link;
1083 SymbolData.d.b = symbolIdx;
1084 return symbol_iterator(SymbolRef(SymbolData, this));
1085}
1086
1087template <class ELFT>
1089 const Elf_Shdr *sec = getRelSection(Rel);
1090 if (sec->sh_type == ELF::SHT_REL)
1091 return getRel(Rel)->r_offset;
1092
1093 return getRela(Rel)->r_offset;
1094}
1095
1096template <class ELFT>
1098 const Elf_Shdr *sec = getRelSection(Rel);
1099 if (sec->sh_type == ELF::SHT_REL)
1100 return getRel(Rel)->getType(EF.isMips64EL());
1101 else
1102 return getRela(Rel)->getType(EF.isMips64EL());
1103}
1104
1105template <class ELFT>
1107 return getELFRelocationTypeName(EF.getHeader().e_machine, Type);
1108}
1109
1110template <class ELFT>
1112 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1113 uint32_t type = getRelocationType(Rel);
1114 EF.getRelocationTypeName(type, Result);
1115}
1116
1117template <class ELFT>
1120 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
1121 return createError("Section is not SHT_RELA");
1122 return (int64_t)getRela(Rel)->r_addend;
1123}
1124
1125template <class ELFT>
1126const typename ELFObjectFile<ELFT>::Elf_Rel *
1128 assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
1129 auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
1130 if (!Ret)
1131 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message()));
1132 return *Ret;
1133}
1134
1135template <class ELFT>
1136const typename ELFObjectFile<ELFT>::Elf_Rela *
1138 assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
1139 auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
1140 if (!Ret)
1141 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message()));
1142 return *Ret;
1143}
1144
1145template <class ELFT>
1148 auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer());
1149 if (Error E = EFOrErr.takeError())
1150 return std::move(E);
1151
1152 ELFObjectFile<ELFT> Obj = {Object, std::move(*EFOrErr), nullptr, nullptr,
1153 nullptr};
1154 if (InitContent)
1155 if (Error E = Obj.initContent())
1156 return std::move(E);
1157 return std::move(Obj);
1158}
1159
1160template <class ELFT>
1162 const Elf_Shdr *DotDynSymSec,
1163 const Elf_Shdr *DotSymtabSec,
1164 const Elf_Shdr *DotSymtabShndx)
1165 : ELFObjectFileBase(getELFType(ELFT::Endianness == llvm::endianness::little,
1166 ELFT::Is64Bits),
1167 Object),
1168 EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
1169 DotSymtabShndxSec(DotSymtabShndx) {}
1170
1171template <class ELFT>
1173 : ELFObjectFile(Other.Data, Other.EF, Other.DotDynSymSec,
1174 Other.DotSymtabSec, Other.DotSymtabShndxSec) {}
1175
1176template <class ELFT>
1179 toDRI(DotSymtabSec,
1180 DotSymtabSec && DotSymtabSec->sh_size >= sizeof(Elf_Sym) ? 1 : 0);
1181 return basic_symbol_iterator(SymbolRef(Sym, this));
1182}
1183
1184template <class ELFT>
1186 const Elf_Shdr *SymTab = DotSymtabSec;
1187 if (!SymTab)
1188 return symbol_begin();
1189 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1190 return basic_symbol_iterator(SymbolRef(Sym, this));
1191}
1192
1193template <class ELFT>
1195 if (!DotDynSymSec || DotDynSymSec->sh_size < sizeof(Elf_Sym))
1196 // Ignore errors here where the dynsym is empty or sh_size less than the
1197 // size of one symbol. These should be handled elsewhere.
1198 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 0), this));
1199 // Skip 0-index NULL symbol.
1200 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 1), this));
1201}
1202
1203template <class ELFT>
1205 const Elf_Shdr *SymTab = DotDynSymSec;
1206 if (!SymTab)
1207 return dynamic_symbol_begin();
1208 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1209 return basic_symbol_iterator(SymbolRef(Sym, this));
1210}
1211
1212template <class ELFT>
1214 auto SectionsOrErr = EF.sections();
1215 if (!SectionsOrErr)
1216 return section_iterator(SectionRef());
1217 return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this));
1218}
1219
1220template <class ELFT>
1222 auto SectionsOrErr = EF.sections();
1223 if (!SectionsOrErr)
1224 return section_iterator(SectionRef());
1225 return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
1226}
1227
1228template <class ELFT>
1230 return ELFT::Is64Bits ? 8 : 4;
1231}
1232
1233template <class ELFT>
1235 constexpr bool IsLittleEndian = ELFT::Endianness == llvm::endianness::little;
1236 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1237 case ELF::ELFCLASS32:
1238 switch (EF.getHeader().e_machine) {
1239 case ELF::EM_68K:
1240 return "elf32-m68k";
1241 case ELF::EM_386:
1242 return "elf32-i386";
1243 case ELF::EM_IAMCU:
1244 return "elf32-iamcu";
1245 case ELF::EM_X86_64:
1246 return "elf32-x86-64";
1247 case ELF::EM_ARM:
1248 return (IsLittleEndian ? "elf32-littlearm" : "elf32-bigarm");
1249 case ELF::EM_AVR:
1250 return "elf32-avr";
1251 case ELF::EM_HEXAGON:
1252 return "elf32-hexagon";
1253 case ELF::EM_LANAI:
1254 return "elf32-lanai";
1255 case ELF::EM_MIPS:
1256 return "elf32-mips";
1257 case ELF::EM_MSP430:
1258 return "elf32-msp430";
1259 case ELF::EM_PPC:
1260 return (IsLittleEndian ? "elf32-powerpcle" : "elf32-powerpc");
1261 case ELF::EM_RISCV:
1262 return "elf32-littleriscv";
1263 case ELF::EM_CSKY:
1264 return "elf32-csky";
1265 case ELF::EM_SPARC:
1267 return "elf32-sparc";
1268 case ELF::EM_AMDGPU:
1269 return "elf32-amdgpu";
1270 case ELF::EM_LOONGARCH:
1271 return "elf32-loongarch";
1272 case ELF::EM_XTENSA:
1273 return "elf32-xtensa";
1274 default:
1275 return "elf32-unknown";
1276 }
1277 case ELF::ELFCLASS64:
1278 switch (EF.getHeader().e_machine) {
1279 case ELF::EM_386:
1280 return "elf64-i386";
1281 case ELF::EM_X86_64:
1282 return "elf64-x86-64";
1283 case ELF::EM_AARCH64:
1284 return (IsLittleEndian ? "elf64-littleaarch64" : "elf64-bigaarch64");
1285 case ELF::EM_PPC64:
1286 return (IsLittleEndian ? "elf64-powerpcle" : "elf64-powerpc");
1287 case ELF::EM_RISCV:
1288 return "elf64-littleriscv";
1289 case ELF::EM_S390:
1290 return "elf64-s390";
1291 case ELF::EM_SPARCV9:
1292 return "elf64-sparc";
1293 case ELF::EM_MIPS:
1294 return "elf64-mips";
1295 case ELF::EM_AMDGPU:
1296 return "elf64-amdgpu";
1297 case ELF::EM_BPF:
1298 return "elf64-bpf";
1299 case ELF::EM_VE:
1300 return "elf64-ve";
1301 case ELF::EM_LOONGARCH:
1302 return "elf64-loongarch";
1303 default:
1304 return "elf64-unknown";
1305 }
1306 default:
1307 // FIXME: Proper error handling.
1308 report_fatal_error("Invalid ELFCLASS!");
1309 }
1310}
1311
1312template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
1313 bool IsLittleEndian = ELFT::Endianness == llvm::endianness::little;
1314 switch (EF.getHeader().e_machine) {
1315 case ELF::EM_68K:
1316 return Triple::m68k;
1317 case ELF::EM_386:
1318 case ELF::EM_IAMCU:
1319 return Triple::x86;
1320 case ELF::EM_X86_64:
1321 return Triple::x86_64;
1322 case ELF::EM_AARCH64:
1323 return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be;
1324 case ELF::EM_ARM:
1325 return Triple::arm;
1326 case ELF::EM_AVR:
1327 return Triple::avr;
1328 case ELF::EM_HEXAGON:
1329 return Triple::hexagon;
1330 case ELF::EM_LANAI:
1331 return Triple::lanai;
1332 case ELF::EM_MIPS:
1333 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1334 case ELF::ELFCLASS32:
1335 return IsLittleEndian ? Triple::mipsel : Triple::mips;
1336 case ELF::ELFCLASS64:
1337 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
1338 default:
1339 report_fatal_error("Invalid ELFCLASS!");
1340 }
1341 case ELF::EM_MSP430:
1342 return Triple::msp430;
1343 case ELF::EM_PPC:
1344 return IsLittleEndian ? Triple::ppcle : Triple::ppc;
1345 case ELF::EM_PPC64:
1346 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
1347 case ELF::EM_RISCV:
1348 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1349 case ELF::ELFCLASS32:
1350 return Triple::riscv32;
1351 case ELF::ELFCLASS64:
1352 return Triple::riscv64;
1353 default:
1354 report_fatal_error("Invalid ELFCLASS!");
1355 }
1356 case ELF::EM_S390:
1357 return Triple::systemz;
1358
1359 case ELF::EM_SPARC:
1361 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
1362 case ELF::EM_SPARCV9:
1363 return Triple::sparcv9;
1364
1365 case ELF::EM_AMDGPU: {
1366 if (!IsLittleEndian)
1367 return Triple::UnknownArch;
1368
1369 unsigned MACH = EF.getHeader().e_flags & ELF::EF_AMDGPU_MACH;
1370 if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST &&
1372 return Triple::r600;
1375 return Triple::amdgcn;
1376
1377 return Triple::UnknownArch;
1378 }
1379
1380 case ELF::EM_CUDA: {
1381 if (EF.getHeader().e_ident[ELF::EI_CLASS] == ELF::ELFCLASS32)
1382 return Triple::nvptx;
1383 return Triple::nvptx64;
1384 }
1385
1386 case ELF::EM_BPF:
1387 return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
1388
1389 case ELF::EM_VE:
1390 return Triple::ve;
1391 case ELF::EM_CSKY:
1392 return Triple::csky;
1393
1394 case ELF::EM_LOONGARCH:
1395 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1396 case ELF::ELFCLASS32:
1397 return Triple::loongarch32;
1398 case ELF::ELFCLASS64:
1399 return Triple::loongarch64;
1400 default:
1401 report_fatal_error("Invalid ELFCLASS!");
1402 }
1403
1404 case ELF::EM_XTENSA:
1405 return Triple::xtensa;
1406
1407 default:
1408 return Triple::UnknownArch;
1409 }
1410}
1411
1412template <class ELFT> Triple::OSType ELFObjectFile<ELFT>::getOS() const {
1413 switch (EF.getHeader().e_ident[ELF::EI_OSABI]) {
1415 return Triple::NetBSD;
1417 return Triple::Linux;
1418 case ELF::ELFOSABI_HURD:
1419 return Triple::Hurd;
1421 return Triple::Solaris;
1422 case ELF::ELFOSABI_AIX:
1423 return Triple::AIX;
1425 return Triple::FreeBSD;
1427 return Triple::OpenBSD;
1428 case ELF::ELFOSABI_CUDA:
1429 return Triple::CUDA;
1431 return Triple::AMDHSA;
1433 return Triple::AMDPAL;
1435 return Triple::Mesa3D;
1436 default:
1437 return Triple::UnknownOS;
1438 }
1439}
1440
1441template <class ELFT>
1443 return EF.getHeader().e_entry;
1444}
1445
1446template <class ELFT>
1449 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
1450}
1451
1452template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
1453 return EF.getHeader().e_type == ELF::ET_REL;
1454}
1455
1456} // end namespace object
1457} // end namespace llvm
1458
1459#endif // LLVM_OBJECT_ELFOBJECTFILE_H
aarch64 promote const
AMDGPU Kernel Attributes
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static StringRef getSymbolName(SymbolKind SymKind)
std::string Name
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Definition: ELFTypes.h:104
Symbol * Sym
Definition: ELF_riscv.cpp:479
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
static uint64_t getSymbolValue(const MCSymbol &Symbol, const MCAsmLayout &Layout)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Base class for error info classes.
Definition: Error.h:45
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
reference get()
Returns a reference to the stored T value.
Definition: Error.h:571
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Manages the enabling and disabling of subtarget specific features.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
@ loongarch32
Definition: Triple.h:61
@ UnknownArch
Definition: Triple.h:47
@ aarch64_be
Definition: Triple.h:52
@ loongarch64
Definition: Triple.h:62
@ mips64el
Definition: Triple.h:67
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
A range adaptor for a pair of iterators.
const SymbolicFile * getObject() const
Definition: SymbolicFile.h:214
DataRefImpl getRawDataRefImpl() const
Definition: SymbolicFile.h:210
static unsigned int getELFType(bool isLE, bool is64Bits)
Definition: Binary.h:78
static Expected< ELFFile > create(StringRef Object)
Definition: ELF.h:839
virtual uint64_t getSymbolSize(DataRefImpl Symb) const =0
virtual uint8_t getEIdentABIVersion() const =0
virtual Error getBuildAttributes(ELFAttributeParser &Attributes) const =0
virtual uint64_t getSectionFlags(DataRefImpl Sec) const =0
virtual uint16_t getEType() const =0
virtual uint8_t getSymbolELFType(DataRefImpl Symb) 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
elf_symbol_iterator_range symbols() const
virtual Expected< int64_t > getRelocationAddend(DataRefImpl Rel) const =0
std::vector< ELFPltEntry > getPltEntries() const
Expected< SubtargetFeatures > getFeatures() const override
virtual uint8_t getSymbolBinding(DataRefImpl Symb) const =0
std::optional< StringRef > tryGetCPUName() const override
iterator_range< elf_symbol_iterator > elf_symbol_iterator_range
Definition: ELFObjectFile.h:86
virtual uint16_t getEMachine() const =0
static bool classof(const Binary *v)
Definition: ELFObjectFile.h:95
virtual unsigned getPlatformFlags() const =0
Returns platform-specific object flags, if any.
void setARMSubArch(Triple &TheTriple) const override
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
const Elf_Rel * getRel(DataRefImpl Rel) const
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
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
Error initContent() 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
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
Expected< int64_t > getAddend() const
const ELFObjectFileBase * getObject() const
ELFRelocationRef(const RelocationRef &B)
const ELFObjectFileBase * getObject() const
uint64_t getOffset() const
ELFSectionRef(const SectionRef &B)
const ELFObjectFileBase * getObject() const
uint8_t getELFType() const
ELFSymbolRef(const SymbolRef &B)
uint8_t getBinding() const
uint64_t getSize() const
StringRef getELFTypeName() const
This class is the base class for all object file types.
Definition: ObjectFile.h:229
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:52
const ObjectFile * getObject() const
Definition: ObjectFile.h:639
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:635
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:597
const ObjectFile * getObject() const
Definition: ObjectFile.h:601
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: ObjectFile.h:168
const ObjectFile * getObject() const
Definition: ObjectFile.h:487
virtual basic_symbol_iterator symbol_begin() const =0
virtual basic_symbol_iterator symbol_end() const =0
const SectionRef & operator*() const
Definition: SymbolicFile.h:83
const SectionRef * operator->() const
Definition: SymbolicFile.h:81
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:215
const SymbolRef & operator*() const
Definition: ObjectFile.h:220
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
@ SHF_ALLOC
Definition: ELF.h:1157
@ SHF_COMPRESSED
Definition: ELF.h:1185
@ SHF_WRITE
Definition: ELF.h:1154
@ SHF_EXECINSTR
Definition: ELF.h:1160
@ EM_MSP430
Definition: ELF.h:222
@ EM_S390
Definition: ELF.h:150
@ EM_PPC64
Definition: ELF.h:149
@ EM_SPARC
Definition: ELF.h:135
@ EM_CSKY
Definition: ELF.h:321
@ EM_SPARC32PLUS
Definition: ELF.h:146
@ EM_68K
Definition: ELF.h:137
@ EM_386
Definition: ELF.h:136
@ EM_CUDA
Definition: ELF.h:286
@ EM_LOONGARCH
Definition: ELF.h:322
@ EM_BPF
Definition: ELF.h:319
@ EM_PPC
Definition: ELF.h:148
@ EM_X86_64
Definition: ELF.h:178
@ EM_HEXAGON
Definition: ELF.h:257
@ EM_LANAI
Definition: ELF.h:318
@ EM_MIPS
Definition: ELF.h:141
@ EM_SPARCV9
Definition: ELF.h:159
@ EM_AARCH64
Definition: ELF.h:280
@ EM_XTENSA
Definition: ELF.h:211
@ EM_RISCV
Definition: ELF.h:317
@ EM_ARM
Definition: ELF.h:156
@ EM_VE
Definition: ELF.h:320
@ EM_IAMCU
Definition: ELF.h:139
@ EM_AMDGPU
Definition: ELF.h:316
@ EM_AVR
Definition: ELF.h:199
@ STV_HIDDEN
Definition: ELF.h:1342
@ STV_PROTECTED
Definition: ELF.h:1343
@ STV_DEFAULT
Definition: ELF.h:1340
@ ELFCLASS64
Definition: ELF.h:329
@ ELFCLASS32
Definition: ELF.h:328
@ EF_AMDGPU_MACH_AMDGCN_LAST
Definition: ELF.h:803
@ EF_AMDGPU_MACH_R600_LAST
Definition: ELF.h:741
@ EF_AMDGPU_MACH_AMDGCN_FIRST
Definition: ELF.h:802
@ EF_AMDGPU_MACH
Definition: ELF.h:707
@ EF_AMDGPU_MACH_R600_FIRST
Definition: ELF.h:740
@ ELFOSABI_HURD
Definition: ELF.h:346
@ ELFOSABI_CUDA
Definition: ELF.h:359
@ ELFOSABI_OPENBSD
Definition: ELF.h:353
@ ELFOSABI_NETBSD
Definition: ELF.h:343
@ ELFOSABI_AMDGPU_HSA
Definition: ELF.h:361
@ ELFOSABI_SOLARIS
Definition: ELF.h:347
@ ELFOSABI_FREEBSD
Definition: ELF.h:350
@ ELFOSABI_LINUX
Definition: ELF.h:345
@ ELFOSABI_AIX
Definition: ELF.h:348
@ ELFOSABI_AMDGPU_MESA3D
Definition: ELF.h:363
@ ELFOSABI_AMDGPU_PAL
Definition: ELF.h:362
@ SHN_ABS
Definition: ELF.h:1054
@ SHN_COMMON
Definition: ELF.h:1055
@ SHN_UNDEF
Definition: ELF.h:1048
@ SHT_PROGBITS
Definition: ELF.h:1063
@ SHT_REL
Definition: ELF.h:1071
@ SHT_ARM_ATTRIBUTES
Definition: ELF.h:1119
@ SHT_NOBITS
Definition: ELF.h:1070
@ SHT_SYMTAB
Definition: ELF.h:1064
@ SHT_HEXAGON_ATTRIBUTES
Definition: ELF.h:1144
@ SHT_DYNAMIC
Definition: ELF.h:1068
@ SHT_SYMTAB_SHNDX
Definition: ELF.h:1078
@ SHT_RISCV_ATTRIBUTES
Definition: ELF.h:1140
@ SHT_RELA
Definition: ELF.h:1066
@ SHT_DYNSYM
Definition: ELF.h:1073
@ EI_ABIVERSION
Definition: ELF.h:56
@ EI_CLASS
Definition: ELF.h:52
@ EI_OSABI
Definition: ELF.h:55
@ STB_GLOBAL
Definition: ELF.h:1311
@ STB_LOCAL
Definition: ELF.h:1310
@ STB_GNU_UNIQUE
Definition: ELF.h:1313
@ STB_WEAK
Definition: ELF.h:1312
@ ET_REL
Definition: ELF.h:116
@ STT_FUNC
Definition: ELF.h:1324
@ STT_NOTYPE
Definition: ELF.h:1322
@ STT_SECTION
Definition: ELF.h:1325
@ STT_FILE
Definition: ELF.h:1326
@ STT_COMMON
Definition: ELF.h:1327
@ STT_GNU_IFUNC
Definition: ELF.h:1329
@ STT_OBJECT
Definition: ELF.h:1323
@ STT_TLS
Definition: ELF.h:1328
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:485
bool operator<(const ELFSymbolRef &A, const ELFSymbolRef &B)
Error createError(const Twine &Err)
Definition: Error.h:84
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
Definition: ELF.cpp:23
constexpr int NumElfSymbolTypes
Definition: ELFObjectFile.h:45
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:47
content_iterator< RelocationRef > relocation_iterator
Definition: ObjectFile.h:77
content_iterator< BasicSymbolRef > basic_symbol_iterator
Definition: SymbolicFile.h:143
const llvm::EnumEntry< unsigned > ElfSymbolTypes[NumElfSymbolTypes]
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:970
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
@ Other
Any other memory.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
@ Dynamic
Denotes mode unknown at compile time.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition: Casting.h:565
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1879
endianness
Definition: bit.h:70
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:109
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1041
std::optional< DataRefImpl > Symbol
Definition: ELFObjectFile.h:52
struct llvm::object::DataRefImpl::@355 d