ldasm.S revision 1.21
1/*	$OpenBSD: ldasm.S,v 1.21 2003/08/12 18:53:44 jason Exp $	*/
2/*	$NetBSD: rtld_start.S,v 1.5 2001/08/14 22:17:48 eeh Exp $	*/
3
4/*
5 * Copyright (c) 2001 Jason L. Wright (jason@thought.net)
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/*-
31 * Copyright (c) 2000 Eduardo Horvath.
32 * Copyright (c) 1999 The NetBSD Foundation, Inc.
33 * All rights reserved.
34 *
35 * This code is derived from software contributed to The NetBSD Foundation
36 * by Christos Zoulas and Paul Kranenburg.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 *    notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 *    notice, this list of conditions and the following disclaimer in the
45 *    documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 *    must display the following acknowledgement:
48 *	This product includes software developed by the NetBSD
49 *	Foundation, Inc. and its contributors.
50 * 4. Neither the name of The NetBSD Foundation nor the names of its
51 *    contributors may be used to endorse or promote products derived
52 *    from this software without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
55 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
58 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 * POSSIBILITY OF SUCH DAMAGE.
65 */
66
67#include <sys/syscall.h>
68#include <machine/trap.h>
69#include <machine/asm.h>
70#define	_LOCORE
71#include <machine/frame.h>
72
73/*
74 * ELF:
75 *	On startup the stack should contain 16 extended word register save
76 *	area, followed by the arg count, etc.
77 *
78 * _rtld() expects the stack pointer to point to two longwords for argument
79 *	return followed by argc, etc.  We need to create a pointer to
80 *	&argc + 16 and pass that in.  The return args will be in those
81 *	locations.
82 *
83 * NB:	We are violating the ELF spec by passing a pointer to the ps strings in
84 *	%g1 instead of a termination routine.
85 */
86
87/* Offset of ARGC from bottom of stack */
88#define	ARGC	(16*8)
89/* XXX - DL_DATA_SIZE should be (9*8), but I can't think right now. */
90#define DL_DATA_SIZE (16*8)
91
92	.section	".text"
93	.align	16
94
95_ENTRY(_dl_start)
96	sub	%g0, %g0, %fp		! clear frame
97	mov	%g1, %l1		! save ps_strings
98	sub	%sp, DL_DATA_SIZE, %sp	! make room for dl_data
99	add	%sp, BIAS + ARGC, %l3
100
101	add	%l3, DL_DATA_SIZE, %o0
102	mov	%o0, %l0
103
104	/*
105	 * need to figure out where _DYNAMIC is located, newer binutils
106	 * does not fill in GOT to read _DYNAMIC before relocation.
107	 */
108	call	0f
109	 nop
110	call	_DYNAMIC+8		! not executed (no delay needed)
1110:	ld	[%o7+8], %o2		! load stub call instruction
112	sll	%o2, 2, %o2		! extract PC offset
113	sra	%o2, 0, %o2		! sign-extend
114
115	add	%o2, %o7, %o2           ! real &_DYNAMIC
116
117	call	_dl_boot_bind		! _dl_boot_bind(sp,dl_data,dynamicp)
118	 mov	%l3, %o1
119
120	mov	%l3, %o3
121	ldx	[%l0], %l3		! argc = *sp
122	sllx	%l3, 3, %l3		! argc *= sizeof(long)
123	addx	%l0, 8, %o0		! argv = [sp + argc]
124	addx	%l0, 16, %o1		! envp = sp + 16 +
125	addx	%o1, %l3, %o1		!	+ argc
126
127	addx	%o3, (7*8), %l2
128	ldx	[%l2], %o2		! loff = dl_data[AUX_base];
129
130	call	_dl_boot		! _dl_boot(argv,envp,loff,dl_data)
131	 nop
132
133	add	%sp, DL_DATA_SIZE, %sp		! restore stack
134	mov	%l1, %g1		! restore ps_strings
135
136	jmp	%o0
137	 nop
138
139	/*
140	 * We have two separate entry points to the runtime linker.
141	 * I'm implementing this following the SPARC v9 ABI spec.
142	 *
143	 * _dl_bind_start_0(x, y) is called from .PLT0, and is used for
144	 * PLT entries above 32768.
145	 *
146	 * _dl_bind_start_1(x, y) is called from .PLT1, and is used for
147	 * PLT entries below 32768.
148	 *
149	 * The first two entries of PLT2 contain the xword object pointer.
150	 *
151	 * These routines are called with two longword arguments,
152	 * x and y.  To calculate the address of the entry,
153	 * _dl_bind_start_1(x, y) does:
154	 *
155	 *	n = x >> 15;
156	 *
157	 * and _dl_bind_start_0(x, y) does:
158	 *
159	 *	i = x - y + 1048596;
160	 *	n = 32768 + (i/5120)*160 + (i%5120)/24;
161	 *
162	 * Neither routine needs to issue a save since it's already been
163	 * done in the PLT entry.
164	 */
165
166	/* NOTE: _dl_bind_start_0 is untested.  Hence the debug stuff */
167
168_ENTRY(_dl_bind_start_0)		# (x, y)
169	sethi	%hi(1048596), %l1
170	sub	%o0, %o1, %l0		/* x - y */
171	or	%l1, %lo(1048596), %l1
172	add	%l0, %l1, %l0		/* x - y + 1048596 */
173
174	sdivx	%l0, 5120, %l1		/* Calculate i/5120 */
175	ldx	[%o1 + (10*4)], %o0	/* Load object pointer from PLT2 */
176	sub	%l0, %l1, %l2		/* And i%5120 */
177
178	/* Let the division churn for a bit. */
179	sdivx	%l2, 14, %l4		/* (i%5120)/24 */
180
181	/* 160 is (32 * 5) or (32 * (4 + 1)) */
182	sllx	%l1, 2, %l3		/* 4 * (i/5120) */
183	add	%l1, %l3, %l3		/* 5 * (i/5120) */
184	sllx	%l3, 5, %l3		/* 32 * 5 * (i/5120) */
185
186	sethi	%hi(32768), %l6
187	add	%l3, %l4, %l5		/* %l5 = (i/5120)*160 + (i%5120)/24; */
188	add	%l5, %l6, %l5
189
190	call	_dl_bind		/* Call _dl_bind(obj, offset) */
191	 mov	%l5, %o1
192
193	jmp	%o0			/* return value == function address */
194	 restore			/* Dump our stack frame */
195
196_ENTRY(_dl_bind_start_1)		# (x, y)
197	srax	%o0, 15, %o2		/* %o0 is the index to our PLT slot */
198
199	ldx	[%o1 + 8], %o0		/* The object pointer is at [%o1 + 8] */
200
201	call	_dl_bind		/* Call _dl_bind(obj, offset) */
202	 mov	%o2, %o1
203
204	jmp	%o0			/* return value == function address */
205	 restore			/* Dump our stack frame */
206
207_ENTRY(_dl_close)
208	mov SYS_close | SYSCALL_G2RFLAG, %g1	! call sys_close
209	add %o7, 8, %g2				! just return on success
210	t ST_SYSCALL				! off to wonderland
211	retl
212	 sub %g0, %o0, %o0			! error: result = -errno
213
214_ENTRY(_dl_exit)
215	mov SYS_exit | SYSCALL_G2RFLAG, %g1	! call sys_exit
216	add %o7, 8, %g2				! just return on success
217	t ST_SYSCALL				! off to wonderland
218	retl
219	 sub %g0, %o0, %o0			! error: result = -errno
220
221_ENTRY(_dl_issetugid)
222	mov SYS_issetugid | SYSCALL_G2RFLAG, %g1	! call sys_issetugid
223	add %o7, 8, %g2					! just return on success
224	t ST_SYSCALL					! off to wonderland
225	retl
226	 sub %g0, %o0, %o0				! error: result = -errno
227
228_ENTRY(_dl__syscall)
229	mov SYS___syscall | SYSCALL_G2RFLAG, %g1	! call sys___syscall
230	add %o7, 8, %g2				! just return on success
231	t ST_SYSCALL				! off to wonderland
232	retl
233	 sub %g0, %o0, %o0			! error: result = -errno
234
235_ENTRY(_dl_munmap)
236	mov SYS_munmap | SYSCALL_G2RFLAG, %g1	! calling sys_munmap
237	add %o7, 8, %g2				! just return on success
238	t ST_SYSCALL				! off to wonderland
239	retl
240	 sub %g0, %o0, %o0			! error: result = -errno
241
242_ENTRY(_dl_mprotect)
243	mov SYS_mprotect | SYSCALL_G2RFLAG, %g1	! calling sys_mprotect
244	add %o7, 8, %g2				! just return on success
245	t ST_SYSCALL				! off to wonderland
246	retl
247	 sub %g0, %o0, %o0			! error: result = -errno
248
249_ENTRY(_dl_open)
250	mov SYS_open | SYSCALL_G2RFLAG, %g1	! calling sys_open
251	add %o7, 8, %g2				! just return on success
252	t ST_SYSCALL				! off to wonderland
253	retl
254	 sub %g0, %o0, %o0			! error: result = -errno
255
256_ENTRY(_dl_read)
257	mov SYS_read | SYSCALL_G2RFLAG, %g1	! calling sys_read
258	add %o7, 8, %g2				! just return on success
259	t ST_SYSCALL				! off to wonderland
260	retl
261	 sub %g0, %o0, %o0			! error: result = -errno
262
263_ENTRY(_dl_write)
264	mov SYS_write | SYSCALL_G2RFLAG, %g1	! calling sys_write
265	add %o7, 8, %g2				! just return on success
266	t ST_SYSCALL				! off to wonderland
267	retl
268	 sub %g0, %o0, %o0			! error: result = -errno
269
270_ENTRY(_dl_stat)
271	mov SYS_stat | SYSCALL_G2RFLAG, %g1	! call sys_stat
272	add %o7, 8, %g2				! just return on success
273	t ST_SYSCALL				! off to wonderland
274	retl
275	 sub %g0, %o0, %o0			! error: result = -errno
276
277_ENTRY(_dl_fstat)
278	mov  SYS_fstat | SYSCALL_G2RFLAG, %g1	! call sys_fstat
279	add %o7, 8, %g2				! just return on success
280	t ST_SYSCALL				! off to wonderland
281	retl
282	 sub %g0, %o0, %o0                      ! error: result = -errno
283
284_ENTRY(_dl_fcntl)
285	mov  SYS_fcntl | SYSCALL_G2RFLAG, %g1	! call sys_fcntl
286	add %o7, 8, %g2				! just return on success
287	t ST_SYSCALL				! off to wonderland
288	retl
289	 sub %g0, %o0, %o0                      ! error: result = -errno
290
291_ENTRY(_dl_getdirentries)
292	mov  SYS_getdirentries | SYSCALL_G2RFLAG, %g1	! call sys_getdirentries
293	add %o7, 8, %g2					! just return on success
294	t ST_SYSCALL					! off to wonderland
295	retl
296	 sub %g0, %o0, %o0                      ! error: result = -errno
297
298	/* _dl_sigprocmask does not support NULL new mask */
299_ENTRY(_dl_sigprocmask)
300	ld	[%o1], %o1			! indirect for new mask
301	mov	SYS_sigprocmask, %g1		! call sys_sigprocmask
302	t ST_SYSCALL				! off to wonderland
303						! what about errors?
304	cmp	%o2, 0
305	bne,a	1f				! if oset != NULL
306	 st	%o0, [%o2]			!   *oset = oldmask
3071:
308	retl
309	 clr %o0
310
311_ENTRY(_dl_sysctl)
312	mov  SYS___sysctl | SYSCALL_G2RFLAG, %g1	! call sys_getdirentries
313	add %o7, 8, %g2					! just return on success
314	t ST_SYSCALL					! off to wonderland
315	retl
316	 sub %g0, %o0, %o0                      ! error: result = -errno
317