1 /* __cxa_atexit backwards-compatibility support for Darwin.
2 Copyright (C) 2006, 2009 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 /* Don't do anything if we are compiling for a kext multilib. */
28 /* It is incorrect to include config.h here, because this file is being
29 compiled for the target, and hence definitions concerning only the host
40 /* This file works around two different problems.
42 The first problem is that there is no __cxa_atexit on Mac OS versions
43 before 10.4. It fixes this by providing a complete atexit and
44 __cxa_atexit emulation called from the regular atexit.
46 The second problem is that on all shipping versions of Mac OS,
47 __cxa_finalize and exit() don't work right: they don't run routines
48 that were registered while other atexit routines are running. This
49 is worked around by wrapping each atexit/__cxa_atexit routine with
50 our own routine which ensures that any __cxa_atexit calls while it
51 is running are honoured.
53 There are still problems which this does not solve. Before 10.4,
54 shared objects linked with previous compilers won't have their
55 atexit calls properly interleaved with code compiled with newer
56 compilers. Also, atexit routines registered from shared objects
57 linked with previous compilers won't get the bug fix. */
59 typedef int (*cxa_atexit_p
)(void (*func
) (void*), void* arg
, const void* dso
);
60 typedef void (*cxa_finalize_p
)(const void *dso
);
61 typedef int (*atexit_p
)(void (*func
)(void));
63 /* These are from "keymgr.h". */
64 extern void *_keymgr_get_and_lock_processwide_ptr (unsigned key
);
65 extern int _keymgr_get_and_lock_processwide_ptr_2 (unsigned, void **);
66 extern int _keymgr_set_and_unlock_processwide_ptr (unsigned key
, void *ptr
);
68 extern void *__keymgr_global
[];
69 typedef struct _Sinfo_Node
{
70 unsigned int size
; /*size of this node*/
71 unsigned short major_version
; /*API major version.*/
72 unsigned short minor_version
; /*API minor version.*/
76 #define CHECK_KEYMGR_ERROR(e) \
77 (((_Tinfo_Node *)__keymgr_global[2])->major_version >= 4 ? (e) : 0)
79 #define CHECK_KEYMGR_ERROR(e) (e)
82 /* Our globals are stored under this keymgr index. */
83 #define KEYMGR_ATEXIT_LIST 14
85 /* The different kinds of callback routines. */
86 typedef void (*atexit_callback
)(void);
87 typedef void (*cxa_atexit_callback
)(void *);
89 /* This structure holds a routine to call. There may be extra fields
90 at the end of the structure that this code doesn't know about. */
91 struct one_atexit_routine
95 cxa_atexit_callback cac
;
97 /* has_arg is 0/2/4 if 'ac' is live, 1/3/5 if 'cac' is live.
98 Higher numbers indicate a later version of the structure that this
99 code doesn't understand and will ignore. */
104 struct atexit_routine_list
106 struct atexit_routine_list
* next
;
107 struct one_atexit_routine r
;
110 /* The various possibilities for status of atexit(). */
112 atexit_status_unknown
= 0,
113 atexit_status_missing
= 1,
114 atexit_status_broken
= 2,
115 atexit_status_working
= 16
118 struct keymgr_atexit_list
120 /* Version of this list. This code knows only about version 0.
121 If the version is higher than 0, this code may add new atexit routines
122 but should not attempt to run the list. */
124 /* 1 if an atexit routine is currently being run by this code, 0
126 char running_routines
;
127 /* Holds a value from 'enum atexit_status'. */
128 unsigned char atexit_status
;
129 /* The list of atexit and cxa_atexit routines registered. If
130 atexit_status_missing it contains all routines registered while
131 linked with this code. If atexit_status_broken it contains all
132 routines registered during cxa_finalize while linked with this
134 struct atexit_routine_list
*l
;
135 /* &__cxa_atexit; set if atexit_status >= atexit_status_broken. */
136 cxa_atexit_p cxa_atexit_f
;
137 /* &__cxa_finalize; set if atexit_status >= atexit_status_broken. */
138 cxa_finalize_p cxa_finalize_f
;
139 /* &atexit; set if atexit_status >= atexit_status_working
140 or atexit_status == atexit_status_missing. */
144 /* Return 0 if __cxa_atexit has the bug it has in Mac OS 10.4: it
145 fails to call routines registered while an atexit routine is
146 running. Return 1 if it works properly, and -1 if an error occurred. */
151 cxa_atexit_p cxa_atexit
;
154 static void cxa_atexit_check_2 (void *arg
)
156 ((struct atexit_data
*)arg
)->result
= 1;
159 static void cxa_atexit_check_1 (void *arg
)
161 struct atexit_data
* aed
= arg
;
162 if (aed
->cxa_atexit (cxa_atexit_check_2
, arg
, arg
) != 0)
167 check_cxa_atexit (cxa_atexit_p cxa_atexit
, cxa_finalize_p cxa_finalize
)
169 struct atexit_data aed
= { 0, cxa_atexit
};
171 /* We re-use &aed as the 'dso' parameter, since it's a unique address. */
172 if (cxa_atexit (cxa_atexit_check_1
, &aed
, &aed
) != 0)
177 /* Call __cxa_finalize again to make sure that cxa_atexit_check_2
178 is removed from the list before AED goes out of scope. */
186 /* This comes from Csu. It works only before 10.4. The prototype has
187 been altered a bit to avoid casting. */
188 extern int _dyld_func_lookup(const char *dyld_func_name
,
189 void *address
) __attribute__((visibility("hidden")));
191 static void our_atexit (void);
193 /* We're running on 10.3.9. Find the address of the system atexit()
194 function. So easy to say, so hard to do. */
196 find_atexit_10_3 (void)
198 unsigned int (*dyld_image_count_fn
)(void);
199 const char *(*dyld_get_image_name_fn
)(unsigned int image_index
);
200 const void *(*dyld_get_image_header_fn
)(unsigned int image_index
);
201 const void *(*NSLookupSymbolInImage_fn
)(const void *image
,
202 const char *symbolName
,
203 unsigned int options
);
204 void *(*NSAddressOfSymbol_fn
)(const void *symbol
);
207 /* Find some dyld functions. */
208 _dyld_func_lookup("__dyld_image_count", &dyld_image_count_fn
);
209 _dyld_func_lookup("__dyld_get_image_name", &dyld_get_image_name_fn
);
210 _dyld_func_lookup("__dyld_get_image_header", &dyld_get_image_header_fn
);
211 _dyld_func_lookup("__dyld_NSLookupSymbolInImage", &NSLookupSymbolInImage_fn
);
212 _dyld_func_lookup("__dyld_NSAddressOfSymbol", &NSAddressOfSymbol_fn
);
214 /* If any of these don't exist, that's an error. */
215 if (! dyld_image_count_fn
|| ! dyld_get_image_name_fn
216 || ! dyld_get_image_header_fn
|| ! NSLookupSymbolInImage_fn
217 || ! NSAddressOfSymbol_fn
)
220 count
= dyld_image_count_fn ();
221 for (i
= 0; i
< count
; i
++)
223 const char * path
= dyld_get_image_name_fn (i
);
227 if (strcmp (path
, "/usr/lib/libSystem.B.dylib") != 0)
229 image
= dyld_get_image_header_fn (i
);
232 /* '4' is NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR. */
233 symbol
= NSLookupSymbolInImage_fn (image
, "_atexit", 4);
236 return NSAddressOfSymbol_fn (symbol
);
242 /* Create (if necessary), find, lock, fill in, and return our globals.
243 Return NULL on error, in which case the globals will not be locked.
244 The caller should call keymgr_set_and_unlock. */
245 static struct keymgr_atexit_list
*
248 struct keymgr_atexit_list
* r
;
251 /* 10.3.9 doesn't have _keymgr_get_and_lock_processwide_ptr_2 so the
252 PPC side can't use it. On 10.4 this just means the error gets
253 reported a little later when
254 _keymgr_set_and_unlock_processwide_ptr finds that the key was
256 r
= _keymgr_get_and_lock_processwide_ptr (KEYMGR_ATEXIT_LIST
);
259 if (_keymgr_get_and_lock_processwide_ptr_2 (KEYMGR_ATEXIT_LIST
, &rr
))
266 r
= calloc (sizeof (struct keymgr_atexit_list
), 1);
271 if (r
->atexit_status
== atexit_status_unknown
)
275 handle
= dlopen ("/usr/lib/libSystem.B.dylib", RTLD_NOLOAD
);
279 r
->atexit_status
= atexit_status_missing
;
280 r
->atexit_f
= find_atexit_10_3 ();
283 if (r
->atexit_f (our_atexit
))
293 r
->cxa_atexit_f
= (cxa_atexit_p
)dlsym (handle
, "__cxa_atexit");
294 r
->cxa_finalize_f
= (cxa_finalize_p
)dlsym (handle
, "__cxa_finalize");
295 if (! r
->cxa_atexit_f
|| ! r
->cxa_finalize_f
)
298 chk_result
= check_cxa_atexit (r
->cxa_atexit_f
, r
->cxa_finalize_f
);
299 if (chk_result
== -1)
301 else if (chk_result
== 0)
302 r
->atexit_status
= atexit_status_broken
;
305 r
->atexit_f
= (atexit_p
)dlsym (handle
, "atexit");
308 r
->atexit_status
= atexit_status_working
;
316 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
, r
);
320 /* Add TO_ADD to ATEXIT_LIST. ATEXIT_LIST may be NULL but is
321 always the result of calling _keymgr_get_and_lock_processwide_ptr and
322 so KEYMGR_ATEXIT_LIST is known to be locked; this routine is responsible
326 add_routine (struct keymgr_atexit_list
* g
,
327 const struct one_atexit_routine
* to_add
)
329 struct atexit_routine_list
* s
330 = malloc (sizeof (struct atexit_routine_list
));
335 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
, g
);
341 result
= _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
, g
);
342 return CHECK_KEYMGR_ERROR (result
) == 0 ? 0 : -1;
345 /* This runs the routines in G->L up to STOP. */
346 static struct keymgr_atexit_list
*
347 run_routines (struct keymgr_atexit_list
*g
,
348 struct atexit_routine_list
*stop
)
352 struct atexit_routine_list
* cur
= g
->l
;
353 if (! cur
|| cur
== stop
)
356 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
, g
);
358 switch (cur
->r
.has_arg
) {
359 case 0: case 2: case 4:
360 cur
->r
.callback
.ac ();
362 case 1: case 3: case 5:
363 cur
->r
.callback
.cac (cur
->r
.arg
);
366 /* Don't understand, so don't call it. */
371 g
= _keymgr_get_and_lock_processwide_ptr (KEYMGR_ATEXIT_LIST
);
378 /* Call the routine described by ROUTINE_PARAM and then call any
379 routines added to KEYMGR_ATEXIT_LIST while that routine was
380 running, all with in_cxa_finalize set. */
383 cxa_atexit_wrapper (void* routine_param
)
385 struct one_atexit_routine
* routine
= routine_param
;
386 struct keymgr_atexit_list
*g
;
387 struct atexit_routine_list
* base
= NULL
;
388 char prev_running
= 0;
390 g
= _keymgr_get_and_lock_processwide_ptr (KEYMGR_ATEXIT_LIST
);
393 prev_running
= g
->running_routines
;
394 g
->running_routines
= 1;
396 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
, g
);
399 if (routine
->has_arg
)
400 routine
->callback
.cac (routine
->arg
);
402 routine
->callback
.ac ();
405 g
= _keymgr_get_and_lock_processwide_ptr (KEYMGR_ATEXIT_LIST
);
407 g
= run_routines (g
, base
);
410 g
->running_routines
= prev_running
;
411 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
, g
);
416 /* This code is used while running on 10.3.9, when __cxa_atexit doesn't
417 exist in the system library. 10.3.9 only supported regular PowerPC,
418 so this code isn't necessary on x86 or ppc64. */
420 /* This routine is called from the system atexit(); it runs everything
421 registered on the KEYMGR_ATEXIT_LIST. */
426 struct keymgr_atexit_list
*g
;
429 g
= _keymgr_get_and_lock_processwide_ptr (KEYMGR_ATEXIT_LIST
);
430 if (! g
|| g
->version
!= 0 || g
->atexit_status
!= atexit_status_missing
)
433 prev_running
= g
->running_routines
;
434 g
->running_routines
= 1;
435 g
= run_routines (g
, NULL
);
438 g
->running_routines
= prev_running
;
439 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
, g
);
443 /* This is our wrapper around atexit and __cxa_atexit. It will return
444 nonzero if an error occurs, and otherwise:
445 - if in_cxa_finalize is set, or running on 10.3.9, add R to
446 KEYMGR_ATEXIT_LIST; or
447 - call the system __cxa_atexit to add cxa_atexit_wrapper with an argument
448 that indicates how cxa_atexit_wrapper should call R. */
451 atexit_common (const struct one_atexit_routine
*r
, const void *dso
)
453 struct keymgr_atexit_list
*g
= get_globals ();
458 if (g
->running_routines
|| g
->atexit_status
== atexit_status_missing
)
459 return add_routine (g
, r
);
461 if (g
->atexit_status
>= atexit_status_working
)
466 cxa_atexit_p cxa_atexit
= g
->cxa_atexit_f
;
467 result
= _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
,
469 if (CHECK_KEYMGR_ERROR (result
))
471 return cxa_atexit (r
->callback
.cac
, r
->arg
, dso
);
475 atexit_p atexit_f
= g
->atexit_f
;
476 result
= _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
,
478 if (CHECK_KEYMGR_ERROR (result
))
480 return atexit_f (r
->callback
.ac
);
485 cxa_atexit_p cxa_atexit
= g
->cxa_atexit_f
;
486 struct one_atexit_routine
*alloced
;
489 result
= _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
, g
);
490 if (CHECK_KEYMGR_ERROR (result
))
493 alloced
= malloc (sizeof (struct one_atexit_routine
));
497 return cxa_atexit (cxa_atexit_wrapper
, alloced
, dso
);
501 /* These are the actual replacement routines; they just funnel into
504 int __cxa_atexit (cxa_atexit_callback func
, void* arg
,
505 const void* dso
) __attribute__((visibility("hidden")));
508 __cxa_atexit (cxa_atexit_callback func
, void* arg
, const void* dso
)
510 struct one_atexit_routine r
;
511 r
.callback
.cac
= func
;
514 return atexit_common (&r
, dso
);
517 int atexit (atexit_callback func
) __attribute__((visibility("hidden")));
519 /* Use __dso_handle to allow even bundles that call atexit() to be unloaded
521 extern void __dso_handle
;
524 atexit (atexit_callback func
)
526 struct one_atexit_routine r
;
527 r
.callback
.ac
= func
;
529 return atexit_common (&r
, &__dso_handle
);