cxa_finalize.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Copyright (C) 1999-2022 Free Software Foundation, Inc.
  2. NOTE: This source is derived from an old version taken from the GNU C
  3. Library (glibc).
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include <assert.h>
  21. #include <stdlib.h>
  22. #include "exit.h"
  23. static boolean_t
  24. catomic_compare_and_exchange_bool_acq (long *mem, long newval, long oldval)
  25. {
  26. return ! __atomic_compare_exchange (mem, &oldval, &newval, 0,
  27. __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE);
  28. }
  29. /* If D is non-NULL, call all functions registered with `__cxa_atexit'
  30. with the same dso handle. Otherwise, if D is NULL, call all of the
  31. registered handlers. */
  32. void
  33. __cxa_finalize (void *d)
  34. {
  35. struct exit_function_list *funcs;
  36. restart:
  37. for (funcs = __exit_funcs; funcs; funcs = funcs->next)
  38. {
  39. struct exit_function *f;
  40. for (f = &funcs->fns[funcs->idx - 1]; f >= &funcs->fns[0]; --f)
  41. {
  42. void (*cxafn) (void *arg, int status);
  43. void *cxaarg;
  44. if ((d == NULL || d == f->func.cxa.dso_handle)
  45. /* We don't want to run this cleanup more than once. */
  46. && (cxafn = f->func.cxa.fn,
  47. cxaarg = f->func.cxa.arg,
  48. ! catomic_compare_and_exchange_bool_acq (&f->flavor, ef_free,
  49. ef_cxa)))
  50. {
  51. uint64_t check = __new_exitfn_called;
  52. #ifdef PTR_DEMANGLE
  53. PTR_DEMANGLE (cxafn);
  54. #endif
  55. cxafn (cxaarg, 0);
  56. /* It is possible that that last exit function registered
  57. more exit functions. Start the loop over. */
  58. if (__builtin_expect (check != __new_exitfn_called, 0))
  59. goto restart;
  60. }
  61. }
  62. }
  63. /* Remove the registered fork handlers. We do not have to
  64. unregister anything if the program is going to terminate anyway. */
  65. #ifdef UNREGISTER_ATFORK
  66. if (d != NULL)
  67. UNREGISTER_ATFORK (d);
  68. #endif
  69. }