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) {
382 auto ParseResult = RISCVISAInfo::parseNormalizedArchString(*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";
589
590 // AMDGCN GFX12.
592 return "gfx1200";
594 return "gfx1201";
595
596 // Generic AMDGCN targets
598 return "gfx9-generic";
600 return "gfx10-1-generic";
602 return "gfx10-3-generic";
604 return "gfx11-generic";
605 default:
606 llvm_unreachable("Unknown EF_AMDGPU_MACH value");
607 }
608}
609
610StringRef ELFObjectFileBase::getNVPTXCPUName() const {
612 unsigned SM = getPlatformFlags() & ELF::EF_CUDA_SM;
613
614 switch (SM) {
615 // Fermi architecture.
617 return "sm_20";
619 return "sm_21";
620
621 // Kepler architecture.
623 return "sm_30";
625 return "sm_32";
627 return "sm_35";
629 return "sm_37";
630
631 // Maxwell architecture.
633 return "sm_50";
635 return "sm_52";
637 return "sm_53";
638
639 // Pascal architecture.
641 return "sm_60";
643 return "sm_61";
645 return "sm_62";
646
647 // Volta architecture.
649 return "sm_70";
651 return "sm_72";
652
653 // Turing architecture.
655 return "sm_75";
656
657 // Ampere architecture.
659 return "sm_80";
661 return "sm_86";
663 return "sm_87";
664
665 // Ada architecture.
667 return "sm_89";
668
669 // Hopper architecture.
671 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_90a" : "sm_90";
672 default:
673 llvm_unreachable("Unknown EF_CUDA_SM value");
674 }
675}
676
677// FIXME Encode from a tablegen description or target parser.
679 if (TheTriple.getSubArch() != Triple::NoSubArch)
680 return;
681
684 // TODO Propagate Error.
685 consumeError(std::move(E));
686 return;
687 }
688
689 std::string Triple;
690 // Default to ARM, but use the triple if it's been set.
691 if (TheTriple.isThumb())
692 Triple = "thumb";
693 else
694 Triple = "arm";
695
696 std::optional<unsigned> Attr =
697 Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
698 if (Attr) {
699 switch (*Attr) {
701 Triple += "v4";
702 break;
704 Triple += "v4t";
705 break;
707 Triple += "v5t";
708 break;
710 Triple += "v5te";
711 break;
713 Triple += "v5tej";
714 break;
716 Triple += "v6";
717 break;
719 Triple += "v6kz";
720 break;
722 Triple += "v6t2";
723 break;
725 Triple += "v6k";
726 break;
727 case ARMBuildAttrs::v7: {
728 std::optional<unsigned> ArchProfileAttr =
730 if (ArchProfileAttr &&
731 *ArchProfileAttr == ARMBuildAttrs::MicroControllerProfile)
732 Triple += "v7m";
733 else
734 Triple += "v7";
735 break;
736 }
738 Triple += "v6m";
739 break;
741 Triple += "v6sm";
742 break;
744 Triple += "v7em";
745 break;
747 Triple += "v8a";
748 break;
750 Triple += "v8r";
751 break;
753 Triple += "v8m.base";
754 break;
756 Triple += "v8m.main";
757 break;
759 Triple += "v8.1m.main";
760 break;
762 Triple += "v9a";
763 break;
764 }
765 }
766 if (!isLittleEndian())
767 Triple += "eb";
768
769 TheTriple.setArchName(Triple);
770}
771
772std::vector<ELFPltEntry> ELFObjectFileBase::getPltEntries() const {
773 std::string Err;
774 const auto Triple = makeTriple();
775 const auto *T = TargetRegistry::lookupTarget(Triple.str(), Err);
776 if (!T)
777 return {};
778 uint32_t JumpSlotReloc = 0, GlobDatReloc = 0;
779 switch (Triple.getArch()) {
780 case Triple::x86:
781 JumpSlotReloc = ELF::R_386_JUMP_SLOT;
782 GlobDatReloc = ELF::R_386_GLOB_DAT;
783 break;
784 case Triple::x86_64:
785 JumpSlotReloc = ELF::R_X86_64_JUMP_SLOT;
786 GlobDatReloc = ELF::R_X86_64_GLOB_DAT;
787 break;
788 case Triple::aarch64:
790 JumpSlotReloc = ELF::R_AARCH64_JUMP_SLOT;
791 break;
792 default:
793 return {};
794 }
795 std::unique_ptr<const MCInstrInfo> MII(T->createMCInstrInfo());
796 std::unique_ptr<const MCInstrAnalysis> MIA(
797 T->createMCInstrAnalysis(MII.get()));
798 if (!MIA)
799 return {};
800 std::vector<std::pair<uint64_t, uint64_t>> PltEntries;
801 std::optional<SectionRef> RelaPlt, RelaDyn;
802 uint64_t GotBaseVA = 0;
803 for (const SectionRef &Section : sections()) {
804 Expected<StringRef> NameOrErr = Section.getName();
805 if (!NameOrErr) {
806 consumeError(NameOrErr.takeError());
807 continue;
808 }
809 StringRef Name = *NameOrErr;
810
811 if (Name == ".rela.plt" || Name == ".rel.plt") {
812 RelaPlt = Section;
813 } else if (Name == ".rela.dyn" || Name == ".rel.dyn") {
814 RelaDyn = Section;
815 } else if (Name == ".got.plt") {
816 GotBaseVA = Section.getAddress();
817 } else if (Name == ".plt" || Name == ".plt.got") {
818 Expected<StringRef> PltContents = Section.getContents();
819 if (!PltContents) {
820 consumeError(PltContents.takeError());
821 return {};
822 }
824 PltEntries,
825 MIA->findPltEntries(Section.getAddress(),
826 arrayRefFromStringRef(*PltContents), Triple));
827 }
828 }
829
830 // Build a map from GOT entry virtual address to PLT entry virtual address.
832 for (auto [Plt, GotPlt] : PltEntries) {
833 uint64_t GotPltEntry = GotPlt;
834 // An x86-32 PIC PLT uses jmp DWORD PTR [ebx-offset]. Add
835 // _GLOBAL_OFFSET_TABLE_ (EBX) to get the .got.plt (or .got) entry address.
836 // See X86MCTargetDesc.cpp:findPltEntries for the 1 << 32 bit.
837 if (GotPltEntry & (uint64_t(1) << 32) && getEMachine() == ELF::EM_386)
838 GotPltEntry = static_cast<int32_t>(GotPltEntry) + GotBaseVA;
839 GotToPlt.insert(std::make_pair(GotPltEntry, Plt));
840 }
841
842 // Find the relocations in the dynamic relocation table that point to
843 // locations in the GOT for which we know the corresponding PLT entry.
844 std::vector<ELFPltEntry> Result;
845 auto handleRels = [&](iterator_range<relocation_iterator> Rels,
846 uint32_t RelType, StringRef PltSec) {
847 for (const auto &R : Rels) {
848 if (R.getType() != RelType)
849 continue;
850 auto PltEntryIter = GotToPlt.find(R.getOffset());
851 if (PltEntryIter != GotToPlt.end()) {
852 symbol_iterator Sym = R.getSymbol();
853 if (Sym == symbol_end())
854 Result.push_back(
855 ELFPltEntry{PltSec, std::nullopt, PltEntryIter->second});
856 else
857 Result.push_back(ELFPltEntry{PltSec, Sym->getRawDataRefImpl(),
858 PltEntryIter->second});
859 }
860 }
861 };
862
863 if (RelaPlt)
864 handleRels(RelaPlt->relocations(), JumpSlotReloc, ".plt");
865
866 // If a symbol needing a PLT entry also needs a GLOB_DAT relocation, GNU ld's
867 // x86 port places the PLT entry in the .plt.got section.
868 if (RelaDyn)
869 handleRels(RelaDyn->relocations(), GlobDatReloc, ".plt.got");
870
871 return Result;
872}
873
874template <class ELFT>
876 const ELFFile<ELFT> &EF, std::optional<unsigned> TextSectionIndex,
877 std::vector<PGOAnalysisMap> *PGOAnalyses) {
878 using Elf_Shdr = typename ELFT::Shdr;
879 bool IsRelocatable = EF.getHeader().e_type == ELF::ET_REL;
880 std::vector<BBAddrMap> BBAddrMaps;
881 if (PGOAnalyses)
882 PGOAnalyses->clear();
883
884 const auto &Sections = cantFail(EF.sections());
885 auto IsMatch = [&](const Elf_Shdr &Sec) -> Expected<bool> {
886 if (Sec.sh_type != ELF::SHT_LLVM_BB_ADDR_MAP &&
887 Sec.sh_type != ELF::SHT_LLVM_BB_ADDR_MAP_V0)
888 return false;
889 if (!TextSectionIndex)
890 return true;
891 Expected<const Elf_Shdr *> TextSecOrErr = EF.getSection(Sec.sh_link);
892 if (!TextSecOrErr)
893 return createError("unable to get the linked-to section for " +
894 describe(EF, Sec) + ": " +
895 toString(TextSecOrErr.takeError()));
896 assert(*TextSecOrErr >= Sections.begin() &&
897 "Text section pointer outside of bounds");
898 if (*TextSectionIndex !=
899 (unsigned)std::distance(Sections.begin(), *TextSecOrErr))
900 return false;
901 return true;
902 };
903
905 EF.getSectionAndRelocations(IsMatch);
906 if (!SectionRelocMapOrErr)
907 return SectionRelocMapOrErr.takeError();
908
909 for (auto const &[Sec, RelocSec] : *SectionRelocMapOrErr) {
910 if (IsRelocatable && !RelocSec)
911 return createError("unable to get relocation section for " +
912 describe(EF, *Sec));
913 Expected<std::vector<BBAddrMap>> BBAddrMapOrErr =
914 EF.decodeBBAddrMap(*Sec, RelocSec, PGOAnalyses);
915 if (!BBAddrMapOrErr) {
916 if (PGOAnalyses)
917 PGOAnalyses->clear();
918 return createError("unable to read " + describe(EF, *Sec) + ": " +
919 toString(BBAddrMapOrErr.takeError()));
920 }
921 std::move(BBAddrMapOrErr->begin(), BBAddrMapOrErr->end(),
922 std::back_inserter(BBAddrMaps));
923 }
924 if (PGOAnalyses)
925 assert(PGOAnalyses->size() == BBAddrMaps.size() &&
926 "The same number of BBAddrMaps and PGOAnalysisMaps should be "
927 "returned when PGO information is requested");
928 return BBAddrMaps;
929}
930
931template <class ELFT>
935 using Elf_Shdr = typename ELFT::Shdr;
936 const Elf_Shdr *VerSec = nullptr;
937 const Elf_Shdr *VerNeedSec = nullptr;
938 const Elf_Shdr *VerDefSec = nullptr;
939 // The user should ensure sections() can't fail here.
940 for (const Elf_Shdr &Sec : cantFail(EF.sections())) {
941 if (Sec.sh_type == ELF::SHT_GNU_versym)
942 VerSec = &Sec;
943 else if (Sec.sh_type == ELF::SHT_GNU_verdef)
944 VerDefSec = &Sec;
945 else if (Sec.sh_type == ELF::SHT_GNU_verneed)
946 VerNeedSec = &Sec;
947 }
948 if (!VerSec)
949 return std::vector<VersionEntry>();
950
952 EF.loadVersionMap(VerNeedSec, VerDefSec);
953 if (!MapOrErr)
954 return MapOrErr.takeError();
955
956 std::vector<VersionEntry> Ret;
957 size_t I = 0;
958 for (const ELFSymbolRef &Sym : Symbols) {
959 ++I;
961 EF.template getEntry<typename ELFT::Versym>(*VerSec, I);
962 if (!VerEntryOrErr)
963 return createError("unable to read an entry with index " + Twine(I) +
964 " from " + describe(EF, *VerSec) + ": " +
965 toString(VerEntryOrErr.takeError()));
966
967 Expected<uint32_t> FlagsOrErr = Sym.getFlags();
968 if (!FlagsOrErr)
969 return createError("unable to read flags for symbol with index " +
970 Twine(I) + ": " + toString(FlagsOrErr.takeError()));
971
972 bool IsDefault;
974 (*VerEntryOrErr)->vs_index, IsDefault, *MapOrErr,
975 (*FlagsOrErr) & SymbolRef::SF_Undefined);
976 if (!VerOrErr)
977 return createError("unable to get a version for entry " + Twine(I) +
978 " of " + describe(EF, *VerSec) + ": " +
979 toString(VerOrErr.takeError()));
980
981 Ret.push_back({(*VerOrErr).str(), IsDefault});
982 }
983
984 return Ret;
985}
986
990 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
991 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
992 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
993 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
994 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
995 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
996 return readDynsymVersionsImpl(cast<ELF64BEObjectFile>(this)->getELFFile(),
997 Symbols);
998}
999
1001 std::optional<unsigned> TextSectionIndex,
1002 std::vector<PGOAnalysisMap> *PGOAnalyses) const {
1003 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
1004 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
1005 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
1006 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
1007 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
1008 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
1009 return readBBAddrMapImpl(cast<ELF64BEObjectFile>(this)->getELFFile(),
1010 TextSectionIndex, PGOAnalyses);
1011}
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: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:1447
bool isThumb() const
Tests whether the target is Thumb (little and big endian).
Definition: Triple.h:847
SubArchType getSubArch() const
get the parsed subarchitecture type for this triple.
Definition: Triple.h:375
@ aarch64_be
Definition: Triple.h:52
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:372
const std::string & str() const
Definition: Triple.h:435
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:875
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:888
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_HEXAGON
Definition: ELF.h:257
@ 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:1324
@ STT_NOTYPE
Definition: ELF.h:1322
@ STT_SECTION
Definition: ELF.h:1325
@ STT_FILE
Definition: ELF.h:1326
@ STT_COMMON
Definition: ELF.h:1327
@ STT_GNU_IFUNC
Definition: ELF.h:1329
@ STT_OBJECT
Definition: ELF.h:1323
@ STT_TLS
Definition: ELF.h:1328
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:2073
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.