crt1.c revision 93399
191396Stmm/*-
291396Stmm * Copyright 1996-1998 John D. Polstra.
391396Stmm * All rights reserved.
491396Stmm *
591396Stmm * Redistribution and use in source and binary forms, with or without
691396Stmm * modification, are permitted provided that the following conditions
791396Stmm * are met:
891396Stmm * 1. Redistributions of source code must retain the above copyright
991396Stmm *    notice, this list of conditions and the following disclaimer.
1091396Stmm * 2. Redistributions in binary form must reproduce the above copyright
1191396Stmm *    notice, this list of conditions and the following disclaimer in the
1291396Stmm *    documentation and/or other materials provided with the distribution.
1391396Stmm *
1491396Stmm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1591396Stmm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1691396Stmm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1791396Stmm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1891396Stmm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1991396Stmm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2091396Stmm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2191396Stmm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2291396Stmm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2391396Stmm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2491396Stmm */
2591396Stmm
2691396Stmm#ifndef __GNUC__
2791396Stmm#error "GCC is needed to compile this file"
2891396Stmm#endif
2991396Stmm
3091396Stmm#include <stddef.h>
3191396Stmm#include <stdlib.h>
3291396Stmm#include "libc_private.h"
3391396Stmm#include "crtbrand.c"
3491396Stmm
3591396Stmmtypedef void (*fptr)(void);
3691396Stmm
3791396Stmmextern void _fini(void);
3891396Stmmextern void _init(void);
3991396Stmmextern int main(int, char **, char **);
4091396Stmm
4191396Stmm#ifdef GCRT
4291396Stmmextern void _mcleanup(void);
4391396Stmmextern void monstartup(void *, void *);
4491396Stmmextern int eprol;
4591396Stmmextern int etext;
4691396Stmm#endif
4791396Stmm
4891396Stmmextern int _DYNAMIC;
4991396Stmm#pragma weak _DYNAMIC
5091396Stmm
5191396Stmm#ifdef __i386__
5291396Stmm#define get_rtld_cleanup()				\
5391396Stmm    ({ fptr __value;					\
5491396Stmm       __asm__("movl %%edx,%0" : "=rm"(__value));	\
5591396Stmm       __value; })
5691396Stmm#else
5791396Stmm#error "This file only supports the i386 architecture"
5891396Stmm#endif
5991396Stmm
6091396Stmmchar **environ;
6191396Stmmconst char *__progname = "";
6291396Stmm
6391396Stmmvoid
6491396Stmm_start(char *arguments, ...)
6591396Stmm{
6691396Stmm    fptr rtld_cleanup;
6791396Stmm    int argc;
6891396Stmm    char **argv;
6991396Stmm    char **env;
7091396Stmm    const char *s;
7191396Stmm
7291396Stmm    rtld_cleanup = get_rtld_cleanup();
7391396Stmm    argv = &arguments;
7491396Stmm    argc = * (int *) (argv - 1);
7591396Stmm    env = argv + argc + 1;
7691396Stmm    environ = env;
7791396Stmm    if (argc > 0 && argv[0] != NULL) {
7891396Stmm	__progname = argv[0];
7991396Stmm	for (s = __progname; *s != '\0'; s++)
80108976Stmm	    if (*s == '/')
81108976Stmm		__progname = s + 1;
82108976Stmm    }
8391396Stmm
8491396Stmm    if (&_DYNAMIC != NULL)
8591396Stmm	atexit(rtld_cleanup);
8691396Stmm
8791396Stmm#ifdef GCRT
88108976Stmm    atexit(_mcleanup);
89108976Stmm#endif
90108976Stmm    atexit(_fini);
91108976Stmm#ifdef GCRT
92108976Stmm    monstartup(&eprol, &etext);
9391396Stmm#endif
9491396Stmm    _init();
9591396Stmm    exit( main(argc, argv, env) );
9691396Stmm}
9791396Stmm
9891396Stmm#ifdef GCRT
9991396Stmm__asm__(".text");
10091396Stmm__asm__("eprol:");
10191396Stmm__asm__(".previous");
10291396Stmm#endif
10391396Stmm
10491396Stmm__asm__(".ident\t\"$FreeBSD: head/lib/csu/amd64/crt1.c 93399 2002-03-29 22:43:43Z markm $\"");
10591396Stmm