about summary refs log tree commit diff
path: root/elf
diff options
context:
space:
mode:
authorRoland McGrath <roland@gnu.org>2002-12-04 12:30:40 +0000
committerRoland McGrath <roland@gnu.org>2002-12-04 12:30:40 +0000
commit216455bc284f57dbaf939059f815935e23456c02 (patch)
tree3ae488a18ace0ebe18d7923eae29ae65a3badada /elf
parent68dc80ca28a192d686b3190373df8468b4ce5cb8 (diff)
downloadglibc-216455bc284f57dbaf939059f815935e23456c02.tar.gz
glibc-216455bc284f57dbaf939059f815935e23456c02.tar.xz
glibc-216455bc284f57dbaf939059f815935e23456c02.zip
* sysdeps/generic/ldsodefs.h (struct rtld_global): Move all [USE_TLS]
	members to the end, so a libpthread compiled with !USE_TLS will still
	find other members properly.

	* sysdeps/i386/i486/bits/string.h (__strcpy_g): Add dummy output
	operand for DEST memory.  Fix dummy input operand to use SRC.
	Reported by Davin McCall <davmac@ozonline.com.au>.

	* sysdeps/generic/libc-tls.c (__libc_setup_tls): Account for TCB
	alignment when initializing the DTV entry.

	* elf/dl-load.c (_dl_map_object_from_fd): If we hit a TLS segment
	when TLS has not been set up, try to set it up if we can.
	* elf/tst-tls4.c: Revert last change.
	* elf/tst-tls5.c: Likewise.
	* elf/tst-tls6.c: Likewise.
	* elf/tst-tls7.c: Likewise.
	* elf/tst-tls8.c: Likewise.
	* elf/tst-tls9.c: Likewise.

	* sysdeps/generic/dl-tls.c [SHARED] (_dl_tls_setup): New function.
	* sysdeps/generic/ldsodefs.h: Declare it.
	* elf/Versions (ld: GLIBC_PRIVATE): Add it.
	* sysdeps/generic/libc-tls.c (init_slotinfo): New static inline
	function, broken out of __libc_setup_tls.
	(init_static_tls): Likewise.
	(__libc_setup_tls): Call them.
	(_dl_tls_setup): New function, uses new subroutines.

	* elf/dl-close.c (free_slotinfo): Make argument pointer to pointer.
	Clear the pointer when returning true.
	(libc_freeres_fn) [SHARED]: If GL(dl_initial_dtv) is null, free the
	first element of the slotinfo list too.

	* sysdeps/generic/dl-tls.c (_dl_determine_tlsoffset): Define only if
	[SHARED].

	* sysdeps/generic/ldsodefs.h (_dl_next_tls_modid): Declare as hidden.
	(_dl_determine_tlsoffset): Likewise.

	* elf/rtld.c (_dl_initial_error_catch_tsd): Renamed from
	startup_error_tsd, made global.
	(dl_main): Update initialization.
	* elf/dl-tsd.c: Likewise.
	* sysdeps/generic/ldsodefs.h: Declare it.
Diffstat (limited to 'elf')
-rw-r--r--elf/Versions2
-rw-r--r--elf/dl-close.c33
-rw-r--r--elf/dl-load.c56
-rw-r--r--elf/dl-tsd.c6
-rw-r--r--elf/rtld.c10
-rw-r--r--elf/tst-tls4.c8
-rw-r--r--elf/tst-tls5.c9
-rw-r--r--elf/tst-tls6.c9
-rw-r--r--elf/tst-tls7.c9
-rw-r--r--elf/tst-tls8.c9
-rw-r--r--elf/tst-tls9.c10
11 files changed, 71 insertions, 90 deletions
diff --git a/elf/Versions b/elf/Versions
index 18d007f6f1..b9e99f27fd 100644
--- a/elf/Versions
+++ b/elf/Versions
@@ -51,6 +51,6 @@ ld {
     _dl_unload_cache;
     _rtld_global; _dl_tls_symaddr; _dl_allocate_tls; _dl_deallocate_tls;
     _dl_get_tls_static_info; _dl_allocate_tls_init;
-    _dl_get_origin;
+    _dl_get_origin; _dl_tls_setup;
   }
 }
diff --git a/elf/dl-close.c b/elf/dl-close.c
index b5c2841d61..0a6265f142 100644
--- a/elf/dl-close.c
+++ b/elf/dl-close.c
@@ -429,28 +429,28 @@ libc_hidden_def (_dl_close)
 
 #ifdef USE_TLS
 static bool
-free_slotinfo (struct dtv_slotinfo_list *elemp)
+free_slotinfo (struct dtv_slotinfo_list **elemp)
 {
   size_t cnt;
 
-  if (elemp == NULL)
+  if (*elemp == NULL)
     /* Nothing here, all is removed (or there never was anything).  */
     return true;
 
-  if (!free_slotinfo (elemp->next))
+  if (!free_slotinfo (&(*elemp)->next))
     /* We cannot free the entry.  */
     return false;
 
-  /* The least we could do is remove next element (if there was any).  */
-  elemp->next = NULL;
+  /* That cleared our next pointer for us.  */
 
-  for (cnt = 0; cnt < elemp->len; ++cnt)
-    if (elemp->slotinfo[cnt].map != NULL)
+  for (cnt = 0; cnt < (*elemp)->len; ++cnt)
+    if ((*elemp)->slotinfo[cnt].map != NULL)
       /* Still used.  */
       return false;
 
   /* We can remove the list element.  */
-  free (elemp);
+  free (*elemp);
+  *elemp = NULL;
 
   return true;
 }
@@ -479,12 +479,17 @@ libc_freeres_fn (free_mem)
   if (USE___THREAD || GL(dl_tls_dtv_slotinfo_list) != NULL)
     {
       /* Free the memory allocated for the dtv slotinfo array.  We can do
-	 this only if all modules which used this memory are unloaded.
-	 Also, the first element of the list does not have to be
-	 deallocated.  It was allocated in the dynamic linker (i.e., with
-	 a different malloc).  */
-      if (free_slotinfo (GL(dl_tls_dtv_slotinfo_list)->next))
-	GL(dl_tls_dtv_slotinfo_list)->next = NULL;
+	 this only if all modules which used this memory are unloaded.  */
+# ifdef SHARED
+      if (GL(dl_initial_dtv) == NULL)
+	/* There was no initial TLS setup, it was set up later when
+	   it used the normal malloc.  */
+	free_slotinfo (&GL(dl_tls_dtv_slotinfo_list));
+# endif
+      /* The first element of the list does not have to be deallocated.
+	 It was allocated in the dynamic linker (i.e., with a different
+	 malloc), and in the static library it's in .bss space.  */
+      free_slotinfo (&GL(dl_tls_dtv_slotinfo_list)->next);
     }
 #endif
 }
diff --git a/elf/dl-load.c b/elf/dl-load.c
index c550204ac5..d80f30813f 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -946,32 +946,64 @@ _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp,
 	    /* Nothing to do for an empty segment.  */
 	    break;
 
+	  l->l_tls_blocksize = ph->p_memsz;
+	  l->l_tls_align = ph->p_align;
+	  l->l_tls_initimage_size = ph->p_filesz;
+	  /* Since we don't know the load address yet only store the
+	     offset.  We will adjust it later.  */
+	  l->l_tls_initimage = (void *) ph->p_vaddr;
+
 	  /* If not loading the initial set of shared libraries,
 	     check whether we should permit loading a TLS segment.  */
-	  if (
-# ifdef SHARED
-	      __builtin_expect (l->l_type == lt_library, 1) ||
-# endif
+	  if (__builtin_expect (l->l_type == lt_library, 1)
 	      /* If GL(dl_tls_max_dtv_idx) == 0, then rtld.c did not
 		 set up TLS data structures, so don't use them now.  */
-	      __builtin_expect (GL(dl_tls_max_dtv_idx), 1) != 0)
+	      || __builtin_expect (GL(dl_tls_max_dtv_idx), 1) != 0)
 	    {
-	      l->l_tls_blocksize = ph->p_memsz;
-	      l->l_tls_align = ph->p_align;
-	      l->l_tls_initimage_size = ph->p_filesz;
-	      /* Since we don't know the load address yet only store the
-		 offset.  We will adjust it later.  */
-	      l->l_tls_initimage = (void *) ph->p_vaddr;
-
 	      /* Assign the next available module ID.  */
 	      l->l_tls_modid = _dl_next_tls_modid ();
 	      break;
 	    }
 
+# ifdef SHARED
 	  if (l->l_prev == NULL)
 	    /* We are loading the executable itself when the dynamic linker
 	       was executed directly.  The setup will happen later.  */
 	    break;
+
+	  /* In a static binary there is no way to tell if we dynamically
+	     loaded libpthread.  */
+	  if (GL(dl_error_catch_tsd) == &_dl_initial_error_catch_tsd)
+# endif
+	    {
+	      /* We have not yet loaded libpthread.
+		 We can do the TLS setup right now!  */
+
+	      void *tcb;
+
+	      /* The first call allocates TLS bookkeeping data structures.
+		 Then we allocate the TCB for the initial thread.  */
+	      if (__builtin_expect (_dl_tls_setup (), 0)
+		  || __builtin_expect ((tcb = _dl_allocate_tls (NULL)) == NULL,
+				       0))
+		{
+		  errval = ENOMEM;
+		  errstring = N_("\
+cannot allocate TLS data structures for initial thread");
+		  goto call_lose;
+		}
+
+	      /* Now we install the TCB in the thread register.  */
+	      if (__builtin_expect (TLS_INIT_TP (tcb, 0), 0) != -1)
+		{
+		  /* Now we are all good.  */
+		  l->l_tls_modid = ++GL(dl_tls_max_dtv_idx);
+		  break;
+		}
+
+	      /* The kernel is too old or somesuch.  */
+	      _dl_deallocate_tls (tcb, 1);
+	    }
 #endif
 
 	  /* Uh-oh, the binary expects TLS support but we cannot
diff --git a/elf/dl-tsd.c b/elf/dl-tsd.c
index c60cd1a760..f44fa7d365 100644
--- a/elf/dl-tsd.c
+++ b/elf/dl-tsd.c
@@ -27,8 +27,8 @@
 /* _dl_error_catch_tsd points to this for the single-threaded case.
    It's reset by the thread library for multithreaded programs
    if we're not using __thread.  */
-static void ** __attribute__ ((const))
-startup_error_tsd (void)
+void ** __attribute__ ((const))
+_dl_initial_error_catch_tsd (void)
 {
 #  if USE___THREAD
   static __thread void *data;
@@ -38,7 +38,7 @@ startup_error_tsd (void)
   return &data;
 }
 void **(*_dl_error_catch_tsd) (void) __attribute__ ((const))
-     = &startup_error_tsd;
+     = &_dl_initial_error_catch_tsd;
 
 # elif USE___THREAD
 
diff --git a/elf/rtld.c b/elf/rtld.c
index 6e20e15fff..f1886431b8 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -563,8 +563,8 @@ match_version (const char *string, struct link_map *map)
 #ifdef _LIBC_REENTRANT
 /* _dl_error_catch_tsd points to this for the single-threaded case.
    It's reset by the thread library for multithreaded programs.  */
-static void ** __attribute__ ((const))
-startup_error_tsd (void)
+void ** __attribute__ ((const))
+_dl_initial_error_catch_tsd (void)
 {
   static void *data;
   return &data;
@@ -602,7 +602,7 @@ dl_main (const ElfW(Phdr) *phdr,
 
 #ifdef _LIBC_REENTRANT
   /* Explicit initialization since the reloc would just be more work.  */
-  GL(dl_error_catch_tsd) = &startup_error_tsd;
+  GL(dl_error_catch_tsd) = &_dl_initial_error_catch_tsd;
 #endif
 
   /* Process the environment variable which control the behaviour.  */
@@ -1180,9 +1180,7 @@ of this helper program; chances are you did not intend to run this program.\n\
 	  slotinfo[++i].map = l;
       assert (i == GL(dl_tls_max_dtv_idx));
 
-      /* Compute the TLS offsets for the various blocks.  We call this
-	 function even if none of the modules available at startup time
-	 uses TLS to initialize some variables.  */
+      /* Compute the TLS offsets for the various blocks.  */
       _dl_determine_tlsoffset ();
 
       /* Construct the static TLS block and the dtv for the initial
diff --git a/elf/tst-tls4.c b/elf/tst-tls4.c
index 32ec147420..f92ee53ce5 100644
--- a/elf/tst-tls4.c
+++ b/elf/tst-tls4.c
@@ -4,14 +4,6 @@
 
 #include <tls.h>
 
-#ifdef USE_TLS
-# include "tls-macros.h"
-
-/* This gives the executable a TLS segment so that even if the libc.so
-   it loads has none (i.e. --with-tls --without-__thread), ld.so will
-   permit loading of objects with TLS segments.  */
-COMMON_INT_DEF(loser);
-#endif
 
 #define TEST_FUNCTION do_test ()
 static int
diff --git a/elf/tst-tls5.c b/elf/tst-tls5.c
index c10015d94d..a571d2cd3f 100644
--- a/elf/tst-tls5.c
+++ b/elf/tst-tls5.c
@@ -4,15 +4,6 @@
 
 #include <tls.h>
 
-#ifdef USE_TLS
-# include "tls-macros.h"
-
-/* This gives the executable a TLS segment so that even if the libc.so
-   it loads has none (i.e. --with-tls --without-__thread), ld.so will
-   permit loading of objects with TLS segments.  */
-COMMON_INT_DEF(loser);
-#endif
-
 
 #define TEST_FUNCTION do_test ()
 static int
diff --git a/elf/tst-tls6.c b/elf/tst-tls6.c
index 3c5c7aae4e..2c015efebb 100644
--- a/elf/tst-tls6.c
+++ b/elf/tst-tls6.c
@@ -5,15 +5,6 @@
 #include <link.h>
 #include <tls.h>
 
-#ifdef USE_TLS
-# include "tls-macros.h"
-
-/* This gives the executable a TLS segment so that even if the libc.so
-   it loads has none (i.e. --with-tls --without-__thread), ld.so will
-   permit loading of objects with TLS segments.  */
-COMMON_INT_DEF(loser);
-#endif
-
 
 #define TEST_FUNCTION do_test ()
 static int
diff --git a/elf/tst-tls7.c b/elf/tst-tls7.c
index 77d2016d08..4f785fe78e 100644
--- a/elf/tst-tls7.c
+++ b/elf/tst-tls7.c
@@ -5,15 +5,6 @@
 #include <link.h>
 #include <tls.h>
 
-#ifdef USE_TLS
-# include "tls-macros.h"
-
-/* This gives the executable a TLS segment so that even if the libc.so
-   it loads has none (i.e. --with-tls --without-__thread), ld.so will
-   permit loading of objects with TLS segments.  */
-COMMON_INT_DEF(loser);
-#endif
-
 
 #define TEST_FUNCTION do_test ()
 static int
diff --git a/elf/tst-tls8.c b/elf/tst-tls8.c
index 7444c261ec..e300bc53d8 100644
--- a/elf/tst-tls8.c
+++ b/elf/tst-tls8.c
@@ -5,15 +5,6 @@
 #include <link.h>
 #include <tls.h>
 
-#ifdef USE_TLS
-# include "tls-macros.h"
-
-/* This gives the executable a TLS segment so that even if the libc.so
-   it loads has none (i.e. --with-tls --without-__thread), ld.so will
-   permit loading of objects with TLS segments.  */
-COMMON_INT_DEF(loser);
-#endif
-
 
 #define TEST_FUNCTION do_test ()
 static int
diff --git a/elf/tst-tls9.c b/elf/tst-tls9.c
index a679efcd63..e317696dfc 100644
--- a/elf/tst-tls9.c
+++ b/elf/tst-tls9.c
@@ -5,16 +5,6 @@
 #include <link.h>
 #include <tls.h>
 
-#ifdef USE_TLS
-# include "tls-macros.h"
-
-/* This gives the executable a TLS segment so that even if the libc.so
-   it loads has none (i.e. --with-tls --without-__thread), ld.so will
-   permit loading of objects with TLS segments.  */
-COMMON_INT_DEF(loser);
-#endif
-
-
 #define TEST_FUNCTION do_test ()
 static int
 do_test (void)