[PATCH 1/3] RISC-V: Emit R_RISCV_ALIGN in norelax code after relaxable code

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


When relaxation is off, gas pads code alignment with fixed nops.  But
if the section has relaxable code before, the linker may still move the
alignment point, for example:

	lui	a0, %hi(foo)	# relaxed to c.lui
	.option norelax
	.balign	8

Emit R_RISCV_ALIGN in this case too.
---
 gas/config/tc-riscv.c                    | 10 ++++++--
 gas/config/tc-riscv.h                    |  1 +
 gas/testsuite/gas/riscv/relax-align-3.s  | 25 ++++++++++++++++++++
 gas/testsuite/gas/riscv/relax-align-3a.d | 30 ++++++++++++++++++++++++
 gas/testsuite/gas/riscv/relax-align-3b.d | 30 ++++++++++++++++++++++++
 gas/testsuite/gas/riscv/relax-align-3c.d | 30 ++++++++++++++++++++++++
 gas/testsuite/gas/riscv/relax-align-3d.d | 30 ++++++++++++++++++++++++
 7 files changed, 154 insertions(+), 2 deletions(-)
 create mode 100644 gas/testsuite/gas/riscv/relax-align-3.s
 create mode 100644 gas/testsuite/gas/riscv/relax-align-3a.d
 create mode 100644 gas/testsuite/gas/riscv/relax-align-3b.d
 create mode 100644 gas/testsuite/gas/riscv/relax-align-3c.d
 create mode 100644 gas/testsuite/gas/riscv/relax-align-3d.d

diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c
index 667d015de45..b67a3e9b6be 100644
--- a/gas/config/tc-riscv.c
+++ b/gas/config/tc-riscv.c
@@ -1972,6 +1972,9 @@ append_insn (struct riscv_cl_insn *ip, expressionS *address_expr,
 				  address_expr, false, reloc_type);
 
 	  ip->fixp->fx_tcbit = riscv_opts.relax;
+	  /* The linker may now change the size of this section.  */
+	  if (riscv_opts.relax)
+	    seg_info (now_seg)->tc_segment_info_data.last_relax = true;
 	  ip->fixp->tc_fix_data.source_macro = source_macro;
 	}
     }
@@ -5374,8 +5377,11 @@ riscv_frag_align_code (int n)
       return false;
     }
 
-  /* When not relaxing, riscv_handle_align handles code alignment.  */
-  if (!riscv_opts.relax)
+  /* When not relaxing, riscv_handle_align handles code alignment.  But
+     if relaxable code came before in this section, the linker may still
+     move this point, so emit R_RISCV_ALIGN anyway.  */
+  if (!riscv_opts.relax
+      && !seg_info (now_seg)->tc_segment_info_data.last_relax)
     return false;
 
   /* If the last item emitted was not an ordinary insn, first align back to
diff --git a/gas/config/tc-riscv.h b/gas/config/tc-riscv.h
index 7518d2a70f7..26bbeb2bc64 100644
--- a/gas/config/tc-riscv.h
+++ b/gas/config/tc-riscv.h
@@ -158,6 +158,7 @@ struct riscv_segment_info_type
   enum riscv_seg_mstate map_state;
   bool rvc;
   bool last_insn16;
+  bool last_relax;
   /* The current mapping symbol with architecture string.  */
   symbolS *arch_map_symbol;
 };
diff --git a/gas/testsuite/gas/riscv/relax-align-3.s b/gas/testsuite/gas/riscv/relax-align-3.s
new file mode 100644
index 00000000000..282e97e8f02
--- /dev/null
+++ b/gas/testsuite/gas/riscv/relax-align-3.s
@@ -0,0 +1,25 @@
+        .text
+        .option relax
+        .balign 4
+        .global _start
+        .type _start, @function
+_start:
+        lui a0, %hi(foo)
+        addi a0, a0, %lo(foo)
+        ret
+        .option push
+
+.ifdef NORELAX
+        .option norelax
+.endif
+.ifdef NORVC
+        .option norvc
+.endif
+        .balign 8
+SHOULD_ALIGN_8_HERE:
+        .word 0x12345678
+
+        .option pop
+
+foo:
+        ret
diff --git a/gas/testsuite/gas/riscv/relax-align-3a.d b/gas/testsuite/gas/riscv/relax-align-3a.d
new file mode 100644
index 00000000000..495d6257eac
--- /dev/null
+++ b/gas/testsuite/gas/riscv/relax-align-3a.d
@@ -0,0 +1,30 @@
+#as: -mrelax -defsym NORELAX=1 -defsym NORVC=1 -march=rv64gc
+#source: relax-align-3.s
+#objdump: -dr
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+0000 <_start-0x2>:
+[ 	]+0:[ 	]+0001[ 	]+nop
+[ 	]+0:[ 	]+R_RISCV_ALIGN[ 	]+\*ABS\*\+0x2
+
+0+0002 <_start>:
+[ 	]+2:[ 	]+00000537[ 	]+lui[ 	]+a0,0x0
+[ 	]+2:[ 	]+R_RISCV_HI20[ 	]+foo
+[ 	]+2:[ 	]+R_RISCV_RELAX[ 	]+\*ABS\*
+[ 	]+6:[ 	]+00050513[ 	]+mv[ 	]+a0,a0
+[ 	]+6:[ 	]+R_RISCV_LO12_I[ 	]+foo
+[ 	]+6:[ 	]+R_RISCV_RELAX[ 	]+\*ABS\*
+[ 	]+a:[ 	]+8082[ 	]+ret
+[ 	]+c:[ 	]+00000013[ 	]+nop
+[ 	]+c:[ 	]+R_RISCV_ALIGN[ 	]+\*ABS\*\+0x4
+
+0+0010 <SHOULD_ALIGN_8_HERE>:
+[ 	]+10:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+0014 <foo>:
+[ 	]+14:[ 	]+8082[ 	]+ret
+[ 	]+16:[ 	]+0001[ 	]+nop
diff --git a/gas/testsuite/gas/riscv/relax-align-3b.d b/gas/testsuite/gas/riscv/relax-align-3b.d
new file mode 100644
index 00000000000..2e07d674a5c
--- /dev/null
+++ b/gas/testsuite/gas/riscv/relax-align-3b.d
@@ -0,0 +1,30 @@
+#as: -mrelax -defsym NORVC=1 -march=rv64gc
+#source: relax-align-3.s
+#objdump: -dr
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+0000 <_start-0x2>:
+[ 	]+0:[ 	]+0001[ 	]+nop
+[ 	]+0:[ 	]+R_RISCV_ALIGN[ 	]+\*ABS\*\+0x2
+
+0+0002 <_start>:
+[ 	]+2:[ 	]+00000537[ 	]+lui[ 	]+a0,0x0
+[ 	]+2:[ 	]+R_RISCV_HI20[ 	]+foo
+[ 	]+2:[ 	]+R_RISCV_RELAX[ 	]+\*ABS\*
+[ 	]+6:[ 	]+00050513[ 	]+mv[ 	]+a0,a0
+[ 	]+6:[ 	]+R_RISCV_LO12_I[ 	]+foo
+[ 	]+6:[ 	]+R_RISCV_RELAX[ 	]+\*ABS\*
+[ 	]+a:[ 	]+8082[ 	]+ret
+[ 	]+c:[ 	]+00000013[ 	]+nop
+[ 	]+c:[ 	]+R_RISCV_ALIGN[ 	]+\*ABS\*\+0x4
+
+0+0010 <SHOULD_ALIGN_8_HERE>:
+[ 	]+10:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+0014 <foo>:
+[ 	]+14:[ 	]+8082[ 	]+ret
+[ 	]+16:[ 	]+0001[ 	]+nop
diff --git a/gas/testsuite/gas/riscv/relax-align-3c.d b/gas/testsuite/gas/riscv/relax-align-3c.d
new file mode 100644
index 00000000000..db0fbcd9292
--- /dev/null
+++ b/gas/testsuite/gas/riscv/relax-align-3c.d
@@ -0,0 +1,30 @@
+#as: -mrelax -defsym NORELAX=1 -march=rv64gc
+#source: relax-align-3.s
+#objdump: -dr
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+0000 <_start-0x2>:
+[ 	]+0:[ 	]+0001[ 	]+nop
+[ 	]+0:[ 	]+R_RISCV_ALIGN[ 	]+\*ABS\*\+0x2
+
+0+0002 <_start>:
+[ 	]+2:[ 	]+00000537[ 	]+lui[ 	]+a0,0x0
+[ 	]+2:[ 	]+R_RISCV_HI20[ 	]+foo
+[ 	]+2:[ 	]+R_RISCV_RELAX[ 	]+\*ABS\*
+[ 	]+6:[ 	]+00050513[ 	]+mv[ 	]+a0,a0
+[ 	]+6:[ 	]+R_RISCV_LO12_I[ 	]+foo
+[ 	]+6:[ 	]+R_RISCV_RELAX[ 	]+\*ABS\*
+[ 	]+a:[ 	]+8082[ 	]+ret
+[ 	]+c:[ 	]+0001[ 	]+nop
+[ 	]+c:[ 	]+R_RISCV_ALIGN[ 	]+\*ABS\*\+0x6
+[ 	]+e:[ 	]+00000013[ 	]+nop
+
+0+0012 <SHOULD_ALIGN_8_HERE>:
+[ 	]+12:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+0016 <foo>:
+[ 	]+16:[ 	]+8082[ 	]+ret
diff --git a/gas/testsuite/gas/riscv/relax-align-3d.d b/gas/testsuite/gas/riscv/relax-align-3d.d
new file mode 100644
index 00000000000..11d21994c18
--- /dev/null
+++ b/gas/testsuite/gas/riscv/relax-align-3d.d
@@ -0,0 +1,30 @@
+#as: -mrelax -march=rv64gc
+#source: relax-align-3.s
+#objdump: -dr
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+0000 <_start-0x2>:
+[ 	]+0:[ 	]+0001[ 	]+nop
+[ 	]+0:[ 	]+R_RISCV_ALIGN[ 	]+\*ABS\*\+0x2
+
+0+0002 <_start>:
+[ 	]+2:[ 	]+00000537[ 	]+lui[ 	]+a0,0x0
+[ 	]+2:[ 	]+R_RISCV_HI20[ 	]+foo
+[ 	]+2:[ 	]+R_RISCV_RELAX[ 	]+\*ABS\*
+[ 	]+6:[ 	]+00050513[ 	]+mv[ 	]+a0,a0
+[ 	]+6:[ 	]+R_RISCV_LO12_I[ 	]+foo
+[ 	]+6:[ 	]+R_RISCV_RELAX[ 	]+\*ABS\*
+[ 	]+a:[ 	]+8082[ 	]+ret
+[ 	]+c:[ 	]+0001[ 	]+nop
+[ 	]+c:[ 	]+R_RISCV_ALIGN[ 	]+\*ABS\*\+0x6
+[ 	]+e:[ 	]+00000013[ 	]+nop
+
+0+0012 <SHOULD_ALIGN_8_HERE>:
+[ 	]+12:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+0016 <foo>:
+[ 	]+16:[ 	]+8082[ 	]+ret
-- 
2.54.0



More information about the Binutils mailing list