crt1_c.c revision 100167
1241675Suqs/*-
2241675Suqs * Copyright 1996-1998 John D. Polstra.
3241675Suqs * All rights reserved.
4241675Suqs *
5241675Suqs * Redistribution and use in source and binary forms, with or without
6241675Suqs * modification, are permitted provided that the following conditions
7241675Suqs * are met:
8241675Suqs * 1. Redistributions of source code must retain the above copyright
9241675Suqs *    notice, this list of conditions and the following disclaimer.
10241675Suqs * 2. Redistributions in binary form must reproduce the above copyright
11241675Suqs *    notice, this list of conditions and the following disclaimer in the
12241675Suqs *    documentation and/or other materials provided with the distribution.
13241675Suqs *
14241675Suqs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15241675Suqs * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16241675Suqs * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17241675Suqs * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18241675Suqs * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19241675Suqs * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20241675Suqs * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21241675Suqs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22241675Suqs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23241675Suqs * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24241675Suqs */
25241675Suqs
26241675Suqs#ifndef lint
27241675Suqs#ifndef __GNUC__
28241675Suqs#error "GCC is needed to compile this file"
29241675Suqs#endif
30241675Suqs#endif /* lint */
31241675Suqs
32241675Suqs#include <stdlib.h>
33241675Suqs
34241675Suqs#include "libc_private.h"
35241675Suqs#include "crtbrand.c"
36241675Suqs
37241675Suqsextern int _DYNAMIC;
38241675Suqs#pragma weak _DYNAMIC
39241675Suqs
40241675Suqstypedef void (*fptr)(void);
41241675Suqs
42241675Suqsextern void _fini(void);
43241675Suqsextern void _init(void);
44241675Suqsextern int main(int, char **, char **);
45241675Suqsextern void _start(char *, ...);
46241675Suqs
47241675Suqs#ifdef GCRT
48241675Suqsextern void _mcleanup(void);
49241675Suqsextern void monstartup(void *, void *);
50241675Suqsextern int eprol;
51241675Suqsextern int etext;
52241675Suqs#endif
53241675Suqs
54241675Suqschar **environ;
55241675Suqsconst char *__progname = "";
56241675Suqs
57241675Suqsstatic __inline fptr
58241675Suqsget_rtld_cleanup(void)
59241675Suqs{
60241675Suqs	fptr retval;
61241675Suqs
62241675Suqs#ifdef	__GNUC__
63241675Suqs	__asm__("movl %%edx,%0" : "=rm"(retval));
64241675Suqs#else
65241675Suqs	retval = (fptr)0; /* XXXX Fix this for other compilers */
66241675Suqs#endif
67241675Suqs	return(retval);
68241675Suqs}
69241675Suqs
70241675Suqs/* The entry function. */
71241675Suqsvoid
72241675Suqs_start(char *ap, ...)
73241675Suqs{
74241675Suqs	fptr cleanup;
75241675Suqs	int argc;
76241675Suqs	char **argv;
77241675Suqs	char **env;
78241675Suqs	const char *s;
79241675Suqs
80241675Suqs	cleanup = get_rtld_cleanup();
81241675Suqs	argv = &ap;
82241675Suqs	argc = *(long *)(void *)(argv - 1);
83241675Suqs	env = argv + argc + 1;
84241675Suqs	environ = env;
85241675Suqs	if (argc > 0 && argv[0] != NULL) {
86241675Suqs		__progname = argv[0];
87241675Suqs		for (s = __progname; *s != '\0'; s++)
88241675Suqs			if (*s == '/')
89241675Suqs				__progname = s + 1;
90241675Suqs	}
91241675Suqs
92241675Suqs	if (&_DYNAMIC != NULL)
93241675Suqs		atexit(cleanup);
94241675Suqs
95241675Suqs#ifdef GCRT
96241675Suqs	atexit(_mcleanup);
97241675Suqs#endif
98241675Suqs	atexit(_fini);
99241675Suqs#ifdef GCRT
100241675Suqs	monstartup(&eprol, &etext);
101241675Suqs#endif
102241675Suqs	_init();
103241675Suqs	exit( main(argc, argv, env) );
104241675Suqs}
105241675Suqs
106241675Suqs#ifdef GCRT
107241675Suqs__asm__(".text");
108241675Suqs__asm__("eprol:");
109241675Suqs__asm__(".previous");
110241675Suqs#endif
111241675Suqs
112241675Suqs__asm__(".ident\t\"$FreeBSD: head/lib/csu/i386-elf/crt1.c 100167 2002-07-16 12:28:50Z markm $\"");
113241675Suqs