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/MathExtras.h"
24 #include "llvm/Support/RISCVAttributeParser.h"
25 #include "llvm/Support/RISCVAttributes.h"
26 #include "llvm/Support/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 Expected
<SubtargetFeatures
> ELFObjectFileBase::getRISCVFeatures() const {
291 SubtargetFeatures Features
;
292 unsigned PlatformFlags
= getPlatformFlags();
294 if (PlatformFlags
& ELF::EF_RISCV_RVC
) {
295 Features
.AddFeature("c");
298 RISCVAttributeParser Attributes
;
299 if (Error E
= getBuildAttributes(Attributes
)) {
303 std::optional
<StringRef
> Attr
=
304 Attributes
.getAttributeString(RISCVAttrs::ARCH
);
306 auto ParseResult
= RISCVISAInfo::parseNormalizedArchString(*Attr
);
308 return ParseResult
.takeError();
309 auto &ISAInfo
= *ParseResult
;
311 if (ISAInfo
->getXLen() == 32)
312 Features
.AddFeature("64bit", false);
313 else if (ISAInfo
->getXLen() == 64)
314 Features
.AddFeature("64bit");
316 llvm_unreachable("XLEN should be 32 or 64.");
318 Features
.addFeaturesVector(ISAInfo
->toFeatureVector());
324 SubtargetFeatures
ELFObjectFileBase::getLoongArchFeatures() const {
325 SubtargetFeatures Features
;
327 switch (getPlatformFlags() & ELF::EF_LOONGARCH_ABI_MODIFIER_MASK
) {
328 case ELF::EF_LOONGARCH_ABI_SOFT_FLOAT
:
330 case ELF::EF_LOONGARCH_ABI_DOUBLE_FLOAT
:
331 Features
.AddFeature("d");
332 // D implies F according to LoongArch ISA spec.
334 case ELF::EF_LOONGARCH_ABI_SINGLE_FLOAT
:
335 Features
.AddFeature("f");
342 Expected
<SubtargetFeatures
> ELFObjectFileBase::getFeatures() const {
343 switch (getEMachine()) {
345 return getMIPSFeatures();
347 return getARMFeatures();
349 return getRISCVFeatures();
350 case ELF::EM_LOONGARCH
:
351 return getLoongArchFeatures();
353 return SubtargetFeatures();
357 std::optional
<StringRef
> ELFObjectFileBase::tryGetCPUName() const {
358 switch (getEMachine()) {
360 return getAMDGPUCPUName();
363 return StringRef("future");
369 StringRef
ELFObjectFileBase::getAMDGPUCPUName() const {
370 assert(getEMachine() == ELF::EM_AMDGPU
);
371 unsigned CPU
= getPlatformFlags() & ELF::EF_AMDGPU_MACH
;
374 // Radeon HD 2000/3000 Series (R600).
375 case ELF::EF_AMDGPU_MACH_R600_R600
:
377 case ELF::EF_AMDGPU_MACH_R600_R630
:
379 case ELF::EF_AMDGPU_MACH_R600_RS880
:
381 case ELF::EF_AMDGPU_MACH_R600_RV670
:
384 // Radeon HD 4000 Series (R700).
385 case ELF::EF_AMDGPU_MACH_R600_RV710
:
387 case ELF::EF_AMDGPU_MACH_R600_RV730
:
389 case ELF::EF_AMDGPU_MACH_R600_RV770
:
392 // Radeon HD 5000 Series (Evergreen).
393 case ELF::EF_AMDGPU_MACH_R600_CEDAR
:
395 case ELF::EF_AMDGPU_MACH_R600_CYPRESS
:
397 case ELF::EF_AMDGPU_MACH_R600_JUNIPER
:
399 case ELF::EF_AMDGPU_MACH_R600_REDWOOD
:
401 case ELF::EF_AMDGPU_MACH_R600_SUMO
:
404 // Radeon HD 6000 Series (Northern Islands).
405 case ELF::EF_AMDGPU_MACH_R600_BARTS
:
407 case ELF::EF_AMDGPU_MACH_R600_CAICOS
:
409 case ELF::EF_AMDGPU_MACH_R600_CAYMAN
:
411 case ELF::EF_AMDGPU_MACH_R600_TURKS
:
415 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX600
:
417 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX601
:
419 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX602
:
423 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX700
:
425 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX701
:
427 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX702
:
429 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX703
:
431 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX704
:
433 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX705
:
437 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX801
:
439 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX802
:
441 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX803
:
443 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX805
:
445 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX810
:
449 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX900
:
451 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX902
:
453 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX904
:
455 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX906
:
457 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX908
:
459 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX909
:
461 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX90A
:
463 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX90C
:
465 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX940
:
467 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX941
:
469 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX942
:
473 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1010
:
475 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1011
:
477 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1012
:
479 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1013
:
481 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1030
:
483 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1031
:
485 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1032
:
487 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1033
:
489 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1034
:
491 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1035
:
493 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1036
:
497 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1100
:
499 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1101
:
501 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1102
:
503 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1103
:
505 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1150
:
507 case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1151
:
510 llvm_unreachable("Unknown EF_AMDGPU_MACH value");
514 // FIXME Encode from a tablegen description or target parser.
515 void ELFObjectFileBase::setARMSubArch(Triple
&TheTriple
) const {
516 if (TheTriple
.getSubArch() != Triple::NoSubArch
)
519 ARMAttributeParser Attributes
;
520 if (Error E
= getBuildAttributes(Attributes
)) {
521 // TODO Propagate Error.
522 consumeError(std::move(E
));
527 // Default to ARM, but use the triple if it's been set.
528 if (TheTriple
.isThumb())
533 std::optional
<unsigned> Attr
=
534 Attributes
.getAttributeValue(ARMBuildAttrs::CPU_arch
);
537 case ARMBuildAttrs::v4
:
540 case ARMBuildAttrs::v4T
:
543 case ARMBuildAttrs::v5T
:
546 case ARMBuildAttrs::v5TE
:
549 case ARMBuildAttrs::v5TEJ
:
552 case ARMBuildAttrs::v6
:
555 case ARMBuildAttrs::v6KZ
:
558 case ARMBuildAttrs::v6T2
:
561 case ARMBuildAttrs::v6K
:
564 case ARMBuildAttrs::v7
: {
565 std::optional
<unsigned> ArchProfileAttr
=
566 Attributes
.getAttributeValue(ARMBuildAttrs::CPU_arch_profile
);
567 if (ArchProfileAttr
&&
568 *ArchProfileAttr
== ARMBuildAttrs::MicroControllerProfile
)
574 case ARMBuildAttrs::v6_M
:
577 case ARMBuildAttrs::v6S_M
:
580 case ARMBuildAttrs::v7E_M
:
583 case ARMBuildAttrs::v8_A
:
586 case ARMBuildAttrs::v8_R
:
589 case ARMBuildAttrs::v8_M_Base
:
590 Triple
+= "v8m.base";
592 case ARMBuildAttrs::v8_M_Main
:
593 Triple
+= "v8m.main";
595 case ARMBuildAttrs::v8_1_M_Main
:
596 Triple
+= "v8.1m.main";
598 case ARMBuildAttrs::v9_A
:
603 if (!isLittleEndian())
606 TheTriple
.setArchName(Triple
);
609 std::vector
<ELFPltEntry
> ELFObjectFileBase::getPltEntries() const {
611 const auto Triple
= makeTriple();
612 const auto *T
= TargetRegistry::lookupTarget(Triple
.str(), Err
);
615 uint32_t JumpSlotReloc
= 0, GlobDatReloc
= 0;
616 switch (Triple
.getArch()) {
618 JumpSlotReloc
= ELF::R_386_JUMP_SLOT
;
619 GlobDatReloc
= ELF::R_386_GLOB_DAT
;
622 JumpSlotReloc
= ELF::R_X86_64_JUMP_SLOT
;
623 GlobDatReloc
= ELF::R_X86_64_GLOB_DAT
;
625 case Triple::aarch64
:
626 case Triple::aarch64_be
:
627 JumpSlotReloc
= ELF::R_AARCH64_JUMP_SLOT
;
632 std::unique_ptr
<const MCInstrInfo
> MII(T
->createMCInstrInfo());
633 std::unique_ptr
<const MCInstrAnalysis
> MIA(
634 T
->createMCInstrAnalysis(MII
.get()));
637 std::vector
<std::pair
<uint64_t, uint64_t>> PltEntries
;
638 std::optional
<SectionRef
> RelaPlt
, RelaDyn
;
639 uint64_t GotBaseVA
= 0;
640 for (const SectionRef
&Section
: sections()) {
641 Expected
<StringRef
> NameOrErr
= Section
.getName();
643 consumeError(NameOrErr
.takeError());
646 StringRef Name
= *NameOrErr
;
648 if (Name
== ".rela.plt" || Name
== ".rel.plt") {
650 } else if (Name
== ".rela.dyn" || Name
== ".rel.dyn") {
652 } else if (Name
== ".got.plt") {
653 GotBaseVA
= Section
.getAddress();
654 } else if (Name
== ".plt" || Name
== ".plt.got") {
655 Expected
<StringRef
> PltContents
= Section
.getContents();
657 consumeError(PltContents
.takeError());
662 MIA
->findPltEntries(Section
.getAddress(),
663 arrayRefFromStringRef(*PltContents
), Triple
));
667 // Build a map from GOT entry virtual address to PLT entry virtual address.
668 DenseMap
<uint64_t, uint64_t> GotToPlt
;
669 for (auto [Plt
, GotPlt
] : PltEntries
) {
670 uint64_t GotPltEntry
= GotPlt
;
671 // An x86-32 PIC PLT uses jmp DWORD PTR [ebx-offset]. Add
672 // _GLOBAL_OFFSET_TABLE_ (EBX) to get the .got.plt (or .got) entry address.
673 // See X86MCTargetDesc.cpp:findPltEntries for the 1 << 32 bit.
674 if (GotPltEntry
& (uint64_t(1) << 32) && getEMachine() == ELF::EM_386
)
675 GotPltEntry
= static_cast<int32_t>(GotPltEntry
) + GotBaseVA
;
676 GotToPlt
.insert(std::make_pair(GotPltEntry
, Plt
));
679 // Find the relocations in the dynamic relocation table that point to
680 // locations in the GOT for which we know the corresponding PLT entry.
681 std::vector
<ELFPltEntry
> Result
;
682 auto handleRels
= [&](iterator_range
<relocation_iterator
> Rels
,
683 uint32_t RelType
, StringRef PltSec
) {
684 for (const auto &R
: Rels
) {
685 if (R
.getType() != RelType
)
687 auto PltEntryIter
= GotToPlt
.find(R
.getOffset());
688 if (PltEntryIter
!= GotToPlt
.end()) {
689 symbol_iterator Sym
= R
.getSymbol();
690 if (Sym
== symbol_end())
692 ELFPltEntry
{PltSec
, std::nullopt
, PltEntryIter
->second
});
694 Result
.push_back(ELFPltEntry
{PltSec
, Sym
->getRawDataRefImpl(),
695 PltEntryIter
->second
});
701 handleRels(RelaPlt
->relocations(), JumpSlotReloc
, ".plt");
703 // If a symbol needing a PLT entry also needs a GLOB_DAT relocation, GNU ld's
704 // x86 port places the PLT entry in the .plt.got section.
706 handleRels(RelaDyn
->relocations(), GlobDatReloc
, ".plt.got");
711 template <class ELFT
>
712 Expected
<std::vector
<BBAddrMap
>> static readBBAddrMapImpl(
713 const ELFFile
<ELFT
> &EF
, std::optional
<unsigned> TextSectionIndex
) {
714 using Elf_Shdr
= typename
ELFT::Shdr
;
715 bool IsRelocatable
= EF
.getHeader().e_type
== ELF::ET_REL
;
716 std::vector
<BBAddrMap
> BBAddrMaps
;
718 const auto &Sections
= cantFail(EF
.sections());
719 auto IsMatch
= [&](const Elf_Shdr
&Sec
) -> Expected
<bool> {
720 if (Sec
.sh_type
!= ELF::SHT_LLVM_BB_ADDR_MAP
&&
721 Sec
.sh_type
!= ELF::SHT_LLVM_BB_ADDR_MAP_V0
)
723 if (!TextSectionIndex
)
725 Expected
<const Elf_Shdr
*> TextSecOrErr
= EF
.getSection(Sec
.sh_link
);
727 return createError("unable to get the linked-to section for " +
728 describe(EF
, Sec
) + ": " +
729 toString(TextSecOrErr
.takeError()));
730 if (*TextSectionIndex
!= std::distance(Sections
.begin(), *TextSecOrErr
))
735 Expected
<MapVector
<const Elf_Shdr
*, const Elf_Shdr
*>> SectionRelocMapOrErr
=
736 EF
.getSectionAndRelocations(IsMatch
);
737 if (!SectionRelocMapOrErr
)
738 return SectionRelocMapOrErr
.takeError();
740 for (auto const &[Sec
, RelocSec
] : *SectionRelocMapOrErr
) {
741 if (IsRelocatable
&& !RelocSec
)
742 return createError("unable to get relocation section for " +
744 Expected
<std::vector
<BBAddrMap
>> BBAddrMapOrErr
=
745 EF
.decodeBBAddrMap(*Sec
, RelocSec
);
747 return createError("unable to read " + describe(EF
, *Sec
) + ": " +
748 toString(BBAddrMapOrErr
.takeError()));
749 std::move(BBAddrMapOrErr
->begin(), BBAddrMapOrErr
->end(),
750 std::back_inserter(BBAddrMaps
));
755 template <class ELFT
>
756 static Expected
<std::vector
<VersionEntry
>>
757 readDynsymVersionsImpl(const ELFFile
<ELFT
> &EF
,
758 ELFObjectFileBase::elf_symbol_iterator_range Symbols
) {
759 using Elf_Shdr
= typename
ELFT::Shdr
;
760 const Elf_Shdr
*VerSec
= nullptr;
761 const Elf_Shdr
*VerNeedSec
= nullptr;
762 const Elf_Shdr
*VerDefSec
= nullptr;
763 // The user should ensure sections() can't fail here.
764 for (const Elf_Shdr
&Sec
: cantFail(EF
.sections())) {
765 if (Sec
.sh_type
== ELF::SHT_GNU_versym
)
767 else if (Sec
.sh_type
== ELF::SHT_GNU_verdef
)
769 else if (Sec
.sh_type
== ELF::SHT_GNU_verneed
)
773 return std::vector
<VersionEntry
>();
775 Expected
<SmallVector
<std::optional
<VersionEntry
>, 0>> MapOrErr
=
776 EF
.loadVersionMap(VerNeedSec
, VerDefSec
);
778 return MapOrErr
.takeError();
780 std::vector
<VersionEntry
> Ret
;
782 for (const ELFSymbolRef
&Sym
: Symbols
) {
784 Expected
<const typename
ELFT::Versym
*> VerEntryOrErr
=
785 EF
.template getEntry
<typename
ELFT::Versym
>(*VerSec
, I
);
787 return createError("unable to read an entry with index " + Twine(I
) +
788 " from " + describe(EF
, *VerSec
) + ": " +
789 toString(VerEntryOrErr
.takeError()));
791 Expected
<uint32_t> FlagsOrErr
= Sym
.getFlags();
793 return createError("unable to read flags for symbol with index " +
794 Twine(I
) + ": " + toString(FlagsOrErr
.takeError()));
797 Expected
<StringRef
> VerOrErr
= EF
.getSymbolVersionByIndex(
798 (*VerEntryOrErr
)->vs_index
, IsDefault
, *MapOrErr
,
799 (*FlagsOrErr
) & SymbolRef::SF_Undefined
);
801 return createError("unable to get a version for entry " + Twine(I
) +
802 " of " + describe(EF
, *VerSec
) + ": " +
803 toString(VerOrErr
.takeError()));
805 Ret
.push_back({(*VerOrErr
).str(), IsDefault
});
811 Expected
<std::vector
<VersionEntry
>>
812 ELFObjectFileBase::readDynsymVersions() const {
813 elf_symbol_iterator_range Symbols
= getDynamicSymbolIterators();
814 if (const auto *Obj
= dyn_cast
<ELF32LEObjectFile
>(this))
815 return readDynsymVersionsImpl(Obj
->getELFFile(), Symbols
);
816 if (const auto *Obj
= dyn_cast
<ELF32BEObjectFile
>(this))
817 return readDynsymVersionsImpl(Obj
->getELFFile(), Symbols
);
818 if (const auto *Obj
= dyn_cast
<ELF64LEObjectFile
>(this))
819 return readDynsymVersionsImpl(Obj
->getELFFile(), Symbols
);
820 return readDynsymVersionsImpl(cast
<ELF64BEObjectFile
>(this)->getELFFile(),
824 Expected
<std::vector
<BBAddrMap
>> ELFObjectFileBase::readBBAddrMap(
825 std::optional
<unsigned> TextSectionIndex
) const {
826 if (const auto *Obj
= dyn_cast
<ELF32LEObjectFile
>(this))
827 return readBBAddrMapImpl(Obj
->getELFFile(), TextSectionIndex
);
828 if (const auto *Obj
= dyn_cast
<ELF64LEObjectFile
>(this))
829 return readBBAddrMapImpl(Obj
->getELFFile(), TextSectionIndex
);
830 if (const auto *Obj
= dyn_cast
<ELF32BEObjectFile
>(this))
831 return readBBAddrMapImpl(Obj
->getELFFile(), TextSectionIndex
);
832 return readBBAddrMapImpl(cast
<ELF64BEObjectFile
>(this)->getELFFile(),