crt1.c revision 34198
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 *      $Id: crt1.c,v 1.4 1996/04/12 02:24:35 jdp Exp $
2634198Sjdp */
2734198Sjdp
2834198Sjdp#ifndef __GNUC__
2934198Sjdp#error "GCC is needed to compile this file"
3034198Sjdp#endif
3134198Sjdp
3234198Sjdp#include <stdlib.h>
3334198Sjdp
3434198Sjdptypedef void (*fptr)(void);
3534198Sjdp
3634198Sjdpextern void _fini(void);
3734198Sjdpextern void _init(void);
3834198Sjdpextern int main(int, char **, char **);
3934198Sjdp
4034198Sjdpextern int _DYNAMIC;
4134198Sjdp#pragma weak _DYNAMIC
4234198Sjdp
4334198Sjdp#ifdef __i386__
4434198Sjdp#define get_rtld_cleanup()				\
4534198Sjdp    ({ fptr __value;					\
4634198Sjdp       __asm__("movl %%edx,%0" : "=rm"(__value));	\
4734198Sjdp       __value; })
4834198Sjdp#else
4934198Sjdp#error "This file only supports the i386 architecture"
5034198Sjdp#endif
5134198Sjdp
5234198Sjdpchar **environ;
5334198Sjdpchar *__progname = "";
5434198Sjdp
5534198Sjdpvoid
5634198Sjdp_start(char *arguments, ...)
5734198Sjdp{
5834198Sjdp    fptr rtld_cleanup;
5934198Sjdp    int argc;
6034198Sjdp    char **argv;
6134198Sjdp    char **env;
6234198Sjdp
6334198Sjdp    rtld_cleanup = get_rtld_cleanup();
6434198Sjdp    argv = &arguments;
6534198Sjdp    argc = * (int *) (argv - 1);
6634198Sjdp    env = argv + argc + 1;
6734198Sjdp    environ = env;
6834198Sjdp    if(argc > 0)
6934198Sjdp	__progname = argv[0];
7034198Sjdp
7134198Sjdp    if(&_DYNAMIC != 0)
7234198Sjdp	atexit(rtld_cleanup);
7334198Sjdp
7434198Sjdp    atexit(_fini);
7534198Sjdp    _init();
7634198Sjdp    exit( main(argc, argv, env) );
7734198Sjdp}
78