xref: /freebsd/contrib/llvm-project/lld/ELF/Thunks.cpp (revision 18054d0220cfc8df9c9568c437bd6fbb59d53c3c)
1 //===- Thunks.cpp --------------------------------------------------------===//
2 //
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
6 //
7 //===---------------------------------------------------------------------===//
8 //
9 // This file contains Thunk subclasses.
10 //
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.
14 //
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.
18 //
19 // i386 and x86-64 don't need thunks.
20 //
21 //===---------------------------------------------------------------------===//
22 
23 #include "Thunks.h"
24 #include "Config.h"
25 #include "InputSection.h"
26 #include "OutputSections.h"
27 #include "Symbols.h"
28 #include "SyntheticSections.h"
29 #include "Target.h"
30 #include "lld/Common/CommonLinkerContext.h"
31 #include "llvm/BinaryFormat/ELF.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/Endian.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/MathExtras.h"
36 #include <cstdint>
37 #include <cstring>
38 
39 using namespace llvm;
40 using namespace llvm::object;
41 using namespace llvm::ELF;
42 using namespace lld;
43 using namespace lld::elf;
44 
45 namespace {
46 
47 // AArch64 long range Thunks
48 class AArch64ABSLongThunk final : public Thunk {
49 public:
50   AArch64ABSLongThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
51   uint32_t size() override { return 16; }
52   void writeTo(uint8_t *buf) override;
53   void addSymbols(ThunkSection &isec) override;
54 };
55 
56 class AArch64ADRPThunk final : public Thunk {
57 public:
58   AArch64ADRPThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
59   uint32_t size() override { return 12; }
60   void writeTo(uint8_t *buf) override;
61   void addSymbols(ThunkSection &isec) override;
62 };
63 
64 // Base class for ARM thunks.
65 //
66 // An ARM thunk may be either short or long. A short thunk is simply a branch
67 // (B) instruction, and it may be used to call ARM functions when the distance
68 // from the thunk to the target is less than 32MB. Long thunks can branch to any
69 // virtual address and can switch between ARM and Thumb, and they are
70 // implemented in the derived classes. This class tries to create a short thunk
71 // if the target is in range, otherwise it creates a long thunk.
72 class ARMThunk : public Thunk {
73 public:
74   ARMThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
75 
76   bool getMayUseShortThunk();
77   uint32_t size() override { return getMayUseShortThunk() ? 4 : sizeLong(); }
78   void writeTo(uint8_t *buf) override;
79   bool isCompatibleWith(const InputSection &isec,
80                         const Relocation &rel) const override;
81 
82   // Returns the size of a long thunk.
83   virtual uint32_t sizeLong() = 0;
84 
85   // Writes a long thunk to Buf.
86   virtual void writeLong(uint8_t *buf) = 0;
87 
88 private:
89   // This field tracks whether all previously considered layouts would allow
90   // this thunk to be short. If we have ever needed a long thunk, we always
91   // create a long thunk, even if the thunk may be short given the current
92   // distance to the target. We do this because transitioning from long to short
93   // can create layout oscillations in certain corner cases which would prevent
94   // the layout from converging.
95   bool mayUseShortThunk = true;
96 };
97 
98 // Base class for Thumb-2 thunks.
99 //
100 // This class is similar to ARMThunk, but it uses the Thumb-2 B.W instruction
101 // which has a range of 16MB.
102 class ThumbThunk : public Thunk {
103 public:
104   ThumbThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {
105     alignment = 2;
106   }
107 
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;
113 
114   // Returns the size of a long thunk.
115   virtual uint32_t sizeLong() = 0;
116 
117   // Writes a long thunk to Buf.
118   virtual void writeLong(uint8_t *buf) = 0;
119 
120 private:
121   // See comment in ARMThunk above.
122   bool mayUseShortThunk = true;
123 };
124 
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 {
128 public:
129   ARMV7ABSLongThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}
130 
131   uint32_t sizeLong() override { return 12; }
132   void writeLong(uint8_t *buf) override;
133   void addSymbols(ThunkSection &isec) override;
134 };
135 
136 class ARMV7PILongThunk final : public ARMThunk {
137 public:
138   ARMV7PILongThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}
139 
140   uint32_t sizeLong() override { return 16; }
141   void writeLong(uint8_t *buf) override;
142   void addSymbols(ThunkSection &isec) override;
143 };
144 
145 class ThumbV7ABSLongThunk final : public ThumbThunk {
146 public:
147   ThumbV7ABSLongThunk(Symbol &dest, int64_t addend)
148       : ThumbThunk(dest, addend) {}
149 
150   uint32_t sizeLong() override { return 10; }
151   void writeLong(uint8_t *buf) override;
152   void addSymbols(ThunkSection &isec) override;
153 };
154 
155 class ThumbV7PILongThunk final : public ThumbThunk {
156 public:
157   ThumbV7PILongThunk(Symbol &dest, int64_t addend) : ThumbThunk(dest, addend) {}
158 
159   uint32_t sizeLong() override { return 12; }
160   void writeLong(uint8_t *buf) override;
161   void addSymbols(ThunkSection &isec) override;
162 };
163 
164 // Implementations of Thunks for older Arm architectures that do not support
165 // the movt/movw instructions. These thunks require at least Architecture v5
166 // as used on processors such as the Arm926ej-s. There are no Thumb entry
167 // points as there is no Thumb branch instruction on these architecture that
168 // can result in a thunk
169 class ARMV5ABSLongThunk final : public ARMThunk {
170 public:
171   ARMV5ABSLongThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}
172 
173   uint32_t sizeLong() override { return 8; }
174   void writeLong(uint8_t *buf) override;
175   void addSymbols(ThunkSection &isec) override;
176   bool isCompatibleWith(const InputSection &isec,
177                         const Relocation &rel) const override;
178 };
179 
180 class ARMV5PILongThunk final : public ARMThunk {
181 public:
182   ARMV5PILongThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}
183 
184   uint32_t sizeLong() override { return 16; }
185   void writeLong(uint8_t *buf) override;
186   void addSymbols(ThunkSection &isec) override;
187   bool isCompatibleWith(const InputSection &isec,
188                         const Relocation &rel) const override;
189 };
190 
191 // Implementations of Thunks for Arm v6-M. Only Thumb instructions are permitted
192 class ThumbV6MABSLongThunk final : public ThumbThunk {
193 public:
194   ThumbV6MABSLongThunk(Symbol &dest, int64_t addend)
195       : ThumbThunk(dest, addend) {}
196 
197   uint32_t sizeLong() override { return 12; }
198   void writeLong(uint8_t *buf) override;
199   void addSymbols(ThunkSection &isec) override;
200 };
201 
202 class ThumbV6MPILongThunk final : public ThumbThunk {
203 public:
204   ThumbV6MPILongThunk(Symbol &dest, int64_t addend)
205       : ThumbThunk(dest, addend) {}
206 
207   uint32_t sizeLong() override { return 16; }
208   void writeLong(uint8_t *buf) override;
209   void addSymbols(ThunkSection &isec) override;
210 };
211 
212 // MIPS LA25 thunk
213 class MipsThunk final : public Thunk {
214 public:
215   MipsThunk(Symbol &dest) : Thunk(dest, 0) {}
216 
217   uint32_t size() override { return 16; }
218   void writeTo(uint8_t *buf) override;
219   void addSymbols(ThunkSection &isec) override;
220   InputSection *getTargetInputSection() const override;
221 };
222 
223 // microMIPS R2-R5 LA25 thunk
224 class MicroMipsThunk final : public Thunk {
225 public:
226   MicroMipsThunk(Symbol &dest) : Thunk(dest, 0) {}
227 
228   uint32_t size() override { return 14; }
229   void writeTo(uint8_t *buf) override;
230   void addSymbols(ThunkSection &isec) override;
231   InputSection *getTargetInputSection() const override;
232 };
233 
234 // microMIPS R6 LA25 thunk
235 class MicroMipsR6Thunk final : public Thunk {
236 public:
237   MicroMipsR6Thunk(Symbol &dest) : Thunk(dest, 0) {}
238 
239   uint32_t size() override { return 12; }
240   void writeTo(uint8_t *buf) override;
241   void addSymbols(ThunkSection &isec) override;
242   InputSection *getTargetInputSection() const override;
243 };
244 
245 class PPC32PltCallStub final : public Thunk {
246 public:
247   // For R_PPC_PLTREL24, Thunk::addend records the addend which will be used to
248   // decide the offsets in the call stub.
249   PPC32PltCallStub(const InputSection &isec, const Relocation &rel,
250                    Symbol &dest)
251       : Thunk(dest, rel.addend), file(isec.file) {}
252   uint32_t size() override { return 16; }
253   void writeTo(uint8_t *buf) override;
254   void addSymbols(ThunkSection &isec) override;
255   bool isCompatibleWith(const InputSection &isec, const Relocation &rel) const override;
256 
257 private:
258   // Records the call site of the call stub.
259   const InputFile *file;
260 };
261 
262 class PPC32LongThunk final : public Thunk {
263 public:
264   PPC32LongThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
265   uint32_t size() override { return config->isPic ? 32 : 16; }
266   void writeTo(uint8_t *buf) override;
267   void addSymbols(ThunkSection &isec) override;
268 };
269 
270 // PPC64 Plt call stubs.
271 // Any call site that needs to call through a plt entry needs a call stub in
272 // the .text section. The call stub is responsible for:
273 // 1) Saving the toc-pointer to the stack.
274 // 2) Loading the target functions address from the procedure linkage table into
275 //    r12 for use by the target functions global entry point, and into the count
276 //    register.
277 // 3) Transferring control to the target function through an indirect branch.
278 class PPC64PltCallStub final : public Thunk {
279 public:
280   PPC64PltCallStub(Symbol &dest) : Thunk(dest, 0) {}
281   uint32_t size() override { return 20; }
282   void writeTo(uint8_t *buf) override;
283   void addSymbols(ThunkSection &isec) override;
284   bool isCompatibleWith(const InputSection &isec,
285                         const Relocation &rel) const override;
286 };
287 
288 // PPC64 R2 Save Stub
289 // When the caller requires a valid R2 TOC pointer but the callee does not
290 // require a TOC pointer and the callee cannot guarantee that it doesn't
291 // clobber R2 then we need to save R2. This stub:
292 // 1) Saves the TOC pointer to the stack.
293 // 2) Tail calls the callee.
294 class PPC64R2SaveStub final : public Thunk {
295 public:
296   PPC64R2SaveStub(Symbol &dest, int64_t addend) : Thunk(dest, addend) {
297     alignment = 16;
298   }
299 
300   // To prevent oscillations in layout when moving from short to long thunks
301   // we make sure that once a thunk has been set to long it cannot go back.
302   bool getMayUseShortThunk() {
303     if (!mayUseShortThunk)
304       return false;
305     if (!isInt<26>(computeOffset())) {
306       mayUseShortThunk = false;
307       return false;
308     }
309     return true;
310   }
311   uint32_t size() override { return getMayUseShortThunk() ? 8 : 32; }
312   void writeTo(uint8_t *buf) override;
313   void addSymbols(ThunkSection &isec) override;
314   bool isCompatibleWith(const InputSection &isec,
315                         const Relocation &rel) const override;
316 
317 private:
318   // Transitioning from long to short can create layout oscillations in
319   // certain corner cases which would prevent the layout from converging.
320   // This is similar to the handling for ARMThunk.
321   bool mayUseShortThunk = true;
322   int64_t computeOffset() const {
323     return destination.getVA() - (getThunkTargetSym()->getVA() + 4);
324   }
325 };
326 
327 // PPC64 R12 Setup Stub
328 // When a caller that does not maintain a toc-pointer performs a local call to
329 // a callee which requires a toc-pointer then we need this stub to place the
330 // callee's global entry point into r12 without a save of R2.
331 class PPC64R12SetupStub final : public Thunk {
332 public:
333   PPC64R12SetupStub(Symbol &dest) : Thunk(dest, 0) { alignment = 16; }
334   uint32_t size() override { return 32; }
335   void writeTo(uint8_t *buf) override;
336   void addSymbols(ThunkSection &isec) override;
337   bool isCompatibleWith(const InputSection &isec,
338                         const Relocation &rel) const override;
339 };
340 
341 // PPC64 PC-relative PLT Stub
342 // When a caller that does not maintain a toc-pointer performs an extern call
343 // then this stub is needed for:
344 // 1) Loading the target functions address from the procedure linkage table into
345 //    r12 for use by the target functions global entry point, and into the count
346 //    register with pc-relative instructions.
347 // 2) Transferring control to the target function through an indirect branch.
348 class PPC64PCRelPLTStub final : public Thunk {
349 public:
350   PPC64PCRelPLTStub(Symbol &dest) : Thunk(dest, 0) { alignment = 16; }
351   uint32_t size() override { return 32; }
352   void writeTo(uint8_t *buf) override;
353   void addSymbols(ThunkSection &isec) override;
354   bool isCompatibleWith(const InputSection &isec,
355                         const Relocation &rel) const override;
356 };
357 
358 // A bl instruction uses a signed 24 bit offset, with an implicit 4 byte
359 // alignment. This gives a possible 26 bits of 'reach'. If the call offset is
360 // larger than that we need to emit a long-branch thunk. The target address
361 // of the callee is stored in a table to be accessed TOC-relative. Since the
362 // call must be local (a non-local call will have a PltCallStub instead) the
363 // table stores the address of the callee's local entry point. For
364 // position-independent code a corresponding relative dynamic relocation is
365 // used.
366 class PPC64LongBranchThunk : public Thunk {
367 public:
368   uint32_t size() override { return 32; }
369   void writeTo(uint8_t *buf) override;
370   void addSymbols(ThunkSection &isec) override;
371   bool isCompatibleWith(const InputSection &isec,
372                         const Relocation &rel) const override;
373 
374 protected:
375   PPC64LongBranchThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
376 };
377 
378 class PPC64PILongBranchThunk final : public PPC64LongBranchThunk {
379 public:
380   PPC64PILongBranchThunk(Symbol &dest, int64_t addend)
381       : PPC64LongBranchThunk(dest, addend) {
382     assert(!dest.isPreemptible);
383     if (Optional<uint32_t> index =
384             in.ppc64LongBranchTarget->addEntry(&dest, addend)) {
385       mainPart->relaDyn->addRelativeReloc(
386           target->relativeRel, *in.ppc64LongBranchTarget, *index * UINT64_C(8),
387           dest, addend + getPPC64GlobalEntryToLocalEntryOffset(dest.stOther),
388           target->symbolicRel, R_ABS);
389     }
390   }
391 };
392 
393 class PPC64PDLongBranchThunk final : public PPC64LongBranchThunk {
394 public:
395   PPC64PDLongBranchThunk(Symbol &dest, int64_t addend)
396       : PPC64LongBranchThunk(dest, addend) {
397     in.ppc64LongBranchTarget->addEntry(&dest, addend);
398   }
399 };
400 
401 } // end anonymous namespace
402 
403 Defined *Thunk::addSymbol(StringRef name, uint8_t type, uint64_t value,
404                           InputSectionBase &section) {
405   Defined *d = addSyntheticLocal(name, type, value, /*size=*/0, section);
406   syms.push_back(d);
407   return d;
408 }
409 
410 void Thunk::setOffset(uint64_t newOffset) {
411   for (Defined *d : syms)
412     d->value = d->value - offset + newOffset;
413   offset = newOffset;
414 }
415 
416 // AArch64 long range Thunks
417 
418 static uint64_t getAArch64ThunkDestVA(const Symbol &s, int64_t a) {
419   uint64_t v = s.isInPlt() ? s.getPltVA() : s.getVA(a);
420   return v;
421 }
422 
423 void AArch64ABSLongThunk::writeTo(uint8_t *buf) {
424   const uint8_t data[] = {
425     0x50, 0x00, 0x00, 0x58, //     ldr x16, L0
426     0x00, 0x02, 0x1f, 0xd6, //     br  x16
427     0x00, 0x00, 0x00, 0x00, // L0: .xword S
428     0x00, 0x00, 0x00, 0x00,
429   };
430   uint64_t s = getAArch64ThunkDestVA(destination, addend);
431   memcpy(buf, data, sizeof(data));
432   target->relocateNoSym(buf + 8, R_AARCH64_ABS64, s);
433 }
434 
435 void AArch64ABSLongThunk::addSymbols(ThunkSection &isec) {
436   addSymbol(saver().save("__AArch64AbsLongThunk_" + destination.getName()),
437             STT_FUNC, 0, isec);
438   addSymbol("$x", STT_NOTYPE, 0, isec);
439   addSymbol("$d", STT_NOTYPE, 8, isec);
440 }
441 
442 // This Thunk has a maximum range of 4Gb, this is sufficient for all programs
443 // using the small code model, including pc-relative ones. At time of writing
444 // clang and gcc do not support the large code model for position independent
445 // code so it is safe to use this for position independent thunks without
446 // worrying about the destination being more than 4Gb away.
447 void AArch64ADRPThunk::writeTo(uint8_t *buf) {
448   const uint8_t data[] = {
449       0x10, 0x00, 0x00, 0x90, // adrp x16, Dest R_AARCH64_ADR_PREL_PG_HI21(Dest)
450       0x10, 0x02, 0x00, 0x91, // add  x16, x16, R_AARCH64_ADD_ABS_LO12_NC(Dest)
451       0x00, 0x02, 0x1f, 0xd6, // br   x16
452   };
453   uint64_t s = getAArch64ThunkDestVA(destination, addend);
454   uint64_t p = getThunkTargetSym()->getVA();
455   memcpy(buf, data, sizeof(data));
456   target->relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21,
457                         getAArch64Page(s) - getAArch64Page(p));
458   target->relocateNoSym(buf + 4, R_AARCH64_ADD_ABS_LO12_NC, s);
459 }
460 
461 void AArch64ADRPThunk::addSymbols(ThunkSection &isec) {
462   addSymbol(saver().save("__AArch64ADRPThunk_" + destination.getName()),
463             STT_FUNC, 0, isec);
464   addSymbol("$x", STT_NOTYPE, 0, isec);
465 }
466 
467 // ARM Target Thunks
468 static uint64_t getARMThunkDestVA(const Symbol &s) {
469   uint64_t v = s.isInPlt() ? s.getPltVA() : s.getVA();
470   return SignExtend64<32>(v);
471 }
472 
473 // This function returns true if the target is not Thumb and is within 2^26, and
474 // it has not previously returned false (see comment for mayUseShortThunk).
475 bool ARMThunk::getMayUseShortThunk() {
476   if (!mayUseShortThunk)
477     return false;
478   uint64_t s = getARMThunkDestVA(destination);
479   if (s & 1) {
480     mayUseShortThunk = false;
481     return false;
482   }
483   uint64_t p = getThunkTargetSym()->getVA();
484   int64_t offset = s - p - 8;
485   mayUseShortThunk = llvm::isInt<26>(offset);
486   return mayUseShortThunk;
487 }
488 
489 void ARMThunk::writeTo(uint8_t *buf) {
490   if (!getMayUseShortThunk()) {
491     writeLong(buf);
492     return;
493   }
494 
495   uint64_t s = getARMThunkDestVA(destination);
496   uint64_t p = getThunkTargetSym()->getVA();
497   int64_t offset = s - p - 8;
498   const uint8_t data[] = {
499     0x00, 0x00, 0x00, 0xea, // b S
500   };
501   memcpy(buf, data, sizeof(data));
502   target->relocateNoSym(buf, R_ARM_JUMP24, offset);
503 }
504 
505 bool ARMThunk::isCompatibleWith(const InputSection &isec,
506                                 const Relocation &rel) const {
507   // Thumb branch relocations can't use BLX
508   return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;
509 }
510 
511 // This function returns true if the target is Thumb and is within 2^25, and
512 // it has not previously returned false (see comment for mayUseShortThunk).
513 bool ThumbThunk::getMayUseShortThunk() {
514   if (!mayUseShortThunk)
515     return false;
516   uint64_t s = getARMThunkDestVA(destination);
517   if ((s & 1) == 0) {
518     mayUseShortThunk = false;
519     return false;
520   }
521   uint64_t p = getThunkTargetSym()->getVA() & ~1;
522   int64_t offset = s - p - 4;
523   mayUseShortThunk = llvm::isInt<25>(offset);
524   return mayUseShortThunk;
525 }
526 
527 void ThumbThunk::writeTo(uint8_t *buf) {
528   if (!getMayUseShortThunk()) {
529     writeLong(buf);
530     return;
531   }
532 
533   uint64_t s = getARMThunkDestVA(destination);
534   uint64_t p = getThunkTargetSym()->getVA();
535   int64_t offset = s - p - 4;
536   const uint8_t data[] = {
537       0x00, 0xf0, 0x00, 0xb0, // b.w S
538   };
539   memcpy(buf, data, sizeof(data));
540   target->relocateNoSym(buf, R_ARM_THM_JUMP24, offset);
541 }
542 
543 bool ThumbThunk::isCompatibleWith(const InputSection &isec,
544                                   const Relocation &rel) const {
545   // ARM branch relocations can't use BLX
546   return rel.type != R_ARM_JUMP24 && rel.type != R_ARM_PC24 && rel.type != R_ARM_PLT32;
547 }
548 
549 void ARMV7ABSLongThunk::writeLong(uint8_t *buf) {
550   const uint8_t data[] = {
551       0x00, 0xc0, 0x00, 0xe3, // movw         ip,:lower16:S
552       0x00, 0xc0, 0x40, 0xe3, // movt         ip,:upper16:S
553       0x1c, 0xff, 0x2f, 0xe1, // bx   ip
554   };
555   uint64_t s = getARMThunkDestVA(destination);
556   memcpy(buf, data, sizeof(data));
557   target->relocateNoSym(buf, R_ARM_MOVW_ABS_NC, s);
558   target->relocateNoSym(buf + 4, R_ARM_MOVT_ABS, s);
559 }
560 
561 void ARMV7ABSLongThunk::addSymbols(ThunkSection &isec) {
562   addSymbol(saver().save("__ARMv7ABSLongThunk_" + destination.getName()),
563             STT_FUNC, 0, isec);
564   addSymbol("$a", STT_NOTYPE, 0, isec);
565 }
566 
567 void ThumbV7ABSLongThunk::writeLong(uint8_t *buf) {
568   const uint8_t data[] = {
569       0x40, 0xf2, 0x00, 0x0c, // movw         ip, :lower16:S
570       0xc0, 0xf2, 0x00, 0x0c, // movt         ip, :upper16:S
571       0x60, 0x47,             // bx   ip
572   };
573   uint64_t s = getARMThunkDestVA(destination);
574   memcpy(buf, data, sizeof(data));
575   target->relocateNoSym(buf, R_ARM_THM_MOVW_ABS_NC, s);
576   target->relocateNoSym(buf + 4, R_ARM_THM_MOVT_ABS, s);
577 }
578 
579 void ThumbV7ABSLongThunk::addSymbols(ThunkSection &isec) {
580   addSymbol(saver().save("__Thumbv7ABSLongThunk_" + destination.getName()),
581             STT_FUNC, 1, isec);
582   addSymbol("$t", STT_NOTYPE, 0, isec);
583 }
584 
585 void ARMV7PILongThunk::writeLong(uint8_t *buf) {
586   const uint8_t data[] = {
587       0xf0, 0xcf, 0x0f, 0xe3, // P:  movw ip,:lower16:S - (P + (L1-P) + 8)
588       0x00, 0xc0, 0x40, 0xe3, //     movt ip,:upper16:S - (P + (L1-P) + 8)
589       0x0f, 0xc0, 0x8c, 0xe0, // L1: add  ip, ip, pc
590       0x1c, 0xff, 0x2f, 0xe1, //     bx   ip
591   };
592   uint64_t s = getARMThunkDestVA(destination);
593   uint64_t p = getThunkTargetSym()->getVA();
594   int64_t offset = s - p - 16;
595   memcpy(buf, data, sizeof(data));
596   target->relocateNoSym(buf, R_ARM_MOVW_PREL_NC, offset);
597   target->relocateNoSym(buf + 4, R_ARM_MOVT_PREL, offset);
598 }
599 
600 void ARMV7PILongThunk::addSymbols(ThunkSection &isec) {
601   addSymbol(saver().save("__ARMV7PILongThunk_" + destination.getName()),
602             STT_FUNC, 0, isec);
603   addSymbol("$a", STT_NOTYPE, 0, isec);
604 }
605 
606 void ThumbV7PILongThunk::writeLong(uint8_t *buf) {
607   const uint8_t data[] = {
608       0x4f, 0xf6, 0xf4, 0x7c, // P:  movw ip,:lower16:S - (P + (L1-P) + 4)
609       0xc0, 0xf2, 0x00, 0x0c, //     movt ip,:upper16:S - (P + (L1-P) + 4)
610       0xfc, 0x44,             // L1: add  ip, pc
611       0x60, 0x47,             //     bx   ip
612   };
613   uint64_t s = getARMThunkDestVA(destination);
614   uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
615   int64_t offset = s - p - 12;
616   memcpy(buf, data, sizeof(data));
617   target->relocateNoSym(buf, R_ARM_THM_MOVW_PREL_NC, offset);
618   target->relocateNoSym(buf + 4, R_ARM_THM_MOVT_PREL, offset);
619 }
620 
621 void ThumbV7PILongThunk::addSymbols(ThunkSection &isec) {
622   addSymbol(saver().save("__ThumbV7PILongThunk_" + destination.getName()),
623             STT_FUNC, 1, isec);
624   addSymbol("$t", STT_NOTYPE, 0, isec);
625 }
626 
627 void ARMV5ABSLongThunk::writeLong(uint8_t *buf) {
628   const uint8_t data[] = {
629       0x04, 0xf0, 0x1f, 0xe5, //     ldr pc, [pc,#-4] ; L1
630       0x00, 0x00, 0x00, 0x00, // L1: .word S
631   };
632   memcpy(buf, data, sizeof(data));
633   target->relocateNoSym(buf + 4, R_ARM_ABS32, getARMThunkDestVA(destination));
634 }
635 
636 void ARMV5ABSLongThunk::addSymbols(ThunkSection &isec) {
637   addSymbol(saver().save("__ARMv5ABSLongThunk_" + destination.getName()),
638             STT_FUNC, 0, isec);
639   addSymbol("$a", STT_NOTYPE, 0, isec);
640   addSymbol("$d", STT_NOTYPE, 4, isec);
641 }
642 
643 bool ARMV5ABSLongThunk::isCompatibleWith(const InputSection &isec,
644                                          const Relocation &rel) const {
645   // Thumb branch relocations can't use BLX
646   return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;
647 }
648 
649 void ARMV5PILongThunk::writeLong(uint8_t *buf) {
650   const uint8_t data[] = {
651       0x04, 0xc0, 0x9f, 0xe5, // P:  ldr ip, [pc,#4] ; L2
652       0x0c, 0xc0, 0x8f, 0xe0, // L1: add ip, pc, ip
653       0x1c, 0xff, 0x2f, 0xe1, //     bx ip
654       0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 8)
655   };
656   uint64_t s = getARMThunkDestVA(destination);
657   uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
658   memcpy(buf, data, sizeof(data));
659   target->relocateNoSym(buf + 12, R_ARM_REL32, s - p - 12);
660 }
661 
662 void ARMV5PILongThunk::addSymbols(ThunkSection &isec) {
663   addSymbol(saver().save("__ARMV5PILongThunk_" + destination.getName()),
664             STT_FUNC, 0, isec);
665   addSymbol("$a", STT_NOTYPE, 0, isec);
666   addSymbol("$d", STT_NOTYPE, 12, isec);
667 }
668 
669 bool ARMV5PILongThunk::isCompatibleWith(const InputSection &isec,
670                                         const Relocation &rel) const {
671   // Thumb branch relocations can't use BLX
672   return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;
673 }
674 
675 void ThumbV6MABSLongThunk::writeLong(uint8_t *buf) {
676   // Most Thumb instructions cannot access the high registers r8 - r15. As the
677   // only register we can corrupt is r12 we must instead spill a low register
678   // to the stack to use as a scratch register. We push r1 even though we
679   // don't need to get some space to use for the return address.
680   const uint8_t data[] = {
681       0x03, 0xb4,            // push {r0, r1} ; Obtain scratch registers
682       0x01, 0x48,            // ldr r0, [pc, #4] ; L1
683       0x01, 0x90,            // str r0, [sp, #4] ; SP + 4 = S
684       0x01, 0xbd,            // pop {r0, pc} ; restore r0 and branch to dest
685       0x00, 0x00, 0x00, 0x00 // L1: .word S
686   };
687   uint64_t s = getARMThunkDestVA(destination);
688   memcpy(buf, data, sizeof(data));
689   target->relocateNoSym(buf + 8, R_ARM_ABS32, s);
690 }
691 
692 void ThumbV6MABSLongThunk::addSymbols(ThunkSection &isec) {
693   addSymbol(saver().save("__Thumbv6MABSLongThunk_" + destination.getName()),
694             STT_FUNC, 1, isec);
695   addSymbol("$t", STT_NOTYPE, 0, isec);
696   addSymbol("$d", STT_NOTYPE, 8, isec);
697 }
698 
699 void ThumbV6MPILongThunk::writeLong(uint8_t *buf) {
700   // Most Thumb instructions cannot access the high registers r8 - r15. As the
701   // only register we can corrupt is ip (r12) we must instead spill a low
702   // register to the stack to use as a scratch register.
703   const uint8_t data[] = {
704       0x01, 0xb4,             // P:  push {r0}        ; Obtain scratch register
705       0x02, 0x48,             //     ldr r0, [pc, #8] ; L2
706       0x84, 0x46,             //     mov ip, r0       ; high to low register
707       0x01, 0xbc,             //     pop {r0}         ; restore scratch register
708       0xe7, 0x44,             // L1: add pc, ip       ; transfer control
709       0xc0, 0x46,             //     nop              ; pad to 4-byte boundary
710       0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 4)
711   };
712   uint64_t s = getARMThunkDestVA(destination);
713   uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
714   memcpy(buf, data, sizeof(data));
715   target->relocateNoSym(buf + 12, R_ARM_REL32, s - p - 12);
716 }
717 
718 void ThumbV6MPILongThunk::addSymbols(ThunkSection &isec) {
719   addSymbol(saver().save("__Thumbv6MPILongThunk_" + destination.getName()),
720             STT_FUNC, 1, isec);
721   addSymbol("$t", STT_NOTYPE, 0, isec);
722   addSymbol("$d", STT_NOTYPE, 12, isec);
723 }
724 
725 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
726 void MipsThunk::writeTo(uint8_t *buf) {
727   uint64_t s = destination.getVA();
728   write32(buf, 0x3c190000); // lui   $25, %hi(func)
729   write32(buf + 4, 0x08000000 | (s >> 2)); // j     func
730   write32(buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
731   write32(buf + 12, 0x00000000); // nop
732   target->relocateNoSym(buf, R_MIPS_HI16, s);
733   target->relocateNoSym(buf + 8, R_MIPS_LO16, s);
734 }
735 
736 void MipsThunk::addSymbols(ThunkSection &isec) {
737   addSymbol(saver().save("__LA25Thunk_" + destination.getName()), STT_FUNC, 0,
738             isec);
739 }
740 
741 InputSection *MipsThunk::getTargetInputSection() const {
742   auto &dr = cast<Defined>(destination);
743   return dyn_cast<InputSection>(dr.section);
744 }
745 
746 // Write microMIPS R2-R5 LA25 thunk code
747 // to call PIC function from the non-PIC one.
748 void MicroMipsThunk::writeTo(uint8_t *buf) {
749   uint64_t s = destination.getVA();
750   write16(buf, 0x41b9);       // lui   $25, %hi(func)
751   write16(buf + 4, 0xd400);   // j     func
752   write16(buf + 8, 0x3339);   // addiu $25, $25, %lo(func)
753   write16(buf + 12, 0x0c00);  // nop
754   target->relocateNoSym(buf, R_MICROMIPS_HI16, s);
755   target->relocateNoSym(buf + 4, R_MICROMIPS_26_S1, s);
756   target->relocateNoSym(buf + 8, R_MICROMIPS_LO16, s);
757 }
758 
759 void MicroMipsThunk::addSymbols(ThunkSection &isec) {
760   Defined *d =
761       addSymbol(saver().save("__microLA25Thunk_" + destination.getName()),
762                 STT_FUNC, 0, isec);
763   d->stOther |= STO_MIPS_MICROMIPS;
764 }
765 
766 InputSection *MicroMipsThunk::getTargetInputSection() const {
767   auto &dr = cast<Defined>(destination);
768   return dyn_cast<InputSection>(dr.section);
769 }
770 
771 // Write microMIPS R6 LA25 thunk code
772 // to call PIC function from the non-PIC one.
773 void MicroMipsR6Thunk::writeTo(uint8_t *buf) {
774   uint64_t s = destination.getVA();
775   uint64_t p = getThunkTargetSym()->getVA();
776   write16(buf, 0x1320);       // lui   $25, %hi(func)
777   write16(buf + 4, 0x3339);   // addiu $25, $25, %lo(func)
778   write16(buf + 8, 0x9400);   // bc    func
779   target->relocateNoSym(buf, R_MICROMIPS_HI16, s);
780   target->relocateNoSym(buf + 4, R_MICROMIPS_LO16, s);
781   target->relocateNoSym(buf + 8, R_MICROMIPS_PC26_S1, s - p - 12);
782 }
783 
784 void MicroMipsR6Thunk::addSymbols(ThunkSection &isec) {
785   Defined *d =
786       addSymbol(saver().save("__microLA25Thunk_" + destination.getName()),
787                 STT_FUNC, 0, isec);
788   d->stOther |= STO_MIPS_MICROMIPS;
789 }
790 
791 InputSection *MicroMipsR6Thunk::getTargetInputSection() const {
792   auto &dr = cast<Defined>(destination);
793   return dyn_cast<InputSection>(dr.section);
794 }
795 
796 void elf::writePPC32PltCallStub(uint8_t *buf, uint64_t gotPltVA,
797                                 const InputFile *file, int64_t addend) {
798   if (!config->isPic) {
799     write32(buf + 0, 0x3d600000 | (gotPltVA + 0x8000) >> 16); // lis r11,ha
800     write32(buf + 4, 0x816b0000 | (uint16_t)gotPltVA);        // lwz r11,l(r11)
801     write32(buf + 8, 0x7d6903a6);                             // mtctr r11
802     write32(buf + 12, 0x4e800420);                            // bctr
803     return;
804   }
805   uint32_t offset;
806   if (addend >= 0x8000) {
807     // The stub loads an address relative to r30 (.got2+Addend). Addend is
808     // almost always 0x8000. The address of .got2 is different in another object
809     // file, so a stub cannot be shared.
810     offset = gotPltVA -
811              (in.ppc32Got2->getParent()->getVA() +
812               (file->ppc32Got2 ? file->ppc32Got2->outSecOff : 0) + addend);
813   } else {
814     // The stub loads an address relative to _GLOBAL_OFFSET_TABLE_ (which is
815     // currently the address of .got).
816     offset = gotPltVA - in.got->getVA();
817   }
818   uint16_t ha = (offset + 0x8000) >> 16, l = (uint16_t)offset;
819   if (ha == 0) {
820     write32(buf + 0, 0x817e0000 | l); // lwz r11,l(r30)
821     write32(buf + 4, 0x7d6903a6);     // mtctr r11
822     write32(buf + 8, 0x4e800420);     // bctr
823     write32(buf + 12, 0x60000000);    // nop
824   } else {
825     write32(buf + 0, 0x3d7e0000 | ha); // addis r11,r30,ha
826     write32(buf + 4, 0x816b0000 | l);  // lwz r11,l(r11)
827     write32(buf + 8, 0x7d6903a6);      // mtctr r11
828     write32(buf + 12, 0x4e800420);     // bctr
829   }
830 }
831 
832 void PPC32PltCallStub::writeTo(uint8_t *buf) {
833   writePPC32PltCallStub(buf, destination.getGotPltVA(), file, addend);
834 }
835 
836 void PPC32PltCallStub::addSymbols(ThunkSection &isec) {
837   std::string buf;
838   raw_string_ostream os(buf);
839   os << format_hex_no_prefix(addend, 8);
840   if (!config->isPic)
841     os << ".plt_call32.";
842   else if (addend >= 0x8000)
843     os << ".got2.plt_pic32.";
844   else
845     os << ".plt_pic32.";
846   os << destination.getName();
847   addSymbol(saver().save(os.str()), STT_FUNC, 0, isec);
848 }
849 
850 bool PPC32PltCallStub::isCompatibleWith(const InputSection &isec,
851                                         const Relocation &rel) const {
852   return !config->isPic || (isec.file == file && rel.addend == addend);
853 }
854 
855 void PPC32LongThunk::addSymbols(ThunkSection &isec) {
856   addSymbol(saver().save("__LongThunk_" + destination.getName()), STT_FUNC, 0,
857             isec);
858 }
859 
860 void PPC32LongThunk::writeTo(uint8_t *buf) {
861   auto ha = [](uint32_t v) -> uint16_t { return (v + 0x8000) >> 16; };
862   auto lo = [](uint32_t v) -> uint16_t { return v; };
863   uint32_t d = destination.getVA(addend);
864   if (config->isPic) {
865     uint32_t off = d - (getThunkTargetSym()->getVA() + 8);
866     write32(buf + 0, 0x7c0802a6);            // mflr r12,0
867     write32(buf + 4, 0x429f0005);            // bcl r20,r31,.+4
868     write32(buf + 8, 0x7d8802a6);            // mtctr r12
869     write32(buf + 12, 0x3d8c0000 | ha(off)); // addis r12,r12,off@ha
870     write32(buf + 16, 0x398c0000 | lo(off)); // addi r12,r12,off@l
871     write32(buf + 20, 0x7c0803a6);           // mtlr r0
872     buf += 24;
873   } else {
874     write32(buf + 0, 0x3d800000 | ha(d));    // lis r12,d@ha
875     write32(buf + 4, 0x398c0000 | lo(d));    // addi r12,r12,d@l
876     buf += 8;
877   }
878   write32(buf + 0, 0x7d8903a6);              // mtctr r12
879   write32(buf + 4, 0x4e800420);              // bctr
880 }
881 
882 void elf::writePPC64LoadAndBranch(uint8_t *buf, int64_t offset) {
883   uint16_t offHa = (offset + 0x8000) >> 16;
884   uint16_t offLo = offset & 0xffff;
885 
886   write32(buf + 0, 0x3d820000 | offHa); // addis r12, r2, OffHa
887   write32(buf + 4, 0xe98c0000 | offLo); // ld    r12, OffLo(r12)
888   write32(buf + 8, 0x7d8903a6);         // mtctr r12
889   write32(buf + 12, 0x4e800420);        // bctr
890 }
891 
892 void PPC64PltCallStub::writeTo(uint8_t *buf) {
893   int64_t offset = destination.getGotPltVA() - getPPC64TocBase();
894   // Save the TOC pointer to the save-slot reserved in the call frame.
895   write32(buf + 0, 0xf8410018); // std     r2,24(r1)
896   writePPC64LoadAndBranch(buf + 4, offset);
897 }
898 
899 void PPC64PltCallStub::addSymbols(ThunkSection &isec) {
900   Defined *s = addSymbol(saver().save("__plt_" + destination.getName()),
901                          STT_FUNC, 0, isec);
902   s->needsTocRestore = true;
903   s->file = destination.file;
904 }
905 
906 bool PPC64PltCallStub::isCompatibleWith(const InputSection &isec,
907                                         const Relocation &rel) const {
908   return rel.type == R_PPC64_REL24 || rel.type == R_PPC64_REL14;
909 }
910 
911 void PPC64R2SaveStub::writeTo(uint8_t *buf) {
912   const int64_t offset = computeOffset();
913   write32(buf + 0, 0xf8410018); // std  r2,24(r1)
914   // The branch offset needs to fit in 26 bits.
915   if (getMayUseShortThunk()) {
916     write32(buf + 4, 0x48000000 | (offset & 0x03fffffc)); // b    <offset>
917   } else if (isInt<34>(offset)) {
918     int nextInstOffset;
919     if (!config->power10Stubs) {
920       uint64_t tocOffset = destination.getVA() - getPPC64TocBase();
921       if (tocOffset >> 16 > 0) {
922         const uint64_t addi = ADDI_R12_TO_R12_NO_DISP | (tocOffset & 0xffff);
923         const uint64_t addis = ADDIS_R12_TO_R2_NO_DISP | ((tocOffset >> 16) & 0xffff);
924         write32(buf + 4, addis); // addis r12, r2 , top of offset
925         write32(buf + 8, addi);  // addi  r12, r12, bottom of offset
926         nextInstOffset = 12;
927       } else {
928         const uint64_t addi = ADDI_R12_TO_R2_NO_DISP | (tocOffset & 0xffff);
929         write32(buf + 4, addi); // addi r12, r2, offset
930         nextInstOffset = 8;
931       }
932     } else {
933       const uint64_t paddi = PADDI_R12_NO_DISP |
934                              (((offset >> 16) & 0x3ffff) << 32) |
935                              (offset & 0xffff);
936       writePrefixedInstruction(buf + 4, paddi); // paddi r12, 0, func@pcrel, 1
937       nextInstOffset = 12;
938     }
939     write32(buf + nextInstOffset, MTCTR_R12); // mtctr r12
940     write32(buf + nextInstOffset + 4, BCTR);  // bctr
941   } else {
942     in.ppc64LongBranchTarget->addEntry(&destination, addend);
943     const int64_t offsetFromTOC =
944         in.ppc64LongBranchTarget->getEntryVA(&destination, addend) -
945         getPPC64TocBase();
946     writePPC64LoadAndBranch(buf + 4, offsetFromTOC);
947   }
948 }
949 
950 void PPC64R2SaveStub::addSymbols(ThunkSection &isec) {
951   Defined *s = addSymbol(saver().save("__toc_save_" + destination.getName()),
952                          STT_FUNC, 0, isec);
953   s->needsTocRestore = true;
954 }
955 
956 bool PPC64R2SaveStub::isCompatibleWith(const InputSection &isec,
957                                        const Relocation &rel) const {
958   return rel.type == R_PPC64_REL24 || rel.type == R_PPC64_REL14;
959 }
960 
961 void PPC64R12SetupStub::writeTo(uint8_t *buf) {
962   int64_t offset = destination.getVA() - getThunkTargetSym()->getVA();
963   if (!isInt<34>(offset))
964     reportRangeError(buf, offset, 34, destination, "R12 setup stub offset");
965 
966   int nextInstOffset;
967   if (!config->power10Stubs) {
968     uint32_t off = destination.getVA(addend) - getThunkTargetSym()->getVA() - 8;
969     write32(buf + 0, 0x7c0802a6);                      // mflr r12
970     write32(buf + 4, 0x429f0005);                      // bcl 20,31,.+4
971     write32(buf + 8, 0x7d6802a6);                      // mflr r11
972     write32(buf + 12, 0x7d8803a6);                     // mtlr r12
973     write32(buf + 16, 0x3d8b0000 | computeHiBits(off));// addis r12,r11,off@ha
974     write32(buf + 20, 0x398c0000 | (off & 0xffff));    // addi r12,r12,off@l
975     nextInstOffset = 24;
976   } else {
977     uint64_t paddi = PADDI_R12_NO_DISP | (((offset >> 16) & 0x3ffff) << 32) |
978                      (offset & 0xffff);
979     writePrefixedInstruction(buf + 0, paddi); // paddi r12, 0, func@pcrel, 1
980     nextInstOffset = 8;
981   }
982   write32(buf + nextInstOffset, MTCTR_R12); // mtctr r12
983   write32(buf + nextInstOffset + 4, BCTR);  // bctr
984 }
985 
986 void PPC64R12SetupStub::addSymbols(ThunkSection &isec) {
987   addSymbol(saver().save("__gep_setup_" + destination.getName()), STT_FUNC, 0,
988             isec);
989 }
990 
991 bool PPC64R12SetupStub::isCompatibleWith(const InputSection &isec,
992                                          const Relocation &rel) const {
993   return rel.type == R_PPC64_REL24_NOTOC;
994 }
995 
996 void PPC64PCRelPLTStub::writeTo(uint8_t *buf) {
997   int nextInstOffset = 0;
998   int64_t offset = destination.getGotPltVA() - getThunkTargetSym()->getVA();
999 
1000   if (config->power10Stubs) {
1001     if (!isInt<34>(offset))
1002       reportRangeError(buf, offset, 34, destination,
1003                        "PC-relative PLT stub offset");
1004     const uint64_t pld = PLD_R12_NO_DISP | (((offset >> 16) & 0x3ffff) << 32) |
1005                    (offset & 0xffff);
1006     writePrefixedInstruction(buf + 0, pld); // pld r12, func@plt@pcrel
1007     nextInstOffset = 8;
1008   } else {
1009     uint32_t off = destination.getVA(addend) - getThunkTargetSym()->getVA() - 8;
1010     write32(buf + 0, 0x7c0802a6);            // mflr r12
1011     write32(buf + 4, 0x429f0005);            // bcl 20,31,.+4
1012     write32(buf + 8, 0x7d6802a6);            // mflr r11
1013     write32(buf + 12, 0x7d8803a6);           // mtlr r12
1014     write32(buf + 16, 0x3d8b0000 | computeHiBits(off)); // addis r12,r11,off@ha
1015     write32(buf + 20, 0x398c0000 | (off & 0xffff)); // addi r12,r12,off@l
1016     nextInstOffset = 24;
1017   }
1018   write32(buf + nextInstOffset, MTCTR_R12); // mtctr r12
1019   write32(buf + nextInstOffset + 4, BCTR);  // bctr
1020 }
1021 
1022 void PPC64PCRelPLTStub::addSymbols(ThunkSection &isec) {
1023   addSymbol(saver().save("__plt_pcrel_" + destination.getName()), STT_FUNC, 0,
1024             isec);
1025 }
1026 
1027 bool PPC64PCRelPLTStub::isCompatibleWith(const InputSection &isec,
1028                                          const Relocation &rel) const {
1029   return rel.type == R_PPC64_REL24_NOTOC;
1030 }
1031 
1032 void PPC64LongBranchThunk::writeTo(uint8_t *buf) {
1033   int64_t offset = in.ppc64LongBranchTarget->getEntryVA(&destination, addend) -
1034                    getPPC64TocBase();
1035   writePPC64LoadAndBranch(buf, offset);
1036 }
1037 
1038 void PPC64LongBranchThunk::addSymbols(ThunkSection &isec) {
1039   addSymbol(saver().save("__long_branch_" + destination.getName()), STT_FUNC, 0,
1040             isec);
1041 }
1042 
1043 bool PPC64LongBranchThunk::isCompatibleWith(const InputSection &isec,
1044                                             const Relocation &rel) const {
1045   return rel.type == R_PPC64_REL24 || rel.type == R_PPC64_REL14;
1046 }
1047 
1048 Thunk::Thunk(Symbol &d, int64_t a) : destination(d), addend(a), offset(0) {}
1049 
1050 Thunk::~Thunk() = default;
1051 
1052 static Thunk *addThunkAArch64(RelType type, Symbol &s, int64_t a) {
1053   if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 &&
1054       type != R_AARCH64_PLT32)
1055     fatal("unrecognized relocation type");
1056   if (config->picThunk)
1057     return make<AArch64ADRPThunk>(s, a);
1058   return make<AArch64ABSLongThunk>(s, a);
1059 }
1060 
1061 // Creates a thunk for Thumb-ARM interworking.
1062 // Arm Architectures v5 and v6 do not support Thumb2 technology. This means
1063 // - MOVT and MOVW instructions cannot be used
1064 // - Only Thumb relocation that can generate a Thunk is a BL, this can always
1065 //   be transformed into a BLX
1066 static Thunk *addThunkPreArmv7(RelType reloc, Symbol &s, int64_t a) {
1067   switch (reloc) {
1068   case R_ARM_PC24:
1069   case R_ARM_PLT32:
1070   case R_ARM_JUMP24:
1071   case R_ARM_CALL:
1072   case R_ARM_THM_CALL:
1073     if (config->picThunk)
1074       return make<ARMV5PILongThunk>(s, a);
1075     return make<ARMV5ABSLongThunk>(s, a);
1076   }
1077   fatal("relocation " + toString(reloc) + " to " + toString(s) +
1078         " not supported for Armv5 or Armv6 targets");
1079 }
1080 
1081 // Create a thunk for Thumb long branch on V6-M.
1082 // Arm Architecture v6-M only supports Thumb instructions. This means
1083 // - MOVT and MOVW instructions cannot be used.
1084 // - Only a limited number of instructions can access registers r8 and above
1085 // - No interworking support is needed (all Thumb).
1086 static Thunk *addThunkV6M(RelType reloc, Symbol &s, int64_t a) {
1087   switch (reloc) {
1088   case R_ARM_THM_JUMP19:
1089   case R_ARM_THM_JUMP24:
1090   case R_ARM_THM_CALL:
1091     if (config->isPic)
1092       return make<ThumbV6MPILongThunk>(s, a);
1093     return make<ThumbV6MABSLongThunk>(s, a);
1094   }
1095   fatal("relocation " + toString(reloc) + " to " + toString(s) +
1096         " not supported for Armv6-M targets");
1097 }
1098 
1099 // Creates a thunk for Thumb-ARM interworking or branch range extension.
1100 static Thunk *addThunkArm(RelType reloc, Symbol &s, int64_t a) {
1101   // Decide which Thunk is needed based on:
1102   // Available instruction set
1103   // - An Arm Thunk can only be used if Arm state is available.
1104   // - A Thumb Thunk can only be used if Thumb state is available.
1105   // - Can only use a Thunk if it uses instructions that the Target supports.
1106   // Relocation is branch or branch and link
1107   // - Branch instructions cannot change state, can only select Thunk that
1108   //   starts in the same state as the caller.
1109   // - Branch and link relocations can change state, can select Thunks from
1110   //   either Arm or Thumb.
1111   // Position independent Thunks if we require position independent code.
1112 
1113   // Handle architectures that have restrictions on the instructions that they
1114   // can use in Thunks. The flags below are set by reading the BuildAttributes
1115   // of the input objects. InputFiles.cpp contains the mapping from ARM
1116   // architecture to flag.
1117   if (!config->armHasMovtMovw) {
1118     if (!config->armJ1J2BranchEncoding)
1119       return addThunkPreArmv7(reloc, s, a);
1120     return addThunkV6M(reloc, s, a);
1121   }
1122 
1123   switch (reloc) {
1124   case R_ARM_PC24:
1125   case R_ARM_PLT32:
1126   case R_ARM_JUMP24:
1127   case R_ARM_CALL:
1128     if (config->picThunk)
1129       return make<ARMV7PILongThunk>(s, a);
1130     return make<ARMV7ABSLongThunk>(s, a);
1131   case R_ARM_THM_JUMP19:
1132   case R_ARM_THM_JUMP24:
1133   case R_ARM_THM_CALL:
1134     if (config->picThunk)
1135       return make<ThumbV7PILongThunk>(s, a);
1136     return make<ThumbV7ABSLongThunk>(s, a);
1137   }
1138   fatal("unrecognized relocation type");
1139 }
1140 
1141 static Thunk *addThunkMips(RelType type, Symbol &s) {
1142   if ((s.stOther & STO_MIPS_MICROMIPS) && isMipsR6())
1143     return make<MicroMipsR6Thunk>(s);
1144   if (s.stOther & STO_MIPS_MICROMIPS)
1145     return make<MicroMipsThunk>(s);
1146   return make<MipsThunk>(s);
1147 }
1148 
1149 static Thunk *addThunkPPC32(const InputSection &isec, const Relocation &rel,
1150                             Symbol &s) {
1151   assert((rel.type == R_PPC_LOCAL24PC || rel.type == R_PPC_REL24 ||
1152           rel.type == R_PPC_PLTREL24) &&
1153          "unexpected relocation type for thunk");
1154   if (s.isInPlt())
1155     return make<PPC32PltCallStub>(isec, rel, s);
1156   return make<PPC32LongThunk>(s, rel.addend);
1157 }
1158 
1159 static Thunk *addThunkPPC64(RelType type, Symbol &s, int64_t a) {
1160   assert((type == R_PPC64_REL14 || type == R_PPC64_REL24 ||
1161           type == R_PPC64_REL24_NOTOC) &&
1162          "unexpected relocation type for thunk");
1163   if (s.isInPlt())
1164     return type == R_PPC64_REL24_NOTOC ? (Thunk *)make<PPC64PCRelPLTStub>(s)
1165                                        : (Thunk *)make<PPC64PltCallStub>(s);
1166 
1167   // This check looks at the st_other bits of the callee. If the value is 1
1168   // then the callee clobbers the TOC and we need an R2 save stub when RelType
1169   // is R_PPC64_REL14 or R_PPC64_REL24.
1170   if ((type == R_PPC64_REL14 || type == R_PPC64_REL24) && (s.stOther >> 5) == 1)
1171     return make<PPC64R2SaveStub>(s, a);
1172 
1173   if (type == R_PPC64_REL24_NOTOC)
1174     return make<PPC64R12SetupStub>(s);
1175 
1176   if (config->picThunk)
1177     return make<PPC64PILongBranchThunk>(s, a);
1178 
1179   return make<PPC64PDLongBranchThunk>(s, a);
1180 }
1181 
1182 Thunk *elf::addThunk(const InputSection &isec, Relocation &rel) {
1183   Symbol &s = *rel.sym;
1184   int64_t a = rel.addend;
1185 
1186   if (config->emachine == EM_AARCH64)
1187     return addThunkAArch64(rel.type, s, a);
1188 
1189   if (config->emachine == EM_ARM)
1190     return addThunkArm(rel.type, s, a);
1191 
1192   if (config->emachine == EM_MIPS)
1193     return addThunkMips(rel.type, s);
1194 
1195   if (config->emachine == EM_PPC)
1196     return addThunkPPC32(isec, rel, s);
1197 
1198   if (config->emachine == EM_PPC64)
1199     return addThunkPPC64(rel.type, s, a);
1200 
1201   llvm_unreachable("add Thunk only supported for ARM, Mips and PowerPC");
1202 }
1203