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