Sat Jul 13 03:50:53 1996 Ulrich Drepper <drepper@cygnus.com>
[glibc/history.git] / elf / dl-deps.c
blob28733ab60d61a733c7f5aee4cd58f113633edcc5
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.
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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, 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)
29 struct list
31 struct link_map *map;
32 struct list *next;
34 struct list head[1 + npreloads], *tailp, *scanp;
35 unsigned int nlist;
37 /* Start the search list with one element: MAP itself. */
38 head[0].map = map;
40 /* Add the preloaded items after MAP but before any of its dependencies. */
41 for (nlist = 0; nlist < npreloads; ++nlist)
43 head[nlist].next = &head[nlist + 1];
44 head[nlist + 1].map = preloads[nlist];
47 /* Terminate the list. */
48 head[nlist++].next = NULL;
50 /* We use `l_reserved' as a mark bit to detect objects we have already
51 put in the search list and avoid adding duplicate elements later in
52 the list. */
53 map->l_reserved = 1;
55 /* Process each element of the search list, loading each of its immediate
56 dependencies and appending them to the list as we step through it.
57 This produces a flat, ordered list that represents a breadth-first
58 search of the dependency tree. */
59 for (scanp = tailp = head; scanp; scanp = scanp->next)
61 struct link_map *l = scanp->map;
63 if (l->l_info[DT_NEEDED])
65 const char *strtab
66 = ((void *) l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr);
67 const ElfW(Dyn) *d;
68 for (d = l->l_ld; d->d_tag != DT_NULL; ++d)
69 if (d->d_tag == DT_NEEDED)
71 /* Map in the needed object. */
72 struct link_map *dep
73 = _dl_map_object (l, strtab + d->d_un.d_val,
74 l->l_type == lt_executable ? lt_library :
75 l->l_type);
77 if (dep->l_reserved)
78 /* This object is already in the search list we are
79 building. Don't add a duplicate pointer. Release the
80 reference just added by _dl_map_object. */
81 --dep->l_opencount;
82 else
84 /* Append DEP to the search list. */
85 tailp->next = alloca (sizeof *tailp);
86 tailp = tailp->next;
87 tailp->map = dep;
88 tailp->next = NULL;
89 ++nlist;
90 /* Set the mark bit that says it's already in the list. */
91 dep->l_reserved = 1;
97 /* Store the search list we built in the object. It will be used for
98 searches in the scope of this object. */
99 map->l_searchlist = malloc (nlist * sizeof (struct link_map *));
100 map->l_nsearchlist = nlist;
102 nlist = 0;
103 for (scanp = head; scanp; scanp = scanp->next)
105 map->l_searchlist[nlist++] = scanp->map;
107 /* Now clear all the mark bits we set in the objects on the search list
108 to avoid duplicates, so the next call starts fresh. */
109 scanp->map->l_reserved = 0;