crtbegin.c (2040953a71ac6237907a1c106049111f8d84d5bc) | crtbegin.c (31d62a73c2e6ac0ff413a7a17700ffc7dce254ef) |
---|---|
1/*- | 1/*- |
2 * Copyright 1996, 1997, 1998, 2000 John D. Polstra. 3 * All rights reserved. | 2 * SPDX-License-Identifier: BSD-1-Clause |
4 * | 3 * |
4 * Copyright 2018 Andrew Turner 5 * |
|
5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. | 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. | |
13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 11 * 12 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 13 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 14 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 15 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 16 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 17 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 18 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 19 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 20 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 21 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 * 25 * $FreeBSD$ | |
26 */ 27 | 22 */ 23 |
24#include <sys/cdefs.h> 25__FBSDID("$FreeBSD$"); 26 |
|
28#include <sys/param.h> 29 | 27#include <sys/param.h> 28 |
30typedef void (*fptr)(void); | 29#include "crt.h" |
31 | 30 |
32static fptr ctor_list[1] __attribute__((section(".ctors"))) = { (fptr) -1 }; 33static fptr dtor_list[1] __attribute__((section(".dtors"))) = { (fptr) -1 }; | 31typedef void (*crt_func)(void); |
34 | 32 |
35static void 36do_ctors(void) 37{ 38 fptr *fpp; | 33/* 34 * On some architectures and toolchains we may need to call the .dtors. 35 * These are called in the order they are in the ELF file. 36 */ 37#ifdef HAVE_CTORS 38static void __do_global_dtors_aux(void) __used; |
39 | 39 |
40 for(fpp = ctor_list + 1; *fpp != 0; ++fpp) 41 ; 42 while(--fpp > ctor_list) 43 (**fpp)(); 44} | 40crt_func __CTOR_LIST__[] __section(".ctors") __hidden = { 41 (crt_func)-1 42}; 43crt_func __DTOR_LIST__[] __section(".dtors") __hidden = { 44 (crt_func)-1 45}; |
45 46static void | 46 47static void |
47do_dtors(void) | 48__do_global_dtors_aux(void) |
48{ | 49{ |
49 fptr *fpp; | 50 crt_func fn; 51 int n; |
50 | 52 |
51 for(fpp = dtor_list + 1; *fpp != 0; ++fpp) 52 (**fpp)(); | 53 for (n = 1;; n++) { 54 fn = __DTOR_LIST__[n]; 55 if (fn == (crt_func)0 || fn == (crt_func)-1) 56 break; 57 fn(); 58 } |
53} 54 | 59} 60 |
61asm ( 62 ".pushsection .fini \n" 63 "\t" INIT_CALL_SEQ(__do_global_dtors_aux) "\n" 64 ".popsection \n" 65); 66#endif 67 |
|
55/* | 68/* |
56 * With very large programs on some architectures (e.g., the Alpha), 57 * it is possible to get relocation overflows on the limited 58 * displacements of call/bsr instructions. It is particularly likely 59 * for the calls from _init() and _fini(), because they are in 60 * separate sections. Avoid the problem by forcing indirect calls. | 69 * Handler for gcj. These provide a _Jv_RegisterClasses function and fill 70 * out the .jcr section. We just need to call this function with a pointer 71 * to the appropriate section. |
61 */ | 72 */ |
62static void (*p_do_ctors)(void) = do_ctors; 63static void (*p_do_dtors)(void) = do_dtors; | 73extern void _Jv_RegisterClasses(void *) __weak_symbol; 74static void register_classes(void) __used; |
64 | 75 |
65extern void _init(void) __attribute__((section(".init"))); | 76crt_func __JCR_LIST__[] __section(".jcr") __used __hidden = { }; |
66 | 77 |
67void 68_init(void) | 78#ifndef CTORS_CONSTRUCTORS 79__attribute__((constructor)) 80#endif 81static void 82register_classes(void) |
69{ | 83{ |
70 (*p_do_ctors)(); 71} | |
72 | 84 |
73extern void _fini(void) __attribute__((section(".fini"))); 74 75void 76_fini(void) 77{ 78 (*p_do_dtors)(); | 85 if (_Jv_RegisterClasses != NULL && __JCR_LIST__[0] != 0) 86 _Jv_RegisterClasses(__JCR_LIST__); |
79} 80 | 87} 88 |
81#include "crtbrand.c" | 89/* 90 * We can't use constructors when they use the .ctors section as they may be 91 * placed before __CTOR_LIST__. 92 */ 93#ifdef CTORS_CONSTRUCTORS 94asm ( 95 ".pushsection .init \n" 96 "\t" INIT_CALL_SEQ(register_classes) "\n" 97 ".popsection \n" 98); 99#endif |