]>
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 RM |
23 | |
24 | void | |
25 | _dl_fini (void) | |
26 | { | |
dacc8ffa | 27 | /* Lots of fun ahead. We have to call the destructors for all still |
c0f62c56 UD |
28 | loaded objects, in all namespaces. The problem is that the ELF |
29 | specification now demands that dependencies between the modules | |
30 | are taken into account. I.e., the destructor for a module is | |
31 | called before the ones for any of its dependencies. | |
dacc8ffa UD |
32 | |
33 | To make things more complicated, we cannot simply use the reverse | |
34 | order of the constructors. Since the user might have loaded objects | |
35 | using `dlopen' there are possibly several other modules with its | |
36 | dependencies to be taken into account. Therefore we have to start | |
37 | determining the order of the modules once again from the beginning. */ | |
c0f62c56 | 38 | |
b1f68750 UD |
39 | /* We run the destructors of the main namespaces last. As for the |
40 | other namespaces, we pick run the destructors in them in reverse | |
41 | order of the namespace ID. */ | |
e145f1cc UD |
42 | #ifdef SHARED |
43 | int do_audit = 0; | |
44 | again: | |
45 | #endif | |
22c83193 | 46 | for (Lmid_t ns = GL(dl_nns) - 1; ns >= 0; --ns) |
3b3938c9 | 47 | { |
c0f62c56 UD |
48 | /* Protect against concurrent loads and unloads. */ |
49 | __rtld_lock_lock_recursive (GL(dl_load_lock)); | |
1ebba33e | 50 | |
c3381f3e | 51 | unsigned int nloaded = GL(dl_ns)[ns]._ns_nloaded; |
9dcafc55 UD |
52 | /* No need to do anything for empty namespaces or those used for |
53 | auditing DSOs. */ | |
e145f1cc UD |
54 | if (nloaded == 0 |
55 | #ifdef SHARED | |
c3381f3e | 56 | || GL(dl_ns)[ns]._ns_loaded->l_auditing != do_audit |
e145f1cc UD |
57 | #endif |
58 | ) | |
58acfe6f FW |
59 | __rtld_lock_unlock_recursive (GL(dl_load_lock)); |
60 | else | |
c0f62c56 | 61 | { |
5fa11a2b AZ |
62 | #ifdef SHARED |
63 | _dl_audit_activity_nsid (ns, LA_ACT_DELETE); | |
64 | #endif | |
65 | ||
58acfe6f FW |
66 | /* Now we can allocate an array to hold all the pointers and |
67 | copy the pointers in. */ | |
68 | struct link_map *maps[nloaded]; | |
69 | ||
70 | unsigned int i; | |
71 | struct link_map *l; | |
72 | assert (nloaded != 0 || GL(dl_ns)[ns]._ns_loaded == NULL); | |
73 | for (l = GL(dl_ns)[ns]._ns_loaded, i = 0; l != NULL; l = l->l_next) | |
74 | /* Do not handle ld.so in secondary namespaces. */ | |
75 | if (l == l->l_real) | |
76 | { | |
77 | assert (i < nloaded); | |
78 | ||
79 | maps[i] = l; | |
80 | l->l_idx = i; | |
81 | ++i; | |
82 | ||
83 | /* Bump l_direct_opencount of all objects so that they | |
84 | are not dlclose()ed from underneath us. */ | |
85 | ++l->l_direct_opencount; | |
86 | } | |
87 | assert (ns != LM_ID_BASE || i == nloaded); | |
88 | assert (ns == LM_ID_BASE || i == nloaded || i == nloaded - 1); | |
89 | unsigned int nmaps = i; | |
90 | ||
c2c299fd AS |
91 | /* Now we have to do the sorting. We can skip looking for the |
92 | binary itself which is at the front of the search list for | |
93 | the main namespace. */ | |
15a0c573 | 94 | _dl_sort_maps (maps, nmaps, (ns == LM_ID_BASE), true); |
58acfe6f FW |
95 | |
96 | /* We do not rely on the linked list of loaded object anymore | |
97 | from this point on. We have our own list here (maps). The | |
98 | various members of this list cannot vanish since the open | |
99 | count is too high and will be decremented in this loop. So | |
100 | we release the lock so that some code which might be called | |
101 | from a destructor can directly or indirectly access the | |
102 | lock. */ | |
103 | __rtld_lock_unlock_recursive (GL(dl_load_lock)); | |
104 | ||
105 | /* 'maps' now contains the objects in the right order. Now | |
106 | call the destructors. We have to process this array from | |
107 | the front. */ | |
108 | for (i = 0; i < nmaps; ++i) | |
c0f62c56 | 109 | { |
58acfe6f | 110 | struct link_map *l = maps[i]; |
dacc8ffa | 111 | |
58acfe6f | 112 | if (l->l_init_called) |
c0f62c56 | 113 | { |
6f360366 | 114 | _dl_call_fini (l); |
9dcafc55 | 115 | #ifdef SHARED |
58acfe6f | 116 | /* Auditing checkpoint: another object closed. */ |
311c9ee5 | 117 | _dl_audit_objclose (l); |
9dcafc55 | 118 | #endif |
58acfe6f | 119 | } |
d66e34cd | 120 | |
58acfe6f FW |
121 | /* Correct the previous increment. */ |
122 | --l->l_direct_opencount; | |
123 | } | |
5fa11a2b AZ |
124 | |
125 | #ifdef SHARED | |
126 | _dl_audit_activity_nsid (ns, LA_ACT_CONSISTENT); | |
127 | #endif | |
dacc8ffa UD |
128 | } |
129 | } | |
9836cfe7 | 130 | |
e145f1cc UD |
131 | #ifdef SHARED |
132 | if (! do_audit && GLRO(dl_naudit) > 0) | |
133 | { | |
134 | do_audit = 1; | |
135 | goto again; | |
136 | } | |
e145f1cc | 137 | |
a1ffb40e | 138 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS)) |
42af49f8 UD |
139 | _dl_debug_printf ("\nruntime linker statistics:\n" |
140 | " final number of relocations: %lu\n" | |
141 | "final number of relocations from cache: %lu\n", | |
142 | GL(dl_num_relocations), | |
143 | GL(dl_num_cache_relocations)); | |
c3381f3e | 144 | #endif |
d66e34cd | 145 | } |