[PATCH 3/3] RISC-V: Treat R_RISCV_ALIGN as relaxable code for later alignment

Kito Cheng kito.cheng@sifive.com
Fri Nov 20 11:18:41 GMT 2026


An R_RISCV_ALIGN can also change the section size, so a later alignment
in norelax code needs R_RISCV_ALIGN too, even when no instruction before
it is relaxable.
---
 gas/config/tc-riscv.c                   |  2 ++
 gas/testsuite/gas/riscv/relax-align-4.d | 38 +++++++++++++++++++++++++
 gas/testsuite/gas/riscv/relax-align-4.s | 23 +++++++++++++++
 3 files changed, 63 insertions(+)
 create mode 100644 gas/testsuite/gas/riscv/relax-align-4.d
 create mode 100644 gas/testsuite/gas/riscv/relax-align-4.s

diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c
index 2d39b39d6e7..392d343edb1 100644
--- a/gas/config/tc-riscv.c
+++ b/gas/config/tc-riscv.c
@@ -5408,6 +5408,8 @@ riscv_frag_align_code (int n)
 
   fix_new_exp (frag_now, nops - frag_now->fr_literal, 0,
 	       &ex, false, BFD_RELOC_RISCV_ALIGN);
+  /* The linker may now change the size of this section.  */
+  info->last_relax = true;
 
   riscv_mapping_state (MAP_INSN, worst_case_bytes, true/* fr_align_code */);
 
diff --git a/gas/testsuite/gas/riscv/relax-align-4.d b/gas/testsuite/gas/riscv/relax-align-4.d
new file mode 100644
index 00000000000..5e66edd8c25
--- /dev/null
+++ b/gas/testsuite/gas/riscv/relax-align-4.d
@@ -0,0 +1,38 @@
+#as: -mrelax -march=rv64gc
+#objdump: -dr
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+000 <b>:
+[ 	]+0:[ 	]+0120006f[ 	]+j[ 	]+[0-9a-z]+[ 	]+<e>
+[ 	]+0:[ 	]+R_RISCV_JAL[ 	]+e
+[ 	]+4:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+[ 	]+4:[ 	]+R_RISCV_ALIGN[ 	]+\*ABS\*\+0x6
+[ 	]+6:[ 	]+00000013[ 	]+nop
+
+0+00a <c>:
+[ 	]\.\.\.$
+[ 	]+a:[ 	]+R_RISCV_64[ 	]+g
+
+0+0012 <e>:
+[ 	]+12:[ 	]+00013083[ 	]+ld[ 	]+ra,0\(sp\)
+[ 	]+16:[ 	]+00000097[ 	]+auipc[ 	]+ra,0x0
+[ 	]+16:[ 	]+R_RISCV_CALL_PLT[ 	]+h
+[ 	]+1a:[ 	]+000080e7[ 	]+jalr[ 	]+ra[ 	]+#.*<e\+0x[0-9a-z]+>
+[ 	]+1e:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+[ 	]+1e:[ 	]+R_RISCV_ALIGN[ 	]+\*ABS\*\+0x2
+
+0+0020 <g>:
+[ 	]+20:[ 	]+00000010[ 	]+\.word[ 	]+0x00000010
+[ 	]+24:[ 	]+00000013[ 	]+nop
+
+Disassembly of section .text.2:
+
+0+000 <h>:
+[ 	]+0:[ 	]+00000097[ 	]+auipc[ 	]+ra,0x0
+[ 	]+0:[ 	]+R_RISCV_CALL_PLT[ 	]+b
+[ 	]+0:[ 	]+R_RISCV_RELAX[ 	]+\*ABS\*
+[ 	]+4:[ 	]+000080e7[ 	]+jalr[ 	]+ra[ 	]+#.*<h>
diff --git a/gas/testsuite/gas/riscv/relax-align-4.s b/gas/testsuite/gas/riscv/relax-align-4.s
new file mode 100644
index 00000000000..a0c3b4e5b03
--- /dev/null
+++ b/gas/testsuite/gas/riscv/relax-align-4.s
@@ -0,0 +1,23 @@
+    .section .text.2
+    .option relax
+    .option rvc
+h:
+    call b
+    .text
+    .option norelax
+    .option norvc
+b:
+    j e
+    .option rvc
+    .option relax
+    .balign 8
+c:
+    .dword g + 0
+e:
+    .option norvc
+    .option norelax
+    ld x1, 0(x2)
+    call h
+    .balign 4
+g:
+    .word 0x10
-- 
2.54.0



More information about the Binutils mailing list