crt1_c.c revision 93399
134198Sjdp/*-
234198Sjdp * Copyright 1996-1998 John D. Polstra.
334198Sjdp * All rights reserved.
434198Sjdp *
534198Sjdp * Redistribution and use in source and binary forms, with or without
634198Sjdp * modification, are permitted provided that the following conditions
734198Sjdp * are met:
834198Sjdp * 1. Redistributions of source code must retain the above copyright
934198Sjdp *    notice, this list of conditions and the following disclaimer.
1034198Sjdp * 2. Redistributions in binary form must reproduce the above copyright
1134198Sjdp *    notice, this list of conditions and the following disclaimer in the
1234198Sjdp *    documentation and/or other materials provided with the distribution.
1334198Sjdp *
1434198Sjdp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1534198Sjdp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1634198Sjdp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1734198Sjdp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1834198Sjdp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1934198Sjdp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2034198Sjdp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2134198Sjdp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2234198Sjdp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2334198Sjdp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2434198Sjdp */
2534198Sjdp
2634198Sjdp#ifndef __GNUC__
2734198Sjdp#error "GCC is needed to compile this file"
2834198Sjdp#endif
2934198Sjdp
3042049Ssteve#include <stddef.h>
3134198Sjdp#include <stdlib.h>
3293399Smarkm#include "libc_private.h"
3367811Sobrien#include "crtbrand.c"
3434198Sjdp
3534198Sjdptypedef void (*fptr)(void);
3634198Sjdp
3734198Sjdpextern void _fini(void);
3834198Sjdpextern void _init(void);
3934198Sjdpextern int main(int, char **, char **);
4034198Sjdp
4138928Sjdp#ifdef GCRT
4238928Sjdpextern void _mcleanup(void);
4338928Sjdpextern void monstartup(void *, void *);
4438928Sjdpextern int eprol;
4538928Sjdpextern int etext;
4638928Sjdp#endif
4738928Sjdp
4834198Sjdpextern int _DYNAMIC;
4934198Sjdp#pragma weak _DYNAMIC
5034198Sjdp
5134198Sjdp#ifdef __i386__
5234198Sjdp#define get_rtld_cleanup()				\
5334198Sjdp    ({ fptr __value;					\
5434198Sjdp       __asm__("movl %%edx,%0" : "=rm"(__value));	\
5534198Sjdp       __value; })
5634198Sjdp#else
5734198Sjdp#error "This file only supports the i386 architecture"
5834198Sjdp#endif
5934198Sjdp
6034198Sjdpchar **environ;
6193399Smarkmconst char *__progname = "";
6234198Sjdp
6334198Sjdpvoid
6434198Sjdp_start(char *arguments, ...)
6534198Sjdp{
6634198Sjdp    fptr rtld_cleanup;
6734198Sjdp    int argc;
6834198Sjdp    char **argv;
6934198Sjdp    char **env;
7093399Smarkm    const char *s;
7134198Sjdp
7234198Sjdp    rtld_cleanup = get_rtld_cleanup();
7334198Sjdp    argv = &arguments;
7434198Sjdp    argc = * (int *) (argv - 1);
7534198Sjdp    env = argv + argc + 1;
7634198Sjdp    environ = env;
7791430Sobrien    if (argc > 0 && argv[0] != NULL) {
7834198Sjdp	__progname = argv[0];
7942049Ssteve	for (s = __progname; *s != '\0'; s++)
8042049Ssteve	    if (*s == '/')
8142049Ssteve		__progname = s + 1;
8242049Ssteve    }
8334198Sjdp
8491430Sobrien    if (&_DYNAMIC != NULL)
8534198Sjdp	atexit(rtld_cleanup);
8634198Sjdp
8738928Sjdp#ifdef GCRT
8838928Sjdp    atexit(_mcleanup);
8938928Sjdp#endif
9034198Sjdp    atexit(_fini);
9138928Sjdp#ifdef GCRT
9238928Sjdp    monstartup(&eprol, &etext);
9338928Sjdp#endif
9434198Sjdp    _init();
9534198Sjdp    exit( main(argc, argv, env) );
9634198Sjdp}
9738928Sjdp
9838928Sjdp#ifdef GCRT
9938928Sjdp__asm__(".text");
10038928Sjdp__asm__("eprol:");
10138928Sjdp__asm__(".previous");
10238928Sjdp#endif
10391430Sobrien
10491430Sobrien__asm__(".ident\t\"$FreeBSD: head/lib/csu/i386-elf/crt1.c 93399 2002-03-29 22:43:43Z markm $\"");
105