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