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

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