opendir change: refinement
[minix.git] / lib / csu / common / crt0-common.c
blob49c0856847e3747c2b49a6b5ef1ce70597cab513
1 /* $NetBSD: crt0-common.c,v 1.9 2012/08/13 02:15:35 matt Exp $ */
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.
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.
35 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
38 #include <sys/cdefs.h>
39 __RCSID("$NetBSD: crt0-common.c,v 1.9 2012/08/13 02:15:35 matt Exp $");
41 #include <sys/types.h>
42 #include <sys/exec.h>
43 #ifndef __minix
44 #include <sys/syscall.h>
45 #endif
46 #include <machine/profile.h>
47 #include <stdlib.h>
48 #include <unistd.h>
50 #include "rtld.h"
52 extern int main(int, char **, char **);
54 #ifndef HAVE_INITFINI_ARRAY
55 extern void _init(void);
56 extern void _fini(void);
57 #endif
58 extern void _libc_init(void);
61 * Arrange for _DYNAMIC to be weak and undefined (and therefore to show up
62 * as being at address zero, unless something else defines it). That way,
63 * if we happen to be compiling without -static but with without any
64 * shared libs present, things will still work.
67 __weakref_visible int rtld_DYNAMIC __weak_reference(_DYNAMIC);
69 #ifdef MCRT0
70 extern void monstartup(u_long, u_long);
71 extern void _mcleanup(void);
72 extern unsigned char __etext, __eprol;
73 #endif /* MCRT0 */
75 char **environ;
76 struct ps_strings *__ps_strings = 0;
78 static char empty_string[] = "";
79 char *__progname = empty_string;
81 #ifndef __minix
82 __dead __dso_hidden void ___start(void (*)(void), const Obj_Entry *,
83 struct ps_strings *);
85 #define write(fd, s, n) __syscall(SYS_write, (fd), (s), (n))
86 #else
87 __dead __dso_hidden void ___start(int, char **, char **, void (*)(void),
88 const Obj_Entry *, struct ps_strings *);
90 #define write(fd, s, n) /* NO write() from here on minix */
91 #endif
93 #define _FATAL(str) \
94 do { \
95 write(2, str, sizeof(str)-1); \
96 _exit(1); \
97 } while (0)
99 #ifdef HAVE_INITFINI_ARRAY
101 * If we are using INIT_ARRAY/FINI_ARRAY and we are linked statically,
102 * we have to process these instead of relying on RTLD to do it for us.
104 * Since we don't need .init or .fini sections, just code them in C
105 * to make life easier.
107 static const fptr_t init_array_start[] __weak_reference(__init_array_start);
108 static const fptr_t init_array_end[] __weak_reference(__init_array_end);
109 static const fptr_t fini_array_start[] __weak_reference(__fini_array_start);
110 static const fptr_t fini_array_end[] __weak_reference(__fini_array_end);
112 static inline void
113 _init(void)
115 for (const fptr_t *f = init_array_start; f < init_array_end; f++) {
116 (*f)();
120 static void
121 _fini(void)
123 for (const fptr_t *f = fini_array_start; f < fini_array_end; f++) {
124 (*f)();
127 #endif /* HAVE_INITFINI_ARRAY */
129 void
130 #ifdef __minix
131 ___start(int argc, char **argv, char **envp,
132 void (*cleanup)(void), /* from shared loader */
133 #else
134 ___start(void (*cleanup)(void), /* from shared loader */
135 #endif /* __minix */
136 const Obj_Entry *obj, /* from shared loader */
137 struct ps_strings *ps_strings)
139 #ifdef __minix
140 /* LSC: We have not yet updated the way we pass arguments to
141 the userspace, so here some code to adapt this to the new
142 ways. */
143 struct ps_strings minix_ps_strings;
145 if (ps_strings == NULL) {
146 minix_ps_strings.ps_envstr = envp;
147 minix_ps_strings.ps_argvstr = argv;
148 minix_ps_strings.ps_nargvstr = argc;
149 ps_strings = &minix_ps_strings;
151 #endif /* __minix */
152 if (ps_strings == NULL)
153 _FATAL("ps_strings missing\n");
154 __ps_strings = ps_strings;
156 environ = ps_strings->ps_envstr;
158 if (ps_strings->ps_argvstr[0] != NULL) {
159 char *c;
160 __progname = ps_strings->ps_argvstr[0];
161 for (c = ps_strings->ps_argvstr[0]; *c; ++c) {
162 if (*c == '/')
163 __progname = c + 1;
165 } else {
166 __progname = empty_string;
169 if (&rtld_DYNAMIC != NULL) {
170 if (obj == NULL)
171 _FATAL("NULL Obj_Entry pointer in GOT\n");
172 if (obj->magic != RTLD_MAGIC)
173 _FATAL("Corrupt Obj_Entry pointer in GOT\n");
174 if (obj->version != RTLD_VERSION)
175 _FATAL("Dynamic linker version mismatch\n");
176 atexit(cleanup);
179 _libc_init();
181 #ifdef MCRT0
182 atexit(_mcleanup);
183 monstartup((u_long)&__eprol, (u_long)&__etext);
184 #endif
186 atexit(_fini);
187 _init();
189 exit(main(ps_strings->ps_nargvstr, ps_strings->ps_argvstr, environ));