summaryrefslogtreecommitdiff
path: root/lld/test
diff options
context:
space:
mode:
authorCraig Topper <craig.topper@sifive.com>2023-04-13 10:39:47 -0700
committerCraig Topper <craig.topper@sifive.com>2023-04-13 10:52:15 -0700
commit85444794cdde8211b916767cc51a37547dbd17e9 (patch)
treea64ff5dc1e7dfb59873ca9fdea9207c8ccf8f3ea /lld/test
parentb60513e384bf0253464d2ff9070f10b488e3701a (diff)
downloadllvm-85444794cdde8211b916767cc51a37547dbd17e9.tar.gz
[lld][RISCV] Implement GP relaxation for R_RISCV_HI20/R_RISCV_LO12_I/R_RISCV_LO12_S.
This implements support for relaxing these relocations to use the GP register to compute addresses of globals in the .sdata and .sbss sections. This feature is off by default and must be enabled by passing --relax-gp to the linker. The GP register might not always be the "global pointer". It can be used for other purposes. See discussion here https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/371 Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D143673
Diffstat (limited to 'lld/test')
-rw-r--r--lld/test/ELF/riscv-relax-hi20-lo12-pie.s33
-rw-r--r--lld/test/ELF/riscv-relax-hi20-lo12.s61
2 files changed, 94 insertions, 0 deletions
diff --git a/lld/test/ELF/riscv-relax-hi20-lo12-pie.s b/lld/test/ELF/riscv-relax-hi20-lo12-pie.s
new file mode 100644
index 000000000000..87e4f8f000e5
--- /dev/null
+++ b/lld/test/ELF/riscv-relax-hi20-lo12-pie.s
@@ -0,0 +1,33 @@
+# REQUIRES: riscv
+# RUN: rm -rf %t && split-file %s %t && cd %t
+
+# RUN: llvm-mc -filetype=obj -triple=riscv32-unknown-elf -mattr=+relax a.s -o rv32.o
+# RUN: llvm-mc -filetype=obj -triple=riscv64-unknown-elf -mattr=+relax a.s -o rv64.o
+# RUN: llvm-mc -filetype=obj -triple=riscv64-unknown-elf -mattr=+relax a.s -o rv64-pie.o
+
+# RUN: ld.lld --relax-gp --undefined=__global_pointer$ rv32.o lds -pie -o rv32
+# RUN: ld.lld --relax-gp --undefined=__global_pointer$ rv64.o lds -shared -o rv64
+# RUN: llvm-objdump -td -M no-aliases --no-show-raw-insn rv32 | FileCheck %s
+# RUN: llvm-objdump -td -M no-aliases --no-show-raw-insn rv64 | FileCheck %s
+
+# CHECK: lui a0, 512
+# CHECK-NEXT: addi a0, a0, 1
+# CHECK-NEXT: lw a0, 1(a0)
+# CHECK-NEXT: sw a0, 1(a0)
+
+#--- a.s
+.globl abs
+abs = 0x200001
+
+.global _start
+_start:
+ lui a0, %hi(abs)
+ addi a0, a0, %lo(abs)
+ lw a0, %lo(abs)(a0)
+ sw a0, %lo(abs)(a0)
+
+#--- lds
+SECTIONS {
+ .text : {*(.text) }
+ .sdata 0x200000 : {}
+}
diff --git a/lld/test/ELF/riscv-relax-hi20-lo12.s b/lld/test/ELF/riscv-relax-hi20-lo12.s
new file mode 100644
index 000000000000..feef08767c19
--- /dev/null
+++ b/lld/test/ELF/riscv-relax-hi20-lo12.s
@@ -0,0 +1,61 @@
+# REQUIRES: riscv
+# RUN: rm -rf %t && split-file %s %t && cd %t
+
+# RUN: llvm-mc -filetype=obj -triple=riscv32-unknown-elf -mattr=+relax a.s -o rv32.o
+# RUN: llvm-mc -filetype=obj -triple=riscv64-unknown-elf -mattr=+relax a.s -o rv64.o
+
+# RUN: ld.lld --relax-gp --undefined=__global_pointer$ rv32.o lds -o rv32
+# RUN: ld.lld --relax-gp --undefined=__global_pointer$ rv64.o lds -o rv64
+# RUN: llvm-objdump -td -M no-aliases --no-show-raw-insn rv32 | FileCheck %s
+# RUN: llvm-objdump -td -M no-aliases --no-show-raw-insn rv64 | FileCheck %s
+
+# CHECK: 00000028 l .text {{0*}}0 a
+
+# CHECK-NOT: lui
+# CHECK: addi a0, gp, -2048
+# CHECK-NEXT: lw a0, -2048(gp)
+# CHECK-NEXT: sw a0, -2048(gp)
+# CHECK-NOT: lui
+# CHECK-NEXT: addi a0, gp, 2047
+# CHECK-NEXT: lb a0, 2047(gp)
+# CHECK-NEXT: sb a0, 2047(gp)
+# CHECK-NEXT: lui a0, 513
+# CHECK-NEXT: addi a0, a0, 0
+# CHECK-NEXT: lw a0, 0(a0)
+# CHECK-NEXT: sw a0, 0(a0)
+# CHECK-EMPTY:
+# CHECK-NEXT: <a>:
+# CHECK-NEXT: addi a0, a0, 1
+
+#--- a.s
+.global _start
+_start:
+ lui a0, %hi(foo)
+ addi a0, a0, %lo(foo)
+ lw a0, %lo(foo)(a0)
+ sw a0, %lo(foo)(a0)
+ lui a0, %hi(bar)
+ addi a0, a0, %lo(bar)
+ lb a0, %lo(bar)(a0)
+ sb a0, %lo(bar)(a0)
+ lui a0, %hi(norelax)
+ addi a0, a0, %lo(norelax)
+ lw a0, %lo(norelax)(a0)
+ sw a0, %lo(norelax)(a0)
+a:
+ addi a0, a0, 1
+
+.section .sdata,"aw"
+foo:
+ .word 0
+ .space 4091
+bar:
+ .byte 0
+norelax:
+ .word 0
+
+#--- lds
+SECTIONS {
+ .text : {*(.text) }
+ .sdata 0x200000 : { }
+}