about summary refs log tree commit diff
path: root/elf/dl-deps.c
diff options
context:
space:
mode:
Diffstat (limited to 'elf/dl-deps.c')
-rw-r--r--elf/dl-deps.c65
1 files changed, 48 insertions, 17 deletions
diff --git a/elf/dl-deps.c b/elf/dl-deps.c
index f87475a670..92403d4110 100644
--- a/elf/dl-deps.c
+++ b/elf/dl-deps.c
@@ -25,20 +25,33 @@ Cambridge, MA 02139, USA.  */
 void
 _dl_map_object_deps (struct link_map *map)
 {
-  unsigned int nlist = 1;
-  struct link_map **list = malloc (sizeof *list);
-  unsigned int done;
+  struct list
+    {
+      struct link_map *map;
+      struct list *next;
+    };
+  struct list head, *tailp, *scanp;
+  unsigned int nlist;
 
   /* Start the search list with one element: MAP itself.  */
-  list[0] = map;
+  head.map = map;
+  head.next = NULL;
+  nlist = 1;
+
 
   /* Process each element of the search list, loading each of its immediate
      dependencies and appending them to the list as we step through it.
      This produces a flat, ordered list that represents a breadth-first
      search of the dependency tree.  */
-  for (done = 0; done < nlist; ++done)
+  for (scanp = tailp = &head; scanp; scanp = scanp->next)
     {
-      struct link_map *l = list[done];
+      struct link_map *l = scanp->map;
+
+      /* We use `l_reserved' as a mark bit to detect objects we have
+         already put in the search list and avoid adding duplicate elements
+         later in the list.  */
+      l->l_reserved = 1;
+
       if (l->l_info[DT_NEEDED])
 	{
 	  const char *strtab
@@ -47,24 +60,42 @@ _dl_map_object_deps (struct link_map *map)
 	  for (d = l->l_ld; d->d_tag != DT_NULL; ++d)
 	    if (d->d_tag == DT_NEEDED)
 	      {
-		/* Extend the list and put this object on the end.  */
-		struct link_map **n
-		  = realloc (list, (nlist + 1) * sizeof *list);
-		if (n)
-		  list = n;
+		/* Map in the needed object.  */
+		struct link_map *dep
+		  = _dl_map_object (l, strtab + d->d_un.d_val);
+
+		if (dep->l_reserved)
+		  /* This object is already in the search list we are
+                     building.  Don't add a duplicate pointer.  Release the
+                     reference just added by _dl_map_object.  */
+		  --dep->l_opencount;
 		else
 		  {
-		    free (list);
-		    _dl_signal_error (ENOMEM, map->l_name,
-				      "finding dependencies");
+		    /* Append DEP to the search list.  */
+		    tailp->next = alloca (sizeof *tailp);
+		    tailp = tailp->next;
+		    tailp->map = dep;
+		    tailp->next = NULL;
+		    ++nlist;
 		  }
-		list[nlist++] = _dl_map_object (l, strtab + d->d_un.d_val);
 	      }
 	}
     }
 
-  map->l_searchlist = list;
+  /* Store the search list we built in the object.  It will be used for
+     searches in the scope of this object.  */
+  map->l_searchlist = malloc (nlist * sizeof (struct link_map *));
   map->l_nsearchlist = nlist;
+
+  nlist = 0;
+  for (scanp = &head; scanp; scanp = scanp->next)
+    {
+      map->l_searchlist[nlist++] = scanp->map;
+
+      /* Now clear all the mark bits we set in the objects on the search list
+	 to avoid duplicates, so the next call starts fresh.  */
+      scanp->map->l_reserved = 0;
+    }
 }
 
 
@@ -86,7 +117,7 @@ _dl_open (struct link_map *parent, const char *file, int mode)
       _dl_relocate_object (l, (mode & RTLD_BINDING_MASK) == RTLD_LAZY);
 
   /* Run the initializer functions of new objects.  */
-  while (init = _dl_init_next ())
+  while (init = _dl_init_next (new))
     (*(void (*) (void)) init) ();
 
   return new;