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