Path: blob/main/contrib/llvm-project/lld/ELF/Thunks.cpp
34878 views
//===- Thunks.cpp --------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===---------------------------------------------------------------------===//7//8// This file contains Thunk subclasses.9//10// A thunk is a small piece of code written after an input section11// which is used to jump between "incompatible" functions12// such as MIPS PIC and non-PIC or ARM non-Thumb and Thumb functions.13//14// If a jump target is too far and its address doesn't fit to a15// short jump instruction, we need to create a thunk too, but we16// haven't supported it yet.17//18// i386 and x86-64 don't need thunks.19//20//===---------------------------------------------------------------------===//2122#include "Thunks.h"23#include "Config.h"24#include "InputFiles.h"25#include "InputSection.h"26#include "OutputSections.h"27#include "Symbols.h"28#include "SyntheticSections.h"29#include "Target.h"30#include "lld/Common/CommonLinkerContext.h"31#include "llvm/BinaryFormat/ELF.h"32#include "llvm/Support/Casting.h"33#include "llvm/Support/ErrorHandling.h"34#include "llvm/Support/MathExtras.h"35#include <cstdint>36#include <cstring>3738using namespace llvm;39using namespace llvm::object;40using namespace llvm::ELF;41using namespace lld;42using namespace lld::elf;4344namespace {4546// Base class for AArch64 thunks.47//48// An AArch64 thunk may be either short or long. A short thunk is simply a49// branch (B) instruction, and it may be used to call AArch64 functions when the50// distance from the thunk to the target is less than 128MB. Long thunks can51// branch to any virtual address and they are implemented in the derived52// classes. This class tries to create a short thunk if the target is in range,53// otherwise it creates a long thunk.54class AArch64Thunk : public Thunk {55public:56AArch64Thunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}57bool getMayUseShortThunk();58void writeTo(uint8_t *buf) override;5960private:61bool mayUseShortThunk = true;62virtual void writeLong(uint8_t *buf) = 0;63};6465// AArch64 long range Thunks.66class AArch64ABSLongThunk final : public AArch64Thunk {67public:68AArch64ABSLongThunk(Symbol &dest, int64_t addend)69: AArch64Thunk(dest, addend) {}70uint32_t size() override { return getMayUseShortThunk() ? 4 : 16; }71void addSymbols(ThunkSection &isec) override;7273private:74void writeLong(uint8_t *buf) override;75};7677class AArch64ADRPThunk final : public AArch64Thunk {78public:79AArch64ADRPThunk(Symbol &dest, int64_t addend) : AArch64Thunk(dest, addend) {}80uint32_t size() override { return getMayUseShortThunk() ? 4 : 12; }81void addSymbols(ThunkSection &isec) override;8283private:84void writeLong(uint8_t *buf) override;85};8687// Base class for ARM thunks.88//89// An ARM thunk may be either short or long. A short thunk is simply a branch90// (B) instruction, and it may be used to call ARM functions when the distance91// from the thunk to the target is less than 32MB. Long thunks can branch to any92// virtual address and can switch between ARM and Thumb, and they are93// implemented in the derived classes. This class tries to create a short thunk94// if the target is in range, otherwise it creates a long thunk.95class ARMThunk : public Thunk {96public:97ARMThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}9899bool getMayUseShortThunk();100uint32_t size() override { return getMayUseShortThunk() ? 4 : sizeLong(); }101void writeTo(uint8_t *buf) override;102bool isCompatibleWith(const InputSection &isec,103const Relocation &rel) const override;104105// Returns the size of a long thunk.106virtual uint32_t sizeLong() = 0;107108// Writes a long thunk to Buf.109virtual void writeLong(uint8_t *buf) = 0;110111private:112// This field tracks whether all previously considered layouts would allow113// this thunk to be short. If we have ever needed a long thunk, we always114// create a long thunk, even if the thunk may be short given the current115// distance to the target. We do this because transitioning from long to short116// can create layout oscillations in certain corner cases which would prevent117// the layout from converging.118bool mayUseShortThunk = true;119};120121// Base class for Thumb-2 thunks.122//123// This class is similar to ARMThunk, but it uses the Thumb-2 B.W instruction124// which has a range of 16MB.125class ThumbThunk : public Thunk {126public:127ThumbThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {128alignment = 2;129}130131bool getMayUseShortThunk();132uint32_t size() override { return getMayUseShortThunk() ? 4 : sizeLong(); }133void writeTo(uint8_t *buf) override;134bool isCompatibleWith(const InputSection &isec,135const Relocation &rel) const override;136137// Returns the size of a long thunk.138virtual uint32_t sizeLong() = 0;139140// Writes a long thunk to Buf.141virtual void writeLong(uint8_t *buf) = 0;142143private:144// See comment in ARMThunk above.145bool mayUseShortThunk = true;146};147148// Specific ARM Thunk implementations. The naming convention is:149// Source State, TargetState, Target Requirement, ABS or PI, Range150class ARMV7ABSLongThunk final : public ARMThunk {151public:152ARMV7ABSLongThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}153154uint32_t sizeLong() override { return 12; }155void writeLong(uint8_t *buf) override;156void addSymbols(ThunkSection &isec) override;157};158159class ARMV7PILongThunk final : public ARMThunk {160public:161ARMV7PILongThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}162163uint32_t sizeLong() override { return 16; }164void writeLong(uint8_t *buf) override;165void addSymbols(ThunkSection &isec) override;166};167168class ThumbV7ABSLongThunk final : public ThumbThunk {169public:170ThumbV7ABSLongThunk(Symbol &dest, int64_t addend)171: ThumbThunk(dest, addend) {}172173uint32_t sizeLong() override { return 10; }174void writeLong(uint8_t *buf) override;175void addSymbols(ThunkSection &isec) override;176};177178class ThumbV7PILongThunk final : public ThumbThunk {179public:180ThumbV7PILongThunk(Symbol &dest, int64_t addend) : ThumbThunk(dest, addend) {}181182uint32_t sizeLong() override { return 12; }183void writeLong(uint8_t *buf) override;184void addSymbols(ThunkSection &isec) override;185};186187// Implementations of Thunks for Arm v6-M. Only Thumb instructions are permitted188class ThumbV6MABSLongThunk final : public ThumbThunk {189public:190ThumbV6MABSLongThunk(Symbol &dest, int64_t addend)191: ThumbThunk(dest, addend) {}192193uint32_t sizeLong() override { return 12; }194void writeLong(uint8_t *buf) override;195void addSymbols(ThunkSection &isec) override;196};197198class ThumbV6MABSXOLongThunk final : public ThumbThunk {199public:200ThumbV6MABSXOLongThunk(Symbol &dest, int64_t addend)201: ThumbThunk(dest, addend) {}202203uint32_t sizeLong() override { return 20; }204void writeLong(uint8_t *buf) override;205void addSymbols(ThunkSection &isec) override;206};207208class ThumbV6MPILongThunk final : public ThumbThunk {209public:210ThumbV6MPILongThunk(Symbol &dest, int64_t addend)211: ThumbThunk(dest, addend) {}212213uint32_t sizeLong() override { return 16; }214void writeLong(uint8_t *buf) override;215void addSymbols(ThunkSection &isec) override;216};217218// Architectures v4, v5 and v6 do not support the movt/movw instructions. v5 and219// v6 support BLX to which BL instructions can be rewritten inline. There are no220// Thumb entrypoints for v5 and v6 as there is no Thumb branch instruction on221// these architecture that can result in a thunk.222223// LDR on v5 and v6 can switch processor state, so for v5 and v6,224// ARMV5LongLdrPcThunk can be used for both Arm->Arm and Arm->Thumb calls. v4225// can also use this thunk, but only for Arm->Arm calls.226class ARMV5LongLdrPcThunk final : public ARMThunk {227public:228ARMV5LongLdrPcThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}229230uint32_t sizeLong() override { return 8; }231void writeLong(uint8_t *buf) override;232void addSymbols(ThunkSection &isec) override;233};234235// Implementations of Thunks for v4. BLX is not supported, and loads236// will not invoke Arm/Thumb state changes.237class ARMV4PILongBXThunk final : public ARMThunk {238public:239ARMV4PILongBXThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}240241uint32_t sizeLong() override { return 16; }242void writeLong(uint8_t *buf) override;243void addSymbols(ThunkSection &isec) override;244};245246class ARMV4PILongThunk final : public ARMThunk {247public:248ARMV4PILongThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}249250uint32_t sizeLong() override { return 12; }251void writeLong(uint8_t *buf) override;252void addSymbols(ThunkSection &isec) override;253};254255class ThumbV4PILongBXThunk final : public ThumbThunk {256public:257ThumbV4PILongBXThunk(Symbol &dest, int64_t addend)258: ThumbThunk(dest, addend) {}259260uint32_t sizeLong() override { return 16; }261void writeLong(uint8_t *buf) override;262void addSymbols(ThunkSection &isec) override;263};264265class ThumbV4PILongThunk final : public ThumbThunk {266public:267ThumbV4PILongThunk(Symbol &dest, int64_t addend)268: ThumbThunk(dest, addend) {}269270uint32_t sizeLong() override { return 20; }271void writeLong(uint8_t *buf) override;272void addSymbols(ThunkSection &isec) override;273};274275class ARMV4ABSLongBXThunk final : public ARMThunk {276public:277ARMV4ABSLongBXThunk(Symbol &dest, int64_t addend) : ARMThunk(dest, addend) {}278279uint32_t sizeLong() override { return 12; }280void writeLong(uint8_t *buf) override;281void addSymbols(ThunkSection &isec) override;282};283284class ThumbV4ABSLongBXThunk final : public ThumbThunk {285public:286ThumbV4ABSLongBXThunk(Symbol &dest, int64_t addend)287: ThumbThunk(dest, addend) {}288289uint32_t sizeLong() override { return 12; }290void writeLong(uint8_t *buf) override;291void addSymbols(ThunkSection &isec) override;292};293294class ThumbV4ABSLongThunk final : public ThumbThunk {295public:296ThumbV4ABSLongThunk(Symbol &dest, int64_t addend)297: ThumbThunk(dest, addend) {}298299uint32_t sizeLong() override { return 16; }300void writeLong(uint8_t *buf) override;301void addSymbols(ThunkSection &isec) override;302};303304// The AVR devices need thunks for R_AVR_LO8_LDI_GS/R_AVR_HI8_LDI_GS305// when their destination is out of range [0, 0x1ffff].306class AVRThunk : public Thunk {307public:308AVRThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}309uint32_t size() override { return 4; }310void writeTo(uint8_t *buf) override;311void addSymbols(ThunkSection &isec) override;312};313314// MIPS LA25 thunk315class MipsThunk final : public Thunk {316public:317MipsThunk(Symbol &dest) : Thunk(dest, 0) {}318319uint32_t size() override { return 16; }320void writeTo(uint8_t *buf) override;321void addSymbols(ThunkSection &isec) override;322InputSection *getTargetInputSection() const override;323};324325// microMIPS R2-R5 LA25 thunk326class MicroMipsThunk final : public Thunk {327public:328MicroMipsThunk(Symbol &dest) : Thunk(dest, 0) {}329330uint32_t size() override { return 14; }331void writeTo(uint8_t *buf) override;332void addSymbols(ThunkSection &isec) override;333InputSection *getTargetInputSection() const override;334};335336// microMIPS R6 LA25 thunk337class MicroMipsR6Thunk final : public Thunk {338public:339MicroMipsR6Thunk(Symbol &dest) : Thunk(dest, 0) {}340341uint32_t size() override { return 12; }342void writeTo(uint8_t *buf) override;343void addSymbols(ThunkSection &isec) override;344InputSection *getTargetInputSection() const override;345};346347class PPC32PltCallStub final : public Thunk {348public:349// For R_PPC_PLTREL24, Thunk::addend records the addend which will be used to350// decide the offsets in the call stub.351PPC32PltCallStub(const InputSection &isec, const Relocation &rel,352Symbol &dest)353: Thunk(dest, rel.addend), file(isec.file) {}354uint32_t size() override { return 16; }355void writeTo(uint8_t *buf) override;356void addSymbols(ThunkSection &isec) override;357bool isCompatibleWith(const InputSection &isec, const Relocation &rel) const override;358359private:360// Records the call site of the call stub.361const InputFile *file;362};363364class PPC32LongThunk final : public Thunk {365public:366PPC32LongThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}367uint32_t size() override { return config->isPic ? 32 : 16; }368void writeTo(uint8_t *buf) override;369void addSymbols(ThunkSection &isec) override;370};371372// PPC64 Plt call stubs.373// Any call site that needs to call through a plt entry needs a call stub in374// the .text section. The call stub is responsible for:375// 1) Saving the toc-pointer to the stack.376// 2) Loading the target functions address from the procedure linkage table into377// r12 for use by the target functions global entry point, and into the count378// register.379// 3) Transferring control to the target function through an indirect branch.380class PPC64PltCallStub final : public Thunk {381public:382PPC64PltCallStub(Symbol &dest) : Thunk(dest, 0) {}383uint32_t size() override { return 20; }384void writeTo(uint8_t *buf) override;385void addSymbols(ThunkSection &isec) override;386bool isCompatibleWith(const InputSection &isec,387const Relocation &rel) const override;388};389390// PPC64 R2 Save Stub391// When the caller requires a valid R2 TOC pointer but the callee does not392// require a TOC pointer and the callee cannot guarantee that it doesn't393// clobber R2 then we need to save R2. This stub:394// 1) Saves the TOC pointer to the stack.395// 2) Tail calls the callee.396class PPC64R2SaveStub final : public Thunk {397public:398PPC64R2SaveStub(Symbol &dest, int64_t addend) : Thunk(dest, addend) {399alignment = 16;400}401402// To prevent oscillations in layout when moving from short to long thunks403// we make sure that once a thunk has been set to long it cannot go back.404bool getMayUseShortThunk() {405if (!mayUseShortThunk)406return false;407if (!isInt<26>(computeOffset())) {408mayUseShortThunk = false;409return false;410}411return true;412}413uint32_t size() override { return getMayUseShortThunk() ? 8 : 32; }414void writeTo(uint8_t *buf) override;415void addSymbols(ThunkSection &isec) override;416bool isCompatibleWith(const InputSection &isec,417const Relocation &rel) const override;418419private:420// Transitioning from long to short can create layout oscillations in421// certain corner cases which would prevent the layout from converging.422// This is similar to the handling for ARMThunk.423bool mayUseShortThunk = true;424int64_t computeOffset() const {425return destination.getVA() - (getThunkTargetSym()->getVA() + 4);426}427};428429// PPC64 R12 Setup Stub430// When a caller that does not maintain TOC calls a target which may possibly431// use TOC (either non-preemptible with localentry>1 or preemptible), we need to432// set r12 to satisfy the requirement of the global entry point.433class PPC64R12SetupStub final : public Thunk {434public:435PPC64R12SetupStub(Symbol &dest, bool gotPlt)436: Thunk(dest, 0), gotPlt(gotPlt) {437alignment = 16;438}439uint32_t size() override { return 32; }440void writeTo(uint8_t *buf) override;441void addSymbols(ThunkSection &isec) override;442bool isCompatibleWith(const InputSection &isec,443const Relocation &rel) const override;444445private:446bool gotPlt;447};448449// A bl instruction uses a signed 24 bit offset, with an implicit 4 byte450// alignment. This gives a possible 26 bits of 'reach'. If the call offset is451// larger than that we need to emit a long-branch thunk. The target address452// of the callee is stored in a table to be accessed TOC-relative. Since the453// call must be local (a non-local call will have a PltCallStub instead) the454// table stores the address of the callee's local entry point. For455// position-independent code a corresponding relative dynamic relocation is456// used.457class PPC64LongBranchThunk : public Thunk {458public:459uint32_t size() override { return 32; }460void writeTo(uint8_t *buf) override;461void addSymbols(ThunkSection &isec) override;462bool isCompatibleWith(const InputSection &isec,463const Relocation &rel) const override;464465protected:466PPC64LongBranchThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}467};468469class PPC64PILongBranchThunk final : public PPC64LongBranchThunk {470public:471PPC64PILongBranchThunk(Symbol &dest, int64_t addend)472: PPC64LongBranchThunk(dest, addend) {473assert(!dest.isPreemptible);474if (std::optional<uint32_t> index =475in.ppc64LongBranchTarget->addEntry(&dest, addend)) {476mainPart->relaDyn->addRelativeReloc(477target->relativeRel, *in.ppc64LongBranchTarget, *index * UINT64_C(8),478dest, addend + getPPC64GlobalEntryToLocalEntryOffset(dest.stOther),479target->symbolicRel, R_ABS);480}481}482};483484class PPC64PDLongBranchThunk final : public PPC64LongBranchThunk {485public:486PPC64PDLongBranchThunk(Symbol &dest, int64_t addend)487: PPC64LongBranchThunk(dest, addend) {488in.ppc64LongBranchTarget->addEntry(&dest, addend);489}490};491492} // end anonymous namespace493494Defined *Thunk::addSymbol(StringRef name, uint8_t type, uint64_t value,495InputSectionBase §ion) {496Defined *d = addSyntheticLocal(name, type, value, /*size=*/0, section);497syms.push_back(d);498return d;499}500501void Thunk::setOffset(uint64_t newOffset) {502for (Defined *d : syms)503d->value = d->value - offset + newOffset;504offset = newOffset;505}506507// AArch64 Thunk base class.508static uint64_t getAArch64ThunkDestVA(const Symbol &s, int64_t a) {509uint64_t v = s.isInPlt() ? s.getPltVA() : s.getVA(a);510return v;511}512513bool AArch64Thunk::getMayUseShortThunk() {514if (!mayUseShortThunk)515return false;516uint64_t s = getAArch64ThunkDestVA(destination, addend);517uint64_t p = getThunkTargetSym()->getVA();518mayUseShortThunk = llvm::isInt<28>(s - p);519return mayUseShortThunk;520}521522void AArch64Thunk::writeTo(uint8_t *buf) {523if (!getMayUseShortThunk()) {524writeLong(buf);525return;526}527uint64_t s = getAArch64ThunkDestVA(destination, addend);528uint64_t p = getThunkTargetSym()->getVA();529write32(buf, 0x14000000); // b S530target->relocateNoSym(buf, R_AARCH64_CALL26, s - p);531}532533// AArch64 long range Thunks.534void AArch64ABSLongThunk::writeLong(uint8_t *buf) {535const uint8_t data[] = {5360x50, 0x00, 0x00, 0x58, // ldr x16, L05370x00, 0x02, 0x1f, 0xd6, // br x165380x00, 0x00, 0x00, 0x00, // L0: .xword S5390x00, 0x00, 0x00, 0x00,540};541uint64_t s = getAArch64ThunkDestVA(destination, addend);542memcpy(buf, data, sizeof(data));543target->relocateNoSym(buf + 8, R_AARCH64_ABS64, s);544}545546void AArch64ABSLongThunk::addSymbols(ThunkSection &isec) {547addSymbol(saver().save("__AArch64AbsLongThunk_" + destination.getName()),548STT_FUNC, 0, isec);549addSymbol("$x", STT_NOTYPE, 0, isec);550if (!getMayUseShortThunk())551addSymbol("$d", STT_NOTYPE, 8, isec);552}553554// This Thunk has a maximum range of 4Gb, this is sufficient for all programs555// using the small code model, including pc-relative ones. At time of writing556// clang and gcc do not support the large code model for position independent557// code so it is safe to use this for position independent thunks without558// worrying about the destination being more than 4Gb away.559void AArch64ADRPThunk::writeLong(uint8_t *buf) {560const uint8_t data[] = {5610x10, 0x00, 0x00, 0x90, // adrp x16, Dest R_AARCH64_ADR_PREL_PG_HI21(Dest)5620x10, 0x02, 0x00, 0x91, // add x16, x16, R_AARCH64_ADD_ABS_LO12_NC(Dest)5630x00, 0x02, 0x1f, 0xd6, // br x16564};565uint64_t s = getAArch64ThunkDestVA(destination, addend);566uint64_t p = getThunkTargetSym()->getVA();567memcpy(buf, data, sizeof(data));568target->relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21,569getAArch64Page(s) - getAArch64Page(p));570target->relocateNoSym(buf + 4, R_AARCH64_ADD_ABS_LO12_NC, s);571}572573void AArch64ADRPThunk::addSymbols(ThunkSection &isec) {574addSymbol(saver().save("__AArch64ADRPThunk_" + destination.getName()),575STT_FUNC, 0, isec);576addSymbol("$x", STT_NOTYPE, 0, isec);577}578579// ARM Target Thunks580static uint64_t getARMThunkDestVA(const Symbol &s) {581uint64_t v = s.isInPlt() ? s.getPltVA() : s.getVA();582return SignExtend64<32>(v);583}584585// This function returns true if the target is not Thumb and is within 2^26, and586// it has not previously returned false (see comment for mayUseShortThunk).587bool ARMThunk::getMayUseShortThunk() {588if (!mayUseShortThunk)589return false;590uint64_t s = getARMThunkDestVA(destination);591if (s & 1) {592mayUseShortThunk = false;593return false;594}595uint64_t p = getThunkTargetSym()->getVA();596int64_t offset = s - p - 8;597mayUseShortThunk = llvm::isInt<26>(offset);598return mayUseShortThunk;599}600601void ARMThunk::writeTo(uint8_t *buf) {602if (!getMayUseShortThunk()) {603writeLong(buf);604return;605}606607uint64_t s = getARMThunkDestVA(destination);608uint64_t p = getThunkTargetSym()->getVA();609int64_t offset = s - p - 8;610write32(buf, 0xea000000); // b S611target->relocateNoSym(buf, R_ARM_JUMP24, offset);612}613614bool ARMThunk::isCompatibleWith(const InputSection &isec,615const Relocation &rel) const {616// v4T does not have BLX, so also deny R_ARM_THM_CALL617if (!config->armHasBlx && rel.type == R_ARM_THM_CALL)618return false;619620// Thumb branch relocations can't use BLX621return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;622}623624// This function returns true if:625// the target is Thumb626// && is within branch range627// && this function has not previously returned false628// (see comment for mayUseShortThunk)629// && the arch supports Thumb branch range extension.630bool ThumbThunk::getMayUseShortThunk() {631if (!mayUseShortThunk || !config->armJ1J2BranchEncoding)632return false;633uint64_t s = getARMThunkDestVA(destination);634if ((s & 1) == 0) {635mayUseShortThunk = false;636return false;637}638uint64_t p = getThunkTargetSym()->getVA() & ~1;639int64_t offset = s - p - 4;640mayUseShortThunk = llvm::isInt<25>(offset);641return mayUseShortThunk;642}643644void ThumbThunk::writeTo(uint8_t *buf) {645if (!getMayUseShortThunk()) {646writeLong(buf);647return;648}649650uint64_t s = getARMThunkDestVA(destination);651uint64_t p = getThunkTargetSym()->getVA();652int64_t offset = s - p - 4;653write16(buf + 0, 0xf000); // b.w S654write16(buf + 2, 0xb000);655target->relocateNoSym(buf, R_ARM_THM_JUMP24, offset);656}657658bool ThumbThunk::isCompatibleWith(const InputSection &isec,659const Relocation &rel) const {660// v4T does not have BLX, so also deny R_ARM_CALL661if (!config->armHasBlx && rel.type == R_ARM_CALL)662return false;663664// ARM branch relocations can't use BLX665return rel.type != R_ARM_JUMP24 && rel.type != R_ARM_PC24 && rel.type != R_ARM_PLT32;666}667668void ARMV7ABSLongThunk::writeLong(uint8_t *buf) {669write32(buf + 0, 0xe300c000); // movw ip,:lower16:S670write32(buf + 4, 0xe340c000); // movt ip,:upper16:S671write32(buf + 8, 0xe12fff1c); // bx ip672uint64_t s = getARMThunkDestVA(destination);673target->relocateNoSym(buf, R_ARM_MOVW_ABS_NC, s);674target->relocateNoSym(buf + 4, R_ARM_MOVT_ABS, s);675}676677void ARMV7ABSLongThunk::addSymbols(ThunkSection &isec) {678addSymbol(saver().save("__ARMv7ABSLongThunk_" + destination.getName()),679STT_FUNC, 0, isec);680addSymbol("$a", STT_NOTYPE, 0, isec);681}682683void ThumbV7ABSLongThunk::writeLong(uint8_t *buf) {684write16(buf + 0, 0xf240); // movw ip, :lower16:S685write16(buf + 2, 0x0c00);686write16(buf + 4, 0xf2c0); // movt ip, :upper16:S687write16(buf + 6, 0x0c00);688write16(buf + 8, 0x4760); // bx ip689uint64_t s = getARMThunkDestVA(destination);690target->relocateNoSym(buf, R_ARM_THM_MOVW_ABS_NC, s);691target->relocateNoSym(buf + 4, R_ARM_THM_MOVT_ABS, s);692}693694void ThumbV7ABSLongThunk::addSymbols(ThunkSection &isec) {695addSymbol(saver().save("__Thumbv7ABSLongThunk_" + destination.getName()),696STT_FUNC, 1, isec);697addSymbol("$t", STT_NOTYPE, 0, isec);698}699700void ARMV7PILongThunk::writeLong(uint8_t *buf) {701write32(buf + 0, 0xe30fcff0); // P: movw ip,:lower16:S - (P + (L1-P) + 8)702write32(buf + 4, 0xe340c000); // movt ip,:upper16:S - (P + (L1-P) + 8)703write32(buf + 8, 0xe08cc00f); // L1: add ip, ip, pc704write32(buf + 12, 0xe12fff1c); // bx ip705uint64_t s = getARMThunkDestVA(destination);706uint64_t p = getThunkTargetSym()->getVA();707int64_t offset = s - p - 16;708target->relocateNoSym(buf, R_ARM_MOVW_PREL_NC, offset);709target->relocateNoSym(buf + 4, R_ARM_MOVT_PREL, offset);710}711712void ARMV7PILongThunk::addSymbols(ThunkSection &isec) {713addSymbol(saver().save("__ARMV7PILongThunk_" + destination.getName()),714STT_FUNC, 0, isec);715addSymbol("$a", STT_NOTYPE, 0, isec);716}717718void ThumbV7PILongThunk::writeLong(uint8_t *buf) {719write16(buf + 0, 0xf64f); // P: movw ip,:lower16:S - (P + (L1-P) + 4)720write16(buf + 2, 0x7cf4);721write16(buf + 4, 0xf2c0); // movt ip,:upper16:S - (P + (L1-P) + 4)722write16(buf + 6, 0x0c00);723write16(buf + 8, 0x44fc); // L1: add ip, pc724write16(buf + 10, 0x4760); // bx ip725uint64_t s = getARMThunkDestVA(destination);726uint64_t p = getThunkTargetSym()->getVA() & ~0x1;727int64_t offset = s - p - 12;728target->relocateNoSym(buf, R_ARM_THM_MOVW_PREL_NC, offset);729target->relocateNoSym(buf + 4, R_ARM_THM_MOVT_PREL, offset);730}731732void ThumbV7PILongThunk::addSymbols(ThunkSection &isec) {733addSymbol(saver().save("__ThumbV7PILongThunk_" + destination.getName()),734STT_FUNC, 1, isec);735addSymbol("$t", STT_NOTYPE, 0, isec);736}737738void ThumbV6MABSLongThunk::writeLong(uint8_t *buf) {739// Most Thumb instructions cannot access the high registers r8 - r15. As the740// only register we can corrupt is r12 we must instead spill a low register741// to the stack to use as a scratch register. We push r1 even though we742// don't need to get some space to use for the return address.743write16(buf + 0, 0xb403); // push {r0, r1} ; Obtain scratch registers744write16(buf + 2, 0x4801); // ldr r0, [pc, #4] ; L1745write16(buf + 4, 0x9001); // str r0, [sp, #4] ; SP + 4 = S746write16(buf + 6, 0xbd01); // pop {r0, pc} ; restore r0 and branch to dest747write32(buf + 8, 0x00000000); // L1: .word S748uint64_t s = getARMThunkDestVA(destination);749target->relocateNoSym(buf + 8, R_ARM_ABS32, s);750}751752void ThumbV6MABSLongThunk::addSymbols(ThunkSection &isec) {753addSymbol(saver().save("__Thumbv6MABSLongThunk_" + destination.getName()),754STT_FUNC, 1, isec);755addSymbol("$t", STT_NOTYPE, 0, isec);756if (!getMayUseShortThunk())757addSymbol("$d", STT_NOTYPE, 8, isec);758}759760void ThumbV6MABSXOLongThunk::writeLong(uint8_t *buf) {761// Most Thumb instructions cannot access the high registers r8 - r15. As the762// only register we can corrupt is r12 we must instead spill a low register763// to the stack to use as a scratch register. We push r1 even though we764// don't need to get some space to use for the return address.765write16(buf + 0, 0xb403); // push {r0, r1} ; Obtain scratch registers766write16(buf + 2, 0x2000); // movs r0, :upper8_15:S767write16(buf + 4, 0x0200); // lsls r0, r0, #8768write16(buf + 6, 0x3000); // adds r0, :upper0_7:S769write16(buf + 8, 0x0200); // lsls r0, r0, #8770write16(buf + 10, 0x3000); // adds r0, :lower8_15:S771write16(buf + 12, 0x0200); // lsls r0, r0, #8772write16(buf + 14, 0x3000); // adds r0, :lower0_7:S773write16(buf + 16, 0x9001); // str r0, [sp, #4] ; SP + 4 = S774write16(buf + 18, 0xbd01); // pop {r0, pc} ; restore r0 and branch to dest775uint64_t s = getARMThunkDestVA(destination);776target->relocateNoSym(buf + 2, R_ARM_THM_ALU_ABS_G3, s);777target->relocateNoSym(buf + 6, R_ARM_THM_ALU_ABS_G2_NC, s);778target->relocateNoSym(buf + 10, R_ARM_THM_ALU_ABS_G1_NC, s);779target->relocateNoSym(buf + 14, R_ARM_THM_ALU_ABS_G0_NC, s);780}781782void ThumbV6MABSXOLongThunk::addSymbols(ThunkSection &isec) {783addSymbol(saver().save("__Thumbv6MABSXOLongThunk_" + destination.getName()),784STT_FUNC, 1, isec);785addSymbol("$t", STT_NOTYPE, 0, isec);786}787788void ThumbV6MPILongThunk::writeLong(uint8_t *buf) {789// Most Thumb instructions cannot access the high registers r8 - r15. As the790// only register we can corrupt is ip (r12) we must instead spill a low791// register to the stack to use as a scratch register.792write16(buf + 0, 0xb401); // P: push {r0} ; Obtain scratch register793write16(buf + 2, 0x4802); // ldr r0, [pc, #8] ; L2794write16(buf + 4, 0x4684); // mov ip, r0 ; high to low register795write16(buf + 6, 0xbc01); // pop {r0} ; restore scratch register796write16(buf + 8, 0x44e7); // L1: add pc, ip ; transfer control797write16(buf + 10, 0x46c0); // nop ; pad to 4-byte boundary798write32(buf + 12, 0x00000000); // L2: .word S - (P + (L1 - P) + 4)799uint64_t s = getARMThunkDestVA(destination);800uint64_t p = getThunkTargetSym()->getVA() & ~0x1;801target->relocateNoSym(buf + 12, R_ARM_REL32, s - p - 12);802}803804void ThumbV6MPILongThunk::addSymbols(ThunkSection &isec) {805addSymbol(saver().save("__Thumbv6MPILongThunk_" + destination.getName()),806STT_FUNC, 1, isec);807addSymbol("$t", STT_NOTYPE, 0, isec);808if (!getMayUseShortThunk())809addSymbol("$d", STT_NOTYPE, 12, isec);810}811812void ARMV5LongLdrPcThunk::writeLong(uint8_t *buf) {813write32(buf + 0, 0xe51ff004); // ldr pc, [pc,#-4] ; L1814write32(buf + 4, 0x00000000); // L1: .word S815target->relocateNoSym(buf + 4, R_ARM_ABS32, getARMThunkDestVA(destination));816}817818void ARMV5LongLdrPcThunk::addSymbols(ThunkSection &isec) {819addSymbol(saver().save("__ARMv5LongLdrPcThunk_" + destination.getName()),820STT_FUNC, 0, isec);821addSymbol("$a", STT_NOTYPE, 0, isec);822if (!getMayUseShortThunk())823addSymbol("$d", STT_NOTYPE, 4, isec);824}825826void ARMV4ABSLongBXThunk::writeLong(uint8_t *buf) {827write32(buf + 0, 0xe59fc000); // ldr r12, [pc] ; L1828write32(buf + 4, 0xe12fff1c); // bx r12829write32(buf + 8, 0x00000000); // L1: .word S830target->relocateNoSym(buf + 8, R_ARM_ABS32, getARMThunkDestVA(destination));831}832833void ARMV4ABSLongBXThunk::addSymbols(ThunkSection &isec) {834addSymbol(saver().save("__ARMv4ABSLongBXThunk_" + destination.getName()),835STT_FUNC, 0, isec);836addSymbol("$a", STT_NOTYPE, 0, isec);837if (!getMayUseShortThunk())838addSymbol("$d", STT_NOTYPE, 8, isec);839}840841void ThumbV4ABSLongBXThunk::writeLong(uint8_t *buf) {842write16(buf + 0, 0x4778); // bx pc843write16(buf + 2, 0xe7fd); // b #-6 ; Arm recommended sequence to follow bx pc844write32(buf + 4, 0xe51ff004); // ldr pc, [pc, #-4] ; L1845write32(buf + 8, 0x00000000); // L1: .word S846target->relocateNoSym(buf + 8, R_ARM_ABS32, getARMThunkDestVA(destination));847}848849void ThumbV4ABSLongBXThunk::addSymbols(ThunkSection &isec) {850addSymbol(saver().save("__Thumbv4ABSLongBXThunk_" + destination.getName()),851STT_FUNC, 1, isec);852addSymbol("$t", STT_NOTYPE, 0, isec);853addSymbol("$a", STT_NOTYPE, 4, isec);854if (!getMayUseShortThunk())855addSymbol("$d", STT_NOTYPE, 8, isec);856}857858void ThumbV4ABSLongThunk::writeLong(uint8_t *buf) {859write16(buf + 0, 0x4778); // bx pc860write16(buf + 2, 0xe7fd); // b #-6 ; Arm recommended sequence to follow bx pc861write32(buf + 4, 0xe59fc000); // ldr r12, [pc] ; L1862write32(buf + 8, 0xe12fff1c); // bx r12863write32(buf + 12, 0x00000000); // L1: .word S864target->relocateNoSym(buf + 12, R_ARM_ABS32, getARMThunkDestVA(destination));865}866867void ThumbV4ABSLongThunk::addSymbols(ThunkSection &isec) {868addSymbol(saver().save("__Thumbv4ABSLongThunk_" + destination.getName()),869STT_FUNC, 1, isec);870addSymbol("$t", STT_NOTYPE, 0, isec);871addSymbol("$a", STT_NOTYPE, 4, isec);872if (!getMayUseShortThunk())873addSymbol("$d", STT_NOTYPE, 12, isec);874}875876void ARMV4PILongBXThunk::writeLong(uint8_t *buf) {877write32(buf + 0, 0xe59fc004); // P: ldr ip, [pc,#4] ; L2878write32(buf + 4, 0xe08fc00c); // L1: add ip, pc, ip879write32(buf + 8, 0xe12fff1c); // bx ip880write32(buf + 12, 0x00000000); // L2: .word S - (P + (L1 - P) + 8)881uint64_t s = getARMThunkDestVA(destination);882uint64_t p = getThunkTargetSym()->getVA() & ~0x1;883target->relocateNoSym(buf + 12, R_ARM_REL32, s - p - 12);884}885886void ARMV4PILongBXThunk::addSymbols(ThunkSection &isec) {887addSymbol(saver().save("__ARMv4PILongBXThunk_" + destination.getName()),888STT_FUNC, 0, isec);889addSymbol("$a", STT_NOTYPE, 0, isec);890if (!getMayUseShortThunk())891addSymbol("$d", STT_NOTYPE, 12, isec);892}893894void ARMV4PILongThunk::writeLong(uint8_t *buf) {895write32(buf + 0, 0xe59fc000); // P: ldr ip, [pc] ; L2896write32(buf + 4, 0xe08ff00c); // L1: add pc, pc, r12897write32(buf + 8, 0x00000000); // L2: .word S - (P + (L1 - P) + 8)898uint64_t s = getARMThunkDestVA(destination);899uint64_t p = getThunkTargetSym()->getVA() & ~0x1;900target->relocateNoSym(buf + 8, R_ARM_REL32, s - p - 12);901}902903void ARMV4PILongThunk::addSymbols(ThunkSection &isec) {904addSymbol(saver().save("__ARMv4PILongThunk_" + destination.getName()),905STT_FUNC, 0, isec);906addSymbol("$a", STT_NOTYPE, 0, isec);907if (!getMayUseShortThunk())908addSymbol("$d", STT_NOTYPE, 8, isec);909}910911void ThumbV4PILongBXThunk::writeLong(uint8_t *buf) {912write16(buf + 0, 0x4778); // P: bx pc913write16(buf + 2, 0xe7fd); // b #-6 ; Arm recommended sequence to follow bx pc914write32(buf + 4, 0xe59fc000); // ldr r12, [pc] ; L2915write32(buf + 8, 0xe08cf00f); // L1: add pc, r12, pc916write32(buf + 12, 0x00000000); // L2: .word S - (P + (L1 - P) + 8)917uint64_t s = getARMThunkDestVA(destination);918uint64_t p = getThunkTargetSym()->getVA() & ~0x1;919target->relocateNoSym(buf + 12, R_ARM_REL32, s - p - 16);920}921922void ThumbV4PILongBXThunk::addSymbols(ThunkSection &isec) {923addSymbol(saver().save("__Thumbv4PILongBXThunk_" + destination.getName()),924STT_FUNC, 1, isec);925addSymbol("$t", STT_NOTYPE, 0, isec);926addSymbol("$a", STT_NOTYPE, 4, isec);927if (!getMayUseShortThunk())928addSymbol("$d", STT_NOTYPE, 12, isec);929}930931void ThumbV4PILongThunk::writeLong(uint8_t *buf) {932write16(buf + 0, 0x4778); // P: bx pc933write16(buf + 2, 0xe7fd); // b #-6 ; Arm recommended sequence to follow bx pc934write32(buf + 4, 0xe59fc004); // ldr ip, [pc,#4] ; L2935write32(buf + 8, 0xe08fc00c); // L1: add ip, pc, ip936write32(buf + 12, 0xe12fff1c); // bx ip937write32(buf + 16, 0x00000000); // L2: .word S - (P + (L1 - P) + 8)938uint64_t s = getARMThunkDestVA(destination);939uint64_t p = getThunkTargetSym()->getVA() & ~0x1;940target->relocateNoSym(buf + 16, R_ARM_REL32, s - p - 16);941}942943void ThumbV4PILongThunk::addSymbols(ThunkSection &isec) {944addSymbol(saver().save("__Thumbv4PILongThunk_" + destination.getName()),945STT_FUNC, 1, isec);946addSymbol("$t", STT_NOTYPE, 0, isec);947addSymbol("$a", STT_NOTYPE, 4, isec);948if (!getMayUseShortThunk())949addSymbol("$d", STT_NOTYPE, 16, isec);950}951952// Use the long jump which covers a range up to 8MiB.953void AVRThunk::writeTo(uint8_t *buf) {954write32(buf, 0x940c); // jmp func955target->relocateNoSym(buf, R_AVR_CALL, destination.getVA());956}957958void AVRThunk::addSymbols(ThunkSection &isec) {959addSymbol(saver().save("__AVRThunk_" + destination.getName()), STT_FUNC, 0,960isec);961}962963// Write MIPS LA25 thunk code to call PIC function from the non-PIC one.964void MipsThunk::writeTo(uint8_t *buf) {965uint64_t s = destination.getVA();966write32(buf, 0x3c190000); // lui $25, %hi(func)967write32(buf + 4, 0x08000000 | (s >> 2)); // j func968write32(buf + 8, 0x27390000); // addiu $25, $25, %lo(func)969write32(buf + 12, 0x00000000); // nop970target->relocateNoSym(buf, R_MIPS_HI16, s);971target->relocateNoSym(buf + 8, R_MIPS_LO16, s);972}973974void MipsThunk::addSymbols(ThunkSection &isec) {975addSymbol(saver().save("__LA25Thunk_" + destination.getName()), STT_FUNC, 0,976isec);977}978979InputSection *MipsThunk::getTargetInputSection() const {980auto &dr = cast<Defined>(destination);981return dyn_cast<InputSection>(dr.section);982}983984// Write microMIPS R2-R5 LA25 thunk code985// to call PIC function from the non-PIC one.986void MicroMipsThunk::writeTo(uint8_t *buf) {987uint64_t s = destination.getVA();988write16(buf, 0x41b9); // lui $25, %hi(func)989write16(buf + 4, 0xd400); // j func990write16(buf + 8, 0x3339); // addiu $25, $25, %lo(func)991write16(buf + 12, 0x0c00); // nop992target->relocateNoSym(buf, R_MICROMIPS_HI16, s);993target->relocateNoSym(buf + 4, R_MICROMIPS_26_S1, s);994target->relocateNoSym(buf + 8, R_MICROMIPS_LO16, s);995}996997void MicroMipsThunk::addSymbols(ThunkSection &isec) {998Defined *d =999addSymbol(saver().save("__microLA25Thunk_" + destination.getName()),1000STT_FUNC, 0, isec);1001d->stOther |= STO_MIPS_MICROMIPS;1002}10031004InputSection *MicroMipsThunk::getTargetInputSection() const {1005auto &dr = cast<Defined>(destination);1006return dyn_cast<InputSection>(dr.section);1007}10081009// Write microMIPS R6 LA25 thunk code1010// to call PIC function from the non-PIC one.1011void MicroMipsR6Thunk::writeTo(uint8_t *buf) {1012uint64_t s = destination.getVA();1013uint64_t p = getThunkTargetSym()->getVA();1014write16(buf, 0x1320); // lui $25, %hi(func)1015write16(buf + 4, 0x3339); // addiu $25, $25, %lo(func)1016write16(buf + 8, 0x9400); // bc func1017target->relocateNoSym(buf, R_MICROMIPS_HI16, s);1018target->relocateNoSym(buf + 4, R_MICROMIPS_LO16, s);1019target->relocateNoSym(buf + 8, R_MICROMIPS_PC26_S1, s - p - 12);1020}10211022void MicroMipsR6Thunk::addSymbols(ThunkSection &isec) {1023Defined *d =1024addSymbol(saver().save("__microLA25Thunk_" + destination.getName()),1025STT_FUNC, 0, isec);1026d->stOther |= STO_MIPS_MICROMIPS;1027}10281029InputSection *MicroMipsR6Thunk::getTargetInputSection() const {1030auto &dr = cast<Defined>(destination);1031return dyn_cast<InputSection>(dr.section);1032}10331034void elf::writePPC32PltCallStub(uint8_t *buf, uint64_t gotPltVA,1035const InputFile *file, int64_t addend) {1036if (!config->isPic) {1037write32(buf + 0, 0x3d600000 | (gotPltVA + 0x8000) >> 16); // lis r11,ha1038write32(buf + 4, 0x816b0000 | (uint16_t)gotPltVA); // lwz r11,l(r11)1039write32(buf + 8, 0x7d6903a6); // mtctr r111040write32(buf + 12, 0x4e800420); // bctr1041return;1042}1043uint32_t offset;1044if (addend >= 0x8000) {1045// The stub loads an address relative to r30 (.got2+Addend). Addend is1046// almost always 0x8000. The address of .got2 is different in another object1047// file, so a stub cannot be shared.1048offset = gotPltVA -1049(in.ppc32Got2->getParent()->getVA() +1050(file->ppc32Got2 ? file->ppc32Got2->outSecOff : 0) + addend);1051} else {1052// The stub loads an address relative to _GLOBAL_OFFSET_TABLE_ (which is1053// currently the address of .got).1054offset = gotPltVA - in.got->getVA();1055}1056uint16_t ha = (offset + 0x8000) >> 16, l = (uint16_t)offset;1057if (ha == 0) {1058write32(buf + 0, 0x817e0000 | l); // lwz r11,l(r30)1059write32(buf + 4, 0x7d6903a6); // mtctr r111060write32(buf + 8, 0x4e800420); // bctr1061write32(buf + 12, 0x60000000); // nop1062} else {1063write32(buf + 0, 0x3d7e0000 | ha); // addis r11,r30,ha1064write32(buf + 4, 0x816b0000 | l); // lwz r11,l(r11)1065write32(buf + 8, 0x7d6903a6); // mtctr r111066write32(buf + 12, 0x4e800420); // bctr1067}1068}10691070void PPC32PltCallStub::writeTo(uint8_t *buf) {1071writePPC32PltCallStub(buf, destination.getGotPltVA(), file, addend);1072}10731074void PPC32PltCallStub::addSymbols(ThunkSection &isec) {1075std::string buf;1076raw_string_ostream os(buf);1077os << format_hex_no_prefix(addend, 8);1078if (!config->isPic)1079os << ".plt_call32.";1080else if (addend >= 0x8000)1081os << ".got2.plt_pic32.";1082else1083os << ".plt_pic32.";1084os << destination.getName();1085addSymbol(saver().save(os.str()), STT_FUNC, 0, isec);1086}10871088bool PPC32PltCallStub::isCompatibleWith(const InputSection &isec,1089const Relocation &rel) const {1090return !config->isPic || (isec.file == file && rel.addend == addend);1091}10921093void PPC32LongThunk::addSymbols(ThunkSection &isec) {1094addSymbol(saver().save("__LongThunk_" + destination.getName()), STT_FUNC, 0,1095isec);1096}10971098void PPC32LongThunk::writeTo(uint8_t *buf) {1099auto ha = [](uint32_t v) -> uint16_t { return (v + 0x8000) >> 16; };1100auto lo = [](uint32_t v) -> uint16_t { return v; };1101uint32_t d = destination.getVA(addend);1102if (config->isPic) {1103uint32_t off = d - (getThunkTargetSym()->getVA() + 8);1104write32(buf + 0, 0x7c0802a6); // mflr r12,01105write32(buf + 4, 0x429f0005); // bcl r20,r31,.+41106write32(buf + 8, 0x7d8802a6); // mtctr r121107write32(buf + 12, 0x3d8c0000 | ha(off)); // addis r12,r12,off@ha1108write32(buf + 16, 0x398c0000 | lo(off)); // addi r12,r12,off@l1109write32(buf + 20, 0x7c0803a6); // mtlr r01110buf += 24;1111} else {1112write32(buf + 0, 0x3d800000 | ha(d)); // lis r12,d@ha1113write32(buf + 4, 0x398c0000 | lo(d)); // addi r12,r12,d@l1114buf += 8;1115}1116write32(buf + 0, 0x7d8903a6); // mtctr r121117write32(buf + 4, 0x4e800420); // bctr1118}11191120void elf::writePPC64LoadAndBranch(uint8_t *buf, int64_t offset) {1121uint16_t offHa = (offset + 0x8000) >> 16;1122uint16_t offLo = offset & 0xffff;11231124write32(buf + 0, 0x3d820000 | offHa); // addis r12, r2, OffHa1125write32(buf + 4, 0xe98c0000 | offLo); // ld r12, OffLo(r12)1126write32(buf + 8, 0x7d8903a6); // mtctr r121127write32(buf + 12, 0x4e800420); // bctr1128}11291130void PPC64PltCallStub::writeTo(uint8_t *buf) {1131int64_t offset = destination.getGotPltVA() - getPPC64TocBase();1132// Save the TOC pointer to the save-slot reserved in the call frame.1133write32(buf + 0, 0xf8410018); // std r2,24(r1)1134writePPC64LoadAndBranch(buf + 4, offset);1135}11361137void PPC64PltCallStub::addSymbols(ThunkSection &isec) {1138Defined *s = addSymbol(saver().save("__plt_" + destination.getName()),1139STT_FUNC, 0, isec);1140s->setNeedsTocRestore(true);1141s->file = destination.file;1142}11431144bool PPC64PltCallStub::isCompatibleWith(const InputSection &isec,1145const Relocation &rel) const {1146return rel.type == R_PPC64_REL24 || rel.type == R_PPC64_REL14;1147}11481149void PPC64R2SaveStub::writeTo(uint8_t *buf) {1150const int64_t offset = computeOffset();1151write32(buf + 0, 0xf8410018); // std r2,24(r1)1152// The branch offset needs to fit in 26 bits.1153if (getMayUseShortThunk()) {1154write32(buf + 4, 0x48000000 | (offset & 0x03fffffc)); // b <offset>1155} else if (isInt<34>(offset)) {1156int nextInstOffset;1157uint64_t tocOffset = destination.getVA() - getPPC64TocBase();1158if (tocOffset >> 16 > 0) {1159const uint64_t addi = ADDI_R12_TO_R12_NO_DISP | (tocOffset & 0xffff);1160const uint64_t addis =1161ADDIS_R12_TO_R2_NO_DISP | ((tocOffset >> 16) & 0xffff);1162write32(buf + 4, addis); // addis r12, r2 , top of offset1163write32(buf + 8, addi); // addi r12, r12, bottom of offset1164nextInstOffset = 12;1165} else {1166const uint64_t addi = ADDI_R12_TO_R2_NO_DISP | (tocOffset & 0xffff);1167write32(buf + 4, addi); // addi r12, r2, offset1168nextInstOffset = 8;1169}1170write32(buf + nextInstOffset, MTCTR_R12); // mtctr r121171write32(buf + nextInstOffset + 4, BCTR); // bctr1172} else {1173in.ppc64LongBranchTarget->addEntry(&destination, addend);1174const int64_t offsetFromTOC =1175in.ppc64LongBranchTarget->getEntryVA(&destination, addend) -1176getPPC64TocBase();1177writePPC64LoadAndBranch(buf + 4, offsetFromTOC);1178}1179}11801181void PPC64R2SaveStub::addSymbols(ThunkSection &isec) {1182Defined *s = addSymbol(saver().save("__toc_save_" + destination.getName()),1183STT_FUNC, 0, isec);1184s->setNeedsTocRestore(true);1185}11861187bool PPC64R2SaveStub::isCompatibleWith(const InputSection &isec,1188const Relocation &rel) const {1189return rel.type == R_PPC64_REL24 || rel.type == R_PPC64_REL14;1190}11911192void PPC64R12SetupStub::writeTo(uint8_t *buf) {1193int64_t offset = (gotPlt ? destination.getGotPltVA() : destination.getVA()) -1194getThunkTargetSym()->getVA();1195if (!isInt<34>(offset))1196reportRangeError(buf, offset, 34, destination, "R12 setup stub offset");11971198int nextInstOffset;1199if (config->power10Stubs) {1200const uint64_t imm = (((offset >> 16) & 0x3ffff) << 32) | (offset & 0xffff);1201// pld 12, func@plt@pcrel or paddi r12, 0, func@pcrel1202writePrefixedInstruction(1203buf, (gotPlt ? PLD_R12_NO_DISP : PADDI_R12_NO_DISP) | imm);1204nextInstOffset = 8;1205} else {1206uint32_t off = offset - 8;1207write32(buf + 0, 0x7d8802a6); // mflr 121208write32(buf + 4, 0x429f0005); // bcl 20,31,.+41209write32(buf + 8, 0x7d6802a6); // mflr 111210write32(buf + 12, 0x7d8803a6); // mtlr 121211write32(buf + 16,12120x3d8b0000 | ((off + 0x8000) >> 16)); // addis 12,11,off@ha1213if (gotPlt)1214write32(buf + 20, 0xe98c0000 | (off & 0xffff)); // ld 12, off@l(12)1215else1216write32(buf + 20, 0x398c0000 | (off & 0xffff)); // addi 12,12,off@l1217nextInstOffset = 24;1218}1219write32(buf + nextInstOffset, MTCTR_R12); // mtctr r121220write32(buf + nextInstOffset + 4, BCTR); // bctr1221}12221223void PPC64R12SetupStub::addSymbols(ThunkSection &isec) {1224addSymbol(saver().save((gotPlt ? "__plt_pcrel_" : "__gep_setup_") +1225destination.getName()),1226STT_FUNC, 0, isec);1227}12281229bool PPC64R12SetupStub::isCompatibleWith(const InputSection &isec,1230const Relocation &rel) const {1231return rel.type == R_PPC64_REL24_NOTOC;1232}12331234void PPC64LongBranchThunk::writeTo(uint8_t *buf) {1235int64_t offset = in.ppc64LongBranchTarget->getEntryVA(&destination, addend) -1236getPPC64TocBase();1237writePPC64LoadAndBranch(buf, offset);1238}12391240void PPC64LongBranchThunk::addSymbols(ThunkSection &isec) {1241addSymbol(saver().save("__long_branch_" + destination.getName()), STT_FUNC, 0,1242isec);1243}12441245bool PPC64LongBranchThunk::isCompatibleWith(const InputSection &isec,1246const Relocation &rel) const {1247return rel.type == R_PPC64_REL24 || rel.type == R_PPC64_REL14;1248}12491250Thunk::Thunk(Symbol &d, int64_t a) : destination(d), addend(a), offset(0) {1251destination.thunkAccessed = true;1252}12531254Thunk::~Thunk() = default;12551256static Thunk *addThunkAArch64(RelType type, Symbol &s, int64_t a) {1257if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 &&1258type != R_AARCH64_PLT32)1259fatal("unrecognized relocation type");1260if (config->picThunk)1261return make<AArch64ADRPThunk>(s, a);1262return make<AArch64ABSLongThunk>(s, a);1263}12641265// Creates a thunk for long branches or Thumb-ARM interworking.1266// Arm Architectures v4t does not support Thumb2 technology, and does not1267// support BLX or LDR Arm/Thumb state switching. This means that1268// - MOVT and MOVW instructions cannot be used.1269// - We can't rewrite BL in place to BLX. We will need thunks.1270//1271// TODO: use B for short Thumb->Arm thunks instead of LDR (this doesn't work for1272// Arm->Thumb, as in Arm state no BX PC trick; it doesn't switch state).1273static Thunk *addThunkArmv4(RelType reloc, Symbol &s, int64_t a) {1274bool thumb_target = s.getVA(a) & 1;12751276switch (reloc) {1277case R_ARM_PC24:1278case R_ARM_PLT32:1279case R_ARM_JUMP24:1280case R_ARM_CALL:1281if (config->picThunk) {1282if (thumb_target)1283return make<ARMV4PILongBXThunk>(s, a);1284return make<ARMV4PILongThunk>(s, a);1285}1286if (thumb_target)1287return make<ARMV4ABSLongBXThunk>(s, a);1288return make<ARMV5LongLdrPcThunk>(s, a);1289case R_ARM_THM_CALL:1290if (config->picThunk) {1291if (thumb_target)1292return make<ThumbV4PILongThunk>(s, a);1293return make<ThumbV4PILongBXThunk>(s, a);1294}1295if (thumb_target)1296return make<ThumbV4ABSLongThunk>(s, a);1297return make<ThumbV4ABSLongBXThunk>(s, a);1298}1299fatal("relocation " + toString(reloc) + " to " + toString(s) +1300" not supported for Armv4 or Armv4T target");1301}13021303// Creates a thunk for Thumb-ARM interworking compatible with Armv5 and Armv6.1304// Arm Architectures v5 and v6 do not support Thumb2 technology. This means that1305// - MOVT and MOVW instructions cannot be used1306// - Only Thumb relocation that can generate a Thunk is a BL, this can always1307// be transformed into a BLX1308static Thunk *addThunkArmv5v6(RelType reloc, Symbol &s, int64_t a) {1309switch (reloc) {1310case R_ARM_PC24:1311case R_ARM_PLT32:1312case R_ARM_JUMP24:1313case R_ARM_CALL:1314case R_ARM_THM_CALL:1315if (config->picThunk)1316return make<ARMV4PILongBXThunk>(s, a);1317return make<ARMV5LongLdrPcThunk>(s, a);1318}1319fatal("relocation " + toString(reloc) + " to " + toString(s) +1320" not supported for Armv5 or Armv6 targets");1321}13221323// Create a thunk for Thumb long branch on V6-M.1324// Arm Architecture v6-M only supports Thumb instructions. This means1325// - MOVT and MOVW instructions cannot be used.1326// - Only a limited number of instructions can access registers r8 and above1327// - No interworking support is needed (all Thumb).1328static Thunk *addThunkV6M(const InputSection &isec, RelType reloc, Symbol &s,1329int64_t a) {1330const bool isPureCode = isec.getParent()->flags & SHF_ARM_PURECODE;1331switch (reloc) {1332case R_ARM_THM_JUMP19:1333case R_ARM_THM_JUMP24:1334case R_ARM_THM_CALL:1335if (config->isPic) {1336if (!isPureCode)1337return make<ThumbV6MPILongThunk>(s, a);13381339fatal("relocation " + toString(reloc) + " to " + toString(s) +1340" not supported for Armv6-M targets for position independent"1341" and execute only code");1342}1343if (isPureCode)1344return make<ThumbV6MABSXOLongThunk>(s, a);1345return make<ThumbV6MABSLongThunk>(s, a);1346}1347fatal("relocation " + toString(reloc) + " to " + toString(s) +1348" not supported for Armv6-M targets");1349}13501351// Creates a thunk for Thumb-ARM interworking or branch range extension.1352static Thunk *addThunkArm(const InputSection &isec, RelType reloc, Symbol &s,1353int64_t a) {1354// Decide which Thunk is needed based on:1355// Available instruction set1356// - An Arm Thunk can only be used if Arm state is available.1357// - A Thumb Thunk can only be used if Thumb state is available.1358// - Can only use a Thunk if it uses instructions that the Target supports.1359// Relocation is branch or branch and link1360// - Branch instructions cannot change state, can only select Thunk that1361// starts in the same state as the caller.1362// - Branch and link relocations can change state, can select Thunks from1363// either Arm or Thumb.1364// Position independent Thunks if we require position independent code.1365// Execute Only Thunks if the output section is execute only code.13661367// Handle architectures that have restrictions on the instructions that they1368// can use in Thunks. The flags below are set by reading the BuildAttributes1369// of the input objects. InputFiles.cpp contains the mapping from ARM1370// architecture to flag.1371if (!config->armHasMovtMovw) {1372if (config->armJ1J2BranchEncoding)1373return addThunkV6M(isec, reloc, s, a);1374if (config->armHasBlx)1375return addThunkArmv5v6(reloc, s, a);1376return addThunkArmv4(reloc, s, a);1377}13781379switch (reloc) {1380case R_ARM_PC24:1381case R_ARM_PLT32:1382case R_ARM_JUMP24:1383case R_ARM_CALL:1384if (config->picThunk)1385return make<ARMV7PILongThunk>(s, a);1386return make<ARMV7ABSLongThunk>(s, a);1387case R_ARM_THM_JUMP19:1388case R_ARM_THM_JUMP24:1389case R_ARM_THM_CALL:1390if (config->picThunk)1391return make<ThumbV7PILongThunk>(s, a);1392return make<ThumbV7ABSLongThunk>(s, a);1393}1394fatal("unrecognized relocation type");1395}13961397static Thunk *addThunkAVR(RelType type, Symbol &s, int64_t a) {1398switch (type) {1399case R_AVR_LO8_LDI_GS:1400case R_AVR_HI8_LDI_GS:1401return make<AVRThunk>(s, a);1402default:1403fatal("unrecognized relocation type " + toString(type));1404}1405}14061407static Thunk *addThunkMips(RelType type, Symbol &s) {1408if ((s.stOther & STO_MIPS_MICROMIPS) && isMipsR6())1409return make<MicroMipsR6Thunk>(s);1410if (s.stOther & STO_MIPS_MICROMIPS)1411return make<MicroMipsThunk>(s);1412return make<MipsThunk>(s);1413}14141415static Thunk *addThunkPPC32(const InputSection &isec, const Relocation &rel,1416Symbol &s) {1417assert((rel.type == R_PPC_LOCAL24PC || rel.type == R_PPC_REL24 ||1418rel.type == R_PPC_PLTREL24) &&1419"unexpected relocation type for thunk");1420if (s.isInPlt())1421return make<PPC32PltCallStub>(isec, rel, s);1422return make<PPC32LongThunk>(s, rel.addend);1423}14241425static Thunk *addThunkPPC64(RelType type, Symbol &s, int64_t a) {1426assert((type == R_PPC64_REL14 || type == R_PPC64_REL24 ||1427type == R_PPC64_REL24_NOTOC) &&1428"unexpected relocation type for thunk");14291430// If we are emitting stubs for NOTOC relocations, we need to tell1431// the PLT resolver that there can be multiple TOCs.1432if (type == R_PPC64_REL24_NOTOC)1433getPPC64TargetInfo()->ppc64DynamicSectionOpt = 0x2;14341435if (s.isInPlt())1436return type == R_PPC64_REL24_NOTOC1437? (Thunk *)make<PPC64R12SetupStub>(s, /*gotPlt=*/true)1438: (Thunk *)make<PPC64PltCallStub>(s);14391440// This check looks at the st_other bits of the callee. If the value is 11441// then the callee clobbers the TOC and we need an R2 save stub when RelType1442// is R_PPC64_REL14 or R_PPC64_REL24.1443if ((type == R_PPC64_REL14 || type == R_PPC64_REL24) && (s.stOther >> 5) == 1)1444return make<PPC64R2SaveStub>(s, a);14451446if (type == R_PPC64_REL24_NOTOC)1447return make<PPC64R12SetupStub>(s, /*gotPlt=*/false);14481449if (config->picThunk)1450return make<PPC64PILongBranchThunk>(s, a);14511452return make<PPC64PDLongBranchThunk>(s, a);1453}14541455Thunk *elf::addThunk(const InputSection &isec, Relocation &rel) {1456Symbol &s = *rel.sym;1457int64_t a = rel.addend;14581459switch (config->emachine) {1460case EM_AARCH64:1461return addThunkAArch64(rel.type, s, a);1462case EM_ARM:1463return addThunkArm(isec, rel.type, s, a);1464case EM_AVR:1465return addThunkAVR(rel.type, s, a);1466case EM_MIPS:1467return addThunkMips(rel.type, s);1468case EM_PPC:1469return addThunkPPC32(isec, rel, s);1470case EM_PPC64:1471return addThunkPPC64(rel.type, s, a);1472default:1473llvm_unreachable("add Thunk only supported for ARM, AVR, Mips and PowerPC");1474}1475}147614771478