3dc1db16c9ffb50645c923cc8c2450a5f1529c62
[jlayton/glibc.git] / elf / dl-fini.c
1 /* Call the termination functions of loaded shared objects.
2    Copyright (C) 1995,96,1998-2002,2004 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 Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the 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    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <alloca.h>
21 #include <assert.h>
22 #include <string.h>
23 #include <ldsodefs.h>
24
25
26 /* Type of the constructor functions.  */
27 typedef void (*fini_t) (void);
28
29
30 void
31 internal_function
32 _dl_fini (void)
33 {
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.
39
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;
48
49   /* Protect against concurrent loads and unloads.  */
50   __rtld_lock_lock_recursive (GL(dl_load_lock));
51
52   /* XXX Could it be (in static binaries) that there is no object loaded?  */
53   assert (GL(dl_nloaded) > 0);
54
55   /* Now we can allocate an array to hold all the pointers and copy
56      the pointers in.  */
57   maps = (struct link_map **) alloca (GL(dl_nloaded)
58                                       * sizeof (struct link_map *));
59   for (l = GL(dl_loaded), i = 0; l != NULL; l = l->l_next)
60     {
61       assert (i < GL(dl_nloaded));
62
63       maps[i++] = l;
64
65       /* Bump l_opencount of all objects so that they are not dlclose()ed
66          from underneath us.  */
67       ++l->l_opencount;
68     }
69   assert (i == GL(dl_nloaded));
70
71   /* Now we have to do the sorting.  */
72   for (l = GL(dl_loaded)->l_next; l != NULL; l = l->l_next)
73     {
74       unsigned int j;
75       unsigned int k;
76
77       /* Find the place in the 'maps' array.  */
78       for (j = 1; maps[j] != l; ++j)
79         ;
80
81       /* Find all object for which the current one is a dependency and
82          move the found object (if necessary) in front.  */
83       for (k = j + 1; k < GL(dl_nloaded); ++k)
84         {
85           struct link_map **runp = maps[k]->l_initfini;
86           if (runp != NULL)
87             {
88               while (*runp != NULL)
89                 if (*runp == l)
90                   {
91                     struct link_map *here = maps[k];
92
93                     /* Move it now.  */
94                     memmove (&maps[j] + 1,
95                              &maps[j],
96                              (k - j) * sizeof (struct link_map *));
97                     maps[j++] = here;
98
99                     break;
100                   }
101                 else
102                   ++runp;
103             }
104
105           if (__builtin_expect (maps[k]->l_reldeps != NULL, 0))
106             {
107               unsigned int m = maps[k]->l_reldepsact;
108               struct link_map **relmaps = maps[k]->l_reldeps;
109
110               while (m-- > 0)
111                 {
112                   if (relmaps[m] == l)
113                     {
114                       struct link_map *here = maps[k];
115
116                       /* Move it now.  */
117                       memmove (&maps[j] + 1,
118                                &maps[j],
119                                (k - j) * sizeof (struct link_map *));
120                       maps[j] = here;
121
122                       break;
123                     }
124                 }
125             }
126         }
127     }
128
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 < GL(dl_nloaded); ++i)
132     {
133       l = maps[i];
134
135       if (l->l_init_called)
136         {
137           /* Make sure nothing happens if we are called twice.  */
138           l->l_init_called = 0;
139
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;
143
144           /* Is there a destructor function?  */
145           if (l->l_info[DT_FINI_ARRAY] == NULL && l->l_info[DT_FINI] == NULL)
146             continue;
147
148           /* When debugging print a message first.  */
149           if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS, 0))
150             INTUSE(_dl_debug_printf) ("\ncalling fini: %s\n\n",
151                                       l->l_name[0]
152                                       ? l->l_name : rtld_progname);
153
154           /* First see whether an array is given.  */
155           if (l->l_info[DT_FINI_ARRAY] != NULL)
156             {
157               ElfW(Addr) *array =
158                 (ElfW(Addr) *) (l->l_addr
159                                 + l->l_info[DT_FINI_ARRAY]->d_un.d_ptr);
160               unsigned int i = (l->l_info[DT_FINI_ARRAYSZ]->d_un.d_val
161                                 / sizeof (ElfW(Addr)));
162               while (i-- > 0)
163                 ((fini_t) array[i]) ();
164             }
165
166           /* Next try the old-style destructor.  */
167           if (l->l_info[DT_FINI] != NULL)
168             ((fini_t) DL_DT_FINI_ADDRESS (l, l->l_addr + l->l_info[DT_FINI]->d_un.d_ptr)) ();
169         }
170
171       /* Correct the previous increment.  */
172       --l->l_opencount;
173     }
174
175   __rtld_lock_unlock_recursive (GL(dl_load_lock));
176
177   if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS, 0))
178     {
179       INTUSE(_dl_debug_printf) ("\nruntime linker statistics:\n");
180       INTUSE(_dl_debug_printf) ("\
181            final number of relocations: %lu\n",
182                                 GL(dl_num_relocations));
183       INTUSE(_dl_debug_printf) ("\
184 final number of relocations from cache: %lu\n",
185                                 GL(dl_num_cache_relocations));
186     }
187 }