update from main archive 961116
[platform/upstream/glibc.git] / elf / dl-deps.c
1 /* Load the dependencies of a mapped object.
2    Copyright (C) 1996 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 <link.h>
21 #include <errno.h>
22 #include <dlfcn.h>
23 #include <stdlib.h>
24
25 void
26 _dl_map_object_deps (struct link_map *map,
27                      struct link_map **preloads, unsigned int npreloads,
28                      int trace_mode)
29 {
30   struct list
31     {
32       struct link_map *map;
33       struct list *next;
34     };
35   struct list head[1 + npreloads], *tailp, *scanp;
36   unsigned int nlist;
37
38   /* Start the search list with one element: MAP itself.  */
39   head[0].map = map;
40
41   /* Add the preloaded items after MAP but before any of its dependencies.  */
42   for (nlist = 0; nlist < npreloads; ++nlist)
43     {
44       head[nlist].next = &head[nlist + 1];
45       head[nlist + 1].map = preloads[nlist];
46     }
47
48   /* Terminate the list.  */
49   head[nlist].next = NULL;
50
51   /* Start here for adding dependencies to the list.  */
52   tailp = &head[nlist++];
53
54   /* We use `l_reserved' as a mark bit to detect objects we have already
55      put in the search list and avoid adding duplicate elements later in
56      the list.  */
57   map->l_reserved = 1;
58
59   /* Process each element of the search list, loading each of its immediate
60      dependencies and appending them to the list as we step through it.
61      This produces a flat, ordered list that represents a breadth-first
62      search of the dependency tree.  */
63   for (scanp = head; scanp; scanp = scanp->next)
64     {
65       struct link_map *l = scanp->map;
66
67       if (l->l_info[DT_NEEDED])
68         {
69           const char *strtab
70             = ((void *) l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr);
71           const ElfW(Dyn) *d;
72           for (d = l->l_ld; d->d_tag != DT_NULL; ++d)
73             if (d->d_tag == DT_NEEDED)
74               {
75                 /* Map in the needed object.  */
76                 struct link_map *dep
77                   = _dl_map_object (l, strtab + d->d_un.d_val,
78                                     l->l_type == lt_executable ? lt_library :
79                                     l->l_type, trace_mode);
80
81                 if (dep->l_reserved)
82                   /* This object is already in the search list we are
83                      building.  Don't add a duplicate pointer.  Release the
84                      reference just added by _dl_map_object.  */
85                   --dep->l_opencount;
86                 else
87                   {
88                     /* Append DEP to the search list.  */
89                     tailp->next = alloca (sizeof *tailp);
90                     tailp = tailp->next;
91                     tailp->map = dep;
92                     tailp->next = NULL;
93                     ++nlist;
94                     /* Set the mark bit that says it's already in the list.  */
95                     dep->l_reserved = 1;
96                   }
97               }
98         }
99     }
100
101   /* Store the search list we built in the object.  It will be used for
102      searches in the scope of this object.  */
103   map->l_searchlist = malloc (nlist * sizeof (struct link_map *));
104   map->l_nsearchlist = nlist;
105
106   nlist = 0;
107   for (scanp = head; scanp; scanp = scanp->next)
108     {
109       map->l_searchlist[nlist++] = scanp->map;
110
111       /* Now clear all the mark bits we set in the objects on the search list
112          to avoid duplicates, so the next call starts fresh.  */
113       scanp->map->l_reserved = 0;
114     }
115 }