LLVM 24.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 constexpr EnumStringDef<uint8_t, 2> ElfSymbolTypeDefs[] = {
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 static constexpr auto ElfSymbolTypes = BUILD_ENUM_STRINGS(ElfSymbolTypeDefs);
60 return ElfSymbolTypes;
61}
62
65
66template <class ELFT>
68createPtr(MemoryBufferRef Object, bool InitContent) {
69 auto Ret = ELFObjectFile<ELFT>::create(Object, InitContent);
70 if (Error E = Ret.takeError())
71 return std::move(E);
72 return std::make_unique<ELFObjectFile<ELFT>>(std::move(*Ret));
73}
74
75Expected<std::unique_ptr<ObjectFile>>
77 std::pair<unsigned char, unsigned char> Ident =
78 getElfArchType(Obj.getBuffer());
79 std::size_t MaxAlignment =
80 1ULL << llvm::countr_zero(
81 reinterpret_cast<uintptr_t>(Obj.getBufferStart()));
82
83 if (MaxAlignment < 2)
84 return createError("Insufficient alignment");
85
86 if (Ident.first == ELF::ELFCLASS32) {
87 if (Ident.second == ELF::ELFDATA2LSB)
88 return createPtr<ELF32LE>(Obj, InitContent);
89 else if (Ident.second == ELF::ELFDATA2MSB)
90 return createPtr<ELF32BE>(Obj, InitContent);
91 else
92 return createError("Invalid ELF data");
93 } else if (Ident.first == ELF::ELFCLASS64) {
94 if (Ident.second == ELF::ELFDATA2LSB)
95 return createPtr<ELF64LE>(Obj, InitContent);
96 else if (Ident.second == ELF::ELFDATA2MSB)
97 return createPtr<ELF64BE>(Obj, InitContent);
98 else
99 return createError("Invalid ELF data");
100 }
101 return createError("Invalid ELF class");
102}
103
104SubtargetFeatures ELFObjectFileBase::getMIPSFeatures() const {
105 SubtargetFeatures Features;
106 unsigned PlatformFlags = getPlatformFlags();
107
108 switch (PlatformFlags & ELF::EF_MIPS_ARCH) {
110 break;
112 Features.AddFeature("mips2");
113 break;
115 Features.AddFeature("mips3");
116 break;
118 Features.AddFeature("mips4");
119 break;
121 Features.AddFeature("mips5");
122 break;
124 Features.AddFeature("mips32");
125 break;
127 Features.AddFeature("mips64");
128 break;
130 Features.AddFeature("mips32r2");
131 break;
133 Features.AddFeature("mips64r2");
134 break;
136 Features.AddFeature("mips32r6");
137 break;
139 Features.AddFeature("mips64r6");
140 break;
141 default:
142 // The flags come from the input file, so an unknown value is possible.
143 break;
144 }
145
146 switch (PlatformFlags & ELF::EF_MIPS_MACH) {
148 Features.AddFeature("cnmips");
149 break;
151 Features.AddFeature("r5900");
152 break;
153 default:
154 // No feature associated with this value.
155 break;
156 }
157
158 if (PlatformFlags & ELF::EF_MIPS_ARCH_ASE_M16)
159 Features.AddFeature("mips16");
160 if (PlatformFlags & ELF::EF_MIPS_MICROMIPS)
161 Features.AddFeature("micromips");
162
163 return Features;
164}
165
166SubtargetFeatures ELFObjectFileBase::getARMFeatures() const {
167 SubtargetFeatures Features;
168 ARMAttributeParser Attributes;
170 consumeError(std::move(E));
171 return SubtargetFeatures();
172 }
173
174 // both ARMv7-M and R have to support thumb hardware div
175 bool isV7 = false;
176 std::optional<unsigned> Attr =
177 Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
178 if (Attr)
179 isV7 = *Attr == ARMBuildAttrs::v7;
180
181 Attr = Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile);
182 if (Attr) {
183 switch (*Attr) {
185 Features.AddFeature("aclass");
186 break;
188 Features.AddFeature("rclass");
189 if (isV7)
190 Features.AddFeature("hwdiv");
191 break;
193 Features.AddFeature("mclass");
194 if (isV7)
195 Features.AddFeature("hwdiv");
196 break;
197 }
198 }
199
200 Attr = Attributes.getAttributeValue(ARMBuildAttrs::THUMB_ISA_use);
201 if (Attr) {
202 switch (*Attr) {
203 default:
204 break;
206 Features.AddFeature("thumb", false);
207 Features.AddFeature("thumb2", false);
208 break;
210 Features.AddFeature("thumb2");
211 break;
212 }
213 }
214
215 Attr = Attributes.getAttributeValue(ARMBuildAttrs::FP_arch);
216 if (Attr) {
217 switch (*Attr) {
218 default:
219 break;
221 Features.AddFeature("vfp2sp", false);
222 Features.AddFeature("vfp3d16sp", false);
223 Features.AddFeature("vfp4d16sp", false);
224 break;
226 Features.AddFeature("vfp2");
227 break;
230 Features.AddFeature("vfp3");
231 break;
234 Features.AddFeature("vfp4");
235 break;
236 }
237 }
238
239 Attr = Attributes.getAttributeValue(ARMBuildAttrs::Advanced_SIMD_arch);
240 if (Attr) {
241 switch (*Attr) {
242 default:
243 break;
245 Features.AddFeature("neon", false);
246 Features.AddFeature("fp16", false);
247 break;
249 Features.AddFeature("neon");
250 break;
252 Features.AddFeature("neon");
253 Features.AddFeature("fp16");
254 break;
255 }
256 }
257
258 Attr = Attributes.getAttributeValue(ARMBuildAttrs::MVE_arch);
259 if (Attr) {
260 switch (*Attr) {
261 default:
262 break;
264 Features.AddFeature("mve", false);
265 Features.AddFeature("mve.fp", false);
266 break;
268 Features.AddFeature("mve.fp", false);
269 Features.AddFeature("mve");
270 break;
272 Features.AddFeature("mve.fp");
273 break;
274 }
275 }
276
277 Attr = Attributes.getAttributeValue(ARMBuildAttrs::DIV_use);
278 if (Attr) {
279 switch (*Attr) {
280 default:
281 break;
283 Features.AddFeature("hwdiv", false);
284 Features.AddFeature("hwdiv-arm", false);
285 break;
287 Features.AddFeature("hwdiv");
288 Features.AddFeature("hwdiv-arm");
289 break;
290 }
291 }
292
293 return Features;
294}
295
296static std::string hexagonAttrToFeatureString(unsigned Attr) {
297 return "v" + utostr(Attr);
298}
299
300SubtargetFeatures ELFObjectFileBase::getHexagonFeatures() const {
301 SubtargetFeatures Features;
302 HexagonAttributeParser Parser;
303 if (Error E = getBuildAttributes(Parser)) {
304 // Return no attributes if none can be read.
305 // This behavior is important for backwards compatibility.
306 consumeError(std::move(E));
307 return Features;
308 }
309 std::optional<unsigned> Attr;
310
311 if ((Attr = Parser.getAttributeValue(HexagonAttrs::ARCH)))
312 Features.AddFeature(hexagonAttrToFeatureString(*Attr));
313
314 if ((Attr = Parser.getAttributeValue(HexagonAttrs::HVXARCH)))
315 Features.AddFeature("hvx" + hexagonAttrToFeatureString(*Attr));
316
317 if ((Attr = Parser.getAttributeValue(HexagonAttrs::HVXIEEEFP)))
318 if (*Attr)
319 Features.AddFeature("hvx-ieee-fp");
320
321 if ((Attr = Parser.getAttributeValue(HexagonAttrs::HVXQFLOAT)))
322 if (*Attr)
323 Features.AddFeature("hvx-qfloat");
324
325 if ((Attr = Parser.getAttributeValue(HexagonAttrs::ZREG)))
326 if (*Attr)
327 Features.AddFeature("zreg");
328
329 if ((Attr = Parser.getAttributeValue(HexagonAttrs::AUDIO)))
330 if (*Attr)
331 Features.AddFeature("audio");
332
333 if ((Attr = Parser.getAttributeValue(HexagonAttrs::CABAC)))
334 if (*Attr)
335 Features.AddFeature("cabac");
336
337 return Features;
338}
339
340Expected<SubtargetFeatures> ELFObjectFileBase::getRISCVFeatures() const {
341 SubtargetFeatures Features;
342 unsigned PlatformFlags = getPlatformFlags();
343
344 if (PlatformFlags & ELF::EF_RISCV_RVC) {
345 Features.AddFeature("zca");
346 }
347
348 RISCVAttributeParser Attributes;
350 return std::move(E);
351 }
352
353 std::optional<StringRef> Attr =
354 Attributes.getAttributeString(RISCVAttrs::ARCH);
355 if (Attr) {
356 auto ParseResult = RISCVISAInfo::parseNormalizedArchString(*Attr);
357 if (!ParseResult)
358 return ParseResult.takeError();
359 auto &ISAInfo = *ParseResult;
360
361 if (ISAInfo->getXLen() == 32)
362 Features.AddFeature("64bit", false);
363 else if (ISAInfo->getXLen() == 64)
364 Features.AddFeature("64bit");
365 else
366 llvm_unreachable("XLEN should be 32 or 64.");
367
368 Features.addFeaturesVector(ISAInfo->toFeatures());
369 }
370
371 return Features;
372}
373
374SubtargetFeatures ELFObjectFileBase::getLoongArchFeatures() const {
375 SubtargetFeatures Features;
376
379 break;
381 Features.AddFeature("d");
382 // D implies F according to LoongArch ISA spec.
383 [[fallthrough]];
385 Features.AddFeature("f");
386 break;
387 }
388
389 return Features;
390}
391
392SubtargetFeatures ELFObjectFileBase::getSparcFeatures() const {
393 SubtargetFeatures Features;
394 unsigned PlatformFlags = getPlatformFlags();
395
396 if (getEMachine() == ELF::EM_SPARC32PLUS ||
397 (PlatformFlags & ELF::EF_SPARC_32PLUS))
398 Features.AddFeature("v9");
399 if (PlatformFlags & ELF::EF_SPARC_SUN_US1)
400 Features.AddFeature("vis");
401 if (PlatformFlags & ELF::EF_SPARC_SUN_US3)
402 Features.AddFeature("vis2");
403
404 return Features;
405}
406
408 switch (getEMachine()) {
409 case ELF::EM_MIPS:
410 return getMIPSFeatures();
411 case ELF::EM_ARM:
412 return getARMFeatures();
413 case ELF::EM_RISCV:
414 return getRISCVFeatures();
415 case ELF::EM_LOONGARCH:
416 return getLoongArchFeatures();
417 case ELF::EM_HEXAGON:
418 return getHexagonFeatures();
419 case ELF::EM_SPARC:
420 case ELF::EM_SPARC32PLUS:
421 case ELF::EM_SPARCV9:
422 return getSparcFeatures();
423 default:
424 return SubtargetFeatures();
425 }
426}
427
428std::optional<StringRef> ELFObjectFileBase::tryGetCPUName() const {
429 switch (getEMachine()) {
430 case ELF::EM_AMDGPU:
431 return getAMDGPUCPUName();
432 case ELF::EM_CUDA:
433 return getNVPTXCPUName();
434 case ELF::EM_PPC:
435 case ELF::EM_PPC64:
436 return StringRef("future");
437 case ELF::EM_BPF:
438 return StringRef("v4");
439 default:
440 return std::nullopt;
441 }
442}
443
444StringRef ELFObjectFileBase::getAMDGPUCPUName() const {
445 assert(getEMachine() == ELF::EM_AMDGPU);
446 unsigned CPU = getPlatformFlags() & ELF::EF_AMDGPU_MACH;
447
448 switch (CPU) {
449#define X(NUM, ENUM, NAME) \
450 case ELF::ENUM: \
451 return NAME;
453#undef X
454
455 default:
456 llvm_unreachable("Unknown EF_AMDGPU_MACH value");
457 }
458}
459
460StringRef ELFObjectFileBase::getNVPTXCPUName() const {
461 assert(getEMachine() == ELF::EM_CUDA);
466
467 switch (SM) {
468 // Fermi architecture.
470 return "sm_20";
472 return "sm_21";
473
474 // Kepler architecture.
476 return "sm_30";
478 return "sm_32";
480 return "sm_35";
482 return "sm_37";
483
484 // Maxwell architecture.
486 return "sm_50";
488 return "sm_52";
490 return "sm_53";
491
492 // Pascal architecture.
494 return "sm_60";
496 return "sm_61";
498 return "sm_62";
499
500 // Volta architecture.
502 return "sm_70";
504 return "sm_72";
505
506 // Turing architecture.
508 return "sm_75";
509
510 // Ampere architecture.
512 return "sm_80";
514 return "sm_86";
516 return "sm_87";
518 return "sm_88";
519
520 // Ada architecture.
522 return "sm_89";
523
524 // Hopper architecture.
527 : "sm_90";
528
529 // Blackwell architecture.
531 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_100a"
532 : "sm_100";
534 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_101a"
535 : "sm_101";
537 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_103a"
538 : "sm_103";
540 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_107a"
541 : "sm_107";
543 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_110a"
544 : "sm_110";
545
546 // Rubin architecture.
548 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_120a"
549 : "sm_120";
551 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_121a"
552 : "sm_121";
553 default:
554 llvm_unreachable("Unknown EF_CUDA_SM value");
555 }
556}
557
558// FIXME Encode from a tablegen description or target parser.
560 if (TheTriple.getSubArch() != Triple::NoSubArch)
561 return;
562
563 ARMAttributeParser Attributes;
564 if (Error E = getBuildAttributes(Attributes)) {
565 // TODO Propagate Error.
566 consumeError(std::move(E));
567 return;
568 }
569
570 std::string Triple;
571 // Default to ARM, but use the triple if it's been set.
572 if (TheTriple.isThumb())
573 Triple = "thumb";
574 else
575 Triple = "arm";
576
577 std::optional<unsigned> Attr =
578 Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
579 if (Attr) {
580 switch (*Attr) {
582 Triple += "v4";
583 break;
585 Triple += "v4t";
586 break;
588 Triple += "v5t";
589 break;
591 Triple += "v5te";
592 break;
594 Triple += "v5tej";
595 break;
597 Triple += "v6";
598 break;
600 Triple += "v6kz";
601 break;
603 Triple += "v6t2";
604 break;
606 Triple += "v6k";
607 break;
608 case ARMBuildAttrs::v7: {
609 std::optional<unsigned> ArchProfileAttr =
610 Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile);
611 if (ArchProfileAttr == ARMBuildAttrs::MicroControllerProfile)
612 Triple += "v7m";
613 else
614 Triple += "v7";
615 break;
616 }
618 Triple += "v6m";
619 break;
621 Triple += "v6sm";
622 break;
624 Triple += "v7em";
625 break;
627 Triple += "v8a";
628 break;
630 Triple += "v8r";
631 break;
633 Triple += "v8m.base";
634 break;
636 Triple += "v8m.main";
637 break;
639 Triple += "v8.1m.main";
640 break;
642 Triple += "v9a";
643 break;
644 }
645 }
646 if (!isLittleEndian())
647 Triple += "eb";
648
649 TheTriple.setArchName(Triple);
650}
651
652std::vector<ELFPltEntry>
654 std::string Err;
655 const auto Triple = makeTriple();
656 const auto *T = TargetRegistry::lookupTarget(Triple, Err);
657 if (!T)
658 return {};
659 uint32_t JumpSlotReloc = 0, GlobDatReloc = 0;
660 switch (Triple.getArch()) {
661 case Triple::x86:
662 JumpSlotReloc = ELF::R_386_JUMP_SLOT;
663 GlobDatReloc = ELF::R_386_GLOB_DAT;
664 break;
665 case Triple::x86_64:
666 JumpSlotReloc = ELF::R_X86_64_JUMP_SLOT;
667 GlobDatReloc = ELF::R_X86_64_GLOB_DAT;
668 break;
669 case Triple::aarch64:
671 JumpSlotReloc = ELF::R_AARCH64_JUMP_SLOT;
672 break;
673 case Triple::arm:
674 case Triple::armeb:
675 case Triple::thumb:
676 case Triple::thumbeb:
677 JumpSlotReloc = ELF::R_ARM_JUMP_SLOT;
678 break;
679 case Triple::hexagon:
680 JumpSlotReloc = ELF::R_HEX_JMP_SLOT;
681 GlobDatReloc = ELF::R_HEX_GLOB_DAT;
682 break;
683 case Triple::riscv32:
684 case Triple::riscv64:
685 JumpSlotReloc = ELF::R_RISCV_JUMP_SLOT;
686 break;
689 JumpSlotReloc = ELF::R_LARCH_JUMP_SLOT;
690 break;
691 default:
692 return {};
693 }
694 std::unique_ptr<const MCInstrInfo> MII(T->createMCInstrInfo());
695 std::unique_ptr<const MCInstrAnalysis> MIA(
696 T->createMCInstrAnalysis(MII.get()));
697 if (!MIA)
698 return {};
699 std::vector<std::pair<uint64_t, uint64_t>> PltEntries;
700 std::optional<SectionRef> RelaPlt, RelaDyn;
701 uint64_t GotBaseVA = 0;
702 for (const SectionRef &Section : sections()) {
703 Expected<StringRef> NameOrErr = Section.getName();
704 if (!NameOrErr) {
705 consumeError(NameOrErr.takeError());
706 continue;
707 }
708 StringRef Name = *NameOrErr;
709
710 if (Name == ".rela.plt" || Name == ".rel.plt") {
711 RelaPlt = Section;
712 } else if (Name == ".rela.dyn" || Name == ".rel.dyn") {
713 RelaDyn = Section;
714 } else if (Name == ".got.plt") {
715 GotBaseVA = Section.getAddress();
716 } else if (Name == ".plt" || Name == ".plt.got") {
717 Expected<StringRef> PltContents = Section.getContents();
718 if (!PltContents) {
719 consumeError(PltContents.takeError());
720 return {};
721 }
723 PltEntries,
724 MIA->findPltEntries(Section.getAddress(),
725 arrayRefFromStringRef(*PltContents), STI));
726 }
727 }
728
729 // Build a map from GOT entry virtual address to PLT entry virtual address.
731 for (auto [Plt, GotPlt] : PltEntries) {
732 uint64_t GotPltEntry = GotPlt;
733 // An x86-32 PIC PLT uses jmp DWORD PTR [ebx-offset]. Add
734 // _GLOBAL_OFFSET_TABLE_ (EBX) to get the .got.plt (or .got) entry address.
735 // See X86MCTargetDesc.cpp:findPltEntries for the 1 << 32 bit.
736 if (GotPltEntry & (uint64_t(1) << 32) && getEMachine() == ELF::EM_386)
737 GotPltEntry = static_cast<int32_t>(GotPltEntry) + GotBaseVA;
738 GotToPlt.insert(std::make_pair(GotPltEntry, Plt));
739 }
740
741 // Find the relocations in the dynamic relocation table that point to
742 // locations in the GOT for which we know the corresponding PLT entry.
743 std::vector<ELFPltEntry> Result;
744 auto handleRels = [&](iterator_range<relocation_iterator> Rels,
745 uint32_t RelType, StringRef PltSec) {
746 for (const auto &R : Rels) {
747 if (R.getType() != RelType)
748 continue;
749 auto PltEntryIter = GotToPlt.find(R.getOffset());
750 if (PltEntryIter != GotToPlt.end()) {
751 symbol_iterator Sym = R.getSymbol();
752 if (Sym == symbol_end())
753 Result.push_back(
754 ELFPltEntry{PltSec, std::nullopt, PltEntryIter->second});
755 else
756 Result.push_back(ELFPltEntry{PltSec, Sym->getRawDataRefImpl(),
757 PltEntryIter->second});
758 }
759 }
760 };
761
762 if (RelaPlt)
763 handleRels(RelaPlt->relocations(), JumpSlotReloc, ".plt");
764
765 // If a symbol needing a PLT entry also needs a GLOB_DAT relocation, GNU ld's
766 // x86 port places the PLT entry in the .plt.got section.
767 if (RelaDyn)
768 handleRels(RelaDyn->relocations(), GlobDatReloc, ".plt.got");
769
770 return Result;
771}
772
773template <class ELFT>
775 const ELFFile<ELFT> &EF, std::optional<unsigned> TextSectionIndex,
776 std::vector<PGOAnalysisMap> *PGOAnalyses) {
777 using Elf_Shdr = typename ELFT::Shdr;
778 bool IsRelocatable = EF.getHeader().e_type == ELF::ET_REL;
779 std::vector<BBAddrMap> BBAddrMaps;
780 if (PGOAnalyses)
781 PGOAnalyses->clear();
782
783 const auto &Sections = cantFail(EF.sections());
784 auto IsMatch = [&](const Elf_Shdr &Sec) -> Expected<bool> {
785 if (Sec.sh_type != ELF::SHT_LLVM_BB_ADDR_MAP)
786 return false;
787 if (!TextSectionIndex)
788 return true;
789 Expected<const Elf_Shdr *> TextSecOrErr = EF.getSection(Sec.sh_link);
790 if (!TextSecOrErr)
791 return createError("unable to get the linked-to section for " +
792 describe(EF, Sec) + ": " +
793 toString(TextSecOrErr.takeError()));
794 assert(*TextSecOrErr >= Sections.begin() &&
795 "Text section pointer outside of bounds");
796 if (*TextSectionIndex !=
797 (unsigned)std::distance(Sections.begin(), *TextSecOrErr))
798 return false;
799 return true;
800 };
801
803 EF.getSectionAndRelocations(IsMatch);
804 if (!SectionRelocMapOrErr)
805 return SectionRelocMapOrErr.takeError();
806
807 for (auto const &[Sec, RelocSec] : *SectionRelocMapOrErr) {
808 if (IsRelocatable && !RelocSec)
809 return createError("unable to get relocation section for " +
810 describe(EF, *Sec));
811 Expected<std::vector<BBAddrMap>> BBAddrMapOrErr =
812 EF.decodeBBAddrMap(*Sec, RelocSec, PGOAnalyses);
813 if (!BBAddrMapOrErr) {
814 if (PGOAnalyses)
815 PGOAnalyses->clear();
816 return createError("unable to read BB addr map section: " +
817 toString(BBAddrMapOrErr.takeError()));
818 }
819 std::move(BBAddrMapOrErr->begin(), BBAddrMapOrErr->end(),
820 std::back_inserter(BBAddrMaps));
821 }
822 if (PGOAnalyses)
823 assert(PGOAnalyses->size() == BBAddrMaps.size() &&
824 "The same number of BBAddrMaps and PGOAnalysisMaps should be "
825 "returned when PGO information is requested");
826 return BBAddrMaps;
827}
828
829template <class ELFT>
830static Expected<std::vector<VersionEntry>>
833 using Elf_Shdr = typename ELFT::Shdr;
834 const Elf_Shdr *VerSec = nullptr;
835 const Elf_Shdr *VerNeedSec = nullptr;
836 const Elf_Shdr *VerDefSec = nullptr;
837 // The user should ensure sections() can't fail here.
838 for (const Elf_Shdr &Sec : cantFail(EF.sections())) {
839 if (Sec.sh_type == ELF::SHT_GNU_versym)
840 VerSec = &Sec;
841 else if (Sec.sh_type == ELF::SHT_GNU_verdef)
842 VerDefSec = &Sec;
843 else if (Sec.sh_type == ELF::SHT_GNU_verneed)
844 VerNeedSec = &Sec;
845 }
846 if (!VerSec)
847 return std::vector<VersionEntry>();
848
850 EF.loadVersionMap(VerNeedSec, VerDefSec);
851 if (!MapOrErr)
852 return MapOrErr.takeError();
853
854 std::vector<VersionEntry> Ret;
855 size_t I = 0;
856 for (const ELFSymbolRef &Sym : Symbols) {
857 ++I;
859 EF.template getEntry<typename ELFT::Versym>(*VerSec, I);
860 if (!VerEntryOrErr)
861 return createError("unable to read an entry with index " + Twine(I) +
862 " from " + describe(EF, *VerSec) + ": " +
863 toString(VerEntryOrErr.takeError()));
864
865 Expected<uint32_t> FlagsOrErr = Sym.getFlags();
866 if (!FlagsOrErr)
867 return createError("unable to read flags for symbol with index " +
868 Twine(I) + ": " + toString(FlagsOrErr.takeError()));
869
870 bool IsDefault;
872 (*VerEntryOrErr)->vs_index, IsDefault, *MapOrErr,
873 (*FlagsOrErr) & SymbolRef::SF_Undefined);
874 if (!VerOrErr)
875 return createError("unable to get a version for entry " + Twine(I) +
876 " of " + describe(EF, *VerSec) + ": " +
877 toString(VerOrErr.takeError()));
878
879 Ret.push_back({(*VerOrErr).str(), IsDefault});
880 }
881
882 return Ret;
883}
884
885Expected<std::vector<VersionEntry>>
888 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
889 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
890 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
891 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
892 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
893 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
894 return readDynsymVersionsImpl(cast<ELF64BEObjectFile>(this)->getELFFile(),
895 Symbols);
896}
897
899 std::optional<unsigned> TextSectionIndex,
900 std::vector<PGOAnalysisMap> *PGOAnalyses) const {
901 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
902 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
903 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
904 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
905 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
906 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
907 return readBBAddrMapImpl(cast<ELF64BEObjectFile>(this)->getELFFile(),
908 TextSectionIndex, PGOAnalyses);
909}
910
912 auto Data = Sec.getRawDataRefImpl();
913 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
914 return Obj->getCrelDecodeProblem(Data);
915 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
916 return Obj->getCrelDecodeProblem(Data);
917 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
918 return Obj->getCrelDecodeProblem(Data);
919 return cast<ELF64BEObjectFile>(this)->getCrelDecodeProblem(Data);
920}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
AMDGPU Kernel Attributes
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
#define AMDGPU_MACH_LIST(X)
Definition ELF.h:576
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::string hexagonAttrToFeatureString(unsigned Attr)
static Expected< std::vector< VersionEntry > > readDynsymVersionsImpl(const ELFFile< ELFT > &EF, ELFObjectFileBase::elf_symbol_iterator_range Symbols)
#define BUILD_ENUM_STRINGS(Tab)
Definition Enum.h:120
#define I(x, y, z)
Definition MD5.cpp:57
#define T
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:766
iterator end()
Definition DenseMap.h:686
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:827
std::optional< uint64_t > 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...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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:48
LLVM_ABI void setArchName(StringRef Str)
Set the architecture (first) component of the triple by name.
Definition Triple.cpp:1721
bool isThumb() const
Tests whether the target is Thumb (little and big endian).
Definition Triple.h:1003
SubArchType getSubArch() const
get the parsed subarchitecture type for this triple.
Definition Triple.h:520
@ loongarch32
Definition Triple.h:65
@ loongarch64
Definition Triple.h:66
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition Triple.h:517
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:346
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:872
Expected< StringRef > getSymbolVersionByIndex(uint32_t SymbolVersionIndex, bool &IsDefault, SmallVector< std::optional< VersionEntry >, 0 > &VersionMap, std::optional< bool > IsSymHidden) const
Definition ELF.h:1119
Expected< Elf_Shdr_Range > sections() const
Definition ELF.h:1037
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:885
Expected< SmallVector< std::optional< VersionEntry >, 0 > > loadVersionMap(const Elf_Shdr *VerNeedSec, const Elf_Shdr *VerDefSec) const
Definition ELF.h:792
Expected< const Elf_Shdr * > getSection(const Elf_Sym &Sym, const Elf_Shdr *SymTab, DataRegion< Elf_Word > ShndxTable) const
Definition ELF.h:650
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.
@ STT_FUNC
Definition ELF.h:1274
@ STT_NOTYPE
Definition ELF.h:1272
@ STT_SECTION
Definition ELF.h:1275
@ STT_FILE
Definition ELF.h:1276
@ STT_COMMON
Definition ELF.h:1277
@ STT_GNU_IFUNC
Definition ELF.h:1279
@ STT_OBJECT
Definition ELF.h:1273
@ STT_TLS
Definition ELF.h:1278
@ EF_MIPS_MACH_5900
Definition ELF.h:362
@ EF_MIPS_ARCH
Definition ELF.h:388
@ EF_MIPS_MICROMIPS
Definition ELF.h:371
@ EF_MIPS_ARCH_32R6
Definition ELF.h:386
@ EF_MIPS_ARCH_64
Definition ELF.h:383
@ EF_MIPS_ARCH_32
Definition ELF.h:382
@ EF_MIPS_MACH_OCTEON
Definition ELF.h:357
@ EF_MIPS_ARCH_4
Definition ELF.h:380
@ EF_MIPS_ARCH_5
Definition ELF.h:381
@ EF_MIPS_ARCH_2
Definition ELF.h:378
@ EF_MIPS_ARCH_32R2
Definition ELF.h:384
@ EF_MIPS_ARCH_64R2
Definition ELF.h:385
@ EF_MIPS_ARCH_ASE_M16
Definition ELF.h:372
@ EF_MIPS_MACH
Definition ELF.h:368
@ EF_MIPS_ARCH_1
Definition ELF.h:377
@ EF_MIPS_ARCH_64R6
Definition ELF.h:387
@ EF_MIPS_ARCH_3
Definition ELF.h:379
@ ELFDATA2MSB
Definition ELF.h:149
@ ELFDATA2LSB
Definition ELF.h:148
@ EF_LOONGARCH_ABI_SINGLE_FLOAT
Definition ELF.h:886
@ EF_LOONGARCH_ABI_DOUBLE_FLOAT
Definition ELF.h:887
@ EF_LOONGARCH_ABI_SOFT_FLOAT
Definition ELF.h:885
@ EF_LOONGARCH_ABI_MODIFIER_MASK
Definition ELF.h:888
@ EF_SPARC_32PLUS
Definition ELF.h:558
@ EF_SPARC_SUN_US1
Definition ELF.h:559
@ EF_SPARC_SUN_US3
Definition ELF.h:561
@ EF_CUDA_SM21
Definition ELF.h:762
@ EF_CUDA_SM90
Definition ELF.h:781
@ EF_CUDA_SM86
Definition ELF.h:777
@ EF_CUDA_SM60
Definition ELF.h:770
@ EF_CUDA_SM
Definition ELF.h:752
@ EF_CUDA_SM89
Definition ELF.h:780
@ EF_CUDA_SM37
Definition ELF.h:766
@ EF_CUDA_SM32
Definition ELF.h:764
@ EF_CUDA_SM72
Definition ELF.h:774
@ EF_CUDA_SM50
Definition ELF.h:767
@ EF_CUDA_ACCELERATORS
Definition ELF.h:805
@ EF_CUDA_SM121
Definition ELF.h:788
@ EF_CUDA_SM61
Definition ELF.h:771
@ EF_CUDA_SM_MASK
Definition ELF.h:755
@ EF_CUDA_SM52
Definition ELF.h:768
@ EF_CUDA_SM35
Definition ELF.h:765
@ EF_CUDA_SM120
Definition ELF.h:787
@ EF_CUDA_SM100
Definition ELF.h:782
@ EF_CUDA_SM62
Definition ELF.h:772
@ EF_CUDA_SM101
Definition ELF.h:783
@ EF_CUDA_SM30
Definition ELF.h:763
@ EF_CUDA_SM_OFFSET
Definition ELF.h:758
@ EF_CUDA_ACCELERATORS_V1
Definition ELF.h:797
@ EF_CUDA_SM75
Definition ELF.h:775
@ EF_CUDA_SM103
Definition ELF.h:784
@ EF_CUDA_SM87
Definition ELF.h:778
@ EF_CUDA_SM20
Definition ELF.h:761
@ EF_CUDA_SM88
Definition ELF.h:779
@ EF_CUDA_SM80
Definition ELF.h:776
@ EF_CUDA_SM107
Definition ELF.h:785
@ EF_CUDA_SM53
Definition ELF.h:769
@ EF_CUDA_SM70
Definition ELF.h:773
@ EF_CUDA_SM110
Definition ELF.h:786
@ ELFCLASS64
Definition ELF.h:142
@ ELFCLASS32
Definition ELF.h:141
@ ET_REL
Definition ELF.h:119
@ EF_AMDGPU_MACH
Definition ELF.h:660
@ ELFABIVERSION_CUDA_V1
Definition ELF.h:199
@ EF_RISCV_RVC
Definition ELF.h:520
@ SHT_GNU_verneed
Definition ELF.h:1048
@ SHT_GNU_verdef
Definition ELF.h:1047
@ SHT_LLVM_BB_ADDR_MAP
Definition ELF.h:1034
@ SHT_GNU_versym
Definition ELF.h:1049
LLVM_ABI EnumStrings< uint8_t, 2 > getElfSymbolTypes()
Error createError(const Twine &Err)
Definition Error.h:86
std::pair< unsigned char, unsigned char > getElfArchType(StringRef Object)
Definition ELF.h:82
std::string describe(const ELFFile< ELFT > &Obj, const typename ELFT::Shdr &Sec)
Definition ELF.h:147
This is an optimization pass for GlobalISel generic memory operations.
ArrayRef< CharT > arrayRefFromStringRef(StringRef Input)
Construct an array ref of bytes from a string ref.
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:2229
std::string utostr(uint64_t X, bool isNeg=false)
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:204
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:1106
Compile-time data representation of enum entries.
Definition Enum.h:47
static LLVM_ABI const Target * lookupTarget(const Triple &TheTriple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.