aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSun Sunny <sunny.sun@corelabtech.com>2024-07-04 03:03:37 +0000
committerNelson Chu <nelson@rivosinc.com>2024-07-04 21:36:48 +0800
commitc83ea305e21e1363b62747984c1b7286cb7a2ca8 (patch)
treed7cd6d25dcbe5f61ae8644bc22140a995d682101
parentf9d218de5c7b2eeeca9b32c9222a63c7376a8ddd (diff)
downloadbinutils-c83ea305e21e1363b62747984c1b7286cb7a2ca8.zip
binutils-c83ea305e21e1363b62747984c1b7286cb7a2ca8.tar.gz
binutils-c83ea305e21e1363b62747984c1b7286cb7a2ca8.tar.bz2
RISC-V: Fix BFD_RELOC_RISCV_PCREL_LO12_S patch issue
In commit dff565fcca8137954d6ad571ef39f6aec5c0429c, the fixups for PCREL_LO12_I and PCREL_LO12_S were mixed, so the "IMM" field were applied to incorrect position, this caused incorrect src registers to be encoded. gas/ * config/tc-riscv.c (md_apply_fix): Fix PCREL_LO12_S issue. * testsuite/gas/riscv/ixup-local.s: Updated for PCREL_LO12_S cases. * testsuite/gas/riscv/fixup-local-relax.d: Likewise. * testsuite/gas/riscv/fixup-local-norelax.d: Likewise. Signed-off-by: Jianwei Sun <sunny.sun@corelabtech.com>
-rw-r--r--gas/config/tc-riscv.c5
-rw-r--r--gas/testsuite/gas/riscv/fixup-local-norelax.d45
-rw-r--r--gas/testsuite/gas/riscv/fixup-local-relax.d76
-rw-r--r--gas/testsuite/gas/riscv/fixup-local.s28
4 files changed, 114 insertions, 40 deletions
diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c
index 5b9bfbd..c09bd42 100644
--- a/gas/config/tc-riscv.c
+++ b/gas/config/tc-riscv.c
@@ -4717,7 +4717,10 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg)
{
bfd_vma target = entry->target;
bfd_vma value = target - entry->address;
- bfd_putl32 (bfd_getl32 (buf) | ENCODE_ITYPE_IMM (value), buf);
+ if (fixP->fx_r_type == BFD_RELOC_RISCV_PCREL_LO12_S)
+ bfd_putl32 (bfd_getl32 (buf) | ENCODE_STYPE_IMM (value), buf);
+ else
+ bfd_putl32 (bfd_getl32 (buf) | ENCODE_ITYPE_IMM (value), buf);
/* Relaxations should never be enabled by `.option relax'. */
if (!riscv_opts.relax)
fixP->fx_done = 1;
diff --git a/gas/testsuite/gas/riscv/fixup-local-norelax.d b/gas/testsuite/gas/riscv/fixup-local-norelax.d
index 3169c69..8b29860 100644
--- a/gas/testsuite/gas/riscv/fixup-local-norelax.d
+++ b/gas/testsuite/gas/riscv/fixup-local-norelax.d
@@ -1,22 +1,45 @@
#as: -march=rv64i -mno-relax
#source: fixup-local.s
-#objdump: -dr
+#objdump: -dr -Mno-aliases
.*:[ ]+file format .*
+
Disassembly of section .text:
0+0000 <foo>:
[ ]+0:[ ]+00000517[ ]+auipc[ ]+a0,0x0
-[ ]+4:[ ]+00850513[ ]+addi[ ]+a0,a0,8 # 8 <foo\+0x8>
+[ ]+4:[ ]+01850513[ ]+addi[ ]+a0,a0,24 # 18 <foo\+0x18>
[ ]+8:[ ]+00000517[ ]+auipc[ ]+a0,0x0
-[ ]+8:[ ]+R_RISCV_PCREL_HI20[ ]+bar.*
-[ ]+c:[ ]+00050513[ ]+mv[ ]+a0,a0
-[ ]+c:[ ]+R_RISCV_PCREL_LO12_I[ ]+.L0.*
+[ ]+c:[ ]+01051503[ ]+lh[ ]+a0,16\(a0\) # 18 <foo\+0x18>
[ ]+10:[ ]+00000517[ ]+auipc[ ]+a0,0x0
-[ ]+10:[ ]+R_RISCV_PCREL_HI20[ ]+foo.*
-[ ]+14:[ ]+00050513[ ]+mv[ ]+a0,a0
-[ ]+14:[ ]+R_RISCV_PCREL_LO12_I[ ]+.L0.*
-[ ]+18:[ ]+00000517[ ]+auipc[ ]a0,0x0
-[ ]+1c:[ ]+00852503[ ]+lw[ ]+a0,8\(a0\) # 20 <foo\+0x20>
-[ ]+20:[ ]+00008067[ ]+ret
+[ ]+14:[ ]+00a51423[ ]+sh[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 ]+.L0.*
+[ ]+48:[ ]+00000517[ ]+auipc[ ]a0,0x0
+[ ]+4c:[ ]+01052503[ ]+lw[ ]+a0,16\(a0\) # 58 <foo\+0x58>
+[ ]+50:[ ]+00000517[ ]+auipc[ ]a0,0x0
+[ ]+54:[ ]+00a52823[ ]+sw[ ]+a0,16\(a0\) # 60 <foo\+0x60>
+[ ]+58:[ ]+00008067[ ]+jalr[ ]+zero,0\(ra\)
diff --git a/gas/testsuite/gas/riscv/fixup-local-relax.d b/gas/testsuite/gas/riscv/fixup-local-relax.d
index 3738de1..13ba70c 100644
--- a/gas/testsuite/gas/riscv/fixup-local-relax.d
+++ b/gas/testsuite/gas/riscv/fixup-local-relax.d
@@ -1,6 +1,6 @@
#as: -march=rv64i -mrelax
#source: fixup-local.s
-#objdump: -dr
+#objdump: -dr -Mno-aliases
.*:[ ]+file format .*
@@ -11,31 +11,73 @@ Disassembly of section .text:
[ ]+0:[ ]+00000517[ ]+auipc[ ]+a0,0x0
[ ]+0:[ ]+R_RISCV_PCREL_HI20[ ]+.LL0.*
[ ]+0:[ ]+R_RISCV_RELAX.*
-[ ]+4:[ ]+00850513[ ]+addi[ ]+a0,a0,8 # 8 <.LL0>
+[ ]+4:[ ]+01850513[ ]+addi[ ]+a0,a0,24 # 18 <.LL0>
[ ]+4:[ ]+R_RISCV_PCREL_LO12_I[ ]+.L0.*
[ ]+4:[ ]+R_RISCV_RELAX.*
-
-0+0008 <.LL0>:
[ ]+8:[ ]+00000517[ ]+auipc[ ]+a0,0x0
-[ ]+8:[ ]+R_RISCV_PCREL_HI20[ ]+bar.*
+[ ]+8:[ ]+R_RISCV_PCREL_HI20[ ]+.LL0.*
[ ]+8:[ ]+R_RISCV_RELAX.*
-[ ]+c:[ ]+00050513[ ]+mv[ ]+a0,a0
+[ ]+c:[ ]+01051503[ ]+lh[ ]+a0,16\(a0\) # 18 <.LL0>
[ ]+c:[ ]+R_RISCV_PCREL_LO12_I[ ]+.L0.*
[ ]+c:[ ]+R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 ]+a0,16\(a0\) # 58 <.LL3>
+[ ]+4c:[ ]+R_RISCV_PCREL_LO12_I[ ]+.LL1.*
+[ ]+4c:[ ]+R_RISCV_RELAX.*
+[ ]+50:[ ]+00000517[ ]+auipc[ ]a0,0x0
+[ ]+50:[ ]+R_RISCV_PCREL_HI20[ ]+.LL3.*
+[ ]+50:[ ]+R_RISCV_RELAX.*
+[ ]+54:[ ]+00a52823[ ]+sw[ ]+a0,16\(a0\) # 60 <.LL3\+0x8>
+[ ]+54:[ ]+R_RISCV_PCREL_LO12_S[ ]+.LL1.*
+[ ]+54:[ ]+R_RISCV_RELAX.*
-0+0020 <.LL2>:
-[ ]+20:[ ]+00008067[ ]+ret
+0+0058 <.LL3>:
+[ ]+58:[ ]+00008067[ ]+jalr[ ]+zero,0\(ra\)
diff --git a/gas/testsuite/gas/riscv/fixup-local.s b/gas/testsuite/gas/riscv/fixup-local.s
index 44b4731..04e7c7f 100644
--- a/gas/testsuite/gas/riscv/fixup-local.s
+++ b/gas/testsuite/gas/riscv/fixup-local.s
@@ -1,13 +1,19 @@
-.global foo
-.global bar
+ .global foo
+ .global bar
foo:
- la a0, .LL0
+ la a0, .LL0
+ lh a0, .LL0
+ sh a0, .LL0, a0
.LL0:
- la a0, bar
- la a0, foo
-.LL1:
- auipc a0, %pcrel_hi(.LL2)
- lw a0, %pcrel_lo(.LL1)(a0)
-
-.LL2:
- ret
+ la a0, bar
+ lh a0, bar
+ sh a0, bar, a0
+ la a0, foo
+ lh a0, foo
+ sh a0, foo, a0
+.LL1: auipc a0, %pcrel_hi(.LL3)
+ lw a0, %pcrel_lo(.LL1)(a0)
+.LL2: auipc a0, %pcrel_hi(.LL3)
+ sw a0, %pcrel_lo(.LL1)(a0)
+.LL3:
+ ret