olga / rpms / glibc

Forked from rpms/glibc 5 years ago
Clone

Blame SOURCES/glibc-rh1468807.patch

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