]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/dl-deps.c
(VERSION): Bump to 2.0.96.
[thirdparty/glibc.git] / elf / dl-deps.c
CommitLineData
efec1d0c 1/* Load the dependencies of a mapped object.
c6222ab9 2 Copyright (C) 1996, 1997, 1998 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
efec1d0c 20#include <dlfcn.h>
a853022c 21#include <errno.h>
efec1d0c 22#include <stdlib.h>
ca34d7a7 23#include <string.h>
a853022c
UD
24#include <elf/ldsodefs.h>
25
1522c368
UD
26#include <assert.h>
27
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))
f41c8091
UD
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))
1522c368
UD
36
37
38/* When loading auxiliary objects we must ignore errors. It's ok if
39 an object is missing. */
993b3242 40struct openaux_args
1522c368
UD
41 {
42 /* The arguments to openaux. */
43 struct link_map *map;
44 int trace_mode;
45 const char *strtab;
46 const ElfW(Dyn) *d;
993b3242 47
1522c368
UD
48 /* The return value of openaux. */
49 struct link_map *aux;
50 };
993b3242
UD
51
52static void
53openaux (void *a)
54{
55 struct openaux_args *args = (struct openaux_args *) a;
56
c6222ab9 57 args->aux = _dl_map_object (args->map, args->strtab + args->d->d_un.d_val, 0,
993b3242
UD
58 (args->map->l_type == lt_executable
59 ? lt_library : args->map->l_type),
60 args->trace_mode);
61}
62
1522c368
UD
63
64
0a54e401 65/* We use a very special kind of list to track the two kinds paths
1522c368
UD
66 through the list of loaded shared objects. We have to
67
1522c368
UD
68 - produce a flat list with unique members of all involved objects
69
70 - produce a flat list of all shared objects.
71*/
72struct list
73 {
74 int done; /* Nonzero if this map was processed. */
75 struct link_map *map; /* The data. */
76
77 struct list *unique; /* Elements for normal list. */
78 struct list *dup; /* Elements in complete list. */
79 };
80
81
efec1d0c 82void
d0fc4041 83internal_function
2064087b 84_dl_map_object_deps (struct link_map *map,
46ec036d
UD
85 struct link_map **preloads, unsigned int npreloads,
86 int trace_mode)
efec1d0c 87{
1522c368 88 struct list known[1 + npreloads + 1];
26b4d766 89 struct list *runp, *utail, *dtail;
1522c368
UD
90 unsigned int nlist, nduplist, i;
91
df4ef2ab
UD
92 inline void preload (struct link_map *map)
93 {
1522c368
UD
94 known[nlist].done = 0;
95 known[nlist].map = map;
96
97 known[nlist].unique = &known[nlist + 1];
98 known[nlist].dup = &known[nlist + 1];
efec1d0c 99
1522c368 100 ++nlist;
df4ef2ab
UD
101 /* We use `l_reserved' as a mark bit to detect objects we have
102 already put in the search list and avoid adding duplicate
103 elements later in the list. */
104 map->l_reserved = 1;
105 }
2064087b 106
1522c368
UD
107 /* No loaded object so far. */
108 nlist = 0;
2064087b 109
1522c368 110 /* First load MAP itself. */
df4ef2ab
UD
111 preload (map);
112
113 /* Add the preloaded items after MAP but before any of its dependencies. */
114 for (i = 0; i < npreloads; ++i)
115 preload (preloads[i]);
116
8a523922 117 /* Terminate the lists. */
1522c368
UD
118 known[nlist - 1].unique = NULL;
119 known[nlist - 1].dup = NULL;
120
1522c368
UD
121 /* Pointer to last unique object. */
122 utail = &known[nlist - 1];
123 /* Pointer to last loaded object. */
124 dtail = &known[nlist - 1];
f68b86cc 125
84384f5b
UD
126 /* Until now we have the same number of libraries in the normal and
127 the list with duplicates. */
128 nduplist = nlist;
efec1d0c 129
1522c368
UD
130 /* Process each element of the search list, loading each of its
131 auxiliary objects and immediate dependencies. Auxiliary objects
132 will be added in the list before the object itself and
133 dependencies will be appended to the list as we step through it.
134 This produces a flat, ordered list that represents a
135 breadth-first search of the dependency tree.
136
137 The whole process is complicated by the fact that we better
138 should use alloca for the temporary list elements. But using
139 alloca means we cannot use recursive function calls. */
140 for (runp = known; runp; )
efec1d0c 141 {
1522c368 142 struct link_map *l = runp->map;
f68b86cc 143
8193034b 144 if (l->l_info[DT_NEEDED] || l->l_info[AUXTAG] || l->l_info[FILTERTAG])
efec1d0c 145 {
1522c368
UD
146 const char *strtab = ((void *) l->l_addr
147 + l->l_info[DT_STRTAB]->d_un.d_ptr);
148 struct openaux_args args;
149 struct list *orig;
266180eb 150 const ElfW(Dyn) *d;
1522c368
UD
151
152 /* Mark map as processed. */
153 runp->done = 1;
154
155 args.strtab = strtab;
156 args.map = l;
157 args.trace_mode = trace_mode;
158 orig = runp;
159
efec1d0c
RM
160 for (d = l->l_ld; d->d_tag != DT_NULL; ++d)
161 if (d->d_tag == DT_NEEDED)
162 {
f68b86cc
RM
163 /* Map in the needed object. */
164 struct link_map *dep
c6222ab9 165 = _dl_map_object (l, strtab + d->d_un.d_val, 0,
ba79d61b 166 l->l_type == lt_executable ? lt_library :
46ec036d 167 l->l_type, trace_mode);
1522c368
UD
168 /* Allocate new entry. */
169 struct list *newp = alloca (sizeof (struct list));
170
171 /* Add it in any case to the duplicate list. */
172 newp->map = dep;
173 newp->dup = NULL;
174 dtail->dup = newp;
175 dtail = newp;
176 ++nduplist;
f68b86cc
RM
177
178 if (dep->l_reserved)
179 /* This object is already in the search list we are
1522c368
UD
180 building. Don't add a duplicate pointer.
181 Release the reference just added by
182 _dl_map_object. */
f68b86cc 183 --dep->l_opencount;
efec1d0c
RM
184 else
185 {
1522c368
UD
186 /* Append DEP to the unique list. */
187 newp->done = 0;
188 newp->unique = NULL;
189 utail->unique = newp;
190 utail = newp;
f68b86cc 191 ++nlist;
f9496a7b
RM
192 /* Set the mark bit that says it's already in the list. */
193 dep->l_reserved = 1;
efec1d0c 194 }
1522c368 195 }
f41c8091 196 else if (d->d_tag == DT_AUXILIARY || d->d_tag == DT_FILTER)
1522c368
UD
197 {
198 char *errstring;
f41c8091 199 struct list *newp;
84384f5b 200
f41c8091 201 if (d->d_tag == DT_AUXILIARY)
1522c368 202 {
f41c8091
UD
203 /* Store the tag in the argument structure. */
204 args.d = d;
205
8d9618b7
UD
206 /* Say that we are about to load an auxiliary library. */
207 if (_dl_debug_libs)
f802accb 208 _dl_debug_message (1, "load auxiliary object=",
8d9618b7
UD
209 strtab + d->d_un.d_val,
210 " requested by file=",
211 l->l_name[0]
212 ? l->l_name : _dl_argv[0],
213 "\n", NULL);
214
f41c8091
UD
215 /* We must be prepared that the addressed shared
216 object is not available. */
8d9618b7 217 if (_dl_catch_error (&errstring, openaux, &args))
f41c8091
UD
218 {
219 /* We are not interested in the error message. */
220 assert (errstring != NULL);
221 free (errstring);
222
223 /* Simply ignore this error and continue the work. */
224 continue;
225 }
1522c368
UD
226 }
227 else
8d9618b7
UD
228 {
229 /* Say that we are about to load an auxiliary library. */
230 if (_dl_debug_libs)
f802accb 231 _dl_debug_message (1, "load filtered object=",
8d9618b7
UD
232 strtab + d->d_un.d_val,
233 " requested by file=",
234 l->l_name[0]
235 ? l->l_name : _dl_argv[0],
236 "\n", NULL);
237
238 /* For filter objects the dependency must be available. */
239 args.aux = _dl_map_object (l, strtab + d->d_un.d_val, 0,
240 (l->l_type == lt_executable
241 ? lt_library : l->l_type),
242 trace_mode);
243 }
f41c8091
UD
244
245 /* The auxiliary object is actually available.
246 Incorporate the map in all the lists. */
247
248 /* Allocate new entry. This always has to be done. */
249 newp = alloca (sizeof (struct list));
250
251 /* Copy the content of the current entry over. */
8193034b 252 orig->dup = memcpy (newp, orig, sizeof (*newp));
f41c8091
UD
253
254 /* Initialize new entry. */
255 orig->done = 0;
256 orig->map = args.aux;
f41c8091
UD
257
258 /* We must handle two situations here: the map is new,
259 so we must add it in all three lists. If the map
260 is already known, we have two further possibilities:
261 - if the object is before the current map in the
262 search list, we do nothing. It is already found
263 early
264 - if the object is after the current one, we must
265 move it just before the current map to make sure
266 the symbols are found early enough
267 */
268 if (args.aux->l_reserved)
1522c368 269 {
f41c8091
UD
270 /* The object is already somewhere in the list.
271 Locate it first. */
272 struct list *late;
273
274 /* This object is already in the search list we
275 are building. Don't add a duplicate pointer.
276 Release the reference just added by
277 _dl_map_object. */
278 --args.aux->l_opencount;
279
280 for (late = orig; late->unique; late = late->unique)
281 if (late->unique->map == args.aux)
282 break;
283
284 if (late->unique)
1522c368 285 {
f41c8091
UD
286 /* The object is somewhere behind the current
287 position in the search path. We have to
288 move it to this earlier position. */
1522c368 289 orig->unique = newp;
f41c8091
UD
290
291 /* Now remove the later entry from the unique list. */
292 late->unique = late->unique->unique;
293
294 /* We must move the earlier in the chain. */
1522c368
UD
295 if (args.aux->l_prev)
296 args.aux->l_prev->l_next = args.aux->l_next;
297 if (args.aux->l_next)
298 args.aux->l_next->l_prev = args.aux->l_prev;
299
300 args.aux->l_prev = newp->map->l_prev;
301 newp->map->l_prev = args.aux;
302 if (args.aux->l_prev != NULL)
303 args.aux->l_prev->l_next = args.aux;
304 args.aux->l_next = newp->map;
305 }
f41c8091
UD
306 else
307 {
308 /* The object must be somewhere earlier in the
309 list. That's good, we only have to insert
310 an entry for the duplicate list. */
311 orig->unique = NULL; /* Never used. */
312
313 /* Now we have a problem. The element
314 pointing to ORIG in the unique list must
315 point to NEWP now. This is the only place
316 where we need this backreference and this
317 situation is really not that frequent. So
318 we don't use a double-linked list but
319 instead search for the preceding element. */
26b4d766 320 late = known;
f41c8091
UD
321 while (late->unique != orig)
322 late = late->unique;
323 late->unique = newp;
324 }
325 }
326 else
327 {
328 /* This is easy. We just add the symbol right here. */
329 orig->unique = newp;
330 ++nlist;
331 /* Set the mark bit that says it's already in the list. */
332 args.aux->l_reserved = 1;
333
334 /* The only problem is that in the double linked
335 list of all objects we don't have this new
336 object at the correct place. Correct this here. */
337 if (args.aux->l_prev)
338 args.aux->l_prev->l_next = args.aux->l_next;
339 if (args.aux->l_next)
340 args.aux->l_next->l_prev = args.aux->l_prev;
341
342 args.aux->l_prev = newp->map->l_prev;
343 newp->map->l_prev = args.aux;
344 if (args.aux->l_prev != NULL)
345 args.aux->l_prev->l_next = args.aux;
346 args.aux->l_next = newp->map;
347 }
1522c368 348
f41c8091
UD
349 /* Move the tail pointers if necessary. */
350 if (orig == utail)
351 utail = newp;
352 if (orig == dtail)
353 dtail = newp;
1522c368 354
f41c8091
UD
355 /* Move on the insert point. */
356 orig = newp;
1522c368 357
f41c8091
UD
358 /* We always add an entry to the duplicate list. */
359 ++nduplist;
efec1d0c
RM
360 }
361 }
1522c368
UD
362 else
363 /* Mark as processed. */
364 runp->done = 1;
365
366 /* If we have no auxiliary objects just go on to the next map. */
367 if (runp->done)
368 do
369 runp = runp->unique;
8193034b 370 while (runp != NULL && runp->done);
efec1d0c
RM
371 }
372
f68b86cc
RM
373 /* Store the search list we built in the object. It will be used for
374 searches in the scope of this object. */
375 map->l_searchlist = malloc (nlist * sizeof (struct link_map *));
df4ef2ab
UD
376 if (map->l_searchlist == NULL)
377 _dl_signal_error (ENOMEM, map->l_name,
378 "cannot allocate symbol search list");
efec1d0c 379 map->l_nsearchlist = nlist;
f68b86cc 380
26b4d766 381 for (nlist = 0, runp = known; runp; runp = runp->unique)
f68b86cc 382 {
1522c368 383 map->l_searchlist[nlist++] = runp->map;
f68b86cc
RM
384
385 /* Now clear all the mark bits we set in the objects on the search list
386 to avoid duplicates, so the next call starts fresh. */
1522c368 387 runp->map->l_reserved = 0;
f68b86cc 388 }
84384f5b 389
84384f5b 390 map->l_ndupsearchlist = nduplist;
1522c368
UD
391 if (nlist == nduplist)
392 map->l_dupsearchlist = map->l_searchlist;
393 else
394 {
395 map->l_dupsearchlist = malloc (nduplist * sizeof (struct link_map *));
396 if (map->l_dupsearchlist == NULL)
397 _dl_signal_error (ENOMEM, map->l_name,
398 "cannot allocate symbol search list");
84384f5b 399
26b4d766 400 for (nlist = 0, runp = known; runp; runp = runp->dup)
0a54e401 401 map->l_dupsearchlist[nlist++] = runp->map;
1522c368 402 }
efec1d0c 403}