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