[PATCH v2] LoongArch: Detect relocations out of section range

mengqinggang mengqinggang@loongson.cn
Wed Nov 25 08:06:58 GMT 2026


Use bfd_reloc_offset_in_range in loongarch_elf_relocate_section to
reject input relocations whose fields extend outside their section.

bfd/
        * elfnn-loongarch.c
        (loongarch_elf_relocate_section): Reject relocations whose
        fields extend outside the input section.  Skip R_LARCH_NONE
        and R_LARCH_RELAX.
ld/
        * testsuite/ld-loongarch-elf/ld-loongarch-elf.exp: Run new test.
        * testsuite/ld-loongarch-elf/reloc-offset.d: New test.
        * testsuite/ld-loongarch-elf/reloc-offset.s: New test.
---
v1 -> v2:
- Check in relocate_section as Alan suggested.

 bfd/elfnn-loongarch.c                             | 15 +++++++++++++++
 .../ld-loongarch-elf/ld-loongarch-elf.exp         |  1 +
 ld/testsuite/ld-loongarch-elf/reloc-offset.d      |  6 ++++++
 ld/testsuite/ld-loongarch-elf/reloc-offset.s      |  8 ++++++++
 4 files changed, 30 insertions(+)
 create mode 100644 ld/testsuite/ld-loongarch-elf/reloc-offset.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/reloc-offset.s

diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
index 3860ae81be5..b4a1fbdf024 100644
--- a/bfd/elfnn-loongarch.c
+++ b/bfd/elfnn-loongarch.c
@@ -3596,6 +3596,21 @@ loongarch_elf_relocate_section (struct bfd_link_info *info,
       if (howto == NULL)
 	return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
 
+      if (r_type != R_LARCH_NONE
+	  && r_type != R_LARCH_RELAX
+	  && !bfd_reloc_offset_in_range (howto, input_bfd, input_section,
+					 rel->r_offset))
+	{
+	  /* xgettext:c-format */
+	  _bfd_error_handler
+	    (_("%pB: bad reloc offset (%#" PRIx64 " + %#" PRIx64
+	       " > %#" PRIx64 ") for %s in section `%pA'"),
+	     input_bfd, (uint64_t) rel->r_offset,
+	     (uint64_t) bfd_get_reloc_size (howto),
+	     (uint64_t) input_section->size, howto->name, input_section);
+	  return false;
+	}
+
       if (r_type == R_LARCH_GNU_VTINHERIT || r_type == R_LARCH_GNU_VTENTRY)
 	continue;
 
diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
index b8e335bf777..8edbfb1f73a 100644
--- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
+++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
@@ -21,6 +21,7 @@
 
 if [istarget loongarch*-*-*] {
   run_dump_test "dtprel"
+  run_dump_test "reloc-offset"
 }
 
 if [istarget "loongarch64-*-*"] {
diff --git a/ld/testsuite/ld-loongarch-elf/reloc-offset.d b/ld/testsuite/ld-loongarch-elf/reloc-offset.d
new file mode 100644
index 00000000000..01c503af6a7
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/reloc-offset.d
@@ -0,0 +1,6 @@
+# Check that the final link rejects an R_LARCH_64_PCREL relocation whose
+# eight-byte field starts at the end of a one-byte .eh_frame section.
+#name: reloc offset final link
+#source: reloc-offset.s
+#ld: -e 0
+#error: .*bad reloc offset.*0x1 \+ 0x8 > 0x1.*R_LARCH_64_PCREL.*\.eh_frame
diff --git a/ld/testsuite/ld-loongarch-elf/reloc-offset.s b/ld/testsuite/ld-loongarch-elf/reloc-offset.s
new file mode 100644
index 00000000000..9334be31bd8
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/reloc-offset.s
@@ -0,0 +1,8 @@
+.section .eh_frame,"a",@progbits
+.byte 0
+.reloc 1, R_LARCH_64_PCREL, foo
+
+.section .text,"ax",@progbits
+.globl foo
+foo:
+  nop
-- 
2.34.1



More information about the Binutils mailing list