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"
31 #include "SyntheticSections.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/ELF.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 // Specific ARM Thunk implementations. The naming convention is:
52 // Source State, TargetState, Target Requirement, ABS or PI, Range
53 template <class ELFT> class ARMV7ABSLongThunk final : public Thunk {
55 ARMV7ABSLongThunk(const SymbolBody &Dest) : Thunk(Dest) {}
57 uint32_t size() const override { return 12; }
58 void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
59 void addSymbols(ThunkSection &IS) override;
62 template <class ELFT> class ARMV7PILongThunk final : public Thunk {
64 ARMV7PILongThunk(const SymbolBody &Dest) : Thunk(Dest) {}
66 uint32_t size() const override { return 16; }
67 void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
68 void addSymbols(ThunkSection &IS) override;
71 template <class ELFT> class ThumbV7ABSLongThunk final : public Thunk {
73 ThumbV7ABSLongThunk(const SymbolBody &Dest) : Thunk(Dest) {
77 uint32_t size() const override { return 10; }
78 void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
79 void addSymbols(ThunkSection &IS) override;
82 template <class ELFT> class ThumbV7PILongThunk final : public Thunk {
84 ThumbV7PILongThunk(const SymbolBody &Dest) : Thunk(Dest) {
88 uint32_t size() const override { return 12; }
89 void writeTo(uint8_t *Buf, ThunkSection &IS) const override;
90 void addSymbols(ThunkSection &IS) override;
94 template <class ELFT> class MipsThunk final : public Thunk {
96 MipsThunk(const SymbolBody &Dest) : Thunk(Dest) {}
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;
104 } // end anonymous namespace
107 static uint64_t getARMThunkDestVA(const SymbolBody &S) {
108 uint64_t V = S.isInPlt() ? S.getPltVA() : S.getVA();
109 return SignExtend64<32>(V);
112 template <class ELFT>
113 void ARMV7ABSLongThunk<ELFT>::writeTo(uint8_t *Buf, ThunkSection &IS) 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(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 ARMV7ABSLongThunk<ELFT>::addSymbols(ThunkSection &IS) {
127 this->ThunkSym = addSyntheticLocal<ELFT>(
128 Saver.save("__ARMv7ABSLongThunk_" + this->Destination.getName()),
129 STT_FUNC, this->Offset, size(), &IS);
130 addSyntheticLocal<ELFT>("$a", STT_NOTYPE, this->Offset, 0, &IS);
133 template <class ELFT>
134 void ThumbV7ABSLongThunk<ELFT>::writeTo(uint8_t *Buf, ThunkSection &IS) const {
135 const uint8_t Data[] = {
136 0x40, 0xf2, 0x00, 0x0c, // movw ip, :lower16:S
137 0xc0, 0xf2, 0x00, 0x0c, // movt ip, :upper16:S
140 uint64_t S = getARMThunkDestVA(this->Destination);
141 memcpy(Buf, Data, sizeof(Data));
142 Target->relocateOne(Buf, R_ARM_THM_MOVW_ABS_NC, S);
143 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_ABS, S);
146 template <class ELFT>
147 void ThumbV7ABSLongThunk<ELFT>::addSymbols(ThunkSection &IS) {
148 this->ThunkSym = addSyntheticLocal<ELFT>(
149 Saver.save("__Thumbv7ABSLongThunk_" + this->Destination.getName()),
150 STT_FUNC, this->Offset, size(), &IS);
151 addSyntheticLocal<ELFT>("$t", STT_NOTYPE, this->Offset, 0, &IS);
154 template <class ELFT>
155 void ARMV7PILongThunk<ELFT>::writeTo(uint8_t *Buf, ThunkSection &IS) const {
156 const uint8_t Data[] = {
157 0xf0, 0xcf, 0x0f, 0xe3, // P: movw ip,:lower16:S - (P + (L1-P) +8)
158 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S - (P + (L1-P+4) +8)
159 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
160 0x1c, 0xff, 0x2f, 0xe1, // bx r12
162 uint64_t S = getARMThunkDestVA(this->Destination);
163 uint64_t P = this->ThunkSym->getVA();
164 memcpy(Buf, Data, sizeof(Data));
165 Target->relocateOne(Buf, R_ARM_MOVW_PREL_NC, S - P - 16);
166 Target->relocateOne(Buf + 4, R_ARM_MOVT_PREL, S - P - 12);
169 template <class ELFT>
170 void ARMV7PILongThunk<ELFT>::addSymbols(ThunkSection &IS) {
171 this->ThunkSym = addSyntheticLocal<ELFT>(
172 Saver.save("__ARMV7PILongThunk_" + this->Destination.getName()), STT_FUNC,
173 this->Offset, size(), &IS);
174 addSyntheticLocal<ELFT>("$a", STT_NOTYPE, this->Offset, 0, &IS);
177 template <class ELFT>
178 void ThumbV7PILongThunk<ELFT>::writeTo(uint8_t *Buf, ThunkSection &IS) const {
179 const uint8_t Data[] = {
180 0x4f, 0xf6, 0xf4, 0x7c, // P: movw ip,:lower16:S - (P + (L1-P) + 4)
181 0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P+4) + 4)
182 0xfc, 0x44, // L1: add r12, pc
183 0x60, 0x47, // bx r12
185 uint64_t S = getARMThunkDestVA(this->Destination);
186 uint64_t P = this->ThunkSym->getVA();
187 memcpy(Buf, Data, sizeof(Data));
188 Target->relocateOne(Buf, R_ARM_THM_MOVW_PREL_NC, S - P - 12);
189 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_PREL, S - P - 8);
192 template <class ELFT>
193 void ThumbV7PILongThunk<ELFT>::addSymbols(ThunkSection &IS) {
194 this->ThunkSym = addSyntheticLocal<ELFT>(
195 Saver.save("__ThumbV7PILongThunk_" + this->Destination.getName()),
196 STT_FUNC, this->Offset, size(), &IS);
197 addSyntheticLocal<ELFT>("$t", STT_NOTYPE, this->Offset, 0, &IS);
200 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
201 template <class ELFT>
202 void MipsThunk<ELFT>::writeTo(uint8_t *Buf, ThunkSection &) const {
203 const endianness E = ELFT::TargetEndianness;
205 uint64_t S = this->Destination.getVA();
206 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func)
207 write32<E>(Buf + 4, 0x08000000 | (S >> 2)); // j func
208 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
209 write32<E>(Buf + 12, 0x00000000); // nop
210 Target->relocateOne(Buf, R_MIPS_HI16, S);
211 Target->relocateOne(Buf + 8, R_MIPS_LO16, S);
214 template <class ELFT> void MipsThunk<ELFT>::addSymbols(ThunkSection &IS) {
215 this->ThunkSym = addSyntheticLocal<ELFT>(
216 Saver.save("__LA25Thunk_" + this->Destination.getName()), STT_FUNC,
217 this->Offset, size(), &IS);
220 template <class ELFT>
221 InputSection *MipsThunk<ELFT>::getTargetInputSection() const {
222 auto *DR = dyn_cast<DefinedRegular>(&this->Destination);
223 return dyn_cast<InputSection>(DR->Section);
226 Thunk::Thunk(const SymbolBody &D) : Destination(D), Offset(0) {}
228 Thunk::~Thunk() = default;
230 // Creates a thunk for Thumb-ARM interworking.
231 template <class ELFT> static Thunk *addThunkArm(uint32_t Reloc, SymbolBody &S) {
232 // ARM relocations need ARM to Thumb interworking Thunks.
233 // Thumb relocations need Thumb to ARM relocations.
234 // Use position independent Thunks if we require position independent code.
240 return make<ARMV7PILongThunk<ELFT>>(S);
241 return make<ARMV7ABSLongThunk<ELFT>>(S);
242 case R_ARM_THM_JUMP19:
243 case R_ARM_THM_JUMP24:
245 return make<ThumbV7PILongThunk<ELFT>>(S);
246 return make<ThumbV7ABSLongThunk<ELFT>>(S);
248 fatal("unrecognized relocation type");
251 template <class ELFT> static Thunk *addThunkMips(SymbolBody &S) {
252 return make<MipsThunk<ELFT>>(S);
255 template <class ELFT> Thunk *addThunk(uint32_t RelocType, SymbolBody &S) {
256 if (Config->EMachine == EM_ARM)
257 return addThunkArm<ELFT>(RelocType, S);
258 else if (Config->EMachine == EM_MIPS)
259 return addThunkMips<ELFT>(S);
260 llvm_unreachable("add Thunk only supported for ARM and Mips");
264 template Thunk *addThunk<ELF32LE>(uint32_t, SymbolBody &);
265 template Thunk *addThunk<ELF32BE>(uint32_t, SymbolBody &);
266 template Thunk *addThunk<ELF64LE>(uint32_t, SymbolBody &);
267 template Thunk *addThunk<ELF64BE>(uint32_t, SymbolBody &);
268 } // end namespace elf
269 } // end namespace lld