darwin-crt3.c 15 KB

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