2016-05-25 04:24:43 +08:00
|
|
|
//===- Relocations.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-05-25 04:24:43 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2016-06-08 20:29:29 +08:00
|
|
|
// This file contains platform-independent functions to process relocations.
|
2016-05-25 04:24:43 +08:00
|
|
|
// I'll describe the overview of this file here.
|
|
|
|
//
|
|
|
|
// Simple relocations are easy to handle for the linker. For example,
|
|
|
|
// for R_X86_64_PC64 relocs, the linker just has to fix up locations
|
|
|
|
// with the relative offsets to the target symbols. It would just be
|
|
|
|
// reading records from relocation sections and applying them to output.
|
|
|
|
//
|
|
|
|
// But not all relocations are that easy to handle. For example, for
|
|
|
|
// R_386_GOTOFF relocs, the linker has to create new GOT entries for
|
|
|
|
// symbols if they don't exist, and fix up locations with GOT entry
|
|
|
|
// offsets from the beginning of GOT section. So there is more than
|
|
|
|
// fixing addresses in relocation processing.
|
|
|
|
//
|
|
|
|
// ELF defines a large number of complex relocations.
|
|
|
|
//
|
|
|
|
// The functions in this file analyze relocations and do whatever needs
|
|
|
|
// to be done. It includes, but not limited to, the following.
|
|
|
|
//
|
|
|
|
// - create GOT/PLT entries
|
|
|
|
// - create new relocations in .dynsym to let the dynamic linker resolve
|
|
|
|
// them at runtime (since ELF supports dynamic linking, not all
|
|
|
|
// relocations can be resolved at link-time)
|
|
|
|
// - create COPY relocs and reserve space in .bss
|
|
|
|
// - replace expensive relocs (in terms of runtime cost) with cheap ones
|
|
|
|
// - error out infeasible combinations such as PIC and non-relative relocs
|
|
|
|
//
|
|
|
|
// Note that the functions in this file don't actually apply relocations
|
|
|
|
// because it doesn't know about the output file nor the output file buffer.
|
|
|
|
// It instead stores Relocation objects to InputSection's Relocations
|
|
|
|
// vector to let it apply later in InputSection::writeTo.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Relocations.h"
|
|
|
|
#include "Config.h"
|
2022-02-24 12:44:34 +08:00
|
|
|
#include "InputFiles.h"
|
2017-06-07 17:35:14 +08:00
|
|
|
#include "LinkerScript.h"
|
2016-05-25 04:24:43 +08:00
|
|
|
#include "OutputSections.h"
|
|
|
|
#include "SymbolTable.h"
|
2017-12-10 00:56:18 +08:00
|
|
|
#include "Symbols.h"
|
2016-11-10 17:48:29 +08:00
|
|
|
#include "SyntheticSections.h"
|
2016-05-25 04:24:43 +08:00
|
|
|
#include "Target.h"
|
2016-07-09 00:10:27 +08:00
|
|
|
#include "Thunks.h"
|
Introduce a flag to warn when ifunc symbols are used with text relocations.
Summary:
This patch adds a new flag, --warn-ifunc-textrel, to work around a glibc bug. When a code with ifunc symbols is used to produce an object file with text relocations, lld always succeeds. However, if that object file is linked using an old version of glibc, the resultant binary just crashes with segmentation fault when it is run (The bug is going to be corrected as of glibc 2.19).
Since there is no way to tell beforehand what library the object file will be linked against in the future, there does not seem to be a fool-proof way for lld to give an error only in cases where the binary will crash. So, with this change (dated 2018-09-25), lld starts to give a warning, contingent on a new command line flag that does not have a gnu counter part. The default value for --warn-ifunc-textrel is false, so lld behaviour will not change unless the user explicitly asks lld to give a warning. Users that link with a glibc library with version 2.19 or newer, or does not use ifunc symbols, or does not generate object files with text relocations do not need to take any action. Other users may consider to start passing warn-ifunc-textrel to lld to get early warnings.
Reviewers: ruiu, espindola
Reviewed By: ruiu
Subscribers: grimar, MaskRay, markj, emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D52430
llvm-svn: 343628
2018-10-03 04:30:22 +08:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2017-11-29 04:39:17 +08:00
|
|
|
#include "lld/Common/Memory.h"
|
2018-06-12 03:42:57 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2019-10-30 06:28:19 +08:00
|
|
|
#include "llvm/Demangle/Demangle.h"
|
2016-05-25 04:24:43 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
2017-02-01 18:26:03 +08:00
|
|
|
#include <algorithm>
|
2016-05-25 04:24:43 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::ELF;
|
|
|
|
using namespace llvm::object;
|
|
|
|
using namespace llvm::support::endian;
|
2020-05-15 13:18:58 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
2016-05-25 04:24:43 +08:00
|
|
|
|
2018-12-19 18:19:40 +08:00
|
|
|
static Optional<std::string> getLinkerScriptLocation(const Symbol &sym) {
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *cmd : script->sectionCommands)
|
|
|
|
if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
|
|
|
|
if (assign->sym == &sym)
|
|
|
|
return assign->location;
|
2018-12-19 18:19:40 +08:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-01-28 09:35:06 +08:00
|
|
|
static std::string getDefinedLocation(const Symbol &sym) {
|
2020-12-31 01:16:26 +08:00
|
|
|
const char msg[] = "\n>>> defined in ";
|
2020-01-28 09:35:06 +08:00
|
|
|
if (sym.file)
|
2020-12-31 01:16:26 +08:00
|
|
|
return msg + toString(sym.file);
|
|
|
|
if (Optional<std::string> loc = getLinkerScriptLocation(sym))
|
|
|
|
return msg + *loc;
|
|
|
|
return "";
|
2020-01-28 09:35:06 +08:00
|
|
|
}
|
|
|
|
|
2017-04-04 05:36:31 +08:00
|
|
|
// Construct a message in the following format.
|
|
|
|
//
|
|
|
|
// >>> defined in /home/alice/src/foo.o
|
|
|
|
// >>> referenced by bar.c:12 (/home/alice/src/bar.c:12)
|
|
|
|
// >>> /home/alice/src/bar.o:(.text+0x1)
|
2017-11-04 05:21:47 +08:00
|
|
|
static std::string getLocation(InputSectionBase &s, const Symbol &sym,
|
2017-04-04 05:36:31 +08:00
|
|
|
uint64_t off) {
|
2020-01-28 09:35:06 +08:00
|
|
|
std::string msg = getDefinedLocation(sym) + "\n>>> referenced by ";
|
2017-12-24 01:21:39 +08:00
|
|
|
std::string src = s.getSrcMsg(sym, off);
|
2017-04-04 05:36:31 +08:00
|
|
|
if (!src.empty())
|
|
|
|
msg += src + "\n>>> ";
|
2017-10-27 11:13:54 +08:00
|
|
|
return msg + s.getObjMsg(off);
|
2017-04-04 05:36:31 +08:00
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
void elf::reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v,
|
|
|
|
int64_t min, uint64_t max) {
|
2020-01-28 09:35:06 +08:00
|
|
|
ErrorPlace errPlace = getErrorPlace(loc);
|
|
|
|
std::string hint;
|
2021-12-26 15:46:47 +08:00
|
|
|
if (rel.sym && !rel.sym->isSection())
|
2021-10-29 00:38:45 +08:00
|
|
|
hint = "; references " + lld::toString(*rel.sym);
|
|
|
|
if (!errPlace.srcLoc.empty())
|
|
|
|
hint += "\n>>> referenced by " + errPlace.srcLoc;
|
2021-12-26 15:46:47 +08:00
|
|
|
if (rel.sym && !rel.sym->isSection())
|
2021-10-29 00:38:45 +08:00
|
|
|
hint += getDefinedLocation(*rel.sym);
|
2020-01-28 09:35:06 +08:00
|
|
|
|
|
|
|
if (errPlace.isec && errPlace.isec->name.startswith(".debug"))
|
|
|
|
hint += "; consider recompiling with -fdebug-types-section to reduce size "
|
|
|
|
"of debug sections";
|
|
|
|
|
|
|
|
errorOrWarn(errPlace.loc + "relocation " + lld::toString(rel.type) +
|
|
|
|
" out of range: " + v.str() + " is not in [" + Twine(min).str() +
|
|
|
|
", " + Twine(max).str() + "]" + hint);
|
|
|
|
}
|
|
|
|
|
2020-09-12 00:31:37 +08:00
|
|
|
void elf::reportRangeError(uint8_t *loc, int64_t v, int n, const Symbol &sym,
|
|
|
|
const Twine &msg) {
|
|
|
|
ErrorPlace errPlace = getErrorPlace(loc);
|
|
|
|
std::string hint;
|
|
|
|
if (!sym.getName().empty())
|
|
|
|
hint = "; references " + lld::toString(sym) + getDefinedLocation(sym);
|
|
|
|
errorOrWarn(errPlace.loc + msg + " is out of range: " + Twine(v) +
|
|
|
|
" is not in [" + Twine(llvm::minIntN(n)) + ", " +
|
|
|
|
Twine(llvm::maxIntN(n)) + "]" + hint);
|
|
|
|
}
|
|
|
|
|
2021-10-26 03:59:20 +08:00
|
|
|
// Build a bitmask with one bit set for each 64 subset of RelExpr.
|
|
|
|
static constexpr uint64_t buildMask() { return 0; }
|
2019-02-15 03:33:26 +08:00
|
|
|
|
2021-10-26 03:59:20 +08:00
|
|
|
template <typename... Tails>
|
|
|
|
static constexpr uint64_t buildMask(int head, Tails... tails) {
|
|
|
|
return (0 <= head && head < 64 ? uint64_t(1) << head : 0) |
|
|
|
|
buildMask(tails...);
|
|
|
|
}
|
2019-02-15 03:33:26 +08:00
|
|
|
|
|
|
|
// Return true if `Expr` is one of `Exprs`.
|
2021-10-26 03:59:20 +08:00
|
|
|
// There are more than 64 but less than 128 RelExprs, so we divide the set of
|
|
|
|
// exprs into [0, 64) and [64, 128) and represent each range as a constant
|
|
|
|
// 64-bit mask. Then we decide which mask to test depending on the value of
|
|
|
|
// expr and use a simple shift and bitwise-and to test for membership.
|
|
|
|
template <RelExpr... Exprs> static bool oneof(RelExpr expr) {
|
|
|
|
assert(0 <= expr && (int)expr < 128 &&
|
|
|
|
"RelExpr is too large for 128-bit mask!");
|
|
|
|
|
|
|
|
if (expr >= 64)
|
|
|
|
return (uint64_t(1) << (expr - 64)) & buildMask((Exprs - 64)...);
|
|
|
|
return (uint64_t(1) << expr) & buildMask(Exprs...);
|
2019-02-15 03:33:26 +08:00
|
|
|
}
|
|
|
|
|
2017-10-13 04:42:15 +08:00
|
|
|
static RelType getMipsPairType(RelType type, bool isLocal) {
|
2017-03-27 03:35:24 +08:00
|
|
|
switch (type) {
|
2016-05-25 04:24:43 +08:00
|
|
|
case R_MIPS_HI16:
|
|
|
|
return R_MIPS_LO16;
|
|
|
|
case R_MIPS_GOT16:
|
2017-10-17 22:39:03 +08:00
|
|
|
// In case of global symbol, the R_MIPS_GOT16 relocation does not
|
|
|
|
// have a pair. Each global symbol has a unique entry in the GOT
|
|
|
|
// and a corresponding instruction with help of the R_MIPS_GOT16
|
|
|
|
// relocation loads an address of the symbol. In case of local
|
|
|
|
// symbol, the R_MIPS_GOT16 relocation creates a GOT entry to hold
|
|
|
|
// the high 16 bits of the symbol's value. A paired R_MIPS_LO16
|
|
|
|
// relocations handle low 16 bits of the address. That allows
|
|
|
|
// to allocate only one GOT entry for every 64 KBytes of local data.
|
2017-10-13 04:42:15 +08:00
|
|
|
return isLocal ? R_MIPS_LO16 : R_MIPS_NONE;
|
2017-09-12 21:08:24 +08:00
|
|
|
case R_MICROMIPS_GOT16:
|
2017-10-13 04:42:15 +08:00
|
|
|
return isLocal ? R_MICROMIPS_LO16 : R_MIPS_NONE;
|
2016-05-25 04:24:43 +08:00
|
|
|
case R_MIPS_PCHI16:
|
|
|
|
return R_MIPS_PCLO16;
|
|
|
|
case R_MICROMIPS_HI16:
|
|
|
|
return R_MICROMIPS_LO16;
|
|
|
|
default:
|
|
|
|
return R_MIPS_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// True if non-preemptable symbol always has the same value regardless of where
|
|
|
|
// the DSO is loaded.
|
2017-11-04 08:31:04 +08:00
|
|
|
static bool isAbsolute(const Symbol &sym) {
|
|
|
|
if (sym.isUndefWeak())
|
2017-09-14 04:43:04 +08:00
|
|
|
return true;
|
2017-11-06 12:35:31 +08:00
|
|
|
if (const auto *dr = dyn_cast<Defined>(&sym))
|
2016-05-25 04:24:43 +08:00
|
|
|
return dr->section == nullptr; // Absolute symbol.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-04 08:31:04 +08:00
|
|
|
static bool isAbsoluteValue(const Symbol &sym) {
|
|
|
|
return isAbsolute(sym) || sym.isTls();
|
2016-10-28 01:28:56 +08:00
|
|
|
}
|
|
|
|
|
2017-04-07 06:39:11 +08:00
|
|
|
// Returns true if Expr refers a PLT entry.
|
2016-05-25 04:24:43 +08:00
|
|
|
static bool needsPlt(RelExpr expr) {
|
2021-10-26 04:05:17 +08:00
|
|
|
return oneof<R_PLT, R_PLT_PC, R_PLT_GOTPLT, R_PPC32_PLTREL, R_PPC64_CALL_PLT>(
|
|
|
|
expr);
|
2016-05-25 04:24:43 +08:00
|
|
|
}
|
|
|
|
|
2017-04-07 06:39:11 +08:00
|
|
|
// Returns true if Expr refers a GOT entry. Note that this function
|
|
|
|
// returns false for TLS variables even though they need GOT, because
|
|
|
|
// TLS variables uses GOT differently than the regular variables.
|
|
|
|
static bool needsGot(RelExpr expr) {
|
2019-08-17 01:01:56 +08:00
|
|
|
return oneof<R_GOT, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE, R_MIPS_GOT_OFF,
|
2021-01-14 01:29:16 +08:00
|
|
|
R_MIPS_GOT_OFF32, R_AARCH64_GOT_PAGE_PC, R_GOT_PC, R_GOTPLT,
|
|
|
|
R_AARCH64_GOT_PAGE>(expr);
|
2017-04-07 06:39:11 +08:00
|
|
|
}
|
|
|
|
|
2016-05-25 04:24:43 +08:00
|
|
|
// True if this expression is of the form Sym - X, where X is a position in the
|
|
|
|
// file (PC, or GOT for example).
|
|
|
|
static bool isRelExpr(RelExpr expr) {
|
2019-06-03 14:21:33 +08:00
|
|
|
return oneof<R_PC, R_GOTREL, R_GOTPLTREL, R_MIPS_GOTREL, R_PPC64_CALL,
|
2019-06-11 21:39:08 +08:00
|
|
|
R_PPC64_RELAX_TOC, R_AARCH64_PAGE_PC, R_RELAX_GOT_PC,
|
2020-08-17 22:30:14 +08:00
|
|
|
R_RISCV_PC_INDIRECT, R_PPC64_RELAX_GOT_PC>(expr);
|
2016-05-25 04:24:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static RelExpr toPlt(RelExpr expr) {
|
2018-01-04 00:52:15 +08:00
|
|
|
switch (expr) {
|
2019-06-03 14:21:33 +08:00
|
|
|
case R_PPC64_CALL:
|
|
|
|
return R_PPC64_CALL_PLT;
|
2018-01-04 00:52:15 +08:00
|
|
|
case R_PC:
|
2016-05-25 04:24:43 +08:00
|
|
|
return R_PLT_PC;
|
2018-01-04 00:52:15 +08:00
|
|
|
case R_ABS:
|
2016-05-25 04:24:43 +08:00
|
|
|
return R_PLT;
|
2018-01-04 00:52:15 +08:00
|
|
|
default:
|
|
|
|
return expr;
|
|
|
|
}
|
2016-05-25 04:24:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static RelExpr fromPlt(RelExpr expr) {
|
|
|
|
// We decided not to use a plt. Optimize a reference to the plt to a
|
|
|
|
// reference to the symbol itself.
|
2018-01-04 00:29:43 +08:00
|
|
|
switch (expr) {
|
|
|
|
case R_PLT_PC:
|
[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
|
|
|
case R_PPC32_PLTREL:
|
2016-05-25 04:24:43 +08:00
|
|
|
return R_PC;
|
2019-06-03 14:21:33 +08:00
|
|
|
case R_PPC64_CALL_PLT:
|
|
|
|
return R_PPC64_CALL;
|
2018-01-04 00:29:43 +08:00
|
|
|
case R_PLT:
|
2016-05-25 04:24:43 +08:00
|
|
|
return R_ABS;
|
2021-10-26 04:05:17 +08:00
|
|
|
case R_PLT_GOTPLT:
|
|
|
|
return R_GOTPLTREL;
|
2018-01-04 00:29:43 +08:00
|
|
|
default:
|
|
|
|
return expr;
|
|
|
|
}
|
2016-05-25 04:24:43 +08:00
|
|
|
}
|
|
|
|
|
2017-03-26 11:41:41 +08:00
|
|
|
// Returns true if a given shared symbol is in a read-only segment in a DSO.
|
2018-01-04 07:26:20 +08:00
|
|
|
template <class ELFT> static bool isReadOnly(SharedSymbol &ss) {
|
2019-04-01 08:11:24 +08:00
|
|
|
using Elf_Phdr = typename ELFT::Phdr;
|
2017-01-10 09:21:50 +08:00
|
|
|
|
|
|
|
// Determine if the symbol is read-only by scanning the DSO's program headers.
|
2022-02-24 09:57:52 +08:00
|
|
|
const auto &file = cast<SharedFile>(*ss.file);
|
2019-04-06 04:16:26 +08:00
|
|
|
for (const Elf_Phdr &phdr :
|
|
|
|
check(file.template getObj<ELFT>().program_headers()))
|
2017-01-10 09:21:50 +08:00
|
|
|
if ((phdr.p_type == ELF::PT_LOAD || phdr.p_type == ELF::PT_GNU_RELRO) &&
|
2018-01-04 07:26:20 +08:00
|
|
|
!(phdr.p_flags & ELF::PF_W) && ss.value >= phdr.p_vaddr &&
|
|
|
|
ss.value < phdr.p_vaddr + phdr.p_memsz)
|
2017-01-10 09:21:50 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-17 11:34:17 +08:00
|
|
|
// Returns symbols at the same offset as a given symbol, including SS itself.
|
2017-02-16 12:39:45 +08:00
|
|
|
//
|
|
|
|
// If two or more symbols are at the same offset, and at least one of
|
|
|
|
// them are copied by a copy relocation, all of them need to be copied.
|
2018-06-12 03:42:57 +08:00
|
|
|
// Otherwise, they would refer to different places at runtime.
|
2017-02-16 12:39:45 +08:00
|
|
|
template <class ELFT>
|
2018-06-12 03:42:57 +08:00
|
|
|
static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &ss) {
|
2019-04-01 08:11:24 +08:00
|
|
|
using Elf_Sym = typename ELFT::Sym;
|
2017-02-16 12:39:45 +08:00
|
|
|
|
2022-02-24 09:57:52 +08:00
|
|
|
const auto &file = cast<SharedFile>(*ss.file);
|
2017-02-27 07:35:34 +08:00
|
|
|
|
2018-06-12 03:42:57 +08:00
|
|
|
SmallSet<SharedSymbol *, 4> ret;
|
2019-04-06 04:16:26 +08:00
|
|
|
for (const Elf_Sym &s : file.template getGlobalELFSyms<ELFT>()) {
|
2017-10-31 01:26:12 +08:00
|
|
|
if (s.st_shndx == SHN_UNDEF || s.st_shndx == SHN_ABS ||
|
2018-08-07 03:09:40 +08:00
|
|
|
s.getType() == STT_TLS || s.st_value != ss.value)
|
2017-02-16 12:39:45 +08:00
|
|
|
continue;
|
2017-12-21 00:28:19 +08:00
|
|
|
StringRef name = check(s.getName(file.getStringTable()));
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *sym = symtab->find(name);
|
2017-02-27 07:35:34 +08:00
|
|
|
if (auto *alias = dyn_cast_or_null<SharedSymbol>(sym))
|
2018-06-12 03:42:57 +08:00
|
|
|
ret.insert(alias);
|
2017-02-16 12:39:45 +08:00
|
|
|
}
|
2021-08-06 01:32:14 +08:00
|
|
|
|
|
|
|
// The loop does not check SHT_GNU_verneed, so ret does not contain
|
|
|
|
// non-default version symbols. If ss has a non-default version, ret won't
|
|
|
|
// contain ss. Just add ss unconditionally. If a non-default version alias is
|
|
|
|
// separately copy relocated, it and ss will have different addresses.
|
|
|
|
// Fortunately this case is impractical and fails with GNU ld as well.
|
|
|
|
ret.insert(&ss);
|
2017-02-16 12:39:45 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-04-27 02:03:04 +08:00
|
|
|
// When a symbol is copy relocated or we create a canonical plt entry, it is
|
|
|
|
// effectively a defined symbol. In the case of copy relocation the symbol is
|
|
|
|
// in .bss and in the case of a canonical plt entry it is in .plt. This function
|
|
|
|
// replaces the existing symbol with a Defined pointing to the appropriate
|
|
|
|
// location.
|
2021-12-23 13:09:57 +08:00
|
|
|
static void replaceWithDefined(Symbol &sym, SectionBase &sec, uint64_t value,
|
2018-04-27 01:58:58 +08:00
|
|
|
uint64_t size) {
|
|
|
|
Symbol old = sym;
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2022-02-06 08:34:02 +08:00
|
|
|
sym.replace(Defined{sym.file, StringRef(), sym.binding, sym.stOther,
|
2021-12-23 13:09:57 +08:00
|
|
|
sym.type, value, size, &sec});
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2022-01-10 05:43:26 +08:00
|
|
|
sym.auxIdx = old.auxIdx;
|
2018-04-27 01:58:58 +08:00
|
|
|
sym.verdefIndex = old.verdefIndex;
|
|
|
|
sym.exportDynamic = true;
|
|
|
|
sym.isUsedInRegularObj = true;
|
2021-12-15 08:28:41 +08:00
|
|
|
// A copy relocated alias may need a GOT entry.
|
|
|
|
sym.needsGot = old.needsGot;
|
2018-04-27 01:58:58 +08:00
|
|
|
}
|
|
|
|
|
2017-01-10 09:21:50 +08:00
|
|
|
// Reserve space in .bss or .bss.rel.ro for copy relocation.
|
2017-02-20 06:48:33 +08:00
|
|
|
//
|
|
|
|
// The copy relocation is pretty much a hack. If you use a copy relocation
|
|
|
|
// in your program, not only the symbol name but the symbol's size, RW/RO
|
|
|
|
// bit and alignment become part of the ABI. In addition to that, if the
|
|
|
|
// symbol has aliases, the aliases become part of the ABI. That's subtle,
|
|
|
|
// but if you violate that implicit ABI, that can cause very counter-
|
|
|
|
// intuitive consequences.
|
|
|
|
//
|
|
|
|
// So, what is the copy relocation? It's for linking non-position
|
|
|
|
// independent code to DSOs. In an ideal world, all references to data
|
|
|
|
// exported by DSOs should go indirectly through GOT. But if object files
|
|
|
|
// are compiled as non-PIC, all data references are direct. There is no
|
|
|
|
// way for the linker to transform the code to use GOT, as machine
|
|
|
|
// instructions are already set in stone in object files. This is where
|
|
|
|
// the copy relocation takes a role.
|
|
|
|
//
|
|
|
|
// A copy relocation instructs the dynamic linker to copy data from a DSO
|
|
|
|
// to a specified address (which is usually in .bss) at load-time. If the
|
|
|
|
// static linker (that's us) finds a direct data reference to a DSO
|
|
|
|
// symbol, it creates a copy relocation, so that the symbol can be
|
|
|
|
// resolved as if it were in .bss rather than in a DSO.
|
|
|
|
//
|
|
|
|
// As you can see in this function, we create a copy relocation for the
|
|
|
|
// dynamic linker, and the relocation contains not only symbol name but
|
2019-10-29 09:41:38 +08:00
|
|
|
// various other information about the symbol. So, such attributes become a
|
2017-02-20 06:48:33 +08:00
|
|
|
// part of the ABI.
|
2017-02-20 10:22:56 +08:00
|
|
|
//
|
|
|
|
// Note for application developers: I can give you a piece of advice if
|
|
|
|
// you are writing a shared library. You probably should export only
|
|
|
|
// functions from your library. You shouldn't export variables.
|
|
|
|
//
|
|
|
|
// As an example what can happen when you export variables without knowing
|
|
|
|
// the semantics of copy relocations, assume that you have an exported
|
|
|
|
// variable of type T. It is an ABI-breaking change to add new members at
|
|
|
|
// end of T even though doing that doesn't change the layout of the
|
|
|
|
// existing members. That's because the space for the new members are not
|
|
|
|
// reserved in .bss unless you recompile the main program. That means they
|
|
|
|
// are likely to overlap with other data that happens to be laid out next
|
|
|
|
// to the variable in .bss. This kind of issue is sometimes very hard to
|
2019-10-29 09:41:38 +08:00
|
|
|
// debug. What's a solution? Instead of exporting a variable V from a DSO,
|
2017-02-20 10:22:56 +08:00
|
|
|
// define an accessor getV().
|
2022-03-13 06:08:10 +08:00
|
|
|
template <class ELFT> static void addCopyRelSymbol(SharedSymbol &ss) {
|
2016-05-25 04:24:43 +08:00
|
|
|
// Copy relocation against zero-sized symbol doesn't make sense.
|
2018-01-04 07:26:20 +08:00
|
|
|
uint64_t symSize = ss.getSize();
|
2018-06-07 05:43:34 +08:00
|
|
|
if (symSize == 0 || ss.alignment == 0)
|
2018-01-04 07:26:20 +08:00
|
|
|
fatal("cannot create a copy relocation for symbol " + toString(ss));
|
2016-05-25 04:24:43 +08:00
|
|
|
|
2017-01-10 09:21:50 +08:00
|
|
|
// See if this symbol is in a read-only segment. If so, preserve the symbol's
|
|
|
|
// memory protection by reserving space in the .bss.rel.ro section.
|
2019-07-03 14:11:50 +08:00
|
|
|
bool isRO = isReadOnly<ELFT>(ss);
|
|
|
|
BssSection *sec =
|
|
|
|
make<BssSection>(isRO ? ".bss.rel.ro" : ".bss", symSize, ss.alignment);
|
2019-09-24 19:48:31 +08:00
|
|
|
OutputSection *osec = (isRO ? in.bssRelRo : in.bss)->getParent();
|
|
|
|
|
|
|
|
// At this point, sectionBases has been migrated to sections. Append sec to
|
|
|
|
// sections.
|
2021-11-26 08:47:07 +08:00
|
|
|
if (osec->commands.empty() ||
|
|
|
|
!isa<InputSectionDescription>(osec->commands.back()))
|
|
|
|
osec->commands.push_back(make<InputSectionDescription>(""));
|
|
|
|
auto *isd = cast<InputSectionDescription>(osec->commands.back());
|
2019-09-24 19:48:31 +08:00
|
|
|
isd->sections.push_back(sec);
|
|
|
|
osec->commitSection(sec);
|
2017-02-09 18:27:57 +08:00
|
|
|
|
2016-05-25 04:24:43 +08:00
|
|
|
// Look through the DSO's dynamic symbol table for aliases and create a
|
|
|
|
// dynamic symbol for each one. This causes the copy relocation to correctly
|
|
|
|
// interpose any aliases.
|
2018-04-27 01:58:58 +08:00
|
|
|
for (SharedSymbol *sym : getSymbolsAt<ELFT>(ss))
|
2021-12-23 13:09:57 +08:00
|
|
|
replaceWithDefined(*sym, *sec, 0, sym->size);
|
2017-02-16 12:39:45 +08:00
|
|
|
|
2021-12-23 13:09:57 +08:00
|
|
|
mainPart->relaDyn->addSymbolReloc(target->copyRel, *sec, 0, ss);
|
2016-05-25 04:24:43 +08:00
|
|
|
}
|
|
|
|
|
2022-01-11 12:16:02 +08:00
|
|
|
// .eh_frame sections are mergeable input sections, so their input
|
|
|
|
// offsets are not linearly mapped to output section. For each input
|
|
|
|
// offset, we need to find a section piece containing the offset and
|
|
|
|
// add the piece's base address to the input offset to compute the
|
|
|
|
// output offset. That isn't cheap.
|
|
|
|
//
|
|
|
|
// This class is to speed up the offset computation. When we process
|
|
|
|
// relocations, we access offsets in the monotonically increasing
|
|
|
|
// order. So we can optimize for that access pattern.
|
|
|
|
//
|
|
|
|
// For sections other than .eh_frame, this class doesn't do anything.
|
|
|
|
namespace {
|
|
|
|
class OffsetGetter {
|
|
|
|
public:
|
|
|
|
explicit OffsetGetter(InputSectionBase &sec) {
|
|
|
|
if (auto *eh = dyn_cast<EhInputSection>(&sec))
|
|
|
|
pieces = eh->pieces;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Translates offsets in input sections to offsets in output sections.
|
|
|
|
// Given offset must increase monotonically. We assume that Piece is
|
|
|
|
// sorted by inputOff.
|
|
|
|
uint64_t get(uint64_t off) {
|
|
|
|
if (pieces.empty())
|
|
|
|
return off;
|
|
|
|
|
|
|
|
while (i != pieces.size() && pieces[i].inputOff + pieces[i].size <= off)
|
|
|
|
++i;
|
|
|
|
if (i == pieces.size())
|
|
|
|
fatal(".eh_frame: relocation is not in any piece");
|
|
|
|
|
|
|
|
// Pieces must be contiguous, so there must be no holes in between.
|
|
|
|
assert(pieces[i].inputOff <= off && "Relocation not in any piece");
|
|
|
|
|
|
|
|
// Offset -1 means that the piece is dead (i.e. garbage collected).
|
|
|
|
if (pieces[i].outputOff == -1)
|
|
|
|
return -1;
|
|
|
|
return pieces[i].outputOff + off - pieces[i].inputOff;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ArrayRef<EhSectionPiece> pieces;
|
|
|
|
size_t i = 0;
|
|
|
|
};
|
2022-01-12 01:54:53 +08:00
|
|
|
|
|
|
|
// This class encapsulates states needed to scan relocations for one
|
|
|
|
// InputSectionBase.
|
|
|
|
class RelocationScanner {
|
|
|
|
public:
|
|
|
|
explicit RelocationScanner(InputSectionBase &sec)
|
|
|
|
: sec(sec), getter(sec), config(elf::config.get()), target(*elf::target) {
|
|
|
|
}
|
|
|
|
template <class ELFT, class RelTy> void scan(ArrayRef<RelTy> rels);
|
|
|
|
|
|
|
|
private:
|
|
|
|
InputSectionBase &sec;
|
|
|
|
OffsetGetter getter;
|
|
|
|
const Configuration *const config;
|
|
|
|
const TargetInfo ⌖
|
|
|
|
|
|
|
|
// End of relocations, used by Mips/PPC64.
|
|
|
|
const void *end = nullptr;
|
|
|
|
|
|
|
|
template <class RelTy> RelType getMipsN32RelType(RelTy *&rel) const;
|
|
|
|
template <class ELFT, class RelTy>
|
|
|
|
int64_t computeMipsAddend(const RelTy &rel, RelExpr expr, bool isLocal) const;
|
|
|
|
template <class ELFT, class RelTy>
|
|
|
|
int64_t computeAddend(const RelTy &rel, RelExpr expr, bool isLocal) const;
|
|
|
|
bool isStaticLinkTimeConstant(RelExpr e, RelType type, const Symbol &sym,
|
|
|
|
uint64_t relOff) const;
|
|
|
|
void processAux(RelExpr expr, RelType type, uint64_t offset, Symbol &sym,
|
|
|
|
int64_t addend) const;
|
|
|
|
template <class ELFT, class RelTy> void scanOne(RelTy *&i);
|
|
|
|
};
|
2022-01-11 12:16:02 +08:00
|
|
|
} // namespace
|
|
|
|
|
2017-03-27 03:35:24 +08:00
|
|
|
// MIPS has an odd notion of "paired" relocations to calculate addends.
|
|
|
|
// For example, if a relocation is of R_MIPS_HI16, there must be a
|
|
|
|
// R_MIPS_LO16 relocation after that, and an addend is calculated using
|
|
|
|
// the two relocations.
|
|
|
|
template <class ELFT, class RelTy>
|
2022-01-12 01:54:53 +08:00
|
|
|
int64_t RelocationScanner::computeMipsAddend(const RelTy &rel, RelExpr expr,
|
|
|
|
bool isLocal) const {
|
2017-10-13 04:42:15 +08:00
|
|
|
if (expr == R_MIPS_GOTREL && isLocal)
|
2017-03-27 03:35:24 +08:00
|
|
|
return sec.getFile<ELFT>()->mipsGp0;
|
|
|
|
|
|
|
|
// The ABI says that the paired relocation is used only for REL.
|
|
|
|
// See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
|
|
|
|
if (RelTy::IsRela)
|
|
|
|
return 0;
|
|
|
|
|
2017-10-12 06:49:24 +08:00
|
|
|
RelType type = rel.getType(config->isMips64EL);
|
2017-10-13 04:42:15 +08:00
|
|
|
uint32_t pairTy = getMipsPairType(type, isLocal);
|
2017-03-27 03:35:24 +08:00
|
|
|
if (pairTy == R_MIPS_NONE)
|
|
|
|
return 0;
|
|
|
|
|
2022-02-21 16:39:26 +08:00
|
|
|
const uint8_t *buf = sec.rawData.data();
|
2017-03-27 03:35:24 +08:00
|
|
|
uint32_t symIndex = rel.getSymbol(config->isMips64EL);
|
|
|
|
|
|
|
|
// To make things worse, paired relocations might not be contiguous in
|
|
|
|
// the relocation table, so we need to do linear search. *sigh*
|
2022-01-12 01:54:53 +08:00
|
|
|
for (const RelTy *ri = &rel; ri != static_cast<const RelTy *>(end); ++ri)
|
2017-10-13 04:42:15 +08:00
|
|
|
if (ri->getType(config->isMips64EL) == pairTy &&
|
|
|
|
ri->getSymbol(config->isMips64EL) == symIndex)
|
2022-01-12 01:54:53 +08:00
|
|
|
return target.getImplicitAddend(buf + ri->r_offset, pairTy);
|
2017-03-26 11:20:49 +08:00
|
|
|
|
2017-03-27 03:35:24 +08:00
|
|
|
warn("can't find matching " + toString(pairTy) + " relocation for " +
|
|
|
|
toString(type));
|
|
|
|
return 0;
|
2016-05-25 04:24:43 +08:00
|
|
|
}
|
|
|
|
|
2017-10-13 04:42:15 +08:00
|
|
|
// Returns an addend of a given relocation. If it is RELA, an addend
|
|
|
|
// is in a relocation itself. If it is REL, we need to read it from an
|
|
|
|
// input section.
|
|
|
|
template <class ELFT, class RelTy>
|
2022-01-12 01:54:53 +08:00
|
|
|
int64_t RelocationScanner::computeAddend(const RelTy &rel, RelExpr expr,
|
|
|
|
bool isLocal) const {
|
2017-10-13 04:42:15 +08:00
|
|
|
int64_t addend;
|
|
|
|
RelType type = rel.getType(config->isMips64EL);
|
|
|
|
|
|
|
|
if (RelTy::IsRela) {
|
|
|
|
addend = getAddend<ELFT>(rel);
|
|
|
|
} else {
|
2022-02-21 16:39:26 +08:00
|
|
|
const uint8_t *buf = sec.rawData.data();
|
2022-01-12 01:54:53 +08:00
|
|
|
addend = target.getImplicitAddend(buf + rel.r_offset, type);
|
2017-10-13 04:42:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (config->emachine == EM_PPC64 && config->isPic && type == R_PPC64_TOC)
|
|
|
|
addend += getPPC64TocBase();
|
|
|
|
if (config->emachine == EM_MIPS)
|
2022-01-12 01:54:53 +08:00
|
|
|
addend += computeMipsAddend<ELFT>(rel, expr, isLocal);
|
2017-10-13 04:42:15 +08:00
|
|
|
|
|
|
|
return addend;
|
|
|
|
}
|
|
|
|
|
2019-05-22 17:06:42 +08:00
|
|
|
// Custom error message if Sym is defined in a discarded section.
|
|
|
|
template <class ELFT>
|
2019-06-15 00:46:02 +08:00
|
|
|
static std::string maybeReportDiscarded(Undefined &sym) {
|
2019-05-22 17:06:42 +08:00
|
|
|
auto *file = dyn_cast_or_null<ObjFile<ELFT>>(sym.file);
|
|
|
|
if (!file || !sym.discardedSecIdx ||
|
|
|
|
file->getSections()[sym.discardedSecIdx] != &InputSection::discarded)
|
|
|
|
return "";
|
2021-12-25 09:10:38 +08:00
|
|
|
ArrayRef<typename ELFT::Shdr> objSections =
|
|
|
|
file->template getELFShdrs<ELFT>();
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2019-06-26 16:09:08 +08:00
|
|
|
std::string msg;
|
|
|
|
if (sym.type == ELF::STT_SECTION) {
|
|
|
|
msg = "relocation refers to a discarded section: ";
|
|
|
|
msg += CHECK(
|
2020-09-09 22:03:53 +08:00
|
|
|
file->getObj().getSectionName(objSections[sym.discardedSecIdx]), file);
|
2019-06-26 16:09:08 +08:00
|
|
|
} else {
|
|
|
|
msg = "relocation refers to a symbol in a discarded section: " +
|
|
|
|
toString(sym);
|
|
|
|
}
|
|
|
|
msg += "\n>>> defined in " + toString(file);
|
2019-05-22 17:06:42 +08:00
|
|
|
|
|
|
|
Elf_Shdr_Impl<ELFT> elfSec = objSections[sym.discardedSecIdx - 1];
|
|
|
|
if (elfSec.sh_type != SHT_GROUP)
|
|
|
|
return msg;
|
|
|
|
|
|
|
|
// If the discarded section is a COMDAT.
|
|
|
|
StringRef signature = file->getShtGroupSignature(objSections, elfSec);
|
|
|
|
if (const InputFile *prevailing =
|
2022-03-16 10:24:41 +08:00
|
|
|
symtab->comdatGroups.lookup(CachedHashStringRef(signature))) {
|
2019-05-22 17:06:42 +08:00
|
|
|
msg += "\n>>> section group signature: " + signature.str() +
|
|
|
|
"\n>>> prevailing definition is in " + toString(prevailing);
|
2022-03-16 10:24:41 +08:00
|
|
|
if (sym.nonPrevailing) {
|
|
|
|
msg += "\n>>> or the symbol in the prevailing group had STB_WEAK "
|
|
|
|
"binding and the symbol in a non-prevailing group had STB_GLOBAL "
|
|
|
|
"binding. Mixing groups with STB_WEAK and STB_GLOBAL binding "
|
|
|
|
"signature is not supported";
|
|
|
|
}
|
|
|
|
}
|
2019-05-22 17:06:42 +08:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2019-06-21 02:25:57 +08:00
|
|
|
// Undefined diagnostics are collected in a vector and emitted once all of
|
|
|
|
// them are known, so that some postprocessing on the list of undefined symbols
|
|
|
|
// can happen before lld emits diagnostics.
|
|
|
|
struct UndefinedDiag {
|
2022-01-17 16:02:47 +08:00
|
|
|
Undefined *sym;
|
2019-06-21 02:25:57 +08:00
|
|
|
struct Loc {
|
|
|
|
InputSectionBase *sec;
|
|
|
|
uint64_t offset;
|
|
|
|
};
|
|
|
|
std::vector<Loc> locs;
|
|
|
|
bool isWarning;
|
|
|
|
};
|
2017-03-24 09:13:30 +08:00
|
|
|
|
2019-06-21 02:25:57 +08:00
|
|
|
static std::vector<UndefinedDiag> undefs;
|
2016-10-06 17:45:04 +08:00
|
|
|
|
2019-10-31 07:45:21 +08:00
|
|
|
// Check whether the definition name def is a mangled function name that matches
|
|
|
|
// the reference name ref.
|
|
|
|
static bool canSuggestExternCForCXX(StringRef ref, StringRef def) {
|
|
|
|
llvm::ItaniumPartialDemangler d;
|
2019-11-09 03:19:03 +08:00
|
|
|
std::string name = def.str();
|
|
|
|
if (d.partialDemangle(name.c_str()))
|
2019-10-31 07:45:21 +08:00
|
|
|
return false;
|
|
|
|
char *buf = d.getFunctionName(nullptr, nullptr);
|
|
|
|
if (!buf)
|
|
|
|
return false;
|
|
|
|
bool ret = ref == buf;
|
|
|
|
free(buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-09-04 17:04:26 +08:00
|
|
|
// Suggest an alternative spelling of an "undefined symbol" diagnostic. Returns
|
|
|
|
// the suggested symbol, which is either in the symbol table, or in the same
|
|
|
|
// file of sym.
|
2019-10-30 06:28:19 +08:00
|
|
|
static const Symbol *getAlternativeSpelling(const Undefined &sym,
|
2019-10-31 07:45:21 +08:00
|
|
|
std::string &pre_hint,
|
|
|
|
std::string &post_hint) {
|
2019-09-04 17:04:26 +08:00
|
|
|
DenseMap<StringRef, const Symbol *> map;
|
2022-01-17 15:56:24 +08:00
|
|
|
if (sym.file && sym.file->kind() == InputFile::ObjKind) {
|
|
|
|
auto *file = cast<ELFFileBase>(sym.file);
|
2019-12-20 08:04:45 +08:00
|
|
|
// If sym is a symbol defined in a discarded section, maybeReportDiscarded()
|
|
|
|
// will give an error. Don't suggest an alternative spelling.
|
|
|
|
if (file && sym.discardedSecIdx != 0 &&
|
|
|
|
file->getSections()[sym.discardedSecIdx] == &InputSection::discarded)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Build a map of local defined symbols.
|
2019-09-04 17:04:26 +08:00
|
|
|
for (const Symbol *s : sym.file->getSymbols())
|
2021-05-04 00:04:55 +08:00
|
|
|
if (s->isLocal() && s->isDefined() && !s->getName().empty())
|
2019-09-04 17:04:26 +08:00
|
|
|
map.try_emplace(s->getName(), s);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto suggest = [&](StringRef newName) -> const Symbol * {
|
|
|
|
// If defined locally.
|
|
|
|
if (const Symbol *s = map.lookup(newName))
|
|
|
|
return s;
|
|
|
|
|
|
|
|
// If in the symbol table and not undefined.
|
|
|
|
if (const Symbol *s = symtab->find(newName))
|
|
|
|
if (!s->isUndefined())
|
|
|
|
return s;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This loop enumerates all strings of Levenshtein distance 1 as typo
|
|
|
|
// correction candidates and suggests the one that exists as a non-undefined
|
|
|
|
// symbol.
|
|
|
|
StringRef name = sym.getName();
|
|
|
|
for (size_t i = 0, e = name.size(); i != e + 1; ++i) {
|
|
|
|
// Insert a character before name[i].
|
|
|
|
std::string newName = (name.substr(0, i) + "0" + name.substr(i)).str();
|
|
|
|
for (char c = '0'; c <= 'z'; ++c) {
|
|
|
|
newName[i] = c;
|
|
|
|
if (const Symbol *s = suggest(newName))
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
if (i == e)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Substitute name[i].
|
2020-01-29 03:23:46 +08:00
|
|
|
newName = std::string(name);
|
2019-09-04 17:04:26 +08:00
|
|
|
for (char c = '0'; c <= 'z'; ++c) {
|
|
|
|
newName[i] = c;
|
|
|
|
if (const Symbol *s = suggest(newName))
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transpose name[i] and name[i+1]. This is of edit distance 2 but it is
|
|
|
|
// common.
|
|
|
|
if (i + 1 < e) {
|
|
|
|
newName[i] = name[i + 1];
|
|
|
|
newName[i + 1] = name[i];
|
|
|
|
if (const Symbol *s = suggest(newName))
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete name[i].
|
|
|
|
newName = (name.substr(0, i) + name.substr(i + 1)).str();
|
|
|
|
if (const Symbol *s = suggest(newName))
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-11-21 02:55:04 +08:00
|
|
|
// Case mismatch, e.g. Foo vs FOO.
|
|
|
|
for (auto &it : map)
|
2021-06-24 16:06:35 +08:00
|
|
|
if (name.equals_insensitive(it.first))
|
2019-11-21 02:55:04 +08:00
|
|
|
return it.second;
|
|
|
|
for (Symbol *sym : symtab->symbols())
|
2021-06-24 16:06:35 +08:00
|
|
|
if (!sym->isUndefined() && name.equals_insensitive(sym->getName()))
|
2019-11-21 02:55:04 +08:00
|
|
|
return sym;
|
|
|
|
|
2019-10-30 06:28:19 +08:00
|
|
|
// The reference may be a mangled name while the definition is not. Suggest a
|
|
|
|
// missing extern "C".
|
|
|
|
if (name.startswith("_Z")) {
|
2019-11-09 03:19:03 +08:00
|
|
|
std::string buf = name.str();
|
2019-10-30 06:28:19 +08:00
|
|
|
llvm::ItaniumPartialDemangler d;
|
2019-11-09 03:19:03 +08:00
|
|
|
if (!d.partialDemangle(buf.c_str()))
|
2019-10-30 06:28:19 +08:00
|
|
|
if (char *buf = d.getFunctionName(nullptr, nullptr)) {
|
|
|
|
const Symbol *s = suggest(buf);
|
|
|
|
free(buf);
|
|
|
|
if (s) {
|
|
|
|
pre_hint = ": extern \"C\" ";
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 07:45:21 +08:00
|
|
|
} else {
|
|
|
|
const Symbol *s = nullptr;
|
|
|
|
for (auto &it : map)
|
|
|
|
if (canSuggestExternCForCXX(name, it.first)) {
|
|
|
|
s = it.second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!s)
|
2019-11-21 03:16:15 +08:00
|
|
|
for (Symbol *sym : symtab->symbols())
|
|
|
|
if (canSuggestExternCForCXX(name, sym->getName())) {
|
2019-10-31 07:45:21 +08:00
|
|
|
s = sym;
|
2019-11-21 03:16:15 +08:00
|
|
|
break;
|
|
|
|
}
|
2019-10-31 07:45:21 +08:00
|
|
|
if (s) {
|
|
|
|
pre_hint = " to declare ";
|
|
|
|
post_hint = " as extern \"C\"?";
|
|
|
|
return s;
|
|
|
|
}
|
2019-10-30 06:28:19 +08:00
|
|
|
}
|
|
|
|
|
2019-09-04 17:04:26 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reportUndefinedSymbol(const UndefinedDiag &undef,
|
|
|
|
bool correctSpelling) {
|
2022-01-17 16:02:47 +08:00
|
|
|
Undefined &sym = *undef.sym;
|
2019-06-12 15:35:42 +08:00
|
|
|
|
2019-05-22 17:06:42 +08:00
|
|
|
auto visibility = [&]() -> std::string {
|
|
|
|
switch (sym.visibility) {
|
|
|
|
case STV_INTERNAL:
|
|
|
|
return "internal ";
|
|
|
|
case STV_HIDDEN:
|
|
|
|
return "hidden ";
|
|
|
|
case STV_PROTECTED:
|
|
|
|
return "protected ";
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
};
|
2017-03-31 03:13:47 +08:00
|
|
|
|
2022-02-06 07:03:55 +08:00
|
|
|
std::string msg;
|
|
|
|
switch (config->ekind) {
|
|
|
|
case ELF32LEKind:
|
|
|
|
msg = maybeReportDiscarded<ELF32LE>(sym);
|
|
|
|
break;
|
|
|
|
case ELF32BEKind:
|
|
|
|
msg = maybeReportDiscarded<ELF32BE>(sym);
|
|
|
|
break;
|
|
|
|
case ELF64LEKind:
|
|
|
|
msg = maybeReportDiscarded<ELF64LE>(sym);
|
|
|
|
break;
|
|
|
|
case ELF64BEKind:
|
|
|
|
msg = maybeReportDiscarded<ELF64BE>(sym);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("");
|
|
|
|
}
|
2019-05-22 17:06:42 +08:00
|
|
|
if (msg.empty())
|
|
|
|
msg = "undefined " + visibility() + "symbol: " + toString(sym);
|
|
|
|
|
2020-03-30 01:02:21 +08:00
|
|
|
const size_t maxUndefReferences = 3;
|
2019-06-21 02:25:57 +08:00
|
|
|
size_t i = 0;
|
|
|
|
for (UndefinedDiag::Loc l : undef.locs) {
|
|
|
|
if (i >= maxUndefReferences)
|
|
|
|
break;
|
|
|
|
InputSectionBase &sec = *l.sec;
|
|
|
|
uint64_t offset = l.offset;
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2019-06-21 02:25:57 +08:00
|
|
|
msg += "\n>>> referenced by ";
|
|
|
|
std::string src = sec.getSrcMsg(sym, offset);
|
|
|
|
if (!src.empty())
|
|
|
|
msg += src + "\n>>> ";
|
|
|
|
msg += sec.getObjMsg(offset);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < undef.locs.size())
|
|
|
|
msg += ("\n>>> referenced " + Twine(undef.locs.size() - i) + " more times")
|
|
|
|
.str();
|
2016-10-06 17:45:04 +08:00
|
|
|
|
2019-10-30 06:28:19 +08:00
|
|
|
if (correctSpelling) {
|
2019-10-31 07:45:21 +08:00
|
|
|
std::string pre_hint = ": ", post_hint;
|
2022-01-17 15:56:24 +08:00
|
|
|
if (const Symbol *corrected =
|
2022-01-17 16:02:47 +08:00
|
|
|
getAlternativeSpelling(sym, pre_hint, post_hint)) {
|
2019-10-31 07:45:21 +08:00
|
|
|
msg += "\n>>> did you mean" + pre_hint + toString(*corrected) + post_hint;
|
2019-09-04 17:04:26 +08:00
|
|
|
if (corrected->file)
|
|
|
|
msg += "\n>>> defined in: " + toString(corrected->file);
|
|
|
|
}
|
2019-10-30 06:28:19 +08:00
|
|
|
}
|
2019-09-04 17:04:26 +08:00
|
|
|
|
2018-12-22 03:28:49 +08:00
|
|
|
if (sym.getName().startswith("_ZTV"))
|
2020-03-31 18:36:23 +08:00
|
|
|
msg +=
|
|
|
|
"\n>>> the vtable symbol may be undefined because the class is missing "
|
|
|
|
"its key function (see https://lld.llvm.org/missingkeyfunction)";
|
2021-12-03 03:58:25 +08:00
|
|
|
if (config->gcSections && config->zStartStopGC &&
|
|
|
|
sym.getName().startswith("__start_")) {
|
|
|
|
msg += "\n>>> the encapsulation symbol needs to be retained under "
|
|
|
|
"--gc-sections properly; consider -z nostart-stop-gc "
|
|
|
|
"(see https://lld.llvm.org/ELF/start-stop-gc)";
|
|
|
|
}
|
2018-12-22 03:28:49 +08:00
|
|
|
|
2019-06-21 02:25:57 +08:00
|
|
|
if (undef.isWarning)
|
2016-10-06 17:45:04 +08:00
|
|
|
warn(msg);
|
2019-06-21 02:25:57 +08:00
|
|
|
else
|
2020-10-19 19:21:23 +08:00
|
|
|
error(msg, ErrorTag::SymbolNotFound, {sym.getName()});
|
2019-06-21 02:25:57 +08:00
|
|
|
}
|
|
|
|
|
2022-02-06 07:03:55 +08:00
|
|
|
void elf::reportUndefinedSymbols() {
|
2019-06-21 02:25:57 +08:00
|
|
|
// Find the first "undefined symbol" diagnostic for each diagnostic, and
|
|
|
|
// collect all "referenced from" lines at the first diagnostic.
|
|
|
|
DenseMap<Symbol *, UndefinedDiag *> firstRef;
|
|
|
|
for (UndefinedDiag &undef : undefs) {
|
|
|
|
assert(undef.locs.size() == 1);
|
|
|
|
if (UndefinedDiag *canon = firstRef.lookup(undef.sym)) {
|
|
|
|
canon->locs.push_back(undef.locs[0]);
|
|
|
|
undef.locs.clear();
|
|
|
|
} else
|
|
|
|
firstRef[undef.sym] = &undef;
|
2017-10-12 12:52:39 +08:00
|
|
|
}
|
|
|
|
|
2019-09-04 17:04:26 +08:00
|
|
|
// Enable spell corrector for the first 2 diagnostics.
|
|
|
|
for (auto it : enumerate(undefs))
|
|
|
|
if (!it.value().locs.empty())
|
2022-02-06 07:03:55 +08:00
|
|
|
reportUndefinedSymbol(it.value(), it.index() < 2);
|
2019-06-21 02:25:57 +08:00
|
|
|
undefs.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report an undefined symbol if necessary.
|
|
|
|
// Returns true if the undefined symbol will produce an error message.
|
2022-01-17 16:02:47 +08:00
|
|
|
static bool maybeReportUndefined(Undefined &sym, InputSectionBase &sec,
|
2019-06-21 02:25:57 +08:00
|
|
|
uint64_t offset) {
|
2020-12-02 00:59:54 +08:00
|
|
|
// If versioned, issue an error (even if the symbol is weak) because we don't
|
|
|
|
// know the defining filename which is required to construct a Verneed entry.
|
2021-12-27 09:25:54 +08:00
|
|
|
if (sym.hasVersionSuffix) {
|
2020-12-02 00:59:54 +08:00
|
|
|
undefs.push_back({&sym, {{&sec, offset}}, false});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (sym.isWeak())
|
2019-06-21 02:25:57 +08:00
|
|
|
return false;
|
|
|
|
|
2019-08-12 01:03:00 +08:00
|
|
|
bool canBeExternal = !sym.isLocal() && sym.visibility == STV_DEFAULT;
|
2019-06-21 02:25:57 +08:00
|
|
|
if (config->unresolvedSymbols == UnresolvedPolicy::Ignore && canBeExternal)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// clang (as of 2019-06-12) / gcc (as of 8.2.1) PPC64 may emit a .rela.toc
|
|
|
|
// which references a switch table in a discarded .rodata/.text section. The
|
|
|
|
// .toc and the .rela.toc are incorrectly not placed in the comdat. The ELF
|
|
|
|
// spec says references from outside the group to a STB_LOCAL symbol are not
|
|
|
|
// allowed. Work around the bug.
|
2020-03-01 10:40:58 +08:00
|
|
|
//
|
|
|
|
// PPC32 .got2 is similar but cannot be fixed. Multiple .got2 is infeasible
|
|
|
|
// because .LC0-.LTOC is not representable if the two labels are in different
|
|
|
|
// .got2
|
2022-01-17 16:02:47 +08:00
|
|
|
if (sym.discardedSecIdx != 0 && (sec.name == ".got2" || sec.name == ".toc"))
|
2019-06-21 02:25:57 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
bool isWarning =
|
|
|
|
(config->unresolvedSymbols == UnresolvedPolicy::Warn && canBeExternal) ||
|
|
|
|
config->noinhibitExec;
|
|
|
|
undefs.push_back({&sym, {{&sec, offset}}, isWarning});
|
|
|
|
return !isWarning;
|
2016-10-06 17:45:04 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 12:02:02 +08:00
|
|
|
// MIPS N32 ABI treats series of successive relocations with the same offset
|
|
|
|
// as a single relocation. The similar approach used by N64 ABI, but this ABI
|
|
|
|
// packs all relocations into the single relocation record. Here we emulate
|
|
|
|
// this for the N32 ABI. Iterate over relocation with the same offset and put
|
|
|
|
// theirs types into the single bit-set.
|
2022-01-12 01:54:53 +08:00
|
|
|
template <class RelTy>
|
|
|
|
RelType RelocationScanner::getMipsN32RelType(RelTy *&rel) const {
|
2018-01-09 06:20:44 +08:00
|
|
|
RelType type = 0;
|
2017-10-12 12:02:02 +08:00
|
|
|
uint64_t offset = rel->r_offset;
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2017-10-12 12:02:02 +08:00
|
|
|
int n = 0;
|
2022-01-12 01:54:53 +08:00
|
|
|
while (rel != static_cast<const RelTy *>(end) && rel->r_offset == offset)
|
2018-01-09 06:20:44 +08:00
|
|
|
type |= (rel++)->getType(config->isMips64EL) << (8 * n++);
|
2017-10-12 12:02:02 +08:00
|
|
|
return type;
|
2016-11-06 06:58:01 +08:00
|
|
|
}
|
|
|
|
|
2021-12-23 12:51:11 +08:00
|
|
|
static void addRelativeReloc(InputSectionBase &isec, uint64_t offsetInSec,
|
2021-07-09 17:04:35 +08:00
|
|
|
Symbol &sym, int64_t addend, RelExpr expr,
|
2018-07-10 04:08:55 +08:00
|
|
|
RelType type) {
|
2021-12-23 12:51:11 +08:00
|
|
|
Partition &part = isec.getPartition();
|
2019-06-08 01:57:58 +08:00
|
|
|
|
2019-07-16 13:50:45 +08:00
|
|
|
// Add a relative relocation. If relrDyn section is enabled, and the
|
2018-07-10 04:08:55 +08:00
|
|
|
// relocation offset is guaranteed to be even, add the relocation to
|
2019-07-16 13:50:45 +08:00
|
|
|
// the relrDyn section, otherwise add it to the relaDyn section.
|
|
|
|
// relrDyn sections don't support odd offsets. Also, relrDyn sections
|
2018-07-10 04:08:55 +08:00
|
|
|
// don't store the addend values, so we must write it to the relocated
|
|
|
|
// address.
|
2021-12-23 12:51:11 +08:00
|
|
|
if (part.relrDyn && isec.alignment >= 2 && offsetInSec % 2 == 0) {
|
|
|
|
isec.relocations.push_back({expr, type, offsetInSec, addend, &sym});
|
|
|
|
part.relrDyn->relocs.push_back({&isec, offsetInSec});
|
2018-07-10 04:08:55 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-07-09 17:04:35 +08:00
|
|
|
part.relaDyn->addRelativeReloc(target->relativeRel, isec, offsetInSec, sym,
|
|
|
|
addend, type, expr);
|
2018-07-10 04:08:55 +08:00
|
|
|
}
|
|
|
|
|
2019-12-15 06:17:35 +08:00
|
|
|
template <class PltSection, class GotPltSection>
|
2021-12-23 12:51:11 +08:00
|
|
|
static void addPltEntry(PltSection &plt, GotPltSection &gotPlt,
|
|
|
|
RelocationBaseSection &rel, RelType type, Symbol &sym) {
|
|
|
|
plt.addEntry(sym);
|
|
|
|
gotPlt.addEntry(sym);
|
|
|
|
rel.addReloc({type, &gotPlt, sym.getGotPltOffset(),
|
|
|
|
sym.isPreemptible ? DynamicReloc::AgainstSymbol
|
|
|
|
: DynamicReloc::AddendOnlyWithTargetVA,
|
|
|
|
sym, 0, R_ABS});
|
2017-03-27 04:51:14 +08:00
|
|
|
}
|
|
|
|
|
2019-03-15 14:58:23 +08:00
|
|
|
static void addGotEntry(Symbol &sym) {
|
2018-09-26 03:26:58 +08:00
|
|
|
in.got->addEntry(sym);
|
2017-03-27 05:00:09 +08:00
|
|
|
uint64_t off = sym.getGotOffset();
|
|
|
|
|
2021-08-30 04:40:08 +08:00
|
|
|
// If preemptible, emit a GLOB_DAT relocation.
|
|
|
|
if (sym.isPreemptible) {
|
2021-12-28 10:15:23 +08:00
|
|
|
mainPart->relaDyn->addReloc({target->gotRel, in.got.get(), off,
|
2021-08-30 04:40:08 +08:00
|
|
|
DynamicReloc::AgainstSymbol, sym, 0, R_ABS});
|
2017-10-13 09:42:27 +08:00
|
|
|
return;
|
2017-10-13 09:19:10 +08:00
|
|
|
}
|
2017-10-13 09:42:27 +08:00
|
|
|
|
2021-08-30 04:40:08 +08:00
|
|
|
// Otherwise, the value is either a link-time constant or the load base
|
|
|
|
// plus a constant.
|
|
|
|
if (!config->isPic || isAbsolute(sym))
|
|
|
|
in.got->relocations.push_back({R_ABS, target->symbolicRel, off, 0, &sym});
|
2021-08-30 04:21:06 +08:00
|
|
|
else
|
2021-12-23 12:51:11 +08:00
|
|
|
addRelativeReloc(*in.got, off, sym, 0, R_ABS, target->symbolicRel);
|
2021-08-30 04:21:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void addTpOffsetGotEntry(Symbol &sym) {
|
|
|
|
in.got->addEntry(sym);
|
|
|
|
uint64_t off = sym.getGotOffset();
|
|
|
|
if (!sym.isPreemptible && !config->isPic) {
|
|
|
|
in.got->relocations.push_back({R_TPREL, target->symbolicRel, off, 0, &sym});
|
2018-07-10 04:08:55 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-07-09 17:15:16 +08:00
|
|
|
mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible(
|
2021-12-23 12:51:11 +08:00
|
|
|
target->tlsGotRel, *in.got, off, sym, target->symbolicRel);
|
2017-03-27 05:00:09 +08:00
|
|
|
}
|
|
|
|
|
2018-01-10 09:37:36 +08:00
|
|
|
// Return true if we can define a symbol in the executable that
|
|
|
|
// contains the value/function of a symbol defined in a shared
|
|
|
|
// library.
|
|
|
|
static bool canDefineSymbolInExecutable(Symbol &sym) {
|
|
|
|
// If the symbol has default visibility the symbol defined in the
|
|
|
|
// executable will preempt it.
|
2018-01-17 03:28:28 +08:00
|
|
|
// Note that we want the visibility of the shared symbol itself, not
|
|
|
|
// the visibility of the symbol in the output file we are producing. That is
|
2019-07-16 13:50:45 +08:00
|
|
|
// why we use Sym.stOther.
|
2018-01-17 03:28:28 +08:00
|
|
|
if ((sym.stOther & 0x3) == STV_DEFAULT)
|
2018-01-10 09:37:36 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// If we are allowed to break address equality of functions, defining
|
|
|
|
// a plt entry will allow the program to call the function in the
|
|
|
|
// .so, but the .so and the executable will no agree on the address
|
|
|
|
// of the function. Similar logic for objects.
|
|
|
|
return ((sym.isFunc() && config->ignoreFunctionAddressEquality) ||
|
|
|
|
(sym.isObject() && config->ignoreDataAddressEquality));
|
|
|
|
}
|
|
|
|
|
2021-11-10 12:24:40 +08:00
|
|
|
// Returns true if a given relocation can be computed at link-time.
|
|
|
|
// This only handles relocation types expected in processRelocAux.
|
|
|
|
//
|
|
|
|
// For instance, we know the offset from a relocation to its target at
|
|
|
|
// link-time if the relocation is PC-relative and refers a
|
|
|
|
// non-interposable function in the same executable. This function
|
|
|
|
// will return true for such relocation.
|
|
|
|
//
|
|
|
|
// If this function returns false, that means we need to emit a
|
|
|
|
// dynamic relocation so that the relocation will be fixed at load-time.
|
2022-01-12 01:54:53 +08:00
|
|
|
bool RelocationScanner::isStaticLinkTimeConstant(RelExpr e, RelType type,
|
|
|
|
const Symbol &sym,
|
|
|
|
uint64_t relOff) const {
|
2021-11-10 12:24:40 +08:00
|
|
|
// These expressions always compute a constant
|
|
|
|
if (oneof<R_GOTPLT, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE, R_MIPS_GOTREL,
|
|
|
|
R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC,
|
|
|
|
R_AARCH64_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC, R_GOTPLTONLY_PC,
|
|
|
|
R_PLT_PC, R_PLT_GOTPLT, R_PPC32_PLTREL, R_PPC64_CALL_PLT,
|
|
|
|
R_PPC64_RELAX_TOC, R_RISCV_ADD, R_AARCH64_GOT_PAGE>(e))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// These never do, except if the entire file is position dependent or if
|
|
|
|
// only the low bits are used.
|
|
|
|
if (e == R_GOT || e == R_PLT)
|
2022-01-12 01:54:53 +08:00
|
|
|
return target.usesOnlyLowPageBits(type) || !config->isPic;
|
2021-11-10 12:24:40 +08:00
|
|
|
|
|
|
|
if (sym.isPreemptible)
|
|
|
|
return false;
|
|
|
|
if (!config->isPic)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// The size of a non preemptible symbol is a constant.
|
|
|
|
if (e == R_SIZE)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// For the target and the relocation, we want to know if they are
|
|
|
|
// absolute or relative.
|
|
|
|
bool absVal = isAbsoluteValue(sym);
|
|
|
|
bool relE = isRelExpr(e);
|
|
|
|
if (absVal && !relE)
|
|
|
|
return true;
|
|
|
|
if (!absVal && relE)
|
|
|
|
return true;
|
|
|
|
if (!absVal && !relE)
|
2022-01-12 01:54:53 +08:00
|
|
|
return target.usesOnlyLowPageBits(type);
|
2021-11-10 12:24:40 +08:00
|
|
|
|
|
|
|
assert(absVal && relE);
|
|
|
|
|
|
|
|
// Allow R_PLT_PC (optimized to R_PC here) to a hidden undefined weak symbol
|
|
|
|
// in PIC mode. This is a little strange, but it allows us to link function
|
|
|
|
// calls to such symbols (e.g. glibc/stdlib/exit.c:__run_exit_handlers).
|
|
|
|
// Normally such a call will be guarded with a comparison, which will load a
|
|
|
|
// zero from the GOT.
|
|
|
|
if (sym.isUndefWeak())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// We set the final symbols values for linker script defined symbols later.
|
|
|
|
// They always can be computed as a link time constant.
|
|
|
|
if (sym.scriptDefined)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
error("relocation " + toString(type) + " cannot refer to absolute symbol: " +
|
2022-01-12 01:54:53 +08:00
|
|
|
toString(sym) + getLocation(sec, sym, relOff));
|
2021-11-10 12:24:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-25 04:24:43 +08:00
|
|
|
// The reason we have to do this early scan is as follows
|
|
|
|
// * To mmap the output file, we need to know the size
|
|
|
|
// * For that, we need to know how many dynamic relocs we will have.
|
|
|
|
// It might be possible to avoid this by outputting the file with write:
|
|
|
|
// * Write the allocated output sections, computing addresses.
|
|
|
|
// * Apply relocations, recording which ones require a dynamic reloc.
|
|
|
|
// * Write the dynamic relocations.
|
|
|
|
// * Write the rest of the file.
|
|
|
|
// This would have some drawbacks. For example, we would only know if .rela.dyn
|
|
|
|
// is needed after applying relocations. If it is, it will go after rw and rx
|
|
|
|
// sections. Given that it is ro, we will need an extra PT_LOAD. This
|
|
|
|
// complicates things for the dynamic linker and means we would have to reserve
|
|
|
|
// space for the extra PT_LOAD even if we end up not using it.
|
2022-01-12 01:54:53 +08:00
|
|
|
void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
|
|
|
|
Symbol &sym, int64_t addend) const {
|
2019-06-14 22:09:15 +08:00
|
|
|
// If the relocation is known to be a link-time constant, we know no dynamic
|
|
|
|
// relocation will be created, pass the control to relocateAlloc() or
|
|
|
|
// relocateNonAlloc() to resolve it.
|
|
|
|
//
|
2021-07-01 00:43:28 +08:00
|
|
|
// The behavior of an undefined weak reference is implementation defined. For
|
|
|
|
// non-link-time constants, we resolve relocations statically (let
|
|
|
|
// relocate{,Non}Alloc() resolve them) for -no-pie and try producing dynamic
|
|
|
|
// relocations for -pie and -shared.
|
|
|
|
//
|
|
|
|
// The general expectation of -no-pie static linking is that there is no
|
|
|
|
// dynamic relocation (except IRELATIVE). Emitting dynamic relocations for
|
|
|
|
// -shared matches the spirit of its -z undefs default. -pie has freedom on
|
|
|
|
// choices, and we choose dynamic relocations to be consistent with the
|
|
|
|
// handling of GOT-generating relocations.
|
2022-01-12 01:54:53 +08:00
|
|
|
if (isStaticLinkTimeConstant(expr, type, sym, offset) ||
|
2021-07-01 00:43:28 +08:00
|
|
|
(!config->isPic && sym.isUndefWeak())) {
|
2018-01-09 08:13:54 +08:00
|
|
|
sec.relocations.push_back({expr, type, offset, addend, &sym});
|
2018-04-27 01:22:44 +08:00
|
|
|
return;
|
2018-01-09 08:13:54 +08:00
|
|
|
}
|
2019-06-14 22:09:15 +08:00
|
|
|
|
2018-01-09 08:13:54 +08:00
|
|
|
bool canWrite = (sec.flags & SHF_WRITE) || !config->zText;
|
|
|
|
if (canWrite) {
|
2022-01-12 01:54:53 +08:00
|
|
|
RelType rel = target.getDynRel(type);
|
|
|
|
if (expr == R_GOT || (rel == target.symbolicRel && !sym.isPreemptible)) {
|
2021-12-23 12:51:11 +08:00
|
|
|
addRelativeReloc(sec, offset, sym, addend, expr, type);
|
2018-04-27 01:22:44 +08:00
|
|
|
return;
|
[ELF][ARM][AARCH64][MIPS][PPC] Simplify the logic to create R_*_RELATIVE for absolute relocation types in writable sections
Summary:
Our rule to create R_*_RELATIVE for absolute relocation types were
loose. D63121 made it stricter but it failed to create R_*_RELATIVE for
R_ARM_TARGET1 and R_PPC64_TOC. rLLD363236 worked around that by
reinstating the original behavior for ARM and PPC64.
This patch is an attempt to simplify the logic.
Note, in ld.bfd, R_ARM_TARGET2 --target2=abs also creates
R_ARM_RELATIVE. This seems a very uncommon scenario (moreover,
--target2=got-rel is the default), so I do not implement any logic
related to it.
Also, delete R_AARCH64_ABS32 from AArch64::getDynRel. We don't have
working ILP32 support yet. Allowing it would create an incorrect
R_AARCH64_RELATIVE.
For MIPS, the (if SymbolRel, then RelativeRel) code is to keep its
behavior unchanged.
Note, in ppc64-abs64-dyn.s, R_PPC64_TOC gets an incorrect addend because
computeAddend() doesn't compute the correct address. We seem to have the
wrong behavior for a long time. The important thing seems that a dynamic
relocation R_PPC64_TOC should not be created as the dynamic loader will
error R_PPC64_TOC is not supported.
Reviewers: atanasyan, grimar, peter.smith, ruiu, sfertile, espindola
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D63383
llvm-svn: 363928
2019-06-20 22:00:08 +08:00
|
|
|
} else if (rel != 0) {
|
2022-01-12 01:54:53 +08:00
|
|
|
if (config->emachine == EM_MIPS && rel == target.symbolicRel)
|
|
|
|
rel = target.relativeRel;
|
2021-12-23 13:09:57 +08:00
|
|
|
sec.getPartition().relaDyn->addSymbolReloc(rel, sec, offset, sym, addend,
|
2021-07-09 17:04:35 +08:00
|
|
|
type);
|
2018-01-09 08:13:54 +08:00
|
|
|
|
|
|
|
// MIPS ABI turns using of GOT and dynamic relocations inside out.
|
|
|
|
// While regular ABI uses dynamic relocations to fill up GOT entries
|
|
|
|
// MIPS ABI requires dynamic linker to fills up GOT entries using
|
|
|
|
// specially sorted dynamic symbol table. This affects even dynamic
|
|
|
|
// relocations against symbols which do not require GOT entries
|
|
|
|
// creation explicitly, i.e. do not have any GOT-relocations. So if
|
|
|
|
// a preemptible symbol has a dynamic relocation we anyway have
|
|
|
|
// to create a GOT entry for it.
|
|
|
|
// If a non-preemptible symbol has a dynamic relocation against it,
|
|
|
|
// dynamic linker takes it st_value, adds offset and writes down
|
|
|
|
// result of the dynamic relocation. In case of preemptible symbol
|
|
|
|
// dynamic linker performs symbol resolution, writes the symbol value
|
|
|
|
// to the GOT entry and reads the GOT entry when it needs to perform
|
|
|
|
// a dynamic relocation.
|
|
|
|
// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
|
|
|
|
if (config->emachine == EM_MIPS)
|
2018-09-26 03:26:58 +08:00
|
|
|
in.mipsGot->addEntry(*sec.file, sym, addend, expr);
|
2018-04-27 01:22:44 +08:00
|
|
|
return;
|
2018-01-09 08:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 22:30:12 +08:00
|
|
|
// When producing an executable, we can perform copy relocations (for
|
|
|
|
// STT_OBJECT) and canonical PLT (for STT_FUNC).
|
|
|
|
if (!config->shared) {
|
|
|
|
if (!canDefineSymbolInExecutable(sym)) {
|
|
|
|
errorOrWarn("cannot preempt symbol: " + toString(sym) +
|
|
|
|
getLocation(sec, sym, offset));
|
|
|
|
return;
|
|
|
|
}
|
2018-01-09 08:13:54 +08:00
|
|
|
|
2019-08-19 22:30:12 +08:00
|
|
|
if (sym.isObject()) {
|
|
|
|
// Produce a copy relocation.
|
|
|
|
if (auto *ss = dyn_cast<SharedSymbol>(&sym)) {
|
|
|
|
if (!config->zCopyreloc)
|
|
|
|
error("unresolvable relocation " + toString(type) +
|
|
|
|
" against symbol '" + toString(*ss) +
|
|
|
|
"'; recompile with -fPIC or remove '-z nocopyreloc'" +
|
|
|
|
getLocation(sec, sym, offset));
|
2021-12-15 08:28:41 +08:00
|
|
|
sym.needsCopy = true;
|
2019-08-19 22:30:12 +08:00
|
|
|
}
|
|
|
|
sec.relocations.push_back({expr, type, offset, addend, &sym});
|
|
|
|
return;
|
2018-01-09 08:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This handles a non PIC program call to function in a shared library. In
|
|
|
|
// an ideal world, we could just report an error saying the relocation can
|
|
|
|
// overflow at runtime. In the real world with glibc, crt1.o has a
|
|
|
|
// R_X86_64_PC32 pointing to libc.so.
|
|
|
|
//
|
|
|
|
// The general idea on how to handle such cases is to create a PLT entry and
|
|
|
|
// use that as the function value.
|
|
|
|
//
|
|
|
|
// For the static linking part, we just return a plt expr and everything
|
2018-02-23 10:05:48 +08:00
|
|
|
// else will use the PLT entry as the address.
|
2018-01-09 08:13:54 +08:00
|
|
|
//
|
|
|
|
// The remaining problem is making sure pointer equality still works. We
|
|
|
|
// need the help of the dynamic linker for that. We let it know that we have
|
|
|
|
// a direct reference to a so symbol by creating an undefined symbol with a
|
|
|
|
// non zero st_value. Seeing that, the dynamic linker resolves the symbol to
|
|
|
|
// the value of the symbol we created. This is true even for got entries, so
|
|
|
|
// pointer equality is maintained. To avoid an infinite loop, the only entry
|
|
|
|
// that points to the real function is a dedicated got entry used by the
|
|
|
|
// plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
|
|
|
|
// R_386_JMP_SLOT, etc).
|
2018-03-15 02:08:33 +08:00
|
|
|
|
|
|
|
// For position independent executable on i386, the plt entry requires ebx
|
|
|
|
// to be set. This causes two problems:
|
|
|
|
// * If some code has a direct reference to a function, it was probably
|
|
|
|
// compiled without -fPIE/-fPIC and doesn't maintain ebx.
|
|
|
|
// * If a library definition gets preempted to the executable, it will have
|
|
|
|
// the wrong ebx value.
|
2019-08-19 22:30:12 +08:00
|
|
|
if (sym.isFunc()) {
|
|
|
|
if (config->pie && config->emachine == EM_386)
|
|
|
|
errorOrWarn("symbol '" + toString(sym) +
|
|
|
|
"' cannot be preempted; recompile with -fPIE" +
|
|
|
|
getLocation(sec, sym, offset));
|
2021-12-15 08:28:41 +08:00
|
|
|
sym.needsCopy = true;
|
|
|
|
sym.needsPlt = true;
|
2019-08-19 22:30:12 +08:00
|
|
|
sec.relocations.push_back({expr, type, offset, addend, &sym});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 04:12:26 +08:00
|
|
|
errorOrWarn("relocation " + toString(type) + " cannot be used against " +
|
|
|
|
(sym.getName().empty() ? "local symbol"
|
|
|
|
: "symbol '" + toString(sym) + "'") +
|
|
|
|
"; recompile with -fPIC" + getLocation(sec, sym, offset));
|
2018-01-09 08:13:54 +08:00
|
|
|
}
|
|
|
|
|
2021-08-30 04:11:35 +08:00
|
|
|
// This function is similar to the `handleTlsRelocation`. MIPS does not
|
|
|
|
// support any relaxations for TLS relocations so by factoring out MIPS
|
|
|
|
// handling in to the separate function we can simplify the code and do not
|
|
|
|
// pollute other `handleTlsRelocation` by MIPS `ifs` statements.
|
|
|
|
// Mips has a custom MipsGotSection that handles the writing of GOT entries
|
|
|
|
// without dynamic relocations.
|
|
|
|
static unsigned handleMipsTlsRelocation(RelType type, Symbol &sym,
|
|
|
|
InputSectionBase &c, uint64_t offset,
|
|
|
|
int64_t addend, RelExpr expr) {
|
|
|
|
if (expr == R_MIPS_TLSLD) {
|
|
|
|
in.mipsGot->addTlsIndex(*c.file);
|
|
|
|
c.relocations.push_back({expr, type, offset, addend, &sym});
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (expr == R_MIPS_TLSGD) {
|
|
|
|
in.mipsGot->addDynTlsEntry(*c.file, sym);
|
|
|
|
c.relocations.push_back({expr, type, offset, addend, &sym});
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notes about General Dynamic and Local Dynamic TLS models below. They may
|
|
|
|
// require the generation of a pair of GOT entries that have associated dynamic
|
|
|
|
// relocations. The pair of GOT entries created are of the form GOT[e0] Module
|
|
|
|
// Index (Used to find pointer to TLS block at run-time) GOT[e1] Offset of
|
|
|
|
// symbol in TLS block.
|
|
|
|
//
|
|
|
|
// Returns the number of relocations processed.
|
2021-12-26 06:23:12 +08:00
|
|
|
static unsigned handleTlsRelocation(RelType type, Symbol &sym,
|
|
|
|
InputSectionBase &c, uint64_t offset,
|
|
|
|
int64_t addend, RelExpr expr) {
|
2021-08-30 04:11:35 +08:00
|
|
|
if (!sym.isTls())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (config->emachine == EM_MIPS)
|
|
|
|
return handleMipsTlsRelocation(type, sym, c, offset, addend, expr);
|
|
|
|
|
2021-10-29 08:52:03 +08:00
|
|
|
if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC,
|
|
|
|
R_TLSDESC_GOTPLT>(expr) &&
|
2021-08-30 04:11:35 +08:00
|
|
|
config->shared) {
|
2021-12-25 14:36:49 +08:00
|
|
|
if (expr != R_TLSDESC_CALL) {
|
|
|
|
sym.needsTlsDesc = true;
|
2021-08-30 04:11:35 +08:00
|
|
|
c.relocations.push_back({expr, type, offset, addend, &sym});
|
2021-12-25 14:36:49 +08:00
|
|
|
}
|
2021-08-30 04:11:35 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ARM, Hexagon and RISC-V do not support GD/LD to IE/LE relaxation. For
|
|
|
|
// PPC64, if the file has missing R_PPC64_TLSGD/R_PPC64_TLSLD, disable
|
|
|
|
// relaxation as well.
|
|
|
|
bool toExecRelax = !config->shared && config->emachine != EM_ARM &&
|
|
|
|
config->emachine != EM_HEXAGON &&
|
|
|
|
config->emachine != EM_RISCV &&
|
|
|
|
!c.file->ppc64DisableTLSRelax;
|
|
|
|
|
|
|
|
// If we are producing an executable and the symbol is non-preemptable, it
|
|
|
|
// must be defined and the code sequence can be relaxed to use Local-Exec.
|
|
|
|
//
|
|
|
|
// ARM and RISC-V do not support any relaxations for TLS relocations, however,
|
|
|
|
// we can omit the DTPMOD dynamic relocations and resolve them at link time
|
|
|
|
// because them are always 1. This may be necessary for static linking as
|
|
|
|
// DTPMOD may not be expected at load time.
|
|
|
|
bool isLocalInExecutable = !sym.isPreemptible && !config->shared;
|
|
|
|
|
|
|
|
// Local Dynamic is for access to module local TLS variables, while still
|
|
|
|
// being suitable for being dynamically loaded via dlopen. GOT[e0] is the
|
|
|
|
// module index, with a special value of 0 for the current module. GOT[e1] is
|
|
|
|
// unused. There only needs to be one module index entry.
|
|
|
|
if (oneof<R_TLSLD_GOT, R_TLSLD_GOTPLT, R_TLSLD_PC, R_TLSLD_HINT>(
|
|
|
|
expr)) {
|
|
|
|
// Local-Dynamic relocs can be relaxed to Local-Exec.
|
|
|
|
if (toExecRelax) {
|
|
|
|
c.relocations.push_back(
|
|
|
|
{target->adjustTlsExpr(type, R_RELAX_TLS_LD_TO_LE), type, offset,
|
|
|
|
addend, &sym});
|
|
|
|
return target->getTlsGdRelaxSkip(type);
|
|
|
|
}
|
|
|
|
if (expr == R_TLSLD_HINT)
|
|
|
|
return 1;
|
2022-02-08 02:26:16 +08:00
|
|
|
config->needsTlsLd = true;
|
2021-08-30 04:11:35 +08:00
|
|
|
c.relocations.push_back({expr, type, offset, addend, &sym});
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local-Dynamic relocs can be relaxed to Local-Exec.
|
2021-11-01 11:30:00 +08:00
|
|
|
if (expr == R_DTPREL) {
|
|
|
|
if (toExecRelax)
|
|
|
|
expr = target->adjustTlsExpr(type, R_RELAX_TLS_LD_TO_LE);
|
|
|
|
c.relocations.push_back({expr, type, offset, addend, &sym});
|
2021-08-30 04:11:35 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local-Dynamic sequence where offset of tls variable relative to dynamic
|
|
|
|
// thread pointer is stored in the got. This cannot be relaxed to Local-Exec.
|
|
|
|
if (expr == R_TLSLD_GOT_OFF) {
|
2021-12-25 14:36:49 +08:00
|
|
|
sym.needsGotDtprel = true;
|
2021-08-30 04:11:35 +08:00
|
|
|
c.relocations.push_back({expr, type, offset, addend, &sym});
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC,
|
2021-10-29 08:52:03 +08:00
|
|
|
R_TLSDESC_GOTPLT, R_TLSGD_GOT, R_TLSGD_GOTPLT, R_TLSGD_PC>(expr)) {
|
2021-08-30 04:11:35 +08:00
|
|
|
if (!toExecRelax) {
|
2021-12-25 14:36:49 +08:00
|
|
|
sym.needsTlsGd = true;
|
2021-08-30 04:11:35 +08:00
|
|
|
c.relocations.push_back({expr, type, offset, addend, &sym});
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
|
|
|
|
// depending on the symbol being locally defined or not.
|
|
|
|
if (sym.isPreemptible) {
|
2021-12-25 14:36:49 +08:00
|
|
|
sym.needsTlsGdToIe = true;
|
2021-08-30 04:11:35 +08:00
|
|
|
c.relocations.push_back(
|
|
|
|
{target->adjustTlsExpr(type, R_RELAX_TLS_GD_TO_IE), type, offset,
|
|
|
|
addend, &sym});
|
|
|
|
} else {
|
|
|
|
c.relocations.push_back(
|
|
|
|
{target->adjustTlsExpr(type, R_RELAX_TLS_GD_TO_LE), type, offset,
|
|
|
|
addend, &sym});
|
|
|
|
}
|
|
|
|
return target->getTlsGdRelaxSkip(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oneof<R_GOT, R_GOTPLT, R_GOT_PC, R_AARCH64_GOT_PAGE_PC, R_GOT_OFF,
|
2021-08-30 04:21:06 +08:00
|
|
|
R_TLSIE_HINT>(expr)) {
|
|
|
|
// Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
|
|
|
|
// defined.
|
|
|
|
if (toExecRelax && isLocalInExecutable) {
|
|
|
|
c.relocations.push_back(
|
|
|
|
{R_RELAX_TLS_IE_TO_LE, type, offset, addend, &sym});
|
|
|
|
} else if (expr != R_TLSIE_HINT) {
|
2021-12-25 14:36:49 +08:00
|
|
|
sym.needsTlsIe = true;
|
2021-11-01 03:38:36 +08:00
|
|
|
// R_GOT needs a relative relocation for PIC on i386 and Hexagon.
|
|
|
|
if (expr == R_GOT && config->isPic && !target->usesOnlyLowPageBits(type))
|
2021-12-23 12:51:11 +08:00
|
|
|
addRelativeReloc(c, offset, sym, addend, expr, type);
|
2021-11-01 01:46:42 +08:00
|
|
|
else
|
|
|
|
c.relocations.push_back({expr, type, offset, addend, &sym});
|
2021-08-30 04:21:06 +08:00
|
|
|
}
|
2021-08-30 04:11:35 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-12 01:54:53 +08:00
|
|
|
template <class ELFT, class RelTy> void RelocationScanner::scanOne(RelTy *&i) {
|
2018-01-09 06:20:44 +08:00
|
|
|
const RelTy &rel = *i;
|
2019-05-21 10:38:11 +08:00
|
|
|
uint32_t symIndex = rel.getSymbol(config->isMips64EL);
|
|
|
|
Symbol &sym = sec.getFile<ELFT>()->getSymbol(symIndex);
|
2018-01-09 06:20:44 +08:00
|
|
|
RelType type;
|
2016-07-20 19:47:50 +08:00
|
|
|
|
2018-01-09 06:20:44 +08:00
|
|
|
// Deal with MIPS oddity.
|
|
|
|
if (config->mipsN32Abi) {
|
2022-01-12 01:54:53 +08:00
|
|
|
type = getMipsN32RelType(i);
|
2018-01-09 06:20:44 +08:00
|
|
|
} else {
|
|
|
|
type = rel.getType(config->isMips64EL);
|
|
|
|
++i;
|
|
|
|
}
|
2017-12-07 06:32:19 +08:00
|
|
|
|
2018-01-09 06:20:44 +08:00
|
|
|
// Get an offset in an output section this relocation is applied to.
|
2022-01-12 01:54:53 +08:00
|
|
|
uint64_t offset = getter.get(rel.r_offset);
|
2018-01-09 06:20:44 +08:00
|
|
|
if (offset == uint64_t(-1))
|
|
|
|
return;
|
2016-05-25 04:24:43 +08:00
|
|
|
|
2019-05-21 10:38:11 +08:00
|
|
|
// Error if the target symbol is undefined. Symbol index 0 may be used by
|
|
|
|
// marker relocations, e.g. R_*_NONE and R_ARM_V4BX. Don't error on them.
|
2021-12-16 16:27:19 +08:00
|
|
|
if (sym.isUndefined() && symIndex != 0 &&
|
2022-01-17 16:05:27 +08:00
|
|
|
maybeReportUndefined(cast<Undefined>(sym), sec, offset))
|
2018-01-09 06:20:44 +08:00
|
|
|
return;
|
2016-11-06 06:58:01 +08:00
|
|
|
|
2022-02-21 16:39:26 +08:00
|
|
|
const uint8_t *relocatedAddr = sec.rawData.begin() + offset;
|
2022-01-12 01:54:53 +08:00
|
|
|
RelExpr expr = target.getRelExpr(type, sym, relocatedAddr);
|
2017-03-26 10:26:33 +08:00
|
|
|
|
2019-12-23 09:01:53 +08:00
|
|
|
// Ignore R_*_NONE and other marker relocations.
|
|
|
|
if (expr == R_NONE)
|
2018-01-09 06:20:44 +08:00
|
|
|
return;
|
2017-10-12 02:37:24 +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
|
|
|
// Read an addend.
|
2022-01-12 01:54:53 +08:00
|
|
|
int64_t addend = computeAddend<ELFT>(rel, expr, sym.isLocal());
|
[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
|
|
|
|
[ELF][PPC64] Suppress toc-indirect to toc-relative relaxation if R_PPC64_TOC16_LO is seen
The current implementation assumes that R_PPC64_TOC16_HA is always followed
by R_PPC64_TOC16_LO_DS. This can break with R_PPC64_TOC16_LO:
// Load the address of the TOC entry, instead of the value stored at that address
addis 3, 2, .LC0@tloc@ha # R_PPC64_TOC16_HA
addi 3, 3, .LC0@tloc@l # R_PPC64_TOC16_LO
blr
which is used by boringssl's util/fipstools/delocate/delocate.go
https://github.com/google/boringssl/blob/master/crypto/fipsmodule/FIPS.md has some documentation.
In short, this tool converts an assembly file to avoid any potential relocations.
The distance to an input .toc is not a constant after linking, so it cannot use an `addis;ld` pair.
Instead, it jumps to a stub which loads the TOC entry address with `addis;addi`.
This patch checks the presence of R_PPC64_TOC16_LO and suppresses
toc-indirect to toc-relative relaxation if R_PPC64_TOC16_LO is seen.
This approach is conservative and loses some relaxation opportunities but is easy to implement.
addis 3, 2, .LC0@toc@ha # no relaxation
addi 3, 3, .LC0@toc@l # no relaxation
li 9, 0
addis 4, 2, .LC0@toc@ha # can relax but suppressed
ld 4, .LC0@toc@l(4) # can relax but suppressed
Also note that interleaved R_PPC64_TOC16_HA and R_PPC64_TOC16_LO_DS is
possible and this patch accounts for that.
addis 3, 2, .LC1@toc@ha # can relax
addis 4, 2, .LC2@toc@ha # can relax
ld 3, .LC1@toc@l(3) # can relax
ld 4, .LC2@toc@l(4) # can relax
Reviewed By: #powerpc, sfertile
Differential Revision: https://reviews.llvm.org/D78431
2020-04-18 14:08:11 +08:00
|
|
|
if (config->emachine == EM_PPC64) {
|
|
|
|
// We can separate the small code model relocations into 2 categories:
|
|
|
|
// 1) Those that access the compiler generated .toc sections.
|
|
|
|
// 2) Those that access the linker allocated got entries.
|
|
|
|
// lld allocates got entries to symbols on demand. Since we don't try to
|
|
|
|
// sort the got entries in any way, we don't have to track which objects
|
|
|
|
// have got-based small code model relocs. The .toc sections get placed
|
|
|
|
// after the end of the linker allocated .got section and we do sort those
|
|
|
|
// so sections addressed with small code model relocations come first.
|
2021-11-10 12:41:05 +08:00
|
|
|
if (type == R_PPC64_TOC16 || type == R_PPC64_TOC16_DS)
|
[ELF][PPC64] Suppress toc-indirect to toc-relative relaxation if R_PPC64_TOC16_LO is seen
The current implementation assumes that R_PPC64_TOC16_HA is always followed
by R_PPC64_TOC16_LO_DS. This can break with R_PPC64_TOC16_LO:
// Load the address of the TOC entry, instead of the value stored at that address
addis 3, 2, .LC0@tloc@ha # R_PPC64_TOC16_HA
addi 3, 3, .LC0@tloc@l # R_PPC64_TOC16_LO
blr
which is used by boringssl's util/fipstools/delocate/delocate.go
https://github.com/google/boringssl/blob/master/crypto/fipsmodule/FIPS.md has some documentation.
In short, this tool converts an assembly file to avoid any potential relocations.
The distance to an input .toc is not a constant after linking, so it cannot use an `addis;ld` pair.
Instead, it jumps to a stub which loads the TOC entry address with `addis;addi`.
This patch checks the presence of R_PPC64_TOC16_LO and suppresses
toc-indirect to toc-relative relaxation if R_PPC64_TOC16_LO is seen.
This approach is conservative and loses some relaxation opportunities but is easy to implement.
addis 3, 2, .LC0@toc@ha # no relaxation
addi 3, 3, .LC0@toc@l # no relaxation
li 9, 0
addis 4, 2, .LC0@toc@ha # can relax but suppressed
ld 4, .LC0@toc@l(4) # can relax but suppressed
Also note that interleaved R_PPC64_TOC16_HA and R_PPC64_TOC16_LO_DS is
possible and this patch accounts for that.
addis 3, 2, .LC1@toc@ha # can relax
addis 4, 2, .LC2@toc@ha # can relax
ld 3, .LC1@toc@l(3) # can relax
ld 4, .LC2@toc@l(4) # can relax
Reviewed By: #powerpc, sfertile
Differential Revision: https://reviews.llvm.org/D78431
2020-04-18 14:08:11 +08:00
|
|
|
sec.file->ppc64SmallCodeModelTocRelocs = true;
|
|
|
|
|
|
|
|
// Record the TOC entry (.toc + addend) as not relaxable. See the comment in
|
|
|
|
// InputSectionBase::relocateAlloc().
|
|
|
|
if (type == R_PPC64_TOC16_LO && sym.isSection() && isa<Defined>(sym) &&
|
|
|
|
cast<Defined>(sym).section->name == ".toc")
|
|
|
|
ppc64noTocRelax.insert({&sym, addend});
|
2020-10-02 03:36:08 +08:00
|
|
|
|
2020-10-23 21:22:00 +08:00
|
|
|
if ((type == R_PPC64_TLSGD && expr == R_TLSDESC_CALL) ||
|
|
|
|
(type == R_PPC64_TLSLD && expr == R_TLSLD_HINT)) {
|
2020-10-02 03:36:08 +08:00
|
|
|
if (i == end) {
|
2020-10-23 21:22:00 +08:00
|
|
|
errorOrWarn("R_PPC64_TLSGD/R_PPC64_TLSLD may not be the last "
|
|
|
|
"relocation" +
|
2020-10-02 03:36:08 +08:00
|
|
|
getLocation(sec, sym, offset));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Offset the 4-byte aligned R_PPC64_TLSGD by one byte in the NOTOC case,
|
|
|
|
// so we can discern it later from the toc-case.
|
|
|
|
if (i->getType(/*isMips64EL=*/false) == R_PPC64_REL24_NOTOC)
|
|
|
|
++offset;
|
|
|
|
}
|
[ELF][PPC64] Suppress toc-indirect to toc-relative relaxation if R_PPC64_TOC16_LO is seen
The current implementation assumes that R_PPC64_TOC16_HA is always followed
by R_PPC64_TOC16_LO_DS. This can break with R_PPC64_TOC16_LO:
// Load the address of the TOC entry, instead of the value stored at that address
addis 3, 2, .LC0@tloc@ha # R_PPC64_TOC16_HA
addi 3, 3, .LC0@tloc@l # R_PPC64_TOC16_LO
blr
which is used by boringssl's util/fipstools/delocate/delocate.go
https://github.com/google/boringssl/blob/master/crypto/fipsmodule/FIPS.md has some documentation.
In short, this tool converts an assembly file to avoid any potential relocations.
The distance to an input .toc is not a constant after linking, so it cannot use an `addis;ld` pair.
Instead, it jumps to a stub which loads the TOC entry address with `addis;addi`.
This patch checks the presence of R_PPC64_TOC16_LO and suppresses
toc-indirect to toc-relative relaxation if R_PPC64_TOC16_LO is seen.
This approach is conservative and loses some relaxation opportunities but is easy to implement.
addis 3, 2, .LC0@toc@ha # no relaxation
addi 3, 3, .LC0@toc@l # no relaxation
li 9, 0
addis 4, 2, .LC0@toc@ha # can relax but suppressed
ld 4, .LC0@toc@l(4) # can relax but suppressed
Also note that interleaved R_PPC64_TOC16_HA and R_PPC64_TOC16_LO_DS is
possible and this patch accounts for that.
addis 3, 2, .LC1@toc@ha # can relax
addis 4, 2, .LC2@toc@ha # can relax
ld 3, .LC1@toc@l(3) # can relax
ld 4, .LC2@toc@l(4) # can relax
Reviewed By: #powerpc, sfertile
Differential Revision: https://reviews.llvm.org/D78431
2020-04-18 14:08:11 +08:00
|
|
|
}
|
|
|
|
|
[ELF] Change GOT*_FROM_END (relative to end(.got)) to GOTPLT* (start(.got.plt))
Summary:
This should address remaining issues discussed in PR36555.
Currently R_GOT*_FROM_END are exclusively used by x86 and x86_64 to
express relocations types relative to the GOT base. We have
_GLOBAL_OFFSET_TABLE_ (GOT base) = start(.got.plt) but end(.got) !=
start(.got.plt)
This can have problems when _GLOBAL_OFFSET_TABLE_ is used as a symbol, e.g.
glibc dl_machine_dynamic assumes _GLOBAL_OFFSET_TABLE_ is start(.got.plt),
which is not true.
extern const ElfW(Addr) _GLOBAL_OFFSET_TABLE_[] attribute_hidden;
return _GLOBAL_OFFSET_TABLE_[0]; // R_X86_64_GOTPC32
In this patch, we
* Change all GOT*_FROM_END to GOTPLT* to fix the problem.
* Add HasGotPltOffRel to denote whether .got.plt should be kept even if
the section is empty.
* Simplify GotSection::empty and GotPltSection::empty by setting
HasGotOffRel and HasGotPltOffRel according to GlobalOffsetTable early.
The change of R_386_GOTPC makes X86::writePltHeader simpler as we don't
have to compute the offset start(.got.plt) - Ebx (it is constant 0).
We still diverge from ld.bfd (at least in most cases) and gold in that
.got.plt and .got are not adjacent, but the advantage doing that is
unclear.
Reviewers: ruiu, sivachandra, espindola
Subscribers: emaste, mehdi_amini, arichardson, dexonsmith, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59594
llvm-svn: 356968
2019-03-26 07:46:19 +08:00
|
|
|
// If the relocation does not emit a GOT or GOTPLT entry but its computation
|
|
|
|
// uses their addresses, we need GOT or GOTPLT to be created.
|
|
|
|
//
|
2021-10-26 04:05:17 +08:00
|
|
|
// The 5 types that relative GOTPLT are all x86 and x86-64 specific.
|
|
|
|
if (oneof<R_GOTPLTONLY_PC, R_GOTPLTREL, R_GOTPLT, R_PLT_GOTPLT,
|
2021-10-29 08:52:03 +08:00
|
|
|
R_TLSDESC_GOTPLT, R_TLSGD_GOTPLT>(expr)) {
|
[ELF] Change GOT*_FROM_END (relative to end(.got)) to GOTPLT* (start(.got.plt))
Summary:
This should address remaining issues discussed in PR36555.
Currently R_GOT*_FROM_END are exclusively used by x86 and x86_64 to
express relocations types relative to the GOT base. We have
_GLOBAL_OFFSET_TABLE_ (GOT base) = start(.got.plt) but end(.got) !=
start(.got.plt)
This can have problems when _GLOBAL_OFFSET_TABLE_ is used as a symbol, e.g.
glibc dl_machine_dynamic assumes _GLOBAL_OFFSET_TABLE_ is start(.got.plt),
which is not true.
extern const ElfW(Addr) _GLOBAL_OFFSET_TABLE_[] attribute_hidden;
return _GLOBAL_OFFSET_TABLE_[0]; // R_X86_64_GOTPC32
In this patch, we
* Change all GOT*_FROM_END to GOTPLT* to fix the problem.
* Add HasGotPltOffRel to denote whether .got.plt should be kept even if
the section is empty.
* Simplify GotSection::empty and GotPltSection::empty by setting
HasGotOffRel and HasGotPltOffRel according to GlobalOffsetTable early.
The change of R_386_GOTPC makes X86::writePltHeader simpler as we don't
have to compute the offset start(.got.plt) - Ebx (it is constant 0).
We still diverge from ld.bfd (at least in most cases) and gold in that
.got.plt and .got are not adjacent, but the advantage doing that is
unclear.
Reviewers: ruiu, sivachandra, espindola
Subscribers: emaste, mehdi_amini, arichardson, dexonsmith, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59594
llvm-svn: 356968
2019-03-26 07:46:19 +08:00
|
|
|
in.gotPlt->hasGotPltOffRel = true;
|
2021-12-03 07:28:37 +08:00
|
|
|
} else if (oneof<R_GOTONLY_PC, R_GOTREL, R_PPC32_PLTREL, R_PPC64_TOCBASE,
|
|
|
|
R_PPC64_RELAX_TOC>(expr)) {
|
2018-09-26 03:26:58 +08:00
|
|
|
in.got->hasGotOffRel = true;
|
[ELF] Change GOT*_FROM_END (relative to end(.got)) to GOTPLT* (start(.got.plt))
Summary:
This should address remaining issues discussed in PR36555.
Currently R_GOT*_FROM_END are exclusively used by x86 and x86_64 to
express relocations types relative to the GOT base. We have
_GLOBAL_OFFSET_TABLE_ (GOT base) = start(.got.plt) but end(.got) !=
start(.got.plt)
This can have problems when _GLOBAL_OFFSET_TABLE_ is used as a symbol, e.g.
glibc dl_machine_dynamic assumes _GLOBAL_OFFSET_TABLE_ is start(.got.plt),
which is not true.
extern const ElfW(Addr) _GLOBAL_OFFSET_TABLE_[] attribute_hidden;
return _GLOBAL_OFFSET_TABLE_[0]; // R_X86_64_GOTPC32
In this patch, we
* Change all GOT*_FROM_END to GOTPLT* to fix the problem.
* Add HasGotPltOffRel to denote whether .got.plt should be kept even if
the section is empty.
* Simplify GotSection::empty and GotPltSection::empty by setting
HasGotOffRel and HasGotPltOffRel according to GlobalOffsetTable early.
The change of R_386_GOTPC makes X86::writePltHeader simpler as we don't
have to compute the offset start(.got.plt) - Ebx (it is constant 0).
We still diverge from ld.bfd (at least in most cases) and gold in that
.got.plt and .got are not adjacent, but the advantage doing that is
unclear.
Reviewers: ruiu, sivachandra, espindola
Subscribers: emaste, mehdi_amini, arichardson, dexonsmith, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D59594
llvm-svn: 356968
2019-03-26 07:46:19 +08:00
|
|
|
}
|
2017-10-12 13:32:52 +08:00
|
|
|
|
2020-12-22 00:47:04 +08:00
|
|
|
// Process TLS relocations, including relaxing TLS relocations. Note that
|
2022-01-12 01:54:53 +08:00
|
|
|
// R_TPREL and R_TPREL_NEG relocations are resolved in processAux.
|
2020-12-22 00:47:04 +08:00
|
|
|
if (expr == R_TPREL || expr == R_TPREL_NEG) {
|
|
|
|
if (config->shared) {
|
|
|
|
errorOrWarn("relocation " + toString(type) + " against " + toString(sym) +
|
|
|
|
" cannot be used with -shared" +
|
|
|
|
getLocation(sec, sym, offset));
|
|
|
|
return;
|
|
|
|
}
|
2021-12-26 06:23:12 +08:00
|
|
|
} else if (unsigned processed =
|
|
|
|
handleTlsRelocation(type, sym, sec, offset, addend, expr)) {
|
2018-01-09 06:20:44 +08:00
|
|
|
i += (processed - 1);
|
|
|
|
return;
|
|
|
|
}
|
2016-05-25 04:24:43 +08:00
|
|
|
|
2021-11-30 15:10:04 +08:00
|
|
|
// Relax relocations.
|
|
|
|
//
|
|
|
|
// If we know that a PLT entry will be resolved within the same ELF module, we
|
|
|
|
// can skip PLT access and directly jump to the destination function. For
|
|
|
|
// example, if we are linking a main executable, all dynamic symbols that can
|
|
|
|
// be resolved within the executable will actually be resolved that way at
|
|
|
|
// runtime, because the main executable is always at the beginning of a search
|
|
|
|
// list. We can leverage that fact.
|
|
|
|
if (!sym.isPreemptible && (!sym.isGnuIFunc() || config->zIfuncNoplt)) {
|
|
|
|
if (expr != R_GOT_PC) {
|
|
|
|
// The 0x8000 bit of r_addend of R_PPC_PLTREL24 is used to choose call
|
|
|
|
// stub type. It should be ignored if optimized to R_PC.
|
|
|
|
if (config->emachine == EM_PPC && expr == R_PPC32_PLTREL)
|
|
|
|
addend &= ~0x8000;
|
|
|
|
// R_HEX_GD_PLT_B22_PCREL (call a@GDPLT) is transformed into
|
|
|
|
// call __tls_get_addr even if the symbol is non-preemptible.
|
|
|
|
if (!(config->emachine == EM_HEXAGON &&
|
|
|
|
(type == R_HEX_GD_PLT_B22_PCREL ||
|
|
|
|
type == R_HEX_GD_PLT_B22_PCREL_X ||
|
|
|
|
type == R_HEX_GD_PLT_B32_PCREL_X)))
|
|
|
|
expr = fromPlt(expr);
|
|
|
|
} else if (!isAbsoluteValue(sym)) {
|
2022-01-12 01:54:53 +08:00
|
|
|
expr = target.adjustGotPcExpr(type, addend, relocatedAddr);
|
2021-11-30 15:10:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 23:25:21 +08:00
|
|
|
// We were asked not to generate PLT entries for ifuncs. Instead, pass the
|
|
|
|
// direct relocation on through.
|
|
|
|
if (sym.isGnuIFunc() && config->zIfuncNoplt) {
|
|
|
|
sym.exportDynamic = true;
|
2021-12-23 13:09:57 +08:00
|
|
|
mainPart->relaDyn->addSymbolReloc(type, sec, offset, sym, addend, type);
|
2019-05-14 23:25:21 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-15 08:28:41 +08:00
|
|
|
if (needsGot(expr)) {
|
|
|
|
if (config->emachine == EM_MIPS) {
|
|
|
|
// MIPS ABI has special rules to process GOT entries and doesn't
|
|
|
|
// require relocation entries for them. A special case is TLS
|
|
|
|
// relocations. In that case dynamic loader applies dynamic
|
|
|
|
// relocations to initialize TLS GOT entries.
|
|
|
|
// See "Global Offset Table" in Chapter 5 in the following document
|
|
|
|
// for detailed description:
|
|
|
|
// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
|
|
|
|
in.mipsGot->addEntry(*sec.file, sym, addend, expr);
|
|
|
|
} else {
|
|
|
|
sym.needsGot = true;
|
2019-02-14 05:49:55 +08:00
|
|
|
}
|
2021-12-15 08:28:41 +08:00
|
|
|
} else if (needsPlt(expr)) {
|
|
|
|
sym.needsPlt = true;
|
2019-02-14 05:49:55 +08:00
|
|
|
} else {
|
2021-12-15 08:28:41 +08:00
|
|
|
sym.hasDirectReloc = true;
|
2018-01-09 06:20:44 +08:00
|
|
|
}
|
2018-04-27 01:22:44 +08:00
|
|
|
|
2022-01-12 01:54:53 +08:00
|
|
|
processAux(expr, type, offset, sym, addend);
|
2018-01-09 06:20:44 +08:00
|
|
|
}
|
2017-04-04 05:36:31 +08:00
|
|
|
|
[ELF][PPC64] Detect missing R_PPC64_TLSGD/R_PPC64_TLSLD and disable TLS relaxation
Alternative to D91611.
The TLS General Dynamic/Local Dynamic code sequences need to mark
`__tls_get_addr` with R_PPC64_TLSGD or R_PPC64_TLSLD, e.g.
```
addis r3, r2, x@got@tlsgd@ha # R_PPC64_GOT_TLSGD16_HA
addi r3, r3, x@got@tlsgd@l # R_PPC64_GOT_TLSGD16_LO
bl __tls_get_addr(x@tlsgd) # R_PPC64_TLSGD followed by R_PPC64_REL24
nop
```
However, there are two deviations form the above:
1. direct call to `__tls_get_addr`. This is essential to implement ld.so in glibc/musl/FreeBSD.
```
bl __tls_get_addr
nop
```
This is only used in a -shared link, and thus not subject to the GD/LD to IE/LE
relaxation issue below.
2. Missing R_PPC64_TLSGD/R_PPC64_TLSGD for compiler generated TLS references
According to Stefan Pintille, "In the early days of the transition from the
ELFv1 ABI that is used for big endian PowerPC Linux distributions to the ELFv2
ABI that is used for little endian PowerPC Linux distributions, there was some
ambiguity in the specification of the relocations for TLS. The GNU linker has
implemented support for correct handling of calls to __tls_get_addr with a
missing relocation. Unfortunately, we didn't notice that the IBM XL compiler
did not handle TLS according to the updated ABI until we tried linking XL
compiled libraries with LLD."
In short, LLD needs to work around the old IBM XL compiler issue.
Otherwise, if the object file is linked in -no-pie or -pie mode,
the result will be incorrect because the 4 instructions are partially
rewritten (the latter 2 are not changed).
Work around the compiler bug by disable General Dynamic/Local Dynamic to
Initial Exec/Local Exec relaxation. Note, we also disable Initial Exec
to Local Exec relaxation for implementation simplicity, though technically it can be kept.
ppc64-tls-missing-gdld.s demonstrates the updated behavior.
Reviewed By: #powerpc, stefanp, grimar
Differential Revision: https://reviews.llvm.org/D92959
2020-12-22 00:45:41 +08:00
|
|
|
// R_PPC64_TLSGD/R_PPC64_TLSLD is required to mark `bl __tls_get_addr` for
|
|
|
|
// General Dynamic/Local Dynamic code sequences. If a GD/LD GOT relocation is
|
|
|
|
// found but no R_PPC64_TLSGD/R_PPC64_TLSLD is seen, we assume that the
|
|
|
|
// instructions are generated by very old IBM XL compilers. Work around the
|
|
|
|
// issue by disabling GD/LD to IE/LE relaxation.
|
|
|
|
template <class RelTy>
|
|
|
|
static void checkPPC64TLSRelax(InputSectionBase &sec, ArrayRef<RelTy> rels) {
|
|
|
|
// Skip if sec is synthetic (sec.file is null) or if sec has been marked.
|
|
|
|
if (!sec.file || sec.file->ppc64DisableTLSRelax)
|
|
|
|
return;
|
|
|
|
bool hasGDLD = false;
|
|
|
|
for (const RelTy &rel : rels) {
|
|
|
|
RelType type = rel.getType(false);
|
|
|
|
switch (type) {
|
|
|
|
case R_PPC64_TLSGD:
|
|
|
|
case R_PPC64_TLSLD:
|
|
|
|
return; // Found a marker
|
|
|
|
case R_PPC64_GOT_TLSGD16:
|
|
|
|
case R_PPC64_GOT_TLSGD16_HA:
|
|
|
|
case R_PPC64_GOT_TLSGD16_HI:
|
|
|
|
case R_PPC64_GOT_TLSGD16_LO:
|
|
|
|
case R_PPC64_GOT_TLSLD16:
|
|
|
|
case R_PPC64_GOT_TLSLD16_HA:
|
|
|
|
case R_PPC64_GOT_TLSLD16_HI:
|
|
|
|
case R_PPC64_GOT_TLSLD16_LO:
|
|
|
|
hasGDLD = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hasGDLD) {
|
|
|
|
sec.file->ppc64DisableTLSRelax = true;
|
|
|
|
warn(toString(sec.file) +
|
|
|
|
": disable TLS relaxation due to R_PPC64_GOT_TLS* relocations without "
|
|
|
|
"R_PPC64_TLSGD/R_PPC64_TLSLD relocations");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 06:20:44 +08:00
|
|
|
template <class ELFT, class RelTy>
|
2022-01-12 01:54:53 +08:00
|
|
|
void RelocationScanner::scan(ArrayRef<RelTy> rels) {
|
2018-01-09 06:20:44 +08:00
|
|
|
// Not all relocations end up in Sec.Relocations, but a lot do.
|
|
|
|
sec.relocations.reserve(rels.size());
|
2017-10-13 13:58:54 +08:00
|
|
|
|
[ELF][PPC64] Detect missing R_PPC64_TLSGD/R_PPC64_TLSLD and disable TLS relaxation
Alternative to D91611.
The TLS General Dynamic/Local Dynamic code sequences need to mark
`__tls_get_addr` with R_PPC64_TLSGD or R_PPC64_TLSLD, e.g.
```
addis r3, r2, x@got@tlsgd@ha # R_PPC64_GOT_TLSGD16_HA
addi r3, r3, x@got@tlsgd@l # R_PPC64_GOT_TLSGD16_LO
bl __tls_get_addr(x@tlsgd) # R_PPC64_TLSGD followed by R_PPC64_REL24
nop
```
However, there are two deviations form the above:
1. direct call to `__tls_get_addr`. This is essential to implement ld.so in glibc/musl/FreeBSD.
```
bl __tls_get_addr
nop
```
This is only used in a -shared link, and thus not subject to the GD/LD to IE/LE
relaxation issue below.
2. Missing R_PPC64_TLSGD/R_PPC64_TLSGD for compiler generated TLS references
According to Stefan Pintille, "In the early days of the transition from the
ELFv1 ABI that is used for big endian PowerPC Linux distributions to the ELFv2
ABI that is used for little endian PowerPC Linux distributions, there was some
ambiguity in the specification of the relocations for TLS. The GNU linker has
implemented support for correct handling of calls to __tls_get_addr with a
missing relocation. Unfortunately, we didn't notice that the IBM XL compiler
did not handle TLS according to the updated ABI until we tried linking XL
compiled libraries with LLD."
In short, LLD needs to work around the old IBM XL compiler issue.
Otherwise, if the object file is linked in -no-pie or -pie mode,
the result will be incorrect because the 4 instructions are partially
rewritten (the latter 2 are not changed).
Work around the compiler bug by disable General Dynamic/Local Dynamic to
Initial Exec/Local Exec relaxation. Note, we also disable Initial Exec
to Local Exec relaxation for implementation simplicity, though technically it can be kept.
ppc64-tls-missing-gdld.s demonstrates the updated behavior.
Reviewed By: #powerpc, stefanp, grimar
Differential Revision: https://reviews.llvm.org/D92959
2020-12-22 00:45:41 +08:00
|
|
|
if (config->emachine == EM_PPC64)
|
|
|
|
checkPPC64TLSRelax<RelTy>(sec, rels);
|
|
|
|
|
2021-04-29 23:51:09 +08:00
|
|
|
// For EhInputSection, OffsetGetter expects the relocations to be sorted by
|
|
|
|
// r_offset. In rare cases (.eh_frame pieces are reordered by a linker
|
|
|
|
// script), the relocations may be unordered.
|
|
|
|
SmallVector<RelTy, 0> storage;
|
|
|
|
if (isa<EhInputSection>(sec))
|
|
|
|
rels = sortRels(rels, storage);
|
|
|
|
|
2022-01-12 01:54:53 +08:00
|
|
|
end = static_cast<const void *>(rels.end());
|
|
|
|
for (auto i = rels.begin(); i != end;)
|
|
|
|
scanOne<ELFT>(i);
|
2018-08-10 01:59:56 +08:00
|
|
|
|
[PPC64] toc-indirect to toc-relative relaxation
This is based on D54720 by Sean Fertile.
When accessing a global symbol which is not defined in the translation unit,
compilers will generate instructions that load the address from the toc entry.
If the symbol is defined, non-preemptable, and addressable with a 32-bit
signed offset from the toc pointer, the address can be computed
directly. e.g.
addis 3, 2, .LC0@toc@ha # R_PPC64_TOC16_HA
ld 3, .LC0@toc@l(3) # R_PPC64_TOC16_LO_DS, load the address from a .toc entry
ld/lwa 3, 0(3) # load the value from the address
.section .toc,"aw",@progbits
.LC0: .tc var[TC],var
can be relaxed to
addis 3,2,var@toc@ha # this may be relaxed to a nop,
addi 3,3,var@toc@l # then this becomes addi 3,2,var@toc
ld/lwa 3, 0(3) # load the value from the address
We can delete the test ppc64-got-indirect.s as its purpose is covered by
newly added ppc64-toc-relax.s and ppc64-toc-relax-constants.s
Reviewed By: ruiu, sfertile
Differential Revision: https://reviews.llvm.org/D60958
llvm-svn: 360112
2019-05-07 12:26:05 +08:00
|
|
|
// Sort relocations by offset for more efficient searching for
|
|
|
|
// R_RISCV_PCREL_HI20 and R_PPC64_ADDR64.
|
|
|
|
if (config->emachine == EM_RISCV ||
|
|
|
|
(config->emachine == EM_PPC64 && sec.name == ".toc"))
|
2019-04-23 10:42:06 +08:00
|
|
|
llvm::stable_sort(sec.relocations,
|
|
|
|
[](const Relocation &lhs, const Relocation &rhs) {
|
|
|
|
return lhs.offset < rhs.offset;
|
|
|
|
});
|
2016-05-25 04:24:43 +08:00
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
template <class ELFT> void elf::scanRelocations(InputSectionBase &s) {
|
2022-01-12 01:54:53 +08:00
|
|
|
RelocationScanner scanner(s);
|
2021-10-28 00:51:06 +08:00
|
|
|
const RelsOrRelas<ELFT> rels = s.template relsOrRelas<ELFT>();
|
|
|
|
if (rels.areRelocsRel())
|
2022-01-12 01:54:53 +08:00
|
|
|
scanner.template scan<ELFT>(rels.rels);
|
2016-05-25 04:24:43 +08:00
|
|
|
else
|
2022-01-12 01:54:53 +08:00
|
|
|
scanner.template scan<ELFT>(rels.relas);
|
2016-05-25 04:24:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-15 08:28:41 +08:00
|
|
|
static bool handleNonPreemptibleIfunc(Symbol &sym) {
|
|
|
|
// Handle a reference to a non-preemptible ifunc. These are special in a
|
|
|
|
// few ways:
|
|
|
|
//
|
|
|
|
// - Unlike most non-preemptible symbols, non-preemptible ifuncs do not have
|
|
|
|
// a fixed value. But assuming that all references to the ifunc are
|
|
|
|
// GOT-generating or PLT-generating, the handling of an ifunc is
|
|
|
|
// relatively straightforward. We create a PLT entry in Iplt, which is
|
|
|
|
// usually at the end of .plt, which makes an indirect call using a
|
|
|
|
// matching GOT entry in igotPlt, which is usually at the end of .got.plt.
|
|
|
|
// The GOT entry is relocated using an IRELATIVE relocation in relaIplt,
|
|
|
|
// which is usually at the end of .rela.plt. Unlike most relocations in
|
|
|
|
// .rela.plt, which may be evaluated lazily without -z now, dynamic
|
|
|
|
// loaders evaluate IRELATIVE relocs eagerly, which means that for
|
|
|
|
// IRELATIVE relocs only, GOT-generating relocations can point directly to
|
|
|
|
// .got.plt without requiring a separate GOT entry.
|
|
|
|
//
|
|
|
|
// - Despite the fact that an ifunc does not have a fixed value, compilers
|
|
|
|
// that are not passed -fPIC will assume that they do, and will emit
|
|
|
|
// direct (non-GOT-generating, non-PLT-generating) relocations to the
|
|
|
|
// symbol. This means that if a direct relocation to the symbol is
|
|
|
|
// seen, the linker must set a value for the symbol, and this value must
|
|
|
|
// be consistent no matter what type of reference is made to the symbol.
|
|
|
|
// This can be done by creating a PLT entry for the symbol in the way
|
|
|
|
// described above and making it canonical, that is, making all references
|
|
|
|
// point to the PLT entry instead of the resolver. In lld we also store
|
|
|
|
// the address of the PLT entry in the dynamic symbol table, which means
|
|
|
|
// that the symbol will also have the same value in other modules.
|
|
|
|
// Because the value loaded from the GOT needs to be consistent with
|
|
|
|
// the value computed using a direct relocation, a non-preemptible ifunc
|
|
|
|
// may end up with two GOT entries, one in .got.plt that points to the
|
|
|
|
// address returned by the resolver and is used only by the PLT entry,
|
|
|
|
// and another in .got that points to the PLT entry and is used by
|
|
|
|
// GOT-generating relocations.
|
|
|
|
//
|
|
|
|
// - The fact that these symbols do not have a fixed value makes them an
|
|
|
|
// exception to the general rule that a statically linked executable does
|
|
|
|
// not require any form of dynamic relocation. To handle these relocations
|
|
|
|
// correctly, the IRELATIVE relocations are stored in an array which a
|
|
|
|
// statically linked executable's startup code must enumerate using the
|
|
|
|
// linker-defined symbols __rela?_iplt_{start,end}.
|
|
|
|
if (!sym.isGnuIFunc() || sym.isPreemptible || config->zIfuncNoplt)
|
|
|
|
return false;
|
|
|
|
// Skip unreferenced non-preemptible ifunc.
|
|
|
|
if (!(sym.needsGot || sym.needsPlt || sym.hasDirectReloc))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
sym.isInIplt = true;
|
|
|
|
|
|
|
|
// Create an Iplt and the associated IRELATIVE relocation pointing to the
|
|
|
|
// original section/value pairs. For non-GOT non-PLT relocation case below, we
|
|
|
|
// may alter section/value, so create a copy of the symbol to make
|
|
|
|
// section/value fixed.
|
2021-12-16 05:15:02 +08:00
|
|
|
auto *directSym = makeDefined(cast<Defined>(sym));
|
2022-01-10 05:43:26 +08:00
|
|
|
directSym->allocateAux();
|
2021-12-23 12:51:11 +08:00
|
|
|
addPltEntry(*in.iplt, *in.igotPlt, *in.relaIplt, target->iRelativeRel,
|
2021-12-15 08:28:41 +08:00
|
|
|
*directSym);
|
2022-01-10 05:43:26 +08:00
|
|
|
sym.allocateAux();
|
|
|
|
symAux.back().pltIdx = symAux[directSym->auxIdx].pltIdx;
|
2021-12-15 08:28:41 +08:00
|
|
|
|
|
|
|
if (sym.hasDirectReloc) {
|
|
|
|
// Change the value to the IPLT and redirect all references to it.
|
|
|
|
auto &d = cast<Defined>(sym);
|
2021-12-28 10:15:23 +08:00
|
|
|
d.section = in.iplt.get();
|
2022-01-10 05:43:26 +08:00
|
|
|
d.value = d.getPltIdx() * target->ipltEntrySize;
|
2021-12-15 08:28:41 +08:00
|
|
|
d.size = 0;
|
|
|
|
// It's important to set the symbol type here so that dynamic loaders
|
|
|
|
// don't try to call the PLT as if it were an ifunc resolver.
|
|
|
|
d.type = STT_FUNC;
|
|
|
|
|
|
|
|
if (sym.needsGot)
|
|
|
|
addGotEntry(sym);
|
|
|
|
} else if (sym.needsGot) {
|
|
|
|
// Redirect GOT accesses to point to the Igot.
|
|
|
|
sym.gotInIgot = true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void elf::postScanRelocations() {
|
|
|
|
auto fn = [](Symbol &sym) {
|
|
|
|
if (handleNonPreemptibleIfunc(sym))
|
|
|
|
return;
|
2022-01-10 05:43:26 +08:00
|
|
|
if (!sym.needsDynReloc())
|
|
|
|
return;
|
|
|
|
sym.allocateAux();
|
|
|
|
|
2021-12-15 08:28:41 +08:00
|
|
|
if (sym.needsGot)
|
|
|
|
addGotEntry(sym);
|
|
|
|
if (sym.needsPlt)
|
2021-12-23 12:51:11 +08:00
|
|
|
addPltEntry(*in.plt, *in.gotPlt, *in.relaPlt, target->pltRel, sym);
|
2021-12-15 08:28:41 +08:00
|
|
|
if (sym.needsCopy) {
|
|
|
|
if (sym.isObject()) {
|
2022-03-13 06:08:10 +08:00
|
|
|
invokeELFT(addCopyRelSymbol, cast<SharedSymbol>(sym));
|
2021-12-15 08:28:41 +08:00
|
|
|
// needsCopy is cleared for sym and its aliases so that in later
|
|
|
|
// iterations aliases won't cause redundant copies.
|
|
|
|
assert(!sym.needsCopy);
|
|
|
|
} else {
|
|
|
|
assert(sym.isFunc() && sym.needsPlt);
|
|
|
|
if (!sym.isDefined()) {
|
2022-01-10 05:43:26 +08:00
|
|
|
replaceWithDefined(sym, *in.plt,
|
|
|
|
target->pltHeaderSize +
|
|
|
|
target->pltEntrySize * sym.getPltIdx(),
|
|
|
|
0);
|
2021-12-15 08:28:41 +08:00
|
|
|
sym.needsCopy = true;
|
|
|
|
if (config->emachine == EM_PPC) {
|
|
|
|
// PPC32 canonical PLT entries are at the beginning of .glink
|
|
|
|
cast<Defined>(sym).value = in.plt->headerSize;
|
|
|
|
in.plt->headerSize += 16;
|
2021-12-23 12:51:11 +08:00
|
|
|
cast<PPC32GlinkSection>(*in.plt).canonical_plts.push_back(&sym);
|
2021-12-15 08:28:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 14:36:49 +08:00
|
|
|
|
|
|
|
if (!sym.isTls())
|
|
|
|
return;
|
|
|
|
bool isLocalInExecutable = !sym.isPreemptible && !config->shared;
|
|
|
|
|
|
|
|
if (sym.needsTlsDesc) {
|
2022-01-11 02:03:21 +08:00
|
|
|
in.got->addTlsDescEntry(sym);
|
2021-12-25 14:36:49 +08:00
|
|
|
mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible(
|
2022-01-11 02:03:21 +08:00
|
|
|
target->tlsDescRel, *in.got, in.got->getTlsDescOffset(sym), sym,
|
2021-12-25 14:36:49 +08:00
|
|
|
target->tlsDescRel);
|
|
|
|
}
|
2022-01-11 02:03:21 +08:00
|
|
|
if (sym.needsTlsGd) {
|
2021-12-25 14:36:49 +08:00
|
|
|
in.got->addDynTlsEntry(sym);
|
|
|
|
uint64_t off = in.got->getGlobalDynOffset(sym);
|
|
|
|
if (isLocalInExecutable)
|
|
|
|
// Write one to the GOT slot.
|
|
|
|
in.got->relocations.push_back(
|
|
|
|
{R_ADDEND, target->symbolicRel, off, 1, &sym});
|
|
|
|
else
|
|
|
|
mainPart->relaDyn->addSymbolReloc(target->tlsModuleIndexRel, *in.got,
|
|
|
|
off, sym);
|
|
|
|
|
|
|
|
// If the symbol is preemptible we need the dynamic linker to write
|
|
|
|
// the offset too.
|
|
|
|
uint64_t offsetOff = off + config->wordsize;
|
|
|
|
if (sym.isPreemptible)
|
|
|
|
mainPart->relaDyn->addSymbolReloc(target->tlsOffsetRel, *in.got,
|
|
|
|
offsetOff, sym);
|
|
|
|
else
|
|
|
|
in.got->relocations.push_back(
|
|
|
|
{R_ABS, target->tlsOffsetRel, offsetOff, 0, &sym});
|
|
|
|
}
|
|
|
|
if (sym.needsTlsGdToIe) {
|
|
|
|
in.got->addEntry(sym);
|
|
|
|
mainPart->relaDyn->addSymbolReloc(target->tlsGotRel, *in.got,
|
|
|
|
sym.getGotOffset(), sym);
|
|
|
|
}
|
|
|
|
if (sym.needsGotDtprel) {
|
|
|
|
in.got->addEntry(sym);
|
|
|
|
in.got->relocations.push_back(
|
|
|
|
{R_ABS, target->tlsOffsetRel, sym.getGotOffset(), 0, &sym});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sym.needsTlsIe && !sym.needsTlsGdToIe)
|
|
|
|
addTpOffsetGotEntry(sym);
|
2021-12-15 08:28:41 +08:00
|
|
|
};
|
2022-01-10 05:43:26 +08:00
|
|
|
|
2022-02-08 02:26:16 +08:00
|
|
|
if (config->needsTlsLd && in.got->addTlsIndex()) {
|
|
|
|
static Undefined dummy(nullptr, "", STB_LOCAL, 0, 0);
|
|
|
|
if (config->shared)
|
|
|
|
mainPart->relaDyn->addReloc(
|
|
|
|
{target->tlsModuleIndexRel, in.got.get(), in.got->getTlsIndexOff()});
|
|
|
|
else
|
|
|
|
in.got->relocations.push_back(
|
|
|
|
{R_ADDEND, target->symbolicRel, in.got->getTlsIndexOff(), 1, &dummy});
|
|
|
|
}
|
|
|
|
|
2022-01-10 05:43:26 +08:00
|
|
|
assert(symAux.empty());
|
2021-12-15 08:28:41 +08:00
|
|
|
for (Symbol *sym : symtab->symbols())
|
|
|
|
fn(*sym);
|
|
|
|
|
|
|
|
// Local symbols may need the aforementioned non-preemptible ifunc and GOT
|
|
|
|
// handling. They don't need regular PLT.
|
2021-12-15 16:37:10 +08:00
|
|
|
for (ELFFileBase *file : objectFiles)
|
2022-01-05 18:07:14 +08:00
|
|
|
for (Symbol *sym : file->getLocalSymbols())
|
2021-12-15 08:28:41 +08:00
|
|
|
fn(*sym);
|
|
|
|
}
|
|
|
|
|
2018-10-04 06:20:26 +08:00
|
|
|
static bool mergeCmp(const InputSection *a, const InputSection *b) {
|
|
|
|
// std::merge requires a strict weak ordering.
|
|
|
|
if (a->outSecOff < b->outSecOff)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (a->outSecOff == b->outSecOff) {
|
|
|
|
auto *ta = dyn_cast<ThunkSection>(a);
|
|
|
|
auto *tb = dyn_cast<ThunkSection>(b);
|
|
|
|
|
|
|
|
// Check if Thunk is immediately before any specific Target
|
|
|
|
// InputSection for example Mips LA25 Thunks.
|
|
|
|
if (ta && ta->getTargetInputSection() == b)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Place Thunk Sections without specific targets before
|
|
|
|
// non-Thunk Sections.
|
|
|
|
if (ta && !tb && !ta->getTargetInputSection())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-24 06:31:08 +08:00
|
|
|
// Call Fn on every executable InputSection accessed via the linker script
|
|
|
|
// InputSectionDescription::Sections.
|
|
|
|
static void forEachInputSectionDescription(
|
|
|
|
ArrayRef<OutputSection *> outputSections,
|
|
|
|
llvm::function_ref<void(OutputSection *, InputSectionDescription *)> fn) {
|
|
|
|
for (OutputSection *os : outputSections) {
|
|
|
|
if (!(os->flags & SHF_ALLOC) || !(os->flags & SHF_EXECINSTR))
|
|
|
|
continue;
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *bc : os->commands)
|
2018-10-24 06:31:08 +08:00
|
|
|
if (auto *isd = dyn_cast<InputSectionDescription>(bc))
|
|
|
|
fn(os, isd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 17:07:10 +08:00
|
|
|
// Thunk Implementation
|
|
|
|
//
|
|
|
|
// Thunks (sometimes called stubs, veneers or branch islands) are small pieces
|
|
|
|
// of code that the linker inserts inbetween a caller and a callee. The thunks
|
|
|
|
// are added at link time rather than compile time as the decision on whether
|
|
|
|
// a thunk is needed, such as the caller and callee being out of range, can only
|
|
|
|
// be made at link time.
|
|
|
|
//
|
|
|
|
// It is straightforward to tell given the current state of the program when a
|
|
|
|
// thunk is needed for a particular call. The more difficult part is that
|
|
|
|
// the thunk needs to be placed in the program such that the caller can reach
|
|
|
|
// the thunk and the thunk can reach the callee; furthermore, adding thunks to
|
|
|
|
// the program alters addresses, which can mean more thunks etc.
|
|
|
|
//
|
|
|
|
// In lld we have a synthetic ThunkSection that can hold many Thunks.
|
|
|
|
// The decision to have a ThunkSection act as a container means that we can
|
|
|
|
// more easily handle the most common case of a single block of contiguous
|
|
|
|
// Thunks by inserting just a single ThunkSection.
|
|
|
|
//
|
|
|
|
// The implementation of Thunks in lld is split across these areas
|
|
|
|
// Relocations.cpp : Framework for creating and placing thunks
|
|
|
|
// Thunks.cpp : The code generated for each supported thunk
|
|
|
|
// Target.cpp : Target specific hooks that the framework uses to decide when
|
|
|
|
// a thunk is used
|
|
|
|
// Synthetic.cpp : Implementation of ThunkSection
|
|
|
|
// Writer.cpp : Iteratively call framework until no more Thunks added
|
|
|
|
//
|
|
|
|
// Thunk placement requirements:
|
|
|
|
// Mips LA25 thunks. These must be placed immediately before the callee section
|
|
|
|
// We can assume that the caller is in range of the Thunk. These are modelled
|
|
|
|
// by Thunks that return the section they must precede with
|
|
|
|
// getTargetInputSection().
|
|
|
|
//
|
|
|
|
// ARM interworking and range extension thunks. These thunks must be placed
|
|
|
|
// within range of the caller. All implemented ARM thunks can always reach the
|
|
|
|
// callee as they use an indirect jump via a register that has no range
|
|
|
|
// restrictions.
|
|
|
|
//
|
|
|
|
// Thunk placement algorithm:
|
|
|
|
// For Mips LA25 ThunkSections; the placement is explicit, it has to be before
|
|
|
|
// getTargetInputSection().
|
|
|
|
//
|
|
|
|
// For thunks that must be placed within range of the caller there are many
|
|
|
|
// possible choices given that the maximum range from the caller is usually
|
|
|
|
// much larger than the average InputSection size. Desirable properties include:
|
|
|
|
// - Maximize reuse of thunks by multiple callers
|
|
|
|
// - Minimize number of ThunkSections to simplify insertion
|
|
|
|
// - Handle impact of already added Thunks on addresses
|
|
|
|
// - Simple to understand and implement
|
|
|
|
//
|
|
|
|
// In lld for the first pass, we pre-create one or more ThunkSections per
|
|
|
|
// InputSectionDescription at Target specific intervals. A ThunkSection is
|
|
|
|
// placed so that the estimated end of the ThunkSection is within range of the
|
|
|
|
// start of the InputSectionDescription or the previous ThunkSection. For
|
|
|
|
// example:
|
|
|
|
// InputSectionDescription
|
|
|
|
// Section 0
|
|
|
|
// ...
|
|
|
|
// Section N
|
|
|
|
// ThunkSection 0
|
|
|
|
// Section N + 1
|
|
|
|
// ...
|
|
|
|
// Section N + K
|
|
|
|
// Thunk Section 1
|
|
|
|
//
|
|
|
|
// The intention is that we can add a Thunk to a ThunkSection that is well
|
|
|
|
// spaced enough to service a number of callers without having to do a lot
|
|
|
|
// of work. An important principle is that it is not an error if a Thunk cannot
|
|
|
|
// be placed in a pre-created ThunkSection; when this happens we create a new
|
|
|
|
// ThunkSection placed next to the caller. This allows us to handle the vast
|
|
|
|
// majority of thunks simply, but also handle rare cases where the branch range
|
|
|
|
// is smaller than the target specific spacing.
|
|
|
|
//
|
|
|
|
// The algorithm is expected to create all the thunks that are needed in a
|
|
|
|
// single pass, with a small number of programs needing a second pass due to
|
|
|
|
// the insertion of thunks in the first pass increasing the offset between
|
|
|
|
// callers and callees that were only just in range.
|
|
|
|
//
|
|
|
|
// A consequence of allowing new ThunkSections to be created outside of the
|
|
|
|
// pre-created ThunkSections is that in rare cases calls to Thunks that were in
|
|
|
|
// range in pass K, are out of range in some pass > K due to the insertion of
|
|
|
|
// more Thunks in between the caller and callee. When this happens we retarget
|
|
|
|
// the relocation back to the original target and create another Thunk.
|
|
|
|
|
|
|
|
// Remove ThunkSections that are empty, this should only be the initial set
|
|
|
|
// precreated on pass 0.
|
|
|
|
|
|
|
|
// Insert the Thunks for OutputSection OS into their designated place
|
|
|
|
// in the Sections vector, and recalculate the InputSection output section
|
|
|
|
// offsets.
|
|
|
|
// This may invalidate any output section offsets stored outside of InputSection
|
2017-10-27 16:56:20 +08:00
|
|
|
void ThunkCreator::mergeThunks(ArrayRef<OutputSection *> outputSections) {
|
|
|
|
forEachInputSectionDescription(
|
|
|
|
outputSections, [&](OutputSection *os, InputSectionDescription *isd) {
|
|
|
|
if (isd->thunkSections.empty())
|
|
|
|
return;
|
|
|
|
|
2017-10-27 16:58:28 +08:00
|
|
|
// Remove any zero sized precreated Thunks.
|
2017-10-27 17:07:10 +08:00
|
|
|
llvm::erase_if(isd->thunkSections,
|
|
|
|
[](const std::pair<ThunkSection *, uint32_t> &ts) {
|
|
|
|
return ts.first->getSize() == 0;
|
|
|
|
});
|
2018-10-04 06:20:26 +08:00
|
|
|
|
2017-10-27 17:07:10 +08:00
|
|
|
// ISD->ThunkSections contains all created ThunkSections, including
|
|
|
|
// those inserted in previous passes. Extract the Thunks created this
|
2019-07-16 13:50:45 +08:00
|
|
|
// pass and order them in ascending outSecOff.
|
2017-10-27 17:07:10 +08:00
|
|
|
std::vector<ThunkSection *> newThunks;
|
2020-01-05 03:53:17 +08:00
|
|
|
for (std::pair<ThunkSection *, uint32_t> ts : isd->thunkSections)
|
2017-10-27 17:07:10 +08:00
|
|
|
if (ts.second == pass)
|
|
|
|
newThunks.push_back(ts.first);
|
2019-04-23 10:42:06 +08:00
|
|
|
llvm::stable_sort(newThunks,
|
|
|
|
[](const ThunkSection *a, const ThunkSection *b) {
|
|
|
|
return a->outSecOff < b->outSecOff;
|
|
|
|
});
|
2017-10-27 16:56:20 +08:00
|
|
|
|
2019-07-16 13:50:45 +08:00
|
|
|
// Merge sorted vectors of Thunks and InputSections by outSecOff
|
2021-12-27 05:06:54 +08:00
|
|
|
SmallVector<InputSection *, 0> tmp;
|
2017-10-27 17:07:10 +08:00
|
|
|
tmp.reserve(isd->sections.size() + newThunks.size());
|
2018-10-04 06:20:26 +08:00
|
|
|
|
2017-10-27 16:56:20 +08:00
|
|
|
std::merge(isd->sections.begin(), isd->sections.end(),
|
2017-10-27 17:07:10 +08:00
|
|
|
newThunks.begin(), newThunks.end(), std::back_inserter(tmp),
|
2018-10-04 06:20:26 +08:00
|
|
|
mergeCmp);
|
|
|
|
|
2017-10-27 16:56:20 +08:00
|
|
|
isd->sections = std::move(tmp);
|
|
|
|
});
|
2017-02-01 18:26:03 +08:00
|
|
|
}
|
|
|
|
|
2017-10-27 17:04:11 +08:00
|
|
|
// Find or create a ThunkSection within the InputSectionDescription (ISD) that
|
2017-11-06 18:36:18 +08:00
|
|
|
// is in range of Src. An ISD maps to a range of InputSections described by a
|
2017-10-27 17:04:11 +08:00
|
|
|
// linker script section pattern such as { .text .text.* }.
|
2021-02-26 21:14:21 +08:00
|
|
|
ThunkSection *ThunkCreator::getISDThunkSec(OutputSection *os,
|
|
|
|
InputSection *isec,
|
2017-10-27 17:04:11 +08:00
|
|
|
InputSectionDescription *isd,
|
2021-02-26 21:14:21 +08:00
|
|
|
const Relocation &rel,
|
|
|
|
uint64_t src) {
|
2017-10-27 17:07:10 +08:00
|
|
|
for (std::pair<ThunkSection *, uint32_t> tp : isd->thunkSections) {
|
|
|
|
ThunkSection *ts = tp.first;
|
2021-02-26 21:14:21 +08:00
|
|
|
uint64_t tsBase = os->addr + ts->outSecOff + rel.addend;
|
|
|
|
uint64_t tsLimit = tsBase + ts->getSize() + rel.addend;
|
|
|
|
if (target->inBranchRange(rel.type, src,
|
|
|
|
(src > tsLimit) ? tsBase : tsLimit))
|
2017-10-27 17:04:11 +08:00
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No suitable ThunkSection exists. This can happen when there is a branch
|
|
|
|
// with lower range than the ThunkSection spacing or when there are too
|
|
|
|
// many Thunks. Create a new ThunkSection as close to the InputSection as
|
|
|
|
// possible. Error if InputSection is so large we cannot place ThunkSection
|
|
|
|
// anywhere in Range.
|
|
|
|
uint64_t thunkSecOff = isec->outSecOff;
|
2021-02-26 21:14:21 +08:00
|
|
|
if (!target->inBranchRange(rel.type, src,
|
|
|
|
os->addr + thunkSecOff + rel.addend)) {
|
2017-10-27 17:04:11 +08:00
|
|
|
thunkSecOff = isec->outSecOff + isec->getSize();
|
2021-02-26 21:14:21 +08:00
|
|
|
if (!target->inBranchRange(rel.type, src,
|
|
|
|
os->addr + thunkSecOff + rel.addend))
|
2017-10-27 17:04:11 +08:00
|
|
|
fatal("InputSection too large for range extension thunk " +
|
|
|
|
isec->getObjMsg(src - (os->addr + isec->outSecOff)));
|
|
|
|
}
|
|
|
|
return addThunkSection(os, isd, thunkSecOff);
|
2017-04-05 18:30:09 +08:00
|
|
|
}
|
|
|
|
|
2017-09-12 17:17:39 +08:00
|
|
|
// Add a Thunk that needs to be placed in a ThunkSection that immediately
|
|
|
|
// precedes its Target.
|
|
|
|
ThunkSection *ThunkCreator::getISThunkSec(InputSection *isec) {
|
2017-04-05 18:30:09 +08:00
|
|
|
ThunkSection *ts = thunkedSections.lookup(isec);
|
|
|
|
if (ts)
|
|
|
|
return ts;
|
2017-06-07 17:35:14 +08:00
|
|
|
|
2017-09-12 17:17:39 +08:00
|
|
|
// Find InputSectionRange within Target Output Section (TOS) that the
|
|
|
|
// InputSection (IS) that we need to precede is in.
|
|
|
|
OutputSection *tos = isec->getParent();
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *bc : tos->commands) {
|
2018-10-04 06:20:26 +08:00
|
|
|
auto *isd = dyn_cast<InputSectionDescription>(bc);
|
|
|
|
if (!isd || isd->sections.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
InputSection *first = isd->sections.front();
|
|
|
|
InputSection *last = isd->sections.back();
|
|
|
|
|
|
|
|
if (isec->outSecOff < first->outSecOff || last->outSecOff < isec->outSecOff)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ts = addThunkSection(tos, isd, isec->outSecOff);
|
|
|
|
thunkedSections[isec] = ts;
|
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2017-04-05 18:30:09 +08:00
|
|
|
}
|
|
|
|
|
2017-10-27 16:58:28 +08:00
|
|
|
// Create one or more ThunkSections per OS that can be used to place Thunks.
|
|
|
|
// We attempt to place the ThunkSections using the following desirable
|
|
|
|
// properties:
|
|
|
|
// - Within range of the maximum number of callers
|
|
|
|
// - Minimise the number of ThunkSections
|
|
|
|
//
|
|
|
|
// We follow a simple but conservative heuristic to place ThunkSections at
|
|
|
|
// offsets that are multiples of a Target specific branch range.
|
2018-03-31 02:32:24 +08:00
|
|
|
// For an InputSectionDescription that is smaller than the range, a single
|
2017-10-27 16:58:28 +08:00
|
|
|
// ThunkSection at the end of the range will do.
|
2018-03-31 02:32:24 +08:00
|
|
|
//
|
|
|
|
// For an InputSectionDescription that is more than twice the size of the range,
|
|
|
|
// we place the last ThunkSection at range bytes from the end of the
|
|
|
|
// InputSectionDescription in order to increase the likelihood that the
|
|
|
|
// distance from a thunk to its target will be sufficiently small to
|
|
|
|
// allow for the creation of a short thunk.
|
2017-10-27 16:58:28 +08:00
|
|
|
void ThunkCreator::createInitialThunkSections(
|
|
|
|
ArrayRef<OutputSection *> outputSections) {
|
2018-08-20 17:37:50 +08:00
|
|
|
uint32_t thunkSectionSpacing = target->getThunkSectionSpacing();
|
2018-10-04 06:20:26 +08:00
|
|
|
|
2017-10-27 16:58:28 +08:00
|
|
|
forEachInputSectionDescription(
|
|
|
|
outputSections, [&](OutputSection *os, InputSectionDescription *isd) {
|
|
|
|
if (isd->sections.empty())
|
|
|
|
return;
|
2018-10-04 06:20:26 +08:00
|
|
|
|
2018-03-31 02:32:24 +08:00
|
|
|
uint32_t isdBegin = isd->sections.front()->outSecOff;
|
|
|
|
uint32_t isdEnd =
|
|
|
|
isd->sections.back()->outSecOff + isd->sections.back()->getSize();
|
|
|
|
uint32_t lastThunkLowerBound = -1;
|
2018-08-20 17:37:50 +08:00
|
|
|
if (isdEnd - isdBegin > thunkSectionSpacing * 2)
|
|
|
|
lastThunkLowerBound = isdEnd - thunkSectionSpacing;
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2017-10-27 16:58:28 +08:00
|
|
|
uint32_t isecLimit;
|
2018-03-31 02:32:24 +08:00
|
|
|
uint32_t prevIsecLimit = isdBegin;
|
2018-08-20 17:37:50 +08:00
|
|
|
uint32_t thunkUpperBound = isdBegin + thunkSectionSpacing;
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2017-10-27 16:58:28 +08:00
|
|
|
for (const InputSection *isec : isd->sections) {
|
|
|
|
isecLimit = isec->outSecOff + isec->getSize();
|
|
|
|
if (isecLimit > thunkUpperBound) {
|
|
|
|
addThunkSection(os, isd, prevIsecLimit);
|
2018-08-20 17:37:50 +08:00
|
|
|
thunkUpperBound = prevIsecLimit + thunkSectionSpacing;
|
2017-10-27 16:58:28 +08:00
|
|
|
}
|
2018-03-31 02:32:24 +08:00
|
|
|
if (isecLimit > lastThunkLowerBound)
|
|
|
|
break;
|
2017-10-27 16:58:28 +08:00
|
|
|
prevIsecLimit = isecLimit;
|
|
|
|
}
|
|
|
|
addThunkSection(os, isd, isecLimit);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-12 17:17:39 +08:00
|
|
|
ThunkSection *ThunkCreator::addThunkSection(OutputSection *os,
|
2017-10-27 16:56:20 +08:00
|
|
|
InputSectionDescription *isd,
|
2017-06-16 21:10:08 +08:00
|
|
|
uint64_t off) {
|
2017-09-12 17:17:39 +08:00
|
|
|
auto *ts = make<ThunkSection>(os, off);
|
2019-05-29 12:06:01 +08:00
|
|
|
ts->partition = os->partition;
|
2020-01-07 23:22:09 +08:00
|
|
|
if ((config->fixCortexA53Errata843419 || config->fixCortexA8) &&
|
|
|
|
!isd->sections.empty()) {
|
|
|
|
// The errata fixes are sensitive to addresses modulo 4 KiB. When we add
|
|
|
|
// thunks we disturb the base addresses of sections placed after the thunks
|
|
|
|
// this makes patches we have generated redundant, and may cause us to
|
|
|
|
// generate more patches as different instructions are now in sensitive
|
|
|
|
// locations. When we generate more patches we may force more branches to
|
|
|
|
// go out of range, causing more thunks to be generated. In pathological
|
|
|
|
// cases this can cause the address dependent content pass not to converge.
|
|
|
|
// We fix this by rounding up the size of the ThunkSection to 4KiB, this
|
|
|
|
// limits the insertion of a ThunkSection on the addresses modulo 4 KiB,
|
|
|
|
// which means that adding Thunks to the section does not invalidate
|
|
|
|
// errata patches for following code.
|
|
|
|
// Rounding up the size to 4KiB has consequences for code-size and can
|
|
|
|
// trip up linker script defined assertions. For example the linux kernel
|
|
|
|
// has an assertion that what LLD represents as an InputSectionDescription
|
|
|
|
// does not exceed 4 KiB even if the overall OutputSection is > 128 Mib.
|
|
|
|
// We use the heuristic of rounding up the size when both of the following
|
|
|
|
// conditions are true:
|
|
|
|
// 1.) The OutputSection is larger than the ThunkSectionSpacing. This
|
|
|
|
// accounts for the case where no single InputSectionDescription is
|
|
|
|
// larger than the OutputSection size. This is conservative but simple.
|
|
|
|
// 2.) The InputSectionDescription is larger than 4 KiB. This will prevent
|
|
|
|
// any assertion failures that an InputSectionDescription is < 4 KiB
|
|
|
|
// in size.
|
|
|
|
uint64_t isdSize = isd->sections.back()->outSecOff +
|
|
|
|
isd->sections.back()->getSize() -
|
|
|
|
isd->sections.front()->outSecOff;
|
|
|
|
if (os->size > target->getThunkSectionSpacing() && isdSize > 4096)
|
|
|
|
ts->roundUpSizeForErrata = true;
|
|
|
|
}
|
2018-10-04 06:20:26 +08:00
|
|
|
isd->thunkSections.push_back({ts, pass});
|
2017-06-16 21:10:08 +08:00
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
2019-05-29 12:06:01 +08:00
|
|
|
static bool isThunkSectionCompatible(InputSection *source,
|
|
|
|
SectionBase *target) {
|
|
|
|
// We can't reuse thunks in different loadable partitions because they might
|
|
|
|
// not be loaded. But partition 1 (the main partition) will always be loaded.
|
|
|
|
if (source->partition != target->partition)
|
|
|
|
return target->partition == 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-26 03:02:56 +08:00
|
|
|
static int64_t getPCBias(RelType type) {
|
|
|
|
if (config->emachine != EM_ARM)
|
|
|
|
return 0;
|
|
|
|
switch (type) {
|
|
|
|
case R_ARM_THM_JUMP19:
|
|
|
|
case R_ARM_THM_JUMP24:
|
|
|
|
case R_ARM_THM_CALL:
|
|
|
|
return 4;
|
|
|
|
default:
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
std::pair<Thunk *, bool> ThunkCreator::getThunk(InputSection *isec,
|
|
|
|
Relocation &rel, uint64_t src) {
|
2018-03-10 01:54:43 +08:00
|
|
|
std::vector<Thunk *> *thunkVec = nullptr;
|
2021-02-26 21:14:21 +08:00
|
|
|
// Arm and Thumb have a PC Bias of 8 and 4 respectively, this is cancelled
|
|
|
|
// out in the relocation addend. We compensate for the PC bias so that
|
|
|
|
// an Arm and Thumb relocation to the same destination get the same keyAddend,
|
|
|
|
// which is usually 0.
|
2022-01-25 01:03:21 +08:00
|
|
|
const int64_t pcBias = getPCBias(rel.type);
|
|
|
|
const int64_t keyAddend = rel.addend + pcBias;
|
2018-10-04 06:20:26 +08:00
|
|
|
|
2019-11-23 16:57:54 +08:00
|
|
|
// We use a ((section, offset), addend) pair to find the thunk position if
|
|
|
|
// possible so that we create only one thunk for aliased symbols or ICFed
|
|
|
|
// sections. There may be multiple relocations sharing the same (section,
|
|
|
|
// offset + addend) pair. We may revert the relocation back to its original
|
|
|
|
// non-Thunk target, so we cannot fold offset + 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 (auto *d = dyn_cast<Defined>(rel.sym))
|
2018-03-10 01:54:43 +08:00
|
|
|
if (!d->isInPlt() && d->section)
|
2021-12-25 04:09:48 +08:00
|
|
|
thunkVec = &thunkedSymbolsBySectionAndAddend[{{d->section, d->value},
|
|
|
|
keyAddend}];
|
2018-03-10 01:54:43 +08:00
|
|
|
if (!thunkVec)
|
2021-02-26 21:14:21 +08:00
|
|
|
thunkVec = &thunkedSymbols[{rel.sym, keyAddend}];
|
2018-10-04 06:20:26 +08:00
|
|
|
|
2018-03-10 01:54:43 +08:00
|
|
|
// Check existing Thunks for Sym to see if they can be reused
|
2018-10-04 06:20:26 +08:00
|
|
|
for (Thunk *t : *thunkVec)
|
2019-05-29 12:06:01 +08:00
|
|
|
if (isThunkSectionCompatible(isec, t->getThunkTargetSym()->section) &&
|
[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
|
|
|
t->isCompatibleWith(*isec, rel) &&
|
2019-11-26 03:02:56 +08:00
|
|
|
target->inBranchRange(rel.type, src,
|
2022-01-25 01:03:21 +08:00
|
|
|
t->getThunkTargetSym()->getVA(-pcBias)))
|
2018-10-04 08:35:24 +08:00
|
|
|
return std::make_pair(t, false);
|
2018-10-04 06:20:26 +08:00
|
|
|
|
2017-07-05 17:53:33 +08:00
|
|
|
// No existing compatible Thunk in range, create a new one
|
[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
|
|
|
Thunk *t = addThunk(*isec, rel);
|
2018-03-10 01:54:43 +08:00
|
|
|
thunkVec->push_back(t);
|
2018-10-04 08:35:24 +08:00
|
|
|
return std::make_pair(t, true);
|
2017-04-05 18:30:09 +08:00
|
|
|
}
|
|
|
|
|
2017-10-27 17:07:10 +08:00
|
|
|
// Return true if the relocation target is an in range Thunk.
|
|
|
|
// Return false if the relocation is not to a Thunk. If the relocation target
|
|
|
|
// was originally to a Thunk, but is no longer in range we revert the
|
|
|
|
// relocation back to its original non-Thunk target.
|
|
|
|
bool ThunkCreator::normalizeExistingThunk(Relocation &rel, uint64_t src) {
|
2018-10-04 06:20:26 +08:00
|
|
|
if (Thunk *t = thunks.lookup(rel.sym)) {
|
2021-02-26 21:14:21 +08:00
|
|
|
if (target->inBranchRange(rel.type, src, rel.sym->getVA(rel.addend)))
|
2017-10-27 17:07:10 +08:00
|
|
|
return true;
|
2018-10-04 06:20:26 +08:00
|
|
|
rel.sym = &t->destination;
|
2020-01-26 10:58:54 +08:00
|
|
|
rel.addend = t->addend;
|
2017-10-27 17:07:10 +08:00
|
|
|
if (rel.sym->isInPlt())
|
|
|
|
rel.expr = toPlt(rel.expr);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-01 18:26:03 +08:00
|
|
|
// Process all relocations from the InputSections that have been assigned
|
2017-10-27 17:07:10 +08:00
|
|
|
// to InputSectionDescriptions and redirect through Thunks if needed. The
|
|
|
|
// function should be called iteratively until it returns false.
|
|
|
|
//
|
|
|
|
// PreConditions:
|
|
|
|
// All InputSections that may need a Thunk are reachable from
|
|
|
|
// OutputSectionCommands.
|
2017-02-01 18:26:03 +08:00
|
|
|
//
|
2017-10-27 17:07:10 +08:00
|
|
|
// All OutputSections have an address and all InputSections have an offset
|
|
|
|
// within the OutputSection.
|
2017-02-01 18:26:03 +08:00
|
|
|
//
|
2017-10-27 17:07:10 +08:00
|
|
|
// The offsets between caller (relocation place) and callee
|
|
|
|
// (relocation target) will not be modified outside of createThunks().
|
|
|
|
//
|
|
|
|
// PostConditions:
|
|
|
|
// If return value is true then ThunkSections have been inserted into
|
|
|
|
// OutputSections. All relocations that needed a Thunk based on the information
|
|
|
|
// available to createThunks() on entry have been redirected to a Thunk. Note
|
|
|
|
// that adding Thunks changes offsets between caller and callee so more Thunks
|
|
|
|
// may be required.
|
|
|
|
//
|
|
|
|
// If return value is false then no more Thunks are needed, and createThunks has
|
|
|
|
// made no changes. If the target requires range extension thunks, currently
|
|
|
|
// ARM, then any future change in offset between caller and callee risks a
|
|
|
|
// relocation out of range error.
|
2017-07-28 03:22:43 +08:00
|
|
|
bool ThunkCreator::createThunks(ArrayRef<OutputSection *> outputSections) {
|
2017-10-27 16:56:20 +08:00
|
|
|
bool addressesChanged = false;
|
2018-10-04 06:20:26 +08:00
|
|
|
|
2018-08-20 17:37:50 +08:00
|
|
|
if (pass == 0 && target->getThunkSectionSpacing())
|
2017-10-27 16:58:28 +08:00
|
|
|
createInitialThunkSections(outputSections);
|
2018-10-04 06:20:26 +08:00
|
|
|
|
2017-02-01 18:26:03 +08:00
|
|
|
// Create all the Thunks and insert them into synthetic ThunkSections. The
|
2017-10-27 16:56:20 +08:00
|
|
|
// ThunkSections are later inserted back into InputSectionDescriptions.
|
2017-02-01 18:26:03 +08:00
|
|
|
// We separate the creation of ThunkSections from the insertion of the
|
2017-10-27 16:56:20 +08:00
|
|
|
// ThunkSections as ThunkSections are not always inserted into the same
|
|
|
|
// InputSectionDescription as the caller.
|
|
|
|
forEachInputSectionDescription(
|
|
|
|
outputSections, [&](OutputSection *os, InputSectionDescription *isd) {
|
|
|
|
for (InputSection *isec : isd->sections)
|
|
|
|
for (Relocation &rel : isec->relocations) {
|
2018-03-24 08:35:11 +08:00
|
|
|
uint64_t src = isec->getVA(rel.offset);
|
2017-10-27 17:07:10 +08:00
|
|
|
|
|
|
|
// If we are a relocation to an existing Thunk, check if it is
|
|
|
|
// still in range. If not then Rel will be altered to point to its
|
|
|
|
// original target so another Thunk can be generated.
|
|
|
|
if (pass > 0 && normalizeExistingThunk(rel, src))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!target->needsThunk(rel.expr, rel.type, isec->file, src,
|
2019-11-23 16:57:54 +08:00
|
|
|
*rel.sym, rel.addend))
|
2017-10-27 16:56:20 +08:00
|
|
|
continue;
|
2018-10-04 06:20:26 +08:00
|
|
|
|
2017-10-27 16:56:20 +08:00
|
|
|
Thunk *t;
|
|
|
|
bool isNew;
|
[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
|
|
|
std::tie(t, isNew) = getThunk(isec, rel, src);
|
2018-10-04 06:20:26 +08:00
|
|
|
|
2017-10-27 16:56:20 +08:00
|
|
|
if (isNew) {
|
|
|
|
// Find or create a ThunkSection for the new Thunk
|
|
|
|
ThunkSection *ts;
|
|
|
|
if (auto *tis = t->getTargetInputSection())
|
|
|
|
ts = getISThunkSec(tis);
|
|
|
|
else
|
2021-02-26 21:14:21 +08:00
|
|
|
ts = getISDThunkSec(os, isec, isd, rel, src);
|
2017-10-27 16:56:20 +08:00
|
|
|
ts->addThunk(t);
|
2018-03-30 06:32:13 +08:00
|
|
|
thunks[t->getThunkTargetSym()] = t;
|
2017-10-27 16:56:20 +08:00
|
|
|
}
|
2018-10-04 06:20:26 +08:00
|
|
|
|
2017-10-27 16:56:20 +08:00
|
|
|
// Redirect relocation to Thunk, we never go via the PLT to a Thunk
|
2018-03-30 06:32:13 +08:00
|
|
|
rel.sym = t->getThunkTargetSym();
|
2017-10-27 16:56:20 +08:00
|
|
|
rel.expr = fromPlt(rel.expr);
|
[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
|
|
|
|
2020-01-26 10:58:54 +08:00
|
|
|
// On AArch64 and PPC, a jump/call relocation may be encoded as
|
2019-12-03 06:20:42 +08:00
|
|
|
// STT_SECTION + non-zero addend, clear the addend after
|
|
|
|
// redirection.
|
2020-01-26 10:58:54 +08:00
|
|
|
if (config->emachine != EM_MIPS)
|
|
|
|
rel.addend = -getPCBias(rel.type);
|
2017-10-27 16:56:20 +08:00
|
|
|
}
|
2018-10-04 06:20:26 +08:00
|
|
|
|
2018-03-30 06:32:13 +08:00
|
|
|
for (auto &p : isd->thunkSections)
|
|
|
|
addressesChanged |= p.first->assignOffsets();
|
2017-10-27 16:56:20 +08:00
|
|
|
});
|
2018-10-04 06:20:26 +08:00
|
|
|
|
2018-03-30 06:32:13 +08:00
|
|
|
for (auto &p : thunkedSections)
|
|
|
|
addressesChanged |= p.second->assignOffsets();
|
|
|
|
|
2017-02-01 18:26:03 +08:00
|
|
|
// Merge all created synthetic ThunkSections back into OutputSection
|
2017-10-27 16:56:20 +08:00
|
|
|
mergeThunks(outputSections);
|
2017-06-16 21:10:08 +08:00
|
|
|
++pass;
|
2017-10-27 16:56:20 +08:00
|
|
|
return addressesChanged;
|
2016-07-21 01:58:07 +08:00
|
|
|
}
|
2016-05-25 04:24:43 +08:00
|
|
|
|
2020-02-11 07:27:53 +08:00
|
|
|
// The following aid in the conversion of call x@GDPLT to call __tls_get_addr
|
|
|
|
// hexagonNeedsTLSSymbol scans for relocations would require a call to
|
|
|
|
// __tls_get_addr.
|
|
|
|
// hexagonTLSSymbolUpdate rebinds the relocation to __tls_get_addr.
|
2020-05-15 13:18:58 +08:00
|
|
|
bool elf::hexagonNeedsTLSSymbol(ArrayRef<OutputSection *> outputSections) {
|
2020-02-11 07:27:53 +08:00
|
|
|
bool needTlsSymbol = false;
|
|
|
|
forEachInputSectionDescription(
|
|
|
|
outputSections, [&](OutputSection *os, InputSectionDescription *isd) {
|
|
|
|
for (InputSection *isec : isd->sections)
|
|
|
|
for (Relocation &rel : isec->relocations)
|
|
|
|
if (rel.sym->type == llvm::ELF::STT_TLS && rel.expr == R_PLT_PC) {
|
|
|
|
needTlsSymbol = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return needTlsSymbol;
|
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
void elf::hexagonTLSSymbolUpdate(ArrayRef<OutputSection *> outputSections) {
|
2020-02-11 07:27:53 +08:00
|
|
|
Symbol *sym = symtab->find("__tls_get_addr");
|
|
|
|
if (!sym)
|
|
|
|
return;
|
|
|
|
bool needEntry = true;
|
|
|
|
forEachInputSectionDescription(
|
|
|
|
outputSections, [&](OutputSection *os, InputSectionDescription *isd) {
|
|
|
|
for (InputSection *isec : isd->sections)
|
|
|
|
for (Relocation &rel : isec->relocations)
|
|
|
|
if (rel.sym->type == llvm::ELF::STT_TLS && rel.expr == R_PLT_PC) {
|
|
|
|
if (needEntry) {
|
2022-01-10 05:43:26 +08:00
|
|
|
sym->allocateAux();
|
2021-12-23 12:51:11 +08:00
|
|
|
addPltEntry(*in.plt, *in.gotPlt, *in.relaPlt, target->pltRel,
|
2020-02-11 07:27:53 +08:00
|
|
|
*sym);
|
|
|
|
needEntry = false;
|
|
|
|
}
|
|
|
|
rel.sym = sym;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
template void elf::scanRelocations<ELF32LE>(InputSectionBase &);
|
|
|
|
template void elf::scanRelocations<ELF32BE>(InputSectionBase &);
|
|
|
|
template void elf::scanRelocations<ELF64LE>(InputSectionBase &);
|
|
|
|
template void elf::scanRelocations<ELF64BE>(InputSectionBase &);
|