xref: /haiku/src/system/glue/crtbegin.c (revision c237c4ce593ee823d9867fd997e51e4c447f5623)
1 /* ===-- crtbegin.c - Provide __dso_handle ---------------------------------===
2  *
3  *      	       The LLVM Compiler Infrastructure
4  *
5  * This file is dual licensed under the MIT and the University of Illinois Open
6  * Source Licenses. See LICENSE.TXT for details.
7  *
8  * ===----------------------------------------------------------------------===
9  */
10 
11 __attribute__((visibility("hidden")))
12 #ifdef CRT_SHARED
13   void *__dso_handle = &__dso_handle;
14 #else
15   void *__dso_handle = (void *)0;
16 #endif
17 
18 static const long __EH_FRAME_LIST__[]
19   __attribute__((section(".eh_frame"), aligned(4), visibility("hidden"))) = {};
20 
21 struct object {
22   void *p[8];
23 };
24 
25 extern void __register_frame_info(const void *, void *)
26   __attribute__((weak));
27 
28 extern void *__deregister_frame_info(const void *) __attribute__((weak));
29 extern void *__deregister_frame_info_bases(const void *) __attribute__((weak));
30 
31 #ifndef CRT_HAS_INITFINI_ARRAY
32 typedef void (*fp)(void);
33 static const fp __CTOR_LIST__[]
34   __attribute__((section(".ctors"), aligned(sizeof(fp)), visibility("hidden"), used)) = { (fp)-1 };
35 extern const fp __CTOR_END__[] __attribute__((visibility("hidden")));
36 #endif
37 
38 #ifdef CRT_SHARED
39 extern void __cxa_finalize(void *) __attribute__((weak));
40 #endif
41 
42 static void __attribute__((used)) __do_init() {
43   static _Bool __initialized;
44   if (__builtin_expect(__initialized, 0))
45     return;
46   __initialized = 1;
47 
48   static struct object ob;
49   if (__register_frame_info)
50     __register_frame_info(__EH_FRAME_LIST__, &ob);
51 
52 #ifndef CRT_HAS_INITFINI_ARRAY
53   unsigned long n = (unsigned long)__CTOR_LIST__[0];
54   if (n == (unsigned long)-1)
55     for (n = 0; __CTOR_LIST__[n + 1] != 0; n++);
56   for (unsigned long i = n; i >= 1; i--)
57     __CTOR_LIST__[i]();
58 #endif
59 }
60 
61 #ifndef CRT_HAS_INITFINI_ARRAY
62 static const fp __DTOR_LIST__[]
63   __attribute__((section(".dtors"), aligned(sizeof(fp)), visibility("hidden"), used)) = { (fp)-1 };
64 extern const fp __DTOR_END__[] __attribute__((visibility("hidden")));
65 #endif
66 
67 __attribute__((section(".init_array"), used))
68   static void (*__init)(void) = __do_init;
69 
70 static void __attribute__((used)) __do_fini() {
71   static _Bool __finalized;
72   if (__builtin_expect(__finalized, 0))
73     return;
74   __finalized = 1;
75 
76 #ifdef CRT_SHARED
77   if (__cxa_finalize)
78     __cxa_finalize(__dso_handle);
79 #endif
80 
81   if (__deregister_frame_info)
82     __deregister_frame_info(__EH_FRAME_LIST__);
83 
84 #ifndef CRT_HAS_INITFINI_ARRAY
85   for (unsigned long i = 1; __DTOR_LIST__[i]; i++)
86     __DTOR_LIST__[i]();
87 #endif
88 }
89 
90 __attribute__((section(".fini_array"), used))
91   static void (*__fini)(void) = __do_fini;
92