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