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/ADT/Triple.h"
15 #include "llvm/BinaryFormat/ELF.h"
16 #include "llvm/MC/MCInstrAnalysis.h"
17 #include "llvm/MC/SubtargetFeature.h"
18 #include "llvm/Object/ELF.h"
19 #include "llvm/Object/ELFTypes.h"
20 #include "llvm/Object/Error.h"
21 #include "llvm/Support/ARMAttributeParser.h"
22 #include "llvm/Support/ARMBuildAttributes.h"
23 #include "llvm/Support/Endian.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/MathExtras.h"
26 #include "llvm/Support/TargetRegistry.h"
32 #include <system_error>
36 using namespace object
;
38 const EnumEntry
<unsigned> llvm::object::ElfSymbolTypes
[NumElfSymbolTypes
] = {
39 {"None", "NOTYPE", ELF::STT_NOTYPE
},
40 {"Object", "OBJECT", ELF::STT_OBJECT
},
41 {"Function", "FUNC", ELF::STT_FUNC
},
42 {"Section", "SECTION", ELF::STT_SECTION
},
43 {"File", "FILE", ELF::STT_FILE
},
44 {"Common", "COMMON", ELF::STT_COMMON
},
45 {"TLS", "TLS", ELF::STT_TLS
},
46 {"Unknown", "<unknown>: 7", 7},
47 {"Unknown", "<unknown>: 8", 8},
48 {"Unknown", "<unknown>: 9", 9},
49 {"GNU_IFunc", "IFUNC", ELF::STT_GNU_IFUNC
},
50 {"OS Specific", "<OS specific>: 11", 11},
51 {"OS Specific", "<OS specific>: 12", 12},
52 {"Proc Specific", "<processor specific>: 13", 13},
53 {"Proc Specific", "<processor specific>: 14", 14},
54 {"Proc Specific", "<processor specific>: 15", 15}
57 ELFObjectFileBase::ELFObjectFileBase(unsigned int Type
, MemoryBufferRef Source
)
58 : ObjectFile(Type
, Source
) {}
61 static Expected
<std::unique_ptr
<ELFObjectFile
<ELFT
>>>
62 createPtr(MemoryBufferRef Object
) {
63 auto Ret
= ELFObjectFile
<ELFT
>::create(Object
);
64 if (Error E
= Ret
.takeError())
66 return make_unique
<ELFObjectFile
<ELFT
>>(std::move(*Ret
));
69 Expected
<std::unique_ptr
<ObjectFile
>>
70 ObjectFile::createELFObjectFile(MemoryBufferRef Obj
) {
71 std::pair
<unsigned char, unsigned char> Ident
=
72 getElfArchType(Obj
.getBuffer());
73 std::size_t MaxAlignment
=
74 1ULL << countTrailingZeros(uintptr_t(Obj
.getBufferStart()));
77 return createError("Insufficient alignment");
79 if (Ident
.first
== ELF::ELFCLASS32
) {
80 if (Ident
.second
== ELF::ELFDATA2LSB
)
81 return createPtr
<ELF32LE
>(Obj
);
82 else if (Ident
.second
== ELF::ELFDATA2MSB
)
83 return createPtr
<ELF32BE
>(Obj
);
85 return createError("Invalid ELF data");
86 } else if (Ident
.first
== ELF::ELFCLASS64
) {
87 if (Ident
.second
== ELF::ELFDATA2LSB
)
88 return createPtr
<ELF64LE
>(Obj
);
89 else if (Ident
.second
== ELF::ELFDATA2MSB
)
90 return createPtr
<ELF64BE
>(Obj
);
92 return createError("Invalid ELF data");
94 return createError("Invalid ELF class");
97 SubtargetFeatures
ELFObjectFileBase::getMIPSFeatures() const {
98 SubtargetFeatures Features
;
99 unsigned PlatformFlags
= getPlatformFlags();
101 switch (PlatformFlags
& ELF::EF_MIPS_ARCH
) {
102 case ELF::EF_MIPS_ARCH_1
:
104 case ELF::EF_MIPS_ARCH_2
:
105 Features
.AddFeature("mips2");
107 case ELF::EF_MIPS_ARCH_3
:
108 Features
.AddFeature("mips3");
110 case ELF::EF_MIPS_ARCH_4
:
111 Features
.AddFeature("mips4");
113 case ELF::EF_MIPS_ARCH_5
:
114 Features
.AddFeature("mips5");
116 case ELF::EF_MIPS_ARCH_32
:
117 Features
.AddFeature("mips32");
119 case ELF::EF_MIPS_ARCH_64
:
120 Features
.AddFeature("mips64");
122 case ELF::EF_MIPS_ARCH_32R2
:
123 Features
.AddFeature("mips32r2");
125 case ELF::EF_MIPS_ARCH_64R2
:
126 Features
.AddFeature("mips64r2");
128 case ELF::EF_MIPS_ARCH_32R6
:
129 Features
.AddFeature("mips32r6");
131 case ELF::EF_MIPS_ARCH_64R6
:
132 Features
.AddFeature("mips64r6");
135 llvm_unreachable("Unknown EF_MIPS_ARCH value");
138 switch (PlatformFlags
& ELF::EF_MIPS_MACH
) {
139 case ELF::EF_MIPS_MACH_NONE
:
140 // No feature associated with this value.
142 case ELF::EF_MIPS_MACH_OCTEON
:
143 Features
.AddFeature("cnmips");
146 llvm_unreachable("Unknown EF_MIPS_ARCH value");
149 if (PlatformFlags
& ELF::EF_MIPS_ARCH_ASE_M16
)
150 Features
.AddFeature("mips16");
151 if (PlatformFlags
& ELF::EF_MIPS_MICROMIPS
)
152 Features
.AddFeature("micromips");
157 SubtargetFeatures
ELFObjectFileBase::getARMFeatures() const {
158 SubtargetFeatures Features
;
159 ARMAttributeParser Attributes
;
160 if (Error E
= getBuildAttributes(Attributes
))
161 return SubtargetFeatures();
163 // both ARMv7-M and R have to support thumb hardware div
165 if (Attributes
.hasAttribute(ARMBuildAttrs::CPU_arch
))
166 isV7
= Attributes
.getAttributeValue(ARMBuildAttrs::CPU_arch
)
167 == ARMBuildAttrs::v7
;
169 if (Attributes
.hasAttribute(ARMBuildAttrs::CPU_arch_profile
)) {
170 switch(Attributes
.getAttributeValue(ARMBuildAttrs::CPU_arch_profile
)) {
171 case ARMBuildAttrs::ApplicationProfile
:
172 Features
.AddFeature("aclass");
174 case ARMBuildAttrs::RealTimeProfile
:
175 Features
.AddFeature("rclass");
177 Features
.AddFeature("hwdiv");
179 case ARMBuildAttrs::MicroControllerProfile
:
180 Features
.AddFeature("mclass");
182 Features
.AddFeature("hwdiv");
187 if (Attributes
.hasAttribute(ARMBuildAttrs::THUMB_ISA_use
)) {
188 switch(Attributes
.getAttributeValue(ARMBuildAttrs::THUMB_ISA_use
)) {
191 case ARMBuildAttrs::Not_Allowed
:
192 Features
.AddFeature("thumb", false);
193 Features
.AddFeature("thumb2", false);
195 case ARMBuildAttrs::AllowThumb32
:
196 Features
.AddFeature("thumb2");
201 if (Attributes
.hasAttribute(ARMBuildAttrs::FP_arch
)) {
202 switch(Attributes
.getAttributeValue(ARMBuildAttrs::FP_arch
)) {
205 case ARMBuildAttrs::Not_Allowed
:
206 Features
.AddFeature("vfp2d16sp", false);
207 Features
.AddFeature("vfp3d16sp", false);
208 Features
.AddFeature("vfp4d16sp", false);
210 case ARMBuildAttrs::AllowFPv2
:
211 Features
.AddFeature("vfp2");
213 case ARMBuildAttrs::AllowFPv3A
:
214 case ARMBuildAttrs::AllowFPv3B
:
215 Features
.AddFeature("vfp3");
217 case ARMBuildAttrs::AllowFPv4A
:
218 case ARMBuildAttrs::AllowFPv4B
:
219 Features
.AddFeature("vfp4");
224 if (Attributes
.hasAttribute(ARMBuildAttrs::Advanced_SIMD_arch
)) {
225 switch(Attributes
.getAttributeValue(ARMBuildAttrs::Advanced_SIMD_arch
)) {
228 case ARMBuildAttrs::Not_Allowed
:
229 Features
.AddFeature("neon", false);
230 Features
.AddFeature("fp16", false);
232 case ARMBuildAttrs::AllowNeon
:
233 Features
.AddFeature("neon");
235 case ARMBuildAttrs::AllowNeon2
:
236 Features
.AddFeature("neon");
237 Features
.AddFeature("fp16");
242 if (Attributes
.hasAttribute(ARMBuildAttrs::MVE_arch
)) {
243 switch(Attributes
.getAttributeValue(ARMBuildAttrs::MVE_arch
)) {
246 case ARMBuildAttrs::Not_Allowed
:
247 Features
.AddFeature("mve", false);
248 Features
.AddFeature("mve.fp", false);
250 case ARMBuildAttrs::AllowMVEInteger
:
251 Features
.AddFeature("mve.fp", false);
252 Features
.AddFeature("mve");
254 case ARMBuildAttrs::AllowMVEIntegerAndFloat
:
255 Features
.AddFeature("mve.fp");
260 if (Attributes
.hasAttribute(ARMBuildAttrs::DIV_use
)) {
261 switch(Attributes
.getAttributeValue(ARMBuildAttrs::DIV_use
)) {
264 case ARMBuildAttrs::DisallowDIV
:
265 Features
.AddFeature("hwdiv", false);
266 Features
.AddFeature("hwdiv-arm", false);
268 case ARMBuildAttrs::AllowDIVExt
:
269 Features
.AddFeature("hwdiv");
270 Features
.AddFeature("hwdiv-arm");
278 SubtargetFeatures
ELFObjectFileBase::getRISCVFeatures() const {
279 SubtargetFeatures Features
;
280 unsigned PlatformFlags
= getPlatformFlags();
282 if (PlatformFlags
& ELF::EF_RISCV_RVC
) {
283 Features
.AddFeature("c");
289 SubtargetFeatures
ELFObjectFileBase::getFeatures() const {
290 switch (getEMachine()) {
292 return getMIPSFeatures();
294 return getARMFeatures();
296 return getRISCVFeatures();
298 return SubtargetFeatures();
302 // FIXME Encode from a tablegen description or target parser.
303 void ELFObjectFileBase::setARMSubArch(Triple
&TheTriple
) const {
304 if (TheTriple
.getSubArch() != Triple::NoSubArch
)
307 ARMAttributeParser Attributes
;
308 if (Error E
= getBuildAttributes(Attributes
))
312 // Default to ARM, but use the triple if it's been set.
313 if (TheTriple
.isThumb())
318 if (Attributes
.hasAttribute(ARMBuildAttrs::CPU_arch
)) {
319 switch(Attributes
.getAttributeValue(ARMBuildAttrs::CPU_arch
)) {
320 case ARMBuildAttrs::v4
:
323 case ARMBuildAttrs::v4T
:
326 case ARMBuildAttrs::v5T
:
329 case ARMBuildAttrs::v5TE
:
332 case ARMBuildAttrs::v5TEJ
:
335 case ARMBuildAttrs::v6
:
338 case ARMBuildAttrs::v6KZ
:
341 case ARMBuildAttrs::v6T2
:
344 case ARMBuildAttrs::v6K
:
347 case ARMBuildAttrs::v7
:
350 case ARMBuildAttrs::v6_M
:
353 case ARMBuildAttrs::v6S_M
:
356 case ARMBuildAttrs::v7E_M
:
361 if (!isLittleEndian())
364 TheTriple
.setArchName(Triple
);
367 std::vector
<std::pair
<DataRefImpl
, uint64_t>>
368 ELFObjectFileBase::getPltAddresses() const {
370 const auto Triple
= makeTriple();
371 const auto *T
= TargetRegistry::lookupTarget(Triple
.str(), Err
);
374 uint64_t JumpSlotReloc
= 0;
375 switch (Triple
.getArch()) {
377 JumpSlotReloc
= ELF::R_386_JUMP_SLOT
;
380 JumpSlotReloc
= ELF::R_X86_64_JUMP_SLOT
;
382 case Triple::aarch64
:
383 JumpSlotReloc
= ELF::R_AARCH64_JUMP_SLOT
;
388 std::unique_ptr
<const MCInstrInfo
> MII(T
->createMCInstrInfo());
389 std::unique_ptr
<const MCInstrAnalysis
> MIA(
390 T
->createMCInstrAnalysis(MII
.get()));
393 Optional
<SectionRef
> Plt
= None
, RelaPlt
= None
, GotPlt
= None
;
394 for (const SectionRef
&Section
: sections()) {
395 Expected
<StringRef
> NameOrErr
= Section
.getName();
397 consumeError(NameOrErr
.takeError());
400 StringRef Name
= *NameOrErr
;
404 else if (Name
== ".rela.plt" || Name
== ".rel.plt")
406 else if (Name
== ".got.plt")
409 if (!Plt
|| !RelaPlt
|| !GotPlt
)
411 Expected
<StringRef
> PltContents
= Plt
->getContents();
413 consumeError(PltContents
.takeError());
416 auto PltEntries
= MIA
->findPltEntries(Plt
->getAddress(),
417 arrayRefFromStringRef(*PltContents
),
418 GotPlt
->getAddress(), Triple
);
419 // Build a map from GOT entry virtual address to PLT entry virtual address.
420 DenseMap
<uint64_t, uint64_t> GotToPlt
;
421 for (const auto &Entry
: PltEntries
)
422 GotToPlt
.insert(std::make_pair(Entry
.second
, Entry
.first
));
423 // Find the relocations in the dynamic relocation table that point to
424 // locations in the GOT for which we know the corresponding PLT entry.
425 std::vector
<std::pair
<DataRefImpl
, uint64_t>> Result
;
426 for (const auto &Relocation
: RelaPlt
->relocations()) {
427 if (Relocation
.getType() != JumpSlotReloc
)
429 auto PltEntryIter
= GotToPlt
.find(Relocation
.getOffset());
430 if (PltEntryIter
!= GotToPlt
.end())
431 Result
.push_back(std::make_pair(
432 Relocation
.getSymbol()->getRawDataRefImpl(), PltEntryIter
->second
));