LLVM 17.0.0git
ELFObjectFile.h
Go to the documentation of this file.
1//===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the ELFObjectFile template class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_OBJECT_ELFOBJECTFILE_H
14#define LLVM_OBJECT_ELFOBJECTFILE_H
15
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/StringRef.h"
22#include "llvm/Object/Binary.h"
23#include "llvm/Object/ELF.h"
25#include "llvm/Object/Error.h"
31#include "llvm/Support/Endian.h"
32#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
52 friend class ELFRelocationRef;
53 friend class ELFSectionRef;
54 friend class ELFSymbolRef;
55
56 SubtargetFeatures getMIPSFeatures() const;
57 SubtargetFeatures getARMFeatures() const;
58 Expected<SubtargetFeatures> getRISCVFeatures() const;
59 SubtargetFeatures getLoongArchFeatures() const;
60
61 StringRef getAMDGPUCPUName() const;
62
63protected:
64 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
65
66 virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
67 virtual uint8_t getSymbolBinding(DataRefImpl Symb) const = 0;
68 virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
69 virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
70
71 virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
72 virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
73 virtual uint64_t getSectionOffset(DataRefImpl Sec) const = 0;
74
76 virtual Error getBuildAttributes(ELFAttributeParser &Attributes) const = 0;
77
78public:
80
82
83 /// Returns platform-specific object flags, if any.
84 virtual unsigned getPlatformFlags() const = 0;
85
87
88 static bool classof(const Binary *v) { return v->isELF(); }
89
91
92 std::optional<StringRef> tryGetCPUName() const override;
93
94 void setARMSubArch(Triple &TheTriple) const override;
95
96 virtual uint16_t getEType() const = 0;
97
98 virtual uint16_t getEMachine() const = 0;
99
100 std::vector<std::pair<std::optional<DataRefImpl>, uint64_t>>
101 getPltAddresses() const;
102
103 /// Returns a vector containing a symbol version for each dynamic symbol.
104 /// Returns an empty vector if version sections do not exist.
106
107 /// Returns a vector of all BB address maps in the object file. When
108 // `TextSectionIndex` is specified, only returns the BB address maps
109 // corresponding to the section with that index.
111 readBBAddrMap(std::optional<unsigned> TextSectionIndex = std::nullopt) const;
112};
113
114class ELFSectionRef : public SectionRef {
115public:
117 assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
118 }
119
121 return cast<ELFObjectFileBase>(SectionRef::getObject());
122 }
123
126 }
127
130 }
131
134 }
135};
136
138public:
140 assert(isa<ELFObjectFileBase>(B->getObject()));
141 }
142
143 const ELFSectionRef *operator->() const {
144 return static_cast<const ELFSectionRef *>(section_iterator::operator->());
145 }
146
147 const ELFSectionRef &operator*() const {
148 return static_cast<const ELFSectionRef &>(section_iterator::operator*());
149 }
150};
151
152class ELFSymbolRef : public SymbolRef {
153public:
155 assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
156 }
157
159 return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
160 }
161
164 }
165
166 uint8_t getBinding() const {
168 }
169
170 uint8_t getOther() const {
172 }
173
174 uint8_t getELFType() const {
176 }
177
179 uint8_t Type = getELFType();
180 for (const auto &EE : ElfSymbolTypes) {
181 if (EE.Value == Type) {
182 return EE.AltName;
183 }
184 }
185 return "";
186 }
187};
188
190public:
192 : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
194
195 const ELFSymbolRef *operator->() const {
196 return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
197 }
198
199 const ELFSymbolRef &operator*() const {
200 return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
201 }
202};
203
205public:
207 assert(isa<ELFObjectFileBase>(RelocationRef::getObject()));
208 }
209
211 return cast<ELFObjectFileBase>(RelocationRef::getObject());
212 }
213
216 }
217};
218
220public:
223 B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {}
224
226 return static_cast<const ELFRelocationRef *>(
228 }
229
231 return static_cast<const ELFRelocationRef &>(
233 }
234};
235
239}
240
241template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
242 uint16_t getEMachine() const override;
243 uint16_t getEType() const override;
244 uint64_t getSymbolSize(DataRefImpl Sym) const override;
245
246public:
248
249 SectionRef toSectionRef(const Elf_Shdr *Sec) const {
250 return SectionRef(toDRI(Sec), this);
251 }
252
253 ELFSymbolRef toSymbolRef(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
254 return ELFSymbolRef({toDRI(SymTable, SymbolNum), this});
255 }
256
257 bool IsContentValid() const { return ContentValid; }
258
259private:
261 const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,
262 const Elf_Shdr *DotSymtabShndxSec);
263
264 bool ContentValid = false;
265
266protected:
268
269 const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
270 const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
271 const Elf_Shdr *DotSymtabShndxSec = nullptr; // SHT_SYMTAB_SHNDX section.
272
273 Error initContent() override;
274
275 void moveSymbolNext(DataRefImpl &Symb) const override;
276 Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
278 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
279 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
280 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
281 Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override;
282 uint8_t getSymbolBinding(DataRefImpl Symb) const override;
283 uint8_t getSymbolOther(DataRefImpl Symb) const override;
284 uint8_t getSymbolELFType(DataRefImpl Symb) const override;
287 const Elf_Shdr *SymTab) const;
289
290 void moveSectionNext(DataRefImpl &Sec) const override;
292 uint64_t getSectionAddress(DataRefImpl Sec) const override;
293 uint64_t getSectionIndex(DataRefImpl Sec) const override;
294 uint64_t getSectionSize(DataRefImpl Sec) const override;
296 getSectionContents(DataRefImpl Sec) const override;
297 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
298 bool isSectionCompressed(DataRefImpl Sec) const override;
299 bool isSectionText(DataRefImpl Sec) const override;
300 bool isSectionData(DataRefImpl Sec) const override;
301 bool isSectionBSS(DataRefImpl Sec) const override;
302 bool isSectionVirtual(DataRefImpl Sec) const override;
303 bool isBerkeleyText(DataRefImpl Sec) const override;
304 bool isBerkeleyData(DataRefImpl Sec) const override;
305 bool isDebugSection(DataRefImpl Sec) const override;
308 std::vector<SectionRef> dynamic_relocation_sections() const override;
310 getRelocatedSection(DataRefImpl Sec) const override;
311
312 void moveRelocationNext(DataRefImpl &Rel) const override;
313 uint64_t getRelocationOffset(DataRefImpl Rel) const override;
315 uint64_t getRelocationType(DataRefImpl Rel) const override;
317 SmallVectorImpl<char> &Result) const override;
318
319 uint32_t getSectionType(DataRefImpl Sec) const override;
320 uint64_t getSectionFlags(DataRefImpl Sec) const override;
321 uint64_t getSectionOffset(DataRefImpl Sec) const override;
323
324 DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
325 DataRefImpl DRI;
326 if (!SymTable) {
327 DRI.d.a = 0;
328 DRI.d.b = 0;
329 return DRI;
330 }
331 assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
332 SymTable->sh_type == ELF::SHT_DYNSYM);
333
334 auto SectionsOrErr = EF.sections();
335 if (!SectionsOrErr) {
336 DRI.d.a = 0;
337 DRI.d.b = 0;
338 return DRI;
339 }
340 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
341 unsigned SymTableIndex =
342 (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
343
344 DRI.d.a = SymTableIndex;
345 DRI.d.b = SymbolNum;
346 return DRI;
347 }
348
349 const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
350 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
351 }
352
353 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
354 DataRefImpl DRI;
355 DRI.p = reinterpret_cast<uintptr_t>(Sec);
356 return DRI;
357 }
358
359 DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
360 DataRefImpl DRI;
361 DRI.p = reinterpret_cast<uintptr_t>(Dyn);
362 return DRI;
363 }
364
365 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
366 unsigned char Binding = ESym->getBinding();
367 unsigned char Visibility = ESym->getVisibility();
368
369 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
370 // visibility is either DEFAULT or PROTECTED. All other symbols are not
371 // exported.
372 return (
373 (Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK ||
374 Binding == ELF::STB_GNU_UNIQUE) &&
375 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED));
376 }
377
378 Error getBuildAttributes(ELFAttributeParser &Attributes) const override {
379 auto SectionsOrErr = EF.sections();
380 if (!SectionsOrErr)
381 return SectionsOrErr.takeError();
382
383 for (const Elf_Shdr &Sec : *SectionsOrErr) {
384 if (Sec.sh_type == ELF::SHT_ARM_ATTRIBUTES ||
385 Sec.sh_type == ELF::SHT_RISCV_ATTRIBUTES) {
386 auto ErrorOrContents = EF.getSectionContents(Sec);
387 if (!ErrorOrContents)
388 return ErrorOrContents.takeError();
389
390 auto Contents = ErrorOrContents.get();
391 if (Contents[0] != ELFAttrs::Format_Version || Contents.size() == 1)
392 return Error::success();
393
394 if (Error E = Attributes.parse(Contents, ELFT::TargetEndianness))
395 return E;
396 break;
397 }
398 }
399 return Error::success();
400 }
401
402 // This flag is used for classof, to distinguish ELFObjectFile from
403 // its subclass. If more subclasses will be created, this flag will
404 // have to become an enum.
406
407public:
410 bool InitContent = true);
411
412 const Elf_Rel *getRel(DataRefImpl Rel) const;
413 const Elf_Rela *getRela(DataRefImpl Rela) const;
414
416 return EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
417 }
418
419 /// Get the relocation section that contains \a Rel.
420 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
421 auto RelSecOrErr = EF.getSection(Rel.d.a);
422 if (!RelSecOrErr)
424 Twine(errorToErrorCode(RelSecOrErr.takeError()).message()));
425 return *RelSecOrErr;
426 }
427
428 const Elf_Shdr *getSection(DataRefImpl Sec) const {
429 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
430 }
431
432 basic_symbol_iterator symbol_begin() const override;
433 basic_symbol_iterator symbol_end() const override;
434
435 bool is64Bit() const override { return getBytesInAddress() == 8; }
436
439
440 section_iterator section_begin() const override;
441 section_iterator section_end() const override;
442
444
445 uint8_t getBytesInAddress() const override;
446 StringRef getFileFormatName() const override;
447 Triple::ArchType getArch() const override;
448 Expected<uint64_t> getStartAddress() const override;
449
450 unsigned getPlatformFlags() const override { return EF.getHeader().e_flags; }
451
452 const ELFFile<ELFT> &getELFFile() const { return EF; }
453
454 bool isDyldType() const { return isDyldELFObject; }
455 static bool classof(const Binary *v) {
456 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
457 ELFT::Is64Bits);
458 }
459
461
462 bool isRelocatableObject() const override;
463
464 void createFakeSections() { EF.createFakeSections(); }
465};
466
471
472template <class ELFT>
474 ++Sym.d.b;
475}
476
478 auto SectionsOrErr = EF.sections();
479 if (!SectionsOrErr)
480 return SectionsOrErr.takeError();
481
482 for (const Elf_Shdr &Sec : *SectionsOrErr) {
483 switch (Sec.sh_type) {
484 case ELF::SHT_DYNSYM: {
485 if (!DotDynSymSec)
486 DotDynSymSec = &Sec;
487 break;
488 }
489 case ELF::SHT_SYMTAB: {
490 if (!DotSymtabSec)
491 DotSymtabSec = &Sec;
492 break;
493 }
495 if (!DotSymtabShndxSec)
496 DotSymtabShndxSec = &Sec;
497 break;
498 }
499 }
500 }
501
502 ContentValid = true;
503 return Error::success();
504}
505
506template <class ELFT>
508 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
509 if (!SymOrErr)
510 return SymOrErr.takeError();
511 auto SymTabOrErr = EF.getSection(Sym.d.a);
512 if (!SymTabOrErr)
513 return SymTabOrErr.takeError();
514 const Elf_Shdr *SymTableSec = *SymTabOrErr;
515 auto StrTabOrErr = EF.getSection(SymTableSec->sh_link);
516 if (!StrTabOrErr)
517 return StrTabOrErr.takeError();
518 const Elf_Shdr *StringTableSec = *StrTabOrErr;
519 auto SymStrTabOrErr = EF.getStringTable(*StringTableSec);
520 if (!SymStrTabOrErr)
521 return SymStrTabOrErr.takeError();
522 Expected<StringRef> Name = (*SymOrErr)->getName(*SymStrTabOrErr);
523 if (Name && !Name->empty())
524 return Name;
525
526 // If the symbol name is empty use the section name.
527 if ((*SymOrErr)->getType() == ELF::STT_SECTION) {
528 if (Expected<section_iterator> SecOrErr = getSymbolSection(Sym)) {
529 consumeError(Name.takeError());
530 return (*SecOrErr)->getName();
531 }
532 }
533 return Name;
534}
535
536template <class ELFT>
538 return getSection(Sec)->sh_flags;
539}
540
541template <class ELFT>
543 return getSection(Sec)->sh_type;
544}
545
546template <class ELFT>
548 return getSection(Sec)->sh_offset;
549}
550
551template <class ELFT>
553 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
554 if (!SymOrErr)
555 report_fatal_error(SymOrErr.takeError());
556
557 uint64_t Ret = (*SymOrErr)->st_value;
558 if ((*SymOrErr)->st_shndx == ELF::SHN_ABS)
559 return Ret;
560
561 const Elf_Ehdr &Header = EF.getHeader();
562 // Clear the ARM/Thumb or microMIPS indicator flag.
563 if ((Header.e_machine == ELF::EM_ARM || Header.e_machine == ELF::EM_MIPS) &&
564 (*SymOrErr)->getType() == ELF::STT_FUNC)
565 Ret &= ~1;
566
567 return Ret;
568}
569
570template <class ELFT>
573 Expected<uint64_t> SymbolValueOrErr = getSymbolValue(Symb);
574 if (!SymbolValueOrErr)
575 // TODO: Test this error.
576 return SymbolValueOrErr.takeError();
577
578 uint64_t Result = *SymbolValueOrErr;
579 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
580 if (!SymOrErr)
581 return SymOrErr.takeError();
582
583 switch ((*SymOrErr)->st_shndx) {
584 case ELF::SHN_COMMON:
585 case ELF::SHN_UNDEF:
586 case ELF::SHN_ABS:
587 return Result;
588 }
589
590 auto SymTabOrErr = EF.getSection(Symb.d.a);
591 if (!SymTabOrErr)
592 return SymTabOrErr.takeError();
593
594 if (EF.getHeader().e_type == ELF::ET_REL) {
595 ArrayRef<Elf_Word> ShndxTable;
596 if (DotSymtabShndxSec) {
597 // TODO: Test this error.
598 if (Expected<ArrayRef<Elf_Word>> ShndxTableOrErr =
599 EF.getSHNDXTable(*DotSymtabShndxSec))
600 ShndxTable = *ShndxTableOrErr;
601 else
602 return ShndxTableOrErr.takeError();
603 }
604
605 Expected<const Elf_Shdr *> SectionOrErr =
606 EF.getSection(**SymOrErr, *SymTabOrErr, ShndxTable);
607 if (!SectionOrErr)
608 return SectionOrErr.takeError();
609 const Elf_Shdr *Section = *SectionOrErr;
610 if (Section)
611 Result += Section->sh_addr;
612 }
613
614 return Result;
615}
616
617template <class ELFT>
619 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
620 if (!SymOrErr)
621 report_fatal_error(SymOrErr.takeError());
622 if ((*SymOrErr)->st_shndx == ELF::SHN_COMMON)
623 return (*SymOrErr)->st_value;
624 return 0;
625}
626
627template <class ELFT>
629 return EF.getHeader().e_machine;
630}
631
632template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const {
633 return EF.getHeader().e_type;
634}
635
636template <class ELFT>
637uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
638 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
639 if (!SymOrErr)
640 report_fatal_error(SymOrErr.takeError());
641 return (*SymOrErr)->st_size;
642}
643
644template <class ELFT>
646 return getSymbolSize(Symb);
647}
648
649template <class ELFT>
651 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
652 if (!SymOrErr)
653 report_fatal_error(SymOrErr.takeError());
654 return (*SymOrErr)->getBinding();
655}
656
657template <class ELFT>
659 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
660 if (!SymOrErr)
661 report_fatal_error(SymOrErr.takeError());
662 return (*SymOrErr)->st_other;
663}
664
665template <class ELFT>
667 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
668 if (!SymOrErr)
669 report_fatal_error(SymOrErr.takeError());
670 return (*SymOrErr)->getType();
671}
672
673template <class ELFT>
676 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
677 if (!SymOrErr)
678 return SymOrErr.takeError();
679
680 switch ((*SymOrErr)->getType()) {
681 case ELF::STT_NOTYPE:
683 case ELF::STT_SECTION:
684 return SymbolRef::ST_Debug;
685 case ELF::STT_FILE:
686 return SymbolRef::ST_File;
687 case ELF::STT_FUNC:
689 case ELF::STT_OBJECT:
690 case ELF::STT_COMMON:
691 return SymbolRef::ST_Data;
692 case ELF::STT_TLS:
693 default:
694 return SymbolRef::ST_Other;
695 }
696}
697
698template <class ELFT>
700 Expected<const Elf_Sym *> SymOrErr = getSymbol(Sym);
701 if (!SymOrErr)
702 return SymOrErr.takeError();
703
704 const Elf_Sym *ESym = *SymOrErr;
706
707 if (ESym->getBinding() != ELF::STB_LOCAL)
708 Result |= SymbolRef::SF_Global;
709
710 if (ESym->getBinding() == ELF::STB_WEAK)
711 Result |= SymbolRef::SF_Weak;
712
713 if (ESym->st_shndx == ELF::SHN_ABS)
714 Result |= SymbolRef::SF_Absolute;
715
716 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION)
718
719 if (Expected<typename ELFT::SymRange> SymbolsOrErr =
720 EF.symbols(DotSymtabSec)) {
721 // Set the SF_FormatSpecific flag for the 0-index null symbol.
722 if (ESym == SymbolsOrErr->begin())
724 } else
725 // TODO: Test this error.
726 return SymbolsOrErr.takeError();
727
728 if (Expected<typename ELFT::SymRange> SymbolsOrErr =
729 EF.symbols(DotDynSymSec)) {
730 // Set the SF_FormatSpecific flag for the 0-index null symbol.
731 if (ESym == SymbolsOrErr->begin())
733 } else
734 // TODO: Test this error.
735 return SymbolsOrErr.takeError();
736
737 if (EF.getHeader().e_machine == ELF::EM_AARCH64) {
738 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
739 StringRef Name = *NameOrErr;
740 if (Name.startswith("$d") || Name.startswith("$x"))
742 } else {
743 // TODO: Actually report errors helpfully.
744 consumeError(NameOrErr.takeError());
745 }
746 } else if (EF.getHeader().e_machine == ELF::EM_ARM) {
747 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
748 StringRef Name = *NameOrErr;
749 // TODO Investigate why empty name symbols need to be marked.
750 if (Name.empty() || Name.startswith("$d") || Name.startswith("$t") ||
751 Name.startswith("$a"))
753 } else {
754 // TODO: Actually report errors helpfully.
755 consumeError(NameOrErr.takeError());
756 }
757 if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1)
758 Result |= SymbolRef::SF_Thumb;
759 } else if (EF.getHeader().e_machine == ELF::EM_RISCV) {
760 if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
761 // Mark empty name symbols used for label differences.
762 if (NameOrErr->empty())
764 } else {
765 // TODO: Actually report errors helpfully.
766 consumeError(NameOrErr.takeError());
767 }
768 }
769
770 if (ESym->st_shndx == ELF::SHN_UNDEF)
771 Result |= SymbolRef::SF_Undefined;
772
773 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
774 Result |= SymbolRef::SF_Common;
775
776 if (isExportedToOtherDSO(ESym))
777 Result |= SymbolRef::SF_Exported;
778
779 if (ESym->getType() == ELF::STT_GNU_IFUNC)
780 Result |= SymbolRef::SF_Indirect;
781
782 if (ESym->getVisibility() == ELF::STV_HIDDEN)
783 Result |= SymbolRef::SF_Hidden;
784
785 return Result;
786}
787
788template <class ELFT>
791 const Elf_Shdr *SymTab) const {
792 ArrayRef<Elf_Word> ShndxTable;
793 if (DotSymtabShndxSec) {
794 // TODO: Test this error.
795 Expected<ArrayRef<Elf_Word>> ShndxTableOrErr =
796 EF.getSHNDXTable(*DotSymtabShndxSec);
797 if (!ShndxTableOrErr)
798 return ShndxTableOrErr.takeError();
799 ShndxTable = *ShndxTableOrErr;
800 }
801
802 auto ESecOrErr = EF.getSection(*ESym, SymTab, ShndxTable);
803 if (!ESecOrErr)
804 return ESecOrErr.takeError();
805
806 const Elf_Shdr *ESec = *ESecOrErr;
807 if (!ESec)
808 return section_end();
809
810 DataRefImpl Sec;
811 Sec.p = reinterpret_cast<intptr_t>(ESec);
812 return section_iterator(SectionRef(Sec, this));
813}
814
815template <class ELFT>
818 Expected<const Elf_Sym *> SymOrErr = getSymbol(Symb);
819 if (!SymOrErr)
820 return SymOrErr.takeError();
821
822 auto SymTabOrErr = EF.getSection(Symb.d.a);
823 if (!SymTabOrErr)
824 return SymTabOrErr.takeError();
825 return getSymbolSection(*SymOrErr, *SymTabOrErr);
826}
827
828template <class ELFT>
830 const Elf_Shdr *ESec = getSection(Sec);
831 Sec = toDRI(++ESec);
832}
833
834template <class ELFT>
836 return EF.getSectionName(*getSection(Sec));
837}
838
839template <class ELFT>
841 return getSection(Sec)->sh_addr;
842}
843
844template <class ELFT>
846 auto SectionsOrErr = EF.sections();
847 handleAllErrors(std::move(SectionsOrErr.takeError()),
848 [](const ErrorInfoBase &) {
849 llvm_unreachable("unable to get section index");
850 });
851 const Elf_Shdr *First = SectionsOrErr->begin();
852 return getSection(Sec) - First;
853}
854
855template <class ELFT>
857 return getSection(Sec)->sh_size;
858}
859
860template <class ELFT>
863 const Elf_Shdr *EShdr = getSection(Sec);
864 if (EShdr->sh_type == ELF::SHT_NOBITS)
865 return ArrayRef((const uint8_t *)base(), (size_t)0);
866 if (Error E =
867 checkOffset(getMemoryBufferRef(),
868 (uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
869 return std::move(E);
870 return ArrayRef((const uint8_t *)base() + EShdr->sh_offset, EShdr->sh_size);
871}
872
873template <class ELFT>
875 return getSection(Sec)->sh_addralign;
876}
877
878template <class ELFT>
880 return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED;
881}
882
883template <class ELFT>
885 return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR;
886}
887
888template <class ELFT>
890 const Elf_Shdr *EShdr = getSection(Sec);
891 return EShdr->sh_type == ELF::SHT_PROGBITS &&
892 EShdr->sh_flags & ELF::SHF_ALLOC &&
893 !(EShdr->sh_flags & ELF::SHF_EXECINSTR);
894}
895
896template <class ELFT>
898 const Elf_Shdr *EShdr = getSection(Sec);
899 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
900 EShdr->sh_type == ELF::SHT_NOBITS;
901}
902
903template <class ELFT>
904std::vector<SectionRef>
906 std::vector<SectionRef> Res;
907 std::vector<uintptr_t> Offsets;
908
909 auto SectionsOrErr = EF.sections();
910 if (!SectionsOrErr)
911 return Res;
912
913 for (const Elf_Shdr &Sec : *SectionsOrErr) {
914 if (Sec.sh_type != ELF::SHT_DYNAMIC)
915 continue;
916 Elf_Dyn *Dynamic =
917 reinterpret_cast<Elf_Dyn *>((uintptr_t)base() + Sec.sh_offset);
918 for (; Dynamic->d_tag != ELF::DT_NULL; Dynamic++) {
919 if (Dynamic->d_tag == ELF::DT_REL || Dynamic->d_tag == ELF::DT_RELA ||
920 Dynamic->d_tag == ELF::DT_JMPREL) {
921 Offsets.push_back(Dynamic->d_un.d_val);
922 }
923 }
924 }
925 for (const Elf_Shdr &Sec : *SectionsOrErr) {
926 if (is_contained(Offsets, Sec.sh_addr))
927 Res.emplace_back(toDRI(&Sec), this);
928 }
929 return Res;
930}
931
932template <class ELFT>
934 return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
935}
936
937template <class ELFT>
939 return getSection(Sec)->sh_flags & ELF::SHF_ALLOC &&
940 (getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR ||
941 !(getSection(Sec)->sh_flags & ELF::SHF_WRITE));
942}
943
944template <class ELFT>
946 const Elf_Shdr *EShdr = getSection(Sec);
947 return !isBerkeleyText(Sec) && EShdr->sh_type != ELF::SHT_NOBITS &&
948 EShdr->sh_flags & ELF::SHF_ALLOC;
949}
950
951template <class ELFT>
953 Expected<StringRef> SectionNameOrErr = getSectionName(Sec);
954 if (!SectionNameOrErr) {
955 // TODO: Report the error message properly.
956 consumeError(SectionNameOrErr.takeError());
957 return false;
958 }
959 StringRef SectionName = SectionNameOrErr.get();
960 return SectionName.startswith(".debug") ||
961 SectionName.startswith(".zdebug") || SectionName == ".gdb_index";
962}
963
964template <class ELFT>
967 DataRefImpl RelData;
968 auto SectionsOrErr = EF.sections();
969 if (!SectionsOrErr)
971 uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
972 RelData.d.a = (Sec.p - SHT) / EF.getHeader().e_shentsize;
973 RelData.d.b = 0;
974 return relocation_iterator(RelocationRef(RelData, this));
975}
976
977template <class ELFT>
980 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
981 relocation_iterator Begin = section_rel_begin(Sec);
982 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
983 return Begin;
984 DataRefImpl RelData = Begin->getRawDataRefImpl();
985 const Elf_Shdr *RelSec = getRelSection(RelData);
986
987 // Error check sh_link here so that getRelocationSymbol can just use it.
988 auto SymSecOrErr = EF.getSection(RelSec->sh_link);
989 if (!SymSecOrErr)
991 Twine(errorToErrorCode(SymSecOrErr.takeError()).message()));
992
993 RelData.d.b += S->sh_size / S->sh_entsize;
994 return relocation_iterator(RelocationRef(RelData, this));
995}
996
997template <class ELFT>
1000 const Elf_Shdr *EShdr = getSection(Sec);
1001 uintX_t Type = EShdr->sh_type;
1002 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
1003 return section_end();
1004
1005 Expected<const Elf_Shdr *> SecOrErr = EF.getSection(EShdr->sh_info);
1006 if (!SecOrErr)
1007 return SecOrErr.takeError();
1008 return section_iterator(SectionRef(toDRI(*SecOrErr), this));
1009}
1010
1011// Relocations
1012template <class ELFT>
1014 ++Rel.d.b;
1015}
1016
1017template <class ELFT>
1020 uint32_t symbolIdx;
1021 const Elf_Shdr *sec = getRelSection(Rel);
1022 if (sec->sh_type == ELF::SHT_REL)
1023 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
1024 else
1025 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
1026 if (!symbolIdx)
1027 return symbol_end();
1028
1029 // FIXME: error check symbolIdx
1030 DataRefImpl SymbolData;
1031 SymbolData.d.a = sec->sh_link;
1032 SymbolData.d.b = symbolIdx;
1033 return symbol_iterator(SymbolRef(SymbolData, this));
1034}
1035
1036template <class ELFT>
1038 const Elf_Shdr *sec = getRelSection(Rel);
1039 if (sec->sh_type == ELF::SHT_REL)
1040 return getRel(Rel)->r_offset;
1041
1042 return getRela(Rel)->r_offset;
1043}
1044
1045template <class ELFT>
1047 const Elf_Shdr *sec = getRelSection(Rel);
1048 if (sec->sh_type == ELF::SHT_REL)
1049 return getRel(Rel)->getType(EF.isMips64EL());
1050 else
1051 return getRela(Rel)->getType(EF.isMips64EL());
1052}
1053
1054template <class ELFT>
1056 return getELFRelocationTypeName(EF.getHeader().e_machine, Type);
1057}
1058
1059template <class ELFT>
1061 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1062 uint32_t type = getRelocationType(Rel);
1063 EF.getRelocationTypeName(type, Result);
1064}
1065
1066template <class ELFT>
1069 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
1070 return createError("Section is not SHT_RELA");
1071 return (int64_t)getRela(Rel)->r_addend;
1072}
1073
1074template <class ELFT>
1075const typename ELFObjectFile<ELFT>::Elf_Rel *
1077 assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
1078 auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
1079 if (!Ret)
1080 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message()));
1081 return *Ret;
1082}
1083
1084template <class ELFT>
1085const typename ELFObjectFile<ELFT>::Elf_Rela *
1087 assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
1088 auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
1089 if (!Ret)
1090 report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message()));
1091 return *Ret;
1092}
1093
1094template <class ELFT>
1097 auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer());
1098 if (Error E = EFOrErr.takeError())
1099 return std::move(E);
1100
1101 ELFObjectFile<ELFT> Obj = {Object, std::move(*EFOrErr), nullptr, nullptr,
1102 nullptr};
1103 if (InitContent)
1104 if (Error E = Obj.initContent())
1105 return std::move(E);
1106 return std::move(Obj);
1107}
1108
1109template <class ELFT>
1111 const Elf_Shdr *DotDynSymSec,
1112 const Elf_Shdr *DotSymtabSec,
1113 const Elf_Shdr *DotSymtabShndx)
1115 getELFType(ELFT::TargetEndianness == support::little, ELFT::Is64Bits),
1116 Object),
1117 EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
1118 DotSymtabShndxSec(DotSymtabShndx) {}
1119
1120template <class ELFT>
1122 : ELFObjectFile(Other.Data, Other.EF, Other.DotDynSymSec,
1123 Other.DotSymtabSec, Other.DotSymtabShndxSec) {}
1124
1125template <class ELFT>
1127 DataRefImpl Sym =
1128 toDRI(DotSymtabSec,
1129 DotSymtabSec && DotSymtabSec->sh_size >= sizeof(Elf_Sym) ? 1 : 0);
1130 return basic_symbol_iterator(SymbolRef(Sym, this));
1131}
1132
1133template <class ELFT>
1135 const Elf_Shdr *SymTab = DotSymtabSec;
1136 if (!SymTab)
1137 return symbol_begin();
1138 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1139 return basic_symbol_iterator(SymbolRef(Sym, this));
1140}
1141
1142template <class ELFT>
1144 if (!DotDynSymSec || DotDynSymSec->sh_size < sizeof(Elf_Sym))
1145 // Ignore errors here where the dynsym is empty or sh_size less than the
1146 // size of one symbol. These should be handled elsewhere.
1147 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 0), this));
1148 // Skip 0-index NULL symbol.
1149 return symbol_iterator(SymbolRef(toDRI(DotDynSymSec, 1), this));
1150}
1151
1152template <class ELFT>
1154 const Elf_Shdr *SymTab = DotDynSymSec;
1155 if (!SymTab)
1156 return dynamic_symbol_begin();
1157 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1158 return basic_symbol_iterator(SymbolRef(Sym, this));
1159}
1160
1161template <class ELFT>
1163 auto SectionsOrErr = EF.sections();
1164 if (!SectionsOrErr)
1165 return section_iterator(SectionRef());
1166 return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this));
1167}
1168
1169template <class ELFT>
1171 auto SectionsOrErr = EF.sections();
1172 if (!SectionsOrErr)
1173 return section_iterator(SectionRef());
1174 return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
1175}
1176
1177template <class ELFT>
1179 return ELFT::Is64Bits ? 8 : 4;
1180}
1181
1182template <class ELFT>
1184 constexpr bool IsLittleEndian = ELFT::TargetEndianness == support::little;
1185 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1186 case ELF::ELFCLASS32:
1187 switch (EF.getHeader().e_machine) {
1188 case ELF::EM_68K:
1189 return "elf32-m68k";
1190 case ELF::EM_386:
1191 return "elf32-i386";
1192 case ELF::EM_IAMCU:
1193 return "elf32-iamcu";
1194 case ELF::EM_X86_64:
1195 return "elf32-x86-64";
1196 case ELF::EM_ARM:
1197 return (IsLittleEndian ? "elf32-littlearm" : "elf32-bigarm");
1198 case ELF::EM_AVR:
1199 return "elf32-avr";
1200 case ELF::EM_HEXAGON:
1201 return "elf32-hexagon";
1202 case ELF::EM_LANAI:
1203 return "elf32-lanai";
1204 case ELF::EM_MIPS:
1205 return "elf32-mips";
1206 case ELF::EM_MSP430:
1207 return "elf32-msp430";
1208 case ELF::EM_PPC:
1209 return (IsLittleEndian ? "elf32-powerpcle" : "elf32-powerpc");
1210 case ELF::EM_RISCV:
1211 return "elf32-littleriscv";
1212 case ELF::EM_CSKY:
1213 return "elf32-csky";
1214 case ELF::EM_SPARC:
1216 return "elf32-sparc";
1217 case ELF::EM_AMDGPU:
1218 return "elf32-amdgpu";
1219 case ELF::EM_LOONGARCH:
1220 return "elf32-loongarch";
1221 case ELF::EM_XTENSA:
1222 return "elf32-xtensa";
1223 default:
1224 return "elf32-unknown";
1225 }
1226 case ELF::ELFCLASS64:
1227 switch (EF.getHeader().e_machine) {
1228 case ELF::EM_386:
1229 return "elf64-i386";
1230 case ELF::EM_X86_64:
1231 return "elf64-x86-64";
1232 case ELF::EM_AARCH64:
1233 return (IsLittleEndian ? "elf64-littleaarch64" : "elf64-bigaarch64");
1234 case ELF::EM_PPC64:
1235 return (IsLittleEndian ? "elf64-powerpcle" : "elf64-powerpc");
1236 case ELF::EM_RISCV:
1237 return "elf64-littleriscv";
1238 case ELF::EM_S390:
1239 return "elf64-s390";
1240 case ELF::EM_SPARCV9:
1241 return "elf64-sparc";
1242 case ELF::EM_MIPS:
1243 return "elf64-mips";
1244 case ELF::EM_AMDGPU:
1245 return "elf64-amdgpu";
1246 case ELF::EM_BPF:
1247 return "elf64-bpf";
1248 case ELF::EM_VE:
1249 return "elf64-ve";
1250 case ELF::EM_LOONGARCH:
1251 return "elf64-loongarch";
1252 default:
1253 return "elf64-unknown";
1254 }
1255 default:
1256 // FIXME: Proper error handling.
1257 report_fatal_error("Invalid ELFCLASS!");
1258 }
1259}
1260
1261template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
1262 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
1263 switch (EF.getHeader().e_machine) {
1264 case ELF::EM_68K:
1265 return Triple::m68k;
1266 case ELF::EM_386:
1267 case ELF::EM_IAMCU:
1268 return Triple::x86;
1269 case ELF::EM_X86_64:
1270 return Triple::x86_64;
1271 case ELF::EM_AARCH64:
1272 return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be;
1273 case ELF::EM_ARM:
1274 return Triple::arm;
1275 case ELF::EM_AVR:
1276 return Triple::avr;
1277 case ELF::EM_HEXAGON:
1278 return Triple::hexagon;
1279 case ELF::EM_LANAI:
1280 return Triple::lanai;
1281 case ELF::EM_MIPS:
1282 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1283 case ELF::ELFCLASS32:
1284 return IsLittleEndian ? Triple::mipsel : Triple::mips;
1285 case ELF::ELFCLASS64:
1286 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
1287 default:
1288 report_fatal_error("Invalid ELFCLASS!");
1289 }
1290 case ELF::EM_MSP430:
1291 return Triple::msp430;
1292 case ELF::EM_PPC:
1293 return IsLittleEndian ? Triple::ppcle : Triple::ppc;
1294 case ELF::EM_PPC64:
1295 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
1296 case ELF::EM_RISCV:
1297 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1298 case ELF::ELFCLASS32:
1299 return Triple::riscv32;
1300 case ELF::ELFCLASS64:
1301 return Triple::riscv64;
1302 default:
1303 report_fatal_error("Invalid ELFCLASS!");
1304 }
1305 case ELF::EM_S390:
1306 return Triple::systemz;
1307
1308 case ELF::EM_SPARC:
1310 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
1311 case ELF::EM_SPARCV9:
1312 return Triple::sparcv9;
1313
1314 case ELF::EM_AMDGPU: {
1315 if (!IsLittleEndian)
1316 return Triple::UnknownArch;
1317
1318 unsigned MACH = EF.getHeader().e_flags & ELF::EF_AMDGPU_MACH;
1319 if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST &&
1321 return Triple::r600;
1324 return Triple::amdgcn;
1325
1326 return Triple::UnknownArch;
1327 }
1328
1329 case ELF::EM_BPF:
1330 return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
1331
1332 case ELF::EM_VE:
1333 return Triple::ve;
1334 case ELF::EM_CSKY:
1335 return Triple::csky;
1336
1337 case ELF::EM_LOONGARCH:
1338 switch (EF.getHeader().e_ident[ELF::EI_CLASS]) {
1339 case ELF::ELFCLASS32:
1340 return Triple::loongarch32;
1341 case ELF::ELFCLASS64:
1342 return Triple::loongarch64;
1343 default:
1344 report_fatal_error("Invalid ELFCLASS!");
1345 }
1346
1347 case ELF::EM_XTENSA:
1348 return Triple::xtensa;
1349
1350 default:
1351 return Triple::UnknownArch;
1352 }
1353}
1354
1355template <class ELFT>
1357 return EF.getHeader().e_entry;
1358}
1359
1360template <class ELFT>
1363 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
1364}
1365
1366template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
1367 return EF.getHeader().e_type == ELF::ET_REL;
1368}
1369
1370} // end namespace object
1371} // end namespace llvm
1372
1373#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
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:47
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
Tagged union holding either a T or a Error.
Definition: Error.h:470
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
reference get()
Returns a reference to the stored T value.
Definition: Error.h:567
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:77
static Expected< ELFFile > create(StringRef Object)
Definition: ELF.h:775
virtual uint64_t getSymbolSize(DataRefImpl Symb) const =0
std::vector< std::pair< std::optional< DataRefImpl >, uint64_t > > getPltAddresses() const
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
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:79
virtual uint16_t getEMachine() const =0
static bool classof(const Binary *v)
Definition: ELFObjectFile.h:88
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:228
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:51
const ObjectFile * getObject() const
Definition: ObjectFile.h:583
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:579
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:80
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:541
const ObjectFile * getObject() const
Definition: ObjectFile.h:545
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: ObjectFile.h:167
const ObjectFile * getObject() const
Definition: ObjectFile.h:431
virtual basic_symbol_iterator symbol_begin() const =0
virtual basic_symbol_iterator symbol_end() const =0
const content_type & operator*() const
Definition: SymbolicFile.h:83
const content_type * 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:214
const SymbolRef & operator*() const
Definition: ObjectFile.h:219
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
@ SHF_ALLOC
Definition: ELF.h:1083
@ SHF_COMPRESSED
Definition: ELF.h:1111
@ SHF_WRITE
Definition: ELF.h:1080
@ SHF_EXECINSTR
Definition: ELF.h:1086
@ STV_HIDDEN
Definition: ELF.h:1268
@ STV_PROTECTED
Definition: ELF.h:1269
@ STV_DEFAULT
Definition: ELF.h:1266
@ ELFCLASS64
Definition: ELF.h:329
@ ELFCLASS32
Definition: ELF.h:328
@ 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
@ SHN_ABS
Definition: ELF.h:986
@ SHN_COMMON
Definition: ELF.h:987
@ SHN_UNDEF
Definition: ELF.h:980
@ SHT_PROGBITS
Definition: ELF.h:995
@ SHT_REL
Definition: ELF.h:1003
@ SHT_ARM_ATTRIBUTES
Definition: ELF.h:1050
@ SHT_NOBITS
Definition: ELF.h:1002
@ SHT_SYMTAB
Definition: ELF.h:996
@ SHT_DYNAMIC
Definition: ELF.h:1000
@ SHT_SYMTAB_SHNDX
Definition: ELF.h:1010
@ SHT_RISCV_ATTRIBUTES
Definition: ELF.h:1068
@ SHT_RELA
Definition: ELF.h:998
@ SHT_DYNSYM
Definition: ELF.h:1005
@ EF_AMDGPU_MACH_AMDGCN_LAST
Definition: ELF.h:784
@ EF_AMDGPU_MACH_R600_LAST
Definition: ELF.h:738
@ EF_AMDGPU_MACH_AMDGCN_FIRST
Definition: ELF.h:783
@ EF_AMDGPU_MACH
Definition: ELF.h:704
@ EF_AMDGPU_MACH_R600_FIRST
Definition: ELF.h:737
@ STB_GLOBAL
Definition: ELF.h:1237
@ STB_LOCAL
Definition: ELF.h:1236
@ STB_GNU_UNIQUE
Definition: ELF.h:1239
@ STB_WEAK
Definition: ELF.h:1238
@ ET_REL
Definition: ELF.h:116
@ EI_CLASS
Definition: ELF.h:52
@ STT_FUNC
Definition: ELF.h:1250
@ STT_NOTYPE
Definition: ELF.h:1248
@ STT_SECTION
Definition: ELF.h:1251
@ STT_FILE
Definition: ELF.h:1252
@ STT_COMMON
Definition: ELF.h:1253
@ STT_GNU_IFUNC
Definition: ELF.h:1255
@ STT_OBJECT
Definition: ELF.h:1249
@ STT_TLS
Definition: ELF.h:1254
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:421
Error createError(const Twine &Err)
Definition: Error.h:84
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
Definition: ELF.cpp:22
constexpr int NumElfSymbolTypes
Definition: ELFObjectFile.h:46
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:47
content_iterator< RelocationRef > relocation_iterator
Definition: ObjectFile.h:76
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:966
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:145
@ 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:566
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1976
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:98
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1043
struct llvm::object::DataRefImpl::@339 d