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