]>
Commit | Line | Data |
---|---|---|
9385eb3d | 1 | /*- |
e9ce8d39 A |
2 | * Copyright (c) 1990, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Chris Torek. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * 1. Redistributions of source code must retain the above copyright | |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
e9ce8d39 A |
16 | * 4. Neither the name of the University nor the names of its contributors |
17 | * may be used to endorse or promote products derived from this software | |
18 | * without specific prior written permission. | |
19 | * | |
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
30 | * SUCH DAMAGE. | |
31 | */ | |
32 | ||
70ad1dc8 A |
33 | #pragma clang diagnostic push |
34 | #pragma clang diagnostic ignored "-Wimplicit-function-declaration" | |
35 | #pragma clang diagnostic ignored "-Wstrict-prototypes" | |
36 | #pragma clang diagnostic ignored "-Winvalid-pp-token" | |
37 | #pragma clang diagnostic ignored "-Wint-conversion" | |
38 | ||
9385eb3d A |
39 | #if defined(LIBC_SCCS) && !defined(lint) |
40 | static char sccsid[] = "@(#)atexit.c 8.2 (Berkeley) 7/3/94"; | |
41 | #endif /* LIBC_SCCS and not lint */ | |
42 | #include <sys/cdefs.h> | |
1f2f436a | 43 | __FBSDID("$FreeBSD: src/lib/libc/stdlib/atexit.c,v 1.8 2007/01/09 00:28:09 imp Exp $"); |
e9ce8d39 | 44 | |
9385eb3d | 45 | #include "namespace.h" |
23e20b00 | 46 | #include <errno.h> |
e9ce8d39 A |
47 | #include <stddef.h> |
48 | #include <stdlib.h> | |
9385eb3d | 49 | #include <unistd.h> |
23e20b00 | 50 | #include <assert.h> |
9385eb3d | 51 | #include <pthread.h> |
ad3c9f2a A |
52 | #if defined(__DYNAMIC__) || defined (__BLOCKS__) |
53 | #include <dlfcn.h> | |
54 | #endif /* defined(__DYNAMIC__) */ | |
e9ce8d39 | 55 | #include "atexit.h" |
9385eb3d A |
56 | #include "un-namespace.h" |
57 | ||
ad3c9f2a A |
58 | #ifdef __BLOCKS__ |
59 | #include <Block.h> | |
23e20b00 | 60 | #include <Block_private.h> |
ad3c9f2a | 61 | #endif /* __BLOCKS__ */ |
9385eb3d | 62 | #include "libc_private.h" |
23e20b00 | 63 | #include <os/alloc_once_private.h> |
9385eb3d | 64 | |
6465356a A |
65 | #include <TargetConditionals.h> |
66 | ||
59e0d9fe A |
67 | #define ATEXIT_FN_EMPTY 0 |
68 | #define ATEXIT_FN_STD 1 | |
69 | #define ATEXIT_FN_CXA 2 | |
ad3c9f2a A |
70 | #ifdef __BLOCKS__ |
71 | #define ATEXIT_FN_BLK 3 | |
72 | #endif /* __BLOCKS__ */ | |
59e0d9fe | 73 | |
9385eb3d A |
74 | static pthread_mutex_t atexit_mutex = PTHREAD_MUTEX_INITIALIZER; |
75 | ||
76 | #define _MUTEX_LOCK(x) if (__isthreaded) _pthread_mutex_lock(x) | |
77 | #define _MUTEX_UNLOCK(x) if (__isthreaded) _pthread_mutex_unlock(x) | |
e9ce8d39 | 78 | |
59e0d9fe A |
79 | struct atexit { |
80 | struct atexit *next; /* next in list */ | |
81 | int ind; /* next index in this table */ | |
82 | struct atexit_fn { | |
83 | int fn_type; /* ATEXIT_? from above */ | |
84 | union { | |
85 | void (*std_func)(void); | |
86 | void (*cxa_func)(void *); | |
ad3c9f2a A |
87 | #ifdef __BLOCKS__ |
88 | void (^block)(void); | |
89 | #endif /* __BLOCKS__ */ | |
59e0d9fe A |
90 | } fn_ptr; /* function pointer */ |
91 | void *fn_arg; /* argument for CXA callback */ | |
92 | void *fn_dso; /* shared module handle */ | |
93 | } fns[ATEXIT_SIZE]; /* the table itself */ | |
94 | }; | |
95 | ||
96 | static struct atexit *__atexit; /* points to head of LIFO stack */ | |
23e20b00 A |
97 | static int __atexit_new_registration; |
98 | ||
99 | __attribute__ ((visibility ("hidden"))) | |
100 | void | |
101 | __atexit_init(void) | |
102 | { | |
103 | __atexit = os_alloc_once(OS_ALLOC_ONCE_KEY_LIBSYSTEM_C, | |
104 | sizeof(struct atexit), NULL); | |
105 | } | |
e9ce8d39 A |
106 | |
107 | /* | |
59e0d9fe A |
108 | * Register the function described by 'fptr' to be called at application |
109 | * exit or owning shared object unload time. This is a helper function | |
110 | * for atexit and __cxa_atexit. | |
e9ce8d39 | 111 | */ |
59e0d9fe A |
112 | static int |
113 | atexit_register(struct atexit_fn *fptr) | |
e9ce8d39 | 114 | { |
23e20b00 A |
115 | struct atexit *p = __atexit; |
116 | assert(p); | |
9385eb3d | 117 | _MUTEX_LOCK(&atexit_mutex); |
23e20b00 | 118 | while (p->ind >= ATEXIT_SIZE) { |
9385eb3d A |
119 | struct atexit *old__atexit; |
120 | old__atexit = __atexit; | |
121 | _MUTEX_UNLOCK(&atexit_mutex); | |
122 | if ((p = (struct atexit *)malloc(sizeof(*p))) == NULL) | |
e9ce8d39 | 123 | return (-1); |
9385eb3d A |
124 | _MUTEX_LOCK(&atexit_mutex); |
125 | if (old__atexit != __atexit) { | |
126 | /* Lost race, retry operation */ | |
127 | _MUTEX_UNLOCK(&atexit_mutex); | |
128 | free(p); | |
129 | _MUTEX_LOCK(&atexit_mutex); | |
130 | p = __atexit; | |
131 | continue; | |
132 | } | |
e9ce8d39 A |
133 | p->ind = 0; |
134 | p->next = __atexit; | |
135 | __atexit = p; | |
136 | } | |
59e0d9fe | 137 | p->fns[p->ind++] = *fptr; |
23e20b00 | 138 | __atexit_new_registration = 1; |
59e0d9fe A |
139 | _MUTEX_UNLOCK(&atexit_mutex); |
140 | return 0; | |
141 | } | |
142 | ||
143 | /* | |
144 | * Register a function to be performed at exit. | |
145 | */ | |
146 | int | |
147 | atexit(void (*func)(void)) | |
148 | { | |
149 | struct atexit_fn fn; | |
150 | int error; | |
151 | ||
152 | fn.fn_type = ATEXIT_FN_STD; | |
ad3c9f2a | 153 | fn.fn_ptr.std_func = func; |
59e0d9fe A |
154 | fn.fn_arg = NULL; |
155 | fn.fn_dso = NULL; | |
23e20b00 A |
156 | |
157 | #if defined(__DYNAMIC__) && !TARGET_OS_IPHONE | |
158 | // <rdar://problem/14596032&15173956> | |
159 | struct dl_info info; | |
160 | if (dladdr(func, &info)) { | |
161 | fn.fn_dso = info.dli_fbase; | |
162 | } | |
163 | #endif | |
59e0d9fe A |
164 | |
165 | error = atexit_register(&fn); | |
166 | return (error); | |
167 | } | |
168 | ||
ad3c9f2a A |
169 | #ifdef __BLOCKS__ |
170 | int | |
171 | atexit_b(void (^block)(void)) | |
172 | { | |
173 | struct atexit_fn fn; | |
ad3c9f2a A |
174 | int error; |
175 | ||
176 | fn.fn_type = ATEXIT_FN_BLK; | |
177 | fn.fn_ptr.block = Block_copy(block); | |
178 | fn.fn_arg = NULL; | |
ad3c9f2a | 179 | fn.fn_dso = NULL; |
ad3c9f2a A |
180 | |
181 | error = atexit_register(&fn); | |
182 | return (error); | |
183 | } | |
184 | #endif /* __BLOCKS__ */ | |
185 | ||
59e0d9fe A |
186 | /* |
187 | * Register a function to be performed at exit or when an shared object | |
188 | * with given dso handle is unloaded dynamically. | |
189 | */ | |
190 | int | |
191 | __cxa_atexit(void (*func)(void *), void *arg, void *dso) | |
192 | { | |
193 | struct atexit_fn fn; | |
194 | int error; | |
195 | ||
196 | fn.fn_type = ATEXIT_FN_CXA; | |
197 | fn.fn_ptr.cxa_func = func;; | |
198 | fn.fn_arg = arg; | |
199 | fn.fn_dso = dso; | |
200 | ||
201 | error = atexit_register(&fn); | |
202 | return (error); | |
203 | } | |
204 | ||
23e20b00 A |
205 | static bool |
206 | __cxa_in_range(const struct __cxa_range_t ranges[], | |
207 | unsigned int count, | |
208 | const void* fn) | |
209 | { | |
210 | uintptr_t addr = (uintptr_t)fn; | |
211 | ||
212 | unsigned int i; | |
213 | for (i = 0; i < count; ++i) { | |
214 | const struct __cxa_range_t *r = &ranges[i]; | |
215 | if (addr < (uintptr_t)r->addr) { | |
216 | continue; | |
217 | } | |
218 | if (addr < ((uintptr_t)r->addr + r->length)) { | |
219 | return true; | |
220 | } | |
221 | } | |
222 | return false; | |
223 | } | |
224 | ||
59e0d9fe | 225 | /* |
23e20b00 A |
226 | * Call handlers registered via __cxa_atexit/atexit that are in a |
227 | * a range specified. | |
228 | * Note: rangeCount==0, means call all handlers. | |
59e0d9fe A |
229 | */ |
230 | void | |
23e20b00 | 231 | __cxa_finalize_ranges(const struct __cxa_range_t ranges[], unsigned int count) |
59e0d9fe A |
232 | { |
233 | struct atexit *p; | |
23e20b00 | 234 | struct atexit_fn *fn; |
59e0d9fe | 235 | int n; |
59e0d9fe | 236 | _MUTEX_LOCK(&atexit_mutex); |
23e20b00 | 237 | |
ad3c9f2a | 238 | restart: |
59e0d9fe A |
239 | for (p = __atexit; p; p = p->next) { |
240 | for (n = p->ind; --n >= 0;) { | |
23e20b00 A |
241 | fn = &p->fns[n]; |
242 | ||
243 | if (fn->fn_type == ATEXIT_FN_EMPTY) { | |
244 | continue; // already been called | |
245 | } | |
246 | ||
247 | // Verify that the entry is within the range being unloaded. | |
248 | if (count > 0) { | |
249 | if (fn->fn_type == ATEXIT_FN_CXA) { | |
250 | // for __cxa_atexit(), call if *dso* is in range be unloaded | |
251 | if (!__cxa_in_range(ranges, count, fn->fn_dso)) { | |
252 | continue; // not being unloaded yet | |
253 | } | |
254 | } else if (fn->fn_type == ATEXIT_FN_STD) { | |
255 | // for atexit, call if *function* is in range be unloaded | |
256 | if (!__cxa_in_range(ranges, count, fn->fn_ptr.std_func)) { | |
257 | continue; // not being unloaded yet | |
258 | } | |
ad3c9f2a | 259 | #ifdef __BLOCKS__ |
23e20b00 A |
260 | } else if (fn->fn_type == ATEXIT_FN_BLK) { |
261 | // for atexit_b, call if block code is in range be unloaded | |
262 | void *a = ((struct Block_layout *)fn->fn_ptr.block)->invoke; | |
263 | if (!__cxa_in_range(ranges, count, a)) { | |
264 | continue; // not being unloaded yet | |
265 | } | |
266 | #endif // __BLOCKS__ | |
267 | } | |
268 | } | |
269 | ||
270 | // Clear the entry to indicate that this handler has been called. | |
271 | int fn_type = fn->fn_type; | |
272 | fn->fn_type = ATEXIT_FN_EMPTY; | |
273 | ||
274 | // Detect recursive registrations. | |
275 | __atexit_new_registration = 0; | |
276 | _MUTEX_UNLOCK(&atexit_mutex); | |
277 | ||
278 | // Call the handler. | |
279 | if (fn_type == ATEXIT_FN_CXA) { | |
280 | fn->fn_ptr.cxa_func(fn->fn_arg); | |
281 | } else if (fn_type == ATEXIT_FN_STD) { | |
282 | fn->fn_ptr.std_func(); | |
283 | #ifdef __BLOCKS__ | |
284 | } else if (fn_type == ATEXIT_FN_BLK) { | |
285 | fn->fn_ptr.block(); | |
286 | #endif // __BLOCKS__ | |
287 | } | |
288 | ||
289 | // Call any recursively registered handlers. | |
59e0d9fe | 290 | _MUTEX_LOCK(&atexit_mutex); |
23e20b00 | 291 | if (__atexit_new_registration) { |
ad3c9f2a | 292 | goto restart; |
23e20b00 | 293 | } |
59e0d9fe A |
294 | } |
295 | } | |
9385eb3d | 296 | _MUTEX_UNLOCK(&atexit_mutex); |
e9ce8d39 | 297 | } |
6465356a | 298 | |
23e20b00 A |
299 | |
300 | /* | |
301 | * Call all handlers registered with __cxa_atexit for the shared | |
302 | * object owning 'dso'. Note: if 'dso' is NULL, then all remaining | |
303 | * handlers are called. | |
304 | */ | |
305 | void | |
306 | __cxa_finalize(const void *dso) | |
307 | { | |
308 | if (dso != NULL) { | |
309 | // Note: this should not happen as only dyld should be calling | |
310 | // this and dyld has switched to call __cxa_finalize_ranges directly. | |
311 | struct __cxa_range_t range; | |
312 | range.addr = dso; | |
313 | range.length = 1; | |
314 | __cxa_finalize_ranges(&range, 1); | |
315 | } else { | |
316 | __cxa_finalize_ranges(NULL, 0); | |
317 | } | |
318 | } | |
319 | ||
6465356a A |
320 | #if !TARGET_IPHONE_SIMULATOR && (__i386__ || __x86_64__) |
321 | /* | |
322 | * Support for thread_local in C++, using existing _tlv_atexit() in libdyld | |
323 | */ | |
324 | ||
325 | void _tlv_atexit(void(*f)(void*), void* arg); /* in libdyld */ | |
326 | ||
327 | void | |
328 | __cxa_thread_atexit(void(*f)(void*), void* arg) | |
329 | { | |
330 | _tlv_atexit(f, arg); | |
331 | } | |
332 | #endif | |
70ad1dc8 | 333 | #pragma clang diagnostic pop |