xref: /original-bsd/lib/csu/sparc/crt0.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  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	8.1 (Berkeley) 06/01/93";
14 #endif /* not lint */
15 
16 /*
17  *	C start up routine.
18  */
19 
20 #include <stddef.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 char **environ = (char **)0;
25 static char empty[1];
26 char *__progname = empty;
27 
28 extern unsigned char etext[];
29 extern volatile void start() asm("start0");
30 extern unsigned char eprol[] asm("eprol");
31 extern void _mcleanup(void);
32 
33 volatile void
34 start(void)
35 {
36 	struct kframe {
37 		int	regarea[16];	/* space for %i and %o variables */
38 		int	kargc;		/* argument count */
39 		char	*kargv[1];	/* actual size depends on kargc */
40 	};
41 	register struct kframe *sp asm("%sp");
42 	register int argc;
43 	register char **argv, **envp;
44 	extern int errno;
45 
46 asm(".globl start");
47 asm("start:");
48 	argc = sp->kargc;
49 	argv = &sp->kargv[0];
50 	environ = envp = &argv[argc + 1];
51 	sp = (struct kframe *)((int)sp - 16);
52 asm("eprol:");
53 
54 #ifdef paranoid
55 	/*
56 	 * The standard I/O library assumes that file descriptors 0, 1, and 2
57 	 * are open. If one of these descriptors is closed prior to the start
58 	 * of the process, I/O gets very confused. To avoid this problem, we
59 	 * insure that the first three file descriptors are open before calling
60 	 * main(). Normally this is undefined, as it adds two unnecessary
61 	 * system calls.
62 	 */
63     {
64 	register int fd;
65 	do {
66 		fd = open("/dev/null", 2);
67 	} while (fd >= 0 && fd < 3);
68 	close(fd);
69     }
70 #endif
71 
72 #ifdef MCRT0
73 	monstartup(eprol, etext);
74 	atexit(_mcleanup);
75 	errno = 0;
76 #endif
77 	if (argv[0])
78 		if ((__progname = strrchr(argv[0], '/')) == NULL)
79 			__progname = argv[0];
80 		else
81 			++__progname;
82 	exit(main(argc, argv, envp));
83 }
84 
85 #ifdef CRT0
86 /*
87  * null mcount and moncontrol,
88  * just in case some routine is compiled for profiling
89  */
90 asm(".globl mcount");
91 asm(".globl _moncontrol");
92 asm("mcount: _moncontrol: retl; nop");
93 #endif
94