xref: /netbsd-src/lib/csu/common/crtbegin.c (revision 12faa818820980eab381a32ceadbaf7334cf49e1)
12ad451b6Smatt /*-
22ad451b6Smatt  * Copyright (c) 2013 The NetBSD Foundation, Inc.
32ad451b6Smatt  * All rights reserved.
42ad451b6Smatt  *
52ad451b6Smatt  * This code is derived from software contributed to The NetBSD Foundation
62ad451b6Smatt  * by Matt Thomas of 3am Software Foundry.
72ad451b6Smatt  *
82ad451b6Smatt  * Redistribution and use in source and binary forms, with or without
92ad451b6Smatt  * modification, are permitted provided that the following conditions
102ad451b6Smatt  * are met:
112ad451b6Smatt  * 1. Redistributions of source code must retain the above copyright
122ad451b6Smatt  *    notice, this list of conditions and the following disclaimer.
132ad451b6Smatt  * 2. Redistributions in binary form must reproduce the above copyright
142ad451b6Smatt  *    notice, this list of conditions and the following disclaimer in the
152ad451b6Smatt  *    documentation and/or other materials provided with the distribution.
162ad451b6Smatt  *
172ad451b6Smatt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
182ad451b6Smatt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
192ad451b6Smatt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
202ad451b6Smatt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
212ad451b6Smatt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
222ad451b6Smatt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
232ad451b6Smatt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
242ad451b6Smatt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
252ad451b6Smatt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
262ad451b6Smatt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
272ad451b6Smatt  * POSSIBILITY OF SUCH DAMAGE.
282ad451b6Smatt  */
292ad451b6Smatt #include <sys/cdefs.h>
30*12faa818Schristos __RCSID("$NetBSD: crtbegin.c,v 1.17 2018/12/28 18:17:11 christos Exp $");
31cd32ef25Sjoerg 
32cd32ef25Sjoerg #include "crtbegin.h"
332ad451b6Smatt 
342ad451b6Smatt typedef void (*fptr_t)(void);
352ad451b6Smatt 
362ad451b6Smatt __dso_hidden const fptr_t __JCR_LIST__[0] __section(".jcr");
37350cb9bcSmatt 
38350cb9bcSmatt __weakref_visible void Jv_RegisterClasses(const fptr_t *)
39350cb9bcSmatt 	__weak_reference(_Jv_RegisterClasses);
40350cb9bcSmatt 
4196e2e1e3Schristos #if !defined(HAVE_INITFINI_ARRAY)
42b16a9777Sjoerg extern __dso_hidden const fptr_t __CTOR_LIST__start __asm("__CTOR_LIST__");
43ad68895fSjoerg 
447118e2b3Smatt __dso_hidden const fptr_t __aligned(sizeof(void *)) __CTOR_LIST__[] __section(".ctors") = {
452ad451b6Smatt 	(fptr_t) -1,
462ad451b6Smatt };
472ad451b6Smatt __dso_hidden extern const fptr_t __CTOR_LIST_END__[];
4896e2e1e3Schristos #endif
492ad451b6Smatt 
502ad451b6Smatt #ifdef SHARED
512ad451b6Smatt __dso_hidden void *__dso_handle = &__dso_handle;
52350cb9bcSmatt 
53350cb9bcSmatt __weakref_visible void cxa_finalize(void *)
54350cb9bcSmatt 	__weak_reference(__cxa_finalize);
552ad451b6Smatt #else
562ad451b6Smatt __dso_hidden void *__dso_handle;
572ad451b6Smatt #endif
582ad451b6Smatt 
59353aedd8Sjoerg #if !defined(__ARM_EABI__) || defined(__ARM_DWARF_EH__)
60b373b9e5Sjoerg __dso_hidden const long __EH_FRAME_LIST__[0] __section(".eh_frame");
61350cb9bcSmatt 
622ad451b6Smatt __weakref_visible void register_frame_info(const void *, const void *)
632ad451b6Smatt 	__weak_reference(__register_frame_info);
642ad451b6Smatt __weakref_visible void deregister_frame_info(const void *)
652ad451b6Smatt 	__weak_reference(__deregister_frame_info);
662ad451b6Smatt 
6721ba1833Smatt static long dwarf_eh_object[8];
68350cb9bcSmatt #endif
692ad451b6Smatt 
702ad451b6Smatt static void __do_global_ctors_aux(void) __used;
712ad451b6Smatt 
72c1007461Smatt static void __section(".text.startup")
__do_global_ctors_aux(void)732ad451b6Smatt __do_global_ctors_aux(void)
742ad451b6Smatt {
75350cb9bcSmatt 	static unsigned char __initialized;
76350cb9bcSmatt 
772ad451b6Smatt 	if (__initialized)
782ad451b6Smatt 		return;
792ad451b6Smatt 
802ad451b6Smatt 	__initialized = 1;
812ad451b6Smatt 
82353aedd8Sjoerg #if !defined(__ARM_EABI__) || defined(__ARM_DWARF_EH__)
832ad451b6Smatt 	if (register_frame_info)
842ad451b6Smatt 		register_frame_info(__EH_FRAME_LIST__, &dwarf_eh_object);
85350cb9bcSmatt #endif
862ad451b6Smatt 
872ad451b6Smatt 	if (Jv_RegisterClasses && __JCR_LIST__[0] != 0)
882ad451b6Smatt 		Jv_RegisterClasses(__JCR_LIST__);
892ad451b6Smatt 
90350cb9bcSmatt #if !defined(HAVE_INITFINI_ARRAY)
916da05bfaSjoerg 	for (const fptr_t *p = __CTOR_LIST_END__; p > &__CTOR_LIST__start + 1; ) {
922ad451b6Smatt 		(*(*--p))();
932ad451b6Smatt 	}
94350cb9bcSmatt #endif
952ad451b6Smatt }
962ad451b6Smatt 
97353aedd8Sjoerg #if !defined(__ARM_EABI__) || defined(SHARED) || defined(__ARM_DWARF_EH__)
98350cb9bcSmatt #if !defined(HAVE_INITFINI_ARRAY)
99b16a9777Sjoerg extern __dso_hidden const fptr_t __DTOR_LIST__start __asm("__DTOR_LIST__");
100ad68895fSjoerg 
1010a181232Sbouyer __dso_hidden const fptr_t __aligned(sizeof(void *)) __DTOR_LIST__[] __section(".dtors") = {
102350cb9bcSmatt 	(fptr_t) -1,
103350cb9bcSmatt };
104350cb9bcSmatt __dso_hidden extern const fptr_t __DTOR_LIST_END__[];
105350cb9bcSmatt #endif
106350cb9bcSmatt 
107350cb9bcSmatt static void __do_global_dtors_aux(void) __used;
108350cb9bcSmatt 
109c1007461Smatt static void __section(".text.exit")
__do_global_dtors_aux(void)1102ad451b6Smatt __do_global_dtors_aux(void)
1112ad451b6Smatt {
112350cb9bcSmatt 	static unsigned char __finished;
113350cb9bcSmatt 
1142ad451b6Smatt 	if (__finished)
1152ad451b6Smatt 		return;
1162ad451b6Smatt 
1172ad451b6Smatt 	__finished = 1;
1182ad451b6Smatt 
119350cb9bcSmatt #ifdef SHARED
1202ad451b6Smatt 	if (cxa_finalize)
121350cb9bcSmatt 		(*cxa_finalize)(__dso_handle);
122350cb9bcSmatt #endif
1232ad451b6Smatt 
124350cb9bcSmatt #if !defined(HAVE_INITFINI_ARRAY)
1256da05bfaSjoerg 	for (const fptr_t *p = &__DTOR_LIST__start + 1; p < __DTOR_LIST_END__; ) {
1262ad451b6Smatt 		(*(*p++))();
1272ad451b6Smatt 	}
128350cb9bcSmatt #endif
1292ad451b6Smatt 
130353aedd8Sjoerg #if !defined(__ARM_EABI__) || defined(__ARM_DWARF_EH__)
1312ad451b6Smatt 	if (deregister_frame_info)
1322ad451b6Smatt 		deregister_frame_info(__EH_FRAME_LIST__);
133350cb9bcSmatt #endif
1342ad451b6Smatt }
135353aedd8Sjoerg #endif /* !__ARM_EABI__ || SHARED || __ARM_DWARF_EH__ */
136