[PATCH 4/5] RISC-V: reject unclosed ULEB128 relocation pairs

Harshit Kumar harshit7kr@gmail.com
Tue Nov 10 20:15:21 GMT 2026


The RISC-V psABI requires each R_RISCV_SET_ULEB128 relocation to pair
with an R_RISCV_SUB_ULEB128 relocation at the same offset.  Previously,
the linker silently ignored an unclosed SET relocation at section end.

Check whether a SET relocation remains open after the relocation loop
completes.  If uleb128_set_rel is non-NULL, report an error to enforce
psABI pairing rules.

bfd/
	PR ld/34987
	* elfnn-riscv.c (riscv_elf_relocate_section): Reject trailing
	unclosed R_RISCV_SET_ULEB128 relocation.

ld/
	PR ld/34987
	* testsuite/ld-riscv-elf/uleb128-unclosed.*: New test.
	* testsuite/ld-riscv-elf/uleb128-set-after-set.*: New test.
	* testsuite/ld-riscv-elf/uleb128-sub-no-set.*: New test.
	* testsuite/ld-riscv-elf/uleb128-mismatched-offset.*: New test.
	* testsuite/ld-riscv-elf/ld-riscv-elf.exp: Run them.

Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
 bfd/elfnn-riscv.c                                     | 11 +++++++++++
 ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp            |  4 ++++
 ld/testsuite/ld-riscv-elf/uleb128-mismatched-offset.d |  5 +++++
 ld/testsuite/ld-riscv-elf/uleb128-mismatched-offset.s | 10 ++++++++++
 ld/testsuite/ld-riscv-elf/uleb128-set-after-set.d     |  5 +++++
 ld/testsuite/ld-riscv-elf/uleb128-set-after-set.s     |  9 +++++++++
 ld/testsuite/ld-riscv-elf/uleb128-sub-no-set.d        |  5 +++++
 ld/testsuite/ld-riscv-elf/uleb128-sub-no-set.s        |  8 ++++++++
 ld/testsuite/ld-riscv-elf/uleb128-unclosed.d          |  5 +++++
 ld/testsuite/ld-riscv-elf/uleb128-unclosed.s          |  8 ++++++++
 10 files changed, 70 insertions(+)
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-mismatched-offset.d
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-mismatched-offset.s
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-set-after-set.d
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-set-after-set.s
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-sub-no-set.d
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-sub-no-set.s
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-unclosed.d
 create mode 100644 ld/testsuite/ld-riscv-elf/uleb128-unclosed.s

diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 34721be876..c06fa0f52c 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -4250,6 +4250,17 @@ riscv_elf_relocate_section (struct bfd_link_info *info,
       goto out;
     }
 
+  if (uleb128_set_rel != NULL)
+    {
+      _bfd_error_handler
+	/* xgettext:c-format */
+	(_("%pB: unclosed R_RISCV_SET_ULEB128 at offset %#" PRIx64
+	   " in section `%pA'"),
+	 input_bfd, (uint64_t) uleb128_set_rel->r_offset, input_section);
+      bfd_set_error (bfd_error_bad_value);
+      goto out;
+    }
+
   ret = (riscv_resolve_tlsdesc_lo_relocs (&pcrel_relocs)
 	 && riscv_resolve_pcrel_lo_relocs (&pcrel_relocs));
  out:
diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
index 9f7f453468..21ee6cc60b 100644
--- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
+++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
@@ -371,7 +371,11 @@ if [istarget "riscv*-*-*"] {
     run_dump_test "uleb128-reloc-bounds-control"
     run_dump_test "uleb128-unterminated"
     run_dump_test "uleb128-unterminated-control"
+    run_dump_test "uleb128-unclosed"
     run_dump_test "uleb128-multibyte-last"
+    run_dump_test "uleb128-sub-no-set"
+    run_dump_test "uleb128-mismatched-offset"
+    run_dump_test "uleb128-set-after-set"
     run_dump_test "tlsdesc-call-bounds"
     run_dump_test "tlsdesc-call-bounds-control"
     run_dump_test "tlsdesc-call-bounds-rv32"
diff --git a/ld/testsuite/ld-riscv-elf/uleb128-mismatched-offset.d b/ld/testsuite/ld-riscv-elf/uleb128-mismatched-offset.d
new file mode 100644
index 0000000000..9385cf1907
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/uleb128-mismatched-offset.d
@@ -0,0 +1,5 @@
+#name: RISC-V R_RISCV_SET/SUB_ULEB128 at different offsets
+#source: uleb128-mismatched-offset.s
+#ld: -e _start
+#error: .*: dangerous relocation: Mismatched R_RISCV_SUB_ULEB128.*
+#target: riscv*-*-*
diff --git a/ld/testsuite/ld-riscv-elf/uleb128-mismatched-offset.s b/ld/testsuite/ld-riscv-elf/uleb128-mismatched-offset.s
new file mode 100644
index 0000000000..e887d6d30a
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/uleb128-mismatched-offset.s
@@ -0,0 +1,10 @@
+	.text
+	.globl	_start
+_start:
+	.reloc	., R_RISCV_SET_ULEB128, foo0
+	.byte	0
+	.reloc	., R_RISCV_SUB_ULEB128, foo0
+	.byte	0
+	.data
+foo0:
+	.byte	0x93
diff --git a/ld/testsuite/ld-riscv-elf/uleb128-set-after-set.d b/ld/testsuite/ld-riscv-elf/uleb128-set-after-set.d
new file mode 100644
index 0000000000..2072a84117
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/uleb128-set-after-set.d
@@ -0,0 +1,5 @@
+#name: RISC-V consecutive R_RISCV_SET_ULEB128 relocations
+#source: uleb128-set-after-set.s
+#ld: -e _start
+#error: .*: dangerous relocation: Mismatched R_RISCV_SET_ULEB128.*
+#target: riscv*-*-*
diff --git a/ld/testsuite/ld-riscv-elf/uleb128-set-after-set.s b/ld/testsuite/ld-riscv-elf/uleb128-set-after-set.s
new file mode 100644
index 0000000000..fb27b061c7
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/uleb128-set-after-set.s
@@ -0,0 +1,9 @@
+	.text
+	.globl	_start
+_start:
+	.reloc	., R_RISCV_SET_ULEB128, foo0
+	.reloc	., R_RISCV_SET_ULEB128, foo0
+	.byte	0
+	.data
+foo0:
+	.byte	0x93
diff --git a/ld/testsuite/ld-riscv-elf/uleb128-sub-no-set.d b/ld/testsuite/ld-riscv-elf/uleb128-sub-no-set.d
new file mode 100644
index 0000000000..42465913e3
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/uleb128-sub-no-set.d
@@ -0,0 +1,5 @@
+#name: RISC-V R_RISCV_SUB_ULEB128 without SET
+#source: uleb128-sub-no-set.s
+#ld: -e _start
+#error: .*: dangerous relocation: Mismatched R_RISCV_SUB_ULEB128.*
+#target: riscv*-*-*
diff --git a/ld/testsuite/ld-riscv-elf/uleb128-sub-no-set.s b/ld/testsuite/ld-riscv-elf/uleb128-sub-no-set.s
new file mode 100644
index 0000000000..30cdd150eb
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/uleb128-sub-no-set.s
@@ -0,0 +1,8 @@
+	.text
+	.globl	_start
+_start:
+	.reloc	., R_RISCV_SUB_ULEB128, foo0
+	.byte	0
+	.data
+foo0:
+	.byte	0x93
diff --git a/ld/testsuite/ld-riscv-elf/uleb128-unclosed.d b/ld/testsuite/ld-riscv-elf/uleb128-unclosed.d
new file mode 100644
index 0000000000..29b374b796
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/uleb128-unclosed.d
@@ -0,0 +1,5 @@
+#name: RISC-V unclosed R_RISCV_SET_ULEB128
+#source: uleb128-unclosed.s
+#ld: -e _start
+#error: .*: unclosed R_RISCV_SET_ULEB128 .*
+#target: riscv*-*-*
diff --git a/ld/testsuite/ld-riscv-elf/uleb128-unclosed.s b/ld/testsuite/ld-riscv-elf/uleb128-unclosed.s
new file mode 100644
index 0000000000..7e0ee7838d
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/uleb128-unclosed.s
@@ -0,0 +1,8 @@
+	.text
+	.globl	_start
+_start:
+	.reloc	0, R_RISCV_SET_ULEB128, foo0
+	.fill	16, 1, 0
+	.data
+foo0:
+	.byte	0x93
-- 
2.55.0




More information about the Binutils mailing list