1 /* Load the dependencies of a mapped object.
2 Copyright (C) 1996-2017 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 <http://www.gnu.org/licenses/>. */
28 #include <sys/param.h>
33 /* Whether an shared object references one or more auxiliary objects
34 is signaled by the AUXTAG entry in l_info. */
35 #define AUXTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
36 + DT_EXTRATAGIDX (DT_AUXILIARY))
37 /* Whether an shared object references one or more auxiliary objects
38 is signaled by the AUXTAG entry in l_info. */
39 #define FILTERTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
40 + DT_EXTRATAGIDX (DT_FILTER))
43 /* When loading auxiliary objects we must ignore errors. It's ok if
44 an object is missing. */
47 /* The arguments to openaux. */
54 /* The return value of openaux. */
61 struct openaux_args *args = (struct openaux_args *) a;
63 args->aux = _dl_map_object (args->map, args->name,
64 (args->map->l_type == lt_executable
65 ? lt_library : args->map->l_type),
66 args->trace_mode, args->open_mode,
71 _dl_build_local_scope (struct link_map **list, struct link_map *map)
73 struct link_map **p = list;
79 for (q = map->l_initfini + 1; *q; ++q)
80 if (! (*q)->l_reserved)
81 p += _dl_build_local_scope (p, *q);
86 /* We use a very special kind of list to track the path
87 through the list of loaded shared objects. We have to
88 produce a flat list with unique members of all involved objects.
92 int done; /* Nonzero if this map was processed. */
93 struct link_map *map; /* The data. */
94 struct list *next; /* Elements for normal list. */
98 /* Macro to expand DST. It is an macro since we use `alloca'. */
99 #define expand_dst(l, str, fatal) \
101 const char *__str = (str); \
102 const char *__result = __str; \
103 size_t __dst_cnt = DL_DST_COUNT (__str, 0); \
105 if (__dst_cnt != 0) \
109 /* DST must not appear in SUID/SGID programs. */ \
110 if (__libc_enable_secure) \
111 _dl_signal_error (0, __str, NULL, N_("\
112 DST not allowed in SUID/SGID programs")); \
114 __newp = (char *) alloca (DL_DST_REQUIRED (l, __str, strlen (__str), \
117 __result = _dl_dst_substitute (l, __str, __newp, 0); \
119 if (*__result == '\0') \
121 /* The replacement for the DST is not known. We can't \
124 _dl_signal_error (0, __str, NULL, N_("\
125 empty dynamic string token substitution")); \
128 /* This is for DT_AUXILIARY. */ \
129 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS)) \
130 _dl_debug_printf (N_("\
131 cannot load auxiliary `%s' because of empty dynamic string token " \
132 "substitution\n"), __str); \
141 preload (struct list *known, unsigned int *nlist, struct link_map *map)
143 known[*nlist].done = 0;
144 known[*nlist].map = map;
145 known[*nlist].next = &known[*nlist + 1];
148 /* We use `l_reserved' as a mark bit to detect objects we have
149 already put in the search list and avoid adding duplicate
150 elements later in the list. */
155 _dl_map_object_deps (struct link_map *map,
156 struct link_map **preloads, unsigned int npreloads,
157 int trace_mode, int open_mode)
159 struct list *known = __alloca (sizeof *known * (1 + npreloads + 1));
160 struct list *runp, *tail;
161 unsigned int nlist, i;
166 struct dl_exception exception;
168 /* No loaded object so far. */
171 /* First load MAP itself. */
172 preload (known, &nlist, map);
174 /* Add the preloaded items after MAP but before any of its dependencies. */
175 for (i = 0; i < npreloads; ++i)
176 preload (known, &nlist, preloads[i]);
178 /* Terminate the lists. */
179 known[nlist - 1].next = NULL;
181 /* Pointer to last unique object. */
182 tail = &known[nlist - 1];
184 /* No alloca'd space yet. */
185 struct link_map **needed_space = NULL;
186 size_t needed_space_bytes = 0;
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.
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. */
202 for (runp = known; runp; )
204 struct link_map *l = runp->map;
205 struct link_map **needed = NULL;
206 unsigned int nneeded = 0;
208 /* Unless otherwise stated, this object is handled. */
211 /* Allocate a temporary record to contain the references to the
212 dependencies of this object. */
213 if (l->l_searchlist.r_list == NULL && l->l_initfini == NULL
214 && l != map && l->l_ldnum > 0)
216 size_t new_size = l->l_ldnum * sizeof (struct link_map *);
218 if (new_size > needed_space_bytes)
220 = extend_alloca (needed_space, needed_space_bytes, new_size);
222 needed = needed_space;
225 if (l->l_info[DT_NEEDED] || l->l_info[AUXTAG] || l->l_info[FILTERTAG])
227 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
228 struct openaux_args args;
232 args.strtab = strtab;
234 args.trace_mode = trace_mode;
235 args.open_mode = open_mode;
238 for (d = l->l_ld; d->d_tag != DT_NULL; ++d)
239 if (__builtin_expect (d->d_tag, DT_NEEDED) == DT_NEEDED)
241 /* Map in the needed object. */
242 struct link_map *dep;
244 /* Recognize DSTs. */
245 name = expand_dst (l, strtab + d->d_un.d_val, 0);
246 /* Store the tag in the argument structure. */
249 int err = _dl_catch_exception (&exception, openaux, &args);
250 if (__glibc_unlikely (exception.errstring != NULL))
261 if (! dep->l_reserved)
263 /* Allocate new entry. */
266 newp = alloca (sizeof (struct list));
268 /* Append DEP to the list. */
275 /* Set the mark bit that says it's already in the list. */
279 /* Remember this dependency. */
281 needed[nneeded++] = dep;
283 else if (d->d_tag == DT_AUXILIARY || d->d_tag == DT_FILTER)
287 /* Recognize DSTs. */
288 name = expand_dst (l, strtab + d->d_un.d_val,
289 d->d_tag == DT_AUXILIARY);
290 /* Store the tag in the argument structure. */
293 /* Say that we are about to load an auxiliary library. */
294 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS,
296 _dl_debug_printf ("load auxiliary object=%s"
297 " requested by file=%s\n",
299 DSO_FILENAME (l->l_name));
301 /* We must be prepared that the addressed shared
302 object is not available. For filter objects the dependency
303 must be available. */
304 int err = _dl_catch_exception (&exception, openaux, &args);
305 if (__glibc_unlikely (exception.errstring != NULL))
307 if (d->d_tag == DT_AUXILIARY)
309 /* We are not interested in the error message. */
310 _dl_exception_free (&exception);
311 /* Simply ignore this error and continue the work. */
324 /* The auxiliary object is actually available.
325 Incorporate the map in all the lists. */
327 /* Allocate new entry. This always has to be done. */
328 newp = alloca (sizeof (struct list));
330 /* We want to insert the new map before the current one,
331 but we have no back links. So we copy the contents of
332 the current entry over. Note that ORIG and NEWP now
333 have switched their meanings. */
334 memcpy (newp, orig, sizeof (*newp));
336 /* Initialize new entry. */
338 orig->map = args.aux;
340 /* Remember this dependency. */
342 needed[nneeded++] = args.aux;
344 /* We must handle two situations here: the map is new,
345 so we must add it in all three lists. If the map
346 is already known, we have two further possibilities:
347 - if the object is before the current map in the
348 search list, we do nothing. It is already found
350 - if the object is after the current one, we must
351 move it just before the current map to make sure
352 the symbols are found early enough
354 if (args.aux->l_reserved)
356 /* The object is already somewhere in the list.
360 /* This object is already in the search list we
361 are building. Don't add a duplicate pointer.
362 Just added by _dl_map_object. */
363 for (late = newp; late->next != NULL; late = late->next)
364 if (late->next->map == args.aux)
367 if (late->next != NULL)
369 /* The object is somewhere behind the current
370 position in the search path. We have to
371 move it to this earlier position. */
374 /* Now remove the later entry from the list
375 and adjust the tail pointer. */
376 if (tail == late->next)
378 late->next = late->next->next;
380 /* We must move the object earlier in the chain. */
381 if (args.aux->l_prev != NULL)
382 args.aux->l_prev->l_next = args.aux->l_next;
383 if (args.aux->l_next != NULL)
384 args.aux->l_next->l_prev = args.aux->l_prev;
386 args.aux->l_prev = newp->map->l_prev;
387 newp->map->l_prev = args.aux;
388 if (args.aux->l_prev != NULL)
389 args.aux->l_prev->l_next = args.aux;
390 args.aux->l_next = newp->map;
394 /* The object must be somewhere earlier in the
395 list. Undo to the current list element what
397 memcpy (orig, newp, sizeof (*newp));
403 /* This is easy. We just add the symbol right here. */
406 /* Set the mark bit that says it's already in the list. */
407 args.aux->l_reserved = 1;
409 /* The only problem is that in the double linked
410 list of all objects we don't have this new
411 object at the correct place. Correct this here. */
412 if (args.aux->l_prev)
413 args.aux->l_prev->l_next = args.aux->l_next;
414 if (args.aux->l_next)
415 args.aux->l_next->l_prev = args.aux->l_prev;
417 args.aux->l_prev = newp->map->l_prev;
418 newp->map->l_prev = args.aux;
419 if (args.aux->l_prev != NULL)
420 args.aux->l_prev->l_next = args.aux;
421 args.aux->l_next = newp->map;
424 /* Move the tail pointer if necessary. */
428 /* Move on the insert point. */
433 /* Terminate the list of dependencies and store the array address. */
436 needed[nneeded++] = NULL;
438 struct link_map **l_initfini = (struct link_map **)
439 malloc ((2 * nneeded + 1) * sizeof needed[0]);
440 if (l_initfini == NULL)
441 _dl_signal_error (ENOMEM, map->l_name, NULL,
442 N_("cannot allocate dependency list"));
444 memcpy (&l_initfini[1], needed, nneeded * sizeof needed[0]);
445 memcpy (&l_initfini[nneeded + 1], l_initfini,
446 nneeded * sizeof needed[0]);
447 atomic_write_barrier ();
448 l->l_initfini = l_initfini;
449 l->l_free_initfini = 1;
452 /* If we have no auxiliary objects just go on to the next map. */
456 while (runp != NULL && runp->done);
460 if (errno == 0 && errno_saved != 0)
461 __set_errno (errno_saved);
463 struct link_map **old_l_initfini = NULL;
464 if (map->l_initfini != NULL && map->l_type == lt_loaded)
466 /* This object was previously loaded as a dependency and we have
467 a separate l_initfini list. We don't need it anymore. */
468 assert (map->l_searchlist.r_list == NULL);
469 old_l_initfini = map->l_initfini;
472 /* Store the search list we built in the object. It will be used for
473 searches in the scope of this object. */
474 struct link_map **l_initfini =
475 (struct link_map **) malloc ((2 * nlist + 1)
476 * sizeof (struct link_map *));
477 if (l_initfini == NULL)
478 _dl_signal_error (ENOMEM, map->l_name, NULL,
479 N_("cannot allocate symbol search list"));
482 map->l_searchlist.r_list = &l_initfini[nlist + 1];
483 map->l_searchlist.r_nlist = nlist;
485 for (nlist = 0, runp = known; runp; runp = runp->next)
487 if (__builtin_expect (trace_mode, 0) && runp->map->l_faked)
488 /* This can happen when we trace the loading. */
489 --map->l_searchlist.r_nlist;
491 map->l_searchlist.r_list[nlist++] = runp->map;
493 /* Now clear all the mark bits we set in the objects on the search list
494 to avoid duplicates, so the next call starts fresh. */
495 runp->map->l_reserved = 0;
498 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK, 0) != 0
499 && map == GL(dl_ns)[LM_ID_BASE]._ns_loaded)
501 /* If we are to compute conflicts, we have to build local scope
502 for each library, not just the ultimate loader. */
503 for (i = 0; i < nlist; ++i)
505 struct link_map *l = map->l_searchlist.r_list[i];
508 /* The local scope has been already computed. */
510 || (l->l_local_scope[0]
511 && l->l_local_scope[0]->r_nlist) != 0)
514 if (l->l_info[AUXTAG] || l->l_info[FILTERTAG])
516 /* As current DT_AUXILIARY/DT_FILTER implementation needs to be
517 rewritten, no need to bother with prelinking the old
519 _dl_signal_error (EINVAL, l->l_name, NULL, N_("\
520 Filters not supported with LD_TRACE_PRELINKING"));
523 cnt = _dl_build_local_scope (l_initfini, l);
524 assert (cnt <= nlist);
525 for (j = 0; j < cnt; j++)
527 l_initfini[j]->l_reserved = 0;
528 if (j && __builtin_expect (l_initfini[j]->l_info[DT_SYMBOLIC]
530 l->l_symbolic_in_local_scope = true;
533 l->l_local_scope[0] =
534 (struct r_scope_elem *) malloc (sizeof (struct r_scope_elem)
536 * sizeof (struct link_map *)));
537 if (l->l_local_scope[0] == NULL)
538 _dl_signal_error (ENOMEM, map->l_name, NULL,
539 N_("cannot allocate symbol search list"));
540 l->l_local_scope[0]->r_nlist = cnt;
541 l->l_local_scope[0]->r_list =
542 (struct link_map **) (l->l_local_scope[0] + 1);
543 memcpy (l->l_local_scope[0]->r_list, l_initfini,
544 cnt * sizeof (struct link_map *));
548 /* Maybe we can remove some relocation dependencies now. */
549 assert (map->l_searchlist.r_list[0] == map);
550 struct link_map_reldeps *l_reldeps = NULL;
551 if (map->l_reldeps != NULL)
553 for (i = 1; i < nlist; ++i)
554 map->l_searchlist.r_list[i]->l_reserved = 1;
556 struct link_map **list = &map->l_reldeps->list[0];
557 for (i = 0; i < map->l_reldeps->act; ++i)
558 if (list[i]->l_reserved)
560 /* Need to allocate new array of relocation dependencies. */
561 l_reldeps = malloc (sizeof (*l_reldeps)
563 * sizeof (struct link_map *));
564 if (l_reldeps == NULL)
565 /* Bad luck, keep the reldeps duplicated between
566 map->l_reldeps->list and map->l_initfini lists. */
571 memcpy (&l_reldeps->list[0], &list[0],
572 i * sizeof (struct link_map *));
573 for (i = i + 1; i < map->l_reldeps->act; ++i)
574 if (!list[i]->l_reserved)
575 l_reldeps->list[j++] = list[i];
580 for (i = 1; i < nlist; ++i)
581 map->l_searchlist.r_list[i]->l_reserved = 0;
584 /* Sort the initializer list to take dependencies into account. The binary
585 itself will always be initialize last. */
586 memcpy (l_initfini, map->l_searchlist.r_list,
587 nlist * sizeof (struct link_map *));
588 /* We can skip looking for the binary itself which is at the front of
590 _dl_sort_maps (&l_initfini[1], nlist - 1, NULL, false);
592 /* Terminate the list of dependencies. */
593 l_initfini[nlist] = NULL;
594 atomic_write_barrier ();
595 map->l_initfini = l_initfini;
596 map->l_free_initfini = 1;
597 if (l_reldeps != NULL)
599 atomic_write_barrier ();
600 void *old_l_reldeps = map->l_reldeps;
601 map->l_reldeps = l_reldeps;
602 _dl_scope_free (old_l_reldeps);
604 if (old_l_initfini != NULL)
605 _dl_scope_free (old_l_initfini);
608 _dl_signal_exception (errno_reason == -1 ? 0 : errno_reason,