crt1_c.c revision 34198
11590Srgrimes/*-
21590Srgrimes * Copyright 1996-1998 John D. Polstra.
31590Srgrimes * All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes *
141590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
151590Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
161590Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
171590Srgrimes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
181590Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
191590Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
201590Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
211590Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
221590Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
231590Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
241590Srgrimes *
251590Srgrimes *      $Id: crt1.c,v 1.4 1996/04/12 02:24:35 jdp Exp $
261590Srgrimes */
271590Srgrimes
281590Srgrimes#ifndef __GNUC__
291590Srgrimes#error "GCC is needed to compile this file"
301590Srgrimes#endif
311590Srgrimes
321590Srgrimes#include <stdlib.h>
331590Srgrimes
341590Srgrimestypedef void (*fptr)(void);
351590Srgrimes
361590Srgrimesextern void _fini(void);
371590Srgrimesextern void _init(void);
381590Srgrimesextern int main(int, char **, char **);
391590Srgrimes
401590Srgrimesextern int _DYNAMIC;
411590Srgrimes#pragma weak _DYNAMIC
421590Srgrimes
431590Srgrimes#ifdef __i386__
4432649Sbde#define get_rtld_cleanup()				\
4532649Sbde    ({ fptr __value;					\
4632649Sbde       __asm__("movl %%edx,%0" : "=rm"(__value));	\
473819Swollman       __value; })
4850477Speter#else
491590Srgrimes#error "This file only supports the i386 architecture"
501590Srgrimes#endif
511590Srgrimes
521590Srgrimeschar **environ;
5311936Sphkchar *__progname = "";
543819Swollman
559336Sdfrvoid
569336Sdfr_start(char *arguments, ...)
571590Srgrimes{
581590Srgrimes    fptr rtld_cleanup;
591590Srgrimes    int argc;
601590Srgrimes    char **argv;
611590Srgrimes    char **env;
621590Srgrimes
631590Srgrimes    rtld_cleanup = get_rtld_cleanup();
641590Srgrimes    argv = &arguments;
651590Srgrimes    argc = * (int *) (argv - 1);
661590Srgrimes    env = argv + argc + 1;
671590Srgrimes    environ = env;
681590Srgrimes    if(argc > 0)
693819Swollman	__progname = argv[0];
701590Srgrimes
711590Srgrimes    if(&_DYNAMIC != 0)
721590Srgrimes	atexit(rtld_cleanup);
731590Srgrimes
741590Srgrimes    atexit(_fini);
751590Srgrimes    _init();
761590Srgrimes    exit( main(argc, argv, env) );
771590Srgrimes}
783819Swollman