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