public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/2] s390: Avoid relocation overflows on undefined weak symbols
@ 2024-06-26 15:20 Jens Remus
  2024-06-26 15:20 ` [PATCH 1/2] s390: Do not replace brcth referencing undefined weak symbol Jens Remus
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Jens Remus @ 2024-06-26 15:20 UTC (permalink / raw)
  To: binutils, Andreas Krebbel; +Cc: Jens Remus

Patch 1 corrects commit 896a639babe2 ("s390: Avoid reloc overflows on
undefined weak symbols") not to replace Branch Relative on Count High
(brcth) referencing an undefined weak symbol definitively resolving to
zero by a trap, as it is not guaranteed that the conditional branch is
taken in any case.

Patch 2 complements commit 896a639babe2 ("s390: Avoid reloc overflows
on undefined weak symbols") by applying similar replacements of
instructions referencing undefined weak symbols that definitively
resolve to zero. This time for PLT32DBL relocations.

Regards,
Jens

Jens Remus (2):
  s390: Do not replace brcth referencing undefined weak symbol
  s390: Avoid reloc overflows on undefined weak symbols (cont)

 bfd/elf64-s390.c                    | 42 ++++++++++++++++++++++++++---
 ld/testsuite/ld-s390/s390.exp       |  5 +++-
 ld/testsuite/ld-s390/weakundef-1.dd |  6 ++---
 ld/testsuite/ld-s390/weakundef-1.s  |  1 -
 ld/testsuite/ld-s390/weakundef-2.dd | 17 ++++++++++++
 ld/testsuite/ld-s390/weakundef-2.s  | 17 ++++++++++++
 6 files changed, 80 insertions(+), 8 deletions(-)
 create mode 100644 ld/testsuite/ld-s390/weakundef-2.dd
 create mode 100644 ld/testsuite/ld-s390/weakundef-2.s

-- 
2.40.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 1/2] s390: Do not replace brcth referencing undefined weak symbol
  2024-06-26 15:20 [PATCH 0/2] s390: Avoid relocation overflows on undefined weak symbols Jens Remus
@ 2024-06-26 15:20 ` Jens Remus
  2024-07-12 12:27   ` Andreas Krebbel
  2024-06-26 15:20 ` [PATCH 2/2] s390: Avoid reloc overflows on undefined weak symbols (cont) Jens Remus
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 10+ messages in thread
From: Jens Remus @ 2024-06-26 15:20 UTC (permalink / raw)
  To: binutils, Andreas Krebbel; +Cc: Jens Remus

Branch Relative on Count High (brcth) is a conditional branch relative
instruction. It is not guaranteed that it only appears within loops
that sooner or later will take the branch. It may very well be used to
check a condition that will prevent the branch from ever being taken.

bfd/
	* elf64-s390.c (elf_s390_relocate_section): Do not replace brcth
	referencing undefined weak symbol with a trap.

ld/
	* testsuite/ld-s390/weakundef-1.s: Update test case accordingly.
	* testsuite/ld-s390/weakundef-1.dd: Likewise.

Fixes: 896a639babe2 ("s390: Avoid reloc overflows on undefined weak symbols")
Signed-off-by: Jens Remus <jremus@linux.ibm.com>
---
 bfd/elf64-s390.c                    | 5 ++---
 ld/testsuite/ld-s390/weakundef-1.dd | 6 +++---
 ld/testsuite/ld-s390/weakundef-1.s  | 1 -
 3 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/bfd/elf64-s390.c b/bfd/elf64-s390.c
index 05dd4e80c6b3..2f35da4a6275 100644
--- a/bfd/elf64-s390.c
+++ b/bfd/elf64-s390.c
@@ -2517,10 +2517,9 @@ elf_s390_relocate_section (bfd *output_bfd,
 		 - store halfword relative long (sthrl)
 		 - execute relative long (exrl)
 		 - compare (logical) relative long (crl, clrl, cgrl, clgrl, cgfrl, clgfrl)
-		 - compare (logical) halfword relative long (chrl, cghrl, clhrl, clghrl)
-		 - branch relative on count high (brcth)  */
+		 - compare (logical) halfword relative long (chrl, cghrl, clhrl, clghrl)  */
 	      else if (op == 0xc005 || (op & 0xff00) == 0xc400
-		       || (op & 0xff00) == 0xc600 || op == 0xcc06)
+		       || (op & 0xff00) == 0xc600)
 		{
 		  /* Emit a 6-byte trap: jg .+2  */
 		  bfd_put_16 (output_bfd, 0xc0f4, insn_start);
diff --git a/ld/testsuite/ld-s390/weakundef-1.dd b/ld/testsuite/ld-s390/weakundef-1.dd
index e5145245602e..04d53c91d58e 100644
--- a/ld/testsuite/ld-s390/weakundef-1.dd
+++ b/ld/testsuite/ld-s390/weakundef-1.dd
@@ -3,13 +3,13 @@ tmpdir/weakundef-1:     file format elf64-s390
 Disassembly of section .text:
 
 .* <foo>:
-.*:	c0 10 00 00 00 1e [	 ]*larl	%r1,20000003c <d>
-.*:	c0 10 00 00 00 1f [	 ]*larl	%r1,200000044 <wd>
+.*:	c0 10 00 00 00 1c [	 ]*larl	%r1,200000038 <d>
+.*:	c0 10 00 00 00 1d [	 ]*larl	%r1,200000040 <wd>
 .*:	e3 10 00 00 00 71 [	 ]*lay	%r1,0
 .*:	c0 f4 00 00 00 01 [	 ]*jg	.*
 .*:	c0 f4 00 00 00 01 [	 ]*jg	.*
 .*:	c0 f4 00 00 00 01 [	 ]*jg	.*
 .*:	c0 f4 00 00 00 01 [	 ]*jg	.*
 .*:	c0 f4 00 00 00 01 [	 ]*jg	.*
-.*:	c0 f4 00 00 00 01 [	 ]*jg	.*
 .*:	c0 04 00 00 00 00 [	 ]*jgnop	.*
+.*:	07 07 [	 ]*nopr	%r7
diff --git a/ld/testsuite/ld-s390/weakundef-1.s b/ld/testsuite/ld-s390/weakundef-1.s
index aeaef8d2456f..db909329cda6 100644
--- a/ld/testsuite/ld-s390/weakundef-1.s
+++ b/ld/testsuite/ld-s390/weakundef-1.s
@@ -9,7 +9,6 @@ foo:
 	lrl	%r1,wu
 	strl	%r1,wu
 	exrl	%r1,wu
-	brcth	%r1,wu
 	pfdrl	%r1,wu
 	.weak	wd
 	.weak	wu
-- 
2.40.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 2/2] s390: Avoid reloc overflows on undefined weak symbols (cont)
  2024-06-26 15:20 [PATCH 0/2] s390: Avoid relocation overflows on undefined weak symbols Jens Remus
  2024-06-26 15:20 ` [PATCH 1/2] s390: Do not replace brcth referencing undefined weak symbol Jens Remus
@ 2024-06-26 15:20 ` Jens Remus
  2024-07-12 12:29   ` Andreas Krebbel
  2024-06-26 18:26 ` [PATCH 0/2] s390: Avoid relocation overflows on undefined weak symbols Fangrui Song
  2024-07-12 14:58 ` Jens Remus
  3 siblings, 1 reply; 10+ messages in thread
From: Jens Remus @ 2024-06-26 15:20 UTC (permalink / raw)
  To: binutils, Andreas Krebbel; +Cc: Jens Remus, Alexander Gordeev, Ilya Leoshkevich

This complements and reuses logic from Andreas Krebbel's commit
896a639babe2 ("s390: Avoid reloc overflows on undefined weak symbols").

Replace relative long addressing instructions of weak symbols, which
will definitely resolve to zero, with either a load address of 0 or a
a trapping insn.

This prevents the PLT32DBL relocation from overflowing in case the
binary will be loaded at 4GB or more.

bfd/
	* elf64-s390.c (elf_s390_relocate_section): Replace
	instructions using undefined weak symbols with relative
	addressing to avoid relocation overflows.

ld/
	* testsuite/ld-s390/s390.exp: Add new test.
	* testsuite/ld-s390/weakundef-2.s: New test.
	* testsuite/ld-s390/weakundef-2.dd: Likewise.

Reported-by: Alexander Gordeev <agordeev@linux.ibm.com>
Suggested-by: Ilya Leoshkevich <iii@linux.ibm.com>
Suggested-by: Andreas Krebbel <krebbel@linux.ibm.com>
Signed-off-by: Jens Remus <jremus@linux.ibm.com>
---
 bfd/elf64-s390.c                    | 37 +++++++++++++++++++++++++++++
 ld/testsuite/ld-s390/s390.exp       |  5 +++-
 ld/testsuite/ld-s390/weakundef-2.dd | 17 +++++++++++++
 ld/testsuite/ld-s390/weakundef-2.s  | 17 +++++++++++++
 4 files changed, 75 insertions(+), 1 deletion(-)
 create mode 100644 ld/testsuite/ld-s390/weakundef-2.dd
 create mode 100644 ld/testsuite/ld-s390/weakundef-2.s

diff --git a/bfd/elf64-s390.c b/bfd/elf64-s390.c
index 2f35da4a6275..f9d9902651bf 100644
--- a/bfd/elf64-s390.c
+++ b/bfd/elf64-s390.c
@@ -2399,6 +2399,43 @@ elf_s390_relocate_section (bfd *output_bfd,
 	      /* We didn't make a PLT entry for this symbol.  This
 		 happens when statically linking PIC code, or when
 		 using -Bsymbolic.  */
+
+	      /* Replace relative long addressing instructions of weak
+		 symbols, which will definitely resolve to zero, with
+		 either a load address of 0 or a trapping insn.
+		 This prevents the PLT32DBL relocation from overflowing in
+		 case the binary will be loaded at 4GB or more.  */
+	      if (h->root.type == bfd_link_hash_undefweak
+		  && !h->root.linker_def
+		  && (bfd_link_executable (info)
+		      || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
+		  && r_type == R_390_PLT32DBL
+		  && rel->r_offset >= 2)
+		{
+		  void *insn_start = contents + rel->r_offset - 2;
+		  uint16_t op = bfd_get_16 (input_bfd, insn_start) & 0xff0f;
+		  uint8_t reg = bfd_get_8 (input_bfd, insn_start + 1) & 0xf0;
+
+		  /* NOTE: The order of the if's is important!  */
+		  /* Replace load address relative long (larl) with load
+		     address (lay) */
+		  if (op == 0xc000)
+		    {
+		      /* larl rX,<weak sym> -> lay rX,0(0)  */
+		      bfd_put_16 (output_bfd, 0xe300 | reg, insn_start);
+		      bfd_put_32 (output_bfd, 0x71, insn_start + 2);
+		      continue;
+		    }
+		  /* Replace branch relative and save long (brasl) with a trap.  */
+		  else if (op == 0xc005)
+		    {
+		      /* brasl rX,<weak sym> -> jg .+2 (6-byte trap)  */
+		      bfd_put_16 (output_bfd, 0xc0f4, insn_start);
+		      bfd_put_32 (output_bfd, 0x1, insn_start + 2);
+		      continue;
+		    }
+		}
+
 	      break;
 	    }
 	  if (s390_is_ifunc_symbol_p (h))
diff --git a/ld/testsuite/ld-s390/s390.exp b/ld/testsuite/ld-s390/s390.exp
index eb9ea35400b2..ac62d7a8a1a5 100644
--- a/ld/testsuite/ld-s390/s390.exp
+++ b/ld/testsuite/ld-s390/s390.exp
@@ -95,9 +95,12 @@ set s390xtests {
      "-m64" {pltoffset-1.s}
      {{objdump "-dzrj.text --stop-address=16" pltoffset-1.dd}}
      "pltoffset-1"}
-    {"WEAKUNDEF1: overflow test"
+    {"WEAKUNDEF1: overflow test (PC32DBL)"
      "-m elf64_s390 -dT 8GB.ld --no-error-rwx-segments" "" "-m64" {weakundef-1.s}
      {{objdump "-dzrj.text" weakundef-1.dd}} "weakundef-1"}
+    {"WEAKUNDEF2: overflow test (PLT32DBL)"
+     "-m elf64_s390 -dT 8GB.ld --no-error-rwx-segments -no-pie" "" "-m64" {weakundef-2.s}
+     {{objdump "-dzrj.text" weakundef-2.dd}} "weakundef-2"}
 }
 
 if [istarget "s390-*-*"] {
diff --git a/ld/testsuite/ld-s390/weakundef-2.dd b/ld/testsuite/ld-s390/weakundef-2.dd
new file mode 100644
index 000000000000..e7f0e2239b6a
--- /dev/null
+++ b/ld/testsuite/ld-s390/weakundef-2.dd
@@ -0,0 +1,17 @@
+tmpdir/weakundef-2:     file format elf64-s390
+
+Disassembly of section .text:
+
+0+200000000 <foo>:
+.*:	c0 10 00 00 00 12 [	 ]*larl	%r1,200000024 <d>
+.*:	c0 10 00 00 00 10 [	 ]*larl	%r1,200000026 <wd>
+.*:	e3 10 00 00 00 71 [	 ]*lay	%r1,0
+.*:	c0 e5 00 00 00 09 [	 ]*brasl	%r14,200000024 <d>
+.*:	c0 e5 00 00 00 07 [	 ]*brasl	%r14,200000026 <wd>
+.*:	c0 f4 00 00 00 01 [	 ]*jg	.*
+
+0+200000024 <d>:
+.*:	07 fe [	 ]*br	%r14
+
+0+200000026 <wd>:
+.*:	07 fe [	 ]*br	%r14
diff --git a/ld/testsuite/ld-s390/weakundef-2.s b/ld/testsuite/ld-s390/weakundef-2.s
new file mode 100644
index 000000000000..d147b53d6dca
--- /dev/null
+++ b/ld/testsuite/ld-s390/weakundef-2.s
@@ -0,0 +1,17 @@
+.text
+	.globl foo
+foo:
+	larl	%r1,d@PLT
+	larl	%r1,wd@PLT
+	larl	%r1,wu@PLT
+	brasl	%r14,d@PLT
+	brasl	%r14,wd@PLT
+	brasl	%r14,wu@PLT
+	.weak	wu
+	.type d,@function
+d:
+	br	%r14
+	.weak	wd
+	.type wd,@function
+wd:
+	br	%r14
-- 
2.40.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 0/2] s390: Avoid relocation overflows on undefined weak symbols
  2024-06-26 15:20 [PATCH 0/2] s390: Avoid relocation overflows on undefined weak symbols Jens Remus
  2024-06-26 15:20 ` [PATCH 1/2] s390: Do not replace brcth referencing undefined weak symbol Jens Remus
  2024-06-26 15:20 ` [PATCH 2/2] s390: Avoid reloc overflows on undefined weak symbols (cont) Jens Remus
@ 2024-06-26 18:26 ` Fangrui Song
  2024-07-01 14:33   ` Jens Remus
  2024-07-12 14:58 ` Jens Remus
  3 siblings, 1 reply; 10+ messages in thread
From: Fangrui Song @ 2024-06-26 18:26 UTC (permalink / raw)
  To: Jens Remus; +Cc: binutils, Andreas Krebbel

On Wed, Jun 26, 2024 at 8:20 AM Jens Remus <jremus@linux.ibm.com> wrote:
>
> Patch 1 corrects commit 896a639babe2 ("s390: Avoid reloc overflows on
> undefined weak symbols") not to replace Branch Relative on Count High
> (brcth) referencing an undefined weak symbol definitively resolving to
> zero by a trap, as it is not guaranteed that the conditional branch is
> taken in any case.
>
> Patch 2 complements commit 896a639babe2 ("s390: Avoid reloc overflows
> on undefined weak symbols") by applying similar replacements of
> instructions referencing undefined weak symbols that definitively
> resolve to zero. This time for PLT32DBL relocations.
>
> Regards,
> Jens
>
> Jens Remus (2):
>   s390: Do not replace brcth referencing undefined weak symbol
>   s390: Avoid reloc overflows on undefined weak symbols (cont)
>
>  bfd/elf64-s390.c                    | 42 ++++++++++++++++++++++++++---
>  ld/testsuite/ld-s390/s390.exp       |  5 +++-
>  ld/testsuite/ld-s390/weakundef-1.dd |  6 ++---
>  ld/testsuite/ld-s390/weakundef-1.s  |  1 -
>  ld/testsuite/ld-s390/weakundef-2.dd | 17 ++++++++++++
>  ld/testsuite/ld-s390/weakundef-2.s  | 17 ++++++++++++
>  6 files changed, 80 insertions(+), 8 deletions(-)
>  create mode 100644 ld/testsuite/ld-s390/weakundef-2.dd
>  create mode 100644 ld/testsuite/ld-s390/weakundef-2.s
>
> --
> 2.40.1
>

Other ports have simpler strategies for unresolved undefined weak symbols
https://reviews.llvm.org/D103001

aarch64: GNU ld: rewrite the instruction to a NOP; ld.lld: branch to
the next instruction
mips: GNU ld: branch to the start of the text segment (?); ld.lld:
branch to zero
ppc32: GNU ld: rewrite the instruction to a NOP; ld.lld: branch to the
current instruction
ppc64: GNU ld: rewrite the instruction to a NOP; ld.lld: branch to the
current instruction
riscv: GNU ld: branch to the absolute zero address (with instruction rewriting)
i386/x86_64: GNU ld/ld.lld: branch to the link-time zero address

I haven't checked the patch closely, but it seems to add quite a few lines.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 0/2] s390: Avoid relocation overflows on undefined weak symbols
  2024-06-26 18:26 ` [PATCH 0/2] s390: Avoid relocation overflows on undefined weak symbols Fangrui Song
@ 2024-07-01 14:33   ` Jens Remus
  2024-07-03  0:43     ` Alan Modra
  0 siblings, 1 reply; 10+ messages in thread
From: Jens Remus @ 2024-07-01 14:33 UTC (permalink / raw)
  To: Fangrui Song; +Cc: binutils, Andreas Krebbel

Hello Fangrui,

thank you for the feedback and hints at how the issue is dealt with by 
other architectures!

Am 26.06.2024 um 20:26 schrieb Fangrui Song:
> On Wed, Jun 26, 2024 at 8:20 AM Jens Remus <jremus@linux.ibm.com> wrote:
>> Patch 1 corrects commit 896a639babe2 ("s390: Avoid reloc overflows on
>> undefined weak symbols") not to replace Branch Relative on Count High
>> (brcth) referencing an undefined weak symbol definitively resolving to
>> zero by a trap, as it is not guaranteed that the conditional branch is
>> taken in any case.
>>
>> Patch 2 complements commit 896a639babe2 ("s390: Avoid reloc overflows
>> on undefined weak symbols") by applying similar replacements of
>> instructions referencing undefined weak symbols that definitively
>> resolve to zero. This time for PLT32DBL relocations.
...
>> Jens Remus (2):
>>    s390: Do not replace brcth referencing undefined weak symbol
>>    s390: Avoid reloc overflows on undefined weak symbols (cont)
>>
>>   bfd/elf64-s390.c                    | 42 ++++++++++++++++++++++++++---
>>   ld/testsuite/ld-s390/s390.exp       |  5 +++-
>>   ld/testsuite/ld-s390/weakundef-1.dd |  6 ++---
>>   ld/testsuite/ld-s390/weakundef-1.s  |  1 -
>>   ld/testsuite/ld-s390/weakundef-2.dd | 17 ++++++++++++
>>   ld/testsuite/ld-s390/weakundef-2.s  | 17 ++++++++++++
>>   6 files changed, 80 insertions(+), 8 deletions(-)
>>   create mode 100644 ld/testsuite/ld-s390/weakundef-2.dd
>>   create mode 100644 ld/testsuite/ld-s390/weakundef-2.s

> Other ports have simpler strategies for unresolved undefined weak symbols
> https://reviews.llvm.org/D103001
> 
> aarch64: GNU ld: rewrite the instruction to a NOP; ld.lld: branch to
> the next instruction
> mips: GNU ld: branch to the start of the text segment (?); ld.lld:
> branch to zero
> ppc32: GNU ld: rewrite the instruction to a NOP; ld.lld: branch to the
> current instruction
> ppc64: GNU ld: rewrite the instruction to a NOP; ld.lld: branch to the
> current instruction
> riscv: GNU ld: branch to the absolute zero address (with instruction rewriting)
> i386/x86_64: GNU ld/ld.lld: branch to the link-time zero address
> 
> I haven't checked the patch closely, but it seems to add quite a few lines.

I had a quick look at bfd.ld AArch64 and PPC64. As you listed they both 
replace branches with NOPs. Yet it is unclear to me how/where they deal 
with (function) pointers resolving to zero.

On s390x using a section start address greater than 4GB at link-time 
causes PC32DBL and PLT32DBL PC-relative relocations for undefined weak 
symbols that definitively resolve to zero to overflow.

So far we have identified three types of PC-relative instructions and 
decided to rewrite them as follows to not change the general behavior:
- PC-relative load address of zero are replaced by a non-PC-relative
   load address of zero. This ensures correctness of run-time checks
   for undef weak symbols that resolved to zero.
- PC-relative branches to zero are replaced by a trap. A branch to zero
   would result in an exception at run-time and so does a trap.
- PC-relative instructions that can be considered optional, such as 
PC-relative prefetch of zero, are replaced by a NOP.

While technically it might be ok to rewrite subject branches as a NOP, 
we think that using a trap is better for debugging, as a NOP might go 
undetected at run-time. Also the complexity of the logic to do so does 
not change whether to rewrite as NOP or trap.

Regards,
Jens
-- 
Jens Remus
Linux on Z Development (D3303) and z/VSE Support
+49-7031-16-1128 Office
jremus@de.ibm.com

IBM

IBM Deutschland Research & Development GmbH; Vorsitzender des 
Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der 
Gesellschaft: Böblingen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 0/2] s390: Avoid relocation overflows on undefined weak symbols
  2024-07-01 14:33   ` Jens Remus
@ 2024-07-03  0:43     ` Alan Modra
  2024-07-09 15:22       ` Jens Remus
  0 siblings, 1 reply; 10+ messages in thread
From: Alan Modra @ 2024-07-03  0:43 UTC (permalink / raw)
  To: Jens Remus; +Cc: Fangrui Song, binutils, Andreas Krebbel

On Mon, Jul 01, 2024 at 04:33:20PM +0200, Jens Remus wrote:
> I had a quick look at bfd.ld AArch64 and PPC64. As you listed they both
> replace branches with NOPs. Yet it is unclear to me how/where they deal with
> (function) pointers resolving to zero.

ppc64 does nothing special.  As with most other targets, you need to
write
  if (foo)
    foo ();
when dealing with a weak function foo that may or may not be defined
at runtime.  However, if you know the function will be resolved at
link time (static linking, or -z nodynamic-undefined-weak) then you
can just write
  foo ();
and get the same behaviour as
  if (foo)
    foo ();

So replacing a branch and link with a nop is only useful in special
cases, and of course doesn't apply if you're calling via a function
pointer.

-- 
Alan Modra

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 0/2] s390: Avoid relocation overflows on undefined weak symbols
  2024-07-03  0:43     ` Alan Modra
@ 2024-07-09 15:22       ` Jens Remus
  0 siblings, 0 replies; 10+ messages in thread
From: Jens Remus @ 2024-07-09 15:22 UTC (permalink / raw)
  To: Alan Modra; +Cc: Fangrui Song, binutils, Andreas Krebbel

Am 03.07.2024 um 02:43 schrieb Alan Modra:
> On Mon, Jul 01, 2024 at 04:33:20PM +0200, Jens Remus wrote:
>> I had a quick look at bfd.ld AArch64 and PPC64. As you listed they both
>> replace branches with NOPs. Yet it is unclear to me how/where they deal with
>> (function) pointers resolving to zero.
> 
> ppc64 does nothing special.  As with most other targets, you need to
> write
>    if (foo)
>      foo ();

On s390x both the address taking in "if (foo)" and the call of "foo ()" 
have a PC-relative relocation that may overflow for undefined weak 
symbols that definitively resolve to zero. So both instructions need to 
be rewritten.

In the ppc64 code I think I found where the call of "foo ()" is 
apparently rewritten as a NOP in bfd/elf64-ppc.c 
ppc64_elf_relocate_section (): "NOP out calls to undefined weak 
functions. ...". I did not find where the address taking in "if (foo)" 
is rewritten with a load of zero.

> when dealing with a weak function foo that may or may not be defined
> at runtime.  However, if you know the function will be resolved at
> link time (static linking, or -z nodynamic-undefined-weak) then you
> can just write
>    foo ();
> and get the same behaviour as
>    if (foo)
>      foo ();
> 
> So replacing a branch and link with a nop is only useful in special
> cases, and of course doesn't apply if you're calling via a function
> pointer.

Thank you for the explanation!

Thanks and regards,
Jens
-- 
Jens Remus
Linux on Z Development (D3303) and z/VSE Support
+49-7031-16-1128 Office
jremus@de.ibm.com

IBM

IBM Deutschland Research & Development GmbH; Vorsitzender des 
Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der 
Gesellschaft: Böblingen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 1/2] s390: Do not replace brcth referencing undefined weak symbol
  2024-06-26 15:20 ` [PATCH 1/2] s390: Do not replace brcth referencing undefined weak symbol Jens Remus
@ 2024-07-12 12:27   ` Andreas Krebbel
  0 siblings, 0 replies; 10+ messages in thread
From: Andreas Krebbel @ 2024-07-12 12:27 UTC (permalink / raw)
  To: Jens Remus, binutils

On 6/26/24 17:20, Jens Remus wrote:
> Branch Relative on Count High (brcth) is a conditional branch relative
> instruction. It is not guaranteed that it only appears within loops
> that sooner or later will take the branch. It may very well be used to
> check a condition that will prevent the branch from ever being taken.
>
> bfd/
> 	* elf64-s390.c (elf_s390_relocate_section): Do not replace brcth
> 	referencing undefined weak symbol with a trap.
>
> ld/
> 	* testsuite/ld-s390/weakundef-1.s: Update test case accordingly.
> 	* testsuite/ld-s390/weakundef-1.dd: Likewise.
>
> Fixes: 896a639babe2 ("s390: Avoid reloc overflows on undefined weak symbols")
> Signed-off-by: Jens Remus <jremus@linux.ibm.com>

Ok. Thanks!


Andreas



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 2/2] s390: Avoid reloc overflows on undefined weak symbols (cont)
  2024-06-26 15:20 ` [PATCH 2/2] s390: Avoid reloc overflows on undefined weak symbols (cont) Jens Remus
@ 2024-07-12 12:29   ` Andreas Krebbel
  0 siblings, 0 replies; 10+ messages in thread
From: Andreas Krebbel @ 2024-07-12 12:29 UTC (permalink / raw)
  To: Jens Remus, binutils; +Cc: Alexander Gordeev, Ilya Leoshkevich

On 6/26/24 17:20, Jens Remus wrote:
> This complements and reuses logic from Andreas Krebbel's commit
> 896a639babe2 ("s390: Avoid reloc overflows on undefined weak symbols").
>
> Replace relative long addressing instructions of weak symbols, which
> will definitely resolve to zero, with either a load address of 0 or a
> a trapping insn.
>
> This prevents the PLT32DBL relocation from overflowing in case the
> binary will be loaded at 4GB or more.
>
> bfd/
> 	* elf64-s390.c (elf_s390_relocate_section): Replace
> 	instructions using undefined weak symbols with relative
> 	addressing to avoid relocation overflows.
>
> ld/
> 	* testsuite/ld-s390/s390.exp: Add new test.
> 	* testsuite/ld-s390/weakundef-2.s: New test.
> 	* testsuite/ld-s390/weakundef-2.dd: Likewise.
>
> Reported-by: Alexander Gordeev <agordeev@linux.ibm.com>
> Suggested-by: Ilya Leoshkevich <iii@linux.ibm.com>
> Suggested-by: Andreas Krebbel <krebbel@linux.ibm.com>
> Signed-off-by: Jens Remus <jremus@linux.ibm.com>

Ok. Thanks!


Andreas


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 0/2] s390: Avoid relocation overflows on undefined weak symbols
  2024-06-26 15:20 [PATCH 0/2] s390: Avoid relocation overflows on undefined weak symbols Jens Remus
                   ` (2 preceding siblings ...)
  2024-06-26 18:26 ` [PATCH 0/2] s390: Avoid relocation overflows on undefined weak symbols Fangrui Song
@ 2024-07-12 14:58 ` Jens Remus
  3 siblings, 0 replies; 10+ messages in thread
From: Jens Remus @ 2024-07-12 14:58 UTC (permalink / raw)
  To: binutils; +Cc: Andreas Krebbel

Am 26.06.2024 um 17:20 schrieb Jens Remus:
> Patch 1 corrects commit 896a639babe2 ("s390: Avoid reloc overflows on
> undefined weak symbols") not to replace Branch Relative on Count High
> (brcth) referencing an undefined weak symbol definitively resolving to
> zero by a trap, as it is not guaranteed that the conditional branch is
> taken in any case.
> 
> Patch 2 complements commit 896a639babe2 ("s390: Avoid reloc overflows
> on undefined weak symbols") by applying similar replacements of
> instructions referencing undefined weak symbols that definitively
> resolve to zero. This time for PLT32DBL relocations.
...

> Jens Remus (2):
>    s390: Do not replace brcth referencing undefined weak symbol
>    s390: Avoid reloc overflows on undefined weak symbols (cont)
...

Committed to mainline with Andreas' approval.

Regards,
Jens
-- 
Jens Remus
Linux on Z Development (D3303) and z/VSE Support
+49-7031-16-1128 Office
jremus@de.ibm.com

IBM

IBM Deutschland Research & Development GmbH; Vorsitzender des 
Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der 
Gesellschaft: Böblingen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2024-07-12 14:59 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-26 15:20 [PATCH 0/2] s390: Avoid relocation overflows on undefined weak symbols Jens Remus
2024-06-26 15:20 ` [PATCH 1/2] s390: Do not replace brcth referencing undefined weak symbol Jens Remus
2024-07-12 12:27   ` Andreas Krebbel
2024-06-26 15:20 ` [PATCH 2/2] s390: Avoid reloc overflows on undefined weak symbols (cont) Jens Remus
2024-07-12 12:29   ` Andreas Krebbel
2024-06-26 18:26 ` [PATCH 0/2] s390: Avoid relocation overflows on undefined weak symbols Fangrui Song
2024-07-01 14:33   ` Jens Remus
2024-07-03  0:43     ` Alan Modra
2024-07-09 15:22       ` Jens Remus
2024-07-12 14:58 ` Jens Remus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).