crt1.c revision 42049
1/*-
2 * Copyright 1996-1998 John D. Polstra.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 *      $Id: crt1.c,v 1.2 1998/09/07 23:31:59 jdp Exp $
26 */
27
28#ifndef __GNUC__
29#error "GCC is needed to compile this file"
30#endif
31
32#include <stddef.h>
33#include <stdlib.h>
34
35typedef void (*fptr)(void);
36
37extern void _fini(void);
38extern void _init(void);
39extern int main(int, char **, char **);
40
41#ifdef GCRT
42extern void _mcleanup(void);
43extern void monstartup(void *, void *);
44extern int eprol;
45extern int etext;
46#endif
47
48extern int _DYNAMIC;
49#pragma weak _DYNAMIC
50
51#ifdef __i386__
52#define get_rtld_cleanup()				\
53    ({ fptr __value;					\
54       __asm__("movl %%edx,%0" : "=rm"(__value));	\
55       __value; })
56#else
57#error "This file only supports the i386 architecture"
58#endif
59
60char **environ;
61char *__progname = "";
62
63void
64_start(char *arguments, ...)
65{
66    fptr rtld_cleanup;
67    int argc;
68    char **argv;
69    char **env;
70
71    rtld_cleanup = get_rtld_cleanup();
72    argv = &arguments;
73    argc = * (int *) (argv - 1);
74    env = argv + argc + 1;
75    environ = env;
76    if(argc > 0 && argv[0] != NULL) {
77	char *s;
78	__progname = argv[0];
79	for (s = __progname; *s != '\0'; s++)
80	    if (*s == '/')
81		__progname = s + 1;
82    }
83
84    if(&_DYNAMIC != NULL)
85	atexit(rtld_cleanup);
86
87#ifdef GCRT
88    atexit(_mcleanup);
89#endif
90    atexit(_fini);
91#ifdef GCRT
92    monstartup(&eprol, &etext);
93#endif
94    _init();
95    exit( main(argc, argv, env) );
96}
97
98#ifdef GCRT
99__asm__(".text");
100__asm__("eprol:");
101__asm__(".previous");
102#endif
103