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