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 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 llvm_unreachable("Unknown EF_MIPS_ARCH value");
143 }
144
145 switch (PlatformFlags & ELF::EF_MIPS_MACH) {
147 // No feature associated with this value.
148 break;
150 Features.AddFeature("cnmips");
151 break;
152 default:
153 llvm_unreachable("Unknown EF_MIPS_ARCH value");
154 }
155
156 if (PlatformFlags & ELF::EF_MIPS_ARCH_ASE_M16)
157 Features.AddFeature("mips16");
158 if (PlatformFlags & ELF::EF_MIPS_MICROMIPS)
159 Features.AddFeature("micromips");
160
161 return Features;
162}
163
164SubtargetFeatures ELFObjectFileBase::getARMFeatures() const {
165 SubtargetFeatures Features;
166 ARMAttributeParser Attributes;
168 consumeError(std::move(E));
169 return SubtargetFeatures();
170 }
171
172 // both ARMv7-M and R have to support thumb hardware div
173 bool isV7 = false;
174 std::optional<unsigned> Attr =
175 Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
176 if (Attr)
177 isV7 = *Attr == ARMBuildAttrs::v7;
178
179 Attr = Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile);
180 if (Attr) {
181 switch (*Attr) {
183 Features.AddFeature("aclass");
184 break;
186 Features.AddFeature("rclass");
187 if (isV7)
188 Features.AddFeature("hwdiv");
189 break;
191 Features.AddFeature("mclass");
192 if (isV7)
193 Features.AddFeature("hwdiv");
194 break;
195 }
196 }
197
198 Attr = Attributes.getAttributeValue(ARMBuildAttrs::THUMB_ISA_use);
199 if (Attr) {
200 switch (*Attr) {
201 default:
202 break;
204 Features.AddFeature("thumb", false);
205 Features.AddFeature("thumb2", false);
206 break;
208 Features.AddFeature("thumb2");
209 break;
210 }
211 }
212
213 Attr = Attributes.getAttributeValue(ARMBuildAttrs::FP_arch);
214 if (Attr) {
215 switch (*Attr) {
216 default:
217 break;
219 Features.AddFeature("vfp2sp", false);
220 Features.AddFeature("vfp3d16sp", false);
221 Features.AddFeature("vfp4d16sp", false);
222 break;
224 Features.AddFeature("vfp2");
225 break;
228 Features.AddFeature("vfp3");
229 break;
232 Features.AddFeature("vfp4");
233 break;
234 }
235 }
236
237 Attr = Attributes.getAttributeValue(ARMBuildAttrs::Advanced_SIMD_arch);
238 if (Attr) {
239 switch (*Attr) {
240 default:
241 break;
243 Features.AddFeature("neon", false);
244 Features.AddFeature("fp16", false);
245 break;
247 Features.AddFeature("neon");
248 break;
250 Features.AddFeature("neon");
251 Features.AddFeature("fp16");
252 break;
253 }
254 }
255
256 Attr = Attributes.getAttributeValue(ARMBuildAttrs::MVE_arch);
257 if (Attr) {
258 switch (*Attr) {
259 default:
260 break;
262 Features.AddFeature("mve", false);
263 Features.AddFeature("mve.fp", false);
264 break;
266 Features.AddFeature("mve.fp", false);
267 Features.AddFeature("mve");
268 break;
270 Features.AddFeature("mve.fp");
271 break;
272 }
273 }
274
275 Attr = Attributes.getAttributeValue(ARMBuildAttrs::DIV_use);
276 if (Attr) {
277 switch (*Attr) {
278 default:
279 break;
281 Features.AddFeature("hwdiv", false);
282 Features.AddFeature("hwdiv-arm", false);
283 break;
285 Features.AddFeature("hwdiv");
286 Features.AddFeature("hwdiv-arm");
287 break;
288 }
289 }
290
291 return Features;
292}
293
294static std::string hexagonAttrToFeatureString(unsigned Attr) {
295 return "v" + utostr(Attr);
296}
297
298SubtargetFeatures ELFObjectFileBase::getHexagonFeatures() const {
299 SubtargetFeatures Features;
300 HexagonAttributeParser Parser;
301 if (Error E = getBuildAttributes(Parser)) {
302 // Return no attributes if none can be read.
303 // This behavior is important for backwards compatibility.
304 consumeError(std::move(E));
305 return Features;
306 }
307 std::optional<unsigned> Attr;
308
309 if ((Attr = Parser.getAttributeValue(HexagonAttrs::ARCH)))
310 Features.AddFeature(hexagonAttrToFeatureString(*Attr));
311
312 if ((Attr = Parser.getAttributeValue(HexagonAttrs::HVXARCH)))
313 Features.AddFeature("hvx" + hexagonAttrToFeatureString(*Attr));
314
315 if ((Attr = Parser.getAttributeValue(HexagonAttrs::HVXIEEEFP)))
316 if (*Attr)
317 Features.AddFeature("hvx-ieee-fp");
318
319 if ((Attr = Parser.getAttributeValue(HexagonAttrs::HVXQFLOAT)))
320 if (*Attr)
321 Features.AddFeature("hvx-qfloat");
322
323 if ((Attr = Parser.getAttributeValue(HexagonAttrs::ZREG)))
324 if (*Attr)
325 Features.AddFeature("zreg");
326
327 if ((Attr = Parser.getAttributeValue(HexagonAttrs::AUDIO)))
328 if (*Attr)
329 Features.AddFeature("audio");
330
331 if ((Attr = Parser.getAttributeValue(HexagonAttrs::CABAC)))
332 if (*Attr)
333 Features.AddFeature("cabac");
334
335 return Features;
336}
337
338Expected<SubtargetFeatures> ELFObjectFileBase::getRISCVFeatures() const {
339 SubtargetFeatures Features;
340 unsigned PlatformFlags = getPlatformFlags();
341
342 if (PlatformFlags & ELF::EF_RISCV_RVC) {
343 Features.AddFeature("zca");
344 }
345
346 RISCVAttributeParser Attributes;
348 return std::move(E);
349 }
350
351 std::optional<StringRef> Attr =
352 Attributes.getAttributeString(RISCVAttrs::ARCH);
353 if (Attr) {
354 auto ParseResult = RISCVISAInfo::parseNormalizedArchString(*Attr);
355 if (!ParseResult)
356 return ParseResult.takeError();
357 auto &ISAInfo = *ParseResult;
358
359 if (ISAInfo->getXLen() == 32)
360 Features.AddFeature("64bit", false);
361 else if (ISAInfo->getXLen() == 64)
362 Features.AddFeature("64bit");
363 else
364 llvm_unreachable("XLEN should be 32 or 64.");
365
366 Features.addFeaturesVector(ISAInfo->toFeatures());
367 }
368
369 return Features;
370}
371
372SubtargetFeatures ELFObjectFileBase::getLoongArchFeatures() const {
373 SubtargetFeatures Features;
374
377 break;
379 Features.AddFeature("d");
380 // D implies F according to LoongArch ISA spec.
381 [[fallthrough]];
383 Features.AddFeature("f");
384 break;
385 }
386
387 return Features;
388}
389
391 switch (getEMachine()) {
392 case ELF::EM_MIPS:
393 return getMIPSFeatures();
394 case ELF::EM_ARM:
395 return getARMFeatures();
396 case ELF::EM_RISCV:
397 return getRISCVFeatures();
399 return getLoongArchFeatures();
400 case ELF::EM_HEXAGON:
401 return getHexagonFeatures();
402 default:
403 return SubtargetFeatures();
404 }
405}
406
407std::optional<StringRef> ELFObjectFileBase::tryGetCPUName() const {
408 switch (getEMachine()) {
409 case ELF::EM_AMDGPU:
410 return getAMDGPUCPUName();
411 case ELF::EM_CUDA:
412 return getNVPTXCPUName();
413 case ELF::EM_PPC:
414 case ELF::EM_PPC64:
415 return StringRef("future");
416 case ELF::EM_BPF:
417 return StringRef("v4");
418 default:
419 return std::nullopt;
420 }
421}
422
423StringRef ELFObjectFileBase::getAMDGPUCPUName() const {
425 unsigned CPU = getPlatformFlags() & ELF::EF_AMDGPU_MACH;
426
427 switch (CPU) {
428#define X(NUM, ENUM, NAME) \
429 case ELF::ENUM: \
430 return NAME;
432#undef X
433
434 default:
435 llvm_unreachable("Unknown EF_AMDGPU_MACH value");
436 }
437}
438
439StringRef ELFObjectFileBase::getNVPTXCPUName() const {
445
446 switch (SM) {
447 // Fermi architecture.
449 return "sm_20";
451 return "sm_21";
452
453 // Kepler architecture.
455 return "sm_30";
457 return "sm_32";
459 return "sm_35";
461 return "sm_37";
462
463 // Maxwell architecture.
465 return "sm_50";
467 return "sm_52";
469 return "sm_53";
470
471 // Pascal architecture.
473 return "sm_60";
475 return "sm_61";
477 return "sm_62";
478
479 // Volta architecture.
481 return "sm_70";
483 return "sm_72";
484
485 // Turing architecture.
487 return "sm_75";
488
489 // Ampere architecture.
491 return "sm_80";
493 return "sm_86";
495 return "sm_87";
497 return "sm_88";
498
499 // Ada architecture.
501 return "sm_89";
502
503 // Hopper architecture.
506 : "sm_90";
507
508 // Blackwell architecture.
510 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_100a"
511 : "sm_100";
513 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_101a"
514 : "sm_101";
516 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_103a"
517 : "sm_103";
519 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_110a"
520 : "sm_110";
521
522 // Rubin architecture.
524 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_120a"
525 : "sm_120";
527 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS ? "sm_121a"
528 : "sm_121";
529 default:
530 llvm_unreachable("Unknown EF_CUDA_SM value");
531 }
532}
533
534// FIXME Encode from a tablegen description or target parser.
536 if (TheTriple.getSubArch() != Triple::NoSubArch)
537 return;
538
539 ARMAttributeParser Attributes;
540 if (Error E = getBuildAttributes(Attributes)) {
541 // TODO Propagate Error.
542 consumeError(std::move(E));
543 return;
544 }
545
546 std::string Triple;
547 // Default to ARM, but use the triple if it's been set.
548 if (TheTriple.isThumb())
549 Triple = "thumb";
550 else
551 Triple = "arm";
552
553 std::optional<unsigned> Attr =
554 Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
555 if (Attr) {
556 switch (*Attr) {
558 Triple += "v4";
559 break;
561 Triple += "v4t";
562 break;
564 Triple += "v5t";
565 break;
567 Triple += "v5te";
568 break;
570 Triple += "v5tej";
571 break;
573 Triple += "v6";
574 break;
576 Triple += "v6kz";
577 break;
579 Triple += "v6t2";
580 break;
582 Triple += "v6k";
583 break;
584 case ARMBuildAttrs::v7: {
585 std::optional<unsigned> ArchProfileAttr =
586 Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile);
587 if (ArchProfileAttr == ARMBuildAttrs::MicroControllerProfile)
588 Triple += "v7m";
589 else
590 Triple += "v7";
591 break;
592 }
594 Triple += "v6m";
595 break;
597 Triple += "v6sm";
598 break;
600 Triple += "v7em";
601 break;
603 Triple += "v8a";
604 break;
606 Triple += "v8r";
607 break;
609 Triple += "v8m.base";
610 break;
612 Triple += "v8m.main";
613 break;
615 Triple += "v8.1m.main";
616 break;
618 Triple += "v9a";
619 break;
620 }
621 }
622 if (!isLittleEndian())
623 Triple += "eb";
624
625 TheTriple.setArchName(Triple);
626}
627
628std::vector<ELFPltEntry>
630 std::string Err;
631 const auto Triple = makeTriple();
632 const auto *T = TargetRegistry::lookupTarget(Triple, Err);
633 if (!T)
634 return {};
635 uint32_t JumpSlotReloc = 0, GlobDatReloc = 0;
636 switch (Triple.getArch()) {
637 case Triple::x86:
638 JumpSlotReloc = ELF::R_386_JUMP_SLOT;
639 GlobDatReloc = ELF::R_386_GLOB_DAT;
640 break;
641 case Triple::x86_64:
642 JumpSlotReloc = ELF::R_X86_64_JUMP_SLOT;
643 GlobDatReloc = ELF::R_X86_64_GLOB_DAT;
644 break;
645 case Triple::aarch64:
647 JumpSlotReloc = ELF::R_AARCH64_JUMP_SLOT;
648 break;
649 case Triple::arm:
650 case Triple::armeb:
651 case Triple::thumb:
652 case Triple::thumbeb:
653 JumpSlotReloc = ELF::R_ARM_JUMP_SLOT;
654 break;
655 case Triple::hexagon:
656 JumpSlotReloc = ELF::R_HEX_JMP_SLOT;
657 GlobDatReloc = ELF::R_HEX_GLOB_DAT;
658 break;
659 case Triple::riscv32:
660 case Triple::riscv64:
661 JumpSlotReloc = ELF::R_RISCV_JUMP_SLOT;
662 break;
663 default:
664 return {};
665 }
666 std::unique_ptr<const MCInstrInfo> MII(T->createMCInstrInfo());
667 std::unique_ptr<const MCInstrAnalysis> MIA(
668 T->createMCInstrAnalysis(MII.get()));
669 if (!MIA)
670 return {};
671 std::vector<std::pair<uint64_t, uint64_t>> PltEntries;
672 std::optional<SectionRef> RelaPlt, RelaDyn;
673 uint64_t GotBaseVA = 0;
674 for (const SectionRef &Section : sections()) {
675 Expected<StringRef> NameOrErr = Section.getName();
676 if (!NameOrErr) {
677 consumeError(NameOrErr.takeError());
678 continue;
679 }
680 StringRef Name = *NameOrErr;
681
682 if (Name == ".rela.plt" || Name == ".rel.plt") {
683 RelaPlt = Section;
684 } else if (Name == ".rela.dyn" || Name == ".rel.dyn") {
685 RelaDyn = Section;
686 } else if (Name == ".got.plt") {
687 GotBaseVA = Section.getAddress();
688 } else if (Name == ".plt" || Name == ".plt.got") {
689 Expected<StringRef> PltContents = Section.getContents();
690 if (!PltContents) {
691 consumeError(PltContents.takeError());
692 return {};
693 }
695 PltEntries,
696 MIA->findPltEntries(Section.getAddress(),
697 arrayRefFromStringRef(*PltContents), STI));
698 }
699 }
700
701 // Build a map from GOT entry virtual address to PLT entry virtual address.
703 for (auto [Plt, GotPlt] : PltEntries) {
704 uint64_t GotPltEntry = GotPlt;
705 // An x86-32 PIC PLT uses jmp DWORD PTR [ebx-offset]. Add
706 // _GLOBAL_OFFSET_TABLE_ (EBX) to get the .got.plt (or .got) entry address.
707 // See X86MCTargetDesc.cpp:findPltEntries for the 1 << 32 bit.
708 if (GotPltEntry & (uint64_t(1) << 32) && getEMachine() == ELF::EM_386)
709 GotPltEntry = static_cast<int32_t>(GotPltEntry) + GotBaseVA;
710 GotToPlt.insert(std::make_pair(GotPltEntry, Plt));
711 }
712
713 // Find the relocations in the dynamic relocation table that point to
714 // locations in the GOT for which we know the corresponding PLT entry.
715 std::vector<ELFPltEntry> Result;
716 auto handleRels = [&](iterator_range<relocation_iterator> Rels,
717 uint32_t RelType, StringRef PltSec) {
718 for (const auto &R : Rels) {
719 if (R.getType() != RelType)
720 continue;
721 auto PltEntryIter = GotToPlt.find(R.getOffset());
722 if (PltEntryIter != GotToPlt.end()) {
723 symbol_iterator Sym = R.getSymbol();
724 if (Sym == symbol_end())
725 Result.push_back(
726 ELFPltEntry{PltSec, std::nullopt, PltEntryIter->second});
727 else
728 Result.push_back(ELFPltEntry{PltSec, Sym->getRawDataRefImpl(),
729 PltEntryIter->second});
730 }
731 }
732 };
733
734 if (RelaPlt)
735 handleRels(RelaPlt->relocations(), JumpSlotReloc, ".plt");
736
737 // If a symbol needing a PLT entry also needs a GLOB_DAT relocation, GNU ld's
738 // x86 port places the PLT entry in the .plt.got section.
739 if (RelaDyn)
740 handleRels(RelaDyn->relocations(), GlobDatReloc, ".plt.got");
741
742 return Result;
743}
744
745template <class ELFT>
747 const ELFFile<ELFT> &EF, std::optional<unsigned> TextSectionIndex,
748 std::vector<PGOAnalysisMap> *PGOAnalyses) {
749 using Elf_Shdr = typename ELFT::Shdr;
750 bool IsRelocatable = EF.getHeader().e_type == ELF::ET_REL;
751 std::vector<BBAddrMap> BBAddrMaps;
752 if (PGOAnalyses)
753 PGOAnalyses->clear();
754
755 const auto &Sections = cantFail(EF.sections());
756 auto IsMatch = [&](const Elf_Shdr &Sec) -> Expected<bool> {
757 if (Sec.sh_type != ELF::SHT_LLVM_BB_ADDR_MAP)
758 return false;
759 if (!TextSectionIndex)
760 return true;
761 Expected<const Elf_Shdr *> TextSecOrErr = EF.getSection(Sec.sh_link);
762 if (!TextSecOrErr)
763 return createError("unable to get the linked-to section for " +
764 describe(EF, Sec) + ": " +
765 toString(TextSecOrErr.takeError()));
766 assert(*TextSecOrErr >= Sections.begin() &&
767 "Text section pointer outside of bounds");
768 if (*TextSectionIndex !=
769 (unsigned)std::distance(Sections.begin(), *TextSecOrErr))
770 return false;
771 return true;
772 };
773
775 EF.getSectionAndRelocations(IsMatch);
776 if (!SectionRelocMapOrErr)
777 return SectionRelocMapOrErr.takeError();
778
779 for (auto const &[Sec, RelocSec] : *SectionRelocMapOrErr) {
780 if (IsRelocatable && !RelocSec)
781 return createError("unable to get relocation section for " +
782 describe(EF, *Sec));
783 Expected<std::vector<BBAddrMap>> BBAddrMapOrErr =
784 EF.decodeBBAddrMap(*Sec, RelocSec, PGOAnalyses);
785 if (!BBAddrMapOrErr) {
786 if (PGOAnalyses)
787 PGOAnalyses->clear();
788 return createError("unable to read BB addr map section: " +
789 toString(BBAddrMapOrErr.takeError()));
790 }
791 std::move(BBAddrMapOrErr->begin(), BBAddrMapOrErr->end(),
792 std::back_inserter(BBAddrMaps));
793 }
794 if (PGOAnalyses)
795 assert(PGOAnalyses->size() == BBAddrMaps.size() &&
796 "The same number of BBAddrMaps and PGOAnalysisMaps should be "
797 "returned when PGO information is requested");
798 return BBAddrMaps;
799}
800
801template <class ELFT>
802static Expected<std::vector<VersionEntry>>
805 using Elf_Shdr = typename ELFT::Shdr;
806 const Elf_Shdr *VerSec = nullptr;
807 const Elf_Shdr *VerNeedSec = nullptr;
808 const Elf_Shdr *VerDefSec = nullptr;
809 // The user should ensure sections() can't fail here.
810 for (const Elf_Shdr &Sec : cantFail(EF.sections())) {
811 if (Sec.sh_type == ELF::SHT_GNU_versym)
812 VerSec = &Sec;
813 else if (Sec.sh_type == ELF::SHT_GNU_verdef)
814 VerDefSec = &Sec;
815 else if (Sec.sh_type == ELF::SHT_GNU_verneed)
816 VerNeedSec = &Sec;
817 }
818 if (!VerSec)
819 return std::vector<VersionEntry>();
820
822 EF.loadVersionMap(VerNeedSec, VerDefSec);
823 if (!MapOrErr)
824 return MapOrErr.takeError();
825
826 std::vector<VersionEntry> Ret;
827 size_t I = 0;
828 for (const ELFSymbolRef &Sym : Symbols) {
829 ++I;
831 EF.template getEntry<typename ELFT::Versym>(*VerSec, I);
832 if (!VerEntryOrErr)
833 return createError("unable to read an entry with index " + Twine(I) +
834 " from " + describe(EF, *VerSec) + ": " +
835 toString(VerEntryOrErr.takeError()));
836
837 Expected<uint32_t> FlagsOrErr = Sym.getFlags();
838 if (!FlagsOrErr)
839 return createError("unable to read flags for symbol with index " +
840 Twine(I) + ": " + toString(FlagsOrErr.takeError()));
841
842 bool IsDefault;
844 (*VerEntryOrErr)->vs_index, IsDefault, *MapOrErr,
845 (*FlagsOrErr) & SymbolRef::SF_Undefined);
846 if (!VerOrErr)
847 return createError("unable to get a version for entry " + Twine(I) +
848 " of " + describe(EF, *VerSec) + ": " +
849 toString(VerOrErr.takeError()));
850
851 Ret.push_back({(*VerOrErr).str(), IsDefault});
852 }
853
854 return Ret;
855}
856
857Expected<std::vector<VersionEntry>>
860 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
861 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
862 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
863 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
864 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
865 return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
866 return readDynsymVersionsImpl(cast<ELF64BEObjectFile>(this)->getELFFile(),
867 Symbols);
868}
869
871 std::optional<unsigned> TextSectionIndex,
872 std::vector<PGOAnalysisMap> *PGOAnalyses) const {
873 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
874 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
875 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
876 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
877 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
878 return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex, PGOAnalyses);
879 return readBBAddrMapImpl(cast<ELF64BEObjectFile>(this)->getELFFile(),
880 TextSectionIndex, PGOAnalyses);
881}
882
884 auto Data = Sec.getRawDataRefImpl();
885 if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
886 return Obj->getCrelDecodeProblem(Data);
887 if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
888 return Obj->getCrelDecodeProblem(Data);
889 if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
890 return Obj->getCrelDecodeProblem(Data);
891 return cast<ELF64BEObjectFile>(this)->getCrelDecodeProblem(Data);
892}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Kernel Attributes
#define X(NUM, ENUM, NAME)
Definition ELF.h:856
#define AMDGPU_MACH_LIST(X)
Definition ELF.h:768
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
static constexpr unsigned SM(unsigned Version)
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:225
iterator end()
Definition DenseMap.h:143
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:286
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...
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:47
LLVM_ABI void setArchName(StringRef Str)
Set the architecture (first) component of the triple by name.
Definition Triple.cpp:1954
bool isThumb() const
Tests whether the target is Thumb (little and big endian).
Definition Triple.h:908
SubArchType getSubArch() const
get the parsed subarchitecture type for this triple.
Definition Triple.h:428
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition Triple.h:425
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:840
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:853
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.
@ 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:1206
@ SHT_GNU_verdef
Definition ELF.h:1205
@ SHT_LLVM_BB_ADDR_MAP
Definition ELF.h:1193
@ SHT_GNU_versym
Definition ELF.h:1207
@ 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:1076
@ EF_LOONGARCH_ABI_DOUBLE_FLOAT
Definition ELF.h:1077
@ EF_LOONGARCH_ABI_SOFT_FLOAT
Definition ELF.h:1075
@ EF_LOONGARCH_ABI_MODIFIER_MASK
Definition ELF.h:1078
@ STT_FUNC
Definition ELF.h:1426
@ STT_NOTYPE
Definition ELF.h:1424
@ STT_SECTION
Definition ELF.h:1427
@ STT_FILE
Definition ELF.h:1428
@ STT_COMMON
Definition ELF.h:1429
@ STT_GNU_IFUNC
Definition ELF.h:1431
@ STT_OBJECT
Definition ELF.h:1425
@ STT_TLS
Definition ELF.h:1430
@ EF_CUDA_SM21
Definition ELF.h:953
@ EF_CUDA_SM90
Definition ELF.h:972
@ EF_CUDA_SM86
Definition ELF.h:968
@ EF_CUDA_SM60
Definition ELF.h:961
@ EF_CUDA_SM
Definition ELF.h:943
@ EF_CUDA_SM89
Definition ELF.h:971
@ EF_CUDA_SM37
Definition ELF.h:957
@ EF_CUDA_SM32
Definition ELF.h:955
@ EF_CUDA_SM72
Definition ELF.h:965
@ EF_CUDA_SM50
Definition ELF.h:958
@ EF_CUDA_ACCELERATORS
Definition ELF.h:995
@ EF_CUDA_SM121
Definition ELF.h:978
@ EF_CUDA_SM61
Definition ELF.h:962
@ EF_CUDA_SM_MASK
Definition ELF.h:946
@ EF_CUDA_SM52
Definition ELF.h:959
@ EF_CUDA_SM35
Definition ELF.h:956
@ EF_CUDA_SM120
Definition ELF.h:977
@ EF_CUDA_SM100
Definition ELF.h:973
@ EF_CUDA_SM62
Definition ELF.h:963
@ EF_CUDA_SM101
Definition ELF.h:974
@ EF_CUDA_SM30
Definition ELF.h:954
@ EF_CUDA_SM_OFFSET
Definition ELF.h:949
@ EF_CUDA_ACCELERATORS_V1
Definition ELF.h:987
@ EF_CUDA_SM75
Definition ELF.h:966
@ EF_CUDA_SM103
Definition ELF.h:975
@ EF_CUDA_SM87
Definition ELF.h:969
@ EF_CUDA_SM20
Definition ELF.h:952
@ EF_CUDA_SM88
Definition ELF.h:970
@ EF_CUDA_SM80
Definition ELF.h:967
@ EF_CUDA_SM53
Definition ELF.h:960
@ EF_CUDA_SM70
Definition ELF.h:964
@ EF_CUDA_SM110
Definition ELF.h:976
@ ELFCLASS64
Definition ELF.h:334
@ ELFCLASS32
Definition ELF.h:333
@ ET_REL
Definition ELF.h:119
@ EF_AMDGPU_MACH
Definition ELF.h:851
@ ELFABIVERSION_CUDA_V1
Definition ELF.h:391
@ EF_RISCV_RVC
Definition ELF.h:712
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:2208
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.