1 //===- RISCV.cpp ----------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "InputFiles.h"
14 using namespace llvm::object;
15 using namespace llvm::support::endian;
16 using namespace llvm::ELF;
18 using namespace lld::elf;
22 class RISCV final : public TargetInfo {
25 uint32_t calcEFlags() const override;
26 RelExpr getRelExpr(RelType Type, const Symbol &S,
27 const uint8_t *Loc) const override;
28 void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override;
31 } // end anonymous namespace
33 RISCV::RISCV() { NoneRel = R_RISCV_NONE; }
35 static uint32_t getEFlags(InputFile *F) {
37 return cast<ObjFile<ELF64LE>>(F)->getObj().getHeader()->e_flags;
38 return cast<ObjFile<ELF32LE>>(F)->getObj().getHeader()->e_flags;
41 uint32_t RISCV::calcEFlags() const {
42 assert(!ObjectFiles.empty());
44 uint32_t Target = getEFlags(ObjectFiles.front());
46 for (InputFile *F : ObjectFiles) {
47 uint32_t EFlags = getEFlags(F);
48 if (EFlags & EF_RISCV_RVC)
49 Target |= EF_RISCV_RVC;
51 if ((EFlags & EF_RISCV_FLOAT_ABI) != (Target & EF_RISCV_FLOAT_ABI))
53 ": cannot link object files with different floating-point ABI");
55 if ((EFlags & EF_RISCV_RVE) != (Target & EF_RISCV_RVE))
57 ": cannot link object files with different EF_RISCV_RVE");
63 RelExpr RISCV::getRelExpr(const RelType Type, const Symbol &S,
64 const uint8_t *Loc) const {
69 case R_RISCV_PCREL_HI20:
70 case R_RISCV_RVC_BRANCH:
71 case R_RISCV_RVC_JUMP:
72 case R_RISCV_32_PCREL:
74 case R_RISCV_PCREL_LO12_I:
75 case R_RISCV_PCREL_LO12_S:
76 return R_RISCV_PC_INDIRECT;
85 // Extract bits V[Begin:End], where range is inclusive, and Begin must be < 63.
86 static uint32_t extractBits(uint64_t V, uint32_t Begin, uint32_t End) {
87 return (V & ((1ULL << (Begin + 1)) - 1)) >> End;
90 void RISCV::relocateOne(uint8_t *Loc, const RelType Type,
91 const uint64_t Val) const {
100 case R_RISCV_RVC_BRANCH: {
101 checkInt(Loc, static_cast<int64_t>(Val) >> 1, 8, Type);
102 checkAlignment(Loc, Val, 2, Type);
103 uint16_t Insn = read16le(Loc) & 0xE383;
104 uint16_t Imm8 = extractBits(Val, 8, 8) << 12;
105 uint16_t Imm4_3 = extractBits(Val, 4, 3) << 10;
106 uint16_t Imm7_6 = extractBits(Val, 7, 6) << 5;
107 uint16_t Imm2_1 = extractBits(Val, 2, 1) << 3;
108 uint16_t Imm5 = extractBits(Val, 5, 5) << 2;
109 Insn |= Imm8 | Imm4_3 | Imm7_6 | Imm2_1 | Imm5;
111 write16le(Loc, Insn);
115 case R_RISCV_RVC_JUMP: {
116 checkInt(Loc, static_cast<int64_t>(Val) >> 1, 11, Type);
117 checkAlignment(Loc, Val, 2, Type);
118 uint16_t Insn = read16le(Loc) & 0xE003;
119 uint16_t Imm11 = extractBits(Val, 11, 11) << 12;
120 uint16_t Imm4 = extractBits(Val, 4, 4) << 11;
121 uint16_t Imm9_8 = extractBits(Val, 9, 8) << 9;
122 uint16_t Imm10 = extractBits(Val, 10, 10) << 8;
123 uint16_t Imm6 = extractBits(Val, 6, 6) << 7;
124 uint16_t Imm7 = extractBits(Val, 7, 7) << 6;
125 uint16_t Imm3_1 = extractBits(Val, 3, 1) << 3;
126 uint16_t Imm5 = extractBits(Val, 5, 5) << 2;
127 Insn |= Imm11 | Imm4 | Imm9_8 | Imm10 | Imm6 | Imm7 | Imm3_1 | Imm5;
129 write16le(Loc, Insn);
133 case R_RISCV_RVC_LUI: {
134 int32_t Imm = ((Val + 0x800) >> 12);
135 checkUInt(Loc, Imm, 6, Type);
136 if (Imm == 0) { // `c.lui rd, 0` is illegal, convert to `c.li rd, 0`
137 write16le(Loc, (read16le(Loc) & 0x0F83) | 0x4000);
139 uint16_t Imm17 = extractBits(Val + 0x800, 17, 17) << 12;
140 uint16_t Imm16_12 = extractBits(Val + 0x800, 16, 12) << 2;
141 write16le(Loc, (read16le(Loc) & 0xEF83) | Imm17 | Imm16_12);
147 checkInt(Loc, static_cast<int64_t>(Val) >> 1, 20, Type);
148 checkAlignment(Loc, Val, 2, Type);
150 uint32_t Insn = read32le(Loc) & 0xFFF;
151 uint32_t Imm20 = extractBits(Val, 20, 20) << 31;
152 uint32_t Imm10_1 = extractBits(Val, 10, 1) << 21;
153 uint32_t Imm11 = extractBits(Val, 11, 11) << 20;
154 uint32_t Imm19_12 = extractBits(Val, 19, 12) << 12;
155 Insn |= Imm20 | Imm10_1 | Imm11 | Imm19_12;
157 write32le(Loc, Insn);
161 case R_RISCV_BRANCH: {
162 checkInt(Loc, static_cast<int64_t>(Val) >> 1, 12, Type);
163 checkAlignment(Loc, Val, 2, Type);
165 uint32_t Insn = read32le(Loc) & 0x1FFF07F;
166 uint32_t Imm12 = extractBits(Val, 12, 12) << 31;
167 uint32_t Imm10_5 = extractBits(Val, 10, 5) << 25;
168 uint32_t Imm4_1 = extractBits(Val, 4, 1) << 8;
169 uint32_t Imm11 = extractBits(Val, 11, 11) << 7;
170 Insn |= Imm12 | Imm10_5 | Imm4_1 | Imm11;
172 write32le(Loc, Insn);
178 checkInt(Loc, Val, 32, Type);
179 if (isInt<32>(Val)) {
180 relocateOne(Loc, R_RISCV_PCREL_HI20, Val);
181 relocateOne(Loc + 4, R_RISCV_PCREL_LO12_I, Val);
186 case R_RISCV_PCREL_HI20:
188 checkInt(Loc, Val, 32, Type);
189 uint32_t Hi = Val + 0x800;
190 write32le(Loc, (read32le(Loc) & 0xFFF) | (Hi & 0xFFFFF000));
194 case R_RISCV_PCREL_LO12_I:
195 case R_RISCV_LO12_I: {
196 checkInt(Loc, Val, 32, Type);
197 uint32_t Hi = Val + 0x800;
198 uint32_t Lo = Val - (Hi & 0xFFFFF000);
199 write32le(Loc, (read32le(Loc) & 0xFFFFF) | ((Lo & 0xFFF) << 20));
203 case R_RISCV_PCREL_LO12_S:
204 case R_RISCV_LO12_S: {
205 checkInt(Loc, Val, 32, Type);
206 uint32_t Hi = Val + 0x800;
207 uint32_t Lo = Val - (Hi & 0xFFFFF000);
208 uint32_t Imm11_5 = extractBits(Lo, 11, 5) << 25;
209 uint32_t Imm4_0 = extractBits(Lo, 4, 0) << 7;
210 write32le(Loc, (read32le(Loc) & 0x1FFF07F) | Imm11_5 | Imm4_0);
218 write16le(Loc, read16le(Loc) + Val);
221 write32le(Loc, read32le(Loc) + Val);
224 write64le(Loc, read64le(Loc) + Val);
227 *Loc = (*Loc & 0xc0) | (((*Loc & 0x3f) - Val) & 0x3f);
233 write16le(Loc, read16le(Loc) - Val);
236 write32le(Loc, read32le(Loc) - Val);
239 write64le(Loc, read64le(Loc) - Val);
242 *Loc = (*Loc & 0xc0) | (Val & 0x3f);
251 case R_RISCV_32_PCREL:
257 return; // Ignored (for now)
259 return; // Do nothing
261 // These are handled by the dynamic linker
262 case R_RISCV_RELATIVE:
264 case R_RISCV_JUMP_SLOT:
265 // GP-relative relocations are only produced after relaxation, which
266 // we don't support for now
267 case R_RISCV_GPREL_I:
268 case R_RISCV_GPREL_S:
270 error(getErrorLocation(Loc) +
271 "unimplemented relocation: " + toString(Type));
276 TargetInfo *elf::getRISCVTargetInfo() {