LLVM 23.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
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
71Expected<std::unique_ptr<ObjectFile>>
73 std::pair<unsigned char, unsigned char> Ident =
74 getElfArchType(Obj.getBuffer());
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;
162 ARMAttributeParser 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 case 79:
315 return "v79";
316 case 81:
317 return "v81";
318 default:
319 return {};
320 }
321}
322
323SubtargetFeatures ELFObjectFileBase::getHexagonFeatures() const {
324 SubtargetFeatures Features;
325 HexagonAttributeParser Parser;
326 if (Error E = getBuildAttributes(Parser)) {
327 // Return no attributes if none can be read.
328 // This behavior is important for backwards compatibility.
329 consumeError(std::move(E));
330 return Features;
331 }
332 std::optional<unsigned> Attr;
333
334 if ((Attr = Parser.getAttributeValue(HexagonAttrs::ARCH))) {
335 if (std::optional<std::string> FeatureString =
337 Features.AddFeature(*FeatureString);
338 }
339
340 if ((Attr = Parser.getAttributeValue(HexagonAttrs::HVXARCH))) {
341 std::optional<std::string> FeatureString =
343 // There is no corresponding hvx arch for v5 and v55.
344 if (FeatureString && *Attr >= 60)
345 Features.AddFeature("hvx" + *FeatureString);
346 }
347
348 if ((Attr = Parser.getAttributeValue(HexagonAttrs::HVXIEEEFP)))
349 if (*Attr)
350 Features.AddFeature("hvx-ieee-fp");
351
352 if ((Attr = Parser.getAttributeValue(HexagonAttrs::HVXQFLOAT)))
353 if (*Attr)
354 Features.AddFeature("hvx-qfloat");
355
356 if ((Attr = Parser.getAttributeValue(HexagonAttrs::ZREG)))
357 if (*Attr)
358 Features.AddFeature("zreg");
359
360 if ((Attr = Parser.getAttributeValue(HexagonAttrs::AUDIO)))
361 if (*Attr)
362 Features.AddFeature("audio");
363
364 if ((Attr = Parser.getAttributeValue(HexagonAttrs::CABAC)))
365 if (*Attr)
366 Features.AddFeature("cabac");
367
368 return Features;
369}
370
371Expected<SubtargetFeatures> ELFObjectFileBase::getRISCVFeatures() const {
372 SubtargetFeatures Features;
373 unsigned PlatformFlags = getPlatformFlags();
374
375 if (PlatformFlags & ELF::EF_RISCV_RVC) {
376 Features.AddFeature("zca");
377 }
378
379 RISCVAttributeParser Attributes;
381 return std::move(E);
382 }
383
384 std::optional<StringRef> Attr =
385 Attributes.getAttributeString(RISCVAttrs::ARCH);
386 if (Attr) {
387 auto ParseResult = RISCVISAInfo::parseNormalizedArchString(*Attr);
388 if (!ParseResult)
389 return ParseResult.takeError();
390 auto &ISAInfo = *ParseResult;
391
392 if (ISAInfo->getXLen() == 32)
393 Features.AddFeature("64bit", false);
394 else if (ISAInfo->getXLen() == 64)
395 Features.AddFeature("64bit");
396 else
397 llvm_unreachable("XLEN should be 32 or 64.");
398
399 Features.addFeaturesVector(ISAInfo->toFeatures());
400 }
401
402 return Features;
403}
404
405SubtargetFeatures ELFObjectFileBase::getLoongArchFeatures() const {
406 SubtargetFeatures Features;
407
410 break;
412 Features.AddFeature("d");
413 // D implies F according to LoongArch ISA spec.
414 [[fallthrough]];
416 Features.AddFeature("f");
417 break;
418 }
419
420 return Features;
421}
422
424 switch (getEMachine()) {
425 case ELF::EM_MIPS:
426 return getMIPSFeatures();
427 case ELF::EM_ARM:
428 return getARMFeatures();
429 case ELF::EM_RISCV:
430 return getRISCVFeatures();
432 return getLoongArchFeatures();
433 case ELF::EM_HEXAGON:
434 return getHexagonFeatures();
435 default:
436 return SubtargetFeatures();
437 }
438}
439
440std::optional<StringRef> ELFObjectFileBase::tryGetCPUName() const {
441 switch (getEMachine()) {
442 case ELF::EM_AMDGPU:
443 return getAMDGPUCPUName();
444 case ELF::EM_CUDA:
445 return getNVPTXCPUName();
446 case ELF::EM_PPC:
447 case ELF::EM_PPC64:
448 return StringRef("future");
449 case ELF::EM_BPF:
450 return StringRef("v4");
451 default:
452 return std::nullopt;
453 }
454}
455
456StringRef ELFObjectFileBase::getAMDGPUCPUName() const {
458 unsigned CPU = getPlatformFlags() & ELF::EF_AMDGPU_MACH;
459
460 switch (CPU) {
461#define X(NUM, ENUM, NAME) \
462 case ELF::ENUM: \
463 return NAME;
465#undef X
466
467 default:
468 llvm_unreachable("Unknown EF_AMDGPU_MACH value");
469 }
470}
471
472StringRef ELFObjectFileBase::getNVPTXCPUName() const {
478
479 switch (SM) {
480 // Fermi architecture.
482 return "sm_20";
484 return "sm_21";
485
486 // Kepler architecture.
488 return "sm_30";
490 return "sm_32";
492 return "sm_35";
494 return "sm_37";
495
496 // Maxwell architecture.
498 return "sm_50";
500 return "sm_52";
502 return "sm_53";
503
504 // Pascal architecture.
506 return "sm_60";
508 return "sm_61";
510 return "sm_62";
511
512 // Volta architecture.
514 return "sm_70";
516 return "sm_72";
517
518 // Turing architecture.
520 return "sm_75";
521
522 // Ampere architecture.
524 return "sm_80";
526 return "sm_86";
528 return "sm_87";
530 return "sm_88";
531
532 // Ada architecture.
534 return "sm_89";
535
536 // Hopper architecture.
539 : "sm_90";
540
541 // Blackwell architecture.
543 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_100a"
544 : "sm_100";
546 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_101a"
547 : "sm_101";
549 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_103a"
550 : "sm_103";
552 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_110a"
553 : "sm_110";
554
555 // Rubin architecture.
557 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_120a"
558 : "sm_120";
560 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_121a"
561 : "sm_121";
562 default:
563 llvm_unreachable("Unknown EF_CUDA_SM value");
564 }
565}
566
567// FIXME Encode from a tablegen description or target parser.
569 if (TheTriple.getSubArch() != Triple::NoSubArch)
570 return;
571
572 ARMAttributeParser Attributes;
573 if (Error E = getBuildAttributes(Attributes)) {
574 // TODO Propagate Error.
575 consumeError(std::move(E));
576 return;
577 }
578
579 std::string Triple;
580 // Default to ARM, but use the triple if it's been set.
581 if (TheTriple.isThumb())
582 Triple = "thumb";
583 else
584 Triple = "arm";
585
586 std::optional<unsigned> Attr =
587 Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
588 if (Attr) {
589 switch (*Attr) {
591 Triple += "v4";
592 break;
594 Triple += "v4t";
595 break;
597 Triple += "v5t";
598 break;
600 Triple += "v5te";
601 break;
603 Triple += "v5tej";
604 break;
606 Triple += "v6";
607 break;
609 Triple += "v6kz";
610 break;
612 Triple += "v6t2";
613 break;
615 Triple += "v6k";
616 break;
617 case ARMBuildAttrs::v7: {
618 std::optional<unsigned> ArchProfileAttr =
619 Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile);
620 if (ArchProfileAttr == ARMBuildAttrs::MicroControllerProfile)
621 Triple += "v7m";
622 else
623 Triple += "v7";
624 break;
625 }
627 Triple += "v6m";
628 break;
630 Triple += "v6sm";
631 break;
633 Triple += "v7em";
634 break;
636 Triple += "v8a";
637 break;
639 Triple += "v8r";
640 break;
642 Triple += "v8m.base";
643 break;
645 Triple += "v8m.main";
646 break;
648 Triple += "v8.1m.main";
649 break;
651 Triple += "v9a";
652 break;
653 }
654 }
655 if (!isLittleEndian())
656 Triple += "eb";
657
658 TheTriple.setArchName(Triple);
659}
660
661std::vector<ELFPltEntry>
663 std::string Err;
664 const auto Triple = makeTriple();
665 const auto *T = TargetRegistry::lookupTarget(Triple, Err);
666 if (!T)
667 return {};
668 uint32_t JumpSlotReloc = 0, GlobDatReloc = 0;
669 switch (Triple.getArch()) {
670 case Triple::x86:
671 JumpSlotReloc = ELF::R_386_JUMP_SLOT;
672 GlobDatReloc = ELF::R_386_GLOB_DAT;
673 break;
674 case Triple::x86_64:
675 JumpSlotReloc = ELF::R_X86_64_JUMP_SLOT;
676 GlobDatReloc = ELF::R_X86_64_GLOB_DAT;
677 break;
678 case Triple::aarch64:
680 JumpSlotReloc = ELF::R_AARCH64_JUMP_SLOT;
681 break;
682 case Triple::arm:
683 case Triple::armeb:
684 case Triple::thumb:
685 case Triple::thumbeb:
686 JumpSlotReloc = ELF::R_ARM_JUMP_SLOT;
687 break;
688 case Triple::hexagon:
689 JumpSlotReloc = ELF::R_HEX_JMP_SLOT;
690 GlobDatReloc = ELF::R_HEX_GLOB_DAT;
691 break;
692 case Triple::riscv32:
693 case Triple::riscv64:
694 JumpSlotReloc = ELF::R_RISCV_JUMP_SLOT;
695 break;
696 default:
697 return {};
698 }
699 std::unique_ptr<const MCInstrInfo> MII(T->createMCInstrInfo());
700 std::unique_ptr<const MCInstrAnalysis> MIA(
701 T->createMCInstrAnalysis(MII.get()));
702 if (!MIA)
703 return {};
704 std::vector<std::pair<uint64_t, uint64_t>> PltEntries;
705 std::optional<SectionRef> RelaPlt, RelaDyn;
706 uint64_t GotBaseVA = 0;
707 for (const SectionRef &Section : sections()) {
708 Expected<StringRef> NameOrErr = Section.getName();
709 if (!NameOrErr) {
710 consumeError(NameOrErr.takeError());
711 continue;
712 }
713 StringRef Name = *NameOrErr;
714
715 if (Name == ".rela.plt" || Name == ".rel.plt") {
716 RelaPlt = Section;
717 } else if (Name == ".rela.dyn" || Name == ".rel.dyn") {
718 RelaDyn = Section;
719 } else if (Name == ".got.plt") {
720 GotBaseVA = Section.getAddress();
721 } else if (Name == ".plt" || Name == ".plt.got") {
722 Expected<StringRef> PltContents = Section.getContents();
723 if (!PltContents) {
724 consumeError(PltContents.takeError());
725 return {};
726 }
728 PltEntries,
729 MIA->findPltEntries(Section.getAddress(),
730 arrayRefFromStringRef(*PltContents), STI));
731 }
732 }
733
734 // Build a map from GOT entry virtual address to PLT entry virtual address.
736 for (auto [Plt, GotPlt] : PltEntries) {
737 uint64_t GotPltEntry = GotPlt;
738 // An x86-32 PIC PLT uses jmp DWORD PTR [ebx-offset]. Add
739 // _GLOBAL_OFFSET_TABLE_ (EBX) to get the .got.plt (or .got) entry address.
740 // See X86MCTargetDesc.cpp:findPltEntries for the 1 << 32 bit.
741 if (GotPltEntry & (uint64_t(1) << 32) && getEMachine() == ELF::EM_386)
742 GotPltEntry = static_cast<int32_t>(GotPltEntry) + GotBaseVA;
743 GotToPlt.insert(std::make_pair(GotPltEntry, Plt));
744 }
745
746 // Find the relocations in the dynamic relocation table that point to
747 // locations in the GOT for which we know the corresponding PLT entry.
748 std::vector<ELFPltEntry> Result;
749 auto handleRels = [&](iterator_range<relocation_iterator> Rels,
750 uint32_t RelType, StringRef PltSec) {
751 for (const auto &R : Rels) {
752 if (R.getType() != RelType)
753 continue;
754 auto PltEntryIter = GotToPlt.find(R.getOffset());
755 if (PltEntryIter != GotToPlt.end()) {
756 symbol_iterator Sym = R.getSymbol();
757 if (Sym == symbol_end())
758 Result.push_back(
759 ELFPltEntry{PltSec, std::nullopt, PltEntryIter->second});
760 else
761 Result.push_back(ELFPltEntry{PltSec, Sym->getRawDataRefImpl(),
762 PltEntryIter->second});
763 }
764 }
765 };
766
767 if (RelaPlt)
768 handleRels(RelaPlt->relocations(), JumpSlotReloc, ".plt");
769
770 // If a symbol needing a PLT entry also needs a GLOB_DAT relocation, GNU ld's
771 // x86 port places the PLT entry in the .plt.got section.
772 if (RelaDyn)
773 handleRels(RelaDyn->relocations(), GlobDatReloc, ".plt.got");
774
775 return Result;
776}
777
778template <class ELFT>
780 const ELFFile<ELFT> &EF, std::optional<unsigned> TextSectionIndex,
781 std::vector<PGOAnalysisMap> *PGOAnalyses) {
782 using Elf_Shdr = typename ELFT::Shdr;
783 bool IsRelocatable = EF.getHeader().e_type == ELF::ET_REL;
784 std::vector<BBAddrMap> BBAddrMaps;
785 if (PGOAnalyses)
786 PGOAnalyses->clear();
787
788 const auto &Sections = cantFail(EF.sections());
789 auto IsMatch = [&](const Elf_Shdr &Sec) -> Expected<bool> {
790 if (Sec.sh_type != ELF::SHT_LLVM_BB_ADDR_MAP)
791 return false;
792 if (!TextSectionIndex)
793 return true;
794 Expected<const Elf_Shdr *> TextSecOrErr = EF.getSection(Sec.sh_link);
795 if (!TextSecOrErr)
796 return createError("unable to get the linked-to section for " +
797 describe(EF, Sec) + ": " +
798 toString(TextSecOrErr.takeError()));
799 assert(*TextSecOrErr >= Sections.begin() &&
800 "Text section pointer outside of bounds");
801 if (*TextSectionIndex !=
802 (unsigned)std::distance(Sections.begin(), *TextSecOrErr))
803 return false;
804 return true;
805 };
806
808 EF.getSectionAndRelocations(IsMatch);
809 if (!SectionRelocMapOrErr)
810 return SectionRelocMapOrErr.takeError();
811
812 for (auto const &[Sec, RelocSec] : *SectionRelocMapOrErr) {
813 if (IsRelocatable && !RelocSec)
814 return createError("unable to get relocation section for " +
815 describe(EF, *Sec));
816 Expected<std::vector<BBAddrMap>> BBAddrMapOrErr =
817 EF.decodeBBAddrMap(*Sec, RelocSec, PGOAnalyses);
818 if (!BBAddrMapOrErr) {
819 if (PGOAnalyses)
820 PGOAnalyses->clear();
821 return createError("unable to read " + describe(EF, *Sec) + ": " +
822 toString(BBAddrMapOrErr.takeError()));
823 }
824 std::move(BBAddrMapOrErr->begin(), BBAddrMapOrErr->end(),
825 std::back_inserter(BBAddrMaps));
826 }
827 if (PGOAnalyses)
828 assert(PGOAnalyses->size() == BBAddrMaps.size() &&
829 "The same number of BBAddrMaps and PGOAnalysisMaps should be "
830 "returned when PGO information is requested");
831 return BBAddrMaps;
832}
833
834template <class ELFT>
835static Expected<std::vector<VersionEntry>>
838 using Elf_Shdr = typename ELFT::Shdr;
839 const Elf_Shdr *VerSec = nullptr;
840 const Elf_Shdr *VerNeedSec = nullptr;
841 const Elf_Shdr *VerDefSec = nullptr;
842 // The user should ensure sections() can't fail here.
843 for (const Elf_Shdr &Sec : cantFail(EF.sections())) {
844 if (Sec.sh_type == ELF::SHT_GNU_versym)
845 VerSec = &Sec;
846 else if (Sec.sh_type == ELF::SHT_GNU_verdef)
847 VerDefSec = &Sec;
848 else if (Sec.sh_type == ELF::SHT_GNU_verneed)
849 VerNeedSec = &Sec;
850 }
851 if (!VerSec)
852 return std::vector<VersionEntry>();
853
855 EF.loadVersionMap(VerNeedSec, VerDefSec);
856 if (!MapOrErr)
857 return MapOrErr.takeError();
858
859 std::vector<VersionEntry> Ret;
860 size_t I = 0;
861 for (const ELFSymbolRef &Sym : Symbols) {
862 ++I;
864 EF.template getEntry<typename ELFT::Versym>(*VerSec, I);
865 if (!VerEntryOrErr)
866 return createError("unable to read an entry with index " + Twine(I) +
867 " from " + describe(EF, *VerSec) + ": " +
868 toString(VerEntryOrErr.takeError()));
869
870 Expected<uint32_t> FlagsOrErr = Sym.getFlags();
871 if (!FlagsOrErr)
872 return createError("unable to read flags for symbol with index " +
873 Twine(I) + ": " + toString(FlagsOrErr.takeError()));
874
875 bool IsDefault;
877 (*VerEntryOrErr)->vs_index, IsDefault, *MapOrErr,
878 (*FlagsOrErr) & SymbolRef::SF_Undefined);
879 if (!VerOrErr)
880 return createError("unable to get a version for entry " + Twine(I) +
881 " of " + describe(EF, *VerSec) + ": " +
882 toString(VerOrErr.takeError()));
883
884 Ret.push_back({(*VerOrErr).str(), IsDefault});
885 }
886
887 return Ret;
888}
889
890Expected<std::vector<VersionEntry>>
893 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
894 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
895 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
896 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
897 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
898 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
899 return readDynsymVersionsImpl(cast<ELF64BEObjectFile>(this)->getELFFile(),
900 Symbols);
901}
902
904 std::optional<unsigned> TextSectionIndex,
905 std::vector<PGOAnalysisMap> *PGOAnalyses) const {
906 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
907 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
908 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
909 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
910 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
911 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
912 return readBBAddrMapImpl(cast<ELF64BEObjectFile>(this)->getELFFile(),
913 TextSectionIndex, PGOAnalyses);
914}
915
917 auto Data = Sec.getRawDataRefImpl();
918 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
919 return Obj->getCrelDecodeProblem(Data);
920 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
921 return Obj->getCrelDecodeProblem(Data);
922 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
923 return Obj->getCrelDecodeProblem(Data);
924 return cast<ELF64BEObjectFile>(this)->getCrelDecodeProblem(Data);
925}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Kernel Attributes
#define X(NUM, ENUM, NAME)
Definition ELF.h:849
#define AMDGPU_MACH_LIST(X)
Definition ELF.h:766
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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)
#define I(x, y, z)
Definition MD5.cpp:57
#define T
static constexpr unsigned SM(unsigned Version)
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
iterator end()
Definition DenseMap.h:81
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:241
std::optional< unsigned > getAttributeValue(unsigned tag) const override
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
Generic base class for all target subtargets.
static LLVM_ABI 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:55
Manages the enabling and disabling of subtarget specific features.
LLVM_ABI void AddFeature(StringRef String, bool Enable=true)
Adds Features.
LLVM_ABI void addFeaturesVector(const ArrayRef< std::string > OtherFeatures)
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
LLVM_ABI void setArchName(StringRef Str)
Set the architecture (first) component of the triple by name.
Definition Triple.cpp:1709
bool isThumb() const
Tests whether the target is Thumb (little and big endian).
Definition Triple.h:952
SubArchType getSubArch() const
get the parsed subarchitecture type for this triple.
Definition Triple.h:423
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition Triple.h:420
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
A range adaptor for a pair of iterators.
DataRefImpl getRawDataRefImpl() const
MemoryBufferRef Data
Definition Binary.h:38
bool isLittleEndian() const
Definition Binary.h:157
const Elf_Ehdr & getHeader() const
Definition ELF.h:347
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:1011
Expected< StringRef > getSymbolVersionByIndex(uint32_t SymbolVersionIndex, bool &IsDefault, SmallVector< std::optional< VersionEntry >, 0 > &VersionMap, std::optional< bool > IsSymHidden) const
Definition ELF.h:1120
Expected< Elf_Shdr_Range > sections() const
Definition ELF.h:1038
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:1024
Expected< SmallVector< std::optional< VersionEntry >, 0 > > loadVersionMap(const Elf_Shdr *VerNeedSec, const Elf_Shdr *VerDefSec) const
Definition ELF.h:793
Expected< const Elf_Shdr * > getSection(const Elf_Sym &Sym, const Elf_Shdr *SymTab, DataRegion< Elf_Word > ShndxTable) const
Definition ELF.h:651
virtual uint8_t getEIdentABIVersion() const =0
virtual Error getBuildAttributes(ELFAttributeParser &Attributes) const =0
std::vector< ELFPltEntry > getPltEntries(const MCSubtargetInfo &STI) const
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
Expected< SubtargetFeatures > getFeatures() const override
std::optional< StringRef > tryGetCPUName() const override
iterator_range< elf_symbol_iterator > elf_symbol_iterator_range
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)
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.
section_iterator_range sections() const
Definition ObjectFile.h:331
ObjectFile(unsigned int Type, MemoryBufferRef Source)
DataRefImpl getRawDataRefImpl() const
Definition ObjectFile.h:603
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:154
@ EM_386
Definition ELF.h:141
@ EM_CUDA
Definition ELF.h:291
@ EM_LOONGARCH
Definition ELF.h:327
@ EM_BPF
Definition ELF.h:324
@ EM_PPC
Definition ELF.h:153
@ EM_HEXAGON
Definition ELF.h:262
@ EM_MIPS
Definition ELF.h:146
@ EM_RISCV
Definition ELF.h:322
@ EM_ARM
Definition ELF.h:161
@ EM_AMDGPU
Definition ELF.h:321
@ SHT_GNU_verneed
Definition ELF.h:1197
@ SHT_GNU_verdef
Definition ELF.h:1196
@ SHT_LLVM_BB_ADDR_MAP
Definition ELF.h:1184
@ SHT_GNU_versym
Definition ELF.h:1198
@ EF_MIPS_ARCH
Definition ELF.h:580
@ EF_MIPS_MICROMIPS
Definition ELF.h:563
@ EF_MIPS_ARCH_32R6
Definition ELF.h:578
@ EF_MIPS_MACH_NONE
Definition ELF.h:541
@ EF_MIPS_ARCH_64
Definition ELF.h:575
@ EF_MIPS_ARCH_32
Definition ELF.h:574
@ EF_MIPS_MACH_OCTEON
Definition ELF.h:549
@ EF_MIPS_ARCH_4
Definition ELF.h:572
@ EF_MIPS_ARCH_5
Definition ELF.h:573
@ EF_MIPS_ARCH_2
Definition ELF.h:570
@ EF_MIPS_ARCH_32R2
Definition ELF.h:576
@ EF_MIPS_ARCH_64R2
Definition ELF.h:577
@ EF_MIPS_ARCH_ASE_M16
Definition ELF.h:564
@ EF_MIPS_MACH
Definition ELF.h:560
@ EF_MIPS_ARCH_1
Definition ELF.h:569
@ EF_MIPS_ARCH_64R6
Definition ELF.h:579
@ EF_MIPS_ARCH_3
Definition ELF.h:571
@ ELFDATA2MSB
Definition ELF.h:341
@ ELFDATA2LSB
Definition ELF.h:340
@ EF_LOONGARCH_ABI_SINGLE_FLOAT
Definition ELF.h:1067
@ EF_LOONGARCH_ABI_DOUBLE_FLOAT
Definition ELF.h:1068
@ EF_LOONGARCH_ABI_SOFT_FLOAT
Definition ELF.h:1066
@ EF_LOONGARCH_ABI_MODIFIER_MASK
Definition ELF.h:1069
@ STT_FUNC
Definition ELF.h:1417
@ STT_NOTYPE
Definition ELF.h:1415
@ STT_SECTION
Definition ELF.h:1418
@ STT_FILE
Definition ELF.h:1419
@ STT_COMMON
Definition ELF.h:1420
@ STT_GNU_IFUNC
Definition ELF.h:1422
@ STT_OBJECT
Definition ELF.h:1416
@ STT_TLS
Definition ELF.h:1421
@ EF_CUDA_SM21
Definition ELF.h:944
@ EF_CUDA_SM90
Definition ELF.h:963
@ EF_CUDA_SM86
Definition ELF.h:959
@ EF_CUDA_SM60
Definition ELF.h:952
@ EF_CUDA_SM
Definition ELF.h:934
@ EF_CUDA_SM89
Definition ELF.h:962
@ EF_CUDA_SM37
Definition ELF.h:948
@ EF_CUDA_SM32
Definition ELF.h:946
@ EF_CUDA_SM72
Definition ELF.h:956
@ EF_CUDA_SM50
Definition ELF.h:949
@ EF_CUDA_ACCELERATORS
Definition ELF.h:986
@ EF_CUDA_SM121
Definition ELF.h:969
@ EF_CUDA_SM61
Definition ELF.h:953
@ EF_CUDA_SM_MASK
Definition ELF.h:937
@ EF_CUDA_SM52
Definition ELF.h:950
@ EF_CUDA_SM35
Definition ELF.h:947
@ EF_CUDA_SM120
Definition ELF.h:968
@ EF_CUDA_SM100
Definition ELF.h:964
@ EF_CUDA_SM62
Definition ELF.h:954
@ EF_CUDA_SM101
Definition ELF.h:965
@ EF_CUDA_SM30
Definition ELF.h:945
@ EF_CUDA_SM_OFFSET
Definition ELF.h:940
@ EF_CUDA_ACCELERATORS_V1
Definition ELF.h:978
@ EF_CUDA_SM75
Definition ELF.h:957
@ EF_CUDA_SM103
Definition ELF.h:966
@ EF_CUDA_SM87
Definition ELF.h:960
@ EF_CUDA_SM20
Definition ELF.h:943
@ EF_CUDA_SM88
Definition ELF.h:961
@ EF_CUDA_SM80
Definition ELF.h:958
@ EF_CUDA_SM53
Definition ELF.h:951
@ EF_CUDA_SM70
Definition ELF.h:955
@ EF_CUDA_SM110
Definition ELF.h:967
@ ELFCLASS64
Definition ELF.h:334
@ ELFCLASS32
Definition ELF.h:333
@ ET_REL
Definition ELF.h:119
@ EF_AMDGPU_MACH
Definition ELF.h:844
@ ELFABIVERSION_CUDA_V1
Definition ELF.h:391
@ EF_RISCV_RVC
Definition ELF.h:710
Error createError(const Twine &Err)
Definition Error.h:86
constexpr int NumElfSymbolTypes
static std::string describe(const ELFFile< ELFT > &Obj, const typename ELFT::Shdr &Sec)
Definition ELF.h:147
std::pair< unsigned char, unsigned char > getElfArchType(StringRef Object)
Definition ELF.h:82
LLVM_ABI const llvm::EnumEntry< unsigned > ElfSymbolTypes[NumElfSymbolTypes]
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
ArrayRef< CharT > arrayRefFromStringRef(StringRef Input)
Construct a string ref from an array ref of unsigned chars.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2208
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:202
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1083
static const Target * lookupTarget(StringRef TripleStr, std::string &Error)
lookupTarget - Lookup a target based on a target triple.