xref: /freebsd/libexec/rtld-elf/amd64/rtld_start.S (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
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	.cfi_startproc
34	.cfi_undefined %rip
35	xorq	%rbp,%rbp		# Clear frame pointer for good form
36	subq	$24,%rsp		# A place to store exit procedure addr
37	.cfi_def_cfa_offset 32
38	movq	%rdi,%r12
39	movq	%rsp,%rsi		# save address of exit proc
40	movq	%rsp,%rdx		# construct address of obj_main
41	addq	$8,%rdx
42	call	_rtld			# Call rtld(sp); returns entry point
43	popq	%rsi			# Get exit procedure address
44	.cfi_def_cfa_offset 24
45	movq	%r12,%rdi		# *ap
46/*
47 * At this point, %rax contains the entry point of the main program, and
48 * %rdx contains a pointer to a termination function that should be
49 * registered with atexit().  (crt1.o registers it.)
50 */
51.globl .rtld_goto_main
52.rtld_goto_main:	# This symbol exists just to make debugging easier.
53	jmp	*%rax			# Enter main program
54	.cfi_endproc
55
56
57/*
58 * Binder entry point.  Control is transferred to here by code in the PLT.
59 * On entry, there are two arguments on the stack.  In ascending address
60 * order, they are (1) "obj", a pointer to the calling object's Obj_Entry,
61 * and (2) "reloff", the byte offset of the appropriate relocation entry
62 * in the PLT relocation table.
63 *
64 * We are careful to preserve all registers, even the caller-save
65 * registers.  That is because this code may be invoked by low-level
66 * assembly-language code that is not ABI-compliant.
67 *
68 * Stack map:
69 * reloff       0x60
70 * obj          0x58
71 * spare	0x50
72 * rflags       0x48
73 * rax          0x40
74 * rdx          0x38
75 * rcx          0x30
76 * rsi          0x28
77 * rdi          0x20
78 * r8           0x18
79 * r9           0x10
80 * r10          0x8
81 * r11          0x0
82 */
83	.align	4
84	.globl	_rtld_bind_start
85	.type	_rtld_bind_start,@function
86_rtld_bind_start:
87	.cfi_startproc
88	.cfi_adjust_cfa_offset	16
89	subq	$8,%rsp
90	.cfi_adjust_cfa_offset	8
91	pushfq				# Save rflags
92	.cfi_adjust_cfa_offset	8
93	pushq	%rax			# Save %rax
94	.cfi_adjust_cfa_offset 8
95	.cfi_offset	%rax,-32
96	pushq	%rdx			# Save %rdx
97	.cfi_adjust_cfa_offset 8
98	.cfi_offset	%rdx,-40
99	pushq	%rcx			# Save %rcx
100	.cfi_adjust_cfa_offset 8
101	.cfi_offset	%rcx,-48
102	pushq	%rsi			# Save %rsi
103	.cfi_adjust_cfa_offset 8
104	.cfi_offset	%rsi,-56
105	pushq	%rdi			# Save %rdi
106	.cfi_adjust_cfa_offset 8
107	.cfi_offset	%rdi,-64
108	pushq	%r8			# Save %r8
109	.cfi_adjust_cfa_offset 8
110	.cfi_offset	%r8,-72
111	pushq	%r9			# Save %r9
112	.cfi_adjust_cfa_offset 8
113	.cfi_offset	%r9,-80
114	pushq	%r10			# Save %r10
115	.cfi_adjust_cfa_offset 8
116	.cfi_offset	%r10,-88
117	pushq	%r11			# Save %r11
118	.cfi_adjust_cfa_offset 8
119	.cfi_offset	%r11,-96
120
121	movq	0x58(%rsp),%rdi		# Fetch obj argument
122	movq	0x60(%rsp),%rsi		# Fetch reloff argument
123	leaq	(%rsi,%rsi,2),%rsi	# multiply by 3
124	leaq	(,%rsi,8),%rsi		# now 8, for 24 (sizeof Elf_Rela)
125
126	call	_rtld_bind		# Transfer control to the binder
127	/* Now %rax contains the entry point of the function being called. */
128
129	movq	%rax,0x60(%rsp)		# Store target over reloff argument
130	popq	%r11			# Restore %r11
131	.cfi_adjust_cfa_offset -8
132	.cfi_restore %r11
133	popq	%r10			# Restore %r10
134	.cfi_adjust_cfa_offset -8
135	.cfi_restore %r10
136	popq	%r9			# Restore %r9
137	.cfi_adjust_cfa_offset -8
138	.cfi_restore %r9
139	popq	%r8			# Restore %r8
140	.cfi_adjust_cfa_offset -8
141	.cfi_restore %r8
142	popq	%rdi			# Restore %rdi
143	.cfi_adjust_cfa_offset -8
144	.cfi_restore %rdi
145	popq	%rsi			# Restore %rsi
146	.cfi_adjust_cfa_offset -8
147	.cfi_restore %rsi
148	popq	%rcx			# Restore %rcx
149	.cfi_adjust_cfa_offset -8
150	.cfi_restore %rcx
151	popq	%rdx			# Restore %rdx
152	.cfi_adjust_cfa_offset -8
153	.cfi_restore %rdx
154	popq	%rax			# Restore %rax
155	.cfi_adjust_cfa_offset -8
156	.cfi_restore %rax
157	popfq				# Restore rflags
158	.cfi_adjust_cfa_offset -8
159	leaq	16(%rsp),%rsp		# Discard spare, obj, do not change rflags
160	ret				# "Return" to target address
161	.cfi_endproc
162	.size	_rtld_bind_start, . - _rtld_bind_start
163
164	.align	4
165	.globl	rtld_dynamic_addr
166	.type	rtld_dynamic_addr,@function
167rtld_dynamic_addr:
168	.cfi_startproc
169	.weak _DYNAMIC
170	.hidden _DYNAMIC
171	lea	_DYNAMIC(%rip),%rax
172	ret
173	.cfi_endproc
174	.size	rtld_dynamic_addr, . - rtld_dynamic_addr
175
176	.section .note.GNU-stack,"",%progbits
177