]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/compiler-rt/lib/crt/crtbegin.c
MFC r355940:
[FreeBSD/FreeBSD.git] / contrib / llvm-project / compiler-rt / lib / crt / crtbegin.c
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
9 #include <stddef.h>
10
11 __attribute__((visibility("hidden"))) void *__dso_handle = &__dso_handle;
12
13 __extension__ static void *__EH_FRAME_LIST__[]
14     __attribute__((section(".eh_frame"), aligned(sizeof(void *)))) = {};
15
16 extern void __register_frame_info(const void *, void *) __attribute__((weak));
17 extern void *__deregister_frame_info(const void *) __attribute__((weak));
18
19 #ifndef CRT_HAS_INITFINI_ARRAY
20 typedef void (*fp)(void);
21
22 static fp __CTOR_LIST__[]
23     __attribute__((section(".ctors"), aligned(sizeof(fp)))) = {(fp)-1};
24 extern fp __CTOR_LIST_END__[];
25 #endif
26
27 extern void __cxa_finalize(void *) __attribute__((weak));
28
29 static void __attribute__((used)) __do_init() {
30   static _Bool __initialized;
31   if (__builtin_expect(__initialized, 0))
32     return;
33   __initialized = 1;
34
35   static struct { void *p[8]; } __object;
36   if (__register_frame_info)
37     __register_frame_info(__EH_FRAME_LIST__, &__object);
38
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]();
42 #endif
43 }
44
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"
52     ".popsection");
53 #elif defined(__arm__)
54 __asm__(".pushsection .init,\"ax\",%progbits\n\t"
55     "bl " __USER_LABEL_PREFIX__ "__do_init\n\t"
56     ".popsection");
57 #endif  // CRT_HAS_INITFINI_ARRAY
58 #endif
59
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__[];
64 #endif
65
66 static void __attribute__((used)) __do_fini() {
67   static _Bool __finalized;
68   if (__builtin_expect(__finalized, 0))
69     return;
70   __finalized = 1;
71
72   if (__cxa_finalize)
73     __cxa_finalize(__dso_handle);
74
75 #ifndef CRT_HAS_INITFINI_ARRAY
76   if (__deregister_frame_info)
77     __deregister_frame_info(__EH_FRAME_LIST__);
78
79   const size_t n = __DTOR_LIST_END__ - __DTOR_LIST__ - 1;
80   for (size_t i = 1; i <= n; i++) __DTOR_LIST__[i]();
81 #endif
82 }
83
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"
91     ".popsection");
92 #elif defined(__arm__)
93 __asm__(".pushsection .fini,\"ax\",%progbits\n\t"
94     "bl " __USER_LABEL_PREFIX__ "__do_fini\n\t"
95     ".popsection");
96 #endif
97 #endif  // CRT_HAS_INIT_FINI_ARRAY