crtbegin.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*-
  2. * SPDX-License-Identifier: BSD-1-Clause
  3. *
  4. * Copyright 2018 Andrew Turner
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  13. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  14. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  16. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  17. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  18. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  19. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  21. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #include <sys/param.h>
  24. #include "crt.h"
  25. typedef void (*crt_func)(void);
  26. extern void *__dso_handle __hidden;
  27. #ifndef SHARED
  28. void *__dso_handle = 0;
  29. #else
  30. void *__dso_handle = &__dso_handle;
  31. void __cxa_finalize(void *) __weak_symbol;
  32. /*
  33. * Call __cxa_finalize with the dso handle in shared objects.
  34. * When we have ctors/dtors call from the dtor handler before calling
  35. * any dtors, otherwise use a destructor.
  36. */
  37. #ifndef HAVE_CTORS
  38. __attribute__((destructor))
  39. #endif
  40. static void
  41. run_cxa_finalize(void)
  42. {
  43. if (__cxa_finalize != NULL)
  44. __cxa_finalize(__dso_handle);
  45. }
  46. #endif
  47. /*
  48. * On some architectures and toolchains we may need to call the .dtors.
  49. * These are called in the order they are in the ELF file.
  50. */
  51. #ifdef HAVE_CTORS
  52. static void __do_global_dtors_aux(void) __used;
  53. static crt_func __CTOR_LIST__[] __section(".ctors") __used = {
  54. (crt_func)-1
  55. };
  56. static crt_func __DTOR_LIST__[] __section(".dtors") __used = {
  57. (crt_func)-1
  58. };
  59. static void
  60. __do_global_dtors_aux(void)
  61. {
  62. crt_func fn;
  63. int n;
  64. #ifdef SHARED
  65. run_cxa_finalize();
  66. #endif
  67. for (n = 1;; n++) {
  68. fn = __DTOR_LIST__[n];
  69. if (fn == (crt_func)0 || fn == (crt_func)-1)
  70. break;
  71. fn();
  72. }
  73. }
  74. asm (
  75. ".pushsection .fini \n"
  76. "\t" INIT_CALL_SEQ(__do_global_dtors_aux) "\n"
  77. ".popsection \n"
  78. );
  79. #endif
  80. /*
  81. * Handler for gcj. These provide a _Jv_RegisterClasses function and fill
  82. * out the .jcr section. We just need to call this function with a pointer
  83. * to the appropriate section.
  84. */
  85. extern void _Jv_RegisterClasses(void *) __weak_symbol;
  86. static void register_classes(void) __used;
  87. static crt_func __JCR_LIST__[] __section(".jcr") __used = { };
  88. #ifndef CTORS_CONSTRUCTORS
  89. __attribute__((constructor))
  90. #endif
  91. static void
  92. register_classes(void)
  93. {
  94. if (_Jv_RegisterClasses != NULL && __JCR_LIST__[0] != 0)
  95. _Jv_RegisterClasses(__JCR_LIST__);
  96. }
  97. /*
  98. * We can't use constructors when they use the .ctors section as they may be
  99. * placed before __CTOR_LIST__.
  100. */
  101. #ifdef CTORS_CONSTRUCTORS
  102. asm (
  103. ".pushsection .init \n"
  104. "\t" INIT_CALL_SEQ(register_classes) "\n"
  105. ".popsection \n"
  106. );
  107. #endif