[patch] Reduce ridiculous stack use in _dl_map_object_deps

Paul Pluzhnikov ppluzhnikov@google.com
Mon Oct 31 21:12:00 GMT 2011


On Mon, Oct 31, 2011 at 1:36 PM, Roland McGrath <roland@hack.frob.com> wrote:
>> +       int new_size;
>
> size_t
...
> No braces around a single statement.  No cast required on extend_alloca.
> It returns the type of its first argument.

Updated patch attached.

Thanks,
-- 
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..4e88149 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;
+  size_t 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,20 @@ _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 *));
+	{
+	  size_t 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
+	      = 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