annotate libgcc/config/darwin-crt3.c @ 144:8f4e72ab4e11

fix segmentation fault caused by nothing next cur_op to end
author Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Sun, 23 Dec 2018 21:23:56 +0900
parents 84e7813d76e9
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* __cxa_atexit backwards-compatibility support for Darwin.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2 Copyright (C) 2006-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
7 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
8 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
9 version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
14 for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
17 permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
18 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
21 a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
23 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 /* Don't do anything if we are compiling for a kext multilib. */
kono
parents:
diff changeset
26 #ifdef __PIC__
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 #include "tconfig.h"
kono
parents:
diff changeset
29 #include "tsystem.h"
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 #include <dlfcn.h>
kono
parents:
diff changeset
32 #include <stdbool.h>
kono
parents:
diff changeset
33 #include <stdlib.h>
kono
parents:
diff changeset
34 #include <string.h>
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 /* This file works around two different problems.
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 The first problem is that there is no __cxa_atexit on Mac OS versions
kono
parents:
diff changeset
39 before 10.4. It fixes this by providing a complete atexit and
kono
parents:
diff changeset
40 __cxa_atexit emulation called from the regular atexit.
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 The second problem is that on all shipping versions of Mac OS,
kono
parents:
diff changeset
43 __cxa_finalize and exit() don't work right: they don't run routines
kono
parents:
diff changeset
44 that were registered while other atexit routines are running. This
kono
parents:
diff changeset
45 is worked around by wrapping each atexit/__cxa_atexit routine with
kono
parents:
diff changeset
46 our own routine which ensures that any __cxa_atexit calls while it
kono
parents:
diff changeset
47 is running are honoured.
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 There are still problems which this does not solve. Before 10.4,
kono
parents:
diff changeset
50 shared objects linked with previous compilers won't have their
kono
parents:
diff changeset
51 atexit calls properly interleaved with code compiled with newer
kono
parents:
diff changeset
52 compilers. Also, atexit routines registered from shared objects
kono
parents:
diff changeset
53 linked with previous compilers won't get the bug fix. */
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 typedef int (*cxa_atexit_p)(void (*func) (void*), void* arg, const void* dso);
kono
parents:
diff changeset
56 typedef void (*cxa_finalize_p)(const void *dso);
kono
parents:
diff changeset
57 typedef int (*atexit_p)(void (*func)(void));
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 /* These are from "keymgr.h". */
kono
parents:
diff changeset
60 extern void *_keymgr_get_and_lock_processwide_ptr (unsigned key);
kono
parents:
diff changeset
61 extern int _keymgr_get_and_lock_processwide_ptr_2 (unsigned, void **);
kono
parents:
diff changeset
62 extern int _keymgr_set_and_unlock_processwide_ptr (unsigned key, void *ptr);
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 extern void *__keymgr_global[];
kono
parents:
diff changeset
65 typedef struct _Sinfo_Node {
kono
parents:
diff changeset
66 unsigned int size ; /*size of this node*/
kono
parents:
diff changeset
67 unsigned short major_version ; /*API major version.*/
kono
parents:
diff changeset
68 unsigned short minor_version ; /*API minor version.*/
kono
parents:
diff changeset
69 } _Tinfo_Node ;
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 #ifdef __ppc__
kono
parents:
diff changeset
72 #define CHECK_KEYMGR_ERROR(e) \
kono
parents:
diff changeset
73 (((_Tinfo_Node *)__keymgr_global[2])->major_version >= 4 ? (e) : 0)
kono
parents:
diff changeset
74 #else
kono
parents:
diff changeset
75 #define CHECK_KEYMGR_ERROR(e) (e)
kono
parents:
diff changeset
76 #endif
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 /* Our globals are stored under this keymgr index. */
kono
parents:
diff changeset
79 #define KEYMGR_ATEXIT_LIST 14
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 /* The different kinds of callback routines. */
kono
parents:
diff changeset
82 typedef void (*atexit_callback)(void);
kono
parents:
diff changeset
83 typedef void (*cxa_atexit_callback)(void *);
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 /* This structure holds a routine to call. There may be extra fields
kono
parents:
diff changeset
86 at the end of the structure that this code doesn't know about. */
kono
parents:
diff changeset
87 struct one_atexit_routine
kono
parents:
diff changeset
88 {
kono
parents:
diff changeset
89 union {
kono
parents:
diff changeset
90 atexit_callback ac;
kono
parents:
diff changeset
91 cxa_atexit_callback cac;
kono
parents:
diff changeset
92 } callback;
kono
parents:
diff changeset
93 /* has_arg is 0/2/4 if 'ac' is live, 1/3/5 if 'cac' is live.
kono
parents:
diff changeset
94 Higher numbers indicate a later version of the structure that this
kono
parents:
diff changeset
95 code doesn't understand and will ignore. */
kono
parents:
diff changeset
96 int has_arg;
kono
parents:
diff changeset
97 void * arg;
kono
parents:
diff changeset
98 };
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 struct atexit_routine_list
kono
parents:
diff changeset
101 {
kono
parents:
diff changeset
102 struct atexit_routine_list * next;
kono
parents:
diff changeset
103 struct one_atexit_routine r;
kono
parents:
diff changeset
104 };
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 /* The various possibilities for status of atexit(). */
kono
parents:
diff changeset
107 enum atexit_status {
kono
parents:
diff changeset
108 atexit_status_unknown = 0,
kono
parents:
diff changeset
109 atexit_status_missing = 1,
kono
parents:
diff changeset
110 atexit_status_broken = 2,
kono
parents:
diff changeset
111 atexit_status_working = 16
kono
parents:
diff changeset
112 };
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 struct keymgr_atexit_list
kono
parents:
diff changeset
115 {
kono
parents:
diff changeset
116 /* Version of this list. This code knows only about version 0.
kono
parents:
diff changeset
117 If the version is higher than 0, this code may add new atexit routines
kono
parents:
diff changeset
118 but should not attempt to run the list. */
kono
parents:
diff changeset
119 short version;
kono
parents:
diff changeset
120 /* 1 if an atexit routine is currently being run by this code, 0
kono
parents:
diff changeset
121 otherwise. */
kono
parents:
diff changeset
122 char running_routines;
kono
parents:
diff changeset
123 /* Holds a value from 'enum atexit_status'. */
kono
parents:
diff changeset
124 unsigned char atexit_status;
kono
parents:
diff changeset
125 /* The list of atexit and cxa_atexit routines registered. If
kono
parents:
diff changeset
126 atexit_status_missing it contains all routines registered while
kono
parents:
diff changeset
127 linked with this code. If atexit_status_broken it contains all
kono
parents:
diff changeset
128 routines registered during cxa_finalize while linked with this
kono
parents:
diff changeset
129 code. */
kono
parents:
diff changeset
130 struct atexit_routine_list *l;
kono
parents:
diff changeset
131 /* &__cxa_atexit; set if atexit_status >= atexit_status_broken. */
kono
parents:
diff changeset
132 cxa_atexit_p cxa_atexit_f;
kono
parents:
diff changeset
133 /* &__cxa_finalize; set if atexit_status >= atexit_status_broken. */
kono
parents:
diff changeset
134 cxa_finalize_p cxa_finalize_f;
kono
parents:
diff changeset
135 /* &atexit; set if atexit_status >= atexit_status_working
kono
parents:
diff changeset
136 or atexit_status == atexit_status_missing. */
kono
parents:
diff changeset
137 atexit_p atexit_f;
kono
parents:
diff changeset
138 };
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 /* Return 0 if __cxa_atexit has the bug it has in Mac OS 10.4: it
kono
parents:
diff changeset
141 fails to call routines registered while an atexit routine is
kono
parents:
diff changeset
142 running. Return 1 if it works properly, and -1 if an error occurred. */
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 struct atexit_data
kono
parents:
diff changeset
145 {
kono
parents:
diff changeset
146 int result;
kono
parents:
diff changeset
147 cxa_atexit_p cxa_atexit;
kono
parents:
diff changeset
148 };
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 static void cxa_atexit_check_2 (void *arg)
kono
parents:
diff changeset
151 {
kono
parents:
diff changeset
152 ((struct atexit_data *)arg)->result = 1;
kono
parents:
diff changeset
153 }
kono
parents:
diff changeset
154
kono
parents:
diff changeset
155 static void cxa_atexit_check_1 (void *arg)
kono
parents:
diff changeset
156 {
kono
parents:
diff changeset
157 struct atexit_data * aed = arg;
kono
parents:
diff changeset
158 if (aed->cxa_atexit (cxa_atexit_check_2, arg, arg) != 0)
kono
parents:
diff changeset
159 aed->result = -1;
kono
parents:
diff changeset
160 }
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 static int
kono
parents:
diff changeset
163 check_cxa_atexit (cxa_atexit_p cxa_atexit, cxa_finalize_p cxa_finalize)
kono
parents:
diff changeset
164 {
kono
parents:
diff changeset
165 struct atexit_data aed = { 0, cxa_atexit };
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 /* We re-use &aed as the 'dso' parameter, since it's a unique address. */
kono
parents:
diff changeset
168 if (cxa_atexit (cxa_atexit_check_1, &aed, &aed) != 0)
kono
parents:
diff changeset
169 return -1;
kono
parents:
diff changeset
170 cxa_finalize (&aed);
kono
parents:
diff changeset
171 if (aed.result == 0)
kono
parents:
diff changeset
172 {
kono
parents:
diff changeset
173 /* Call __cxa_finalize again to make sure that cxa_atexit_check_2
kono
parents:
diff changeset
174 is removed from the list before AED goes out of scope. */
kono
parents:
diff changeset
175 cxa_finalize (&aed);
kono
parents:
diff changeset
176 aed.result = 0;
kono
parents:
diff changeset
177 }
kono
parents:
diff changeset
178 return aed.result;
kono
parents:
diff changeset
179 }
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181 #ifdef __ppc__
kono
parents:
diff changeset
182 /* This comes from Csu. It works only before 10.4. The prototype has
kono
parents:
diff changeset
183 been altered a bit to avoid casting. */
kono
parents:
diff changeset
184 extern int _dyld_func_lookup(const char *dyld_func_name,
kono
parents:
diff changeset
185 void *address) __attribute__((visibility("hidden")));
kono
parents:
diff changeset
186
kono
parents:
diff changeset
187 static void our_atexit (void);
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189 /* We're running on 10.3.9. Find the address of the system atexit()
kono
parents:
diff changeset
190 function. So easy to say, so hard to do. */
kono
parents:
diff changeset
191 static atexit_p
kono
parents:
diff changeset
192 find_atexit_10_3 (void)
kono
parents:
diff changeset
193 {
kono
parents:
diff changeset
194 unsigned int (*dyld_image_count_fn)(void);
kono
parents:
diff changeset
195 const char *(*dyld_get_image_name_fn)(unsigned int image_index);
kono
parents:
diff changeset
196 const void *(*dyld_get_image_header_fn)(unsigned int image_index);
kono
parents:
diff changeset
197 const void *(*NSLookupSymbolInImage_fn)(const void *image,
kono
parents:
diff changeset
198 const char *symbolName,
kono
parents:
diff changeset
199 unsigned int options);
kono
parents:
diff changeset
200 void *(*NSAddressOfSymbol_fn)(const void *symbol);
kono
parents:
diff changeset
201 unsigned i, count;
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 /* Find some dyld functions. */
kono
parents:
diff changeset
204 _dyld_func_lookup("__dyld_image_count", &dyld_image_count_fn);
kono
parents:
diff changeset
205 _dyld_func_lookup("__dyld_get_image_name", &dyld_get_image_name_fn);
kono
parents:
diff changeset
206 _dyld_func_lookup("__dyld_get_image_header", &dyld_get_image_header_fn);
kono
parents:
diff changeset
207 _dyld_func_lookup("__dyld_NSLookupSymbolInImage", &NSLookupSymbolInImage_fn);
kono
parents:
diff changeset
208 _dyld_func_lookup("__dyld_NSAddressOfSymbol", &NSAddressOfSymbol_fn);
kono
parents:
diff changeset
209
kono
parents:
diff changeset
210 /* If any of these don't exist, that's an error. */
kono
parents:
diff changeset
211 if (! dyld_image_count_fn || ! dyld_get_image_name_fn
kono
parents:
diff changeset
212 || ! dyld_get_image_header_fn || ! NSLookupSymbolInImage_fn
kono
parents:
diff changeset
213 || ! NSAddressOfSymbol_fn)
kono
parents:
diff changeset
214 return NULL;
kono
parents:
diff changeset
215
kono
parents:
diff changeset
216 count = dyld_image_count_fn ();
kono
parents:
diff changeset
217 for (i = 0; i < count; i++)
kono
parents:
diff changeset
218 {
kono
parents:
diff changeset
219 const char * path = dyld_get_image_name_fn (i);
kono
parents:
diff changeset
220 const void * image;
kono
parents:
diff changeset
221 const void * symbol;
kono
parents:
diff changeset
222
kono
parents:
diff changeset
223 if (strcmp (path, "/usr/lib/libSystem.B.dylib") != 0)
kono
parents:
diff changeset
224 continue;
kono
parents:
diff changeset
225 image = dyld_get_image_header_fn (i);
kono
parents:
diff changeset
226 if (! image)
kono
parents:
diff changeset
227 return NULL;
kono
parents:
diff changeset
228 /* '4' is NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR. */
kono
parents:
diff changeset
229 symbol = NSLookupSymbolInImage_fn (image, "_atexit", 4);
kono
parents:
diff changeset
230 if (! symbol)
kono
parents:
diff changeset
231 return NULL;
kono
parents:
diff changeset
232 return NSAddressOfSymbol_fn (symbol);
kono
parents:
diff changeset
233 }
kono
parents:
diff changeset
234 return NULL;
kono
parents:
diff changeset
235 }
kono
parents:
diff changeset
236 #endif
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 /* Create (if necessary), find, lock, fill in, and return our globals.
kono
parents:
diff changeset
239 Return NULL on error, in which case the globals will not be locked.
kono
parents:
diff changeset
240 The caller should call keymgr_set_and_unlock. */
kono
parents:
diff changeset
241 static struct keymgr_atexit_list *
kono
parents:
diff changeset
242 get_globals (void)
kono
parents:
diff changeset
243 {
kono
parents:
diff changeset
244 struct keymgr_atexit_list * r;
kono
parents:
diff changeset
245
kono
parents:
diff changeset
246 #ifdef __ppc__
kono
parents:
diff changeset
247 /* 10.3.9 doesn't have _keymgr_get_and_lock_processwide_ptr_2 so the
kono
parents:
diff changeset
248 PPC side can't use it. On 10.4 this just means the error gets
kono
parents:
diff changeset
249 reported a little later when
kono
parents:
diff changeset
250 _keymgr_set_and_unlock_processwide_ptr finds that the key was
kono
parents:
diff changeset
251 never locked. */
kono
parents:
diff changeset
252 r = _keymgr_get_and_lock_processwide_ptr (KEYMGR_ATEXIT_LIST);
kono
parents:
diff changeset
253 #else
kono
parents:
diff changeset
254 void * rr;
kono
parents:
diff changeset
255 if (_keymgr_get_and_lock_processwide_ptr_2 (KEYMGR_ATEXIT_LIST, &rr))
kono
parents:
diff changeset
256 return NULL;
kono
parents:
diff changeset
257 r = rr;
kono
parents:
diff changeset
258 #endif
kono
parents:
diff changeset
259
kono
parents:
diff changeset
260 if (r == NULL)
kono
parents:
diff changeset
261 {
kono
parents:
diff changeset
262 r = calloc (sizeof (struct keymgr_atexit_list), 1);
kono
parents:
diff changeset
263 if (! r)
kono
parents:
diff changeset
264 return NULL;
kono
parents:
diff changeset
265 }
kono
parents:
diff changeset
266
kono
parents:
diff changeset
267 if (r->atexit_status == atexit_status_unknown)
kono
parents:
diff changeset
268 {
kono
parents:
diff changeset
269 void *handle;
kono
parents:
diff changeset
270
kono
parents:
diff changeset
271 handle = dlopen ("/usr/lib/libSystem.B.dylib", RTLD_NOLOAD);
kono
parents:
diff changeset
272 if (!handle)
kono
parents:
diff changeset
273 {
kono
parents:
diff changeset
274 #ifdef __ppc__
kono
parents:
diff changeset
275 r->atexit_status = atexit_status_missing;
kono
parents:
diff changeset
276 r->atexit_f = find_atexit_10_3 ();
kono
parents:
diff changeset
277 if (! r->atexit_f)
kono
parents:
diff changeset
278 goto error;
kono
parents:
diff changeset
279 if (r->atexit_f (our_atexit))
kono
parents:
diff changeset
280 goto error;
kono
parents:
diff changeset
281 #else
kono
parents:
diff changeset
282 goto error;
kono
parents:
diff changeset
283 #endif
kono
parents:
diff changeset
284 }
kono
parents:
diff changeset
285 else
kono
parents:
diff changeset
286 {
kono
parents:
diff changeset
287 int chk_result;
kono
parents:
diff changeset
288
kono
parents:
diff changeset
289 r->cxa_atexit_f = (cxa_atexit_p)dlsym (handle, "__cxa_atexit");
kono
parents:
diff changeset
290 r->cxa_finalize_f = (cxa_finalize_p)dlsym (handle, "__cxa_finalize");
kono
parents:
diff changeset
291 if (! r->cxa_atexit_f || ! r->cxa_finalize_f)
kono
parents:
diff changeset
292 goto error;
kono
parents:
diff changeset
293
kono
parents:
diff changeset
294 chk_result = check_cxa_atexit (r->cxa_atexit_f, r->cxa_finalize_f);
kono
parents:
diff changeset
295 if (chk_result == -1)
kono
parents:
diff changeset
296 goto error;
kono
parents:
diff changeset
297 else if (chk_result == 0)
kono
parents:
diff changeset
298 r->atexit_status = atexit_status_broken;
kono
parents:
diff changeset
299 else
kono
parents:
diff changeset
300 {
kono
parents:
diff changeset
301 r->atexit_f = (atexit_p)dlsym (handle, "atexit");
kono
parents:
diff changeset
302 if (! r->atexit_f)
kono
parents:
diff changeset
303 goto error;
kono
parents:
diff changeset
304 r->atexit_status = atexit_status_working;
kono
parents:
diff changeset
305 }
kono
parents:
diff changeset
306 }
kono
parents:
diff changeset
307 }
kono
parents:
diff changeset
308
kono
parents:
diff changeset
309 return r;
kono
parents:
diff changeset
310
kono
parents:
diff changeset
311 error:
kono
parents:
diff changeset
312 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST, r);
kono
parents:
diff changeset
313 return NULL;
kono
parents:
diff changeset
314 }
kono
parents:
diff changeset
315
kono
parents:
diff changeset
316 /* Add TO_ADD to ATEXIT_LIST. ATEXIT_LIST may be NULL but is
kono
parents:
diff changeset
317 always the result of calling _keymgr_get_and_lock_processwide_ptr and
kono
parents:
diff changeset
318 so KEYMGR_ATEXIT_LIST is known to be locked; this routine is responsible
kono
parents:
diff changeset
319 for unlocking it. */
kono
parents:
diff changeset
320
kono
parents:
diff changeset
321 static int
kono
parents:
diff changeset
322 add_routine (struct keymgr_atexit_list * g,
kono
parents:
diff changeset
323 const struct one_atexit_routine * to_add)
kono
parents:
diff changeset
324 {
kono
parents:
diff changeset
325 struct atexit_routine_list * s
kono
parents:
diff changeset
326 = malloc (sizeof (struct atexit_routine_list));
kono
parents:
diff changeset
327 int result;
kono
parents:
diff changeset
328
kono
parents:
diff changeset
329 if (!s)
kono
parents:
diff changeset
330 {
kono
parents:
diff changeset
331 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST, g);
kono
parents:
diff changeset
332 return -1;
kono
parents:
diff changeset
333 }
kono
parents:
diff changeset
334 s->r = *to_add;
kono
parents:
diff changeset
335 s->next = g->l;
kono
parents:
diff changeset
336 g->l = s;
kono
parents:
diff changeset
337 result = _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST, g);
kono
parents:
diff changeset
338 return CHECK_KEYMGR_ERROR (result) == 0 ? 0 : -1;
kono
parents:
diff changeset
339 }
kono
parents:
diff changeset
340
kono
parents:
diff changeset
341 /* This runs the routines in G->L up to STOP. */
kono
parents:
diff changeset
342 static struct keymgr_atexit_list *
kono
parents:
diff changeset
343 run_routines (struct keymgr_atexit_list *g,
kono
parents:
diff changeset
344 struct atexit_routine_list *stop)
kono
parents:
diff changeset
345 {
kono
parents:
diff changeset
346 for (;;)
kono
parents:
diff changeset
347 {
kono
parents:
diff changeset
348 struct atexit_routine_list * cur = g->l;
kono
parents:
diff changeset
349 if (! cur || cur == stop)
kono
parents:
diff changeset
350 break;
kono
parents:
diff changeset
351 g->l = cur->next;
kono
parents:
diff changeset
352 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST, g);
kono
parents:
diff changeset
353
kono
parents:
diff changeset
354 switch (cur->r.has_arg) {
kono
parents:
diff changeset
355 case 0: case 2: case 4:
kono
parents:
diff changeset
356 cur->r.callback.ac ();
kono
parents:
diff changeset
357 break;
kono
parents:
diff changeset
358 case 1: case 3: case 5:
kono
parents:
diff changeset
359 cur->r.callback.cac (cur->r.arg);
kono
parents:
diff changeset
360 break;
kono
parents:
diff changeset
361 default:
kono
parents:
diff changeset
362 /* Don't understand, so don't call it. */
kono
parents:
diff changeset
363 break;
kono
parents:
diff changeset
364 }
kono
parents:
diff changeset
365 free (cur);
kono
parents:
diff changeset
366
kono
parents:
diff changeset
367 g = _keymgr_get_and_lock_processwide_ptr (KEYMGR_ATEXIT_LIST);
kono
parents:
diff changeset
368 if (! g)
kono
parents:
diff changeset
369 break;
kono
parents:
diff changeset
370 }
kono
parents:
diff changeset
371 return g;
kono
parents:
diff changeset
372 }
kono
parents:
diff changeset
373
kono
parents:
diff changeset
374 /* Call the routine described by ROUTINE_PARAM and then call any
kono
parents:
diff changeset
375 routines added to KEYMGR_ATEXIT_LIST while that routine was
kono
parents:
diff changeset
376 running, all with in_cxa_finalize set. */
kono
parents:
diff changeset
377
kono
parents:
diff changeset
378 static void
kono
parents:
diff changeset
379 cxa_atexit_wrapper (void* routine_param)
kono
parents:
diff changeset
380 {
kono
parents:
diff changeset
381 struct one_atexit_routine * routine = routine_param;
kono
parents:
diff changeset
382 struct keymgr_atexit_list *g;
kono
parents:
diff changeset
383 struct atexit_routine_list * base = NULL;
kono
parents:
diff changeset
384 char prev_running = 0;
kono
parents:
diff changeset
385
kono
parents:
diff changeset
386 g = _keymgr_get_and_lock_processwide_ptr (KEYMGR_ATEXIT_LIST);
kono
parents:
diff changeset
387 if (g)
kono
parents:
diff changeset
388 {
kono
parents:
diff changeset
389 prev_running = g->running_routines;
kono
parents:
diff changeset
390 g->running_routines = 1;
kono
parents:
diff changeset
391 base = g->l;
kono
parents:
diff changeset
392 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST, g);
kono
parents:
diff changeset
393 }
kono
parents:
diff changeset
394
kono
parents:
diff changeset
395 if (routine->has_arg)
kono
parents:
diff changeset
396 routine->callback.cac (routine->arg);
kono
parents:
diff changeset
397 else
kono
parents:
diff changeset
398 routine->callback.ac ();
kono
parents:
diff changeset
399
kono
parents:
diff changeset
400 if (g)
kono
parents:
diff changeset
401 g = _keymgr_get_and_lock_processwide_ptr (KEYMGR_ATEXIT_LIST);
kono
parents:
diff changeset
402 if (g)
kono
parents:
diff changeset
403 g = run_routines (g, base);
kono
parents:
diff changeset
404 if (g)
kono
parents:
diff changeset
405 {
kono
parents:
diff changeset
406 g->running_routines = prev_running;
kono
parents:
diff changeset
407 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST, g);
kono
parents:
diff changeset
408 }
kono
parents:
diff changeset
409 }
kono
parents:
diff changeset
410
kono
parents:
diff changeset
411 #ifdef __ppc__
kono
parents:
diff changeset
412 /* This code is used while running on 10.3.9, when __cxa_atexit doesn't
kono
parents:
diff changeset
413 exist in the system library. 10.3.9 only supported regular PowerPC,
kono
parents:
diff changeset
414 so this code isn't necessary on x86 or ppc64. */
kono
parents:
diff changeset
415
kono
parents:
diff changeset
416 /* This routine is called from the system atexit(); it runs everything
kono
parents:
diff changeset
417 registered on the KEYMGR_ATEXIT_LIST. */
kono
parents:
diff changeset
418
kono
parents:
diff changeset
419 static void
kono
parents:
diff changeset
420 our_atexit (void)
kono
parents:
diff changeset
421 {
kono
parents:
diff changeset
422 struct keymgr_atexit_list *g;
kono
parents:
diff changeset
423 char prev_running;
kono
parents:
diff changeset
424
kono
parents:
diff changeset
425 g = _keymgr_get_and_lock_processwide_ptr (KEYMGR_ATEXIT_LIST);
kono
parents:
diff changeset
426 if (! g || g->version != 0 || g->atexit_status != atexit_status_missing)
kono
parents:
diff changeset
427 return;
kono
parents:
diff changeset
428
kono
parents:
diff changeset
429 prev_running = g->running_routines;
kono
parents:
diff changeset
430 g->running_routines = 1;
kono
parents:
diff changeset
431 g = run_routines (g, NULL);
kono
parents:
diff changeset
432 if (! g)
kono
parents:
diff changeset
433 return;
kono
parents:
diff changeset
434 g->running_routines = prev_running;
kono
parents:
diff changeset
435 _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST, g);
kono
parents:
diff changeset
436 }
kono
parents:
diff changeset
437 #endif
kono
parents:
diff changeset
438
kono
parents:
diff changeset
439 /* This is our wrapper around atexit and __cxa_atexit. It will return
kono
parents:
diff changeset
440 nonzero if an error occurs, and otherwise:
kono
parents:
diff changeset
441 - if in_cxa_finalize is set, or running on 10.3.9, add R to
kono
parents:
diff changeset
442 KEYMGR_ATEXIT_LIST; or
kono
parents:
diff changeset
443 - call the system __cxa_atexit to add cxa_atexit_wrapper with an argument
kono
parents:
diff changeset
444 that indicates how cxa_atexit_wrapper should call R. */
kono
parents:
diff changeset
445
kono
parents:
diff changeset
446 static int
kono
parents:
diff changeset
447 atexit_common (const struct one_atexit_routine *r, const void *dso)
kono
parents:
diff changeset
448 {
kono
parents:
diff changeset
449 struct keymgr_atexit_list *g = get_globals ();
kono
parents:
diff changeset
450
kono
parents:
diff changeset
451 if (! g)
kono
parents:
diff changeset
452 return -1;
kono
parents:
diff changeset
453
kono
parents:
diff changeset
454 if (g->running_routines || g->atexit_status == atexit_status_missing)
kono
parents:
diff changeset
455 return add_routine (g, r);
kono
parents:
diff changeset
456
kono
parents:
diff changeset
457 if (g->atexit_status >= atexit_status_working)
kono
parents:
diff changeset
458 {
kono
parents:
diff changeset
459 int result;
kono
parents:
diff changeset
460 if (r->has_arg)
kono
parents:
diff changeset
461 {
kono
parents:
diff changeset
462 cxa_atexit_p cxa_atexit = g->cxa_atexit_f;
kono
parents:
diff changeset
463 result = _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST,
kono
parents:
diff changeset
464 g);
kono
parents:
diff changeset
465 if (CHECK_KEYMGR_ERROR (result))
kono
parents:
diff changeset
466 return -1;
kono
parents:
diff changeset
467 return cxa_atexit (r->callback.cac, r->arg, dso);
kono
parents:
diff changeset
468 }
kono
parents:
diff changeset
469 else
kono
parents:
diff changeset
470 {
kono
parents:
diff changeset
471 atexit_p atexit_f = g->atexit_f;
kono
parents:
diff changeset
472 result = _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST,
kono
parents:
diff changeset
473 g);
kono
parents:
diff changeset
474 if (CHECK_KEYMGR_ERROR (result))
kono
parents:
diff changeset
475 return -1;
kono
parents:
diff changeset
476 return atexit_f (r->callback.ac);
kono
parents:
diff changeset
477 }
kono
parents:
diff changeset
478 }
kono
parents:
diff changeset
479 else
kono
parents:
diff changeset
480 {
kono
parents:
diff changeset
481 cxa_atexit_p cxa_atexit = g->cxa_atexit_f;
kono
parents:
diff changeset
482 struct one_atexit_routine *alloced;
kono
parents:
diff changeset
483 int result;
kono
parents:
diff changeset
484
kono
parents:
diff changeset
485 result = _keymgr_set_and_unlock_processwide_ptr (KEYMGR_ATEXIT_LIST, g);
kono
parents:
diff changeset
486 if (CHECK_KEYMGR_ERROR (result))
kono
parents:
diff changeset
487 return -1;
kono
parents:
diff changeset
488
kono
parents:
diff changeset
489 alloced = malloc (sizeof (struct one_atexit_routine));
kono
parents:
diff changeset
490 if (! alloced)
kono
parents:
diff changeset
491 return -1;
kono
parents:
diff changeset
492 *alloced = *r;
kono
parents:
diff changeset
493 return cxa_atexit (cxa_atexit_wrapper, alloced, dso);
kono
parents:
diff changeset
494 }
kono
parents:
diff changeset
495 }
kono
parents:
diff changeset
496
kono
parents:
diff changeset
497 /* These are the actual replacement routines; they just funnel into
kono
parents:
diff changeset
498 atexit_common. */
kono
parents:
diff changeset
499
kono
parents:
diff changeset
500 int __cxa_atexit (cxa_atexit_callback func, void* arg,
kono
parents:
diff changeset
501 const void* dso) __attribute__((visibility("hidden")));
kono
parents:
diff changeset
502
kono
parents:
diff changeset
503 int
kono
parents:
diff changeset
504 __cxa_atexit (cxa_atexit_callback func, void* arg, const void* dso)
kono
parents:
diff changeset
505 {
kono
parents:
diff changeset
506 struct one_atexit_routine r;
kono
parents:
diff changeset
507 r.callback.cac = func;
kono
parents:
diff changeset
508 r.has_arg = 1;
kono
parents:
diff changeset
509 r.arg = arg;
kono
parents:
diff changeset
510 return atexit_common (&r, dso);
kono
parents:
diff changeset
511 }
kono
parents:
diff changeset
512
kono
parents:
diff changeset
513 int atexit (atexit_callback func) __attribute__((visibility("hidden")));
kono
parents:
diff changeset
514
kono
parents:
diff changeset
515 /* Use __dso_handle to allow even bundles that call atexit() to be unloaded
kono
parents:
diff changeset
516 on 10.4. */
kono
parents:
diff changeset
517 extern void __dso_handle;
kono
parents:
diff changeset
518
kono
parents:
diff changeset
519 int
kono
parents:
diff changeset
520 atexit (atexit_callback func)
kono
parents:
diff changeset
521 {
kono
parents:
diff changeset
522 struct one_atexit_routine r;
kono
parents:
diff changeset
523 r.callback.ac = func;
kono
parents:
diff changeset
524 r.has_arg = 0;
kono
parents:
diff changeset
525 return atexit_common (&r, &__dso_handle);
kono
parents:
diff changeset
526 }
kono
parents:
diff changeset
527
kono
parents:
diff changeset
528 #endif /* __PIC__ */