2016-07-09 00:10:27 +08:00
|
|
|
//===- Thunks.cpp --------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-07-09 00:10:27 +08:00
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
//
|
2016-07-10 07:16:00 +08:00
|
|
|
// This file contains Thunk subclasses.
|
|
|
|
//
|
|
|
|
// A thunk is a small piece of code written after an input section
|
|
|
|
// which is used to jump between "incompatible" functions
|
|
|
|
// such as MIPS PIC and non-PIC or ARM non-Thumb and Thumb functions.
|
|
|
|
//
|
|
|
|
// If a jump target is too far and its address doesn't fit to a
|
|
|
|
// short jump instruction, we need to create a thunk too, but we
|
|
|
|
// haven't supported it yet.
|
|
|
|
//
|
|
|
|
// i386 and x86-64 don't need thunks.
|
2016-07-09 00:10:27 +08:00
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Thunks.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "Config.h"
|
2016-07-09 00:10:27 +08:00
|
|
|
#include "InputSection.h"
|
|
|
|
#include "OutputSections.h"
|
|
|
|
#include "Symbols.h"
|
2017-02-01 18:26:03 +08:00
|
|
|
#include "SyntheticSections.h"
|
2016-07-09 00:10:27 +08:00
|
|
|
#include "Target.h"
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2017-11-29 04:39:17 +08:00
|
|
|
#include "lld/Common/Memory.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2016-07-09 00:10:27 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
2016-07-09 00:10:27 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
using namespace llvm::ELF;
|
2020-05-15 13:18:58 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
2016-07-09 00:10:27 +08:00
|
|
|
|
2016-07-10 06:52:30 +08:00
|
|
|
namespace {
|
2016-11-05 09:00:56 +08:00
|
|
|
|
2017-11-29 19:15:12 +08:00
|
|
|
// AArch64 long range Thunks
|
|
|
|
class AArch64ABSLongThunk final : public Thunk {
|
|
|
|
public:
|
2019-11-23 16:57:54 +08:00
|
|
|
AArch64ABSLongThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
|
2018-03-29 05:33:31 +08:00
|
|
|
uint32_t size() override { return 16; }
|
|
|
|
void writeTo(uint8_t *buf) override;
|
2017-11-29 19:15:12 +08:00
|
|
|
void addSymbols(ThunkSection &isec) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AArch64ADRPThunk final : public Thunk {
|
|
|
|
public:
|
2019-11-23 16:57:54 +08:00
|
|
|
AArch64ADRPThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
|
2018-03-29 05:33:31 +08:00
|
|
|
uint32_t size() override { return 12; }
|
|
|
|
void writeTo(uint8_t *buf) override;
|
2017-11-29 19:15:12 +08:00
|
|
|
void addSymbols(ThunkSection &isec) override;
|
|
|
|
};
|
|
|
|
|
2018-03-30 06:43:52 +08:00
|
|
|
// Base class for ARM thunks.
|
|
|
|
//
|
|
|
|
// An ARM thunk may be either short or long. A short thunk is simply a branch
|
|
|
|
// (B) instruction, and it may be used to call ARM functions when the distance
|
|
|
|
// from the thunk to the target is less than 32MB. Long thunks can branch to any
|
|
|
|
// virtual address and can switch between ARM and Thumb, and they are
|
|
|
|
// implemented in the derived classes. This class tries to create a short thunk
|
|
|
|
// if the target is in range, otherwise it creates a long thunk.
|
|
|
|
class ARMThunk : public Thunk {
|
2016-07-09 00:10:27 +08:00
|
|
|
public:
|
2019-11-23 16:57:54 +08:00
|
|
|
ARMThunk(Symbol &dest) : Thunk(dest, 0) {}
|
2016-07-10 06:52:30 +08:00
|
|
|
|
2019-07-03 15:08:27 +08:00
|
|
|
bool getMayUseShortThunk();
|
|
|
|
uint32_t size() override { return getMayUseShortThunk() ? 4 : sizeLong(); }
|
2018-03-29 05:33:31 +08:00
|
|
|
void writeTo(uint8_t *buf) override;
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
bool isCompatibleWith(const InputSection &isec,
|
|
|
|
const Relocation &rel) const override;
|
2018-03-30 06:43:52 +08:00
|
|
|
|
|
|
|
// Returns the size of a long thunk.
|
|
|
|
virtual uint32_t sizeLong() = 0;
|
|
|
|
|
|
|
|
// Writes a long thunk to Buf.
|
|
|
|
virtual void writeLong(uint8_t *buf) = 0;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// This field tracks whether all previously considered layouts would allow
|
|
|
|
// this thunk to be short. If we have ever needed a long thunk, we always
|
|
|
|
// create a long thunk, even if the thunk may be short given the current
|
|
|
|
// distance to the target. We do this because transitioning from long to short
|
|
|
|
// can create layout oscillations in certain corner cases which would prevent
|
|
|
|
// the layout from converging.
|
|
|
|
bool mayUseShortThunk = true;
|
2016-07-09 00:10:27 +08:00
|
|
|
};
|
|
|
|
|
2018-03-30 06:43:52 +08:00
|
|
|
// Base class for Thumb-2 thunks.
|
|
|
|
//
|
|
|
|
// This class is similar to ARMThunk, but it uses the Thumb-2 B.W instruction
|
|
|
|
// which has a range of 16MB.
|
|
|
|
class ThumbThunk : public Thunk {
|
2016-07-09 00:10:27 +08:00
|
|
|
public:
|
2019-11-23 16:57:54 +08:00
|
|
|
ThumbThunk(Symbol &dest) : Thunk(dest, 0) { alignment = 2; }
|
2016-07-10 06:52:30 +08:00
|
|
|
|
2019-07-03 15:08:27 +08:00
|
|
|
bool getMayUseShortThunk();
|
|
|
|
uint32_t size() override { return getMayUseShortThunk() ? 4 : sizeLong(); }
|
2018-03-29 05:33:31 +08:00
|
|
|
void writeTo(uint8_t *buf) override;
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
bool isCompatibleWith(const InputSection &isec,
|
|
|
|
const Relocation &rel) const override;
|
2018-03-30 06:43:52 +08:00
|
|
|
|
|
|
|
// Returns the size of a long thunk.
|
|
|
|
virtual uint32_t sizeLong() = 0;
|
|
|
|
|
|
|
|
// Writes a long thunk to Buf.
|
|
|
|
virtual void writeLong(uint8_t *buf) = 0;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// See comment in ARMThunk above.
|
|
|
|
bool mayUseShortThunk = true;
|
2016-07-09 00:10:27 +08:00
|
|
|
};
|
|
|
|
|
2018-03-30 06:43:52 +08:00
|
|
|
// Specific ARM Thunk implementations. The naming convention is:
|
|
|
|
// Source State, TargetState, Target Requirement, ABS or PI, Range
|
|
|
|
class ARMV7ABSLongThunk final : public ARMThunk {
|
2016-07-09 00:10:27 +08:00
|
|
|
public:
|
2018-03-30 06:43:52 +08:00
|
|
|
ARMV7ABSLongThunk(Symbol &dest) : ARMThunk(dest) {}
|
2016-07-10 06:52:30 +08:00
|
|
|
|
2018-03-30 06:43:52 +08:00
|
|
|
uint32_t sizeLong() override { return 12; }
|
|
|
|
void writeLong(uint8_t *buf) override;
|
2017-03-16 18:40:50 +08:00
|
|
|
void addSymbols(ThunkSection &isec) override;
|
2016-07-09 00:10:27 +08:00
|
|
|
};
|
|
|
|
|
2018-03-30 06:43:52 +08:00
|
|
|
class ARMV7PILongThunk final : public ARMThunk {
|
2016-07-09 00:10:27 +08:00
|
|
|
public:
|
2018-03-30 06:43:52 +08:00
|
|
|
ARMV7PILongThunk(Symbol &dest) : ARMThunk(dest) {}
|
2016-07-10 06:52:30 +08:00
|
|
|
|
2018-03-30 06:43:52 +08:00
|
|
|
uint32_t sizeLong() override { return 16; }
|
|
|
|
void writeLong(uint8_t *buf) override;
|
|
|
|
void addSymbols(ThunkSection &isec) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ThumbV7ABSLongThunk final : public ThumbThunk {
|
|
|
|
public:
|
|
|
|
ThumbV7ABSLongThunk(Symbol &dest) : ThumbThunk(dest) {}
|
|
|
|
|
|
|
|
uint32_t sizeLong() override { return 10; }
|
|
|
|
void writeLong(uint8_t *buf) override;
|
|
|
|
void addSymbols(ThunkSection &isec) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ThumbV7PILongThunk final : public ThumbThunk {
|
|
|
|
public:
|
|
|
|
ThumbV7PILongThunk(Symbol &dest) : ThumbThunk(dest) {}
|
|
|
|
|
|
|
|
uint32_t sizeLong() override { return 12; }
|
|
|
|
void writeLong(uint8_t *buf) override;
|
2017-03-16 18:40:50 +08:00
|
|
|
void addSymbols(ThunkSection &isec) override;
|
2016-07-09 00:10:27 +08:00
|
|
|
};
|
|
|
|
|
2018-08-20 17:37:50 +08:00
|
|
|
// Implementations of Thunks for older Arm architectures that do not support
|
|
|
|
// the movt/movw instructions. These thunks require at least Architecture v5
|
|
|
|
// as used on processors such as the Arm926ej-s. There are no Thumb entry
|
|
|
|
// points as there is no Thumb branch instruction on these architecture that
|
|
|
|
// can result in a thunk
|
|
|
|
class ARMV5ABSLongThunk final : public ARMThunk {
|
|
|
|
public:
|
|
|
|
ARMV5ABSLongThunk(Symbol &dest) : ARMThunk(dest) {}
|
|
|
|
|
|
|
|
uint32_t sizeLong() override { return 8; }
|
|
|
|
void writeLong(uint8_t *buf) override;
|
|
|
|
void addSymbols(ThunkSection &isec) override;
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
bool isCompatibleWith(const InputSection &isec,
|
|
|
|
const Relocation &rel) const override;
|
2018-08-20 17:37:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class ARMV5PILongThunk final : public ARMThunk {
|
|
|
|
public:
|
|
|
|
ARMV5PILongThunk(Symbol &dest) : ARMThunk(dest) {}
|
|
|
|
|
|
|
|
uint32_t sizeLong() override { return 16; }
|
|
|
|
void writeLong(uint8_t *buf) override;
|
|
|
|
void addSymbols(ThunkSection &isec) override;
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
bool isCompatibleWith(const InputSection &isec,
|
|
|
|
const Relocation &rel) const override;
|
2018-08-20 17:37:50 +08:00
|
|
|
};
|
|
|
|
|
2018-12-17 18:33:47 +08:00
|
|
|
// Implementations of Thunks for Arm v6-M. Only Thumb instructions are permitted
|
|
|
|
class ThumbV6MABSLongThunk final : public ThumbThunk {
|
|
|
|
public:
|
|
|
|
ThumbV6MABSLongThunk(Symbol &dest) : ThumbThunk(dest) {}
|
|
|
|
|
|
|
|
uint32_t sizeLong() override { return 12; }
|
|
|
|
void writeLong(uint8_t *buf) override;
|
|
|
|
void addSymbols(ThunkSection &isec) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ThumbV6MPILongThunk final : public ThumbThunk {
|
|
|
|
public:
|
|
|
|
ThumbV6MPILongThunk(Symbol &dest) : ThumbThunk(dest) {}
|
|
|
|
|
|
|
|
uint32_t sizeLong() override { return 16; }
|
|
|
|
void writeLong(uint8_t *buf) override;
|
|
|
|
void addSymbols(ThunkSection &isec) override;
|
|
|
|
};
|
|
|
|
|
2016-07-10 06:52:32 +08:00
|
|
|
// MIPS LA25 thunk
|
2017-05-17 15:10:59 +08:00
|
|
|
class MipsThunk final : public Thunk {
|
2016-07-09 00:10:27 +08:00
|
|
|
public:
|
2019-11-23 16:57:54 +08:00
|
|
|
MipsThunk(Symbol &dest) : Thunk(dest, 0) {}
|
2016-07-10 06:52:30 +08:00
|
|
|
|
2018-03-29 05:33:31 +08:00
|
|
|
uint32_t size() override { return 16; }
|
|
|
|
void writeTo(uint8_t *buf) override;
|
2017-03-16 18:40:50 +08:00
|
|
|
void addSymbols(ThunkSection &isec) override;
|
2017-02-24 00:49:07 +08:00
|
|
|
InputSection *getTargetInputSection() const override;
|
2016-07-09 00:10:27 +08:00
|
|
|
};
|
2016-11-05 09:00:56 +08:00
|
|
|
|
2017-10-03 21:30:02 +08:00
|
|
|
// microMIPS R2-R5 LA25 thunk
|
|
|
|
class MicroMipsThunk final : public Thunk {
|
|
|
|
public:
|
2019-11-23 16:57:54 +08:00
|
|
|
MicroMipsThunk(Symbol &dest) : Thunk(dest, 0) {}
|
2017-10-03 21:30:02 +08:00
|
|
|
|
2018-03-29 05:33:31 +08:00
|
|
|
uint32_t size() override { return 14; }
|
|
|
|
void writeTo(uint8_t *buf) override;
|
2017-10-03 21:30:02 +08:00
|
|
|
void addSymbols(ThunkSection &isec) override;
|
|
|
|
InputSection *getTargetInputSection() const override;
|
|
|
|
};
|
|
|
|
|
|
|
|
// microMIPS R6 LA25 thunk
|
|
|
|
class MicroMipsR6Thunk final : public Thunk {
|
|
|
|
public:
|
2019-11-23 16:57:54 +08:00
|
|
|
MicroMipsR6Thunk(Symbol &dest) : Thunk(dest, 0) {}
|
2017-10-03 21:30:02 +08:00
|
|
|
|
2018-03-29 05:33:31 +08:00
|
|
|
uint32_t size() override { return 12; }
|
|
|
|
void writeTo(uint8_t *buf) override;
|
2017-10-03 21:30:02 +08:00
|
|
|
void addSymbols(ThunkSection &isec) override;
|
|
|
|
InputSection *getTargetInputSection() const override;
|
|
|
|
};
|
|
|
|
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
class PPC32PltCallStub final : public Thunk {
|
|
|
|
public:
|
2019-11-23 16:57:54 +08:00
|
|
|
// For R_PPC_PLTREL24, Thunk::addend records the addend which will be used to
|
|
|
|
// decide the offsets in the call stub.
|
|
|
|
PPC32PltCallStub(const InputSection &isec, const Relocation &rel,
|
|
|
|
Symbol &dest)
|
2020-01-26 10:58:54 +08:00
|
|
|
: Thunk(dest, rel.addend), file(isec.file) {}
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
uint32_t size() override { return 16; }
|
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
void addSymbols(ThunkSection &isec) override;
|
|
|
|
bool isCompatibleWith(const InputSection &isec, const Relocation &rel) const override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Records the call site of the call stub.
|
|
|
|
const InputFile *file;
|
|
|
|
};
|
2018-05-07 03:13:29 +08:00
|
|
|
|
2020-01-26 10:58:54 +08:00
|
|
|
class PPC32LongThunk final : public Thunk {
|
|
|
|
public:
|
|
|
|
PPC32LongThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
|
|
|
|
uint32_t size() override { return config->isPic ? 32 : 16; }
|
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
void addSymbols(ThunkSection &isec) override;
|
|
|
|
};
|
|
|
|
|
2018-05-07 03:13:29 +08:00
|
|
|
// PPC64 Plt call stubs.
|
|
|
|
// Any call site that needs to call through a plt entry needs a call stub in
|
|
|
|
// the .text section. The call stub is responsible for:
|
|
|
|
// 1) Saving the toc-pointer to the stack.
|
|
|
|
// 2) Loading the target functions address from the procedure linkage table into
|
|
|
|
// r12 for use by the target functions global entry point, and into the count
|
|
|
|
// register.
|
2019-10-29 09:41:38 +08:00
|
|
|
// 3) Transferring control to the target function through an indirect branch.
|
2018-05-07 03:13:29 +08:00
|
|
|
class PPC64PltCallStub final : public Thunk {
|
|
|
|
public:
|
2019-11-23 16:57:54 +08:00
|
|
|
PPC64PltCallStub(Symbol &dest) : Thunk(dest, 0) {}
|
2018-05-07 03:50:04 +08:00
|
|
|
uint32_t size() override { return 20; }
|
2018-05-07 03:13:29 +08:00
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
void addSymbols(ThunkSection &isec) override;
|
|
|
|
};
|
|
|
|
|
2018-11-15 01:56:43 +08:00
|
|
|
// A bl instruction uses a signed 24 bit offset, with an implicit 4 byte
|
|
|
|
// alignment. This gives a possible 26 bits of 'reach'. If the call offset is
|
|
|
|
// larger then that we need to emit a long-branch thunk. The target address
|
|
|
|
// of the callee is stored in a table to be accessed TOC-relative. Since the
|
|
|
|
// call must be local (a non-local call will have a PltCallStub instead) the
|
|
|
|
// table stores the address of the callee's local entry point. For
|
|
|
|
// position-independent code a corresponding relative dynamic relocation is
|
|
|
|
// used.
|
|
|
|
class PPC64LongBranchThunk : public Thunk {
|
|
|
|
public:
|
|
|
|
uint32_t size() override { return 16; }
|
|
|
|
void writeTo(uint8_t *buf) override;
|
|
|
|
void addSymbols(ThunkSection &isec) override;
|
|
|
|
|
|
|
|
protected:
|
2019-12-03 06:20:42 +08:00
|
|
|
PPC64LongBranchThunk(Symbol &dest, int64_t addend) : Thunk(dest, addend) {}
|
2018-11-15 01:56:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class PPC64PILongBranchThunk final : public PPC64LongBranchThunk {
|
|
|
|
public:
|
2019-12-03 06:20:42 +08:00
|
|
|
PPC64PILongBranchThunk(Symbol &dest, int64_t addend)
|
|
|
|
: PPC64LongBranchThunk(dest, addend) {
|
2018-11-15 01:56:43 +08:00
|
|
|
assert(!dest.isPreemptible);
|
2019-12-03 06:20:42 +08:00
|
|
|
if (Optional<uint32_t> index =
|
|
|
|
in.ppc64LongBranchTarget->addEntry(&dest, addend)) {
|
|
|
|
mainPart->relaDyn->addReloc(
|
|
|
|
{target->relativeRel, in.ppc64LongBranchTarget, *index * UINT64_C(8),
|
|
|
|
true, &dest,
|
|
|
|
addend + getPPC64GlobalEntryToLocalEntryOffset(dest.stOther)});
|
|
|
|
}
|
2018-11-15 01:56:43 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class PPC64PDLongBranchThunk final : public PPC64LongBranchThunk {
|
|
|
|
public:
|
2019-12-03 06:20:42 +08:00
|
|
|
PPC64PDLongBranchThunk(Symbol &dest, int64_t addend)
|
|
|
|
: PPC64LongBranchThunk(dest, addend) {
|
|
|
|
in.ppc64LongBranchTarget->addEntry(&dest, addend);
|
2018-11-15 01:56:43 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-05 09:00:56 +08:00
|
|
|
} // end anonymous namespace
|
2016-07-10 06:52:30 +08:00
|
|
|
|
2018-03-30 06:32:13 +08:00
|
|
|
Defined *Thunk::addSymbol(StringRef name, uint8_t type, uint64_t value,
|
|
|
|
InputSectionBase §ion) {
|
2019-07-16 12:46:31 +08:00
|
|
|
Defined *d = addSyntheticLocal(name, type, value, /*size=*/0, section);
|
2018-03-30 06:32:13 +08:00
|
|
|
syms.push_back(d);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Thunk::setOffset(uint64_t newOffset) {
|
|
|
|
for (Defined *d : syms)
|
|
|
|
d->value = d->value - offset + newOffset;
|
|
|
|
offset = newOffset;
|
|
|
|
}
|
|
|
|
|
2017-11-29 19:15:12 +08:00
|
|
|
// AArch64 long range Thunks
|
|
|
|
|
2019-11-23 16:57:54 +08:00
|
|
|
static uint64_t getAArch64ThunkDestVA(const Symbol &s, int64_t a) {
|
|
|
|
uint64_t v = s.isInPlt() ? s.getPltVA() : s.getVA(a);
|
2017-11-29 19:15:12 +08:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2018-03-29 05:33:31 +08:00
|
|
|
void AArch64ABSLongThunk::writeTo(uint8_t *buf) {
|
2017-11-29 19:15:12 +08:00
|
|
|
const uint8_t data[] = {
|
|
|
|
0x50, 0x00, 0x00, 0x58, // ldr x16, L0
|
|
|
|
0x00, 0x02, 0x1f, 0xd6, // br x16
|
|
|
|
0x00, 0x00, 0x00, 0x00, // L0: .xword S
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
};
|
2019-11-23 16:57:54 +08:00
|
|
|
uint64_t s = getAArch64ThunkDestVA(destination, addend);
|
2017-11-29 19:15:12 +08:00
|
|
|
memcpy(buf, data, sizeof(data));
|
2020-01-23 13:39:16 +08:00
|
|
|
target->relocateNoSym(buf + 8, R_AARCH64_ABS64, s);
|
2017-11-29 19:15:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AArch64ABSLongThunk::addSymbols(ThunkSection &isec) {
|
2018-03-30 06:32:13 +08:00
|
|
|
addSymbol(saver.save("__AArch64AbsLongThunk_" + destination.getName()),
|
|
|
|
STT_FUNC, 0, isec);
|
|
|
|
addSymbol("$x", STT_NOTYPE, 0, isec);
|
|
|
|
addSymbol("$d", STT_NOTYPE, 8, isec);
|
2017-11-29 19:15:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This Thunk has a maximum range of 4Gb, this is sufficient for all programs
|
|
|
|
// using the small code model, including pc-relative ones. At time of writing
|
|
|
|
// clang and gcc do not support the large code model for position independent
|
|
|
|
// code so it is safe to use this for position independent thunks without
|
|
|
|
// worrying about the destination being more than 4Gb away.
|
2018-03-29 05:33:31 +08:00
|
|
|
void AArch64ADRPThunk::writeTo(uint8_t *buf) {
|
2017-11-29 19:15:12 +08:00
|
|
|
const uint8_t data[] = {
|
|
|
|
0x10, 0x00, 0x00, 0x90, // adrp x16, Dest R_AARCH64_ADR_PREL_PG_HI21(Dest)
|
|
|
|
0x10, 0x02, 0x00, 0x91, // add x16, x16, R_AARCH64_ADD_ABS_LO12_NC(Dest)
|
|
|
|
0x00, 0x02, 0x1f, 0xd6, // br x16
|
|
|
|
};
|
2019-11-23 16:57:54 +08:00
|
|
|
uint64_t s = getAArch64ThunkDestVA(destination, addend);
|
2018-03-30 06:32:13 +08:00
|
|
|
uint64_t p = getThunkTargetSym()->getVA();
|
2017-11-29 19:15:12 +08:00
|
|
|
memcpy(buf, data, sizeof(data));
|
2020-01-23 13:39:16 +08:00
|
|
|
target->relocateNoSym(buf, R_AARCH64_ADR_PREL_PG_HI21,
|
|
|
|
getAArch64Page(s) - getAArch64Page(p));
|
|
|
|
target->relocateNoSym(buf + 4, R_AARCH64_ADD_ABS_LO12_NC, s);
|
2017-11-29 19:15:12 +08:00
|
|
|
}
|
|
|
|
|
2018-03-30 06:32:13 +08:00
|
|
|
void AArch64ADRPThunk::addSymbols(ThunkSection &isec) {
|
|
|
|
addSymbol(saver.save("__AArch64ADRPThunk_" + destination.getName()), STT_FUNC,
|
|
|
|
0, isec);
|
|
|
|
addSymbol("$x", STT_NOTYPE, 0, isec);
|
2017-11-29 19:15:12 +08:00
|
|
|
}
|
|
|
|
|
2016-07-10 06:52:30 +08:00
|
|
|
// ARM Target Thunks
|
2017-11-04 05:21:47 +08:00
|
|
|
static uint64_t getARMThunkDestVA(const Symbol &s) {
|
2017-03-17 19:56:54 +08:00
|
|
|
uint64_t v = s.isInPlt() ? s.getPltVA() : s.getVA();
|
2016-09-01 21:52:52 +08:00
|
|
|
return SignExtend64<32>(v);
|
2016-07-10 06:52:30 +08:00
|
|
|
}
|
2016-07-09 00:10:27 +08:00
|
|
|
|
2018-03-30 06:43:52 +08:00
|
|
|
// This function returns true if the target is not Thumb and is within 2^26, and
|
2019-07-16 13:50:45 +08:00
|
|
|
// it has not previously returned false (see comment for mayUseShortThunk).
|
2019-07-03 15:08:27 +08:00
|
|
|
bool ARMThunk::getMayUseShortThunk() {
|
2018-03-30 06:43:52 +08:00
|
|
|
if (!mayUseShortThunk)
|
|
|
|
return false;
|
|
|
|
uint64_t s = getARMThunkDestVA(destination);
|
|
|
|
if (s & 1) {
|
|
|
|
mayUseShortThunk = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint64_t p = getThunkTargetSym()->getVA();
|
|
|
|
int64_t offset = s - p - 8;
|
|
|
|
mayUseShortThunk = llvm::isInt<26>(offset);
|
|
|
|
return mayUseShortThunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ARMThunk::writeTo(uint8_t *buf) {
|
2019-07-03 15:08:27 +08:00
|
|
|
if (!getMayUseShortThunk()) {
|
2018-03-30 06:43:52 +08:00
|
|
|
writeLong(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t s = getARMThunkDestVA(destination);
|
|
|
|
uint64_t p = getThunkTargetSym()->getVA();
|
|
|
|
int64_t offset = s - p - 8;
|
|
|
|
const uint8_t data[] = {
|
|
|
|
0x00, 0x00, 0x00, 0xea, // b S
|
|
|
|
};
|
|
|
|
memcpy(buf, data, sizeof(data));
|
2020-01-23 13:39:16 +08:00
|
|
|
target->relocateNoSym(buf, R_ARM_JUMP24, offset);
|
2018-03-30 06:43:52 +08:00
|
|
|
}
|
|
|
|
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
bool ARMThunk::isCompatibleWith(const InputSection &isec,
|
|
|
|
const Relocation &rel) const {
|
2018-03-30 06:43:52 +08:00
|
|
|
// Thumb branch relocations can't use BLX
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;
|
2018-03-30 06:43:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function returns true if the target is Thumb and is within 2^25, and
|
2019-07-16 13:50:45 +08:00
|
|
|
// it has not previously returned false (see comment for mayUseShortThunk).
|
2019-07-03 15:08:27 +08:00
|
|
|
bool ThumbThunk::getMayUseShortThunk() {
|
2018-03-30 06:43:52 +08:00
|
|
|
if (!mayUseShortThunk)
|
|
|
|
return false;
|
|
|
|
uint64_t s = getARMThunkDestVA(destination);
|
|
|
|
if ((s & 1) == 0) {
|
|
|
|
mayUseShortThunk = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint64_t p = getThunkTargetSym()->getVA() & ~1;
|
|
|
|
int64_t offset = s - p - 4;
|
|
|
|
mayUseShortThunk = llvm::isInt<25>(offset);
|
|
|
|
return mayUseShortThunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThumbThunk::writeTo(uint8_t *buf) {
|
2019-07-03 15:08:27 +08:00
|
|
|
if (!getMayUseShortThunk()) {
|
2018-03-30 06:43:52 +08:00
|
|
|
writeLong(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t s = getARMThunkDestVA(destination);
|
|
|
|
uint64_t p = getThunkTargetSym()->getVA();
|
|
|
|
int64_t offset = s - p - 4;
|
|
|
|
const uint8_t data[] = {
|
|
|
|
0x00, 0xf0, 0x00, 0xb0, // b.w S
|
|
|
|
};
|
|
|
|
memcpy(buf, data, sizeof(data));
|
2020-01-23 13:39:16 +08:00
|
|
|
target->relocateNoSym(buf, R_ARM_THM_JUMP24, offset);
|
2018-03-30 06:43:52 +08:00
|
|
|
}
|
|
|
|
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
bool ThumbThunk::isCompatibleWith(const InputSection &isec,
|
|
|
|
const Relocation &rel) const {
|
2018-03-30 06:43:52 +08:00
|
|
|
// ARM branch relocations can't use BLX
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
return rel.type != R_ARM_JUMP24 && rel.type != R_ARM_PC24 && rel.type != R_ARM_PLT32;
|
2018-03-30 06:43:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ARMV7ABSLongThunk::writeLong(uint8_t *buf) {
|
2016-07-10 06:52:30 +08:00
|
|
|
const uint8_t data[] = {
|
|
|
|
0x00, 0xc0, 0x00, 0xe3, // movw ip,:lower16:S
|
|
|
|
0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S
|
|
|
|
0x1c, 0xff, 0x2f, 0xe1, // bx ip
|
|
|
|
};
|
2017-05-17 15:10:59 +08:00
|
|
|
uint64_t s = getARMThunkDestVA(destination);
|
2016-07-10 06:52:30 +08:00
|
|
|
memcpy(buf, data, sizeof(data));
|
2020-01-23 13:39:16 +08:00
|
|
|
target->relocateNoSym(buf, R_ARM_MOVW_ABS_NC, s);
|
|
|
|
target->relocateNoSym(buf + 4, R_ARM_MOVT_ABS, s);
|
2016-07-10 06:52:30 +08:00
|
|
|
}
|
2016-07-09 00:10:27 +08:00
|
|
|
|
2017-05-17 15:10:59 +08:00
|
|
|
void ARMV7ABSLongThunk::addSymbols(ThunkSection &isec) {
|
2018-03-30 06:32:13 +08:00
|
|
|
addSymbol(saver.save("__ARMv7ABSLongThunk_" + destination.getName()),
|
|
|
|
STT_FUNC, 0, isec);
|
|
|
|
addSymbol("$a", STT_NOTYPE, 0, isec);
|
2017-02-01 18:26:03 +08:00
|
|
|
}
|
|
|
|
|
2018-03-30 06:43:52 +08:00
|
|
|
void ThumbV7ABSLongThunk::writeLong(uint8_t *buf) {
|
2016-07-10 06:52:30 +08:00
|
|
|
const uint8_t data[] = {
|
|
|
|
0x40, 0xf2, 0x00, 0x0c, // movw ip, :lower16:S
|
|
|
|
0xc0, 0xf2, 0x00, 0x0c, // movt ip, :upper16:S
|
|
|
|
0x60, 0x47, // bx ip
|
|
|
|
};
|
2017-05-17 15:10:59 +08:00
|
|
|
uint64_t s = getARMThunkDestVA(destination);
|
2016-07-10 06:52:30 +08:00
|
|
|
memcpy(buf, data, sizeof(data));
|
2020-01-23 13:39:16 +08:00
|
|
|
target->relocateNoSym(buf, R_ARM_THM_MOVW_ABS_NC, s);
|
|
|
|
target->relocateNoSym(buf + 4, R_ARM_THM_MOVT_ABS, s);
|
2016-07-10 06:52:30 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 15:10:59 +08:00
|
|
|
void ThumbV7ABSLongThunk::addSymbols(ThunkSection &isec) {
|
2018-03-30 06:32:13 +08:00
|
|
|
addSymbol(saver.save("__Thumbv7ABSLongThunk_" + destination.getName()),
|
|
|
|
STT_FUNC, 1, isec);
|
|
|
|
addSymbol("$t", STT_NOTYPE, 0, isec);
|
2017-02-01 18:26:03 +08:00
|
|
|
}
|
|
|
|
|
2018-03-30 06:43:52 +08:00
|
|
|
void ARMV7PILongThunk::writeLong(uint8_t *buf) {
|
2016-07-10 06:52:30 +08:00
|
|
|
const uint8_t data[] = {
|
2017-09-22 05:04:42 +08:00
|
|
|
0xf0, 0xcf, 0x0f, 0xe3, // P: movw ip,:lower16:S - (P + (L1-P) + 8)
|
|
|
|
0x00, 0xc0, 0x40, 0xe3, // movt ip,:upper16:S - (P + (L1-P) + 8)
|
2018-08-31 16:03:33 +08:00
|
|
|
0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
|
|
|
|
0x1c, 0xff, 0x2f, 0xe1, // bx ip
|
2016-07-10 06:52:30 +08:00
|
|
|
};
|
2017-05-17 15:10:59 +08:00
|
|
|
uint64_t s = getARMThunkDestVA(destination);
|
2018-03-30 06:32:13 +08:00
|
|
|
uint64_t p = getThunkTargetSym()->getVA();
|
2019-01-11 00:08:23 +08:00
|
|
|
int64_t offset = s - p - 16;
|
2016-07-10 06:52:30 +08:00
|
|
|
memcpy(buf, data, sizeof(data));
|
2020-01-23 13:39:16 +08:00
|
|
|
target->relocateNoSym(buf, R_ARM_MOVW_PREL_NC, offset);
|
|
|
|
target->relocateNoSym(buf + 4, R_ARM_MOVT_PREL, offset);
|
2016-07-10 06:52:30 +08:00
|
|
|
}
|
|
|
|
|
2017-05-17 15:10:59 +08:00
|
|
|
void ARMV7PILongThunk::addSymbols(ThunkSection &isec) {
|
2018-03-30 06:32:13 +08:00
|
|
|
addSymbol(saver.save("__ARMV7PILongThunk_" + destination.getName()), STT_FUNC,
|
|
|
|
0, isec);
|
|
|
|
addSymbol("$a", STT_NOTYPE, 0, isec);
|
2017-02-01 18:26:03 +08:00
|
|
|
}
|
|
|
|
|
2018-03-30 06:43:52 +08:00
|
|
|
void ThumbV7PILongThunk::writeLong(uint8_t *buf) {
|
2016-07-10 06:52:30 +08:00
|
|
|
const uint8_t data[] = {
|
|
|
|
0x4f, 0xf6, 0xf4, 0x7c, // P: movw ip,:lower16:S - (P + (L1-P) + 4)
|
2017-09-22 05:04:42 +08:00
|
|
|
0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P) + 4)
|
2018-08-31 16:03:33 +08:00
|
|
|
0xfc, 0x44, // L1: add ip, pc
|
|
|
|
0x60, 0x47, // bx ip
|
2016-07-10 06:52:30 +08:00
|
|
|
};
|
2017-05-17 15:10:59 +08:00
|
|
|
uint64_t s = getARMThunkDestVA(destination);
|
2018-03-30 06:32:13 +08:00
|
|
|
uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
|
2019-01-11 00:08:23 +08:00
|
|
|
int64_t offset = s - p - 12;
|
2016-07-10 06:52:30 +08:00
|
|
|
memcpy(buf, data, sizeof(data));
|
2020-01-23 13:39:16 +08:00
|
|
|
target->relocateNoSym(buf, R_ARM_THM_MOVW_PREL_NC, offset);
|
|
|
|
target->relocateNoSym(buf + 4, R_ARM_THM_MOVT_PREL, offset);
|
2016-07-10 06:52:30 +08:00
|
|
|
}
|
2016-07-09 00:10:27 +08:00
|
|
|
|
2017-05-17 15:10:59 +08:00
|
|
|
void ThumbV7PILongThunk::addSymbols(ThunkSection &isec) {
|
2018-03-30 06:32:13 +08:00
|
|
|
addSymbol(saver.save("__ThumbV7PILongThunk_" + destination.getName()),
|
|
|
|
STT_FUNC, 1, isec);
|
|
|
|
addSymbol("$t", STT_NOTYPE, 0, isec);
|
2017-02-01 18:26:03 +08:00
|
|
|
}
|
|
|
|
|
2018-08-20 17:37:50 +08:00
|
|
|
void ARMV5ABSLongThunk::writeLong(uint8_t *buf) {
|
|
|
|
const uint8_t data[] = {
|
|
|
|
0x04, 0xf0, 0x1f, 0xe5, // ldr pc, [pc,#-4] ; L1
|
|
|
|
0x00, 0x00, 0x00, 0x00, // L1: .word S
|
|
|
|
};
|
|
|
|
memcpy(buf, data, sizeof(data));
|
2020-01-23 13:39:16 +08:00
|
|
|
target->relocateNoSym(buf + 4, R_ARM_ABS32, getARMThunkDestVA(destination));
|
2018-08-20 17:37:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ARMV5ABSLongThunk::addSymbols(ThunkSection &isec) {
|
|
|
|
addSymbol(saver.save("__ARMv5ABSLongThunk_" + destination.getName()),
|
|
|
|
STT_FUNC, 0, isec);
|
|
|
|
addSymbol("$a", STT_NOTYPE, 0, isec);
|
|
|
|
addSymbol("$d", STT_NOTYPE, 4, isec);
|
|
|
|
}
|
|
|
|
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
bool ARMV5ABSLongThunk::isCompatibleWith(const InputSection &isec,
|
|
|
|
const Relocation &rel) const {
|
2018-08-20 17:37:50 +08:00
|
|
|
// Thumb branch relocations can't use BLX
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;
|
2018-08-20 17:37:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ARMV5PILongThunk::writeLong(uint8_t *buf) {
|
|
|
|
const uint8_t data[] = {
|
|
|
|
0x04, 0xc0, 0x9f, 0xe5, // P: ldr ip, [pc,#4] ; L2
|
|
|
|
0x0c, 0xc0, 0x8f, 0xe0, // L1: add ip, pc, ip
|
|
|
|
0x1c, 0xff, 0x2f, 0xe1, // bx ip
|
|
|
|
0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 8)
|
|
|
|
};
|
|
|
|
uint64_t s = getARMThunkDestVA(destination);
|
|
|
|
uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
|
|
|
|
memcpy(buf, data, sizeof(data));
|
2020-01-23 13:39:16 +08:00
|
|
|
target->relocateNoSym(buf + 12, R_ARM_REL32, s - p - 12);
|
2018-08-20 17:37:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ARMV5PILongThunk::addSymbols(ThunkSection &isec) {
|
|
|
|
addSymbol(saver.save("__ARMV5PILongThunk_" + destination.getName()), STT_FUNC,
|
|
|
|
0, isec);
|
|
|
|
addSymbol("$a", STT_NOTYPE, 0, isec);
|
|
|
|
addSymbol("$d", STT_NOTYPE, 12, isec);
|
|
|
|
}
|
|
|
|
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
bool ARMV5PILongThunk::isCompatibleWith(const InputSection &isec,
|
|
|
|
const Relocation &rel) const {
|
2018-08-20 17:37:50 +08:00
|
|
|
// Thumb branch relocations can't use BLX
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
return rel.type != R_ARM_THM_JUMP19 && rel.type != R_ARM_THM_JUMP24;
|
2018-08-20 17:37:50 +08:00
|
|
|
}
|
|
|
|
|
2018-12-17 18:33:47 +08:00
|
|
|
void ThumbV6MABSLongThunk::writeLong(uint8_t *buf) {
|
|
|
|
// Most Thumb instructions cannot access the high registers r8 - r15. As the
|
|
|
|
// only register we can corrupt is r12 we must instead spill a low register
|
|
|
|
// to the stack to use as a scratch register. We push r1 even though we
|
|
|
|
// don't need to get some space to use for the return address.
|
|
|
|
const uint8_t data[] = {
|
|
|
|
0x03, 0xb4, // push {r0, r1} ; Obtain scratch registers
|
|
|
|
0x01, 0x48, // ldr r0, [pc, #4] ; L1
|
|
|
|
0x01, 0x90, // str r0, [sp, #4] ; SP + 4 = S
|
|
|
|
0x01, 0xbd, // pop {r0, pc} ; restore r0 and branch to dest
|
|
|
|
0x00, 0x00, 0x00, 0x00 // L1: .word S
|
|
|
|
};
|
|
|
|
uint64_t s = getARMThunkDestVA(destination);
|
|
|
|
memcpy(buf, data, sizeof(data));
|
2020-01-23 13:39:16 +08:00
|
|
|
target->relocateNoSym(buf + 8, R_ARM_ABS32, s);
|
2018-12-17 18:33:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThumbV6MABSLongThunk::addSymbols(ThunkSection &isec) {
|
|
|
|
addSymbol(saver.save("__Thumbv6MABSLongThunk_" + destination.getName()),
|
|
|
|
STT_FUNC, 1, isec);
|
|
|
|
addSymbol("$t", STT_NOTYPE, 0, isec);
|
|
|
|
addSymbol("$d", STT_NOTYPE, 8, isec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThumbV6MPILongThunk::writeLong(uint8_t *buf) {
|
|
|
|
// Most Thumb instructions cannot access the high registers r8 - r15. As the
|
|
|
|
// only register we can corrupt is ip (r12) we must instead spill a low
|
|
|
|
// register to the stack to use as a scratch register.
|
|
|
|
const uint8_t data[] = {
|
|
|
|
0x01, 0xb4, // P: push {r0} ; Obtain scratch register
|
|
|
|
0x02, 0x48, // ldr r0, [pc, #8] ; L2
|
|
|
|
0x84, 0x46, // mov ip, r0 ; high to low register
|
|
|
|
0x01, 0xbc, // pop {r0} ; restore scratch register
|
|
|
|
0xe7, 0x44, // L1: add pc, ip ; transfer control
|
|
|
|
0xc0, 0x46, // nop ; pad to 4-byte boundary
|
|
|
|
0x00, 0x00, 0x00, 0x00, // L2: .word S - (P + (L1 - P) + 4)
|
|
|
|
};
|
|
|
|
uint64_t s = getARMThunkDestVA(destination);
|
|
|
|
uint64_t p = getThunkTargetSym()->getVA() & ~0x1;
|
|
|
|
memcpy(buf, data, sizeof(data));
|
2020-01-23 13:39:16 +08:00
|
|
|
target->relocateNoSym(buf + 12, R_ARM_REL32, s - p - 12);
|
2018-12-17 18:33:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThumbV6MPILongThunk::addSymbols(ThunkSection &isec) {
|
|
|
|
addSymbol(saver.save("__Thumbv6MPILongThunk_" + destination.getName()),
|
|
|
|
STT_FUNC, 1, isec);
|
|
|
|
addSymbol("$t", STT_NOTYPE, 0, isec);
|
|
|
|
addSymbol("$d", STT_NOTYPE, 12, isec);
|
|
|
|
}
|
|
|
|
|
2016-07-10 06:52:32 +08:00
|
|
|
// Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
|
2018-03-29 05:33:31 +08:00
|
|
|
void MipsThunk::writeTo(uint8_t *buf) {
|
2017-07-04 23:04:30 +08:00
|
|
|
uint64_t s = destination.getVA();
|
2018-03-10 02:03:22 +08:00
|
|
|
write32(buf, 0x3c190000); // lui $25, %hi(func)
|
|
|
|
write32(buf + 4, 0x08000000 | (s >> 2)); // j func
|
|
|
|
write32(buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
|
|
|
|
write32(buf + 12, 0x00000000); // nop
|
2020-01-23 13:39:16 +08:00
|
|
|
target->relocateNoSym(buf, R_MIPS_HI16, s);
|
|
|
|
target->relocateNoSym(buf + 8, R_MIPS_LO16, s);
|
2016-07-09 00:10:27 +08:00
|
|
|
}
|
2016-07-10 06:52:30 +08:00
|
|
|
|
2017-05-17 15:10:59 +08:00
|
|
|
void MipsThunk::addSymbols(ThunkSection &isec) {
|
2018-03-30 06:32:13 +08:00
|
|
|
addSymbol(saver.save("__LA25Thunk_" + destination.getName()), STT_FUNC, 0,
|
|
|
|
isec);
|
2017-02-01 18:26:03 +08:00
|
|
|
}
|
2016-07-09 00:10:27 +08:00
|
|
|
|
2017-05-17 15:10:59 +08:00
|
|
|
InputSection *MipsThunk::getTargetInputSection() const {
|
2017-11-30 02:32:57 +08:00
|
|
|
auto &dr = cast<Defined>(destination);
|
|
|
|
return dyn_cast<InputSection>(dr.section);
|
2017-01-28 08:48:06 +08:00
|
|
|
}
|
2017-01-27 21:10:16 +08:00
|
|
|
|
2017-10-03 21:30:02 +08:00
|
|
|
// Write microMIPS R2-R5 LA25 thunk code
|
|
|
|
// to call PIC function from the non-PIC one.
|
2018-03-29 05:33:31 +08:00
|
|
|
void MicroMipsThunk::writeTo(uint8_t *buf) {
|
2019-03-14 00:00:35 +08:00
|
|
|
uint64_t s = destination.getVA();
|
2018-03-10 02:03:22 +08:00
|
|
|
write16(buf, 0x41b9); // lui $25, %hi(func)
|
|
|
|
write16(buf + 4, 0xd400); // j func
|
|
|
|
write16(buf + 8, 0x3339); // addiu $25, $25, %lo(func)
|
|
|
|
write16(buf + 12, 0x0c00); // nop
|
2020-01-23 13:39:16 +08:00
|
|
|
target->relocateNoSym(buf, R_MICROMIPS_HI16, s);
|
|
|
|
target->relocateNoSym(buf + 4, R_MICROMIPS_26_S1, s);
|
|
|
|
target->relocateNoSym(buf + 8, R_MICROMIPS_LO16, s);
|
2017-10-03 21:30:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MicroMipsThunk::addSymbols(ThunkSection &isec) {
|
2018-03-30 06:32:13 +08:00
|
|
|
Defined *d = addSymbol(
|
|
|
|
saver.save("__microLA25Thunk_" + destination.getName()), STT_FUNC, 0, isec);
|
|
|
|
d->stOther |= STO_MIPS_MICROMIPS;
|
2017-10-03 21:30:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
InputSection *MicroMipsThunk::getTargetInputSection() const {
|
2017-11-30 02:43:34 +08:00
|
|
|
auto &dr = cast<Defined>(destination);
|
|
|
|
return dyn_cast<InputSection>(dr.section);
|
2017-10-03 21:30:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write microMIPS R6 LA25 thunk code
|
|
|
|
// to call PIC function from the non-PIC one.
|
2018-03-29 05:33:31 +08:00
|
|
|
void MicroMipsR6Thunk::writeTo(uint8_t *buf) {
|
2019-03-14 00:00:35 +08:00
|
|
|
uint64_t s = destination.getVA();
|
2018-03-30 06:32:13 +08:00
|
|
|
uint64_t p = getThunkTargetSym()->getVA();
|
2018-03-10 02:03:22 +08:00
|
|
|
write16(buf, 0x1320); // lui $25, %hi(func)
|
|
|
|
write16(buf + 4, 0x3339); // addiu $25, $25, %lo(func)
|
|
|
|
write16(buf + 8, 0x9400); // bc func
|
2020-01-23 13:39:16 +08:00
|
|
|
target->relocateNoSym(buf, R_MICROMIPS_HI16, s);
|
|
|
|
target->relocateNoSym(buf + 4, R_MICROMIPS_LO16, s);
|
|
|
|
target->relocateNoSym(buf + 8, R_MICROMIPS_PC26_S1, s - p - 12);
|
2017-10-03 21:30:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MicroMipsR6Thunk::addSymbols(ThunkSection &isec) {
|
2018-03-30 06:32:13 +08:00
|
|
|
Defined *d = addSymbol(
|
|
|
|
saver.save("__microLA25Thunk_" + destination.getName()), STT_FUNC, 0, isec);
|
|
|
|
d->stOther |= STO_MIPS_MICROMIPS;
|
2017-10-03 21:30:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
InputSection *MicroMipsR6Thunk::getTargetInputSection() const {
|
2017-11-30 05:29:52 +08:00
|
|
|
auto &dr = cast<Defined>(destination);
|
|
|
|
return dyn_cast<InputSection>(dr.section);
|
2017-10-03 21:30:02 +08:00
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
void elf::writePPC32PltCallStub(uint8_t *buf, uint64_t gotPltVA,
|
|
|
|
const InputFile *file, int64_t addend) {
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
if (!config->isPic) {
|
2019-12-18 03:23:37 +08:00
|
|
|
write32(buf + 0, 0x3d600000 | (gotPltVA + 0x8000) >> 16); // lis r11,ha
|
|
|
|
write32(buf + 4, 0x816b0000 | (uint16_t)gotPltVA); // lwz r11,l(r11)
|
|
|
|
write32(buf + 8, 0x7d6903a6); // mtctr r11
|
|
|
|
write32(buf + 12, 0x4e800420); // bctr
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint32_t offset;
|
|
|
|
if (addend >= 0x8000) {
|
|
|
|
// The stub loads an address relative to r30 (.got2+Addend). Addend is
|
|
|
|
// almost always 0x8000. The address of .got2 is different in another object
|
|
|
|
// file, so a stub cannot be shared.
|
2019-12-18 03:23:37 +08:00
|
|
|
offset = gotPltVA - (in.ppc32Got2->getParent()->getVA() +
|
|
|
|
file->ppc32Got2OutSecOff + addend);
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
} else {
|
|
|
|
// The stub loads an address relative to _GLOBAL_OFFSET_TABLE_ (which is
|
|
|
|
// currently the address of .got).
|
2019-12-18 03:23:37 +08:00
|
|
|
offset = gotPltVA - in.got->getVA();
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
}
|
|
|
|
uint16_t ha = (offset + 0x8000) >> 16, l = (uint16_t)offset;
|
|
|
|
if (ha == 0) {
|
|
|
|
write32(buf + 0, 0x817e0000 | l); // lwz r11,l(r30)
|
|
|
|
write32(buf + 4, 0x7d6903a6); // mtctr r11
|
|
|
|
write32(buf + 8, 0x4e800420); // bctr
|
|
|
|
write32(buf + 12, 0x60000000); // nop
|
|
|
|
} else {
|
|
|
|
write32(buf + 0, 0x3d7e0000 | ha); // addis r11,r30,ha
|
|
|
|
write32(buf + 4, 0x816b0000 | l); // lwz r11,l(r11)
|
|
|
|
write32(buf + 8, 0x7d6903a6); // mtctr r11
|
|
|
|
write32(buf + 12, 0x4e800420); // bctr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-18 03:23:37 +08:00
|
|
|
void PPC32PltCallStub::writeTo(uint8_t *buf) {
|
|
|
|
writePPC32PltCallStub(buf, destination.getGotPltVA(), file, addend);
|
|
|
|
}
|
|
|
|
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
void PPC32PltCallStub::addSymbols(ThunkSection &isec) {
|
|
|
|
std::string buf;
|
|
|
|
raw_string_ostream os(buf);
|
|
|
|
os << format_hex_no_prefix(addend, 8);
|
|
|
|
if (!config->isPic)
|
|
|
|
os << ".plt_call32.";
|
|
|
|
else if (addend >= 0x8000)
|
|
|
|
os << ".got2.plt_pic32.";
|
|
|
|
else
|
|
|
|
os << ".plt_pic32.";
|
|
|
|
os << destination.getName();
|
|
|
|
addSymbol(saver.save(os.str()), STT_FUNC, 0, isec);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PPC32PltCallStub::isCompatibleWith(const InputSection &isec,
|
|
|
|
const Relocation &rel) const {
|
|
|
|
return !config->isPic || (isec.file == file && rel.addend == addend);
|
|
|
|
}
|
|
|
|
|
2020-01-26 10:58:54 +08:00
|
|
|
void PPC32LongThunk::addSymbols(ThunkSection &isec) {
|
|
|
|
addSymbol(saver.save("__LongThunk_" + destination.getName()), STT_FUNC, 0,
|
|
|
|
isec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PPC32LongThunk::writeTo(uint8_t *buf) {
|
|
|
|
auto ha = [](uint32_t v) -> uint16_t { return (v + 0x8000) >> 16; };
|
|
|
|
auto lo = [](uint32_t v) -> uint16_t { return v; };
|
|
|
|
uint32_t d = destination.getVA(addend);
|
|
|
|
if (config->isPic) {
|
|
|
|
uint32_t off = d - (getThunkTargetSym()->getVA() + 8);
|
|
|
|
write32(buf + 0, 0x7c0802a6); // mflr r12,0
|
|
|
|
write32(buf + 4, 0x429f0005); // bcl r20,r31,.+4
|
|
|
|
write32(buf + 8, 0x7d8802a6); // mtctr r12
|
|
|
|
write32(buf + 12, 0x3d8c0000 | ha(off)); // addis r12,r12,off@ha
|
|
|
|
write32(buf + 16, 0x398c0000 | lo(off)); // addi r12,r12,off@l
|
|
|
|
write32(buf + 20, 0x7c0803a6); // mtlr r0
|
|
|
|
buf += 24;
|
|
|
|
} else {
|
|
|
|
write32(buf + 0, 0x3d800000 | ha(d)); // lis r12,d@ha
|
|
|
|
write32(buf + 4, 0x398c0000 | lo(d)); // addi r12,r12,d@l
|
|
|
|
buf += 8;
|
|
|
|
}
|
|
|
|
write32(buf + 0, 0x7d8903a6); // mtctr r12
|
|
|
|
write32(buf + 4, 0x4e800420); // bctr
|
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
void elf::writePPC64LoadAndBranch(uint8_t *buf, int64_t offset) {
|
2018-11-15 01:56:43 +08:00
|
|
|
uint16_t offHa = (offset + 0x8000) >> 16;
|
|
|
|
uint16_t offLo = offset & 0xffff;
|
2018-05-07 03:13:29 +08:00
|
|
|
|
2018-11-15 01:56:43 +08:00
|
|
|
write32(buf + 0, 0x3d820000 | offHa); // addis r12, r2, OffHa
|
|
|
|
write32(buf + 4, 0xe98c0000 | offLo); // ld r12, OffLo(r12)
|
|
|
|
write32(buf + 8, 0x7d8903a6); // mtctr r12
|
|
|
|
write32(buf + 12, 0x4e800420); // bctr
|
|
|
|
}
|
|
|
|
|
|
|
|
void PPC64PltCallStub::writeTo(uint8_t *buf) {
|
|
|
|
int64_t offset = destination.getGotPltVA() - getPPC64TocBase();
|
|
|
|
// Save the TOC pointer to the save-slot reserved in the call frame.
|
|
|
|
write32(buf + 0, 0xf8410018); // std r2,24(r1)
|
[ELF][PPC64] Implement IPLT code sequence for non-preemptible IFUNC
Non-preemptible IFUNC are placed in in.iplt (.glink on EM_PPC64). If
there is a non-GOT non-PLT relocation, for pointer equality, we change
the type of the symbol from STT_IFUNC and STT_FUNC and bind it to the
.glink entry.
On EM_386, EM_X86_64, EM_ARM, and EM_AARCH64, the PLT code sequence
loads the address from its associated .got.plt slot. An IPLT also has an
associated .got.plt slot and can use the same code sequence.
On EM_PPC64, the PLT code sequence is actually a bl instruction in
.glink . It jumps to `__glink_PLTresolve` (the PLT header). and
`__glink_PLTresolve` computes the .plt slot (relocated by
R_PPC64_JUMP_SLOT).
An IPLT does not have an associated R_PPC64_JUMP_SLOT, so we cannot use
`bl` in .iplt . Instead, create a call stub which has a similar code
sequence as PPC64PltCallStub. We don't save the TOC pointer, so such
scenarios will not work: a function pointer to a non-preemptible ifunc,
which resolves to a function defined in another DSO. This is the
restriction described by https://sourceware.org/glibc/wiki/GNU_IFUNC
(though on many architectures it works in practice):
Requirement (a): Resolver must be defined in the same translation unit as the implementations.
If an ifunc is taken address but not called, technically we don't need
an entry for it, but we currently do that.
This patch makes
// clang -fuse-ld=lld -fno-pie -no-pie a.c
// clang -fuse-ld=lld -fPIE -pie a.c
#include <stdio.h>
static void impl(void) { puts("meow"); }
void thefunc(void) __attribute__((ifunc("resolver")));
void *resolver(void) { return &impl; }
int main(void) {
thefunc();
void (*theptr)(void) = &thefunc;
theptr();
}
work on Linux glibc and FreeBSD. Calling a function pointer pointing to
a Non-preemptible IFUNC never worked before.
Differential Revision: https://reviews.llvm.org/D71509
2019-12-14 10:30:21 +08:00
|
|
|
writePPC64LoadAndBranch(buf + 4, offset);
|
2018-05-07 03:13:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PPC64PltCallStub::addSymbols(ThunkSection &isec) {
|
|
|
|
Defined *s = addSymbol(saver.save("__plt_" + destination.getName()), STT_FUNC,
|
|
|
|
0, isec);
|
|
|
|
s->needsTocRestore = true;
|
2019-12-18 08:45:04 +08:00
|
|
|
s->file = destination.file;
|
2018-05-07 03:13:29 +08:00
|
|
|
}
|
|
|
|
|
2018-11-15 01:56:43 +08:00
|
|
|
void PPC64LongBranchThunk::writeTo(uint8_t *buf) {
|
2019-12-03 06:20:42 +08:00
|
|
|
int64_t offset = in.ppc64LongBranchTarget->getEntryVA(&destination, addend) -
|
|
|
|
getPPC64TocBase();
|
[ELF][PPC64] Implement IPLT code sequence for non-preemptible IFUNC
Non-preemptible IFUNC are placed in in.iplt (.glink on EM_PPC64). If
there is a non-GOT non-PLT relocation, for pointer equality, we change
the type of the symbol from STT_IFUNC and STT_FUNC and bind it to the
.glink entry.
On EM_386, EM_X86_64, EM_ARM, and EM_AARCH64, the PLT code sequence
loads the address from its associated .got.plt slot. An IPLT also has an
associated .got.plt slot and can use the same code sequence.
On EM_PPC64, the PLT code sequence is actually a bl instruction in
.glink . It jumps to `__glink_PLTresolve` (the PLT header). and
`__glink_PLTresolve` computes the .plt slot (relocated by
R_PPC64_JUMP_SLOT).
An IPLT does not have an associated R_PPC64_JUMP_SLOT, so we cannot use
`bl` in .iplt . Instead, create a call stub which has a similar code
sequence as PPC64PltCallStub. We don't save the TOC pointer, so such
scenarios will not work: a function pointer to a non-preemptible ifunc,
which resolves to a function defined in another DSO. This is the
restriction described by https://sourceware.org/glibc/wiki/GNU_IFUNC
(though on many architectures it works in practice):
Requirement (a): Resolver must be defined in the same translation unit as the implementations.
If an ifunc is taken address but not called, technically we don't need
an entry for it, but we currently do that.
This patch makes
// clang -fuse-ld=lld -fno-pie -no-pie a.c
// clang -fuse-ld=lld -fPIE -pie a.c
#include <stdio.h>
static void impl(void) { puts("meow"); }
void thefunc(void) __attribute__((ifunc("resolver")));
void *resolver(void) { return &impl; }
int main(void) {
thefunc();
void (*theptr)(void) = &thefunc;
theptr();
}
work on Linux glibc and FreeBSD. Calling a function pointer pointing to
a Non-preemptible IFUNC never worked before.
Differential Revision: https://reviews.llvm.org/D71509
2019-12-14 10:30:21 +08:00
|
|
|
writePPC64LoadAndBranch(buf, offset);
|
2018-11-15 01:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PPC64LongBranchThunk::addSymbols(ThunkSection &isec) {
|
|
|
|
addSymbol(saver.save("__long_branch_" + destination.getName()), STT_FUNC, 0,
|
|
|
|
isec);
|
|
|
|
}
|
|
|
|
|
2019-11-23 16:57:54 +08:00
|
|
|
Thunk::Thunk(Symbol &d, int64_t a) : destination(d), addend(a), offset(0) {}
|
2017-02-01 18:26:03 +08:00
|
|
|
|
2017-03-16 18:40:50 +08:00
|
|
|
Thunk::~Thunk() = default;
|
2016-07-10 06:52:30 +08:00
|
|
|
|
2019-11-23 16:57:54 +08:00
|
|
|
static Thunk *addThunkAArch64(RelType type, Symbol &s, int64_t a) {
|
2020-06-24 07:10:07 +08:00
|
|
|
if (type != R_AARCH64_CALL26 && type != R_AARCH64_JUMP26 &&
|
|
|
|
type != R_AARCH64_PLT32)
|
2017-11-29 19:15:12 +08:00
|
|
|
fatal("unrecognized relocation type");
|
2019-01-16 20:09:13 +08:00
|
|
|
if (config->picThunk)
|
2019-11-23 16:57:54 +08:00
|
|
|
return make<AArch64ADRPThunk>(s, a);
|
|
|
|
return make<AArch64ABSLongThunk>(s, a);
|
2017-11-29 19:15:12 +08:00
|
|
|
}
|
|
|
|
|
2016-07-10 06:03:51 +08:00
|
|
|
// Creates a thunk for Thumb-ARM interworking.
|
2018-08-20 17:37:50 +08:00
|
|
|
// Arm Architectures v5 and v6 do not support Thumb2 technology. This means
|
|
|
|
// - MOVT and MOVW instructions cannot be used
|
|
|
|
// - Only Thumb relocation that can generate a Thunk is a BL, this can always
|
|
|
|
// be transformed into a BLX
|
|
|
|
static Thunk *addThunkPreArmv7(RelType reloc, Symbol &s) {
|
|
|
|
switch (reloc) {
|
|
|
|
case R_ARM_PC24:
|
|
|
|
case R_ARM_PLT32:
|
|
|
|
case R_ARM_JUMP24:
|
|
|
|
case R_ARM_CALL:
|
|
|
|
case R_ARM_THM_CALL:
|
2019-01-16 20:09:13 +08:00
|
|
|
if (config->picThunk)
|
2018-08-20 17:37:50 +08:00
|
|
|
return make<ARMV5PILongThunk>(s);
|
|
|
|
return make<ARMV5ABSLongThunk>(s);
|
|
|
|
}
|
|
|
|
fatal("relocation " + toString(reloc) + " to " + toString(s) +
|
|
|
|
" not supported for Armv5 or Armv6 targets");
|
|
|
|
}
|
|
|
|
|
2018-12-17 18:33:47 +08:00
|
|
|
// Create a thunk for Thumb long branch on V6-M.
|
|
|
|
// Arm Architecture v6-M only supports Thumb instructions. This means
|
|
|
|
// - MOVT and MOVW instructions cannot be used.
|
|
|
|
// - Only a limited number of instructions can access registers r8 and above
|
|
|
|
// - No interworking support is needed (all Thumb).
|
|
|
|
static Thunk *addThunkV6M(RelType reloc, Symbol &s) {
|
|
|
|
switch (reloc) {
|
|
|
|
case R_ARM_THM_JUMP19:
|
|
|
|
case R_ARM_THM_JUMP24:
|
|
|
|
case R_ARM_THM_CALL:
|
|
|
|
if (config->isPic)
|
|
|
|
return make<ThumbV6MPILongThunk>(s);
|
|
|
|
return make<ThumbV6MABSLongThunk>(s);
|
|
|
|
}
|
|
|
|
fatal("relocation " + toString(reloc) + " to " + toString(s) +
|
|
|
|
" not supported for Armv6-M targets");
|
|
|
|
}
|
|
|
|
|
2018-08-20 17:37:50 +08:00
|
|
|
// Creates a thunk for Thumb-ARM interworking or branch range extension.
|
2017-11-04 05:21:47 +08:00
|
|
|
static Thunk *addThunkArm(RelType reloc, Symbol &s) {
|
2018-08-20 17:37:50 +08:00
|
|
|
// Decide which Thunk is needed based on:
|
|
|
|
// Available instruction set
|
|
|
|
// - An Arm Thunk can only be used if Arm state is available.
|
|
|
|
// - A Thumb Thunk can only be used if Thumb state is available.
|
|
|
|
// - Can only use a Thunk if it uses instructions that the Target supports.
|
|
|
|
// Relocation is branch or branch and link
|
|
|
|
// - Branch instructions cannot change state, can only select Thunk that
|
|
|
|
// starts in the same state as the caller.
|
|
|
|
// - Branch and link relocations can change state, can select Thunks from
|
|
|
|
// either Arm or Thumb.
|
|
|
|
// Position independent Thunks if we require position independent code.
|
|
|
|
|
2018-12-17 18:33:47 +08:00
|
|
|
// Handle architectures that have restrictions on the instructions that they
|
|
|
|
// can use in Thunks. The flags below are set by reading the BuildAttributes
|
|
|
|
// of the input objects. InputFiles.cpp contains the mapping from ARM
|
|
|
|
// architecture to flag.
|
2018-08-20 17:37:50 +08:00
|
|
|
if (!config->armHasMovtMovw) {
|
|
|
|
if (!config->armJ1J2BranchEncoding)
|
|
|
|
return addThunkPreArmv7(reloc, s);
|
2018-12-17 18:33:47 +08:00
|
|
|
return addThunkV6M(reloc, s);
|
2018-08-20 17:37:50 +08:00
|
|
|
}
|
|
|
|
|
2016-07-10 06:03:51 +08:00
|
|
|
switch (reloc) {
|
2016-07-09 00:10:27 +08:00
|
|
|
case R_ARM_PC24:
|
|
|
|
case R_ARM_PLT32:
|
|
|
|
case R_ARM_JUMP24:
|
2017-10-27 17:04:11 +08:00
|
|
|
case R_ARM_CALL:
|
2019-01-16 20:09:13 +08:00
|
|
|
if (config->picThunk)
|
2017-05-17 15:10:59 +08:00
|
|
|
return make<ARMV7PILongThunk>(s);
|
|
|
|
return make<ARMV7ABSLongThunk>(s);
|
2016-07-09 00:10:27 +08:00
|
|
|
case R_ARM_THM_JUMP19:
|
|
|
|
case R_ARM_THM_JUMP24:
|
2017-10-27 17:04:11 +08:00
|
|
|
case R_ARM_THM_CALL:
|
2019-01-16 20:09:13 +08:00
|
|
|
if (config->picThunk)
|
2017-05-17 15:10:59 +08:00
|
|
|
return make<ThumbV7PILongThunk>(s);
|
|
|
|
return make<ThumbV7ABSLongThunk>(s);
|
2016-07-09 00:10:27 +08:00
|
|
|
}
|
2016-07-10 06:03:51 +08:00
|
|
|
fatal("unrecognized relocation type");
|
|
|
|
}
|
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
static Thunk *addThunkMips(RelType type, Symbol &s) {
|
2017-10-03 21:30:02 +08:00
|
|
|
if ((s.stOther & STO_MIPS_MICROMIPS) && isMipsR6())
|
|
|
|
return make<MicroMipsR6Thunk>(s);
|
|
|
|
if (s.stOther & STO_MIPS_MICROMIPS)
|
|
|
|
return make<MicroMipsThunk>(s);
|
|
|
|
return make<MipsThunk>(s);
|
|
|
|
}
|
2016-07-09 00:10:27 +08:00
|
|
|
|
2019-11-23 16:57:54 +08:00
|
|
|
static Thunk *addThunkPPC32(const InputSection &isec, const Relocation &rel,
|
|
|
|
Symbol &s) {
|
2020-01-26 10:58:54 +08:00
|
|
|
assert((rel.type == R_PPC_LOCAL24PC || rel.type == R_PPC_REL24 ||
|
|
|
|
rel.type == R_PPC_PLTREL24) &&
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
"unexpected relocation type for thunk");
|
2020-01-26 10:58:54 +08:00
|
|
|
if (s.isInPlt())
|
|
|
|
return make<PPC32PltCallStub>(isec, rel, s);
|
|
|
|
return make<PPC32LongThunk>(s, rel.addend);
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
}
|
|
|
|
|
2019-12-03 06:20:42 +08:00
|
|
|
static Thunk *addThunkPPC64(RelType type, Symbol &s, int64_t a) {
|
2020-03-27 12:20:13 +08:00
|
|
|
assert((type == R_PPC64_REL14 || type == R_PPC64_REL24) &&
|
|
|
|
"unexpected relocation type for thunk");
|
2018-11-15 01:56:43 +08:00
|
|
|
if (s.isInPlt())
|
2018-05-07 03:13:29 +08:00
|
|
|
return make<PPC64PltCallStub>(s);
|
2018-11-15 01:56:43 +08:00
|
|
|
|
2019-01-16 20:09:13 +08:00
|
|
|
if (config->picThunk)
|
2019-12-03 06:20:42 +08:00
|
|
|
return make<PPC64PILongBranchThunk>(s, a);
|
2018-11-15 01:56:43 +08:00
|
|
|
|
2019-12-03 06:20:42 +08:00
|
|
|
return make<PPC64PDLongBranchThunk>(s, a);
|
2018-05-07 03:13:29 +08:00
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
Thunk *elf::addThunk(const InputSection &isec, Relocation &rel) {
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
Symbol &s = *rel.sym;
|
2019-11-23 16:57:54 +08:00
|
|
|
int64_t a = rel.addend;
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
|
2017-11-29 19:15:12 +08:00
|
|
|
if (config->emachine == EM_AARCH64)
|
2019-11-23 16:57:54 +08:00
|
|
|
return addThunkAArch64(rel.type, s, a);
|
2018-05-07 03:13:29 +08:00
|
|
|
|
|
|
|
if (config->emachine == EM_ARM)
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
return addThunkArm(rel.type, s);
|
2018-05-07 03:13:29 +08:00
|
|
|
|
|
|
|
if (config->emachine == EM_MIPS)
|
[PPC32] Improve the 32-bit PowerPC port
Many -static/-no-pie/-shared/-pie applications linked against glibc or musl
should work with this patch. This also helps FreeBSD PowerPC64 to migrate
their lib32 (PR40888).
* Fix default image base and max page size.
* Support new-style Secure PLT (see below). Old-style BSS PLT is not
implemented, so it is not suitable for FreeBSD rtld now because it doesn't
support Secure PLT yet.
* Support more initial relocation types:
R_PPC_ADDR32, R_PPC_REL16*, R_PPC_LOCAL24PC, R_PPC_PLTREL24, and R_PPC_GOT16.
The addend of R_PPC_PLTREL24 is special: it decides the call stub PLT type
but it should be ignored for the computation of target symbol VA.
* Support GNU ifunc
* Support .glink used for lazy PLT resolution in glibc
* Add a new thunk type: PPC32PltCallStub that is similar to PPC64PltCallStub.
It is used by R_PPC_REL24 and R_PPC_PLTREL24.
A PLT stub used in -fPIE/-fPIC usually loads an address relative to
.got2+0x8000 (-fpie/-fpic code uses _GLOBAL_OFFSET_TABLE_ relative
addresses).
Two .got2 sections in two object files have different addresses, thus a PLT stub
can't be shared by two object files. To handle this incompatibility,
change the parameters of Thunk::isCompatibleWith to
`const InputSection &, const Relocation &`.
PowerPC psABI specified an old-style .plt (BSS PLT) that is both
writable and executable. Linkers don't make separate RW- and RWE segments,
which causes all initially writable memory (think .data) executable.
This is a big security concern so a new PLT scheme (secure PLT) was developed to
address the security issue.
TLS will be implemented in D62940.
glibc older than ~2012 requires .rela.dyn to include .rela.plt, it can
not handle the DT_RELA+DT_RELASZ == DT_JMPREL case correctly. A hack
(not included in this patch) in LinkerScript.cpp addOrphanSections() to
work around the issue:
if (Config->EMachine == EM_PPC) {
// Older glibc assumes .rela.dyn includes .rela.plt
Add(In.RelaDyn);
if (In.RelaPlt->isLive() && !In.RelaPlt->Parent)
In.RelaDyn->getParent()->addSection(In.RelaPlt);
}
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D62464
llvm-svn: 362721
2019-06-07 01:03:00 +08:00
|
|
|
return addThunkMips(rel.type, s);
|
|
|
|
|
|
|
|
if (config->emachine == EM_PPC)
|
|
|
|
return addThunkPPC32(isec, rel, s);
|
2018-05-07 03:13:29 +08:00
|
|
|
|
|
|
|
if (config->emachine == EM_PPC64)
|
2019-12-03 06:20:42 +08:00
|
|
|
return addThunkPPC64(rel.type, s, a);
|
2018-05-07 03:13:29 +08:00
|
|
|
|
|
|
|
llvm_unreachable("add Thunk only supported for ARM, Mips and PowerPC");
|
2016-07-09 00:10:27 +08:00
|
|
|
}
|