d5474c
commit e156dabc766d6f6f99ce9402999eae380a3ec1f2
d5474c
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
d5474c
Date:   Mon Oct 26 15:48:01 2020 +0000
d5474c
d5474c
    aarch64: Add variant PCS lazy binding test [BZ #26798]
d5474c
    
d5474c
    This test fails without bug 26798 fixed because some integer registers
d5474c
    likely get clobbered by lazy binding and variant PCS only allows x16
d5474c
    and x17 to be clobbered at call time.
d5474c
    
d5474c
    The test requires binutils 2.32.1 or newer for handling variant PCS
d5474c
    symbols. SVE registers are not covered by this test, to avoid the
d5474c
    complexity of handling multiple compile- and runtime feature support
d5474c
    cases.
d5474c
d5474c
(Trivial textual conflicts due to lack of PAC and BTI support)
d5474c
d5474c
# Conflicts:
d5474c
#	sysdeps/aarch64/Makefile
d5474c
#	sysdeps/aarch64/configure
d5474c
#	sysdeps/aarch64/configure.ac
d5474c
d5474c
diff --git a/sysdeps/aarch64/Makefile b/sysdeps/aarch64/Makefile
d5474c
index 94baaf52dda4b801..3ec78fefc6dd5797 100644
d5474c
--- a/sysdeps/aarch64/Makefile
d5474c
+++ b/sysdeps/aarch64/Makefile
d5474c
@@ -3,6 +3,13 @@ long-double-fcts = yes
d5474c
 ifeq ($(subdir),elf)
d5474c
 sysdep-dl-routines += tlsdesc dl-tlsdesc
d5474c
 gen-as-const-headers += dl-link.sym
d5474c
+
d5474c
+ifeq (yes,$(aarch64-variant-pcs))
d5474c
+tests += tst-vpcs
d5474c
+modules-names += tst-vpcs-mod
d5474c
+LDFLAGS-tst-vpcs-mod.so = -Wl,-z,lazy
d5474c
+$(objpfx)tst-vpcs: $(objpfx)tst-vpcs-mod.so
d5474c
+endif
d5474c
 endif
d5474c
 
d5474c
 ifeq ($(subdir),csu)
d5474c
diff --git a/sysdeps/aarch64/configure b/sysdeps/aarch64/configure
d5474c
index 5bd355a6917df365..f78a79338aba1e34 100644
d5474c
--- a/sysdeps/aarch64/configure
d5474c
+++ b/sysdeps/aarch64/configure
d5474c
@@ -172,3 +172,43 @@ else
d5474c
   config_vars="$config_vars
d5474c
 default-abi = lp64"
d5474c
 fi
d5474c
+
d5474c
+# Check if binutils supports variant PCS symbols.
d5474c
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for variant PCS support" >&5
d5474c
+$as_echo_n "checking for variant PCS support... " >&6; }
d5474c
+if ${libc_cv_aarch64_variant_pcs+:} false; then :
d5474c
+  $as_echo_n "(cached) " >&6
d5474c
+else
d5474c
+    cat > conftest.S <
d5474c
+.global foo
d5474c
+.type foo, %function
d5474c
+.variant_pcs foo
d5474c
+foo:
d5474c
+	ret
d5474c
+.global bar
d5474c
+.type bar, %function
d5474c
+bar:
d5474c
+	b foo
d5474c
+EOF
d5474c
+  libc_cv_aarch64_variant_pcs=no
d5474c
+  if { ac_try='${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -nostdlib -nostartfiles $no_ssp -shared -fPIC -o conftest.so conftest.S'
d5474c
+  { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
d5474c
+  (eval $ac_try) 2>&5
d5474c
+  ac_status=$?
d5474c
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
d5474c
+  test $ac_status = 0; }; } \
d5474c
+     && { ac_try='$READELF -dW conftest.so | grep -q AARCH64_VARIANT_PCS'
d5474c
+  { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
d5474c
+  (eval $ac_try) 2>&5
d5474c
+  ac_status=$?
d5474c
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
d5474c
+  test $ac_status = 0; }; }
d5474c
+  then
d5474c
+    libc_cv_aarch64_variant_pcs=yes
d5474c
+  fi
d5474c
+  rm -rf conftest.*
d5474c
+fi
d5474c
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_aarch64_variant_pcs" >&5
d5474c
+$as_echo "$libc_cv_aarch64_variant_pcs" >&6; }
d5474c
+config_vars="$config_vars
d5474c
+aarch64-variant-pcs = $libc_cv_aarch64_variant_pcs"
d5474c
diff --git a/sysdeps/aarch64/configure.ac b/sysdeps/aarch64/configure.ac
d5474c
index 7851dd4dac345b2a..7f13bfb93b60bfd7 100644
d5474c
--- a/sysdeps/aarch64/configure.ac
d5474c
+++ b/sysdeps/aarch64/configure.ac
d5474c
@@ -20,3 +20,25 @@ if test $libc_cv_aarch64_be = yes; then
d5474c
 else
d5474c
   LIBC_CONFIG_VAR([default-abi], [lp64])
d5474c
 fi
d5474c
+
d5474c
+# Check if binutils supports variant PCS symbols.
d5474c
+AC_CACHE_CHECK([for variant PCS support], [libc_cv_aarch64_variant_pcs], [dnl
d5474c
+  cat > conftest.S <
d5474c
+.global foo
d5474c
+.type foo, %function
d5474c
+.variant_pcs foo
d5474c
+foo:
d5474c
+	ret
d5474c
+.global bar
d5474c
+.type bar, %function
d5474c
+bar:
d5474c
+	b foo
d5474c
+EOF
d5474c
+  libc_cv_aarch64_variant_pcs=no
d5474c
+  if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -nostdlib -nostartfiles $no_ssp -shared -fPIC -o conftest.so conftest.S]) \
d5474c
+     && AC_TRY_COMMAND([$READELF -dW conftest.so | grep -q AARCH64_VARIANT_PCS])
d5474c
+  then
d5474c
+    libc_cv_aarch64_variant_pcs=yes
d5474c
+  fi
d5474c
+  rm -rf conftest.*])
d5474c
+LIBC_CONFIG_VAR([aarch64-variant-pcs], [$libc_cv_aarch64_variant_pcs])
d5474c
diff --git a/sysdeps/aarch64/tst-vpcs-mod.S b/sysdeps/aarch64/tst-vpcs-mod.S
d5474c
new file mode 100644
d5474c
index 0000000000000000..b2642ba030daaca7
d5474c
--- /dev/null
d5474c
+++ b/sysdeps/aarch64/tst-vpcs-mod.S
d5474c
@@ -0,0 +1,141 @@
d5474c
+/* Record the register state before and after a variant PCS call.
d5474c
+   Copyright (C) 2020 Free Software Foundation, Inc.
d5474c
+
d5474c
+   This file is part of the GNU C Library.
d5474c
+
d5474c
+   The GNU C Library is free software; you can redistribute it and/or
d5474c
+   modify it under the terms of the GNU Lesser General Public License as
d5474c
+   published by the Free Software Foundation; either version 2.1 of the
d5474c
+   License, or (at your option) any later version.
d5474c
+
d5474c
+   The GNU C Library is distributed in the hope that it will be useful,
d5474c
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
d5474c
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d5474c
+   Lesser General Public License for more details.
d5474c
+
d5474c
+   You should have received a copy of the GNU Lesser General Public
d5474c
+   License along with the GNU C Library.  If not, see
d5474c
+   <https://www.gnu.org/licenses/>.  */
d5474c
+
d5474c
+	.variant_pcs	vpcs_call
d5474c
+	.global	vpcs_call
d5474c
+	.type	vpcs_call, %function
d5474c
+vpcs_call:
d5474c
+	.cfi_startproc
d5474c
+	hint	34 /* bti c.  */
d5474c
+
d5474c
+	/* Save register state to *x0.  */
d5474c
+	stp	x0, x1, [x0]
d5474c
+	stp	x2, x3, [x0, 16]
d5474c
+	stp	x4, x5, [x0, 32]
d5474c
+	stp	x6, x7, [x0, 48]
d5474c
+	stp	x8, x9, [x0, 64]
d5474c
+	stp	x10, x11, [x0, 80]
d5474c
+	stp	x12, x13, [x0, 96]
d5474c
+	stp	x14, x15, [x0, 112]
d5474c
+	stp	x16, x17, [x0, 128]
d5474c
+	stp	x18, x19, [x0, 144]
d5474c
+	stp	x20, x21, [x0, 160]
d5474c
+	stp	x22, x23, [x0, 176]
d5474c
+	stp	x24, x25, [x0, 192]
d5474c
+	stp	x26, x27, [x0, 208]
d5474c
+	stp	x28, x29, [x0, 224]
d5474c
+	mov	x1, sp
d5474c
+	stp	x30, x1, [x0, 240]
d5474c
+	stp	q0, q1, [x0, 256]
d5474c
+	stp	q2, q3, [x0, 288]
d5474c
+	stp	q4, q5, [x0, 320]
d5474c
+	stp	q6, q7, [x0, 352]
d5474c
+	stp	q8, q9, [x0, 384]
d5474c
+	stp	q10, q11, [x0, 416]
d5474c
+	stp	q12, q13, [x0, 448]
d5474c
+	stp	q14, q15, [x0, 480]
d5474c
+	stp	q16, q17, [x0, 512]
d5474c
+	stp	q18, q19, [x0, 544]
d5474c
+	stp	q20, q21, [x0, 576]
d5474c
+	stp	q22, q23, [x0, 608]
d5474c
+	stp	q24, q25, [x0, 640]
d5474c
+	stp	q26, q27, [x0, 672]
d5474c
+	stp	q28, q29, [x0, 704]
d5474c
+	stp	q30, q31, [x0, 736]
d5474c
+	ret
d5474c
+	.cfi_endproc
d5474c
+	.size	vpcs_call, .-vpcs_call
d5474c
+
d5474c
+	.global	vpcs_call_regs
d5474c
+	.type	vpcs_call_regs, %function
d5474c
+vpcs_call_regs:
d5474c
+	.cfi_startproc
d5474c
+	hint	34 /* bti c.  */
d5474c
+
d5474c
+	stp     x29, x30, [sp, -160]!
d5474c
+	mov     x29, sp
d5474c
+
d5474c
+	/* Save callee-saved registers.  */
d5474c
+	stp	x19, x20, [sp, 16]
d5474c
+	stp	x21, x22, [sp, 32]
d5474c
+	stp	x23, x24, [sp, 48]
d5474c
+	stp	x25, x26, [sp, 64]
d5474c
+	stp	x27, x28, [sp, 80]
d5474c
+	stp	d8, d9, [sp, 96]
d5474c
+	stp	d10, d11, [sp, 112]
d5474c
+	stp	d12, d13, [sp, 128]
d5474c
+	stp	d14, d15, [sp, 144]
d5474c
+
d5474c
+	/* Initialize most registers from *x1, and save x0, x1, x29, x30,
d5474c
+	   and sp (== x29), so *x1 contains the register state.  */
d5474c
+	stp	x0, x1, [x1]
d5474c
+	str	x29, [x1, 232]
d5474c
+	ldp	x2, x3, [x1, 16]
d5474c
+	ldp	x4, x5, [x1, 32]
d5474c
+	ldp	x6, x7, [x1, 48]
d5474c
+	ldp	x8, x9, [x1, 64]
d5474c
+	ldp	x10, x11, [x1, 80]
d5474c
+	ldp	x12, x13, [x1, 96]
d5474c
+	ldp	x14, x15, [x1, 112]
d5474c
+	ldp	x16, x17, [x1, 128]
d5474c
+	ldp	x18, x19, [x1, 144]
d5474c
+	ldp	x20, x21, [x1, 160]
d5474c
+	ldp	x22, x23, [x1, 176]
d5474c
+	ldp	x24, x25, [x1, 192]
d5474c
+	ldp	x26, x27, [x1, 208]
d5474c
+	ldr	x28, [x1, 224]
d5474c
+	/* Skip x29, x30, sp.  */
d5474c
+	ldp	q0, q1, [x1, 256]
d5474c
+	ldp	q2, q3, [x1, 288]
d5474c
+	ldp	q4, q5, [x1, 320]
d5474c
+	ldp	q6, q7, [x1, 352]
d5474c
+	ldp	q8, q9, [x1, 384]
d5474c
+	ldp	q10, q11, [x1, 416]
d5474c
+	ldp	q12, q13, [x1, 448]
d5474c
+	ldp	q14, q15, [x1, 480]
d5474c
+	ldp	q16, q17, [x1, 512]
d5474c
+	ldp	q18, q19, [x1, 544]
d5474c
+	ldp	q20, q21, [x1, 576]
d5474c
+	ldp	q22, q23, [x1, 608]
d5474c
+	ldp	q24, q25, [x1, 640]
d5474c
+	ldp	q26, q27, [x1, 672]
d5474c
+	ldp	q28, q29, [x1, 704]
d5474c
+	ldp	q30, q31, [x1, 736]
d5474c
+
d5474c
+	/* Emulate a BL using B, but save x30 before the branch.  */
d5474c
+	adr	x30, .L_return_addr
d5474c
+	stp	x30, x29, [x1, 240]
d5474c
+	b	vpcs_call
d5474c
+.L_return_addr:
d5474c
+
d5474c
+	/* Restore callee-saved registers.  */
d5474c
+	ldp	x19, x20, [sp, 16]
d5474c
+	ldp	x21, x22, [sp, 32]
d5474c
+	ldp	x23, x24, [sp, 48]
d5474c
+	ldp	x25, x26, [sp, 64]
d5474c
+	ldp	x27, x28, [sp, 80]
d5474c
+	ldp	d8, d9, [sp, 96]
d5474c
+	ldp	d10, d11, [sp, 112]
d5474c
+	ldp	d12, d13, [sp, 128]
d5474c
+	ldp	d14, d15, [sp, 144]
d5474c
+
d5474c
+	ldp     x29, x30, [sp], 160
d5474c
+	ret
d5474c
+	.cfi_endproc
d5474c
+	.size	vpcs_call_regs, .-vpcs_call_regs
d5474c
diff --git a/sysdeps/aarch64/tst-vpcs.c b/sysdeps/aarch64/tst-vpcs.c
d5474c
new file mode 100644
d5474c
index 0000000000000000..92a701eb7cdea8ac
d5474c
--- /dev/null
d5474c
+++ b/sysdeps/aarch64/tst-vpcs.c
d5474c
@@ -0,0 +1,78 @@
d5474c
+/* Test that variant PCS calls don't clobber registers with lazy binding.
d5474c
+   Copyright (C) 2020 Free Software Foundation, Inc.
d5474c
+   This file is part of the GNU C Library.
d5474c
+
d5474c
+   The GNU C Library is free software; you can redistribute it and/or
d5474c
+   modify it under the terms of the GNU Lesser General Public
d5474c
+   License as published by the Free Software Foundation; either
d5474c
+   version 2.1 of the License, or (at your option) any later version.
d5474c
+
d5474c
+   The GNU C Library is distributed in the hope that it will be useful,
d5474c
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
d5474c
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d5474c
+   Lesser General Public License for more details.
d5474c
+
d5474c
+   You should have received a copy of the GNU Lesser General Public
d5474c
+   License along with the GNU C Library; if not, see
d5474c
+   <https://www.gnu.org/licenses/>.  */
d5474c
+
d5474c
+#include <stdint.h>
d5474c
+#include <stdio.h>
d5474c
+#include <support/check.h>
d5474c
+
d5474c
+struct regs
d5474c
+{
d5474c
+  uint64_t x[32];
d5474c
+  union {
d5474c
+    long double q[32];
d5474c
+    uint64_t u[64];
d5474c
+  } v;
d5474c
+};
d5474c
+
d5474c
+/* Gives the registers in the caller and callee around a variant PCS call.
d5474c
+   Most registers are initialized from BEFORE in the caller so they can
d5474c
+   have values that likely show clobbers.  Register state extensions such
d5474c
+   as SVE is not covered here, only the base registers.  */
d5474c
+void vpcs_call_regs (struct regs *after, struct regs *before);
d5474c
+
d5474c
+static int
d5474c
+do_test (void)
d5474c
+{
d5474c
+  struct regs before, after;
d5474c
+  int err = 0;
d5474c
+
d5474c
+  unsigned char *p = (unsigned char *)&before;
d5474c
+  for (int i = 0; i < sizeof before; i++)
d5474c
+    p[i] = i & 0xff;
d5474c
+
d5474c
+  vpcs_call_regs (&after, &before);
d5474c
+
d5474c
+  for (int i = 0; i < 32; i++)
d5474c
+    if (before.x[i] != after.x[i])
d5474c
+      {
d5474c
+	if (i == 16 || i == 17)
d5474c
+	  /* Variant PCS allows clobbering x16 and x17.  */
d5474c
+	  continue;
d5474c
+	err++;
d5474c
+	printf ("x%d: before: 0x%016llx after: 0x%016llx\n",
d5474c
+	  i,
d5474c
+	  (unsigned long long)before.x[i],
d5474c
+	  (unsigned long long)after.x[i]);
d5474c
+      }
d5474c
+  for (int i = 0; i < 64; i++)
d5474c
+    if (before.v.u[i] != after.v.u[i])
d5474c
+      {
d5474c
+	err++;
d5474c
+	printf ("v%d: before: 0x%016llx %016llx after: 0x%016llx %016llx\n",
d5474c
+	  i/2,
d5474c
+	  (unsigned long long)before.v.u[2*(i/2)+1],
d5474c
+	  (unsigned long long)before.v.u[2*(i/2)],
d5474c
+	  (unsigned long long)after.v.u[2*(i/2)+1],
d5474c
+	  (unsigned long long)after.v.u[2*(i/2)]);
d5474c
+      }
d5474c
+  if (err)
d5474c
+    FAIL_EXIT1 ("The variant PCS call clobbered %d registers.\n", err);
d5474c
+  return 0;
d5474c
+}
d5474c
+
d5474c
+#include <support/test-driver.c>