xref: /original-bsd/lib/csu/i386/crt0.c (revision da7c76f1)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * William Jolitz.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)crt0.c	5.2 (Berkeley) 05/14/90";
13 #endif /* not lint */
14 
15 /*
16  *	C start up routine.
17  *	Robert Henry, UCB, 20 Oct 81
18  *
19  *	We make the following (true) assumption:
20  *	1) The only register variable that we can trust is ebp,
21  *	which points to the base of the kernel calling frame.
22  */
23 
24 char **environ = (char **)0;
25 static int fd;
26 
27 asm("#define _start start");
28 asm("#define _eprol eprol");
29 asm("	.text");
30 asm("	.long 0xc000c000");
31 extern	unsigned char	etext;
32 extern	unsigned char	eprol;
33 start()
34 {
35 	struct kframe {
36 		int	kargc;
37 		char	*kargv[1];	/* size depends on kargc */
38 		char	kargstr[1];	/* size varies */
39 		char	kenvstr[1];	/* size varies */
40 	};
41 	/*
42 	 *	ALL REGISTER VARIABLES!!!
43 	 */
44 	register struct kframe *kfp;	/* r10 */
45 	register char **targv;
46 	register char **argv;
47 	extern int errno;
48 
49 #ifdef lint
50 	kfp = 0;
51 	initcode = initcode = 0;
52 #else not lint
53 	asm("	lea	4(%ebp),%ebx");	/* catch it quick */
54 #endif not lint
55 	for (argv = targv = &kfp->kargv[0]; *targv++; /* void */)
56 		/* void */ ;
57 	if (targv >= (char **)(*argv))
58 		--targv;
59 	environ = targv;
60 asm("eprol:");
61 
62 #ifdef paranoid
63 	/*
64 	 * The standard I/O library assumes that file descriptors 0, 1, and 2
65 	 * are open. If one of these descriptors is closed prior to the start
66 	 * of the process, I/O gets very confused. To avoid this problem, we
67 	 * insure that the first three file descriptors are open before calling
68 	 * main(). Normally this is undefined, as it adds two unnecessary
69 	 * system calls.
70 	 */
71 	do	{
72 		fd = open("/dev/null", 2);
73 	} while (fd >= 0 && fd < 3);
74 	close(fd);
75 #endif paranoid
76 
77 #ifdef MCRT0
78 	monstartup(&eprol, &etext);
79 #endif MCRT0
80 	errno = 0;
81 	exit(main(kfp->kargc, argv, environ));
82 }
83 asm("#undef _start");
84 asm("#undef _eprol");
85 
86 #ifdef MCRT0
87 /*ARGSUSED*/
88 exit(code)
89 	register int code;	/* r11 */
90 {
91 	monitor(0);
92 	_cleanup();
93 	asm("	pushl	8(bp)") ;
94 	asm("	movl $1,%eax");
95 	asm("	.byte 0x9a; .long 0; .word 0");
96 }
97 #endif MCRT0
98 
99 #ifdef CRT0
100 /*
101  * null mcount and moncontrol,
102  * just in case some routine is compiled for profiling
103  */
104 moncontrol(val)
105 	int val;
106 {
107 
108 }
109 asm("	.globl	mcount");
110 asm("mcount:	ret");
111 #endif CRT0
112