1 /* Load the dependencies of a mapped object.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 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. */
26 #include <sys/param.h>
27 #include <elf/ldsodefs.h>
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))
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;
45 extern size_t _dl_platformlen;
47 /* When loading auxiliary objects we must ignore errors. It's ok if
48 an object is missing. */
51 /* The arguments to openaux. */
57 /* The return value of openaux. */
64 struct openaux_args *args = (struct openaux_args *) a;
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),
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
77 - produce a flat list with unique members of all involved objects
79 - produce a flat list of all shared objects.
83 int done; /* Nonzero if this map was processed. */
84 struct link_map *map; /* The data. */
86 struct list *unique; /* Elements for normal list. */
87 struct list *dup; /* Elements in complete list. */
91 /* Macro to expand DST. It is an macro since we use `alloca'. */
92 #define expand_dst(l, str, fatal) \
94 const char *__str = (str); \
95 const char *__result = __str; \
96 size_t __cnt = DL_DST_COUNT(__str, 0); \
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"); \
107 __newp = (char *) alloca (DL_DST_REQUIRED (l, __str, strlen (__str), \
110 __result = DL_DST_SUBSTITUTE (l, __str, __newp, 0); \
112 if (*__result == '\0') \
114 /* The replacement for the DST is not known. We can't \
117 _dl_signal_error (0, __str, \
118 "empty dynamics string token substitution"); \
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); \
136 _dl_map_object_deps (struct link_map *map,
137 struct link_map **preloads, unsigned int npreloads,
140 struct list known[1 + npreloads + 1];
141 struct list *runp, *utail, *dtail;
142 unsigned int nlist, nduplist, i;
144 inline void preload (struct link_map *map)
146 known[nlist].done = 0;
147 known[nlist].map = map;
149 known[nlist].unique = &known[nlist + 1];
150 known[nlist].dup = &known[nlist + 1];
153 /* We use `l_reserved' as a mark bit to detect objects we have
154 already put in the search list and avoid adding duplicate
155 elements later in the list. */
159 /* No loaded object so far. */
162 /* First load MAP itself. */
165 /* Add the preloaded items after MAP but before any of its dependencies. */
166 for (i = 0; i < npreloads; ++i)
167 preload (preloads[i]);
169 /* Terminate the lists. */
170 known[nlist - 1].unique = NULL;
171 known[nlist - 1].dup = NULL;
173 /* Pointer to last unique object. */
174 utail = &known[nlist - 1];
175 /* Pointer to last loaded object. */
176 dtail = &known[nlist - 1];
178 /* Until now we have the same number of libraries in the normal and
179 the list with duplicates. */
182 /* Process each element of the search list, loading each of its
183 auxiliary objects and immediate dependencies. Auxiliary objects
184 will be added in the list before the object itself and
185 dependencies will be appended to the list as we step through it.
186 This produces a flat, ordered list that represents a
187 breadth-first search of the dependency tree.
189 The whole process is complicated by the fact that we better
190 should use alloca for the temporary list elements. But using
191 alloca means we cannot use recursive function calls. */
192 for (runp = known; runp; )
194 struct link_map *l = runp->map;
196 if (l->l_info[DT_NEEDED] || l->l_info[AUXTAG] || l->l_info[FILTERTAG])
198 const char *strtab = (const void *) l->l_info[DT_STRTAB]->d_un.d_ptr;
199 struct openaux_args args;
203 /* Mark map as processed. */
206 args.strtab = strtab;
208 args.trace_mode = trace_mode;
211 for (d = l->l_ld; d->d_tag != DT_NULL; ++d)
212 if (__builtin_expect (d->d_tag, DT_NEEDED) == DT_NEEDED)
214 /* Map in the needed object. */
215 struct link_map *dep;
216 /* Allocate new entry. */
221 /* Recognize DSTs. */
222 name = expand_dst (l, strtab + d->d_un.d_val, 0);
224 dep = _dl_map_object (l, name, 0,
225 l->l_type == lt_executable ? lt_library :
226 l->l_type, trace_mode);
228 /* Add it in any case to the duplicate list. */
229 newp = alloca (sizeof (struct list));
237 /* This object is already in the search list we are
238 building. Don't add a duplicate pointer.
239 Release the reference just added by
244 /* Append DEP to the unique list. */
247 utail->unique = newp;
250 /* Set the mark bit that says it's already in the list. */
254 else if (d->d_tag == DT_AUXILIARY || d->d_tag == DT_FILTER)
261 /* Recognize DSTs. */
262 name = expand_dst (l, strtab + d->d_un.d_val,
263 d->d_tag == DT_AUXILIARY);
265 if (d->d_tag == DT_AUXILIARY)
267 /* Store the tag in the argument structure. */
270 /* Say that we are about to load an auxiliary library. */
272 _dl_debug_message (1, "load auxiliary object=",
273 name, " requested by file=",
275 ? l->l_name : _dl_argv[0],
278 /* We must be prepared that the addressed shared
279 object is not available. */
280 if (_dl_catch_error (&errstring, openaux, &args))
282 /* We are not interested in the error message. */
283 assert (errstring != NULL);
286 /* Simply ignore this error and continue the work. */
292 /* Say that we are about to load an auxiliary library. */
294 _dl_debug_message (1, "load filtered object=", name,
295 " requested by file=",
297 ? l->l_name : _dl_argv[0],
300 /* For filter objects the dependency must be available. */
301 args.aux = _dl_map_object (l, name, 0,
302 (l->l_type == lt_executable
303 ? lt_library : l->l_type),
307 /* The auxiliary object is actually available.
308 Incorporate the map in all the lists. */
310 /* Allocate new entry. This always has to be done. */
311 newp = alloca (sizeof (struct list));
313 /* We want to insert the new map before the current one,
314 but we have no back links. So we copy the contents of
315 the current entry over. Note that ORIG and NEWP now
316 have switched their meanings. */
317 orig->dup = memcpy (newp, orig, sizeof (*newp));
319 /* Initialize new entry. */
321 orig->map = args.aux;
323 /* We must handle two situations here: the map is new,
324 so we must add it in all three lists. If the map
325 is already known, we have two further possibilities:
326 - if the object is before the current map in the
327 search list, we do nothing. It is already found
329 - if the object is after the current one, we must
330 move it just before the current map to make sure
331 the symbols are found early enough
333 if (args.aux->l_reserved)
335 /* The object is already somewhere in the list.
339 /* This object is already in the search list we
340 are building. Don't add a duplicate pointer.
341 Release the reference just added by
343 --args.aux->l_opencount;
345 for (late = newp; late->unique; late = late->unique)
346 if (late->unique->map == args.aux)
351 /* The object is somewhere behind the current
352 position in the search path. We have to
353 move it to this earlier position. */
356 /* Now remove the later entry from the unique list
357 and adjust the tail pointer. */
358 if (utail == late->unique)
360 late->unique = late->unique->unique;
362 /* We must move the object earlier in the chain. */
363 if (args.aux->l_prev)
364 args.aux->l_prev->l_next = args.aux->l_next;
365 if (args.aux->l_next)
366 args.aux->l_next->l_prev = args.aux->l_prev;
368 args.aux->l_prev = newp->map->l_prev;
369 newp->map->l_prev = args.aux;
370 if (args.aux->l_prev != NULL)
371 args.aux->l_prev->l_next = args.aux;
372 args.aux->l_next = newp->map;
376 /* The object must be somewhere earlier in the
377 list. That's good, we only have to insert
378 an entry for the duplicate list. */
379 orig->unique = NULL; /* Never used. */
381 /* Now we have a problem. The element
382 pointing to ORIG in the unique list must
383 point to NEWP now. This is the only place
384 where we need this backreference and this
385 situation is really not that frequent. So
386 we don't use a double-linked list but
387 instead search for the preceding element. */
389 while (late->unique != orig)
396 /* This is easy. We just add the symbol right here. */
399 /* Set the mark bit that says it's already in the list. */
400 args.aux->l_reserved = 1;
402 /* The only problem is that in the double linked
403 list of all objects we don't have this new
404 object at the correct place. Correct this here. */
405 if (args.aux->l_prev)
406 args.aux->l_prev->l_next = args.aux->l_next;
407 if (args.aux->l_next)
408 args.aux->l_next->l_prev = args.aux->l_prev;
410 args.aux->l_prev = newp->map->l_prev;
411 newp->map->l_prev = args.aux;
412 if (args.aux->l_prev != NULL)
413 args.aux->l_prev->l_next = args.aux;
414 args.aux->l_next = newp->map;
417 /* Move the tail pointers if necessary. */
423 /* Move on the insert point. */
426 /* We always add an entry to the duplicate list. */
431 /* Mark as processed. */
434 /* If we have no auxiliary objects just go on to the next map. */
438 while (runp != NULL && runp->done);
441 /* Store the search list we built in the object. It will be used for
442 searches in the scope of this object. */
443 map->l_searchlist.r_list = malloc (nlist * sizeof (struct link_map *));
444 if (map->l_searchlist.r_list == NULL)
445 _dl_signal_error (ENOMEM, map->l_name,
446 "cannot allocate symbol search list");
447 map->l_searchlist.r_nlist = nlist;
449 for (nlist = 0, runp = known; runp; runp = runp->unique)
451 if (trace_mode && runp->map->l_opencount == 0)
452 /* This can happen when we trace the loading. */
453 --map->l_searchlist.r_nlist;
455 map->l_searchlist.r_list[nlist++] = runp->map;
457 /* Now clear all the mark bits we set in the objects on the search list
458 to avoid duplicates, so the next call starts fresh. */
459 runp->map->l_reserved = 0;
462 map->l_searchlist.r_nduplist = nduplist;
463 if (nlist == nduplist)
464 map->l_searchlist.r_duplist = map->l_searchlist.r_list;
469 map->l_searchlist.r_duplist = malloc (nduplist
470 * sizeof (struct link_map *));
471 if (map->l_searchlist.r_duplist == NULL)
472 _dl_signal_error (ENOMEM, map->l_name,
473 "cannot allocate symbol search list");
475 for (cnt = 0, runp = known; runp; runp = runp->dup)
476 if (trace_mode && runp->map->l_opencount == 0)
477 /* This can happen when we trace the loading. */
478 --map->l_searchlist.r_nduplist;
480 map->l_searchlist.r_duplist[cnt++] = runp->map;