1 /* $MirOS: src/lib/csu/common_elf/common.c,v 1.8 2013/10/31 20:06:15 tg Exp $
2  * derived from the following files:
3  * $NetBSD: common.c,v 1.16 2004/08/26 21:01:12 thorpej Exp $
4  * $OpenBSD: crt0.c,v 1.11 2003/06/27 22:30:38 deraadt Exp $
5  */
6 
7 /*
8  * Copyright (c) 2003, 2004, 2005, 2009, 2013
9  *	Thorsten "mirabilos" Glaser <tg@mirbsd.org>
10  * Copyright (c) 1995 Christopher G. Demetriou
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *          This product includes software developed for the
24  *          NetBSD Project.  See http://www.NetBSD.org/ for
25  *          information about NetBSD.
26  * 4. The name of the author may not be used to endorse or promote products
27  *    derived from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
34  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  *
40  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
41  */
42 
43 #ifndef _COMMON_H
44 #error	not a stand-alone file
45 #endif
46 
47 __RCSID("$MirOS: src/lib/csu/common_elf/common.c,v 1.8 2013/10/31 20:06:15 tg Exp $");
48 
49 struct ps_strings *__ps_strings = NULL;
50 char __progname_storage[NAME_MAX + 1];
51 
52 static char *
_strrchr(char * p,int ch)53 _strrchr(char *p, int ch)
54 {
55 	char *save;
56 
57 	for (save = NULL;; ++p) {
58 		if (*p == ch)
59 			save = (char *)p;
60 		if (!*p)
61 			return (save);
62 	}
63 /* NOTREACHED */
64 }
65 
66 #ifdef MCRT0
67 __asm__(".text"
68     "\n_eprol:");
69 #endif
70 
___start(int argc,char ** argv,char ** envp,void (* cleanup)(void),const Obj_Entry * obj,struct ps_strings * ps_strings)71 void ___start(int argc, char **argv, char **envp,
72     void (*cleanup) (void) __attribute__((__unused__)),
73     const Obj_Entry *obj __attribute__((__unused__)),
74     struct ps_strings *ps_strings)
75 {
76 	char *namep, *s;
77 
78 	environ = envp;
79 
80 	if ((namep = argv[0]) != NULL) {	/* NULL ptr if argc == 0 */
81 		if ((__progname = _strrchr(namep, '/')) == NULL)
82 			__progname = namep;
83 		else
84 			++__progname;
85 		for (s = __progname_storage; (*__progname) &&
86 		    (s < &__progname_storage[NAME_MAX]); /* nothing */ )
87 			*s++ = *__progname++;
88 		*s = '\0';
89 		__progname = __progname_storage;
90 	}
91 	if (ps_strings != (struct ps_strings *)0)
92 		__ps_strings = ps_strings;
93 
94 #ifdef MCRT0
95 	atexit(_mcleanup);
96 	monstartup((u_long)&_eprol, (u_long)&_etext);
97 #endif
98 
99 	atexit(_fini);
100 	_init();
101 
102 	exit(main(argc, argv, environ));
103 }
104