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 __extension__ static void *__EH_FRAME_LIST__[]
14 __attribute__((section(".eh_frame"), aligned(sizeof(void *)))) = {};
16 extern void __register_frame_info(const void *, void *) __attribute__((weak));
17 extern void *__deregister_frame_info(const void *) __attribute__((weak));
19 #ifndef CRT_HAS_INITFINI_ARRAY
20 typedef void (*fp)(void);
22 static fp __CTOR_LIST__[]
23 __attribute__((section(".ctors"), aligned(sizeof(fp)))) = {(fp)-1};
24 extern fp __CTOR_LIST_END__[];
27 extern void __cxa_finalize(void *) __attribute__((weak));
29 static void __attribute__((used)) __do_init() {
30 static _Bool __initialized;
31 if (__builtin_expect(__initialized, 0))
35 static struct { void *p[8]; } __object;
36 if (__register_frame_info)
37 __register_frame_info(__EH_FRAME_LIST__, &__object);
39 #ifndef CRT_HAS_INITFINI_ARRAY
40 const size_t n = __CTOR_LIST_END__ - __CTOR_LIST__ - 1;
41 for (size_t i = n; i >= 1; i--) __CTOR_LIST__[i]();
45 #ifdef CRT_HAS_INITFINI_ARRAY
46 __attribute__((section(".init_array"),
47 used)) static void (*__init)(void) = __do_init;
48 #else // CRT_HAS_INITFINI_ARRAY
49 #if defined(__i386__) || defined(__x86_64__)
50 __asm__(".pushsection .init,\"ax\",@progbits\n\t"
51 "call " __USER_LABEL_PREFIX__ "__do_init\n\t"
53 #elif defined(__arm__)
54 __asm__(".pushsection .init,\"ax\",%progbits\n\t"
55 "bl " __USER_LABEL_PREFIX__ "__do_init\n\t"
57 #endif // CRT_HAS_INITFINI_ARRAY
60 #ifndef CRT_HAS_INITFINI_ARRAY
61 static fp __DTOR_LIST__[]
62 __attribute__((section(".dtors"), aligned(sizeof(fp)))) = {(fp)-1};
63 extern fp __DTOR_LIST_END__[];
66 static void __attribute__((used)) __do_fini() {
67 static _Bool __finalized;
68 if (__builtin_expect(__finalized, 0))
73 __cxa_finalize(__dso_handle);
75 #ifndef CRT_HAS_INITFINI_ARRAY
76 if (__deregister_frame_info)
77 __deregister_frame_info(__EH_FRAME_LIST__);
79 const size_t n = __DTOR_LIST_END__ - __DTOR_LIST__ - 1;
80 for (size_t i = 1; i <= n; i++) __DTOR_LIST__[i]();
84 #ifdef CRT_HAS_INITFINI_ARRAY
85 __attribute__((section(".fini_array"),
86 used)) static void (*__fini)(void) = __do_fini;
87 #else // CRT_HAS_INITFINI_ARRAY
88 #if defined(__i386__) || defined(__x86_64__)
89 __asm__(".pushsection .fini,\"ax\",@progbits\n\t"
90 "call " __USER_LABEL_PREFIX__ "__do_fini\n\t"
92 #elif defined(__arm__)
93 __asm__(".pushsection .fini,\"ax\",%progbits\n\t"
94 "bl " __USER_LABEL_PREFIX__ "__do_fini\n\t"
97 #endif // CRT_HAS_INIT_FINI_ARRAY