Linux: consolidate link implementation
[glibc.git] / elf / dl-sort-maps.c
blob3e2a6a584ec46f802a775ff088fdedb009014f0d
1 /* Sort array of link maps according to dependencies.
2 Copyright (C) 2017-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <ldsodefs.h>
21 #include <elf/dl-tunables.h>
23 /* Note: this is the older, "original" sorting algorithm, being used as
24 default up to 2.35.
26 Sort array MAPS according to dependencies of the contained objects.
27 If FOR_FINI is true, this is called for finishing an object. */
28 static void
29 _dl_sort_maps_original (struct link_map **maps, unsigned int nmaps,
30 bool force_first, bool for_fini)
32 /* Allows caller to do the common optimization of skipping the first map,
33 usually the main binary. */
34 maps += force_first;
35 nmaps -= force_first;
37 /* A list of one element need not be sorted. */
38 if (nmaps <= 1)
39 return;
41 unsigned int i = 0;
42 uint16_t seen[nmaps];
43 memset (seen, 0, nmaps * sizeof (seen[0]));
44 while (1)
46 /* Keep track of which object we looked at this round. */
47 ++seen[i];
48 struct link_map *thisp = maps[i];
50 if (__glibc_unlikely (for_fini))
52 /* Do not handle ld.so in secondary namespaces and objects which
53 are not removed. */
54 if (thisp != thisp->l_real || thisp->l_idx == -1)
55 goto skip;
58 /* Find the last object in the list for which the current one is
59 a dependency and move the current object behind the object
60 with the dependency. */
61 unsigned int k = nmaps - 1;
62 while (k > i)
64 struct link_map **runp = maps[k]->l_initfini;
65 if (runp != NULL)
66 /* Look through the dependencies of the object. */
67 while (*runp != NULL)
68 if (__glibc_unlikely (*runp++ == thisp))
70 move:
71 /* Move the current object to the back past the last
72 object with it as the dependency. */
73 memmove (&maps[i], &maps[i + 1],
74 (k - i) * sizeof (maps[0]));
75 maps[k] = thisp;
77 if (seen[i + 1] > nmaps - i)
79 ++i;
80 goto next_clear;
83 uint16_t this_seen = seen[i];
84 memmove (&seen[i], &seen[i + 1], (k - i) * sizeof (seen[0]));
85 seen[k] = this_seen;
87 goto next;
90 if (__glibc_unlikely (for_fini && maps[k]->l_reldeps != NULL))
92 unsigned int m = maps[k]->l_reldeps->act;
93 struct link_map **relmaps = &maps[k]->l_reldeps->list[0];
95 /* Look through the relocation dependencies of the object. */
96 while (m-- > 0)
97 if (__glibc_unlikely (relmaps[m] == thisp))
99 /* If a cycle exists with a link time dependency,
100 preserve the latter. */
101 struct link_map **runp = thisp->l_initfini;
102 if (runp != NULL)
103 while (*runp != NULL)
104 if (__glibc_unlikely (*runp++ == maps[k]))
105 goto ignore;
106 goto move;
108 ignore:;
111 --k;
114 skip:
115 if (++i == nmaps)
116 break;
117 next_clear:
118 memset (&seen[i], 0, (nmaps - i) * sizeof (seen[0]));
120 next:;
124 #if !HAVE_TUNABLES
125 /* In this case, just default to the original algorithm. */
126 strong_alias (_dl_sort_maps_original, _dl_sort_maps);
127 #else
129 /* We use a recursive function due to its better clarity and ease of
130 implementation, as well as faster execution speed. We already use
131 alloca() for list allocation during the breadth-first search of
132 dependencies in _dl_map_object_deps(), and this should be on the
133 same order of worst-case stack usage.
135 Note: the '*rpo' parameter is supposed to point to one past the
136 last element of the array where we save the sort results, and is
137 decremented before storing the current map at each level. */
139 static void
140 dfs_traversal (struct link_map ***rpo, struct link_map *map,
141 bool *do_reldeps)
143 /* _dl_map_object_deps ignores l_faked objects when calculating the
144 number of maps before calling _dl_sort_maps, ignore them as well. */
145 if (map->l_visited || map->l_faked)
146 return;
148 map->l_visited = 1;
150 if (map->l_initfini)
152 for (int i = 0; map->l_initfini[i] != NULL; i++)
154 struct link_map *dep = map->l_initfini[i];
155 if (dep->l_visited == 0
156 && dep->l_main_map == 0)
157 dfs_traversal (rpo, dep, do_reldeps);
161 if (__glibc_unlikely (do_reldeps != NULL && map->l_reldeps != NULL))
163 /* Indicate that we encountered relocation dependencies during
164 traversal. */
165 *do_reldeps = true;
167 for (int m = map->l_reldeps->act - 1; m >= 0; m--)
169 struct link_map *dep = map->l_reldeps->list[m];
170 if (dep->l_visited == 0
171 && dep->l_main_map == 0)
172 dfs_traversal (rpo, dep, do_reldeps);
176 *rpo -= 1;
177 **rpo = map;
180 /* Topologically sort array MAPS according to dependencies of the contained
181 objects. */
183 static void
184 _dl_sort_maps_dfs (struct link_map **maps, unsigned int nmaps,
185 bool force_first, bool for_fini)
187 struct link_map *first_map = maps[0];
188 for (int i = nmaps - 1; i >= 0; i--)
189 maps[i]->l_visited = 0;
191 /* We apply DFS traversal for each of maps[i] until the whole total order
192 is found and we're at the start of the Reverse-Postorder (RPO) sequence,
193 which is a topological sort.
195 We go from maps[nmaps - 1] backwards towards maps[0] at this level.
196 Due to the breadth-first search (BFS) ordering we receive, going
197 backwards usually gives a more shallow depth-first recursion depth,
198 adding more stack usage safety. Also, combined with the natural
199 processing order of l_initfini[] at each node during DFS, this maintains
200 an ordering closer to the original link ordering in the sorting results
201 under most simpler cases.
203 Another reason we order the top level backwards, it that maps[0] is
204 usually exactly the main object of which we're in the midst of
205 _dl_map_object_deps() processing, and maps[0]->l_initfini[] is still
206 blank. If we start the traversal from maps[0], since having no
207 dependencies yet filled in, maps[0] will always be immediately
208 incorrectly placed at the last place in the order (first in reverse).
209 Adjusting the order so that maps[0] is last traversed naturally avoids
210 this problem.
212 To summarize, just passing in the full list, and iterating from back
213 to front makes things much more straightforward. */
215 /* Array to hold RPO sorting results, before we copy back to maps[]. */
216 struct link_map *rpo[nmaps];
218 /* The 'head' position during each DFS iteration. Note that we start at
219 one past the last element due to first-decrement-then-store (see the
220 bottom of above dfs_traversal() routine). */
221 struct link_map **rpo_head = &rpo[nmaps];
223 bool do_reldeps = false;
224 bool *do_reldeps_ref = (for_fini ? &do_reldeps : NULL);
226 for (int i = nmaps - 1; i >= 0; i--)
228 dfs_traversal (&rpo_head, maps[i], do_reldeps_ref);
230 /* We can break early if all objects are already placed. */
231 if (rpo_head == rpo)
232 goto end;
234 assert (rpo_head == rpo);
236 end:
237 /* Here we may do a second pass of sorting, using only l_initfini[]
238 static dependency links. This is avoided if !FOR_FINI or if we didn't
239 find any reldeps in the first DFS traversal.
241 The reason we do this is: while it is unspecified how circular
242 dependencies should be handled, the presumed reasonable behavior is to
243 have destructors to respect static dependency links as much as possible,
244 overriding reldeps if needed. And the first sorting pass, which takes
245 l_initfini/l_reldeps links equally, may not preserve this priority.
247 Hence we do a 2nd sorting pass, taking only DT_NEEDED links into account
248 (see how the do_reldeps argument to dfs_traversal() is NULL below). */
249 if (do_reldeps)
251 for (int i = nmaps - 1; i >= 0; i--)
252 rpo[i]->l_visited = 0;
254 struct link_map **maps_head = &maps[nmaps];
255 for (int i = nmaps - 1; i >= 0; i--)
257 dfs_traversal (&maps_head, rpo[i], NULL);
259 /* We can break early if all objects are already placed.
260 The below memcpy is not needed in the do_reldeps case here,
261 since we wrote back to maps[] during DFS traversal. */
262 if (maps_head == maps)
263 return;
265 assert (maps_head == maps);
266 return;
269 memcpy (maps, rpo, sizeof (struct link_map *) * nmaps);
271 /* Skipping the first object at maps[0] is not valid in general,
272 since traversing along object dependency-links may "find" that
273 first object even when it is not included in the initial order
274 (e.g., a dlopen'ed shared object can have circular dependencies
275 linked back to itself). In such a case, traversing N-1 objects
276 will create a N-object result, and raise problems. Instead,
277 force the object back into first place after sorting. This naive
278 approach may introduce further dependency ordering violations
279 compared to rotating the cycle until the first map is again in
280 the first position, but as there is a cycle, at least one
281 violation is already present. */
282 if (force_first && maps[0] != first_map)
284 int i;
285 for (i = 0; maps[i] != first_map; ++i)
287 assert (i < nmaps);
288 memmove (&maps[1], maps, i * sizeof (maps[0]));
289 maps[0] = first_map;
293 void
294 _dl_sort_maps_init (void)
296 int32_t algorithm = TUNABLE_GET (glibc, rtld, dynamic_sort, int32_t, NULL);
297 GLRO(dl_dso_sort_algo) = algorithm == 1 ? dso_sort_algorithm_original
298 : dso_sort_algorithm_dfs;
301 void
302 _dl_sort_maps (struct link_map **maps, unsigned int nmaps,
303 bool force_first, bool for_fini)
305 /* It can be tempting to use a static function pointer to store and call
306 the current selected sorting algorithm routine, but experimentation
307 shows that current processors still do not handle indirect branches
308 that efficiently, plus a static function pointer will involve
309 PTR_MANGLE/DEMANGLE, further impairing performance of small, common
310 input cases. A simple if-case with direct function calls appears to
311 be the fastest. */
312 if (__glibc_likely (GLRO(dl_dso_sort_algo) == dso_sort_algorithm_original))
313 _dl_sort_maps_original (maps, nmaps, force_first, for_fini);
314 else
315 _dl_sort_maps_dfs (maps, nmaps, force_first, for_fini);
318 #endif /* HAVE_TUNABLES. */