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 //===---------------------------------------------------------------------===//
27 #include "InputSection.h"
29 #include "OutputSections.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/ELF.h"
34 #include "llvm/Support/Endian.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/MathExtras.h"
41 using namespace llvm::object;
42 using namespace llvm::support::endian;
43 using namespace llvm::ELF;
50 // Specific ARM Thunk implementations. The naming convention is:
51 // Source State, TargetState, Target Requirement, ABS or PI, Range
53 class ARMToThumbV7ABSLongThunk final : public Thunk<ELFT> {
55 ARMToThumbV7ABSLongThunk(const SymbolBody &Dest,
56 const InputSection<ELFT> &Owner)
57 : Thunk<ELFT>(Dest, Owner) {}
59 uint32_t size() const override { return 12; }
60 void writeTo(uint8_t *Buf) const override;
63 template <class ELFT> class ARMToThumbV7PILongThunk final : public Thunk<ELFT> {
65 ARMToThumbV7PILongThunk(const SymbolBody &Dest,
66 const InputSection<ELFT> &Owner)
67 : Thunk<ELFT>(Dest, Owner) {}
69 uint32_t size() const override { return 16; }
70 void writeTo(uint8_t *Buf) const override;
74 class ThumbToARMV7ABSLongThunk final : public Thunk<ELFT> {
76 ThumbToARMV7ABSLongThunk(const SymbolBody &Dest,
77 const InputSection<ELFT> &Owner)
78 : Thunk<ELFT>(Dest, Owner) {}
80 uint32_t size() const override { return 10; }
81 void writeTo(uint8_t *Buf) const override;
84 template <class ELFT> class ThumbToARMV7PILongThunk final : public Thunk<ELFT> {
86 ThumbToARMV7PILongThunk(const SymbolBody &Dest,
87 const InputSection<ELFT> &Owner)
88 : Thunk<ELFT>(Dest, Owner) {}
90 uint32_t size() const override { return 12; }
91 void writeTo(uint8_t *Buf) const override;
95 template <class ELFT> class MipsThunk final : public Thunk<ELFT> {
97 MipsThunk(const SymbolBody &Dest, const InputSection<ELFT> &Owner)
98 : Thunk<ELFT>(Dest, Owner) {}
100 uint32_t size() const override { return 16; }
101 void writeTo(uint8_t *Buf) const override;
104 } // end anonymous namespace
107 template <class ELFT> static uint64_t getARMThunkDestVA(const SymbolBody &S) {
108 uint64_t V = S.isInPlt() ? S.getPltVA<ELFT>() : S.getVA<ELFT>();
109 return SignExtend64<32>(V);
112 template <class ELFT>
113 void ARMToThumbV7ABSLongThunk<ELFT>::writeTo(uint8_t *Buf) const {
114 const uint8_t Data[] = {
115 0x00, 0xc0, 0x00, 0xe3, // movw ip,:lower16:S
116 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S
117 0x1c, 0xff, 0x2f, 0xe1, // bx ip
119 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination);
120 memcpy(Buf, Data, sizeof(Data));
121 Target->relocateOne(Buf, R_ARM_MOVW_ABS_NC, S);
122 Target->relocateOne(Buf + 4, R_ARM_MOVT_ABS, S);
125 template <class ELFT>
126 void ThumbToARMV7ABSLongThunk<ELFT>::writeTo(uint8_t *Buf) const {
127 const uint8_t Data[] = {
128 0x40, 0xf2, 0x00, 0x0c, // movw ip, :lower16:S
129 0xc0, 0xf2, 0x00, 0x0c, // movt ip, :upper16:S
132 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination);
133 memcpy(Buf, Data, sizeof(Data));
134 Target->relocateOne(Buf, R_ARM_THM_MOVW_ABS_NC, S);
135 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_ABS, S);
138 template <class ELFT>
139 void ARMToThumbV7PILongThunk<ELFT>::writeTo(uint8_t *Buf) const {
140 const uint8_t Data[] = {
141 0xf0, 0xcf, 0x0f, 0xe3, // P: movw ip,:lower16:S - (P + (L1-P) +8)
142 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S - (P + (L1-P+4) +8)
143 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
144 0x1c, 0xff, 0x2f, 0xe1, // bx r12
146 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination);
147 uint64_t P = this->getVA();
148 memcpy(Buf, Data, sizeof(Data));
149 Target->relocateOne(Buf, R_ARM_MOVW_PREL_NC, S - P - 16);
150 Target->relocateOne(Buf + 4, R_ARM_MOVT_PREL, S - P - 12);
153 template <class ELFT>
154 void ThumbToARMV7PILongThunk<ELFT>::writeTo(uint8_t *Buf) const {
155 const uint8_t Data[] = {
156 0x4f, 0xf6, 0xf4, 0x7c, // P: movw ip,:lower16:S - (P + (L1-P) + 4)
157 0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P+4) + 4)
158 0xfc, 0x44, // L1: add r12, pc
159 0x60, 0x47, // bx r12
161 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination);
162 uint64_t P = this->getVA();
163 memcpy(Buf, Data, sizeof(Data));
164 Target->relocateOne(Buf, R_ARM_THM_MOVW_PREL_NC, S - P - 12);
165 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_PREL, S - P - 8);
168 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
169 template <class ELFT> void MipsThunk<ELFT>::writeTo(uint8_t *Buf) const {
170 const endianness E = ELFT::TargetEndianness;
172 uint64_t S = this->Destination.template getVA<ELFT>();
173 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func)
174 write32<E>(Buf + 4, 0x08000000 | (S >> 2)); // j func
175 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
176 write32<E>(Buf + 12, 0x00000000); // nop
177 Target->relocateOne(Buf, R_MIPS_HI16, S);
178 Target->relocateOne(Buf + 8, R_MIPS_LO16, S);
181 template <class ELFT>
182 Thunk<ELFT>::Thunk(const SymbolBody &D, const InputSection<ELFT> &O)
183 : Destination(D), Owner(O), Offset(O.getThunkOff() + O.getThunksSize()) {}
185 template <class ELFT> typename ELFT::uint Thunk<ELFT>::getVA() const {
186 return Owner.OutSec->Addr + Owner.OutSecOff + Offset;
189 template <class ELFT> Thunk<ELFT>::~Thunk() = default;
191 // Creates a thunk for Thumb-ARM interworking.
192 template <class ELFT>
193 static Thunk<ELFT> *createThunkArm(uint32_t Reloc, SymbolBody &S,
194 InputSection<ELFT> &IS) {
195 // ARM relocations need ARM to Thumb interworking Thunks.
196 // Thumb relocations need Thumb to ARM relocations.
197 // Use position independent Thunks if we require position independent code.
203 return new (BAlloc) ARMToThumbV7PILongThunk<ELFT>(S, IS);
204 return new (BAlloc) ARMToThumbV7ABSLongThunk<ELFT>(S, IS);
205 case R_ARM_THM_JUMP19:
206 case R_ARM_THM_JUMP24:
208 return new (BAlloc) ThumbToARMV7PILongThunk<ELFT>(S, IS);
209 return new (BAlloc) ThumbToARMV7ABSLongThunk<ELFT>(S, IS);
211 fatal("unrecognized relocation type");
214 template <class ELFT>
215 static void addThunkARM(uint32_t Reloc, SymbolBody &S, InputSection<ELFT> &IS) {
216 // Only one Thunk supported per symbol.
217 if (S.hasThunk<ELFT>())
220 // ARM Thunks are added to the same InputSection as the relocation. This
221 // isn't strictly necessary but it makes it more likely that a limited range
222 // branch can reach the Thunk, and it makes Thunks to the PLT section easier
223 Thunk<ELFT> *T = createThunkArm(Reloc, S, IS);
225 if (auto *Sym = dyn_cast<DefinedRegular<ELFT>>(&S))
227 else if (auto *Sym = dyn_cast<SharedSymbol<ELFT>>(&S))
230 fatal("symbol not DefinedRegular or Shared");
233 template <class ELFT>
234 static void addThunkMips(uint32_t RelocType, SymbolBody &S,
235 InputSection<ELFT> &IS) {
236 // Only one Thunk supported per symbol.
237 if (S.hasThunk<ELFT>())
240 // Mips Thunks are added to the InputSection defining S.
241 auto *R = cast<DefinedRegular<ELFT>>(&S);
242 auto *Sec = cast<InputSection<ELFT>>(R->Section);
243 auto *T = new (BAlloc) MipsThunk<ELFT>(S, *Sec);
248 template <class ELFT>
249 void addThunk(uint32_t RelocType, SymbolBody &S, InputSection<ELFT> &IS) {
250 if (Config->EMachine == EM_ARM)
251 addThunkARM<ELFT>(RelocType, S, IS);
252 else if (Config->EMachine == EM_MIPS)
253 addThunkMips<ELFT>(RelocType, S, IS);
255 llvm_unreachable("add Thunk only supported for ARM and Mips");
258 template void addThunk<ELF32LE>(uint32_t, SymbolBody &,
259 InputSection<ELF32LE> &);
260 template void addThunk<ELF32BE>(uint32_t, SymbolBody &,
261 InputSection<ELF32BE> &);
262 template void addThunk<ELF64LE>(uint32_t, SymbolBody &,
263 InputSection<ELF64LE> &);
264 template void addThunk<ELF64BE>(uint32_t, SymbolBody &,
265 InputSection<ELF64BE> &);
267 template class Thunk<ELF32LE>;
268 template class Thunk<ELF32BE>;
269 template class Thunk<ELF64LE>;
270 template class Thunk<ELF64BE>;
272 } // end namespace elf
273 } // end namespace lld