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