LLVM 18.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/Endian.h"
31#include "llvm/Support/Error.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 Expected<SubtargetFeatures> getRISCVFeatures() const;
65 SubtargetFeatures getLoongArchFeatures() const;
66
67 StringRef getAMDGPUCPUName() const;
68
69protected:
70 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
71
72 virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
73 virtual uint8_t getSymbolBinding(DataRefImpl Symb) const = 0;
74 virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
75 virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
76
77 virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
78 virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
79 virtual uint64_t getSectionOffset(DataRefImpl Sec) const = 0;
80
82 virtual Error getBuildAttributes(ELFAttributeParser &Attributes) const = 0;
83
84public:
86
88
89 /// Returns platform-specific object flags, if any.
90 virtual unsigned getPlatformFlags() const = 0;
91
93
94 static bool classof(const Binary *v) { return v->isELF(); }
95
97
98 std::optional<StringRef> tryGetCPUName() const override;
99
100 void setARMSubArch(Triple &TheTriple) const override;
101
102 virtual uint16_t getEType() const = 0;
103
104 virtual uint16_t getEMachine() const = 0;
105
106 std::vector<ELFPltEntry> getPltEntries() const;
107
108 /// Returns a vector containing a symbol version for each dynamic symbol.
109 /// Returns an empty vector if version sections do not exist.
111
112 /// Returns a vector of all BB address maps in the object file. When
113 // `TextSectionIndex` is specified, only returns the BB address maps
114 // corresponding to the section with that index.
116 readBBAddrMap(std::optional<unsigned> TextSectionIndex = std::nullopt) const;
117};
118
119class ELFSectionRef : public SectionRef {
120public:
122 assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
123 }
124
126 return cast<ELFObjectFileBase>(SectionRef::getObject());
127 }
128
131 }
132
135 }
136
139 }
140};
141
143public:
145 assert(isa<ELFObjectFileBase>(B->getObject()));
146 }
147
148 const ELFSectionRef *operator->() const {
149 return static_cast<const ELFSectionRef *>(section_iterator::operator->());
150 }
151
152 const ELFSectionRef &operator*() const {
153 return static_cast<const ELFSectionRef &>(section_iterator::operator*());
154 }
155};
156
157class ELFSymbolRef : public SymbolRef {
158public:
160 assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
161 }
162
164 return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
165 }
166
169 }
170
171 uint8_t getBinding() const {
173 }
174
175 uint8_t getOther() const {
177 }
178
179 uint8_t getELFType() const {
181 }
182
184 uint8_t Type = getELFType();
185 for (const auto &EE : ElfSymbolTypes) {
186 if (EE.Value == Type) {
187 return EE.AltName;
188 }
189 }
190 return "";
191 }
192};
193
195public:
197 : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
199
200 const ELFSymbolRef *operator->() const {
201 return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
202 }
203
204 const ELFSymbolRef &operator*() const {
205 return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
206 }
207};
208
210public:
212 assert(isa<ELFObjectFileBase>(RelocationRef::getObject()));
213 }
214
216 return cast<ELFObjectFileBase>(RelocationRef::getObject());
217 }
218
221 }
222};
223
225public:
228 B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {}
229
231 return static_cast<const ELFRelocationRef *>(
233 }
234
236 return static_cast<const ELFRelocationRef &>(
238 }
239};
240
244}
245
246template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
247 uint16_t getEMachine() const override;
248 uint16_t getEType() const override;
249 uint64_t getSymbolSize(DataRefImpl Sym) const override;
250
251public:
253
254 SectionRef toSectionRef(const Elf_Shdr *Sec) const {
255 return SectionRef(toDRI(Sec), this);
256 }
257
258 ELFSymbolRef toSymbolRef(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
259 return ELFSymbolRef({toDRI(SymTable, SymbolNum), this});
260 }
261
262 bool IsContentValid() const { return ContentValid; }
263
264private:
266 const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,
267 const Elf_Shdr *DotSymtabShndxSec);
268
269 bool ContentValid = false;
270
271protected:
273
274 const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
275 const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
276 const Elf_Shdr *DotSymtabShndxSec = nullptr; // SHT_SYMTAB_SHNDX section.
277
278 Error initContent() override;
279
280 void moveSymbolNext(DataRefImpl &Symb) const override;
281 Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
283 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
284 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
285 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
286 Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override;
287 uint8_t getSymbolBinding(DataRefImpl Symb) const override;
288 uint8_t getSymbolOther(DataRefImpl Symb) const override;
289 uint8_t getSymbolELFType(DataRefImpl Symb) const override;
292 const Elf_Shdr *SymTab) const;
294
295 void moveSectionNext(DataRefImpl &Sec) const override;
297 uint64_t getSectionAddress(DataRefImpl Sec) const override;
298 uint64_t getSectionIndex(DataRefImpl Sec) const override;
299 uint64_t getSectionSize(DataRefImpl Sec) const override;
301 getSectionContents(DataRefImpl Sec) const override;
302 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
303 bool isSectionCompressed(DataRefImpl Sec) const override;
304 bool isSectionText(DataRefImpl Sec) const override;
305 bool isSectionData(DataRefImpl Sec) const override;
306 bool isSectionBSS(DataRefImpl Sec) const override;
307 bool isSectionVirtual(DataRefImpl Sec) const override;
308 bool isBerkeleyText(DataRefImpl Sec) const override;
309 bool isBerkeleyData(DataRefImpl Sec) const override;
310 bool isDebugSection(DataRefImpl Sec) const override;
313 std::vector<SectionRef> dynamic_relocation_sections() const override;
315 getRelocatedSection(DataRefImpl Sec) const override;
316
317 void moveRelocationNext(DataRefImpl &Rel) const override;
318 uint64_t getRelocationOffset(DataRefImpl Rel) const override;
320 uint64_t getRelocationType(DataRefImpl Rel) const override;
322 SmallVectorImpl<char> &Result) const override;
323
324 uint32_t getSectionType(DataRefImpl Sec) const override;
325 uint64_t getSectionFlags(DataRefImpl Sec) const override;
326 uint64_t getSectionOffset(DataRefImpl Sec) const override;
328
329 DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
330 DataRefImpl DRI;
331 if (!SymTable) {
332 DRI.d.a = 0;
333 DRI.d.b = 0;
334 return DRI;
335 }
336 assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
337 SymTable->sh_type == ELF::SHT_DYNSYM);
338
339 auto SectionsOrErr = EF.sections();
340 if (!SectionsOrErr) {
341 DRI.d.a = 0;
342 DRI.d.b = 0;
343 return DRI;
344 }
345 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
346 unsigned SymTableIndex =
347 (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
348
349 DRI.d.a = SymTableIndex;
350 DRI.d.b = SymbolNum;
351 return DRI;
352 }
353
354 const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
355 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
356 }
357
358 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
359 DataRefImpl DRI;
360 DRI.p = reinterpret_cast<uintptr_t>(Sec);
361 return DRI;
362 }
363
364 DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
365 DataRefImpl DRI;
366 DRI.p = reinterpret_cast<uintptr_t>(Dyn);
367 return DRI;
368 }
369
370 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
371 unsigned char Binding = ESym->getBinding();
372 unsigned char Visibility = ESym->getVisibility();
373
374 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
375 // visibility is either DEFAULT or PROTECTED. All other symbols are not
376 // exported.
377 return (
378 (Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK ||
379 Binding == ELF::STB_GNU_UNIQUE) &&
380 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED));
381 }
382
383 Error getBuildAttributes(ELFAttributeParser &Attributes) const override {
384 auto SectionsOrErr = EF.sections();
385 if (!SectionsOrErr)
386 return SectionsOrErr.takeError();
387
388 for (const Elf_Shdr &Sec : *SectionsOrErr) {
389 if (Sec.sh_type == ELF::SHT_ARM_ATTRIBUTES ||
390 Sec.sh_type == ELF::SHT_RISCV_ATTRIBUTES) {
391 auto ErrorOrContents = EF.getSectionContents(Sec);
392 if (!ErrorOrContents)
393 return ErrorOrContents.takeError();
394
395 auto Contents = ErrorOrContents.get();
396 if (Contents[0] != ELFAttrs::Format_Version || Contents.size() == 1)
397 return Error::success();
398
399 if (Error E = Attributes.parse(Contents, ELFT::TargetEndianness))
400 return E;
401 break;
402 }
403 }
404 return Error::success();
405 }
406
407 // This flag is used for classof, to distinguish ELFObjectFile from
408 // its subclass. If more subclasses will be created, this flag will
409 // have to become an enum.
410 bool isDyldELFObject = false;
411
412public:
415 bool InitContent = true);
416
417 const Elf_Rel *getRel(DataRefImpl Rel) const;
418 const Elf_Rela *getRela(DataRefImpl Rela) const;
419
421 return EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
422 }
423
424 /// Get the relocation section that contains \a Rel.
425 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
426 auto RelSecOrErr = EF.getSection(Rel.d.a);
427 if (!RelSecOrErr)
429 Twine(errorToErrorCode(RelSecOrErr.takeError()).message()));
430 return *RelSecOrErr;
431 }
432
433 const Elf_Shdr *getSection(DataRefImpl Sec) const {
434 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
435 }
436
437 basic_symbol_iterator symbol_begin() const override;
438 basic_symbol_iterator symbol_end() const override;
439
440 bool is64Bit() const override { return getBytesInAddress() == 8; }
441
444
445 section_iterator section_begin() const override;
446 section_iterator section_end() const override;
447
449
450 uint8_t getBytesInAddress() const override;
451 StringRef getFileFormatName() const override;
452 Triple::ArchType getArch() const override;
453 Expected<uint64_t> getStartAddress() const override;
454
455 unsigned getPlatformFlags() const override { return EF.getHeader().e_flags; }
456
457 const ELFFile<ELFT> &getELFFile() const { return EF; }
458
459 bool isDyldType() const { return isDyldELFObject; }
460 static bool classof(const Binary *v) {
461 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
462 ELFT::Is64Bits);
463 }
464
466
467 bool isRelocatableObject() const override;
468
469 void createFakeSections() { EF.createFakeSections(); }
470};
471
476
477template <class ELFT>
479 ++Sym.d.b;
480}
481
483 auto SectionsOrErr = EF.sections();
484 if (!SectionsOrErr)
485 return SectionsOrErr.takeError();
486
487 for (const Elf_Shdr &Sec : *SectionsOrErr) {
488 switch (Sec.sh_type) {
489 case ELF::SHT_DYNSYM: {
490 if (!DotDynSymSec)
491 DotDynSymSec = &Sec;
492 break;
493 }
494 case ELF::SHT_SYMTAB: {
495 if (!DotSymtabSec)
496 DotSymtabSec = &Sec;
497 break;
498 }
500 if (!DotSymtabShndxSec)
501 DotSymtabShndxSec = &Sec;
502 break;
503 }
504 }
505 }
506
507 ContentValid = true;
508 return Error::success();
509}
510
511template <class ELFT>
513 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
514 if (!SymOrErr)
515 return SymOrErr.takeError();
516 auto SymTabOrErr = EF.getSection(Sym.d.a);
517 if (!SymTabOrErr)
518 return SymTabOrErr.takeError();
519 const Elf_Shdr *SymTableSec = *SymTabOrErr;
520 auto StrTabOrErr = EF.getSection(SymTableSec->sh_link);
521 if (!StrTabOrErr)
522 return StrTabOrErr.takeError();
523 const Elf_Shdr *StringTableSec = *StrTabOrErr;
524 auto SymStrTabOrErr = EF.getStringTable(*StringTableSec);
525 if (!SymStrTabOrErr)
526 return SymStrTabOrErr.takeError();
527 Expected<StringRef> Name = (*SymOrErr)->getName(*SymStrTabOrErr);
528 if (Name && !Name->empty())
529 return Name;
530
531 // If the symbol name is empty use the section name.
532 if ((*SymOrErr)->getType() == ELF::STT_SECTION) {
533 Expected<section_iterator> SecOrErr = getSymbolSection(Sym);
534 if (SecOrErr)
535 return (*SecOrErr)->getName();
536 return SecOrErr.takeError();
537 }
538 return Name;
539}
540
541template <class ELFT>
543 return getSection(Sec)->sh_flags;
544}
545
546template <class ELFT>
548 return getSection(Sec)->sh_type;
549}
550
551template <class ELFT>
553 return getSection(Sec)->sh_offset;
554}
555
556template <class ELFT>
558 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
559 if (!SymOrErr)
560 report_fatal_error(SymOrErr.takeError());
561
562 uint64_t Ret = (*SymOrErr)->st_value;
563 if ((*SymOrErr)->st_shndx == ELF::SHN_ABS)
564 return Ret;
565
566 const Elf_Ehdr &Header = EF.getHeader();
567 // Clear the ARM/Thumb or microMIPS indicator flag.
568 if ((Header.e_machine == ELF::EM_ARM || Header.e_machine == ELF::EM_MIPS) &&
569 (*SymOrErr)->getType() == ELF::STT_FUNC)
570 Ret &= ~1;
571
572 return Ret;
573}
574
575template <class ELFT>
578 Expected<uint64_t> SymbolValueOrErr = getSymbolValue(Symb);
579 if (!SymbolValueOrErr)
580 // TODO: Test this error.
581 return SymbolValueOrErr.takeError();
582
583 uint64_t Result = *SymbolValueOrErr;
584 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
585 if (!SymOrErr)
586 return SymOrErr.takeError();
587
588 switch ((*SymOrErr)->st_shndx) {
589 case ELF::SHN_COMMON:
590 case ELF::SHN_UNDEF:
591 case ELF::SHN_ABS:
592 return Result;
593 }
594
595 auto SymTabOrErr = EF.getSection(Symb.d.a);
596 if (!SymTabOrErr)
597 return SymTabOrErr.takeError();
598
599 if (EF.getHeader().e_type == ELF::ET_REL) {
600 ArrayRef<Elf_Word> ShndxTable;
601 if (DotSymtabShndxSec) {
602 // TODO: Test this error.
603 if (Expected<ArrayRef<Elf_Word>> ShndxTableOrErr =
604 EF.getSHNDXTable(*DotSymtabShndxSec))
605 ShndxTable = *ShndxTableOrErr;
606 else
607 return ShndxTableOrErr.takeError();
608 }
609
610 Expected<const Elf_Shdr *> SectionOrErr =
611 EF.getSection(**SymOrErr, *SymTabOrErr, ShndxTable);
612 if (!SectionOrErr)
613 return SectionOrErr.takeError();
614 const Elf_Shdr *Section = *SectionOrErr;
615 if (Section)
616 Result += Section->sh_addr;
617 }
618
619 return Result;
620}
621
622template <class ELFT>
624 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
625 if (!SymOrErr)
626 report_fatal_error(SymOrErr.takeError());
627 if ((*SymOrErr)->st_shndx == ELF::SHN_COMMON)
628 return (*SymOrErr)->st_value;
629 return 0;
630}
631
632template <class ELFT>
634 return EF.getHeader().e_machine;
635}
636
637template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const {
638 return EF.getHeader().e_type;
639}
640
641template <class ELFT>
642uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
643 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
644 if (!SymOrErr)
645 report_fatal_error(SymOrErr.takeError());
646 return (*SymOrErr)->st_size;
647}
648
649template <class ELFT>
651 return getSymbolSize(Symb);
652}
653
654template <class ELFT>
656 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
657 if (!SymOrErr)
658 report_fatal_error(SymOrErr.takeError());
659 return (*SymOrErr)->getBinding();
660}
661
662template <class ELFT>
664 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
665 if (!SymOrErr)
666 report_fatal_error(SymOrErr.takeError());
667 return (*SymOrErr)->st_other;
668}
669
670template <class ELFT>
672 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
673 if (!SymOrErr)
674 report_fatal_error(SymOrErr.takeError());
675 return (*SymOrErr)->getType();
676}
677
678template <class ELFT>
681 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
682 if (!SymOrErr)
683 return SymOrErr.takeError();
684
685 switch ((*SymOrErr)->getType()) {
686 case ELF::STT_NOTYPE:
688 case ELF::STT_SECTION:
689 return SymbolRef::ST_Debug;
690 case ELF::STT_FILE:
691 return SymbolRef::ST_File;
692 case ELF::STT_FUNC:
694 case ELF::STT_OBJECT:
695 case ELF::STT_COMMON:
696 return SymbolRef::ST_Data;
697 case ELF::STT_TLS:
698 default:
699 return SymbolRef::ST_Other;
700 }
701}
702
703template <class ELFT>
705 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
706 if (!SymOrErr)
707 return SymOrErr.takeError();
708
709 const Elf_Sym *ESym = *SymOrErr;
711
712 if (ESym->getBinding() != ELF::STB_LOCAL)
713 Result |= SymbolRef::SF_Global;
714
715 if (ESym->getBinding() == ELF::STB_WEAK)
716 Result |= SymbolRef::SF_Weak;
717
718 if (ESym->st_shndx == ELF::SHN_ABS)
719 Result |= SymbolRef::SF_Absolute;
720
721 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION)
723
724 if (Expected<typename ELFT::SymRange> SymbolsOrErr =
725 EF.symbols(DotSymtabSec)) {
726 // Set the SF_FormatSpecific flag for the 0-index null symbol.
727 if (ESym == SymbolsOrErr->begin())
729 } else
730 // TODO: Test this error.
731 return SymbolsOrErr.takeError();
732
733 if (Expected<typename ELFT::SymRange> SymbolsOrErr =
734 EF.symbols(DotDynSymSec)) {
735 // Set the SF_FormatSpecific flag for the 0-index null symbol.
736 if (ESym == SymbolsOrErr->begin())
738 } else
739 // TODO: Test this error.
740 return SymbolsOrErr.takeError();
741
742 if (EF.getHeader().e_machine == ELF::EM_AARCH64) {
743 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
744 StringRef Name = *NameOrErr;
745 if (Name.startswith("$d") || Name.startswith("$x"))
747 } else {
748 // TODO: Actually report errors helpfully.
749 consumeError(NameOrErr.takeError());
750 }
751 } else if (EF.getHeader().e_machine == ELF::EM_ARM) {
752 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
753 StringRef Name = *NameOrErr;
754 // TODO Investigate why empty name symbols need to be marked.
755 if (Name.empty() || Name.startswith("$d") || Name.startswith("$t") ||
756 Name.startswith("$a"))
758 } else {
759 // TODO: Actually report errors helpfully.
760 consumeError(NameOrErr.takeError());
761 }
762 if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1)
763 Result |= SymbolRef::SF_Thumb;
764 } else if (EF.getHeader().e_machine == ELF::EM_CSKY) {
765 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
766 StringRef Name = *NameOrErr;
767 if (Name.startswith("$d") || Name.startswith("$t"))
769 } else {
770 // TODO: Actually report errors helpfully.
771 consumeError(NameOrErr.takeError());
772 }
773 } else if (EF.getHeader().e_machine == ELF::EM_RISCV) {
774 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
775 StringRef Name = *NameOrErr;
776 // Mark empty name symbols (used for label differences) and mapping
777 // symbols.
778 if (Name.empty() || Name.startswith("$d") || Name.startswith("$x"))
780 } else {
781 // TODO: Actually report errors helpfully.
782 consumeError(NameOrErr.takeError());
783 }
784 }
785
786 if (ESym->st_shndx == ELF::SHN_UNDEF)
787 Result |= SymbolRef::SF_Undefined;
788
789 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
790 Result |= SymbolRef::SF_Common;
791
792 if (isExportedToOtherDSO(ESym))
793 Result |= SymbolRef::SF_Exported;
794
795 if (ESym->getType() == ELF::STT_GNU_IFUNC)
796 Result |= SymbolRef::SF_Indirect;
797
798 if (ESym->getVisibility() == ELF::STV_HIDDEN)
799 Result |= SymbolRef::SF_Hidden;
800
801 return Result;
802}
803
804template <class ELFT>
807 const Elf_Shdr *SymTab) const {
808 ArrayRef<Elf_Word> ShndxTable;
809 if (DotSymtabShndxSec) {
810 // TODO: Test this error.
811 Expected<ArrayRef<Elf_Word>> ShndxTableOrErr =
812 EF.getSHNDXTable(*DotSymtabShndxSec);
813 if (!ShndxTableOrErr)
814 return ShndxTableOrErr.takeError();
815 ShndxTable = *ShndxTableOrErr;
816 }
817
818 auto ESecOrErr = EF.getSection(*ESym, SymTab, ShndxTable);
819 if (!ESecOrErr)
820 return ESecOrErr.takeError();
821
822 const Elf_Shdr *ESec = *ESecOrErr;
823 if (!ESec)
824 return section_end();
825
826 DataRefImpl Sec;
827 Sec.p = reinterpret_cast<intptr_t>(ESec);
828 return section_iterator(SectionRef(Sec, this));
829}
830
831template <class ELFT>
834 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
835 if (!SymOrErr)
836 return SymOrErr.takeError();
837
838 auto SymTabOrErr = EF.getSection(Symb.d.a);
839 if (!SymTabOrErr)
840 return SymTabOrErr.takeError();
841 return getSymbolSection(*SymOrErr, *SymTabOrErr);
842}
843
844template <class ELFT>
846 const Elf_Shdr *ESec = getSection(Sec);
847 Sec = toDRI(++ESec);
848}
849
850template <class ELFT>
852 return EF.getSectionName(*getSection(Sec));
853}
854
855template <class ELFT>
857 return getSection(Sec)->sh_addr;
858}
859
860template <class ELFT>
862 auto SectionsOrErr = EF.sections();
863 handleAllErrors(std::move(SectionsOrErr.takeError()),
864 [](const ErrorInfoBase &) {
865 llvm_unreachable("unable to get section index");
866 });
867 const Elf_Shdr *First = SectionsOrErr->begin();
868 return getSection(Sec) - First;
869}
870
871template <class ELFT>
873 return getSection(Sec)->sh_size;
874}
875
876template <class ELFT>
879 const Elf_Shdr *EShdr = getSection(Sec);
880 if (EShdr->sh_type == ELF::SHT_NOBITS)
881 return ArrayRef((const uint8_t *)base(), (size_t)0);
882 if (Error E =
883 checkOffset(getMemoryBufferRef(),
884 (uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
885 return std::move(E);
886 return ArrayRef((const uint8_t *)base() + EShdr->sh_offset, EShdr->sh_size);
887}
888
889template <class ELFT>
891 return getSection(Sec)->sh_addralign;
892}
893
894template <class ELFT>
896 return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED;
897}
898
899template <class ELFT>
901 return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR;
902}
903
904template <class ELFT>
906 const Elf_Shdr *EShdr = getSection(Sec);
907 return EShdr->sh_type == ELF::SHT_PROGBITS &&
908 EShdr->sh_flags & ELF::SHF_ALLOC &&
909 !(EShdr->sh_flags & ELF::SHF_EXECINSTR);
910}
911
912template <class ELFT>
914 const Elf_Shdr *EShdr = getSection(Sec);
915 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
916 EShdr->sh_type == ELF::SHT_NOBITS;
917}
918
919template <class ELFT>
920std::vector<SectionRef>
922 std::vector<SectionRef> Res;
923 std::vector<uintptr_t> Offsets;
924
925 auto SectionsOrErr = EF.sections();
926 if (!SectionsOrErr)
927 return Res;
928
929 for (const Elf_Shdr &Sec : *SectionsOrErr) {
930 if (Sec.sh_type != ELF::SHT_DYNAMIC)
931 continue;
932 Elf_Dyn *Dynamic =
933 reinterpret_cast<Elf_Dyn *>((uintptr_t)base() + Sec.sh_offset);
934 for (; Dynamic->d_tag != ELF::DT_NULL; Dynamic++) {
935 if (Dynamic->d_tag == ELF::DT_REL || Dynamic->d_tag == ELF::DT_RELA ||
936 Dynamic->d_tag == ELF::DT_JMPREL) {
937 Offsets.push_back(Dynamic->d_un.d_val);
938 }
939 }
940 }
941 for (const Elf_Shdr &Sec : *SectionsOrErr) {
942 if (is_contained(Offsets, Sec.sh_addr))
943 Res.emplace_back(toDRI(&Sec), this);
944 }
945 return Res;
946}
947
948template <class ELFT>
950 return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
951}
952
953template <class ELFT>
955 return getSection(Sec)->sh_flags & ELF::SHF_ALLOC &&
956 (getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR ||
957 !(getSection(Sec)->sh_flags & ELF::SHF_WRITE));
958}
959
960template <class ELFT>
962 const Elf_Shdr *EShdr = getSection(Sec);
963 return !isBerkeleyText(Sec) && EShdr->sh_type != ELF::SHT_NOBITS &&
964 EShdr->sh_flags & ELF::SHF_ALLOC;
965}
966
967template <class ELFT>
969 Expected<StringRef> SectionNameOrErr = getSectionName(Sec);
970 if (!SectionNameOrErr) {
971 // TODO: Report the error message properly.
972 consumeError(SectionNameOrErr.takeError());
973 return false;
974 }
975 StringRef SectionName = SectionNameOrErr.get();
976 return SectionName.startswith(".debug") ||
977 SectionName.startswith(".zdebug") || SectionName == ".gdb_index";
978}
979
980template <class ELFT>
983 DataRefImpl RelData;
984 auto SectionsOrErr = EF.sections();
985 if (!SectionsOrErr)
987 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
988 RelData.d.a = (Sec.p - SHT) / EF.getHeader().e_shentsize;
989 RelData.d.b = 0;
990 return relocation_iterator(RelocationRef(RelData, this));
991}
992
993template <class ELFT>
996 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
997 relocation_iterator Begin = section_rel_begin(Sec);
998 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
999 return Begin;
1000 DataRefImpl RelData = Begin->getRawDataRefImpl();
1001 const Elf_Shdr *RelSec = getRelSection(RelData);
1002
1003 // Error check sh_link here so that getRelocationSymbol can just use it.
1004 auto SymSecOrErr = EF.getSection(RelSec->sh_link);
1005 if (!SymSecOrErr)
1007 Twine(errorToErrorCode(SymSecOrErr.takeError()).message()));
1008
1009 RelData.d.b += S->sh_size / S->sh_entsize;
1010 return relocation_iterator(RelocationRef(RelData, this));
1011}
1012
1013template <class ELFT>
1016 const Elf_Shdr *EShdr = getSection(Sec);
1017 uintX_t Type = EShdr->sh_type;
1018 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
1019 return section_end();
1020
1021 Expected<const Elf_Shdr *> SecOrErr = EF.getSection(EShdr->sh_info);
1022 if (!SecOrErr)
1023 return SecOrErr.takeError();
1024 return section_iterator(SectionRef(toDRI(*SecOrErr), this));
1025}
1026
1027// Relocations
1028template <class ELFT>
1030 ++Rel.d.b;
1031}
1032
1033template <class ELFT>
1036 uint32_t symbolIdx;
1037 const Elf_Shdr *sec = getRelSection(Rel);
1038 if (sec->sh_type == ELF::SHT_REL)
1039 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
1040 else
1041 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
1042 if (!symbolIdx)
1043 return symbol_end();
1044
1045 // FIXME: error check symbolIdx
1046 DataRefImpl SymbolData;
1047 SymbolData.d.a = sec->sh_link;
1048 SymbolData.d.b = symbolIdx;
1049 return symbol_iterator(SymbolRef(SymbolData, this));
1050}
1051
1052template <class ELFT>
1054 const Elf_Shdr *sec = getRelSection(Rel);
1055 if (sec->sh_type == ELF::SHT_REL)
1056 return getRel(Rel)->r_offset;
1057
1058 return getRela(Rel)->r_offset;
1059}
1060
1061template <class ELFT>
1063 const Elf_Shdr *sec = getRelSection(Rel);
1064 if (sec->sh_type == ELF::SHT_REL)
1065 return getRel(Rel)->getType(EF.isMips64EL());
1066 else
1067 return getRela(Rel)->getType(EF.isMips64EL());
1068}
1069
1070template <class ELFT>
1072 return getELFRelocationTypeName(EF.getHeader().e_machine, Type);
1073}
1074
1075template <class ELFT>
1077 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1078 uint32_t type = getRelocationType(Rel);
1079 EF.getRelocationTypeName(type, Result);
1080}
1081
1082template <class ELFT>
1085 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
1086 return createError("Section is not SHT_RELA");
1087 return (int64_t)getRela(Rel)->r_addend;
1088}
1089
1090template <class ELFT>
1091const typename ELFObjectFile<ELFT>::Elf_Rel *
1093 assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
1094 auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
1095 if (!Ret)
1096 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message()));
1097 return *Ret;
1098}
1099
1100template <class ELFT>
1101const typename ELFObjectFile<ELFT>::Elf_Rela *
1103 assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
1104 auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
1105 if (!Ret)
1106 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message()));
1107 return *Ret;
1108}
1109
1110template <class ELFT>
1113 auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer());
1114 if (Error E = EFOrErr.takeError())
1115 return std::move(E);
1116
1117 ELFObjectFile<ELFT> Obj = {Object, std::move(*EFOrErr), nullptr, nullptr,
1118 nullptr};
1119 if (InitContent)
1120 if (Error E = Obj.initContent())
1121 return std::move(E);
1122 return std::move(Obj);
1123}
1124
1125template <class ELFT>
1127 const Elf_Shdr *DotDynSymSec,
1128 const Elf_Shdr *DotSymtabSec,
1129 const Elf_Shdr *DotSymtabShndx)
1131 getELFType(ELFT::TargetEndianness == support::little, ELFT::Is64Bits),
1132 Object),
1133 EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
1134 DotSymtabShndxSec(DotSymtabShndx) {}
1135
1136template <class ELFT>
1138 : ELFObjectFile(Other.Data, Other.EF, Other.DotDynSymSec,
1139 Other.DotSymtabSec, Other.DotSymtabShndxSec) {}
1140
1141template <class ELFT>
1144 toDRI(DotSymtabSec,
1145 DotSymtabSec && DotSymtabSec->sh_size >= sizeof(Elf_Sym) ? 1 : 0);
1146 return basic_symbol_iterator(SymbolRef(Sym, this));
1147}
1148
1149template <class ELFT>
1151 const Elf_Shdr *SymTab = DotSymtabSec;
1152 if (!SymTab)
1153 return symbol_begin();
1154 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1155 return basic_symbol_iterator(SymbolRef(Sym, this));
1156}
1157
1158template <class ELFT>
1160 if (!DotDynSymSec || DotDynSymSec->sh_size < sizeof(Elf_Sym))
1161 // Ignore errors here where the dynsym is empty or sh_size less than the
1162 // size of one symbol. These should be handled elsewhere.
1163 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 0), this));
1164 // Skip 0-index NULL symbol.
1165 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 1), this));
1166}
1167
1168template <class ELFT>
1170 const Elf_Shdr *SymTab = DotDynSymSec;
1171 if (!SymTab)
1172 return dynamic_symbol_begin();
1173 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1174 return basic_symbol_iterator(SymbolRef(Sym, this));
1175}
1176
1177template <class ELFT>
1179 auto SectionsOrErr = EF.sections();
1180 if (!SectionsOrErr)
1181 return section_iterator(SectionRef());
1182 return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this));
1183}
1184
1185template <class ELFT>
1187 auto SectionsOrErr = EF.sections();
1188 if (!SectionsOrErr)
1189 return section_iterator(SectionRef());
1190 return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
1191}
1192
1193template <class ELFT>
1195 return ELFT::Is64Bits ? 8 : 4;
1196}
1197
1198template <class ELFT>
1200 constexpr bool IsLittleEndian = ELFT::TargetEndianness == support::little;
1201 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1202 case ELF::ELFCLASS32:
1203 switch (EF.getHeader().e_machine) {
1204 case ELF::EM_68K:
1205 return "elf32-m68k";
1206 case ELF::EM_386:
1207 return "elf32-i386";
1208 case ELF::EM_IAMCU:
1209 return "elf32-iamcu";
1210 case ELF::EM_X86_64:
1211 return "elf32-x86-64";
1212 case ELF::EM_ARM:
1213 return (IsLittleEndian ? "elf32-littlearm" : "elf32-bigarm");
1214 case ELF::EM_AVR:
1215 return "elf32-avr";
1216 case ELF::EM_HEXAGON:
1217 return "elf32-hexagon";
1218 case ELF::EM_LANAI:
1219 return "elf32-lanai";
1220 case ELF::EM_MIPS:
1221 return "elf32-mips";
1222 case ELF::EM_MSP430:
1223 return "elf32-msp430";
1224 case ELF::EM_PPC:
1225 return (IsLittleEndian ? "elf32-powerpcle" : "elf32-powerpc");
1226 case ELF::EM_RISCV:
1227 return "elf32-littleriscv";
1228 case ELF::EM_CSKY:
1229 return "elf32-csky";
1230 case ELF::EM_SPARC:
1232 return "elf32-sparc";
1233 case ELF::EM_AMDGPU:
1234 return "elf32-amdgpu";
1235 case ELF::EM_LOONGARCH:
1236 return "elf32-loongarch";
1237 case ELF::EM_XTENSA:
1238 return "elf32-xtensa";
1239 default:
1240 return "elf32-unknown";
1241 }
1242 case ELF::ELFCLASS64:
1243 switch (EF.getHeader().e_machine) {
1244 case ELF::EM_386:
1245 return "elf64-i386";
1246 case ELF::EM_X86_64:
1247 return "elf64-x86-64";
1248 case ELF::EM_AARCH64:
1249 return (IsLittleEndian ? "elf64-littleaarch64" : "elf64-bigaarch64");
1250 case ELF::EM_PPC64:
1251 return (IsLittleEndian ? "elf64-powerpcle" : "elf64-powerpc");
1252 case ELF::EM_RISCV:
1253 return "elf64-littleriscv";
1254 case ELF::EM_S390:
1255 return "elf64-s390";
1256 case ELF::EM_SPARCV9:
1257 return "elf64-sparc";
1258 case ELF::EM_MIPS:
1259 return "elf64-mips";
1260 case ELF::EM_AMDGPU:
1261 return "elf64-amdgpu";
1262 case ELF::EM_BPF:
1263 return "elf64-bpf";
1264 case ELF::EM_VE:
1265 return "elf64-ve";
1266 case ELF::EM_LOONGARCH:
1267 return "elf64-loongarch";
1268 default:
1269 return "elf64-unknown";
1270 }
1271 default:
1272 // FIXME: Proper error handling.
1273 report_fatal_error("Invalid ELFCLASS!");
1274 }
1275}
1276
1277template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
1278 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
1279 switch (EF.getHeader().e_machine) {
1280 case ELF::EM_68K:
1281 return Triple::m68k;
1282 case ELF::EM_386:
1283 case ELF::EM_IAMCU:
1284 return Triple::x86;
1285 case ELF::EM_X86_64:
1286 return Triple::x86_64;
1287 case ELF::EM_AARCH64:
1288 return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be;
1289 case ELF::EM_ARM:
1290 return Triple::arm;
1291 case ELF::EM_AVR:
1292 return Triple::avr;
1293 case ELF::EM_HEXAGON:
1294 return Triple::hexagon;
1295 case ELF::EM_LANAI:
1296 return Triple::lanai;
1297 case ELF::EM_MIPS:
1298 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1299 case ELF::ELFCLASS32:
1300 return IsLittleEndian ? Triple::mipsel : Triple::mips;
1301 case ELF::ELFCLASS64:
1302 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
1303 default:
1304 report_fatal_error("Invalid ELFCLASS!");
1305 }
1306 case ELF::EM_MSP430:
1307 return Triple::msp430;
1308 case ELF::EM_PPC:
1309 return IsLittleEndian ? Triple::ppcle : Triple::ppc;
1310 case ELF::EM_PPC64:
1311 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
1312 case ELF::EM_RISCV:
1313 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1314 case ELF::ELFCLASS32:
1315 return Triple::riscv32;
1316 case ELF::ELFCLASS64:
1317 return Triple::riscv64;
1318 default:
1319 report_fatal_error("Invalid ELFCLASS!");
1320 }
1321 case ELF::EM_S390:
1322 return Triple::systemz;
1323
1324 case ELF::EM_SPARC:
1326 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
1327 case ELF::EM_SPARCV9:
1328 return Triple::sparcv9;
1329
1330 case ELF::EM_AMDGPU: {
1331 if (!IsLittleEndian)
1332 return Triple::UnknownArch;
1333
1334 unsigned MACH = EF.getHeader().e_flags & ELF::EF_AMDGPU_MACH;
1335 if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST &&
1337 return Triple::r600;
1340 return Triple::amdgcn;
1341
1342 return Triple::UnknownArch;
1343 }
1344
1345 case ELF::EM_BPF:
1346 return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
1347
1348 case ELF::EM_VE:
1349 return Triple::ve;
1350 case ELF::EM_CSKY:
1351 return Triple::csky;
1352
1353 case ELF::EM_LOONGARCH:
1354 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1355 case ELF::ELFCLASS32:
1356 return Triple::loongarch32;
1357 case ELF::ELFCLASS64:
1358 return Triple::loongarch64;
1359 default:
1360 report_fatal_error("Invalid ELFCLASS!");
1361 }
1362
1363 case ELF::EM_XTENSA:
1364 return Triple::xtensa;
1365
1366 default:
1367 return Triple::UnknownArch;
1368 }
1369}
1370
1371template <class ELFT>
1373 return EF.getHeader().e_entry;
1374}
1375
1376template <class ELFT>
1379 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
1380}
1381
1382template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
1383 return EF.getHeader().e_type == ELF::ET_REL;
1384}
1385
1386} // end namespace object
1387} // end namespace llvm
1388
1389#endif // LLVM_OBJECT_ELFOBJECTFILE_H
aarch64 promote const
AMDGPU Kernel Attributes
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static StringRef getSymbolName(SymbolKind SymKind)
std::string Name
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Definition: ELFTypes.h:104
Symbol * Sym
Definition: ELF_riscv.cpp:468
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
static uint64_t getSymbolValue(const MCSymbol &Symbol, const MCAsmLayout &Layout)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Base class for error info classes.
Definition: Error.h:45
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
reference get()
Returns a reference to the stored T value.
Definition: Error.h:571
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
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:792
virtual uint64_t getSymbolSize(DataRefImpl Symb) const =0
Expected< std::vector< BBAddrMap > > readBBAddrMap(std::optional< unsigned > TextSectionIndex=std::nullopt) const
Returns a vector of all BB address maps in the object file. When.
virtual Error getBuildAttributes(ELFAttributeParser &Attributes) const =0
virtual uint64_t getSectionFlags(DataRefImpl Sec) const =0
virtual uint16_t getEType() const =0
virtual uint8_t getSymbolELFType(DataRefImpl Symb) const =0
Expected< std::vector< VersionEntry > > readDynsymVersions() const
Returns a vector containing a symbol version for each dynamic symbol.
virtual uint8_t getSymbolOther(DataRefImpl Symb) const =0
virtual elf_symbol_iterator_range getDynamicSymbolIterators() const =0
virtual uint32_t getSectionType(DataRefImpl Sec) const =0
elf_symbol_iterator_range symbols() const
virtual Expected< int64_t > getRelocationAddend(DataRefImpl Rel) const =0
std::vector< ELFPltEntry > getPltEntries() const
Expected< SubtargetFeatures > getFeatures() const override
virtual uint8_t getSymbolBinding(DataRefImpl Symb) const =0
std::optional< StringRef > tryGetCPUName() const override
iterator_range< elf_symbol_iterator > elf_symbol_iterator_range
Definition: ELFObjectFile.h:85
virtual uint16_t getEMachine() const =0
static bool classof(const Binary *v)
Definition: ELFObjectFile.h:94
virtual unsigned getPlatformFlags() const =0
Returns platform-specific object flags, if any.
void setARMSubArch(Triple &TheTriple) const override
virtual uint64_t getSectionOffset(DataRefImpl Sec) const =0
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
SectionRef toSectionRef(const Elf_Shdr *Sec) const
uint32_t getSectionType(DataRefImpl Sec) const override
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
elf_symbol_iterator_range getDynamicSymbolIterators() const override
Expected< const Elf_Sym * > getSymbol(DataRefImpl Sym) const
const Elf_Rel * getRel(DataRefImpl Rel) const
Expected< section_iterator > getSymbolSection(const Elf_Sym *Symb, const Elf_Shdr *SymTab) const
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override
basic_symbol_iterator symbol_begin() const override
Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
ELFSymbolRef toSymbolRef(const Elf_Shdr *SymTable, unsigned SymbolNum) const
const Elf_Rela * getRela(DataRefImpl Rela) const
static Expected< ELFObjectFile< ELFT > > create(MemoryBufferRef Object, bool InitContent=true)
bool isExportedToOtherDSO(const Elf_Sym *ESym) const
uint64_t getSectionAddress(DataRefImpl Sec) const override
Expected< ArrayRef< uint8_t > > getSectionContents(DataRefImpl Sec) const override
void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const override
uint64_t getSectionIndex(DataRefImpl Sec) const override
Expected< uint32_t > getSymbolFlags(DataRefImpl Symb) const override
bool isSectionData(DataRefImpl Sec) const override
const Elf_Shdr * DotSymtabSec
const Elf_Shdr * DotDynSymSec
uint32_t getSymbolAlignment(DataRefImpl Symb) const override
const Elf_Shdr * toELFShdrIter(DataRefImpl Sec) const
Triple::ArchType getArch() const override
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
uint64_t getSectionSize(DataRefImpl Sec) const override
bool isBerkeleyData(DataRefImpl Sec) const override
StringRef getFileFormatName() const override
bool isRelocatableObject() const override
True if this is a relocatable object (.o/.obj).
void moveSymbolNext(DataRefImpl &Symb) const override
uint8_t getSymbolOther(DataRefImpl Symb) const override
section_iterator section_end() const override
const Elf_Shdr * getRelSection(DataRefImpl Rel) const
Get the relocation section that contains Rel.
Expected< int64_t > getRelocationAddend(DataRefImpl Rel) const override
uint64_t getRelocationOffset(DataRefImpl Rel) const override
Expected< section_iterator > getRelocatedSection(DataRefImpl Sec) const override
void moveSectionNext(DataRefImpl &Sec) const override
bool isSectionBSS(DataRefImpl Sec) const override
unsigned getPlatformFlags() const override
Returns platform-specific object flags, if any.
uint64_t getSectionFlags(DataRefImpl Sec) const override
void moveRelocationNext(DataRefImpl &Rel) const override
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
Expected< StringRef > getSymbolName(DataRefImpl Symb) const override
Error initContent() override
basic_symbol_iterator symbol_end() const override
uint8_t getSymbolBinding(DataRefImpl Symb) const override
uint64_t getSectionOffset(DataRefImpl Sec) const override
relocation_iterator section_rel_end(DataRefImpl Sec) const override
Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const override
const Elf_Shdr * getSection(DataRefImpl Sec) const
Error getBuildAttributes(ELFAttributeParser &Attributes) const override
DataRefImpl toDRI(const Elf_Shdr *Sec) const
elf_symbol_iterator dynamic_symbol_begin() const
const Elf_Shdr * DotSymtabShndxSec
elf_symbol_iterator dynamic_symbol_end() const
DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const
bool isDebugSection(DataRefImpl Sec) const override
section_iterator section_begin() const override
bool isSectionCompressed(DataRefImpl Sec) const override
bool isBerkeleyText(DataRefImpl Sec) const override
uint8_t getBytesInAddress() const override
The number of bytes used to represent an address in this object file format.
Expected< uint64_t > getStartAddress() const override
Expected< int64_t > getAddend() const
const ELFObjectFileBase * getObject() const
ELFRelocationRef(const RelocationRef &B)
const ELFObjectFileBase * getObject() const
uint64_t getOffset() const
ELFSectionRef(const SectionRef &B)
const ELFObjectFileBase * getObject() const
uint8_t getELFType() const
ELFSymbolRef(const SymbolRef &B)
uint8_t getBinding() const
uint64_t getSize() const
StringRef getELFTypeName() const
This class is the base class for all object file types.
Definition: ObjectFile.h:229
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:52
const ObjectFile * getObject() const
Definition: ObjectFile.h:638
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:634
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:596
const ObjectFile * getObject() const
Definition: ObjectFile.h:600
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:486
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_CLASS
Definition: ELF.h:52
@ STB_GLOBAL
Definition: ELF.h:1243
@ STB_LOCAL
Definition: ELF.h:1242
@ STB_GNU_UNIQUE
Definition: ELF.h:1245
@ STB_WEAK
Definition: ELF.h:1244
@ ET_REL
Definition: ELF.h:116
@ EM_MSP430
Definition: ELF.h:222
@ EM_S390
Definition: ELF.h:150
@ EM_PPC64
Definition: ELF.h:149
@ EM_SPARC
Definition: ELF.h:135
@ EM_CSKY
Definition: ELF.h:321
@ EM_SPARC32PLUS
Definition: ELF.h:146
@ EM_68K
Definition: ELF.h:137
@ EM_386
Definition: ELF.h:136
@ EM_LOONGARCH
Definition: ELF.h:322
@ EM_BPF
Definition: ELF.h:319
@ EM_PPC
Definition: ELF.h:148
@ EM_X86_64
Definition: ELF.h:178
@ EM_HEXAGON
Definition: ELF.h:257
@ EM_LANAI
Definition: ELF.h:318
@ EM_MIPS
Definition: ELF.h:141
@ EM_SPARCV9
Definition: ELF.h:159
@ EM_AARCH64
Definition: ELF.h:280
@ EM_XTENSA
Definition: ELF.h:211
@ EM_RISCV
Definition: ELF.h:317
@ EM_ARM
Definition: ELF.h:156
@ EM_VE
Definition: ELF.h:320
@ EM_IAMCU
Definition: ELF.h:139
@ EM_AMDGPU
Definition: ELF.h:316
@ EM_AVR
Definition: ELF.h:199
@ SHT_PROGBITS
Definition: ELF.h:1000
@ SHT_REL
Definition: ELF.h:1008
@ SHT_ARM_ATTRIBUTES
Definition: ELF.h:1056
@ SHT_NOBITS
Definition: ELF.h:1007
@ SHT_SYMTAB
Definition: ELF.h:1001
@ SHT_DYNAMIC
Definition: ELF.h:1005
@ SHT_SYMTAB_SHNDX
Definition: ELF.h:1015
@ SHT_RISCV_ATTRIBUTES
Definition: ELF.h:1074
@ SHT_RELA
Definition: ELF.h:1003
@ SHT_DYNSYM
Definition: ELF.h:1010
@ SHN_ABS
Definition: ELF.h:991
@ SHN_COMMON
Definition: ELF.h:992
@ SHN_UNDEF
Definition: ELF.h:985
@ EF_AMDGPU_MACH_AMDGCN_LAST
Definition: ELF.h:789
@ EF_AMDGPU_MACH_R600_LAST
Definition: ELF.h:738
@ EF_AMDGPU_MACH_AMDGCN_FIRST
Definition: ELF.h:788
@ EF_AMDGPU_MACH
Definition: ELF.h:704
@ EF_AMDGPU_MACH_R600_FIRST
Definition: ELF.h:737
@ STV_HIDDEN
Definition: ELF.h:1274
@ STV_PROTECTED
Definition: ELF.h:1275
@ STV_DEFAULT
Definition: ELF.h:1272
@ ELFCLASS64
Definition: ELF.h:329
@ ELFCLASS32
Definition: ELF.h:328
@ SHF_ALLOC
Definition: ELF.h:1089
@ SHF_COMPRESSED
Definition: ELF.h:1117
@ SHF_WRITE
Definition: ELF.h:1086
@ SHF_EXECINSTR
Definition: ELF.h:1092
@ STT_FUNC
Definition: ELF.h:1256
@ STT_NOTYPE
Definition: ELF.h:1254
@ STT_SECTION
Definition: ELF.h:1257
@ STT_FILE
Definition: ELF.h:1258
@ STT_COMMON
Definition: ELF.h:1259
@ STT_GNU_IFUNC
Definition: ELF.h:1261
@ STT_OBJECT
Definition: ELF.h:1255
@ STT_TLS
Definition: ELF.h:1260
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:438
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:970
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
@ Other
Any other memory.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
@ Dynamic
Denotes mode unknown at compile time.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition: Casting.h:565
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1884
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:109
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1041
std::optional< DataRefImpl > Symbol
Definition: ELFObjectFile.h:53
struct llvm::object::DataRefImpl::@351 d