LLVM 19.0.0git
ELFObjectFile.h
Go to the documentation of this file.
1//===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the ELFObjectFile template class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_OBJECT_ELFOBJECTFILE_H
14#define LLVM_OBJECT_ELFOBJECTFILE_H
15
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/StringRef.h"
21#include "llvm/Object/Binary.h"
22#include "llvm/Object/ELF.h"
24#include "llvm/Object/Error.h"
30#include "llvm/Support/Error.h"
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_type == ELF::SHT_PROGBITS &&
953 EShdr->sh_flags & ELF::SHF_ALLOC &&
954 !(EShdr->sh_flags & ELF::SHF_EXECINSTR);
955}
956
957template <class ELFT>
959 const Elf_Shdr *EShdr = getSection(Sec);
960 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
961 EShdr->sh_type == ELF::SHT_NOBITS;
962}
963
964template <class ELFT>
965std::vector<SectionRef>
967 std::vector<SectionRef> Res;
968 std::vector<uintptr_t> Offsets;
969
970 auto SectionsOrErr = EF.sections();
971 if (!SectionsOrErr)
972 return Res;
973
974 for (const Elf_Shdr &Sec : *SectionsOrErr) {
975 if (Sec.sh_type != ELF::SHT_DYNAMIC)
976 continue;
977 Elf_Dyn *Dynamic =
978 reinterpret_cast<Elf_Dyn *>((uintptr_t)base() + Sec.sh_offset);
979 for (; Dynamic->d_tag != ELF::DT_NULL; Dynamic++) {
980 if (Dynamic->d_tag == ELF::DT_REL || Dynamic->d_tag == ELF::DT_RELA ||
981 Dynamic->d_tag == ELF::DT_JMPREL) {
982 Offsets.push_back(Dynamic->d_un.d_val);
983 }
984 }
985 }
986 for (const Elf_Shdr &Sec : *SectionsOrErr) {
987 if (is_contained(Offsets, Sec.sh_addr))
988 Res.emplace_back(toDRI(&Sec), this);
989 }
990 return Res;
991}
992
993template <class ELFT>
995 return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
996}
997
998template <class ELFT>
1000 return getSection(Sec)->sh_flags & ELF::SHF_ALLOC &&
1001 (getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR ||
1002 !(getSection(Sec)->sh_flags & ELF::SHF_WRITE));
1003}
1004
1005template <class ELFT>
1007 const Elf_Shdr *EShdr = getSection(Sec);
1008 return !isBerkeleyText(Sec) && EShdr->sh_type != ELF::SHT_NOBITS &&
1009 EShdr->sh_flags & ELF::SHF_ALLOC;
1010}
1011
1012template <class ELFT>
1014 Expected<StringRef> SectionNameOrErr = getSectionName(Sec);
1015 if (!SectionNameOrErr) {
1016 // TODO: Report the error message properly.
1017 consumeError(SectionNameOrErr.takeError());
1018 return false;
1019 }
1020 StringRef SectionName = SectionNameOrErr.get();
1021 return SectionName.starts_with(".debug") ||
1022 SectionName.starts_with(".zdebug") || SectionName == ".gdb_index";
1023}
1024
1025template <class ELFT>
1028 DataRefImpl RelData;
1029 auto SectionsOrErr = EF.sections();
1030 if (!SectionsOrErr)
1032 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
1033 RelData.d.a = (Sec.p - SHT) / EF.getHeader().e_shentsize;
1034 RelData.d.b = 0;
1035 if (reinterpret_cast<const Elf_Shdr *>(Sec.p)->sh_type == ELF::SHT_CREL) {
1036 if (RelData.d.a + 1 > Crels.size())
1037 Crels.resize(RelData.d.a + 1);
1038 auto &Crel = Crels[RelData.d.a];
1039 if (Crel.empty()) {
1040 ArrayRef<uint8_t> Content = cantFail(getSectionContents(Sec));
1041 size_t I = 0;
1042 Error Err = decodeCrel<ELFT::Is64Bits>(
1043 Content, [&](uint64_t Count, bool) { Crel.resize(Count); },
1044 [&](Elf_Crel Crel) { Crels[RelData.d.a][I++] = Crel; });
1045 if (Err) {
1046 Crel.assign(1, Elf_Crel{0, 0, 0, 0});
1047 if (RelData.d.a + 1 > CrelDecodeProblems.size())
1048 CrelDecodeProblems.resize(RelData.d.a + 1);
1049 CrelDecodeProblems[RelData.d.a] = toString(std::move(Err));
1050 }
1051 }
1052 }
1053 return relocation_iterator(RelocationRef(RelData, this));
1054}
1055
1056template <class ELFT>
1059 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1060 relocation_iterator Begin = section_rel_begin(Sec);
1061 DataRefImpl RelData = Begin->getRawDataRefImpl();
1062 if (S->sh_type == ELF::SHT_CREL) {
1063 RelData.d.b = Crels[RelData.d.a].size();
1064 return relocation_iterator(RelocationRef(RelData, this));
1065 }
1066 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
1067 return Begin;
1068 const Elf_Shdr *RelSec = getRelSection(RelData);
1069
1070 // Error check sh_link here so that getRelocationSymbol can just use it.
1071 auto SymSecOrErr = EF.getSection(RelSec->sh_link);
1072 if (!SymSecOrErr)
1074 Twine(errorToErrorCode(SymSecOrErr.takeError()).message()));
1075
1076 RelData.d.b += S->sh_size / S->sh_entsize;
1077 return relocation_iterator(RelocationRef(RelData, this));
1078}
1079
1080template <class ELFT>
1083 const Elf_Shdr *EShdr = getSection(Sec);
1084 uintX_t Type = EShdr->sh_type;
1086 return section_end();
1087
1088 Expected<const Elf_Shdr *> SecOrErr = EF.getSection(EShdr->sh_info);
1089 if (!SecOrErr)
1090 return SecOrErr.takeError();
1091 return section_iterator(SectionRef(toDRI(*SecOrErr), this));
1092}
1093
1094// Relocations
1095template <class ELFT>
1097 ++Rel.d.b;
1098}
1099
1100template <class ELFT>
1103 uint32_t symbolIdx;
1104 const Elf_Shdr *sec = getRelSection(Rel);
1105 if (sec->sh_type == ELF::SHT_CREL)
1106 symbolIdx = getCrel(Rel).r_symidx;
1107 else if (sec->sh_type == ELF::SHT_REL)
1108 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
1109 else
1110 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
1111 if (!symbolIdx)
1112 return symbol_end();
1113
1114 // FIXME: error check symbolIdx
1115 DataRefImpl SymbolData;
1116 SymbolData.d.a = sec->sh_link;
1117 SymbolData.d.b = symbolIdx;
1118 return symbol_iterator(SymbolRef(SymbolData, this));
1119}
1120
1121template <class ELFT>
1123 const Elf_Shdr *sec = getRelSection(Rel);
1124 if (sec->sh_type == ELF::SHT_CREL)
1125 return getCrel(Rel).r_offset;
1126 if (sec->sh_type == ELF::SHT_REL)
1127 return getRel(Rel)->r_offset;
1128
1129 return getRela(Rel)->r_offset;
1130}
1131
1132template <class ELFT>
1134 const Elf_Shdr *sec = getRelSection(Rel);
1135 if (sec->sh_type == ELF::SHT_CREL)
1136 return getCrel(Rel).r_type;
1137 if (sec->sh_type == ELF::SHT_REL)
1138 return getRel(Rel)->getType(EF.isMips64EL());
1139 else
1140 return getRela(Rel)->getType(EF.isMips64EL());
1141}
1142
1143template <class ELFT>
1145 return getELFRelocationTypeName(EF.getHeader().e_machine, Type);
1146}
1147
1148template <class ELFT>
1150 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1151 uint32_t type = getRelocationType(Rel);
1152 EF.getRelocationTypeName(type, Result);
1153}
1154
1155template <class ELFT>
1158 if (getRelSection(Rel)->sh_type == ELF::SHT_RELA)
1159 return (int64_t)getRela(Rel)->r_addend;
1160 if (getRelSection(Rel)->sh_type == ELF::SHT_CREL)
1161 return (int64_t)getCrel(Rel).r_addend;
1162 return createError("Relocation section does not have addends");
1163}
1164
1165template <class ELFT>
1166const typename ELFObjectFile<ELFT>::Elf_Rel *
1168 assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
1169 auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
1170 if (!Ret)
1171 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message()));
1172 return *Ret;
1173}
1174
1175template <class ELFT>
1176const typename ELFObjectFile<ELFT>::Elf_Rela *
1178 assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
1179 auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
1180 if (!Ret)
1181 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message()));
1182 return *Ret;
1183}
1184
1185template <class ELFT>
1188 assert(getRelSection(Crel)->sh_type == ELF::SHT_CREL);
1189 assert(Crel.d.a < Crels.size());
1190 return Crels[Crel.d.a][Crel.d.b];
1191}
1192
1193template <class ELFT>
1196 auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer());
1197 if (Error E = EFOrErr.takeError())
1198 return std::move(E);
1199
1200 ELFObjectFile<ELFT> Obj = {Object, std::move(*EFOrErr), nullptr, nullptr,
1201 nullptr};
1202 if (InitContent)
1203 if (Error E = Obj.initContent())
1204 return std::move(E);
1205 return std::move(Obj);
1206}
1207
1208template <class ELFT>
1210 const Elf_Shdr *DotDynSymSec,
1211 const Elf_Shdr *DotSymtabSec,
1212 const Elf_Shdr *DotSymtabShndx)
1213 : ELFObjectFileBase(getELFType(ELFT::Endianness == llvm::endianness::little,
1214 ELFT::Is64Bits),
1215 Object),
1216 EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
1217 DotSymtabShndxSec(DotSymtabShndx) {}
1218
1219template <class ELFT>
1221 : ELFObjectFile(Other.Data, Other.EF, Other.DotDynSymSec,
1222 Other.DotSymtabSec, Other.DotSymtabShndxSec) {}
1223
1224template <class ELFT>
1227 toDRI(DotSymtabSec,
1228 DotSymtabSec && DotSymtabSec->sh_size >= sizeof(Elf_Sym) ? 1 : 0);
1229 return basic_symbol_iterator(SymbolRef(Sym, this));
1230}
1231
1232template <class ELFT>
1234 const Elf_Shdr *SymTab = DotSymtabSec;
1235 if (!SymTab)
1236 return symbol_begin();
1237 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1238 return basic_symbol_iterator(SymbolRef(Sym, this));
1239}
1240
1241template <class ELFT>
1243 if (!DotDynSymSec || DotDynSymSec->sh_size < sizeof(Elf_Sym))
1244 // Ignore errors here where the dynsym is empty or sh_size less than the
1245 // size of one symbol. These should be handled elsewhere.
1246 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 0), this));
1247 // Skip 0-index NULL symbol.
1248 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 1), this));
1249}
1250
1251template <class ELFT>
1253 const Elf_Shdr *SymTab = DotDynSymSec;
1254 if (!SymTab)
1255 return dynamic_symbol_begin();
1256 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1257 return basic_symbol_iterator(SymbolRef(Sym, this));
1258}
1259
1260template <class ELFT>
1262 auto SectionsOrErr = EF.sections();
1263 if (!SectionsOrErr)
1264 return section_iterator(SectionRef());
1265 return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this));
1266}
1267
1268template <class ELFT>
1270 auto SectionsOrErr = EF.sections();
1271 if (!SectionsOrErr)
1272 return section_iterator(SectionRef());
1273 return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
1274}
1275
1276template <class ELFT>
1278 return ELFT::Is64Bits ? 8 : 4;
1279}
1280
1281template <class ELFT>
1283 constexpr bool IsLittleEndian = ELFT::Endianness == llvm::endianness::little;
1284 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1285 case ELF::ELFCLASS32:
1286 switch (EF.getHeader().e_machine) {
1287 case ELF::EM_68K:
1288 return "elf32-m68k";
1289 case ELF::EM_386:
1290 return "elf32-i386";
1291 case ELF::EM_IAMCU:
1292 return "elf32-iamcu";
1293 case ELF::EM_X86_64:
1294 return "elf32-x86-64";
1295 case ELF::EM_ARM:
1296 return (IsLittleEndian ? "elf32-littlearm" : "elf32-bigarm");
1297 case ELF::EM_AVR:
1298 return "elf32-avr";
1299 case ELF::EM_HEXAGON:
1300 return "elf32-hexagon";
1301 case ELF::EM_LANAI:
1302 return "elf32-lanai";
1303 case ELF::EM_MIPS:
1304 return "elf32-mips";
1305 case ELF::EM_MSP430:
1306 return "elf32-msp430";
1307 case ELF::EM_PPC:
1308 return (IsLittleEndian ? "elf32-powerpcle" : "elf32-powerpc");
1309 case ELF::EM_RISCV:
1310 return "elf32-littleriscv";
1311 case ELF::EM_CSKY:
1312 return "elf32-csky";
1313 case ELF::EM_SPARC:
1315 return "elf32-sparc";
1316 case ELF::EM_AMDGPU:
1317 return "elf32-amdgpu";
1318 case ELF::EM_LOONGARCH:
1319 return "elf32-loongarch";
1320 case ELF::EM_XTENSA:
1321 return "elf32-xtensa";
1322 default:
1323 return "elf32-unknown";
1324 }
1325 case ELF::ELFCLASS64:
1326 switch (EF.getHeader().e_machine) {
1327 case ELF::EM_386:
1328 return "elf64-i386";
1329 case ELF::EM_X86_64:
1330 return "elf64-x86-64";
1331 case ELF::EM_AARCH64:
1332 return (IsLittleEndian ? "elf64-littleaarch64" : "elf64-bigaarch64");
1333 case ELF::EM_PPC64:
1334 return (IsLittleEndian ? "elf64-powerpcle" : "elf64-powerpc");
1335 case ELF::EM_RISCV:
1336 return "elf64-littleriscv";
1337 case ELF::EM_S390:
1338 return "elf64-s390";
1339 case ELF::EM_SPARCV9:
1340 return "elf64-sparc";
1341 case ELF::EM_MIPS:
1342 return "elf64-mips";
1343 case ELF::EM_AMDGPU:
1344 return "elf64-amdgpu";
1345 case ELF::EM_BPF:
1346 return "elf64-bpf";
1347 case ELF::EM_VE:
1348 return "elf64-ve";
1349 case ELF::EM_LOONGARCH:
1350 return "elf64-loongarch";
1351 default:
1352 return "elf64-unknown";
1353 }
1354 default:
1355 // FIXME: Proper error handling.
1356 report_fatal_error("Invalid ELFCLASS!");
1357 }
1358}
1359
1360template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
1361 bool IsLittleEndian = ELFT::Endianness == llvm::endianness::little;
1362 switch (EF.getHeader().e_machine) {
1363 case ELF::EM_68K:
1364 return Triple::m68k;
1365 case ELF::EM_386:
1366 case ELF::EM_IAMCU:
1367 return Triple::x86;
1368 case ELF::EM_X86_64:
1369 return Triple::x86_64;
1370 case ELF::EM_AARCH64:
1371 return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be;
1372 case ELF::EM_ARM:
1373 return Triple::arm;
1374 case ELF::EM_AVR:
1375 return Triple::avr;
1376 case ELF::EM_HEXAGON:
1377 return Triple::hexagon;
1378 case ELF::EM_LANAI:
1379 return Triple::lanai;
1380 case ELF::EM_MIPS:
1381 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1382 case ELF::ELFCLASS32:
1383 return IsLittleEndian ? Triple::mipsel : Triple::mips;
1384 case ELF::ELFCLASS64:
1385 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
1386 default:
1387 report_fatal_error("Invalid ELFCLASS!");
1388 }
1389 case ELF::EM_MSP430:
1390 return Triple::msp430;
1391 case ELF::EM_PPC:
1392 return IsLittleEndian ? Triple::ppcle : Triple::ppc;
1393 case ELF::EM_PPC64:
1394 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
1395 case ELF::EM_RISCV:
1396 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1397 case ELF::ELFCLASS32:
1398 return Triple::riscv32;
1399 case ELF::ELFCLASS64:
1400 return Triple::riscv64;
1401 default:
1402 report_fatal_error("Invalid ELFCLASS!");
1403 }
1404 case ELF::EM_S390:
1405 return Triple::systemz;
1406
1407 case ELF::EM_SPARC:
1409 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
1410 case ELF::EM_SPARCV9:
1411 return Triple::sparcv9;
1412
1413 case ELF::EM_AMDGPU: {
1414 if (!IsLittleEndian)
1415 return Triple::UnknownArch;
1416
1417 unsigned MACH = EF.getHeader().e_flags & ELF::EF_AMDGPU_MACH;
1418 if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST &&
1420 return Triple::r600;
1423 return Triple::amdgcn;
1424
1425 return Triple::UnknownArch;
1426 }
1427
1428 case ELF::EM_CUDA: {
1429 if (EF.getHeader().e_ident[ELF::EI_CLASS] == ELF::ELFCLASS32)
1430 return Triple::nvptx;
1431 return Triple::nvptx64;
1432 }
1433
1434 case ELF::EM_BPF:
1435 return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
1436
1437 case ELF::EM_VE:
1438 return Triple::ve;
1439 case ELF::EM_CSKY:
1440 return Triple::csky;
1441
1442 case ELF::EM_LOONGARCH:
1443 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1444 case ELF::ELFCLASS32:
1445 return Triple::loongarch32;
1446 case ELF::ELFCLASS64:
1447 return Triple::loongarch64;
1448 default:
1449 report_fatal_error("Invalid ELFCLASS!");
1450 }
1451
1452 case ELF::EM_XTENSA:
1453 return Triple::xtensa;
1454
1455 default:
1456 return Triple::UnknownArch;
1457 }
1458}
1459
1460template <class ELFT> Triple::OSType ELFObjectFile<ELFT>::getOS() const {
1461 switch (EF.getHeader().e_ident[ELF::EI_OSABI]) {
1463 return Triple::NetBSD;
1465 return Triple::Linux;
1466 case ELF::ELFOSABI_HURD:
1467 return Triple::Hurd;
1469 return Triple::Solaris;
1470 case ELF::ELFOSABI_AIX:
1471 return Triple::AIX;
1473 return Triple::FreeBSD;
1475 return Triple::OpenBSD;
1476 case ELF::ELFOSABI_CUDA:
1477 return Triple::CUDA;
1479 return Triple::AMDHSA;
1481 return Triple::AMDPAL;
1483 return Triple::Mesa3D;
1484 default:
1485 return Triple::UnknownOS;
1486 }
1487}
1488
1489template <class ELFT>
1491 return EF.getHeader().e_entry;
1492}
1493
1494template <class ELFT>
1497 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
1498}
1499
1500template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
1501 return EF.getHeader().e_type == ELF::ET_REL;
1502}
1503
1504template <class ELFT>
1506 uintptr_t SHT = reinterpret_cast<uintptr_t>(cantFail(EF.sections()).begin());
1507 auto I = (Sec.p - SHT) / EF.getHeader().e_shentsize;
1508 if (I < CrelDecodeProblems.size())
1509 return CrelDecodeProblems[I];
1510 return "";
1511}
1512
1513} // end namespace object
1514} // end namespace llvm
1515
1516#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:586
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
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.
@ EI_ABIVERSION
Definition: ELF.h:57
@ EI_CLASS
Definition: ELF.h:53
@ EI_OSABI
Definition: ELF.h:56
@ ET_REL
Definition: ELF.h:117
@ 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
@ 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
@ SHN_ABS
Definition: ELF.h:1059
@ SHN_COMMON
Definition: ELF.h:1060
@ SHN_UNDEF
Definition: ELF.h:1053
@ EF_AMDGPU_MACH_AMDGCN_LAST
Definition: ELF.h:808
@ EF_AMDGPU_MACH_R600_LAST
Definition: ELF.h:742
@ EF_AMDGPU_MACH_AMDGCN_FIRST
Definition: ELF.h:807
@ EF_AMDGPU_MACH
Definition: ELF.h:708
@ EF_AMDGPU_MACH_R600_FIRST
Definition: ELF.h:741
@ STT_FUNC
Definition: ELF.h:1332
@ STT_NOTYPE
Definition: ELF.h:1330
@ STT_SECTION
Definition: ELF.h:1333
@ STT_FILE
Definition: ELF.h:1334
@ STT_COMMON
Definition: ELF.h:1335
@ STT_GNU_IFUNC
Definition: ELF.h:1337
@ STT_OBJECT
Definition: ELF.h:1331
@ STT_TLS
Definition: ELF.h:1336
@ STV_HIDDEN
Definition: ELF.h:1350
@ STV_PROTECTED
Definition: ELF.h:1351
@ STV_DEFAULT
Definition: ELF.h:1348
@ ELFCLASS64
Definition: ELF.h:330
@ ELFCLASS32
Definition: ELF.h:329
@ SHT_PROGBITS
Definition: ELF.h:1068
@ SHT_REL
Definition: ELF.h:1076
@ SHT_ARM_ATTRIBUTES
Definition: ELF.h:1127
@ SHT_NOBITS
Definition: ELF.h:1075
@ SHT_SYMTAB
Definition: ELF.h:1069
@ SHT_HEXAGON_ATTRIBUTES
Definition: ELF.h:1152
@ SHT_CREL
Definition: ELF.h:1089
@ SHT_DYNAMIC
Definition: ELF.h:1073
@ SHT_SYMTAB_SHNDX
Definition: ELF.h:1083
@ SHT_RISCV_ATTRIBUTES
Definition: ELF.h:1148
@ SHT_RELA
Definition: ELF.h:1071
@ SHT_DYNSYM
Definition: ELF.h:1078
@ STB_GLOBAL
Definition: ELF.h:1319
@ STB_LOCAL
Definition: ELF.h:1318
@ STB_GNU_UNIQUE
Definition: ELF.h:1321
@ STB_WEAK
Definition: ELF.h:1320
@ SHF_ALLOC
Definition: ELF.h:1165
@ SHF_COMPRESSED
Definition: ELF.h:1193
@ SHF_WRITE
Definition: ELF.h:1162
@ SHF_EXECINSTR
Definition: ELF.h:1168
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::@362 d