1 /* $NetBSD: crt0-common.c,v 1.13 2013/01/31 22:24:25 matt Exp $ */
2
3 /*
4 * Copyright (c) 1998 Christos Zoulas
5 * Copyright (c) 1995 Christopher G. Demetriou
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed for the
19 * NetBSD Project. See http://www.NetBSD.org/ for
20 * information about NetBSD.
21 * 4. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
36 */
37
38 #include <sys/cdefs.h>
39 __RCSID("$NetBSD: crt0-common.c,v 1.13 2013/01/31 22:24:25 matt Exp $");
40
41 #include <sys/types.h>
42 #include <sys/exec.h>
43 #if !defined(__minix)
44 #include <sys/syscall.h>
45 #else
46 #include <string.h>
47 #endif /* !defined(__minix) */
48 #include <machine/profile.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51
52 #include "rtld.h"
53
54 extern int main(int, char **, char **);
55
56 #ifndef HAVE_INITFINI_ARRAY
57 extern void _init(void);
58 extern void _fini(void);
59 #endif
60 extern void _libc_init(void);
61
62 /*
63 * Arrange for _DYNAMIC to be weak and undefined (and therefore to show up
64 * as being at address zero, unless something else defines it). That way,
65 * if we happen to be compiling without -static but with without any
66 * shared libs present, things will still work.
67 */
68
69 __weakref_visible int rtld_DYNAMIC __weak_reference(_DYNAMIC);
70
71 #ifdef MCRT0
72 extern void monstartup(u_long, u_long);
73 extern void _mcleanup(void);
74 extern unsigned char __etext, __eprol;
75 #endif /* MCRT0 */
76
77 char **environ;
78 struct ps_strings *__ps_strings = 0;
79
80 static char empty_string[] = "";
81 char *__progname = empty_string;
82
83 __dead __dso_hidden void ___start(void (*)(void), const Obj_Entry *,
84 struct ps_strings *);
85
86 #if !defined(__minix)
87 #define write(fd, s, n) __syscall(SYS_write, (fd), (s), (n))
88 #else
89 #define write(fd, s, n) /* NO write() from here on minix */
90 #endif /* !defined(__minix) */
91
92 #define _FATAL(str) \
93 do { \
94 write(2, str, sizeof(str)-1); \
95 _exit(1); \
96 } while (0)
97
98 #ifdef HAVE_INITFINI_ARRAY
99 /*
100 * If we are using INIT_ARRAY/FINI_ARRAY and we are linked statically,
101 * we have to process these instead of relying on RTLD to do it for us.
102 *
103 * Since we don't need .init or .fini sections, just code them in C
104 * to make life easier.
105 */
106 __weakref_visible const fptr_t preinit_array_start[1]
107 __weak_reference(__preinit_array_start);
108 __weakref_visible const fptr_t preinit_array_end[1]
109 __weak_reference(__preinit_array_end);
110 __weakref_visible const fptr_t init_array_start[1]
111 __weak_reference(__init_array_start);
112 __weakref_visible const fptr_t init_array_end[1]
113 __weak_reference(__init_array_end);
114 __weakref_visible const fptr_t fini_array_start[1]
115 __weak_reference(__fini_array_start);
116 __weakref_visible const fptr_t fini_array_end[1]
117 __weak_reference(__fini_array_end);
118
119 static inline void
_preinit(void)120 _preinit(void)
121 {
122 for (const fptr_t *f = preinit_array_start; f < preinit_array_end; f++) {
123 (*f)();
124 }
125 }
126
127 static inline void
_init(void)128 _init(void)
129 {
130 for (const fptr_t *f = init_array_start; f < init_array_end; f++) {
131 (*f)();
132 }
133 }
134
135 static void
_fini(void)136 _fini(void)
137 {
138 for (const fptr_t *f = fini_array_start; f < fini_array_end; f++) {
139 (*f)();
140 }
141 }
142 #endif /* HAVE_INITFINI_ARRAY */
143
144 void
___start(void (* cleanup)(void),const Obj_Entry * obj,struct ps_strings * ps_strings)145 ___start(void (*cleanup)(void), /* from shared loader */
146 const Obj_Entry *obj, /* from shared loader */
147 struct ps_strings *ps_strings)
148 {
149
150 if (ps_strings == NULL)
151 _FATAL("ps_strings missing\n");
152 __ps_strings = ps_strings;
153
154 environ = ps_strings->ps_envstr;
155
156 if (ps_strings->ps_argvstr[0] != NULL) {
157 char *c;
158 __progname = ps_strings->ps_argvstr[0];
159 for (c = ps_strings->ps_argvstr[0]; *c; ++c) {
160 if (*c == '/')
161 __progname = c + 1;
162 }
163 } else {
164 __progname = empty_string;
165 }
166
167 if (&rtld_DYNAMIC != NULL) {
168 if (obj == NULL)
169 _FATAL("NULL Obj_Entry pointer in GOT\n");
170 if (obj->magic != RTLD_MAGIC)
171 _FATAL("Corrupt Obj_Entry pointer in GOT\n");
172 if (obj->version != RTLD_VERSION)
173 _FATAL("Dynamic linker version mismatch\n");
174 atexit(cleanup);
175 }
176
177 _libc_init();
178
179 #ifdef HAVE_INITFINI_ARRAY
180 _preinit();
181 #endif
182
183 #ifdef MCRT0
184 atexit(_mcleanup);
185 monstartup((u_long)&__eprol, (u_long)&__etext);
186 #endif
187
188 atexit(_fini);
189 _init();
190
191 exit(main(ps_strings->ps_nargvstr, ps_strings->ps_argvstr, environ));
192 }
193