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