xref: /titanic_50/usr/src/lib/crt/amd64/mach-crt1.s (revision cc31cba96d9862a1075a8ec47d6149e04c7ad62d)
1*cc31cba9SRichard Lowe/*
2*cc31cba9SRichard Lowe * CDDL HEADER START
3*cc31cba9SRichard Lowe *
4*cc31cba9SRichard Lowe * The contents of this file are subject to the terms of the
5*cc31cba9SRichard Lowe * Common Development and Distribution License (the "License").
6*cc31cba9SRichard Lowe * You may not use this file except in compliance with the License.
7*cc31cba9SRichard Lowe *
8*cc31cba9SRichard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*cc31cba9SRichard Lowe * or http://www.opensolaris.org/os/licensing.
10*cc31cba9SRichard Lowe * See the License for the specific language governing permissions
11*cc31cba9SRichard Lowe * and limitations under the License.
12*cc31cba9SRichard Lowe *
13*cc31cba9SRichard Lowe * When distributing Covered Code, include this CDDL HEADER in each
14*cc31cba9SRichard Lowe * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*cc31cba9SRichard Lowe * If applicable, add the following below this CDDL HEADER, with the
16*cc31cba9SRichard Lowe * fields enclosed by brackets "[]" replaced with your own identifying
17*cc31cba9SRichard Lowe * information: Portions Copyright [yyyy] [name of copyright owner]
18*cc31cba9SRichard Lowe *
19*cc31cba9SRichard Lowe * CDDL HEADER END
20*cc31cba9SRichard Lowe */
21*cc31cba9SRichard Lowe
22*cc31cba9SRichard Lowe/*
23*cc31cba9SRichard Lowe * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*cc31cba9SRichard Lowe * Use is subject to license terms.
25*cc31cba9SRichard Lowe */
26*cc31cba9SRichard Lowe
27*cc31cba9SRichard Lowe#include <sys/asm_linkage.h>
28*cc31cba9SRichard Lowe
29*cc31cba9SRichard Lowe	.file	"mach-crt1.s"
30*cc31cba9SRichard Lowe
31*cc31cba9SRichard Lowe	.global	_start_crt
32*cc31cba9SRichard Lowe
33*cc31cba9SRichard Lowe/*
34*cc31cba9SRichard Lowe *   The SVR4/amd64 ABI (pages 3-29) says that when the entry
35*cc31cba9SRichard Lowe *   point runs registers' %rbp, %rsp, %rdx values are specified
36*cc31cba9SRichard Lowe *   the following:
37*cc31cba9SRichard Lowe *
38*cc31cba9SRichard Lowe *	%rbp The content of this register is unspecified at
39*cc31cba9SRichard Lowe *		process initialization time, but the user code should mark
40*cc31cba9SRichard Lowe *		the deepest stack frame by setting the frame pointer to zero.
41*cc31cba9SRichard Lowe *		No other frame's %ebp should have a zero value.
42*cc31cba9SRichard Lowe *
43*cc31cba9SRichard Lowe *	%rsp Performing its usual job, the stack pointer holds the address
44*cc31cba9SRichard Lowe *	of the bottom of the stack, which is guaranteed to be
45*cc31cba9SRichard Lowe *	quadword aligned.
46*cc31cba9SRichard Lowe *
47*cc31cba9SRichard Lowe *		The stack contains the arguments and environment:
48*cc31cba9SRichard Lowe *	  ...
49*cc31cba9SRichard Lowe *	  envp[0]		(16+(8*argc))(%rsp)
50*cc31cba9SRichard Lowe *	  NULL			(8+(8*argc))(%rsp)
51*cc31cba9SRichard Lowe *	  ...
52*cc31cba9SRichard Lowe *	  argv[0]		8(%rsp)
53*cc31cba9SRichard Lowe *	  argc			0(%rsp)
54*cc31cba9SRichard Lowe *
55*cc31cba9SRichard Lowe *	%rdx In a conforming program, this register contains a function
56*cc31cba9SRichard Lowe *		pointer that the application should register with atexit(BA_OS).
57*cc31cba9SRichard Lowe *		This function is used for shared object termination code
58*cc31cba9SRichard Lowe *		[see Dynamic Linking in Chapter 5 of the System V ABI].
59*cc31cba9SRichard Lowe *
60*cc31cba9SRichard Lowe */
61*cc31cba9SRichard Lowe
62*cc31cba9SRichard LoweENTRY_NP(_start)
63*cc31cba9SRichard Lowe/*
64*cc31cba9SRichard Lowe * Allocate a NULL return address and a NULL previous %rbp as if
65*cc31cba9SRichard Lowe * there was a genuine call to _start.
66*cc31cba9SRichard Lowe */
67*cc31cba9SRichard Lowe	pushq	$0
68*cc31cba9SRichard Lowe	pushq	$0
69*cc31cba9SRichard Lowe	movq	%rsp,%rbp		/* The first stack frame */
70*cc31cba9SRichard Lowe
71*cc31cba9SRichard Lowe/*
72*cc31cba9SRichard Lowe * The stack now is
73*cc31cba9SRichard Lowe *
74*cc31cba9SRichard Lowe *	  envp[0]		(32+(8*argc))(%rsp)	 - (A)
75*cc31cba9SRichard Lowe *	  NULL			(24+(8*argc))(%rsp)
76*cc31cba9SRichard Lowe *	  ...
77*cc31cba9SRichard Lowe *	  argv[0]		24(%rbp)		 - (B)
78*cc31cba9SRichard Lowe *	  argc			16(%rbp)
79*cc31cba9SRichard Lowe *	  0			8(%rbp)
80*cc31cba9SRichard Lowe *	  0			0(%rbp)
81*cc31cba9SRichard Lowe */
82*cc31cba9SRichard Lowe
83*cc31cba9SRichard Lowe	andq	$-16,%rsp		/* align the stack */
84*cc31cba9SRichard Lowe	movq	16(%rbp),%rdi		/* argc */
85*cc31cba9SRichard Lowe	leaq	24(%rbp),%rsi		/* argv */
86*cc31cba9SRichard Lowe	/* NB: rt_do_exit, if applicable, is already in %rdx */
87*cc31cba9SRichard Lowe	call	_start_crt
88*cc31cba9SRichard Lowe	hlt
89*cc31cba9SRichard LoweSET_SIZE(_start)
90*cc31cba9SRichard Lowe
91*cc31cba9SRichard Lowe/*
92*cc31cba9SRichard Lowe * The following is here in case any object module compiled with cc -p
93*cc31cba9SRichard Lowe *	was linked into this module.
94*cc31cba9SRichard Lowe */
95*cc31cba9SRichard LoweENTRY_NP(_mcount)
96*cc31cba9SRichard Lowe	.weak	_mcount
97*cc31cba9SRichard Lowe	ret
98*cc31cba9SRichard LoweSET_SIZE(_mcount)
99