2018-08-10 01:59:56 +08:00
|
|
|
//===- RISCV.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
|
2018-08-10 01:59:56 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "InputFiles.h"
|
2019-12-18 05:43:04 +08:00
|
|
|
#include "Symbols.h"
|
2019-07-02 01:12:18 +08:00
|
|
|
#include "SyntheticSections.h"
|
2018-08-10 01:59:56 +08:00
|
|
|
#include "Target.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
using namespace llvm::support::endian;
|
|
|
|
using namespace llvm::ELF;
|
2020-05-15 13:18:58 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
2018-08-10 01:59:56 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class RISCV final : public TargetInfo {
|
|
|
|
public:
|
2018-09-26 16:11:34 +08:00
|
|
|
RISCV();
|
2018-11-02 04:08:39 +08:00
|
|
|
uint32_t calcEFlags() const override;
|
2019-07-02 01:12:18 +08:00
|
|
|
void writeGotHeader(uint8_t *buf) const override;
|
|
|
|
void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
|
|
|
|
void writePltHeader(uint8_t *buf) const override;
|
2019-12-18 05:43:04 +08:00
|
|
|
void writePlt(uint8_t *buf, const Symbol &sym,
|
|
|
|
uint64_t pltEntryAddr) const override;
|
2019-07-02 01:12:18 +08:00
|
|
|
RelType getDynRel(RelType type) const override;
|
2018-08-10 01:59:56 +08:00
|
|
|
RelExpr getRelExpr(RelType type, const Symbol &s,
|
|
|
|
const uint8_t *loc) const override;
|
2020-01-23 13:39:16 +08:00
|
|
|
void relocate(uint8_t *loc, const Relocation &rel,
|
|
|
|
uint64_t val) const override;
|
2018-08-10 01:59:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2019-07-02 01:12:26 +08:00
|
|
|
const uint64_t dtpOffset = 0x800;
|
|
|
|
|
2019-07-02 01:12:18 +08:00
|
|
|
enum Op {
|
|
|
|
ADDI = 0x13,
|
|
|
|
AUIPC = 0x17,
|
|
|
|
JALR = 0x67,
|
|
|
|
LD = 0x3003,
|
|
|
|
LW = 0x2003,
|
|
|
|
SRLI = 0x5013,
|
|
|
|
SUB = 0x40000033,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Reg {
|
|
|
|
X_RA = 1,
|
|
|
|
X_T0 = 5,
|
|
|
|
X_T1 = 6,
|
|
|
|
X_T2 = 7,
|
|
|
|
X_T3 = 28,
|
|
|
|
};
|
|
|
|
|
|
|
|
static uint32_t hi20(uint32_t val) { return (val + 0x800) >> 12; }
|
|
|
|
static uint32_t lo12(uint32_t val) { return val & 4095; }
|
|
|
|
|
|
|
|
static uint32_t itype(uint32_t op, uint32_t rd, uint32_t rs1, uint32_t imm) {
|
|
|
|
return op | (rd << 7) | (rs1 << 15) | (imm << 20);
|
|
|
|
}
|
|
|
|
static uint32_t rtype(uint32_t op, uint32_t rd, uint32_t rs1, uint32_t rs2) {
|
|
|
|
return op | (rd << 7) | (rs1 << 15) | (rs2 << 20);
|
|
|
|
}
|
|
|
|
static uint32_t utype(uint32_t op, uint32_t rd, uint32_t imm) {
|
|
|
|
return op | (rd << 7) | (imm << 12);
|
|
|
|
}
|
|
|
|
|
|
|
|
RISCV::RISCV() {
|
|
|
|
copyRel = R_RISCV_COPY;
|
|
|
|
noneRel = R_RISCV_NONE;
|
|
|
|
pltRel = R_RISCV_JUMP_SLOT;
|
|
|
|
relativeRel = R_RISCV_RELATIVE;
|
2020-02-05 12:33:41 +08:00
|
|
|
iRelativeRel = R_RISCV_IRELATIVE;
|
2019-07-02 01:12:26 +08:00
|
|
|
if (config->is64) {
|
|
|
|
symbolicRel = R_RISCV_64;
|
|
|
|
tlsModuleIndexRel = R_RISCV_TLS_DTPMOD64;
|
|
|
|
tlsOffsetRel = R_RISCV_TLS_DTPREL64;
|
|
|
|
tlsGotRel = R_RISCV_TLS_TPREL64;
|
|
|
|
} else {
|
|
|
|
symbolicRel = R_RISCV_32;
|
|
|
|
tlsModuleIndexRel = R_RISCV_TLS_DTPMOD32;
|
|
|
|
tlsOffsetRel = R_RISCV_TLS_DTPREL32;
|
|
|
|
tlsGotRel = R_RISCV_TLS_TPREL32;
|
|
|
|
}
|
2019-07-02 01:12:18 +08:00
|
|
|
gotRel = symbolicRel;
|
|
|
|
|
|
|
|
// .got[0] = _DYNAMIC
|
|
|
|
gotBaseSymInGotPlt = false;
|
|
|
|
gotHeaderEntriesNum = 1;
|
|
|
|
|
|
|
|
// .got.plt[0] = _dl_runtime_resolve, .got.plt[1] = link_map
|
|
|
|
gotPltHeaderEntriesNum = 2;
|
|
|
|
|
|
|
|
pltHeaderSize = 32;
|
2019-12-15 06:17:35 +08:00
|
|
|
pltEntrySize = 16;
|
|
|
|
ipltEntrySize = 16;
|
2019-07-02 01:12:18 +08:00
|
|
|
}
|
2018-09-26 16:11:34 +08:00
|
|
|
|
2018-08-10 01:59:56 +08:00
|
|
|
static uint32_t getEFlags(InputFile *f) {
|
|
|
|
if (config->is64)
|
2020-09-09 22:03:53 +08:00
|
|
|
return cast<ObjFile<ELF64LE>>(f)->getObj().getHeader().e_flags;
|
|
|
|
return cast<ObjFile<ELF32LE>>(f)->getObj().getHeader().e_flags;
|
2018-08-10 01:59:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t RISCV::calcEFlags() const {
|
[lld][RISCV] Use an e_flags of 0 if there are only binary input files.
Summary:
If none of the input files are ELF object files (for example, when
generating an object file from a single binary input file via
"-b binary"), use a fallback value for the ELF header flags instead
of crashing with an assertion failure.
Reviewers: MaskRay, ruiu, espindola
Reviewed By: MaskRay, ruiu
Subscribers: kevans, grimar, emaste, arichardson, asb, rbar, johnrusso, simoncook, sabuasal, niosHD, kito-cheng, shiva0217, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, rkruppe, PkmX, jocewei, psnobl, benna, Jim, lenary, s.egerton, pzheng, sameer.abuasal, apazos, luismarques, llvm-commits, jrtc27
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D71101
2019-12-22 01:59:37 +08:00
|
|
|
// If there are only binary input files (from -b binary), use a
|
|
|
|
// value of 0 for the ELF header flags.
|
|
|
|
if (objectFiles.empty())
|
|
|
|
return 0;
|
2018-08-10 01:59:56 +08:00
|
|
|
|
|
|
|
uint32_t target = getEFlags(objectFiles.front());
|
|
|
|
|
|
|
|
for (InputFile *f : objectFiles) {
|
|
|
|
uint32_t eflags = getEFlags(f);
|
|
|
|
if (eflags & EF_RISCV_RVC)
|
|
|
|
target |= EF_RISCV_RVC;
|
|
|
|
|
|
|
|
if ((eflags & EF_RISCV_FLOAT_ABI) != (target & EF_RISCV_FLOAT_ABI))
|
|
|
|
error(toString(f) +
|
|
|
|
": cannot link object files with different floating-point ABI");
|
|
|
|
|
|
|
|
if ((eflags & EF_RISCV_RVE) != (target & EF_RISCV_RVE))
|
|
|
|
error(toString(f) +
|
|
|
|
": cannot link object files with different EF_RISCV_RVE");
|
|
|
|
}
|
|
|
|
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2019-07-02 01:12:18 +08:00
|
|
|
void RISCV::writeGotHeader(uint8_t *buf) const {
|
|
|
|
if (config->is64)
|
|
|
|
write64le(buf, mainPart->dynamic->getVA());
|
|
|
|
else
|
|
|
|
write32le(buf, mainPart->dynamic->getVA());
|
|
|
|
}
|
|
|
|
|
|
|
|
void RISCV::writeGotPlt(uint8_t *buf, const Symbol &s) const {
|
|
|
|
if (config->is64)
|
|
|
|
write64le(buf, in.plt->getVA());
|
|
|
|
else
|
|
|
|
write32le(buf, in.plt->getVA());
|
|
|
|
}
|
|
|
|
|
|
|
|
void RISCV::writePltHeader(uint8_t *buf) const {
|
|
|
|
// 1: auipc t2, %pcrel_hi(.got.plt)
|
|
|
|
// sub t1, t1, t3
|
|
|
|
// l[wd] t3, %pcrel_lo(1b)(t2); t3 = _dl_runtime_resolve
|
2019-07-16 13:50:45 +08:00
|
|
|
// addi t1, t1, -pltHeaderSize-12; t1 = &.plt[i] - &.plt[0]
|
2019-07-02 01:12:18 +08:00
|
|
|
// addi t0, t2, %pcrel_lo(1b)
|
|
|
|
// srli t1, t1, (rv64?1:2); t1 = &.got.plt[i] - &.got.plt[0]
|
|
|
|
// l[wd] t0, Wordsize(t0); t0 = link_map
|
|
|
|
// jr t3
|
|
|
|
uint32_t offset = in.gotPlt->getVA() - in.plt->getVA();
|
|
|
|
uint32_t load = config->is64 ? LD : LW;
|
|
|
|
write32le(buf + 0, utype(AUIPC, X_T2, hi20(offset)));
|
|
|
|
write32le(buf + 4, rtype(SUB, X_T1, X_T1, X_T3));
|
|
|
|
write32le(buf + 8, itype(load, X_T3, X_T2, lo12(offset)));
|
|
|
|
write32le(buf + 12, itype(ADDI, X_T1, X_T1, -target->pltHeaderSize - 12));
|
|
|
|
write32le(buf + 16, itype(ADDI, X_T0, X_T2, lo12(offset)));
|
|
|
|
write32le(buf + 20, itype(SRLI, X_T1, X_T1, config->is64 ? 1 : 2));
|
|
|
|
write32le(buf + 24, itype(load, X_T0, X_T0, config->wordsize));
|
|
|
|
write32le(buf + 28, itype(JALR, 0, X_T3, 0));
|
|
|
|
}
|
|
|
|
|
2019-12-18 05:43:04 +08:00
|
|
|
void RISCV::writePlt(uint8_t *buf, const Symbol &sym,
|
|
|
|
uint64_t pltEntryAddr) const {
|
2019-07-02 01:12:18 +08:00
|
|
|
// 1: auipc t3, %pcrel_hi(f@.got.plt)
|
|
|
|
// l[wd] t3, %pcrel_lo(1b)(t3)
|
|
|
|
// jalr t1, t3
|
|
|
|
// nop
|
2019-12-18 05:43:04 +08:00
|
|
|
uint32_t offset = sym.getGotPltVA() - pltEntryAddr;
|
2019-07-02 01:12:18 +08:00
|
|
|
write32le(buf + 0, utype(AUIPC, X_T3, hi20(offset)));
|
|
|
|
write32le(buf + 4, itype(config->is64 ? LD : LW, X_T3, X_T3, lo12(offset)));
|
|
|
|
write32le(buf + 8, itype(JALR, X_T1, X_T3, 0));
|
|
|
|
write32le(buf + 12, itype(ADDI, 0, 0, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
RelType RISCV::getDynRel(RelType type) const {
|
2019-07-09 11:56:44 +08:00
|
|
|
return type == target->symbolicRel ? type
|
|
|
|
: static_cast<RelType>(R_RISCV_NONE);
|
2019-07-02 01:12:18 +08:00
|
|
|
}
|
|
|
|
|
2018-08-10 01:59:56 +08:00
|
|
|
RelExpr RISCV::getRelExpr(const RelType type, const Symbol &s,
|
|
|
|
const uint8_t *loc) const {
|
|
|
|
switch (type) {
|
2020-01-01 07:06:31 +08:00
|
|
|
case R_RISCV_NONE:
|
|
|
|
return R_NONE;
|
|
|
|
case R_RISCV_32:
|
|
|
|
case R_RISCV_64:
|
|
|
|
case R_RISCV_HI20:
|
|
|
|
case R_RISCV_LO12_I:
|
|
|
|
case R_RISCV_LO12_S:
|
|
|
|
case R_RISCV_RVC_LUI:
|
|
|
|
return R_ABS;
|
[ELF][RISCV] Treat R_RISCV_{ADD,SET,SUB}* as link-time constants
R_RISCV_{ADD,SET,SUB}* are used for local label computation.
Add a new RelExpr member R_RISCV_ADD to represent them.
R_RISCV_ADD is treated as a link-time constant because otherwise
R_RISCV_{ADD,SET,SUB}* are not allowed in -pie/-shared mode.
In glibc Scrt1.o, .rela.eh_frame contains such relocations.
Because .eh_frame is not writable, we get this error:
ld.lld: error: can't create dynamic relocation R_RISCV_ADD32 against symbol: .L0 in readonly segment; recompil object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
>>> defined in ..../riscv64-linux-gnu/lib/Scrt1.o
With D63076 and this patch, I can run -pie/-shared programs linked against glibc.
Note llvm-mc cannot currently produce R_RISCV_SET* so they are not tested.
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D63183
llvm-svn: 363128
2019-06-12 15:53:06 +08:00
|
|
|
case R_RISCV_ADD8:
|
|
|
|
case R_RISCV_ADD16:
|
|
|
|
case R_RISCV_ADD32:
|
|
|
|
case R_RISCV_ADD64:
|
|
|
|
case R_RISCV_SET6:
|
|
|
|
case R_RISCV_SET8:
|
|
|
|
case R_RISCV_SET16:
|
|
|
|
case R_RISCV_SET32:
|
|
|
|
case R_RISCV_SUB6:
|
|
|
|
case R_RISCV_SUB8:
|
|
|
|
case R_RISCV_SUB16:
|
|
|
|
case R_RISCV_SUB32:
|
|
|
|
case R_RISCV_SUB64:
|
|
|
|
return R_RISCV_ADD;
|
2018-08-10 01:59:56 +08:00
|
|
|
case R_RISCV_JAL:
|
|
|
|
case R_RISCV_BRANCH:
|
|
|
|
case R_RISCV_PCREL_HI20:
|
|
|
|
case R_RISCV_RVC_BRANCH:
|
|
|
|
case R_RISCV_RVC_JUMP:
|
|
|
|
case R_RISCV_32_PCREL:
|
|
|
|
return R_PC;
|
2019-07-02 01:12:18 +08:00
|
|
|
case R_RISCV_CALL:
|
|
|
|
case R_RISCV_CALL_PLT:
|
|
|
|
return R_PLT_PC;
|
|
|
|
case R_RISCV_GOT_HI20:
|
|
|
|
return R_GOT_PC;
|
2018-08-10 01:59:56 +08:00
|
|
|
case R_RISCV_PCREL_LO12_I:
|
|
|
|
case R_RISCV_PCREL_LO12_S:
|
|
|
|
return R_RISCV_PC_INDIRECT;
|
2019-07-02 01:12:26 +08:00
|
|
|
case R_RISCV_TLS_GD_HI20:
|
|
|
|
return R_TLSGD_PC;
|
|
|
|
case R_RISCV_TLS_GOT_HI20:
|
|
|
|
config->hasStaticTlsModel = true;
|
|
|
|
return R_GOT_PC;
|
|
|
|
case R_RISCV_TPREL_HI20:
|
|
|
|
case R_RISCV_TPREL_LO12_I:
|
|
|
|
case R_RISCV_TPREL_LO12_S:
|
2020-12-19 00:24:42 +08:00
|
|
|
return R_TPREL;
|
2018-08-10 01:59:56 +08:00
|
|
|
case R_RISCV_RELAX:
|
2019-07-02 01:12:26 +08:00
|
|
|
case R_RISCV_TPREL_ADD:
|
2020-01-01 07:06:31 +08:00
|
|
|
return R_NONE;
|
[lld][RISCV] Print error when encountering R_RISCV_ALIGN
Summary:
Unlike R_RISCV_RELAX, which is a linker hint, R_RISCV_ALIGN requires the
support of the linker even when ignoring all R_RISCV_RELAX relocations.
This is because the compiler emits as many NOPs as may be required for
the requested alignment, more than may be required pre-relaxation, to
allow for the target becoming more unaligned after relaxing earlier
sequences. This means that the target is often not initially aligned in
the object files, and so the R_RISCV_ALIGN relocations cannot just be
ignored. Since we do not support linker relaxation, we must turn these
into errors.
Reviewers: ruiu, MaskRay, espindola
Reviewed By: MaskRay
Subscribers: grimar, Jim, emaste, arichardson, asb, rbar, johnrusso, simoncook, sabuasal, niosHD, kito-cheng, shiva0217, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, rkruppe, PkmX, jocewei, psnobl, benna, lenary, s.egerton, pzheng, sameer.abuasal, apazos, luismarques, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D71820
2020-01-21 10:49:42 +08:00
|
|
|
case R_RISCV_ALIGN:
|
|
|
|
// Not just a hint; always padded to the worst-case number of NOPs, so may
|
|
|
|
// not currently be aligned, and without linker relaxation support we can't
|
|
|
|
// delete NOPs to realign.
|
|
|
|
errorOrWarn(getErrorLocation(loc) + "relocation R_RISCV_ALIGN requires "
|
|
|
|
"unimplemented linker relaxation; recompile with -mno-relax");
|
|
|
|
return R_NONE;
|
2018-08-10 01:59:56 +08:00
|
|
|
default:
|
2020-01-01 07:06:31 +08:00
|
|
|
error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
|
|
|
|
") against symbol " + toString(s));
|
|
|
|
return R_NONE;
|
2018-08-10 01:59:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract bits V[Begin:End], where range is inclusive, and Begin must be < 63.
|
|
|
|
static uint32_t extractBits(uint64_t v, uint32_t begin, uint32_t end) {
|
|
|
|
return (v & ((1ULL << (begin + 1)) - 1)) >> end;
|
|
|
|
}
|
|
|
|
|
2020-01-23 13:39:16 +08:00
|
|
|
void RISCV::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
|
2019-04-09 19:39:23 +08:00
|
|
|
const unsigned bits = config->wordsize * 8;
|
|
|
|
|
2020-01-23 13:39:16 +08:00
|
|
|
switch (rel.type) {
|
2018-08-10 01:59:56 +08:00
|
|
|
case R_RISCV_32:
|
|
|
|
write32le(loc, val);
|
|
|
|
return;
|
|
|
|
case R_RISCV_64:
|
|
|
|
write64le(loc, val);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case R_RISCV_RVC_BRANCH: {
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, static_cast<int64_t>(val) >> 1, 8, rel);
|
|
|
|
checkAlignment(loc, val, 2, rel);
|
2018-08-10 01:59:56 +08:00
|
|
|
uint16_t insn = read16le(loc) & 0xE383;
|
|
|
|
uint16_t imm8 = extractBits(val, 8, 8) << 12;
|
|
|
|
uint16_t imm4_3 = extractBits(val, 4, 3) << 10;
|
|
|
|
uint16_t imm7_6 = extractBits(val, 7, 6) << 5;
|
|
|
|
uint16_t imm2_1 = extractBits(val, 2, 1) << 3;
|
|
|
|
uint16_t imm5 = extractBits(val, 5, 5) << 2;
|
|
|
|
insn |= imm8 | imm4_3 | imm7_6 | imm2_1 | imm5;
|
[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
|
|
|
|
2018-08-10 01:59:56 +08:00
|
|
|
write16le(loc, insn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case R_RISCV_RVC_JUMP: {
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, static_cast<int64_t>(val) >> 1, 11, rel);
|
|
|
|
checkAlignment(loc, val, 2, rel);
|
2018-08-10 01:59:56 +08:00
|
|
|
uint16_t insn = read16le(loc) & 0xE003;
|
|
|
|
uint16_t imm11 = extractBits(val, 11, 11) << 12;
|
|
|
|
uint16_t imm4 = extractBits(val, 4, 4) << 11;
|
|
|
|
uint16_t imm9_8 = extractBits(val, 9, 8) << 9;
|
|
|
|
uint16_t imm10 = extractBits(val, 10, 10) << 8;
|
|
|
|
uint16_t imm6 = extractBits(val, 6, 6) << 7;
|
|
|
|
uint16_t imm7 = extractBits(val, 7, 7) << 6;
|
|
|
|
uint16_t imm3_1 = extractBits(val, 3, 1) << 3;
|
|
|
|
uint16_t imm5 = extractBits(val, 5, 5) << 2;
|
|
|
|
insn |= imm11 | imm4 | imm9_8 | imm10 | imm6 | imm7 | imm3_1 | imm5;
|
[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
|
|
|
|
2018-08-10 01:59:56 +08:00
|
|
|
write16le(loc, insn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case R_RISCV_RVC_LUI: {
|
2019-04-09 19:39:23 +08:00
|
|
|
int64_t imm = SignExtend64(val + 0x800, bits) >> 12;
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, imm, 6, rel);
|
2018-08-10 01:59:56 +08:00
|
|
|
if (imm == 0) { // `c.lui rd, 0` is illegal, convert to `c.li rd, 0`
|
|
|
|
write16le(loc, (read16le(loc) & 0x0F83) | 0x4000);
|
|
|
|
} else {
|
|
|
|
uint16_t imm17 = extractBits(val + 0x800, 17, 17) << 12;
|
|
|
|
uint16_t imm16_12 = extractBits(val + 0x800, 16, 12) << 2;
|
|
|
|
write16le(loc, (read16le(loc) & 0xEF83) | imm17 | imm16_12);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case R_RISCV_JAL: {
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, static_cast<int64_t>(val) >> 1, 20, rel);
|
|
|
|
checkAlignment(loc, val, 2, rel);
|
2018-08-10 01:59:56 +08:00
|
|
|
|
|
|
|
uint32_t insn = read32le(loc) & 0xFFF;
|
|
|
|
uint32_t imm20 = extractBits(val, 20, 20) << 31;
|
|
|
|
uint32_t imm10_1 = extractBits(val, 10, 1) << 21;
|
|
|
|
uint32_t imm11 = extractBits(val, 11, 11) << 20;
|
|
|
|
uint32_t imm19_12 = extractBits(val, 19, 12) << 12;
|
|
|
|
insn |= imm20 | imm10_1 | imm11 | imm19_12;
|
|
|
|
|
|
|
|
write32le(loc, insn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case R_RISCV_BRANCH: {
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, static_cast<int64_t>(val) >> 1, 12, rel);
|
|
|
|
checkAlignment(loc, val, 2, rel);
|
2018-08-10 01:59:56 +08:00
|
|
|
|
|
|
|
uint32_t insn = read32le(loc) & 0x1FFF07F;
|
|
|
|
uint32_t imm12 = extractBits(val, 12, 12) << 31;
|
|
|
|
uint32_t imm10_5 = extractBits(val, 10, 5) << 25;
|
|
|
|
uint32_t imm4_1 = extractBits(val, 4, 1) << 8;
|
|
|
|
uint32_t imm11 = extractBits(val, 11, 11) << 7;
|
|
|
|
insn |= imm12 | imm10_5 | imm4_1 | imm11;
|
|
|
|
|
|
|
|
write32le(loc, insn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// auipc + jalr pair
|
2019-07-02 01:12:18 +08:00
|
|
|
case R_RISCV_CALL:
|
|
|
|
case R_RISCV_CALL_PLT: {
|
2019-04-09 19:39:23 +08:00
|
|
|
int64_t hi = SignExtend64(val + 0x800, bits) >> 12;
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, hi, 20, rel);
|
2019-04-09 19:39:23 +08:00
|
|
|
if (isInt<20>(hi)) {
|
2020-01-23 13:39:16 +08:00
|
|
|
relocateNoSym(loc, R_RISCV_PCREL_HI20, val);
|
|
|
|
relocateNoSym(loc + 4, R_RISCV_PCREL_LO12_I, val);
|
2018-08-10 01:59:56 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-02 01:12:18 +08:00
|
|
|
case R_RISCV_GOT_HI20:
|
2018-08-10 01:59:56 +08:00
|
|
|
case R_RISCV_PCREL_HI20:
|
2019-07-02 01:12:26 +08:00
|
|
|
case R_RISCV_TLS_GD_HI20:
|
|
|
|
case R_RISCV_TLS_GOT_HI20:
|
|
|
|
case R_RISCV_TPREL_HI20:
|
2018-08-10 01:59:56 +08:00
|
|
|
case R_RISCV_HI20: {
|
2019-04-09 19:39:23 +08:00
|
|
|
uint64_t hi = val + 0x800;
|
2020-01-23 13:39:16 +08:00
|
|
|
checkInt(loc, SignExtend64(hi, bits) >> 12, 20, rel);
|
2018-08-10 01:59:56 +08:00
|
|
|
write32le(loc, (read32le(loc) & 0xFFF) | (hi & 0xFFFFF000));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case R_RISCV_PCREL_LO12_I:
|
2019-07-02 01:12:26 +08:00
|
|
|
case R_RISCV_TPREL_LO12_I:
|
2018-08-10 01:59:56 +08:00
|
|
|
case R_RISCV_LO12_I: {
|
2019-04-09 19:39:23 +08:00
|
|
|
uint64_t hi = (val + 0x800) >> 12;
|
|
|
|
uint64_t lo = val - (hi << 12);
|
2018-08-10 01:59:56 +08:00
|
|
|
write32le(loc, (read32le(loc) & 0xFFFFF) | ((lo & 0xFFF) << 20));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case R_RISCV_PCREL_LO12_S:
|
2019-07-02 01:12:26 +08:00
|
|
|
case R_RISCV_TPREL_LO12_S:
|
2018-08-10 01:59:56 +08:00
|
|
|
case R_RISCV_LO12_S: {
|
2019-04-09 19:39:23 +08:00
|
|
|
uint64_t hi = (val + 0x800) >> 12;
|
|
|
|
uint64_t lo = val - (hi << 12);
|
2018-08-10 01:59:56 +08:00
|
|
|
uint32_t imm11_5 = extractBits(lo, 11, 5) << 25;
|
|
|
|
uint32_t imm4_0 = extractBits(lo, 4, 0) << 7;
|
|
|
|
write32le(loc, (read32le(loc) & 0x1FFF07F) | imm11_5 | imm4_0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case R_RISCV_ADD8:
|
|
|
|
*loc += val;
|
|
|
|
return;
|
|
|
|
case R_RISCV_ADD16:
|
|
|
|
write16le(loc, read16le(loc) + val);
|
|
|
|
return;
|
|
|
|
case R_RISCV_ADD32:
|
|
|
|
write32le(loc, read32le(loc) + val);
|
|
|
|
return;
|
|
|
|
case R_RISCV_ADD64:
|
|
|
|
write64le(loc, read64le(loc) + val);
|
|
|
|
return;
|
|
|
|
case R_RISCV_SUB6:
|
|
|
|
*loc = (*loc & 0xc0) | (((*loc & 0x3f) - val) & 0x3f);
|
|
|
|
return;
|
|
|
|
case R_RISCV_SUB8:
|
|
|
|
*loc -= val;
|
|
|
|
return;
|
|
|
|
case R_RISCV_SUB16:
|
|
|
|
write16le(loc, read16le(loc) - val);
|
|
|
|
return;
|
|
|
|
case R_RISCV_SUB32:
|
|
|
|
write32le(loc, read32le(loc) - val);
|
|
|
|
return;
|
|
|
|
case R_RISCV_SUB64:
|
|
|
|
write64le(loc, read64le(loc) - val);
|
|
|
|
return;
|
|
|
|
case R_RISCV_SET6:
|
|
|
|
*loc = (*loc & 0xc0) | (val & 0x3f);
|
|
|
|
return;
|
|
|
|
case R_RISCV_SET8:
|
|
|
|
*loc = val;
|
|
|
|
return;
|
|
|
|
case R_RISCV_SET16:
|
|
|
|
write16le(loc, val);
|
|
|
|
return;
|
|
|
|
case R_RISCV_SET32:
|
|
|
|
case R_RISCV_32_PCREL:
|
|
|
|
write32le(loc, val);
|
|
|
|
return;
|
|
|
|
|
2019-07-02 01:12:26 +08:00
|
|
|
case R_RISCV_TLS_DTPREL32:
|
|
|
|
write32le(loc, val - dtpOffset);
|
|
|
|
break;
|
|
|
|
case R_RISCV_TLS_DTPREL64:
|
|
|
|
write64le(loc, val - dtpOffset);
|
|
|
|
break;
|
|
|
|
|
2018-08-10 01:59:56 +08:00
|
|
|
case R_RISCV_RELAX:
|
|
|
|
return; // Ignored (for now)
|
2020-01-01 07:06:31 +08:00
|
|
|
|
2018-08-10 01:59:56 +08:00
|
|
|
default:
|
2020-01-01 07:06:31 +08:00
|
|
|
llvm_unreachable("unknown relocation");
|
2018-08-10 01:59:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
TargetInfo *elf::getRISCVTargetInfo() {
|
2018-08-10 01:59:56 +08:00
|
|
|
static RISCV target;
|
|
|
|
return ⌖
|
|
|
|
}
|