xref: /openbsd-src/lib/csu/crtbegin.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: crtbegin.c,v 1.17 2013/12/28 18:38:42 kettenis Exp $	*/
2 /*	$NetBSD: crtbegin.c,v 1.1 1996/09/12 16:59:03 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1993 Paul Kranenburg
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Paul Kranenburg.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Run-time module for GNU C++ compiled shared libraries.
36  *
37  * The linker constructs the following arrays of pointers to global
38  * constructors and destructors. The first element contains the
39  * number of pointers in each.
40  * The tables are also null-terminated.
41  */
42 #include <stdlib.h>
43 
44 #include "md_init.h"
45 #include "os-note-elf.h"
46 #include "extern.h"
47 
48 struct dwarf2_eh_object {
49 	void *space[8];
50 };
51 
52 void __register_frame_info(const void *, struct dwarf2_eh_object *)
53     __attribute__((weak));
54 
55 void __register_frame_info(const void *begin, struct dwarf2_eh_object *ob)
56 {
57 }
58 
59 static const char __EH_FRAME_BEGIN__[]
60     __attribute__((section(".eh_frame"), aligned(4))) = { };
61 
62 /*
63  * java class registration hooks
64  */
65 
66 #if (__GNUC__ > 2)
67 static void *__JCR_LIST__[]
68     __attribute__((section(".jcr"), aligned(sizeof(void*)))) = { };
69 
70 extern void _Jv_RegisterClasses (void *)
71     __attribute__((weak));
72 #endif
73 
74 /*
75  * Include support for the __cxa_atexit/__cxa_finalize C++ abi for
76  * gcc > 2.x. __dso_handle is NULL in the main program and a unique
77  * value for each C++ shared library. For more info on this API, see:
78  *
79  *     http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor
80  */
81 
82 #if (__GNUC__ > 2)
83 void *__dso_handle = NULL;
84 __asm(".hidden  __dso_handle");
85 
86 long __guard_local __dso_hidden __attribute__((section(".openbsd.randomdata")));
87 
88 extern int __cxa_atexit(void (*)(void *), void *, void *) __attribute__((weak));
89 
90 int
91 atexit(void (*fn)(void))
92 {
93 	return (__cxa_atexit((void (*)(void *))fn, NULL, NULL));
94 }
95 #endif
96 
97 static const init_f __CTOR_LIST__[1]
98     __attribute__((section(".ctors"))) = { (void *)-1 };	/* XXX */
99 static const init_f __DTOR_LIST__[1]
100     __attribute__((section(".dtors"))) = { (void *)-1 };	/* XXX */
101 
102 static void	__dtors(void) __used;
103 static void	__ctors(void) __used;
104 
105 static void
106 __ctors()
107 {
108 	unsigned long i = (unsigned long) __CTOR_LIST__[0];
109 	const init_f *p;
110 
111 	if (i == -1)  {
112 		for (i = 1; __CTOR_LIST__[i] != NULL; i++)
113 			;
114 		i--;
115 	}
116 	p = __CTOR_LIST__ + i;
117 	while (i--)
118 		(**p--)();
119 }
120 
121 static void
122 __dtors()
123 {
124 	const init_f *p = __DTOR_LIST__ + 1;
125 
126 	while (*p)
127 		(**p++)();
128 }
129 
130 void __init(void);
131 void __fini(void);
132 static void __do_init(void) __used;
133 static void __do_fini(void) __used;
134 
135 MD_SECTION_PROLOGUE(".init", __init);
136 
137 MD_SECTION_PROLOGUE(".fini", __fini);
138 
139 MD_SECT_CALL_FUNC(".init", __do_init);
140 MD_SECT_CALL_FUNC(".fini", __do_fini);
141 
142 
143 void
144 __do_init()
145 {
146 	static int initialized = 0;
147 	static struct dwarf2_eh_object object;
148 
149 	/*
150 	 * Call global constructors.
151 	 * Arrange to call global destructors at exit.
152 	 */
153 	if (!initialized) {
154 		initialized = 1;
155 
156 		__register_frame_info(__EH_FRAME_BEGIN__, &object);
157 
158 #if (__GNUC__ > 2)
159 		if (__JCR_LIST__[0] && _Jv_RegisterClasses)
160 			_Jv_RegisterClasses(__JCR_LIST__);
161 #endif
162 
163 		(__ctors)();
164 
165 		atexit(__fini);
166 	}
167 }
168 
169 void
170 __do_fini()
171 {
172 	static int finalized = 0;
173 
174 	if (!finalized) {
175 		finalized = 1;
176 		/*
177 		 * Call global destructors.
178 		 */
179 		(__dtors)();
180 	}
181 }
182 
183