[PATCH] [PATCH] riscv: Fix jal immediate encoding for constant offsets
Mingzhu Yan
yanmingzhu@iscas.ac.cn
Mon Jun 16 15:03:04 GMT 2025
This patch corrects an issue in the riscv gas where
JMP instructions with constant, non-symbolic PC-relative
offsets were incorrectly assembled with a zero immediate.
Previously, an instruction like j 4 would incorrectly assemble to
0 0000006f j 0 <.text>
But now, it is
0: 0040006f j 4 <.text+0x4>
---
gas/config/tc-riscv.c | 6 ++++++
gas/testsuite/gas/riscv/branch.d | 13 +++++++++++++
gas/testsuite/gas/riscv/branch.s | 5 +++++
3 files changed, 24 insertions(+)
create mode 100644 gas/testsuite/gas/riscv/branch.d
create mode 100644 gas/testsuite/gas/riscv/branch.s
diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c
index e219dd21a..9ab6a9cbf 100644
--- a/gas/config/tc-riscv.c
+++ b/gas/config/tc-riscv.c
@@ -4754,6 +4754,12 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg)
if (!riscv_opts.relax && S_IS_LOCAL (fixP->fx_addsy))
fixP->fx_done = 1;
}
+ else
+ {
+ /* Handles constant offset jumps like 'j 4'. */
+ bfd_putl32 (bfd_getl32 (buf) | ENCODE_JTYPE_IMM (*valP), buf);
+ fixP->fx_done = 1;
+ }
break;
case BFD_RELOC_12_PCREL:
diff --git a/gas/testsuite/gas/riscv/branch.d b/gas/testsuite/gas/riscv/branch.d
new file mode 100644
index 000000000..786cf9f7d
--- /dev/null
+++ b/gas/testsuite/gas/riscv/branch.d
@@ -0,0 +1,13 @@
+#objdump: -drw
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+ <target-0x4>:
+[ ]+0:[ ]+0040006f[ ]+j[ ]+4 <target>
+
+0+4 <target>:
+[ ]+4:[ ]+0040006f[ ]+j[ ]+8 <target\+0x4>
+[ ]+8:[ ]+00008067[ ]+ret
diff --git a/gas/testsuite/gas/riscv/branch.s b/gas/testsuite/gas/riscv/branch.s
new file mode 100644
index 000000000..6b6e287bc
--- /dev/null
+++ b/gas/testsuite/gas/riscv/branch.s
@@ -0,0 +1,5 @@
+ .text
+ j 4
+target:
+ j 4
+ ret
--
2.43.0
More information about the Binutils
mailing list