1 //===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===//
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 // Implementation of ELF support for the MC-JIT runtime dynamic linker.
11 //===----------------------------------------------------------------------===//
13 #include "RuntimeDyldELF.h"
14 #include "RuntimeDyldCheckerImpl.h"
15 #include "Targets/RuntimeDyldELFMips.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/BinaryFormat/ELF.h"
20 #include "llvm/Object/ELFObjectFile.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/Endian.h"
23 #include "llvm/Support/MemoryBuffer.h"
26 using namespace llvm::object
;
27 using namespace llvm::support::endian
;
29 #define DEBUG_TYPE "dyld"
31 static void or32le(void *P
, int32_t V
) { write32le(P
, read32le(P
) | V
); }
33 static void or32AArch64Imm(void *L
, uint64_t Imm
) {
34 or32le(L
, (Imm
& 0xFFF) << 10);
37 template <class T
> static void write(bool isBE
, void *P
, T V
) {
38 isBE
? write
<T
, support::big
>(P
, V
) : write
<T
, support::little
>(P
, V
);
41 static void write32AArch64Addr(void *L
, uint64_t Imm
) {
42 uint32_t ImmLo
= (Imm
& 0x3) << 29;
43 uint32_t ImmHi
= (Imm
& 0x1FFFFC) << 3;
44 uint64_t Mask
= (0x3 << 29) | (0x1FFFFC << 3);
45 write32le(L
, (read32le(L
) & ~Mask
) | ImmLo
| ImmHi
);
48 // Return the bits [Start, End] from Val shifted Start bits.
49 // For instance, getBits(0xF0, 4, 8) returns 0xF.
50 static uint64_t getBits(uint64_t Val
, int Start
, int End
) {
51 uint64_t Mask
= ((uint64_t)1 << (End
+ 1 - Start
)) - 1;
52 return (Val
>> Start
) & Mask
;
57 template <class ELFT
> class DyldELFObject
: public ELFObjectFile
<ELFT
> {
58 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT
)
60 typedef typename
ELFT::uint addr_type
;
62 DyldELFObject(ELFObjectFile
<ELFT
> &&Obj
);
65 static Expected
<std::unique_ptr
<DyldELFObject
>>
66 create(MemoryBufferRef Wrapper
);
68 void updateSectionAddress(const SectionRef
&Sec
, uint64_t Addr
);
70 void updateSymbolAddress(const SymbolRef
&SymRef
, uint64_t Addr
);
72 // Methods for type inquiry through isa, cast and dyn_cast
73 static bool classof(const Binary
*v
) {
74 return (isa
<ELFObjectFile
<ELFT
>>(v
) &&
75 classof(cast
<ELFObjectFile
<ELFT
>>(v
)));
77 static bool classof(const ELFObjectFile
<ELFT
> *v
) {
78 return v
->isDyldType();
84 // The MemoryBuffer passed into this constructor is just a wrapper around the
85 // actual memory. Ultimately, the Binary parent class will take ownership of
86 // this MemoryBuffer object but not the underlying memory.
88 DyldELFObject
<ELFT
>::DyldELFObject(ELFObjectFile
<ELFT
> &&Obj
)
89 : ELFObjectFile
<ELFT
>(std::move(Obj
)) {
90 this->isDyldELFObject
= true;
94 Expected
<std::unique_ptr
<DyldELFObject
<ELFT
>>>
95 DyldELFObject
<ELFT
>::create(MemoryBufferRef Wrapper
) {
96 auto Obj
= ELFObjectFile
<ELFT
>::create(Wrapper
);
97 if (auto E
= Obj
.takeError())
99 std::unique_ptr
<DyldELFObject
<ELFT
>> Ret(
100 new DyldELFObject
<ELFT
>(std::move(*Obj
)));
101 return std::move(Ret
);
104 template <class ELFT
>
105 void DyldELFObject
<ELFT
>::updateSectionAddress(const SectionRef
&Sec
,
107 DataRefImpl ShdrRef
= Sec
.getRawDataRefImpl();
109 const_cast<Elf_Shdr
*>(reinterpret_cast<const Elf_Shdr
*>(ShdrRef
.p
));
111 // This assumes the address passed in matches the target address bitness
112 // The template-based type cast handles everything else.
113 shdr
->sh_addr
= static_cast<addr_type
>(Addr
);
116 template <class ELFT
>
117 void DyldELFObject
<ELFT
>::updateSymbolAddress(const SymbolRef
&SymRef
,
120 Elf_Sym
*sym
= const_cast<Elf_Sym
*>(
121 ELFObjectFile
<ELFT
>::getSymbol(SymRef
.getRawDataRefImpl()));
123 // This assumes the address passed in matches the target address bitness
124 // The template-based type cast handles everything else.
125 sym
->st_value
= static_cast<addr_type
>(Addr
);
128 class LoadedELFObjectInfo final
129 : public LoadedObjectInfoHelper
<LoadedELFObjectInfo
,
130 RuntimeDyld::LoadedObjectInfo
> {
132 LoadedELFObjectInfo(RuntimeDyldImpl
&RTDyld
, ObjSectionToIDMap ObjSecToIDMap
)
133 : LoadedObjectInfoHelper(RTDyld
, std::move(ObjSecToIDMap
)) {}
135 OwningBinary
<ObjectFile
>
136 getObjectForDebug(const ObjectFile
&Obj
) const override
;
139 template <typename ELFT
>
140 static Expected
<std::unique_ptr
<DyldELFObject
<ELFT
>>>
141 createRTDyldELFObject(MemoryBufferRef Buffer
, const ObjectFile
&SourceObject
,
142 const LoadedELFObjectInfo
&L
) {
143 typedef typename
ELFT::Shdr Elf_Shdr
;
144 typedef typename
ELFT::uint addr_type
;
146 Expected
<std::unique_ptr
<DyldELFObject
<ELFT
>>> ObjOrErr
=
147 DyldELFObject
<ELFT
>::create(Buffer
);
148 if (Error E
= ObjOrErr
.takeError())
151 std::unique_ptr
<DyldELFObject
<ELFT
>> Obj
= std::move(*ObjOrErr
);
153 // Iterate over all sections in the object.
154 auto SI
= SourceObject
.section_begin();
155 for (const auto &Sec
: Obj
->sections()) {
156 Expected
<StringRef
> NameOrErr
= Sec
.getName();
158 consumeError(NameOrErr
.takeError());
162 if (*NameOrErr
!= "") {
163 DataRefImpl ShdrRef
= Sec
.getRawDataRefImpl();
164 Elf_Shdr
*shdr
= const_cast<Elf_Shdr
*>(
165 reinterpret_cast<const Elf_Shdr
*>(ShdrRef
.p
));
167 if (uint64_t SecLoadAddr
= L
.getSectionLoadAddress(*SI
)) {
168 // This assumes that the address passed in matches the target address
169 // bitness. The template-based type cast handles everything else.
170 shdr
->sh_addr
= static_cast<addr_type
>(SecLoadAddr
);
176 return std::move(Obj
);
179 static OwningBinary
<ObjectFile
>
180 createELFDebugObject(const ObjectFile
&Obj
, const LoadedELFObjectInfo
&L
) {
181 assert(Obj
.isELF() && "Not an ELF object file.");
183 std::unique_ptr
<MemoryBuffer
> Buffer
=
184 MemoryBuffer::getMemBufferCopy(Obj
.getData(), Obj
.getFileName());
186 Expected
<std::unique_ptr
<ObjectFile
>> DebugObj(nullptr);
187 handleAllErrors(DebugObj
.takeError());
188 if (Obj
.getBytesInAddress() == 4 && Obj
.isLittleEndian())
190 createRTDyldELFObject
<ELF32LE
>(Buffer
->getMemBufferRef(), Obj
, L
);
191 else if (Obj
.getBytesInAddress() == 4 && !Obj
.isLittleEndian())
193 createRTDyldELFObject
<ELF32BE
>(Buffer
->getMemBufferRef(), Obj
, L
);
194 else if (Obj
.getBytesInAddress() == 8 && !Obj
.isLittleEndian())
196 createRTDyldELFObject
<ELF64BE
>(Buffer
->getMemBufferRef(), Obj
, L
);
197 else if (Obj
.getBytesInAddress() == 8 && Obj
.isLittleEndian())
199 createRTDyldELFObject
<ELF64LE
>(Buffer
->getMemBufferRef(), Obj
, L
);
201 llvm_unreachable("Unexpected ELF format");
203 handleAllErrors(DebugObj
.takeError());
204 return OwningBinary
<ObjectFile
>(std::move(*DebugObj
), std::move(Buffer
));
207 OwningBinary
<ObjectFile
>
208 LoadedELFObjectInfo::getObjectForDebug(const ObjectFile
&Obj
) const {
209 return createELFDebugObject(Obj
, *this);
212 } // anonymous namespace
216 RuntimeDyldELF::RuntimeDyldELF(RuntimeDyld::MemoryManager
&MemMgr
,
217 JITSymbolResolver
&Resolver
)
218 : RuntimeDyldImpl(MemMgr
, Resolver
), GOTSectionID(0), CurrentGOTIndex(0) {}
219 RuntimeDyldELF::~RuntimeDyldELF() {}
221 void RuntimeDyldELF::registerEHFrames() {
222 for (int i
= 0, e
= UnregisteredEHFrameSections
.size(); i
!= e
; ++i
) {
223 SID EHFrameSID
= UnregisteredEHFrameSections
[i
];
224 uint8_t *EHFrameAddr
= Sections
[EHFrameSID
].getAddress();
225 uint64_t EHFrameLoadAddr
= Sections
[EHFrameSID
].getLoadAddress();
226 size_t EHFrameSize
= Sections
[EHFrameSID
].getSize();
227 MemMgr
.registerEHFrames(EHFrameAddr
, EHFrameLoadAddr
, EHFrameSize
);
229 UnregisteredEHFrameSections
.clear();
232 std::unique_ptr
<RuntimeDyldELF
>
233 llvm::RuntimeDyldELF::create(Triple::ArchType Arch
,
234 RuntimeDyld::MemoryManager
&MemMgr
,
235 JITSymbolResolver
&Resolver
) {
238 return std::make_unique
<RuntimeDyldELF
>(MemMgr
, Resolver
);
242 case Triple::mips64el
:
243 return std::make_unique
<RuntimeDyldELFMips
>(MemMgr
, Resolver
);
247 std::unique_ptr
<RuntimeDyld::LoadedObjectInfo
>
248 RuntimeDyldELF::loadObject(const object::ObjectFile
&O
) {
249 if (auto ObjSectionToIDOrErr
= loadObjectImpl(O
))
250 return std::make_unique
<LoadedELFObjectInfo
>(*this, *ObjSectionToIDOrErr
);
253 raw_string_ostream
ErrStream(ErrorStr
);
254 logAllUnhandledErrors(ObjSectionToIDOrErr
.takeError(), ErrStream
);
259 void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry
&Section
,
260 uint64_t Offset
, uint64_t Value
,
261 uint32_t Type
, int64_t Addend
,
262 uint64_t SymOffset
) {
265 report_fatal_error("Relocation type not implemented yet!");
267 case ELF::R_X86_64_NONE
:
269 case ELF::R_X86_64_8
: {
271 assert((int64_t)Value
<= INT8_MAX
&& (int64_t)Value
>= INT8_MIN
);
272 uint8_t TruncatedAddr
= (Value
& 0xFF);
273 *Section
.getAddressWithOffset(Offset
) = TruncatedAddr
;
274 LLVM_DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr
) << " at "
275 << format("%p\n", Section
.getAddressWithOffset(Offset
)));
278 case ELF::R_X86_64_16
: {
280 assert((int64_t)Value
<= INT16_MAX
&& (int64_t)Value
>= INT16_MIN
);
281 uint16_t TruncatedAddr
= (Value
& 0xFFFF);
282 support::ulittle16_t::ref(Section
.getAddressWithOffset(Offset
)) =
284 LLVM_DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr
) << " at "
285 << format("%p\n", Section
.getAddressWithOffset(Offset
)));
288 case ELF::R_X86_64_64
: {
289 support::ulittle64_t::ref(Section
.getAddressWithOffset(Offset
)) =
291 LLVM_DEBUG(dbgs() << "Writing " << format("%p", (Value
+ Addend
)) << " at "
292 << format("%p\n", Section
.getAddressWithOffset(Offset
)));
295 case ELF::R_X86_64_32
:
296 case ELF::R_X86_64_32S
: {
298 assert((Type
== ELF::R_X86_64_32
&& (Value
<= UINT32_MAX
)) ||
299 (Type
== ELF::R_X86_64_32S
&&
300 ((int64_t)Value
<= INT32_MAX
&& (int64_t)Value
>= INT32_MIN
)));
301 uint32_t TruncatedAddr
= (Value
& 0xFFFFFFFF);
302 support::ulittle32_t::ref(Section
.getAddressWithOffset(Offset
)) =
304 LLVM_DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr
) << " at "
305 << format("%p\n", Section
.getAddressWithOffset(Offset
)));
308 case ELF::R_X86_64_PC8
: {
309 uint64_t FinalAddress
= Section
.getLoadAddressWithOffset(Offset
);
310 int64_t RealOffset
= Value
+ Addend
- FinalAddress
;
311 assert(isInt
<8>(RealOffset
));
312 int8_t TruncOffset
= (RealOffset
& 0xFF);
313 Section
.getAddress()[Offset
] = TruncOffset
;
316 case ELF::R_X86_64_PC32
: {
317 uint64_t FinalAddress
= Section
.getLoadAddressWithOffset(Offset
);
318 int64_t RealOffset
= Value
+ Addend
- FinalAddress
;
319 assert(isInt
<32>(RealOffset
));
320 int32_t TruncOffset
= (RealOffset
& 0xFFFFFFFF);
321 support::ulittle32_t::ref(Section
.getAddressWithOffset(Offset
)) =
325 case ELF::R_X86_64_PC64
: {
326 uint64_t FinalAddress
= Section
.getLoadAddressWithOffset(Offset
);
327 int64_t RealOffset
= Value
+ Addend
- FinalAddress
;
328 support::ulittle64_t::ref(Section
.getAddressWithOffset(Offset
)) =
330 LLVM_DEBUG(dbgs() << "Writing " << format("%p", RealOffset
) << " at "
331 << format("%p\n", FinalAddress
));
334 case ELF::R_X86_64_GOTOFF64
: {
335 // Compute Value - GOTBase.
336 uint64_t GOTBase
= 0;
337 for (const auto &Section
: Sections
) {
338 if (Section
.getName() == ".got") {
339 GOTBase
= Section
.getLoadAddressWithOffset(0);
343 assert(GOTBase
!= 0 && "missing GOT");
344 int64_t GOTOffset
= Value
- GOTBase
+ Addend
;
345 support::ulittle64_t::ref(Section
.getAddressWithOffset(Offset
)) = GOTOffset
;
351 void RuntimeDyldELF::resolveX86Relocation(const SectionEntry
&Section
,
352 uint64_t Offset
, uint32_t Value
,
353 uint32_t Type
, int32_t Addend
) {
355 case ELF::R_386_32
: {
356 support::ulittle32_t::ref(Section
.getAddressWithOffset(Offset
)) =
360 // Handle R_386_PLT32 like R_386_PC32 since it should be able to
361 // reach any 32 bit address.
362 case ELF::R_386_PLT32
:
363 case ELF::R_386_PC32
: {
364 uint32_t FinalAddress
=
365 Section
.getLoadAddressWithOffset(Offset
) & 0xFFFFFFFF;
366 uint32_t RealOffset
= Value
+ Addend
- FinalAddress
;
367 support::ulittle32_t::ref(Section
.getAddressWithOffset(Offset
)) =
372 // There are other relocation types, but it appears these are the
373 // only ones currently used by the LLVM ELF object writer
374 report_fatal_error("Relocation type not implemented yet!");
379 void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry
&Section
,
380 uint64_t Offset
, uint64_t Value
,
381 uint32_t Type
, int64_t Addend
) {
382 uint32_t *TargetPtr
=
383 reinterpret_cast<uint32_t *>(Section
.getAddressWithOffset(Offset
));
384 uint64_t FinalAddress
= Section
.getLoadAddressWithOffset(Offset
);
385 // Data should use target endian. Code should always use little endian.
386 bool isBE
= Arch
== Triple::aarch64_be
;
388 LLVM_DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
389 << format("%llx", Section
.getAddressWithOffset(Offset
))
390 << " FinalAddress: 0x" << format("%llx", FinalAddress
)
391 << " Value: 0x" << format("%llx", Value
) << " Type: 0x"
392 << format("%x", Type
) << " Addend: 0x"
393 << format("%llx", Addend
) << "\n");
397 report_fatal_error("Relocation type not implemented yet!");
399 case ELF::R_AARCH64_ABS16
: {
400 uint64_t Result
= Value
+ Addend
;
401 assert(static_cast<int64_t>(Result
) >= INT16_MIN
&& Result
< UINT16_MAX
);
402 write(isBE
, TargetPtr
, static_cast<uint16_t>(Result
& 0xffffU
));
405 case ELF::R_AARCH64_ABS32
: {
406 uint64_t Result
= Value
+ Addend
;
407 assert(static_cast<int64_t>(Result
) >= INT32_MIN
&& Result
< UINT32_MAX
);
408 write(isBE
, TargetPtr
, static_cast<uint32_t>(Result
& 0xffffffffU
));
411 case ELF::R_AARCH64_ABS64
:
412 write(isBE
, TargetPtr
, Value
+ Addend
);
414 case ELF::R_AARCH64_PLT32
: {
415 uint64_t Result
= Value
+ Addend
- FinalAddress
;
416 assert(static_cast<int64_t>(Result
) >= INT32_MIN
&&
417 static_cast<int64_t>(Result
) <= INT32_MAX
);
418 write(isBE
, TargetPtr
, static_cast<uint32_t>(Result
));
421 case ELF::R_AARCH64_PREL32
: {
422 uint64_t Result
= Value
+ Addend
- FinalAddress
;
423 assert(static_cast<int64_t>(Result
) >= INT32_MIN
&&
424 static_cast<int64_t>(Result
) <= UINT32_MAX
);
425 write(isBE
, TargetPtr
, static_cast<uint32_t>(Result
& 0xffffffffU
));
428 case ELF::R_AARCH64_PREL64
:
429 write(isBE
, TargetPtr
, Value
+ Addend
- FinalAddress
);
431 case ELF::R_AARCH64_CONDBR19
: {
432 uint64_t BranchImm
= Value
+ Addend
- FinalAddress
;
434 assert(isInt
<21>(BranchImm
));
435 *TargetPtr
&= 0xff00001fU
;
436 // Immediate:20:2 goes in bits 23:5 of Bcc, CBZ, CBNZ
437 or32le(TargetPtr
, (BranchImm
& 0x001FFFFC) << 3);
440 case ELF::R_AARCH64_TSTBR14
: {
441 uint64_t BranchImm
= Value
+ Addend
- FinalAddress
;
443 assert(isInt
<16>(BranchImm
));
445 *TargetPtr
&= 0xfff8001fU
;
446 // Immediate:15:2 goes in bits 18:5 of TBZ, TBNZ
447 or32le(TargetPtr
, (BranchImm
& 0x0FFFFFFC) << 3);
450 case ELF::R_AARCH64_CALL26
: // fallthrough
451 case ELF::R_AARCH64_JUMP26
: {
452 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the
454 uint64_t BranchImm
= Value
+ Addend
- FinalAddress
;
456 // "Check that -2^27 <= result < 2^27".
457 assert(isInt
<28>(BranchImm
));
458 or32le(TargetPtr
, (BranchImm
& 0x0FFFFFFC) >> 2);
461 case ELF::R_AARCH64_MOVW_UABS_G3
:
462 or32le(TargetPtr
, ((Value
+ Addend
) & 0xFFFF000000000000) >> 43);
464 case ELF::R_AARCH64_MOVW_UABS_G2_NC
:
465 or32le(TargetPtr
, ((Value
+ Addend
) & 0xFFFF00000000) >> 27);
467 case ELF::R_AARCH64_MOVW_UABS_G1_NC
:
468 or32le(TargetPtr
, ((Value
+ Addend
) & 0xFFFF0000) >> 11);
470 case ELF::R_AARCH64_MOVW_UABS_G0_NC
:
471 or32le(TargetPtr
, ((Value
+ Addend
) & 0xFFFF) << 5);
473 case ELF::R_AARCH64_ADR_PREL_PG_HI21
: {
474 // Operation: Page(S+A) - Page(P)
476 ((Value
+ Addend
) & ~0xfffULL
) - (FinalAddress
& ~0xfffULL
);
478 // Check that -2^32 <= X < 2^32
479 assert(isInt
<33>(Result
) && "overflow check failed for relocation");
481 // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken
482 // from bits 32:12 of X.
483 write32AArch64Addr(TargetPtr
, Result
>> 12);
486 case ELF::R_AARCH64_ADD_ABS_LO12_NC
:
488 // Immediate goes in bits 21:10 of LD/ST instruction, taken
489 // from bits 11:0 of X
490 or32AArch64Imm(TargetPtr
, Value
+ Addend
);
492 case ELF::R_AARCH64_LDST8_ABS_LO12_NC
:
494 // Immediate goes in bits 21:10 of LD/ST instruction, taken
495 // from bits 11:0 of X
496 or32AArch64Imm(TargetPtr
, getBits(Value
+ Addend
, 0, 11));
498 case ELF::R_AARCH64_LDST16_ABS_LO12_NC
:
500 // Immediate goes in bits 21:10 of LD/ST instruction, taken
501 // from bits 11:1 of X
502 or32AArch64Imm(TargetPtr
, getBits(Value
+ Addend
, 1, 11));
504 case ELF::R_AARCH64_LDST32_ABS_LO12_NC
:
506 // Immediate goes in bits 21:10 of LD/ST instruction, taken
507 // from bits 11:2 of X
508 or32AArch64Imm(TargetPtr
, getBits(Value
+ Addend
, 2, 11));
510 case ELF::R_AARCH64_LDST64_ABS_LO12_NC
:
512 // Immediate goes in bits 21:10 of LD/ST instruction, taken
513 // from bits 11:3 of X
514 or32AArch64Imm(TargetPtr
, getBits(Value
+ Addend
, 3, 11));
516 case ELF::R_AARCH64_LDST128_ABS_LO12_NC
:
518 // Immediate goes in bits 21:10 of LD/ST instruction, taken
519 // from bits 11:4 of X
520 or32AArch64Imm(TargetPtr
, getBits(Value
+ Addend
, 4, 11));
522 case ELF::R_AARCH64_LD_PREL_LO19
: {
523 // Operation: S + A - P
524 uint64_t Result
= Value
+ Addend
- FinalAddress
;
526 // "Check that -2^20 <= result < 2^20".
527 assert(isInt
<21>(Result
));
529 *TargetPtr
&= 0xff00001fU
;
530 // Immediate goes in bits 23:5 of LD imm instruction, taken
531 // from bits 20:2 of X
532 *TargetPtr
|= ((Result
& 0xffc) << (5 - 2));
535 case ELF::R_AARCH64_ADR_PREL_LO21
: {
536 // Operation: S + A - P
537 uint64_t Result
= Value
+ Addend
- FinalAddress
;
539 // "Check that -2^20 <= result < 2^20".
540 assert(isInt
<21>(Result
));
542 *TargetPtr
&= 0x9f00001fU
;
543 // Immediate goes in bits 23:5, 30:29 of ADR imm instruction, taken
544 // from bits 20:0 of X
545 *TargetPtr
|= ((Result
& 0xffc) << (5 - 2));
546 *TargetPtr
|= (Result
& 0x3) << 29;
552 void RuntimeDyldELF::resolveARMRelocation(const SectionEntry
&Section
,
553 uint64_t Offset
, uint32_t Value
,
554 uint32_t Type
, int32_t Addend
) {
555 // TODO: Add Thumb relocations.
556 uint32_t *TargetPtr
=
557 reinterpret_cast<uint32_t *>(Section
.getAddressWithOffset(Offset
));
558 uint32_t FinalAddress
= Section
.getLoadAddressWithOffset(Offset
) & 0xFFFFFFFF;
561 LLVM_DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
562 << Section
.getAddressWithOffset(Offset
)
563 << " FinalAddress: " << format("%p", FinalAddress
)
564 << " Value: " << format("%x", Value
)
565 << " Type: " << format("%x", Type
)
566 << " Addend: " << format("%x", Addend
) << "\n");
570 llvm_unreachable("Not implemented relocation type!");
572 case ELF::R_ARM_NONE
:
574 // Write a 31bit signed offset
575 case ELF::R_ARM_PREL31
:
576 support::ulittle32_t::ref
{TargetPtr
} =
577 (support::ulittle32_t::ref
{TargetPtr
} & 0x80000000) |
578 ((Value
- FinalAddress
) & ~0x80000000);
580 case ELF::R_ARM_TARGET1
:
581 case ELF::R_ARM_ABS32
:
582 support::ulittle32_t::ref
{TargetPtr
} = Value
;
584 // Write first 16 bit of 32 bit value to the mov instruction.
585 // Last 4 bit should be shifted.
586 case ELF::R_ARM_MOVW_ABS_NC
:
587 case ELF::R_ARM_MOVT_ABS
:
588 if (Type
== ELF::R_ARM_MOVW_ABS_NC
)
589 Value
= Value
& 0xFFFF;
590 else if (Type
== ELF::R_ARM_MOVT_ABS
)
591 Value
= (Value
>> 16) & 0xFFFF;
592 support::ulittle32_t::ref
{TargetPtr
} =
593 (support::ulittle32_t::ref
{TargetPtr
} & ~0x000F0FFF) | (Value
& 0xFFF) |
594 (((Value
>> 12) & 0xF) << 16);
596 // Write 24 bit relative value to the branch instruction.
597 case ELF::R_ARM_PC24
: // Fall through.
598 case ELF::R_ARM_CALL
: // Fall through.
599 case ELF::R_ARM_JUMP24
:
600 int32_t RelValue
= static_cast<int32_t>(Value
- FinalAddress
- 8);
601 RelValue
= (RelValue
& 0x03FFFFFC) >> 2;
602 assert((support::ulittle32_t::ref
{TargetPtr
} & 0xFFFFFF) == 0xFFFFFE);
603 support::ulittle32_t::ref
{TargetPtr
} =
604 (support::ulittle32_t::ref
{TargetPtr
} & 0xFF000000) | RelValue
;
609 void RuntimeDyldELF::setMipsABI(const ObjectFile
&Obj
) {
610 if (Arch
== Triple::UnknownArch
||
611 !StringRef(Triple::getArchTypePrefix(Arch
)).equals("mips")) {
612 IsMipsO32ABI
= false;
613 IsMipsN32ABI
= false;
614 IsMipsN64ABI
= false;
617 if (auto *E
= dyn_cast
<ELFObjectFileBase
>(&Obj
)) {
618 unsigned AbiVariant
= E
->getPlatformFlags();
619 IsMipsO32ABI
= AbiVariant
& ELF::EF_MIPS_ABI_O32
;
620 IsMipsN32ABI
= AbiVariant
& ELF::EF_MIPS_ABI2
;
622 IsMipsN64ABI
= Obj
.getFileFormatName().equals("elf64-mips");
625 // Return the .TOC. section and offset.
626 Error
RuntimeDyldELF::findPPC64TOCSection(const ELFObjectFileBase
&Obj
,
627 ObjSectionToIDMap
&LocalSections
,
628 RelocationValueRef
&Rel
) {
629 // Set a default SectionID in case we do not find a TOC section below.
630 // This may happen for references to TOC base base (sym@toc, .odp
631 // relocation) without a .toc directive. In this case just use the
632 // first section (which is usually the .odp) since the code won't
633 // reference the .toc base directly.
634 Rel
.SymbolName
= nullptr;
637 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
638 // order. The TOC starts where the first of these sections starts.
639 for (auto &Section
: Obj
.sections()) {
640 Expected
<StringRef
> NameOrErr
= Section
.getName();
642 return NameOrErr
.takeError();
643 StringRef SectionName
= *NameOrErr
;
645 if (SectionName
== ".got"
646 || SectionName
== ".toc"
647 || SectionName
== ".tocbss"
648 || SectionName
== ".plt") {
649 if (auto SectionIDOrErr
=
650 findOrEmitSection(Obj
, Section
, false, LocalSections
))
651 Rel
.SectionID
= *SectionIDOrErr
;
653 return SectionIDOrErr
.takeError();
658 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
659 // thus permitting a full 64 Kbytes segment.
662 return Error::success();
665 // Returns the sections and offset associated with the ODP entry referenced
667 Error
RuntimeDyldELF::findOPDEntrySection(const ELFObjectFileBase
&Obj
,
668 ObjSectionToIDMap
&LocalSections
,
669 RelocationValueRef
&Rel
) {
670 // Get the ELF symbol value (st_value) to compare with Relocation offset in
672 for (section_iterator si
= Obj
.section_begin(), se
= Obj
.section_end();
675 Expected
<section_iterator
> RelSecOrErr
= si
->getRelocatedSection();
677 report_fatal_error(toString(RelSecOrErr
.takeError()));
679 section_iterator RelSecI
= *RelSecOrErr
;
680 if (RelSecI
== Obj
.section_end())
683 Expected
<StringRef
> NameOrErr
= RelSecI
->getName();
685 return NameOrErr
.takeError();
686 StringRef RelSectionName
= *NameOrErr
;
688 if (RelSectionName
!= ".opd")
691 for (elf_relocation_iterator i
= si
->relocation_begin(),
692 e
= si
->relocation_end();
694 // The R_PPC64_ADDR64 relocation indicates the first field
696 uint64_t TypeFunc
= i
->getType();
697 if (TypeFunc
!= ELF::R_PPC64_ADDR64
) {
702 uint64_t TargetSymbolOffset
= i
->getOffset();
703 symbol_iterator TargetSymbol
= i
->getSymbol();
705 if (auto AddendOrErr
= i
->getAddend())
706 Addend
= *AddendOrErr
;
708 return AddendOrErr
.takeError();
714 // Just check if following relocation is a R_PPC64_TOC
715 uint64_t TypeTOC
= i
->getType();
716 if (TypeTOC
!= ELF::R_PPC64_TOC
)
719 // Finally compares the Symbol value and the target symbol offset
720 // to check if this .opd entry refers to the symbol the relocation
722 if (Rel
.Addend
!= (int64_t)TargetSymbolOffset
)
725 section_iterator TSI
= Obj
.section_end();
726 if (auto TSIOrErr
= TargetSymbol
->getSection())
729 return TSIOrErr
.takeError();
730 assert(TSI
!= Obj
.section_end() && "TSI should refer to a valid section");
732 bool IsCode
= TSI
->isText();
733 if (auto SectionIDOrErr
= findOrEmitSection(Obj
, *TSI
, IsCode
,
735 Rel
.SectionID
= *SectionIDOrErr
;
737 return SectionIDOrErr
.takeError();
738 Rel
.Addend
= (intptr_t)Addend
;
739 return Error::success();
742 llvm_unreachable("Attempting to get address of ODP entry!");
745 // Relocation masks following the #lo(value), #hi(value), #ha(value),
746 // #higher(value), #highera(value), #highest(value), and #highesta(value)
747 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
750 static inline uint16_t applyPPClo(uint64_t value
) { return value
& 0xffff; }
752 static inline uint16_t applyPPChi(uint64_t value
) {
753 return (value
>> 16) & 0xffff;
756 static inline uint16_t applyPPCha (uint64_t value
) {
757 return ((value
+ 0x8000) >> 16) & 0xffff;
760 static inline uint16_t applyPPChigher(uint64_t value
) {
761 return (value
>> 32) & 0xffff;
764 static inline uint16_t applyPPChighera (uint64_t value
) {
765 return ((value
+ 0x8000) >> 32) & 0xffff;
768 static inline uint16_t applyPPChighest(uint64_t value
) {
769 return (value
>> 48) & 0xffff;
772 static inline uint16_t applyPPChighesta (uint64_t value
) {
773 return ((value
+ 0x8000) >> 48) & 0xffff;
776 void RuntimeDyldELF::resolvePPC32Relocation(const SectionEntry
&Section
,
777 uint64_t Offset
, uint64_t Value
,
778 uint32_t Type
, int64_t Addend
) {
779 uint8_t *LocalAddress
= Section
.getAddressWithOffset(Offset
);
782 report_fatal_error("Relocation type not implemented yet!");
784 case ELF::R_PPC_ADDR16_LO
:
785 writeInt16BE(LocalAddress
, applyPPClo(Value
+ Addend
));
787 case ELF::R_PPC_ADDR16_HI
:
788 writeInt16BE(LocalAddress
, applyPPChi(Value
+ Addend
));
790 case ELF::R_PPC_ADDR16_HA
:
791 writeInt16BE(LocalAddress
, applyPPCha(Value
+ Addend
));
796 void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry
&Section
,
797 uint64_t Offset
, uint64_t Value
,
798 uint32_t Type
, int64_t Addend
) {
799 uint8_t *LocalAddress
= Section
.getAddressWithOffset(Offset
);
802 report_fatal_error("Relocation type not implemented yet!");
804 case ELF::R_PPC64_ADDR16
:
805 writeInt16BE(LocalAddress
, applyPPClo(Value
+ Addend
));
807 case ELF::R_PPC64_ADDR16_DS
:
808 writeInt16BE(LocalAddress
, applyPPClo(Value
+ Addend
) & ~3);
810 case ELF::R_PPC64_ADDR16_LO
:
811 writeInt16BE(LocalAddress
, applyPPClo(Value
+ Addend
));
813 case ELF::R_PPC64_ADDR16_LO_DS
:
814 writeInt16BE(LocalAddress
, applyPPClo(Value
+ Addend
) & ~3);
816 case ELF::R_PPC64_ADDR16_HI
:
817 case ELF::R_PPC64_ADDR16_HIGH
:
818 writeInt16BE(LocalAddress
, applyPPChi(Value
+ Addend
));
820 case ELF::R_PPC64_ADDR16_HA
:
821 case ELF::R_PPC64_ADDR16_HIGHA
:
822 writeInt16BE(LocalAddress
, applyPPCha(Value
+ Addend
));
824 case ELF::R_PPC64_ADDR16_HIGHER
:
825 writeInt16BE(LocalAddress
, applyPPChigher(Value
+ Addend
));
827 case ELF::R_PPC64_ADDR16_HIGHERA
:
828 writeInt16BE(LocalAddress
, applyPPChighera(Value
+ Addend
));
830 case ELF::R_PPC64_ADDR16_HIGHEST
:
831 writeInt16BE(LocalAddress
, applyPPChighest(Value
+ Addend
));
833 case ELF::R_PPC64_ADDR16_HIGHESTA
:
834 writeInt16BE(LocalAddress
, applyPPChighesta(Value
+ Addend
));
836 case ELF::R_PPC64_ADDR14
: {
837 assert(((Value
+ Addend
) & 3) == 0);
838 // Preserve the AA/LK bits in the branch instruction
839 uint8_t aalk
= *(LocalAddress
+ 3);
840 writeInt16BE(LocalAddress
+ 2, (aalk
& 3) | ((Value
+ Addend
) & 0xfffc));
842 case ELF::R_PPC64_REL16_LO
: {
843 uint64_t FinalAddress
= Section
.getLoadAddressWithOffset(Offset
);
844 uint64_t Delta
= Value
- FinalAddress
+ Addend
;
845 writeInt16BE(LocalAddress
, applyPPClo(Delta
));
847 case ELF::R_PPC64_REL16_HI
: {
848 uint64_t FinalAddress
= Section
.getLoadAddressWithOffset(Offset
);
849 uint64_t Delta
= Value
- FinalAddress
+ Addend
;
850 writeInt16BE(LocalAddress
, applyPPChi(Delta
));
852 case ELF::R_PPC64_REL16_HA
: {
853 uint64_t FinalAddress
= Section
.getLoadAddressWithOffset(Offset
);
854 uint64_t Delta
= Value
- FinalAddress
+ Addend
;
855 writeInt16BE(LocalAddress
, applyPPCha(Delta
));
857 case ELF::R_PPC64_ADDR32
: {
858 int64_t Result
= static_cast<int64_t>(Value
+ Addend
);
859 if (SignExtend64
<32>(Result
) != Result
)
860 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow");
861 writeInt32BE(LocalAddress
, Result
);
863 case ELF::R_PPC64_REL24
: {
864 uint64_t FinalAddress
= Section
.getLoadAddressWithOffset(Offset
);
865 int64_t delta
= static_cast<int64_t>(Value
- FinalAddress
+ Addend
);
866 if (SignExtend64
<26>(delta
) != delta
)
867 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
868 // We preserve bits other than LI field, i.e. PO and AA/LK fields.
869 uint32_t Inst
= readBytesUnaligned(LocalAddress
, 4);
870 writeInt32BE(LocalAddress
, (Inst
& 0xFC000003) | (delta
& 0x03FFFFFC));
872 case ELF::R_PPC64_REL32
: {
873 uint64_t FinalAddress
= Section
.getLoadAddressWithOffset(Offset
);
874 int64_t delta
= static_cast<int64_t>(Value
- FinalAddress
+ Addend
);
875 if (SignExtend64
<32>(delta
) != delta
)
876 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
877 writeInt32BE(LocalAddress
, delta
);
879 case ELF::R_PPC64_REL64
: {
880 uint64_t FinalAddress
= Section
.getLoadAddressWithOffset(Offset
);
881 uint64_t Delta
= Value
- FinalAddress
+ Addend
;
882 writeInt64BE(LocalAddress
, Delta
);
884 case ELF::R_PPC64_ADDR64
:
885 writeInt64BE(LocalAddress
, Value
+ Addend
);
890 void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry
&Section
,
891 uint64_t Offset
, uint64_t Value
,
892 uint32_t Type
, int64_t Addend
) {
893 uint8_t *LocalAddress
= Section
.getAddressWithOffset(Offset
);
896 report_fatal_error("Relocation type not implemented yet!");
898 case ELF::R_390_PC16DBL
:
899 case ELF::R_390_PLT16DBL
: {
900 int64_t Delta
= (Value
+ Addend
) - Section
.getLoadAddressWithOffset(Offset
);
901 assert(int16_t(Delta
/ 2) * 2 == Delta
&& "R_390_PC16DBL overflow");
902 writeInt16BE(LocalAddress
, Delta
/ 2);
905 case ELF::R_390_PC32DBL
:
906 case ELF::R_390_PLT32DBL
: {
907 int64_t Delta
= (Value
+ Addend
) - Section
.getLoadAddressWithOffset(Offset
);
908 assert(int32_t(Delta
/ 2) * 2 == Delta
&& "R_390_PC32DBL overflow");
909 writeInt32BE(LocalAddress
, Delta
/ 2);
912 case ELF::R_390_PC16
: {
913 int64_t Delta
= (Value
+ Addend
) - Section
.getLoadAddressWithOffset(Offset
);
914 assert(int16_t(Delta
) == Delta
&& "R_390_PC16 overflow");
915 writeInt16BE(LocalAddress
, Delta
);
918 case ELF::R_390_PC32
: {
919 int64_t Delta
= (Value
+ Addend
) - Section
.getLoadAddressWithOffset(Offset
);
920 assert(int32_t(Delta
) == Delta
&& "R_390_PC32 overflow");
921 writeInt32BE(LocalAddress
, Delta
);
924 case ELF::R_390_PC64
: {
925 int64_t Delta
= (Value
+ Addend
) - Section
.getLoadAddressWithOffset(Offset
);
926 writeInt64BE(LocalAddress
, Delta
);
930 *LocalAddress
= (uint8_t)(Value
+ Addend
);
933 writeInt16BE(LocalAddress
, Value
+ Addend
);
936 writeInt32BE(LocalAddress
, Value
+ Addend
);
939 writeInt64BE(LocalAddress
, Value
+ Addend
);
944 void RuntimeDyldELF::resolveBPFRelocation(const SectionEntry
&Section
,
945 uint64_t Offset
, uint64_t Value
,
946 uint32_t Type
, int64_t Addend
) {
947 bool isBE
= Arch
== Triple::bpfeb
;
951 report_fatal_error("Relocation type not implemented yet!");
953 case ELF::R_BPF_NONE
:
954 case ELF::R_BPF_64_64
:
955 case ELF::R_BPF_64_32
:
956 case ELF::R_BPF_64_NODYLD32
:
958 case ELF::R_BPF_64_ABS64
: {
959 write(isBE
, Section
.getAddressWithOffset(Offset
), Value
+ Addend
);
960 LLVM_DEBUG(dbgs() << "Writing " << format("%p", (Value
+ Addend
)) << " at "
961 << format("%p\n", Section
.getAddressWithOffset(Offset
)));
964 case ELF::R_BPF_64_ABS32
: {
966 assert(Value
<= UINT32_MAX
);
967 write(isBE
, Section
.getAddressWithOffset(Offset
), static_cast<uint32_t>(Value
));
968 LLVM_DEBUG(dbgs() << "Writing " << format("%p", Value
) << " at "
969 << format("%p\n", Section
.getAddressWithOffset(Offset
)));
975 // The target location for the relocation is described by RE.SectionID and
976 // RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
977 // SectionEntry has three members describing its location.
978 // SectionEntry::Address is the address at which the section has been loaded
979 // into memory in the current (host) process. SectionEntry::LoadAddress is the
980 // address that the section will have in the target process.
981 // SectionEntry::ObjAddress is the address of the bits for this section in the
982 // original emitted object image (also in the current address space).
984 // Relocations will be applied as if the section were loaded at
985 // SectionEntry::LoadAddress, but they will be applied at an address based
986 // on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
987 // Target memory contents if they are required for value calculations.
989 // The Value parameter here is the load address of the symbol for the
990 // relocation to be applied. For relocations which refer to symbols in the
991 // current object Value will be the LoadAddress of the section in which
992 // the symbol resides (RE.Addend provides additional information about the
993 // symbol location). For external symbols, Value will be the address of the
994 // symbol in the target address space.
995 void RuntimeDyldELF::resolveRelocation(const RelocationEntry
&RE
,
997 const SectionEntry
&Section
= Sections
[RE
.SectionID
];
998 return resolveRelocation(Section
, RE
.Offset
, Value
, RE
.RelType
, RE
.Addend
,
999 RE
.SymOffset
, RE
.SectionID
);
1002 void RuntimeDyldELF::resolveRelocation(const SectionEntry
&Section
,
1003 uint64_t Offset
, uint64_t Value
,
1004 uint32_t Type
, int64_t Addend
,
1005 uint64_t SymOffset
, SID SectionID
) {
1007 case Triple::x86_64
:
1008 resolveX86_64Relocation(Section
, Offset
, Value
, Type
, Addend
, SymOffset
);
1011 resolveX86Relocation(Section
, Offset
, (uint32_t)(Value
& 0xffffffffL
), Type
,
1012 (uint32_t)(Addend
& 0xffffffffL
));
1014 case Triple::aarch64
:
1015 case Triple::aarch64_be
:
1016 resolveAArch64Relocation(Section
, Offset
, Value
, Type
, Addend
);
1018 case Triple::arm
: // Fall through.
1021 case Triple::thumbeb
:
1022 resolveARMRelocation(Section
, Offset
, (uint32_t)(Value
& 0xffffffffL
), Type
,
1023 (uint32_t)(Addend
& 0xffffffffL
));
1025 case Triple::ppc
: // Fall through.
1027 resolvePPC32Relocation(Section
, Offset
, Value
, Type
, Addend
);
1029 case Triple::ppc64
: // Fall through.
1030 case Triple::ppc64le
:
1031 resolvePPC64Relocation(Section
, Offset
, Value
, Type
, Addend
);
1033 case Triple::systemz
:
1034 resolveSystemZRelocation(Section
, Offset
, Value
, Type
, Addend
);
1038 resolveBPFRelocation(Section
, Offset
, Value
, Type
, Addend
);
1041 llvm_unreachable("Unsupported CPU type!");
1045 void *RuntimeDyldELF::computePlaceholderAddress(unsigned SectionID
, uint64_t Offset
) const {
1046 return (void *)(Sections
[SectionID
].getObjAddress() + Offset
);
1049 void RuntimeDyldELF::processSimpleRelocation(unsigned SectionID
, uint64_t Offset
, unsigned RelType
, RelocationValueRef Value
) {
1050 RelocationEntry
RE(SectionID
, Offset
, RelType
, Value
.Addend
, Value
.Offset
);
1051 if (Value
.SymbolName
)
1052 addRelocationForSymbol(RE
, Value
.SymbolName
);
1054 addRelocationForSection(RE
, Value
.SectionID
);
1057 uint32_t RuntimeDyldELF::getMatchingLoRelocation(uint32_t RelType
,
1058 bool IsLocal
) const {
1060 case ELF::R_MICROMIPS_GOT16
:
1062 return ELF::R_MICROMIPS_LO16
;
1064 case ELF::R_MICROMIPS_HI16
:
1065 return ELF::R_MICROMIPS_LO16
;
1066 case ELF::R_MIPS_GOT16
:
1068 return ELF::R_MIPS_LO16
;
1070 case ELF::R_MIPS_HI16
:
1071 return ELF::R_MIPS_LO16
;
1072 case ELF::R_MIPS_PCHI16
:
1073 return ELF::R_MIPS_PCLO16
;
1077 return ELF::R_MIPS_NONE
;
1080 // Sometimes we don't need to create thunk for a branch.
1081 // This typically happens when branch target is located
1082 // in the same object file. In such case target is either
1083 // a weak symbol or symbol in a different executable section.
1084 // This function checks if branch target is located in the
1085 // same object file and if distance between source and target
1086 // fits R_AARCH64_CALL26 relocation. If both conditions are
1087 // met, it emits direct jump to the target and returns true.
1088 // Otherwise false is returned and thunk is created.
1089 bool RuntimeDyldELF::resolveAArch64ShortBranch(
1090 unsigned SectionID
, relocation_iterator RelI
,
1091 const RelocationValueRef
&Value
) {
1093 if (Value
.SymbolName
) {
1094 auto Loc
= GlobalSymbolTable
.find(Value
.SymbolName
);
1096 // Don't create direct branch for external symbols.
1097 if (Loc
== GlobalSymbolTable
.end())
1100 const auto &SymInfo
= Loc
->second
;
1102 uint64_t(Sections
[SymInfo
.getSectionID()].getLoadAddressWithOffset(
1103 SymInfo
.getOffset()));
1105 Address
= uint64_t(Sections
[Value
.SectionID
].getLoadAddress());
1107 uint64_t Offset
= RelI
->getOffset();
1108 uint64_t SourceAddress
= Sections
[SectionID
].getLoadAddressWithOffset(Offset
);
1110 // R_AARCH64_CALL26 requires immediate to be in range -2^27 <= imm < 2^27
1111 // If distance between source and target is out of range then we should
1113 if (!isInt
<28>(Address
+ Value
.Addend
- SourceAddress
))
1116 resolveRelocation(Sections
[SectionID
], Offset
, Address
, RelI
->getType(),
1122 void RuntimeDyldELF::resolveAArch64Branch(unsigned SectionID
,
1123 const RelocationValueRef
&Value
,
1124 relocation_iterator RelI
,
1127 LLVM_DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation.");
1128 SectionEntry
&Section
= Sections
[SectionID
];
1130 uint64_t Offset
= RelI
->getOffset();
1131 unsigned RelType
= RelI
->getType();
1132 // Look for an existing stub.
1133 StubMap::const_iterator i
= Stubs
.find(Value
);
1134 if (i
!= Stubs
.end()) {
1135 resolveRelocation(Section
, Offset
,
1136 (uint64_t)Section
.getAddressWithOffset(i
->second
),
1138 LLVM_DEBUG(dbgs() << " Stub function found\n");
1139 } else if (!resolveAArch64ShortBranch(SectionID
, RelI
, Value
)) {
1140 // Create a new stub function.
1141 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1142 Stubs
[Value
] = Section
.getStubOffset();
1143 uint8_t *StubTargetAddr
= createStubFunction(
1144 Section
.getAddressWithOffset(Section
.getStubOffset()));
1146 RelocationEntry
REmovz_g3(SectionID
, StubTargetAddr
- Section
.getAddress(),
1147 ELF::R_AARCH64_MOVW_UABS_G3
, Value
.Addend
);
1148 RelocationEntry
REmovk_g2(SectionID
,
1149 StubTargetAddr
- Section
.getAddress() + 4,
1150 ELF::R_AARCH64_MOVW_UABS_G2_NC
, Value
.Addend
);
1151 RelocationEntry
REmovk_g1(SectionID
,
1152 StubTargetAddr
- Section
.getAddress() + 8,
1153 ELF::R_AARCH64_MOVW_UABS_G1_NC
, Value
.Addend
);
1154 RelocationEntry
REmovk_g0(SectionID
,
1155 StubTargetAddr
- Section
.getAddress() + 12,
1156 ELF::R_AARCH64_MOVW_UABS_G0_NC
, Value
.Addend
);
1158 if (Value
.SymbolName
) {
1159 addRelocationForSymbol(REmovz_g3
, Value
.SymbolName
);
1160 addRelocationForSymbol(REmovk_g2
, Value
.SymbolName
);
1161 addRelocationForSymbol(REmovk_g1
, Value
.SymbolName
);
1162 addRelocationForSymbol(REmovk_g0
, Value
.SymbolName
);
1164 addRelocationForSection(REmovz_g3
, Value
.SectionID
);
1165 addRelocationForSection(REmovk_g2
, Value
.SectionID
);
1166 addRelocationForSection(REmovk_g1
, Value
.SectionID
);
1167 addRelocationForSection(REmovk_g0
, Value
.SectionID
);
1169 resolveRelocation(Section
, Offset
,
1170 reinterpret_cast<uint64_t>(Section
.getAddressWithOffset(
1171 Section
.getStubOffset())),
1173 Section
.advanceStubOffset(getMaxStubSize());
1177 Expected
<relocation_iterator
>
1178 RuntimeDyldELF::processRelocationRef(
1179 unsigned SectionID
, relocation_iterator RelI
, const ObjectFile
&O
,
1180 ObjSectionToIDMap
&ObjSectionToID
, StubMap
&Stubs
) {
1181 const auto &Obj
= cast
<ELFObjectFileBase
>(O
);
1182 uint64_t RelType
= RelI
->getType();
1184 if (Expected
<int64_t> AddendOrErr
= ELFRelocationRef(*RelI
).getAddend())
1185 Addend
= *AddendOrErr
;
1187 consumeError(AddendOrErr
.takeError());
1188 elf_symbol_iterator Symbol
= RelI
->getSymbol();
1190 // Obtain the symbol name which is referenced in the relocation
1191 StringRef TargetName
;
1192 if (Symbol
!= Obj
.symbol_end()) {
1193 if (auto TargetNameOrErr
= Symbol
->getName())
1194 TargetName
= *TargetNameOrErr
;
1196 return TargetNameOrErr
.takeError();
1198 LLVM_DEBUG(dbgs() << "\t\tRelType: " << RelType
<< " Addend: " << Addend
1199 << " TargetName: " << TargetName
<< "\n");
1200 RelocationValueRef Value
;
1201 // First search for the symbol in the local symbol table
1202 SymbolRef::Type SymType
= SymbolRef::ST_Unknown
;
1204 // Search for the symbol in the global symbol table
1205 RTDyldSymbolTable::const_iterator gsi
= GlobalSymbolTable
.end();
1206 if (Symbol
!= Obj
.symbol_end()) {
1207 gsi
= GlobalSymbolTable
.find(TargetName
.data());
1208 Expected
<SymbolRef::Type
> SymTypeOrErr
= Symbol
->getType();
1209 if (!SymTypeOrErr
) {
1211 raw_string_ostream
OS(Buf
);
1212 logAllUnhandledErrors(SymTypeOrErr
.takeError(), OS
);
1214 report_fatal_error(Buf
);
1216 SymType
= *SymTypeOrErr
;
1218 if (gsi
!= GlobalSymbolTable
.end()) {
1219 const auto &SymInfo
= gsi
->second
;
1220 Value
.SectionID
= SymInfo
.getSectionID();
1221 Value
.Offset
= SymInfo
.getOffset();
1222 Value
.Addend
= SymInfo
.getOffset() + Addend
;
1225 case SymbolRef::ST_Debug
: {
1226 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
1227 // and can be changed by another developers. Maybe best way is add
1228 // a new symbol type ST_Section to SymbolRef and use it.
1229 auto SectionOrErr
= Symbol
->getSection();
1230 if (!SectionOrErr
) {
1232 raw_string_ostream
OS(Buf
);
1233 logAllUnhandledErrors(SectionOrErr
.takeError(), OS
);
1235 report_fatal_error(Buf
);
1237 section_iterator si
= *SectionOrErr
;
1238 if (si
== Obj
.section_end())
1239 llvm_unreachable("Symbol section not found, bad object file format!");
1240 LLVM_DEBUG(dbgs() << "\t\tThis is section symbol\n");
1241 bool isCode
= si
->isText();
1242 if (auto SectionIDOrErr
= findOrEmitSection(Obj
, (*si
), isCode
,
1244 Value
.SectionID
= *SectionIDOrErr
;
1246 return SectionIDOrErr
.takeError();
1247 Value
.Addend
= Addend
;
1250 case SymbolRef::ST_Data
:
1251 case SymbolRef::ST_Function
:
1252 case SymbolRef::ST_Unknown
: {
1253 Value
.SymbolName
= TargetName
.data();
1254 Value
.Addend
= Addend
;
1256 // Absolute relocations will have a zero symbol ID (STN_UNDEF), which
1257 // will manifest here as a NULL symbol name.
1258 // We can set this as a valid (but empty) symbol name, and rely
1259 // on addRelocationForSymbol to handle this.
1260 if (!Value
.SymbolName
)
1261 Value
.SymbolName
= "";
1265 llvm_unreachable("Unresolved symbol type!");
1270 uint64_t Offset
= RelI
->getOffset();
1272 LLVM_DEBUG(dbgs() << "\t\tSectionID: " << SectionID
<< " Offset: " << Offset
1274 if ((Arch
== Triple::aarch64
|| Arch
== Triple::aarch64_be
)) {
1275 if ((RelType
== ELF::R_AARCH64_CALL26
||
1276 RelType
== ELF::R_AARCH64_JUMP26
) &&
1277 MemMgr
.allowStubAllocation()) {
1278 resolveAArch64Branch(SectionID
, Value
, RelI
, Stubs
);
1279 } else if (RelType
== ELF::R_AARCH64_ADR_GOT_PAGE
) {
1280 // Craete new GOT entry or find existing one. If GOT entry is
1281 // to be created, then we also emit ABS64 relocation for it.
1282 uint64_t GOTOffset
= findOrAllocGOTEntry(Value
, ELF::R_AARCH64_ABS64
);
1283 resolveGOTOffsetRelocation(SectionID
, Offset
, GOTOffset
+ Addend
,
1284 ELF::R_AARCH64_ADR_PREL_PG_HI21
);
1286 } else if (RelType
== ELF::R_AARCH64_LD64_GOT_LO12_NC
) {
1287 uint64_t GOTOffset
= findOrAllocGOTEntry(Value
, ELF::R_AARCH64_ABS64
);
1288 resolveGOTOffsetRelocation(SectionID
, Offset
, GOTOffset
+ Addend
,
1289 ELF::R_AARCH64_LDST64_ABS_LO12_NC
);
1291 processSimpleRelocation(SectionID
, Offset
, RelType
, Value
);
1293 } else if (Arch
== Triple::arm
) {
1294 if (RelType
== ELF::R_ARM_PC24
|| RelType
== ELF::R_ARM_CALL
||
1295 RelType
== ELF::R_ARM_JUMP24
) {
1296 // This is an ARM branch relocation, need to use a stub function.
1297 LLVM_DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.\n");
1298 SectionEntry
&Section
= Sections
[SectionID
];
1300 // Look for an existing stub.
1301 StubMap::const_iterator i
= Stubs
.find(Value
);
1302 if (i
!= Stubs
.end()) {
1305 reinterpret_cast<uint64_t>(Section
.getAddressWithOffset(i
->second
)),
1307 LLVM_DEBUG(dbgs() << " Stub function found\n");
1309 // Create a new stub function.
1310 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1311 Stubs
[Value
] = Section
.getStubOffset();
1312 uint8_t *StubTargetAddr
= createStubFunction(
1313 Section
.getAddressWithOffset(Section
.getStubOffset()));
1314 RelocationEntry
RE(SectionID
, StubTargetAddr
- Section
.getAddress(),
1315 ELF::R_ARM_ABS32
, Value
.Addend
);
1316 if (Value
.SymbolName
)
1317 addRelocationForSymbol(RE
, Value
.SymbolName
);
1319 addRelocationForSection(RE
, Value
.SectionID
);
1321 resolveRelocation(Section
, Offset
, reinterpret_cast<uint64_t>(
1322 Section
.getAddressWithOffset(
1323 Section
.getStubOffset())),
1325 Section
.advanceStubOffset(getMaxStubSize());
1328 uint32_t *Placeholder
=
1329 reinterpret_cast<uint32_t*>(computePlaceholderAddress(SectionID
, Offset
));
1330 if (RelType
== ELF::R_ARM_PREL31
|| RelType
== ELF::R_ARM_TARGET1
||
1331 RelType
== ELF::R_ARM_ABS32
) {
1332 Value
.Addend
+= *Placeholder
;
1333 } else if (RelType
== ELF::R_ARM_MOVW_ABS_NC
|| RelType
== ELF::R_ARM_MOVT_ABS
) {
1334 // See ELF for ARM documentation
1335 Value
.Addend
+= (int16_t)((*Placeholder
& 0xFFF) | (((*Placeholder
>> 16) & 0xF) << 12));
1337 processSimpleRelocation(SectionID
, Offset
, RelType
, Value
);
1339 } else if (IsMipsO32ABI
) {
1340 uint8_t *Placeholder
= reinterpret_cast<uint8_t *>(
1341 computePlaceholderAddress(SectionID
, Offset
));
1342 uint32_t Opcode
= readBytesUnaligned(Placeholder
, 4);
1343 if (RelType
== ELF::R_MIPS_26
) {
1344 // This is an Mips branch relocation, need to use a stub function.
1345 LLVM_DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
1346 SectionEntry
&Section
= Sections
[SectionID
];
1348 // Extract the addend from the instruction.
1349 // We shift up by two since the Value will be down shifted again
1350 // when applying the relocation.
1351 uint32_t Addend
= (Opcode
& 0x03ffffff) << 2;
1353 Value
.Addend
+= Addend
;
1355 // Look up for existing stub.
1356 StubMap::const_iterator i
= Stubs
.find(Value
);
1357 if (i
!= Stubs
.end()) {
1358 RelocationEntry
RE(SectionID
, Offset
, RelType
, i
->second
);
1359 addRelocationForSection(RE
, SectionID
);
1360 LLVM_DEBUG(dbgs() << " Stub function found\n");
1362 // Create a new stub function.
1363 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1364 Stubs
[Value
] = Section
.getStubOffset();
1366 unsigned AbiVariant
= Obj
.getPlatformFlags();
1368 uint8_t *StubTargetAddr
= createStubFunction(
1369 Section
.getAddressWithOffset(Section
.getStubOffset()), AbiVariant
);
1371 // Creating Hi and Lo relocations for the filled stub instructions.
1372 RelocationEntry
REHi(SectionID
, StubTargetAddr
- Section
.getAddress(),
1373 ELF::R_MIPS_HI16
, Value
.Addend
);
1374 RelocationEntry
RELo(SectionID
,
1375 StubTargetAddr
- Section
.getAddress() + 4,
1376 ELF::R_MIPS_LO16
, Value
.Addend
);
1378 if (Value
.SymbolName
) {
1379 addRelocationForSymbol(REHi
, Value
.SymbolName
);
1380 addRelocationForSymbol(RELo
, Value
.SymbolName
);
1382 addRelocationForSection(REHi
, Value
.SectionID
);
1383 addRelocationForSection(RELo
, Value
.SectionID
);
1386 RelocationEntry
RE(SectionID
, Offset
, RelType
, Section
.getStubOffset());
1387 addRelocationForSection(RE
, SectionID
);
1388 Section
.advanceStubOffset(getMaxStubSize());
1390 } else if (RelType
== ELF::R_MIPS_HI16
|| RelType
== ELF::R_MIPS_PCHI16
) {
1391 int64_t Addend
= (Opcode
& 0x0000ffff) << 16;
1392 RelocationEntry
RE(SectionID
, Offset
, RelType
, Addend
);
1393 PendingRelocs
.push_back(std::make_pair(Value
, RE
));
1394 } else if (RelType
== ELF::R_MIPS_LO16
|| RelType
== ELF::R_MIPS_PCLO16
) {
1395 int64_t Addend
= Value
.Addend
+ SignExtend32
<16>(Opcode
& 0x0000ffff);
1396 for (auto I
= PendingRelocs
.begin(); I
!= PendingRelocs
.end();) {
1397 const RelocationValueRef
&MatchingValue
= I
->first
;
1398 RelocationEntry
&Reloc
= I
->second
;
1399 if (MatchingValue
== Value
&&
1400 RelType
== getMatchingLoRelocation(Reloc
.RelType
) &&
1401 SectionID
== Reloc
.SectionID
) {
1402 Reloc
.Addend
+= Addend
;
1403 if (Value
.SymbolName
)
1404 addRelocationForSymbol(Reloc
, Value
.SymbolName
);
1406 addRelocationForSection(Reloc
, Value
.SectionID
);
1407 I
= PendingRelocs
.erase(I
);
1411 RelocationEntry
RE(SectionID
, Offset
, RelType
, Addend
);
1412 if (Value
.SymbolName
)
1413 addRelocationForSymbol(RE
, Value
.SymbolName
);
1415 addRelocationForSection(RE
, Value
.SectionID
);
1417 if (RelType
== ELF::R_MIPS_32
)
1418 Value
.Addend
+= Opcode
;
1419 else if (RelType
== ELF::R_MIPS_PC16
)
1420 Value
.Addend
+= SignExtend32
<18>((Opcode
& 0x0000ffff) << 2);
1421 else if (RelType
== ELF::R_MIPS_PC19_S2
)
1422 Value
.Addend
+= SignExtend32
<21>((Opcode
& 0x0007ffff) << 2);
1423 else if (RelType
== ELF::R_MIPS_PC21_S2
)
1424 Value
.Addend
+= SignExtend32
<23>((Opcode
& 0x001fffff) << 2);
1425 else if (RelType
== ELF::R_MIPS_PC26_S2
)
1426 Value
.Addend
+= SignExtend32
<28>((Opcode
& 0x03ffffff) << 2);
1427 processSimpleRelocation(SectionID
, Offset
, RelType
, Value
);
1429 } else if (IsMipsN32ABI
|| IsMipsN64ABI
) {
1430 uint32_t r_type
= RelType
& 0xff;
1431 RelocationEntry
RE(SectionID
, Offset
, RelType
, Value
.Addend
);
1432 if (r_type
== ELF::R_MIPS_CALL16
|| r_type
== ELF::R_MIPS_GOT_PAGE
1433 || r_type
== ELF::R_MIPS_GOT_DISP
) {
1434 StringMap
<uint64_t>::iterator i
= GOTSymbolOffsets
.find(TargetName
);
1435 if (i
!= GOTSymbolOffsets
.end())
1436 RE
.SymOffset
= i
->second
;
1438 RE
.SymOffset
= allocateGOTEntries(1);
1439 GOTSymbolOffsets
[TargetName
] = RE
.SymOffset
;
1441 if (Value
.SymbolName
)
1442 addRelocationForSymbol(RE
, Value
.SymbolName
);
1444 addRelocationForSection(RE
, Value
.SectionID
);
1445 } else if (RelType
== ELF::R_MIPS_26
) {
1446 // This is an Mips branch relocation, need to use a stub function.
1447 LLVM_DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
1448 SectionEntry
&Section
= Sections
[SectionID
];
1450 // Look up for existing stub.
1451 StubMap::const_iterator i
= Stubs
.find(Value
);
1452 if (i
!= Stubs
.end()) {
1453 RelocationEntry
RE(SectionID
, Offset
, RelType
, i
->second
);
1454 addRelocationForSection(RE
, SectionID
);
1455 LLVM_DEBUG(dbgs() << " Stub function found\n");
1457 // Create a new stub function.
1458 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1459 Stubs
[Value
] = Section
.getStubOffset();
1461 unsigned AbiVariant
= Obj
.getPlatformFlags();
1463 uint8_t *StubTargetAddr
= createStubFunction(
1464 Section
.getAddressWithOffset(Section
.getStubOffset()), AbiVariant
);
1467 // Creating Hi and Lo relocations for the filled stub instructions.
1468 RelocationEntry
REHi(SectionID
, StubTargetAddr
- Section
.getAddress(),
1469 ELF::R_MIPS_HI16
, Value
.Addend
);
1470 RelocationEntry
RELo(SectionID
,
1471 StubTargetAddr
- Section
.getAddress() + 4,
1472 ELF::R_MIPS_LO16
, Value
.Addend
);
1473 if (Value
.SymbolName
) {
1474 addRelocationForSymbol(REHi
, Value
.SymbolName
);
1475 addRelocationForSymbol(RELo
, Value
.SymbolName
);
1477 addRelocationForSection(REHi
, Value
.SectionID
);
1478 addRelocationForSection(RELo
, Value
.SectionID
);
1481 // Creating Highest, Higher, Hi and Lo relocations for the filled stub
1483 RelocationEntry
REHighest(SectionID
,
1484 StubTargetAddr
- Section
.getAddress(),
1485 ELF::R_MIPS_HIGHEST
, Value
.Addend
);
1486 RelocationEntry
REHigher(SectionID
,
1487 StubTargetAddr
- Section
.getAddress() + 4,
1488 ELF::R_MIPS_HIGHER
, Value
.Addend
);
1489 RelocationEntry
REHi(SectionID
,
1490 StubTargetAddr
- Section
.getAddress() + 12,
1491 ELF::R_MIPS_HI16
, Value
.Addend
);
1492 RelocationEntry
RELo(SectionID
,
1493 StubTargetAddr
- Section
.getAddress() + 20,
1494 ELF::R_MIPS_LO16
, Value
.Addend
);
1495 if (Value
.SymbolName
) {
1496 addRelocationForSymbol(REHighest
, Value
.SymbolName
);
1497 addRelocationForSymbol(REHigher
, Value
.SymbolName
);
1498 addRelocationForSymbol(REHi
, Value
.SymbolName
);
1499 addRelocationForSymbol(RELo
, Value
.SymbolName
);
1501 addRelocationForSection(REHighest
, Value
.SectionID
);
1502 addRelocationForSection(REHigher
, Value
.SectionID
);
1503 addRelocationForSection(REHi
, Value
.SectionID
);
1504 addRelocationForSection(RELo
, Value
.SectionID
);
1507 RelocationEntry
RE(SectionID
, Offset
, RelType
, Section
.getStubOffset());
1508 addRelocationForSection(RE
, SectionID
);
1509 Section
.advanceStubOffset(getMaxStubSize());
1512 processSimpleRelocation(SectionID
, Offset
, RelType
, Value
);
1515 } else if (Arch
== Triple::ppc64
|| Arch
== Triple::ppc64le
) {
1516 if (RelType
== ELF::R_PPC64_REL24
) {
1517 // Determine ABI variant in use for this object.
1518 unsigned AbiVariant
= Obj
.getPlatformFlags();
1519 AbiVariant
&= ELF::EF_PPC64_ABI
;
1520 // A PPC branch relocation will need a stub function if the target is
1521 // an external symbol (either Value.SymbolName is set, or SymType is
1522 // Symbol::ST_Unknown) or if the target address is not within the
1523 // signed 24-bits branch address.
1524 SectionEntry
&Section
= Sections
[SectionID
];
1525 uint8_t *Target
= Section
.getAddressWithOffset(Offset
);
1526 bool RangeOverflow
= false;
1527 bool IsExtern
= Value
.SymbolName
|| SymType
== SymbolRef::ST_Unknown
;
1529 if (AbiVariant
!= 2) {
1530 // In the ELFv1 ABI, a function call may point to the .opd entry,
1531 // so the final symbol value is calculated based on the relocation
1532 // values in the .opd section.
1533 if (auto Err
= findOPDEntrySection(Obj
, ObjSectionToID
, Value
))
1534 return std::move(Err
);
1536 // In the ELFv2 ABI, a function symbol may provide a local entry
1537 // point, which must be used for direct calls.
1538 if (Value
.SectionID
== SectionID
){
1539 uint8_t SymOther
= Symbol
->getOther();
1540 Value
.Addend
+= ELF::decodePPC64LocalEntryOffset(SymOther
);
1543 uint8_t *RelocTarget
=
1544 Sections
[Value
.SectionID
].getAddressWithOffset(Value
.Addend
);
1545 int64_t delta
= static_cast<int64_t>(Target
- RelocTarget
);
1546 // If it is within 26-bits branch range, just set the branch target
1547 if (SignExtend64
<26>(delta
) != delta
) {
1548 RangeOverflow
= true;
1549 } else if ((AbiVariant
!= 2) ||
1550 (AbiVariant
== 2 && Value
.SectionID
== SectionID
)) {
1551 RelocationEntry
RE(SectionID
, Offset
, RelType
, Value
.Addend
);
1552 addRelocationForSection(RE
, Value
.SectionID
);
1555 if (IsExtern
|| (AbiVariant
== 2 && Value
.SectionID
!= SectionID
) ||
1557 // It is an external symbol (either Value.SymbolName is set, or
1558 // SymType is SymbolRef::ST_Unknown) or out of range.
1559 StubMap::const_iterator i
= Stubs
.find(Value
);
1560 if (i
!= Stubs
.end()) {
1561 // Symbol function stub already created, just relocate to it
1562 resolveRelocation(Section
, Offset
,
1563 reinterpret_cast<uint64_t>(
1564 Section
.getAddressWithOffset(i
->second
)),
1566 LLVM_DEBUG(dbgs() << " Stub function found\n");
1568 // Create a new stub function.
1569 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1570 Stubs
[Value
] = Section
.getStubOffset();
1571 uint8_t *StubTargetAddr
= createStubFunction(
1572 Section
.getAddressWithOffset(Section
.getStubOffset()),
1574 RelocationEntry
RE(SectionID
, StubTargetAddr
- Section
.getAddress(),
1575 ELF::R_PPC64_ADDR64
, Value
.Addend
);
1577 // Generates the 64-bits address loads as exemplified in section
1578 // 4.5.1 in PPC64 ELF ABI. Note that the relocations need to
1579 // apply to the low part of the instructions, so we have to update
1580 // the offset according to the target endianness.
1581 uint64_t StubRelocOffset
= StubTargetAddr
- Section
.getAddress();
1582 if (!IsTargetLittleEndian
)
1583 StubRelocOffset
+= 2;
1585 RelocationEntry
REhst(SectionID
, StubRelocOffset
+ 0,
1586 ELF::R_PPC64_ADDR16_HIGHEST
, Value
.Addend
);
1587 RelocationEntry
REhr(SectionID
, StubRelocOffset
+ 4,
1588 ELF::R_PPC64_ADDR16_HIGHER
, Value
.Addend
);
1589 RelocationEntry
REh(SectionID
, StubRelocOffset
+ 12,
1590 ELF::R_PPC64_ADDR16_HI
, Value
.Addend
);
1591 RelocationEntry
REl(SectionID
, StubRelocOffset
+ 16,
1592 ELF::R_PPC64_ADDR16_LO
, Value
.Addend
);
1594 if (Value
.SymbolName
) {
1595 addRelocationForSymbol(REhst
, Value
.SymbolName
);
1596 addRelocationForSymbol(REhr
, Value
.SymbolName
);
1597 addRelocationForSymbol(REh
, Value
.SymbolName
);
1598 addRelocationForSymbol(REl
, Value
.SymbolName
);
1600 addRelocationForSection(REhst
, Value
.SectionID
);
1601 addRelocationForSection(REhr
, Value
.SectionID
);
1602 addRelocationForSection(REh
, Value
.SectionID
);
1603 addRelocationForSection(REl
, Value
.SectionID
);
1606 resolveRelocation(Section
, Offset
, reinterpret_cast<uint64_t>(
1607 Section
.getAddressWithOffset(
1608 Section
.getStubOffset())),
1610 Section
.advanceStubOffset(getMaxStubSize());
1612 if (IsExtern
|| (AbiVariant
== 2 && Value
.SectionID
!= SectionID
)) {
1613 // Restore the TOC for external calls
1614 if (AbiVariant
== 2)
1615 writeInt32BE(Target
+ 4, 0xE8410018); // ld r2,24(r1)
1617 writeInt32BE(Target
+ 4, 0xE8410028); // ld r2,40(r1)
1620 } else if (RelType
== ELF::R_PPC64_TOC16
||
1621 RelType
== ELF::R_PPC64_TOC16_DS
||
1622 RelType
== ELF::R_PPC64_TOC16_LO
||
1623 RelType
== ELF::R_PPC64_TOC16_LO_DS
||
1624 RelType
== ELF::R_PPC64_TOC16_HI
||
1625 RelType
== ELF::R_PPC64_TOC16_HA
) {
1626 // These relocations are supposed to subtract the TOC address from
1627 // the final value. This does not fit cleanly into the RuntimeDyld
1628 // scheme, since there may be *two* sections involved in determining
1629 // the relocation value (the section of the symbol referred to by the
1630 // relocation, and the TOC section associated with the current module).
1632 // Fortunately, these relocations are currently only ever generated
1633 // referring to symbols that themselves reside in the TOC, which means
1634 // that the two sections are actually the same. Thus they cancel out
1635 // and we can immediately resolve the relocation right now.
1637 case ELF::R_PPC64_TOC16
: RelType
= ELF::R_PPC64_ADDR16
; break;
1638 case ELF::R_PPC64_TOC16_DS
: RelType
= ELF::R_PPC64_ADDR16_DS
; break;
1639 case ELF::R_PPC64_TOC16_LO
: RelType
= ELF::R_PPC64_ADDR16_LO
; break;
1640 case ELF::R_PPC64_TOC16_LO_DS
: RelType
= ELF::R_PPC64_ADDR16_LO_DS
; break;
1641 case ELF::R_PPC64_TOC16_HI
: RelType
= ELF::R_PPC64_ADDR16_HI
; break;
1642 case ELF::R_PPC64_TOC16_HA
: RelType
= ELF::R_PPC64_ADDR16_HA
; break;
1643 default: llvm_unreachable("Wrong relocation type.");
1646 RelocationValueRef TOCValue
;
1647 if (auto Err
= findPPC64TOCSection(Obj
, ObjSectionToID
, TOCValue
))
1648 return std::move(Err
);
1649 if (Value
.SymbolName
|| Value
.SectionID
!= TOCValue
.SectionID
)
1650 llvm_unreachable("Unsupported TOC relocation.");
1651 Value
.Addend
-= TOCValue
.Addend
;
1652 resolveRelocation(Sections
[SectionID
], Offset
, Value
.Addend
, RelType
, 0);
1654 // There are two ways to refer to the TOC address directly: either
1655 // via a ELF::R_PPC64_TOC relocation (where both symbol and addend are
1656 // ignored), or via any relocation that refers to the magic ".TOC."
1657 // symbols (in which case the addend is respected).
1658 if (RelType
== ELF::R_PPC64_TOC
) {
1659 RelType
= ELF::R_PPC64_ADDR64
;
1660 if (auto Err
= findPPC64TOCSection(Obj
, ObjSectionToID
, Value
))
1661 return std::move(Err
);
1662 } else if (TargetName
== ".TOC.") {
1663 if (auto Err
= findPPC64TOCSection(Obj
, ObjSectionToID
, Value
))
1664 return std::move(Err
);
1665 Value
.Addend
+= Addend
;
1668 RelocationEntry
RE(SectionID
, Offset
, RelType
, Value
.Addend
);
1670 if (Value
.SymbolName
)
1671 addRelocationForSymbol(RE
, Value
.SymbolName
);
1673 addRelocationForSection(RE
, Value
.SectionID
);
1675 } else if (Arch
== Triple::systemz
&&
1676 (RelType
== ELF::R_390_PLT32DBL
|| RelType
== ELF::R_390_GOTENT
)) {
1677 // Create function stubs for both PLT and GOT references, regardless of
1678 // whether the GOT reference is to data or code. The stub contains the
1679 // full address of the symbol, as needed by GOT references, and the
1680 // executable part only adds an overhead of 8 bytes.
1682 // We could try to conserve space by allocating the code and data
1683 // parts of the stub separately. However, as things stand, we allocate
1684 // a stub for every relocation, so using a GOT in JIT code should be
1685 // no less space efficient than using an explicit constant pool.
1686 LLVM_DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation.");
1687 SectionEntry
&Section
= Sections
[SectionID
];
1689 // Look for an existing stub.
1690 StubMap::const_iterator i
= Stubs
.find(Value
);
1691 uintptr_t StubAddress
;
1692 if (i
!= Stubs
.end()) {
1693 StubAddress
= uintptr_t(Section
.getAddressWithOffset(i
->second
));
1694 LLVM_DEBUG(dbgs() << " Stub function found\n");
1696 // Create a new stub function.
1697 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1699 uintptr_t BaseAddress
= uintptr_t(Section
.getAddress());
1700 uintptr_t StubAlignment
= getStubAlignment();
1702 (BaseAddress
+ Section
.getStubOffset() + StubAlignment
- 1) &
1704 unsigned StubOffset
= StubAddress
- BaseAddress
;
1706 Stubs
[Value
] = StubOffset
;
1707 createStubFunction((uint8_t *)StubAddress
);
1708 RelocationEntry
RE(SectionID
, StubOffset
+ 8, ELF::R_390_64
,
1710 if (Value
.SymbolName
)
1711 addRelocationForSymbol(RE
, Value
.SymbolName
);
1713 addRelocationForSection(RE
, Value
.SectionID
);
1714 Section
.advanceStubOffset(getMaxStubSize());
1717 if (RelType
== ELF::R_390_GOTENT
)
1718 resolveRelocation(Section
, Offset
, StubAddress
+ 8, ELF::R_390_PC32DBL
,
1721 resolveRelocation(Section
, Offset
, StubAddress
, RelType
, Addend
);
1722 } else if (Arch
== Triple::x86_64
) {
1723 if (RelType
== ELF::R_X86_64_PLT32
) {
1724 // The way the PLT relocations normally work is that the linker allocates
1726 // PLT and this relocation makes a PC-relative call into the PLT. The PLT
1727 // entry will then jump to an address provided by the GOT. On first call,
1729 // GOT address will point back into PLT code that resolves the symbol. After
1730 // the first call, the GOT entry points to the actual function.
1732 // For local functions we're ignoring all of that here and just replacing
1733 // the PLT32 relocation type with PC32, which will translate the relocation
1734 // into a PC-relative call directly to the function. For external symbols we
1735 // can't be sure the function will be within 2^32 bytes of the call site, so
1736 // we need to create a stub, which calls into the GOT. This case is
1737 // equivalent to the usual PLT implementation except that we use the stub
1738 // mechanism in RuntimeDyld (which puts stubs at the end of the section)
1739 // rather than allocating a PLT section.
1740 if (Value
.SymbolName
&& MemMgr
.allowStubAllocation()) {
1741 // This is a call to an external function.
1742 // Look for an existing stub.
1743 SectionEntry
*Section
= &Sections
[SectionID
];
1744 StubMap::const_iterator i
= Stubs
.find(Value
);
1745 uintptr_t StubAddress
;
1746 if (i
!= Stubs
.end()) {
1747 StubAddress
= uintptr_t(Section
->getAddress()) + i
->second
;
1748 LLVM_DEBUG(dbgs() << " Stub function found\n");
1750 // Create a new stub function (equivalent to a PLT entry).
1751 LLVM_DEBUG(dbgs() << " Create a new stub function\n");
1753 uintptr_t BaseAddress
= uintptr_t(Section
->getAddress());
1754 uintptr_t StubAlignment
= getStubAlignment();
1756 (BaseAddress
+ Section
->getStubOffset() + StubAlignment
- 1) &
1758 unsigned StubOffset
= StubAddress
- BaseAddress
;
1759 Stubs
[Value
] = StubOffset
;
1760 createStubFunction((uint8_t *)StubAddress
);
1762 // Bump our stub offset counter
1763 Section
->advanceStubOffset(getMaxStubSize());
1765 // Allocate a GOT Entry
1766 uint64_t GOTOffset
= allocateGOTEntries(1);
1767 // This potentially creates a new Section which potentially
1768 // invalidates the Section pointer, so reload it.
1769 Section
= &Sections
[SectionID
];
1771 // The load of the GOT address has an addend of -4
1772 resolveGOTOffsetRelocation(SectionID
, StubOffset
+ 2, GOTOffset
- 4,
1773 ELF::R_X86_64_PC32
);
1775 // Fill in the value of the symbol we're targeting into the GOT
1776 addRelocationForSymbol(
1777 computeGOTOffsetRE(GOTOffset
, 0, ELF::R_X86_64_64
),
1781 // Make the target call a call into the stub table.
1782 resolveRelocation(*Section
, Offset
, StubAddress
, ELF::R_X86_64_PC32
,
1785 Value
.Addend
+= support::ulittle32_t::ref(
1786 computePlaceholderAddress(SectionID
, Offset
));
1787 processSimpleRelocation(SectionID
, Offset
, ELF::R_X86_64_PC32
, Value
);
1789 } else if (RelType
== ELF::R_X86_64_GOTPCREL
||
1790 RelType
== ELF::R_X86_64_GOTPCRELX
||
1791 RelType
== ELF::R_X86_64_REX_GOTPCRELX
) {
1792 uint64_t GOTOffset
= allocateGOTEntries(1);
1793 resolveGOTOffsetRelocation(SectionID
, Offset
, GOTOffset
+ Addend
,
1794 ELF::R_X86_64_PC32
);
1796 // Fill in the value of the symbol we're targeting into the GOT
1797 RelocationEntry RE
=
1798 computeGOTOffsetRE(GOTOffset
, Value
.Offset
, ELF::R_X86_64_64
);
1799 if (Value
.SymbolName
)
1800 addRelocationForSymbol(RE
, Value
.SymbolName
);
1802 addRelocationForSection(RE
, Value
.SectionID
);
1803 } else if (RelType
== ELF::R_X86_64_GOT64
) {
1804 // Fill in a 64-bit GOT offset.
1805 uint64_t GOTOffset
= allocateGOTEntries(1);
1806 resolveRelocation(Sections
[SectionID
], Offset
, GOTOffset
,
1807 ELF::R_X86_64_64
, 0);
1809 // Fill in the value of the symbol we're targeting into the GOT
1810 RelocationEntry RE
=
1811 computeGOTOffsetRE(GOTOffset
, Value
.Offset
, ELF::R_X86_64_64
);
1812 if (Value
.SymbolName
)
1813 addRelocationForSymbol(RE
, Value
.SymbolName
);
1815 addRelocationForSection(RE
, Value
.SectionID
);
1816 } else if (RelType
== ELF::R_X86_64_GOTPC64
) {
1817 // Materialize the address of the base of the GOT relative to the PC.
1818 // This doesn't create a GOT entry, but it does mean we need a GOT
1820 (void)allocateGOTEntries(0);
1821 resolveGOTOffsetRelocation(SectionID
, Offset
, Addend
, ELF::R_X86_64_PC64
);
1822 } else if (RelType
== ELF::R_X86_64_GOTOFF64
) {
1823 // GOTOFF relocations ultimately require a section difference relocation.
1824 (void)allocateGOTEntries(0);
1825 processSimpleRelocation(SectionID
, Offset
, RelType
, Value
);
1826 } else if (RelType
== ELF::R_X86_64_PC32
) {
1827 Value
.Addend
+= support::ulittle32_t::ref(computePlaceholderAddress(SectionID
, Offset
));
1828 processSimpleRelocation(SectionID
, Offset
, RelType
, Value
);
1829 } else if (RelType
== ELF::R_X86_64_PC64
) {
1830 Value
.Addend
+= support::ulittle64_t::ref(computePlaceholderAddress(SectionID
, Offset
));
1831 processSimpleRelocation(SectionID
, Offset
, RelType
, Value
);
1833 processSimpleRelocation(SectionID
, Offset
, RelType
, Value
);
1836 if (Arch
== Triple::x86
) {
1837 Value
.Addend
+= support::ulittle32_t::ref(computePlaceholderAddress(SectionID
, Offset
));
1839 processSimpleRelocation(SectionID
, Offset
, RelType
, Value
);
1844 size_t RuntimeDyldELF::getGOTEntrySize() {
1845 // We don't use the GOT in all of these cases, but it's essentially free
1846 // to put them all here.
1849 case Triple::x86_64
:
1850 case Triple::aarch64
:
1851 case Triple::aarch64_be
:
1853 case Triple::ppc64le
:
1854 case Triple::systemz
:
1855 Result
= sizeof(uint64_t);
1860 Result
= sizeof(uint32_t);
1863 case Triple::mipsel
:
1864 case Triple::mips64
:
1865 case Triple::mips64el
:
1866 if (IsMipsO32ABI
|| IsMipsN32ABI
)
1867 Result
= sizeof(uint32_t);
1868 else if (IsMipsN64ABI
)
1869 Result
= sizeof(uint64_t);
1871 llvm_unreachable("Mips ABI not handled");
1874 llvm_unreachable("Unsupported CPU type!");
1879 uint64_t RuntimeDyldELF::allocateGOTEntries(unsigned no
) {
1880 if (GOTSectionID
== 0) {
1881 GOTSectionID
= Sections
.size();
1882 // Reserve a section id. We'll allocate the section later
1883 // once we know the total size
1884 Sections
.push_back(SectionEntry(".got", nullptr, 0, 0, 0));
1886 uint64_t StartOffset
= CurrentGOTIndex
* getGOTEntrySize();
1887 CurrentGOTIndex
+= no
;
1891 uint64_t RuntimeDyldELF::findOrAllocGOTEntry(const RelocationValueRef
&Value
,
1892 unsigned GOTRelType
) {
1893 auto E
= GOTOffsetMap
.insert({Value
, 0});
1895 uint64_t GOTOffset
= allocateGOTEntries(1);
1897 // Create relocation for newly created GOT entry
1898 RelocationEntry RE
=
1899 computeGOTOffsetRE(GOTOffset
, Value
.Offset
, GOTRelType
);
1900 if (Value
.SymbolName
)
1901 addRelocationForSymbol(RE
, Value
.SymbolName
);
1903 addRelocationForSection(RE
, Value
.SectionID
);
1905 E
.first
->second
= GOTOffset
;
1908 return E
.first
->second
;
1911 void RuntimeDyldELF::resolveGOTOffsetRelocation(unsigned SectionID
,
1915 // Fill in the relative address of the GOT Entry into the stub
1916 RelocationEntry
GOTRE(SectionID
, Offset
, Type
, GOTOffset
);
1917 addRelocationForSection(GOTRE
, GOTSectionID
);
1920 RelocationEntry
RuntimeDyldELF::computeGOTOffsetRE(uint64_t GOTOffset
,
1921 uint64_t SymbolOffset
,
1923 return RelocationEntry(GOTSectionID
, GOTOffset
, Type
, SymbolOffset
);
1926 Error
RuntimeDyldELF::finalizeLoad(const ObjectFile
&Obj
,
1927 ObjSectionToIDMap
&SectionMap
) {
1929 if (!PendingRelocs
.empty())
1930 return make_error
<RuntimeDyldError
>("Can't find matching LO16 reloc");
1932 // If necessary, allocate the global offset table
1933 if (GOTSectionID
!= 0) {
1934 // Allocate memory for the section
1935 size_t TotalSize
= CurrentGOTIndex
* getGOTEntrySize();
1936 uint8_t *Addr
= MemMgr
.allocateDataSection(TotalSize
, getGOTEntrySize(),
1937 GOTSectionID
, ".got", false);
1939 return make_error
<RuntimeDyldError
>("Unable to allocate memory for GOT!");
1941 Sections
[GOTSectionID
] =
1942 SectionEntry(".got", Addr
, TotalSize
, TotalSize
, 0);
1944 // For now, initialize all GOT entries to zero. We'll fill them in as
1945 // needed when GOT-based relocations are applied.
1946 memset(Addr
, 0, TotalSize
);
1947 if (IsMipsN32ABI
|| IsMipsN64ABI
) {
1948 // To correctly resolve Mips GOT relocations, we need a mapping from
1949 // object's sections to GOTs.
1950 for (section_iterator SI
= Obj
.section_begin(), SE
= Obj
.section_end();
1952 if (SI
->relocation_begin() != SI
->relocation_end()) {
1953 Expected
<section_iterator
> RelSecOrErr
= SI
->getRelocatedSection();
1955 return make_error
<RuntimeDyldError
>(
1956 toString(RelSecOrErr
.takeError()));
1958 section_iterator RelocatedSection
= *RelSecOrErr
;
1959 ObjSectionToIDMap::iterator i
= SectionMap
.find(*RelocatedSection
);
1960 assert (i
!= SectionMap
.end());
1961 SectionToGOTMap
[i
->second
] = GOTSectionID
;
1964 GOTSymbolOffsets
.clear();
1968 // Look for and record the EH frame section.
1969 ObjSectionToIDMap::iterator i
, e
;
1970 for (i
= SectionMap
.begin(), e
= SectionMap
.end(); i
!= e
; ++i
) {
1971 const SectionRef
&Section
= i
->first
;
1974 Expected
<StringRef
> NameOrErr
= Section
.getName();
1978 consumeError(NameOrErr
.takeError());
1980 if (Name
== ".eh_frame") {
1981 UnregisteredEHFrameSections
.push_back(i
->second
);
1987 CurrentGOTIndex
= 0;
1989 return Error::success();
1992 bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile
&Obj
) const {
1996 bool RuntimeDyldELF::relocationNeedsGot(const RelocationRef
&R
) const {
1997 unsigned RelTy
= R
.getType();
1998 if (Arch
== Triple::aarch64
|| Arch
== Triple::aarch64_be
)
1999 return RelTy
== ELF::R_AARCH64_ADR_GOT_PAGE
||
2000 RelTy
== ELF::R_AARCH64_LD64_GOT_LO12_NC
;
2002 if (Arch
== Triple::x86_64
)
2003 return RelTy
== ELF::R_X86_64_GOTPCREL
||
2004 RelTy
== ELF::R_X86_64_GOTPCRELX
||
2005 RelTy
== ELF::R_X86_64_GOT64
||
2006 RelTy
== ELF::R_X86_64_REX_GOTPCRELX
;
2010 bool RuntimeDyldELF::relocationNeedsStub(const RelocationRef
&R
) const {
2011 if (Arch
!= Triple::x86_64
)
2012 return true; // Conservative answer
2014 switch (R
.getType()) {
2016 return true; // Conservative answer
2019 case ELF::R_X86_64_GOTPCREL
:
2020 case ELF::R_X86_64_GOTPCRELX
:
2021 case ELF::R_X86_64_REX_GOTPCRELX
:
2022 case ELF::R_X86_64_GOTPC64
:
2023 case ELF::R_X86_64_GOT64
:
2024 case ELF::R_X86_64_GOTOFF64
:
2025 case ELF::R_X86_64_PC32
:
2026 case ELF::R_X86_64_PC64
:
2027 case ELF::R_X86_64_64
:
2028 // We know that these reloation types won't need a stub function. This list
2029 // can be extended as needed.