1/*	$NetBSD: rtld_start.S,v 1.5 2001/08/14 22:17:48 eeh Exp $	*/
2
3/*-
4 * Copyright (c) 2001 Jake Burkholder.
5 * Copyright (c) 2000 Eduardo Horvath.
6 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Christos Zoulas and Paul Kranenburg.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 *    must display the following acknowledgement:
22 *        This product includes software developed by the NetBSD
23 *        Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 *    contributors may be used to endorse or promote products derived
26 *    from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 *
40 * $FreeBSD$
41 */
42
43#include <machine/asm.h>
44
45/*
46 * ELF:
47 *	On startup the stack should contain 16 extended word register save
48 *	area, followed by the arg count, etc.
49 */
50
51ENTRY(.rtld_start)
52	clr	%fp
53	mov	%o0, %l0
54	mov	%o3, %l1
55
56	sub	%sp, 16, %sp
57	add	%sp, SPOFF + CCFSZ + 0x0, %o1
58	call	_rtld
59	 add	%sp, SPOFF + CCFSZ + 0x8, %o2
60
61	ldx	[%sp + SPOFF + CCFSZ + 0x0], %o1
62	ldx	[%sp + SPOFF + CCFSZ + 0x8], %o2
63	add	%sp, 16, %sp
64
65	mov	%l1, %o3
66	jmp	%o0
67	 mov	%l0, %o0
68END(.rtld_start)
69
70/*
71 * Find the address of _DYNAMIC by disassembling a call instruction to it.
72 * Binutils may not fill in the GOT as expected on other architectures.
73 */
74.weak	_DYNAMIC
75
76ENTRY(rtld_dynamic_addr)
77	save	%sp, -CCFSZ, %sp
78	call	1f
79	 nop
80	call	_DYNAMIC + 8
811:	lduw	[%o7 + 8], %o0
82	sll	%o0, 2, %o0
83	sra	%o0, 0, %o0
84	ret
85	 restore %o0, %o7, %o0
86END(rtld_dynamic_addr)
87
88	/*
89	 * We have two separate entry points to the runtime linker.
90	 * I'm implementing this following the SPARC v9 ABI spec.
91	 *
92	 * _rtld_bind_start_0(x, y) is called from .PLT0, and is used for
93	 * PLT entries above 32768.
94	 *
95	 * _rtld_bind_start_1(x, y) is called from .PLT1, and is used for
96	 * PLT entries below 32768.
97	 *
98	 * The first two entries of PLT2 contain the xword object pointer.
99	 *
100	 * These routines are called with two longword arguments,
101	 * x and y.  To calculate the address of the entry,
102	 * _rtld_bind_start_1(x, y) does:
103	 *
104	 *	n = x >> 15;
105	 *
106	 * and _rtld_bind_start_0(x, y) should do, according to the SCD:
107	 *
108	 *	i = x - y - 1048596;
109	 *	n = 32768 + (i/5120)*160 + (i%5120)/24;
110	 *
111	 * Note that the number of 1048596 from above is incorrect; rather,
112	 * we need to use HIPLTOFFS as defined below.
113	 *
114	 * Neither routine needs to issue a save since it's already been
115	 * done in the PLT entry.
116	 */
117
118#define	NPLTLOSLOTS	32768
119#define	PLTSLOTSZ	32
120/*
121 * - 16 to compensate for the difference of the positions of the jumps that
122 * generate the arguments in .PLT0 and the high plt entry.
123 */
124#define	HIPLTOFFS	(NPLTLOSLOTS * PLTSLOTSZ - 16)
125
126ENTRY(_rtld_bind_start_0)
127	sethi	%hi(HIPLTOFFS), %l1
128	or	%l1, %lo(HIPLTOFFS), %l1
129	sub	%o0, %o1, %l0		/* x - y */
130	sub	%l0, %l1, %l0		/* i = x - y - HIPLTOFFS */
131	sethi	%hi(5120), %l7
132	sdivx	%l0, %l7, %l1		/* Calculate i / 5120 */
133	mulx	%l1, %l7, %l3
134	sub	%l0, %l3, %l2		/* And i % 5120 */
135	mulx	%l1, 160, %l5		/* (i / 5120) * 160 */
136	sdivx	%l2, 24, %l4		/* (i % 5120) / 24 */
137	sethi	%hi(NPLTLOSLOTS), %l6
138	add	%l4, %l5, %l4		/* (i / 5120) * 160 + (i % 5120) / 24 */
139	add	%l4, %l6, %l4		/* + NPLTLOSLOTS */
140	sub	%l4, 4, %l4		/* XXX:	4 entries are reserved */
141
142	sllx	%l4, 1, %l5		/* Each element is an Elf_Rela which */
143	add	%l5, %l4, %l4		/*  is 3 longwords or 24 bytes. */
144	sllx	%l4, 3, %l4		/*  So multiply by 24. */
145
146	ldx	[%o1 + (10*4)], %o0	/* Load object pointer from PLT2 */
147
148	call	_rtld_bind		/* Call _rtld_bind(obj, offset) */
149	 mov	%l4, %o1
150
151	jmp	%o0			/* return value == function address */
152	 restore			/* Dump our stack frame */
153END(_rtld_bind_start_0)
154
155ENTRY(_rtld_bind_start_1)
156	srax	%o0, 15, %o2		/* %o0 is the index to our PLT slot */
157	sub	%o2, 4, %o2		/* XXX:	4 entries are reserved */
158
159	sllx	%o2, 1, %o3		/* Each element is an Elf_Rela which */
160	add	%o3, %o2, %o2		/*  is 3 longwords or 24 bytes. */
161	sllx	%o2, 3, %o2		/*  So multiply by 24. */
162
163	ldx	[%o1 + 8], %o0		/* The object pointer is at [%o1 + 8] */
164
165	call	_rtld_bind		/* Call _rtld_bind(obj, offset) */
166	 mov	%o2, %o1
167
168	jmp	%o0			/* return value == function address */
169	 restore			/* Dump our stack frame */
170END(_rtld_bind_start_1)
171