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 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 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 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 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 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 void ARMV7ABSLongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
113 const uint8_t Data[] = {
114 0x00, 0xc0, 0x00, 0xe3, // movw ip,:lower16:S
115 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S
116 0x1c, 0xff, 0x2f, 0xe1, // bx ip
118 uint64_t S = getARMThunkDestVA(Destination);
119 memcpy(Buf, Data, sizeof(Data));
120 Target->relocateOne(Buf, R_ARM_MOVW_ABS_NC, S);
121 Target->relocateOne(Buf + 4, R_ARM_MOVT_ABS, S);
124 void ARMV7ABSLongThunk::addSymbols(ThunkSection &IS) {
125 ThunkSym = addSyntheticLocal(
126 Saver.save("__ARMv7ABSLongThunk_" + Destination.getName()), STT_FUNC,
127 Offset, size(), &IS);
128 addSyntheticLocal("$a", STT_NOTYPE, Offset, 0, &IS);
131 void ThumbV7ABSLongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
132 const uint8_t Data[] = {
133 0x40, 0xf2, 0x00, 0x0c, // movw ip, :lower16:S
134 0xc0, 0xf2, 0x00, 0x0c, // movt ip, :upper16:S
137 uint64_t S = getARMThunkDestVA(Destination);
138 memcpy(Buf, Data, sizeof(Data));
139 Target->relocateOne(Buf, R_ARM_THM_MOVW_ABS_NC, S);
140 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_ABS, S);
143 void ThumbV7ABSLongThunk::addSymbols(ThunkSection &IS) {
144 ThunkSym = addSyntheticLocal(
145 Saver.save("__Thumbv7ABSLongThunk_" + Destination.getName()), STT_FUNC,
146 Offset, size(), &IS);
147 addSyntheticLocal("$t", STT_NOTYPE, Offset, 0, &IS);
150 void ARMV7PILongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
151 const uint8_t Data[] = {
152 0xf0, 0xcf, 0x0f, 0xe3, // P: movw ip,:lower16:S - (P + (L1-P) +8)
153 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S - (P + (L1-P+4) +8)
154 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
155 0x1c, 0xff, 0x2f, 0xe1, // bx r12
157 uint64_t S = getARMThunkDestVA(Destination);
158 uint64_t P = ThunkSym->getVA();
159 memcpy(Buf, Data, sizeof(Data));
160 Target->relocateOne(Buf, R_ARM_MOVW_PREL_NC, S - P - 16);
161 Target->relocateOne(Buf + 4, R_ARM_MOVT_PREL, S - P - 12);
164 void ARMV7PILongThunk::addSymbols(ThunkSection &IS) {
165 ThunkSym = addSyntheticLocal(
166 Saver.save("__ARMV7PILongThunk_" + Destination.getName()), STT_FUNC,
167 Offset, size(), &IS);
168 addSyntheticLocal("$a", STT_NOTYPE, Offset, 0, &IS);
171 void ThumbV7PILongThunk::writeTo(uint8_t *Buf, ThunkSection &IS) const {
172 const uint8_t Data[] = {
173 0x4f, 0xf6, 0xf4, 0x7c, // P: movw ip,:lower16:S - (P + (L1-P) + 4)
174 0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P+4) + 4)
175 0xfc, 0x44, // L1: add r12, pc
176 0x60, 0x47, // bx r12
178 uint64_t S = getARMThunkDestVA(Destination);
179 uint64_t P = ThunkSym->getVA();
180 memcpy(Buf, Data, sizeof(Data));
181 Target->relocateOne(Buf, R_ARM_THM_MOVW_PREL_NC, S - P - 12);
182 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_PREL, S - P - 8);
185 void ThumbV7PILongThunk::addSymbols(ThunkSection &IS) {
186 ThunkSym = addSyntheticLocal(
187 Saver.save("__ThumbV7PILongThunk_" + Destination.getName()), STT_FUNC,
188 Offset, size(), &IS);
189 addSyntheticLocal("$t", STT_NOTYPE, Offset, 0, &IS);
192 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
193 void MipsThunk::writeTo(uint8_t *Buf, ThunkSection &) const {
194 uint64_t S = this->Destination.getVA();
195 write32(Buf, 0x3c190000, Config->Endianness); // lui $25, %hi(func)
196 write32(Buf + 4, 0x08000000 | (S >> 2), Config->Endianness); // j func
197 write32(Buf + 8, 0x27390000, Config->Endianness); // addiu $25, $25, %lo(func)
198 write32(Buf + 12, 0x00000000, Config->Endianness); // nop
199 Target->relocateOne(Buf, R_MIPS_HI16, S);
200 Target->relocateOne(Buf + 8, R_MIPS_LO16, S);
203 void MipsThunk::addSymbols(ThunkSection &IS) {
205 addSyntheticLocal(Saver.save("__LA25Thunk_" + Destination.getName()),
206 STT_FUNC, Offset, size(), &IS);
209 InputSection *MipsThunk::getTargetInputSection() const {
210 auto *DR = dyn_cast<DefinedRegular>(&Destination);
211 return dyn_cast<InputSection>(DR->Section);
214 Thunk::Thunk(const SymbolBody &D) : Destination(D), Offset(0) {}
216 Thunk::~Thunk() = default;
218 // Creates a thunk for Thumb-ARM interworking.
219 static Thunk *addThunkArm(uint32_t Reloc, SymbolBody &S) {
220 // ARM relocations need ARM to Thumb interworking Thunks.
221 // Thumb relocations need Thumb to ARM relocations.
222 // Use position independent Thunks if we require position independent code.
228 return make<ARMV7PILongThunk>(S);
229 return make<ARMV7ABSLongThunk>(S);
230 case R_ARM_THM_JUMP19:
231 case R_ARM_THM_JUMP24:
233 return make<ThumbV7PILongThunk>(S);
234 return make<ThumbV7ABSLongThunk>(S);
236 fatal("unrecognized relocation type");
239 static Thunk *addThunkMips(SymbolBody &S) {
240 return make<MipsThunk>(S);
243 Thunk *addThunk(uint32_t RelocType, SymbolBody &S) {
244 if (Config->EMachine == EM_ARM)
245 return addThunkArm(RelocType, S);
246 else if (Config->EMachine == EM_MIPS)
247 return addThunkMips(S);
248 llvm_unreachable("add Thunk only supported for ARM and Mips");
252 } // end namespace elf
253 } // end namespace lld