xref: /freebsd/lib/csu/common/crtbegin.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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 #include <sys/param.h>
26 
27 #include "crt.h"
28 
29 typedef void (*crt_func)(void);
30 
31 extern void *__dso_handle __hidden;
32 
33 #ifndef SHARED
34 void *__dso_handle = 0;
35 #else
36 void *__dso_handle = &__dso_handle;
37 void __cxa_finalize(void *) __weak_symbol;
38 
39 /*
40  * Call __cxa_finalize with the dso handle in shared objects.
41  * When we have ctors/dtors call from the dtor handler before calling
42  * any dtors, otherwise use a destructor.
43  */
44 #ifndef HAVE_CTORS
45 __attribute__((destructor))
46 #endif
47 static void
48 run_cxa_finalize(void)
49 {
50 
51 	if (__cxa_finalize != NULL)
52 		__cxa_finalize(__dso_handle);
53 }
54 #endif
55 
56 /*
57  * On some architectures and toolchains we may need to call the .dtors.
58  * These are called in the order they are in the ELF file.
59  */
60 #ifdef HAVE_CTORS
61 static void __do_global_dtors_aux(void) __used;
62 
63 static crt_func __CTOR_LIST__[] __section(".ctors") __used = {
64 	(crt_func)-1
65 };
66 static crt_func __DTOR_LIST__[] __section(".dtors") __used = {
67 	(crt_func)-1
68 };
69 
70 static void
71 __do_global_dtors_aux(void)
72 {
73 	crt_func fn;
74 	int n;
75 
76 #ifdef SHARED
77 	run_cxa_finalize();
78 #endif
79 
80 	for (n = 1;; n++) {
81 		fn = __DTOR_LIST__[n];
82 		if (fn == (crt_func)0 || fn == (crt_func)-1)
83 			break;
84 		fn();
85 	}
86 }
87 
88 asm (
89     ".pushsection .fini		\n"
90     "\t" INIT_CALL_SEQ(__do_global_dtors_aux) "\n"
91     ".popsection		\n"
92 );
93 #endif
94 
95 /*
96  * Handler for gcj. These provide a _Jv_RegisterClasses function and fill
97  * out the .jcr section. We just need to call this function with a pointer
98  * to the appropriate section.
99  */
100 extern void _Jv_RegisterClasses(void *) __weak_symbol;
101 static void register_classes(void) __used;
102 
103 static crt_func __JCR_LIST__[] __section(".jcr") __used = { };
104 
105 #ifndef CTORS_CONSTRUCTORS
106 __attribute__((constructor))
107 #endif
108 static void
109 register_classes(void)
110 {
111 
112 	if (_Jv_RegisterClasses != NULL && __JCR_LIST__[0] != 0)
113 		_Jv_RegisterClasses(__JCR_LIST__);
114 }
115 
116 /*
117  * We can't use constructors when they use the .ctors section as they may be
118  * placed before __CTOR_LIST__.
119  */
120 #ifdef CTORS_CONSTRUCTORS
121 asm (
122     ".pushsection .init		\n"
123     "\t" INIT_CALL_SEQ(register_classes) "\n"
124     ".popsection		\n"
125 );
126 #endif
127