blob: 95b57dc0db4260dd43ca9587d3eb2f8bc679f04a [file] [log] [blame]
Peter Smithfb05cd92016-07-08 16:10:271//===- 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:0010// This file contains Thunk subclasses.
Peter Smithfb05cd92016-07-08 16:10:2711//
Rui Ueyama89c37622016-07-09 23:16:0012// 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:2722//===---------------------------------------------------------------------===//
23
24#include "Thunks.h"
Eugene Zelenko22886a22016-11-05 01:00:5625#include "Config.h"
Peter Smithfb05cd92016-07-08 16:10:2726#include "InputSection.h"
27#include "OutputSections.h"
28#include "Symbols.h"
Peter Smith3a52eb02017-02-01 10:26:0329#include "SyntheticSections.h"
Peter Smithfb05cd92016-07-08 16:10:2730#include "Target.h"
Bob Haarmanb8a59c82017-10-25 22:28:3831#include "lld/Common/ErrorHandler.h"
Rui Ueyama2017d522017-11-28 20:39:1732#include "lld/Common/Memory.h"
Zachary Turner264b5d92017-06-07 03:48:5633#include "llvm/BinaryFormat/ELF.h"
Eugene Zelenko22886a22016-11-05 01:00:5634#include "llvm/Support/Casting.h"
Peter Smithfb05cd92016-07-08 16:10:2735#include "llvm/Support/Endian.h"
Eugene Zelenko22886a22016-11-05 01:00:5636#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/MathExtras.h"
38#include <cstdint>
39#include <cstring>
Peter Smithfb05cd92016-07-08 16:10:2740
41using namespace llvm;
42using namespace llvm::object;
Peter Smithfb05cd92016-07-08 16:10:2743using namespace llvm::ELF;
44
45namespace lld {
46namespace elf {
47
Rui Ueyama3d2bbb12016-07-09 22:52:3048namespace {
Eugene Zelenko22886a22016-11-05 01:00:5649
Peter Smith31dddc92017-11-29 11:15:1250// AArch64 long range Thunks
51class AArch64ABSLongThunk final : public Thunk {
52public:
53 AArch64ABSLongThunk(Symbol &Dest) : Thunk(Dest) {}
Peter Collingbournecebab4a2018-03-28 21:33:3154 uint32_t size() override { return 16; }
55 void writeTo(uint8_t *Buf) override;
Peter Smith31dddc92017-11-29 11:15:1256 void addSymbols(ThunkSection &IS) override;
57};
58
59class AArch64ADRPThunk final : public Thunk {
60public:
61 AArch64ADRPThunk(Symbol &Dest) : Thunk(Dest) {}
Peter Collingbournecebab4a2018-03-28 21:33:3162 uint32_t size() override { return 12; }
63 void writeTo(uint8_t *Buf) override;
Peter Smith31dddc92017-11-29 11:15:1264 void addSymbols(ThunkSection &IS) override;
65};
66
Peter Collingbourne015d30c2018-03-29 22:43:5267// Base class for ARM thunks.
68//
69// An ARM thunk may be either short or long. A short thunk is simply a branch
70// (B) instruction, and it may be used to call ARM functions when the distance
71// from the thunk to the target is less than 32MB. Long thunks can branch to any
72// virtual address and can switch between ARM and Thumb, and they are
73// implemented in the derived classes. This class tries to create a short thunk
74// if the target is in range, otherwise it creates a long thunk.
75class ARMThunk : public Thunk {
76public:
77 ARMThunk(Symbol &Dest) : Thunk(Dest) {}
78
79 bool mayUseShortThunk();
80 uint32_t size() override { return mayUseShortThunk() ? 4 : sizeLong(); }
81 void writeTo(uint8_t *Buf) override;
82 bool isCompatibleWith(RelType Type) const override;
83
84 // Returns the size of a long thunk.
85 virtual uint32_t sizeLong() = 0;
86
87 // Writes a long thunk to Buf.
88 virtual void writeLong(uint8_t *Buf) = 0;
89
90private:
91 // This field tracks whether all previously considered layouts would allow
92 // this thunk to be short. If we have ever needed a long thunk, we always
93 // create a long thunk, even if the thunk may be short given the current
94 // distance to the target. We do this because transitioning from long to short
95 // can create layout oscillations in certain corner cases which would prevent
96 // the layout from converging.
97 bool MayUseShortThunk = true;
98};
99
100// Base class for Thumb-2 thunks.
101//
102// This class is similar to ARMThunk, but it uses the Thumb-2 B.W instruction
103// which has a range of 16MB.
104class ThumbThunk : public Thunk {
105public:
106 ThumbThunk(Symbol &Dest) : Thunk(Dest) { Alignment = 2; }
107
108 bool mayUseShortThunk();
109 uint32_t size() override { return mayUseShortThunk() ? 4 : sizeLong(); }
110 void writeTo(uint8_t *Buf) override;
111 bool isCompatibleWith(RelType Type) const override;
112
113 // Returns the size of a long thunk.
114 virtual uint32_t sizeLong() = 0;
115
116 // Writes a long thunk to Buf.
117 virtual void writeLong(uint8_t *Buf) = 0;
118
119private:
120 // See comment in ARMThunk above.
121 bool MayUseShortThunk = true;
122};
123
Rui Ueyama3d2bbb12016-07-09 22:52:30124// Specific ARM Thunk implementations. The naming convention is:
125// Source State, TargetState, Target Requirement, ABS or PI, Range
Peter Collingbourne015d30c2018-03-29 22:43:52126class ARMV7ABSLongThunk final : public ARMThunk {
Rui Ueyama3d2bbb12016-07-09 22:52:30127public:
Peter Collingbourne015d30c2018-03-29 22:43:52128 ARMV7ABSLongThunk(Symbol &Dest) : ARMThunk(Dest) {}
Rui Ueyama3d2bbb12016-07-09 22:52:30129
Peter Collingbourne015d30c2018-03-29 22:43:52130 uint32_t sizeLong() override { return 12; }
131 void writeLong(uint8_t *Buf) override;
George Rimar7b827042017-03-16 10:40:50132 void addSymbols(ThunkSection &IS) override;
Rui Ueyama3d2bbb12016-07-09 22:52:30133};
134
Peter Collingbourne015d30c2018-03-29 22:43:52135class ARMV7PILongThunk final : public ARMThunk {
Rui Ueyama3d2bbb12016-07-09 22:52:30136public:
Peter Collingbourne015d30c2018-03-29 22:43:52137 ARMV7PILongThunk(Symbol &Dest) : ARMThunk(Dest) {}
Rui Ueyama3d2bbb12016-07-09 22:52:30138
Peter Collingbourne015d30c2018-03-29 22:43:52139 uint32_t sizeLong() override { return 16; }
140 void writeLong(uint8_t *Buf) override;
George Rimar7b827042017-03-16 10:40:50141 void addSymbols(ThunkSection &IS) override;
Rui Ueyama3d2bbb12016-07-09 22:52:30142};
143
Peter Collingbourne015d30c2018-03-29 22:43:52144class ThumbV7ABSLongThunk final : public ThumbThunk {
Rui Ueyama3d2bbb12016-07-09 22:52:30145public:
Peter Collingbourne015d30c2018-03-29 22:43:52146 ThumbV7ABSLongThunk(Symbol &Dest) : ThumbThunk(Dest) {}
Rui Ueyama3d2bbb12016-07-09 22:52:30147
Peter Collingbourne015d30c2018-03-29 22:43:52148 uint32_t sizeLong() override { return 10; }
149 void writeLong(uint8_t *Buf) override;
George Rimar7b827042017-03-16 10:40:50150 void addSymbols(ThunkSection &IS) override;
Rui Ueyama3d2bbb12016-07-09 22:52:30151};
152
Peter Collingbourne015d30c2018-03-29 22:43:52153class ThumbV7PILongThunk final : public ThumbThunk {
Rui Ueyama3d2bbb12016-07-09 22:52:30154public:
Peter Collingbourne015d30c2018-03-29 22:43:52155 ThumbV7PILongThunk(Symbol &Dest) : ThumbThunk(Dest) {}
Rui Ueyama3d2bbb12016-07-09 22:52:30156
Peter Collingbourne015d30c2018-03-29 22:43:52157 uint32_t sizeLong() override { return 12; }
158 void writeLong(uint8_t *Buf) override;
George Rimar7b827042017-03-16 10:40:50159 void addSymbols(ThunkSection &IS) override;
Rui Ueyama3d2bbb12016-07-09 22:52:30160};
161
Peter Smitha8656c622018-08-20 09:37:50162// Implementations of Thunks for older Arm architectures that do not support
163// the movt/movw instructions. These thunks require at least Architecture v5
164// as used on processors such as the Arm926ej-s. There are no Thumb entry
165// points as there is no Thumb branch instruction on these architecture that
166// can result in a thunk
167class ARMV5ABSLongThunk final : public ARMThunk {
168public:
169 ARMV5ABSLongThunk(Symbol &Dest) : ARMThunk(Dest) {}
170
171 uint32_t sizeLong() override { return 8; }
172 void writeLong(uint8_t *Buf) override;
173 void addSymbols(ThunkSection &IS) override;
174 bool isCompatibleWith(uint32_t RelocType) const override;
175};
176
177class ARMV5PILongThunk final : public ARMThunk {
178public:
179 ARMV5PILongThunk(Symbol &Dest) : ARMThunk(Dest) {}
180
181 uint32_t sizeLong() override { return 16; }
182 void writeLong(uint8_t *Buf) override;
183 void addSymbols(ThunkSection &IS) override;
184 bool isCompatibleWith(uint32_t RelocType) const override;
185};
186
Peter Smith6ece0ad2018-12-17 10:33:47187// Implementations of Thunks for Arm v6-M. Only Thumb instructions are permitted
188class ThumbV6MABSLongThunk final : public ThumbThunk {
189public:
190 ThumbV6MABSLongThunk(Symbol &Dest) : ThumbThunk(Dest) {}
191
192 uint32_t sizeLong() override { return 12; }
193 void writeLong(uint8_t *Buf) override;
194 void addSymbols(ThunkSection &IS) override;
195};
196
197class ThumbV6MPILongThunk final : public ThumbThunk {
198public:
199 ThumbV6MPILongThunk(Symbol &Dest) : ThumbThunk(Dest) {}
200
201 uint32_t sizeLong() override { return 16; }
202 void writeLong(uint8_t *Buf) override;
203 void addSymbols(ThunkSection &IS) override;
204};
205
Rui Ueyamae2efadc2016-07-09 22:52:32206// MIPS LA25 thunk
George Rimarec84ffc2017-05-17 07:10:59207class MipsThunk final : public Thunk {
Rui Ueyama3d2bbb12016-07-09 22:52:30208public:
Rui Ueyamaf52496e2017-11-03 21:21:47209 MipsThunk(Symbol &Dest) : Thunk(Dest) {}
Rui Ueyama3d2bbb12016-07-09 22:52:30210
Peter Collingbournecebab4a2018-03-28 21:33:31211 uint32_t size() override { return 16; }
212 void writeTo(uint8_t *Buf) override;
George Rimar7b827042017-03-16 10:40:50213 void addSymbols(ThunkSection &IS) override;
Rafael Espindola774ea7d2017-02-23 16:49:07214 InputSection *getTargetInputSection() const override;
Rui Ueyama3d2bbb12016-07-09 22:52:30215};
Eugene Zelenko22886a22016-11-05 01:00:56216
Simon Atanasyanf8db4532017-10-03 13:30:02217// microMIPS R2-R5 LA25 thunk
218class MicroMipsThunk final : public Thunk {
219public:
Rui Ueyamaf52496e2017-11-03 21:21:47220 MicroMipsThunk(Symbol &Dest) : Thunk(Dest) {}
Simon Atanasyanf8db4532017-10-03 13:30:02221
Peter Collingbournecebab4a2018-03-28 21:33:31222 uint32_t size() override { return 14; }
223 void writeTo(uint8_t *Buf) override;
Simon Atanasyanf8db4532017-10-03 13:30:02224 void addSymbols(ThunkSection &IS) override;
225 InputSection *getTargetInputSection() const override;
226};
227
228// microMIPS R6 LA25 thunk
229class MicroMipsR6Thunk final : public Thunk {
230public:
Rui Ueyamaf52496e2017-11-03 21:21:47231 MicroMipsR6Thunk(Symbol &Dest) : Thunk(Dest) {}
Simon Atanasyanf8db4532017-10-03 13:30:02232
Peter Collingbournecebab4a2018-03-28 21:33:31233 uint32_t size() override { return 12; }
234 void writeTo(uint8_t *Buf) override;
Simon Atanasyanf8db4532017-10-03 13:30:02235 void addSymbols(ThunkSection &IS) override;
236 InputSection *getTargetInputSection() const override;
237};
238
Sean Fertiled2e887d2018-05-06 19:13:29239
240// PPC64 Plt call stubs.
241// Any call site that needs to call through a plt entry needs a call stub in
242// the .text section. The call stub is responsible for:
243// 1) Saving the toc-pointer to the stack.
244// 2) Loading the target functions address from the procedure linkage table into
245// r12 for use by the target functions global entry point, and into the count
246// register.
247// 3) Transfering control to the target function through an indirect branch.
248class PPC64PltCallStub final : public Thunk {
249public:
250 PPC64PltCallStub(Symbol &Dest) : Thunk(Dest) {}
Roman Lebedev7cb68962018-05-06 19:50:04251 uint32_t size() override { return 20; }
Sean Fertiled2e887d2018-05-06 19:13:29252 void writeTo(uint8_t *Buf) override;
253 void addSymbols(ThunkSection &IS) override;
254};
255
Sean Fertile614dc112018-11-14 17:56:43256// A bl instruction uses a signed 24 bit offset, with an implicit 4 byte
257// alignment. This gives a possible 26 bits of 'reach'. If the call offset is
258// larger then that we need to emit a long-branch thunk. The target address
259// of the callee is stored in a table to be accessed TOC-relative. Since the
260// call must be local (a non-local call will have a PltCallStub instead) the
261// table stores the address of the callee's local entry point. For
262// position-independent code a corresponding relative dynamic relocation is
263// used.
264class PPC64LongBranchThunk : public Thunk {
265public:
266 uint32_t size() override { return 16; }
267 void writeTo(uint8_t *Buf) override;
268 void addSymbols(ThunkSection &IS) override;
269
270protected:
271 PPC64LongBranchThunk(Symbol &Dest) : Thunk(Dest) {}
272};
273
274class PPC64PILongBranchThunk final : public PPC64LongBranchThunk {
275public:
276 PPC64PILongBranchThunk(Symbol &Dest) : PPC64LongBranchThunk(Dest) {
277 assert(!Dest.IsPreemptible);
278 if (Dest.isInPPC64Branchlt())
279 return;
280
281 In.PPC64LongBranchTarget->addEntry(Dest);
282 In.RelaDyn->addReloc({Target->RelativeRel, In.PPC64LongBranchTarget,
283 Dest.getPPC64LongBranchOffset(), true, &Dest,
284 getPPC64GlobalEntryToLocalEntryOffset(Dest.StOther)});
285 }
286};
287
288class PPC64PDLongBranchThunk final : public PPC64LongBranchThunk {
289public:
290 PPC64PDLongBranchThunk(Symbol &Dest) : PPC64LongBranchThunk(Dest) {
291 if (!Dest.isInPPC64Branchlt())
292 In.PPC64LongBranchTarget->addEntry(Dest);
293 }
294};
295
Eugene Zelenko22886a22016-11-05 01:00:56296} // end anonymous namespace
Rui Ueyama3d2bbb12016-07-09 22:52:30297
Peter Collingbournec5391ce2018-03-29 22:32:13298Defined *Thunk::addSymbol(StringRef Name, uint8_t Type, uint64_t Value,
299 InputSectionBase &Section) {
300 Defined *D = addSyntheticLocal(Name, Type, Value, /*Size=*/0, Section);
301 Syms.push_back(D);
302 return D;
303}
304
305void Thunk::setOffset(uint64_t NewOffset) {
306 for (Defined *D : Syms)
307 D->Value = D->Value - Offset + NewOffset;
308 Offset = NewOffset;
309}
310
Peter Smith31dddc92017-11-29 11:15:12311// AArch64 long range Thunks
312
313static uint64_t getAArch64ThunkDestVA(const Symbol &S) {
314 uint64_t V = S.isInPlt() ? S.getPltVA() : S.getVA();
315 return V;
316}
317
Peter Collingbournecebab4a2018-03-28 21:33:31318void AArch64ABSLongThunk::writeTo(uint8_t *Buf) {
Peter Smith31dddc92017-11-29 11:15:12319 const uint8_t Data[] = {
320 0x50, 0x00, 0x00, 0x58, // ldr x16, L0
321 0x00, 0x02, 0x1f, 0xd6, // br x16
322 0x00, 0x00, 0x00, 0x00, // L0: .xword S
323 0x00, 0x00, 0x00, 0x00,
324 };
325 uint64_t S = getAArch64ThunkDestVA(Destination);
326 memcpy(Buf, Data, sizeof(Data));
327 Target->relocateOne(Buf + 8, R_AARCH64_ABS64, S);
328}
329
330void AArch64ABSLongThunk::addSymbols(ThunkSection &IS) {
Peter Collingbournec5391ce2018-03-29 22:32:13331 addSymbol(Saver.save("__AArch64AbsLongThunk_" + Destination.getName()),
332 STT_FUNC, 0, IS);
333 addSymbol("$x", STT_NOTYPE, 0, IS);
334 addSymbol("$d", STT_NOTYPE, 8, IS);
Peter Smith31dddc92017-11-29 11:15:12335}
336
337// This Thunk has a maximum range of 4Gb, this is sufficient for all programs
338// using the small code model, including pc-relative ones. At time of writing
339// clang and gcc do not support the large code model for position independent
340// code so it is safe to use this for position independent thunks without
341// worrying about the destination being more than 4Gb away.
Peter Collingbournecebab4a2018-03-28 21:33:31342void AArch64ADRPThunk::writeTo(uint8_t *Buf) {
Peter Smith31dddc92017-11-29 11:15:12343 const uint8_t Data[] = {
344 0x10, 0x00, 0x00, 0x90, // adrp x16, Dest R_AARCH64_ADR_PREL_PG_HI21(Dest)
345 0x10, 0x02, 0x00, 0x91, // add x16, x16, R_AARCH64_ADD_ABS_LO12_NC(Dest)
346 0x00, 0x02, 0x1f, 0xd6, // br x16
347 };
348 uint64_t S = getAArch64ThunkDestVA(Destination);
Peter Collingbournec5391ce2018-03-29 22:32:13349 uint64_t P = getThunkTargetSym()->getVA();
Peter Smith31dddc92017-11-29 11:15:12350 memcpy(Buf, Data, sizeof(Data));
351 Target->relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
352 getAArch64Page(S) - getAArch64Page(P));
353 Target->relocateOne(Buf + 4, R_AARCH64_ADD_ABS_LO12_NC, S);
354}
355
Peter Collingbournec5391ce2018-03-29 22:32:13356void AArch64ADRPThunk::addSymbols(ThunkSection &IS) {
357 addSymbol(Saver.save("__AArch64ADRPThunk_" + Destination.getName()), STT_FUNC,
358 0, IS);
359 addSymbol("$x", STT_NOTYPE, 0, IS);
Peter Smith31dddc92017-11-29 11:15:12360}
361
Rui Ueyama3d2bbb12016-07-09 22:52:30362// ARM Target Thunks
Rui Ueyamaf52496e2017-11-03 21:21:47363static uint64_t getARMThunkDestVA(const Symbol &S) {
George Rimarf64618a2017-03-17 11:56:54364 uint64_t V = S.isInPlt() ? S.getPltVA() : S.getVA();
Rafael Espindola3a9eef12016-09-01 13:52:52365 return SignExtend64<32>(V);
Rui Ueyama3d2bbb12016-07-09 22:52:30366}
367
Peter Collingbourne015d30c2018-03-29 22:43:52368// This function returns true if the target is not Thumb and is within 2^26, and
369// it has not previously returned false (see comment for MayUseShortThunk).
370bool ARMThunk::mayUseShortThunk() {
371 if (!MayUseShortThunk)
372 return false;
373 uint64_t S = getARMThunkDestVA(Destination);
374 if (S & 1) {
375 MayUseShortThunk = false;
376 return false;
377 }
378 uint64_t P = getThunkTargetSym()->getVA();
379 int64_t Offset = S - P - 8;
380 MayUseShortThunk = llvm::isInt<26>(Offset);
381 return MayUseShortThunk;
382}
383
384void ARMThunk::writeTo(uint8_t *Buf) {
385 if (!mayUseShortThunk()) {
386 writeLong(Buf);
387 return;
388 }
389
390 uint64_t S = getARMThunkDestVA(Destination);
391 uint64_t P = getThunkTargetSym()->getVA();
392 int64_t Offset = S - P - 8;
393 const uint8_t Data[] = {
394 0x00, 0x00, 0x00, 0xea, // b S
395 };
396 memcpy(Buf, Data, sizeof(Data));
397 Target->relocateOne(Buf, R_ARM_JUMP24, Offset);
398}
399
400bool ARMThunk::isCompatibleWith(RelType Type) const {
401 // Thumb branch relocations can't use BLX
402 return Type != R_ARM_THM_JUMP19 && Type != R_ARM_THM_JUMP24;
403}
404
405// This function returns true if the target is Thumb and is within 2^25, and
406// it has not previously returned false (see comment for MayUseShortThunk).
407bool ThumbThunk::mayUseShortThunk() {
408 if (!MayUseShortThunk)
409 return false;
410 uint64_t S = getARMThunkDestVA(Destination);
411 if ((S & 1) == 0) {
412 MayUseShortThunk = false;
413 return false;
414 }
415 uint64_t P = getThunkTargetSym()->getVA() & ~1;
416 int64_t Offset = S - P - 4;
417 MayUseShortThunk = llvm::isInt<25>(Offset);
418 return MayUseShortThunk;
419}
420
421void ThumbThunk::writeTo(uint8_t *Buf) {
422 if (!mayUseShortThunk()) {
423 writeLong(Buf);
424 return;
425 }
426
427 uint64_t S = getARMThunkDestVA(Destination);
428 uint64_t P = getThunkTargetSym()->getVA();
429 int64_t Offset = S - P - 4;
430 const uint8_t Data[] = {
431 0x00, 0xf0, 0x00, 0xb0, // b.w S
432 };
433 memcpy(Buf, Data, sizeof(Data));
434 Target->relocateOne(Buf, R_ARM_THM_JUMP24, Offset);
435}
436
437bool ThumbThunk::isCompatibleWith(RelType Type) const {
438 // ARM branch relocations can't use BLX
439 return Type != R_ARM_JUMP24 && Type != R_ARM_PC24 && Type != R_ARM_PLT32;
440}
441
442void ARMV7ABSLongThunk::writeLong(uint8_t *Buf) {
Rui Ueyama3d2bbb12016-07-09 22:52:30443 const uint8_t Data[] = {
444 0x00, 0xc0, 0x00, 0xe3, // movw ip,:lower16:S
445 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S
446 0x1c, 0xff, 0x2f, 0xe1, // bx ip
447 };
George Rimarec84ffc2017-05-17 07:10:59448 uint64_t S = getARMThunkDestVA(Destination);
Rui Ueyama3d2bbb12016-07-09 22:52:30449 memcpy(Buf, Data, sizeof(Data));
450 Target->relocateOne(Buf, R_ARM_MOVW_ABS_NC, S);
451 Target->relocateOne(Buf + 4, R_ARM_MOVT_ABS, S);
452}
453
George Rimarec84ffc2017-05-17 07:10:59454void ARMV7ABSLongThunk::addSymbols(ThunkSection &IS) {
Peter Collingbournec5391ce2018-03-29 22:32:13455 addSymbol(Saver.save("__ARMv7ABSLongThunk_" + Destination.getName()),
456 STT_FUNC, 0, IS);
457 addSymbol("$a", STT_NOTYPE, 0, IS);
Peter Smith3a52eb02017-02-01 10:26:03458}
459
Peter Collingbourne015d30c2018-03-29 22:43:52460void ThumbV7ABSLongThunk::writeLong(uint8_t *Buf) {
Rui Ueyama3d2bbb12016-07-09 22:52:30461 const uint8_t Data[] = {
462 0x40, 0xf2, 0x00, 0x0c, // movw ip, :lower16:S
463 0xc0, 0xf2, 0x00, 0x0c, // movt ip, :upper16:S
464 0x60, 0x47, // bx ip
465 };
George Rimarec84ffc2017-05-17 07:10:59466 uint64_t S = getARMThunkDestVA(Destination);
Rui Ueyama3d2bbb12016-07-09 22:52:30467 memcpy(Buf, Data, sizeof(Data));
468 Target->relocateOne(Buf, R_ARM_THM_MOVW_ABS_NC, S);
469 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_ABS, S);
470}
471
George Rimarec84ffc2017-05-17 07:10:59472void ThumbV7ABSLongThunk::addSymbols(ThunkSection &IS) {
Peter Collingbournec5391ce2018-03-29 22:32:13473 addSymbol(Saver.save("__Thumbv7ABSLongThunk_" + Destination.getName()),
474 STT_FUNC, 1, IS);
475 addSymbol("$t", STT_NOTYPE, 0, IS);
Peter Smith3a52eb02017-02-01 10:26:03476}
477
Peter Collingbourne015d30c2018-03-29 22:43:52478void ARMV7PILongThunk::writeLong(uint8_t *Buf) {
Rui Ueyama3d2bbb12016-07-09 22:52:30479 const uint8_t Data[] = {
Shoaib Meenai75d616b2017-09-21 21:04:42480 0xf0, 0xcf, 0x0f, 0xe3, // P: movw ip,:lower16:S - (P + (L1-P) + 8)
481 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S - (P + (L1-P) + 8)
Martin Storsjo24f590f2018-08-31 08:03:33482 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
483 0x1c, 0xff, 0x2f, 0xe1, // bx ip
Rui Ueyama3d2bbb12016-07-09 22:52:30484 };
George Rimarec84ffc2017-05-17 07:10:59485 uint64_t S = getARMThunkDestVA(Destination);
Peter Collingbournec5391ce2018-03-29 22:32:13486 uint64_t P = getThunkTargetSym()->getVA();
Peter Smith1811e482019-01-10 16:08:23487 int64_t Offset = S - P - 16;
Rui Ueyama3d2bbb12016-07-09 22:52:30488 memcpy(Buf, Data, sizeof(Data));
Shoaib Meenai75d616b2017-09-21 21:04:42489 Target->relocateOne(Buf, R_ARM_MOVW_PREL_NC, Offset);
490 Target->relocateOne(Buf + 4, R_ARM_MOVT_PREL, Offset);
Rui Ueyama3d2bbb12016-07-09 22:52:30491}
492
George Rimarec84ffc2017-05-17 07:10:59493void ARMV7PILongThunk::addSymbols(ThunkSection &IS) {
Peter Collingbournec5391ce2018-03-29 22:32:13494 addSymbol(Saver.save("__ARMV7PILongThunk_" + Destination.getName()), STT_FUNC,
495 0, IS);
496 addSymbol("$a", STT_NOTYPE, 0, IS);
Peter Smith3a52eb02017-02-01 10:26:03497}
498
Peter Collingbourne015d30c2018-03-29 22:43:52499void ThumbV7PILongThunk::writeLong(uint8_t *Buf) {
Rui Ueyama3d2bbb12016-07-09 22:52:30500 const uint8_t Data[] = {
501 0x4f, 0xf6, 0xf4, 0x7c, // P: movw ip,:lower16:S - (P + (L1-P) + 4)
Shoaib Meenai75d616b2017-09-21 21:04:42502 0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P) + 4)
Martin Storsjo24f590f2018-08-31 08:03:33503 0xfc, 0x44, // L1: add ip, pc
504 0x60, 0x47, // bx ip
Rui Ueyama3d2bbb12016-07-09 22:52:30505 };
George Rimarec84ffc2017-05-17 07:10:59506 uint64_t S = getARMThunkDestVA(Destination);
Peter Collingbournec5391ce2018-03-29 22:32:13507 uint64_t P = getThunkTargetSym()->getVA() & ~0x1;
Peter Smith1811e482019-01-10 16:08:23508 int64_t Offset = S - P - 12;
Rui Ueyama3d2bbb12016-07-09 22:52:30509 memcpy(Buf, Data, sizeof(Data));
Shoaib Meenai75d616b2017-09-21 21:04:42510 Target->relocateOne(Buf, R_ARM_THM_MOVW_PREL_NC, Offset);
511 Target->relocateOne(Buf + 4, R_ARM_THM_MOVT_PREL, Offset);
Rui Ueyama3d2bbb12016-07-09 22:52:30512}
513
George Rimarec84ffc2017-05-17 07:10:59514void ThumbV7PILongThunk::addSymbols(ThunkSection &IS) {
Peter Collingbournec5391ce2018-03-29 22:32:13515 addSymbol(Saver.save("__ThumbV7PILongThunk_" + Destination.getName()),
516 STT_FUNC, 1, IS);
517 addSymbol("$t", STT_NOTYPE, 0, IS);
Peter Smith3a52eb02017-02-01 10:26:03518}
519
Peter Smitha8656c622018-08-20 09:37:50520void ARMV5ABSLongThunk::writeLong(uint8_t *Buf) {
521 const uint8_t Data[] = {
522 0x04, 0xf0, 0x1f, 0xe5, // ldr pc, [pc,#-4] ; L1
523 0x00, 0x00, 0x00, 0x00, // L1: .word S
524 };
525 memcpy(Buf, Data, sizeof(Data));
526 Target->relocateOne(Buf + 4, R_ARM_ABS32, getARMThunkDestVA(Destination));
527}
528
529void ARMV5ABSLongThunk::addSymbols(ThunkSection &IS) {
530 addSymbol(Saver.save("__ARMv5ABSLongThunk_" + Destination.getName()),
531 STT_FUNC, 0, IS);
532 addSymbol("$a", STT_NOTYPE, 0, IS);
533 addSymbol("$d", STT_NOTYPE, 4, IS);
534}
535
536bool ARMV5ABSLongThunk::isCompatibleWith(uint32_t RelocType) const {
537 // Thumb branch relocations can't use BLX
538 return RelocType != R_ARM_THM_JUMP19 && RelocType != R_ARM_THM_JUMP24;
539}
540
541void ARMV5PILongThunk::writeLong(uint8_t *Buf) {
542 const uint8_t Data[] = {
543 0x04, 0xc0, 0x9f, 0xe5, // P: ldr ip, [pc,#4] ; L2
544 0x0c, 0xc0, 0x8f, 0xe0, // L1: add ip, pc, ip
545 0x1c, 0xff, 0x2f, 0xe1, // bx ip
546 0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 8)
547 };
548 uint64_t S = getARMThunkDestVA(Destination);
549 uint64_t P = getThunkTargetSym()->getVA() & ~0x1;
550 memcpy(Buf, Data, sizeof(Data));
551 Target->relocateOne(Buf + 12, R_ARM_REL32, S - P - 12);
552}
553
554void ARMV5PILongThunk::addSymbols(ThunkSection &IS) {
555 addSymbol(Saver.save("__ARMV5PILongThunk_" + Destination.getName()), STT_FUNC,
556 0, IS);
557 addSymbol("$a", STT_NOTYPE, 0, IS);
558 addSymbol("$d", STT_NOTYPE, 12, IS);
559}
560
561bool ARMV5PILongThunk::isCompatibleWith(uint32_t RelocType) const {
562 // Thumb branch relocations can't use BLX
563 return RelocType != R_ARM_THM_JUMP19 && RelocType != R_ARM_THM_JUMP24;
564}
565
Peter Smith6ece0ad2018-12-17 10:33:47566void ThumbV6MABSLongThunk::writeLong(uint8_t *Buf) {
567 // Most Thumb instructions cannot access the high registers r8 - r15. As the
568 // only register we can corrupt is r12 we must instead spill a low register
569 // to the stack to use as a scratch register. We push r1 even though we
570 // don't need to get some space to use for the return address.
571 const uint8_t Data[] = {
572 0x03, 0xb4, // push {r0, r1} ; Obtain scratch registers
573 0x01, 0x48, // ldr r0, [pc, #4] ; L1
574 0x01, 0x90, // str r0, [sp, #4] ; SP + 4 = S
575 0x01, 0xbd, // pop {r0, pc} ; restore r0 and branch to dest
576 0x00, 0x00, 0x00, 0x00 // L1: .word S
577 };
578 uint64_t S = getARMThunkDestVA(Destination);
579 memcpy(Buf, Data, sizeof(Data));
580 Target->relocateOne(Buf + 8, R_ARM_ABS32, S);
581}
582
583void ThumbV6MABSLongThunk::addSymbols(ThunkSection &IS) {
584 addSymbol(Saver.save("__Thumbv6MABSLongThunk_" + Destination.getName()),
585 STT_FUNC, 1, IS);
586 addSymbol("$t", STT_NOTYPE, 0, IS);
587 addSymbol("$d", STT_NOTYPE, 8, IS);
588}
589
590void ThumbV6MPILongThunk::writeLong(uint8_t *Buf) {
591 // Most Thumb instructions cannot access the high registers r8 - r15. As the
592 // only register we can corrupt is ip (r12) we must instead spill a low
593 // register to the stack to use as a scratch register.
594 const uint8_t Data[] = {
595 0x01, 0xb4, // P: push {r0} ; Obtain scratch register
596 0x02, 0x48, // ldr r0, [pc, #8] ; L2
597 0x84, 0x46, // mov ip, r0 ; high to low register
598 0x01, 0xbc, // pop {r0} ; restore scratch register
599 0xe7, 0x44, // L1: add pc, ip ; transfer control
600 0xc0, 0x46, // nop ; pad to 4-byte boundary
601 0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 4)
602 };
603 uint64_t S = getARMThunkDestVA(Destination);
604 uint64_t P = getThunkTargetSym()->getVA() & ~0x1;
605 memcpy(Buf, Data, sizeof(Data));
606 Target->relocateOne(Buf + 12, R_ARM_REL32, S - P - 12);
607}
608
609void ThumbV6MPILongThunk::addSymbols(ThunkSection &IS) {
610 addSymbol(Saver.save("__Thumbv6MPILongThunk_" + Destination.getName()),
611 STT_FUNC, 1, IS);
612 addSymbol("$t", STT_NOTYPE, 0, IS);
613 addSymbol("$d", STT_NOTYPE, 12, IS);
614}
615
Rui Ueyamae2efadc2016-07-09 22:52:32616// Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
Peter Collingbournecebab4a2018-03-28 21:33:31617void MipsThunk::writeTo(uint8_t *Buf) {
Simon Atanasyan2e5c46c2017-07-04 15:04:30618 uint64_t S = Destination.getVA();
Fangrui Song0c483022018-03-09 18:03:22619 write32(Buf, 0x3c190000); // lui $25, %hi(func)
620 write32(Buf + 4, 0x08000000 | (S >> 2)); // j func
621 write32(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
622 write32(Buf + 12, 0x00000000); // nop
Rui Ueyamae2efadc2016-07-09 22:52:32623 Target->relocateOne(Buf, R_MIPS_HI16, S);
624 Target->relocateOne(Buf + 8, R_MIPS_LO16, S);
Rui Ueyama3d2bbb12016-07-09 22:52:30625}
Peter Smithfb05cd92016-07-08 16:10:27626
George Rimarec84ffc2017-05-17 07:10:59627void MipsThunk::addSymbols(ThunkSection &IS) {
Peter Collingbournec5391ce2018-03-29 22:32:13628 addSymbol(Saver.save("__LA25Thunk_" + Destination.getName()), STT_FUNC, 0,
629 IS);
Rui Ueyamaf20ee9f2017-01-28 00:48:06630}
Peter Smith5191c6f2017-01-27 13:10:16631
George Rimarec84ffc2017-05-17 07:10:59632InputSection *MipsThunk::getTargetInputSection() const {
Rafael Espindolad42f7e52017-11-29 18:32:57633 auto &DR = cast<Defined>(Destination);
634 return dyn_cast<InputSection>(DR.Section);
Peter Smith3a52eb02017-02-01 10:26:03635}
636
Simon Atanasyanf8db4532017-10-03 13:30:02637// Write microMIPS R2-R5 LA25 thunk code
638// to call PIC function from the non-PIC one.
Peter Collingbournecebab4a2018-03-28 21:33:31639void MicroMipsThunk::writeTo(uint8_t *Buf) {
Simon Atanasyan3a7044e2017-11-09 10:42:22640 uint64_t S = Destination.getVA() | 1;
Fangrui Song0c483022018-03-09 18:03:22641 write16(Buf, 0x41b9); // lui $25, %hi(func)
642 write16(Buf + 4, 0xd400); // j func
643 write16(Buf + 8, 0x3339); // addiu $25, $25, %lo(func)
644 write16(Buf + 12, 0x0c00); // nop
Simon Atanasyanf8db4532017-10-03 13:30:02645 Target->relocateOne(Buf, R_MICROMIPS_HI16, S);
646 Target->relocateOne(Buf + 4, R_MICROMIPS_26_S1, S);
647 Target->relocateOne(Buf + 8, R_MICROMIPS_LO16, S);
648}
649
650void MicroMipsThunk::addSymbols(ThunkSection &IS) {
Peter Collingbournec5391ce2018-03-29 22:32:13651 Defined *D = addSymbol(
652 Saver.save("__microLA25Thunk_" + Destination.getName()), STT_FUNC, 0, IS);
653 D->StOther |= STO_MIPS_MICROMIPS;
Simon Atanasyanf8db4532017-10-03 13:30:02654}
655
656InputSection *MicroMipsThunk::getTargetInputSection() const {
Rafael Espindolaea561062017-11-29 18:43:34657 auto &DR = cast<Defined>(Destination);
658 return dyn_cast<InputSection>(DR.Section);
Simon Atanasyanf8db4532017-10-03 13:30:02659}
660
661// Write microMIPS R6 LA25 thunk code
662// to call PIC function from the non-PIC one.
Peter Collingbournecebab4a2018-03-28 21:33:31663void MicroMipsR6Thunk::writeTo(uint8_t *Buf) {
Simon Atanasyan3a7044e2017-11-09 10:42:22664 uint64_t S = Destination.getVA() | 1;
Peter Collingbournec5391ce2018-03-29 22:32:13665 uint64_t P = getThunkTargetSym()->getVA();
Fangrui Song0c483022018-03-09 18:03:22666 write16(Buf, 0x1320); // lui $25, %hi(func)
667 write16(Buf + 4, 0x3339); // addiu $25, $25, %lo(func)
668 write16(Buf + 8, 0x9400); // bc func
Simon Atanasyanf8db4532017-10-03 13:30:02669 Target->relocateOne(Buf, R_MICROMIPS_HI16, S);
670 Target->relocateOne(Buf + 4, R_MICROMIPS_LO16, S);
671 Target->relocateOne(Buf + 8, R_MICROMIPS_PC26_S1, S - P - 12);
672}
673
674void MicroMipsR6Thunk::addSymbols(ThunkSection &IS) {
Peter Collingbournec5391ce2018-03-29 22:32:13675 Defined *D = addSymbol(
676 Saver.save("__microLA25Thunk_" + Destination.getName()), STT_FUNC, 0, IS);
677 D->StOther |= STO_MIPS_MICROMIPS;
Simon Atanasyanf8db4532017-10-03 13:30:02678}
679
680InputSection *MicroMipsR6Thunk::getTargetInputSection() const {
Rafael Espindolacf5dc9f2017-11-29 21:29:52681 auto &DR = cast<Defined>(Destination);
682 return dyn_cast<InputSection>(DR.Section);
Simon Atanasyanf8db4532017-10-03 13:30:02683}
684
Sean Fertile614dc112018-11-14 17:56:43685static void writePPCLoadAndBranch(uint8_t *Buf, int64_t Offset) {
686 uint16_t OffHa = (Offset + 0x8000) >> 16;
687 uint16_t OffLo = Offset & 0xffff;
Sean Fertiled2e887d2018-05-06 19:13:29688
Sean Fertile614dc112018-11-14 17:56:43689 write32(Buf + 0, 0x3d820000 | OffHa); // addis r12, r2, OffHa
690 write32(Buf + 4, 0xe98c0000 | OffLo); // ld r12, OffLo(r12)
691 write32(Buf + 8, 0x7d8903a6); // mtctr r12
692 write32(Buf + 12, 0x4e800420); // bctr
693}
694
695void PPC64PltCallStub::writeTo(uint8_t *Buf) {
696 int64_t Offset = Destination.getGotPltVA() - getPPC64TocBase();
697 // Save the TOC pointer to the save-slot reserved in the call frame.
698 write32(Buf + 0, 0xf8410018); // std r2,24(r1)
699 writePPCLoadAndBranch(Buf + 4, Offset);
Sean Fertiled2e887d2018-05-06 19:13:29700}
701
702void PPC64PltCallStub::addSymbols(ThunkSection &IS) {
703 Defined *S = addSymbol(Saver.save("__plt_" + Destination.getName()), STT_FUNC,
704 0, IS);
705 S->NeedsTocRestore = true;
706}
707
Sean Fertile614dc112018-11-14 17:56:43708void PPC64LongBranchThunk::writeTo(uint8_t *Buf) {
709 int64_t Offset = Destination.getPPC64LongBranchTableVA() - getPPC64TocBase();
710 writePPCLoadAndBranch(Buf, Offset);
711}
712
713void PPC64LongBranchThunk::addSymbols(ThunkSection &IS) {
714 addSymbol(Saver.save("__long_branch_" + Destination.getName()), STT_FUNC, 0,
715 IS);
716}
717
Rui Ueyamaf52496e2017-11-03 21:21:47718Thunk::Thunk(Symbol &D) : Destination(D), Offset(0) {}
Peter Smith3a52eb02017-02-01 10:26:03719
George Rimar7b827042017-03-16 10:40:50720Thunk::~Thunk() = default;
Peter Smithfb05cd92016-07-08 16:10:27721
Peter Smith31dddc92017-11-29 11:15:12722static Thunk *addThunkAArch64(RelType Type, Symbol &S) {
723 if (Type != R_AARCH64_CALL26 && Type != R_AARCH64_JUMP26)
724 fatal("unrecognized relocation type");
725 if (Config->Pic)
726 return make<AArch64ADRPThunk>(S);
727 return make<AArch64ABSLongThunk>(S);
728}
729
Rui Ueyama5e3d6042016-07-09 22:03:51730// Creates a thunk for Thumb-ARM interworking.
Peter Smitha8656c622018-08-20 09:37:50731// Arm Architectures v5 and v6 do not support Thumb2 technology. This means
732// - MOVT and MOVW instructions cannot be used
733// - Only Thumb relocation that can generate a Thunk is a BL, this can always
734// be transformed into a BLX
735static Thunk *addThunkPreArmv7(RelType Reloc, Symbol &S) {
736 switch (Reloc) {
737 case R_ARM_PC24:
738 case R_ARM_PLT32:
739 case R_ARM_JUMP24:
740 case R_ARM_CALL:
741 case R_ARM_THM_CALL:
742 if (Config->Pic)
743 return make<ARMV5PILongThunk>(S);
744 return make<ARMV5ABSLongThunk>(S);
745 }
746 fatal("relocation " + toString(Reloc) + " to " + toString(S) +
747 " not supported for Armv5 or Armv6 targets");
748}
749
Peter Smith6ece0ad2018-12-17 10:33:47750// Create a thunk for Thumb long branch on V6-M.
751// Arm Architecture v6-M only supports Thumb instructions. This means
752// - MOVT and MOVW instructions cannot be used.
753// - Only a limited number of instructions can access registers r8 and above
754// - No interworking support is needed (all Thumb).
755static Thunk *addThunkV6M(RelType Reloc, Symbol &S) {
756 switch (Reloc) {
757 case R_ARM_THM_JUMP19:
758 case R_ARM_THM_JUMP24:
759 case R_ARM_THM_CALL:
760 if (Config->Pic)
761 return make<ThumbV6MPILongThunk>(S);
762 return make<ThumbV6MABSLongThunk>(S);
763 }
764 fatal("relocation " + toString(Reloc) + " to " + toString(S) +
765 " not supported for Armv6-M targets");
766}
767
Peter Smitha8656c622018-08-20 09:37:50768// Creates a thunk for Thumb-ARM interworking or branch range extension.
Rui Ueyamaf52496e2017-11-03 21:21:47769static Thunk *addThunkArm(RelType Reloc, Symbol &S) {
Peter Smitha8656c622018-08-20 09:37:50770 // Decide which Thunk is needed based on:
771 // Available instruction set
772 // - An Arm Thunk can only be used if Arm state is available.
773 // - A Thumb Thunk can only be used if Thumb state is available.
774 // - Can only use a Thunk if it uses instructions that the Target supports.
775 // Relocation is branch or branch and link
776 // - Branch instructions cannot change state, can only select Thunk that
777 // starts in the same state as the caller.
778 // - Branch and link relocations can change state, can select Thunks from
779 // either Arm or Thumb.
780 // Position independent Thunks if we require position independent code.
781
Peter Smith6ece0ad2018-12-17 10:33:47782 // Handle architectures that have restrictions on the instructions that they
783 // can use in Thunks. The flags below are set by reading the BuildAttributes
784 // of the input objects. InputFiles.cpp contains the mapping from ARM
785 // architecture to flag.
Peter Smitha8656c622018-08-20 09:37:50786 if (!Config->ARMHasMovtMovw) {
787 if (!Config->ARMJ1J2BranchEncoding)
788 return addThunkPreArmv7(Reloc, S);
Peter Smith6ece0ad2018-12-17 10:33:47789 return addThunkV6M(Reloc, S);
Peter Smitha8656c622018-08-20 09:37:50790 }
791
Rui Ueyama5e3d6042016-07-09 22:03:51792 switch (Reloc) {
Peter Smithfb05cd92016-07-08 16:10:27793 case R_ARM_PC24:
794 case R_ARM_PLT32:
795 case R_ARM_JUMP24:
Peter Smith75030b6d2017-10-27 09:04:11796 case R_ARM_CALL:
Rui Ueyamad57e74b72017-03-17 23:29:01797 if (Config->Pic)
George Rimarec84ffc2017-05-17 07:10:59798 return make<ARMV7PILongThunk>(S);
799 return make<ARMV7ABSLongThunk>(S);
Peter Smithfb05cd92016-07-08 16:10:27800 case R_ARM_THM_JUMP19:
801 case R_ARM_THM_JUMP24:
Peter Smith75030b6d2017-10-27 09:04:11802 case R_ARM_THM_CALL:
Rui Ueyamad57e74b72017-03-17 23:29:01803 if (Config->Pic)
George Rimarec84ffc2017-05-17 07:10:59804 return make<ThumbV7PILongThunk>(S);
805 return make<ThumbV7ABSLongThunk>(S);
Peter Smithfb05cd92016-07-08 16:10:27806 }
Rui Ueyama5e3d6042016-07-09 22:03:51807 fatal("unrecognized relocation type");
808}
809
Rui Ueyamaf52496e2017-11-03 21:21:47810static Thunk *addThunkMips(RelType Type, Symbol &S) {
Simon Atanasyanf8db4532017-10-03 13:30:02811 if ((S.StOther & STO_MIPS_MICROMIPS) && isMipsR6())
812 return make<MicroMipsR6Thunk>(S);
813 if (S.StOther & STO_MIPS_MICROMIPS)
814 return make<MicroMipsThunk>(S);
815 return make<MipsThunk>(S);
816}
Peter Smithfb05cd92016-07-08 16:10:27817
Sean Fertiled2e887d2018-05-06 19:13:29818static Thunk *addThunkPPC64(RelType Type, Symbol &S) {
Sean Fertile614dc112018-11-14 17:56:43819 assert(Type == R_PPC64_REL24 && "unexpected relocation type for thunk");
820 if (S.isInPlt())
Sean Fertiled2e887d2018-05-06 19:13:29821 return make<PPC64PltCallStub>(S);
Sean Fertile614dc112018-11-14 17:56:43822
823 if (Config->Pic)
824 return make<PPC64PILongBranchThunk>(S);
825
826 return make<PPC64PDLongBranchThunk>(S);
Sean Fertiled2e887d2018-05-06 19:13:29827}
828
Rui Ueyamaf52496e2017-11-03 21:21:47829Thunk *addThunk(RelType Type, Symbol &S) {
Peter Smith31dddc92017-11-29 11:15:12830 if (Config->EMachine == EM_AARCH64)
831 return addThunkAArch64(Type, S);
Sean Fertiled2e887d2018-05-06 19:13:29832
833 if (Config->EMachine == EM_ARM)
Rui Ueyama67533a22017-10-11 22:49:24834 return addThunkArm(Type, S);
Sean Fertiled2e887d2018-05-06 19:13:29835
836 if (Config->EMachine == EM_MIPS)
Rui Ueyama67533a22017-10-11 22:49:24837 return addThunkMips(Type, S);
Sean Fertiled2e887d2018-05-06 19:13:29838
839 if (Config->EMachine == EM_PPC64)
840 return addThunkPPC64(Type, S);
841
842 llvm_unreachable("add Thunk only supported for ARM, Mips and PowerPC");
Peter Smithfb05cd92016-07-08 16:10:27843}
844
Eugene Zelenko22886a22016-11-05 01:00:56845} // end namespace elf
846} // end namespace lld