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" 100b57cec5SDimitry Andric #include "SyntheticSections.h" 110b57cec5SDimitry Andric #include "Target.h" 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric using namespace llvm; 140b57cec5SDimitry Andric using namespace llvm::object; 150b57cec5SDimitry Andric using namespace llvm::support::endian; 160b57cec5SDimitry Andric using namespace llvm::ELF; 170b57cec5SDimitry Andric using namespace lld; 180b57cec5SDimitry Andric using namespace lld::elf; 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric namespace { 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric class RISCV final : public TargetInfo { 230b57cec5SDimitry Andric public: 240b57cec5SDimitry Andric RISCV(); 250b57cec5SDimitry Andric uint32_t calcEFlags() const override; 260b57cec5SDimitry Andric void writeGotHeader(uint8_t *buf) const override; 270b57cec5SDimitry Andric void writeGotPlt(uint8_t *buf, const Symbol &s) const override; 280b57cec5SDimitry Andric void writePltHeader(uint8_t *buf) const override; 290b57cec5SDimitry Andric void writePlt(uint8_t *buf, uint64_t gotPltEntryAddr, uint64_t pltEntryAddr, 300b57cec5SDimitry Andric int32_t index, unsigned relOff) const override; 310b57cec5SDimitry Andric RelType getDynRel(RelType type) const override; 320b57cec5SDimitry Andric RelExpr getRelExpr(RelType type, const Symbol &s, 330b57cec5SDimitry Andric const uint8_t *loc) const override; 340b57cec5SDimitry Andric void relocateOne(uint8_t *loc, RelType type, uint64_t val) const override; 350b57cec5SDimitry Andric }; 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric } // end anonymous namespace 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric const uint64_t dtpOffset = 0x800; 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric enum Op { 420b57cec5SDimitry Andric ADDI = 0x13, 430b57cec5SDimitry Andric AUIPC = 0x17, 440b57cec5SDimitry Andric JALR = 0x67, 450b57cec5SDimitry Andric LD = 0x3003, 460b57cec5SDimitry Andric LW = 0x2003, 470b57cec5SDimitry Andric SRLI = 0x5013, 480b57cec5SDimitry Andric SUB = 0x40000033, 490b57cec5SDimitry Andric }; 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric enum Reg { 520b57cec5SDimitry Andric X_RA = 1, 530b57cec5SDimitry Andric X_T0 = 5, 540b57cec5SDimitry Andric X_T1 = 6, 550b57cec5SDimitry Andric X_T2 = 7, 560b57cec5SDimitry Andric X_T3 = 28, 570b57cec5SDimitry Andric }; 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric static uint32_t hi20(uint32_t val) { return (val + 0x800) >> 12; } 600b57cec5SDimitry Andric static uint32_t lo12(uint32_t val) { return val & 4095; } 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric static uint32_t itype(uint32_t op, uint32_t rd, uint32_t rs1, uint32_t imm) { 630b57cec5SDimitry Andric return op | (rd << 7) | (rs1 << 15) | (imm << 20); 640b57cec5SDimitry Andric } 650b57cec5SDimitry Andric static uint32_t rtype(uint32_t op, uint32_t rd, uint32_t rs1, uint32_t rs2) { 660b57cec5SDimitry Andric return op | (rd << 7) | (rs1 << 15) | (rs2 << 20); 670b57cec5SDimitry Andric } 680b57cec5SDimitry Andric static uint32_t utype(uint32_t op, uint32_t rd, uint32_t imm) { 690b57cec5SDimitry Andric return op | (rd << 7) | (imm << 12); 700b57cec5SDimitry Andric } 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric RISCV::RISCV() { 730b57cec5SDimitry Andric copyRel = R_RISCV_COPY; 740b57cec5SDimitry Andric noneRel = R_RISCV_NONE; 750b57cec5SDimitry Andric pltRel = R_RISCV_JUMP_SLOT; 760b57cec5SDimitry Andric relativeRel = R_RISCV_RELATIVE; 770b57cec5SDimitry Andric if (config->is64) { 780b57cec5SDimitry Andric symbolicRel = R_RISCV_64; 790b57cec5SDimitry Andric tlsModuleIndexRel = R_RISCV_TLS_DTPMOD64; 800b57cec5SDimitry Andric tlsOffsetRel = R_RISCV_TLS_DTPREL64; 810b57cec5SDimitry Andric tlsGotRel = R_RISCV_TLS_TPREL64; 820b57cec5SDimitry Andric } else { 830b57cec5SDimitry Andric symbolicRel = R_RISCV_32; 840b57cec5SDimitry Andric tlsModuleIndexRel = R_RISCV_TLS_DTPMOD32; 850b57cec5SDimitry Andric tlsOffsetRel = R_RISCV_TLS_DTPREL32; 860b57cec5SDimitry Andric tlsGotRel = R_RISCV_TLS_TPREL32; 870b57cec5SDimitry Andric } 880b57cec5SDimitry Andric gotRel = symbolicRel; 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric // .got[0] = _DYNAMIC 910b57cec5SDimitry Andric gotBaseSymInGotPlt = false; 920b57cec5SDimitry Andric gotHeaderEntriesNum = 1; 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric // .got.plt[0] = _dl_runtime_resolve, .got.plt[1] = link_map 950b57cec5SDimitry Andric gotPltHeaderEntriesNum = 2; 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric pltEntrySize = 16; 980b57cec5SDimitry Andric pltHeaderSize = 32; 990b57cec5SDimitry Andric } 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric static uint32_t getEFlags(InputFile *f) { 1020b57cec5SDimitry Andric if (config->is64) 1030b57cec5SDimitry Andric return cast<ObjFile<ELF64LE>>(f)->getObj().getHeader()->e_flags; 1040b57cec5SDimitry Andric return cast<ObjFile<ELF32LE>>(f)->getObj().getHeader()->e_flags; 1050b57cec5SDimitry Andric } 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric uint32_t RISCV::calcEFlags() const { 108*a1517e11SDimitry Andric // If there are only binary input files (from -b binary), use a 109*a1517e11SDimitry Andric // value of 0 for the ELF header flags. 110*a1517e11SDimitry Andric if (objectFiles.empty()) 111*a1517e11SDimitry Andric return 0; 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric uint32_t target = getEFlags(objectFiles.front()); 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric for (InputFile *f : objectFiles) { 1160b57cec5SDimitry Andric uint32_t eflags = getEFlags(f); 1170b57cec5SDimitry Andric if (eflags & EF_RISCV_RVC) 1180b57cec5SDimitry Andric target |= EF_RISCV_RVC; 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric if ((eflags & EF_RISCV_FLOAT_ABI) != (target & EF_RISCV_FLOAT_ABI)) 1210b57cec5SDimitry Andric error(toString(f) + 1220b57cec5SDimitry Andric ": cannot link object files with different floating-point ABI"); 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric if ((eflags & EF_RISCV_RVE) != (target & EF_RISCV_RVE)) 1250b57cec5SDimitry Andric error(toString(f) + 1260b57cec5SDimitry Andric ": cannot link object files with different EF_RISCV_RVE"); 1270b57cec5SDimitry Andric } 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric return target; 1300b57cec5SDimitry Andric } 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric void RISCV::writeGotHeader(uint8_t *buf) const { 1330b57cec5SDimitry Andric if (config->is64) 1340b57cec5SDimitry Andric write64le(buf, mainPart->dynamic->getVA()); 1350b57cec5SDimitry Andric else 1360b57cec5SDimitry Andric write32le(buf, mainPart->dynamic->getVA()); 1370b57cec5SDimitry Andric } 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric void RISCV::writeGotPlt(uint8_t *buf, const Symbol &s) const { 1400b57cec5SDimitry Andric if (config->is64) 1410b57cec5SDimitry Andric write64le(buf, in.plt->getVA()); 1420b57cec5SDimitry Andric else 1430b57cec5SDimitry Andric write32le(buf, in.plt->getVA()); 1440b57cec5SDimitry Andric } 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric void RISCV::writePltHeader(uint8_t *buf) const { 1470b57cec5SDimitry Andric // 1: auipc t2, %pcrel_hi(.got.plt) 1480b57cec5SDimitry Andric // sub t1, t1, t3 1490b57cec5SDimitry Andric // l[wd] t3, %pcrel_lo(1b)(t2); t3 = _dl_runtime_resolve 1500b57cec5SDimitry Andric // addi t1, t1, -pltHeaderSize-12; t1 = &.plt[i] - &.plt[0] 1510b57cec5SDimitry Andric // addi t0, t2, %pcrel_lo(1b) 1520b57cec5SDimitry Andric // srli t1, t1, (rv64?1:2); t1 = &.got.plt[i] - &.got.plt[0] 1530b57cec5SDimitry Andric // l[wd] t0, Wordsize(t0); t0 = link_map 1540b57cec5SDimitry Andric // jr t3 1550b57cec5SDimitry Andric uint32_t offset = in.gotPlt->getVA() - in.plt->getVA(); 1560b57cec5SDimitry Andric uint32_t load = config->is64 ? LD : LW; 1570b57cec5SDimitry Andric write32le(buf + 0, utype(AUIPC, X_T2, hi20(offset))); 1580b57cec5SDimitry Andric write32le(buf + 4, rtype(SUB, X_T1, X_T1, X_T3)); 1590b57cec5SDimitry Andric write32le(buf + 8, itype(load, X_T3, X_T2, lo12(offset))); 1600b57cec5SDimitry Andric write32le(buf + 12, itype(ADDI, X_T1, X_T1, -target->pltHeaderSize - 12)); 1610b57cec5SDimitry Andric write32le(buf + 16, itype(ADDI, X_T0, X_T2, lo12(offset))); 1620b57cec5SDimitry Andric write32le(buf + 20, itype(SRLI, X_T1, X_T1, config->is64 ? 1 : 2)); 1630b57cec5SDimitry Andric write32le(buf + 24, itype(load, X_T0, X_T0, config->wordsize)); 1640b57cec5SDimitry Andric write32le(buf + 28, itype(JALR, 0, X_T3, 0)); 1650b57cec5SDimitry Andric } 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric void RISCV::writePlt(uint8_t *buf, uint64_t gotPltEntryAddr, 1680b57cec5SDimitry Andric uint64_t pltEntryAddr, int32_t index, 1690b57cec5SDimitry Andric unsigned relOff) const { 1700b57cec5SDimitry Andric // 1: auipc t3, %pcrel_hi(f@.got.plt) 1710b57cec5SDimitry Andric // l[wd] t3, %pcrel_lo(1b)(t3) 1720b57cec5SDimitry Andric // jalr t1, t3 1730b57cec5SDimitry Andric // nop 1740b57cec5SDimitry Andric uint32_t offset = gotPltEntryAddr - pltEntryAddr; 1750b57cec5SDimitry Andric write32le(buf + 0, utype(AUIPC, X_T3, hi20(offset))); 1760b57cec5SDimitry Andric write32le(buf + 4, itype(config->is64 ? LD : LW, X_T3, X_T3, lo12(offset))); 1770b57cec5SDimitry Andric write32le(buf + 8, itype(JALR, X_T1, X_T3, 0)); 1780b57cec5SDimitry Andric write32le(buf + 12, itype(ADDI, 0, 0, 0)); 1790b57cec5SDimitry Andric } 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric RelType RISCV::getDynRel(RelType type) const { 1820b57cec5SDimitry Andric return type == target->symbolicRel ? type 1830b57cec5SDimitry Andric : static_cast<RelType>(R_RISCV_NONE); 1840b57cec5SDimitry Andric } 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric RelExpr RISCV::getRelExpr(const RelType type, const Symbol &s, 1870b57cec5SDimitry Andric const uint8_t *loc) const { 1880b57cec5SDimitry Andric switch (type) { 1890b57cec5SDimitry Andric case R_RISCV_ADD8: 1900b57cec5SDimitry Andric case R_RISCV_ADD16: 1910b57cec5SDimitry Andric case R_RISCV_ADD32: 1920b57cec5SDimitry Andric case R_RISCV_ADD64: 1930b57cec5SDimitry Andric case R_RISCV_SET6: 1940b57cec5SDimitry Andric case R_RISCV_SET8: 1950b57cec5SDimitry Andric case R_RISCV_SET16: 1960b57cec5SDimitry Andric case R_RISCV_SET32: 1970b57cec5SDimitry Andric case R_RISCV_SUB6: 1980b57cec5SDimitry Andric case R_RISCV_SUB8: 1990b57cec5SDimitry Andric case R_RISCV_SUB16: 2000b57cec5SDimitry Andric case R_RISCV_SUB32: 2010b57cec5SDimitry Andric case R_RISCV_SUB64: 2020b57cec5SDimitry Andric return R_RISCV_ADD; 2030b57cec5SDimitry Andric case R_RISCV_JAL: 2040b57cec5SDimitry Andric case R_RISCV_BRANCH: 2050b57cec5SDimitry Andric case R_RISCV_PCREL_HI20: 2060b57cec5SDimitry Andric case R_RISCV_RVC_BRANCH: 2070b57cec5SDimitry Andric case R_RISCV_RVC_JUMP: 2080b57cec5SDimitry Andric case R_RISCV_32_PCREL: 2090b57cec5SDimitry Andric return R_PC; 2100b57cec5SDimitry Andric case R_RISCV_CALL: 2110b57cec5SDimitry Andric case R_RISCV_CALL_PLT: 2120b57cec5SDimitry Andric return R_PLT_PC; 2130b57cec5SDimitry Andric case R_RISCV_GOT_HI20: 2140b57cec5SDimitry Andric return R_GOT_PC; 2150b57cec5SDimitry Andric case R_RISCV_PCREL_LO12_I: 2160b57cec5SDimitry Andric case R_RISCV_PCREL_LO12_S: 2170b57cec5SDimitry Andric return R_RISCV_PC_INDIRECT; 2180b57cec5SDimitry Andric case R_RISCV_TLS_GD_HI20: 2190b57cec5SDimitry Andric return R_TLSGD_PC; 2200b57cec5SDimitry Andric case R_RISCV_TLS_GOT_HI20: 2210b57cec5SDimitry Andric config->hasStaticTlsModel = true; 2220b57cec5SDimitry Andric return R_GOT_PC; 2230b57cec5SDimitry Andric case R_RISCV_TPREL_HI20: 2240b57cec5SDimitry Andric case R_RISCV_TPREL_LO12_I: 2250b57cec5SDimitry Andric case R_RISCV_TPREL_LO12_S: 2260b57cec5SDimitry Andric return R_TLS; 2270b57cec5SDimitry Andric case R_RISCV_RELAX: 2280b57cec5SDimitry Andric case R_RISCV_ALIGN: 2290b57cec5SDimitry Andric case R_RISCV_TPREL_ADD: 2300b57cec5SDimitry Andric return R_HINT; 2310b57cec5SDimitry Andric default: 2320b57cec5SDimitry Andric return R_ABS; 2330b57cec5SDimitry Andric } 2340b57cec5SDimitry Andric } 2350b57cec5SDimitry Andric 2360b57cec5SDimitry Andric // Extract bits V[Begin:End], where range is inclusive, and Begin must be < 63. 2370b57cec5SDimitry Andric static uint32_t extractBits(uint64_t v, uint32_t begin, uint32_t end) { 2380b57cec5SDimitry Andric return (v & ((1ULL << (begin + 1)) - 1)) >> end; 2390b57cec5SDimitry Andric } 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric void RISCV::relocateOne(uint8_t *loc, const RelType type, 2420b57cec5SDimitry Andric const uint64_t val) const { 2430b57cec5SDimitry Andric const unsigned bits = config->wordsize * 8; 2440b57cec5SDimitry Andric 2450b57cec5SDimitry Andric switch (type) { 2460b57cec5SDimitry Andric case R_RISCV_32: 2470b57cec5SDimitry Andric write32le(loc, val); 2480b57cec5SDimitry Andric return; 2490b57cec5SDimitry Andric case R_RISCV_64: 2500b57cec5SDimitry Andric write64le(loc, val); 2510b57cec5SDimitry Andric return; 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andric case R_RISCV_RVC_BRANCH: { 2540b57cec5SDimitry Andric checkInt(loc, static_cast<int64_t>(val) >> 1, 8, type); 2550b57cec5SDimitry Andric checkAlignment(loc, val, 2, type); 2560b57cec5SDimitry Andric uint16_t insn = read16le(loc) & 0xE383; 2570b57cec5SDimitry Andric uint16_t imm8 = extractBits(val, 8, 8) << 12; 2580b57cec5SDimitry Andric uint16_t imm4_3 = extractBits(val, 4, 3) << 10; 2590b57cec5SDimitry Andric uint16_t imm7_6 = extractBits(val, 7, 6) << 5; 2600b57cec5SDimitry Andric uint16_t imm2_1 = extractBits(val, 2, 1) << 3; 2610b57cec5SDimitry Andric uint16_t imm5 = extractBits(val, 5, 5) << 2; 2620b57cec5SDimitry Andric insn |= imm8 | imm4_3 | imm7_6 | imm2_1 | imm5; 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric write16le(loc, insn); 2650b57cec5SDimitry Andric return; 2660b57cec5SDimitry Andric } 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric case R_RISCV_RVC_JUMP: { 2690b57cec5SDimitry Andric checkInt(loc, static_cast<int64_t>(val) >> 1, 11, type); 2700b57cec5SDimitry Andric checkAlignment(loc, val, 2, type); 2710b57cec5SDimitry Andric uint16_t insn = read16le(loc) & 0xE003; 2720b57cec5SDimitry Andric uint16_t imm11 = extractBits(val, 11, 11) << 12; 2730b57cec5SDimitry Andric uint16_t imm4 = extractBits(val, 4, 4) << 11; 2740b57cec5SDimitry Andric uint16_t imm9_8 = extractBits(val, 9, 8) << 9; 2750b57cec5SDimitry Andric uint16_t imm10 = extractBits(val, 10, 10) << 8; 2760b57cec5SDimitry Andric uint16_t imm6 = extractBits(val, 6, 6) << 7; 2770b57cec5SDimitry Andric uint16_t imm7 = extractBits(val, 7, 7) << 6; 2780b57cec5SDimitry Andric uint16_t imm3_1 = extractBits(val, 3, 1) << 3; 2790b57cec5SDimitry Andric uint16_t imm5 = extractBits(val, 5, 5) << 2; 2800b57cec5SDimitry Andric insn |= imm11 | imm4 | imm9_8 | imm10 | imm6 | imm7 | imm3_1 | imm5; 2810b57cec5SDimitry Andric 2820b57cec5SDimitry Andric write16le(loc, insn); 2830b57cec5SDimitry Andric return; 2840b57cec5SDimitry Andric } 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric case R_RISCV_RVC_LUI: { 2870b57cec5SDimitry Andric int64_t imm = SignExtend64(val + 0x800, bits) >> 12; 2880b57cec5SDimitry Andric checkInt(loc, imm, 6, type); 2890b57cec5SDimitry Andric if (imm == 0) { // `c.lui rd, 0` is illegal, convert to `c.li rd, 0` 2900b57cec5SDimitry Andric write16le(loc, (read16le(loc) & 0x0F83) | 0x4000); 2910b57cec5SDimitry Andric } else { 2920b57cec5SDimitry Andric uint16_t imm17 = extractBits(val + 0x800, 17, 17) << 12; 2930b57cec5SDimitry Andric uint16_t imm16_12 = extractBits(val + 0x800, 16, 12) << 2; 2940b57cec5SDimitry Andric write16le(loc, (read16le(loc) & 0xEF83) | imm17 | imm16_12); 2950b57cec5SDimitry Andric } 2960b57cec5SDimitry Andric return; 2970b57cec5SDimitry Andric } 2980b57cec5SDimitry Andric 2990b57cec5SDimitry Andric case R_RISCV_JAL: { 3000b57cec5SDimitry Andric checkInt(loc, static_cast<int64_t>(val) >> 1, 20, type); 3010b57cec5SDimitry Andric checkAlignment(loc, val, 2, type); 3020b57cec5SDimitry Andric 3030b57cec5SDimitry Andric uint32_t insn = read32le(loc) & 0xFFF; 3040b57cec5SDimitry Andric uint32_t imm20 = extractBits(val, 20, 20) << 31; 3050b57cec5SDimitry Andric uint32_t imm10_1 = extractBits(val, 10, 1) << 21; 3060b57cec5SDimitry Andric uint32_t imm11 = extractBits(val, 11, 11) << 20; 3070b57cec5SDimitry Andric uint32_t imm19_12 = extractBits(val, 19, 12) << 12; 3080b57cec5SDimitry Andric insn |= imm20 | imm10_1 | imm11 | imm19_12; 3090b57cec5SDimitry Andric 3100b57cec5SDimitry Andric write32le(loc, insn); 3110b57cec5SDimitry Andric return; 3120b57cec5SDimitry Andric } 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric case R_RISCV_BRANCH: { 3150b57cec5SDimitry Andric checkInt(loc, static_cast<int64_t>(val) >> 1, 12, type); 3160b57cec5SDimitry Andric checkAlignment(loc, val, 2, type); 3170b57cec5SDimitry Andric 3180b57cec5SDimitry Andric uint32_t insn = read32le(loc) & 0x1FFF07F; 3190b57cec5SDimitry Andric uint32_t imm12 = extractBits(val, 12, 12) << 31; 3200b57cec5SDimitry Andric uint32_t imm10_5 = extractBits(val, 10, 5) << 25; 3210b57cec5SDimitry Andric uint32_t imm4_1 = extractBits(val, 4, 1) << 8; 3220b57cec5SDimitry Andric uint32_t imm11 = extractBits(val, 11, 11) << 7; 3230b57cec5SDimitry Andric insn |= imm12 | imm10_5 | imm4_1 | imm11; 3240b57cec5SDimitry Andric 3250b57cec5SDimitry Andric write32le(loc, insn); 3260b57cec5SDimitry Andric return; 3270b57cec5SDimitry Andric } 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric // auipc + jalr pair 3300b57cec5SDimitry Andric case R_RISCV_CALL: 3310b57cec5SDimitry Andric case R_RISCV_CALL_PLT: { 3320b57cec5SDimitry Andric int64_t hi = SignExtend64(val + 0x800, bits) >> 12; 3330b57cec5SDimitry Andric checkInt(loc, hi, 20, type); 3340b57cec5SDimitry Andric if (isInt<20>(hi)) { 3350b57cec5SDimitry Andric relocateOne(loc, R_RISCV_PCREL_HI20, val); 3360b57cec5SDimitry Andric relocateOne(loc + 4, R_RISCV_PCREL_LO12_I, val); 3370b57cec5SDimitry Andric } 3380b57cec5SDimitry Andric return; 3390b57cec5SDimitry Andric } 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric case R_RISCV_GOT_HI20: 3420b57cec5SDimitry Andric case R_RISCV_PCREL_HI20: 3430b57cec5SDimitry Andric case R_RISCV_TLS_GD_HI20: 3440b57cec5SDimitry Andric case R_RISCV_TLS_GOT_HI20: 3450b57cec5SDimitry Andric case R_RISCV_TPREL_HI20: 3460b57cec5SDimitry Andric case R_RISCV_HI20: { 3470b57cec5SDimitry Andric uint64_t hi = val + 0x800; 3480b57cec5SDimitry Andric checkInt(loc, SignExtend64(hi, bits) >> 12, 20, type); 3490b57cec5SDimitry Andric write32le(loc, (read32le(loc) & 0xFFF) | (hi & 0xFFFFF000)); 3500b57cec5SDimitry Andric return; 3510b57cec5SDimitry Andric } 3520b57cec5SDimitry Andric 3530b57cec5SDimitry Andric case R_RISCV_PCREL_LO12_I: 3540b57cec5SDimitry Andric case R_RISCV_TPREL_LO12_I: 3550b57cec5SDimitry Andric case R_RISCV_LO12_I: { 3560b57cec5SDimitry Andric uint64_t hi = (val + 0x800) >> 12; 3570b57cec5SDimitry Andric uint64_t lo = val - (hi << 12); 3580b57cec5SDimitry Andric write32le(loc, (read32le(loc) & 0xFFFFF) | ((lo & 0xFFF) << 20)); 3590b57cec5SDimitry Andric return; 3600b57cec5SDimitry Andric } 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric case R_RISCV_PCREL_LO12_S: 3630b57cec5SDimitry Andric case R_RISCV_TPREL_LO12_S: 3640b57cec5SDimitry Andric case R_RISCV_LO12_S: { 3650b57cec5SDimitry Andric uint64_t hi = (val + 0x800) >> 12; 3660b57cec5SDimitry Andric uint64_t lo = val - (hi << 12); 3670b57cec5SDimitry Andric uint32_t imm11_5 = extractBits(lo, 11, 5) << 25; 3680b57cec5SDimitry Andric uint32_t imm4_0 = extractBits(lo, 4, 0) << 7; 3690b57cec5SDimitry Andric write32le(loc, (read32le(loc) & 0x1FFF07F) | imm11_5 | imm4_0); 3700b57cec5SDimitry Andric return; 3710b57cec5SDimitry Andric } 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric case R_RISCV_ADD8: 3740b57cec5SDimitry Andric *loc += val; 3750b57cec5SDimitry Andric return; 3760b57cec5SDimitry Andric case R_RISCV_ADD16: 3770b57cec5SDimitry Andric write16le(loc, read16le(loc) + val); 3780b57cec5SDimitry Andric return; 3790b57cec5SDimitry Andric case R_RISCV_ADD32: 3800b57cec5SDimitry Andric write32le(loc, read32le(loc) + val); 3810b57cec5SDimitry Andric return; 3820b57cec5SDimitry Andric case R_RISCV_ADD64: 3830b57cec5SDimitry Andric write64le(loc, read64le(loc) + val); 3840b57cec5SDimitry Andric return; 3850b57cec5SDimitry Andric case R_RISCV_SUB6: 3860b57cec5SDimitry Andric *loc = (*loc & 0xc0) | (((*loc & 0x3f) - val) & 0x3f); 3870b57cec5SDimitry Andric return; 3880b57cec5SDimitry Andric case R_RISCV_SUB8: 3890b57cec5SDimitry Andric *loc -= val; 3900b57cec5SDimitry Andric return; 3910b57cec5SDimitry Andric case R_RISCV_SUB16: 3920b57cec5SDimitry Andric write16le(loc, read16le(loc) - val); 3930b57cec5SDimitry Andric return; 3940b57cec5SDimitry Andric case R_RISCV_SUB32: 3950b57cec5SDimitry Andric write32le(loc, read32le(loc) - val); 3960b57cec5SDimitry Andric return; 3970b57cec5SDimitry Andric case R_RISCV_SUB64: 3980b57cec5SDimitry Andric write64le(loc, read64le(loc) - val); 3990b57cec5SDimitry Andric return; 4000b57cec5SDimitry Andric case R_RISCV_SET6: 4010b57cec5SDimitry Andric *loc = (*loc & 0xc0) | (val & 0x3f); 4020b57cec5SDimitry Andric return; 4030b57cec5SDimitry Andric case R_RISCV_SET8: 4040b57cec5SDimitry Andric *loc = val; 4050b57cec5SDimitry Andric return; 4060b57cec5SDimitry Andric case R_RISCV_SET16: 4070b57cec5SDimitry Andric write16le(loc, val); 4080b57cec5SDimitry Andric return; 4090b57cec5SDimitry Andric case R_RISCV_SET32: 4100b57cec5SDimitry Andric case R_RISCV_32_PCREL: 4110b57cec5SDimitry Andric write32le(loc, val); 4120b57cec5SDimitry Andric return; 4130b57cec5SDimitry Andric 4140b57cec5SDimitry Andric case R_RISCV_TLS_DTPREL32: 4150b57cec5SDimitry Andric write32le(loc, val - dtpOffset); 4160b57cec5SDimitry Andric break; 4170b57cec5SDimitry Andric case R_RISCV_TLS_DTPREL64: 4180b57cec5SDimitry Andric write64le(loc, val - dtpOffset); 4190b57cec5SDimitry Andric break; 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andric case R_RISCV_ALIGN: 4220b57cec5SDimitry Andric case R_RISCV_RELAX: 4230b57cec5SDimitry Andric return; // Ignored (for now) 4240b57cec5SDimitry Andric case R_RISCV_NONE: 4250b57cec5SDimitry Andric return; // Do nothing 4260b57cec5SDimitry Andric 4270b57cec5SDimitry Andric // These are handled by the dynamic linker 4280b57cec5SDimitry Andric case R_RISCV_RELATIVE: 4290b57cec5SDimitry Andric case R_RISCV_COPY: 4300b57cec5SDimitry Andric case R_RISCV_JUMP_SLOT: 4310b57cec5SDimitry Andric // GP-relative relocations are only produced after relaxation, which 4320b57cec5SDimitry Andric // we don't support for now 4330b57cec5SDimitry Andric case R_RISCV_GPREL_I: 4340b57cec5SDimitry Andric case R_RISCV_GPREL_S: 4350b57cec5SDimitry Andric default: 4360b57cec5SDimitry Andric error(getErrorLocation(loc) + 4370b57cec5SDimitry Andric "unimplemented relocation: " + toString(type)); 4380b57cec5SDimitry Andric return; 4390b57cec5SDimitry Andric } 4400b57cec5SDimitry Andric } 4410b57cec5SDimitry Andric 4420b57cec5SDimitry Andric TargetInfo *elf::getRISCVTargetInfo() { 4430b57cec5SDimitry Andric static RISCV target; 4440b57cec5SDimitry Andric return ⌖ 4450b57cec5SDimitry Andric } 446