xref: /titanic_41/usr/src/lib/common/amd64/crt1.s (revision 66f9d5cb3cc0652e2d9d1366fb950efbe4ca2f24)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27/*
28 * This crt1.o module is provided as the bare minimum required to build
29 * a 64-bit executable with gcc.  It is installed in /usr/lib/amd64
30 * where it will be picked up by gcc, along with crti.o and crtn.o
31 */
32
33	.ident	"%Z%%M%	%I%	%E% SMI"
34
35	.file	"crt1.s"
36
37	.globl	_start
38
39/* global entities defined elsewhere but used here */
40	.globl	main
41	.globl	__fpstart
42	.globl	exit
43	.globl	_exit
44	.weak	_DYNAMIC
45
46	.section	.data
47
48	.weak	environ
49	.set	environ,_environ
50	.globl	_environ
51	.type	_environ,@object
52	.size	_environ,8
53	.align	8
54_environ:
55	.8byte	0x0
56
57	.globl	___Argv
58	.type	___Argv,@object
59	.size	___Argv,8
60	.align	8
61___Argv:
62	.8byte	0x0
63
64	.section	.text
65	.align	8
66
67/*
68 *   The SVR4/i386 ABI (pages 3-29) says that when the entry
69 *   point runs registers' %rbp, %rsp, %rdx values are specified
70 *   the following:
71 *
72 *	%rbp The content of this register is unspecified at
73 *		process initialization time, but the user code should mark
74 *		the deepest stack frame by setting the frame pointer to zero.
75 *		No other frame's %ebp should have a zero value.
76 *
77 *	%rsp Performing its usual job, the stack pointer holds the address
78 *	of the bottom of the stack, which is guaranteed to be
79 *	quadword aligned.
80 *
81 *		The stack contains the arguments and environment:
82 *        ...
83 *        envp[0]		(16+(8*argc))(%rsp)
84 *        NULL			(8+(8*argc))(%rsp)
85 *        ...
86 *        argv[0]		8(%rsp)
87 *        argc			0(%rsp)
88 *
89 *	%rdx In a conforming program, this register contains a function
90 *		pointer that the application should register with atexit(BA_OS).
91 *		This function is used for shared object termination code
92 *		[see Dynamic Linking in Chapter 5 of the System V ABI].
93 *
94 */
95
96	.type	_start,@function
97_start:
98/*
99 * Allocate a NULL return address and a NULL previous %rbp as if
100 * there was a genuine call to _start.
101 */
102	pushq	$0
103	pushq	$0
104	movq	%rsp,%rbp		/* The first stack frame */
105
106/*
107 * The stack now is
108 *
109 *        envp[0]		(32+(8*argc))(%rsp)      - (A)
110 *        NULL			(24+(8*argc))(%rsp)
111 *        ...
112 *        argv[0]		24(%rbp)		 - (B)
113 *        argc			16(%rbp)
114 *	  0			8(%rbp)
115 *	  0			0(%rbp)
116 */
117
118	movq	$_DYNAMIC,%rax
119	testq	%rax,%rax
120	jz	1f
121	movq	%rdx,%rdi		/* register rt_do_exit */
122	call	atexit
1231:
124	movq	$_fini,%rdi
125	call	atexit
126
127/*
128 * Calculate the location of the envp array by adding the size of
129 * the argv array to the start of the argv array.
130 */
131	movq	16(%rbp),%rax		/* argc */
132	movq	_environ, %rcx
133	testq	%rcx, %rcx		/* check if _environ==0 */
134	jne	1f
135	leaq	32(%rbp,%rax,8),%rcx	/* (A) */
136	movq	%rcx,_environ		/* copy to _environ */
1371:
138
139/*
140 * Force stack alignment - below here there must have been an even
141 * number of un-popped pushq instructions whenever a call is reached
142 */
143	andq	$-16,%rsp
144	pushq	%rdx
145	leaq	24(%rbp),%rdx		/* argv (B) */
146	movq	%rdx,___Argv
147	pushq	%rcx
148	pushq	%rdx
149	pushq	%rax
150	call	__fpstart
151	call	_init
152	popq	%rdi
153	popq	%rsi
154	popq	%rdx
155	popq	%rcx
156	call	main			/* main(argc,argv,envp) */
157	pushq	%rax
158	pushq	%rax
159	movq	%rax,%rdi		/* and call exit */
160	call	exit
161	popq	%rdi
162	popq	%rdi
163	call	_exit		/* if user redefined exit, call _exit */
164	hlt
165	.size	_start, .-_start
166
167/*
168 * The following is here in case any object module compiled with cc -p
169 *	was linked into this module.
170 */
171	.globl	_mcount
172	.section	.text
173	.align	8
174	.type	_mcount,@function
175_mcount:
176	ret
177	.size	_mcount, .-_mcount
178
179	.globl	__longdouble_used
180	.section	.data
181	.align	8
182	.type	__longdouble_used,@object
183	.size	__longdouble_used,4
184__longdouble_used:
185	.4byte	0
186