Update.
[platform/upstream/glibc.git] / elf / dl-deps.c
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.
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.  */
19
20 #include <dlfcn.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <elf/ldsodefs.h>
25
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))
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))
36
37
38 /* When loading auxiliary objects we must ignore errors.  It's ok if
39    an object is missing.  */
40 struct openaux_args
41   {
42     /* The arguments to openaux.  */
43     struct link_map *map;
44     int trace_mode;
45     const char *strtab;
46     const ElfW(Dyn) *d;
47
48     /* The return value of openaux.  */
49     struct link_map *aux;
50   };
51
52 static void
53 openaux (void *a)
54 {
55   struct openaux_args *args = (struct openaux_args *) a;
56
57   args->aux = _dl_map_object (args->map, args->strtab + args->d->d_un.d_val, 0,
58                               (args->map->l_type == lt_executable
59                                ? lt_library : args->map->l_type),
60                               args->trace_mode);
61 }
62
63
64
65 /* We use a very special kind of list to track the two kinds paths
66    through the list of loaded shared objects.  We have to
67
68    - produce a flat list with unique members of all involved objects
69
70    - produce a flat list of all shared objects.
71 */
72 struct 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
82 void
83 internal_function
84 _dl_map_object_deps (struct link_map *map,
85                      struct link_map **preloads, unsigned int npreloads,
86                      int trace_mode)
87 {
88   struct list known[1 + npreloads + 1];
89   struct list *runp, *utail, *dtail;
90   unsigned int nlist, nduplist, i;
91
92   inline void preload (struct link_map *map)
93     {
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];
99
100       ++nlist;
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     }
106
107   /* No loaded object so far.  */
108   nlist = 0;
109
110   /* First load MAP itself.  */
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
117   /* Terminate the lists.  */
118   known[nlist - 1].unique = NULL;
119   known[nlist - 1].dup = NULL;
120
121   /* Pointer to last unique object.  */
122   utail = &known[nlist - 1];
123   /* Pointer to last loaded object.  */
124   dtail = &known[nlist - 1];
125
126   /* Until now we have the same number of libraries in the normal and
127      the list with duplicates.  */
128   nduplist = nlist;
129
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; )
141     {
142       struct link_map *l = runp->map;
143
144       if (l->l_info[DT_NEEDED] || l->l_info[AUXTAG] || l->l_info[FILTERTAG])
145         {
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;
150           const ElfW(Dyn) *d;
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
160           for (d = l->l_ld; d->d_tag != DT_NULL; ++d)
161             if (d->d_tag == DT_NEEDED)
162               {
163                 /* Map in the needed object.  */
164                 struct link_map *dep
165                   = _dl_map_object (l, strtab + d->d_un.d_val, 0,
166                                     l->l_type == lt_executable ? lt_library :
167                                     l->l_type, trace_mode);
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;
177
178                 if (dep->l_reserved)
179                   /* This object is already in the search list we are
180                      building.  Don't add a duplicate pointer.
181                      Release the reference just added by
182                      _dl_map_object.  */
183                   --dep->l_opencount;
184                 else
185                   {
186                     /* Append DEP to the unique list.  */
187                     newp->done = 0;
188                     newp->unique = NULL;
189                     utail->unique = newp;
190                     utail = newp;
191                     ++nlist;
192                     /* Set the mark bit that says it's already in the list.  */
193                     dep->l_reserved = 1;
194                   }
195               }
196             else if (d->d_tag == DT_AUXILIARY || d->d_tag == DT_FILTER)
197               {
198                 char *errstring;
199                 struct list *newp;
200
201                 if (d->d_tag == DT_AUXILIARY)
202                   {
203                     /* Store the tag in the argument structure.  */
204                     args.d = d;
205
206                     /* Say that we are about to load an auxiliary library.  */
207                     if (_dl_debug_libs)
208                       _dl_debug_message (1, "load auxiliary object=",
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
215                     /* We must be prepared that the addressed shared
216                        object is not available.  */
217                     if (_dl_catch_error (&errstring, openaux, &args))
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                       }
226                   }
227                 else
228                   {
229                     /* Say that we are about to load an auxiliary library.  */
230                     if (_dl_debug_libs)
231                       _dl_debug_message (1, "load filtered object=",
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                   }
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.  */
252                 orig->dup = memcpy (newp, orig, sizeof (*newp));
253
254                 /* Initialize new entry.  */
255                 orig->done = 0;
256                 orig->map = args.aux;
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)
269                   {
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)
285                       {
286                         /* The object is somewhere behind the current
287                            position in the search path.  We have to
288                            move it to this earlier position.  */
289                         orig->unique = newp;
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.  */
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                       }
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.  */
320                         late = known;
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                   }
348
349                 /* Move the tail pointers if necessary.  */
350                 if (orig == utail)
351                   utail = newp;
352                 if (orig == dtail)
353                   dtail = newp;
354
355                 /* Move on the insert point.  */
356                 orig = newp;
357
358                 /* We always add an entry to the duplicate list.  */
359                 ++nduplist;
360               }
361         }
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;
370         while (runp != NULL && runp->done);
371     }
372
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 *));
376   if (map->l_searchlist == NULL)
377     _dl_signal_error (ENOMEM, map->l_name,
378                       "cannot allocate symbol search list");
379   map->l_nsearchlist = nlist;
380
381   for (nlist = 0, runp = known; runp; runp = runp->unique)
382     {
383       map->l_searchlist[nlist++] = runp->map;
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.  */
387       runp->map->l_reserved = 0;
388     }
389
390   map->l_ndupsearchlist = nduplist;
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");
399
400       for (nlist = 0, runp = known; runp; runp = runp->dup)
401         map->l_dupsearchlist[nlist++] = runp->map;
402     }
403 }