2010-02-02  Andreas Schwab  <schwab@redhat.com>

	* sysdeps/generic/ldsodefs.h (struct rtld_global): Add
	_dl_load_write_lock.
	* elf/rtld.c (_rtld_global): Initialize it.
	* elf/dl-support.c (_dl_load_write_lock): Define .
	* elf/dl-close.c (_dl_close_worker): Lock GL(dl_load_write_lock)
	when modifying the list of loaded objects.
	* elf/dl-object.c (_dl_new_object): Likewise.
	* elf/dl-iteratephdr.c (__dl_iterate_phdr): Lock
	GL(dl_load_write_lock) instead of GL(dl_load_lock).

Index: glibc-2.11.3/elf/dl-close.c
===================================================================
--- glibc-2.11.3.orig/elf/dl-close.c
+++ glibc-2.11.3/elf/dl-close.c
@@ -529,6 +529,9 @@ _dl_close_worker (struct link_map *map)
   size_t tls_free_end;
   tls_free_start = tls_free_end = NO_TLS_OFFSET;
 
+  /* We modify the list of loaded objects.  */
+  __rtld_lock_lock_recursive (GL(dl_load_write_lock));
+
   /* Check each element of the search list to see if all references to
      it are gone.  */
   for (unsigned int i = first_loaded; i < nloaded; ++i)
@@ -687,6 +690,8 @@ _dl_close_worker (struct link_map *map)
 	}
     }
 
+  __rtld_lock_unlock_recursive (GL(dl_load_write_lock));
+
   /* If we removed any object which uses TLS bump the generation counter.  */
   if (any_tls)
     {
Index: glibc-2.11.3/elf/dl-iteratephdr.c
===================================================================
--- glibc-2.11.3.orig/elf/dl-iteratephdr.c
+++ glibc-2.11.3/elf/dl-iteratephdr.c
@@ -26,7 +26,7 @@
 static void
 cancel_handler (void *arg __attribute__((unused)))
 {
-  __rtld_lock_unlock_recursive (GL(dl_load_lock));
+  __rtld_lock_unlock_recursive (GL(dl_load_write_lock));
 }
 
 hidden_proto (__dl_iterate_phdr)
@@ -38,8 +38,8 @@ __dl_iterate_phdr (int (*callback) (stru
   struct dl_phdr_info info;
   int ret = 0;
 
-  /* Make sure we are alone.  */
-  __rtld_lock_lock_recursive (GL(dl_load_lock));
+  /* Make sure nobody modifies the list of loaded objects.  */
+  __rtld_lock_lock_recursive (GL(dl_load_write_lock));
   __libc_cleanup_push (cancel_handler, 0);
 
   /* We have to determine the namespace of the caller since this determines
@@ -80,7 +80,7 @@ __dl_iterate_phdr (int (*callback) (stru
 
   /* Release the lock.  */
   __libc_cleanup_pop (0);
-  __rtld_lock_unlock_recursive (GL(dl_load_lock));
+  __rtld_lock_unlock_recursive (GL(dl_load_write_lock));
 
   return ret;
 }
Index: glibc-2.11.3/elf/dl-object.c
===================================================================
--- glibc-2.11.3.orig/elf/dl-object.c
+++ glibc-2.11.3/elf/dl-object.c
@@ -113,6 +113,9 @@ _dl_new_object (char *realname, const ch
   new->l_scope = new->l_scope_mem;
   new->l_scope_max = sizeof (new->l_scope_mem) / sizeof (new->l_scope_mem[0]);
 
+  /* We modify the list of loaded objects.  */
+  __rtld_lock_lock_recursive (GL(dl_load_write_lock));
+
   /* Counter for the scopes we have to handle.  */
   int idx = 0;
 
@@ -120,6 +123,8 @@ _dl_new_object (char *realname, const ch
     /* Add the global scope.  */
     new->l_scope[idx++] = &GL(dl_ns)[nsid]._ns_loaded->l_searchlist;
 
+  __rtld_lock_unlock_recursive (GL(dl_load_write_lock));
+
   /* If we have no loader the new object acts as it.  */
   if (loader == NULL)
     loader = new;
Index: glibc-2.11.3/elf/dl-support.c
===================================================================
--- glibc-2.11.3.orig/elf/dl-support.c
+++ glibc-2.11.3/elf/dl-support.c
@@ -161,6 +161,10 @@ const ElfW(Ehdr) *_dl_sysinfo_dso;
    the loaded object might as well require a call to this function.
    At this time it is not anymore a problem to modify the tables.  */
 __rtld_lock_define_initialized_recursive (, _dl_load_lock)
+/* This lock is used to keep __dl_iterate_phdr from inspecting the
+   list of loaded objects while an object is added to or removed from
+   that list.  */
+__rtld_lock_define_initialized_recursive (, _dl_load_write_lock)
 
 
 #ifdef HAVE_AUX_VECTOR
Index: glibc-2.11.3/elf/rtld.c
===================================================================
--- glibc-2.11.3.orig/elf/rtld.c
+++ glibc-2.11.3/elf/rtld.c
@@ -128,6 +128,7 @@ struct rtld_global _rtld_global =
     ._dl_stack_flags = DEFAULT_STACK_PERMS,
 #ifdef _LIBC_REENTRANT
     ._dl_load_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER,
+    ._dl_load_write_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER,
 #endif
     ._dl_nns = 1,
     ._dl_ns =
Index: glibc-2.11.3/nptl/sysdeps/unix/sysv/linux/fork.c
===================================================================
--- glibc-2.11.3.orig/nptl/sysdeps/unix/sysv/linux/fork.c
+++ glibc-2.11.3/nptl/sysdeps/unix/sysv/linux/fork.c
@@ -180,8 +180,9 @@ __libc_fork (void)
       /* Reset locks in the I/O code.  */
       _IO_list_resetlock ();
 
-      /* Reset the lock the dynamic loader uses to protect its data.  */
+      /* Reset the locks the dynamic loader uses to protect its data.  */
       __rtld_lock_initialize (GL(dl_load_lock));
+      __rtld_lock_initialize (GL(dl_load_write_lock));
 
       /* Run the handlers registered for the child.  */
       while (allp != NULL)
Index: glibc-2.11.3/sysdeps/generic/ldsodefs.h
===================================================================
--- glibc-2.11.3.orig/sysdeps/generic/ldsodefs.h
+++ glibc-2.11.3/sysdeps/generic/ldsodefs.h
@@ -416,6 +416,10 @@ struct rtld_global
      the loaded object might as well require a call to this function.
      At this time it is not anymore a problem to modify the tables.  */
   __rtld_lock_define_recursive (EXTERN, _dl_load_lock)
+  /* This lock is used to keep __dl_iterate_phdr from inspecting the
+     list of loaded objects while an object is added to or removed
+     from that list.  */
+  __rtld_lock_define_recursive (EXTERN, _dl_load_write_lock)
 
   /* Incremented whenever something may have been added to dl_loaded.  */
   EXTERN unsigned long long _dl_load_adds;
