d8307d
commit 82bc69c012838a381c4167c156a06f4598f34227
d8307d
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
d8307d
Date:   Thu Apr 25 15:35:35 2019 +0100
d8307d
d8307d
    aarch64: handle STO_AARCH64_VARIANT_PCS
d8307d
    
d8307d
    Avoid lazy binding of symbols that may follow a variant PCS with different
d8307d
    register usage convention from the base PCS.
d8307d
    
d8307d
    Currently the lazy binding entry code does not preserve all the registers
d8307d
    required for AdvSIMD and SVE vector calls.  Saving and restoring all
d8307d
    registers unconditionally may break existing binaries, even if they never
d8307d
    use vector calls, because of the larger stack requirement for lazy
d8307d
    resolution, which can be significant on an SVE system.
d8307d
    
d8307d
    The solution is to mark all symbols in the symbol table that may follow
d8307d
    a variant PCS so the dynamic linker can handle them specially.  In this
d8307d
    patch such symbols are always resolved at load time, not lazily.
d8307d
    
d8307d
    So currently LD_AUDIT for variant PCS symbols are not supported, for that
d8307d
    the _dl_runtime_profile entry needs to be changed e.g. to unconditionally
d8307d
    save/restore all registers (but pass down arg and retval registers to
d8307d
    pltentry/exit callbacks according to the base PCS).
d8307d
    
d8307d
    This patch also removes a __builtin_expect from the modified code because
d8307d
    the branch prediction hint did not seem useful.
d8307d
    
d8307d
            * sysdeps/aarch64/dl-dtprocnum.h: New file.
d8307d
            * sysdeps/aarch64/dl-machine.h (DT_AARCH64): Define.
d8307d
            (elf_machine_runtime_setup): Handle DT_AARCH64_VARIANT_PCS.
d8307d
            (elf_machine_lazy_rel): Check STO_AARCH64_VARIANT_PCS and bind such
d8307d
            symbols at load time.
d8307d
            * sysdeps/aarch64/linkmap.h (struct link_map_machine): Add variant_pcs.
d8307d
d8307d
diff --git a/sysdeps/aarch64/dl-dtprocnum.h b/sysdeps/aarch64/dl-dtprocnum.h
d8307d
new file mode 100644
d8307d
index 0000000000000000..4ac2adf23458e02d
d8307d
--- /dev/null
d8307d
+++ b/sysdeps/aarch64/dl-dtprocnum.h
d8307d
@@ -0,0 +1,21 @@
d8307d
+/* Configuration of lookup functions.  AArch64 version.
d8307d
+   Copyright (C) 2019 Free Software Foundation, Inc.
d8307d
+   This file is part of the GNU C Library.
d8307d
+
d8307d
+   The GNU C Library is free software; you can redistribute it and/or
d8307d
+   modify it under the terms of the GNU Lesser General Public
d8307d
+   License as published by the Free Software Foundation; either
d8307d
+   version 2.1 of the License, or (at your option) any later version.
d8307d
+
d8307d
+   The GNU C Library is distributed in the hope that it will be useful,
d8307d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
d8307d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d8307d
+   Lesser General Public License for more details.
d8307d
+
d8307d
+   You should have received a copy of the GNU Lesser General Public
d8307d
+   License along with the GNU C Library.  If not, see
d8307d
+   <http://www.gnu.org/licenses/>.  */
d8307d
+
d8307d
+/* Number of extra dynamic section entries for this architecture.  By
d8307d
+   default there are none.  */
d8307d
+#define DT_THISPROCNUM	DT_AARCH64_NUM
d8307d
diff --git a/sysdeps/aarch64/dl-machine.h b/sysdeps/aarch64/dl-machine.h
d8307d
index 4935aa7c543876db..d4494852b32b8783 100644
d8307d
--- a/sysdeps/aarch64/dl-machine.h
d8307d
+++ b/sysdeps/aarch64/dl-machine.h
d8307d
@@ -27,6 +27,9 @@
d8307d
 #include <dl-irel.h>
d8307d
 #include <cpu-features.c>
d8307d
 
d8307d
+/* Translate a processor specific dynamic tag to the index in l_info array.  */
d8307d
+#define DT_AARCH64(x) (DT_AARCH64_##x - DT_LOPROC + DT_NUM)
d8307d
+
d8307d
 /* Return nonzero iff ELF header is compatible with the running host.  */
d8307d
 static inline int __attribute__ ((unused))
d8307d
 elf_machine_matches_host (const ElfW(Ehdr) *ehdr)
d8307d
@@ -102,6 +105,10 @@ elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
d8307d
 	}
d8307d
     }
d8307d
 
d8307d
+  /* Check if STO_AARCH64_VARIANT_PCS needs to be handled.  */
d8307d
+  if (l->l_info[DT_AARCH64 (VARIANT_PCS)])
d8307d
+    l->l_mach.variant_pcs = 1;
d8307d
+
d8307d
   return lazy;
d8307d
 }
d8307d
 
d8307d
@@ -388,10 +395,37 @@ elf_machine_lazy_rel (struct link_map *map,
d8307d
   /* Check for unexpected PLT reloc type.  */
d8307d
   if (__builtin_expect (r_type == AARCH64_R(JUMP_SLOT), 1))
d8307d
     {
d8307d
-      if (__builtin_expect (map->l_mach.plt, 0) == 0)
d8307d
-	*reloc_addr += l_addr;
d8307d
-      else
d8307d
-	*reloc_addr = map->l_mach.plt;
d8307d
+      if (map->l_mach.plt == 0)
d8307d
+	{
d8307d
+	  /* Prelinking.  */
d8307d
+	  *reloc_addr += l_addr;
d8307d
+	  return;
d8307d
+	}
d8307d
+
d8307d
+      if (__glibc_unlikely (map->l_mach.variant_pcs))
d8307d
+	{
d8307d
+	  /* Check the symbol table for variant PCS symbols.  */
d8307d
+	  const Elf_Symndx symndx = ELFW (R_SYM) (reloc->r_info);
d8307d
+	  const ElfW (Sym) *symtab =
d8307d
+	    (const void *)D_PTR (map, l_info[DT_SYMTAB]);
d8307d
+	  const ElfW (Sym) *sym = &symtab[symndx];
d8307d
+	  if (__glibc_unlikely (sym->st_other & STO_AARCH64_VARIANT_PCS))
d8307d
+	    {
d8307d
+	      /* Avoid lazy resolution of variant PCS symbols.  */
d8307d
+	      const struct r_found_version *version = NULL;
d8307d
+	      if (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
d8307d
+		{
d8307d
+		  const ElfW (Half) *vernum =
d8307d
+		    (const void *)D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
d8307d
+		  version = &map->l_versions[vernum[symndx] & 0x7fff];
d8307d
+		}
d8307d
+	      elf_machine_rela (map, reloc, sym, version, reloc_addr,
d8307d
+				skip_ifunc);
d8307d
+	      return;
d8307d
+	    }
d8307d
+	}
d8307d
+
d8307d
+      *reloc_addr = map->l_mach.plt;
d8307d
     }
d8307d
   else if (__builtin_expect (r_type == AARCH64_R(TLSDESC), 1))
d8307d
     {
d8307d
diff --git a/sysdeps/aarch64/linkmap.h b/sysdeps/aarch64/linkmap.h
d8307d
index 6852f343a1efd150..dd8597470c3d2174 100644
d8307d
--- a/sysdeps/aarch64/linkmap.h
d8307d
+++ b/sysdeps/aarch64/linkmap.h
d8307d
@@ -20,4 +20,5 @@ struct link_map_machine
d8307d
 {
d8307d
   ElfW(Addr) plt;	  /* Address of .plt */
d8307d
   void *tlsdesc_table;	  /* Address of TLS descriptor hash table.  */
d8307d
+  int variant_pcs;	  /* If set, PLT calls may follow a variant PCS.  */
d8307d
 };