xref: /titanic_51/usr/src/cmd/sgs/rtld/amd64/boot.s (revision b453864f3587ccc3324d7a3b0438a1e542dcfde7)
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 (c) 1988 AT&T
24 *	  All Rights Reserved
25 *
26 *
27 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28 * Use is subject to license terms.
29 */
30#pragma ident	"%Z%%M%	%I%	%E% SMI"
31
32/*
33 * Bootstrap routine for run-time linker.
34 * We get control from exec which has loaded our text and
35 * data into the process' address space and created the process
36 * stack.
37 *
38 * On entry, the process stack looks like this:
39 *
40 *	#			# <- %rsp
41 *	#_______________________#  high addresses
42 *	#	strings		#
43 *	#_______________________#
44 *	#	0 word		#
45 *	#_______________________#
46 *	#	Auxiliary	#
47 *	#	entries		#
48 *	#	...		#
49 *	#	(size varies)	#
50 *	#_______________________#
51 *	#	0 word		#
52 *	#_______________________#
53 *	#	Environment	#
54 *	#	pointers	#
55 *	#	...		#
56 *	#	(one word each)	#
57 *	#_______________________#
58 *	#	0 word		#
59 *	#_______________________#
60 *	#	Argument	# low addresses
61 *	#	pointers	#
62 *	#	Argc words	#
63 *	#_______________________#
64 *	#	argc		#
65 *	#_______________________# <- %rbp
66 *
67 *
68 * We must calculate the address at which ld.so was loaded,
69 * find the addr of the dynamic section of ld.so, of argv[0], and  of
70 * the process' environment pointers - and pass the thing to _setup
71 * to handle.  We then call _rtld - on return we jump to the entry
72 * point for the a.out.
73 */
74
75#if	defined(lint)
76
77extern	unsigned long	_setup();
78extern	void		atexit_fini();
79void
80main()
81{
82	(void) _setup();
83	atexit_fini();
84}
85
86#else
87
88#include	<link.h>
89
90	.file	"boot.s"
91	.text
92	.globl	_rt_boot
93	.globl	_setup
94	.globl	_GLOBAL_OFFSET_TABLE_
95	.type	_rt_boot,@function
96	.align	4
97
98_rt_alias:
99	/ in case we were invoked from libc.so
100	jmp	.get_got
101_rt_boot:
102	/ save for referencing args
103	movq	%rsp,%rbp
104	/ make room for a max sized boot vector
105	subq	$EB_MAX_SIZE64,%rsp
106	/ use esi as a pointer to &eb[0]
107	movq	%rsp,%rsi
108	/ set up tag for argv
109	movq	$EB_ARGV,0(%rsi)
110	/ get address of argv
111	leaq	8(%rbp),%rax
112	/ put after tag
113	movq	%rax,8(%rsi)
114	/ set up tag for envp
115	movq	$EB_ENVP,16(%rsi)
116	/ get # of args
117	movq	(%rbp),%rax
118	/ one for the zero & one for argc
119	addq	$2,%rax
120	/ now points past args & @ envp
121	leaq	(%rbp,%rax,8),%rdi
122	/ set envp
123	movq	%rdi,24(%rsi)
124	/ next
125.L0:	addq	$8,%rdi
126	/ search for 0 at end of env
127	cmpq	$0,-8(%rdi)
128	jne	.L0
129	/ set up tag for auxv
130	movq	$EB_AUXV,32(%rsi)
131	/ point to auxv
132	movq	%rdi,40(%rsi)
133	/ set up NULL tag
134	movq	$EB_NULL,48(%rsi)
135
136	/ arg1 - address of &eb[0]
137	movq	%rsi, %rdi
138.get_got:
139	leaq	_GLOBAL_OFFSET_TABLE_(%rip), %rbx
140	/ addq	$_GLOBAL_OFFSET_TABLE_, %rbx
141	movq	%rbx,%r9
142	// addq	$[.L2-.L1], %rbx
143	movq	%rbx,%r10
144
145	movq	(%rbx),%rsi
146
147	/ _setup(&eb[0], _DYNAMIC)
148	call	_setup@PLT
149	/ release stack frame
150	movq	%rbp,%rsp
151
152	movq	atexit_fini@GOTPCREL(%rip), %rdx
153	/ transfer control to a.out
154	jmp	*%rax
155	.size	_rt_boot,.-_rt_boot
156#endif
157