1ece8a530Spatrick //===- Thunks.cpp --------------------------------------------------------===//
2ece8a530Spatrick //
3ece8a530Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ece8a530Spatrick // See https://llvm.org/LICENSE.txt for license information.
5ece8a530Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ece8a530Spatrick //
7ece8a530Spatrick //===---------------------------------------------------------------------===//
8ece8a530Spatrick //
9ece8a530Spatrick // This file contains Thunk subclasses.
10ece8a530Spatrick //
11ece8a530Spatrick // A thunk is a small piece of code written after an input section
12ece8a530Spatrick // which is used to jump between "incompatible" functions
13ece8a530Spatrick // such as MIPS PIC and non-PIC or ARM non-Thumb and Thumb functions.
14ece8a530Spatrick //
15ece8a530Spatrick // If a jump target is too far and its address doesn't fit to a
16ece8a530Spatrick // short jump instruction, we need to create a thunk too, but we
17ece8a530Spatrick // haven't supported it yet.
18ece8a530Spatrick //
19ece8a530Spatrick // i386 and x86-64 don't need thunks.
20ece8a530Spatrick //
21ece8a530Spatrick //===---------------------------------------------------------------------===//
22ece8a530Spatrick
23ece8a530Spatrick #include "Thunks.h"
24ece8a530Spatrick #include "Config.h"
25dfe94b16Srobert #include "InputFiles.h"
26ece8a530Spatrick #include "InputSection.h"
27ece8a530Spatrick #include "OutputSections.h"
28ece8a530Spatrick #include "Symbols.h"
29ece8a530Spatrick #include "SyntheticSections.h"
30ece8a530Spatrick #include "Target.h"
31dfe94b16Srobert #include "lld/Common/CommonLinkerContext.h"
32ece8a530Spatrick #include "llvm/BinaryFormat/ELF.h"
33ece8a530Spatrick #include "llvm/Support/Casting.h"
34ece8a530Spatrick #include "llvm/Support/ErrorHandling.h"
35ece8a530Spatrick #include "llvm/Support/MathExtras.h"
36ece8a530Spatrick #include <cstdint>
37ece8a530Spatrick #include <cstring>
38ece8a530Spatrick
39ece8a530Spatrick using namespace llvm;
40ece8a530Spatrick using namespace llvm::object;
41ece8a530Spatrick using namespace llvm::ELF;
42bb684c34Spatrick using namespace lld;
43bb684c34Spatrick using namespace lld::elf;
44ece8a530Spatrick
45ece8a530Spatrick namespace {
46ece8a530Spatrick
47ece8a530Spatrick // AArch64 long range Thunks
48ece8a530Spatrick class AArch64ABSLongThunk final : public Thunk {
49ece8a530Spatrick public:
AArch64ABSLongThunk(Symbol & dest,int64_t addend)50ece8a530Spatrick AArch64ABSLongThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
size()51ece8a530Spatrick uint32_t size() override { return 16; }
52ece8a530Spatrick void writeTo(uint8_t *buf) override;
53ece8a530Spatrick void addSymbols(ThunkSection &isec) override;
54ece8a530Spatrick };
55ece8a530Spatrick
56ece8a530Spatrick class AArch64ADRPThunk final : public Thunk {
57ece8a530Spatrick public:
AArch64ADRPThunk(Symbol & dest,int64_t addend)58ece8a530Spatrick AArch64ADRPThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
size()59ece8a530Spatrick uint32_t size() override { return 12; }
60ece8a530Spatrick void writeTo(uint8_t *buf) override;
61ece8a530Spatrick void addSymbols(ThunkSection &isec) override;
62ece8a530Spatrick };
63ece8a530Spatrick
64ece8a530Spatrick // Base class for ARM thunks.
65ece8a530Spatrick //
66ece8a530Spatrick // An ARM thunk may be either short or long. A short thunk is simply a branch
67ece8a530Spatrick // (B) instruction, and it may be used to call ARM functions when the distance
68ece8a530Spatrick // from the thunk to the target is less than 32MB. Long thunks can branch to any
69ece8a530Spatrick // virtual address and can switch between ARM and Thumb, and they are
70ece8a530Spatrick // implemented in the derived classes. This class tries to create a short thunk
71ece8a530Spatrick // if the target is in range, otherwise it creates a long thunk.
72ece8a530Spatrick class ARMThunk : public Thunk {
73ece8a530Spatrick public:
ARMThunk(Symbol & dest,int64_t addend)741cf9926bSpatrick ARMThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
75ece8a530Spatrick
76ece8a530Spatrick bool getMayUseShortThunk();
size()77ece8a530Spatrick uint32_t size() override { return getMayUseShortThunk() ? 4 : sizeLong(); }
78ece8a530Spatrick void writeTo(uint8_t *buf) override;
79ece8a530Spatrick bool isCompatibleWith(const InputSection &isec,
80ece8a530Spatrick const Relocation &rel) const override;
81ece8a530Spatrick
82ece8a530Spatrick // Returns the size of a long thunk.
83ece8a530Spatrick virtual uint32_t sizeLong() = 0;
84ece8a530Spatrick
85ece8a530Spatrick // Writes a long thunk to Buf.
86ece8a530Spatrick virtual void writeLong(uint8_t *buf) = 0;
87ece8a530Spatrick
88ece8a530Spatrick private:
89ece8a530Spatrick // This field tracks whether all previously considered layouts would allow
90ece8a530Spatrick // this thunk to be short. If we have ever needed a long thunk, we always
91ece8a530Spatrick // create a long thunk, even if the thunk may be short given the current
92ece8a530Spatrick // distance to the target. We do this because transitioning from long to short
93ece8a530Spatrick // can create layout oscillations in certain corner cases which would prevent
94ece8a530Spatrick // the layout from converging.
95ece8a530Spatrick bool mayUseShortThunk = true;
96ece8a530Spatrick };
97ece8a530Spatrick
98ece8a530Spatrick // Base class for Thumb-2 thunks.
99ece8a530Spatrick //
100ece8a530Spatrick // This class is similar to ARMThunk, but it uses the Thumb-2 B.W instruction
101ece8a530Spatrick // which has a range of 16MB.
102ece8a530Spatrick class ThumbThunk : public Thunk {
103ece8a530Spatrick public:
ThumbThunk(Symbol & dest,int64_t addend)1041cf9926bSpatrick ThumbThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {
1051cf9926bSpatrick alignment = 2;
1061cf9926bSpatrick }
107ece8a530Spatrick
108ece8a530Spatrick bool getMayUseShortThunk();
size()109ece8a530Spatrick uint32_t size() override { return getMayUseShortThunk() ? 4 : sizeLong(); }
110ece8a530Spatrick void writeTo(uint8_t *buf) override;
111ece8a530Spatrick bool isCompatibleWith(const InputSection &isec,
112ece8a530Spatrick const Relocation &rel) const override;
113ece8a530Spatrick
114ece8a530Spatrick // Returns the size of a long thunk.
115ece8a530Spatrick virtual uint32_t sizeLong() = 0;
116ece8a530Spatrick
117ece8a530Spatrick // Writes a long thunk to Buf.
118ece8a530Spatrick virtual void writeLong(uint8_t *buf) = 0;
119ece8a530Spatrick
120ece8a530Spatrick private:
121ece8a530Spatrick // See comment in ARMThunk above.
122ece8a530Spatrick bool mayUseShortThunk = true;
123ece8a530Spatrick };
124ece8a530Spatrick
125ece8a530Spatrick // Specific ARM Thunk implementations. The naming convention is:
126ece8a530Spatrick // Source State, TargetState, Target Requirement, ABS or PI, Range
127ece8a530Spatrick class ARMV7ABSLongThunk final : public ARMThunk {
128ece8a530Spatrick public:
ARMV7ABSLongThunk(Symbol & dest,int64_t addend)1291cf9926bSpatrick ARMV7ABSLongThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}
130ece8a530Spatrick
sizeLong()131ece8a530Spatrick uint32_t sizeLong() override { return 12; }
132ece8a530Spatrick void writeLong(uint8_t *buf) override;
133ece8a530Spatrick void addSymbols(ThunkSection &isec) override;
134ece8a530Spatrick };
135ece8a530Spatrick
136ece8a530Spatrick class ARMV7PILongThunk final : public ARMThunk {
137ece8a530Spatrick public:
ARMV7PILongThunk(Symbol & dest,int64_t addend)1381cf9926bSpatrick ARMV7PILongThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}
139ece8a530Spatrick
sizeLong()140ece8a530Spatrick uint32_t sizeLong() override { return 16; }
141ece8a530Spatrick void writeLong(uint8_t *buf) override;
142ece8a530Spatrick void addSymbols(ThunkSection &isec) override;
143ece8a530Spatrick };
144ece8a530Spatrick
145ece8a530Spatrick class ThumbV7ABSLongThunk final : public ThumbThunk {
146ece8a530Spatrick public:
ThumbV7ABSLongThunk(Symbol & dest,int64_t addend)1471cf9926bSpatrick ThumbV7ABSLongThunk(Symbol &dest, int64_t addend)
1481cf9926bSpatrick : ThumbThunk(dest, addend) {}
149ece8a530Spatrick
sizeLong()150ece8a530Spatrick uint32_t sizeLong() override { return 10; }
151ece8a530Spatrick void writeLong(uint8_t *buf) override;
152ece8a530Spatrick void addSymbols(ThunkSection &isec) override;
153ece8a530Spatrick };
154ece8a530Spatrick
155ece8a530Spatrick class ThumbV7PILongThunk final : public ThumbThunk {
156ece8a530Spatrick public:
ThumbV7PILongThunk(Symbol & dest,int64_t addend)1571cf9926bSpatrick ThumbV7PILongThunk(Symbol &dest, int64_t addend) : ThumbThunk(dest, addend) {}
158ece8a530Spatrick
sizeLong()159ece8a530Spatrick uint32_t sizeLong() override { return 12; }
160ece8a530Spatrick void writeLong(uint8_t *buf) override;
161ece8a530Spatrick void addSymbols(ThunkSection &isec) override;
162ece8a530Spatrick };
163ece8a530Spatrick
164ece8a530Spatrick // Implementations of Thunks for Arm v6-M. Only Thumb instructions are permitted
165ece8a530Spatrick class ThumbV6MABSLongThunk final : public ThumbThunk {
166ece8a530Spatrick public:
ThumbV6MABSLongThunk(Symbol & dest,int64_t addend)1671cf9926bSpatrick ThumbV6MABSLongThunk(Symbol &dest, int64_t addend)
1681cf9926bSpatrick : ThumbThunk(dest, addend) {}
169ece8a530Spatrick
sizeLong()170ece8a530Spatrick uint32_t sizeLong() override { return 12; }
171ece8a530Spatrick void writeLong(uint8_t *buf) override;
172ece8a530Spatrick void addSymbols(ThunkSection &isec) override;
173ece8a530Spatrick };
174ece8a530Spatrick
175ece8a530Spatrick class ThumbV6MPILongThunk final : public ThumbThunk {
176ece8a530Spatrick public:
ThumbV6MPILongThunk(Symbol & dest,int64_t addend)1771cf9926bSpatrick ThumbV6MPILongThunk(Symbol &dest, int64_t addend)
1781cf9926bSpatrick : ThumbThunk(dest, addend) {}
179ece8a530Spatrick
sizeLong()180ece8a530Spatrick uint32_t sizeLong() override { return 16; }
181ece8a530Spatrick void writeLong(uint8_t *buf) override;
182ece8a530Spatrick void addSymbols(ThunkSection &isec) override;
183ece8a530Spatrick };
184ece8a530Spatrick
185dfe94b16Srobert // Architectures v4, v5 and v6 do not support the movt/movw instructions. v5 and
186dfe94b16Srobert // v6 support BLX to which BL instructions can be rewritten inline. There are no
187dfe94b16Srobert // Thumb entrypoints for v5 and v6 as there is no Thumb branch instruction on
188dfe94b16Srobert // these architecture that can result in a thunk.
189dfe94b16Srobert
190dfe94b16Srobert // LDR on v5 and v6 can switch processor state, so for v5 and v6,
191dfe94b16Srobert // ARMV5LongLdrPcThunk can be used for both Arm->Arm and Arm->Thumb calls. v4
192dfe94b16Srobert // can also use this thunk, but only for Arm->Arm calls.
193dfe94b16Srobert class ARMV5LongLdrPcThunk final : public ARMThunk {
194dfe94b16Srobert public:
ARMV5LongLdrPcThunk(Symbol & dest,int64_t addend)195dfe94b16Srobert ARMV5LongLdrPcThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}
196dfe94b16Srobert
sizeLong()197dfe94b16Srobert uint32_t sizeLong() override { return 8; }
198dfe94b16Srobert void writeLong(uint8_t *buf) override;
199dfe94b16Srobert void addSymbols(ThunkSection &isec) override;
200dfe94b16Srobert };
201dfe94b16Srobert
202dfe94b16Srobert // Implementations of Thunks for v4. BLX is not supported, and loads
203dfe94b16Srobert // will not invoke Arm/Thumb state changes.
204dfe94b16Srobert class ARMV4PILongBXThunk final : public ARMThunk {
205dfe94b16Srobert public:
ARMV4PILongBXThunk(Symbol & dest,int64_t addend)206dfe94b16Srobert ARMV4PILongBXThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}
207dfe94b16Srobert
sizeLong()208dfe94b16Srobert uint32_t sizeLong() override { return 16; }
209dfe94b16Srobert void writeLong(uint8_t *buf) override;
210dfe94b16Srobert void addSymbols(ThunkSection &isec) override;
211dfe94b16Srobert };
212dfe94b16Srobert
213dfe94b16Srobert class ARMV4PILongThunk final : public ARMThunk {
214dfe94b16Srobert public:
ARMV4PILongThunk(Symbol & dest,int64_t addend)215dfe94b16Srobert ARMV4PILongThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}
216dfe94b16Srobert
sizeLong()217dfe94b16Srobert uint32_t sizeLong() override { return 12; }
218dfe94b16Srobert void writeLong(uint8_t *buf) override;
219dfe94b16Srobert void addSymbols(ThunkSection &isec) override;
220dfe94b16Srobert };
221dfe94b16Srobert
222dfe94b16Srobert class ThumbV4PILongBXThunk final : public ThumbThunk {
223dfe94b16Srobert public:
ThumbV4PILongBXThunk(Symbol & dest,int64_t addend)224dfe94b16Srobert ThumbV4PILongBXThunk(Symbol &dest, int64_t addend)
225dfe94b16Srobert : ThumbThunk(dest, addend) {}
226dfe94b16Srobert
sizeLong()227dfe94b16Srobert uint32_t sizeLong() override { return 16; }
228dfe94b16Srobert void writeLong(uint8_t *buf) override;
229dfe94b16Srobert void addSymbols(ThunkSection &isec) override;
230dfe94b16Srobert };
231dfe94b16Srobert
232dfe94b16Srobert class ThumbV4PILongThunk final : public ThumbThunk {
233dfe94b16Srobert public:
ThumbV4PILongThunk(Symbol & dest,int64_t addend)234dfe94b16Srobert ThumbV4PILongThunk(Symbol &dest, int64_t addend)
235dfe94b16Srobert : ThumbThunk(dest, addend) {}
236dfe94b16Srobert
sizeLong()237dfe94b16Srobert uint32_t sizeLong() override { return 20; }
238dfe94b16Srobert void writeLong(uint8_t *buf) override;
239dfe94b16Srobert void addSymbols(ThunkSection &isec) override;
240dfe94b16Srobert };
241dfe94b16Srobert
242dfe94b16Srobert class ARMV4ABSLongBXThunk final : public ARMThunk {
243dfe94b16Srobert public:
ARMV4ABSLongBXThunk(Symbol & dest,int64_t addend)244dfe94b16Srobert ARMV4ABSLongBXThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}
245dfe94b16Srobert
sizeLong()246dfe94b16Srobert uint32_t sizeLong() override { return 12; }
247dfe94b16Srobert void writeLong(uint8_t *buf) override;
248dfe94b16Srobert void addSymbols(ThunkSection &isec) override;
249dfe94b16Srobert };
250dfe94b16Srobert
251dfe94b16Srobert class ThumbV4ABSLongBXThunk final : public ThumbThunk {
252dfe94b16Srobert public:
ThumbV4ABSLongBXThunk(Symbol & dest,int64_t addend)253dfe94b16Srobert ThumbV4ABSLongBXThunk(Symbol &dest, int64_t addend)
254dfe94b16Srobert : ThumbThunk(dest, addend) {}
255dfe94b16Srobert
sizeLong()256dfe94b16Srobert uint32_t sizeLong() override { return 12; }
257dfe94b16Srobert void writeLong(uint8_t *buf) override;
258dfe94b16Srobert void addSymbols(ThunkSection &isec) override;
259dfe94b16Srobert };
260dfe94b16Srobert
261dfe94b16Srobert class ThumbV4ABSLongThunk final : public ThumbThunk {
262dfe94b16Srobert public:
ThumbV4ABSLongThunk(Symbol & dest,int64_t addend)263dfe94b16Srobert ThumbV4ABSLongThunk(Symbol &dest, int64_t addend)
264dfe94b16Srobert : ThumbThunk(dest, addend) {}
265dfe94b16Srobert
sizeLong()266dfe94b16Srobert uint32_t sizeLong() override { return 16; }
267dfe94b16Srobert void writeLong(uint8_t *buf) override;
268dfe94b16Srobert void addSymbols(ThunkSection &isec) override;
269dfe94b16Srobert };
270dfe94b16Srobert
271ece8a530Spatrick // MIPS LA25 thunk
272ece8a530Spatrick class MipsThunk final : public Thunk {
273ece8a530Spatrick public:
MipsThunk(Symbol & dest)274ece8a530Spatrick MipsThunk(Symbol &dest) : Thunk(dest, 0) {}
275ece8a530Spatrick
size()276ece8a530Spatrick uint32_t size() override { return 16; }
277ece8a530Spatrick void writeTo(uint8_t *buf) override;
278ece8a530Spatrick void addSymbols(ThunkSection &isec) override;
279ece8a530Spatrick InputSection *getTargetInputSection() const override;
280ece8a530Spatrick };
281ece8a530Spatrick
282ece8a530Spatrick // microMIPS R2-R5 LA25 thunk
283ece8a530Spatrick class MicroMipsThunk final : public Thunk {
284ece8a530Spatrick public:
MicroMipsThunk(Symbol & dest)285ece8a530Spatrick MicroMipsThunk(Symbol &dest) : Thunk(dest, 0) {}
286ece8a530Spatrick
size()287ece8a530Spatrick uint32_t size() override { return 14; }
288ece8a530Spatrick void writeTo(uint8_t *buf) override;
289ece8a530Spatrick void addSymbols(ThunkSection &isec) override;
290ece8a530Spatrick InputSection *getTargetInputSection() const override;
291ece8a530Spatrick };
292ece8a530Spatrick
293ece8a530Spatrick // microMIPS R6 LA25 thunk
294ece8a530Spatrick class MicroMipsR6Thunk final : public Thunk {
295ece8a530Spatrick public:
MicroMipsR6Thunk(Symbol & dest)296ece8a530Spatrick MicroMipsR6Thunk(Symbol &dest) : Thunk(dest, 0) {}
297ece8a530Spatrick
size()298ece8a530Spatrick uint32_t size() override { return 12; }
299ece8a530Spatrick void writeTo(uint8_t *buf) override;
300ece8a530Spatrick void addSymbols(ThunkSection &isec) override;
301ece8a530Spatrick InputSection *getTargetInputSection() const override;
302ece8a530Spatrick };
303ece8a530Spatrick
304ece8a530Spatrick class PPC32PltCallStub final : public Thunk {
305ece8a530Spatrick public:
306ece8a530Spatrick // For R_PPC_PLTREL24, Thunk::addend records the addend which will be used to
307ece8a530Spatrick // decide the offsets in the call stub.
PPC32PltCallStub(const InputSection & isec,const Relocation & rel,Symbol & dest)308ece8a530Spatrick PPC32PltCallStub(const InputSection &isec, const Relocation &rel,
309ece8a530Spatrick Symbol &dest)
310ece8a530Spatrick : Thunk(dest, rel.addend), file(isec.file) {}
size()311ece8a530Spatrick uint32_t size() override { return 16; }
312ece8a530Spatrick void writeTo(uint8_t *buf) override;
313ece8a530Spatrick void addSymbols(ThunkSection &isec) override;
314ece8a530Spatrick bool isCompatibleWith(const InputSection &isec, const Relocation &rel) const override;
315ece8a530Spatrick
316ece8a530Spatrick private:
317ece8a530Spatrick // Records the call site of the call stub.
318ece8a530Spatrick const InputFile *file;
319ece8a530Spatrick };
320ece8a530Spatrick
321ece8a530Spatrick class PPC32LongThunk final : public Thunk {
322ece8a530Spatrick public:
PPC32LongThunk(Symbol & dest,int64_t addend)323ece8a530Spatrick PPC32LongThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
size()324ece8a530Spatrick uint32_t size() override { return config->isPic ? 32 : 16; }
325ece8a530Spatrick void writeTo(uint8_t *buf) override;
326ece8a530Spatrick void addSymbols(ThunkSection &isec) override;
327ece8a530Spatrick };
328ece8a530Spatrick
329ece8a530Spatrick // PPC64 Plt call stubs.
330ece8a530Spatrick // Any call site that needs to call through a plt entry needs a call stub in
331ece8a530Spatrick // the .text section. The call stub is responsible for:
332ece8a530Spatrick // 1) Saving the toc-pointer to the stack.
333ece8a530Spatrick // 2) Loading the target functions address from the procedure linkage table into
334ece8a530Spatrick // r12 for use by the target functions global entry point, and into the count
335ece8a530Spatrick // register.
336ece8a530Spatrick // 3) Transferring control to the target function through an indirect branch.
337ece8a530Spatrick class PPC64PltCallStub final : public Thunk {
338ece8a530Spatrick public:
PPC64PltCallStub(Symbol & dest)339ece8a530Spatrick PPC64PltCallStub(Symbol &dest) : Thunk(dest, 0) {}
size()340ece8a530Spatrick uint32_t size() override { return 20; }
341ece8a530Spatrick void writeTo(uint8_t *buf) override;
342ece8a530Spatrick void addSymbols(ThunkSection &isec) override;
3431cf9926bSpatrick bool isCompatibleWith(const InputSection &isec,
3441cf9926bSpatrick const Relocation &rel) const override;
345ece8a530Spatrick };
346ece8a530Spatrick
347bb684c34Spatrick // PPC64 R2 Save Stub
348bb684c34Spatrick // When the caller requires a valid R2 TOC pointer but the callee does not
349bb684c34Spatrick // require a TOC pointer and the callee cannot guarantee that it doesn't
350bb684c34Spatrick // clobber R2 then we need to save R2. This stub:
351bb684c34Spatrick // 1) Saves the TOC pointer to the stack.
352bb684c34Spatrick // 2) Tail calls the callee.
353bb684c34Spatrick class PPC64R2SaveStub final : public Thunk {
354bb684c34Spatrick public:
PPC64R2SaveStub(Symbol & dest,int64_t addend)3551cf9926bSpatrick PPC64R2SaveStub(Symbol &dest, int64_t addend) : Thunk(dest, addend) {
3561cf9926bSpatrick alignment = 16;
3571cf9926bSpatrick }
3581cf9926bSpatrick
3591cf9926bSpatrick // To prevent oscillations in layout when moving from short to long thunks
3601cf9926bSpatrick // we make sure that once a thunk has been set to long it cannot go back.
getMayUseShortThunk()3611cf9926bSpatrick bool getMayUseShortThunk() {
3621cf9926bSpatrick if (!mayUseShortThunk)
3631cf9926bSpatrick return false;
3641cf9926bSpatrick if (!isInt<26>(computeOffset())) {
3651cf9926bSpatrick mayUseShortThunk = false;
3661cf9926bSpatrick return false;
3671cf9926bSpatrick }
3681cf9926bSpatrick return true;
3691cf9926bSpatrick }
size()3701cf9926bSpatrick uint32_t size() override { return getMayUseShortThunk() ? 8 : 32; }
371bb684c34Spatrick void writeTo(uint8_t *buf) override;
372bb684c34Spatrick void addSymbols(ThunkSection &isec) override;
3731cf9926bSpatrick bool isCompatibleWith(const InputSection &isec,
3741cf9926bSpatrick const Relocation &rel) const override;
3751cf9926bSpatrick
3761cf9926bSpatrick private:
3771cf9926bSpatrick // Transitioning from long to short can create layout oscillations in
3781cf9926bSpatrick // certain corner cases which would prevent the layout from converging.
3791cf9926bSpatrick // This is similar to the handling for ARMThunk.
3801cf9926bSpatrick bool mayUseShortThunk = true;
computeOffset() const3811cf9926bSpatrick int64_t computeOffset() const {
3821cf9926bSpatrick return destination.getVA() - (getThunkTargetSym()->getVA() + 4);
3831cf9926bSpatrick }
3841cf9926bSpatrick };
3851cf9926bSpatrick
3861cf9926bSpatrick // PPC64 R12 Setup Stub
3871cf9926bSpatrick // When a caller that does not maintain a toc-pointer performs a local call to
3881cf9926bSpatrick // a callee which requires a toc-pointer then we need this stub to place the
3891cf9926bSpatrick // callee's global entry point into r12 without a save of R2.
3901cf9926bSpatrick class PPC64R12SetupStub final : public Thunk {
3911cf9926bSpatrick public:
PPC64R12SetupStub(Symbol & dest)3921cf9926bSpatrick PPC64R12SetupStub(Symbol &dest) : Thunk(dest, 0) { alignment = 16; }
size()3931cf9926bSpatrick uint32_t size() override { return 32; }
3941cf9926bSpatrick void writeTo(uint8_t *buf) override;
3951cf9926bSpatrick void addSymbols(ThunkSection &isec) override;
3961cf9926bSpatrick bool isCompatibleWith(const InputSection &isec,
3971cf9926bSpatrick const Relocation &rel) const override;
3981cf9926bSpatrick };
3991cf9926bSpatrick
4001cf9926bSpatrick // PPC64 PC-relative PLT Stub
4011cf9926bSpatrick // When a caller that does not maintain a toc-pointer performs an extern call
4021cf9926bSpatrick // then this stub is needed for:
4031cf9926bSpatrick // 1) Loading the target functions address from the procedure linkage table into
4041cf9926bSpatrick // r12 for use by the target functions global entry point, and into the count
4051cf9926bSpatrick // register with pc-relative instructions.
4061cf9926bSpatrick // 2) Transferring control to the target function through an indirect branch.
4071cf9926bSpatrick class PPC64PCRelPLTStub final : public Thunk {
4081cf9926bSpatrick public:
PPC64PCRelPLTStub(Symbol & dest)4091cf9926bSpatrick PPC64PCRelPLTStub(Symbol &dest) : Thunk(dest, 0) { alignment = 16; }
size()4101cf9926bSpatrick uint32_t size() override { return 32; }
4111cf9926bSpatrick void writeTo(uint8_t *buf) override;
4121cf9926bSpatrick void addSymbols(ThunkSection &isec) override;
4131cf9926bSpatrick bool isCompatibleWith(const InputSection &isec,
4141cf9926bSpatrick const Relocation &rel) const override;
415bb684c34Spatrick };
416bb684c34Spatrick
417ece8a530Spatrick // A bl instruction uses a signed 24 bit offset, with an implicit 4 byte
418ece8a530Spatrick // alignment. This gives a possible 26 bits of 'reach'. If the call offset is
4191cf9926bSpatrick // larger than that we need to emit a long-branch thunk. The target address
420ece8a530Spatrick // of the callee is stored in a table to be accessed TOC-relative. Since the
421ece8a530Spatrick // call must be local (a non-local call will have a PltCallStub instead) the
422ece8a530Spatrick // table stores the address of the callee's local entry point. For
423ece8a530Spatrick // position-independent code a corresponding relative dynamic relocation is
424ece8a530Spatrick // used.
425ece8a530Spatrick class PPC64LongBranchThunk : public Thunk {
426ece8a530Spatrick public:
size()4271cf9926bSpatrick uint32_t size() override { return 32; }
428ece8a530Spatrick void writeTo(uint8_t *buf) override;
429ece8a530Spatrick void addSymbols(ThunkSection &isec) override;
4301cf9926bSpatrick bool isCompatibleWith(const InputSection &isec,
4311cf9926bSpatrick const Relocation &rel) const override;
432ece8a530Spatrick
433ece8a530Spatrick protected:
PPC64LongBranchThunk(Symbol & dest,int64_t addend)434ece8a530Spatrick PPC64LongBranchThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
435ece8a530Spatrick };
436ece8a530Spatrick
437ece8a530Spatrick class PPC64PILongBranchThunk final : public PPC64LongBranchThunk {
438ece8a530Spatrick public:
PPC64PILongBranchThunk(Symbol & dest,int64_t addend)439ece8a530Spatrick PPC64PILongBranchThunk(Symbol &dest, int64_t addend)
440ece8a530Spatrick : PPC64LongBranchThunk(dest, addend) {
441ece8a530Spatrick assert(!dest.isPreemptible);
442dfe94b16Srobert if (std::optional<uint32_t> index =
443ece8a530Spatrick in.ppc64LongBranchTarget->addEntry(&dest, addend)) {
4441cf9926bSpatrick mainPart->relaDyn->addRelativeReloc(
445dfe94b16Srobert target->relativeRel, *in.ppc64LongBranchTarget, *index * UINT64_C(8),
4461cf9926bSpatrick dest, addend + getPPC64GlobalEntryToLocalEntryOffset(dest.stOther),
4471cf9926bSpatrick target->symbolicRel, R_ABS);
448ece8a530Spatrick }
449ece8a530Spatrick }
450ece8a530Spatrick };
451ece8a530Spatrick
452ece8a530Spatrick class PPC64PDLongBranchThunk final : public PPC64LongBranchThunk {
453ece8a530Spatrick public:
PPC64PDLongBranchThunk(Symbol & dest,int64_t addend)454ece8a530Spatrick PPC64PDLongBranchThunk(Symbol &dest, int64_t addend)
455ece8a530Spatrick : PPC64LongBranchThunk(dest, addend) {
456ece8a530Spatrick in.ppc64LongBranchTarget->addEntry(&dest, addend);
457ece8a530Spatrick }
458ece8a530Spatrick };
459ece8a530Spatrick
460ece8a530Spatrick } // end anonymous namespace
461ece8a530Spatrick
addSymbol(StringRef name,uint8_t type,uint64_t value,InputSectionBase & section)462ece8a530Spatrick Defined *Thunk::addSymbol(StringRef name, uint8_t type, uint64_t value,
463ece8a530Spatrick InputSectionBase §ion) {
464ece8a530Spatrick Defined *d = addSyntheticLocal(name, type, value, /*size=*/0, section);
465ece8a530Spatrick syms.push_back(d);
466ece8a530Spatrick return d;
467ece8a530Spatrick }
468ece8a530Spatrick
setOffset(uint64_t newOffset)469ece8a530Spatrick void Thunk::setOffset(uint64_t newOffset) {
470ece8a530Spatrick for (Defined *d : syms)
471ece8a530Spatrick d->value = d->value - offset + newOffset;
472ece8a530Spatrick offset = newOffset;
473ece8a530Spatrick }
474ece8a530Spatrick
475ece8a530Spatrick // AArch64 long range Thunks
476ece8a530Spatrick
getAArch64ThunkDestVA(const Symbol & s,int64_t a)477ece8a530Spatrick static uint64_t getAArch64ThunkDestVA(const Symbol &s, int64_t a) {
478ece8a530Spatrick uint64_t v = s.isInPlt() ? s.getPltVA() : s.getVA(a);
479ece8a530Spatrick return v;
480ece8a530Spatrick }
481ece8a530Spatrick
writeTo(uint8_t * buf)482ece8a530Spatrick void AArch64ABSLongThunk::writeTo(uint8_t *buf) {
483ece8a530Spatrick const uint8_t data[] = {
484ece8a530Spatrick 0x50, 0x00, 0x00, 0x58, // ldr x16, L0
485ece8a530Spatrick 0x00, 0x02, 0x1f, 0xd6, // br x16
486ece8a530Spatrick 0x00, 0x00, 0x00, 0x00, // L0: .xword S
487ece8a530Spatrick 0x00, 0x00, 0x00, 0x00,
488ece8a530Spatrick };
489ece8a530Spatrick uint64_t s = getAArch64ThunkDestVA(destination, addend);
490ece8a530Spatrick memcpy(buf, data, sizeof(data));
491bb684c34Spatrick target->relocateNoSym(buf + 8, R_AARCH64_ABS64, s);
492ece8a530Spatrick }
493ece8a530Spatrick
addSymbols(ThunkSection & isec)494ece8a530Spatrick void AArch64ABSLongThunk::addSymbols(ThunkSection &isec) {
495dfe94b16Srobert addSymbol(saver().save("__AArch64AbsLongThunk_" + destination.getName()),
496ece8a530Spatrick STT_FUNC, 0, isec);
497ece8a530Spatrick addSymbol("$x", STT_NOTYPE, 0, isec);
498ece8a530Spatrick addSymbol("$d", STT_NOTYPE, 8, isec);
499ece8a530Spatrick }
500ece8a530Spatrick
501ece8a530Spatrick // This Thunk has a maximum range of 4Gb, this is sufficient for all programs
502ece8a530Spatrick // using the small code model, including pc-relative ones. At time of writing
503ece8a530Spatrick // clang and gcc do not support the large code model for position independent
504ece8a530Spatrick // code so it is safe to use this for position independent thunks without
505ece8a530Spatrick // worrying about the destination being more than 4Gb away.
writeTo(uint8_t * buf)506ece8a530Spatrick void AArch64ADRPThunk::writeTo(uint8_t *buf) {
507ece8a530Spatrick const uint8_t data[] = {
508ece8a530Spatrick 0x10, 0x00, 0x00, 0x90, // adrp x16, Dest R_AARCH64_ADR_PREL_PG_HI21(Dest)
509ece8a530Spatrick 0x10, 0x02, 0x00, 0x91, // add x16, x16, R_AARCH64_ADD_ABS_LO12_NC(Dest)
510ece8a530Spatrick 0x00, 0x02, 0x1f, 0xd6, // br x16
511ece8a530Spatrick };
512ece8a530Spatrick uint64_t s = getAArch64ThunkDestVA(destination, addend);
513ece8a530Spatrick uint64_t p = getThunkTargetSym()->getVA();
514ece8a530Spatrick memcpy(buf, data, sizeof(data));
515bb684c34Spatrick target->relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21,
516ece8a530Spatrick getAArch64Page(s) - getAArch64Page(p));
517bb684c34Spatrick target->relocateNoSym(buf + 4, R_AARCH64_ADD_ABS_LO12_NC, s);
518ece8a530Spatrick }
519ece8a530Spatrick
addSymbols(ThunkSection & isec)520ece8a530Spatrick void AArch64ADRPThunk::addSymbols(ThunkSection &isec) {
521dfe94b16Srobert addSymbol(saver().save("__AArch64ADRPThunk_" + destination.getName()),
522dfe94b16Srobert STT_FUNC, 0, isec);
523ece8a530Spatrick addSymbol("$x", STT_NOTYPE, 0, isec);
524ece8a530Spatrick }
525ece8a530Spatrick
526ece8a530Spatrick // ARM Target Thunks
getARMThunkDestVA(const Symbol & s)527ece8a530Spatrick static uint64_t getARMThunkDestVA(const Symbol &s) {
528ece8a530Spatrick uint64_t v = s.isInPlt() ? s.getPltVA() : s.getVA();
529ece8a530Spatrick return SignExtend64<32>(v);
530ece8a530Spatrick }
531ece8a530Spatrick
532ece8a530Spatrick // This function returns true if the target is not Thumb and is within 2^26, and
533ece8a530Spatrick // it has not previously returned false (see comment for mayUseShortThunk).
getMayUseShortThunk()534ece8a530Spatrick bool ARMThunk::getMayUseShortThunk() {
535ece8a530Spatrick if (!mayUseShortThunk)
536ece8a530Spatrick return false;
537ece8a530Spatrick uint64_t s = getARMThunkDestVA(destination);
538ece8a530Spatrick if (s & 1) {
539ece8a530Spatrick mayUseShortThunk = false;
540ece8a530Spatrick return false;
541ece8a530Spatrick }
542ece8a530Spatrick uint64_t p = getThunkTargetSym()->getVA();
543ece8a530Spatrick int64_t offset = s - p - 8;
544ece8a530Spatrick mayUseShortThunk = llvm::isInt<26>(offset);
545ece8a530Spatrick return mayUseShortThunk;
546ece8a530Spatrick }
547ece8a530Spatrick
writeTo(uint8_t * buf)548ece8a530Spatrick void ARMThunk::writeTo(uint8_t *buf) {
549ece8a530Spatrick if (!getMayUseShortThunk()) {
550ece8a530Spatrick writeLong(buf);
551ece8a530Spatrick return;
552ece8a530Spatrick }
553ece8a530Spatrick
554ece8a530Spatrick uint64_t s = getARMThunkDestVA(destination);
555ece8a530Spatrick uint64_t p = getThunkTargetSym()->getVA();
556ece8a530Spatrick int64_t offset = s - p - 8;
557ece8a530Spatrick const uint8_t data[] = {
558ece8a530Spatrick 0x00, 0x00, 0x00, 0xea, // b S
559ece8a530Spatrick };
560ece8a530Spatrick memcpy(buf, data, sizeof(data));
561bb684c34Spatrick target->relocateNoSym(buf, R_ARM_JUMP24, offset);
562ece8a530Spatrick }
563ece8a530Spatrick
isCompatibleWith(const InputSection & isec,const Relocation & rel) const564ece8a530Spatrick bool ARMThunk::isCompatibleWith(const InputSection &isec,
565ece8a530Spatrick const Relocation &rel) const {
566dfe94b16Srobert // v4T does not have BLX, so also deny R_ARM_THM_CALL
567dfe94b16Srobert if (!config->armHasBlx && rel.type == R_ARM_THM_CALL)
568dfe94b16Srobert return false;
569dfe94b16Srobert
570ece8a530Spatrick // Thumb branch relocations can't use BLX
571ece8a530Spatrick return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;
572ece8a530Spatrick }
573ece8a530Spatrick
574dfe94b16Srobert // This function returns true if:
575dfe94b16Srobert // the target is Thumb
576dfe94b16Srobert // && is within branch range
577dfe94b16Srobert // && this function has not previously returned false
578dfe94b16Srobert // (see comment for mayUseShortThunk)
579dfe94b16Srobert // && the arch supports Thumb branch range extension.
getMayUseShortThunk()580ece8a530Spatrick bool ThumbThunk::getMayUseShortThunk() {
581dfe94b16Srobert if (!mayUseShortThunk || !config->armJ1J2BranchEncoding)
582ece8a530Spatrick return false;
583ece8a530Spatrick uint64_t s = getARMThunkDestVA(destination);
584ece8a530Spatrick if ((s & 1) == 0) {
585ece8a530Spatrick mayUseShortThunk = false;
586ece8a530Spatrick return false;
587ece8a530Spatrick }
588ece8a530Spatrick uint64_t p = getThunkTargetSym()->getVA() & ~1;
589ece8a530Spatrick int64_t offset = s - p - 4;
590ece8a530Spatrick mayUseShortThunk = llvm::isInt<25>(offset);
591ece8a530Spatrick return mayUseShortThunk;
592ece8a530Spatrick }
593ece8a530Spatrick
writeTo(uint8_t * buf)594ece8a530Spatrick void ThumbThunk::writeTo(uint8_t *buf) {
595ece8a530Spatrick if (!getMayUseShortThunk()) {
596ece8a530Spatrick writeLong(buf);
597ece8a530Spatrick return;
598ece8a530Spatrick }
599ece8a530Spatrick
600ece8a530Spatrick uint64_t s = getARMThunkDestVA(destination);
601ece8a530Spatrick uint64_t p = getThunkTargetSym()->getVA();
602ece8a530Spatrick int64_t offset = s - p - 4;
603ece8a530Spatrick const uint8_t data[] = {
604ece8a530Spatrick 0x00, 0xf0, 0x00, 0xb0, // b.w S
605ece8a530Spatrick };
606ece8a530Spatrick memcpy(buf, data, sizeof(data));
607bb684c34Spatrick target->relocateNoSym(buf, R_ARM_THM_JUMP24, offset);
608ece8a530Spatrick }
609ece8a530Spatrick
isCompatibleWith(const InputSection & isec,const Relocation & rel) const610ece8a530Spatrick bool ThumbThunk::isCompatibleWith(const InputSection &isec,
611ece8a530Spatrick const Relocation &rel) const {
612dfe94b16Srobert // v4T does not have BLX, so also deny R_ARM_CALL
613dfe94b16Srobert if (!config->armHasBlx && rel.type == R_ARM_CALL)
614dfe94b16Srobert return false;
615dfe94b16Srobert
616ece8a530Spatrick // ARM branch relocations can't use BLX
617ece8a530Spatrick return rel.type != R_ARM_JUMP24 && rel.type != R_ARM_PC24 && rel.type != R_ARM_PLT32;
618ece8a530Spatrick }
619ece8a530Spatrick
writeLong(uint8_t * buf)620ece8a530Spatrick void ARMV7ABSLongThunk::writeLong(uint8_t *buf) {
621ece8a530Spatrick const uint8_t data[] = {
622ece8a530Spatrick 0x00, 0xc0, 0x00, 0xe3, // movw ip,:lower16:S
623ece8a530Spatrick 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S
624ece8a530Spatrick 0x1c, 0xff, 0x2f, 0xe1, // bx ip
625ece8a530Spatrick };
626ece8a530Spatrick uint64_t s = getARMThunkDestVA(destination);
627ece8a530Spatrick memcpy(buf, data, sizeof(data));
628bb684c34Spatrick target->relocateNoSym(buf, R_ARM_MOVW_ABS_NC, s);
629bb684c34Spatrick target->relocateNoSym(buf + 4, R_ARM_MOVT_ABS, s);
630ece8a530Spatrick }
631ece8a530Spatrick
addSymbols(ThunkSection & isec)632ece8a530Spatrick void ARMV7ABSLongThunk::addSymbols(ThunkSection &isec) {
633dfe94b16Srobert addSymbol(saver().save("__ARMv7ABSLongThunk_" + destination.getName()),
634ece8a530Spatrick STT_FUNC, 0, isec);
635ece8a530Spatrick addSymbol("$a", STT_NOTYPE, 0, isec);
636ece8a530Spatrick }
637ece8a530Spatrick
writeLong(uint8_t * buf)638ece8a530Spatrick void ThumbV7ABSLongThunk::writeLong(uint8_t *buf) {
639ece8a530Spatrick const uint8_t data[] = {
640ece8a530Spatrick 0x40, 0xf2, 0x00, 0x0c, // movw ip, :lower16:S
641ece8a530Spatrick 0xc0, 0xf2, 0x00, 0x0c, // movt ip, :upper16:S
642ece8a530Spatrick 0x60, 0x47, // bx ip
643ece8a530Spatrick };
644ece8a530Spatrick uint64_t s = getARMThunkDestVA(destination);
645ece8a530Spatrick memcpy(buf, data, sizeof(data));
646bb684c34Spatrick target->relocateNoSym(buf, R_ARM_THM_MOVW_ABS_NC, s);
647bb684c34Spatrick target->relocateNoSym(buf + 4, R_ARM_THM_MOVT_ABS, s);
648ece8a530Spatrick }
649ece8a530Spatrick
addSymbols(ThunkSection & isec)650ece8a530Spatrick void ThumbV7ABSLongThunk::addSymbols(ThunkSection &isec) {
651dfe94b16Srobert addSymbol(saver().save("__Thumbv7ABSLongThunk_" + destination.getName()),
652ece8a530Spatrick STT_FUNC, 1, isec);
653ece8a530Spatrick addSymbol("$t", STT_NOTYPE, 0, isec);
654ece8a530Spatrick }
655ece8a530Spatrick
writeLong(uint8_t * buf)656ece8a530Spatrick void ARMV7PILongThunk::writeLong(uint8_t *buf) {
657ece8a530Spatrick const uint8_t data[] = {
658ece8a530Spatrick 0xf0, 0xcf, 0x0f, 0xe3, // P: movw ip,:lower16:S - (P + (L1-P) + 8)
659ece8a530Spatrick 0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S - (P + (L1-P) + 8)
660ece8a530Spatrick 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
661ece8a530Spatrick 0x1c, 0xff, 0x2f, 0xe1, // bx ip
662ece8a530Spatrick };
663ece8a530Spatrick uint64_t s = getARMThunkDestVA(destination);
664ece8a530Spatrick uint64_t p = getThunkTargetSym()->getVA();
665ece8a530Spatrick int64_t offset = s - p - 16;
666ece8a530Spatrick memcpy(buf, data, sizeof(data));
667bb684c34Spatrick target->relocateNoSym(buf, R_ARM_MOVW_PREL_NC, offset);
668bb684c34Spatrick target->relocateNoSym(buf + 4, R_ARM_MOVT_PREL, offset);
669ece8a530Spatrick }
670ece8a530Spatrick
addSymbols(ThunkSection & isec)671ece8a530Spatrick void ARMV7PILongThunk::addSymbols(ThunkSection &isec) {
672dfe94b16Srobert addSymbol(saver().save("__ARMV7PILongThunk_" + destination.getName()),
673dfe94b16Srobert STT_FUNC, 0, isec);
674ece8a530Spatrick addSymbol("$a", STT_NOTYPE, 0, isec);
675ece8a530Spatrick }
676ece8a530Spatrick
writeLong(uint8_t * buf)677ece8a530Spatrick void ThumbV7PILongThunk::writeLong(uint8_t *buf) {
678ece8a530Spatrick const uint8_t data[] = {
679ece8a530Spatrick 0x4f, 0xf6, 0xf4, 0x7c, // P: movw ip,:lower16:S - (P + (L1-P) + 4)
680ece8a530Spatrick 0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P) + 4)
681ece8a530Spatrick 0xfc, 0x44, // L1: add ip, pc
682ece8a530Spatrick 0x60, 0x47, // bx ip
683ece8a530Spatrick };
684ece8a530Spatrick uint64_t s = getARMThunkDestVA(destination);
685ece8a530Spatrick uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
686ece8a530Spatrick int64_t offset = s - p - 12;
687ece8a530Spatrick memcpy(buf, data, sizeof(data));
688bb684c34Spatrick target->relocateNoSym(buf, R_ARM_THM_MOVW_PREL_NC, offset);
689bb684c34Spatrick target->relocateNoSym(buf + 4, R_ARM_THM_MOVT_PREL, offset);
690ece8a530Spatrick }
691ece8a530Spatrick
addSymbols(ThunkSection & isec)692ece8a530Spatrick void ThumbV7PILongThunk::addSymbols(ThunkSection &isec) {
693dfe94b16Srobert addSymbol(saver().save("__ThumbV7PILongThunk_" + destination.getName()),
694ece8a530Spatrick STT_FUNC, 1, isec);
695ece8a530Spatrick addSymbol("$t", STT_NOTYPE, 0, isec);
696ece8a530Spatrick }
697ece8a530Spatrick
writeLong(uint8_t * buf)698ece8a530Spatrick void ThumbV6MABSLongThunk::writeLong(uint8_t *buf) {
699ece8a530Spatrick // Most Thumb instructions cannot access the high registers r8 - r15. As the
700ece8a530Spatrick // only register we can corrupt is r12 we must instead spill a low register
701ece8a530Spatrick // to the stack to use as a scratch register. We push r1 even though we
702ece8a530Spatrick // don't need to get some space to use for the return address.
703ece8a530Spatrick const uint8_t data[] = {
704ece8a530Spatrick 0x03, 0xb4, // push {r0, r1} ; Obtain scratch registers
705ece8a530Spatrick 0x01, 0x48, // ldr r0, [pc, #4] ; L1
706ece8a530Spatrick 0x01, 0x90, // str r0, [sp, #4] ; SP + 4 = S
707ece8a530Spatrick 0x01, 0xbd, // pop {r0, pc} ; restore r0 and branch to dest
708ece8a530Spatrick 0x00, 0x00, 0x00, 0x00 // L1: .word S
709ece8a530Spatrick };
710ece8a530Spatrick uint64_t s = getARMThunkDestVA(destination);
711ece8a530Spatrick memcpy(buf, data, sizeof(data));
712bb684c34Spatrick target->relocateNoSym(buf + 8, R_ARM_ABS32, s);
713ece8a530Spatrick }
714ece8a530Spatrick
addSymbols(ThunkSection & isec)715ece8a530Spatrick void ThumbV6MABSLongThunk::addSymbols(ThunkSection &isec) {
716dfe94b16Srobert addSymbol(saver().save("__Thumbv6MABSLongThunk_" + destination.getName()),
717ece8a530Spatrick STT_FUNC, 1, isec);
718ece8a530Spatrick addSymbol("$t", STT_NOTYPE, 0, isec);
719ece8a530Spatrick addSymbol("$d", STT_NOTYPE, 8, isec);
720ece8a530Spatrick }
721ece8a530Spatrick
writeLong(uint8_t * buf)722ece8a530Spatrick void ThumbV6MPILongThunk::writeLong(uint8_t *buf) {
723ece8a530Spatrick // Most Thumb instructions cannot access the high registers r8 - r15. As the
724ece8a530Spatrick // only register we can corrupt is ip (r12) we must instead spill a low
725ece8a530Spatrick // register to the stack to use as a scratch register.
726ece8a530Spatrick const uint8_t data[] = {
727ece8a530Spatrick 0x01, 0xb4, // P: push {r0} ; Obtain scratch register
728ece8a530Spatrick 0x02, 0x48, // ldr r0, [pc, #8] ; L2
729ece8a530Spatrick 0x84, 0x46, // mov ip, r0 ; high to low register
730ece8a530Spatrick 0x01, 0xbc, // pop {r0} ; restore scratch register
731ece8a530Spatrick 0xe7, 0x44, // L1: add pc, ip ; transfer control
732ece8a530Spatrick 0xc0, 0x46, // nop ; pad to 4-byte boundary
733ece8a530Spatrick 0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 4)
734ece8a530Spatrick };
735ece8a530Spatrick uint64_t s = getARMThunkDestVA(destination);
736ece8a530Spatrick uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
737ece8a530Spatrick memcpy(buf, data, sizeof(data));
738bb684c34Spatrick target->relocateNoSym(buf + 12, R_ARM_REL32, s - p - 12);
739ece8a530Spatrick }
740ece8a530Spatrick
addSymbols(ThunkSection & isec)741ece8a530Spatrick void ThumbV6MPILongThunk::addSymbols(ThunkSection &isec) {
742dfe94b16Srobert addSymbol(saver().save("__Thumbv6MPILongThunk_" + destination.getName()),
743ece8a530Spatrick STT_FUNC, 1, isec);
744ece8a530Spatrick addSymbol("$t", STT_NOTYPE, 0, isec);
745ece8a530Spatrick addSymbol("$d", STT_NOTYPE, 12, isec);
746ece8a530Spatrick }
747ece8a530Spatrick
writeLong(uint8_t * buf)748dfe94b16Srobert void ARMV5LongLdrPcThunk::writeLong(uint8_t *buf) {
749dfe94b16Srobert const uint8_t data[] = {
750dfe94b16Srobert 0x04, 0xf0, 0x1f, 0xe5, // ldr pc, [pc,#-4] ; L1
751dfe94b16Srobert 0x00, 0x00, 0x00, 0x00, // L1: .word S
752dfe94b16Srobert };
753dfe94b16Srobert memcpy(buf, data, sizeof(data));
754dfe94b16Srobert target->relocateNoSym(buf + 4, R_ARM_ABS32, getARMThunkDestVA(destination));
755dfe94b16Srobert }
756dfe94b16Srobert
addSymbols(ThunkSection & isec)757dfe94b16Srobert void ARMV5LongLdrPcThunk::addSymbols(ThunkSection &isec) {
758dfe94b16Srobert addSymbol(saver().save("__ARMv5LongLdrPcThunk_" + destination.getName()),
759dfe94b16Srobert STT_FUNC, 0, isec);
760dfe94b16Srobert addSymbol("$a", STT_NOTYPE, 0, isec);
761dfe94b16Srobert addSymbol("$d", STT_NOTYPE, 4, isec);
762dfe94b16Srobert }
763dfe94b16Srobert
writeLong(uint8_t * buf)764dfe94b16Srobert void ARMV4ABSLongBXThunk::writeLong(uint8_t *buf) {
765dfe94b16Srobert const uint8_t data[] = {
766dfe94b16Srobert 0x00, 0xc0, 0x9f, 0xe5, // ldr r12, [pc] ; L1
767dfe94b16Srobert 0x1c, 0xff, 0x2f, 0xe1, // bx r12
768dfe94b16Srobert 0x00, 0x00, 0x00, 0x00, // L1: .word S
769dfe94b16Srobert };
770dfe94b16Srobert memcpy(buf, data, sizeof(data));
771dfe94b16Srobert target->relocateNoSym(buf + 8, R_ARM_ABS32, getARMThunkDestVA(destination));
772dfe94b16Srobert }
773dfe94b16Srobert
addSymbols(ThunkSection & isec)774dfe94b16Srobert void ARMV4ABSLongBXThunk::addSymbols(ThunkSection &isec) {
775dfe94b16Srobert addSymbol(saver().save("__ARMv4ABSLongBXThunk_" + destination.getName()),
776dfe94b16Srobert STT_FUNC, 0, isec);
777dfe94b16Srobert addSymbol("$a", STT_NOTYPE, 0, isec);
778dfe94b16Srobert addSymbol("$d", STT_NOTYPE, 8, isec);
779dfe94b16Srobert }
780dfe94b16Srobert
writeLong(uint8_t * buf)781dfe94b16Srobert void ThumbV4ABSLongBXThunk::writeLong(uint8_t *buf) {
782dfe94b16Srobert const uint8_t data[] = {
783dfe94b16Srobert 0x78, 0x47, // bx pc
784dfe94b16Srobert 0xfd, 0xe7, // b #-6 ; Arm recommended sequence to follow bx pc
785dfe94b16Srobert 0x04, 0xf0, 0x1f, 0xe5, // ldr pc, [pc, #-4] ; L1
786dfe94b16Srobert 0x00, 0x00, 0x00, 0x00, // L1: .word S
787dfe94b16Srobert };
788dfe94b16Srobert memcpy(buf, data, sizeof(data));
789dfe94b16Srobert target->relocateNoSym(buf + 8, R_ARM_ABS32, getARMThunkDestVA(destination));
790dfe94b16Srobert }
791dfe94b16Srobert
addSymbols(ThunkSection & isec)792dfe94b16Srobert void ThumbV4ABSLongBXThunk::addSymbols(ThunkSection &isec) {
793dfe94b16Srobert addSymbol(saver().save("__Thumbv4ABSLongBXThunk_" + destination.getName()),
794dfe94b16Srobert STT_FUNC, 1, isec);
795dfe94b16Srobert addSymbol("$t", STT_NOTYPE, 0, isec);
796dfe94b16Srobert addSymbol("$a", STT_NOTYPE, 4, isec);
797dfe94b16Srobert addSymbol("$d", STT_NOTYPE, 8, isec);
798dfe94b16Srobert }
799dfe94b16Srobert
writeLong(uint8_t * buf)800dfe94b16Srobert void ThumbV4ABSLongThunk::writeLong(uint8_t *buf) {
801dfe94b16Srobert const uint8_t data[] = {
802dfe94b16Srobert 0x78, 0x47, // bx pc
803dfe94b16Srobert 0xfd, 0xe7, // b #-6 ; Arm recommended sequence to follow bx pc
804dfe94b16Srobert 0x00, 0xc0, 0x9f, 0xe5, // ldr r12, [pc] ; L1
805dfe94b16Srobert 0x1c, 0xff, 0x2f, 0xe1, // bx r12
806dfe94b16Srobert 0x00, 0x00, 0x00, 0x00, // L1: .word S
807dfe94b16Srobert };
808dfe94b16Srobert memcpy(buf, data, sizeof(data));
809dfe94b16Srobert target->relocateNoSym(buf + 12, R_ARM_ABS32, getARMThunkDestVA(destination));
810dfe94b16Srobert }
811dfe94b16Srobert
addSymbols(ThunkSection & isec)812dfe94b16Srobert void ThumbV4ABSLongThunk::addSymbols(ThunkSection &isec) {
813dfe94b16Srobert addSymbol(saver().save("__Thumbv4ABSLongThunk_" + destination.getName()),
814dfe94b16Srobert STT_FUNC, 1, isec);
815dfe94b16Srobert addSymbol("$t", STT_NOTYPE, 0, isec);
816dfe94b16Srobert addSymbol("$a", STT_NOTYPE, 4, isec);
817dfe94b16Srobert addSymbol("$d", STT_NOTYPE, 12, isec);
818dfe94b16Srobert }
819dfe94b16Srobert
writeLong(uint8_t * buf)820dfe94b16Srobert void ARMV4PILongBXThunk::writeLong(uint8_t *buf) {
821dfe94b16Srobert const uint8_t data[] = {
822dfe94b16Srobert 0x04, 0xc0, 0x9f, 0xe5, // P: ldr ip, [pc,#4] ; L2
823dfe94b16Srobert 0x0c, 0xc0, 0x8f, 0xe0, // L1: add ip, pc, ip
824dfe94b16Srobert 0x1c, 0xff, 0x2f, 0xe1, // bx ip
825dfe94b16Srobert 0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 8)
826dfe94b16Srobert };
827dfe94b16Srobert uint64_t s = getARMThunkDestVA(destination);
828dfe94b16Srobert uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
829dfe94b16Srobert memcpy(buf, data, sizeof(data));
830dfe94b16Srobert target->relocateNoSym(buf + 12, R_ARM_REL32, s - p - 12);
831dfe94b16Srobert }
832dfe94b16Srobert
addSymbols(ThunkSection & isec)833dfe94b16Srobert void ARMV4PILongBXThunk::addSymbols(ThunkSection &isec) {
834dfe94b16Srobert addSymbol(saver().save("__ARMv4PILongBXThunk_" + destination.getName()),
835dfe94b16Srobert STT_FUNC, 0, isec);
836dfe94b16Srobert addSymbol("$a", STT_NOTYPE, 0, isec);
837dfe94b16Srobert addSymbol("$d", STT_NOTYPE, 12, isec);
838dfe94b16Srobert }
839dfe94b16Srobert
writeLong(uint8_t * buf)840dfe94b16Srobert void ARMV4PILongThunk::writeLong(uint8_t *buf) {
841dfe94b16Srobert const uint8_t data[] = {
842dfe94b16Srobert 0x00, 0xc0, 0x9f, 0xe5, // P: ldr ip, [pc] ; L2
843dfe94b16Srobert 0x0c, 0xf0, 0x8f, 0xe0, // L1: add pc, pc, r12
844dfe94b16Srobert 0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 8)
845dfe94b16Srobert };
846dfe94b16Srobert uint64_t s = getARMThunkDestVA(destination);
847dfe94b16Srobert uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
848dfe94b16Srobert memcpy(buf, data, sizeof(data));
849dfe94b16Srobert target->relocateNoSym(buf + 8, R_ARM_REL32, s - p - 12);
850dfe94b16Srobert }
851dfe94b16Srobert
addSymbols(ThunkSection & isec)852dfe94b16Srobert void ARMV4PILongThunk::addSymbols(ThunkSection &isec) {
853dfe94b16Srobert addSymbol(saver().save("__ARMv4PILongThunk_" + destination.getName()),
854dfe94b16Srobert STT_FUNC, 0, isec);
855dfe94b16Srobert addSymbol("$a", STT_NOTYPE, 0, isec);
856dfe94b16Srobert addSymbol("$d", STT_NOTYPE, 8, isec);
857dfe94b16Srobert }
858dfe94b16Srobert
writeLong(uint8_t * buf)859dfe94b16Srobert void ThumbV4PILongBXThunk::writeLong(uint8_t *buf) {
860dfe94b16Srobert const uint8_t data[] = {
861dfe94b16Srobert 0x78, 0x47, // P: bx pc
862dfe94b16Srobert 0xfd, 0xe7, // b #-6 ; Arm recommended sequence to follow bx pc
863dfe94b16Srobert 0x00, 0xc0, 0x9f, 0xe5, // ldr r12, [pc] ; L2
864dfe94b16Srobert 0x0f, 0xf0, 0x8c, 0xe0, // L1: add pc, r12, pc
865dfe94b16Srobert 0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 8)
866dfe94b16Srobert };
867dfe94b16Srobert uint64_t s = getARMThunkDestVA(destination);
868dfe94b16Srobert uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
869dfe94b16Srobert memcpy(buf, data, sizeof(data));
870dfe94b16Srobert target->relocateNoSym(buf + 12, R_ARM_REL32, s - p - 16);
871dfe94b16Srobert }
872dfe94b16Srobert
addSymbols(ThunkSection & isec)873dfe94b16Srobert void ThumbV4PILongBXThunk::addSymbols(ThunkSection &isec) {
874dfe94b16Srobert addSymbol(saver().save("__Thumbv4PILongBXThunk_" + destination.getName()),
875dfe94b16Srobert STT_FUNC, 1, isec);
876dfe94b16Srobert addSymbol("$t", STT_NOTYPE, 0, isec);
877dfe94b16Srobert addSymbol("$a", STT_NOTYPE, 4, isec);
878dfe94b16Srobert addSymbol("$d", STT_NOTYPE, 12, isec);
879dfe94b16Srobert }
880dfe94b16Srobert
writeLong(uint8_t * buf)881dfe94b16Srobert void ThumbV4PILongThunk::writeLong(uint8_t *buf) {
882dfe94b16Srobert const uint8_t data[] = {
883dfe94b16Srobert 0x78, 0x47, // P: bx pc
884dfe94b16Srobert 0xfd, 0xe7, // b #-6 ; Arm recommended sequence to follow bx pc
885dfe94b16Srobert 0x04, 0xc0, 0x9f, 0xe5, // ldr ip, [pc,#4] ; L2
886dfe94b16Srobert 0x0c, 0xc0, 0x8f, 0xe0, // L1: add ip, pc, ip
887dfe94b16Srobert 0x1c, 0xff, 0x2f, 0xe1, // bx ip
888dfe94b16Srobert 0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 8)
889dfe94b16Srobert };
890dfe94b16Srobert uint64_t s = getARMThunkDestVA(destination);
891dfe94b16Srobert uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
892dfe94b16Srobert memcpy(buf, data, sizeof(data));
893dfe94b16Srobert target->relocateNoSym(buf + 16, R_ARM_REL32, s - p - 16);
894dfe94b16Srobert }
895dfe94b16Srobert
addSymbols(ThunkSection & isec)896dfe94b16Srobert void ThumbV4PILongThunk::addSymbols(ThunkSection &isec) {
897dfe94b16Srobert addSymbol(saver().save("__Thumbv4PILongThunk_" + destination.getName()),
898dfe94b16Srobert STT_FUNC, 1, isec);
899dfe94b16Srobert addSymbol("$t", STT_NOTYPE, 0, isec);
900dfe94b16Srobert addSymbol("$a", STT_NOTYPE, 4, isec);
901dfe94b16Srobert addSymbol("$d", STT_NOTYPE, 16, isec);
902dfe94b16Srobert }
903dfe94b16Srobert
904ece8a530Spatrick // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
writeTo(uint8_t * buf)905ece8a530Spatrick void MipsThunk::writeTo(uint8_t *buf) {
906ece8a530Spatrick uint64_t s = destination.getVA();
907ece8a530Spatrick write32(buf, 0x3c190000); // lui $25, %hi(func)
908ece8a530Spatrick write32(buf + 4, 0x08000000 | (s >> 2)); // j func
909ece8a530Spatrick write32(buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
910ece8a530Spatrick write32(buf + 12, 0x00000000); // nop
911bb684c34Spatrick target->relocateNoSym(buf, R_MIPS_HI16, s);
912bb684c34Spatrick target->relocateNoSym(buf + 8, R_MIPS_LO16, s);
913ece8a530Spatrick }
914ece8a530Spatrick
addSymbols(ThunkSection & isec)915ece8a530Spatrick void MipsThunk::addSymbols(ThunkSection &isec) {
916dfe94b16Srobert addSymbol(saver().save("__LA25Thunk_" + destination.getName()), STT_FUNC, 0,
917ece8a530Spatrick isec);
918ece8a530Spatrick }
919ece8a530Spatrick
getTargetInputSection() const920ece8a530Spatrick InputSection *MipsThunk::getTargetInputSection() const {
921ece8a530Spatrick auto &dr = cast<Defined>(destination);
922ece8a530Spatrick return dyn_cast<InputSection>(dr.section);
923ece8a530Spatrick }
924ece8a530Spatrick
925ece8a530Spatrick // Write microMIPS R2-R5 LA25 thunk code
926ece8a530Spatrick // to call PIC function from the non-PIC one.
writeTo(uint8_t * buf)927ece8a530Spatrick void MicroMipsThunk::writeTo(uint8_t *buf) {
928ece8a530Spatrick uint64_t s = destination.getVA();
929ece8a530Spatrick write16(buf, 0x41b9); // lui $25, %hi(func)
930ece8a530Spatrick write16(buf + 4, 0xd400); // j func
931ece8a530Spatrick write16(buf + 8, 0x3339); // addiu $25, $25, %lo(func)
932ece8a530Spatrick write16(buf + 12, 0x0c00); // nop
933bb684c34Spatrick target->relocateNoSym(buf, R_MICROMIPS_HI16, s);
934bb684c34Spatrick target->relocateNoSym(buf + 4, R_MICROMIPS_26_S1, s);
935bb684c34Spatrick target->relocateNoSym(buf + 8, R_MICROMIPS_LO16, s);
936ece8a530Spatrick }
937ece8a530Spatrick
addSymbols(ThunkSection & isec)938ece8a530Spatrick void MicroMipsThunk::addSymbols(ThunkSection &isec) {
939dfe94b16Srobert Defined *d =
940dfe94b16Srobert addSymbol(saver().save("__microLA25Thunk_" + destination.getName()),
941dfe94b16Srobert STT_FUNC, 0, isec);
942ece8a530Spatrick d->stOther |= STO_MIPS_MICROMIPS;
943ece8a530Spatrick }
944ece8a530Spatrick
getTargetInputSection() const945ece8a530Spatrick InputSection *MicroMipsThunk::getTargetInputSection() const {
946ece8a530Spatrick auto &dr = cast<Defined>(destination);
947ece8a530Spatrick return dyn_cast<InputSection>(dr.section);
948ece8a530Spatrick }
949ece8a530Spatrick
950ece8a530Spatrick // Write microMIPS R6 LA25 thunk code
951ece8a530Spatrick // to call PIC function from the non-PIC one.
writeTo(uint8_t * buf)952ece8a530Spatrick void MicroMipsR6Thunk::writeTo(uint8_t *buf) {
953ece8a530Spatrick uint64_t s = destination.getVA();
954ece8a530Spatrick uint64_t p = getThunkTargetSym()->getVA();
955ece8a530Spatrick write16(buf, 0x1320); // lui $25, %hi(func)
956ece8a530Spatrick write16(buf + 4, 0x3339); // addiu $25, $25, %lo(func)
957ece8a530Spatrick write16(buf + 8, 0x9400); // bc func
958bb684c34Spatrick target->relocateNoSym(buf, R_MICROMIPS_HI16, s);
959bb684c34Spatrick target->relocateNoSym(buf + 4, R_MICROMIPS_LO16, s);
960bb684c34Spatrick target->relocateNoSym(buf + 8, R_MICROMIPS_PC26_S1, s - p - 12);
961ece8a530Spatrick }
962ece8a530Spatrick
addSymbols(ThunkSection & isec)963ece8a530Spatrick void MicroMipsR6Thunk::addSymbols(ThunkSection &isec) {
964dfe94b16Srobert Defined *d =
965dfe94b16Srobert addSymbol(saver().save("__microLA25Thunk_" + destination.getName()),
966dfe94b16Srobert STT_FUNC, 0, isec);
967ece8a530Spatrick d->stOther |= STO_MIPS_MICROMIPS;
968ece8a530Spatrick }
969ece8a530Spatrick
getTargetInputSection() const970ece8a530Spatrick InputSection *MicroMipsR6Thunk::getTargetInputSection() const {
971ece8a530Spatrick auto &dr = cast<Defined>(destination);
972ece8a530Spatrick return dyn_cast<InputSection>(dr.section);
973ece8a530Spatrick }
974ece8a530Spatrick
writePPC32PltCallStub(uint8_t * buf,uint64_t gotPltVA,const InputFile * file,int64_t addend)975bb684c34Spatrick void elf::writePPC32PltCallStub(uint8_t *buf, uint64_t gotPltVA,
976ece8a530Spatrick const InputFile *file, int64_t addend) {
977ece8a530Spatrick if (!config->isPic) {
978ece8a530Spatrick write32(buf + 0, 0x3d600000 | (gotPltVA + 0x8000) >> 16); // lis r11,ha
979ece8a530Spatrick write32(buf + 4, 0x816b0000 | (uint16_t)gotPltVA); // lwz r11,l(r11)
980ece8a530Spatrick write32(buf + 8, 0x7d6903a6); // mtctr r11
981ece8a530Spatrick write32(buf + 12, 0x4e800420); // bctr
982ece8a530Spatrick return;
983ece8a530Spatrick }
984ece8a530Spatrick uint32_t offset;
985ece8a530Spatrick if (addend >= 0x8000) {
986ece8a530Spatrick // The stub loads an address relative to r30 (.got2+Addend). Addend is
987ece8a530Spatrick // almost always 0x8000. The address of .got2 is different in another object
988ece8a530Spatrick // file, so a stub cannot be shared.
989dfe94b16Srobert offset = gotPltVA -
990dfe94b16Srobert (in.ppc32Got2->getParent()->getVA() +
991dfe94b16Srobert (file->ppc32Got2 ? file->ppc32Got2->outSecOff : 0) + addend);
992ece8a530Spatrick } else {
993ece8a530Spatrick // The stub loads an address relative to _GLOBAL_OFFSET_TABLE_ (which is
994ece8a530Spatrick // currently the address of .got).
995ece8a530Spatrick offset = gotPltVA - in.got->getVA();
996ece8a530Spatrick }
997ece8a530Spatrick uint16_t ha = (offset + 0x8000) >> 16, l = (uint16_t)offset;
998ece8a530Spatrick if (ha == 0) {
999ece8a530Spatrick write32(buf + 0, 0x817e0000 | l); // lwz r11,l(r30)
1000ece8a530Spatrick write32(buf + 4, 0x7d6903a6); // mtctr r11
1001ece8a530Spatrick write32(buf + 8, 0x4e800420); // bctr
1002ece8a530Spatrick write32(buf + 12, 0x60000000); // nop
1003ece8a530Spatrick } else {
1004ece8a530Spatrick write32(buf + 0, 0x3d7e0000 | ha); // addis r11,r30,ha
1005ece8a530Spatrick write32(buf + 4, 0x816b0000 | l); // lwz r11,l(r11)
1006ece8a530Spatrick write32(buf + 8, 0x7d6903a6); // mtctr r11
1007ece8a530Spatrick write32(buf + 12, 0x4e800420); // bctr
1008ece8a530Spatrick }
1009ece8a530Spatrick }
1010ece8a530Spatrick
writeTo(uint8_t * buf)1011ece8a530Spatrick void PPC32PltCallStub::writeTo(uint8_t *buf) {
1012ece8a530Spatrick writePPC32PltCallStub(buf, destination.getGotPltVA(), file, addend);
1013ece8a530Spatrick }
1014ece8a530Spatrick
addSymbols(ThunkSection & isec)1015ece8a530Spatrick void PPC32PltCallStub::addSymbols(ThunkSection &isec) {
1016ece8a530Spatrick std::string buf;
1017ece8a530Spatrick raw_string_ostream os(buf);
1018ece8a530Spatrick os << format_hex_no_prefix(addend, 8);
1019ece8a530Spatrick if (!config->isPic)
1020ece8a530Spatrick os << ".plt_call32.";
1021ece8a530Spatrick else if (addend >= 0x8000)
1022ece8a530Spatrick os << ".got2.plt_pic32.";
1023ece8a530Spatrick else
1024ece8a530Spatrick os << ".plt_pic32.";
1025ece8a530Spatrick os << destination.getName();
1026dfe94b16Srobert addSymbol(saver().save(os.str()), STT_FUNC, 0, isec);
1027ece8a530Spatrick }
1028ece8a530Spatrick
isCompatibleWith(const InputSection & isec,const Relocation & rel) const1029ece8a530Spatrick bool PPC32PltCallStub::isCompatibleWith(const InputSection &isec,
1030ece8a530Spatrick const Relocation &rel) const {
1031ece8a530Spatrick return !config->isPic || (isec.file == file && rel.addend == addend);
1032ece8a530Spatrick }
1033ece8a530Spatrick
addSymbols(ThunkSection & isec)1034ece8a530Spatrick void PPC32LongThunk::addSymbols(ThunkSection &isec) {
1035dfe94b16Srobert addSymbol(saver().save("__LongThunk_" + destination.getName()), STT_FUNC, 0,
1036ece8a530Spatrick isec);
1037ece8a530Spatrick }
1038ece8a530Spatrick
writeTo(uint8_t * buf)1039ece8a530Spatrick void PPC32LongThunk::writeTo(uint8_t *buf) {
1040ece8a530Spatrick auto ha = [](uint32_t v) -> uint16_t { return (v + 0x8000) >> 16; };
1041ece8a530Spatrick auto lo = [](uint32_t v) -> uint16_t { return v; };
1042ece8a530Spatrick uint32_t d = destination.getVA(addend);
1043ece8a530Spatrick if (config->isPic) {
1044ece8a530Spatrick uint32_t off = d - (getThunkTargetSym()->getVA() + 8);
1045ece8a530Spatrick write32(buf + 0, 0x7c0802a6); // mflr r12,0
1046ece8a530Spatrick write32(buf + 4, 0x429f0005); // bcl r20,r31,.+4
1047ece8a530Spatrick write32(buf + 8, 0x7d8802a6); // mtctr r12
1048ece8a530Spatrick write32(buf + 12, 0x3d8c0000 | ha(off)); // addis r12,r12,off@ha
1049ece8a530Spatrick write32(buf + 16, 0x398c0000 | lo(off)); // addi r12,r12,off@l
1050ece8a530Spatrick write32(buf + 20, 0x7c0803a6); // mtlr r0
1051ece8a530Spatrick buf += 24;
1052ece8a530Spatrick } else {
1053ece8a530Spatrick write32(buf + 0, 0x3d800000 | ha(d)); // lis r12,d@ha
1054ece8a530Spatrick write32(buf + 4, 0x398c0000 | lo(d)); // addi r12,r12,d@l
1055ece8a530Spatrick buf += 8;
1056ece8a530Spatrick }
1057ece8a530Spatrick write32(buf + 0, 0x7d8903a6); // mtctr r12
1058ece8a530Spatrick write32(buf + 4, 0x4e800420); // bctr
1059ece8a530Spatrick }
1060ece8a530Spatrick
writePPC64LoadAndBranch(uint8_t * buf,int64_t offset)1061bb684c34Spatrick void elf::writePPC64LoadAndBranch(uint8_t *buf, int64_t offset) {
1062ece8a530Spatrick uint16_t offHa = (offset + 0x8000) >> 16;
1063ece8a530Spatrick uint16_t offLo = offset & 0xffff;
1064ece8a530Spatrick
1065ece8a530Spatrick write32(buf + 0, 0x3d820000 | offHa); // addis r12, r2, OffHa
1066ece8a530Spatrick write32(buf + 4, 0xe98c0000 | offLo); // ld r12, OffLo(r12)
1067ece8a530Spatrick write32(buf + 8, 0x7d8903a6); // mtctr r12
1068ece8a530Spatrick write32(buf + 12, 0x4e800420); // bctr
1069ece8a530Spatrick }
1070ece8a530Spatrick
writeTo(uint8_t * buf)1071ece8a530Spatrick void PPC64PltCallStub::writeTo(uint8_t *buf) {
1072ece8a530Spatrick int64_t offset = destination.getGotPltVA() - getPPC64TocBase();
1073ece8a530Spatrick // Save the TOC pointer to the save-slot reserved in the call frame.
1074ece8a530Spatrick write32(buf + 0, 0xf8410018); // std r2,24(r1)
1075ece8a530Spatrick writePPC64LoadAndBranch(buf + 4, offset);
1076ece8a530Spatrick }
1077ece8a530Spatrick
addSymbols(ThunkSection & isec)1078ece8a530Spatrick void PPC64PltCallStub::addSymbols(ThunkSection &isec) {
1079dfe94b16Srobert Defined *s = addSymbol(saver().save("__plt_" + destination.getName()),
1080dfe94b16Srobert STT_FUNC, 0, isec);
1081ece8a530Spatrick s->needsTocRestore = true;
1082ece8a530Spatrick s->file = destination.file;
1083ece8a530Spatrick }
1084ece8a530Spatrick
isCompatibleWith(const InputSection & isec,const Relocation & rel) const10851cf9926bSpatrick bool PPC64PltCallStub::isCompatibleWith(const InputSection &isec,
10861cf9926bSpatrick const Relocation &rel) const {
10871cf9926bSpatrick return rel.type == R_PPC64_REL24 || rel.type == R_PPC64_REL14;
10881cf9926bSpatrick }
10891cf9926bSpatrick
writeTo(uint8_t * buf)1090bb684c34Spatrick void PPC64R2SaveStub::writeTo(uint8_t *buf) {
10911cf9926bSpatrick const int64_t offset = computeOffset();
1092bb684c34Spatrick write32(buf + 0, 0xf8410018); // std r2,24(r1)
10931cf9926bSpatrick // The branch offset needs to fit in 26 bits.
10941cf9926bSpatrick if (getMayUseShortThunk()) {
1095bb684c34Spatrick write32(buf + 4, 0x48000000 | (offset & 0x03fffffc)); // b <offset>
10961cf9926bSpatrick } else if (isInt<34>(offset)) {
10971cf9926bSpatrick int nextInstOffset;
10981cf9926bSpatrick uint64_t tocOffset = destination.getVA() - getPPC64TocBase();
10991cf9926bSpatrick if (tocOffset >> 16 > 0) {
11001cf9926bSpatrick const uint64_t addi = ADDI_R12_TO_R12_NO_DISP | (tocOffset & 0xffff);
1101dfe94b16Srobert const uint64_t addis =
1102dfe94b16Srobert ADDIS_R12_TO_R2_NO_DISP | ((tocOffset >> 16) & 0xffff);
11031cf9926bSpatrick write32(buf + 4, addis); // addis r12, r2 , top of offset
11041cf9926bSpatrick write32(buf + 8, addi); // addi r12, r12, bottom of offset
11051cf9926bSpatrick nextInstOffset = 12;
11061cf9926bSpatrick } else {
11071cf9926bSpatrick const uint64_t addi = ADDI_R12_TO_R2_NO_DISP | (tocOffset & 0xffff);
11081cf9926bSpatrick write32(buf + 4, addi); // addi r12, r2, offset
11091cf9926bSpatrick nextInstOffset = 8;
11101cf9926bSpatrick }
11111cf9926bSpatrick write32(buf + nextInstOffset, MTCTR_R12); // mtctr r12
11121cf9926bSpatrick write32(buf + nextInstOffset + 4, BCTR); // bctr
11131cf9926bSpatrick } else {
11141cf9926bSpatrick in.ppc64LongBranchTarget->addEntry(&destination, addend);
11151cf9926bSpatrick const int64_t offsetFromTOC =
11161cf9926bSpatrick in.ppc64LongBranchTarget->getEntryVA(&destination, addend) -
11171cf9926bSpatrick getPPC64TocBase();
11181cf9926bSpatrick writePPC64LoadAndBranch(buf + 4, offsetFromTOC);
11191cf9926bSpatrick }
1120bb684c34Spatrick }
1121bb684c34Spatrick
addSymbols(ThunkSection & isec)1122bb684c34Spatrick void PPC64R2SaveStub::addSymbols(ThunkSection &isec) {
1123dfe94b16Srobert Defined *s = addSymbol(saver().save("__toc_save_" + destination.getName()),
1124bb684c34Spatrick STT_FUNC, 0, isec);
1125bb684c34Spatrick s->needsTocRestore = true;
1126bb684c34Spatrick }
1127bb684c34Spatrick
isCompatibleWith(const InputSection & isec,const Relocation & rel) const11281cf9926bSpatrick bool PPC64R2SaveStub::isCompatibleWith(const InputSection &isec,
11291cf9926bSpatrick const Relocation &rel) const {
11301cf9926bSpatrick return rel.type == R_PPC64_REL24 || rel.type == R_PPC64_REL14;
11311cf9926bSpatrick }
11321cf9926bSpatrick
writeTo(uint8_t * buf)11331cf9926bSpatrick void PPC64R12SetupStub::writeTo(uint8_t *buf) {
11341cf9926bSpatrick int64_t offset = destination.getVA() - getThunkTargetSym()->getVA();
11351cf9926bSpatrick if (!isInt<34>(offset))
11361cf9926bSpatrick reportRangeError(buf, offset, 34, destination, "R12 setup stub offset");
11371cf9926bSpatrick
11381cf9926bSpatrick int nextInstOffset;
1139dfe94b16Srobert if (!config->power10Stubs) {
11401cf9926bSpatrick uint32_t off = destination.getVA(addend) - getThunkTargetSym()->getVA() - 8;
11411cf9926bSpatrick write32(buf + 0, 0x7c0802a6); // mflr r12
11421cf9926bSpatrick write32(buf + 4, 0x429f0005); // bcl 20,31,.+4
11431cf9926bSpatrick write32(buf + 8, 0x7d6802a6); // mflr r11
11441cf9926bSpatrick write32(buf + 12, 0x7d8803a6); // mtlr r12
11451cf9926bSpatrick write32(buf + 16, 0x3d8b0000 | computeHiBits(off));// addis r12,r11,off@ha
11461cf9926bSpatrick write32(buf + 20, 0x398c0000 | (off & 0xffff)); // addi r12,r12,off@l
11471cf9926bSpatrick nextInstOffset = 24;
11481cf9926bSpatrick } else {
11491cf9926bSpatrick uint64_t paddi = PADDI_R12_NO_DISP | (((offset >> 16) & 0x3ffff) << 32) |
11501cf9926bSpatrick (offset & 0xffff);
11511cf9926bSpatrick writePrefixedInstruction(buf + 0, paddi); // paddi r12, 0, func@pcrel, 1
11521cf9926bSpatrick nextInstOffset = 8;
11531cf9926bSpatrick }
11541cf9926bSpatrick write32(buf + nextInstOffset, MTCTR_R12); // mtctr r12
11551cf9926bSpatrick write32(buf + nextInstOffset + 4, BCTR); // bctr
11561cf9926bSpatrick }
11571cf9926bSpatrick
addSymbols(ThunkSection & isec)11581cf9926bSpatrick void PPC64R12SetupStub::addSymbols(ThunkSection &isec) {
1159dfe94b16Srobert addSymbol(saver().save("__gep_setup_" + destination.getName()), STT_FUNC, 0,
11601cf9926bSpatrick isec);
11611cf9926bSpatrick }
11621cf9926bSpatrick
isCompatibleWith(const InputSection & isec,const Relocation & rel) const11631cf9926bSpatrick bool PPC64R12SetupStub::isCompatibleWith(const InputSection &isec,
11641cf9926bSpatrick const Relocation &rel) const {
11651cf9926bSpatrick return rel.type == R_PPC64_REL24_NOTOC;
11661cf9926bSpatrick }
11671cf9926bSpatrick
writeTo(uint8_t * buf)11681cf9926bSpatrick void PPC64PCRelPLTStub::writeTo(uint8_t *buf) {
11691cf9926bSpatrick int nextInstOffset = 0;
11701cf9926bSpatrick int64_t offset = destination.getGotPltVA() - getThunkTargetSym()->getVA();
11711cf9926bSpatrick
1172dfe94b16Srobert if (config->power10Stubs) {
11731cf9926bSpatrick if (!isInt<34>(offset))
11741cf9926bSpatrick reportRangeError(buf, offset, 34, destination,
11751cf9926bSpatrick "PC-relative PLT stub offset");
11761cf9926bSpatrick const uint64_t pld = PLD_R12_NO_DISP | (((offset >> 16) & 0x3ffff) << 32) |
11771cf9926bSpatrick (offset & 0xffff);
11781cf9926bSpatrick writePrefixedInstruction(buf + 0, pld); // pld r12, func@plt@pcrel
11791cf9926bSpatrick nextInstOffset = 8;
11801cf9926bSpatrick } else {
11811cf9926bSpatrick uint32_t off = destination.getVA(addend) - getThunkTargetSym()->getVA() - 8;
11821cf9926bSpatrick write32(buf + 0, 0x7c0802a6); // mflr r12
11831cf9926bSpatrick write32(buf + 4, 0x429f0005); // bcl 20,31,.+4
11841cf9926bSpatrick write32(buf + 8, 0x7d6802a6); // mflr r11
11851cf9926bSpatrick write32(buf + 12, 0x7d8803a6); // mtlr r12
11861cf9926bSpatrick write32(buf + 16, 0x3d8b0000 | computeHiBits(off)); // addis r12,r11,off@ha
11871cf9926bSpatrick write32(buf + 20, 0x398c0000 | (off & 0xffff)); // addi r12,r12,off@l
11881cf9926bSpatrick nextInstOffset = 24;
11891cf9926bSpatrick }
11901cf9926bSpatrick write32(buf + nextInstOffset, MTCTR_R12); // mtctr r12
11911cf9926bSpatrick write32(buf + nextInstOffset + 4, BCTR); // bctr
11921cf9926bSpatrick }
11931cf9926bSpatrick
addSymbols(ThunkSection & isec)11941cf9926bSpatrick void PPC64PCRelPLTStub::addSymbols(ThunkSection &isec) {
1195dfe94b16Srobert addSymbol(saver().save("__plt_pcrel_" + destination.getName()), STT_FUNC, 0,
11961cf9926bSpatrick isec);
11971cf9926bSpatrick }
11981cf9926bSpatrick
isCompatibleWith(const InputSection & isec,const Relocation & rel) const11991cf9926bSpatrick bool PPC64PCRelPLTStub::isCompatibleWith(const InputSection &isec,
12001cf9926bSpatrick const Relocation &rel) const {
12011cf9926bSpatrick return rel.type == R_PPC64_REL24_NOTOC;
12021cf9926bSpatrick }
12031cf9926bSpatrick
writeTo(uint8_t * buf)1204ece8a530Spatrick void PPC64LongBranchThunk::writeTo(uint8_t *buf) {
1205ece8a530Spatrick int64_t offset = in.ppc64LongBranchTarget->getEntryVA(&destination, addend) -
1206ece8a530Spatrick getPPC64TocBase();
1207ece8a530Spatrick writePPC64LoadAndBranch(buf, offset);
1208ece8a530Spatrick }
1209ece8a530Spatrick
addSymbols(ThunkSection & isec)1210ece8a530Spatrick void PPC64LongBranchThunk::addSymbols(ThunkSection &isec) {
1211dfe94b16Srobert addSymbol(saver().save("__long_branch_" + destination.getName()), STT_FUNC, 0,
1212ece8a530Spatrick isec);
1213ece8a530Spatrick }
1214ece8a530Spatrick
isCompatibleWith(const InputSection & isec,const Relocation & rel) const12151cf9926bSpatrick bool PPC64LongBranchThunk::isCompatibleWith(const InputSection &isec,
12161cf9926bSpatrick const Relocation &rel) const {
12171cf9926bSpatrick return rel.type == R_PPC64_REL24 || rel.type == R_PPC64_REL14;
12181cf9926bSpatrick }
12191cf9926bSpatrick
Thunk(Symbol & d,int64_t a)1220*b1fea01fStobhe Thunk::Thunk(Symbol &d, int64_t a) : destination(d), addend(a), offset(0) {
1221*b1fea01fStobhe destination.thunkAccessed = true;
1222*b1fea01fStobhe }
1223ece8a530Spatrick
1224ece8a530Spatrick Thunk::~Thunk() = default;
1225ece8a530Spatrick
addThunkAArch64(RelType type,Symbol & s,int64_t a)1226ece8a530Spatrick static Thunk *addThunkAArch64(RelType type, Symbol &s, int64_t a) {
1227bb684c34Spatrick if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 &&
1228bb684c34Spatrick type != R_AARCH64_PLT32)
1229ece8a530Spatrick fatal("unrecognized relocation type");
1230ece8a530Spatrick if (config->picThunk)
1231ece8a530Spatrick return make<AArch64ADRPThunk>(s, a);
1232ece8a530Spatrick return make<AArch64ABSLongThunk>(s, a);
1233ece8a530Spatrick }
1234ece8a530Spatrick
1235dfe94b16Srobert // Creates a thunk for long branches or Thumb-ARM interworking.
1236dfe94b16Srobert // Arm Architectures v4t does not support Thumb2 technology, and does not
1237dfe94b16Srobert // support BLX or LDR Arm/Thumb state switching. This means that
1238dfe94b16Srobert // - MOVT and MOVW instructions cannot be used.
1239dfe94b16Srobert // - We can't rewrite BL in place to BLX. We will need thunks.
1240dfe94b16Srobert //
1241dfe94b16Srobert // TODO: use B for short Thumb->Arm thunks instead of LDR (this doesn't work for
1242dfe94b16Srobert // Arm->Thumb, as in Arm state no BX PC trick; it doesn't switch state).
addThunkArmv4(RelType reloc,Symbol & s,int64_t a)1243dfe94b16Srobert static Thunk *addThunkArmv4(RelType reloc, Symbol &s, int64_t a) {
1244dfe94b16Srobert bool thumb_target = s.getVA(a) & 1;
1245dfe94b16Srobert
1246dfe94b16Srobert switch (reloc) {
1247dfe94b16Srobert case R_ARM_PC24:
1248dfe94b16Srobert case R_ARM_PLT32:
1249dfe94b16Srobert case R_ARM_JUMP24:
1250dfe94b16Srobert case R_ARM_CALL:
1251dfe94b16Srobert if (config->picThunk) {
1252dfe94b16Srobert if (thumb_target)
1253dfe94b16Srobert return make<ARMV4PILongBXThunk>(s, a);
1254dfe94b16Srobert return make<ARMV4PILongThunk>(s, a);
1255dfe94b16Srobert }
1256dfe94b16Srobert if (thumb_target)
1257dfe94b16Srobert return make<ARMV4ABSLongBXThunk>(s, a);
1258dfe94b16Srobert return make<ARMV5LongLdrPcThunk>(s, a);
1259dfe94b16Srobert case R_ARM_THM_CALL:
1260dfe94b16Srobert if (config->picThunk) {
1261dfe94b16Srobert if (thumb_target)
1262dfe94b16Srobert return make<ThumbV4PILongThunk>(s, a);
1263dfe94b16Srobert return make<ThumbV4PILongBXThunk>(s, a);
1264dfe94b16Srobert }
1265dfe94b16Srobert if (thumb_target)
1266dfe94b16Srobert return make<ThumbV4ABSLongThunk>(s, a);
1267dfe94b16Srobert return make<ThumbV4ABSLongBXThunk>(s, a);
1268dfe94b16Srobert }
1269dfe94b16Srobert fatal("relocation " + toString(reloc) + " to " + toString(s) +
1270dfe94b16Srobert " not supported for Armv4 or Armv4T target");
1271dfe94b16Srobert }
1272dfe94b16Srobert
1273dfe94b16Srobert // Creates a thunk for Thumb-ARM interworking compatible with Armv5 and Armv6.
1274dfe94b16Srobert // Arm Architectures v5 and v6 do not support Thumb2 technology. This means that
1275ece8a530Spatrick // - MOVT and MOVW instructions cannot be used
1276ece8a530Spatrick // - Only Thumb relocation that can generate a Thunk is a BL, this can always
1277ece8a530Spatrick // be transformed into a BLX
addThunkArmv5v6(RelType reloc,Symbol & s,int64_t a)1278dfe94b16Srobert static Thunk *addThunkArmv5v6(RelType reloc, Symbol &s, int64_t a) {
1279ece8a530Spatrick switch (reloc) {
1280ece8a530Spatrick case R_ARM_PC24:
1281ece8a530Spatrick case R_ARM_PLT32:
1282ece8a530Spatrick case R_ARM_JUMP24:
1283ece8a530Spatrick case R_ARM_CALL:
1284ece8a530Spatrick case R_ARM_THM_CALL:
1285ece8a530Spatrick if (config->picThunk)
1286dfe94b16Srobert return make<ARMV4PILongBXThunk>(s, a);
1287dfe94b16Srobert return make<ARMV5LongLdrPcThunk>(s, a);
1288ece8a530Spatrick }
1289ece8a530Spatrick fatal("relocation " + toString(reloc) + " to " + toString(s) +
1290ece8a530Spatrick " not supported for Armv5 or Armv6 targets");
1291ece8a530Spatrick }
1292ece8a530Spatrick
1293ece8a530Spatrick // Create a thunk for Thumb long branch on V6-M.
1294ece8a530Spatrick // Arm Architecture v6-M only supports Thumb instructions. This means
1295ece8a530Spatrick // - MOVT and MOVW instructions cannot be used.
1296ece8a530Spatrick // - Only a limited number of instructions can access registers r8 and above
1297ece8a530Spatrick // - No interworking support is needed (all Thumb).
addThunkV6M(RelType reloc,Symbol & s,int64_t a)12981cf9926bSpatrick static Thunk *addThunkV6M(RelType reloc, Symbol &s, int64_t a) {
1299ece8a530Spatrick switch (reloc) {
1300ece8a530Spatrick case R_ARM_THM_JUMP19:
1301ece8a530Spatrick case R_ARM_THM_JUMP24:
1302ece8a530Spatrick case R_ARM_THM_CALL:
1303ece8a530Spatrick if (config->isPic)
13041cf9926bSpatrick return make<ThumbV6MPILongThunk>(s, a);
13051cf9926bSpatrick return make<ThumbV6MABSLongThunk>(s, a);
1306ece8a530Spatrick }
1307ece8a530Spatrick fatal("relocation " + toString(reloc) + " to " + toString(s) +
1308ece8a530Spatrick " not supported for Armv6-M targets");
1309ece8a530Spatrick }
1310ece8a530Spatrick
1311ece8a530Spatrick // Creates a thunk for Thumb-ARM interworking or branch range extension.
addThunkArm(RelType reloc,Symbol & s,int64_t a)13121cf9926bSpatrick static Thunk *addThunkArm(RelType reloc, Symbol &s, int64_t a) {
1313ece8a530Spatrick // Decide which Thunk is needed based on:
1314ece8a530Spatrick // Available instruction set
1315ece8a530Spatrick // - An Arm Thunk can only be used if Arm state is available.
1316ece8a530Spatrick // - A Thumb Thunk can only be used if Thumb state is available.
1317ece8a530Spatrick // - Can only use a Thunk if it uses instructions that the Target supports.
1318ece8a530Spatrick // Relocation is branch or branch and link
1319ece8a530Spatrick // - Branch instructions cannot change state, can only select Thunk that
1320ece8a530Spatrick // starts in the same state as the caller.
1321ece8a530Spatrick // - Branch and link relocations can change state, can select Thunks from
1322ece8a530Spatrick // either Arm or Thumb.
1323ece8a530Spatrick // Position independent Thunks if we require position independent code.
1324ece8a530Spatrick
1325ece8a530Spatrick // Handle architectures that have restrictions on the instructions that they
1326ece8a530Spatrick // can use in Thunks. The flags below are set by reading the BuildAttributes
1327ece8a530Spatrick // of the input objects. InputFiles.cpp contains the mapping from ARM
1328ece8a530Spatrick // architecture to flag.
1329ece8a530Spatrick if (!config->armHasMovtMovw) {
1330dfe94b16Srobert if (config->armJ1J2BranchEncoding)
13311cf9926bSpatrick return addThunkV6M(reloc, s, a);
1332dfe94b16Srobert if (config->armHasBlx)
1333dfe94b16Srobert return addThunkArmv5v6(reloc, s, a);
1334dfe94b16Srobert return addThunkArmv4(reloc, s, a);
1335ece8a530Spatrick }
1336ece8a530Spatrick
1337ece8a530Spatrick switch (reloc) {
1338ece8a530Spatrick case R_ARM_PC24:
1339ece8a530Spatrick case R_ARM_PLT32:
1340ece8a530Spatrick case R_ARM_JUMP24:
1341ece8a530Spatrick case R_ARM_CALL:
1342ece8a530Spatrick if (config->picThunk)
13431cf9926bSpatrick return make<ARMV7PILongThunk>(s, a);
13441cf9926bSpatrick return make<ARMV7ABSLongThunk>(s, a);
1345ece8a530Spatrick case R_ARM_THM_JUMP19:
1346ece8a530Spatrick case R_ARM_THM_JUMP24:
1347ece8a530Spatrick case R_ARM_THM_CALL:
1348ece8a530Spatrick if (config->picThunk)
13491cf9926bSpatrick return make<ThumbV7PILongThunk>(s, a);
13501cf9926bSpatrick return make<ThumbV7ABSLongThunk>(s, a);
1351ece8a530Spatrick }
1352ece8a530Spatrick fatal("unrecognized relocation type");
1353ece8a530Spatrick }
1354ece8a530Spatrick
addThunkMips(RelType type,Symbol & s)1355ece8a530Spatrick static Thunk *addThunkMips(RelType type, Symbol &s) {
1356ece8a530Spatrick if ((s.stOther & STO_MIPS_MICROMIPS) && isMipsR6())
1357ece8a530Spatrick return make<MicroMipsR6Thunk>(s);
1358ece8a530Spatrick if (s.stOther & STO_MIPS_MICROMIPS)
1359ece8a530Spatrick return make<MicroMipsThunk>(s);
1360ece8a530Spatrick return make<MipsThunk>(s);
1361ece8a530Spatrick }
1362ece8a530Spatrick
addThunkPPC32(const InputSection & isec,const Relocation & rel,Symbol & s)1363ece8a530Spatrick static Thunk *addThunkPPC32(const InputSection &isec, const Relocation &rel,
1364ece8a530Spatrick Symbol &s) {
1365ece8a530Spatrick assert((rel.type == R_PPC_LOCAL24PC || rel.type == R_PPC_REL24 ||
1366ece8a530Spatrick rel.type == R_PPC_PLTREL24) &&
1367ece8a530Spatrick "unexpected relocation type for thunk");
1368ece8a530Spatrick if (s.isInPlt())
1369ece8a530Spatrick return make<PPC32PltCallStub>(isec, rel, s);
1370ece8a530Spatrick return make<PPC32LongThunk>(s, rel.addend);
1371ece8a530Spatrick }
1372ece8a530Spatrick
addThunkPPC64(RelType type,Symbol & s,int64_t a)1373ece8a530Spatrick static Thunk *addThunkPPC64(RelType type, Symbol &s, int64_t a) {
13741cf9926bSpatrick assert((type == R_PPC64_REL14 || type == R_PPC64_REL24 ||
13751cf9926bSpatrick type == R_PPC64_REL24_NOTOC) &&
1376bb684c34Spatrick "unexpected relocation type for thunk");
1377ece8a530Spatrick if (s.isInPlt())
13781cf9926bSpatrick return type == R_PPC64_REL24_NOTOC ? (Thunk *)make<PPC64PCRelPLTStub>(s)
13791cf9926bSpatrick : (Thunk *)make<PPC64PltCallStub>(s);
1380ece8a530Spatrick
1381bb684c34Spatrick // This check looks at the st_other bits of the callee. If the value is 1
13821cf9926bSpatrick // then the callee clobbers the TOC and we need an R2 save stub when RelType
13831cf9926bSpatrick // is R_PPC64_REL14 or R_PPC64_REL24.
13841cf9926bSpatrick if ((type == R_PPC64_REL14 || type == R_PPC64_REL24) && (s.stOther >> 5) == 1)
13851cf9926bSpatrick return make<PPC64R2SaveStub>(s, a);
13861cf9926bSpatrick
13871cf9926bSpatrick if (type == R_PPC64_REL24_NOTOC)
1388dfe94b16Srobert return make<PPC64R12SetupStub>(s);
1389bb684c34Spatrick
1390ece8a530Spatrick if (config->picThunk)
1391ece8a530Spatrick return make<PPC64PILongBranchThunk>(s, a);
1392ece8a530Spatrick
1393ece8a530Spatrick return make<PPC64PDLongBranchThunk>(s, a);
1394ece8a530Spatrick }
1395ece8a530Spatrick
addThunk(const InputSection & isec,Relocation & rel)1396bb684c34Spatrick Thunk *elf::addThunk(const InputSection &isec, Relocation &rel) {
1397ece8a530Spatrick Symbol &s = *rel.sym;
1398ece8a530Spatrick int64_t a = rel.addend;
1399ece8a530Spatrick
1400ece8a530Spatrick if (config->emachine == EM_AARCH64)
1401ece8a530Spatrick return addThunkAArch64(rel.type, s, a);
1402ece8a530Spatrick
1403ece8a530Spatrick if (config->emachine == EM_ARM)
14041cf9926bSpatrick return addThunkArm(rel.type, s, a);
1405ece8a530Spatrick
1406ece8a530Spatrick if (config->emachine == EM_MIPS)
1407ece8a530Spatrick return addThunkMips(rel.type, s);
1408ece8a530Spatrick
1409ece8a530Spatrick if (config->emachine == EM_PPC)
1410ece8a530Spatrick return addThunkPPC32(isec, rel, s);
1411ece8a530Spatrick
1412ece8a530Spatrick if (config->emachine == EM_PPC64)
1413ece8a530Spatrick return addThunkPPC64(rel.type, s, a);
1414ece8a530Spatrick
1415ece8a530Spatrick llvm_unreachable("add Thunk only supported for ARM, Mips and PowerPC");
1416ece8a530Spatrick }
1417