summary refs log tree commit diff
path: root/elf
diff options
context:
space:
mode:
Diffstat (limited to 'elf')
-rw-r--r--elf/Makefile10
-rw-r--r--elf/Versions4
-rw-r--r--elf/dl-load.c32
-rw-r--r--elf/dl-minimal.c12
-rw-r--r--elf/dl-open.c84
-rw-r--r--elf/dl-support.c12
-rw-r--r--elf/dl-sym.c22
-rw-r--r--elf/rtld.c131
-rw-r--r--elf/tls-macros.h16
-rw-r--r--elf/tst-tls1.c10
-rw-r--r--elf/tst-tls2.c10
-rw-r--r--elf/tst-tls3.c10
-rw-r--r--elf/tst-tls4.c56
-rw-r--r--elf/tst-tls5.c72
-rw-r--r--elf/tst-tlsmod1.c2
-rw-r--r--elf/tst-tlsmod2.c32
16 files changed, 409 insertions, 106 deletions
diff --git a/elf/Makefile b/elf/Makefile
index 9c5d4f6210..4ef1614c06 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -119,7 +119,7 @@ tests = loadtest restest1 preloadtest loadfail multiload origtest resolvfail \
 	$(tests-nodlopen-$(have-z-nodlopen)) neededtest neededtest2 \
 	neededtest3 neededtest4 unload2 lateglobal initfirst global \
 	restest2 next dblload dblunload reldep5 reldep6 tst-tls1 tst-tls2 \
-	tst-tls3
+	tst-tls3 tst-tls4 tst-tls5
 test-srcs = tst-pathopt
 tests-vis-yes = vismain
 tests-nodelete-yes = nodelete
@@ -137,7 +137,7 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
 		unload2mod unload2dep ltglobmod1 ltglobmod2 pathoptobj \
 		dblloadmod1 dblloadmod2 dblloadmod3 reldepmod5 reldepmod6 \
 	        reldep6mod0 reldep6mod1 reldep6mod2 reldep6mod3 reldep6mod4 \
-		tst-tlsmod1
+		tst-tlsmod1 tst-tlsmod2
 modules-vis-yes = vismod1 vismod2 vismod3
 modules-nodelete-yes = nodelmod1 nodelmod2 nodelmod3 nodelmod4
 modules-nodlopen-yes = nodlopenmod nodlopenmod2
@@ -442,3 +442,9 @@ $(objpfx)reldep6: $(libdl)
 $(objpfx)reldep6.out: $(objpfx)reldep6mod3.so $(objpfx)reldep6mod4.so
 
 $(objpfx)tst-tls3: $(objpfx)tst-tlsmod1.so
+
+$(objpfx)tst-tls4: $(libdl)
+$(objpfx)tst-tls4.out: $(objpfx)tst-tlsmod2.so
+
+$(objpfx)tst-tls5: $(libdl)
+$(objpfx)tst-tls5.out: $(objpfx)tst-tlsmod2.so
diff --git a/elf/Versions b/elf/Versions
index 8d8a1e8eac..927e26ff46 100644
--- a/elf/Versions
+++ b/elf/Versions
@@ -27,7 +27,7 @@ libc {
 ld {
   GLIBC_2.0 {
     # Function from libc.so which must be shared with libc.
-    calloc; free; malloc; realloc;
+    calloc; free; malloc; realloc; __libc_memalign;
 
     _r_debug;
   }
@@ -49,6 +49,6 @@ ld {
     _dl_map_object; _dl_map_object_deps; _dl_out_of_memory;
     _dl_relocate_object; _dl_signal_error; _dl_start_profile; _dl_starting_up;
     _dl_unload_cache;
-    _rtld_global;
+    _rtld_global; _dl_tls_symaddr;
   }
 }
diff --git a/elf/dl-load.c b/elf/dl-load.c
index 1fffe72108..48e7cba235 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -941,30 +941,18 @@ _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp,
 
 #ifdef USE_TLS
 	case PT_TLS:
-	  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_offset;
-
-	/* This is the first element of the initialization image list.
-	   It is created as a circular list so that we can easily
-	   append to it.  */
-	  if (GL(dl_initimage_list) == NULL)
-	    GL(dl_initimage_list) = l->l_tls_nextimage = l->l_tls_previmage
-	      = l;
-	  else
+	  if (ph->p_memsz > 0)
 	    {
-	      l->l_tls_nextimage = GL(dl_initimage_list)->l_tls_nextimage;
-	      l->l_tls_nextimage->l_tls_previmage = l;
-	      l->l_tls_previmage = GL(dl_initimage_list);
-	      l->l_tls_previmage->l_tls_nextimage = l;
-	      GL(dl_initimage_list) = l;
+	      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_offset;
+
+	      /* Assign the next available module ID.  */
+	      l->l_tls_modid = _dl_next_tls_modid ();
 	    }
-
-	  /* Assign the next available module ID.  */
-	  l->l_tls_modid = _dl_next_tls_modid ();
 	  break;
 #endif
 	}
diff --git a/elf/dl-minimal.c b/elf/dl-minimal.c
index c65151cb56..275ad86fe0 100644
--- a/elf/dl-minimal.c
+++ b/elf/dl-minimal.c
@@ -21,8 +21,9 @@
 #include <limits.h>
 #include <string.h>
 #include <unistd.h>
-#include <sys/types.h>
 #include <sys/mman.h>
+#include <sys/param.h>
+#include <sys/types.h>
 #include <ldsodefs.h>
 #include <stdio-common/_itoa.h>
 
@@ -120,6 +121,15 @@ realloc (void *ptr, size_t n)
   assert (new == ptr);
   return new;
 }
+
+/* Return alligned memory block.  */
+void * weak_function
+__libc_memalign (size_t align, size_t n)
+{
+  void *newp = malloc (n + align - 1);
+
+  return (void *) roundup ((uintptr_t) newp, align);
+}
 
 /* Avoid signal frobnication in setjmp/longjmp.  Keeps things smaller.  */
 
diff --git a/elf/dl-open.c b/elf/dl-open.c
index 8f30bc009d..d9ed499054 100644
--- a/elf/dl-open.c
+++ b/elf/dl-open.c
@@ -31,6 +31,7 @@
 #include <bp-sym.h>
 
 #include <dl-dst.h>
+#include <dl-tls.h>
 
 
 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
@@ -353,6 +354,73 @@ dl_open_worker (void *a)
 	imap->l_scope[cnt++] = &new->l_searchlist;
 	imap->l_scope[cnt] = NULL;
       }
+#if USE_TLS
+    else if (new->l_searchlist.r_list[i]->l_opencount == 1
+	     /* Only if the module defines thread local data.  */
+	     && __builtin_expect (new->l_searchlist.r_list[i]->l_tls_blocksize
+				  > 0, 0))
+      {
+	/* Now that we know the object is loaded successfully add
+	   modules containing TLS data to the dtv info table.  We
+	   might have to increase its size.  */
+	struct dtv_slotinfo_list *listp;
+	struct dtv_slotinfo_list *prevp;
+	size_t idx = new->l_searchlist.r_list[i]->l_tls_modid;
+
+	assert (new->l_searchlist.r_list[i]->l_type == lt_loaded);
+
+	/* Find the place in the stv slotinfo list.  */
+	listp = GL(dl_tls_dtv_slotinfo_list);
+	prevp = NULL;		/* Needed to shut up gcc.  */
+	do
+	  {
+	    /* Does it fit in the array of this list element?  */
+	    if (idx <= listp->len)
+	      break;
+	    prevp = listp;
+	  }
+	while ((listp = listp->next) != NULL);
+
+	if (listp == NULL)
+	  {
+	    /* When we come here it means we have to add a new element
+	       to the slotinfo list.  And the new module must be in
+	       the first slot.  */
+	    assert (idx == 0);
+
+	    listp = prevp->next = (struct dtv_slotinfo_list *)
+	      malloc (sizeof (struct dtv_slotinfo_list)
+		      + TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
+	    if (listp == NULL)
+	      {
+		/* We ran out of memory.  We will simply fail this
+		   call but don't undo anything we did so far.  The
+		   application will crash or be terminated anyway very
+		   soon.  */
+
+		/* We have to do this since some entries in the dtv
+		   slotinfo array might already point to this
+		   generation.  */
+		++GL(dl_tls_generation);
+
+		_dl_signal_error (ENOMEM, "dlopen", NULL,
+				  N_("cannot create TLS data structures"));
+	      }
+
+	    listp->len = TLS_SLOTINFO_SURPLUS;
+	    listp->next = NULL;
+	    memset (listp->slotinfo, '\0',
+		    TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
+	  }
+
+	/* Add the information into the slotinfo data structure.  */
+	listp->slotinfo[idx].map = new->l_searchlist.r_list[i];
+	listp->slotinfo[idx].gen = GL(dl_tls_generation) + 1;
+      }
+
+  /* Bump the generation number.  */
+  ++GL(dl_tls_generation);
+#endif
 
   /* Run the initializer functions of new objects.  */
   _dl_init (new, __libc_argc, __libc_argv, __environ);
@@ -424,10 +492,18 @@ _dl_open (const char *file, int mode, const void *caller)
 	{
 	  unsigned int i;
 
-	  /* Increment open counters for all objects since this has
-	     not happened yet.  */
-	  for (i = 0; i < args.map->l_searchlist.r_nlist; ++i)
-	    ++args.map->l_searchlist.r_list[i]->l_opencount;
+	  /* Increment open counters for all objects since this
+	     sometimes has not happened yet.  */
+	  if (args.map->l_searchlist.r_list[0]->l_opencount == 0)
+	    for (i = 0; i < args.map->l_searchlist.r_nlist; ++i)
+	      ++args.map->l_searchlist.r_list[i]->l_opencount;
+
+	  /* Maybe some of the modules which were loaded uses TLS.
+	     Since it will be removed in the folowing _dl_close call
+	     we have to mark the dtv array as having gaps to fill
+	     the holes.  This is a pessimistic assumption which won't
+	     hurt if not true.  */
+	  GL(dl_tls_dtv_gaps) = true;
 
 	  _dl_close (args.map);
 	}
diff --git a/elf/dl-support.c b/elf/dl-support.c
index 914b43f057..876776acba 100644
--- a/elf/dl-support.c
+++ b/elf/dl-support.c
@@ -138,20 +138,22 @@ int _dl_correct_cache_id = _DL_CACHE_DEFAULT_ID;
 __libc_lock_define_initialized_recursive (, _dl_load_lock)
 
 #ifdef USE_TLS
-/* Beginning of the list of link maps for objects which contain
-   thread-local storage sections.  This will be traversed to
-   initialize new TLS blocks.  */
-struct link_map *_dl_initimage_list;
 
 /* Highest dtv index currently needed.  */
 size_t _dl_tls_max_dtv_idx;
 /* Flag signalling whether there are gaps in the module ID allocation.  */
 bool _dl_tls_dtv_gaps;
-
+/* Information about the dtv slots.  */
+struct dtv_slotinfo_list *_dl_tls_dtv_slotinfo_list;
+/* Number of modules in the static TLS block.  */
+size_t _dl_tls_static_nelem;
 /* Size of the static TLS block.  */
 size_t _dl_tls_static_size;
 /* Alignment requirement of the static TLS block.  */
 size_t _dl_tls_static_align;
+
+/* Generation counter for the dtv.  */
+size_t _dl_tls_generation;
 #endif
 
 
diff --git a/elf/dl-sym.c b/elf/dl-sym.c
index b0db159aa6..8cb5d2f411 100644
--- a/elf/dl-sym.c
+++ b/elf/dl-sym.c
@@ -83,7 +83,16 @@ RTLD_NEXT used in code not dynamically loaded"));
     }
 
   if (ref != NULL)
-    return DL_SYMBOL_ADDRESS (result, ref);
+    {
+#if defined USE_TLS && defined SHARED
+      if (ELFW(ST_TYPE) (ref->st_info) == STT_TLS)
+	/* The found symbol is a thread-local storage variable.
+	   Return the address for to the current thread.  */
+	return _dl_tls_symaddr (result, ref);
+#endif
+
+      return DL_SYMBOL_ADDRESS (result, ref);
+    }
 
   return NULL;
 }
@@ -152,7 +161,16 @@ RTLD_NEXT used in code not dynamically loaded"));
     }
 
   if (ref != NULL)
-    return DL_SYMBOL_ADDRESS (result, ref);
+    {
+#if defined USE_TLS && defined SHARED
+      if (ELFW(ST_TYPE) (ref->st_info) == STT_TLS)
+	/* The found symbol is a thread-local storage variable.
+	   Return the address for to the current thread.  */
+	return _dl_tls_symaddr (result, ref);
+#endif
+
+      return DL_SYMBOL_ADDRESS (result, ref);
+    }
 
   return NULL;
 }
diff --git a/elf/rtld.c b/elf/rtld.c
index 6463ed600e..d7653f2394 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -223,7 +223,7 @@ _dl_start_final (void *arg, struct link_map *bootstrap_map_p,
   ElfW(Ehdr) *ehdr;
   ElfW(Phdr) *phdr;
   size_t cnt;
-  dtv_t initdtv[2];
+  dtv_t initdtv[3];
 #endif
 
   if (HP_TIMING_AVAIL)
@@ -291,16 +291,18 @@ _dl_start_final (void *arg, struct link_map *bootstrap_map_p,
 	tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
 			     & ~(max_align - 1));
 
-	/* Initialize the dtv.  */
+	/* Initialize the dtv.  [0] is the length, [1] the generation
+	   counter.  */
 	initdtv[0].counter = 1;
+	initdtv[1].counter = 0;
 
 	/* Initialize the TLS block.  */
 # if TLS_TCB_AT_TP
-	initdtv[1].pointer = tlsblock;
+	initdtv[2].pointer = tlsblock;
 # elif TLS_DTV_AT_TP
 	GL(dl_rtld_map).l_tls_offset = roundup (TLS_INIT_TCB_SIZE,
 						GL(dl_rtld_map).l_tls_align);
-	initdtv[1].pointer = (char *) tlsblock + GL(dl_rtld_map).l_tls_offset);
+	initdtv[2].pointer = (char *) tlsblock + GL(dl_rtld_map).l_tls_offset);
 # else
 #  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
 # endif
@@ -723,22 +725,20 @@ of this helper program; chances are you did not intend to run this program.\n\
 	break;
 #ifdef USE_TLS
       case PT_TLS:
-	/* Note that in the case the dynamic linker we duplicate work
-	   here since we read the PT_TLS entry already in
-	   _dl_start_final.  But the result is repeatable so do not
-	   check for this special but unimportant case.  */
-	GL(dl_loaded)->l_tls_blocksize = ph->p_memsz;
-	GL(dl_loaded)->l_tls_align = ph->p_align;
-	GL(dl_loaded)->l_tls_initimage_size = ph->p_filesz;
-	GL(dl_loaded)->l_tls_initimage = (void *) ph->p_vaddr;
-	/* This is the first element of the initialization image list.
-	   We create the list as circular since we have to append at
-	   the end.  */
-	GL(dl_initimage_list) = GL(dl_loaded)->l_tls_nextimage
-	  = GL(dl_loaded)->l_tls_previmage = GL(dl_loaded);
-
-	/* This image gets the ID one.  */
-	GL(dl_tls_max_dtv_idx) = GL(dl_loaded)->l_tls_modid = 1;
+	if (ph->p_memsz > 0)
+	  {
+	    /* Note that in the case the dynamic linker we duplicate work
+	       here since we read the PT_TLS entry already in
+	       _dl_start_final.  But the result is repeatable so do not
+	       check for this special but unimportant case.  */
+	    GL(dl_loaded)->l_tls_blocksize = ph->p_memsz;
+	    GL(dl_loaded)->l_tls_align = ph->p_align;
+	    GL(dl_loaded)->l_tls_initimage_size = ph->p_filesz;
+	    GL(dl_loaded)->l_tls_initimage = (void *) ph->p_vaddr;
+
+	    /* This image gets the ID one.  */
+	    GL(dl_tls_max_dtv_idx) = GL(dl_loaded)->l_tls_modid = 1;
+	  }
 	break;
 #endif
       }
@@ -1188,43 +1188,66 @@ of this helper program; chances are you did not intend to run this program.\n\
      use the static model.  First add the dynamic linker to the list
      if it also uses TLS.  */
   if (GL(dl_rtld_map).l_tls_blocksize != 0)
+    /* Assign a module ID.  */
+    GL(dl_rtld_map).l_tls_modid = _dl_next_tls_modid ();
+
+# ifndef SHARED
+  /* If dynamic loading of modules with TLS is impossible we do not
+     have to initialize any of the TLS functionality unless any of the
+     initial modules uses TLS.  */
+  if (GL(dl_tls_max_dtv_idx) > 0)
+# endif
     {
-      /* At to the list.  */
-      if (GL(dl_initimage_list) == NULL)
-	GL(dl_initimage_list) = GL(dl_rtld_map).l_tls_nextimage
-	  = GL(dl_rtld_map).l_tls_previmage = &GL(dl_rtld_map);
-	  else
-	    {
-	      GL(dl_rtld_map).l_tls_nextimage
-		= GL(dl_initimage_list)->l_tls_nextimage;
-	      GL(dl_rtld_map).l_tls_nextimage->l_tls_previmage
-		= &GL(dl_rtld_map);
-	      GL(dl_rtld_map).l_tls_previmage = GL(dl_initimage_list);
-	      GL(dl_rtld_map).l_tls_previmage->l_tls_nextimage
-		= &GL(dl_rtld_map);
-	      GL(dl_initimage_list) = &GL(dl_rtld_map);
-	    }
+      struct link_map *l;
+      size_t nelem;
+      struct dtv_slotinfo *slotinfo;
+
+      /* Number of elements in the static TLS block.  */
+      GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
+
+      /* Allocate the array which contains the information about the
+	 dtv slots.  We allocate a few entries more than needed to
+	 avoid the need for reallocation.  */
+      nelem = GL(dl_tls_max_dtv_idx) + 1 + TLS_SLOTINFO_SURPLUS;
+
+      /* Allocate.  */
+      GL(dl_tls_dtv_slotinfo_list) = (struct dtv_slotinfo_list *)
+	malloc (sizeof (struct dtv_slotinfo_list)
+		+ nelem * sizeof (struct dtv_slotinfo));
+      /* No need to check the return value.  If memory allocation failed
+	 the program would have been terminated.  */
+
+      slotinfo = memset (GL(dl_tls_dtv_slotinfo_list)->slotinfo, '\0',
+			 nelem * sizeof (struct dtv_slotinfo));
+      GL(dl_tls_dtv_slotinfo_list)->len = nelem;
+      GL(dl_tls_dtv_slotinfo_list)->next = NULL;
+
+      /* Fill in the information from the loaded modules.  */
+      for (l = GL(dl_loaded), i = 0; l != NULL; l = l->l_next)
+	if (l->l_tls_blocksize != 0)
+	  /* This is a module with TLS data.  Store the map reference.
+	     The generation counter is zero.  */
+	  slotinfo[++i].map = l;
+      assert (i == GL(dl_tls_max_dtv_idx));
+
+      /* Computer 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.  */
+      _dl_determine_tlsoffset ();
+
+      /* Construct the static TLS block and the dtv for the initial
+	 thread.  For some platforms this will include allocating memory
+	 for the thread descriptor.  The memory for the TLS block will
+	 never be freed.  It should be allocated accordingly.  The dtv
+	 array can be changed if dynamic loading requires it.  */
+      tcbp = _dl_allocate_tls ();
+      if (tcbp == NULL)
+	_dl_fatal_printf ("\
+cannot allocate TLS data structures for inital thread");
 
-      /* Assign a module ID.  */
-      GL(dl_rtld_map).l_tls_modid = _dl_next_tls_modid ();
+      /* And finally install it for the main thread.  */
+      TLS_INIT_TP (tcbp);
     }
-
-  /* Computer 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.  */
-    _dl_determine_tlsoffset (GL(dl_initimage_list));
-
-  /* Construct the static TLS block and the dtv for the initial
-     thread.  For some platforms this will include allocating memory
-     for the thread descriptor.  The memory for the TLS block will
-     never be freed.  It should be allocated accordingly.  The dtv
-     array can be changed if dynamic loading requires it.  */
-  tcbp = _dl_allocate_tls ();
-  if (tcbp == NULL)
-    _dl_fatal_printf ("cannot allocate TLS data structures for inital thread");
-
-  /* And finally install it for the main thread.  */
-  TLS_INIT_TP (tcbp);
 #endif
 
   if (GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_LIBLIST)]
diff --git a/elf/tls-macros.h b/elf/tls-macros.h
index 35b57d4f04..26745e9e21 100644
--- a/elf/tls-macros.h
+++ b/elf/tls-macros.h
@@ -49,15 +49,15 @@
 
 # ifdef PIC
 #  define TLS_LD(x) \
-  ({ int *__l;								      \
+  ({ int *__l, __c, __d;						      \
      asm ("leal " #x "@tlsldm(%%ebx),%%eax\n\t"				      \
 	  "call ___tls_get_addr@plt\n\t"				      \
 	  "leal " #x "@dtpoff(%%eax), %%eax"				      \
-	  : "=a" (__l));						      \
+	  : "=a" (__l), "=&c" (__c), "=&d" (__d));			      \
      __l; })
 # else
 #  define TLS_LD(x) \
-  ({ int *__l, __b;							      \
+  ({ int *__l, __b, __c, __d;						      \
      asm ("call 1f\n\t"							      \
 	  ".subsection 1\n"						      \
 	  "1:\tmovl (%%esp), %%ebx\n\t"					      \
@@ -67,21 +67,21 @@
 	  "leal " #x "@tlsldm(%%ebx),%%eax\n\t"				      \
 	  "call ___tls_get_addr@plt\n\t"				      \
 	  "leal " #x "@dtpoff(%%eax), %%eax"				      \
-	  : "=a" (__l), "=&b" (__b));					      \
+	  : "=a" (__l), "=&b" (__b), "=&c" (__c), "=&d" (__d));		      \
      __l; })
 # endif
 
 # ifdef PIC
 #  define TLS_GD(x) \
-  ({ int *__l;								      \
+  ({ int *__l, __c, __d;						      \
      asm ("leal " #x "@tlsgd(%%ebx),%%eax\n\t"				      \
 	  "call ___tls_get_addr@plt\n\t"				      \
 	  "nop"								      \
-	  : "=a" (__l));						      \
+	  : "=a" (__l), "=&c" (__c), "=&d" (__d));			      \
      __l; })
 # else
 #  define TLS_GD(x) \
-  ({ int *__l, __b;							      \
+  ({ int *__l, __b, __c, __d;						      \
      asm ("call 1f\n\t"							      \
 	  ".subsection 1\n"						      \
 	  "1:\tmovl (%%esp), %%ebx\n\t"					      \
@@ -91,7 +91,7 @@
 	  "leal " #x "@tlsgd(%%ebx),%%eax\n\t"				      \
 	  "call ___tls_get_addr@plt\n\t"				      \
 	  "nop"								      \
-	  : "=a" (__l), "=&b" (__b));					      \
+	  : "=a" (__l), "=&b" (__b), "=&c" (__c), "=&d" (__d));		      \
      __l; })
 # endif
 
diff --git a/elf/tst-tls1.c b/elf/tst-tls1.c
index 5e67482ead..74e9a915b3 100644
--- a/elf/tst-tls1.c
+++ b/elf/tst-tls1.c
@@ -5,13 +5,16 @@
 #include "tls-macros.h"
 
 
+#ifdef USE_TLS
 /* Two common 'int' variables in TLS.  */
 COMMON_INT_DEF(foo);
 COMMON_INT_DEF(bar);
+#endif
 
 
-int
-main (void)
+#define TEST_FUNCTION do_test ()
+static int
+do_test (void)
 {
 #ifdef USE_TLS
   int result = 0;
@@ -82,3 +85,6 @@ main (void)
   return 0;
 #endif
 }
+
+
+#include "../test-skeleton.c"
diff --git a/elf/tst-tls2.c b/elf/tst-tls2.c
index 1810ffa1e3..a15c5f5fe8 100644
--- a/elf/tst-tls2.c
+++ b/elf/tst-tls2.c
@@ -5,13 +5,16 @@
 #include "tls-macros.h"
 
 
+#ifdef USE_TLS
 /* Two 'int' variables in TLS.  */
 VAR_INT_DEF(foo);
 VAR_INT_DEF(bar);
+#endif
 
 
-int
-main (void)
+#define TEST_FUNCTION do_test ()
+static int
+do_test (void)
 {
 #ifdef USE_TLS
   int result = 0;
@@ -82,3 +85,6 @@ main (void)
   return 0;
 #endif
 }
+
+
+#include "../test-skeleton.c"
diff --git a/elf/tst-tls3.c b/elf/tst-tls3.c
index c86f1840a5..58bb183c8d 100644
--- a/elf/tst-tls3.c
+++ b/elf/tst-tls3.c
@@ -5,17 +5,20 @@
 #include "tls-macros.h"
 
 
+#ifdef USE_TLS
 /* One define int variable, two externs.  */
 COMMON_INT_DECL(foo);
 VAR_INT_DECL(bar);
 VAR_INT_DEF(baz);
+#endif
 
 
 extern int in_dso (void);
 
 
-int
-main (void)
+#define TEST_FUNCTION do_test ()
+static int
+do_test (void)
 {
 #ifdef USE_TLS
   int result = 0;
@@ -67,3 +70,6 @@ main (void)
   return 0;
 #endif
 }
+
+
+#include "../test-skeleton.c"
diff --git a/elf/tst-tls4.c b/elf/tst-tls4.c
new file mode 100644
index 0000000000..f92ee53ce5
--- /dev/null
+++ b/elf/tst-tls4.c
@@ -0,0 +1,56 @@
+#include <dlfcn.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <tls.h>
+
+
+#define TEST_FUNCTION do_test ()
+static int
+do_test (void)
+{
+#ifdef USE_TLS
+  static const char modname[] = "tst-tlsmod2.so";
+  int result = 0;
+  int *foop;
+  int (*fp) (int, int *);
+  void *h;
+
+  h = dlopen (modname, RTLD_LAZY);
+  if (h == NULL)
+    {
+      printf ("cannot open '%s': %s\n", modname, dlerror ());
+      exit (1);
+    }
+
+  fp = dlsym (h, "in_dso");
+  if (fp == NULL)
+    {
+      printf ("cannot get symbol 'in_dso': %s\n", dlerror ());
+      exit (1);
+    }
+
+  result |= fp (0, NULL);
+
+  foop = dlsym (h, "foo");
+  if (foop == NULL)
+    {
+      printf ("cannot get symbol 'foo' the second time: %s\n", dlerror ());
+      exit (1);
+    }
+  if (*foop != 16)
+    {
+      puts ("foo != 16");
+      result = 1;
+    }
+
+  dlclose (h);
+
+  return result;
+#else
+  return 0;
+#endif
+}
+
+
+#include "../test-skeleton.c"
diff --git a/elf/tst-tls5.c b/elf/tst-tls5.c
new file mode 100644
index 0000000000..a571d2cd3f
--- /dev/null
+++ b/elf/tst-tls5.c
@@ -0,0 +1,72 @@
+#include <dlfcn.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <tls.h>
+
+
+#define TEST_FUNCTION do_test ()
+static int
+do_test (void)
+{
+#ifdef USE_TLS
+  static const char modname[] = "tst-tlsmod2.so";
+  int result = 0;
+  int *foop;
+  int *foop2;
+  int (*fp) (int, int *);
+  void *h;
+
+  h = dlopen (modname, RTLD_LAZY);
+  if (h == NULL)
+    {
+      printf ("cannot open '%s': %s\n", modname, dlerror ());
+      exit (1);
+    }
+
+  foop = dlsym (h, "foo");
+  if (foop == NULL)
+    {
+      printf ("cannot get symbol 'foo': %s\n", dlerror ());
+      exit (1);
+    }
+
+  *foop = 42;
+
+  fp = dlsym (h, "in_dso");
+  if (fp == NULL)
+    {
+      printf ("cannot get symbol 'in_dso': %s\n", dlerror ());
+      exit (1);
+    }
+
+  result |= fp (42, foop);
+
+  foop2 = dlsym (h, "foo");
+  if (foop2 == NULL)
+    {
+      printf ("cannot get symbol 'foo' the second time: %s\n", dlerror ());
+      exit (1);
+    }
+
+  if (foop != foop2)
+    {
+      puts ("address of 'foo' different the second time");
+      result = 1;
+    }
+  else if (*foop != 16)
+    {
+      puts ("foo != 16");
+      result = 1;
+    }
+
+  dlclose (h);
+
+  return result;
+#else
+  return 0;
+#endif
+}
+
+
+#include "../test-skeleton.c"
diff --git a/elf/tst-tlsmod1.c b/elf/tst-tlsmod1.c
index 3a632865f8..cca8df6549 100644
--- a/elf/tst-tlsmod1.c
+++ b/elf/tst-tlsmod1.c
@@ -4,10 +4,12 @@
 #include "tls-macros.h"
 
 
+#ifdef USE_TLS
 /* One define int variable, two externs.  */
 COMMON_INT_DEF(foo);
 VAR_INT_DEF(bar);
 VAR_INT_DECL(baz);
+#endif
 
 
 int
diff --git a/elf/tst-tlsmod2.c b/elf/tst-tlsmod2.c
new file mode 100644
index 0000000000..30ed67163d
--- /dev/null
+++ b/elf/tst-tlsmod2.c
@@ -0,0 +1,32 @@
+#include <stdio.h>
+
+#include <tls.h>
+#include "tls-macros.h"
+
+#ifdef USE_TLS
+
+COMMON_INT_DEF(foo);
+
+
+int
+in_dso (int n, int *caller_foop)
+{
+  int *foop = TLS_GD (foo);
+  int result = 0;
+
+  if (caller_foop != NULL && foop != caller_foop)
+    {
+      printf ("callers address of foo differs: %p vs %p\n", caller_foop, foop);
+      result = 1;
+    }
+  else if (*foop != n)
+    {
+      printf ("foo != %d\n", n);
+      result = 1;
+    }
+
+  *foop = 16;
+
+  return result;
+}
+#endif