xref: /freebsd/contrib/llvm-project/lld/ELF/Arch/RISCV.cpp (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
1 //===- RISCV.cpp ----------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "InputFiles.h"
10 #include "OutputSections.h"
11 #include "Symbols.h"
12 #include "SyntheticSections.h"
13 #include "Target.h"
14 #include "llvm/Support/ELFAttributes.h"
15 #include "llvm/Support/LEB128.h"
16 #include "llvm/Support/RISCVAttributeParser.h"
17 #include "llvm/Support/RISCVAttributes.h"
18 #include "llvm/Support/RISCVISAInfo.h"
19 #include "llvm/Support/TimeProfiler.h"
20 
21 using namespace llvm;
22 using namespace llvm::object;
23 using namespace llvm::support::endian;
24 using namespace llvm::ELF;
25 using namespace lld;
26 using namespace lld::elf;
27 
28 namespace {
29 
30 class RISCV final : public TargetInfo {
31 public:
32   RISCV();
33   uint32_t calcEFlags() const override;
34   int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override;
35   void writeGotHeader(uint8_t *buf) const override;
36   void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
37   void writeIgotPlt(uint8_t *buf, const Symbol &s) const override;
38   void writePltHeader(uint8_t *buf) const override;
39   void writePlt(uint8_t *buf, const Symbol &sym,
40                 uint64_t pltEntryAddr) const override;
41   RelType getDynRel(RelType type) const override;
42   RelExpr getRelExpr(RelType type, const Symbol &s,
43                      const uint8_t *loc) const override;
44   void relocate(uint8_t *loc, const Relocation &rel,
45                 uint64_t val) const override;
46   void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override;
47   bool relaxOnce(int pass) const override;
48 };
49 
50 } // end anonymous namespace
51 
52 // These are internal relocation numbers for GP relaxation. They aren't part
53 // of the psABI spec.
54 #define INTERNAL_R_RISCV_GPREL_I 256
55 #define INTERNAL_R_RISCV_GPREL_S 257
56 
57 const uint64_t dtpOffset = 0x800;
58 
59 enum Op {
60   ADDI = 0x13,
61   AUIPC = 0x17,
62   JALR = 0x67,
63   LD = 0x3003,
64   LW = 0x2003,
65   SRLI = 0x5013,
66   SUB = 0x40000033,
67 };
68 
69 enum Reg {
70   X_RA = 1,
71   X_GP = 3,
72   X_TP = 4,
73   X_T0 = 5,
74   X_T1 = 6,
75   X_T2 = 7,
76   X_T3 = 28,
77 };
78 
79 static uint32_t hi20(uint32_t val) { return (val + 0x800) >> 12; }
80 static uint32_t lo12(uint32_t val) { return val & 4095; }
81 
82 static uint32_t itype(uint32_t op, uint32_t rd, uint32_t rs1, uint32_t imm) {
83   return op | (rd << 7) | (rs1 << 15) | (imm << 20);
84 }
85 static uint32_t rtype(uint32_t op, uint32_t rd, uint32_t rs1, uint32_t rs2) {
86   return op | (rd << 7) | (rs1 << 15) | (rs2 << 20);
87 }
88 static uint32_t utype(uint32_t op, uint32_t rd, uint32_t imm) {
89   return op | (rd << 7) | (imm << 12);
90 }
91 
92 // Extract bits v[begin:end], where range is inclusive, and begin must be < 63.
93 static uint32_t extractBits(uint64_t v, uint32_t begin, uint32_t end) {
94   return (v & ((1ULL << (begin + 1)) - 1)) >> end;
95 }
96 
97 static uint32_t setLO12_I(uint32_t insn, uint32_t imm) {
98   return (insn & 0xfffff) | (imm << 20);
99 }
100 static uint32_t setLO12_S(uint32_t insn, uint32_t imm) {
101   return (insn & 0x1fff07f) | (extractBits(imm, 11, 5) << 25) |
102          (extractBits(imm, 4, 0) << 7);
103 }
104 
105 RISCV::RISCV() {
106   copyRel = R_RISCV_COPY;
107   pltRel = R_RISCV_JUMP_SLOT;
108   relativeRel = R_RISCV_RELATIVE;
109   iRelativeRel = R_RISCV_IRELATIVE;
110   if (config->is64) {
111     symbolicRel = R_RISCV_64;
112     tlsModuleIndexRel = R_RISCV_TLS_DTPMOD64;
113     tlsOffsetRel = R_RISCV_TLS_DTPREL64;
114     tlsGotRel = R_RISCV_TLS_TPREL64;
115   } else {
116     symbolicRel = R_RISCV_32;
117     tlsModuleIndexRel = R_RISCV_TLS_DTPMOD32;
118     tlsOffsetRel = R_RISCV_TLS_DTPREL32;
119     tlsGotRel = R_RISCV_TLS_TPREL32;
120   }
121   gotRel = symbolicRel;
122 
123   // .got[0] = _DYNAMIC
124   gotHeaderEntriesNum = 1;
125 
126   // .got.plt[0] = _dl_runtime_resolve, .got.plt[1] = link_map
127   gotPltHeaderEntriesNum = 2;
128 
129   pltHeaderSize = 32;
130   pltEntrySize = 16;
131   ipltEntrySize = 16;
132 }
133 
134 static uint32_t getEFlags(InputFile *f) {
135   if (config->is64)
136     return cast<ObjFile<ELF64LE>>(f)->getObj().getHeader().e_flags;
137   return cast<ObjFile<ELF32LE>>(f)->getObj().getHeader().e_flags;
138 }
139 
140 uint32_t RISCV::calcEFlags() const {
141   // If there are only binary input files (from -b binary), use a
142   // value of 0 for the ELF header flags.
143   if (ctx.objectFiles.empty())
144     return 0;
145 
146   uint32_t target = getEFlags(ctx.objectFiles.front());
147 
148   for (InputFile *f : ctx.objectFiles) {
149     uint32_t eflags = getEFlags(f);
150     if (eflags & EF_RISCV_RVC)
151       target |= EF_RISCV_RVC;
152 
153     if ((eflags & EF_RISCV_FLOAT_ABI) != (target & EF_RISCV_FLOAT_ABI))
154       error(
155           toString(f) +
156           ": cannot link object files with different floating-point ABI from " +
157           toString(ctx.objectFiles[0]));
158 
159     if ((eflags & EF_RISCV_RVE) != (target & EF_RISCV_RVE))
160       error(toString(f) +
161             ": cannot link object files with different EF_RISCV_RVE");
162   }
163 
164   return target;
165 }
166 
167 int64_t RISCV::getImplicitAddend(const uint8_t *buf, RelType type) const {
168   switch (type) {
169   default:
170     internalLinkerError(getErrorLocation(buf),
171                         "cannot read addend for relocation " + toString(type));
172     return 0;
173   case R_RISCV_32:
174   case R_RISCV_TLS_DTPMOD32:
175   case R_RISCV_TLS_DTPREL32:
176   case R_RISCV_TLS_TPREL32:
177     return SignExtend64<32>(read32le(buf));
178   case R_RISCV_64:
179   case R_RISCV_TLS_DTPMOD64:
180   case R_RISCV_TLS_DTPREL64:
181   case R_RISCV_TLS_TPREL64:
182     return read64le(buf);
183   case R_RISCV_RELATIVE:
184   case R_RISCV_IRELATIVE:
185     return config->is64 ? read64le(buf) : read32le(buf);
186   case R_RISCV_NONE:
187   case R_RISCV_JUMP_SLOT:
188     // These relocations are defined as not having an implicit addend.
189     return 0;
190   }
191 }
192 
193 void RISCV::writeGotHeader(uint8_t *buf) const {
194   if (config->is64)
195     write64le(buf, mainPart->dynamic->getVA());
196   else
197     write32le(buf, mainPart->dynamic->getVA());
198 }
199 
200 void RISCV::writeGotPlt(uint8_t *buf, const Symbol &s) const {
201   if (config->is64)
202     write64le(buf, in.plt->getVA());
203   else
204     write32le(buf, in.plt->getVA());
205 }
206 
207 void RISCV::writeIgotPlt(uint8_t *buf, const Symbol &s) const {
208   if (config->writeAddends) {
209     if (config->is64)
210       write64le(buf, s.getVA());
211     else
212       write32le(buf, s.getVA());
213   }
214 }
215 
216 void RISCV::writePltHeader(uint8_t *buf) const {
217   // 1: auipc t2, %pcrel_hi(.got.plt)
218   // sub t1, t1, t3
219   // l[wd] t3, %pcrel_lo(1b)(t2); t3 = _dl_runtime_resolve
220   // addi t1, t1, -pltHeaderSize-12; t1 = &.plt[i] - &.plt[0]
221   // addi t0, t2, %pcrel_lo(1b)
222   // srli t1, t1, (rv64?1:2); t1 = &.got.plt[i] - &.got.plt[0]
223   // l[wd] t0, Wordsize(t0); t0 = link_map
224   // jr t3
225   uint32_t offset = in.gotPlt->getVA() - in.plt->getVA();
226   uint32_t load = config->is64 ? LD : LW;
227   write32le(buf + 0, utype(AUIPC, X_T2, hi20(offset)));
228   write32le(buf + 4, rtype(SUB, X_T1, X_T1, X_T3));
229   write32le(buf + 8, itype(load, X_T3, X_T2, lo12(offset)));
230   write32le(buf + 12, itype(ADDI, X_T1, X_T1, -target->pltHeaderSize - 12));
231   write32le(buf + 16, itype(ADDI, X_T0, X_T2, lo12(offset)));
232   write32le(buf + 20, itype(SRLI, X_T1, X_T1, config->is64 ? 1 : 2));
233   write32le(buf + 24, itype(load, X_T0, X_T0, config->wordsize));
234   write32le(buf + 28, itype(JALR, 0, X_T3, 0));
235 }
236 
237 void RISCV::writePlt(uint8_t *buf, const Symbol &sym,
238                      uint64_t pltEntryAddr) const {
239   // 1: auipc t3, %pcrel_hi(f@.got.plt)
240   // l[wd] t3, %pcrel_lo(1b)(t3)
241   // jalr t1, t3
242   // nop
243   uint32_t offset = sym.getGotPltVA() - pltEntryAddr;
244   write32le(buf + 0, utype(AUIPC, X_T3, hi20(offset)));
245   write32le(buf + 4, itype(config->is64 ? LD : LW, X_T3, X_T3, lo12(offset)));
246   write32le(buf + 8, itype(JALR, X_T1, X_T3, 0));
247   write32le(buf + 12, itype(ADDI, 0, 0, 0));
248 }
249 
250 RelType RISCV::getDynRel(RelType type) const {
251   return type == target->symbolicRel ? type
252                                      : static_cast<RelType>(R_RISCV_NONE);
253 }
254 
255 RelExpr RISCV::getRelExpr(const RelType type, const Symbol &s,
256                           const uint8_t *loc) const {
257   switch (type) {
258   case R_RISCV_NONE:
259     return R_NONE;
260   case R_RISCV_32:
261   case R_RISCV_64:
262   case R_RISCV_HI20:
263   case R_RISCV_LO12_I:
264   case R_RISCV_LO12_S:
265   case R_RISCV_RVC_LUI:
266     return R_ABS;
267   case R_RISCV_ADD8:
268   case R_RISCV_ADD16:
269   case R_RISCV_ADD32:
270   case R_RISCV_ADD64:
271   case R_RISCV_SET6:
272   case R_RISCV_SET8:
273   case R_RISCV_SET16:
274   case R_RISCV_SET32:
275   case R_RISCV_SUB6:
276   case R_RISCV_SUB8:
277   case R_RISCV_SUB16:
278   case R_RISCV_SUB32:
279   case R_RISCV_SUB64:
280     return R_RISCV_ADD;
281   case R_RISCV_JAL:
282   case R_RISCV_BRANCH:
283   case R_RISCV_PCREL_HI20:
284   case R_RISCV_RVC_BRANCH:
285   case R_RISCV_RVC_JUMP:
286   case R_RISCV_32_PCREL:
287     return R_PC;
288   case R_RISCV_CALL:
289   case R_RISCV_CALL_PLT:
290   case R_RISCV_PLT32:
291     return R_PLT_PC;
292   case R_RISCV_GOT_HI20:
293     return R_GOT_PC;
294   case R_RISCV_PCREL_LO12_I:
295   case R_RISCV_PCREL_LO12_S:
296     return R_RISCV_PC_INDIRECT;
297   case R_RISCV_TLS_GD_HI20:
298     return R_TLSGD_PC;
299   case R_RISCV_TLS_GOT_HI20:
300     return R_GOT_PC;
301   case R_RISCV_TPREL_HI20:
302   case R_RISCV_TPREL_LO12_I:
303   case R_RISCV_TPREL_LO12_S:
304     return R_TPREL;
305   case R_RISCV_ALIGN:
306     return R_RELAX_HINT;
307   case R_RISCV_TPREL_ADD:
308   case R_RISCV_RELAX:
309     return config->relax ? R_RELAX_HINT : R_NONE;
310   case R_RISCV_SET_ULEB128:
311   case R_RISCV_SUB_ULEB128:
312     return R_RISCV_LEB128;
313   default:
314     error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
315           ") against symbol " + toString(s));
316     return R_NONE;
317   }
318 }
319 
320 void RISCV::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
321   const unsigned bits = config->wordsize * 8;
322 
323   switch (rel.type) {
324   case R_RISCV_32:
325     write32le(loc, val);
326     return;
327   case R_RISCV_64:
328     write64le(loc, val);
329     return;
330 
331   case R_RISCV_RVC_BRANCH: {
332     checkInt(loc, val, 9, rel);
333     checkAlignment(loc, val, 2, rel);
334     uint16_t insn = read16le(loc) & 0xE383;
335     uint16_t imm8 = extractBits(val, 8, 8) << 12;
336     uint16_t imm4_3 = extractBits(val, 4, 3) << 10;
337     uint16_t imm7_6 = extractBits(val, 7, 6) << 5;
338     uint16_t imm2_1 = extractBits(val, 2, 1) << 3;
339     uint16_t imm5 = extractBits(val, 5, 5) << 2;
340     insn |= imm8 | imm4_3 | imm7_6 | imm2_1 | imm5;
341 
342     write16le(loc, insn);
343     return;
344   }
345 
346   case R_RISCV_RVC_JUMP: {
347     checkInt(loc, val, 12, rel);
348     checkAlignment(loc, val, 2, rel);
349     uint16_t insn = read16le(loc) & 0xE003;
350     uint16_t imm11 = extractBits(val, 11, 11) << 12;
351     uint16_t imm4 = extractBits(val, 4, 4) << 11;
352     uint16_t imm9_8 = extractBits(val, 9, 8) << 9;
353     uint16_t imm10 = extractBits(val, 10, 10) << 8;
354     uint16_t imm6 = extractBits(val, 6, 6) << 7;
355     uint16_t imm7 = extractBits(val, 7, 7) << 6;
356     uint16_t imm3_1 = extractBits(val, 3, 1) << 3;
357     uint16_t imm5 = extractBits(val, 5, 5) << 2;
358     insn |= imm11 | imm4 | imm9_8 | imm10 | imm6 | imm7 | imm3_1 | imm5;
359 
360     write16le(loc, insn);
361     return;
362   }
363 
364   case R_RISCV_RVC_LUI: {
365     int64_t imm = SignExtend64(val + 0x800, bits) >> 12;
366     checkInt(loc, imm, 6, rel);
367     if (imm == 0) { // `c.lui rd, 0` is illegal, convert to `c.li rd, 0`
368       write16le(loc, (read16le(loc) & 0x0F83) | 0x4000);
369     } else {
370       uint16_t imm17 = extractBits(val + 0x800, 17, 17) << 12;
371       uint16_t imm16_12 = extractBits(val + 0x800, 16, 12) << 2;
372       write16le(loc, (read16le(loc) & 0xEF83) | imm17 | imm16_12);
373     }
374     return;
375   }
376 
377   case R_RISCV_JAL: {
378     checkInt(loc, val, 21, rel);
379     checkAlignment(loc, val, 2, rel);
380 
381     uint32_t insn = read32le(loc) & 0xFFF;
382     uint32_t imm20 = extractBits(val, 20, 20) << 31;
383     uint32_t imm10_1 = extractBits(val, 10, 1) << 21;
384     uint32_t imm11 = extractBits(val, 11, 11) << 20;
385     uint32_t imm19_12 = extractBits(val, 19, 12) << 12;
386     insn |= imm20 | imm10_1 | imm11 | imm19_12;
387 
388     write32le(loc, insn);
389     return;
390   }
391 
392   case R_RISCV_BRANCH: {
393     checkInt(loc, val, 13, rel);
394     checkAlignment(loc, val, 2, rel);
395 
396     uint32_t insn = read32le(loc) & 0x1FFF07F;
397     uint32_t imm12 = extractBits(val, 12, 12) << 31;
398     uint32_t imm10_5 = extractBits(val, 10, 5) << 25;
399     uint32_t imm4_1 = extractBits(val, 4, 1) << 8;
400     uint32_t imm11 = extractBits(val, 11, 11) << 7;
401     insn |= imm12 | imm10_5 | imm4_1 | imm11;
402 
403     write32le(loc, insn);
404     return;
405   }
406 
407   // auipc + jalr pair
408   case R_RISCV_CALL:
409   case R_RISCV_CALL_PLT: {
410     int64_t hi = SignExtend64(val + 0x800, bits) >> 12;
411     checkInt(loc, hi, 20, rel);
412     if (isInt<20>(hi)) {
413       relocateNoSym(loc, R_RISCV_PCREL_HI20, val);
414       relocateNoSym(loc + 4, R_RISCV_PCREL_LO12_I, val);
415     }
416     return;
417   }
418 
419   case R_RISCV_GOT_HI20:
420   case R_RISCV_PCREL_HI20:
421   case R_RISCV_TLS_GD_HI20:
422   case R_RISCV_TLS_GOT_HI20:
423   case R_RISCV_TPREL_HI20:
424   case R_RISCV_HI20: {
425     uint64_t hi = val + 0x800;
426     checkInt(loc, SignExtend64(hi, bits) >> 12, 20, rel);
427     write32le(loc, (read32le(loc) & 0xFFF) | (hi & 0xFFFFF000));
428     return;
429   }
430 
431   case R_RISCV_PCREL_LO12_I:
432   case R_RISCV_TPREL_LO12_I:
433   case R_RISCV_LO12_I: {
434     uint64_t hi = (val + 0x800) >> 12;
435     uint64_t lo = val - (hi << 12);
436     write32le(loc, setLO12_I(read32le(loc), lo & 0xfff));
437     return;
438   }
439 
440   case R_RISCV_PCREL_LO12_S:
441   case R_RISCV_TPREL_LO12_S:
442   case R_RISCV_LO12_S: {
443     uint64_t hi = (val + 0x800) >> 12;
444     uint64_t lo = val - (hi << 12);
445     write32le(loc, setLO12_S(read32le(loc), lo));
446     return;
447   }
448 
449   case INTERNAL_R_RISCV_GPREL_I:
450   case INTERNAL_R_RISCV_GPREL_S: {
451     Defined *gp = ElfSym::riscvGlobalPointer;
452     int64_t displace = SignExtend64(val - gp->getVA(), bits);
453     checkInt(loc, displace, 12, rel);
454     uint32_t insn = (read32le(loc) & ~(31 << 15)) | (X_GP << 15);
455     if (rel.type == INTERNAL_R_RISCV_GPREL_I)
456       insn = setLO12_I(insn, displace);
457     else
458       insn = setLO12_S(insn, displace);
459     write32le(loc, insn);
460     return;
461   }
462 
463   case R_RISCV_ADD8:
464     *loc += val;
465     return;
466   case R_RISCV_ADD16:
467     write16le(loc, read16le(loc) + val);
468     return;
469   case R_RISCV_ADD32:
470     write32le(loc, read32le(loc) + val);
471     return;
472   case R_RISCV_ADD64:
473     write64le(loc, read64le(loc) + val);
474     return;
475   case R_RISCV_SUB6:
476     *loc = (*loc & 0xc0) | (((*loc & 0x3f) - val) & 0x3f);
477     return;
478   case R_RISCV_SUB8:
479     *loc -= val;
480     return;
481   case R_RISCV_SUB16:
482     write16le(loc, read16le(loc) - val);
483     return;
484   case R_RISCV_SUB32:
485     write32le(loc, read32le(loc) - val);
486     return;
487   case R_RISCV_SUB64:
488     write64le(loc, read64le(loc) - val);
489     return;
490   case R_RISCV_SET6:
491     *loc = (*loc & 0xc0) | (val & 0x3f);
492     return;
493   case R_RISCV_SET8:
494     *loc = val;
495     return;
496   case R_RISCV_SET16:
497     write16le(loc, val);
498     return;
499   case R_RISCV_SET32:
500   case R_RISCV_32_PCREL:
501   case R_RISCV_PLT32:
502     write32le(loc, val);
503     return;
504 
505   case R_RISCV_TLS_DTPREL32:
506     write32le(loc, val - dtpOffset);
507     break;
508   case R_RISCV_TLS_DTPREL64:
509     write64le(loc, val - dtpOffset);
510     break;
511 
512   case R_RISCV_RELAX:
513     return; // Ignored (for now)
514 
515   default:
516     llvm_unreachable("unknown relocation");
517   }
518 }
519 
520 void RISCV::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
521   uint64_t secAddr = sec.getOutputSection()->addr;
522   if (auto *s = dyn_cast<InputSection>(&sec))
523     secAddr += s->outSecOff;
524   else if (auto *ehIn = dyn_cast<EhInputSection>(&sec))
525     secAddr += ehIn->getParent()->outSecOff;
526   for (size_t i = 0, size = sec.relocs().size(); i != size; ++i) {
527     const Relocation &rel = sec.relocs()[i];
528     uint8_t *loc = buf + rel.offset;
529     const uint64_t val =
530         sec.getRelocTargetVA(sec.file, rel.type, rel.addend,
531                              secAddr + rel.offset, *rel.sym, rel.expr);
532 
533     switch (rel.expr) {
534     case R_RELAX_HINT:
535       break;
536     case R_RISCV_LEB128:
537       if (i + 1 < size) {
538         const Relocation &rel1 = sec.relocs()[i + 1];
539         if (rel.type == R_RISCV_SET_ULEB128 &&
540             rel1.type == R_RISCV_SUB_ULEB128 && rel.offset == rel1.offset) {
541           auto val = rel.sym->getVA(rel.addend) - rel1.sym->getVA(rel1.addend);
542           if (overwriteULEB128(loc, val) >= 0x80)
543             errorOrWarn(sec.getLocation(rel.offset) + ": ULEB128 value " +
544                         Twine(val) + " exceeds available space; references '" +
545                         lld::toString(*rel.sym) + "'");
546           ++i;
547           continue;
548         }
549       }
550       errorOrWarn(sec.getLocation(rel.offset) +
551                   ": R_RISCV_SET_ULEB128 not paired with R_RISCV_SUB_SET128");
552       return;
553     default:
554       relocate(loc, rel, val);
555       break;
556     }
557   }
558 }
559 
560 namespace {
561 struct SymbolAnchor {
562   uint64_t offset;
563   Defined *d;
564   bool end; // true for the anchor of st_value+st_size
565 };
566 } // namespace
567 
568 struct elf::RISCVRelaxAux {
569   // This records symbol start and end offsets which will be adjusted according
570   // to the nearest relocDeltas element.
571   SmallVector<SymbolAnchor, 0> anchors;
572   // For relocations[i], the actual offset is r_offset - (i ? relocDeltas[i-1] :
573   // 0).
574   std::unique_ptr<uint32_t[]> relocDeltas;
575   // For relocations[i], the actual type is relocTypes[i].
576   std::unique_ptr<RelType[]> relocTypes;
577   SmallVector<uint32_t, 0> writes;
578 };
579 
580 static void initSymbolAnchors() {
581   SmallVector<InputSection *, 0> storage;
582   for (OutputSection *osec : outputSections) {
583     if (!(osec->flags & SHF_EXECINSTR))
584       continue;
585     for (InputSection *sec : getInputSections(*osec, storage)) {
586       sec->relaxAux = make<RISCVRelaxAux>();
587       if (sec->relocs().size()) {
588         sec->relaxAux->relocDeltas =
589             std::make_unique<uint32_t[]>(sec->relocs().size());
590         sec->relaxAux->relocTypes =
591             std::make_unique<RelType[]>(sec->relocs().size());
592       }
593     }
594   }
595   // Store anchors (st_value and st_value+st_size) for symbols relative to text
596   // sections.
597   //
598   // For a defined symbol foo, we may have `d->file != file` with --wrap=foo.
599   // We should process foo, as the defining object file's symbol table may not
600   // contain foo after redirectSymbols changed the foo entry to __wrap_foo. To
601   // avoid adding a Defined that is undefined in one object file, use
602   // `!d->scriptDefined` to exclude symbols that are definitely not wrapped.
603   //
604   // `relaxAux->anchors` may contain duplicate symbols, but that is fine.
605   for (InputFile *file : ctx.objectFiles)
606     for (Symbol *sym : file->getSymbols()) {
607       auto *d = dyn_cast<Defined>(sym);
608       if (!d || (d->file != file && !d->scriptDefined))
609         continue;
610       if (auto *sec = dyn_cast_or_null<InputSection>(d->section))
611         if (sec->flags & SHF_EXECINSTR && sec->relaxAux) {
612           // If sec is discarded, relaxAux will be nullptr.
613           sec->relaxAux->anchors.push_back({d->value, d, false});
614           sec->relaxAux->anchors.push_back({d->value + d->size, d, true});
615         }
616     }
617   // Sort anchors by offset so that we can find the closest relocation
618   // efficiently. For a zero size symbol, ensure that its start anchor precedes
619   // its end anchor. For two symbols with anchors at the same offset, their
620   // order does not matter.
621   for (OutputSection *osec : outputSections) {
622     if (!(osec->flags & SHF_EXECINSTR))
623       continue;
624     for (InputSection *sec : getInputSections(*osec, storage)) {
625       llvm::sort(sec->relaxAux->anchors, [](auto &a, auto &b) {
626         return std::make_pair(a.offset, a.end) <
627                std::make_pair(b.offset, b.end);
628       });
629     }
630   }
631 }
632 
633 // Relax R_RISCV_CALL/R_RISCV_CALL_PLT auipc+jalr to c.j, c.jal, or jal.
634 static void relaxCall(const InputSection &sec, size_t i, uint64_t loc,
635                       Relocation &r, uint32_t &remove) {
636   const bool rvc = getEFlags(sec.file) & EF_RISCV_RVC;
637   const Symbol &sym = *r.sym;
638   const uint64_t insnPair = read64le(sec.content().data() + r.offset);
639   const uint32_t rd = extractBits(insnPair, 32 + 11, 32 + 7);
640   const uint64_t dest =
641       (r.expr == R_PLT_PC ? sym.getPltVA() : sym.getVA()) + r.addend;
642   const int64_t displace = dest - loc;
643 
644   if (rvc && isInt<12>(displace) && rd == 0) {
645     sec.relaxAux->relocTypes[i] = R_RISCV_RVC_JUMP;
646     sec.relaxAux->writes.push_back(0xa001); // c.j
647     remove = 6;
648   } else if (rvc && isInt<12>(displace) && rd == X_RA &&
649              !config->is64) { // RV32C only
650     sec.relaxAux->relocTypes[i] = R_RISCV_RVC_JUMP;
651     sec.relaxAux->writes.push_back(0x2001); // c.jal
652     remove = 6;
653   } else if (isInt<21>(displace)) {
654     sec.relaxAux->relocTypes[i] = R_RISCV_JAL;
655     sec.relaxAux->writes.push_back(0x6f | rd << 7); // jal
656     remove = 4;
657   }
658 }
659 
660 // Relax local-exec TLS when hi20 is zero.
661 static void relaxTlsLe(const InputSection &sec, size_t i, uint64_t loc,
662                        Relocation &r, uint32_t &remove) {
663   uint64_t val = r.sym->getVA(r.addend);
664   if (hi20(val) != 0)
665     return;
666   uint32_t insn = read32le(sec.content().data() + r.offset);
667   switch (r.type) {
668   case R_RISCV_TPREL_HI20:
669   case R_RISCV_TPREL_ADD:
670     // Remove lui rd, %tprel_hi(x) and add rd, rd, tp, %tprel_add(x).
671     sec.relaxAux->relocTypes[i] = R_RISCV_RELAX;
672     remove = 4;
673     break;
674   case R_RISCV_TPREL_LO12_I:
675     // addi rd, rd, %tprel_lo(x) => addi rd, tp, st_value(x)
676     sec.relaxAux->relocTypes[i] = R_RISCV_32;
677     insn = (insn & ~(31 << 15)) | (X_TP << 15);
678     sec.relaxAux->writes.push_back(setLO12_I(insn, val));
679     break;
680   case R_RISCV_TPREL_LO12_S:
681     // sw rs, %tprel_lo(x)(rd) => sw rs, st_value(x)(rd)
682     sec.relaxAux->relocTypes[i] = R_RISCV_32;
683     insn = (insn & ~(31 << 15)) | (X_TP << 15);
684     sec.relaxAux->writes.push_back(setLO12_S(insn, val));
685     break;
686   }
687 }
688 
689 static void relaxHi20Lo12(const InputSection &sec, size_t i, uint64_t loc,
690                           Relocation &r, uint32_t &remove) {
691   const Defined *gp = ElfSym::riscvGlobalPointer;
692   if (!gp)
693     return;
694 
695   if (!isInt<12>(r.sym->getVA(r.addend) - gp->getVA()))
696     return;
697 
698   switch (r.type) {
699   case R_RISCV_HI20:
700     // Remove lui rd, %hi20(x).
701     sec.relaxAux->relocTypes[i] = R_RISCV_RELAX;
702     remove = 4;
703     break;
704   case R_RISCV_LO12_I:
705     sec.relaxAux->relocTypes[i] = INTERNAL_R_RISCV_GPREL_I;
706     break;
707   case R_RISCV_LO12_S:
708     sec.relaxAux->relocTypes[i] = INTERNAL_R_RISCV_GPREL_S;
709     break;
710   }
711 }
712 
713 static bool relax(InputSection &sec) {
714   const uint64_t secAddr = sec.getVA();
715   auto &aux = *sec.relaxAux;
716   bool changed = false;
717   ArrayRef<SymbolAnchor> sa = ArrayRef(aux.anchors);
718   uint64_t delta = 0;
719 
720   std::fill_n(aux.relocTypes.get(), sec.relocs().size(), R_RISCV_NONE);
721   aux.writes.clear();
722   for (auto [i, r] : llvm::enumerate(sec.relocs())) {
723     const uint64_t loc = secAddr + r.offset - delta;
724     uint32_t &cur = aux.relocDeltas[i], remove = 0;
725     switch (r.type) {
726     case R_RISCV_ALIGN: {
727       const uint64_t nextLoc = loc + r.addend;
728       const uint64_t align = PowerOf2Ceil(r.addend + 2);
729       // All bytes beyond the alignment boundary should be removed.
730       remove = nextLoc - ((loc + align - 1) & -align);
731       assert(static_cast<int32_t>(remove) >= 0 &&
732              "R_RISCV_ALIGN needs expanding the content");
733       break;
734     }
735     case R_RISCV_CALL:
736     case R_RISCV_CALL_PLT:
737       if (i + 1 != sec.relocs().size() &&
738           sec.relocs()[i + 1].type == R_RISCV_RELAX)
739         relaxCall(sec, i, loc, r, remove);
740       break;
741     case R_RISCV_TPREL_HI20:
742     case R_RISCV_TPREL_ADD:
743     case R_RISCV_TPREL_LO12_I:
744     case R_RISCV_TPREL_LO12_S:
745       if (i + 1 != sec.relocs().size() &&
746           sec.relocs()[i + 1].type == R_RISCV_RELAX)
747         relaxTlsLe(sec, i, loc, r, remove);
748       break;
749     case R_RISCV_HI20:
750     case R_RISCV_LO12_I:
751     case R_RISCV_LO12_S:
752       if (i + 1 != sec.relocs().size() &&
753           sec.relocs()[i + 1].type == R_RISCV_RELAX)
754         relaxHi20Lo12(sec, i, loc, r, remove);
755       break;
756     }
757 
758     // For all anchors whose offsets are <= r.offset, they are preceded by
759     // the previous relocation whose `relocDeltas` value equals `delta`.
760     // Decrease their st_value and update their st_size.
761     for (; sa.size() && sa[0].offset <= r.offset; sa = sa.slice(1)) {
762       if (sa[0].end)
763         sa[0].d->size = sa[0].offset - delta - sa[0].d->value;
764       else
765         sa[0].d->value = sa[0].offset - delta;
766     }
767     delta += remove;
768     if (delta != cur) {
769       cur = delta;
770       changed = true;
771     }
772   }
773 
774   for (const SymbolAnchor &a : sa) {
775     if (a.end)
776       a.d->size = a.offset - delta - a.d->value;
777     else
778       a.d->value = a.offset - delta;
779   }
780   // Inform assignAddresses that the size has changed.
781   if (!isUInt<32>(delta))
782     fatal("section size decrease is too large: " + Twine(delta));
783   sec.bytesDropped = delta;
784   return changed;
785 }
786 
787 // When relaxing just R_RISCV_ALIGN, relocDeltas is usually changed only once in
788 // the absence of a linker script. For call and load/store R_RISCV_RELAX, code
789 // shrinkage may reduce displacement and make more relocations eligible for
790 // relaxation. Code shrinkage may increase displacement to a call/load/store
791 // target at a higher fixed address, invalidating an earlier relaxation. Any
792 // change in section sizes can have cascading effect and require another
793 // relaxation pass.
794 bool RISCV::relaxOnce(int pass) const {
795   llvm::TimeTraceScope timeScope("RISC-V relaxOnce");
796   if (config->relocatable)
797     return false;
798 
799   if (pass == 0)
800     initSymbolAnchors();
801 
802   SmallVector<InputSection *, 0> storage;
803   bool changed = false;
804   for (OutputSection *osec : outputSections) {
805     if (!(osec->flags & SHF_EXECINSTR))
806       continue;
807     for (InputSection *sec : getInputSections(*osec, storage))
808       changed |= relax(*sec);
809   }
810   return changed;
811 }
812 
813 void elf::riscvFinalizeRelax(int passes) {
814   llvm::TimeTraceScope timeScope("Finalize RISC-V relaxation");
815   log("relaxation passes: " + Twine(passes));
816   SmallVector<InputSection *, 0> storage;
817   for (OutputSection *osec : outputSections) {
818     if (!(osec->flags & SHF_EXECINSTR))
819       continue;
820     for (InputSection *sec : getInputSections(*osec, storage)) {
821       RISCVRelaxAux &aux = *sec->relaxAux;
822       if (!aux.relocDeltas)
823         continue;
824 
825       MutableArrayRef<Relocation> rels = sec->relocs();
826       ArrayRef<uint8_t> old = sec->content();
827       size_t newSize = old.size() - aux.relocDeltas[rels.size() - 1];
828       size_t writesIdx = 0;
829       uint8_t *p = context().bAlloc.Allocate<uint8_t>(newSize);
830       uint64_t offset = 0;
831       int64_t delta = 0;
832       sec->content_ = p;
833       sec->size = newSize;
834       sec->bytesDropped = 0;
835 
836       // Update section content: remove NOPs for R_RISCV_ALIGN and rewrite
837       // instructions for relaxed relocations.
838       for (size_t i = 0, e = rels.size(); i != e; ++i) {
839         uint32_t remove = aux.relocDeltas[i] - delta;
840         delta = aux.relocDeltas[i];
841         if (remove == 0 && aux.relocTypes[i] == R_RISCV_NONE)
842           continue;
843 
844         // Copy from last location to the current relocated location.
845         const Relocation &r = rels[i];
846         uint64_t size = r.offset - offset;
847         memcpy(p, old.data() + offset, size);
848         p += size;
849 
850         // For R_RISCV_ALIGN, we will place `offset` in a location (among NOPs)
851         // to satisfy the alignment requirement. If both `remove` and r.addend
852         // are multiples of 4, it is as if we have skipped some NOPs. Otherwise
853         // we are in the middle of a 4-byte NOP, and we need to rewrite the NOP
854         // sequence.
855         int64_t skip = 0;
856         if (r.type == R_RISCV_ALIGN) {
857           if (remove % 4 || r.addend % 4) {
858             skip = r.addend - remove;
859             int64_t j = 0;
860             for (; j + 4 <= skip; j += 4)
861               write32le(p + j, 0x00000013); // nop
862             if (j != skip) {
863               assert(j + 2 == skip);
864               write16le(p + j, 0x0001); // c.nop
865             }
866           }
867         } else if (RelType newType = aux.relocTypes[i]) {
868           switch (newType) {
869           case INTERNAL_R_RISCV_GPREL_I:
870           case INTERNAL_R_RISCV_GPREL_S:
871             break;
872           case R_RISCV_RELAX:
873             // Used by relaxTlsLe to indicate the relocation is ignored.
874             break;
875           case R_RISCV_RVC_JUMP:
876             skip = 2;
877             write16le(p, aux.writes[writesIdx++]);
878             break;
879           case R_RISCV_JAL:
880             skip = 4;
881             write32le(p, aux.writes[writesIdx++]);
882             break;
883           case R_RISCV_32:
884             // Used by relaxTlsLe to write a uint32_t then suppress the handling
885             // in relocateAlloc.
886             skip = 4;
887             write32le(p, aux.writes[writesIdx++]);
888             aux.relocTypes[i] = R_RISCV_NONE;
889             break;
890           default:
891             llvm_unreachable("unsupported type");
892           }
893         }
894 
895         p += skip;
896         offset = r.offset + skip + remove;
897       }
898       memcpy(p, old.data() + offset, old.size() - offset);
899 
900       // Subtract the previous relocDeltas value from the relocation offset.
901       // For a pair of R_RISCV_CALL/R_RISCV_RELAX with the same offset, decrease
902       // their r_offset by the same delta.
903       delta = 0;
904       for (size_t i = 0, e = rels.size(); i != e;) {
905         uint64_t cur = rels[i].offset;
906         do {
907           rels[i].offset -= delta;
908           if (aux.relocTypes[i] != R_RISCV_NONE)
909             rels[i].type = aux.relocTypes[i];
910         } while (++i != e && rels[i].offset == cur);
911         delta = aux.relocDeltas[i - 1];
912       }
913     }
914   }
915 }
916 
917 namespace {
918 // Representation of the merged .riscv.attributes input sections. The psABI
919 // specifies merge policy for attributes. E.g. if we link an object without an
920 // extension with an object with the extension, the output Tag_RISCV_arch shall
921 // contain the extension. Some tools like objdump parse .riscv.attributes and
922 // disabling some instructions if the first Tag_RISCV_arch does not contain an
923 // extension.
924 class RISCVAttributesSection final : public SyntheticSection {
925 public:
926   RISCVAttributesSection()
927       : SyntheticSection(0, SHT_RISCV_ATTRIBUTES, 1, ".riscv.attributes") {}
928 
929   size_t getSize() const override { return size; }
930   void writeTo(uint8_t *buf) override;
931 
932   static constexpr StringRef vendor = "riscv";
933   DenseMap<unsigned, unsigned> intAttr;
934   DenseMap<unsigned, StringRef> strAttr;
935   size_t size = 0;
936 };
937 } // namespace
938 
939 static void mergeArch(RISCVISAInfo::OrderedExtensionMap &mergedExts,
940                       unsigned &mergedXlen, const InputSectionBase *sec,
941                       StringRef s) {
942   auto maybeInfo = RISCVISAInfo::parseNormalizedArchString(s);
943   if (!maybeInfo) {
944     errorOrWarn(toString(sec) + ": " + s + ": " +
945                 llvm::toString(maybeInfo.takeError()));
946     return;
947   }
948 
949   // Merge extensions.
950   RISCVISAInfo &info = **maybeInfo;
951   if (mergedExts.empty()) {
952     mergedExts = info.getExtensions();
953     mergedXlen = info.getXLen();
954   } else {
955     for (const auto &ext : info.getExtensions()) {
956       if (auto it = mergedExts.find(ext.first); it != mergedExts.end()) {
957         if (std::tie(it->second.MajorVersion, it->second.MinorVersion) >=
958             std::tie(ext.second.MajorVersion, ext.second.MinorVersion))
959           continue;
960       }
961       mergedExts[ext.first] = ext.second;
962     }
963   }
964 }
965 
966 static RISCVAttributesSection *
967 mergeAttributesSection(const SmallVector<InputSectionBase *, 0> &sections) {
968   RISCVISAInfo::OrderedExtensionMap exts;
969   const InputSectionBase *firstStackAlign = nullptr;
970   unsigned firstStackAlignValue = 0, xlen = 0;
971   bool hasArch = false;
972 
973   in.riscvAttributes = std::make_unique<RISCVAttributesSection>();
974   auto &merged = static_cast<RISCVAttributesSection &>(*in.riscvAttributes);
975 
976   // Collect all tags values from attributes section.
977   const auto &attributesTags = RISCVAttrs::getRISCVAttributeTags();
978   for (const InputSectionBase *sec : sections) {
979     RISCVAttributeParser parser;
980     if (Error e = parser.parse(sec->content(), llvm::endianness::little))
981       warn(toString(sec) + ": " + llvm::toString(std::move(e)));
982     for (const auto &tag : attributesTags) {
983       switch (RISCVAttrs::AttrType(tag.attr)) {
984         // Integer attributes.
985       case RISCVAttrs::STACK_ALIGN:
986         if (auto i = parser.getAttributeValue(tag.attr)) {
987           auto r = merged.intAttr.try_emplace(tag.attr, *i);
988           if (r.second) {
989             firstStackAlign = sec;
990             firstStackAlignValue = *i;
991           } else if (r.first->second != *i) {
992             errorOrWarn(toString(sec) + " has stack_align=" + Twine(*i) +
993                         " but " + toString(firstStackAlign) +
994                         " has stack_align=" + Twine(firstStackAlignValue));
995           }
996         }
997         continue;
998       case RISCVAttrs::UNALIGNED_ACCESS:
999         if (auto i = parser.getAttributeValue(tag.attr))
1000           merged.intAttr[tag.attr] |= *i;
1001         continue;
1002 
1003         // String attributes.
1004       case RISCVAttrs::ARCH:
1005         if (auto s = parser.getAttributeString(tag.attr)) {
1006           hasArch = true;
1007           mergeArch(exts, xlen, sec, *s);
1008         }
1009         continue;
1010 
1011         // Attributes which use the default handling.
1012       case RISCVAttrs::PRIV_SPEC:
1013       case RISCVAttrs::PRIV_SPEC_MINOR:
1014       case RISCVAttrs::PRIV_SPEC_REVISION:
1015         break;
1016       }
1017 
1018       // Fallback for deprecated priv_spec* and other unknown attributes: retain
1019       // the attribute if all input sections agree on the value. GNU ld uses 0
1020       // and empty strings as default values which are not dumped to the output.
1021       // TODO Adjust after resolution to
1022       // https://github.com/riscv-non-isa/riscv-elf-psabi-doc/issues/352
1023       if (tag.attr % 2 == 0) {
1024         if (auto i = parser.getAttributeValue(tag.attr)) {
1025           auto r = merged.intAttr.try_emplace(tag.attr, *i);
1026           if (!r.second && r.first->second != *i)
1027             r.first->second = 0;
1028         }
1029       } else if (auto s = parser.getAttributeString(tag.attr)) {
1030         auto r = merged.strAttr.try_emplace(tag.attr, *s);
1031         if (!r.second && r.first->second != *s)
1032           r.first->second = {};
1033       }
1034     }
1035   }
1036 
1037   if (hasArch) {
1038     if (auto result = RISCVISAInfo::postProcessAndChecking(
1039             std::make_unique<RISCVISAInfo>(xlen, exts))) {
1040       merged.strAttr.try_emplace(RISCVAttrs::ARCH,
1041                                  saver().save((*result)->toString()));
1042     } else {
1043       errorOrWarn(llvm::toString(result.takeError()));
1044     }
1045   }
1046 
1047   // The total size of headers: format-version [ <section-length> "vendor-name"
1048   // [ <file-tag> <size>.
1049   size_t size = 5 + merged.vendor.size() + 1 + 5;
1050   for (auto &attr : merged.intAttr)
1051     if (attr.second != 0)
1052       size += getULEB128Size(attr.first) + getULEB128Size(attr.second);
1053   for (auto &attr : merged.strAttr)
1054     if (!attr.second.empty())
1055       size += getULEB128Size(attr.first) + attr.second.size() + 1;
1056   merged.size = size;
1057   return &merged;
1058 }
1059 
1060 void RISCVAttributesSection::writeTo(uint8_t *buf) {
1061   const size_t size = getSize();
1062   uint8_t *const end = buf + size;
1063   *buf = ELFAttrs::Format_Version;
1064   write32(buf + 1, size - 1);
1065   buf += 5;
1066 
1067   memcpy(buf, vendor.data(), vendor.size());
1068   buf += vendor.size() + 1;
1069 
1070   *buf = ELFAttrs::File;
1071   write32(buf + 1, end - buf);
1072   buf += 5;
1073 
1074   for (auto &attr : intAttr) {
1075     if (attr.second == 0)
1076       continue;
1077     buf += encodeULEB128(attr.first, buf);
1078     buf += encodeULEB128(attr.second, buf);
1079   }
1080   for (auto &attr : strAttr) {
1081     if (attr.second.empty())
1082       continue;
1083     buf += encodeULEB128(attr.first, buf);
1084     memcpy(buf, attr.second.data(), attr.second.size());
1085     buf += attr.second.size() + 1;
1086   }
1087 }
1088 
1089 void elf::mergeRISCVAttributesSections() {
1090   // Find the first input SHT_RISCV_ATTRIBUTES; return if not found.
1091   size_t place =
1092       llvm::find_if(ctx.inputSections,
1093                     [](auto *s) { return s->type == SHT_RISCV_ATTRIBUTES; }) -
1094       ctx.inputSections.begin();
1095   if (place == ctx.inputSections.size())
1096     return;
1097 
1098   // Extract all SHT_RISCV_ATTRIBUTES sections into `sections`.
1099   SmallVector<InputSectionBase *, 0> sections;
1100   llvm::erase_if(ctx.inputSections, [&](InputSectionBase *s) {
1101     if (s->type != SHT_RISCV_ATTRIBUTES)
1102       return false;
1103     sections.push_back(s);
1104     return true;
1105   });
1106 
1107   // Add the merged section.
1108   ctx.inputSections.insert(ctx.inputSections.begin() + place,
1109                            mergeAttributesSection(sections));
1110 }
1111 
1112 TargetInfo *elf::getRISCVTargetInfo() {
1113   static RISCV target;
1114   return &target;
1115 }
1116