xref: /original-bsd/lib/csu/i386/crt0.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)crt0.c	8.1 (Berkeley) 06/01/93";
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 #include <stddef.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 char **environ = (char **)0;
26 static char empty[1];
27 char *__progname = empty;
28 static int fd;
29 
30 asm(".text");
31 asm(".long 0xc000c000");
32 
33 extern	unsigned char	etext;
34 extern	unsigned char	eprol asm ("eprol");
35 extern			start() asm("start");
36 
37 start()
38 {
39 	struct kframe {
40 		int	kargc;
41 		char	*kargv[1];	/* size depends on kargc */
42 		char	kargstr[1];	/* size varies */
43 		char	kenvstr[1];	/* size varies */
44 	};
45 	/*
46 	 *	ALL REGISTER VARIABLES!!!
47 	 */
48 	register struct kframe *kfp;	/* r10 */
49 	register char **targv;
50 	register char **argv;
51 	extern int errno;
52 	extern void _mcleanup();
53 
54 #ifdef lint
55 	kfp = 0;
56 	initcode = initcode = 0;
57 #else
58 	asm("lea 4(%ebp),%ebx");	/* catch it quick */
59 #endif
60 	for (argv = targv = &kfp->kargv[0]; *targv++; /* void */)
61 		/* void */ ;
62 	if (targv >= (char **)(*argv))
63 		--targv;
64 	environ = targv;
65 asm("eprol:");
66 
67 #ifdef paranoid
68 	/*
69 	 * The standard I/O library assumes that file descriptors 0, 1, and 2
70 	 * are open. If one of these descriptors is closed prior to the start
71 	 * of the process, I/O gets very confused. To avoid this problem, we
72 	 * insure that the first three file descriptors are open before calling
73 	 * main(). Normally this is undefined, as it adds two unnecessary
74 	 * system calls.
75 	 */
76 	do	{
77 		fd = open("/dev/null", 2);
78 	} while (fd >= 0 && fd < 3);
79 	close(fd);
80 #endif
81 
82 #ifdef MCRT0
83 	atexit(_mcleanup);
84 	monstartup(&eprol, &etext);
85 #endif
86 	errno = 0;
87 	if (argv[0])
88 		if ((__progname = strrchr(argv[0], '/')) == NULL)
89 			__progname = argv[0];
90 		else
91 			++__progname;
92 	exit(main(kfp->kargc, argv, environ));
93 }
94 
95 #ifdef CRT0
96 /*
97  * null moncontrol just in case some routine is compiled for profiling
98  */
99 moncontrol(val)
100 	int val;
101 {
102 
103 }
104 #endif
105