ldasm.S revision 1.11
1191762Simp/*	$OpenBSD: ldasm.S,v 1.11 2023/07/08 14:09:43 jasper Exp $ */
2191762Simp
3191762Simp/*
4191762Simp * Copyright (c) 2016 Dale Rahn
5191762Simp *
6191762Simp * Redistribution and use in source and binary forms, with or without
7191762Simp * modification, are permitted provided that the following conditions
8191762Simp * are met:
9191762Simp * 1. Redistributions of source code must retain the above copyright
10191762Simp *    notice, this list of conditions and the following disclaimer.
11191762Simp * 2. Redistributions in binary form must reproduce the above copyright
12191762Simp *    notice, this list of conditions and the following disclaimer in the
13191762Simp *    documentation and/or other materials provided with the distribution.
14191762Simp *
15191762Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16191762Simp * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17191762Simp * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18191762Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19191762Simp * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20191762Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21191762Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22191762Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23191762Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24191762Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25191762Simp * SUCH DAMAGE.
26191762Simp *
27191762Simp */
28191762Simp
29191762Simp#define DL_DATA_SIZE	(16 * 8)	/* XXX */
30191762Simp#include <machine/asm.h>
31191762Simp#include <sys/syscall.h>
32191762Simp
33191762Simp	.section .boot.text,"ax",@progbits
34191762Simp	_ALIGN_TEXT
35191762Simp	.globl	_dl_start
36191762Simp	.type	_dl_start,#function
37191762Simp_dl_start:
38191762Simp	mov	x29, sp
39191762Simp	// need to worry about alignment, I think not?
40191762Simp	mov	x19, sp
41191762Simp	sub	sp, sp, #8+8+DL_DATA_SIZE	// dl_data size
42191762Simp	add	x20, sp, #4			// dl_data
43191762Simp	mov	x21, lr				// save old lr
44191762Simp
45191762Simp	mov	x0, x29				// original stack
46191762Simp	mov	x1, x20				// dl_data
47191762Simp
48191762Simp	adrp	x2, _DYNAMIC			/* &_DYNAMIC */
49191762Simp	add	x2, x2, #:lo12:_DYNAMIC
50191762Simp
51191762Simp	bl	_dl_boot_bind
52191762Simp
53191762Simp	add	x0, x29, #8			// argv
54191762Simp	ldr	x1, [x29]			// load argc
55191762Simp	add	x1, x0, x1, lsl #3		// envp = argv + argc * 8
56191762Simp	add	x1, x1, #8			//                        + 8
57191762Simp	ldr	x2, [x20, #7*8]			// loff from dl_data
58191762Simp	mov	x3, x20				// dl_data
59191762Simp	bl	_dl_boot
60191762Simp	mov	x17, x0
61191762Simp
62191762Simp	mov	sp, x29				// move stack back
63191762Simp	mov	x29, xzr			// clear frame back pointer
64191762Simp	mov	lr, xzr
65191762Simp
66191762Simp	adrp	x8, :got:_dl_dtors
67191762Simp	ldr	x2, [x8, :got_lo12:_dl_dtors]
68191762Simp
69191762Simp	br	x17
70191762SimpEND(_dl_start)
71191762Simp
72191762SimpENTRY(_dl_bind_start)
73191762Simp	/*
74191762Simp	 * x16 is pointer to pltgot[2]
75191762Simp	 * x17 is available as scratch register
76191762Simp	 * return address and pointer to pltgot entry for this
77191762Simp	 * relocation are on the stack
78191762Simp	 */
79191762Simp	mov	x17, sp
80191762Simp
81191762Simp	// save parameter/result registers
82191762Simp	stp	x0, x1, [sp, #-16]!
83191762Simp	stp	x2, x3, [sp, #-16]!
84191762Simp	stp	x4, x5, [sp, #-16]!
85191762Simp	stp	x6, x7, [sp, #-16]!
86191762Simp	stp	x8, xzr, [sp, #-16]!
87191762Simp
88191762Simp	/*
89191762Simp	 * no need to save v0-v9 as ld.so is compiled with
90191762Simp	 * -march=armv8-a+nofp+nosimd and therefore doesn't touch the
91191762Simp	 * SIMD and Floating-Point registers
92191762Simp	 */
93191762Simp
94191762Simp	ldr	x0, [x16, #-8]		// object
95191762Simp	ldr	x2, [x17]
96191762Simp	sub	x1, x2, x16
97191762Simp	sub	x1, x1, #8
98191762Simp	lsr	x1, x1, #3		// relidx
99191762Simp	bl	_dl_bind
100191762Simp	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