Update.
[glibc.git] / elf / dl-deps.c
blob7842013c92974040bcc46d7e77ecac716c696362
1 /* Load the dependencies of a mapped object.
2 Copyright (C) 1996-2001, 2002 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <dlfcn.h>
22 #include <errno.h>
23 #include <libintl.h>
24 #include <stddef.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/param.h>
29 #include <ldsodefs.h>
31 #include <dl-dst.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. */
45 struct openaux_args
47 /* The arguments to openaux. */
48 struct link_map *map;
49 int trace_mode;
50 int open_mode;
51 const char *strtab;
52 const char *name;
54 /* The return value of openaux. */
55 struct link_map *aux;
58 static void
59 openaux (void *a)
61 struct openaux_args *args = (struct openaux_args *) a;
63 args->aux = INTUSE(_dl_map_object) (args->map, args->name, 0,
64 (args->map->l_type == lt_executable
65 ? lt_library : args->map->l_type),
66 args->trace_mode, args->open_mode);
69 static ptrdiff_t
70 internal_function
71 _dl_build_local_scope (struct link_map **list, struct link_map *map)
73 struct link_map **p = list;
74 struct link_map **q;
76 *p++ = map;
77 map->l_reserved = 1;
78 if (map->l_initfini)
79 for (q = map->l_initfini + 1; *q; ++q)
80 if (! (*q)->l_reserved)
81 p += _dl_build_local_scope (p, *q);
82 return p - list;
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.
90 struct list
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) \
100 ({ \
101 const char *__str = (str); \
102 const char *__result = __str; \
103 size_t __cnt = DL_DST_COUNT(__str, 0); \
105 if (__cnt != 0) \
107 char *__newp; \
109 /* DST must not appear in SUID/SGID programs. */ \
110 if (INTUSE(__libc_enable_secure)) \
111 INTUSE(_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), \
115 __cnt)); \
117 __result = INTUSE(_dl_dst_substitute) (l, __str, __newp, 0); \
119 if (*__result == '\0') \
121 /* The replacement for the DST is not known. We can't \
122 processed. */ \
123 if (fatal) \
124 INTUSE(_dl_signal_error) (0, __str, NULL, N_("\
125 empty dynamics string token substitution")); \
126 else \
128 /* This is for DT_AUXILIARY. */ \
129 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_LIBS, 0)) \
130 INTUSE(_dl_debug_printf) (N_("\
131 cannot load auxiliary `%s' because of empty dynamic string token " \
132 "substitution\n"), __str); \
133 continue; \
138 __result; })
141 void
142 internal_function
143 _dl_map_object_deps (struct link_map *map,
144 struct link_map **preloads, unsigned int npreloads,
145 int trace_mode, int open_mode)
147 struct list known[1 + npreloads + 1];
148 struct list *runp, *tail;
149 unsigned int nlist, i;
150 /* Object name. */
151 const char *name;
152 int errno_saved;
153 int errno_reason;
154 const char *errstring;
155 const char *objname;
157 auto inline void preload (struct link_map *map);
159 inline void preload (struct link_map *map)
161 known[nlist].done = 0;
162 known[nlist].map = map;
163 known[nlist].next = &known[nlist + 1];
165 ++nlist;
166 /* We use `l_reserved' as a mark bit to detect objects we have
167 already put in the search list and avoid adding duplicate
168 elements later in the list. */
169 map->l_reserved = 1;
172 /* No loaded object so far. */
173 nlist = 0;
175 /* First load MAP itself. */
176 preload (map);
178 /* Add the preloaded items after MAP but before any of its dependencies. */
179 for (i = 0; i < npreloads; ++i)
180 preload (preloads[i]);
182 /* Terminate the lists. */
183 known[nlist - 1].next = NULL;
185 /* Pointer to last unique object. */
186 tail = &known[nlist - 1];
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. */
198 errno_saved = errno;
199 errno_reason = 0;
200 errstring = NULL;
201 errno = 0;
202 name = NULL;
203 for (runp = known; runp; )
205 struct link_map *l = runp->map;
206 struct link_map **needed = NULL;
207 unsigned int nneeded = 0;
209 /* Unless otherwise stated, this object is handled. */
210 runp->done = 1;
212 /* Allocate a temporary record to contain the references to the
213 dependencies of this object. */
214 if (l->l_searchlist.r_list == NULL && l->l_initfini == NULL
215 && l != map && l->l_ldnum > 0)
216 needed = (struct link_map **) alloca (l->l_ldnum
217 * sizeof (struct link_map *));
219 if (l->l_info[DT_NEEDED] || l->l_info[AUXTAG] || l->l_info[FILTERTAG])
221 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
222 struct openaux_args args;
223 struct list *orig;
224 const ElfW(Dyn) *d;
226 args.strtab = strtab;
227 args.map = l;
228 args.trace_mode = trace_mode;
229 args.open_mode = open_mode;
230 orig = runp;
232 for (d = l->l_ld; d->d_tag != DT_NULL; ++d)
233 if (__builtin_expect (d->d_tag, DT_NEEDED) == DT_NEEDED)
235 /* Map in the needed object. */
236 struct link_map *dep;
237 int err;
239 /* Recognize DSTs. */
240 name = expand_dst (l, strtab + d->d_un.d_val, 0);
241 /* Store the tag in the argument structure. */
242 args.name = name;
244 err = INTUSE(_dl_catch_error) (&objname, &errstring, openaux,
245 &args);
246 if (__builtin_expect (errstring != NULL, 0))
248 if (err)
249 errno_reason = err;
250 else
251 errno_reason = -1;
252 goto out;
254 else
255 dep = args.aux;
257 /* Skip those are not dlopened if we are dlopened. */
258 if (map->l_type == lt_loaded && dep->l_type != lt_loaded)
259 continue;
261 if (! dep->l_reserved)
263 /* Allocate new entry. */
264 struct list *newp;
266 newp = alloca (sizeof (struct list));
268 /* Append DEP to the list. */
269 newp->map = dep;
270 newp->done = 0;
271 newp->next = NULL;
272 tail->next = newp;
273 tail = newp;
274 ++nlist;
275 /* Set the mark bit that says it's already in the list. */
276 dep->l_reserved = 1;
279 /* Remember this dependency. */
280 if (needed != NULL)
281 needed[nneeded++] = dep;
283 else if (d->d_tag == DT_AUXILIARY || d->d_tag == DT_FILTER)
285 struct list *newp;
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. */
291 args.name = name;
293 if (d->d_tag == DT_AUXILIARY)
295 int err;
297 /* Say that we are about to load an auxiliary library. */
298 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_LIBS,
300 INTUSE(_dl_debug_printf) ("load auxiliary object=%s"
301 " requested by file=%s\n",
302 name,
303 l->l_name[0]
304 ? l->l_name : rtld_progname);
306 /* We must be prepared that the addressed shared
307 object is not available. */
308 err = INTUSE(_dl_catch_error) (&objname, &errstring,
309 openaux, &args);
310 if (__builtin_expect (errstring != NULL, 0))
312 /* We are not interested in the error message. */
313 assert (errstring != NULL);
314 if (errstring != INTUSE(_dl_out_of_memory))
315 free ((char *) errstring);
317 /* Simply ignore this error and continue the work. */
318 continue;
321 else
323 int err;
325 /* Say that we are about to load an auxiliary library. */
326 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_LIBS,
328 INTUSE(_dl_debug_printf) ("load filtered object=%s"
329 " requested by file=%s\n",
330 name,
331 l->l_name[0]
332 ? l->l_name : rtld_progname);
334 /* For filter objects the dependency must be available. */
335 err = INTUSE(_dl_catch_error) (&objname, &errstring,
336 openaux, &args);
337 if (__builtin_expect (errstring != NULL, 0))
339 if (err)
340 errno_reason = err;
341 else
342 errno_reason = -1;
343 goto out;
347 /* The auxiliary object is actually available.
348 Incorporate the map in all the lists. */
350 /* Allocate new entry. This always has to be done. */
351 newp = alloca (sizeof (struct list));
353 /* We want to insert the new map before the current one,
354 but we have no back links. So we copy the contents of
355 the current entry over. Note that ORIG and NEWP now
356 have switched their meanings. */
357 memcpy (newp, orig, sizeof (*newp));
359 /* Initialize new entry. */
360 orig->done = 0;
361 orig->map = args.aux;
363 /* Remember this dependency. */
364 if (needed != NULL)
365 needed[nneeded++] = args.aux;
367 /* We must handle two situations here: the map is new,
368 so we must add it in all three lists. If the map
369 is already known, we have two further possibilities:
370 - if the object is before the current map in the
371 search list, we do nothing. It is already found
372 early
373 - if the object is after the current one, we must
374 move it just before the current map to make sure
375 the symbols are found early enough
377 if (args.aux->l_reserved)
379 /* The object is already somewhere in the list.
380 Locate it first. */
381 struct list *late;
383 /* This object is already in the search list we
384 are building. Don't add a duplicate pointer.
385 Just added by _dl_map_object. */
386 for (late = newp; late->next != NULL; late = late->next)
387 if (late->next->map == args.aux)
388 break;
390 if (late->next != NULL)
392 /* The object is somewhere behind the current
393 position in the search path. We have to
394 move it to this earlier position. */
395 orig->next = newp;
397 /* Now remove the later entry from the list
398 and adjust the tail pointer. */
399 if (tail == late->next)
400 tail = late;
401 late->next = late->next->next;
403 /* We must move the object earlier in the chain. */
404 if (args.aux->l_prev != NULL)
405 args.aux->l_prev->l_next = args.aux->l_next;
406 if (args.aux->l_next != NULL)
407 args.aux->l_next->l_prev = args.aux->l_prev;
409 args.aux->l_prev = newp->map->l_prev;
410 newp->map->l_prev = args.aux;
411 if (args.aux->l_prev != NULL)
412 args.aux->l_prev->l_next = args.aux;
413 args.aux->l_next = newp->map;
415 else
417 /* The object must be somewhere earlier in the
418 list. Undo to the current list element what
419 we did above. */
420 memcpy (orig, newp, sizeof (*newp));
421 continue;
424 else
426 /* This is easy. We just add the symbol right here. */
427 orig->next = newp;
428 ++nlist;
429 /* Set the mark bit that says it's already in the list. */
430 args.aux->l_reserved = 1;
432 /* The only problem is that in the double linked
433 list of all objects we don't have this new
434 object at the correct place. Correct this here. */
435 if (args.aux->l_prev)
436 args.aux->l_prev->l_next = args.aux->l_next;
437 if (args.aux->l_next)
438 args.aux->l_next->l_prev = args.aux->l_prev;
440 args.aux->l_prev = newp->map->l_prev;
441 newp->map->l_prev = args.aux;
442 if (args.aux->l_prev != NULL)
443 args.aux->l_prev->l_next = args.aux;
444 args.aux->l_next = newp->map;
447 /* Move the tail pointer if necessary. */
448 if (orig == tail)
449 tail = newp;
451 /* Move on the insert point. */
452 orig = newp;
456 /* Terminate the list of dependencies and store the array address. */
457 if (needed != NULL)
459 needed[nneeded++] = NULL;
461 l->l_initfini = (struct link_map **)
462 malloc ((nneeded + 1) * sizeof needed[0]);
463 if (l->l_initfini == NULL)
464 INTUSE(_dl_signal_error) (ENOMEM, map->l_name, NULL,
465 N_("cannot allocate dependency list"));
466 l->l_initfini[0] = l;
467 memcpy (&l->l_initfini[1], needed, nneeded * sizeof needed[0]);
470 /* If we have no auxiliary objects just go on to the next map. */
471 if (runp->done)
473 runp = runp->next;
474 while (runp != NULL && runp->done);
477 out:
478 if (errno == 0 && errno_saved != 0)
479 __set_errno (errno_saved);
481 if (map->l_initfini != NULL && map->l_type == lt_loaded)
483 /* This object was previously loaded as a dependency and we have
484 a separate l_initfini list. We don't need it anymore. */
485 assert (map->l_searchlist.r_list == NULL);
486 free (map->l_initfini);
489 /* Store the search list we built in the object. It will be used for
490 searches in the scope of this object. */
491 map->l_initfini =
492 (struct link_map **) malloc ((2 * nlist + 1)
493 * sizeof (struct link_map *));
494 if (map->l_initfini == NULL)
495 INTUSE(_dl_signal_error) (ENOMEM, map->l_name, NULL,
496 N_("cannot allocate symbol search list"));
499 map->l_searchlist.r_list = &map->l_initfini[nlist + 1];
500 map->l_searchlist.r_nlist = nlist;
502 for (nlist = 0, runp = known; runp; runp = runp->next)
504 if (__builtin_expect (trace_mode, 0) && runp->map->l_faked)
505 /* This can happen when we trace the loading. */
506 --map->l_searchlist.r_nlist;
507 else
508 map->l_searchlist.r_list[nlist++] = runp->map;
510 /* Now clear all the mark bits we set in the objects on the search list
511 to avoid duplicates, so the next call starts fresh. */
512 runp->map->l_reserved = 0;
515 if (__builtin_expect(GL(dl_debug_mask) & DL_DEBUG_PRELINK, 0) != 0
516 && map == GL(dl_loaded))
518 /* If we are to compute conflicts, we have to build local scope
519 for each library, not just the ultimate loader. */
520 for (i = 0; i < nlist; ++i)
522 struct link_map *l = map->l_searchlist.r_list[i];
523 unsigned int j, cnt;
525 /* The local scope has been already computed. */
526 if (l == map
527 || (l->l_local_scope[0]
528 && l->l_local_scope[0]->r_nlist) != 0)
529 continue;
531 if (l->l_info[AUXTAG] || l->l_info[FILTERTAG])
533 /* As current DT_AUXILIARY/DT_FILTER implementation needs to be
534 rewritten, no need to bother with prelinking the old
535 implementation. */
536 INTUSE(_dl_signal_error) (EINVAL, l->l_name, NULL, N_("\
537 Filters not supported with LD_TRACE_PRELINKING"));
540 cnt = _dl_build_local_scope (map->l_initfini, l);
541 assert (cnt <= nlist);
542 for (j = 0; j < cnt; j++)
543 map->l_initfini[j]->l_reserved = 0;
545 l->l_local_scope[0] =
546 (struct r_scope_elem *) malloc (sizeof (struct r_scope_elem)
547 + (cnt
548 * sizeof (struct link_map *)));
549 if (l->l_local_scope[0] == NULL)
550 INTUSE(_dl_signal_error) (ENOMEM, map->l_name, NULL,
551 N_("cannot allocate symbol search list"));
552 l->l_local_scope[0]->r_nlist = cnt;
553 l->l_local_scope[0]->r_list =
554 (struct link_map **) (l->l_local_scope[0] + 1);
555 memcpy (l->l_local_scope[0]->r_list, map->l_initfini,
556 cnt * sizeof (struct link_map *));
560 /* Maybe we can remove some relocation dependencies now. */
561 assert (map->l_searchlist.r_list[0] == map);
562 for (i = 0; i < map->l_reldepsact; ++i)
564 unsigned int j;
566 for (j = 1; j < nlist; ++j)
567 if (map->l_searchlist.r_list[j] == map->l_reldeps[i])
569 /* A direct or transitive dependency is also on the list
570 of relocation dependencies. Remove the latter. */
571 --map->l_reldeps[i]->l_opencount;
573 for (j = i + 1; j < map->l_reldepsact; ++j)
574 map->l_reldeps[j - 1] = map->l_reldeps[j];
576 --map->l_reldepsact;
578 /* Account for the '++i' performed by the 'for'. */
579 --i;
580 break;
584 /* Now determine the order in which the initialization has to happen. */
585 memcpy (map->l_initfini, map->l_searchlist.r_list,
586 nlist * sizeof (struct link_map *));
587 /* We can skip looking for the binary itself which is at the front
588 of the search list. Look through the list backward so that circular
589 dependencies are not changing the order. */
590 for (i = 1; i < nlist; ++i)
592 struct link_map *l = map->l_searchlist.r_list[i];
593 unsigned int j;
594 unsigned int k;
596 /* Find the place in the initfini list where the map is currently
597 located. */
598 for (j = 1; map->l_initfini[j] != l; ++j)
601 /* Find all object for which the current one is a dependency and
602 move the found object (if necessary) in front. */
603 for (k = j + 1; k < nlist; ++k)
605 struct link_map **runp;
607 runp = map->l_initfini[k]->l_initfini;
608 if (runp != NULL)
610 while (*runp != NULL)
611 if (__builtin_expect (*runp++ == l, 0))
613 struct link_map *here = map->l_initfini[k];
615 /* Move it now. */
616 memmove (&map->l_initfini[j] + 1,
617 &map->l_initfini[j],
618 (k - j) * sizeof (struct link_map *));
619 map->l_initfini[j] = here;
621 /* Don't insert further matches before the last
622 entry moved to the front. */
623 ++j;
625 break;
630 /* Terminate the list of dependencies. */
631 map->l_initfini[nlist] = NULL;
633 if (errno_reason)
634 INTUSE(_dl_signal_error) (errno_reason == -1 ? 0 : errno_reason, objname,
635 NULL, errstring);
637 INTDEF (_dl_map_object_deps)