System dependent I/O functions for dynamiclinker, generic version.
[glibc/history.git] / elf / dl-deps.c
blobe2fd3408222fc49a12e6c6e66259007ed754c550
1 /* Load the dependencies of a mapped object.
2 Copyright (C) 1996, 1997 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 <link.h>
21 #include <errno.h>
22 #include <dlfcn.h>
23 #include <stdlib.h>
25 struct openaux_args
27 /* The arguments to openaux. */
28 struct link_map *map;
29 int trace_mode;
30 const char *strtab;
31 ElfW(Dyn) *d;
33 /* The return value of openaux. */
34 struct link_map *aux;
37 static void
38 openaux (void *a)
40 struct openaux_args *args = (struct openaux_args *) a;
42 args->aux = _dl_map_object (args->map, args->strtab + args->d->d_un.d_val,
43 (args->map->l_type == lt_executable
44 ? lt_library : args->map->l_type),
45 args->trace_mode);
48 void
49 _dl_map_object_deps (struct link_map *map,
50 struct link_map **preloads, unsigned int npreloads,
51 int trace_mode)
53 struct list
55 struct link_map *map;
56 struct list *next;
58 struct list *head, *tailp, *scanp;
59 struct list duphead, *duptailp;
60 unsigned int nduplist;
61 unsigned int nlist, naux, i;
62 inline void preload (struct link_map *map)
64 head[nlist].next = &head[nlist + 1];
65 head[nlist++].map = map;
67 /* We use `l_reserved' as a mark bit to detect objects we have
68 already put in the search list and avoid adding duplicate
69 elements later in the list. */
70 map->l_reserved = 1;
73 naux = nlist = 0;
75 /* XXX The AUXILIARY implementation isn't correct in the moment. XXX
76 XXX The problem is that we currently do not handle auxiliary XXX
77 XXX entries in the loaded objects. XXX */
79 #define AUXTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGNUM \
80 + DT_EXTRATAGIDX (DT_AUXILIARY))
82 /* First determine the number of auxiliary objects we have to load. */
83 if (map->l_info[AUXTAG])
85 ElfW(Dyn) *d;
86 for (d = map->l_ld; d->d_tag != DT_NULL; ++d)
87 if (d->d_tag == DT_AUXILIARY)
88 ++naux;
91 /* Now we can allocate the array for the linker maps. */
92 head = (struct list *) alloca (sizeof (struct list)
93 * (naux + npreloads + 2));
95 /* Load the auxiliary objects, even before the object itself. */
96 if (map->l_info[AUXTAG])
98 /* There is at least one auxiliary library specified. We try to
99 load it, and if we can, use its symbols in preference to our
100 own. But if we can't load it, we just silently ignore it. */
101 struct openaux_args args;
102 args.strtab
103 = ((void *) map->l_addr + map->l_info[DT_STRTAB]->d_un.d_ptr);
104 args.map = map;
105 args.trace_mode = trace_mode;
107 for (args.d = map->l_ld; args.d->d_tag != DT_NULL; ++args.d)
108 if (args.d->d_tag == DT_AUXILIARY)
110 char *errstring;
111 const char *objname;
112 if (! _dl_catch_error (&errstring, &objname, openaux, &args))
113 /* The auxiliary object is actually there. Use it as
114 the first search element, even before MAP itself. */
115 preload (args.aux);
119 /* Next load MAP itself. */
120 preload (map);
122 /* Add the preloaded items after MAP but before any of its dependencies. */
123 for (i = 0; i < npreloads; ++i)
124 preload (preloads[i]);
126 /* Terminate the lists. */
127 head[nlist - 1].next = NULL;
128 duphead.next = NULL;
130 /* Start here for adding dependencies to the list. */
131 tailp = &head[nlist - 1];
133 /* Until now we have the same number of libraries in the normal and
134 the list with duplicates. */
135 nduplist = nlist;
136 duptailp = &duphead;
138 /* Process each element of the search list, loading each of its immediate
139 dependencies and appending them to the list as we step through it.
140 This produces a flat, ordered list that represents a breadth-first
141 search of the dependency tree. */
142 for (scanp = head; scanp; scanp = scanp->next)
144 struct link_map *l = scanp->map;
146 if (l->l_info[DT_NEEDED])
148 const char *strtab
149 = ((void *) l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr);
150 const ElfW(Dyn) *d;
151 for (d = l->l_ld; d->d_tag != DT_NULL; ++d)
152 if (d->d_tag == DT_NEEDED)
154 /* Map in the needed object. */
155 struct link_map *dep
156 = _dl_map_object (l, strtab + d->d_un.d_val,
157 l->l_type == lt_executable ? lt_library :
158 l->l_type, trace_mode);
160 if (dep->l_reserved)
161 /* This object is already in the search list we are
162 building. Don't add a duplicate pointer. Release the
163 reference just added by _dl_map_object. */
164 --dep->l_opencount;
165 else
167 /* Append DEP to the search list. */
168 tailp->next = alloca (sizeof *tailp);
169 tailp = tailp->next;
170 tailp->map = dep;
171 tailp->next = NULL;
172 ++nlist;
173 /* Set the mark bit that says it's already in the list. */
174 dep->l_reserved = 1;
177 /* In any case append DEP to the duplicates search list. */
178 duptailp->next = alloca (sizeof *duptailp);
179 duptailp = duptailp->next;
180 duptailp->map = dep;
181 duptailp->next = NULL;
182 ++nduplist;
187 /* Store the search list we built in the object. It will be used for
188 searches in the scope of this object. */
189 map->l_searchlist = malloc (nlist * sizeof (struct link_map *));
190 if (map->l_searchlist == NULL)
191 _dl_signal_error (ENOMEM, map->l_name,
192 "cannot allocate symbol search list");
193 map->l_nsearchlist = nlist;
195 nlist = 0;
196 for (scanp = head; scanp; scanp = scanp->next)
198 map->l_searchlist[nlist++] = scanp->map;
200 /* Now clear all the mark bits we set in the objects on the search list
201 to avoid duplicates, so the next call starts fresh. */
202 scanp->map->l_reserved = 0;
205 map->l_dupsearchlist = malloc (nduplist * sizeof (struct link_map *));
206 if (map->l_dupsearchlist == NULL)
207 _dl_signal_error (ENOMEM, map->l_name,
208 "cannot allocate symbol search list");
209 map->l_ndupsearchlist = nduplist;
211 for (nlist = 0; nlist < naux + 1 + npreloads; ++nlist)
212 map->l_dupsearchlist[nlist] = head[nlist].map;
213 for (scanp = duphead.next; scanp; scanp = scanp->next)
214 map->l_dupsearchlist[nlist++] = scanp->map;