]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/Thunks.cpp
Merge compiler-rt trunk r321414 to contrib/compiler-rt.
[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::support::endian;
44 using namespace llvm::ELF;
45
46 namespace lld {
47 namespace elf {
48
49 namespace {
50
51 // AArch64 long range Thunks
52 class AArch64ABSLongThunk final : public Thunk {
53 public:
54   AArch64ABSLongThunk(Symbol &Dest) : Thunk(Dest) {}
55   uint32_t size() const override { return 16; }
56   void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
57   void addSymbols(ThunkSection &IS) override;
58 };
59
60 class AArch64ADRPThunk final : public Thunk {
61 public:
62   AArch64ADRPThunk(Symbol &Dest) : Thunk(Dest) {}
63   uint32_t size() const override { return 12; }
64   void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
65   void addSymbols(ThunkSection &IS) override;
66 };
67
68 // Specific ARM Thunk implementations. The naming convention is:
69 // Source State, TargetState, Target Requirement, ABS or PI, Range
70 class ARMV7ABSLongThunk final : public Thunk {
71 public:
72   ARMV7ABSLongThunk(Symbol &Dest) : Thunk(Dest) {}
73
74   uint32_t size() const override { return 12; }
75   void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
76   void addSymbols(ThunkSection &IS) override;
77   bool isCompatibleWith(RelType Type) const override;
78 };
79
80 class ARMV7PILongThunk final : public Thunk {
81 public:
82   ARMV7PILongThunk(Symbol &Dest) : Thunk(Dest) {}
83
84   uint32_t size() const override { return 16; }
85   void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
86   void addSymbols(ThunkSection &IS) override;
87   bool isCompatibleWith(RelType Type) const override;
88 };
89
90 class ThumbV7ABSLongThunk final : public Thunk {
91 public:
92   ThumbV7ABSLongThunk(Symbol &Dest) : Thunk(Dest) { Alignment = 2; }
93
94   uint32_t size() const override { return 10; }
95   void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
96   void addSymbols(ThunkSection &IS) override;
97   bool isCompatibleWith(RelType Type) const override;
98 };
99
100 class ThumbV7PILongThunk final : public Thunk {
101 public:
102   ThumbV7PILongThunk(Symbol &Dest) : Thunk(Dest) { Alignment = 2; }
103
104   uint32_t size() const override { return 12; }
105   void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
106   void addSymbols(ThunkSection &IS) override;
107   bool isCompatibleWith(RelType Type) const override;
108 };
109
110 // MIPS LA25 thunk
111 class MipsThunk final : public Thunk {
112 public:
113   MipsThunk(Symbol &Dest) : Thunk(Dest) {}
114
115   uint32_t size() const override { return 16; }
116   void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
117   void addSymbols(ThunkSection &IS) override;
118   InputSection *getTargetInputSection() const override;
119 };
120
121 // microMIPS R2-R5 LA25 thunk
122 class MicroMipsThunk final : public Thunk {
123 public:
124   MicroMipsThunk(Symbol &Dest) : Thunk(Dest) {}
125
126   uint32_t size() const override { return 14; }
127   void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
128   void addSymbols(ThunkSection &IS) override;
129   InputSection *getTargetInputSection() const override;
130 };
131
132 // microMIPS R6 LA25 thunk
133 class MicroMipsR6Thunk final : public Thunk {
134 public:
135   MicroMipsR6Thunk(Symbol &Dest) : Thunk(Dest) {}
136
137   uint32_t size() const override { return 12; }
138   void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
139   void addSymbols(ThunkSection &IS) override;
140   InputSection *getTargetInputSection() const override;
141 };
142
143 } // end anonymous namespace
144
145 // AArch64 long range Thunks
146
147 static uint64_t getAArch64ThunkDestVA(const Symbol &S) {
148   uint64_t V = S.isInPlt() ? S.getPltVA() : S.getVA();
149   return V;
150 }
151
152 void AArch64ABSLongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
153   const uint8_t Data[] = {
154     0x50, 0x00, 0x00, 0x58, //     ldr x16, L0
155     0x00, 0x02, 0x1f, 0xd6, //     br  x16
156     0x00, 0x00, 0x00, 0x00, // L0: .xword S
157     0x00, 0x00, 0x00, 0x00,
158   };
159   uint64_t S = getAArch64ThunkDestVA(Destination);
160   memcpy(Buf, Data, sizeof(Data));
161   Target->relocateOne(Buf + 8, R_AARCH64_ABS64, S);
162 }
163
164 void AArch64ABSLongThunk::addSymbols(ThunkSection &IS) {
165   ThunkSym = addSyntheticLocal(
166       Saver.save("__AArch64AbsLongThunk_" + Destination.getName()), STT_FUNC,
167       Offset, size(), IS);
168   addSyntheticLocal("$x", STT_NOTYPE, Offset, 0, IS);
169   addSyntheticLocal("$d", STT_NOTYPE, Offset + 8, 0, IS);
170 }
171
172 // This Thunk has a maximum range of 4Gb, this is sufficient for all programs
173 // using the small code model, including pc-relative ones. At time of writing
174 // clang and gcc do not support the large code model for position independent
175 // code so it is safe to use this for position independent thunks without
176 // worrying about the destination being more than 4Gb away.
177 void AArch64ADRPThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
178   const uint8_t Data[] = {
179       0x10, 0x00, 0x00, 0x90, // adrp x16, Dest R_AARCH64_ADR_PREL_PG_HI21(Dest)
180       0x10, 0x02, 0x00, 0x91, // add  x16, x16, R_AARCH64_ADD_ABS_LO12_NC(Dest)
181       0x00, 0x02, 0x1f, 0xd6, // br   x16
182   };
183   uint64_t S = getAArch64ThunkDestVA(Destination);
184   uint64_t P = ThunkSym->getVA();
185   memcpy(Buf, Data, sizeof(Data));
186   Target->relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
187                       getAArch64Page(S) - getAArch64Page(P));
188   Target->relocateOne(Buf + 4, R_AARCH64_ADD_ABS_LO12_NC, S);
189 }
190
191 void AArch64ADRPThunk::addSymbols(ThunkSection &IS)
192 {
193   ThunkSym = addSyntheticLocal(
194       Saver.save("__AArch64ADRPThunk_" + Destination.getName()), STT_FUNC,
195       Offset, size(), IS);
196   addSyntheticLocal("$x", STT_NOTYPE, Offset, 0, IS);
197 }
198
199 // ARM Target Thunks
200 static uint64_t getARMThunkDestVA(const Symbol &S) {
201   uint64_t V = S.isInPlt() ? S.getPltVA() : S.getVA();
202   return SignExtend64<32>(V);
203 }
204
205 void ARMV7ABSLongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
206   const uint8_t Data[] = {
207       0x00, 0xc0, 0x00, 0xe3, // movw         ip,:lower16:S
208       0x00, 0xc0, 0x40, 0xe3, // movt         ip,:upper16:S
209       0x1c, 0xff, 0x2f, 0xe1, // bx   ip
210   };
211   uint64_t S = getARMThunkDestVA(Destination);
212   memcpy(Buf, Data, sizeof(Data));
213   Target->relocateOne(Buf, R_ARM_MOVW_ABS_NC, S);
214   Target->relocateOne(Buf + 4, R_ARM_MOVT_ABS, S);
215 }
216
217 void ARMV7ABSLongThunk::addSymbols(ThunkSection &IS) {
218   ThunkSym = addSyntheticLocal(
219       Saver.save("__ARMv7ABSLongThunk_" + Destination.getName()), STT_FUNC,
220       Offset, size(), IS);
221   addSyntheticLocal("$a", STT_NOTYPE, Offset, 0, IS);
222 }
223
224 bool ARMV7ABSLongThunk::isCompatibleWith(RelType Type) const {
225   // Thumb branch relocations can't use BLX
226   return Type != R_ARM_THM_JUMP19 && Type != R_ARM_THM_JUMP24;
227 }
228
229 void ThumbV7ABSLongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
230   const uint8_t Data[] = {
231       0x40, 0xf2, 0x00, 0x0c, // movw         ip, :lower16:S
232       0xc0, 0xf2, 0x00, 0x0c, // movt         ip, :upper16:S
233       0x60, 0x47,             // bx   ip
234   };
235   uint64_t S = getARMThunkDestVA(Destination);
236   memcpy(Buf, Data, sizeof(Data));
237   Target->relocateOne(Buf, R_ARM_THM_MOVW_ABS_NC, S);
238   Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_ABS, S);
239 }
240
241 void ThumbV7ABSLongThunk::addSymbols(ThunkSection &IS) {
242   ThunkSym = addSyntheticLocal(
243       Saver.save("__Thumbv7ABSLongThunk_" + Destination.getName()), STT_FUNC,
244       Offset | 0x1, size(), IS);
245   addSyntheticLocal("$t", STT_NOTYPE, Offset, 0, IS);
246 }
247
248 bool ThumbV7ABSLongThunk::isCompatibleWith(RelType Type) const {
249   // ARM branch relocations can't use BLX
250   return Type != R_ARM_JUMP24 && Type != R_ARM_PC24 && Type != R_ARM_PLT32;
251 }
252
253 void ARMV7PILongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
254   const uint8_t Data[] = {
255       0xf0, 0xcf, 0x0f, 0xe3, // P:  movw ip,:lower16:S - (P + (L1-P) + 8)
256       0x00, 0xc0, 0x40, 0xe3, //     movt ip,:upper16:S - (P + (L1-P) + 8)
257       0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
258       0x1c, 0xff, 0x2f, 0xe1, //     bx r12
259   };
260   uint64_t S = getARMThunkDestVA(Destination);
261   uint64_t P = ThunkSym->getVA();
262   uint64_t Offset = S - P - 16;
263   memcpy(Buf, Data, sizeof(Data));
264   Target->relocateOne(Buf, R_ARM_MOVW_PREL_NC, Offset);
265   Target->relocateOne(Buf + 4, R_ARM_MOVT_PREL, Offset);
266 }
267
268 void ARMV7PILongThunk::addSymbols(ThunkSection &IS) {
269   ThunkSym = addSyntheticLocal(
270       Saver.save("__ARMV7PILongThunk_" + Destination.getName()), STT_FUNC,
271       Offset, size(), IS);
272   addSyntheticLocal("$a", STT_NOTYPE, Offset, 0, IS);
273 }
274
275 bool ARMV7PILongThunk::isCompatibleWith(RelType Type) const {
276   // Thumb branch relocations can't use BLX
277   return Type != R_ARM_THM_JUMP19 && Type != R_ARM_THM_JUMP24;
278 }
279
280 void ThumbV7PILongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
281   const uint8_t Data[] = {
282       0x4f, 0xf6, 0xf4, 0x7c, // P:  movw ip,:lower16:S - (P + (L1-P) + 4)
283       0xc0, 0xf2, 0x00, 0x0c, //     movt ip,:upper16:S - (P + (L1-P) + 4)
284       0xfc, 0x44,             // L1: add  r12, pc
285       0x60, 0x47,             //     bx   r12
286   };
287   uint64_t S = getARMThunkDestVA(Destination);
288   uint64_t P = ThunkSym->getVA() & ~0x1;
289   uint64_t Offset = S - P - 12;
290   memcpy(Buf, Data, sizeof(Data));
291   Target->relocateOne(Buf, R_ARM_THM_MOVW_PREL_NC, Offset);
292   Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_PREL, Offset);
293 }
294
295 void ThumbV7PILongThunk::addSymbols(ThunkSection &IS) {
296   ThunkSym = addSyntheticLocal(
297       Saver.save("__ThumbV7PILongThunk_" + Destination.getName()), STT_FUNC,
298       Offset | 0x1, size(), IS);
299   addSyntheticLocal("$t", STT_NOTYPE, Offset, 0, IS);
300 }
301
302 bool ThumbV7PILongThunk::isCompatibleWith(RelType Type) const {
303   // ARM branch relocations can't use BLX
304   return Type != R_ARM_JUMP24 && Type != R_ARM_PC24 && Type != R_ARM_PLT32;
305 }
306
307 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
308 void MipsThunk::writeTo(uint8_t *Buf, ThunkSection &) const {
309   uint64_t S = Destination.getVA();
310   write32(Buf, 0x3c190000, Config->Endianness); // lui   $25, %hi(func)
311   write32(Buf + 4, 0x08000000 | (S >> 2), Config->Endianness); // j     func
312   write32(Buf + 8, 0x27390000, Config->Endianness); // addiu $25, $25, %lo(func)
313   write32(Buf + 12, 0x00000000, Config->Endianness); // nop
314   Target->relocateOne(Buf, R_MIPS_HI16, S);
315   Target->relocateOne(Buf + 8, R_MIPS_LO16, S);
316 }
317
318 void MipsThunk::addSymbols(ThunkSection &IS) {
319   ThunkSym =
320       addSyntheticLocal(Saver.save("__LA25Thunk_" + Destination.getName()),
321                         STT_FUNC, Offset, size(), IS);
322 }
323
324 InputSection *MipsThunk::getTargetInputSection() const {
325   auto &DR = cast<Defined>(Destination);
326   return dyn_cast<InputSection>(DR.Section);
327 }
328
329 // Write microMIPS R2-R5 LA25 thunk code
330 // to call PIC function from the non-PIC one.
331 void MicroMipsThunk::writeTo(uint8_t *Buf, ThunkSection &) const {
332   uint64_t S = Destination.getVA() | 1;
333   write16(Buf, 0x41b9, Config->Endianness);       // lui   $25, %hi(func)
334   write16(Buf + 4, 0xd400, Config->Endianness);   // j     func
335   write16(Buf + 8, 0x3339, Config->Endianness);   // addiu $25, $25, %lo(func)
336   write16(Buf + 12, 0x0c00, Config->Endianness);  // nop
337   Target->relocateOne(Buf, R_MICROMIPS_HI16, S);
338   Target->relocateOne(Buf + 4, R_MICROMIPS_26_S1, S);
339   Target->relocateOne(Buf + 8, R_MICROMIPS_LO16, S);
340 }
341
342 void MicroMipsThunk::addSymbols(ThunkSection &IS) {
343   ThunkSym =
344       addSyntheticLocal(Saver.save("__microLA25Thunk_" + Destination.getName()),
345                         STT_FUNC, Offset, size(), IS);
346   ThunkSym->StOther |= STO_MIPS_MICROMIPS;
347 }
348
349 InputSection *MicroMipsThunk::getTargetInputSection() const {
350   auto &DR = cast<Defined>(Destination);
351   return dyn_cast<InputSection>(DR.Section);
352 }
353
354 // Write microMIPS R6 LA25 thunk code
355 // to call PIC function from the non-PIC one.
356 void MicroMipsR6Thunk::writeTo(uint8_t *Buf, ThunkSection &) const {
357   uint64_t S = Destination.getVA() | 1;
358   uint64_t P = ThunkSym->getVA();
359   write16(Buf, 0x1320, Config->Endianness);       // lui   $25, %hi(func)
360   write16(Buf + 4, 0x3339, Config->Endianness);   // addiu $25, $25, %lo(func)
361   write16(Buf + 8, 0x9400, Config->Endianness);   // bc    func
362   Target->relocateOne(Buf, R_MICROMIPS_HI16, S);
363   Target->relocateOne(Buf + 4, R_MICROMIPS_LO16, S);
364   Target->relocateOne(Buf + 8, R_MICROMIPS_PC26_S1, S - P - 12);
365 }
366
367 void MicroMipsR6Thunk::addSymbols(ThunkSection &IS) {
368   ThunkSym =
369       addSyntheticLocal(Saver.save("__microLA25Thunk_" + Destination.getName()),
370                         STT_FUNC, Offset, size(), IS);
371   ThunkSym->StOther |= STO_MIPS_MICROMIPS;
372 }
373
374 InputSection *MicroMipsR6Thunk::getTargetInputSection() const {
375   auto &DR = cast<Defined>(Destination);
376   return dyn_cast<InputSection>(DR.Section);
377 }
378
379 Thunk::Thunk(Symbol &D) : Destination(D), Offset(0) {}
380
381 Thunk::~Thunk() = default;
382
383 static Thunk *addThunkAArch64(RelType Type, Symbol &S) {
384   if (Type != R_AARCH64_CALL26 && Type != R_AARCH64_JUMP26)
385     fatal("unrecognized relocation type");
386   if (Config->Pic)
387     return make<AArch64ADRPThunk>(S);
388   return make<AArch64ABSLongThunk>(S);
389 }
390
391 // Creates a thunk for Thumb-ARM interworking.
392 static Thunk *addThunkArm(RelType Reloc, Symbol &S) {
393   // ARM relocations need ARM to Thumb interworking Thunks.
394   // Thumb relocations need Thumb to ARM relocations.
395   // Use position independent Thunks if we require position independent code.
396   switch (Reloc) {
397   case R_ARM_PC24:
398   case R_ARM_PLT32:
399   case R_ARM_JUMP24:
400   case R_ARM_CALL:
401     if (Config->Pic)
402       return make<ARMV7PILongThunk>(S);
403     return make<ARMV7ABSLongThunk>(S);
404   case R_ARM_THM_JUMP19:
405   case R_ARM_THM_JUMP24:
406   case R_ARM_THM_CALL:
407     if (Config->Pic)
408       return make<ThumbV7PILongThunk>(S);
409     return make<ThumbV7ABSLongThunk>(S);
410   }
411   fatal("unrecognized relocation type");
412 }
413
414 static Thunk *addThunkMips(RelType Type, Symbol &S) {
415   if ((S.StOther & STO_MIPS_MICROMIPS) && isMipsR6())
416     return make<MicroMipsR6Thunk>(S);
417   if (S.StOther & STO_MIPS_MICROMIPS)
418     return make<MicroMipsThunk>(S);
419   return make<MipsThunk>(S);
420 }
421
422 Thunk *addThunk(RelType Type, Symbol &S) {
423   if (Config->EMachine == EM_AARCH64)
424     return addThunkAArch64(Type, S);
425   else if (Config->EMachine == EM_ARM)
426     return addThunkArm(Type, S);
427   else if (Config->EMachine == EM_MIPS)
428     return addThunkMips(Type, S);
429   llvm_unreachable("add Thunk only supported for ARM and Mips");
430   return nullptr;
431 }
432
433 } // end namespace elf
434 } // end namespace lld