crt1_c.c revision 34198
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 * $Id: crt1.c,v 1.4 1996/04/12 02:24:35 jdp Exp $ 26 */ 27 28#ifndef __GNUC__ 29#error "GCC is needed to compile this file" 30#endif 31 32#include <stdlib.h> 33 34typedef void (*fptr)(void); 35 36extern void _fini(void); 37extern void _init(void); 38extern int main(int, char **, char **); 39 40extern int _DYNAMIC; 41#pragma weak _DYNAMIC 42 43#ifdef __i386__ 44#define get_rtld_cleanup() \ 45 ({ fptr __value; \ 46 __asm__("movl %%edx,%0" : "=rm"(__value)); \ 47 __value; }) 48#else 49#error "This file only supports the i386 architecture" 50#endif 51 52char **environ; 53char *__progname = ""; 54 55void 56_start(char *arguments, ...) 57{ 58 fptr rtld_cleanup; 59 int argc; 60 char **argv; 61 char **env; 62 63 rtld_cleanup = get_rtld_cleanup(); 64 argv = &arguments; 65 argc = * (int *) (argv - 1); 66 env = argv + argc + 1; 67 environ = env; 68 if(argc > 0) 69 __progname = argv[0]; 70 71 if(&_DYNAMIC != 0) 72 atexit(rtld_cleanup); 73 74 atexit(_fini); 75 _init(); 76 exit( main(argc, argv, env) ); 77} 78