1 |
|
|
/* $OpenBSD: crtbeginS.c,v 1.19 2017/02/19 21:39:32 guenther 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 |
|
|
/* |
49 |
|
|
* java class registration hooks |
50 |
|
|
*/ |
51 |
|
|
|
52 |
|
|
MD_DATA_SECTION_FLAGS_SYMBOL(".jcr", "aw", void *, __JCR_LIST__); |
53 |
|
|
|
54 |
|
|
extern void _Jv_RegisterClasses (void *) |
55 |
|
|
__attribute__((weak)); |
56 |
|
|
|
57 |
|
|
|
58 |
|
|
/* |
59 |
|
|
* Include support for the __cxa_atexit/__cxa_finalize C++ abi for |
60 |
|
|
* gcc > 2.x. __dso_handle is NULL in the main program and a unique |
61 |
|
|
* value for each C++ shared library. For more info on this API, see: |
62 |
|
|
* |
63 |
|
|
* http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor |
64 |
|
|
*/ |
65 |
|
|
|
66 |
|
|
void *__dso_handle = &__dso_handle; |
67 |
|
|
__asm(".hidden __dso_handle"); |
68 |
|
|
|
69 |
|
|
long __guard_local __dso_hidden __attribute__((section(".openbsd.randomdata"))); |
70 |
|
|
|
71 |
|
|
extern int __cxa_atexit(void (*)(void *), void *, void *) __attribute__((weak)); |
72 |
|
|
extern void __cxa_finalize(void *) __attribute__((weak)); |
73 |
|
|
|
74 |
|
|
int |
75 |
|
|
atexit(void (*fn)(void)) |
76 |
|
|
{ |
77 |
|
97672 |
return (__cxa_atexit((void (*)(void *))fn, NULL, &__dso_handle)); |
78 |
|
|
} |
79 |
|
|
asm(".hidden atexit"); |
80 |
|
|
|
81 |
|
|
/* |
82 |
|
|
* Ditto for pthread_atfork() |
83 |
|
|
*/ |
84 |
|
|
int _thread_atfork(void (*)(void), void (*)(void), void (*)(void), void *) |
85 |
|
|
__attribute__((weak)); |
86 |
|
|
|
87 |
|
|
int |
88 |
|
|
pthread_atfork(void (*prep)(void), void (*parent)(void), void (*child)(void)) |
89 |
|
|
{ |
90 |
|
8 |
return (_thread_atfork(prep, parent, child, &__dso_handle)); |
91 |
|
|
} |
92 |
|
|
/* hppa doesn't permit directives in first column, so space after newline */ |
93 |
|
|
asm(".hidden pthread_atfork\n .weak pthread_atfork"); |
94 |
|
|
|
95 |
|
|
MD_DATA_SECTION_SYMBOL_VALUE(".ctors", init_f, __CTOR_LIST__, -1); |
96 |
|
|
MD_DATA_SECTION_SYMBOL_VALUE(".dtors", init_f, __DTOR_LIST__, -1); |
97 |
|
|
|
98 |
|
|
|
99 |
|
|
static void |
100 |
|
|
__ctors(void) |
101 |
|
|
{ |
102 |
|
34560 |
unsigned long i = (unsigned long) __CTOR_LIST__[0]; |
103 |
|
|
const init_f *p; |
104 |
|
|
|
105 |
✓✗ |
17280 |
if (i == -1) { |
106 |
✓✓ |
6910750 |
for (i = 1; __CTOR_LIST__[i] != NULL; i++) |
107 |
|
|
; |
108 |
|
17280 |
i--; |
109 |
|
17280 |
} |
110 |
|
17280 |
p = __CTOR_LIST__ + i; |
111 |
✓✓ |
6910750 |
while (i--) |
112 |
|
3438095 |
(**p--)(); |
113 |
|
17280 |
} |
114 |
|
|
|
115 |
|
|
static void |
116 |
|
|
__dtors(void) |
117 |
|
|
{ |
118 |
|
|
const init_f *p = __DTOR_LIST__ + 1; |
119 |
|
|
|
120 |
✗✓ |
216 |
while (*p) |
121 |
|
|
(**p++)(); |
122 |
|
72 |
} |
123 |
|
|
|
124 |
|
|
void _init(void); |
125 |
|
|
void _fini(void); |
126 |
|
|
static void _do_init(void) __used; |
127 |
|
|
static void _do_fini(void) __used; |
128 |
|
|
|
129 |
|
|
MD_SECTION_PROLOGUE(".init", _init); |
130 |
|
|
|
131 |
|
|
MD_SECTION_PROLOGUE(".fini", _fini); |
132 |
|
|
|
133 |
|
|
MD_SECT_CALL_FUNC(".init", _do_init); |
134 |
|
|
MD_SECT_CALL_FUNC(".fini", _do_fini); |
135 |
|
|
|
136 |
|
|
|
137 |
|
|
void |
138 |
|
|
_do_init(void) |
139 |
|
|
{ |
140 |
|
|
static int initialized; |
141 |
|
|
|
142 |
|
|
/* |
143 |
|
|
* Call global constructors. |
144 |
|
|
* Arrange to call global destructors at exit. |
145 |
|
|
*/ |
146 |
✓✗ |
34560 |
if (!initialized) { |
147 |
|
17280 |
initialized = 1; |
148 |
|
|
|
149 |
✗✓ |
17280 |
if (__JCR_LIST__[0] && _Jv_RegisterClasses) |
150 |
|
|
_Jv_RegisterClasses(__JCR_LIST__); |
151 |
|
17280 |
__ctors(); |
152 |
|
17280 |
} |
153 |
|
17280 |
} |
154 |
|
|
|
155 |
|
|
void |
156 |
|
|
_do_fini(void) |
157 |
|
|
{ |
158 |
|
|
static int finalized; |
159 |
|
|
|
160 |
✓✗ |
19278 |
if (!finalized) { |
161 |
|
9639 |
finalized = 1; |
162 |
|
|
|
163 |
✓✗ |
9639 |
if (__cxa_finalize != NULL) |
164 |
|
9639 |
__cxa_finalize(__dso_handle); |
165 |
|
|
|
166 |
|
|
/* |
167 |
|
|
* since the _init() function sets up the destructors to |
168 |
|
|
* be called by atexit, do not call the destructors here. |
169 |
|
|
*/ |
170 |
|
72 |
__dtors(); |
171 |
|
72 |
} |
172 |
|
72 |
} |