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