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