xref: /titanic_41/usr/src/cmd/sgs/rtld/amd64/boot.s (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate/*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate/*
23*7c478bd9Sstevel@tonic-gate *	Copyright (c) 1988 AT&T
24*7c478bd9Sstevel@tonic-gate *	  All Rights Reserved
25*7c478bd9Sstevel@tonic-gate *
26*7c478bd9Sstevel@tonic-gate *
27*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate */
30*7c478bd9Sstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate
32*7c478bd9Sstevel@tonic-gate/*
33*7c478bd9Sstevel@tonic-gate * Bootstrap routine for run-time linker.
34*7c478bd9Sstevel@tonic-gate * We get control from exec which has loaded our text and
35*7c478bd9Sstevel@tonic-gate * data into the process' address space and created the process
36*7c478bd9Sstevel@tonic-gate * stack.
37*7c478bd9Sstevel@tonic-gate *
38*7c478bd9Sstevel@tonic-gate * On entry, the process stack looks like this:
39*7c478bd9Sstevel@tonic-gate *
40*7c478bd9Sstevel@tonic-gate *	#			# <- %rsp
41*7c478bd9Sstevel@tonic-gate *	#_______________________#  high addresses
42*7c478bd9Sstevel@tonic-gate *	#	strings		#
43*7c478bd9Sstevel@tonic-gate *	#_______________________#
44*7c478bd9Sstevel@tonic-gate *	#	0 word		#
45*7c478bd9Sstevel@tonic-gate *	#_______________________#
46*7c478bd9Sstevel@tonic-gate *	#	Auxiliary	#
47*7c478bd9Sstevel@tonic-gate *	#	entries		#
48*7c478bd9Sstevel@tonic-gate *	#	...		#
49*7c478bd9Sstevel@tonic-gate *	#	(size varies)	#
50*7c478bd9Sstevel@tonic-gate *	#_______________________#
51*7c478bd9Sstevel@tonic-gate *	#	0 word		#
52*7c478bd9Sstevel@tonic-gate *	#_______________________#
53*7c478bd9Sstevel@tonic-gate *	#	Environment	#
54*7c478bd9Sstevel@tonic-gate *	#	pointers	#
55*7c478bd9Sstevel@tonic-gate *	#	...		#
56*7c478bd9Sstevel@tonic-gate *	#	(one word each)	#
57*7c478bd9Sstevel@tonic-gate *	#_______________________#
58*7c478bd9Sstevel@tonic-gate *	#	0 word		#
59*7c478bd9Sstevel@tonic-gate *	#_______________________#
60*7c478bd9Sstevel@tonic-gate *	#	Argument	# low addresses
61*7c478bd9Sstevel@tonic-gate *	#	pointers	#
62*7c478bd9Sstevel@tonic-gate *	#	Argc words	#
63*7c478bd9Sstevel@tonic-gate *	#_______________________#
64*7c478bd9Sstevel@tonic-gate *	#	argc		#
65*7c478bd9Sstevel@tonic-gate *	#_______________________# <- %rbp
66*7c478bd9Sstevel@tonic-gate *
67*7c478bd9Sstevel@tonic-gate *
68*7c478bd9Sstevel@tonic-gate * We must calculate the address at which ld.so was loaded,
69*7c478bd9Sstevel@tonic-gate * find the addr of the dynamic section of ld.so, of argv[0], and  of
70*7c478bd9Sstevel@tonic-gate * the process' environment pointers - and pass the thing to _setup
71*7c478bd9Sstevel@tonic-gate * to handle.  We then call _rtld - on return we jump to the entry
72*7c478bd9Sstevel@tonic-gate * point for the a.out.
73*7c478bd9Sstevel@tonic-gate */
74*7c478bd9Sstevel@tonic-gate
75*7c478bd9Sstevel@tonic-gate#if	defined(lint)
76*7c478bd9Sstevel@tonic-gate
77*7c478bd9Sstevel@tonic-gateextern	unsigned long	_setup();
78*7c478bd9Sstevel@tonic-gateextern	void		atexit_fini();
79*7c478bd9Sstevel@tonic-gatevoid
80*7c478bd9Sstevel@tonic-gatemain()
81*7c478bd9Sstevel@tonic-gate{
82*7c478bd9Sstevel@tonic-gate	(void) _setup();
83*7c478bd9Sstevel@tonic-gate	atexit_fini();
84*7c478bd9Sstevel@tonic-gate}
85*7c478bd9Sstevel@tonic-gate
86*7c478bd9Sstevel@tonic-gate#else
87*7c478bd9Sstevel@tonic-gate
88*7c478bd9Sstevel@tonic-gate#include	<link.h>
89*7c478bd9Sstevel@tonic-gate
90*7c478bd9Sstevel@tonic-gate	.file	"boot.s"
91*7c478bd9Sstevel@tonic-gate	.text
92*7c478bd9Sstevel@tonic-gate	.globl	_rt_boot
93*7c478bd9Sstevel@tonic-gate	.globl	_setup
94*7c478bd9Sstevel@tonic-gate	.globl	_GLOBAL_OFFSET_TABLE_
95*7c478bd9Sstevel@tonic-gate	.type	_rt_boot,@function
96*7c478bd9Sstevel@tonic-gate	.align	4
97*7c478bd9Sstevel@tonic-gate
98*7c478bd9Sstevel@tonic-gate_rt_alias:
99*7c478bd9Sstevel@tonic-gate	/ in case we were invoked from libc.so
100*7c478bd9Sstevel@tonic-gate	jmp	.get_got
101*7c478bd9Sstevel@tonic-gate_rt_boot:
102*7c478bd9Sstevel@tonic-gate	/ save for referencing args
103*7c478bd9Sstevel@tonic-gate	movq	%rsp,%rbp
104*7c478bd9Sstevel@tonic-gate	/ make room for a max sized boot vector
105*7c478bd9Sstevel@tonic-gate	subq	$EB_MAX_SIZE64,%rsp
106*7c478bd9Sstevel@tonic-gate	/ use esi as a pointer to &eb[0]
107*7c478bd9Sstevel@tonic-gate	movq	%rsp,%rsi
108*7c478bd9Sstevel@tonic-gate	/ set up tag for argv
109*7c478bd9Sstevel@tonic-gate	movq	$EB_ARGV,0(%rsi)
110*7c478bd9Sstevel@tonic-gate	/ get address of argv
111*7c478bd9Sstevel@tonic-gate	leaq	8(%rbp),%rax
112*7c478bd9Sstevel@tonic-gate	/ put after tag
113*7c478bd9Sstevel@tonic-gate	movq	%rax,8(%rsi)
114*7c478bd9Sstevel@tonic-gate	/ set up tag for envp
115*7c478bd9Sstevel@tonic-gate	movq	$EB_ENVP,16(%rsi)
116*7c478bd9Sstevel@tonic-gate	/ get # of args
117*7c478bd9Sstevel@tonic-gate	movq	(%rbp),%rax
118*7c478bd9Sstevel@tonic-gate	/ one for the zero & one for argc
119*7c478bd9Sstevel@tonic-gate	addq	$2,%rax
120*7c478bd9Sstevel@tonic-gate	/ now points past args & @ envp
121*7c478bd9Sstevel@tonic-gate	leaq	(%rbp,%rax,8),%rdi
122*7c478bd9Sstevel@tonic-gate	/ set envp
123*7c478bd9Sstevel@tonic-gate	movq	%rdi,24(%rsi)
124*7c478bd9Sstevel@tonic-gate	/ next
125*7c478bd9Sstevel@tonic-gate.L0:	addq	$8,%rdi
126*7c478bd9Sstevel@tonic-gate	/ search for 0 at end of env
127*7c478bd9Sstevel@tonic-gate	cmpq	$0,-8(%rdi)
128*7c478bd9Sstevel@tonic-gate	jne	.L0
129*7c478bd9Sstevel@tonic-gate	/ set up tag for auxv
130*7c478bd9Sstevel@tonic-gate	movq	$EB_AUXV,32(%rsi)
131*7c478bd9Sstevel@tonic-gate	/ point to auxv
132*7c478bd9Sstevel@tonic-gate	movq	%rdi,40(%rsi)
133*7c478bd9Sstevel@tonic-gate	/ set up NULL tag
134*7c478bd9Sstevel@tonic-gate	movq	$EB_NULL,48(%rsi)
135*7c478bd9Sstevel@tonic-gate
136*7c478bd9Sstevel@tonic-gate	/ arg1 - address of &eb[0]
137*7c478bd9Sstevel@tonic-gate	movq	%rsi, %rdi
138*7c478bd9Sstevel@tonic-gate.get_got:
139*7c478bd9Sstevel@tonic-gate	leaq	_GLOBAL_OFFSET_TABLE_(%rip), %rbx
140*7c478bd9Sstevel@tonic-gate	/ addq	$_GLOBAL_OFFSET_TABLE_, %rbx
141*7c478bd9Sstevel@tonic-gate	movq	%rbx,%r9
142*7c478bd9Sstevel@tonic-gate	// addq	$[.L2-.L1], %rbx
143*7c478bd9Sstevel@tonic-gate	movq	%rbx,%r10
144*7c478bd9Sstevel@tonic-gate
145*7c478bd9Sstevel@tonic-gate	movq	(%rbx),%rsi
146*7c478bd9Sstevel@tonic-gate
147*7c478bd9Sstevel@tonic-gate	/ _setup(&eb[0], _DYNAMIC)
148*7c478bd9Sstevel@tonic-gate	call	_setup@PLT
149*7c478bd9Sstevel@tonic-gate	/ release stack frame
150*7c478bd9Sstevel@tonic-gate	movq	%rbp,%rsp
151*7c478bd9Sstevel@tonic-gate
152*7c478bd9Sstevel@tonic-gate	movq	atexit_fini@GOTPCREL(%rip), %rdx
153*7c478bd9Sstevel@tonic-gate	/ transfer control to a.out
154*7c478bd9Sstevel@tonic-gate	jmp	*%rax
155*7c478bd9Sstevel@tonic-gate	.size	_rt_boot,.-_rt_boot
156*7c478bd9Sstevel@tonic-gate#endif
157