Module Name:    src
Committed By:   joerg
Date:           Thu Nov 17 16:20:11 UTC 2011

Modified Files:
        src/libexec/ld.elf_so: rtld.c
        src/tests/lib/libc/tls: t_tls_dlopen.c t_tls_dynamic.c
        src/tests/lib/libc/tls/dso: h_tls_dlopen.c
        src/tests/lib/libc/tls_dso: h_tls_dynamic.c

Log Message:
FreeBSD bug report 161344: TLS area for the main thread is set up to
early, if e.g. pointers to functions are used as initializers.


To generate a diff of this commit:
cvs rdiff -u -r1.153 -r1.154 src/libexec/ld.elf_so/rtld.c
cvs rdiff -u -r1.1 -r1.2 src/tests/lib/libc/tls/t_tls_dlopen.c \
    src/tests/lib/libc/tls/t_tls_dynamic.c
cvs rdiff -u -r1.1 -r1.2 src/tests/lib/libc/tls/dso/h_tls_dlopen.c
cvs rdiff -u -r1.1 -r1.2 src/tests/lib/libc/tls_dso/h_tls_dynamic.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/libexec/ld.elf_so/rtld.c
diff -u src/libexec/ld.elf_so/rtld.c:1.153 src/libexec/ld.elf_so/rtld.c:1.154
--- src/libexec/ld.elf_so/rtld.c:1.153	Sun Oct 23 21:06:07 2011
+++ src/libexec/ld.elf_so/rtld.c	Thu Nov 17 16:20:11 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: rtld.c,v 1.153 2011/10/23 21:06:07 christos Exp $	 */
+/*	$NetBSD: rtld.c,v 1.154 2011/11/17 16:20:11 joerg Exp $	 */
 
 /*
  * Copyright 1996 John D. Polstra.
@@ -40,7 +40,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: rtld.c,v 1.153 2011/10/23 21:06:07 christos Exp $");
+__RCSID("$NetBSD: rtld.c,v 1.154 2011/11/17 16:20:11 joerg Exp $");
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -642,13 +642,12 @@ _rtld(Elf_Addr *sp, Elf_Addr relocbase)
 	}
 
 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
-	dbg(("initializing initial Thread Local Storage"));
+	dbg(("initializing initial Thread Local Storage offsets"));
 	/*
 	 * All initial objects get the TLS space from the static block.
 	 */
 	for (obj = _rtld_objlist; obj != NULL; obj = obj->next)
 		_rtld_tls_offset_allocate(obj);
-	_rtld_tls_initial_allocation();
 #endif
 
 	dbg(("relocating objects"));
@@ -659,6 +658,16 @@ _rtld(Elf_Addr *sp, Elf_Addr relocbase)
 	if (_rtld_do_copy_relocations(_rtld_objmain) == -1)
 		_rtld_die();
 
+#if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
+	dbg(("initializing Thread Local Storage for main thread"));
+	/*
+	 * Set up TLS area for the main thread.
+	 * This has to be done after all relocations are processed,
+	 * since .tdata may contain relocations.
+	 */
+	_rtld_tls_initial_allocation();
+#endif
+
 	/*
 	 * Set the __progname,  environ and, __mainprog_obj before
 	 * calling anything that might use them.

Index: src/tests/lib/libc/tls/t_tls_dlopen.c
diff -u src/tests/lib/libc/tls/t_tls_dlopen.c:1.1 src/tests/lib/libc/tls/t_tls_dlopen.c:1.2
--- src/tests/lib/libc/tls/t_tls_dlopen.c:1.1	Wed Mar  9 23:10:07 2011
+++ src/tests/lib/libc/tls/t_tls_dlopen.c	Thu Nov 17 16:20:11 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: t_tls_dlopen.c,v 1.1 2011/03/09 23:10:07 joerg Exp $	*/
+/*	$NetBSD: t_tls_dlopen.c,v 1.2 2011/11/17 16:20:11 joerg Exp $	*/
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
  * All rights reserved.
@@ -32,11 +32,12 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_tls_dlopen.c,v 1.1 2011/03/09 23:10:07 joerg Exp $");
+__RCSID("$NetBSD: t_tls_dlopen.c,v 1.2 2011/11/17 16:20:11 joerg Exp $");
 
 #include <atf-c.h>
 #include <dlfcn.h>
 #include <pthread.h>
+#include <unistd.h>
 
 #include <sys/tls.h>
 
@@ -56,18 +57,24 @@ void (*testf_helper)(int, int);
 
 __thread int var1 = 1;
 __thread int var2;
+__thread int *var3 = &optind;
+int var4_helper;
+__thread int *var4 = &var4_helper;
 
 static void *
 testf(void *dummy)
 {
 	ATF_CHECK_EQ(var1, 1);
 	ATF_CHECK_EQ(var2, 0);
+	ATF_CHECK_EQ(var3, &optind);
+	ATF_CHECK_EQ(var4, &var4_helper);
 	testf_helper(2, 2);
 	ATF_CHECK_EQ(var1, 2);
 	ATF_CHECK_EQ(var2, 2);
 	testf_helper(3, 3);
 	ATF_CHECK_EQ(var1, 3);
 	ATF_CHECK_EQ(var2, 3);
+	ATF_CHECK_EQ(var3, &optind);
 
 	return NULL;
 }
Index: src/tests/lib/libc/tls/t_tls_dynamic.c
diff -u src/tests/lib/libc/tls/t_tls_dynamic.c:1.1 src/tests/lib/libc/tls/t_tls_dynamic.c:1.2
--- src/tests/lib/libc/tls/t_tls_dynamic.c:1.1	Wed Mar  9 23:10:07 2011
+++ src/tests/lib/libc/tls/t_tls_dynamic.c	Thu Nov 17 16:20:11 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: t_tls_dynamic.c,v 1.1 2011/03/09 23:10:07 joerg Exp $	*/
+/*	$NetBSD: t_tls_dynamic.c,v 1.2 2011/11/17 16:20:11 joerg Exp $	*/
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
  * All rights reserved.
@@ -32,10 +32,11 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_tls_dynamic.c,v 1.1 2011/03/09 23:10:07 joerg Exp $");
+__RCSID("$NetBSD: t_tls_dynamic.c,v 1.2 2011/11/17 16:20:11 joerg Exp $");
 
 #include <atf-c.h>
 #include <pthread.h>
+#include <unistd.h>
 
 #include <sys/tls.h>
 
@@ -55,6 +56,11 @@ void testf_dso_helper(int, int);
 
 extern __thread int var1;
 extern __thread int var2;
+extern __thread pid_t (*dso_var1)(void);
+
+__thread int *var3 = &optind;
+int var4_helper;
+__thread int *var4 = &var4_helper;
 
 static void *
 testf(void *dummy)
@@ -67,6 +73,9 @@ testf(void *dummy)
 	testf_dso_helper(3, 3);
 	ATF_CHECK_EQ(var1, 3);
 	ATF_CHECK_EQ(var2, 3);
+	ATF_CHECK_EQ(var3, &optind);
+	ATF_CHECK_EQ(var4, &var4_helper);
+	ATF_CHECK_EQ(dso_var1, getpid);
 
 	return NULL;
 }

Index: src/tests/lib/libc/tls/dso/h_tls_dlopen.c
diff -u src/tests/lib/libc/tls/dso/h_tls_dlopen.c:1.1 src/tests/lib/libc/tls/dso/h_tls_dlopen.c:1.2
--- src/tests/lib/libc/tls/dso/h_tls_dlopen.c:1.1	Wed Mar  9 23:10:08 2011
+++ src/tests/lib/libc/tls/dso/h_tls_dlopen.c	Thu Nov 17 16:20:11 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: h_tls_dlopen.c,v 1.1 2011/03/09 23:10:08 joerg Exp $	*/
+/*	$NetBSD: h_tls_dlopen.c,v 1.2 2011/11/17 16:20:11 joerg Exp $	*/
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
  * All rights reserved.
@@ -32,8 +32,10 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: h_tls_dlopen.c,v 1.1 2011/03/09 23:10:08 joerg Exp $");
+__RCSID("$NetBSD: h_tls_dlopen.c,v 1.2 2011/11/17 16:20:11 joerg Exp $");
 
+#include <atf-c.h>
+#include <unistd.h>
 #include <sys/tls.h>
 
 #if !defined(__HAVE_TLS_VARIANT_I) && !defined(__HAVE_TLS_VARIANT_II)
@@ -42,6 +44,8 @@ __RCSID("$NetBSD: h_tls_dlopen.c,v 1.1 2
 
 extern __thread int var1;
 extern __thread int var2;
+extern __thread int *var3;
+__thread static pid_t (*local_var)(void) = getpid;
 
 void testf_dso_helper(int x, int y);
 
@@ -50,4 +54,6 @@ testf_dso_helper(int x, int y)
 {
 	var1 = x;
 	var2 = y;
+	var3 = &optind;
+	ATF_CHECK_EQ(local_var, getpid);
 }

Index: src/tests/lib/libc/tls_dso/h_tls_dynamic.c
diff -u src/tests/lib/libc/tls_dso/h_tls_dynamic.c:1.1 src/tests/lib/libc/tls_dso/h_tls_dynamic.c:1.2
--- src/tests/lib/libc/tls_dso/h_tls_dynamic.c:1.1	Wed Mar  9 23:10:08 2011
+++ src/tests/lib/libc/tls_dso/h_tls_dynamic.c	Thu Nov 17 16:20:11 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: h_tls_dynamic.c,v 1.1 2011/03/09 23:10:08 joerg Exp $	*/
+/*	$NetBSD: h_tls_dynamic.c,v 1.2 2011/11/17 16:20:11 joerg Exp $	*/
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
  * All rights reserved.
@@ -32,8 +32,9 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: h_tls_dynamic.c,v 1.1 2011/03/09 23:10:08 joerg Exp $");
+__RCSID("$NetBSD: h_tls_dynamic.c,v 1.2 2011/11/17 16:20:11 joerg Exp $");
 
+#include <unistd.h>
 #include <sys/tls.h>
 
 #if !defined(__HAVE_TLS_VARIANT_I) && !defined(__HAVE_TLS_VARIANT_II)
@@ -43,6 +44,8 @@ __RCSID("$NetBSD: h_tls_dynamic.c,v 1.1 
 __thread int var1 = 1;
 __thread int var2;
 
+__thread pid_t (*dso_var1)(void) = getpid;
+
 void testf_dso_helper(int x, int y);
 
 void

Reply via email to