134192Sjdp/*-
234192Sjdp * Copyright 1996-1998 John D. Polstra.
334192Sjdp * All rights reserved.
434192Sjdp *
534192Sjdp * Redistribution and use in source and binary forms, with or without
634192Sjdp * modification, are permitted provided that the following conditions
734192Sjdp * are met:
834192Sjdp * 1. Redistributions of source code must retain the above copyright
934192Sjdp *    notice, this list of conditions and the following disclaimer.
1034192Sjdp * 2. Redistributions in binary form must reproduce the above copyright
1134192Sjdp *    notice, this list of conditions and the following disclaimer in the
1234192Sjdp *    documentation and/or other materials provided with the distribution.
1334192Sjdp *
1434192Sjdp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1534192Sjdp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1634192Sjdp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1734192Sjdp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1834192Sjdp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1934192Sjdp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2034192Sjdp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2134192Sjdp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2234192Sjdp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2334192Sjdp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2434192Sjdp *
2550476Speter * $FreeBSD$
2634192Sjdp */
2734192Sjdp
2834192Sjdp	.text
2934192Sjdp	.align	4
3034192Sjdp	.globl	.rtld_start
3134192Sjdp	.type	.rtld_start,@function
3234192Sjdp.rtld_start:
33115280Speter	xorq	%rbp,%rbp		# Clear frame pointer for good form
34127254Speter	subq	$24,%rsp		# A place to store exit procedure addr
35115280Speter	movq	%rdi,%r12
36115280Speter	movq	%rsp,%rsi		# save address of exit proc
37115280Speter	movq	%rsp,%rdx		# construct address of obj_main
38115280Speter	addq	$8,%rdx
3934192Sjdp	call	_rtld@PLT		# Call rtld(sp); returns entry point
40115280Speter	popq	%rsi			# Get exit procedure address
41115280Speter	movq	%r12,%rdi		# *ap
4234192Sjdp/*
43115280Speter * At this point, %rax contains the entry point of the main program, and
44115280Speter * %rdx contains a pointer to a termination function that should be
4534192Sjdp * registered with atexit().  (crt1.o registers it.)
4634192Sjdp */
4734192Sjdp.globl .rtld_goto_main
4834192Sjdp.rtld_goto_main:	# This symbol exists just to make debugging easier.
49115280Speter	jmp	*%rax			# Enter main program
5034192Sjdp
5134192Sjdp
5234192Sjdp/*
5334192Sjdp * Binder entry point.  Control is transferred to here by code in the PLT.
5434192Sjdp * On entry, there are two arguments on the stack.  In ascending address
5534192Sjdp * order, they are (1) "obj", a pointer to the calling object's Obj_Entry,
5634192Sjdp * and (2) "reloff", the byte offset of the appropriate relocation entry
5734192Sjdp * in the PLT relocation table.
5834192Sjdp *
59229461Seadler * We are careful to preserve all registers, even the caller-save
6034192Sjdp * registers.  That is because this code may be invoked by low-level
6134192Sjdp * assembly-language code that is not ABI-compliant.
62115280Speter *
63115280Speter * Stack map:
64127254Speter * reloff       0x60
65127254Speter * obj          0x58
66127254Speter * spare	0x50
67115280Speter * rflags       0x48
68115280Speter * rax          0x40
69115280Speter * rdx          0x38
70115280Speter * rcx          0x30
71115280Speter * rsi          0x28
72115280Speter * rdi          0x20
73115280Speter * r8           0x18
74115280Speter * r9           0x10
75115280Speter * r10          0x8
76115280Speter * r11          0x0
7734192Sjdp */
7834192Sjdp	.align	4
7934192Sjdp	.globl	_rtld_bind_start
8034192Sjdp	.type	_rtld_bind_start,@function
8134192Sjdp_rtld_bind_start:
82264719Skib	.cfi_startproc
83264719Skib	.cfi_adjust_cfa_offset	16
84127254Speter	subq	$8,%rsp
85264719Skib	.cfi_adjust_cfa_offset	8
86115280Speter	pushfq				# Save rflags
87264719Skib	.cfi_adjust_cfa_offset	8
88115280Speter	pushq	%rax			# Save %rax
89264719Skib	.cfi_adjust_cfa_offset 8
90264719Skib	.cfi_offset	%rax,-32
91115280Speter	pushq	%rdx			# Save %rdx
92264719Skib	.cfi_adjust_cfa_offset 8
93264719Skib	.cfi_offset	%rdx,-40
94115280Speter	pushq	%rcx			# Save %rcx
95264719Skib	.cfi_adjust_cfa_offset 8
96264719Skib	.cfi_offset	%rcx,-48
97115280Speter	pushq	%rsi			# Save %rsi
98264719Skib	.cfi_adjust_cfa_offset 8
99264719Skib	.cfi_offset	%rsi,-56
100115280Speter	pushq	%rdi			# Save %rdi
101264719Skib	.cfi_adjust_cfa_offset 8
102264719Skib	.cfi_offset	%rdi,-64
103115280Speter	pushq	%r8			# Save %r8
104264719Skib	.cfi_adjust_cfa_offset 8
105264719Skib	.cfi_offset	%r8,-72
106115280Speter	pushq	%r9			# Save %r9
107264719Skib	.cfi_adjust_cfa_offset 8
108264719Skib	.cfi_offset	%r9,-80
109115280Speter	pushq	%r10			# Save %r10
110264719Skib	.cfi_adjust_cfa_offset 8
111264719Skib	.cfi_offset	%r10,-88
112115280Speter	pushq	%r11			# Save %r11
113264719Skib	.cfi_adjust_cfa_offset 8
114264719Skib	.cfi_offset	%r11,-96
11534192Sjdp
116127254Speter	movq	0x58(%rsp),%rdi		# Fetch obj argument
117127254Speter	movq	0x60(%rsp),%rsi		# Fetch reloff argument
118115280Speter	leaq	(%rsi,%rsi,2),%rsi	# multiply by 3
119115280Speter	leaq	(,%rsi,8),%rsi		# now 8, for 24 (sizeof Elf_Rela)
120115280Speter
12134192Sjdp	call	_rtld_bind@PLT		# Transfer control to the binder
122115280Speter	/* Now %rax contains the entry point of the function being called. */
12334192Sjdp
124127254Speter	movq	%rax,0x60(%rsp)		# Store target over reloff argument
125115280Speter	popq	%r11			# Restore %r11
126264719Skib	.cfi_adjust_cfa_offset -8
127264719Skib	.cfi_restore %r11
128115280Speter	popq	%r10			# Restore %r10
129264719Skib	.cfi_adjust_cfa_offset -8
130264719Skib	.cfi_restore %r10
131115280Speter	popq	%r9			# Restore %r9
132264719Skib	.cfi_adjust_cfa_offset -8
133264719Skib	.cfi_restore %r9
134115280Speter	popq	%r8			# Restore %r8
135264719Skib	.cfi_adjust_cfa_offset -8
136264719Skib	.cfi_restore %r8
137115280Speter	popq	%rdi			# Restore %rdi
138264719Skib	.cfi_adjust_cfa_offset -8
139264719Skib	.cfi_restore %rdi
140115280Speter	popq	%rsi			# Restore %rsi
141264719Skib	.cfi_adjust_cfa_offset -8
142264719Skib	.cfi_restore %rsi
143115280Speter	popq	%rcx			# Restore %rcx
144264719Skib	.cfi_adjust_cfa_offset -8
145264719Skib	.cfi_restore %rcx
146115280Speter	popq	%rdx			# Restore %rdx
147264719Skib	.cfi_adjust_cfa_offset -8
148264719Skib	.cfi_restore %rdx
149115280Speter	popq	%rax			# Restore %rax
150264719Skib	.cfi_adjust_cfa_offset -8
151264719Skib	.cfi_restore %rax
152115280Speter	popfq				# Restore rflags
153264719Skib	.cfi_adjust_cfa_offset -8
154127254Speter	leaq	16(%rsp),%rsp		# Discard spare, obj, do not change rflags
15534192Sjdp	ret				# "Return" to target address
156264719Skib	.cfi_endproc
157264719Skib	.size	_rtld_bind_start, . - _rtld_bind_start
158217103Skib
159217103Skib	.section .note.GNU-stack,"",%progbits
160