blob: 50b05c777aec47b76f7285f69e70a51c0e4c430e [file] [log] [blame]
Peter Smithfb05cd92016-07-08 16:10:27 +00001//===- 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//
Rui Ueyama89c37622016-07-09 23:16:00 +000010// This file contains Thunk subclasses.
Peter Smithfb05cd92016-07-08 16:10:27 +000011//
Rui Ueyama89c37622016-07-09 23:16:00 +000012// 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//
Peter Smithfb05cd92016-07-08 16:10:27 +000022//===---------------------------------------------------------------------===//
23
24#include "Thunks.h"
25#include "Error.h"
26#include "InputFiles.h"
27#include "InputSection.h"
28#include "OutputSections.h"
29#include "Symbols.h"
30#include "Target.h"
Rui Ueyama8b8d0052016-07-08 17:58:54 +000031#include "llvm/Support/Allocator.h"
Peter Smithfb05cd92016-07-08 16:10:27 +000032
33#include "llvm/Object/ELF.h"
34#include "llvm/Support/ELF.h"
35#include "llvm/Support/Endian.h"
36
37using namespace llvm;
38using namespace llvm::object;
39using namespace llvm::support::endian;
40using namespace llvm::ELF;
41
42namespace lld {
43namespace elf {
44
Rui Ueyama3d2bbb12016-07-09 22:52:30 +000045namespace {
46// Specific ARM Thunk implementations. The naming convention is:
47// Source State, TargetState, Target Requirement, ABS or PI, Range
48template <class ELFT>
49class ARMToThumbV7ABSLongThunk final : public Thunk<ELFT> {
50public:
51 ARMToThumbV7ABSLongThunk(const SymbolBody &Dest,
52 const InputSection<ELFT> &Owner)
53 : Thunk<ELFT>(Dest, Owner) {}
54
55 uint32_t size() const override { return 12; }
56 void writeTo(uint8_t *Buf) const override;
57};
58
59template <class ELFT> class ARMToThumbV7PILongThunk final : public Thunk<ELFT> {
60public:
61 ARMToThumbV7PILongThunk(const SymbolBody &Dest,
62 const InputSection<ELFT> &Owner)
63 : Thunk<ELFT>(Dest, Owner) {}
64
65 uint32_t size() const override { return 16; }
66 void writeTo(uint8_t *Buf) const override;
67};
68
69template <class ELFT>
70class ThumbToARMV7ABSLongThunk final : public Thunk<ELFT> {
71public:
72 ThumbToARMV7ABSLongThunk(const SymbolBody &Dest,
73 const InputSection<ELFT> &Owner)
74 : Thunk<ELFT>(Dest, Owner) {}
75
76 uint32_t size() const override { return 10; }
77 void writeTo(uint8_t *Buf) const override;
78};
79
80template <class ELFT> class ThumbToARMV7PILongThunk final : public Thunk<ELFT> {
81public:
82 ThumbToARMV7PILongThunk(const SymbolBody &Dest,
83 const InputSection<ELFT> &Owner)
84 : Thunk<ELFT>(Dest, Owner) {}
85
86 uint32_t size() const override { return 12; }
87 void writeTo(uint8_t *Buf) const override;
88};
89
Rui Ueyamae2efadc2016-07-09 22:52:32 +000090// MIPS LA25 thunk
Rui Ueyama3d2bbb12016-07-09 22:52:30 +000091template <class ELFT> class MipsThunk final : public Thunk<ELFT> {
92public:
93 MipsThunk(const SymbolBody &Dest, const InputSection<ELFT> &Owner)
94 : Thunk<ELFT>(Dest, Owner) {}
95
96 uint32_t size() const override { return 16; }
97 void writeTo(uint8_t *Buf) const override;
98};
99} // anonymous namespace
100
101// ARM Target Thunks
102template <class ELFT> static uint64_t getARMThunkDestVA(const SymbolBody &S) {
Rafael Espindola3a9eef12016-09-01 13:52:52 +0000103 uint64_t V = S.isInPlt() ? S.getPltVA<ELFT>() : S.getVA<ELFT>();
104 return SignExtend64<32>(V);
Rui Ueyama3d2bbb12016-07-09 22:52:30 +0000105}
106
107template <class ELFT>
108void ARMToThumbV7ABSLongThunk<ELFT>::writeTo(uint8_t *Buf) const {
109 const uint8_t Data[] = {
110 0x00, 0xc0, 0x00, 0xe3, // movw ip,:lower16:S
111 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S
112 0x1c, 0xff, 0x2f, 0xe1, // bx ip
113 };
114 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination);
115 memcpy(Buf, Data, sizeof(Data));
116 Target->relocateOne(Buf, R_ARM_MOVW_ABS_NC, S);
117 Target->relocateOne(Buf + 4, R_ARM_MOVT_ABS, S);
118}
119
120template <class ELFT>
121void ThumbToARMV7ABSLongThunk<ELFT>::writeTo(uint8_t *Buf) const {
122 const uint8_t Data[] = {
123 0x40, 0xf2, 0x00, 0x0c, // movw ip, :lower16:S
124 0xc0, 0xf2, 0x00, 0x0c, // movt ip, :upper16:S
125 0x60, 0x47, // bx ip
126 };
127 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination);
128 memcpy(Buf, Data, sizeof(Data));
129 Target->relocateOne(Buf, R_ARM_THM_MOVW_ABS_NC, S);
130 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_ABS, S);
131}
132
133template <class ELFT>
134void ARMToThumbV7PILongThunk<ELFT>::writeTo(uint8_t *Buf) const {
135 const uint8_t Data[] = {
136 0xf0, 0xcf, 0x0f, 0xe3, // P: movw ip,:lower16:S - (P + (L1-P) +8)
137 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S - (P + (L1-P+4) +8)
138 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
139 0x1c, 0xff, 0x2f, 0xe1, // bx r12
140 };
141 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination);
142 uint64_t P = this->getVA();
143 memcpy(Buf, Data, sizeof(Data));
144 Target->relocateOne(Buf, R_ARM_MOVW_PREL_NC, S - P - 16);
145 Target->relocateOne(Buf + 4, R_ARM_MOVT_PREL, S - P - 12);
146}
147
148template <class ELFT>
149void ThumbToARMV7PILongThunk<ELFT>::writeTo(uint8_t *Buf) const {
150 const uint8_t Data[] = {
151 0x4f, 0xf6, 0xf4, 0x7c, // P: movw ip,:lower16:S - (P + (L1-P) + 4)
152 0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P+4) + 4)
153 0xfc, 0x44, // L1: add r12, pc
154 0x60, 0x47, // bx r12
155 };
156 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination);
157 uint64_t P = this->getVA();
158 memcpy(Buf, Data, sizeof(Data));
159 Target->relocateOne(Buf, R_ARM_THM_MOVW_PREL_NC, S - P - 12);
160 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_PREL, S - P - 8);
161}
162
Rui Ueyamae2efadc2016-07-09 22:52:32 +0000163// Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
Rui Ueyama3d2bbb12016-07-09 22:52:30 +0000164template <class ELFT> void MipsThunk<ELFT>::writeTo(uint8_t *Buf) const {
Rui Ueyamae2efadc2016-07-09 22:52:32 +0000165 const endianness E = ELFT::TargetEndianness;
166
167 uint64_t S = this->Destination.template getVA<ELFT>();
168 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func)
169 write32<E>(Buf + 4, 0x08000000 | (S >> 2)); // j func
170 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
171 write32<E>(Buf + 12, 0x00000000); // nop
172 Target->relocateOne(Buf, R_MIPS_HI16, S);
173 Target->relocateOne(Buf + 8, R_MIPS_LO16, S);
Rui Ueyama3d2bbb12016-07-09 22:52:30 +0000174}
Peter Smithfb05cd92016-07-08 16:10:27 +0000175
176template <class ELFT>
177Thunk<ELFT>::Thunk(const SymbolBody &D, const InputSection<ELFT> &O)
178 : Destination(D), Owner(O), Offset(O.getThunkOff() + O.getThunksSize()) {}
179
180template <class ELFT> typename ELFT::uint Thunk<ELFT>::getVA() const {
181 return Owner.OutSec->getVA() + Owner.OutSecOff + Offset;
182}
183
Rui Ueyama3d2bbb12016-07-09 22:52:30 +0000184template <class ELFT> Thunk<ELFT>::~Thunk() {}
Peter Smithfb05cd92016-07-08 16:10:27 +0000185
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000186// Creates a thunk for Thumb-ARM interworking.
Peter Smithfb05cd92016-07-08 16:10:27 +0000187template <class ELFT>
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000188static Thunk<ELFT> *createThunkArm(uint32_t Reloc, SymbolBody &S,
189 InputSection<ELFT> &IS) {
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000190 // ARM relocations need ARM to Thumb interworking Thunks.
191 // Thumb relocations need Thumb to ARM relocations.
192 // Use position independent Thunks if we require position independent code.
Rui Ueyamab933df12016-07-12 23:28:30 +0000193 BumpPtrAllocator &Alloc = IS.getFile()->Alloc;
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000194 switch (Reloc) {
Peter Smithfb05cd92016-07-08 16:10:27 +0000195 case R_ARM_PC24:
196 case R_ARM_PLT32:
197 case R_ARM_JUMP24:
Rui Ueyamab933df12016-07-12 23:28:30 +0000198 if (Config->Pic)
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000199 return new (Alloc) ARMToThumbV7PILongThunk<ELFT>(S, IS);
200 return new (Alloc) ARMToThumbV7ABSLongThunk<ELFT>(S, IS);
Peter Smithfb05cd92016-07-08 16:10:27 +0000201 case R_ARM_THM_JUMP19:
202 case R_ARM_THM_JUMP24:
Rui Ueyamab933df12016-07-12 23:28:30 +0000203 if (Config->Pic)
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000204 return new (Alloc) ThumbToARMV7PILongThunk<ELFT>(S, IS);
205 return new (Alloc) ThumbToARMV7ABSLongThunk<ELFT>(S, IS);
Peter Smithfb05cd92016-07-08 16:10:27 +0000206 }
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000207 fatal("unrecognized relocation type");
208}
209
210template <class ELFT>
211static void addThunkARM(uint32_t Reloc, SymbolBody &S, InputSection<ELFT> &IS) {
212 // Only one Thunk supported per symbol.
213 if (S.hasThunk<ELFT>())
214 return;
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000215
Peter Smithfb05cd92016-07-08 16:10:27 +0000216 // ARM Thunks are added to the same InputSection as the relocation. This
217 // isn't strictly necessary but it makes it more likely that a limited range
218 // branch can reach the Thunk, and it makes Thunks to the PLT section easier
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000219 Thunk<ELFT> *T = createThunkArm(Reloc, S, IS);
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000220 IS.addThunk(T);
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000221 if (auto *Sym = dyn_cast<DefinedRegular<ELFT>>(&S))
222 Sym->ThunkData = T;
223 else if (auto *Sym = dyn_cast<SharedSymbol<ELFT>>(&S))
224 Sym->ThunkData = T;
Peter Smithfb05cd92016-07-08 16:10:27 +0000225 else
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000226 fatal("symbol not DefinedRegular or Shared");
Peter Smithfb05cd92016-07-08 16:10:27 +0000227}
228
229template <class ELFT>
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000230static void addThunkMips(uint32_t RelocType, SymbolBody &S,
231 InputSection<ELFT> &IS) {
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000232 // Only one Thunk supported per symbol.
Peter Smithfb05cd92016-07-08 16:10:27 +0000233 if (S.hasThunk<ELFT>())
Peter Smithfb05cd92016-07-08 16:10:27 +0000234 return;
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000235
236 // Mips Thunks are added to the InputSection defining S.
Peter Smithfb05cd92016-07-08 16:10:27 +0000237 auto *R = cast<DefinedRegular<ELFT>>(&S);
238 auto *Sec = cast<InputSection<ELFT>>(R->Section);
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000239 auto *T = new (IS.getFile()->Alloc) MipsThunk<ELFT>(S, *Sec);
Peter Smithfb05cd92016-07-08 16:10:27 +0000240 Sec->addThunk(T);
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000241 R->ThunkData = T;
Peter Smithfb05cd92016-07-08 16:10:27 +0000242}
243
244template <class ELFT>
245void addThunk(uint32_t RelocType, SymbolBody &S, InputSection<ELFT> &IS) {
246 if (Config->EMachine == EM_ARM)
247 addThunkARM<ELFT>(RelocType, S, IS);
248 else if (Config->EMachine == EM_MIPS)
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000249 addThunkMips<ELFT>(RelocType, S, IS);
Peter Smithfb05cd92016-07-08 16:10:27 +0000250 else
251 llvm_unreachable("add Thunk only supported for ARM and Mips");
252}
253
254template void addThunk<ELF32LE>(uint32_t, SymbolBody &,
255 InputSection<ELF32LE> &);
256template void addThunk<ELF32BE>(uint32_t, SymbolBody &,
257 InputSection<ELF32BE> &);
258template void addThunk<ELF64LE>(uint32_t, SymbolBody &,
259 InputSection<ELF64LE> &);
260template void addThunk<ELF64BE>(uint32_t, SymbolBody &,
261 InputSection<ELF64BE> &);
262
Rui Ueyamaec4220d2016-07-09 22:06:47 +0000263template class Thunk<ELF32LE>;
264template class Thunk<ELF32BE>;
265template class Thunk<ELF64LE>;
266template class Thunk<ELF64BE>;
Peter Smithfb05cd92016-07-08 16:10:27 +0000267
268} // namespace elf
269} // namespace lld