xref: /llvm-project-15.0.7/lld/ELF/Thunks.cpp (revision f2d571c8)
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) {
76     alignment = 2;
77   }
78 
79   uint32_t size() const override { return 10; }
80   void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
81   void addSymbols(ThunkSection &IS) override;
82   bool isCompatibleWith(uint32_t RelocType) const override;
83 };
84 
85 class ThumbV7PILongThunk final : public Thunk {
86 public:
87   ThumbV7PILongThunk(const SymbolBody &Dest) : Thunk(Dest) {
88     alignment = 2;
89   }
90 
91   uint32_t size() const override { return 12; }
92   void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
93   void addSymbols(ThunkSection &IS) override;
94   bool isCompatibleWith(uint32_t RelocType) const override;
95 };
96 
97 // MIPS LA25 thunk
98 class MipsThunk final : public Thunk {
99 public:
100   MipsThunk(const SymbolBody &Dest) : Thunk(Dest) {}
101 
102   uint32_t size() const override { return 16; }
103   void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
104   void addSymbols(ThunkSection &IS) override;
105   InputSection *getTargetInputSection() const override;
106 };
107 
108 } // end anonymous namespace
109 
110 // ARM Target Thunks
111 static uint64_t getARMThunkDestVA(const SymbolBody &S) {
112   uint64_t V = S.isInPlt() ? S.getPltVA() : S.getVA();
113   return SignExtend64<32>(V);
114 }
115 
116 void ARMV7ABSLongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
117   const uint8_t Data[] = {
118       0x00, 0xc0, 0x00, 0xe3, // movw         ip,:lower16:S
119       0x00, 0xc0, 0x40, 0xe3, // movt         ip,:upper16:S
120       0x1c, 0xff, 0x2f, 0xe1, // bx   ip
121   };
122   uint64_t S = getARMThunkDestVA(Destination);
123   memcpy(Buf, Data, sizeof(Data));
124   Target->relocateOne(Buf, R_ARM_MOVW_ABS_NC, S);
125   Target->relocateOne(Buf + 4, R_ARM_MOVT_ABS, S);
126 }
127 
128 void ARMV7ABSLongThunk::addSymbols(ThunkSection &IS) {
129   ThunkSym = addSyntheticLocal(
130       Saver.save("__ARMv7ABSLongThunk_" + Destination.getName()), STT_FUNC,
131       Offset, size(), &IS);
132   addSyntheticLocal("$a", STT_NOTYPE, Offset, 0, &IS);
133 }
134 
135 bool ARMV7ABSLongThunk::isCompatibleWith(uint32_t RelocType) const {
136   // Thumb branch relocations can't use BLX
137   return RelocType != R_ARM_THM_JUMP19 && RelocType != R_ARM_THM_JUMP24;
138 }
139 
140 void ThumbV7ABSLongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
141   const uint8_t Data[] = {
142       0x40, 0xf2, 0x00, 0x0c, // movw         ip, :lower16:S
143       0xc0, 0xf2, 0x00, 0x0c, // movt         ip, :upper16:S
144       0x60, 0x47,             // bx   ip
145   };
146   uint64_t S = getARMThunkDestVA(Destination);
147   memcpy(Buf, Data, sizeof(Data));
148   Target->relocateOne(Buf, R_ARM_THM_MOVW_ABS_NC, S);
149   Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_ABS, S);
150 }
151 
152 void ThumbV7ABSLongThunk::addSymbols(ThunkSection &IS) {
153   ThunkSym = addSyntheticLocal(
154       Saver.save("__Thumbv7ABSLongThunk_" + Destination.getName()), STT_FUNC,
155       Offset | 0x1, size(), &IS);
156   addSyntheticLocal("$t", STT_NOTYPE, Offset, 0, &IS);
157 }
158 
159 bool ThumbV7ABSLongThunk::isCompatibleWith(uint32_t RelocType) const {
160   // ARM branch relocations can't use BLX
161   return RelocType != R_ARM_JUMP24 && RelocType != R_ARM_PC24 &&
162          RelocType != R_ARM_PLT32;
163 }
164 
165 void ARMV7PILongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
166   const uint8_t Data[] = {
167       0xf0, 0xcf, 0x0f, 0xe3, // P:  movw ip,:lower16:S - (P + (L1-P) +8)
168       0x00, 0xc0, 0x40, 0xe3, //     movt ip,:upper16:S - (P + (L1-P+4) +8)
169       0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
170       0x1c, 0xff, 0x2f, 0xe1, //     bx r12
171   };
172   uint64_t S = getARMThunkDestVA(Destination);
173   uint64_t P = ThunkSym->getVA();
174   memcpy(Buf, Data, sizeof(Data));
175   Target->relocateOne(Buf, R_ARM_MOVW_PREL_NC, S - P - 16);
176   Target->relocateOne(Buf + 4, R_ARM_MOVT_PREL, S - P - 12);
177 }
178 
179 void ARMV7PILongThunk::addSymbols(ThunkSection &IS) {
180   ThunkSym = addSyntheticLocal(
181       Saver.save("__ARMV7PILongThunk_" + Destination.getName()), STT_FUNC,
182       Offset, size(), &IS);
183   addSyntheticLocal("$a", STT_NOTYPE, Offset, 0, &IS);
184 }
185 
186 bool ARMV7PILongThunk::isCompatibleWith(uint32_t RelocType) const {
187   // Thumb branch relocations can't use BLX
188   return RelocType != R_ARM_THM_JUMP19 && RelocType != R_ARM_THM_JUMP24;
189 }
190 
191 void ThumbV7PILongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
192   const uint8_t Data[] = {
193       0x4f, 0xf6, 0xf4, 0x7c, // P:  movw ip,:lower16:S - (P + (L1-P) + 4)
194       0xc0, 0xf2, 0x00, 0x0c, //     movt ip,:upper16:S - (P + (L1-P+4) + 4)
195       0xfc, 0x44,             // L1: add  r12, pc
196       0x60, 0x47,             //     bx   r12
197   };
198   uint64_t S = getARMThunkDestVA(Destination);
199   uint64_t P = ThunkSym->getVA() & ~0x1;
200   memcpy(Buf, Data, sizeof(Data));
201   Target->relocateOne(Buf, R_ARM_THM_MOVW_PREL_NC, S - P - 12);
202   Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_PREL, S - P - 8);
203 }
204 
205 void ThumbV7PILongThunk::addSymbols(ThunkSection &IS) {
206   ThunkSym = addSyntheticLocal(
207       Saver.save("__ThumbV7PILongThunk_" + Destination.getName()), STT_FUNC,
208       Offset | 0x1, size(), &IS);
209   addSyntheticLocal("$t", STT_NOTYPE, Offset, 0, &IS);
210 }
211 
212 bool ThumbV7PILongThunk::isCompatibleWith(uint32_t RelocType) const {
213   // ARM branch relocations can't use BLX
214   return RelocType != R_ARM_JUMP24 && RelocType != R_ARM_PC24 &&
215          RelocType != R_ARM_PLT32;
216 }
217 
218 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
219 void MipsThunk::writeTo(uint8_t *Buf, ThunkSection &) const {
220   uint64_t S = Destination.getVA();
221   write32(Buf, 0x3c190000, Config->Endianness);                // lui   $25, %hi(func)
222   write32(Buf + 4, 0x08000000 | (S >> 2), Config->Endianness); // j     func
223   write32(Buf + 8, 0x27390000, Config->Endianness);            // addiu $25, $25, %lo(func)
224   write32(Buf + 12, 0x00000000, Config->Endianness);           // nop
225   Target->relocateOne(Buf, R_MIPS_HI16, S);
226   Target->relocateOne(Buf + 8, R_MIPS_LO16, S);
227 }
228 
229 void MipsThunk::addSymbols(ThunkSection &IS) {
230   ThunkSym =
231       addSyntheticLocal(Saver.save("__LA25Thunk_" + Destination.getName()),
232                         STT_FUNC, Offset, size(), &IS);
233 }
234 
235 InputSection *MipsThunk::getTargetInputSection() const {
236   auto *DR = dyn_cast<DefinedRegular>(&Destination);
237   return dyn_cast<InputSection>(DR->Section);
238 }
239 
240 Thunk::Thunk(const SymbolBody &D) : Destination(D), Offset(0) {}
241 
242 Thunk::~Thunk() = default;
243 
244 // Creates a thunk for Thumb-ARM interworking.
245 static Thunk *addThunkArm(uint32_t Reloc, SymbolBody &S) {
246   // ARM relocations need ARM to Thumb interworking Thunks.
247   // Thumb relocations need Thumb to ARM relocations.
248   // Use position independent Thunks if we require position independent code.
249   switch (Reloc) {
250   case R_ARM_PC24:
251   case R_ARM_PLT32:
252   case R_ARM_JUMP24:
253     if (Config->Pic)
254       return make<ARMV7PILongThunk>(S);
255     return make<ARMV7ABSLongThunk>(S);
256   case R_ARM_THM_JUMP19:
257   case R_ARM_THM_JUMP24:
258     if (Config->Pic)
259       return make<ThumbV7PILongThunk>(S);
260     return make<ThumbV7ABSLongThunk>(S);
261   }
262   fatal("unrecognized relocation type");
263 }
264 
265 static Thunk *addThunkMips(SymbolBody &S) {
266   return make<MipsThunk>(S);
267 }
268 
269 Thunk *addThunk(uint32_t RelocType, SymbolBody &S) {
270   if (Config->EMachine == EM_ARM)
271     return addThunkArm(RelocType, S);
272   else if (Config->EMachine == EM_MIPS)
273     return addThunkMips(S);
274   llvm_unreachable("add Thunk only supported for ARM and Mips");
275   return nullptr;
276 }
277 
278 } // end namespace elf
279 } // end namespace lld
280