Fix TLS access for -static -pthread
authorJan Kratochvil <jan.kratochvil@redhat.com>
Wed, 21 May 2014 14:25:53 +0000 (16:25 +0200)
committerJan Kratochvil <jan.kratochvil@redhat.com>
Wed, 21 May 2014 14:25:53 +0000 (16:25 +0200)
I have posted:
TLS variables access for -static -lpthread executables
https://sourceware.org/ml/libc-help/2014-03/msg00024.html
and the GDB patch below has been confirmed as OK for current glibcs.

Further work should be done for newer glibcs:
Improve TLS variables glibc compatibility
https://sourceware.org/bugzilla/show_bug.cgi?id=16954

Still the patch below implements the feature in a fully functional way backward
compatible with current glibcs, it depends on the following glibc source line:
csu/libc-tls.c
main_map->l_tls_modid = 1;

gdb/
2014-05-21  Jan Kratochvil  <jan.kratochvil@redhat.com>

Fix TLS access for -static -pthread.
* linux-thread-db.c (struct thread_db_info): Add td_thr_tlsbase_p.
(try_thread_db_load_1): Initialize it.
(thread_db_get_thread_local_address): Call it if LM is zero.
* target.c (target_translate_tls_address): Remove LM_ADDR zero check.
* target.h (struct target_ops) (to_get_thread_local_address): Add
load_module_addr comment.

gdb/gdbserver/
2014-05-21  Jan Kratochvil  <jan.kratochvil@redhat.com>

Fix TLS access for -static -pthread.
* gdbserver/thread-db.c (struct thread_db): Add td_thr_tlsbase_p.
(thread_db_get_tls_address): Call it if LOAD_MODULE is zero.
(thread_db_load_search, try_thread_db_load_1): Initialize it.

gdb/testsuite/
2014-05-21  Jan Kratochvil  <jan.kratochvil@redhat.com>

Fix TLS access for -static -pthread.
* gdb.threads/staticthreads.c <HAVE_TLS> (tlsvar): New.
<HAVE_TLS> (thread_function, main): Initialize it.
* gdb.threads/staticthreads.exp: Try gdb_compile_pthreads for $have_tls.
Add clean_restart.
<$have_tls != "">: Check TLSVAR.

Message-ID: <20140410115204.GB16411@host2.jankratochvil.net>

gdb/ChangeLog
gdb/gdbserver/ChangeLog
gdb/gdbserver/thread-db.c
gdb/linux-thread-db.c
gdb/target.c
gdb/target.h
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.threads/staticthreads.c
gdb/testsuite/gdb.threads/staticthreads.exp

index 6fe325de7eb98309daa8f1b92ccd7ee53b4d01f7..cfd8612b87e95db65a117687d3ed69aeb290e875 100644 (file)
@@ -1,3 +1,13 @@
+2014-05-21  Jan Kratochvil  <jan.kratochvil@redhat.com>
+
+       Fix TLS access for -static -pthread.
+       * linux-thread-db.c (struct thread_db_info): Add td_thr_tlsbase_p.
+       (try_thread_db_load_1): Initialize it.
+       (thread_db_get_thread_local_address): Call it if LM is zero.
+       * target.c (target_translate_tls_address): Remove LM_ADDR zero check.
+       * target.h (struct target_ops) (to_get_thread_local_address): Add
+       load_module_addr comment.
+
 2014-05-21  Pedro Alves  <palves@redhat.com>
 
        * dcache.c (dcache_read_memory_partial): If reading the cache line
index 35225f10ca0bab481fd9faebc4a25b8fbca1a8bf..81bd30efb7504ca843c780fa451e8ccb62c8e740 100644 (file)
@@ -1,3 +1,10 @@
+2014-05-21  Jan Kratochvil  <jan.kratochvil@redhat.com>
+
+       Fix TLS access for -static -pthread.
+       * gdbserver/thread-db.c (struct thread_db): Add td_thr_tlsbase_p.
+       (thread_db_get_tls_address): Call it if LOAD_MODULE is zero.
+       (thread_db_load_search, try_thread_db_load_1): Initialize it.
+
 2014-05-20  Pedro Alves  <palves@redhat.com>
 
        * linux-aarch64-low.c (aarch64_insert_point)
index ae0d191aba3cde89f88a1dff106134f886b6b97f..3ea0cc3f3bfcfed1e460a5bf8a976151e0108734 100644 (file)
@@ -88,6 +88,9 @@ struct thread_db
   td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th,
                                     psaddr_t map_address,
                                     size_t offset, psaddr_t *address);
+  td_err_e (*td_thr_tlsbase_p) (const td_thrhandle_t *th,
+                               unsigned long int modid,
+                               psaddr_t *base);
   const char ** (*td_symbol_list_p) (void);
 };
 
@@ -503,7 +506,10 @@ thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
   if (thread_db == NULL || !thread_db->all_symbols_looked_up)
     return TD_ERR;
 
-  if (thread_db->td_thr_tls_get_addr_p == NULL)
+  /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase
+     could work.  */
+  if (thread_db->td_thr_tls_get_addr_p == NULL
+      || (load_module == 0 && thread_db->td_thr_tlsbase_p == NULL))
     return -1;
 
   lwp = get_thread_lwp (thread);
@@ -514,12 +520,28 @@ thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
 
   saved_inferior = current_inferior;
   current_inferior = thread;
-  /* Note the cast through uintptr_t: this interface only works if
-     a target address fits in a psaddr_t, which is a host pointer.
-     So a 32-bit debugger can not access 64-bit TLS through this.  */
-  err = thread_db->td_thr_tls_get_addr_p (&lwp->th,
-                                         (psaddr_t) (uintptr_t) load_module,
-                                         offset, &addr);
+
+  if (load_module != 0)
+    {
+      /* Note the cast through uintptr_t: this interface only works if
+        a target address fits in a psaddr_t, which is a host pointer.
+        So a 32-bit debugger can not access 64-bit TLS through this.  */
+      err = thread_db->td_thr_tls_get_addr_p (&lwp->th,
+                                            (psaddr_t) (uintptr_t) load_module,
+                                             offset, &addr);
+    }
+  else
+    {
+      /* This code path handles the case of -static -pthread executables:
+        https://sourceware.org/ml/libc-help/2014-03/msg00024.html
+        For older GNU libc r_debug.r_map is NULL.  For GNU libc after
+        PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
+        The constant number 1 depends on GNU __libc_setup_tls
+        initialization of l_tls_modid to 1.  */
+      err = thread_db->td_thr_tlsbase_p (&lwp->th, 1, &addr);
+      addr = (char *) addr + offset;
+    }
+
   current_inferior = saved_inferior;
   if (err == TD_OK)
     {
@@ -571,6 +593,7 @@ thread_db_load_search (void)
   tdb->td_ta_set_event_p = &td_ta_set_event;
   tdb->td_ta_event_getmsg_p = &td_ta_event_getmsg;
   tdb->td_thr_tls_get_addr_p = &td_thr_tls_get_addr;
+  tdb->td_thr_tlsbase_p = &td_thr_tlsbase;
 
   return 1;
 }
@@ -639,6 +662,7 @@ try_thread_db_load_1 (void *handle)
   CHK (0, tdb->td_ta_set_event_p = dlsym (handle, "td_ta_set_event"));
   CHK (0, tdb->td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg"));
   CHK (0, tdb->td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr"));
+  CHK (0, tdb->td_thr_tlsbase_p = dlsym (handle, "td_thr_tlsbase"));
 
 #undef CHK
 
index ca614a3e72ff3dcba00e9379a9628ae305959d88..c0f7b1a4a59cca02d99d9ed07aae10558eae2c41 100644 (file)
@@ -196,6 +196,9 @@ struct thread_db_info
   td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th,
                                     psaddr_t map_address,
                                     size_t offset, psaddr_t *address);
+  td_err_e (*td_thr_tlsbase_p) (const td_thrhandle_t *th,
+                               unsigned long int modid,
+                               psaddr_t *base);
 };
 
 /* List of known processes using thread_db, and the required
@@ -799,6 +802,7 @@ try_thread_db_load_1 (struct thread_db_info *info)
   info->td_ta_event_getmsg_p = dlsym (info->handle, "td_ta_event_getmsg");
   info->td_thr_event_enable_p = dlsym (info->handle, "td_thr_event_enable");
   info->td_thr_tls_get_addr_p = dlsym (info->handle, "td_thr_tls_get_addr");
+  info->td_thr_tlsbase_p = dlsym (info->handle, "td_thr_tlsbase");
 
   if (thread_db_find_new_threads_silently (inferior_ptid) != 0)
     {
@@ -1811,21 +1815,39 @@ thread_db_get_thread_local_address (struct target_ops *ops,
 
       info = get_thread_db_info (ptid_get_pid (ptid));
 
-      /* glibc doesn't provide the needed interface.  */
-      if (!info->td_thr_tls_get_addr_p)
-       throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR,
-                    _("No TLS library support"));
-
-      /* Caller should have verified that lm != 0.  */
-      gdb_assert (lm != 0);
-
       /* Finally, get the address of the variable.  */
-      /* Note the cast through uintptr_t: this interface only works if
-        a target address fits in a psaddr_t, which is a host pointer.
-        So a 32-bit debugger can not access 64-bit TLS through this.  */
-      err = info->td_thr_tls_get_addr_p (&thread_info->private->th,
-                                        (psaddr_t)(uintptr_t) lm,
-                                        offset, &address);
+      if (lm != 0)
+       {
+         /* glibc doesn't provide the needed interface.  */
+         if (!info->td_thr_tls_get_addr_p)
+           throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR,
+                        _("No TLS library support"));
+
+         /* Note the cast through uintptr_t: this interface only works if
+            a target address fits in a psaddr_t, which is a host pointer.
+            So a 32-bit debugger can not access 64-bit TLS through this.  */
+         err = info->td_thr_tls_get_addr_p (&thread_info->private->th,
+                                            (psaddr_t)(uintptr_t) lm,
+                                            offset, &address);
+       }
+      else
+       {
+         /* If glibc doesn't provide the needed interface throw an error
+            that LM is zero - normally cases it should not be.  */
+         if (!info->td_thr_tlsbase_p)
+           throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR,
+                        _("TLS load module not found"));
+
+         /* This code path handles the case of -static -pthread executables:
+            https://sourceware.org/ml/libc-help/2014-03/msg00024.html
+            For older GNU libc r_debug.r_map is NULL.  For GNU libc after
+            PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
+            The constant number 1 depends on GNU __libc_setup_tls
+            initialization of l_tls_modid to 1.  */
+         err = info->td_thr_tlsbase_p (&thread_info->private->th,
+                                       1, &address);
+         address = (char *) address + offset;
+       }
 
 #ifdef THREAD_DB_HAS_TD_NOTALLOC
       /* The memory hasn't been allocated, yet.  */
index d08e2ea720210047ff23355cf5a01fde378661c1..71292d333a34bd21dc2d932523c673236f69129c 100644 (file)
@@ -757,10 +757,6 @@ target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset)
          /* Fetch the load module address for this objfile.  */
          lm_addr = gdbarch_fetch_tls_load_module_address (target_gdbarch (),
                                                           objfile);
-         /* If it's 0, throw the appropriate exception.  */
-         if (lm_addr == 0)
-           throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR,
-                        _("TLS load module not found"));
 
          addr = target->to_get_thread_local_address (target, ptid,
                                                      lm_addr, offset);
index 23a75664524e897b52225d5dd584ebcd654d8251..937152902aea40ad0b9fd64dbe1bc490a51ee1c2 100644 (file)
@@ -605,7 +605,8 @@ struct target_ops
        thread-local storage for the thread PTID and the shared library
        or executable file given by OBJFILE.  If that block of
        thread-local storage hasn't been allocated yet, this function
-       may return an error.  */
+       may return an error.  LOAD_MODULE_ADDR may be zero for statically
+       linked multithreaded inferiors.  */
     CORE_ADDR (*to_get_thread_local_address) (struct target_ops *ops,
                                              ptid_t ptid,
                                              CORE_ADDR load_module_addr,
index 1e39142e688836cd40f1ec262aef750aa6863e3b..56b6cc5f7329353a4b7ff8528670d70e4f0347a4 100644 (file)
@@ -1,3 +1,12 @@
+2014-05-21  Jan Kratochvil  <jan.kratochvil@redhat.com>
+
+       Fix TLS access for -static -pthread.
+       * gdb.threads/staticthreads.c <HAVE_TLS> (tlsvar): New.
+       <HAVE_TLS> (thread_function, main): Initialize it.
+       * gdb.threads/staticthreads.exp: Try gdb_compile_pthreads for $have_tls.
+       Add clean_restart.
+       <$have_tls != "">: Check TLSVAR.
+
 2014-05-21  Pedro Alves  <palves@redhat.com>
 
        * gdb.base/dcache-line-read-error.c: New.
index e834d7f465562e8efcb4a3a041ba3d5d9c289570..5c8eabea47ed6f1e40c434f668562e9c1ff273a4 100644 (file)
 
 sem_t semaphore;
 
+#ifdef HAVE_TLS
+__thread int tlsvar;
+#endif
+
 void *
 thread_function (void *arg)
 {
-  printf ("Thread executing\n");
+#ifdef HAVE_TLS
+  tlsvar = 2;
+#endif
+  printf ("Thread executing\n"); /* tlsvar-is-set */
   while (sem_wait (&semaphore) != 0)
     {
       if (errno != EINTR)
@@ -57,6 +64,9 @@ main (int argc, char **argv)
       return -1;
     }
 
+#ifdef HAVE_TLS
+  tlsvar = 1;
+#endif
 
   /* Create a thread, wait for it to complete.  */
   {
index 80b0ba879e4ca29990ad87a4c0f9a3ddbebb5419..9fa625a83ca047d3a3891d02adaaa6c16868e953 100644 (file)
 standard_testfile
 set static_flag "-static"
 
-if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" \
-        executable \
-        [list debug "additional_flags=${static_flag}" \
-            ]] != "" } {
-    return -1
+foreach have_tls { "-DHAVE_TLS" "" } {
+    if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" \
+            executable \
+            [list debug "additional_flags=${static_flag} ${have_tls}" \
+                ]] == "" } {
+       break
+    }
+    if { $have_tls == "" } {
+       return -1
+    }
 }
 
 clean_restart ${binfile}
@@ -89,3 +94,18 @@ gdb_test_multiple "quit" "$test" {
         pass "$test"
     }
 }
+clean_restart ${binfile}
+
+
+if { "$have_tls" != "" } {
+    if ![runto_main] {
+       return -1
+    }
+    gdb_breakpoint [gdb_get_line_number "tlsvar-is-set"]
+    gdb_continue_to_breakpoint "tlsvar-is-set" ".* tlsvar-is-set .*"
+    gdb_test "p tlsvar" " = 2" "tlsvar in thread"
+    gdb_test "thread 1" ".*"
+    # Unwind from pthread_join.
+    gdb_test "up 10" " in main .*"
+    gdb_test "p tlsvar" " = 1" "tlsvar in main"
+}
This page took 0.041418 seconds and 4 git commands to generate.