sol2-c1.asm revision 50397
1217309Snwhitehorn! crt1.s for Solaris 2, x86
2217309Snwhitehorn
3217309Snwhitehorn!   Copyright (C) 1993, 1998 Free Software Foundation, Inc.
4217309Snwhitehorn!   Written By Fred Fish, Nov 1992
5217309Snwhitehorn! 
6217309Snwhitehorn! This file is free software; you can redistribute it and/or modify it
7217309Snwhitehorn! under the terms of the GNU General Public License as published by the
8217309Snwhitehorn! Free Software Foundation; either version 2, or (at your option) any
9217309Snwhitehorn! later version.
10217309Snwhitehorn! 
11217309Snwhitehorn! In addition to the permissions in the GNU General Public License, the
12217309Snwhitehorn! Free Software Foundation gives you unlimited permission to link the
13217309Snwhitehorn! compiled version of this file with other programs, and to distribute
14217309Snwhitehorn! those programs without any restriction coming from the use of this
15217309Snwhitehorn! file.  (The General Public License restrictions do apply in other
16217309Snwhitehorn! respects; for example, they cover modification of the file, and
17217309Snwhitehorn! distribution when not linked into another program.)
18217309Snwhitehorn! 
19217309Snwhitehorn! This file is distributed in the hope that it will be useful, but
20217309Snwhitehorn! WITHOUT ANY WARRANTY; without even the implied warranty of
21217309Snwhitehorn! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22217309Snwhitehorn! General Public License for more details.
23217309Snwhitehorn! 
24217309Snwhitehorn! You should have received a copy of the GNU General Public License
25217309Snwhitehorn! along with this program; see the file COPYING.  If not, write to
26217309Snwhitehorn! the Free Software Foundation, 59 Temple Place - Suite 330,
27217309Snwhitehorn! Boston, MA 02111-1307, USA.
28217309Snwhitehorn! 
29217309Snwhitehorn!    As a special exception, if you link this library with files
30!    compiled with GCC to produce an executable, this does not cause
31!    the resulting executable to be covered by the GNU General Public License.
32!    This exception does not however invalidate any other reasons why
33!    the executable file might be covered by the GNU General Public License.
34! 
35
36! This file takes control of the process from the kernel, as specified
37! in section 3 of the System V Application Binary Interface, Intel386
38! Processor Supplement.  It has been constructed from information obtained
39! from the ABI, information obtained from single stepping existing
40! Solaris executables through their startup code with gdb, and from
41! information obtained by single stepping executables on other i386 SVR4
42! implementations.  This file is the first thing linked into any executable.
43
44	.file	"crt1.s"
45	.ident	"GNU C crt1.s"
46	.weak	_cleanup
47	.weak	_DYNAMIC
48	.text
49
50! Start creating the initial frame by pushing a NULL value for the return
51! address of the initial frame, and mark the end of the stack frame chain
52! (the innermost stack frame) with a NULL value, per page 3-32 of the ABI.
53! Initialize the first stack frame pointer in %ebp (the contents of which
54! are unspecified at process initialization).
55
56	.globl	_start
57_start:
58	pushl	$0x0
59	pushl	$0x0
60	movl	%esp,%ebp
61
62! As specified per page 3-32 of the ABI, %edx contains a function 
63! pointer that should be registered with atexit(), for proper
64! shared object termination.  Just push it onto the stack for now
65! to preserve it.  We want to register _cleanup() first.
66
67	pushl	%edx
68
69! Check to see if there is an _cleanup() function linked in, and if
70! so, register it with atexit() as the last thing to be run by
71! atexit().
72
73	movl	$_cleanup,%eax
74	testl	%eax,%eax
75	je	.L1
76	pushl	$_cleanup
77	call	atexit
78	addl	$0x4,%esp
79.L1:
80
81! Now check to see if we have an _DYNAMIC table, and if so then
82! we need to register the function pointer previously in %edx, but
83! now conveniently saved on the stack as the argument to pass to
84! atexit().
85
86	movl	$_DYNAMIC,%eax
87	testl	%eax,%eax
88	je	.L2
89	call	atexit
90.L2:
91
92! Register _fini() with atexit().  We will take care of calling _init()
93! directly.
94
95	pushl	$_fini
96	call	atexit
97
98! Compute the address of the environment vector on the stack and load
99! it into the global variable _environ.  Currently argc is at 8 off
100! the frame pointer.  Fetch the argument count into %eax, scale by the
101! size of each arg (4 bytes) and compute the address of the environment
102! vector which is 16 bytes (the two zero words we pushed, plus argc,
103! plus the null word terminating the arg vector) further up the stack,
104! off the frame pointer (whew!).
105
106	movl	8(%ebp),%eax
107	leal	16(%ebp,%eax,4),%edx
108	movl	%edx,_environ
109
110! Push the environment vector pointer, the argument vector pointer,
111! and the argument count on to the stack to set up the arguments
112! for _init(), _fpstart(), and main().  Note that the environment
113! vector pointer and the arg count were previously loaded into
114! %edx and %eax respectively.  The only new value we need to compute
115! is the argument vector pointer, which is at a fixed address off
116! the initial frame pointer.
117
118	pushl	%edx
119	leal	12(%ebp),%edx
120	pushl	%edx
121	pushl	%eax
122
123! Call _init(argc, argv, environ), _fpstart(argc, argv, environ), and
124! main(argc, argv, environ).
125
126	call	_init
127	call	__fpstart
128	call	main
129
130! Pop the argc, argv, and environ arguments off the stack, push the
131! value returned from main(), and call exit().
132
133	addl	$12,%esp
134	pushl	%eax
135	call	exit
136
137! An inline equivalent of _exit, as specified in Figure 3-26 of the ABI.
138
139	pushl	$0x0
140	movl	$0x1,%eax
141	lcall	$7,$0
142
143! If all else fails, just try a halt!
144
145	hlt
146	.type	_start,@function
147	.size	_start,.-_start
148
149! A dummy profiling support routine for non-profiling executables,
150! in case we link in some objects that have been compiled for profiling.
151
152	.weak	_mcount
153_mcount:
154	ret
155	.type	_mcount,@function
156	.size	_mcount,.-_mcount
157