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