]>
Commit | Line | Data |
---|---|---|
efec1d0c | 1 | /* Load the dependencies of a mapped object. |
04277e02 | 2 | Copyright (C) 1996-2019 Free Software Foundation, Inc. |
afd4eb37 UD |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
afd4eb37 UD |
9 | |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
afd4eb37 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
efec1d0c | 18 | |
385b4cf4 | 19 | #include <atomic.h> |
dc5efe83 | 20 | #include <assert.h> |
efec1d0c | 21 | #include <dlfcn.h> |
a853022c | 22 | #include <errno.h> |
8e17ea58 | 23 | #include <libintl.h> |
32e6df36 | 24 | #include <stddef.h> |
efec1d0c | 25 | #include <stdlib.h> |
ca34d7a7 | 26 | #include <string.h> |
06535ae9 | 27 | #include <unistd.h> |
dc5efe83 | 28 | #include <sys/param.h> |
a42195db | 29 | #include <ldsodefs.h> |
92d6aa85 | 30 | #include <scratch_buffer.h> |
a853022c | 31 | |
dc5efe83 | 32 | #include <dl-dst.h> |
1522c368 UD |
33 | |
34 | /* Whether an shared object references one or more auxiliary objects | |
35 | is signaled by the AUXTAG entry in l_info. */ | |
b0982c4a | 36 | #define AUXTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \ |
1522c368 | 37 | + DT_EXTRATAGIDX (DT_AUXILIARY)) |
f41c8091 UD |
38 | /* Whether an shared object references one or more auxiliary objects |
39 | is signaled by the AUXTAG entry in l_info. */ | |
b0982c4a | 40 | #define FILTERTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \ |
f41c8091 | 41 | + DT_EXTRATAGIDX (DT_FILTER)) |
1522c368 UD |
42 | |
43 | ||
44 | /* When loading auxiliary objects we must ignore errors. It's ok if | |
45 | an object is missing. */ | |
993b3242 | 46 | struct openaux_args |
1522c368 UD |
47 | { |
48 | /* The arguments to openaux. */ | |
49 | struct link_map *map; | |
50 | int trace_mode; | |
87837aac | 51 | int open_mode; |
1522c368 | 52 | const char *strtab; |
dc5efe83 | 53 | const char *name; |
993b3242 | 54 | |
1522c368 UD |
55 | /* The return value of openaux. */ |
56 | struct link_map *aux; | |
57 | }; | |
993b3242 UD |
58 | |
59 | static void | |
60 | openaux (void *a) | |
61 | { | |
62 | struct openaux_args *args = (struct openaux_args *) a; | |
63 | ||
8e9f92e9 | 64 | args->aux = _dl_map_object (args->map, args->name, |
154d10bd UD |
65 | (args->map->l_type == lt_executable |
66 | ? lt_library : args->map->l_type), | |
c0f62c56 UD |
67 | args->trace_mode, args->open_mode, |
68 | args->map->l_ns); | |
993b3242 UD |
69 | } |
70 | ||
32e6df36 | 71 | static ptrdiff_t |
32e6df36 UD |
72 | _dl_build_local_scope (struct link_map **list, struct link_map *map) |
73 | { | |
74 | struct link_map **p = list; | |
75 | struct link_map **q; | |
76 | ||
77 | *p++ = map; | |
78 | map->l_reserved = 1; | |
79 | if (map->l_initfini) | |
80 | for (q = map->l_initfini + 1; *q; ++q) | |
81 | if (! (*q)->l_reserved) | |
82 | p += _dl_build_local_scope (p, *q); | |
83 | return p - list; | |
84 | } | |
1522c368 UD |
85 | |
86 | ||
80d9c5f0 | 87 | /* We use a very special kind of list to track the path |
1522c368 | 88 | through the list of loaded shared objects. We have to |
80d9c5f0 | 89 | produce a flat list with unique members of all involved objects. |
1522c368 UD |
90 | */ |
91 | struct list | |
92 | { | |
93 | int done; /* Nonzero if this map was processed. */ | |
94 | struct link_map *map; /* The data. */ | |
ac55a25b | 95 | struct list *next; /* Elements for normal list. */ |
1522c368 UD |
96 | }; |
97 | ||
98 | ||
dc5efe83 UD |
99 | /* Macro to expand DST. It is an macro since we use `alloca'. */ |
100 | #define expand_dst(l, str, fatal) \ | |
101 | ({ \ | |
102 | const char *__str = (str); \ | |
103 | const char *__result = __str; \ | |
5aad5f61 | 104 | size_t __dst_cnt = _dl_dst_count (__str); \ |
dc5efe83 | 105 | \ |
ac55a25b | 106 | if (__dst_cnt != 0) \ |
dc5efe83 | 107 | { \ |
06535ae9 UD |
108 | char *__newp; \ |
109 | \ | |
110 | /* DST must not appear in SUID/SGID programs. */ \ | |
6bc6bd3b | 111 | if (__libc_enable_secure) \ |
154d10bd | 112 | _dl_signal_error (0, __str, NULL, N_("\ |
87837aac | 113 | DST not allowed in SUID/SGID programs")); \ |
06535ae9 UD |
114 | \ |
115 | __newp = (char *) alloca (DL_DST_REQUIRED (l, __str, strlen (__str), \ | |
ac55a25b | 116 | __dst_cnt)); \ |
dc5efe83 | 117 | \ |
2bd86632 | 118 | __result = _dl_dst_substitute (l, __str, __newp); \ |
dc5efe83 UD |
119 | \ |
120 | if (*__result == '\0') \ | |
121 | { \ | |
122 | /* The replacement for the DST is not known. We can't \ | |
123 | processed. */ \ | |
124 | if (fatal) \ | |
154d10bd | 125 | _dl_signal_error (0, __str, NULL, N_("\ |
11bf311e | 126 | empty dynamic string token substitution")); \ |
dc5efe83 UD |
127 | else \ |
128 | { \ | |
129 | /* This is for DT_AUXILIARY. */ \ | |
a1ffb40e | 130 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS)) \ |
154d10bd | 131 | _dl_debug_printf (N_("\ |
7969407a UD |
132 | cannot load auxiliary `%s' because of empty dynamic string token " \ |
133 | "substitution\n"), __str); \ | |
dc5efe83 UD |
134 | continue; \ |
135 | } \ | |
136 | } \ | |
137 | } \ | |
138 | \ | |
139 | __result; }) | |
140 | ||
06210a44 KS |
141 | static void |
142 | preload (struct list *known, unsigned int *nlist, struct link_map *map) | |
143 | { | |
144 | known[*nlist].done = 0; | |
145 | known[*nlist].map = map; | |
146 | known[*nlist].next = &known[*nlist + 1]; | |
147 | ||
148 | ++*nlist; | |
149 | /* We use `l_reserved' as a mark bit to detect objects we have | |
150 | already put in the search list and avoid adding duplicate | |
151 | elements later in the list. */ | |
152 | map->l_reserved = 1; | |
153 | } | |
dc5efe83 | 154 | |
d9cb1a7d | 155 | void |
2064087b | 156 | _dl_map_object_deps (struct link_map *map, |
46ec036d | 157 | struct link_map **preloads, unsigned int npreloads, |
87837aac | 158 | int trace_mode, int open_mode) |
efec1d0c | 159 | { |
db2f05ba | 160 | struct list *known = __alloca (sizeof *known * (1 + npreloads + 1)); |
80d9c5f0 UD |
161 | struct list *runp, *tail; |
162 | unsigned int nlist, i; | |
c77a4478 UD |
163 | /* Object name. */ |
164 | const char *name; | |
165 | int errno_saved; | |
166 | int errno_reason; | |
2449ae7b | 167 | struct dl_exception exception; |
1522c368 | 168 | |
1522c368 UD |
169 | /* No loaded object so far. */ |
170 | nlist = 0; | |
2064087b | 171 | |
1522c368 | 172 | /* First load MAP itself. */ |
06210a44 | 173 | preload (known, &nlist, map); |
df4ef2ab UD |
174 | |
175 | /* Add the preloaded items after MAP but before any of its dependencies. */ | |
176 | for (i = 0; i < npreloads; ++i) | |
06210a44 | 177 | preload (known, &nlist, preloads[i]); |
df4ef2ab | 178 | |
8a523922 | 179 | /* Terminate the lists. */ |
80d9c5f0 | 180 | known[nlist - 1].next = NULL; |
1522c368 | 181 | |
1522c368 | 182 | /* Pointer to last unique object. */ |
80d9c5f0 | 183 | tail = &known[nlist - 1]; |
efec1d0c | 184 | |
92d6aa85 FW |
185 | struct scratch_buffer needed_space; |
186 | scratch_buffer_init (&needed_space); | |
02f9c6cf | 187 | |
1522c368 UD |
188 | /* Process each element of the search list, loading each of its |
189 | auxiliary objects and immediate dependencies. Auxiliary objects | |
190 | will be added in the list before the object itself and | |
191 | dependencies will be appended to the list as we step through it. | |
192 | This produces a flat, ordered list that represents a | |
193 | breadth-first search of the dependency tree. | |
194 | ||
195 | The whole process is complicated by the fact that we better | |
196 | should use alloca for the temporary list elements. But using | |
197 | alloca means we cannot use recursive function calls. */ | |
c77a4478 UD |
198 | errno_saved = errno; |
199 | errno_reason = 0; | |
200 | errno = 0; | |
201 | name = NULL; | |
1522c368 | 202 | for (runp = known; runp; ) |
efec1d0c | 203 | { |
1522c368 | 204 | struct link_map *l = runp->map; |
dacc8ffa UD |
205 | struct link_map **needed = NULL; |
206 | unsigned int nneeded = 0; | |
207 | ||
208 | /* Unless otherwise stated, this object is handled. */ | |
209 | runp->done = 1; | |
210 | ||
211 | /* Allocate a temporary record to contain the references to the | |
212 | dependencies of this object. */ | |
07a3d63e UD |
213 | if (l->l_searchlist.r_list == NULL && l->l_initfini == NULL |
214 | && l != map && l->l_ldnum > 0) | |
02f9c6cf | 215 | { |
92d6aa85 FW |
216 | /* l->l_ldnum includes space for the terminating NULL. */ |
217 | if (!scratch_buffer_set_array_size | |
218 | (&needed_space, l->l_ldnum, sizeof (struct link_map *))) | |
219 | _dl_signal_error (ENOMEM, map->l_name, NULL, | |
220 | N_("cannot allocate dependency buffer")); | |
221 | needed = needed_space.data; | |
02f9c6cf | 222 | } |
f68b86cc | 223 | |
8193034b | 224 | if (l->l_info[DT_NEEDED] || l->l_info[AUXTAG] || l->l_info[FILTERTAG]) |
efec1d0c | 225 | { |
a42195db | 226 | const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]); |
1522c368 UD |
227 | struct openaux_args args; |
228 | struct list *orig; | |
266180eb | 229 | const ElfW(Dyn) *d; |
1522c368 | 230 | |
1522c368 UD |
231 | args.strtab = strtab; |
232 | args.map = l; | |
233 | args.trace_mode = trace_mode; | |
87837aac | 234 | args.open_mode = open_mode; |
1522c368 UD |
235 | orig = runp; |
236 | ||
efec1d0c | 237 | for (d = l->l_ld; d->d_tag != DT_NULL; ++d) |
e3e35cfc | 238 | if (__builtin_expect (d->d_tag, DT_NEEDED) == DT_NEEDED) |
efec1d0c | 239 | { |
f68b86cc | 240 | /* Map in the needed object. */ |
dc5efe83 | 241 | struct link_map *dep; |
dc5efe83 UD |
242 | |
243 | /* Recognize DSTs. */ | |
244 | name = expand_dst (l, strtab + d->d_un.d_val, 0); | |
c77a4478 UD |
245 | /* Store the tag in the argument structure. */ |
246 | args.name = name; | |
dc5efe83 | 247 | |
2449ae7b FW |
248 | int err = _dl_catch_exception (&exception, openaux, &args); |
249 | if (__glibc_unlikely (exception.errstring != NULL)) | |
c77a4478 | 250 | { |
505d4b24 UD |
251 | if (err) |
252 | errno_reason = err; | |
c77a4478 UD |
253 | else |
254 | errno_reason = -1; | |
255 | goto out; | |
256 | } | |
257 | else | |
258 | dep = args.aux; | |
1522c368 | 259 | |
42c4f32a | 260 | if (! dep->l_reserved) |
efec1d0c | 261 | { |
80d9c5f0 UD |
262 | /* Allocate new entry. */ |
263 | struct list *newp; | |
264 | ||
265 | newp = alloca (sizeof (struct list)); | |
266 | ||
267 | /* Append DEP to the list. */ | |
268 | newp->map = dep; | |
1522c368 | 269 | newp->done = 0; |
80d9c5f0 UD |
270 | newp->next = NULL; |
271 | tail->next = newp; | |
272 | tail = newp; | |
f68b86cc | 273 | ++nlist; |
f9496a7b RM |
274 | /* Set the mark bit that says it's already in the list. */ |
275 | dep->l_reserved = 1; | |
efec1d0c | 276 | } |
dacc8ffa UD |
277 | |
278 | /* Remember this dependency. */ | |
279 | if (needed != NULL) | |
280 | needed[nneeded++] = dep; | |
1522c368 | 281 | } |
f41c8091 | 282 | else if (d->d_tag == DT_AUXILIARY || d->d_tag == DT_FILTER) |
1522c368 | 283 | { |
f41c8091 | 284 | struct list *newp; |
dc5efe83 UD |
285 | |
286 | /* Recognize DSTs. */ | |
287 | name = expand_dst (l, strtab + d->d_un.d_val, | |
288 | d->d_tag == DT_AUXILIARY); | |
c77a4478 UD |
289 | /* Store the tag in the argument structure. */ |
290 | args.name = name; | |
84384f5b | 291 | |
3dfb9a5c OB |
292 | /* Say that we are about to load an auxiliary library. */ |
293 | if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, | |
294 | 0)) | |
295 | _dl_debug_printf ("load auxiliary object=%s" | |
296 | " requested by file=%s\n", | |
297 | name, | |
298 | DSO_FILENAME (l->l_name)); | |
299 | ||
300 | /* We must be prepared that the addressed shared | |
301 | object is not available. For filter objects the dependency | |
302 | must be available. */ | |
2449ae7b FW |
303 | int err = _dl_catch_exception (&exception, openaux, &args); |
304 | if (__glibc_unlikely (exception.errstring != NULL)) | |
1522c368 | 305 | { |
3dfb9a5c | 306 | if (d->d_tag == DT_AUXILIARY) |
f41c8091 UD |
307 | { |
308 | /* We are not interested in the error message. */ | |
2449ae7b | 309 | _dl_exception_free (&exception); |
f41c8091 UD |
310 | /* Simply ignore this error and continue the work. */ |
311 | continue; | |
312 | } | |
3dfb9a5c | 313 | else |
c77a4478 | 314 | { |
505d4b24 UD |
315 | if (err) |
316 | errno_reason = err; | |
c77a4478 UD |
317 | else |
318 | errno_reason = -1; | |
319 | goto out; | |
320 | } | |
8d9618b7 | 321 | } |
f41c8091 UD |
322 | |
323 | /* The auxiliary object is actually available. | |
324 | Incorporate the map in all the lists. */ | |
325 | ||
326 | /* Allocate new entry. This always has to be done. */ | |
327 | newp = alloca (sizeof (struct list)); | |
328 | ||
ee0f7bd6 UD |
329 | /* We want to insert the new map before the current one, |
330 | but we have no back links. So we copy the contents of | |
331 | the current entry over. Note that ORIG and NEWP now | |
332 | have switched their meanings. */ | |
80d9c5f0 | 333 | memcpy (newp, orig, sizeof (*newp)); |
f41c8091 UD |
334 | |
335 | /* Initialize new entry. */ | |
336 | orig->done = 0; | |
337 | orig->map = args.aux; | |
f41c8091 | 338 | |
dacc8ffa UD |
339 | /* Remember this dependency. */ |
340 | if (needed != NULL) | |
341 | needed[nneeded++] = args.aux; | |
342 | ||
f41c8091 UD |
343 | /* We must handle two situations here: the map is new, |
344 | so we must add it in all three lists. If the map | |
345 | is already known, we have two further possibilities: | |
346 | - if the object is before the current map in the | |
347 | search list, we do nothing. It is already found | |
348 | early | |
349 | - if the object is after the current one, we must | |
350 | move it just before the current map to make sure | |
351 | the symbols are found early enough | |
352 | */ | |
353 | if (args.aux->l_reserved) | |
1522c368 | 354 | { |
f41c8091 UD |
355 | /* The object is already somewhere in the list. |
356 | Locate it first. */ | |
357 | struct list *late; | |
358 | ||
359 | /* This object is already in the search list we | |
360 | are building. Don't add a duplicate pointer. | |
42c4f32a | 361 | Just added by _dl_map_object. */ |
23382b36 | 362 | for (late = newp; late->next != NULL; late = late->next) |
80d9c5f0 | 363 | if (late->next->map == args.aux) |
f41c8091 UD |
364 | break; |
365 | ||
23382b36 | 366 | if (late->next != NULL) |
1522c368 | 367 | { |
f41c8091 UD |
368 | /* The object is somewhere behind the current |
369 | position in the search path. We have to | |
370 | move it to this earlier position. */ | |
80d9c5f0 | 371 | orig->next = newp; |
f41c8091 | 372 | |
80d9c5f0 | 373 | /* Now remove the later entry from the list |
ee0f7bd6 | 374 | and adjust the tail pointer. */ |
80d9c5f0 UD |
375 | if (tail == late->next) |
376 | tail = late; | |
377 | late->next = late->next->next; | |
f41c8091 | 378 | |
ee0f7bd6 | 379 | /* We must move the object earlier in the chain. */ |
23382b36 | 380 | if (args.aux->l_prev != NULL) |
1522c368 | 381 | args.aux->l_prev->l_next = args.aux->l_next; |
23382b36 | 382 | if (args.aux->l_next != NULL) |
1522c368 UD |
383 | args.aux->l_next->l_prev = args.aux->l_prev; |
384 | ||
385 | args.aux->l_prev = newp->map->l_prev; | |
386 | newp->map->l_prev = args.aux; | |
387 | if (args.aux->l_prev != NULL) | |
388 | args.aux->l_prev->l_next = args.aux; | |
389 | args.aux->l_next = newp->map; | |
390 | } | |
f41c8091 UD |
391 | else |
392 | { | |
393 | /* The object must be somewhere earlier in the | |
23382b36 UD |
394 | list. Undo to the current list element what |
395 | we did above. */ | |
396 | memcpy (orig, newp, sizeof (*newp)); | |
397 | continue; | |
f41c8091 UD |
398 | } |
399 | } | |
400 | else | |
401 | { | |
402 | /* This is easy. We just add the symbol right here. */ | |
80d9c5f0 | 403 | orig->next = newp; |
f41c8091 UD |
404 | ++nlist; |
405 | /* Set the mark bit that says it's already in the list. */ | |
406 | args.aux->l_reserved = 1; | |
407 | ||
408 | /* The only problem is that in the double linked | |
409 | list of all objects we don't have this new | |
410 | object at the correct place. Correct this here. */ | |
411 | if (args.aux->l_prev) | |
412 | args.aux->l_prev->l_next = args.aux->l_next; | |
413 | if (args.aux->l_next) | |
414 | args.aux->l_next->l_prev = args.aux->l_prev; | |
415 | ||
416 | args.aux->l_prev = newp->map->l_prev; | |
417 | newp->map->l_prev = args.aux; | |
418 | if (args.aux->l_prev != NULL) | |
419 | args.aux->l_prev->l_next = args.aux; | |
420 | args.aux->l_next = newp->map; | |
421 | } | |
1522c368 | 422 | |
80d9c5f0 UD |
423 | /* Move the tail pointer if necessary. */ |
424 | if (orig == tail) | |
425 | tail = newp; | |
1522c368 | 426 | |
f41c8091 UD |
427 | /* Move on the insert point. */ |
428 | orig = newp; | |
efec1d0c RM |
429 | } |
430 | } | |
dacc8ffa UD |
431 | |
432 | /* Terminate the list of dependencies and store the array address. */ | |
433 | if (needed != NULL) | |
434 | { | |
435 | needed[nneeded++] = NULL; | |
436 | ||
385b4cf4 | 437 | struct link_map **l_initfini = (struct link_map **) |
aff4519d | 438 | malloc ((2 * nneeded + 1) * sizeof needed[0]); |
385b4cf4 | 439 | if (l_initfini == NULL) |
92d6aa85 FW |
440 | { |
441 | scratch_buffer_free (&needed_space); | |
442 | _dl_signal_error (ENOMEM, map->l_name, NULL, | |
443 | N_("cannot allocate dependency list")); | |
444 | } | |
385b4cf4 UD |
445 | l_initfini[0] = l; |
446 | memcpy (&l_initfini[1], needed, nneeded * sizeof needed[0]); | |
447 | memcpy (&l_initfini[nneeded + 1], l_initfini, | |
aff4519d | 448 | nneeded * sizeof needed[0]); |
385b4cf4 UD |
449 | atomic_write_barrier (); |
450 | l->l_initfini = l_initfini; | |
0479b305 | 451 | l->l_free_initfini = 1; |
dacc8ffa | 452 | } |
1522c368 UD |
453 | |
454 | /* If we have no auxiliary objects just go on to the next map. */ | |
455 | if (runp->done) | |
456 | do | |
80d9c5f0 | 457 | runp = runp->next; |
8193034b | 458 | while (runp != NULL && runp->done); |
efec1d0c RM |
459 | } |
460 | ||
c4bb124a | 461 | out: |
92d6aa85 FW |
462 | scratch_buffer_free (&needed_space); |
463 | ||
c77a4478 UD |
464 | if (errno == 0 && errno_saved != 0) |
465 | __set_errno (errno_saved); | |
466 | ||
385b4cf4 | 467 | struct link_map **old_l_initfini = NULL; |
752a2a50 UD |
468 | if (map->l_initfini != NULL && map->l_type == lt_loaded) |
469 | { | |
470 | /* This object was previously loaded as a dependency and we have | |
471 | a separate l_initfini list. We don't need it anymore. */ | |
472 | assert (map->l_searchlist.r_list == NULL); | |
385b4cf4 | 473 | old_l_initfini = map->l_initfini; |
752a2a50 UD |
474 | } |
475 | ||
f68b86cc RM |
476 | /* Store the search list we built in the object. It will be used for |
477 | searches in the scope of this object. */ | |
385b4cf4 | 478 | struct link_map **l_initfini = |
80d9c5f0 | 479 | (struct link_map **) malloc ((2 * nlist + 1) |
2e93b4a4 | 480 | * sizeof (struct link_map *)); |
385b4cf4 | 481 | if (l_initfini == NULL) |
154d10bd UD |
482 | _dl_signal_error (ENOMEM, map->l_name, NULL, |
483 | N_("cannot allocate symbol search list")); | |
2e93b4a4 UD |
484 | |
485 | ||
385b4cf4 | 486 | map->l_searchlist.r_list = &l_initfini[nlist + 1]; |
be935610 | 487 | map->l_searchlist.r_nlist = nlist; |
f68b86cc | 488 | |
80d9c5f0 | 489 | for (nlist = 0, runp = known; runp; runp = runp->next) |
f68b86cc | 490 | { |
c4bb124a | 491 | if (__builtin_expect (trace_mode, 0) && runp->map->l_faked) |
ad9570d7 UD |
492 | /* This can happen when we trace the loading. */ |
493 | --map->l_searchlist.r_nlist; | |
494 | else | |
495 | map->l_searchlist.r_list[nlist++] = runp->map; | |
f68b86cc RM |
496 | |
497 | /* Now clear all the mark bits we set in the objects on the search list | |
498 | to avoid duplicates, so the next call starts fresh. */ | |
1522c368 | 499 | runp->map->l_reserved = 0; |
f68b86cc | 500 | } |
84384f5b | 501 | |
1fc07491 | 502 | if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK, 0) != 0 |
c0f62c56 | 503 | && map == GL(dl_ns)[LM_ID_BASE]._ns_loaded) |
32e6df36 UD |
504 | { |
505 | /* If we are to compute conflicts, we have to build local scope | |
506 | for each library, not just the ultimate loader. */ | |
507 | for (i = 0; i < nlist; ++i) | |
508 | { | |
509 | struct link_map *l = map->l_searchlist.r_list[i]; | |
510 | unsigned int j, cnt; | |
511 | ||
512 | /* The local scope has been already computed. */ | |
513 | if (l == map | |
514 | || (l->l_local_scope[0] | |
515 | && l->l_local_scope[0]->r_nlist) != 0) | |
516 | continue; | |
517 | ||
518 | if (l->l_info[AUXTAG] || l->l_info[FILTERTAG]) | |
519 | { | |
520 | /* As current DT_AUXILIARY/DT_FILTER implementation needs to be | |
521 | rewritten, no need to bother with prelinking the old | |
522 | implementation. */ | |
154d10bd | 523 | _dl_signal_error (EINVAL, l->l_name, NULL, N_("\ |
32e6df36 UD |
524 | Filters not supported with LD_TRACE_PRELINKING")); |
525 | } | |
526 | ||
385b4cf4 | 527 | cnt = _dl_build_local_scope (l_initfini, l); |
32e6df36 UD |
528 | assert (cnt <= nlist); |
529 | for (j = 0; j < cnt; j++) | |
4ad43b62 UD |
530 | { |
531 | l_initfini[j]->l_reserved = 0; | |
532 | if (j && __builtin_expect (l_initfini[j]->l_info[DT_SYMBOLIC] | |
533 | != NULL, 0)) | |
534 | l->l_symbolic_in_local_scope = true; | |
535 | } | |
32e6df36 UD |
536 | |
537 | l->l_local_scope[0] = | |
538 | (struct r_scope_elem *) malloc (sizeof (struct r_scope_elem) | |
539 | + (cnt | |
540 | * sizeof (struct link_map *))); | |
541 | if (l->l_local_scope[0] == NULL) | |
154d10bd UD |
542 | _dl_signal_error (ENOMEM, map->l_name, NULL, |
543 | N_("cannot allocate symbol search list")); | |
32e6df36 UD |
544 | l->l_local_scope[0]->r_nlist = cnt; |
545 | l->l_local_scope[0]->r_list = | |
546 | (struct link_map **) (l->l_local_scope[0] + 1); | |
385b4cf4 | 547 | memcpy (l->l_local_scope[0]->r_list, l_initfini, |
32e6df36 UD |
548 | cnt * sizeof (struct link_map *)); |
549 | } | |
550 | } | |
551 | ||
c4bb124a UD |
552 | /* Maybe we can remove some relocation dependencies now. */ |
553 | assert (map->l_searchlist.r_list[0] == map); | |
385b4cf4 UD |
554 | struct link_map_reldeps *l_reldeps = NULL; |
555 | if (map->l_reldeps != NULL) | |
c4bb124a | 556 | { |
385b4cf4 UD |
557 | for (i = 1; i < nlist; ++i) |
558 | map->l_searchlist.r_list[i]->l_reserved = 1; | |
c4bb124a | 559 | |
385b4cf4 UD |
560 | struct link_map **list = &map->l_reldeps->list[0]; |
561 | for (i = 0; i < map->l_reldeps->act; ++i) | |
562 | if (list[i]->l_reserved) | |
c4bb124a | 563 | { |
385b4cf4 | 564 | /* Need to allocate new array of relocation dependencies. */ |
385b4cf4 | 565 | l_reldeps = malloc (sizeof (*l_reldeps) |
968dad0a | 566 | + map->l_reldepsmax |
385b4cf4 UD |
567 | * sizeof (struct link_map *)); |
568 | if (l_reldeps == NULL) | |
569 | /* Bad luck, keep the reldeps duplicated between | |
570 | map->l_reldeps->list and map->l_initfini lists. */ | |
571 | ; | |
572 | else | |
573 | { | |
574 | unsigned int j = i; | |
575 | memcpy (&l_reldeps->list[0], &list[0], | |
576 | i * sizeof (struct link_map *)); | |
577 | for (i = i + 1; i < map->l_reldeps->act; ++i) | |
578 | if (!list[i]->l_reserved) | |
579 | l_reldeps->list[j++] = list[i]; | |
580 | l_reldeps->act = j; | |
581 | } | |
c4bb124a | 582 | } |
385b4cf4 UD |
583 | |
584 | for (i = 1; i < nlist; ++i) | |
585 | map->l_searchlist.r_list[i]->l_reserved = 0; | |
c4bb124a UD |
586 | } |
587 | ||
6b1e7d19 UD |
588 | /* Sort the initializer list to take dependencies into account. The binary |
589 | itself will always be initialize last. */ | |
385b4cf4 | 590 | memcpy (l_initfini, map->l_searchlist.r_list, |
2e93b4a4 | 591 | nlist * sizeof (struct link_map *)); |
c2c299fd AS |
592 | /* We can skip looking for the binary itself which is at the front of |
593 | the search list. */ | |
594 | _dl_sort_maps (&l_initfini[1], nlist - 1, NULL, false); | |
968dad0a | 595 | |
a8571d37 | 596 | /* Terminate the list of dependencies. */ |
385b4cf4 UD |
597 | l_initfini[nlist] = NULL; |
598 | atomic_write_barrier (); | |
599 | map->l_initfini = l_initfini; | |
0479b305 | 600 | map->l_free_initfini = 1; |
385b4cf4 UD |
601 | if (l_reldeps != NULL) |
602 | { | |
603 | atomic_write_barrier (); | |
604 | void *old_l_reldeps = map->l_reldeps; | |
605 | map->l_reldeps = l_reldeps; | |
606 | _dl_scope_free (old_l_reldeps); | |
607 | } | |
608 | if (old_l_initfini != NULL) | |
0479b305 | 609 | _dl_scope_free (old_l_initfini); |
f55ffe58 AS |
610 | |
611 | if (errno_reason) | |
2449ae7b FW |
612 | _dl_signal_exception (errno_reason == -1 ? 0 : errno_reason, |
613 | &exception, NULL); | |
efec1d0c | 614 | } |