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"
30#include <algorithm>
31#include <cstddef>
32#include <cstdint>
33#include <memory>
34#include <optional>
35#include <string>
36#include <utility>
37
38using namespace llvm;
39using namespace object;
40
42 {"None", "NOTYPE", ELF::STT_NOTYPE},
43 {"Object", "OBJECT", ELF::STT_OBJECT},
44 {"Function", "FUNC", ELF::STT_FUNC},
45 {"Section", "SECTION", ELF::STT_SECTION},
46 {"File", "FILE", ELF::STT_FILE},
47 {"Common", "COMMON", ELF::STT_COMMON},
48 {"TLS", "TLS", ELF::STT_TLS},
49 {"Unknown", "<unknown>: 7", 7},
50 {"Unknown", "<unknown>: 8", 8},
51 {"Unknown", "<unknown>: 9", 9},
52 {"GNU_IFunc", "IFUNC", ELF::STT_GNU_IFUNC},
53 {"OS Specific", "<OS specific>: 11", 11},
54 {"OS Specific", "<OS specific>: 12", 12},
55 {"Proc Specific", "<processor specific>: 13", 13},
56 {"Proc Specific", "<processor specific>: 14", 14},
57 {"Proc Specific", "<processor specific>: 15", 15}
58};
59
61 : ObjectFile(Type, Source) {}
62
63template <class ELFT>
65createPtr(MemoryBufferRef Object, bool InitContent) {
66 auto Ret = ELFObjectFile<ELFT>::create(Object, InitContent);
67 if (Error E = Ret.takeError())
68 return std::move(E);
69 return std::make_unique<ELFObjectFile<ELFT>>(std::move(*Ret));
70}
71
74 std::pair<unsigned char, unsigned char> Ident =
76 std::size_t MaxAlignment =
77 1ULL << llvm::countr_zero(
78 reinterpret_cast<uintptr_t>(Obj.getBufferStart()));
79
80 if (MaxAlignment < 2)
81 return createError("Insufficient alignment");
82
83 if (Ident.first == ELF::ELFCLASS32) {
84 if (Ident.second == ELF::ELFDATA2LSB)
85 return createPtr<ELF32LE>(Obj, InitContent);
86 else if (Ident.second == ELF::ELFDATA2MSB)
87 return createPtr<ELF32BE>(Obj, InitContent);
88 else
89 return createError("Invalid ELF data");
90 } else if (Ident.first == ELF::ELFCLASS64) {
91 if (Ident.second == ELF::ELFDATA2LSB)
92 return createPtr<ELF64LE>(Obj, InitContent);
93 else if (Ident.second == ELF::ELFDATA2MSB)
94 return createPtr<ELF64BE>(Obj, InitContent);
95 else
96 return createError("Invalid ELF data");
97 }
98 return createError("Invalid ELF class");
99}
100
101SubtargetFeatures ELFObjectFileBase::getMIPSFeatures() const {
102 SubtargetFeatures Features;
103 unsigned PlatformFlags = getPlatformFlags();
104
105 switch (PlatformFlags & ELF::EF_MIPS_ARCH) {
107 break;
109 Features.AddFeature("mips2");
110 break;
112 Features.AddFeature("mips3");
113 break;
115 Features.AddFeature("mips4");
116 break;
118 Features.AddFeature("mips5");
119 break;
121 Features.AddFeature("mips32");
122 break;
124 Features.AddFeature("mips64");
125 break;
127 Features.AddFeature("mips32r2");
128 break;
130 Features.AddFeature("mips64r2");
131 break;
133 Features.AddFeature("mips32r6");
134 break;
136 Features.AddFeature("mips64r6");
137 break;
138 default:
139 llvm_unreachable("Unknown EF_MIPS_ARCH value");
140 }
141
142 switch (PlatformFlags & ELF::EF_MIPS_MACH) {
144 // No feature associated with this value.
145 break;
147 Features.AddFeature("cnmips");
148 break;
149 default:
150 llvm_unreachable("Unknown EF_MIPS_ARCH value");
151 }
152
153 if (PlatformFlags & ELF::EF_MIPS_ARCH_ASE_M16)
154 Features.AddFeature("mips16");
155 if (PlatformFlags & ELF::EF_MIPS_MICROMIPS)
156 Features.AddFeature("micromips");
157
158 return Features;
159}
160
161SubtargetFeatures ELFObjectFileBase::getARMFeatures() const {
162 SubtargetFeatures Features;
164 if (Error E = getBuildAttributes(Attributes)) {
165 consumeError(std::move(E));
166 return SubtargetFeatures();
167 }
168
169 // both ARMv7-M and R have to support thumb hardware div
170 bool isV7 = false;
171 std::optional<unsigned> Attr =
172 Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
173 if (Attr)
174 isV7 = *Attr == ARMBuildAttrs::v7;
175
176 Attr = Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile);
177 if (Attr) {
178 switch (*Attr) {
180 Features.AddFeature("aclass");
181 break;
183 Features.AddFeature("rclass");
184 if (isV7)
185 Features.AddFeature("hwdiv");
186 break;
188 Features.AddFeature("mclass");
189 if (isV7)
190 Features.AddFeature("hwdiv");
191 break;
192 }
193 }
194
195 Attr = Attributes.getAttributeValue(ARMBuildAttrs::THUMB_ISA_use);
196 if (Attr) {
197 switch (*Attr) {
198 default:
199 break;
201 Features.AddFeature("thumb", false);
202 Features.AddFeature("thumb2", false);
203 break;
205 Features.AddFeature("thumb2");
206 break;
207 }
208 }
209
210 Attr = Attributes.getAttributeValue(ARMBuildAttrs::FP_arch);
211 if (Attr) {
212 switch (*Attr) {
213 default:
214 break;
216 Features.AddFeature("vfp2sp", false);
217 Features.AddFeature("vfp3d16sp", false);
218 Features.AddFeature("vfp4d16sp", false);
219 break;
221 Features.AddFeature("vfp2");
222 break;
225 Features.AddFeature("vfp3");
226 break;
229 Features.AddFeature("vfp4");
230 break;
231 }
232 }
233
234 Attr = Attributes.getAttributeValue(ARMBuildAttrs::Advanced_SIMD_arch);
235 if (Attr) {
236 switch (*Attr) {
237 default:
238 break;
240 Features.AddFeature("neon", false);
241 Features.AddFeature("fp16", false);
242 break;
244 Features.AddFeature("neon");
245 break;
247 Features.AddFeature("neon");
248 Features.AddFeature("fp16");
249 break;
250 }
251 }
252
253 Attr = Attributes.getAttributeValue(ARMBuildAttrs::MVE_arch);
254 if (Attr) {
255 switch (*Attr) {
256 default:
257 break;
259 Features.AddFeature("mve", false);
260 Features.AddFeature("mve.fp", false);
261 break;
263 Features.AddFeature("mve.fp", false);
264 Features.AddFeature("mve");
265 break;
267 Features.AddFeature("mve.fp");
268 break;
269 }
270 }
271
272 Attr = Attributes.getAttributeValue(ARMBuildAttrs::DIV_use);
273 if (Attr) {
274 switch (*Attr) {
275 default:
276 break;
278 Features.AddFeature("hwdiv", false);
279 Features.AddFeature("hwdiv-arm", false);
280 break;
282 Features.AddFeature("hwdiv");
283 Features.AddFeature("hwdiv-arm");
284 break;
285 }
286 }
287
288 return Features;
289}
290
291static std::optional<std::string> hexagonAttrToFeatureString(unsigned Attr) {
292 switch (Attr) {
293 case 5:
294 return "v5";
295 case 55:
296 return "v55";
297 case 60:
298 return "v60";
299 case 62:
300 return "v62";
301 case 65:
302 return "v65";
303 case 67:
304 return "v67";
305 case 68:
306 return "v68";
307 case 69:
308 return "v69";
309 case 71:
310 return "v71";
311 case 73:
312 return "v73";
313 default:
314 return {};
315 }
316}
317
318SubtargetFeatures ELFObjectFileBase::getHexagonFeatures() const {
319 SubtargetFeatures Features;
322 // Return no attributes if none can be read.
323 // This behavior is important for backwards compatibility.
324 consumeError(std::move(E));
325 return Features;
326 }
327 std::optional<unsigned> Attr;
328
329 if ((Attr = Parser.getAttributeValue(HexagonAttrs::ARCH))) {
330 if (std::optional<std::string> FeatureString =
332 Features.AddFeature(*FeatureString);
333 }
334
335 if ((Attr = Parser.getAttributeValue(HexagonAttrs::HVXARCH))) {
336 std::optional<std::string> FeatureString =
338 // There is no corresponding hvx arch for v5 and v55.
339 if (FeatureString && *Attr >= 60)
340 Features.AddFeature("hvx" + *FeatureString);
341 }
342
343 if ((Attr = Parser.getAttributeValue(HexagonAttrs::HVXIEEEFP)))
344 if (*Attr)
345 Features.AddFeature("hvx-ieee-fp");
346
347 if ((Attr = Parser.getAttributeValue(HexagonAttrs::HVXQFLOAT)))
348 if (*Attr)
349 Features.AddFeature("hvx-qfloat");
350
351 if ((Attr = Parser.getAttributeValue(HexagonAttrs::ZREG)))
352 if (*Attr)
353 Features.AddFeature("zreg");
354
355 if ((Attr = Parser.getAttributeValue(HexagonAttrs::AUDIO)))
356 if (*Attr)
357 Features.AddFeature("audio");
358
359 if ((Attr = Parser.getAttributeValue(HexagonAttrs::CABAC)))
360 if (*Attr)
361 Features.AddFeature("cabac");
362
363 return Features;
364}
365
366Expected<SubtargetFeatures> ELFObjectFileBase::getRISCVFeatures() const {
367 SubtargetFeatures Features;
368 unsigned PlatformFlags = getPlatformFlags();
369
370 if (PlatformFlags & ELF::EF_RISCV_RVC) {
371 Features.AddFeature("zca");
372 }
373
375 if (Error E = getBuildAttributes(Attributes)) {
376 return std::move(E);
377 }
378
379 std::optional<StringRef> Attr =
380 Attributes.getAttributeString(RISCVAttrs::ARCH);
381 if (Attr) {
383 if (!ParseResult)
384 return ParseResult.takeError();
385 auto &ISAInfo = *ParseResult;
386
387 if (ISAInfo->getXLen() == 32)
388 Features.AddFeature("64bit", false);
389 else if (ISAInfo->getXLen() == 64)
390 Features.AddFeature("64bit");
391 else
392 llvm_unreachable("XLEN should be 32 or 64.");
393
394 Features.addFeaturesVector(ISAInfo->toFeatures());
395 }
396
397 return Features;
398}
399
400SubtargetFeatures ELFObjectFileBase::getLoongArchFeatures() const {
401 SubtargetFeatures Features;
402
405 break;
407 Features.AddFeature("d");
408 // D implies F according to LoongArch ISA spec.
409 [[fallthrough]];
411 Features.AddFeature("f");
412 break;
413 }
414
415 return Features;
416}
417
419 switch (getEMachine()) {
420 case ELF::EM_MIPS:
421 return getMIPSFeatures();
422 case ELF::EM_ARM:
423 return getARMFeatures();
424 case ELF::EM_RISCV:
425 return getRISCVFeatures();
427 return getLoongArchFeatures();
428 case ELF::EM_HEXAGON:
429 return getHexagonFeatures();
430 default:
431 return SubtargetFeatures();
432 }
433}
434
435std::optional<StringRef> ELFObjectFileBase::tryGetCPUName() const {
436 switch (getEMachine()) {
437 case ELF::EM_AMDGPU:
438 return getAMDGPUCPUName();
439 case ELF::EM_CUDA:
440 return getNVPTXCPUName();
441 case ELF::EM_PPC:
442 case ELF::EM_PPC64:
443 return StringRef("future");
444 default:
445 return std::nullopt;
446 }
447}
448
449StringRef ELFObjectFileBase::getAMDGPUCPUName() const {
451 unsigned CPU = getPlatformFlags() & ELF::EF_AMDGPU_MACH;
452
453 switch (CPU) {
454 // Radeon HD 2000/3000 Series (R600).
456 return "r600";
458 return "r630";
460 return "rs880";
462 return "rv670";
463
464 // Radeon HD 4000 Series (R700).
466 return "rv710";
468 return "rv730";
470 return "rv770";
471
472 // Radeon HD 5000 Series (Evergreen).
474 return "cedar";
476 return "cypress";
478 return "juniper";
480 return "redwood";
482 return "sumo";
483
484 // Radeon HD 6000 Series (Northern Islands).
486 return "barts";
488 return "caicos";
490 return "cayman";
492 return "turks";
493
494 // AMDGCN GFX6.
496 return "gfx600";
498 return "gfx601";
500 return "gfx602";
501
502 // AMDGCN GFX7.
504 return "gfx700";
506 return "gfx701";
508 return "gfx702";
510 return "gfx703";
512 return "gfx704";
514 return "gfx705";
515
516 // AMDGCN GFX8.
518 return "gfx801";
520 return "gfx802";
522 return "gfx803";
524 return "gfx805";
526 return "gfx810";
527
528 // AMDGCN GFX9.
530 return "gfx900";
532 return "gfx902";
534 return "gfx904";
536 return "gfx906";
538 return "gfx908";
540 return "gfx909";
542 return "gfx90a";
544 return "gfx90c";
546 return "gfx940";
548 return "gfx941";
550 return "gfx942";
551
552 // AMDGCN GFX10.
554 return "gfx1010";
556 return "gfx1011";
558 return "gfx1012";
560 return "gfx1013";
562 return "gfx1030";
564 return "gfx1031";
566 return "gfx1032";
568 return "gfx1033";
570 return "gfx1034";
572 return "gfx1035";
574 return "gfx1036";
575
576 // AMDGCN GFX11.
578 return "gfx1100";
580 return "gfx1101";
582 return "gfx1102";
584 return "gfx1103";
586 return "gfx1150";
588 return "gfx1151";
590 return "gfx1152";
591
592 // AMDGCN GFX12.
594 return "gfx1200";
596 return "gfx1201";
597
598 // Generic AMDGCN targets
600 return "gfx9-generic";
602 return "gfx10-1-generic";
604 return "gfx10-3-generic";
606 return "gfx11-generic";
608 return "gfx12-generic";
609 default:
610 llvm_unreachable("Unknown EF_AMDGPU_MACH value");
611 }
612}
613
614StringRef ELFObjectFileBase::getNVPTXCPUName() const {
616 unsigned SM = getPlatformFlags() & ELF::EF_CUDA_SM;
617
618 switch (SM) {
619 // Fermi architecture.
621 return "sm_20";
623 return "sm_21";
624
625 // Kepler architecture.
627 return "sm_30";
629 return "sm_32";
631 return "sm_35";
633 return "sm_37";
634
635 // Maxwell architecture.
637 return "sm_50";
639 return "sm_52";
641 return "sm_53";
642
643 // Pascal architecture.
645 return "sm_60";
647 return "sm_61";
649 return "sm_62";
650
651 // Volta architecture.
653 return "sm_70";
655 return "sm_72";
656
657 // Turing architecture.
659 return "sm_75";
660
661 // Ampere architecture.
663 return "sm_80";
665 return "sm_86";
667 return "sm_87";
668
669 // Ada architecture.
671 return "sm_89";
672
673 // Hopper architecture.
675 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_90a" : "sm_90";
676 default:
677 llvm_unreachable("Unknown EF_CUDA_SM value");
678 }
679}
680
681// FIXME Encode from a tablegen description or target parser.
683 if (TheTriple.getSubArch() != Triple::NoSubArch)
684 return;
685
688 // TODO Propagate Error.
689 consumeError(std::move(E));
690 return;
691 }
692
693 std::string Triple;
694 // Default to ARM, but use the triple if it's been set.
695 if (TheTriple.isThumb())
696 Triple = "thumb";
697 else
698 Triple = "arm";
699
700 std::optional<unsigned> Attr =
701 Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
702 if (Attr) {
703 switch (*Attr) {
705 Triple += "v4";
706 break;
708 Triple += "v4t";
709 break;
711 Triple += "v5t";
712 break;
714 Triple += "v5te";
715 break;
717 Triple += "v5tej";
718 break;
720 Triple += "v6";
721 break;
723 Triple += "v6kz";
724 break;
726 Triple += "v6t2";
727 break;
729 Triple += "v6k";
730 break;
731 case ARMBuildAttrs::v7: {
732 std::optional<unsigned> ArchProfileAttr =
734 if (ArchProfileAttr &&
735 *ArchProfileAttr == ARMBuildAttrs::MicroControllerProfile)
736 Triple += "v7m";
737 else
738 Triple += "v7";
739 break;
740 }
742 Triple += "v6m";
743 break;
745 Triple += "v6sm";
746 break;
748 Triple += "v7em";
749 break;
751 Triple += "v8a";
752 break;
754 Triple += "v8r";
755 break;
757 Triple += "v8m.base";
758 break;
760 Triple += "v8m.main";
761 break;
763 Triple += "v8.1m.main";
764 break;
766 Triple += "v9a";
767 break;
768 }
769 }
770 if (!isLittleEndian())
771 Triple += "eb";
772
773 TheTriple.setArchName(Triple);
774}
775
776std::vector<ELFPltEntry> ELFObjectFileBase::getPltEntries() const {
777 std::string Err;
778 const auto Triple = makeTriple();
779 const auto *T = TargetRegistry::lookupTarget(Triple.str(), Err);
780 if (!T)
781 return {};
782 uint32_t JumpSlotReloc = 0, GlobDatReloc = 0;
783 switch (Triple.getArch()) {
784 case Triple::x86:
785 JumpSlotReloc = ELF::R_386_JUMP_SLOT;
786 GlobDatReloc = ELF::R_386_GLOB_DAT;
787 break;
788 case Triple::x86_64:
789 JumpSlotReloc = ELF::R_X86_64_JUMP_SLOT;
790 GlobDatReloc = ELF::R_X86_64_GLOB_DAT;
791 break;
792 case Triple::aarch64:
794 JumpSlotReloc = ELF::R_AARCH64_JUMP_SLOT;
795 break;
796 default:
797 return {};
798 }
799 std::unique_ptr<const MCInstrInfo> MII(T->createMCInstrInfo());
800 std::unique_ptr<const MCInstrAnalysis> MIA(
801 T->createMCInstrAnalysis(MII.get()));
802 if (!MIA)
803 return {};
804 std::vector<std::pair<uint64_t, uint64_t>> PltEntries;
805 std::optional<SectionRef> RelaPlt, RelaDyn;
806 uint64_t GotBaseVA = 0;
807 for (const SectionRef &Section : sections()) {
808 Expected<StringRef> NameOrErr = Section.getName();
809 if (!NameOrErr) {
810 consumeError(NameOrErr.takeError());
811 continue;
812 }
813 StringRef Name = *NameOrErr;
814
815 if (Name == ".rela.plt" || Name == ".rel.plt") {
816 RelaPlt = Section;
817 } else if (Name == ".rela.dyn" || Name == ".rel.dyn") {
818 RelaDyn = Section;
819 } else if (Name == ".got.plt") {
820 GotBaseVA = Section.getAddress();
821 } else if (Name == ".plt" || Name == ".plt.got") {
822 Expected<StringRef> PltContents = Section.getContents();
823 if (!PltContents) {
824 consumeError(PltContents.takeError());
825 return {};
826 }
828 PltEntries,
829 MIA->findPltEntries(Section.getAddress(),
830 arrayRefFromStringRef(*PltContents), Triple));
831 }
832 }
833
834 // Build a map from GOT entry virtual address to PLT entry virtual address.
836 for (auto [Plt, GotPlt] : PltEntries) {
837 uint64_t GotPltEntry = GotPlt;
838 // An x86-32 PIC PLT uses jmp DWORD PTR [ebx-offset]. Add
839 // _GLOBAL_OFFSET_TABLE_ (EBX) to get the .got.plt (or .got) entry address.
840 // See X86MCTargetDesc.cpp:findPltEntries for the 1 << 32 bit.
841 if (GotPltEntry & (uint64_t(1) << 32) && getEMachine() == ELF::EM_386)
842 GotPltEntry = static_cast<int32_t>(GotPltEntry) + GotBaseVA;
843 GotToPlt.insert(std::make_pair(GotPltEntry, Plt));
844 }
845
846 // Find the relocations in the dynamic relocation table that point to
847 // locations in the GOT for which we know the corresponding PLT entry.
848 std::vector<ELFPltEntry> Result;
849 auto handleRels = [&](iterator_range<relocation_iterator> Rels,
850 uint32_t RelType, StringRef PltSec) {
851 for (const auto &R : Rels) {
852 if (R.getType() != RelType)
853 continue;
854 auto PltEntryIter = GotToPlt.find(R.getOffset());
855 if (PltEntryIter != GotToPlt.end()) {
856 symbol_iterator Sym = R.getSymbol();
857 if (Sym == symbol_end())
858 Result.push_back(
859 ELFPltEntry{PltSec, std::nullopt, PltEntryIter->second});
860 else
861 Result.push_back(ELFPltEntry{PltSec, Sym->getRawDataRefImpl(),
862 PltEntryIter->second});
863 }
864 }
865 };
866
867 if (RelaPlt)
868 handleRels(RelaPlt->relocations(), JumpSlotReloc, ".plt");
869
870 // If a symbol needing a PLT entry also needs a GLOB_DAT relocation, GNU ld's
871 // x86 port places the PLT entry in the .plt.got section.
872 if (RelaDyn)
873 handleRels(RelaDyn->relocations(), GlobDatReloc, ".plt.got");
874
875 return Result;
876}
877
878template <class ELFT>
880 const ELFFile<ELFT> &EF, std::optional<unsigned> TextSectionIndex,
881 std::vector<PGOAnalysisMap> *PGOAnalyses) {
882 using Elf_Shdr = typename ELFT::Shdr;
883 bool IsRelocatable = EF.getHeader().e_type == ELF::ET_REL;
884 std::vector<BBAddrMap> BBAddrMaps;
885 if (PGOAnalyses)
886 PGOAnalyses->clear();
887
888 const auto &Sections = cantFail(EF.sections());
889 auto IsMatch = [&](const Elf_Shdr &Sec) -> Expected<bool> {
890 if (Sec.sh_type != ELF::SHT_LLVM_BB_ADDR_MAP &&
891 Sec.sh_type != ELF::SHT_LLVM_BB_ADDR_MAP_V0)
892 return false;
893 if (!TextSectionIndex)
894 return true;
895 Expected<const Elf_Shdr *> TextSecOrErr = EF.getSection(Sec.sh_link);
896 if (!TextSecOrErr)
897 return createError("unable to get the linked-to section for " +
898 describe(EF, Sec) + ": " +
899 toString(TextSecOrErr.takeError()));
900 assert(*TextSecOrErr >= Sections.begin() &&
901 "Text section pointer outside of bounds");
902 if (*TextSectionIndex !=
903 (unsigned)std::distance(Sections.begin(), *TextSecOrErr))
904 return false;
905 return true;
906 };
907
909 EF.getSectionAndRelocations(IsMatch);
910 if (!SectionRelocMapOrErr)
911 return SectionRelocMapOrErr.takeError();
912
913 for (auto const &[Sec, RelocSec] : *SectionRelocMapOrErr) {
914 if (IsRelocatable && !RelocSec)
915 return createError("unable to get relocation section for " +
916 describe(EF, *Sec));
917 Expected<std::vector<BBAddrMap>> BBAddrMapOrErr =
918 EF.decodeBBAddrMap(*Sec, RelocSec, PGOAnalyses);
919 if (!BBAddrMapOrErr) {
920 if (PGOAnalyses)
921 PGOAnalyses->clear();
922 return createError("unable to read " + describe(EF, *Sec) + ": " +
923 toString(BBAddrMapOrErr.takeError()));
924 }
925 std::move(BBAddrMapOrErr->begin(), BBAddrMapOrErr->end(),
926 std::back_inserter(BBAddrMaps));
927 }
928 if (PGOAnalyses)
929 assert(PGOAnalyses->size() == BBAddrMaps.size() &&
930 "The same number of BBAddrMaps and PGOAnalysisMaps should be "
931 "returned when PGO information is requested");
932 return BBAddrMaps;
933}
934
935template <class ELFT>
939 using Elf_Shdr = typename ELFT::Shdr;
940 const Elf_Shdr *VerSec = nullptr;
941 const Elf_Shdr *VerNeedSec = nullptr;
942 const Elf_Shdr *VerDefSec = nullptr;
943 // The user should ensure sections() can't fail here.
944 for (const Elf_Shdr &Sec : cantFail(EF.sections())) {
945 if (Sec.sh_type == ELF::SHT_GNU_versym)
946 VerSec = &Sec;
947 else if (Sec.sh_type == ELF::SHT_GNU_verdef)
948 VerDefSec = &Sec;
949 else if (Sec.sh_type == ELF::SHT_GNU_verneed)
950 VerNeedSec = &Sec;
951 }
952 if (!VerSec)
953 return std::vector<VersionEntry>();
954
956 EF.loadVersionMap(VerNeedSec, VerDefSec);
957 if (!MapOrErr)
958 return MapOrErr.takeError();
959
960 std::vector<VersionEntry> Ret;
961 size_t I = 0;
962 for (const ELFSymbolRef &Sym : Symbols) {
963 ++I;
965 EF.template getEntry<typename ELFT::Versym>(*VerSec, I);
966 if (!VerEntryOrErr)
967 return createError("unable to read an entry with index " + Twine(I) +
968 " from " + describe(EF, *VerSec) + ": " +
969 toString(VerEntryOrErr.takeError()));
970
971 Expected<uint32_t> FlagsOrErr = Sym.getFlags();
972 if (!FlagsOrErr)
973 return createError("unable to read flags for symbol with index " +
974 Twine(I) + ": " + toString(FlagsOrErr.takeError()));
975
976 bool IsDefault;
978 (*VerEntryOrErr)->vs_index, IsDefault, *MapOrErr,
979 (*FlagsOrErr) & SymbolRef::SF_Undefined);
980 if (!VerOrErr)
981 return createError("unable to get a version for entry " + Twine(I) +
982 " of " + describe(EF, *VerSec) + ": " +
983 toString(VerOrErr.takeError()));
984
985 Ret.push_back({(*VerOrErr).str(), IsDefault});
986 }
987
988 return Ret;
989}
990
994 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
995 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
996 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
997 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
998 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
999 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
1000 return readDynsymVersionsImpl(cast<ELF64BEObjectFile>(this)->getELFFile(),
1001 Symbols);
1002}
1003
1005 std::optional<unsigned> TextSectionIndex,
1006 std::vector<PGOAnalysisMap> *PGOAnalyses) const {
1007 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
1008 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
1009 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
1010 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
1011 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
1012 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
1013 return readBBAddrMapImpl(cast<ELF64BEObjectFile>(this)->getELFFile(),
1014 TextSectionIndex, PGOAnalyses);
1015}
1016
1018 auto Data = Sec.getRawDataRefImpl();
1019 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
1020 return Obj->getCrelDecodeProblem(Data);
1021 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
1022 return Obj->getCrelDecodeProblem(Data);
1023 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
1024 return Obj->getCrelDecodeProblem(Data);
1025 return cast<ELF64BEObjectFile>(this)->getCrelDecodeProblem(Data);
1026}
AMDGPU Kernel Attributes
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 std::optional< std::string > hexagonAttrToFeatureString(unsigned Attr)
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:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
const char * getBufferStart() const
StringRef getBuffer() const
This class represents success/failure for parsing-like operations that find it important to chain tog...
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:1543
bool isThumb() const
Tests whether the target is Thumb (little and big endian).
Definition: Triple.h:850
SubArchType getSubArch() const
get the parsed subarchitecture type for this triple.
Definition: Triple.h:374
@ aarch64_be
Definition: Triple.h:52
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:371
const std::string & str() const
Definition: Triple.h:438
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.
MemoryBufferRef Data
Definition: Binary.h:37
bool isLittleEndian() const
Definition: Binary.h:155
const Elf_Ehdr & getHeader() const
Definition: ELF.h:278
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:928
Expected< StringRef > getSymbolVersionByIndex(uint32_t SymbolVersionIndex, bool &IsDefault, SmallVector< std::optional< VersionEntry >, 0 > &VersionMap, std::optional< bool > IsSymHidden) const
Definition: ELF.h:1004
Expected< Elf_Shdr_Range > sections() const
Definition: ELF.h:924
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:941
Expected< SmallVector< std::optional< VersionEntry >, 0 > > loadVersionMap(const Elf_Shdr *VerNeedSec, const Elf_Shdr *VerDefSec) const
Definition: ELF.h:718
Expected< const Elf_Shdr * > getSection(const Elf_Sym &Sym, const Elf_Shdr *SymTab, DataRegion< Elf_Word > ShndxTable) const
Definition: ELF.h:576
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
StringRef getCrelDecodeProblem(SectionRef Sec) const
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:329
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:598
virtual basic_symbol_iterator symbol_end() const =0
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ ET_REL
Definition: ELF.h:117
@ EM_PPC64
Definition: ELF.h:150
@ EM_386
Definition: ELF.h:137
@ EM_CUDA
Definition: ELF.h:287
@ EM_LOONGARCH
Definition: ELF.h:323
@ EM_PPC
Definition: ELF.h:149
@ EM_HEXAGON
Definition: ELF.h:258
@ EM_MIPS
Definition: ELF.h:142
@ EM_RISCV
Definition: ELF.h:318
@ EM_ARM
Definition: ELF.h:157
@ EM_AMDGPU
Definition: ELF.h:317
@ ELFCLASS64
Definition: ELF.h:330
@ ELFCLASS32
Definition: ELF.h:329
@ SHT_LLVM_BB_ADDR_MAP_V0
Definition: ELF.h:1104
@ SHT_GNU_verneed
Definition: ELF.h:1117
@ SHT_GNU_verdef
Definition: ELF.h:1116
@ SHT_LLVM_BB_ADDR_MAP
Definition: ELF.h:1108
@ SHT_GNU_versym
Definition: ELF.h:1118
@ EF_AMDGPU_MACH_AMDGCN_GFX703
Definition: ELF.h:751
@ EF_AMDGPU_MACH_AMDGCN_GFX1035
Definition: ELF.h:775
@ EF_AMDGPU_MACH_AMDGCN_GFX1031
Definition: ELF.h:769
@ EF_AMDGPU_MACH_R600_CAYMAN
Definition: ELF.h:733
@ EF_AMDGPU_MACH_AMDGCN_GFX704
Definition: ELF.h:752
@ EF_AMDGPU_MACH_AMDGCN_GFX902
Definition: ELF.h:759
@ EF_AMDGPU_MACH_AMDGCN_GFX810
Definition: ELF.h:757
@ EF_AMDGPU_MACH_AMDGCN_GFX1036
Definition: ELF.h:783
@ EF_AMDGPU_MACH_AMDGCN_GFX1102
Definition: ELF.h:785
@ EF_AMDGPU_MACH_R600_RV730
Definition: ELF.h:722
@ EF_AMDGPU_MACH_R600_RV710
Definition: ELF.h:721
@ EF_AMDGPU_MACH_AMDGCN_GFX908
Definition: ELF.h:762
@ EF_AMDGPU_MACH_AMDGCN_GFX1011
Definition: ELF.h:766
@ EF_AMDGPU_MACH_R600_CYPRESS
Definition: ELF.h:726
@ EF_AMDGPU_MACH_AMDGCN_GFX1032
Definition: ELF.h:770
@ EF_AMDGPU_MACH_R600_R600
Definition: ELF.h:716
@ EF_AMDGPU_MACH_AMDGCN_GFX940
Definition: ELF.h:778
@ EF_AMDGPU_MACH_AMDGCN_GFX941
Definition: ELF.h:789
@ EF_AMDGPU_MACH_R600_TURKS
Definition: ELF.h:734
@ EF_AMDGPU_MACH_R600_JUNIPER
Definition: ELF.h:727
@ EF_AMDGPU_MACH_AMDGCN_GFX601
Definition: ELF.h:747
@ EF_AMDGPU_MACH_AMDGCN_GFX942
Definition: ELF.h:790
@ EF_AMDGPU_MACH_AMDGCN_GFX1152
Definition: ELF.h:799
@ EF_AMDGPU_MACH_R600_R630
Definition: ELF.h:717
@ EF_AMDGPU_MACH_R600_REDWOOD
Definition: ELF.h:728
@ EF_AMDGPU_MACH_R600_RV770
Definition: ELF.h:723
@ EF_AMDGPU_MACH_AMDGCN_GFX600
Definition: ELF.h:746
@ EF_AMDGPU_MACH_AMDGCN_GFX602
Definition: ELF.h:772
@ EF_AMDGPU_MACH_AMDGCN_GFX1101
Definition: ELF.h:784
@ EF_AMDGPU_MACH_AMDGCN_GFX1100
Definition: ELF.h:779
@ EF_AMDGPU_MACH_AMDGCN_GFX1033
Definition: ELF.h:771
@ EF_AMDGPU_MACH_AMDGCN_GFX801
Definition: ELF.h:754
@ EF_AMDGPU_MACH_AMDGCN_GFX705
Definition: ELF.h:773
@ EF_AMDGPU_MACH_AMDGCN_GFX1010
Definition: ELF.h:765
@ EF_AMDGPU_MACH_R600_RV670
Definition: ELF.h:719
@ EF_AMDGPU_MACH_AMDGCN_GFX701
Definition: ELF.h:749
@ EF_AMDGPU_MACH_AMDGCN_GFX10_3_GENERIC
Definition: ELF.h:797
@ EF_AMDGPU_MACH_AMDGCN_GFX1012
Definition: ELF.h:767
@ EF_AMDGPU_MACH_AMDGCN_GFX1151
Definition: ELF.h:788
@ EF_AMDGPU_MACH_AMDGCN_GFX1030
Definition: ELF.h:768
@ EF_AMDGPU_MACH_R600_CEDAR
Definition: ELF.h:725
@ EF_AMDGPU_MACH_AMDGCN_GFX1200
Definition: ELF.h:786
@ EF_AMDGPU_MACH_AMDGCN_GFX700
Definition: ELF.h:748
@ EF_AMDGPU_MACH_AMDGCN_GFX11_GENERIC
Definition: ELF.h:798
@ EF_AMDGPU_MACH_AMDGCN_GFX803
Definition: ELF.h:756
@ EF_AMDGPU_MACH_AMDGCN_GFX802
Definition: ELF.h:755
@ EF_AMDGPU_MACH_AMDGCN_GFX90C
Definition: ELF.h:764
@ EF_AMDGPU_MACH_AMDGCN_GFX900
Definition: ELF.h:758
@ EF_AMDGPU_MACH_AMDGCN_GFX909
Definition: ELF.h:763
@ EF_AMDGPU_MACH
Definition: ELF.h:708
@ EF_AMDGPU_MACH_AMDGCN_GFX906
Definition: ELF.h:761
@ EF_AMDGPU_MACH_AMDGCN_GFX9_GENERIC
Definition: ELF.h:795
@ EF_AMDGPU_MACH_AMDGCN_GFX1103
Definition: ELF.h:782
@ EF_AMDGPU_MACH_R600_CAICOS
Definition: ELF.h:732
@ EF_AMDGPU_MACH_AMDGCN_GFX90A
Definition: ELF.h:777
@ EF_AMDGPU_MACH_AMDGCN_GFX1034
Definition: ELF.h:776
@ EF_AMDGPU_MACH_AMDGCN_GFX1013
Definition: ELF.h:780
@ EF_AMDGPU_MACH_AMDGCN_GFX12_GENERIC
Definition: ELF.h:803
@ EF_AMDGPU_MACH_AMDGCN_GFX10_1_GENERIC
Definition: ELF.h:796
@ EF_AMDGPU_MACH_AMDGCN_GFX904
Definition: ELF.h:760
@ EF_AMDGPU_MACH_R600_RS880
Definition: ELF.h:718
@ EF_AMDGPU_MACH_AMDGCN_GFX805
Definition: ELF.h:774
@ EF_AMDGPU_MACH_AMDGCN_GFX1201
Definition: ELF.h:792
@ EF_AMDGPU_MACH_AMDGCN_GFX1150
Definition: ELF.h:781
@ EF_AMDGPU_MACH_R600_SUMO
Definition: ELF.h:729
@ EF_AMDGPU_MACH_R600_BARTS
Definition: ELF.h:731
@ EF_AMDGPU_MACH_AMDGCN_GFX702
Definition: ELF.h:750
@ ELFDATA2MSB
Definition: ELF.h:337
@ ELFDATA2LSB
Definition: ELF.h:336
@ EF_LOONGARCH_ABI_SINGLE_FLOAT
Definition: ELF.h:991
@ EF_LOONGARCH_ABI_DOUBLE_FLOAT
Definition: ELF.h:992
@ EF_LOONGARCH_ABI_SOFT_FLOAT
Definition: ELF.h:990
@ EF_LOONGARCH_ABI_MODIFIER_MASK
Definition: ELF.h:993
@ EF_RISCV_RVC
Definition: ELF.h:674
@ STT_FUNC
Definition: ELF.h:1332
@ STT_NOTYPE
Definition: ELF.h:1330
@ STT_SECTION
Definition: ELF.h:1333
@ STT_FILE
Definition: ELF.h:1334
@ STT_COMMON
Definition: ELF.h:1335
@ STT_GNU_IFUNC
Definition: ELF.h:1337
@ STT_OBJECT
Definition: ELF.h:1331
@ STT_TLS
Definition: ELF.h:1336
@ EF_CUDA_SM21
Definition: ELF.h:877
@ EF_CUDA_SM90
Definition: ELF.h:896
@ EF_CUDA_SM86
Definition: ELF.h:892
@ EF_CUDA_SM60
Definition: ELF.h:885
@ EF_CUDA_SM
Definition: ELF.h:873
@ EF_CUDA_SM89
Definition: ELF.h:894
@ EF_CUDA_SM37
Definition: ELF.h:881
@ EF_CUDA_SM32
Definition: ELF.h:879
@ EF_CUDA_SM72
Definition: ELF.h:889
@ EF_CUDA_SM50
Definition: ELF.h:882
@ EF_CUDA_ACCELERATORS
Definition: ELF.h:905
@ EF_CUDA_SM61
Definition: ELF.h:886
@ EF_CUDA_SM52
Definition: ELF.h:883
@ EF_CUDA_SM35
Definition: ELF.h:880
@ EF_CUDA_SM62
Definition: ELF.h:887
@ EF_CUDA_SM30
Definition: ELF.h:878
@ EF_CUDA_SM75
Definition: ELF.h:890
@ EF_CUDA_SM87
Definition: ELF.h:893
@ EF_CUDA_SM20
Definition: ELF.h:876
@ EF_CUDA_SM80
Definition: ELF.h:891
@ EF_CUDA_SM53
Definition: ELF.h:884
@ EF_CUDA_SM70
Definition: ELF.h:888
@ EF_MIPS_ARCH
Definition: ELF.h:569
@ EF_MIPS_MICROMIPS
Definition: ELF.h:552
@ EF_MIPS_ARCH_32R6
Definition: ELF.h:567
@ EF_MIPS_MACH_NONE
Definition: ELF.h:530
@ EF_MIPS_ARCH_64
Definition: ELF.h:564
@ EF_MIPS_ARCH_32
Definition: ELF.h:563
@ EF_MIPS_MACH_OCTEON
Definition: ELF.h:538
@ EF_MIPS_ARCH_4
Definition: ELF.h:561
@ EF_MIPS_ARCH_5
Definition: ELF.h:562
@ EF_MIPS_ARCH_2
Definition: ELF.h:559
@ EF_MIPS_ARCH_32R2
Definition: ELF.h:565
@ EF_MIPS_ARCH_64R2
Definition: ELF.h:566
@ EF_MIPS_ARCH_ASE_M16
Definition: ELF.h:553
@ EF_MIPS_MACH
Definition: ELF.h:549
@ EF_MIPS_ARCH_1
Definition: ELF.h:558
@ EF_MIPS_ARCH_64R6
Definition: ELF.h:568
@ EF_MIPS_ARCH_3
Definition: ELF.h:560
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:46
static std::string describe(const ELFFile< ELFT > &Obj, const typename ELFT::Shdr &Sec)
Definition: ELF.h:144
std::pair< unsigned char, unsigned char > getElfArchType(StringRef Object)
Definition: ELF.h:79
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:2067
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:756
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
static const Target * lookupTarget(StringRef Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.