[RFC PATCH 16/17] sparc: give the field size of relocations with no field

Harshit Kumar harshit7kr@gmail.com
Mon Nov 16 18:43:32 GMT 2026


R_SPARC_WDISP16, R_SPARC_WDISP10, R_SPARC_GOTDATA_OP and the
R_SPARC_TLS_GD_ADD, LDM_ADD, LDO_ADD, IE_LD and IE_LDX relocations
have no dst_mask, so the relocation offset check skips them.  But
_bfd_sparc_elf_relocate_section reads and rewrites the 4-octet
instruction at their r_offset.  Give them a field size of 4.  The type
is masked as in relocate_section, as ELF64 keeps extra data in the
upper bits.

bfd/
	* elfxx-sparc.c (_bfd_sparc_elf_reloc_field_size): New function.
	* elfxx-sparc.h (_bfd_sparc_elf_reloc_field_size): Declare.
	* elf32-sparc.c (elf_backend_reloc_field_size): Define.
	* elf64-sparc.c (elf_backend_reloc_field_size): Define.

ld/
	* testsuite/ld-sparc/reloc-field-wdisp16.d: New test.
	* testsuite/ld-sparc/reloc-field-wdisp16.s: New file.
	* testsuite/ld-sparc/sparc.exp: Run it.

Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
 bfd/elf32-sparc.c                           |  1 +
 bfd/elf64-sparc.c                           |  2 ++
 bfd/elfxx-sparc.c                           | 26 +++++++++++++++++++++
 bfd/elfxx-sparc.h                           |  2 ++
 ld/testsuite/ld-sparc/reloc-field-wdisp16.d |  3 +++
 ld/testsuite/ld-sparc/reloc-field-wdisp16.s |  8 +++++++
 ld/testsuite/ld-sparc/sparc.exp             |  2 ++
 7 files changed, 44 insertions(+)
 create mode 100644 ld/testsuite/ld-sparc/reloc-field-wdisp16.d
 create mode 100644 ld/testsuite/ld-sparc/reloc-field-wdisp16.s

diff --git a/bfd/elf32-sparc.c b/bfd/elf32-sparc.c
index 837b2e16ff..c75e0a50f3 100644
--- a/bfd/elf32-sparc.c
+++ b/bfd/elf32-sparc.c
@@ -263,6 +263,7 @@ elf32_sparc_reloc_type_class (const struct bfd_link_info *info,
 #define elf_backend_reloc_type_class	elf32_sparc_reloc_type_class
 
 #define elf_info_to_howto		_bfd_sparc_elf_info_to_howto
+#define elf_backend_reloc_field_size	_bfd_sparc_elf_reloc_field_size
 #define bfd_elf32_bfd_reloc_type_lookup	_bfd_sparc_elf_reloc_type_lookup
 #define bfd_elf32_bfd_reloc_name_lookup \
   _bfd_sparc_elf_reloc_name_lookup
diff --git a/bfd/elf64-sparc.c b/bfd/elf64-sparc.c
index 0e28bc2e07..2e268f9c57 100644
--- a/bfd/elf64-sparc.c
+++ b/bfd/elf64-sparc.c
@@ -934,6 +934,8 @@ static const struct elf_size_info elf64_sparc_size_info =
   _bfd_sparc_elf_link_hash_table_create
 #define elf_info_to_howto \
   _bfd_sparc_elf_info_to_howto
+#define elf_backend_reloc_field_size \
+  _bfd_sparc_elf_reloc_field_size
 #define elf_backend_copy_indirect_symbol \
   _bfd_sparc_elf_copy_indirect_symbol
 #define bfd_elf64_bfd_reloc_type_lookup \
diff --git a/bfd/elfxx-sparc.c b/bfd/elfxx-sparc.c
index 694ae638c1..273365c0d6 100644
--- a/bfd/elfxx-sparc.c
+++ b/bfd/elfxx-sparc.c
@@ -663,6 +663,32 @@ _bfd_sparc_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
 }
 
 
+/* These relocations have no dst_mask, but relocate_section reads and
+   rewrites the 4-octet instruction at their r_offset.  */
+
+bool
+_bfd_sparc_elf_reloc_field_size (const bfd *abfd ATTRIBUTE_UNUSED,
+				 unsigned int r_type,
+				 bool rel_p ATTRIBUTE_UNUSED,
+				 unsigned int *size)
+{
+  switch (r_type & 0xff)
+    {
+    case R_SPARC_WDISP16:
+    case R_SPARC_WDISP10:
+    case R_SPARC_GOTDATA_OP:
+    case R_SPARC_TLS_GD_ADD:
+    case R_SPARC_TLS_LDM_ADD:
+    case R_SPARC_TLS_LDO_ADD:
+    case R_SPARC_TLS_IE_LD:
+    case R_SPARC_TLS_IE_LDX:
+      *size = 4;
+      return true;
+    default:
+      return false;
+    }
+}
+
 /* The nop opcode we use.  */
 #define SPARC_NOP 0x01000000
 
diff --git a/bfd/elfxx-sparc.h b/bfd/elfxx-sparc.h
index 61f735e7e4..93fa2c2564 100644
--- a/bfd/elfxx-sparc.h
+++ b/bfd/elfxx-sparc.h
@@ -91,6 +91,8 @@ extern reloc_howto_type *_bfd_sparc_elf_reloc_name_lookup
   (bfd *, const char *) ATTRIBUTE_HIDDEN;
 extern bool _bfd_sparc_elf_info_to_howto
   (bfd *, arelent *, Elf_Internal_Rela *) ATTRIBUTE_HIDDEN;
+extern bool _bfd_sparc_elf_reloc_field_size
+  (const bfd *, unsigned int, bool, unsigned int *) ATTRIBUTE_HIDDEN;
 extern reloc_howto_type *_bfd_sparc_elf_info_to_howto_ptr
   (bfd*, unsigned int) ATTRIBUTE_HIDDEN;
 extern bool _bfd_sparc_elf_mkobject
diff --git a/ld/testsuite/ld-sparc/reloc-field-wdisp16.d b/ld/testsuite/ld-sparc/reloc-field-wdisp16.d
new file mode 100644
index 0000000000..9a9819e424
--- /dev/null
+++ b/ld/testsuite/ld-sparc/reloc-field-wdisp16.d
@@ -0,0 +1,3 @@
+#name: SPARC WDISP16 field size for the relocation offset check
+#ld:
+#error: .*bad reloc offset \(0 \+ 0x4 > 0x2\) for section `\.probe'
diff --git a/ld/testsuite/ld-sparc/reloc-field-wdisp16.s b/ld/testsuite/ld-sparc/reloc-field-wdisp16.s
new file mode 100644
index 0000000000..446f62f1cb
--- /dev/null
+++ b/ld/testsuite/ld-sparc/reloc-field-wdisp16.s
@@ -0,0 +1,8 @@
+	.text
+	.globl _start
+_start:
+	.section .probe,"",@progbits
+	.byte 0, 0
+	/* R_SPARC_WDISP16 is a field of a 4-octet instruction, which
+	   would end past this 2-octet section.  */
+	.reloc 0, R_SPARC_WDISP16, _start
diff --git a/ld/testsuite/ld-sparc/sparc.exp b/ld/testsuite/ld-sparc/sparc.exp
index 4f6f0cf895..a44210b9cf 100644
--- a/ld/testsuite/ld-sparc/sparc.exp
+++ b/ld/testsuite/ld-sparc/sparc.exp
@@ -158,6 +158,8 @@ if { ![istarget "sparc-*-elf*"] } {
     run_ld_link_tests $sparc64tests
 }
 
+run_dump_test "reloc-field-wdisp16"
+
 if { [istarget "sparc*-*-linux*"] && [isnative] } {
     run_ld_link_exec_tests [list \
 	[list \
-- 
2.55.0




More information about the Binutils mailing list