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