xref: /freebsd/lib/csu/common/crtbegin.c (revision 6ee34bca48a9e0867d46b24a78855e225d46ddda)
131d62a73SAndrew Turner /*-
231d62a73SAndrew Turner  * SPDX-License-Identifier: BSD-1-Clause
331d62a73SAndrew Turner  *
431d62a73SAndrew Turner  * Copyright 2018 Andrew Turner
531d62a73SAndrew Turner  *
631d62a73SAndrew Turner  * Redistribution and use in source and binary forms, with or without
731d62a73SAndrew Turner  * modification, are permitted provided that the following conditions
831d62a73SAndrew Turner  * are met:
931d62a73SAndrew Turner  * 1. Redistributions of source code must retain the above copyright
1031d62a73SAndrew Turner  *    notice, this list of conditions and the following disclaimer.
1131d62a73SAndrew Turner  *
1231d62a73SAndrew Turner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1331d62a73SAndrew Turner  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1431d62a73SAndrew Turner  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1531d62a73SAndrew Turner  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1631d62a73SAndrew Turner  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1731d62a73SAndrew Turner  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1831d62a73SAndrew Turner  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1931d62a73SAndrew Turner  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2031d62a73SAndrew Turner  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2131d62a73SAndrew Turner  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2231d62a73SAndrew Turner  */
2331d62a73SAndrew Turner 
2431d62a73SAndrew Turner #include <sys/param.h>
2531d62a73SAndrew Turner 
2631d62a73SAndrew Turner #include "crt.h"
2731d62a73SAndrew Turner 
2831d62a73SAndrew Turner typedef void (*crt_func)(void);
2931d62a73SAndrew Turner 
30fd8767bbSAndrew Turner extern void *__dso_handle __hidden;
31fd8767bbSAndrew Turner 
32b7305f91SAndrew Turner #ifndef SHARED
33fd8767bbSAndrew Turner void *__dso_handle = 0;
34b7305f91SAndrew Turner #else
35b7305f91SAndrew Turner void *__dso_handle = &__dso_handle;
36b7305f91SAndrew Turner void __cxa_finalize(void *) __weak_symbol;
37b7305f91SAndrew Turner 
38b7305f91SAndrew Turner /*
39b7305f91SAndrew Turner  * Call __cxa_finalize with the dso handle in shared objects.
40b7305f91SAndrew Turner  * When we have ctors/dtors call from the dtor handler before calling
41b7305f91SAndrew Turner  * any dtors, otherwise use a destructor.
42b7305f91SAndrew Turner  */
43b7305f91SAndrew Turner #ifndef HAVE_CTORS
44b7305f91SAndrew Turner __attribute__((destructor))
45b7305f91SAndrew Turner #endif
46b7305f91SAndrew Turner static void
47b7305f91SAndrew Turner run_cxa_finalize(void)
48b7305f91SAndrew Turner {
49b7305f91SAndrew Turner 
50b7305f91SAndrew Turner 	if (__cxa_finalize != NULL)
51b7305f91SAndrew Turner 		__cxa_finalize(__dso_handle);
52b7305f91SAndrew Turner }
53fd8767bbSAndrew Turner #endif
54fd8767bbSAndrew Turner 
5531d62a73SAndrew Turner /*
5631d62a73SAndrew Turner  * On some architectures and toolchains we may need to call the .dtors.
5731d62a73SAndrew Turner  * These are called in the order they are in the ELF file.
5831d62a73SAndrew Turner  */
5931d62a73SAndrew Turner #ifdef HAVE_CTORS
6031d62a73SAndrew Turner static void __do_global_dtors_aux(void) __used;
6131d62a73SAndrew Turner 
6257706376SEd Maste static crt_func __CTOR_LIST__[] __section(".ctors") __used = {
6331d62a73SAndrew Turner 	(crt_func)-1
6431d62a73SAndrew Turner };
6557706376SEd Maste static crt_func __DTOR_LIST__[] __section(".dtors") __used = {
6631d62a73SAndrew Turner 	(crt_func)-1
6731d62a73SAndrew Turner };
6831d62a73SAndrew Turner 
69*6ee34bcaSKonstantin Belousov extern const char startof_dtors[] __asm(".startof..dtors")
70*6ee34bcaSKonstantin Belousov     __weak_symbol __hidden;
71*6ee34bcaSKonstantin Belousov extern const char sizeof_dtors[] __asm(".sizeof..dtors")
72*6ee34bcaSKonstantin Belousov     __weak_symbol __hidden;
73*6ee34bcaSKonstantin Belousov 
7431d62a73SAndrew Turner static void
7531d62a73SAndrew Turner __do_global_dtors_aux(void)
7631d62a73SAndrew Turner {
7731d62a73SAndrew Turner 	crt_func fn;
78*6ee34bcaSKonstantin Belousov 	uintptr_t dtors_end;
7931d62a73SAndrew Turner 	int n;
8031d62a73SAndrew Turner 
81b7305f91SAndrew Turner #ifdef SHARED
82b7305f91SAndrew Turner 	run_cxa_finalize();
83b7305f91SAndrew Turner #endif
84b7305f91SAndrew Turner 
85*6ee34bcaSKonstantin Belousov 	dtors_end = (uintptr_t)&startof_dtors + (uintptr_t)&sizeof_dtors;
8631d62a73SAndrew Turner 	for (n = 1;; n++) {
8731d62a73SAndrew Turner 		fn = __DTOR_LIST__[n];
88*6ee34bcaSKonstantin Belousov 		if (fn == (crt_func)0 || fn == (crt_func)-1 || (dtors_end > 0 &&
89*6ee34bcaSKonstantin Belousov 		    (uintptr_t)&__DTOR_LIST__[n] >= dtors_end))
9031d62a73SAndrew Turner 			break;
9131d62a73SAndrew Turner 		fn();
9231d62a73SAndrew Turner 	}
9331d62a73SAndrew Turner }
9431d62a73SAndrew Turner 
9531d62a73SAndrew Turner asm (
9631d62a73SAndrew Turner     ".pushsection .fini		\n"
9731d62a73SAndrew Turner     "\t" INIT_CALL_SEQ(__do_global_dtors_aux) "\n"
9831d62a73SAndrew Turner     ".popsection		\n"
9931d62a73SAndrew Turner );
10031d62a73SAndrew Turner #endif
10131d62a73SAndrew Turner 
10231d62a73SAndrew Turner /*
10331d62a73SAndrew Turner  * Handler for gcj. These provide a _Jv_RegisterClasses function and fill
10431d62a73SAndrew Turner  * out the .jcr section. We just need to call this function with a pointer
10531d62a73SAndrew Turner  * to the appropriate section.
10631d62a73SAndrew Turner  */
10731d62a73SAndrew Turner extern void _Jv_RegisterClasses(void *) __weak_symbol;
10831d62a73SAndrew Turner static void register_classes(void) __used;
10931d62a73SAndrew Turner 
110fb7128c2SAndrew Turner static crt_func __JCR_LIST__[] __section(".jcr") __used = { };
11131d62a73SAndrew Turner 
11231d62a73SAndrew Turner #ifndef CTORS_CONSTRUCTORS
11331d62a73SAndrew Turner __attribute__((constructor))
11431d62a73SAndrew Turner #endif
11531d62a73SAndrew Turner static void
11631d62a73SAndrew Turner register_classes(void)
11731d62a73SAndrew Turner {
11831d62a73SAndrew Turner 
11931d62a73SAndrew Turner 	if (_Jv_RegisterClasses != NULL && __JCR_LIST__[0] != 0)
12031d62a73SAndrew Turner 		_Jv_RegisterClasses(__JCR_LIST__);
12131d62a73SAndrew Turner }
12231d62a73SAndrew Turner 
12331d62a73SAndrew Turner /*
12431d62a73SAndrew Turner  * We can't use constructors when they use the .ctors section as they may be
12531d62a73SAndrew Turner  * placed before __CTOR_LIST__.
12631d62a73SAndrew Turner  */
12731d62a73SAndrew Turner #ifdef CTORS_CONSTRUCTORS
12831d62a73SAndrew Turner asm (
12931d62a73SAndrew Turner     ".pushsection .init		\n"
13031d62a73SAndrew Turner     "\t" INIT_CALL_SEQ(register_classes) "\n"
13131d62a73SAndrew Turner     ".popsection		\n"
13231d62a73SAndrew Turner );
13331d62a73SAndrew Turner #endif
134