2017-06-17 01:32:43 +08:00
|
|
|
//===- PPC64.cpp ----------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Symbols.h"
|
|
|
|
#include "SyntheticSections.h"
|
|
|
|
#include "Target.h"
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2017-06-17 01:32:43 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
2018-03-20 01:40:14 +08:00
|
|
|
using namespace llvm::object;
|
|
|
|
using namespace llvm::support::endian;
|
2017-06-17 01:32:43 +08:00
|
|
|
using namespace llvm::ELF;
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
|
|
|
|
|
|
|
static uint64_t PPC64TocOffset = 0x8000;
|
|
|
|
|
|
|
|
uint64_t elf::getPPC64TocBase() {
|
|
|
|
// The TOC consists of sections .got, .toc, .tocbss, .plt in that order. The
|
|
|
|
// TOC starts where the first of these sections starts. We always create a
|
|
|
|
// .got when we see a relocation that uses it, so for us the start is always
|
|
|
|
// the .got.
|
|
|
|
uint64_t TocVA = InX::Got->getVA();
|
|
|
|
|
|
|
|
// Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
|
|
|
|
// thus permitting a full 64 Kbytes segment. Note that the glibc startup
|
|
|
|
// code (crt1.o) assumes that you can get from the TOC base to the
|
|
|
|
// start of the .toc section with only a single (signed) 16-bit relocation.
|
|
|
|
return TocVA + PPC64TocOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class PPC64 final : public TargetInfo {
|
|
|
|
public:
|
|
|
|
PPC64();
|
2018-03-20 01:40:14 +08:00
|
|
|
uint32_t calcEFlags() const override;
|
2017-11-04 05:21:47 +08:00
|
|
|
RelExpr getRelExpr(RelType Type, const Symbol &S,
|
2017-06-17 01:32:43 +08:00
|
|
|
const uint8_t *Loc) const override;
|
2018-05-09 10:07:53 +08:00
|
|
|
void writePltHeader(uint8_t *Buf) const override;
|
|
|
|
void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr,
|
|
|
|
int32_t Index, unsigned RelOff) const override;
|
2017-10-12 06:49:24 +08:00
|
|
|
void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override;
|
2018-03-20 01:40:14 +08:00
|
|
|
void writeGotHeader(uint8_t *Buf) const override;
|
2018-05-07 03:13:29 +08:00
|
|
|
bool needsThunk(RelExpr Expr, RelType Type, const InputFile *File,
|
|
|
|
uint64_t BranchAddr, const Symbol &S) const override;
|
2017-06-17 01:32:43 +08:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Relocation masks following the #lo(value), #hi(value), #ha(value),
|
|
|
|
// #higher(value), #highera(value), #highest(value), and #highesta(value)
|
|
|
|
// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
|
|
|
|
// document.
|
|
|
|
static uint16_t applyPPCLo(uint64_t V) { return V; }
|
|
|
|
static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
|
|
|
|
static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
|
|
|
|
static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
|
|
|
|
static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
|
|
|
|
static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
|
|
|
|
static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
|
|
|
|
|
|
|
|
PPC64::PPC64() {
|
2018-04-03 03:47:21 +08:00
|
|
|
GotRel = R_PPC64_GLOB_DAT;
|
2018-05-07 03:13:29 +08:00
|
|
|
PltRel = R_PPC64_JMP_SLOT;
|
2017-06-17 01:32:43 +08:00
|
|
|
RelativeRel = R_PPC64_RELATIVE;
|
|
|
|
GotEntrySize = 8;
|
2018-05-09 10:07:53 +08:00
|
|
|
PltEntrySize = 4;
|
2017-06-17 01:32:43 +08:00
|
|
|
GotPltEntrySize = 8;
|
2018-03-20 01:40:14 +08:00
|
|
|
GotBaseSymInGotPlt = false;
|
|
|
|
GotBaseSymOff = 0x8000;
|
2018-05-04 23:09:49 +08:00
|
|
|
GotHeaderEntriesNum = 1;
|
|
|
|
GotPltHeaderEntriesNum = 2;
|
2018-05-09 10:07:53 +08:00
|
|
|
PltHeaderSize = 60;
|
2018-05-07 03:13:29 +08:00
|
|
|
NeedsThunks = true;
|
2017-06-17 01:32:43 +08:00
|
|
|
|
|
|
|
// We need 64K pages (at least under glibc/Linux, the loader won't
|
|
|
|
// set different permissions on a finer granularity than that).
|
|
|
|
DefaultMaxPageSize = 65536;
|
|
|
|
|
|
|
|
// The PPC64 ELF ABI v1 spec, says:
|
|
|
|
//
|
|
|
|
// It is normally desirable to put segments with different characteristics
|
|
|
|
// in separate 256 Mbyte portions of the address space, to give the
|
|
|
|
// operating system full paging flexibility in the 64-bit address space.
|
|
|
|
//
|
|
|
|
// And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
|
|
|
|
// use 0x10000000 as the starting address.
|
|
|
|
DefaultImageBase = 0x10000000;
|
2018-04-03 05:11:13 +08:00
|
|
|
|
2018-04-20 09:21:24 +08:00
|
|
|
TrapInstr =
|
|
|
|
(Config->IsLE == sys::IsLittleEndianHost) ? 0x7fe00008 : 0x0800e07f;
|
2017-06-17 01:32:43 +08:00
|
|
|
}
|
|
|
|
|
2018-03-20 01:40:14 +08:00
|
|
|
static uint32_t getEFlags(InputFile *File) {
|
2018-05-04 23:09:49 +08:00
|
|
|
// Get the e_flag from the input file and issue an error if incompatible
|
|
|
|
// e_flag encountered.
|
2018-05-05 00:04:04 +08:00
|
|
|
uint32_t EFlags;
|
|
|
|
switch (Config->EKind) {
|
|
|
|
case ELF64BEKind:
|
|
|
|
EFlags = cast<ObjFile<ELF64BE>>(File)->getObj().getHeader()->e_flags;
|
|
|
|
break;
|
|
|
|
case ELF64LEKind:
|
|
|
|
EFlags = cast<ObjFile<ELF64LE>>(File)->getObj().getHeader()->e_flags;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown Config->EKind");
|
|
|
|
}
|
2018-05-04 23:09:49 +08:00
|
|
|
if (EFlags > 2) {
|
|
|
|
error("incompatible e_flags: " + toString(File));
|
|
|
|
return 0;
|
2018-03-20 01:40:14 +08:00
|
|
|
}
|
2018-05-04 23:09:49 +08:00
|
|
|
return EFlags;
|
2018-03-20 01:40:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t PPC64::calcEFlags() const {
|
|
|
|
assert(!ObjectFiles.empty());
|
|
|
|
|
2018-05-04 23:09:49 +08:00
|
|
|
uint32_t NonZeroFlag;
|
|
|
|
for (InputFile *F : makeArrayRef(ObjectFiles)) {
|
|
|
|
NonZeroFlag = getEFlags(F);
|
|
|
|
if (NonZeroFlag)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that all input files have either the same e_flags, or zero.
|
|
|
|
for (InputFile *F : makeArrayRef(ObjectFiles)) {
|
|
|
|
uint32_t Flag = getEFlags(F);
|
|
|
|
if (Flag == 0 || Flag == NonZeroFlag)
|
2018-03-20 01:40:14 +08:00
|
|
|
continue;
|
2018-05-04 23:09:49 +08:00
|
|
|
error(toString(F) + ": ABI version " + Twine(Flag) +
|
|
|
|
" is not compatible with ABI version " + Twine(NonZeroFlag) +
|
|
|
|
" output");
|
2018-03-20 01:40:14 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2018-05-04 23:09:49 +08:00
|
|
|
|
|
|
|
if (NonZeroFlag == 1) {
|
|
|
|
error("PPC64 V1 ABI not supported");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 2;
|
2018-03-20 01:40:14 +08:00
|
|
|
}
|
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
RelExpr PPC64::getRelExpr(RelType Type, const Symbol &S,
|
2017-10-12 11:14:06 +08:00
|
|
|
const uint8_t *Loc) const {
|
2017-06-17 01:32:43 +08:00
|
|
|
switch (Type) {
|
|
|
|
case R_PPC64_TOC16:
|
|
|
|
case R_PPC64_TOC16_DS:
|
|
|
|
case R_PPC64_TOC16_HA:
|
|
|
|
case R_PPC64_TOC16_HI:
|
|
|
|
case R_PPC64_TOC16_LO:
|
|
|
|
case R_PPC64_TOC16_LO_DS:
|
|
|
|
return R_GOTREL;
|
|
|
|
case R_PPC64_TOC:
|
|
|
|
return R_PPC_TOC;
|
|
|
|
case R_PPC64_REL24:
|
2018-05-04 23:09:49 +08:00
|
|
|
return R_PPC_CALL_PLT;
|
2018-03-21 23:04:04 +08:00
|
|
|
case R_PPC64_REL16_LO:
|
|
|
|
case R_PPC64_REL16_HA:
|
|
|
|
return R_PC;
|
2017-10-12 11:14:06 +08:00
|
|
|
default:
|
|
|
|
return R_ABS;
|
2017-06-17 01:32:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 01:40:14 +08:00
|
|
|
void PPC64::writeGotHeader(uint8_t *Buf) const {
|
2018-05-04 23:09:49 +08:00
|
|
|
write64(Buf, getPPC64TocBase());
|
2018-03-20 01:40:14 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 10:07:53 +08:00
|
|
|
void PPC64::writePltHeader(uint8_t *Buf) const {
|
|
|
|
// The generic resolver stub goes first.
|
|
|
|
write32(Buf + 0, 0x7c0802a6); // mflr r0
|
|
|
|
write32(Buf + 4, 0x429f0005); // bcl 20,4*cr7+so,8 <_glink+0x8>
|
|
|
|
write32(Buf + 8, 0x7d6802a6); // mflr r11
|
|
|
|
write32(Buf + 12, 0x7c0803a6); // mtlr r0
|
|
|
|
write32(Buf + 16, 0x7d8b6050); // subf r12, r11, r12
|
|
|
|
write32(Buf + 20, 0x380cffcc); // subi r0,r12,52
|
|
|
|
write32(Buf + 24, 0x7800f082); // srdi r0,r0,62,2
|
|
|
|
write32(Buf + 28, 0xe98b002c); // ld r12,44(r11)
|
|
|
|
write32(Buf + 32, 0x7d6c5a14); // add r11,r12,r11
|
|
|
|
write32(Buf + 36, 0xe98b0000); // ld r12,0(r11)
|
|
|
|
write32(Buf + 40, 0xe96b0008); // ld r11,8(r11)
|
|
|
|
write32(Buf + 44, 0x7d8903a6); // mtctr r12
|
|
|
|
write32(Buf + 48, 0x4e800420); // bctr
|
|
|
|
|
|
|
|
// The 'bcl' instruction will set the link register to the address of the
|
|
|
|
// following instruction ('mflr r11'). Here we store the offset from that
|
|
|
|
// instruction to the first entry in the GotPlt section.
|
|
|
|
int64_t GotPltOffset = InX::GotPlt->getVA() - (InX::Plt->getVA() + 8);
|
|
|
|
write64(Buf + 52, GotPltOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PPC64::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
|
|
|
|
uint64_t PltEntryAddr, int32_t Index,
|
|
|
|
unsigned RelOff) const {
|
|
|
|
int32_t Offset = PltHeaderSize + Index * PltEntrySize;
|
|
|
|
// bl __glink_PLTresolve
|
|
|
|
write32(Buf, 0x48000000 | ((-Offset) & 0x03FFFFFc));
|
|
|
|
}
|
|
|
|
|
2017-10-12 06:49:24 +08:00
|
|
|
static std::pair<RelType, uint64_t> toAddr16Rel(RelType Type, uint64_t Val) {
|
2017-06-17 01:32:43 +08:00
|
|
|
uint64_t V = Val - PPC64TocOffset;
|
|
|
|
switch (Type) {
|
|
|
|
case R_PPC64_TOC16:
|
|
|
|
return {R_PPC64_ADDR16, V};
|
|
|
|
case R_PPC64_TOC16_DS:
|
|
|
|
return {R_PPC64_ADDR16_DS, V};
|
|
|
|
case R_PPC64_TOC16_HA:
|
|
|
|
return {R_PPC64_ADDR16_HA, V};
|
|
|
|
case R_PPC64_TOC16_HI:
|
|
|
|
return {R_PPC64_ADDR16_HI, V};
|
|
|
|
case R_PPC64_TOC16_LO:
|
|
|
|
return {R_PPC64_ADDR16_LO, V};
|
|
|
|
case R_PPC64_TOC16_LO_DS:
|
|
|
|
return {R_PPC64_ADDR16_LO_DS, V};
|
|
|
|
default:
|
|
|
|
return {Type, Val};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 06:49:24 +08:00
|
|
|
void PPC64::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
|
2017-06-17 01:32:43 +08:00
|
|
|
// For a TOC-relative relocation, proceed in terms of the corresponding
|
|
|
|
// ADDR16 relocation type.
|
|
|
|
std::tie(Type, Val) = toAddr16Rel(Type, Val);
|
|
|
|
|
|
|
|
switch (Type) {
|
|
|
|
case R_PPC64_ADDR14: {
|
2018-03-30 06:40:52 +08:00
|
|
|
checkAlignment(Loc, Val, 4, Type);
|
2017-06-17 01:32:43 +08:00
|
|
|
// Preserve the AA/LK bits in the branch instruction
|
|
|
|
uint8_t AALK = Loc[3];
|
2018-03-10 02:03:22 +08:00
|
|
|
write16(Loc + 2, (AALK & 3) | (Val & 0xfffc));
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case R_PPC64_ADDR16:
|
2018-03-30 06:40:52 +08:00
|
|
|
checkInt(Loc, Val, 16, Type);
|
2018-03-10 02:03:22 +08:00
|
|
|
write16(Loc, Val);
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_PPC64_ADDR16_DS:
|
2018-03-30 06:40:52 +08:00
|
|
|
checkInt(Loc, Val, 16, Type);
|
2018-03-10 02:03:22 +08:00
|
|
|
write16(Loc, (read16(Loc) & 3) | (Val & ~3));
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_PPC64_ADDR16_HA:
|
|
|
|
case R_PPC64_REL16_HA:
|
2018-03-10 02:03:22 +08:00
|
|
|
write16(Loc, applyPPCHa(Val));
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_PPC64_ADDR16_HI:
|
|
|
|
case R_PPC64_REL16_HI:
|
2018-03-10 02:03:22 +08:00
|
|
|
write16(Loc, applyPPCHi(Val));
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_PPC64_ADDR16_HIGHER:
|
2018-03-10 02:03:22 +08:00
|
|
|
write16(Loc, applyPPCHigher(Val));
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_PPC64_ADDR16_HIGHERA:
|
2018-03-10 02:03:22 +08:00
|
|
|
write16(Loc, applyPPCHighera(Val));
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_PPC64_ADDR16_HIGHEST:
|
2018-03-10 02:03:22 +08:00
|
|
|
write16(Loc, applyPPCHighest(Val));
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_PPC64_ADDR16_HIGHESTA:
|
2018-03-10 02:03:22 +08:00
|
|
|
write16(Loc, applyPPCHighesta(Val));
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_PPC64_ADDR16_LO:
|
2018-03-21 23:04:04 +08:00
|
|
|
case R_PPC64_REL16_LO:
|
2018-03-10 02:03:22 +08:00
|
|
|
write16(Loc, applyPPCLo(Val));
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_PPC64_ADDR16_LO_DS:
|
2018-03-10 02:03:22 +08:00
|
|
|
write16(Loc, (read16(Loc) & 3) | (applyPPCLo(Val) & ~3));
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_PPC64_ADDR32:
|
|
|
|
case R_PPC64_REL32:
|
2018-03-30 06:40:52 +08:00
|
|
|
checkInt(Loc, Val, 32, Type);
|
2018-03-10 02:03:22 +08:00
|
|
|
write32(Loc, Val);
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_PPC64_ADDR64:
|
|
|
|
case R_PPC64_REL64:
|
|
|
|
case R_PPC64_TOC:
|
2018-03-10 02:03:22 +08:00
|
|
|
write64(Loc, Val);
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
case R_PPC64_REL24: {
|
|
|
|
uint32_t Mask = 0x03FFFFFC;
|
2018-03-30 06:40:52 +08:00
|
|
|
checkInt(Loc, Val, 24, Type);
|
2018-03-10 02:03:22 +08:00
|
|
|
write32(Loc, (read32(Loc) & ~Mask) | (Val & Mask));
|
2017-06-17 01:32:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-07 03:13:29 +08:00
|
|
|
bool PPC64::needsThunk(RelExpr Expr, RelType Type, const InputFile *File,
|
|
|
|
uint64_t BranchAddr, const Symbol &S) const {
|
|
|
|
// If a function is in the plt it needs to be called through
|
|
|
|
// a call stub.
|
|
|
|
return Type == R_PPC64_REL24 && S.isInPlt();
|
|
|
|
}
|
|
|
|
|
2017-06-17 04:15:03 +08:00
|
|
|
TargetInfo *elf::getPPC64TargetInfo() {
|
|
|
|
static PPC64 Target;
|
|
|
|
return &Target;
|
|
|
|
}
|