some coverity fixes.
[minix.git] / lib / csu / common / crt0-common.c
blob77e1a09845ffe656f1635b54317c9c2d8d1360a4
1 /* $NetBSD: crt0-common.c,v 1.1 2010/08/07 18:01:33 joerg 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.1 2010/08/07 18:01:33 joerg Exp $");
41 #include <sys/types.h>
42 #ifndef __minix
43 #include <sys/syscall.h>
44 #endif
45 #include <machine/profile.h>
46 #include <stdlib.h>
47 #include <unistd.h>
49 #include "rtld.h"
51 extern int main(int, char **, char **);
53 extern void _init(void);
54 extern void _fini(void);
57 * Arrange for _DYNAMIC to be weak and undefined (and therefore to show up
58 * as being at address zero, unless something else defines it). That way,
59 * if we happen to be compiling without -static but with without any
60 * shared libs present, things will still work.
63 #if __GNUC_PREREQ__(4,2)
64 static int rtld_DYNAMIC __attribute__((__weakref__, __alias__("_DYNAMIC")));
65 #define DYNAMIC_SYM rtld_DYNAMIC
66 #else
67 extern int _DYNAMIC __weak_reference(_DYNAMIC);
68 #define DYNAMIC_SYM _DYNAMIC
69 #endif
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 */
77 char **environ;
78 struct ps_strings *__ps_strings = 0;
80 static char empty_string[] = "";
81 char *__progname = empty_string;
83 void ___start(int, char **, char **, void (*)(void),
84 const Obj_Entry *, struct ps_strings *);
86 #ifndef __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
92 #define _FATAL(str) \
93 do { \
94 write(2, str, sizeof(str)-1); \
95 _exit(1); \
96 } while (0)
98 void
99 ___start(int argc, char **argv, char **envp,
100 void (*cleanup)(void), /* from shared loader */
101 const Obj_Entry *obj, /* from shared loader */
102 struct ps_strings *ps_strings)
104 environ = envp;
106 if (argv[0] != NULL) {
107 char *c;
108 __progname = argv[0];
109 for (c = argv[0]; *c; ++c) {
110 if (*c == '/')
111 __progname = c + 1;
113 } else {
114 __progname = empty_string;
117 if (ps_strings != NULL)
118 __ps_strings = ps_strings;
120 if (&DYNAMIC_SYM != NULL) {
121 if ((obj == NULL) || (obj->magic != RTLD_MAGIC))
122 _FATAL("Corrupt Obj_Entry pointer in GOT\n");
123 if (obj->version != RTLD_VERSION)
124 _FATAL("Dynamic linker version mismatch\n");
125 atexit(cleanup);
128 #ifdef MCRT0
129 atexit(_mcleanup);
130 monstartup((u_long)&__eprol, (u_long)&__etext);
131 #endif
133 atexit(_fini);
134 _init();
136 exit(main(argc, argv, environ));