xref: /freebsd/contrib/llvm-project/lld/ELF/Arch/RISCV.cpp (revision 753f127f3ace09432b2baeffd71a308760641a62)
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"
10*753f127fSDimitry Andric #include "OutputSections.h"
11480093f4SDimitry Andric #include "Symbols.h"
120b57cec5SDimitry Andric #include "SyntheticSections.h"
130b57cec5SDimitry Andric #include "Target.h"
14*753f127fSDimitry Andric #include "llvm/Support/TimeProfiler.h"
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric using namespace llvm;
170b57cec5SDimitry Andric using namespace llvm::object;
180b57cec5SDimitry Andric using namespace llvm::support::endian;
190b57cec5SDimitry Andric using namespace llvm::ELF;
205ffd83dbSDimitry Andric using namespace lld;
215ffd83dbSDimitry Andric using namespace lld::elf;
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric namespace {
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric class RISCV final : public TargetInfo {
260b57cec5SDimitry Andric public:
270b57cec5SDimitry Andric   RISCV();
280b57cec5SDimitry Andric   uint32_t calcEFlags() const override;
29fe6060f1SDimitry Andric   int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override;
300b57cec5SDimitry Andric   void writeGotHeader(uint8_t *buf) const override;
310b57cec5SDimitry Andric   void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
32fe6060f1SDimitry Andric   void writeIgotPlt(uint8_t *buf, const Symbol &s) const override;
330b57cec5SDimitry Andric   void writePltHeader(uint8_t *buf) const override;
34480093f4SDimitry Andric   void writePlt(uint8_t *buf, const Symbol &sym,
35480093f4SDimitry Andric                 uint64_t pltEntryAddr) const override;
360b57cec5SDimitry Andric   RelType getDynRel(RelType type) const override;
370b57cec5SDimitry Andric   RelExpr getRelExpr(RelType type, const Symbol &s,
380b57cec5SDimitry Andric                      const uint8_t *loc) const override;
395ffd83dbSDimitry Andric   void relocate(uint8_t *loc, const Relocation &rel,
405ffd83dbSDimitry Andric                 uint64_t val) const override;
41*753f127fSDimitry Andric   bool relaxOnce(int pass) const override;
420b57cec5SDimitry Andric };
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric } // end anonymous namespace
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric const uint64_t dtpOffset = 0x800;
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric enum Op {
490b57cec5SDimitry Andric   ADDI = 0x13,
500b57cec5SDimitry Andric   AUIPC = 0x17,
510b57cec5SDimitry Andric   JALR = 0x67,
520b57cec5SDimitry Andric   LD = 0x3003,
530b57cec5SDimitry Andric   LW = 0x2003,
540b57cec5SDimitry Andric   SRLI = 0x5013,
550b57cec5SDimitry Andric   SUB = 0x40000033,
560b57cec5SDimitry Andric };
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric enum Reg {
590b57cec5SDimitry Andric   X_RA = 1,
600b57cec5SDimitry Andric   X_T0 = 5,
610b57cec5SDimitry Andric   X_T1 = 6,
620b57cec5SDimitry Andric   X_T2 = 7,
630b57cec5SDimitry Andric   X_T3 = 28,
640b57cec5SDimitry Andric };
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric static uint32_t hi20(uint32_t val) { return (val + 0x800) >> 12; }
670b57cec5SDimitry Andric static uint32_t lo12(uint32_t val) { return val & 4095; }
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric static uint32_t itype(uint32_t op, uint32_t rd, uint32_t rs1, uint32_t imm) {
700b57cec5SDimitry Andric   return op | (rd << 7) | (rs1 << 15) | (imm << 20);
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric static uint32_t rtype(uint32_t op, uint32_t rd, uint32_t rs1, uint32_t rs2) {
730b57cec5SDimitry Andric   return op | (rd << 7) | (rs1 << 15) | (rs2 << 20);
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric static uint32_t utype(uint32_t op, uint32_t rd, uint32_t imm) {
760b57cec5SDimitry Andric   return op | (rd << 7) | (imm << 12);
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric RISCV::RISCV() {
800b57cec5SDimitry Andric   copyRel = R_RISCV_COPY;
810b57cec5SDimitry Andric   pltRel = R_RISCV_JUMP_SLOT;
820b57cec5SDimitry Andric   relativeRel = R_RISCV_RELATIVE;
835ffd83dbSDimitry Andric   iRelativeRel = R_RISCV_IRELATIVE;
840b57cec5SDimitry Andric   if (config->is64) {
850b57cec5SDimitry Andric     symbolicRel = R_RISCV_64;
860b57cec5SDimitry Andric     tlsModuleIndexRel = R_RISCV_TLS_DTPMOD64;
870b57cec5SDimitry Andric     tlsOffsetRel = R_RISCV_TLS_DTPREL64;
880b57cec5SDimitry Andric     tlsGotRel = R_RISCV_TLS_TPREL64;
890b57cec5SDimitry Andric   } else {
900b57cec5SDimitry Andric     symbolicRel = R_RISCV_32;
910b57cec5SDimitry Andric     tlsModuleIndexRel = R_RISCV_TLS_DTPMOD32;
920b57cec5SDimitry Andric     tlsOffsetRel = R_RISCV_TLS_DTPREL32;
930b57cec5SDimitry Andric     tlsGotRel = R_RISCV_TLS_TPREL32;
940b57cec5SDimitry Andric   }
950b57cec5SDimitry Andric   gotRel = symbolicRel;
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   // .got[0] = _DYNAMIC
980b57cec5SDimitry Andric   gotHeaderEntriesNum = 1;
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric   // .got.plt[0] = _dl_runtime_resolve, .got.plt[1] = link_map
1010b57cec5SDimitry Andric   gotPltHeaderEntriesNum = 2;
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric   pltHeaderSize = 32;
104480093f4SDimitry Andric   pltEntrySize = 16;
105480093f4SDimitry Andric   ipltEntrySize = 16;
1060b57cec5SDimitry Andric }
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric static uint32_t getEFlags(InputFile *f) {
1090b57cec5SDimitry Andric   if (config->is64)
110e8d8bef9SDimitry Andric     return cast<ObjFile<ELF64LE>>(f)->getObj().getHeader().e_flags;
111e8d8bef9SDimitry Andric   return cast<ObjFile<ELF32LE>>(f)->getObj().getHeader().e_flags;
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric uint32_t RISCV::calcEFlags() const {
115a1517e11SDimitry Andric   // If there are only binary input files (from -b binary), use a
116a1517e11SDimitry Andric   // value of 0 for the ELF header flags.
11781ad6265SDimitry Andric   if (ctx->objectFiles.empty())
118a1517e11SDimitry Andric     return 0;
1190b57cec5SDimitry Andric 
12081ad6265SDimitry Andric   uint32_t target = getEFlags(ctx->objectFiles.front());
1210b57cec5SDimitry Andric 
12281ad6265SDimitry Andric   for (InputFile *f : ctx->objectFiles) {
1230b57cec5SDimitry Andric     uint32_t eflags = getEFlags(f);
1240b57cec5SDimitry Andric     if (eflags & EF_RISCV_RVC)
1250b57cec5SDimitry Andric       target |= EF_RISCV_RVC;
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric     if ((eflags & EF_RISCV_FLOAT_ABI) != (target & EF_RISCV_FLOAT_ABI))
1280b57cec5SDimitry Andric       error(toString(f) +
1290b57cec5SDimitry Andric             ": cannot link object files with different floating-point ABI");
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric     if ((eflags & EF_RISCV_RVE) != (target & EF_RISCV_RVE))
1320b57cec5SDimitry Andric       error(toString(f) +
1330b57cec5SDimitry Andric             ": cannot link object files with different EF_RISCV_RVE");
1340b57cec5SDimitry Andric   }
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric   return target;
1370b57cec5SDimitry Andric }
1380b57cec5SDimitry Andric 
139fe6060f1SDimitry Andric int64_t RISCV::getImplicitAddend(const uint8_t *buf, RelType type) const {
140fe6060f1SDimitry Andric   switch (type) {
141fe6060f1SDimitry Andric   default:
142fe6060f1SDimitry Andric     internalLinkerError(getErrorLocation(buf),
143fe6060f1SDimitry Andric                         "cannot read addend for relocation " + toString(type));
144fe6060f1SDimitry Andric     return 0;
145fe6060f1SDimitry Andric   case R_RISCV_32:
146fe6060f1SDimitry Andric   case R_RISCV_TLS_DTPMOD32:
147fe6060f1SDimitry Andric   case R_RISCV_TLS_DTPREL32:
148fe6060f1SDimitry Andric     return SignExtend64<32>(read32le(buf));
149fe6060f1SDimitry Andric   case R_RISCV_64:
150fe6060f1SDimitry Andric     return read64le(buf);
151fe6060f1SDimitry Andric   case R_RISCV_RELATIVE:
152fe6060f1SDimitry Andric   case R_RISCV_IRELATIVE:
153fe6060f1SDimitry Andric     return config->is64 ? read64le(buf) : read32le(buf);
154fe6060f1SDimitry Andric   case R_RISCV_NONE:
155fe6060f1SDimitry Andric   case R_RISCV_JUMP_SLOT:
156fe6060f1SDimitry Andric     // These relocations are defined as not having an implicit addend.
157fe6060f1SDimitry Andric     return 0;
158fe6060f1SDimitry Andric   }
159fe6060f1SDimitry Andric }
160fe6060f1SDimitry Andric 
1610b57cec5SDimitry Andric void RISCV::writeGotHeader(uint8_t *buf) const {
1620b57cec5SDimitry Andric   if (config->is64)
1630b57cec5SDimitry Andric     write64le(buf, mainPart->dynamic->getVA());
1640b57cec5SDimitry Andric   else
1650b57cec5SDimitry Andric     write32le(buf, mainPart->dynamic->getVA());
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric void RISCV::writeGotPlt(uint8_t *buf, const Symbol &s) const {
1690b57cec5SDimitry Andric   if (config->is64)
1700b57cec5SDimitry Andric     write64le(buf, in.plt->getVA());
1710b57cec5SDimitry Andric   else
1720b57cec5SDimitry Andric     write32le(buf, in.plt->getVA());
1730b57cec5SDimitry Andric }
1740b57cec5SDimitry Andric 
175fe6060f1SDimitry Andric void RISCV::writeIgotPlt(uint8_t *buf, const Symbol &s) const {
176fe6060f1SDimitry Andric   if (config->writeAddends) {
177fe6060f1SDimitry Andric     if (config->is64)
178fe6060f1SDimitry Andric       write64le(buf, s.getVA());
179fe6060f1SDimitry Andric     else
180fe6060f1SDimitry Andric       write32le(buf, s.getVA());
181fe6060f1SDimitry Andric   }
182fe6060f1SDimitry Andric }
183fe6060f1SDimitry Andric 
1840b57cec5SDimitry Andric void RISCV::writePltHeader(uint8_t *buf) const {
1850b57cec5SDimitry Andric   // 1: auipc t2, %pcrel_hi(.got.plt)
1860b57cec5SDimitry Andric   // sub t1, t1, t3
1870b57cec5SDimitry Andric   // l[wd] t3, %pcrel_lo(1b)(t2); t3 = _dl_runtime_resolve
1880b57cec5SDimitry Andric   // addi t1, t1, -pltHeaderSize-12; t1 = &.plt[i] - &.plt[0]
1890b57cec5SDimitry Andric   // addi t0, t2, %pcrel_lo(1b)
1900b57cec5SDimitry Andric   // srli t1, t1, (rv64?1:2); t1 = &.got.plt[i] - &.got.plt[0]
1910b57cec5SDimitry Andric   // l[wd] t0, Wordsize(t0); t0 = link_map
1920b57cec5SDimitry Andric   // jr t3
1930b57cec5SDimitry Andric   uint32_t offset = in.gotPlt->getVA() - in.plt->getVA();
1940b57cec5SDimitry Andric   uint32_t load = config->is64 ? LD : LW;
1950b57cec5SDimitry Andric   write32le(buf + 0, utype(AUIPC, X_T2, hi20(offset)));
1960b57cec5SDimitry Andric   write32le(buf + 4, rtype(SUB, X_T1, X_T1, X_T3));
1970b57cec5SDimitry Andric   write32le(buf + 8, itype(load, X_T3, X_T2, lo12(offset)));
1980b57cec5SDimitry Andric   write32le(buf + 12, itype(ADDI, X_T1, X_T1, -target->pltHeaderSize - 12));
1990b57cec5SDimitry Andric   write32le(buf + 16, itype(ADDI, X_T0, X_T2, lo12(offset)));
2000b57cec5SDimitry Andric   write32le(buf + 20, itype(SRLI, X_T1, X_T1, config->is64 ? 1 : 2));
2010b57cec5SDimitry Andric   write32le(buf + 24, itype(load, X_T0, X_T0, config->wordsize));
2020b57cec5SDimitry Andric   write32le(buf + 28, itype(JALR, 0, X_T3, 0));
2030b57cec5SDimitry Andric }
2040b57cec5SDimitry Andric 
205480093f4SDimitry Andric void RISCV::writePlt(uint8_t *buf, const Symbol &sym,
206480093f4SDimitry Andric                      uint64_t pltEntryAddr) const {
2070b57cec5SDimitry Andric   // 1: auipc t3, %pcrel_hi(f@.got.plt)
2080b57cec5SDimitry Andric   // l[wd] t3, %pcrel_lo(1b)(t3)
2090b57cec5SDimitry Andric   // jalr t1, t3
2100b57cec5SDimitry Andric   // nop
211480093f4SDimitry Andric   uint32_t offset = sym.getGotPltVA() - pltEntryAddr;
2120b57cec5SDimitry Andric   write32le(buf + 0, utype(AUIPC, X_T3, hi20(offset)));
2130b57cec5SDimitry Andric   write32le(buf + 4, itype(config->is64 ? LD : LW, X_T3, X_T3, lo12(offset)));
2140b57cec5SDimitry Andric   write32le(buf + 8, itype(JALR, X_T1, X_T3, 0));
2150b57cec5SDimitry Andric   write32le(buf + 12, itype(ADDI, 0, 0, 0));
2160b57cec5SDimitry Andric }
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric RelType RISCV::getDynRel(RelType type) const {
2190b57cec5SDimitry Andric   return type == target->symbolicRel ? type
2200b57cec5SDimitry Andric                                      : static_cast<RelType>(R_RISCV_NONE);
2210b57cec5SDimitry Andric }
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric RelExpr RISCV::getRelExpr(const RelType type, const Symbol &s,
2240b57cec5SDimitry Andric                           const uint8_t *loc) const {
2250b57cec5SDimitry Andric   switch (type) {
226480093f4SDimitry Andric   case R_RISCV_NONE:
227480093f4SDimitry Andric     return R_NONE;
228480093f4SDimitry Andric   case R_RISCV_32:
229480093f4SDimitry Andric   case R_RISCV_64:
230480093f4SDimitry Andric   case R_RISCV_HI20:
231480093f4SDimitry Andric   case R_RISCV_LO12_I:
232480093f4SDimitry Andric   case R_RISCV_LO12_S:
233480093f4SDimitry Andric   case R_RISCV_RVC_LUI:
234480093f4SDimitry Andric     return R_ABS;
2350b57cec5SDimitry Andric   case R_RISCV_ADD8:
2360b57cec5SDimitry Andric   case R_RISCV_ADD16:
2370b57cec5SDimitry Andric   case R_RISCV_ADD32:
2380b57cec5SDimitry Andric   case R_RISCV_ADD64:
2390b57cec5SDimitry Andric   case R_RISCV_SET6:
2400b57cec5SDimitry Andric   case R_RISCV_SET8:
2410b57cec5SDimitry Andric   case R_RISCV_SET16:
2420b57cec5SDimitry Andric   case R_RISCV_SET32:
2430b57cec5SDimitry Andric   case R_RISCV_SUB6:
2440b57cec5SDimitry Andric   case R_RISCV_SUB8:
2450b57cec5SDimitry Andric   case R_RISCV_SUB16:
2460b57cec5SDimitry Andric   case R_RISCV_SUB32:
2470b57cec5SDimitry Andric   case R_RISCV_SUB64:
2480b57cec5SDimitry Andric     return R_RISCV_ADD;
2490b57cec5SDimitry Andric   case R_RISCV_JAL:
2500b57cec5SDimitry Andric   case R_RISCV_BRANCH:
2510b57cec5SDimitry Andric   case R_RISCV_PCREL_HI20:
2520b57cec5SDimitry Andric   case R_RISCV_RVC_BRANCH:
2530b57cec5SDimitry Andric   case R_RISCV_RVC_JUMP:
2540b57cec5SDimitry Andric   case R_RISCV_32_PCREL:
2550b57cec5SDimitry Andric     return R_PC;
2560b57cec5SDimitry Andric   case R_RISCV_CALL:
2570b57cec5SDimitry Andric   case R_RISCV_CALL_PLT:
2580b57cec5SDimitry Andric     return R_PLT_PC;
2590b57cec5SDimitry Andric   case R_RISCV_GOT_HI20:
2600b57cec5SDimitry Andric     return R_GOT_PC;
2610b57cec5SDimitry Andric   case R_RISCV_PCREL_LO12_I:
2620b57cec5SDimitry Andric   case R_RISCV_PCREL_LO12_S:
2630b57cec5SDimitry Andric     return R_RISCV_PC_INDIRECT;
2640b57cec5SDimitry Andric   case R_RISCV_TLS_GD_HI20:
2650b57cec5SDimitry Andric     return R_TLSGD_PC;
2660b57cec5SDimitry Andric   case R_RISCV_TLS_GOT_HI20:
2674824e7fdSDimitry Andric     config->hasTlsIe = true;
2680b57cec5SDimitry Andric     return R_GOT_PC;
2690b57cec5SDimitry Andric   case R_RISCV_TPREL_HI20:
2700b57cec5SDimitry Andric   case R_RISCV_TPREL_LO12_I:
2710b57cec5SDimitry Andric   case R_RISCV_TPREL_LO12_S:
272e8d8bef9SDimitry Andric     return R_TPREL;
2730b57cec5SDimitry Andric   case R_RISCV_TPREL_ADD:
274480093f4SDimitry Andric     return R_NONE;
27555e4f9d5SDimitry Andric   case R_RISCV_ALIGN:
276*753f127fSDimitry Andric     return R_RELAX_HINT;
277*753f127fSDimitry Andric   case R_RISCV_RELAX:
278*753f127fSDimitry Andric     return config->relax ? R_RELAX_HINT : R_NONE;
2790b57cec5SDimitry Andric   default:
280480093f4SDimitry Andric     error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
281480093f4SDimitry Andric           ") against symbol " + toString(s));
282480093f4SDimitry Andric     return R_NONE;
2830b57cec5SDimitry Andric   }
2840b57cec5SDimitry Andric }
2850b57cec5SDimitry Andric 
2860b57cec5SDimitry Andric // Extract bits V[Begin:End], where range is inclusive, and Begin must be < 63.
2870b57cec5SDimitry Andric static uint32_t extractBits(uint64_t v, uint32_t begin, uint32_t end) {
2880b57cec5SDimitry Andric   return (v & ((1ULL << (begin + 1)) - 1)) >> end;
2890b57cec5SDimitry Andric }
2900b57cec5SDimitry Andric 
2915ffd83dbSDimitry Andric void RISCV::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
2920b57cec5SDimitry Andric   const unsigned bits = config->wordsize * 8;
2930b57cec5SDimitry Andric 
2945ffd83dbSDimitry Andric   switch (rel.type) {
2950b57cec5SDimitry Andric   case R_RISCV_32:
2960b57cec5SDimitry Andric     write32le(loc, val);
2970b57cec5SDimitry Andric     return;
2980b57cec5SDimitry Andric   case R_RISCV_64:
2990b57cec5SDimitry Andric     write64le(loc, val);
3000b57cec5SDimitry Andric     return;
3010b57cec5SDimitry Andric 
3020b57cec5SDimitry Andric   case R_RISCV_RVC_BRANCH: {
303*753f127fSDimitry Andric     checkInt(loc, val, 9, rel);
3045ffd83dbSDimitry Andric     checkAlignment(loc, val, 2, rel);
3050b57cec5SDimitry Andric     uint16_t insn = read16le(loc) & 0xE383;
3060b57cec5SDimitry Andric     uint16_t imm8 = extractBits(val, 8, 8) << 12;
3070b57cec5SDimitry Andric     uint16_t imm4_3 = extractBits(val, 4, 3) << 10;
3080b57cec5SDimitry Andric     uint16_t imm7_6 = extractBits(val, 7, 6) << 5;
3090b57cec5SDimitry Andric     uint16_t imm2_1 = extractBits(val, 2, 1) << 3;
3100b57cec5SDimitry Andric     uint16_t imm5 = extractBits(val, 5, 5) << 2;
3110b57cec5SDimitry Andric     insn |= imm8 | imm4_3 | imm7_6 | imm2_1 | imm5;
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric     write16le(loc, insn);
3140b57cec5SDimitry Andric     return;
3150b57cec5SDimitry Andric   }
3160b57cec5SDimitry Andric 
3170b57cec5SDimitry Andric   case R_RISCV_RVC_JUMP: {
318*753f127fSDimitry Andric     checkInt(loc, val, 12, rel);
3195ffd83dbSDimitry Andric     checkAlignment(loc, val, 2, rel);
3200b57cec5SDimitry Andric     uint16_t insn = read16le(loc) & 0xE003;
3210b57cec5SDimitry Andric     uint16_t imm11 = extractBits(val, 11, 11) << 12;
3220b57cec5SDimitry Andric     uint16_t imm4 = extractBits(val, 4, 4) << 11;
3230b57cec5SDimitry Andric     uint16_t imm9_8 = extractBits(val, 9, 8) << 9;
3240b57cec5SDimitry Andric     uint16_t imm10 = extractBits(val, 10, 10) << 8;
3250b57cec5SDimitry Andric     uint16_t imm6 = extractBits(val, 6, 6) << 7;
3260b57cec5SDimitry Andric     uint16_t imm7 = extractBits(val, 7, 7) << 6;
3270b57cec5SDimitry Andric     uint16_t imm3_1 = extractBits(val, 3, 1) << 3;
3280b57cec5SDimitry Andric     uint16_t imm5 = extractBits(val, 5, 5) << 2;
3290b57cec5SDimitry Andric     insn |= imm11 | imm4 | imm9_8 | imm10 | imm6 | imm7 | imm3_1 | imm5;
3300b57cec5SDimitry Andric 
3310b57cec5SDimitry Andric     write16le(loc, insn);
3320b57cec5SDimitry Andric     return;
3330b57cec5SDimitry Andric   }
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric   case R_RISCV_RVC_LUI: {
3360b57cec5SDimitry Andric     int64_t imm = SignExtend64(val + 0x800, bits) >> 12;
3375ffd83dbSDimitry Andric     checkInt(loc, imm, 6, rel);
3380b57cec5SDimitry Andric     if (imm == 0) { // `c.lui rd, 0` is illegal, convert to `c.li rd, 0`
3390b57cec5SDimitry Andric       write16le(loc, (read16le(loc) & 0x0F83) | 0x4000);
3400b57cec5SDimitry Andric     } else {
3410b57cec5SDimitry Andric       uint16_t imm17 = extractBits(val + 0x800, 17, 17) << 12;
3420b57cec5SDimitry Andric       uint16_t imm16_12 = extractBits(val + 0x800, 16, 12) << 2;
3430b57cec5SDimitry Andric       write16le(loc, (read16le(loc) & 0xEF83) | imm17 | imm16_12);
3440b57cec5SDimitry Andric     }
3450b57cec5SDimitry Andric     return;
3460b57cec5SDimitry Andric   }
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric   case R_RISCV_JAL: {
349*753f127fSDimitry Andric     checkInt(loc, val, 21, rel);
3505ffd83dbSDimitry Andric     checkAlignment(loc, val, 2, rel);
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric     uint32_t insn = read32le(loc) & 0xFFF;
3530b57cec5SDimitry Andric     uint32_t imm20 = extractBits(val, 20, 20) << 31;
3540b57cec5SDimitry Andric     uint32_t imm10_1 = extractBits(val, 10, 1) << 21;
3550b57cec5SDimitry Andric     uint32_t imm11 = extractBits(val, 11, 11) << 20;
3560b57cec5SDimitry Andric     uint32_t imm19_12 = extractBits(val, 19, 12) << 12;
3570b57cec5SDimitry Andric     insn |= imm20 | imm10_1 | imm11 | imm19_12;
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric     write32le(loc, insn);
3600b57cec5SDimitry Andric     return;
3610b57cec5SDimitry Andric   }
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric   case R_RISCV_BRANCH: {
364*753f127fSDimitry Andric     checkInt(loc, val, 13, rel);
3655ffd83dbSDimitry Andric     checkAlignment(loc, val, 2, rel);
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric     uint32_t insn = read32le(loc) & 0x1FFF07F;
3680b57cec5SDimitry Andric     uint32_t imm12 = extractBits(val, 12, 12) << 31;
3690b57cec5SDimitry Andric     uint32_t imm10_5 = extractBits(val, 10, 5) << 25;
3700b57cec5SDimitry Andric     uint32_t imm4_1 = extractBits(val, 4, 1) << 8;
3710b57cec5SDimitry Andric     uint32_t imm11 = extractBits(val, 11, 11) << 7;
3720b57cec5SDimitry Andric     insn |= imm12 | imm10_5 | imm4_1 | imm11;
3730b57cec5SDimitry Andric 
3740b57cec5SDimitry Andric     write32le(loc, insn);
3750b57cec5SDimitry Andric     return;
3760b57cec5SDimitry Andric   }
3770b57cec5SDimitry Andric 
3780b57cec5SDimitry Andric   // auipc + jalr pair
3790b57cec5SDimitry Andric   case R_RISCV_CALL:
3800b57cec5SDimitry Andric   case R_RISCV_CALL_PLT: {
3810b57cec5SDimitry Andric     int64_t hi = SignExtend64(val + 0x800, bits) >> 12;
3825ffd83dbSDimitry Andric     checkInt(loc, hi, 20, rel);
3830b57cec5SDimitry Andric     if (isInt<20>(hi)) {
3845ffd83dbSDimitry Andric       relocateNoSym(loc, R_RISCV_PCREL_HI20, val);
3855ffd83dbSDimitry Andric       relocateNoSym(loc + 4, R_RISCV_PCREL_LO12_I, val);
3860b57cec5SDimitry Andric     }
3870b57cec5SDimitry Andric     return;
3880b57cec5SDimitry Andric   }
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric   case R_RISCV_GOT_HI20:
3910b57cec5SDimitry Andric   case R_RISCV_PCREL_HI20:
3920b57cec5SDimitry Andric   case R_RISCV_TLS_GD_HI20:
3930b57cec5SDimitry Andric   case R_RISCV_TLS_GOT_HI20:
3940b57cec5SDimitry Andric   case R_RISCV_TPREL_HI20:
3950b57cec5SDimitry Andric   case R_RISCV_HI20: {
3960b57cec5SDimitry Andric     uint64_t hi = val + 0x800;
3975ffd83dbSDimitry Andric     checkInt(loc, SignExtend64(hi, bits) >> 12, 20, rel);
3980b57cec5SDimitry Andric     write32le(loc, (read32le(loc) & 0xFFF) | (hi & 0xFFFFF000));
3990b57cec5SDimitry Andric     return;
4000b57cec5SDimitry Andric   }
4010b57cec5SDimitry Andric 
4020b57cec5SDimitry Andric   case R_RISCV_PCREL_LO12_I:
4030b57cec5SDimitry Andric   case R_RISCV_TPREL_LO12_I:
4040b57cec5SDimitry Andric   case R_RISCV_LO12_I: {
4050b57cec5SDimitry Andric     uint64_t hi = (val + 0x800) >> 12;
4060b57cec5SDimitry Andric     uint64_t lo = val - (hi << 12);
4070b57cec5SDimitry Andric     write32le(loc, (read32le(loc) & 0xFFFFF) | ((lo & 0xFFF) << 20));
4080b57cec5SDimitry Andric     return;
4090b57cec5SDimitry Andric   }
4100b57cec5SDimitry Andric 
4110b57cec5SDimitry Andric   case R_RISCV_PCREL_LO12_S:
4120b57cec5SDimitry Andric   case R_RISCV_TPREL_LO12_S:
4130b57cec5SDimitry Andric   case R_RISCV_LO12_S: {
4140b57cec5SDimitry Andric     uint64_t hi = (val + 0x800) >> 12;
4150b57cec5SDimitry Andric     uint64_t lo = val - (hi << 12);
4160b57cec5SDimitry Andric     uint32_t imm11_5 = extractBits(lo, 11, 5) << 25;
4170b57cec5SDimitry Andric     uint32_t imm4_0 = extractBits(lo, 4, 0) << 7;
4180b57cec5SDimitry Andric     write32le(loc, (read32le(loc) & 0x1FFF07F) | imm11_5 | imm4_0);
4190b57cec5SDimitry Andric     return;
4200b57cec5SDimitry Andric   }
4210b57cec5SDimitry Andric 
4220b57cec5SDimitry Andric   case R_RISCV_ADD8:
4230b57cec5SDimitry Andric     *loc += val;
4240b57cec5SDimitry Andric     return;
4250b57cec5SDimitry Andric   case R_RISCV_ADD16:
4260b57cec5SDimitry Andric     write16le(loc, read16le(loc) + val);
4270b57cec5SDimitry Andric     return;
4280b57cec5SDimitry Andric   case R_RISCV_ADD32:
4290b57cec5SDimitry Andric     write32le(loc, read32le(loc) + val);
4300b57cec5SDimitry Andric     return;
4310b57cec5SDimitry Andric   case R_RISCV_ADD64:
4320b57cec5SDimitry Andric     write64le(loc, read64le(loc) + val);
4330b57cec5SDimitry Andric     return;
4340b57cec5SDimitry Andric   case R_RISCV_SUB6:
4350b57cec5SDimitry Andric     *loc = (*loc & 0xc0) | (((*loc & 0x3f) - val) & 0x3f);
4360b57cec5SDimitry Andric     return;
4370b57cec5SDimitry Andric   case R_RISCV_SUB8:
4380b57cec5SDimitry Andric     *loc -= val;
4390b57cec5SDimitry Andric     return;
4400b57cec5SDimitry Andric   case R_RISCV_SUB16:
4410b57cec5SDimitry Andric     write16le(loc, read16le(loc) - val);
4420b57cec5SDimitry Andric     return;
4430b57cec5SDimitry Andric   case R_RISCV_SUB32:
4440b57cec5SDimitry Andric     write32le(loc, read32le(loc) - val);
4450b57cec5SDimitry Andric     return;
4460b57cec5SDimitry Andric   case R_RISCV_SUB64:
4470b57cec5SDimitry Andric     write64le(loc, read64le(loc) - val);
4480b57cec5SDimitry Andric     return;
4490b57cec5SDimitry Andric   case R_RISCV_SET6:
4500b57cec5SDimitry Andric     *loc = (*loc & 0xc0) | (val & 0x3f);
4510b57cec5SDimitry Andric     return;
4520b57cec5SDimitry Andric   case R_RISCV_SET8:
4530b57cec5SDimitry Andric     *loc = val;
4540b57cec5SDimitry Andric     return;
4550b57cec5SDimitry Andric   case R_RISCV_SET16:
4560b57cec5SDimitry Andric     write16le(loc, val);
4570b57cec5SDimitry Andric     return;
4580b57cec5SDimitry Andric   case R_RISCV_SET32:
4590b57cec5SDimitry Andric   case R_RISCV_32_PCREL:
4600b57cec5SDimitry Andric     write32le(loc, val);
4610b57cec5SDimitry Andric     return;
4620b57cec5SDimitry Andric 
4630b57cec5SDimitry Andric   case R_RISCV_TLS_DTPREL32:
4640b57cec5SDimitry Andric     write32le(loc, val - dtpOffset);
4650b57cec5SDimitry Andric     break;
4660b57cec5SDimitry Andric   case R_RISCV_TLS_DTPREL64:
4670b57cec5SDimitry Andric     write64le(loc, val - dtpOffset);
4680b57cec5SDimitry Andric     break;
4690b57cec5SDimitry Andric 
4700b57cec5SDimitry Andric   case R_RISCV_RELAX:
4710b57cec5SDimitry Andric     return; // Ignored (for now)
4720b57cec5SDimitry Andric 
4730b57cec5SDimitry Andric   default:
474480093f4SDimitry Andric     llvm_unreachable("unknown relocation");
4750b57cec5SDimitry Andric   }
4760b57cec5SDimitry Andric }
4770b57cec5SDimitry Andric 
478*753f127fSDimitry Andric namespace {
479*753f127fSDimitry Andric struct SymbolAnchor {
480*753f127fSDimitry Andric   uint64_t offset;
481*753f127fSDimitry Andric   Defined *d;
482*753f127fSDimitry Andric   bool end; // true for the anchor of st_value+st_size
483*753f127fSDimitry Andric };
484*753f127fSDimitry Andric } // namespace
485*753f127fSDimitry Andric 
486*753f127fSDimitry Andric struct elf::RISCVRelaxAux {
487*753f127fSDimitry Andric   // This records symbol start and end offsets which will be adjusted according
488*753f127fSDimitry Andric   // to the nearest relocDeltas element.
489*753f127fSDimitry Andric   SmallVector<SymbolAnchor, 0> anchors;
490*753f127fSDimitry Andric   // For relocations[i], the actual offset is r_offset - (i ? relocDeltas[i-1] :
491*753f127fSDimitry Andric   // 0).
492*753f127fSDimitry Andric   std::unique_ptr<uint32_t[]> relocDeltas;
493*753f127fSDimitry Andric   // For relocations[i], the actual type is relocTypes[i].
494*753f127fSDimitry Andric   std::unique_ptr<RelType[]> relocTypes;
495*753f127fSDimitry Andric   SmallVector<uint32_t, 0> writes;
496*753f127fSDimitry Andric };
497*753f127fSDimitry Andric 
498*753f127fSDimitry Andric static void initSymbolAnchors() {
499*753f127fSDimitry Andric   SmallVector<InputSection *, 0> storage;
500*753f127fSDimitry Andric   for (OutputSection *osec : outputSections) {
501*753f127fSDimitry Andric     if (!(osec->flags & SHF_EXECINSTR))
502*753f127fSDimitry Andric       continue;
503*753f127fSDimitry Andric     for (InputSection *sec : getInputSections(*osec, storage)) {
504*753f127fSDimitry Andric       sec->relaxAux = make<RISCVRelaxAux>();
505*753f127fSDimitry Andric       if (sec->relocations.size()) {
506*753f127fSDimitry Andric         sec->relaxAux->relocDeltas =
507*753f127fSDimitry Andric             std::make_unique<uint32_t[]>(sec->relocations.size());
508*753f127fSDimitry Andric         sec->relaxAux->relocTypes =
509*753f127fSDimitry Andric             std::make_unique<RelType[]>(sec->relocations.size());
510*753f127fSDimitry Andric       }
511*753f127fSDimitry Andric     }
512*753f127fSDimitry Andric   }
513*753f127fSDimitry Andric   // Store anchors (st_value and st_value+st_size) for symbols relative to text
514*753f127fSDimitry Andric   // sections.
515*753f127fSDimitry Andric   for (InputFile *file : ctx->objectFiles)
516*753f127fSDimitry Andric     for (Symbol *sym : file->getSymbols()) {
517*753f127fSDimitry Andric       auto *d = dyn_cast<Defined>(sym);
518*753f127fSDimitry Andric       if (!d || d->file != file)
519*753f127fSDimitry Andric         continue;
520*753f127fSDimitry Andric       if (auto *sec = dyn_cast_or_null<InputSection>(d->section))
521*753f127fSDimitry Andric         if (sec->flags & SHF_EXECINSTR && sec->relaxAux) {
522*753f127fSDimitry Andric           // If sec is discarded, relaxAux will be nullptr.
523*753f127fSDimitry Andric           sec->relaxAux->anchors.push_back({d->value, d, false});
524*753f127fSDimitry Andric           sec->relaxAux->anchors.push_back({d->value + d->size, d, true});
525*753f127fSDimitry Andric         }
526*753f127fSDimitry Andric     }
527*753f127fSDimitry Andric   // Sort anchors by offset so that we can find the closest relocation
528*753f127fSDimitry Andric   // efficiently. For a zero size symbol, ensure that its start anchor precedes
529*753f127fSDimitry Andric   // its end anchor. For two symbols with anchors at the same offset, their
530*753f127fSDimitry Andric   // order does not matter.
531*753f127fSDimitry Andric   for (OutputSection *osec : outputSections) {
532*753f127fSDimitry Andric     if (!(osec->flags & SHF_EXECINSTR))
533*753f127fSDimitry Andric       continue;
534*753f127fSDimitry Andric     for (InputSection *sec : getInputSections(*osec, storage)) {
535*753f127fSDimitry Andric       llvm::sort(sec->relaxAux->anchors, [](auto &a, auto &b) {
536*753f127fSDimitry Andric         return std::make_pair(a.offset, a.end) <
537*753f127fSDimitry Andric                std::make_pair(b.offset, b.end);
538*753f127fSDimitry Andric       });
539*753f127fSDimitry Andric     }
540*753f127fSDimitry Andric   }
541*753f127fSDimitry Andric }
542*753f127fSDimitry Andric 
543*753f127fSDimitry Andric // Relax R_RISCV_CALL/R_RISCV_CALL_PLT auipc+jalr to c.j, c.jal, or jal.
544*753f127fSDimitry Andric static void relaxCall(const InputSection &sec, size_t i, uint64_t loc,
545*753f127fSDimitry Andric                       Relocation &r, uint32_t &remove) {
546*753f127fSDimitry Andric   const bool rvc = config->eflags & EF_RISCV_RVC;
547*753f127fSDimitry Andric   const Symbol &sym = *r.sym;
548*753f127fSDimitry Andric   const uint64_t insnPair = read64le(sec.rawData.data() + r.offset);
549*753f127fSDimitry Andric   const uint32_t rd = extractBits(insnPair, 32 + 11, 32 + 7);
550*753f127fSDimitry Andric   const uint64_t dest =
551*753f127fSDimitry Andric       (r.expr == R_PLT_PC ? sym.getPltVA() : sym.getVA()) + r.addend;
552*753f127fSDimitry Andric   const int64_t displace = dest - loc;
553*753f127fSDimitry Andric 
554*753f127fSDimitry Andric   if (rvc && isInt<12>(displace) && rd == 0) {
555*753f127fSDimitry Andric     sec.relaxAux->relocTypes[i] = R_RISCV_RVC_JUMP;
556*753f127fSDimitry Andric     sec.relaxAux->writes.push_back(0xa001); // c.j
557*753f127fSDimitry Andric     remove = 6;
558*753f127fSDimitry Andric   } else if (rvc && isInt<12>(displace) && rd == X_RA &&
559*753f127fSDimitry Andric              !config->is64) { // RV32C only
560*753f127fSDimitry Andric     sec.relaxAux->relocTypes[i] = R_RISCV_RVC_JUMP;
561*753f127fSDimitry Andric     sec.relaxAux->writes.push_back(0x2001); // c.jal
562*753f127fSDimitry Andric     remove = 6;
563*753f127fSDimitry Andric   } else if (isInt<21>(displace)) {
564*753f127fSDimitry Andric     sec.relaxAux->relocTypes[i] = R_RISCV_JAL;
565*753f127fSDimitry Andric     sec.relaxAux->writes.push_back(0x6f | rd << 7); // jal
566*753f127fSDimitry Andric     remove = 4;
567*753f127fSDimitry Andric   }
568*753f127fSDimitry Andric }
569*753f127fSDimitry Andric 
570*753f127fSDimitry Andric static bool relax(InputSection &sec) {
571*753f127fSDimitry Andric   const uint64_t secAddr = sec.getVA();
572*753f127fSDimitry Andric   auto &aux = *sec.relaxAux;
573*753f127fSDimitry Andric   bool changed = false;
574*753f127fSDimitry Andric 
575*753f127fSDimitry Andric   // Get st_value delta for symbols relative to this section from the previous
576*753f127fSDimitry Andric   // iteration.
577*753f127fSDimitry Andric   DenseMap<const Defined *, uint64_t> valueDelta;
578*753f127fSDimitry Andric   ArrayRef<SymbolAnchor> sa = makeArrayRef(aux.anchors);
579*753f127fSDimitry Andric   uint32_t delta = 0;
580*753f127fSDimitry Andric   for (auto it : llvm::enumerate(sec.relocations)) {
581*753f127fSDimitry Andric     for (; sa.size() && sa[0].offset <= it.value().offset; sa = sa.slice(1))
582*753f127fSDimitry Andric       if (!sa[0].end)
583*753f127fSDimitry Andric         valueDelta[sa[0].d] = delta;
584*753f127fSDimitry Andric     delta = aux.relocDeltas[it.index()];
585*753f127fSDimitry Andric   }
586*753f127fSDimitry Andric   for (const SymbolAnchor &sa : sa)
587*753f127fSDimitry Andric     if (!sa.end)
588*753f127fSDimitry Andric       valueDelta[sa.d] = delta;
589*753f127fSDimitry Andric   sa = makeArrayRef(aux.anchors);
590*753f127fSDimitry Andric   delta = 0;
591*753f127fSDimitry Andric 
592*753f127fSDimitry Andric   std::fill_n(aux.relocTypes.get(), sec.relocations.size(), R_RISCV_NONE);
593*753f127fSDimitry Andric   aux.writes.clear();
594*753f127fSDimitry Andric   for (auto it : llvm::enumerate(sec.relocations)) {
595*753f127fSDimitry Andric     Relocation &r = it.value();
596*753f127fSDimitry Andric     const size_t i = it.index();
597*753f127fSDimitry Andric     const uint64_t loc = secAddr + r.offset - delta;
598*753f127fSDimitry Andric     uint32_t &cur = aux.relocDeltas[i], remove = 0;
599*753f127fSDimitry Andric     switch (r.type) {
600*753f127fSDimitry Andric     case R_RISCV_ALIGN: {
601*753f127fSDimitry Andric       const uint64_t nextLoc = loc + r.addend;
602*753f127fSDimitry Andric       const uint64_t align = PowerOf2Ceil(r.addend + 2);
603*753f127fSDimitry Andric       // All bytes beyond the alignment boundary should be removed.
604*753f127fSDimitry Andric       remove = nextLoc - ((loc + align - 1) & -align);
605*753f127fSDimitry Andric       assert(static_cast<int32_t>(remove) >= 0 &&
606*753f127fSDimitry Andric              "R_RISCV_ALIGN needs expanding the content");
607*753f127fSDimitry Andric       break;
608*753f127fSDimitry Andric     }
609*753f127fSDimitry Andric     case R_RISCV_CALL:
610*753f127fSDimitry Andric     case R_RISCV_CALL_PLT:
611*753f127fSDimitry Andric       if (i + 1 != sec.relocations.size() &&
612*753f127fSDimitry Andric           sec.relocations[i + 1].type == R_RISCV_RELAX)
613*753f127fSDimitry Andric         relaxCall(sec, i, loc, r, remove);
614*753f127fSDimitry Andric       break;
615*753f127fSDimitry Andric     }
616*753f127fSDimitry Andric 
617*753f127fSDimitry Andric     // For all anchors whose offsets are <= r.offset, they are preceded by
618*753f127fSDimitry Andric     // the previous relocation whose `relocDeltas` value equals `delta`.
619*753f127fSDimitry Andric     // Decrease their st_value and update their st_size.
620*753f127fSDimitry Andric     for (; sa.size() && sa[0].offset <= r.offset; sa = sa.slice(1)) {
621*753f127fSDimitry Andric       if (sa[0].end)
622*753f127fSDimitry Andric         sa[0].d->size = sa[0].offset - delta - sa[0].d->value;
623*753f127fSDimitry Andric       else
624*753f127fSDimitry Andric         sa[0].d->value -= delta - valueDelta.find(sa[0].d)->second;
625*753f127fSDimitry Andric     }
626*753f127fSDimitry Andric     delta += remove;
627*753f127fSDimitry Andric     if (delta != cur) {
628*753f127fSDimitry Andric       cur = delta;
629*753f127fSDimitry Andric       changed = true;
630*753f127fSDimitry Andric     }
631*753f127fSDimitry Andric   }
632*753f127fSDimitry Andric 
633*753f127fSDimitry Andric   for (const SymbolAnchor &a : sa) {
634*753f127fSDimitry Andric     if (a.end)
635*753f127fSDimitry Andric       a.d->size = a.offset - delta - a.d->value;
636*753f127fSDimitry Andric     else
637*753f127fSDimitry Andric       a.d->value -= delta - valueDelta.find(a.d)->second;
638*753f127fSDimitry Andric   }
639*753f127fSDimitry Andric   // Inform assignAddresses that the size has changed.
640*753f127fSDimitry Andric   if (!isUInt<16>(delta))
641*753f127fSDimitry Andric     fatal("section size decrease is too large");
642*753f127fSDimitry Andric   sec.bytesDropped = delta;
643*753f127fSDimitry Andric   return changed;
644*753f127fSDimitry Andric }
645*753f127fSDimitry Andric 
646*753f127fSDimitry Andric // When relaxing just R_RISCV_ALIGN, relocDeltas is usually changed only once in
647*753f127fSDimitry Andric // the absence of a linker script. For call and load/store R_RISCV_RELAX, code
648*753f127fSDimitry Andric // shrinkage may reduce displacement and make more relocations eligible for
649*753f127fSDimitry Andric // relaxation. Code shrinkage may increase displacement to a call/load/store
650*753f127fSDimitry Andric // target at a higher fixed address, invalidating an earlier relaxation. Any
651*753f127fSDimitry Andric // change in section sizes can have cascading effect and require another
652*753f127fSDimitry Andric // relaxation pass.
653*753f127fSDimitry Andric bool RISCV::relaxOnce(int pass) const {
654*753f127fSDimitry Andric   llvm::TimeTraceScope timeScope("RISC-V relaxOnce");
655*753f127fSDimitry Andric   if (config->relocatable)
656*753f127fSDimitry Andric     return false;
657*753f127fSDimitry Andric 
658*753f127fSDimitry Andric   if (pass == 0)
659*753f127fSDimitry Andric     initSymbolAnchors();
660*753f127fSDimitry Andric 
661*753f127fSDimitry Andric   SmallVector<InputSection *, 0> storage;
662*753f127fSDimitry Andric   bool changed = false;
663*753f127fSDimitry Andric   for (OutputSection *osec : outputSections) {
664*753f127fSDimitry Andric     if (!(osec->flags & SHF_EXECINSTR))
665*753f127fSDimitry Andric       continue;
666*753f127fSDimitry Andric     for (InputSection *sec : getInputSections(*osec, storage))
667*753f127fSDimitry Andric       changed |= relax(*sec);
668*753f127fSDimitry Andric   }
669*753f127fSDimitry Andric   return changed;
670*753f127fSDimitry Andric }
671*753f127fSDimitry Andric 
672*753f127fSDimitry Andric void elf::riscvFinalizeRelax(int passes) {
673*753f127fSDimitry Andric   llvm::TimeTraceScope timeScope("Finalize RISC-V relaxation");
674*753f127fSDimitry Andric   log("relaxation passes: " + Twine(passes));
675*753f127fSDimitry Andric   SmallVector<InputSection *, 0> storage;
676*753f127fSDimitry Andric   for (OutputSection *osec : outputSections) {
677*753f127fSDimitry Andric     if (!(osec->flags & SHF_EXECINSTR))
678*753f127fSDimitry Andric       continue;
679*753f127fSDimitry Andric     for (InputSection *sec : getInputSections(*osec, storage)) {
680*753f127fSDimitry Andric       RISCVRelaxAux &aux = *sec->relaxAux;
681*753f127fSDimitry Andric       if (!aux.relocDeltas)
682*753f127fSDimitry Andric         continue;
683*753f127fSDimitry Andric 
684*753f127fSDimitry Andric       auto &rels = sec->relocations;
685*753f127fSDimitry Andric       ArrayRef<uint8_t> old = sec->rawData;
686*753f127fSDimitry Andric       size_t newSize =
687*753f127fSDimitry Andric           old.size() - aux.relocDeltas[sec->relocations.size() - 1];
688*753f127fSDimitry Andric       size_t writesIdx = 0;
689*753f127fSDimitry Andric       uint8_t *p = context().bAlloc.Allocate<uint8_t>(newSize);
690*753f127fSDimitry Andric       uint64_t offset = 0;
691*753f127fSDimitry Andric       int64_t delta = 0;
692*753f127fSDimitry Andric       sec->rawData = makeArrayRef(p, newSize);
693*753f127fSDimitry Andric       sec->bytesDropped = 0;
694*753f127fSDimitry Andric 
695*753f127fSDimitry Andric       // Update section content: remove NOPs for R_RISCV_ALIGN and rewrite
696*753f127fSDimitry Andric       // instructions for relaxed relocations.
697*753f127fSDimitry Andric       for (size_t i = 0, e = rels.size(); i != e; ++i) {
698*753f127fSDimitry Andric         uint32_t remove = aux.relocDeltas[i] - delta;
699*753f127fSDimitry Andric         delta = aux.relocDeltas[i];
700*753f127fSDimitry Andric         if (remove == 0)
701*753f127fSDimitry Andric           continue;
702*753f127fSDimitry Andric 
703*753f127fSDimitry Andric         // Copy from last location to the current relocated location.
704*753f127fSDimitry Andric         const Relocation &r = rels[i];
705*753f127fSDimitry Andric         uint64_t size = r.offset - offset;
706*753f127fSDimitry Andric         memcpy(p, old.data() + offset, size);
707*753f127fSDimitry Andric         p += size;
708*753f127fSDimitry Andric 
709*753f127fSDimitry Andric         // For R_RISCV_ALIGN, we will place `offset` in a location (among NOPs)
710*753f127fSDimitry Andric         // to satisfy the alignment requirement. If `remove` is a multiple of 4,
711*753f127fSDimitry Andric         // it is as if we have skipped some NOPs. Otherwise we are in the middle
712*753f127fSDimitry Andric         // of a 4-byte NOP, and we need to rewrite the NOP sequence.
713*753f127fSDimitry Andric         int64_t skip = 0;
714*753f127fSDimitry Andric         if (r.type == R_RISCV_ALIGN) {
715*753f127fSDimitry Andric           if (remove % 4 != 0) {
716*753f127fSDimitry Andric             skip = r.addend - remove;
717*753f127fSDimitry Andric             int64_t j = 0;
718*753f127fSDimitry Andric             for (; j + 4 <= skip; j += 4)
719*753f127fSDimitry Andric               write32le(p + j, 0x00000013); // nop
720*753f127fSDimitry Andric             if (j != skip) {
721*753f127fSDimitry Andric               assert(j + 2 == skip);
722*753f127fSDimitry Andric               write16le(p + j, 0x0001); // c.nop
723*753f127fSDimitry Andric             }
724*753f127fSDimitry Andric           }
725*753f127fSDimitry Andric         } else if (RelType newType = aux.relocTypes[i]) {
726*753f127fSDimitry Andric           const uint32_t insn = aux.writes[writesIdx++];
727*753f127fSDimitry Andric           switch (newType) {
728*753f127fSDimitry Andric           case R_RISCV_RVC_JUMP:
729*753f127fSDimitry Andric             skip = 2;
730*753f127fSDimitry Andric             write16le(p, insn);
731*753f127fSDimitry Andric             break;
732*753f127fSDimitry Andric           case R_RISCV_JAL:
733*753f127fSDimitry Andric             skip = 4;
734*753f127fSDimitry Andric             write32le(p, insn);
735*753f127fSDimitry Andric             break;
736*753f127fSDimitry Andric           default:
737*753f127fSDimitry Andric             llvm_unreachable("unsupported type");
738*753f127fSDimitry Andric           }
739*753f127fSDimitry Andric         }
740*753f127fSDimitry Andric 
741*753f127fSDimitry Andric         p += skip;
742*753f127fSDimitry Andric         offset = r.offset + skip + remove;
743*753f127fSDimitry Andric       }
744*753f127fSDimitry Andric       memcpy(p, old.data() + offset, old.size() - offset);
745*753f127fSDimitry Andric 
746*753f127fSDimitry Andric       // Subtract the previous relocDeltas value from the relocation offset.
747*753f127fSDimitry Andric       // For a pair of R_RISCV_CALL/R_RISCV_RELAX with the same offset, decrease
748*753f127fSDimitry Andric       // their r_offset by the same delta.
749*753f127fSDimitry Andric       delta = 0;
750*753f127fSDimitry Andric       for (size_t i = 0, e = rels.size(); i != e;) {
751*753f127fSDimitry Andric         uint64_t cur = rels[i].offset;
752*753f127fSDimitry Andric         do {
753*753f127fSDimitry Andric           rels[i].offset -= delta;
754*753f127fSDimitry Andric           if (aux.relocTypes[i] != R_RISCV_NONE)
755*753f127fSDimitry Andric             rels[i].type = aux.relocTypes[i];
756*753f127fSDimitry Andric         } while (++i != e && rels[i].offset == cur);
757*753f127fSDimitry Andric         delta = aux.relocDeltas[i - 1];
758*753f127fSDimitry Andric       }
759*753f127fSDimitry Andric     }
760*753f127fSDimitry Andric   }
761*753f127fSDimitry Andric }
762*753f127fSDimitry Andric 
7635ffd83dbSDimitry Andric TargetInfo *elf::getRISCVTargetInfo() {
7640b57cec5SDimitry Andric   static RISCV target;
7650b57cec5SDimitry Andric   return &target;
7660b57cec5SDimitry Andric }
767