[PATCH 2/5] RISC-V: reject relocation offsets outside the section
Harshit Kumar
harshit7kr@gmail.com
Tue Nov 10 20:15:19 GMT 2026
riscv_elf_relocate_section applies relocations without checking that
the relocated field lies inside the section, so a relocation with a
bad r_offset reads and writes past the section contents.
Add riscv_reloc_field_in_range and check each relocation field against
bfd_get_section_limit_octets before the relocation is applied. The
generic bfd_reloc_offset_in_range is not used because it requires a
non-zero HOWTO size, and several RISC-V checks need a span that the
HOWTO does not describe.
R_RISCV_TLSDESC_CALL is one of those. Its HOWTO size is zero, but in
unrelaxed links riscv_resolve_tlsdesc_lo_relocs rewrites the call
instruction with bfd_putl32. Check the full RISCV_INSN_LEN span for
it. Without this, a TLSDESC_CALL at the end of the section makes
AddressSanitizer report a heap-buffer-overflow write in bfd_putl32,
called from riscv_resolve_tlsdesc_lo_relocs.
Skip R_RISCV_NONE before the check. Relaxation changes a relocation
that it no longer needs to R_RISCV_NONE, and the offset of a retired
relocation can lie past the end of the shrunken section. It needs no
processing, so range checking it would reject valid links.
The check runs before the bfd_link_relocatable test, so ld -r rejects
such input too.
riscv_bad_reloc_offset takes a RELAX argument that selects the wording
used during relaxation. This patch only uses the final-link wording;
a later patch uses the other.
The tests use .option norvc so that instruction lengths are fixed.
riscv-reloc-offset.exp assembles an object and then patches r_offset to
values that GAS cannot produce.
bfd/
PR ld/34987
* elfnn-riscv.c (RISCV_INSN_LEN): Define.
(riscv_bad_reloc_offset): New function.
(riscv_reloc_field_in_range): New function.
(riscv_elf_relocate_section): Skip R_RISCV_NONE. Check that
relocation fields, including the R_RISCV_TLSDESC_CALL
instruction, lie within the section.
ld/
PR ld/34987
* testsuite/ld-riscv-elf/riscv-reloc-offset.exp: New test driver.
* testsuite/ld-riscv-elf/huge-offset.s: New file.
* testsuite/ld-riscv-elf/call-bounds-norelax*: New tests.
* testsuite/ld-riscv-elf/call-plt-bounds-norelax*: New tests.
* testsuite/ld-riscv-elf/set8-reloc-bounds*: New tests.
* testsuite/ld-riscv-elf/set32-reloc-bounds*: New tests.
* testsuite/ld-riscv-elf/tlsdesc-call-bounds*: New tests.
* testsuite/ld-riscv-elf/relax-none-bounds.*: New test.
* testsuite/ld-riscv-elf/ld-riscv-elf.exp: Run them.
Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
bfd/elfnn-riscv.c | 79 +++++++-
.../call-bounds-norelax-control.d | 13 ++
.../ld-riscv-elf/call-bounds-norelax-fail.d | 5 +
.../ld-riscv-elf/call-bounds-norelax-fail.s | 7 +
.../call-bounds-norelax-rv32-fail.d | 6 +
.../call-bounds-norelax-rv32-pass.d | 14 ++
.../ld-riscv-elf/call-bounds-norelax.s | 8 +
.../call-plt-bounds-norelax-control.d | 13 ++
.../call-plt-bounds-norelax-fail.d | 5 +
.../call-plt-bounds-norelax-fail.s | 7 +
.../ld-riscv-elf/call-plt-bounds-norelax.s | 6 +
ld/testsuite/ld-riscv-elf/huge-offset.s | 8 +
ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp | 16 ++
ld/testsuite/ld-riscv-elf/relax-none-bounds.d | 15 ++
ld/testsuite/ld-riscv-elf/relax-none-bounds.s | 12 ++
.../ld-riscv-elf/riscv-reloc-offset.exp | 187 ++++++++++++++++++
.../ld-riscv-elf/set32-reloc-bounds-control.d | 10 +
.../ld-riscv-elf/set32-reloc-bounds-pass.s | 9 +
.../ld-riscv-elf/set32-reloc-bounds.d | 5 +
.../ld-riscv-elf/set32-reloc-bounds.s | 9 +
.../ld-riscv-elf/set8-reloc-bounds-control.d | 10 +
.../ld-riscv-elf/set8-reloc-bounds-pass.s | 9 +
ld/testsuite/ld-riscv-elf/set8-reloc-bounds.d | 5 +
ld/testsuite/ld-riscv-elf/set8-reloc-bounds.s | 8 +
.../tlsdesc-call-bounds-control.d | 15 ++
.../ld-riscv-elf/tlsdesc-call-bounds-pass.s | 18 ++
.../tlsdesc-call-bounds-rv32-control.d | 16 ++
.../tlsdesc-call-bounds-rv32-pass.s | 18 ++
.../ld-riscv-elf/tlsdesc-call-bounds-rv32.d | 6 +
.../ld-riscv-elf/tlsdesc-call-bounds-rv32.s | 17 ++
.../ld-riscv-elf/tlsdesc-call-bounds.d | 5 +
.../ld-riscv-elf/tlsdesc-call-bounds.s | 17 ++
32 files changed, 576 insertions(+), 2 deletions(-)
create mode 100644 ld/testsuite/ld-riscv-elf/call-bounds-norelax-control.d
create mode 100644 ld/testsuite/ld-riscv-elf/call-bounds-norelax-fail.d
create mode 100644 ld/testsuite/ld-riscv-elf/call-bounds-norelax-fail.s
create mode 100644 ld/testsuite/ld-riscv-elf/call-bounds-norelax-rv32-fail.d
create mode 100644 ld/testsuite/ld-riscv-elf/call-bounds-norelax-rv32-pass.d
create mode 100644 ld/testsuite/ld-riscv-elf/call-bounds-norelax.s
create mode 100644 ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax-control.d
create mode 100644 ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax-fail.d
create mode 100644 ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax-fail.s
create mode 100644 ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax.s
create mode 100644 ld/testsuite/ld-riscv-elf/huge-offset.s
create mode 100644 ld/testsuite/ld-riscv-elf/relax-none-bounds.d
create mode 100644 ld/testsuite/ld-riscv-elf/relax-none-bounds.s
create mode 100644 ld/testsuite/ld-riscv-elf/riscv-reloc-offset.exp
create mode 100644 ld/testsuite/ld-riscv-elf/set32-reloc-bounds-control.d
create mode 100644 ld/testsuite/ld-riscv-elf/set32-reloc-bounds-pass.s
create mode 100644 ld/testsuite/ld-riscv-elf/set32-reloc-bounds.d
create mode 100644 ld/testsuite/ld-riscv-elf/set32-reloc-bounds.s
create mode 100644 ld/testsuite/ld-riscv-elf/set8-reloc-bounds-control.d
create mode 100644 ld/testsuite/ld-riscv-elf/set8-reloc-bounds-pass.s
create mode 100644 ld/testsuite/ld-riscv-elf/set8-reloc-bounds.d
create mode 100644 ld/testsuite/ld-riscv-elf/set8-reloc-bounds.s
create mode 100644 ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-control.d
create mode 100644 ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-pass.s
create mode 100644 ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32-control.d
create mode 100644 ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32-pass.s
create mode 100644 ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32.d
create mode 100644 ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32.s
create mode 100644 ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds.d
create mode 100644 ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds.s
diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 5dd09fed8a..b96586f522 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -2454,6 +2454,66 @@ riscv_global_pointer_value (struct bfd_link_info *info)
return h->u.def.value + sec_addr (h->u.def.section);
}
+/* Standard 32-bit RISC-V instruction length in octets. */
+#define RISCV_INSN_LEN 4
+
+/* Report a relocation field that does not lie inside its section.
+ Parameter RELAX selects diagnostics when checking relaxation passes
+ rather than the final link. */
+
+static void
+riscv_bad_reloc_offset (bfd *abfd, asection *sec, bfd_vma offset,
+ bfd_vma size, bfd_vma sec_limit, bool relax)
+{
+ if (relax)
+ {
+ if (offset > sec_limit)
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("%pB: bad relocation offset during relaxation (%#" PRIx64
+ " > %#" PRIx64 ") in section `%pA'"),
+ abfd, (uint64_t) offset, (uint64_t) sec_limit, sec);
+ else
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("%pB: bad relocation offset during relaxation (%#" PRIx64
+ " + %#" PRIx64 " > %#" PRIx64 ") in section `%pA'"),
+ abfd, (uint64_t) offset, (uint64_t) size,
+ (uint64_t) sec_limit, sec);
+ }
+ else
+ {
+ if (offset > sec_limit)
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("%pB: bad reloc offset (%#" PRIx64
+ " > %#" PRIx64 ") for section `%pA'"),
+ abfd, (uint64_t) offset, (uint64_t) sec_limit, sec);
+ else
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("%pB: bad reloc offset (%#" PRIx64
+ " + %#" PRIx64 " > %#" PRIx64 ") for section `%pA'"),
+ abfd, (uint64_t) offset, (uint64_t) size,
+ (uint64_t) sec_limit, sec);
+ }
+ bfd_set_error (bfd_error_bad_value);
+}
+
+/* Return true if a field of SIZE bytes at OFFSET lies inside SEC. */
+
+static bool
+riscv_reloc_field_in_range (bfd *abfd, asection *sec, bfd_vma offset,
+ bfd_vma size, bfd_vma sec_limit)
+{
+ if (offset > sec_limit || size > sec_limit - offset)
+ {
+ riscv_bad_reloc_offset (abfd, sec, offset, size, sec_limit, false);
+ return false;
+ }
+ return true;
+}
+
/* Emplace a static relocation. */
static bfd_reloc_status_type
@@ -3082,10 +3142,25 @@ riscv_elf_relocate_section (struct bfd_link_info *info,
bool resolved_to_zero;
bool via_plt = false;
bool relative_got = false;
-
- if (howto == NULL)
+ bfd_vma reloc_size;
+ bfd_vma sec_limit;
+
+ /* Relaxation retires a relocation by rewriting it to R_RISCV_NONE.
+ The offset of a retired relocation can lie past the end of the
+ shrunken section, and it needs no processing, so skip it before
+ any bounds check. */
+ if (howto == NULL || r_type == R_RISCV_NONE)
continue;
+ reloc_size = (r_type == R_RISCV_TLSDESC_CALL
+ ? RISCV_INSN_LEN : bfd_get_reloc_size (howto));
+ /* General relocations write into the section contents, so the field
+ must lie within the section limit. */
+ sec_limit = bfd_get_section_limit_octets (input_bfd, input_section);
+ if (!riscv_reloc_field_in_range (input_bfd, input_section,
+ rel->r_offset, reloc_size, sec_limit))
+ goto out;
+
/* This is a final link. */
r_symndx = ELFNN_R_SYM (rel->r_info);
h = NULL;
diff --git a/ld/testsuite/ld-riscv-elf/call-bounds-norelax-control.d b/ld/testsuite/ld-riscv-elf/call-bounds-norelax-control.d
new file mode 100644
index 0000000000..bb58e9a706
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/call-bounds-norelax-control.d
@@ -0,0 +1,13 @@
+#name: RISC-V unrelaxed call within section (control)
+#source: call-bounds-norelax.s
+#ld: --no-relax -e _start
+#objdump: -d
+#target: riscv*-*-*
+
+.*: file format elf.*-.*riscv.*
+
+Disassembly of section \.text:
+
+[0-9a-f]+ <_start>:
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+auipc[ ]+ra,0x0
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+jalr[ ]+8\(ra\) # [0-9a-f]+ <target>
diff --git a/ld/testsuite/ld-riscv-elf/call-bounds-norelax-fail.d b/ld/testsuite/ld-riscv-elf/call-bounds-norelax-fail.d
new file mode 100644
index 0000000000..a5f3d173c4
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/call-bounds-norelax-fail.d
@@ -0,0 +1,5 @@
+#name: RISC-V unrelaxed R_RISCV_CALL past section boundary
+#source: call-bounds-norelax-fail.s
+#ld: --no-relax -e _start
+#error: .*: bad reloc offset \(0 \+ 0x8 > 0x4\) for section `\.text'
+#target: riscv*-*-*
diff --git a/ld/testsuite/ld-riscv-elf/call-bounds-norelax-fail.s b/ld/testsuite/ld-riscv-elf/call-bounds-norelax-fail.s
new file mode 100644
index 0000000000..8bfab5824f
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/call-bounds-norelax-fail.s
@@ -0,0 +1,7 @@
+ .option norvc
+ .text
+ .globl _start
+_start:
+ nop
+ .reloc 0, R_RISCV_CALL, target
+target:
diff --git a/ld/testsuite/ld-riscv-elf/call-bounds-norelax-rv32-fail.d b/ld/testsuite/ld-riscv-elf/call-bounds-norelax-rv32-fail.d
new file mode 100644
index 0000000000..321b50fa4b
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/call-bounds-norelax-rv32-fail.d
@@ -0,0 +1,6 @@
+#name: RISC-V RV32 unrelaxed R_RISCV_CALL past section boundary
+#source: call-bounds-norelax-fail.s
+#as: -march=rv32i -mabi=ilp32
+#ld: -melf32lriscv --no-relax -e _start
+#error: .*: bad reloc offset \(0 \+ 0x8 > 0x4\) for section `\.text'
+#target: riscv*-*-*
diff --git a/ld/testsuite/ld-riscv-elf/call-bounds-norelax-rv32-pass.d b/ld/testsuite/ld-riscv-elf/call-bounds-norelax-rv32-pass.d
new file mode 100644
index 0000000000..c09595c0be
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/call-bounds-norelax-rv32-pass.d
@@ -0,0 +1,14 @@
+#name: RISC-V RV32 unrelaxed R_RISCV_CALL within section (control)
+#source: call-bounds-norelax.s
+#as: -march=rv32i -mabi=ilp32
+#ld: -melf32lriscv --no-relax -e _start
+#objdump: -d
+#target: riscv*-*-*
+
+.*: file format elf32-.*riscv.*
+
+Disassembly of section \.text:
+
+[0-9a-f]+ <_start>:
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+auipc[ ]+ra,0x0
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+jalr[ ]+8\(ra\) # [0-9a-f]+ <target>
diff --git a/ld/testsuite/ld-riscv-elf/call-bounds-norelax.s b/ld/testsuite/ld-riscv-elf/call-bounds-norelax.s
new file mode 100644
index 0000000000..53fcc0696d
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/call-bounds-norelax.s
@@ -0,0 +1,8 @@
+ .option norvc
+ .text
+ .globl _start
+_start:
+ auipc ra, 0
+ jalr ra, 0(ra)
+ .reloc 0, R_RISCV_CALL, target
+target:
diff --git a/ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax-control.d b/ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax-control.d
new file mode 100644
index 0000000000..1dd5403ba5
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax-control.d
@@ -0,0 +1,13 @@
+#name: RISC-V unrelaxed R_RISCV_CALL_PLT within section (control)
+#source: call-plt-bounds-norelax.s
+#ld: --no-relax -e _start
+#objdump: -d
+#target: riscv*-*-*
+
+.*: file format elf.*-.*riscv.*
+
+Disassembly of section \.text:
+
+[0-9a-f]+ <_start>:
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+auipc[ ]+ra,0x0
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+jalr[ ]+8\(ra\) # [0-9a-f]+ <target>
diff --git a/ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax-fail.d b/ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax-fail.d
new file mode 100644
index 0000000000..9130090f09
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax-fail.d
@@ -0,0 +1,5 @@
+#name: RISC-V unrelaxed R_RISCV_CALL_PLT past section boundary
+#source: call-plt-bounds-norelax-fail.s
+#ld: --no-relax -e _start
+#error: .*: bad reloc offset \(0 \+ 0x8 > 0x4\) for section `\.text'
+#target: riscv*-*-*
diff --git a/ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax-fail.s b/ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax-fail.s
new file mode 100644
index 0000000000..1a85a9de41
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax-fail.s
@@ -0,0 +1,7 @@
+ .option norvc
+ .text
+ .globl _start
+_start:
+ nop
+ .reloc 0, R_RISCV_CALL_PLT, target
+target:
diff --git a/ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax.s b/ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax.s
new file mode 100644
index 0000000000..5cd79d484a
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/call-plt-bounds-norelax.s
@@ -0,0 +1,6 @@
+ .option norvc
+ .text
+ .globl _start
+_start:
+ call target
+target:
diff --git a/ld/testsuite/ld-riscv-elf/huge-offset.s b/ld/testsuite/ld-riscv-elf/huge-offset.s
new file mode 100644
index 0000000000..73d5e7ea9c
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/huge-offset.s
@@ -0,0 +1,8 @@
+ .text
+ .globl _start
+_start:
+ .reloc 0, R_RISCV_SET8, foo
+ .fill 16, 1, 0
+ .data
+foo:
+ .byte 0x93
diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
index ad12f8d63b..4e3a227800 100644
--- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
+++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
@@ -363,6 +363,22 @@ if [istarget "riscv*-*-*"] {
run_dump_test_ifunc "ifunc-overwrite" rv64 pie
run_dump_test_ifunc "ifunc-overwrite" rv64 pic
+ run_dump_test "set8-reloc-bounds"
+ run_dump_test "set8-reloc-bounds-control"
+ run_dump_test "set32-reloc-bounds"
+ run_dump_test "set32-reloc-bounds-control"
+ run_dump_test "tlsdesc-call-bounds"
+ run_dump_test "tlsdesc-call-bounds-control"
+ run_dump_test "tlsdesc-call-bounds-rv32"
+ run_dump_test "tlsdesc-call-bounds-rv32-control"
+ run_dump_test "relax-none-bounds"
+ run_dump_test "call-bounds-norelax-fail"
+ run_dump_test "call-bounds-norelax-control"
+ run_dump_test "call-bounds-norelax-rv32-fail"
+ run_dump_test "call-bounds-norelax-rv32-pass"
+ run_dump_test "call-plt-bounds-norelax-fail"
+ run_dump_test "call-plt-bounds-norelax-control"
+
# TODO: Make the following tests work under RV32.
if [istarget "riscv32-*-*"] {
return
diff --git a/ld/testsuite/ld-riscv-elf/relax-none-bounds.d b/ld/testsuite/ld-riscv-elf/relax-none-bounds.d
new file mode 100644
index 0000000000..eda95ec0e0
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-none-bounds.d
@@ -0,0 +1,15 @@
+#name: RISC-V relaxed section with R_RISCV_NONE past shrunken section
+#source: relax-none-bounds.s
+#ld: --relax -e _start
+#objdump: -d
+#target: riscv*-*-*
+
+.*: file format elf.*-.*riscv.*
+
+Disassembly of section \.text:
+
+[0-9a-f]+ <_start>:
+[ ]+[0-9a-f]+:[ ]+004000ef[ ]+jal[ ]+[0-9a-f]+ <target>
+
+[0-9a-f]+ <target>:
+[ ]+[0-9a-f]+:[ ]+00008067[ ]+ret
diff --git a/ld/testsuite/ld-riscv-elf/relax-none-bounds.s b/ld/testsuite/ld-riscv-elf/relax-none-bounds.s
new file mode 100644
index 0000000000..def76caf13
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/relax-none-bounds.s
@@ -0,0 +1,12 @@
+ .section .text.target, "ax", @progbits
+ .option relax
+target:
+ ret
+
+ .section .text, "ax", @progbits
+ .option relax
+ .globl _start
+ .type _start, @function
+_start:
+ call target
+ .reloc 8, R_RISCV_NONE, 0
diff --git a/ld/testsuite/ld-riscv-elf/riscv-reloc-offset.exp b/ld/testsuite/ld-riscv-elf/riscv-reloc-offset.exp
new file mode 100644
index 0000000000..d5e95941cb
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/riscv-reloc-offset.exp
@@ -0,0 +1,187 @@
+# Expect script for the RISC-V relocation offset bounds test.
+# Copyright (C) 2026 Free Software Foundation, Inc.
+#
+# This file is part of the GNU Binutils.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301, USA.
+
+# The test patches binary object files on the host, so it requires a local host.
+if { [is_remote host] } {
+ return
+}
+
+if { ![istarget "riscv*-*-*"] } {
+ return
+}
+
+global tmpdir
+if { ![info exists tmpdir] } {
+ set tmpdir "tmpdir"
+}
+
+set obj "$tmpdir/huge-offset.o"
+set bin "$tmpdir/huge-offset.out"
+
+proc riscv_cleanup_files {} {
+ global obj bin
+ remote_file host delete $obj $bin
+}
+
+riscv_cleanup_files
+
+# Return file offset, pack format, and entry size of the first relocation in
+# .rela.text of OBJ, or empty list on failure.
+
+proc riscv_rela_info {obj} {
+ global READELF
+
+ set output [run_host_cmd $READELF "-SW $obj"]
+ if { ![regexp {\.rela\.text[ \t]+RELA[ \t]+[0-9a-fA-F]+[ \t]+([0-9a-fA-F]+)[ \t]+([0-9a-fA-F]+)[ \t]+([0-9a-fA-F]+)} $output -> rela_off rela_size rela_entsize] } {
+ return {}
+ }
+ set rela_off [expr 0x$rela_off]
+ set rela_size [expr 0x$rela_size]
+ set rela_entsize [expr 0x$rela_entsize]
+ if { $rela_size == 0 || $rela_entsize == 0 } {
+ return {}
+ }
+
+ set output [run_host_cmd $READELF "-h $obj"]
+ set is_big [regexp {big endian} $output]
+ if { [regexp {Class:[ \t]+ELF64} $output] } {
+ set fmt [expr { $is_big ? "W" : "w" }]
+ } elseif { [regexp {Class:[ \t]+ELF32} $output] } {
+ set fmt [expr { $is_big ? "I" : "i" }]
+ } else {
+ return {}
+ }
+
+ return [list $rela_off $fmt $rela_entsize]
+}
+
+# Patch the offset of the first relocation in .rela.text of OBJ.
+
+proc riscv_patch_reloc_offset {obj offset} {
+ set info [riscv_rela_info $obj]
+ if { [llength $info] != 3 } {
+ return 0
+ }
+ lassign $info rela_off fmt entsize
+
+ set f [open $obj r+]
+ fconfigure $f -translation binary
+ seek $f $rela_off
+ puts -nonewline $f [binary format $fmt $offset]
+ close $f
+ return 1
+}
+
+proc riscv_check_abnormal_termination {testname output} {
+ if { [regexp "(core dumped|Aborted|double free|Segmentation fault)" $output] } {
+ verbose -log "$output"
+ fail "$testname: abnormal termination in linker"
+ return 1
+ }
+ return 0
+}
+
+# Run the linker on OBJ, verifying it fails with DIAG_PATTERN without crashing
+# or producing an output file.
+
+proc riscv_expect_reject {testname bin ld_cmd_flags diag_pattern} {
+ global ld LDFLAGS
+ remote_file host delete $bin
+ set output [run_host_cmd $ld "$LDFLAGS $ld_cmd_flags"]
+ if { [riscv_check_abnormal_termination $testname $output] } {
+ return
+ }
+ if { ![regexp $diag_pattern $output] } {
+ verbose -log "$output"
+ fail "$testname: expected diagnostic"
+ } elseif { [remote_file host exists $bin] } {
+ fail "$testname: output file created"
+ } else {
+ pass "$testname: rejected with the diagnostic"
+ }
+}
+
+# Run the linker on OBJ, verifying it succeeds without crashing and creates BIN.
+
+proc riscv_expect_link {testname bin ld_cmd_flags} {
+ global ld LDFLAGS
+ remote_file host delete $bin
+ set output [run_host_cmd $ld "$LDFLAGS $ld_cmd_flags"]
+ if { [riscv_check_abnormal_termination $testname $output] } {
+ return
+ }
+ if { ![remote_file host exists $bin] } {
+ verbose -log "$output"
+ fail "$testname: expected success"
+ } else {
+ pass "$testname: linked"
+ }
+}
+
+set src "$srcdir/$subdir/huge-offset.s"
+
+if { ![default_ld_assemble $AS "" $src $obj] } {
+ fail "RISC-V relocation offset past the section (assemble)"
+} else {
+ # Large positive offset that exceeds section boundaries and tests arithmetic
+ # overflow: on ELF64, 0x7fffffffffff8b11 overflows 64-bit addition when added
+ # to relocation size while remaining positive in signed representation.
+ # On ELF32, 0x7ffffff0 does the same for 32-bit addition.
+ set output [run_host_cmd $READELF "-h $obj"]
+ if { [regexp {Class:[ \t]+ELF64} $output] } {
+ set huge 0x7fffffffffff8b11
+ } else {
+ set huge 0x7ffffff0
+ }
+
+ # Out-of-bounds offset must be rejected.
+ if { ![riscv_patch_reloc_offset $obj $huge] } {
+ fail "RISC-V relocation offset past the section (patch)"
+ } else {
+ riscv_expect_reject "RISC-V relocation offset past the section" \
+ $bin "-e _start -o $bin $obj" "bad reloc offset"
+ }
+
+ # Relocation at the last valid byte must be accepted.
+ if { ![riscv_patch_reloc_offset $obj 15] } {
+ fail "RISC-V relocation at the last byte (patch)"
+ } else {
+ riscv_expect_link "RISC-V relocation at the last byte" \
+ $bin "-e _start -o $bin $obj"
+ }
+
+ # Relocatable link (-r) must also reject the out-of-bounds relocation.
+ if { ![riscv_patch_reloc_offset $obj $huge] } {
+ fail "RISC-V relocation offset past the section (-r patch)"
+ } else {
+ riscv_expect_reject "RISC-V relocation offset past the section (-r)" \
+ $bin "-r -o $bin $obj" "bad reloc offset"
+ }
+
+ # Relocatable link (-r) at the last valid byte must be accepted.
+ if { ![riscv_patch_reloc_offset $obj 15] } {
+ fail "RISC-V relocation at the last byte (-r control patch)"
+ } else {
+ riscv_expect_link "RISC-V relocation at the last byte (-r)" \
+ $bin "-r -o $bin $obj"
+ }
+}
+
+riscv_cleanup_files
diff --git a/ld/testsuite/ld-riscv-elf/set32-reloc-bounds-control.d b/ld/testsuite/ld-riscv-elf/set32-reloc-bounds-control.d
new file mode 100644
index 0000000000..eacac27eda
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/set32-reloc-bounds-control.d
@@ -0,0 +1,10 @@
+#name: RISC-V R_RISCV_SET32 relocation at the last four bytes
+#source: set32-reloc-bounds-pass.s
+#ld: -e _start
+#readelf: -h
+#target: riscv*-*-*
+
+ELF Header:
+ Magic: 7f 45 4c 46 .*
+ Class: ELF(32|64)
+#pass
diff --git a/ld/testsuite/ld-riscv-elf/set32-reloc-bounds-pass.s b/ld/testsuite/ld-riscv-elf/set32-reloc-bounds-pass.s
new file mode 100644
index 0000000000..9c389a0efc
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/set32-reloc-bounds-pass.s
@@ -0,0 +1,9 @@
+ .text
+ .globl _start
+_start:
+ .fill 12, 1, 0
+ .reloc ., R_RISCV_SET32, foo0
+ .word 0
+ .data
+foo0:
+ .word 0xdeadbeef
diff --git a/ld/testsuite/ld-riscv-elf/set32-reloc-bounds.d b/ld/testsuite/ld-riscv-elf/set32-reloc-bounds.d
new file mode 100644
index 0000000000..d1049d5fe7
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/set32-reloc-bounds.d
@@ -0,0 +1,5 @@
+#name: RISC-V R_RISCV_SET32 relocation field straddles the section end
+#source: set32-reloc-bounds.s
+#ld: -e _start
+#error: .*: bad reloc offset \(0xe \+ 0x4 > 0x10\) for section `\.text'
+#target: riscv*-*-*
diff --git a/ld/testsuite/ld-riscv-elf/set32-reloc-bounds.s b/ld/testsuite/ld-riscv-elf/set32-reloc-bounds.s
new file mode 100644
index 0000000000..b1d18b018e
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/set32-reloc-bounds.s
@@ -0,0 +1,9 @@
+ .text
+ .globl _start
+_start:
+ .fill 14, 1, 0
+ .reloc ., R_RISCV_SET32, foo0
+ .fill 2, 1, 0
+ .data
+foo0:
+ .word 0xdeadbeef
diff --git a/ld/testsuite/ld-riscv-elf/set8-reloc-bounds-control.d b/ld/testsuite/ld-riscv-elf/set8-reloc-bounds-control.d
new file mode 100644
index 0000000000..ab562dfd5c
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/set8-reloc-bounds-control.d
@@ -0,0 +1,10 @@
+#name: RISC-V R_RISCV_SET8 relocation at the last byte
+#source: set8-reloc-bounds-pass.s
+#ld: -e _start
+#readelf: -h
+#target: riscv*-*-*
+
+ELF Header:
+ Magic: 7f 45 4c 46 .*
+ Class: ELF(32|64)
+#pass
diff --git a/ld/testsuite/ld-riscv-elf/set8-reloc-bounds-pass.s b/ld/testsuite/ld-riscv-elf/set8-reloc-bounds-pass.s
new file mode 100644
index 0000000000..faba71211c
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/set8-reloc-bounds-pass.s
@@ -0,0 +1,9 @@
+ .text
+ .globl _start
+_start:
+ .fill 15, 1, 0
+ .reloc ., R_RISCV_SET8, foo0
+ .byte 0
+ .data
+foo0:
+ .byte 0x93
diff --git a/ld/testsuite/ld-riscv-elf/set8-reloc-bounds.d b/ld/testsuite/ld-riscv-elf/set8-reloc-bounds.d
new file mode 100644
index 0000000000..2fab06efbd
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/set8-reloc-bounds.d
@@ -0,0 +1,5 @@
+#name: RISC-V R_RISCV_SET8 relocation offset at the end of the section
+#source: set8-reloc-bounds.s
+#ld: -e _start
+#error: .*: bad reloc offset \(0x10 \+ 0x1 > 0x10\) for section `\.text'
+#target: riscv*-*-*
diff --git a/ld/testsuite/ld-riscv-elf/set8-reloc-bounds.s b/ld/testsuite/ld-riscv-elf/set8-reloc-bounds.s
new file mode 100644
index 0000000000..070ae241ee
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/set8-reloc-bounds.s
@@ -0,0 +1,8 @@
+ .text
+ .globl _start
+_start:
+ .fill 16, 1, 0
+ .reloc ., R_RISCV_SET8, foo0
+ .data
+foo0:
+ .byte 0x93
diff --git a/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-control.d b/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-control.d
new file mode 100644
index 0000000000..9cf7fed12e
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-control.d
@@ -0,0 +1,15 @@
+#name: RISC-V R_RISCV_TLSDESC_CALL in the last four bytes (smoke control)
+#source: tlsdesc-call-bounds-pass.s
+#ld: -no-pie --no-relax
+#objdump: -d
+#target: riscv64*-*-*
+
+.*: file format elf.*-.*riscv.*
+
+Disassembly of section \.text:
+
+[0-9a-f]+ <_start>:
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+nop
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+nop
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+lui[ ]+a0,0x0
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+addi[ ]+a0,a0,16 # [0-9a-f]+ <sl>
diff --git a/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-pass.s b/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-pass.s
new file mode 100644
index 0000000000..1b74992eaf
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-pass.s
@@ -0,0 +1,18 @@
+ .section .tbss,"awT",@nobits
+ .zero 0x10
+ .globl sl
+ .type sl,@object
+ .size sl,4
+sl:
+ .zero 4
+
+ .text
+ .globl _start
+ .type _start,@function
+_start:
+.desc1:
+ auipc a0, %tlsdesc_hi(sl)
+ ld t0, %tlsdesc_load_lo(.desc1)(a0)
+ addi a0, a0, %tlsdesc_add_lo(.desc1)
+ .reloc 12, R_RISCV_TLSDESC_CALL, .desc1
+ nop
diff --git a/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32-control.d b/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32-control.d
new file mode 100644
index 0000000000..38f7977721
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32-control.d
@@ -0,0 +1,16 @@
+#name: RISC-V RV32 R_RISCV_TLSDESC_CALL in the last four bytes (smoke control)
+#source: tlsdesc-call-bounds-rv32-pass.s
+#as: -march=rv32i -mabi=ilp32
+#ld: -melf32lriscv -no-pie --no-relax
+#objdump: -d
+#target: riscv*-*-*
+
+.*: file format elf32-.*riscv.*
+
+Disassembly of section \.text:
+
+[0-9a-f]+ <_start>:
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+nop
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+nop
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+lui[ ]+a0,0x0
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+addi[ ]+a0,a0,16 # [0-9a-f]+ <sl>
diff --git a/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32-pass.s b/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32-pass.s
new file mode 100644
index 0000000000..37b30048a1
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32-pass.s
@@ -0,0 +1,18 @@
+ .section .tbss,"awT",@nobits
+ .zero 0x10
+ .globl sl
+ .type sl,@object
+ .size sl,4
+sl:
+ .zero 4
+
+ .text
+ .globl _start
+ .type _start,@function
+_start:
+.desc1:
+ auipc a0, %tlsdesc_hi(sl)
+ lw t0, %tlsdesc_load_lo(.desc1)(a0)
+ addi a0, a0, %tlsdesc_add_lo(.desc1)
+ .reloc 12, R_RISCV_TLSDESC_CALL, .desc1
+ nop
diff --git a/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32.d b/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32.d
new file mode 100644
index 0000000000..adfbf818d8
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32.d
@@ -0,0 +1,6 @@
+#name: RISC-V RV32 R_RISCV_TLSDESC_CALL past the section
+#source: tlsdesc-call-bounds-rv32.s
+#as: -march=rv32i -mabi=ilp32
+#ld: -melf32lriscv -no-pie --no-relax
+#error: .*: bad reloc offset \(0xc \+ 0x4 > 0xc\) for section `\.text'
+#target: riscv*-*-*
diff --git a/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32.s b/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32.s
new file mode 100644
index 0000000000..bacdeee771
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds-rv32.s
@@ -0,0 +1,17 @@
+ .section .tbss,"awT",@nobits
+ .zero 0x10
+ .globl sl
+ .type sl,@object
+ .size sl,4
+sl:
+ .zero 4
+
+ .text
+ .globl _start
+ .type _start,@function
+_start:
+.desc1:
+ auipc a0, %tlsdesc_hi(sl)
+ lw t0, %tlsdesc_load_lo(.desc1)(a0)
+ addi a0, a0, %tlsdesc_add_lo(.desc1)
+ .reloc 12, R_RISCV_TLSDESC_CALL, .desc1
diff --git a/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds.d b/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds.d
new file mode 100644
index 0000000000..6c7815d1a5
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds.d
@@ -0,0 +1,5 @@
+#name: RISC-V R_RISCV_TLSDESC_CALL past the section
+#source: tlsdesc-call-bounds.s
+#ld: -no-pie --no-relax
+#error: .*: bad reloc offset \(0xc \+ 0x4 > 0xc\) for section `\.text'
+#target: riscv64*-*-*
diff --git a/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds.s b/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds.s
new file mode 100644
index 0000000000..0b04dbf0ea
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/tlsdesc-call-bounds.s
@@ -0,0 +1,17 @@
+ .section .tbss,"awT",@nobits
+ .zero 0x10
+ .globl sl
+ .type sl,@object
+ .size sl,4
+sl:
+ .zero 4
+
+ .text
+ .globl _start
+ .type _start,@function
+_start:
+.desc1:
+ auipc a0, %tlsdesc_hi(sl)
+ ld t0, %tlsdesc_load_lo(.desc1)(a0)
+ addi a0, a0, %tlsdesc_add_lo(.desc1)
+ .reloc 12, R_RISCV_TLSDESC_CALL, .desc1
--
2.55.0
More information about the Binutils
mailing list