550b74
Early backport of upstream patch under discussion:
550b74
550b74
  [PATCH v3] elf: Fix DFS sorting algorithm for LD_TRACE_LOADED_OBJECTS
550b74
  with missing libraries (BZ #28868)
550b74
  <https://sourceware.org/pipermail/libc-alpha/2022-February/136641.html>
550b74
550b74
The tests are still being discussed upstream and are not backported
550b74
here.
550b74
550b74
diff --git a/elf/dl-deps.c b/elf/dl-deps.c
550b74
index 237d9636c5be780c..9e30c6c3f6c58783 100644
550b74
--- a/elf/dl-deps.c
550b74
+++ b/elf/dl-deps.c
550b74
@@ -489,6 +489,8 @@ _dl_map_object_deps (struct link_map *map,
550b74
 
550b74
   for (nlist = 0, runp = known; runp; runp = runp->next)
550b74
     {
550b74
+      /* _dl_sort_maps ignores l_faked object, so it is save to not considere
550b74
+	 them for nlist.  */
550b74
       if (__builtin_expect (trace_mode, 0) && runp->map->l_faked)
550b74
 	/* This can happen when we trace the loading.  */
550b74
 	--map->l_searchlist.r_nlist;
550b74
diff --git a/elf/dl-sort-maps.c b/elf/dl-sort-maps.c
550b74
index a274ed66cc987735..72f4ff0e6eda3377 100644
550b74
--- a/elf/dl-sort-maps.c
550b74
+++ b/elf/dl-sort-maps.c
550b74
@@ -140,7 +140,9 @@ static void
550b74
 dfs_traversal (struct link_map ***rpo, struct link_map *map,
550b74
 	       bool *do_reldeps)
550b74
 {
550b74
-  if (map->l_visited)
550b74
+  /* _dl_map_object_deps filter l_faked objects when calculating the
550b74
+     number of maps before calling _dl_sort_maps, ignore them as well.  */
550b74
+  if (map->l_visited || map->l_faked)
550b74
     return;
550b74
 
550b74
   map->l_visited = 1;