From b37899d34d2190ef4b454283188f22519f096048 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Sun, 19 Mar 2023 18:10:07 +0300 Subject: hurd: Only check for TLS initialization inside rtld or in static builds When glibc is built as a shared library, TLS is always initialized by the call of TLS_INIT_TP () macro made inside the dynamic loader, prior to running the main program (see dl-call_tls_init_tp.h). We can take advantage of this: we know for sure that __LIBC_NO_TLS () will evaluate to 0 in all other cases, so let the compiler know that explicitly too. Also, only define _hurd_tls_init () and TLS_INIT_TP () under the same conditions (either !SHARED or inside rtld), to statically assert that this is the case. Other than a microoptimization, this also helps with avoiding awkward sharing of the __libc_tls_initialized variable between ld.so and libc.so that we would have to do otherwise -- we know for sure that no sharing is required, simply because __libc_tls_initialized would always be set to true inside libc.so. Signed-off-by: Sergey Bugaev Message-Id: <20230319151017.531737-25-bugaevc@gmail.com> --- sysdeps/mach/hurd/x86_64/tls.h | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'sysdeps/mach/hurd/x86_64/tls.h') diff --git a/sysdeps/mach/hurd/x86_64/tls.h b/sysdeps/mach/hurd/x86_64/tls.h index cf74e1f441..da504d9c81 100644 --- a/sysdeps/mach/hurd/x86_64/tls.h +++ b/sysdeps/mach/hurd/x86_64/tls.h @@ -68,10 +68,6 @@ _Static_assert (offsetof (tcbhead_t, stack_guard) == 0x28, _Static_assert (offsetof (tcbhead_t, __private_ss) == 0x70, "split stack pointer offset"); -extern unsigned char __libc_tls_initialized; - -# define __LIBC_NO_TLS() __builtin_expect (!__libc_tls_initialized, 0) - /* The TCB can have any size and the memory following the address the thread pointer points to is unspecified. Allocate the TCB there. */ # define TLS_TCB_AT_TP 1 @@ -87,8 +83,6 @@ extern unsigned char __libc_tls_initialized; # define TCB_ALIGNMENT 64 -# define TLS_INIT_TP(descr) _hurd_tls_init ((tcbhead_t *) (descr)) - # define THREAD_SELF \ (*(tcbhead_t * __seg_fs *) offsetof (tcbhead_t, tcb)) /* Read member of the thread descriptor directly. */ @@ -174,6 +168,10 @@ _hurd_tls_new (thread_t child, tcbhead_t *tcb) i386_FSGS_BASE_STATE_COUNT); } +# if !defined (SHARED) || IS_IN (rtld) +extern unsigned char __libc_tls_initialized; +# define __LIBC_NO_TLS() __builtin_expect (!__libc_tls_initialized, 0) + static inline bool __attribute__ ((unused)) _hurd_tls_init (tcbhead_t *tcb) { @@ -184,11 +182,18 @@ _hurd_tls_init (tcbhead_t *tcb) tcb->multiple_threads = 1; err = _hurd_tls_new (self, tcb); + if (err == 0) + __libc_tls_initialized = 1; __mach_port_deallocate (__mach_task_self (), self); - __libc_tls_initialized = 1; return err == 0; } +# define TLS_INIT_TP(descr) _hurd_tls_init ((tcbhead_t *) (descr)) +# else /* defined (SHARED) && !IS_IN (rtld) */ +# define __LIBC_NO_TLS() 0 +# endif + + /* Global scope switch support. */ # define THREAD_GSCOPE_FLAG_UNUSED 0 -- cgit 1.4.1