[PATCH] D156772: [lld][LoongArch] Support the R_LARCH_PCREL20_S2 relocation type

Lu Weining via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 31 21:06:33 PDT 2023


SixWeining created this revision.
SixWeining added reviewers: xen0n, MaskRay, xry111.
Herald added subscribers: arichardson, emaste.
Herald added a project: All.
SixWeining requested review of this revision.
Herald added subscribers: llvm-commits, wangpc.
Herald added a project: LLVM.

`R_LARCH_PCREL20_S2` is a new added relocation type in LoongArch ELF
psABI v2.10 [1] which is not corvered by D138135 <https://reviews.llvm.org/D138135> except `R_LARCH_64_PCREL`.

A motivation to support `R_LARCH_PCREL20_S2` in lld is to build the
runtime of .NET core (a.k.a `CoreCLR`) in which strict PC-relative
semantics need to be guaranteed [2]. The normal `pcalau12i + addi.d`
approach doesn't work because the code will be copied to other places
with different "page" and offsets. To achieve this, we can use `pcaddi`
with explicit `R_LARCH_PCREL20_S2` reloc to address +-2MB PC-relative
range with 4-bytes aligned.

[1]: https://github.com/loongson/la-abi-specs/releases/tag/v2.10
[2]: https://github.com/dotnet/runtime/blob/release/7.0/src/coreclr/vm/loongarch64/asmhelpers.S#L307


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156772

Files:
  lld/ELF/Arch/LoongArch.cpp
  lld/test/ELF/loongarch-pcrel20-s2.s


Index: lld/test/ELF/loongarch-pcrel20-s2.s
===================================================================
--- /dev/null
+++ lld/test/ELF/loongarch-pcrel20-s2.s
@@ -0,0 +1,44 @@
+# REQUIRES: loongarch
+
+# RUN: llvm-mc --filetype=obj --triple=loongarch32-unknown-elf %s -o %t.la32.o
+# RUN: llvm-mc --filetype=obj --triple=loongarch64-unknown-elf %s -o %t.la64.o
+
+# RUN: ld.lld %t.la32.o --section-start=.text=0x20000 --section-start=.data=0x20008 -o %t.la32.1
+# RUN: ld.lld %t.la64.o --section-start=.text=0x20000 --section-start=.data=0x20008 -o %t.la64.1
+# RUN: llvm-objdump --no-show-raw-insn -d %t.la32.1 | FileCheck --match-full-lines --check-prefix=CASE1 %s
+# RUN: llvm-objdump --no-show-raw-insn -d %t.la64.1 | FileCheck --match-full-lines --check-prefix=CASE1 %s
+# CASE1: 20000: pcaddi $t0, 2
+
+# RUN: ld.lld %t.la32.o --section-start=.text=0x20008 --section-start=.data=0x20000 -o %t.la32.2
+# RUN: ld.lld %t.la64.o --section-start=.text=0x20008 --section-start=.data=0x20000 -o %t.la64.2
+# RUN: llvm-objdump --no-show-raw-insn -d %t.la32.2 | FileCheck --match-full-lines --check-prefix=CASE2 %s
+# RUN: llvm-objdump --no-show-raw-insn -d %t.la64.2 | FileCheck --match-full-lines --check-prefix=CASE2 %s
+# CASE2: 20008: pcaddi $t0, -2
+
+# RUN: not ld.lld %t.la32.o --section-start=.text=0x20000 --section-start=.data=0x220000 -o /dev/null 2>&1 | \
+# RUN:   FileCheck -DFILE=%t.la32.o --check-prefix=ERROR-RANGE %s
+# RUN: not ld.lld %t.la64.o --section-start=.text=0x20000 --section-start=.data=0x220000 -o /dev/null 2>&1 | \
+# RUN:   FileCheck -DFILE=%t.la64.o --check-prefix=ERROR-RANGE %s
+# ERROR-RANGE: error: [[FILE]]:(.text+0x0): relocation R_LARCH_PCREL20_S2 out of range: 2097152 is not in [-2097152, 2097151]; references section '.data'
+
+# RUN: not ld.lld %t.la32.o --section-start=.text=0x20000 --section-start=.data=0x40001 -o /dev/null 2>&1 | \
+# RUN:   FileCheck -DFILE=%t.la32.o --check-prefix=ERROR-ALIGN-1 %s
+# RUN: not ld.lld %t.la64.o --section-start=.text=0x20000 --section-start=.data=0x40001 -o /dev/null 2>&1 | \
+# RUN:   FileCheck -DFILE=%t.la64.o --check-prefix=ERROR-ALIGN-1 %s
+# ERROR-ALIGN-1: error: [[FILE]]:(.text+0x0): improper alignment for relocation R_LARCH_PCREL20_S2: 0x20001 is not aligned to 4 bytes
+
+# RUN: not ld.lld %t.la32.o --section-start=.text=0x20000 --section-start=.data=0x40002 -o /dev/null 2>&1 | \
+# RUN:   FileCheck -DFILE=%t.la32.o --check-prefix=ERROR-ALIGN-2 %s
+# RUN: not ld.lld %t.la64.o --section-start=.text=0x20000 --section-start=.data=0x40002 -o /dev/null 2>&1 | \
+# RUN:   FileCheck -DFILE=%t.la64.o --check-prefix=ERROR-ALIGN-2 %s
+# ERROR-ALIGN-2: error: [[FILE]]:(.text+0x0): improper alignment for relocation R_LARCH_PCREL20_S2: 0x20002 is not aligned to 4 bytes
+
+.global _start
+
+_start:
+1:
+  pcaddi $t0, 0
+  .reloc 1b, R_LARCH_PCREL20_S2, .data
+
+.data
+  .word 0
Index: lld/ELF/Arch/LoongArch.cpp
===================================================================
--- lld/ELF/Arch/LoongArch.cpp
+++ lld/ELF/Arch/LoongArch.cpp
@@ -457,6 +457,7 @@
     return R_RISCV_ADD;
   case R_LARCH_32_PCREL:
   case R_LARCH_64_PCREL:
+  case R_LARCH_PCREL20_S2:
     return R_PC;
   case R_LARCH_B16:
   case R_LARCH_B21:
@@ -564,6 +565,12 @@
     write64le(loc, val);
     return;
 
+  case R_LARCH_PCREL20_S2:
+    checkInt(loc, val, 22, rel);
+    checkAlignment(loc, val, 4, rel);
+    write32le(loc, setJ20(read32le(loc), val >> 2));
+    return;
+
   case R_LARCH_B16:
     checkInt(loc, val, 18, rel);
     checkAlignment(loc, val, 4, rel);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156772.545911.patch
Type: text/x-patch
Size: 3552 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230801/f3efacdc/attachment.bin>


More information about the llvm-commits mailing list