summary refs log tree commit diff
path: root/elf/dl-deps.c
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2000-03-30 16:30:49 +0000
committerUlrich Drepper <drepper@redhat.com>2000-03-30 16:30:49 +0000
commitdacc8ffa420575038f04c543c31065fea81d5732 (patch)
treee48ee55d563a39779776f3c7e02d76bc05a4cbb4 /elf/dl-deps.c
parent38e986ecd8aba6953cfa4e7f104c671100f00999 (diff)
downloadglibc-dacc8ffa420575038f04c543c31065fea81d5732.tar.gz
glibc-dacc8ffa420575038f04c543c31065fea81d5732.tar.xz
glibc-dacc8ffa420575038f04c543c31065fea81d5732.zip
Update.
2000-03-30  Ulrich Drepper  <drepper@redhat.com>

	Implement dynamic determination of constructor/destructor order in
	the dynamic linker.
	* elf/Versions [ld.so] (GLIBC_2.0): Remove _dl_init_next.
	(GLIBC_2.2): Add _dl_init.
	* elf/dl-close.c: Also call all destructors in FINI_ARRAY.
	r_duplist is not anymore allocated separately.  l_initfini is and
	therefore free it if necessary.
	* elf/dl-deps.c: If a searchlist has to be allocated, put all in one
	malloc block.  Otherwise allocate l_initfini list only.
	Put dependencies for the object in l_initfini list.
	Sort dependencies for the object to be loaded topologically.
	* elf/dl-fini.c: Before running the destructors sort the topologically.
	* elf/dl-init.c (_dl_init): Renamed from _dl_init_next.  Rewrite to
	call constructors instead of iterating over the pointers.  Get list of
	objects for which to run constructors from l_initfini element. Accept
	argc, argv, and env as parameters and pass them to the constructors.
	* elf/ld-load.c (_dl_map_object_from_fd): Initialize l_ldnum member
	with size of dynamic section.
	* elf/dl-open.c (dl_open_worker): Only call _dl_init instead of
	_dl_init_next and calling constructors ourself.
	* elf/dl-preinit.c (_dl_preinit): Renamed from _dl_preinit_next.
	Take argc, argv, and env as parameters and pass them to the
	constructors.  Rewrite to call all constructors and not iterate over
	the pointers.
	* elf/dynamic-link.h: Don't relocate DT_FINI_ARRAY entry.  Don't
	precompute l_initcount and l_preinitcount.
	* elf/link.h (struct link_map): Add l_ldnum member.
	Make l_phdr_allocated part of the bitfield.  Remove l_runcount,
	l_initcount, and l_preinitcount.  Add l_initfini.
	* sysdeps/generic/ldsodefs.h: Replace _dl_init_next prototype with
	one for _dl_init.
	* sysdeps/i386/dl-machine (RTLD_START): Rewrite to match new init
	function interface.
	* sysdeps/unix/sysv/linux/init-first.h: Removed.
	* sysdeps/unix/sysv/linux/Dist: Delete file here as well.
	* sysdeps/unix/sysv/linux/init-first.c [PIC]: Don't use
	SYSDEP_CALL_INIT.  Make _init a strong alias of init.  The calling
	conventions now match.

	* sysdeps/generic/libc-start.c: Calling __libc_init_first has no
	effect for shared objects.  Don't emit message and call only for
	static library.
Diffstat (limited to 'elf/dl-deps.c')
-rw-r--r--elf/dl-deps.c95
1 files changed, 83 insertions, 12 deletions
diff --git a/elf/dl-deps.c b/elf/dl-deps.c
index 26dd0f80a9..dc372e6cd9 100644
--- a/elf/dl-deps.c
+++ b/elf/dl-deps.c
@@ -192,6 +192,17 @@ _dl_map_object_deps (struct link_map *map,
   for (runp = known; runp; )
     {
       struct link_map *l = runp->map;
+      struct link_map **needed = NULL;
+      unsigned int nneeded = 0;
+
+      /* Unless otherwise stated, this object is handled.  */
+      runp->done = 1;
+
+      /* Allocate a temporary record to contain the references to the
+	 dependencies of this object.  */
+      if (l->l_searchlist.r_list == NULL && l != map && l->l_ldnum > 0)
+	needed = (struct link_map **) alloca (l->l_ldnum
+					      * sizeof (struct link_map *));
 
       if (l->l_info[DT_NEEDED] || l->l_info[AUXTAG] || l->l_info[FILTERTAG])
 	{
@@ -200,9 +211,6 @@ _dl_map_object_deps (struct link_map *map,
 	  struct list *orig;
 	  const ElfW(Dyn) *d;
 
-	  /* Mark map as processed.  */
-	  runp->done = 1;
-
 	  args.strtab = strtab;
 	  args.map = l;
 	  args.trace_mode = trace_mode;
@@ -250,6 +258,10 @@ _dl_map_object_deps (struct link_map *map,
 		    /* Set the mark bit that says it's already in the list.  */
 		    dep->l_reserved = 1;
 		  }
+
+		/* Remember this dependency.  */
+		if (needed != NULL)
+		  needed[nneeded++] = dep;
 	      }
 	    else if (d->d_tag == DT_AUXILIARY || d->d_tag == DT_FILTER)
 	      {
@@ -320,6 +332,10 @@ _dl_map_object_deps (struct link_map *map,
 		orig->done = 0;
 		orig->map = args.aux;
 
+		/* Remember this dependency.  */
+		if (needed != NULL)
+		  needed[nneeded++] = args.aux;
+
 		/* We must handle two situations here: the map is new,
 		   so we must add it in all three lists.  If the map
 		   is already known, we have two further possibilities:
@@ -427,9 +443,19 @@ _dl_map_object_deps (struct link_map *map,
 		++nduplist;
 	      }
 	}
-      else
-	/* Mark as processed.  */
-	runp->done = 1;
+
+      /* Terminate the list of dependencies and store the array address.  */
+      if (needed != NULL)
+	{
+	  needed[nneeded++] = NULL;
+
+	  l->l_initfini =
+	    (struct link_map **) malloc (nneeded * sizeof (struct link_map));
+	  if (l->l_initfini == NULL)
+	    _dl_signal_error (ENOMEM, map->l_name,
+			      "cannot allocate dependency list");
+	  memcpy (l->l_initfini, needed, nneeded * sizeof (struct link_map));
+	}
 
       /* If we have no auxiliary objects just go on to the next map.  */
       if (runp->done)
@@ -440,7 +466,9 @@ _dl_map_object_deps (struct link_map *map,
 
   /* Store the search list we built in the object.  It will be used for
      searches in the scope of this object.  */
-  map->l_searchlist.r_list = malloc (nlist * sizeof (struct link_map *));
+  map->l_searchlist.r_list = malloc ((2 * nlist
+				      + (nlist == nduplist ? 0 : nduplist))
+				     * sizeof (struct link_map *));
   if (map->l_searchlist.r_list == NULL)
     _dl_signal_error (ENOMEM, map->l_name,
 		      "cannot allocate symbol search list");
@@ -466,11 +494,7 @@ _dl_map_object_deps (struct link_map *map,
     {
       unsigned int cnt;
 
-      map->l_searchlist.r_duplist = malloc (nduplist
-					    * sizeof (struct link_map *));
-      if (map->l_searchlist.r_duplist == NULL)
-	_dl_signal_error (ENOMEM, map->l_name,
-			  "cannot allocate symbol search list");
+      map->l_searchlist.r_duplist = map->l_searchlist.r_list + nlist;
 
       for (cnt = 0, runp = known; runp; runp = runp->dup)
 	if (trace_mode && runp->map->l_opencount == 0)
@@ -479,4 +503,51 @@ _dl_map_object_deps (struct link_map *map,
 	else
 	  map->l_searchlist.r_duplist[cnt++] = runp->map;
     }
+
+  /* Now determine the order in which the initialization has to happen.  */
+  map->l_initfini =
+    (struct link_map **) memcpy (map->l_searchlist.r_duplist + nduplist,
+				 map->l_searchlist.r_list,
+				 nlist * sizeof (struct link_map *));
+  /* We can skip looking for the binary itself which is at the front
+     of the search list.  Look through the list backward so that circular
+     dependencies are not changing the order.  */
+  for (i = 1; i < nlist; ++i)
+    {
+      struct link_map *l = map->l_searchlist.r_list[i];
+      unsigned int j;
+      unsigned int k;
+
+      /* Find the place in the initfini list where the map is currently
+	 located.  */
+      for (j = 1; map->l_initfini[j] != l; ++j)
+	;
+
+      /* Find all object for which the current one is a dependency and
+	 move the found object (if necessary) in front.  */
+      for (k = j + 1; k < nlist; ++k)
+	{
+	  struct link_map **runp;
+
+	  runp = map->l_initfini[k]->l_initfini;
+	  if (runp != NULL)
+	    {
+	      while (*runp != NULL)
+		if (*runp == l)
+		  {
+		    struct link_map *here = map->l_initfini[k];
+
+		    /* Move it now.  */
+		    memmove (&map->l_initfini[j] + 1,
+			     &map->l_initfini[j],
+			     (k - j) * sizeof (struct link_map *));
+		    map->l_initfini[j] = here;
+
+		    break;
+		  }
+		else
+		  ++runp;
+	    }
+	}
+    }
 }