1 //===- Thunks.cpp --------------------------------------------------------===//
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 // This file contains Thunk subclasses.
11 // A thunk is a small piece of code written after an input section
12 // which is used to jump between "incompatible" functions
13 // such as MIPS PIC and non-PIC or ARM non-Thumb and Thumb functions.
15 // If a jump target is too far and its address doesn't fit to a
16 // short jump instruction, we need to create a thunk too, but we
17 // haven't supported it yet.
19 // i386 and x86-64 don't need thunks.
21 //===---------------------------------------------------------------------===//
25 #include "InputSection.h"
26 #include "OutputSections.h"
28 #include "SyntheticSections.h"
30 #include "lld/Common/ErrorHandler.h"
31 #include "lld/Common/Memory.h"
32 #include "llvm/BinaryFormat/ELF.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/Endian.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/MathExtras.h"
41 using namespace llvm::object;
42 using namespace llvm::ELF;
49 // AArch64 long range Thunks
50 class AArch64ABSLongThunk final : public Thunk {
52 AArch64ABSLongThunk(Symbol &dest) : Thunk(dest) {}
53 uint32_t size() override { return 16; }
54 void writeTo(uint8_t *buf) override;
55 void addSymbols(ThunkSection &isec) override;
58 class AArch64ADRPThunk final : public Thunk {
60 AArch64ADRPThunk(Symbol &dest) : Thunk(dest) {}
61 uint32_t size() override { return 12; }
62 void writeTo(uint8_t *buf) override;
63 void addSymbols(ThunkSection &isec) override;
66 // Base class for ARM thunks.
68 // An ARM thunk may be either short or long. A short thunk is simply a branch
69 // (B) instruction, and it may be used to call ARM functions when the distance
70 // from the thunk to the target is less than 32MB. Long thunks can branch to any
71 // virtual address and can switch between ARM and Thumb, and they are
72 // implemented in the derived classes. This class tries to create a short thunk
73 // if the target is in range, otherwise it creates a long thunk.
74 class ARMThunk : public Thunk {
76 ARMThunk(Symbol &dest) : Thunk(dest) {}
78 bool getMayUseShortThunk();
79 uint32_t size() override { return getMayUseShortThunk() ? 4 : sizeLong(); }
80 void writeTo(uint8_t *buf) override;
81 bool isCompatibleWith(const InputSection &isec,
82 const Relocation &rel) const override;
84 // Returns the size of a long thunk.
85 virtual uint32_t sizeLong() = 0;
87 // Writes a long thunk to Buf.
88 virtual void writeLong(uint8_t *buf) = 0;
91 // This field tracks whether all previously considered layouts would allow
92 // this thunk to be short. If we have ever needed a long thunk, we always
93 // create a long thunk, even if the thunk may be short given the current
94 // distance to the target. We do this because transitioning from long to short
95 // can create layout oscillations in certain corner cases which would prevent
96 // the layout from converging.
97 bool mayUseShortThunk = true;
100 // Base class for Thumb-2 thunks.
102 // This class is similar to ARMThunk, but it uses the Thumb-2 B.W instruction
103 // which has a range of 16MB.
104 class ThumbThunk : public Thunk {
106 ThumbThunk(Symbol &dest) : Thunk(dest) { alignment = 2; }
108 bool getMayUseShortThunk();
109 uint32_t size() override { return getMayUseShortThunk() ? 4 : sizeLong(); }
110 void writeTo(uint8_t *buf) override;
111 bool isCompatibleWith(const InputSection &isec,
112 const Relocation &rel) const override;
114 // Returns the size of a long thunk.
115 virtual uint32_t sizeLong() = 0;
117 // Writes a long thunk to Buf.
118 virtual void writeLong(uint8_t *buf) = 0;
121 // See comment in ARMThunk above.
122 bool mayUseShortThunk = true;
125 // Specific ARM Thunk implementations. The naming convention is:
126 // Source State, TargetState, Target Requirement, ABS or PI, Range
127 class ARMV7ABSLongThunk final : public ARMThunk {
129 ARMV7ABSLongThunk(Symbol &dest) : ARMThunk(dest) {}
131 uint32_t sizeLong() override { return 12; }
132 void writeLong(uint8_t *buf) override;
133 void addSymbols(ThunkSection &isec) override;
136 class ARMV7PILongThunk final : public ARMThunk {
138 ARMV7PILongThunk(Symbol &dest) : ARMThunk(dest) {}
140 uint32_t sizeLong() override { return 16; }
141 void writeLong(uint8_t *buf) override;
142 void addSymbols(ThunkSection &isec) override;
145 class ThumbV7ABSLongThunk final : public ThumbThunk {
147 ThumbV7ABSLongThunk(Symbol &dest) : ThumbThunk(dest) {}
149 uint32_t sizeLong() override { return 10; }
150 void writeLong(uint8_t *buf) override;
151 void addSymbols(ThunkSection &isec) override;
154 class ThumbV7PILongThunk final : public ThumbThunk {
156 ThumbV7PILongThunk(Symbol &dest) : ThumbThunk(dest) {}
158 uint32_t sizeLong() override { return 12; }
159 void writeLong(uint8_t *buf) override;
160 void addSymbols(ThunkSection &isec) override;
163 // Implementations of Thunks for older Arm architectures that do not support
164 // the movt/movw instructions. These thunks require at least Architecture v5
165 // as used on processors such as the Arm926ej-s. There are no Thumb entry
166 // points as there is no Thumb branch instruction on these architecture that
167 // can result in a thunk
168 class ARMV5ABSLongThunk final : public ARMThunk {
170 ARMV5ABSLongThunk(Symbol &dest) : ARMThunk(dest) {}
172 uint32_t sizeLong() override { return 8; }
173 void writeLong(uint8_t *buf) override;
174 void addSymbols(ThunkSection &isec) override;
175 bool isCompatibleWith(const InputSection &isec,
176 const Relocation &rel) const override;
179 class ARMV5PILongThunk final : public ARMThunk {
181 ARMV5PILongThunk(Symbol &dest) : ARMThunk(dest) {}
183 uint32_t sizeLong() override { return 16; }
184 void writeLong(uint8_t *buf) override;
185 void addSymbols(ThunkSection &isec) override;
186 bool isCompatibleWith(const InputSection &isec,
187 const Relocation &rel) const override;
190 // Implementations of Thunks for Arm v6-M. Only Thumb instructions are permitted
191 class ThumbV6MABSLongThunk final : public ThumbThunk {
193 ThumbV6MABSLongThunk(Symbol &dest) : ThumbThunk(dest) {}
195 uint32_t sizeLong() override { return 12; }
196 void writeLong(uint8_t *buf) override;
197 void addSymbols(ThunkSection &isec) override;
200 class ThumbV6MPILongThunk final : public ThumbThunk {
202 ThumbV6MPILongThunk(Symbol &dest) : ThumbThunk(dest) {}
204 uint32_t sizeLong() override { return 16; }
205 void writeLong(uint8_t *buf) override;
206 void addSymbols(ThunkSection &isec) override;
210 class MipsThunk final : public Thunk {
212 MipsThunk(Symbol &dest) : Thunk(dest) {}
214 uint32_t size() override { return 16; }
215 void writeTo(uint8_t *buf) override;
216 void addSymbols(ThunkSection &isec) override;
217 InputSection *getTargetInputSection() const override;
220 // microMIPS R2-R5 LA25 thunk
221 class MicroMipsThunk final : public Thunk {
223 MicroMipsThunk(Symbol &dest) : Thunk(dest) {}
225 uint32_t size() override { return 14; }
226 void writeTo(uint8_t *buf) override;
227 void addSymbols(ThunkSection &isec) override;
228 InputSection *getTargetInputSection() const override;
231 // microMIPS R6 LA25 thunk
232 class MicroMipsR6Thunk final : public Thunk {
234 MicroMipsR6Thunk(Symbol &dest) : Thunk(dest) {}
236 uint32_t size() override { return 12; }
237 void writeTo(uint8_t *buf) override;
238 void addSymbols(ThunkSection &isec) override;
239 InputSection *getTargetInputSection() const override;
242 class PPC32PltCallStub final : public Thunk {
244 PPC32PltCallStub(const InputSection &isec, const Relocation &rel, Symbol &dest)
245 : Thunk(dest), addend(rel.type == R_PPC_PLTREL24 ? rel.addend : 0),
247 uint32_t size() override { return 16; }
248 void writeTo(uint8_t *buf) override;
249 void addSymbols(ThunkSection &isec) override;
250 bool isCompatibleWith(const InputSection &isec, const Relocation &rel) const override;
253 // For R_PPC_PLTREL24, this records the addend, which will be used to decide
254 // the offsets in the call stub.
257 // Records the call site of the call stub.
258 const InputFile *file;
261 // PPC64 Plt call stubs.
262 // Any call site that needs to call through a plt entry needs a call stub in
263 // the .text section. The call stub is responsible for:
264 // 1) Saving the toc-pointer to the stack.
265 // 2) Loading the target functions address from the procedure linkage table into
266 // r12 for use by the target functions global entry point, and into the count
268 // 3) Transfering control to the target function through an indirect branch.
269 class PPC64PltCallStub final : public Thunk {
271 PPC64PltCallStub(Symbol &dest) : Thunk(dest) {}
272 uint32_t size() override { return 20; }
273 void writeTo(uint8_t *buf) override;
274 void addSymbols(ThunkSection &isec) override;
277 // A bl instruction uses a signed 24 bit offset, with an implicit 4 byte
278 // alignment. This gives a possible 26 bits of 'reach'. If the call offset is
279 // larger then that we need to emit a long-branch thunk. The target address
280 // of the callee is stored in a table to be accessed TOC-relative. Since the
281 // call must be local (a non-local call will have a PltCallStub instead) the
282 // table stores the address of the callee's local entry point. For
283 // position-independent code a corresponding relative dynamic relocation is
285 class PPC64LongBranchThunk : public Thunk {
287 uint32_t size() override { return 16; }
288 void writeTo(uint8_t *buf) override;
289 void addSymbols(ThunkSection &isec) override;
292 PPC64LongBranchThunk(Symbol &dest) : Thunk(dest) {}
295 class PPC64PILongBranchThunk final : public PPC64LongBranchThunk {
297 PPC64PILongBranchThunk(Symbol &dest) : PPC64LongBranchThunk(dest) {
298 assert(!dest.isPreemptible);
299 if (dest.isInPPC64Branchlt())
302 in.ppc64LongBranchTarget->addEntry(dest);
303 mainPart->relaDyn->addReloc(
304 {target->relativeRel, in.ppc64LongBranchTarget,
305 dest.getPPC64LongBranchOffset(), true, &dest,
306 getPPC64GlobalEntryToLocalEntryOffset(dest.stOther)});
310 class PPC64PDLongBranchThunk final : public PPC64LongBranchThunk {
312 PPC64PDLongBranchThunk(Symbol &dest) : PPC64LongBranchThunk(dest) {
313 if (!dest.isInPPC64Branchlt())
314 in.ppc64LongBranchTarget->addEntry(dest);
318 } // end anonymous namespace
320 Defined *Thunk::addSymbol(StringRef name, uint8_t type, uint64_t value,
321 InputSectionBase §ion) {
322 Defined *d = addSyntheticLocal(name, type, value, /*size=*/0, section);
327 void Thunk::setOffset(uint64_t newOffset) {
328 for (Defined *d : syms)
329 d->value = d->value - offset + newOffset;
333 // AArch64 long range Thunks
335 static uint64_t getAArch64ThunkDestVA(const Symbol &s) {
336 uint64_t v = s.isInPlt() ? s.getPltVA() : s.getVA();
340 void AArch64ABSLongThunk::writeTo(uint8_t *buf) {
341 const uint8_t data[] = {
342 0x50, 0x00, 0x00, 0x58, // ldr x16, L0
343 0x00, 0x02, 0x1f, 0xd6, // br x16
344 0x00, 0x00, 0x00, 0x00, // L0: .xword S
345 0x00, 0x00, 0x00, 0x00,
347 uint64_t s = getAArch64ThunkDestVA(destination);
348 memcpy(buf, data, sizeof(data));
349 target->relocateOne(buf + 8, R_AARCH64_ABS64, s);
352 void AArch64ABSLongThunk::addSymbols(ThunkSection &isec) {
353 addSymbol(saver.save("__AArch64AbsLongThunk_" + destination.getName()),
355 addSymbol("$x", STT_NOTYPE, 0, isec);
356 addSymbol("$d", STT_NOTYPE, 8, isec);
359 // This Thunk has a maximum range of 4Gb, this is sufficient for all programs
360 // using the small code model, including pc-relative ones. At time of writing
361 // clang and gcc do not support the large code model for position independent
362 // code so it is safe to use this for position independent thunks without
363 // worrying about the destination being more than 4Gb away.
364 void AArch64ADRPThunk::writeTo(uint8_t *buf) {
365 const uint8_t data[] = {
366 0x10, 0x00, 0x00, 0x90, // adrp x16, Dest R_AARCH64_ADR_PREL_PG_HI21(Dest)
367 0x10, 0x02, 0x00, 0x91, // add x16, x16, R_AARCH64_ADD_ABS_LO12_NC(Dest)
368 0x00, 0x02, 0x1f, 0xd6, // br x16
370 uint64_t s = getAArch64ThunkDestVA(destination);
371 uint64_t p = getThunkTargetSym()->getVA();
372 memcpy(buf, data, sizeof(data));
373 target->relocateOne(buf, R_AARCH64_ADR_PREL_PG_HI21,
374 getAArch64Page(s) - getAArch64Page(p));
375 target->relocateOne(buf + 4, R_AARCH64_ADD_ABS_LO12_NC, s);
378 void AArch64ADRPThunk::addSymbols(ThunkSection &isec) {
379 addSymbol(saver.save("__AArch64ADRPThunk_" + destination.getName()), STT_FUNC,
381 addSymbol("$x", STT_NOTYPE, 0, isec);
385 static uint64_t getARMThunkDestVA(const Symbol &s) {
386 uint64_t v = s.isInPlt() ? s.getPltVA() : s.getVA();
387 return SignExtend64<32>(v);
390 // This function returns true if the target is not Thumb and is within 2^26, and
391 // it has not previously returned false (see comment for mayUseShortThunk).
392 bool ARMThunk::getMayUseShortThunk() {
393 if (!mayUseShortThunk)
395 uint64_t s = getARMThunkDestVA(destination);
397 mayUseShortThunk = false;
400 uint64_t p = getThunkTargetSym()->getVA();
401 int64_t offset = s - p - 8;
402 mayUseShortThunk = llvm::isInt<26>(offset);
403 return mayUseShortThunk;
406 void ARMThunk::writeTo(uint8_t *buf) {
407 if (!getMayUseShortThunk()) {
412 uint64_t s = getARMThunkDestVA(destination);
413 uint64_t p = getThunkTargetSym()->getVA();
414 int64_t offset = s - p - 8;
415 const uint8_t data[] = {
416 0x00, 0x00, 0x00, 0xea, // b S
418 memcpy(buf, data, sizeof(data));
419 target->relocateOne(buf, R_ARM_JUMP24, offset);
422 bool ARMThunk::isCompatibleWith(const InputSection &isec,
423 const Relocation &rel) const {
424 // Thumb branch relocations can't use BLX
425 return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;
428 // This function returns true if the target is Thumb and is within 2^25, and
429 // it has not previously returned false (see comment for mayUseShortThunk).
430 bool ThumbThunk::getMayUseShortThunk() {
431 if (!mayUseShortThunk)
433 uint64_t s = getARMThunkDestVA(destination);
435 mayUseShortThunk = false;
438 uint64_t p = getThunkTargetSym()->getVA() & ~1;
439 int64_t offset = s - p - 4;
440 mayUseShortThunk = llvm::isInt<25>(offset);
441 return mayUseShortThunk;
444 void ThumbThunk::writeTo(uint8_t *buf) {
445 if (!getMayUseShortThunk()) {
450 uint64_t s = getARMThunkDestVA(destination);
451 uint64_t p = getThunkTargetSym()->getVA();
452 int64_t offset = s - p - 4;
453 const uint8_t data[] = {
454 0x00, 0xf0, 0x00, 0xb0, // b.w S
456 memcpy(buf, data, sizeof(data));
457 target->relocateOne(buf, R_ARM_THM_JUMP24, offset);
460 bool ThumbThunk::isCompatibleWith(const InputSection &isec,
461 const Relocation &rel) const {
462 // ARM branch relocations can't use BLX
463 return rel.type != R_ARM_JUMP24 && rel.type != R_ARM_PC24 && rel.type != R_ARM_PLT32;
466 void ARMV7ABSLongThunk::writeLong(uint8_t *buf) {
467 const uint8_t data[] = {
468 0x00, 0xc0, 0x00, 0xe3, // movw ip,:lower16:S
469 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S
470 0x1c, 0xff, 0x2f, 0xe1, // bx ip
472 uint64_t s = getARMThunkDestVA(destination);
473 memcpy(buf, data, sizeof(data));
474 target->relocateOne(buf, R_ARM_MOVW_ABS_NC, s);
475 target->relocateOne(buf + 4, R_ARM_MOVT_ABS, s);
478 void ARMV7ABSLongThunk::addSymbols(ThunkSection &isec) {
479 addSymbol(saver.save("__ARMv7ABSLongThunk_" + destination.getName()),
481 addSymbol("$a", STT_NOTYPE, 0, isec);
484 void ThumbV7ABSLongThunk::writeLong(uint8_t *buf) {
485 const uint8_t data[] = {
486 0x40, 0xf2, 0x00, 0x0c, // movw ip, :lower16:S
487 0xc0, 0xf2, 0x00, 0x0c, // movt ip, :upper16:S
490 uint64_t s = getARMThunkDestVA(destination);
491 memcpy(buf, data, sizeof(data));
492 target->relocateOne(buf, R_ARM_THM_MOVW_ABS_NC, s);
493 target->relocateOne(buf + 4, R_ARM_THM_MOVT_ABS, s);
496 void ThumbV7ABSLongThunk::addSymbols(ThunkSection &isec) {
497 addSymbol(saver.save("__Thumbv7ABSLongThunk_" + destination.getName()),
499 addSymbol("$t", STT_NOTYPE, 0, isec);
502 void ARMV7PILongThunk::writeLong(uint8_t *buf) {
503 const uint8_t data[] = {
504 0xf0, 0xcf, 0x0f, 0xe3, // P: movw ip,:lower16:S - (P + (L1-P) + 8)
505 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S - (P + (L1-P) + 8)
506 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
507 0x1c, 0xff, 0x2f, 0xe1, // bx ip
509 uint64_t s = getARMThunkDestVA(destination);
510 uint64_t p = getThunkTargetSym()->getVA();
511 int64_t offset = s - p - 16;
512 memcpy(buf, data, sizeof(data));
513 target->relocateOne(buf, R_ARM_MOVW_PREL_NC, offset);
514 target->relocateOne(buf + 4, R_ARM_MOVT_PREL, offset);
517 void ARMV7PILongThunk::addSymbols(ThunkSection &isec) {
518 addSymbol(saver.save("__ARMV7PILongThunk_" + destination.getName()), STT_FUNC,
520 addSymbol("$a", STT_NOTYPE, 0, isec);
523 void ThumbV7PILongThunk::writeLong(uint8_t *buf) {
524 const uint8_t data[] = {
525 0x4f, 0xf6, 0xf4, 0x7c, // P: movw ip,:lower16:S - (P + (L1-P) + 4)
526 0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P) + 4)
527 0xfc, 0x44, // L1: add ip, pc
530 uint64_t s = getARMThunkDestVA(destination);
531 uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
532 int64_t offset = s - p - 12;
533 memcpy(buf, data, sizeof(data));
534 target->relocateOne(buf, R_ARM_THM_MOVW_PREL_NC, offset);
535 target->relocateOne(buf + 4, R_ARM_THM_MOVT_PREL, offset);
538 void ThumbV7PILongThunk::addSymbols(ThunkSection &isec) {
539 addSymbol(saver.save("__ThumbV7PILongThunk_" + destination.getName()),
541 addSymbol("$t", STT_NOTYPE, 0, isec);
544 void ARMV5ABSLongThunk::writeLong(uint8_t *buf) {
545 const uint8_t data[] = {
546 0x04, 0xf0, 0x1f, 0xe5, // ldr pc, [pc,#-4] ; L1
547 0x00, 0x00, 0x00, 0x00, // L1: .word S
549 memcpy(buf, data, sizeof(data));
550 target->relocateOne(buf + 4, R_ARM_ABS32, getARMThunkDestVA(destination));
553 void ARMV5ABSLongThunk::addSymbols(ThunkSection &isec) {
554 addSymbol(saver.save("__ARMv5ABSLongThunk_" + destination.getName()),
556 addSymbol("$a", STT_NOTYPE, 0, isec);
557 addSymbol("$d", STT_NOTYPE, 4, isec);
560 bool ARMV5ABSLongThunk::isCompatibleWith(const InputSection &isec,
561 const Relocation &rel) const {
562 // Thumb branch relocations can't use BLX
563 return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;
566 void ARMV5PILongThunk::writeLong(uint8_t *buf) {
567 const uint8_t data[] = {
568 0x04, 0xc0, 0x9f, 0xe5, // P: ldr ip, [pc,#4] ; L2
569 0x0c, 0xc0, 0x8f, 0xe0, // L1: add ip, pc, ip
570 0x1c, 0xff, 0x2f, 0xe1, // bx ip
571 0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 8)
573 uint64_t s = getARMThunkDestVA(destination);
574 uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
575 memcpy(buf, data, sizeof(data));
576 target->relocateOne(buf + 12, R_ARM_REL32, s - p - 12);
579 void ARMV5PILongThunk::addSymbols(ThunkSection &isec) {
580 addSymbol(saver.save("__ARMV5PILongThunk_" + destination.getName()), STT_FUNC,
582 addSymbol("$a", STT_NOTYPE, 0, isec);
583 addSymbol("$d", STT_NOTYPE, 12, isec);
586 bool ARMV5PILongThunk::isCompatibleWith(const InputSection &isec,
587 const Relocation &rel) const {
588 // Thumb branch relocations can't use BLX
589 return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;
592 void ThumbV6MABSLongThunk::writeLong(uint8_t *buf) {
593 // Most Thumb instructions cannot access the high registers r8 - r15. As the
594 // only register we can corrupt is r12 we must instead spill a low register
595 // to the stack to use as a scratch register. We push r1 even though we
596 // don't need to get some space to use for the return address.
597 const uint8_t data[] = {
598 0x03, 0xb4, // push {r0, r1} ; Obtain scratch registers
599 0x01, 0x48, // ldr r0, [pc, #4] ; L1
600 0x01, 0x90, // str r0, [sp, #4] ; SP + 4 = S
601 0x01, 0xbd, // pop {r0, pc} ; restore r0 and branch to dest
602 0x00, 0x00, 0x00, 0x00 // L1: .word S
604 uint64_t s = getARMThunkDestVA(destination);
605 memcpy(buf, data, sizeof(data));
606 target->relocateOne(buf + 8, R_ARM_ABS32, s);
609 void ThumbV6MABSLongThunk::addSymbols(ThunkSection &isec) {
610 addSymbol(saver.save("__Thumbv6MABSLongThunk_" + destination.getName()),
612 addSymbol("$t", STT_NOTYPE, 0, isec);
613 addSymbol("$d", STT_NOTYPE, 8, isec);
616 void ThumbV6MPILongThunk::writeLong(uint8_t *buf) {
617 // Most Thumb instructions cannot access the high registers r8 - r15. As the
618 // only register we can corrupt is ip (r12) we must instead spill a low
619 // register to the stack to use as a scratch register.
620 const uint8_t data[] = {
621 0x01, 0xb4, // P: push {r0} ; Obtain scratch register
622 0x02, 0x48, // ldr r0, [pc, #8] ; L2
623 0x84, 0x46, // mov ip, r0 ; high to low register
624 0x01, 0xbc, // pop {r0} ; restore scratch register
625 0xe7, 0x44, // L1: add pc, ip ; transfer control
626 0xc0, 0x46, // nop ; pad to 4-byte boundary
627 0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 4)
629 uint64_t s = getARMThunkDestVA(destination);
630 uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
631 memcpy(buf, data, sizeof(data));
632 target->relocateOne(buf + 12, R_ARM_REL32, s - p - 12);
635 void ThumbV6MPILongThunk::addSymbols(ThunkSection &isec) {
636 addSymbol(saver.save("__Thumbv6MPILongThunk_" + destination.getName()),
638 addSymbol("$t", STT_NOTYPE, 0, isec);
639 addSymbol("$d", STT_NOTYPE, 12, isec);
642 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
643 void MipsThunk::writeTo(uint8_t *buf) {
644 uint64_t s = destination.getVA();
645 write32(buf, 0x3c190000); // lui $25, %hi(func)
646 write32(buf + 4, 0x08000000 | (s >> 2)); // j func
647 write32(buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
648 write32(buf + 12, 0x00000000); // nop
649 target->relocateOne(buf, R_MIPS_HI16, s);
650 target->relocateOne(buf + 8, R_MIPS_LO16, s);
653 void MipsThunk::addSymbols(ThunkSection &isec) {
654 addSymbol(saver.save("__LA25Thunk_" + destination.getName()), STT_FUNC, 0,
658 InputSection *MipsThunk::getTargetInputSection() const {
659 auto &dr = cast<Defined>(destination);
660 return dyn_cast<InputSection>(dr.section);
663 // Write microMIPS R2-R5 LA25 thunk code
664 // to call PIC function from the non-PIC one.
665 void MicroMipsThunk::writeTo(uint8_t *buf) {
666 uint64_t s = destination.getVA();
667 write16(buf, 0x41b9); // lui $25, %hi(func)
668 write16(buf + 4, 0xd400); // j func
669 write16(buf + 8, 0x3339); // addiu $25, $25, %lo(func)
670 write16(buf + 12, 0x0c00); // nop
671 target->relocateOne(buf, R_MICROMIPS_HI16, s);
672 target->relocateOne(buf + 4, R_MICROMIPS_26_S1, s);
673 target->relocateOne(buf + 8, R_MICROMIPS_LO16, s);
676 void MicroMipsThunk::addSymbols(ThunkSection &isec) {
677 Defined *d = addSymbol(
678 saver.save("__microLA25Thunk_" + destination.getName()), STT_FUNC, 0, isec);
679 d->stOther |= STO_MIPS_MICROMIPS;
682 InputSection *MicroMipsThunk::getTargetInputSection() const {
683 auto &dr = cast<Defined>(destination);
684 return dyn_cast<InputSection>(dr.section);
687 // Write microMIPS R6 LA25 thunk code
688 // to call PIC function from the non-PIC one.
689 void MicroMipsR6Thunk::writeTo(uint8_t *buf) {
690 uint64_t s = destination.getVA();
691 uint64_t p = getThunkTargetSym()->getVA();
692 write16(buf, 0x1320); // lui $25, %hi(func)
693 write16(buf + 4, 0x3339); // addiu $25, $25, %lo(func)
694 write16(buf + 8, 0x9400); // bc func
695 target->relocateOne(buf, R_MICROMIPS_HI16, s);
696 target->relocateOne(buf + 4, R_MICROMIPS_LO16, s);
697 target->relocateOne(buf + 8, R_MICROMIPS_PC26_S1, s - p - 12);
700 void MicroMipsR6Thunk::addSymbols(ThunkSection &isec) {
701 Defined *d = addSymbol(
702 saver.save("__microLA25Thunk_" + destination.getName()), STT_FUNC, 0, isec);
703 d->stOther |= STO_MIPS_MICROMIPS;
706 InputSection *MicroMipsR6Thunk::getTargetInputSection() const {
707 auto &dr = cast<Defined>(destination);
708 return dyn_cast<InputSection>(dr.section);
711 void PPC32PltCallStub::writeTo(uint8_t *buf) {
712 if (!config->isPic) {
713 uint64_t va = destination.getGotPltVA();
714 write32(buf + 0, 0x3d600000 | (va + 0x8000) >> 16); // lis r11,ha
715 write32(buf + 4, 0x816b0000 | (uint16_t)va); // lwz r11,l(r11)
716 write32(buf + 8, 0x7d6903a6); // mtctr r11
717 write32(buf + 12, 0x4e800420); // bctr
721 if (addend >= 0x8000) {
722 // The stub loads an address relative to r30 (.got2+Addend). Addend is
723 // almost always 0x8000. The address of .got2 is different in another object
724 // file, so a stub cannot be shared.
725 offset = destination.getGotPltVA() - (in.ppc32Got2->getParent()->getVA() +
726 file->ppc32Got2OutSecOff + addend);
728 // The stub loads an address relative to _GLOBAL_OFFSET_TABLE_ (which is
729 // currently the address of .got).
730 offset = destination.getGotPltVA() - in.got->getVA();
732 uint16_t ha = (offset + 0x8000) >> 16, l = (uint16_t)offset;
734 write32(buf + 0, 0x817e0000 | l); // lwz r11,l(r30)
735 write32(buf + 4, 0x7d6903a6); // mtctr r11
736 write32(buf + 8, 0x4e800420); // bctr
737 write32(buf + 12, 0x60000000); // nop
739 write32(buf + 0, 0x3d7e0000 | ha); // addis r11,r30,ha
740 write32(buf + 4, 0x816b0000 | l); // lwz r11,l(r11)
741 write32(buf + 8, 0x7d6903a6); // mtctr r11
742 write32(buf + 12, 0x4e800420); // bctr
746 void PPC32PltCallStub::addSymbols(ThunkSection &isec) {
748 raw_string_ostream os(buf);
749 os << format_hex_no_prefix(addend, 8);
751 os << ".plt_call32.";
752 else if (addend >= 0x8000)
753 os << ".got2.plt_pic32.";
756 os << destination.getName();
757 addSymbol(saver.save(os.str()), STT_FUNC, 0, isec);
760 bool PPC32PltCallStub::isCompatibleWith(const InputSection &isec,
761 const Relocation &rel) const {
762 return !config->isPic || (isec.file == file && rel.addend == addend);
765 static void writePPCLoadAndBranch(uint8_t *buf, int64_t offset) {
766 uint16_t offHa = (offset + 0x8000) >> 16;
767 uint16_t offLo = offset & 0xffff;
769 write32(buf + 0, 0x3d820000 | offHa); // addis r12, r2, OffHa
770 write32(buf + 4, 0xe98c0000 | offLo); // ld r12, OffLo(r12)
771 write32(buf + 8, 0x7d8903a6); // mtctr r12
772 write32(buf + 12, 0x4e800420); // bctr
775 void PPC64PltCallStub::writeTo(uint8_t *buf) {
776 int64_t offset = destination.getGotPltVA() - getPPC64TocBase();
777 // Save the TOC pointer to the save-slot reserved in the call frame.
778 write32(buf + 0, 0xf8410018); // std r2,24(r1)
779 writePPCLoadAndBranch(buf + 4, offset);
782 void PPC64PltCallStub::addSymbols(ThunkSection &isec) {
783 Defined *s = addSymbol(saver.save("__plt_" + destination.getName()), STT_FUNC,
785 s->needsTocRestore = true;
788 void PPC64LongBranchThunk::writeTo(uint8_t *buf) {
789 int64_t offset = destination.getPPC64LongBranchTableVA() - getPPC64TocBase();
790 writePPCLoadAndBranch(buf, offset);
793 void PPC64LongBranchThunk::addSymbols(ThunkSection &isec) {
794 addSymbol(saver.save("__long_branch_" + destination.getName()), STT_FUNC, 0,
798 Thunk::Thunk(Symbol &d) : destination(d), offset(0) {}
800 Thunk::~Thunk() = default;
802 static Thunk *addThunkAArch64(RelType type, Symbol &s) {
803 if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26)
804 fatal("unrecognized relocation type");
805 if (config->picThunk)
806 return make<AArch64ADRPThunk>(s);
807 return make<AArch64ABSLongThunk>(s);
810 // Creates a thunk for Thumb-ARM interworking.
811 // Arm Architectures v5 and v6 do not support Thumb2 technology. This means
812 // - MOVT and MOVW instructions cannot be used
813 // - Only Thumb relocation that can generate a Thunk is a BL, this can always
814 // be transformed into a BLX
815 static Thunk *addThunkPreArmv7(RelType reloc, Symbol &s) {
822 if (config->picThunk)
823 return make<ARMV5PILongThunk>(s);
824 return make<ARMV5ABSLongThunk>(s);
826 fatal("relocation " + toString(reloc) + " to " + toString(s) +
827 " not supported for Armv5 or Armv6 targets");
830 // Create a thunk for Thumb long branch on V6-M.
831 // Arm Architecture v6-M only supports Thumb instructions. This means
832 // - MOVT and MOVW instructions cannot be used.
833 // - Only a limited number of instructions can access registers r8 and above
834 // - No interworking support is needed (all Thumb).
835 static Thunk *addThunkV6M(RelType reloc, Symbol &s) {
837 case R_ARM_THM_JUMP19:
838 case R_ARM_THM_JUMP24:
841 return make<ThumbV6MPILongThunk>(s);
842 return make<ThumbV6MABSLongThunk>(s);
844 fatal("relocation " + toString(reloc) + " to " + toString(s) +
845 " not supported for Armv6-M targets");
848 // Creates a thunk for Thumb-ARM interworking or branch range extension.
849 static Thunk *addThunkArm(RelType reloc, Symbol &s) {
850 // Decide which Thunk is needed based on:
851 // Available instruction set
852 // - An Arm Thunk can only be used if Arm state is available.
853 // - A Thumb Thunk can only be used if Thumb state is available.
854 // - Can only use a Thunk if it uses instructions that the Target supports.
855 // Relocation is branch or branch and link
856 // - Branch instructions cannot change state, can only select Thunk that
857 // starts in the same state as the caller.
858 // - Branch and link relocations can change state, can select Thunks from
859 // either Arm or Thumb.
860 // Position independent Thunks if we require position independent code.
862 // Handle architectures that have restrictions on the instructions that they
863 // can use in Thunks. The flags below are set by reading the BuildAttributes
864 // of the input objects. InputFiles.cpp contains the mapping from ARM
865 // architecture to flag.
866 if (!config->armHasMovtMovw) {
867 if (!config->armJ1J2BranchEncoding)
868 return addThunkPreArmv7(reloc, s);
869 return addThunkV6M(reloc, s);
877 if (config->picThunk)
878 return make<ARMV7PILongThunk>(s);
879 return make<ARMV7ABSLongThunk>(s);
880 case R_ARM_THM_JUMP19:
881 case R_ARM_THM_JUMP24:
883 if (config->picThunk)
884 return make<ThumbV7PILongThunk>(s);
885 return make<ThumbV7ABSLongThunk>(s);
887 fatal("unrecognized relocation type");
890 static Thunk *addThunkMips(RelType type, Symbol &s) {
891 if ((s.stOther & STO_MIPS_MICROMIPS) && isMipsR6())
892 return make<MicroMipsR6Thunk>(s);
893 if (s.stOther & STO_MIPS_MICROMIPS)
894 return make<MicroMipsThunk>(s);
895 return make<MipsThunk>(s);
898 static Thunk *addThunkPPC32(const InputSection &isec, const Relocation &rel, Symbol &s) {
899 assert((rel.type == R_PPC_REL24 || rel.type == R_PPC_PLTREL24) &&
900 "unexpected relocation type for thunk");
901 return make<PPC32PltCallStub>(isec, rel, s);
904 static Thunk *addThunkPPC64(RelType type, Symbol &s) {
905 assert(type == R_PPC64_REL24 && "unexpected relocation type for thunk");
907 return make<PPC64PltCallStub>(s);
909 if (config->picThunk)
910 return make<PPC64PILongBranchThunk>(s);
912 return make<PPC64PDLongBranchThunk>(s);
915 Thunk *addThunk(const InputSection &isec, Relocation &rel) {
916 Symbol &s = *rel.sym;
918 if (config->emachine == EM_AARCH64)
919 return addThunkAArch64(rel.type, s);
921 if (config->emachine == EM_ARM)
922 return addThunkArm(rel.type, s);
924 if (config->emachine == EM_MIPS)
925 return addThunkMips(rel.type, s);
927 if (config->emachine == EM_PPC)
928 return addThunkPPC32(isec, rel, s);
930 if (config->emachine == EM_PPC64)
931 return addThunkPPC64(rel.type, s);
933 llvm_unreachable("add Thunk only supported for ARM, Mips and PowerPC");
936 } // end namespace elf
937 } // end namespace lld