1 /* __cxa_atexit backwards-compatibility support for Darwin.
2 Copyright (C) 2006-2018 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. */
36 /* This file works around two different problems.
38 The first problem is that there is no __cxa_atexit on Mac OS versions
39 before 10.4. It fixes this by providing a complete atexit and
40 __cxa_atexit emulation called from the regular atexit.
42 The second problem is that on all shipping versions of Mac OS,
43 __cxa_finalize and exit() don't work right: they don't run routines
44 that were registered while other atexit routines are running. This
45 is worked around by wrapping each atexit/__cxa_atexit routine with
46 our own routine which ensures that any __cxa_atexit calls while it
47 is running are honoured.
49 There are still problems which this does not solve. Before 10.4,
50 shared objects linked with previous compilers won't have their
51 atexit calls properly interleaved with code compiled with newer
52 compilers. Also, atexit routines registered from shared objects
53 linked with previous compilers won't get the bug fix. */
55 typedef int (*cxa_atexit_p
)(void (*func
) (void*), void* arg
, const void* dso
);
56 typedef void (*cxa_finalize_p
)(const void *dso
);
57 typedef int (*atexit_p
)(void (*func
)(void));
59 /* These are from "keymgr.h". */
60 extern void *_keymgr_get_and_lock_processwide_ptr (unsigned key
);
61 extern int _keymgr_get_and_lock_processwide_ptr_2 (unsigned, void **);
62 extern int _keymgr_set_and_unlock_processwide_ptr (unsigned key
, void *ptr
);
64 extern void *__keymgr_global
[];
65 typedef struct _Sinfo_Node
{
66 unsigned int size
; /*size of this node*/
67 unsigned short major_version
; /*API major version.*/
68 unsigned short minor_version
; /*API minor version.*/
72 #define CHECK_KEYMGR_ERROR(e) \
73 (((_Tinfo_Node *)__keymgr_global[2])->major_version >= 4 ? (e) : 0)
75 #define CHECK_KEYMGR_ERROR(e) (e)
78 /* Our globals are stored under this keymgr index. */
79 #define KEYMGR_ATEXIT_LIST 14
81 /* The different kinds of callback routines. */
82 typedef void (*atexit_callback
)(void);
83 typedef void (*cxa_atexit_callback
)(void *);
85 /* This structure holds a routine to call. There may be extra fields
86 at the end of the structure that this code doesn't know about. */
87 struct one_atexit_routine
91 cxa_atexit_callback cac
;
93 /* has_arg is 0/2/4 if 'ac' is live, 1/3/5 if 'cac' is live.
94 Higher numbers indicate a later version of the structure that this
95 code doesn't understand and will ignore. */
100 struct atexit_routine_list
102 struct atexit_routine_list
* next
;
103 struct one_atexit_routine r
;
106 /* The various possibilities for status of atexit(). */
108 atexit_status_unknown
= 0,
109 atexit_status_missing
= 1,
110 atexit_status_broken
= 2,
111 atexit_status_working
= 16
114 struct keymgr_atexit_list
116 /* Version of this list. This code knows only about version 0.
117 If the version is higher than 0, this code may add new atexit routines
118 but should not attempt to run the list. */
120 /* 1 if an atexit routine is currently being run by this code, 0
122 char running_routines
;
123 /* Holds a value from 'enum atexit_status'. */
124 unsigned char atexit_status
;
125 /* The list of atexit and cxa_atexit routines registered. If
126 atexit_status_missing it contains all routines registered while
127 linked with this code. If atexit_status_broken it contains all
128 routines registered during cxa_finalize while linked with this
130 struct atexit_routine_list
*l
;
131 /* &__cxa_atexit; set if atexit_status >= atexit_status_broken. */
132 cxa_atexit_p cxa_atexit_f
;
133 /* &__cxa_finalize; set if atexit_status >= atexit_status_broken. */
134 cxa_finalize_p cxa_finalize_f
;
135 /* &atexit; set if atexit_status >= atexit_status_working
136 or atexit_status == atexit_status_missing. */
140 /* Return 0 if __cxa_atexit has the bug it has in Mac OS 10.4: it
141 fails to call routines registered while an atexit routine is
142 running. Return 1 if it works properly, and -1 if an error occurred. */
147 cxa_atexit_p cxa_atexit
;
150 static void cxa_atexit_check_2 (void *arg
)
152 ((struct atexit_data
*)arg
)->result
= 1;
155 static void cxa_atexit_check_1 (void *arg
)
157 struct atexit_data
* aed
= arg
;
158 if (aed
->cxa_atexit (cxa_atexit_check_2
, arg
, arg
) != 0)
163 check_cxa_atexit (cxa_atexit_p cxa_atexit
, cxa_finalize_p cxa_finalize
)
165 struct atexit_data aed
= { 0, cxa_atexit
};
167 /* We re-use &aed as the 'dso' parameter, since it's a unique address. */
168 if (cxa_atexit (cxa_atexit_check_1
, &aed
, &aed
) != 0)
173 /* Call __cxa_finalize again to make sure that cxa_atexit_check_2
174 is removed from the list before AED goes out of scope. */
182 /* This comes from Csu. It works only before 10.4. The prototype has
183 been altered a bit to avoid casting. */
184 extern int _dyld_func_lookup(const char *dyld_func_name
,
185 void *address
) __attribute__((visibility("hidden")));
187 static void our_atexit (void);
189 /* We're running on 10.3.9. Find the address of the system atexit()
190 function. So easy to say, so hard to do. */
192 find_atexit_10_3 (void)
194 unsigned int (*dyld_image_count_fn
)(void);
195 const char *(*dyld_get_image_name_fn
)(unsigned int image_index
);
196 const void *(*dyld_get_image_header_fn
)(unsigned int image_index
);
197 const void *(*NSLookupSymbolInImage_fn
)(const void *image
,
198 const char *symbolName
,
199 unsigned int options
);
200 void *(*NSAddressOfSymbol_fn
)(const void *symbol
);
203 /* Find some dyld functions. */
204 _dyld_func_lookup("__dyld_image_count", &dyld_image_count_fn
);
205 _dyld_func_lookup("__dyld_get_image_name", &dyld_get_image_name_fn
);
206 _dyld_func_lookup("__dyld_get_image_header", &dyld_get_image_header_fn
);
207 _dyld_func_lookup("__dyld_NSLookupSymbolInImage", &NSLookupSymbolInImage_fn
);
208 _dyld_func_lookup("__dyld_NSAddressOfSymbol", &NSAddressOfSymbol_fn
);
210 /* If any of these don't exist, that's an error. */
211 if (! dyld_image_count_fn
|| ! dyld_get_image_name_fn
212 || ! dyld_get_image_header_fn
|| ! NSLookupSymbolInImage_fn
213 || ! NSAddressOfSymbol_fn
)
216 count
= dyld_image_count_fn ();
217 for (i
= 0; i
< count
; i
++)
219 const char * path
= dyld_get_image_name_fn (i
);
223 if (strcmp (path
, "/usr/lib/libSystem.B.dylib") != 0)
225 image
= dyld_get_image_header_fn (i
);
228 /* '4' is NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR. */
229 symbol
= NSLookupSymbolInImage_fn (image
, "_atexit", 4);
232 return NSAddressOfSymbol_fn (symbol
);
238 /* Create (if necessary), find, lock, fill in, and return our globals.
239 Return NULL on error, in which case the globals will not be locked.
240 The caller should call keymgr_set_and_unlock. */
241 static struct keymgr_atexit_list
*
244 struct keymgr_atexit_list
* r
;
247 /* 10.3.9 doesn't have _keymgr_get_and_lock_processwide_ptr_2 so the
248 PPC side can't use it. On 10.4 this just means the error gets
249 reported a little later when
250 _keymgr_set_and_unlock_processwide_ptr finds that the key was
252 r
= _keymgr_get_and_lock_processwide_ptr (KEYMGR_ATEXIT_LIST
);
255 if (_keymgr_get_and_lock_processwide_ptr_2 (KEYMGR_ATEXIT_LIST
, &rr
))
262 r
= calloc (sizeof (struct keymgr_atexit_list
), 1);
267 if (r
->atexit_status
== atexit_status_unknown
)
271 handle
= dlopen ("/usr/lib/libSystem.B.dylib", RTLD_NOLOAD
);
275 r
->atexit_status
= atexit_status_missing
;
276 r
->atexit_f
= find_atexit_10_3 ();
279 if (r
->atexit_f (our_atexit
))
289 r
->cxa_atexit_f
= (cxa_atexit_p
)dlsym (handle
, "__cxa_atexit");
290 r
->cxa_finalize_f
= (cxa_finalize_p
)dlsym (handle
, "__cxa_finalize");
291 if (! r
->cxa_atexit_f
|| ! r
->cxa_finalize_f
)
294 chk_result
= check_cxa_atexit (r
->cxa_atexit_f
, r
->cxa_finalize_f
);
295 if (chk_result
== -1)
297 else if (chk_result
== 0)
298 r
->atexit_status
= atexit_status_broken
;
301 r
->atexit_f
= (atexit_p
)dlsym (handle
, "atexit");
304 r
->atexit_status
= atexit_status_working
;
312 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
, r
);
316 /* Add TO_ADD to ATEXIT_LIST. ATEXIT_LIST may be NULL but is
317 always the result of calling _keymgr_get_and_lock_processwide_ptr and
318 so KEYMGR_ATEXIT_LIST is known to be locked; this routine is responsible
322 add_routine (struct keymgr_atexit_list
* g
,
323 const struct one_atexit_routine
* to_add
)
325 struct atexit_routine_list
* s
326 = malloc (sizeof (struct atexit_routine_list
));
331 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
, g
);
337 result
= _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
, g
);
338 return CHECK_KEYMGR_ERROR (result
) == 0 ? 0 : -1;
341 /* This runs the routines in G->L up to STOP. */
342 static struct keymgr_atexit_list
*
343 run_routines (struct keymgr_atexit_list
*g
,
344 struct atexit_routine_list
*stop
)
348 struct atexit_routine_list
* cur
= g
->l
;
349 if (! cur
|| cur
== stop
)
352 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
, g
);
354 switch (cur
->r
.has_arg
) {
355 case 0: case 2: case 4:
356 cur
->r
.callback
.ac ();
358 case 1: case 3: case 5:
359 cur
->r
.callback
.cac (cur
->r
.arg
);
362 /* Don't understand, so don't call it. */
367 g
= _keymgr_get_and_lock_processwide_ptr (KEYMGR_ATEXIT_LIST
);
374 /* Call the routine described by ROUTINE_PARAM and then call any
375 routines added to KEYMGR_ATEXIT_LIST while that routine was
376 running, all with in_cxa_finalize set. */
379 cxa_atexit_wrapper (void* routine_param
)
381 struct one_atexit_routine
* routine
= routine_param
;
382 struct keymgr_atexit_list
*g
;
383 struct atexit_routine_list
* base
= NULL
;
384 char prev_running
= 0;
386 g
= _keymgr_get_and_lock_processwide_ptr (KEYMGR_ATEXIT_LIST
);
389 prev_running
= g
->running_routines
;
390 g
->running_routines
= 1;
392 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
, g
);
395 if (routine
->has_arg
)
396 routine
->callback
.cac (routine
->arg
);
398 routine
->callback
.ac ();
401 g
= _keymgr_get_and_lock_processwide_ptr (KEYMGR_ATEXIT_LIST
);
403 g
= run_routines (g
, base
);
406 g
->running_routines
= prev_running
;
407 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
, g
);
412 /* This code is used while running on 10.3.9, when __cxa_atexit doesn't
413 exist in the system library. 10.3.9 only supported regular PowerPC,
414 so this code isn't necessary on x86 or ppc64. */
416 /* This routine is called from the system atexit(); it runs everything
417 registered on the KEYMGR_ATEXIT_LIST. */
422 struct keymgr_atexit_list
*g
;
425 g
= _keymgr_get_and_lock_processwide_ptr (KEYMGR_ATEXIT_LIST
);
426 if (! g
|| g
->version
!= 0 || g
->atexit_status
!= atexit_status_missing
)
429 prev_running
= g
->running_routines
;
430 g
->running_routines
= 1;
431 g
= run_routines (g
, NULL
);
434 g
->running_routines
= prev_running
;
435 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
, g
);
439 /* This is our wrapper around atexit and __cxa_atexit. It will return
440 nonzero if an error occurs, and otherwise:
441 - if in_cxa_finalize is set, or running on 10.3.9, add R to
442 KEYMGR_ATEXIT_LIST; or
443 - call the system __cxa_atexit to add cxa_atexit_wrapper with an argument
444 that indicates how cxa_atexit_wrapper should call R. */
447 atexit_common (const struct one_atexit_routine
*r
, const void *dso
)
449 struct keymgr_atexit_list
*g
= get_globals ();
454 if (g
->running_routines
|| g
->atexit_status
== atexit_status_missing
)
455 return add_routine (g
, r
);
457 if (g
->atexit_status
>= atexit_status_working
)
462 cxa_atexit_p cxa_atexit
= g
->cxa_atexit_f
;
463 result
= _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
,
465 if (CHECK_KEYMGR_ERROR (result
))
467 return cxa_atexit (r
->callback
.cac
, r
->arg
, dso
);
471 atexit_p atexit_f
= g
->atexit_f
;
472 result
= _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
,
474 if (CHECK_KEYMGR_ERROR (result
))
476 return atexit_f (r
->callback
.ac
);
481 cxa_atexit_p cxa_atexit
= g
->cxa_atexit_f
;
482 struct one_atexit_routine
*alloced
;
485 result
= _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST
, g
);
486 if (CHECK_KEYMGR_ERROR (result
))
489 alloced
= malloc (sizeof (struct one_atexit_routine
));
493 return cxa_atexit (cxa_atexit_wrapper
, alloced
, dso
);
497 /* These are the actual replacement routines; they just funnel into
500 int __cxa_atexit (cxa_atexit_callback func
, void* arg
,
501 const void* dso
) __attribute__((visibility("hidden")));
504 __cxa_atexit (cxa_atexit_callback func
, void* arg
, const void* dso
)
506 struct one_atexit_routine r
;
507 r
.callback
.cac
= func
;
510 return atexit_common (&r
, dso
);
513 int atexit (atexit_callback func
) __attribute__((visibility("hidden")));
515 /* Use __dso_handle to allow even bundles that call atexit() to be unloaded
517 extern void __dso_handle
;
520 atexit (atexit_callback func
)
522 struct one_atexit_routine r
;
523 r
.callback
.ac
= func
;
525 return atexit_common (&r
, &__dso_handle
);