2017-06-29 01:05:39 +08:00
|
|
|
//===- SPARCV9.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
|
2017-06-29 01:05:39 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "InputFiles.h"
|
|
|
|
#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-29 01:05:39 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::support::endian;
|
|
|
|
using namespace llvm::ELF;
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class SPARCV9 final : public TargetInfo {
|
|
|
|
public:
|
|
|
|
SPARCV9();
|
2017-11-04 05:21:47 +08:00
|
|
|
RelExpr getRelExpr(RelType Type, const Symbol &S,
|
2017-06-29 01:05:39 +08:00
|
|
|
const uint8_t *Loc) const override;
|
|
|
|
void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, 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;
|
2017-06-29 01:05:39 +08:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
SPARCV9::SPARCV9() {
|
|
|
|
CopyRel = R_SPARC_COPY;
|
|
|
|
GotRel = R_SPARC_GLOB_DAT;
|
2018-09-26 16:11:34 +08:00
|
|
|
NoneRel = R_SPARC_NONE;
|
2017-06-29 01:05:39 +08:00
|
|
|
PltRel = R_SPARC_JMP_SLOT;
|
|
|
|
RelativeRel = R_SPARC_RELATIVE;
|
|
|
|
PltEntrySize = 32;
|
|
|
|
PltHeaderSize = 4 * PltEntrySize;
|
|
|
|
|
2019-05-14 00:01:26 +08:00
|
|
|
DefaultCommonPageSize = 8192;
|
2017-06-29 01:05:39 +08:00
|
|
|
DefaultMaxPageSize = 0x100000;
|
|
|
|
DefaultImageBase = 0x100000;
|
|
|
|
}
|
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
RelExpr SPARCV9::getRelExpr(RelType Type, const Symbol &S,
|
2017-10-12 11:14:06 +08:00
|
|
|
const uint8_t *Loc) const {
|
2017-06-29 01:05:39 +08:00
|
|
|
switch (Type) {
|
|
|
|
case R_SPARC_32:
|
|
|
|
case R_SPARC_UA32:
|
|
|
|
case R_SPARC_64:
|
|
|
|
case R_SPARC_UA64:
|
|
|
|
return R_ABS;
|
|
|
|
case R_SPARC_PC10:
|
|
|
|
case R_SPARC_PC22:
|
|
|
|
case R_SPARC_DISP32:
|
|
|
|
case R_SPARC_WDISP30:
|
|
|
|
return R_PC;
|
|
|
|
case R_SPARC_GOT10:
|
|
|
|
return R_GOT_OFF;
|
|
|
|
case R_SPARC_GOT22:
|
|
|
|
return R_GOT_OFF;
|
|
|
|
case R_SPARC_WPLT30:
|
|
|
|
return R_PLT_PC;
|
|
|
|
case R_SPARC_NONE:
|
|
|
|
return R_NONE;
|
|
|
|
default:
|
2019-02-15 02:02:20 +08:00
|
|
|
error(getErrorLocation(Loc) + "unknown relocation (" + Twine(Type) +
|
|
|
|
") against symbol " + toString(S));
|
|
|
|
return R_NONE;
|
2017-06-29 01:05:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 06:49:24 +08:00
|
|
|
void SPARCV9::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
|
2017-06-29 01:05:39 +08:00
|
|
|
switch (Type) {
|
|
|
|
case R_SPARC_32:
|
|
|
|
case R_SPARC_UA32:
|
|
|
|
// V-word32
|
2018-03-30 06:40:52 +08:00
|
|
|
checkUInt(Loc, Val, 32, Type);
|
2017-06-29 01:05:39 +08:00
|
|
|
write32be(Loc, Val);
|
|
|
|
break;
|
|
|
|
case R_SPARC_DISP32:
|
|
|
|
// V-disp32
|
2018-03-30 06:40:52 +08:00
|
|
|
checkInt(Loc, Val, 32, Type);
|
2017-06-29 01:05:39 +08:00
|
|
|
write32be(Loc, Val);
|
|
|
|
break;
|
|
|
|
case R_SPARC_WDISP30:
|
|
|
|
case R_SPARC_WPLT30:
|
|
|
|
// V-disp30
|
2018-03-30 06:40:52 +08:00
|
|
|
checkInt(Loc, Val, 32, Type);
|
2017-06-29 01:05:39 +08:00
|
|
|
write32be(Loc, (read32be(Loc) & ~0x3fffffff) | ((Val >> 2) & 0x3fffffff));
|
|
|
|
break;
|
|
|
|
case R_SPARC_22:
|
|
|
|
// V-imm22
|
2018-03-30 06:40:52 +08:00
|
|
|
checkUInt(Loc, Val, 22, Type);
|
2017-06-29 01:05:39 +08:00
|
|
|
write32be(Loc, (read32be(Loc) & ~0x003fffff) | (Val & 0x003fffff));
|
|
|
|
break;
|
|
|
|
case R_SPARC_GOT22:
|
|
|
|
case R_SPARC_PC22:
|
|
|
|
// T-imm22
|
|
|
|
write32be(Loc, (read32be(Loc) & ~0x003fffff) | ((Val >> 10) & 0x003fffff));
|
|
|
|
break;
|
|
|
|
case R_SPARC_WDISP19:
|
|
|
|
// V-disp19
|
2018-03-30 06:40:52 +08:00
|
|
|
checkInt(Loc, Val, 21, Type);
|
2017-06-29 01:05:39 +08:00
|
|
|
write32be(Loc, (read32be(Loc) & ~0x0007ffff) | ((Val >> 2) & 0x0007ffff));
|
|
|
|
break;
|
|
|
|
case R_SPARC_GOT10:
|
|
|
|
case R_SPARC_PC10:
|
|
|
|
// T-simm10
|
|
|
|
write32be(Loc, (read32be(Loc) & ~0x000003ff) | (Val & 0x000003ff));
|
|
|
|
break;
|
|
|
|
case R_SPARC_64:
|
|
|
|
case R_SPARC_UA64:
|
|
|
|
case R_SPARC_GLOB_DAT:
|
|
|
|
// V-xword64
|
|
|
|
write64be(Loc, Val);
|
|
|
|
break;
|
|
|
|
default:
|
2019-02-15 02:02:20 +08:00
|
|
|
llvm_unreachable("unknown relocation");
|
2017-06-29 01:05:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SPARCV9::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
|
|
|
|
uint64_t PltEntryAddr, int32_t Index,
|
|
|
|
unsigned RelOff) const {
|
|
|
|
const uint8_t PltData[] = {
|
|
|
|
0x03, 0x00, 0x00, 0x00, // sethi (. - .PLT0), %g1
|
|
|
|
0x30, 0x68, 0x00, 0x00, // ba,a %xcc, .PLT1
|
|
|
|
0x01, 0x00, 0x00, 0x00, // nop
|
|
|
|
0x01, 0x00, 0x00, 0x00, // nop
|
|
|
|
0x01, 0x00, 0x00, 0x00, // nop
|
|
|
|
0x01, 0x00, 0x00, 0x00, // nop
|
|
|
|
0x01, 0x00, 0x00, 0x00, // nop
|
|
|
|
0x01, 0x00, 0x00, 0x00 // nop
|
|
|
|
};
|
|
|
|
memcpy(Buf, PltData, sizeof(PltData));
|
|
|
|
|
2019-03-28 09:37:48 +08:00
|
|
|
uint64_t Off = PltHeaderSize + PltEntrySize * Index;
|
2017-06-29 01:05:39 +08:00
|
|
|
relocateOne(Buf, R_SPARC_22, Off);
|
|
|
|
relocateOne(Buf + 4, R_SPARC_WDISP19, -(Off + 4 - PltEntrySize));
|
|
|
|
}
|
|
|
|
|
|
|
|
TargetInfo *elf::getSPARCV9TargetInfo() {
|
|
|
|
static SPARCV9 Target;
|
|
|
|
return &Target;
|
|
|
|
}
|