1 /* $OpenBSD: crt0.c,v 1.14 2019/05/10 13:29:21 guenther Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Christopher G. Demetriou 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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 the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Christopher G. Demetriou 18 * for the NetBSD Project. 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 #include <stdlib.h> 35 #include <limits.h> 36 37 #include "md_init.h" 38 #ifdef MD_RCRT0_START 39 #include "boot.h" 40 #endif 41 #include "extern.h" 42 43 /* some defaults */ 44 #ifndef MD_START_ARGS 45 #define MD_START_ARGS \ 46 int argc, char **argv, char **envp, void (*cleanup)(void) 47 #endif 48 static void ___start(MD_START_ARGS) __used; 49 #ifndef MD_EPROL_LABEL 50 #define MD_EPROL_LABEL __asm(" .text\n_eprol:") 51 #endif 52 #ifndef RCRT0_RELRO 53 #define RCRT0_RELRO() do {} while (0) 54 #endif 55 56 char ***_csu_finish(char **_argv, char **_envp, void (*_cleanup)(void)); 57 58 #ifdef MCRT0 59 #include <sys/gmon.h> 60 extern unsigned char _etext, _eprol; 61 #endif /* MCRT0 */ 62 63 #ifdef RCRT0 64 #ifdef MD_RCRT0_START 65 MD_RCRT0_START; 66 #endif 67 #else 68 #ifdef MD_CRT0_START 69 MD_CRT0_START; 70 #endif 71 #endif 72 73 extern __dso_hidden initarray_f __preinit_array_start[], 74 __preinit_array_end[], __init_array_start[], __init_array_end[]; 75 76 extern char __csu_do_fini_array __dso_hidden; 77 78 static void 79 ___start(MD_START_ARGS) 80 { 81 size_t size, i; 82 char ***environp; 83 #ifdef MD_START_SETUP 84 MD_START_SETUP 85 #endif 86 87 environp = _csu_finish(argv, envp, cleanup); 88 89 #ifndef RCRT0 90 if (cleanup == NULL) { 91 #endif 92 size = __preinit_array_end - __preinit_array_start; 93 for (i = 0; i < size; i++) 94 __preinit_array_start[i](argc, argv, envp, NULL); 95 RCRT0_RELRO(); 96 size = __init_array_end - __init_array_start; 97 for (i = 0; i < size; i++) 98 __init_array_start[i](argc, argv, envp, NULL); 99 __csu_do_fini_array = 1; 100 #ifndef RCRT0 101 } 102 #endif 103 104 #ifdef MCRT0 105 atexit(_mcleanup); 106 _monstartup((u_long)&_eprol, (u_long)&_etext); 107 #endif 108 109 __init(); 110 111 exit(main(argc, argv, *environp)); 112 } 113 114 #ifdef MCRT0 115 MD_EPROL_LABEL; 116 #endif 117