1 | /* Call the termination functions of loaded shared objects. |
2 | Copyright (C) 1995-2020 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, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <assert.h> |
20 | #include <string.h> |
21 | #include <ldsodefs.h> |
22 | #include <elf-initfini.h> |
23 | |
24 | |
25 | /* Type of the constructor functions. */ |
26 | typedef void (*fini_t) (void); |
27 | |
28 | |
29 | void |
30 | _dl_fini (void) |
31 | { |
32 | /* Lots of fun ahead. We have to call the destructors for all still |
33 | loaded objects, in all namespaces. The problem is that the ELF |
34 | specification now demands that dependencies between the modules |
35 | are taken into account. I.e., the destructor for a module is |
36 | called before the ones for any of its dependencies. |
37 | |
38 | To make things more complicated, we cannot simply use the reverse |
39 | order of the constructors. Since the user might have loaded objects |
40 | using `dlopen' there are possibly several other modules with its |
41 | dependencies to be taken into account. Therefore we have to start |
42 | determining the order of the modules once again from the beginning. */ |
43 | |
44 | /* We run the destructors of the main namespaces last. As for the |
45 | other namespaces, we pick run the destructors in them in reverse |
46 | order of the namespace ID. */ |
47 | #ifdef SHARED |
48 | int do_audit = 0; |
49 | again: |
50 | #endif |
51 | for (Lmid_t ns = GL(dl_nns) - 1; ns >= 0; --ns) |
52 | { |
53 | /* Protect against concurrent loads and unloads. */ |
54 | __rtld_lock_lock_recursive (GL(dl_load_lock)); |
55 | |
56 | unsigned int nloaded = GL(dl_ns)[ns]._ns_nloaded; |
57 | /* No need to do anything for empty namespaces or those used for |
58 | auditing DSOs. */ |
59 | if (nloaded == 0 |
60 | #ifdef SHARED |
61 | || GL(dl_ns)[ns]._ns_loaded->l_auditing != do_audit |
62 | #endif |
63 | ) |
64 | __rtld_lock_unlock_recursive (GL(dl_load_lock)); |
65 | else |
66 | { |
67 | /* Now we can allocate an array to hold all the pointers and |
68 | copy the pointers in. */ |
69 | struct link_map *maps[nloaded]; |
70 | |
71 | unsigned int i; |
72 | struct link_map *l; |
73 | assert (nloaded != 0 || GL(dl_ns)[ns]._ns_loaded == NULL); |
74 | for (l = GL(dl_ns)[ns]._ns_loaded, i = 0; l != NULL; l = l->l_next) |
75 | /* Do not handle ld.so in secondary namespaces. */ |
76 | if (l == l->l_real) |
77 | { |
78 | assert (i < nloaded); |
79 | |
80 | maps[i] = l; |
81 | l->l_idx = i; |
82 | ++i; |
83 | |
84 | /* Bump l_direct_opencount of all objects so that they |
85 | are not dlclose()ed from underneath us. */ |
86 | ++l->l_direct_opencount; |
87 | } |
88 | assert (ns != LM_ID_BASE || i == nloaded); |
89 | assert (ns == LM_ID_BASE || i == nloaded || i == nloaded - 1); |
90 | unsigned int nmaps = i; |
91 | |
92 | /* Now we have to do the sorting. We can skip looking for the |
93 | binary itself which is at the front of the search list for |
94 | the main namespace. */ |
95 | _dl_sort_maps (maps + (ns == LM_ID_BASE), nmaps - (ns == LM_ID_BASE), |
96 | NULL, true); |
97 | |
98 | /* We do not rely on the linked list of loaded object anymore |
99 | from this point on. We have our own list here (maps). The |
100 | various members of this list cannot vanish since the open |
101 | count is too high and will be decremented in this loop. So |
102 | we release the lock so that some code which might be called |
103 | from a destructor can directly or indirectly access the |
104 | lock. */ |
105 | __rtld_lock_unlock_recursive (GL(dl_load_lock)); |
106 | |
107 | /* 'maps' now contains the objects in the right order. Now |
108 | call the destructors. We have to process this array from |
109 | the front. */ |
110 | for (i = 0; i < nmaps; ++i) |
111 | { |
112 | struct link_map *l = maps[i]; |
113 | |
114 | if (l->l_init_called) |
115 | { |
116 | /* Make sure nothing happens if we are called twice. */ |
117 | l->l_init_called = 0; |
118 | |
119 | /* Is there a destructor function? */ |
120 | if (l->l_info[DT_FINI_ARRAY] != NULL |
121 | || (ELF_INITFINI && l->l_info[DT_FINI] != NULL)) |
122 | { |
123 | /* When debugging print a message first. */ |
124 | if (__builtin_expect (GLRO(dl_debug_mask) |
125 | & DL_DEBUG_IMPCALLS, 0)) |
126 | _dl_debug_printf ("\ncalling fini: %s [%lu]\n\n" , |
127 | DSO_FILENAME (l->l_name), |
128 | ns); |
129 | |
130 | /* First see whether an array is given. */ |
131 | if (l->l_info[DT_FINI_ARRAY] != NULL) |
132 | { |
133 | ElfW(Addr) *array = |
134 | (ElfW(Addr) *) (l->l_addr |
135 | + l->l_info[DT_FINI_ARRAY]->d_un.d_ptr); |
136 | unsigned int i = (l->l_info[DT_FINI_ARRAYSZ]->d_un.d_val |
137 | / sizeof (ElfW(Addr))); |
138 | while (i-- > 0) |
139 | ((fini_t) array[i]) (); |
140 | } |
141 | |
142 | /* Next try the old-style destructor. */ |
143 | if (ELF_INITFINI && l->l_info[DT_FINI] != NULL) |
144 | DL_CALL_DT_FINI |
145 | (l, l->l_addr + l->l_info[DT_FINI]->d_un.d_ptr); |
146 | } |
147 | |
148 | #ifdef SHARED |
149 | /* Auditing checkpoint: another object closed. */ |
150 | if (!do_audit && __builtin_expect (GLRO(dl_naudit) > 0, 0)) |
151 | { |
152 | struct audit_ifaces *afct = GLRO(dl_audit); |
153 | for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) |
154 | { |
155 | if (afct->objclose != NULL) |
156 | { |
157 | struct auditstate *state |
158 | = link_map_audit_state (l, cnt); |
159 | /* Return value is ignored. */ |
160 | (void) afct->objclose (&state->cookie); |
161 | } |
162 | afct = afct->next; |
163 | } |
164 | } |
165 | #endif |
166 | } |
167 | |
168 | /* Correct the previous increment. */ |
169 | --l->l_direct_opencount; |
170 | } |
171 | } |
172 | } |
173 | |
174 | #ifdef SHARED |
175 | if (! do_audit && GLRO(dl_naudit) > 0) |
176 | { |
177 | do_audit = 1; |
178 | goto again; |
179 | } |
180 | |
181 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS)) |
182 | _dl_debug_printf ("\nruntime linker statistics:\n" |
183 | " final number of relocations: %lu\n" |
184 | "final number of relocations from cache: %lu\n" , |
185 | GL(dl_num_relocations), |
186 | GL(dl_num_cache_relocations)); |
187 | #endif |
188 | } |
189 | |