diff mbox series

[RS6000] Power10 libffi fixes

Message ID 20200922003011.GB5452@bubble.grove.modra.org
State New
Headers show
Series [RS6000] Power10 libffi fixes | expand

Commit Message

Alan Modra Sept. 22, 2020, 12:30 a.m. UTC
Power10 pc-relative code doesn't use or preserve r2 as a TOC pointer.
That means calling between pc-relative and TOC using code can't be
done without intervening linker stubs, and a call from TOC code to
pc-relative code must have a nop after the bl in order to restore r2.

Now the PowerPC libffi assembly code doesn't use r2 except for the
implicit use when making calls back to C, ffi_closure_helper_LINUX64
and ffi_prep_args64.  So changing the assembly to interoperate with
pc-relative code without stubs is easily done.  Controlling that is a
new built-in macro.

Upstream libffi currently has a different patch applied to work around
the power10 build failure.  I'll post a delta for upstream.
Bootstrapped and regression tested on power8, built for power10.

gcc/
	* config/rs6000/rs6000-c.c (rs6000_target_modify_macros):
	Conditionally define __PCREL__.
libffi/
	* src/powerpc/linux64.S (ffi_call_LINUX64): Don't emit global
	entry when __PCREL__.  Call using @notoc.
	(ffi_closure_LINUX64, ffi_go_closure_linux64): Likewise.

Comments

Segher Boessenkool Sept. 23, 2020, 12:16 a.m. UTC | #1
Hi!

On Tue, Sep 22, 2020 at 10:00:11AM +0930, Alan Modra wrote:
> Power10 pc-relative code doesn't use or preserve r2 as a TOC pointer.
> That means calling between pc-relative and TOC using code can't be
> done without intervening linker stubs, and a call from TOC code to
> pc-relative code must have a nop after the bl in order to restore r2.
> 
> Now the PowerPC libffi assembly code doesn't use r2 except for the
> implicit use when making calls back to C, ffi_closure_helper_LINUX64
> and ffi_prep_args64.  So changing the assembly to interoperate with
> pc-relative code without stubs is easily done.  Controlling that is a
> new built-in macro.
> 
> Upstream libffi currently has a different patch applied to work around
> the power10 build failure.  I'll post a delta for upstream.
> Bootstrapped and regression tested on power8, built for power10.
> 
> gcc/
> 	* config/rs6000/rs6000-c.c (rs6000_target_modify_macros):
> 	Conditionally define __PCREL__.

Please do that as a separate (earlier) patch (because it *is*, and to
simplify backports, etc).

> libffi/
> 	* src/powerpc/linux64.S (ffi_call_LINUX64): Don't emit global
> 	entry when __PCREL__.  Call using @notoc.
> 	(ffi_closure_LINUX64, ffi_go_closure_linux64): Likewise.

This is okay for trunk, and for backports (possibly expedited, talk
with Peter for what is wanted/needed for AT).

Thanks!


Segher
Alan Modra Sept. 24, 2020, 3:41 a.m. UTC | #2
On Tue, Sep 22, 2020 at 07:16:57PM -0500, Segher Boessenkool wrote:
> Hi!
> 
> On Tue, Sep 22, 2020 at 10:00:11AM +0930, Alan Modra wrote:
> > gcc/
> > 	* config/rs6000/rs6000-c.c (rs6000_target_modify_macros):
> > 	Conditionally define __PCREL__.
> 
> Please do that as a separate (earlier) patch (because it *is*, and to
> simplify backports, etc).

Done.

> > libffi/
> > 	* src/powerpc/linux64.S (ffi_call_LINUX64): Don't emit global
> > 	entry when __PCREL__.  Call using @notoc.
> > 	(ffi_closure_LINUX64, ffi_go_closure_linux64): Likewise.
> 
> This is okay for trunk, and for backports (possibly expedited, talk
> with Peter for what is wanted/needed for AT).

I've fixed the changelog, a comment, and added a nop after bl for the
old calls without @notoc.  While there really isn't a need for the
nops in libffi.so since the callee is hidden visibility, there is a
miniscule chance that a static libffi.a user has a very large TOC and
somehow manages to have ffi_call_LINUX64 and ffi_prep_args64 using
different TOC pointers.  In that case the linker would arrange the
call to go via a toc-adjusting stub and want to replace the nop with a
toc restore.
Alan Modra Sept. 24, 2020, 5:39 a.m. UTC | #3
On Thu, Sep 24, 2020 at 01:11:02PM +0930, Alan Modra wrote:
> I've fixed the changelog, a comment, and added a nop after bl for the
> old calls without @notoc.  While there really isn't a need for the
> nops in libffi.so since the callee is hidden visibility, there is a
> miniscule chance that a static libffi.a user has a very large TOC and
> somehow manages to have ffi_call_LINUX64 and ffi_prep_args64 using
> different TOC pointers.  In that case the linker would arrange the
> call to go via a toc-adjusting stub and want to replace the nop with a
> toc restore.

Adding those nops broke libffi, and I pushed the patch before
bootstrap finished..  So this one committed as obvious to fix the
breakage.

	* src/powerpc/linux64_closure.S (ffi_closure_LINUX64): Correct
	location of .Lret.

diff --git a/libffi/src/powerpc/linux64_closure.S b/libffi/src/powerpc/linux64_closure.S
index 3e30db36190..5663bb40223 100644
--- a/libffi/src/powerpc/linux64_closure.S
+++ b/libffi/src/powerpc/linux64_closure.S
@@ -159,15 +159,17 @@ ffi_closure_LINUX64:
 # if defined _CALL_LINUX || _CALL_ELF == 2
 #  ifdef __PCREL__
 	bl ffi_closure_helper_LINUX64@notoc
+.Lret:
 #  else
 	bl ffi_closure_helper_LINUX64
+.Lret:
 	nop
 #  endif
 # else
 	bl .ffi_closure_helper_LINUX64
+.Lret:
 	nop
 # endif
-.Lret:
 
 	# now r3 contains the return type
 	# so use it to look up in a table
diff mbox series

Patch

diff --git a/gcc/config/rs6000/rs6000-c.c b/gcc/config/rs6000/rs6000-c.c
index f5982907e90..cc1e997524e 100644
--- a/gcc/config/rs6000/rs6000-c.c
+++ b/gcc/config/rs6000/rs6000-c.c
@@ -597,6 +597,9 @@  rs6000_target_modify_macros (bool define_p, HOST_WIDE_INT flags,
   /* Tell the user if we support the MMA instructions.  */
   if ((flags & OPTION_MASK_MMA) != 0)
     rs6000_define_or_undefine_macro (define_p, "__MMA__");
+  /* Whether pc-relative code is being generated.  */
+  if ((flags & OPTION_MASK_PCREL) != 0)
+    rs6000_define_or_undefine_macro (define_p, "__PCREL__");
 }
 
 void
diff --git a/libffi/src/powerpc/linux64.S b/libffi/src/powerpc/linux64.S
index b2ae60ead6e..bfb4d2957ae 100644
--- a/libffi/src/powerpc/linux64.S
+++ b/libffi/src/powerpc/linux64.S
@@ -36,8 +36,10 @@ 
 	.cfi_startproc
 # if _CALL_ELF == 2
 ffi_call_LINUX64:
+#  ifndef __PCREL__
 	addis	%r2, %r12, .TOC.-ffi_call_LINUX64@ha
 	addi	%r2, %r2, .TOC.-ffi_call_LINUX64@l
+#  endif
 	.localentry ffi_call_LINUX64, . - ffi_call_LINUX64
 # else
 	.section	".opd","aw"
@@ -89,7 +91,11 @@  ffi_call_LINUX64:
 	/* Call ffi_prep_args64.  */
 	mr	%r4, %r1
 # if defined _CALL_LINUX || _CALL_ELF == 2
+#  ifdef __PCREL__
+	bl	ffi_prep_args64@notoc
+#  else
 	bl	ffi_prep_args64
+#  endif
 # else
 	bl	.ffi_prep_args64
 # endif
diff --git a/libffi/src/powerpc/linux64_closure.S b/libffi/src/powerpc/linux64_closure.S
index 6487d2a2970..938e86034f1 100644
--- a/libffi/src/powerpc/linux64_closure.S
+++ b/libffi/src/powerpc/linux64_closure.S
@@ -37,8 +37,10 @@ 
 	.cfi_startproc
 # if _CALL_ELF == 2
 ffi_closure_LINUX64:
+#  ifndef __PCREL__
 	addis	%r2, %r12, .TOC.-ffi_closure_LINUX64@ha
 	addi	%r2, %r2, .TOC.-ffi_closure_LINUX64@l
+#  endif
 	.localentry ffi_closure_LINUX64, . - ffi_closure_LINUX64
 # else
 	.section        ".opd","aw"
@@ -155,7 +157,11 @@  ffi_closure_LINUX64:
 
 	# make the call
 # if defined _CALL_LINUX || _CALL_ELF == 2
+#  ifdef __PCREL__
+	bl ffi_closure_helper_LINUX64@notoc
+#  else
 	bl ffi_closure_helper_LINUX64
+#  endif
 # else
 	bl .ffi_closure_helper_LINUX64
 # endif
@@ -396,8 +402,10 @@  ffi_closure_LINUX64:
 	.cfi_startproc
 # if _CALL_ELF == 2
 ffi_go_closure_linux64:
+#  ifndef __PCREL__
 	addis	%r2, %r12, .TOC.-ffi_go_closure_linux64@ha
 	addi	%r2, %r2, .TOC.-ffi_go_closure_linux64@l
+#  endif
 	.localentry ffi_go_closure_linux64, . - ffi_go_closure_linux64
 # else
 	.section        ".opd","aw"