8a8cfb
commit a509eb117fac1d764b15eba64993f4bdb63d7f3c
8a8cfb
Author: Florian Weimer <fweimer@redhat.com>
8a8cfb
Date:   Wed Nov 27 16:37:17 2019 +0100
8a8cfb
8a8cfb
    Avoid late dlopen failure due to scope, TLS slotinfo updates [BZ #25112]
8a8cfb
    
8a8cfb
    This change splits the scope and TLS slotinfo updates in dlopen into
8a8cfb
    two parts: one to resize the data structures, and one to actually apply
8a8cfb
    the update.  The call to add_to_global_resize in dl_open_worker is moved
8a8cfb
    before the demarcation point at which no further memory allocations are
8a8cfb
    allowed.
8a8cfb
    
8a8cfb
    _dl_add_to_slotinfo is adjusted to make the list update optional.  There
8a8cfb
    is some optimization possibility here because we could grow the slotinfo
8a8cfb
    list of arrays in a single call, one the largest TLS modid is known.
8a8cfb
    
8a8cfb
    This commit does not fix the fatal meory allocation failure in
8a8cfb
    _dl_update_slotinfo.  Ideally, this error during dlopen should be
8a8cfb
    recoverable.
8a8cfb
    
8a8cfb
    The update order of scopes and TLS data structures is retained, although
8a8cfb
    it appears to be more correct to fully initialize TLS first, and then
8a8cfb
    expose symbols in the newly loaded objects via the scope update.
8a8cfb
    
8a8cfb
    Tested on x86_64-linux-gnu.
8a8cfb
    
8a8cfb
    Change-Id: I240c58387dabda3ca1bcab48b02115175fa83d6c
8a8cfb
8a8cfb
diff --git a/elf/dl-open.c b/elf/dl-open.c
8a8cfb
index 85db4f0ecb5f29ce..b330cff7d349224a 100644
8a8cfb
--- a/elf/dl-open.c
8a8cfb
+++ b/elf/dl-open.c
8a8cfb
@@ -33,6 +33,7 @@
8a8cfb
 #include <stap-probe.h>
8a8cfb
 #include <atomic.h>
8a8cfb
 #include <libc-internal.h>
8a8cfb
+#include <array_length.h>
8a8cfb
 
8a8cfb
 #include <dl-dst.h>
8a8cfb
 #include <dl-prop.h>
8a8cfb
@@ -214,6 +215,215 @@ _dl_find_dso_for_object (const ElfW(Addr) addr)
8a8cfb
 }
8a8cfb
 rtld_hidden_def (_dl_find_dso_for_object);
8a8cfb
 
8a8cfb
+/* Return true if NEW is found in the scope for MAP.  */
8a8cfb
+static size_t
8a8cfb
+scope_has_map (struct link_map *map, struct link_map *new)
8a8cfb
+{
8a8cfb
+  size_t cnt;
8a8cfb
+  for (cnt = 0; map->l_scope[cnt] != NULL; ++cnt)
8a8cfb
+    if (map->l_scope[cnt] == &new->l_searchlist)
8a8cfb
+      return true;
8a8cfb
+  return false;
8a8cfb
+}
8a8cfb
+
8a8cfb
+/* Return the length of the scope for MAP.  */
8a8cfb
+static size_t
8a8cfb
+scope_size (struct link_map *map)
8a8cfb
+{
8a8cfb
+  size_t cnt;
8a8cfb
+  for (cnt = 0; map->l_scope[cnt] != NULL; )
8a8cfb
+    ++cnt;
8a8cfb
+  return cnt;
8a8cfb
+}
8a8cfb
+
8a8cfb
+/* Resize the scopes of depended-upon objects, so that the new object
8a8cfb
+   can be added later without further allocation of memory.  This
8a8cfb
+   function can raise an exceptions due to malloc failure.  */
8a8cfb
+static void
8a8cfb
+resize_scopes (struct link_map *new)
8a8cfb
+{
8a8cfb
+  /* If the file is not loaded now as a dependency, add the search
8a8cfb
+     list of the newly loaded object to the scope.  */
8a8cfb
+  for (unsigned int i = 0; i < new->l_searchlist.r_nlist; ++i)
8a8cfb
+    {
8a8cfb
+      struct link_map *imap = new->l_searchlist.r_list[i];
8a8cfb
+
8a8cfb
+      /* If the initializer has been called already, the object has
8a8cfb
+	 not been loaded here and now.  */
8a8cfb
+      if (imap->l_init_called && imap->l_type == lt_loaded)
8a8cfb
+	{
8a8cfb
+	  if (scope_has_map (imap, new))
8a8cfb
+	    /* Avoid duplicates.  */
8a8cfb
+	    continue;
8a8cfb
+
8a8cfb
+	  size_t cnt = scope_size (imap);
8a8cfb
+	  if (__glibc_unlikely (cnt + 1 >= imap->l_scope_max))
8a8cfb
+	    {
8a8cfb
+	      /* The l_scope array is too small.  Allocate a new one
8a8cfb
+		 dynamically.  */
8a8cfb
+	      size_t new_size;
8a8cfb
+	      struct r_scope_elem **newp;
8a8cfb
+
8a8cfb
+	      if (imap->l_scope != imap->l_scope_mem
8a8cfb
+		  && imap->l_scope_max < array_length (imap->l_scope_mem))
8a8cfb
+		{
8a8cfb
+		  /* If the current l_scope memory is not pointing to
8a8cfb
+		     the static memory in the structure, but the
8a8cfb
+		     static memory in the structure is large enough to
8a8cfb
+		     use for cnt + 1 scope entries, then switch to
8a8cfb
+		     using the static memory.  */
8a8cfb
+		  new_size = array_length (imap->l_scope_mem);
8a8cfb
+		  newp = imap->l_scope_mem;
8a8cfb
+		}
8a8cfb
+	      else
8a8cfb
+		{
8a8cfb
+		  new_size = imap->l_scope_max * 2;
8a8cfb
+		  newp = (struct r_scope_elem **)
8a8cfb
+		    malloc (new_size * sizeof (struct r_scope_elem *));
8a8cfb
+		  if (newp == NULL)
8a8cfb
+		    _dl_signal_error (ENOMEM, "dlopen", NULL,
8a8cfb
+				      N_("cannot create scope list"));
8a8cfb
+		}
8a8cfb
+
8a8cfb
+	      /* Copy the array and the terminating NULL.  */
8a8cfb
+	      memcpy (newp, imap->l_scope,
8a8cfb
+		      (cnt + 1) * sizeof (imap->l_scope[0]));
8a8cfb
+	      struct r_scope_elem **old = imap->l_scope;
8a8cfb
+
8a8cfb
+	      imap->l_scope = newp;
8a8cfb
+
8a8cfb
+	      if (old != imap->l_scope_mem)
8a8cfb
+		_dl_scope_free (old);
8a8cfb
+
8a8cfb
+	      imap->l_scope_max = new_size;
8a8cfb
+	    }
8a8cfb
+	}
8a8cfb
+    }
8a8cfb
+}
8a8cfb
+
8a8cfb
+/* Second stage of resize_scopes: Add NEW to the scopes.  Also print
8a8cfb
+   debugging information about scopes if requested.
8a8cfb
+
8a8cfb
+   This function cannot raise an exception because all required memory
8a8cfb
+   has been allocated by a previous call to resize_scopes.  */
8a8cfb
+static void
8a8cfb
+update_scopes (struct link_map *new)
8a8cfb
+{
8a8cfb
+  for (unsigned int i = 0; i < new->l_searchlist.r_nlist; ++i)
8a8cfb
+    {
8a8cfb
+      struct link_map *imap = new->l_searchlist.r_list[i];
8a8cfb
+      int from_scope = 0;
8a8cfb
+
8a8cfb
+      if (imap->l_init_called && imap->l_type == lt_loaded)
8a8cfb
+	{
8a8cfb
+	  if (scope_has_map (imap, new))
8a8cfb
+	    /* Avoid duplicates.  */
8a8cfb
+	    continue;
8a8cfb
+
8a8cfb
+	  size_t cnt = scope_size (imap);
8a8cfb
+	  /* Assert that resize_scopes has sufficiently enlarged the
8a8cfb
+	     array.  */
8a8cfb
+	  assert (cnt + 1 < imap->l_scope_max);
8a8cfb
+
8a8cfb
+	  /* First terminate the extended list.  Otherwise a thread
8a8cfb
+	     might use the new last element and then use the garbage
8a8cfb
+	     at offset IDX+1.  */
8a8cfb
+	  imap->l_scope[cnt + 1] = NULL;
8a8cfb
+	  atomic_write_barrier ();
8a8cfb
+	  imap->l_scope[cnt] = &new->l_searchlist;
8a8cfb
+
8a8cfb
+	  from_scope = cnt;
8a8cfb
+	}
8a8cfb
+
8a8cfb
+      /* Print scope information.  */
8a8cfb
+      if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES))
8a8cfb
+	_dl_show_scope (imap, from_scope);
8a8cfb
+    }
8a8cfb
+}
8a8cfb
+
8a8cfb
+/* Call _dl_add_to_slotinfo with DO_ADD set to false, to allocate
8a8cfb
+   space in GL (dl_tls_dtv_slotinfo_list).  This can raise an
8a8cfb
+   exception.  The return value is true if any of the new objects use
8a8cfb
+   TLS.  */
8a8cfb
+static bool
8a8cfb
+resize_tls_slotinfo (struct link_map *new)
8a8cfb
+{
8a8cfb
+  bool any_tls = false;
8a8cfb
+  for (unsigned int i = 0; i < new->l_searchlist.r_nlist; ++i)
8a8cfb
+    {
8a8cfb
+      struct link_map *imap = new->l_searchlist.r_list[i];
8a8cfb
+
8a8cfb
+      /* Only add TLS memory if this object is loaded now and
8a8cfb
+	 therefore is not yet initialized.  */
8a8cfb
+      if (! imap->l_init_called && imap->l_tls_blocksize > 0)
8a8cfb
+	{
8a8cfb
+	  _dl_add_to_slotinfo (imap, false);
8a8cfb
+	  any_tls = true;
8a8cfb
+	}
8a8cfb
+    }
8a8cfb
+  return any_tls;
8a8cfb
+}
8a8cfb
+
8a8cfb
+/* Second stage of TLS update, after resize_tls_slotinfo.  This
8a8cfb
+   function does not raise any exception.  It should only be called if
8a8cfb
+   resize_tls_slotinfo returned true.  */
8a8cfb
+static void
8a8cfb
+update_tls_slotinfo (struct link_map *new)
8a8cfb
+{
8a8cfb
+  unsigned int first_static_tls = new->l_searchlist.r_nlist;
8a8cfb
+  for (unsigned int i = 0; i < new->l_searchlist.r_nlist; ++i)
8a8cfb
+    {
8a8cfb
+      struct link_map *imap = new->l_searchlist.r_list[i];
8a8cfb
+
8a8cfb
+      /* Only add TLS memory if this object is loaded now and
8a8cfb
+	 therefore is not yet initialized.  */
8a8cfb
+      if (! imap->l_init_called && imap->l_tls_blocksize > 0)
8a8cfb
+	{
8a8cfb
+	  _dl_add_to_slotinfo (imap, true);
8a8cfb
+
8a8cfb
+	  if (imap->l_need_tls_init
8a8cfb
+	      && first_static_tls == new->l_searchlist.r_nlist)
8a8cfb
+	    first_static_tls = i;
8a8cfb
+	}
8a8cfb
+    }
8a8cfb
+
8a8cfb
+  if (__builtin_expect (++GL(dl_tls_generation) == 0, 0))
8a8cfb
+    _dl_fatal_printf (N_("\
8a8cfb
+TLS generation counter wrapped!  Please report this."));
8a8cfb
+
8a8cfb
+  /* We need a second pass for static tls data, because
8a8cfb
+     _dl_update_slotinfo must not be run while calls to
8a8cfb
+     _dl_add_to_slotinfo are still pending.  */
8a8cfb
+  for (unsigned int i = first_static_tls; i < new->l_searchlist.r_nlist; ++i)
8a8cfb
+    {
8a8cfb
+      struct link_map *imap = new->l_searchlist.r_list[i];
8a8cfb
+
8a8cfb
+      if (imap->l_need_tls_init
8a8cfb
+	  && ! imap->l_init_called
8a8cfb
+	  && imap->l_tls_blocksize > 0)
8a8cfb
+	{
8a8cfb
+	  /* For static TLS we have to allocate the memory here and
8a8cfb
+	     now, but we can delay updating the DTV.  */
8a8cfb
+	  imap->l_need_tls_init = 0;
8a8cfb
+#ifdef SHARED
8a8cfb
+	  /* Update the slot information data for at least the
8a8cfb
+	     generation of the DSO we are allocating data for.  */
8a8cfb
+
8a8cfb
+	  /* FIXME: This can terminate the process on memory
8a8cfb
+	     allocation failure.  It is not possible to raise
8a8cfb
+	     exceptions from this context; to fix this bug,
8a8cfb
+	     _dl_update_slotinfo would have to be split into two
8a8cfb
+	     operations, similar to resize_scopes and update_scopes
8a8cfb
+	     above.  This is related to bug 16134.  */
8a8cfb
+	  _dl_update_slotinfo (imap->l_tls_modid);
8a8cfb
+#endif
8a8cfb
+
8a8cfb
+	  GL(dl_init_static_tls) (imap);
8a8cfb
+	  assert (imap->l_need_tls_init == 0);
8a8cfb
+	}
8a8cfb
+    }
8a8cfb
+}
8a8cfb
+
8a8cfb
 /* struct dl_init_args and call_dl_init are used to call _dl_init with
8a8cfb
    exception handling disabled.  */
8a8cfb
 struct dl_init_args
8a8cfb
@@ -431,133 +641,40 @@ dl_open_worker (void *a)
8a8cfb
      relocation.  */
8a8cfb
   _dl_open_check (new);
8a8cfb
 
8a8cfb
-  /* If the file is not loaded now as a dependency, add the search
8a8cfb
-     list of the newly loaded object to the scope.  */
8a8cfb
-  bool any_tls = false;
8a8cfb
-  unsigned int first_static_tls = new->l_searchlist.r_nlist;
8a8cfb
-  for (unsigned int i = 0; i < new->l_searchlist.r_nlist; ++i)
8a8cfb
-    {
8a8cfb
-      struct link_map *imap = new->l_searchlist.r_list[i];
8a8cfb
-      int from_scope = 0;
8a8cfb
+  /* This only performs the memory allocations.  The actual update of
8a8cfb
+     the scopes happens below, after failure is impossible.  */
8a8cfb
+  resize_scopes (new);
8a8cfb
 
8a8cfb
-      /* If the initializer has been called already, the object has
8a8cfb
-	 not been loaded here and now.  */
8a8cfb
-      if (imap->l_init_called && imap->l_type == lt_loaded)
8a8cfb
-	{
8a8cfb
-	  struct r_scope_elem **runp = imap->l_scope;
8a8cfb
-	  size_t cnt = 0;
8a8cfb
-
8a8cfb
-	  while (*runp != NULL)
8a8cfb
-	    {
8a8cfb
-	      if (*runp == &new->l_searchlist)
8a8cfb
-		break;
8a8cfb
-	      ++cnt;
8a8cfb
-	      ++runp;
8a8cfb
-	    }
8a8cfb
-
8a8cfb
-	  if (*runp != NULL)
8a8cfb
-	    /* Avoid duplicates.  */
8a8cfb
-	    continue;
8a8cfb
-
8a8cfb
-	  if (__glibc_unlikely (cnt + 1 >= imap->l_scope_max))
8a8cfb
-	    {
8a8cfb
-	      /* The 'r_scope' array is too small.  Allocate a new one
8a8cfb
-		 dynamically.  */
8a8cfb
-	      size_t new_size;
8a8cfb
-	      struct r_scope_elem **newp;
8a8cfb
-
8a8cfb
-#define SCOPE_ELEMS(imap) \
8a8cfb
-  (sizeof (imap->l_scope_mem) / sizeof (imap->l_scope_mem[0]))
8a8cfb
+  /* Increase the size of the GL (dl_tls_dtv_slotinfo_list) data
8a8cfb
+     structure.  */
8a8cfb
+  bool any_tls = resize_tls_slotinfo (new);
8a8cfb
 
8a8cfb
-	      if (imap->l_scope != imap->l_scope_mem
8a8cfb
-		  && imap->l_scope_max < SCOPE_ELEMS (imap))
8a8cfb
-		{
8a8cfb
-		  new_size = SCOPE_ELEMS (imap);
8a8cfb
-		  newp = imap->l_scope_mem;
8a8cfb
-		}
8a8cfb
-	      else
8a8cfb
-		{
8a8cfb
-		  new_size = imap->l_scope_max * 2;
8a8cfb
-		  newp = (struct r_scope_elem **)
8a8cfb
-		    malloc (new_size * sizeof (struct r_scope_elem *));
8a8cfb
-		  if (newp == NULL)
8a8cfb
-		    _dl_signal_error (ENOMEM, "dlopen", NULL,
8a8cfb
-				      N_("cannot create scope list"));
8a8cfb
-		}
8a8cfb
-
8a8cfb
-	      memcpy (newp, imap->l_scope, cnt * sizeof (imap->l_scope[0]));
8a8cfb
-	      struct r_scope_elem **old = imap->l_scope;
8a8cfb
-
8a8cfb
-	      imap->l_scope = newp;
8a8cfb
-
8a8cfb
-	      if (old != imap->l_scope_mem)
8a8cfb
-		_dl_scope_free (old);
8a8cfb
-
8a8cfb
-	      imap->l_scope_max = new_size;
8a8cfb
-	    }
8a8cfb
-
8a8cfb
-	  /* First terminate the extended list.  Otherwise a thread
8a8cfb
-	     might use the new last element and then use the garbage
8a8cfb
-	     at offset IDX+1.  */
8a8cfb
-	  imap->l_scope[cnt + 1] = NULL;
8a8cfb
-	  atomic_write_barrier ();
8a8cfb
-	  imap->l_scope[cnt] = &new->l_searchlist;
8a8cfb
-
8a8cfb
-	  /* Print only new scope information.  */
8a8cfb
-	  from_scope = cnt;
8a8cfb
-	}
8a8cfb
-      /* Only add TLS memory if this object is loaded now and
8a8cfb
-	 therefore is not yet initialized.  */
8a8cfb
-      else if (! imap->l_init_called
8a8cfb
-	       /* Only if the module defines thread local data.  */
8a8cfb
-	       && __builtin_expect (imap->l_tls_blocksize > 0, 0))
8a8cfb
-	{
8a8cfb
-	  /* Now that we know the object is loaded successfully add
8a8cfb
-	     modules containing TLS data to the slot info table.  We
8a8cfb
-	     might have to increase its size.  */
8a8cfb
-	  _dl_add_to_slotinfo (imap);
8a8cfb
-
8a8cfb
-	  if (imap->l_need_tls_init
8a8cfb
-	      && first_static_tls == new->l_searchlist.r_nlist)
8a8cfb
-	    first_static_tls = i;
8a8cfb
-
8a8cfb
-	  /* We have to bump the generation counter.  */
8a8cfb
-	  any_tls = true;
8a8cfb
-	}
8a8cfb
-
8a8cfb
-      /* Print scope information.  */
8a8cfb
-      if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES))
8a8cfb
-	_dl_show_scope (imap, from_scope);
8a8cfb
-    }
8a8cfb
-
8a8cfb
-  /* Bump the generation number if necessary.  */
8a8cfb
-  if (any_tls && __builtin_expect (++GL(dl_tls_generation) == 0, 0))
8a8cfb
-    _dl_fatal_printf (N_("\
8a8cfb
-TLS generation counter wrapped!  Please report this."));
8a8cfb
-
8a8cfb
-  /* We need a second pass for static tls data, because _dl_update_slotinfo
8a8cfb
-     must not be run while calls to _dl_add_to_slotinfo are still pending.  */
8a8cfb
-  for (unsigned int i = first_static_tls; i < new->l_searchlist.r_nlist; ++i)
8a8cfb
-    {
8a8cfb
-      struct link_map *imap = new->l_searchlist.r_list[i];
8a8cfb
-
8a8cfb
-      if (imap->l_need_tls_init
8a8cfb
-	  && ! imap->l_init_called
8a8cfb
-	  && imap->l_tls_blocksize > 0)
8a8cfb
-	{
8a8cfb
-	  /* For static TLS we have to allocate the memory here and
8a8cfb
-	     now, but we can delay updating the DTV.  */
8a8cfb
-	  imap->l_need_tls_init = 0;
8a8cfb
-#ifdef SHARED
8a8cfb
-	  /* Update the slot information data for at least the
8a8cfb
-	     generation of the DSO we are allocating data for.  */
8a8cfb
-	  _dl_update_slotinfo (imap->l_tls_modid);
8a8cfb
-#endif
8a8cfb
+  /* Perform the necessary allocations for adding new global objects
8a8cfb
+     to the global scope below.  */
8a8cfb
+  if (mode & RTLD_GLOBAL)
8a8cfb
+    add_to_global_resize (new);
8a8cfb
 
8a8cfb
-	  GL(dl_init_static_tls) (imap);
8a8cfb
-	  assert (imap->l_need_tls_init == 0);
8a8cfb
-	}
8a8cfb
-    }
8a8cfb
+  /* Demarcation point: After this, no recoverable errors are allowed.
8a8cfb
+     All memory allocations for new objects must have happened
8a8cfb
+     before.  */
8a8cfb
+
8a8cfb
+  /* Second stage after resize_scopes: Actually perform the scope
8a8cfb
+     update.  After this, dlsym and lazy binding can bind to new
8a8cfb
+     objects.  */
8a8cfb
+  update_scopes (new);
8a8cfb
+
8a8cfb
+  /* FIXME: It is unclear whether the order here is correct.
8a8cfb
+     Shouldn't new objects be made available for binding (and thus
8a8cfb
+     execution) only after there TLS data has been set up fully?
8a8cfb
+     Fixing bug 16134 will likely make this distinction less
8a8cfb
+     important.  */
8a8cfb
+
8a8cfb
+  /* Second stage after resize_tls_slotinfo: Update the slotinfo data
8a8cfb
+     structures.  */
8a8cfb
+  if (any_tls)
8a8cfb
+    /* FIXME: This calls _dl_update_slotinfo, which aborts the process
8a8cfb
+       on memory allocation failure.  See bug 16134.  */
8a8cfb
+    update_tls_slotinfo (new);
8a8cfb
 
8a8cfb
   /* Notify the debugger all new objects have been relocated.  */
8a8cfb
   if (relocation_in_progress)
8a8cfb
diff --git a/elf/dl-tls.c b/elf/dl-tls.c
8a8cfb
index c87caf13d6a97ba4..a2def280b7096960 100644
8a8cfb
--- a/elf/dl-tls.c
8a8cfb
+++ b/elf/dl-tls.c
8a8cfb
@@ -883,7 +883,7 @@ _dl_tls_get_addr_soft (struct link_map *l)
8a8cfb
 
8a8cfb
 
8a8cfb
 void
8a8cfb
-_dl_add_to_slotinfo (struct link_map *l)
8a8cfb
+_dl_add_to_slotinfo (struct link_map *l, bool do_add)
8a8cfb
 {
8a8cfb
   /* Now that we know the object is loaded successfully add
8a8cfb
      modules containing TLS data to the dtv info table.  We
8a8cfb
@@ -939,6 +939,9 @@ cannot create TLS data structures"));
8a8cfb
     }
8a8cfb
 
8a8cfb
   /* Add the information into the slotinfo data structure.  */
8a8cfb
-  listp->slotinfo[idx].map = l;
8a8cfb
-  listp->slotinfo[idx].gen = GL(dl_tls_generation) + 1;
8a8cfb
+  if (do_add)
8a8cfb
+    {
8a8cfb
+      listp->slotinfo[idx].map = l;
8a8cfb
+      listp->slotinfo[idx].gen = GL(dl_tls_generation) + 1;
8a8cfb
+    }
8a8cfb
 }
8a8cfb
diff --git a/elf/rtld.c b/elf/rtld.c
8a8cfb
index 4ec26a79cbb0aa4f..0aa1a2a19f649e16 100644
8a8cfb
--- a/elf/rtld.c
8a8cfb
+++ b/elf/rtld.c
8a8cfb
@@ -2167,7 +2167,7 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
8a8cfb
 
8a8cfb
 	  /* Add object to slot information data if necessasy.  */
8a8cfb
 	  if (l->l_tls_blocksize != 0 && tls_init_tp_called)
8a8cfb
-	    _dl_add_to_slotinfo (l);
8a8cfb
+	    _dl_add_to_slotinfo (l, true);
8a8cfb
 	}
8a8cfb
     }
8a8cfb
   else
8a8cfb
@@ -2215,7 +2215,7 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
8a8cfb
 
8a8cfb
 	  /* Add object to slot information data if necessasy.  */
8a8cfb
 	  if (l->l_tls_blocksize != 0 && tls_init_tp_called)
8a8cfb
-	    _dl_add_to_slotinfo (l);
8a8cfb
+	    _dl_add_to_slotinfo (l, true);
8a8cfb
 	}
8a8cfb
       HP_TIMING_NOW (stop);
8a8cfb
 
8a8cfb
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
8a8cfb
index 57fbefea3cb841e9..c6b7e61badbfd513 100644
8a8cfb
--- a/sysdeps/generic/ldsodefs.h
8a8cfb
+++ b/sysdeps/generic/ldsodefs.h
8a8cfb
@@ -1135,8 +1135,15 @@ extern void *_dl_open (const char *name, int mode, const void *caller,
8a8cfb
    old scope, OLD can't be freed until no thread is using it.  */
8a8cfb
 extern int _dl_scope_free (void *) attribute_hidden;
8a8cfb
 
8a8cfb
-/* Add module to slot information data.  */
8a8cfb
-extern void _dl_add_to_slotinfo (struct link_map  *l) attribute_hidden;
8a8cfb
+
8a8cfb
+/* Add module to slot information data.  If DO_ADD is false, only the
8a8cfb
+   required memory is allocated.  Must be called with GL
8a8cfb
+   (dl_load_lock) acquired.  If the function has already been called
8a8cfb
+   for the link map L with !do_add, then this function will not raise
8a8cfb
+   an exception, otherwise it is possible that it encounters a memory
8a8cfb
+   allocation failure.  */
8a8cfb
+extern void _dl_add_to_slotinfo (struct link_map *l, bool do_add)
8a8cfb
+  attribute_hidden;
8a8cfb
 
8a8cfb
 /* Update slot information data for at least the generation of the
8a8cfb
    module with the given index.  */