1 /*- 2 * Copyright (c) 1992 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This software was developed by the Computer Systems Engineering group 6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 7 * contributed to Berkeley. 8 * 9 * %sccs.include.redist.c% 10 */ 11 12 #ifndef lint 13 static char sccsid[] = "@(#)crt0.c 5.1 (Berkeley) 06/25/92"; 14 #endif /* not lint */ 15 16 /* 17 * C start up routine. 18 */ 19 20 char **environ = (char **)0; 21 22 extern unsigned char etext[]; 23 extern volatile void start() asm("start0"); 24 extern unsigned char eprol[] asm("eprol"); 25 extern void _mcleanup(void); 26 volatile void exit(int); 27 28 volatile void 29 start(void) 30 { 31 struct kframe { 32 int regarea[16]; /* space for %i and %o variables */ 33 int kargc; /* argument count */ 34 char *kargv[1]; /* actual size depends on kargc */ 35 }; 36 register struct kframe *sp asm("%sp"); 37 register int argc; 38 register char **argv, **envp; 39 extern int errno; 40 41 asm(".globl start"); 42 asm("start:"); 43 argc = sp->kargc; 44 argv = &sp->kargv[0]; 45 environ = envp = &argv[argc + 1]; 46 sp = (struct kframe *)((int)sp - 16); 47 asm("eprol:"); 48 49 #ifdef paranoid 50 /* 51 * The standard I/O library assumes that file descriptors 0, 1, and 2 52 * are open. If one of these descriptors is closed prior to the start 53 * of the process, I/O gets very confused. To avoid this problem, we 54 * insure that the first three file descriptors are open before calling 55 * main(). Normally this is undefined, as it adds two unnecessary 56 * system calls. 57 */ 58 { 59 register int fd; 60 do { 61 fd = open("/dev/null", 2); 62 } while (fd >= 0 && fd < 3); 63 close(fd); 64 } 65 #endif 66 67 #ifdef MCRT0 68 monstartup(eprol, etext); 69 atexit(_mcleanup); 70 errno = 0; 71 #endif 72 exit(main(argc, argv, envp)); 73 /* NOTREACHED */ 74 } 75 76 #ifdef CRT0 77 /* 78 * null mcount and moncontrol, 79 * just in case some routine is compiled for profiling 80 */ 81 asm(".globl mcount"); 82 asm(".globl _moncontrol"); 83 asm("mcount: _moncontrol: retl; nop"); 84 #endif CRT0 85