[patch] Reduce ridiculous stack use in _dl_map_object_deps

Paul Pluzhnikov ppluzhnikov@google.com
Mon Oct 31 18:04:00 GMT 2011


On Sun, Oct 30, 2011 at 6:35 AM, Marek Polacek <mpolacek@redhat.com> wrote:

> Couldn't we perhaps use extend_alloca here?  But I didn't really try.

Thanks for your comments. I updated the patch, and discovered that
extend_alloca is currently broken on Linux/x86 due to
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50938


-- 
Paul Pluzhnikov


2011-10-31  Paul Pluzhnikov  <ppluzhnikov@google.com>

	* elf/dl-deps.c (_dl_map_object_deps): Reuse alloca space to reduce
	stack usage.
-------------- next part --------------
diff --git a/elf/dl-deps.c b/elf/dl-deps.c
index 95b1088..257fa41 100644
--- a/elf/dl-deps.c
+++ b/elf/dl-deps.c
@@ -156,6 +156,8 @@ _dl_map_object_deps (struct link_map *map,
   int errno_reason;
   const char *errstring;
   const char *objname;
+  struct link_map **needed_space;
+  unsigned int needed_space_bytes;
 
   auto inline void preload (struct link_map *map);
 
@@ -175,6 +177,10 @@ _dl_map_object_deps (struct link_map *map,
   /* No loaded object so far.  */
   nlist = 0;
 
+  /* No alloca'd space yet.  */
+  needed_space = NULL;
+  needed_space_bytes = 0;
+
   /* First load MAP itself.  */
   preload (map);
 
@@ -216,8 +222,23 @@ _dl_map_object_deps (struct link_map *map,
 	 dependencies of this object.  */
       if (l->l_searchlist.r_list == NULL && l->l_initfini == NULL
 	  && l != map && l->l_ldnum > 0)
-	needed = (struct link_map **) alloca (l->l_ldnum
-					      * sizeof (struct link_map *));
+	{
+	  int new_size;
+
+	  /* 16-align so extend_alloca has a chance to re-use the space.
+	     Note that extend_alloca is broken for recent versions of GCC
+	     on x86: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50938  */
+	  new_size = (l->l_ldnum * sizeof (struct link_map *) + 15) & ~15;
+
+	  if (new_size > needed_space_bytes)
+	    {
+	      needed_space
+		= (struct link_map **) extend_alloca (needed_space,
+						      needed_space_bytes,
+						      new_size);
+	    }
+	  needed = needed_space;
+	}
 
       if (l->l_info[DT_NEEDED] || l->l_info[AUXTAG] || l->l_info[FILTERTAG])
 	{


More information about the Libc-alpha mailing list