Update.
[glibc.git] / elf / dl-deps.c
blob355618ad1c899820010509bb68d6dff39c19c958
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.
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.
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.
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. */
20 #include <dlfcn.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <elf/ldsodefs.h>
26 #include <assert.h>
28 /* Whether an shared object references one or more auxiliary objects
29 is signaled by the AUXTAG entry in l_info. */
30 #define AUXTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGNUM \
31 + DT_EXTRATAGIDX (DT_AUXILIARY))
32 /* Whether an shared object references one or more auxiliary objects
33 is signaled by the AUXTAG entry in l_info. */
34 #define FILTERTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGNUM \
35 + DT_EXTRATAGIDX (DT_FILTER))
37 /* This is zero at program start to signal that the global scope map is
38 allocated by rtld. Later it keeps the size of the map. It might be
39 reset if in _dl_close if the last global object is removed. */
40 size_t _dl_global_scope_alloc;
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 const char *strtab;
51 const ElfW(Dyn) *d;
53 /* The return value of openaux. */
54 struct link_map *aux;
57 static void
58 openaux (void *a)
60 struct openaux_args *args = (struct openaux_args *) a;
62 args->aux = _dl_map_object (args->map, args->strtab + args->d->d_un.d_val, 0,
63 (args->map->l_type == lt_executable
64 ? lt_library : args->map->l_type),
65 args->trace_mode);
70 /* We use a very special kind of list to track the two kinds paths
71 through the list of loaded shared objects. We have to
73 - produce a flat list with unique members of all involved objects
75 - produce a flat list of all shared objects.
77 struct list
79 int done; /* Nonzero if this map was processed. */
80 struct link_map *map; /* The data. */
82 struct list *unique; /* Elements for normal list. */
83 struct list *dup; /* Elements in complete list. */
87 unsigned int
88 internal_function
89 _dl_map_object_deps (struct link_map *map,
90 struct link_map **preloads, unsigned int npreloads,
91 int trace_mode, int global_scope)
93 struct list known[1 + npreloads + 1];
94 struct list *runp, *utail, *dtail;
95 unsigned int nlist, nduplist, i;
96 unsigned int to_add = 0;
98 inline void preload (struct link_map *map)
100 known[nlist].done = 0;
101 known[nlist].map = map;
103 known[nlist].unique = &known[nlist + 1];
104 known[nlist].dup = &known[nlist + 1];
106 ++nlist;
107 /* We use `l_reserved' as a mark bit to detect objects we have
108 already put in the search list and avoid adding duplicate
109 elements later in the list. */
110 map->l_reserved = 1;
113 /* No loaded object so far. */
114 nlist = 0;
116 /* First load MAP itself. */
117 preload (map);
119 /* Add the preloaded items after MAP but before any of its dependencies. */
120 for (i = 0; i < npreloads; ++i)
121 preload (preloads[i]);
123 /* Terminate the lists. */
124 known[nlist - 1].unique = NULL;
125 known[nlist - 1].dup = NULL;
127 /* Pointer to last unique object. */
128 utail = &known[nlist - 1];
129 /* Pointer to last loaded object. */
130 dtail = &known[nlist - 1];
132 /* Until now we have the same number of libraries in the normal and
133 the list with duplicates. */
134 nduplist = nlist;
136 /* Process each element of the search list, loading each of its
137 auxiliary objects and immediate dependencies. Auxiliary objects
138 will be added in the list before the object itself and
139 dependencies will be appended to the list as we step through it.
140 This produces a flat, ordered list that represents a
141 breadth-first search of the dependency tree.
143 The whole process is complicated by the fact that we better
144 should use alloca for the temporary list elements. But using
145 alloca means we cannot use recursive function calls. */
146 for (runp = known; runp; )
148 struct link_map *l = runp->map;
150 if (l->l_info[DT_NEEDED] || l->l_info[AUXTAG] || l->l_info[FILTERTAG])
152 const char *strtab = ((void *) l->l_addr
153 + l->l_info[DT_STRTAB]->d_un.d_ptr);
154 struct openaux_args args;
155 struct list *orig;
156 const ElfW(Dyn) *d;
158 /* Mark map as processed. */
159 runp->done = 1;
161 args.strtab = strtab;
162 args.map = l;
163 args.trace_mode = trace_mode;
164 orig = runp;
166 for (d = l->l_ld; d->d_tag != DT_NULL; ++d)
167 if (__builtin_expect (d->d_tag, DT_NEEDED) == DT_NEEDED)
169 /* Map in the needed object. */
170 struct link_map *dep
171 = _dl_map_object (l, strtab + d->d_un.d_val, 0,
172 l->l_type == lt_executable ? lt_library :
173 l->l_type, trace_mode);
174 /* Allocate new entry. */
175 struct list *newp = alloca (sizeof (struct list));
177 /* Add it in any case to the duplicate list. */
178 newp->map = dep;
179 newp->dup = NULL;
180 dtail->dup = newp;
181 dtail = newp;
182 ++nduplist;
184 if (dep->l_reserved)
185 /* This object is already in the search list we are
186 building. Don't add a duplicate pointer.
187 Release the reference just added by
188 _dl_map_object. */
189 --dep->l_opencount;
190 else
192 /* Append DEP to the unique list. */
193 newp->done = 0;
194 newp->unique = NULL;
195 utail->unique = newp;
196 utail = newp;
197 ++nlist;
198 /* Set the mark bit that says it's already in the list. */
199 dep->l_reserved = 1;
202 else if (d->d_tag == DT_AUXILIARY || d->d_tag == DT_FILTER)
204 char *errstring;
205 struct list *newp;
207 if (d->d_tag == DT_AUXILIARY)
209 /* Store the tag in the argument structure. */
210 args.d = d;
212 /* Say that we are about to load an auxiliary library. */
213 if (_dl_debug_libs)
214 _dl_debug_message (1, "load auxiliary object=",
215 strtab + d->d_un.d_val,
216 " requested by file=",
217 l->l_name[0]
218 ? l->l_name : _dl_argv[0],
219 "\n", NULL);
221 /* We must be prepared that the addressed shared
222 object is not available. */
223 if (_dl_catch_error (&errstring, openaux, &args))
225 /* We are not interested in the error message. */
226 assert (errstring != NULL);
227 free (errstring);
229 /* Simply ignore this error and continue the work. */
230 continue;
233 else
235 /* Say that we are about to load an auxiliary library. */
236 if (_dl_debug_libs)
237 _dl_debug_message (1, "load filtered object=",
238 strtab + d->d_un.d_val,
239 " requested by file=",
240 l->l_name[0]
241 ? l->l_name : _dl_argv[0],
242 "\n", NULL);
244 /* For filter objects the dependency must be available. */
245 args.aux = _dl_map_object (l, strtab + d->d_un.d_val, 0,
246 (l->l_type == lt_executable
247 ? lt_library : l->l_type),
248 trace_mode);
251 /* The auxiliary object is actually available.
252 Incorporate the map in all the lists. */
254 /* Allocate new entry. This always has to be done. */
255 newp = alloca (sizeof (struct list));
257 /* Copy the content of the current entry over. */
258 orig->dup = memcpy (newp, orig, sizeof (*newp));
260 /* Initialize new entry. */
261 orig->done = 0;
262 orig->map = args.aux;
264 /* We must handle two situations here: the map is new,
265 so we must add it in all three lists. If the map
266 is already known, we have two further possibilities:
267 - if the object is before the current map in the
268 search list, we do nothing. It is already found
269 early
270 - if the object is after the current one, we must
271 move it just before the current map to make sure
272 the symbols are found early enough
274 if (args.aux->l_reserved)
276 /* The object is already somewhere in the list.
277 Locate it first. */
278 struct list *late;
280 /* This object is already in the search list we
281 are building. Don't add a duplicate pointer.
282 Release the reference just added by
283 _dl_map_object. */
284 --args.aux->l_opencount;
286 for (late = orig; late->unique; late = late->unique)
287 if (late->unique->map == args.aux)
288 break;
290 if (late->unique)
292 /* The object is somewhere behind the current
293 position in the search path. We have to
294 move it to this earlier position. */
295 orig->unique = newp;
297 /* Now remove the later entry from the unique list. */
298 late->unique = late->unique->unique;
300 /* We must move the earlier in the chain. */
301 if (args.aux->l_prev)
302 args.aux->l_prev->l_next = args.aux->l_next;
303 if (args.aux->l_next)
304 args.aux->l_next->l_prev = args.aux->l_prev;
306 args.aux->l_prev = newp->map->l_prev;
307 newp->map->l_prev = args.aux;
308 if (args.aux->l_prev != NULL)
309 args.aux->l_prev->l_next = args.aux;
310 args.aux->l_next = newp->map;
312 else
314 /* The object must be somewhere earlier in the
315 list. That's good, we only have to insert
316 an entry for the duplicate list. */
317 orig->unique = NULL; /* Never used. */
319 /* Now we have a problem. The element
320 pointing to ORIG in the unique list must
321 point to NEWP now. This is the only place
322 where we need this backreference and this
323 situation is really not that frequent. So
324 we don't use a double-linked list but
325 instead search for the preceding element. */
326 late = known;
327 while (late->unique != orig)
328 late = late->unique;
329 late->unique = newp;
332 else
334 /* This is easy. We just add the symbol right here. */
335 orig->unique = newp;
336 ++nlist;
337 /* Set the mark bit that says it's already in the list. */
338 args.aux->l_reserved = 1;
340 /* The only problem is that in the double linked
341 list of all objects we don't have this new
342 object at the correct place. Correct this here. */
343 if (args.aux->l_prev)
344 args.aux->l_prev->l_next = args.aux->l_next;
345 if (args.aux->l_next)
346 args.aux->l_next->l_prev = args.aux->l_prev;
348 args.aux->l_prev = newp->map->l_prev;
349 newp->map->l_prev = args.aux;
350 if (args.aux->l_prev != NULL)
351 args.aux->l_prev->l_next = args.aux;
352 args.aux->l_next = newp->map;
355 /* Move the tail pointers if necessary. */
356 if (orig == utail)
357 utail = newp;
358 if (orig == dtail)
359 dtail = newp;
361 /* Move on the insert point. */
362 orig = newp;
364 /* We always add an entry to the duplicate list. */
365 ++nduplist;
368 else
369 /* Mark as processed. */
370 runp->done = 1;
372 /* If we have no auxiliary objects just go on to the next map. */
373 if (runp->done)
375 runp = runp->unique;
376 while (runp != NULL && runp->done);
379 /* Store the search list we built in the object. It will be used for
380 searches in the scope of this object. */
381 map->l_searchlist.r_list = malloc (nlist * sizeof (struct link_map *));
382 if (map->l_searchlist.r_list == NULL)
383 _dl_signal_error (ENOMEM, map->l_name,
384 "cannot allocate symbol search list");
385 map->l_searchlist.r_nlist = nlist;
387 for (nlist = 0, runp = known; runp; runp = runp->unique)
389 map->l_searchlist.r_list[nlist++] = runp->map;
391 /* Now clear all the mark bits we set in the objects on the search list
392 to avoid duplicates, so the next call starts fresh. */
393 runp->map->l_reserved = 0;
396 map->l_searchlist.r_nduplist = nduplist;
397 if (nlist == nduplist)
398 map->l_searchlist.r_duplist = map->l_searchlist.r_list;
399 else
401 unsigned int cnt;
403 map->l_searchlist.r_duplist = malloc (nduplist
404 * sizeof (struct link_map *));
405 if (map->l_searchlist.r_duplist == NULL)
406 _dl_signal_error (ENOMEM, map->l_name,
407 "cannot allocate symbol search list");
409 for (cnt = 0, runp = known; runp; runp = runp->dup)
410 map->l_searchlist.r_duplist[cnt++] = runp->map;
413 /* Now that all this succeeded put the objects in the global scope if
414 this is necessary. We put the original object and all the dependencies
415 in the global scope. If an object is already loaded and not in the
416 global scope we promote it. */
417 if (global_scope)
419 unsigned int cnt;
420 struct link_map **new_global;
422 /* Count the objects we have to put in the global scope. */
423 for (cnt = 0; cnt < nlist; ++cnt)
424 if (map->l_searchlist.r_list[cnt]->l_global == 0)
425 ++to_add;
427 /* The symbols of the new objects and its dependencies are to be
428 introduced into the global scope that will be used to resolve
429 references from other dynamically-loaded objects.
431 The global scope is the searchlist in the main link map. We
432 extend this list if necessary. There is one problem though:
433 since this structure was allocated very early (before the libc
434 is loaded) the memory it uses is allocated by the malloc()-stub
435 in the ld.so. When we come here these functions are not used
436 anymore. Instead the malloc() implementation of the libc is
437 used. But this means the block from the main map cannot be used
438 in an realloc() call. Therefore we allocate a completely new
439 array the first time we have to add something to the locale scope. */
441 if (_dl_global_scope_alloc == 0)
443 /* This is the first dynamic object given global scope. */
444 _dl_global_scope_alloc = _dl_main_searchlist->r_nlist + to_add + 8;
445 new_global = (struct link_map **)
446 malloc (_dl_global_scope_alloc * sizeof (struct link_map *));
447 if (new_global == NULL)
449 _dl_global_scope_alloc = 0;
450 nomem:
451 _dl_signal_error (ENOMEM, map->l_libname->name,
452 "cannot extend global scope");
453 return 0;
456 /* Copy over the old entries. */
457 memcpy (new_global, _dl_main_searchlist->r_list,
458 (_dl_main_searchlist->r_nlist * sizeof (struct link_map *)));
460 _dl_main_searchlist->r_list = new_global;
462 else if (_dl_main_searchlist->r_nlist + to_add > _dl_global_scope_alloc)
464 /* We have to extend the existing array of link maps in the
465 main map. */
466 new_global = (struct link_map **)
467 realloc (_dl_main_searchlist->r_list,
468 ((_dl_global_scope_alloc + to_add + 8)
469 * sizeof (struct link_map *)));
470 if (new_global == NULL)
471 goto nomem;
473 _dl_global_scope_alloc += to_add + 8;
474 _dl_main_searchlist->r_list = new_global;
477 /* Now add the new entries. */
478 to_add = 0;
479 for (cnt = 0; cnt < nlist; ++cnt)
480 if (map->l_searchlist.r_list[cnt]->l_global == 0)
482 _dl_main_searchlist->r_list[_dl_main_searchlist->r_nlist + to_add]
483 = map->l_searchlist.r_list[cnt];
484 ++to_add;
487 /* XXX Do we have to add something to r_dupsearchlist??? --drepper */
490 return to_add;