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 "OutputSections.h" 11 #include "Symbols.h" 12 #include "SyntheticSections.h" 13 #include "Target.h" 14 #include "llvm/Support/ELFAttributes.h" 15 #include "llvm/Support/LEB128.h" 16 #include "llvm/Support/RISCVAttributeParser.h" 17 #include "llvm/Support/RISCVAttributes.h" 18 #include "llvm/Support/RISCVISAInfo.h" 19 #include "llvm/Support/TimeProfiler.h" 20 21 using namespace llvm; 22 using namespace llvm::object; 23 using namespace llvm::support::endian; 24 using namespace llvm::ELF; 25 using namespace lld; 26 using namespace lld::elf; 27 28 namespace { 29 30 class RISCV final : public TargetInfo { 31 public: 32 RISCV(); 33 uint32_t calcEFlags() const override; 34 int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override; 35 void writeGotHeader(uint8_t *buf) const override; 36 void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 37 void writeIgotPlt(uint8_t *buf, const Symbol &s) const override; 38 void writePltHeader(uint8_t *buf) const override; 39 void writePlt(uint8_t *buf, const Symbol &sym, 40 uint64_t pltEntryAddr) const override; 41 RelType getDynRel(RelType type) const override; 42 RelExpr getRelExpr(RelType type, const Symbol &s, 43 const uint8_t *loc) const override; 44 void relocate(uint8_t *loc, const Relocation &rel, 45 uint64_t val) const override; 46 bool relaxOnce(int pass) const override; 47 }; 48 49 } // end anonymous namespace 50 51 const uint64_t dtpOffset = 0x800; 52 53 enum Op { 54 ADDI = 0x13, 55 AUIPC = 0x17, 56 JALR = 0x67, 57 LD = 0x3003, 58 LW = 0x2003, 59 SRLI = 0x5013, 60 SUB = 0x40000033, 61 }; 62 63 enum Reg { 64 X_RA = 1, 65 X_TP = 4, 66 X_T0 = 5, 67 X_T1 = 6, 68 X_T2 = 7, 69 X_T3 = 28, 70 }; 71 72 static uint32_t hi20(uint32_t val) { return (val + 0x800) >> 12; } 73 static uint32_t lo12(uint32_t val) { return val & 4095; } 74 75 static uint32_t itype(uint32_t op, uint32_t rd, uint32_t rs1, uint32_t imm) { 76 return op | (rd << 7) | (rs1 << 15) | (imm << 20); 77 } 78 static uint32_t rtype(uint32_t op, uint32_t rd, uint32_t rs1, uint32_t rs2) { 79 return op | (rd << 7) | (rs1 << 15) | (rs2 << 20); 80 } 81 static uint32_t utype(uint32_t op, uint32_t rd, uint32_t imm) { 82 return op | (rd << 7) | (imm << 12); 83 } 84 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; 88 } 89 90 static uint32_t setLO12_I(uint32_t insn, uint32_t imm) { 91 return (insn & 0xfffff) | (imm << 20); 92 } 93 static uint32_t setLO12_S(uint32_t insn, uint32_t imm) { 94 return (insn & 0x1fff07f) | (extractBits(imm, 11, 5) << 25) | 95 (extractBits(imm, 4, 0) << 7); 96 } 97 98 RISCV::RISCV() { 99 copyRel = R_RISCV_COPY; 100 pltRel = R_RISCV_JUMP_SLOT; 101 relativeRel = R_RISCV_RELATIVE; 102 iRelativeRel = R_RISCV_IRELATIVE; 103 if (config->is64) { 104 symbolicRel = R_RISCV_64; 105 tlsModuleIndexRel = R_RISCV_TLS_DTPMOD64; 106 tlsOffsetRel = R_RISCV_TLS_DTPREL64; 107 tlsGotRel = R_RISCV_TLS_TPREL64; 108 } else { 109 symbolicRel = R_RISCV_32; 110 tlsModuleIndexRel = R_RISCV_TLS_DTPMOD32; 111 tlsOffsetRel = R_RISCV_TLS_DTPREL32; 112 tlsGotRel = R_RISCV_TLS_TPREL32; 113 } 114 gotRel = symbolicRel; 115 116 // .got[0] = _DYNAMIC 117 gotHeaderEntriesNum = 1; 118 119 // .got.plt[0] = _dl_runtime_resolve, .got.plt[1] = link_map 120 gotPltHeaderEntriesNum = 2; 121 122 pltHeaderSize = 32; 123 pltEntrySize = 16; 124 ipltEntrySize = 16; 125 } 126 127 static uint32_t getEFlags(InputFile *f) { 128 if (config->is64) 129 return cast<ObjFile<ELF64LE>>(f)->getObj().getHeader().e_flags; 130 return cast<ObjFile<ELF32LE>>(f)->getObj().getHeader().e_flags; 131 } 132 133 uint32_t RISCV::calcEFlags() const { 134 // If there are only binary input files (from -b binary), use a 135 // value of 0 for the ELF header flags. 136 if (ctx.objectFiles.empty()) 137 return 0; 138 139 uint32_t target = getEFlags(ctx.objectFiles.front()); 140 141 for (InputFile *f : ctx.objectFiles) { 142 uint32_t eflags = getEFlags(f); 143 if (eflags & EF_RISCV_RVC) 144 target |= EF_RISCV_RVC; 145 146 if ((eflags & EF_RISCV_FLOAT_ABI) != (target & EF_RISCV_FLOAT_ABI)) 147 error( 148 toString(f) + 149 ": cannot link object files with different floating-point ABI from " + 150 toString(ctx.objectFiles[0])); 151 152 if ((eflags & EF_RISCV_RVE) != (target & EF_RISCV_RVE)) 153 error(toString(f) + 154 ": cannot link object files with different EF_RISCV_RVE"); 155 } 156 157 return target; 158 } 159 160 int64_t RISCV::getImplicitAddend(const uint8_t *buf, RelType type) const { 161 switch (type) { 162 default: 163 internalLinkerError(getErrorLocation(buf), 164 "cannot read addend for relocation " + toString(type)); 165 return 0; 166 case R_RISCV_32: 167 case R_RISCV_TLS_DTPMOD32: 168 case R_RISCV_TLS_DTPREL32: 169 case R_RISCV_TLS_TPREL32: 170 return SignExtend64<32>(read32le(buf)); 171 case R_RISCV_64: 172 case R_RISCV_TLS_DTPMOD64: 173 case R_RISCV_TLS_DTPREL64: 174 case R_RISCV_TLS_TPREL64: 175 return read64le(buf); 176 case R_RISCV_RELATIVE: 177 case R_RISCV_IRELATIVE: 178 return config->is64 ? read64le(buf) : read32le(buf); 179 case R_RISCV_NONE: 180 case R_RISCV_JUMP_SLOT: 181 // These relocations are defined as not having an implicit addend. 182 return 0; 183 } 184 } 185 186 void RISCV::writeGotHeader(uint8_t *buf) const { 187 if (config->is64) 188 write64le(buf, mainPart->dynamic->getVA()); 189 else 190 write32le(buf, mainPart->dynamic->getVA()); 191 } 192 193 void RISCV::writeGotPlt(uint8_t *buf, const Symbol &s) const { 194 if (config->is64) 195 write64le(buf, in.plt->getVA()); 196 else 197 write32le(buf, in.plt->getVA()); 198 } 199 200 void RISCV::writeIgotPlt(uint8_t *buf, const Symbol &s) const { 201 if (config->writeAddends) { 202 if (config->is64) 203 write64le(buf, s.getVA()); 204 else 205 write32le(buf, s.getVA()); 206 } 207 } 208 209 void RISCV::writePltHeader(uint8_t *buf) const { 210 // 1: auipc t2, %pcrel_hi(.got.plt) 211 // sub t1, t1, t3 212 // l[wd] t3, %pcrel_lo(1b)(t2); t3 = _dl_runtime_resolve 213 // addi t1, t1, -pltHeaderSize-12; t1 = &.plt[i] - &.plt[0] 214 // addi t0, t2, %pcrel_lo(1b) 215 // srli t1, t1, (rv64?1:2); t1 = &.got.plt[i] - &.got.plt[0] 216 // l[wd] t0, Wordsize(t0); t0 = link_map 217 // jr t3 218 uint32_t offset = in.gotPlt->getVA() - in.plt->getVA(); 219 uint32_t load = config->is64 ? LD : LW; 220 write32le(buf + 0, utype(AUIPC, X_T2, hi20(offset))); 221 write32le(buf + 4, rtype(SUB, X_T1, X_T1, X_T3)); 222 write32le(buf + 8, itype(load, X_T3, X_T2, lo12(offset))); 223 write32le(buf + 12, itype(ADDI, X_T1, X_T1, -target->pltHeaderSize - 12)); 224 write32le(buf + 16, itype(ADDI, X_T0, X_T2, lo12(offset))); 225 write32le(buf + 20, itype(SRLI, X_T1, X_T1, config->is64 ? 1 : 2)); 226 write32le(buf + 24, itype(load, X_T0, X_T0, config->wordsize)); 227 write32le(buf + 28, itype(JALR, 0, X_T3, 0)); 228 } 229 230 void RISCV::writePlt(uint8_t *buf, const Symbol &sym, 231 uint64_t pltEntryAddr) const { 232 // 1: auipc t3, %pcrel_hi(f@.got.plt) 233 // l[wd] t3, %pcrel_lo(1b)(t3) 234 // jalr t1, t3 235 // nop 236 uint32_t offset = sym.getGotPltVA() - pltEntryAddr; 237 write32le(buf + 0, utype(AUIPC, X_T3, hi20(offset))); 238 write32le(buf + 4, itype(config->is64 ? LD : LW, X_T3, X_T3, lo12(offset))); 239 write32le(buf + 8, itype(JALR, X_T1, X_T3, 0)); 240 write32le(buf + 12, itype(ADDI, 0, 0, 0)); 241 } 242 243 RelType RISCV::getDynRel(RelType type) const { 244 return type == target->symbolicRel ? type 245 : static_cast<RelType>(R_RISCV_NONE); 246 } 247 248 RelExpr RISCV::getRelExpr(const RelType type, const Symbol &s, 249 const uint8_t *loc) const { 250 switch (type) { 251 case R_RISCV_NONE: 252 return R_NONE; 253 case R_RISCV_32: 254 case R_RISCV_64: 255 case R_RISCV_HI20: 256 case R_RISCV_LO12_I: 257 case R_RISCV_LO12_S: 258 case R_RISCV_RVC_LUI: 259 return R_ABS; 260 case R_RISCV_ADD8: 261 case R_RISCV_ADD16: 262 case R_RISCV_ADD32: 263 case R_RISCV_ADD64: 264 case R_RISCV_SET6: 265 case R_RISCV_SET8: 266 case R_RISCV_SET16: 267 case R_RISCV_SET32: 268 case R_RISCV_SUB6: 269 case R_RISCV_SUB8: 270 case R_RISCV_SUB16: 271 case R_RISCV_SUB32: 272 case R_RISCV_SUB64: 273 return R_RISCV_ADD; 274 case R_RISCV_JAL: 275 case R_RISCV_BRANCH: 276 case R_RISCV_PCREL_HI20: 277 case R_RISCV_RVC_BRANCH: 278 case R_RISCV_RVC_JUMP: 279 case R_RISCV_32_PCREL: 280 return R_PC; 281 case R_RISCV_CALL: 282 case R_RISCV_CALL_PLT: 283 return R_PLT_PC; 284 case R_RISCV_GOT_HI20: 285 return R_GOT_PC; 286 case R_RISCV_PCREL_LO12_I: 287 case R_RISCV_PCREL_LO12_S: 288 return R_RISCV_PC_INDIRECT; 289 case R_RISCV_TLS_GD_HI20: 290 return R_TLSGD_PC; 291 case R_RISCV_TLS_GOT_HI20: 292 return R_GOT_PC; 293 case R_RISCV_TPREL_HI20: 294 case R_RISCV_TPREL_LO12_I: 295 case R_RISCV_TPREL_LO12_S: 296 return R_TPREL; 297 case R_RISCV_ALIGN: 298 return R_RELAX_HINT; 299 case R_RISCV_TPREL_ADD: 300 case R_RISCV_RELAX: 301 return config->relax ? R_RELAX_HINT : R_NONE; 302 default: 303 error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) + 304 ") against symbol " + toString(s)); 305 return R_NONE; 306 } 307 } 308 309 void RISCV::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { 310 const unsigned bits = config->wordsize * 8; 311 312 switch (rel.type) { 313 case R_RISCV_32: 314 write32le(loc, val); 315 return; 316 case R_RISCV_64: 317 write64le(loc, val); 318 return; 319 320 case R_RISCV_RVC_BRANCH: { 321 checkInt(loc, val, 9, rel); 322 checkAlignment(loc, val, 2, rel); 323 uint16_t insn = read16le(loc) & 0xE383; 324 uint16_t imm8 = extractBits(val, 8, 8) << 12; 325 uint16_t imm4_3 = extractBits(val, 4, 3) << 10; 326 uint16_t imm7_6 = extractBits(val, 7, 6) << 5; 327 uint16_t imm2_1 = extractBits(val, 2, 1) << 3; 328 uint16_t imm5 = extractBits(val, 5, 5) << 2; 329 insn |= imm8 | imm4_3 | imm7_6 | imm2_1 | imm5; 330 331 write16le(loc, insn); 332 return; 333 } 334 335 case R_RISCV_RVC_JUMP: { 336 checkInt(loc, val, 12, rel); 337 checkAlignment(loc, val, 2, rel); 338 uint16_t insn = read16le(loc) & 0xE003; 339 uint16_t imm11 = extractBits(val, 11, 11) << 12; 340 uint16_t imm4 = extractBits(val, 4, 4) << 11; 341 uint16_t imm9_8 = extractBits(val, 9, 8) << 9; 342 uint16_t imm10 = extractBits(val, 10, 10) << 8; 343 uint16_t imm6 = extractBits(val, 6, 6) << 7; 344 uint16_t imm7 = extractBits(val, 7, 7) << 6; 345 uint16_t imm3_1 = extractBits(val, 3, 1) << 3; 346 uint16_t imm5 = extractBits(val, 5, 5) << 2; 347 insn |= imm11 | imm4 | imm9_8 | imm10 | imm6 | imm7 | imm3_1 | imm5; 348 349 write16le(loc, insn); 350 return; 351 } 352 353 case R_RISCV_RVC_LUI: { 354 int64_t imm = SignExtend64(val + 0x800, bits) >> 12; 355 checkInt(loc, imm, 6, rel); 356 if (imm == 0) { // `c.lui rd, 0` is illegal, convert to `c.li rd, 0` 357 write16le(loc, (read16le(loc) & 0x0F83) | 0x4000); 358 } else { 359 uint16_t imm17 = extractBits(val + 0x800, 17, 17) << 12; 360 uint16_t imm16_12 = extractBits(val + 0x800, 16, 12) << 2; 361 write16le(loc, (read16le(loc) & 0xEF83) | imm17 | imm16_12); 362 } 363 return; 364 } 365 366 case R_RISCV_JAL: { 367 checkInt(loc, val, 21, rel); 368 checkAlignment(loc, val, 2, rel); 369 370 uint32_t insn = read32le(loc) & 0xFFF; 371 uint32_t imm20 = extractBits(val, 20, 20) << 31; 372 uint32_t imm10_1 = extractBits(val, 10, 1) << 21; 373 uint32_t imm11 = extractBits(val, 11, 11) << 20; 374 uint32_t imm19_12 = extractBits(val, 19, 12) << 12; 375 insn |= imm20 | imm10_1 | imm11 | imm19_12; 376 377 write32le(loc, insn); 378 return; 379 } 380 381 case R_RISCV_BRANCH: { 382 checkInt(loc, val, 13, rel); 383 checkAlignment(loc, val, 2, rel); 384 385 uint32_t insn = read32le(loc) & 0x1FFF07F; 386 uint32_t imm12 = extractBits(val, 12, 12) << 31; 387 uint32_t imm10_5 = extractBits(val, 10, 5) << 25; 388 uint32_t imm4_1 = extractBits(val, 4, 1) << 8; 389 uint32_t imm11 = extractBits(val, 11, 11) << 7; 390 insn |= imm12 | imm10_5 | imm4_1 | imm11; 391 392 write32le(loc, insn); 393 return; 394 } 395 396 // auipc + jalr pair 397 case R_RISCV_CALL: 398 case R_RISCV_CALL_PLT: { 399 int64_t hi = SignExtend64(val + 0x800, bits) >> 12; 400 checkInt(loc, hi, 20, rel); 401 if (isInt<20>(hi)) { 402 relocateNoSym(loc, R_RISCV_PCREL_HI20, val); 403 relocateNoSym(loc + 4, R_RISCV_PCREL_LO12_I, val); 404 } 405 return; 406 } 407 408 case R_RISCV_GOT_HI20: 409 case R_RISCV_PCREL_HI20: 410 case R_RISCV_TLS_GD_HI20: 411 case R_RISCV_TLS_GOT_HI20: 412 case R_RISCV_TPREL_HI20: 413 case R_RISCV_HI20: { 414 uint64_t hi = val + 0x800; 415 checkInt(loc, SignExtend64(hi, bits) >> 12, 20, rel); 416 write32le(loc, (read32le(loc) & 0xFFF) | (hi & 0xFFFFF000)); 417 return; 418 } 419 420 case R_RISCV_PCREL_LO12_I: 421 case R_RISCV_TPREL_LO12_I: 422 case R_RISCV_LO12_I: { 423 uint64_t hi = (val + 0x800) >> 12; 424 uint64_t lo = val - (hi << 12); 425 write32le(loc, setLO12_I(read32le(loc), lo & 0xfff)); 426 return; 427 } 428 429 case R_RISCV_PCREL_LO12_S: 430 case R_RISCV_TPREL_LO12_S: 431 case R_RISCV_LO12_S: { 432 uint64_t hi = (val + 0x800) >> 12; 433 uint64_t lo = val - (hi << 12); 434 write32le(loc, setLO12_S(read32le(loc), lo)); 435 return; 436 } 437 438 case R_RISCV_ADD8: 439 *loc += val; 440 return; 441 case R_RISCV_ADD16: 442 write16le(loc, read16le(loc) + val); 443 return; 444 case R_RISCV_ADD32: 445 write32le(loc, read32le(loc) + val); 446 return; 447 case R_RISCV_ADD64: 448 write64le(loc, read64le(loc) + val); 449 return; 450 case R_RISCV_SUB6: 451 *loc = (*loc & 0xc0) | (((*loc & 0x3f) - val) & 0x3f); 452 return; 453 case R_RISCV_SUB8: 454 *loc -= val; 455 return; 456 case R_RISCV_SUB16: 457 write16le(loc, read16le(loc) - val); 458 return; 459 case R_RISCV_SUB32: 460 write32le(loc, read32le(loc) - val); 461 return; 462 case R_RISCV_SUB64: 463 write64le(loc, read64le(loc) - val); 464 return; 465 case R_RISCV_SET6: 466 *loc = (*loc & 0xc0) | (val & 0x3f); 467 return; 468 case R_RISCV_SET8: 469 *loc = val; 470 return; 471 case R_RISCV_SET16: 472 write16le(loc, val); 473 return; 474 case R_RISCV_SET32: 475 case R_RISCV_32_PCREL: 476 write32le(loc, val); 477 return; 478 479 case R_RISCV_TLS_DTPREL32: 480 write32le(loc, val - dtpOffset); 481 break; 482 case R_RISCV_TLS_DTPREL64: 483 write64le(loc, val - dtpOffset); 484 break; 485 486 case R_RISCV_RELAX: 487 return; // Ignored (for now) 488 489 default: 490 llvm_unreachable("unknown relocation"); 491 } 492 } 493 494 namespace { 495 struct SymbolAnchor { 496 uint64_t offset; 497 Defined *d; 498 bool end; // true for the anchor of st_value+st_size 499 }; 500 } // namespace 501 502 struct elf::RISCVRelaxAux { 503 // This records symbol start and end offsets which will be adjusted according 504 // to the nearest relocDeltas element. 505 SmallVector<SymbolAnchor, 0> anchors; 506 // For relocations[i], the actual offset is r_offset - (i ? relocDeltas[i-1] : 507 // 0). 508 std::unique_ptr<uint32_t[]> relocDeltas; 509 // For relocations[i], the actual type is relocTypes[i]. 510 std::unique_ptr<RelType[]> relocTypes; 511 SmallVector<uint32_t, 0> writes; 512 }; 513 514 static void initSymbolAnchors() { 515 SmallVector<InputSection *, 0> storage; 516 for (OutputSection *osec : outputSections) { 517 if (!(osec->flags & SHF_EXECINSTR)) 518 continue; 519 for (InputSection *sec : getInputSections(*osec, storage)) { 520 sec->relaxAux = make<RISCVRelaxAux>(); 521 if (sec->relocs().size()) { 522 sec->relaxAux->relocDeltas = 523 std::make_unique<uint32_t[]>(sec->relocs().size()); 524 sec->relaxAux->relocTypes = 525 std::make_unique<RelType[]>(sec->relocs().size()); 526 } 527 } 528 } 529 // Store anchors (st_value and st_value+st_size) for symbols relative to text 530 // sections. 531 for (InputFile *file : ctx.objectFiles) 532 for (Symbol *sym : file->getSymbols()) { 533 auto *d = dyn_cast<Defined>(sym); 534 if (!d || d->file != file) 535 continue; 536 if (auto *sec = dyn_cast_or_null<InputSection>(d->section)) 537 if (sec->flags & SHF_EXECINSTR && sec->relaxAux) { 538 // If sec is discarded, relaxAux will be nullptr. 539 sec->relaxAux->anchors.push_back({d->value, d, false}); 540 sec->relaxAux->anchors.push_back({d->value + d->size, d, true}); 541 } 542 } 543 // Sort anchors by offset so that we can find the closest relocation 544 // efficiently. For a zero size symbol, ensure that its start anchor precedes 545 // its end anchor. For two symbols with anchors at the same offset, their 546 // order does not matter. 547 for (OutputSection *osec : outputSections) { 548 if (!(osec->flags & SHF_EXECINSTR)) 549 continue; 550 for (InputSection *sec : getInputSections(*osec, storage)) { 551 llvm::sort(sec->relaxAux->anchors, [](auto &a, auto &b) { 552 return std::make_pair(a.offset, a.end) < 553 std::make_pair(b.offset, b.end); 554 }); 555 } 556 } 557 } 558 559 // Relax R_RISCV_CALL/R_RISCV_CALL_PLT auipc+jalr to c.j, c.jal, or jal. 560 static void relaxCall(const InputSection &sec, size_t i, uint64_t loc, 561 Relocation &r, uint32_t &remove) { 562 const bool rvc = config->eflags & EF_RISCV_RVC; 563 const Symbol &sym = *r.sym; 564 const uint64_t insnPair = read64le(sec.content().data() + r.offset); 565 const uint32_t rd = extractBits(insnPair, 32 + 11, 32 + 7); 566 const uint64_t dest = 567 (r.expr == R_PLT_PC ? sym.getPltVA() : sym.getVA()) + r.addend; 568 const int64_t displace = dest - loc; 569 570 if (rvc && isInt<12>(displace) && rd == 0) { 571 sec.relaxAux->relocTypes[i] = R_RISCV_RVC_JUMP; 572 sec.relaxAux->writes.push_back(0xa001); // c.j 573 remove = 6; 574 } else if (rvc && isInt<12>(displace) && rd == X_RA && 575 !config->is64) { // RV32C only 576 sec.relaxAux->relocTypes[i] = R_RISCV_RVC_JUMP; 577 sec.relaxAux->writes.push_back(0x2001); // c.jal 578 remove = 6; 579 } else if (isInt<21>(displace)) { 580 sec.relaxAux->relocTypes[i] = R_RISCV_JAL; 581 sec.relaxAux->writes.push_back(0x6f | rd << 7); // jal 582 remove = 4; 583 } 584 } 585 586 // Relax local-exec TLS when hi20 is zero. 587 static void relaxTlsLe(const InputSection &sec, size_t i, uint64_t loc, 588 Relocation &r, uint32_t &remove) { 589 uint64_t val = r.sym->getVA(r.addend); 590 if (hi20(val) != 0) 591 return; 592 uint32_t insn = read32le(sec.content().data() + r.offset); 593 switch (r.type) { 594 case R_RISCV_TPREL_HI20: 595 case R_RISCV_TPREL_ADD: 596 // Remove lui rd, %tprel_hi(x) and add rd, rd, tp, %tprel_add(x). 597 sec.relaxAux->relocTypes[i] = R_RISCV_RELAX; 598 remove = 4; 599 break; 600 case R_RISCV_TPREL_LO12_I: 601 // addi rd, rd, %tprel_lo(x) => addi rd, tp, st_value(x) 602 sec.relaxAux->relocTypes[i] = R_RISCV_32; 603 insn = (insn & ~(31 << 15)) | (X_TP << 15); 604 sec.relaxAux->writes.push_back(setLO12_I(insn, val)); 605 break; 606 case R_RISCV_TPREL_LO12_S: 607 // sw rs, %tprel_lo(x)(rd) => sw rs, st_value(x)(rd) 608 sec.relaxAux->relocTypes[i] = R_RISCV_32; 609 insn = (insn & ~(31 << 15)) | (X_TP << 15); 610 sec.relaxAux->writes.push_back(setLO12_S(insn, val)); 611 break; 612 } 613 } 614 615 static bool relax(InputSection &sec) { 616 const uint64_t secAddr = sec.getVA(); 617 auto &aux = *sec.relaxAux; 618 bool changed = false; 619 620 // Get st_value delta for symbols relative to this section from the previous 621 // iteration. 622 DenseMap<const Defined *, uint64_t> valueDelta; 623 ArrayRef<SymbolAnchor> sa = ArrayRef(aux.anchors); 624 uint32_t delta = 0; 625 for (auto [i, r] : llvm::enumerate(sec.relocs())) { 626 for (; sa.size() && sa[0].offset <= r.offset; sa = sa.slice(1)) 627 if (!sa[0].end) 628 valueDelta[sa[0].d] = delta; 629 delta = aux.relocDeltas[i]; 630 } 631 for (const SymbolAnchor &sa : sa) 632 if (!sa.end) 633 valueDelta[sa.d] = delta; 634 sa = ArrayRef(aux.anchors); 635 delta = 0; 636 637 std::fill_n(aux.relocTypes.get(), sec.relocs().size(), R_RISCV_NONE); 638 aux.writes.clear(); 639 for (auto [i, r] : llvm::enumerate(sec.relocs())) { 640 const uint64_t loc = secAddr + r.offset - delta; 641 uint32_t &cur = aux.relocDeltas[i], remove = 0; 642 switch (r.type) { 643 case R_RISCV_ALIGN: { 644 const uint64_t nextLoc = loc + r.addend; 645 const uint64_t align = PowerOf2Ceil(r.addend + 2); 646 // All bytes beyond the alignment boundary should be removed. 647 remove = nextLoc - ((loc + align - 1) & -align); 648 assert(static_cast<int32_t>(remove) >= 0 && 649 "R_RISCV_ALIGN needs expanding the content"); 650 break; 651 } 652 case R_RISCV_CALL: 653 case R_RISCV_CALL_PLT: 654 if (i + 1 != sec.relocs().size() && 655 sec.relocs()[i + 1].type == R_RISCV_RELAX) 656 relaxCall(sec, i, loc, r, remove); 657 break; 658 case R_RISCV_TPREL_HI20: 659 case R_RISCV_TPREL_ADD: 660 case R_RISCV_TPREL_LO12_I: 661 case R_RISCV_TPREL_LO12_S: 662 if (i + 1 != sec.relocs().size() && 663 sec.relocs()[i + 1].type == R_RISCV_RELAX) 664 relaxTlsLe(sec, i, loc, r, remove); 665 break; 666 } 667 668 // For all anchors whose offsets are <= r.offset, they are preceded by 669 // the previous relocation whose `relocDeltas` value equals `delta`. 670 // Decrease their st_value and update their st_size. 671 for (; sa.size() && sa[0].offset <= r.offset; sa = sa.slice(1)) { 672 if (sa[0].end) 673 sa[0].d->size = sa[0].offset - delta - sa[0].d->value; 674 else 675 sa[0].d->value -= delta - valueDelta.find(sa[0].d)->second; 676 } 677 delta += remove; 678 if (delta != cur) { 679 cur = delta; 680 changed = true; 681 } 682 } 683 684 for (const SymbolAnchor &a : sa) { 685 if (a.end) 686 a.d->size = a.offset - delta - a.d->value; 687 else 688 a.d->value -= delta - valueDelta.find(a.d)->second; 689 } 690 // Inform assignAddresses that the size has changed. 691 if (!isUInt<16>(delta)) 692 fatal("section size decrease is too large"); 693 sec.bytesDropped = delta; 694 return changed; 695 } 696 697 // When relaxing just R_RISCV_ALIGN, relocDeltas is usually changed only once in 698 // the absence of a linker script. For call and load/store R_RISCV_RELAX, code 699 // shrinkage may reduce displacement and make more relocations eligible for 700 // relaxation. Code shrinkage may increase displacement to a call/load/store 701 // target at a higher fixed address, invalidating an earlier relaxation. Any 702 // change in section sizes can have cascading effect and require another 703 // relaxation pass. 704 bool RISCV::relaxOnce(int pass) const { 705 llvm::TimeTraceScope timeScope("RISC-V relaxOnce"); 706 if (config->relocatable) 707 return false; 708 709 if (pass == 0) 710 initSymbolAnchors(); 711 712 SmallVector<InputSection *, 0> storage; 713 bool changed = false; 714 for (OutputSection *osec : outputSections) { 715 if (!(osec->flags & SHF_EXECINSTR)) 716 continue; 717 for (InputSection *sec : getInputSections(*osec, storage)) 718 changed |= relax(*sec); 719 } 720 return changed; 721 } 722 723 void elf::riscvFinalizeRelax(int passes) { 724 llvm::TimeTraceScope timeScope("Finalize RISC-V relaxation"); 725 log("relaxation passes: " + Twine(passes)); 726 SmallVector<InputSection *, 0> storage; 727 for (OutputSection *osec : outputSections) { 728 if (!(osec->flags & SHF_EXECINSTR)) 729 continue; 730 for (InputSection *sec : getInputSections(*osec, storage)) { 731 RISCVRelaxAux &aux = *sec->relaxAux; 732 if (!aux.relocDeltas) 733 continue; 734 735 MutableArrayRef<Relocation> rels = sec->relocs(); 736 ArrayRef<uint8_t> old = sec->content(); 737 size_t newSize = old.size() - aux.relocDeltas[rels.size() - 1]; 738 size_t writesIdx = 0; 739 uint8_t *p = context().bAlloc.Allocate<uint8_t>(newSize); 740 uint64_t offset = 0; 741 int64_t delta = 0; 742 sec->content_ = p; 743 sec->size = newSize; 744 sec->bytesDropped = 0; 745 746 // Update section content: remove NOPs for R_RISCV_ALIGN and rewrite 747 // instructions for relaxed relocations. 748 for (size_t i = 0, e = rels.size(); i != e; ++i) { 749 uint32_t remove = aux.relocDeltas[i] - delta; 750 delta = aux.relocDeltas[i]; 751 if (remove == 0 && aux.relocTypes[i] == R_RISCV_NONE) 752 continue; 753 754 // Copy from last location to the current relocated location. 755 const Relocation &r = rels[i]; 756 uint64_t size = r.offset - offset; 757 memcpy(p, old.data() + offset, size); 758 p += size; 759 760 // For R_RISCV_ALIGN, we will place `offset` in a location (among NOPs) 761 // to satisfy the alignment requirement. If both `remove` and r.addend 762 // are multiples of 4, it is as if we have skipped some NOPs. Otherwise 763 // we are in the middle of a 4-byte NOP, and we need to rewrite the NOP 764 // sequence. 765 int64_t skip = 0; 766 if (r.type == R_RISCV_ALIGN) { 767 if (remove % 4 || r.addend % 4) { 768 skip = r.addend - remove; 769 int64_t j = 0; 770 for (; j + 4 <= skip; j += 4) 771 write32le(p + j, 0x00000013); // nop 772 if (j != skip) { 773 assert(j + 2 == skip); 774 write16le(p + j, 0x0001); // c.nop 775 } 776 } 777 } else if (RelType newType = aux.relocTypes[i]) { 778 switch (newType) { 779 case R_RISCV_RELAX: 780 // Used by relaxTlsLe to indicate the relocation is ignored. 781 break; 782 case R_RISCV_RVC_JUMP: 783 skip = 2; 784 write16le(p, aux.writes[writesIdx++]); 785 break; 786 case R_RISCV_JAL: 787 skip = 4; 788 write32le(p, aux.writes[writesIdx++]); 789 break; 790 case R_RISCV_32: 791 // Used by relaxTlsLe to write a uint32_t then suppress the handling 792 // in relocateAlloc. 793 skip = 4; 794 write32le(p, aux.writes[writesIdx++]); 795 aux.relocTypes[i] = R_RISCV_NONE; 796 break; 797 default: 798 llvm_unreachable("unsupported type"); 799 } 800 } 801 802 p += skip; 803 offset = r.offset + skip + remove; 804 } 805 memcpy(p, old.data() + offset, old.size() - offset); 806 807 // Subtract the previous relocDeltas value from the relocation offset. 808 // For a pair of R_RISCV_CALL/R_RISCV_RELAX with the same offset, decrease 809 // their r_offset by the same delta. 810 delta = 0; 811 for (size_t i = 0, e = rels.size(); i != e;) { 812 uint64_t cur = rels[i].offset; 813 do { 814 rels[i].offset -= delta; 815 if (aux.relocTypes[i] != R_RISCV_NONE) 816 rels[i].type = aux.relocTypes[i]; 817 } while (++i != e && rels[i].offset == cur); 818 delta = aux.relocDeltas[i - 1]; 819 } 820 } 821 } 822 } 823 824 namespace { 825 // Representation of the merged .riscv.attributes input sections. The psABI 826 // specifies merge policy for attributes. E.g. if we link an object without an 827 // extension with an object with the extension, the output Tag_RISCV_arch shall 828 // contain the extension. Some tools like objdump parse .riscv.attributes and 829 // disabling some instructions if the first Tag_RISCV_arch does not contain an 830 // extension. 831 class RISCVAttributesSection final : public SyntheticSection { 832 public: 833 RISCVAttributesSection() 834 : SyntheticSection(0, SHT_RISCV_ATTRIBUTES, 1, ".riscv.attributes") {} 835 836 size_t getSize() const override { return size; } 837 void writeTo(uint8_t *buf) override; 838 839 static constexpr StringRef vendor = "riscv"; 840 DenseMap<unsigned, unsigned> intAttr; 841 DenseMap<unsigned, StringRef> strAttr; 842 size_t size = 0; 843 }; 844 } // namespace 845 846 static void mergeArch(RISCVISAInfo::OrderedExtensionMap &mergedExts, 847 unsigned &mergedXlen, const InputSectionBase *sec, 848 StringRef s) { 849 auto maybeInfo = RISCVISAInfo::parseNormalizedArchString(s); 850 if (!maybeInfo) { 851 errorOrWarn(toString(sec) + ": " + s + ": " + 852 llvm::toString(maybeInfo.takeError())); 853 return; 854 } 855 856 // Merge extensions. 857 RISCVISAInfo &info = **maybeInfo; 858 if (mergedExts.empty()) { 859 mergedExts = info.getExtensions(); 860 mergedXlen = info.getXLen(); 861 } else { 862 for (const auto &ext : info.getExtensions()) { 863 if (auto it = mergedExts.find(ext.first); it != mergedExts.end()) { 864 if (std::tie(it->second.MajorVersion, it->second.MinorVersion) >= 865 std::tie(ext.second.MajorVersion, ext.second.MinorVersion)) 866 continue; 867 } 868 mergedExts[ext.first] = ext.second; 869 } 870 } 871 } 872 873 static RISCVAttributesSection * 874 mergeAttributesSection(const SmallVector<InputSectionBase *, 0> §ions) { 875 RISCVISAInfo::OrderedExtensionMap exts; 876 const InputSectionBase *firstStackAlign = nullptr; 877 unsigned firstStackAlignValue = 0, xlen = 0; 878 bool hasArch = false; 879 880 in.riscvAttributes = std::make_unique<RISCVAttributesSection>(); 881 auto &merged = static_cast<RISCVAttributesSection &>(*in.riscvAttributes); 882 883 // Collect all tags values from attributes section. 884 const auto &attributesTags = RISCVAttrs::getRISCVAttributeTags(); 885 for (const InputSectionBase *sec : sections) { 886 RISCVAttributeParser parser; 887 if (Error e = parser.parse(sec->content(), support::little)) 888 warn(toString(sec) + ": " + llvm::toString(std::move(e))); 889 for (const auto &tag : attributesTags) { 890 switch (RISCVAttrs::AttrType(tag.attr)) { 891 // Integer attributes. 892 case RISCVAttrs::STACK_ALIGN: 893 if (auto i = parser.getAttributeValue(tag.attr)) { 894 auto r = merged.intAttr.try_emplace(tag.attr, *i); 895 if (r.second) { 896 firstStackAlign = sec; 897 firstStackAlignValue = *i; 898 } else if (r.first->second != *i) { 899 errorOrWarn(toString(sec) + " has stack_align=" + Twine(*i) + 900 " but " + toString(firstStackAlign) + 901 " has stack_align=" + Twine(firstStackAlignValue)); 902 } 903 } 904 continue; 905 case RISCVAttrs::UNALIGNED_ACCESS: 906 if (auto i = parser.getAttributeValue(tag.attr)) 907 merged.intAttr[tag.attr] |= *i; 908 continue; 909 910 // String attributes. 911 case RISCVAttrs::ARCH: 912 if (auto s = parser.getAttributeString(tag.attr)) { 913 hasArch = true; 914 mergeArch(exts, xlen, sec, *s); 915 } 916 continue; 917 918 // Attributes which use the default handling. 919 case RISCVAttrs::PRIV_SPEC: 920 case RISCVAttrs::PRIV_SPEC_MINOR: 921 case RISCVAttrs::PRIV_SPEC_REVISION: 922 break; 923 } 924 925 // Fallback for deprecated priv_spec* and other unknown attributes: retain 926 // the attribute if all input sections agree on the value. GNU ld uses 0 927 // and empty strings as default values which are not dumped to the output. 928 // TODO Adjust after resolution to 929 // https://github.com/riscv-non-isa/riscv-elf-psabi-doc/issues/352 930 if (tag.attr % 2 == 0) { 931 if (auto i = parser.getAttributeValue(tag.attr)) { 932 auto r = merged.intAttr.try_emplace(tag.attr, *i); 933 if (!r.second && r.first->second != *i) 934 r.first->second = 0; 935 } 936 } else if (auto s = parser.getAttributeString(tag.attr)) { 937 auto r = merged.strAttr.try_emplace(tag.attr, *s); 938 if (!r.second && r.first->second != *s) 939 r.first->second = {}; 940 } 941 } 942 } 943 944 if (hasArch) { 945 if (auto result = RISCVISAInfo::postProcessAndChecking( 946 std::make_unique<RISCVISAInfo>(xlen, exts))) { 947 merged.strAttr.try_emplace(RISCVAttrs::ARCH, 948 saver().save((*result)->toString())); 949 } else { 950 errorOrWarn(llvm::toString(result.takeError())); 951 } 952 } 953 954 // The total size of headers: format-version [ <section-length> "vendor-name" 955 // [ <file-tag> <size>. 956 size_t size = 5 + merged.vendor.size() + 1 + 5; 957 for (auto &attr : merged.intAttr) 958 if (attr.second != 0) 959 size += getULEB128Size(attr.first) + getULEB128Size(attr.second); 960 for (auto &attr : merged.strAttr) 961 if (!attr.second.empty()) 962 size += getULEB128Size(attr.first) + attr.second.size() + 1; 963 merged.size = size; 964 return &merged; 965 } 966 967 void RISCVAttributesSection::writeTo(uint8_t *buf) { 968 const size_t size = getSize(); 969 uint8_t *const end = buf + size; 970 *buf = ELFAttrs::Format_Version; 971 write32(buf + 1, size - 1); 972 buf += 5; 973 974 memcpy(buf, vendor.data(), vendor.size()); 975 buf += vendor.size() + 1; 976 977 *buf = ELFAttrs::File; 978 write32(buf + 1, end - buf); 979 buf += 5; 980 981 for (auto &attr : intAttr) { 982 if (attr.second == 0) 983 continue; 984 buf += encodeULEB128(attr.first, buf); 985 buf += encodeULEB128(attr.second, buf); 986 } 987 for (auto &attr : strAttr) { 988 if (attr.second.empty()) 989 continue; 990 buf += encodeULEB128(attr.first, buf); 991 memcpy(buf, attr.second.data(), attr.second.size()); 992 buf += attr.second.size() + 1; 993 } 994 } 995 996 void elf::mergeRISCVAttributesSections() { 997 // Find the first input SHT_RISCV_ATTRIBUTES; return if not found. 998 size_t place = 999 llvm::find_if(ctx.inputSections, 1000 [](auto *s) { return s->type == SHT_RISCV_ATTRIBUTES; }) - 1001 ctx.inputSections.begin(); 1002 if (place == ctx.inputSections.size()) 1003 return; 1004 1005 // Extract all SHT_RISCV_ATTRIBUTES sections into `sections`. 1006 SmallVector<InputSectionBase *, 0> sections; 1007 llvm::erase_if(ctx.inputSections, [&](InputSectionBase *s) { 1008 if (s->type != SHT_RISCV_ATTRIBUTES) 1009 return false; 1010 sections.push_back(s); 1011 return true; 1012 }); 1013 1014 // Add the merged section. 1015 ctx.inputSections.insert(ctx.inputSections.begin() + place, 1016 mergeAttributesSection(sections)); 1017 } 1018 1019 TargetInfo *elf::getRISCVTargetInfo() { 1020 static RISCV target; 1021 return ⌖ 1022 } 1023