Update.
[glibc.git] / elf / dl-fini.c
blobc7cc8d955578b031941657199a55f70973adefa0
1 /* Call the termination functions of loaded shared objects.
2 Copyright (C) 1995,96,98,99,2000,2001 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 <alloca.h>
21 #include <assert.h>
22 #include <string.h>
23 #include <ldsodefs.h>
26 /* Type of the constructor functions. */
27 typedef void (*fini_t) (void);
30 void
31 internal_function
32 _dl_fini (void)
34 /* Lots of fun ahead. We have to call the destructors for all still
35 loaded objects. The problem is that the ELF specification now
36 demands that dependencies between the modules are taken into account.
37 I.e., the destructor for a module is called before the ones for any
38 of its dependencies.
40 To make things more complicated, we cannot simply use the reverse
41 order of the constructors. Since the user might have loaded objects
42 using `dlopen' there are possibly several other modules with its
43 dependencies to be taken into account. Therefore we have to start
44 determining the order of the modules once again from the beginning. */
45 unsigned int i;
46 struct link_map *l;
47 struct link_map **maps;
49 /* XXX Could it be (in static binaries) that there is no object loaded? */
50 assert (_dl_nloaded > 0);
52 /* Now we can allocate an array to hold all the pointers and copy
53 the pointers in. */
54 maps = (struct link_map **) alloca (_dl_nloaded
55 * sizeof (struct link_map *));
56 for (l = _dl_loaded, i = 0; l != NULL; l = l->l_next)
58 assert (i < _dl_nloaded);
60 maps[i++] = l;
62 /* Bump l_opencount of all objects so that they are not dlclose()ed
63 from underneath us. */
64 ++l->l_opencount;
66 assert (i == _dl_nloaded);
68 /* Now we have to do the sorting. */
69 for (l = _dl_loaded->l_next; l != NULL; l = l->l_next)
71 unsigned int j;
72 unsigned int k;
74 /* Find the place in the `maps' array. */
75 for (j = 1; maps[j] != l; ++j)
78 /* Find all object for which the current one is a dependency and
79 move the found object (if necessary) in front. */
80 for (k = j + 1; k < _dl_nloaded; ++k)
82 struct link_map **runp;
84 runp = maps[k]->l_initfini;
85 if (runp != NULL)
87 while (*runp != NULL)
88 if (*runp == l)
90 struct link_map *here = maps[k];
92 /* Move it now. */
93 memmove (&maps[j] + 1,
94 &maps[j],
95 (k - j) * sizeof (struct link_map *));
96 maps[j++] = here;
98 break;
100 else
101 ++runp;
104 if (__builtin_expect (maps[k]->l_reldeps != NULL, 0))
106 unsigned int m = maps[k]->l_reldepsact;
107 struct link_map **relmaps = maps[k]->l_reldeps;
109 while (m-- > 0)
111 if (relmaps[m] == l)
113 struct link_map *here = maps[k];
115 /* Move it now. */
116 memmove (&maps[j] + 1,
117 &maps[j],
118 (k - j) * sizeof (struct link_map *));
119 maps[j] = here;
121 break;
129 /* `maps' now contains the objects in the right order. Now call the
130 destructors. We have to process this array from the front. */
131 for (i = 0; i < _dl_nloaded; ++i)
133 l = maps[i];
135 if (l->l_init_called)
137 /* Make sure nothing happens if we are called twice. */
138 l->l_init_called = 0;
140 /* Don't call the destructors for objects we are not supposed to. */
141 if (l->l_name[0] == '\0' && l->l_type == lt_executable)
142 continue;
144 /* Is there a destructor function? */
145 if (l->l_info[DT_FINI_ARRAY] == NULL && l->l_info[DT_FINI] == NULL)
146 continue;
148 /* When debugging print a message first. */
149 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_IMPCALLS, 0))
150 _dl_debug_printf ("\ncalling fini: %s\n\n",
151 l->l_name[0] ? l->l_name : _dl_argv[0]);
153 /* First see whether an array is given. */
154 if (l->l_info[DT_FINI_ARRAY] != NULL)
156 ElfW(Addr) *array =
157 (ElfW(Addr) *) (l->l_addr
158 + l->l_info[DT_FINI_ARRAY]->d_un.d_ptr);
159 unsigned int sz = (l->l_info[DT_FINI_ARRAYSZ]->d_un.d_val
160 / sizeof (ElfW(Addr)));
161 unsigned int cnt;
163 for (cnt = 0; cnt < sz; ++cnt)
164 ((fini_t) (l->l_addr + array[cnt])) ();
167 /* Next try the old-style destructor. */
168 if (l->l_info[DT_FINI] != NULL)
169 ((fini_t) DL_DT_FINI_ADDRESS (l, l->l_addr + l->l_info[DT_FINI]->d_un.d_ptr)) ();