1/*	$OpenBSD: ldasm.S,v 1.11 2023/07/08 14:09:43 jasper Exp $ */
2
3/*
4 * Copyright (c) 2016 Dale Rahn
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 */
28
29#define DL_DATA_SIZE	(16 * 8)	/* XXX */
30#include <machine/asm.h>
31#include <sys/syscall.h>
32
33	.section .boot.text,"ax",@progbits
34	_ALIGN_TEXT
35	.globl	_dl_start
36	.type	_dl_start,#function
37_dl_start:
38	mov	x29, sp
39	// need to worry about alignment, I think not?
40	mov	x19, sp
41	sub	sp, sp, #8+8+DL_DATA_SIZE	// dl_data size
42	add	x20, sp, #4			// dl_data
43	mov	x21, lr				// save old lr
44
45	mov	x0, x29				// original stack
46	mov	x1, x20				// dl_data
47
48	adrp	x2, _DYNAMIC			/* &_DYNAMIC */
49	add	x2, x2, #:lo12:_DYNAMIC
50
51	bl	_dl_boot_bind
52
53	add	x0, x29, #8			// argv
54	ldr	x1, [x29]			// load argc
55	add	x1, x0, x1, lsl #3		// envp = argv + argc * 8
56	add	x1, x1, #8			//                        + 8
57	ldr	x2, [x20, #7*8]			// loff from dl_data
58	mov	x3, x20				// dl_data
59	bl	_dl_boot
60	mov	x17, x0
61
62	mov	sp, x29				// move stack back
63	mov	x29, xzr			// clear frame back pointer
64	mov	lr, xzr
65
66	adrp	x8, :got:_dl_dtors
67	ldr	x2, [x8, :got_lo12:_dl_dtors]
68
69	br	x17
70END(_dl_start)
71
72ENTRY(_dl_bind_start)
73	/*
74	 * x16 is pointer to pltgot[2]
75	 * x17 is available as scratch register
76	 * return address and pointer to pltgot entry for this
77	 * relocation are on the stack
78	 */
79	mov	x17, sp
80
81	// save parameter/result registers
82	stp	x0, x1, [sp, #-16]!
83	stp	x2, x3, [sp, #-16]!
84	stp	x4, x5, [sp, #-16]!
85	stp	x6, x7, [sp, #-16]!
86	stp	x8, xzr, [sp, #-16]!
87
88	/*
89	 * no need to save v0-v9 as ld.so is compiled with
90	 * -march=armv8-a+nofp+nosimd and therefore doesn't touch the
91	 * SIMD and Floating-Point registers
92	 */
93
94	ldr	x0, [x16, #-8]		// object
95	ldr	x2, [x17]
96	sub	x1, x2, x16
97	sub	x1, x1, #8
98	lsr	x1, x1, #3		// relidx
99	bl	_dl_bind
100	mov	x17, x0
101
102	// restore parameter/result registers
103	ldp	x8, xzr, [sp], #16
104	ldp	x6, x7, [sp], #16
105	ldp	x4, x5, [sp], #16
106	ldp	x2, x3, [sp], #16
107	ldp	x0, x1, [sp], #16
108
109	// restore LR saved by PLT stub
110	ldp	xzr, x30, [sp], #16
111	br	x17
112END(_dl_bind_start)
113