1 //===- Thunks.cpp --------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===---------------------------------------------------------------------===//
10 // This file contains Thunk subclasses.
12 // A thunk is a small piece of code written after an input section
13 // which is used to jump between "incompatible" functions
14 // such as MIPS PIC and non-PIC or ARM non-Thumb and Thumb functions.
16 // If a jump target is too far and its address doesn't fit to a
17 // short jump instruction, we need to create a thunk too, but we
18 // haven't supported it yet.
20 // i386 and x86-64 don't need thunks.
22 //===---------------------------------------------------------------------===//
26 #include "InputSection.h"
27 #include "OutputSections.h"
29 #include "SyntheticSections.h"
31 #include "lld/Common/ErrorHandler.h"
32 #include "lld/Common/Memory.h"
33 #include "llvm/BinaryFormat/ELF.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/Endian.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/MathExtras.h"
42 using namespace llvm::object;
43 using namespace llvm::ELF;
50 // AArch64 long range Thunks
51 class AArch64ABSLongThunk final : public Thunk {
53 AArch64ABSLongThunk(Symbol &Dest) : Thunk(Dest) {}
54 uint32_t size() override { return 16; }
55 void writeTo(uint8_t *Buf) override;
56 void addSymbols(ThunkSection &IS) override;
59 class AArch64ADRPThunk final : public Thunk {
61 AArch64ADRPThunk(Symbol &Dest) : Thunk(Dest) {}
62 uint32_t size() override { return 12; }
63 void writeTo(uint8_t *Buf) override;
64 void addSymbols(ThunkSection &IS) override;
67 // Base class for ARM thunks.
69 // An ARM thunk may be either short or long. A short thunk is simply a branch
70 // (B) instruction, and it may be used to call ARM functions when the distance
71 // from the thunk to the target is less than 32MB. Long thunks can branch to any
72 // virtual address and can switch between ARM and Thumb, and they are
73 // implemented in the derived classes. This class tries to create a short thunk
74 // if the target is in range, otherwise it creates a long thunk.
75 class ARMThunk : public Thunk {
77 ARMThunk(Symbol &Dest) : Thunk(Dest) {}
79 bool mayUseShortThunk();
80 uint32_t size() override { return mayUseShortThunk() ? 4 : sizeLong(); }
81 void writeTo(uint8_t *Buf) override;
82 bool isCompatibleWith(RelType Type) 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 mayUseShortThunk();
109 uint32_t size() override { return mayUseShortThunk() ? 4 : sizeLong(); }
110 void writeTo(uint8_t *Buf) override;
111 bool isCompatibleWith(RelType Type) const override;
113 // Returns the size of a long thunk.
114 virtual uint32_t sizeLong() = 0;
116 // Writes a long thunk to Buf.
117 virtual void writeLong(uint8_t *Buf) = 0;
120 // See comment in ARMThunk above.
121 bool MayUseShortThunk = true;
124 // Specific ARM Thunk implementations. The naming convention is:
125 // Source State, TargetState, Target Requirement, ABS or PI, Range
126 class ARMV7ABSLongThunk final : public ARMThunk {
128 ARMV7ABSLongThunk(Symbol &Dest) : ARMThunk(Dest) {}
130 uint32_t sizeLong() override { return 12; }
131 void writeLong(uint8_t *Buf) override;
132 void addSymbols(ThunkSection &IS) override;
135 class ARMV7PILongThunk final : public ARMThunk {
137 ARMV7PILongThunk(Symbol &Dest) : ARMThunk(Dest) {}
139 uint32_t sizeLong() override { return 16; }
140 void writeLong(uint8_t *Buf) override;
141 void addSymbols(ThunkSection &IS) override;
144 class ThumbV7ABSLongThunk final : public ThumbThunk {
146 ThumbV7ABSLongThunk(Symbol &Dest) : ThumbThunk(Dest) {}
148 uint32_t sizeLong() override { return 10; }
149 void writeLong(uint8_t *Buf) override;
150 void addSymbols(ThunkSection &IS) override;
153 class ThumbV7PILongThunk final : public ThumbThunk {
155 ThumbV7PILongThunk(Symbol &Dest) : ThumbThunk(Dest) {}
157 uint32_t sizeLong() override { return 12; }
158 void writeLong(uint8_t *Buf) override;
159 void addSymbols(ThunkSection &IS) override;
163 class MipsThunk final : public Thunk {
165 MipsThunk(Symbol &Dest) : Thunk(Dest) {}
167 uint32_t size() override { return 16; }
168 void writeTo(uint8_t *Buf) override;
169 void addSymbols(ThunkSection &IS) override;
170 InputSection *getTargetInputSection() const override;
173 // microMIPS R2-R5 LA25 thunk
174 class MicroMipsThunk final : public Thunk {
176 MicroMipsThunk(Symbol &Dest) : Thunk(Dest) {}
178 uint32_t size() override { return 14; }
179 void writeTo(uint8_t *Buf) override;
180 void addSymbols(ThunkSection &IS) override;
181 InputSection *getTargetInputSection() const override;
184 // microMIPS R6 LA25 thunk
185 class MicroMipsR6Thunk final : public Thunk {
187 MicroMipsR6Thunk(Symbol &Dest) : Thunk(Dest) {}
189 uint32_t size() override { return 12; }
190 void writeTo(uint8_t *Buf) override;
191 void addSymbols(ThunkSection &IS) override;
192 InputSection *getTargetInputSection() const override;
196 // PPC64 Plt call stubs.
197 // Any call site that needs to call through a plt entry needs a call stub in
198 // the .text section. The call stub is responsible for:
199 // 1) Saving the toc-pointer to the stack.
200 // 2) Loading the target functions address from the procedure linkage table into
201 // r12 for use by the target functions global entry point, and into the count
203 // 3) Transfering control to the target function through an indirect branch.
204 class PPC64PltCallStub final : public Thunk {
206 PPC64PltCallStub(Symbol &Dest) : Thunk(Dest) {}
207 uint32_t size() override { return 20; }
208 void writeTo(uint8_t *Buf) override;
209 void addSymbols(ThunkSection &IS) override;
212 } // end anonymous namespace
214 Defined *Thunk::addSymbol(StringRef Name, uint8_t Type, uint64_t Value,
215 InputSectionBase &Section) {
216 Defined *D = addSyntheticLocal(Name, Type, Value, /*Size=*/0, Section);
221 void Thunk::setOffset(uint64_t NewOffset) {
222 for (Defined *D : Syms)
223 D->Value = D->Value - Offset + NewOffset;
227 // AArch64 long range Thunks
229 static uint64_t getAArch64ThunkDestVA(const Symbol &S) {
230 uint64_t V = S.isInPlt() ? S.getPltVA() : S.getVA();
234 void AArch64ABSLongThunk::writeTo(uint8_t *Buf) {
235 const uint8_t Data[] = {
236 0x50, 0x00, 0x00, 0x58, // ldr x16, L0
237 0x00, 0x02, 0x1f, 0xd6, // br x16
238 0x00, 0x00, 0x00, 0x00, // L0: .xword S
239 0x00, 0x00, 0x00, 0x00,
241 uint64_t S = getAArch64ThunkDestVA(Destination);
242 memcpy(Buf, Data, sizeof(Data));
243 Target->relocateOne(Buf + 8, R_AARCH64_ABS64, S);
246 void AArch64ABSLongThunk::addSymbols(ThunkSection &IS) {
247 addSymbol(Saver.save("__AArch64AbsLongThunk_" + Destination.getName()),
249 addSymbol("$x", STT_NOTYPE, 0, IS);
250 addSymbol("$d", STT_NOTYPE, 8, IS);
253 // This Thunk has a maximum range of 4Gb, this is sufficient for all programs
254 // using the small code model, including pc-relative ones. At time of writing
255 // clang and gcc do not support the large code model for position independent
256 // code so it is safe to use this for position independent thunks without
257 // worrying about the destination being more than 4Gb away.
258 void AArch64ADRPThunk::writeTo(uint8_t *Buf) {
259 const uint8_t Data[] = {
260 0x10, 0x00, 0x00, 0x90, // adrp x16, Dest R_AARCH64_ADR_PREL_PG_HI21(Dest)
261 0x10, 0x02, 0x00, 0x91, // add x16, x16, R_AARCH64_ADD_ABS_LO12_NC(Dest)
262 0x00, 0x02, 0x1f, 0xd6, // br x16
264 uint64_t S = getAArch64ThunkDestVA(Destination);
265 uint64_t P = getThunkTargetSym()->getVA();
266 memcpy(Buf, Data, sizeof(Data));
267 Target->relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
268 getAArch64Page(S) - getAArch64Page(P));
269 Target->relocateOne(Buf + 4, R_AARCH64_ADD_ABS_LO12_NC, S);
272 void AArch64ADRPThunk::addSymbols(ThunkSection &IS) {
273 addSymbol(Saver.save("__AArch64ADRPThunk_" + Destination.getName()), STT_FUNC,
275 addSymbol("$x", STT_NOTYPE, 0, IS);
279 static uint64_t getARMThunkDestVA(const Symbol &S) {
280 uint64_t V = S.isInPlt() ? S.getPltVA() : S.getVA();
281 return SignExtend64<32>(V);
284 // This function returns true if the target is not Thumb and is within 2^26, and
285 // it has not previously returned false (see comment for MayUseShortThunk).
286 bool ARMThunk::mayUseShortThunk() {
287 if (!MayUseShortThunk)
289 uint64_t S = getARMThunkDestVA(Destination);
291 MayUseShortThunk = false;
294 uint64_t P = getThunkTargetSym()->getVA();
295 int64_t Offset = S - P - 8;
296 MayUseShortThunk = llvm::isInt<26>(Offset);
297 return MayUseShortThunk;
300 void ARMThunk::writeTo(uint8_t *Buf) {
301 if (!mayUseShortThunk()) {
306 uint64_t S = getARMThunkDestVA(Destination);
307 uint64_t P = getThunkTargetSym()->getVA();
308 int64_t Offset = S - P - 8;
309 const uint8_t Data[] = {
310 0x00, 0x00, 0x00, 0xea, // b S
312 memcpy(Buf, Data, sizeof(Data));
313 Target->relocateOne(Buf, R_ARM_JUMP24, Offset);
316 bool ARMThunk::isCompatibleWith(RelType Type) const {
317 // Thumb branch relocations can't use BLX
318 return Type != R_ARM_THM_JUMP19 && Type != R_ARM_THM_JUMP24;
321 // This function returns true if the target is Thumb and is within 2^25, and
322 // it has not previously returned false (see comment for MayUseShortThunk).
323 bool ThumbThunk::mayUseShortThunk() {
324 if (!MayUseShortThunk)
326 uint64_t S = getARMThunkDestVA(Destination);
328 MayUseShortThunk = false;
331 uint64_t P = getThunkTargetSym()->getVA() & ~1;
332 int64_t Offset = S - P - 4;
333 MayUseShortThunk = llvm::isInt<25>(Offset);
334 return MayUseShortThunk;
337 void ThumbThunk::writeTo(uint8_t *Buf) {
338 if (!mayUseShortThunk()) {
343 uint64_t S = getARMThunkDestVA(Destination);
344 uint64_t P = getThunkTargetSym()->getVA();
345 int64_t Offset = S - P - 4;
346 const uint8_t Data[] = {
347 0x00, 0xf0, 0x00, 0xb0, // b.w S
349 memcpy(Buf, Data, sizeof(Data));
350 Target->relocateOne(Buf, R_ARM_THM_JUMP24, Offset);
353 bool ThumbThunk::isCompatibleWith(RelType Type) const {
354 // ARM branch relocations can't use BLX
355 return Type != R_ARM_JUMP24 && Type != R_ARM_PC24 && Type != R_ARM_PLT32;
358 void ARMV7ABSLongThunk::writeLong(uint8_t *Buf) {
359 const uint8_t Data[] = {
360 0x00, 0xc0, 0x00, 0xe3, // movw ip,:lower16:S
361 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S
362 0x1c, 0xff, 0x2f, 0xe1, // bx ip
364 uint64_t S = getARMThunkDestVA(Destination);
365 memcpy(Buf, Data, sizeof(Data));
366 Target->relocateOne(Buf, R_ARM_MOVW_ABS_NC, S);
367 Target->relocateOne(Buf + 4, R_ARM_MOVT_ABS, S);
370 void ARMV7ABSLongThunk::addSymbols(ThunkSection &IS) {
371 addSymbol(Saver.save("__ARMv7ABSLongThunk_" + Destination.getName()),
373 addSymbol("$a", STT_NOTYPE, 0, IS);
376 void ThumbV7ABSLongThunk::writeLong(uint8_t *Buf) {
377 const uint8_t Data[] = {
378 0x40, 0xf2, 0x00, 0x0c, // movw ip, :lower16:S
379 0xc0, 0xf2, 0x00, 0x0c, // movt ip, :upper16:S
382 uint64_t S = getARMThunkDestVA(Destination);
383 memcpy(Buf, Data, sizeof(Data));
384 Target->relocateOne(Buf, R_ARM_THM_MOVW_ABS_NC, S);
385 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_ABS, S);
388 void ThumbV7ABSLongThunk::addSymbols(ThunkSection &IS) {
389 addSymbol(Saver.save("__Thumbv7ABSLongThunk_" + Destination.getName()),
391 addSymbol("$t", STT_NOTYPE, 0, IS);
394 void ARMV7PILongThunk::writeLong(uint8_t *Buf) {
395 const uint8_t Data[] = {
396 0xf0, 0xcf, 0x0f, 0xe3, // P: movw ip,:lower16:S - (P + (L1-P) + 8)
397 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S - (P + (L1-P) + 8)
398 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
399 0x1c, 0xff, 0x2f, 0xe1, // bx r12
401 uint64_t S = getARMThunkDestVA(Destination);
402 uint64_t P = getThunkTargetSym()->getVA();
403 uint64_t Offset = S - P - 16;
404 memcpy(Buf, Data, sizeof(Data));
405 Target->relocateOne(Buf, R_ARM_MOVW_PREL_NC, Offset);
406 Target->relocateOne(Buf + 4, R_ARM_MOVT_PREL, Offset);
409 void ARMV7PILongThunk::addSymbols(ThunkSection &IS) {
410 addSymbol(Saver.save("__ARMV7PILongThunk_" + Destination.getName()), STT_FUNC,
412 addSymbol("$a", STT_NOTYPE, 0, IS);
415 void ThumbV7PILongThunk::writeLong(uint8_t *Buf) {
416 const uint8_t Data[] = {
417 0x4f, 0xf6, 0xf4, 0x7c, // P: movw ip,:lower16:S - (P + (L1-P) + 4)
418 0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P) + 4)
419 0xfc, 0x44, // L1: add r12, pc
420 0x60, 0x47, // bx r12
422 uint64_t S = getARMThunkDestVA(Destination);
423 uint64_t P = getThunkTargetSym()->getVA() & ~0x1;
424 uint64_t Offset = S - P - 12;
425 memcpy(Buf, Data, sizeof(Data));
426 Target->relocateOne(Buf, R_ARM_THM_MOVW_PREL_NC, Offset);
427 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_PREL, Offset);
430 void ThumbV7PILongThunk::addSymbols(ThunkSection &IS) {
431 addSymbol(Saver.save("__ThumbV7PILongThunk_" + Destination.getName()),
433 addSymbol("$t", STT_NOTYPE, 0, IS);
436 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
437 void MipsThunk::writeTo(uint8_t *Buf) {
438 uint64_t S = Destination.getVA();
439 write32(Buf, 0x3c190000); // lui $25, %hi(func)
440 write32(Buf + 4, 0x08000000 | (S >> 2)); // j func
441 write32(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
442 write32(Buf + 12, 0x00000000); // nop
443 Target->relocateOne(Buf, R_MIPS_HI16, S);
444 Target->relocateOne(Buf + 8, R_MIPS_LO16, S);
447 void MipsThunk::addSymbols(ThunkSection &IS) {
448 addSymbol(Saver.save("__LA25Thunk_" + Destination.getName()), STT_FUNC, 0,
452 InputSection *MipsThunk::getTargetInputSection() const {
453 auto &DR = cast<Defined>(Destination);
454 return dyn_cast<InputSection>(DR.Section);
457 // Write microMIPS R2-R5 LA25 thunk code
458 // to call PIC function from the non-PIC one.
459 void MicroMipsThunk::writeTo(uint8_t *Buf) {
460 uint64_t S = Destination.getVA() | 1;
461 write16(Buf, 0x41b9); // lui $25, %hi(func)
462 write16(Buf + 4, 0xd400); // j func
463 write16(Buf + 8, 0x3339); // addiu $25, $25, %lo(func)
464 write16(Buf + 12, 0x0c00); // nop
465 Target->relocateOne(Buf, R_MICROMIPS_HI16, S);
466 Target->relocateOne(Buf + 4, R_MICROMIPS_26_S1, S);
467 Target->relocateOne(Buf + 8, R_MICROMIPS_LO16, S);
470 void MicroMipsThunk::addSymbols(ThunkSection &IS) {
471 Defined *D = addSymbol(
472 Saver.save("__microLA25Thunk_" + Destination.getName()), STT_FUNC, 0, IS);
473 D->StOther |= STO_MIPS_MICROMIPS;
476 InputSection *MicroMipsThunk::getTargetInputSection() const {
477 auto &DR = cast<Defined>(Destination);
478 return dyn_cast<InputSection>(DR.Section);
481 // Write microMIPS R6 LA25 thunk code
482 // to call PIC function from the non-PIC one.
483 void MicroMipsR6Thunk::writeTo(uint8_t *Buf) {
484 uint64_t S = Destination.getVA() | 1;
485 uint64_t P = getThunkTargetSym()->getVA();
486 write16(Buf, 0x1320); // lui $25, %hi(func)
487 write16(Buf + 4, 0x3339); // addiu $25, $25, %lo(func)
488 write16(Buf + 8, 0x9400); // bc func
489 Target->relocateOne(Buf, R_MICROMIPS_HI16, S);
490 Target->relocateOne(Buf + 4, R_MICROMIPS_LO16, S);
491 Target->relocateOne(Buf + 8, R_MICROMIPS_PC26_S1, S - P - 12);
494 void MicroMipsR6Thunk::addSymbols(ThunkSection &IS) {
495 Defined *D = addSymbol(
496 Saver.save("__microLA25Thunk_" + Destination.getName()), STT_FUNC, 0, IS);
497 D->StOther |= STO_MIPS_MICROMIPS;
500 InputSection *MicroMipsR6Thunk::getTargetInputSection() const {
501 auto &DR = cast<Defined>(Destination);
502 return dyn_cast<InputSection>(DR.Section);
505 void PPC64PltCallStub::writeTo(uint8_t *Buf) {
506 int64_t Off = Destination.getGotPltVA() - getPPC64TocBase();
507 // Need to add 0x8000 to offset to account for the low bits being signed.
508 uint16_t OffHa = (Off + 0x8000) >> 16;
509 uint16_t OffLo = Off;
511 write32(Buf + 0, 0xf8410018); // std r2,24(r1)
512 write32(Buf + 4, 0x3d820000 | OffHa); // addis r12,r2, X@plt@to@ha
513 write32(Buf + 8, 0xe98c0000 | OffLo); // ld r12,X@plt@toc@l(r12)
514 write32(Buf + 12, 0x7d8903a6); // mtctr r12
515 write32(Buf + 16, 0x4e800420); // bctr
518 void PPC64PltCallStub::addSymbols(ThunkSection &IS) {
519 Defined *S = addSymbol(Saver.save("__plt_" + Destination.getName()), STT_FUNC,
521 S->NeedsTocRestore = true;
524 Thunk::Thunk(Symbol &D) : Destination(D), Offset(0) {}
526 Thunk::~Thunk() = default;
528 static Thunk *addThunkAArch64(RelType Type, Symbol &S) {
529 if (Type != R_AARCH64_CALL26 && Type != R_AARCH64_JUMP26)
530 fatal("unrecognized relocation type");
532 return make<AArch64ADRPThunk>(S);
533 return make<AArch64ABSLongThunk>(S);
536 // Creates a thunk for Thumb-ARM interworking.
537 static Thunk *addThunkArm(RelType Reloc, Symbol &S) {
538 // ARM relocations need ARM to Thumb interworking Thunks.
539 // Thumb relocations need Thumb to ARM relocations.
540 // Use position independent Thunks if we require position independent code.
547 return make<ARMV7PILongThunk>(S);
548 return make<ARMV7ABSLongThunk>(S);
549 case R_ARM_THM_JUMP19:
550 case R_ARM_THM_JUMP24:
553 return make<ThumbV7PILongThunk>(S);
554 return make<ThumbV7ABSLongThunk>(S);
556 fatal("unrecognized relocation type");
559 static Thunk *addThunkMips(RelType Type, Symbol &S) {
560 if ((S.StOther & STO_MIPS_MICROMIPS) && isMipsR6())
561 return make<MicroMipsR6Thunk>(S);
562 if (S.StOther & STO_MIPS_MICROMIPS)
563 return make<MicroMipsThunk>(S);
564 return make<MipsThunk>(S);
567 static Thunk *addThunkPPC64(RelType Type, Symbol &S) {
568 if (Type == R_PPC64_REL24)
569 return make<PPC64PltCallStub>(S);
570 fatal("unexpected relocation type");
573 Thunk *addThunk(RelType Type, Symbol &S) {
574 if (Config->EMachine == EM_AARCH64)
575 return addThunkAArch64(Type, S);
577 if (Config->EMachine == EM_ARM)
578 return addThunkArm(Type, S);
580 if (Config->EMachine == EM_MIPS)
581 return addThunkMips(Type, S);
583 if (Config->EMachine == EM_PPC64)
584 return addThunkPPC64(Type, S);
586 llvm_unreachable("add Thunk only supported for ARM, Mips and PowerPC");
589 } // end namespace elf
590 } // end namespace lld