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