[lld] [lld][LoongArch] Support the R_LARCH_CALL36 relocation type (PR #73346)

WÁNG Xuěruì via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 21 04:52:23 PST 2023


================
@@ -590,6 +591,24 @@ void LoongArch::relocate(uint8_t *loc, const Relocation &rel,
     write32le(loc, setD10k16(read32le(loc), val >> 2));
     return;
 
+  case R_LARCH_CALL36: {
+    // This relocation is designed for the adjancent pcaddu18i+jirl pair that
+    // are patched in one time. Because of sign extension of these insns'
+    // immediate fields, the relocation range is [-128G - 0x20000, +128G -
+    // 0x20000) (of course must be 4-bytes aligned).
+    if (((int64_t)val + 0x20000) != llvm::SignExtend64(val + 0x20000, 38))
+      reportRangeError(loc, rel, Twine(val), llvm::minIntN(38) - 0x20000,
+                       llvm::maxIntN(38) - 0x20000);
+    checkAlignment(loc, val, 4, rel);
+    // Since jirl performs sign extension on the offset immediate, adds (1<<17)
+    // to original val to get the correct hi20.
+    uint32_t hi20 = extractBits(val + (1 << 17), 37, 18);
+    uint32_t lo18 = val & (1 << 18) - 1;
----------------
xen0n wrote:

This could just be `extractBits(val, 17, 2)` because `checkAlignment` already passed before. So we can just `write32le(loc + 4, setK16(read32le(loc + 4), lo18))` later without the shift...

https://github.com/llvm/llvm-project/pull/73346


More information about the llvm-commits mailing list