]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/dl-deps.c
Update.
[thirdparty/glibc.git] / elf / dl-deps.c
1 /* Load the dependencies of a mapped object.
2 Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
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. */
19
20 #include <assert.h>
21 #include <dlfcn.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/param.h>
27 #include <elf/ldsodefs.h>
28
29 #include <dl-dst.h>
30
31 /* Whether an shared object references one or more auxiliary objects
32 is signaled by the AUXTAG entry in l_info. */
33 #define AUXTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGNUM \
34 + DT_EXTRATAGIDX (DT_AUXILIARY))
35 /* Whether an shared object references one or more auxiliary objects
36 is signaled by the AUXTAG entry in l_info. */
37 #define FILTERTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGNUM \
38 + DT_EXTRATAGIDX (DT_FILTER))
39
40 /* This is zero at program start to signal that the global scope map is
41 allocated by rtld. Later it keeps the size of the map. It might be
42 reset if in _dl_close if the last global object is removed. */
43 size_t _dl_global_scope_alloc;
44
45 extern size_t _dl_platformlen;
46
47 /* When loading auxiliary objects we must ignore errors. It's ok if
48 an object is missing. */
49 struct openaux_args
50 {
51 /* The arguments to openaux. */
52 struct link_map *map;
53 int trace_mode;
54 const char *strtab;
55 const char *name;
56
57 /* The return value of openaux. */
58 struct link_map *aux;
59 };
60
61 static void
62 openaux (void *a)
63 {
64 struct openaux_args *args = (struct openaux_args *) a;
65
66 args->aux = _dl_map_object (args->map, args->name, 0,
67 (args->map->l_type == lt_executable
68 ? lt_library : args->map->l_type),
69 args->trace_mode);
70 }
71
72
73
74 /* We use a very special kind of list to track the two kinds paths
75 through the list of loaded shared objects. We have to
76
77 - produce a flat list with unique members of all involved objects
78
79 - produce a flat list of all shared objects.
80 */
81 struct list
82 {
83 int done; /* Nonzero if this map was processed. */
84 struct link_map *map; /* The data. */
85
86 struct list *unique; /* Elements for normal list. */
87 struct list *dup; /* Elements in complete list. */
88 };
89
90
91 /* Macro to expand DST. It is an macro since we use `alloca'. */
92 #define expand_dst(l, str, fatal) \
93 ({ \
94 const char *__str = (str); \
95 const char *__result = __str; \
96 size_t __cnt = DL_DST_COUNT(__str, 0); \
97 \
98 if (__cnt != 0) \
99 { \
100 char *__newp; \
101 \
102 /* DST must not appear in SUID/SGID programs. */ \
103 if (__libc_enable_secure) \
104 _dl_signal_error (0, __str, \
105 "DST not allowed in SUID/SGID programs"); \
106 \
107 __newp = (char *) alloca (DL_DST_REQUIRED (l, __str, strlen (__str), \
108 __cnt)); \
109 \
110 __result = DL_DST_SUBSTITUTE (l, __str, __newp, 0); \
111 \
112 if (*__result == '\0') \
113 { \
114 /* The replacement for the DST is not known. We can't \
115 processed. */ \
116 if (fatal) \
117 _dl_signal_error (0, __str, \
118 "empty dynamics string token substitution"); \
119 else \
120 { \
121 /* This is for DT_AUXILIARY. */ \
122 if (_dl_debug_libs) \
123 _dl_debug_message (1, "cannot load auxiliary `", __str, \
124 "' because of empty dynamic string" \
125 " token substitution\n", NULL); \
126 continue; \
127 } \
128 } \
129 } \
130 \
131 __result; })
132
133
134 unsigned int
135 internal_function
136 _dl_map_object_deps (struct link_map *map,
137 struct link_map **preloads, unsigned int npreloads,
138 int trace_mode, int global_scope)
139 {
140 struct list known[1 + npreloads + 1];
141 struct list *runp, *utail, *dtail;
142 unsigned int nlist, nduplist, i;
143 unsigned int to_add = 0;
144
145 inline void preload (struct link_map *map)
146 {
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];
152
153 ++nlist;
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 }
159
160 /* No loaded object so far. */
161 nlist = 0;
162
163 /* First load MAP itself. */
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
170 /* Terminate the lists. */
171 known[nlist - 1].unique = NULL;
172 known[nlist - 1].dup = NULL;
173
174 /* Pointer to last unique object. */
175 utail = &known[nlist - 1];
176 /* Pointer to last loaded object. */
177 dtail = &known[nlist - 1];
178
179 /* Until now we have the same number of libraries in the normal and
180 the list with duplicates. */
181 nduplist = nlist;
182
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; )
194 {
195 struct link_map *l = runp->map;
196
197 if (l->l_info[DT_NEEDED] || l->l_info[AUXTAG] || l->l_info[FILTERTAG])
198 {
199 const char *strtab = (const void *) l->l_info[DT_STRTAB]->d_un.d_ptr;
200 struct openaux_args args;
201 struct list *orig;
202 const ElfW(Dyn) *d;
203
204 /* Mark map as processed. */
205 runp->done = 1;
206
207 args.strtab = strtab;
208 args.map = l;
209 args.trace_mode = trace_mode;
210 orig = runp;
211
212 for (d = l->l_ld; d->d_tag != DT_NULL; ++d)
213 if (__builtin_expect (d->d_tag, DT_NEEDED) == DT_NEEDED)
214 {
215 /* Map in the needed object. */
216 struct link_map *dep;
217 /* Allocate new entry. */
218 struct list *newp;
219 /* Object name. */
220 const char *name;
221
222 /* Recognize DSTs. */
223 name = expand_dst (l, strtab + d->d_un.d_val, 0);
224
225 dep = _dl_map_object (l, name, 0,
226 l->l_type == lt_executable ? lt_library :
227 l->l_type, trace_mode);
228
229 /* Add it in any case to the duplicate list. */
230 newp = alloca (sizeof (struct list));
231 newp->map = dep;
232 newp->dup = NULL;
233 dtail->dup = newp;
234 dtail = newp;
235 ++nduplist;
236
237 if (dep->l_reserved)
238 /* This object is already in the search list we are
239 building. Don't add a duplicate pointer.
240 Release the reference just added by
241 _dl_map_object. */
242 --dep->l_opencount;
243 else
244 {
245 /* Append DEP to the unique list. */
246 newp->done = 0;
247 newp->unique = NULL;
248 utail->unique = newp;
249 utail = newp;
250 ++nlist;
251 /* Set the mark bit that says it's already in the list. */
252 dep->l_reserved = 1;
253 }
254 }
255 else if (d->d_tag == DT_AUXILIARY || d->d_tag == DT_FILTER)
256 {
257 char *errstring;
258 struct list *newp;
259 /* Object name. */
260 const char *name;
261
262 /* Recognize DSTs. */
263 name = expand_dst (l, strtab + d->d_un.d_val,
264 d->d_tag == DT_AUXILIARY);
265
266 if (d->d_tag == DT_AUXILIARY)
267 {
268 /* Store the tag in the argument structure. */
269 args.name = name;
270
271 /* Say that we are about to load an auxiliary library. */
272 if (_dl_debug_libs)
273 _dl_debug_message (1, "load auxiliary object=",
274 name, " requested by file=",
275 l->l_name[0]
276 ? l->l_name : _dl_argv[0],
277 "\n", NULL);
278
279 /* We must be prepared that the addressed shared
280 object is not available. */
281 if (_dl_catch_error (&errstring, openaux, &args))
282 {
283 /* We are not interested in the error message. */
284 assert (errstring != NULL);
285 free (errstring);
286
287 /* Simply ignore this error and continue the work. */
288 continue;
289 }
290 }
291 else
292 {
293 /* Say that we are about to load an auxiliary library. */
294 if (_dl_debug_libs)
295 _dl_debug_message (1, "load filtered object=", name,
296 " requested by file=",
297 l->l_name[0]
298 ? l->l_name : _dl_argv[0],
299 "\n", NULL);
300
301 /* For filter objects the dependency must be available. */
302 args.aux = _dl_map_object (l, name, 0,
303 (l->l_type == lt_executable
304 ? lt_library : l->l_type),
305 trace_mode);
306 }
307
308 /* The auxiliary object is actually available.
309 Incorporate the map in all the lists. */
310
311 /* Allocate new entry. This always has to be done. */
312 newp = alloca (sizeof (struct list));
313
314 /* We want to insert the new map before the current one,
315 but we have no back links. So we copy the contents of
316 the current entry over. Note that ORIG and NEWP now
317 have switched their meanings. */
318 orig->dup = memcpy (newp, orig, sizeof (*newp));
319
320 /* Initialize new entry. */
321 orig->done = 0;
322 orig->map = args.aux;
323
324 /* We must handle two situations here: the map is new,
325 so we must add it in all three lists. If the map
326 is already known, we have two further possibilities:
327 - if the object is before the current map in the
328 search list, we do nothing. It is already found
329 early
330 - if the object is after the current one, we must
331 move it just before the current map to make sure
332 the symbols are found early enough
333 */
334 if (args.aux->l_reserved)
335 {
336 /* The object is already somewhere in the list.
337 Locate it first. */
338 struct list *late;
339
340 /* This object is already in the search list we
341 are building. Don't add a duplicate pointer.
342 Release the reference just added by
343 _dl_map_object. */
344 --args.aux->l_opencount;
345
346 for (late = newp; late->unique; late = late->unique)
347 if (late->unique->map == args.aux)
348 break;
349
350 if (late->unique)
351 {
352 /* The object is somewhere behind the current
353 position in the search path. We have to
354 move it to this earlier position. */
355 orig->unique = newp;
356
357 /* Now remove the later entry from the unique list
358 and adjust the tail pointer. */
359 if (utail == late->unique)
360 utail = late;
361 late->unique = late->unique->unique;
362
363 /* We must move the object earlier in the chain. */
364 if (args.aux->l_prev)
365 args.aux->l_prev->l_next = args.aux->l_next;
366 if (args.aux->l_next)
367 args.aux->l_next->l_prev = args.aux->l_prev;
368
369 args.aux->l_prev = newp->map->l_prev;
370 newp->map->l_prev = args.aux;
371 if (args.aux->l_prev != NULL)
372 args.aux->l_prev->l_next = args.aux;
373 args.aux->l_next = newp->map;
374 }
375 else
376 {
377 /* The object must be somewhere earlier in the
378 list. That's good, we only have to insert
379 an entry for the duplicate list. */
380 orig->unique = NULL; /* Never used. */
381
382 /* Now we have a problem. The element
383 pointing to ORIG in the unique list must
384 point to NEWP now. This is the only place
385 where we need this backreference and this
386 situation is really not that frequent. So
387 we don't use a double-linked list but
388 instead search for the preceding element. */
389 late = known;
390 while (late->unique != orig)
391 late = late->unique;
392 late->unique = newp;
393 }
394 }
395 else
396 {
397 /* This is easy. We just add the symbol right here. */
398 orig->unique = newp;
399 ++nlist;
400 /* Set the mark bit that says it's already in the list. */
401 args.aux->l_reserved = 1;
402
403 /* The only problem is that in the double linked
404 list of all objects we don't have this new
405 object at the correct place. Correct this here. */
406 if (args.aux->l_prev)
407 args.aux->l_prev->l_next = args.aux->l_next;
408 if (args.aux->l_next)
409 args.aux->l_next->l_prev = args.aux->l_prev;
410
411 args.aux->l_prev = newp->map->l_prev;
412 newp->map->l_prev = args.aux;
413 if (args.aux->l_prev != NULL)
414 args.aux->l_prev->l_next = args.aux;
415 args.aux->l_next = newp->map;
416 }
417
418 /* Move the tail pointers if necessary. */
419 if (orig == utail)
420 utail = newp;
421 if (orig == dtail)
422 dtail = newp;
423
424 /* Move on the insert point. */
425 orig = newp;
426
427 /* We always add an entry to the duplicate list. */
428 ++nduplist;
429 }
430 }
431 else
432 /* Mark as processed. */
433 runp->done = 1;
434
435 /* If we have no auxiliary objects just go on to the next map. */
436 if (runp->done)
437 do
438 runp = runp->unique;
439 while (runp != NULL && runp->done);
440 }
441
442 /* Store the search list we built in the object. It will be used for
443 searches in the scope of this object. */
444 map->l_searchlist.r_list = malloc (nlist * sizeof (struct link_map *));
445 if (map->l_searchlist.r_list == NULL)
446 _dl_signal_error (ENOMEM, map->l_name,
447 "cannot allocate symbol search list");
448 map->l_searchlist.r_nlist = nlist;
449
450 for (nlist = 0, runp = known; runp; runp = runp->unique)
451 {
452 if (trace_mode && runp->map->l_opencount == 0)
453 /* This can happen when we trace the loading. */
454 --map->l_searchlist.r_nlist;
455 else
456 map->l_searchlist.r_list[nlist++] = runp->map;
457
458 /* Now clear all the mark bits we set in the objects on the search list
459 to avoid duplicates, so the next call starts fresh. */
460 runp->map->l_reserved = 0;
461 }
462
463 map->l_searchlist.r_nduplist = nduplist;
464 if (nlist == nduplist)
465 map->l_searchlist.r_duplist = map->l_searchlist.r_list;
466 else
467 {
468 unsigned int cnt;
469
470 map->l_searchlist.r_duplist = malloc (nduplist
471 * sizeof (struct link_map *));
472 if (map->l_searchlist.r_duplist == NULL)
473 _dl_signal_error (ENOMEM, map->l_name,
474 "cannot allocate symbol search list");
475
476 for (cnt = 0, runp = known; runp; runp = runp->dup)
477 if (trace_mode && runp->map->l_opencount == 0)
478 /* This can happen when we trace the loading. */
479 --map->l_searchlist.r_nduplist;
480 else
481 map->l_searchlist.r_duplist[cnt++] = runp->map;
482 }
483
484 /* Now that all this succeeded put the objects in the global scope if
485 this is necessary. We put the original object and all the dependencies
486 in the global scope. If an object is already loaded and not in the
487 global scope we promote it. */
488 if (global_scope)
489 {
490 unsigned int cnt;
491 struct link_map **new_global;
492
493 /* Count the objects we have to put in the global scope. */
494 for (cnt = 0; cnt < nlist; ++cnt)
495 if (map->l_searchlist.r_list[cnt]->l_global == 0)
496 ++to_add;
497
498 /* The symbols of the new objects and its dependencies are to be
499 introduced into the global scope that will be used to resolve
500 references from other dynamically-loaded objects.
501
502 The global scope is the searchlist in the main link map. We
503 extend this list if necessary. There is one problem though:
504 since this structure was allocated very early (before the libc
505 is loaded) the memory it uses is allocated by the malloc()-stub
506 in the ld.so. When we come here these functions are not used
507 anymore. Instead the malloc() implementation of the libc is
508 used. But this means the block from the main map cannot be used
509 in an realloc() call. Therefore we allocate a completely new
510 array the first time we have to add something to the locale scope. */
511
512 if (_dl_global_scope_alloc == 0)
513 {
514 /* This is the first dynamic object given global scope. */
515 _dl_global_scope_alloc = _dl_main_searchlist->r_nlist + to_add + 8;
516 new_global = (struct link_map **)
517 malloc (_dl_global_scope_alloc * sizeof (struct link_map *));
518 if (new_global == NULL)
519 {
520 _dl_global_scope_alloc = 0;
521 nomem:
522 _dl_signal_error (ENOMEM, map->l_libname->name,
523 "cannot extend global scope");
524 return 0;
525 }
526
527 /* Copy over the old entries. */
528 memcpy (new_global, _dl_main_searchlist->r_list,
529 (_dl_main_searchlist->r_nlist * sizeof (struct link_map *)));
530
531 _dl_main_searchlist->r_list = new_global;
532 }
533 else if (_dl_main_searchlist->r_nlist + to_add > _dl_global_scope_alloc)
534 {
535 /* We have to extend the existing array of link maps in the
536 main map. */
537 new_global = (struct link_map **)
538 realloc (_dl_main_searchlist->r_list,
539 ((_dl_global_scope_alloc + to_add + 8)
540 * sizeof (struct link_map *)));
541 if (new_global == NULL)
542 goto nomem;
543
544 _dl_global_scope_alloc += to_add + 8;
545 _dl_main_searchlist->r_list = new_global;
546 }
547
548 /* Now add the new entries. */
549 to_add = 0;
550 for (cnt = 0; cnt < nlist; ++cnt)
551 if (map->l_searchlist.r_list[cnt]->l_global == 0)
552 {
553 _dl_main_searchlist->r_list[_dl_main_searchlist->r_nlist + to_add]
554 = map->l_searchlist.r_list[cnt];
555 ++to_add;
556 }
557
558 /* XXX Do we have to add something to r_dupsearchlist??? --drepper */
559 }
560
561 return to_add;
562 }