]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
elf, nptl: Initialize static TLS directly in ld.so
authorFlorian Weimer <fweimer@redhat.com>
Wed, 5 May 2021 04:20:31 +0000 (06:20 +0200)
committerFlorian Weimer <fweimer@redhat.com>
Wed, 5 May 2021 04:20:31 +0000 (06:20 +0200)
The stack list is available in ld.so since commit
1daccf403b1bd86370eb94edca794dc106d02039 ("nptl: Move stack list
variables into _rtld_global"), so it's possible to walk the stack
list directly in ld.so and perform the initialization there.

This eliminates an unprotected function pointer from _rtld_global
and reduces the libpthread initialization code.

elf/dl-open.c
elf/dl-reloc.c
elf/dl-support.c
elf/dl-tls.c
elf/rtld.c
nptl/allocatestack.c
nptl/nptl-init.c
nptl/pthreadP.h
sysdeps/generic/ldsodefs.h

index ab7aaa345eb36cf84d4858dcd69abee9827f5617..09f0df7d38cebd22f1eea7c36c2c79e7aff4ec8e 100644 (file)
@@ -426,7 +426,7 @@ TLS generation counter wrapped!  Please report this."));
          _dl_update_slotinfo (imap->l_tls_modid);
 #endif
 
-         GL(dl_init_static_tls) (imap);
+         dl_init_static_tls (imap);
          assert (imap->l_need_tls_init == 0);
        }
     }
index c2df26deea48c0ae85123c49c96f022b6a548049..bb9ca1a101105e20a835f6b80e78e57ff0ec0ff9 100644 (file)
@@ -118,7 +118,7 @@ _dl_try_allocate_static_tls (struct link_map *map, bool optional)
        (void) _dl_update_slotinfo (map->l_tls_modid);
 #endif
 
-      GL(dl_init_static_tls) (map);
+      dl_init_static_tls (map);
     }
   else
     map->l_need_tls_init = 1;
@@ -141,6 +141,7 @@ cannot allocate memory in static TLS block"));
     }
 }
 
+#if !THREAD_GSCOPE_IN_TCB
 /* Initialize static TLS area and DTV for current (only) thread.
    libpthread implementations should provide their own hook
    to handle all threads.  */
@@ -159,7 +160,7 @@ _dl_nothread_init_static_tls (struct link_map *map)
   memset (__mempcpy (dest, map->l_tls_initimage, map->l_tls_initimage_size),
          '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
 }
-
+#endif /* !THREAD_GSCOPE_IN_TCB */
 
 void
 _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[],
index 7fc2ee78e2af5abdee6ea6ae8f2f220673eaae89..f966a2e7cd586e0e7244934d17ee572211bd816d 100644 (file)
@@ -138,8 +138,6 @@ void *_dl_random;
 #include <dl-procruntime.c>
 #include <dl-procinfo.c>
 
-void (*_dl_init_static_tls) (struct link_map *) = &_dl_nothread_init_static_tls;
-
 size_t _dl_pagesize = EXEC_PAGESIZE;
 
 size_t _dl_minsigstacksize = CONSTANT_MINSIGSTKSZ;
@@ -197,6 +195,7 @@ list_t _dl_stack_user;
 int _dl_stack_cache_lock;
 #else
 int _dl_thread_gscope_count;
+void (*_dl_init_static_tls) (struct link_map *) = &_dl_nothread_init_static_tls;
 #endif
 struct dl_scope_free_list *_dl_scope_free_list;
 
index f8b32b3ecbd19e6f0cda6780df3c3e0b9381c877..6baff0c1eae9bd417711e68d123b895b6d408407 100644 (file)
 #include <dl-tls.h>
 #include <ldsodefs.h>
 
+#if THREAD_GSCOPE_IN_TCB
+# include <list.h>
+#endif
+
 #define TUNABLE_NAMESPACE rtld
 #include <dl-tunables.h>
 
@@ -1005,3 +1009,38 @@ cannot create TLS data structures"));
       listp->slotinfo[idx].gen = GL(dl_tls_generation) + 1;
     }
 }
+
+#if THREAD_GSCOPE_IN_TCB
+static inline void __attribute__((always_inline))
+init_one_static_tls (struct pthread *curp, struct link_map *map)
+{
+# if TLS_TCB_AT_TP
+  void *dest = (char *) curp - map->l_tls_offset;
+# elif TLS_DTV_AT_TP
+  void *dest = (char *) curp + map->l_tls_offset + TLS_PRE_TCB_SIZE;
+# else
+#  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
+# endif
+
+  /* Initialize the memory.  */
+  memset (__mempcpy (dest, map->l_tls_initimage, map->l_tls_initimage_size),
+         '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
+}
+
+void
+_dl_init_static_tls (struct link_map *map)
+{
+  lll_lock (GL (dl_stack_cache_lock), LLL_PRIVATE);
+
+  /* Iterate over the list with system-allocated threads first.  */
+  list_t *runp;
+  list_for_each (runp, &GL (dl_stack_used))
+    init_one_static_tls (list_entry (runp, struct pthread, list), map);
+
+  /* Now the list with threads using user-allocated stacks.  */
+  list_for_each (runp, &GL (dl_stack_user))
+    init_one_static_tls (list_entry (runp, struct pthread, list), map);
+
+  lll_unlock (GL (dl_stack_cache_lock), LLL_PRIVATE);
+}
+#endif /* THREAD_GSCOPE_IN_TCB */
index 34879016ad595ee94206988ce52839e1f2865473..ad325d4c107cb041b45bec0f4be3c49301a9ab23 100644 (file)
@@ -1139,7 +1139,9 @@ dl_main (const ElfW(Phdr) *phdr,
   struct dl_main_state state;
   dl_main_state_init (&state);
 
+#if !THREAD_GSCOPE_IN_TCB
   GL(dl_init_static_tls) = &_dl_nothread_init_static_tls;
+#endif
 
 #if defined SHARED && defined _LIBC_REENTRANT \
     && defined __rtld_lock_default_lock_recursive
index f1270c310943232afcc0b68929d8134545c94288..8aaba088b1c0dd298be77e0b892ad9b6b9e78997 100644 (file)
@@ -951,38 +951,3 @@ __reclaim_stacks (void)
   GL (dl_stack_cache_lock) = LLL_LOCK_INITIALIZER;
   __default_pthread_attr_lock = LLL_LOCK_INITIALIZER;
 }
-
-
-static inline void __attribute__((always_inline))
-init_one_static_tls (struct pthread *curp, struct link_map *map)
-{
-# if TLS_TCB_AT_TP
-  void *dest = (char *) curp - map->l_tls_offset;
-# elif TLS_DTV_AT_TP
-  void *dest = (char *) curp + map->l_tls_offset + TLS_PRE_TCB_SIZE;
-# else
-#  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
-# endif
-
-  /* Initialize the memory.  */
-  memset (__mempcpy (dest, map->l_tls_initimage, map->l_tls_initimage_size),
-         '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
-}
-
-void
-attribute_hidden
-__pthread_init_static_tls (struct link_map *map)
-{
-  lll_lock (GL (dl_stack_cache_lock), LLL_PRIVATE);
-
-  /* Iterate over the list with system-allocated threads first.  */
-  list_t *runp;
-  list_for_each (runp, &GL (dl_stack_used))
-    init_one_static_tls (list_entry (runp, struct pthread, list), map);
-
-  /* Now the list with threads using user-allocated stacks.  */
-  list_for_each (runp, &GL (dl_stack_user))
-    init_one_static_tls (list_entry (runp, struct pthread, list), map);
-
-  lll_unlock (GL (dl_stack_cache_lock), LLL_PRIVATE);
-}
index b0879bd87ee62a8bbea87918ff1cfeab8972cbfe..fcab5a09045aa61380261519663e8ff80d3c99ea 100644 (file)
@@ -191,8 +191,6 @@ __pthread_initialize_minimal_internal (void)
   GL(dl_make_stack_executable_hook) = &__make_stacks_executable;
 #endif
 
-  GL(dl_init_static_tls) = &__pthread_init_static_tls;
-
   /* Register the fork generation counter with the libc.  */
 #ifndef TLS_MULTIPLE_THREADS_IN_TCB
   __libc_multiple_threads_ptr =
index 00d2cfe764ee8506f64690cbea9247eae0cddb27..0ce63672c4aec96d6372beb72a0783c16d3b6685 100644 (file)
@@ -381,8 +381,6 @@ extern int __pthread_multiple_threads attribute_hidden;
 extern int *__libc_multiple_threads_ptr attribute_hidden;
 #endif
 
-extern void __pthread_init_static_tls (struct link_map *) attribute_hidden;
-
 extern size_t __pthread_get_minstack (const pthread_attr_t *attr);
 
 /* Namespace save aliases.  */
index 67c66860150a3f0f40f537638be381351df13e2a..1b064c5894f1607bddc42ccc7d8b7f53cdff8d93 100644 (file)
@@ -464,7 +464,9 @@ struct rtld_global
   /* Generation counter for the dtv.  */
   EXTERN size_t _dl_tls_generation;
 
+#if !THREAD_GSCOPE_IN_TCB
   EXTERN void (*_dl_init_static_tls) (struct link_map *);
+#endif
 
   /* Scopes to free after next THREAD_GSCOPE_WAIT ().  */
   EXTERN struct dl_scope_free_list
@@ -1270,6 +1272,23 @@ extern void _dl_non_dynamic_init (void)
 extern void _dl_aux_init (ElfW(auxv_t) *av)
      attribute_hidden;
 
+/* Initialize the static TLS space for the link map in all existing
+   threads. */
+#if THREAD_GSCOPE_IN_TCB
+void _dl_init_static_tls (struct link_map *map) attribute_hidden;
+#endif
+static inline void
+dl_init_static_tls (struct link_map *map)
+{
+#if THREAD_GSCOPE_IN_TCB
+  /* The stack list is available to ld.so, so the initialization can
+     be handled within ld.so directly.  */
+  _dl_init_static_tls (map);
+#else
+  GL (dl_init_static_tls) (map);
+#endif
+}
+
 /* Return true if the ld.so copy in this namespace is actually active
    and working.  If false, the dl_open/dlfcn hooks have to be used to
    call into the outer dynamic linker (which happens after static