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