1//===- LoongArch.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 "RelocScan.h"
12#include "Symbols.h"
13#include "SyntheticSections.h"
14#include "Target.h"
15#include "llvm/BinaryFormat/ELF.h"
16#include "llvm/Support/LEB128.h"
17
18using namespace llvm;
19using namespace llvm::object;
20using namespace llvm::support::endian;
21using namespace llvm::ELF;
22using namespace lld;
23using namespace lld::elf;
24
25namespace {
26class LoongArch final : public TargetInfo {
27public:
28 LoongArch(Ctx &);
29 uint32_t calcEFlags() const override;
30 int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override;
31 void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
32 void writeIgotPlt(uint8_t *buf, const Symbol &s) const override;
33 void writePltHeader(uint8_t *buf) const override;
34 void writePlt(uint8_t *buf, const Symbol &sym,
35 uint64_t pltEntryAddr) const override;
36 RelType getDynRel(RelType type) const override;
37 RelExpr getRelExpr(RelType type, const Symbol &s,
38 const uint8_t *loc) const override;
39 bool usesOnlyLowPageBits(RelType type) const override;
40 template <class RelTy>
41 void scanSectionImpl(InputSectionBase &, Relocs<RelTy>, unsigned shard);
42 void scanSection(InputSectionBase &sec, unsigned shard) override {
43 if (ctx.arg.is64)
44 elf::scanSection1<ELF64LE>(target&: *this, sec, shard);
45 else
46 elf::scanSection1<ELF32LE>(target&: *this, sec, shard);
47 }
48 void relocate(uint8_t *loc, const Relocation &rel,
49 uint64_t val) const override;
50 bool relaxOnce(int pass) const override;
51 bool synthesizeAlign(uint64_t &dot, InputSection *sec) override;
52 void relocateAlloc(InputSection &sec, uint8_t *buf) const override;
53 void finalizeRelax(int passes) const override;
54
55private:
56 void tlsdescToIe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
57 void tlsdescToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
58 bool tryGotToPCRel(uint8_t *loc, const Relocation &rHi20,
59 const Relocation &rLo12, uint64_t secAddr) const;
60 template <class ELFT, class RelTy>
61 bool synthesizeAlignForInput(uint64_t &dot, InputSection *sec,
62 Relocs<RelTy> rels);
63 template <class ELFT, class RelTy>
64 void finalizeSynthesizeAligns(uint64_t &dot, InputSection *sec,
65 Relocs<RelTy> rels);
66 template <class ELFT>
67 bool synthesizeAlignAux(uint64_t &dot, InputSection *sec);
68
69 // The following two variables are used by synthesized ALIGN relocations.
70 InputSection *baseSec = nullptr;
71 // r_offset and r_addend pairs.
72 SmallVector<std::pair<uint64_t, uint64_t>, 0> synthesizedAligns;
73};
74} // end anonymous namespace
75
76namespace {
77enum Op {
78 SUB_W = 0x00110000,
79 SUB_D = 0x00118000,
80 BREAK = 0x002a0000,
81 SRLI_W = 0x00448000,
82 SRLI_D = 0x00450000,
83 ADDI_W = 0x02800000,
84 ADDI_D = 0x02c00000,
85 ANDI = 0x03400000,
86 ORI = 0x03800000,
87 LU12I_W = 0x14000000,
88 PCADDI = 0x18000000,
89 PCADDU12I = 0x1c000000,
90 PCALAU12I = 0x1a000000,
91 LD_W = 0x28800000,
92 LD_D = 0x28c00000,
93 JIRL = 0x4c000000,
94 B = 0x50000000,
95 BL = 0x54000000,
96};
97
98enum Reg {
99 R_ZERO = 0,
100 R_RA = 1,
101 R_TP = 2,
102 R_A0 = 4,
103 R_T0 = 12,
104 R_T1 = 13,
105 R_T2 = 14,
106 R_T3 = 15,
107};
108} // namespace
109
110// Mask out the input's lowest 12 bits for use with `pcalau12i`, in sequences
111// like `pcalau12i + addi.[wd]` or `pcalau12i + {ld,st}.*` where the `pcalau12i`
112// produces a PC-relative intermediate value with the lowest 12 bits zeroed (the
113// "page") for the next instruction to add in the "page offset". (`pcalau12i`
114// stands for something like "PC ALigned Add Upper that starts from the 12th
115// bit, Immediate".)
116//
117// Here a "page" is in fact just another way to refer to the 12-bit range
118// allowed by the immediate field of the addi/ld/st instructions, and not
119// related to the system or the kernel's actual page size. The semantics happen
120// to match the AArch64 `adrp`, so the concept of "page" is borrowed here.
121static uint64_t getLoongArchPage(uint64_t p) {
122 return p & ~static_cast<uint64_t>(0xfff);
123}
124
125static uint32_t lo12(uint32_t val) { return val & 0xfff; }
126
127// Calculate the adjusted page delta between dest and PC.
128uint64_t elf::getLoongArchPageDelta(uint64_t dest, uint64_t pc, RelType type) {
129 // Note that if the sequence being relocated is `pcalau12i + addi.d + lu32i.d
130 // + lu52i.d`, they must be adjacent so that we can infer the PC of
131 // `pcalau12i` when calculating the page delta for the other two instructions
132 // (lu32i.d and lu52i.d). Compensate all the sign-extensions is a bit
133 // complicated. Just use psABI recommended algorithm.
134 uint64_t pcalau12i_pc;
135 switch (type) {
136 case R_LARCH_PCALA64_LO20:
137 case R_LARCH_GOT64_PC_LO20:
138 case R_LARCH_TLS_IE64_PC_LO20:
139 case R_LARCH_TLS_DESC64_PC_LO20:
140 pcalau12i_pc = pc - 8;
141 break;
142 case R_LARCH_PCALA64_HI12:
143 case R_LARCH_GOT64_PC_HI12:
144 case R_LARCH_TLS_IE64_PC_HI12:
145 case R_LARCH_TLS_DESC64_PC_HI12:
146 pcalau12i_pc = pc - 12;
147 break;
148 default:
149 pcalau12i_pc = pc;
150 break;
151 }
152 uint64_t result = getLoongArchPage(p: dest) - getLoongArchPage(p: pcalau12i_pc);
153 if (dest & 0x800)
154 result += 0x1000 - 0x1'0000'0000;
155 if (result & 0x8000'0000)
156 result += 0x1'0000'0000;
157 return result;
158}
159
160static uint32_t hi20(uint32_t val) { return (val + 0x800) >> 12; }
161
162static uint32_t insn(uint32_t op, uint32_t d, uint32_t j, uint32_t k) {
163 return op | d | (j << 5) | (k << 10);
164}
165
166// Extract bits v[begin:end], where range is inclusive.
167static uint32_t extractBits(uint64_t v, uint32_t begin, uint32_t end) {
168 return begin == 63 ? v >> end : (v & ((1ULL << (begin + 1)) - 1)) >> end;
169}
170
171static uint32_t getD5(uint64_t v) { return extractBits(v, begin: 4, end: 0); }
172
173static uint32_t getJ5(uint64_t v) { return extractBits(v, begin: 9, end: 5); }
174
175static uint32_t setD5k16(uint32_t insn, uint32_t imm) {
176 uint32_t immLo = extractBits(v: imm, begin: 15, end: 0);
177 uint32_t immHi = extractBits(v: imm, begin: 20, end: 16);
178 return (insn & 0xfc0003e0) | (immLo << 10) | immHi;
179}
180
181static uint32_t setD10k16(uint32_t insn, uint32_t imm) {
182 uint32_t immLo = extractBits(v: imm, begin: 15, end: 0);
183 uint32_t immHi = extractBits(v: imm, begin: 25, end: 16);
184 return (insn & 0xfc000000) | (immLo << 10) | immHi;
185}
186
187static uint32_t setJ20(uint32_t insn, uint32_t imm) {
188 return (insn & 0xfe00001f) | (extractBits(v: imm, begin: 19, end: 0) << 5);
189}
190
191static uint32_t setJ5(uint32_t insn, uint32_t imm) {
192 return (insn & 0xfffffc1f) | (extractBits(v: imm, begin: 4, end: 0) << 5);
193}
194
195static uint32_t setK12(uint32_t insn, uint32_t imm) {
196 return (insn & 0xffc003ff) | (extractBits(v: imm, begin: 11, end: 0) << 10);
197}
198
199static uint32_t setK16(uint32_t insn, uint32_t imm) {
200 return (insn & 0xfc0003ff) | (extractBits(v: imm, begin: 15, end: 0) << 10);
201}
202
203static bool isJirl(uint32_t insn) {
204 return (insn & 0xfc000000) == JIRL;
205}
206
207static void handleUleb128(Ctx &ctx, uint8_t *loc, uint64_t val) {
208 const uint32_t maxcount = 1 + 64 / 7;
209 uint32_t count;
210 const char *error = nullptr;
211 uint64_t orig = decodeULEB128(p: loc, n: &count, end: nullptr, error: &error);
212 if (count > maxcount || (count == maxcount && error))
213 Err(ctx) << getErrorLoc(ctx, loc) << "extra space for uleb128";
214 uint64_t mask = count < maxcount ? (1ULL << 7 * count) - 1 : -1ULL;
215 encodeULEB128(Value: (orig + val) & mask, p: loc, PadTo: count);
216}
217
218LoongArch::LoongArch(Ctx &ctx) : TargetInfo(ctx) {
219 // The LoongArch ISA itself does not have a limit on page sizes. According to
220 // the ISA manual, the PS (page size) field in MTLB entries and CSR.STLBPS is
221 // 6 bits wide, meaning the maximum page size is 2^63 which is equivalent to
222 // "unlimited".
223 // However, practically the maximum usable page size is constrained by the
224 // kernel implementation, and 64KiB is the biggest non-huge page size
225 // supported by Linux as of v6.4. The most widespread page size in use,
226 // though, is 16KiB.
227 defaultCommonPageSize = 16384;
228 defaultMaxPageSize = 65536;
229 write32le(P: trapInstr.data(), V: BREAK); // break 0
230
231 copyRel = R_LARCH_COPY;
232 pltRel = R_LARCH_JUMP_SLOT;
233 relativeRel = R_LARCH_RELATIVE;
234 iRelativeRel = R_LARCH_IRELATIVE;
235
236 if (ctx.arg.is64) {
237 symbolicRel = R_LARCH_64;
238 tlsModuleIndexRel = R_LARCH_TLS_DTPMOD64;
239 tlsOffsetRel = R_LARCH_TLS_DTPREL64;
240 tlsGotRel = R_LARCH_TLS_TPREL64;
241 tlsDescRel = R_LARCH_TLS_DESC64;
242 } else {
243 symbolicRel = R_LARCH_32;
244 tlsModuleIndexRel = R_LARCH_TLS_DTPMOD32;
245 tlsOffsetRel = R_LARCH_TLS_DTPREL32;
246 tlsGotRel = R_LARCH_TLS_TPREL32;
247 tlsDescRel = R_LARCH_TLS_DESC32;
248 }
249
250 gotRel = symbolicRel;
251
252 // .got.plt[0] = _dl_runtime_resolve, .got.plt[1] = link_map
253 gotPltHeaderEntriesNum = 2;
254
255 pltHeaderSize = 32;
256 pltEntrySize = 16;
257 ipltEntrySize = 16;
258}
259
260static uint32_t getEFlags(Ctx &ctx, const InputFile *f) {
261 if (ctx.arg.is64)
262 return cast<ObjFile<ELF64LE>>(Val: f)->getObj().getHeader().e_flags;
263 return cast<ObjFile<ELF32LE>>(Val: f)->getObj().getHeader().e_flags;
264}
265
266static bool inputFileHasCode(const InputFile *f) {
267 for (const auto *sec : f->getSections())
268 if (sec && sec->flags & SHF_EXECINSTR)
269 return true;
270
271 return false;
272}
273
274uint32_t LoongArch::calcEFlags() const {
275 // If there are only binary input files (from -b binary), use a
276 // value of 0 for the ELF header flags.
277 if (ctx.objectFiles.empty())
278 return 0;
279
280 uint32_t target = 0;
281 const InputFile *targetFile;
282 for (const InputFile *f : ctx.objectFiles) {
283 // Do not enforce ABI compatibility if the input file does not contain code.
284 // This is useful for allowing linkage with data-only object files produced
285 // with tools like objcopy, that have zero e_flags.
286 if (!inputFileHasCode(f))
287 continue;
288
289 // Take the first non-zero e_flags as the reference.
290 uint32_t flags = getEFlags(ctx, f);
291 if (target == 0 && flags != 0) {
292 target = flags;
293 targetFile = f;
294 }
295
296 if ((flags & EF_LOONGARCH_ABI_MODIFIER_MASK) !=
297 (target & EF_LOONGARCH_ABI_MODIFIER_MASK))
298 ErrAlways(ctx) << f
299 << ": cannot link object files with different ABI from "
300 << targetFile;
301
302 // We cannot process psABI v1.x / object ABI v0 files (containing stack
303 // relocations), unlike ld.bfd.
304 //
305 // Instead of blindly accepting every v0 object and only failing at
306 // relocation processing time, just disallow interlink altogether. We
307 // don't expect significant usage of object ABI v0 in the wild (the old
308 // world may continue using object ABI v0 for a while, but as it's not
309 // binary-compatible with the upstream i.e. new-world ecosystem, it's not
310 // being considered here).
311 //
312 // There are briefly some new-world systems with object ABI v0 binaries too.
313 // It is because these systems were built before the new ABI was finalized.
314 // These are not supported either due to the extremely small number of them,
315 // and the few impacted users are advised to simply rebuild world or
316 // reinstall a recent system.
317 if ((flags & EF_LOONGARCH_OBJABI_MASK) != EF_LOONGARCH_OBJABI_V1)
318 ErrAlways(ctx) << f << ": unsupported object file ABI version";
319 }
320
321 return target;
322}
323
324int64_t LoongArch::getImplicitAddend(const uint8_t *buf, RelType type) const {
325 switch (type) {
326 default:
327 InternalErr(ctx, buf) << "cannot read addend for relocation " << type;
328 return 0;
329 case R_LARCH_32:
330 case R_LARCH_TLS_DTPMOD32:
331 case R_LARCH_TLS_DTPREL32:
332 case R_LARCH_TLS_TPREL32:
333 return SignExtend64<32>(x: read32le(P: buf));
334 case R_LARCH_64:
335 case R_LARCH_TLS_DTPMOD64:
336 case R_LARCH_TLS_DTPREL64:
337 case R_LARCH_TLS_TPREL64:
338 return read64le(P: buf);
339 case R_LARCH_RELATIVE:
340 case R_LARCH_IRELATIVE:
341 return ctx.arg.is64 ? read64le(P: buf) : read32le(P: buf);
342 case R_LARCH_NONE:
343 case R_LARCH_JUMP_SLOT:
344 // These relocations are defined as not having an implicit addend.
345 return 0;
346 case R_LARCH_TLS_DESC32:
347 return read32le(P: buf + 4);
348 case R_LARCH_TLS_DESC64:
349 return read64le(P: buf + 8);
350 }
351}
352
353void LoongArch::writeGotPlt(uint8_t *buf, const Symbol &s) const {
354 if (ctx.arg.is64)
355 write64le(P: buf, V: ctx.in.plt->getVA());
356 else
357 write32le(P: buf, V: ctx.in.plt->getVA());
358}
359
360void LoongArch::writeIgotPlt(uint8_t *buf, const Symbol &s) const {
361 if (ctx.arg.writeAddends) {
362 if (ctx.arg.is64)
363 write64le(P: buf, V: s.getVA(ctx));
364 else
365 write32le(P: buf, V: s.getVA(ctx));
366 }
367}
368
369void LoongArch::writePltHeader(uint8_t *buf) const {
370 // The LoongArch PLT is currently structured just like that of RISCV.
371 // Annoyingly, this means the PLT is still using `pcaddu12i` to perform
372 // PC-relative addressing (because `pcaddu12i` is the same as RISCV `auipc`),
373 // in contrast to the AArch64-like page-offset scheme with `pcalau12i` that
374 // is used everywhere else involving PC-relative operations in the LoongArch
375 // ELF psABI v2.00.
376 //
377 // The `pcrel_{hi20,lo12}` operators are illustrative only and not really
378 // supported by LoongArch assemblers.
379 //
380 // pcaddu12i $t2, %pcrel_hi20(.got.plt)
381 // sub.[wd] $t1, $t1, $t3
382 // ld.[wd] $t3, $t2, %pcrel_lo12(.got.plt) ; t3 = _dl_runtime_resolve
383 // addi.[wd] $t1, $t1, -pltHeaderSize-12 ; t1 = &.plt[i] - &.plt[0]
384 // addi.[wd] $t0, $t2, %pcrel_lo12(.got.plt)
385 // srli.[wd] $t1, $t1, (is64?1:2) ; t1 = &.got.plt[i] - &.got.plt[0]
386 // ld.[wd] $t0, $t0, Wordsize ; t0 = link_map
387 // jr $t3
388 uint32_t offset = ctx.in.gotPlt->getVA() - ctx.in.plt->getVA();
389 uint32_t sub = ctx.arg.is64 ? SUB_D : SUB_W;
390 uint32_t ld = ctx.arg.is64 ? LD_D : LD_W;
391 uint32_t addi = ctx.arg.is64 ? ADDI_D : ADDI_W;
392 uint32_t srli = ctx.arg.is64 ? SRLI_D : SRLI_W;
393 write32le(P: buf + 0, V: insn(op: PCADDU12I, d: R_T2, j: hi20(val: offset), k: 0));
394 write32le(P: buf + 4, V: insn(op: sub, d: R_T1, j: R_T1, k: R_T3));
395 write32le(P: buf + 8, V: insn(op: ld, d: R_T3, j: R_T2, k: lo12(val: offset)));
396 write32le(P: buf + 12,
397 V: insn(op: addi, d: R_T1, j: R_T1, k: lo12(val: -ctx.target->pltHeaderSize - 12)));
398 write32le(P: buf + 16, V: insn(op: addi, d: R_T0, j: R_T2, k: lo12(val: offset)));
399 write32le(P: buf + 20, V: insn(op: srli, d: R_T1, j: R_T1, k: ctx.arg.is64 ? 1 : 2));
400 write32le(P: buf + 24, V: insn(op: ld, d: R_T0, j: R_T0, k: ctx.arg.wordsize));
401 write32le(P: buf + 28, V: insn(op: JIRL, d: R_ZERO, j: R_T3, k: 0));
402}
403
404void LoongArch::writePlt(uint8_t *buf, const Symbol &sym,
405 uint64_t pltEntryAddr) const {
406 // See the comment in writePltHeader for reason why pcaddu12i is used instead
407 // of the pcalau12i that's more commonly seen in the ELF psABI v2.0 days.
408 //
409 // pcaddu12i $t3, %pcrel_hi20(f@.got.plt)
410 // ld.[wd] $t3, $t3, %pcrel_lo12(f@.got.plt)
411 // jirl $t1, $t3, 0
412 // nop
413 uint32_t offset = sym.getGotPltVA(ctx) - pltEntryAddr;
414 write32le(P: buf + 0, V: insn(op: PCADDU12I, d: R_T3, j: hi20(val: offset), k: 0));
415 write32le(P: buf + 4,
416 V: insn(op: ctx.arg.is64 ? LD_D : LD_W, d: R_T3, j: R_T3, k: lo12(val: offset)));
417 write32le(P: buf + 8, V: insn(op: JIRL, d: R_T1, j: R_T3, k: 0));
418 write32le(P: buf + 12, V: insn(op: ANDI, d: R_ZERO, j: R_ZERO, k: 0));
419}
420
421RelType LoongArch::getDynRel(RelType type) const {
422 return type == ctx.target->symbolicRel ? type
423 : static_cast<RelType>(R_LARCH_NONE);
424}
425
426// Used by relocateNonAlloc(), scanEhSection(), and the extreme code model
427// fallback in relocateAlloc(). For alloc sections, scanSectionImpl() is the
428// primary relocation classifier.
429RelExpr LoongArch::getRelExpr(const RelType type, const Symbol &s,
430 const uint8_t *loc) const {
431 switch (type) {
432 case R_LARCH_NONE:
433 return R_NONE;
434 case R_LARCH_32:
435 case R_LARCH_64:
436 return R_ABS;
437 case R_LARCH_ADD6:
438 case R_LARCH_ADD8:
439 case R_LARCH_ADD16:
440 case R_LARCH_ADD32:
441 case R_LARCH_ADD64:
442 case R_LARCH_ADD_ULEB128:
443 case R_LARCH_SUB6:
444 case R_LARCH_SUB8:
445 case R_LARCH_SUB16:
446 case R_LARCH_SUB32:
447 case R_LARCH_SUB64:
448 case R_LARCH_SUB_ULEB128:
449 // The LoongArch add/sub relocs behave like the RISCV counterparts; reuse
450 // the RelExpr to avoid code duplication.
451 return RE_RISCV_ADD;
452 case R_LARCH_32_PCREL:
453 case R_LARCH_64_PCREL:
454 case R_LARCH_PCREL20_S2:
455 case R_LARCH_PCADD_HI20:
456 return R_PC;
457 case R_LARCH_TLS_DTPREL32:
458 case R_LARCH_TLS_DTPREL64:
459 return R_DTPREL;
460 // TLS IE relocations, reverted by the extreme code model fallback in
461 // relocateAlloc() .
462 case R_LARCH_TLS_IE_PC_HI20:
463 return RE_LOONGARCH_GOT_PAGE_PC;
464 case R_LARCH_TLS_IE_PC_LO12:
465 return RE_LOONGARCH_GOT;
466 // TLS DESC relocations, similarly reverted by the extreme code model
467 // fallback in relocateAlloc().
468 case R_LARCH_TLS_DESC_PC_HI20:
469 return RE_LOONGARCH_TLSDESC_PAGE_PC;
470 case R_LARCH_TLS_DESC_PC_LO12:
471 case R_LARCH_TLS_DESC_LD:
472 return R_TLSDESC;
473 case R_LARCH_TLS_DESC_PCREL20_S2:
474 return R_TLSDESC_PC;
475 default:
476 Err(ctx) << getErrorLoc(ctx, loc) << "unknown relocation (" << type.v
477 << ") against symbol " << &s;
478 return R_NONE;
479 }
480}
481
482bool LoongArch::usesOnlyLowPageBits(RelType type) const {
483 switch (type) {
484 default:
485 return false;
486 case R_LARCH_PCALA_LO12:
487 case R_LARCH_GOT_LO12:
488 case R_LARCH_GOT_PC_LO12:
489 case R_LARCH_TLS_IE_PC_LO12:
490 case R_LARCH_TLS_DESC_LO12:
491 case R_LARCH_TLS_DESC_PC_LO12:
492 return true;
493 }
494}
495
496template <class RelTy>
497void LoongArch::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels,
498 unsigned shard) {
499 RelocScan rs(ctx, &sec, shard);
500 sec.relocations.reserve(N: rels.size());
501 for (auto it = rels.begin(); it != rels.end(); ++it) {
502 RelType type = it->getType(false);
503 uint32_t symIndex = it->getSymbol(false);
504 Symbol &sym = sec.file->getSymbol(symbolIndex: symIndex);
505 uint64_t offset = it->r_offset;
506 if (sym.isUndefined() && symIndex != 0 &&
507 rs.maybeReportUndefined(sym&: cast<Undefined>(Val&: sym), offset))
508 continue;
509 int64_t addend = rs.getAddend(*it, type);
510 RelExpr expr;
511 // Relocation types that only need a RelExpr set `expr` and break out of
512 // the switch to reach rs.process(). Types that need special handling
513 // (fast-path helpers, TLS) call a handler and use `continue`.
514 switch (type) {
515 case R_LARCH_NONE:
516 case R_LARCH_MARK_LA:
517 case R_LARCH_MARK_PCREL:
518 continue;
519
520 // Absolute relocations:
521 case R_LARCH_32:
522 case R_LARCH_64:
523 case R_LARCH_ABS_HI20:
524 case R_LARCH_ABS_LO12:
525 case R_LARCH_ABS64_LO20:
526 case R_LARCH_ABS64_HI12:
527 expr = R_ABS;
528 break;
529
530 case R_LARCH_PCALA_LO12:
531 // R_LARCH_PCALA_LO12 on JIRL is used for function calls (glibc 2.37).
532 expr = isJirl(insn: read32le(P: sec.content().data() + offset)) ? R_PLT : R_ABS;
533 break;
534
535 // PC-indirect relocations (lo12 paired with a preceding hi20 pcadd):
536 case R_LARCH_PCADD_LO12:
537 case R_LARCH_GOT_PCADD_LO12:
538 case R_LARCH_TLS_IE_PCADD_LO12:
539 case R_LARCH_TLS_LD_PCADD_LO12:
540 case R_LARCH_TLS_GD_PCADD_LO12:
541 case R_LARCH_TLS_DESC_PCADD_LO12:
542 expr = RE_LOONGARCH_PC_INDIRECT;
543 break;
544
545 // PC-relative relocations:
546 case R_LARCH_32_PCREL:
547 case R_LARCH_64_PCREL:
548 case R_LARCH_PCREL20_S2:
549 case R_LARCH_PCADD_HI20:
550 rs.processR_PC(type, offset, addend, sym);
551 continue;
552
553 // PLT-generating relocations:
554 case R_LARCH_B16:
555 case R_LARCH_B21:
556 case R_LARCH_B26:
557 case R_LARCH_CALL30:
558 case R_LARCH_CALL36:
559 rs.processR_PLT_PC(type, offset, addend, sym);
560 continue;
561
562 // Page-PC relocations:
563 case R_LARCH_PCALA_HI20:
564 // Why not RE_LOONGARCH_PAGE_PC, majority of references don't go through
565 // PLT anyway so why waste time checking only to get everything relaxed
566 // back to it?
567 //
568 // This is again due to the R_LARCH_PCALA_LO12 on JIRL case, where we want
569 // both the HI20 and LO12 to potentially refer to the PLT. But in reality
570 // the HI20 reloc appears earlier, and the relocs don't contain enough
571 // information to let us properly resolve semantics per symbol.
572 // Unlike RISCV, our LO12 relocs *do not* point to their corresponding
573 // HI20 relocs, hence it is nearly impossible to 100% accurately determine
574 // each HI20's "flavor" without taking big performance hits, in the
575 // presence of edge cases (e.g. HI20 without pairing LO12; paired LO12
576 // placed so far apart that relationship is not certain anymore), and
577 // programmer mistakes (e.g. as outlined in
578 // https://github.com/loongson/la-abi-specs/pull/3).
579 //
580 // Ideally we would scan in an extra pass for all LO12s on JIRL, then mark
581 // every HI20 reloc referring to the same symbol differently; this is not
582 // feasible with the current function signature of getRelExpr that doesn't
583 // allow for such inter-pass state.
584 //
585 // So, unfortunately we have to again workaround this quirk the same way
586 // as BFD: assuming every R_LARCH_PCALA_HI20 is potentially PLT-needing,
587 // only relaxing back to RE_LOONGARCH_PAGE_PC if it's known not so at a
588 // later stage.
589 expr = RE_LOONGARCH_PLT_PAGE_PC;
590 break;
591 case R_LARCH_PCALA64_LO20:
592 case R_LARCH_PCALA64_HI12:
593 expr = RE_LOONGARCH_PAGE_PC;
594 break;
595
596 // GOT-generating relocations:
597 case R_LARCH_GOT_PC_HI20:
598 case R_LARCH_GOT64_PC_LO20:
599 case R_LARCH_GOT64_PC_HI12:
600 expr = RE_LOONGARCH_GOT_PAGE_PC;
601 break;
602 case R_LARCH_GOT_PCADD_HI20:
603 expr = R_GOT_PC;
604 break;
605 case R_LARCH_GOT_PC_LO12:
606 expr = RE_LOONGARCH_GOT;
607 break;
608 case R_LARCH_GOT_HI20:
609 case R_LARCH_GOT_LO12:
610 case R_LARCH_GOT64_LO20:
611 case R_LARCH_GOT64_HI12:
612 expr = R_GOT;
613 break;
614
615 // DTPREL relocations:
616 case R_LARCH_TLS_DTPREL32:
617 case R_LARCH_TLS_DTPREL64:
618 expr = R_DTPREL;
619 break;
620
621 // TLS LE relocations:
622 case R_LARCH_TLS_TPREL32:
623 case R_LARCH_TLS_TPREL64:
624 case R_LARCH_TLS_LE_HI20:
625 case R_LARCH_TLS_LE_HI20_R:
626 case R_LARCH_TLS_LE_LO12:
627 case R_LARCH_TLS_LE_LO12_R:
628 case R_LARCH_TLS_LE64_LO20:
629 case R_LARCH_TLS_LE64_HI12:
630 if (rs.checkTlsLe(offset, sym, type))
631 continue;
632 expr = R_TPREL;
633 break;
634 // TLS IE relocations (optimizable to LE in non-extreme code model):
635 case R_LARCH_TLS_IE_PC_HI20:
636 rs.handleTlsIe(ieExpr: RE_LOONGARCH_GOT_PAGE_PC, type, offset, addend, sym);
637 continue;
638 case R_LARCH_TLS_IE_PC_LO12:
639 rs.handleTlsIe(ieExpr: RE_LOONGARCH_GOT, type, offset, addend, sym);
640 continue;
641 // TLS IE relocations (extreme code model, no IE->LE optimization):
642 case R_LARCH_TLS_IE64_PC_LO20:
643 case R_LARCH_TLS_IE64_PC_HI12:
644 rs.handleTlsIe<false>(ieExpr: RE_LOONGARCH_GOT_PAGE_PC, type, offset, addend,
645 sym);
646 continue;
647 // TLS IE relocations (pcadd/absolute, no IE->LE optimization):
648 case R_LARCH_TLS_IE_PCADD_HI20:
649 rs.handleTlsIe<false>(ieExpr: R_GOT_PC, type, offset, addend, sym);
650 continue;
651 case R_LARCH_TLS_IE_HI20:
652 case R_LARCH_TLS_IE_LO12:
653 case R_LARCH_TLS_IE64_LO20:
654 case R_LARCH_TLS_IE64_HI12:
655 rs.handleTlsIe<false>(ieExpr: R_GOT, type, offset, addend, sym);
656 continue;
657 // TLS GD/LD relocations (no GD/LD->IE/LE optimization):
658 case R_LARCH_TLS_LD_PC_HI20:
659 case R_LARCH_TLS_GD_PC_HI20:
660 sym.setFlags(NEEDS_TLSGD);
661 sec.addReloc(r: {.expr: RE_LOONGARCH_TLSGD_PAGE_PC, .type: type, .offset: offset, .addend: addend, .sym: &sym});
662 continue;
663 case R_LARCH_TLS_LD_HI20:
664 ctx.needsTlsLd.store(i: true, m: std::memory_order_relaxed);
665 sec.addReloc(r: {.expr: R_TLSLD_GOT, .type: type, .offset: offset, .addend: addend, .sym: &sym});
666 continue;
667 case R_LARCH_TLS_GD_HI20:
668 sym.setFlags(NEEDS_TLSGD);
669 sec.addReloc(r: {.expr: R_TLSGD_GOT, .type: type, .offset: offset, .addend: addend, .sym: &sym});
670 continue;
671 case R_LARCH_TLS_LD_PCREL20_S2:
672 case R_LARCH_TLS_LD_PCADD_HI20:
673 ctx.needsTlsLd.store(i: true, m: std::memory_order_relaxed);
674 sec.addReloc(r: {.expr: R_TLSLD_PC, .type: type, .offset: offset, .addend: addend, .sym: &sym});
675 continue;
676 case R_LARCH_TLS_GD_PCREL20_S2:
677 case R_LARCH_TLS_GD_PCADD_HI20:
678 sym.setFlags(NEEDS_TLSGD);
679 sec.addReloc(r: {.expr: R_TLSGD_PC, .type: type, .offset: offset, .addend: addend, .sym: &sym});
680 continue;
681
682 // TLSDESC relocations (optimizable to IE/LE in non-extreme code model):
683 case R_LARCH_TLS_DESC_PC_HI20:
684 rs.handleTlsDesc(sharedExpr: RE_LOONGARCH_TLSDESC_PAGE_PC, ieExpr: RE_LOONGARCH_GOT_PAGE_PC,
685 type, offset, addend, sym);
686 continue;
687 case R_LARCH_TLS_DESC_PC_LO12:
688 case R_LARCH_TLS_DESC_LD:
689 rs.handleTlsDesc(sharedExpr: R_TLSDESC, ieExpr: RE_LOONGARCH_GOT_PAGE_PC, type, offset,
690 addend, sym);
691 continue;
692 case R_LARCH_TLS_DESC_PCREL20_S2:
693 rs.handleTlsDesc(sharedExpr: R_TLSDESC_PC, ieExpr: RE_LOONGARCH_GOT_PAGE_PC, type, offset,
694 addend, sym);
695 continue;
696 case R_LARCH_TLS_DESC_CALL:
697 if (!ctx.arg.shared)
698 sec.addReloc(
699 r: {.expr: sym.isPreemptible ? R_GOT : R_TPREL, .type: type, .offset: offset, .addend: addend, .sym: &sym});
700 continue;
701 // TLSDESC relocations (extreme code model, no optimization):
702 case R_LARCH_TLS_DESC64_PC_LO20:
703 case R_LARCH_TLS_DESC64_PC_HI12:
704 sym.setFlags(NEEDS_TLSDESC);
705 sec.addReloc(r: {.expr: RE_LOONGARCH_TLSDESC_PAGE_PC, .type: type, .offset: offset, .addend: addend, .sym: &sym});
706 continue;
707 // TLSDESC relocations (absolute/pcadd, no optimization):
708 case R_LARCH_TLS_DESC_HI20:
709 case R_LARCH_TLS_DESC_LO12:
710 case R_LARCH_TLS_DESC64_LO20:
711 case R_LARCH_TLS_DESC64_HI12:
712 case R_LARCH_TLS_DESC_PCADD_HI20:
713 sym.setFlags(NEEDS_TLSDESC);
714 sec.addReloc(r: {.expr: R_TLSDESC, .type: type, .offset: offset, .addend: addend, .sym: &sym});
715 continue;
716
717 // Relaxation hints:
718 case R_LARCH_TLS_LE_ADD_R:
719 case R_LARCH_RELAX:
720 if (ctx.arg.relax)
721 sec.addReloc(r: {.expr: R_RELAX_HINT, .type: type, .offset: offset, .addend: addend, .sym: &sym});
722 continue;
723 case R_LARCH_ALIGN:
724 sec.addReloc(r: {.expr: R_RELAX_HINT, .type: type, .offset: offset, .addend: addend, .sym: &sym});
725 continue;
726
727 // Misc relocations:
728 case R_LARCH_ADD6:
729 case R_LARCH_ADD8:
730 case R_LARCH_ADD16:
731 case R_LARCH_ADD32:
732 case R_LARCH_ADD64:
733 case R_LARCH_ADD_ULEB128:
734 case R_LARCH_SUB6:
735 case R_LARCH_SUB8:
736 case R_LARCH_SUB16:
737 case R_LARCH_SUB32:
738 case R_LARCH_SUB64:
739 case R_LARCH_SUB_ULEB128:
740 expr = RE_RISCV_ADD;
741 break;
742
743 default:
744 Err(ctx) << getErrorLoc(ctx, loc: sec.content().data() + offset)
745 << "unknown relocation (" << type.v << ") against symbol "
746 << &sym;
747 continue;
748 }
749 rs.process(expr, type, offset, sym, addend);
750 }
751
752 llvm::stable_sort(sec.relocs(),
753 [](const Relocation &lhs, const Relocation &rhs) {
754 return lhs.offset < rhs.offset;
755 });
756}
757
758void LoongArch::relocate(uint8_t *loc, const Relocation &rel,
759 uint64_t val) const {
760 switch (rel.type) {
761 case R_LARCH_32_PCREL:
762 checkInt(ctx, loc, v: val, n: 32, rel);
763 [[fallthrough]];
764 case R_LARCH_32:
765 case R_LARCH_TLS_DTPREL32:
766 write32le(P: loc, V: val);
767 return;
768 case R_LARCH_64:
769 case R_LARCH_TLS_DTPREL64:
770 case R_LARCH_64_PCREL:
771 write64le(P: loc, V: val);
772 return;
773
774 // Relocs intended for `pcaddi`.
775 case R_LARCH_PCREL20_S2:
776 case R_LARCH_TLS_LD_PCREL20_S2:
777 case R_LARCH_TLS_GD_PCREL20_S2:
778 case R_LARCH_TLS_DESC_PCREL20_S2:
779 checkInt(ctx, loc, v: val, n: 22, rel);
780 checkAlignment(ctx, loc, v: val, n: 4, rel);
781 write32le(P: loc, V: setJ20(insn: read32le(P: loc), imm: val >> 2));
782 return;
783
784 case R_LARCH_B16:
785 checkInt(ctx, loc, v: val, n: 18, rel);
786 checkAlignment(ctx, loc, v: val, n: 4, rel);
787 write32le(P: loc, V: setK16(insn: read32le(P: loc), imm: val >> 2));
788 return;
789
790 case R_LARCH_B21:
791 checkInt(ctx, loc, v: val, n: 23, rel);
792 checkAlignment(ctx, loc, v: val, n: 4, rel);
793 write32le(P: loc, V: setD5k16(insn: read32le(P: loc), imm: val >> 2));
794 return;
795
796 case R_LARCH_B26:
797 checkInt(ctx, loc, v: val, n: 28, rel);
798 checkAlignment(ctx, loc, v: val, n: 4, rel);
799 write32le(P: loc, V: setD10k16(insn: read32le(P: loc), imm: val >> 2));
800 return;
801
802 case R_LARCH_CALL30: {
803 // This relocation is designed for adjacent pcaddu12i+jirl pairs that
804 // are patched in one time.
805 // The relocation range is [-2G, +2G) (of course must be 4-byte aligned).
806 checkInt(ctx, loc, v: val, n: 32, rel);
807 checkAlignment(ctx, loc, v: val, n: 4, rel);
808 // Although jirl adds the immediate as a signed value, it is always positive
809 // in this case, so no adjustment is needed, unlike CALL36.
810 uint32_t hi20 = extractBits(v: val, begin: 31, end: 12);
811 // Despite the name, the lower part is actually 12 bits with 4-byte aligned.
812 uint32_t lo10 = extractBits(v: val, begin: 11, end: 2);
813 write32le(P: loc, V: setJ20(insn: read32le(P: loc), imm: hi20));
814 write32le(P: loc + 4, V: setK16(insn: read32le(P: loc + 4), imm: lo10));
815 return;
816 }
817
818 case R_LARCH_CALL36: {
819 // This relocation is designed for adjacent pcaddu18i+jirl pairs that
820 // are patched in one time. Because of sign extension of these insns'
821 // immediate fields, the relocation range is [-128G - 0x20000, +128G -
822 // 0x20000) (of course must be 4-byte aligned).
823 if (((int64_t)val + 0x20000) != llvm::SignExtend64(X: val + 0x20000, B: 38))
824 reportRangeError(ctx, loc, rel, v: Twine(val), min: llvm::minIntN(N: 38) - 0x20000,
825 max: llvm::maxIntN(N: 38) - 0x20000);
826 checkAlignment(ctx, loc, v: val, n: 4, rel);
827 // Since jirl performs sign extension on the offset immediate, adds (1<<17)
828 // to original val to get the correct hi20.
829 uint32_t hi20 = extractBits(v: val + (1 << 17), begin: 37, end: 18);
830 // Despite the name, the lower part is actually 18 bits with 4-byte aligned.
831 uint32_t lo16 = extractBits(v: val, begin: 17, end: 2);
832 write32le(P: loc, V: setJ20(insn: read32le(P: loc), imm: hi20));
833 write32le(P: loc + 4, V: setK16(insn: read32le(P: loc + 4), imm: lo16));
834 return;
835 }
836
837 // Relocs intended for `addi`, `ld` or `st`.
838 case R_LARCH_PCALA_LO12:
839 // We have to again inspect the insn word to handle the R_LARCH_PCALA_LO12
840 // on JIRL case: firstly JIRL wants its immediate's 2 lowest zeroes
841 // removed by us (in contrast to regular R_LARCH_PCALA_LO12), secondly
842 // its immediate slot width is different too (16, not 12).
843 // In this case, process like an R_LARCH_B16, but without overflow checking
844 // and only taking the value's lowest 12 bits.
845 if (isJirl(insn: read32le(P: loc))) {
846 checkAlignment(ctx, loc, v: val, n: 4, rel);
847 val = SignExtend64<12>(x: val);
848 write32le(P: loc, V: setK16(insn: read32le(P: loc), imm: val >> 2));
849 return;
850 }
851 [[fallthrough]];
852 case R_LARCH_ABS_LO12:
853 case R_LARCH_GOT_PC_LO12:
854 case R_LARCH_GOT_LO12:
855 case R_LARCH_TLS_LE_LO12:
856 case R_LARCH_TLS_IE_PC_LO12:
857 case R_LARCH_TLS_IE_LO12:
858 case R_LARCH_TLS_LE_LO12_R:
859 case R_LARCH_TLS_DESC_PC_LO12:
860 case R_LARCH_TLS_DESC_LO12:
861 case R_LARCH_PCADD_LO12:
862 case R_LARCH_GOT_PCADD_LO12:
863 case R_LARCH_TLS_IE_PCADD_LO12:
864 case R_LARCH_TLS_LD_PCADD_LO12:
865 case R_LARCH_TLS_GD_PCADD_LO12:
866 case R_LARCH_TLS_DESC_PCADD_LO12:
867 write32le(P: loc, V: setK12(insn: read32le(P: loc), imm: extractBits(v: val, begin: 11, end: 0)));
868 return;
869
870 // Relocs intended for `lu12i.w` or `pcalau12i`.
871 case R_LARCH_ABS_HI20:
872 case R_LARCH_PCALA_HI20:
873 case R_LARCH_GOT_PC_HI20:
874 case R_LARCH_GOT_HI20:
875 case R_LARCH_TLS_LE_HI20:
876 case R_LARCH_TLS_IE_PC_HI20:
877 case R_LARCH_TLS_IE_HI20:
878 case R_LARCH_TLS_LD_PC_HI20:
879 case R_LARCH_TLS_LD_HI20:
880 case R_LARCH_TLS_GD_PC_HI20:
881 case R_LARCH_TLS_GD_HI20:
882 case R_LARCH_TLS_DESC_PC_HI20:
883 case R_LARCH_TLS_DESC_HI20:
884 write32le(P: loc, V: setJ20(insn: read32le(P: loc), imm: extractBits(v: val, begin: 31, end: 12)));
885 return;
886 case R_LARCH_PCADD_HI20:
887 case R_LARCH_GOT_PCADD_HI20:
888 case R_LARCH_TLS_IE_PCADD_HI20:
889 case R_LARCH_TLS_LD_PCADD_HI20:
890 case R_LARCH_TLS_GD_PCADD_HI20:
891 case R_LARCH_TLS_DESC_PCADD_HI20: {
892 uint64_t hi = val + 0x800;
893 checkInt(ctx, loc, v: SignExtend64(X: hi, B: ctx.arg.wordsize * 8) >> 12, n: 20, rel);
894 write32le(P: loc, V: setJ20(insn: read32le(P: loc), imm: extractBits(v: hi, begin: 31, end: 12)));
895 return;
896 }
897 case R_LARCH_TLS_LE_HI20_R:
898 write32le(P: loc, V: setJ20(insn: read32le(P: loc), imm: extractBits(v: val + 0x800, begin: 31, end: 12)));
899 return;
900
901 // Relocs intended for `lu32i.d`.
902 case R_LARCH_ABS64_LO20:
903 case R_LARCH_PCALA64_LO20:
904 case R_LARCH_GOT64_PC_LO20:
905 case R_LARCH_GOT64_LO20:
906 case R_LARCH_TLS_LE64_LO20:
907 case R_LARCH_TLS_IE64_PC_LO20:
908 case R_LARCH_TLS_IE64_LO20:
909 case R_LARCH_TLS_DESC64_PC_LO20:
910 case R_LARCH_TLS_DESC64_LO20:
911 write32le(P: loc, V: setJ20(insn: read32le(P: loc), imm: extractBits(v: val, begin: 51, end: 32)));
912 return;
913
914 // Relocs intended for `lu52i.d`.
915 case R_LARCH_ABS64_HI12:
916 case R_LARCH_PCALA64_HI12:
917 case R_LARCH_GOT64_PC_HI12:
918 case R_LARCH_GOT64_HI12:
919 case R_LARCH_TLS_LE64_HI12:
920 case R_LARCH_TLS_IE64_PC_HI12:
921 case R_LARCH_TLS_IE64_HI12:
922 case R_LARCH_TLS_DESC64_PC_HI12:
923 case R_LARCH_TLS_DESC64_HI12:
924 write32le(P: loc, V: setK12(insn: read32le(P: loc), imm: extractBits(v: val, begin: 63, end: 52)));
925 return;
926
927 case R_LARCH_ADD6:
928 *loc = (*loc & 0xc0) | ((*loc + val) & 0x3f);
929 return;
930 case R_LARCH_ADD8:
931 *loc += val;
932 return;
933 case R_LARCH_ADD16:
934 write16le(P: loc, V: read16le(P: loc) + val);
935 return;
936 case R_LARCH_ADD32:
937 write32le(P: loc, V: read32le(P: loc) + val);
938 return;
939 case R_LARCH_ADD64:
940 write64le(P: loc, V: read64le(P: loc) + val);
941 return;
942 case R_LARCH_ADD_ULEB128:
943 handleUleb128(ctx, loc, val);
944 return;
945 case R_LARCH_SUB6:
946 *loc = (*loc & 0xc0) | ((*loc - val) & 0x3f);
947 return;
948 case R_LARCH_SUB8:
949 *loc -= val;
950 return;
951 case R_LARCH_SUB16:
952 write16le(P: loc, V: read16le(P: loc) - val);
953 return;
954 case R_LARCH_SUB32:
955 write32le(P: loc, V: read32le(P: loc) - val);
956 return;
957 case R_LARCH_SUB64:
958 write64le(P: loc, V: read64le(P: loc) - val);
959 return;
960 case R_LARCH_SUB_ULEB128:
961 handleUleb128(ctx, loc, val: -val);
962 return;
963
964 case R_LARCH_MARK_LA:
965 case R_LARCH_MARK_PCREL:
966 // no-op
967 return;
968
969 case R_LARCH_TLS_LE_ADD_R:
970 case R_LARCH_RELAX:
971 return; // Ignored (for now)
972
973 case R_LARCH_TLS_DESC_LD:
974 return; // nothing to do.
975 case R_LARCH_TLS_DESC32:
976 write32le(P: loc + 4, V: val);
977 return;
978 case R_LARCH_TLS_DESC64:
979 write64le(P: loc + 8, V: val);
980 return;
981
982 default:
983 llvm_unreachable("unknown relocation");
984 }
985}
986
987// If the section alignment is > 4, advance `dot` to insert NOPs and synthesize
988// an ALIGN relocation. Otherwise, return false to use default handling.
989template <class ELFT, class RelTy>
990bool LoongArch::synthesizeAlignForInput(uint64_t &dot, InputSection *sec,
991 Relocs<RelTy> rels) {
992 if (!baseSec) {
993 // Record the first input section with RELAX relocations. We will synthesize
994 // ALIGN relocations here.
995 for (auto rel : rels) {
996 if (rel.getType(false) == R_LARCH_RELAX) {
997 baseSec = sec;
998 break;
999 }
1000 }
1001 } else if (sec->addralign > 4) {
1002 // If the alignment is > 4, synthesize an ALIGN unless an ALIGN relocation
1003 // at offset 0 already guarantees `addralign`. Note: A weaker ALIGN at
1004 // offset 0 from older assemblers do not suppress synthesis (e.g.
1005 // `.p2align 2; .option norelax; nop; .p2align 3` does not suppress
1006 // synthesized `.p2align 3`).
1007 bool covered = llvm::any_of(rels, [&](const RelTy &rel) {
1008 if (rel.r_offset != 0 || rel.getType(false) != R_LARCH_ALIGN)
1009 return false;
1010 if constexpr (RelTy::HasAddend)
1011 return uint64_t(rel.r_addend) >= sec->addralign - 4;
1012 return false;
1013 });
1014 if (!covered) {
1015 synthesizedAligns.emplace_back(Args: dot - baseSec->getVA(),
1016 Args: sec->addralign - 4);
1017 dot += sec->addralign - 4;
1018 return true;
1019 }
1020 }
1021 return false;
1022}
1023
1024// Finalize the relocation section by appending synthesized ALIGN relocations
1025// after processing all input sections.
1026template <class ELFT, class RelTy>
1027void LoongArch::finalizeSynthesizeAligns(uint64_t &dot, InputSection *sec,
1028 Relocs<RelTy> rels) {
1029 auto *f = cast<ObjFile<ELFT>>(baseSec->file);
1030 auto shdr = f->template getELFShdrs<ELFT>()[baseSec->relSecIdx];
1031 // Create a copy of InputSection.
1032 sec = make<InputSection>(*f, shdr, baseSec->name);
1033 auto *baseRelSec = cast<InputSection>(f->getSections()[baseSec->relSecIdx]);
1034 *sec = *baseRelSec;
1035 baseSec = nullptr;
1036
1037 // Allocate buffer for original and synthesized relocations in RELA format.
1038 // If CREL is used, OutputSection::finalizeNonAllocCrel will convert RELA to
1039 // CREL.
1040 auto newSize = rels.size() + synthesizedAligns.size();
1041 auto *relas = makeThreadLocalN<typename ELFT::Rela>(newSize);
1042 sec->size = newSize * sizeof(typename ELFT::Rela);
1043 sec->content_ = reinterpret_cast<uint8_t *>(relas);
1044 sec->type = SHT_RELA;
1045 // Copy original relocations to the new buffer, potentially converting CREL to
1046 // RELA.
1047 for (auto [i, r] : llvm::enumerate(rels)) {
1048 relas[i].r_offset = r.r_offset;
1049 relas[i].setSymbolAndType(r.getSymbol(0), r.getType(0), false);
1050 if constexpr (RelTy::HasAddend)
1051 relas[i].r_addend = r.r_addend;
1052 }
1053 // Append synthesized ALIGN relocations to the buffer.
1054 for (auto [i, r] : llvm::enumerate(First&: synthesizedAligns)) {
1055 auto &rela = relas[rels.size() + i];
1056 rela.r_offset = r.first;
1057 rela.setSymbolAndType(0, R_LARCH_ALIGN, false);
1058 rela.r_addend = r.second;
1059 }
1060 synthesizedAligns.clear();
1061 // Replace the old relocation section with the new one in the output section.
1062 // addOrphanSections ensures that the output relocation section is processed
1063 // after osec.
1064 for (SectionCommand *cmd : sec->getParent()->commands) {
1065 auto *isd = dyn_cast<InputSectionDescription>(Val: cmd);
1066 if (!isd)
1067 continue;
1068 for (auto *&isec : isd->sections)
1069 if (isec == baseRelSec)
1070 isec = sec;
1071 }
1072}
1073
1074template <class ELFT>
1075bool LoongArch::synthesizeAlignAux(uint64_t &dot, InputSection *sec) {
1076 bool ret = false;
1077 if (sec) {
1078 invokeOnRelocs(*sec, ret = synthesizeAlignForInput<ELFT>, dot, sec);
1079 } else if (baseSec) {
1080 invokeOnRelocs(*baseSec, finalizeSynthesizeAligns<ELFT>, dot, sec);
1081 }
1082 return ret;
1083}
1084
1085// Without linker relaxation enabled for a particular relocatable file or
1086// section, the assembler will not generate R_LARCH_ALIGN relocations for
1087// alignment directives. This becomes problematic in a two-stage linking
1088// process: ld -r a.o b.o -o ab.o; ld ab.o -o ab. This function synthesizes an
1089// R_LARCH_ALIGN relocation at section start when needed.
1090//
1091// When called with an input section (`sec` is not null): If the section
1092// alignment is > 4, advance `dot` to insert NOPs and synthesize an ALIGN
1093// relocation.
1094//
1095// When called after all input sections are processed (`sec` is null): The
1096// output relocation section is updated with all the newly synthesized ALIGN
1097// relocations.
1098bool LoongArch::synthesizeAlign(uint64_t &dot, InputSection *sec) {
1099 assert(ctx.arg.relocatable);
1100 if (ctx.arg.is64)
1101 return synthesizeAlignAux<ELF64LE>(dot, sec);
1102 return synthesizeAlignAux<ELF32LE>(dot, sec);
1103}
1104
1105static bool relaxable(ArrayRef<Relocation> relocs, size_t i) {
1106 return i + 1 < relocs.size() && relocs[i + 1].type == R_LARCH_RELAX;
1107}
1108
1109static bool isPairRelaxable(ArrayRef<Relocation> relocs, size_t i) {
1110 return relaxable(relocs, i) && relaxable(relocs, i: i + 2) &&
1111 relocs[i].offset + 4 == relocs[i + 2].offset;
1112}
1113
1114// Relax code sequence.
1115// From:
1116// pcalau12i $a0, %pc_hi20(sym) | %ld_pc_hi20(sym) | %gd_pc_hi20(sym)
1117// | %desc_pc_hi20(sym)
1118// addi.w/d $a0, $a0, %pc_lo12(sym) | %got_pc_lo12(sym) | %got_pc_lo12(sym)
1119// | %desc_pc_lo12(sym)
1120// To:
1121// pcaddi $a0, %pc_lo12(sym) | %got_pc_lo12(sym) | %got_pc_lo12(sym)
1122// | %desc_pcrel_20(sym)
1123//
1124// From:
1125// pcalau12i $a0, %got_pc_hi20(sym_got)
1126// ld.w/d $a0, $a0, %got_pc_lo12(sym_got)
1127// To:
1128// pcaddi $a0, %got_pc_hi20(sym_got)
1129static void relaxPCHi20Lo12(Ctx &ctx, const InputSection &sec, size_t i,
1130 uint64_t loc, Relocation &rHi20, Relocation &rLo12,
1131 uint32_t &remove, uint32_t prevRemove) {
1132 // check if the relocations are relaxable sequences.
1133 if (!((rHi20.type == R_LARCH_PCALA_HI20 &&
1134 rLo12.type == R_LARCH_PCALA_LO12) ||
1135 (rHi20.type == R_LARCH_GOT_PC_HI20 &&
1136 rLo12.type == R_LARCH_GOT_PC_LO12) ||
1137 (rHi20.type == R_LARCH_TLS_GD_PC_HI20 &&
1138 rLo12.type == R_LARCH_GOT_PC_LO12) ||
1139 (rHi20.type == R_LARCH_TLS_LD_PC_HI20 &&
1140 rLo12.type == R_LARCH_GOT_PC_LO12) ||
1141 (rHi20.type == R_LARCH_TLS_DESC_PC_HI20 &&
1142 rLo12.type == R_LARCH_TLS_DESC_PC_LO12)))
1143 return;
1144
1145 // GOT references to absolute symbols can't be relaxed to use pcaddi in
1146 // position-independent code, because these instructions produce a relative
1147 // address.
1148 // Meanwhile skip undefined, preemptible and STT_GNU_IFUNC symbols, because
1149 // these symbols may be resolve in runtime.
1150 // Moreover, relaxation can only occur if the addends of both relocations are
1151 // zero for GOT references.
1152 if (rHi20.type == R_LARCH_GOT_PC_HI20 &&
1153 (!rHi20.sym || rHi20.sym != rLo12.sym || !rHi20.sym->isDefined() ||
1154 rHi20.sym->isPreemptible || rHi20.sym->isGnuIFunc() ||
1155 (ctx.arg.isPic && !cast<Defined>(Val&: *rHi20.sym).section) ||
1156 rHi20.addend != 0 || rLo12.addend != 0))
1157 return;
1158
1159 uint64_t dest = 0;
1160 if (rHi20.expr == RE_LOONGARCH_PLT_PAGE_PC)
1161 dest = rHi20.sym->getPltVA(ctx);
1162 else if (rHi20.expr == RE_LOONGARCH_PAGE_PC ||
1163 rHi20.expr == RE_LOONGARCH_GOT_PAGE_PC)
1164 dest = rHi20.sym->getVA(ctx);
1165 else if (rHi20.expr == RE_LOONGARCH_TLSGD_PAGE_PC)
1166 dest = ctx.in.got->getGlobalDynAddr(b: *rHi20.sym);
1167 else if (rHi20.expr == RE_LOONGARCH_TLSDESC_PAGE_PC)
1168 dest = ctx.in.got->getTlsDescAddr(sym: *rHi20.sym);
1169 else {
1170 Err(ctx) << getErrorLoc(ctx, loc: (const uint8_t *)loc) << "unknown expr ("
1171 << rHi20.expr << ") against symbol " << rHi20.sym
1172 << "in relaxPCHi20Lo12";
1173 return;
1174 }
1175 dest += rHi20.addend;
1176
1177 const int64_t displace = dest - loc;
1178 // Check if the displace aligns 4 bytes or exceeds the range of pcaddi.
1179 if ((displace & 0x3) != 0 || !isInt<22>(x: displace))
1180 return;
1181
1182 // Note: If we can ensure that the .o files generated by LLVM only contain
1183 // relaxable instruction sequences with R_LARCH_RELAX, then we do not need to
1184 // decode instructions. The relaxable instruction sequences imply the
1185 // following constraints:
1186 // * For relocation pairs related to got_pc, the opcodes of instructions
1187 // must be pcalau12i + ld.w/d. In other cases, the opcodes must be pcalau12i +
1188 // addi.w/d.
1189 // * The destination register of pcalau12i is guaranteed to be used only by
1190 // the immediately following instruction.
1191 const uint32_t currInsn = read32le(P: sec.content().data() + rHi20.offset);
1192 const uint32_t nextInsn = read32le(P: sec.content().data() + rLo12.offset);
1193 // Check if use the same register.
1194 if (getD5(v: currInsn) != getJ5(v: nextInsn) || getJ5(v: nextInsn) != getD5(v: nextInsn))
1195 return;
1196
1197 // When the caller specifies the old value of `remove`, disallow its
1198 // increment.
1199 if (prevRemove < 4)
1200 return;
1201
1202 sec.relaxAux->relocTypes[i] = R_LARCH_RELAX;
1203 if (rHi20.type == R_LARCH_TLS_GD_PC_HI20)
1204 sec.relaxAux->relocTypes[i + 2] = R_LARCH_TLS_GD_PCREL20_S2;
1205 else if (rHi20.type == R_LARCH_TLS_LD_PC_HI20)
1206 sec.relaxAux->relocTypes[i + 2] = R_LARCH_TLS_LD_PCREL20_S2;
1207 else if (rHi20.type == R_LARCH_TLS_DESC_PC_HI20)
1208 sec.relaxAux->relocTypes[i + 2] = R_LARCH_TLS_DESC_PCREL20_S2;
1209 else
1210 sec.relaxAux->relocTypes[i + 2] = R_LARCH_PCREL20_S2;
1211 sec.relaxAux->writes.push_back(Elt: insn(op: PCADDI, d: getD5(v: nextInsn), j: 0, k: 0));
1212 remove = 4;
1213}
1214
1215// Relax code sequence.
1216// From:
1217// la32r:
1218// pcaddu12i $ra, %call30(foo)
1219// jirl $ra, $ra, 0
1220// la32s/la64:
1221// pcaddu18i $ra, %call36(foo)
1222// jirl $ra, $ra, 0
1223// To:
1224// b/bl foo
1225static void relaxMediumCall(Ctx &ctx, const InputSection &sec, size_t i,
1226 uint64_t loc, Relocation &r, uint32_t &remove,
1227 uint32_t prevRemove) {
1228 const uint64_t dest =
1229 (r.expr == R_PLT_PC ? r.sym->getPltVA(ctx) : r.sym->getVA(ctx)) +
1230 r.addend;
1231
1232 const int64_t displace = dest - loc;
1233 // Check if the displace aligns 4 bytes or exceeds the range of b[l].
1234 if ((displace & 0x3) != 0 || !isInt<28>(x: displace))
1235 return;
1236
1237 // When the caller specifies the old value of `remove`, disallow its
1238 // increment.
1239 if (prevRemove < 4)
1240 return;
1241
1242 const uint32_t nextInsn = read32le(P: sec.content().data() + r.offset + 4);
1243 if (getD5(v: nextInsn) == R_RA) {
1244 // convert jirl to bl
1245 sec.relaxAux->relocTypes[i] = R_LARCH_B26;
1246 sec.relaxAux->writes.push_back(Elt: insn(op: BL, d: 0, j: 0, k: 0));
1247 remove = 4;
1248 } else if (getD5(v: nextInsn) == R_ZERO) {
1249 // convert jirl to b
1250 sec.relaxAux->relocTypes[i] = R_LARCH_B26;
1251 sec.relaxAux->writes.push_back(Elt: insn(op: B, d: 0, j: 0, k: 0));
1252 remove = 4;
1253 }
1254}
1255
1256// Relax code sequence.
1257// From:
1258// lu12i.w $rd, %le_hi20_r(sym)
1259// add.w/d $rd, $rd, $tp, %le_add_r(sym)
1260// addi/ld/st.w/d $rd, $rd, %le_lo12_r(sym)
1261// To:
1262// addi/ld/st.w/d $rd, $tp, %le_lo12_r(sym)
1263static void relaxTlsLe(Ctx &ctx, const InputSection &sec, size_t i,
1264 uint64_t loc, Relocation &r, uint32_t &remove) {
1265 uint64_t val = r.sym->getVA(ctx, addend: r.addend);
1266 // Check if the val exceeds the range of addi/ld/st.
1267 if (!isInt<12>(x: val))
1268 return;
1269 uint32_t currInsn = read32le(P: sec.content().data() + r.offset);
1270 switch (r.type) {
1271 case R_LARCH_TLS_LE_HI20_R:
1272 case R_LARCH_TLS_LE_ADD_R:
1273 sec.relaxAux->relocTypes[i] = R_LARCH_RELAX;
1274 remove = 4;
1275 break;
1276 case R_LARCH_TLS_LE_LO12_R:
1277 sec.relaxAux->writes.push_back(Elt: setJ5(insn: currInsn, imm: R_TP));
1278 sec.relaxAux->relocTypes[i] = R_LARCH_TLS_LE_LO12_R;
1279 break;
1280 }
1281}
1282
1283static bool relax(Ctx &ctx, int pass, InputSection &sec) {
1284 const uint64_t secAddr = sec.getVA();
1285 const MutableArrayRef<Relocation> relocs = sec.relocs();
1286 auto &aux = *sec.relaxAux;
1287 bool changed = false;
1288 ArrayRef<SymbolAnchor> sa = ArrayRef(aux.anchors);
1289 uint64_t delta = 0;
1290
1291 std::fill_n(first: aux.relocTypes.get(), n: relocs.size(), value: R_LARCH_NONE);
1292 aux.writes.clear();
1293 for (auto [i, r] : llvm::enumerate(First: relocs)) {
1294 const uint64_t loc = secAddr + r.offset - delta;
1295 uint32_t &cur = aux.relocDeltas[i], remove = 0;
1296 // Prevent oscillation between states by disallowing the increment of
1297 // `remove` after a few passes. The previous `remove` value is
1298 // `cur-delta`.
1299 uint32_t prevRemove = pass < 4 ? UINT32_MAX : cur - delta;
1300 switch (r.type) {
1301 case R_LARCH_ALIGN: {
1302 const uint64_t addend =
1303 r.sym->isUndefined() ? Log2_64(Value: r.addend) + 1 : r.addend;
1304 const uint64_t allBytes = (1ULL << (addend & 0xff)) - 4;
1305 const uint64_t align = 1ULL << (addend & 0xff);
1306 const uint64_t maxBytes = addend >> 8;
1307 const uint64_t off = loc & (align - 1);
1308 const uint64_t curBytes = off == 0 ? 0 : align - off;
1309 // All bytes beyond the alignment boundary should be removed.
1310 // If emit bytes more than max bytes to emit, remove all.
1311 if (maxBytes != 0 && curBytes > maxBytes)
1312 remove = allBytes;
1313 else
1314 remove = allBytes - curBytes;
1315 // If we can't satisfy this alignment, we've found a bad input.
1316 if (LLVM_UNLIKELY(static_cast<int32_t>(remove) < 0)) {
1317 Err(ctx) << getErrorLoc(ctx, loc: (const uint8_t *)loc)
1318 << "insufficient padding bytes for " << r.type << ": "
1319 << allBytes << " bytes available for "
1320 << "requested alignment of " << align << " bytes";
1321 remove = 0;
1322 }
1323 break;
1324 }
1325 case R_LARCH_PCALA_HI20:
1326 case R_LARCH_GOT_PC_HI20:
1327 case R_LARCH_TLS_GD_PC_HI20:
1328 case R_LARCH_TLS_LD_PC_HI20:
1329 // The overflow check for i+2 will be carried out in isPairRelaxable.
1330 if (isPairRelaxable(relocs, i))
1331 relaxPCHi20Lo12(ctx, sec, i, loc, rHi20&: r, rLo12&: relocs[i + 2], remove, prevRemove);
1332 break;
1333 case R_LARCH_TLS_DESC_PC_HI20:
1334 if (r.expr == RE_LOONGARCH_GOT_PAGE_PC || r.expr == R_TPREL) {
1335 if (relaxable(relocs, i))
1336 remove = 4;
1337 } else if (isPairRelaxable(relocs, i))
1338 relaxPCHi20Lo12(ctx, sec, i, loc, rHi20&: r, rLo12&: relocs[i + 2], remove, prevRemove);
1339 break;
1340 case R_LARCH_CALL30:
1341 case R_LARCH_CALL36:
1342 if (relaxable(relocs, i))
1343 relaxMediumCall(ctx, sec, i, loc, r, remove, prevRemove);
1344 break;
1345 case R_LARCH_TLS_LE_HI20_R:
1346 case R_LARCH_TLS_LE_ADD_R:
1347 case R_LARCH_TLS_LE_LO12_R:
1348 if (relaxable(relocs, i))
1349 relaxTlsLe(ctx, sec, i, loc, r, remove);
1350 break;
1351 case R_LARCH_TLS_IE_PC_HI20:
1352 if (relaxable(relocs, i) && r.expr == R_TPREL &&
1353 isUInt<12>(x: r.sym->getVA(ctx, addend: r.addend)))
1354 remove = 4;
1355 break;
1356 case R_LARCH_TLS_DESC_PC_LO12:
1357 if (relaxable(relocs, i) &&
1358 (r.expr == RE_LOONGARCH_GOT_PAGE_PC || r.expr == R_TPREL))
1359 remove = 4;
1360 break;
1361 case R_LARCH_TLS_DESC_LD:
1362 if (relaxable(relocs, i) && r.expr == R_TPREL &&
1363 isUInt<12>(x: r.sym->getVA(ctx, addend: r.addend)))
1364 remove = 4;
1365 break;
1366 }
1367
1368 // For all anchors whose offsets are <= r.offset, they are preceded by
1369 // the previous relocation whose `relocDeltas` value equals `delta`.
1370 // Decrease their st_value and update their st_size.
1371 for (; sa.size() && sa[0].offset <= r.offset; sa = sa.slice(N: 1)) {
1372 if (sa[0].end)
1373 sa[0].d->size = sa[0].offset - delta - sa[0].d->value;
1374 else
1375 sa[0].d->value = sa[0].offset - delta;
1376 }
1377 delta += remove;
1378 if (delta != cur) {
1379 cur = delta;
1380 changed = true;
1381 }
1382 }
1383
1384 for (const SymbolAnchor &a : sa) {
1385 if (a.end)
1386 a.d->size = a.offset - delta - a.d->value;
1387 else
1388 a.d->value = a.offset - delta;
1389 }
1390 // Inform assignAddresses that the size has changed.
1391 if (!isUInt<32>(x: delta))
1392 Fatal(ctx) << "section size decrease is too large: " << delta;
1393 sec.bytesDropped = delta;
1394 return changed;
1395}
1396
1397// Convert TLS IE to LE in the normal or medium code model.
1398// Original code sequence:
1399// * pcalau12i $a0, %ie_pc_hi20(sym)
1400// * ld.d $a0, $a0, %ie_pc_lo12(sym)
1401//
1402// The code sequence converted is as follows:
1403// * lu12i.w $a0, %le_hi20(sym) # le_hi20 != 0, otherwise NOP
1404// * ori $a0, src, %le_lo12(sym) # le_hi20 != 0, src = $a0,
1405// # otherwise, src = $zero
1406//
1407// When relaxation enables, redundant NOPs can be removed.
1408static void tlsIeToLe(uint8_t *loc, const Relocation &rel, uint64_t val) {
1409 assert(isInt<32>(val) &&
1410 "val exceeds the range of medium code model in tlsIeToLe");
1411
1412 bool isUInt12 = isUInt<12>(x: val);
1413 const uint32_t currInsn = read32le(P: loc);
1414 switch (rel.type) {
1415 case R_LARCH_TLS_IE_PC_HI20:
1416 if (isUInt12)
1417 write32le(P: loc, V: insn(op: ANDI, d: R_ZERO, j: R_ZERO, k: 0)); // nop
1418 else
1419 write32le(P: loc, V: insn(op: LU12I_W, d: getD5(v: currInsn), j: extractBits(v: val, begin: 31, end: 12),
1420 k: 0)); // lu12i.w $a0, %le_hi20
1421 break;
1422 case R_LARCH_TLS_IE_PC_LO12:
1423 if (isUInt12)
1424 write32le(P: loc, V: insn(op: ORI, d: getD5(v: currInsn), j: R_ZERO,
1425 k: val)); // ori $a0, $zero, %le_lo12
1426 else
1427 write32le(P: loc, V: insn(op: ORI, d: getD5(v: currInsn), j: getJ5(v: currInsn),
1428 k: lo12(val))); // ori $a0, $a0, %le_lo12
1429 break;
1430 }
1431}
1432
1433// Convert TLSDESC GD/LD to IE.
1434// In normal or medium code model, there are two forms of code sequences:
1435// * pcalau12i $a0, %desc_pc_hi20(sym_desc)
1436// * addi.d $a0, $a0, %desc_pc_lo12(sym_desc)
1437// * ld.d $ra, $a0, %desc_ld(sym_desc)
1438// * jirl $ra, $ra, %desc_call(sym_desc)
1439// ------
1440// * pcaddi $a0, %desc_pcrel_20(a)
1441// * load $ra, $a0, %desc_ld(a)
1442// * jirl $ra, $ra, %desc_call(a)
1443//
1444// The code sequence obtained is as follows:
1445// * pcalau12i $a0, %ie_pc_hi20(sym_ie)
1446// * ld.[wd] $a0, $a0, %ie_pc_lo12(sym_ie)
1447//
1448// Simplicity, whether tlsdescToIe or tlsdescToLe, we always tend to convert the
1449// preceding instructions to NOPs, due to both forms of code sequence
1450// (corresponding to relocation combinations:
1451// R_LARCH_TLS_DESC_PC_HI20+R_LARCH_TLS_DESC_PC_LO12 and
1452// R_LARCH_TLS_DESC_PCREL20_S2) have same process.
1453//
1454// When relaxation enables, redundant NOPs can be removed.
1455void LoongArch::tlsdescToIe(uint8_t *loc, const Relocation &rel,
1456 uint64_t val) const {
1457 switch (rel.type) {
1458 case R_LARCH_TLS_DESC_PC_HI20:
1459 case R_LARCH_TLS_DESC_PC_LO12:
1460 case R_LARCH_TLS_DESC_PCREL20_S2:
1461 write32le(P: loc, V: insn(op: ANDI, d: R_ZERO, j: R_ZERO, k: 0)); // nop
1462 break;
1463 case R_LARCH_TLS_DESC_LD:
1464 write32le(P: loc, V: insn(op: PCALAU12I, d: R_A0, j: 0, k: 0)); // pcalau12i $a0, %ie_pc_hi20
1465 relocateNoSym(loc, type: R_LARCH_TLS_IE_PC_HI20, val);
1466 break;
1467 case R_LARCH_TLS_DESC_CALL:
1468 write32le(P: loc, V: insn(op: ctx.arg.is64 ? LD_D : LD_W, d: R_A0, j: R_A0,
1469 k: 0)); // ld.[wd] $a0, $a0, %ie_pc_lo12
1470 relocateNoSym(loc, type: R_LARCH_TLS_IE_PC_LO12, val);
1471 break;
1472 default:
1473 llvm_unreachable("unsupported relocation for TLSDESC to IE");
1474 }
1475}
1476
1477// Convert TLSDESC GD/LD to LE.
1478// The code sequence obtained in the normal or medium code model is as follows:
1479// * lu12i.w $a0, %le_hi20(sym) # le_hi20 != 0, otherwise NOP
1480// * ori $a0, src, %le_lo12(sym) # le_hi20 != 0, src = $a0,
1481// # otherwise, src = $zero
1482// See the comment in tlsdescToIe for detailed information.
1483void LoongArch::tlsdescToLe(uint8_t *loc, const Relocation &rel,
1484 uint64_t val) const {
1485 assert(isInt<32>(val) &&
1486 "val exceeds the range of medium code model in tlsdescToLe");
1487
1488 bool isUInt12 = isUInt<12>(x: val);
1489 switch (rel.type) {
1490 case R_LARCH_TLS_DESC_PC_HI20:
1491 case R_LARCH_TLS_DESC_PC_LO12:
1492 case R_LARCH_TLS_DESC_PCREL20_S2:
1493 write32le(P: loc, V: insn(op: ANDI, d: R_ZERO, j: R_ZERO, k: 0)); // nop
1494 break;
1495 case R_LARCH_TLS_DESC_LD:
1496 if (isUInt12)
1497 write32le(P: loc, V: insn(op: ANDI, d: R_ZERO, j: R_ZERO, k: 0)); // nop
1498 else
1499 write32le(P: loc, V: insn(op: LU12I_W, d: R_A0, j: extractBits(v: val, begin: 31, end: 12),
1500 k: 0)); // lu12i.w $a0, %le_hi20
1501 break;
1502 case R_LARCH_TLS_DESC_CALL:
1503 if (isUInt12)
1504 write32le(P: loc, V: insn(op: ORI, d: R_A0, j: R_ZERO, k: val)); // ori $a0, $zero, %le_lo12
1505 else
1506 write32le(P: loc,
1507 V: insn(op: ORI, d: R_A0, j: R_A0, k: lo12(val))); // ori $a0, $a0, %le_lo12
1508 break;
1509 default:
1510 llvm_unreachable("unsupported relocation for TLSDESC to LE");
1511 }
1512}
1513
1514// Try GOT indirection to PC relative optimization.
1515// From:
1516// * pcalau12i $a0, %got_pc_hi20(sym_got)
1517// * ld.w/d $a0, $a0, %got_pc_lo12(sym_got)
1518// To:
1519// * pcalau12i $a0, %pc_hi20(sym)
1520// * addi.w/d $a0, $a0, %pc_lo12(sym)
1521//
1522// Note: Althouth the optimization has been performed, the GOT entries still
1523// exists, similarly to AArch64. Eliminating the entries will increase code
1524// complexity.
1525bool LoongArch::tryGotToPCRel(uint8_t *loc, const Relocation &rHi20,
1526 const Relocation &rLo12, uint64_t secAddr) const {
1527 // Check if the relocations apply to consecutive instructions.
1528 if (rHi20.offset + 4 != rLo12.offset)
1529 return false;
1530
1531 // Check if the relocations reference the same symbol and skip undefined,
1532 // preemptible and STT_GNU_IFUNC symbols.
1533 if (!rHi20.sym || rHi20.sym != rLo12.sym || !rHi20.sym->isDefined() ||
1534 rHi20.sym->isPreemptible || rHi20.sym->isGnuIFunc())
1535 return false;
1536
1537 // GOT references to absolute symbols can't be relaxed to use PCALAU12I/ADDI
1538 // in position-independent code because these instructions produce a relative
1539 // address.
1540 if ((ctx.arg.isPic && !cast<Defined>(Val&: *rHi20.sym).section))
1541 return false;
1542
1543 // Check if the addends of the both relocations are zero.
1544 if (rHi20.addend != 0 || rLo12.addend != 0)
1545 return false;
1546
1547 const uint32_t currInsn = read32le(P: loc);
1548 const uint32_t nextInsn = read32le(P: loc + 4);
1549 const uint32_t ldOpcode = ctx.arg.is64 ? LD_D : LD_W;
1550 // Check if the first instruction is PCALAU12I and the second instruction is
1551 // LD.
1552 if ((currInsn & 0xfe000000) != PCALAU12I ||
1553 (nextInsn & 0xffc00000) != ldOpcode)
1554 return false;
1555
1556 // Check if use the same register.
1557 if (getD5(v: currInsn) != getJ5(v: nextInsn) || getJ5(v: nextInsn) != getD5(v: nextInsn))
1558 return false;
1559
1560 Symbol &sym = *rHi20.sym;
1561 uint64_t symLocal = sym.getVA(ctx);
1562 const int64_t displace = symLocal - getLoongArchPage(p: secAddr + rHi20.offset);
1563 // Check if the symbol address is in
1564 // [(PC & ~0xfff) - 2GiB - 0x800, (PC & ~0xfff) + 2GiB - 0x800).
1565 const int64_t underflow = -0x80000000LL - 0x800;
1566 const int64_t overflow = 0x80000000LL - 0x800;
1567 if (!(displace >= underflow && displace < overflow))
1568 return false;
1569
1570 Relocation newRHi20 = {.expr: RE_LOONGARCH_PAGE_PC, .type: R_LARCH_PCALA_HI20, .offset: rHi20.offset,
1571 .addend: rHi20.addend, .sym: &sym};
1572 Relocation newRLo12 = {.expr: R_ABS, .type: R_LARCH_PCALA_LO12, .offset: rLo12.offset, .addend: rLo12.addend,
1573 .sym: &sym};
1574 uint64_t pageDelta =
1575 getLoongArchPageDelta(dest: symLocal, pc: secAddr + rHi20.offset, type: rHi20.type);
1576 // pcalau12i $a0, %pc_hi20
1577 write32le(P: loc, V: insn(op: PCALAU12I, d: getD5(v: currInsn), j: 0, k: 0));
1578 relocate(loc, rel: newRHi20, val: pageDelta);
1579 // addi.w/d $a0, $a0, %pc_lo12
1580 write32le(P: loc + 4, V: insn(op: ctx.arg.is64 ? ADDI_D : ADDI_W, d: getD5(v: nextInsn),
1581 j: getJ5(v: nextInsn), k: 0));
1582 relocate(loc: loc + 4, rel: newRLo12, val: SignExtend64(X: symLocal, B: 64));
1583 return true;
1584}
1585
1586// During TLSDESC to IE, the converted code sequence always includes an
1587// instruction related to the Lo12 relocation (ld.[wd]). To obtain correct val
1588// in `getRelocTargetVA`, expr of this instruction should be adjusted to R_GOT,
1589// while expr of other instructions related to the Hi20 relocation (pcalau12i)
1590// should be adjusted to RE_LOONGARCH_GOT_PAGE_PC. Specifically, in the normal
1591// or medium code model, the instruction with relocation R_LARCH_TLS_DESC_CALL
1592// is the candidate of Lo12 relocation.
1593
1594static bool pairForGotRels(ArrayRef<Relocation> relocs) {
1595 // Check if R_LARCH_GOT_PC_HI20 and R_LARCH_GOT_PC_LO12 always appear in
1596 // pairs.
1597 size_t i = 0;
1598 const size_t size = relocs.size();
1599 for (; i != size; ++i) {
1600 if (relocs[i].type == R_LARCH_GOT_PC_HI20) {
1601 if (i + 1 < size && relocs[i + 1].type == R_LARCH_GOT_PC_LO12) {
1602 ++i;
1603 continue;
1604 }
1605 if (relaxable(relocs, i) && i + 2 < size &&
1606 relocs[i + 2].type == R_LARCH_GOT_PC_LO12) {
1607 i += 2;
1608 continue;
1609 }
1610 break;
1611 } else if (relocs[i].type == R_LARCH_GOT_PC_LO12) {
1612 break;
1613 }
1614 }
1615 return i == size;
1616}
1617
1618void LoongArch::relocateAlloc(InputSection &sec, uint8_t *buf) const {
1619 const unsigned bits = ctx.arg.is64 ? 64 : 32;
1620 uint64_t secAddr = sec.getOutputSection()->addr + sec.outSecOff;
1621 bool isExtreme = false;
1622 const MutableArrayRef<Relocation> relocs = sec.relocs();
1623 const bool isPairForGotRels = pairForGotRels(relocs);
1624 for (size_t i = 0, size = relocs.size(); i != size; ++i) {
1625 Relocation &rel = relocs[i];
1626 if (rel.expr == R_RELAX_HINT)
1627 continue;
1628 uint8_t *loc = buf + rel.offset;
1629 uint64_t val = SignExtend64(
1630 X: sec.getRelocTargetVA(ctx, r: rel, p: secAddr + rel.offset), B: bits);
1631 switch (rel.type) {
1632 case R_LARCH_TLS_IE_PC_HI20:
1633 case R_LARCH_TLS_IE_PC_LO12:
1634 // IE to LE. Not supported in extreme code model.
1635 if (rel.expr != R_TPREL)
1636 break;
1637 if (rel.type == R_LARCH_TLS_IE_PC_HI20)
1638 isExtreme =
1639 i + 2 < size && relocs[i + 2].type == R_LARCH_TLS_IE64_PC_LO20;
1640 if (isExtreme) {
1641 rel.expr = getRelExpr(type: rel.type, s: *rel.sym, loc);
1642 val = SignExtend64(X: sec.getRelocTargetVA(ctx, r: rel, p: secAddr + rel.offset),
1643 B: bits);
1644 break;
1645 }
1646 if (relaxable(relocs, i) && rel.type == R_LARCH_TLS_IE_PC_HI20 &&
1647 isUInt<12>(x: val))
1648 continue;
1649 tlsIeToLe(loc, rel, val);
1650 continue;
1651
1652 case R_LARCH_TLS_DESC_PC_HI20:
1653 case R_LARCH_TLS_DESC_PC_LO12:
1654 case R_LARCH_TLS_DESC_LD:
1655 case R_LARCH_TLS_DESC_PCREL20_S2:
1656 // TLSDESC to LE/IE. Not supported in extreme code model.
1657 if (rel.expr != R_TPREL && rel.expr != RE_LOONGARCH_GOT_PAGE_PC)
1658 break;
1659 if (rel.type == R_LARCH_TLS_DESC_PC_HI20)
1660 isExtreme =
1661 i + 2 < size && relocs[i + 2].type == R_LARCH_TLS_DESC64_PC_LO20;
1662 if (isExtreme) {
1663 rel.expr = getRelExpr(type: rel.type, s: *rel.sym, loc);
1664 val = SignExtend64(X: sec.getRelocTargetVA(ctx, r: rel, p: secAddr + rel.offset),
1665 B: bits);
1666 break;
1667 }
1668 if (relaxable(relocs, i) && (rel.type == R_LARCH_TLS_DESC_PC_HI20 ||
1669 rel.type == R_LARCH_TLS_DESC_PC_LO12))
1670 continue;
1671 if (rel.expr == R_TPREL) {
1672 if (relaxable(relocs, i) && rel.type == R_LARCH_TLS_DESC_LD &&
1673 isUInt<12>(x: val))
1674 continue;
1675 tlsdescToLe(loc, rel, val);
1676 } else {
1677 tlsdescToIe(loc, rel, val);
1678 }
1679 continue;
1680
1681 case R_LARCH_TLS_DESC_CALL:
1682 if (isExtreme)
1683 continue;
1684 if (rel.expr == R_TPREL)
1685 tlsdescToLe(loc, rel, val);
1686 else
1687 tlsdescToIe(loc, rel, val);
1688 continue;
1689
1690 case R_LARCH_GOT_PC_HI20:
1691 // GOT indirection to PC relative optimization in normal or medium code
1692 // model, whether or not with R_LARCH_RELAX. If the code sequence can be
1693 // relaxed to a single pcaddi, the first instruction will be removed and
1694 // it will not reach here.
1695 if (isPairForGotRels) {
1696 bool isRelax = relaxable(relocs, i);
1697 const Relocation lo12Rel = isRelax ? relocs[i + 2] : relocs[i + 1];
1698 if (lo12Rel.type == R_LARCH_GOT_PC_LO12 &&
1699 tryGotToPCRel(loc, rHi20: rel, rLo12: lo12Rel, secAddr)) {
1700 i += isRelax ? 2 : 1;
1701 continue;
1702 }
1703 }
1704 break;
1705
1706 default:
1707 break;
1708 }
1709 relocate(loc, rel, val);
1710 }
1711}
1712
1713// When relaxing just R_LARCH_ALIGN, relocDeltas is usually changed only once in
1714// the absence of a linker script. For call and load/store R_LARCH_RELAX, code
1715// shrinkage may reduce displacement and make more relocations eligible for
1716// relaxation. Code shrinkage may increase displacement to a call/load/store
1717// target at a higher fixed address, invalidating an earlier relaxation. Any
1718// change in section sizes can have cascading effect and require another
1719// relaxation pass.
1720bool LoongArch::relaxOnce(int pass) const {
1721 if (pass == 0)
1722 initSymbolAnchors(ctx);
1723
1724 SmallVector<InputSection *, 0> storage;
1725 bool changed = false;
1726 for (OutputSection *osec : ctx.outputSections) {
1727 if (!(osec->flags & SHF_EXECINSTR))
1728 continue;
1729 for (InputSection *sec : getInputSections(os: *osec, storage))
1730 if (sec->relaxAux)
1731 changed |= relax(ctx, pass, sec&: *sec);
1732 }
1733 return changed;
1734}
1735
1736void LoongArch::finalizeRelax(int passes) const {
1737 Log(ctx) << "relaxation passes: " << passes;
1738 SmallVector<InputSection *, 0> storage;
1739 for (OutputSection *osec : ctx.outputSections) {
1740 if (!(osec->flags & SHF_EXECINSTR))
1741 continue;
1742 for (InputSection *sec : getInputSections(os: *osec, storage)) {
1743 if (!sec->relaxAux)
1744 continue;
1745 RelaxAux &aux = *sec->relaxAux;
1746 if (!aux.relocDeltas)
1747 continue;
1748
1749 MutableArrayRef<Relocation> rels = sec->relocs();
1750 ArrayRef<uint8_t> old = sec->content();
1751 size_t newSize = old.size() - aux.relocDeltas[rels.size() - 1];
1752 size_t writesIdx = 0;
1753 uint8_t *p = ctx.bAlloc.Allocate<uint8_t>(Num: newSize);
1754 uint64_t offset = 0;
1755 int64_t delta = 0;
1756 sec->content_ = p;
1757 sec->size = newSize;
1758 sec->bytesDropped = 0;
1759
1760 // Update section content: remove NOPs for R_LARCH_ALIGN and rewrite
1761 // instructions for relaxed relocations.
1762 for (size_t i = 0, e = rels.size(); i != e; ++i) {
1763 uint32_t remove = aux.relocDeltas[i] - delta;
1764 delta = aux.relocDeltas[i];
1765 if (remove == 0 && aux.relocTypes[i] == R_LARCH_NONE)
1766 continue;
1767
1768 // Copy from last location to the current relocated location.
1769 Relocation &r = rels[i];
1770 uint64_t size = r.offset - offset;
1771 memcpy(dest: p, src: old.data() + offset, n: size);
1772 p += size;
1773
1774 int64_t skip = 0;
1775 if (RelType newType = aux.relocTypes[i]) {
1776 switch (newType) {
1777 case R_LARCH_RELAX:
1778 break;
1779 case R_LARCH_PCREL20_S2:
1780 skip = 4;
1781 write32le(P: p, V: aux.writes[writesIdx++]);
1782 // RelExpr is needed for relocating.
1783 r.expr = r.sym->hasFlag(bit: NEEDS_PLT) ? R_PLT_PC : R_PC;
1784 break;
1785 case R_LARCH_B26:
1786 case R_LARCH_TLS_LE_LO12_R:
1787 skip = 4;
1788 write32le(P: p, V: aux.writes[writesIdx++]);
1789 break;
1790 case R_LARCH_TLS_GD_PCREL20_S2:
1791 // Note: R_LARCH_TLS_LD_PCREL20_S2 must also use R_TLSGD_PC instead
1792 // of R_TLSLD_PC due to historical reasons. In fact, right now TLSLD
1793 // behaves exactly like TLSGD on LoongArch.
1794 //
1795 // This reason has also been mentioned in mold commit:
1796 // https://github.com/rui314/mold/commit/5dfa1cf07c03bd57cb3d493b652ef22441bcd71c
1797 case R_LARCH_TLS_LD_PCREL20_S2:
1798 skip = 4;
1799 write32le(P: p, V: aux.writes[writesIdx++]);
1800 r.expr = R_TLSGD_PC;
1801 break;
1802 case R_LARCH_TLS_DESC_PCREL20_S2:
1803 skip = 4;
1804 write32le(P: p, V: aux.writes[writesIdx++]);
1805 r.expr = R_TLSDESC_PC;
1806 break;
1807 default:
1808 llvm_unreachable("unsupported type");
1809 }
1810 }
1811
1812 p += skip;
1813 offset = r.offset + skip + remove;
1814 }
1815 memcpy(dest: p, src: old.data() + offset, n: old.size() - offset);
1816
1817 // Subtract the previous relocDeltas value from the relocation offset.
1818 // For a pair of R_LARCH_XXX/R_LARCH_RELAX with the same offset, decrease
1819 // their r_offset by the same delta.
1820 delta = 0;
1821 for (size_t i = 0, e = rels.size(); i != e;) {
1822 uint64_t cur = rels[i].offset;
1823 do {
1824 rels[i].offset -= delta;
1825 if (aux.relocTypes[i] != R_LARCH_NONE)
1826 rels[i].type = aux.relocTypes[i];
1827 } while (++i != e && rels[i].offset == cur);
1828 delta = aux.relocDeltas[i - 1];
1829 }
1830 }
1831 }
1832}
1833
1834void elf::setLoongArchTargetInfo(Ctx &ctx) {
1835 ctx.target.reset(p: new LoongArch(ctx));
1836}
1837