1 //===- RISCV.cpp ----------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "InputFiles.h" 10 #include "SyntheticSections.h" 11 #include "Target.h" 12 13 using namespace llvm; 14 using namespace llvm::object; 15 using namespace llvm::support::endian; 16 using namespace llvm::ELF; 17 using namespace lld; 18 using namespace lld::elf; 19 20 namespace { 21 22 class RISCV final : public TargetInfo { 23 public: 24 RISCV(); 25 uint32_t calcEFlags() const override; 26 void writeGotHeader(uint8_t *buf) const override; 27 void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 28 void writePltHeader(uint8_t *buf) const override; 29 void writePlt(uint8_t *buf, uint64_t gotPltEntryAddr, uint64_t pltEntryAddr, 30 int32_t index, unsigned relOff) const override; 31 RelType getDynRel(RelType type) const override; 32 RelExpr getRelExpr(RelType type, const Symbol &s, 33 const uint8_t *loc) const override; 34 void relocateOne(uint8_t *loc, RelType type, uint64_t val) const override; 35 }; 36 37 } // end anonymous namespace 38 39 const uint64_t dtpOffset = 0x800; 40 41 enum Op { 42 ADDI = 0x13, 43 AUIPC = 0x17, 44 JALR = 0x67, 45 LD = 0x3003, 46 LW = 0x2003, 47 SRLI = 0x5013, 48 SUB = 0x40000033, 49 }; 50 51 enum Reg { 52 X_RA = 1, 53 X_T0 = 5, 54 X_T1 = 6, 55 X_T2 = 7, 56 X_T3 = 28, 57 }; 58 59 static uint32_t hi20(uint32_t val) { return (val + 0x800) >> 12; } 60 static uint32_t lo12(uint32_t val) { return val & 4095; } 61 62 static uint32_t itype(uint32_t op, uint32_t rd, uint32_t rs1, uint32_t imm) { 63 return op | (rd << 7) | (rs1 << 15) | (imm << 20); 64 } 65 static uint32_t rtype(uint32_t op, uint32_t rd, uint32_t rs1, uint32_t rs2) { 66 return op | (rd << 7) | (rs1 << 15) | (rs2 << 20); 67 } 68 static uint32_t utype(uint32_t op, uint32_t rd, uint32_t imm) { 69 return op | (rd << 7) | (imm << 12); 70 } 71 72 RISCV::RISCV() { 73 copyRel = R_RISCV_COPY; 74 noneRel = R_RISCV_NONE; 75 pltRel = R_RISCV_JUMP_SLOT; 76 relativeRel = R_RISCV_RELATIVE; 77 if (config->is64) { 78 symbolicRel = R_RISCV_64; 79 tlsModuleIndexRel = R_RISCV_TLS_DTPMOD64; 80 tlsOffsetRel = R_RISCV_TLS_DTPREL64; 81 tlsGotRel = R_RISCV_TLS_TPREL64; 82 } else { 83 symbolicRel = R_RISCV_32; 84 tlsModuleIndexRel = R_RISCV_TLS_DTPMOD32; 85 tlsOffsetRel = R_RISCV_TLS_DTPREL32; 86 tlsGotRel = R_RISCV_TLS_TPREL32; 87 } 88 gotRel = symbolicRel; 89 90 // .got[0] = _DYNAMIC 91 gotBaseSymInGotPlt = false; 92 gotHeaderEntriesNum = 1; 93 94 // .got.plt[0] = _dl_runtime_resolve, .got.plt[1] = link_map 95 gotPltHeaderEntriesNum = 2; 96 97 pltEntrySize = 16; 98 pltHeaderSize = 32; 99 } 100 101 static uint32_t getEFlags(InputFile *f) { 102 if (config->is64) 103 return cast<ObjFile<ELF64LE>>(f)->getObj().getHeader()->e_flags; 104 return cast<ObjFile<ELF32LE>>(f)->getObj().getHeader()->e_flags; 105 } 106 107 uint32_t RISCV::calcEFlags() const { 108 assert(!objectFiles.empty()); 109 110 uint32_t target = getEFlags(objectFiles.front()); 111 112 for (InputFile *f : objectFiles) { 113 uint32_t eflags = getEFlags(f); 114 if (eflags & EF_RISCV_RVC) 115 target |= EF_RISCV_RVC; 116 117 if ((eflags & EF_RISCV_FLOAT_ABI) != (target & EF_RISCV_FLOAT_ABI)) 118 error(toString(f) + 119 ": cannot link object files with different floating-point ABI"); 120 121 if ((eflags & EF_RISCV_RVE) != (target & EF_RISCV_RVE)) 122 error(toString(f) + 123 ": cannot link object files with different EF_RISCV_RVE"); 124 } 125 126 return target; 127 } 128 129 void RISCV::writeGotHeader(uint8_t *buf) const { 130 if (config->is64) 131 write64le(buf, mainPart->dynamic->getVA()); 132 else 133 write32le(buf, mainPart->dynamic->getVA()); 134 } 135 136 void RISCV::writeGotPlt(uint8_t *buf, const Symbol &s) const { 137 if (config->is64) 138 write64le(buf, in.plt->getVA()); 139 else 140 write32le(buf, in.plt->getVA()); 141 } 142 143 void RISCV::writePltHeader(uint8_t *buf) const { 144 // 1: auipc t2, %pcrel_hi(.got.plt) 145 // sub t1, t1, t3 146 // l[wd] t3, %pcrel_lo(1b)(t2); t3 = _dl_runtime_resolve 147 // addi t1, t1, -pltHeaderSize-12; t1 = &.plt[i] - &.plt[0] 148 // addi t0, t2, %pcrel_lo(1b) 149 // srli t1, t1, (rv64?1:2); t1 = &.got.plt[i] - &.got.plt[0] 150 // l[wd] t0, Wordsize(t0); t0 = link_map 151 // jr t3 152 uint32_t offset = in.gotPlt->getVA() - in.plt->getVA(); 153 uint32_t load = config->is64 ? LD : LW; 154 write32le(buf + 0, utype(AUIPC, X_T2, hi20(offset))); 155 write32le(buf + 4, rtype(SUB, X_T1, X_T1, X_T3)); 156 write32le(buf + 8, itype(load, X_T3, X_T2, lo12(offset))); 157 write32le(buf + 12, itype(ADDI, X_T1, X_T1, -target->pltHeaderSize - 12)); 158 write32le(buf + 16, itype(ADDI, X_T0, X_T2, lo12(offset))); 159 write32le(buf + 20, itype(SRLI, X_T1, X_T1, config->is64 ? 1 : 2)); 160 write32le(buf + 24, itype(load, X_T0, X_T0, config->wordsize)); 161 write32le(buf + 28, itype(JALR, 0, X_T3, 0)); 162 } 163 164 void RISCV::writePlt(uint8_t *buf, uint64_t gotPltEntryAddr, 165 uint64_t pltEntryAddr, int32_t index, 166 unsigned relOff) const { 167 // 1: auipc t3, %pcrel_hi(f@.got.plt) 168 // l[wd] t3, %pcrel_lo(1b)(t3) 169 // jalr t1, t3 170 // nop 171 uint32_t offset = gotPltEntryAddr - pltEntryAddr; 172 write32le(buf + 0, utype(AUIPC, X_T3, hi20(offset))); 173 write32le(buf + 4, itype(config->is64 ? LD : LW, X_T3, X_T3, lo12(offset))); 174 write32le(buf + 8, itype(JALR, X_T1, X_T3, 0)); 175 write32le(buf + 12, itype(ADDI, 0, 0, 0)); 176 } 177 178 RelType RISCV::getDynRel(RelType type) const { 179 return type == target->symbolicRel ? type 180 : static_cast<RelType>(R_RISCV_NONE); 181 } 182 183 RelExpr RISCV::getRelExpr(const RelType type, const Symbol &s, 184 const uint8_t *loc) const { 185 switch (type) { 186 case R_RISCV_ADD8: 187 case R_RISCV_ADD16: 188 case R_RISCV_ADD32: 189 case R_RISCV_ADD64: 190 case R_RISCV_SET6: 191 case R_RISCV_SET8: 192 case R_RISCV_SET16: 193 case R_RISCV_SET32: 194 case R_RISCV_SUB6: 195 case R_RISCV_SUB8: 196 case R_RISCV_SUB16: 197 case R_RISCV_SUB32: 198 case R_RISCV_SUB64: 199 return R_RISCV_ADD; 200 case R_RISCV_JAL: 201 case R_RISCV_BRANCH: 202 case R_RISCV_PCREL_HI20: 203 case R_RISCV_RVC_BRANCH: 204 case R_RISCV_RVC_JUMP: 205 case R_RISCV_32_PCREL: 206 return R_PC; 207 case R_RISCV_CALL: 208 case R_RISCV_CALL_PLT: 209 return R_PLT_PC; 210 case R_RISCV_GOT_HI20: 211 return R_GOT_PC; 212 case R_RISCV_PCREL_LO12_I: 213 case R_RISCV_PCREL_LO12_S: 214 return R_RISCV_PC_INDIRECT; 215 case R_RISCV_TLS_GD_HI20: 216 return R_TLSGD_PC; 217 case R_RISCV_TLS_GOT_HI20: 218 config->hasStaticTlsModel = true; 219 return R_GOT_PC; 220 case R_RISCV_TPREL_HI20: 221 case R_RISCV_TPREL_LO12_I: 222 case R_RISCV_TPREL_LO12_S: 223 return R_TLS; 224 case R_RISCV_RELAX: 225 case R_RISCV_ALIGN: 226 case R_RISCV_TPREL_ADD: 227 return R_HINT; 228 default: 229 return R_ABS; 230 } 231 } 232 233 // Extract bits V[Begin:End], where range is inclusive, and Begin must be < 63. 234 static uint32_t extractBits(uint64_t v, uint32_t begin, uint32_t end) { 235 return (v & ((1ULL << (begin + 1)) - 1)) >> end; 236 } 237 238 void RISCV::relocateOne(uint8_t *loc, const RelType type, 239 const uint64_t val) const { 240 const unsigned bits = config->wordsize * 8; 241 242 switch (type) { 243 case R_RISCV_32: 244 write32le(loc, val); 245 return; 246 case R_RISCV_64: 247 write64le(loc, val); 248 return; 249 250 case R_RISCV_RVC_BRANCH: { 251 checkInt(loc, static_cast<int64_t>(val) >> 1, 8, type); 252 checkAlignment(loc, val, 2, type); 253 uint16_t insn = read16le(loc) & 0xE383; 254 uint16_t imm8 = extractBits(val, 8, 8) << 12; 255 uint16_t imm4_3 = extractBits(val, 4, 3) << 10; 256 uint16_t imm7_6 = extractBits(val, 7, 6) << 5; 257 uint16_t imm2_1 = extractBits(val, 2, 1) << 3; 258 uint16_t imm5 = extractBits(val, 5, 5) << 2; 259 insn |= imm8 | imm4_3 | imm7_6 | imm2_1 | imm5; 260 261 write16le(loc, insn); 262 return; 263 } 264 265 case R_RISCV_RVC_JUMP: { 266 checkInt(loc, static_cast<int64_t>(val) >> 1, 11, type); 267 checkAlignment(loc, val, 2, type); 268 uint16_t insn = read16le(loc) & 0xE003; 269 uint16_t imm11 = extractBits(val, 11, 11) << 12; 270 uint16_t imm4 = extractBits(val, 4, 4) << 11; 271 uint16_t imm9_8 = extractBits(val, 9, 8) << 9; 272 uint16_t imm10 = extractBits(val, 10, 10) << 8; 273 uint16_t imm6 = extractBits(val, 6, 6) << 7; 274 uint16_t imm7 = extractBits(val, 7, 7) << 6; 275 uint16_t imm3_1 = extractBits(val, 3, 1) << 3; 276 uint16_t imm5 = extractBits(val, 5, 5) << 2; 277 insn |= imm11 | imm4 | imm9_8 | imm10 | imm6 | imm7 | imm3_1 | imm5; 278 279 write16le(loc, insn); 280 return; 281 } 282 283 case R_RISCV_RVC_LUI: { 284 int64_t imm = SignExtend64(val + 0x800, bits) >> 12; 285 checkInt(loc, imm, 6, type); 286 if (imm == 0) { // `c.lui rd, 0` is illegal, convert to `c.li rd, 0` 287 write16le(loc, (read16le(loc) & 0x0F83) | 0x4000); 288 } else { 289 uint16_t imm17 = extractBits(val + 0x800, 17, 17) << 12; 290 uint16_t imm16_12 = extractBits(val + 0x800, 16, 12) << 2; 291 write16le(loc, (read16le(loc) & 0xEF83) | imm17 | imm16_12); 292 } 293 return; 294 } 295 296 case R_RISCV_JAL: { 297 checkInt(loc, static_cast<int64_t>(val) >> 1, 20, type); 298 checkAlignment(loc, val, 2, type); 299 300 uint32_t insn = read32le(loc) & 0xFFF; 301 uint32_t imm20 = extractBits(val, 20, 20) << 31; 302 uint32_t imm10_1 = extractBits(val, 10, 1) << 21; 303 uint32_t imm11 = extractBits(val, 11, 11) << 20; 304 uint32_t imm19_12 = extractBits(val, 19, 12) << 12; 305 insn |= imm20 | imm10_1 | imm11 | imm19_12; 306 307 write32le(loc, insn); 308 return; 309 } 310 311 case R_RISCV_BRANCH: { 312 checkInt(loc, static_cast<int64_t>(val) >> 1, 12, type); 313 checkAlignment(loc, val, 2, type); 314 315 uint32_t insn = read32le(loc) & 0x1FFF07F; 316 uint32_t imm12 = extractBits(val, 12, 12) << 31; 317 uint32_t imm10_5 = extractBits(val, 10, 5) << 25; 318 uint32_t imm4_1 = extractBits(val, 4, 1) << 8; 319 uint32_t imm11 = extractBits(val, 11, 11) << 7; 320 insn |= imm12 | imm10_5 | imm4_1 | imm11; 321 322 write32le(loc, insn); 323 return; 324 } 325 326 // auipc + jalr pair 327 case R_RISCV_CALL: 328 case R_RISCV_CALL_PLT: { 329 int64_t hi = SignExtend64(val + 0x800, bits) >> 12; 330 checkInt(loc, hi, 20, type); 331 if (isInt<20>(hi)) { 332 relocateOne(loc, R_RISCV_PCREL_HI20, val); 333 relocateOne(loc + 4, R_RISCV_PCREL_LO12_I, val); 334 } 335 return; 336 } 337 338 case R_RISCV_GOT_HI20: 339 case R_RISCV_PCREL_HI20: 340 case R_RISCV_TLS_GD_HI20: 341 case R_RISCV_TLS_GOT_HI20: 342 case R_RISCV_TPREL_HI20: 343 case R_RISCV_HI20: { 344 uint64_t hi = val + 0x800; 345 checkInt(loc, SignExtend64(hi, bits) >> 12, 20, type); 346 write32le(loc, (read32le(loc) & 0xFFF) | (hi & 0xFFFFF000)); 347 return; 348 } 349 350 case R_RISCV_PCREL_LO12_I: 351 case R_RISCV_TPREL_LO12_I: 352 case R_RISCV_LO12_I: { 353 uint64_t hi = (val + 0x800) >> 12; 354 uint64_t lo = val - (hi << 12); 355 write32le(loc, (read32le(loc) & 0xFFFFF) | ((lo & 0xFFF) << 20)); 356 return; 357 } 358 359 case R_RISCV_PCREL_LO12_S: 360 case R_RISCV_TPREL_LO12_S: 361 case R_RISCV_LO12_S: { 362 uint64_t hi = (val + 0x800) >> 12; 363 uint64_t lo = val - (hi << 12); 364 uint32_t imm11_5 = extractBits(lo, 11, 5) << 25; 365 uint32_t imm4_0 = extractBits(lo, 4, 0) << 7; 366 write32le(loc, (read32le(loc) & 0x1FFF07F) | imm11_5 | imm4_0); 367 return; 368 } 369 370 case R_RISCV_ADD8: 371 *loc += val; 372 return; 373 case R_RISCV_ADD16: 374 write16le(loc, read16le(loc) + val); 375 return; 376 case R_RISCV_ADD32: 377 write32le(loc, read32le(loc) + val); 378 return; 379 case R_RISCV_ADD64: 380 write64le(loc, read64le(loc) + val); 381 return; 382 case R_RISCV_SUB6: 383 *loc = (*loc & 0xc0) | (((*loc & 0x3f) - val) & 0x3f); 384 return; 385 case R_RISCV_SUB8: 386 *loc -= val; 387 return; 388 case R_RISCV_SUB16: 389 write16le(loc, read16le(loc) - val); 390 return; 391 case R_RISCV_SUB32: 392 write32le(loc, read32le(loc) - val); 393 return; 394 case R_RISCV_SUB64: 395 write64le(loc, read64le(loc) - val); 396 return; 397 case R_RISCV_SET6: 398 *loc = (*loc & 0xc0) | (val & 0x3f); 399 return; 400 case R_RISCV_SET8: 401 *loc = val; 402 return; 403 case R_RISCV_SET16: 404 write16le(loc, val); 405 return; 406 case R_RISCV_SET32: 407 case R_RISCV_32_PCREL: 408 write32le(loc, val); 409 return; 410 411 case R_RISCV_TLS_DTPREL32: 412 write32le(loc, val - dtpOffset); 413 break; 414 case R_RISCV_TLS_DTPREL64: 415 write64le(loc, val - dtpOffset); 416 break; 417 418 case R_RISCV_ALIGN: 419 case R_RISCV_RELAX: 420 return; // Ignored (for now) 421 case R_RISCV_NONE: 422 return; // Do nothing 423 424 // These are handled by the dynamic linker 425 case R_RISCV_RELATIVE: 426 case R_RISCV_COPY: 427 case R_RISCV_JUMP_SLOT: 428 // GP-relative relocations are only produced after relaxation, which 429 // we don't support for now 430 case R_RISCV_GPREL_I: 431 case R_RISCV_GPREL_S: 432 default: 433 error(getErrorLocation(loc) + 434 "unimplemented relocation: " + toString(type)); 435 return; 436 } 437 } 438 439 TargetInfo *elf::getRISCVTargetInfo() { 440 static RISCV target; 441 return ⌖ 442 } 443