xref: /freebsd/libexec/rtld-elf/amd64/rtld_start.S (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1/*-
2 * Copyright 1996-1998 John D. Polstra.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * $FreeBSD$
26 */
27
28	.text
29	.align	4
30	.globl	.rtld_start
31	.type	.rtld_start,@function
32.rtld_start:
33	xorq	%rbp,%rbp		# Clear frame pointer for good form
34	subq	$24,%rsp		# A place to store exit procedure addr
35	movq	%rdi,%r12
36	movq	%rsp,%rsi		# save address of exit proc
37	movq	%rsp,%rdx		# construct address of obj_main
38	addq	$8,%rdx
39	call	_rtld			# Call rtld(sp); returns entry point
40	popq	%rsi			# Get exit procedure address
41	movq	%r12,%rdi		# *ap
42/*
43 * At this point, %rax contains the entry point of the main program, and
44 * %rdx contains a pointer to a termination function that should be
45 * registered with atexit().  (crt1.o registers it.)
46 */
47.globl .rtld_goto_main
48.rtld_goto_main:	# This symbol exists just to make debugging easier.
49	jmp	*%rax			# Enter main program
50
51
52/*
53 * Binder entry point.  Control is transferred to here by code in the PLT.
54 * On entry, there are two arguments on the stack.  In ascending address
55 * order, they are (1) "obj", a pointer to the calling object's Obj_Entry,
56 * and (2) "reloff", the byte offset of the appropriate relocation entry
57 * in the PLT relocation table.
58 *
59 * We are careful to preserve all registers, even the caller-save
60 * registers.  That is because this code may be invoked by low-level
61 * assembly-language code that is not ABI-compliant.
62 *
63 * Stack map:
64 * reloff       0x60
65 * obj          0x58
66 * spare	0x50
67 * rflags       0x48
68 * rax          0x40
69 * rdx          0x38
70 * rcx          0x30
71 * rsi          0x28
72 * rdi          0x20
73 * r8           0x18
74 * r9           0x10
75 * r10          0x8
76 * r11          0x0
77 */
78	.align	4
79	.globl	_rtld_bind_start
80	.type	_rtld_bind_start,@function
81_rtld_bind_start:
82	.cfi_startproc
83	.cfi_adjust_cfa_offset	16
84	subq	$8,%rsp
85	.cfi_adjust_cfa_offset	8
86	pushfq				# Save rflags
87	.cfi_adjust_cfa_offset	8
88	pushq	%rax			# Save %rax
89	.cfi_adjust_cfa_offset 8
90	.cfi_offset	%rax,-32
91	pushq	%rdx			# Save %rdx
92	.cfi_adjust_cfa_offset 8
93	.cfi_offset	%rdx,-40
94	pushq	%rcx			# Save %rcx
95	.cfi_adjust_cfa_offset 8
96	.cfi_offset	%rcx,-48
97	pushq	%rsi			# Save %rsi
98	.cfi_adjust_cfa_offset 8
99	.cfi_offset	%rsi,-56
100	pushq	%rdi			# Save %rdi
101	.cfi_adjust_cfa_offset 8
102	.cfi_offset	%rdi,-64
103	pushq	%r8			# Save %r8
104	.cfi_adjust_cfa_offset 8
105	.cfi_offset	%r8,-72
106	pushq	%r9			# Save %r9
107	.cfi_adjust_cfa_offset 8
108	.cfi_offset	%r9,-80
109	pushq	%r10			# Save %r10
110	.cfi_adjust_cfa_offset 8
111	.cfi_offset	%r10,-88
112	pushq	%r11			# Save %r11
113	.cfi_adjust_cfa_offset 8
114	.cfi_offset	%r11,-96
115
116	movq	0x58(%rsp),%rdi		# Fetch obj argument
117	movq	0x60(%rsp),%rsi		# Fetch reloff argument
118	leaq	(%rsi,%rsi,2),%rsi	# multiply by 3
119	leaq	(,%rsi,8),%rsi		# now 8, for 24 (sizeof Elf_Rela)
120
121	call	_rtld_bind		# Transfer control to the binder
122	/* Now %rax contains the entry point of the function being called. */
123
124	movq	%rax,0x60(%rsp)		# Store target over reloff argument
125	popq	%r11			# Restore %r11
126	.cfi_adjust_cfa_offset -8
127	.cfi_restore %r11
128	popq	%r10			# Restore %r10
129	.cfi_adjust_cfa_offset -8
130	.cfi_restore %r10
131	popq	%r9			# Restore %r9
132	.cfi_adjust_cfa_offset -8
133	.cfi_restore %r9
134	popq	%r8			# Restore %r8
135	.cfi_adjust_cfa_offset -8
136	.cfi_restore %r8
137	popq	%rdi			# Restore %rdi
138	.cfi_adjust_cfa_offset -8
139	.cfi_restore %rdi
140	popq	%rsi			# Restore %rsi
141	.cfi_adjust_cfa_offset -8
142	.cfi_restore %rsi
143	popq	%rcx			# Restore %rcx
144	.cfi_adjust_cfa_offset -8
145	.cfi_restore %rcx
146	popq	%rdx			# Restore %rdx
147	.cfi_adjust_cfa_offset -8
148	.cfi_restore %rdx
149	popq	%rax			# Restore %rax
150	.cfi_adjust_cfa_offset -8
151	.cfi_restore %rax
152	popfq				# Restore rflags
153	.cfi_adjust_cfa_offset -8
154	leaq	16(%rsp),%rsp		# Discard spare, obj, do not change rflags
155	ret				# "Return" to target address
156	.cfi_endproc
157	.size	_rtld_bind_start, . - _rtld_bind_start
158
159	.align	4
160	.globl	rtld_dynamic_addr
161	.type	rtld_dynamic_addr,@function
162rtld_dynamic_addr:
163	.cfi_startproc
164	.weak _DYNAMIC
165	.hidden _DYNAMIC
166	lea	_DYNAMIC(%rip),%rax
167	ret
168	.cfi_endproc
169	.size	rtld_dynamic_addr, . - rtld_dynamic_addr
170
171	.section .note.GNU-stack,"",%progbits
172