[RFC PATCH 03/17] mips: check only the last of composed relocations
Harshit Kumar
harshit7kr@gmail.com
Mon Nov 16 18:43:19 GMT 2026
MIPS relocate_section composes consecutive relocations at the same
offset: each one but the last passes its result to the next as the
addend, and only the last one writes the field. The intermediate ones
may have a wider howto than the field, as R_MIPS_SUB (8 octets) in
%hi(%neg(%gp_rel(x))), whose final R_MIPS_HI16 writes 4, so a
relocation offset check that took every howto size would reject such a
sequence in the last word of a section. Set relocs_compose_at_offset,
so that only the relocation that writes is checked. In a REL object
the first relocation's addend is read from the contents, but
mips_elf_read_rel_addend checks that offset itself.
AArch64 and kvx compose relocations the same way, but do not set the
flag: AArch64 TLS relaxation also rewrites the instruction at an
intermediate relocation. The regression run shows no sequence that the
check rejects on either, but no kvx sequence was built by hand to probe
it, as the MIPS one was.
bfd/
* elf32-mips.c (elf_backend_relocs_compose_at_offset): Define.
* elfn32-mips.c (elf_backend_relocs_compose_at_offset): Define.
* elf64-mips.c (elf_backend_relocs_compose_at_offset): Define.
ld/
* testsuite/ld-mips-elf/reloc-field-compose.s: New file.
* testsuite/ld-mips-elf/reloc-field-compose-n32.d: New test.
* testsuite/ld-mips-elf/reloc-field-compose-n64.d: New test.
* testsuite/ld-mips-elf/mips-elf.exp: Run them.
Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
bfd/elf32-mips.c | 1 +
bfd/elf64-mips.c | 1 +
bfd/elfn32-mips.c | 1 +
ld/testsuite/ld-mips-elf/mips-elf.exp | 5 +++++
ld/testsuite/ld-mips-elf/reloc-field-compose-n32.d | 8 ++++++++
ld/testsuite/ld-mips-elf/reloc-field-compose-n64.d | 8 ++++++++
ld/testsuite/ld-mips-elf/reloc-field-compose.s | 6 ++++++
7 files changed, 30 insertions(+)
create mode 100644 ld/testsuite/ld-mips-elf/reloc-field-compose-n32.d
create mode 100644 ld/testsuite/ld-mips-elf/reloc-field-compose-n64.d
create mode 100644 ld/testsuite/ld-mips-elf/reloc-field-compose.s
diff --git a/bfd/elf32-mips.c b/bfd/elf32-mips.c
index 85c9510bf9..b81d9368c0 100644
--- a/bfd/elf32-mips.c
+++ b/bfd/elf32-mips.c
@@ -4007,6 +4007,7 @@ static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap = {
#define elf_backend_collect true
#define elf_backend_type_change_ok true
#define elf_backend_can_gc_sections true
+#define elf_backend_relocs_compose_at_offset true
#define elf_backend_gc_mark_extra_sections \
_bfd_mips_elf_gc_mark_extra_sections
#define elf_info_to_howto mips_info_to_howto_rela
diff --git a/bfd/elf64-mips.c b/bfd/elf64-mips.c
index af86bd25c1..0f543fb2db 100644
--- a/bfd/elf64-mips.c
+++ b/bfd/elf64-mips.c
@@ -4726,6 +4726,7 @@ static const struct elf_size_info mips_elf64_size_info =
#define elf_backend_collect true
#define elf_backend_type_change_ok true
#define elf_backend_can_gc_sections true
+#define elf_backend_relocs_compose_at_offset true
#define elf_backend_gc_mark_extra_sections \
_bfd_mips_elf_gc_mark_extra_sections
#define elf_info_to_howto mips_elf64_info_to_howto_rela
diff --git a/bfd/elfn32-mips.c b/bfd/elfn32-mips.c
index 91c5e66db5..74bb7083f7 100644
--- a/bfd/elfn32-mips.c
+++ b/bfd/elfn32-mips.c
@@ -4115,6 +4115,7 @@ static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap = {
#define elf_backend_collect true
#define elf_backend_type_change_ok true
#define elf_backend_can_gc_sections true
+#define elf_backend_relocs_compose_at_offset true
#define elf_backend_gc_mark_extra_sections \
_bfd_mips_elf_gc_mark_extra_sections
#define elf_info_to_howto mips_info_to_howto_rela
diff --git a/ld/testsuite/ld-mips-elf/mips-elf.exp b/ld/testsuite/ld-mips-elf/mips-elf.exp
index 04b2951f2f..ef59a44f68 100644
--- a/ld/testsuite/ld-mips-elf/mips-elf.exp
+++ b/ld/testsuite/ld-mips-elf/mips-elf.exp
@@ -339,6 +339,11 @@ run_dump_test_n32 "branch-absolute-addend-n32"
run_dump_test_n64 "branch-absolute-n64"
run_dump_test_n64 "branch-absolute-addend-n64"
+# A composed relocation at the end of a section; only its last part
+# writes the field.
+run_dump_test_n32 "reloc-field-compose-n32"
+run_dump_test_n64 "reloc-field-compose-n64"
+
run_dump_test_o32 "mips16-pcrel-0"
run_dump_test_o32 "mips16-pcrel-1" noarch
run_dump_test_o32 "mips16e2-pcrel-0" noarch
diff --git a/ld/testsuite/ld-mips-elf/reloc-field-compose-n32.d b/ld/testsuite/ld-mips-elf/reloc-field-compose-n32.d
new file mode 100644
index 0000000000..2709af2e8a
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/reloc-field-compose-n32.d
@@ -0,0 +1,8 @@
+#name: MIPS composed relocation at section end (n32)
+#source: reloc-field-compose.s
+#ld: -r
+#readelf: -r
+
+#...
+0+4 .*R_MIPS_GPREL16 .*
+#pass
diff --git a/ld/testsuite/ld-mips-elf/reloc-field-compose-n64.d b/ld/testsuite/ld-mips-elf/reloc-field-compose-n64.d
new file mode 100644
index 0000000000..7dbb3e9343
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/reloc-field-compose-n64.d
@@ -0,0 +1,8 @@
+#name: MIPS composed relocation at section end (n64)
+#source: reloc-field-compose.s
+#ld: -r
+#readelf: -r
+
+#...
+0+4 .*R_MIPS_GPREL16 .*
+#pass
diff --git a/ld/testsuite/ld-mips-elf/reloc-field-compose.s b/ld/testsuite/ld-mips-elf/reloc-field-compose.s
new file mode 100644
index 0000000000..20b59ff8fb
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/reloc-field-compose.s
@@ -0,0 +1,6 @@
+ # A composed relocation whose last part writes the final word
+ # of the section: only that part's field must fit.
+ .section .t2,"ax",@progbits
+ .align 2
+ nop
+ lui $2,%hi(%neg(%gp_rel(foo)))
--
2.55.0
More information about the Binutils
mailing list