Blame SOURCES/gdb-rhbz1080657-tls-variable-static-linked-binary-2of3.patch

7ab123
commit 5876f5032f60c45c4bd19e7ea7d0c14d0346b93e
7ab123
Author: Jan Kratochvil <jan.kratochvil@redhat.com>
7ab123
Date:   Wed May 21 16:25:53 2014 +0200
7ab123
7ab123
    Fix TLS access for -static -pthread
7ab123
    
7ab123
    I have posted:
7ab123
    	TLS variables access for -static -lpthread executables
7ab123
    	https://sourceware.org/ml/libc-help/2014-03/msg00024.html
7ab123
    and the GDB patch below has been confirmed as OK for current glibcs.
7ab123
    
7ab123
    Further work should be done for newer glibcs:
7ab123
    	Improve TLS variables glibc compatibility
7ab123
    	https://sourceware.org/bugzilla/show_bug.cgi?id=16954
7ab123
    
7ab123
    Still the patch below implements the feature in a fully functional way backward
7ab123
    compatible with current glibcs, it depends on the following glibc source line:
7ab123
    	csu/libc-tls.c
7ab123
    	main_map->l_tls_modid = 1;
7ab123
    
7ab123
    gdb/
7ab123
    2014-05-21  Jan Kratochvil  <jan.kratochvil@redhat.com>
7ab123
    
7ab123
    	Fix TLS access for -static -pthread.
7ab123
    	* linux-thread-db.c (struct thread_db_info): Add td_thr_tlsbase_p.
7ab123
    	(try_thread_db_load_1): Initialize it.
7ab123
    	(thread_db_get_thread_local_address): Call it if LM is zero.
7ab123
    	* target.c (target_translate_tls_address): Remove LM_ADDR zero check.
7ab123
    	* target.h (struct target_ops) (to_get_thread_local_address): Add
7ab123
    	load_module_addr comment.
7ab123
    
7ab123
    gdb/gdbserver/
7ab123
    2014-05-21  Jan Kratochvil  <jan.kratochvil@redhat.com>
7ab123
    
7ab123
    	Fix TLS access for -static -pthread.
7ab123
    	* gdbserver/thread-db.c (struct thread_db): Add td_thr_tlsbase_p.
7ab123
    	(thread_db_get_tls_address): Call it if LOAD_MODULE is zero.
7ab123
    	(thread_db_load_search, try_thread_db_load_1): Initialize it.
7ab123
    
7ab123
    gdb/testsuite/
7ab123
    2014-05-21  Jan Kratochvil  <jan.kratochvil@redhat.com>
7ab123
    
7ab123
    	Fix TLS access for -static -pthread.
7ab123
    	* gdb.threads/staticthreads.c <HAVE_TLS> (tlsvar): New.
7ab123
    	<HAVE_TLS> (thread_function, main): Initialize it.
7ab123
    	* gdb.threads/staticthreads.exp: Try gdb_compile_pthreads for $have_tls.
7ab123
    	Add clean_restart.
7ab123
    	<$have_tls != "">: Check TLSVAR.
7ab123
    
7ab123
    Message-ID: <20140410115204.GB16411@host2.jankratochvil.net>
7ab123
7ab123
Index: gdb-7.6.1/gdb/gdbserver/thread-db.c
7ab123
===================================================================
7ab123
--- gdb-7.6.1.orig/gdb/gdbserver/thread-db.c
7ab123
+++ gdb-7.6.1/gdb/gdbserver/thread-db.c
7ab123
@@ -88,6 +88,9 @@ struct thread_db
7ab123
   td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th,
7ab123
 				     psaddr_t map_address,
7ab123
 				     size_t offset, psaddr_t *address);
7ab123
+  td_err_e (*td_thr_tlsbase_p) (const td_thrhandle_t *th,
7ab123
+				unsigned long int modid,
7ab123
+				psaddr_t *base);
7ab123
   const char ** (*td_symbol_list_p) (void);
7ab123
 };
7ab123
 
7ab123
@@ -497,7 +500,10 @@ thread_db_get_tls_address (struct thread
7ab123
   if (thread_db == NULL || !thread_db->all_symbols_looked_up)
7ab123
     return TD_ERR;
7ab123
 
7ab123
-  if (thread_db->td_thr_tls_get_addr_p == NULL)
7ab123
+  /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase
7ab123
+     could work.  */
7ab123
+  if (thread_db->td_thr_tls_get_addr_p == NULL
7ab123
+      || (load_module == 0 && thread_db->td_thr_tlsbase_p == NULL))
7ab123
     return -1;
7ab123
 
7ab123
   lwp = get_thread_lwp (thread);
7ab123
@@ -508,12 +514,28 @@ thread_db_get_tls_address (struct thread
7ab123
 
7ab123
   saved_inferior = current_inferior;
7ab123
   current_inferior = thread;
7ab123
-  /* Note the cast through uintptr_t: this interface only works if
7ab123
-     a target address fits in a psaddr_t, which is a host pointer.
7ab123
-     So a 32-bit debugger can not access 64-bit TLS through this.  */
7ab123
-  err = thread_db->td_thr_tls_get_addr_p (&lwp->th,
7ab123
-					  (psaddr_t) (uintptr_t) load_module,
7ab123
-					  offset, &addr);
7ab123
+
7ab123
+  if (load_module != 0)
7ab123
+    {
7ab123
+      /* Note the cast through uintptr_t: this interface only works if
7ab123
+	 a target address fits in a psaddr_t, which is a host pointer.
7ab123
+	 So a 32-bit debugger can not access 64-bit TLS through this.  */
7ab123
+      err = thread_db->td_thr_tls_get_addr_p (&lwp->th,
7ab123
+					     (psaddr_t) (uintptr_t) load_module,
7ab123
+					      offset, &addr);
7ab123
+    }
7ab123
+  else
7ab123
+    {
7ab123
+      /* This code path handles the case of -static -pthread executables:
7ab123
+	 https://sourceware.org/ml/libc-help/2014-03/msg00024.html
7ab123
+	 For older GNU libc r_debug.r_map is NULL.  For GNU libc after
7ab123
+	 PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
7ab123
+	 The constant number 1 depends on GNU __libc_setup_tls
7ab123
+	 initialization of l_tls_modid to 1.  */
7ab123
+      err = thread_db->td_thr_tlsbase_p (&lwp->th, 1, &addr);
7ab123
+      addr = (char *) addr + offset;
7ab123
+    }
7ab123
+
7ab123
   current_inferior = saved_inferior;
7ab123
   if (err == TD_OK)
7ab123
     {
7ab123
@@ -565,6 +587,7 @@ thread_db_load_search (void)
7ab123
   tdb->td_ta_set_event_p = &td_ta_set_event;
7ab123
   tdb->td_ta_event_getmsg_p = &td_ta_event_getmsg;
7ab123
   tdb->td_thr_tls_get_addr_p = &td_thr_tls_get_addr;
7ab123
+  tdb->td_thr_tlsbase_p = &td_thr_tlsbase;
7ab123
 
7ab123
   return 1;
7ab123
 }
7ab123
@@ -633,6 +656,7 @@ try_thread_db_load_1 (void *handle)
7ab123
   CHK (0, tdb->td_ta_set_event_p = dlsym (handle, "td_ta_set_event"));
7ab123
   CHK (0, tdb->td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg"));
7ab123
   CHK (0, tdb->td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr"));
7ab123
+  CHK (0, tdb->td_thr_tlsbase_p = dlsym (handle, "td_thr_tlsbase"));
7ab123
 
7ab123
 #undef CHK
7ab123
 
7ab123
Index: gdb-7.6.1/gdb/linux-thread-db.c
7ab123
===================================================================
7ab123
--- gdb-7.6.1.orig/gdb/linux-thread-db.c
7ab123
+++ gdb-7.6.1/gdb/linux-thread-db.c
7ab123
@@ -196,6 +196,9 @@ struct thread_db_info
7ab123
   td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th,
7ab123
 				     psaddr_t map_address,
7ab123
 				     size_t offset, psaddr_t *address);
7ab123
+  td_err_e (*td_thr_tlsbase_p) (const td_thrhandle_t *th,
7ab123
+				unsigned long int modid,
7ab123
+				psaddr_t *base);
7ab123
 };
7ab123
 
7ab123
 /* List of known processes using thread_db, and the required
7ab123
@@ -797,6 +800,7 @@ try_thread_db_load_1 (struct thread_db_i
7ab123
   info->td_ta_event_getmsg_p = dlsym (info->handle, "td_ta_event_getmsg");
7ab123
   info->td_thr_event_enable_p = dlsym (info->handle, "td_thr_event_enable");
7ab123
   info->td_thr_tls_get_addr_p = dlsym (info->handle, "td_thr_tls_get_addr");
7ab123
+  info->td_thr_tlsbase_p = dlsym (info->handle, "td_thr_tlsbase");
7ab123
 
7ab123
   if (thread_db_find_new_threads_silently (inferior_ptid) != 0)
7ab123
     {
7ab123
@@ -1798,21 +1802,39 @@ thread_db_get_thread_local_address (stru
7ab123
 
7ab123
       info = get_thread_db_info (GET_PID (ptid));
7ab123
 
7ab123
-      /* glibc doesn't provide the needed interface.  */
7ab123
-      if (!info->td_thr_tls_get_addr_p)
7ab123
-	throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR,
7ab123
-		     _("No TLS library support"));
7ab123
-
7ab123
-      /* Caller should have verified that lm != 0.  */
7ab123
-      gdb_assert (lm != 0);
7ab123
-
7ab123
       /* Finally, get the address of the variable.  */
7ab123
-      /* Note the cast through uintptr_t: this interface only works if
7ab123
-	 a target address fits in a psaddr_t, which is a host pointer.
7ab123
-	 So a 32-bit debugger can not access 64-bit TLS through this.  */
7ab123
-      err = info->td_thr_tls_get_addr_p (&thread_info->private->th,
7ab123
-					 (psaddr_t)(uintptr_t) lm,
7ab123
-					 offset, &address);
7ab123
+      if (lm != 0)
7ab123
+	{
7ab123
+	  /* glibc doesn't provide the needed interface.  */
7ab123
+	  if (!info->td_thr_tls_get_addr_p)
7ab123
+	    throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR,
7ab123
+			 _("No TLS library support"));
7ab123
+
7ab123
+	  /* Note the cast through uintptr_t: this interface only works if
7ab123
+	     a target address fits in a psaddr_t, which is a host pointer.
7ab123
+	     So a 32-bit debugger can not access 64-bit TLS through this.  */
7ab123
+	  err = info->td_thr_tls_get_addr_p (&thread_info->private->th,
7ab123
+					     (psaddr_t)(uintptr_t) lm,
7ab123
+					     offset, &address);
7ab123
+	}
7ab123
+      else
7ab123
+	{
7ab123
+	  /* If glibc doesn't provide the needed interface throw an error
7ab123
+	     that LM is zero - normally cases it should not be.  */
7ab123
+	  if (!info->td_thr_tlsbase_p)
7ab123
+	    throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR,
7ab123
+			 _("TLS load module not found"));
7ab123
+
7ab123
+	  /* This code path handles the case of -static -pthread executables:
7ab123
+	     https://sourceware.org/ml/libc-help/2014-03/msg00024.html
7ab123
+	     For older GNU libc r_debug.r_map is NULL.  For GNU libc after
7ab123
+	     PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
7ab123
+	     The constant number 1 depends on GNU __libc_setup_tls
7ab123
+	     initialization of l_tls_modid to 1.  */
7ab123
+	  err = info->td_thr_tlsbase_p (&thread_info->private->th,
7ab123
+					1, &address);
7ab123
+	  address = (char *) address + offset;
7ab123
+	}
7ab123
 
7ab123
 #ifdef THREAD_DB_HAS_TD_NOTALLOC
7ab123
       /* The memory hasn't been allocated, yet.  */
7ab123
Index: gdb-7.6.1/gdb/target.c
7ab123
===================================================================
7ab123
--- gdb-7.6.1.orig/gdb/target.c
7ab123
+++ gdb-7.6.1/gdb/target.c
7ab123
@@ -1165,10 +1165,6 @@ target_translate_tls_address (struct obj
7ab123
 	  /* Fetch the load module address for this objfile.  */
7ab123
 	  lm_addr = gdbarch_fetch_tls_load_module_address (target_gdbarch (),
7ab123
 	                                                   objfile);
7ab123
-	  /* If it's 0, throw the appropriate exception.  */
7ab123
-	  if (lm_addr == 0)
7ab123
-	    throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR,
7ab123
-			 _("TLS load module not found"));
7ab123
 
7ab123
 	  addr = target->to_get_thread_local_address (target, ptid,
7ab123
 						      lm_addr, offset);
7ab123
Index: gdb-7.6.1/gdb/target.h
7ab123
===================================================================
7ab123
--- gdb-7.6.1.orig/gdb/target.h
7ab123
+++ gdb-7.6.1/gdb/target.h
7ab123
@@ -557,7 +557,8 @@ struct target_ops
7ab123
        thread-local storage for the thread PTID and the shared library
7ab123
        or executable file given by OBJFILE.  If that block of
7ab123
        thread-local storage hasn't been allocated yet, this function
7ab123
-       may return an error.  */
7ab123
+       may return an error.  LOAD_MODULE_ADDR may be zero for statically
7ab123
+       linked multithreaded inferiors.  */
7ab123
     CORE_ADDR (*to_get_thread_local_address) (struct target_ops *ops,
7ab123
 					      ptid_t ptid,
7ab123
 					      CORE_ADDR load_module_addr,
7ab123
Index: gdb-7.6.1/gdb/testsuite/gdb.threads/staticthreads.c
7ab123
===================================================================
7ab123
--- gdb-7.6.1.orig/gdb/testsuite/gdb.threads/staticthreads.c
7ab123
+++ gdb-7.6.1/gdb/testsuite/gdb.threads/staticthreads.c
7ab123
@@ -28,10 +28,17 @@
7ab123
 
7ab123
 sem_t semaphore;
7ab123
 
7ab123
+#ifdef HAVE_TLS
7ab123
+__thread int tlsvar;
7ab123
+#endif
7ab123
+
7ab123
 void *
7ab123
 thread_function (void *arg)
7ab123
 {
7ab123
-  printf ("Thread executing\n");
7ab123
+#ifdef HAVE_TLS
7ab123
+  tlsvar = 2;
7ab123
+#endif
7ab123
+  printf ("Thread executing\n"); /* tlsvar-is-set */
7ab123
   while (sem_wait (&semaphore) != 0)
7ab123
     {
7ab123
       if (errno != EINTR)
7ab123
@@ -57,6 +64,9 @@ main (int argc, char **argv)
7ab123
       return -1;
7ab123
     }
7ab123
 
7ab123
+#ifdef HAVE_TLS
7ab123
+  tlsvar = 1;
7ab123
+#endif
7ab123
 
7ab123
   /* Create a thread, wait for it to complete.  */
7ab123
   {
7ab123
Index: gdb-7.6.1/gdb/testsuite/gdb.threads/staticthreads.exp
7ab123
===================================================================
7ab123
--- gdb-7.6.1.orig/gdb/testsuite/gdb.threads/staticthreads.exp
7ab123
+++ gdb-7.6.1/gdb/testsuite/gdb.threads/staticthreads.exp
7ab123
@@ -22,11 +22,16 @@
7ab123
 standard_testfile
7ab123
 set static_flag "-static"
7ab123
 
7ab123
-if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" \
7ab123
-	 executable \
7ab123
-	 [list debug "additional_flags=${static_flag}" \
7ab123
-	     ]] != "" } {
7ab123
-    return -1
7ab123
+foreach have_tls { "-DHAVE_TLS" "" } {
7ab123
+    if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" \
7ab123
+	     executable \
7ab123
+	     [list debug "additional_flags=${static_flag} ${have_tls}" \
7ab123
+		 ]] == "" } {
7ab123
+	break
7ab123
+    }
7ab123
+    if { $have_tls == "" } {
7ab123
+	return -1
7ab123
+    }
7ab123
 }
7ab123
 
7ab123
 clean_restart ${binfile}
7ab123
@@ -96,3 +101,18 @@ gdb_test_multiple "quit" "$test" {
7ab123
         pass "$test"
7ab123
     }
7ab123
 }
7ab123
+clean_restart ${binfile}
7ab123
+
7ab123
+
7ab123
+if { "$have_tls" != "" } {
7ab123
+    if ![runto_main] {
7ab123
+	return -1
7ab123
+    }
7ab123
+    gdb_breakpoint [gdb_get_line_number "tlsvar-is-set"]
7ab123
+    gdb_continue_to_breakpoint "tlsvar-is-set" ".* tlsvar-is-set .*"
7ab123
+    gdb_test "p tlsvar" " = 2" "tlsvar in thread"
7ab123
+    gdb_test "thread 1" ".*"
7ab123
+    # Unwind from pthread_join.
7ab123
+    gdb_test "up 10" " in main .*"
7ab123
+    gdb_test "p tlsvar" " = 1" "tlsvar in main"
7ab123
+}