2016-05-25 04:24:43 +08:00
|
|
|
//===- Relocations.cpp ----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
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"
|
2017-02-01 18:26:03 +08:00
|
|
|
#include "Memory.h"
|
2016-05-25 04:24:43 +08:00
|
|
|
#include "OutputSections.h"
|
2016-11-10 17:48:29 +08:00
|
|
|
#include "Strings.h"
|
2016-05-25 04:24:43 +08:00
|
|
|
#include "SymbolTable.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"
|
2016-05-25 04:24:43 +08:00
|
|
|
|
|
|
|
#include "llvm/Support/Endian.h"
|
|
|
|
#include "llvm/Support/raw_ostream.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;
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace elf {
|
|
|
|
|
|
|
|
static bool refersToGotEntry(RelExpr Expr) {
|
2016-12-01 13:43:48 +08:00
|
|
|
return isRelExprOneOf<R_GOT, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE, R_MIPS_GOT_OFF,
|
|
|
|
R_MIPS_GOT_OFF32, R_MIPS_TLSGD, R_MIPS_TLSLD,
|
|
|
|
R_GOT_PAGE_PC, R_GOT_PC, R_GOT_FROM_END, R_TLSGD,
|
|
|
|
R_TLSGD_PC, R_TLSDESC, R_TLSDESC_PAGE>(Expr);
|
2016-05-25 04:24:43 +08:00
|
|
|
}
|
|
|
|
|
2016-05-28 12:49:57 +08:00
|
|
|
static bool isPreemptible(const SymbolBody &Body, uint32_t Type) {
|
|
|
|
// In case of MIPS GP-relative relocations always resolve to a definition
|
|
|
|
// in a regular input file, ignoring the one-definition rule. So we,
|
|
|
|
// for example, should not attempt to create a dynamic relocation even
|
|
|
|
// if the target symbol is preemptible. There are two two MIPS GP-relative
|
|
|
|
// relocations R_MIPS_GPREL16 and R_MIPS_GPREL32. But only R_MIPS_GPREL16
|
|
|
|
// can be against a preemptible symbol.
|
2016-06-10 20:26:09 +08:00
|
|
|
// To get MIPS relocation type we apply 0xff mask. In case of O32 ABI all
|
2016-05-28 12:49:57 +08:00
|
|
|
// relocation types occupy eight bit. In case of N64 ABI we extract first
|
|
|
|
// relocation from 3-in-1 packet because only the first relocation can
|
|
|
|
// be against a real symbol.
|
2016-06-10 20:26:09 +08:00
|
|
|
if (Config->EMachine == EM_MIPS && (Type & 0xff) == R_MIPS_GPREL16)
|
2016-05-28 12:49:57 +08:00
|
|
|
return false;
|
|
|
|
return Body.isPreemptible();
|
|
|
|
}
|
|
|
|
|
2016-09-23 21:54:48 +08:00
|
|
|
// This function is similar to the `handleTlsRelocation`. ARM and MIPS do not
|
|
|
|
// support any relaxations for TLS relocations so by factoring out ARM and MIPS
|
|
|
|
// handling in to the separate function we can simplify the code and do not
|
|
|
|
// pollute `handleTlsRelocation` by ARM and MIPS `ifs` statements.
|
2016-11-17 05:01:02 +08:00
|
|
|
template <class ELFT, class GOT>
|
2017-02-23 10:28:28 +08:00
|
|
|
static unsigned
|
|
|
|
handleNoRelaxTlsRelocation(GOT *Got, uint32_t Type, SymbolBody &Body,
|
|
|
|
InputSectionBase &C, typename ELFT::uint Offset,
|
|
|
|
int64_t Addend, RelExpr Expr) {
|
2017-03-23 11:11:03 +08:00
|
|
|
auto addModuleReloc = [&](uint64_t Off, bool LD) {
|
2016-11-30 00:23:50 +08:00
|
|
|
// The Dynamic TLS Module Index Relocation can be statically resolved to 1
|
|
|
|
// if we know that we are linking an executable. For ARM we resolve the
|
|
|
|
// relocation when writing the Got. MIPS has a custom Got implementation
|
|
|
|
// that writes the Module index in directly.
|
2017-03-18 07:29:01 +08:00
|
|
|
if (!Body.isPreemptible() && !Config->Pic && Config->EMachine == EM_ARM)
|
2016-11-30 00:23:50 +08:00
|
|
|
Got->Relocations.push_back(
|
|
|
|
{R_ABS, Target->TlsModuleIndexRel, Off, 0, &Body});
|
|
|
|
else {
|
|
|
|
SymbolBody *Dest = LD ? nullptr : &Body;
|
|
|
|
In<ELFT>::RelaDyn->addReloc(
|
|
|
|
{Target->TlsModuleIndexRel, Got, Off, false, Dest, 0});
|
|
|
|
}
|
|
|
|
};
|
2017-03-23 11:11:03 +08:00
|
|
|
|
2017-02-16 14:24:16 +08:00
|
|
|
if (isRelExprOneOf<R_MIPS_TLSLD, R_TLSLD_PC>(Expr)) {
|
2017-03-18 07:29:01 +08:00
|
|
|
if (Got->addTlsIndex() && (Config->Pic || Config->EMachine == EM_ARM))
|
2017-03-23 11:11:03 +08:00
|
|
|
addModuleReloc(Got->getTlsIndexOff(), true);
|
2016-09-08 04:37:34 +08:00
|
|
|
C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
|
2016-06-23 23:26:31 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2017-03-23 11:11:03 +08:00
|
|
|
|
2016-06-23 23:26:31 +08:00
|
|
|
if (Target->isTlsGlobalDynamicRel(Type)) {
|
2016-11-17 05:01:02 +08:00
|
|
|
if (Got->addDynTlsEntry(Body) &&
|
2016-09-23 21:54:48 +08:00
|
|
|
(Body.isPreemptible() || Config->EMachine == EM_ARM)) {
|
2017-03-23 11:11:03 +08:00
|
|
|
uint64_t Off = Got->getGlobalDynOffset(Body);
|
|
|
|
addModuleReloc(Off, false);
|
2016-09-23 21:54:48 +08:00
|
|
|
if (Body.isPreemptible())
|
2016-11-17 05:01:02 +08:00
|
|
|
In<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, Got,
|
2017-03-23 11:11:03 +08:00
|
|
|
Off + Config->Wordsize, false, &Body, 0});
|
2016-06-23 23:26:31 +08:00
|
|
|
}
|
2016-09-08 04:37:34 +08:00
|
|
|
C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
|
2016-06-23 23:26:31 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2017-03-23 11:11:03 +08:00
|
|
|
|
2016-06-23 23:26:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-25 04:24:43 +08:00
|
|
|
// Returns the number of relocations processed.
|
|
|
|
template <class ELFT>
|
2017-02-16 08:12:34 +08:00
|
|
|
static unsigned
|
2017-02-23 10:28:28 +08:00
|
|
|
handleTlsRelocation(uint32_t Type, SymbolBody &Body, InputSectionBase &C,
|
2017-02-16 08:12:34 +08:00
|
|
|
typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
|
2017-03-23 11:44:08 +08:00
|
|
|
typedef typename ELFT::uint uintX_t;
|
|
|
|
|
2016-10-26 20:36:56 +08:00
|
|
|
if (!(C.Flags & SHF_ALLOC))
|
2016-05-25 04:24:43 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!Body.isTls())
|
|
|
|
return 0;
|
|
|
|
|
2016-11-17 05:01:02 +08:00
|
|
|
if (Config->EMachine == EM_ARM)
|
|
|
|
return handleNoRelaxTlsRelocation<ELFT>(In<ELFT>::Got, Type, Body, C,
|
|
|
|
Offset, Addend, Expr);
|
|
|
|
if (Config->EMachine == EM_MIPS)
|
|
|
|
return handleNoRelaxTlsRelocation<ELFT>(In<ELFT>::MipsGot, Type, Body, C,
|
|
|
|
Offset, Addend, Expr);
|
2016-06-23 23:26:31 +08:00
|
|
|
|
2016-12-14 00:59:19 +08:00
|
|
|
bool IsPreemptible = isPreemptible(Body, Type);
|
2017-02-16 14:24:16 +08:00
|
|
|
if (isRelExprOneOf<R_TLSDESC, R_TLSDESC_PAGE, R_TLSDESC_CALL>(Expr) &&
|
2016-06-03 03:49:53 +08:00
|
|
|
Config->Shared) {
|
2016-11-11 19:33:32 +08:00
|
|
|
if (In<ELFT>::Got->addDynTlsEntry(Body)) {
|
|
|
|
uintX_t Off = In<ELFT>::Got->getGlobalDynOffset(Body);
|
2016-12-19 19:58:01 +08:00
|
|
|
In<ELFT>::RelaDyn->addReloc({Target->TlsDescRel, In<ELFT>::Got, Off,
|
2016-12-20 00:50:20 +08:00
|
|
|
!IsPreemptible, &Body, 0});
|
2016-06-03 03:49:53 +08:00
|
|
|
}
|
2016-10-20 17:59:26 +08:00
|
|
|
if (Expr != R_TLSDESC_CALL)
|
2016-09-08 04:37:34 +08:00
|
|
|
C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
|
2016-06-03 03:49:53 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-02-16 14:24:16 +08:00
|
|
|
if (isRelExprOneOf<R_TLSLD_PC, R_TLSLD>(Expr)) {
|
2016-05-25 04:24:43 +08:00
|
|
|
// Local-Dynamic relocs can be relaxed to Local-Exec.
|
|
|
|
if (!Config->Shared) {
|
|
|
|
C.Relocations.push_back(
|
2016-09-08 04:37:34 +08:00
|
|
|
{R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body});
|
2016-05-25 04:24:43 +08:00
|
|
|
return 2;
|
|
|
|
}
|
2016-11-11 19:33:32 +08:00
|
|
|
if (In<ELFT>::Got->addTlsIndex())
|
2016-11-16 18:02:27 +08:00
|
|
|
In<ELFT>::RelaDyn->addReloc({Target->TlsModuleIndexRel, In<ELFT>::Got,
|
|
|
|
In<ELFT>::Got->getTlsIndexOff(), false,
|
|
|
|
nullptr, 0});
|
2016-09-08 04:37:34 +08:00
|
|
|
C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
|
2016-05-25 04:24:43 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local-Dynamic relocs can be relaxed to Local-Exec.
|
|
|
|
if (Target->isTlsLocalDynamicRel(Type) && !Config->Shared) {
|
|
|
|
C.Relocations.push_back(
|
2016-09-08 04:37:34 +08:00
|
|
|
{R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Body});
|
2016-05-25 04:24:43 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-02-16 14:24:16 +08:00
|
|
|
if (isRelExprOneOf<R_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL>(Expr) ||
|
2016-06-03 03:49:53 +08:00
|
|
|
Target->isTlsGlobalDynamicRel(Type)) {
|
2016-05-25 04:24:43 +08:00
|
|
|
if (Config->Shared) {
|
2016-11-11 19:33:32 +08:00
|
|
|
if (In<ELFT>::Got->addDynTlsEntry(Body)) {
|
|
|
|
uintX_t Off = In<ELFT>::Got->getGlobalDynOffset(Body);
|
2016-11-16 18:02:27 +08:00
|
|
|
In<ELFT>::RelaDyn->addReloc(
|
2016-11-11 19:33:32 +08:00
|
|
|
{Target->TlsModuleIndexRel, In<ELFT>::Got, Off, false, &Body, 0});
|
2016-06-09 05:31:59 +08:00
|
|
|
|
|
|
|
// If the symbol is preemptible we need the dynamic linker to write
|
|
|
|
// the offset too.
|
2016-11-29 11:45:36 +08:00
|
|
|
uintX_t OffsetOff = Off + (uintX_t)sizeof(uintX_t);
|
2016-12-14 00:59:19 +08:00
|
|
|
if (IsPreemptible)
|
2016-11-16 18:02:27 +08:00
|
|
|
In<ELFT>::RelaDyn->addReloc({Target->TlsOffsetRel, In<ELFT>::Got,
|
2016-11-29 11:45:36 +08:00
|
|
|
OffsetOff, false, &Body, 0});
|
|
|
|
else
|
|
|
|
In<ELFT>::Got->Relocations.push_back(
|
|
|
|
{R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Body});
|
2016-05-25 04:24:43 +08:00
|
|
|
}
|
2016-09-08 04:37:34 +08:00
|
|
|
C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
|
2016-05-25 04:24:43 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
|
|
|
|
// depending on the symbol being locally defined or not.
|
2016-12-14 00:59:19 +08:00
|
|
|
if (IsPreemptible) {
|
2016-05-25 04:24:43 +08:00
|
|
|
C.Relocations.push_back(
|
2016-06-05 07:22:34 +08:00
|
|
|
{Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
|
2016-09-08 04:37:34 +08:00
|
|
|
Offset, Addend, &Body});
|
2016-05-25 04:24:43 +08:00
|
|
|
if (!Body.isInGot()) {
|
2016-11-11 19:33:32 +08:00
|
|
|
In<ELFT>::Got->addEntry(Body);
|
2016-11-16 18:02:27 +08:00
|
|
|
In<ELFT>::RelaDyn->addReloc({Target->TlsGotRel, In<ELFT>::Got,
|
2017-03-17 22:12:51 +08:00
|
|
|
Body.getGotOffset(), false, &Body, 0});
|
2016-05-25 04:24:43 +08:00
|
|
|
}
|
2017-03-23 11:44:08 +08:00
|
|
|
} else {
|
|
|
|
C.Relocations.push_back(
|
|
|
|
{Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
|
|
|
|
Offset, Addend, &Body});
|
2016-05-25 04:24:43 +08:00
|
|
|
}
|
2016-06-05 07:04:39 +08:00
|
|
|
return Target->TlsGdRelaxSkip;
|
2016-05-25 04:24:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
|
|
|
|
// defined.
|
2016-12-14 00:59:19 +08:00
|
|
|
if (Target->isTlsInitialExecRel(Type) && !Config->Shared && !IsPreemptible) {
|
2016-05-25 04:24:43 +08:00
|
|
|
C.Relocations.push_back(
|
2016-09-08 04:37:34 +08:00
|
|
|
{R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Body});
|
2016-05-25 04:24:43 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
|
|
|
|
return read32<E>(Loc) & 0xffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class RelTy>
|
|
|
|
static uint32_t getMipsPairType(const RelTy *Rel, const SymbolBody &Sym) {
|
2017-03-18 07:29:01 +08:00
|
|
|
switch (Rel->getType(Config->IsMips64EL)) {
|
2016-05-25 04:24:43 +08:00
|
|
|
case R_MIPS_HI16:
|
|
|
|
return R_MIPS_LO16;
|
|
|
|
case R_MIPS_GOT16:
|
|
|
|
return Sym.isLocal() ? R_MIPS_LO16 : R_MIPS_NONE;
|
|
|
|
case R_MIPS_PCHI16:
|
|
|
|
return R_MIPS_PCLO16;
|
|
|
|
case R_MICROMIPS_HI16:
|
|
|
|
return R_MICROMIPS_LO16;
|
|
|
|
default:
|
|
|
|
return R_MIPS_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT, class RelTy>
|
|
|
|
static int32_t findMipsPairedAddend(const uint8_t *Buf, const uint8_t *BufLoc,
|
|
|
|
SymbolBody &Sym, const RelTy *Rel,
|
|
|
|
const RelTy *End) {
|
2017-03-18 07:29:01 +08:00
|
|
|
uint32_t SymIndex = Rel->getSymbol(Config->IsMips64EL);
|
2016-05-25 04:24:43 +08:00
|
|
|
uint32_t Type = getMipsPairType(Rel, Sym);
|
|
|
|
|
|
|
|
// Some MIPS relocations use addend calculated from addend of the relocation
|
|
|
|
// itself and addend of paired relocation. ABI requires to compute such
|
|
|
|
// combined addend in case of REL relocation record format only.
|
|
|
|
// See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
|
|
|
|
if (RelTy::IsRela || Type == R_MIPS_NONE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (const RelTy *RI = Rel; RI != End; ++RI) {
|
2017-03-18 07:29:01 +08:00
|
|
|
if (RI->getType(Config->IsMips64EL) != Type)
|
2016-05-25 04:24:43 +08:00
|
|
|
continue;
|
2017-03-18 07:29:01 +08:00
|
|
|
if (RI->getSymbol(Config->IsMips64EL) != SymIndex)
|
2016-05-25 04:24:43 +08:00
|
|
|
continue;
|
|
|
|
const endianness E = ELFT::TargetEndianness;
|
|
|
|
return ((read32<E>(BufLoc) & 0xffff) << 16) +
|
|
|
|
readSignedLo16<E>(Buf + RI->r_offset);
|
|
|
|
}
|
2016-11-24 02:07:33 +08:00
|
|
|
warn("can't find matching " + toString(Type) + " relocation for " +
|
2017-03-18 07:29:01 +08:00
|
|
|
toString(Rel->getType(Config->IsMips64EL)));
|
2016-05-25 04:24:43 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// True if non-preemptable symbol always has the same value regardless of where
|
|
|
|
// the DSO is loaded.
|
|
|
|
template <class ELFT> static bool isAbsolute(const SymbolBody &Body) {
|
|
|
|
if (Body.isUndefined())
|
|
|
|
return !Body.isLocal() && Body.symbol()->isWeak();
|
2017-03-01 03:29:55 +08:00
|
|
|
if (const auto *DR = dyn_cast<DefinedRegular>(&Body))
|
2016-05-25 04:24:43 +08:00
|
|
|
return DR->Section == nullptr; // Absolute symbol.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-28 01:28:56 +08:00
|
|
|
template <class ELFT> static bool isAbsoluteValue(const SymbolBody &Body) {
|
|
|
|
return isAbsolute<ELFT>(Body) || Body.isTls();
|
|
|
|
}
|
|
|
|
|
2016-05-25 04:24:43 +08:00
|
|
|
static bool needsPlt(RelExpr Expr) {
|
2017-02-01 18:26:03 +08:00
|
|
|
return isRelExprOneOf<R_PLT_PC, R_PPC_PLT_OPD, R_PLT, R_PLT_PAGE_PC>(Expr);
|
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) {
|
2016-12-01 13:43:48 +08:00
|
|
|
return isRelExprOneOf<R_PC, R_GOTREL, R_GOTREL_FROM_END, R_MIPS_GOTREL,
|
2017-02-01 18:26:03 +08:00
|
|
|
R_PAGE_PC, R_RELAX_GOT_PC>(Expr);
|
2016-05-25 04:24:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2017-02-23 10:28:28 +08:00
|
|
|
static bool
|
|
|
|
isStaticLinkTimeConstant(RelExpr E, uint32_t Type, const SymbolBody &Body,
|
|
|
|
InputSectionBase &S, typename ELFT::uint RelOff) {
|
2016-05-25 04:24:43 +08:00
|
|
|
// These expressions always compute a constant
|
2016-12-01 13:43:48 +08:00
|
|
|
if (isRelExprOneOf<R_SIZE, R_GOT_FROM_END, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE,
|
2017-03-21 05:03:43 +08:00
|
|
|
R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC,
|
|
|
|
R_MIPS_TLSGD, R_GOT_PAGE_PC, R_GOT_PC, R_PLT_PC,
|
|
|
|
R_TLSGD_PC, R_TLSGD, R_PPC_PLT_OPD, R_TLSDESC_CALL,
|
|
|
|
R_TLSDESC_PAGE, R_HINT>(E))
|
2016-05-25 04:24:43 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// These never do, except if the entire file is position dependent or if
|
|
|
|
// only the low bits are used.
|
2016-06-03 03:49:53 +08:00
|
|
|
if (E == R_GOT || E == R_PLT || E == R_TLSDESC)
|
2017-03-18 07:29:01 +08:00
|
|
|
return Target->usesOnlyLowPageBits(Type) || !Config->Pic;
|
2016-05-25 04:24:43 +08:00
|
|
|
|
2016-05-28 12:49:57 +08:00
|
|
|
if (isPreemptible(Body, Type))
|
2016-05-25 04:24:43 +08:00
|
|
|
return false;
|
|
|
|
|
2017-03-18 07:29:01 +08:00
|
|
|
if (!Config->Pic)
|
2016-05-25 04:24:43 +08:00
|
|
|
return true;
|
|
|
|
|
2016-10-28 01:28:56 +08:00
|
|
|
bool AbsVal = isAbsoluteValue<ELFT>(Body);
|
2016-05-25 04:24:43 +08:00
|
|
|
bool RelE = isRelExpr(E);
|
|
|
|
if (AbsVal && !RelE)
|
|
|
|
return true;
|
|
|
|
if (!AbsVal && RelE)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Relative relocation to an absolute value. This is normally unrepresentable,
|
|
|
|
// but if the relocation refers to a weak undefined symbol, we allow it to
|
|
|
|
// resolve to the image base. This is a little strange, but it allows us to
|
|
|
|
// link function calls to such symbols. Normally such a call will be guarded
|
|
|
|
// with a comparison, which will load a zero from the GOT.
|
2016-12-08 14:19:47 +08:00
|
|
|
// Another special case is MIPS _gp_disp symbol which represents offset
|
|
|
|
// between start of a function and '_gp' value and defined as absolute just
|
|
|
|
// to simplify the code.
|
2016-05-25 04:24:43 +08:00
|
|
|
if (AbsVal && RelE) {
|
|
|
|
if (Body.isUndefined() && !Body.isLocal() && Body.symbol()->isWeak())
|
|
|
|
return true;
|
2017-02-23 10:28:28 +08:00
|
|
|
error(S.getLocation<ELFT>(RelOff) + ": relocation " + toString(Type) +
|
2016-11-25 04:24:18 +08:00
|
|
|
" cannot refer to absolute symbol '" + toString(Body) +
|
2016-11-24 02:07:33 +08:00
|
|
|
"' defined in " + toString(Body.File));
|
2016-05-25 04:24:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Target->usesOnlyLowPageBits(Type);
|
|
|
|
}
|
|
|
|
|
|
|
|
static RelExpr toPlt(RelExpr Expr) {
|
|
|
|
if (Expr == R_PPC_OPD)
|
|
|
|
return R_PPC_PLT_OPD;
|
|
|
|
if (Expr == R_PC)
|
|
|
|
return R_PLT_PC;
|
2016-06-05 03:11:14 +08:00
|
|
|
if (Expr == R_PAGE_PC)
|
|
|
|
return R_PLT_PAGE_PC;
|
2016-05-25 04:24:43 +08:00
|
|
|
if (Expr == R_ABS)
|
|
|
|
return R_PLT;
|
|
|
|
return Expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
if (Expr == R_PLT_PC)
|
|
|
|
return R_PC;
|
|
|
|
if (Expr == R_PPC_PLT_OPD)
|
|
|
|
return R_PPC_OPD;
|
|
|
|
if (Expr == R_PLT)
|
|
|
|
return R_ABS;
|
|
|
|
return Expr;
|
|
|
|
}
|
|
|
|
|
2017-02-27 07:35:34 +08:00
|
|
|
template <class ELFT> static bool isReadOnly(SharedSymbol *SS) {
|
2017-01-10 09:21:50 +08:00
|
|
|
typedef typename ELFT::Phdr Elf_Phdr;
|
2017-02-27 07:35:34 +08:00
|
|
|
uint64_t Value = SS->getValue<ELFT>();
|
2017-01-10 09:21:50 +08:00
|
|
|
|
|
|
|
// Determine if the symbol is read-only by scanning the DSO's program headers.
|
2017-02-27 07:35:34 +08:00
|
|
|
auto *File = cast<SharedFile<ELFT>>(SS->File);
|
|
|
|
for (const Elf_Phdr &Phdr : check(File->getObj().program_headers()))
|
2017-01-10 09:21:50 +08:00
|
|
|
if ((Phdr.p_type == ELF::PT_LOAD || Phdr.p_type == ELF::PT_GNU_RELRO) &&
|
|
|
|
!(Phdr.p_flags & ELF::PF_W) && Value >= Phdr.p_vaddr &&
|
|
|
|
Value < Phdr.p_vaddr + Phdr.p_memsz)
|
|
|
|
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.
|
|
|
|
// Otherwise, they would refer different places at runtime.
|
|
|
|
template <class ELFT>
|
2017-02-27 07:35:34 +08:00
|
|
|
static std::vector<SharedSymbol *> getSymbolsAt(SharedSymbol *SS) {
|
2017-02-16 12:39:45 +08:00
|
|
|
typedef typename ELFT::Sym Elf_Sym;
|
|
|
|
|
2017-02-27 07:35:34 +08:00
|
|
|
auto *File = cast<SharedFile<ELFT>>(SS->File);
|
|
|
|
uint64_t Shndx = SS->getShndx<ELFT>();
|
|
|
|
uint64_t Value = SS->getValue<ELFT>();
|
|
|
|
|
|
|
|
std::vector<SharedSymbol *> Ret;
|
|
|
|
for (const Elf_Sym &S : File->getGlobalSymbols()) {
|
|
|
|
if (S.st_shndx != Shndx || S.st_value != Value)
|
2017-02-16 12:39:45 +08:00
|
|
|
continue;
|
2017-02-27 07:35:34 +08:00
|
|
|
StringRef Name = check(S.getName(File->getStringTable()));
|
2017-02-16 12:39:45 +08:00
|
|
|
SymbolBody *Sym = Symtab<ELFT>::X->find(Name);
|
2017-02-27 07:35:34 +08:00
|
|
|
if (auto *Alias = dyn_cast_or_null<SharedSymbol>(Sym))
|
2017-02-16 12:39:45 +08:00
|
|
|
Ret.push_back(Alias);
|
|
|
|
}
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
// various other informtion about the symbol. So, such attributes become a
|
|
|
|
// 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
|
|
|
|
// debug. What's a solution? Instead of exporting a varaible V from a DSO,
|
|
|
|
// define an accessor getV().
|
2017-02-27 07:35:34 +08:00
|
|
|
template <class ELFT> static void addCopyRelSymbol(SharedSymbol *SS) {
|
2016-05-25 04:24:43 +08:00
|
|
|
typedef typename ELFT::uint uintX_t;
|
|
|
|
|
|
|
|
// Copy relocation against zero-sized symbol doesn't make sense.
|
|
|
|
uintX_t SymSize = SS->template getSize<ELFT>();
|
|
|
|
if (SymSize == 0)
|
2016-11-25 04:24:18 +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.
|
2017-02-27 07:35:34 +08:00
|
|
|
bool IsReadOnly = isReadOnly<ELFT>(SS);
|
2017-03-17 18:14:53 +08:00
|
|
|
BssSection *Sec = IsReadOnly ? In<ELFT>::BssRelRo : In<ELFT>::Bss;
|
|
|
|
uintX_t Off = Sec->reserveSpace(SS->getAlignment<ELFT>(), SymSize);
|
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.
|
2017-02-27 07:35:34 +08:00
|
|
|
for (SharedSymbol *Sym : getSymbolsAt<ELFT>(SS)) {
|
2017-03-09 01:24:24 +08:00
|
|
|
Sym->NeedsCopy = true;
|
2017-03-17 18:14:53 +08:00
|
|
|
Sym->CopyRelSec = Sec;
|
|
|
|
Sym->CopyRelSecOff = Off;
|
2017-02-20 10:22:56 +08:00
|
|
|
Sym->symbol()->IsUsedInRegularObj = true;
|
2016-05-25 04:24:43 +08:00
|
|
|
}
|
2017-02-16 12:39:45 +08:00
|
|
|
|
2017-03-17 18:14:53 +08:00
|
|
|
In<ELFT>::RelaDyn->addReloc({Target->CopyRel, Sec, Off, false, SS, 0});
|
2016-05-25 04:24:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
static RelExpr adjustExpr(const elf::ObjectFile<ELFT> &File, SymbolBody &Body,
|
2016-05-25 22:31:37 +08:00
|
|
|
bool IsWrite, RelExpr Expr, uint32_t Type,
|
2017-02-23 10:28:28 +08:00
|
|
|
const uint8_t *Data, InputSectionBase &S,
|
2016-11-15 16:07:14 +08:00
|
|
|
typename ELFT::uint RelOff) {
|
2016-05-28 12:49:57 +08:00
|
|
|
bool Preemptible = isPreemptible(Body, Type);
|
2016-05-25 22:31:37 +08:00
|
|
|
if (Body.isGnuIFunc()) {
|
2016-05-25 04:24:43 +08:00
|
|
|
Expr = toPlt(Expr);
|
2016-05-25 22:31:37 +08:00
|
|
|
} else if (!Preemptible) {
|
|
|
|
if (needsPlt(Expr))
|
|
|
|
Expr = fromPlt(Expr);
|
2016-10-28 01:28:56 +08:00
|
|
|
if (Expr == R_GOT_PC && !isAbsoluteValue<ELFT>(Body))
|
2016-06-17 23:01:50 +08:00
|
|
|
Expr = Target->adjustRelaxExpr(Type, Data, Expr);
|
2016-05-25 22:31:37 +08:00
|
|
|
}
|
2016-05-25 04:24:43 +08:00
|
|
|
|
2016-11-15 16:07:14 +08:00
|
|
|
if (IsWrite || isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, S, RelOff))
|
2016-05-25 04:24:43 +08:00
|
|
|
return Expr;
|
|
|
|
|
|
|
|
// This relocation would require the dynamic linker to write a value to read
|
|
|
|
// only memory. We can hack around it if we are producing an executable and
|
|
|
|
// the refered symbol can be preemepted to refer to the executable.
|
2017-03-18 07:29:01 +08:00
|
|
|
if (Config->Shared || (Config->Pic && !isRelExpr(Expr))) {
|
2017-03-15 06:50:07 +08:00
|
|
|
error(S.getLocation<ELFT>(RelOff) + ": can't create dynamic relocation " +
|
|
|
|
toString(Type) + " against " +
|
|
|
|
(Body.getName().empty() ? "local symbol in readonly segment"
|
|
|
|
: "symbol '" + toString(Body) + "'") +
|
|
|
|
" defined in " + toString(Body.File));
|
2016-05-25 04:24:43 +08:00
|
|
|
return Expr;
|
|
|
|
}
|
|
|
|
if (Body.getVisibility() != STV_DEFAULT) {
|
2017-02-23 10:28:28 +08:00
|
|
|
error(S.getLocation<ELFT>(RelOff) + ": cannot preempt symbol '" +
|
|
|
|
toString(Body) + "' defined in " + toString(Body.File));
|
2016-05-25 04:24:43 +08:00
|
|
|
return Expr;
|
|
|
|
}
|
|
|
|
if (Body.isObject()) {
|
|
|
|
// Produce a copy relocation.
|
2017-02-27 07:35:34 +08:00
|
|
|
auto *B = cast<SharedSymbol>(&Body);
|
2017-03-09 01:24:24 +08:00
|
|
|
if (!B->NeedsCopy) {
|
|
|
|
if (Config->ZNocopyreloc)
|
|
|
|
error(S.getLocation<ELFT>(RelOff) + ": unresolvable relocation " +
|
|
|
|
toString(Type) + " against symbol '" + toString(*B) +
|
|
|
|
"'; recompile with -fPIC or remove '-z nocopyreloc'");
|
2017-02-22 05:41:50 +08:00
|
|
|
|
2017-03-09 01:24:24 +08:00
|
|
|
addCopyRelSymbol<ELFT>(B);
|
|
|
|
}
|
2016-05-25 04:24:43 +08:00
|
|
|
return Expr;
|
|
|
|
}
|
|
|
|
if (Body.isFunc()) {
|
|
|
|
// 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
|
|
|
|
// else will use the the PLT entry as the address.
|
|
|
|
//
|
|
|
|
// 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).
|
2017-02-16 14:12:22 +08:00
|
|
|
Body.NeedsPltAddr = true;
|
2016-05-25 04:24:43 +08:00
|
|
|
return toPlt(Expr);
|
|
|
|
}
|
2016-11-25 04:24:18 +08:00
|
|
|
error("symbol '" + toString(Body) + "' defined in " + toString(Body.File) +
|
2016-11-17 01:24:06 +08:00
|
|
|
" is missing type");
|
2016-05-25 04:24:43 +08:00
|
|
|
|
|
|
|
return Expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT, class RelTy>
|
2017-02-16 08:12:34 +08:00
|
|
|
static int64_t computeAddend(const elf::ObjectFile<ELFT> &File,
|
|
|
|
const uint8_t *SectionData, const RelTy *End,
|
|
|
|
const RelTy &RI, RelExpr Expr, SymbolBody &Body) {
|
2017-03-18 07:29:01 +08:00
|
|
|
uint32_t Type = RI.getType(Config->IsMips64EL);
|
2017-02-16 08:12:34 +08:00
|
|
|
int64_t Addend = getAddend<ELFT>(RI);
|
2016-05-25 04:24:43 +08:00
|
|
|
const uint8_t *BufLoc = SectionData + RI.r_offset;
|
|
|
|
if (!RelTy::IsRela)
|
|
|
|
Addend += Target->getImplicitAddend(BufLoc, Type);
|
|
|
|
if (Config->EMachine == EM_MIPS) {
|
|
|
|
Addend += findMipsPairedAddend<ELFT>(SectionData, BufLoc, Body, &RI, End);
|
2016-11-17 05:01:02 +08:00
|
|
|
if (Expr == R_MIPS_GOTREL && Body.isLocal())
|
|
|
|
Addend += File.MipsGp0;
|
2016-05-25 04:24:43 +08:00
|
|
|
}
|
2017-03-18 07:29:01 +08:00
|
|
|
if (Config->Pic && Config->EMachine == EM_PPC64 && Type == R_PPC64_TOC)
|
2016-05-25 04:24:43 +08:00
|
|
|
Addend += getPPC64TocBase();
|
|
|
|
return Addend;
|
|
|
|
}
|
|
|
|
|
2016-10-26 19:07:09 +08:00
|
|
|
template <class ELFT>
|
2017-02-23 10:28:28 +08:00
|
|
|
static void reportUndefined(SymbolBody &Sym, InputSectionBase &S,
|
2016-10-26 19:07:09 +08:00
|
|
|
typename ELFT::uint Offset) {
|
2017-03-24 09:13:30 +08:00
|
|
|
if (Config->UnresolvedSymbols == UnresolvedPolicy::IgnoreAll)
|
|
|
|
return;
|
|
|
|
|
2017-01-27 23:52:08 +08:00
|
|
|
bool CanBeExternal = Sym.symbol()->computeBinding() != STB_LOCAL &&
|
|
|
|
Sym.getVisibility() == STV_DEFAULT;
|
2017-03-24 09:13:30 +08:00
|
|
|
if (Config->UnresolvedSymbols == UnresolvedPolicy::Ignore && CanBeExternal)
|
2016-10-06 17:45:04 +08:00
|
|
|
return;
|
|
|
|
|
2017-02-23 10:28:28 +08:00
|
|
|
std::string Msg = S.getLocation<ELFT>(Offset) + ": undefined symbol '" +
|
|
|
|
toString(Sym) + "'";
|
2016-10-06 17:45:04 +08:00
|
|
|
|
2017-01-27 23:52:08 +08:00
|
|
|
if (Config->UnresolvedSymbols == UnresolvedPolicy::WarnAll ||
|
2017-03-18 05:32:49 +08:00
|
|
|
(Config->UnresolvedSymbols == UnresolvedPolicy::Warn && CanBeExternal)) {
|
2016-10-06 17:45:04 +08:00
|
|
|
warn(Msg);
|
2017-03-18 05:32:49 +08:00
|
|
|
} else {
|
2016-10-06 17:45:04 +08:00
|
|
|
error(Msg);
|
2017-03-18 05:32:49 +08:00
|
|
|
if (Config->ArchiveWithoutSymbolsSeen) {
|
|
|
|
message("At least one archive listed no symbols in its index."
|
|
|
|
" This can happen when creating archives with a version"
|
|
|
|
" of ar that does not understand the object files in"
|
|
|
|
" the archive. For example, if you are using LLVM"
|
|
|
|
" bitcode objects (such as created by -flto), you may"
|
|
|
|
" need to use llvm-ar or GNU ar with a plugin.");
|
|
|
|
// Reset to false so that we print the message only once.
|
|
|
|
Config->ArchiveWithoutSymbolsSeen = false;
|
|
|
|
}
|
|
|
|
}
|
2016-10-06 17:45:04 +08:00
|
|
|
}
|
|
|
|
|
2016-11-06 06:58:01 +08:00
|
|
|
template <class RelTy>
|
|
|
|
static std::pair<uint32_t, uint32_t>
|
|
|
|
mergeMipsN32RelTypes(uint32_t Type, uint32_t Offset, RelTy *I, RelTy *E) {
|
|
|
|
// 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.
|
|
|
|
uint32_t Processed = 0;
|
|
|
|
for (; I != E && Offset == I->r_offset; ++I) {
|
|
|
|
++Processed;
|
2017-03-18 07:29:01 +08:00
|
|
|
Type |= I->getType(Config->IsMips64EL) << (8 * Processed);
|
2016-11-06 06:58:01 +08:00
|
|
|
}
|
|
|
|
return std::make_pair(Type, Processed);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
template <class ELFT, class RelTy>
|
2017-02-23 10:28:28 +08:00
|
|
|
static void scanRelocs(InputSectionBase &C, ArrayRef<RelTy> Rels) {
|
2016-05-25 04:24:43 +08:00
|
|
|
typedef typename ELFT::uint uintX_t;
|
|
|
|
|
2016-10-26 20:36:56 +08:00
|
|
|
bool IsWrite = C.Flags & SHF_WRITE;
|
2017-03-15 06:50:07 +08:00
|
|
|
if (!Config->ZText)
|
|
|
|
IsWrite = true;
|
2016-05-25 04:24:43 +08:00
|
|
|
|
2017-03-17 20:07:44 +08:00
|
|
|
auto AddDyn = [=](const DynamicReloc &Reloc) {
|
2016-11-16 18:02:27 +08:00
|
|
|
In<ELFT>::RelaDyn->addReloc(Reloc);
|
2016-05-25 04:24:43 +08:00
|
|
|
};
|
|
|
|
|
2017-02-23 10:28:28 +08:00
|
|
|
const elf::ObjectFile<ELFT> *File = C.getFile<ELFT>();
|
2016-09-12 21:13:53 +08:00
|
|
|
ArrayRef<uint8_t> SectionData = C.Data;
|
2016-05-25 04:24:43 +08:00
|
|
|
const uint8_t *Buf = SectionData.begin();
|
2016-07-20 19:47:50 +08:00
|
|
|
|
2016-07-22 05:15:32 +08:00
|
|
|
ArrayRef<EhSectionPiece> Pieces;
|
2017-03-07 05:17:18 +08:00
|
|
|
if (auto *Eh = dyn_cast<EhInputSection>(&C))
|
2016-07-22 05:15:32 +08:00
|
|
|
Pieces = Eh->Pieces;
|
|
|
|
|
|
|
|
ArrayRef<EhSectionPiece>::iterator PieceI = Pieces.begin();
|
|
|
|
ArrayRef<EhSectionPiece>::iterator PieceE = Pieces.end();
|
2016-07-20 19:47:50 +08:00
|
|
|
|
2016-05-25 04:24:43 +08:00
|
|
|
for (auto I = Rels.begin(), E = Rels.end(); I != E; ++I) {
|
|
|
|
const RelTy &RI = *I;
|
2016-11-15 15:32:51 +08:00
|
|
|
SymbolBody &Body = File->getRelocTargetSym(RI);
|
2017-03-18 07:29:01 +08:00
|
|
|
uint32_t Type = RI.getType(Config->IsMips64EL);
|
2016-05-25 04:24:43 +08:00
|
|
|
|
2016-11-06 06:58:01 +08:00
|
|
|
if (Config->MipsN32Abi) {
|
|
|
|
uint32_t Processed;
|
|
|
|
std::tie(Type, Processed) =
|
|
|
|
mergeMipsN32RelTypes(Type, RI.r_offset, I + 1, E);
|
|
|
|
I += Processed;
|
|
|
|
}
|
|
|
|
|
2016-10-20 16:36:42 +08:00
|
|
|
// We only report undefined symbols if they are referenced somewhere in the
|
|
|
|
// code.
|
2016-10-06 17:45:04 +08:00
|
|
|
if (!Body.isLocal() && Body.isUndefined() && !Body.symbol()->isWeak())
|
2017-02-23 10:28:28 +08:00
|
|
|
reportUndefined<ELFT>(Body, C, RI.r_offset);
|
2016-10-06 17:45:04 +08:00
|
|
|
|
2016-05-25 04:24:43 +08:00
|
|
|
RelExpr Expr = Target->getRelExpr(Type, Body);
|
2017-02-23 14:22:28 +08:00
|
|
|
|
|
|
|
// Ignore "hint" relocations because they are only markers for relaxation.
|
|
|
|
if (isRelExprOneOf<R_HINT, R_NONE>(Expr))
|
|
|
|
continue;
|
|
|
|
|
2016-05-28 12:49:57 +08:00
|
|
|
bool Preemptible = isPreemptible(Body, Type);
|
2016-11-15 16:07:14 +08:00
|
|
|
Expr = adjustExpr(*File, Body, IsWrite, Expr, Type, Buf + RI.r_offset, C,
|
|
|
|
RI.r_offset);
|
2016-11-24 09:43:21 +08:00
|
|
|
if (ErrorCount)
|
2016-05-25 04:24:43 +08:00
|
|
|
continue;
|
|
|
|
|
2016-06-23 12:33:42 +08:00
|
|
|
// Skip a relocation that points to a dead piece
|
2016-07-20 19:47:50 +08:00
|
|
|
// in a eh_frame section.
|
|
|
|
while (PieceI != PieceE &&
|
|
|
|
(PieceI->InputOff + PieceI->size() <= RI.r_offset))
|
|
|
|
++PieceI;
|
2016-07-21 01:58:07 +08:00
|
|
|
|
|
|
|
// Compute the offset of this section in the output section. We do it here
|
|
|
|
// to try to compute it only once.
|
|
|
|
uintX_t Offset;
|
|
|
|
if (PieceI != PieceE) {
|
|
|
|
assert(PieceI->InputOff <= RI.r_offset && "Relocation not in any piece");
|
2016-10-20 18:55:58 +08:00
|
|
|
if (PieceI->OutputOff == -1)
|
2016-07-21 01:58:07 +08:00
|
|
|
continue;
|
|
|
|
Offset = PieceI->OutputOff + RI.r_offset - PieceI->InputOff;
|
|
|
|
} else {
|
2016-08-19 23:46:28 +08:00
|
|
|
Offset = RI.r_offset;
|
2016-07-21 01:58:07 +08:00
|
|
|
}
|
2016-06-17 23:42:36 +08:00
|
|
|
|
2016-05-25 04:24:43 +08:00
|
|
|
// This relocation does not require got entry, but it is relative to got and
|
|
|
|
// needs it to be created. Here we request for that.
|
2017-02-16 14:24:16 +08:00
|
|
|
if (isRelExprOneOf<R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_GOTREL,
|
|
|
|
R_GOTREL_FROM_END, R_PPC_TOC>(Expr))
|
2016-11-11 19:33:32 +08:00
|
|
|
In<ELFT>::Got->HasGotOffRel = true;
|
2016-05-25 04:24:43 +08:00
|
|
|
|
2017-02-16 08:12:34 +08:00
|
|
|
int64_t Addend = computeAddend(*File, Buf, E, RI, Expr, Body);
|
2016-05-25 04:24:43 +08:00
|
|
|
|
2016-07-21 01:58:07 +08:00
|
|
|
if (unsigned Processed =
|
|
|
|
handleTlsRelocation<ELFT>(Type, Body, C, Offset, Addend, Expr)) {
|
2016-05-25 04:24:43 +08:00
|
|
|
I += (Processed - 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-02-23 14:22:28 +08:00
|
|
|
if (Expr == R_TLSDESC_CALL)
|
2016-06-03 03:49:53 +08:00
|
|
|
continue;
|
|
|
|
|
2017-03-24 08:50:37 +08:00
|
|
|
if (!needsPlt(Expr) && !refersToGotEntry(Expr) &&
|
|
|
|
isPreemptible(Body, Type)) {
|
2016-05-25 04:24:43 +08:00
|
|
|
// We don't know anything about the finaly symbol. Just ask the dynamic
|
|
|
|
// linker to handle the relocation for us.
|
2016-11-25 16:56:36 +08:00
|
|
|
if (!Target->isPicRel(Type))
|
2017-02-23 10:28:28 +08:00
|
|
|
error(C.getLocation<ELFT>(Offset) + ": relocation " + toString(Type) +
|
2016-11-25 16:56:36 +08:00
|
|
|
" cannot be used against shared object; recompile with -fPIC.");
|
2016-07-21 01:58:07 +08:00
|
|
|
AddDyn({Target->getDynRel(Type), &C, Offset, false, &Body, Addend});
|
2016-11-25 16:56:36 +08:00
|
|
|
|
2016-05-25 04:24:43 +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
|
2016-06-20 05:39:37 +08:00
|
|
|
if (Config->EMachine == EM_MIPS)
|
2016-11-17 05:01:02 +08:00
|
|
|
In<ELFT>::MipsGot->addEntry(Body, Addend, Expr);
|
2016-05-25 04:24:43 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-24 08:50:37 +08:00
|
|
|
// If the relocation points to something in the file, we can process it.
|
|
|
|
bool Constant =
|
|
|
|
isStaticLinkTimeConstant<ELFT>(Expr, Type, Body, C, RI.r_offset);
|
|
|
|
|
|
|
|
// If the output being produced is position independent, the final value
|
|
|
|
// is still not known. In that case we still need some help from the
|
|
|
|
// dynamic linker. We can however do better than just copying the incoming
|
|
|
|
// relocation. We can process some of it and and just ask the dynamic
|
|
|
|
// linker to add the load address.
|
|
|
|
if (!Constant)
|
|
|
|
AddDyn({Target->RelativeRel, &C, Offset, true, &Body, Addend});
|
|
|
|
|
|
|
|
// If the produced value is a constant, we just remember to write it
|
|
|
|
// when outputting this section. We also have to do it if the format
|
|
|
|
// uses Elf_Rel, since in that case the written value is the addend.
|
|
|
|
if (Constant || !RelTy::IsRela)
|
|
|
|
C.Relocations.push_back({Expr, Type, Offset, Addend, &Body});
|
|
|
|
|
2016-05-25 04:24:43 +08:00
|
|
|
// At this point we are done with the relocated position. Some relocations
|
|
|
|
// also require us to create a got or plt entry.
|
|
|
|
|
|
|
|
// If a relocation needs PLT, we create a PLT and a GOT slot for the symbol.
|
|
|
|
if (needsPlt(Expr)) {
|
|
|
|
if (Body.isInPlt())
|
|
|
|
continue;
|
2016-12-08 20:58:55 +08:00
|
|
|
|
|
|
|
if (Body.isGnuIFunc() && !Preemptible) {
|
2017-03-17 19:41:56 +08:00
|
|
|
InX::Iplt->addEntry<ELFT>(Body);
|
2016-12-08 20:58:55 +08:00
|
|
|
In<ELFT>::IgotPlt->addEntry(Body);
|
|
|
|
In<ELFT>::RelaIplt->addReloc({Target->IRelativeRel, In<ELFT>::IgotPlt,
|
2017-03-16 20:58:11 +08:00
|
|
|
Body.getGotPltOffset(), !Preemptible,
|
|
|
|
&Body, 0});
|
2016-12-08 20:58:55 +08:00
|
|
|
} else {
|
2017-03-17 19:41:56 +08:00
|
|
|
InX::Plt->addEntry<ELFT>(Body);
|
2016-12-08 20:58:55 +08:00
|
|
|
In<ELFT>::GotPlt->addEntry(Body);
|
|
|
|
In<ELFT>::RelaPlt->addReloc({Target->PltRel, In<ELFT>::GotPlt,
|
2017-03-16 20:58:11 +08:00
|
|
|
Body.getGotPltOffset(), !Preemptible,
|
2016-12-08 20:58:55 +08:00
|
|
|
&Body, 0});
|
|
|
|
}
|
2016-05-25 04:24:43 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (refersToGotEntry(Expr)) {
|
2016-06-20 05:39:37 +08:00
|
|
|
if (Config->EMachine == EM_MIPS) {
|
2016-09-08 17:07:12 +08:00
|
|
|
// 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.
|
2016-05-25 04:24:43 +08:00
|
|
|
// 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
|
2016-11-17 05:01:02 +08:00
|
|
|
In<ELFT>::MipsGot->addEntry(Body, Addend, Expr);
|
2016-09-08 17:07:19 +08:00
|
|
|
if (Body.isTls() && Body.isPreemptible())
|
2017-03-17 22:12:51 +08:00
|
|
|
AddDyn({Target->TlsGotRel, In<ELFT>::MipsGot, Body.getGotOffset(),
|
|
|
|
false, &Body, 0});
|
2016-06-20 05:39:37 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Body.isInGot())
|
2016-05-25 04:24:43 +08:00
|
|
|
continue;
|
|
|
|
|
2016-11-11 19:33:32 +08:00
|
|
|
In<ELFT>::Got->addEntry(Body);
|
2017-03-17 22:12:51 +08:00
|
|
|
uintX_t Off = Body.getGotOffset();
|
2016-11-29 11:45:36 +08:00
|
|
|
uint32_t DynType;
|
2016-11-30 00:23:50 +08:00
|
|
|
RelExpr GotRE = R_ABS;
|
|
|
|
if (Body.isTls()) {
|
2016-11-29 11:45:36 +08:00
|
|
|
DynType = Target->TlsGotRel;
|
2016-11-30 00:23:50 +08:00
|
|
|
GotRE = R_TLS;
|
2017-03-18 07:29:01 +08:00
|
|
|
} else if (!Preemptible && Config->Pic && !isAbsolute<ELFT>(Body))
|
2016-11-29 11:45:36 +08:00
|
|
|
DynType = Target->RelativeRel;
|
|
|
|
else
|
|
|
|
DynType = Target->GotRel;
|
|
|
|
|
2016-12-02 09:57:24 +08:00
|
|
|
// FIXME: this logic is almost duplicated above.
|
2017-03-18 07:29:01 +08:00
|
|
|
bool Constant = !Preemptible && !(Config->Pic && !isAbsolute<ELFT>(Body));
|
2016-12-02 09:57:24 +08:00
|
|
|
if (!Constant)
|
2016-11-29 11:45:36 +08:00
|
|
|
AddDyn({DynType, In<ELFT>::Got, Off, !Preemptible, &Body, 0});
|
2016-12-06 20:19:24 +08:00
|
|
|
if (Constant || (!RelTy::IsRela && !Preemptible))
|
2016-11-30 00:23:50 +08:00
|
|
|
In<ELFT>::Got->Relocations.push_back({GotRE, DynType, Off, 0, &Body});
|
2016-05-25 04:24:43 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 10:28:28 +08:00
|
|
|
template <class ELFT> void scanRelocations(InputSectionBase &S) {
|
2016-11-10 22:53:24 +08:00
|
|
|
if (S.AreRelocsRela)
|
2017-02-23 10:28:28 +08:00
|
|
|
scanRelocs<ELFT>(S, S.relas<ELFT>());
|
2016-05-25 04:24:43 +08:00
|
|
|
else
|
2017-02-23 10:28:28 +08:00
|
|
|
scanRelocs<ELFT>(S, S.rels<ELFT>());
|
2016-05-25 04:24:43 +08:00
|
|
|
}
|
|
|
|
|
2017-02-01 18:26:03 +08:00
|
|
|
// 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-02-24 23:07:30 +08:00
|
|
|
static void mergeThunks(OutputSection *OS,
|
2017-03-16 18:40:50 +08:00
|
|
|
std::vector<ThunkSection *> &Thunks) {
|
2017-02-01 18:26:03 +08:00
|
|
|
// Order Thunks in ascending OutSecOff
|
2017-03-16 18:40:50 +08:00
|
|
|
auto ThunkCmp = [](const ThunkSection *A, const ThunkSection *B) {
|
2017-02-01 18:26:03 +08:00
|
|
|
return A->OutSecOff < B->OutSecOff;
|
|
|
|
};
|
|
|
|
std::stable_sort(Thunks.begin(), Thunks.end(), ThunkCmp);
|
|
|
|
|
|
|
|
// Merge sorted vectors of Thunks and InputSections by OutSecOff
|
2017-02-24 00:49:07 +08:00
|
|
|
std::vector<InputSection *> Tmp;
|
2017-02-01 18:26:03 +08:00
|
|
|
Tmp.reserve(OS->Sections.size() + Thunks.size());
|
2017-02-24 00:49:07 +08:00
|
|
|
auto MergeCmp = [](const InputSection *A, const InputSection *B) {
|
2017-02-01 18:26:03 +08:00
|
|
|
// std::merge requires a strict weak ordering.
|
|
|
|
if (A->OutSecOff < B->OutSecOff)
|
|
|
|
return true;
|
|
|
|
if (A->OutSecOff == B->OutSecOff)
|
|
|
|
// Check if Thunk is immediately before any specific Target InputSection
|
|
|
|
// for example Mips LA25 Thunks.
|
2017-03-16 18:40:50 +08:00
|
|
|
if (auto *TA = dyn_cast<ThunkSection>(A))
|
2017-02-01 18:26:03 +08:00
|
|
|
if (TA && TA->getTargetInputSection() == B)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
std::merge(OS->Sections.begin(), OS->Sections.end(), Thunks.begin(),
|
|
|
|
Thunks.end(), std::back_inserter(Tmp), MergeCmp);
|
|
|
|
OS->Sections = std::move(Tmp);
|
2017-03-16 18:24:54 +08:00
|
|
|
OS->assignOffsets();
|
2017-02-01 18:26:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process all relocations from the InputSections that have been assigned
|
|
|
|
// to OutputSections and redirect through Thunks if needed.
|
|
|
|
//
|
|
|
|
// createThunks must be called after scanRelocs has created the Relocations for
|
|
|
|
// each InputSection. It must be called before the static symbol table is
|
|
|
|
// finalized. If any Thunks are added to an OutputSection the output section
|
|
|
|
// offsets of the InputSections will change.
|
|
|
|
//
|
|
|
|
// FIXME: All Thunks are assumed to be in range of the relocation. Range
|
|
|
|
// extension Thunks are not yet supported.
|
2017-01-18 17:57:14 +08:00
|
|
|
template <class ELFT>
|
2017-03-08 22:06:24 +08:00
|
|
|
bool createThunks(ArrayRef<OutputSection *> OutputSections) {
|
2017-02-01 18:26:03 +08:00
|
|
|
// Track Symbols that already have a Thunk
|
2017-03-16 18:40:50 +08:00
|
|
|
DenseMap<SymbolBody *, Thunk *> ThunkedSymbols;
|
2017-02-01 18:26:03 +08:00
|
|
|
// Track InputSections that have a ThunkSection placed in front
|
2017-03-16 18:40:50 +08:00
|
|
|
DenseMap<InputSection *, ThunkSection *> ThunkedSections;
|
2017-02-01 18:26:03 +08:00
|
|
|
// Track the ThunksSections that need to be inserted into an OutputSection
|
2017-03-16 18:40:50 +08:00
|
|
|
std::map<OutputSection *, std::vector<ThunkSection *>> ThunkSections;
|
2017-02-01 18:26:03 +08:00
|
|
|
|
|
|
|
// Find or create a Thunk for Body for relocation Type
|
|
|
|
auto GetThunk = [&](SymbolBody &Body, uint32_t Type) {
|
|
|
|
auto res = ThunkedSymbols.insert({&Body, nullptr});
|
|
|
|
if (res.second == true)
|
|
|
|
res.first->second = addThunk<ELFT>(Type, Body);
|
|
|
|
return std::make_pair(res.first->second, res.second);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Find or create a ThunkSection to be placed immediately before IS
|
2017-02-24 23:07:30 +08:00
|
|
|
auto GetISThunkSec = [&](InputSection *IS, OutputSection *OS) {
|
2017-03-16 18:40:50 +08:00
|
|
|
ThunkSection *TS = ThunkedSections.lookup(IS);
|
2017-02-01 18:26:03 +08:00
|
|
|
if (TS)
|
|
|
|
return TS;
|
2017-02-24 23:07:30 +08:00
|
|
|
auto *TOS = cast<OutputSection>(IS->OutSec);
|
2017-03-16 18:40:50 +08:00
|
|
|
TS = make<ThunkSection>(TOS, IS->OutSecOff);
|
2017-03-07 17:45:04 +08:00
|
|
|
ThunkSections[TOS].push_back(TS);
|
2017-02-01 18:26:03 +08:00
|
|
|
ThunkedSections[IS] = TS;
|
|
|
|
return TS;
|
|
|
|
};
|
|
|
|
// Find or create a ThunkSection to be placed as last executable section in
|
|
|
|
// OS.
|
2017-03-16 18:40:50 +08:00
|
|
|
auto GetOSThunkSec = [&](ThunkSection *&TS, OutputSection *OS) {
|
2017-02-01 18:26:03 +08:00
|
|
|
if (TS == nullptr) {
|
|
|
|
uint32_t Off = 0;
|
|
|
|
for (auto *IS : OS->Sections) {
|
2017-03-08 23:44:30 +08:00
|
|
|
Off = IS->OutSecOff + IS->getSize();
|
2017-02-01 18:26:03 +08:00
|
|
|
if ((IS->Flags & SHF_EXECINSTR) == 0)
|
|
|
|
break;
|
|
|
|
}
|
2017-03-16 18:40:50 +08:00
|
|
|
TS = make<ThunkSection>(OS, Off);
|
2017-02-01 18:26:03 +08:00
|
|
|
ThunkSections[OS].push_back(TS);
|
|
|
|
}
|
|
|
|
return TS;
|
|
|
|
};
|
|
|
|
// Create all the Thunks and insert them into synthetic ThunkSections. The
|
|
|
|
// ThunkSections are later inserted back into the OutputSection.
|
|
|
|
|
|
|
|
// We separate the creation of ThunkSections from the insertion of the
|
|
|
|
// ThunkSections back into the OutputSection as ThunkSections are not always
|
|
|
|
// inserted into the same OutputSection as the caller.
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Base : OutputSections) {
|
|
|
|
auto *OS = dyn_cast<OutputSection>(Base);
|
2017-01-20 23:25:45 +08:00
|
|
|
if (OS == nullptr)
|
|
|
|
continue;
|
2017-02-01 18:26:03 +08:00
|
|
|
|
2017-03-16 18:40:50 +08:00
|
|
|
ThunkSection *OSTS = nullptr;
|
2017-02-24 00:49:07 +08:00
|
|
|
for (InputSection *IS : OS->Sections) {
|
2017-02-01 18:26:03 +08:00
|
|
|
for (Relocation &Rel : IS->Relocations) {
|
|
|
|
SymbolBody &Body = *Rel.Sym;
|
2017-03-16 18:55:45 +08:00
|
|
|
if (Target->needsThunk(Rel.Expr, Rel.Type, IS->File, Body)) {
|
2017-03-16 18:40:50 +08:00
|
|
|
Thunk *T;
|
2017-02-01 18:26:03 +08:00
|
|
|
bool IsNew;
|
|
|
|
std::tie(T, IsNew) = GetThunk(Body, Rel.Type);
|
|
|
|
if (IsNew) {
|
|
|
|
// Find or create a ThunkSection for the new Thunk
|
2017-03-16 18:40:50 +08:00
|
|
|
ThunkSection *TS;
|
2017-02-01 18:26:03 +08:00
|
|
|
if (auto *TIS = T->getTargetInputSection())
|
|
|
|
TS = GetISThunkSec(TIS, OS);
|
|
|
|
else
|
|
|
|
TS = GetOSThunkSec(OSTS, OS);
|
|
|
|
TS->addThunk(T);
|
|
|
|
}
|
|
|
|
// Redirect relocation to Thunk, we never go via the PLT to a Thunk
|
|
|
|
Rel.Sym = T->ThunkSym;
|
|
|
|
Rel.Expr = fromPlt(Rel.Expr);
|
|
|
|
}
|
2017-01-18 17:57:14 +08:00
|
|
|
}
|
2016-07-21 01:58:07 +08:00
|
|
|
}
|
|
|
|
}
|
2017-02-01 18:26:03 +08:00
|
|
|
|
|
|
|
// Merge all created synthetic ThunkSections back into OutputSection
|
|
|
|
for (auto &KV : ThunkSections)
|
2017-03-16 18:48:40 +08:00
|
|
|
mergeThunks(KV.first, KV.second);
|
2017-03-08 22:06:24 +08:00
|
|
|
return !ThunkSections.empty();
|
2016-07-21 01:58:07 +08:00
|
|
|
}
|
2016-05-25 04:24:43 +08:00
|
|
|
|
2017-02-23 10:28:28 +08:00
|
|
|
template void scanRelocations<ELF32LE>(InputSectionBase &);
|
|
|
|
template void scanRelocations<ELF32BE>(InputSectionBase &);
|
|
|
|
template void scanRelocations<ELF64LE>(InputSectionBase &);
|
|
|
|
template void scanRelocations<ELF64BE>(InputSectionBase &);
|
2016-11-10 22:53:24 +08:00
|
|
|
|
2017-03-08 22:06:24 +08:00
|
|
|
template bool createThunks<ELF32LE>(ArrayRef<OutputSection *>);
|
|
|
|
template bool createThunks<ELF32BE>(ArrayRef<OutputSection *>);
|
|
|
|
template bool createThunks<ELF64LE>(ArrayRef<OutputSection *>);
|
|
|
|
template bool createThunks<ELF64BE>(ArrayRef<OutputSection *>);
|
2016-05-25 04:24:43 +08:00
|
|
|
}
|
|
|
|
}
|