7c0489
commit 0a8ce6a0966283b17f373f430929bcadef1ae205
7c0489
Author: David Kilroy <David.Kilroy@arm.com>
7c0489
Date:   Wed Feb 12 14:31:17 2020 -0300
7c0489
7c0489
    elf: avoid stack allocation in dl_open_worker
7c0489
    
7c0489
    As the sort was removed, there's no need to keep a separate map of
7c0489
    links. Instead, when relocating objects iterate over l_initfini
7c0489
    directly.
7c0489
    
7c0489
    This allows us to remove the loop copying l_initfini elements into
7c0489
    map. We still need a loop to identify the first and last elements that
7c0489
    need relocation.
7c0489
    
7c0489
    Tested by running the testsuite on x86_64.
7c0489
7c0489
diff --git a/elf/dl-open.c b/elf/dl-open.c
7c0489
index 980a28c836ca9a7a..46a4c1e5a3f8d2dd 100644
7c0489
--- a/elf/dl-open.c
7c0489
+++ b/elf/dl-open.c
7c0489
@@ -618,25 +618,18 @@ dl_open_worker (void *a)
7c0489
      This allows IFUNC relocations to work and it also means copy
7c0489
      relocation of dependencies are if necessary overwritten.
7c0489
      __dl_map_object_deps has already sorted l_initfini for us.  */
7c0489
-  unsigned int nmaps = 0;
7c0489
+  unsigned int first = UINT_MAX;
7c0489
+  unsigned int last = 0;
7c0489
   unsigned int j = 0;
7c0489
   struct link_map *l = new->l_initfini[0];
7c0489
   do
7c0489
     {
7c0489
       if (! l->l_real->l_relocated)
7c0489
-	++nmaps;
7c0489
-      l = new->l_initfini[++j];
7c0489
-    }
7c0489
-  while (l != NULL);
7c0489
-  /* Stack allocation is limited by the number of loaded objects.  */
7c0489
-  struct link_map *maps[nmaps];
7c0489
-  nmaps = 0;
7c0489
-  j = 0;
7c0489
-  l = new->l_initfini[0];
7c0489
-  do
7c0489
-    {
7c0489
-      if (! l->l_real->l_relocated)
7c0489
-	maps[nmaps++] = l;
7c0489
+	{
7c0489
+	  if (first == UINT_MAX)
7c0489
+	    first = j;
7c0489
+	  last = j + 1;
7c0489
+	}
7c0489
       l = new->l_initfini[++j];
7c0489
     }
7c0489
   while (l != NULL);
7c0489
@@ -651,9 +644,12 @@ dl_open_worker (void *a)
7c0489
      them.  However, such relocation dependencies in IFUNC resolvers
7c0489
      are undefined anyway, so this is not a problem.  */
7c0489
 
7c0489
-  for (unsigned int i = nmaps; i-- > 0; )
7c0489
+  for (unsigned int i = last; i-- > first; )
7c0489
     {
7c0489
-      l = maps[i];
7c0489
+      l = new->l_initfini[i];
7c0489
+
7c0489
+      if (l->l_real->l_relocated)
7c0489
+	continue;
7c0489
 
7c0489
       if (! relocation_in_progress)
7c0489
 	{