[PATCH 1/2] RISC-V: Reject conflicting Zcmt and Zcd extensions

Jiawei jiawei@iscas.ac.cn
Wed Nov 25 11:52:01 GMT 2026


Zcmt table jumps reuse c.fsdsp encodings from Zcd.  Reject an ISA that
enables both, including when C and D imply Zcd.  Applying the check
after attribute merging also catches conflicts between individually
valid input objects.

Validate the first input architecture before copying it to the output.
Older assemblers can have recorded both extensions in one object, in
which case there may be no attribute merge to validate the ISA.
Exercise such an object in the existing table-jump test and check GAS
and relocatable linking separately.

bfd/ChangeLog:

	* elfxx-riscv.c (riscv_parse_check_conflicts): Reject Zcmt with Zcd.
	(riscv_merge_attributes): Validate the first input ISA string before
	copying it.

gas/ChangeLog:

	* testsuite/gas/riscv/march-fail-zcmt-zcd-explicit.d: Test conflict.
	* testsuite/gas/riscv/march-fail-zcmt-zcd-implicit.d: Test implies.
	* testsuite/gas/riscv/march-fail-zcmt-zcd.l: Expect conflict.

ld/ChangeLog:

	* testsuite/ld-riscv-elf/ld-riscv-elf.exp: New case.
	* testsuite/ld-riscv-elf/table-jump-zcd-conflict.d: New expect.
	* testsuite/ld-riscv-elf/attr-merge-arch-failed-05.d: Test objects.
	* testsuite/ld-riscv-elf/attr-merge-arch-failed-05.l: Expect conflict.
	* testsuite/ld-riscv-elf/attr-merge-arch-failed-05a.s: Zcd input.
	* testsuite/ld-riscv-elf/attr-merge-arch-failed-05b.s: Zcmt input.
	* testsuite/ld-riscv-elf/table-jump-zcd-conflict.s: Emit combination.
---
 bfd/elfxx-riscv.c                             | 35 +++++++++++++++----
 .../gas/riscv/march-fail-zcmt-zcd-explicit.d  |  3 ++
 .../gas/riscv/march-fail-zcmt-zcd-implicit.d  |  3 ++
 gas/testsuite/gas/riscv/march-fail-zcmt-zcd.l |  2 ++
 .../ld-riscv-elf/attr-merge-arch-failed-05.d  |  5 +++
 .../ld-riscv-elf/attr-merge-arch-failed-05.l  |  2 ++
 .../ld-riscv-elf/attr-merge-arch-failed-05a.s |  2 ++
 .../ld-riscv-elf/attr-merge-arch-failed-05b.s |  2 ++
 ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp    |  1 +
 .../ld-riscv-elf/table-jump-zcd-conflict.d    | 13 +++----
 .../ld-riscv-elf/table-jump-zcd-conflict.s    | 17 +++++++++
 11 files changed, 70 insertions(+), 15 deletions(-)
 create mode 100644 gas/testsuite/gas/riscv/march-fail-zcmt-zcd-explicit.d
 create mode 100644 gas/testsuite/gas/riscv/march-fail-zcmt-zcd-implicit.d
 create mode 100644 gas/testsuite/gas/riscv/march-fail-zcmt-zcd.l
 create mode 100644 ld/testsuite/ld-riscv-elf/attr-merge-arch-failed-05.d
 create mode 100644 ld/testsuite/ld-riscv-elf/attr-merge-arch-failed-05.l
 create mode 100644 ld/testsuite/ld-riscv-elf/attr-merge-arch-failed-05a.s
 create mode 100644 ld/testsuite/ld-riscv-elf/attr-merge-arch-failed-05b.s
 create mode 100644 ld/testsuite/ld-riscv-elf/table-jump-zcd-conflict.s

diff --git a/bfd/elfxx-riscv.c b/bfd/elfxx-riscv.c
index b14fc88b024..eb0442b0453 100644
--- a/bfd/elfxx-riscv.c
+++ b/bfd/elfxx-riscv.c
@@ -2347,6 +2347,13 @@ riscv_parse_check_conflicts (riscv_parse_subset_t *rps)
 	(_("zcmp' is incompatible with `d' and `c', or `zcd' extension"));
       no_conflict = false;
     }
+  if (riscv_subset_supports (rps, "zcmt")
+      && riscv_subset_supports (rps, "zcd"))
+    {
+      rps->error_handler
+	(_("`zcmt' conflicts with the `c+d'/`zcd' extension"));
+      no_conflict = false;
+    }
   if (riscv_lookup_subset (rps->subset_list, "zcf", &subset)
       && xlen > 32)
     {
@@ -3985,13 +3992,32 @@ riscv_merge_attributes (bfd *ibfd, struct bfd_link_info *info,
   if (bfd_get_section_by_name (ibfd, sec_name) == NULL)
     return true;
 
-  if (!elf_known_obj_attributes_proc (obfd)[0].i)
+  in_attr = elf_known_obj_attributes_proc (ibfd);
+  out_attr = elf_known_obj_attributes_proc (obfd);
+
+  /* The first architecture string is copied without merging.  Check it
+     too, since it may come from an assembler that accepted conflicts.  */
+  if (in_attr[Tag_RISCV_arch].s && !out_attr[Tag_RISCV_arch].s)
+    {
+      unsigned int xlen;
+      riscv_subset_list_t subsets = { 0 };
+      riscv_parse_subset_t rps = {
+	.subset_list = &subsets,
+	.error_handler = _bfd_error_handler,
+	.xlen = &xlen
+      };
+      bool valid = riscv_parse_subset (&rps, in_attr[Tag_RISCV_arch].s);
+
+      riscv_release_subset_list (&subsets);
+      if (!valid)
+	return false;
+    }
+
+  if (!out_attr[0].i)
     {
       /* This is the first object.  Copy the attributes.  */
       _bfd_elf_copy_obj_attributes (ibfd, obfd);
 
-      out_attr = elf_known_obj_attributes_proc (obfd);
-
       /* Use the Tag_null value to indicate the attributes have been
 	 initialized.  */
       out_attr[0].i = 1;
@@ -4034,9 +4060,6 @@ riscv_merge_attributes (bfd *ibfd, struct bfd_link_info *info,
       return true;
     }
 
-  in_attr = elf_known_obj_attributes_proc (ibfd);
-  out_attr = elf_known_obj_attributes_proc (obfd);
-
   for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
     {
     switch (i)
diff --git a/gas/testsuite/gas/riscv/march-fail-zcmt-zcd-explicit.d b/gas/testsuite/gas/riscv/march-fail-zcmt-zcd-explicit.d
new file mode 100644
index 00000000000..1fc3f399584
--- /dev/null
+++ b/gas/testsuite/gas/riscv/march-fail-zcmt-zcd-explicit.d
@@ -0,0 +1,3 @@
+#as: -march=rv32i_zcd_zcmt
+#source: empty.s
+#error_output: march-fail-zcmt-zcd.l
diff --git a/gas/testsuite/gas/riscv/march-fail-zcmt-zcd-implicit.d b/gas/testsuite/gas/riscv/march-fail-zcmt-zcd-implicit.d
new file mode 100644
index 00000000000..4301f29c3cc
--- /dev/null
+++ b/gas/testsuite/gas/riscv/march-fail-zcmt-zcd-implicit.d
@@ -0,0 +1,3 @@
+#as: -march=rv64gc_zcmt
+#source: empty.s
+#error_output: march-fail-zcmt-zcd.l
diff --git a/gas/testsuite/gas/riscv/march-fail-zcmt-zcd.l b/gas/testsuite/gas/riscv/march-fail-zcmt-zcd.l
new file mode 100644
index 00000000000..6c69958125d
--- /dev/null
+++ b/gas/testsuite/gas/riscv/march-fail-zcmt-zcd.l
@@ -0,0 +1,2 @@
+.*Assembler messages:
+.*Error: `zcmt' conflicts with the `c\+d'/`zcd' extension
diff --git a/ld/testsuite/ld-riscv-elf/attr-merge-arch-failed-05.d b/ld/testsuite/ld-riscv-elf/attr-merge-arch-failed-05.d
new file mode 100644
index 00000000000..90ebd439a43
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/attr-merge-arch-failed-05.d
@@ -0,0 +1,5 @@
+#source: attr-merge-arch-failed-05a.s
+#source: attr-merge-arch-failed-05b.s
+#as: -mabi=ilp32
+#ld: -r -m[riscv_choose_ilp32_emul]
+#error_output: attr-merge-arch-failed-05.l
diff --git a/ld/testsuite/ld-riscv-elf/attr-merge-arch-failed-05.l b/ld/testsuite/ld-riscv-elf/attr-merge-arch-failed-05.l
new file mode 100644
index 00000000000..27aab2f2b0f
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/attr-merge-arch-failed-05.l
@@ -0,0 +1,2 @@
+.*: `zcmt' conflicts with the `c\+d'/`zcd' extension
+.*: failed to merge target specific data of file .*
diff --git a/ld/testsuite/ld-riscv-elf/attr-merge-arch-failed-05a.s b/ld/testsuite/ld-riscv-elf/attr-merge-arch-failed-05a.s
new file mode 100644
index 00000000000..474ab51faa4
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/attr-merge-arch-failed-05a.s
@@ -0,0 +1,2 @@
+	.attribute arch, "rv32i_zcd"
+	c.fsdsp ft0, 0(sp)
diff --git a/ld/testsuite/ld-riscv-elf/attr-merge-arch-failed-05b.s b/ld/testsuite/ld-riscv-elf/attr-merge-arch-failed-05b.s
new file mode 100644
index 00000000000..8df803b2dd0
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/attr-merge-arch-failed-05b.s
@@ -0,0 +1,2 @@
+	.attribute arch, "rv32i_zcmt"
+	cm.jt 0
diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
index 0adee8006e5..8949b9f30f1 100644
--- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
+++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
@@ -214,6 +214,7 @@ if [istarget "riscv*-*-*"] {
     run_dump_test "attr-merge-arch-failed-02"
     run_dump_test "attr-merge-arch-failed-03"
     run_dump_test "attr-merge-arch-failed-04"
+    run_dump_test "attr-merge-arch-failed-05"
     run_dump_test "attr-merge-strict-align-01"
     run_dump_test "attr-merge-strict-align-02"
     run_dump_test "attr-merge-strict-align-03"
diff --git a/ld/testsuite/ld-riscv-elf/table-jump-zcd-conflict.d b/ld/testsuite/ld-riscv-elf/table-jump-zcd-conflict.d
index a026a6e173b..05ea8303263 100644
--- a/ld/testsuite/ld-riscv-elf/table-jump-zcd-conflict.d
+++ b/ld/testsuite/ld-riscv-elf/table-jump-zcd-conflict.d
@@ -1,9 +1,4 @@
-#source: table-jump.s
-#as: -march=rv64imafd_zicsr_zifencei_zca_zcd_zcmt --defsym TEST_CM_JT_JALT=1
-#ld: -Ttable-jump.ld --relax-tbljal
-#readelf: -SWs
-
-#failif
-#...
-.*(__jvt_base\$|\.riscv\.jvt).*
-#pass
+#source: table-jump-zcd-conflict.s
+#as: -march=rv64i -mno-arch-attr
+#ld: -m[riscv_choose_lp64_emul] -Ttable-jump.ld --relax-tbljal
+#error_output: attr-merge-arch-failed-05.l
diff --git a/ld/testsuite/ld-riscv-elf/table-jump-zcd-conflict.s b/ld/testsuite/ld-riscv-elf/table-jump-zcd-conflict.s
new file mode 100644
index 00000000000..0f15814e618
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/table-jump-zcd-conflict.s
@@ -0,0 +1,17 @@
+# Older assemblers could emit this combination in Tag_RISCV_arch.
+	.section .riscv.attributes, "", %0x70000003
+	.byte	0x41
+.Lvendor:
+	.4byte	.Lend - .Lvendor
+	.asciz	"riscv"
+.Lfile:
+	.byte	1
+	.4byte	.Lend - .Lfile
+	.byte	5
+	.asciz	"rv64i2p1_f2p2_d2p2_zicsr2p0_zca1p0_zcd1p0_zcmt1p0"
+.Lend:
+
+	.text
+	.globl	_start
+_start:
+	nop

base-commit: 9e9d08ed5bc2237478299335351954dc50be5ffa



More information about the Binutils mailing list