]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commit
s390: Rewrite emitted relocations when rewriting instructions
authorJens Remus <jremus@linux.ibm.com>
Thu, 16 Oct 2025 09:06:17 +0000 (11:06 +0200)
committerJens Remus <jremus@linux.ibm.com>
Thu, 16 Oct 2025 09:06:17 +0000 (11:06 +0200)
commitad8434760ee1124a402705c8e815a9ba30eb7533
treef99f94e7038cf4333d3376801537320f3c3e5842
parent9c5a8b25703c9b316eeefb1acf87df34279c328b
s390: Rewrite emitted relocations when rewriting instructions

When the linker rewrites instructions it may need to rewrite the
associated relocations, so that when emitted with option --emit-relocs,
they make sense with the rewritten instructions.  Otherwise post link
analysis and optimization tools may not be able to perform correct
modifications of executables.

Under certain conditions the linker rewrites:
- GOT access using lgrl to larl, changing the GOTENT to a PC32DBL reloc
- GOT access using lg to larl, changing the GOT20 to a PC32DBL reloc
- Relative long addressing instructions of weak symbols, which
  definitively resolve to zero, to either (1) load address of zero,
  (2) a NOP, or (3) a trapping instruction.

In case of a rewrite of GOT access using lgrl/lg to larl emit the
PC32DBL relocation.  In case of a rewrite of relative long addressing
instructions of weak symbols, which definitively resolve to zero, emit
a NONE relocation and reset the symbol table index and addend.  Update
the gotreloc* and weakundef* tests to check for the respective
relocations.

This aligns with how GNU linker behaves on x86-64, when rewriting
instructions/relocations in elf_x86_64_convert_load_reloc().

bfd/
* elf64-s390.c (elf_s390_relocate_section): Rewrite emitted
relocations when rewriting instructions.
* elf32-s390.c (elf_s390_relocate_section): Likewise.

ld/testsuite/
* ld-s390/s390.exp: Link gotreloc* and weakundef* tests with
option --emit-relocs.
* ld-s390/gotreloc_31-1.dd: Expect rewritten instructions to be
accompanied by a PC32DBL or NONE relocation respectively.
* ld-s390/gotreloc_31-no-pie-1.dd: Likewise.
* ld-s390/gotreloc_64-no-pie-1.dd: Likewise.
* ld-s390/gotreloc_64-norelro-1.dd: Likewise.
* ld-s390/gotreloc_64-relro-1.dd: Likewise.
* ld-s390/weakundef-1.dd: Likewise.
* ld-s390/weakundef-2.dd: Likewise.

Fixes: a63cc5f70ca6 ("Rewrite GOT accesses using larl if possible")
Fixes: 896a639babe2 ("s390: Avoid reloc overflows on undefined weak symbols")
Fixes: 6f8b365b653d ("s390: Avoid reloc overflows on undefined weak symbols (cont)")
Signed-off-by: Jens Remus <jremus@linux.ibm.com>
bfd/elf32-s390.c
bfd/elf64-s390.c
ld/testsuite/ld-s390/gotreloc_31-1.dd
ld/testsuite/ld-s390/gotreloc_31-no-pie-1.dd
ld/testsuite/ld-s390/gotreloc_64-no-pie-1.dd
ld/testsuite/ld-s390/gotreloc_64-norelro-1.dd
ld/testsuite/ld-s390/gotreloc_64-relro-1.dd
ld/testsuite/ld-s390/s390.exp
ld/testsuite/ld-s390/weakundef-1.dd
ld/testsuite/ld-s390/weakundef-2.dd