xref: /freebsd/contrib/llvm-project/lld/ELF/Arch/RISCV.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
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;
270b57cec5SDimitry Andric   void writeGotHeader(uint8_t *buf) const override;
280b57cec5SDimitry Andric   void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
290b57cec5SDimitry Andric   void writePltHeader(uint8_t *buf) const override;
30480093f4SDimitry Andric   void writePlt(uint8_t *buf, const Symbol &sym,
31480093f4SDimitry Andric                 uint64_t pltEntryAddr) const override;
320b57cec5SDimitry Andric   RelType getDynRel(RelType type) const override;
330b57cec5SDimitry Andric   RelExpr getRelExpr(RelType type, const Symbol &s,
340b57cec5SDimitry Andric                      const uint8_t *loc) const override;
355ffd83dbSDimitry Andric   void relocate(uint8_t *loc, const Relocation &rel,
365ffd83dbSDimitry Andric                 uint64_t val) const override;
370b57cec5SDimitry Andric };
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric } // end anonymous namespace
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric const uint64_t dtpOffset = 0x800;
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric enum Op {
440b57cec5SDimitry Andric   ADDI = 0x13,
450b57cec5SDimitry Andric   AUIPC = 0x17,
460b57cec5SDimitry Andric   JALR = 0x67,
470b57cec5SDimitry Andric   LD = 0x3003,
480b57cec5SDimitry Andric   LW = 0x2003,
490b57cec5SDimitry Andric   SRLI = 0x5013,
500b57cec5SDimitry Andric   SUB = 0x40000033,
510b57cec5SDimitry Andric };
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric enum Reg {
540b57cec5SDimitry Andric   X_RA = 1,
550b57cec5SDimitry Andric   X_T0 = 5,
560b57cec5SDimitry Andric   X_T1 = 6,
570b57cec5SDimitry Andric   X_T2 = 7,
580b57cec5SDimitry Andric   X_T3 = 28,
590b57cec5SDimitry Andric };
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric static uint32_t hi20(uint32_t val) { return (val + 0x800) >> 12; }
620b57cec5SDimitry Andric static uint32_t lo12(uint32_t val) { return val & 4095; }
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric static uint32_t itype(uint32_t op, uint32_t rd, uint32_t rs1, uint32_t imm) {
650b57cec5SDimitry Andric   return op | (rd << 7) | (rs1 << 15) | (imm << 20);
660b57cec5SDimitry Andric }
670b57cec5SDimitry Andric static uint32_t rtype(uint32_t op, uint32_t rd, uint32_t rs1, uint32_t rs2) {
680b57cec5SDimitry Andric   return op | (rd << 7) | (rs1 << 15) | (rs2 << 20);
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric static uint32_t utype(uint32_t op, uint32_t rd, uint32_t imm) {
710b57cec5SDimitry Andric   return op | (rd << 7) | (imm << 12);
720b57cec5SDimitry Andric }
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric RISCV::RISCV() {
750b57cec5SDimitry Andric   copyRel = R_RISCV_COPY;
760b57cec5SDimitry Andric   noneRel = R_RISCV_NONE;
770b57cec5SDimitry Andric   pltRel = R_RISCV_JUMP_SLOT;
780b57cec5SDimitry Andric   relativeRel = R_RISCV_RELATIVE;
795ffd83dbSDimitry Andric   iRelativeRel = R_RISCV_IRELATIVE;
800b57cec5SDimitry Andric   if (config->is64) {
810b57cec5SDimitry Andric     symbolicRel = R_RISCV_64;
820b57cec5SDimitry Andric     tlsModuleIndexRel = R_RISCV_TLS_DTPMOD64;
830b57cec5SDimitry Andric     tlsOffsetRel = R_RISCV_TLS_DTPREL64;
840b57cec5SDimitry Andric     tlsGotRel = R_RISCV_TLS_TPREL64;
850b57cec5SDimitry Andric   } else {
860b57cec5SDimitry Andric     symbolicRel = R_RISCV_32;
870b57cec5SDimitry Andric     tlsModuleIndexRel = R_RISCV_TLS_DTPMOD32;
880b57cec5SDimitry Andric     tlsOffsetRel = R_RISCV_TLS_DTPREL32;
890b57cec5SDimitry Andric     tlsGotRel = R_RISCV_TLS_TPREL32;
900b57cec5SDimitry Andric   }
910b57cec5SDimitry Andric   gotRel = symbolicRel;
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric   // .got[0] = _DYNAMIC
940b57cec5SDimitry Andric   gotBaseSymInGotPlt = false;
950b57cec5SDimitry Andric   gotHeaderEntriesNum = 1;
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   // .got.plt[0] = _dl_runtime_resolve, .got.plt[1] = link_map
980b57cec5SDimitry Andric   gotPltHeaderEntriesNum = 2;
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric   pltHeaderSize = 32;
101480093f4SDimitry Andric   pltEntrySize = 16;
102480093f4SDimitry Andric   ipltEntrySize = 16;
1030b57cec5SDimitry Andric }
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric static uint32_t getEFlags(InputFile *f) {
1060b57cec5SDimitry Andric   if (config->is64)
107*e8d8bef9SDimitry Andric     return cast<ObjFile<ELF64LE>>(f)->getObj().getHeader().e_flags;
108*e8d8bef9SDimitry Andric   return cast<ObjFile<ELF32LE>>(f)->getObj().getHeader().e_flags;
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric uint32_t RISCV::calcEFlags() const {
112a1517e11SDimitry Andric   // If there are only binary input files (from -b binary), use a
113a1517e11SDimitry Andric   // value of 0 for the ELF header flags.
114a1517e11SDimitry Andric   if (objectFiles.empty())
115a1517e11SDimitry Andric     return 0;
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   uint32_t target = getEFlags(objectFiles.front());
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   for (InputFile *f : objectFiles) {
1200b57cec5SDimitry Andric     uint32_t eflags = getEFlags(f);
1210b57cec5SDimitry Andric     if (eflags & EF_RISCV_RVC)
1220b57cec5SDimitry Andric       target |= EF_RISCV_RVC;
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric     if ((eflags & EF_RISCV_FLOAT_ABI) != (target & EF_RISCV_FLOAT_ABI))
1250b57cec5SDimitry Andric       error(toString(f) +
1260b57cec5SDimitry Andric             ": cannot link object files with different floating-point ABI");
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric     if ((eflags & EF_RISCV_RVE) != (target & EF_RISCV_RVE))
1290b57cec5SDimitry Andric       error(toString(f) +
1300b57cec5SDimitry Andric             ": cannot link object files with different EF_RISCV_RVE");
1310b57cec5SDimitry Andric   }
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric   return target;
1340b57cec5SDimitry Andric }
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric void RISCV::writeGotHeader(uint8_t *buf) const {
1370b57cec5SDimitry Andric   if (config->is64)
1380b57cec5SDimitry Andric     write64le(buf, mainPart->dynamic->getVA());
1390b57cec5SDimitry Andric   else
1400b57cec5SDimitry Andric     write32le(buf, mainPart->dynamic->getVA());
1410b57cec5SDimitry Andric }
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric void RISCV::writeGotPlt(uint8_t *buf, const Symbol &s) const {
1440b57cec5SDimitry Andric   if (config->is64)
1450b57cec5SDimitry Andric     write64le(buf, in.plt->getVA());
1460b57cec5SDimitry Andric   else
1470b57cec5SDimitry Andric     write32le(buf, in.plt->getVA());
1480b57cec5SDimitry Andric }
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric void RISCV::writePltHeader(uint8_t *buf) const {
1510b57cec5SDimitry Andric   // 1: auipc t2, %pcrel_hi(.got.plt)
1520b57cec5SDimitry Andric   // sub t1, t1, t3
1530b57cec5SDimitry Andric   // l[wd] t3, %pcrel_lo(1b)(t2); t3 = _dl_runtime_resolve
1540b57cec5SDimitry Andric   // addi t1, t1, -pltHeaderSize-12; t1 = &.plt[i] - &.plt[0]
1550b57cec5SDimitry Andric   // addi t0, t2, %pcrel_lo(1b)
1560b57cec5SDimitry Andric   // srli t1, t1, (rv64?1:2); t1 = &.got.plt[i] - &.got.plt[0]
1570b57cec5SDimitry Andric   // l[wd] t0, Wordsize(t0); t0 = link_map
1580b57cec5SDimitry Andric   // jr t3
1590b57cec5SDimitry Andric   uint32_t offset = in.gotPlt->getVA() - in.plt->getVA();
1600b57cec5SDimitry Andric   uint32_t load = config->is64 ? LD : LW;
1610b57cec5SDimitry Andric   write32le(buf + 0, utype(AUIPC, X_T2, hi20(offset)));
1620b57cec5SDimitry Andric   write32le(buf + 4, rtype(SUB, X_T1, X_T1, X_T3));
1630b57cec5SDimitry Andric   write32le(buf + 8, itype(load, X_T3, X_T2, lo12(offset)));
1640b57cec5SDimitry Andric   write32le(buf + 12, itype(ADDI, X_T1, X_T1, -target->pltHeaderSize - 12));
1650b57cec5SDimitry Andric   write32le(buf + 16, itype(ADDI, X_T0, X_T2, lo12(offset)));
1660b57cec5SDimitry Andric   write32le(buf + 20, itype(SRLI, X_T1, X_T1, config->is64 ? 1 : 2));
1670b57cec5SDimitry Andric   write32le(buf + 24, itype(load, X_T0, X_T0, config->wordsize));
1680b57cec5SDimitry Andric   write32le(buf + 28, itype(JALR, 0, X_T3, 0));
1690b57cec5SDimitry Andric }
1700b57cec5SDimitry Andric 
171480093f4SDimitry Andric void RISCV::writePlt(uint8_t *buf, const Symbol &sym,
172480093f4SDimitry Andric                      uint64_t pltEntryAddr) const {
1730b57cec5SDimitry Andric   // 1: auipc t3, %pcrel_hi(f@.got.plt)
1740b57cec5SDimitry Andric   // l[wd] t3, %pcrel_lo(1b)(t3)
1750b57cec5SDimitry Andric   // jalr t1, t3
1760b57cec5SDimitry Andric   // nop
177480093f4SDimitry Andric   uint32_t offset = sym.getGotPltVA() - pltEntryAddr;
1780b57cec5SDimitry Andric   write32le(buf + 0, utype(AUIPC, X_T3, hi20(offset)));
1790b57cec5SDimitry Andric   write32le(buf + 4, itype(config->is64 ? LD : LW, X_T3, X_T3, lo12(offset)));
1800b57cec5SDimitry Andric   write32le(buf + 8, itype(JALR, X_T1, X_T3, 0));
1810b57cec5SDimitry Andric   write32le(buf + 12, itype(ADDI, 0, 0, 0));
1820b57cec5SDimitry Andric }
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric RelType RISCV::getDynRel(RelType type) const {
1850b57cec5SDimitry Andric   return type == target->symbolicRel ? type
1860b57cec5SDimitry Andric                                      : static_cast<RelType>(R_RISCV_NONE);
1870b57cec5SDimitry Andric }
1880b57cec5SDimitry Andric 
1890b57cec5SDimitry Andric RelExpr RISCV::getRelExpr(const RelType type, const Symbol &s,
1900b57cec5SDimitry Andric                           const uint8_t *loc) const {
1910b57cec5SDimitry Andric   switch (type) {
192480093f4SDimitry Andric   case R_RISCV_NONE:
193480093f4SDimitry Andric     return R_NONE;
194480093f4SDimitry Andric   case R_RISCV_32:
195480093f4SDimitry Andric   case R_RISCV_64:
196480093f4SDimitry Andric   case R_RISCV_HI20:
197480093f4SDimitry Andric   case R_RISCV_LO12_I:
198480093f4SDimitry Andric   case R_RISCV_LO12_S:
199480093f4SDimitry Andric   case R_RISCV_RVC_LUI:
200480093f4SDimitry Andric     return R_ABS;
2010b57cec5SDimitry Andric   case R_RISCV_ADD8:
2020b57cec5SDimitry Andric   case R_RISCV_ADD16:
2030b57cec5SDimitry Andric   case R_RISCV_ADD32:
2040b57cec5SDimitry Andric   case R_RISCV_ADD64:
2050b57cec5SDimitry Andric   case R_RISCV_SET6:
2060b57cec5SDimitry Andric   case R_RISCV_SET8:
2070b57cec5SDimitry Andric   case R_RISCV_SET16:
2080b57cec5SDimitry Andric   case R_RISCV_SET32:
2090b57cec5SDimitry Andric   case R_RISCV_SUB6:
2100b57cec5SDimitry Andric   case R_RISCV_SUB8:
2110b57cec5SDimitry Andric   case R_RISCV_SUB16:
2120b57cec5SDimitry Andric   case R_RISCV_SUB32:
2130b57cec5SDimitry Andric   case R_RISCV_SUB64:
2140b57cec5SDimitry Andric     return R_RISCV_ADD;
2150b57cec5SDimitry Andric   case R_RISCV_JAL:
2160b57cec5SDimitry Andric   case R_RISCV_BRANCH:
2170b57cec5SDimitry Andric   case R_RISCV_PCREL_HI20:
2180b57cec5SDimitry Andric   case R_RISCV_RVC_BRANCH:
2190b57cec5SDimitry Andric   case R_RISCV_RVC_JUMP:
2200b57cec5SDimitry Andric   case R_RISCV_32_PCREL:
2210b57cec5SDimitry Andric     return R_PC;
2220b57cec5SDimitry Andric   case R_RISCV_CALL:
2230b57cec5SDimitry Andric   case R_RISCV_CALL_PLT:
2240b57cec5SDimitry Andric     return R_PLT_PC;
2250b57cec5SDimitry Andric   case R_RISCV_GOT_HI20:
2260b57cec5SDimitry Andric     return R_GOT_PC;
2270b57cec5SDimitry Andric   case R_RISCV_PCREL_LO12_I:
2280b57cec5SDimitry Andric   case R_RISCV_PCREL_LO12_S:
2290b57cec5SDimitry Andric     return R_RISCV_PC_INDIRECT;
2300b57cec5SDimitry Andric   case R_RISCV_TLS_GD_HI20:
2310b57cec5SDimitry Andric     return R_TLSGD_PC;
2320b57cec5SDimitry Andric   case R_RISCV_TLS_GOT_HI20:
2330b57cec5SDimitry Andric     config->hasStaticTlsModel = true;
2340b57cec5SDimitry Andric     return R_GOT_PC;
2350b57cec5SDimitry Andric   case R_RISCV_TPREL_HI20:
2360b57cec5SDimitry Andric   case R_RISCV_TPREL_LO12_I:
2370b57cec5SDimitry Andric   case R_RISCV_TPREL_LO12_S:
238*e8d8bef9SDimitry Andric     return R_TPREL;
2390b57cec5SDimitry Andric   case R_RISCV_RELAX:
2400b57cec5SDimitry Andric   case R_RISCV_TPREL_ADD:
241480093f4SDimitry Andric     return R_NONE;
24255e4f9d5SDimitry Andric   case R_RISCV_ALIGN:
24355e4f9d5SDimitry Andric     // Not just a hint; always padded to the worst-case number of NOPs, so may
24455e4f9d5SDimitry Andric     // not currently be aligned, and without linker relaxation support we can't
24555e4f9d5SDimitry Andric     // delete NOPs to realign.
24655e4f9d5SDimitry Andric     errorOrWarn(getErrorLocation(loc) + "relocation R_RISCV_ALIGN requires "
24755e4f9d5SDimitry Andric                 "unimplemented linker relaxation; recompile with -mno-relax");
24855e4f9d5SDimitry Andric     return R_NONE;
2490b57cec5SDimitry Andric   default:
250480093f4SDimitry Andric     error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
251480093f4SDimitry Andric           ") against symbol " + toString(s));
252480093f4SDimitry Andric     return R_NONE;
2530b57cec5SDimitry Andric   }
2540b57cec5SDimitry Andric }
2550b57cec5SDimitry Andric 
2560b57cec5SDimitry Andric // Extract bits V[Begin:End], where range is inclusive, and Begin must be < 63.
2570b57cec5SDimitry Andric static uint32_t extractBits(uint64_t v, uint32_t begin, uint32_t end) {
2580b57cec5SDimitry Andric   return (v & ((1ULL << (begin + 1)) - 1)) >> end;
2590b57cec5SDimitry Andric }
2600b57cec5SDimitry Andric 
2615ffd83dbSDimitry Andric void RISCV::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
2620b57cec5SDimitry Andric   const unsigned bits = config->wordsize * 8;
2630b57cec5SDimitry Andric 
2645ffd83dbSDimitry Andric   switch (rel.type) {
2650b57cec5SDimitry Andric   case R_RISCV_32:
2660b57cec5SDimitry Andric     write32le(loc, val);
2670b57cec5SDimitry Andric     return;
2680b57cec5SDimitry Andric   case R_RISCV_64:
2690b57cec5SDimitry Andric     write64le(loc, val);
2700b57cec5SDimitry Andric     return;
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric   case R_RISCV_RVC_BRANCH: {
2735ffd83dbSDimitry Andric     checkInt(loc, static_cast<int64_t>(val) >> 1, 8, rel);
2745ffd83dbSDimitry Andric     checkAlignment(loc, val, 2, rel);
2750b57cec5SDimitry Andric     uint16_t insn = read16le(loc) & 0xE383;
2760b57cec5SDimitry Andric     uint16_t imm8 = extractBits(val, 8, 8) << 12;
2770b57cec5SDimitry Andric     uint16_t imm4_3 = extractBits(val, 4, 3) << 10;
2780b57cec5SDimitry Andric     uint16_t imm7_6 = extractBits(val, 7, 6) << 5;
2790b57cec5SDimitry Andric     uint16_t imm2_1 = extractBits(val, 2, 1) << 3;
2800b57cec5SDimitry Andric     uint16_t imm5 = extractBits(val, 5, 5) << 2;
2810b57cec5SDimitry Andric     insn |= imm8 | imm4_3 | imm7_6 | imm2_1 | imm5;
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric     write16le(loc, insn);
2840b57cec5SDimitry Andric     return;
2850b57cec5SDimitry Andric   }
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric   case R_RISCV_RVC_JUMP: {
2885ffd83dbSDimitry Andric     checkInt(loc, static_cast<int64_t>(val) >> 1, 11, rel);
2895ffd83dbSDimitry Andric     checkAlignment(loc, val, 2, rel);
2900b57cec5SDimitry Andric     uint16_t insn = read16le(loc) & 0xE003;
2910b57cec5SDimitry Andric     uint16_t imm11 = extractBits(val, 11, 11) << 12;
2920b57cec5SDimitry Andric     uint16_t imm4 = extractBits(val, 4, 4) << 11;
2930b57cec5SDimitry Andric     uint16_t imm9_8 = extractBits(val, 9, 8) << 9;
2940b57cec5SDimitry Andric     uint16_t imm10 = extractBits(val, 10, 10) << 8;
2950b57cec5SDimitry Andric     uint16_t imm6 = extractBits(val, 6, 6) << 7;
2960b57cec5SDimitry Andric     uint16_t imm7 = extractBits(val, 7, 7) << 6;
2970b57cec5SDimitry Andric     uint16_t imm3_1 = extractBits(val, 3, 1) << 3;
2980b57cec5SDimitry Andric     uint16_t imm5 = extractBits(val, 5, 5) << 2;
2990b57cec5SDimitry Andric     insn |= imm11 | imm4 | imm9_8 | imm10 | imm6 | imm7 | imm3_1 | imm5;
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric     write16le(loc, insn);
3020b57cec5SDimitry Andric     return;
3030b57cec5SDimitry Andric   }
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric   case R_RISCV_RVC_LUI: {
3060b57cec5SDimitry Andric     int64_t imm = SignExtend64(val + 0x800, bits) >> 12;
3075ffd83dbSDimitry Andric     checkInt(loc, imm, 6, rel);
3080b57cec5SDimitry Andric     if (imm == 0) { // `c.lui rd, 0` is illegal, convert to `c.li rd, 0`
3090b57cec5SDimitry Andric       write16le(loc, (read16le(loc) & 0x0F83) | 0x4000);
3100b57cec5SDimitry Andric     } else {
3110b57cec5SDimitry Andric       uint16_t imm17 = extractBits(val + 0x800, 17, 17) << 12;
3120b57cec5SDimitry Andric       uint16_t imm16_12 = extractBits(val + 0x800, 16, 12) << 2;
3130b57cec5SDimitry Andric       write16le(loc, (read16le(loc) & 0xEF83) | imm17 | imm16_12);
3140b57cec5SDimitry Andric     }
3150b57cec5SDimitry Andric     return;
3160b57cec5SDimitry Andric   }
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric   case R_RISCV_JAL: {
3195ffd83dbSDimitry Andric     checkInt(loc, static_cast<int64_t>(val) >> 1, 20, rel);
3205ffd83dbSDimitry Andric     checkAlignment(loc, val, 2, rel);
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric     uint32_t insn = read32le(loc) & 0xFFF;
3230b57cec5SDimitry Andric     uint32_t imm20 = extractBits(val, 20, 20) << 31;
3240b57cec5SDimitry Andric     uint32_t imm10_1 = extractBits(val, 10, 1) << 21;
3250b57cec5SDimitry Andric     uint32_t imm11 = extractBits(val, 11, 11) << 20;
3260b57cec5SDimitry Andric     uint32_t imm19_12 = extractBits(val, 19, 12) << 12;
3270b57cec5SDimitry Andric     insn |= imm20 | imm10_1 | imm11 | imm19_12;
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric     write32le(loc, insn);
3300b57cec5SDimitry Andric     return;
3310b57cec5SDimitry Andric   }
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric   case R_RISCV_BRANCH: {
3345ffd83dbSDimitry Andric     checkInt(loc, static_cast<int64_t>(val) >> 1, 12, rel);
3355ffd83dbSDimitry Andric     checkAlignment(loc, val, 2, rel);
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric     uint32_t insn = read32le(loc) & 0x1FFF07F;
3380b57cec5SDimitry Andric     uint32_t imm12 = extractBits(val, 12, 12) << 31;
3390b57cec5SDimitry Andric     uint32_t imm10_5 = extractBits(val, 10, 5) << 25;
3400b57cec5SDimitry Andric     uint32_t imm4_1 = extractBits(val, 4, 1) << 8;
3410b57cec5SDimitry Andric     uint32_t imm11 = extractBits(val, 11, 11) << 7;
3420b57cec5SDimitry Andric     insn |= imm12 | imm10_5 | imm4_1 | imm11;
3430b57cec5SDimitry Andric 
3440b57cec5SDimitry Andric     write32le(loc, insn);
3450b57cec5SDimitry Andric     return;
3460b57cec5SDimitry Andric   }
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric   // auipc + jalr pair
3490b57cec5SDimitry Andric   case R_RISCV_CALL:
3500b57cec5SDimitry Andric   case R_RISCV_CALL_PLT: {
3510b57cec5SDimitry Andric     int64_t hi = SignExtend64(val + 0x800, bits) >> 12;
3525ffd83dbSDimitry Andric     checkInt(loc, hi, 20, rel);
3530b57cec5SDimitry Andric     if (isInt<20>(hi)) {
3545ffd83dbSDimitry Andric       relocateNoSym(loc, R_RISCV_PCREL_HI20, val);
3555ffd83dbSDimitry Andric       relocateNoSym(loc + 4, R_RISCV_PCREL_LO12_I, val);
3560b57cec5SDimitry Andric     }
3570b57cec5SDimitry Andric     return;
3580b57cec5SDimitry Andric   }
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric   case R_RISCV_GOT_HI20:
3610b57cec5SDimitry Andric   case R_RISCV_PCREL_HI20:
3620b57cec5SDimitry Andric   case R_RISCV_TLS_GD_HI20:
3630b57cec5SDimitry Andric   case R_RISCV_TLS_GOT_HI20:
3640b57cec5SDimitry Andric   case R_RISCV_TPREL_HI20:
3650b57cec5SDimitry Andric   case R_RISCV_HI20: {
3660b57cec5SDimitry Andric     uint64_t hi = val + 0x800;
3675ffd83dbSDimitry Andric     checkInt(loc, SignExtend64(hi, bits) >> 12, 20, rel);
3680b57cec5SDimitry Andric     write32le(loc, (read32le(loc) & 0xFFF) | (hi & 0xFFFFF000));
3690b57cec5SDimitry Andric     return;
3700b57cec5SDimitry Andric   }
3710b57cec5SDimitry Andric 
3720b57cec5SDimitry Andric   case R_RISCV_PCREL_LO12_I:
3730b57cec5SDimitry Andric   case R_RISCV_TPREL_LO12_I:
3740b57cec5SDimitry Andric   case R_RISCV_LO12_I: {
3750b57cec5SDimitry Andric     uint64_t hi = (val + 0x800) >> 12;
3760b57cec5SDimitry Andric     uint64_t lo = val - (hi << 12);
3770b57cec5SDimitry Andric     write32le(loc, (read32le(loc) & 0xFFFFF) | ((lo & 0xFFF) << 20));
3780b57cec5SDimitry Andric     return;
3790b57cec5SDimitry Andric   }
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric   case R_RISCV_PCREL_LO12_S:
3820b57cec5SDimitry Andric   case R_RISCV_TPREL_LO12_S:
3830b57cec5SDimitry Andric   case R_RISCV_LO12_S: {
3840b57cec5SDimitry Andric     uint64_t hi = (val + 0x800) >> 12;
3850b57cec5SDimitry Andric     uint64_t lo = val - (hi << 12);
3860b57cec5SDimitry Andric     uint32_t imm11_5 = extractBits(lo, 11, 5) << 25;
3870b57cec5SDimitry Andric     uint32_t imm4_0 = extractBits(lo, 4, 0) << 7;
3880b57cec5SDimitry Andric     write32le(loc, (read32le(loc) & 0x1FFF07F) | imm11_5 | imm4_0);
3890b57cec5SDimitry Andric     return;
3900b57cec5SDimitry Andric   }
3910b57cec5SDimitry Andric 
3920b57cec5SDimitry Andric   case R_RISCV_ADD8:
3930b57cec5SDimitry Andric     *loc += val;
3940b57cec5SDimitry Andric     return;
3950b57cec5SDimitry Andric   case R_RISCV_ADD16:
3960b57cec5SDimitry Andric     write16le(loc, read16le(loc) + val);
3970b57cec5SDimitry Andric     return;
3980b57cec5SDimitry Andric   case R_RISCV_ADD32:
3990b57cec5SDimitry Andric     write32le(loc, read32le(loc) + val);
4000b57cec5SDimitry Andric     return;
4010b57cec5SDimitry Andric   case R_RISCV_ADD64:
4020b57cec5SDimitry Andric     write64le(loc, read64le(loc) + val);
4030b57cec5SDimitry Andric     return;
4040b57cec5SDimitry Andric   case R_RISCV_SUB6:
4050b57cec5SDimitry Andric     *loc = (*loc & 0xc0) | (((*loc & 0x3f) - val) & 0x3f);
4060b57cec5SDimitry Andric     return;
4070b57cec5SDimitry Andric   case R_RISCV_SUB8:
4080b57cec5SDimitry Andric     *loc -= val;
4090b57cec5SDimitry Andric     return;
4100b57cec5SDimitry Andric   case R_RISCV_SUB16:
4110b57cec5SDimitry Andric     write16le(loc, read16le(loc) - val);
4120b57cec5SDimitry Andric     return;
4130b57cec5SDimitry Andric   case R_RISCV_SUB32:
4140b57cec5SDimitry Andric     write32le(loc, read32le(loc) - val);
4150b57cec5SDimitry Andric     return;
4160b57cec5SDimitry Andric   case R_RISCV_SUB64:
4170b57cec5SDimitry Andric     write64le(loc, read64le(loc) - val);
4180b57cec5SDimitry Andric     return;
4190b57cec5SDimitry Andric   case R_RISCV_SET6:
4200b57cec5SDimitry Andric     *loc = (*loc & 0xc0) | (val & 0x3f);
4210b57cec5SDimitry Andric     return;
4220b57cec5SDimitry Andric   case R_RISCV_SET8:
4230b57cec5SDimitry Andric     *loc = val;
4240b57cec5SDimitry Andric     return;
4250b57cec5SDimitry Andric   case R_RISCV_SET16:
4260b57cec5SDimitry Andric     write16le(loc, val);
4270b57cec5SDimitry Andric     return;
4280b57cec5SDimitry Andric   case R_RISCV_SET32:
4290b57cec5SDimitry Andric   case R_RISCV_32_PCREL:
4300b57cec5SDimitry Andric     write32le(loc, val);
4310b57cec5SDimitry Andric     return;
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric   case R_RISCV_TLS_DTPREL32:
4340b57cec5SDimitry Andric     write32le(loc, val - dtpOffset);
4350b57cec5SDimitry Andric     break;
4360b57cec5SDimitry Andric   case R_RISCV_TLS_DTPREL64:
4370b57cec5SDimitry Andric     write64le(loc, val - dtpOffset);
4380b57cec5SDimitry Andric     break;
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric   case R_RISCV_RELAX:
4410b57cec5SDimitry Andric     return; // Ignored (for now)
4420b57cec5SDimitry Andric 
4430b57cec5SDimitry Andric   default:
444480093f4SDimitry Andric     llvm_unreachable("unknown relocation");
4450b57cec5SDimitry Andric   }
4460b57cec5SDimitry Andric }
4470b57cec5SDimitry Andric 
4485ffd83dbSDimitry Andric TargetInfo *elf::getRISCVTargetInfo() {
4490b57cec5SDimitry Andric   static RISCV target;
4500b57cec5SDimitry Andric   return &target;
4510b57cec5SDimitry Andric }
452