LLVM 19.0.0git
ELFObjectFile.h
Go to the documentation of this file.
1//===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the ELFObjectFile template class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_OBJECT_ELFOBJECTFILE_H
14#define LLVM_OBJECT_ELFOBJECTFILE_H
15
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/StringRef.h"
21#include "llvm/Object/Binary.h"
22#include "llvm/Object/ELF.h"
24#include "llvm/Object/Error.h"
30#include "llvm/Support/Error.h"
36#include <cassert>
37#include <cstdint>
38
39namespace llvm {
40
41template <typename T> class SmallVectorImpl;
42
43namespace object {
44
45constexpr int NumElfSymbolTypes = 16;
47
49
52 std::optional<DataRefImpl> Symbol;
54};
55
57 friend class ELFRelocationRef;
58 friend class ELFSectionRef;
59 friend class ELFSymbolRef;
60
61 SubtargetFeatures getMIPSFeatures() const;
62 SubtargetFeatures getARMFeatures() const;
63 SubtargetFeatures getHexagonFeatures() const;
64 Expected<SubtargetFeatures> getRISCVFeatures() const;
65 SubtargetFeatures getLoongArchFeatures() const;
66
67 StringRef getAMDGPUCPUName() const;
68 StringRef getNVPTXCPUName() const;
69
70protected:
71 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
72
73 virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
74 virtual uint8_t getSymbolBinding(DataRefImpl Symb) const = 0;
75 virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
76 virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
77
78 virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
79 virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
80 virtual uint64_t getSectionOffset(DataRefImpl Sec) const = 0;
81
83 virtual Error getBuildAttributes(ELFAttributeParser &Attributes) const = 0;
84
85public:
87
89
90 /// Returns platform-specific object flags, if any.
91 virtual unsigned getPlatformFlags() const = 0;
92
94
95 static bool classof(const Binary *v) { return v->isELF(); }
96
98
99 std::optional<StringRef> tryGetCPUName() const override;
100
101 void setARMSubArch(Triple &TheTriple) const override;
102
103 virtual uint16_t getEType() const = 0;
104
105 virtual uint16_t getEMachine() const = 0;
106
107 virtual uint8_t getEIdentABIVersion() const = 0;
108
109 std::vector<ELFPltEntry> getPltEntries() const;
110
111 /// Returns a vector containing a symbol version for each dynamic symbol.
112 /// Returns an empty vector if version sections do not exist.
114
115 /// Returns a vector of all BB address maps in the object file. When
116 /// `TextSectionIndex` is specified, only returns the BB address maps
117 /// corresponding to the section with that index. When `PGOAnalyses`is
118 /// specified (PGOAnalyses is not nullptr), the vector is cleared then filled
119 /// with extra PGO data. `PGOAnalyses` will always be the same length as the
120 /// return value when it is requested assuming no error occurs. Upon failure,
121 /// `PGOAnalyses` will be emptied.
123 readBBAddrMap(std::optional<unsigned> TextSectionIndex = std::nullopt,
124 std::vector<PGOAnalysisMap> *PGOAnalyses = nullptr) const;
125};
126
127class ELFSectionRef : public SectionRef {
128public:
130 assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
131 }
132
134 return cast<ELFObjectFileBase>(SectionRef::getObject());
135 }
136
139 }
140
143 }
144
147 }
148};
149
151public:
153 assert(isa<ELFObjectFileBase>(B->getObject()));
154 }
155
156 const ELFSectionRef *operator->() const {
157 return static_cast<const ELFSectionRef *>(section_iterator::operator->());
158 }
159
160 const ELFSectionRef &operator*() const {
161 return static_cast<const ELFSectionRef &>(section_iterator::operator*());
162 }
163};
164
165class ELFSymbolRef : public SymbolRef {
166public:
168 assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
169 }
170
172 return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
173 }
174
177 }
178
179 uint8_t getBinding() const {
181 }
182
183 uint8_t getOther() const {
185 }
186
187 uint8_t getELFType() const {
189 }
190
192 uint8_t Type = getELFType();
193 for (const auto &EE : ElfSymbolTypes) {
194 if (EE.Value == Type) {
195 return EE.AltName;
196 }
197 }
198 return "";
199 }
200};
201
203public:
205 : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
207
208 const ELFSymbolRef *operator->() const {
209 return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
210 }
211
212 const ELFSymbolRef &operator*() const {
213 return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
214 }
215};
216
218public:
220 assert(isa<ELFObjectFileBase>(RelocationRef::getObject()));
221 }
222
224 return cast<ELFObjectFileBase>(RelocationRef::getObject());
225 }
226
229 }
230};
231
233public:
236 B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {}
237
239 return static_cast<const ELFRelocationRef *>(
241 }
242
244 return static_cast<const ELFRelocationRef &>(
246 }
247};
248
252}
253
254template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
255 uint16_t getEMachine() const override;
256 uint16_t getEType() const override;
257 uint8_t getEIdentABIVersion() const override;
258 uint64_t getSymbolSize(DataRefImpl Sym) const override;
259
260public:
262
263 SectionRef toSectionRef(const Elf_Shdr *Sec) const {
264 return SectionRef(toDRI(Sec), this);
265 }
266
267 ELFSymbolRef toSymbolRef(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
268 return ELFSymbolRef({toDRI(SymTable, SymbolNum), this});
269 }
270
271 bool IsContentValid() const { return ContentValid; }
272
273private:
275 const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,
276 const Elf_Shdr *DotSymtabShndxSec);
277
278 bool ContentValid = false;
279
280protected:
282
283 const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
284 const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
285 const Elf_Shdr *DotSymtabShndxSec = nullptr; // SHT_SYMTAB_SHNDX section.
286
287 Error initContent() override;
288
289 void moveSymbolNext(DataRefImpl &Symb) const override;
290 Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
292 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
293 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
294 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
295 Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override;
296 uint8_t getSymbolBinding(DataRefImpl Symb) const override;
297 uint8_t getSymbolOther(DataRefImpl Symb) const override;
298 uint8_t getSymbolELFType(DataRefImpl Symb) const override;
301 const Elf_Shdr *SymTab) const;
303
304 void moveSectionNext(DataRefImpl &Sec) const override;
306 uint64_t getSectionAddress(DataRefImpl Sec) const override;
307 uint64_t getSectionIndex(DataRefImpl Sec) const override;
308 uint64_t getSectionSize(DataRefImpl Sec) const override;
310 getSectionContents(DataRefImpl Sec) const override;
311 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
312 bool isSectionCompressed(DataRefImpl Sec) const override;
313 bool isSectionText(DataRefImpl Sec) const override;
314 bool isSectionData(DataRefImpl Sec) const override;
315 bool isSectionBSS(DataRefImpl Sec) const override;
316 bool isSectionVirtual(DataRefImpl Sec) const override;
317 bool isBerkeleyText(DataRefImpl Sec) const override;
318 bool isBerkeleyData(DataRefImpl Sec) const override;
319 bool isDebugSection(DataRefImpl Sec) const override;
322 std::vector<SectionRef> dynamic_relocation_sections() const override;
324 getRelocatedSection(DataRefImpl Sec) const override;
325
326 void moveRelocationNext(DataRefImpl &Rel) const override;
327 uint64_t getRelocationOffset(DataRefImpl Rel) const override;
329 uint64_t getRelocationType(DataRefImpl Rel) const override;
331 SmallVectorImpl<char> &Result) const override;
332
333 uint32_t getSectionType(DataRefImpl Sec) const override;
334 uint64_t getSectionFlags(DataRefImpl Sec) const override;
335 uint64_t getSectionOffset(DataRefImpl Sec) const override;
337
338 DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
339 DataRefImpl DRI;
340 if (!SymTable) {
341 DRI.d.a = 0;
342 DRI.d.b = 0;
343 return DRI;
344 }
345 assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
346 SymTable->sh_type == ELF::SHT_DYNSYM);
347
348 auto SectionsOrErr = EF.sections();
349 if (!SectionsOrErr) {
350 DRI.d.a = 0;
351 DRI.d.b = 0;
352 return DRI;
353 }
354 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
355 unsigned SymTableIndex =
356 (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
357
358 DRI.d.a = SymTableIndex;
359 DRI.d.b = SymbolNum;
360 return DRI;
361 }
362
363 const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
364 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
365 }
366
367 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
368 DataRefImpl DRI;
369 DRI.p = reinterpret_cast<uintptr_t>(Sec);
370 return DRI;
371 }
372
373 DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
374 DataRefImpl DRI;
375 DRI.p = reinterpret_cast<uintptr_t>(Dyn);
376 return DRI;
377 }
378
379 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
380 unsigned char Binding = ESym->getBinding();
381 unsigned char Visibility = ESym->getVisibility();
382
383 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
384 // visibility is either DEFAULT or PROTECTED. All other symbols are not
385 // exported.
386 return (
387 (Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK ||
388 Binding == ELF::STB_GNU_UNIQUE) &&
389 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED));
390 }
391
392 Error getBuildAttributes(ELFAttributeParser &Attributes) const override {
394 switch (getEMachine()) {
395 case ELF::EM_ARM:
397 break;
398 case ELF::EM_RISCV:
400 break;
401 case ELF::EM_HEXAGON:
403 break;
404 default:
405 return Error::success();
406 }
407
408 auto SectionsOrErr = EF.sections();
409 if (!SectionsOrErr)
410 return SectionsOrErr.takeError();
411 for (const Elf_Shdr &Sec : *SectionsOrErr) {
412 if (Sec.sh_type != Type)
413 continue;
414 auto ErrorOrContents = EF.getSectionContents(Sec);
415 if (!ErrorOrContents)
416 return ErrorOrContents.takeError();
417
418 auto Contents = ErrorOrContents.get();
419 if (Contents[0] != ELFAttrs::Format_Version || Contents.size() == 1)
420 return Error::success();
421
422 if (Error E = Attributes.parse(Contents, ELFT::TargetEndianness))
423 return E;
424 break;
425 }
426 return Error::success();
427 }
428
429 // This flag is used for classof, to distinguish ELFObjectFile from
430 // its subclass. If more subclasses will be created, this flag will
431 // have to become an enum.
432 bool isDyldELFObject = false;
433
434public:
437 bool InitContent = true);
438
439 const Elf_Rel *getRel(DataRefImpl Rel) const;
440 const Elf_Rela *getRela(DataRefImpl Rela) const;
441
443 return EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
444 }
445
446 /// Get the relocation section that contains \a Rel.
447 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
448 auto RelSecOrErr = EF.getSection(Rel.d.a);
449 if (!RelSecOrErr)
451 Twine(errorToErrorCode(RelSecOrErr.takeError()).message()));
452 return *RelSecOrErr;
453 }
454
455 const Elf_Shdr *getSection(DataRefImpl Sec) const {
456 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
457 }
458
459 basic_symbol_iterator symbol_begin() const override;
460 basic_symbol_iterator symbol_end() const override;
461
462 bool is64Bit() const override { return getBytesInAddress() == 8; }
463
466
467 section_iterator section_begin() const override;
468 section_iterator section_end() const override;
469
471
472 uint8_t getBytesInAddress() const override;
473 StringRef getFileFormatName() const override;
474 Triple::ArchType getArch() const override;
475 Triple::OSType getOS() const override;
476 Expected<uint64_t> getStartAddress() const override;
477
478 unsigned getPlatformFlags() const override { return EF.getHeader().e_flags; }
479
480 const ELFFile<ELFT> &getELFFile() const { return EF; }
481
482 bool isDyldType() const { return isDyldELFObject; }
483 static bool classof(const Binary *v) {
484 return v->getType() ==
485 getELFType(ELFT::TargetEndianness == llvm::endianness::little,
486 ELFT::Is64Bits);
487 }
488
490
491 bool isRelocatableObject() const override;
492
493 void createFakeSections() { EF.createFakeSections(); }
494};
495
500
501template <class ELFT>
503 ++Sym.d.b;
504}
505
507 auto SectionsOrErr = EF.sections();
508 if (!SectionsOrErr)
509 return SectionsOrErr.takeError();
510
511 for (const Elf_Shdr &Sec : *SectionsOrErr) {
512 switch (Sec.sh_type) {
513 case ELF::SHT_DYNSYM: {
514 if (!DotDynSymSec)
515 DotDynSymSec = &Sec;
516 break;
517 }
518 case ELF::SHT_SYMTAB: {
519 if (!DotSymtabSec)
520 DotSymtabSec = &Sec;
521 break;
522 }
524 if (!DotSymtabShndxSec)
525 DotSymtabShndxSec = &Sec;
526 break;
527 }
528 }
529 }
530
531 ContentValid = true;
532 return Error::success();
533}
534
535template <class ELFT>
537 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
538 if (!SymOrErr)
539 return SymOrErr.takeError();
540 auto SymTabOrErr = EF.getSection(Sym.d.a);
541 if (!SymTabOrErr)
542 return SymTabOrErr.takeError();
543 const Elf_Shdr *SymTableSec = *SymTabOrErr;
544 auto StrTabOrErr = EF.getSection(SymTableSec->sh_link);
545 if (!StrTabOrErr)
546 return StrTabOrErr.takeError();
547 const Elf_Shdr *StringTableSec = *StrTabOrErr;
548 auto SymStrTabOrErr = EF.getStringTable(*StringTableSec);
549 if (!SymStrTabOrErr)
550 return SymStrTabOrErr.takeError();
551 Expected<StringRef> Name = (*SymOrErr)->getName(*SymStrTabOrErr);
552 if (Name && !Name->empty())
553 return Name;
554
555 // If the symbol name is empty use the section name.
556 if ((*SymOrErr)->getType() == ELF::STT_SECTION) {
557 Expected<section_iterator> SecOrErr = getSymbolSection(Sym);
558 if (SecOrErr)
559 return (*SecOrErr)->getName();
560 return SecOrErr.takeError();
561 }
562 return Name;
563}
564
565template <class ELFT>
567 return getSection(Sec)->sh_flags;
568}
569
570template <class ELFT>
572 return getSection(Sec)->sh_type;
573}
574
575template <class ELFT>
577 return getSection(Sec)->sh_offset;
578}
579
580template <class ELFT>
582 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
583 if (!SymOrErr)
584 report_fatal_error(SymOrErr.takeError());
585
586 uint64_t Ret = (*SymOrErr)->st_value;
587 if ((*SymOrErr)->st_shndx == ELF::SHN_ABS)
588 return Ret;
589
590 const Elf_Ehdr &Header = EF.getHeader();
591 // Clear the ARM/Thumb or microMIPS indicator flag.
592 if ((Header.e_machine == ELF::EM_ARM || Header.e_machine == ELF::EM_MIPS) &&
593 (*SymOrErr)->getType() == ELF::STT_FUNC)
594 Ret &= ~1;
595
596 return Ret;
597}
598
599template <class ELFT>
602 Expected<uint64_t> SymbolValueOrErr = getSymbolValue(Symb);
603 if (!SymbolValueOrErr)
604 // TODO: Test this error.
605 return SymbolValueOrErr.takeError();
606
607 uint64_t Result = *SymbolValueOrErr;
608 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
609 if (!SymOrErr)
610 return SymOrErr.takeError();
611
612 switch ((*SymOrErr)->st_shndx) {
613 case ELF::SHN_COMMON:
614 case ELF::SHN_UNDEF:
615 case ELF::SHN_ABS:
616 return Result;
617 }
618
619 auto SymTabOrErr = EF.getSection(Symb.d.a);
620 if (!SymTabOrErr)
621 return SymTabOrErr.takeError();
622
623 if (EF.getHeader().e_type == ELF::ET_REL) {
624 ArrayRef<Elf_Word> ShndxTable;
625 if (DotSymtabShndxSec) {
626 // TODO: Test this error.
627 if (Expected<ArrayRef<Elf_Word>> ShndxTableOrErr =
628 EF.getSHNDXTable(*DotSymtabShndxSec))
629 ShndxTable = *ShndxTableOrErr;
630 else
631 return ShndxTableOrErr.takeError();
632 }
633
634 Expected<const Elf_Shdr *> SectionOrErr =
635 EF.getSection(**SymOrErr, *SymTabOrErr, ShndxTable);
636 if (!SectionOrErr)
637 return SectionOrErr.takeError();
638 const Elf_Shdr *Section = *SectionOrErr;
639 if (Section)
640 Result += Section->sh_addr;
641 }
642
643 return Result;
644}
645
646template <class ELFT>
648 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
649 if (!SymOrErr)
650 report_fatal_error(SymOrErr.takeError());
651 if ((*SymOrErr)->st_shndx == ELF::SHN_COMMON)
652 return (*SymOrErr)->st_value;
653 return 0;
654}
655
656template <class ELFT>
658 return EF.getHeader().e_machine;
659}
660
661template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const {
662 return EF.getHeader().e_type;
663}
664
665template <class ELFT> uint8_t ELFObjectFile<ELFT>::getEIdentABIVersion() const {
666 return EF.getHeader().e_ident[ELF::EI_ABIVERSION];
667}
668
669template <class ELFT>
670uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
671 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
672 if (!SymOrErr)
673 report_fatal_error(SymOrErr.takeError());
674 return (*SymOrErr)->st_size;
675}
676
677template <class ELFT>
679 return getSymbolSize(Symb);
680}
681
682template <class ELFT>
684 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
685 if (!SymOrErr)
686 report_fatal_error(SymOrErr.takeError());
687 return (*SymOrErr)->getBinding();
688}
689
690template <class ELFT>
692 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
693 if (!SymOrErr)
694 report_fatal_error(SymOrErr.takeError());
695 return (*SymOrErr)->st_other;
696}
697
698template <class ELFT>
700 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
701 if (!SymOrErr)
702 report_fatal_error(SymOrErr.takeError());
703 return (*SymOrErr)->getType();
704}
705
706template <class ELFT>
709 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
710 if (!SymOrErr)
711 return SymOrErr.takeError();
712
713 switch ((*SymOrErr)->getType()) {
714 case ELF::STT_NOTYPE:
716 case ELF::STT_SECTION:
717 return SymbolRef::ST_Debug;
718 case ELF::STT_FILE:
719 return SymbolRef::ST_File;
720 case ELF::STT_FUNC:
722 case ELF::STT_OBJECT:
723 case ELF::STT_COMMON:
724 return SymbolRef::ST_Data;
725 case ELF::STT_TLS:
726 default:
727 return SymbolRef::ST_Other;
728 }
729}
730
731template <class ELFT>
733 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
734 if (!SymOrErr)
735 return SymOrErr.takeError();
736
737 const Elf_Sym *ESym = *SymOrErr;
739
740 if (ESym->getBinding() != ELF::STB_LOCAL)
741 Result |= SymbolRef::SF_Global;
742
743 if (ESym->getBinding() == ELF::STB_WEAK)
744 Result |= SymbolRef::SF_Weak;
745
746 if (ESym->st_shndx == ELF::SHN_ABS)
747 Result |= SymbolRef::SF_Absolute;
748
749 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION)
751
752 if (Expected<typename ELFT::SymRange> SymbolsOrErr =
753 EF.symbols(DotSymtabSec)) {
754 // Set the SF_FormatSpecific flag for the 0-index null symbol.
755 if (ESym == SymbolsOrErr->begin())
757 } else
758 // TODO: Test this error.
759 return SymbolsOrErr.takeError();
760
761 if (Expected<typename ELFT::SymRange> SymbolsOrErr =
762 EF.symbols(DotDynSymSec)) {
763 // Set the SF_FormatSpecific flag for the 0-index null symbol.
764 if (ESym == SymbolsOrErr->begin())
766 } else
767 // TODO: Test this error.
768 return SymbolsOrErr.takeError();
769
770 if (EF.getHeader().e_machine == ELF::EM_AARCH64) {
771 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
772 StringRef Name = *NameOrErr;
773 if (Name.starts_with("$d") || Name.starts_with("$x"))
775 } else {
776 // TODO: Actually report errors helpfully.
777 consumeError(NameOrErr.takeError());
778 }
779 } else if (EF.getHeader().e_machine == ELF::EM_ARM) {
780 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
781 StringRef Name = *NameOrErr;
782 // TODO Investigate why empty name symbols need to be marked.
783 if (Name.empty() || Name.starts_with("$d") || Name.starts_with("$t") ||
784 Name.starts_with("$a"))
786 } else {
787 // TODO: Actually report errors helpfully.
788 consumeError(NameOrErr.takeError());
789 }
790 if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1)
791 Result |= SymbolRef::SF_Thumb;
792 } else if (EF.getHeader().e_machine == ELF::EM_CSKY) {
793 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
794 StringRef Name = *NameOrErr;
795 if (Name.starts_with("$d") || Name.starts_with("$t"))
797 } else {
798 // TODO: Actually report errors helpfully.
799 consumeError(NameOrErr.takeError());
800 }
801 } else if (EF.getHeader().e_machine == ELF::EM_RISCV) {
802 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
803 StringRef Name = *NameOrErr;
804 // Mark empty name symbols (used for label differences) and mapping
805 // symbols.
806 if (Name.empty() || Name.starts_with("$d") || Name.starts_with("$x"))
808 } else {
809 // TODO: Actually report errors helpfully.
810 consumeError(NameOrErr.takeError());
811 }
812 }
813
814 if (ESym->st_shndx == ELF::SHN_UNDEF)
815 Result |= SymbolRef::SF_Undefined;
816
817 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
818 Result |= SymbolRef::SF_Common;
819
820 if (isExportedToOtherDSO(ESym))
821 Result |= SymbolRef::SF_Exported;
822
823 if (ESym->getType() == ELF::STT_GNU_IFUNC)
824 Result |= SymbolRef::SF_Indirect;
825
826 if (ESym->getVisibility() == ELF::STV_HIDDEN)
827 Result |= SymbolRef::SF_Hidden;
828
829 return Result;
830}
831
832template <class ELFT>
835 const Elf_Shdr *SymTab) const {
836 ArrayRef<Elf_Word> ShndxTable;
837 if (DotSymtabShndxSec) {
838 // TODO: Test this error.
839 Expected<ArrayRef<Elf_Word>> ShndxTableOrErr =
840 EF.getSHNDXTable(*DotSymtabShndxSec);
841 if (!ShndxTableOrErr)
842 return ShndxTableOrErr.takeError();
843 ShndxTable = *ShndxTableOrErr;
844 }
845
846 auto ESecOrErr = EF.getSection(*ESym, SymTab, ShndxTable);
847 if (!ESecOrErr)
848 return ESecOrErr.takeError();
849
850 const Elf_Shdr *ESec = *ESecOrErr;
851 if (!ESec)
852 return section_end();
853
854 DataRefImpl Sec;
855 Sec.p = reinterpret_cast<intptr_t>(ESec);
856 return section_iterator(SectionRef(Sec, this));
857}
858
859template <class ELFT>
862 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
863 if (!SymOrErr)
864 return SymOrErr.takeError();
865
866 auto SymTabOrErr = EF.getSection(Symb.d.a);
867 if (!SymTabOrErr)
868 return SymTabOrErr.takeError();
869 return getSymbolSection(*SymOrErr, *SymTabOrErr);
870}
871
872template <class ELFT>
874 const Elf_Shdr *ESec = getSection(Sec);
875 Sec = toDRI(++ESec);
876}
877
878template <class ELFT>
880 return EF.getSectionName(*getSection(Sec));
881}
882
883template <class ELFT>
885 return getSection(Sec)->sh_addr;
886}
887
888template <class ELFT>
890 auto SectionsOrErr = EF.sections();
891 handleAllErrors(std::move(SectionsOrErr.takeError()),
892 [](const ErrorInfoBase &) {
893 llvm_unreachable("unable to get section index");
894 });
895 const Elf_Shdr *First = SectionsOrErr->begin();
896 return getSection(Sec) - First;
897}
898
899template <class ELFT>
901 return getSection(Sec)->sh_size;
902}
903
904template <class ELFT>
907 const Elf_Shdr *EShdr = getSection(Sec);
908 if (EShdr->sh_type == ELF::SHT_NOBITS)
909 return ArrayRef((const uint8_t *)base(), (size_t)0);
910 if (Error E =
911 checkOffset(getMemoryBufferRef(),
912 (uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
913 return std::move(E);
914 return ArrayRef((const uint8_t *)base() + EShdr->sh_offset, EShdr->sh_size);
915}
916
917template <class ELFT>
919 return getSection(Sec)->sh_addralign;
920}
921
922template <class ELFT>
924 return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED;
925}
926
927template <class ELFT>
929 return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR;
930}
931
932template <class ELFT>
934 const Elf_Shdr *EShdr = getSection(Sec);
935 return EShdr->sh_type == ELF::SHT_PROGBITS &&
936 EShdr->sh_flags & ELF::SHF_ALLOC &&
937 !(EShdr->sh_flags & ELF::SHF_EXECINSTR);
938}
939
940template <class ELFT>
942 const Elf_Shdr *EShdr = getSection(Sec);
943 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
944 EShdr->sh_type == ELF::SHT_NOBITS;
945}
946
947template <class ELFT>
948std::vector<SectionRef>
950 std::vector<SectionRef> Res;
951 std::vector<uintptr_t> Offsets;
952
953 auto SectionsOrErr = EF.sections();
954 if (!SectionsOrErr)
955 return Res;
956
957 for (const Elf_Shdr &Sec : *SectionsOrErr) {
958 if (Sec.sh_type != ELF::SHT_DYNAMIC)
959 continue;
960 Elf_Dyn *Dynamic =
961 reinterpret_cast<Elf_Dyn *>((uintptr_t)base() + Sec.sh_offset);
962 for (; Dynamic->d_tag != ELF::DT_NULL; Dynamic++) {
963 if (Dynamic->d_tag == ELF::DT_REL || Dynamic->d_tag == ELF::DT_RELA ||
964 Dynamic->d_tag == ELF::DT_JMPREL) {
965 Offsets.push_back(Dynamic->d_un.d_val);
966 }
967 }
968 }
969 for (const Elf_Shdr &Sec : *SectionsOrErr) {
970 if (is_contained(Offsets, Sec.sh_addr))
971 Res.emplace_back(toDRI(&Sec), this);
972 }
973 return Res;
974}
975
976template <class ELFT>
978 return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
979}
980
981template <class ELFT>
983 return getSection(Sec)->sh_flags & ELF::SHF_ALLOC &&
984 (getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR ||
985 !(getSection(Sec)->sh_flags & ELF::SHF_WRITE));
986}
987
988template <class ELFT>
990 const Elf_Shdr *EShdr = getSection(Sec);
991 return !isBerkeleyText(Sec) && EShdr->sh_type != ELF::SHT_NOBITS &&
992 EShdr->sh_flags & ELF::SHF_ALLOC;
993}
994
995template <class ELFT>
997 Expected<StringRef> SectionNameOrErr = getSectionName(Sec);
998 if (!SectionNameOrErr) {
999 // TODO: Report the error message properly.
1000 consumeError(SectionNameOrErr.takeError());
1001 return false;
1002 }
1003 StringRef SectionName = SectionNameOrErr.get();
1004 return SectionName.starts_with(".debug") ||
1005 SectionName.starts_with(".zdebug") || SectionName == ".gdb_index";
1006}
1007
1008template <class ELFT>
1011 DataRefImpl RelData;
1012 auto SectionsOrErr = EF.sections();
1013 if (!SectionsOrErr)
1015 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
1016 RelData.d.a = (Sec.p - SHT) / EF.getHeader().e_shentsize;
1017 RelData.d.b = 0;
1018 return relocation_iterator(RelocationRef(RelData, this));
1019}
1020
1021template <class ELFT>
1024 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
1025 relocation_iterator Begin = section_rel_begin(Sec);
1026 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
1027 return Begin;
1028 DataRefImpl RelData = Begin->getRawDataRefImpl();
1029 const Elf_Shdr *RelSec = getRelSection(RelData);
1030
1031 // Error check sh_link here so that getRelocationSymbol can just use it.
1032 auto SymSecOrErr = EF.getSection(RelSec->sh_link);
1033 if (!SymSecOrErr)
1035 Twine(errorToErrorCode(SymSecOrErr.takeError()).message()));
1036
1037 RelData.d.b += S->sh_size / S->sh_entsize;
1038 return relocation_iterator(RelocationRef(RelData, this));
1039}
1040
1041template <class ELFT>
1044 const Elf_Shdr *EShdr = getSection(Sec);
1045 uintX_t Type = EShdr->sh_type;
1046 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
1047 return section_end();
1048
1049 Expected<const Elf_Shdr *> SecOrErr = EF.getSection(EShdr->sh_info);
1050 if (!SecOrErr)
1051 return SecOrErr.takeError();
1052 return section_iterator(SectionRef(toDRI(*SecOrErr), this));
1053}
1054
1055// Relocations
1056template <class ELFT>
1058 ++Rel.d.b;
1059}
1060
1061template <class ELFT>
1064 uint32_t symbolIdx;
1065 const Elf_Shdr *sec = getRelSection(Rel);
1066 if (sec->sh_type == ELF::SHT_REL)
1067 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
1068 else
1069 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
1070 if (!symbolIdx)
1071 return symbol_end();
1072
1073 // FIXME: error check symbolIdx
1074 DataRefImpl SymbolData;
1075 SymbolData.d.a = sec->sh_link;
1076 SymbolData.d.b = symbolIdx;
1077 return symbol_iterator(SymbolRef(SymbolData, this));
1078}
1079
1080template <class ELFT>
1082 const Elf_Shdr *sec = getRelSection(Rel);
1083 if (sec->sh_type == ELF::SHT_REL)
1084 return getRel(Rel)->r_offset;
1085
1086 return getRela(Rel)->r_offset;
1087}
1088
1089template <class ELFT>
1091 const Elf_Shdr *sec = getRelSection(Rel);
1092 if (sec->sh_type == ELF::SHT_REL)
1093 return getRel(Rel)->getType(EF.isMips64EL());
1094 else
1095 return getRela(Rel)->getType(EF.isMips64EL());
1096}
1097
1098template <class ELFT>
1100 return getELFRelocationTypeName(EF.getHeader().e_machine, Type);
1101}
1102
1103template <class ELFT>
1105 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1106 uint32_t type = getRelocationType(Rel);
1107 EF.getRelocationTypeName(type, Result);
1108}
1109
1110template <class ELFT>
1113 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
1114 return createError("Section is not SHT_RELA");
1115 return (int64_t)getRela(Rel)->r_addend;
1116}
1117
1118template <class ELFT>
1119const typename ELFObjectFile<ELFT>::Elf_Rel *
1121 assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
1122 auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
1123 if (!Ret)
1124 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message()));
1125 return *Ret;
1126}
1127
1128template <class ELFT>
1129const typename ELFObjectFile<ELFT>::Elf_Rela *
1131 assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
1132 auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
1133 if (!Ret)
1134 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message()));
1135 return *Ret;
1136}
1137
1138template <class ELFT>
1141 auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer());
1142 if (Error E = EFOrErr.takeError())
1143 return std::move(E);
1144
1145 ELFObjectFile<ELFT> Obj = {Object, std::move(*EFOrErr), nullptr, nullptr,
1146 nullptr};
1147 if (InitContent)
1148 if (Error E = Obj.initContent())
1149 return std::move(E);
1150 return std::move(Obj);
1151}
1152
1153template <class ELFT>
1155 const Elf_Shdr *DotDynSymSec,
1156 const Elf_Shdr *DotSymtabSec,
1157 const Elf_Shdr *DotSymtabShndx)
1159 getELFType(ELFT::TargetEndianness == llvm::endianness::little,
1160 ELFT::Is64Bits),
1161 Object),
1162 EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
1163 DotSymtabShndxSec(DotSymtabShndx) {}
1164
1165template <class ELFT>
1167 : ELFObjectFile(Other.Data, Other.EF, Other.DotDynSymSec,
1168 Other.DotSymtabSec, Other.DotSymtabShndxSec) {}
1169
1170template <class ELFT>
1173 toDRI(DotSymtabSec,
1174 DotSymtabSec && DotSymtabSec->sh_size >= sizeof(Elf_Sym) ? 1 : 0);
1175 return basic_symbol_iterator(SymbolRef(Sym, this));
1176}
1177
1178template <class ELFT>
1180 const Elf_Shdr *SymTab = DotSymtabSec;
1181 if (!SymTab)
1182 return symbol_begin();
1183 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1184 return basic_symbol_iterator(SymbolRef(Sym, this));
1185}
1186
1187template <class ELFT>
1189 if (!DotDynSymSec || DotDynSymSec->sh_size < sizeof(Elf_Sym))
1190 // Ignore errors here where the dynsym is empty or sh_size less than the
1191 // size of one symbol. These should be handled elsewhere.
1192 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 0), this));
1193 // Skip 0-index NULL symbol.
1194 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 1), this));
1195}
1196
1197template <class ELFT>
1199 const Elf_Shdr *SymTab = DotDynSymSec;
1200 if (!SymTab)
1201 return dynamic_symbol_begin();
1202 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1203 return basic_symbol_iterator(SymbolRef(Sym, this));
1204}
1205
1206template <class ELFT>
1208 auto SectionsOrErr = EF.sections();
1209 if (!SectionsOrErr)
1210 return section_iterator(SectionRef());
1211 return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this));
1212}
1213
1214template <class ELFT>
1216 auto SectionsOrErr = EF.sections();
1217 if (!SectionsOrErr)
1218 return section_iterator(SectionRef());
1219 return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
1220}
1221
1222template <class ELFT>
1224 return ELFT::Is64Bits ? 8 : 4;
1225}
1226
1227template <class ELFT>
1229 constexpr bool IsLittleEndian =
1230 ELFT::TargetEndianness == llvm::endianness::little;
1231 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1232 case ELF::ELFCLASS32:
1233 switch (EF.getHeader().e_machine) {
1234 case ELF::EM_68K:
1235 return "elf32-m68k";
1236 case ELF::EM_386:
1237 return "elf32-i386";
1238 case ELF::EM_IAMCU:
1239 return "elf32-iamcu";
1240 case ELF::EM_X86_64:
1241 return "elf32-x86-64";
1242 case ELF::EM_ARM:
1243 return (IsLittleEndian ? "elf32-littlearm" : "elf32-bigarm");
1244 case ELF::EM_AVR:
1245 return "elf32-avr";
1246 case ELF::EM_HEXAGON:
1247 return "elf32-hexagon";
1248 case ELF::EM_LANAI:
1249 return "elf32-lanai";
1250 case ELF::EM_MIPS:
1251 return "elf32-mips";
1252 case ELF::EM_MSP430:
1253 return "elf32-msp430";
1254 case ELF::EM_PPC:
1255 return (IsLittleEndian ? "elf32-powerpcle" : "elf32-powerpc");
1256 case ELF::EM_RISCV:
1257 return "elf32-littleriscv";
1258 case ELF::EM_CSKY:
1259 return "elf32-csky";
1260 case ELF::EM_SPARC:
1262 return "elf32-sparc";
1263 case ELF::EM_AMDGPU:
1264 return "elf32-amdgpu";
1265 case ELF::EM_LOONGARCH:
1266 return "elf32-loongarch";
1267 case ELF::EM_XTENSA:
1268 return "elf32-xtensa";
1269 default:
1270 return "elf32-unknown";
1271 }
1272 case ELF::ELFCLASS64:
1273 switch (EF.getHeader().e_machine) {
1274 case ELF::EM_386:
1275 return "elf64-i386";
1276 case ELF::EM_X86_64:
1277 return "elf64-x86-64";
1278 case ELF::EM_AARCH64:
1279 return (IsLittleEndian ? "elf64-littleaarch64" : "elf64-bigaarch64");
1280 case ELF::EM_PPC64:
1281 return (IsLittleEndian ? "elf64-powerpcle" : "elf64-powerpc");
1282 case ELF::EM_RISCV:
1283 return "elf64-littleriscv";
1284 case ELF::EM_S390:
1285 return "elf64-s390";
1286 case ELF::EM_SPARCV9:
1287 return "elf64-sparc";
1288 case ELF::EM_MIPS:
1289 return "elf64-mips";
1290 case ELF::EM_AMDGPU:
1291 return "elf64-amdgpu";
1292 case ELF::EM_BPF:
1293 return "elf64-bpf";
1294 case ELF::EM_VE:
1295 return "elf64-ve";
1296 case ELF::EM_LOONGARCH:
1297 return "elf64-loongarch";
1298 default:
1299 return "elf64-unknown";
1300 }
1301 default:
1302 // FIXME: Proper error handling.
1303 report_fatal_error("Invalid ELFCLASS!");
1304 }
1305}
1306
1307template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
1308 bool IsLittleEndian = ELFT::TargetEndianness == llvm::endianness::little;
1309 switch (EF.getHeader().e_machine) {
1310 case ELF::EM_68K:
1311 return Triple::m68k;
1312 case ELF::EM_386:
1313 case ELF::EM_IAMCU:
1314 return Triple::x86;
1315 case ELF::EM_X86_64:
1316 return Triple::x86_64;
1317 case ELF::EM_AARCH64:
1318 return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be;
1319 case ELF::EM_ARM:
1320 return Triple::arm;
1321 case ELF::EM_AVR:
1322 return Triple::avr;
1323 case ELF::EM_HEXAGON:
1324 return Triple::hexagon;
1325 case ELF::EM_LANAI:
1326 return Triple::lanai;
1327 case ELF::EM_MIPS:
1328 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1329 case ELF::ELFCLASS32:
1330 return IsLittleEndian ? Triple::mipsel : Triple::mips;
1331 case ELF::ELFCLASS64:
1332 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
1333 default:
1334 report_fatal_error("Invalid ELFCLASS!");
1335 }
1336 case ELF::EM_MSP430:
1337 return Triple::msp430;
1338 case ELF::EM_PPC:
1339 return IsLittleEndian ? Triple::ppcle : Triple::ppc;
1340 case ELF::EM_PPC64:
1341 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
1342 case ELF::EM_RISCV:
1343 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1344 case ELF::ELFCLASS32:
1345 return Triple::riscv32;
1346 case ELF::ELFCLASS64:
1347 return Triple::riscv64;
1348 default:
1349 report_fatal_error("Invalid ELFCLASS!");
1350 }
1351 case ELF::EM_S390:
1352 return Triple::systemz;
1353
1354 case ELF::EM_SPARC:
1356 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
1357 case ELF::EM_SPARCV9:
1358 return Triple::sparcv9;
1359
1360 case ELF::EM_AMDGPU: {
1361 if (!IsLittleEndian)
1362 return Triple::UnknownArch;
1363
1364 unsigned MACH = EF.getHeader().e_flags & ELF::EF_AMDGPU_MACH;
1365 if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST &&
1367 return Triple::r600;
1370 return Triple::amdgcn;
1371
1372 return Triple::UnknownArch;
1373 }
1374
1375 case ELF::EM_CUDA: {
1376 if (EF.getHeader().e_ident[ELF::EI_CLASS] == ELF::ELFCLASS32)
1377 return Triple::nvptx;
1378 return Triple::nvptx64;
1379 }
1380
1381 case ELF::EM_BPF:
1382 return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
1383
1384 case ELF::EM_VE:
1385 return Triple::ve;
1386 case ELF::EM_CSKY:
1387 return Triple::csky;
1388
1389 case ELF::EM_LOONGARCH:
1390 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1391 case ELF::ELFCLASS32:
1392 return Triple::loongarch32;
1393 case ELF::ELFCLASS64:
1394 return Triple::loongarch64;
1395 default:
1396 report_fatal_error("Invalid ELFCLASS!");
1397 }
1398
1399 case ELF::EM_XTENSA:
1400 return Triple::xtensa;
1401
1402 default:
1403 return Triple::UnknownArch;
1404 }
1405}
1406
1407template <class ELFT> Triple::OSType ELFObjectFile<ELFT>::getOS() const {
1408 switch (EF.getHeader().e_ident[ELF::EI_OSABI]) {
1410 return Triple::NetBSD;
1412 return Triple::Linux;
1413 case ELF::ELFOSABI_HURD:
1414 return Triple::Hurd;
1416 return Triple::Solaris;
1417 case ELF::ELFOSABI_AIX:
1418 return Triple::AIX;
1420 return Triple::FreeBSD;
1422 return Triple::OpenBSD;
1423 case ELF::ELFOSABI_CUDA:
1424 return Triple::CUDA;
1426 return Triple::AMDHSA;
1428 return Triple::AMDPAL;
1430 return Triple::Mesa3D;
1431 default:
1432 return Triple::UnknownOS;
1433 }
1434}
1435
1436template <class ELFT>
1438 return EF.getHeader().e_entry;
1439}
1440
1441template <class ELFT>
1444 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
1445}
1446
1447template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
1448 return EF.getHeader().e_type == ELF::ET_REL;
1449}
1450
1451} // end namespace object
1452} // end namespace llvm
1453
1454#endif // LLVM_OBJECT_ELFOBJECTFILE_H
aarch64 promote const
AMDGPU Kernel Attributes
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static StringRef getSymbolName(SymbolKind SymKind)
std::string Name
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Definition: ELFTypes.h:104
Symbol * Sym
Definition: ELF_riscv.cpp:479
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
static uint64_t getSymbolValue(const MCSymbol &Symbol, const MCAsmLayout &Layout)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Base class for error info classes.
Definition: Error.h:45
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
reference get()
Returns a reference to the stored T value.
Definition: Error.h:571
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Manages the enabling and disabling of subtarget specific features.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
@ loongarch32
Definition: Triple.h:61
@ UnknownArch
Definition: Triple.h:47
@ aarch64_be
Definition: Triple.h:52
@ loongarch64
Definition: Triple.h:62
@ mips64el
Definition: Triple.h:67
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
A range adaptor for a pair of iterators.
const SymbolicFile * getObject() const
Definition: SymbolicFile.h:214
DataRefImpl getRawDataRefImpl() const
Definition: SymbolicFile.h:210
static unsigned int getELFType(bool isLE, bool is64Bits)
Definition: Binary.h:78
static Expected< ELFFile > create(StringRef Object)
Definition: ELF.h:839
virtual uint64_t getSymbolSize(DataRefImpl Symb) const =0
virtual uint8_t getEIdentABIVersion() const =0
virtual Error getBuildAttributes(ELFAttributeParser &Attributes) const =0
virtual uint64_t getSectionFlags(DataRefImpl Sec) const =0
virtual uint16_t getEType() const =0
virtual uint8_t getSymbolELFType(DataRefImpl Symb) const =0
Expected< std::vector< VersionEntry > > readDynsymVersions() const
Returns a vector containing a symbol version for each dynamic symbol.
virtual uint8_t getSymbolOther(DataRefImpl Symb) const =0
virtual elf_symbol_iterator_range getDynamicSymbolIterators() const =0
virtual uint32_t getSectionType(DataRefImpl Sec) const =0
elf_symbol_iterator_range symbols() const
virtual Expected< int64_t > getRelocationAddend(DataRefImpl Rel) const =0
std::vector< ELFPltEntry > getPltEntries() const
Expected< SubtargetFeatures > getFeatures() const override
virtual uint8_t getSymbolBinding(DataRefImpl Symb) const =0
std::optional< StringRef > tryGetCPUName() const override
iterator_range< elf_symbol_iterator > elf_symbol_iterator_range
Definition: ELFObjectFile.h:86
virtual uint16_t getEMachine() const =0
static bool classof(const Binary *v)
Definition: ELFObjectFile.h:95
virtual unsigned getPlatformFlags() const =0
Returns platform-specific object flags, if any.
void setARMSubArch(Triple &TheTriple) const override
virtual uint64_t getSectionOffset(DataRefImpl Sec) const =0
Expected< std::vector< BBAddrMap > > readBBAddrMap(std::optional< unsigned > TextSectionIndex=std::nullopt, std::vector< PGOAnalysisMap > *PGOAnalyses=nullptr) const
Returns a vector of all BB address maps in the object file.
static bool classof(const Binary *v)
const ELFFile< ELFT > & getELFFile() const
Expected< StringRef > getSectionName(DataRefImpl Sec) const override
std::vector< SectionRef > dynamic_relocation_sections() const override
uint64_t getRelocationType(DataRefImpl Rel) const override
bool isSectionText(DataRefImpl Sec) const override
uint8_t getSymbolELFType(DataRefImpl Symb) const override
uint64_t getSectionAlignment(DataRefImpl Sec) const override
bool is64Bit() const override
DataRefImpl toDRI(const Elf_Dyn *Dyn) const
bool isSectionVirtual(DataRefImpl Sec) const override
Triple::OSType getOS() const override
SectionRef toSectionRef(const Elf_Shdr *Sec) const
uint32_t getSectionType(DataRefImpl Sec) const override
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
elf_symbol_iterator_range getDynamicSymbolIterators() const override
Expected< const Elf_Sym * > getSymbol(DataRefImpl Sym) const
const Elf_Rel * getRel(DataRefImpl Rel) const
Expected< section_iterator > getSymbolSection(const Elf_Sym *Symb, const Elf_Shdr *SymTab) const
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override
basic_symbol_iterator symbol_begin() const override
Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
ELFSymbolRef toSymbolRef(const Elf_Shdr *SymTable, unsigned SymbolNum) const
const Elf_Rela * getRela(DataRefImpl Rela) const
static Expected< ELFObjectFile< ELFT > > create(MemoryBufferRef Object, bool InitContent=true)
bool isExportedToOtherDSO(const Elf_Sym *ESym) const
uint64_t getSectionAddress(DataRefImpl Sec) const override
Expected< ArrayRef< uint8_t > > getSectionContents(DataRefImpl Sec) const override
void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const override
uint64_t getSectionIndex(DataRefImpl Sec) const override
Expected< uint32_t > getSymbolFlags(DataRefImpl Symb) const override
bool isSectionData(DataRefImpl Sec) const override
const Elf_Shdr * DotSymtabSec
const Elf_Shdr * DotDynSymSec
uint32_t getSymbolAlignment(DataRefImpl Symb) const override
const Elf_Shdr * toELFShdrIter(DataRefImpl Sec) const
Triple::ArchType getArch() const override
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
uint64_t getSectionSize(DataRefImpl Sec) const override
bool isBerkeleyData(DataRefImpl Sec) const override
StringRef getFileFormatName() const override
bool isRelocatableObject() const override
True if this is a relocatable object (.o/.obj).
void moveSymbolNext(DataRefImpl &Symb) const override
uint8_t getSymbolOther(DataRefImpl Symb) const override
section_iterator section_end() const override
const Elf_Shdr * getRelSection(DataRefImpl Rel) const
Get the relocation section that contains Rel.
Expected< int64_t > getRelocationAddend(DataRefImpl Rel) const override
uint64_t getRelocationOffset(DataRefImpl Rel) const override
Expected< section_iterator > getRelocatedSection(DataRefImpl Sec) const override
void moveSectionNext(DataRefImpl &Sec) const override
bool isSectionBSS(DataRefImpl Sec) const override
unsigned getPlatformFlags() const override
Returns platform-specific object flags, if any.
uint64_t getSectionFlags(DataRefImpl Sec) const override
void moveRelocationNext(DataRefImpl &Rel) const override
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
Expected< StringRef > getSymbolName(DataRefImpl Symb) const override
Error initContent() override
basic_symbol_iterator symbol_end() const override
uint8_t getSymbolBinding(DataRefImpl Symb) const override
uint64_t getSectionOffset(DataRefImpl Sec) const override
relocation_iterator section_rel_end(DataRefImpl Sec) const override
Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const override
const Elf_Shdr * getSection(DataRefImpl Sec) const
Error getBuildAttributes(ELFAttributeParser &Attributes) const override
DataRefImpl toDRI(const Elf_Shdr *Sec) const
elf_symbol_iterator dynamic_symbol_begin() const
const Elf_Shdr * DotSymtabShndxSec
elf_symbol_iterator dynamic_symbol_end() const
DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const
bool isDebugSection(DataRefImpl Sec) const override
section_iterator section_begin() const override
bool isSectionCompressed(DataRefImpl Sec) const override
bool isBerkeleyText(DataRefImpl Sec) const override
uint8_t getBytesInAddress() const override
The number of bytes used to represent an address in this object file format.
Expected< uint64_t > getStartAddress() const override
Expected< int64_t > getAddend() const
const ELFObjectFileBase * getObject() const
ELFRelocationRef(const RelocationRef &B)
const ELFObjectFileBase * getObject() const
uint64_t getOffset() const
ELFSectionRef(const SectionRef &B)
const ELFObjectFileBase * getObject() const
uint8_t getELFType() const
ELFSymbolRef(const SymbolRef &B)
uint8_t getBinding() const
uint64_t getSize() const
StringRef getELFTypeName() const
This class is the base class for all object file types.
Definition: ObjectFile.h:229
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:52
const ObjectFile * getObject() const
Definition: ObjectFile.h:639
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:635
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:597
const ObjectFile * getObject() const
Definition: ObjectFile.h:601
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: ObjectFile.h:168
const ObjectFile * getObject() const
Definition: ObjectFile.h:487
virtual basic_symbol_iterator symbol_begin() const =0
virtual basic_symbol_iterator symbol_end() const =0
const SectionRef & operator*() const
Definition: SymbolicFile.h:83
const SectionRef * operator->() const
Definition: SymbolicFile.h:81
const ELFRelocationRef & operator*() const
elf_relocation_iterator(const relocation_iterator &B)
const ELFRelocationRef * operator->() const
elf_section_iterator(const section_iterator &B)
const ELFSectionRef * operator->() const
const ELFSectionRef & operator*() const
const ELFSymbolRef & operator*() const
elf_symbol_iterator(const basic_symbol_iterator &B)
const ELFSymbolRef * operator->() const
const SymbolRef * operator->() const
Definition: ObjectFile.h:215
const SymbolRef & operator*() const
Definition: ObjectFile.h:220
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
@ SHF_ALLOC
Definition: ELF.h:1157
@ SHF_COMPRESSED
Definition: ELF.h:1185
@ SHF_WRITE
Definition: ELF.h:1154
@ SHF_EXECINSTR
Definition: ELF.h:1160
@ EM_MSP430
Definition: ELF.h:222
@ EM_S390
Definition: ELF.h:150
@ EM_PPC64
Definition: ELF.h:149
@ EM_SPARC
Definition: ELF.h:135
@ EM_CSKY
Definition: ELF.h:321
@ EM_SPARC32PLUS
Definition: ELF.h:146
@ EM_68K
Definition: ELF.h:137
@ EM_386
Definition: ELF.h:136
@ EM_CUDA
Definition: ELF.h:286
@ EM_LOONGARCH
Definition: ELF.h:322
@ EM_BPF
Definition: ELF.h:319
@ EM_PPC
Definition: ELF.h:148
@ EM_X86_64
Definition: ELF.h:178
@ EM_HEXAGON
Definition: ELF.h:257
@ EM_LANAI
Definition: ELF.h:318
@ EM_MIPS
Definition: ELF.h:141
@ EM_SPARCV9
Definition: ELF.h:159
@ EM_AARCH64
Definition: ELF.h:280
@ EM_XTENSA
Definition: ELF.h:211
@ EM_RISCV
Definition: ELF.h:317
@ EM_ARM
Definition: ELF.h:156
@ EM_VE
Definition: ELF.h:320
@ EM_IAMCU
Definition: ELF.h:139
@ EM_AMDGPU
Definition: ELF.h:316
@ EM_AVR
Definition: ELF.h:199
@ STV_HIDDEN
Definition: ELF.h:1342
@ STV_PROTECTED
Definition: ELF.h:1343
@ STV_DEFAULT
Definition: ELF.h:1340
@ ELFCLASS64
Definition: ELF.h:329
@ ELFCLASS32
Definition: ELF.h:328
@ EF_AMDGPU_MACH_AMDGCN_LAST
Definition: ELF.h:803
@ EF_AMDGPU_MACH_R600_LAST
Definition: ELF.h:741
@ EF_AMDGPU_MACH_AMDGCN_FIRST
Definition: ELF.h:802
@ EF_AMDGPU_MACH
Definition: ELF.h:707
@ EF_AMDGPU_MACH_R600_FIRST
Definition: ELF.h:740
@ ELFOSABI_HURD
Definition: ELF.h:346
@ ELFOSABI_CUDA
Definition: ELF.h:359
@ ELFOSABI_OPENBSD
Definition: ELF.h:353
@ ELFOSABI_NETBSD
Definition: ELF.h:343
@ ELFOSABI_AMDGPU_HSA
Definition: ELF.h:361
@ ELFOSABI_SOLARIS
Definition: ELF.h:347
@ ELFOSABI_FREEBSD
Definition: ELF.h:350
@ ELFOSABI_LINUX
Definition: ELF.h:345
@ ELFOSABI_AIX
Definition: ELF.h:348
@ ELFOSABI_AMDGPU_MESA3D
Definition: ELF.h:363
@ ELFOSABI_AMDGPU_PAL
Definition: ELF.h:362
@ SHN_ABS
Definition: ELF.h:1054
@ SHN_COMMON
Definition: ELF.h:1055
@ SHN_UNDEF
Definition: ELF.h:1048
@ SHT_PROGBITS
Definition: ELF.h:1063
@ SHT_REL
Definition: ELF.h:1071
@ SHT_ARM_ATTRIBUTES
Definition: ELF.h:1119
@ SHT_NOBITS
Definition: ELF.h:1070
@ SHT_SYMTAB
Definition: ELF.h:1064
@ SHT_HEXAGON_ATTRIBUTES
Definition: ELF.h:1144
@ SHT_DYNAMIC
Definition: ELF.h:1068
@ SHT_SYMTAB_SHNDX
Definition: ELF.h:1078
@ SHT_RISCV_ATTRIBUTES
Definition: ELF.h:1140
@ SHT_RELA
Definition: ELF.h:1066
@ SHT_DYNSYM
Definition: ELF.h:1073
@ EI_ABIVERSION
Definition: ELF.h:56
@ EI_CLASS
Definition: ELF.h:52
@ EI_OSABI
Definition: ELF.h:55
@ STB_GLOBAL
Definition: ELF.h:1311
@ STB_LOCAL
Definition: ELF.h:1310
@ STB_GNU_UNIQUE
Definition: ELF.h:1313
@ STB_WEAK
Definition: ELF.h:1312
@ ET_REL
Definition: ELF.h:116
@ STT_FUNC
Definition: ELF.h:1324
@ STT_NOTYPE
Definition: ELF.h:1322
@ STT_SECTION
Definition: ELF.h:1325
@ STT_FILE
Definition: ELF.h:1326
@ STT_COMMON
Definition: ELF.h:1327
@ STT_GNU_IFUNC
Definition: ELF.h:1329
@ STT_OBJECT
Definition: ELF.h:1323
@ STT_TLS
Definition: ELF.h:1328
static constexpr const StringLiteral & getSectionName(DebugSectionKind SectionKind)
Return the name of the section.
static Expected< const T * > getObject(MemoryBufferRef M, const void *Ptr, const uint64_t Size=sizeof(T))
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Definition: ELF.h:485
Error createError(const Twine &Err)
Definition: Error.h:84
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
Definition: ELF.cpp:23
constexpr int NumElfSymbolTypes
Definition: ELFObjectFile.h:45
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:47
content_iterator< RelocationRef > relocation_iterator
Definition: ObjectFile.h:77
content_iterator< BasicSymbolRef > basic_symbol_iterator
Definition: SymbolicFile.h:143
const llvm::EnumEntry< unsigned > ElfSymbolTypes[NumElfSymbolTypes]
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:970
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
@ Other
Any other memory.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
@ Dynamic
Denotes mode unknown at compile time.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition: Casting.h:565
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1888
endianness
Definition: bit.h:70
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:109
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1041
std::optional< DataRefImpl > Symbol
Definition: ELFObjectFile.h:52
struct llvm::object::DataRefImpl::@353 d