ce426f
#
ce426f
# Patch provided by Ulrich Drepper in BZ#919562.
ce426f
#
ce426f
# Patch has not been sent upstream.
ce426f
#
ce426f
# 2013-03-08  Ulrich Drepper  <drepper@gmail.com>
ce426f
#
ce426f
#	* elf/rtld.c (dl_main): Correctly determine when the application
ce426f
#	required DSOs with TLS and bump the generation counter in that
ce426f
#	case.  The current condition does not account for an audit
ce426f
#	module using TLS.
ce426f
#	* elf/dl-tls.c (_dl_count_modids): New function.
ce426f
#	(_dl_allocate_tls_init): Add assertion to check TLS
ce426f
#	generation.
ce426f
#	* sysdeps/generic/ldsodefs.h: Declare _dl_count_modids.
ce426f
#	* elf/Makefile: Add rules to build and run tst-audit9.
ce426f
#	* elf/tst-audit9.c: New file.
ce426f
#	* elf/tst-auditmod9a.c: New file.
ce426f
#	* elf/tst-auditmod9b.c: New file.
ce426f
#
ce426f
diff -urN glibc-2.17-c758a686/elf/dl-tls.c glibc-2.17-c758a686/elf/dl-tls.c
ce426f
--- glibc-2.17-c758a686/elf/dl-tls.c	2013-04-24 16:06:10.410756438 -0400
ce426f
+++ glibc-2.17-c758a686/elf/dl-tls.c	2013-04-24 16:06:49.092604707 -0400
ce426f
@@ -109,6 +109,28 @@
ce426f
 }
ce426f
 
ce426f
 
ce426f
+size_t
ce426f
+internal_function
ce426f
+_dl_count_modids (void)
ce426f
+{
ce426f
+  if (! __builtin_expect (GL(dl_tls_dtv_gaps), true))
ce426f
+    return GL(dl_tls_max_dtv_idx);
ce426f
+
ce426f
+  size_t n = 0;
ce426f
+  struct dtv_slotinfo_list *runp = GL(dl_tls_dtv_slotinfo_list);
ce426f
+  while (runp != NULL)
ce426f
+    {
ce426f
+      for (size_t i = 0; i < runp->len; ++i)
ce426f
+	if (runp->slotinfo[i].map != NULL)
ce426f
+	  ++n;
ce426f
+
ce426f
+      runp = runp->next;
ce426f
+    }
ce426f
+
ce426f
+  return n;
ce426f
+}
ce426f
+
ce426f
+
ce426f
 #ifdef SHARED
ce426f
 void
ce426f
 internal_function
ce426f
@@ -411,6 +433,7 @@
ce426f
 
ce426f
 	  /* Keep track of the maximum generation number.  This might
ce426f
 	     not be the generation counter.  */
ce426f
+	  assert (listp->slotinfo[cnt].gen <= GL(dl_tls_generation));
ce426f
 	  maxgen = MAX (maxgen, listp->slotinfo[cnt].gen);
ce426f
 
ce426f
 	  if (map->l_tls_offset == NO_TLS_OFFSET
ce426f
diff -urN glibc-2.17-c758a686/elf/Makefile glibc-2.17-c758a686/elf/Makefile
ce426f
--- glibc-2.17-c758a686/elf/Makefile	2013-04-24 16:06:10.408756448 -0400
ce426f
+++ glibc-2.17-c758a686/elf/Makefile	2013-04-24 16:07:29.475457962 -0400
ce426f
@@ -198,7 +200,7 @@
ce426f
 	 tst-dlmodcount tst-dlopenrpath tst-deep1 \
ce426f
 	 tst-dlmopen1 tst-dlmopen2 tst-dlmopen3 \
ce426f
 	 unload3 unload4 unload5 unload6 unload7 unload8 tst-global1 order2 \
ce426f
-	 tst-audit1 tst-audit2 tst-audit8 \
ce426f
+	 tst-audit1 tst-audit2 tst-audit8 tst-audit9 \
ce426f
 	 tst-stackguard1 tst-addr1 tst-thrlock \
ce426f
 	 tst-unique1 tst-unique2 tst-unique3 tst-unique4 \
ce426f
 	 tst-initorder tst-initorder2 tst-relsort1
ce426f
@@ -251,7 +253,8 @@
ce426f
 		unload8mod1 unload8mod1x unload8mod2 unload8mod3 \
ce426f
 		order2mod1 order2mod2 order2mod3 order2mod4 \
ce426f
 		tst-unique1mod1 tst-unique1mod2 \
ce426f
-		tst-unique2mod1 tst-unique2mod2 \
ce426f
+		tst-unique2mod1 tst-unique2mod2 \
ce426f
+		tst-auditmod9a tst-auditmod9b \
ce426f
 		tst-unique3lib tst-unique3lib2 \
ce426f
 		tst-unique4lib \
ce426f
 		tst-initordera1 tst-initorderb1 \
ce426f
@@ -574,6 +577,8 @@
ce426f
 ifuncmod1.so-no-z-defs = yes
ce426f
 ifuncmod5.so-no-z-defs = yes
ce426f
 ifuncmod6.so-no-z-defs = yes
ce426f
+tst-auditmod9a.so-no-z-defs = yes
ce426f
+tst-auditmod9b.so-no-z-defs = yes
ce426f
 
ce426f
 ifeq ($(build-shared),yes)
ce426f
 # Build all the modules even when not actually running test programs.
ce426f
@@ -1015,6 +1020,10 @@
ce426f
 $(objpfx)tst-audit7.out: $(objpfx)tst-auditmod7b.so
ce426f
 tst-audit7-ENV = LD_AUDIT=$(objpfx)tst-auditmod7b.so
ce426f
 
ce426f
+$(objpfx)tst-audit9: $(libdl)
ce426f
+$(objpfx)tst-audit9.out: $(objpfx)tst-auditmod9a.so $(objpfx)tst-auditmod9b.so
ce426f
+tst-audit9-ENV = LD_AUDIT=$(objpfx)tst-auditmod9a.so
ce426f
+
ce426f
 $(objpfx)tst-audit8: $(common-objpfx)math/libm.so
ce426f
 $(objpfx)tst-audit8.out: $(objpfx)tst-auditmod1.so
ce426f
 tst-audit8-ENV = LD_AUDIT=$(objpfx)tst-auditmod1.so
ce426f
diff -urN glibc-2.17-c758a686/elf/rtld.c glibc-2.17-c758a686/elf/rtld.c
ce426f
--- glibc-2.17-c758a686/elf/rtld.c	2013-04-24 16:06:10.410756438 -0400
ce426f
+++ glibc-2.17-c758a686/elf/rtld.c	2013-04-24 16:06:49.096604693 -0400
ce426f
@@ -1637,6 +1637,10 @@
ce426f
 	}
ce426f
     }
ce426f
 
ce426f
+  /* Keep track of the currently loaded modules to count how many
ce426f
+     non-audit modules which use TLS are loaded.  */
ce426f
+  size_t count_modids = _dl_count_modids ();
ce426f
+
ce426f
   /* Set up debugging before the debugger is notified for the first time.  */
ce426f
 #ifdef ELF_MACHINE_DEBUG_SETUP
ce426f
   /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way.  */
ce426f
@@ -2281,7 +2285,8 @@
ce426f
 # define NONTLS_INIT_TP do { } while (0)
ce426f
 #endif
ce426f
 
ce426f
-  if (!was_tls_init_tp_called && GL(dl_tls_max_dtv_idx) > 0)
ce426f
+  if ((!was_tls_init_tp_called && GL(dl_tls_max_dtv_idx) > 0)
ce426f
+      || count_modids != _dl_count_modids ())
ce426f
     ++GL(dl_tls_generation);
ce426f
 
ce426f
   /* Now that we have completed relocation, the initializer data
ce426f
diff -urN glibc-2.17-c758a686/elf/tst-audit9.c glibc-2.17-c758a686/elf/tst-audit9.c
ce426f
--- glibc-2.17-c758a686/elf/tst-audit9.c	1969-12-31 19:00:00.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/elf/tst-audit9.c	2013-04-24 16:06:49.096604693 -0400
ce426f
@@ -0,0 +1,8 @@
ce426f
+#include <dlfcn.h>
ce426f
+
ce426f
+int main(void)
ce426f
+{
ce426f
+  void *h = dlopen("$ORIGIN/tst-auditmod9b.so", RTLD_LAZY);
ce426f
+  int (*fp)(void) = dlsym(h, "f");
ce426f
+  return fp() - 1;
ce426f
+}
ce426f
diff -urN glibc-2.17-c758a686/elf/tst-auditmod9a.c glibc-2.17-c758a686/elf/tst-auditmod9a.c
ce426f
--- glibc-2.17-c758a686/elf/tst-auditmod9a.c	1969-12-31 19:00:00.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/elf/tst-auditmod9a.c	2013-04-24 16:06:49.097604689 -0400
ce426f
@@ -0,0 +1,16 @@
ce426f
+#include <stdint.h>
ce426f
+
ce426f
+__thread int var;
ce426f
+
ce426f
+unsigned int
ce426f
+la_version (unsigned int v)
ce426f
+{
ce426f
+  return v;
ce426f
+}
ce426f
+
ce426f
+void
ce426f
+la_activity (uintptr_t *cookie, unsigned int flag)
ce426f
+{
ce426f
+  ++var;
ce426f
+}
ce426f
+
ce426f
diff -urN glibc-2.17-c758a686/elf/tst-auditmod9b.c glibc-2.17-c758a686/elf/tst-auditmod9b.c
ce426f
--- glibc-2.17-c758a686/elf/tst-auditmod9b.c	1969-12-31 19:00:00.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/elf/tst-auditmod9b.c	2013-04-24 16:06:49.097604689 -0400
ce426f
@@ -0,0 +1,6 @@
ce426f
+__thread int a;
ce426f
+
ce426f
+int f(void)
ce426f
+{
ce426f
+  return ++a;
ce426f
+}
ce426f
diff -urN glibc-2.17-c758a686/sysdeps/generic/ldsodefs.h glibc-2.17-c758a686/sysdeps/generic/ldsodefs.h
ce426f
--- glibc-2.17-c758a686/sysdeps/generic/ldsodefs.h	2013-04-24 16:06:10.545755798 -0400
ce426f
+++ glibc-2.17-c758a686/sysdeps/generic/ldsodefs.h	2013-04-24 16:06:49.098604686 -0400
ce426f
@@ -1031,6 +1031,9 @@
ce426f
 /* Determine next available module ID.  */
ce426f
 extern size_t _dl_next_tls_modid (void) internal_function attribute_hidden;
ce426f
 
ce426f
+/* Count the modules with TLS segments.  */
ce426f
+extern size_t _dl_count_modids (void) internal_function attribute_hidden;
ce426f
+
ce426f
 /* Calculate offset of the TLS blocks in the static TLS block.  */
ce426f
 extern void _dl_determine_tlsoffset (void) internal_function attribute_hidden;
ce426f