1 /* $OpenBSD: crt0.c,v 1.18 2023/07/27 18:17:14 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 <sys/syscall.h>
35 #include <stdlib.h>
36 #include <limits.h>
37
38 #include "md_init.h"
39 #ifdef RCRT0
40 # include BOOT_H
41 #endif
42 #include "extern.h"
43
44 #define STR(x) __STRING(x) /* shorter macro name for MD_RCRT0_START */
45
46 /* some defaults */
47 #ifndef MD_START_ARGS
48 #define MD_START_ARGS \
49 int argc, char **argv, char **envp, void (*cleanup)(void)
50 #endif
51 static void ___start(MD_START_ARGS) __used;
52 #ifndef MD_EPROL_LABEL
53 #define MD_EPROL_LABEL __asm(" .text\n_eprol:")
54 #endif
55 #ifndef RCRT0_RELRO
56 #define RCRT0_RELRO() do {} while (0)
57 #endif
58
59 char ***_csu_finish(char **_argv, char **_envp, void (*_cleanup)(void));
60
61 #ifdef MCRT0
62 #include <sys/gmon.h>
63 extern __dso_hidden unsigned char _etext, _eprol;
64 #endif /* MCRT0 */
65
66 #ifdef RCRT0
67 #ifdef MD_RCRT0_START
68 MD_RCRT0_START;
69 #endif
70 #else
71 #ifdef MD_CRT0_START
72 MD_CRT0_START;
73 #endif
74 #endif
75
76 extern __dso_hidden initarray_f __preinit_array_start[],
77 __preinit_array_end[], __init_array_start[], __init_array_end[];
78
79 extern char __csu_do_fini_array __dso_hidden;
80
81 static void
___start(MD_START_ARGS)82 ___start(MD_START_ARGS)
83 {
84 size_t size, i;
85 char ***environp;
86 #ifdef MD_START_SETUP
87 MD_START_SETUP
88 #endif
89
90 environp = _csu_finish(argv, envp, cleanup);
91
92 #ifndef RCRT0
93 if (cleanup == NULL) {
94 #endif
95 size = __preinit_array_end - __preinit_array_start;
96 for (i = 0; i < size; i++)
97 __preinit_array_start[i](argc, argv, envp, NULL);
98 RCRT0_RELRO();
99 size = __init_array_end - __init_array_start;
100 for (i = 0; i < size; i++)
101 __init_array_start[i](argc, argv, envp, NULL);
102 __csu_do_fini_array = 1;
103 #ifndef RCRT0
104 }
105 #endif
106
107 #ifdef MCRT0
108 atexit(_mcleanup);
109 _monstartup((u_long)&_eprol, (u_long)&_etext);
110 #endif
111
112 __init();
113
114 exit(main(argc, argv, *environp));
115 }
116
117 #ifdef MCRT0
118 MD_EPROL_LABEL;
119 #endif
120