blob: b5fe570c270e0db4ffe97ce96e4e8f279f493786 [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//
10// This file contains both the Target independent and Target specific Thunk
11// classes
12//
13// A Thunk Object represents a single Thunk that will be written to an
14// InputSection when the InputSection contents are written. The InputSection
15// maintains a list of Thunks that it owns.
16//===---------------------------------------------------------------------===//
17
18#include "Thunks.h"
19#include "Error.h"
20#include "InputFiles.h"
21#include "InputSection.h"
22#include "OutputSections.h"
23#include "Symbols.h"
24#include "Target.h"
Rui Ueyama8b8d0052016-07-08 17:58:54 +000025#include "llvm/Support/Allocator.h"
Peter Smithfb05cd92016-07-08 16:10:27 +000026
27#include "llvm/Object/ELF.h"
28#include "llvm/Support/ELF.h"
29#include "llvm/Support/Endian.h"
30
31using namespace llvm;
32using namespace llvm::object;
33using namespace llvm::support::endian;
34using namespace llvm::ELF;
35
36namespace lld {
37namespace elf {
38
Rui Ueyama3d2bbb12016-07-09 22:52:30 +000039namespace {
40// Specific ARM Thunk implementations. The naming convention is:
41// Source State, TargetState, Target Requirement, ABS or PI, Range
42template <class ELFT>
43class ARMToThumbV7ABSLongThunk final : public Thunk<ELFT> {
44public:
45 ARMToThumbV7ABSLongThunk(const SymbolBody &Dest,
46 const InputSection<ELFT> &Owner)
47 : Thunk<ELFT>(Dest, Owner) {}
48
49 uint32_t size() const override { return 12; }
50 void writeTo(uint8_t *Buf) const override;
51};
52
53template <class ELFT> class ARMToThumbV7PILongThunk final : public Thunk<ELFT> {
54public:
55 ARMToThumbV7PILongThunk(const SymbolBody &Dest,
56 const InputSection<ELFT> &Owner)
57 : Thunk<ELFT>(Dest, Owner) {}
58
59 uint32_t size() const override { return 16; }
60 void writeTo(uint8_t *Buf) const override;
61};
62
63template <class ELFT>
64class ThumbToARMV7ABSLongThunk final : public Thunk<ELFT> {
65public:
66 ThumbToARMV7ABSLongThunk(const SymbolBody &Dest,
67 const InputSection<ELFT> &Owner)
68 : Thunk<ELFT>(Dest, Owner) {}
69
70 uint32_t size() const override { return 10; }
71 void writeTo(uint8_t *Buf) const override;
72};
73
74template <class ELFT> class ThumbToARMV7PILongThunk final : public Thunk<ELFT> {
75public:
76 ThumbToARMV7PILongThunk(const SymbolBody &Dest,
77 const InputSection<ELFT> &Owner)
78 : Thunk<ELFT>(Dest, Owner) {}
79
80 uint32_t size() const override { return 12; }
81 void writeTo(uint8_t *Buf) const override;
82};
83
Rui Ueyamae2efadc2016-07-09 22:52:32 +000084// MIPS LA25 thunk
Rui Ueyama3d2bbb12016-07-09 22:52:30 +000085template <class ELFT> class MipsThunk final : public Thunk<ELFT> {
86public:
87 MipsThunk(const SymbolBody &Dest, const InputSection<ELFT> &Owner)
88 : Thunk<ELFT>(Dest, Owner) {}
89
90 uint32_t size() const override { return 16; }
91 void writeTo(uint8_t *Buf) const override;
92};
93} // anonymous namespace
94
95// ARM Target Thunks
96template <class ELFT> static uint64_t getARMThunkDestVA(const SymbolBody &S) {
97 return S.isInPlt() ? S.getPltVA<ELFT>() : S.getVA<ELFT>();
98}
99
100template <class ELFT>
101void ARMToThumbV7ABSLongThunk<ELFT>::writeTo(uint8_t *Buf) const {
102 const uint8_t Data[] = {
103 0x00, 0xc0, 0x00, 0xe3, // movw ip,:lower16:S
104 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S
105 0x1c, 0xff, 0x2f, 0xe1, // bx ip
106 };
107 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination);
108 memcpy(Buf, Data, sizeof(Data));
109 Target->relocateOne(Buf, R_ARM_MOVW_ABS_NC, S);
110 Target->relocateOne(Buf + 4, R_ARM_MOVT_ABS, S);
111}
112
113template <class ELFT>
114void ThumbToARMV7ABSLongThunk<ELFT>::writeTo(uint8_t *Buf) const {
115 const uint8_t Data[] = {
116 0x40, 0xf2, 0x00, 0x0c, // movw ip, :lower16:S
117 0xc0, 0xf2, 0x00, 0x0c, // movt ip, :upper16:S
118 0x60, 0x47, // bx ip
119 };
120 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination);
121 memcpy(Buf, Data, sizeof(Data));
122 Target->relocateOne(Buf, R_ARM_THM_MOVW_ABS_NC, S);
123 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_ABS, S);
124}
125
126template <class ELFT>
127void ARMToThumbV7PILongThunk<ELFT>::writeTo(uint8_t *Buf) const {
128 const uint8_t Data[] = {
129 0xf0, 0xcf, 0x0f, 0xe3, // P: movw ip,:lower16:S - (P + (L1-P) +8)
130 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S - (P + (L1-P+4) +8)
131 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
132 0x1c, 0xff, 0x2f, 0xe1, // bx r12
133 };
134 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination);
135 uint64_t P = this->getVA();
136 memcpy(Buf, Data, sizeof(Data));
137 Target->relocateOne(Buf, R_ARM_MOVW_PREL_NC, S - P - 16);
138 Target->relocateOne(Buf + 4, R_ARM_MOVT_PREL, S - P - 12);
139}
140
141template <class ELFT>
142void ThumbToARMV7PILongThunk<ELFT>::writeTo(uint8_t *Buf) const {
143 const uint8_t Data[] = {
144 0x4f, 0xf6, 0xf4, 0x7c, // P: movw ip,:lower16:S - (P + (L1-P) + 4)
145 0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P+4) + 4)
146 0xfc, 0x44, // L1: add r12, pc
147 0x60, 0x47, // bx r12
148 };
149 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination);
150 uint64_t P = this->getVA();
151 memcpy(Buf, Data, sizeof(Data));
152 Target->relocateOne(Buf, R_ARM_THM_MOVW_PREL_NC, S - P - 12);
153 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_PREL, S - P - 8);
154}
155
Rui Ueyamae2efadc2016-07-09 22:52:32 +0000156// Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
Rui Ueyama3d2bbb12016-07-09 22:52:30 +0000157template <class ELFT> void MipsThunk<ELFT>::writeTo(uint8_t *Buf) const {
Rui Ueyamae2efadc2016-07-09 22:52:32 +0000158 const endianness E = ELFT::TargetEndianness;
159
160 uint64_t S = this->Destination.template getVA<ELFT>();
161 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func)
162 write32<E>(Buf + 4, 0x08000000 | (S >> 2)); // j func
163 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
164 write32<E>(Buf + 12, 0x00000000); // nop
165 Target->relocateOne(Buf, R_MIPS_HI16, S);
166 Target->relocateOne(Buf + 8, R_MIPS_LO16, S);
Rui Ueyama3d2bbb12016-07-09 22:52:30 +0000167}
Peter Smithfb05cd92016-07-08 16:10:27 +0000168
169template <class ELFT>
170Thunk<ELFT>::Thunk(const SymbolBody &D, const InputSection<ELFT> &O)
171 : Destination(D), Owner(O), Offset(O.getThunkOff() + O.getThunksSize()) {}
172
173template <class ELFT> typename ELFT::uint Thunk<ELFT>::getVA() const {
174 return Owner.OutSec->getVA() + Owner.OutSecOff + Offset;
175}
176
Rui Ueyama3d2bbb12016-07-09 22:52:30 +0000177template <class ELFT> Thunk<ELFT>::~Thunk() {}
Peter Smithfb05cd92016-07-08 16:10:27 +0000178
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000179// Creates a thunk for Thumb-ARM interworking.
Peter Smithfb05cd92016-07-08 16:10:27 +0000180template <class ELFT>
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000181static Thunk<ELFT> *createThunkArm(uint32_t Reloc, SymbolBody &S,
182 InputSection<ELFT> &IS) {
Peter Smithfb05cd92016-07-08 16:10:27 +0000183 bool NeedsPI = Config->Pic || Config->Pie || Config->Shared;
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000184 BumpPtrAllocator &Alloc = IS.getFile()->Alloc;
185
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000186 // ARM relocations need ARM to Thumb interworking Thunks.
187 // Thumb relocations need Thumb to ARM relocations.
188 // Use position independent Thunks if we require position independent code.
189 switch (Reloc) {
Peter Smithfb05cd92016-07-08 16:10:27 +0000190 case R_ARM_PC24:
191 case R_ARM_PLT32:
192 case R_ARM_JUMP24:
193 if (NeedsPI)
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000194 return new (Alloc) ARMToThumbV7PILongThunk<ELFT>(S, IS);
195 return new (Alloc) ARMToThumbV7ABSLongThunk<ELFT>(S, IS);
Peter Smithfb05cd92016-07-08 16:10:27 +0000196 case R_ARM_THM_JUMP19:
197 case R_ARM_THM_JUMP24:
198 if (NeedsPI)
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000199 return new (Alloc) ThumbToARMV7PILongThunk<ELFT>(S, IS);
200 return new (Alloc) ThumbToARMV7ABSLongThunk<ELFT>(S, IS);
Peter Smithfb05cd92016-07-08 16:10:27 +0000201 }
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000202 fatal("unrecognized relocation type");
203}
204
205template <class ELFT>
206static void addThunkARM(uint32_t Reloc, SymbolBody &S, InputSection<ELFT> &IS) {
207 // Only one Thunk supported per symbol.
208 if (S.hasThunk<ELFT>())
209 return;
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000210
Peter Smithfb05cd92016-07-08 16:10:27 +0000211 // ARM Thunks are added to the same InputSection as the relocation. This
212 // isn't strictly necessary but it makes it more likely that a limited range
213 // branch can reach the Thunk, and it makes Thunks to the PLT section easier
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000214 Thunk<ELFT> *T = createThunkArm(Reloc, S, IS);
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000215 IS.addThunk(T);
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000216 if (auto *Sym = dyn_cast<DefinedRegular<ELFT>>(&S))
217 Sym->ThunkData = T;
218 else if (auto *Sym = dyn_cast<SharedSymbol<ELFT>>(&S))
219 Sym->ThunkData = T;
Peter Smithfb05cd92016-07-08 16:10:27 +0000220 else
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000221 fatal("symbol not DefinedRegular or Shared");
Peter Smithfb05cd92016-07-08 16:10:27 +0000222}
223
224template <class ELFT>
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000225static void addThunkMips(uint32_t RelocType, SymbolBody &S,
226 InputSection<ELFT> &IS) {
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000227 // Only one Thunk supported per symbol.
Peter Smithfb05cd92016-07-08 16:10:27 +0000228 if (S.hasThunk<ELFT>())
Peter Smithfb05cd92016-07-08 16:10:27 +0000229 return;
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000230
231 // Mips Thunks are added to the InputSection defining S.
Peter Smithfb05cd92016-07-08 16:10:27 +0000232 auto *R = cast<DefinedRegular<ELFT>>(&S);
233 auto *Sec = cast<InputSection<ELFT>>(R->Section);
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000234 auto *T = new (IS.getFile()->Alloc) MipsThunk<ELFT>(S, *Sec);
Peter Smithfb05cd92016-07-08 16:10:27 +0000235 Sec->addThunk(T);
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000236 R->ThunkData = T;
Peter Smithfb05cd92016-07-08 16:10:27 +0000237}
238
239template <class ELFT>
240void addThunk(uint32_t RelocType, SymbolBody &S, InputSection<ELFT> &IS) {
241 if (Config->EMachine == EM_ARM)
242 addThunkARM<ELFT>(RelocType, S, IS);
243 else if (Config->EMachine == EM_MIPS)
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000244 addThunkMips<ELFT>(RelocType, S, IS);
Peter Smithfb05cd92016-07-08 16:10:27 +0000245 else
246 llvm_unreachable("add Thunk only supported for ARM and Mips");
247}
248
249template void addThunk<ELF32LE>(uint32_t, SymbolBody &,
250 InputSection<ELF32LE> &);
251template void addThunk<ELF32BE>(uint32_t, SymbolBody &,
252 InputSection<ELF32BE> &);
253template void addThunk<ELF64LE>(uint32_t, SymbolBody &,
254 InputSection<ELF64LE> &);
255template void addThunk<ELF64BE>(uint32_t, SymbolBody &,
256 InputSection<ELF64BE> &);
257
Rui Ueyamaec4220d2016-07-09 22:06:47 +0000258template class Thunk<ELF32LE>;
259template class Thunk<ELF32BE>;
260template class Thunk<ELF64LE>;
261template class Thunk<ELF64BE>;
Peter Smithfb05cd92016-07-08 16:10:27 +0000262
263} // namespace elf
264} // namespace lld