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" 10480093f4SDimitry Andric #include "Symbols.h" 110b57cec5SDimitry Andric #include "SyntheticSections.h" 120b57cec5SDimitry Andric #include "Target.h" 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric using namespace llvm; 150b57cec5SDimitry Andric using namespace llvm::object; 160b57cec5SDimitry Andric using namespace llvm::support::endian; 170b57cec5SDimitry Andric using namespace llvm::ELF; 185ffd83dbSDimitry Andric using namespace lld; 195ffd83dbSDimitry Andric using namespace lld::elf; 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric namespace { 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric class RISCV final : public TargetInfo { 240b57cec5SDimitry Andric public: 250b57cec5SDimitry Andric RISCV(); 260b57cec5SDimitry Andric uint32_t calcEFlags() const override; 27*fe6060f1SDimitry Andric int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override; 280b57cec5SDimitry Andric void writeGotHeader(uint8_t *buf) const override; 290b57cec5SDimitry Andric void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 30*fe6060f1SDimitry Andric void writeIgotPlt(uint8_t *buf, const Symbol &s) const override; 310b57cec5SDimitry Andric void writePltHeader(uint8_t *buf) const override; 32480093f4SDimitry Andric void writePlt(uint8_t *buf, const Symbol &sym, 33480093f4SDimitry Andric uint64_t pltEntryAddr) const override; 340b57cec5SDimitry Andric RelType getDynRel(RelType type) const override; 350b57cec5SDimitry Andric RelExpr getRelExpr(RelType type, const Symbol &s, 360b57cec5SDimitry Andric const uint8_t *loc) const override; 375ffd83dbSDimitry Andric void relocate(uint8_t *loc, const Relocation &rel, 385ffd83dbSDimitry Andric uint64_t val) const override; 390b57cec5SDimitry Andric }; 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric } // end anonymous namespace 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric const uint64_t dtpOffset = 0x800; 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric enum Op { 460b57cec5SDimitry Andric ADDI = 0x13, 470b57cec5SDimitry Andric AUIPC = 0x17, 480b57cec5SDimitry Andric JALR = 0x67, 490b57cec5SDimitry Andric LD = 0x3003, 500b57cec5SDimitry Andric LW = 0x2003, 510b57cec5SDimitry Andric SRLI = 0x5013, 520b57cec5SDimitry Andric SUB = 0x40000033, 530b57cec5SDimitry Andric }; 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric enum Reg { 560b57cec5SDimitry Andric X_RA = 1, 570b57cec5SDimitry Andric X_T0 = 5, 580b57cec5SDimitry Andric X_T1 = 6, 590b57cec5SDimitry Andric X_T2 = 7, 600b57cec5SDimitry Andric X_T3 = 28, 610b57cec5SDimitry Andric }; 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric static uint32_t hi20(uint32_t val) { return (val + 0x800) >> 12; } 640b57cec5SDimitry Andric static uint32_t lo12(uint32_t val) { return val & 4095; } 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric static uint32_t itype(uint32_t op, uint32_t rd, uint32_t rs1, uint32_t imm) { 670b57cec5SDimitry Andric return op | (rd << 7) | (rs1 << 15) | (imm << 20); 680b57cec5SDimitry Andric } 690b57cec5SDimitry Andric static uint32_t rtype(uint32_t op, uint32_t rd, uint32_t rs1, uint32_t rs2) { 700b57cec5SDimitry Andric return op | (rd << 7) | (rs1 << 15) | (rs2 << 20); 710b57cec5SDimitry Andric } 720b57cec5SDimitry Andric static uint32_t utype(uint32_t op, uint32_t rd, uint32_t imm) { 730b57cec5SDimitry Andric return op | (rd << 7) | (imm << 12); 740b57cec5SDimitry Andric } 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric RISCV::RISCV() { 770b57cec5SDimitry Andric copyRel = R_RISCV_COPY; 780b57cec5SDimitry Andric noneRel = R_RISCV_NONE; 790b57cec5SDimitry Andric pltRel = R_RISCV_JUMP_SLOT; 800b57cec5SDimitry Andric relativeRel = R_RISCV_RELATIVE; 815ffd83dbSDimitry Andric iRelativeRel = R_RISCV_IRELATIVE; 820b57cec5SDimitry Andric if (config->is64) { 830b57cec5SDimitry Andric symbolicRel = R_RISCV_64; 840b57cec5SDimitry Andric tlsModuleIndexRel = R_RISCV_TLS_DTPMOD64; 850b57cec5SDimitry Andric tlsOffsetRel = R_RISCV_TLS_DTPREL64; 860b57cec5SDimitry Andric tlsGotRel = R_RISCV_TLS_TPREL64; 870b57cec5SDimitry Andric } else { 880b57cec5SDimitry Andric symbolicRel = R_RISCV_32; 890b57cec5SDimitry Andric tlsModuleIndexRel = R_RISCV_TLS_DTPMOD32; 900b57cec5SDimitry Andric tlsOffsetRel = R_RISCV_TLS_DTPREL32; 910b57cec5SDimitry Andric tlsGotRel = R_RISCV_TLS_TPREL32; 920b57cec5SDimitry Andric } 930b57cec5SDimitry Andric gotRel = symbolicRel; 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric // .got[0] = _DYNAMIC 960b57cec5SDimitry Andric gotBaseSymInGotPlt = false; 970b57cec5SDimitry Andric gotHeaderEntriesNum = 1; 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric // .got.plt[0] = _dl_runtime_resolve, .got.plt[1] = link_map 1000b57cec5SDimitry Andric gotPltHeaderEntriesNum = 2; 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric pltHeaderSize = 32; 103480093f4SDimitry Andric pltEntrySize = 16; 104480093f4SDimitry Andric ipltEntrySize = 16; 1050b57cec5SDimitry Andric } 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric static uint32_t getEFlags(InputFile *f) { 1080b57cec5SDimitry Andric if (config->is64) 109e8d8bef9SDimitry Andric return cast<ObjFile<ELF64LE>>(f)->getObj().getHeader().e_flags; 110e8d8bef9SDimitry Andric return cast<ObjFile<ELF32LE>>(f)->getObj().getHeader().e_flags; 1110b57cec5SDimitry Andric } 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric uint32_t RISCV::calcEFlags() const { 114a1517e11SDimitry Andric // If there are only binary input files (from -b binary), use a 115a1517e11SDimitry Andric // value of 0 for the ELF header flags. 116a1517e11SDimitry Andric if (objectFiles.empty()) 117a1517e11SDimitry Andric return 0; 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric uint32_t target = getEFlags(objectFiles.front()); 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric for (InputFile *f : objectFiles) { 1220b57cec5SDimitry Andric uint32_t eflags = getEFlags(f); 1230b57cec5SDimitry Andric if (eflags & EF_RISCV_RVC) 1240b57cec5SDimitry Andric target |= EF_RISCV_RVC; 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric if ((eflags & EF_RISCV_FLOAT_ABI) != (target & EF_RISCV_FLOAT_ABI)) 1270b57cec5SDimitry Andric error(toString(f) + 1280b57cec5SDimitry Andric ": cannot link object files with different floating-point ABI"); 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric if ((eflags & EF_RISCV_RVE) != (target & EF_RISCV_RVE)) 1310b57cec5SDimitry Andric error(toString(f) + 1320b57cec5SDimitry Andric ": cannot link object files with different EF_RISCV_RVE"); 1330b57cec5SDimitry Andric } 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric return target; 1360b57cec5SDimitry Andric } 1370b57cec5SDimitry Andric 138*fe6060f1SDimitry Andric int64_t RISCV::getImplicitAddend(const uint8_t *buf, RelType type) const { 139*fe6060f1SDimitry Andric switch (type) { 140*fe6060f1SDimitry Andric default: 141*fe6060f1SDimitry Andric internalLinkerError(getErrorLocation(buf), 142*fe6060f1SDimitry Andric "cannot read addend for relocation " + toString(type)); 143*fe6060f1SDimitry Andric return 0; 144*fe6060f1SDimitry Andric case R_RISCV_32: 145*fe6060f1SDimitry Andric case R_RISCV_TLS_DTPMOD32: 146*fe6060f1SDimitry Andric case R_RISCV_TLS_DTPREL32: 147*fe6060f1SDimitry Andric return SignExtend64<32>(read32le(buf)); 148*fe6060f1SDimitry Andric case R_RISCV_64: 149*fe6060f1SDimitry Andric return read64le(buf); 150*fe6060f1SDimitry Andric case R_RISCV_RELATIVE: 151*fe6060f1SDimitry Andric case R_RISCV_IRELATIVE: 152*fe6060f1SDimitry Andric return config->is64 ? read64le(buf) : read32le(buf); 153*fe6060f1SDimitry Andric case R_RISCV_NONE: 154*fe6060f1SDimitry Andric case R_RISCV_JUMP_SLOT: 155*fe6060f1SDimitry Andric // These relocations are defined as not having an implicit addend. 156*fe6060f1SDimitry Andric return 0; 157*fe6060f1SDimitry Andric } 158*fe6060f1SDimitry Andric } 159*fe6060f1SDimitry Andric 1600b57cec5SDimitry Andric void RISCV::writeGotHeader(uint8_t *buf) const { 1610b57cec5SDimitry Andric if (config->is64) 1620b57cec5SDimitry Andric write64le(buf, mainPart->dynamic->getVA()); 1630b57cec5SDimitry Andric else 1640b57cec5SDimitry Andric write32le(buf, mainPart->dynamic->getVA()); 1650b57cec5SDimitry Andric } 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric void RISCV::writeGotPlt(uint8_t *buf, const Symbol &s) const { 1680b57cec5SDimitry Andric if (config->is64) 1690b57cec5SDimitry Andric write64le(buf, in.plt->getVA()); 1700b57cec5SDimitry Andric else 1710b57cec5SDimitry Andric write32le(buf, in.plt->getVA()); 1720b57cec5SDimitry Andric } 1730b57cec5SDimitry Andric 174*fe6060f1SDimitry Andric void RISCV::writeIgotPlt(uint8_t *buf, const Symbol &s) const { 175*fe6060f1SDimitry Andric if (config->writeAddends) { 176*fe6060f1SDimitry Andric if (config->is64) 177*fe6060f1SDimitry Andric write64le(buf, s.getVA()); 178*fe6060f1SDimitry Andric else 179*fe6060f1SDimitry Andric write32le(buf, s.getVA()); 180*fe6060f1SDimitry Andric } 181*fe6060f1SDimitry Andric } 182*fe6060f1SDimitry Andric 1830b57cec5SDimitry Andric void RISCV::writePltHeader(uint8_t *buf) const { 1840b57cec5SDimitry Andric // 1: auipc t2, %pcrel_hi(.got.plt) 1850b57cec5SDimitry Andric // sub t1, t1, t3 1860b57cec5SDimitry Andric // l[wd] t3, %pcrel_lo(1b)(t2); t3 = _dl_runtime_resolve 1870b57cec5SDimitry Andric // addi t1, t1, -pltHeaderSize-12; t1 = &.plt[i] - &.plt[0] 1880b57cec5SDimitry Andric // addi t0, t2, %pcrel_lo(1b) 1890b57cec5SDimitry Andric // srli t1, t1, (rv64?1:2); t1 = &.got.plt[i] - &.got.plt[0] 1900b57cec5SDimitry Andric // l[wd] t0, Wordsize(t0); t0 = link_map 1910b57cec5SDimitry Andric // jr t3 1920b57cec5SDimitry Andric uint32_t offset = in.gotPlt->getVA() - in.plt->getVA(); 1930b57cec5SDimitry Andric uint32_t load = config->is64 ? LD : LW; 1940b57cec5SDimitry Andric write32le(buf + 0, utype(AUIPC, X_T2, hi20(offset))); 1950b57cec5SDimitry Andric write32le(buf + 4, rtype(SUB, X_T1, X_T1, X_T3)); 1960b57cec5SDimitry Andric write32le(buf + 8, itype(load, X_T3, X_T2, lo12(offset))); 1970b57cec5SDimitry Andric write32le(buf + 12, itype(ADDI, X_T1, X_T1, -target->pltHeaderSize - 12)); 1980b57cec5SDimitry Andric write32le(buf + 16, itype(ADDI, X_T0, X_T2, lo12(offset))); 1990b57cec5SDimitry Andric write32le(buf + 20, itype(SRLI, X_T1, X_T1, config->is64 ? 1 : 2)); 2000b57cec5SDimitry Andric write32le(buf + 24, itype(load, X_T0, X_T0, config->wordsize)); 2010b57cec5SDimitry Andric write32le(buf + 28, itype(JALR, 0, X_T3, 0)); 2020b57cec5SDimitry Andric } 2030b57cec5SDimitry Andric 204480093f4SDimitry Andric void RISCV::writePlt(uint8_t *buf, const Symbol &sym, 205480093f4SDimitry Andric uint64_t pltEntryAddr) const { 2060b57cec5SDimitry Andric // 1: auipc t3, %pcrel_hi(f@.got.plt) 2070b57cec5SDimitry Andric // l[wd] t3, %pcrel_lo(1b)(t3) 2080b57cec5SDimitry Andric // jalr t1, t3 2090b57cec5SDimitry Andric // nop 210480093f4SDimitry Andric uint32_t offset = sym.getGotPltVA() - pltEntryAddr; 2110b57cec5SDimitry Andric write32le(buf + 0, utype(AUIPC, X_T3, hi20(offset))); 2120b57cec5SDimitry Andric write32le(buf + 4, itype(config->is64 ? LD : LW, X_T3, X_T3, lo12(offset))); 2130b57cec5SDimitry Andric write32le(buf + 8, itype(JALR, X_T1, X_T3, 0)); 2140b57cec5SDimitry Andric write32le(buf + 12, itype(ADDI, 0, 0, 0)); 2150b57cec5SDimitry Andric } 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric RelType RISCV::getDynRel(RelType type) const { 2180b57cec5SDimitry Andric return type == target->symbolicRel ? type 2190b57cec5SDimitry Andric : static_cast<RelType>(R_RISCV_NONE); 2200b57cec5SDimitry Andric } 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric RelExpr RISCV::getRelExpr(const RelType type, const Symbol &s, 2230b57cec5SDimitry Andric const uint8_t *loc) const { 2240b57cec5SDimitry Andric switch (type) { 225480093f4SDimitry Andric case R_RISCV_NONE: 226480093f4SDimitry Andric return R_NONE; 227480093f4SDimitry Andric case R_RISCV_32: 228480093f4SDimitry Andric case R_RISCV_64: 229480093f4SDimitry Andric case R_RISCV_HI20: 230480093f4SDimitry Andric case R_RISCV_LO12_I: 231480093f4SDimitry Andric case R_RISCV_LO12_S: 232480093f4SDimitry Andric case R_RISCV_RVC_LUI: 233480093f4SDimitry Andric return R_ABS; 2340b57cec5SDimitry Andric case R_RISCV_ADD8: 2350b57cec5SDimitry Andric case R_RISCV_ADD16: 2360b57cec5SDimitry Andric case R_RISCV_ADD32: 2370b57cec5SDimitry Andric case R_RISCV_ADD64: 2380b57cec5SDimitry Andric case R_RISCV_SET6: 2390b57cec5SDimitry Andric case R_RISCV_SET8: 2400b57cec5SDimitry Andric case R_RISCV_SET16: 2410b57cec5SDimitry Andric case R_RISCV_SET32: 2420b57cec5SDimitry Andric case R_RISCV_SUB6: 2430b57cec5SDimitry Andric case R_RISCV_SUB8: 2440b57cec5SDimitry Andric case R_RISCV_SUB16: 2450b57cec5SDimitry Andric case R_RISCV_SUB32: 2460b57cec5SDimitry Andric case R_RISCV_SUB64: 2470b57cec5SDimitry Andric return R_RISCV_ADD; 2480b57cec5SDimitry Andric case R_RISCV_JAL: 2490b57cec5SDimitry Andric case R_RISCV_BRANCH: 2500b57cec5SDimitry Andric case R_RISCV_PCREL_HI20: 2510b57cec5SDimitry Andric case R_RISCV_RVC_BRANCH: 2520b57cec5SDimitry Andric case R_RISCV_RVC_JUMP: 2530b57cec5SDimitry Andric case R_RISCV_32_PCREL: 2540b57cec5SDimitry Andric return R_PC; 2550b57cec5SDimitry Andric case R_RISCV_CALL: 2560b57cec5SDimitry Andric case R_RISCV_CALL_PLT: 2570b57cec5SDimitry Andric return R_PLT_PC; 2580b57cec5SDimitry Andric case R_RISCV_GOT_HI20: 2590b57cec5SDimitry Andric return R_GOT_PC; 2600b57cec5SDimitry Andric case R_RISCV_PCREL_LO12_I: 2610b57cec5SDimitry Andric case R_RISCV_PCREL_LO12_S: 2620b57cec5SDimitry Andric return R_RISCV_PC_INDIRECT; 2630b57cec5SDimitry Andric case R_RISCV_TLS_GD_HI20: 2640b57cec5SDimitry Andric return R_TLSGD_PC; 2650b57cec5SDimitry Andric case R_RISCV_TLS_GOT_HI20: 2660b57cec5SDimitry Andric config->hasStaticTlsModel = true; 2670b57cec5SDimitry Andric return R_GOT_PC; 2680b57cec5SDimitry Andric case R_RISCV_TPREL_HI20: 2690b57cec5SDimitry Andric case R_RISCV_TPREL_LO12_I: 2700b57cec5SDimitry Andric case R_RISCV_TPREL_LO12_S: 271e8d8bef9SDimitry Andric return R_TPREL; 2720b57cec5SDimitry Andric case R_RISCV_RELAX: 2730b57cec5SDimitry Andric case R_RISCV_TPREL_ADD: 274480093f4SDimitry Andric return R_NONE; 27555e4f9d5SDimitry Andric case R_RISCV_ALIGN: 27655e4f9d5SDimitry Andric // Not just a hint; always padded to the worst-case number of NOPs, so may 27755e4f9d5SDimitry Andric // not currently be aligned, and without linker relaxation support we can't 27855e4f9d5SDimitry Andric // delete NOPs to realign. 27955e4f9d5SDimitry Andric errorOrWarn(getErrorLocation(loc) + "relocation R_RISCV_ALIGN requires " 28055e4f9d5SDimitry Andric "unimplemented linker relaxation; recompile with -mno-relax"); 28155e4f9d5SDimitry Andric return R_NONE; 2820b57cec5SDimitry Andric default: 283480093f4SDimitry Andric error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) + 284480093f4SDimitry Andric ") against symbol " + toString(s)); 285480093f4SDimitry Andric return R_NONE; 2860b57cec5SDimitry Andric } 2870b57cec5SDimitry Andric } 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric // Extract bits V[Begin:End], where range is inclusive, and Begin must be < 63. 2900b57cec5SDimitry Andric static uint32_t extractBits(uint64_t v, uint32_t begin, uint32_t end) { 2910b57cec5SDimitry Andric return (v & ((1ULL << (begin + 1)) - 1)) >> end; 2920b57cec5SDimitry Andric } 2930b57cec5SDimitry Andric 2945ffd83dbSDimitry Andric void RISCV::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const { 2950b57cec5SDimitry Andric const unsigned bits = config->wordsize * 8; 2960b57cec5SDimitry Andric 2975ffd83dbSDimitry Andric switch (rel.type) { 2980b57cec5SDimitry Andric case R_RISCV_32: 2990b57cec5SDimitry Andric write32le(loc, val); 3000b57cec5SDimitry Andric return; 3010b57cec5SDimitry Andric case R_RISCV_64: 3020b57cec5SDimitry Andric write64le(loc, val); 3030b57cec5SDimitry Andric return; 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric case R_RISCV_RVC_BRANCH: { 3065ffd83dbSDimitry Andric checkInt(loc, static_cast<int64_t>(val) >> 1, 8, rel); 3075ffd83dbSDimitry Andric checkAlignment(loc, val, 2, rel); 3080b57cec5SDimitry Andric uint16_t insn = read16le(loc) & 0xE383; 3090b57cec5SDimitry Andric uint16_t imm8 = extractBits(val, 8, 8) << 12; 3100b57cec5SDimitry Andric uint16_t imm4_3 = extractBits(val, 4, 3) << 10; 3110b57cec5SDimitry Andric uint16_t imm7_6 = extractBits(val, 7, 6) << 5; 3120b57cec5SDimitry Andric uint16_t imm2_1 = extractBits(val, 2, 1) << 3; 3130b57cec5SDimitry Andric uint16_t imm5 = extractBits(val, 5, 5) << 2; 3140b57cec5SDimitry Andric insn |= imm8 | imm4_3 | imm7_6 | imm2_1 | imm5; 3150b57cec5SDimitry Andric 3160b57cec5SDimitry Andric write16le(loc, insn); 3170b57cec5SDimitry Andric return; 3180b57cec5SDimitry Andric } 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric case R_RISCV_RVC_JUMP: { 3215ffd83dbSDimitry Andric checkInt(loc, static_cast<int64_t>(val) >> 1, 11, rel); 3225ffd83dbSDimitry Andric checkAlignment(loc, val, 2, rel); 3230b57cec5SDimitry Andric uint16_t insn = read16le(loc) & 0xE003; 3240b57cec5SDimitry Andric uint16_t imm11 = extractBits(val, 11, 11) << 12; 3250b57cec5SDimitry Andric uint16_t imm4 = extractBits(val, 4, 4) << 11; 3260b57cec5SDimitry Andric uint16_t imm9_8 = extractBits(val, 9, 8) << 9; 3270b57cec5SDimitry Andric uint16_t imm10 = extractBits(val, 10, 10) << 8; 3280b57cec5SDimitry Andric uint16_t imm6 = extractBits(val, 6, 6) << 7; 3290b57cec5SDimitry Andric uint16_t imm7 = extractBits(val, 7, 7) << 6; 3300b57cec5SDimitry Andric uint16_t imm3_1 = extractBits(val, 3, 1) << 3; 3310b57cec5SDimitry Andric uint16_t imm5 = extractBits(val, 5, 5) << 2; 3320b57cec5SDimitry Andric insn |= imm11 | imm4 | imm9_8 | imm10 | imm6 | imm7 | imm3_1 | imm5; 3330b57cec5SDimitry Andric 3340b57cec5SDimitry Andric write16le(loc, insn); 3350b57cec5SDimitry Andric return; 3360b57cec5SDimitry Andric } 3370b57cec5SDimitry Andric 3380b57cec5SDimitry Andric case R_RISCV_RVC_LUI: { 3390b57cec5SDimitry Andric int64_t imm = SignExtend64(val + 0x800, bits) >> 12; 3405ffd83dbSDimitry Andric checkInt(loc, imm, 6, rel); 3410b57cec5SDimitry Andric if (imm == 0) { // `c.lui rd, 0` is illegal, convert to `c.li rd, 0` 3420b57cec5SDimitry Andric write16le(loc, (read16le(loc) & 0x0F83) | 0x4000); 3430b57cec5SDimitry Andric } else { 3440b57cec5SDimitry Andric uint16_t imm17 = extractBits(val + 0x800, 17, 17) << 12; 3450b57cec5SDimitry Andric uint16_t imm16_12 = extractBits(val + 0x800, 16, 12) << 2; 3460b57cec5SDimitry Andric write16le(loc, (read16le(loc) & 0xEF83) | imm17 | imm16_12); 3470b57cec5SDimitry Andric } 3480b57cec5SDimitry Andric return; 3490b57cec5SDimitry Andric } 3500b57cec5SDimitry Andric 3510b57cec5SDimitry Andric case R_RISCV_JAL: { 3525ffd83dbSDimitry Andric checkInt(loc, static_cast<int64_t>(val) >> 1, 20, rel); 3535ffd83dbSDimitry Andric checkAlignment(loc, val, 2, rel); 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andric uint32_t insn = read32le(loc) & 0xFFF; 3560b57cec5SDimitry Andric uint32_t imm20 = extractBits(val, 20, 20) << 31; 3570b57cec5SDimitry Andric uint32_t imm10_1 = extractBits(val, 10, 1) << 21; 3580b57cec5SDimitry Andric uint32_t imm11 = extractBits(val, 11, 11) << 20; 3590b57cec5SDimitry Andric uint32_t imm19_12 = extractBits(val, 19, 12) << 12; 3600b57cec5SDimitry Andric insn |= imm20 | imm10_1 | imm11 | imm19_12; 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric write32le(loc, insn); 3630b57cec5SDimitry Andric return; 3640b57cec5SDimitry Andric } 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric case R_RISCV_BRANCH: { 3675ffd83dbSDimitry Andric checkInt(loc, static_cast<int64_t>(val) >> 1, 12, rel); 3685ffd83dbSDimitry Andric checkAlignment(loc, val, 2, rel); 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric uint32_t insn = read32le(loc) & 0x1FFF07F; 3710b57cec5SDimitry Andric uint32_t imm12 = extractBits(val, 12, 12) << 31; 3720b57cec5SDimitry Andric uint32_t imm10_5 = extractBits(val, 10, 5) << 25; 3730b57cec5SDimitry Andric uint32_t imm4_1 = extractBits(val, 4, 1) << 8; 3740b57cec5SDimitry Andric uint32_t imm11 = extractBits(val, 11, 11) << 7; 3750b57cec5SDimitry Andric insn |= imm12 | imm10_5 | imm4_1 | imm11; 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric write32le(loc, insn); 3780b57cec5SDimitry Andric return; 3790b57cec5SDimitry Andric } 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric // auipc + jalr pair 3820b57cec5SDimitry Andric case R_RISCV_CALL: 3830b57cec5SDimitry Andric case R_RISCV_CALL_PLT: { 3840b57cec5SDimitry Andric int64_t hi = SignExtend64(val + 0x800, bits) >> 12; 3855ffd83dbSDimitry Andric checkInt(loc, hi, 20, rel); 3860b57cec5SDimitry Andric if (isInt<20>(hi)) { 3875ffd83dbSDimitry Andric relocateNoSym(loc, R_RISCV_PCREL_HI20, val); 3885ffd83dbSDimitry Andric relocateNoSym(loc + 4, R_RISCV_PCREL_LO12_I, val); 3890b57cec5SDimitry Andric } 3900b57cec5SDimitry Andric return; 3910b57cec5SDimitry Andric } 3920b57cec5SDimitry Andric 3930b57cec5SDimitry Andric case R_RISCV_GOT_HI20: 3940b57cec5SDimitry Andric case R_RISCV_PCREL_HI20: 3950b57cec5SDimitry Andric case R_RISCV_TLS_GD_HI20: 3960b57cec5SDimitry Andric case R_RISCV_TLS_GOT_HI20: 3970b57cec5SDimitry Andric case R_RISCV_TPREL_HI20: 3980b57cec5SDimitry Andric case R_RISCV_HI20: { 3990b57cec5SDimitry Andric uint64_t hi = val + 0x800; 4005ffd83dbSDimitry Andric checkInt(loc, SignExtend64(hi, bits) >> 12, 20, rel); 4010b57cec5SDimitry Andric write32le(loc, (read32le(loc) & 0xFFF) | (hi & 0xFFFFF000)); 4020b57cec5SDimitry Andric return; 4030b57cec5SDimitry Andric } 4040b57cec5SDimitry Andric 4050b57cec5SDimitry Andric case R_RISCV_PCREL_LO12_I: 4060b57cec5SDimitry Andric case R_RISCV_TPREL_LO12_I: 4070b57cec5SDimitry Andric case R_RISCV_LO12_I: { 4080b57cec5SDimitry Andric uint64_t hi = (val + 0x800) >> 12; 4090b57cec5SDimitry Andric uint64_t lo = val - (hi << 12); 4100b57cec5SDimitry Andric write32le(loc, (read32le(loc) & 0xFFFFF) | ((lo & 0xFFF) << 20)); 4110b57cec5SDimitry Andric return; 4120b57cec5SDimitry Andric } 4130b57cec5SDimitry Andric 4140b57cec5SDimitry Andric case R_RISCV_PCREL_LO12_S: 4150b57cec5SDimitry Andric case R_RISCV_TPREL_LO12_S: 4160b57cec5SDimitry Andric case R_RISCV_LO12_S: { 4170b57cec5SDimitry Andric uint64_t hi = (val + 0x800) >> 12; 4180b57cec5SDimitry Andric uint64_t lo = val - (hi << 12); 4190b57cec5SDimitry Andric uint32_t imm11_5 = extractBits(lo, 11, 5) << 25; 4200b57cec5SDimitry Andric uint32_t imm4_0 = extractBits(lo, 4, 0) << 7; 4210b57cec5SDimitry Andric write32le(loc, (read32le(loc) & 0x1FFF07F) | imm11_5 | imm4_0); 4220b57cec5SDimitry Andric return; 4230b57cec5SDimitry Andric } 4240b57cec5SDimitry Andric 4250b57cec5SDimitry Andric case R_RISCV_ADD8: 4260b57cec5SDimitry Andric *loc += val; 4270b57cec5SDimitry Andric return; 4280b57cec5SDimitry Andric case R_RISCV_ADD16: 4290b57cec5SDimitry Andric write16le(loc, read16le(loc) + val); 4300b57cec5SDimitry Andric return; 4310b57cec5SDimitry Andric case R_RISCV_ADD32: 4320b57cec5SDimitry Andric write32le(loc, read32le(loc) + val); 4330b57cec5SDimitry Andric return; 4340b57cec5SDimitry Andric case R_RISCV_ADD64: 4350b57cec5SDimitry Andric write64le(loc, read64le(loc) + val); 4360b57cec5SDimitry Andric return; 4370b57cec5SDimitry Andric case R_RISCV_SUB6: 4380b57cec5SDimitry Andric *loc = (*loc & 0xc0) | (((*loc & 0x3f) - val) & 0x3f); 4390b57cec5SDimitry Andric return; 4400b57cec5SDimitry Andric case R_RISCV_SUB8: 4410b57cec5SDimitry Andric *loc -= val; 4420b57cec5SDimitry Andric return; 4430b57cec5SDimitry Andric case R_RISCV_SUB16: 4440b57cec5SDimitry Andric write16le(loc, read16le(loc) - val); 4450b57cec5SDimitry Andric return; 4460b57cec5SDimitry Andric case R_RISCV_SUB32: 4470b57cec5SDimitry Andric write32le(loc, read32le(loc) - val); 4480b57cec5SDimitry Andric return; 4490b57cec5SDimitry Andric case R_RISCV_SUB64: 4500b57cec5SDimitry Andric write64le(loc, read64le(loc) - val); 4510b57cec5SDimitry Andric return; 4520b57cec5SDimitry Andric case R_RISCV_SET6: 4530b57cec5SDimitry Andric *loc = (*loc & 0xc0) | (val & 0x3f); 4540b57cec5SDimitry Andric return; 4550b57cec5SDimitry Andric case R_RISCV_SET8: 4560b57cec5SDimitry Andric *loc = val; 4570b57cec5SDimitry Andric return; 4580b57cec5SDimitry Andric case R_RISCV_SET16: 4590b57cec5SDimitry Andric write16le(loc, val); 4600b57cec5SDimitry Andric return; 4610b57cec5SDimitry Andric case R_RISCV_SET32: 4620b57cec5SDimitry Andric case R_RISCV_32_PCREL: 4630b57cec5SDimitry Andric write32le(loc, val); 4640b57cec5SDimitry Andric return; 4650b57cec5SDimitry Andric 4660b57cec5SDimitry Andric case R_RISCV_TLS_DTPREL32: 4670b57cec5SDimitry Andric write32le(loc, val - dtpOffset); 4680b57cec5SDimitry Andric break; 4690b57cec5SDimitry Andric case R_RISCV_TLS_DTPREL64: 4700b57cec5SDimitry Andric write64le(loc, val - dtpOffset); 4710b57cec5SDimitry Andric break; 4720b57cec5SDimitry Andric 4730b57cec5SDimitry Andric case R_RISCV_RELAX: 4740b57cec5SDimitry Andric return; // Ignored (for now) 4750b57cec5SDimitry Andric 4760b57cec5SDimitry Andric default: 477480093f4SDimitry Andric llvm_unreachable("unknown relocation"); 4780b57cec5SDimitry Andric } 4790b57cec5SDimitry Andric } 4800b57cec5SDimitry Andric 4815ffd83dbSDimitry Andric TargetInfo *elf::getRISCVTargetInfo() { 4820b57cec5SDimitry Andric static RISCV target; 4830b57cec5SDimitry Andric return ⌖ 4840b57cec5SDimitry Andric } 485