cxxabi.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2019-2020, Andrew Kaster <andrewdkaster@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <assert.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <sys/internals.h>
  30. //#define GLOBAL_DTORS_DEBUG
  31. extern "C" {
  32. struct __exit_entry {
  33. AtExitFunction method;
  34. void* parameter;
  35. void* dso_handle;
  36. bool has_been_called;
  37. };
  38. static __exit_entry __exit_entries[1024] {};
  39. static int __exit_entry_count = 0;
  40. int __cxa_atexit(AtExitFunction exit_function, void* parameter, void* dso_handle)
  41. {
  42. if (__exit_entry_count >= 1024)
  43. return -1;
  44. __exit_entries[__exit_entry_count++] = { exit_function, parameter, dso_handle, false };
  45. return 0;
  46. }
  47. void __cxa_finalize(void* dso_handle)
  48. {
  49. // From the itanium abi, https://itanium-cxx-abi.github.io/cxx-abi/abi.html#dso-dtor-runtime-api
  50. //
  51. // When __cxa_finalize(d) is called, it should walk the termination function list, calling each in turn
  52. // if d matches __dso_handle for the termination function entry. If d == NULL, it should call all of them.
  53. // Multiple calls to __cxa_finalize shall not result in calling termination function entries multiple times;
  54. // the implementation may either remove entries or mark them finished.
  55. int entry_index = __exit_entry_count;
  56. #ifdef GLOBAL_DTORS_DEBUG
  57. dbgprintf("__cxa_finalize: %d entries in the finalizer list\n", entry_index);
  58. #endif
  59. while (--entry_index >= 0) {
  60. auto& exit_entry = __exit_entries[entry_index];
  61. bool needs_calling = !exit_entry.has_been_called && (!dso_handle || dso_handle == exit_entry.dso_handle);
  62. if (needs_calling) {
  63. #ifdef GLOBAL_DTORS_DEBUG
  64. dbgprintf("__cxa_finalize: calling entry[%d] %p(%p) dso: %p\n", entry_index, exit_entry.method, exit_entry.parameter, exit_entry.dso_handle);
  65. #endif
  66. exit_entry.method(exit_entry.parameter);
  67. exit_entry.has_been_called = true;
  68. }
  69. }
  70. }
  71. [[noreturn]] void __cxa_pure_virtual()
  72. {
  73. ASSERT_NOT_REACHED();
  74. }
  75. } // extern "C"