]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/csu/common/crtbegin.c
MFC r339738, r339744, r339770, r339773, r339864-r339866, r339907-r339908, r339912...
[FreeBSD/FreeBSD.git] / lib / csu / common / crtbegin.c
1 /*-
2  * SPDX-License-Identifier: BSD-1-Clause
3  *
4  * Copyright 2018 Andrew Turner
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
13  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
14  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
15  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
16  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
17  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
18  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
19  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
21  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22  */
23
24 #include <sys/cdefs.h>
25 __FBSDID("$FreeBSD$");
26
27 #include <sys/param.h>
28
29 #include "crt.h"
30
31 typedef void (*crt_func)(void);
32
33 extern void *__dso_handle __hidden;
34
35 #ifndef SHARED
36 void *__dso_handle = 0;
37 #else
38 void *__dso_handle = &__dso_handle;
39 void __cxa_finalize(void *) __weak_symbol;
40
41 /*
42  * Call __cxa_finalize with the dso handle in shared objects.
43  * When we have ctors/dtors call from the dtor handler before calling
44  * any dtors, otherwise use a destructor.
45  */
46 #ifndef HAVE_CTORS
47 __attribute__((destructor))
48 #endif
49 static void
50 run_cxa_finalize(void)
51 {
52
53         if (__cxa_finalize != NULL)
54                 __cxa_finalize(__dso_handle);
55 }
56 #endif
57
58 /*
59  * On some architectures and toolchains we may need to call the .dtors.
60  * These are called in the order they are in the ELF file.
61  */
62 #ifdef HAVE_CTORS
63 static void __do_global_dtors_aux(void) __used;
64
65 static crt_func __CTOR_LIST__[] __section(".ctors") __used = {
66         (crt_func)-1
67 };
68 static crt_func __DTOR_LIST__[] __section(".dtors") __used = {
69         (crt_func)-1
70 };
71
72 static void
73 __do_global_dtors_aux(void)
74 {
75         crt_func fn;
76         int n;
77
78 #ifdef SHARED
79         run_cxa_finalize();
80 #endif
81
82         for (n = 1;; n++) {
83                 fn = __DTOR_LIST__[n];
84                 if (fn == (crt_func)0 || fn == (crt_func)-1)
85                         break;
86                 fn();
87         }
88 }
89
90 asm (
91     ".pushsection .fini         \n"
92     "\t" INIT_CALL_SEQ(__do_global_dtors_aux) "\n"
93     ".popsection                \n"
94 );
95 #endif
96
97 /*
98  * Handler for gcj. These provide a _Jv_RegisterClasses function and fill
99  * out the .jcr section. We just need to call this function with a pointer
100  * to the appropriate section.
101  */
102 extern void _Jv_RegisterClasses(void *) __weak_symbol;
103 static void register_classes(void) __used;
104
105 static crt_func __JCR_LIST__[] __section(".jcr") __used = { };
106
107 #ifndef CTORS_CONSTRUCTORS
108 __attribute__((constructor))
109 #endif
110 static void
111 register_classes(void)
112 {
113
114         if (_Jv_RegisterClasses != NULL && __JCR_LIST__[0] != 0)
115                 _Jv_RegisterClasses(__JCR_LIST__);
116 }
117
118 /*
119  * We can't use constructors when they use the .ctors section as they may be
120  * placed before __CTOR_LIST__.
121  */
122 #ifdef CTORS_CONSTRUCTORS
123 asm (
124     ".pushsection .init         \n"
125     "\t" INIT_CALL_SEQ(register_classes) "\n"
126     ".popsection                \n"
127 );
128 #endif