LLVM 19.0.0git
ELFObjectFile.cpp
Go to the documentation of this file.
1//===- ELFObjectFile.cpp - ELF object file implementation -----------------===//
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// Part of the ELFObjectFile class implementation.
10//
11//===----------------------------------------------------------------------===//
12
17#include "llvm/Object/ELF.h"
19#include "llvm/Object/Error.h"
29#include <algorithm>
30#include <cstddef>
31#include <cstdint>
32#include <memory>
33#include <optional>
34#include <string>
35#include <utility>
36
37using namespace llvm;
38using namespace object;
39
41 {"None", "NOTYPE", ELF::STT_NOTYPE},
42 {"Object", "OBJECT", ELF::STT_OBJECT},
43 {"Function", "FUNC", ELF::STT_FUNC},
44 {"Section", "SECTION", ELF::STT_SECTION},
45 {"File", "FILE", ELF::STT_FILE},
46 {"Common", "COMMON", ELF::STT_COMMON},
47 {"TLS", "TLS", ELF::STT_TLS},
48 {"Unknown", "<unknown>: 7", 7},
49 {"Unknown", "<unknown>: 8", 8},
50 {"Unknown", "<unknown>: 9", 9},
51 {"GNU_IFunc", "IFUNC", ELF::STT_GNU_IFUNC},
52 {"OS Specific", "<OS specific>: 11", 11},
53 {"OS Specific", "<OS specific>: 12", 12},
54 {"Proc Specific", "<processor specific>: 13", 13},
55 {"Proc Specific", "<processor specific>: 14", 14},
56 {"Proc Specific", "<processor specific>: 15", 15}
57};
58
60 : ObjectFile(Type, Source) {}
61
62template <class ELFT>
64createPtr(MemoryBufferRef Object, bool InitContent) {
65 auto Ret = ELFObjectFile<ELFT>::create(Object, InitContent);
66 if (Error E = Ret.takeError())
67 return std::move(E);
68 return std::make_unique<ELFObjectFile<ELFT>>(std::move(*Ret));
69}
70
73 std::pair<unsigned char, unsigned char> Ident =
75 std::size_t MaxAlignment =
76 1ULL << llvm::countr_zero(
77 reinterpret_cast<uintptr_t>(Obj.getBufferStart()));
78
79 if (MaxAlignment < 2)
80 return createError("Insufficient alignment");
81
82 if (Ident.first == ELF::ELFCLASS32) {
83 if (Ident.second == ELF::ELFDATA2LSB)
84 return createPtr<ELF32LE>(Obj, InitContent);
85 else if (Ident.second == ELF::ELFDATA2MSB)
86 return createPtr<ELF32BE>(Obj, InitContent);
87 else
88 return createError("Invalid ELF data");
89 } else if (Ident.first == ELF::ELFCLASS64) {
90 if (Ident.second == ELF::ELFDATA2LSB)
91 return createPtr<ELF64LE>(Obj, InitContent);
92 else if (Ident.second == ELF::ELFDATA2MSB)
93 return createPtr<ELF64BE>(Obj, InitContent);
94 else
95 return createError("Invalid ELF data");
96 }
97 return createError("Invalid ELF class");
98}
99
100SubtargetFeatures ELFObjectFileBase::getMIPSFeatures() const {
101 SubtargetFeatures Features;
102 unsigned PlatformFlags = getPlatformFlags();
103
104 switch (PlatformFlags & ELF::EF_MIPS_ARCH) {
106 break;
108 Features.AddFeature("mips2");
109 break;
111 Features.AddFeature("mips3");
112 break;
114 Features.AddFeature("mips4");
115 break;
117 Features.AddFeature("mips5");
118 break;
120 Features.AddFeature("mips32");
121 break;
123 Features.AddFeature("mips64");
124 break;
126 Features.AddFeature("mips32r2");
127 break;
129 Features.AddFeature("mips64r2");
130 break;
132 Features.AddFeature("mips32r6");
133 break;
135 Features.AddFeature("mips64r6");
136 break;
137 default:
138 llvm_unreachable("Unknown EF_MIPS_ARCH value");
139 }
140
141 switch (PlatformFlags & ELF::EF_MIPS_MACH) {
143 // No feature associated with this value.
144 break;
146 Features.AddFeature("cnmips");
147 break;
148 default:
149 llvm_unreachable("Unknown EF_MIPS_ARCH value");
150 }
151
152 if (PlatformFlags & ELF::EF_MIPS_ARCH_ASE_M16)
153 Features.AddFeature("mips16");
154 if (PlatformFlags & ELF::EF_MIPS_MICROMIPS)
155 Features.AddFeature("micromips");
156
157 return Features;
158}
159
160SubtargetFeatures ELFObjectFileBase::getARMFeatures() const {
161 SubtargetFeatures Features;
163 if (Error E = getBuildAttributes(Attributes)) {
164 consumeError(std::move(E));
165 return SubtargetFeatures();
166 }
167
168 // both ARMv7-M and R have to support thumb hardware div
169 bool isV7 = false;
170 std::optional<unsigned> Attr =
171 Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
172 if (Attr)
173 isV7 = *Attr == ARMBuildAttrs::v7;
174
175 Attr = Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile);
176 if (Attr) {
177 switch (*Attr) {
179 Features.AddFeature("aclass");
180 break;
182 Features.AddFeature("rclass");
183 if (isV7)
184 Features.AddFeature("hwdiv");
185 break;
187 Features.AddFeature("mclass");
188 if (isV7)
189 Features.AddFeature("hwdiv");
190 break;
191 }
192 }
193
194 Attr = Attributes.getAttributeValue(ARMBuildAttrs::THUMB_ISA_use);
195 if (Attr) {
196 switch (*Attr) {
197 default:
198 break;
200 Features.AddFeature("thumb", false);
201 Features.AddFeature("thumb2", false);
202 break;
204 Features.AddFeature("thumb2");
205 break;
206 }
207 }
208
209 Attr = Attributes.getAttributeValue(ARMBuildAttrs::FP_arch);
210 if (Attr) {
211 switch (*Attr) {
212 default:
213 break;
215 Features.AddFeature("vfp2sp", false);
216 Features.AddFeature("vfp3d16sp", false);
217 Features.AddFeature("vfp4d16sp", false);
218 break;
220 Features.AddFeature("vfp2");
221 break;
224 Features.AddFeature("vfp3");
225 break;
228 Features.AddFeature("vfp4");
229 break;
230 }
231 }
232
233 Attr = Attributes.getAttributeValue(ARMBuildAttrs::Advanced_SIMD_arch);
234 if (Attr) {
235 switch (*Attr) {
236 default:
237 break;
239 Features.AddFeature("neon", false);
240 Features.AddFeature("fp16", false);
241 break;
243 Features.AddFeature("neon");
244 break;
246 Features.AddFeature("neon");
247 Features.AddFeature("fp16");
248 break;
249 }
250 }
251
252 Attr = Attributes.getAttributeValue(ARMBuildAttrs::MVE_arch);
253 if (Attr) {
254 switch (*Attr) {
255 default:
256 break;
258 Features.AddFeature("mve", false);
259 Features.AddFeature("mve.fp", false);
260 break;
262 Features.AddFeature("mve.fp", false);
263 Features.AddFeature("mve");
264 break;
266 Features.AddFeature("mve.fp");
267 break;
268 }
269 }
270
271 Attr = Attributes.getAttributeValue(ARMBuildAttrs::DIV_use);
272 if (Attr) {
273 switch (*Attr) {
274 default:
275 break;
277 Features.AddFeature("hwdiv", false);
278 Features.AddFeature("hwdiv-arm", false);
279 break;
281 Features.AddFeature("hwdiv");
282 Features.AddFeature("hwdiv-arm");
283 break;
284 }
285 }
286
287 return Features;
288}
289
290Expected<SubtargetFeatures> ELFObjectFileBase::getRISCVFeatures() const {
291 SubtargetFeatures Features;
292 unsigned PlatformFlags = getPlatformFlags();
293
294 if (PlatformFlags & ELF::EF_RISCV_RVC) {
295 Features.AddFeature("zca");
296 }
297
299 if (Error E = getBuildAttributes(Attributes)) {
300 return std::move(E);
301 }
302
303 std::optional<StringRef> Attr =
304 Attributes.getAttributeString(RISCVAttrs::ARCH);
305 if (Attr) {
306 auto ParseResult = RISCVISAInfo::parseNormalizedArchString(*Attr);
307 if (!ParseResult)
308 return ParseResult.takeError();
309 auto &ISAInfo = *ParseResult;
310
311 if (ISAInfo->getXLen() == 32)
312 Features.AddFeature("64bit", false);
313 else if (ISAInfo->getXLen() == 64)
314 Features.AddFeature("64bit");
315 else
316 llvm_unreachable("XLEN should be 32 or 64.");
317
318 Features.addFeaturesVector(ISAInfo->toFeatures());
319 }
320
321 return Features;
322}
323
324SubtargetFeatures ELFObjectFileBase::getLoongArchFeatures() const {
325 SubtargetFeatures Features;
326
329 break;
331 Features.AddFeature("d");
332 // D implies F according to LoongArch ISA spec.
333 [[fallthrough]];
335 Features.AddFeature("f");
336 break;
337 }
338
339 return Features;
340}
341
343 switch (getEMachine()) {
344 case ELF::EM_MIPS:
345 return getMIPSFeatures();
346 case ELF::EM_ARM:
347 return getARMFeatures();
348 case ELF::EM_RISCV:
349 return getRISCVFeatures();
351 return getLoongArchFeatures();
352 default:
353 return SubtargetFeatures();
354 }
355}
356
357std::optional<StringRef> ELFObjectFileBase::tryGetCPUName() const {
358 switch (getEMachine()) {
359 case ELF::EM_AMDGPU:
360 return getAMDGPUCPUName();
361 case ELF::EM_CUDA:
362 return getNVPTXCPUName();
363 case ELF::EM_PPC:
364 case ELF::EM_PPC64:
365 return StringRef("future");
366 default:
367 return std::nullopt;
368 }
369}
370
371StringRef ELFObjectFileBase::getAMDGPUCPUName() const {
373 unsigned CPU = getPlatformFlags() & ELF::EF_AMDGPU_MACH;
374
375 switch (CPU) {
376 // Radeon HD 2000/3000 Series (R600).
378 return "r600";
380 return "r630";
382 return "rs880";
384 return "rv670";
385
386 // Radeon HD 4000 Series (R700).
388 return "rv710";
390 return "rv730";
392 return "rv770";
393
394 // Radeon HD 5000 Series (Evergreen).
396 return "cedar";
398 return "cypress";
400 return "juniper";
402 return "redwood";
404 return "sumo";
405
406 // Radeon HD 6000 Series (Northern Islands).
408 return "barts";
410 return "caicos";
412 return "cayman";
414 return "turks";
415
416 // AMDGCN GFX6.
418 return "gfx600";
420 return "gfx601";
422 return "gfx602";
423
424 // AMDGCN GFX7.
426 return "gfx700";
428 return "gfx701";
430 return "gfx702";
432 return "gfx703";
434 return "gfx704";
436 return "gfx705";
437
438 // AMDGCN GFX8.
440 return "gfx801";
442 return "gfx802";
444 return "gfx803";
446 return "gfx805";
448 return "gfx810";
449
450 // AMDGCN GFX9.
452 return "gfx900";
454 return "gfx902";
456 return "gfx904";
458 return "gfx906";
460 return "gfx908";
462 return "gfx909";
464 return "gfx90a";
466 return "gfx90c";
468 return "gfx940";
470 return "gfx941";
472 return "gfx942";
473
474 // AMDGCN GFX10.
476 return "gfx1010";
478 return "gfx1011";
480 return "gfx1012";
482 return "gfx1013";
484 return "gfx1030";
486 return "gfx1031";
488 return "gfx1032";
490 return "gfx1033";
492 return "gfx1034";
494 return "gfx1035";
496 return "gfx1036";
497
498 // AMDGCN GFX11.
500 return "gfx1100";
502 return "gfx1101";
504 return "gfx1102";
506 return "gfx1103";
508 return "gfx1150";
510 return "gfx1151";
511
512 // AMDGCN GFX12.
514 return "gfx1200";
516 return "gfx1201";
517
518 // Generic AMDGCN targets
520 return "gfx9-generic";
522 return "gfx10-1-generic";
524 return "gfx10-3-generic";
526 return "gfx11-generic";
527 default:
528 llvm_unreachable("Unknown EF_AMDGPU_MACH value");
529 }
530}
531
532StringRef ELFObjectFileBase::getNVPTXCPUName() const {
534 unsigned SM = getPlatformFlags() & ELF::EF_CUDA_SM;
535
536 switch (SM) {
537 // Fermi architecture.
539 return "sm_20";
541 return "sm_21";
542
543 // Kepler architecture.
545 return "sm_30";
547 return "sm_32";
549 return "sm_35";
551 return "sm_37";
552
553 // Maxwell architecture.
555 return "sm_50";
557 return "sm_52";
559 return "sm_53";
560
561 // Pascal architecture.
563 return "sm_60";
565 return "sm_61";
567 return "sm_62";
568
569 // Volta architecture.
571 return "sm_70";
573 return "sm_72";
574
575 // Turing architecture.
577 return "sm_75";
578
579 // Ampere architecture.
581 return "sm_80";
583 return "sm_86";
585 return "sm_87";
586
587 // Ada architecture.
589 return "sm_89";
590
591 // Hopper architecture.
593 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_90a" : "sm_90";
594 default:
595 llvm_unreachable("Unknown EF_CUDA_SM value");
596 }
597}
598
599// FIXME Encode from a tablegen description or target parser.
601 if (TheTriple.getSubArch() != Triple::NoSubArch)
602 return;
603
606 // TODO Propagate Error.
607 consumeError(std::move(E));
608 return;
609 }
610
611 std::string Triple;
612 // Default to ARM, but use the triple if it's been set.
613 if (TheTriple.isThumb())
614 Triple = "thumb";
615 else
616 Triple = "arm";
617
618 std::optional<unsigned> Attr =
619 Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
620 if (Attr) {
621 switch (*Attr) {
623 Triple += "v4";
624 break;
626 Triple += "v4t";
627 break;
629 Triple += "v5t";
630 break;
632 Triple += "v5te";
633 break;
635 Triple += "v5tej";
636 break;
638 Triple += "v6";
639 break;
641 Triple += "v6kz";
642 break;
644 Triple += "v6t2";
645 break;
647 Triple += "v6k";
648 break;
649 case ARMBuildAttrs::v7: {
650 std::optional<unsigned> ArchProfileAttr =
652 if (ArchProfileAttr &&
653 *ArchProfileAttr == ARMBuildAttrs::MicroControllerProfile)
654 Triple += "v7m";
655 else
656 Triple += "v7";
657 break;
658 }
660 Triple += "v6m";
661 break;
663 Triple += "v6sm";
664 break;
666 Triple += "v7em";
667 break;
669 Triple += "v8a";
670 break;
672 Triple += "v8r";
673 break;
675 Triple += "v8m.base";
676 break;
678 Triple += "v8m.main";
679 break;
681 Triple += "v8.1m.main";
682 break;
684 Triple += "v9a";
685 break;
686 }
687 }
688 if (!isLittleEndian())
689 Triple += "eb";
690
691 TheTriple.setArchName(Triple);
692}
693
694std::vector<ELFPltEntry> ELFObjectFileBase::getPltEntries() const {
695 std::string Err;
696 const auto Triple = makeTriple();
697 const auto *T = TargetRegistry::lookupTarget(Triple.str(), Err);
698 if (!T)
699 return {};
700 uint32_t JumpSlotReloc = 0, GlobDatReloc = 0;
701 switch (Triple.getArch()) {
702 case Triple::x86:
703 JumpSlotReloc = ELF::R_386_JUMP_SLOT;
704 GlobDatReloc = ELF::R_386_GLOB_DAT;
705 break;
706 case Triple::x86_64:
707 JumpSlotReloc = ELF::R_X86_64_JUMP_SLOT;
708 GlobDatReloc = ELF::R_X86_64_GLOB_DAT;
709 break;
710 case Triple::aarch64:
712 JumpSlotReloc = ELF::R_AARCH64_JUMP_SLOT;
713 break;
714 default:
715 return {};
716 }
717 std::unique_ptr<const MCInstrInfo> MII(T->createMCInstrInfo());
718 std::unique_ptr<const MCInstrAnalysis> MIA(
719 T->createMCInstrAnalysis(MII.get()));
720 if (!MIA)
721 return {};
722 std::vector<std::pair<uint64_t, uint64_t>> PltEntries;
723 std::optional<SectionRef> RelaPlt, RelaDyn;
724 uint64_t GotBaseVA = 0;
725 for (const SectionRef &Section : sections()) {
726 Expected<StringRef> NameOrErr = Section.getName();
727 if (!NameOrErr) {
728 consumeError(NameOrErr.takeError());
729 continue;
730 }
731 StringRef Name = *NameOrErr;
732
733 if (Name == ".rela.plt" || Name == ".rel.plt") {
734 RelaPlt = Section;
735 } else if (Name == ".rela.dyn" || Name == ".rel.dyn") {
736 RelaDyn = Section;
737 } else if (Name == ".got.plt") {
738 GotBaseVA = Section.getAddress();
739 } else if (Name == ".plt" || Name == ".plt.got") {
740 Expected<StringRef> PltContents = Section.getContents();
741 if (!PltContents) {
742 consumeError(PltContents.takeError());
743 return {};
744 }
746 PltEntries,
747 MIA->findPltEntries(Section.getAddress(),
748 arrayRefFromStringRef(*PltContents), Triple));
749 }
750 }
751
752 // Build a map from GOT entry virtual address to PLT entry virtual address.
754 for (auto [Plt, GotPlt] : PltEntries) {
755 uint64_t GotPltEntry = GotPlt;
756 // An x86-32 PIC PLT uses jmp DWORD PTR [ebx-offset]. Add
757 // _GLOBAL_OFFSET_TABLE_ (EBX) to get the .got.plt (or .got) entry address.
758 // See X86MCTargetDesc.cpp:findPltEntries for the 1 << 32 bit.
759 if (GotPltEntry & (uint64_t(1) << 32) && getEMachine() == ELF::EM_386)
760 GotPltEntry = static_cast<int32_t>(GotPltEntry) + GotBaseVA;
761 GotToPlt.insert(std::make_pair(GotPltEntry, Plt));
762 }
763
764 // Find the relocations in the dynamic relocation table that point to
765 // locations in the GOT for which we know the corresponding PLT entry.
766 std::vector<ELFPltEntry> Result;
767 auto handleRels = [&](iterator_range<relocation_iterator> Rels,
768 uint32_t RelType, StringRef PltSec) {
769 for (const auto &R : Rels) {
770 if (R.getType() != RelType)
771 continue;
772 auto PltEntryIter = GotToPlt.find(R.getOffset());
773 if (PltEntryIter != GotToPlt.end()) {
774 symbol_iterator Sym = R.getSymbol();
775 if (Sym == symbol_end())
776 Result.push_back(
777 ELFPltEntry{PltSec, std::nullopt, PltEntryIter->second});
778 else
779 Result.push_back(ELFPltEntry{PltSec, Sym->getRawDataRefImpl(),
780 PltEntryIter->second});
781 }
782 }
783 };
784
785 if (RelaPlt)
786 handleRels(RelaPlt->relocations(), JumpSlotReloc, ".plt");
787
788 // If a symbol needing a PLT entry also needs a GLOB_DAT relocation, GNU ld's
789 // x86 port places the PLT entry in the .plt.got section.
790 if (RelaDyn)
791 handleRels(RelaDyn->relocations(), GlobDatReloc, ".plt.got");
792
793 return Result;
794}
795
796template <class ELFT>
798 const ELFFile<ELFT> &EF, std::optional<unsigned> TextSectionIndex,
799 std::vector<PGOAnalysisMap> *PGOAnalyses) {
800 using Elf_Shdr = typename ELFT::Shdr;
801 bool IsRelocatable = EF.getHeader().e_type == ELF::ET_REL;
802 std::vector<BBAddrMap> BBAddrMaps;
803 if (PGOAnalyses)
804 PGOAnalyses->clear();
805
806 const auto &Sections = cantFail(EF.sections());
807 auto IsMatch = [&](const Elf_Shdr &Sec) -> Expected<bool> {
808 if (Sec.sh_type != ELF::SHT_LLVM_BB_ADDR_MAP &&
809 Sec.sh_type != ELF::SHT_LLVM_BB_ADDR_MAP_V0)
810 return false;
811 if (!TextSectionIndex)
812 return true;
813 Expected<const Elf_Shdr *> TextSecOrErr = EF.getSection(Sec.sh_link);
814 if (!TextSecOrErr)
815 return createError("unable to get the linked-to section for " +
816 describe(EF, Sec) + ": " +
817 toString(TextSecOrErr.takeError()));
818 assert(*TextSecOrErr >= Sections.begin() &&
819 "Text section pointer outside of bounds");
820 if (*TextSectionIndex !=
821 (unsigned)std::distance(Sections.begin(), *TextSecOrErr))
822 return false;
823 return true;
824 };
825
827 EF.getSectionAndRelocations(IsMatch);
828 if (!SectionRelocMapOrErr)
829 return SectionRelocMapOrErr.takeError();
830
831 for (auto const &[Sec, RelocSec] : *SectionRelocMapOrErr) {
832 if (IsRelocatable && !RelocSec)
833 return createError("unable to get relocation section for " +
834 describe(EF, *Sec));
835 Expected<std::vector<BBAddrMap>> BBAddrMapOrErr =
836 EF.decodeBBAddrMap(*Sec, RelocSec, PGOAnalyses);
837 if (!BBAddrMapOrErr) {
838 if (PGOAnalyses)
839 PGOAnalyses->clear();
840 return createError("unable to read " + describe(EF, *Sec) + ": " +
841 toString(BBAddrMapOrErr.takeError()));
842 }
843 std::move(BBAddrMapOrErr->begin(), BBAddrMapOrErr->end(),
844 std::back_inserter(BBAddrMaps));
845 }
846 if (PGOAnalyses)
847 assert(PGOAnalyses->size() == BBAddrMaps.size() &&
848 "The same number of BBAddrMaps and PGOAnalysisMaps should be "
849 "returned when PGO information is requested");
850 return BBAddrMaps;
851}
852
853template <class ELFT>
857 using Elf_Shdr = typename ELFT::Shdr;
858 const Elf_Shdr *VerSec = nullptr;
859 const Elf_Shdr *VerNeedSec = nullptr;
860 const Elf_Shdr *VerDefSec = nullptr;
861 // The user should ensure sections() can't fail here.
862 for (const Elf_Shdr &Sec : cantFail(EF.sections())) {
863 if (Sec.sh_type == ELF::SHT_GNU_versym)
864 VerSec = &Sec;
865 else if (Sec.sh_type == ELF::SHT_GNU_verdef)
866 VerDefSec = &Sec;
867 else if (Sec.sh_type == ELF::SHT_GNU_verneed)
868 VerNeedSec = &Sec;
869 }
870 if (!VerSec)
871 return std::vector<VersionEntry>();
872
874 EF.loadVersionMap(VerNeedSec, VerDefSec);
875 if (!MapOrErr)
876 return MapOrErr.takeError();
877
878 std::vector<VersionEntry> Ret;
879 size_t I = 0;
880 for (const ELFSymbolRef &Sym : Symbols) {
881 ++I;
883 EF.template getEntry<typename ELFT::Versym>(*VerSec, I);
884 if (!VerEntryOrErr)
885 return createError("unable to read an entry with index " + Twine(I) +
886 " from " + describe(EF, *VerSec) + ": " +
887 toString(VerEntryOrErr.takeError()));
888
889 Expected<uint32_t> FlagsOrErr = Sym.getFlags();
890 if (!FlagsOrErr)
891 return createError("unable to read flags for symbol with index " +
892 Twine(I) + ": " + toString(FlagsOrErr.takeError()));
893
894 bool IsDefault;
896 (*VerEntryOrErr)->vs_index, IsDefault, *MapOrErr,
897 (*FlagsOrErr) & SymbolRef::SF_Undefined);
898 if (!VerOrErr)
899 return createError("unable to get a version for entry " + Twine(I) +
900 " of " + describe(EF, *VerSec) + ": " +
901 toString(VerOrErr.takeError()));
902
903 Ret.push_back({(*VerOrErr).str(), IsDefault});
904 }
905
906 return Ret;
907}
908
912 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
913 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
914 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
915 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
916 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
917 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
918 return readDynsymVersionsImpl(cast<ELF64BEObjectFile>(this)->getELFFile(),
919 Symbols);
920}
921
923 std::optional<unsigned> TextSectionIndex,
924 std::vector<PGOAnalysisMap> *PGOAnalyses) const {
925 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
926 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
927 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
928 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
929 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
930 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
931 return readBBAddrMapImpl(cast<ELF64BEObjectFile>(this)->getELFFile(),
932 TextSectionIndex, PGOAnalyses);
933}
AMDGPU Kernel Attributes
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::string Name
static Expected< std::unique_ptr< ELFObjectFile< ELFT > > > createPtr(MemoryBufferRef Object, bool InitContent)
static Expected< std::vector< BBAddrMap > > readBBAddrMapImpl(const ELFFile< ELFT > &EF, std::optional< unsigned > TextSectionIndex, std::vector< PGOAnalysisMap > *PGOAnalyses)
static Expected< std::vector< VersionEntry > > readDynsymVersionsImpl(const ELFFile< ELFT > &EF, ELFObjectFileBase::elf_symbol_iterator_range Symbols)
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
iterator end()
Definition: DenseMap.h:84
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:220
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
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
const char * getBufferStart() const
StringRef getBuffer() const
static llvm::Expected< std::unique_ptr< RISCVISAInfo > > parseNormalizedArchString(StringRef Arch)
Parse RISC-V ISA info from an arch string that is already in normalized form (as defined in the psABI...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Manages the enabling and disabling of subtarget specific features.
void AddFeature(StringRef String, bool Enable=true)
Adds Features.
void addFeaturesVector(const ArrayRef< std::string > OtherFeatures)
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
void setArchName(StringRef Str)
Set the architecture (first) component of the triple by name.
Definition: Triple.cpp:1431
bool isThumb() const
Tests whether the target is Thumb (little and big endian).
Definition: Triple.h:836
SubArchType getSubArch() const
get the parsed subarchitecture type for this triple.
Definition: Triple.h:364
@ aarch64_be
Definition: Triple.h:52
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:361
const std::string & str() const
Definition: Triple.h:424
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.
bool isLittleEndian() const
Definition: Binary.h:155
const Elf_Ehdr & getHeader() const
Definition: ELF.h:235
Expected< std::vector< BBAddrMap > > decodeBBAddrMap(const Elf_Shdr &Sec, const Elf_Shdr *RelaSec=nullptr, std::vector< PGOAnalysisMap > *PGOAnalyses=nullptr) const
Returns a vector of BBAddrMap structs corresponding to each function within the text section that the...
Definition: ELF.cpp:868
Expected< StringRef > getSymbolVersionByIndex(uint32_t SymbolVersionIndex, bool &IsDefault, SmallVector< std::optional< VersionEntry >, 0 > &VersionMap, std::optional< bool > IsSymHidden) const
Definition: ELF.h:956
Expected< Elf_Shdr_Range > sections() const
Definition: ELF.h:876
Expected< MapVector< const Elf_Shdr *, const Elf_Shdr * > > getSectionAndRelocations(std::function< Expected< bool >(const Elf_Shdr &)> IsMatch) const
Returns a map from every section matching IsMatch to its relocation section, or nullptr if it has no ...
Definition: ELF.cpp:881
Expected< SmallVector< std::optional< VersionEntry >, 0 > > loadVersionMap(const Elf_Shdr *VerNeedSec, const Elf_Shdr *VerDefSec) const
Definition: ELF.h:670
Expected< const Elf_Shdr * > getSection(const Elf_Sym &Sym, const Elf_Shdr *SymTab, DataRegion< Elf_Word > ShndxTable) const
Definition: ELF.h:528
virtual Error getBuildAttributes(ELFAttributeParser &Attributes) const =0
Expected< std::vector< VersionEntry > > readDynsymVersions() const
Returns a vector containing a symbol version for each dynamic symbol.
virtual elf_symbol_iterator_range getDynamicSymbolIterators() const =0
std::vector< ELFPltEntry > getPltEntries() const
Expected< SubtargetFeatures > getFeatures() const override
std::optional< StringRef > tryGetCPUName() const override
virtual uint16_t getEMachine() const =0
virtual unsigned getPlatformFlags() const =0
Returns platform-specific object flags, if any.
ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
void setARMSubArch(Triple &TheTriple) const override
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 Expected< ELFObjectFile< ELFT > > create(MemoryBufferRef Object, bool InitContent=true)
This class is the base class for all object file types.
Definition: ObjectFile.h:229
static Expected< std::unique_ptr< ObjectFile > > createELFObjectFile(MemoryBufferRef Object, bool InitContent=true)
Triple makeTriple() const
Create a triple from the data in this object file.
Definition: ObjectFile.cpp:109
section_iterator_range sections() const
Definition: ObjectFile.h:328
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
virtual basic_symbol_iterator symbol_end() const =0
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ EM_PPC64
Definition: ELF.h:149
@ EM_386
Definition: ELF.h:136
@ EM_CUDA
Definition: ELF.h:286
@ EM_LOONGARCH
Definition: ELF.h:322
@ EM_PPC
Definition: ELF.h:148
@ EM_MIPS
Definition: ELF.h:141
@ EM_RISCV
Definition: ELF.h:317
@ EM_ARM
Definition: ELF.h:156
@ EM_AMDGPU
Definition: ELF.h:316
@ EF_LOONGARCH_ABI_SINGLE_FLOAT
Definition: ELF.h:986
@ EF_LOONGARCH_ABI_DOUBLE_FLOAT
Definition: ELF.h:987
@ EF_LOONGARCH_ABI_SOFT_FLOAT
Definition: ELF.h:985
@ EF_LOONGARCH_ABI_MODIFIER_MASK
Definition: ELF.h:988
@ ELFDATA2MSB
Definition: ELF.h:336
@ ELFDATA2LSB
Definition: ELF.h:335
@ EF_MIPS_ARCH
Definition: ELF.h:568
@ EF_MIPS_MICROMIPS
Definition: ELF.h:551
@ EF_MIPS_ARCH_32R6
Definition: ELF.h:566
@ EF_MIPS_MACH_NONE
Definition: ELF.h:529
@ EF_MIPS_ARCH_64
Definition: ELF.h:563
@ EF_MIPS_ARCH_32
Definition: ELF.h:562
@ EF_MIPS_MACH_OCTEON
Definition: ELF.h:537
@ EF_MIPS_ARCH_4
Definition: ELF.h:560
@ EF_MIPS_ARCH_5
Definition: ELF.h:561
@ EF_MIPS_ARCH_2
Definition: ELF.h:558
@ EF_MIPS_ARCH_32R2
Definition: ELF.h:564
@ EF_MIPS_ARCH_64R2
Definition: ELF.h:565
@ EF_MIPS_ARCH_ASE_M16
Definition: ELF.h:552
@ EF_MIPS_MACH
Definition: ELF.h:548
@ EF_MIPS_ARCH_1
Definition: ELF.h:557
@ EF_MIPS_ARCH_64R6
Definition: ELF.h:567
@ EF_MIPS_ARCH_3
Definition: ELF.h:559
@ ELFCLASS64
Definition: ELF.h:329
@ ELFCLASS32
Definition: ELF.h:328
@ EF_AMDGPU_MACH_AMDGCN_GFX703
Definition: ELF.h:750
@ EF_AMDGPU_MACH_AMDGCN_GFX1035
Definition: ELF.h:774
@ EF_AMDGPU_MACH_AMDGCN_GFX1031
Definition: ELF.h:768
@ EF_AMDGPU_MACH_R600_CAYMAN
Definition: ELF.h:732
@ EF_AMDGPU_MACH_AMDGCN_GFX704
Definition: ELF.h:751
@ EF_AMDGPU_MACH_AMDGCN_GFX902
Definition: ELF.h:758
@ EF_AMDGPU_MACH_AMDGCN_GFX810
Definition: ELF.h:756
@ EF_AMDGPU_MACH_AMDGCN_GFX1036
Definition: ELF.h:782
@ EF_AMDGPU_MACH_AMDGCN_GFX1102
Definition: ELF.h:784
@ EF_AMDGPU_MACH_R600_RV730
Definition: ELF.h:721
@ EF_AMDGPU_MACH_R600_RV710
Definition: ELF.h:720
@ EF_AMDGPU_MACH_AMDGCN_GFX908
Definition: ELF.h:761
@ EF_AMDGPU_MACH_AMDGCN_GFX1011
Definition: ELF.h:765
@ EF_AMDGPU_MACH_R600_CYPRESS
Definition: ELF.h:725
@ EF_AMDGPU_MACH_AMDGCN_GFX1032
Definition: ELF.h:769
@ EF_AMDGPU_MACH_R600_R600
Definition: ELF.h:715
@ EF_AMDGPU_MACH_AMDGCN_GFX940
Definition: ELF.h:777
@ EF_AMDGPU_MACH_AMDGCN_GFX941
Definition: ELF.h:788
@ EF_AMDGPU_MACH_R600_TURKS
Definition: ELF.h:733
@ EF_AMDGPU_MACH_R600_JUNIPER
Definition: ELF.h:726
@ EF_AMDGPU_MACH_AMDGCN_GFX601
Definition: ELF.h:746
@ EF_AMDGPU_MACH_AMDGCN_GFX942
Definition: ELF.h:789
@ EF_AMDGPU_MACH_R600_R630
Definition: ELF.h:716
@ EF_AMDGPU_MACH_R600_REDWOOD
Definition: ELF.h:727
@ EF_AMDGPU_MACH_R600_RV770
Definition: ELF.h:722
@ EF_AMDGPU_MACH_AMDGCN_GFX600
Definition: ELF.h:745
@ EF_AMDGPU_MACH_AMDGCN_GFX602
Definition: ELF.h:771
@ EF_AMDGPU_MACH_AMDGCN_GFX1101
Definition: ELF.h:783
@ EF_AMDGPU_MACH_AMDGCN_GFX1100
Definition: ELF.h:778
@ EF_AMDGPU_MACH_AMDGCN_GFX1033
Definition: ELF.h:770
@ EF_AMDGPU_MACH_AMDGCN_GFX801
Definition: ELF.h:753
@ EF_AMDGPU_MACH_AMDGCN_GFX705
Definition: ELF.h:772
@ EF_AMDGPU_MACH_AMDGCN_GFX1010
Definition: ELF.h:764
@ EF_AMDGPU_MACH_R600_RV670
Definition: ELF.h:718
@ EF_AMDGPU_MACH_AMDGCN_GFX701
Definition: ELF.h:748
@ EF_AMDGPU_MACH_AMDGCN_GFX10_3_GENERIC
Definition: ELF.h:796
@ EF_AMDGPU_MACH_AMDGCN_GFX1012
Definition: ELF.h:766
@ EF_AMDGPU_MACH_AMDGCN_GFX1151
Definition: ELF.h:787
@ EF_AMDGPU_MACH_AMDGCN_GFX1030
Definition: ELF.h:767
@ EF_AMDGPU_MACH_R600_CEDAR
Definition: ELF.h:724
@ EF_AMDGPU_MACH_AMDGCN_GFX1200
Definition: ELF.h:785
@ EF_AMDGPU_MACH_AMDGCN_GFX700
Definition: ELF.h:747
@ EF_AMDGPU_MACH_AMDGCN_GFX11_GENERIC
Definition: ELF.h:797
@ EF_AMDGPU_MACH_AMDGCN_GFX803
Definition: ELF.h:755
@ EF_AMDGPU_MACH_AMDGCN_GFX802
Definition: ELF.h:754
@ EF_AMDGPU_MACH_AMDGCN_GFX90C
Definition: ELF.h:763
@ EF_AMDGPU_MACH_AMDGCN_GFX900
Definition: ELF.h:757
@ EF_AMDGPU_MACH_AMDGCN_GFX909
Definition: ELF.h:762
@ EF_AMDGPU_MACH
Definition: ELF.h:707
@ EF_AMDGPU_MACH_AMDGCN_GFX906
Definition: ELF.h:760
@ EF_AMDGPU_MACH_AMDGCN_GFX9_GENERIC
Definition: ELF.h:794
@ EF_AMDGPU_MACH_AMDGCN_GFX1103
Definition: ELF.h:781
@ EF_AMDGPU_MACH_R600_CAICOS
Definition: ELF.h:731
@ EF_AMDGPU_MACH_AMDGCN_GFX90A
Definition: ELF.h:776
@ EF_AMDGPU_MACH_AMDGCN_GFX1034
Definition: ELF.h:775
@ EF_AMDGPU_MACH_AMDGCN_GFX1013
Definition: ELF.h:779
@ EF_AMDGPU_MACH_AMDGCN_GFX10_1_GENERIC
Definition: ELF.h:795
@ EF_AMDGPU_MACH_AMDGCN_GFX904
Definition: ELF.h:759
@ EF_AMDGPU_MACH_R600_RS880
Definition: ELF.h:717
@ EF_AMDGPU_MACH_AMDGCN_GFX805
Definition: ELF.h:773
@ EF_AMDGPU_MACH_AMDGCN_GFX1201
Definition: ELF.h:791
@ EF_AMDGPU_MACH_AMDGCN_GFX1150
Definition: ELF.h:780
@ EF_AMDGPU_MACH_R600_SUMO
Definition: ELF.h:728
@ EF_AMDGPU_MACH_R600_BARTS
Definition: ELF.h:730
@ EF_AMDGPU_MACH_AMDGCN_GFX702
Definition: ELF.h:749
@ EF_RISCV_RVC
Definition: ELF.h:673
@ SHT_LLVM_BB_ADDR_MAP_V0
Definition: ELF.h:1096
@ SHT_GNU_verneed
Definition: ELF.h:1109
@ SHT_GNU_verdef
Definition: ELF.h:1108
@ SHT_LLVM_BB_ADDR_MAP
Definition: ELF.h:1100
@ SHT_GNU_versym
Definition: ELF.h:1110
@ EF_CUDA_SM21
Definition: ELF.h:872
@ EF_CUDA_SM90
Definition: ELF.h:891
@ EF_CUDA_SM86
Definition: ELF.h:887
@ EF_CUDA_SM60
Definition: ELF.h:880
@ EF_CUDA_SM
Definition: ELF.h:868
@ EF_CUDA_SM89
Definition: ELF.h:889
@ EF_CUDA_SM37
Definition: ELF.h:876
@ EF_CUDA_SM32
Definition: ELF.h:874
@ EF_CUDA_SM72
Definition: ELF.h:884
@ EF_CUDA_SM50
Definition: ELF.h:877
@ EF_CUDA_ACCELERATORS
Definition: ELF.h:900
@ EF_CUDA_SM61
Definition: ELF.h:881
@ EF_CUDA_SM52
Definition: ELF.h:878
@ EF_CUDA_SM35
Definition: ELF.h:875
@ EF_CUDA_SM62
Definition: ELF.h:882
@ EF_CUDA_SM30
Definition: ELF.h:873
@ EF_CUDA_SM75
Definition: ELF.h:885
@ EF_CUDA_SM87
Definition: ELF.h:888
@ EF_CUDA_SM20
Definition: ELF.h:871
@ EF_CUDA_SM80
Definition: ELF.h:886
@ EF_CUDA_SM53
Definition: ELF.h:879
@ EF_CUDA_SM70
Definition: ELF.h:883
@ ET_REL
Definition: ELF.h:116
@ STT_FUNC
Definition: ELF.h:1322
@ STT_NOTYPE
Definition: ELF.h:1320
@ STT_SECTION
Definition: ELF.h:1323
@ STT_FILE
Definition: ELF.h:1324
@ STT_COMMON
Definition: ELF.h:1325
@ STT_GNU_IFUNC
Definition: ELF.h:1327
@ STT_OBJECT
Definition: ELF.h:1321
@ STT_TLS
Definition: ELF.h:1326
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
Error createError(const Twine &Err)
Definition: Error.h:84
constexpr int NumElfSymbolTypes
Definition: ELFObjectFile.h:45
static std::string describe(const ELFFile< ELFT > &Obj, const typename ELFT::Shdr &Sec)
Definition: ELF.h:142
std::pair< unsigned char, unsigned char > getElfArchType(StringRef Object)
Definition: ELF.h:77
const llvm::EnumEntry< unsigned > ElfSymbolTypes[NumElfSymbolTypes]
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2082
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition: bit.h:215
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:749
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1041
static const Target * lookupTarget(StringRef Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.