[PATCH 2/3] RISC-V: Use the 2-byte worst case for alignment in norvc code

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


Relaxing RVC code before a norvc region can move it by 2 bytes, so the
nops reserved for R_RISCV_ALIGN can be 2 bytes too few, and an alignment
of 4 gets no R_RISCV_ALIGN at all:

	lui	a0, %hi(foo)	# relaxed to c.lui
	.option norvc
	.balign	4

Use the 2-byte worst case if the object has RVC code.
---
 gas/config/tc-riscv.c                         | 17 ++--
 gas/testsuite/gas/riscv/mapping-dis.d         |  3 +-
 gas/testsuite/gas/riscv/mapping-symbols.d     |  2 +-
 gas/testsuite/gas/riscv/relax-align-2.d       | 78 ++++++++++---------
 gas/testsuite/gas/riscv/relax-align-3a.d      | 14 ++--
 gas/testsuite/gas/riscv/relax-align-3b.d      | 14 ++--
 gas/testsuite/gas/riscv/relax-align.d         | 49 ++++++------
 ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp    |  5 ++
 .../ld-riscv-elf/relax-align-1-norelax.d      | 24 ++++++
 .../relax-align-1-norelax.norvc.d             | 23 ++++++
 .../ld-riscv-elf/relax-align-1-norvc.d        | 23 ++++++
 .../ld-riscv-elf/relax-align-1-relax.rvc.d    | 24 ++++++
 ld/testsuite/ld-riscv-elf/relax-align-1.s     | 25 ++++++
 .../ld-riscv-elf/relax-align-10-norelax.d     | 25 ++++++
 .../relax-align-10-norelax.norvc.d            | 25 ++++++
 .../ld-riscv-elf/relax-align-10-norvc.d       | 25 ++++++
 .../ld-riscv-elf/relax-align-10-relax.rvc.d   | 25 ++++++
 ld/testsuite/ld-riscv-elf/relax-align-10.s    | 25 ++++++
 .../ld-riscv-elf/relax-align-11-norelax.d     | 26 +++++++
 .../relax-align-11-norelax.norvc.d            | 26 +++++++
 .../ld-riscv-elf/relax-align-11-norvc.d       | 26 +++++++
 .../ld-riscv-elf/relax-align-11-relax.rvc.d   | 26 +++++++
 ld/testsuite/ld-riscv-elf/relax-align-11.s    | 26 +++++++
 .../ld-riscv-elf/relax-align-12-norelax.d     | 25 ++++++
 .../relax-align-12-norelax.norvc.d            | 25 ++++++
 .../ld-riscv-elf/relax-align-12-norvc.d       | 25 ++++++
 .../ld-riscv-elf/relax-align-12-relax.rvc.d   | 25 ++++++
 ld/testsuite/ld-riscv-elf/relax-align-12.s    | 25 ++++++
 .../ld-riscv-elf/relax-align-2-norelax.d      | 21 +++++
 .../relax-align-2-norelax.norvc.d             | 23 ++++++
 .../ld-riscv-elf/relax-align-2-norvc.d        | 23 ++++++
 .../ld-riscv-elf/relax-align-2-relax.rvc.d    | 21 +++++
 ld/testsuite/ld-riscv-elf/relax-align-2.s     | 24 ++++++
 .../ld-riscv-elf/relax-align-3-norelax.d      | 21 +++++
 .../relax-align-3-norelax.norvc.d             | 21 +++++
 .../ld-riscv-elf/relax-align-3-norvc.d        | 21 +++++
 .../ld-riscv-elf/relax-align-3-relax.rvc.d    | 21 +++++
 ld/testsuite/ld-riscv-elf/relax-align-3.s     | 25 ++++++
 .../ld-riscv-elf/relax-align-4-norelax.d      | 19 +++++
 .../relax-align-4-norelax.norvc.d             | 19 +++++
 .../ld-riscv-elf/relax-align-4-norvc.d        | 19 +++++
 .../ld-riscv-elf/relax-align-4-relax.rvc.d    | 19 +++++
 ld/testsuite/ld-riscv-elf/relax-align-4.s     | 24 ++++++
 .../ld-riscv-elf/relax-align-5-norelax.d      | 22 ++++++
 .../relax-align-5-norelax.norvc.d             | 22 ++++++
 .../ld-riscv-elf/relax-align-5-norvc.d        | 22 ++++++
 .../ld-riscv-elf/relax-align-5-relax.rvc.d    | 22 ++++++
 ld/testsuite/ld-riscv-elf/relax-align-5.s     | 25 ++++++
 .../ld-riscv-elf/relax-align-6-norelax.d      | 21 +++++
 .../relax-align-6-norelax.norvc.d             | 21 +++++
 .../ld-riscv-elf/relax-align-6-norvc.d        | 21 +++++
 .../ld-riscv-elf/relax-align-6-relax.rvc.d    | 21 +++++
 ld/testsuite/ld-riscv-elf/relax-align-6.s     | 24 ++++++
 .../ld-riscv-elf/relax-align-7-norelax.d      | 22 ++++++
 .../relax-align-7-norelax.norvc.d             | 22 ++++++
 .../ld-riscv-elf/relax-align-7-norvc.d        | 22 ++++++
 .../ld-riscv-elf/relax-align-7-relax.rvc.d    | 22 ++++++
 ld/testsuite/ld-riscv-elf/relax-align-7.s     | 26 +++++++
 .../ld-riscv-elf/relax-align-8-norelax.d      | 20 +++++
 .../relax-align-8-norelax.norvc.d             | 20 +++++
 .../ld-riscv-elf/relax-align-8-norvc.d        | 20 +++++
 .../ld-riscv-elf/relax-align-8-relax.rvc.d    | 20 +++++
 ld/testsuite/ld-riscv-elf/relax-align-8.s     | 25 ++++++
 .../ld-riscv-elf/relax-align-9-norelax.d      | 26 +++++++
 .../relax-align-9-norelax.norvc.d             | 26 +++++++
 .../ld-riscv-elf/relax-align-9-norvc.d        | 26 +++++++
 .../ld-riscv-elf/relax-align-9-relax.rvc.d    | 26 +++++++
 ld/testsuite/ld-riscv-elf/relax-align-9.s     | 26 +++++++
 ld/testsuite/ld-riscv-elf/relax-align.ld      |  6 ++
 69 files changed, 1496 insertions(+), 82 deletions(-)
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-1-norelax.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-1-norelax.norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-1-norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-1-relax.rvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-1.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-10-norelax.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-10-norelax.norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-10-norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-10-relax.rvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-10.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-11-norelax.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-11-norelax.norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-11-norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-11-relax.rvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-11.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-12-norelax.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-12-norelax.norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-12-norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-12-relax.rvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-12.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-2-norelax.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-2-norelax.norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-2-norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-2-relax.rvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-2.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-3-norelax.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-3-norelax.norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-3-norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-3-relax.rvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-3.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-4-norelax.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-4-norelax.norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-4-norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-4-relax.rvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-4.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-5-norelax.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-5-norelax.norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-5-norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-5-relax.rvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-5.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-6-norelax.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-6-norelax.norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-6-norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-6-relax.rvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-6.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-7-norelax.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-7-norelax.norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-7-norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-7-relax.rvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-7.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-8-norelax.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-8-norelax.norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-8-norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-8-relax.rvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-8.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-9-norelax.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-9-norelax.norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-9-norvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-9-relax.rvc.d
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align-9.s
 create mode 100644 ld/testsuite/ld-riscv-elf/relax-align.ld

diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c
index b67a3e9b6be..2d39b39d6e7 100644
--- a/gas/config/tc-riscv.c
+++ b/gas/config/tc-riscv.c
@@ -5363,7 +5363,12 @@ bool
 riscv_frag_align_code (int n)
 {
   bfd_vma bytes = (bfd_vma) 1 << n;
-  bfd_vma insn_alignment = riscv_opts.rvc ? 2 : 4;
+  struct riscv_segment_info_type *info
+    = &seg_info (now_seg)->tc_segment_info_data;
+  /* Relaxing RVC code anywhere before this point can move it by 2 bytes,
+     even if RVC is off here, so use the 2-byte worst case then.  */
+  bfd_vma insn_alignment
+    = (riscv_opts.rvc || (elf_flags & EF_RISCV_RVC)) ? 2 : 4;
   bfd_vma worst_case_bytes = bytes - insn_alignment;
   char *nops;
   expressionS ex;
@@ -5373,24 +5378,22 @@ riscv_frag_align_code (int n)
   if (bytes <= insn_alignment)
     {
       if (bytes == insn_alignment)
-	seg_info (now_seg)->tc_segment_info_data.last_insn16 = false;
+	info->last_insn16 = false;
       return false;
     }
 
   /* 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)
+  if (!riscv_opts.relax && !info->last_relax)
     return false;
 
   /* If the last item emitted was not an ordinary insn, first align back to
      insn granularity.  Don't do this unconditionally, to avoid altering frags
      when that's not actually needed.  */
-  if (seg_info (now_seg)->tc_segment_info_data.map_state != MAP_INSN
-      || seg_info (now_seg)->tc_segment_info_data.last_insn16)
+  if (info->map_state != MAP_INSN || info->last_insn16)
     frag_align_code (riscv_opts.rvc ? 1 : 2, 0);
-  seg_info (now_seg)->tc_segment_info_data.last_insn16 = false;
+  info->last_insn16 = false;
 
   /* Maybe we should use frag_var to create a new rs_align_code fragment,
      rather than just use frag_more to handle an alignment here?  So that we
diff --git a/gas/testsuite/gas/riscv/mapping-dis.d b/gas/testsuite/gas/riscv/mapping-dis.d
index 133843a8a7c..caa53a24829 100644
--- a/gas/testsuite/gas/riscv/mapping-dis.d
+++ b/gas/testsuite/gas/riscv/mapping-dis.d
@@ -88,6 +88,7 @@ Disassembly of section .text.relax.align:
 0+000 <.text.relax.align>:
 [ 	]+[0-9a-f]+:[ 	]+0001[ 	]+nop
 [ 	]+[0-9a-f]+:[ 	]+4505[ 	]+li[ 	]+a0,1
+[ 	]+[0-9a-f]+:[ 	]+0001[ 	]+nop
 [ 	]+[0-9a-f]+:[ 	]+00000013[ 	]+nop
 [ 	]+[0-9a-f]+:[ 	]+00200513[ 	]+li[ 	]+a0,2
-[ 	]+[0-9a-f]+:[ 	]+00000013[ 	]+nop
+[ 	]+[0-9a-f]+:[ 	]+0001[ 	]+\.insn	2, 0x0001
diff --git a/gas/testsuite/gas/riscv/mapping-symbols.d b/gas/testsuite/gas/riscv/mapping-symbols.d
index ddc150274ad..1891b36f979 100644
--- a/gas/testsuite/gas/riscv/mapping-symbols.d
+++ b/gas/testsuite/gas/riscv/mapping-symbols.d
@@ -40,7 +40,7 @@ SYMBOL TABLE:
 0+06 l       .text.section.padding	0+00 \$d
 0+00 l    d  .text.relax.align	0+00 .text.relax.align
 0+00 l       .text.relax.align	0+00 \$xrv32i2p1_c2p0_zca1p0
-0+08 l       .text.relax.align	0+00 \$xrv32i2p1
+0+0a l       .text.relax.align	0+00 \$xrv32i2p1
 0+0a l       .text.section.padding	0+00 \$xrv32i2p1_c2p0_zca1p0
 0+03 l       .text.odd.align.start.insn	0+00 \$d
 0+04 l       .text.odd.align.start.insn	0+00 \$xrv32i2p1
diff --git a/gas/testsuite/gas/riscv/relax-align-2.d b/gas/testsuite/gas/riscv/relax-align-2.d
index 36ab78eba0c..640769577cf 100644
--- a/gas/testsuite/gas/riscv/relax-align-2.d
+++ b/gas/testsuite/gas/riscv/relax-align-2.d
@@ -9,44 +9,48 @@ Disassembly of section .text:
 0+000 <rvc_func>:
 [ 	]+0:[ 	]+8082[ 	]+ret
 [ 	]+2:[ 	]+0001[ 	]+nop
-[ 	]+4:[ 	]+00000013[ 	]+nop[ 	]+4: R_RISCV_ALIGN[ 	]+\*ABS\*\+0x4
-
-0+008 <non_rvc_func>:
-[ 	]+8:[ 	]+00008067[ 	]+ret
-
-0+00c <insn>:
-[ 	]+c:[ 	]+00000013[ 	]+nop
-[ 	]+10:[ 	]+0000[ 	]+\.insn	2, 0x0+
-[ 	]+12:[ 	]+0001[ 	]+\.insn	2, 0x0*1
-[ 	]+14:[ 	]+00000013[ 	]+nop[ 	]+14: R_RISCV_ALIGN[ 	]+\*ABS\*\+0x4
-[ 	]+18:[ 	]+00008067[ 	]+ret
-
-0+001c <hword>:
-[ 	]+1c:[ 	]+00000013[ 	]+nop
-[ 	]+20:[ 	]+0000[ 	]+\.short	0x0+
-[ 	]+22:[ 	]+0001[ 	]+\.insn	2, 0x0*1
-[ 	]+24:[ 	]+00000013[ 	]+nop[ 	]+24: R_RISCV_ALIGN[ 	]+\*ABS\*\+0x4
-[ 	]+28:[ 	]+00008067[ 	]+ret
-
-0+002c <byte>:
-[ 	]+2c:[ 	]+00000013[ 	]+nop
-[ 	]+30:[ 	]+00[ 	]+\.byte	0x0+
-[ 	]+31:[ 	]+00[ 	]+\.byte	0x0+
-[ 	]+32:[ 	]+0001[ 	]+\.insn	2, 0x0*1
-[ 	]+34:[ 	]+00000013[ 	]+nop[ 	]+34: R_RISCV_ALIGN[ 	]+\*ABS\*\+0x4
-[ 	]+38:[ 	]+00008067[ 	]+ret
-[ 	]+3c:[ 	]+00000013[ 	]+nop[ 	]+3c: R_RISCV_ALIGN[ 	]+\*ABS\*\+0x4
-
-0+0040 <func1>:
+[ 	]+4:[ 	]+0001[ 	]+nop[ 	]+4: R_RISCV_ALIGN[ 	]+\*ABS\*\+0x6
+[ 	]+6:[ 	]+00000013[ 	]+nop
+
+0+00a <non_rvc_func>:
+[ 	]+a:[ 	]+00008067[ 	]+ret
+
+0+00e <insn>:
+[ 	]+e:[ 	]+00000013[ 	]+nop
+[ 	]+12:[ 	]+0000[ 	]+\.insn[ 	]+2, 0x0000
+[ 	]+14:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001[ 	]+14: R_RISCV_ALIGN[ 	]+\*ABS\*\+0x6
+[ 	]+16:[ 	]+00000013[ 	]+nop
+[ 	]+1a:[ 	]+00008067[ 	]+ret
+
+0+001e <hword>:
+[ 	]+1e:[ 	]+00000013[ 	]+nop
+[ 	]+22:[ 	]+0000[ 	]+\.short[ 	]+0x0000
+[ 	]+24:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001[ 	]+24: R_RISCV_ALIGN[ 	]+\*ABS\*\+0x6
+[ 	]+26:[ 	]+00000013[ 	]+nop
+[ 	]+2a:[ 	]+00008067[ 	]+ret
+
+0+002e <byte>:
+[ 	]+2e:[ 	]+00000013[ 	]+nop
+[ 	]+32:[ 	]+00[ 	]+\.byte[ 	]+0x00
+[ 	]+33:[ 	]+00[ 	]+\.byte[ 	]+0x00
+[ 	]+34:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001[ 	]+34: R_RISCV_ALIGN[ 	]+\*ABS\*\+0x6
+[ 	]+36:[ 	]+00000013[ 	]+nop
+[ 	]+3a:[ 	]+00008067[ 	]+ret
+[ 	]+3e:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001[ 	]+3e: R_RISCV_ALIGN[ 	]+\*ABS\*\+0x6
 [ 	]+40:[ 	]+00000013[ 	]+nop
-[ 	]+44:[ 	]+00008067[ 	]+ret
 
-0+0048 <func2>:
-[ 	]+48:[ 	]+8082[ 	]+ret
-[ 	]+4a:[ 	]+0001[ 	]+nop
-[ 	]+4c:[ 	]+00000013[ 	]+nop[ 	]+4c: R_RISCV_ALIGN[ 	]+\*ABS\*\+0x4
+0+0044 <func1>:
+[ 	]+44:[ 	]+00000013[ 	]+nop
+[ 	]+48:[ 	]+00008067[ 	]+ret
 
-0+0050 <func3>:
-[ 	]+50:[ 	]+00000013[ 	]+nop
-[ 	]+54:[ 	]+00008067[ 	]+ret
+0+004c <func2>:
+[ 	]+4c:[ 	]+8082[ 	]+ret
+[ 	]+4e:[ 	]+0001[ 	]+nop
+[ 	]+50:[ 	]+0001[ 	]+nop[ 	]+50: R_RISCV_ALIGN[ 	]+\*ABS\*\+0x6
+[ 	]+52:[ 	]+00000013[ 	]+nop
+
+0+0056 <func3>:
+[ 	]+56:[ 	]+00000013[ 	]+nop
+[ 	]+5a:[ 	]+00008067[ 	]+ret
+[ 	]+5e:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
 #pass
diff --git a/gas/testsuite/gas/riscv/relax-align-3a.d b/gas/testsuite/gas/riscv/relax-align-3a.d
index 495d6257eac..c7b1c649c1a 100644
--- a/gas/testsuite/gas/riscv/relax-align-3a.d
+++ b/gas/testsuite/gas/riscv/relax-align-3a.d
@@ -19,12 +19,12 @@ Disassembly of section .text:
 [ 	]+6:[ 	]+R_RISCV_LO12_I[ 	]+foo
 [ 	]+6:[ 	]+R_RISCV_RELAX[ 	]+\*ABS\*
 [ 	]+a:[ 	]+8082[ 	]+ret
-[ 	]+c:[ 	]+00000013[ 	]+nop
-[ 	]+c:[ 	]+R_RISCV_ALIGN[ 	]+\*ABS\*\+0x4
+[ 	]+c:[ 	]+0001[ 	]+nop
+[ 	]+c:[ 	]+R_RISCV_ALIGN[ 	]+\*ABS\*\+0x6
+[ 	]+e:[ 	]+00000013[ 	]+nop
 
-0+0010 <SHOULD_ALIGN_8_HERE>:
-[ 	]+10:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+0+0012 <SHOULD_ALIGN_8_HERE>:
+[ 	]+12:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
 
-0+0014 <foo>:
-[ 	]+14:[ 	]+8082[ 	]+ret
-[ 	]+16:[ 	]+0001[ 	]+nop
+0+0016 <foo>:
+[ 	]+16:[ 	]+8082[ 	]+ret
diff --git a/gas/testsuite/gas/riscv/relax-align-3b.d b/gas/testsuite/gas/riscv/relax-align-3b.d
index 2e07d674a5c..9b680aa3cf8 100644
--- a/gas/testsuite/gas/riscv/relax-align-3b.d
+++ b/gas/testsuite/gas/riscv/relax-align-3b.d
@@ -19,12 +19,12 @@ Disassembly of section .text:
 [ 	]+6:[ 	]+R_RISCV_LO12_I[ 	]+foo
 [ 	]+6:[ 	]+R_RISCV_RELAX[ 	]+\*ABS\*
 [ 	]+a:[ 	]+8082[ 	]+ret
-[ 	]+c:[ 	]+00000013[ 	]+nop
-[ 	]+c:[ 	]+R_RISCV_ALIGN[ 	]+\*ABS\*\+0x4
+[ 	]+c:[ 	]+0001[ 	]+nop
+[ 	]+c:[ 	]+R_RISCV_ALIGN[ 	]+\*ABS\*\+0x6
+[ 	]+e:[ 	]+00000013[ 	]+nop
 
-0+0010 <SHOULD_ALIGN_8_HERE>:
-[ 	]+10:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+0+0012 <SHOULD_ALIGN_8_HERE>:
+[ 	]+12:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
 
-0+0014 <foo>:
-[ 	]+14:[ 	]+8082[ 	]+ret
-[ 	]+16:[ 	]+0001[ 	]+nop
+0+0016 <foo>:
+[ 	]+16:[ 	]+8082[ 	]+ret
diff --git a/gas/testsuite/gas/riscv/relax-align.d b/gas/testsuite/gas/riscv/relax-align.d
index 0aed58e0835..9a1ac8a5aba 100644
--- a/gas/testsuite/gas/riscv/relax-align.d
+++ b/gas/testsuite/gas/riscv/relax-align.d
@@ -9,26 +9,31 @@ Disassembly of section .text:
 0+000 <rvc_func>:
 [ 	]+0:[ 	]+8082[ 	]+ret
 [ 	]+2:[ 	]+0001[ 	]+nop
-
-0+004 <non_rvc_func>:
-[ 	]+4:[ 	]+00008067[ 	]+ret
-
-0+008 <insn>:
-[ 	]+8:[ 	]+00000013[ 	]+nop
-[ 	]+c:[ 	]+0000[ 	]+\.insn	2, 0x0+
-[ 	]+e:[ 	]+0001[ 	]+\.insn	2, 0x0*1
-[ 	]+10:[ 	]+00008067[ 	]+ret
-
-0+0014 <hword>:
-[ 	]+14:[ 	]+00000013[ 	]+nop
-[ 	]+18:[ 	]+0000[ 	]+\.short	0x0+
-[ 	]+1a:[ 	]+0001[ 	]+\.insn	2, 0x0*1
-[ 	]+1c:[ 	]+00008067[ 	]+ret
-
-0+0020 <byte>:
-[ 	]+20:[ 	]+00000013[ 	]+nop
-[ 	]+24:[ 	]+00[ 	]+\.byte	0x0+
-[ 	]+25:[ 	]+00[ 	]+\.byte	0x0+
-[ 	]+26:[ 	]+0001[ 	]+\.insn	2, 0x0*1
-[ 	]+28:[ 	]+00008067[ 	]+ret
+[ 	]+4:[ 	]+0001[ 	]+nop
+[ 	]+4:[ 	]+R_RISCV_ALIGN[ 	]+\*ABS\*\+0x2
+
+0+006 <non_rvc_func>:
+[ 	]+6:[ 	]+00008067[ 	]+ret
+
+0+00a <insn>:
+[ 	]+a:[ 	]+00000013[ 	]+nop
+[ 	]+e:[ 	]+0000[ 	]+\.insn[ 	]+2, 0x0000
+[ 	]+10:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+[ 	]+10:[ 	]+R_RISCV_ALIGN[ 	]+\*ABS\*\+0x2
+[ 	]+12:[ 	]+00008067[ 	]+ret
+
+0+0016 <hword>:
+[ 	]+16:[ 	]+00000013[ 	]+nop
+[ 	]+1a:[ 	]+0000[ 	]+\.short[ 	]+0x0000
+[ 	]+1c:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+[ 	]+1c:[ 	]+R_RISCV_ALIGN[ 	]+\*ABS\*\+0x2
+[ 	]+1e:[ 	]+00008067[ 	]+ret
+
+0+0022 <byte>:
+[ 	]+22:[ 	]+00000013[ 	]+nop
+[ 	]+26:[ 	]+00[ 	]+\.byte[ 	]+0x00
+[ 	]+27:[ 	]+00[ 	]+\.byte[ 	]+0x00
+[ 	]+28:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+[ 	]+28:[ 	]+R_RISCV_ALIGN[ 	]+\*ABS\*\+0x2
+[ 	]+2a:[ 	]+00008067[ 	]+ret
 #pass
diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
index cba0923c650..8dd3ca5b112 100644
--- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
+++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
@@ -207,6 +207,11 @@ if [istarget "riscv*-*-*"] {
     run_dump_test "attr-phdr"
     run_dump_test "relax-max-align-gp"
     run_dump_test "relax-data-segment-align"
+    foreach n {1 2 3 4 5 6 7 8 9 10 11 12} {
+	foreach v {norvc norelax.norvc norelax relax.rvc} {
+	    run_dump_test "relax-align-$n-$v"
+	}
+    }
     run_dump_test "uleb128"
     run_dump_test "pr31179"
     run_dump_test "pr31179-r"
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-1-norelax.d b/ld/testsuite/ld-riscv-elf/relax-align-1-norelax.d
new file mode 100644
index 00000000000..5a4302905a4
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-1-norelax.d
@@ -0,0 +1,24 @@
+#source: relax-align-1.s
+#as: -mrelax -march=rv64gc -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01450513[ 	]+addi[ 	]+a0,a0,20[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+0001[ 	]+nop
+[ 	]+100c:[ 	]+00000013[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_8_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+8082[ 	]+ret
+[ 	]+1016:[ 	]+0001[ 	]+nop
+[ 	]+1018:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-1-norelax.norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-1-norelax.norvc.d
new file mode 100644
index 00000000000..f2b96efbde5
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-1-norelax.norvc.d
@@ -0,0 +1,23 @@
+#source: relax-align-1.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1 -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01450513[ 	]+addi[ 	]+a0,a0,20[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+0001[ 	]+nop
+[ 	]+100c:[ 	]+00000013[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_8_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+8082[ 	]+ret
+[ 	]+1016:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-1-norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-1-norvc.d
new file mode 100644
index 00000000000..9ef2aea2a76
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-1-norvc.d
@@ -0,0 +1,23 @@
+#source: relax-align-1.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01450513[ 	]+addi[ 	]+a0,a0,20[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+0001[ 	]+nop
+[ 	]+100c:[ 	]+00000013[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_8_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+8082[ 	]+ret
+[ 	]+1016:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-1-relax.rvc.d b/ld/testsuite/ld-riscv-elf/relax-align-1-relax.rvc.d
new file mode 100644
index 00000000000..260d13dfa43
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-1-relax.rvc.d
@@ -0,0 +1,24 @@
+#source: relax-align-1.s
+#as: -mrelax -march=rv64gc
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01450513[ 	]+addi[ 	]+a0,a0,20[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+0001[ 	]+nop
+[ 	]+100c:[ 	]+00000013[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_8_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+8082[ 	]+ret
+[ 	]+1016:[ 	]+0001[ 	]+nop
+[ 	]+1018:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-1.s b/ld/testsuite/ld-riscv-elf/relax-align-1.s
new file mode 100644
index 00000000000..5f41cab8621
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-1.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)
+	mul a0, a1, a4
+        .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/ld/testsuite/ld-riscv-elf/relax-align-10-norelax.d b/ld/testsuite/ld-riscv-elf/relax-align-10-norelax.d
new file mode 100644
index 00000000000..a2ac0c860aa
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-10-norelax.d
@@ -0,0 +1,25 @@
+#source: relax-align-10.s
+#as: -mrelax -march=rv64gc -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+014000ef[ 	]+jal[ 	]+[0-9a-z]+[ 	]+<foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+1008:[ 	]+00000013[ 	]+nop
+[ 	]+100c:[ 	]+00000013[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+00008067[ 	]+ret
+[ 	]+1018:[ \t]+0001[ \t]+\.insn\t2, 0x0001
+[ 	]+101a:[ 	]+00000013[ 	]+nop
+[ 	]+101e:[ 	]+00000013[ 	]+nop
+[ 	]+1022:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-10-norelax.norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-10-norelax.norvc.d
new file mode 100644
index 00000000000..45056581c8d
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-10-norelax.norvc.d
@@ -0,0 +1,25 @@
+#source: relax-align-10.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1 -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+014000ef[ 	]+jal[ 	]+[0-9a-z]+[ 	]+<foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+1008:[ 	]+00000013[ 	]+nop
+[ 	]+100c:[ 	]+00000013[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+00008067[ 	]+ret
+[ 	]+1018:[ \t]+0001[ \t]+\.insn\t2, 0x0001
+[ 	]+101a:[ 	]+00000013[ 	]+nop
+[ 	]+101e:[ 	]+00000013[ 	]+nop
+[ 	]+1022:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-10-norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-10-norvc.d
new file mode 100644
index 00000000000..f2ce4305b78
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-10-norvc.d
@@ -0,0 +1,25 @@
+#source: relax-align-10.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+014000ef[ 	]+jal[ 	]+[0-9a-z]+[ 	]+<foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+1008:[ 	]+00000013[ 	]+nop
+[ 	]+100c:[ 	]+00000013[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+00008067[ 	]+ret
+[ 	]+1018:[ \t]+0001[ \t]+\.insn\t2, 0x0001
+[ 	]+101a:[ 	]+00000013[ 	]+nop
+[ 	]+101e:[ 	]+00000013[ 	]+nop
+[ 	]+1022:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-10-relax.rvc.d b/ld/testsuite/ld-riscv-elf/relax-align-10-relax.rvc.d
new file mode 100644
index 00000000000..64fff86c18c
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-10-relax.rvc.d
@@ -0,0 +1,25 @@
+#source: relax-align-10.s
+#as: -mrelax -march=rv64gc
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+014000ef[ 	]+jal[ 	]+[0-9a-z]+[ 	]+<foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+1008:[ 	]+00000013[ 	]+nop
+[ 	]+100c:[ 	]+00000013[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+00008067[ 	]+ret
+[ 	]+1018:[ \t]+0001[ \t]+\.insn\t2, 0x0001
+[ 	]+101a:[ 	]+00000013[ 	]+nop
+[ 	]+101e:[ 	]+00000013[ 	]+nop
+[ 	]+1022:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-10.s b/ld/testsuite/ld-riscv-elf/relax-align-10.s
new file mode 100644
index 00000000000..c1b76b0c5b6
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-10.s
@@ -0,0 +1,25 @@
+        .text
+        .option relax
+        .balign 2
+        .global _start
+        .type _start, @function
+_start:
+	call foo
+        .option norvc
+	mul a0, a1, a4
+        .option push
+
+.ifdef NORELAX
+        .option norelax
+.endif
+.ifdef NORVC
+        .option norvc
+.endif
+        .balign 16
+SHOULD_ALIGN_16_HERE:
+        .word 0x12345678
+
+        .option pop
+
+foo:
+        ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-11-norelax.d b/ld/testsuite/ld-riscv-elf/relax-align-11-norelax.d
new file mode 100644
index 00000000000..bc72838c2ac
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-11-norelax.d
@@ -0,0 +1,26 @@
+#source: relax-align-11.s
+#as: -mrelax -march=rv64gc -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01450513[ 	]+addi[ 	]+a0,a0,20[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+00000013[ 	]+nop
+[ 	]+100e:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+00008067[ 	]+ret
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+00008067[ 	]+ret
+[ 	]+1018:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+[ 	]+101a:[ 	]+00000013[ 	]+nop
+[ 	]+101e:[ 	]+00000013[ 	]+nop
+[ 	]+1022:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-11-norelax.norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-11-norelax.norvc.d
new file mode 100644
index 00000000000..4ae38a23adf
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-11-norelax.norvc.d
@@ -0,0 +1,26 @@
+#source: relax-align-11.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1 -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01450513[ 	]+addi[ 	]+a0,a0,20[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+00000013[ 	]+nop
+[ 	]+100e:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+00008067[ 	]+ret
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+00008067[ 	]+ret
+[ 	]+1018:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+[ 	]+101a:[ 	]+00000013[ 	]+nop
+[ 	]+101e:[ 	]+00000013[ 	]+nop
+[ 	]+1022:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-11-norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-11-norvc.d
new file mode 100644
index 00000000000..d0fd4512ed4
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-11-norvc.d
@@ -0,0 +1,26 @@
+#source: relax-align-11.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01450513[ 	]+addi[ 	]+a0,a0,20[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+00000013[ 	]+nop
+[ 	]+100e:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+00008067[ 	]+ret
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+00008067[ 	]+ret
+[ 	]+1018:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+[ 	]+101a:[ 	]+00000013[ 	]+nop
+[ 	]+101e:[ 	]+00000013[ 	]+nop
+[ 	]+1022:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-11-relax.rvc.d b/ld/testsuite/ld-riscv-elf/relax-align-11-relax.rvc.d
new file mode 100644
index 00000000000..22cb5d9b770
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-11-relax.rvc.d
@@ -0,0 +1,26 @@
+#source: relax-align-11.s
+#as: -mrelax -march=rv64gc
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01450513[ 	]+addi[ 	]+a0,a0,20[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+00000013[ 	]+nop
+[ 	]+100e:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+00008067[ 	]+ret
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+00008067[ 	]+ret
+[ 	]+1018:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+[ 	]+101a:[ 	]+00000013[ 	]+nop
+[ 	]+101e:[ 	]+00000013[ 	]+nop
+[ 	]+1022:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-11.s b/ld/testsuite/ld-riscv-elf/relax-align-11.s
new file mode 100644
index 00000000000..999035aeed6
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-11.s
@@ -0,0 +1,26 @@
+        .text
+        .option relax
+        .balign 2
+        .global _start
+        .type _start, @function
+_start:
+        lui a0, %hi(foo)
+        addi a0, a0, %lo(foo)
+        .option norvc
+	mul a0, a1, a4
+        .option push
+
+.ifdef NORELAX
+        .option norelax
+.endif
+.ifdef NORVC
+        .option norvc
+.endif
+        .balign 16
+SHOULD_ALIGN_16_HERE:
+	ret
+
+        .option pop
+
+foo:
+        ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-12-norelax.d b/ld/testsuite/ld-riscv-elf/relax-align-12-norelax.d
new file mode 100644
index 00000000000..2780f7f7072
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-12-norelax.d
@@ -0,0 +1,25 @@
+#source: relax-align-12.s
+#as: -mrelax -march=rv64gc -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+014000ef[ 	]+jal[ 	]+[0-9a-z]+[ 	]+<foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+1008:[ 	]+00000013[ 	]+nop
+[ 	]+100c:[ 	]+00000013[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+00008067[ 	]+ret
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+00008067[ 	]+ret
+[ 	]+1018:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+[ 	]+101a:[ 	]+00000013[ 	]+nop
+[ 	]+101e:[ 	]+00000013[ 	]+nop
+[ 	]+1022:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-12-norelax.norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-12-norelax.norvc.d
new file mode 100644
index 00000000000..65f225f4e88
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-12-norelax.norvc.d
@@ -0,0 +1,25 @@
+#source: relax-align-12.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1 -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+014000ef[ 	]+jal[ 	]+[0-9a-z]+[ 	]+<foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+1008:[ 	]+00000013[ 	]+nop
+[ 	]+100c:[ 	]+00000013[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+00008067[ 	]+ret
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+00008067[ 	]+ret
+[ 	]+1018:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+[ 	]+101a:[ 	]+00000013[ 	]+nop
+[ 	]+101e:[ 	]+00000013[ 	]+nop
+[ 	]+1022:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-12-norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-12-norvc.d
new file mode 100644
index 00000000000..c2978a2255f
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-12-norvc.d
@@ -0,0 +1,25 @@
+#source: relax-align-12.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+014000ef[ 	]+jal[ 	]+[0-9a-z]+[ 	]+<foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+1008:[ 	]+00000013[ 	]+nop
+[ 	]+100c:[ 	]+00000013[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+00008067[ 	]+ret
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+00008067[ 	]+ret
+[ 	]+1018:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+[ 	]+101a:[ 	]+00000013[ 	]+nop
+[ 	]+101e:[ 	]+00000013[ 	]+nop
+[ 	]+1022:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-12-relax.rvc.d b/ld/testsuite/ld-riscv-elf/relax-align-12-relax.rvc.d
new file mode 100644
index 00000000000..cde857bdb6d
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-12-relax.rvc.d
@@ -0,0 +1,25 @@
+#source: relax-align-12.s
+#as: -mrelax -march=rv64gc
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+014000ef[ 	]+jal[ 	]+[0-9a-z]+[ 	]+<foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+1008:[ 	]+00000013[ 	]+nop
+[ 	]+100c:[ 	]+00000013[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+00008067[ 	]+ret
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+00008067[ 	]+ret
+[ 	]+1018:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+[ 	]+101a:[ 	]+00000013[ 	]+nop
+[ 	]+101e:[ 	]+00000013[ 	]+nop
+[ 	]+1022:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-12.s b/ld/testsuite/ld-riscv-elf/relax-align-12.s
new file mode 100644
index 00000000000..0754055a47d
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-12.s
@@ -0,0 +1,25 @@
+        .text
+        .option relax
+        .balign 2
+        .global _start
+        .type _start, @function
+_start:
+	call foo
+        .option norvc
+	mul a0, a1, a4
+        .option push
+
+.ifdef NORELAX
+        .option norelax
+.endif
+.ifdef NORVC
+        .option norvc
+.endif
+        .balign 16
+SHOULD_ALIGN_16_HERE:
+	ret
+
+        .option pop
+
+foo:
+        ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-2-norelax.d b/ld/testsuite/ld-riscv-elf/relax-align-2-norelax.d
new file mode 100644
index 00000000000..276f3ae60ee
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-2-norelax.d
@@ -0,0 +1,21 @@
+#source: relax-align-2.s
+#as: -mrelax -march=rv64gc -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+00c000ef[ 	]+jal[ 	]+100c <foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+
+0+1008 <SHOULD_ALIGN_8_HERE>:
+[ 	]+1008:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+100c <foo>:
+[ 	]+100c:[ 	]+8082[ 	]+ret
+[ 	]+100e:[ 	]+0001[ 	]+nop
+[ 	]+1010:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-2-norelax.norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-2-norelax.norvc.d
new file mode 100644
index 00000000000..236fce7eb3f
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-2-norelax.norvc.d
@@ -0,0 +1,23 @@
+#source: relax-align-2.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1 -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+014000ef[ 	]+jal[ 	]+[0-9a-z]+[ 	]+<foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+1008:[ 	]+0001[ 	]+nop
+[ 	]+100a:[ 	]+0001[ 	]+nop
+[ 	]+100c:[ 	]+00000013[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_8_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+8082[ 	]+ret
+[ 	]+1016:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-2-norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-2-norvc.d
new file mode 100644
index 00000000000..d1b72489560
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-2-norvc.d
@@ -0,0 +1,23 @@
+#source: relax-align-2.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+014000ef[ 	]+jal[ 	]+[0-9a-z]+[ 	]+<foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+1008:[ 	]+0001[ 	]+nop
+[ 	]+100a:[ 	]+0001[ 	]+nop
+[ 	]+100c:[ 	]+00000013[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_8_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+8082[ 	]+ret
+[ 	]+1016:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-2-relax.rvc.d b/ld/testsuite/ld-riscv-elf/relax-align-2-relax.rvc.d
new file mode 100644
index 00000000000..dec504482de
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-2-relax.rvc.d
@@ -0,0 +1,21 @@
+#source: relax-align-2.s
+#as: -mrelax -march=rv64gc
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+00c000ef[ 	]+jal[ 	]+100c <foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+
+0+1008 <SHOULD_ALIGN_8_HERE>:
+[ 	]+1008:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+100c <foo>:
+[ 	]+100c:[ 	]+8082[ 	]+ret
+[ 	]+100e:[ 	]+0001[ 	]+nop
+[ 	]+1010:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-2.s b/ld/testsuite/ld-riscv-elf/relax-align-2.s
new file mode 100644
index 00000000000..4c7000813c3
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-2.s
@@ -0,0 +1,24 @@
+        .text
+        .option relax
+        .balign 4
+        .global _start
+        .type _start, @function
+_start:
+	call foo
+	mul a0, a1, a4
+        .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/ld/testsuite/ld-riscv-elf/relax-align-3-norelax.d b/ld/testsuite/ld-riscv-elf/relax-align-3-norelax.d
new file mode 100644
index 00000000000..0307f728968
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-3-norelax.d
@@ -0,0 +1,21 @@
+#source: relax-align-3.s
+#as: -mrelax -march=rv64gc -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01050513[ 	]+addi[ 	]+a0,a0,16[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+0001[ 	]+nop
+
+0+100c <SHOULD_ALIGN_4_HERE>:
+[ 	]+100c:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1010 <foo>:
+[ 	]+1010:[ 	]+8082[ 	]+ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-3-norelax.norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-3-norelax.norvc.d
new file mode 100644
index 00000000000..fed5c802cfc
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-3-norelax.norvc.d
@@ -0,0 +1,21 @@
+#source: relax-align-3.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1 -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01050513[ 	]+addi[ 	]+a0,a0,16[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+0001[ 	]+nop
+
+0+100c <SHOULD_ALIGN_4_HERE>:
+[ 	]+100c:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1010 <foo>:
+[ 	]+1010:[ 	]+8082[ 	]+ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-3-norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-3-norvc.d
new file mode 100644
index 00000000000..3d293eb031e
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-3-norvc.d
@@ -0,0 +1,21 @@
+#source: relax-align-3.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01050513[ 	]+addi[ 	]+a0,a0,16[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+0001[ 	]+nop
+
+0+100c <SHOULD_ALIGN_4_HERE>:
+[ 	]+100c:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1010 <foo>:
+[ 	]+1010:[ 	]+8082[ 	]+ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-3-relax.rvc.d b/ld/testsuite/ld-riscv-elf/relax-align-3-relax.rvc.d
new file mode 100644
index 00000000000..30bc492809d
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-3-relax.rvc.d
@@ -0,0 +1,21 @@
+#source: relax-align-3.s
+#as: -mrelax -march=rv64gc
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01050513[ 	]+addi[ 	]+a0,a0,16[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+0001[ 	]+nop
+
+0+100c <SHOULD_ALIGN_4_HERE>:
+[ 	]+100c:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1010 <foo>:
+[ 	]+1010:[ 	]+8082[ 	]+ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-3.s b/ld/testsuite/ld-riscv-elf/relax-align-3.s
new file mode 100644
index 00000000000..336bb3b59bf
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-3.s
@@ -0,0 +1,25 @@
+        .text
+        .option relax
+        .balign 2
+        .global _start
+        .type _start, @function
+_start:
+        lui a0, %hi(foo)
+        addi a0, a0, %lo(foo)
+	mul a0, a1, a4
+        .option push
+
+.ifdef NORELAX
+        .option norelax
+.endif
+.ifdef NORVC
+        .option norvc
+.endif
+        .balign 4
+SHOULD_ALIGN_4_HERE:
+        .word 0x12345678
+
+        .option pop
+
+foo:
+        ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-4-norelax.d b/ld/testsuite/ld-riscv-elf/relax-align-4-norelax.d
new file mode 100644
index 00000000000..b2bc425ef36
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-4-norelax.d
@@ -0,0 +1,19 @@
+#source: relax-align-4.s
+#as: -mrelax -march=rv64gc -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+00c000ef[ 	]+jal[ 	]+100c <foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+
+0+1008 <SHOULD_ALIGN_4_HERE>:
+[ 	]+1008:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+100c <foo>:
+[ 	]+100c:[ 	]+8082[ 	]+ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-4-norelax.norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-4-norelax.norvc.d
new file mode 100644
index 00000000000..90a1fa49fb8
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-4-norelax.norvc.d
@@ -0,0 +1,19 @@
+#source: relax-align-4.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1 -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+00c000ef[ 	]+jal[ 	]+100c <foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+
+0+1008 <SHOULD_ALIGN_4_HERE>:
+[ 	]+1008:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+100c <foo>:
+[ 	]+100c:[ 	]+8082[ 	]+ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-4-norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-4-norvc.d
new file mode 100644
index 00000000000..49145a17b62
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-4-norvc.d
@@ -0,0 +1,19 @@
+#source: relax-align-4.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+00c000ef[ 	]+jal[ 	]+100c <foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+
+0+1008 <SHOULD_ALIGN_4_HERE>:
+[ 	]+1008:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+100c <foo>:
+[ 	]+100c:[ 	]+8082[ 	]+ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-4-relax.rvc.d b/ld/testsuite/ld-riscv-elf/relax-align-4-relax.rvc.d
new file mode 100644
index 00000000000..2f3df94e394
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-4-relax.rvc.d
@@ -0,0 +1,19 @@
+#source: relax-align-4.s
+#as: -mrelax -march=rv64gc
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+00c000ef[ 	]+jal[ 	]+100c <foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+
+0+1008 <SHOULD_ALIGN_4_HERE>:
+[ 	]+1008:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+100c <foo>:
+[ 	]+100c:[ 	]+8082[ 	]+ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-4.s b/ld/testsuite/ld-riscv-elf/relax-align-4.s
new file mode 100644
index 00000000000..a62ab73b3d4
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-4.s
@@ -0,0 +1,24 @@
+        .text
+        .option relax
+        .balign 2
+        .global _start
+        .type _start, @function
+_start:
+	call foo
+	mul a0, a1, a4
+        .option push
+
+.ifdef NORELAX
+        .option norelax
+.endif
+.ifdef NORVC
+        .option norvc
+.endif
+        .balign 4
+SHOULD_ALIGN_4_HERE:
+        .word 0x12345678
+
+        .option pop
+
+foo:
+        ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-5-norelax.d b/ld/testsuite/ld-riscv-elf/relax-align-5-norelax.d
new file mode 100644
index 00000000000..4c8260de600
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-5-norelax.d
@@ -0,0 +1,22 @@
+#source: relax-align-5.s
+#as: -mrelax -march=rv64gc -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01450513[ 	]+addi[ 	]+a0,a0,20[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+00000013[ 	]+nop
+[ 	]+100e:[ 	]+0001[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+8082[ 	]+ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-5-norelax.norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-5-norelax.norvc.d
new file mode 100644
index 00000000000..3962ca22da0
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-5-norelax.norvc.d
@@ -0,0 +1,22 @@
+#source: relax-align-5.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1 -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01450513[ 	]+addi[ 	]+a0,a0,20[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+00000013[ 	]+nop
+[ 	]+100e:[ 	]+0001[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+8082[ 	]+ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-5-norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-5-norvc.d
new file mode 100644
index 00000000000..0a9b6af3bbd
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-5-norvc.d
@@ -0,0 +1,22 @@
+#source: relax-align-5.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01450513[ 	]+addi[ 	]+a0,a0,20[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+00000013[ 	]+nop
+[ 	]+100e:[ 	]+0001[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+8082[ 	]+ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-5-relax.rvc.d b/ld/testsuite/ld-riscv-elf/relax-align-5-relax.rvc.d
new file mode 100644
index 00000000000..ebc4f044236
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-5-relax.rvc.d
@@ -0,0 +1,22 @@
+#source: relax-align-5.s
+#as: -mrelax -march=rv64gc
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01450513[ 	]+addi[ 	]+a0,a0,20[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+00000013[ 	]+nop
+[ 	]+100e:[ 	]+0001[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+8082[ 	]+ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-5.s b/ld/testsuite/ld-riscv-elf/relax-align-5.s
new file mode 100644
index 00000000000..f04857185ce
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-5.s
@@ -0,0 +1,25 @@
+        .text
+        .option relax
+        .balign 2
+        .global _start
+        .type _start, @function
+_start:
+        lui a0, %hi(foo)
+        addi a0, a0, %lo(foo)
+	mul a0, a1, a4
+        .option push
+
+.ifdef NORELAX
+        .option norelax
+.endif
+.ifdef NORVC
+        .option norvc
+.endif
+        .balign 16
+SHOULD_ALIGN_16_HERE:
+        .word 0x12345678
+
+        .option pop
+
+foo:
+        ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-6-norelax.d b/ld/testsuite/ld-riscv-elf/relax-align-6-norelax.d
new file mode 100644
index 00000000000..f515030697e
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-6-norelax.d
@@ -0,0 +1,21 @@
+#source: relax-align-6.s
+#as: -mrelax -march=rv64gc -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+014000ef[ 	]+jal[ 	]+[0-9a-z]+[ 	]+<foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+1008:[ 	]+00000013[ 	]+nop
+[ 	]+100c:[ 	]+00000013[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+8082[ 	]+ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-6-norelax.norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-6-norelax.norvc.d
new file mode 100644
index 00000000000..463fcca5536
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-6-norelax.norvc.d
@@ -0,0 +1,21 @@
+#source: relax-align-6.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1 -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+014000ef[ 	]+jal[ 	]+[0-9a-z]+[ 	]+<foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+1008:[ 	]+00000013[ 	]+nop
+[ 	]+100c:[ 	]+00000013[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+8082[ 	]+ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-6-norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-6-norvc.d
new file mode 100644
index 00000000000..61a4f50543c
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-6-norvc.d
@@ -0,0 +1,21 @@
+#source: relax-align-6.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+014000ef[ 	]+jal[ 	]+[0-9a-z]+[ 	]+<foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+1008:[ 	]+00000013[ 	]+nop
+[ 	]+100c:[ 	]+00000013[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+8082[ 	]+ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-6-relax.rvc.d b/ld/testsuite/ld-riscv-elf/relax-align-6-relax.rvc.d
new file mode 100644
index 00000000000..6cfa0dc34a6
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-6-relax.rvc.d
@@ -0,0 +1,21 @@
+#source: relax-align-6.s
+#as: -mrelax -march=rv64gc
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+014000ef[ 	]+jal[ 	]+[0-9a-z]+[ 	]+<foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+1008:[ 	]+00000013[ 	]+nop
+[ 	]+100c:[ 	]+00000013[ 	]+nop
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+8082[ 	]+ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-6.s b/ld/testsuite/ld-riscv-elf/relax-align-6.s
new file mode 100644
index 00000000000..3dab05f14f0
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-6.s
@@ -0,0 +1,24 @@
+        .text
+        .option relax
+        .balign 2
+        .global _start
+        .type _start, @function
+_start:
+	call foo
+	mul a0, a1, a4
+        .option push
+
+.ifdef NORELAX
+        .option norelax
+.endif
+.ifdef NORVC
+        .option norvc
+.endif
+        .balign 16
+SHOULD_ALIGN_16_HERE:
+        .word 0x12345678
+
+        .option pop
+
+foo:
+        ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-7-norelax.d b/ld/testsuite/ld-riscv-elf/relax-align-7-norelax.d
new file mode 100644
index 00000000000..bf111ea9287
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-7-norelax.d
@@ -0,0 +1,22 @@
+#source: relax-align-7.s
+#as: -mrelax -march=rv64gc -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01050513[ 	]+addi[ 	]+a0,a0,16[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+
+0+100c <SHOULD_ALIGN_4_HERE>:
+[ 	]+100c:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1010 <foo>:
+[ 	]+1010:[ 	]+00008067[ 	]+ret
+[ 	]+1014:[ \t]+0001[ \t]+\.insn\t2, 0x0001
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-7-norelax.norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-7-norelax.norvc.d
new file mode 100644
index 00000000000..71300c0e122
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-7-norelax.norvc.d
@@ -0,0 +1,22 @@
+#source: relax-align-7.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1 -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01050513[ 	]+addi[ 	]+a0,a0,16[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+
+0+100c <SHOULD_ALIGN_4_HERE>:
+[ 	]+100c:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1010 <foo>:
+[ 	]+1010:[ 	]+00008067[ 	]+ret
+[ 	]+1014:[ \t]+0001[ \t]+\.insn\t2, 0x0001
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-7-norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-7-norvc.d
new file mode 100644
index 00000000000..c1775865f5a
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-7-norvc.d
@@ -0,0 +1,22 @@
+#source: relax-align-7.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01050513[ 	]+addi[ 	]+a0,a0,16[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+
+0+100c <SHOULD_ALIGN_4_HERE>:
+[ 	]+100c:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1010 <foo>:
+[ 	]+1010:[ 	]+00008067[ 	]+ret
+[ 	]+1014:[ \t]+0001[ \t]+\.insn\t2, 0x0001
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-7-relax.rvc.d b/ld/testsuite/ld-riscv-elf/relax-align-7-relax.rvc.d
new file mode 100644
index 00000000000..76e14ed6bfe
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-7-relax.rvc.d
@@ -0,0 +1,22 @@
+#source: relax-align-7.s
+#as: -mrelax -march=rv64gc
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01050513[ 	]+addi[ 	]+a0,a0,16[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+
+0+100c <SHOULD_ALIGN_4_HERE>:
+[ 	]+100c:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1010 <foo>:
+[ 	]+1010:[ 	]+00008067[ 	]+ret
+[ 	]+1014:[ \t]+0001[ \t]+\.insn\t2, 0x0001
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-7.s b/ld/testsuite/ld-riscv-elf/relax-align-7.s
new file mode 100644
index 00000000000..a7f9151e275
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-7.s
@@ -0,0 +1,26 @@
+        .text
+        .option relax
+        .balign 2
+        .global _start
+        .type _start, @function
+_start:
+        lui a0, %hi(foo)
+        addi a0, a0, %lo(foo)
+	.option norvc
+	mul a0, a1, a4
+        .option push
+
+.ifdef NORELAX
+        .option norelax
+.endif
+.ifdef NORVC
+        .option norvc
+.endif
+        .balign 4
+SHOULD_ALIGN_4_HERE:
+        .word 0x12345678
+
+        .option pop
+
+foo:
+        ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-8-norelax.d b/ld/testsuite/ld-riscv-elf/relax-align-8-norelax.d
new file mode 100644
index 00000000000..958323aa109
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-8-norelax.d
@@ -0,0 +1,20 @@
+#source: relax-align-8.s
+#as: -mrelax -march=rv64gc -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+00c000ef[ 	]+jal[ 	]+100c <foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+
+0+1008 <SHOULD_ALIGN_4_HERE>:
+[ 	]+1008:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+100c <foo>:
+[ 	]+100c:[ 	]+00008067[ 	]+ret
+[ 	]+1010:[ \t]+0001[ \t]+\.insn\t2, 0x0001
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-8-norelax.norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-8-norelax.norvc.d
new file mode 100644
index 00000000000..aa8ae53c373
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-8-norelax.norvc.d
@@ -0,0 +1,20 @@
+#source: relax-align-8.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1 -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+00c000ef[ 	]+jal[ 	]+100c <foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+
+0+1008 <SHOULD_ALIGN_4_HERE>:
+[ 	]+1008:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+100c <foo>:
+[ 	]+100c:[ 	]+00008067[ 	]+ret
+[ 	]+1010:[ \t]+0001[ \t]+\.insn\t2, 0x0001
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-8-norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-8-norvc.d
new file mode 100644
index 00000000000..6141c176f04
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-8-norvc.d
@@ -0,0 +1,20 @@
+#source: relax-align-8.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+00c000ef[ 	]+jal[ 	]+100c <foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+
+0+1008 <SHOULD_ALIGN_4_HERE>:
+[ 	]+1008:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+100c <foo>:
+[ 	]+100c:[ 	]+00008067[ 	]+ret
+[ 	]+1010:[ \t]+0001[ \t]+\.insn\t2, 0x0001
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-8-relax.rvc.d b/ld/testsuite/ld-riscv-elf/relax-align-8-relax.rvc.d
new file mode 100644
index 00000000000..1c0cc208194
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-8-relax.rvc.d
@@ -0,0 +1,20 @@
+#source: relax-align-8.s
+#as: -mrelax -march=rv64gc
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+00c000ef[ 	]+jal[ 	]+100c <foo>
+[ 	]+1004:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+
+0+1008 <SHOULD_ALIGN_4_HERE>:
+[ 	]+1008:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+100c <foo>:
+[ 	]+100c:[ 	]+00008067[ 	]+ret
+[ 	]+1010:[ \t]+0001[ \t]+\.insn\t2, 0x0001
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-8.s b/ld/testsuite/ld-riscv-elf/relax-align-8.s
new file mode 100644
index 00000000000..da303f177d9
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-8.s
@@ -0,0 +1,25 @@
+        .text
+        .option relax
+        .balign 2
+        .global _start
+        .type _start, @function
+_start:
+	call foo
+	.option norvc
+	mul a0, a1, a4
+        .option push
+
+.ifdef NORELAX
+        .option norelax
+.endif
+.ifdef NORVC
+        .option norvc
+.endif
+        .balign 4
+SHOULD_ALIGN_4_HERE:
+        .word 0x12345678
+
+        .option pop
+
+foo:
+        ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-9-norelax.d b/ld/testsuite/ld-riscv-elf/relax-align-9-norelax.d
new file mode 100644
index 00000000000..e31229e3b67
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-9-norelax.d
@@ -0,0 +1,26 @@
+#source: relax-align-9.s
+#as: -mrelax -march=rv64gc -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01450513[ 	]+addi[ 	]+a0,a0,20[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+00000013[ 	]+nop
+[ 	]+100e:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+00008067[ 	]+ret
+[ 	]+1018:[ \t]+0001[ \t]+\.insn\t2, 0x0001
+[ 	]+101a:[ 	]+00000013[ 	]+nop
+[ 	]+101e:[ 	]+00000013[ 	]+nop
+[ 	]+1022:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-9-norelax.norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-9-norelax.norvc.d
new file mode 100644
index 00000000000..75b1d0c05c5
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-9-norelax.norvc.d
@@ -0,0 +1,26 @@
+#source: relax-align-9.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1 -defsym NORELAX=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01450513[ 	]+addi[ 	]+a0,a0,20[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+00000013[ 	]+nop
+[ 	]+100e:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+00008067[ 	]+ret
+[ 	]+1018:[ \t]+0001[ \t]+\.insn\t2, 0x0001
+[ 	]+101a:[ 	]+00000013[ 	]+nop
+[ 	]+101e:[ 	]+00000013[ 	]+nop
+[ 	]+1022:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-9-norvc.d b/ld/testsuite/ld-riscv-elf/relax-align-9-norvc.d
new file mode 100644
index 00000000000..754bbf6b34a
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-9-norvc.d
@@ -0,0 +1,26 @@
+#source: relax-align-9.s
+#as: -mrelax -march=rv64gc -defsym NORVC=1
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01450513[ 	]+addi[ 	]+a0,a0,20[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+00000013[ 	]+nop
+[ 	]+100e:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+00008067[ 	]+ret
+[ 	]+1018:[ \t]+0001[ \t]+\.insn\t2, 0x0001
+[ 	]+101a:[ 	]+00000013[ 	]+nop
+[ 	]+101e:[ 	]+00000013[ 	]+nop
+[ 	]+1022:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-9-relax.rvc.d b/ld/testsuite/ld-riscv-elf/relax-align-9-relax.rvc.d
new file mode 100644
index 00000000000..0fc833a489d
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-9-relax.rvc.d
@@ -0,0 +1,26 @@
+#source: relax-align-9.s
+#as: -mrelax -march=rv64gc
+#ld: -melf64lriscv -Trelax-align.ld
+#objdump: -d
+
+.*:[ ]+file format .*
+
+
+Disassembly of section .text:
+
+0+1000 <_start>:
+[ 	]+1000:[ 	]+6505[ 	]+lui[ 	]+a0,0x1
+[ 	]+1002:[ 	]+01450513[ 	]+addi[ 	]+a0,a0,20[ 	]+#.*<foo>
+[ 	]+1006:[ 	]+02e58533[ 	]+mul[ 	]+a0,a1,a4
+[ 	]+100a:[ 	]+00000013[ 	]+nop
+[ 	]+100e:[ 	]+0001[ 	]+\.insn[ 	]+2, 0x0001
+
+0+1010 <SHOULD_ALIGN_16_HERE>:
+[ 	]+1010:[ 	]+12345678[ 	]+\.word[ 	]+0x12345678
+
+0+1014 <foo>:
+[ 	]+1014:[ 	]+00008067[ 	]+ret
+[ 	]+1018:[ \t]+0001[ \t]+\.insn\t2, 0x0001
+[ 	]+101a:[ 	]+00000013[ 	]+nop
+[ 	]+101e:[ 	]+00000013[ 	]+nop
+[ 	]+1022:[ 	]+00000013[ 	]+nop
diff --git a/ld/testsuite/ld-riscv-elf/relax-align-9.s b/ld/testsuite/ld-riscv-elf/relax-align-9.s
new file mode 100644
index 00000000000..58ba9ed69a8
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align-9.s
@@ -0,0 +1,26 @@
+        .text
+        .option relax
+        .balign 2
+        .global _start
+        .type _start, @function
+_start:
+        lui a0, %hi(foo)
+        addi a0, a0, %lo(foo)
+        .option norvc
+	mul a0, a1, a4
+        .option push
+
+.ifdef NORELAX
+        .option norelax
+.endif
+.ifdef NORVC
+        .option norvc
+.endif
+        .balign 16
+SHOULD_ALIGN_16_HERE:
+        .word 0x12345678
+
+        .option pop
+
+foo:
+        ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-align.ld b/ld/testsuite/ld-riscv-elf/relax-align.ld
new file mode 100644
index 00000000000..b82cd89f6fc
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-align.ld
@@ -0,0 +1,6 @@
+ENTRY(_start)
+SECTIONS {
+	.text 0x0001000 : {
+		*(.text*)
+	}
+}
-- 
2.54.0



More information about the Binutils mailing list