blob: 42c21ce3fda54dac623226535e9a7ff4472ba2ba [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
84// Mips thunk.
85// Only the MIPS LA25 Thunk is supported, the implementation is delegated
86// to the MipsTargetInfo class in Target.cpp
87template <class ELFT> class MipsThunk final : public Thunk<ELFT> {
88public:
89 MipsThunk(const SymbolBody &Dest, const InputSection<ELFT> &Owner)
90 : Thunk<ELFT>(Dest, Owner) {}
91
92 uint32_t size() const override { return 16; }
93 void writeTo(uint8_t *Buf) const override;
94};
95} // anonymous namespace
96
97// ARM Target Thunks
98template <class ELFT> static uint64_t getARMThunkDestVA(const SymbolBody &S) {
99 return S.isInPlt() ? S.getPltVA<ELFT>() : S.getVA<ELFT>();
100}
101
102template <class ELFT>
103void ARMToThumbV7ABSLongThunk<ELFT>::writeTo(uint8_t *Buf) const {
104 const uint8_t Data[] = {
105 0x00, 0xc0, 0x00, 0xe3, // movw ip,:lower16:S
106 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S
107 0x1c, 0xff, 0x2f, 0xe1, // bx ip
108 };
109 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination);
110 memcpy(Buf, Data, sizeof(Data));
111 Target->relocateOne(Buf, R_ARM_MOVW_ABS_NC, S);
112 Target->relocateOne(Buf + 4, R_ARM_MOVT_ABS, S);
113}
114
115template <class ELFT>
116void ThumbToARMV7ABSLongThunk<ELFT>::writeTo(uint8_t *Buf) const {
117 const uint8_t Data[] = {
118 0x40, 0xf2, 0x00, 0x0c, // movw ip, :lower16:S
119 0xc0, 0xf2, 0x00, 0x0c, // movt ip, :upper16:S
120 0x60, 0x47, // bx ip
121 };
122 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination);
123 memcpy(Buf, Data, sizeof(Data));
124 Target->relocateOne(Buf, R_ARM_THM_MOVW_ABS_NC, S);
125 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_ABS, S);
126}
127
128template <class ELFT>
129void ARMToThumbV7PILongThunk<ELFT>::writeTo(uint8_t *Buf) const {
130 const uint8_t Data[] = {
131 0xf0, 0xcf, 0x0f, 0xe3, // P: movw ip,:lower16:S - (P + (L1-P) +8)
132 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S - (P + (L1-P+4) +8)
133 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
134 0x1c, 0xff, 0x2f, 0xe1, // bx r12
135 };
136 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination);
137 uint64_t P = this->getVA();
138 memcpy(Buf, Data, sizeof(Data));
139 Target->relocateOne(Buf, R_ARM_MOVW_PREL_NC, S - P - 16);
140 Target->relocateOne(Buf + 4, R_ARM_MOVT_PREL, S - P - 12);
141}
142
143template <class ELFT>
144void ThumbToARMV7PILongThunk<ELFT>::writeTo(uint8_t *Buf) const {
145 const uint8_t Data[] = {
146 0x4f, 0xf6, 0xf4, 0x7c, // P: movw ip,:lower16:S - (P + (L1-P) + 4)
147 0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P+4) + 4)
148 0xfc, 0x44, // L1: add r12, pc
149 0x60, 0x47, // bx r12
150 };
151 uint64_t S = getARMThunkDestVA<ELFT>(this->Destination);
152 uint64_t P = this->getVA();
153 memcpy(Buf, Data, sizeof(Data));
154 Target->relocateOne(Buf, R_ARM_THM_MOVW_PREL_NC, S - P - 12);
155 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_PREL, S - P - 8);
156}
157
158template <class ELFT> void MipsThunk<ELFT>::writeTo(uint8_t *Buf) const {
159 const SymbolBody &D = this->Destination;
160 uint64_t S = D.getVA<ELFT>();
161 Target->writeThunk(Buf, S);
162}
Peter Smithfb05cd92016-07-08 16:10:27 +0000163
164template <class ELFT>
165Thunk<ELFT>::Thunk(const SymbolBody &D, const InputSection<ELFT> &O)
166 : Destination(D), Owner(O), Offset(O.getThunkOff() + O.getThunksSize()) {}
167
168template <class ELFT> typename ELFT::uint Thunk<ELFT>::getVA() const {
169 return Owner.OutSec->getVA() + Owner.OutSecOff + Offset;
170}
171
Rui Ueyama3d2bbb12016-07-09 22:52:30 +0000172template <class ELFT> Thunk<ELFT>::~Thunk() {}
Peter Smithfb05cd92016-07-08 16:10:27 +0000173
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000174// Creates a thunk for Thumb-ARM interworking.
Peter Smithfb05cd92016-07-08 16:10:27 +0000175template <class ELFT>
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000176static Thunk<ELFT> *createThunkArm(uint32_t Reloc, SymbolBody &S,
177 InputSection<ELFT> &IS) {
Peter Smithfb05cd92016-07-08 16:10:27 +0000178 bool NeedsPI = Config->Pic || Config->Pie || Config->Shared;
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000179 BumpPtrAllocator &Alloc = IS.getFile()->Alloc;
180
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000181 // ARM relocations need ARM to Thumb interworking Thunks.
182 // Thumb relocations need Thumb to ARM relocations.
183 // Use position independent Thunks if we require position independent code.
184 switch (Reloc) {
Peter Smithfb05cd92016-07-08 16:10:27 +0000185 case R_ARM_PC24:
186 case R_ARM_PLT32:
187 case R_ARM_JUMP24:
188 if (NeedsPI)
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000189 return new (Alloc) ARMToThumbV7PILongThunk<ELFT>(S, IS);
190 return new (Alloc) ARMToThumbV7ABSLongThunk<ELFT>(S, IS);
Peter Smithfb05cd92016-07-08 16:10:27 +0000191 case R_ARM_THM_JUMP19:
192 case R_ARM_THM_JUMP24:
193 if (NeedsPI)
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000194 return new (Alloc) ThumbToARMV7PILongThunk<ELFT>(S, IS);
195 return new (Alloc) ThumbToARMV7ABSLongThunk<ELFT>(S, IS);
Peter Smithfb05cd92016-07-08 16:10:27 +0000196 }
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000197 fatal("unrecognized relocation type");
198}
199
200template <class ELFT>
201static void addThunkARM(uint32_t Reloc, SymbolBody &S, InputSection<ELFT> &IS) {
202 // Only one Thunk supported per symbol.
203 if (S.hasThunk<ELFT>())
204 return;
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000205
Peter Smithfb05cd92016-07-08 16:10:27 +0000206 // ARM Thunks are added to the same InputSection as the relocation. This
207 // isn't strictly necessary but it makes it more likely that a limited range
208 // branch can reach the Thunk, and it makes Thunks to the PLT section easier
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000209 Thunk<ELFT> *T = createThunkArm(Reloc, S, IS);
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000210 IS.addThunk(T);
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000211 if (auto *Sym = dyn_cast<DefinedRegular<ELFT>>(&S))
212 Sym->ThunkData = T;
213 else if (auto *Sym = dyn_cast<SharedSymbol<ELFT>>(&S))
214 Sym->ThunkData = T;
Peter Smithfb05cd92016-07-08 16:10:27 +0000215 else
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000216 fatal("symbol not DefinedRegular or Shared");
Peter Smithfb05cd92016-07-08 16:10:27 +0000217}
218
219template <class ELFT>
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000220static void addThunkMips(uint32_t RelocType, SymbolBody &S,
221 InputSection<ELFT> &IS) {
Rui Ueyama5e3d6042016-07-09 22:03:51 +0000222 // Only one Thunk supported per symbol.
Peter Smithfb05cd92016-07-08 16:10:27 +0000223 if (S.hasThunk<ELFT>())
Peter Smithfb05cd92016-07-08 16:10:27 +0000224 return;
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000225
226 // Mips Thunks are added to the InputSection defining S.
Peter Smithfb05cd92016-07-08 16:10:27 +0000227 auto *R = cast<DefinedRegular<ELFT>>(&S);
228 auto *Sec = cast<InputSection<ELFT>>(R->Section);
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000229 auto *T = new (IS.getFile()->Alloc) MipsThunk<ELFT>(S, *Sec);
Peter Smithfb05cd92016-07-08 16:10:27 +0000230 Sec->addThunk(T);
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000231 R->ThunkData = T;
Peter Smithfb05cd92016-07-08 16:10:27 +0000232}
233
234template <class ELFT>
235void addThunk(uint32_t RelocType, SymbolBody &S, InputSection<ELFT> &IS) {
236 if (Config->EMachine == EM_ARM)
237 addThunkARM<ELFT>(RelocType, S, IS);
238 else if (Config->EMachine == EM_MIPS)
Rui Ueyama8b8d0052016-07-08 17:58:54 +0000239 addThunkMips<ELFT>(RelocType, S, IS);
Peter Smithfb05cd92016-07-08 16:10:27 +0000240 else
241 llvm_unreachable("add Thunk only supported for ARM and Mips");
242}
243
244template void addThunk<ELF32LE>(uint32_t, SymbolBody &,
245 InputSection<ELF32LE> &);
246template void addThunk<ELF32BE>(uint32_t, SymbolBody &,
247 InputSection<ELF32BE> &);
248template void addThunk<ELF64LE>(uint32_t, SymbolBody &,
249 InputSection<ELF64LE> &);
250template void addThunk<ELF64BE>(uint32_t, SymbolBody &,
251 InputSection<ELF64BE> &);
252
Rui Ueyamaec4220d2016-07-09 22:06:47 +0000253template class Thunk<ELF32LE>;
254template class Thunk<ELF32BE>;
255template class Thunk<ELF64LE>;
256template class Thunk<ELF64BE>;
Peter Smithfb05cd92016-07-08 16:10:27 +0000257
258} // namespace elf
259} // namespace lld