[PATCH] LoongArch: Detect relocations out of section range
mengqinggang
mengqinggang@loongson.cn
Thu Nov 19 12:47:42 GMT 2026
Use bfd_reloc_offset_in_range in check_relocs to reject relocations
whose fields are out of the input section range during final linking.
bfd/
* elfnn-loongarch.c (loongarch_elf_check_relocs): Reject relocations
whose fields are out of the input section range.
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.
---
bfd/elfnn-loongarch.c | 24 +++++++++++++++++++
.../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, 39 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..25e68d43f9f 100644
--- a/bfd/elfnn-loongarch.c
+++ b/bfd/elfnn-loongarch.c
@@ -1148,6 +1148,7 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
{
unsigned int r_type;
unsigned int r_symndx;
+ reloc_howto_type *howto;
struct elf_link_hash_entry *h;
const Elf_Internal_Sym *isym = NULL;
bool is_abs_symbol = false;
@@ -1155,6 +1156,29 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
r_symndx = ELFNN_R_SYM (rel->r_info);
r_type = ELFNN_R_TYPE (rel->r_info);
+ /* Don't check R_LARCH_NONE. */
+ if (r_type == R_LARCH_NONE)
+ continue;
+
+ howto = loongarch_elf_rtype_to_howto (abfd, r_type);
+ if (howto == NULL)
+ {
+ _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
+ abfd, r_type);
+ return false;
+ }
+
+ if (!bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
+ {
+ /* xgettext:c-format */
+ _bfd_error_handler
+ (_("%pB: %s: bad reloc offset (%#" PRIx64 " + %#x > %#" PRIx64 ")"
+ " for section `%pA'"), abfd, howto->name,
+ (uint64_t) rel->r_offset, bfd_get_reloc_size (howto),
+ (uint64_t) sec->size, sec);
+ return false;
+ }
+
if (r_symndx < symtab_hdr->sh_info)
{
/* A local symbol. */
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..25ac373bd29
--- /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: .*R_LARCH_64_PCREL.*bad reloc offset.*0x1 \+ 0x8 > 0x1.*\.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