1 //===- Thunks.cpp --------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===---------------------------------------------------------------------===//
8 //
9 // This file contains Thunk subclasses.
10 //
11 // A thunk is a small piece of code written after an input section
12 // which is used to jump between "incompatible" functions
13 // such as MIPS PIC and non-PIC or ARM non-Thumb and Thumb functions.
14 //
15 // If a jump target is too far and its address doesn't fit to a
16 // short jump instruction, we need to create a thunk too, but we
17 // haven't supported it yet.
18 //
19 // i386 and x86-64 don't need thunks.
20 //
21 //===---------------------------------------------------------------------===//
22
23 #include "Thunks.h"
24 #include "Config.h"
25 #include "InputSection.h"
26 #include "OutputSections.h"
27 #include "Symbols.h"
28 #include "SyntheticSections.h"
29 #include "Target.h"
30 #include "lld/Common/ErrorHandler.h"
31 #include "lld/Common/Memory.h"
32 #include "llvm/BinaryFormat/ELF.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/Endian.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/MathExtras.h"
37 #include <cstdint>
38 #include <cstring>
39
40 using namespace llvm;
41 using namespace llvm::object;
42 using namespace llvm::ELF;
43 using namespace lld;
44 using namespace lld::elf;
45
46 namespace {
47
48 // AArch64 long range Thunks
49 class AArch64ABSLongThunk final : public Thunk {
50 public:
AArch64ABSLongThunk(Symbol & dest,int64_t addend)51 AArch64ABSLongThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
size()52 uint32_t size() override { return 16; }
53 void writeTo(uint8_t *buf) override;
54 void addSymbols(ThunkSection &isec) override;
55 };
56
57 class AArch64ADRPThunk final : public Thunk {
58 public:
AArch64ADRPThunk(Symbol & dest,int64_t addend)59 AArch64ADRPThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
size()60 uint32_t size() override { return 12; }
61 void writeTo(uint8_t *buf) override;
62 void addSymbols(ThunkSection &isec) override;
63 };
64
65 // Base class for ARM thunks.
66 //
67 // An ARM thunk may be either short or long. A short thunk is simply a branch
68 // (B) instruction, and it may be used to call ARM functions when the distance
69 // from the thunk to the target is less than 32MB. Long thunks can branch to any
70 // virtual address and can switch between ARM and Thumb, and they are
71 // implemented in the derived classes. This class tries to create a short thunk
72 // if the target is in range, otherwise it creates a long thunk.
73 class ARMThunk : public Thunk {
74 public:
ARMThunk(Symbol & dest)75 ARMThunk(Symbol &dest) : Thunk(dest, 0) {}
76
77 bool getMayUseShortThunk();
size()78 uint32_t size() override { return getMayUseShortThunk() ? 4 : sizeLong(); }
79 void writeTo(uint8_t *buf) override;
80 bool isCompatibleWith(const InputSection &isec,
81 const Relocation &rel) const override;
82
83 // Returns the size of a long thunk.
84 virtual uint32_t sizeLong() = 0;
85
86 // Writes a long thunk to Buf.
87 virtual void writeLong(uint8_t *buf) = 0;
88
89 private:
90 // This field tracks whether all previously considered layouts would allow
91 // this thunk to be short. If we have ever needed a long thunk, we always
92 // create a long thunk, even if the thunk may be short given the current
93 // distance to the target. We do this because transitioning from long to short
94 // can create layout oscillations in certain corner cases which would prevent
95 // the layout from converging.
96 bool mayUseShortThunk = true;
97 };
98
99 // Base class for Thumb-2 thunks.
100 //
101 // This class is similar to ARMThunk, but it uses the Thumb-2 B.W instruction
102 // which has a range of 16MB.
103 class ThumbThunk : public Thunk {
104 public:
ThumbThunk(Symbol & dest)105 ThumbThunk(Symbol &dest) : Thunk(dest, 0) { alignment = 2; }
106
107 bool getMayUseShortThunk();
size()108 uint32_t size() override { return getMayUseShortThunk() ? 4 : sizeLong(); }
109 void writeTo(uint8_t *buf) override;
110 bool isCompatibleWith(const InputSection &isec,
111 const Relocation &rel) 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
119 private:
120 // See comment in ARMThunk above.
121 bool mayUseShortThunk = true;
122 };
123
124 // Specific ARM Thunk implementations. The naming convention is:
125 // Source State, TargetState, Target Requirement, ABS or PI, Range
126 class ARMV7ABSLongThunk final : public ARMThunk {
127 public:
ARMV7ABSLongThunk(Symbol & dest)128 ARMV7ABSLongThunk(Symbol &dest) : ARMThunk(dest) {}
129
sizeLong()130 uint32_t sizeLong() override { return 12; }
131 void writeLong(uint8_t *buf) override;
132 void addSymbols(ThunkSection &isec) override;
133 };
134
135 class ARMV7PILongThunk final : public ARMThunk {
136 public:
ARMV7PILongThunk(Symbol & dest)137 ARMV7PILongThunk(Symbol &dest) : ARMThunk(dest) {}
138
sizeLong()139 uint32_t sizeLong() override { return 16; }
140 void writeLong(uint8_t *buf) override;
141 void addSymbols(ThunkSection &isec) override;
142 };
143
144 class ThumbV7ABSLongThunk final : public ThumbThunk {
145 public:
ThumbV7ABSLongThunk(Symbol & dest)146 ThumbV7ABSLongThunk(Symbol &dest) : ThumbThunk(dest) {}
147
sizeLong()148 uint32_t sizeLong() override { return 10; }
149 void writeLong(uint8_t *buf) override;
150 void addSymbols(ThunkSection &isec) override;
151 };
152
153 class ThumbV7PILongThunk final : public ThumbThunk {
154 public:
ThumbV7PILongThunk(Symbol & dest)155 ThumbV7PILongThunk(Symbol &dest) : ThumbThunk(dest) {}
156
sizeLong()157 uint32_t sizeLong() override { return 12; }
158 void writeLong(uint8_t *buf) override;
159 void addSymbols(ThunkSection &isec) override;
160 };
161
162 // 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
167 class ARMV5ABSLongThunk final : public ARMThunk {
168 public:
ARMV5ABSLongThunk(Symbol & dest)169 ARMV5ABSLongThunk(Symbol &dest) : ARMThunk(dest) {}
170
sizeLong()171 uint32_t sizeLong() override { return 8; }
172 void writeLong(uint8_t *buf) override;
173 void addSymbols(ThunkSection &isec) override;
174 bool isCompatibleWith(const InputSection &isec,
175 const Relocation &rel) const override;
176 };
177
178 class ARMV5PILongThunk final : public ARMThunk {
179 public:
ARMV5PILongThunk(Symbol & dest)180 ARMV5PILongThunk(Symbol &dest) : ARMThunk(dest) {}
181
sizeLong()182 uint32_t sizeLong() override { return 16; }
183 void writeLong(uint8_t *buf) override;
184 void addSymbols(ThunkSection &isec) override;
185 bool isCompatibleWith(const InputSection &isec,
186 const Relocation &rel) const override;
187 };
188
189 // Implementations of Thunks for Arm v6-M. Only Thumb instructions are permitted
190 class ThumbV6MABSLongThunk final : public ThumbThunk {
191 public:
ThumbV6MABSLongThunk(Symbol & dest)192 ThumbV6MABSLongThunk(Symbol &dest) : ThumbThunk(dest) {}
193
sizeLong()194 uint32_t sizeLong() override { return 12; }
195 void writeLong(uint8_t *buf) override;
196 void addSymbols(ThunkSection &isec) override;
197 };
198
199 class ThumbV6MPILongThunk final : public ThumbThunk {
200 public:
ThumbV6MPILongThunk(Symbol & dest)201 ThumbV6MPILongThunk(Symbol &dest) : ThumbThunk(dest) {}
202
sizeLong()203 uint32_t sizeLong() override { return 16; }
204 void writeLong(uint8_t *buf) override;
205 void addSymbols(ThunkSection &isec) override;
206 };
207
208 // MIPS LA25 thunk
209 class MipsThunk final : public Thunk {
210 public:
MipsThunk(Symbol & dest)211 MipsThunk(Symbol &dest) : Thunk(dest, 0) {}
212
size()213 uint32_t size() override { return 16; }
214 void writeTo(uint8_t *buf) override;
215 void addSymbols(ThunkSection &isec) override;
216 InputSection *getTargetInputSection() const override;
217 };
218
219 // microMIPS R2-R5 LA25 thunk
220 class MicroMipsThunk final : public Thunk {
221 public:
MicroMipsThunk(Symbol & dest)222 MicroMipsThunk(Symbol &dest) : Thunk(dest, 0) {}
223
size()224 uint32_t size() override { return 14; }
225 void writeTo(uint8_t *buf) override;
226 void addSymbols(ThunkSection &isec) override;
227 InputSection *getTargetInputSection() const override;
228 };
229
230 // microMIPS R6 LA25 thunk
231 class MicroMipsR6Thunk final : public Thunk {
232 public:
MicroMipsR6Thunk(Symbol & dest)233 MicroMipsR6Thunk(Symbol &dest) : Thunk(dest, 0) {}
234
size()235 uint32_t size() override { return 12; }
236 void writeTo(uint8_t *buf) override;
237 void addSymbols(ThunkSection &isec) override;
238 InputSection *getTargetInputSection() const override;
239 };
240
241 class PPC32PltCallStub final : public Thunk {
242 public:
243 // For R_PPC_PLTREL24, Thunk::addend records the addend which will be used to
244 // decide the offsets in the call stub.
PPC32PltCallStub(const InputSection & isec,const Relocation & rel,Symbol & dest)245 PPC32PltCallStub(const InputSection &isec, const Relocation &rel,
246 Symbol &dest)
247 : Thunk(dest, rel.addend), file(isec.file) {}
size()248 uint32_t size() override { return 16; }
249 void writeTo(uint8_t *buf) override;
250 void addSymbols(ThunkSection &isec) override;
251 bool isCompatibleWith(const InputSection &isec, const Relocation &rel) const override;
252
253 private:
254 // Records the call site of the call stub.
255 const InputFile *file;
256 };
257
258 class PPC32LongThunk final : public Thunk {
259 public:
PPC32LongThunk(Symbol & dest,int64_t addend)260 PPC32LongThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
size()261 uint32_t size() override { return config->isPic ? 32 : 16; }
262 void writeTo(uint8_t *buf) override;
263 void addSymbols(ThunkSection &isec) override;
264 };
265
266 // PPC64 Plt call stubs.
267 // Any call site that needs to call through a plt entry needs a call stub in
268 // the .text section. The call stub is responsible for:
269 // 1) Saving the toc-pointer to the stack.
270 // 2) Loading the target functions address from the procedure linkage table into
271 // r12 for use by the target functions global entry point, and into the count
272 // register.
273 // 3) Transferring control to the target function through an indirect branch.
274 class PPC64PltCallStub final : public Thunk {
275 public:
PPC64PltCallStub(Symbol & dest)276 PPC64PltCallStub(Symbol &dest) : Thunk(dest, 0) {}
size()277 uint32_t size() override { return 20; }
278 void writeTo(uint8_t *buf) override;
279 void addSymbols(ThunkSection &isec) override;
280 bool isCompatibleWith(const InputSection &isec,
281 const Relocation &rel) const override;
282 };
283
284 // PPC64 R2 Save Stub
285 // When the caller requires a valid R2 TOC pointer but the callee does not
286 // require a TOC pointer and the callee cannot guarantee that it doesn't
287 // clobber R2 then we need to save R2. This stub:
288 // 1) Saves the TOC pointer to the stack.
289 // 2) Tail calls the callee.
290 class PPC64R2SaveStub final : public Thunk {
291 public:
PPC64R2SaveStub(Symbol & dest,int64_t addend)292 PPC64R2SaveStub(Symbol &dest, int64_t addend) : Thunk(dest, addend) {
293 alignment = 16;
294 }
295
296 // To prevent oscillations in layout when moving from short to long thunks
297 // we make sure that once a thunk has been set to long it cannot go back.
getMayUseShortThunk()298 bool getMayUseShortThunk() {
299 if (!mayUseShortThunk)
300 return false;
301 if (!isInt<26>(computeOffset())) {
302 mayUseShortThunk = false;
303 return false;
304 }
305 return true;
306 }
size()307 uint32_t size() override { return getMayUseShortThunk() ? 8 : 20; }
308 void writeTo(uint8_t *buf) override;
309 void addSymbols(ThunkSection &isec) override;
310
311 private:
312 // Transitioning from long to short can create layout oscillations in
313 // certain corner cases which would prevent the layout from converging.
314 // This is similar to the handling for ARMThunk.
315 bool mayUseShortThunk = true;
computeOffset() const316 int64_t computeOffset() const {
317 return destination.getVA() - (getThunkTargetSym()->getVA() + 4);
318 }
319 };
320
321 // PPC64 R12 Setup Stub
322 // When a caller that does not maintain a toc-pointer performs a local call to
323 // a callee which requires a toc-pointer then we need this stub to place the
324 // callee's global entry point into r12 without a save of R2.
325 class PPC64R12SetupStub final : public Thunk {
326 public:
PPC64R12SetupStub(Symbol & dest)327 PPC64R12SetupStub(Symbol &dest) : Thunk(dest, 0) { alignment = 16; }
size()328 uint32_t size() override { return 16; }
329 void writeTo(uint8_t *buf) override;
330 void addSymbols(ThunkSection &isec) override;
331 };
332
333 // PPC64 PC-relative PLT Stub
334 // When a caller that does not maintain a toc-pointer performs an extern call
335 // then this stub is needed for:
336 // 1) Loading the target functions address from the procedure linkage table into
337 // r12 for use by the target functions global entry point, and into the count
338 // register with pc-relative instructions.
339 // 2) Transferring control to the target function through an indirect branch.
340 class PPC64PCRelPLTStub final : public Thunk {
341 public:
PPC64PCRelPLTStub(Symbol & dest)342 PPC64PCRelPLTStub(Symbol &dest) : Thunk(dest, 0) { alignment = 16; }
size()343 uint32_t size() override { return 16; }
344 void writeTo(uint8_t *buf) override;
345 void addSymbols(ThunkSection &isec) override;
346 bool isCompatibleWith(const InputSection &isec,
347 const Relocation &rel) const override;
348 };
349
350 // A bl instruction uses a signed 24 bit offset, with an implicit 4 byte
351 // alignment. This gives a possible 26 bits of 'reach'. If the call offset is
352 // larger than that we need to emit a long-branch thunk. The target address
353 // of the callee is stored in a table to be accessed TOC-relative. Since the
354 // call must be local (a non-local call will have a PltCallStub instead) the
355 // table stores the address of the callee's local entry point. For
356 // position-independent code a corresponding relative dynamic relocation is
357 // used.
358 class PPC64LongBranchThunk : public Thunk {
359 public:
size()360 uint32_t size() override { return 16; }
361 void writeTo(uint8_t *buf) override;
362 void addSymbols(ThunkSection &isec) override;
363 bool isCompatibleWith(const InputSection &isec,
364 const Relocation &rel) const override;
365
366 protected:
PPC64LongBranchThunk(Symbol & dest,int64_t addend)367 PPC64LongBranchThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
368 };
369
370 class PPC64PILongBranchThunk final : public PPC64LongBranchThunk {
371 public:
PPC64PILongBranchThunk(Symbol & dest,int64_t addend)372 PPC64PILongBranchThunk(Symbol &dest, int64_t addend)
373 : PPC64LongBranchThunk(dest, addend) {
374 assert(!dest.isPreemptible);
375 if (Optional<uint32_t> index =
376 in.ppc64LongBranchTarget->addEntry(&dest, addend)) {
377 mainPart->relaDyn->addReloc(
378 {target->relativeRel, in.ppc64LongBranchTarget, *index * UINT64_C(8),
379 true, &dest,
380 addend + getPPC64GlobalEntryToLocalEntryOffset(dest.stOther)});
381 }
382 }
383 };
384
385 class PPC64PDLongBranchThunk final : public PPC64LongBranchThunk {
386 public:
PPC64PDLongBranchThunk(Symbol & dest,int64_t addend)387 PPC64PDLongBranchThunk(Symbol &dest, int64_t addend)
388 : PPC64LongBranchThunk(dest, addend) {
389 in.ppc64LongBranchTarget->addEntry(&dest, addend);
390 }
391 };
392
393 // A bl instruction uses a signed 24 bit offset, with an implicit 4 byte
394 // alignment. This gives a possible 26 bits of 'reach'. If the caller and
395 // callee do not use toc and the call offset is larger than 26 bits,
396 // we need to emit a pc-rel based long-branch thunk. The target address of
397 // the callee is computed with a PC-relative offset.
398 class PPC64PCRelLongBranchThunk final : public Thunk {
399 public:
PPC64PCRelLongBranchThunk(Symbol & dest,int64_t addend)400 PPC64PCRelLongBranchThunk(Symbol &dest, int64_t addend)
401 : Thunk(dest, addend) {
402 alignment = 16;
403 }
size()404 uint32_t size() override { return 16; }
405 void writeTo(uint8_t *buf) override;
406 void addSymbols(ThunkSection &isec) override;
407 bool isCompatibleWith(const InputSection &isec,
408 const Relocation &rel) const override;
409 };
410
411 } // end anonymous namespace
412
addSymbol(StringRef name,uint8_t type,uint64_t value,InputSectionBase & section)413 Defined *Thunk::addSymbol(StringRef name, uint8_t type, uint64_t value,
414 InputSectionBase §ion) {
415 Defined *d = addSyntheticLocal(name, type, value, /*size=*/0, section);
416 syms.push_back(d);
417 return d;
418 }
419
setOffset(uint64_t newOffset)420 void Thunk::setOffset(uint64_t newOffset) {
421 for (Defined *d : syms)
422 d->value = d->value - offset + newOffset;
423 offset = newOffset;
424 }
425
426 // AArch64 long range Thunks
427
getAArch64ThunkDestVA(const Symbol & s,int64_t a)428 static uint64_t getAArch64ThunkDestVA(const Symbol &s, int64_t a) {
429 uint64_t v = s.isInPlt() ? s.getPltVA() : s.getVA(a);
430 return v;
431 }
432
writeTo(uint8_t * buf)433 void AArch64ABSLongThunk::writeTo(uint8_t *buf) {
434 const uint8_t data[] = {
435 0x50, 0x00, 0x00, 0x58, // ldr x16, L0
436 0x00, 0x02, 0x1f, 0xd6, // br x16
437 0x00, 0x00, 0x00, 0x00, // L0: .xword S
438 0x00, 0x00, 0x00, 0x00,
439 };
440 uint64_t s = getAArch64ThunkDestVA(destination, addend);
441 memcpy(buf, data, sizeof(data));
442 target->relocateNoSym(buf + 8, R_AARCH64_ABS64, s);
443 }
444
addSymbols(ThunkSection & isec)445 void AArch64ABSLongThunk::addSymbols(ThunkSection &isec) {
446 addSymbol(saver.save("__AArch64AbsLongThunk_" + destination.getName()),
447 STT_FUNC, 0, isec);
448 addSymbol("$x", STT_NOTYPE, 0, isec);
449 addSymbol("$d", STT_NOTYPE, 8, isec);
450 }
451
452 // This Thunk has a maximum range of 4Gb, this is sufficient for all programs
453 // using the small code model, including pc-relative ones. At time of writing
454 // clang and gcc do not support the large code model for position independent
455 // code so it is safe to use this for position independent thunks without
456 // worrying about the destination being more than 4Gb away.
writeTo(uint8_t * buf)457 void AArch64ADRPThunk::writeTo(uint8_t *buf) {
458 const uint8_t data[] = {
459 0x10, 0x00, 0x00, 0x90, // adrp x16, Dest R_AARCH64_ADR_PREL_PG_HI21(Dest)
460 0x10, 0x02, 0x00, 0x91, // add x16, x16, R_AARCH64_ADD_ABS_LO12_NC(Dest)
461 0x00, 0x02, 0x1f, 0xd6, // br x16
462 };
463 uint64_t s = getAArch64ThunkDestVA(destination, addend);
464 uint64_t p = getThunkTargetSym()->getVA();
465 memcpy(buf, data, sizeof(data));
466 target->relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21,
467 getAArch64Page(s) - getAArch64Page(p));
468 target->relocateNoSym(buf + 4, R_AARCH64_ADD_ABS_LO12_NC, s);
469 }
470
addSymbols(ThunkSection & isec)471 void AArch64ADRPThunk::addSymbols(ThunkSection &isec) {
472 addSymbol(saver.save("__AArch64ADRPThunk_" + destination.getName()), STT_FUNC,
473 0, isec);
474 addSymbol("$x", STT_NOTYPE, 0, isec);
475 }
476
477 // ARM Target Thunks
getARMThunkDestVA(const Symbol & s)478 static uint64_t getARMThunkDestVA(const Symbol &s) {
479 uint64_t v = s.isInPlt() ? s.getPltVA() : s.getVA();
480 return SignExtend64<32>(v);
481 }
482
483 // This function returns true if the target is not Thumb and is within 2^26, and
484 // it has not previously returned false (see comment for mayUseShortThunk).
getMayUseShortThunk()485 bool ARMThunk::getMayUseShortThunk() {
486 if (!mayUseShortThunk)
487 return false;
488 uint64_t s = getARMThunkDestVA(destination);
489 if (s & 1) {
490 mayUseShortThunk = false;
491 return false;
492 }
493 uint64_t p = getThunkTargetSym()->getVA();
494 int64_t offset = s - p - 8;
495 mayUseShortThunk = llvm::isInt<26>(offset);
496 return mayUseShortThunk;
497 }
498
writeTo(uint8_t * buf)499 void ARMThunk::writeTo(uint8_t *buf) {
500 if (!getMayUseShortThunk()) {
501 writeLong(buf);
502 return;
503 }
504
505 uint64_t s = getARMThunkDestVA(destination);
506 uint64_t p = getThunkTargetSym()->getVA();
507 int64_t offset = s - p - 8;
508 const uint8_t data[] = {
509 0x00, 0x00, 0x00, 0xea, // b S
510 };
511 memcpy(buf, data, sizeof(data));
512 target->relocateNoSym(buf, R_ARM_JUMP24, offset);
513 }
514
isCompatibleWith(const InputSection & isec,const Relocation & rel) const515 bool ARMThunk::isCompatibleWith(const InputSection &isec,
516 const Relocation &rel) const {
517 // Thumb branch relocations can't use BLX
518 return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;
519 }
520
521 // This function returns true if the target is Thumb and is within 2^25, and
522 // it has not previously returned false (see comment for mayUseShortThunk).
getMayUseShortThunk()523 bool ThumbThunk::getMayUseShortThunk() {
524 if (!mayUseShortThunk)
525 return false;
526 uint64_t s = getARMThunkDestVA(destination);
527 if ((s & 1) == 0) {
528 mayUseShortThunk = false;
529 return false;
530 }
531 uint64_t p = getThunkTargetSym()->getVA() & ~1;
532 int64_t offset = s - p - 4;
533 mayUseShortThunk = llvm::isInt<25>(offset);
534 return mayUseShortThunk;
535 }
536
writeTo(uint8_t * buf)537 void ThumbThunk::writeTo(uint8_t *buf) {
538 if (!getMayUseShortThunk()) {
539 writeLong(buf);
540 return;
541 }
542
543 uint64_t s = getARMThunkDestVA(destination);
544 uint64_t p = getThunkTargetSym()->getVA();
545 int64_t offset = s - p - 4;
546 const uint8_t data[] = {
547 0x00, 0xf0, 0x00, 0xb0, // b.w S
548 };
549 memcpy(buf, data, sizeof(data));
550 target->relocateNoSym(buf, R_ARM_THM_JUMP24, offset);
551 }
552
isCompatibleWith(const InputSection & isec,const Relocation & rel) const553 bool ThumbThunk::isCompatibleWith(const InputSection &isec,
554 const Relocation &rel) const {
555 // ARM branch relocations can't use BLX
556 return rel.type != R_ARM_JUMP24 && rel.type != R_ARM_PC24 && rel.type != R_ARM_PLT32;
557 }
558
writeLong(uint8_t * buf)559 void ARMV7ABSLongThunk::writeLong(uint8_t *buf) {
560 const uint8_t data[] = {
561 0x00, 0xc0, 0x00, 0xe3, // movw ip,:lower16:S
562 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S
563 0x1c, 0xff, 0x2f, 0xe1, // bx ip
564 };
565 uint64_t s = getARMThunkDestVA(destination);
566 memcpy(buf, data, sizeof(data));
567 target->relocateNoSym(buf, R_ARM_MOVW_ABS_NC, s);
568 target->relocateNoSym(buf + 4, R_ARM_MOVT_ABS, s);
569 }
570
addSymbols(ThunkSection & isec)571 void ARMV7ABSLongThunk::addSymbols(ThunkSection &isec) {
572 addSymbol(saver.save("__ARMv7ABSLongThunk_" + destination.getName()),
573 STT_FUNC, 0, isec);
574 addSymbol("$a", STT_NOTYPE, 0, isec);
575 }
576
writeLong(uint8_t * buf)577 void ThumbV7ABSLongThunk::writeLong(uint8_t *buf) {
578 const uint8_t data[] = {
579 0x40, 0xf2, 0x00, 0x0c, // movw ip, :lower16:S
580 0xc0, 0xf2, 0x00, 0x0c, // movt ip, :upper16:S
581 0x60, 0x47, // bx ip
582 };
583 uint64_t s = getARMThunkDestVA(destination);
584 memcpy(buf, data, sizeof(data));
585 target->relocateNoSym(buf, R_ARM_THM_MOVW_ABS_NC, s);
586 target->relocateNoSym(buf + 4, R_ARM_THM_MOVT_ABS, s);
587 }
588
addSymbols(ThunkSection & isec)589 void ThumbV7ABSLongThunk::addSymbols(ThunkSection &isec) {
590 addSymbol(saver.save("__Thumbv7ABSLongThunk_" + destination.getName()),
591 STT_FUNC, 1, isec);
592 addSymbol("$t", STT_NOTYPE, 0, isec);
593 }
594
writeLong(uint8_t * buf)595 void ARMV7PILongThunk::writeLong(uint8_t *buf) {
596 const uint8_t data[] = {
597 0xf0, 0xcf, 0x0f, 0xe3, // P: movw ip,:lower16:S - (P + (L1-P) + 8)
598 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S - (P + (L1-P) + 8)
599 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
600 0x1c, 0xff, 0x2f, 0xe1, // bx ip
601 };
602 uint64_t s = getARMThunkDestVA(destination);
603 uint64_t p = getThunkTargetSym()->getVA();
604 int64_t offset = s - p - 16;
605 memcpy(buf, data, sizeof(data));
606 target->relocateNoSym(buf, R_ARM_MOVW_PREL_NC, offset);
607 target->relocateNoSym(buf + 4, R_ARM_MOVT_PREL, offset);
608 }
609
addSymbols(ThunkSection & isec)610 void ARMV7PILongThunk::addSymbols(ThunkSection &isec) {
611 addSymbol(saver.save("__ARMV7PILongThunk_" + destination.getName()), STT_FUNC,
612 0, isec);
613 addSymbol("$a", STT_NOTYPE, 0, isec);
614 }
615
writeLong(uint8_t * buf)616 void ThumbV7PILongThunk::writeLong(uint8_t *buf) {
617 const uint8_t data[] = {
618 0x4f, 0xf6, 0xf4, 0x7c, // P: movw ip,:lower16:S - (P + (L1-P) + 4)
619 0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P) + 4)
620 0xfc, 0x44, // L1: add ip, pc
621 0x60, 0x47, // bx ip
622 };
623 uint64_t s = getARMThunkDestVA(destination);
624 uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
625 int64_t offset = s - p - 12;
626 memcpy(buf, data, sizeof(data));
627 target->relocateNoSym(buf, R_ARM_THM_MOVW_PREL_NC, offset);
628 target->relocateNoSym(buf + 4, R_ARM_THM_MOVT_PREL, offset);
629 }
630
addSymbols(ThunkSection & isec)631 void ThumbV7PILongThunk::addSymbols(ThunkSection &isec) {
632 addSymbol(saver.save("__ThumbV7PILongThunk_" + destination.getName()),
633 STT_FUNC, 1, isec);
634 addSymbol("$t", STT_NOTYPE, 0, isec);
635 }
636
writeLong(uint8_t * buf)637 void ARMV5ABSLongThunk::writeLong(uint8_t *buf) {
638 const uint8_t data[] = {
639 0x04, 0xf0, 0x1f, 0xe5, // ldr pc, [pc,#-4] ; L1
640 0x00, 0x00, 0x00, 0x00, // L1: .word S
641 };
642 memcpy(buf, data, sizeof(data));
643 target->relocateNoSym(buf + 4, R_ARM_ABS32, getARMThunkDestVA(destination));
644 }
645
addSymbols(ThunkSection & isec)646 void ARMV5ABSLongThunk::addSymbols(ThunkSection &isec) {
647 addSymbol(saver.save("__ARMv5ABSLongThunk_" + destination.getName()),
648 STT_FUNC, 0, isec);
649 addSymbol("$a", STT_NOTYPE, 0, isec);
650 addSymbol("$d", STT_NOTYPE, 4, isec);
651 }
652
isCompatibleWith(const InputSection & isec,const Relocation & rel) const653 bool ARMV5ABSLongThunk::isCompatibleWith(const InputSection &isec,
654 const Relocation &rel) const {
655 // Thumb branch relocations can't use BLX
656 return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;
657 }
658
writeLong(uint8_t * buf)659 void ARMV5PILongThunk::writeLong(uint8_t *buf) {
660 const uint8_t data[] = {
661 0x04, 0xc0, 0x9f, 0xe5, // P: ldr ip, [pc,#4] ; L2
662 0x0c, 0xc0, 0x8f, 0xe0, // L1: add ip, pc, ip
663 0x1c, 0xff, 0x2f, 0xe1, // bx ip
664 0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 8)
665 };
666 uint64_t s = getARMThunkDestVA(destination);
667 uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
668 memcpy(buf, data, sizeof(data));
669 target->relocateNoSym(buf + 12, R_ARM_REL32, s - p - 12);
670 }
671
addSymbols(ThunkSection & isec)672 void ARMV5PILongThunk::addSymbols(ThunkSection &isec) {
673 addSymbol(saver.save("__ARMV5PILongThunk_" + destination.getName()), STT_FUNC,
674 0, isec);
675 addSymbol("$a", STT_NOTYPE, 0, isec);
676 addSymbol("$d", STT_NOTYPE, 12, isec);
677 }
678
isCompatibleWith(const InputSection & isec,const Relocation & rel) const679 bool ARMV5PILongThunk::isCompatibleWith(const InputSection &isec,
680 const Relocation &rel) const {
681 // Thumb branch relocations can't use BLX
682 return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;
683 }
684
writeLong(uint8_t * buf)685 void ThumbV6MABSLongThunk::writeLong(uint8_t *buf) {
686 // Most Thumb instructions cannot access the high registers r8 - r15. As the
687 // only register we can corrupt is r12 we must instead spill a low register
688 // to the stack to use as a scratch register. We push r1 even though we
689 // don't need to get some space to use for the return address.
690 const uint8_t data[] = {
691 0x03, 0xb4, // push {r0, r1} ; Obtain scratch registers
692 0x01, 0x48, // ldr r0, [pc, #4] ; L1
693 0x01, 0x90, // str r0, [sp, #4] ; SP + 4 = S
694 0x01, 0xbd, // pop {r0, pc} ; restore r0 and branch to dest
695 0x00, 0x00, 0x00, 0x00 // L1: .word S
696 };
697 uint64_t s = getARMThunkDestVA(destination);
698 memcpy(buf, data, sizeof(data));
699 target->relocateNoSym(buf + 8, R_ARM_ABS32, s);
700 }
701
addSymbols(ThunkSection & isec)702 void ThumbV6MABSLongThunk::addSymbols(ThunkSection &isec) {
703 addSymbol(saver.save("__Thumbv6MABSLongThunk_" + destination.getName()),
704 STT_FUNC, 1, isec);
705 addSymbol("$t", STT_NOTYPE, 0, isec);
706 addSymbol("$d", STT_NOTYPE, 8, isec);
707 }
708
writeLong(uint8_t * buf)709 void ThumbV6MPILongThunk::writeLong(uint8_t *buf) {
710 // Most Thumb instructions cannot access the high registers r8 - r15. As the
711 // only register we can corrupt is ip (r12) we must instead spill a low
712 // register to the stack to use as a scratch register.
713 const uint8_t data[] = {
714 0x01, 0xb4, // P: push {r0} ; Obtain scratch register
715 0x02, 0x48, // ldr r0, [pc, #8] ; L2
716 0x84, 0x46, // mov ip, r0 ; high to low register
717 0x01, 0xbc, // pop {r0} ; restore scratch register
718 0xe7, 0x44, // L1: add pc, ip ; transfer control
719 0xc0, 0x46, // nop ; pad to 4-byte boundary
720 0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 4)
721 };
722 uint64_t s = getARMThunkDestVA(destination);
723 uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
724 memcpy(buf, data, sizeof(data));
725 target->relocateNoSym(buf + 12, R_ARM_REL32, s - p - 12);
726 }
727
addSymbols(ThunkSection & isec)728 void ThumbV6MPILongThunk::addSymbols(ThunkSection &isec) {
729 addSymbol(saver.save("__Thumbv6MPILongThunk_" + destination.getName()),
730 STT_FUNC, 1, isec);
731 addSymbol("$t", STT_NOTYPE, 0, isec);
732 addSymbol("$d", STT_NOTYPE, 12, isec);
733 }
734
735 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
writeTo(uint8_t * buf)736 void MipsThunk::writeTo(uint8_t *buf) {
737 uint64_t s = destination.getVA();
738 write32(buf, 0x3c190000); // lui $25, %hi(func)
739 write32(buf + 4, 0x08000000 | (s >> 2)); // j func
740 write32(buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
741 write32(buf + 12, 0x00000000); // nop
742 target->relocateNoSym(buf, R_MIPS_HI16, s);
743 target->relocateNoSym(buf + 8, R_MIPS_LO16, s);
744 }
745
addSymbols(ThunkSection & isec)746 void MipsThunk::addSymbols(ThunkSection &isec) {
747 addSymbol(saver.save("__LA25Thunk_" + destination.getName()), STT_FUNC, 0,
748 isec);
749 }
750
getTargetInputSection() const751 InputSection *MipsThunk::getTargetInputSection() const {
752 auto &dr = cast<Defined>(destination);
753 return dyn_cast<InputSection>(dr.section);
754 }
755
756 // Write microMIPS R2-R5 LA25 thunk code
757 // to call PIC function from the non-PIC one.
writeTo(uint8_t * buf)758 void MicroMipsThunk::writeTo(uint8_t *buf) {
759 uint64_t s = destination.getVA();
760 write16(buf, 0x41b9); // lui $25, %hi(func)
761 write16(buf + 4, 0xd400); // j func
762 write16(buf + 8, 0x3339); // addiu $25, $25, %lo(func)
763 write16(buf + 12, 0x0c00); // nop
764 target->relocateNoSym(buf, R_MICROMIPS_HI16, s);
765 target->relocateNoSym(buf + 4, R_MICROMIPS_26_S1, s);
766 target->relocateNoSym(buf + 8, R_MICROMIPS_LO16, s);
767 }
768
addSymbols(ThunkSection & isec)769 void MicroMipsThunk::addSymbols(ThunkSection &isec) {
770 Defined *d = addSymbol(
771 saver.save("__microLA25Thunk_" + destination.getName()), STT_FUNC, 0, isec);
772 d->stOther |= STO_MIPS_MICROMIPS;
773 }
774
getTargetInputSection() const775 InputSection *MicroMipsThunk::getTargetInputSection() const {
776 auto &dr = cast<Defined>(destination);
777 return dyn_cast<InputSection>(dr.section);
778 }
779
780 // Write microMIPS R6 LA25 thunk code
781 // to call PIC function from the non-PIC one.
writeTo(uint8_t * buf)782 void MicroMipsR6Thunk::writeTo(uint8_t *buf) {
783 uint64_t s = destination.getVA();
784 uint64_t p = getThunkTargetSym()->getVA();
785 write16(buf, 0x1320); // lui $25, %hi(func)
786 write16(buf + 4, 0x3339); // addiu $25, $25, %lo(func)
787 write16(buf + 8, 0x9400); // bc func
788 target->relocateNoSym(buf, R_MICROMIPS_HI16, s);
789 target->relocateNoSym(buf + 4, R_MICROMIPS_LO16, s);
790 target->relocateNoSym(buf + 8, R_MICROMIPS_PC26_S1, s - p - 12);
791 }
792
addSymbols(ThunkSection & isec)793 void MicroMipsR6Thunk::addSymbols(ThunkSection &isec) {
794 Defined *d = addSymbol(
795 saver.save("__microLA25Thunk_" + destination.getName()), STT_FUNC, 0, isec);
796 d->stOther |= STO_MIPS_MICROMIPS;
797 }
798
getTargetInputSection() const799 InputSection *MicroMipsR6Thunk::getTargetInputSection() const {
800 auto &dr = cast<Defined>(destination);
801 return dyn_cast<InputSection>(dr.section);
802 }
803
writePPC32PltCallStub(uint8_t * buf,uint64_t gotPltVA,const InputFile * file,int64_t addend)804 void elf::writePPC32PltCallStub(uint8_t *buf, uint64_t gotPltVA,
805 const InputFile *file, int64_t addend) {
806 if (!config->isPic) {
807 write32(buf + 0, 0x3d600000 | (gotPltVA + 0x8000) >> 16); // lis r11,ha
808 write32(buf + 4, 0x816b0000 | (uint16_t)gotPltVA); // lwz r11,l(r11)
809 write32(buf + 8, 0x7d6903a6); // mtctr r11
810 write32(buf + 12, 0x4e800420); // bctr
811 return;
812 }
813 uint32_t offset;
814 if (addend >= 0x8000) {
815 // The stub loads an address relative to r30 (.got2+Addend). Addend is
816 // almost always 0x8000. The address of .got2 is different in another object
817 // file, so a stub cannot be shared.
818 offset = gotPltVA - (in.ppc32Got2->getParent()->getVA() +
819 file->ppc32Got2OutSecOff + addend);
820 } else {
821 // The stub loads an address relative to _GLOBAL_OFFSET_TABLE_ (which is
822 // currently the address of .got).
823 offset = gotPltVA - in.got->getVA();
824 }
825 uint16_t ha = (offset + 0x8000) >> 16, l = (uint16_t)offset;
826 if (ha == 0) {
827 write32(buf + 0, 0x817e0000 | l); // lwz r11,l(r30)
828 write32(buf + 4, 0x7d6903a6); // mtctr r11
829 write32(buf + 8, 0x4e800420); // bctr
830 write32(buf + 12, 0x60000000); // nop
831 } else {
832 write32(buf + 0, 0x3d7e0000 | ha); // addis r11,r30,ha
833 write32(buf + 4, 0x816b0000 | l); // lwz r11,l(r11)
834 write32(buf + 8, 0x7d6903a6); // mtctr r11
835 write32(buf + 12, 0x4e800420); // bctr
836 }
837 }
838
writeTo(uint8_t * buf)839 void PPC32PltCallStub::writeTo(uint8_t *buf) {
840 writePPC32PltCallStub(buf, destination.getGotPltVA(), file, addend);
841 }
842
addSymbols(ThunkSection & isec)843 void PPC32PltCallStub::addSymbols(ThunkSection &isec) {
844 std::string buf;
845 raw_string_ostream os(buf);
846 os << format_hex_no_prefix(addend, 8);
847 if (!config->isPic)
848 os << ".plt_call32.";
849 else if (addend >= 0x8000)
850 os << ".got2.plt_pic32.";
851 else
852 os << ".plt_pic32.";
853 os << destination.getName();
854 addSymbol(saver.save(os.str()), STT_FUNC, 0, isec);
855 }
856
isCompatibleWith(const InputSection & isec,const Relocation & rel) const857 bool PPC32PltCallStub::isCompatibleWith(const InputSection &isec,
858 const Relocation &rel) const {
859 return !config->isPic || (isec.file == file && rel.addend == addend);
860 }
861
addSymbols(ThunkSection & isec)862 void PPC32LongThunk::addSymbols(ThunkSection &isec) {
863 addSymbol(saver.save("__LongThunk_" + destination.getName()), STT_FUNC, 0,
864 isec);
865 }
866
writeTo(uint8_t * buf)867 void PPC32LongThunk::writeTo(uint8_t *buf) {
868 auto ha = [](uint32_t v) -> uint16_t { return (v + 0x8000) >> 16; };
869 auto lo = [](uint32_t v) -> uint16_t { return v; };
870 uint32_t d = destination.getVA(addend);
871 if (config->isPic) {
872 uint32_t off = d - (getThunkTargetSym()->getVA() + 8);
873 write32(buf + 0, 0x7c0802a6); // mflr r12,0
874 write32(buf + 4, 0x429f0005); // bcl r20,r31,.+4
875 write32(buf + 8, 0x7d8802a6); // mtctr r12
876 write32(buf + 12, 0x3d8c0000 | ha(off)); // addis r12,r12,off@ha
877 write32(buf + 16, 0x398c0000 | lo(off)); // addi r12,r12,off@l
878 write32(buf + 20, 0x7c0803a6); // mtlr r0
879 buf += 24;
880 } else {
881 write32(buf + 0, 0x3d800000 | ha(d)); // lis r12,d@ha
882 write32(buf + 4, 0x398c0000 | lo(d)); // addi r12,r12,d@l
883 buf += 8;
884 }
885 write32(buf + 0, 0x7d8903a6); // mtctr r12
886 write32(buf + 4, 0x4e800420); // bctr
887 }
888
writePPC64LoadAndBranch(uint8_t * buf,int64_t offset)889 void elf::writePPC64LoadAndBranch(uint8_t *buf, int64_t offset) {
890 uint16_t offHa = (offset + 0x8000) >> 16;
891 uint16_t offLo = offset & 0xffff;
892
893 write32(buf + 0, 0x3d820000 | offHa); // addis r12, r2, OffHa
894 write32(buf + 4, 0xe98c0000 | offLo); // ld r12, OffLo(r12)
895 write32(buf + 8, 0x7d8903a6); // mtctr r12
896 write32(buf + 12, 0x4e800420); // bctr
897 }
898
writeTo(uint8_t * buf)899 void PPC64PltCallStub::writeTo(uint8_t *buf) {
900 int64_t offset = destination.getGotPltVA() - getPPC64TocBase();
901 // Save the TOC pointer to the save-slot reserved in the call frame.
902 write32(buf + 0, 0xf8410018); // std r2,24(r1)
903 writePPC64LoadAndBranch(buf + 4, offset);
904 }
905
addSymbols(ThunkSection & isec)906 void PPC64PltCallStub::addSymbols(ThunkSection &isec) {
907 Defined *s = addSymbol(saver.save("__plt_" + destination.getName()), STT_FUNC,
908 0, isec);
909 s->needsTocRestore = true;
910 s->file = destination.file;
911 }
912
isCompatibleWith(const InputSection & isec,const Relocation & rel) const913 bool PPC64PltCallStub::isCompatibleWith(const InputSection &isec,
914 const Relocation &rel) const {
915 return rel.type == R_PPC64_REL24 || rel.type == R_PPC64_REL14;
916 }
917
writeTo(uint8_t * buf)918 void PPC64R2SaveStub::writeTo(uint8_t *buf) {
919 const int64_t offset = computeOffset();
920 write32(buf + 0, 0xf8410018); // std r2,24(r1)
921 // The branch offset needs to fit in 26 bits.
922 if (getMayUseShortThunk()) {
923 write32(buf + 4, 0x48000000 | (offset & 0x03fffffc)); // b <offset>
924 } else if (isInt<34>(offset)) {
925 const uint64_t paddi = PADDI_R12_NO_DISP |
926 (((offset >> 16) & 0x3ffff) << 32) |
927 (offset & 0xffff);
928 writePrefixedInstruction(buf + 4, paddi); // paddi r12, 0, func@pcrel, 1
929 write32(buf + 12, MTCTR_R12); // mtctr r12
930 write32(buf + 16, BCTR); // bctr
931 } else {
932 in.ppc64LongBranchTarget->addEntry(&destination, addend);
933 const int64_t offsetFromTOC =
934 in.ppc64LongBranchTarget->getEntryVA(&destination, addend) -
935 getPPC64TocBase();
936 writePPC64LoadAndBranch(buf + 4, offsetFromTOC);
937 }
938 }
939
addSymbols(ThunkSection & isec)940 void PPC64R2SaveStub::addSymbols(ThunkSection &isec) {
941 Defined *s = addSymbol(saver.save("__toc_save_" + destination.getName()),
942 STT_FUNC, 0, isec);
943 s->needsTocRestore = true;
944 }
945
writeTo(uint8_t * buf)946 void PPC64R12SetupStub::writeTo(uint8_t *buf) {
947 int64_t offset = destination.getVA() - getThunkTargetSym()->getVA();
948 if (!isInt<34>(offset))
949 reportRangeError(buf, offset, 34, destination, "R12 setup stub offset");
950 uint64_t paddi = PADDI_R12_NO_DISP | (((offset >> 16) & 0x3ffff) << 32) |
951 (offset & 0xffff);
952
953 writePrefixedInstruction(buf + 0, paddi); // paddi r12, 0, func@pcrel, 1
954 write32(buf + 8, MTCTR_R12); // mtctr r12
955 write32(buf + 12, BCTR); // bctr
956 }
957
addSymbols(ThunkSection & isec)958 void PPC64R12SetupStub::addSymbols(ThunkSection &isec) {
959 addSymbol(saver.save("__gep_setup_" + destination.getName()), STT_FUNC, 0,
960 isec);
961 }
962
writeTo(uint8_t * buf)963 void PPC64PCRelPLTStub::writeTo(uint8_t *buf) {
964 int64_t offset = destination.getGotPltVA() - getThunkTargetSym()->getVA();
965 if (!isInt<34>(offset))
966 reportRangeError(buf, offset, 34, destination,
967 "PC-relative PLT stub offset");
968 uint64_t pld =
969 PLD_R12_NO_DISP | (((offset >> 16) & 0x3ffff) << 32) | (offset & 0xffff);
970
971 writePrefixedInstruction(buf + 0, pld); // pld r12, func@plt@pcrel
972 write32(buf + 8, MTCTR_R12); // mtctr r12
973 write32(buf + 12, BCTR); // bctr
974 }
975
addSymbols(ThunkSection & isec)976 void PPC64PCRelPLTStub::addSymbols(ThunkSection &isec) {
977 addSymbol(saver.save("__plt_pcrel_" + destination.getName()), STT_FUNC, 0,
978 isec);
979 }
980
isCompatibleWith(const InputSection & isec,const Relocation & rel) const981 bool PPC64PCRelPLTStub::isCompatibleWith(const InputSection &isec,
982 const Relocation &rel) const {
983 return rel.type == R_PPC64_REL24_NOTOC;
984 }
985
writeTo(uint8_t * buf)986 void PPC64LongBranchThunk::writeTo(uint8_t *buf) {
987 int64_t offset = in.ppc64LongBranchTarget->getEntryVA(&destination, addend) -
988 getPPC64TocBase();
989 writePPC64LoadAndBranch(buf, offset);
990 }
991
addSymbols(ThunkSection & isec)992 void PPC64LongBranchThunk::addSymbols(ThunkSection &isec) {
993 addSymbol(saver.save("__long_branch_" + destination.getName()), STT_FUNC, 0,
994 isec);
995 }
996
isCompatibleWith(const InputSection & isec,const Relocation & rel) const997 bool PPC64LongBranchThunk::isCompatibleWith(const InputSection &isec,
998 const Relocation &rel) const {
999 return rel.type == R_PPC64_REL24 || rel.type == R_PPC64_REL14;
1000 }
1001
writeTo(uint8_t * buf)1002 void PPC64PCRelLongBranchThunk::writeTo(uint8_t *buf) {
1003 int64_t offset = destination.getVA() - getThunkTargetSym()->getVA();
1004 if (!isInt<34>(offset))
1005 reportRangeError(buf, offset, 34, destination,
1006 "PC-relative long branch stub offset");
1007 uint64_t paddi = PADDI_R12_NO_DISP | (((offset >> 16) & 0x3ffff) << 32) |
1008 (offset & 0xffff);
1009
1010 writePrefixedInstruction(buf + 0, paddi); // paddi r12, 0, func@pcrel, 1
1011 write32(buf + 8, MTCTR_R12); // mtctr r12
1012 write32(buf + 12, BCTR); // bctr
1013 }
1014
addSymbols(ThunkSection & isec)1015 void PPC64PCRelLongBranchThunk::addSymbols(ThunkSection &isec) {
1016 addSymbol(saver.save("__long_branch_pcrel_" + destination.getName()),
1017 STT_FUNC, 0, isec);
1018 }
1019
isCompatibleWith(const InputSection & isec,const Relocation & rel) const1020 bool PPC64PCRelLongBranchThunk::isCompatibleWith(const InputSection &isec,
1021 const Relocation &rel) const {
1022 return rel.type == R_PPC64_REL24_NOTOC;
1023 }
1024
Thunk(Symbol & d,int64_t a)1025 Thunk::Thunk(Symbol &d, int64_t a) : destination(d), addend(a), offset(0) {}
1026
1027 Thunk::~Thunk() = default;
1028
addThunkAArch64(RelType type,Symbol & s,int64_t a)1029 static Thunk *addThunkAArch64(RelType type, Symbol &s, int64_t a) {
1030 if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 &&
1031 type != R_AARCH64_PLT32)
1032 fatal("unrecognized relocation type");
1033 if (config->picThunk)
1034 return make<AArch64ADRPThunk>(s, a);
1035 return make<AArch64ABSLongThunk>(s, a);
1036 }
1037
1038 // Creates a thunk for Thumb-ARM interworking.
1039 // Arm Architectures v5 and v6 do not support Thumb2 technology. This means
1040 // - MOVT and MOVW instructions cannot be used
1041 // - Only Thumb relocation that can generate a Thunk is a BL, this can always
1042 // be transformed into a BLX
addThunkPreArmv7(RelType reloc,Symbol & s)1043 static Thunk *addThunkPreArmv7(RelType reloc, Symbol &s) {
1044 switch (reloc) {
1045 case R_ARM_PC24:
1046 case R_ARM_PLT32:
1047 case R_ARM_JUMP24:
1048 case R_ARM_CALL:
1049 case R_ARM_THM_CALL:
1050 if (config->picThunk)
1051 return make<ARMV5PILongThunk>(s);
1052 return make<ARMV5ABSLongThunk>(s);
1053 }
1054 fatal("relocation " + toString(reloc) + " to " + toString(s) +
1055 " not supported for Armv5 or Armv6 targets");
1056 }
1057
1058 // Create a thunk for Thumb long branch on V6-M.
1059 // Arm Architecture v6-M only supports Thumb instructions. This means
1060 // - MOVT and MOVW instructions cannot be used.
1061 // - Only a limited number of instructions can access registers r8 and above
1062 // - No interworking support is needed (all Thumb).
addThunkV6M(RelType reloc,Symbol & s)1063 static Thunk *addThunkV6M(RelType reloc, Symbol &s) {
1064 switch (reloc) {
1065 case R_ARM_THM_JUMP19:
1066 case R_ARM_THM_JUMP24:
1067 case R_ARM_THM_CALL:
1068 if (config->isPic)
1069 return make<ThumbV6MPILongThunk>(s);
1070 return make<ThumbV6MABSLongThunk>(s);
1071 }
1072 fatal("relocation " + toString(reloc) + " to " + toString(s) +
1073 " not supported for Armv6-M targets");
1074 }
1075
1076 // Creates a thunk for Thumb-ARM interworking or branch range extension.
addThunkArm(RelType reloc,Symbol & s)1077 static Thunk *addThunkArm(RelType reloc, Symbol &s) {
1078 // Decide which Thunk is needed based on:
1079 // Available instruction set
1080 // - An Arm Thunk can only be used if Arm state is available.
1081 // - A Thumb Thunk can only be used if Thumb state is available.
1082 // - Can only use a Thunk if it uses instructions that the Target supports.
1083 // Relocation is branch or branch and link
1084 // - Branch instructions cannot change state, can only select Thunk that
1085 // starts in the same state as the caller.
1086 // - Branch and link relocations can change state, can select Thunks from
1087 // either Arm or Thumb.
1088 // Position independent Thunks if we require position independent code.
1089
1090 // Handle architectures that have restrictions on the instructions that they
1091 // can use in Thunks. The flags below are set by reading the BuildAttributes
1092 // of the input objects. InputFiles.cpp contains the mapping from ARM
1093 // architecture to flag.
1094 if (!config->armHasMovtMovw) {
1095 if (!config->armJ1J2BranchEncoding)
1096 return addThunkPreArmv7(reloc, s);
1097 return addThunkV6M(reloc, s);
1098 }
1099
1100 switch (reloc) {
1101 case R_ARM_PC24:
1102 case R_ARM_PLT32:
1103 case R_ARM_JUMP24:
1104 case R_ARM_CALL:
1105 if (config->picThunk)
1106 return make<ARMV7PILongThunk>(s);
1107 return make<ARMV7ABSLongThunk>(s);
1108 case R_ARM_THM_JUMP19:
1109 case R_ARM_THM_JUMP24:
1110 case R_ARM_THM_CALL:
1111 if (config->picThunk)
1112 return make<ThumbV7PILongThunk>(s);
1113 return make<ThumbV7ABSLongThunk>(s);
1114 }
1115 fatal("unrecognized relocation type");
1116 }
1117
addThunkMips(RelType type,Symbol & s)1118 static Thunk *addThunkMips(RelType type, Symbol &s) {
1119 if ((s.stOther & STO_MIPS_MICROMIPS) && isMipsR6())
1120 return make<MicroMipsR6Thunk>(s);
1121 if (s.stOther & STO_MIPS_MICROMIPS)
1122 return make<MicroMipsThunk>(s);
1123 return make<MipsThunk>(s);
1124 }
1125
addThunkPPC32(const InputSection & isec,const Relocation & rel,Symbol & s)1126 static Thunk *addThunkPPC32(const InputSection &isec, const Relocation &rel,
1127 Symbol &s) {
1128 assert((rel.type == R_PPC_LOCAL24PC || rel.type == R_PPC_REL24 ||
1129 rel.type == R_PPC_PLTREL24) &&
1130 "unexpected relocation type for thunk");
1131 if (s.isInPlt())
1132 return make<PPC32PltCallStub>(isec, rel, s);
1133 return make<PPC32LongThunk>(s, rel.addend);
1134 }
1135
addThunkPPC64(RelType type,Symbol & s,int64_t a)1136 static Thunk *addThunkPPC64(RelType type, Symbol &s, int64_t a) {
1137 assert((type == R_PPC64_REL14 || type == R_PPC64_REL24 ||
1138 type == R_PPC64_REL24_NOTOC) &&
1139 "unexpected relocation type for thunk");
1140 if (s.isInPlt())
1141 return type == R_PPC64_REL24_NOTOC ? (Thunk *)make<PPC64PCRelPLTStub>(s)
1142 : (Thunk *)make<PPC64PltCallStub>(s);
1143
1144 // This check looks at the st_other bits of the callee. If the value is 1
1145 // then the callee clobbers the TOC and we need an R2 save stub when RelType
1146 // is R_PPC64_REL14 or R_PPC64_REL24.
1147 if ((type == R_PPC64_REL14 || type == R_PPC64_REL24) && (s.stOther >> 5) == 1)
1148 return make<PPC64R2SaveStub>(s, a);
1149
1150 if (type == R_PPC64_REL24_NOTOC)
1151 return (s.stOther >> 5) > 1
1152 ? (Thunk *)make<PPC64R12SetupStub>(s)
1153 : (Thunk *)make<PPC64PCRelLongBranchThunk>(s, a);
1154
1155 if (config->picThunk)
1156 return make<PPC64PILongBranchThunk>(s, a);
1157
1158 return make<PPC64PDLongBranchThunk>(s, a);
1159 }
1160
addThunk(const InputSection & isec,Relocation & rel)1161 Thunk *elf::addThunk(const InputSection &isec, Relocation &rel) {
1162 Symbol &s = *rel.sym;
1163 int64_t a = rel.addend;
1164
1165 if (config->emachine == EM_AARCH64)
1166 return addThunkAArch64(rel.type, s, a);
1167
1168 if (config->emachine == EM_ARM)
1169 return addThunkArm(rel.type, s);
1170
1171 if (config->emachine == EM_MIPS)
1172 return addThunkMips(rel.type, s);
1173
1174 if (config->emachine == EM_PPC)
1175 return addThunkPPC32(isec, rel, s);
1176
1177 if (config->emachine == EM_PPC64)
1178 return addThunkPPC64(rel.type, s, a);
1179
1180 llvm_unreachable("add Thunk only supported for ARM, Mips and PowerPC");
1181 }
1182