1 /* $NetBSD: crtbegin.S,v 1.3 2013/09/12 15:36:14 joerg Exp $ */
3 * Copyright (c) 2012 Valeriy E. Ushakov
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
27 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <machine/asm.h>
33 RCSID("$NetBSD: crtbegin.S,v 1.3 2013/09/12 15:36:14 joerg Exp $")
35 .section .ctors, "aw", @progbits
40 .section .dtors, "aw", @progbits
45 .section .eh_frame, "a", @progbits
49 .section .jcr, "aw", @progbits
53 .section .data.rel, "aw", @progbits
57 .type __dso_handle, @object
76 .weak __deregister_frame_info
77 .weak __register_frame_info
78 .weak _Jv_RegisterClasses
81 * A bit of CPP syntactic sugar for accessing variables.
83 * For PIC we are obliged to use @(r0, r12) since r12 has the GOT
84 * address and only r0 can be used in @(r0, Rm) addressing mode, so we
85 * always load variable address to r0.
88 #define VAR_DATUM(var) var@GOTOFF
89 #define FUNC_DATUM(f) f@GOT
90 #define R0VAR (r0, r12)
92 #define VAR_DATUM(var) var
93 #define FUNC_DATUM(f) f
98 __do_global_ctors_aux:
111 !! if (__initialized) return;
112 mov.l .Lc___initialized, r0
117 !! __initialized = 1;
122 !! if (__register_frame_info)
123 !! __register_frame_info(&__EH_FRAME_LIST__[0], &__dwarf_eh_object)
125 mov.l .Lc___register_frame_info_GOT, r0
128 bt .Lc___register_frame_info_done
129 mov.l .Lc___register_frame_info, r0
130 mov.l .Lc___EH_FRAME_LIST__, r4
131 mov.l .Lc___dwarf_eh_object, r5
133 .Lc___register_frame_info_call:
137 mov.l .Lc___register_frame_info, r0
139 bt .Lc___register_frame_info_done
140 mov.l .Lc___EH_FRAME_LIST__, r4
141 mov.l .Lc___dwarf_eh_object, r5
145 .Lc___register_frame_info_done:
147 !! if (_Jv_RegisterClasses && __JCR_LIST__[0])
148 !! _Jv_RegisterClasses(&__JCR_LIST__[0]);
150 mov.l .Lc__Jv_RegisterClasses_GOT, r0
153 bt .Lc__Jv_RegisterClasses_done
155 mov.l .Lc___JCR_LIST__, r0
158 bt .Lc__Jv_RegisterClasses_done
160 mov.l .Lc__Jv_RegisterClasses, r2
162 .Lc__Jv_RegisterClasses_call:
167 mov.l .Lc__Jv_RegisterClasses, r2
169 bt .Lc__Jv_RegisterClasses_done
171 mov.l .Lc___JCR_LIST__, r0
174 bt .Lc__Jv_RegisterClasses_done
177 .Lc__Jv_RegisterClasses_call:
181 .Lc__Jv_RegisterClasses_done:
184 !! call all constructors on __CTOR_LIST__ in reverse order
185 mov.l .Lc___CTOR_LIST_END__, r8
191 not r9, r0 ! sentinel at __CTOR_LIST__[0] is -1
194 bt.s .Lc_ctor_list_done
198 bra .Lc_ctor_list_loop
217 .long VAR_DATUM(__initialized)
219 .Lc___register_frame_info_GOT:
220 .long __register_frame_info@GOT
222 .Lc___register_frame_info:
223 CALL_DATUM(__register_frame_info, .Lc___register_frame_info_call)
224 .Lc___EH_FRAME_LIST__:
225 .long VAR_DATUM(__EH_FRAME_LIST__)
226 .Lc___dwarf_eh_object:
227 .long VAR_DATUM(__dwarf_eh_object)
229 .Lc__Jv_RegisterClasses_GOT:
230 .long _Jv_RegisterClasses@GOT
232 .Lc__Jv_RegisterClasses:
233 CALL_DATUM(_Jv_RegisterClasses, .Lc__Jv_RegisterClasses_call)
235 .long VAR_DATUM(__JCR_LIST__)
236 .Lc___CTOR_LIST_END__:
237 .long VAR_DATUM(__CTOR_LIST_END__)
240 __do_global_dtors_aux:
253 !! if (__finished) return;
254 mov.l .Ld___finished, r0
263 #ifdef SHARED /* implies PIC */
264 !! if (__cxa_finalize)
265 !! __cxa_finalize(&__dso_handle);
266 mov.l .Ld___cxa_finalize_GOT, r0
269 bt .Ld___cxa_finalize_done
270 mov.l .Ld___cxa_finalize, r0
271 mov.l .Ld___dso_handle, r4
272 .Ld___cxa_finalize_call:
275 .Ld___cxa_finalize_done:
278 !! call all destructors on __DTOR_LIST__
279 mov.l .Ld___DTOR_LIST__, r8
283 add #4, r8 ! skip first entry that we know to be -1
287 bt .Ld_dtor_list_done
290 bra .Ld_dtor_list_loop
294 !! if (__deregister_frame_info)
295 !! __deregister_frame_info(&__EH_FRAME_LIST__[0]);
297 mov.l .Ld___deregister_frame_info_GOT, r0
300 bt .Ld___deregister_frame_info_done
301 mov.l .Ld___deregister_frame_info, r0
302 mov.l .Ld___EH_FRAME_LIST__, r4
303 .Ld___deregister_frame_info_call:
307 mov.l .Ld___deregister_frame_info, r0
309 bt .Ld___deregister_frame_info_done
310 mov.l .Ld___EH_FRAME_LIST__, r4
314 .Ld___deregister_frame_info_done:
331 .long VAR_DATUM(__finished)
332 #ifdef SHARED /* implies PIC */
333 .Ld___cxa_finalize_GOT:
334 .long __cxa_finalize@GOT
336 CALL_DATUM(__cxa_finalize, .Ld___cxa_finalize_call)
338 .long VAR_DATUM(__dso_handle)
341 .long VAR_DATUM(__DTOR_LIST__)
343 .Ld___deregister_frame_info_GOT:
344 .long __deregister_frame_info@GOT
346 .Ld___deregister_frame_info:
347 CALL_DATUM(__deregister_frame_info, .Ld___deregister_frame_info_call)
348 .Ld___EH_FRAME_LIST__:
349 .long VAR_DATUM(__EH_FRAME_LIST__)
353 #define _CALL_INIT_FINI_FUNCTION(func) \
356 0: braf r1; /* NB: branch, not call ... */ \
357 lds r0, pr; /* skip the following .long when returning */ \
359 1: .long func - (0b+4); \
362 .section .init, "ax", @progbits
363 _CALL_INIT_FINI_FUNCTION(__do_global_ctors_aux)
365 .section .fini, "ax", @progbits
366 _CALL_INIT_FINI_FUNCTION(__do_global_dtors_aux)