ldasm.S revision 1.3
1/*	$OpenBSD: ldasm.S,v 1.3 2017/06/04 14:26:27 patrick 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#include <SYS.h>
33
34ENTRY(_dl_start)
35	mov	x29, sp
36	// need to worry about alignment, I think not?
37	mov	x19, sp
38	sub	sp, sp, #8+8+DL_DATA_SIZE	// dl_data size
39	add	x20, sp, #4			// dl_data
40	mov	x21, lr				// save old lr
41
42	mov	x0, x29				// original stack
43	mov	x1, x20				// dl_data
44
45	adrp	x2, :got:_DYNAMIC		/* &_DYNAMIC */
46	add	x2, x2, #:lo12:_DYNAMIC
47
48	bl	_dl_boot_bind
49
50	add	x0, x29, #8			// argv
51	ldr	x1, [x29]			// load argc
52	add	x1, x0, x1, lsl #3		// envp = argv + argc * 8
53	add	x1, x1, #8			//                        + 8
54	ldr	x2, [x20, #7*8]			// loff from dl_data
55	mov	x3, x20				// dl_data
56	bl	_dl_boot
57
58	mov	sp, x29				// move stack back
59	mov	x29, xzr			// clear frame back pointer
60	mov	lr, xzr
61
62	adrp	x8, :got:_dl_dtors
63	ldr	x2, [x8, :got_lo12:_dl_dtors]
64
65	br	x0
66
67ENTRY(_dl_bind_start)
68	/*
69	 * ip is pointer to got entry for this relocation
70	 * lr is pointer to pltgot[2], which is entry -1 of got plt reloc.
71	 * return address is on stack
72	 */
73	stp	x29, x30, [sp, #-160]!
74	stp	 x0,  x1, [sp,#16]
75	stp	 x2,  x3, [sp,#32]
76	stp	 x4,  x5, [sp,#48]
77	stp	 x6,  x7, [sp,#64]
78	stp	 x8,  x9, [sp,#80]
79	stp	x10, x11, [sp,#96]
80	stp	x12, x13, [sp,#112]
81	stp	x14, x15, [sp,#128]
82	str	x18,      [sp,#144]
83
84	mov	x1, x16		// reladdr
85
86
87	ldp	 x0,  x1, [sp,#16]
88	ldp	 x2,  x3, [sp,#32]
89	ldp	 x4,  x5, [sp,#48]
90	ldp	 x6,  x7, [sp,#64]
91	ldp	 x8,  x9, [sp,#80]
92	ldp	x10, x11, [sp,#96]
93	ldp	x12, x13, [sp,#112]
94	ldp	x14, x15, [sp,#128]
95	ldr	x18,      [sp,#144]
96	ldp	x29, x30, [sp], #-160
97	br x16
98
99
100#if 0
101	stmdb	sp!, {r0-r4,sl,fp}
102
103	sub	r1, ip, lr		/* r1 = 4 * (n + 1) */
104	sub	r1, r1, #4		/* r1 = 4 * n */
105	mov	r1, r1, lsr #2		/* r1 = n */
106
107	ldr	r0, [lr, #-4]
108	bl	_dl_bind
109	mov	ip, r0
110	ldmia	sp!, {r0-r4,sl,fp,lr}
111	mov	pc, ip
112#endif
113
114	/* STUB */
115
116
117/* ld.so SYSCALLS */
118
119#define DL_SYSCALL(n) DL_SYSCALL2(n,n)
120#define DL_SYSCALL2(n,c)					\
121	.global		__CONCAT(_dl_,n)		;\
122	.type		__CONCAT(_dl_,n)%function	;\
123__CONCAT(_dl_,n):					;\
124	SYSTRAP(c)					;\
125	bcs	.L_cerr					;\
126	ret
127
128	.section	".text"
129	.align		4
130DL_SYSCALL(close)
131
132
133	.global		_dl_exit
134	.type		_dl_exit%function
135_dl_exit:
136	SYSTRAP(exit)
137	1:
138		b 1b
139
140DL_SYSCALL(issetugid)
141DL_SYSCALL(getthrid)
142DL_SYSCALL2(_syscall,__syscall)
143DL_SYSCALL(munmap)
144DL_SYSCALL(mprotect)
145DL_SYSCALL(open)
146DL_SYSCALL(read)
147DL_SYSCALL(write)
148DL_SYSCALL(fstat)
149DL_SYSCALL(readlink)
150DL_SYSCALL(utrace)
151DL_SYSCALL(getentropy)
152DL_SYSCALL(sendsyslog)
153DL_SYSCALL(pledge)
154DL_SYSCALL2(getcwd,__getcwd)
155DL_SYSCALL(sysctl)
156DL_SYSCALL2(set_tcb,__set_tcb)
157DL_SYSCALL(thrkill)
158
159DL_SYSCALL(getdents)
160
161.L_cerr:
162	neg	w0, w0		/* r0 = -errno */
163	ret
164
165ENTRY(_rtld_tlsdesc)
166	ldr	x0, [x0, #8]
167	ret
168