xref: /llvm-project-15.0.7/lld/ELF/Thunks.cpp (revision feefb087)
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 "Error.h"
27 #include "InputSection.h"
28 #include "Memory.h"
29 #include "OutputSections.h"
30 #include "Symbols.h"
31 #include "SyntheticSections.h"
32 #include "Target.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 // Specific ARM Thunk implementations. The naming convention is:
52 // Source State, TargetState, Target Requirement, ABS or PI, Range
53 class ARMV7ABSLongThunk final : public Thunk {
54 public:
55   ARMV7ABSLongThunk(const SymbolBody &Dest) : Thunk(Dest) {}
56 
57   uint32_t size() const override { return 12; }
58   void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
59   void addSymbols(ThunkSection &IS) override;
60   bool isCompatibleWith(uint32_t RelocType) const override;
61 };
62 
63 class ARMV7PILongThunk final : public Thunk {
64 public:
65   ARMV7PILongThunk(const SymbolBody &Dest) : Thunk(Dest) {}
66 
67   uint32_t size() const override { return 16; }
68   void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
69   void addSymbols(ThunkSection &IS) override;
70   bool isCompatibleWith(uint32_t RelocType) const override;
71 };
72 
73 class ThumbV7ABSLongThunk final : public Thunk {
74 public:
75   ThumbV7ABSLongThunk(const SymbolBody &Dest) : Thunk(Dest) { Alignment = 2; }
76 
77   uint32_t size() const override { return 10; }
78   void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
79   void addSymbols(ThunkSection &IS) override;
80   bool isCompatibleWith(uint32_t RelocType) const override;
81 };
82 
83 class ThumbV7PILongThunk final : public Thunk {
84 public:
85   ThumbV7PILongThunk(const SymbolBody &Dest) : Thunk(Dest) { Alignment = 2; }
86 
87   uint32_t size() const override { return 12; }
88   void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
89   void addSymbols(ThunkSection &IS) override;
90   bool isCompatibleWith(uint32_t RelocType) const override;
91 };
92 
93 // MIPS LA25 thunk
94 class MipsThunk final : public Thunk {
95 public:
96   MipsThunk(const SymbolBody &Dest) : Thunk(Dest) {}
97 
98   uint32_t size() const override { return 16; }
99   void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
100   void addSymbols(ThunkSection &IS) override;
101   InputSection *getTargetInputSection() const override;
102 };
103 
104 // microMIPS R2-R5 LA25 thunk
105 class MicroMipsThunk final : public Thunk {
106 public:
107   MicroMipsThunk(const SymbolBody &Dest) : Thunk(Dest) {}
108 
109   uint32_t size() const override { return 14; }
110   void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
111   void addSymbols(ThunkSection &IS) override;
112   InputSection *getTargetInputSection() const override;
113 };
114 
115 // microMIPS R6 LA25 thunk
116 class MicroMipsR6Thunk final : public Thunk {
117 public:
118   MicroMipsR6Thunk(const SymbolBody &Dest) : Thunk(Dest) {}
119 
120   uint32_t size() const override { return 12; }
121   void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
122   void addSymbols(ThunkSection &IS) override;
123   InputSection *getTargetInputSection() const override;
124 };
125 
126 } // end anonymous namespace
127 
128 // ARM Target Thunks
129 static uint64_t getARMThunkDestVA(const SymbolBody &S) {
130   uint64_t V = S.isInPlt() ? S.getPltVA() : S.getVA();
131   return SignExtend64<32>(V);
132 }
133 
134 void ARMV7ABSLongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
135   const uint8_t Data[] = {
136       0x00, 0xc0, 0x00, 0xe3, // movw         ip,:lower16:S
137       0x00, 0xc0, 0x40, 0xe3, // movt         ip,:upper16:S
138       0x1c, 0xff, 0x2f, 0xe1, // bx   ip
139   };
140   uint64_t S = getARMThunkDestVA(Destination);
141   memcpy(Buf, Data, sizeof(Data));
142   Target->relocateOne(Buf, R_ARM_MOVW_ABS_NC, S);
143   Target->relocateOne(Buf + 4, R_ARM_MOVT_ABS, S);
144 }
145 
146 void ARMV7ABSLongThunk::addSymbols(ThunkSection &IS) {
147   ThunkSym = addSyntheticLocal(
148       Saver.save("__ARMv7ABSLongThunk_" + Destination.getName()), STT_FUNC,
149       Offset, size(), &IS);
150   addSyntheticLocal("$a", STT_NOTYPE, Offset, 0, &IS);
151 }
152 
153 bool ARMV7ABSLongThunk::isCompatibleWith(uint32_t RelocType) const {
154   // Thumb branch relocations can't use BLX
155   return RelocType != R_ARM_THM_JUMP19 && RelocType != R_ARM_THM_JUMP24;
156 }
157 
158 void ThumbV7ABSLongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
159   const uint8_t Data[] = {
160       0x40, 0xf2, 0x00, 0x0c, // movw         ip, :lower16:S
161       0xc0, 0xf2, 0x00, 0x0c, // movt         ip, :upper16:S
162       0x60, 0x47,             // bx   ip
163   };
164   uint64_t S = getARMThunkDestVA(Destination);
165   memcpy(Buf, Data, sizeof(Data));
166   Target->relocateOne(Buf, R_ARM_THM_MOVW_ABS_NC, S);
167   Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_ABS, S);
168 }
169 
170 void ThumbV7ABSLongThunk::addSymbols(ThunkSection &IS) {
171   ThunkSym = addSyntheticLocal(
172       Saver.save("__Thumbv7ABSLongThunk_" + Destination.getName()), STT_FUNC,
173       Offset | 0x1, size(), &IS);
174   addSyntheticLocal("$t", STT_NOTYPE, Offset, 0, &IS);
175 }
176 
177 bool ThumbV7ABSLongThunk::isCompatibleWith(uint32_t RelocType) const {
178   // ARM branch relocations can't use BLX
179   return RelocType != R_ARM_JUMP24 && RelocType != R_ARM_PC24 &&
180          RelocType != R_ARM_PLT32;
181 }
182 
183 void ARMV7PILongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
184   const uint8_t Data[] = {
185       0xf0, 0xcf, 0x0f, 0xe3, // P:  movw ip,:lower16:S - (P + (L1-P) + 8)
186       0x00, 0xc0, 0x40, 0xe3, //     movt ip,:upper16:S - (P + (L1-P) + 8)
187       0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
188       0x1c, 0xff, 0x2f, 0xe1, //     bx r12
189   };
190   uint64_t S = getARMThunkDestVA(Destination);
191   uint64_t P = ThunkSym->getVA();
192   uint64_t Offset = S - P - 16;
193   memcpy(Buf, Data, sizeof(Data));
194   Target->relocateOne(Buf, R_ARM_MOVW_PREL_NC, Offset);
195   Target->relocateOne(Buf + 4, R_ARM_MOVT_PREL, Offset);
196 }
197 
198 void ARMV7PILongThunk::addSymbols(ThunkSection &IS) {
199   ThunkSym = addSyntheticLocal(
200       Saver.save("__ARMV7PILongThunk_" + Destination.getName()), STT_FUNC,
201       Offset, size(), &IS);
202   addSyntheticLocal("$a", STT_NOTYPE, Offset, 0, &IS);
203 }
204 
205 bool ARMV7PILongThunk::isCompatibleWith(uint32_t RelocType) const {
206   // Thumb branch relocations can't use BLX
207   return RelocType != R_ARM_THM_JUMP19 && RelocType != R_ARM_THM_JUMP24;
208 }
209 
210 void ThumbV7PILongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
211   const uint8_t Data[] = {
212       0x4f, 0xf6, 0xf4, 0x7c, // P:  movw ip,:lower16:S - (P + (L1-P) + 4)
213       0xc0, 0xf2, 0x00, 0x0c, //     movt ip,:upper16:S - (P + (L1-P) + 4)
214       0xfc, 0x44,             // L1: add  r12, pc
215       0x60, 0x47,             //     bx   r12
216   };
217   uint64_t S = getARMThunkDestVA(Destination);
218   uint64_t P = ThunkSym->getVA() & ~0x1;
219   uint64_t Offset = S - P - 12;
220   memcpy(Buf, Data, sizeof(Data));
221   Target->relocateOne(Buf, R_ARM_THM_MOVW_PREL_NC, Offset);
222   Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_PREL, Offset);
223 }
224 
225 void ThumbV7PILongThunk::addSymbols(ThunkSection &IS) {
226   ThunkSym = addSyntheticLocal(
227       Saver.save("__ThumbV7PILongThunk_" + Destination.getName()), STT_FUNC,
228       Offset | 0x1, size(), &IS);
229   addSyntheticLocal("$t", STT_NOTYPE, Offset, 0, &IS);
230 }
231 
232 bool ThumbV7PILongThunk::isCompatibleWith(uint32_t RelocType) const {
233   // ARM branch relocations can't use BLX
234   return RelocType != R_ARM_JUMP24 && RelocType != R_ARM_PC24 &&
235          RelocType != R_ARM_PLT32;
236 }
237 
238 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
239 void MipsThunk::writeTo(uint8_t *Buf, ThunkSection &) const {
240   uint64_t S = Destination.getVA();
241   write32(Buf, 0x3c190000, Config->Endianness); // lui   $25, %hi(func)
242   write32(Buf + 4, 0x08000000 | (S >> 2), Config->Endianness); // j     func
243   write32(Buf + 8, 0x27390000, Config->Endianness); // addiu $25, $25, %lo(func)
244   write32(Buf + 12, 0x00000000, Config->Endianness); // nop
245   Target->relocateOne(Buf, R_MIPS_HI16, S);
246   Target->relocateOne(Buf + 8, R_MIPS_LO16, S);
247 }
248 
249 void MipsThunk::addSymbols(ThunkSection &IS) {
250   ThunkSym =
251       addSyntheticLocal(Saver.save("__LA25Thunk_" + Destination.getName()),
252                         STT_FUNC, Offset, size(), &IS);
253 }
254 
255 InputSection *MipsThunk::getTargetInputSection() const {
256   auto *DR = dyn_cast<DefinedRegular>(&Destination);
257   return dyn_cast<InputSection>(DR->Section);
258 }
259 
260 // Write microMIPS R2-R5 LA25 thunk code
261 // to call PIC function from the non-PIC one.
262 void MicroMipsThunk::writeTo(uint8_t *Buf, ThunkSection &) const {
263   uint64_t S = Destination.getVA();
264   write16(Buf, 0x41b9, Config->Endianness);       // lui   $25, %hi(func)
265   write16(Buf + 4, 0xd400, Config->Endianness);   // j     func
266   write16(Buf + 8, 0x3339, Config->Endianness);   // addiu $25, $25, %lo(func)
267   write16(Buf + 12, 0x0c00, Config->Endianness);  // nop
268   Target->relocateOne(Buf, R_MICROMIPS_HI16, S);
269   Target->relocateOne(Buf + 4, R_MICROMIPS_26_S1, S);
270   Target->relocateOne(Buf + 8, R_MICROMIPS_LO16, S);
271 }
272 
273 void MicroMipsThunk::addSymbols(ThunkSection &IS) {
274   ThunkSym =
275       addSyntheticLocal(Saver.save("__microLA25Thunk_" + Destination.getName()),
276                         STT_FUNC, Offset, size(), &IS);
277   ThunkSym->StOther |= STO_MIPS_MICROMIPS;
278 }
279 
280 InputSection *MicroMipsThunk::getTargetInputSection() const {
281   auto *DR = dyn_cast<DefinedRegular>(&Destination);
282   return dyn_cast<InputSection>(DR->Section);
283 }
284 
285 // Write microMIPS R6 LA25 thunk code
286 // to call PIC function from the non-PIC one.
287 void MicroMipsR6Thunk::writeTo(uint8_t *Buf, ThunkSection &) const {
288   uint64_t S = Destination.getVA();
289   uint64_t P = ThunkSym->getVA();
290   write16(Buf, 0x1320, Config->Endianness);       // lui   $25, %hi(func)
291   write16(Buf + 4, 0x3339, Config->Endianness);   // addiu $25, $25, %lo(func)
292   write16(Buf + 8, 0x9400, Config->Endianness);   // bc    func
293   Target->relocateOne(Buf, R_MICROMIPS_HI16, S);
294   Target->relocateOne(Buf + 4, R_MICROMIPS_LO16, S);
295   Target->relocateOne(Buf + 8, R_MICROMIPS_PC26_S1, S - P - 12);
296 }
297 
298 void MicroMipsR6Thunk::addSymbols(ThunkSection &IS) {
299   ThunkSym =
300       addSyntheticLocal(Saver.save("__microLA25Thunk_" + Destination.getName()),
301                         STT_FUNC, Offset, size(), &IS);
302   ThunkSym->StOther |= STO_MIPS_MICROMIPS;
303 }
304 
305 InputSection *MicroMipsR6Thunk::getTargetInputSection() const {
306   auto *DR = dyn_cast<DefinedRegular>(&Destination);
307   return dyn_cast<InputSection>(DR->Section);
308 }
309 
310 Thunk::Thunk(const SymbolBody &D) : Destination(D), Offset(0) {}
311 
312 Thunk::~Thunk() = default;
313 
314 // Creates a thunk for Thumb-ARM interworking.
315 static Thunk *addThunkArm(uint32_t Reloc, SymbolBody &S) {
316   // ARM relocations need ARM to Thumb interworking Thunks.
317   // Thumb relocations need Thumb to ARM relocations.
318   // Use position independent Thunks if we require position independent code.
319   switch (Reloc) {
320   case R_ARM_PC24:
321   case R_ARM_PLT32:
322   case R_ARM_JUMP24:
323     if (Config->Pic)
324       return make<ARMV7PILongThunk>(S);
325     return make<ARMV7ABSLongThunk>(S);
326   case R_ARM_THM_JUMP19:
327   case R_ARM_THM_JUMP24:
328     if (Config->Pic)
329       return make<ThumbV7PILongThunk>(S);
330     return make<ThumbV7ABSLongThunk>(S);
331   }
332   fatal("unrecognized relocation type");
333 }
334 
335 static Thunk *addThunkMips(uint32_t Reloc, SymbolBody &S) {
336   if ((S.StOther & STO_MIPS_MICROMIPS) && isMipsR6())
337     return make<MicroMipsR6Thunk>(S);
338   if (S.StOther & STO_MIPS_MICROMIPS)
339     return make<MicroMipsThunk>(S);
340   return make<MipsThunk>(S);
341 }
342 
343 Thunk *addThunk(uint32_t RelocType, SymbolBody &S) {
344   if (Config->EMachine == EM_ARM)
345     return addThunkArm(RelocType, S);
346   else if (Config->EMachine == EM_MIPS)
347     return addThunkMips(RelocType, S);
348   llvm_unreachable("add Thunk only supported for ARM and Mips");
349   return nullptr;
350 }
351 
352 } // end namespace elf
353 } // end namespace lld
354