crtbegin.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //===-- crtbegin.c - Start of constructors and destructors ----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include <stddef.h>
  9. __attribute__((visibility("hidden"))) void *__dso_handle = &__dso_handle;
  10. #ifdef EH_USE_FRAME_REGISTRY
  11. __extension__ static void *__EH_FRAME_LIST__[]
  12. __attribute__((section(".eh_frame"), aligned(sizeof(void *)))) = {};
  13. extern void __register_frame_info(const void *, void *) __attribute__((weak));
  14. extern void *__deregister_frame_info(const void *) __attribute__((weak));
  15. #endif
  16. #ifndef CRT_HAS_INITFINI_ARRAY
  17. typedef void (*fp)(void);
  18. static fp __CTOR_LIST__[]
  19. __attribute__((section(".ctors"), aligned(sizeof(fp)))) = {(fp)-1};
  20. extern fp __CTOR_LIST_END__[];
  21. #endif
  22. extern void __cxa_finalize(void *) __attribute__((weak));
  23. static void __attribute__((used)) __do_init(void) {
  24. static _Bool __initialized;
  25. if (__builtin_expect(__initialized, 0))
  26. return;
  27. __initialized = 1;
  28. #ifdef EH_USE_FRAME_REGISTRY
  29. static struct { void *p[8]; } __object;
  30. if (__register_frame_info)
  31. __register_frame_info(__EH_FRAME_LIST__, &__object);
  32. #endif
  33. #ifndef CRT_HAS_INITFINI_ARRAY
  34. const size_t n = __CTOR_LIST_END__ - __CTOR_LIST__ - 1;
  35. for (size_t i = n; i >= 1; i--) __CTOR_LIST__[i]();
  36. #endif
  37. }
  38. #ifdef CRT_HAS_INITFINI_ARRAY
  39. __attribute__((section(".init_array"),
  40. used)) static void (*__init)(void) = __do_init;
  41. #elif defined(__i386__) || defined(__x86_64__)
  42. __asm__(".pushsection .init,\"ax\",@progbits\n\t"
  43. "call __do_init\n\t"
  44. ".popsection");
  45. #elif defined(__riscv)
  46. __asm__(".pushsection .init,\"ax\",%progbits\n\t"
  47. "call __do_init\n\t"
  48. ".popsection");
  49. #elif defined(__arm__) || defined(__aarch64__)
  50. __asm__(".pushsection .init,\"ax\",%progbits\n\t"
  51. "bl __do_init\n\t"
  52. ".popsection");
  53. #elif defined(__mips__)
  54. __asm__(".pushsection .init,\"ax\",@progbits\n\t"
  55. "jal __do_init\n\t"
  56. ".popsection");
  57. #elif defined(__powerpc__) || defined(__powerpc64__)
  58. __asm__(".pushsection .init,\"ax\",@progbits\n\t"
  59. "bl __do_init\n\t"
  60. "nop\n\t"
  61. ".popsection");
  62. #elif defined(__sparc__)
  63. __asm__(".pushsection .init,\"ax\",@progbits\n\t"
  64. "call __do_init\n\t"
  65. ".popsection");
  66. #else
  67. #error "crtbegin without .init_fini array unimplemented for this architecture"
  68. #endif // CRT_HAS_INITFINI_ARRAY
  69. #ifndef CRT_HAS_INITFINI_ARRAY
  70. static fp __DTOR_LIST__[]
  71. __attribute__((section(".dtors"), aligned(sizeof(fp)))) = {(fp)-1};
  72. extern fp __DTOR_LIST_END__[];
  73. #endif
  74. static void __attribute__((used)) __do_fini(void) {
  75. static _Bool __finalized;
  76. if (__builtin_expect(__finalized, 0))
  77. return;
  78. __finalized = 1;
  79. if (__cxa_finalize)
  80. __cxa_finalize(__dso_handle);
  81. #ifndef CRT_HAS_INITFINI_ARRAY
  82. const size_t n = __DTOR_LIST_END__ - __DTOR_LIST__ - 1;
  83. for (size_t i = 1; i <= n; i++) __DTOR_LIST__[i]();
  84. #endif
  85. #ifdef EH_USE_FRAME_REGISTRY
  86. if (__deregister_frame_info)
  87. __deregister_frame_info(__EH_FRAME_LIST__);
  88. #endif
  89. }
  90. #ifdef CRT_HAS_INITFINI_ARRAY
  91. __attribute__((section(".fini_array"),
  92. used)) static void (*__fini)(void) = __do_fini;
  93. #elif defined(__i386__) || defined(__x86_64__)
  94. __asm__(".pushsection .fini,\"ax\",@progbits\n\t"
  95. "call __do_fini\n\t"
  96. ".popsection");
  97. #elif defined(__arm__) || defined(__aarch64__)
  98. __asm__(".pushsection .fini,\"ax\",%progbits\n\t"
  99. "bl __do_fini\n\t"
  100. ".popsection");
  101. #elif defined(__mips__)
  102. __asm__(".pushsection .fini,\"ax\",@progbits\n\t"
  103. "jal __do_fini\n\t"
  104. ".popsection");
  105. #elif defined(__powerpc__) || defined(__powerpc64__)
  106. __asm__(".pushsection .fini,\"ax\",@progbits\n\t"
  107. "bl __do_fini\n\t"
  108. "nop\n\t"
  109. ".popsection");
  110. #elif defined(__riscv)
  111. __asm__(".pushsection .fini,\"ax\",@progbits\n\t"
  112. "call __do_fini\n\t"
  113. ".popsection");
  114. #elif defined(__sparc__)
  115. __asm__(".pushsection .fini,\"ax\",@progbits\n\t"
  116. "call __do_fini\n\t"
  117. ".popsection");
  118. #else
  119. #error "crtbegin without .init_fini array unimplemented for this architecture"
  120. #endif // CRT_HAS_INIT_FINI_ARRAY