xref: /original-bsd/lib/csu/vax/crt0.c (revision 023a1e8a)
1 /*-
2  * Copyright (c) 1982 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)crt0.c	5.11 (Berkeley) 07/16/92";
10 #endif /* not lint */
11 
12 /*
13  *	C start up routine.
14  *	Robert Henry, UCB, 20 Oct 81
15  *
16  *	We make the following (true) assumptions:
17  *	1) when the kernel calls start, it does a jump to location 2,
18  *	and thus avoids the register save mask.  We are NOT called
19  *	with a calls!  see sys1.c:setregs().
20  *	2) The only register variable that we can trust is sp,
21  *	which points to the base of the kernel calling frame.
22  *	Do NOT believe the documentation in exec(2) regarding the
23  *	values of fp and ap.
24  */
25 
26 char **environ = (char **)0;
27 static int fd;
28 
29 extern	unsigned char	etext;
30 extern	unsigned char	eprol asm ("eprol");
31 extern			start() asm("start");
32 
33 /*
34  * Two kluges: store sp at entry in environ, and
35  * install 16 bits of 0 at location 0 (a zero register save mask).
36  * These two hacks remove limits on the use of local
37  * and register variables in start().
38  */
39 asm(".text; .word 0; movl sp,_environ; jbr start+2");
40 
41 start()
42 {
43 	struct kframe {
44 		int	kargc;
45 		char	*kargv[1];	/* size depends on kargc */
46 		char	kargstr[1];	/* size varies */
47 		char	kenvstr[1];	/* size varies */
48 	};
49 	register struct kframe *kfp;
50 	register char **targv;
51 	register char **argv;
52 	extern int errno;
53 	extern void _mcleanup();
54 
55 #ifdef lint
56 	kfp = 0;
57 	initcode = initcode = 0;
58 #else not lint
59 	kfp = (struct kframe *) environ;
60 #endif not lint
61 	for (argv = targv = &kfp->kargv[0]; *targv++; /* void */)
62 		/* void */ ;
63 	if (targv >= (char **)(*argv))
64 		--targv;
65 	environ = targv;
66 asm("eprol:");
67 
68 #ifdef paranoid
69 	/*
70 	 * The standard I/O library assumes that file descriptors 0, 1, and 2
71 	 * are open. If one of these descriptors is closed prior to the start
72 	 * of the process, I/O gets very confused. To avoid this problem, we
73 	 * insure that the first three file descriptors are open before calling
74 	 * main(). Normally this is undefined, as it adds two unnecessary
75 	 * system calls.
76 	 */
77 	do	{
78 		fd = open("/dev/null", 2);
79 	} while (fd >= 0 && fd < 3);
80 	close(fd);
81 #endif paranoid
82 
83 #ifdef MCRT0
84 	atexit(_mcleanup);
85 	monstartup(&eprol, &etext);
86 #endif MCRT0
87 	errno = 0;
88 	exit(main(kfp->kargc, argv, environ));
89 }
90 
91 #ifdef CRT0
92 /*
93  * null moncontrol, just in case some routine is compiled for profiling
94  */
95 moncontrol(val)
96 	int val;
97 {
98 
99 }
100 #endif CRT0
101