crt1.c revision 204756
1168404Spjd/*-
2168404Spjd * Copyright 1996-1998 John D. Polstra.
3168404Spjd * All rights reserved.
4168404Spjd * Copyright (c) 1995 Christopher G. Demetriou
5168404Spjd * All rights reserved.
6168404Spjd *
7168404Spjd * Redistribution and use in source and binary forms, with or without
8168404Spjd * modification, are permitted provided that the following conditions
9168404Spjd * are met:
10168404Spjd * 1. Redistributions of source code must retain the above copyright
11168404Spjd *    notice, this list of conditions and the following disclaimer.
12168404Spjd * 2. Redistributions in binary form must reproduce the above copyright
13168404Spjd *    notice, this list of conditions and the following disclaimer in the
14168404Spjd *    documentation and/or other materials provided with the distribution.
15168404Spjd * 3. All advertising materials mentioning features or use of this software
16168404Spjd *    must display the following acknowledgement:
17168404Spjd *      This product includes software developed by Christopher G. Demetriou
18168404Spjd *    for the NetBSD Project.
19168404Spjd * 4. The name of the author may not be used to endorse or promote products
20168404Spjd *    derived from this software without specific prior written permission
21168404Spjd *
22219089Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23168404Spjd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24168404Spjd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25168404Spjd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26168404Spjd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27168404Spjd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28168404Spjd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29168404Spjd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30168404Spjd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31168404Spjd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32168404Spjd *
33168404Spjd * $FreeBSD: head/lib/csu/mips/crt1.c 204756 2010-03-05 13:28:05Z uqs $
34168404Spjd */
35168404Spjd
36168404Spjd#include <sys/cdefs.h>
37168404Spjd__FBSDID("$FreeBSD: head/lib/csu/mips/crt1.c 204756 2010-03-05 13:28:05Z uqs $");
38168404Spjd
39168404Spjd#ifndef __GNUC__
40185029Spjd#error "GCC is needed to compile this file"
41168404Spjd#endif
42169023Spjd
43168404Spjd#include <stdlib.h>
44168404Spjd#include "libc_private.h"
45168404Spjd#include "crtbrand.c"
46168404Spjd
47168404Spjdstruct Struct_Obj_Entry;
48168404Spjdstruct ps_strings;
49168404Spjd
50168404Spjd#ifndef NOSHARED
51168404Spjdextern int _DYNAMIC;
52168404Spjd#pragma weak _DYNAMIC
53185029Spjd#endif
54219089Spjd
55219089Spjdextern void _init(void);
56168404Spjdextern void _fini(void);
57185029Spjdextern int main(int, char **, char **);
58168404Spjd
59168404Spjd#ifdef GCRT
60185029Spjdextern void _mcleanup(void);
61185029Spjdextern void monstartup(void *, void *);
62185029Spjdextern int eprol;
63185029Spjdextern int etext;
64185029Spjd#endif
65185029Spjd
66185029Spjdchar **environ;
67185029Spjdconst char *__progname = "";
68185029Spjd
69185029Spjdvoid __start(char **, void (*)(void), struct Struct_Obj_Entry *, struct ps_strings *);
70185029Spjd
71185029Spjd/* The entry function. */
72185029Spjdvoid
73185029Spjd__start(char **ap,
74185029Spjd	void (*cleanup)(void),			/* from shared loader */
75185029Spjd	struct Struct_Obj_Entry *obj,		/* from shared loader */
76185029Spjd	struct ps_strings *ps_strings)
77185029Spjd{
78185029Spjd	int argc;
79185029Spjd	char **argv;
80185029Spjd	char **env;
81185029Spjd
82185029Spjd	argc = * (long *) ap;
83185029Spjd	argv = ap + 1;
84185029Spjd	env  = ap + 2 + argc;
85185029Spjd	environ = env;
86185029Spjd	if(argc > 0 && argv[0] != NULL) {
87185029Spjd		const char *s;
88185029Spjd		__progname = argv[0];
89185029Spjd		for (s = __progname; *s != '\0'; s++)
90185029Spjd			if (*s == '/')
91185029Spjd				__progname = s + 1;
92185029Spjd	}
93185029Spjd
94185029Spjd#ifndef NOSHARED
95185029Spjd	if (&_DYNAMIC != NULL)
96185029Spjd		atexit(cleanup);
97185029Spjd#endif
98185029Spjd#ifdef GCRT
99185029Spjd	atexit(_mcleanup);
100185029Spjd#endif
101168404Spjd	atexit(_fini);
102168404Spjd#ifdef GCRT
103168404Spjd	monstartup(&eprol, &etext);
104168404Spjd#endif
105168404Spjd#ifndef NOGPREL
106168404Spjd	_init();
107168404Spjd#endif
108168404Spjd	exit( main(argc, argv, env) );
109168404Spjd}
110168404Spjd
111168404Spjd#ifdef GCRT
112168404Spjd__asm__(".text");
113185029Spjd__asm__("eprol:");
114185029Spjd__asm__(".previous");
115185029Spjd#endif
116185029Spjd