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