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;
|
2019-10-07 16:31:18 +08:00
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace elf {
|
2017-06-29 01:05:39 +08:00
|
|
|
|
|
|
|
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;
|
2019-12-18 05:43:04 +08:00
|
|
|
void writePlt(uint8_t *buf, const Symbol &sym,
|
|
|
|
uint64_t pltEntryAddr) 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;
|
2019-06-11 20:59:30 +08:00
|
|
|
symbolicRel = R_SPARC_64;
|
2017-06-29 01:05:39 +08:00
|
|
|
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:
|
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-18 05:43:04 +08:00
|
|
|
void SPARCV9::writePlt(uint8_t *buf, const Symbol & /*sym*/,
|
|
|
|
uint64_t pltEntryAddr) const {
|
2017-06-29 01:05:39 +08:00
|
|
|
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-12-18 05:43:04 +08:00
|
|
|
uint64_t off = pltEntryAddr - in.plt->getVA();
|
2017-06-29 01:05:39 +08:00
|
|
|
relocateOne(buf, R_SPARC_22, off);
|
|
|
|
relocateOne(buf + 4, R_SPARC_WDISP19, -(off + 4 - pltEntrySize));
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:31:18 +08:00
|
|
|
TargetInfo *getSPARCV9TargetInfo() {
|
2017-06-29 01:05:39 +08:00
|
|
|
static SPARCV9 target;
|
|
|
|
return ⌖
|
|
|
|
}
|
2019-10-07 16:31:18 +08:00
|
|
|
|
|
|
|
} // namespace elf
|
|
|
|
} // namespace lld
|