fa3bfd
Backport of this upstream patch:
fa3bfd
fa3bfd
commit 031e519c95c069abe4e4c7c59e2b4b67efccdee5
fa3bfd
Author: H.J. Lu <hjl.tools@gmail.com>
fa3bfd
Date:   Thu Jul 6 04:43:06 2017 -0700
fa3bfd
fa3bfd
    x86-64: Align the stack in __tls_get_addr [BZ #21609]
fa3bfd
    
fa3bfd
    This change forces realignment of the stack pointer in __tls_get_addr, so
fa3bfd
    that binaries compiled by GCCs older than GCC 4.9:
fa3bfd
    
fa3bfd
    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
fa3bfd
    
fa3bfd
    continue to work even if vector instructions are used in glibc which
fa3bfd
    require the ABI stack realignment.
fa3bfd
    
fa3bfd
    __tls_get_addr_slow is added to handle the slow paths in the default
fa3bfd
    implementation of__tls_get_addr in elf/dl-tls.c.  The new __tls_get_addr
fa3bfd
    calls __tls_get_addr_slow after realigning the stack.  Internal calls
fa3bfd
    within ld.so go directly to the default implementation of __tls_get_addr
fa3bfd
    because they do not need stack realignment.
fa3bfd
    
fa3bfd
            [BZ #21609]
fa3bfd
            * sysdeps/x86_64/Makefile (sysdep-dl-routines): Add tls_get_addr.
fa3bfd
            (gen-as-const-headers): Add rtld-offsets.sym.
fa3bfd
            * sysdeps/x86_64/dl-tls.c: New file.
fa3bfd
            * sysdeps/x86_64/rtld-offsets.sym: Likwise.
fa3bfd
            * sysdeps/x86_64/tls_get_addr.S: Likewise.
fa3bfd
            * sysdeps/x86_64/dl-tls.h: Add multiple inclusion guards.
fa3bfd
            * sysdeps/x86_64/tlsdesc.sym (TI_MODULE_OFFSET): New.
fa3bfd
            (TI_OFFSET_OFFSET): Likwise.
fa3bfd
fa3bfd
Adjusted for drift in sysdeps/x86_64/Makefile and lack of
fa3bfd
TLS_DTV_UNALLOCATED consolidation (upstream commit
fa3bfd
aca1daef298b43bd7b1987b31f5aabcf6c2f6021).
fa3bfd
fa3bfd
diff --git a/sysdeps/x86_64/Makefile b/sysdeps/x86_64/Makefile
fa3bfd
index 6c03a89fb3ea3063..c6766bb2b443a28a 100644
fa3bfd
--- a/sysdeps/x86_64/Makefile
fa3bfd
+++ b/sysdeps/x86_64/Makefile
fa3bfd
@@ -19,7 +19,7 @@ gen-as-const-headers += locale-defines.sym
fa3bfd
 endif
fa3bfd
 
fa3bfd
 ifeq ($(subdir),elf)
fa3bfd
-sysdep-dl-routines += tlsdesc dl-tlsdesc
fa3bfd
+sysdep-dl-routines += tlsdesc dl-tlsdesc tls_get_addr
fa3bfd
 
fa3bfd
 tests += ifuncmain8
fa3bfd
 modules-names += ifuncmod8
fa3bfd
@@ -69,5 +69,5 @@ endif
fa3bfd
 endif
fa3bfd
 
fa3bfd
 ifeq ($(subdir),csu)
fa3bfd
-gen-as-const-headers += tlsdesc.sym
fa3bfd
+gen-as-const-headers += tlsdesc.sym rtld-offsets.sym
fa3bfd
 endif
fa3bfd
diff --git a/sysdeps/x86_64/dl-tls.c b/sysdeps/x86_64/dl-tls.c
fa3bfd
new file mode 100644
fa3bfd
index 0000000000000000..3584805c8ecca59a
fa3bfd
--- /dev/null
fa3bfd
+++ b/sysdeps/x86_64/dl-tls.c
fa3bfd
@@ -0,0 +1,53 @@
fa3bfd
+/* Thread-local storage handling in the ELF dynamic linker.  x86-64 version.
fa3bfd
+   Copyright (C) 2017 Free Software Foundation, Inc.
fa3bfd
+   This file is part of the GNU C Library.
fa3bfd
+
fa3bfd
+   The GNU C Library is free software; you can redistribute it and/or
fa3bfd
+   modify it under the terms of the GNU Lesser General Public
fa3bfd
+   License as published by the Free Software Foundation; either
fa3bfd
+   version 2.1 of the License, or (at your option) any later version.
fa3bfd
+
fa3bfd
+   The GNU C Library is distributed in the hope that it will be useful,
fa3bfd
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
fa3bfd
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
fa3bfd
+   Lesser General Public License for more details.
fa3bfd
+
fa3bfd
+   You should have received a copy of the GNU Lesser General Public
fa3bfd
+   License along with the GNU C Library; if not, see
fa3bfd
+   <http://www.gnu.org/licenses/>.  */
fa3bfd
+
fa3bfd
+#ifdef SHARED
fa3bfd
+/* Work around GCC PR58066, due to which __tls_get_addr may be called
fa3bfd
+   with an unaligned stack.  The compat implementation is in
fa3bfd
+   tls_get_addr-compat.S.  */
fa3bfd
+
fa3bfd
+# include <dl-tls.h>
fa3bfd
+
fa3bfd
+/* Define __tls_get_addr within elf/dl-tls.c under a different
fa3bfd
+   name.  */
fa3bfd
+extern __typeof__ (__tls_get_addr) ___tls_get_addr;
fa3bfd
+
fa3bfd
+# define __tls_get_addr ___tls_get_addr
fa3bfd
+# include <elf/dl-tls.c>
fa3bfd
+# undef __tls_get_addr
fa3bfd
+
fa3bfd
+hidden_ver (___tls_get_addr, __tls_get_addr)
fa3bfd
+
fa3bfd
+/* Only handle slow paths for __tls_get_addr.  */
fa3bfd
+attribute_hidden
fa3bfd
+void *
fa3bfd
+__tls_get_addr_slow (GET_ADDR_ARGS)
fa3bfd
+{
fa3bfd
+  dtv_t *dtv = THREAD_DTV ();
fa3bfd
+
fa3bfd
+  if (__glibc_unlikely (dtv[0].counter != GL(dl_tls_generation)))
fa3bfd
+    return update_get_addr (GET_ADDR_PARAM);
fa3bfd
+
fa3bfd
+  return tls_get_addr_tail (GET_ADDR_PARAM, dtv, NULL);
fa3bfd
+}
fa3bfd
+#else
fa3bfd
+
fa3bfd
+/* No compatibility symbol needed.  */
fa3bfd
+# include <elf/dl-tls.c>
fa3bfd
+
fa3bfd
+#endif
fa3bfd
diff --git a/sysdeps/x86_64/dl-tls.h b/sysdeps/x86_64/dl-tls.h
fa3bfd
index 56162ee64a4aae7d..0b4a6b3b634a83f4 100644
fa3bfd
--- a/sysdeps/x86_64/dl-tls.h
fa3bfd
+++ b/sysdeps/x86_64/dl-tls.h
fa3bfd
@@ -16,6 +16,10 @@
fa3bfd
    License along with the GNU C Library; if not, see
fa3bfd
    <http://www.gnu.org/licenses/>.  */
fa3bfd
 
fa3bfd
+#ifndef _X86_64_DL_TLS_H
fa3bfd
+#define _X86_64_DL_TLS_H
fa3bfd
+
fa3bfd
+#include <stdint.h>
fa3bfd
 
fa3bfd
 /* Type used for the representation of TLS information in the GOT.  */
fa3bfd
 typedef struct dl_tls_index
fa3bfd
@@ -29,3 +33,5 @@ extern void *__tls_get_addr (tls_index *ti);
fa3bfd
 
fa3bfd
 /* Value used for dtv entries for which the allocation is delayed.  */
fa3bfd
 #define TLS_DTV_UNALLOCATED	((void *) -1l)
fa3bfd
+
fa3bfd
+#endif /* _X86_64_DL_TLS_H */
fa3bfd
diff --git a/sysdeps/x86_64/rtld-offsets.sym b/sysdeps/x86_64/rtld-offsets.sym
fa3bfd
new file mode 100644
fa3bfd
index 0000000000000000..fd41b51521ac80bd
fa3bfd
--- /dev/null
fa3bfd
+++ b/sysdeps/x86_64/rtld-offsets.sym
fa3bfd
@@ -0,0 +1,6 @@
fa3bfd
+#define SHARED
fa3bfd
+#include <ldsodefs.h>
fa3bfd
+
fa3bfd
+--
fa3bfd
+
fa3bfd
+GL_TLS_GENERATION_OFFSET        offsetof (struct rtld_global, _dl_tls_generation)
fa3bfd
diff --git a/sysdeps/x86_64/tls_get_addr.S b/sysdeps/x86_64/tls_get_addr.S
fa3bfd
new file mode 100644
fa3bfd
index 0000000000000000..9d38fb3be54fbcb5
fa3bfd
--- /dev/null
fa3bfd
+++ b/sysdeps/x86_64/tls_get_addr.S
fa3bfd
@@ -0,0 +1,61 @@
fa3bfd
+/* Stack-aligning implementation of __tls_get_addr.  x86-64 version.
fa3bfd
+   Copyright (C) 2017 Free Software Foundation, Inc.
fa3bfd
+   This file is part of the GNU C Library.
fa3bfd
+
fa3bfd
+   The GNU C Library is free software; you can redistribute it and/or
fa3bfd
+   modify it under the terms of the GNU Lesser General Public
fa3bfd
+   License as published by the Free Software Foundation; either
fa3bfd
+   version 2.1 of the License, or (at your option) any later version.
fa3bfd
+
fa3bfd
+   The GNU C Library is distributed in the hope that it will be useful,
fa3bfd
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
fa3bfd
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
fa3bfd
+   Lesser General Public License for more details.
fa3bfd
+
fa3bfd
+   You should have received a copy of the GNU Lesser General Public
fa3bfd
+   License along with the GNU C Library; if not, see
fa3bfd
+   <http://www.gnu.org/licenses/>.  */
fa3bfd
+
fa3bfd
+#ifdef SHARED
fa3bfd
+
fa3bfd
+# include <sysdep.h>
fa3bfd
+# include "tlsdesc.h"
fa3bfd
+# include "rtld-offsets.h"
fa3bfd
+
fa3bfd
+/* See __tls_get_addr and __tls_get_addr_slow in dl-tls.c.  This function
fa3bfd
+   call __tls_get_addr_slow on both slow paths.  It realigns the stack
fa3bfd
+   before the call to work around GCC PR58066.  */
fa3bfd
+
fa3bfd
+ENTRY (__tls_get_addr)
fa3bfd
+	mov 	%fs:DTV_OFFSET, %RDX_LP
fa3bfd
+	mov	GL_TLS_GENERATION_OFFSET+_rtld_local(%rip), %RAX_LP
fa3bfd
+	/* GL(dl_tls_generation) == dtv[0].counter */
fa3bfd
+	cmp	%RAX_LP, (%rdx)
fa3bfd
+	jne	1f
fa3bfd
+	mov	TI_MODULE_OFFSET(%rdi), %RAX_LP
fa3bfd
+	/* dtv[ti->ti_module] */
fa3bfd
+# ifdef __LP64__
fa3bfd
+	salq	$4, %rax
fa3bfd
+	movq	(%rdx,%rax), %rax
fa3bfd
+# else
fa3bfd
+	movl	(%rdx,%rax, 8), %eax
fa3bfd
+# endif
fa3bfd
+	cmp	$-1, %RAX_LP
fa3bfd
+	je	1f
fa3bfd
+	add	TI_OFFSET_OFFSET(%rdi), %RAX_LP
fa3bfd
+	ret
fa3bfd
+1:
fa3bfd
+	/* On the slow path, align the stack.  */
fa3bfd
+	pushq	%rbp
fa3bfd
+	cfi_def_cfa_offset (16)
fa3bfd
+	cfi_offset (%rbp, -16)
fa3bfd
+	mov	%RSP_LP, %RBP_LP
fa3bfd
+	cfi_def_cfa_register (%rbp)
fa3bfd
+	and	$-16, %RSP_LP
fa3bfd
+	call	__tls_get_addr_slow
fa3bfd
+	mov	%RBP_LP, %RSP_LP
fa3bfd
+	popq	%rbp
fa3bfd
+	cfi_def_cfa (%rsp, 8)
fa3bfd
+	ret
fa3bfd
+END (__tls_get_addr)
fa3bfd
+#endif /* SHARED */
fa3bfd
diff --git a/sysdeps/x86_64/tlsdesc.sym b/sysdeps/x86_64/tlsdesc.sym
fa3bfd
index 33854975d04184b2..fc897ab4b522b1a9 100644
fa3bfd
--- a/sysdeps/x86_64/tlsdesc.sym
fa3bfd
+++ b/sysdeps/x86_64/tlsdesc.sym
fa3bfd
@@ -15,3 +15,6 @@ TLSDESC_ARG			offsetof(struct tlsdesc, arg)
fa3bfd
 TLSDESC_GEN_COUNT		offsetof(struct tlsdesc_dynamic_arg, gen_count)
fa3bfd
 TLSDESC_MODID			offsetof(struct tlsdesc_dynamic_arg, tlsinfo.ti_module)
fa3bfd
 TLSDESC_MODOFF			offsetof(struct tlsdesc_dynamic_arg, tlsinfo.ti_offset)
fa3bfd
+
fa3bfd
+TI_MODULE_OFFSET 		offsetof(tls_index, ti_module)
fa3bfd
+TI_OFFSET_OFFSET 		offsetof(tls_index, ti_offset)