xref: /openbsd-src/lib/csu/crt0.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: crt0.c,v 1.7 2016/05/07 19:30:53 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 
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 char	***_csu_finish(char **_argv, char **_envp, void (*_cleanup)(void));
56 
57 #ifdef MCRT0
58 #include <sys/gmon.h>
59 extern unsigned char _etext, _eprol;
60 #endif /* MCRT0 */
61 
62 #ifdef RCRT0
63 #ifdef MD_RCRT0_START
64 MD_RCRT0_START;
65 #endif
66 #else
67 #ifdef MD_CRT0_START
68 MD_CRT0_START;
69 #endif
70 #endif
71 
72 void
73 MD_START(MD_START_ARGS)
74 {
75 	char ***environp;
76 #ifdef MD_START_SETUP
77 	MD_START_SETUP
78 #endif
79 
80 #ifndef MD_NO_CLEANUP
81 	environp = _csu_finish(argv, envp, cleanup);
82 #else
83 	environp = _csu_finish(argv, envp, NULL);
84 #endif
85 
86 #ifdef MCRT0
87 	atexit(_mcleanup);
88 	_monstartup((u_long)&_eprol, (u_long)&_etext);
89 #endif
90 
91 	__init();
92 
93 	exit(main(argc, argv, *environp));
94 }
95 
96 #ifdef MCRT0
97 MD_EPROL_LABEL;
98 #endif
99