[PATCH] RISC-V: Keep -mno-relax branch relocations to labels in other sections
Kito Cheng
kito.cheng@sifive.com
Fri Nov 20 09:30:11 GMT 2026
Since af514e5f6d1, with -mno-relax gas resolves branch and jump fixups
to local symbols, even when the symbol is in another section. The
offset is then wrong and no relocation is emitted. Only resolve the
fixup when the symbol is in the same section, as PCREL_HI20 already
does.
---
gas/config/tc-riscv.c | 16 +++++++++----
.../gas/riscv/no-relax-branch-section.d | 23 +++++++++++++++++++
.../gas/riscv/no-relax-branch-section.s | 20 ++++++++++++++++
3 files changed, 55 insertions(+), 4 deletions(-)
create mode 100644 gas/testsuite/gas/riscv/no-relax-branch-section.d
create mode 100644 gas/testsuite/gas/riscv/no-relax-branch-section.s
diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c
index 667d015de45..adb32e695a7 100644
--- a/gas/config/tc-riscv.c
+++ b/gas/config/tc-riscv.c
@@ -4992,7 +4992,9 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg)
bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
bfd_vma delta = target - md_pcrel_from (fixP);
bfd_putl32 (bfd_getl32 (buf) | ENCODE_JTYPE_IMM (delta), buf);
- if (!riscv_opts.relax && S_IS_LOCAL (fixP->fx_addsy))
+ if (!riscv_opts.relax
+ && S_IS_LOCAL (fixP->fx_addsy)
+ && S_GET_SEGMENT (fixP->fx_addsy) == seg)
{
if (!VALID_JTYPE_IMM (delta))
as_bad_where (fixP->fx_file, fixP->fx_line,
@@ -5010,7 +5012,9 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg)
bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
bfd_vma delta = target - md_pcrel_from (fixP);
bfd_putl32 (bfd_getl32 (buf) | ENCODE_BTYPE_IMM (delta), buf);
- if (!riscv_opts.relax && S_IS_LOCAL (fixP->fx_addsy))
+ if (!riscv_opts.relax
+ && S_IS_LOCAL (fixP->fx_addsy)
+ && S_GET_SEGMENT (fixP->fx_addsy) == seg)
{
if (!VALID_BTYPE_IMM (delta))
as_bad_where (fixP->fx_file, fixP->fx_line,
@@ -5028,7 +5032,9 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg)
bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
bfd_vma delta = target - md_pcrel_from (fixP);
bfd_putl16 (bfd_getl16 (buf) | ENCODE_CBTYPE_IMM (delta), buf);
- if (!riscv_opts.relax && S_IS_LOCAL (fixP->fx_addsy))
+ if (!riscv_opts.relax
+ && S_IS_LOCAL (fixP->fx_addsy)
+ && S_GET_SEGMENT (fixP->fx_addsy) == seg)
{
if (!VALID_CBTYPE_IMM (delta))
as_bad_where (fixP->fx_file, fixP->fx_line,
@@ -5046,7 +5052,9 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg)
bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
bfd_vma delta = target - md_pcrel_from (fixP);
bfd_putl16 (bfd_getl16 (buf) | ENCODE_CJTYPE_IMM (delta), buf);
- if (!riscv_opts.relax && S_IS_LOCAL (fixP->fx_addsy))
+ if (!riscv_opts.relax
+ && S_IS_LOCAL (fixP->fx_addsy)
+ && S_GET_SEGMENT (fixP->fx_addsy) == seg)
{
if (!VALID_CJTYPE_IMM (delta))
as_bad_where (fixP->fx_file, fixP->fx_line,
diff --git a/gas/testsuite/gas/riscv/no-relax-branch-section.d b/gas/testsuite/gas/riscv/no-relax-branch-section.d
new file mode 100644
index 00000000000..5515e831f05
--- /dev/null
+++ b/gas/testsuite/gas/riscv/no-relax-branch-section.d
@@ -0,0 +1,23 @@
+#as: -march=rv64ic -mno-relax
+#objdump: -dr -Mno-aliases
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+0 <.text>:
+[ ]+0:[ ]+[0-9a-f]+[ ]+beq[ ]+.*
+[ ]+0:[ ]+R_RISCV_BRANCH[ ]+.Lother
+[ ]+4:[ ]+[0-9a-f]+[ ]+jal[ ]+.*
+[ ]+4:[ ]+R_RISCV_JAL[ ]+.Lother
+[ ]+8:[ ]+[0-9a-f]+[ ]+c.beqz[ ]+.*
+[ ]+8:[ ]+R_RISCV_RVC_BRANCH[ ]+.Lother
+[ ]+a:[ ]+[0-9a-f]+[ ]+c.j[ ]+.*
+[ ]+a:[ ]+R_RISCV_RVC_JUMP[ ]+.Lother
+[ ]+c:[ ]+00b50663[ ]+beq[ ]+a0,a1,18 <.text\+0x18>
+[ ]+10:[ ]+008000ef[ ]+jal[ ]+ra,18 <.text\+0x18>
+[ ]+14:[ ]+c111[ ]+c.beqz[ ]+a0,18 <.text\+0x18>
+[ ]+16:[ ]+a009[ ]+c.j[ ]+18 <.text\+0x18>
+[ ]+18:[ ]+00008067[ ]+jalr[ ]+zero,0\(ra\)
+#pass
diff --git a/gas/testsuite/gas/riscv/no-relax-branch-section.s b/gas/testsuite/gas/riscv/no-relax-branch-section.s
new file mode 100644
index 00000000000..71d2b005a29
--- /dev/null
+++ b/gas/testsuite/gas/riscv/no-relax-branch-section.s
@@ -0,0 +1,20 @@
+# With -mno-relax, a branch to a local label in another section still
+# needs a relocation. One in the same section is resolved.
+
+ .option exact
+ .text
+ beq a0, a1, .Lother
+ jal .Lother
+ c.beqz a0, .Lother
+ c.j .Lother
+ beq a0, a1, .Lsame
+ jal .Lsame
+ c.beqz a0, .Lsame
+ c.j .Lsame
+.Lsame:
+ ret
+
+ .section .text.other, "ax"
+ nop
+.Lother:
+ ret
--
2.54.0
More information about the Binutils
mailing list