1 //===- ELFObjectFile.cpp - ELF object file implementation -----------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Part of the ELFObjectFile class implementation.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Object/ELFObjectFile.h"
14 #include "llvm/BinaryFormat/ELF.h"
15 #include "llvm/MC/MCInstrAnalysis.h"
16 #include "llvm/MC/TargetRegistry.h"
17 #include "llvm/Object/ELF.h"
18 #include "llvm/Object/ELFTypes.h"
19 #include "llvm/Object/Error.h"
20 #include "llvm/Support/ARMAttributeParser.h"
21 #include "llvm/Support/ARMBuildAttributes.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/HexagonAttributeParser.h"
24 #include "llvm/Support/RISCVAttributeParser.h"
25 #include "llvm/Support/RISCVAttributes.h"
26 #include "llvm/TargetParser/RISCVISAInfo.h"
27 #include "llvm/TargetParser/SubtargetFeature.h"
28 #include "llvm/TargetParser/Triple.h"
38 using namespace object
;
40 const EnumEntry
<unsigned> llvm::object::ElfSymbolTypes
[NumElfSymbolTypes
] = {
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}
59 ELFObjectFileBase::ELFObjectFileBase(unsigned int Type
, MemoryBufferRef Source
)
60 : ObjectFile(Type
, Source
) {}
63 static Expected
<std::unique_ptr
<ELFObjectFile
<ELFT
>>>
64 createPtr(MemoryBufferRef Object
, bool InitContent
) {
65 auto Ret
= ELFObjectFile
<ELFT
>::create(Object
, InitContent
);
66 if (Error E
= Ret
.takeError())
68 return std::make_unique
<ELFObjectFile
<ELFT
>>(std::move(*Ret
));
71 Expected
<std::unique_ptr
<ObjectFile
>>
72 ObjectFile::createELFObjectFile(MemoryBufferRef Obj
, bool InitContent
) {
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()));
80 return createError("Insufficient alignment");
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
);
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
);
95 return createError("Invalid ELF data");
97 return createError("Invalid ELF class");
100 SubtargetFeatures
ELFObjectFileBase::getMIPSFeatures() const {
101 SubtargetFeatures Features
;
102 unsigned PlatformFlags
= getPlatformFlags();
104 switch (PlatformFlags
& ELF::EF_MIPS_ARCH
) {
105 case ELF::EF_MIPS_ARCH_1
:
107 case ELF::EF_MIPS_ARCH_2
:
108 Features
.AddFeature("mips2");
110 case ELF::EF_MIPS_ARCH_3
:
111 Features
.AddFeature("mips3");
113 case ELF::EF_MIPS_ARCH_4
:
114 Features
.AddFeature("mips4");
116 case ELF::EF_MIPS_ARCH_5
:
117 Features
.AddFeature("mips5");
119 case ELF::EF_MIPS_ARCH_32
:
120 Features
.AddFeature("mips32");
122 case ELF::EF_MIPS_ARCH_64
:
123 Features
.AddFeature("mips64");
125 case ELF::EF_MIPS_ARCH_32R2
:
126 Features
.AddFeature("mips32r2");
128 case ELF::EF_MIPS_ARCH_64R2
:
129 Features
.AddFeature("mips64r2");
131 case ELF::EF_MIPS_ARCH_32R6
:
132 Features
.AddFeature("mips32r6");
134 case ELF::EF_MIPS_ARCH_64R6
:
135 Features
.AddFeature("mips64r6");
138 llvm_unreachable("Unknown EF_MIPS_ARCH value");
141 switch (PlatformFlags
& ELF::EF_MIPS_MACH
) {
142 case ELF::EF_MIPS_MACH_NONE
:
143 // No feature associated with this value.
145 case ELF::EF_MIPS_MACH_OCTEON
:
146 Features
.AddFeature("cnmips");
149 llvm_unreachable("Unknown EF_MIPS_ARCH value");
152 if (PlatformFlags
& ELF::EF_MIPS_ARCH_ASE_M16
)
153 Features
.AddFeature("mips16");
154 if (PlatformFlags
& ELF::EF_MIPS_MICROMIPS
)
155 Features
.AddFeature("micromips");
160 SubtargetFeatures
ELFObjectFileBase::getARMFeatures() const {
161 SubtargetFeatures Features
;
162 ARMAttributeParser Attributes
;
163 if (Error E
= getBuildAttributes(Attributes
)) {
164 consumeError(std::move(E
));
165 return SubtargetFeatures();
168 // both ARMv7-M and R have to support thumb hardware div
170 std::optional
<unsigned> Attr
=
171 Attributes
.getAttributeValue(ARMBuildAttrs::CPU_arch
);
173 isV7
= *Attr
== ARMBuildAttrs::v7
;
175 Attr
= Attributes
.getAttributeValue(ARMBuildAttrs::CPU_arch_profile
);
178 case ARMBuildAttrs::ApplicationProfile
:
179 Features
.AddFeature("aclass");
181 case ARMBuildAttrs::RealTimeProfile
:
182 Features
.AddFeature("rclass");
184 Features
.AddFeature("hwdiv");
186 case ARMBuildAttrs::MicroControllerProfile
:
187 Features
.AddFeature("mclass");
189 Features
.AddFeature("hwdiv");
194 Attr
= Attributes
.getAttributeValue(ARMBuildAttrs::THUMB_ISA_use
);
199 case ARMBuildAttrs::Not_Allowed
:
200 Features
.AddFeature("thumb", false);
201 Features
.AddFeature("thumb2", false);
203 case ARMBuildAttrs::AllowThumb32
:
204 Features
.AddFeature("thumb2");
209 Attr
= Attributes
.getAttributeValue(ARMBuildAttrs::FP_arch
);
214 case ARMBuildAttrs::Not_Allowed
:
215 Features
.AddFeature("vfp2sp", false);
216 Features
.AddFeature("vfp3d16sp", false);
217 Features
.AddFeature("vfp4d16sp", false);
219 case ARMBuildAttrs::AllowFPv2
:
220 Features
.AddFeature("vfp2");
222 case ARMBuildAttrs::AllowFPv3A
:
223 case ARMBuildAttrs::AllowFPv3B
:
224 Features
.AddFeature("vfp3");
226 case ARMBuildAttrs::AllowFPv4A
:
227 case ARMBuildAttrs::AllowFPv4B
:
228 Features
.AddFeature("vfp4");
233 Attr
= Attributes
.getAttributeValue(ARMBuildAttrs::Advanced_SIMD_arch
);
238 case ARMBuildAttrs::Not_Allowed
:
239 Features
.AddFeature("neon", false);
240 Features
.AddFeature("fp16", false);
242 case ARMBuildAttrs::AllowNeon
:
243 Features
.AddFeature("neon");
245 case ARMBuildAttrs::AllowNeon2
:
246 Features
.AddFeature("neon");
247 Features
.AddFeature("fp16");
252 Attr
= Attributes
.getAttributeValue(ARMBuildAttrs::MVE_arch
);
257 case ARMBuildAttrs::Not_Allowed
:
258 Features
.AddFeature("mve", false);
259 Features
.AddFeature("mve.fp", false);
261 case ARMBuildAttrs::AllowMVEInteger
:
262 Features
.AddFeature("mve.fp", false);
263 Features
.AddFeature("mve");
265 case ARMBuildAttrs::AllowMVEIntegerAndFloat
:
266 Features
.AddFeature("mve.fp");
271 Attr
= Attributes
.getAttributeValue(ARMBuildAttrs::DIV_use
);
276 case ARMBuildAttrs::DisallowDIV
:
277 Features
.AddFeature("hwdiv", false);
278 Features
.AddFeature("hwdiv-arm", false);
280 case ARMBuildAttrs::AllowDIVExt
:
281 Features
.AddFeature("hwdiv");
282 Features
.AddFeature("hwdiv-arm");
290 static std::optional
<std::string
> hexagonAttrToFeatureString(unsigned Attr
) {
319 SubtargetFeatures
ELFObjectFileBase::getHexagonFeatures() const {
320 SubtargetFeatures Features
;
321 HexagonAttributeParser Parser
;
322 if (Error E
= getBuildAttributes(Parser
)) {
323 // Return no attributes if none can be read.
324 // This behavior is important for backwards compatibility.
325 consumeError(std::move(E
));
328 std::optional
<unsigned> Attr
;
330 if ((Attr
= Parser
.getAttributeValue(HexagonAttrs::ARCH
))) {
331 if (std::optional
<std::string
> FeatureString
=
332 hexagonAttrToFeatureString(*Attr
))
333 Features
.AddFeature(*FeatureString
);
336 if ((Attr
= Parser
.getAttributeValue(HexagonAttrs::HVXARCH
))) {
337 std::optional
<std::string
> FeatureString
=
338 hexagonAttrToFeatureString(*Attr
);
339 // There is no corresponding hvx arch for v5 and v55.
340 if (FeatureString
&& *Attr
>= 60)
341 Features
.AddFeature("hvx" + *FeatureString
);
344 if ((Attr
= Parser
.getAttributeValue(HexagonAttrs::HVXIEEEFP
)))
346 Features
.AddFeature("hvx-ieee-fp");
348 if ((Attr
= Parser
.getAttributeValue(HexagonAttrs::HVXQFLOAT
)))
350 Features
.AddFeature("hvx-qfloat");
352 if ((Attr
= Parser
.getAttributeValue(HexagonAttrs::ZREG
)))
354 Features
.AddFeature("zreg");
356 if ((Attr
= Parser
.getAttributeValue(HexagonAttrs::AUDIO
)))
358 Features
.AddFeature("audio");
360 if ((Attr
= Parser
.getAttributeValue(HexagonAttrs::CABAC
)))
362 Features
.AddFeature("cabac");
367 Expected
<SubtargetFeatures
> ELFObjectFileBase::getRISCVFeatures() const {
368 SubtargetFeatures Features
;
369 unsigned PlatformFlags
= getPlatformFlags();
371 if (PlatformFlags
& ELF::EF_RISCV_RVC
) {
372 Features
.AddFeature("zca");
375 RISCVAttributeParser Attributes
;
376 if (Error E
= getBuildAttributes(Attributes
)) {
380 std::optional
<StringRef
> Attr
=
381 Attributes
.getAttributeString(RISCVAttrs::ARCH
);
383 auto ParseResult
= RISCVISAInfo::parseNormalizedArchString(*Attr
);
385 return ParseResult
.takeError();
386 auto &ISAInfo
= *ParseResult
;
388 if (ISAInfo
->getXLen() == 32)
389 Features
.AddFeature("64bit", false);
390 else if (ISAInfo
->getXLen() == 64)
391 Features
.AddFeature("64bit");
393 llvm_unreachable("XLEN should be 32 or 64.");
395 Features
.addFeaturesVector(ISAInfo
->toFeatures());
401 SubtargetFeatures
ELFObjectFileBase::getLoongArchFeatures() const {
402 SubtargetFeatures Features
;
404 switch (getPlatformFlags() & ELF::EF_LOONGARCH_ABI_MODIFIER_MASK
) {
405 case ELF::EF_LOONGARCH_ABI_SOFT_FLOAT
:
407 case ELF::EF_LOONGARCH_ABI_DOUBLE_FLOAT
:
408 Features
.AddFeature("d");
409 // D implies F according to LoongArch ISA spec.
411 case ELF::EF_LOONGARCH_ABI_SINGLE_FLOAT
:
412 Features
.AddFeature("f");
419 Expected
<SubtargetFeatures
> ELFObjectFileBase::getFeatures() const {
420 switch (getEMachine()) {
422 return getMIPSFeatures();
424 return getARMFeatures();
426 return getRISCVFeatures();
427 case ELF::EM_LOONGARCH
:
428 return getLoongArchFeatures();
429 case ELF::EM_HEXAGON
:
430 return getHexagonFeatures();
432 return SubtargetFeatures();
436 std::optional
<StringRef
> ELFObjectFileBase::tryGetCPUName() const {
437 switch (getEMachine()) {
439 return getAMDGPUCPUName();
441 return getNVPTXCPUName();
444 return StringRef("future");
446 return StringRef("v4");
452 StringRef
ELFObjectFileBase::getAMDGPUCPUName() const {
453 assert(getEMachine() == ELF::EM_AMDGPU
);
454 unsigned CPU
= getPlatformFlags() & ELF::EF_AMDGPU_MACH
;
457 // Radeon HD 2000/3000 Series (R600).
458 case ELF::EF_AMDGPU_MACH_R600_R600
:
460 case ELF::EF_AMDGPU_MACH_R600_R630
:
462 case ELF::EF_AMDGPU_MACH_R600_RS880
:
464 case ELF::EF_AMDGPU_MACH_R600_RV670
:
467 // Radeon HD 4000 Series (R700).
468 case ELF::EF_AMDGPU_MACH_R600_RV710
:
470 case ELF::EF_AMDGPU_MACH_R600_RV730
:
472 case ELF::EF_AMDGPU_MACH_R600_RV770
:
475 // Radeon HD 5000 Series (Evergreen).
476 case ELF::EF_AMDGPU_MACH_R600_CEDAR
:
478 case ELF::EF_AMDGPU_MACH_R600_CYPRESS
:
480 case ELF::EF_AMDGPU_MACH_R600_JUNIPER
:
482 case ELF::EF_AMDGPU_MACH_R600_REDWOOD
:
484 case ELF::EF_AMDGPU_MACH_R600_SUMO
:
487 // Radeon HD 6000 Series (Northern Islands).
488 case ELF::EF_AMDGPU_MACH_R600_BARTS
:
490 case ELF::EF_AMDGPU_MACH_R600_CAICOS
:
492 case ELF::EF_AMDGPU_MACH_R600_CAYMAN
:
494 case ELF::EF_AMDGPU_MACH_R600_TURKS
:
498 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX600
:
500 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX601
:
502 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX602
:
506 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX700
:
508 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX701
:
510 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX702
:
512 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX703
:
514 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX704
:
516 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX705
:
520 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX801
:
522 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX802
:
524 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX803
:
526 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX805
:
528 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX810
:
532 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX900
:
534 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX902
:
536 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX904
:
538 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX906
:
540 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX908
:
542 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX909
:
544 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX90A
:
546 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX90C
:
548 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX940
:
550 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX941
:
552 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX942
:
554 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX950
:
558 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1010
:
560 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1011
:
562 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1012
:
564 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1013
:
566 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1030
:
568 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1031
:
570 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1032
:
572 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1033
:
574 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1034
:
576 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1035
:
578 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1036
:
582 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1100
:
584 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1101
:
586 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1102
:
588 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1103
:
590 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1150
:
592 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1151
:
594 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1152
:
596 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1153
:
600 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1200
:
602 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1201
:
605 // Generic AMDGCN targets
606 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX9_GENERIC
:
607 return "gfx9-generic";
608 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX9_4_GENERIC
:
609 return "gfx9-4-generic";
610 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX10_1_GENERIC
:
611 return "gfx10-1-generic";
612 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX10_3_GENERIC
:
613 return "gfx10-3-generic";
614 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX11_GENERIC
:
615 return "gfx11-generic";
616 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX12_GENERIC
:
617 return "gfx12-generic";
619 llvm_unreachable("Unknown EF_AMDGPU_MACH value");
623 StringRef
ELFObjectFileBase::getNVPTXCPUName() const {
624 assert(getEMachine() == ELF::EM_CUDA
);
625 unsigned SM
= getPlatformFlags() & ELF::EF_CUDA_SM
;
628 // Fermi architecture.
629 case ELF::EF_CUDA_SM20
:
631 case ELF::EF_CUDA_SM21
:
634 // Kepler architecture.
635 case ELF::EF_CUDA_SM30
:
637 case ELF::EF_CUDA_SM32
:
639 case ELF::EF_CUDA_SM35
:
641 case ELF::EF_CUDA_SM37
:
644 // Maxwell architecture.
645 case ELF::EF_CUDA_SM50
:
647 case ELF::EF_CUDA_SM52
:
649 case ELF::EF_CUDA_SM53
:
652 // Pascal architecture.
653 case ELF::EF_CUDA_SM60
:
655 case ELF::EF_CUDA_SM61
:
657 case ELF::EF_CUDA_SM62
:
660 // Volta architecture.
661 case ELF::EF_CUDA_SM70
:
663 case ELF::EF_CUDA_SM72
:
666 // Turing architecture.
667 case ELF::EF_CUDA_SM75
:
670 // Ampere architecture.
671 case ELF::EF_CUDA_SM80
:
673 case ELF::EF_CUDA_SM86
:
675 case ELF::EF_CUDA_SM87
:
679 case ELF::EF_CUDA_SM89
:
682 // Hopper architecture.
683 case ELF::EF_CUDA_SM90
:
684 return getPlatformFlags() & ELF::EF_CUDA_ACCELERATORS
? "sm_90a" : "sm_90";
686 llvm_unreachable("Unknown EF_CUDA_SM value");
690 // FIXME Encode from a tablegen description or target parser.
691 void ELFObjectFileBase::setARMSubArch(Triple
&TheTriple
) const {
692 if (TheTriple
.getSubArch() != Triple::NoSubArch
)
695 ARMAttributeParser Attributes
;
696 if (Error E
= getBuildAttributes(Attributes
)) {
697 // TODO Propagate Error.
698 consumeError(std::move(E
));
703 // Default to ARM, but use the triple if it's been set.
704 if (TheTriple
.isThumb())
709 std::optional
<unsigned> Attr
=
710 Attributes
.getAttributeValue(ARMBuildAttrs::CPU_arch
);
713 case ARMBuildAttrs::v4
:
716 case ARMBuildAttrs::v4T
:
719 case ARMBuildAttrs::v5T
:
722 case ARMBuildAttrs::v5TE
:
725 case ARMBuildAttrs::v5TEJ
:
728 case ARMBuildAttrs::v6
:
731 case ARMBuildAttrs::v6KZ
:
734 case ARMBuildAttrs::v6T2
:
737 case ARMBuildAttrs::v6K
:
740 case ARMBuildAttrs::v7
: {
741 std::optional
<unsigned> ArchProfileAttr
=
742 Attributes
.getAttributeValue(ARMBuildAttrs::CPU_arch_profile
);
743 if (ArchProfileAttr
&&
744 *ArchProfileAttr
== ARMBuildAttrs::MicroControllerProfile
)
750 case ARMBuildAttrs::v6_M
:
753 case ARMBuildAttrs::v6S_M
:
756 case ARMBuildAttrs::v7E_M
:
759 case ARMBuildAttrs::v8_A
:
762 case ARMBuildAttrs::v8_R
:
765 case ARMBuildAttrs::v8_M_Base
:
766 Triple
+= "v8m.base";
768 case ARMBuildAttrs::v8_M_Main
:
769 Triple
+= "v8m.main";
771 case ARMBuildAttrs::v8_1_M_Main
:
772 Triple
+= "v8.1m.main";
774 case ARMBuildAttrs::v9_A
:
779 if (!isLittleEndian())
782 TheTriple
.setArchName(Triple
);
785 std::vector
<ELFPltEntry
> ELFObjectFileBase::getPltEntries() const {
787 const auto Triple
= makeTriple();
788 const auto *T
= TargetRegistry::lookupTarget(Triple
.str(), Err
);
791 uint32_t JumpSlotReloc
= 0, GlobDatReloc
= 0;
792 switch (Triple
.getArch()) {
794 JumpSlotReloc
= ELF::R_386_JUMP_SLOT
;
795 GlobDatReloc
= ELF::R_386_GLOB_DAT
;
798 JumpSlotReloc
= ELF::R_X86_64_JUMP_SLOT
;
799 GlobDatReloc
= ELF::R_X86_64_GLOB_DAT
;
801 case Triple::aarch64
:
802 case Triple::aarch64_be
:
803 JumpSlotReloc
= ELF::R_AARCH64_JUMP_SLOT
;
808 std::unique_ptr
<const MCInstrInfo
> MII(T
->createMCInstrInfo());
809 std::unique_ptr
<const MCInstrAnalysis
> MIA(
810 T
->createMCInstrAnalysis(MII
.get()));
813 std::vector
<std::pair
<uint64_t, uint64_t>> PltEntries
;
814 std::optional
<SectionRef
> RelaPlt
, RelaDyn
;
815 uint64_t GotBaseVA
= 0;
816 for (const SectionRef
&Section
: sections()) {
817 Expected
<StringRef
> NameOrErr
= Section
.getName();
819 consumeError(NameOrErr
.takeError());
822 StringRef Name
= *NameOrErr
;
824 if (Name
== ".rela.plt" || Name
== ".rel.plt") {
826 } else if (Name
== ".rela.dyn" || Name
== ".rel.dyn") {
828 } else if (Name
== ".got.plt") {
829 GotBaseVA
= Section
.getAddress();
830 } else if (Name
== ".plt" || Name
== ".plt.got") {
831 Expected
<StringRef
> PltContents
= Section
.getContents();
833 consumeError(PltContents
.takeError());
838 MIA
->findPltEntries(Section
.getAddress(),
839 arrayRefFromStringRef(*PltContents
), Triple
));
843 // Build a map from GOT entry virtual address to PLT entry virtual address.
844 DenseMap
<uint64_t, uint64_t> GotToPlt
;
845 for (auto [Plt
, GotPlt
] : PltEntries
) {
846 uint64_t GotPltEntry
= GotPlt
;
847 // An x86-32 PIC PLT uses jmp DWORD PTR [ebx-offset]. Add
848 // _GLOBAL_OFFSET_TABLE_ (EBX) to get the .got.plt (or .got) entry address.
849 // See X86MCTargetDesc.cpp:findPltEntries for the 1 << 32 bit.
850 if (GotPltEntry
& (uint64_t(1) << 32) && getEMachine() == ELF::EM_386
)
851 GotPltEntry
= static_cast<int32_t>(GotPltEntry
) + GotBaseVA
;
852 GotToPlt
.insert(std::make_pair(GotPltEntry
, Plt
));
855 // Find the relocations in the dynamic relocation table that point to
856 // locations in the GOT for which we know the corresponding PLT entry.
857 std::vector
<ELFPltEntry
> Result
;
858 auto handleRels
= [&](iterator_range
<relocation_iterator
> Rels
,
859 uint32_t RelType
, StringRef PltSec
) {
860 for (const auto &R
: Rels
) {
861 if (R
.getType() != RelType
)
863 auto PltEntryIter
= GotToPlt
.find(R
.getOffset());
864 if (PltEntryIter
!= GotToPlt
.end()) {
865 symbol_iterator Sym
= R
.getSymbol();
866 if (Sym
== symbol_end())
868 ELFPltEntry
{PltSec
, std::nullopt
, PltEntryIter
->second
});
870 Result
.push_back(ELFPltEntry
{PltSec
, Sym
->getRawDataRefImpl(),
871 PltEntryIter
->second
});
877 handleRels(RelaPlt
->relocations(), JumpSlotReloc
, ".plt");
879 // If a symbol needing a PLT entry also needs a GLOB_DAT relocation, GNU ld's
880 // x86 port places the PLT entry in the .plt.got section.
882 handleRels(RelaDyn
->relocations(), GlobDatReloc
, ".plt.got");
887 template <class ELFT
>
888 Expected
<std::vector
<BBAddrMap
>> static readBBAddrMapImpl(
889 const ELFFile
<ELFT
> &EF
, std::optional
<unsigned> TextSectionIndex
,
890 std::vector
<PGOAnalysisMap
> *PGOAnalyses
) {
891 using Elf_Shdr
= typename
ELFT::Shdr
;
892 bool IsRelocatable
= EF
.getHeader().e_type
== ELF::ET_REL
;
893 std::vector
<BBAddrMap
> BBAddrMaps
;
895 PGOAnalyses
->clear();
897 const auto &Sections
= cantFail(EF
.sections());
898 auto IsMatch
= [&](const Elf_Shdr
&Sec
) -> Expected
<bool> {
899 if (Sec
.sh_type
!= ELF::SHT_LLVM_BB_ADDR_MAP
&&
900 Sec
.sh_type
!= ELF::SHT_LLVM_BB_ADDR_MAP_V0
)
902 if (!TextSectionIndex
)
904 Expected
<const Elf_Shdr
*> TextSecOrErr
= EF
.getSection(Sec
.sh_link
);
906 return createError("unable to get the linked-to section for " +
907 describe(EF
, Sec
) + ": " +
908 toString(TextSecOrErr
.takeError()));
909 assert(*TextSecOrErr
>= Sections
.begin() &&
910 "Text section pointer outside of bounds");
911 if (*TextSectionIndex
!=
912 (unsigned)std::distance(Sections
.begin(), *TextSecOrErr
))
917 Expected
<MapVector
<const Elf_Shdr
*, const Elf_Shdr
*>> SectionRelocMapOrErr
=
918 EF
.getSectionAndRelocations(IsMatch
);
919 if (!SectionRelocMapOrErr
)
920 return SectionRelocMapOrErr
.takeError();
922 for (auto const &[Sec
, RelocSec
] : *SectionRelocMapOrErr
) {
923 if (IsRelocatable
&& !RelocSec
)
924 return createError("unable to get relocation section for " +
926 Expected
<std::vector
<BBAddrMap
>> BBAddrMapOrErr
=
927 EF
.decodeBBAddrMap(*Sec
, RelocSec
, PGOAnalyses
);
928 if (!BBAddrMapOrErr
) {
930 PGOAnalyses
->clear();
931 return createError("unable to read " + describe(EF
, *Sec
) + ": " +
932 toString(BBAddrMapOrErr
.takeError()));
934 std::move(BBAddrMapOrErr
->begin(), BBAddrMapOrErr
->end(),
935 std::back_inserter(BBAddrMaps
));
938 assert(PGOAnalyses
->size() == BBAddrMaps
.size() &&
939 "The same number of BBAddrMaps and PGOAnalysisMaps should be "
940 "returned when PGO information is requested");
944 template <class ELFT
>
945 static Expected
<std::vector
<VersionEntry
>>
946 readDynsymVersionsImpl(const ELFFile
<ELFT
> &EF
,
947 ELFObjectFileBase::elf_symbol_iterator_range Symbols
) {
948 using Elf_Shdr
= typename
ELFT::Shdr
;
949 const Elf_Shdr
*VerSec
= nullptr;
950 const Elf_Shdr
*VerNeedSec
= nullptr;
951 const Elf_Shdr
*VerDefSec
= nullptr;
952 // The user should ensure sections() can't fail here.
953 for (const Elf_Shdr
&Sec
: cantFail(EF
.sections())) {
954 if (Sec
.sh_type
== ELF::SHT_GNU_versym
)
956 else if (Sec
.sh_type
== ELF::SHT_GNU_verdef
)
958 else if (Sec
.sh_type
== ELF::SHT_GNU_verneed
)
962 return std::vector
<VersionEntry
>();
964 Expected
<SmallVector
<std::optional
<VersionEntry
>, 0>> MapOrErr
=
965 EF
.loadVersionMap(VerNeedSec
, VerDefSec
);
967 return MapOrErr
.takeError();
969 std::vector
<VersionEntry
> Ret
;
971 for (const ELFSymbolRef
&Sym
: Symbols
) {
973 Expected
<const typename
ELFT::Versym
*> VerEntryOrErr
=
974 EF
.template getEntry
<typename
ELFT::Versym
>(*VerSec
, I
);
976 return createError("unable to read an entry with index " + Twine(I
) +
977 " from " + describe(EF
, *VerSec
) + ": " +
978 toString(VerEntryOrErr
.takeError()));
980 Expected
<uint32_t> FlagsOrErr
= Sym
.getFlags();
982 return createError("unable to read flags for symbol with index " +
983 Twine(I
) + ": " + toString(FlagsOrErr
.takeError()));
986 Expected
<StringRef
> VerOrErr
= EF
.getSymbolVersionByIndex(
987 (*VerEntryOrErr
)->vs_index
, IsDefault
, *MapOrErr
,
988 (*FlagsOrErr
) & SymbolRef::SF_Undefined
);
990 return createError("unable to get a version for entry " + Twine(I
) +
991 " of " + describe(EF
, *VerSec
) + ": " +
992 toString(VerOrErr
.takeError()));
994 Ret
.push_back({(*VerOrErr
).str(), IsDefault
});
1000 Expected
<std::vector
<VersionEntry
>>
1001 ELFObjectFileBase::readDynsymVersions() const {
1002 elf_symbol_iterator_range Symbols
= getDynamicSymbolIterators();
1003 if (const auto *Obj
= dyn_cast
<ELF32LEObjectFile
>(this))
1004 return readDynsymVersionsImpl(Obj
->getELFFile(), Symbols
);
1005 if (const auto *Obj
= dyn_cast
<ELF32BEObjectFile
>(this))
1006 return readDynsymVersionsImpl(Obj
->getELFFile(), Symbols
);
1007 if (const auto *Obj
= dyn_cast
<ELF64LEObjectFile
>(this))
1008 return readDynsymVersionsImpl(Obj
->getELFFile(), Symbols
);
1009 return readDynsymVersionsImpl(cast
<ELF64BEObjectFile
>(this)->getELFFile(),
1013 Expected
<std::vector
<BBAddrMap
>> ELFObjectFileBase::readBBAddrMap(
1014 std::optional
<unsigned> TextSectionIndex
,
1015 std::vector
<PGOAnalysisMap
> *PGOAnalyses
) const {
1016 if (const auto *Obj
= dyn_cast
<ELF32LEObjectFile
>(this))
1017 return readBBAddrMapImpl(Obj
->getELFFile(), TextSectionIndex
, PGOAnalyses
);
1018 if (const auto *Obj
= dyn_cast
<ELF64LEObjectFile
>(this))
1019 return readBBAddrMapImpl(Obj
->getELFFile(), TextSectionIndex
, PGOAnalyses
);
1020 if (const auto *Obj
= dyn_cast
<ELF32BEObjectFile
>(this))
1021 return readBBAddrMapImpl(Obj
->getELFFile(), TextSectionIndex
, PGOAnalyses
);
1022 return readBBAddrMapImpl(cast
<ELF64BEObjectFile
>(this)->getELFFile(),
1023 TextSectionIndex
, PGOAnalyses
);
1026 StringRef
ELFObjectFileBase::getCrelDecodeProblem(SectionRef Sec
) const {
1027 auto Data
= Sec
.getRawDataRefImpl();
1028 if (const auto *Obj
= dyn_cast
<ELF32LEObjectFile
>(this))
1029 return Obj
->getCrelDecodeProblem(Data
);
1030 if (const auto *Obj
= dyn_cast
<ELF32BEObjectFile
>(this))
1031 return Obj
->getCrelDecodeProblem(Data
);
1032 if (const auto *Obj
= dyn_cast
<ELF64LEObjectFile
>(this))
1033 return Obj
->getCrelDecodeProblem(Data
);
1034 return cast
<ELF64BEObjectFile
>(this)->getCrelDecodeProblem(Data
);