ldasm.S revision 1.6
1/*	$OpenBSD: ldasm.S,v 1.6 2018/07/24 09:27:44 kettenis 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
33ENTRY(_dl_start)
34	mov	x29, sp
35	// need to worry about alignment, I think not?
36	mov	x19, sp
37	sub	sp, sp, #8+8+DL_DATA_SIZE	// dl_data size
38	add	x20, sp, #4			// dl_data
39	mov	x21, lr				// save old lr
40
41	mov	x0, x29				// original stack
42	mov	x1, x20				// dl_data
43
44	adrp	x2, _DYNAMIC			/* &_DYNAMIC */
45	add	x2, x2, #:lo12:_DYNAMIC
46
47	bl	_dl_boot_bind
48
49	add	x0, x29, #8			// argv
50	ldr	x1, [x29]			// load argc
51	add	x1, x0, x1, lsl #3		// envp = argv + argc * 8
52	add	x1, x1, #8			//                        + 8
53	ldr	x2, [x20, #7*8]			// loff from dl_data
54	mov	x3, x20				// dl_data
55	bl	_dl_boot
56
57	mov	sp, x29				// move stack back
58	mov	x29, xzr			// clear frame back pointer
59	mov	lr, xzr
60
61	adrp	x8, :got:_dl_dtors
62	ldr	x2, [x8, :got_lo12:_dl_dtors]
63
64	br	x0
65
66ENTRY(_dl_bind_start)
67	/*
68	 * x16 is pointer to pltgot[2]
69	 * x17 is available as scratch register
70	 * return address and pointer to pltgot entry for this
71	 * relocation are on the stack
72	 */
73	mov x17, sp
74
75	// save parameter/result registers
76	stp	x0, x1, [sp, #-16]!
77	stp	x2, x3, [sp, #-16]!
78	stp	x4, x5, [sp, #-16]!
79	stp	x6, x7, [sp, #-16]!
80	stp	x8, xzr, [sp, #-16]!
81
82	/*
83	 * no need to save v0-v9 as ld.so is compiled with
84	 * -march=armv8-a+nofp+nosimd and therefore doesn't touch the
85	 * SIMD and Floating-Point registers
86	 */
87
88	ldr	x0, [x16, #-8]		// object
89	ldr	x2, [x17]
90	sub	x1, x2, x16
91	sub	x1, x1, #8
92	lsr	x1, x1, #3		// relidx
93	bl	_dl_bind
94	mov	x17, x0
95
96	// restore parameter/result registers
97	ldp	x8, xzr, [sp], #16
98	ldp	x6, x7, [sp], #16
99	ldp	x4, x5, [sp], #16
100	ldp	x2, x3, [sp], #16
101	ldp	x0, x1, [sp], #16
102
103	// restore LR saved by PLT stub
104	ldp	xzr, x30, [sp], #16
105	br	x17
106
107ENTRY(_rtld_tlsdesc)
108	ldr	x0, [x0, #8]
109	ret
110