(openpty): Allow the use of more ptys.
[glibc.git] / elf / dl-deps.c
blobd81593b942d640479821df3d44fc6c18d4ac20cc
1 /* Load the dependencies of a mapped object.
2 Copyright (C) 1996, 1997, 1998 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 void
26 _dl_map_object_deps (struct link_map *map,
27 struct link_map **preloads, unsigned int npreloads,
28 int trace_mode)
30 struct list
32 struct link_map *map;
33 struct list *next;
35 struct list *head, *tailp, *scanp;
36 struct list duphead, *duptailp;
37 unsigned int nduplist;
38 unsigned int nlist, naux, i;
39 inline void preload (struct link_map *map)
41 head[nlist].next = &head[nlist + 1];
42 head[nlist++].map = map;
44 /* We use `l_reserved' as a mark bit to detect objects we have
45 already put in the search list and avoid adding duplicate
46 elements later in the list. */
47 map->l_reserved = 1;
50 naux = nlist = 0;
52 /* XXX The AUXILIARY implementation isn't correct in the moment. XXX
53 XXX The problem is that we currently do not handle auxiliary XXX
54 XXX entries in the loaded objects. XXX */
56 #define AUXTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGNUM \
57 + DT_EXTRATAGIDX (DT_AUXILIARY))
59 /* First determine the number of auxiliary objects we have to load. */
60 if (map->l_info[AUXTAG])
62 ElfW(Dyn) *d;
63 for (d = map->l_ld; d->d_tag != DT_NULL; ++d)
64 if (d->d_tag == DT_AUXILIARY)
65 ++naux;
68 /* Now we can allocate the array for the linker maps. */
69 head = (struct list *) alloca (sizeof (struct list)
70 * (naux + npreloads + 2));
72 /* Load the auxiliary objects, even before the object itself. */
73 if (map->l_info[AUXTAG])
75 /* There is at least one auxiliary library specified. We try to
76 load it, and if we can, use its symbols in preference to our
77 own. But if we can't load it, we just silently ignore it. */
78 const char *strtab
79 = ((void *) map->l_addr + map->l_info[DT_STRTAB]->d_un.d_ptr);
80 ElfW(Dyn) *d;
82 for (d = map->l_ld; d->d_tag != DT_NULL; ++d)
83 if (d->d_tag == DT_AUXILIARY)
85 struct link_map *aux;
86 void openaux (void)
88 aux = _dl_map_object (map, strtab + d->d_un.d_val, 0,
89 (map->l_type == lt_executable
90 ? lt_library : map->l_type),
91 trace_mode);
93 char *errstring;
94 if (! _dl_catch_error (&errstring, openaux))
95 /* The auxiliary object is actually there. Use it as
96 the first search element, even before MAP itself. */
97 preload (aux);
101 /* Next load MAP itself. */
102 preload (map);
104 /* Add the preloaded items after MAP but before any of its dependencies. */
105 for (i = 0; i < npreloads; ++i)
106 preload (preloads[i]);
108 /* Terminate the lists. */
109 head[nlist - 1].next = NULL;
110 duphead.next = NULL;
112 /* Start here for adding dependencies to the list. */
113 tailp = &head[nlist - 1];
115 /* Until now we have the same number of libraries in the normal and
116 the list with duplicates. */
117 nduplist = nlist;
118 duptailp = &duphead;
120 /* Process each element of the search list, loading each of its immediate
121 dependencies and appending them to the list as we step through it.
122 This produces a flat, ordered list that represents a breadth-first
123 search of the dependency tree. */
124 for (scanp = head; scanp; scanp = scanp->next)
126 struct link_map *l = scanp->map;
128 if (l->l_info[DT_NEEDED])
130 const char *strtab
131 = ((void *) l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr);
132 const ElfW(Dyn) *d;
133 for (d = l->l_ld; d->d_tag != DT_NULL; ++d)
134 if (d->d_tag == DT_NEEDED)
136 /* Map in the needed object. */
137 struct link_map *dep
138 = _dl_map_object (l, strtab + d->d_un.d_val, 0,
139 l->l_type == lt_executable ? lt_library :
140 l->l_type, trace_mode);
142 if (dep->l_reserved)
143 /* This object is already in the search list we are
144 building. Don't add a duplicate pointer. Release the
145 reference just added by _dl_map_object. */
146 --dep->l_opencount;
147 else
149 /* Append DEP to the search list. */
150 tailp->next = alloca (sizeof *tailp);
151 tailp = tailp->next;
152 tailp->map = dep;
153 tailp->next = NULL;
154 ++nlist;
155 /* Set the mark bit that says it's already in the list. */
156 dep->l_reserved = 1;
159 /* In any case append DEP to the duplicates search list. */
160 duptailp->next = alloca (sizeof *duptailp);
161 duptailp = duptailp->next;
162 duptailp->map = dep;
163 duptailp->next = NULL;
164 ++nduplist;
169 /* Store the search list we built in the object. It will be used for
170 searches in the scope of this object. */
171 map->l_searchlist = malloc (nlist * sizeof (struct link_map *));
172 if (map->l_searchlist == NULL)
173 _dl_signal_error (ENOMEM, map->l_name,
174 "cannot allocate symbol search list");
175 map->l_nsearchlist = nlist;
177 nlist = 0;
178 for (scanp = head; scanp; scanp = scanp->next)
180 map->l_searchlist[nlist++] = scanp->map;
182 /* Now clear all the mark bits we set in the objects on the search list
183 to avoid duplicates, so the next call starts fresh. */
184 scanp->map->l_reserved = 0;
187 map->l_dupsearchlist = malloc (nduplist * sizeof (struct link_map *));
188 if (map->l_dupsearchlist == NULL)
189 _dl_signal_error (ENOMEM, map->l_name,
190 "cannot allocate symbol search list");
191 map->l_ndupsearchlist = nduplist;
193 for (nlist = 0; nlist < naux + 1 + npreloads; ++nlist)
194 map->l_dupsearchlist[nlist] = head[nlist].map;
195 for (scanp = duphead.next; scanp; scanp = scanp->next)
196 map->l_dupsearchlist[nlist++] = scanp->map;