1 //===- Thunks.cpp --------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===---------------------------------------------------------------------===//
10 // This file contains Thunk subclasses.
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.
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.
20 // i386 and x86-64 don't need thunks.
22 //===---------------------------------------------------------------------===//
26 #include "InputSection.h"
27 #include "OutputSections.h"
29 #include "SyntheticSections.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"
42 using namespace llvm::object;
43 using namespace llvm::support::endian;
44 using namespace llvm::ELF;
51 // AArch64 long range Thunks
52 class AArch64ABSLongThunk final : public Thunk {
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;
60 class AArch64ADRPThunk final : public Thunk {
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;
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 {
72 ARMV7ABSLongThunk(Symbol &Dest) : Thunk(Dest) {}
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;
80 class ARMV7PILongThunk final : public Thunk {
82 ARMV7PILongThunk(Symbol &Dest) : Thunk(Dest) {}
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;
90 class ThumbV7ABSLongThunk final : public Thunk {
92 ThumbV7ABSLongThunk(Symbol &Dest) : Thunk(Dest) { Alignment = 2; }
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;
100 class ThumbV7PILongThunk final : public Thunk {
102 ThumbV7PILongThunk(Symbol &Dest) : Thunk(Dest) { Alignment = 2; }
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;
111 class MipsThunk final : public Thunk {
113 MipsThunk(Symbol &Dest) : Thunk(Dest) {}
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;
121 // microMIPS R2-R5 LA25 thunk
122 class MicroMipsThunk final : public Thunk {
124 MicroMipsThunk(Symbol &Dest) : Thunk(Dest) {}
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;
132 // microMIPS R6 LA25 thunk
133 class MicroMipsR6Thunk final : public Thunk {
135 MicroMipsR6Thunk(Symbol &Dest) : Thunk(Dest) {}
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;
143 } // end anonymous namespace
145 // AArch64 long range Thunks
147 static uint64_t getAArch64ThunkDestVA(const Symbol &S) {
148 uint64_t V = S.isInPlt() ? S.getPltVA() : S.getVA();
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,
159 uint64_t S = getAArch64ThunkDestVA(Destination);
160 memcpy(Buf, Data, sizeof(Data));
161 Target->relocateOne(Buf + 8, R_AARCH64_ABS64, S);
164 void AArch64ABSLongThunk::addSymbols(ThunkSection &IS) {
165 ThunkSym = addSyntheticLocal(
166 Saver.save("__AArch64AbsLongThunk_" + Destination.getName()), STT_FUNC,
168 addSyntheticLocal("$x", STT_NOTYPE, Offset, 0, IS);
169 addSyntheticLocal("$d", STT_NOTYPE, Offset + 8, 0, IS);
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
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);
191 void AArch64ADRPThunk::addSymbols(ThunkSection &IS)
193 ThunkSym = addSyntheticLocal(
194 Saver.save("__AArch64ADRPThunk_" + Destination.getName()), STT_FUNC,
196 addSyntheticLocal("$x", STT_NOTYPE, Offset, 0, IS);
200 static uint64_t getARMThunkDestVA(const Symbol &S) {
201 uint64_t V = S.isInPlt() ? S.getPltVA() : S.getVA();
202 return SignExtend64<32>(V);
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
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);
217 void ARMV7ABSLongThunk::addSymbols(ThunkSection &IS) {
218 ThunkSym = addSyntheticLocal(
219 Saver.save("__ARMv7ABSLongThunk_" + Destination.getName()), STT_FUNC,
221 addSyntheticLocal("$a", STT_NOTYPE, Offset, 0, IS);
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;
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
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);
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);
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;
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
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);
268 void ARMV7PILongThunk::addSymbols(ThunkSection &IS) {
269 ThunkSym = addSyntheticLocal(
270 Saver.save("__ARMV7PILongThunk_" + Destination.getName()), STT_FUNC,
272 addSyntheticLocal("$a", STT_NOTYPE, Offset, 0, IS);
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;
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
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);
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);
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;
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);
318 void MipsThunk::addSymbols(ThunkSection &IS) {
320 addSyntheticLocal(Saver.save("__LA25Thunk_" + Destination.getName()),
321 STT_FUNC, Offset, size(), IS);
324 InputSection *MipsThunk::getTargetInputSection() const {
325 auto &DR = cast<Defined>(Destination);
326 return dyn_cast<InputSection>(DR.Section);
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);
342 void MicroMipsThunk::addSymbols(ThunkSection &IS) {
344 addSyntheticLocal(Saver.save("__microLA25Thunk_" + Destination.getName()),
345 STT_FUNC, Offset, size(), IS);
346 ThunkSym->StOther |= STO_MIPS_MICROMIPS;
349 InputSection *MicroMipsThunk::getTargetInputSection() const {
350 auto &DR = cast<Defined>(Destination);
351 return dyn_cast<InputSection>(DR.Section);
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);
367 void MicroMipsR6Thunk::addSymbols(ThunkSection &IS) {
369 addSyntheticLocal(Saver.save("__microLA25Thunk_" + Destination.getName()),
370 STT_FUNC, Offset, size(), IS);
371 ThunkSym->StOther |= STO_MIPS_MICROMIPS;
374 InputSection *MicroMipsR6Thunk::getTargetInputSection() const {
375 auto &DR = cast<Defined>(Destination);
376 return dyn_cast<InputSection>(DR.Section);
379 Thunk::Thunk(Symbol &D) : Destination(D), Offset(0) {}
381 Thunk::~Thunk() = default;
383 static Thunk *addThunkAArch64(RelType Type, Symbol &S) {
384 if (Type != R_AARCH64_CALL26 && Type != R_AARCH64_JUMP26)
385 fatal("unrecognized relocation type");
387 return make<AArch64ADRPThunk>(S);
388 return make<AArch64ABSLongThunk>(S);
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.
402 return make<ARMV7PILongThunk>(S);
403 return make<ARMV7ABSLongThunk>(S);
404 case R_ARM_THM_JUMP19:
405 case R_ARM_THM_JUMP24:
408 return make<ThumbV7PILongThunk>(S);
409 return make<ThumbV7ABSLongThunk>(S);
411 fatal("unrecognized relocation type");
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);
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");
433 } // end namespace elf
434 } // end namespace lld