1/*	$OpenBSD: ldasm.S,v 1.31 2023/05/18 16:33:39 guenther Exp $	*/
2
3/*
4 * Copyright (c) 2002,2004 Dale Rahn
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 */
29
30
31#define DL_DATA_SIZE	(16*8)	 /* 16 * sizeof(ELF_Addr) */
32#define DL_LOFF_OFFSET	(7*8)	/* index 7 */
33#include <machine/asm.h>
34
35	.section .boot.text,"ax",@progbits
36	.align	16,0xcc
37	.globl	_dl_start
38	.type	_dl_start,@function
39_dl_start:
40	movq	%rsp, %r12		# save stack pointer for _rtld
41
42	subq	$8, %rsp		# align stack
43	andq	$~15, %rsp
44	addq	$8, %rsp
45
46	pushq	%rbx			# save ps_strings
47	subq	$DL_DATA_SIZE, %rsp	# allocate dl_data
48
49	leaq	_DYNAMIC(%rip),%rdx	# &_DYNAMIC
50	movq	%rsp, %rsi		# dl_data for dl_boot_bind
51	movq	%r12, %rdi		# load saved SP for dl_boot_bind
52	call	_dl_boot_bind@PLT	# _dl_boot_bind(sp,dl_data,dynamicp)
53
54	movq	%rsp, %rcx		# dl_data
55	movq	DL_LOFF_OFFSET(%rsp), %rdx		# loff from dl_data
56
57	movq	(%r12), %rdi
58	leaq	16(%r12,%rdi,8), %rsi	# envp
59	movq	%r12, %rdi
60	addq	$8,%rdi			# argv
61	call	_dl_boot@PLT		# _dl_boot(argv,envp,loff,dl_data)
62
63	addq	$DL_DATA_SIZE,%rsp	# return dl_data
64
65	leaq	_dl_dtors(%rip), %rdx	# %rdx = cleanup
66	movq	%r12, %rsp
67	jmp	*%rax
68END(_dl_start)
69
70_ENTRY(_dl_bind_start)
71	.cfi_startproc
72	.cfi_adjust_cfa_offset	16
73	endbr64
74	pushfq				# save registers
75	.cfi_adjust_cfa_offset	8
76	/*.cfi_offset	%rflags, -16 */
77	pushq	%rax
78	.cfi_adjust_cfa_offset	8
79	.cfi_offset	%rax, -24
80	pushq	%rcx
81	.cfi_adjust_cfa_offset	8
82	.cfi_offset	%rcx, -32
83	pushq	%rdx
84	.cfi_adjust_cfa_offset	8
85	.cfi_offset	%rdx, -40
86	pushq	%rsi
87	.cfi_adjust_cfa_offset	8
88	.cfi_offset	%rsi, -48
89	pushq	%rdi
90	.cfi_adjust_cfa_offset	8
91	.cfi_offset	%rdi, -56
92	pushq	%r8
93	.cfi_adjust_cfa_offset	8
94	.cfi_offset	%r8, -64
95	pushq	%r9
96	.cfi_adjust_cfa_offset	8
97	.cfi_offset	%r9, -72
98	pushq	%r10
99	.cfi_adjust_cfa_offset	8
100	.cfi_offset	%r10, -80
101	pushq	%r11
102	.cfi_adjust_cfa_offset	8
103	.cfi_offset	%r11, -88
104
105	movq	80(%rsp), %rdi		# Copy of reloff
106	movq	88(%rsp), %rsi		# Copy of obj
107	call	_dl_bind@PLT		# Call the binder
108	movq	%rax,88(%rsp)		# Store function to be called in obj
109
110	popq	%r11			# restore registers
111	.cfi_adjust_cfa_offset	-8
112	.cfi_restore	%r11
113	popq	%r10
114	.cfi_adjust_cfa_offset	-8
115	.cfi_restore	%r10
116	popq	%r9
117	.cfi_adjust_cfa_offset	-8
118	.cfi_restore	%r9
119	popq	%r8
120	.cfi_adjust_cfa_offset	-8
121	.cfi_restore	%r8
122	popq	%rdi
123	.cfi_adjust_cfa_offset	-8
124	.cfi_restore	%rdi
125	popq	%rsi
126	.cfi_adjust_cfa_offset	-8
127	.cfi_restore	%rsi
128	popq	%rdx
129	.cfi_adjust_cfa_offset	-8
130	.cfi_restore	%rdx
131	popq	%rcx
132	.cfi_adjust_cfa_offset	-8
133	.cfi_restore	%rcx
134	popq	%rax
135	.cfi_adjust_cfa_offset	-8
136	.cfi_restore	%rax
137	popfq
138	.cfi_adjust_cfa_offset	-8
139	/*.cfi_restore	%rflags */
140
141	leaq	8(%rsp),%rsp		# Discard reloff, do not change eflags
142	.cfi_adjust_cfa_offset	-8
143	ret
144	.cfi_endproc
145END(_dl_bind_start)
146