LLVM 20.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 case ELF::EM_BPF:
445 return StringRef("v4");
446 default:
447 return std::nullopt;
448 }
449}
450
451StringRef ELFObjectFileBase::getAMDGPUCPUName() const {
453 unsigned CPU = getPlatformFlags() & ELF::EF_AMDGPU_MACH;
454
455 switch (CPU) {
456 // Radeon HD 2000/3000 Series (R600).
458 return "r600";
460 return "r630";
462 return "rs880";
464 return "rv670";
465
466 // Radeon HD 4000 Series (R700).
468 return "rv710";
470 return "rv730";
472 return "rv770";
473
474 // Radeon HD 5000 Series (Evergreen).
476 return "cedar";
478 return "cypress";
480 return "juniper";
482 return "redwood";
484 return "sumo";
485
486 // Radeon HD 6000 Series (Northern Islands).
488 return "barts";
490 return "caicos";
492 return "cayman";
494 return "turks";
495
496 // AMDGCN GFX6.
498 return "gfx600";
500 return "gfx601";
502 return "gfx602";
503
504 // AMDGCN GFX7.
506 return "gfx700";
508 return "gfx701";
510 return "gfx702";
512 return "gfx703";
514 return "gfx704";
516 return "gfx705";
517
518 // AMDGCN GFX8.
520 return "gfx801";
522 return "gfx802";
524 return "gfx803";
526 return "gfx805";
528 return "gfx810";
529
530 // AMDGCN GFX9.
532 return "gfx900";
534 return "gfx902";
536 return "gfx904";
538 return "gfx906";
540 return "gfx908";
542 return "gfx909";
544 return "gfx90a";
546 return "gfx90c";
548 return "gfx940";
550 return "gfx941";
552 return "gfx942";
553
554 // AMDGCN GFX10.
556 return "gfx1010";
558 return "gfx1011";
560 return "gfx1012";
562 return "gfx1013";
564 return "gfx1030";
566 return "gfx1031";
568 return "gfx1032";
570 return "gfx1033";
572 return "gfx1034";
574 return "gfx1035";
576 return "gfx1036";
577
578 // AMDGCN GFX11.
580 return "gfx1100";
582 return "gfx1101";
584 return "gfx1102";
586 return "gfx1103";
588 return "gfx1150";
590 return "gfx1151";
592 return "gfx1152";
593
594 // AMDGCN GFX12.
596 return "gfx1200";
598 return "gfx1201";
599
600 // Generic AMDGCN targets
602 return "gfx9-generic";
604 return "gfx10-1-generic";
606 return "gfx10-3-generic";
608 return "gfx11-generic";
610 return "gfx12-generic";
611 default:
612 llvm_unreachable("Unknown EF_AMDGPU_MACH value");
613 }
614}
615
616StringRef ELFObjectFileBase::getNVPTXCPUName() const {
618 unsigned SM = getPlatformFlags() & ELF::EF_CUDA_SM;
619
620 switch (SM) {
621 // Fermi architecture.
623 return "sm_20";
625 return "sm_21";
626
627 // Kepler architecture.
629 return "sm_30";
631 return "sm_32";
633 return "sm_35";
635 return "sm_37";
636
637 // Maxwell architecture.
639 return "sm_50";
641 return "sm_52";
643 return "sm_53";
644
645 // Pascal architecture.
647 return "sm_60";
649 return "sm_61";
651 return "sm_62";
652
653 // Volta architecture.
655 return "sm_70";
657 return "sm_72";
658
659 // Turing architecture.
661 return "sm_75";
662
663 // Ampere architecture.
665 return "sm_80";
667 return "sm_86";
669 return "sm_87";
670
671 // Ada architecture.
673 return "sm_89";
674
675 // Hopper architecture.
677 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_90a" : "sm_90";
678 default:
679 llvm_unreachable("Unknown EF_CUDA_SM value");
680 }
681}
682
683// FIXME Encode from a tablegen description or target parser.
685 if (TheTriple.getSubArch() != Triple::NoSubArch)
686 return;
687
690 // TODO Propagate Error.
691 consumeError(std::move(E));
692 return;
693 }
694
695 std::string Triple;
696 // Default to ARM, but use the triple if it's been set.
697 if (TheTriple.isThumb())
698 Triple = "thumb";
699 else
700 Triple = "arm";
701
702 std::optional<unsigned> Attr =
703 Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
704 if (Attr) {
705 switch (*Attr) {
707 Triple += "v4";
708 break;
710 Triple += "v4t";
711 break;
713 Triple += "v5t";
714 break;
716 Triple += "v5te";
717 break;
719 Triple += "v5tej";
720 break;
722 Triple += "v6";
723 break;
725 Triple += "v6kz";
726 break;
728 Triple += "v6t2";
729 break;
731 Triple += "v6k";
732 break;
733 case ARMBuildAttrs::v7: {
734 std::optional<unsigned> ArchProfileAttr =
736 if (ArchProfileAttr &&
737 *ArchProfileAttr == ARMBuildAttrs::MicroControllerProfile)
738 Triple += "v7m";
739 else
740 Triple += "v7";
741 break;
742 }
744 Triple += "v6m";
745 break;
747 Triple += "v6sm";
748 break;
750 Triple += "v7em";
751 break;
753 Triple += "v8a";
754 break;
756 Triple += "v8r";
757 break;
759 Triple += "v8m.base";
760 break;
762 Triple += "v8m.main";
763 break;
765 Triple += "v8.1m.main";
766 break;
768 Triple += "v9a";
769 break;
770 }
771 }
772 if (!isLittleEndian())
773 Triple += "eb";
774
775 TheTriple.setArchName(Triple);
776}
777
778std::vector<ELFPltEntry> ELFObjectFileBase::getPltEntries() const {
779 std::string Err;
780 const auto Triple = makeTriple();
781 const auto *T = TargetRegistry::lookupTarget(Triple.str(), Err);
782 if (!T)
783 return {};
784 uint32_t JumpSlotReloc = 0, GlobDatReloc = 0;
785 switch (Triple.getArch()) {
786 case Triple::x86:
787 JumpSlotReloc = ELF::R_386_JUMP_SLOT;
788 GlobDatReloc = ELF::R_386_GLOB_DAT;
789 break;
790 case Triple::x86_64:
791 JumpSlotReloc = ELF::R_X86_64_JUMP_SLOT;
792 GlobDatReloc = ELF::R_X86_64_GLOB_DAT;
793 break;
794 case Triple::aarch64:
796 JumpSlotReloc = ELF::R_AARCH64_JUMP_SLOT;
797 break;
798 default:
799 return {};
800 }
801 std::unique_ptr<const MCInstrInfo> MII(T->createMCInstrInfo());
802 std::unique_ptr<const MCInstrAnalysis> MIA(
803 T->createMCInstrAnalysis(MII.get()));
804 if (!MIA)
805 return {};
806 std::vector<std::pair<uint64_t, uint64_t>> PltEntries;
807 std::optional<SectionRef> RelaPlt, RelaDyn;
808 uint64_t GotBaseVA = 0;
809 for (const SectionRef &Section : sections()) {
810 Expected<StringRef> NameOrErr = Section.getName();
811 if (!NameOrErr) {
812 consumeError(NameOrErr.takeError());
813 continue;
814 }
815 StringRef Name = *NameOrErr;
816
817 if (Name == ".rela.plt" || Name == ".rel.plt") {
818 RelaPlt = Section;
819 } else if (Name == ".rela.dyn" || Name == ".rel.dyn") {
820 RelaDyn = Section;
821 } else if (Name == ".got.plt") {
822 GotBaseVA = Section.getAddress();
823 } else if (Name == ".plt" || Name == ".plt.got") {
824 Expected<StringRef> PltContents = Section.getContents();
825 if (!PltContents) {
826 consumeError(PltContents.takeError());
827 return {};
828 }
830 PltEntries,
831 MIA->findPltEntries(Section.getAddress(),
832 arrayRefFromStringRef(*PltContents), Triple));
833 }
834 }
835
836 // Build a map from GOT entry virtual address to PLT entry virtual address.
838 for (auto [Plt, GotPlt] : PltEntries) {
839 uint64_t GotPltEntry = GotPlt;
840 // An x86-32 PIC PLT uses jmp DWORD PTR [ebx-offset]. Add
841 // _GLOBAL_OFFSET_TABLE_ (EBX) to get the .got.plt (or .got) entry address.
842 // See X86MCTargetDesc.cpp:findPltEntries for the 1 << 32 bit.
843 if (GotPltEntry & (uint64_t(1) << 32) && getEMachine() == ELF::EM_386)
844 GotPltEntry = static_cast<int32_t>(GotPltEntry) + GotBaseVA;
845 GotToPlt.insert(std::make_pair(GotPltEntry, Plt));
846 }
847
848 // Find the relocations in the dynamic relocation table that point to
849 // locations in the GOT for which we know the corresponding PLT entry.
850 std::vector<ELFPltEntry> Result;
851 auto handleRels = [&](iterator_range<relocation_iterator> Rels,
852 uint32_t RelType, StringRef PltSec) {
853 for (const auto &R : Rels) {
854 if (R.getType() != RelType)
855 continue;
856 auto PltEntryIter = GotToPlt.find(R.getOffset());
857 if (PltEntryIter != GotToPlt.end()) {
858 symbol_iterator Sym = R.getSymbol();
859 if (Sym == symbol_end())
860 Result.push_back(
861 ELFPltEntry{PltSec, std::nullopt, PltEntryIter->second});
862 else
863 Result.push_back(ELFPltEntry{PltSec, Sym->getRawDataRefImpl(),
864 PltEntryIter->second});
865 }
866 }
867 };
868
869 if (RelaPlt)
870 handleRels(RelaPlt->relocations(), JumpSlotReloc, ".plt");
871
872 // If a symbol needing a PLT entry also needs a GLOB_DAT relocation, GNU ld's
873 // x86 port places the PLT entry in the .plt.got section.
874 if (RelaDyn)
875 handleRels(RelaDyn->relocations(), GlobDatReloc, ".plt.got");
876
877 return Result;
878}
879
880template <class ELFT>
882 const ELFFile<ELFT> &EF, std::optional<unsigned> TextSectionIndex,
883 std::vector<PGOAnalysisMap> *PGOAnalyses) {
884 using Elf_Shdr = typename ELFT::Shdr;
885 bool IsRelocatable = EF.getHeader().e_type == ELF::ET_REL;
886 std::vector<BBAddrMap> BBAddrMaps;
887 if (PGOAnalyses)
888 PGOAnalyses->clear();
889
890 const auto &Sections = cantFail(EF.sections());
891 auto IsMatch = [&](const Elf_Shdr &Sec) -> Expected<bool> {
892 if (Sec.sh_type != ELF::SHT_LLVM_BB_ADDR_MAP &&
893 Sec.sh_type != ELF::SHT_LLVM_BB_ADDR_MAP_V0)
894 return false;
895 if (!TextSectionIndex)
896 return true;
897 Expected<const Elf_Shdr *> TextSecOrErr = EF.getSection(Sec.sh_link);
898 if (!TextSecOrErr)
899 return createError("unable to get the linked-to section for " +
900 describe(EF, Sec) + ": " +
901 toString(TextSecOrErr.takeError()));
902 assert(*TextSecOrErr >= Sections.begin() &&
903 "Text section pointer outside of bounds");
904 if (*TextSectionIndex !=
905 (unsigned)std::distance(Sections.begin(), *TextSecOrErr))
906 return false;
907 return true;
908 };
909
911 EF.getSectionAndRelocations(IsMatch);
912 if (!SectionRelocMapOrErr)
913 return SectionRelocMapOrErr.takeError();
914
915 for (auto const &[Sec, RelocSec] : *SectionRelocMapOrErr) {
916 if (IsRelocatable && !RelocSec)
917 return createError("unable to get relocation section for " +
918 describe(EF, *Sec));
919 Expected<std::vector<BBAddrMap>> BBAddrMapOrErr =
920 EF.decodeBBAddrMap(*Sec, RelocSec, PGOAnalyses);
921 if (!BBAddrMapOrErr) {
922 if (PGOAnalyses)
923 PGOAnalyses->clear();
924 return createError("unable to read " + describe(EF, *Sec) + ": " +
925 toString(BBAddrMapOrErr.takeError()));
926 }
927 std::move(BBAddrMapOrErr->begin(), BBAddrMapOrErr->end(),
928 std::back_inserter(BBAddrMaps));
929 }
930 if (PGOAnalyses)
931 assert(PGOAnalyses->size() == BBAddrMaps.size() &&
932 "The same number of BBAddrMaps and PGOAnalysisMaps should be "
933 "returned when PGO information is requested");
934 return BBAddrMaps;
935}
936
937template <class ELFT>
941 using Elf_Shdr = typename ELFT::Shdr;
942 const Elf_Shdr *VerSec = nullptr;
943 const Elf_Shdr *VerNeedSec = nullptr;
944 const Elf_Shdr *VerDefSec = nullptr;
945 // The user should ensure sections() can't fail here.
946 for (const Elf_Shdr &Sec : cantFail(EF.sections())) {
947 if (Sec.sh_type == ELF::SHT_GNU_versym)
948 VerSec = &Sec;
949 else if (Sec.sh_type == ELF::SHT_GNU_verdef)
950 VerDefSec = &Sec;
951 else if (Sec.sh_type == ELF::SHT_GNU_verneed)
952 VerNeedSec = &Sec;
953 }
954 if (!VerSec)
955 return std::vector<VersionEntry>();
956
958 EF.loadVersionMap(VerNeedSec, VerDefSec);
959 if (!MapOrErr)
960 return MapOrErr.takeError();
961
962 std::vector<VersionEntry> Ret;
963 size_t I = 0;
964 for (const ELFSymbolRef &Sym : Symbols) {
965 ++I;
967 EF.template getEntry<typename ELFT::Versym>(*VerSec, I);
968 if (!VerEntryOrErr)
969 return createError("unable to read an entry with index " + Twine(I) +
970 " from " + describe(EF, *VerSec) + ": " +
971 toString(VerEntryOrErr.takeError()));
972
973 Expected<uint32_t> FlagsOrErr = Sym.getFlags();
974 if (!FlagsOrErr)
975 return createError("unable to read flags for symbol with index " +
976 Twine(I) + ": " + toString(FlagsOrErr.takeError()));
977
978 bool IsDefault;
980 (*VerEntryOrErr)->vs_index, IsDefault, *MapOrErr,
981 (*FlagsOrErr) & SymbolRef::SF_Undefined);
982 if (!VerOrErr)
983 return createError("unable to get a version for entry " + Twine(I) +
984 " of " + describe(EF, *VerSec) + ": " +
985 toString(VerOrErr.takeError()));
986
987 Ret.push_back({(*VerOrErr).str(), IsDefault});
988 }
989
990 return Ret;
991}
992
996 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
997 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
998 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
999 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
1000 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
1001 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
1002 return readDynsymVersionsImpl(cast<ELF64BEObjectFile>(this)->getELFFile(),
1003 Symbols);
1004}
1005
1007 std::optional<unsigned> TextSectionIndex,
1008 std::vector<PGOAnalysisMap> *PGOAnalyses) const {
1009 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
1010 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
1011 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
1012 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
1013 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
1014 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
1015 return readBBAddrMapImpl(cast<ELF64BEObjectFile>(this)->getELFFile(),
1016 TextSectionIndex, PGOAnalyses);
1017}
1018
1020 auto Data = Sec.getRawDataRefImpl();
1021 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
1022 return Obj->getCrelDecodeProblem(Data);
1023 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
1024 return Obj->getCrelDecodeProblem(Data);
1025 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
1026 return Obj->getCrelDecodeProblem(Data);
1027 return cast<ELF64BEObjectFile>(this)->getCrelDecodeProblem(Data);
1028}
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:211
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:1546
bool isThumb() const
Tests whether the target is Thumb (little and big endian).
Definition: Triple.h:852
SubArchType getSubArch() const
get the parsed subarchitecture type for this triple.
Definition: Triple.h:376
@ aarch64_be
Definition: Triple.h:52
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:373
const std::string & str() const
Definition: Triple.h:440
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.
@ ELFDATA2MSB
Definition: ELF.h:337
@ ELFDATA2LSB
Definition: ELF.h:336
@ 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
@ EF_CUDA_SM21
Definition: ELF.h:898
@ EF_CUDA_SM90
Definition: ELF.h:917
@ EF_CUDA_SM86
Definition: ELF.h:913
@ EF_CUDA_SM60
Definition: ELF.h:906
@ EF_CUDA_SM
Definition: ELF.h:894
@ EF_CUDA_SM89
Definition: ELF.h:915
@ EF_CUDA_SM37
Definition: ELF.h:902
@ EF_CUDA_SM32
Definition: ELF.h:900
@ EF_CUDA_SM72
Definition: ELF.h:910
@ EF_CUDA_SM50
Definition: ELF.h:903
@ EF_CUDA_ACCELERATORS
Definition: ELF.h:926
@ EF_CUDA_SM61
Definition: ELF.h:907
@ EF_CUDA_SM52
Definition: ELF.h:904
@ EF_CUDA_SM35
Definition: ELF.h:901
@ EF_CUDA_SM62
Definition: ELF.h:908
@ EF_CUDA_SM30
Definition: ELF.h:899
@ EF_CUDA_SM75
Definition: ELF.h:911
@ EF_CUDA_SM87
Definition: ELF.h:914
@ EF_CUDA_SM20
Definition: ELF.h:897
@ EF_CUDA_SM80
Definition: ELF.h:912
@ EF_CUDA_SM53
Definition: ELF.h:905
@ EF_CUDA_SM70
Definition: ELF.h:909
@ 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_BPF
Definition: ELF.h:320
@ 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
@ ET_REL
Definition: ELF.h:117
@ EF_AMDGPU_MACH_AMDGCN_GFX703
Definition: ELF.h:772
@ EF_AMDGPU_MACH_AMDGCN_GFX1035
Definition: ELF.h:796
@ EF_AMDGPU_MACH_AMDGCN_GFX1031
Definition: ELF.h:790
@ EF_AMDGPU_MACH_R600_CAYMAN
Definition: ELF.h:754
@ EF_AMDGPU_MACH_AMDGCN_GFX704
Definition: ELF.h:773
@ EF_AMDGPU_MACH_AMDGCN_GFX902
Definition: ELF.h:780
@ EF_AMDGPU_MACH_AMDGCN_GFX810
Definition: ELF.h:778
@ EF_AMDGPU_MACH_AMDGCN_GFX1036
Definition: ELF.h:804
@ EF_AMDGPU_MACH_AMDGCN_GFX1102
Definition: ELF.h:806
@ EF_AMDGPU_MACH_R600_RV730
Definition: ELF.h:743
@ EF_AMDGPU_MACH_R600_RV710
Definition: ELF.h:742
@ EF_AMDGPU_MACH_AMDGCN_GFX908
Definition: ELF.h:783
@ EF_AMDGPU_MACH_AMDGCN_GFX1011
Definition: ELF.h:787
@ EF_AMDGPU_MACH_R600_CYPRESS
Definition: ELF.h:747
@ EF_AMDGPU_MACH_AMDGCN_GFX1032
Definition: ELF.h:791
@ EF_AMDGPU_MACH_R600_R600
Definition: ELF.h:737
@ EF_AMDGPU_MACH_AMDGCN_GFX940
Definition: ELF.h:799
@ EF_AMDGPU_MACH_AMDGCN_GFX941
Definition: ELF.h:810
@ EF_AMDGPU_MACH_R600_TURKS
Definition: ELF.h:755
@ EF_AMDGPU_MACH_R600_JUNIPER
Definition: ELF.h:748
@ EF_AMDGPU_MACH_AMDGCN_GFX601
Definition: ELF.h:768
@ EF_AMDGPU_MACH_AMDGCN_GFX942
Definition: ELF.h:811
@ EF_AMDGPU_MACH_AMDGCN_GFX1152
Definition: ELF.h:820
@ EF_AMDGPU_MACH_R600_R630
Definition: ELF.h:738
@ EF_AMDGPU_MACH_R600_REDWOOD
Definition: ELF.h:749
@ EF_AMDGPU_MACH_R600_RV770
Definition: ELF.h:744
@ EF_AMDGPU_MACH_AMDGCN_GFX600
Definition: ELF.h:767
@ EF_AMDGPU_MACH_AMDGCN_GFX602
Definition: ELF.h:793
@ EF_AMDGPU_MACH_AMDGCN_GFX1101
Definition: ELF.h:805
@ EF_AMDGPU_MACH_AMDGCN_GFX1100
Definition: ELF.h:800
@ EF_AMDGPU_MACH_AMDGCN_GFX1033
Definition: ELF.h:792
@ EF_AMDGPU_MACH_AMDGCN_GFX801
Definition: ELF.h:775
@ EF_AMDGPU_MACH_AMDGCN_GFX705
Definition: ELF.h:794
@ EF_AMDGPU_MACH_AMDGCN_GFX1010
Definition: ELF.h:786
@ EF_AMDGPU_MACH_R600_RV670
Definition: ELF.h:740
@ EF_AMDGPU_MACH_AMDGCN_GFX701
Definition: ELF.h:770
@ EF_AMDGPU_MACH_AMDGCN_GFX10_3_GENERIC
Definition: ELF.h:818
@ EF_AMDGPU_MACH_AMDGCN_GFX1012
Definition: ELF.h:788
@ EF_AMDGPU_MACH_AMDGCN_GFX1151
Definition: ELF.h:809
@ EF_AMDGPU_MACH_AMDGCN_GFX1030
Definition: ELF.h:789
@ EF_AMDGPU_MACH_R600_CEDAR
Definition: ELF.h:746
@ EF_AMDGPU_MACH_AMDGCN_GFX1200
Definition: ELF.h:807
@ EF_AMDGPU_MACH_AMDGCN_GFX700
Definition: ELF.h:769
@ EF_AMDGPU_MACH_AMDGCN_GFX11_GENERIC
Definition: ELF.h:819
@ EF_AMDGPU_MACH_AMDGCN_GFX803
Definition: ELF.h:777
@ EF_AMDGPU_MACH_AMDGCN_GFX802
Definition: ELF.h:776
@ EF_AMDGPU_MACH_AMDGCN_GFX90C
Definition: ELF.h:785
@ EF_AMDGPU_MACH_AMDGCN_GFX900
Definition: ELF.h:779
@ EF_AMDGPU_MACH_AMDGCN_GFX909
Definition: ELF.h:784
@ EF_AMDGPU_MACH
Definition: ELF.h:729
@ EF_AMDGPU_MACH_AMDGCN_GFX906
Definition: ELF.h:782
@ EF_AMDGPU_MACH_AMDGCN_GFX9_GENERIC
Definition: ELF.h:816
@ EF_AMDGPU_MACH_AMDGCN_GFX1103
Definition: ELF.h:803
@ EF_AMDGPU_MACH_R600_CAICOS
Definition: ELF.h:753
@ EF_AMDGPU_MACH_AMDGCN_GFX90A
Definition: ELF.h:798
@ EF_AMDGPU_MACH_AMDGCN_GFX1034
Definition: ELF.h:797
@ EF_AMDGPU_MACH_AMDGCN_GFX1013
Definition: ELF.h:801
@ EF_AMDGPU_MACH_AMDGCN_GFX12_GENERIC
Definition: ELF.h:824
@ EF_AMDGPU_MACH_AMDGCN_GFX10_1_GENERIC
Definition: ELF.h:817
@ EF_AMDGPU_MACH_AMDGCN_GFX904
Definition: ELF.h:781
@ EF_AMDGPU_MACH_R600_RS880
Definition: ELF.h:739
@ EF_AMDGPU_MACH_AMDGCN_GFX805
Definition: ELF.h:795
@ EF_AMDGPU_MACH_AMDGCN_GFX1201
Definition: ELF.h:813
@ EF_AMDGPU_MACH_AMDGCN_GFX1150
Definition: ELF.h:802
@ EF_AMDGPU_MACH_R600_SUMO
Definition: ELF.h:750
@ EF_AMDGPU_MACH_R600_BARTS
Definition: ELF.h:752
@ EF_AMDGPU_MACH_AMDGCN_GFX702
Definition: ELF.h:771
@ SHT_LLVM_BB_ADDR_MAP_V0
Definition: ELF.h:1125
@ SHT_GNU_verneed
Definition: ELF.h:1138
@ SHT_GNU_verdef
Definition: ELF.h:1137
@ SHT_LLVM_BB_ADDR_MAP
Definition: ELF.h:1129
@ SHT_GNU_versym
Definition: ELF.h:1139
@ EF_RISCV_RVC
Definition: ELF.h:674
@ ELFCLASS64
Definition: ELF.h:330
@ ELFCLASS32
Definition: ELF.h:329
@ EF_LOONGARCH_ABI_SINGLE_FLOAT
Definition: ELF.h:1012
@ EF_LOONGARCH_ABI_DOUBLE_FLOAT
Definition: ELF.h:1013
@ EF_LOONGARCH_ABI_SOFT_FLOAT
Definition: ELF.h:1011
@ EF_LOONGARCH_ABI_MODIFIER_MASK
Definition: ELF.h:1014
@ STT_FUNC
Definition: ELF.h:1353
@ STT_NOTYPE
Definition: ELF.h:1351
@ STT_SECTION
Definition: ELF.h:1354
@ STT_FILE
Definition: ELF.h:1355
@ STT_COMMON
Definition: ELF.h:1356
@ STT_GNU_IFUNC
Definition: ELF.h:1358
@ STT_OBJECT
Definition: ELF.h:1352
@ STT_TLS
Definition: ELF.h:1357
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:2098
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.