1 //===-- crtbegin.c - Start of constructors and destructors ----------------===//
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
7 //===----------------------------------------------------------------------===//
11 __attribute__((visibility("hidden"))) void *__dso_handle
= &__dso_handle
;
13 #ifdef EH_USE_FRAME_REGISTRY
14 __extension__
static void *__EH_FRAME_LIST__
[]
15 __attribute__((section(".eh_frame"), aligned(sizeof(void *)))) = {};
17 extern void __register_frame_info(const void *, void *) __attribute__((weak
));
18 extern void *__deregister_frame_info(const void *) __attribute__((weak
));
21 #ifndef CRT_HAS_INITFINI_ARRAY
22 typedef void (*fp
)(void);
24 static fp __CTOR_LIST__
[]
25 __attribute__((section(".ctors"), aligned(sizeof(fp
)))) = {(fp
)-1};
26 extern fp __CTOR_LIST_END__
[];
29 extern void __cxa_finalize(void *) __attribute__((weak
));
31 static void __attribute__((used
)) __do_init(void) {
32 static _Bool __initialized
;
33 if (__builtin_expect(__initialized
, 0))
37 #ifdef EH_USE_FRAME_REGISTRY
38 static struct { void *p
[8]; } __object
;
39 if (__register_frame_info
)
40 __register_frame_info(__EH_FRAME_LIST__
, &__object
);
42 #ifndef CRT_HAS_INITFINI_ARRAY
43 const size_t n
= __CTOR_LIST_END__
- __CTOR_LIST__
- 1;
44 for (size_t i
= n
; i
>= 1; i
--) __CTOR_LIST__
[i
]();
48 #ifdef CRT_HAS_INITFINI_ARRAY
49 __attribute__((section(".init_array"),
50 used
)) static void (*__init
)(void) = __do_init
;
51 #elif defined(__i386__) || defined(__x86_64__)
52 __asm__(".pushsection .init,\"ax\",@progbits\n\t"
53 "call " __USER_LABEL_PREFIX__
"__do_init\n\t"
55 #elif defined(__riscv)
56 __asm__(".pushsection .init,\"ax\",%progbits\n\t"
57 "call " __USER_LABEL_PREFIX__
"__do_init\n\t"
59 #elif defined(__arm__) || defined(__aarch64__)
60 __asm__(".pushsection .init,\"ax\",%progbits\n\t"
61 "bl " __USER_LABEL_PREFIX__
"__do_init\n\t"
63 #elif defined(__powerpc__) || defined(__powerpc64__)
64 __asm__(".pushsection .init,\"ax\",@progbits\n\t"
65 "bl " __USER_LABEL_PREFIX__
"__do_init\n\t"
68 #elif defined(__sparc__)
69 __asm__(".pushsection .init,\"ax\",@progbits\n\t"
70 "call " __USER_LABEL_PREFIX__
"__do_init\n\t"
73 #error "crtbegin without .init_fini array unimplemented for this architecture"
74 #endif // CRT_HAS_INITFINI_ARRAY
76 #ifndef CRT_HAS_INITFINI_ARRAY
77 static fp __DTOR_LIST__
[]
78 __attribute__((section(".dtors"), aligned(sizeof(fp
)))) = {(fp
)-1};
79 extern fp __DTOR_LIST_END__
[];
82 static void __attribute__((used
)) __do_fini(void) {
83 static _Bool __finalized
;
84 if (__builtin_expect(__finalized
, 0))
89 __cxa_finalize(__dso_handle
);
91 #ifndef CRT_HAS_INITFINI_ARRAY
92 const size_t n
= __DTOR_LIST_END__
- __DTOR_LIST__
- 1;
93 for (size_t i
= 1; i
<= n
; i
++) __DTOR_LIST__
[i
]();
95 #ifdef EH_USE_FRAME_REGISTRY
96 if (__deregister_frame_info
)
97 __deregister_frame_info(__EH_FRAME_LIST__
);
101 #ifdef CRT_HAS_INITFINI_ARRAY
102 __attribute__((section(".fini_array"),
103 used
)) static void (*__fini
)(void) = __do_fini
;
104 #elif defined(__i386__) || defined(__x86_64__)
105 __asm__(".pushsection .fini,\"ax\",@progbits\n\t"
106 "call " __USER_LABEL_PREFIX__
"__do_fini\n\t"
108 #elif defined(__arm__) || defined(__aarch64__)
109 __asm__(".pushsection .fini,\"ax\",%progbits\n\t"
110 "bl " __USER_LABEL_PREFIX__
"__do_fini\n\t"
112 #elif defined(__powerpc__) || defined(__powerpc64__)
113 __asm__(".pushsection .fini,\"ax\",@progbits\n\t"
114 "bl " __USER_LABEL_PREFIX__
"__do_fini\n\t"
117 #elif defined(__riscv)
118 __asm__(".pushsection .fini,\"ax\",@progbits\n\t"
119 "call " __USER_LABEL_PREFIX__
"__do_fini\n\t"
121 #elif defined(__sparc__)
122 __asm__(".pushsection .fini,\"ax\",@progbits\n\t"
123 "call " __USER_LABEL_PREFIX__
"__do_fini\n\t"
126 #error "crtbegin without .init_fini array unimplemented for this architecture"
127 #endif // CRT_HAS_INIT_FINI_ARRAY