10b57cec5SDimitry Andric //===- Thunks.cpp --------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===---------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains Thunk subclasses.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric // A thunk is a small piece of code written after an input section
120b57cec5SDimitry Andric // which is used to jump between "incompatible" functions
130b57cec5SDimitry Andric // such as MIPS PIC and non-PIC or ARM non-Thumb and Thumb functions.
140b57cec5SDimitry Andric //
150b57cec5SDimitry Andric // If a jump target is too far and its address doesn't fit to a
160b57cec5SDimitry Andric // short jump instruction, we need to create a thunk too, but we
170b57cec5SDimitry Andric // haven't supported it yet.
180b57cec5SDimitry Andric //
190b57cec5SDimitry Andric // i386 and x86-64 don't need thunks.
200b57cec5SDimitry Andric //
210b57cec5SDimitry Andric //===---------------------------------------------------------------------===//
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric #include "Thunks.h"
240b57cec5SDimitry Andric #include "Config.h"
2581ad6265SDimitry Andric #include "InputFiles.h"
260b57cec5SDimitry Andric #include "InputSection.h"
270b57cec5SDimitry Andric #include "OutputSections.h"
280b57cec5SDimitry Andric #include "Symbols.h"
290b57cec5SDimitry Andric #include "SyntheticSections.h"
300b57cec5SDimitry Andric #include "Target.h"
3104eeddc0SDimitry Andric #include "lld/Common/CommonLinkerContext.h"
320b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
330b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
340b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
350b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
360b57cec5SDimitry Andric #include <cstdint>
370b57cec5SDimitry Andric #include <cstring>
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric using namespace llvm;
400b57cec5SDimitry Andric using namespace llvm::object;
410b57cec5SDimitry Andric using namespace llvm::ELF;
425ffd83dbSDimitry Andric using namespace lld;
435ffd83dbSDimitry Andric using namespace lld::elf;
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric namespace {
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric // AArch64 long range Thunks
480b57cec5SDimitry Andric class AArch64ABSLongThunk final : public Thunk {
490b57cec5SDimitry Andric public:
50480093f4SDimitry Andric   AArch64ABSLongThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
510b57cec5SDimitry Andric   uint32_t size() override { return 16; }
520b57cec5SDimitry Andric   void writeTo(uint8_t *buf) override;
530b57cec5SDimitry Andric   void addSymbols(ThunkSection &isec) override;
540b57cec5SDimitry Andric };
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric class AArch64ADRPThunk final : public Thunk {
570b57cec5SDimitry Andric public:
58480093f4SDimitry Andric   AArch64ADRPThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
590b57cec5SDimitry Andric   uint32_t size() override { return 12; }
600b57cec5SDimitry Andric   void writeTo(uint8_t *buf) override;
610b57cec5SDimitry Andric   void addSymbols(ThunkSection &isec) override;
620b57cec5SDimitry Andric };
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric // Base class for ARM thunks.
650b57cec5SDimitry Andric //
660b57cec5SDimitry Andric // An ARM thunk may be either short or long. A short thunk is simply a branch
670b57cec5SDimitry Andric // (B) instruction, and it may be used to call ARM functions when the distance
680b57cec5SDimitry Andric // from the thunk to the target is less than 32MB. Long thunks can branch to any
690b57cec5SDimitry Andric // virtual address and can switch between ARM and Thumb, and they are
700b57cec5SDimitry Andric // implemented in the derived classes. This class tries to create a short thunk
710b57cec5SDimitry Andric // if the target is in range, otherwise it creates a long thunk.
720b57cec5SDimitry Andric class ARMThunk : public Thunk {
730b57cec5SDimitry Andric public:
74fe6060f1SDimitry Andric   ARMThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric   bool getMayUseShortThunk();
770b57cec5SDimitry Andric   uint32_t size() override { return getMayUseShortThunk() ? 4 : sizeLong(); }
780b57cec5SDimitry Andric   void writeTo(uint8_t *buf) override;
790b57cec5SDimitry Andric   bool isCompatibleWith(const InputSection &isec,
800b57cec5SDimitry Andric                         const Relocation &rel) const override;
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric   // Returns the size of a long thunk.
830b57cec5SDimitry Andric   virtual uint32_t sizeLong() = 0;
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   // Writes a long thunk to Buf.
860b57cec5SDimitry Andric   virtual void writeLong(uint8_t *buf) = 0;
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric private:
890b57cec5SDimitry Andric   // This field tracks whether all previously considered layouts would allow
900b57cec5SDimitry Andric   // this thunk to be short. If we have ever needed a long thunk, we always
910b57cec5SDimitry Andric   // create a long thunk, even if the thunk may be short given the current
920b57cec5SDimitry Andric   // distance to the target. We do this because transitioning from long to short
930b57cec5SDimitry Andric   // can create layout oscillations in certain corner cases which would prevent
940b57cec5SDimitry Andric   // the layout from converging.
950b57cec5SDimitry Andric   bool mayUseShortThunk = true;
960b57cec5SDimitry Andric };
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric // Base class for Thumb-2 thunks.
990b57cec5SDimitry Andric //
1000b57cec5SDimitry Andric // This class is similar to ARMThunk, but it uses the Thumb-2 B.W instruction
1010b57cec5SDimitry Andric // which has a range of 16MB.
1020b57cec5SDimitry Andric class ThumbThunk : public Thunk {
1030b57cec5SDimitry Andric public:
104fe6060f1SDimitry Andric   ThumbThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {
105fe6060f1SDimitry Andric     alignment = 2;
106fe6060f1SDimitry Andric   }
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   bool getMayUseShortThunk();
1090b57cec5SDimitry Andric   uint32_t size() override { return getMayUseShortThunk() ? 4 : sizeLong(); }
1100b57cec5SDimitry Andric   void writeTo(uint8_t *buf) override;
1110b57cec5SDimitry Andric   bool isCompatibleWith(const InputSection &isec,
1120b57cec5SDimitry Andric                         const Relocation &rel) const override;
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   // Returns the size of a long thunk.
1150b57cec5SDimitry Andric   virtual uint32_t sizeLong() = 0;
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   // Writes a long thunk to Buf.
1180b57cec5SDimitry Andric   virtual void writeLong(uint8_t *buf) = 0;
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric private:
1210b57cec5SDimitry Andric   // See comment in ARMThunk above.
1220b57cec5SDimitry Andric   bool mayUseShortThunk = true;
1230b57cec5SDimitry Andric };
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric // Specific ARM Thunk implementations. The naming convention is:
1260b57cec5SDimitry Andric // Source State, TargetState, Target Requirement, ABS or PI, Range
1270b57cec5SDimitry Andric class ARMV7ABSLongThunk final : public ARMThunk {
1280b57cec5SDimitry Andric public:
129fe6060f1SDimitry Andric   ARMV7ABSLongThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric   uint32_t sizeLong() override { return 12; }
1320b57cec5SDimitry Andric   void writeLong(uint8_t *buf) override;
1330b57cec5SDimitry Andric   void addSymbols(ThunkSection &isec) override;
1340b57cec5SDimitry Andric };
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric class ARMV7PILongThunk final : public ARMThunk {
1370b57cec5SDimitry Andric public:
138fe6060f1SDimitry Andric   ARMV7PILongThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric   uint32_t sizeLong() override { return 16; }
1410b57cec5SDimitry Andric   void writeLong(uint8_t *buf) override;
1420b57cec5SDimitry Andric   void addSymbols(ThunkSection &isec) override;
1430b57cec5SDimitry Andric };
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric class ThumbV7ABSLongThunk final : public ThumbThunk {
1460b57cec5SDimitry Andric public:
147fe6060f1SDimitry Andric   ThumbV7ABSLongThunk(Symbol &dest, int64_t addend)
148fe6060f1SDimitry Andric       : ThumbThunk(dest, addend) {}
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric   uint32_t sizeLong() override { return 10; }
1510b57cec5SDimitry Andric   void writeLong(uint8_t *buf) override;
1520b57cec5SDimitry Andric   void addSymbols(ThunkSection &isec) override;
1530b57cec5SDimitry Andric };
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric class ThumbV7PILongThunk final : public ThumbThunk {
1560b57cec5SDimitry Andric public:
157fe6060f1SDimitry Andric   ThumbV7PILongThunk(Symbol &dest, int64_t addend) : ThumbThunk(dest, addend) {}
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric   uint32_t sizeLong() override { return 12; }
1600b57cec5SDimitry Andric   void writeLong(uint8_t *buf) override;
1610b57cec5SDimitry Andric   void addSymbols(ThunkSection &isec) override;
1620b57cec5SDimitry Andric };
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric // Implementations of Thunks for older Arm architectures that do not support
1650b57cec5SDimitry Andric // the movt/movw instructions. These thunks require at least Architecture v5
1660b57cec5SDimitry Andric // as used on processors such as the Arm926ej-s. There are no Thumb entry
1670b57cec5SDimitry Andric // points as there is no Thumb branch instruction on these architecture that
1680b57cec5SDimitry Andric // can result in a thunk
1690b57cec5SDimitry Andric class ARMV5ABSLongThunk final : public ARMThunk {
1700b57cec5SDimitry Andric public:
171fe6060f1SDimitry Andric   ARMV5ABSLongThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   uint32_t sizeLong() override { return 8; }
1740b57cec5SDimitry Andric   void writeLong(uint8_t *buf) override;
1750b57cec5SDimitry Andric   void addSymbols(ThunkSection &isec) override;
1760b57cec5SDimitry Andric   bool isCompatibleWith(const InputSection &isec,
1770b57cec5SDimitry Andric                         const Relocation &rel) const override;
1780b57cec5SDimitry Andric };
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric class ARMV5PILongThunk final : public ARMThunk {
1810b57cec5SDimitry Andric public:
182fe6060f1SDimitry Andric   ARMV5PILongThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric   uint32_t sizeLong() override { return 16; }
1850b57cec5SDimitry Andric   void writeLong(uint8_t *buf) override;
1860b57cec5SDimitry Andric   void addSymbols(ThunkSection &isec) override;
1870b57cec5SDimitry Andric   bool isCompatibleWith(const InputSection &isec,
1880b57cec5SDimitry Andric                         const Relocation &rel) const override;
1890b57cec5SDimitry Andric };
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric // Implementations of Thunks for Arm v6-M. Only Thumb instructions are permitted
1920b57cec5SDimitry Andric class ThumbV6MABSLongThunk final : public ThumbThunk {
1930b57cec5SDimitry Andric public:
194fe6060f1SDimitry Andric   ThumbV6MABSLongThunk(Symbol &dest, int64_t addend)
195fe6060f1SDimitry Andric       : ThumbThunk(dest, addend) {}
1960b57cec5SDimitry Andric 
1970b57cec5SDimitry Andric   uint32_t sizeLong() override { return 12; }
1980b57cec5SDimitry Andric   void writeLong(uint8_t *buf) override;
1990b57cec5SDimitry Andric   void addSymbols(ThunkSection &isec) override;
2000b57cec5SDimitry Andric };
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric class ThumbV6MPILongThunk final : public ThumbThunk {
2030b57cec5SDimitry Andric public:
204fe6060f1SDimitry Andric   ThumbV6MPILongThunk(Symbol &dest, int64_t addend)
205fe6060f1SDimitry Andric       : ThumbThunk(dest, addend) {}
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric   uint32_t sizeLong() override { return 16; }
2080b57cec5SDimitry Andric   void writeLong(uint8_t *buf) override;
2090b57cec5SDimitry Andric   void addSymbols(ThunkSection &isec) override;
2100b57cec5SDimitry Andric };
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric // MIPS LA25 thunk
2130b57cec5SDimitry Andric class MipsThunk final : public Thunk {
2140b57cec5SDimitry Andric public:
215480093f4SDimitry Andric   MipsThunk(Symbol &dest) : Thunk(dest, 0) {}
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric   uint32_t size() override { return 16; }
2180b57cec5SDimitry Andric   void writeTo(uint8_t *buf) override;
2190b57cec5SDimitry Andric   void addSymbols(ThunkSection &isec) override;
2200b57cec5SDimitry Andric   InputSection *getTargetInputSection() const override;
2210b57cec5SDimitry Andric };
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric // microMIPS R2-R5 LA25 thunk
2240b57cec5SDimitry Andric class MicroMipsThunk final : public Thunk {
2250b57cec5SDimitry Andric public:
226480093f4SDimitry Andric   MicroMipsThunk(Symbol &dest) : Thunk(dest, 0) {}
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric   uint32_t size() override { return 14; }
2290b57cec5SDimitry Andric   void writeTo(uint8_t *buf) override;
2300b57cec5SDimitry Andric   void addSymbols(ThunkSection &isec) override;
2310b57cec5SDimitry Andric   InputSection *getTargetInputSection() const override;
2320b57cec5SDimitry Andric };
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric // microMIPS R6 LA25 thunk
2350b57cec5SDimitry Andric class MicroMipsR6Thunk final : public Thunk {
2360b57cec5SDimitry Andric public:
237480093f4SDimitry Andric   MicroMipsR6Thunk(Symbol &dest) : Thunk(dest, 0) {}
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric   uint32_t size() override { return 12; }
2400b57cec5SDimitry Andric   void writeTo(uint8_t *buf) override;
2410b57cec5SDimitry Andric   void addSymbols(ThunkSection &isec) override;
2420b57cec5SDimitry Andric   InputSection *getTargetInputSection() const override;
2430b57cec5SDimitry Andric };
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric class PPC32PltCallStub final : public Thunk {
2460b57cec5SDimitry Andric public:
247480093f4SDimitry Andric   // For R_PPC_PLTREL24, Thunk::addend records the addend which will be used to
248480093f4SDimitry Andric   // decide the offsets in the call stub.
249480093f4SDimitry Andric   PPC32PltCallStub(const InputSection &isec, const Relocation &rel,
250480093f4SDimitry Andric                    Symbol &dest)
25113138422SDimitry Andric       : Thunk(dest, rel.addend), file(isec.file) {}
2520b57cec5SDimitry Andric   uint32_t size() override { return 16; }
2530b57cec5SDimitry Andric   void writeTo(uint8_t *buf) override;
2540b57cec5SDimitry Andric   void addSymbols(ThunkSection &isec) override;
2550b57cec5SDimitry Andric   bool isCompatibleWith(const InputSection &isec, const Relocation &rel) const override;
2560b57cec5SDimitry Andric 
2570b57cec5SDimitry Andric private:
2580b57cec5SDimitry Andric   // Records the call site of the call stub.
2590b57cec5SDimitry Andric   const InputFile *file;
2600b57cec5SDimitry Andric };
2610b57cec5SDimitry Andric 
26213138422SDimitry Andric class PPC32LongThunk final : public Thunk {
26313138422SDimitry Andric public:
26413138422SDimitry Andric   PPC32LongThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
26513138422SDimitry Andric   uint32_t size() override { return config->isPic ? 32 : 16; }
26613138422SDimitry Andric   void writeTo(uint8_t *buf) override;
26713138422SDimitry Andric   void addSymbols(ThunkSection &isec) override;
26813138422SDimitry Andric };
26913138422SDimitry Andric 
2700b57cec5SDimitry Andric // PPC64 Plt call stubs.
2710b57cec5SDimitry Andric // Any call site that needs to call through a plt entry needs a call stub in
2720b57cec5SDimitry Andric // the .text section. The call stub is responsible for:
2730b57cec5SDimitry Andric // 1) Saving the toc-pointer to the stack.
2740b57cec5SDimitry Andric // 2) Loading the target functions address from the procedure linkage table into
2750b57cec5SDimitry Andric //    r12 for use by the target functions global entry point, and into the count
2760b57cec5SDimitry Andric //    register.
277480093f4SDimitry Andric // 3) Transferring control to the target function through an indirect branch.
2780b57cec5SDimitry Andric class PPC64PltCallStub final : public Thunk {
2790b57cec5SDimitry Andric public:
280480093f4SDimitry Andric   PPC64PltCallStub(Symbol &dest) : Thunk(dest, 0) {}
2810b57cec5SDimitry Andric   uint32_t size() override { return 20; }
2820b57cec5SDimitry Andric   void writeTo(uint8_t *buf) override;
2830b57cec5SDimitry Andric   void addSymbols(ThunkSection &isec) override;
284e8d8bef9SDimitry Andric   bool isCompatibleWith(const InputSection &isec,
285e8d8bef9SDimitry Andric                         const Relocation &rel) const override;
2860b57cec5SDimitry Andric };
2870b57cec5SDimitry Andric 
2885ffd83dbSDimitry Andric // PPC64 R2 Save Stub
2895ffd83dbSDimitry Andric // When the caller requires a valid R2 TOC pointer but the callee does not
2905ffd83dbSDimitry Andric // require a TOC pointer and the callee cannot guarantee that it doesn't
2915ffd83dbSDimitry Andric // clobber R2 then we need to save R2. This stub:
2925ffd83dbSDimitry Andric // 1) Saves the TOC pointer to the stack.
2935ffd83dbSDimitry Andric // 2) Tail calls the callee.
2945ffd83dbSDimitry Andric class PPC64R2SaveStub final : public Thunk {
2955ffd83dbSDimitry Andric public:
296e8d8bef9SDimitry Andric   PPC64R2SaveStub(Symbol &dest, int64_t addend) : Thunk(dest, addend) {
297e8d8bef9SDimitry Andric     alignment = 16;
298e8d8bef9SDimitry Andric   }
299e8d8bef9SDimitry Andric 
300e8d8bef9SDimitry Andric   // To prevent oscillations in layout when moving from short to long thunks
301e8d8bef9SDimitry Andric   // we make sure that once a thunk has been set to long it cannot go back.
302e8d8bef9SDimitry Andric   bool getMayUseShortThunk() {
303e8d8bef9SDimitry Andric     if (!mayUseShortThunk)
304e8d8bef9SDimitry Andric       return false;
305e8d8bef9SDimitry Andric     if (!isInt<26>(computeOffset())) {
306e8d8bef9SDimitry Andric       mayUseShortThunk = false;
307e8d8bef9SDimitry Andric       return false;
308e8d8bef9SDimitry Andric     }
309e8d8bef9SDimitry Andric     return true;
310e8d8bef9SDimitry Andric   }
311fe6060f1SDimitry Andric   uint32_t size() override { return getMayUseShortThunk() ? 8 : 32; }
312e8d8bef9SDimitry Andric   void writeTo(uint8_t *buf) override;
313e8d8bef9SDimitry Andric   void addSymbols(ThunkSection &isec) override;
314fe6060f1SDimitry Andric   bool isCompatibleWith(const InputSection &isec,
315fe6060f1SDimitry Andric                         const Relocation &rel) const override;
316e8d8bef9SDimitry Andric 
317e8d8bef9SDimitry Andric private:
318e8d8bef9SDimitry Andric   // Transitioning from long to short can create layout oscillations in
319e8d8bef9SDimitry Andric   // certain corner cases which would prevent the layout from converging.
320e8d8bef9SDimitry Andric   // This is similar to the handling for ARMThunk.
321e8d8bef9SDimitry Andric   bool mayUseShortThunk = true;
322e8d8bef9SDimitry Andric   int64_t computeOffset() const {
323e8d8bef9SDimitry Andric     return destination.getVA() - (getThunkTargetSym()->getVA() + 4);
324e8d8bef9SDimitry Andric   }
325e8d8bef9SDimitry Andric };
326e8d8bef9SDimitry Andric 
327e8d8bef9SDimitry Andric // PPC64 R12 Setup Stub
328e8d8bef9SDimitry Andric // When a caller that does not maintain a toc-pointer performs a local call to
329e8d8bef9SDimitry Andric // a callee which requires a toc-pointer then we need this stub to place the
330e8d8bef9SDimitry Andric // callee's global entry point into r12 without a save of R2.
331e8d8bef9SDimitry Andric class PPC64R12SetupStub final : public Thunk {
332e8d8bef9SDimitry Andric public:
333e8d8bef9SDimitry Andric   PPC64R12SetupStub(Symbol &dest) : Thunk(dest, 0) { alignment = 16; }
334fe6060f1SDimitry Andric   uint32_t size() override { return 32; }
3355ffd83dbSDimitry Andric   void writeTo(uint8_t *buf) override;
3365ffd83dbSDimitry Andric   void addSymbols(ThunkSection &isec) override;
337fe6060f1SDimitry Andric   bool isCompatibleWith(const InputSection &isec,
338fe6060f1SDimitry Andric                         const Relocation &rel) const override;
3395ffd83dbSDimitry Andric };
3405ffd83dbSDimitry Andric 
341e8d8bef9SDimitry Andric // PPC64 PC-relative PLT Stub
342e8d8bef9SDimitry Andric // When a caller that does not maintain a toc-pointer performs an extern call
343e8d8bef9SDimitry Andric // then this stub is needed for:
344e8d8bef9SDimitry Andric // 1) Loading the target functions address from the procedure linkage table into
345e8d8bef9SDimitry Andric //    r12 for use by the target functions global entry point, and into the count
346e8d8bef9SDimitry Andric //    register with pc-relative instructions.
347e8d8bef9SDimitry Andric // 2) Transferring control to the target function through an indirect branch.
348e8d8bef9SDimitry Andric class PPC64PCRelPLTStub final : public Thunk {
349e8d8bef9SDimitry Andric public:
350e8d8bef9SDimitry Andric   PPC64PCRelPLTStub(Symbol &dest) : Thunk(dest, 0) { alignment = 16; }
351fe6060f1SDimitry Andric   uint32_t size() override { return 32; }
352e8d8bef9SDimitry Andric   void writeTo(uint8_t *buf) override;
353e8d8bef9SDimitry Andric   void addSymbols(ThunkSection &isec) override;
354e8d8bef9SDimitry Andric   bool isCompatibleWith(const InputSection &isec,
355e8d8bef9SDimitry Andric                         const Relocation &rel) const override;
356e8d8bef9SDimitry Andric };
357e8d8bef9SDimitry Andric 
3580b57cec5SDimitry Andric // A bl instruction uses a signed 24 bit offset, with an implicit 4 byte
3590b57cec5SDimitry Andric // alignment. This gives a possible 26 bits of 'reach'. If the call offset is
360e8d8bef9SDimitry Andric // larger than that we need to emit a long-branch thunk. The target address
3610b57cec5SDimitry Andric // of the callee is stored in a table to be accessed TOC-relative. Since the
3620b57cec5SDimitry Andric // call must be local (a non-local call will have a PltCallStub instead) the
3630b57cec5SDimitry Andric // table stores the address of the callee's local entry point. For
3640b57cec5SDimitry Andric // position-independent code a corresponding relative dynamic relocation is
3650b57cec5SDimitry Andric // used.
3660b57cec5SDimitry Andric class PPC64LongBranchThunk : public Thunk {
3670b57cec5SDimitry Andric public:
368fe6060f1SDimitry Andric   uint32_t size() override { return 32; }
3690b57cec5SDimitry Andric   void writeTo(uint8_t *buf) override;
3700b57cec5SDimitry Andric   void addSymbols(ThunkSection &isec) override;
371e8d8bef9SDimitry Andric   bool isCompatibleWith(const InputSection &isec,
372e8d8bef9SDimitry Andric                         const Relocation &rel) const override;
3730b57cec5SDimitry Andric 
3740b57cec5SDimitry Andric protected:
375480093f4SDimitry Andric   PPC64LongBranchThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
3760b57cec5SDimitry Andric };
3770b57cec5SDimitry Andric 
3780b57cec5SDimitry Andric class PPC64PILongBranchThunk final : public PPC64LongBranchThunk {
3790b57cec5SDimitry Andric public:
380480093f4SDimitry Andric   PPC64PILongBranchThunk(Symbol &dest, int64_t addend)
381480093f4SDimitry Andric       : PPC64LongBranchThunk(dest, addend) {
3820b57cec5SDimitry Andric     assert(!dest.isPreemptible);
383480093f4SDimitry Andric     if (Optional<uint32_t> index =
384480093f4SDimitry Andric             in.ppc64LongBranchTarget->addEntry(&dest, addend)) {
385fe6060f1SDimitry Andric       mainPart->relaDyn->addRelativeReloc(
3860eae32dcSDimitry Andric           target->relativeRel, *in.ppc64LongBranchTarget, *index * UINT64_C(8),
387fe6060f1SDimitry Andric           dest, addend + getPPC64GlobalEntryToLocalEntryOffset(dest.stOther),
388fe6060f1SDimitry Andric           target->symbolicRel, R_ABS);
389480093f4SDimitry Andric     }
3900b57cec5SDimitry Andric   }
3910b57cec5SDimitry Andric };
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric class PPC64PDLongBranchThunk final : public PPC64LongBranchThunk {
3940b57cec5SDimitry Andric public:
395480093f4SDimitry Andric   PPC64PDLongBranchThunk(Symbol &dest, int64_t addend)
396480093f4SDimitry Andric       : PPC64LongBranchThunk(dest, addend) {
397480093f4SDimitry Andric     in.ppc64LongBranchTarget->addEntry(&dest, addend);
3980b57cec5SDimitry Andric   }
3990b57cec5SDimitry Andric };
4000b57cec5SDimitry Andric 
4010b57cec5SDimitry Andric } // end anonymous namespace
4020b57cec5SDimitry Andric 
4030b57cec5SDimitry Andric Defined *Thunk::addSymbol(StringRef name, uint8_t type, uint64_t value,
4040b57cec5SDimitry Andric                           InputSectionBase &section) {
4050b57cec5SDimitry Andric   Defined *d = addSyntheticLocal(name, type, value, /*size=*/0, section);
4060b57cec5SDimitry Andric   syms.push_back(d);
4070b57cec5SDimitry Andric   return d;
4080b57cec5SDimitry Andric }
4090b57cec5SDimitry Andric 
4100b57cec5SDimitry Andric void Thunk::setOffset(uint64_t newOffset) {
4110b57cec5SDimitry Andric   for (Defined *d : syms)
4120b57cec5SDimitry Andric     d->value = d->value - offset + newOffset;
4130b57cec5SDimitry Andric   offset = newOffset;
4140b57cec5SDimitry Andric }
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric // AArch64 long range Thunks
4170b57cec5SDimitry Andric 
418480093f4SDimitry Andric static uint64_t getAArch64ThunkDestVA(const Symbol &s, int64_t a) {
419480093f4SDimitry Andric   uint64_t v = s.isInPlt() ? s.getPltVA() : s.getVA(a);
4200b57cec5SDimitry Andric   return v;
4210b57cec5SDimitry Andric }
4220b57cec5SDimitry Andric 
4230b57cec5SDimitry Andric void AArch64ABSLongThunk::writeTo(uint8_t *buf) {
4240b57cec5SDimitry Andric   const uint8_t data[] = {
4250b57cec5SDimitry Andric     0x50, 0x00, 0x00, 0x58, //     ldr x16, L0
4260b57cec5SDimitry Andric     0x00, 0x02, 0x1f, 0xd6, //     br  x16
4270b57cec5SDimitry Andric     0x00, 0x00, 0x00, 0x00, // L0: .xword S
4280b57cec5SDimitry Andric     0x00, 0x00, 0x00, 0x00,
4290b57cec5SDimitry Andric   };
430480093f4SDimitry Andric   uint64_t s = getAArch64ThunkDestVA(destination, addend);
4310b57cec5SDimitry Andric   memcpy(buf, data, sizeof(data));
4325ffd83dbSDimitry Andric   target->relocateNoSym(buf + 8, R_AARCH64_ABS64, s);
4330b57cec5SDimitry Andric }
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric void AArch64ABSLongThunk::addSymbols(ThunkSection &isec) {
43604eeddc0SDimitry Andric   addSymbol(saver().save("__AArch64AbsLongThunk_" + destination.getName()),
4370b57cec5SDimitry Andric             STT_FUNC, 0, isec);
4380b57cec5SDimitry Andric   addSymbol("$x", STT_NOTYPE, 0, isec);
4390b57cec5SDimitry Andric   addSymbol("$d", STT_NOTYPE, 8, isec);
4400b57cec5SDimitry Andric }
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric // This Thunk has a maximum range of 4Gb, this is sufficient for all programs
4430b57cec5SDimitry Andric // using the small code model, including pc-relative ones. At time of writing
4440b57cec5SDimitry Andric // clang and gcc do not support the large code model for position independent
4450b57cec5SDimitry Andric // code so it is safe to use this for position independent thunks without
4460b57cec5SDimitry Andric // worrying about the destination being more than 4Gb away.
4470b57cec5SDimitry Andric void AArch64ADRPThunk::writeTo(uint8_t *buf) {
4480b57cec5SDimitry Andric   const uint8_t data[] = {
4490b57cec5SDimitry Andric       0x10, 0x00, 0x00, 0x90, // adrp x16, Dest R_AARCH64_ADR_PREL_PG_HI21(Dest)
4500b57cec5SDimitry Andric       0x10, 0x02, 0x00, 0x91, // add  x16, x16, R_AARCH64_ADD_ABS_LO12_NC(Dest)
4510b57cec5SDimitry Andric       0x00, 0x02, 0x1f, 0xd6, // br   x16
4520b57cec5SDimitry Andric   };
453480093f4SDimitry Andric   uint64_t s = getAArch64ThunkDestVA(destination, addend);
4540b57cec5SDimitry Andric   uint64_t p = getThunkTargetSym()->getVA();
4550b57cec5SDimitry Andric   memcpy(buf, data, sizeof(data));
4565ffd83dbSDimitry Andric   target->relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21,
4570b57cec5SDimitry Andric                         getAArch64Page(s) - getAArch64Page(p));
4585ffd83dbSDimitry Andric   target->relocateNoSym(buf + 4, R_AARCH64_ADD_ABS_LO12_NC, s);
4590b57cec5SDimitry Andric }
4600b57cec5SDimitry Andric 
4610b57cec5SDimitry Andric void AArch64ADRPThunk::addSymbols(ThunkSection &isec) {
46204eeddc0SDimitry Andric   addSymbol(saver().save("__AArch64ADRPThunk_" + destination.getName()),
46304eeddc0SDimitry Andric             STT_FUNC, 0, isec);
4640b57cec5SDimitry Andric   addSymbol("$x", STT_NOTYPE, 0, isec);
4650b57cec5SDimitry Andric }
4660b57cec5SDimitry Andric 
4670b57cec5SDimitry Andric // ARM Target Thunks
4680b57cec5SDimitry Andric static uint64_t getARMThunkDestVA(const Symbol &s) {
4690b57cec5SDimitry Andric   uint64_t v = s.isInPlt() ? s.getPltVA() : s.getVA();
4700b57cec5SDimitry Andric   return SignExtend64<32>(v);
4710b57cec5SDimitry Andric }
4720b57cec5SDimitry Andric 
4730b57cec5SDimitry Andric // This function returns true if the target is not Thumb and is within 2^26, and
4740b57cec5SDimitry Andric // it has not previously returned false (see comment for mayUseShortThunk).
4750b57cec5SDimitry Andric bool ARMThunk::getMayUseShortThunk() {
4760b57cec5SDimitry Andric   if (!mayUseShortThunk)
4770b57cec5SDimitry Andric     return false;
4780b57cec5SDimitry Andric   uint64_t s = getARMThunkDestVA(destination);
4790b57cec5SDimitry Andric   if (s & 1) {
4800b57cec5SDimitry Andric     mayUseShortThunk = false;
4810b57cec5SDimitry Andric     return false;
4820b57cec5SDimitry Andric   }
4830b57cec5SDimitry Andric   uint64_t p = getThunkTargetSym()->getVA();
4840b57cec5SDimitry Andric   int64_t offset = s - p - 8;
4850b57cec5SDimitry Andric   mayUseShortThunk = llvm::isInt<26>(offset);
4860b57cec5SDimitry Andric   return mayUseShortThunk;
4870b57cec5SDimitry Andric }
4880b57cec5SDimitry Andric 
4890b57cec5SDimitry Andric void ARMThunk::writeTo(uint8_t *buf) {
4900b57cec5SDimitry Andric   if (!getMayUseShortThunk()) {
4910b57cec5SDimitry Andric     writeLong(buf);
4920b57cec5SDimitry Andric     return;
4930b57cec5SDimitry Andric   }
4940b57cec5SDimitry Andric 
4950b57cec5SDimitry Andric   uint64_t s = getARMThunkDestVA(destination);
4960b57cec5SDimitry Andric   uint64_t p = getThunkTargetSym()->getVA();
4970b57cec5SDimitry Andric   int64_t offset = s - p - 8;
4980b57cec5SDimitry Andric   const uint8_t data[] = {
4990b57cec5SDimitry Andric     0x00, 0x00, 0x00, 0xea, // b S
5000b57cec5SDimitry Andric   };
5010b57cec5SDimitry Andric   memcpy(buf, data, sizeof(data));
5025ffd83dbSDimitry Andric   target->relocateNoSym(buf, R_ARM_JUMP24, offset);
5030b57cec5SDimitry Andric }
5040b57cec5SDimitry Andric 
5050b57cec5SDimitry Andric bool ARMThunk::isCompatibleWith(const InputSection &isec,
5060b57cec5SDimitry Andric                                 const Relocation &rel) const {
5070b57cec5SDimitry Andric   // Thumb branch relocations can't use BLX
5080b57cec5SDimitry Andric   return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;
5090b57cec5SDimitry Andric }
5100b57cec5SDimitry Andric 
5110b57cec5SDimitry Andric // This function returns true if the target is Thumb and is within 2^25, and
5120b57cec5SDimitry Andric // it has not previously returned false (see comment for mayUseShortThunk).
5130b57cec5SDimitry Andric bool ThumbThunk::getMayUseShortThunk() {
5140b57cec5SDimitry Andric   if (!mayUseShortThunk)
5150b57cec5SDimitry Andric     return false;
5160b57cec5SDimitry Andric   uint64_t s = getARMThunkDestVA(destination);
5170b57cec5SDimitry Andric   if ((s & 1) == 0) {
5180b57cec5SDimitry Andric     mayUseShortThunk = false;
5190b57cec5SDimitry Andric     return false;
5200b57cec5SDimitry Andric   }
5210b57cec5SDimitry Andric   uint64_t p = getThunkTargetSym()->getVA() & ~1;
5220b57cec5SDimitry Andric   int64_t offset = s - p - 4;
5230b57cec5SDimitry Andric   mayUseShortThunk = llvm::isInt<25>(offset);
5240b57cec5SDimitry Andric   return mayUseShortThunk;
5250b57cec5SDimitry Andric }
5260b57cec5SDimitry Andric 
5270b57cec5SDimitry Andric void ThumbThunk::writeTo(uint8_t *buf) {
5280b57cec5SDimitry Andric   if (!getMayUseShortThunk()) {
5290b57cec5SDimitry Andric     writeLong(buf);
5300b57cec5SDimitry Andric     return;
5310b57cec5SDimitry Andric   }
5320b57cec5SDimitry Andric 
5330b57cec5SDimitry Andric   uint64_t s = getARMThunkDestVA(destination);
5340b57cec5SDimitry Andric   uint64_t p = getThunkTargetSym()->getVA();
5350b57cec5SDimitry Andric   int64_t offset = s - p - 4;
5360b57cec5SDimitry Andric   const uint8_t data[] = {
5370b57cec5SDimitry Andric       0x00, 0xf0, 0x00, 0xb0, // b.w S
5380b57cec5SDimitry Andric   };
5390b57cec5SDimitry Andric   memcpy(buf, data, sizeof(data));
5405ffd83dbSDimitry Andric   target->relocateNoSym(buf, R_ARM_THM_JUMP24, offset);
5410b57cec5SDimitry Andric }
5420b57cec5SDimitry Andric 
5430b57cec5SDimitry Andric bool ThumbThunk::isCompatibleWith(const InputSection &isec,
5440b57cec5SDimitry Andric                                   const Relocation &rel) const {
5450b57cec5SDimitry Andric   // ARM branch relocations can't use BLX
5460b57cec5SDimitry Andric   return rel.type != R_ARM_JUMP24 && rel.type != R_ARM_PC24 && rel.type != R_ARM_PLT32;
5470b57cec5SDimitry Andric }
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric void ARMV7ABSLongThunk::writeLong(uint8_t *buf) {
5500b57cec5SDimitry Andric   const uint8_t data[] = {
5510b57cec5SDimitry Andric       0x00, 0xc0, 0x00, 0xe3, // movw         ip,:lower16:S
5520b57cec5SDimitry Andric       0x00, 0xc0, 0x40, 0xe3, // movt         ip,:upper16:S
5530b57cec5SDimitry Andric       0x1c, 0xff, 0x2f, 0xe1, // bx   ip
5540b57cec5SDimitry Andric   };
5550b57cec5SDimitry Andric   uint64_t s = getARMThunkDestVA(destination);
5560b57cec5SDimitry Andric   memcpy(buf, data, sizeof(data));
5575ffd83dbSDimitry Andric   target->relocateNoSym(buf, R_ARM_MOVW_ABS_NC, s);
5585ffd83dbSDimitry Andric   target->relocateNoSym(buf + 4, R_ARM_MOVT_ABS, s);
5590b57cec5SDimitry Andric }
5600b57cec5SDimitry Andric 
5610b57cec5SDimitry Andric void ARMV7ABSLongThunk::addSymbols(ThunkSection &isec) {
56204eeddc0SDimitry Andric   addSymbol(saver().save("__ARMv7ABSLongThunk_" + destination.getName()),
5630b57cec5SDimitry Andric             STT_FUNC, 0, isec);
5640b57cec5SDimitry Andric   addSymbol("$a", STT_NOTYPE, 0, isec);
5650b57cec5SDimitry Andric }
5660b57cec5SDimitry Andric 
5670b57cec5SDimitry Andric void ThumbV7ABSLongThunk::writeLong(uint8_t *buf) {
5680b57cec5SDimitry Andric   const uint8_t data[] = {
5690b57cec5SDimitry Andric       0x40, 0xf2, 0x00, 0x0c, // movw         ip, :lower16:S
5700b57cec5SDimitry Andric       0xc0, 0xf2, 0x00, 0x0c, // movt         ip, :upper16:S
5710b57cec5SDimitry Andric       0x60, 0x47,             // bx   ip
5720b57cec5SDimitry Andric   };
5730b57cec5SDimitry Andric   uint64_t s = getARMThunkDestVA(destination);
5740b57cec5SDimitry Andric   memcpy(buf, data, sizeof(data));
5755ffd83dbSDimitry Andric   target->relocateNoSym(buf, R_ARM_THM_MOVW_ABS_NC, s);
5765ffd83dbSDimitry Andric   target->relocateNoSym(buf + 4, R_ARM_THM_MOVT_ABS, s);
5770b57cec5SDimitry Andric }
5780b57cec5SDimitry Andric 
5790b57cec5SDimitry Andric void ThumbV7ABSLongThunk::addSymbols(ThunkSection &isec) {
58004eeddc0SDimitry Andric   addSymbol(saver().save("__Thumbv7ABSLongThunk_" + destination.getName()),
5810b57cec5SDimitry Andric             STT_FUNC, 1, isec);
5820b57cec5SDimitry Andric   addSymbol("$t", STT_NOTYPE, 0, isec);
5830b57cec5SDimitry Andric }
5840b57cec5SDimitry Andric 
5850b57cec5SDimitry Andric void ARMV7PILongThunk::writeLong(uint8_t *buf) {
5860b57cec5SDimitry Andric   const uint8_t data[] = {
5870b57cec5SDimitry Andric       0xf0, 0xcf, 0x0f, 0xe3, // P:  movw ip,:lower16:S - (P + (L1-P) + 8)
5880b57cec5SDimitry Andric       0x00, 0xc0, 0x40, 0xe3, //     movt ip,:upper16:S - (P + (L1-P) + 8)
5890b57cec5SDimitry Andric       0x0f, 0xc0, 0x8c, 0xe0, // L1: add  ip, ip, pc
5900b57cec5SDimitry Andric       0x1c, 0xff, 0x2f, 0xe1, //     bx   ip
5910b57cec5SDimitry Andric   };
5920b57cec5SDimitry Andric   uint64_t s = getARMThunkDestVA(destination);
5930b57cec5SDimitry Andric   uint64_t p = getThunkTargetSym()->getVA();
5940b57cec5SDimitry Andric   int64_t offset = s - p - 16;
5950b57cec5SDimitry Andric   memcpy(buf, data, sizeof(data));
5965ffd83dbSDimitry Andric   target->relocateNoSym(buf, R_ARM_MOVW_PREL_NC, offset);
5975ffd83dbSDimitry Andric   target->relocateNoSym(buf + 4, R_ARM_MOVT_PREL, offset);
5980b57cec5SDimitry Andric }
5990b57cec5SDimitry Andric 
6000b57cec5SDimitry Andric void ARMV7PILongThunk::addSymbols(ThunkSection &isec) {
60104eeddc0SDimitry Andric   addSymbol(saver().save("__ARMV7PILongThunk_" + destination.getName()),
60204eeddc0SDimitry Andric             STT_FUNC, 0, isec);
6030b57cec5SDimitry Andric   addSymbol("$a", STT_NOTYPE, 0, isec);
6040b57cec5SDimitry Andric }
6050b57cec5SDimitry Andric 
6060b57cec5SDimitry Andric void ThumbV7PILongThunk::writeLong(uint8_t *buf) {
6070b57cec5SDimitry Andric   const uint8_t data[] = {
6080b57cec5SDimitry Andric       0x4f, 0xf6, 0xf4, 0x7c, // P:  movw ip,:lower16:S - (P + (L1-P) + 4)
6090b57cec5SDimitry Andric       0xc0, 0xf2, 0x00, 0x0c, //     movt ip,:upper16:S - (P + (L1-P) + 4)
6100b57cec5SDimitry Andric       0xfc, 0x44,             // L1: add  ip, pc
6110b57cec5SDimitry Andric       0x60, 0x47,             //     bx   ip
6120b57cec5SDimitry Andric   };
6130b57cec5SDimitry Andric   uint64_t s = getARMThunkDestVA(destination);
6140b57cec5SDimitry Andric   uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
6150b57cec5SDimitry Andric   int64_t offset = s - p - 12;
6160b57cec5SDimitry Andric   memcpy(buf, data, sizeof(data));
6175ffd83dbSDimitry Andric   target->relocateNoSym(buf, R_ARM_THM_MOVW_PREL_NC, offset);
6185ffd83dbSDimitry Andric   target->relocateNoSym(buf + 4, R_ARM_THM_MOVT_PREL, offset);
6190b57cec5SDimitry Andric }
6200b57cec5SDimitry Andric 
6210b57cec5SDimitry Andric void ThumbV7PILongThunk::addSymbols(ThunkSection &isec) {
62204eeddc0SDimitry Andric   addSymbol(saver().save("__ThumbV7PILongThunk_" + destination.getName()),
6230b57cec5SDimitry Andric             STT_FUNC, 1, isec);
6240b57cec5SDimitry Andric   addSymbol("$t", STT_NOTYPE, 0, isec);
6250b57cec5SDimitry Andric }
6260b57cec5SDimitry Andric 
6270b57cec5SDimitry Andric void ARMV5ABSLongThunk::writeLong(uint8_t *buf) {
6280b57cec5SDimitry Andric   const uint8_t data[] = {
6290b57cec5SDimitry Andric       0x04, 0xf0, 0x1f, 0xe5, //     ldr pc, [pc,#-4] ; L1
6300b57cec5SDimitry Andric       0x00, 0x00, 0x00, 0x00, // L1: .word S
6310b57cec5SDimitry Andric   };
6320b57cec5SDimitry Andric   memcpy(buf, data, sizeof(data));
6335ffd83dbSDimitry Andric   target->relocateNoSym(buf + 4, R_ARM_ABS32, getARMThunkDestVA(destination));
6340b57cec5SDimitry Andric }
6350b57cec5SDimitry Andric 
6360b57cec5SDimitry Andric void ARMV5ABSLongThunk::addSymbols(ThunkSection &isec) {
63704eeddc0SDimitry Andric   addSymbol(saver().save("__ARMv5ABSLongThunk_" + destination.getName()),
6380b57cec5SDimitry Andric             STT_FUNC, 0, isec);
6390b57cec5SDimitry Andric   addSymbol("$a", STT_NOTYPE, 0, isec);
6400b57cec5SDimitry Andric   addSymbol("$d", STT_NOTYPE, 4, isec);
6410b57cec5SDimitry Andric }
6420b57cec5SDimitry Andric 
6430b57cec5SDimitry Andric bool ARMV5ABSLongThunk::isCompatibleWith(const InputSection &isec,
6440b57cec5SDimitry Andric                                          const Relocation &rel) const {
6450b57cec5SDimitry Andric   // Thumb branch relocations can't use BLX
6460b57cec5SDimitry Andric   return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;
6470b57cec5SDimitry Andric }
6480b57cec5SDimitry Andric 
6490b57cec5SDimitry Andric void ARMV5PILongThunk::writeLong(uint8_t *buf) {
6500b57cec5SDimitry Andric   const uint8_t data[] = {
6510b57cec5SDimitry Andric       0x04, 0xc0, 0x9f, 0xe5, // P:  ldr ip, [pc,#4] ; L2
6520b57cec5SDimitry Andric       0x0c, 0xc0, 0x8f, 0xe0, // L1: add ip, pc, ip
6530b57cec5SDimitry Andric       0x1c, 0xff, 0x2f, 0xe1, //     bx ip
6540b57cec5SDimitry Andric       0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 8)
6550b57cec5SDimitry Andric   };
6560b57cec5SDimitry Andric   uint64_t s = getARMThunkDestVA(destination);
6570b57cec5SDimitry Andric   uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
6580b57cec5SDimitry Andric   memcpy(buf, data, sizeof(data));
6595ffd83dbSDimitry Andric   target->relocateNoSym(buf + 12, R_ARM_REL32, s - p - 12);
6600b57cec5SDimitry Andric }
6610b57cec5SDimitry Andric 
6620b57cec5SDimitry Andric void ARMV5PILongThunk::addSymbols(ThunkSection &isec) {
66304eeddc0SDimitry Andric   addSymbol(saver().save("__ARMV5PILongThunk_" + destination.getName()),
66404eeddc0SDimitry Andric             STT_FUNC, 0, isec);
6650b57cec5SDimitry Andric   addSymbol("$a", STT_NOTYPE, 0, isec);
6660b57cec5SDimitry Andric   addSymbol("$d", STT_NOTYPE, 12, isec);
6670b57cec5SDimitry Andric }
6680b57cec5SDimitry Andric 
6690b57cec5SDimitry Andric bool ARMV5PILongThunk::isCompatibleWith(const InputSection &isec,
6700b57cec5SDimitry Andric                                         const Relocation &rel) const {
6710b57cec5SDimitry Andric   // Thumb branch relocations can't use BLX
6720b57cec5SDimitry Andric   return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;
6730b57cec5SDimitry Andric }
6740b57cec5SDimitry Andric 
6750b57cec5SDimitry Andric void ThumbV6MABSLongThunk::writeLong(uint8_t *buf) {
6760b57cec5SDimitry Andric   // Most Thumb instructions cannot access the high registers r8 - r15. As the
6770b57cec5SDimitry Andric   // only register we can corrupt is r12 we must instead spill a low register
6780b57cec5SDimitry Andric   // to the stack to use as a scratch register. We push r1 even though we
6790b57cec5SDimitry Andric   // don't need to get some space to use for the return address.
6800b57cec5SDimitry Andric   const uint8_t data[] = {
6810b57cec5SDimitry Andric       0x03, 0xb4,            // push {r0, r1} ; Obtain scratch registers
6820b57cec5SDimitry Andric       0x01, 0x48,            // ldr r0, [pc, #4] ; L1
6830b57cec5SDimitry Andric       0x01, 0x90,            // str r0, [sp, #4] ; SP + 4 = S
6840b57cec5SDimitry Andric       0x01, 0xbd,            // pop {r0, pc} ; restore r0 and branch to dest
6850b57cec5SDimitry Andric       0x00, 0x00, 0x00, 0x00 // L1: .word S
6860b57cec5SDimitry Andric   };
6870b57cec5SDimitry Andric   uint64_t s = getARMThunkDestVA(destination);
6880b57cec5SDimitry Andric   memcpy(buf, data, sizeof(data));
6895ffd83dbSDimitry Andric   target->relocateNoSym(buf + 8, R_ARM_ABS32, s);
6900b57cec5SDimitry Andric }
6910b57cec5SDimitry Andric 
6920b57cec5SDimitry Andric void ThumbV6MABSLongThunk::addSymbols(ThunkSection &isec) {
69304eeddc0SDimitry Andric   addSymbol(saver().save("__Thumbv6MABSLongThunk_" + destination.getName()),
6940b57cec5SDimitry Andric             STT_FUNC, 1, isec);
6950b57cec5SDimitry Andric   addSymbol("$t", STT_NOTYPE, 0, isec);
6960b57cec5SDimitry Andric   addSymbol("$d", STT_NOTYPE, 8, isec);
6970b57cec5SDimitry Andric }
6980b57cec5SDimitry Andric 
6990b57cec5SDimitry Andric void ThumbV6MPILongThunk::writeLong(uint8_t *buf) {
7000b57cec5SDimitry Andric   // Most Thumb instructions cannot access the high registers r8 - r15. As the
7010b57cec5SDimitry Andric   // only register we can corrupt is ip (r12) we must instead spill a low
7020b57cec5SDimitry Andric   // register to the stack to use as a scratch register.
7030b57cec5SDimitry Andric   const uint8_t data[] = {
7040b57cec5SDimitry Andric       0x01, 0xb4,             // P:  push {r0}        ; Obtain scratch register
7050b57cec5SDimitry Andric       0x02, 0x48,             //     ldr r0, [pc, #8] ; L2
7060b57cec5SDimitry Andric       0x84, 0x46,             //     mov ip, r0       ; high to low register
7070b57cec5SDimitry Andric       0x01, 0xbc,             //     pop {r0}         ; restore scratch register
7080b57cec5SDimitry Andric       0xe7, 0x44,             // L1: add pc, ip       ; transfer control
7090b57cec5SDimitry Andric       0xc0, 0x46,             //     nop              ; pad to 4-byte boundary
7100b57cec5SDimitry Andric       0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 4)
7110b57cec5SDimitry Andric   };
7120b57cec5SDimitry Andric   uint64_t s = getARMThunkDestVA(destination);
7130b57cec5SDimitry Andric   uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
7140b57cec5SDimitry Andric   memcpy(buf, data, sizeof(data));
7155ffd83dbSDimitry Andric   target->relocateNoSym(buf + 12, R_ARM_REL32, s - p - 12);
7160b57cec5SDimitry Andric }
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric void ThumbV6MPILongThunk::addSymbols(ThunkSection &isec) {
71904eeddc0SDimitry Andric   addSymbol(saver().save("__Thumbv6MPILongThunk_" + destination.getName()),
7200b57cec5SDimitry Andric             STT_FUNC, 1, isec);
7210b57cec5SDimitry Andric   addSymbol("$t", STT_NOTYPE, 0, isec);
7220b57cec5SDimitry Andric   addSymbol("$d", STT_NOTYPE, 12, isec);
7230b57cec5SDimitry Andric }
7240b57cec5SDimitry Andric 
7250b57cec5SDimitry Andric // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
7260b57cec5SDimitry Andric void MipsThunk::writeTo(uint8_t *buf) {
7270b57cec5SDimitry Andric   uint64_t s = destination.getVA();
7280b57cec5SDimitry Andric   write32(buf, 0x3c190000); // lui   $25, %hi(func)
7290b57cec5SDimitry Andric   write32(buf + 4, 0x08000000 | (s >> 2)); // j     func
7300b57cec5SDimitry Andric   write32(buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
7310b57cec5SDimitry Andric   write32(buf + 12, 0x00000000); // nop
7325ffd83dbSDimitry Andric   target->relocateNoSym(buf, R_MIPS_HI16, s);
7335ffd83dbSDimitry Andric   target->relocateNoSym(buf + 8, R_MIPS_LO16, s);
7340b57cec5SDimitry Andric }
7350b57cec5SDimitry Andric 
7360b57cec5SDimitry Andric void MipsThunk::addSymbols(ThunkSection &isec) {
73704eeddc0SDimitry Andric   addSymbol(saver().save("__LA25Thunk_" + destination.getName()), STT_FUNC, 0,
7380b57cec5SDimitry Andric             isec);
7390b57cec5SDimitry Andric }
7400b57cec5SDimitry Andric 
7410b57cec5SDimitry Andric InputSection *MipsThunk::getTargetInputSection() const {
7420b57cec5SDimitry Andric   auto &dr = cast<Defined>(destination);
7430b57cec5SDimitry Andric   return dyn_cast<InputSection>(dr.section);
7440b57cec5SDimitry Andric }
7450b57cec5SDimitry Andric 
7460b57cec5SDimitry Andric // Write microMIPS R2-R5 LA25 thunk code
7470b57cec5SDimitry Andric // to call PIC function from the non-PIC one.
7480b57cec5SDimitry Andric void MicroMipsThunk::writeTo(uint8_t *buf) {
7490b57cec5SDimitry Andric   uint64_t s = destination.getVA();
7500b57cec5SDimitry Andric   write16(buf, 0x41b9);       // lui   $25, %hi(func)
7510b57cec5SDimitry Andric   write16(buf + 4, 0xd400);   // j     func
7520b57cec5SDimitry Andric   write16(buf + 8, 0x3339);   // addiu $25, $25, %lo(func)
7530b57cec5SDimitry Andric   write16(buf + 12, 0x0c00);  // nop
7545ffd83dbSDimitry Andric   target->relocateNoSym(buf, R_MICROMIPS_HI16, s);
7555ffd83dbSDimitry Andric   target->relocateNoSym(buf + 4, R_MICROMIPS_26_S1, s);
7565ffd83dbSDimitry Andric   target->relocateNoSym(buf + 8, R_MICROMIPS_LO16, s);
7570b57cec5SDimitry Andric }
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric void MicroMipsThunk::addSymbols(ThunkSection &isec) {
76004eeddc0SDimitry Andric   Defined *d =
76104eeddc0SDimitry Andric       addSymbol(saver().save("__microLA25Thunk_" + destination.getName()),
76204eeddc0SDimitry Andric                 STT_FUNC, 0, isec);
7630b57cec5SDimitry Andric   d->stOther |= STO_MIPS_MICROMIPS;
7640b57cec5SDimitry Andric }
7650b57cec5SDimitry Andric 
7660b57cec5SDimitry Andric InputSection *MicroMipsThunk::getTargetInputSection() const {
7670b57cec5SDimitry Andric   auto &dr = cast<Defined>(destination);
7680b57cec5SDimitry Andric   return dyn_cast<InputSection>(dr.section);
7690b57cec5SDimitry Andric }
7700b57cec5SDimitry Andric 
7710b57cec5SDimitry Andric // Write microMIPS R6 LA25 thunk code
7720b57cec5SDimitry Andric // to call PIC function from the non-PIC one.
7730b57cec5SDimitry Andric void MicroMipsR6Thunk::writeTo(uint8_t *buf) {
7740b57cec5SDimitry Andric   uint64_t s = destination.getVA();
7750b57cec5SDimitry Andric   uint64_t p = getThunkTargetSym()->getVA();
7760b57cec5SDimitry Andric   write16(buf, 0x1320);       // lui   $25, %hi(func)
7770b57cec5SDimitry Andric   write16(buf + 4, 0x3339);   // addiu $25, $25, %lo(func)
7780b57cec5SDimitry Andric   write16(buf + 8, 0x9400);   // bc    func
7795ffd83dbSDimitry Andric   target->relocateNoSym(buf, R_MICROMIPS_HI16, s);
7805ffd83dbSDimitry Andric   target->relocateNoSym(buf + 4, R_MICROMIPS_LO16, s);
7815ffd83dbSDimitry Andric   target->relocateNoSym(buf + 8, R_MICROMIPS_PC26_S1, s - p - 12);
7820b57cec5SDimitry Andric }
7830b57cec5SDimitry Andric 
7840b57cec5SDimitry Andric void MicroMipsR6Thunk::addSymbols(ThunkSection &isec) {
78504eeddc0SDimitry Andric   Defined *d =
78604eeddc0SDimitry Andric       addSymbol(saver().save("__microLA25Thunk_" + destination.getName()),
78704eeddc0SDimitry Andric                 STT_FUNC, 0, isec);
7880b57cec5SDimitry Andric   d->stOther |= STO_MIPS_MICROMIPS;
7890b57cec5SDimitry Andric }
7900b57cec5SDimitry Andric 
7910b57cec5SDimitry Andric InputSection *MicroMipsR6Thunk::getTargetInputSection() const {
7920b57cec5SDimitry Andric   auto &dr = cast<Defined>(destination);
7930b57cec5SDimitry Andric   return dyn_cast<InputSection>(dr.section);
7940b57cec5SDimitry Andric }
7950b57cec5SDimitry Andric 
7965ffd83dbSDimitry Andric void elf::writePPC32PltCallStub(uint8_t *buf, uint64_t gotPltVA,
797480093f4SDimitry Andric                                 const InputFile *file, int64_t addend) {
7980b57cec5SDimitry Andric   if (!config->isPic) {
799480093f4SDimitry Andric     write32(buf + 0, 0x3d600000 | (gotPltVA + 0x8000) >> 16); // lis r11,ha
800480093f4SDimitry Andric     write32(buf + 4, 0x816b0000 | (uint16_t)gotPltVA);        // lwz r11,l(r11)
8010b57cec5SDimitry Andric     write32(buf + 8, 0x7d6903a6);                             // mtctr r11
8020b57cec5SDimitry Andric     write32(buf + 12, 0x4e800420);                            // bctr
8030b57cec5SDimitry Andric     return;
8040b57cec5SDimitry Andric   }
8050b57cec5SDimitry Andric   uint32_t offset;
8060b57cec5SDimitry Andric   if (addend >= 0x8000) {
8070b57cec5SDimitry Andric     // The stub loads an address relative to r30 (.got2+Addend). Addend is
8080b57cec5SDimitry Andric     // almost always 0x8000. The address of .got2 is different in another object
8090b57cec5SDimitry Andric     // file, so a stub cannot be shared.
8100eae32dcSDimitry Andric     offset = gotPltVA -
8110eae32dcSDimitry Andric              (in.ppc32Got2->getParent()->getVA() +
8120eae32dcSDimitry Andric               (file->ppc32Got2 ? file->ppc32Got2->outSecOff : 0) + addend);
8130b57cec5SDimitry Andric   } else {
8140b57cec5SDimitry Andric     // The stub loads an address relative to _GLOBAL_OFFSET_TABLE_ (which is
8150b57cec5SDimitry Andric     // currently the address of .got).
816480093f4SDimitry Andric     offset = gotPltVA - in.got->getVA();
8170b57cec5SDimitry Andric   }
8180b57cec5SDimitry Andric   uint16_t ha = (offset + 0x8000) >> 16, l = (uint16_t)offset;
8190b57cec5SDimitry Andric   if (ha == 0) {
8200b57cec5SDimitry Andric     write32(buf + 0, 0x817e0000 | l); // lwz r11,l(r30)
8210b57cec5SDimitry Andric     write32(buf + 4, 0x7d6903a6);     // mtctr r11
8220b57cec5SDimitry Andric     write32(buf + 8, 0x4e800420);     // bctr
8230b57cec5SDimitry Andric     write32(buf + 12, 0x60000000);    // nop
8240b57cec5SDimitry Andric   } else {
8250b57cec5SDimitry Andric     write32(buf + 0, 0x3d7e0000 | ha); // addis r11,r30,ha
8260b57cec5SDimitry Andric     write32(buf + 4, 0x816b0000 | l);  // lwz r11,l(r11)
8270b57cec5SDimitry Andric     write32(buf + 8, 0x7d6903a6);      // mtctr r11
8280b57cec5SDimitry Andric     write32(buf + 12, 0x4e800420);     // bctr
8290b57cec5SDimitry Andric   }
8300b57cec5SDimitry Andric }
8310b57cec5SDimitry Andric 
832480093f4SDimitry Andric void PPC32PltCallStub::writeTo(uint8_t *buf) {
833480093f4SDimitry Andric   writePPC32PltCallStub(buf, destination.getGotPltVA(), file, addend);
834480093f4SDimitry Andric }
835480093f4SDimitry Andric 
8360b57cec5SDimitry Andric void PPC32PltCallStub::addSymbols(ThunkSection &isec) {
8370b57cec5SDimitry Andric   std::string buf;
8380b57cec5SDimitry Andric   raw_string_ostream os(buf);
8390b57cec5SDimitry Andric   os << format_hex_no_prefix(addend, 8);
8400b57cec5SDimitry Andric   if (!config->isPic)
8410b57cec5SDimitry Andric     os << ".plt_call32.";
8420b57cec5SDimitry Andric   else if (addend >= 0x8000)
8430b57cec5SDimitry Andric     os << ".got2.plt_pic32.";
8440b57cec5SDimitry Andric   else
8450b57cec5SDimitry Andric     os << ".plt_pic32.";
8460b57cec5SDimitry Andric   os << destination.getName();
84704eeddc0SDimitry Andric   addSymbol(saver().save(os.str()), STT_FUNC, 0, isec);
8480b57cec5SDimitry Andric }
8490b57cec5SDimitry Andric 
8500b57cec5SDimitry Andric bool PPC32PltCallStub::isCompatibleWith(const InputSection &isec,
8510b57cec5SDimitry Andric                                         const Relocation &rel) const {
8520b57cec5SDimitry Andric   return !config->isPic || (isec.file == file && rel.addend == addend);
8530b57cec5SDimitry Andric }
8540b57cec5SDimitry Andric 
85513138422SDimitry Andric void PPC32LongThunk::addSymbols(ThunkSection &isec) {
85604eeddc0SDimitry Andric   addSymbol(saver().save("__LongThunk_" + destination.getName()), STT_FUNC, 0,
85713138422SDimitry Andric             isec);
85813138422SDimitry Andric }
85913138422SDimitry Andric 
86013138422SDimitry Andric void PPC32LongThunk::writeTo(uint8_t *buf) {
86113138422SDimitry Andric   auto ha = [](uint32_t v) -> uint16_t { return (v + 0x8000) >> 16; };
86213138422SDimitry Andric   auto lo = [](uint32_t v) -> uint16_t { return v; };
86313138422SDimitry Andric   uint32_t d = destination.getVA(addend);
86413138422SDimitry Andric   if (config->isPic) {
86513138422SDimitry Andric     uint32_t off = d - (getThunkTargetSym()->getVA() + 8);
86613138422SDimitry Andric     write32(buf + 0, 0x7c0802a6);            // mflr r12,0
86713138422SDimitry Andric     write32(buf + 4, 0x429f0005);            // bcl r20,r31,.+4
86813138422SDimitry Andric     write32(buf + 8, 0x7d8802a6);            // mtctr r12
86913138422SDimitry Andric     write32(buf + 12, 0x3d8c0000 | ha(off)); // addis r12,r12,off@ha
87013138422SDimitry Andric     write32(buf + 16, 0x398c0000 | lo(off)); // addi r12,r12,off@l
87113138422SDimitry Andric     write32(buf + 20, 0x7c0803a6);           // mtlr r0
87213138422SDimitry Andric     buf += 24;
87313138422SDimitry Andric   } else {
87413138422SDimitry Andric     write32(buf + 0, 0x3d800000 | ha(d));    // lis r12,d@ha
87513138422SDimitry Andric     write32(buf + 4, 0x398c0000 | lo(d));    // addi r12,r12,d@l
87613138422SDimitry Andric     buf += 8;
87713138422SDimitry Andric   }
87813138422SDimitry Andric   write32(buf + 0, 0x7d8903a6);              // mtctr r12
87913138422SDimitry Andric   write32(buf + 4, 0x4e800420);              // bctr
88013138422SDimitry Andric }
88113138422SDimitry Andric 
8825ffd83dbSDimitry Andric void elf::writePPC64LoadAndBranch(uint8_t *buf, int64_t offset) {
8830b57cec5SDimitry Andric   uint16_t offHa = (offset + 0x8000) >> 16;
8840b57cec5SDimitry Andric   uint16_t offLo = offset & 0xffff;
8850b57cec5SDimitry Andric 
8860b57cec5SDimitry Andric   write32(buf + 0, 0x3d820000 | offHa); // addis r12, r2, OffHa
8870b57cec5SDimitry Andric   write32(buf + 4, 0xe98c0000 | offLo); // ld    r12, OffLo(r12)
8880b57cec5SDimitry Andric   write32(buf + 8, 0x7d8903a6);         // mtctr r12
8890b57cec5SDimitry Andric   write32(buf + 12, 0x4e800420);        // bctr
8900b57cec5SDimitry Andric }
8910b57cec5SDimitry Andric 
8920b57cec5SDimitry Andric void PPC64PltCallStub::writeTo(uint8_t *buf) {
8930b57cec5SDimitry Andric   int64_t offset = destination.getGotPltVA() - getPPC64TocBase();
8940b57cec5SDimitry Andric   // Save the TOC pointer to the save-slot reserved in the call frame.
8950b57cec5SDimitry Andric   write32(buf + 0, 0xf8410018); // std     r2,24(r1)
896480093f4SDimitry Andric   writePPC64LoadAndBranch(buf + 4, offset);
8970b57cec5SDimitry Andric }
8980b57cec5SDimitry Andric 
8990b57cec5SDimitry Andric void PPC64PltCallStub::addSymbols(ThunkSection &isec) {
90004eeddc0SDimitry Andric   Defined *s = addSymbol(saver().save("__plt_" + destination.getName()),
90104eeddc0SDimitry Andric                          STT_FUNC, 0, isec);
9020b57cec5SDimitry Andric   s->needsTocRestore = true;
903480093f4SDimitry Andric   s->file = destination.file;
9040b57cec5SDimitry Andric }
9050b57cec5SDimitry Andric 
906e8d8bef9SDimitry Andric bool PPC64PltCallStub::isCompatibleWith(const InputSection &isec,
907e8d8bef9SDimitry Andric                                         const Relocation &rel) const {
908e8d8bef9SDimitry Andric   return rel.type == R_PPC64_REL24 || rel.type == R_PPC64_REL14;
909e8d8bef9SDimitry Andric }
910e8d8bef9SDimitry Andric 
9115ffd83dbSDimitry Andric void PPC64R2SaveStub::writeTo(uint8_t *buf) {
912e8d8bef9SDimitry Andric   const int64_t offset = computeOffset();
9135ffd83dbSDimitry Andric   write32(buf + 0, 0xf8410018); // std  r2,24(r1)
914e8d8bef9SDimitry Andric   // The branch offset needs to fit in 26 bits.
915e8d8bef9SDimitry Andric   if (getMayUseShortThunk()) {
9165ffd83dbSDimitry Andric     write32(buf + 4, 0x48000000 | (offset & 0x03fffffc)); // b    <offset>
917e8d8bef9SDimitry Andric   } else if (isInt<34>(offset)) {
918fe6060f1SDimitry Andric     int nextInstOffset;
919fe6060f1SDimitry Andric     uint64_t tocOffset = destination.getVA() - getPPC64TocBase();
920fe6060f1SDimitry Andric     if (tocOffset >> 16 > 0) {
921fe6060f1SDimitry Andric       const uint64_t addi = ADDI_R12_TO_R12_NO_DISP | (tocOffset & 0xffff);
922753f127fSDimitry Andric       const uint64_t addis =
923753f127fSDimitry Andric           ADDIS_R12_TO_R2_NO_DISP | ((tocOffset >> 16) & 0xffff);
924fe6060f1SDimitry Andric       write32(buf + 4, addis); // addis r12, r2 , top of offset
925fe6060f1SDimitry Andric       write32(buf + 8, addi);  // addi  r12, r12, bottom of offset
926fe6060f1SDimitry Andric       nextInstOffset = 12;
927fe6060f1SDimitry Andric     } else {
928fe6060f1SDimitry Andric       const uint64_t addi = ADDI_R12_TO_R2_NO_DISP | (tocOffset & 0xffff);
929fe6060f1SDimitry Andric       write32(buf + 4, addi); // addi r12, r2, offset
930fe6060f1SDimitry Andric       nextInstOffset = 8;
931fe6060f1SDimitry Andric     }
932fe6060f1SDimitry Andric     write32(buf + nextInstOffset, MTCTR_R12); // mtctr r12
933fe6060f1SDimitry Andric     write32(buf + nextInstOffset + 4, BCTR);  // bctr
934e8d8bef9SDimitry Andric   } else {
935e8d8bef9SDimitry Andric     in.ppc64LongBranchTarget->addEntry(&destination, addend);
936e8d8bef9SDimitry Andric     const int64_t offsetFromTOC =
937e8d8bef9SDimitry Andric         in.ppc64LongBranchTarget->getEntryVA(&destination, addend) -
938e8d8bef9SDimitry Andric         getPPC64TocBase();
939e8d8bef9SDimitry Andric     writePPC64LoadAndBranch(buf + 4, offsetFromTOC);
940e8d8bef9SDimitry Andric   }
9415ffd83dbSDimitry Andric }
9425ffd83dbSDimitry Andric 
9435ffd83dbSDimitry Andric void PPC64R2SaveStub::addSymbols(ThunkSection &isec) {
94404eeddc0SDimitry Andric   Defined *s = addSymbol(saver().save("__toc_save_" + destination.getName()),
9455ffd83dbSDimitry Andric                          STT_FUNC, 0, isec);
9465ffd83dbSDimitry Andric   s->needsTocRestore = true;
9475ffd83dbSDimitry Andric }
9485ffd83dbSDimitry Andric 
949fe6060f1SDimitry Andric bool PPC64R2SaveStub::isCompatibleWith(const InputSection &isec,
950fe6060f1SDimitry Andric                                        const Relocation &rel) const {
951fe6060f1SDimitry Andric   return rel.type == R_PPC64_REL24 || rel.type == R_PPC64_REL14;
952fe6060f1SDimitry Andric }
953fe6060f1SDimitry Andric 
954e8d8bef9SDimitry Andric void PPC64R12SetupStub::writeTo(uint8_t *buf) {
955e8d8bef9SDimitry Andric   int64_t offset = destination.getVA() - getThunkTargetSym()->getVA();
956e8d8bef9SDimitry Andric   if (!isInt<34>(offset))
957e8d8bef9SDimitry Andric     reportRangeError(buf, offset, 34, destination, "R12 setup stub offset");
958fe6060f1SDimitry Andric 
959fe6060f1SDimitry Andric   int nextInstOffset;
9604824e7fdSDimitry Andric   if (!config->power10Stubs) {
961fe6060f1SDimitry Andric     uint32_t off = destination.getVA(addend) - getThunkTargetSym()->getVA() - 8;
962fe6060f1SDimitry Andric     write32(buf + 0, 0x7c0802a6);                      // mflr r12
963fe6060f1SDimitry Andric     write32(buf + 4, 0x429f0005);                      // bcl 20,31,.+4
964fe6060f1SDimitry Andric     write32(buf + 8, 0x7d6802a6);                      // mflr r11
965fe6060f1SDimitry Andric     write32(buf + 12, 0x7d8803a6);                     // mtlr r12
966fe6060f1SDimitry Andric     write32(buf + 16, 0x3d8b0000 | computeHiBits(off));// addis r12,r11,off@ha
967fe6060f1SDimitry Andric     write32(buf + 20, 0x398c0000 | (off & 0xffff));    // addi r12,r12,off@l
968fe6060f1SDimitry Andric     nextInstOffset = 24;
969fe6060f1SDimitry Andric   } else {
970e8d8bef9SDimitry Andric     uint64_t paddi = PADDI_R12_NO_DISP | (((offset >> 16) & 0x3ffff) << 32) |
971e8d8bef9SDimitry Andric                      (offset & 0xffff);
972e8d8bef9SDimitry Andric     writePrefixedInstruction(buf + 0, paddi); // paddi r12, 0, func@pcrel, 1
973fe6060f1SDimitry Andric     nextInstOffset = 8;
974fe6060f1SDimitry Andric   }
975fe6060f1SDimitry Andric   write32(buf + nextInstOffset, MTCTR_R12); // mtctr r12
976fe6060f1SDimitry Andric   write32(buf + nextInstOffset + 4, BCTR);  // bctr
977e8d8bef9SDimitry Andric }
978e8d8bef9SDimitry Andric 
979e8d8bef9SDimitry Andric void PPC64R12SetupStub::addSymbols(ThunkSection &isec) {
98004eeddc0SDimitry Andric   addSymbol(saver().save("__gep_setup_" + destination.getName()), STT_FUNC, 0,
981e8d8bef9SDimitry Andric             isec);
982e8d8bef9SDimitry Andric }
983e8d8bef9SDimitry Andric 
984fe6060f1SDimitry Andric bool PPC64R12SetupStub::isCompatibleWith(const InputSection &isec,
985fe6060f1SDimitry Andric                                          const Relocation &rel) const {
986fe6060f1SDimitry Andric   return rel.type == R_PPC64_REL24_NOTOC;
987fe6060f1SDimitry Andric }
988fe6060f1SDimitry Andric 
989e8d8bef9SDimitry Andric void PPC64PCRelPLTStub::writeTo(uint8_t *buf) {
990fe6060f1SDimitry Andric   int nextInstOffset = 0;
991e8d8bef9SDimitry Andric   int64_t offset = destination.getGotPltVA() - getThunkTargetSym()->getVA();
992fe6060f1SDimitry Andric 
9934824e7fdSDimitry Andric   if (config->power10Stubs) {
994e8d8bef9SDimitry Andric     if (!isInt<34>(offset))
995e8d8bef9SDimitry Andric       reportRangeError(buf, offset, 34, destination,
996e8d8bef9SDimitry Andric                        "PC-relative PLT stub offset");
997fe6060f1SDimitry Andric     const uint64_t pld = PLD_R12_NO_DISP | (((offset >> 16) & 0x3ffff) << 32) |
998fe6060f1SDimitry Andric                    (offset & 0xffff);
999e8d8bef9SDimitry Andric     writePrefixedInstruction(buf + 0, pld); // pld r12, func@plt@pcrel
1000fe6060f1SDimitry Andric     nextInstOffset = 8;
1001fe6060f1SDimitry Andric   } else {
1002fe6060f1SDimitry Andric     uint32_t off = destination.getVA(addend) - getThunkTargetSym()->getVA() - 8;
1003fe6060f1SDimitry Andric     write32(buf + 0, 0x7c0802a6);            // mflr r12
1004fe6060f1SDimitry Andric     write32(buf + 4, 0x429f0005);            // bcl 20,31,.+4
1005fe6060f1SDimitry Andric     write32(buf + 8, 0x7d6802a6);            // mflr r11
1006fe6060f1SDimitry Andric     write32(buf + 12, 0x7d8803a6);           // mtlr r12
1007fe6060f1SDimitry Andric     write32(buf + 16, 0x3d8b0000 | computeHiBits(off)); // addis r12,r11,off@ha
1008fe6060f1SDimitry Andric     write32(buf + 20, 0x398c0000 | (off & 0xffff)); // addi r12,r12,off@l
1009fe6060f1SDimitry Andric     nextInstOffset = 24;
1010fe6060f1SDimitry Andric   }
1011fe6060f1SDimitry Andric   write32(buf + nextInstOffset, MTCTR_R12); // mtctr r12
1012fe6060f1SDimitry Andric   write32(buf + nextInstOffset + 4, BCTR);  // bctr
1013e8d8bef9SDimitry Andric }
1014e8d8bef9SDimitry Andric 
1015e8d8bef9SDimitry Andric void PPC64PCRelPLTStub::addSymbols(ThunkSection &isec) {
101604eeddc0SDimitry Andric   addSymbol(saver().save("__plt_pcrel_" + destination.getName()), STT_FUNC, 0,
1017e8d8bef9SDimitry Andric             isec);
1018e8d8bef9SDimitry Andric }
1019e8d8bef9SDimitry Andric 
1020e8d8bef9SDimitry Andric bool PPC64PCRelPLTStub::isCompatibleWith(const InputSection &isec,
1021e8d8bef9SDimitry Andric                                          const Relocation &rel) const {
1022e8d8bef9SDimitry Andric   return rel.type == R_PPC64_REL24_NOTOC;
1023e8d8bef9SDimitry Andric }
1024e8d8bef9SDimitry Andric 
10250b57cec5SDimitry Andric void PPC64LongBranchThunk::writeTo(uint8_t *buf) {
1026480093f4SDimitry Andric   int64_t offset = in.ppc64LongBranchTarget->getEntryVA(&destination, addend) -
1027480093f4SDimitry Andric                    getPPC64TocBase();
1028480093f4SDimitry Andric   writePPC64LoadAndBranch(buf, offset);
10290b57cec5SDimitry Andric }
10300b57cec5SDimitry Andric 
10310b57cec5SDimitry Andric void PPC64LongBranchThunk::addSymbols(ThunkSection &isec) {
103204eeddc0SDimitry Andric   addSymbol(saver().save("__long_branch_" + destination.getName()), STT_FUNC, 0,
10330b57cec5SDimitry Andric             isec);
10340b57cec5SDimitry Andric }
10350b57cec5SDimitry Andric 
1036e8d8bef9SDimitry Andric bool PPC64LongBranchThunk::isCompatibleWith(const InputSection &isec,
1037e8d8bef9SDimitry Andric                                             const Relocation &rel) const {
1038e8d8bef9SDimitry Andric   return rel.type == R_PPC64_REL24 || rel.type == R_PPC64_REL14;
1039e8d8bef9SDimitry Andric }
1040e8d8bef9SDimitry Andric 
1041480093f4SDimitry Andric Thunk::Thunk(Symbol &d, int64_t a) : destination(d), addend(a), offset(0) {}
10420b57cec5SDimitry Andric 
10430b57cec5SDimitry Andric Thunk::~Thunk() = default;
10440b57cec5SDimitry Andric 
1045480093f4SDimitry Andric static Thunk *addThunkAArch64(RelType type, Symbol &s, int64_t a) {
10465ffd83dbSDimitry Andric   if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 &&
10475ffd83dbSDimitry Andric       type != R_AARCH64_PLT32)
10480b57cec5SDimitry Andric     fatal("unrecognized relocation type");
10490b57cec5SDimitry Andric   if (config->picThunk)
1050480093f4SDimitry Andric     return make<AArch64ADRPThunk>(s, a);
1051480093f4SDimitry Andric   return make<AArch64ABSLongThunk>(s, a);
10520b57cec5SDimitry Andric }
10530b57cec5SDimitry Andric 
10540b57cec5SDimitry Andric // Creates a thunk for Thumb-ARM interworking.
10550b57cec5SDimitry Andric // Arm Architectures v5 and v6 do not support Thumb2 technology. This means
10560b57cec5SDimitry Andric // - MOVT and MOVW instructions cannot be used
10570b57cec5SDimitry Andric // - Only Thumb relocation that can generate a Thunk is a BL, this can always
10580b57cec5SDimitry Andric //   be transformed into a BLX
1059fe6060f1SDimitry Andric static Thunk *addThunkPreArmv7(RelType reloc, Symbol &s, int64_t a) {
10600b57cec5SDimitry Andric   switch (reloc) {
10610b57cec5SDimitry Andric   case R_ARM_PC24:
10620b57cec5SDimitry Andric   case R_ARM_PLT32:
10630b57cec5SDimitry Andric   case R_ARM_JUMP24:
10640b57cec5SDimitry Andric   case R_ARM_CALL:
10650b57cec5SDimitry Andric   case R_ARM_THM_CALL:
10660b57cec5SDimitry Andric     if (config->picThunk)
1067fe6060f1SDimitry Andric       return make<ARMV5PILongThunk>(s, a);
1068fe6060f1SDimitry Andric     return make<ARMV5ABSLongThunk>(s, a);
10690b57cec5SDimitry Andric   }
10700b57cec5SDimitry Andric   fatal("relocation " + toString(reloc) + " to " + toString(s) +
10710b57cec5SDimitry Andric         " not supported for Armv5 or Armv6 targets");
10720b57cec5SDimitry Andric }
10730b57cec5SDimitry Andric 
10740b57cec5SDimitry Andric // Create a thunk for Thumb long branch on V6-M.
10750b57cec5SDimitry Andric // Arm Architecture v6-M only supports Thumb instructions. This means
10760b57cec5SDimitry Andric // - MOVT and MOVW instructions cannot be used.
10770b57cec5SDimitry Andric // - Only a limited number of instructions can access registers r8 and above
10780b57cec5SDimitry Andric // - No interworking support is needed (all Thumb).
1079fe6060f1SDimitry Andric static Thunk *addThunkV6M(RelType reloc, Symbol &s, int64_t a) {
10800b57cec5SDimitry Andric   switch (reloc) {
10810b57cec5SDimitry Andric   case R_ARM_THM_JUMP19:
10820b57cec5SDimitry Andric   case R_ARM_THM_JUMP24:
10830b57cec5SDimitry Andric   case R_ARM_THM_CALL:
10840b57cec5SDimitry Andric     if (config->isPic)
1085fe6060f1SDimitry Andric       return make<ThumbV6MPILongThunk>(s, a);
1086fe6060f1SDimitry Andric     return make<ThumbV6MABSLongThunk>(s, a);
10870b57cec5SDimitry Andric   }
10880b57cec5SDimitry Andric   fatal("relocation " + toString(reloc) + " to " + toString(s) +
10890b57cec5SDimitry Andric         " not supported for Armv6-M targets");
10900b57cec5SDimitry Andric }
10910b57cec5SDimitry Andric 
10920b57cec5SDimitry Andric // Creates a thunk for Thumb-ARM interworking or branch range extension.
1093fe6060f1SDimitry Andric static Thunk *addThunkArm(RelType reloc, Symbol &s, int64_t a) {
10940b57cec5SDimitry Andric   // Decide which Thunk is needed based on:
10950b57cec5SDimitry Andric   // Available instruction set
10960b57cec5SDimitry Andric   // - An Arm Thunk can only be used if Arm state is available.
10970b57cec5SDimitry Andric   // - A Thumb Thunk can only be used if Thumb state is available.
10980b57cec5SDimitry Andric   // - Can only use a Thunk if it uses instructions that the Target supports.
10990b57cec5SDimitry Andric   // Relocation is branch or branch and link
11000b57cec5SDimitry Andric   // - Branch instructions cannot change state, can only select Thunk that
11010b57cec5SDimitry Andric   //   starts in the same state as the caller.
11020b57cec5SDimitry Andric   // - Branch and link relocations can change state, can select Thunks from
11030b57cec5SDimitry Andric   //   either Arm or Thumb.
11040b57cec5SDimitry Andric   // Position independent Thunks if we require position independent code.
11050b57cec5SDimitry Andric 
11060b57cec5SDimitry Andric   // Handle architectures that have restrictions on the instructions that they
11070b57cec5SDimitry Andric   // can use in Thunks. The flags below are set by reading the BuildAttributes
11080b57cec5SDimitry Andric   // of the input objects. InputFiles.cpp contains the mapping from ARM
11090b57cec5SDimitry Andric   // architecture to flag.
11100b57cec5SDimitry Andric   if (!config->armHasMovtMovw) {
11110b57cec5SDimitry Andric     if (!config->armJ1J2BranchEncoding)
1112fe6060f1SDimitry Andric       return addThunkPreArmv7(reloc, s, a);
1113fe6060f1SDimitry Andric     return addThunkV6M(reloc, s, a);
11140b57cec5SDimitry Andric   }
11150b57cec5SDimitry Andric 
11160b57cec5SDimitry Andric   switch (reloc) {
11170b57cec5SDimitry Andric   case R_ARM_PC24:
11180b57cec5SDimitry Andric   case R_ARM_PLT32:
11190b57cec5SDimitry Andric   case R_ARM_JUMP24:
11200b57cec5SDimitry Andric   case R_ARM_CALL:
11210b57cec5SDimitry Andric     if (config->picThunk)
1122fe6060f1SDimitry Andric       return make<ARMV7PILongThunk>(s, a);
1123fe6060f1SDimitry Andric     return make<ARMV7ABSLongThunk>(s, a);
11240b57cec5SDimitry Andric   case R_ARM_THM_JUMP19:
11250b57cec5SDimitry Andric   case R_ARM_THM_JUMP24:
11260b57cec5SDimitry Andric   case R_ARM_THM_CALL:
11270b57cec5SDimitry Andric     if (config->picThunk)
1128fe6060f1SDimitry Andric       return make<ThumbV7PILongThunk>(s, a);
1129fe6060f1SDimitry Andric     return make<ThumbV7ABSLongThunk>(s, a);
11300b57cec5SDimitry Andric   }
11310b57cec5SDimitry Andric   fatal("unrecognized relocation type");
11320b57cec5SDimitry Andric }
11330b57cec5SDimitry Andric 
11340b57cec5SDimitry Andric static Thunk *addThunkMips(RelType type, Symbol &s) {
11350b57cec5SDimitry Andric   if ((s.stOther & STO_MIPS_MICROMIPS) && isMipsR6())
11360b57cec5SDimitry Andric     return make<MicroMipsR6Thunk>(s);
11370b57cec5SDimitry Andric   if (s.stOther & STO_MIPS_MICROMIPS)
11380b57cec5SDimitry Andric     return make<MicroMipsThunk>(s);
11390b57cec5SDimitry Andric   return make<MipsThunk>(s);
11400b57cec5SDimitry Andric }
11410b57cec5SDimitry Andric 
1142480093f4SDimitry Andric static Thunk *addThunkPPC32(const InputSection &isec, const Relocation &rel,
1143480093f4SDimitry Andric                             Symbol &s) {
114413138422SDimitry Andric   assert((rel.type == R_PPC_LOCAL24PC || rel.type == R_PPC_REL24 ||
114513138422SDimitry Andric           rel.type == R_PPC_PLTREL24) &&
11460b57cec5SDimitry Andric          "unexpected relocation type for thunk");
114713138422SDimitry Andric   if (s.isInPlt())
11480b57cec5SDimitry Andric     return make<PPC32PltCallStub>(isec, rel, s);
114913138422SDimitry Andric   return make<PPC32LongThunk>(s, rel.addend);
11500b57cec5SDimitry Andric }
11510b57cec5SDimitry Andric 
1152480093f4SDimitry Andric static Thunk *addThunkPPC64(RelType type, Symbol &s, int64_t a) {
1153e8d8bef9SDimitry Andric   assert((type == R_PPC64_REL14 || type == R_PPC64_REL24 ||
1154e8d8bef9SDimitry Andric           type == R_PPC64_REL24_NOTOC) &&
11555ffd83dbSDimitry Andric          "unexpected relocation type for thunk");
11560b57cec5SDimitry Andric   if (s.isInPlt())
1157e8d8bef9SDimitry Andric     return type == R_PPC64_REL24_NOTOC ? (Thunk *)make<PPC64PCRelPLTStub>(s)
1158e8d8bef9SDimitry Andric                                        : (Thunk *)make<PPC64PltCallStub>(s);
11590b57cec5SDimitry Andric 
11605ffd83dbSDimitry Andric   // This check looks at the st_other bits of the callee. If the value is 1
1161e8d8bef9SDimitry Andric   // then the callee clobbers the TOC and we need an R2 save stub when RelType
1162e8d8bef9SDimitry Andric   // is R_PPC64_REL14 or R_PPC64_REL24.
1163e8d8bef9SDimitry Andric   if ((type == R_PPC64_REL14 || type == R_PPC64_REL24) && (s.stOther >> 5) == 1)
1164e8d8bef9SDimitry Andric     return make<PPC64R2SaveStub>(s, a);
1165e8d8bef9SDimitry Andric 
1166e8d8bef9SDimitry Andric   if (type == R_PPC64_REL24_NOTOC)
11674824e7fdSDimitry Andric     return make<PPC64R12SetupStub>(s);
11685ffd83dbSDimitry Andric 
11690b57cec5SDimitry Andric   if (config->picThunk)
1170480093f4SDimitry Andric     return make<PPC64PILongBranchThunk>(s, a);
11710b57cec5SDimitry Andric 
1172480093f4SDimitry Andric   return make<PPC64PDLongBranchThunk>(s, a);
11730b57cec5SDimitry Andric }
11740b57cec5SDimitry Andric 
11755ffd83dbSDimitry Andric Thunk *elf::addThunk(const InputSection &isec, Relocation &rel) {
11760b57cec5SDimitry Andric   Symbol &s = *rel.sym;
1177480093f4SDimitry Andric   int64_t a = rel.addend;
11780b57cec5SDimitry Andric 
11790b57cec5SDimitry Andric   if (config->emachine == EM_AARCH64)
1180480093f4SDimitry Andric     return addThunkAArch64(rel.type, s, a);
11810b57cec5SDimitry Andric 
11820b57cec5SDimitry Andric   if (config->emachine == EM_ARM)
1183fe6060f1SDimitry Andric     return addThunkArm(rel.type, s, a);
11840b57cec5SDimitry Andric 
11850b57cec5SDimitry Andric   if (config->emachine == EM_MIPS)
11860b57cec5SDimitry Andric     return addThunkMips(rel.type, s);
11870b57cec5SDimitry Andric 
11880b57cec5SDimitry Andric   if (config->emachine == EM_PPC)
11890b57cec5SDimitry Andric     return addThunkPPC32(isec, rel, s);
11900b57cec5SDimitry Andric 
11910b57cec5SDimitry Andric   if (config->emachine == EM_PPC64)
1192480093f4SDimitry Andric     return addThunkPPC64(rel.type, s, a);
11930b57cec5SDimitry Andric 
11940b57cec5SDimitry Andric   llvm_unreachable("add Thunk only supported for ARM, Mips and PowerPC");
11950b57cec5SDimitry Andric }
1196