]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/dl-deps.c
Update.
[thirdparty/glibc.git] / elf / dl-deps.c
CommitLineData
efec1d0c 1/* Load the dependencies of a mapped object.
d9cb1a7d 2 Copyright (C) 1996, 1997, 1998, 1999, 2000 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
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
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
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
efec1d0c 19
dc5efe83 20#include <assert.h>
efec1d0c 21#include <dlfcn.h>
a853022c 22#include <errno.h>
8e17ea58 23#include <libintl.h>
efec1d0c 24#include <stdlib.h>
ca34d7a7 25#include <string.h>
06535ae9 26#include <unistd.h>
dc5efe83 27#include <sys/param.h>
a42195db 28#include <ldsodefs.h>
a853022c 29
dc5efe83 30#include <dl-dst.h>
1522c368
UD
31
32/* Whether an shared object references one or more auxiliary objects
33 is signaled by the AUXTAG entry in l_info. */
b0982c4a 34#define AUXTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
1522c368 35 + DT_EXTRATAGIDX (DT_AUXILIARY))
f41c8091
UD
36/* Whether an shared object references one or more auxiliary objects
37 is signaled by the AUXTAG entry in l_info. */
b0982c4a 38#define FILTERTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
f41c8091 39 + DT_EXTRATAGIDX (DT_FILTER))
1522c368 40
e3e35cfc
UD
41/* This is zero at program start to signal that the global scope map is
42 allocated by rtld. Later it keeps the size of the map. It might be
43 reset if in _dl_close if the last global object is removed. */
44size_t _dl_global_scope_alloc;
45
dc5efe83 46extern size_t _dl_platformlen;
1522c368
UD
47
48/* When loading auxiliary objects we must ignore errors. It's ok if
49 an object is missing. */
993b3242 50struct openaux_args
1522c368
UD
51 {
52 /* The arguments to openaux. */
53 struct link_map *map;
54 int trace_mode;
55 const char *strtab;
dc5efe83 56 const char *name;
993b3242 57
1522c368
UD
58 /* The return value of openaux. */
59 struct link_map *aux;
60 };
993b3242
UD
61
62static void
63openaux (void *a)
64{
65 struct openaux_args *args = (struct openaux_args *) a;
66
dc5efe83 67 args->aux = _dl_map_object (args->map, args->name, 0,
993b3242
UD
68 (args->map->l_type == lt_executable
69 ? lt_library : args->map->l_type),
bf8b3e74 70 args->trace_mode, 0);
993b3242
UD
71}
72
1522c368
UD
73
74
0a54e401 75/* We use a very special kind of list to track the two kinds paths
1522c368
UD
76 through the list of loaded shared objects. We have to
77
1522c368
UD
78 - produce a flat list with unique members of all involved objects
79
80 - produce a flat list of all shared objects.
81*/
82struct list
83 {
84 int done; /* Nonzero if this map was processed. */
85 struct link_map *map; /* The data. */
86
87 struct list *unique; /* Elements for normal list. */
88 struct list *dup; /* Elements in complete list. */
89 };
90
91
dc5efe83
UD
92/* Macro to expand DST. It is an macro since we use `alloca'. */
93#define expand_dst(l, str, fatal) \
94 ({ \
95 const char *__str = (str); \
96 const char *__result = __str; \
97 size_t __cnt = DL_DST_COUNT(__str, 0); \
98 \
99 if (__cnt != 0) \
100 { \
06535ae9
UD
101 char *__newp; \
102 \
103 /* DST must not appear in SUID/SGID programs. */ \
104 if (__libc_enable_secure) \
105 _dl_signal_error (0, __str, \
8e17ea58 106 N_("DST not allowed in SUID/SGID programs")); \
06535ae9
UD
107 \
108 __newp = (char *) alloca (DL_DST_REQUIRED (l, __str, strlen (__str), \
109 __cnt)); \
dc5efe83
UD
110 \
111 __result = DL_DST_SUBSTITUTE (l, __str, __newp, 0); \
112 \
113 if (*__result == '\0') \
114 { \
115 /* The replacement for the DST is not known. We can't \
116 processed. */ \
117 if (fatal) \
8e17ea58
UD
118 _dl_signal_error (0, __str, N_("\
119empty dynamics string token substitution")); \
dc5efe83
UD
120 else \
121 { \
122 /* This is for DT_AUXILIARY. */ \
466a0ec9 123 if (__builtin_expect (_dl_debug_libs, 0)) \
dc5efe83
UD
124 _dl_debug_message (1, "cannot load auxiliary `", __str, \
125 "' because of empty dynamic string" \
126 " token substitution\n", NULL); \
127 continue; \
128 } \
129 } \
130 } \
131 \
132 __result; })
133
134
d9cb1a7d 135void
d0fc4041 136internal_function
2064087b 137_dl_map_object_deps (struct link_map *map,
46ec036d 138 struct link_map **preloads, unsigned int npreloads,
d9cb1a7d 139 int trace_mode)
efec1d0c 140{
1522c368 141 struct list known[1 + npreloads + 1];
26b4d766 142 struct list *runp, *utail, *dtail;
1522c368
UD
143 unsigned int nlist, nduplist, i;
144
df4ef2ab
UD
145 inline void preload (struct link_map *map)
146 {
1522c368
UD
147 known[nlist].done = 0;
148 known[nlist].map = map;
149
150 known[nlist].unique = &known[nlist + 1];
151 known[nlist].dup = &known[nlist + 1];
efec1d0c 152
1522c368 153 ++nlist;
df4ef2ab
UD
154 /* We use `l_reserved' as a mark bit to detect objects we have
155 already put in the search list and avoid adding duplicate
156 elements later in the list. */
157 map->l_reserved = 1;
158 }
2064087b 159
1522c368
UD
160 /* No loaded object so far. */
161 nlist = 0;
2064087b 162
1522c368 163 /* First load MAP itself. */
df4ef2ab
UD
164 preload (map);
165
166 /* Add the preloaded items after MAP but before any of its dependencies. */
167 for (i = 0; i < npreloads; ++i)
168 preload (preloads[i]);
169
8a523922 170 /* Terminate the lists. */
1522c368
UD
171 known[nlist - 1].unique = NULL;
172 known[nlist - 1].dup = NULL;
173
1522c368
UD
174 /* Pointer to last unique object. */
175 utail = &known[nlist - 1];
176 /* Pointer to last loaded object. */
177 dtail = &known[nlist - 1];
f68b86cc 178
84384f5b
UD
179 /* Until now we have the same number of libraries in the normal and
180 the list with duplicates. */
181 nduplist = nlist;
efec1d0c 182
1522c368
UD
183 /* Process each element of the search list, loading each of its
184 auxiliary objects and immediate dependencies. Auxiliary objects
185 will be added in the list before the object itself and
186 dependencies will be appended to the list as we step through it.
187 This produces a flat, ordered list that represents a
188 breadth-first search of the dependency tree.
189
190 The whole process is complicated by the fact that we better
191 should use alloca for the temporary list elements. But using
192 alloca means we cannot use recursive function calls. */
193 for (runp = known; runp; )
efec1d0c 194 {
1522c368 195 struct link_map *l = runp->map;
dacc8ffa
UD
196 struct link_map **needed = NULL;
197 unsigned int nneeded = 0;
198
199 /* Unless otherwise stated, this object is handled. */
200 runp->done = 1;
201
202 /* Allocate a temporary record to contain the references to the
203 dependencies of this object. */
07a3d63e
UD
204 if (l->l_searchlist.r_list == NULL && l->l_initfini == NULL
205 && l != map && l->l_ldnum > 0)
dacc8ffa
UD
206 needed = (struct link_map **) alloca (l->l_ldnum
207 * sizeof (struct link_map *));
f68b86cc 208
8193034b 209 if (l->l_info[DT_NEEDED] || l->l_info[AUXTAG] || l->l_info[FILTERTAG])
efec1d0c 210 {
a42195db 211 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
1522c368
UD
212 struct openaux_args args;
213 struct list *orig;
266180eb 214 const ElfW(Dyn) *d;
1522c368 215
1522c368
UD
216 args.strtab = strtab;
217 args.map = l;
218 args.trace_mode = trace_mode;
219 orig = runp;
220
efec1d0c 221 for (d = l->l_ld; d->d_tag != DT_NULL; ++d)
e3e35cfc 222 if (__builtin_expect (d->d_tag, DT_NEEDED) == DT_NEEDED)
efec1d0c 223 {
f68b86cc 224 /* Map in the needed object. */
dc5efe83 225 struct link_map *dep;
1522c368 226 /* Allocate new entry. */
dc5efe83
UD
227 struct list *newp;
228 /* Object name. */
229 const char *name;
230
231 /* Recognize DSTs. */
232 name = expand_dst (l, strtab + d->d_un.d_val, 0);
233
234 dep = _dl_map_object (l, name, 0,
235 l->l_type == lt_executable ? lt_library :
bf8b3e74 236 l->l_type, trace_mode, 0);
1522c368
UD
237
238 /* Add it in any case to the duplicate list. */
dc5efe83 239 newp = alloca (sizeof (struct list));
1522c368
UD
240 newp->map = dep;
241 newp->dup = NULL;
242 dtail->dup = newp;
243 dtail = newp;
244 ++nduplist;
f68b86cc 245
42c4f32a 246 if (! dep->l_reserved)
efec1d0c 247 {
1522c368
UD
248 /* Append DEP to the unique list. */
249 newp->done = 0;
250 newp->unique = NULL;
251 utail->unique = newp;
252 utail = newp;
f68b86cc 253 ++nlist;
f9496a7b
RM
254 /* Set the mark bit that says it's already in the list. */
255 dep->l_reserved = 1;
efec1d0c 256 }
dacc8ffa
UD
257
258 /* Remember this dependency. */
259 if (needed != NULL)
260 needed[nneeded++] = dep;
1522c368 261 }
f41c8091 262 else if (d->d_tag == DT_AUXILIARY || d->d_tag == DT_FILTER)
1522c368 263 {
8e17ea58
UD
264 const char *objname;
265 const char *errstring;
f41c8091 266 struct list *newp;
dc5efe83
UD
267 /* Object name. */
268 const char *name;
269
270 /* Recognize DSTs. */
271 name = expand_dst (l, strtab + d->d_un.d_val,
272 d->d_tag == DT_AUXILIARY);
84384f5b 273
f41c8091 274 if (d->d_tag == DT_AUXILIARY)
1522c368 275 {
f41c8091 276 /* Store the tag in the argument structure. */
dc5efe83 277 args.name = name;
f41c8091 278
8d9618b7 279 /* Say that we are about to load an auxiliary library. */
466a0ec9 280 if (__builtin_expect (_dl_debug_libs, 0))
f802accb 281 _dl_debug_message (1, "load auxiliary object=",
dc5efe83 282 name, " requested by file=",
8d9618b7
UD
283 l->l_name[0]
284 ? l->l_name : _dl_argv[0],
285 "\n", NULL);
286
f41c8091
UD
287 /* We must be prepared that the addressed shared
288 object is not available. */
8e17ea58 289 if (_dl_catch_error (&objname, &errstring, openaux, &args))
f41c8091
UD
290 {
291 /* We are not interested in the error message. */
292 assert (errstring != NULL);
ca3c0135
UD
293 if (errstring != _dl_out_of_memory)
294 free ((char *) errstring);
f41c8091
UD
295
296 /* Simply ignore this error and continue the work. */
297 continue;
298 }
1522c368
UD
299 }
300 else
8d9618b7
UD
301 {
302 /* Say that we are about to load an auxiliary library. */
466a0ec9 303 if (__builtin_expect (_dl_debug_libs, 0))
dc5efe83 304 _dl_debug_message (1, "load filtered object=", name,
8d9618b7
UD
305 " requested by file=",
306 l->l_name[0]
307 ? l->l_name : _dl_argv[0],
308 "\n", NULL);
309
310 /* For filter objects the dependency must be available. */
dc5efe83 311 args.aux = _dl_map_object (l, name, 0,
8d9618b7
UD
312 (l->l_type == lt_executable
313 ? lt_library : l->l_type),
bf8b3e74 314 trace_mode, 0);
8d9618b7 315 }
f41c8091
UD
316
317 /* The auxiliary object is actually available.
318 Incorporate the map in all the lists. */
319
320 /* Allocate new entry. This always has to be done. */
321 newp = alloca (sizeof (struct list));
322
ee0f7bd6
UD
323 /* We want to insert the new map before the current one,
324 but we have no back links. So we copy the contents of
325 the current entry over. Note that ORIG and NEWP now
326 have switched their meanings. */
8193034b 327 orig->dup = memcpy (newp, orig, sizeof (*newp));
f41c8091
UD
328
329 /* Initialize new entry. */
330 orig->done = 0;
331 orig->map = args.aux;
f41c8091 332
dacc8ffa
UD
333 /* Remember this dependency. */
334 if (needed != NULL)
335 needed[nneeded++] = args.aux;
336
f41c8091
UD
337 /* We must handle two situations here: the map is new,
338 so we must add it in all three lists. If the map
339 is already known, we have two further possibilities:
340 - if the object is before the current map in the
341 search list, we do nothing. It is already found
342 early
343 - if the object is after the current one, we must
344 move it just before the current map to make sure
345 the symbols are found early enough
346 */
347 if (args.aux->l_reserved)
1522c368 348 {
f41c8091
UD
349 /* The object is already somewhere in the list.
350 Locate it first. */
351 struct list *late;
352
353 /* This object is already in the search list we
354 are building. Don't add a duplicate pointer.
42c4f32a 355 Just added by _dl_map_object. */
ee0f7bd6 356 for (late = newp; late->unique; late = late->unique)
f41c8091
UD
357 if (late->unique->map == args.aux)
358 break;
359
360 if (late->unique)
1522c368 361 {
f41c8091
UD
362 /* The object is somewhere behind the current
363 position in the search path. We have to
364 move it to this earlier position. */
1522c368 365 orig->unique = newp;
f41c8091 366
ee0f7bd6
UD
367 /* Now remove the later entry from the unique list
368 and adjust the tail pointer. */
369 if (utail == late->unique)
370 utail = late;
f41c8091
UD
371 late->unique = late->unique->unique;
372
ee0f7bd6 373 /* We must move the object earlier in the chain. */
1522c368
UD
374 if (args.aux->l_prev)
375 args.aux->l_prev->l_next = args.aux->l_next;
376 if (args.aux->l_next)
377 args.aux->l_next->l_prev = args.aux->l_prev;
378
379 args.aux->l_prev = newp->map->l_prev;
380 newp->map->l_prev = args.aux;
381 if (args.aux->l_prev != NULL)
382 args.aux->l_prev->l_next = args.aux;
383 args.aux->l_next = newp->map;
384 }
f41c8091
UD
385 else
386 {
387 /* The object must be somewhere earlier in the
388 list. That's good, we only have to insert
389 an entry for the duplicate list. */
390 orig->unique = NULL; /* Never used. */
391
392 /* Now we have a problem. The element
393 pointing to ORIG in the unique list must
394 point to NEWP now. This is the only place
395 where we need this backreference and this
396 situation is really not that frequent. So
397 we don't use a double-linked list but
398 instead search for the preceding element. */
26b4d766 399 late = known;
f41c8091
UD
400 while (late->unique != orig)
401 late = late->unique;
402 late->unique = newp;
403 }
404 }
405 else
406 {
407 /* This is easy. We just add the symbol right here. */
408 orig->unique = newp;
409 ++nlist;
410 /* Set the mark bit that says it's already in the list. */
411 args.aux->l_reserved = 1;
412
413 /* The only problem is that in the double linked
414 list of all objects we don't have this new
415 object at the correct place. Correct this here. */
416 if (args.aux->l_prev)
417 args.aux->l_prev->l_next = args.aux->l_next;
418 if (args.aux->l_next)
419 args.aux->l_next->l_prev = args.aux->l_prev;
420
421 args.aux->l_prev = newp->map->l_prev;
422 newp->map->l_prev = args.aux;
423 if (args.aux->l_prev != NULL)
424 args.aux->l_prev->l_next = args.aux;
425 args.aux->l_next = newp->map;
426 }
1522c368 427
f41c8091
UD
428 /* Move the tail pointers if necessary. */
429 if (orig == utail)
430 utail = newp;
431 if (orig == dtail)
432 dtail = newp;
1522c368 433
f41c8091
UD
434 /* Move on the insert point. */
435 orig = newp;
1522c368 436
f41c8091
UD
437 /* We always add an entry to the duplicate list. */
438 ++nduplist;
efec1d0c
RM
439 }
440 }
dacc8ffa
UD
441
442 /* Terminate the list of dependencies and store the array address. */
443 if (needed != NULL)
444 {
445 needed[nneeded++] = NULL;
446
c6d80202 447 l->l_initfini = malloc (nneeded * sizeof needed[0]);
dacc8ffa
UD
448 if (l->l_initfini == NULL)
449 _dl_signal_error (ENOMEM, map->l_name,
8e17ea58 450 N_("cannot allocate dependency list"));
c6d80202 451 memcpy (l->l_initfini, needed, nneeded * sizeof needed[0]);
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
457 runp = runp->unique;
8193034b 458 while (runp != NULL && runp->done);
efec1d0c
RM
459 }
460
752a2a50
UD
461 if (map->l_initfini != NULL && map->l_type == lt_loaded)
462 {
463 /* This object was previously loaded as a dependency and we have
464 a separate l_initfini list. We don't need it anymore. */
465 assert (map->l_searchlist.r_list == NULL);
466 free (map->l_initfini);
467 }
468
f68b86cc
RM
469 /* Store the search list we built in the object. It will be used for
470 searches in the scope of this object. */
a8571d37 471 map->l_searchlist.r_list = malloc ((2 * nlist + 1
dacc8ffa
UD
472 + (nlist == nduplist ? 0 : nduplist))
473 * sizeof (struct link_map *));
be935610 474 if (map->l_searchlist.r_list == NULL)
df4ef2ab 475 _dl_signal_error (ENOMEM, map->l_name,
8e17ea58 476 N_("cannot allocate symbol search list"));
be935610 477 map->l_searchlist.r_nlist = nlist;
f68b86cc 478
26b4d766 479 for (nlist = 0, runp = known; runp; runp = runp->unique)
f68b86cc 480 {
a881e0a0 481 if (trace_mode && runp->map->l_faked)
ad9570d7
UD
482 /* This can happen when we trace the loading. */
483 --map->l_searchlist.r_nlist;
484 else
485 map->l_searchlist.r_list[nlist++] = runp->map;
f68b86cc
RM
486
487 /* Now clear all the mark bits we set in the objects on the search list
488 to avoid duplicates, so the next call starts fresh. */
1522c368 489 runp->map->l_reserved = 0;
f68b86cc 490 }
84384f5b 491
be935610 492 map->l_searchlist.r_nduplist = nduplist;
1522c368 493 if (nlist == nduplist)
be935610 494 map->l_searchlist.r_duplist = map->l_searchlist.r_list;
1522c368
UD
495 else
496 {
e3e35cfc
UD
497 unsigned int cnt;
498
dacc8ffa 499 map->l_searchlist.r_duplist = map->l_searchlist.r_list + nlist;
84384f5b 500
e3e35cfc 501 for (cnt = 0, runp = known; runp; runp = runp->dup)
a881e0a0 502 if (trace_mode && runp->map->l_faked)
ad9570d7
UD
503 /* This can happen when we trace the loading. */
504 --map->l_searchlist.r_nduplist;
505 else
506 map->l_searchlist.r_duplist[cnt++] = runp->map;
1522c368 507 }
dacc8ffa
UD
508
509 /* Now determine the order in which the initialization has to happen. */
510 map->l_initfini =
511 (struct link_map **) memcpy (map->l_searchlist.r_duplist + nduplist,
512 map->l_searchlist.r_list,
513 nlist * sizeof (struct link_map *));
514 /* We can skip looking for the binary itself which is at the front
515 of the search list. Look through the list backward so that circular
516 dependencies are not changing the order. */
517 for (i = 1; i < nlist; ++i)
518 {
519 struct link_map *l = map->l_searchlist.r_list[i];
520 unsigned int j;
521 unsigned int k;
522
523 /* Find the place in the initfini list where the map is currently
524 located. */
525 for (j = 1; map->l_initfini[j] != l; ++j)
526 ;
527
528 /* Find all object for which the current one is a dependency and
529 move the found object (if necessary) in front. */
530 for (k = j + 1; k < nlist; ++k)
531 {
532 struct link_map **runp;
533
534 runp = map->l_initfini[k]->l_initfini;
535 if (runp != NULL)
536 {
537 while (*runp != NULL)
538 if (*runp == l)
539 {
540 struct link_map *here = map->l_initfini[k];
541
542 /* Move it now. */
543 memmove (&map->l_initfini[j] + 1,
544 &map->l_initfini[j],
545 (k - j) * sizeof (struct link_map *));
546 map->l_initfini[j] = here;
547
548 break;
549 }
550 else
551 ++runp;
552 }
553 }
554 }
a8571d37
AJ
555 /* Terminate the list of dependencies. */
556 map->l_initfini[nlist] = NULL;
efec1d0c 557}