1178739Sgonzo/*-
2178739Sgonzo * Copyright 1996-1998 John D. Polstra.
3178739Sgonzo * All rights reserved.
4178739Sgonzo * Copyright (c) 1995 Christopher G. Demetriou
5178739Sgonzo * All rights reserved.
6178739Sgonzo *
7178739Sgonzo * Redistribution and use in source and binary forms, with or without
8178739Sgonzo * modification, are permitted provided that the following conditions
9178739Sgonzo * are met:
10178739Sgonzo * 1. Redistributions of source code must retain the above copyright
11178739Sgonzo *    notice, this list of conditions and the following disclaimer.
12178739Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
13178739Sgonzo *    notice, this list of conditions and the following disclaimer in the
14178739Sgonzo *    documentation and/or other materials provided with the distribution.
15178739Sgonzo * 3. All advertising materials mentioning features or use of this software
16178739Sgonzo *    must display the following acknowledgement:
17178739Sgonzo *      This product includes software developed by Christopher G. Demetriou
18178739Sgonzo *    for the NetBSD Project.
19178739Sgonzo * 4. The name of the author may not be used to endorse or promote products
20178739Sgonzo *    derived from this software without specific prior written permission
21178739Sgonzo *
22178739Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23178739Sgonzo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24178739Sgonzo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25178739Sgonzo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26178739Sgonzo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27178739Sgonzo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28178739Sgonzo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29178739Sgonzo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30178739Sgonzo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31178739Sgonzo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32178739Sgonzo *
33178739Sgonzo * $FreeBSD$
34178739Sgonzo */
35178739Sgonzo
36178739Sgonzo#include <sys/cdefs.h>
37178739Sgonzo__FBSDID("$FreeBSD$");
38178739Sgonzo
39178739Sgonzo#ifndef __GNUC__
40178739Sgonzo#error "GCC is needed to compile this file"
41178739Sgonzo#endif
42178739Sgonzo
43178739Sgonzo#include <stdlib.h>
44178739Sgonzo#include "libc_private.h"
45178739Sgonzo#include "crtbrand.c"
46178739Sgonzo
47178739Sgonzostruct Struct_Obj_Entry;
48178739Sgonzostruct ps_strings;
49178739Sgonzo
50178739Sgonzo#ifndef NOSHARED
51178739Sgonzoextern int _DYNAMIC;
52178739Sgonzo#pragma weak _DYNAMIC
53178739Sgonzo#endif
54178739Sgonzo
55178739Sgonzoextern void _init(void);
56178739Sgonzoextern void _fini(void);
57178739Sgonzoextern int main(int, char **, char **);
58178739Sgonzo
59178739Sgonzo#ifdef GCRT
60178739Sgonzoextern void _mcleanup(void);
61178739Sgonzoextern void monstartup(void *, void *);
62178739Sgonzoextern int eprol;
63178739Sgonzoextern int etext;
64178739Sgonzo#endif
65178739Sgonzo
66178739Sgonzochar **environ;
67178739Sgonzoconst char *__progname = "";
68178739Sgonzo
69204756Suqsvoid __start(char **, void (*)(void), struct Struct_Obj_Entry *, struct ps_strings *);
70204756Suqs
71178739Sgonzo/* The entry function. */
72178739Sgonzovoid
73178739Sgonzo__start(char **ap,
74178739Sgonzo	void (*cleanup)(void),			/* from shared loader */
75204757Suqs	struct Struct_Obj_Entry *obj __unused,	/* from shared loader */
76204757Suqs	struct ps_strings *ps_strings __unused)
77178739Sgonzo{
78178739Sgonzo	int argc;
79178739Sgonzo	char **argv;
80178739Sgonzo	char **env;
81178739Sgonzo
82178739Sgonzo	argc = * (long *) ap;
83178739Sgonzo	argv = ap + 1;
84178739Sgonzo	env  = ap + 2 + argc;
85245777Skib	if (environ == NULL)
86245777Skib		environ = env;
87218179Simp	if (argc > 0 && argv[0] != NULL) {
88178739Sgonzo		const char *s;
89178739Sgonzo		__progname = argv[0];
90178739Sgonzo		for (s = __progname; *s != '\0'; s++)
91178739Sgonzo			if (*s == '/')
92178739Sgonzo				__progname = s + 1;
93178739Sgonzo	}
94178739Sgonzo
95178739Sgonzo#ifndef NOSHARED
96178739Sgonzo	if (&_DYNAMIC != NULL)
97178739Sgonzo		atexit(cleanup);
98178739Sgonzo#endif
99178739Sgonzo#ifdef GCRT
100178739Sgonzo	atexit(_mcleanup);
101178739Sgonzo#endif
102178739Sgonzo	atexit(_fini);
103178739Sgonzo#ifdef GCRT
104178739Sgonzo	monstartup(&eprol, &etext);
105178739Sgonzo#endif
106178739Sgonzo#ifndef NOGPREL
107178739Sgonzo	_init();
108178739Sgonzo#endif
109178739Sgonzo	exit( main(argc, argv, env) );
110178739Sgonzo}
111178739Sgonzo
112178739Sgonzo#ifdef GCRT
113178739Sgonzo__asm__(".text");
114178739Sgonzo__asm__("eprol:");
115178739Sgonzo__asm__(".previous");
116178739Sgonzo#endif
117