]>
Commit | Line | Data |
---|---|---|
d66e34cd | 1 | /* Call the termination functions of loaded shared objects. |
581c785b | 2 | Copyright (C) 1995-2022 Free Software Foundation, Inc. |
afd4eb37 | 3 | This file is part of the GNU C Library. |
d66e34cd | 4 | |
afd4eb37 | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
d66e34cd | 9 | |
afd4eb37 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
d66e34cd | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
d66e34cd | 18 | |
dacc8ffa UD |
19 | #include <assert.h> |
20 | #include <string.h> | |
a42195db | 21 | #include <ldsodefs.h> |
f4349837 | 22 | #include <elf-initfini.h> |
d66e34cd | 23 | |
dacc8ffa UD |
24 | |
25 | /* Type of the constructor functions. */ | |
26 | typedef void (*fini_t) (void); | |
27 | ||
28 | ||
d66e34cd RM |
29 | void |
30 | _dl_fini (void) | |
31 | { | |
dacc8ffa | 32 | /* Lots of fun ahead. We have to call the destructors for all still |
c0f62c56 UD |
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. | |
dacc8ffa UD |
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. */ | |
c0f62c56 | 43 | |
b1f68750 UD |
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. */ | |
e145f1cc UD |
47 | #ifdef SHARED |
48 | int do_audit = 0; | |
49 | again: | |
50 | #endif | |
22c83193 | 51 | for (Lmid_t ns = GL(dl_nns) - 1; ns >= 0; --ns) |
3b3938c9 | 52 | { |
c0f62c56 UD |
53 | /* Protect against concurrent loads and unloads. */ |
54 | __rtld_lock_lock_recursive (GL(dl_load_lock)); | |
1ebba33e | 55 | |
c3381f3e | 56 | unsigned int nloaded = GL(dl_ns)[ns]._ns_nloaded; |
9dcafc55 UD |
57 | /* No need to do anything for empty namespaces or those used for |
58 | auditing DSOs. */ | |
e145f1cc UD |
59 | if (nloaded == 0 |
60 | #ifdef SHARED | |
c3381f3e | 61 | || GL(dl_ns)[ns]._ns_loaded->l_auditing != do_audit |
e145f1cc UD |
62 | #endif |
63 | ) | |
58acfe6f FW |
64 | __rtld_lock_unlock_recursive (GL(dl_load_lock)); |
65 | else | |
c0f62c56 | 66 | { |
5fa11a2b AZ |
67 | #ifdef SHARED |
68 | _dl_audit_activity_nsid (ns, LA_ACT_DELETE); | |
69 | #endif | |
70 | ||
58acfe6f FW |
71 | /* Now we can allocate an array to hold all the pointers and |
72 | copy the pointers in. */ | |
73 | struct link_map *maps[nloaded]; | |
74 | ||
75 | unsigned int i; | |
76 | struct link_map *l; | |
77 | assert (nloaded != 0 || GL(dl_ns)[ns]._ns_loaded == NULL); | |
78 | for (l = GL(dl_ns)[ns]._ns_loaded, i = 0; l != NULL; l = l->l_next) | |
79 | /* Do not handle ld.so in secondary namespaces. */ | |
80 | if (l == l->l_real) | |
81 | { | |
82 | assert (i < nloaded); | |
83 | ||
84 | maps[i] = l; | |
85 | l->l_idx = i; | |
86 | ++i; | |
87 | ||
88 | /* Bump l_direct_opencount of all objects so that they | |
89 | are not dlclose()ed from underneath us. */ | |
90 | ++l->l_direct_opencount; | |
91 | } | |
92 | assert (ns != LM_ID_BASE || i == nloaded); | |
93 | assert (ns == LM_ID_BASE || i == nloaded || i == nloaded - 1); | |
94 | unsigned int nmaps = i; | |
95 | ||
c2c299fd AS |
96 | /* Now we have to do the sorting. We can skip looking for the |
97 | binary itself which is at the front of the search list for | |
98 | the main namespace. */ | |
15a0c573 | 99 | _dl_sort_maps (maps, nmaps, (ns == LM_ID_BASE), true); |
58acfe6f FW |
100 | |
101 | /* We do not rely on the linked list of loaded object anymore | |
102 | from this point on. We have our own list here (maps). The | |
103 | various members of this list cannot vanish since the open | |
104 | count is too high and will be decremented in this loop. So | |
105 | we release the lock so that some code which might be called | |
106 | from a destructor can directly or indirectly access the | |
107 | lock. */ | |
108 | __rtld_lock_unlock_recursive (GL(dl_load_lock)); | |
109 | ||
110 | /* 'maps' now contains the objects in the right order. Now | |
111 | call the destructors. We have to process this array from | |
112 | the front. */ | |
113 | for (i = 0; i < nmaps; ++i) | |
c0f62c56 | 114 | { |
58acfe6f | 115 | struct link_map *l = maps[i]; |
dacc8ffa | 116 | |
58acfe6f | 117 | if (l->l_init_called) |
c0f62c56 | 118 | { |
58acfe6f FW |
119 | /* Make sure nothing happens if we are called twice. */ |
120 | l->l_init_called = 0; | |
121 | ||
122 | /* Is there a destructor function? */ | |
123 | if (l->l_info[DT_FINI_ARRAY] != NULL | |
f4349837 | 124 | || (ELF_INITFINI && l->l_info[DT_FINI] != NULL)) |
9dcafc55 | 125 | { |
58acfe6f FW |
126 | /* When debugging print a message first. */ |
127 | if (__builtin_expect (GLRO(dl_debug_mask) | |
128 | & DL_DEBUG_IMPCALLS, 0)) | |
129 | _dl_debug_printf ("\ncalling fini: %s [%lu]\n\n", | |
130 | DSO_FILENAME (l->l_name), | |
131 | ns); | |
132 | ||
133 | /* First see whether an array is given. */ | |
134 | if (l->l_info[DT_FINI_ARRAY] != NULL) | |
135 | { | |
136 | ElfW(Addr) *array = | |
137 | (ElfW(Addr) *) (l->l_addr | |
138 | + l->l_info[DT_FINI_ARRAY]->d_un.d_ptr); | |
139 | unsigned int i = (l->l_info[DT_FINI_ARRAYSZ]->d_un.d_val | |
140 | / sizeof (ElfW(Addr))); | |
141 | while (i-- > 0) | |
142 | ((fini_t) array[i]) (); | |
143 | } | |
144 | ||
145 | /* Next try the old-style destructor. */ | |
f4349837 | 146 | if (ELF_INITFINI && l->l_info[DT_FINI] != NULL) |
58acfe6f FW |
147 | DL_CALL_DT_FINI |
148 | (l, l->l_addr + l->l_info[DT_FINI]->d_un.d_ptr); | |
9dcafc55 UD |
149 | } |
150 | ||
9dcafc55 | 151 | #ifdef SHARED |
58acfe6f | 152 | /* Auditing checkpoint: another object closed. */ |
311c9ee5 | 153 | _dl_audit_objclose (l); |
9dcafc55 | 154 | #endif |
58acfe6f | 155 | } |
d66e34cd | 156 | |
58acfe6f FW |
157 | /* Correct the previous increment. */ |
158 | --l->l_direct_opencount; | |
159 | } | |
5fa11a2b AZ |
160 | |
161 | #ifdef SHARED | |
162 | _dl_audit_activity_nsid (ns, LA_ACT_CONSISTENT); | |
163 | #endif | |
dacc8ffa UD |
164 | } |
165 | } | |
9836cfe7 | 166 | |
e145f1cc UD |
167 | #ifdef SHARED |
168 | if (! do_audit && GLRO(dl_naudit) > 0) | |
169 | { | |
170 | do_audit = 1; | |
171 | goto again; | |
172 | } | |
e145f1cc | 173 | |
a1ffb40e | 174 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS)) |
42af49f8 UD |
175 | _dl_debug_printf ("\nruntime linker statistics:\n" |
176 | " final number of relocations: %lu\n" | |
177 | "final number of relocations from cache: %lu\n", | |
178 | GL(dl_num_relocations), | |
179 | GL(dl_num_cache_relocations)); | |
c3381f3e | 180 | #endif |
d66e34cd | 181 | } |