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

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