crt1.c revision 204756
1239310Sdim/*-
2239310Sdim * Copyright 1996-1998 John D. Polstra.
3239310Sdim * All rights reserved.
4239310Sdim * Copyright (c) 1995 Christopher G. Demetriou
5239310Sdim * All rights reserved.
6239310Sdim *
7239310Sdim * Redistribution and use in source and binary forms, with or without
8239310Sdim * modification, are permitted provided that the following conditions
9239310Sdim * are met:
10239310Sdim * 1. Redistributions of source code must retain the above copyright
11239310Sdim *    notice, this list of conditions and the following disclaimer.
12239310Sdim * 2. Redistributions in binary form must reproduce the above copyright
13239310Sdim *    notice, this list of conditions and the following disclaimer in the
14239310Sdim *    documentation and/or other materials provided with the distribution.
15249423Sdim * 3. All advertising materials mentioning features or use of this software
16249423Sdim *    must display the following acknowledgement:
17249423Sdim *      This product includes software developed by Christopher G. Demetriou
18239310Sdim *    for the NetBSD Project.
19239310Sdim * 4. The name of the author may not be used to endorse or promote products
20249423Sdim *    derived from this software without specific prior written permission
21239310Sdim *
22239310Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23239310Sdim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24239310Sdim * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25239310Sdim * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26239310Sdim * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27239310Sdim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28249423Sdim * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29249423Sdim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30249423Sdim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31249423Sdim * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32239310Sdim *
33239310Sdim * $FreeBSD: head/lib/csu/mips/crt1.c 204756 2010-03-05 13:28:05Z uqs $
34249423Sdim */
35239310Sdim
36249423Sdim#include <sys/cdefs.h>
37249423Sdim__FBSDID("$FreeBSD: head/lib/csu/mips/crt1.c 204756 2010-03-05 13:28:05Z uqs $");
38239310Sdim
39249423Sdim#ifndef __GNUC__
40249423Sdim#error "GCC is needed to compile this file"
41249423Sdim#endif
42249423Sdim
43249423Sdim#include <stdlib.h>
44239310Sdim#include "libc_private.h"
45251662Sdim#include "crtbrand.c"
46251662Sdim
47251662Sdimstruct Struct_Obj_Entry;
48251662Sdimstruct ps_strings;
49251662Sdim
50239310Sdim#ifndef NOSHARED
51239310Sdimextern int _DYNAMIC;
52239310Sdim#pragma weak _DYNAMIC
53239310Sdim#endif
54239310Sdim
55239310Sdimextern void _init(void);
56239310Sdimextern void _fini(void);
57239310Sdimextern int main(int, char **, char **);
58239310Sdim
59249423Sdim#ifdef GCRT
60249423Sdimextern void _mcleanup(void);
61239310Sdimextern void monstartup(void *, void *);
62239310Sdimextern int eprol;
63239310Sdimextern int etext;
64239310Sdim#endif
65239310Sdim
66239310Sdimchar **environ;
67249423Sdimconst char *__progname = "";
68249423Sdim
69249423Sdimvoid __start(char **, void (*)(void), struct Struct_Obj_Entry *, struct ps_strings *);
70249423Sdim
71249423Sdim/* The entry function. */
72249423Sdimvoid
73239310Sdim__start(char **ap,
74239310Sdim	void (*cleanup)(void),			/* from shared loader */
75239310Sdim	struct Struct_Obj_Entry *obj,		/* from shared loader */
76239310Sdim	struct ps_strings *ps_strings)
77239310Sdim{
78239310Sdim	int argc;
79239310Sdim	char **argv;
80239310Sdim	char **env;
81239310Sdim
82251662Sdim	argc = * (long *) ap;
83251662Sdim	argv = ap + 1;
84239310Sdim	env  = ap + 2 + argc;
85239310Sdim	environ = env;
86239310Sdim	if(argc > 0 && argv[0] != NULL) {
87239310Sdim		const char *s;
88239310Sdim		__progname = argv[0];
89239310Sdim		for (s = __progname; *s != '\0'; s++)
90239310Sdim			if (*s == '/')
91239310Sdim				__progname = s + 1;
92249423Sdim	}
93239310Sdim
94239310Sdim#ifndef NOSHARED
95249423Sdim	if (&_DYNAMIC != NULL)
96239310Sdim		atexit(cleanup);
97239310Sdim#endif
98239310Sdim#ifdef GCRT
99239310Sdim	atexit(_mcleanup);
100239310Sdim#endif
101239310Sdim	atexit(_fini);
102239310Sdim#ifdef GCRT
103239310Sdim	monstartup(&eprol, &etext);
104239310Sdim#endif
105249423Sdim#ifndef NOGPREL
106249423Sdim	_init();
107249423Sdim#endif
108249423Sdim	exit( main(argc, argv, env) );
109249423Sdim}
110249423Sdim
111249423Sdim#ifdef GCRT
112249423Sdim__asm__(".text");
113249423Sdim__asm__("eprol:");
114249423Sdim__asm__(".previous");
115249423Sdim#endif
116249423Sdim