glibc/glibc-dso_deps.patch

460 lines
12 KiB
Diff
Raw Normal View History

@@ -, +, @@
17645)
---
elf/Makefile | 1 +
elf/dl-deps.c | 143 +++++++++++++++++++++++++++++------------------
elf/dl-fini.c | 176 +++++++++++++++++++++++++++++++---------------------------
elf/dl-open.c | 74 ++++++++++++++++++++++++
4 files changed, 256 insertions(+), 138 deletions(-)
--- a/elf/Makefile
+++ a/elf/Makefile
@@ -513,6 +513,7 @@ $(objpfx)unload8mod1.so: $(objpfx)unload8mod2.so
$(objpfx)unload8mod2.so: $(objpfx)unload8mod3.so
$(objpfx)unload8mod3.so: $(libdl)
$(objpfx)tst-initordera2.so: $(objpfx)tst-initordera1.so
+$(objpfx)tst-initorderb1.so: $(objpfx)tst-initordera2.so
$(objpfx)tst-initorderb2.so: $(objpfx)tst-initorderb1.so $(objpfx)tst-initordera2.so
$(objpfx)tst-initordera3.so: $(objpfx)tst-initorderb2.so $(objpfx)tst-initorderb1.so
$(objpfx)tst-initordera4.so: $(objpfx)tst-initordera3.so
--- a/elf/dl-deps.c
+++ a/elf/dl-deps.c
@@ -139,6 +139,93 @@ cannot load auxiliary `%s' because of empty dynamic string token " \
__result; })
static void
+weight (struct link_map **maps, size_t nmaps, char *seen, unsigned int *w)
+{
+ int i, c;
+ struct link_map *m, **r;
+
+ /* Start at 1 because the first entry is the binary itself. */
+ for (i = 1; i < nmaps; ++i)
+ {
+ m = maps[i];
+ if ((r = m->l_initfini))
+ {
+ c = 0;
+ while (*r)
+ {
+
+ /* If not a self reference, not being unloaded, and not
+ already removed from the list, increment the reference
+ counter. */
+ if (*r != m && (*r)->l_idx >= 0 && !seen[(*r)->l_idx])
+ ++c;
+ ++r;
+ }
+
+ /* Update the {w}eight vector with {c}ount map(s) dependency */
+ w[i] = c;
+ }
+ else
+ w[i] = 0;
+ }
+}
+
+static void
+sort (struct link_map **maps, size_t nmaps)
+{
+ int c, i, k;
+ unsigned int w[nmaps];
+ char seen[nmaps];
+ struct link_map *t;
+ memset(seen, 0, nmaps);
+
+ /* Use the l_idx field as an unique identifier. */
+ for (i = 0; i < nmaps; ++i)
+ {
+ /* Ensure to not touch a link_map with l_idx < 0 as it is
+ used to flag a dso is being unloaded. */
+ if (maps[i]->l_idx >= 0)
+ maps[i]->l_idx = i;
+ }
+
+ /* We can skip looking for the binary itself which is at the front
+ of the search list. */
+ w[0] = 0;
+ seen[0] = 1;
+
+ /* The list is sorted in place, with entries added from end to start. */
+ weight(maps, k = nmaps, seen, w);
+ c = 0;
+ while (c < k)
+ {
+ for (i = k - 1; i >= 1; --i)
+ {
+ if (w[i] == c && maps[i]->l_idx >= 0)
+ {
+ seen[maps[i]->l_idx] = 1;
+
+ /* Reducing the number of entries or swaping entries
+ require remaking the {w}eight map. */
+ if (--k != i)
+ {
+ t = maps[k];
+ maps[k] = maps[i];
+ maps[i] = t;
+ }
+ weight(maps, k, seen, w);
+ c = -1;
+ break;
+ }
+ }
+
+ /* If c != 0, there are cycles, and the first entry in the map,
+ with the lowest reference {c}ount is removed attempting to
+ to break the cycle. */
+ ++c;
+ }
+}
+
+static void
preload (struct list *known, unsigned int *nlist, struct link_map *map)
{
known[*nlist].done = 0;
@@ -611,61 +698,7 @@ Filters not supported with LD_TRACE_PRELINKING"));
memcpy (l_initfini, map->l_searchlist.r_list,
nlist * sizeof (struct link_map *));
if (__glibc_likely (nlist > 1))
- {
- /* We can skip looking for the binary itself which is at the front
- of the search list. */
- i = 1;
- uint16_t seen[nlist];
- memset (seen, 0, nlist * sizeof (seen[0]));
- while (1)
- {
- /* Keep track of which object we looked at this round. */
- ++seen[i];
- struct link_map *thisp = l_initfini[i];
-
- /* Find the last object in the list for which the current one is
- a dependency and move the current object behind the object
- with the dependency. */
- unsigned int k = nlist - 1;
- while (k > i)
- {
- struct link_map **runp = l_initfini[k]->l_initfini;
- if (runp != NULL)
- /* Look through the dependencies of the object. */
- while (*runp != NULL)
- if (__glibc_unlikely (*runp++ == thisp))
- {
- /* Move the current object to the back past the last
- object with it as the dependency. */
- memmove (&l_initfini[i], &l_initfini[i + 1],
- (k - i) * sizeof (l_initfini[0]));
- l_initfini[k] = thisp;
-
- if (seen[i + 1] > nlist - i)
- {
- ++i;
- goto next_clear;
- }
-
- uint16_t this_seen = seen[i];
- memmove (&seen[i], &seen[i + 1],
- (k - i) * sizeof (seen[0]));
- seen[k] = this_seen;
-
- goto next;
- }
-
- --k;
- }
-
- if (++i == nlist)
- break;
- next_clear:
- memset (&seen[i], 0, (nlist - i) * sizeof (seen[0]));
-
- next:;
- }
- }
+ sort (l_initfini, nlist);
/* Terminate the list of dependencies. */
l_initfini[nlist] = NULL;
--- a/elf/dl-fini.c
+++ a/elf/dl-fini.c
@@ -26,102 +26,112 @@
typedef void (*fini_t) (void);
-void
-internal_function
-_dl_sort_fini (struct link_map **maps, size_t nmaps, char *used, Lmid_t ns)
+static void
+weight (struct link_map **maps, size_t nmaps, char *seen, unsigned int *w, int from)
{
- /* A list of one element need not be sorted. */
- if (nmaps == 1)
- return;
+ int i, c;
+ struct link_map *m, **r;
+ for (i = from; i < nmaps; ++i)
+ {
+ m = maps[i];
+ if ((r = m->l_initfini))
+ {
+ c = 0;
+ while (*r)
+ {
+ /* If not a self reference, not being unloaded, and not
+ already removed from the list, increment the reference
+ counter. */
+ if (*r != m && (*r)->l_idx >= 0 && !seen[(*r)->l_idx])
+ ++c;
+ ++r;
+ }
+ /* Update the {w}eight vector with {c}ount map(s) dependency */
+ w[i] = c;
+ }
+ else
+ w[i] = 0;
+ }
+}
+
+
+static void
+sort (struct link_map **maps, size_t nmaps, char *used, Lmid_t ns)
+{
+ int from;
+ int c, i, k;
+ unsigned int w[nmaps];
+ char seen[nmaps];
+ struct link_map *t;
+
+ memset(seen, 0, nmaps);
+
+ /* Use the l_idx field as an unique identifier. */
+ for (i = 0; i < nmaps; ++i)
+ {
+ /* Ensure to not touch a link_map with l_idx < 0 as it is
+ used to flag a dso is being unloaded. */
+ if (maps[i]->l_idx >= 0)
+ maps[i]->l_idx = i;
+ }
/* We can skip looking for the binary itself which is at the front
of the search list for the main namespace. */
- unsigned int i = ns == LM_ID_BASE;
- uint16_t seen[nmaps];
- memset (seen, 0, nmaps * sizeof (seen[0]));
- while (1)
+ from = ns == LM_ID_BASE;
+ if (from)
{
- /* Keep track of which object we looked at this round. */
- ++seen[i];
- struct link_map *thisp = maps[i];
-
- /* Do not handle ld.so in secondary namespaces and object which
- are not removed. */
- if (thisp != thisp->l_real || thisp->l_idx == -1)
- goto skip;
-
- /* Find the last object in the list for which the current one is
- a dependency and move the current object behind the object
- with the dependency. */
- unsigned int k = nmaps - 1;
- while (k > i)
- {
- struct link_map **runp = maps[k]->l_initfini;
- if (runp != NULL)
- /* Look through the dependencies of the object. */
- while (*runp != NULL)
- if (__glibc_unlikely (*runp++ == thisp))
- {
- move:
- /* Move the current object to the back past the last
- object with it as the dependency. */
- memmove (&maps[i], &maps[i + 1],
- (k - i) * sizeof (maps[0]));
- maps[k] = thisp;
-
- if (used != NULL)
- {
- char here_used = used[i];
- memmove (&used[i], &used[i + 1],
- (k - i) * sizeof (used[0]));
- used[k] = here_used;
- }
+ w[0] = 0;
+ seen[0] = 1;
+ }
- if (seen[i + 1] > nmaps - i)
+ /* The list is sorted in place, with entries added from end to start. */
+ weight(maps, k = nmaps, seen, w, from);
+ c = 0;
+ while (c < k)
+ {
+ for (i = k - 1; i >= from; --i)
+ {
+ t = maps[i];
+ /* Do not handle ld.so in secondary namespaces and object which
+ are not removed. */
+ if (w[i] == c && maps[i]->l_idx >= 0 && t->l_real == t)
+ {
+ seen[maps[i]->l_idx] = 1;
+ /* Reducing the number of entries or swaping entries
+ require remaking the {w}eight map. */
+ if (--k != i)
+ {
+ maps[i] = maps[k];
+ maps[k] = t;
+ if (used)
{
- ++i;
- goto next_clear;
+ char c = used[i];
+ used[i] = used[k];
+ used[k] = c;
}
-
- uint16_t this_seen = seen[i];
- memmove (&seen[i], &seen[i + 1], (k - i) * sizeof (seen[0]));
- seen[k] = this_seen;
-
- goto next;
}
-
- if (__glibc_unlikely (maps[k]->l_reldeps != NULL))
- {
- unsigned int m = maps[k]->l_reldeps->act;
- struct link_map **relmaps = &maps[k]->l_reldeps->list[0];
-
- /* Look through the relocation dependencies of the object. */
- while (m-- > 0)
- if (__glibc_unlikely (relmaps[m] == thisp))
- {
- /* If a cycle exists with a link time dependency,
- preserve the latter. */
- struct link_map **runp = thisp->l_initfini;
- if (runp != NULL)
- while (*runp != NULL)
- if (__glibc_unlikely (*runp++ == maps[k]))
- goto ignore;
- goto move;
- }
- ignore:;
+ weight(maps, k, seen, w, from);
+ c = -1;
+ break;
}
-
- --k;
}
+ /* If c != 0, there are cycles, and the first entry in the map,
+ with the lowest reference {c}ount is removed attempting to
+ to break the cycle. */
+ ++c;
+ }
+}
- skip:
- if (++i == nmaps)
- break;
- next_clear:
- memset (&seen[i], 0, (nmaps - i) * sizeof (seen[0]));
- next:;
- }
+void
+internal_function
+_dl_sort_fini (struct link_map **maps, size_t nmaps, char *used, Lmid_t ns)
+{
+ /* A list of one element need not be sorted. */
+ if (nmaps == 1)
+ return;
+
+ sort (maps, nmaps, used, ns);
}
--- a/elf/dl-open.c
+++ a/elf/dl-open.c
@@ -182,6 +182,80 @@ _dl_find_dso_for_object (const ElfW(Addr) addr)
rtld_hidden_def (_dl_find_dso_for_object);
static void
+weight (struct link_map **maps, size_t nmaps, char *seen, unsigned int *w)
+{
+ int i, c;
+ struct link_map *m, **r;
+ for (i = 0; i < nmaps; ++i)
+ {
+ m = maps[i];
+ if ((r = m->l_initfini))
+ {
+ c = 0;
+ while (*r)
+ {
+ /* If not a self reference, not being unloaded, and not
+ already removed from the list, increment the reference
+ counter. */
+ if (*r != m && (*r)->l_idx >= 0 && !seen[(*r)->l_idx])
+ ++c;
+ ++r;
+ }
+ /* Update the {w}eight vector with {c}ount map(s) dependency */
+ w[i] = c;
+ }
+ else
+ w[i] = 0;
+ }
+}
+
+static void
+sort (struct link_map **maps, size_t nmaps)
+{
+ int c, i, k;
+ unsigned int w[nmaps];
+ char seen[nmaps];
+ struct link_map *t;
+ memset(seen, 0, nmaps);
+ /* Use the l_idx field as an unique identifier. */
+ for (i = 0; i < nmaps; ++i)
+ {
+ /* Ensure to not touch a link_map with l_idx < 0 as it is
+ used to flag a dso is being unloaded. */
+ if (maps[i]->l_idx >= 0)
+ maps[i]->l_idx = i;
+ }
+ /* The list is sorted in place, with entries added from end to start. */
+ weight(maps, k = nmaps, seen, w);
+ c = 0;
+ while (c < k)
+ {
+ for (i = k - 1; i >= 0; --i)
+ {
+ if (w[i] == c && maps[i]->l_idx >= 0)
+ {
+ seen[maps[i]->l_idx] = 1;
+ /* Reducing the number of entries or swaping entries
+ require remaking the {w}eight map. */
+ if (--k != i)
+ {
+ t = maps[k];
+ maps[k] = maps[i];
+ maps[i] = t;
+ }
+ weight(maps, k, seen, w);
+ c = -1;
+ break;
+ }
+ }
+ /* If c != 0, there are cycles, and the first entry in the map,
+ with the lowest reference {c}ount is removed attempting to
+ to break the cycle. */
+ ++c;
+ }
+}
+
+static void
dl_open_worker (void *a)
{
struct dl_open_args *args = a;
--