2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
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
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid
[] = "@(#)atexit.c 8.2 (Berkeley) 7/3/94";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/lib/libc/stdlib/atexit.c,v 1.8 2007/01/09 00:28:09 imp Exp $");
39 #include "namespace.h"
46 #if defined(__DYNAMIC__) || defined (__BLOCKS__)
48 #endif /* defined(__DYNAMIC__) */
50 #include "un-namespace.h"
54 #include <Block_private.h>
55 #endif /* __BLOCKS__ */
56 #include "libc_private.h"
57 #include <os/alloc_once_private.h>
59 #include <TargetConditionals.h>
61 #define ATEXIT_FN_EMPTY 0
62 #define ATEXIT_FN_STD 1
63 #define ATEXIT_FN_CXA 2
65 #define ATEXIT_FN_BLK 3
66 #endif /* __BLOCKS__ */
68 static pthread_mutex_t atexit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
70 #define _MUTEX_LOCK(x) if (__isthreaded) _pthread_mutex_lock(x)
71 #define _MUTEX_UNLOCK(x) if (__isthreaded) _pthread_mutex_unlock(x)
74 struct atexit
*next
; /* next in list */
75 int ind
; /* next index in this table */
77 int fn_type
; /* ATEXIT_? from above */
79 void (*std_func
)(void);
80 void (*cxa_func
)(void *);
83 #endif /* __BLOCKS__ */
84 } fn_ptr
; /* function pointer */
85 void *fn_arg
; /* argument for CXA callback */
86 void *fn_dso
; /* shared module handle */
87 } fns
[ATEXIT_SIZE
]; /* the table itself */
90 static struct atexit
*__atexit
; /* points to head of LIFO stack */
91 static int __atexit_new_registration
;
93 __attribute__ ((visibility ("hidden")))
97 __atexit
= os_alloc_once(OS_ALLOC_ONCE_KEY_LIBSYSTEM_C
,
98 sizeof(struct atexit
), NULL
);
102 * Register the function described by 'fptr' to be called at application
103 * exit or owning shared object unload time. This is a helper function
104 * for atexit and __cxa_atexit.
107 atexit_register(struct atexit_fn
*fptr
)
109 struct atexit
*p
= __atexit
;
111 _MUTEX_LOCK(&atexit_mutex
);
112 while (p
->ind
>= ATEXIT_SIZE
) {
113 struct atexit
*old__atexit
;
114 old__atexit
= __atexit
;
115 _MUTEX_UNLOCK(&atexit_mutex
);
116 if ((p
= (struct atexit
*)malloc(sizeof(*p
))) == NULL
)
118 _MUTEX_LOCK(&atexit_mutex
);
119 if (old__atexit
!= __atexit
) {
120 /* Lost race, retry operation */
121 _MUTEX_UNLOCK(&atexit_mutex
);
123 _MUTEX_LOCK(&atexit_mutex
);
131 p
->fns
[p
->ind
++] = *fptr
;
132 __atexit_new_registration
= 1;
133 _MUTEX_UNLOCK(&atexit_mutex
);
138 * Register a function to be performed at exit.
141 atexit(void (*func
)(void))
146 fn
.fn_type
= ATEXIT_FN_STD
;
147 fn
.fn_ptr
.std_func
= func
;
151 #if defined(__DYNAMIC__) && !TARGET_OS_IPHONE
152 // <rdar://problem/14596032&15173956>
154 if (dladdr(func
, &info
)) {
155 fn
.fn_dso
= info
.dli_fbase
;
159 error
= atexit_register(&fn
);
165 atexit_b(void (^block
)(void))
170 fn
.fn_type
= ATEXIT_FN_BLK
;
171 fn
.fn_ptr
.block
= Block_copy(block
);
175 error
= atexit_register(&fn
);
178 #endif /* __BLOCKS__ */
181 * Register a function to be performed at exit or when an shared object
182 * with given dso handle is unloaded dynamically.
185 __cxa_atexit(void (*func
)(void *), void *arg
, void *dso
)
190 fn
.fn_type
= ATEXIT_FN_CXA
;
191 fn
.fn_ptr
.cxa_func
= func
;;
195 error
= atexit_register(&fn
);
200 __cxa_in_range(const struct __cxa_range_t ranges
[],
204 uintptr_t addr
= (uintptr_t)fn
;
207 for (i
= 0; i
< count
; ++i
) {
208 const struct __cxa_range_t
*r
= &ranges
[i
];
209 if (addr
< (uintptr_t)r
->addr
) {
212 if (addr
< ((uintptr_t)r
->addr
+ r
->length
)) {
220 * Call handlers registered via __cxa_atexit/atexit that are in a
222 * Note: rangeCount==0, means call all handlers.
225 __cxa_finalize_ranges(const struct __cxa_range_t ranges
[], unsigned int count
)
228 struct atexit_fn
*fn
;
230 _MUTEX_LOCK(&atexit_mutex
);
233 for (p
= __atexit
; p
; p
= p
->next
) {
234 for (n
= p
->ind
; --n
>= 0;) {
237 if (fn
->fn_type
== ATEXIT_FN_EMPTY
) {
238 continue; // already been called
241 // Verify that the entry is within the range being unloaded.
243 if (fn
->fn_type
== ATEXIT_FN_CXA
) {
244 // for __cxa_atexit(), call if *dso* is in range be unloaded
245 if (!__cxa_in_range(ranges
, count
, fn
->fn_dso
)) {
246 continue; // not being unloaded yet
248 } else if (fn
->fn_type
== ATEXIT_FN_STD
) {
249 // for atexit, call if *function* is in range be unloaded
250 if (!__cxa_in_range(ranges
, count
, fn
->fn_ptr
.std_func
)) {
251 continue; // not being unloaded yet
254 } else if (fn
->fn_type
== ATEXIT_FN_BLK
) {
255 // for atexit_b, call if block code is in range be unloaded
256 void *a
= ((struct Block_layout
*)fn
->fn_ptr
.block
)->invoke
;
257 if (!__cxa_in_range(ranges
, count
, a
)) {
258 continue; // not being unloaded yet
264 // Clear the entry to indicate that this handler has been called.
265 int fn_type
= fn
->fn_type
;
266 fn
->fn_type
= ATEXIT_FN_EMPTY
;
268 // Detect recursive registrations.
269 __atexit_new_registration
= 0;
270 _MUTEX_UNLOCK(&atexit_mutex
);
273 if (fn_type
== ATEXIT_FN_CXA
) {
274 fn
->fn_ptr
.cxa_func(fn
->fn_arg
);
275 } else if (fn_type
== ATEXIT_FN_STD
) {
276 fn
->fn_ptr
.std_func();
278 } else if (fn_type
== ATEXIT_FN_BLK
) {
283 // Call any recursively registered handlers.
284 _MUTEX_LOCK(&atexit_mutex
);
285 if (__atexit_new_registration
) {
290 _MUTEX_UNLOCK(&atexit_mutex
);
295 * Call all handlers registered with __cxa_atexit for the shared
296 * object owning 'dso'. Note: if 'dso' is NULL, then all remaining
297 * handlers are called.
300 __cxa_finalize(const void *dso
)
303 // Note: this should not happen as only dyld should be calling
304 // this and dyld has switched to call __cxa_finalize_ranges directly.
305 struct __cxa_range_t range
;
308 __cxa_finalize_ranges(&range
, 1);
310 __cxa_finalize_ranges(NULL
, 0);
314 #if !TARGET_IPHONE_SIMULATOR && (__i386__ || __x86_64__)
316 * Support for thread_local in C++, using existing _tlv_atexit() in libdyld
319 void _tlv_atexit(void(*f
)(void*), void* arg
); /* in libdyld */
322 __cxa_thread_atexit(void(*f
)(void*), void* arg
)