1 /* $OpenBSD: crt0.c,v 1.4 2014/12/27 16:04:22 kettenis 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 42 /* some defaults */ 43 #ifndef MD_START_ARGS 44 #define MD_START_ARGS \ 45 int argc, char **argv, char **envp, void (*cleanup)(void) 46 #endif 47 #ifndef MD_START 48 #define MD_START ___start 49 static void ___start(MD_START_ARGS) __used; 50 #endif 51 #ifndef MD_EPROL_LABEL 52 #define MD_EPROL_LABEL __asm(" .text\n_eprol:") 53 #endif 54 55 void __init_tcb(char **_envp); 56 #pragma weak __init_tcb 57 58 static char *_strrchr(char *, char); 59 60 char **environ; 61 char *__progname = ""; 62 char __progname_storage[NAME_MAX+1]; 63 64 #ifdef MCRT0 65 extern void monstartup(u_long, u_long); 66 extern void _mcleanup(void); 67 extern unsigned char _etext, _eprol; 68 #endif /* MCRT0 */ 69 70 #ifdef RCRT0 71 #ifdef MD_RCRT0_START 72 MD_RCRT0_START; 73 #endif 74 #else 75 #ifdef MD_CRT0_START 76 MD_CRT0_START; 77 #endif 78 #endif 79 80 void 81 MD_START(MD_START_ARGS) 82 { 83 char *namep, *s; 84 #ifdef MD_START_SETUP 85 MD_START_SETUP 86 #endif 87 88 environ = envp; 89 90 if ((namep = argv[0]) != NULL) { /* NULL ptr if argc = 0 */ 91 if ((__progname = _strrchr(namep, '/')) == NULL) 92 __progname = namep; 93 else 94 __progname++; 95 for (s = __progname_storage; *__progname && 96 s < &__progname_storage[sizeof __progname_storage - 1]; ) 97 *s++ = *__progname++; 98 *s = '\0'; 99 __progname = __progname_storage; 100 } 101 102 #ifndef MD_NO_CLEANUP 103 if (cleanup != NULL) 104 atexit(cleanup); 105 else 106 #endif 107 if (__init_tcb != NULL) 108 __init_tcb(envp); 109 110 #ifdef MCRT0 111 atexit(_mcleanup); 112 monstartup((u_long)&_eprol, (u_long)&_etext); 113 #endif 114 115 __init(); 116 117 exit(main(argc, argv, envp)); 118 } 119 120 static char * 121 _strrchr(char *p, char ch) 122 { 123 char *save; 124 125 for (save = NULL;; ++p) { 126 if (*p == ch) 127 save = p; 128 if (*p == '\0') 129 return (save); 130 } 131 } 132 133 #ifdef MCRT0 134 MD_EPROL_LABEL; 135 #endif 136