10b57cec5SDimitry Andric //===- RISCV.cpp ----------------------------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "InputFiles.h" 10753f127fSDimitry Andric #include "OutputSections.h" 11480093f4SDimitry Andric #include "Symbols.h" 120b57cec5SDimitry Andric #include "SyntheticSections.h" 130b57cec5SDimitry Andric #include "Target.h" 14bdd1243dSDimitry Andric #include "llvm/Support/ELFAttributes.h" 15bdd1243dSDimitry Andric #include "llvm/Support/LEB128.h" 16bdd1243dSDimitry Andric #include "llvm/Support/RISCVAttributeParser.h" 17bdd1243dSDimitry Andric #include "llvm/Support/RISCVAttributes.h" 18bdd1243dSDimitry Andric #include "llvm/Support/RISCVISAInfo.h" 19753f127fSDimitry Andric #include "llvm/Support/TimeProfiler.h" 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric using namespace llvm; 220b57cec5SDimitry Andric using namespace llvm::object; 230b57cec5SDimitry Andric using namespace llvm::support::endian; 240b57cec5SDimitry Andric using namespace llvm::ELF; 255ffd83dbSDimitry Andric using namespace lld; 265ffd83dbSDimitry Andric using namespace lld::elf; 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric namespace { 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric class RISCV final : public TargetInfo { 310b57cec5SDimitry Andric public: 320b57cec5SDimitry Andric RISCV(); 330b57cec5SDimitry Andric uint32_t calcEFlags() const override; 34fe6060f1SDimitry Andric int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override; 350b57cec5SDimitry Andric void writeGotHeader(uint8_t *buf) const override; 360b57cec5SDimitry Andric void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 37fe6060f1SDimitry Andric void writeIgotPlt(uint8_t *buf, const Symbol &s) const override; 380b57cec5SDimitry Andric void writePltHeader(uint8_t *buf) const override; 39480093f4SDimitry Andric void writePlt(uint8_t *buf, const Symbol &sym, 40480093f4SDimitry Andric uint64_t pltEntryAddr) const override; 410b57cec5SDimitry Andric RelType getDynRel(RelType type) const override; 420b57cec5SDimitry Andric RelExpr getRelExpr(RelType type, const Symbol &s, 430b57cec5SDimitry Andric const uint8_t *loc) const override; 445ffd83dbSDimitry Andric void relocate(uint8_t *loc, const Relocation &rel, 455ffd83dbSDimitry Andric uint64_t val) const override; 46753f127fSDimitry Andric bool relaxOnce(int pass) const override; 470b57cec5SDimitry Andric }; 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric } // end anonymous namespace 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric const uint64_t dtpOffset = 0x800; 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric enum Op { 540b57cec5SDimitry Andric ADDI = 0x13, 550b57cec5SDimitry Andric AUIPC = 0x17, 560b57cec5SDimitry Andric JALR = 0x67, 570b57cec5SDimitry Andric LD = 0x3003, 580b57cec5SDimitry Andric LW = 0x2003, 590b57cec5SDimitry Andric SRLI = 0x5013, 600b57cec5SDimitry Andric SUB = 0x40000033, 610b57cec5SDimitry Andric }; 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric enum Reg { 640b57cec5SDimitry Andric X_RA = 1, 65fcaf7f86SDimitry Andric X_TP = 4, 660b57cec5SDimitry Andric X_T0 = 5, 670b57cec5SDimitry Andric X_T1 = 6, 680b57cec5SDimitry Andric X_T2 = 7, 690b57cec5SDimitry Andric X_T3 = 28, 700b57cec5SDimitry Andric }; 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric static uint32_t hi20(uint32_t val) { return (val + 0x800) >> 12; } 730b57cec5SDimitry Andric static uint32_t lo12(uint32_t val) { return val & 4095; } 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric static uint32_t itype(uint32_t op, uint32_t rd, uint32_t rs1, uint32_t imm) { 760b57cec5SDimitry Andric return op | (rd << 7) | (rs1 << 15) | (imm << 20); 770b57cec5SDimitry Andric } 780b57cec5SDimitry Andric static uint32_t rtype(uint32_t op, uint32_t rd, uint32_t rs1, uint32_t rs2) { 790b57cec5SDimitry Andric return op | (rd << 7) | (rs1 << 15) | (rs2 << 20); 800b57cec5SDimitry Andric } 810b57cec5SDimitry Andric static uint32_t utype(uint32_t op, uint32_t rd, uint32_t imm) { 820b57cec5SDimitry Andric return op | (rd << 7) | (imm << 12); 830b57cec5SDimitry Andric } 840b57cec5SDimitry Andric 85fcaf7f86SDimitry Andric // Extract bits v[begin:end], where range is inclusive, and begin must be < 63. 86fcaf7f86SDimitry Andric static uint32_t extractBits(uint64_t v, uint32_t begin, uint32_t end) { 87fcaf7f86SDimitry Andric return (v & ((1ULL << (begin + 1)) - 1)) >> end; 88fcaf7f86SDimitry Andric } 89fcaf7f86SDimitry Andric 90fcaf7f86SDimitry Andric static uint32_t setLO12_I(uint32_t insn, uint32_t imm) { 91fcaf7f86SDimitry Andric return (insn & 0xfffff) | (imm << 20); 92fcaf7f86SDimitry Andric } 93fcaf7f86SDimitry Andric static uint32_t setLO12_S(uint32_t insn, uint32_t imm) { 94fcaf7f86SDimitry Andric return (insn & 0x1fff07f) | (extractBits(imm, 11, 5) << 25) | 95fcaf7f86SDimitry Andric (extractBits(imm, 4, 0) << 7); 96fcaf7f86SDimitry Andric } 97fcaf7f86SDimitry Andric 980b57cec5SDimitry Andric RISCV::RISCV() { 990b57cec5SDimitry Andric copyRel = R_RISCV_COPY; 1000b57cec5SDimitry Andric pltRel = R_RISCV_JUMP_SLOT; 1010b57cec5SDimitry Andric relativeRel = R_RISCV_RELATIVE; 1025ffd83dbSDimitry Andric iRelativeRel = R_RISCV_IRELATIVE; 1030b57cec5SDimitry Andric if (config->is64) { 1040b57cec5SDimitry Andric symbolicRel = R_RISCV_64; 1050b57cec5SDimitry Andric tlsModuleIndexRel = R_RISCV_TLS_DTPMOD64; 1060b57cec5SDimitry Andric tlsOffsetRel = R_RISCV_TLS_DTPREL64; 1070b57cec5SDimitry Andric tlsGotRel = R_RISCV_TLS_TPREL64; 1080b57cec5SDimitry Andric } else { 1090b57cec5SDimitry Andric symbolicRel = R_RISCV_32; 1100b57cec5SDimitry Andric tlsModuleIndexRel = R_RISCV_TLS_DTPMOD32; 1110b57cec5SDimitry Andric tlsOffsetRel = R_RISCV_TLS_DTPREL32; 1120b57cec5SDimitry Andric tlsGotRel = R_RISCV_TLS_TPREL32; 1130b57cec5SDimitry Andric } 1140b57cec5SDimitry Andric gotRel = symbolicRel; 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric // .got[0] = _DYNAMIC 1170b57cec5SDimitry Andric gotHeaderEntriesNum = 1; 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric // .got.plt[0] = _dl_runtime_resolve, .got.plt[1] = link_map 1200b57cec5SDimitry Andric gotPltHeaderEntriesNum = 2; 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric pltHeaderSize = 32; 123480093f4SDimitry Andric pltEntrySize = 16; 124480093f4SDimitry Andric ipltEntrySize = 16; 1250b57cec5SDimitry Andric } 1260b57cec5SDimitry Andric 1270b57cec5SDimitry Andric static uint32_t getEFlags(InputFile *f) { 1280b57cec5SDimitry Andric if (config->is64) 129e8d8bef9SDimitry Andric return cast<ObjFile<ELF64LE>>(f)->getObj().getHeader().e_flags; 130e8d8bef9SDimitry Andric return cast<ObjFile<ELF32LE>>(f)->getObj().getHeader().e_flags; 1310b57cec5SDimitry Andric } 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric uint32_t RISCV::calcEFlags() const { 134a1517e11SDimitry Andric // If there are only binary input files (from -b binary), use a 135a1517e11SDimitry Andric // value of 0 for the ELF header flags. 136bdd1243dSDimitry Andric if (ctx.objectFiles.empty()) 137a1517e11SDimitry Andric return 0; 1380b57cec5SDimitry Andric 139bdd1243dSDimitry Andric uint32_t target = getEFlags(ctx.objectFiles.front()); 1400b57cec5SDimitry Andric 141bdd1243dSDimitry Andric for (InputFile *f : ctx.objectFiles) { 1420b57cec5SDimitry Andric uint32_t eflags = getEFlags(f); 1430b57cec5SDimitry Andric if (eflags & EF_RISCV_RVC) 1440b57cec5SDimitry Andric target |= EF_RISCV_RVC; 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric if ((eflags & EF_RISCV_FLOAT_ABI) != (target & EF_RISCV_FLOAT_ABI)) 147bdd1243dSDimitry Andric error( 148bdd1243dSDimitry Andric toString(f) + 149bdd1243dSDimitry Andric ": cannot link object files with different floating-point ABI from " + 150bdd1243dSDimitry Andric toString(ctx.objectFiles[0])); 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric if ((eflags & EF_RISCV_RVE) != (target & EF_RISCV_RVE)) 1530b57cec5SDimitry Andric error(toString(f) + 1540b57cec5SDimitry Andric ": cannot link object files with different EF_RISCV_RVE"); 1550b57cec5SDimitry Andric } 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric return target; 1580b57cec5SDimitry Andric } 1590b57cec5SDimitry Andric 160fe6060f1SDimitry Andric int64_t RISCV::getImplicitAddend(const uint8_t *buf, RelType type) const { 161fe6060f1SDimitry Andric switch (type) { 162fe6060f1SDimitry Andric default: 163fe6060f1SDimitry Andric internalLinkerError(getErrorLocation(buf), 164fe6060f1SDimitry Andric "cannot read addend for relocation " + toString(type)); 165fe6060f1SDimitry Andric return 0; 166fe6060f1SDimitry Andric case R_RISCV_32: 167fe6060f1SDimitry Andric case R_RISCV_TLS_DTPMOD32: 168fe6060f1SDimitry Andric case R_RISCV_TLS_DTPREL32: 169bdd1243dSDimitry Andric case R_RISCV_TLS_TPREL32: 170fe6060f1SDimitry Andric return SignExtend64<32>(read32le(buf)); 171fe6060f1SDimitry Andric case R_RISCV_64: 172bdd1243dSDimitry Andric case R_RISCV_TLS_DTPMOD64: 173bdd1243dSDimitry Andric case R_RISCV_TLS_DTPREL64: 174bdd1243dSDimitry Andric case R_RISCV_TLS_TPREL64: 175fe6060f1SDimitry Andric return read64le(buf); 176fe6060f1SDimitry Andric case R_RISCV_RELATIVE: 177fe6060f1SDimitry Andric case R_RISCV_IRELATIVE: 178fe6060f1SDimitry Andric return config->is64 ? read64le(buf) : read32le(buf); 179fe6060f1SDimitry Andric case R_RISCV_NONE: 180fe6060f1SDimitry Andric case R_RISCV_JUMP_SLOT: 181fe6060f1SDimitry Andric // These relocations are defined as not having an implicit addend. 182fe6060f1SDimitry Andric return 0; 183fe6060f1SDimitry Andric } 184fe6060f1SDimitry Andric } 185fe6060f1SDimitry Andric 1860b57cec5SDimitry Andric void RISCV::writeGotHeader(uint8_t *buf) const { 1870b57cec5SDimitry Andric if (config->is64) 1880b57cec5SDimitry Andric write64le(buf, mainPart->dynamic->getVA()); 1890b57cec5SDimitry Andric else 1900b57cec5SDimitry Andric write32le(buf, mainPart->dynamic->getVA()); 1910b57cec5SDimitry Andric } 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric void RISCV::writeGotPlt(uint8_t *buf, const Symbol &s) const { 1940b57cec5SDimitry Andric if (config->is64) 1950b57cec5SDimitry Andric write64le(buf, in.plt->getVA()); 1960b57cec5SDimitry Andric else 1970b57cec5SDimitry Andric write32le(buf, in.plt->getVA()); 1980b57cec5SDimitry Andric } 1990b57cec5SDimitry Andric 200fe6060f1SDimitry Andric void RISCV::writeIgotPlt(uint8_t *buf, const Symbol &s) const { 201fe6060f1SDimitry Andric if (config->writeAddends) { 202fe6060f1SDimitry Andric if (config->is64) 203fe6060f1SDimitry Andric write64le(buf, s.getVA()); 204fe6060f1SDimitry Andric else 205fe6060f1SDimitry Andric write32le(buf, s.getVA()); 206fe6060f1SDimitry Andric } 207fe6060f1SDimitry Andric } 208fe6060f1SDimitry Andric 2090b57cec5SDimitry Andric void RISCV::writePltHeader(uint8_t *buf) const { 2100b57cec5SDimitry Andric // 1: auipc t2, %pcrel_hi(.got.plt) 2110b57cec5SDimitry Andric // sub t1, t1, t3 2120b57cec5SDimitry Andric // l[wd] t3, %pcrel_lo(1b)(t2); t3 = _dl_runtime_resolve 2130b57cec5SDimitry Andric // addi t1, t1, -pltHeaderSize-12; t1 = &.plt[i] - &.plt[0] 2140b57cec5SDimitry Andric // addi t0, t2, %pcrel_lo(1b) 2150b57cec5SDimitry Andric // srli t1, t1, (rv64?1:2); t1 = &.got.plt[i] - &.got.plt[0] 2160b57cec5SDimitry Andric // l[wd] t0, Wordsize(t0); t0 = link_map 2170b57cec5SDimitry Andric // jr t3 2180b57cec5SDimitry Andric uint32_t offset = in.gotPlt->getVA() - in.plt->getVA(); 2190b57cec5SDimitry Andric uint32_t load = config->is64 ? LD : LW; 2200b57cec5SDimitry Andric write32le(buf + 0, utype(AUIPC, X_T2, hi20(offset))); 2210b57cec5SDimitry Andric write32le(buf + 4, rtype(SUB, X_T1, X_T1, X_T3)); 2220b57cec5SDimitry Andric write32le(buf + 8, itype(load, X_T3, X_T2, lo12(offset))); 2230b57cec5SDimitry Andric write32le(buf + 12, itype(ADDI, X_T1, X_T1, -target->pltHeaderSize - 12)); 2240b57cec5SDimitry Andric write32le(buf + 16, itype(ADDI, X_T0, X_T2, lo12(offset))); 2250b57cec5SDimitry Andric write32le(buf + 20, itype(SRLI, X_T1, X_T1, config->is64 ? 1 : 2)); 2260b57cec5SDimitry Andric write32le(buf + 24, itype(load, X_T0, X_T0, config->wordsize)); 2270b57cec5SDimitry Andric write32le(buf + 28, itype(JALR, 0, X_T3, 0)); 2280b57cec5SDimitry Andric } 2290b57cec5SDimitry Andric 230480093f4SDimitry Andric void RISCV::writePlt(uint8_t *buf, const Symbol &sym, 231480093f4SDimitry Andric uint64_t pltEntryAddr) const { 2320b57cec5SDimitry Andric // 1: auipc t3, %pcrel_hi(f@.got.plt) 2330b57cec5SDimitry Andric // l[wd] t3, %pcrel_lo(1b)(t3) 2340b57cec5SDimitry Andric // jalr t1, t3 2350b57cec5SDimitry Andric // nop 236480093f4SDimitry Andric uint32_t offset = sym.getGotPltVA() - pltEntryAddr; 2370b57cec5SDimitry Andric write32le(buf + 0, utype(AUIPC, X_T3, hi20(offset))); 2380b57cec5SDimitry Andric write32le(buf + 4, itype(config->is64 ? LD : LW, X_T3, X_T3, lo12(offset))); 2390b57cec5SDimitry Andric write32le(buf + 8, itype(JALR, X_T1, X_T3, 0)); 2400b57cec5SDimitry Andric write32le(buf + 12, itype(ADDI, 0, 0, 0)); 2410b57cec5SDimitry Andric } 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric RelType RISCV::getDynRel(RelType type) const { 2440b57cec5SDimitry Andric return type == target->symbolicRel ? type 2450b57cec5SDimitry Andric : static_cast<RelType>(R_RISCV_NONE); 2460b57cec5SDimitry Andric } 2470b57cec5SDimitry Andric 2480b57cec5SDimitry Andric RelExpr RISCV::getRelExpr(const RelType type, const Symbol &s, 2490b57cec5SDimitry Andric const uint8_t *loc) const { 2500b57cec5SDimitry Andric switch (type) { 251480093f4SDimitry Andric case R_RISCV_NONE: 252480093f4SDimitry Andric return R_NONE; 253480093f4SDimitry Andric case R_RISCV_32: 254480093f4SDimitry Andric case R_RISCV_64: 255480093f4SDimitry Andric case R_RISCV_HI20: 256480093f4SDimitry Andric case R_RISCV_LO12_I: 257480093f4SDimitry Andric case R_RISCV_LO12_S: 258480093f4SDimitry Andric case R_RISCV_RVC_LUI: 259480093f4SDimitry Andric return R_ABS; 2600b57cec5SDimitry Andric case R_RISCV_ADD8: 2610b57cec5SDimitry Andric case R_RISCV_ADD16: 2620b57cec5SDimitry Andric case R_RISCV_ADD32: 2630b57cec5SDimitry Andric case R_RISCV_ADD64: 2640b57cec5SDimitry Andric case R_RISCV_SET6: 2650b57cec5SDimitry Andric case R_RISCV_SET8: 2660b57cec5SDimitry Andric case R_RISCV_SET16: 2670b57cec5SDimitry Andric case R_RISCV_SET32: 2680b57cec5SDimitry Andric case R_RISCV_SUB6: 2690b57cec5SDimitry Andric case R_RISCV_SUB8: 2700b57cec5SDimitry Andric case R_RISCV_SUB16: 2710b57cec5SDimitry Andric case R_RISCV_SUB32: 2720b57cec5SDimitry Andric case R_RISCV_SUB64: 2730b57cec5SDimitry Andric return R_RISCV_ADD; 2740b57cec5SDimitry Andric case R_RISCV_JAL: 2750b57cec5SDimitry Andric case R_RISCV_BRANCH: 2760b57cec5SDimitry Andric case R_RISCV_PCREL_HI20: 2770b57cec5SDimitry Andric case R_RISCV_RVC_BRANCH: 2780b57cec5SDimitry Andric case R_RISCV_RVC_JUMP: 2790b57cec5SDimitry Andric case R_RISCV_32_PCREL: 2800b57cec5SDimitry Andric return R_PC; 2810b57cec5SDimitry Andric case R_RISCV_CALL: 2820b57cec5SDimitry Andric case R_RISCV_CALL_PLT: 2830b57cec5SDimitry Andric return R_PLT_PC; 2840b57cec5SDimitry Andric case R_RISCV_GOT_HI20: 2850b57cec5SDimitry Andric return R_GOT_PC; 2860b57cec5SDimitry Andric case R_RISCV_PCREL_LO12_I: 2870b57cec5SDimitry Andric case R_RISCV_PCREL_LO12_S: 2880b57cec5SDimitry Andric return R_RISCV_PC_INDIRECT; 2890b57cec5SDimitry Andric case R_RISCV_TLS_GD_HI20: 2900b57cec5SDimitry Andric return R_TLSGD_PC; 2910b57cec5SDimitry Andric case R_RISCV_TLS_GOT_HI20: 2920b57cec5SDimitry Andric return R_GOT_PC; 2930b57cec5SDimitry Andric case R_RISCV_TPREL_HI20: 2940b57cec5SDimitry Andric case R_RISCV_TPREL_LO12_I: 2950b57cec5SDimitry Andric case R_RISCV_TPREL_LO12_S: 296e8d8bef9SDimitry Andric return R_TPREL; 29755e4f9d5SDimitry Andric case R_RISCV_ALIGN: 298753f127fSDimitry Andric return R_RELAX_HINT; 299fcaf7f86SDimitry Andric case R_RISCV_TPREL_ADD: 300753f127fSDimitry Andric case R_RISCV_RELAX: 301753f127fSDimitry Andric return config->relax ? R_RELAX_HINT : R_NONE; 3020b57cec5SDimitry Andric default: 303480093f4SDimitry Andric error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) + 304480093f4SDimitry Andric ") against symbol " + toString(s)); 305480093f4SDimitry Andric return R_NONE; 3060b57cec5SDimitry Andric } 3070b57cec5SDimitry Andric } 3080b57cec5SDimitry Andric 3095ffd83dbSDimitry Andric void RISCV::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { 3100b57cec5SDimitry Andric const unsigned bits = config->wordsize * 8; 3110b57cec5SDimitry Andric 3125ffd83dbSDimitry Andric switch (rel.type) { 3130b57cec5SDimitry Andric case R_RISCV_32: 3140b57cec5SDimitry Andric write32le(loc, val); 3150b57cec5SDimitry Andric return; 3160b57cec5SDimitry Andric case R_RISCV_64: 3170b57cec5SDimitry Andric write64le(loc, val); 3180b57cec5SDimitry Andric return; 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric case R_RISCV_RVC_BRANCH: { 321753f127fSDimitry Andric checkInt(loc, val, 9, rel); 3225ffd83dbSDimitry Andric checkAlignment(loc, val, 2, rel); 3230b57cec5SDimitry Andric uint16_t insn = read16le(loc) & 0xE383; 3240b57cec5SDimitry Andric uint16_t imm8 = extractBits(val, 8, 8) << 12; 3250b57cec5SDimitry Andric uint16_t imm4_3 = extractBits(val, 4, 3) << 10; 3260b57cec5SDimitry Andric uint16_t imm7_6 = extractBits(val, 7, 6) << 5; 3270b57cec5SDimitry Andric uint16_t imm2_1 = extractBits(val, 2, 1) << 3; 3280b57cec5SDimitry Andric uint16_t imm5 = extractBits(val, 5, 5) << 2; 3290b57cec5SDimitry Andric insn |= imm8 | imm4_3 | imm7_6 | imm2_1 | imm5; 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric write16le(loc, insn); 3320b57cec5SDimitry Andric return; 3330b57cec5SDimitry Andric } 3340b57cec5SDimitry Andric 3350b57cec5SDimitry Andric case R_RISCV_RVC_JUMP: { 336753f127fSDimitry Andric checkInt(loc, val, 12, rel); 3375ffd83dbSDimitry Andric checkAlignment(loc, val, 2, rel); 3380b57cec5SDimitry Andric uint16_t insn = read16le(loc) & 0xE003; 3390b57cec5SDimitry Andric uint16_t imm11 = extractBits(val, 11, 11) << 12; 3400b57cec5SDimitry Andric uint16_t imm4 = extractBits(val, 4, 4) << 11; 3410b57cec5SDimitry Andric uint16_t imm9_8 = extractBits(val, 9, 8) << 9; 3420b57cec5SDimitry Andric uint16_t imm10 = extractBits(val, 10, 10) << 8; 3430b57cec5SDimitry Andric uint16_t imm6 = extractBits(val, 6, 6) << 7; 3440b57cec5SDimitry Andric uint16_t imm7 = extractBits(val, 7, 7) << 6; 3450b57cec5SDimitry Andric uint16_t imm3_1 = extractBits(val, 3, 1) << 3; 3460b57cec5SDimitry Andric uint16_t imm5 = extractBits(val, 5, 5) << 2; 3470b57cec5SDimitry Andric insn |= imm11 | imm4 | imm9_8 | imm10 | imm6 | imm7 | imm3_1 | imm5; 3480b57cec5SDimitry Andric 3490b57cec5SDimitry Andric write16le(loc, insn); 3500b57cec5SDimitry Andric return; 3510b57cec5SDimitry Andric } 3520b57cec5SDimitry Andric 3530b57cec5SDimitry Andric case R_RISCV_RVC_LUI: { 3540b57cec5SDimitry Andric int64_t imm = SignExtend64(val + 0x800, bits) >> 12; 3555ffd83dbSDimitry Andric checkInt(loc, imm, 6, rel); 3560b57cec5SDimitry Andric if (imm == 0) { // `c.lui rd, 0` is illegal, convert to `c.li rd, 0` 3570b57cec5SDimitry Andric write16le(loc, (read16le(loc) & 0x0F83) | 0x4000); 3580b57cec5SDimitry Andric } else { 3590b57cec5SDimitry Andric uint16_t imm17 = extractBits(val + 0x800, 17, 17) << 12; 3600b57cec5SDimitry Andric uint16_t imm16_12 = extractBits(val + 0x800, 16, 12) << 2; 3610b57cec5SDimitry Andric write16le(loc, (read16le(loc) & 0xEF83) | imm17 | imm16_12); 3620b57cec5SDimitry Andric } 3630b57cec5SDimitry Andric return; 3640b57cec5SDimitry Andric } 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric case R_RISCV_JAL: { 367753f127fSDimitry Andric checkInt(loc, val, 21, rel); 3685ffd83dbSDimitry Andric checkAlignment(loc, val, 2, rel); 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric uint32_t insn = read32le(loc) & 0xFFF; 3710b57cec5SDimitry Andric uint32_t imm20 = extractBits(val, 20, 20) << 31; 3720b57cec5SDimitry Andric uint32_t imm10_1 = extractBits(val, 10, 1) << 21; 3730b57cec5SDimitry Andric uint32_t imm11 = extractBits(val, 11, 11) << 20; 3740b57cec5SDimitry Andric uint32_t imm19_12 = extractBits(val, 19, 12) << 12; 3750b57cec5SDimitry Andric insn |= imm20 | imm10_1 | imm11 | imm19_12; 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric write32le(loc, insn); 3780b57cec5SDimitry Andric return; 3790b57cec5SDimitry Andric } 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric case R_RISCV_BRANCH: { 382753f127fSDimitry Andric checkInt(loc, val, 13, rel); 3835ffd83dbSDimitry Andric checkAlignment(loc, val, 2, rel); 3840b57cec5SDimitry Andric 3850b57cec5SDimitry Andric uint32_t insn = read32le(loc) & 0x1FFF07F; 3860b57cec5SDimitry Andric uint32_t imm12 = extractBits(val, 12, 12) << 31; 3870b57cec5SDimitry Andric uint32_t imm10_5 = extractBits(val, 10, 5) << 25; 3880b57cec5SDimitry Andric uint32_t imm4_1 = extractBits(val, 4, 1) << 8; 3890b57cec5SDimitry Andric uint32_t imm11 = extractBits(val, 11, 11) << 7; 3900b57cec5SDimitry Andric insn |= imm12 | imm10_5 | imm4_1 | imm11; 3910b57cec5SDimitry Andric 3920b57cec5SDimitry Andric write32le(loc, insn); 3930b57cec5SDimitry Andric return; 3940b57cec5SDimitry Andric } 3950b57cec5SDimitry Andric 3960b57cec5SDimitry Andric // auipc + jalr pair 3970b57cec5SDimitry Andric case R_RISCV_CALL: 3980b57cec5SDimitry Andric case R_RISCV_CALL_PLT: { 3990b57cec5SDimitry Andric int64_t hi = SignExtend64(val + 0x800, bits) >> 12; 4005ffd83dbSDimitry Andric checkInt(loc, hi, 20, rel); 4010b57cec5SDimitry Andric if (isInt<20>(hi)) { 4025ffd83dbSDimitry Andric relocateNoSym(loc, R_RISCV_PCREL_HI20, val); 4035ffd83dbSDimitry Andric relocateNoSym(loc + 4, R_RISCV_PCREL_LO12_I, val); 4040b57cec5SDimitry Andric } 4050b57cec5SDimitry Andric return; 4060b57cec5SDimitry Andric } 4070b57cec5SDimitry Andric 4080b57cec5SDimitry Andric case R_RISCV_GOT_HI20: 4090b57cec5SDimitry Andric case R_RISCV_PCREL_HI20: 4100b57cec5SDimitry Andric case R_RISCV_TLS_GD_HI20: 4110b57cec5SDimitry Andric case R_RISCV_TLS_GOT_HI20: 4120b57cec5SDimitry Andric case R_RISCV_TPREL_HI20: 4130b57cec5SDimitry Andric case R_RISCV_HI20: { 4140b57cec5SDimitry Andric uint64_t hi = val + 0x800; 4155ffd83dbSDimitry Andric checkInt(loc, SignExtend64(hi, bits) >> 12, 20, rel); 4160b57cec5SDimitry Andric write32le(loc, (read32le(loc) & 0xFFF) | (hi & 0xFFFFF000)); 4170b57cec5SDimitry Andric return; 4180b57cec5SDimitry Andric } 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric case R_RISCV_PCREL_LO12_I: 4210b57cec5SDimitry Andric case R_RISCV_TPREL_LO12_I: 4220b57cec5SDimitry Andric case R_RISCV_LO12_I: { 4230b57cec5SDimitry Andric uint64_t hi = (val + 0x800) >> 12; 4240b57cec5SDimitry Andric uint64_t lo = val - (hi << 12); 425fcaf7f86SDimitry Andric write32le(loc, setLO12_I(read32le(loc), lo & 0xfff)); 4260b57cec5SDimitry Andric return; 4270b57cec5SDimitry Andric } 4280b57cec5SDimitry Andric 4290b57cec5SDimitry Andric case R_RISCV_PCREL_LO12_S: 4300b57cec5SDimitry Andric case R_RISCV_TPREL_LO12_S: 4310b57cec5SDimitry Andric case R_RISCV_LO12_S: { 4320b57cec5SDimitry Andric uint64_t hi = (val + 0x800) >> 12; 4330b57cec5SDimitry Andric uint64_t lo = val - (hi << 12); 434fcaf7f86SDimitry Andric write32le(loc, setLO12_S(read32le(loc), lo)); 4350b57cec5SDimitry Andric return; 4360b57cec5SDimitry Andric } 4370b57cec5SDimitry Andric 4380b57cec5SDimitry Andric case R_RISCV_ADD8: 4390b57cec5SDimitry Andric *loc += val; 4400b57cec5SDimitry Andric return; 4410b57cec5SDimitry Andric case R_RISCV_ADD16: 4420b57cec5SDimitry Andric write16le(loc, read16le(loc) + val); 4430b57cec5SDimitry Andric return; 4440b57cec5SDimitry Andric case R_RISCV_ADD32: 4450b57cec5SDimitry Andric write32le(loc, read32le(loc) + val); 4460b57cec5SDimitry Andric return; 4470b57cec5SDimitry Andric case R_RISCV_ADD64: 4480b57cec5SDimitry Andric write64le(loc, read64le(loc) + val); 4490b57cec5SDimitry Andric return; 4500b57cec5SDimitry Andric case R_RISCV_SUB6: 4510b57cec5SDimitry Andric *loc = (*loc & 0xc0) | (((*loc & 0x3f) - val) & 0x3f); 4520b57cec5SDimitry Andric return; 4530b57cec5SDimitry Andric case R_RISCV_SUB8: 4540b57cec5SDimitry Andric *loc -= val; 4550b57cec5SDimitry Andric return; 4560b57cec5SDimitry Andric case R_RISCV_SUB16: 4570b57cec5SDimitry Andric write16le(loc, read16le(loc) - val); 4580b57cec5SDimitry Andric return; 4590b57cec5SDimitry Andric case R_RISCV_SUB32: 4600b57cec5SDimitry Andric write32le(loc, read32le(loc) - val); 4610b57cec5SDimitry Andric return; 4620b57cec5SDimitry Andric case R_RISCV_SUB64: 4630b57cec5SDimitry Andric write64le(loc, read64le(loc) - val); 4640b57cec5SDimitry Andric return; 4650b57cec5SDimitry Andric case R_RISCV_SET6: 4660b57cec5SDimitry Andric *loc = (*loc & 0xc0) | (val & 0x3f); 4670b57cec5SDimitry Andric return; 4680b57cec5SDimitry Andric case R_RISCV_SET8: 4690b57cec5SDimitry Andric *loc = val; 4700b57cec5SDimitry Andric return; 4710b57cec5SDimitry Andric case R_RISCV_SET16: 4720b57cec5SDimitry Andric write16le(loc, val); 4730b57cec5SDimitry Andric return; 4740b57cec5SDimitry Andric case R_RISCV_SET32: 4750b57cec5SDimitry Andric case R_RISCV_32_PCREL: 4760b57cec5SDimitry Andric write32le(loc, val); 4770b57cec5SDimitry Andric return; 4780b57cec5SDimitry Andric 4790b57cec5SDimitry Andric case R_RISCV_TLS_DTPREL32: 4800b57cec5SDimitry Andric write32le(loc, val - dtpOffset); 4810b57cec5SDimitry Andric break; 4820b57cec5SDimitry Andric case R_RISCV_TLS_DTPREL64: 4830b57cec5SDimitry Andric write64le(loc, val - dtpOffset); 4840b57cec5SDimitry Andric break; 4850b57cec5SDimitry Andric 4860b57cec5SDimitry Andric case R_RISCV_RELAX: 4870b57cec5SDimitry Andric return; // Ignored (for now) 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric default: 490480093f4SDimitry Andric llvm_unreachable("unknown relocation"); 4910b57cec5SDimitry Andric } 4920b57cec5SDimitry Andric } 4930b57cec5SDimitry Andric 494753f127fSDimitry Andric namespace { 495753f127fSDimitry Andric struct SymbolAnchor { 496753f127fSDimitry Andric uint64_t offset; 497753f127fSDimitry Andric Defined *d; 498753f127fSDimitry Andric bool end; // true for the anchor of st_value+st_size 499753f127fSDimitry Andric }; 500753f127fSDimitry Andric } // namespace 501753f127fSDimitry Andric 502753f127fSDimitry Andric struct elf::RISCVRelaxAux { 503753f127fSDimitry Andric // This records symbol start and end offsets which will be adjusted according 504753f127fSDimitry Andric // to the nearest relocDeltas element. 505753f127fSDimitry Andric SmallVector<SymbolAnchor, 0> anchors; 506753f127fSDimitry Andric // For relocations[i], the actual offset is r_offset - (i ? relocDeltas[i-1] : 507753f127fSDimitry Andric // 0). 508753f127fSDimitry Andric std::unique_ptr<uint32_t[]> relocDeltas; 509753f127fSDimitry Andric // For relocations[i], the actual type is relocTypes[i]. 510753f127fSDimitry Andric std::unique_ptr<RelType[]> relocTypes; 511753f127fSDimitry Andric SmallVector<uint32_t, 0> writes; 512753f127fSDimitry Andric }; 513753f127fSDimitry Andric 514753f127fSDimitry Andric static void initSymbolAnchors() { 515753f127fSDimitry Andric SmallVector<InputSection *, 0> storage; 516753f127fSDimitry Andric for (OutputSection *osec : outputSections) { 517753f127fSDimitry Andric if (!(osec->flags & SHF_EXECINSTR)) 518753f127fSDimitry Andric continue; 519753f127fSDimitry Andric for (InputSection *sec : getInputSections(*osec, storage)) { 520753f127fSDimitry Andric sec->relaxAux = make<RISCVRelaxAux>(); 521bdd1243dSDimitry Andric if (sec->relocs().size()) { 522753f127fSDimitry Andric sec->relaxAux->relocDeltas = 523bdd1243dSDimitry Andric std::make_unique<uint32_t[]>(sec->relocs().size()); 524753f127fSDimitry Andric sec->relaxAux->relocTypes = 525bdd1243dSDimitry Andric std::make_unique<RelType[]>(sec->relocs().size()); 526753f127fSDimitry Andric } 527753f127fSDimitry Andric } 528753f127fSDimitry Andric } 529753f127fSDimitry Andric // Store anchors (st_value and st_value+st_size) for symbols relative to text 530753f127fSDimitry Andric // sections. 531bdd1243dSDimitry Andric for (InputFile *file : ctx.objectFiles) 532753f127fSDimitry Andric for (Symbol *sym : file->getSymbols()) { 533753f127fSDimitry Andric auto *d = dyn_cast<Defined>(sym); 534753f127fSDimitry Andric if (!d || d->file != file) 535753f127fSDimitry Andric continue; 536753f127fSDimitry Andric if (auto *sec = dyn_cast_or_null<InputSection>(d->section)) 537753f127fSDimitry Andric if (sec->flags & SHF_EXECINSTR && sec->relaxAux) { 538753f127fSDimitry Andric // If sec is discarded, relaxAux will be nullptr. 539753f127fSDimitry Andric sec->relaxAux->anchors.push_back({d->value, d, false}); 540753f127fSDimitry Andric sec->relaxAux->anchors.push_back({d->value + d->size, d, true}); 541753f127fSDimitry Andric } 542753f127fSDimitry Andric } 543753f127fSDimitry Andric // Sort anchors by offset so that we can find the closest relocation 544753f127fSDimitry Andric // efficiently. For a zero size symbol, ensure that its start anchor precedes 545753f127fSDimitry Andric // its end anchor. For two symbols with anchors at the same offset, their 546753f127fSDimitry Andric // order does not matter. 547753f127fSDimitry Andric for (OutputSection *osec : outputSections) { 548753f127fSDimitry Andric if (!(osec->flags & SHF_EXECINSTR)) 549753f127fSDimitry Andric continue; 550753f127fSDimitry Andric for (InputSection *sec : getInputSections(*osec, storage)) { 551753f127fSDimitry Andric llvm::sort(sec->relaxAux->anchors, [](auto &a, auto &b) { 552753f127fSDimitry Andric return std::make_pair(a.offset, a.end) < 553753f127fSDimitry Andric std::make_pair(b.offset, b.end); 554753f127fSDimitry Andric }); 555753f127fSDimitry Andric } 556753f127fSDimitry Andric } 557753f127fSDimitry Andric } 558753f127fSDimitry Andric 559753f127fSDimitry Andric // Relax R_RISCV_CALL/R_RISCV_CALL_PLT auipc+jalr to c.j, c.jal, or jal. 560753f127fSDimitry Andric static void relaxCall(const InputSection &sec, size_t i, uint64_t loc, 561753f127fSDimitry Andric Relocation &r, uint32_t &remove) { 562753f127fSDimitry Andric const bool rvc = config->eflags & EF_RISCV_RVC; 563753f127fSDimitry Andric const Symbol &sym = *r.sym; 564bdd1243dSDimitry Andric const uint64_t insnPair = read64le(sec.content().data() + r.offset); 565753f127fSDimitry Andric const uint32_t rd = extractBits(insnPair, 32 + 11, 32 + 7); 566753f127fSDimitry Andric const uint64_t dest = 567753f127fSDimitry Andric (r.expr == R_PLT_PC ? sym.getPltVA() : sym.getVA()) + r.addend; 568753f127fSDimitry Andric const int64_t displace = dest - loc; 569753f127fSDimitry Andric 570753f127fSDimitry Andric if (rvc && isInt<12>(displace) && rd == 0) { 571753f127fSDimitry Andric sec.relaxAux->relocTypes[i] = R_RISCV_RVC_JUMP; 572753f127fSDimitry Andric sec.relaxAux->writes.push_back(0xa001); // c.j 573753f127fSDimitry Andric remove = 6; 574753f127fSDimitry Andric } else if (rvc && isInt<12>(displace) && rd == X_RA && 575753f127fSDimitry Andric !config->is64) { // RV32C only 576753f127fSDimitry Andric sec.relaxAux->relocTypes[i] = R_RISCV_RVC_JUMP; 577753f127fSDimitry Andric sec.relaxAux->writes.push_back(0x2001); // c.jal 578753f127fSDimitry Andric remove = 6; 579753f127fSDimitry Andric } else if (isInt<21>(displace)) { 580753f127fSDimitry Andric sec.relaxAux->relocTypes[i] = R_RISCV_JAL; 581753f127fSDimitry Andric sec.relaxAux->writes.push_back(0x6f | rd << 7); // jal 582753f127fSDimitry Andric remove = 4; 583753f127fSDimitry Andric } 584753f127fSDimitry Andric } 585753f127fSDimitry Andric 586fcaf7f86SDimitry Andric // Relax local-exec TLS when hi20 is zero. 587fcaf7f86SDimitry Andric static void relaxTlsLe(const InputSection &sec, size_t i, uint64_t loc, 588fcaf7f86SDimitry Andric Relocation &r, uint32_t &remove) { 589fcaf7f86SDimitry Andric uint64_t val = r.sym->getVA(r.addend); 590fcaf7f86SDimitry Andric if (hi20(val) != 0) 591fcaf7f86SDimitry Andric return; 592bdd1243dSDimitry Andric uint32_t insn = read32le(sec.content().data() + r.offset); 593fcaf7f86SDimitry Andric switch (r.type) { 594fcaf7f86SDimitry Andric case R_RISCV_TPREL_HI20: 595fcaf7f86SDimitry Andric case R_RISCV_TPREL_ADD: 596fcaf7f86SDimitry Andric // Remove lui rd, %tprel_hi(x) and add rd, rd, tp, %tprel_add(x). 597fcaf7f86SDimitry Andric sec.relaxAux->relocTypes[i] = R_RISCV_RELAX; 598fcaf7f86SDimitry Andric remove = 4; 599fcaf7f86SDimitry Andric break; 600fcaf7f86SDimitry Andric case R_RISCV_TPREL_LO12_I: 601fcaf7f86SDimitry Andric // addi rd, rd, %tprel_lo(x) => addi rd, tp, st_value(x) 602fcaf7f86SDimitry Andric sec.relaxAux->relocTypes[i] = R_RISCV_32; 603fcaf7f86SDimitry Andric insn = (insn & ~(31 << 15)) | (X_TP << 15); 604fcaf7f86SDimitry Andric sec.relaxAux->writes.push_back(setLO12_I(insn, val)); 605fcaf7f86SDimitry Andric break; 606fcaf7f86SDimitry Andric case R_RISCV_TPREL_LO12_S: 607fcaf7f86SDimitry Andric // sw rs, %tprel_lo(x)(rd) => sw rs, st_value(x)(rd) 608fcaf7f86SDimitry Andric sec.relaxAux->relocTypes[i] = R_RISCV_32; 609fcaf7f86SDimitry Andric insn = (insn & ~(31 << 15)) | (X_TP << 15); 610fcaf7f86SDimitry Andric sec.relaxAux->writes.push_back(setLO12_S(insn, val)); 611fcaf7f86SDimitry Andric break; 612fcaf7f86SDimitry Andric } 613fcaf7f86SDimitry Andric } 614fcaf7f86SDimitry Andric 615753f127fSDimitry Andric static bool relax(InputSection &sec) { 616753f127fSDimitry Andric const uint64_t secAddr = sec.getVA(); 617753f127fSDimitry Andric auto &aux = *sec.relaxAux; 618753f127fSDimitry Andric bool changed = false; 619753f127fSDimitry Andric 620753f127fSDimitry Andric // Get st_value delta for symbols relative to this section from the previous 621753f127fSDimitry Andric // iteration. 622753f127fSDimitry Andric DenseMap<const Defined *, uint64_t> valueDelta; 623bdd1243dSDimitry Andric ArrayRef<SymbolAnchor> sa = ArrayRef(aux.anchors); 624753f127fSDimitry Andric uint32_t delta = 0; 625bdd1243dSDimitry Andric for (auto [i, r] : llvm::enumerate(sec.relocs())) { 626bdd1243dSDimitry Andric for (; sa.size() && sa[0].offset <= r.offset; sa = sa.slice(1)) 627753f127fSDimitry Andric if (!sa[0].end) 628753f127fSDimitry Andric valueDelta[sa[0].d] = delta; 629bdd1243dSDimitry Andric delta = aux.relocDeltas[i]; 630753f127fSDimitry Andric } 631753f127fSDimitry Andric for (const SymbolAnchor &sa : sa) 632753f127fSDimitry Andric if (!sa.end) 633753f127fSDimitry Andric valueDelta[sa.d] = delta; 634bdd1243dSDimitry Andric sa = ArrayRef(aux.anchors); 635753f127fSDimitry Andric delta = 0; 636753f127fSDimitry Andric 637bdd1243dSDimitry Andric std::fill_n(aux.relocTypes.get(), sec.relocs().size(), R_RISCV_NONE); 638753f127fSDimitry Andric aux.writes.clear(); 639bdd1243dSDimitry Andric for (auto [i, r] : llvm::enumerate(sec.relocs())) { 640753f127fSDimitry Andric const uint64_t loc = secAddr + r.offset - delta; 641753f127fSDimitry Andric uint32_t &cur = aux.relocDeltas[i], remove = 0; 642753f127fSDimitry Andric switch (r.type) { 643753f127fSDimitry Andric case R_RISCV_ALIGN: { 644753f127fSDimitry Andric const uint64_t nextLoc = loc + r.addend; 645753f127fSDimitry Andric const uint64_t align = PowerOf2Ceil(r.addend + 2); 646753f127fSDimitry Andric // All bytes beyond the alignment boundary should be removed. 647753f127fSDimitry Andric remove = nextLoc - ((loc + align - 1) & -align); 648753f127fSDimitry Andric assert(static_cast<int32_t>(remove) >= 0 && 649753f127fSDimitry Andric "R_RISCV_ALIGN needs expanding the content"); 650753f127fSDimitry Andric break; 651753f127fSDimitry Andric } 652753f127fSDimitry Andric case R_RISCV_CALL: 653753f127fSDimitry Andric case R_RISCV_CALL_PLT: 654bdd1243dSDimitry Andric if (i + 1 != sec.relocs().size() && 655bdd1243dSDimitry Andric sec.relocs()[i + 1].type == R_RISCV_RELAX) 656753f127fSDimitry Andric relaxCall(sec, i, loc, r, remove); 657753f127fSDimitry Andric break; 658fcaf7f86SDimitry Andric case R_RISCV_TPREL_HI20: 659fcaf7f86SDimitry Andric case R_RISCV_TPREL_ADD: 660fcaf7f86SDimitry Andric case R_RISCV_TPREL_LO12_I: 661fcaf7f86SDimitry Andric case R_RISCV_TPREL_LO12_S: 662bdd1243dSDimitry Andric if (i + 1 != sec.relocs().size() && 663bdd1243dSDimitry Andric sec.relocs()[i + 1].type == R_RISCV_RELAX) 664fcaf7f86SDimitry Andric relaxTlsLe(sec, i, loc, r, remove); 665fcaf7f86SDimitry Andric break; 666753f127fSDimitry Andric } 667753f127fSDimitry Andric 668753f127fSDimitry Andric // For all anchors whose offsets are <= r.offset, they are preceded by 669753f127fSDimitry Andric // the previous relocation whose `relocDeltas` value equals `delta`. 670753f127fSDimitry Andric // Decrease their st_value and update their st_size. 671753f127fSDimitry Andric for (; sa.size() && sa[0].offset <= r.offset; sa = sa.slice(1)) { 672753f127fSDimitry Andric if (sa[0].end) 673753f127fSDimitry Andric sa[0].d->size = sa[0].offset - delta - sa[0].d->value; 674753f127fSDimitry Andric else 675753f127fSDimitry Andric sa[0].d->value -= delta - valueDelta.find(sa[0].d)->second; 676753f127fSDimitry Andric } 677753f127fSDimitry Andric delta += remove; 678753f127fSDimitry Andric if (delta != cur) { 679753f127fSDimitry Andric cur = delta; 680753f127fSDimitry Andric changed = true; 681753f127fSDimitry Andric } 682753f127fSDimitry Andric } 683753f127fSDimitry Andric 684753f127fSDimitry Andric for (const SymbolAnchor &a : sa) { 685753f127fSDimitry Andric if (a.end) 686753f127fSDimitry Andric a.d->size = a.offset - delta - a.d->value; 687753f127fSDimitry Andric else 688753f127fSDimitry Andric a.d->value -= delta - valueDelta.find(a.d)->second; 689753f127fSDimitry Andric } 690753f127fSDimitry Andric // Inform assignAddresses that the size has changed. 691753f127fSDimitry Andric if (!isUInt<16>(delta)) 692753f127fSDimitry Andric fatal("section size decrease is too large"); 693753f127fSDimitry Andric sec.bytesDropped = delta; 694753f127fSDimitry Andric return changed; 695753f127fSDimitry Andric } 696753f127fSDimitry Andric 697753f127fSDimitry Andric // When relaxing just R_RISCV_ALIGN, relocDeltas is usually changed only once in 698753f127fSDimitry Andric // the absence of a linker script. For call and load/store R_RISCV_RELAX, code 699753f127fSDimitry Andric // shrinkage may reduce displacement and make more relocations eligible for 700753f127fSDimitry Andric // relaxation. Code shrinkage may increase displacement to a call/load/store 701753f127fSDimitry Andric // target at a higher fixed address, invalidating an earlier relaxation. Any 702753f127fSDimitry Andric // change in section sizes can have cascading effect and require another 703753f127fSDimitry Andric // relaxation pass. 704753f127fSDimitry Andric bool RISCV::relaxOnce(int pass) const { 705753f127fSDimitry Andric llvm::TimeTraceScope timeScope("RISC-V relaxOnce"); 706753f127fSDimitry Andric if (config->relocatable) 707753f127fSDimitry Andric return false; 708753f127fSDimitry Andric 709753f127fSDimitry Andric if (pass == 0) 710753f127fSDimitry Andric initSymbolAnchors(); 711753f127fSDimitry Andric 712753f127fSDimitry Andric SmallVector<InputSection *, 0> storage; 713753f127fSDimitry Andric bool changed = false; 714753f127fSDimitry Andric for (OutputSection *osec : outputSections) { 715753f127fSDimitry Andric if (!(osec->flags & SHF_EXECINSTR)) 716753f127fSDimitry Andric continue; 717753f127fSDimitry Andric for (InputSection *sec : getInputSections(*osec, storage)) 718753f127fSDimitry Andric changed |= relax(*sec); 719753f127fSDimitry Andric } 720753f127fSDimitry Andric return changed; 721753f127fSDimitry Andric } 722753f127fSDimitry Andric 723753f127fSDimitry Andric void elf::riscvFinalizeRelax(int passes) { 724753f127fSDimitry Andric llvm::TimeTraceScope timeScope("Finalize RISC-V relaxation"); 725753f127fSDimitry Andric log("relaxation passes: " + Twine(passes)); 726753f127fSDimitry Andric SmallVector<InputSection *, 0> storage; 727753f127fSDimitry Andric for (OutputSection *osec : outputSections) { 728753f127fSDimitry Andric if (!(osec->flags & SHF_EXECINSTR)) 729753f127fSDimitry Andric continue; 730753f127fSDimitry Andric for (InputSection *sec : getInputSections(*osec, storage)) { 731753f127fSDimitry Andric RISCVRelaxAux &aux = *sec->relaxAux; 732753f127fSDimitry Andric if (!aux.relocDeltas) 733753f127fSDimitry Andric continue; 734753f127fSDimitry Andric 735bdd1243dSDimitry Andric MutableArrayRef<Relocation> rels = sec->relocs(); 736bdd1243dSDimitry Andric ArrayRef<uint8_t> old = sec->content(); 737bdd1243dSDimitry Andric size_t newSize = old.size() - aux.relocDeltas[rels.size() - 1]; 738753f127fSDimitry Andric size_t writesIdx = 0; 739753f127fSDimitry Andric uint8_t *p = context().bAlloc.Allocate<uint8_t>(newSize); 740753f127fSDimitry Andric uint64_t offset = 0; 741753f127fSDimitry Andric int64_t delta = 0; 742bdd1243dSDimitry Andric sec->content_ = p; 743bdd1243dSDimitry Andric sec->size = newSize; 744753f127fSDimitry Andric sec->bytesDropped = 0; 745753f127fSDimitry Andric 746753f127fSDimitry Andric // Update section content: remove NOPs for R_RISCV_ALIGN and rewrite 747753f127fSDimitry Andric // instructions for relaxed relocations. 748753f127fSDimitry Andric for (size_t i = 0, e = rels.size(); i != e; ++i) { 749753f127fSDimitry Andric uint32_t remove = aux.relocDeltas[i] - delta; 750753f127fSDimitry Andric delta = aux.relocDeltas[i]; 751fcaf7f86SDimitry Andric if (remove == 0 && aux.relocTypes[i] == R_RISCV_NONE) 752753f127fSDimitry Andric continue; 753753f127fSDimitry Andric 754753f127fSDimitry Andric // Copy from last location to the current relocated location. 755753f127fSDimitry Andric const Relocation &r = rels[i]; 756753f127fSDimitry Andric uint64_t size = r.offset - offset; 757753f127fSDimitry Andric memcpy(p, old.data() + offset, size); 758753f127fSDimitry Andric p += size; 759753f127fSDimitry Andric 760753f127fSDimitry Andric // For R_RISCV_ALIGN, we will place `offset` in a location (among NOPs) 7616246ae0bSDimitry Andric // to satisfy the alignment requirement. If both `remove` and r.addend 7626246ae0bSDimitry Andric // are multiples of 4, it is as if we have skipped some NOPs. Otherwise 7636246ae0bSDimitry Andric // we are in the middle of a 4-byte NOP, and we need to rewrite the NOP 7646246ae0bSDimitry Andric // sequence. 765753f127fSDimitry Andric int64_t skip = 0; 766753f127fSDimitry Andric if (r.type == R_RISCV_ALIGN) { 7676246ae0bSDimitry Andric if (remove % 4 || r.addend % 4) { 768753f127fSDimitry Andric skip = r.addend - remove; 769753f127fSDimitry Andric int64_t j = 0; 770753f127fSDimitry Andric for (; j + 4 <= skip; j += 4) 771753f127fSDimitry Andric write32le(p + j, 0x00000013); // nop 772753f127fSDimitry Andric if (j != skip) { 773753f127fSDimitry Andric assert(j + 2 == skip); 774753f127fSDimitry Andric write16le(p + j, 0x0001); // c.nop 775753f127fSDimitry Andric } 776753f127fSDimitry Andric } 777753f127fSDimitry Andric } else if (RelType newType = aux.relocTypes[i]) { 778753f127fSDimitry Andric switch (newType) { 779fcaf7f86SDimitry Andric case R_RISCV_RELAX: 780fcaf7f86SDimitry Andric // Used by relaxTlsLe to indicate the relocation is ignored. 781fcaf7f86SDimitry Andric break; 782753f127fSDimitry Andric case R_RISCV_RVC_JUMP: 783753f127fSDimitry Andric skip = 2; 784fcaf7f86SDimitry Andric write16le(p, aux.writes[writesIdx++]); 785753f127fSDimitry Andric break; 786753f127fSDimitry Andric case R_RISCV_JAL: 787753f127fSDimitry Andric skip = 4; 788fcaf7f86SDimitry Andric write32le(p, aux.writes[writesIdx++]); 789fcaf7f86SDimitry Andric break; 790fcaf7f86SDimitry Andric case R_RISCV_32: 791fcaf7f86SDimitry Andric // Used by relaxTlsLe to write a uint32_t then suppress the handling 792fcaf7f86SDimitry Andric // in relocateAlloc. 793fcaf7f86SDimitry Andric skip = 4; 794fcaf7f86SDimitry Andric write32le(p, aux.writes[writesIdx++]); 795fcaf7f86SDimitry Andric aux.relocTypes[i] = R_RISCV_NONE; 796753f127fSDimitry Andric break; 797753f127fSDimitry Andric default: 798753f127fSDimitry Andric llvm_unreachable("unsupported type"); 799753f127fSDimitry Andric } 800753f127fSDimitry Andric } 801753f127fSDimitry Andric 802753f127fSDimitry Andric p += skip; 803753f127fSDimitry Andric offset = r.offset + skip + remove; 804753f127fSDimitry Andric } 805753f127fSDimitry Andric memcpy(p, old.data() + offset, old.size() - offset); 806753f127fSDimitry Andric 807753f127fSDimitry Andric // Subtract the previous relocDeltas value from the relocation offset. 808753f127fSDimitry Andric // For a pair of R_RISCV_CALL/R_RISCV_RELAX with the same offset, decrease 809753f127fSDimitry Andric // their r_offset by the same delta. 810753f127fSDimitry Andric delta = 0; 811753f127fSDimitry Andric for (size_t i = 0, e = rels.size(); i != e;) { 812753f127fSDimitry Andric uint64_t cur = rels[i].offset; 813753f127fSDimitry Andric do { 814753f127fSDimitry Andric rels[i].offset -= delta; 815753f127fSDimitry Andric if (aux.relocTypes[i] != R_RISCV_NONE) 816753f127fSDimitry Andric rels[i].type = aux.relocTypes[i]; 817753f127fSDimitry Andric } while (++i != e && rels[i].offset == cur); 818753f127fSDimitry Andric delta = aux.relocDeltas[i - 1]; 819753f127fSDimitry Andric } 820753f127fSDimitry Andric } 821753f127fSDimitry Andric } 822753f127fSDimitry Andric } 823753f127fSDimitry Andric 824bdd1243dSDimitry Andric namespace { 825bdd1243dSDimitry Andric // Representation of the merged .riscv.attributes input sections. The psABI 826bdd1243dSDimitry Andric // specifies merge policy for attributes. E.g. if we link an object without an 827bdd1243dSDimitry Andric // extension with an object with the extension, the output Tag_RISCV_arch shall 828bdd1243dSDimitry Andric // contain the extension. Some tools like objdump parse .riscv.attributes and 829bdd1243dSDimitry Andric // disabling some instructions if the first Tag_RISCV_arch does not contain an 830bdd1243dSDimitry Andric // extension. 831bdd1243dSDimitry Andric class RISCVAttributesSection final : public SyntheticSection { 832bdd1243dSDimitry Andric public: 833bdd1243dSDimitry Andric RISCVAttributesSection() 834bdd1243dSDimitry Andric : SyntheticSection(0, SHT_RISCV_ATTRIBUTES, 1, ".riscv.attributes") {} 835bdd1243dSDimitry Andric 836bdd1243dSDimitry Andric size_t getSize() const override { return size; } 837bdd1243dSDimitry Andric void writeTo(uint8_t *buf) override; 838bdd1243dSDimitry Andric 839bdd1243dSDimitry Andric static constexpr StringRef vendor = "riscv"; 840bdd1243dSDimitry Andric DenseMap<unsigned, unsigned> intAttr; 841bdd1243dSDimitry Andric DenseMap<unsigned, StringRef> strAttr; 842bdd1243dSDimitry Andric size_t size = 0; 843bdd1243dSDimitry Andric }; 844bdd1243dSDimitry Andric } // namespace 845bdd1243dSDimitry Andric 846bdd1243dSDimitry Andric static void mergeArch(RISCVISAInfo::OrderedExtensionMap &mergedExts, 847bdd1243dSDimitry Andric unsigned &mergedXlen, const InputSectionBase *sec, 848bdd1243dSDimitry Andric StringRef s) { 849*1ac55f4cSDimitry Andric auto maybeInfo = RISCVISAInfo::parseNormalizedArchString(s); 850bdd1243dSDimitry Andric if (!maybeInfo) { 851bdd1243dSDimitry Andric errorOrWarn(toString(sec) + ": " + s + ": " + 852bdd1243dSDimitry Andric llvm::toString(maybeInfo.takeError())); 853bdd1243dSDimitry Andric return; 854bdd1243dSDimitry Andric } 855bdd1243dSDimitry Andric 856bdd1243dSDimitry Andric // Merge extensions. 857bdd1243dSDimitry Andric RISCVISAInfo &info = **maybeInfo; 858bdd1243dSDimitry Andric if (mergedExts.empty()) { 859bdd1243dSDimitry Andric mergedExts = info.getExtensions(); 860bdd1243dSDimitry Andric mergedXlen = info.getXLen(); 861bdd1243dSDimitry Andric } else { 862bdd1243dSDimitry Andric for (const auto &ext : info.getExtensions()) { 863bdd1243dSDimitry Andric if (auto it = mergedExts.find(ext.first); it != mergedExts.end()) { 864bdd1243dSDimitry Andric if (std::tie(it->second.MajorVersion, it->second.MinorVersion) >= 865bdd1243dSDimitry Andric std::tie(ext.second.MajorVersion, ext.second.MinorVersion)) 866bdd1243dSDimitry Andric continue; 867bdd1243dSDimitry Andric } 868bdd1243dSDimitry Andric mergedExts[ext.first] = ext.second; 869bdd1243dSDimitry Andric } 870bdd1243dSDimitry Andric } 871bdd1243dSDimitry Andric } 872bdd1243dSDimitry Andric 873bdd1243dSDimitry Andric static RISCVAttributesSection * 874bdd1243dSDimitry Andric mergeAttributesSection(const SmallVector<InputSectionBase *, 0> §ions) { 875bdd1243dSDimitry Andric RISCVISAInfo::OrderedExtensionMap exts; 876bdd1243dSDimitry Andric const InputSectionBase *firstStackAlign = nullptr; 877bdd1243dSDimitry Andric unsigned firstStackAlignValue = 0, xlen = 0; 878bdd1243dSDimitry Andric bool hasArch = false; 879bdd1243dSDimitry Andric 880bdd1243dSDimitry Andric in.riscvAttributes = std::make_unique<RISCVAttributesSection>(); 881bdd1243dSDimitry Andric auto &merged = static_cast<RISCVAttributesSection &>(*in.riscvAttributes); 882bdd1243dSDimitry Andric 883bdd1243dSDimitry Andric // Collect all tags values from attributes section. 884bdd1243dSDimitry Andric const auto &attributesTags = RISCVAttrs::getRISCVAttributeTags(); 885bdd1243dSDimitry Andric for (const InputSectionBase *sec : sections) { 886bdd1243dSDimitry Andric RISCVAttributeParser parser; 887bdd1243dSDimitry Andric if (Error e = parser.parse(sec->content(), support::little)) 888bdd1243dSDimitry Andric warn(toString(sec) + ": " + llvm::toString(std::move(e))); 889bdd1243dSDimitry Andric for (const auto &tag : attributesTags) { 890bdd1243dSDimitry Andric switch (RISCVAttrs::AttrType(tag.attr)) { 891bdd1243dSDimitry Andric // Integer attributes. 892bdd1243dSDimitry Andric case RISCVAttrs::STACK_ALIGN: 893bdd1243dSDimitry Andric if (auto i = parser.getAttributeValue(tag.attr)) { 894bdd1243dSDimitry Andric auto r = merged.intAttr.try_emplace(tag.attr, *i); 895bdd1243dSDimitry Andric if (r.second) { 896bdd1243dSDimitry Andric firstStackAlign = sec; 897bdd1243dSDimitry Andric firstStackAlignValue = *i; 898bdd1243dSDimitry Andric } else if (r.first->second != *i) { 899bdd1243dSDimitry Andric errorOrWarn(toString(sec) + " has stack_align=" + Twine(*i) + 900bdd1243dSDimitry Andric " but " + toString(firstStackAlign) + 901bdd1243dSDimitry Andric " has stack_align=" + Twine(firstStackAlignValue)); 902bdd1243dSDimitry Andric } 903bdd1243dSDimitry Andric } 904bdd1243dSDimitry Andric continue; 905bdd1243dSDimitry Andric case RISCVAttrs::UNALIGNED_ACCESS: 906bdd1243dSDimitry Andric if (auto i = parser.getAttributeValue(tag.attr)) 907bdd1243dSDimitry Andric merged.intAttr[tag.attr] |= *i; 908bdd1243dSDimitry Andric continue; 909bdd1243dSDimitry Andric 910bdd1243dSDimitry Andric // String attributes. 911bdd1243dSDimitry Andric case RISCVAttrs::ARCH: 912bdd1243dSDimitry Andric if (auto s = parser.getAttributeString(tag.attr)) { 913bdd1243dSDimitry Andric hasArch = true; 914bdd1243dSDimitry Andric mergeArch(exts, xlen, sec, *s); 915bdd1243dSDimitry Andric } 916bdd1243dSDimitry Andric continue; 917bdd1243dSDimitry Andric 918bdd1243dSDimitry Andric // Attributes which use the default handling. 919bdd1243dSDimitry Andric case RISCVAttrs::PRIV_SPEC: 920bdd1243dSDimitry Andric case RISCVAttrs::PRIV_SPEC_MINOR: 921bdd1243dSDimitry Andric case RISCVAttrs::PRIV_SPEC_REVISION: 922bdd1243dSDimitry Andric break; 923bdd1243dSDimitry Andric } 924bdd1243dSDimitry Andric 925bdd1243dSDimitry Andric // Fallback for deprecated priv_spec* and other unknown attributes: retain 926bdd1243dSDimitry Andric // the attribute if all input sections agree on the value. GNU ld uses 0 927bdd1243dSDimitry Andric // and empty strings as default values which are not dumped to the output. 928bdd1243dSDimitry Andric // TODO Adjust after resolution to 929bdd1243dSDimitry Andric // https://github.com/riscv-non-isa/riscv-elf-psabi-doc/issues/352 930bdd1243dSDimitry Andric if (tag.attr % 2 == 0) { 931bdd1243dSDimitry Andric if (auto i = parser.getAttributeValue(tag.attr)) { 932bdd1243dSDimitry Andric auto r = merged.intAttr.try_emplace(tag.attr, *i); 933bdd1243dSDimitry Andric if (!r.second && r.first->second != *i) 934bdd1243dSDimitry Andric r.first->second = 0; 935bdd1243dSDimitry Andric } 936bdd1243dSDimitry Andric } else if (auto s = parser.getAttributeString(tag.attr)) { 937bdd1243dSDimitry Andric auto r = merged.strAttr.try_emplace(tag.attr, *s); 938bdd1243dSDimitry Andric if (!r.second && r.first->second != *s) 939bdd1243dSDimitry Andric r.first->second = {}; 940bdd1243dSDimitry Andric } 941bdd1243dSDimitry Andric } 942bdd1243dSDimitry Andric } 943bdd1243dSDimitry Andric 944bdd1243dSDimitry Andric if (hasArch) { 945bdd1243dSDimitry Andric if (auto result = RISCVISAInfo::postProcessAndChecking( 946bdd1243dSDimitry Andric std::make_unique<RISCVISAInfo>(xlen, exts))) { 947bdd1243dSDimitry Andric merged.strAttr.try_emplace(RISCVAttrs::ARCH, 948bdd1243dSDimitry Andric saver().save((*result)->toString())); 949bdd1243dSDimitry Andric } else { 950bdd1243dSDimitry Andric errorOrWarn(llvm::toString(result.takeError())); 951bdd1243dSDimitry Andric } 952bdd1243dSDimitry Andric } 953bdd1243dSDimitry Andric 954bdd1243dSDimitry Andric // The total size of headers: format-version [ <section-length> "vendor-name" 955bdd1243dSDimitry Andric // [ <file-tag> <size>. 956bdd1243dSDimitry Andric size_t size = 5 + merged.vendor.size() + 1 + 5; 957bdd1243dSDimitry Andric for (auto &attr : merged.intAttr) 958bdd1243dSDimitry Andric if (attr.second != 0) 959bdd1243dSDimitry Andric size += getULEB128Size(attr.first) + getULEB128Size(attr.second); 960bdd1243dSDimitry Andric for (auto &attr : merged.strAttr) 961bdd1243dSDimitry Andric if (!attr.second.empty()) 962bdd1243dSDimitry Andric size += getULEB128Size(attr.first) + attr.second.size() + 1; 963bdd1243dSDimitry Andric merged.size = size; 964bdd1243dSDimitry Andric return &merged; 965bdd1243dSDimitry Andric } 966bdd1243dSDimitry Andric 967bdd1243dSDimitry Andric void RISCVAttributesSection::writeTo(uint8_t *buf) { 968bdd1243dSDimitry Andric const size_t size = getSize(); 969bdd1243dSDimitry Andric uint8_t *const end = buf + size; 970bdd1243dSDimitry Andric *buf = ELFAttrs::Format_Version; 971bdd1243dSDimitry Andric write32(buf + 1, size - 1); 972bdd1243dSDimitry Andric buf += 5; 973bdd1243dSDimitry Andric 974bdd1243dSDimitry Andric memcpy(buf, vendor.data(), vendor.size()); 975bdd1243dSDimitry Andric buf += vendor.size() + 1; 976bdd1243dSDimitry Andric 977bdd1243dSDimitry Andric *buf = ELFAttrs::File; 978bdd1243dSDimitry Andric write32(buf + 1, end - buf); 979bdd1243dSDimitry Andric buf += 5; 980bdd1243dSDimitry Andric 981bdd1243dSDimitry Andric for (auto &attr : intAttr) { 982bdd1243dSDimitry Andric if (attr.second == 0) 983bdd1243dSDimitry Andric continue; 984bdd1243dSDimitry Andric buf += encodeULEB128(attr.first, buf); 985bdd1243dSDimitry Andric buf += encodeULEB128(attr.second, buf); 986bdd1243dSDimitry Andric } 987bdd1243dSDimitry Andric for (auto &attr : strAttr) { 988bdd1243dSDimitry Andric if (attr.second.empty()) 989bdd1243dSDimitry Andric continue; 990bdd1243dSDimitry Andric buf += encodeULEB128(attr.first, buf); 991bdd1243dSDimitry Andric memcpy(buf, attr.second.data(), attr.second.size()); 992bdd1243dSDimitry Andric buf += attr.second.size() + 1; 993bdd1243dSDimitry Andric } 994bdd1243dSDimitry Andric } 995bdd1243dSDimitry Andric 996bdd1243dSDimitry Andric void elf::mergeRISCVAttributesSections() { 997bdd1243dSDimitry Andric // Find the first input SHT_RISCV_ATTRIBUTES; return if not found. 998bdd1243dSDimitry Andric size_t place = 999bdd1243dSDimitry Andric llvm::find_if(ctx.inputSections, 1000bdd1243dSDimitry Andric [](auto *s) { return s->type == SHT_RISCV_ATTRIBUTES; }) - 1001bdd1243dSDimitry Andric ctx.inputSections.begin(); 1002bdd1243dSDimitry Andric if (place == ctx.inputSections.size()) 1003bdd1243dSDimitry Andric return; 1004bdd1243dSDimitry Andric 1005bdd1243dSDimitry Andric // Extract all SHT_RISCV_ATTRIBUTES sections into `sections`. 1006bdd1243dSDimitry Andric SmallVector<InputSectionBase *, 0> sections; 1007bdd1243dSDimitry Andric llvm::erase_if(ctx.inputSections, [&](InputSectionBase *s) { 1008bdd1243dSDimitry Andric if (s->type != SHT_RISCV_ATTRIBUTES) 1009bdd1243dSDimitry Andric return false; 1010bdd1243dSDimitry Andric sections.push_back(s); 1011bdd1243dSDimitry Andric return true; 1012bdd1243dSDimitry Andric }); 1013bdd1243dSDimitry Andric 1014bdd1243dSDimitry Andric // Add the merged section. 1015bdd1243dSDimitry Andric ctx.inputSections.insert(ctx.inputSections.begin() + place, 1016bdd1243dSDimitry Andric mergeAttributesSection(sections)); 1017bdd1243dSDimitry Andric } 1018bdd1243dSDimitry Andric 10195ffd83dbSDimitry Andric TargetInfo *elf::getRISCVTargetInfo() { 10200b57cec5SDimitry Andric static RISCV target; 10210b57cec5SDimitry Andric return ⌖ 10220b57cec5SDimitry Andric } 1023