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