00db10
# 
00db10
# commit f8aeae347377f3dfa8cbadde057adf1827fb1d44
00db10
# Author: Alexandre Oliva <aoliva@redhat.com>
00db10
# Date:   Tue Mar 17 01:14:11 2015 -0300
00db10
# 
00db10
#     Fix DTV race, assert, DTV_SURPLUS Static TLS limit, and nptl_db garbage
00db10
#     
00db10
#     for  ChangeLog
00db10
#     
00db10
#         [BZ #17090]
00db10
#         [BZ #17620]
00db10
#         [BZ #17621]
00db10
#         [BZ #17628]
00db10
#         * NEWS: Update.
00db10
#         * elf/dl-tls.c (_dl_update_slotinfo): Clean up outdated DTV
00db10
#         entries with Static TLS too.  Skip entries past the end of the
00db10
#         allocated DTV, from Alan Modra.
00db10
#         (tls_get_addr_tail): Update to glibc_likely/unlikely.  Move
00db10
#         Static TLS DTV entry set up from...
00db10
#          (_dl_allocate_tls_init): ... here (fix modid assertion), ...
00db10
#         * elf/dl-reloc.c (_dl_nothread_init_static_tls): ... here...
00db10
#         * nptl/allocatestack.c (init_one_static_tls): ... and here...
00db10
#         * elf/dlopen.c (dl_open_worker): Drop l_tls_modid upper bound
00db10
#         for Static TLS.
00db10
#         * elf/tlsdeschtab.h (map_generation): Return size_t.  Check
00db10
#         that the slot we find is associated with the given map before
00db10
#         using its generation count.
00db10
#         * nptl_db/db_info.c: Include ldsodefs.h.
00db10
#         (rtld_global, dtv_slotinfo_list, dtv_slotinfo): New typedefs.
00db10
#         * nptl_db/structs.def (DB_RTLD_VARIABLE): New macro.
00db10
#         (DB_MAIN_VARIABLE, DB_RTLD_GLOBAL_FIELD): Likewise.
00db10
#         (link_map::l_tls_offset): New struct field.
00db10
#         (dtv_t::counter): Likewise.
00db10
#         (rtld_global): New struct.
00db10
#         (_rtld_global): New rtld variable.
00db10
#         (dl_tls_dtv_slotinfo_list): New rtld global field.
00db10
#         (dtv_slotinfo_list): New struct.
00db10
#         (dtv_slotinfo): Likewise.
00db10
#         * nptl_db/td_symbol_list.c: Drop gnu/lib-names.h include.
00db10
#         (td_lookup): Rename to...
00db10
#         (td_mod_lookup): ... this.  Use new mod parameter instead of
00db10
#         LIBPTHREAD_SO.
00db10
#         * nptl_db/td_thr_tlsbase.c: Include link.h.
00db10
#         (dtv_slotinfo_list, dtv_slotinfo): New functions.
00db10
#         (td_thr_tlsbase): Check DTV generation.  Compute Static TLS
00db10
#         addresses even if the DTV is out of date or missing them.
00db10
#         * nptl_db/fetch-value.c (_td_locate_field): Do not refuse to
00db10
#         index zero-length arrays.
00db10
#         * nptl_db/thread_dbP.h: Include gnu/lib-names.h.
00db10
#         (td_lookup): Make it a macro implemented in terms of...
00db10
#         (td_mod_lookup): ... this declaration.
00db10
#         * nptl_db/db-symbols.awk (DB_RTLD_VARIABLE): Override.
00db10
#         (DB_MAIN_VARIABLE): Likewise.
00db10
# 
00db10
diff -urN glibc-2.17-c758a686/elf/dl-open.c glibc-2.17-c758a686/elf/dl-open.c
00db10
--- glibc-2.17-c758a686/elf/dl-open.c	2012-12-24 22:02:13.000000000 -0500
00db10
+++ glibc-2.17-c758a686/elf/dl-open.c	2015-03-17 14:39:47.746505786 -0400
00db10
@@ -535,17 +535,7 @@
00db10
 	  && imap->l_tls_blocksize > 0)
00db10
 	{
00db10
 	  /* For static TLS we have to allocate the memory here and
00db10
-	     now.  This includes allocating memory in the DTV.  But we
00db10
-	     cannot change any DTV other than our own.  So, if we
00db10
-	     cannot guarantee that there is room in the DTV we don't
00db10
-	     even try it and fail the load.
00db10
-
00db10
-	     XXX We could track the minimum DTV slots allocated in
00db10
-	     all threads.  */
00db10
-	  if (! RTLD_SINGLE_THREAD_P && imap->l_tls_modid > DTV_SURPLUS)
00db10
-	    _dl_signal_error (0, "dlopen", NULL, N_("\
00db10
-cannot load any more object with static TLS"));
00db10
-
00db10
+	     now, but we can delay updating the DTV.  */
00db10
 	  imap->l_need_tls_init = 0;
00db10
 #ifdef SHARED
00db10
 	  /* Update the slot information data for at least the
00db10
diff -urN glibc-2.17-c758a686/elf/dl-reloc.c glibc-2.17-c758a686/elf/dl-reloc.c
00db10
--- glibc-2.17-c758a686/elf/dl-reloc.c	2012-12-24 22:02:13.000000000 -0500
00db10
+++ glibc-2.17-c758a686/elf/dl-reloc.c	2015-03-17 14:39:47.747505754 -0400
00db10
@@ -136,12 +136,6 @@
00db10
 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
00db10
 #endif
00db10
 
00db10
-  /* Fill in the DTV slot so that a later LD/GD access will find it.  */
00db10
-  dtv_t *dtv = THREAD_DTV ();
00db10
-  assert (map->l_tls_modid <= dtv[-1].counter);
00db10
-  dtv[map->l_tls_modid].pointer.val = dest;
00db10
-  dtv[map->l_tls_modid].pointer.is_static = true;
00db10
-
00db10
   /* Initialize the memory.  */
00db10
   memset (__mempcpy (dest, map->l_tls_initimage, map->l_tls_initimage_size),
00db10
 	  '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
00db10
diff -urN glibc-2.17-c758a686/elf/dl-tls.c glibc-2.17-c758a686/elf/dl-tls.c
00db10
--- glibc-2.17-c758a686/elf/dl-tls.c	2015-03-17 14:39:31.335019227 -0400
00db10
+++ glibc-2.17-c758a686/elf/dl-tls.c	2015-03-17 14:42:48.443852579 -0400
00db10
@@ -432,17 +432,14 @@
00db10
 	  assert (listp->slotinfo[cnt].gen <= GL(dl_tls_generation));
00db10
 	  maxgen = MAX (maxgen, listp->slotinfo[cnt].gen);
00db10
 
00db10
+	  dtv[map->l_tls_modid].pointer.val = TLS_DTV_UNALLOCATED;
00db10
+	  dtv[map->l_tls_modid].pointer.is_static = false;
00db10
+
00db10
 	  if (map->l_tls_offset == NO_TLS_OFFSET
00db10
 	      || map->l_tls_offset == FORCED_DYNAMIC_TLS_OFFSET)
00db10
-	    {
00db10
-	      /* For dynamically loaded modules we simply store
00db10
-		 the value indicating deferred allocation.  */
00db10
-	      dtv[map->l_tls_modid].pointer.val = TLS_DTV_UNALLOCATED;
00db10
-	      dtv[map->l_tls_modid].pointer.is_static = false;
00db10
-	      continue;
00db10
-	    }
00db10
+	    continue;
00db10
 
00db10
-	  assert (map->l_tls_modid == cnt);
00db10
+	  assert (map->l_tls_modid == total + cnt);
00db10
 	  assert (map->l_tls_blocksize >= map->l_tls_initimage_size);
00db10
 #if TLS_TCB_AT_TP
00db10
 	  assert ((size_t) map->l_tls_offset >= map->l_tls_blocksize);
00db10
@@ -454,8 +451,6 @@
00db10
 #endif
00db10
 
00db10
 	  /* Copy the initialization image and clear the BSS part.  */
00db10
-	  dtv[map->l_tls_modid].pointer.val = dest;
00db10
-	  dtv[map->l_tls_modid].pointer.is_static = true;
00db10
 	  memset (__mempcpy (dest, map->l_tls_initimage,
00db10
 			     map->l_tls_initimage_size), '\0',
00db10
 		  map->l_tls_blocksize - map->l_tls_initimage_size);
00db10
@@ -623,13 +618,16 @@
00db10
 	      struct link_map *map = listp->slotinfo[cnt].map;
00db10
 	      if (map == NULL)
00db10
 		{
00db10
-		  /* If this modid was used at some point the memory
00db10
-		     might still be allocated.  */
00db10
-		  if (! dtv[total + cnt].pointer.is_static
00db10
-		      && dtv[total + cnt].pointer.val != TLS_DTV_UNALLOCATED)
00db10
+		  if (dtv[-1].counter >= total + cnt)
00db10
 		    {
00db10
-		      free (dtv[total + cnt].pointer.val);
00db10
+		      /* If this modid was used at some point the memory
00db10
+			 might still be allocated.  */
00db10
+		      if (! dtv[total + cnt].pointer.is_static
00db10
+			  && (dtv[total + cnt].pointer.val
00db10
+			      != TLS_DTV_UNALLOCATED))
00db10
+			free (dtv[total + cnt].pointer.val);
00db10
 		      dtv[total + cnt].pointer.val = TLS_DTV_UNALLOCATED;
00db10
+		      dtv[total + cnt].pointer.is_static = false;
00db10
 		    }
00db10
 
00db10
 		  continue;
00db10
@@ -693,10 +691,8 @@
00db10
 		   memalign and not malloc.  */
00db10
 		free (dtv[modid].pointer.val);
00db10
 
00db10
-	      /* This module is loaded dynamically- We defer memory
00db10
-		 allocation.  */
00db10
-	      dtv[modid].pointer.is_static = false;
00db10
 	      dtv[modid].pointer.val = TLS_DTV_UNALLOCATED;
00db10
+	      dtv[modid].pointer.is_static = false;
00db10
 
00db10
 	      if (modid == req_modid)
00db10
 		the_map = map;
00db10
@@ -734,13 +730,12 @@
00db10
       the_map = listp->slotinfo[idx].map;
00db10
     }
00db10
 
00db10
- again:
00db10
   /* Make sure that, if a dlopen running in parallel forces the
00db10
      variable into static storage, we'll wait until the address in the
00db10
      static TLS block is set up, and use that.  If we're undecided
00db10
      yet, make sure we make the decision holding the lock as well.  */
00db10
-  if (__builtin_expect (the_map->l_tls_offset
00db10
-			!= FORCED_DYNAMIC_TLS_OFFSET, 0))
00db10
+  if (__glibc_unlikely (the_map->l_tls_offset
00db10
+			!= FORCED_DYNAMIC_TLS_OFFSET))
00db10
     {
00db10
       __rtld_lock_lock_recursive (GL(dl_load_lock));
00db10
       if (__builtin_expect (the_map->l_tls_offset == NO_TLS_OFFSET, 1))
00db10
@@ -748,22 +743,28 @@
00db10
 	  the_map->l_tls_offset = FORCED_DYNAMIC_TLS_OFFSET;
00db10
 	  __rtld_lock_unlock_recursive (GL(dl_load_lock));
00db10
 	}
00db10
-      else
00db10
+      else if (__glibc_likely (the_map->l_tls_offset
00db10
+			       != FORCED_DYNAMIC_TLS_OFFSET))
00db10
 	{
00db10
+#if TLS_TCB_AT_TP
00db10
+	  void *p = (char *) THREAD_SELF - the_map->l_tls_offset;
00db10
+#elif TLS_DTV_AT_TP
00db10
+	  void *p = (char *) THREAD_SELF + the_map->l_tls_offset + TLS_PRE_TCB_SIZE;
00db10
+#else
00db10
+# error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
00db10
+#endif
00db10
 	  __rtld_lock_unlock_recursive (GL(dl_load_lock));
00db10
-	  if (__builtin_expect (the_map->l_tls_offset
00db10
-				!= FORCED_DYNAMIC_TLS_OFFSET, 1))
00db10
-	    {
00db10
-	      void *p = dtv[GET_ADDR_MODULE].pointer.val;
00db10
-	      if (__builtin_expect (p == TLS_DTV_UNALLOCATED, 0))
00db10
-		goto again;
00db10
+	  dtv[GET_ADDR_MODULE].pointer.is_static = true;
00db10
+	  dtv[GET_ADDR_MODULE].pointer.val = p;
00db10
 
00db10
-	      return (char *) p + GET_ADDR_OFFSET;
00db10
-	    }
00db10
+	  return (char *) p + GET_ADDR_OFFSET;
00db10
 	}
00db10
+      else
00db10
+	__rtld_lock_unlock_recursive (GL(dl_load_lock));
00db10
+
00db10
     }
00db10
   void *p = dtv[GET_ADDR_MODULE].pointer.val = allocate_and_init (the_map);
00db10
-  dtv[GET_ADDR_MODULE].pointer.is_static = false;
00db10
+  assert (!dtv[GET_ADDR_MODULE].pointer.is_static);
00db10
 
00db10
   return (char *) p + GET_ADDR_OFFSET;
00db10
 }
00db10
diff -urN glibc-2.17-c758a686/elf/tlsdeschtab.h glibc-2.17-c758a686/elf/tlsdeschtab.h
00db10
--- glibc-2.17-c758a686/elf/tlsdeschtab.h	2012-12-24 22:02:13.000000000 -0500
00db10
+++ glibc-2.17-c758a686/elf/tlsdeschtab.h	2015-03-17 14:39:47.747505754 -0400
00db10
@@ -42,7 +42,7 @@
00db10
   return tdp->tlsinfo.ti_offset == tdq->tlsinfo.ti_offset;
00db10
 }
00db10
 
00db10
-inline static int
00db10
+inline static size_t
00db10
 map_generation (struct link_map *map)
00db10
 {
00db10
   size_t idx = map->l_tls_modid;
00db10
@@ -58,7 +58,7 @@
00db10
 	     we can assume that, if the generation count is zero, we
00db10
 	     still haven't determined the generation count for this
00db10
 	     module.  */
00db10
-	  if (listp->slotinfo[idx].gen)
00db10
+	  if (listp->slotinfo[idx].map == map && listp->slotinfo[idx].gen)
00db10
 	    return listp->slotinfo[idx].gen;
00db10
 	  else
00db10
 	    break;
00db10
diff -urN glibc-2.17-c758a686/nptl/allocatestack.c glibc-2.17-c758a686/nptl/allocatestack.c
00db10
--- glibc-2.17-c758a686/nptl/allocatestack.c	2015-03-17 14:39:31.342019008 -0400
00db10
+++ glibc-2.17-c758a686/nptl/allocatestack.c	2015-03-17 14:39:47.747505754 -0400
00db10
@@ -1157,7 +1157,6 @@
00db10
 static inline void __attribute__((always_inline))
00db10
 init_one_static_tls (struct pthread *curp, struct link_map *map)
00db10
 {
00db10
-  dtv_t *dtv = GET_DTV (TLS_TPADJ (curp));
00db10
 # if TLS_TCB_AT_TP
00db10
   void *dest = (char *) curp - map->l_tls_offset;
00db10
 # elif TLS_DTV_AT_TP
00db10
@@ -1166,11 +1165,9 @@
00db10
 #  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
00db10
 # endif
00db10
 
00db10
-  /* Fill in the DTV slot so that a later LD/GD access will find it.  */
00db10
-  dtv[map->l_tls_modid].pointer.val = dest;
00db10
-  dtv[map->l_tls_modid].pointer.is_static = true;
00db10
-
00db10
-  /* Initialize the memory.  */
00db10
+  /* We cannot delay the initialization of the Static TLS area, since
00db10
+     it can be accessed with LE or IE, but since the DTV is only used
00db10
+     by GD and LD, we can delay its update to avoid a race.  */
00db10
   memset (__mempcpy (dest, map->l_tls_initimage, map->l_tls_initimage_size),
00db10
 	  '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
00db10
 }
00db10
diff -urN glibc-2.17-c758a686/nptl_db/db_info.c glibc-2.17-c758a686/nptl_db/db_info.c
00db10
--- glibc-2.17-c758a686/nptl_db/db_info.c	2012-12-24 22:02:13.000000000 -0500
00db10
+++ glibc-2.17-c758a686/nptl_db/db_info.c	2015-03-17 14:39:47.747505754 -0400
00db10
@@ -20,6 +20,7 @@
00db10
 
00db10
 #include "thread_dbP.h"
00db10
 #include <tls.h>
00db10
+#include <ldsodefs.h>
00db10
 
00db10
 typedef struct pthread pthread;
00db10
 typedef struct pthread_key_struct pthread_key_struct;
00db10
@@ -36,6 +37,9 @@
00db10
 } dtv;
00db10
 
00db10
 typedef struct link_map link_map;
00db10
+typedef struct rtld_global rtld_global;
00db10
+typedef struct dtv_slotinfo_list dtv_slotinfo_list;
00db10
+typedef struct dtv_slotinfo dtv_slotinfo;
00db10
 
00db10
 /* Actually static in nptl/init.c, but we only need it for typeof.  */
00db10
 extern bool __nptl_initial_report_events;
00db10
diff -urN glibc-2.17-c758a686/nptl_db/db-symbols.awk glibc-2.17-c758a686/nptl_db/db-symbols.awk
00db10
--- glibc-2.17-c758a686/nptl_db/db-symbols.awk	2012-12-24 22:02:13.000000000 -0500
00db10
+++ glibc-2.17-c758a686/nptl_db/db-symbols.awk	2015-03-17 14:39:47.747505754 -0400
00db10
@@ -2,6 +2,8 @@
00db10
 # we've just built.  It checks for all the symbols used in td_symbol_list.
00db10
 
00db10
 BEGIN {
00db10
+%define DB_RTLD_VARIABLE(name) /* Nothing. */
00db10
+%define DB_MAIN_VARIABLE(name) /* Nothing. */
00db10
 %define DB_LOOKUP_NAME(idx, name)		required[STRINGIFY (name)] = 1;
00db10
 %define DB_LOOKUP_NAME_TH_UNIQUE(idx, name)	th_unique[STRINGIFY (name)] = 1;
00db10
 %include "db-symbols.h"
00db10
diff -urN glibc-2.17-c758a686/nptl_db/fetch-value.c glibc-2.17-c758a686/nptl_db/fetch-value.c
00db10
--- glibc-2.17-c758a686/nptl_db/fetch-value.c	2012-12-24 22:02:13.000000000 -0500
00db10
+++ glibc-2.17-c758a686/nptl_db/fetch-value.c	2015-03-17 14:39:47.747505754 -0400
00db10
@@ -68,7 +68,8 @@
00db10
 	}
00db10
     }
00db10
 
00db10
-  if (idx != 0 && idx - (psaddr_t) 0 > DB_DESC_NELEM (desc))
00db10
+  if (idx != 0 && DB_DESC_NELEM (desc) != 0
00db10
+      && idx - (psaddr_t) 0 > DB_DESC_NELEM (desc))
00db10
     /* This is an internal indicator to callers with nonzero IDX
00db10
        that the IDX value is too big.  */
00db10
     return TD_NOAPLIC;
00db10
diff -urN glibc-2.17-c758a686/nptl_db/structs.def glibc-2.17-c758a686/nptl_db/structs.def
00db10
--- glibc-2.17-c758a686/nptl_db/structs.def	2012-12-24 22:02:13.000000000 -0500
00db10
+++ glibc-2.17-c758a686/nptl_db/structs.def	2015-03-17 14:44:29.779682236 -0400
00db10
@@ -22,6 +22,28 @@
00db10
 # define STRUCTS_DEF_DEFAULTS 1
00db10
 #endif
00db10
 
00db10
+#ifndef DB_RTLD_VARIABLE
00db10
+# define DB_RTLD_VARIABLE(name) DB_VARIABLE (name)
00db10
+#endif
00db10
+
00db10
+#ifndef DB_MAIN_VARIABLE
00db10
+# define DB_MAIN_VARIABLE(name) DB_VARIABLE (name)
00db10
+#endif
00db10
+
00db10
+#ifndef DB_RTLD_GLOBAL_FIELD
00db10
+# if !defined IS_IN_libpthread
00db10
+#  define DB_RTLD_GLOBAL_FIELD(field)		\
00db10
+  DB_STRUCT_FIELD (rtld_global, _##field)	\
00db10
+  DB_MAIN_VARIABLE (_##field)
00db10
+# elif defined SHARED
00db10
+#  define DB_RTLD_GLOBAL_FIELD(field)		\
00db10
+  DB_STRUCT_FIELD (rtld_global, _##field)
00db10
+# else
00db10
+#  define DB_RTLD_GLOBAL_FIELD(field)		\
00db10
+  DB_MAIN_VARIABLE (_##field)
00db10
+# endif
00db10
+#endif /* DB_RTLD_GLOBAL_FIELD */
00db10
+
00db10
 DB_STRUCT (pthread)
00db10
 DB_STRUCT_FIELD (pthread, list)
00db10
 DB_STRUCT_FIELD (pthread, report_events)
00db10
@@ -70,14 +92,31 @@
00db10
 DB_STRUCT_ARRAY_FIELD (pthread_key_data_level2, data)
00db10
 
00db10
 DB_STRUCT_FIELD (link_map, l_tls_modid)
00db10
+DB_STRUCT_FIELD (link_map, l_tls_offset)
00db10
 
00db10
 DB_STRUCT_ARRAY_FIELD (dtv, dtv)
00db10
 #define pointer_val pointer.val /* Field of anonymous struct in dtv_t.  */
00db10
 DB_STRUCT_FIELD (dtv_t, pointer_val)
00db10
+DB_STRUCT_FIELD (dtv_t, counter)
00db10
 #if !defined IS_IN_libpthread || TLS_TCB_AT_TP
00db10
 DB_STRUCT_FIELD (pthread, dtvp)
00db10
 #endif
00db10
 
00db10
+#if !(defined IS_IN_libpthread && !defined SHARED)
00db10
+DB_STRUCT (rtld_global)
00db10
+DB_RTLD_VARIABLE (_rtld_global)
00db10
+#endif
00db10
+DB_RTLD_GLOBAL_FIELD (dl_tls_dtv_slotinfo_list)
00db10
+
00db10
+DB_STRUCT (dtv_slotinfo_list)
00db10
+DB_STRUCT_FIELD (dtv_slotinfo_list, len)
00db10
+DB_STRUCT_FIELD (dtv_slotinfo_list, next)
00db10
+DB_STRUCT_ARRAY_FIELD (dtv_slotinfo_list, slotinfo)
00db10
+
00db10
+DB_STRUCT (dtv_slotinfo)
00db10
+DB_STRUCT_FIELD (dtv_slotinfo, gen)
00db10
+DB_STRUCT_FIELD (dtv_slotinfo, map)
00db10
+
00db10
 #ifdef STRUCTS_DEF_DEFAULTS
00db10
 # undef DB_STRUCT_ARRAY_FIELD
00db10
 # undef DB_ARRAY_VARIABLE
00db10
diff -urN glibc-2.17-c758a686/nptl_db/td_symbol_list.c glibc-2.17-c758a686/nptl_db/td_symbol_list.c
00db10
--- glibc-2.17-c758a686/nptl_db/td_symbol_list.c	2012-12-24 22:02:13.000000000 -0500
00db10
+++ glibc-2.17-c758a686/nptl_db/td_symbol_list.c	2015-03-17 14:39:47.747505754 -0400
00db10
@@ -18,7 +18,6 @@
00db10
    <http://www.gnu.org/licenses/>.  */
00db10
 
00db10
 #include <assert.h>
00db10
-#include <gnu/lib-names.h>
00db10
 #include "thread_dbP.h"
00db10
 
00db10
 static const char *symbol_list_arr[] =
00db10
@@ -41,12 +40,12 @@
00db10
 
00db10
 
00db10
 ps_err_e
00db10
-td_lookup (struct ps_prochandle *ps, int idx, psaddr_t *sym_addr)
00db10
+td_mod_lookup (struct ps_prochandle *ps, const char *mod,
00db10
+	       int idx, psaddr_t *sym_addr)
00db10
 {
00db10
   ps_err_e result;
00db10
   assert (idx >= 0 && idx < SYM_NUM_MESSAGES);
00db10
-  result = ps_pglobal_lookup (ps, LIBPTHREAD_SO, symbol_list_arr[idx],
00db10
-			      sym_addr);
00db10
+  result = ps_pglobal_lookup (ps, mod, symbol_list_arr[idx], sym_addr);
00db10
 
00db10
 #ifdef HAVE_ASM_GLOBAL_DOT_NAME
00db10
   /* For PowerPC, 64-bit uses dot symbols but 32-bit does not.
00db10
diff -urN glibc-2.17-c758a686/nptl_db/td_thr_tlsbase.c glibc-2.17-c758a686/nptl_db/td_thr_tlsbase.c
00db10
--- glibc-2.17-c758a686/nptl_db/td_thr_tlsbase.c	2012-12-24 22:02:13.000000000 -0500
00db10
+++ glibc-2.17-c758a686/nptl_db/td_thr_tlsbase.c	2015-03-17 14:39:47.748505723 -0400
00db10
@@ -17,14 +17,118 @@
00db10
    <http://www.gnu.org/licenses/>.  */
00db10
 
00db10
 #include "thread_dbP.h"
00db10
+#include <link.h>
00db10
 
00db10
+/* Get the DTV slotinfo list head entry from the dynamic loader state
00db10
+   into *LISTHEAD.  */
00db10
+static td_err_e
00db10
+dtv_slotinfo_list (td_thragent_t *ta,
00db10
+		   psaddr_t *listhead)
00db10
+{
00db10
+  td_err_e err;
00db10
+  psaddr_t head;
00db10
+
00db10
+  if (ta->ta_addr__rtld_global == 0
00db10
+      && td_mod_lookup (ta->ph, LD_SO, SYM__rtld_global,
00db10
+			&ta->ta_addr__rtld_global) != PS_OK)
00db10
+    ta->ta_addr__rtld_global = (void*)-1;
00db10
+
00db10
+  if (ta->ta_addr__rtld_global != (void*)-1)
00db10
+    {
00db10
+      err = DB_GET_FIELD (head, ta, ta->ta_addr__rtld_global,
00db10
+			  rtld_global, _dl_tls_dtv_slotinfo_list, 0);
00db10
+      if (err != TD_OK)
00db10
+	return err;
00db10
+    }
00db10
+  else
00db10
+    {
00db10
+      if (ta->ta_addr__dl_tls_dtv_slotinfo_list == 0
00db10
+	  && td_mod_lookup (ta->ph, NULL, SYM__dl_tls_dtv_slotinfo_list,
00db10
+			    &ta->ta_addr__dl_tls_dtv_slotinfo_list) != PS_OK)
00db10
+	return TD_ERR;
00db10
+
00db10
+      err = _td_fetch_value (ta, ta->ta_var__dl_tls_dtv_slotinfo_list,
00db10
+			     SYM_DESC__dl_tls_dtv_slotinfo_list,
00db10
+			     0, ta->ta_addr__dl_tls_dtv_slotinfo_list, &head;;
00db10
+      if (err != TD_OK)
00db10
+	return err;
00db10
+    }
00db10
+
00db10
+  *listhead = head;
00db10
+  return TD_OK;
00db10
+}
00db10
+
00db10
+/* Get the address of the DTV slotinfo entry for MODID into
00db10
+   *DTVSLOTINFO.  */
00db10
+static td_err_e
00db10
+dtv_slotinfo (td_thragent_t *ta,
00db10
+	      unsigned long int modid,
00db10
+	      psaddr_t *dtvslotinfo)
00db10
+{
00db10
+  td_err_e err;
00db10
+  psaddr_t slot, temp;
00db10
+  size_t slbase = 0;
00db10
+
00db10
+  err = dtv_slotinfo_list (ta, &slot);
00db10
+  if (err != TD_OK)
00db10
+    return err;
00db10
+
00db10
+  while (slot)
00db10
+    {
00db10
+      /* Get the number of entries in this list entry's array.  */
00db10
+      err = DB_GET_FIELD (temp, ta, slot, dtv_slotinfo_list, len, 0);
00db10
+      if (err != TD_OK)
00db10
+	return err;
00db10
+      size_t len = (uintptr_t)temp;
00db10
+
00db10
+      /* Did we find the list entry for modid?  */
00db10
+      if (modid < slbase + len)
00db10
+	break;
00db10
+
00db10
+      /* We didn't, so get the next list entry.  */
00db10
+      slbase += len;
00db10
+      err = DB_GET_FIELD (temp, ta, slot, dtv_slotinfo_list,
00db10
+			  next, 0);
00db10
+      if (err != TD_OK)
00db10
+	return err;
00db10
+      slot = temp;
00db10
+    }
00db10
+
00db10
+  /* We reached the end of the list and found nothing.  */
00db10
+  if (!slot)
00db10
+    return TD_ERR;
00db10
+
00db10
+  /* Take the slotinfo for modid from the list entry.  */
00db10
+  err = DB_GET_FIELD_ADDRESS (temp, ta, slot, dtv_slotinfo_list,
00db10
+			      slotinfo, modid - slbase);
00db10
+  if (err != TD_OK)
00db10
+    return err;
00db10
+  slot = temp;
00db10
+
00db10
+  *dtvslotinfo = slot;
00db10
+  return TD_OK;
00db10
+}
00db10
+
00db10
+/* Return in *BASE the base address of the TLS block for MODID within
00db10
+   TH.
00db10
+
00db10
+   It should return success and yield the correct pointer in any
00db10
+   circumstance where the TLS block for the module and thread
00db10
+   requested has already been initialized.
00db10
+
00db10
+   It should fail with TD_TLSDEFER only when the thread could not
00db10
+   possibly have observed any values in that TLS block.  That way, the
00db10
+   debugger can fall back to showing initial values from the PT_TLS
00db10
+   segment (and refusing attempts to mutate) for the TD_TLSDEFER case,
00db10
+   and never fail to make the values the program will actually see
00db10
+   available to the user of the debugger.  */
00db10
 td_err_e
00db10
 td_thr_tlsbase (const td_thrhandle_t *th,
00db10
 		unsigned long int modid,
00db10
 		psaddr_t *base)
00db10
 {
00db10
   td_err_e err;
00db10
-  psaddr_t dtv, dtvslot, dtvptr;
00db10
+  psaddr_t dtv, dtvslot, dtvptr, temp;
00db10
 
00db10
   if (modid < 1)
00db10
     return TD_NOTLS;
00db10
@@ -50,11 +154,75 @@
00db10
 	return TD_TLSDEFER;
00db10
     }
00db10
 
00db10
+  err = dtv_slotinfo (th->th_ta_p, modid, &temp);
00db10
+  if (err != TD_OK)
00db10
+    return err;
00db10
+
00db10
+  psaddr_t slot;
00db10
+  err = DB_GET_STRUCT (slot, th->th_ta_p, temp, dtv_slotinfo);
00db10
+  if (err != TD_OK)
00db10
+    return err;
00db10
+
00db10
+  /* Take the link_map from the slotinfo.  */
00db10
+  psaddr_t map;
00db10
+  err = DB_GET_FIELD_LOCAL (map, th->th_ta_p, slot, dtv_slotinfo, map, 0);
00db10
+  if (err != TD_OK)
00db10
+    return err;
00db10
+  if (!map)
00db10
+    return TD_ERR;
00db10
+
00db10
+  /* Ok, the modid is good, now find out what DTV generation it
00db10
+     requires.  */
00db10
+  err = DB_GET_FIELD_LOCAL (temp, th->th_ta_p, slot, dtv_slotinfo, gen, 0);
00db10
+  if (err != TD_OK)
00db10
+    return err;
00db10
+  size_t modgen = (uintptr_t)temp;
00db10
+
00db10
   /* Get the DTV pointer from the thread descriptor.  */
00db10
   err = DB_GET_FIELD (dtv, th->th_ta_p, pd, pthread, dtvp, 0);
00db10
   if (err != TD_OK)
00db10
     return err;
00db10
 
00db10
+  psaddr_t dtvgenloc;
00db10
+  /* Get the DTV generation count at dtv[0].counter.  */
00db10
+  err = DB_GET_FIELD_ADDRESS (dtvgenloc, th->th_ta_p, dtv, dtv, dtv, 0);
00db10
+  if (err != TD_OK)
00db10
+    return err;
00db10
+  err = DB_GET_FIELD (temp, th->th_ta_p, dtvgenloc, dtv_t, counter, 0);
00db10
+  if (err != TD_OK)
00db10
+    return err;
00db10
+  size_t dtvgen = (uintptr_t)temp;
00db10
+
00db10
+  /* Is the DTV current enough?  */
00db10
+  if (dtvgen < modgen)
00db10
+    {
00db10
+    try_static_tls:
00db10
+      /* If the module uses Static TLS, we're still good.  */
00db10
+      err = DB_GET_FIELD (temp, th->th_ta_p, map, link_map, l_tls_offset, 0);
00db10
+      if (err != TD_OK)
00db10
+	return err;
00db10
+      ptrdiff_t tlsoff = (uintptr_t)temp;
00db10
+
00db10
+      if (tlsoff != FORCED_DYNAMIC_TLS_OFFSET
00db10
+	  && tlsoff != NO_TLS_OFFSET)
00db10
+	{
00db10
+	  psaddr_t tp = pd;
00db10
+
00db10
+#if TLS_TCB_AT_TP
00db10
+	  dtvptr = tp - tlsoff;
00db10
+#elif TLS_DTV_AT_TP
00db10
+	  dtvptr = tp + tlsoff + TLS_PRE_TCB_SIZE;
00db10
+#else
00db10
+# error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
00db10
+#endif
00db10
+
00db10
+	  *base = dtvptr;
00db10
+	  return TD_OK;
00db10
+	}
00db10
+
00db10
+      return TD_TLSDEFER;
00db10
+    }
00db10
+
00db10
   /* Find the corresponding entry in the DTV.  */
00db10
   err = DB_GET_FIELD_ADDRESS (dtvslot, th->th_ta_p, dtv, dtv, dtv, modid);
00db10
   if (err != TD_OK)
00db10
@@ -68,7 +236,7 @@
00db10
   /* It could be that the memory for this module is not allocated for
00db10
      the given thread.  */
00db10
   if ((uintptr_t) dtvptr & 1)
00db10
-    return TD_TLSDEFER;
00db10
+    goto try_static_tls;
00db10
 
00db10
   *base = dtvptr;
00db10
   return TD_OK;
00db10
diff -urN glibc-2.17-c758a686/nptl_db/thread_dbP.h glibc-2.17-c758a686/nptl_db/thread_dbP.h
00db10
--- glibc-2.17-c758a686/nptl_db/thread_dbP.h	2012-12-24 22:02:13.000000000 -0500
00db10
+++ glibc-2.17-c758a686/nptl_db/thread_dbP.h	2015-03-17 14:39:47.748505723 -0400
00db10
@@ -29,6 +29,7 @@
00db10
 #include "thread_db.h"
00db10
 #include "../nptl/pthreadP.h"  	/* This is for *_BITMASK only.  */
00db10
 #include <list.h>
00db10
+#include <gnu/lib-names.h>
00db10
 
00db10
 /* Indeces for the symbol names.  */
00db10
 enum
00db10
@@ -139,11 +140,11 @@
00db10
 }
00db10
 
00db10
 
00db10
-/* Internal wrapper around ps_pglobal_lookup.  */
00db10
-extern ps_err_e td_lookup (struct ps_prochandle *ps,
00db10
-			   int idx, psaddr_t *sym_addr) attribute_hidden;
00db10
-
00db10
-
00db10
+/* Internal wrappers around ps_pglobal_lookup.  */
00db10
+extern ps_err_e td_mod_lookup (struct ps_prochandle *ps, const char *modname,
00db10
+			       int idx, psaddr_t *sym_addr) attribute_hidden;
00db10
+#define td_lookup(ps, idx, sym_addr) \
00db10
+  td_mod_lookup ((ps), LIBPTHREAD_SO, (idx), (sym_addr))
00db10
 
00db10
 
00db10
 /* Store in psaddr_t VAR the address of inferior's symbol NAME.  */