crt1.c revision 91430
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
26#ifndef __GNUC__
27#error "GCC is needed to compile this file"
28#endif
29
30#include <stddef.h>
31#include <stdlib.h>
32#include "crtbrand.c"
33
34typedef void (*fptr)(void);
35
36extern void _fini(void);
37extern void _init(void);
38extern int main(int, char **, char **);
39
40#ifdef GCRT
41extern void _mcleanup(void);
42extern void monstartup(void *, void *);
43extern int eprol;
44extern int etext;
45#endif
46
47extern int _DYNAMIC;
48#pragma weak _DYNAMIC
49
50#ifdef __i386__
51#define get_rtld_cleanup()				\
52    ({ fptr __value;					\
53       __asm__("movl %%edx,%0" : "=rm"(__value));	\
54       __value; })
55#else
56#error "This file only supports the i386 architecture"
57#endif
58
59char **environ;
60char *__progname = "";
61
62void
63_start(char *arguments, ...)
64{
65    fptr rtld_cleanup;
66    int argc;
67    char **argv;
68    char **env;
69
70    rtld_cleanup = get_rtld_cleanup();
71    argv = &arguments;
72    argc = * (int *) (argv - 1);
73    env = argv + argc + 1;
74    environ = env;
75    if (argc > 0 && argv[0] != NULL) {
76	char *s;
77	__progname = argv[0];
78	for (s = __progname; *s != '\0'; s++)
79	    if (*s == '/')
80		__progname = s + 1;
81    }
82
83    if (&_DYNAMIC != NULL)
84	atexit(rtld_cleanup);
85
86#ifdef GCRT
87    atexit(_mcleanup);
88#endif
89    atexit(_fini);
90#ifdef GCRT
91    monstartup(&eprol, &etext);
92#endif
93    _init();
94    exit( main(argc, argv, env) );
95}
96
97#ifdef GCRT
98__asm__(".text");
99__asm__("eprol:");
100__asm__(".previous");
101#endif
102
103__asm__(".ident\t\"$FreeBSD: head/lib/csu/amd64/crt1.c 91430 2002-02-27 22:13:02Z obrien $\"");
104