xref: /llvm-project-15.0.7/lld/ELF/Thunks.cpp (revision 22bdb331)
1 //===- Thunks.cpp --------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===---------------------------------------------------------------------===//
9 //
10 // This file contains Thunk subclasses.
11 //
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.
15 //
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.
19 //
20 // i386 and x86-64 don't need thunks.
21 //
22 //===---------------------------------------------------------------------===//
23 
24 #include "Thunks.h"
25 #include "Config.h"
26 #include "InputSection.h"
27 #include "OutputSections.h"
28 #include "Symbols.h"
29 #include "SyntheticSections.h"
30 #include "Target.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"
38 #include <cstdint>
39 #include <cstring>
40 
41 using namespace llvm;
42 using namespace llvm::object;
43 using namespace llvm::ELF;
44 
45 namespace lld {
46 namespace elf {
47 
48 namespace {
49 
50 // AArch64 long range Thunks
51 class AArch64ABSLongThunk final : public Thunk {
52 public:
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;
57 };
58 
59 class AArch64ADRPThunk final : public Thunk {
60 public:
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;
65 };
66 
67 // Base class for ARM thunks.
68 //
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 {
76 public:
77   ARMThunk(Symbol &Dest) : Thunk(Dest) {}
78 
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;
83 
84   // Returns the size of a long thunk.
85   virtual uint32_t sizeLong() = 0;
86 
87   // Writes a long thunk to Buf.
88   virtual void writeLong(uint8_t *Buf) = 0;
89 
90 private:
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;
98 };
99 
100 // Base class for Thumb-2 thunks.
101 //
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 {
105 public:
106   ThumbThunk(Symbol &Dest) : Thunk(Dest) { Alignment = 2; }
107 
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;
112 
113   // Returns the size of a long thunk.
114   virtual uint32_t sizeLong() = 0;
115 
116   // Writes a long thunk to Buf.
117   virtual void writeLong(uint8_t *Buf) = 0;
118 
119 private:
120   // See comment in ARMThunk above.
121   bool MayUseShortThunk = true;
122 };
123 
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 {
127 public:
128   ARMV7ABSLongThunk(Symbol &Dest) : ARMThunk(Dest) {}
129 
130   uint32_t sizeLong() override { return 12; }
131   void writeLong(uint8_t *Buf) override;
132   void addSymbols(ThunkSection &IS) override;
133 };
134 
135 class ARMV7PILongThunk final : public ARMThunk {
136 public:
137   ARMV7PILongThunk(Symbol &Dest) : ARMThunk(Dest) {}
138 
139   uint32_t sizeLong() override { return 16; }
140   void writeLong(uint8_t *Buf) override;
141   void addSymbols(ThunkSection &IS) override;
142 };
143 
144 class ThumbV7ABSLongThunk final : public ThumbThunk {
145 public:
146   ThumbV7ABSLongThunk(Symbol &Dest) : ThumbThunk(Dest) {}
147 
148   uint32_t sizeLong() override { return 10; }
149   void writeLong(uint8_t *Buf) override;
150   void addSymbols(ThunkSection &IS) override;
151 };
152 
153 class ThumbV7PILongThunk final : public ThumbThunk {
154 public:
155   ThumbV7PILongThunk(Symbol &Dest) : ThumbThunk(Dest) {}
156 
157   uint32_t sizeLong() override { return 12; }
158   void writeLong(uint8_t *Buf) override;
159   void addSymbols(ThunkSection &IS) override;
160 };
161 
162 // Implementations of Thunks for older Arm architectures that do not support
163 // the movt/movw instructions. These thunks require at least Architecture v5
164 // as used on processors such as the Arm926ej-s. There are no Thumb entry
165 // points as there is no Thumb branch instruction on these architecture that
166 // can result in a thunk
167 class ARMV5ABSLongThunk final : public ARMThunk {
168 public:
169   ARMV5ABSLongThunk(Symbol &Dest) : ARMThunk(Dest) {}
170 
171   uint32_t sizeLong() override { return 8; }
172   void writeLong(uint8_t *Buf) override;
173   void addSymbols(ThunkSection &IS) override;
174   bool isCompatibleWith(uint32_t RelocType) const override;
175 };
176 
177 class ARMV5PILongThunk final : public ARMThunk {
178 public:
179   ARMV5PILongThunk(Symbol &Dest) : ARMThunk(Dest) {}
180 
181   uint32_t sizeLong() override { return 16; }
182   void writeLong(uint8_t *Buf) override;
183   void addSymbols(ThunkSection &IS) override;
184   bool isCompatibleWith(uint32_t RelocType) const override;
185 };
186 
187 // MIPS LA25 thunk
188 class MipsThunk final : public Thunk {
189 public:
190   MipsThunk(Symbol &Dest) : Thunk(Dest) {}
191 
192   uint32_t size() override { return 16; }
193   void writeTo(uint8_t *Buf) override;
194   void addSymbols(ThunkSection &IS) override;
195   InputSection *getTargetInputSection() const override;
196 };
197 
198 // microMIPS R2-R5 LA25 thunk
199 class MicroMipsThunk final : public Thunk {
200 public:
201   MicroMipsThunk(Symbol &Dest) : Thunk(Dest) {}
202 
203   uint32_t size() override { return 14; }
204   void writeTo(uint8_t *Buf) override;
205   void addSymbols(ThunkSection &IS) override;
206   InputSection *getTargetInputSection() const override;
207 };
208 
209 // microMIPS R6 LA25 thunk
210 class MicroMipsR6Thunk final : public Thunk {
211 public:
212   MicroMipsR6Thunk(Symbol &Dest) : Thunk(Dest) {}
213 
214   uint32_t size() override { return 12; }
215   void writeTo(uint8_t *Buf) override;
216   void addSymbols(ThunkSection &IS) override;
217   InputSection *getTargetInputSection() const override;
218 };
219 
220 
221 // PPC64 Plt call stubs.
222 // Any call site that needs to call through a plt entry needs a call stub in
223 // the .text section. The call stub is responsible for:
224 // 1) Saving the toc-pointer to the stack.
225 // 2) Loading the target functions address from the procedure linkage table into
226 //    r12 for use by the target functions global entry point, and into the count
227 //    register.
228 // 3) Transfering control to the target function through an indirect branch.
229 class PPC64PltCallStub final : public Thunk {
230 public:
231   PPC64PltCallStub(Symbol &Dest) : Thunk(Dest) {}
232   uint32_t size() override { return 20; }
233   void writeTo(uint8_t *Buf) override;
234   void addSymbols(ThunkSection &IS) override;
235 };
236 
237 // A bl instruction uses a signed 24 bit offset, with an implicit 4 byte
238 // alignment. This gives a possible 26 bits of 'reach'. If the call offset is
239 // larger then that we need to emit a long-branch thunk. The target address
240 // of the callee is stored in a table to be accessed TOC-relative. Since the
241 // call must be local (a non-local call will have a PltCallStub instead) the
242 // table stores the address of the callee's local entry point. For
243 // position-independent code a corresponding relative dynamic relocation is
244 // used.
245 class PPC64LongBranchThunk : public Thunk {
246 public:
247   uint32_t size() override { return 16; }
248   void writeTo(uint8_t *Buf) override;
249   void addSymbols(ThunkSection &IS) override;
250 
251 protected:
252   PPC64LongBranchThunk(Symbol &Dest) : Thunk(Dest) {}
253 };
254 
255 class PPC64PILongBranchThunk final : public PPC64LongBranchThunk {
256 public:
257   PPC64PILongBranchThunk(Symbol &Dest) : PPC64LongBranchThunk(Dest) {
258     assert(!Dest.IsPreemptible);
259     if (Dest.isInPPC64Branchlt())
260       return;
261 
262     In.PPC64LongBranchTarget->addEntry(Dest);
263     In.RelaDyn->addReloc({Target->RelativeRel, In.PPC64LongBranchTarget,
264                           Dest.getPPC64LongBranchOffset(), true, &Dest,
265                           getPPC64GlobalEntryToLocalEntryOffset(Dest.StOther)});
266   }
267 };
268 
269 class PPC64PDLongBranchThunk final : public PPC64LongBranchThunk {
270 public:
271   PPC64PDLongBranchThunk(Symbol &Dest) : PPC64LongBranchThunk(Dest) {
272     if (!Dest.isInPPC64Branchlt())
273       In.PPC64LongBranchTarget->addEntry(Dest);
274   }
275 };
276 
277 } // end anonymous namespace
278 
279 Defined *Thunk::addSymbol(StringRef Name, uint8_t Type, uint64_t Value,
280                           InputSectionBase &Section) {
281   Defined *D = addSyntheticLocal(Name, Type, Value, /*Size=*/0, Section);
282   Syms.push_back(D);
283   return D;
284 }
285 
286 void Thunk::setOffset(uint64_t NewOffset) {
287   for (Defined *D : Syms)
288     D->Value = D->Value - Offset + NewOffset;
289   Offset = NewOffset;
290 }
291 
292 // AArch64 long range Thunks
293 
294 static uint64_t getAArch64ThunkDestVA(const Symbol &S) {
295   uint64_t V = S.isInPlt() ? S.getPltVA() : S.getVA();
296   return V;
297 }
298 
299 void AArch64ABSLongThunk::writeTo(uint8_t *Buf) {
300   const uint8_t Data[] = {
301     0x50, 0x00, 0x00, 0x58, //     ldr x16, L0
302     0x00, 0x02, 0x1f, 0xd6, //     br  x16
303     0x00, 0x00, 0x00, 0x00, // L0: .xword S
304     0x00, 0x00, 0x00, 0x00,
305   };
306   uint64_t S = getAArch64ThunkDestVA(Destination);
307   memcpy(Buf, Data, sizeof(Data));
308   Target->relocateOne(Buf + 8, R_AARCH64_ABS64, S);
309 }
310 
311 void AArch64ABSLongThunk::addSymbols(ThunkSection &IS) {
312   addSymbol(Saver.save("__AArch64AbsLongThunk_" + Destination.getName()),
313             STT_FUNC, 0, IS);
314   addSymbol("$x", STT_NOTYPE, 0, IS);
315   addSymbol("$d", STT_NOTYPE, 8, IS);
316 }
317 
318 // This Thunk has a maximum range of 4Gb, this is sufficient for all programs
319 // using the small code model, including pc-relative ones. At time of writing
320 // clang and gcc do not support the large code model for position independent
321 // code so it is safe to use this for position independent thunks without
322 // worrying about the destination being more than 4Gb away.
323 void AArch64ADRPThunk::writeTo(uint8_t *Buf) {
324   const uint8_t Data[] = {
325       0x10, 0x00, 0x00, 0x90, // adrp x16, Dest R_AARCH64_ADR_PREL_PG_HI21(Dest)
326       0x10, 0x02, 0x00, 0x91, // add  x16, x16, R_AARCH64_ADD_ABS_LO12_NC(Dest)
327       0x00, 0x02, 0x1f, 0xd6, // br   x16
328   };
329   uint64_t S = getAArch64ThunkDestVA(Destination);
330   uint64_t P = getThunkTargetSym()->getVA();
331   memcpy(Buf, Data, sizeof(Data));
332   Target->relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
333                       getAArch64Page(S) - getAArch64Page(P));
334   Target->relocateOne(Buf + 4, R_AARCH64_ADD_ABS_LO12_NC, S);
335 }
336 
337 void AArch64ADRPThunk::addSymbols(ThunkSection &IS) {
338   addSymbol(Saver.save("__AArch64ADRPThunk_" + Destination.getName()), STT_FUNC,
339             0, IS);
340   addSymbol("$x", STT_NOTYPE, 0, IS);
341 }
342 
343 // ARM Target Thunks
344 static uint64_t getARMThunkDestVA(const Symbol &S) {
345   uint64_t V = S.isInPlt() ? S.getPltVA() : S.getVA();
346   return SignExtend64<32>(V);
347 }
348 
349 // This function returns true if the target is not Thumb and is within 2^26, and
350 // it has not previously returned false (see comment for MayUseShortThunk).
351 bool ARMThunk::mayUseShortThunk() {
352   if (!MayUseShortThunk)
353     return false;
354   uint64_t S = getARMThunkDestVA(Destination);
355   if (S & 1) {
356     MayUseShortThunk = false;
357     return false;
358   }
359   uint64_t P = getThunkTargetSym()->getVA();
360   int64_t Offset = S - P - 8;
361   MayUseShortThunk = llvm::isInt<26>(Offset);
362   return MayUseShortThunk;
363 }
364 
365 void ARMThunk::writeTo(uint8_t *Buf) {
366   if (!mayUseShortThunk()) {
367     writeLong(Buf);
368     return;
369   }
370 
371   uint64_t S = getARMThunkDestVA(Destination);
372   uint64_t P = getThunkTargetSym()->getVA();
373   int64_t Offset = S - P - 8;
374   const uint8_t Data[] = {
375     0x00, 0x00, 0x00, 0xea, // b S
376   };
377   memcpy(Buf, Data, sizeof(Data));
378   Target->relocateOne(Buf, R_ARM_JUMP24, Offset);
379 }
380 
381 bool ARMThunk::isCompatibleWith(RelType Type) const {
382   // Thumb branch relocations can't use BLX
383   return Type != R_ARM_THM_JUMP19 && Type != R_ARM_THM_JUMP24;
384 }
385 
386 // This function returns true if the target is Thumb and is within 2^25, and
387 // it has not previously returned false (see comment for MayUseShortThunk).
388 bool ThumbThunk::mayUseShortThunk() {
389   if (!MayUseShortThunk)
390     return false;
391   uint64_t S = getARMThunkDestVA(Destination);
392   if ((S & 1) == 0) {
393     MayUseShortThunk = false;
394     return false;
395   }
396   uint64_t P = getThunkTargetSym()->getVA() & ~1;
397   int64_t Offset = S - P - 4;
398   MayUseShortThunk = llvm::isInt<25>(Offset);
399   return MayUseShortThunk;
400 }
401 
402 void ThumbThunk::writeTo(uint8_t *Buf) {
403   if (!mayUseShortThunk()) {
404     writeLong(Buf);
405     return;
406   }
407 
408   uint64_t S = getARMThunkDestVA(Destination);
409   uint64_t P = getThunkTargetSym()->getVA();
410   int64_t Offset = S - P - 4;
411   const uint8_t Data[] = {
412       0x00, 0xf0, 0x00, 0xb0, // b.w S
413   };
414   memcpy(Buf, Data, sizeof(Data));
415   Target->relocateOne(Buf, R_ARM_THM_JUMP24, Offset);
416 }
417 
418 bool ThumbThunk::isCompatibleWith(RelType Type) const {
419   // ARM branch relocations can't use BLX
420   return Type != R_ARM_JUMP24 && Type != R_ARM_PC24 && Type != R_ARM_PLT32;
421 }
422 
423 void ARMV7ABSLongThunk::writeLong(uint8_t *Buf) {
424   const uint8_t Data[] = {
425       0x00, 0xc0, 0x00, 0xe3, // movw         ip,:lower16:S
426       0x00, 0xc0, 0x40, 0xe3, // movt         ip,:upper16:S
427       0x1c, 0xff, 0x2f, 0xe1, // bx   ip
428   };
429   uint64_t S = getARMThunkDestVA(Destination);
430   memcpy(Buf, Data, sizeof(Data));
431   Target->relocateOne(Buf, R_ARM_MOVW_ABS_NC, S);
432   Target->relocateOne(Buf + 4, R_ARM_MOVT_ABS, S);
433 }
434 
435 void ARMV7ABSLongThunk::addSymbols(ThunkSection &IS) {
436   addSymbol(Saver.save("__ARMv7ABSLongThunk_" + Destination.getName()),
437             STT_FUNC, 0, IS);
438   addSymbol("$a", STT_NOTYPE, 0, IS);
439 }
440 
441 void ThumbV7ABSLongThunk::writeLong(uint8_t *Buf) {
442   const uint8_t Data[] = {
443       0x40, 0xf2, 0x00, 0x0c, // movw         ip, :lower16:S
444       0xc0, 0xf2, 0x00, 0x0c, // movt         ip, :upper16:S
445       0x60, 0x47,             // bx   ip
446   };
447   uint64_t S = getARMThunkDestVA(Destination);
448   memcpy(Buf, Data, sizeof(Data));
449   Target->relocateOne(Buf, R_ARM_THM_MOVW_ABS_NC, S);
450   Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_ABS, S);
451 }
452 
453 void ThumbV7ABSLongThunk::addSymbols(ThunkSection &IS) {
454   addSymbol(Saver.save("__Thumbv7ABSLongThunk_" + Destination.getName()),
455             STT_FUNC, 1, IS);
456   addSymbol("$t", STT_NOTYPE, 0, IS);
457 }
458 
459 void ARMV7PILongThunk::writeLong(uint8_t *Buf) {
460   const uint8_t Data[] = {
461       0xf0, 0xcf, 0x0f, 0xe3, // P:  movw ip,:lower16:S - (P + (L1-P) + 8)
462       0x00, 0xc0, 0x40, 0xe3, //     movt ip,:upper16:S - (P + (L1-P) + 8)
463       0x0f, 0xc0, 0x8c, 0xe0, // L1: add  ip, ip, pc
464       0x1c, 0xff, 0x2f, 0xe1, //     bx   ip
465   };
466   uint64_t S = getARMThunkDestVA(Destination);
467   uint64_t P = getThunkTargetSym()->getVA();
468   uint64_t Offset = S - P - 16;
469   memcpy(Buf, Data, sizeof(Data));
470   Target->relocateOne(Buf, R_ARM_MOVW_PREL_NC, Offset);
471   Target->relocateOne(Buf + 4, R_ARM_MOVT_PREL, Offset);
472 }
473 
474 void ARMV7PILongThunk::addSymbols(ThunkSection &IS) {
475   addSymbol(Saver.save("__ARMV7PILongThunk_" + Destination.getName()), STT_FUNC,
476             0, IS);
477   addSymbol("$a", STT_NOTYPE, 0, IS);
478 }
479 
480 void ThumbV7PILongThunk::writeLong(uint8_t *Buf) {
481   const uint8_t Data[] = {
482       0x4f, 0xf6, 0xf4, 0x7c, // P:  movw ip,:lower16:S - (P + (L1-P) + 4)
483       0xc0, 0xf2, 0x00, 0x0c, //     movt ip,:upper16:S - (P + (L1-P) + 4)
484       0xfc, 0x44,             // L1: add  ip, pc
485       0x60, 0x47,             //     bx   ip
486   };
487   uint64_t S = getARMThunkDestVA(Destination);
488   uint64_t P = getThunkTargetSym()->getVA() & ~0x1;
489   uint64_t Offset = S - P - 12;
490   memcpy(Buf, Data, sizeof(Data));
491   Target->relocateOne(Buf, R_ARM_THM_MOVW_PREL_NC, Offset);
492   Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_PREL, Offset);
493 }
494 
495 void ThumbV7PILongThunk::addSymbols(ThunkSection &IS) {
496   addSymbol(Saver.save("__ThumbV7PILongThunk_" + Destination.getName()),
497             STT_FUNC, 1, IS);
498   addSymbol("$t", STT_NOTYPE, 0, IS);
499 }
500 
501 void ARMV5ABSLongThunk::writeLong(uint8_t *Buf) {
502   const uint8_t Data[] = {
503       0x04, 0xf0, 0x1f, 0xe5, //     ldr pc, [pc,#-4] ; L1
504       0x00, 0x00, 0x00, 0x00, // L1: .word S
505   };
506   memcpy(Buf, Data, sizeof(Data));
507   Target->relocateOne(Buf + 4, R_ARM_ABS32, getARMThunkDestVA(Destination));
508 }
509 
510 void ARMV5ABSLongThunk::addSymbols(ThunkSection &IS) {
511   addSymbol(Saver.save("__ARMv5ABSLongThunk_" + Destination.getName()),
512             STT_FUNC, 0, IS);
513   addSymbol("$a", STT_NOTYPE, 0, IS);
514   addSymbol("$d", STT_NOTYPE, 4, IS);
515 }
516 
517 bool ARMV5ABSLongThunk::isCompatibleWith(uint32_t RelocType) const {
518   // Thumb branch relocations can't use BLX
519   return RelocType != R_ARM_THM_JUMP19 && RelocType != R_ARM_THM_JUMP24;
520 }
521 
522 void ARMV5PILongThunk::writeLong(uint8_t *Buf) {
523   const uint8_t Data[] = {
524       0x04, 0xc0, 0x9f, 0xe5, // P:  ldr ip, [pc,#4] ; L2
525       0x0c, 0xc0, 0x8f, 0xe0, // L1: add ip, pc, ip
526       0x1c, 0xff, 0x2f, 0xe1, //     bx ip
527       0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 8)
528   };
529   uint64_t S = getARMThunkDestVA(Destination);
530   uint64_t P = getThunkTargetSym()->getVA() & ~0x1;
531   memcpy(Buf, Data, sizeof(Data));
532   Target->relocateOne(Buf + 12, R_ARM_REL32, S - P - 12);
533 }
534 
535 void ARMV5PILongThunk::addSymbols(ThunkSection &IS) {
536   addSymbol(Saver.save("__ARMV5PILongThunk_" + Destination.getName()), STT_FUNC,
537             0, IS);
538   addSymbol("$a", STT_NOTYPE, 0, IS);
539   addSymbol("$d", STT_NOTYPE, 12, IS);
540 }
541 
542 bool ARMV5PILongThunk::isCompatibleWith(uint32_t RelocType) const {
543   // Thumb branch relocations can't use BLX
544   return RelocType != R_ARM_THM_JUMP19 && RelocType != R_ARM_THM_JUMP24;
545 }
546 
547 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
548 void MipsThunk::writeTo(uint8_t *Buf) {
549   uint64_t S = Destination.getVA();
550   write32(Buf, 0x3c190000); // lui   $25, %hi(func)
551   write32(Buf + 4, 0x08000000 | (S >> 2)); // j     func
552   write32(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
553   write32(Buf + 12, 0x00000000); // nop
554   Target->relocateOne(Buf, R_MIPS_HI16, S);
555   Target->relocateOne(Buf + 8, R_MIPS_LO16, S);
556 }
557 
558 void MipsThunk::addSymbols(ThunkSection &IS) {
559   addSymbol(Saver.save("__LA25Thunk_" + Destination.getName()), STT_FUNC, 0,
560             IS);
561 }
562 
563 InputSection *MipsThunk::getTargetInputSection() const {
564   auto &DR = cast<Defined>(Destination);
565   return dyn_cast<InputSection>(DR.Section);
566 }
567 
568 // Write microMIPS R2-R5 LA25 thunk code
569 // to call PIC function from the non-PIC one.
570 void MicroMipsThunk::writeTo(uint8_t *Buf) {
571   uint64_t S = Destination.getVA() | 1;
572   write16(Buf, 0x41b9);       // lui   $25, %hi(func)
573   write16(Buf + 4, 0xd400);   // j     func
574   write16(Buf + 8, 0x3339);   // addiu $25, $25, %lo(func)
575   write16(Buf + 12, 0x0c00);  // nop
576   Target->relocateOne(Buf, R_MICROMIPS_HI16, S);
577   Target->relocateOne(Buf + 4, R_MICROMIPS_26_S1, S);
578   Target->relocateOne(Buf + 8, R_MICROMIPS_LO16, S);
579 }
580 
581 void MicroMipsThunk::addSymbols(ThunkSection &IS) {
582   Defined *D = addSymbol(
583       Saver.save("__microLA25Thunk_" + Destination.getName()), STT_FUNC, 0, IS);
584   D->StOther |= STO_MIPS_MICROMIPS;
585 }
586 
587 InputSection *MicroMipsThunk::getTargetInputSection() const {
588   auto &DR = cast<Defined>(Destination);
589   return dyn_cast<InputSection>(DR.Section);
590 }
591 
592 // Write microMIPS R6 LA25 thunk code
593 // to call PIC function from the non-PIC one.
594 void MicroMipsR6Thunk::writeTo(uint8_t *Buf) {
595   uint64_t S = Destination.getVA() | 1;
596   uint64_t P = getThunkTargetSym()->getVA();
597   write16(Buf, 0x1320);       // lui   $25, %hi(func)
598   write16(Buf + 4, 0x3339);   // addiu $25, $25, %lo(func)
599   write16(Buf + 8, 0x9400);   // bc    func
600   Target->relocateOne(Buf, R_MICROMIPS_HI16, S);
601   Target->relocateOne(Buf + 4, R_MICROMIPS_LO16, S);
602   Target->relocateOne(Buf + 8, R_MICROMIPS_PC26_S1, S - P - 12);
603 }
604 
605 void MicroMipsR6Thunk::addSymbols(ThunkSection &IS) {
606   Defined *D = addSymbol(
607       Saver.save("__microLA25Thunk_" + Destination.getName()), STT_FUNC, 0, IS);
608   D->StOther |= STO_MIPS_MICROMIPS;
609 }
610 
611 InputSection *MicroMipsR6Thunk::getTargetInputSection() const {
612   auto &DR = cast<Defined>(Destination);
613   return dyn_cast<InputSection>(DR.Section);
614 }
615 
616 static void writePPCLoadAndBranch(uint8_t *Buf, int64_t Offset) {
617   uint16_t OffHa = (Offset + 0x8000) >> 16;
618   uint16_t OffLo = Offset & 0xffff;
619 
620   write32(Buf + 0, 0x3d820000 | OffHa); // addis r12, r2, OffHa
621   write32(Buf + 4, 0xe98c0000 | OffLo); // ld    r12, OffLo(r12)
622   write32(Buf + 8, 0x7d8903a6);         // mtctr r12
623   write32(Buf + 12, 0x4e800420);        // bctr
624 }
625 
626 void PPC64PltCallStub::writeTo(uint8_t *Buf) {
627   int64_t Offset = Destination.getGotPltVA() - getPPC64TocBase();
628   // Save the TOC pointer to the save-slot reserved in the call frame.
629   write32(Buf + 0, 0xf8410018); // std     r2,24(r1)
630   writePPCLoadAndBranch(Buf + 4, Offset);
631 }
632 
633 void PPC64PltCallStub::addSymbols(ThunkSection &IS) {
634   Defined *S = addSymbol(Saver.save("__plt_" + Destination.getName()), STT_FUNC,
635                          0, IS);
636   S->NeedsTocRestore = true;
637 }
638 
639 void PPC64LongBranchThunk::writeTo(uint8_t *Buf) {
640   int64_t Offset = Destination.getPPC64LongBranchTableVA() - getPPC64TocBase();
641   writePPCLoadAndBranch(Buf, Offset);
642 }
643 
644 void PPC64LongBranchThunk::addSymbols(ThunkSection &IS) {
645   addSymbol(Saver.save("__long_branch_" + Destination.getName()), STT_FUNC, 0,
646             IS);
647 }
648 
649 Thunk::Thunk(Symbol &D) : Destination(D), Offset(0) {}
650 
651 Thunk::~Thunk() = default;
652 
653 static Thunk *addThunkAArch64(RelType Type, Symbol &S) {
654   if (Type != R_AARCH64_CALL26 && Type != R_AARCH64_JUMP26)
655     fatal("unrecognized relocation type");
656   if (Config->Pic)
657     return make<AArch64ADRPThunk>(S);
658   return make<AArch64ABSLongThunk>(S);
659 }
660 
661 // Creates a thunk for Thumb-ARM interworking.
662 // Arm Architectures v5 and v6 do not support Thumb2 technology. This means
663 // - MOVT and MOVW instructions cannot be used
664 // - Only Thumb relocation that can generate a Thunk is a BL, this can always
665 //   be transformed into a BLX
666 static Thunk *addThunkPreArmv7(RelType Reloc, Symbol &S) {
667   switch (Reloc) {
668   case R_ARM_PC24:
669   case R_ARM_PLT32:
670   case R_ARM_JUMP24:
671   case R_ARM_CALL:
672   case R_ARM_THM_CALL:
673     if (Config->Pic)
674       return make<ARMV5PILongThunk>(S);
675     return make<ARMV5ABSLongThunk>(S);
676   }
677   fatal("relocation " + toString(Reloc) + " to " + toString(S) +
678         " not supported for Armv5 or Armv6 targets");
679 }
680 
681 // Creates a thunk for Thumb-ARM interworking or branch range extension.
682 static Thunk *addThunkArm(RelType Reloc, Symbol &S) {
683   // Decide which Thunk is needed based on:
684   // Available instruction set
685   // - An Arm Thunk can only be used if Arm state is available.
686   // - A Thumb Thunk can only be used if Thumb state is available.
687   // - Can only use a Thunk if it uses instructions that the Target supports.
688   // Relocation is branch or branch and link
689   // - Branch instructions cannot change state, can only select Thunk that
690   //   starts in the same state as the caller.
691   // - Branch and link relocations can change state, can select Thunks from
692   //   either Arm or Thumb.
693   // Position independent Thunks if we require position independent code.
694 
695   if (!Config->ARMHasMovtMovw) {
696     if (!Config->ARMJ1J2BranchEncoding)
697       return addThunkPreArmv7(Reloc, S);
698     else
699       // The Armv6-m architecture (Cortex-M0) does not have Arm instructions or
700       // support the MOVT MOVW instructions so it cannot use any of the Thunks
701       // currently implemented.
702       fatal("thunks not supported for architecture Armv6-m");
703   }
704 
705   switch (Reloc) {
706   case R_ARM_PC24:
707   case R_ARM_PLT32:
708   case R_ARM_JUMP24:
709   case R_ARM_CALL:
710     if (Config->Pic)
711       return make<ARMV7PILongThunk>(S);
712     return make<ARMV7ABSLongThunk>(S);
713   case R_ARM_THM_JUMP19:
714   case R_ARM_THM_JUMP24:
715   case R_ARM_THM_CALL:
716     if (Config->Pic)
717       return make<ThumbV7PILongThunk>(S);
718     return make<ThumbV7ABSLongThunk>(S);
719   }
720   fatal("unrecognized relocation type");
721 }
722 
723 static Thunk *addThunkMips(RelType Type, Symbol &S) {
724   if ((S.StOther & STO_MIPS_MICROMIPS) && isMipsR6())
725     return make<MicroMipsR6Thunk>(S);
726   if (S.StOther & STO_MIPS_MICROMIPS)
727     return make<MicroMipsThunk>(S);
728   return make<MipsThunk>(S);
729 }
730 
731 static Thunk *addThunkPPC64(RelType Type, Symbol &S) {
732   assert(Type == R_PPC64_REL24 && "unexpected relocation type for thunk");
733   if (S.isInPlt())
734     return make<PPC64PltCallStub>(S);
735 
736   if (Config->Pic)
737     return make<PPC64PILongBranchThunk>(S);
738 
739   return make<PPC64PDLongBranchThunk>(S);
740 }
741 
742 Thunk *addThunk(RelType Type, Symbol &S) {
743   if (Config->EMachine == EM_AARCH64)
744     return addThunkAArch64(Type, S);
745 
746   if (Config->EMachine == EM_ARM)
747     return addThunkArm(Type, S);
748 
749   if (Config->EMachine == EM_MIPS)
750     return addThunkMips(Type, S);
751 
752   if (Config->EMachine == EM_PPC64)
753     return addThunkPPC64(Type, S);
754 
755   llvm_unreachable("add Thunk only supported for ARM, Mips and PowerPC");
756 }
757 
758 } // end namespace elf
759 } // end namespace lld
760