]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/Thunks.cpp
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / Thunks.cpp
1 //===- Thunks.cpp --------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===---------------------------------------------------------------------===//
9 //
10 // This file contains Thunk subclasses.
11 //
12 // A thunk is a small piece of code written after an input section
13 // which is used to jump between "incompatible" functions
14 // such as MIPS PIC and non-PIC or ARM non-Thumb and Thumb functions.
15 //
16 // If a jump target is too far and its address doesn't fit to a
17 // short jump instruction, we need to create a thunk too, but we
18 // haven't supported it yet.
19 //
20 // i386 and x86-64 don't need thunks.
21 //
22 //===---------------------------------------------------------------------===//
23
24 #include "Thunks.h"
25 #include "Config.h"
26 #include "InputSection.h"
27 #include "OutputSections.h"
28 #include "Symbols.h"
29 #include "SyntheticSections.h"
30 #include "Target.h"
31 #include "lld/Common/ErrorHandler.h"
32 #include "lld/Common/Memory.h"
33 #include "llvm/BinaryFormat/ELF.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/Endian.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/MathExtras.h"
38 #include <cstdint>
39 #include <cstring>
40
41 using namespace llvm;
42 using namespace llvm::object;
43 using namespace llvm::ELF;
44
45 namespace lld {
46 namespace elf {
47
48 namespace {
49
50 // AArch64 long range Thunks
51 class AArch64ABSLongThunk final : public Thunk {
52 public:
53   AArch64ABSLongThunk(Symbol &Dest) : Thunk(Dest) {}
54   uint32_t size() override { return 16; }
55   void writeTo(uint8_t *Buf) override;
56   void addSymbols(ThunkSection &IS) override;
57 };
58
59 class AArch64ADRPThunk final : public Thunk {
60 public:
61   AArch64ADRPThunk(Symbol &Dest) : Thunk(Dest) {}
62   uint32_t size() override { return 12; }
63   void writeTo(uint8_t *Buf) override;
64   void addSymbols(ThunkSection &IS) override;
65 };
66
67 // Base class for ARM thunks.
68 //
69 // An ARM thunk may be either short or long. A short thunk is simply a branch
70 // (B) instruction, and it may be used to call ARM functions when the distance
71 // from the thunk to the target is less than 32MB. Long thunks can branch to any
72 // virtual address and can switch between ARM and Thumb, and they are
73 // implemented in the derived classes. This class tries to create a short thunk
74 // if the target is in range, otherwise it creates a long thunk.
75 class ARMThunk : public Thunk {
76 public:
77   ARMThunk(Symbol &Dest) : Thunk(Dest) {}
78
79   bool mayUseShortThunk();
80   uint32_t size() override { return mayUseShortThunk() ? 4 : sizeLong(); }
81   void writeTo(uint8_t *Buf) override;
82   bool isCompatibleWith(RelType Type) const override;
83
84   // Returns the size of a long thunk.
85   virtual uint32_t sizeLong() = 0;
86
87   // Writes a long thunk to Buf.
88   virtual void writeLong(uint8_t *Buf) = 0;
89
90 private:
91   // This field tracks whether all previously considered layouts would allow
92   // this thunk to be short. If we have ever needed a long thunk, we always
93   // create a long thunk, even if the thunk may be short given the current
94   // distance to the target. We do this because transitioning from long to short
95   // can create layout oscillations in certain corner cases which would prevent
96   // the layout from converging.
97   bool MayUseShortThunk = true;
98 };
99
100 // Base class for Thumb-2 thunks.
101 //
102 // This class is similar to ARMThunk, but it uses the Thumb-2 B.W instruction
103 // which has a range of 16MB.
104 class ThumbThunk : public Thunk {
105 public:
106   ThumbThunk(Symbol &Dest) : Thunk(Dest) { Alignment = 2; }
107
108   bool mayUseShortThunk();
109   uint32_t size() override { return mayUseShortThunk() ? 4 : sizeLong(); }
110   void writeTo(uint8_t *Buf) override;
111   bool isCompatibleWith(RelType Type) const override;
112
113   // Returns the size of a long thunk.
114   virtual uint32_t sizeLong() = 0;
115
116   // Writes a long thunk to Buf.
117   virtual void writeLong(uint8_t *Buf) = 0;
118
119 private:
120   // See comment in ARMThunk above.
121   bool MayUseShortThunk = true;
122 };
123
124 // Specific ARM Thunk implementations. The naming convention is:
125 // Source State, TargetState, Target Requirement, ABS or PI, Range
126 class ARMV7ABSLongThunk final : public ARMThunk {
127 public:
128   ARMV7ABSLongThunk(Symbol &Dest) : ARMThunk(Dest) {}
129
130   uint32_t sizeLong() override { return 12; }
131   void writeLong(uint8_t *Buf) override;
132   void addSymbols(ThunkSection &IS) override;
133 };
134
135 class ARMV7PILongThunk final : public ARMThunk {
136 public:
137   ARMV7PILongThunk(Symbol &Dest) : ARMThunk(Dest) {}
138
139   uint32_t sizeLong() override { return 16; }
140   void writeLong(uint8_t *Buf) override;
141   void addSymbols(ThunkSection &IS) override;
142 };
143
144 class ThumbV7ABSLongThunk final : public ThumbThunk {
145 public:
146   ThumbV7ABSLongThunk(Symbol &Dest) : ThumbThunk(Dest) {}
147
148   uint32_t sizeLong() override { return 10; }
149   void writeLong(uint8_t *Buf) override;
150   void addSymbols(ThunkSection &IS) override;
151 };
152
153 class ThumbV7PILongThunk final : public ThumbThunk {
154 public:
155   ThumbV7PILongThunk(Symbol &Dest) : ThumbThunk(Dest) {}
156
157   uint32_t sizeLong() override { return 12; }
158   void writeLong(uint8_t *Buf) override;
159   void addSymbols(ThunkSection &IS) override;
160 };
161
162 // MIPS LA25 thunk
163 class MipsThunk final : public Thunk {
164 public:
165   MipsThunk(Symbol &Dest) : Thunk(Dest) {}
166
167   uint32_t size() override { return 16; }
168   void writeTo(uint8_t *Buf) override;
169   void addSymbols(ThunkSection &IS) override;
170   InputSection *getTargetInputSection() const override;
171 };
172
173 // microMIPS R2-R5 LA25 thunk
174 class MicroMipsThunk final : public Thunk {
175 public:
176   MicroMipsThunk(Symbol &Dest) : Thunk(Dest) {}
177
178   uint32_t size() override { return 14; }
179   void writeTo(uint8_t *Buf) override;
180   void addSymbols(ThunkSection &IS) override;
181   InputSection *getTargetInputSection() const override;
182 };
183
184 // microMIPS R6 LA25 thunk
185 class MicroMipsR6Thunk final : public Thunk {
186 public:
187   MicroMipsR6Thunk(Symbol &Dest) : Thunk(Dest) {}
188
189   uint32_t size() override { return 12; }
190   void writeTo(uint8_t *Buf) override;
191   void addSymbols(ThunkSection &IS) override;
192   InputSection *getTargetInputSection() const override;
193 };
194
195
196 // PPC64 Plt call stubs.
197 // Any call site that needs to call through a plt entry needs a call stub in
198 // the .text section. The call stub is responsible for:
199 // 1) Saving the toc-pointer to the stack.
200 // 2) Loading the target functions address from the procedure linkage table into
201 //    r12 for use by the target functions global entry point, and into the count
202 //    register.
203 // 3) Transfering control to the target function through an indirect branch.
204 class PPC64PltCallStub final : public Thunk {
205 public:
206   PPC64PltCallStub(Symbol &Dest) : Thunk(Dest) {}
207   uint32_t size() override { return 20; }
208   void writeTo(uint8_t *Buf) override;
209   void addSymbols(ThunkSection &IS) override;
210 };
211
212 } // end anonymous namespace
213
214 Defined *Thunk::addSymbol(StringRef Name, uint8_t Type, uint64_t Value,
215                           InputSectionBase &Section) {
216   Defined *D = addSyntheticLocal(Name, Type, Value, /*Size=*/0, Section);
217   Syms.push_back(D);
218   return D;
219 }
220
221 void Thunk::setOffset(uint64_t NewOffset) {
222   for (Defined *D : Syms)
223     D->Value = D->Value - Offset + NewOffset;
224   Offset = NewOffset;
225 }
226
227 // AArch64 long range Thunks
228
229 static uint64_t getAArch64ThunkDestVA(const Symbol &S) {
230   uint64_t V = S.isInPlt() ? S.getPltVA() : S.getVA();
231   return V;
232 }
233
234 void AArch64ABSLongThunk::writeTo(uint8_t *Buf) {
235   const uint8_t Data[] = {
236     0x50, 0x00, 0x00, 0x58, //     ldr x16, L0
237     0x00, 0x02, 0x1f, 0xd6, //     br  x16
238     0x00, 0x00, 0x00, 0x00, // L0: .xword S
239     0x00, 0x00, 0x00, 0x00,
240   };
241   uint64_t S = getAArch64ThunkDestVA(Destination);
242   memcpy(Buf, Data, sizeof(Data));
243   Target->relocateOne(Buf + 8, R_AARCH64_ABS64, S);
244 }
245
246 void AArch64ABSLongThunk::addSymbols(ThunkSection &IS) {
247   addSymbol(Saver.save("__AArch64AbsLongThunk_" + Destination.getName()),
248             STT_FUNC, 0, IS);
249   addSymbol("$x", STT_NOTYPE, 0, IS);
250   addSymbol("$d", STT_NOTYPE, 8, IS);
251 }
252
253 // This Thunk has a maximum range of 4Gb, this is sufficient for all programs
254 // using the small code model, including pc-relative ones. At time of writing
255 // clang and gcc do not support the large code model for position independent
256 // code so it is safe to use this for position independent thunks without
257 // worrying about the destination being more than 4Gb away.
258 void AArch64ADRPThunk::writeTo(uint8_t *Buf) {
259   const uint8_t Data[] = {
260       0x10, 0x00, 0x00, 0x90, // adrp x16, Dest R_AARCH64_ADR_PREL_PG_HI21(Dest)
261       0x10, 0x02, 0x00, 0x91, // add  x16, x16, R_AARCH64_ADD_ABS_LO12_NC(Dest)
262       0x00, 0x02, 0x1f, 0xd6, // br   x16
263   };
264   uint64_t S = getAArch64ThunkDestVA(Destination);
265   uint64_t P = getThunkTargetSym()->getVA();
266   memcpy(Buf, Data, sizeof(Data));
267   Target->relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
268                       getAArch64Page(S) - getAArch64Page(P));
269   Target->relocateOne(Buf + 4, R_AARCH64_ADD_ABS_LO12_NC, S);
270 }
271
272 void AArch64ADRPThunk::addSymbols(ThunkSection &IS) {
273   addSymbol(Saver.save("__AArch64ADRPThunk_" + Destination.getName()), STT_FUNC,
274             0, IS);
275   addSymbol("$x", STT_NOTYPE, 0, IS);
276 }
277
278 // ARM Target Thunks
279 static uint64_t getARMThunkDestVA(const Symbol &S) {
280   uint64_t V = S.isInPlt() ? S.getPltVA() : S.getVA();
281   return SignExtend64<32>(V);
282 }
283
284 // This function returns true if the target is not Thumb and is within 2^26, and
285 // it has not previously returned false (see comment for MayUseShortThunk).
286 bool ARMThunk::mayUseShortThunk() {
287   if (!MayUseShortThunk)
288     return false;
289   uint64_t S = getARMThunkDestVA(Destination);
290   if (S & 1) {
291     MayUseShortThunk = false;
292     return false;
293   }
294   uint64_t P = getThunkTargetSym()->getVA();
295   int64_t Offset = S - P - 8;
296   MayUseShortThunk = llvm::isInt<26>(Offset);
297   return MayUseShortThunk;
298 }
299
300 void ARMThunk::writeTo(uint8_t *Buf) {
301   if (!mayUseShortThunk()) {
302     writeLong(Buf);
303     return;
304   }
305
306   uint64_t S = getARMThunkDestVA(Destination);
307   uint64_t P = getThunkTargetSym()->getVA();
308   int64_t Offset = S - P - 8;
309   const uint8_t Data[] = {
310     0x00, 0x00, 0x00, 0xea, // b S
311   };
312   memcpy(Buf, Data, sizeof(Data));
313   Target->relocateOne(Buf, R_ARM_JUMP24, Offset);
314 }
315
316 bool ARMThunk::isCompatibleWith(RelType Type) const {
317   // Thumb branch relocations can't use BLX
318   return Type != R_ARM_THM_JUMP19 && Type != R_ARM_THM_JUMP24;
319 }
320
321 // This function returns true if the target is Thumb and is within 2^25, and
322 // it has not previously returned false (see comment for MayUseShortThunk).
323 bool ThumbThunk::mayUseShortThunk() {
324   if (!MayUseShortThunk)
325     return false;
326   uint64_t S = getARMThunkDestVA(Destination);
327   if ((S & 1) == 0) {
328     MayUseShortThunk = false;
329     return false;
330   }
331   uint64_t P = getThunkTargetSym()->getVA() & ~1;
332   int64_t Offset = S - P - 4;
333   MayUseShortThunk = llvm::isInt<25>(Offset);
334   return MayUseShortThunk;
335 }
336
337 void ThumbThunk::writeTo(uint8_t *Buf) {
338   if (!mayUseShortThunk()) {
339     writeLong(Buf);
340     return;
341   }
342
343   uint64_t S = getARMThunkDestVA(Destination);
344   uint64_t P = getThunkTargetSym()->getVA();
345   int64_t Offset = S - P - 4;
346   const uint8_t Data[] = {
347       0x00, 0xf0, 0x00, 0xb0, // b.w S
348   };
349   memcpy(Buf, Data, sizeof(Data));
350   Target->relocateOne(Buf, R_ARM_THM_JUMP24, Offset);
351 }
352
353 bool ThumbThunk::isCompatibleWith(RelType Type) const {
354   // ARM branch relocations can't use BLX
355   return Type != R_ARM_JUMP24 && Type != R_ARM_PC24 && Type != R_ARM_PLT32;
356 }
357
358 void ARMV7ABSLongThunk::writeLong(uint8_t *Buf) {
359   const uint8_t Data[] = {
360       0x00, 0xc0, 0x00, 0xe3, // movw         ip,:lower16:S
361       0x00, 0xc0, 0x40, 0xe3, // movt         ip,:upper16:S
362       0x1c, 0xff, 0x2f, 0xe1, // bx   ip
363   };
364   uint64_t S = getARMThunkDestVA(Destination);
365   memcpy(Buf, Data, sizeof(Data));
366   Target->relocateOne(Buf, R_ARM_MOVW_ABS_NC, S);
367   Target->relocateOne(Buf + 4, R_ARM_MOVT_ABS, S);
368 }
369
370 void ARMV7ABSLongThunk::addSymbols(ThunkSection &IS) {
371   addSymbol(Saver.save("__ARMv7ABSLongThunk_" + Destination.getName()),
372             STT_FUNC, 0, IS);
373   addSymbol("$a", STT_NOTYPE, 0, IS);
374 }
375
376 void ThumbV7ABSLongThunk::writeLong(uint8_t *Buf) {
377   const uint8_t Data[] = {
378       0x40, 0xf2, 0x00, 0x0c, // movw         ip, :lower16:S
379       0xc0, 0xf2, 0x00, 0x0c, // movt         ip, :upper16:S
380       0x60, 0x47,             // bx   ip
381   };
382   uint64_t S = getARMThunkDestVA(Destination);
383   memcpy(Buf, Data, sizeof(Data));
384   Target->relocateOne(Buf, R_ARM_THM_MOVW_ABS_NC, S);
385   Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_ABS, S);
386 }
387
388 void ThumbV7ABSLongThunk::addSymbols(ThunkSection &IS) {
389   addSymbol(Saver.save("__Thumbv7ABSLongThunk_" + Destination.getName()),
390             STT_FUNC, 1, IS);
391   addSymbol("$t", STT_NOTYPE, 0, IS);
392 }
393
394 void ARMV7PILongThunk::writeLong(uint8_t *Buf) {
395   const uint8_t Data[] = {
396       0xf0, 0xcf, 0x0f, 0xe3, // P:  movw ip,:lower16:S - (P + (L1-P) + 8)
397       0x00, 0xc0, 0x40, 0xe3, //     movt ip,:upper16:S - (P + (L1-P) + 8)
398       0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
399       0x1c, 0xff, 0x2f, 0xe1, //     bx r12
400   };
401   uint64_t S = getARMThunkDestVA(Destination);
402   uint64_t P = getThunkTargetSym()->getVA();
403   uint64_t Offset = S - P - 16;
404   memcpy(Buf, Data, sizeof(Data));
405   Target->relocateOne(Buf, R_ARM_MOVW_PREL_NC, Offset);
406   Target->relocateOne(Buf + 4, R_ARM_MOVT_PREL, Offset);
407 }
408
409 void ARMV7PILongThunk::addSymbols(ThunkSection &IS) {
410   addSymbol(Saver.save("__ARMV7PILongThunk_" + Destination.getName()), STT_FUNC,
411             0, IS);
412   addSymbol("$a", STT_NOTYPE, 0, IS);
413 }
414
415 void ThumbV7PILongThunk::writeLong(uint8_t *Buf) {
416   const uint8_t Data[] = {
417       0x4f, 0xf6, 0xf4, 0x7c, // P:  movw ip,:lower16:S - (P + (L1-P) + 4)
418       0xc0, 0xf2, 0x00, 0x0c, //     movt ip,:upper16:S - (P + (L1-P) + 4)
419       0xfc, 0x44,             // L1: add  r12, pc
420       0x60, 0x47,             //     bx   r12
421   };
422   uint64_t S = getARMThunkDestVA(Destination);
423   uint64_t P = getThunkTargetSym()->getVA() & ~0x1;
424   uint64_t Offset = S - P - 12;
425   memcpy(Buf, Data, sizeof(Data));
426   Target->relocateOne(Buf, R_ARM_THM_MOVW_PREL_NC, Offset);
427   Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_PREL, Offset);
428 }
429
430 void ThumbV7PILongThunk::addSymbols(ThunkSection &IS) {
431   addSymbol(Saver.save("__ThumbV7PILongThunk_" + Destination.getName()),
432             STT_FUNC, 1, IS);
433   addSymbol("$t", STT_NOTYPE, 0, IS);
434 }
435
436 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
437 void MipsThunk::writeTo(uint8_t *Buf) {
438   uint64_t S = Destination.getVA();
439   write32(Buf, 0x3c190000); // lui   $25, %hi(func)
440   write32(Buf + 4, 0x08000000 | (S >> 2)); // j     func
441   write32(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
442   write32(Buf + 12, 0x00000000); // nop
443   Target->relocateOne(Buf, R_MIPS_HI16, S);
444   Target->relocateOne(Buf + 8, R_MIPS_LO16, S);
445 }
446
447 void MipsThunk::addSymbols(ThunkSection &IS) {
448   addSymbol(Saver.save("__LA25Thunk_" + Destination.getName()), STT_FUNC, 0,
449             IS);
450 }
451
452 InputSection *MipsThunk::getTargetInputSection() const {
453   auto &DR = cast<Defined>(Destination);
454   return dyn_cast<InputSection>(DR.Section);
455 }
456
457 // Write microMIPS R2-R5 LA25 thunk code
458 // to call PIC function from the non-PIC one.
459 void MicroMipsThunk::writeTo(uint8_t *Buf) {
460   uint64_t S = Destination.getVA() | 1;
461   write16(Buf, 0x41b9);       // lui   $25, %hi(func)
462   write16(Buf + 4, 0xd400);   // j     func
463   write16(Buf + 8, 0x3339);   // addiu $25, $25, %lo(func)
464   write16(Buf + 12, 0x0c00);  // nop
465   Target->relocateOne(Buf, R_MICROMIPS_HI16, S);
466   Target->relocateOne(Buf + 4, R_MICROMIPS_26_S1, S);
467   Target->relocateOne(Buf + 8, R_MICROMIPS_LO16, S);
468 }
469
470 void MicroMipsThunk::addSymbols(ThunkSection &IS) {
471   Defined *D = addSymbol(
472       Saver.save("__microLA25Thunk_" + Destination.getName()), STT_FUNC, 0, IS);
473   D->StOther |= STO_MIPS_MICROMIPS;
474 }
475
476 InputSection *MicroMipsThunk::getTargetInputSection() const {
477   auto &DR = cast<Defined>(Destination);
478   return dyn_cast<InputSection>(DR.Section);
479 }
480
481 // Write microMIPS R6 LA25 thunk code
482 // to call PIC function from the non-PIC one.
483 void MicroMipsR6Thunk::writeTo(uint8_t *Buf) {
484   uint64_t S = Destination.getVA() | 1;
485   uint64_t P = getThunkTargetSym()->getVA();
486   write16(Buf, 0x1320);       // lui   $25, %hi(func)
487   write16(Buf + 4, 0x3339);   // addiu $25, $25, %lo(func)
488   write16(Buf + 8, 0x9400);   // bc    func
489   Target->relocateOne(Buf, R_MICROMIPS_HI16, S);
490   Target->relocateOne(Buf + 4, R_MICROMIPS_LO16, S);
491   Target->relocateOne(Buf + 8, R_MICROMIPS_PC26_S1, S - P - 12);
492 }
493
494 void MicroMipsR6Thunk::addSymbols(ThunkSection &IS) {
495   Defined *D = addSymbol(
496       Saver.save("__microLA25Thunk_" + Destination.getName()), STT_FUNC, 0, IS);
497   D->StOther |= STO_MIPS_MICROMIPS;
498 }
499
500 InputSection *MicroMipsR6Thunk::getTargetInputSection() const {
501   auto &DR = cast<Defined>(Destination);
502   return dyn_cast<InputSection>(DR.Section);
503 }
504
505 void PPC64PltCallStub::writeTo(uint8_t *Buf) {
506   int64_t Off = Destination.getGotPltVA() - getPPC64TocBase();
507   // Need to add 0x8000 to offset to account for the low bits being signed.
508   uint16_t OffHa = (Off + 0x8000) >> 16;
509   uint16_t OffLo = Off;
510
511   write32(Buf +  0, 0xf8410018);          // std     r2,24(r1)
512   write32(Buf +  4, 0x3d820000 | OffHa);  // addis   r12,r2, X@plt@to@ha
513   write32(Buf +  8, 0xe98c0000 | OffLo);  // ld      r12,X@plt@toc@l(r12)
514   write32(Buf + 12, 0x7d8903a6);          // mtctr   r12
515   write32(Buf + 16, 0x4e800420);          // bctr
516 }
517
518 void PPC64PltCallStub::addSymbols(ThunkSection &IS) {
519   Defined *S = addSymbol(Saver.save("__plt_" + Destination.getName()), STT_FUNC,
520                          0, IS);
521   S->NeedsTocRestore = true;
522 }
523
524 Thunk::Thunk(Symbol &D) : Destination(D), Offset(0) {}
525
526 Thunk::~Thunk() = default;
527
528 static Thunk *addThunkAArch64(RelType Type, Symbol &S) {
529   if (Type != R_AARCH64_CALL26 && Type != R_AARCH64_JUMP26)
530     fatal("unrecognized relocation type");
531   if (Config->Pic)
532     return make<AArch64ADRPThunk>(S);
533   return make<AArch64ABSLongThunk>(S);
534 }
535
536 // Creates a thunk for Thumb-ARM interworking.
537 static Thunk *addThunkArm(RelType Reloc, Symbol &S) {
538   // ARM relocations need ARM to Thumb interworking Thunks.
539   // Thumb relocations need Thumb to ARM relocations.
540   // Use position independent Thunks if we require position independent code.
541   switch (Reloc) {
542   case R_ARM_PC24:
543   case R_ARM_PLT32:
544   case R_ARM_JUMP24:
545   case R_ARM_CALL:
546     if (Config->Pic)
547       return make<ARMV7PILongThunk>(S);
548     return make<ARMV7ABSLongThunk>(S);
549   case R_ARM_THM_JUMP19:
550   case R_ARM_THM_JUMP24:
551   case R_ARM_THM_CALL:
552     if (Config->Pic)
553       return make<ThumbV7PILongThunk>(S);
554     return make<ThumbV7ABSLongThunk>(S);
555   }
556   fatal("unrecognized relocation type");
557 }
558
559 static Thunk *addThunkMips(RelType Type, Symbol &S) {
560   if ((S.StOther & STO_MIPS_MICROMIPS) && isMipsR6())
561     return make<MicroMipsR6Thunk>(S);
562   if (S.StOther & STO_MIPS_MICROMIPS)
563     return make<MicroMipsThunk>(S);
564   return make<MipsThunk>(S);
565 }
566
567 static Thunk *addThunkPPC64(RelType Type, Symbol &S) {
568   if (Type == R_PPC64_REL24)
569     return make<PPC64PltCallStub>(S);
570   fatal("unexpected relocation type");
571 }
572
573 Thunk *addThunk(RelType Type, Symbol &S) {
574   if (Config->EMachine == EM_AARCH64)
575     return addThunkAArch64(Type, S);
576
577   if (Config->EMachine == EM_ARM)
578     return addThunkArm(Type, S);
579
580   if (Config->EMachine == EM_MIPS)
581     return addThunkMips(Type, S);
582
583   if (Config->EMachine == EM_PPC64)
584     return addThunkPPC64(Type, S);
585
586   llvm_unreachable("add Thunk only supported for ARM, Mips and PowerPC");
587 }
588
589 } // end namespace elf
590 } // end namespace lld