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 Expected<SubtargetFeatures> getRISCVFeatures() const;
64 SubtargetFeatures getLoongArchFeatures() const;
65
66 StringRef getAMDGPUCPUName() const;
67 StringRef getNVPTXCPUName() const;
68
69protected:
70 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
71
72 virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
73 virtual uint8_t getSymbolBinding(DataRefImpl Symb) const = 0;
74 virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
75 virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
76
77 virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
78 virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
79 virtual uint64_t getSectionOffset(DataRefImpl Sec) const = 0;
80
82 virtual Error getBuildAttributes(ELFAttributeParser &Attributes) const = 0;
83
84public:
86
88
89 /// Returns platform-specific object flags, if any.
90 virtual unsigned getPlatformFlags() const = 0;
91
93
94 static bool classof(const Binary *v) { return v->isELF(); }
95
97
98 std::optional<StringRef> tryGetCPUName() const override;
99
100 void setARMSubArch(Triple &TheTriple) const override;
101
102 virtual uint16_t getEType() const = 0;
103
104 virtual uint16_t getEMachine() const = 0;
105
106 virtual uint8_t getEIdentABIVersion() const = 0;
107
108 std::vector<ELFPltEntry> getPltEntries() const;
109
110 /// Returns a vector containing a symbol version for each dynamic symbol.
111 /// Returns an empty vector if version sections do not exist.
113
114 /// Returns a vector of all BB address maps in the object file. When
115 /// `TextSectionIndex` is specified, only returns the BB address maps
116 /// corresponding to the section with that index. When `PGOAnalyses`is
117 /// specified (PGOAnalyses is not nullptr), the vector is cleared then filled
118 /// with extra PGO data. `PGOAnalyses` will always be the same length as the
119 /// return value when it is requested assuming no error occurs. Upon failure,
120 /// `PGOAnalyses` will be emptied.
122 readBBAddrMap(std::optional<unsigned> TextSectionIndex = std::nullopt,
123 std::vector<PGOAnalysisMap> *PGOAnalyses = nullptr) const;
124};
125
126class ELFSectionRef : public SectionRef {
127public:
129 assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
130 }
131
133 return cast<ELFObjectFileBase>(SectionRef::getObject());
134 }
135
138 }
139
142 }
143
146 }
147};
148
150public:
152 assert(isa<ELFObjectFileBase>(B->getObject()));
153 }
154
155 const ELFSectionRef *operator->() const {
156 return static_cast<const ELFSectionRef *>(section_iterator::operator->());
157 }
158
159 const ELFSectionRef &operator*() const {
160 return static_cast<const ELFSectionRef &>(section_iterator::operator*());
161 }
162};
163
164class ELFSymbolRef : public SymbolRef {
165public:
167 assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
168 }
169
171 return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
172 }
173
176 }
177
178 uint8_t getBinding() const {
180 }
181
182 uint8_t getOther() const {
184 }
185
186 uint8_t getELFType() const {
188 }
189
191 uint8_t Type = getELFType();
192 for (const auto &EE : ElfSymbolTypes) {
193 if (EE.Value == Type) {
194 return EE.AltName;
195 }
196 }
197 return "";
198 }
199};
200
202public:
204 : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
206
207 const ELFSymbolRef *operator->() const {
208 return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
209 }
210
211 const ELFSymbolRef &operator*() const {
212 return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
213 }
214};
215
217public:
219 assert(isa<ELFObjectFileBase>(RelocationRef::getObject()));
220 }
221
223 return cast<ELFObjectFileBase>(RelocationRef::getObject());
224 }
225
228 }
229};
230
232public:
235 B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {}
236
238 return static_cast<const ELFRelocationRef *>(
240 }
241
243 return static_cast<const ELFRelocationRef &>(
245 }
246};
247
251}
252
253template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
254 uint16_t getEMachine() const override;
255 uint16_t getEType() const override;
256 uint8_t getEIdentABIVersion() const override;
257 uint64_t getSymbolSize(DataRefImpl Sym) const override;
258
259public:
261
262 SectionRef toSectionRef(const Elf_Shdr *Sec) const {
263 return SectionRef(toDRI(Sec), this);
264 }
265
266 ELFSymbolRef toSymbolRef(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
267 return ELFSymbolRef({toDRI(SymTable, SymbolNum), this});
268 }
269
270 bool IsContentValid() const { return ContentValid; }
271
272private:
274 const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,
275 const Elf_Shdr *DotSymtabShndxSec);
276
277 bool ContentValid = false;
278
279protected:
281
282 const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
283 const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
284 const Elf_Shdr *DotSymtabShndxSec = nullptr; // SHT_SYMTAB_SHNDX section.
285
286 Error initContent() override;
287
288 void moveSymbolNext(DataRefImpl &Symb) const override;
289 Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
291 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
292 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
293 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
294 Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override;
295 uint8_t getSymbolBinding(DataRefImpl Symb) const override;
296 uint8_t getSymbolOther(DataRefImpl Symb) const override;
297 uint8_t getSymbolELFType(DataRefImpl Symb) const override;
300 const Elf_Shdr *SymTab) const;
302
303 void moveSectionNext(DataRefImpl &Sec) const override;
305 uint64_t getSectionAddress(DataRefImpl Sec) const override;
306 uint64_t getSectionIndex(DataRefImpl Sec) const override;
307 uint64_t getSectionSize(DataRefImpl Sec) const override;
309 getSectionContents(DataRefImpl Sec) const override;
310 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
311 bool isSectionCompressed(DataRefImpl Sec) const override;
312 bool isSectionText(DataRefImpl Sec) const override;
313 bool isSectionData(DataRefImpl Sec) const override;
314 bool isSectionBSS(DataRefImpl Sec) const override;
315 bool isSectionVirtual(DataRefImpl Sec) const override;
316 bool isBerkeleyText(DataRefImpl Sec) const override;
317 bool isBerkeleyData(DataRefImpl Sec) const override;
318 bool isDebugSection(DataRefImpl Sec) const override;
321 std::vector<SectionRef> dynamic_relocation_sections() const override;
323 getRelocatedSection(DataRefImpl Sec) const override;
324
325 void moveRelocationNext(DataRefImpl &Rel) const override;
326 uint64_t getRelocationOffset(DataRefImpl Rel) const override;
328 uint64_t getRelocationType(DataRefImpl Rel) const override;
330 SmallVectorImpl<char> &Result) const override;
331
332 uint32_t getSectionType(DataRefImpl Sec) const override;
333 uint64_t getSectionFlags(DataRefImpl Sec) const override;
334 uint64_t getSectionOffset(DataRefImpl Sec) const override;
336
337 DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
338 DataRefImpl DRI;
339 if (!SymTable) {
340 DRI.d.a = 0;
341 DRI.d.b = 0;
342 return DRI;
343 }
344 assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
345 SymTable->sh_type == ELF::SHT_DYNSYM);
346
347 auto SectionsOrErr = EF.sections();
348 if (!SectionsOrErr) {
349 DRI.d.a = 0;
350 DRI.d.b = 0;
351 return DRI;
352 }
353 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
354 unsigned SymTableIndex =
355 (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
356
357 DRI.d.a = SymTableIndex;
358 DRI.d.b = SymbolNum;
359 return DRI;
360 }
361
362 const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
363 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
364 }
365
366 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
367 DataRefImpl DRI;
368 DRI.p = reinterpret_cast<uintptr_t>(Sec);
369 return DRI;
370 }
371
372 DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
373 DataRefImpl DRI;
374 DRI.p = reinterpret_cast<uintptr_t>(Dyn);
375 return DRI;
376 }
377
378 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
379 unsigned char Binding = ESym->getBinding();
380 unsigned char Visibility = ESym->getVisibility();
381
382 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
383 // visibility is either DEFAULT or PROTECTED. All other symbols are not
384 // exported.
385 return (
386 (Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK ||
387 Binding == ELF::STB_GNU_UNIQUE) &&
388 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED));
389 }
390
391 Error getBuildAttributes(ELFAttributeParser &Attributes) const override {
393 switch (getEMachine()) {
394 case ELF::EM_ARM:
396 break;
397 case ELF::EM_RISCV:
399 break;
400 default:
401 return Error::success();
402 }
403
404 auto SectionsOrErr = EF.sections();
405 if (!SectionsOrErr)
406 return SectionsOrErr.takeError();
407 for (const Elf_Shdr &Sec : *SectionsOrErr) {
408 if (Sec.sh_type != Type)
409 continue;
410 auto ErrorOrContents = EF.getSectionContents(Sec);
411 if (!ErrorOrContents)
412 return ErrorOrContents.takeError();
413
414 auto Contents = ErrorOrContents.get();
415 if (Contents[0] != ELFAttrs::Format_Version || Contents.size() == 1)
416 return Error::success();
417
418 if (Error E = Attributes.parse(Contents, ELFT::TargetEndianness))
419 return E;
420 break;
421 }
422 return Error::success();
423 }
424
425 // This flag is used for classof, to distinguish ELFObjectFile from
426 // its subclass. If more subclasses will be created, this flag will
427 // have to become an enum.
428 bool isDyldELFObject = false;
429
430public:
433 bool InitContent = true);
434
435 const Elf_Rel *getRel(DataRefImpl Rel) const;
436 const Elf_Rela *getRela(DataRefImpl Rela) const;
437
439 return EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
440 }
441
442 /// Get the relocation section that contains \a Rel.
443 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
444 auto RelSecOrErr = EF.getSection(Rel.d.a);
445 if (!RelSecOrErr)
447 Twine(errorToErrorCode(RelSecOrErr.takeError()).message()));
448 return *RelSecOrErr;
449 }
450
451 const Elf_Shdr *getSection(DataRefImpl Sec) const {
452 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
453 }
454
455 basic_symbol_iterator symbol_begin() const override;
456 basic_symbol_iterator symbol_end() const override;
457
458 bool is64Bit() const override { return getBytesInAddress() == 8; }
459
462
463 section_iterator section_begin() const override;
464 section_iterator section_end() const override;
465
467
468 uint8_t getBytesInAddress() const override;
469 StringRef getFileFormatName() const override;
470 Triple::ArchType getArch() const override;
471 Triple::OSType getOS() const override;
472 Expected<uint64_t> getStartAddress() const override;
473
474 unsigned getPlatformFlags() const override { return EF.getHeader().e_flags; }
475
476 const ELFFile<ELFT> &getELFFile() const { return EF; }
477
478 bool isDyldType() const { return isDyldELFObject; }
479 static bool classof(const Binary *v) {
480 return v->getType() ==
481 getELFType(ELFT::TargetEndianness == llvm::endianness::little,
482 ELFT::Is64Bits);
483 }
484
486
487 bool isRelocatableObject() const override;
488
489 void createFakeSections() { EF.createFakeSections(); }
490};
491
496
497template <class ELFT>
499 ++Sym.d.b;
500}
501
503 auto SectionsOrErr = EF.sections();
504 if (!SectionsOrErr)
505 return SectionsOrErr.takeError();
506
507 for (const Elf_Shdr &Sec : *SectionsOrErr) {
508 switch (Sec.sh_type) {
509 case ELF::SHT_DYNSYM: {
510 if (!DotDynSymSec)
511 DotDynSymSec = &Sec;
512 break;
513 }
514 case ELF::SHT_SYMTAB: {
515 if (!DotSymtabSec)
516 DotSymtabSec = &Sec;
517 break;
518 }
520 if (!DotSymtabShndxSec)
521 DotSymtabShndxSec = &Sec;
522 break;
523 }
524 }
525 }
526
527 ContentValid = true;
528 return Error::success();
529}
530
531template <class ELFT>
533 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
534 if (!SymOrErr)
535 return SymOrErr.takeError();
536 auto SymTabOrErr = EF.getSection(Sym.d.a);
537 if (!SymTabOrErr)
538 return SymTabOrErr.takeError();
539 const Elf_Shdr *SymTableSec = *SymTabOrErr;
540 auto StrTabOrErr = EF.getSection(SymTableSec->sh_link);
541 if (!StrTabOrErr)
542 return StrTabOrErr.takeError();
543 const Elf_Shdr *StringTableSec = *StrTabOrErr;
544 auto SymStrTabOrErr = EF.getStringTable(*StringTableSec);
545 if (!SymStrTabOrErr)
546 return SymStrTabOrErr.takeError();
547 Expected<StringRef> Name = (*SymOrErr)->getName(*SymStrTabOrErr);
548 if (Name && !Name->empty())
549 return Name;
550
551 // If the symbol name is empty use the section name.
552 if ((*SymOrErr)->getType() == ELF::STT_SECTION) {
553 Expected<section_iterator> SecOrErr = getSymbolSection(Sym);
554 if (SecOrErr)
555 return (*SecOrErr)->getName();
556 return SecOrErr.takeError();
557 }
558 return Name;
559}
560
561template <class ELFT>
563 return getSection(Sec)->sh_flags;
564}
565
566template <class ELFT>
568 return getSection(Sec)->sh_type;
569}
570
571template <class ELFT>
573 return getSection(Sec)->sh_offset;
574}
575
576template <class ELFT>
578 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
579 if (!SymOrErr)
580 report_fatal_error(SymOrErr.takeError());
581
582 uint64_t Ret = (*SymOrErr)->st_value;
583 if ((*SymOrErr)->st_shndx == ELF::SHN_ABS)
584 return Ret;
585
586 const Elf_Ehdr &Header = EF.getHeader();
587 // Clear the ARM/Thumb or microMIPS indicator flag.
588 if ((Header.e_machine == ELF::EM_ARM || Header.e_machine == ELF::EM_MIPS) &&
589 (*SymOrErr)->getType() == ELF::STT_FUNC)
590 Ret &= ~1;
591
592 return Ret;
593}
594
595template <class ELFT>
598 Expected<uint64_t> SymbolValueOrErr = getSymbolValue(Symb);
599 if (!SymbolValueOrErr)
600 // TODO: Test this error.
601 return SymbolValueOrErr.takeError();
602
603 uint64_t Result = *SymbolValueOrErr;
604 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
605 if (!SymOrErr)
606 return SymOrErr.takeError();
607
608 switch ((*SymOrErr)->st_shndx) {
609 case ELF::SHN_COMMON:
610 case ELF::SHN_UNDEF:
611 case ELF::SHN_ABS:
612 return Result;
613 }
614
615 auto SymTabOrErr = EF.getSection(Symb.d.a);
616 if (!SymTabOrErr)
617 return SymTabOrErr.takeError();
618
619 if (EF.getHeader().e_type == ELF::ET_REL) {
620 ArrayRef<Elf_Word> ShndxTable;
621 if (DotSymtabShndxSec) {
622 // TODO: Test this error.
623 if (Expected<ArrayRef<Elf_Word>> ShndxTableOrErr =
624 EF.getSHNDXTable(*DotSymtabShndxSec))
625 ShndxTable = *ShndxTableOrErr;
626 else
627 return ShndxTableOrErr.takeError();
628 }
629
630 Expected<const Elf_Shdr *> SectionOrErr =
631 EF.getSection(**SymOrErr, *SymTabOrErr, ShndxTable);
632 if (!SectionOrErr)
633 return SectionOrErr.takeError();
634 const Elf_Shdr *Section = *SectionOrErr;
635 if (Section)
636 Result += Section->sh_addr;
637 }
638
639 return Result;
640}
641
642template <class ELFT>
644 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
645 if (!SymOrErr)
646 report_fatal_error(SymOrErr.takeError());
647 if ((*SymOrErr)->st_shndx == ELF::SHN_COMMON)
648 return (*SymOrErr)->st_value;
649 return 0;
650}
651
652template <class ELFT>
654 return EF.getHeader().e_machine;
655}
656
657template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const {
658 return EF.getHeader().e_type;
659}
660
661template <class ELFT> uint8_t ELFObjectFile<ELFT>::getEIdentABIVersion() const {
662 return EF.getHeader().e_ident[ELF::EI_ABIVERSION];
663}
664
665template <class ELFT>
666uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
667 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
668 if (!SymOrErr)
669 report_fatal_error(SymOrErr.takeError());
670 return (*SymOrErr)->st_size;
671}
672
673template <class ELFT>
675 return getSymbolSize(Symb);
676}
677
678template <class ELFT>
680 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
681 if (!SymOrErr)
682 report_fatal_error(SymOrErr.takeError());
683 return (*SymOrErr)->getBinding();
684}
685
686template <class ELFT>
688 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
689 if (!SymOrErr)
690 report_fatal_error(SymOrErr.takeError());
691 return (*SymOrErr)->st_other;
692}
693
694template <class ELFT>
696 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
697 if (!SymOrErr)
698 report_fatal_error(SymOrErr.takeError());
699 return (*SymOrErr)->getType();
700}
701
702template <class ELFT>
705 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
706 if (!SymOrErr)
707 return SymOrErr.takeError();
708
709 switch ((*SymOrErr)->getType()) {
710 case ELF::STT_NOTYPE:
712 case ELF::STT_SECTION:
713 return SymbolRef::ST_Debug;
714 case ELF::STT_FILE:
715 return SymbolRef::ST_File;
716 case ELF::STT_FUNC:
718 case ELF::STT_OBJECT:
719 case ELF::STT_COMMON:
720 return SymbolRef::ST_Data;
721 case ELF::STT_TLS:
722 default:
723 return SymbolRef::ST_Other;
724 }
725}
726
727template <class ELFT>
729 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
730 if (!SymOrErr)
731 return SymOrErr.takeError();
732
733 const Elf_Sym *ESym = *SymOrErr;
735
736 if (ESym->getBinding() != ELF::STB_LOCAL)
737 Result |= SymbolRef::SF_Global;
738
739 if (ESym->getBinding() == ELF::STB_WEAK)
740 Result |= SymbolRef::SF_Weak;
741
742 if (ESym->st_shndx == ELF::SHN_ABS)
743 Result |= SymbolRef::SF_Absolute;
744
745 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION)
747
748 if (Expected<typename ELFT::SymRange> SymbolsOrErr =
749 EF.symbols(DotSymtabSec)) {
750 // Set the SF_FormatSpecific flag for the 0-index null symbol.
751 if (ESym == SymbolsOrErr->begin())
753 } else
754 // TODO: Test this error.
755 return SymbolsOrErr.takeError();
756
757 if (Expected<typename ELFT::SymRange> SymbolsOrErr =
758 EF.symbols(DotDynSymSec)) {
759 // Set the SF_FormatSpecific flag for the 0-index null symbol.
760 if (ESym == SymbolsOrErr->begin())
762 } else
763 // TODO: Test this error.
764 return SymbolsOrErr.takeError();
765
766 if (EF.getHeader().e_machine == ELF::EM_AARCH64) {
767 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
768 StringRef Name = *NameOrErr;
769 if (Name.starts_with("$d") || Name.starts_with("$x"))
771 } else {
772 // TODO: Actually report errors helpfully.
773 consumeError(NameOrErr.takeError());
774 }
775 } else if (EF.getHeader().e_machine == ELF::EM_ARM) {
776 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
777 StringRef Name = *NameOrErr;
778 // TODO Investigate why empty name symbols need to be marked.
779 if (Name.empty() || Name.starts_with("$d") || Name.starts_with("$t") ||
780 Name.starts_with("$a"))
782 } else {
783 // TODO: Actually report errors helpfully.
784 consumeError(NameOrErr.takeError());
785 }
786 if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1)
787 Result |= SymbolRef::SF_Thumb;
788 } else if (EF.getHeader().e_machine == ELF::EM_CSKY) {
789 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
790 StringRef Name = *NameOrErr;
791 if (Name.starts_with("$d") || Name.starts_with("$t"))
793 } else {
794 // TODO: Actually report errors helpfully.
795 consumeError(NameOrErr.takeError());
796 }
797 } else if (EF.getHeader().e_machine == ELF::EM_RISCV) {
798 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
799 StringRef Name = *NameOrErr;
800 // Mark empty name symbols (used for label differences) and mapping
801 // symbols.
802 if (Name.empty() || Name.starts_with("$d") || Name.starts_with("$x"))
804 } else {
805 // TODO: Actually report errors helpfully.
806 consumeError(NameOrErr.takeError());
807 }
808 }
809
810 if (ESym->st_shndx == ELF::SHN_UNDEF)
811 Result |= SymbolRef::SF_Undefined;
812
813 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
814 Result |= SymbolRef::SF_Common;
815
816 if (isExportedToOtherDSO(ESym))
817 Result |= SymbolRef::SF_Exported;
818
819 if (ESym->getType() == ELF::STT_GNU_IFUNC)
820 Result |= SymbolRef::SF_Indirect;
821
822 if (ESym->getVisibility() == ELF::STV_HIDDEN)
823 Result |= SymbolRef::SF_Hidden;
824
825 return Result;
826}
827
828template <class ELFT>
831 const Elf_Shdr *SymTab) const {
832 ArrayRef<Elf_Word> ShndxTable;
833 if (DotSymtabShndxSec) {
834 // TODO: Test this error.
835 Expected<ArrayRef<Elf_Word>> ShndxTableOrErr =
836 EF.getSHNDXTable(*DotSymtabShndxSec);
837 if (!ShndxTableOrErr)
838 return ShndxTableOrErr.takeError();
839 ShndxTable = *ShndxTableOrErr;
840 }
841
842 auto ESecOrErr = EF.getSection(*ESym, SymTab, ShndxTable);
843 if (!ESecOrErr)
844 return ESecOrErr.takeError();
845
846 const Elf_Shdr *ESec = *ESecOrErr;
847 if (!ESec)
848 return section_end();
849
850 DataRefImpl Sec;
851 Sec.p = reinterpret_cast<intptr_t>(ESec);
852 return section_iterator(SectionRef(Sec, this));
853}
854
855template <class ELFT>
858 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
859 if (!SymOrErr)
860 return SymOrErr.takeError();
861
862 auto SymTabOrErr = EF.getSection(Symb.d.a);
863 if (!SymTabOrErr)
864 return SymTabOrErr.takeError();
865 return getSymbolSection(*SymOrErr, *SymTabOrErr);
866}
867
868template <class ELFT>
870 const Elf_Shdr *ESec = getSection(Sec);
871 Sec = toDRI(++ESec);
872}
873
874template <class ELFT>
876 return EF.getSectionName(*getSection(Sec));
877}
878
879template <class ELFT>
881 return getSection(Sec)->sh_addr;
882}
883
884template <class ELFT>
886 auto SectionsOrErr = EF.sections();
887 handleAllErrors(std::move(SectionsOrErr.takeError()),
888 [](const ErrorInfoBase &) {
889 llvm_unreachable("unable to get section index");
890 });
891 const Elf_Shdr *First = SectionsOrErr->begin();
892 return getSection(Sec) - First;
893}
894
895template <class ELFT>
897 return getSection(Sec)->sh_size;
898}
899
900template <class ELFT>
903 const Elf_Shdr *EShdr = getSection(Sec);
904 if (EShdr->sh_type == ELF::SHT_NOBITS)
905 return ArrayRef((const uint8_t *)base(), (size_t)0);
906 if (Error E =
907 checkOffset(getMemoryBufferRef(),
908 (uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
909 return std::move(E);
910 return ArrayRef((const uint8_t *)base() + EShdr->sh_offset, EShdr->sh_size);
911}
912
913template <class ELFT>
915 return getSection(Sec)->sh_addralign;
916}
917
918template <class ELFT>
920 return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED;
921}
922
923template <class ELFT>
925 return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR;
926}
927
928template <class ELFT>
930 const Elf_Shdr *EShdr = getSection(Sec);
931 return EShdr->sh_type == ELF::SHT_PROGBITS &&
932 EShdr->sh_flags & ELF::SHF_ALLOC &&
933 !(EShdr->sh_flags & ELF::SHF_EXECINSTR);
934}
935
936template <class ELFT>
938 const Elf_Shdr *EShdr = getSection(Sec);
939 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
940 EShdr->sh_type == ELF::SHT_NOBITS;
941}
942
943template <class ELFT>
944std::vector<SectionRef>
946 std::vector<SectionRef> Res;
947 std::vector<uintptr_t> Offsets;
948
949 auto SectionsOrErr = EF.sections();
950 if (!SectionsOrErr)
951 return Res;
952
953 for (const Elf_Shdr &Sec : *SectionsOrErr) {
954 if (Sec.sh_type != ELF::SHT_DYNAMIC)
955 continue;
956 Elf_Dyn *Dynamic =
957 reinterpret_cast<Elf_Dyn *>((uintptr_t)base() + Sec.sh_offset);
958 for (; Dynamic->d_tag != ELF::DT_NULL; Dynamic++) {
959 if (Dynamic->d_tag == ELF::DT_REL || Dynamic->d_tag == ELF::DT_RELA ||
960 Dynamic->d_tag == ELF::DT_JMPREL) {
961 Offsets.push_back(Dynamic->d_un.d_val);
962 }
963 }
964 }
965 for (const Elf_Shdr &Sec : *SectionsOrErr) {
966 if (is_contained(Offsets, Sec.sh_addr))
967 Res.emplace_back(toDRI(&Sec), this);
968 }
969 return Res;
970}
971
972template <class ELFT>
974 return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
975}
976
977template <class ELFT>
979 return getSection(Sec)->sh_flags & ELF::SHF_ALLOC &&
980 (getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR ||
981 !(getSection(Sec)->sh_flags & ELF::SHF_WRITE));
982}
983
984template <class ELFT>
986 const Elf_Shdr *EShdr = getSection(Sec);
987 return !isBerkeleyText(Sec) && EShdr->sh_type != ELF::SHT_NOBITS &&
988 EShdr->sh_flags & ELF::SHF_ALLOC;
989}
990
991template <class ELFT>
993 Expected<StringRef> SectionNameOrErr = getSectionName(Sec);
994 if (!SectionNameOrErr) {
995 // TODO: Report the error message properly.
996 consumeError(SectionNameOrErr.takeError());
997 return false;
998 }
999 StringRef SectionName = SectionNameOrErr.get();
1000 return SectionName.starts_with(".debug") ||
1001 SectionName.starts_with(".zdebug") || SectionName == ".gdb_index";
1002}
1003
1004template <class ELFT>
1007 DataRefImpl RelData;
1008 auto SectionsOrErr = EF.sections();
1009 if (!SectionsOrErr)
1011 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
1012 RelData.d.a = (Sec.p - SHT) / EF.getHeader().e_shentsize;
1013 RelData.d.b = 0;
1014 return relocation_iterator(RelocationRef(RelData, this));
1015}
1016
1017template <class ELFT>
1020 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1021 relocation_iterator Begin = section_rel_begin(Sec);
1022 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
1023 return Begin;
1024 DataRefImpl RelData = Begin->getRawDataRefImpl();
1025 const Elf_Shdr *RelSec = getRelSection(RelData);
1026
1027 // Error check sh_link here so that getRelocationSymbol can just use it.
1028 auto SymSecOrErr = EF.getSection(RelSec->sh_link);
1029 if (!SymSecOrErr)
1031 Twine(errorToErrorCode(SymSecOrErr.takeError()).message()));
1032
1033 RelData.d.b += S->sh_size / S->sh_entsize;
1034 return relocation_iterator(RelocationRef(RelData, this));
1035}
1036
1037template <class ELFT>
1040 const Elf_Shdr *EShdr = getSection(Sec);
1041 uintX_t Type = EShdr->sh_type;
1042 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
1043 return section_end();
1044
1045 Expected<const Elf_Shdr *> SecOrErr = EF.getSection(EShdr->sh_info);
1046 if (!SecOrErr)
1047 return SecOrErr.takeError();
1048 return section_iterator(SectionRef(toDRI(*SecOrErr), this));
1049}
1050
1051// Relocations
1052template <class ELFT>
1054 ++Rel.d.b;
1055}
1056
1057template <class ELFT>
1060 uint32_t symbolIdx;
1061 const Elf_Shdr *sec = getRelSection(Rel);
1062 if (sec->sh_type == ELF::SHT_REL)
1063 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
1064 else
1065 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
1066 if (!symbolIdx)
1067 return symbol_end();
1068
1069 // FIXME: error check symbolIdx
1070 DataRefImpl SymbolData;
1071 SymbolData.d.a = sec->sh_link;
1072 SymbolData.d.b = symbolIdx;
1073 return symbol_iterator(SymbolRef(SymbolData, this));
1074}
1075
1076template <class ELFT>
1078 const Elf_Shdr *sec = getRelSection(Rel);
1079 if (sec->sh_type == ELF::SHT_REL)
1080 return getRel(Rel)->r_offset;
1081
1082 return getRela(Rel)->r_offset;
1083}
1084
1085template <class ELFT>
1087 const Elf_Shdr *sec = getRelSection(Rel);
1088 if (sec->sh_type == ELF::SHT_REL)
1089 return getRel(Rel)->getType(EF.isMips64EL());
1090 else
1091 return getRela(Rel)->getType(EF.isMips64EL());
1092}
1093
1094template <class ELFT>
1096 return getELFRelocationTypeName(EF.getHeader().e_machine, Type);
1097}
1098
1099template <class ELFT>
1101 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1102 uint32_t type = getRelocationType(Rel);
1103 EF.getRelocationTypeName(type, Result);
1104}
1105
1106template <class ELFT>
1109 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
1110 return createError("Section is not SHT_RELA");
1111 return (int64_t)getRela(Rel)->r_addend;
1112}
1113
1114template <class ELFT>
1115const typename ELFObjectFile<ELFT>::Elf_Rel *
1117 assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
1118 auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
1119 if (!Ret)
1120 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message()));
1121 return *Ret;
1122}
1123
1124template <class ELFT>
1125const typename ELFObjectFile<ELFT>::Elf_Rela *
1127 assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
1128 auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
1129 if (!Ret)
1130 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message()));
1131 return *Ret;
1132}
1133
1134template <class ELFT>
1137 auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer());
1138 if (Error E = EFOrErr.takeError())
1139 return std::move(E);
1140
1141 ELFObjectFile<ELFT> Obj = {Object, std::move(*EFOrErr), nullptr, nullptr,
1142 nullptr};
1143 if (InitContent)
1144 if (Error E = Obj.initContent())
1145 return std::move(E);
1146 return std::move(Obj);
1147}
1148
1149template <class ELFT>
1151 const Elf_Shdr *DotDynSymSec,
1152 const Elf_Shdr *DotSymtabSec,
1153 const Elf_Shdr *DotSymtabShndx)
1155 getELFType(ELFT::TargetEndianness == llvm::endianness::little,
1156 ELFT::Is64Bits),
1157 Object),
1158 EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
1159 DotSymtabShndxSec(DotSymtabShndx) {}
1160
1161template <class ELFT>
1163 : ELFObjectFile(Other.Data, Other.EF, Other.DotDynSymSec,
1164 Other.DotSymtabSec, Other.DotSymtabShndxSec) {}
1165
1166template <class ELFT>
1169 toDRI(DotSymtabSec,
1170 DotSymtabSec && DotSymtabSec->sh_size >= sizeof(Elf_Sym) ? 1 : 0);
1171 return basic_symbol_iterator(SymbolRef(Sym, this));
1172}
1173
1174template <class ELFT>
1176 const Elf_Shdr *SymTab = DotSymtabSec;
1177 if (!SymTab)
1178 return symbol_begin();
1179 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1180 return basic_symbol_iterator(SymbolRef(Sym, this));
1181}
1182
1183template <class ELFT>
1185 if (!DotDynSymSec || DotDynSymSec->sh_size < sizeof(Elf_Sym))
1186 // Ignore errors here where the dynsym is empty or sh_size less than the
1187 // size of one symbol. These should be handled elsewhere.
1188 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 0), this));
1189 // Skip 0-index NULL symbol.
1190 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 1), this));
1191}
1192
1193template <class ELFT>
1195 const Elf_Shdr *SymTab = DotDynSymSec;
1196 if (!SymTab)
1197 return dynamic_symbol_begin();
1198 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1199 return basic_symbol_iterator(SymbolRef(Sym, this));
1200}
1201
1202template <class ELFT>
1204 auto SectionsOrErr = EF.sections();
1205 if (!SectionsOrErr)
1206 return section_iterator(SectionRef());
1207 return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this));
1208}
1209
1210template <class ELFT>
1212 auto SectionsOrErr = EF.sections();
1213 if (!SectionsOrErr)
1214 return section_iterator(SectionRef());
1215 return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
1216}
1217
1218template <class ELFT>
1220 return ELFT::Is64Bits ? 8 : 4;
1221}
1222
1223template <class ELFT>
1225 constexpr bool IsLittleEndian =
1226 ELFT::TargetEndianness == llvm::endianness::little;
1227 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1228 case ELF::ELFCLASS32:
1229 switch (EF.getHeader().e_machine) {
1230 case ELF::EM_68K:
1231 return "elf32-m68k";
1232 case ELF::EM_386:
1233 return "elf32-i386";
1234 case ELF::EM_IAMCU:
1235 return "elf32-iamcu";
1236 case ELF::EM_X86_64:
1237 return "elf32-x86-64";
1238 case ELF::EM_ARM:
1239 return (IsLittleEndian ? "elf32-littlearm" : "elf32-bigarm");
1240 case ELF::EM_AVR:
1241 return "elf32-avr";
1242 case ELF::EM_HEXAGON:
1243 return "elf32-hexagon";
1244 case ELF::EM_LANAI:
1245 return "elf32-lanai";
1246 case ELF::EM_MIPS:
1247 return "elf32-mips";
1248 case ELF::EM_MSP430:
1249 return "elf32-msp430";
1250 case ELF::EM_PPC:
1251 return (IsLittleEndian ? "elf32-powerpcle" : "elf32-powerpc");
1252 case ELF::EM_RISCV:
1253 return "elf32-littleriscv";
1254 case ELF::EM_CSKY:
1255 return "elf32-csky";
1256 case ELF::EM_SPARC:
1258 return "elf32-sparc";
1259 case ELF::EM_AMDGPU:
1260 return "elf32-amdgpu";
1261 case ELF::EM_LOONGARCH:
1262 return "elf32-loongarch";
1263 case ELF::EM_XTENSA:
1264 return "elf32-xtensa";
1265 default:
1266 return "elf32-unknown";
1267 }
1268 case ELF::ELFCLASS64:
1269 switch (EF.getHeader().e_machine) {
1270 case ELF::EM_386:
1271 return "elf64-i386";
1272 case ELF::EM_X86_64:
1273 return "elf64-x86-64";
1274 case ELF::EM_AARCH64:
1275 return (IsLittleEndian ? "elf64-littleaarch64" : "elf64-bigaarch64");
1276 case ELF::EM_PPC64:
1277 return (IsLittleEndian ? "elf64-powerpcle" : "elf64-powerpc");
1278 case ELF::EM_RISCV:
1279 return "elf64-littleriscv";
1280 case ELF::EM_S390:
1281 return "elf64-s390";
1282 case ELF::EM_SPARCV9:
1283 return "elf64-sparc";
1284 case ELF::EM_MIPS:
1285 return "elf64-mips";
1286 case ELF::EM_AMDGPU:
1287 return "elf64-amdgpu";
1288 case ELF::EM_BPF:
1289 return "elf64-bpf";
1290 case ELF::EM_VE:
1291 return "elf64-ve";
1292 case ELF::EM_LOONGARCH:
1293 return "elf64-loongarch";
1294 default:
1295 return "elf64-unknown";
1296 }
1297 default:
1298 // FIXME: Proper error handling.
1299 report_fatal_error("Invalid ELFCLASS!");
1300 }
1301}
1302
1303template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
1304 bool IsLittleEndian = ELFT::TargetEndianness == llvm::endianness::little;
1305 switch (EF.getHeader().e_machine) {
1306 case ELF::EM_68K:
1307 return Triple::m68k;
1308 case ELF::EM_386:
1309 case ELF::EM_IAMCU:
1310 return Triple::x86;
1311 case ELF::EM_X86_64:
1312 return Triple::x86_64;
1313 case ELF::EM_AARCH64:
1314 return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be;
1315 case ELF::EM_ARM:
1316 return Triple::arm;
1317 case ELF::EM_AVR:
1318 return Triple::avr;
1319 case ELF::EM_HEXAGON:
1320 return Triple::hexagon;
1321 case ELF::EM_LANAI:
1322 return Triple::lanai;
1323 case ELF::EM_MIPS:
1324 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1325 case ELF::ELFCLASS32:
1326 return IsLittleEndian ? Triple::mipsel : Triple::mips;
1327 case ELF::ELFCLASS64:
1328 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
1329 default:
1330 report_fatal_error("Invalid ELFCLASS!");
1331 }
1332 case ELF::EM_MSP430:
1333 return Triple::msp430;
1334 case ELF::EM_PPC:
1335 return IsLittleEndian ? Triple::ppcle : Triple::ppc;
1336 case ELF::EM_PPC64:
1337 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
1338 case ELF::EM_RISCV:
1339 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1340 case ELF::ELFCLASS32:
1341 return Triple::riscv32;
1342 case ELF::ELFCLASS64:
1343 return Triple::riscv64;
1344 default:
1345 report_fatal_error("Invalid ELFCLASS!");
1346 }
1347 case ELF::EM_S390:
1348 return Triple::systemz;
1349
1350 case ELF::EM_SPARC:
1352 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
1353 case ELF::EM_SPARCV9:
1354 return Triple::sparcv9;
1355
1356 case ELF::EM_AMDGPU: {
1357 if (!IsLittleEndian)
1358 return Triple::UnknownArch;
1359
1360 unsigned MACH = EF.getHeader().e_flags & ELF::EF_AMDGPU_MACH;
1361 if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST &&
1363 return Triple::r600;
1366 return Triple::amdgcn;
1367
1368 return Triple::UnknownArch;
1369 }
1370
1371 case ELF::EM_CUDA: {
1372 if (EF.getHeader().e_ident[ELF::EI_CLASS] == ELF::ELFCLASS32)
1373 return Triple::nvptx;
1374 return Triple::nvptx64;
1375 }
1376
1377 case ELF::EM_BPF:
1378 return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
1379
1380 case ELF::EM_VE:
1381 return Triple::ve;
1382 case ELF::EM_CSKY:
1383 return Triple::csky;
1384
1385 case ELF::EM_LOONGARCH:
1386 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1387 case ELF::ELFCLASS32:
1388 return Triple::loongarch32;
1389 case ELF::ELFCLASS64:
1390 return Triple::loongarch64;
1391 default:
1392 report_fatal_error("Invalid ELFCLASS!");
1393 }
1394
1395 case ELF::EM_XTENSA:
1396 return Triple::xtensa;
1397
1398 default:
1399 return Triple::UnknownArch;
1400 }
1401}
1402
1403template <class ELFT> Triple::OSType ELFObjectFile<ELFT>::getOS() const {
1404 switch (EF.getHeader().e_ident[ELF::EI_OSABI]) {
1406 return Triple::NetBSD;
1408 return Triple::Linux;
1409 case ELF::ELFOSABI_HURD:
1410 return Triple::Hurd;
1412 return Triple::Solaris;
1413 case ELF::ELFOSABI_AIX:
1414 return Triple::AIX;
1416 return Triple::FreeBSD;
1418 return Triple::OpenBSD;
1419 case ELF::ELFOSABI_CUDA:
1420 return Triple::CUDA;
1422 return Triple::AMDHSA;
1424 return Triple::AMDPAL;
1426 return Triple::Mesa3D;
1427 default:
1428 return Triple::UnknownOS;
1429 }
1430}
1431
1432template <class ELFT>
1434 return EF.getHeader().e_entry;
1435}
1436
1437template <class ELFT>
1440 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
1441}
1442
1443template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
1444 return EF.getHeader().e_type == ELF::ET_REL;
1445}
1446
1447} // end namespace object
1448} // end namespace llvm
1449
1450#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< 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:85
virtual uint16_t getEMachine() const =0
static bool classof(const Binary *v)
Definition: ELFObjectFile.h:94
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:1155
@ SHF_COMPRESSED
Definition: ELF.h:1183
@ SHF_WRITE
Definition: ELF.h:1152
@ SHF_EXECINSTR
Definition: ELF.h:1158
@ 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:1340
@ STV_PROTECTED
Definition: ELF.h:1341
@ STV_DEFAULT
Definition: ELF.h:1338
@ 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_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:1309
@ STB_LOCAL
Definition: ELF.h:1308
@ STB_GNU_UNIQUE
Definition: ELF.h:1311
@ STB_WEAK
Definition: ELF.h:1310
@ ET_REL
Definition: ELF.h:116
@ STT_FUNC
Definition: ELF.h:1322
@ STT_NOTYPE
Definition: ELF.h:1320
@ STT_SECTION
Definition: ELF.h:1323
@ STT_FILE
Definition: ELF.h:1324
@ STT_COMMON
Definition: ELF.h:1325
@ STT_GNU_IFUNC
Definition: ELF.h:1327
@ STT_OBJECT
Definition: ELF.h:1321
@ STT_TLS
Definition: ELF.h:1326
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
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:1888
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::@353 d