[RFC PATCH 17/17] s390: give the field size of TLS relocations with no field
Harshit Kumar
harshit7kr@gmail.com
Mon Nov 16 18:43:33 GMT 2026
R_390_TLS_LOAD, R_390_TLS_GDCALL and R_390_TLS_LDCALL have no
dst_mask, so the relocation offset check skips them. But
elf_s390_relocate_section reads and rewrites the instruction at their
r_offset. For s390x that is 6 octets (lg or brasl), so give them a
field size of 6. For s390 relocate_section reads 4 octets, whatever
the instruction, so give them 4. It also writes the 2 octets after
those for a brasl call, but a size of 6 would reject a 4-octet bas
call that ends the section, so that case is left to relocate_section.
bfd/
* elf32-s390.c (elf_s390_reloc_field_size): New function.
(elf_backend_reloc_field_size): Define.
* elf64-s390.c (elf_s390_reloc_field_size): New function.
(elf_backend_reloc_field_size): Define.
ld/
* testsuite/ld-s390/reloc-field-tls-gdcall.d: New test.
* testsuite/ld-s390/reloc-field-tls-gdcall.s: New file.
* testsuite/ld-s390/s390.exp: Run it.
Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
bfd/elf32-s390.c | 26 +++++++++++++++++++
bfd/elf64-s390.c | 23 ++++++++++++++++
ld/testsuite/ld-s390/reloc-field-tls-gdcall.d | 3 +++
ld/testsuite/ld-s390/reloc-field-tls-gdcall.s | 8 ++++++
ld/testsuite/ld-s390/s390.exp | 1 +
5 files changed, 61 insertions(+)
create mode 100644 ld/testsuite/ld-s390/reloc-field-tls-gdcall.d
create mode 100644 ld/testsuite/ld-s390/reloc-field-tls-gdcall.s
diff --git a/bfd/elf32-s390.c b/bfd/elf32-s390.c
index 1f2e94aa94..3bacbceb6c 100644
--- a/bfd/elf32-s390.c
+++ b/bfd/elf32-s390.c
@@ -3887,6 +3887,31 @@ elf32_s390_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
}
+/* R_390_TLS_LOAD, R_390_TLS_GDCALL and R_390_TLS_LDCALL have no
+ dst_mask, but relocate_section reads the 4 octets at their r_offset,
+ whatever the instruction there, and rewrites it. For a brasl call it
+ also writes the 2 octets after those; a field size of 6 would reject
+ a 4-octet bas call that ends the section, so that case is left to
+ relocate_section. */
+
+static bool
+elf_s390_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_390_TLS_LOAD:
+ case R_390_TLS_GDCALL:
+ case R_390_TLS_LDCALL:
+ *size = 4;
+ return true;
+ default:
+ return false;
+ }
+}
+
#define TARGET_BIG_SYM s390_elf32_vec
#define TARGET_BIG_NAME "elf32-s390"
#define ELF_ARCH bfd_arch_s390
@@ -3904,6 +3929,7 @@ elf32_s390_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
#define elf_backend_want_dynrelro 1
#define elf_backend_rela_normal 1
+#define elf_backend_reloc_field_size elf_s390_reloc_field_size
#define elf_info_to_howto elf_s390_info_to_howto
#define bfd_elf32_bfd_is_local_label_name elf_s390_is_local_label_name
diff --git a/bfd/elf64-s390.c b/bfd/elf64-s390.c
index 23482e2646..cc18a97910 100644
--- a/bfd/elf64-s390.c
+++ b/bfd/elf64-s390.c
@@ -4434,6 +4434,28 @@ static const struct elf_size_info s390_elf64_size_info =
bfd_elf64_swap_reloca_out
};
+/* R_390_TLS_LOAD, R_390_TLS_GDCALL and R_390_TLS_LDCALL have no
+ dst_mask, but relocate_section reads and rewrites the 6-octet lg or
+ brasl instruction at their r_offset. */
+
+static bool
+elf_s390_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_390_TLS_LOAD:
+ case R_390_TLS_GDCALL:
+ case R_390_TLS_LDCALL:
+ *size = 6;
+ return true;
+ default:
+ return false;
+ }
+}
+
#define TARGET_BIG_SYM s390_elf64_vec
#define TARGET_BIG_NAME "elf64-s390"
#define ELF_ARCH bfd_arch_s390
@@ -4453,6 +4475,7 @@ static const struct elf_size_info s390_elf64_size_info =
#define elf_backend_want_dynrelro 1
#define elf_backend_rela_normal 1
+#define elf_backend_reloc_field_size elf_s390_reloc_field_size
#define elf_info_to_howto elf_s390_info_to_howto
#define bfd_elf64_bfd_is_local_label_name elf_s390_is_local_label_name
diff --git a/ld/testsuite/ld-s390/reloc-field-tls-gdcall.d b/ld/testsuite/ld-s390/reloc-field-tls-gdcall.d
new file mode 100644
index 0000000000..d51f6f5900
--- /dev/null
+++ b/ld/testsuite/ld-s390/reloc-field-tls-gdcall.d
@@ -0,0 +1,3 @@
+#name: s390 TLS_GDCALL field size for the relocation offset check
+#ld:
+#error: .*bad reloc offset \(0 \+ 0x[46] > 0x2\) for section `\.probe'
diff --git a/ld/testsuite/ld-s390/reloc-field-tls-gdcall.s b/ld/testsuite/ld-s390/reloc-field-tls-gdcall.s
new file mode 100644
index 0000000000..50a78aaa81
--- /dev/null
+++ b/ld/testsuite/ld-s390/reloc-field-tls-gdcall.s
@@ -0,0 +1,8 @@
+ .text
+ .globl _start
+_start:
+ .section .probe,"",@progbits
+ .byte 0, 0
+ /* R_390_TLS_GDCALL marks a call of 4 or 6 octets, which a TLS
+ transition rewrites; it would end past this 2-octet section. */
+ .reloc 0, R_390_TLS_GDCALL, _start
diff --git a/ld/testsuite/ld-s390/s390.exp b/ld/testsuite/ld-s390/s390.exp
index 80536e2e39..9674295db8 100644
--- a/ld/testsuite/ld-s390/s390.exp
+++ b/ld/testsuite/ld-s390/s390.exp
@@ -180,6 +180,7 @@ if { [istarget "s390-*-*"] || [istarget "s390x-*-*"] } {
run_dump_test "pr26918-1"
run_dump_test "reloccheck-1"
run_dump_test "reloctlsle-1"
+ run_dump_test "reloc-field-tls-gdcall"
}
--
2.55.0
More information about the Binutils
mailing list