[RFC PATCH 13/17] mips: give the field size of JALR
Harshit Kumar
harshit7kr@gmail.com
Mon Nov 16 18:43:29 GMT 2026
R_MIPS_JALR and R_MICROMIPS_JALR have no dst_mask, so the relocation
offset check skips them, but mips_elf_perform_relocation reads the
jump at their r_offset, and may turn it into a branch. Give
R_MIPS_JALR a field size of 4, for the o32, n32 and n64 backends. A
microMIPS jump may be a 2-octet compact jump with no delay slot, which
can end a section, so give R_MICROMIPS_JALR only 2. The microMIPS
shuffle in mips_elf_perform_relocation still reads and writes 4
octets at r_offset, as on master; past a 2-octet jump that ends its
section, that is left to the backend.
bfd/
* elfxx-mips.c (_bfd_mips_elf_reloc_field_size): New function.
* elfxx-mips.h (_bfd_mips_elf_reloc_field_size): Declare.
* elf32-mips.c (elf_backend_reloc_field_size): Define.
* elfn32-mips.c (elf_backend_reloc_field_size): Define.
* elf64-mips.c (mips_elf64_reloc_field_size): Call
_bfd_mips_elf_reloc_field_size first.
ld/
* testsuite/ld-mips-elf/reloc-field-jalr.d: New test.
* testsuite/ld-mips-elf/reloc-field-jalr.s: New file.
* testsuite/ld-mips-elf/mips-elf.exp: Run it.
Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
bfd/elf32-mips.c | 1 +
bfd/elf64-mips.c | 7 ++++--
bfd/elfn32-mips.c | 1 +
bfd/elfxx-mips.c | 27 +++++++++++++++++++++
bfd/elfxx-mips.h | 2 ++
ld/testsuite/ld-mips-elf/mips-elf.exp | 1 +
ld/testsuite/ld-mips-elf/reloc-field-jalr.d | 3 +++
ld/testsuite/ld-mips-elf/reloc-field-jalr.s | 8 ++++++
8 files changed, 48 insertions(+), 2 deletions(-)
create mode 100644 ld/testsuite/ld-mips-elf/reloc-field-jalr.d
create mode 100644 ld/testsuite/ld-mips-elf/reloc-field-jalr.s
diff --git a/bfd/elf32-mips.c b/bfd/elf32-mips.c
index b81d9368c0..f314d5a9d6 100644
--- a/bfd/elf32-mips.c
+++ b/bfd/elf32-mips.c
@@ -4008,6 +4008,7 @@ static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap = {
#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_reloc_field_size _bfd_mips_elf_reloc_field_size
#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 4870786318..d0ea587ddb 100644
--- a/bfd/elf64-mips.c
+++ b/bfd/elf64-mips.c
@@ -4003,12 +4003,15 @@ mips_elf64_info_to_howto_rela (bfd *abfd,
look the howto up here, without reporting an unknown type. */
static bool
-mips_elf64_reloc_field_size (const bfd *abfd ATTRIBUTE_UNUSED,
+mips_elf64_reloc_field_size (const bfd *abfd,
unsigned int r_type, bool rel_p,
unsigned int *size)
{
- reloc_howto_type *howto = mips_elf64_lookup_howto (r_type, !rel_p);
+ reloc_howto_type *howto;
+ if (_bfd_mips_elf_reloc_field_size (abfd, r_type, rel_p, size))
+ return true;
+ howto = mips_elf64_lookup_howto (r_type, !rel_p);
if (howto == NULL || howto->dst_mask == 0)
*size = 0;
else
diff --git a/bfd/elfn32-mips.c b/bfd/elfn32-mips.c
index 74bb7083f7..6d240dcd50 100644
--- a/bfd/elfn32-mips.c
+++ b/bfd/elfn32-mips.c
@@ -4116,6 +4116,7 @@ static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap = {
#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_reloc_field_size _bfd_mips_elf_reloc_field_size
#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/elfxx-mips.c b/bfd/elfxx-mips.c
index 2c3132a71b..d9efa74d0c 100644
--- a/bfd/elfxx-mips.c
+++ b/bfd/elfxx-mips.c
@@ -13034,6 +13034,33 @@ _bfd_mips_elf_gc_mark_hook (asection *sec,
return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
}
+/* R_MIPS_JALR and R_MICROMIPS_JALR have no dst_mask, but
+ mips_elf_perform_relocation reads the jump at their r_offset, and may
+ turn it into a branch. A MIPS jump is 4 octets; a microMIPS one may
+ be a 2-octet compact jump with no delay slot, so only 2 are certain
+ there. The microMIPS shuffle still accesses 4 octets at r_offset;
+ past a 2-octet jump that ends its section, that is left to the
+ backend. */
+
+bool
+_bfd_mips_elf_reloc_field_size (const bfd *abfd ATTRIBUTE_UNUSED,
+ unsigned int r_type,
+ bool rel_p ATTRIBUTE_UNUSED,
+ unsigned int *size)
+{
+ switch (r_type)
+ {
+ case R_MIPS_JALR:
+ *size = 4;
+ return true;
+ case R_MICROMIPS_JALR:
+ *size = 2;
+ return true;
+ default:
+ return false;
+ }
+}
+
/* Prevent .MIPS.abiflags from being discarded with --gc-sections. */
bool
diff --git a/bfd/elfxx-mips.h b/bfd/elfxx-mips.h
index 1b9abed451..338e4cebad 100644
--- a/bfd/elfxx-mips.h
+++ b/bfd/elfxx-mips.h
@@ -189,6 +189,8 @@ extern long _bfd_mips_elf_get_synthetic_symtab
(bfd *, long, asymbol **, long, asymbol **, asymbol **) ATTRIBUTE_HIDDEN;
extern bool _bfd_mips_elf_gc_mark_extra_sections
(struct bfd_link_info *, elf_gc_mark_hook_fn) ATTRIBUTE_HIDDEN;
+extern bool _bfd_mips_elf_reloc_field_size
+ (const bfd *, unsigned int, bool, unsigned int *) ATTRIBUTE_HIDDEN;
extern bool _bfd_mips_init_file_header
(bfd *abfd, struct bfd_link_info *link_info) ATTRIBUTE_HIDDEN;
diff --git a/ld/testsuite/ld-mips-elf/mips-elf.exp b/ld/testsuite/ld-mips-elf/mips-elf.exp
index ef59a44f68..576a666b23 100644
--- a/ld/testsuite/ld-mips-elf/mips-elf.exp
+++ b/ld/testsuite/ld-mips-elf/mips-elf.exp
@@ -343,6 +343,7 @@ run_dump_test_n64 "branch-absolute-addend-n64"
# writes the field.
run_dump_test_n32 "reloc-field-compose-n32"
run_dump_test_n64 "reloc-field-compose-n64"
+run_dump_test_o32 "reloc-field-jalr"
run_dump_test_o32 "mips16-pcrel-0"
run_dump_test_o32 "mips16-pcrel-1" noarch
diff --git a/ld/testsuite/ld-mips-elf/reloc-field-jalr.d b/ld/testsuite/ld-mips-elf/reloc-field-jalr.d
new file mode 100644
index 0000000000..97152fe9f8
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/reloc-field-jalr.d
@@ -0,0 +1,3 @@
+#name: MIPS JALR field size for the relocation offset check
+#ld:
+#error: .*bad reloc offset \(0 \+ 0x4 > 0x2\) for section `\.probe'
diff --git a/ld/testsuite/ld-mips-elf/reloc-field-jalr.s b/ld/testsuite/ld-mips-elf/reloc-field-jalr.s
new file mode 100644
index 0000000000..43b3b4c4e8
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/reloc-field-jalr.s
@@ -0,0 +1,8 @@
+ .text
+ .globl _start
+_start:
+ .section .probe,"",@progbits
+ .byte 0, 0
+ /* R_MIPS_JALR marks a 4-octet jump, which the linker reads and may
+ turn into a branch; it would end past this 2-octet section. */
+ .reloc 0, R_MIPS_JALR, _start
--
2.55.0
More information about the Binutils
mailing list