rtld_start.S revision 117211
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: head/libexec/rtld-elf/sparc64/rtld_start.S 117211 2003-07-04 00:05:15Z jake $
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
75ENTRY(rtld_dynamic)
76	save	%sp, -CCFSZ, %sp
77	call	1f
78	 nop
79	call	_DYNAMIC + 8
801:	lduw	[%o7 + 8], %o0
81	sll	%o0, 2, %o0
82	sra	%o0, 0, %o0
83	ret
84	 restore %o0, %o7, %o0
85END(rtld_dynamic)
86
87	/*
88	 * We have two separate entry points to the runtime linker.
89	 * I'm implementing this following the SPARC v9 ABI spec.
90	 *
91	 * _rtld_bind_start_0(x, y) is called from .PLT0, and is used for
92	 * PLT entries above 32768.
93	 *
94	 * _rtld_bind_start_1(x, y) is called from .PLT1, and is used for
95	 * PLT entries below 32768.
96	 *
97	 * The first two entries of PLT2 contain the xword object pointer.
98	 *
99	 * These routines are called with two longword arguments,
100	 * x and y.  To calculate the address of the entry,
101	 * _rtld_bind_start_1(x, y) does:
102	 *
103	 *	n = x >> 15;
104	 *
105	 * and _rtld_bind_start_0(x, y) should do, according to the SCD:
106	 *
107	 *	i = x - y - 1048596;
108	 *	n = 32768 + (i/5120)*160 + (i%5120)/24;
109	 *
110	 * Note that the number of 1048596 from above is incorrect; rather,
111	 * we need to use HIPLTOFFS as defined below.
112	 *
113	 * Neither routine needs to issue a save since it's already been
114	 * done in the PLT entry.
115	 */
116
117#define	NPLTLOSLOTS	32768
118#define	PLTSLOTSZ	32
119/*
120 * - 16 to compensate for the difference of the positions of the jumps that
121 * generate the arguments in .PLT0 and the high plt entry.
122 */
123#define	HIPLTOFFS	(NPLTLOSLOTS * PLTSLOTSZ - 16)
124
125ENTRY(_rtld_bind_start_0)
126	sethi	%hi(HIPLTOFFS), %l1
127	or	%l1, %lo(HIPLTOFFS), %l1
128	sub	%o0, %o1, %l0		/* x - y */
129	sub	%l0, %l1, %l0		/* i = x - y - HIPLTOFFS */
130	sethi	%hi(5120), %l7
131	sdivx	%l0, %l7, %l1		/* Calculate i / 5120 */
132	mulx	%l1, %l7, %l3
133	sub	%l0, %l3, %l2		/* And i % 5120 */
134	mulx	%l1, 160, %l5		/* (i / 5120) * 160 */
135	sdivx	%l2, 24, %l4		/* (i % 5120) / 24 */
136	sethi	%hi(NPLTLOSLOTS), %l6
137	add	%l4, %l5, %l4		/* (i / 5120) * 160 + (i % 5120) / 24 */
138	add	%l4, %l6, %l4		/* + NPLTLOSLOTS */
139	sub	%l4, 4, %l4		/* XXX:	4 entries are reserved */
140
141	sllx	%l4, 1, %l5		/* Each element is an Elf_Rela which */
142	add	%l5, %l4, %l4		/*  is 3 longwords or 24 bytes. */
143	sllx	%l4, 3, %l4		/*  So multiply by 24. */
144
145	ldx	[%o1 + (10*4)], %o0	/* Load object pointer from PLT2 */
146
147	call	_rtld_bind		/* Call _rtld_bind(obj, offset) */
148	 mov	%l4, %o1
149
150	jmp	%o0			/* return value == function address */
151	 restore			/* Dump our stack frame */
152END(_rtld_bind_start_0)
153
154ENTRY(_rtld_bind_start_1)
155	srax	%o0, 15, %o2		/* %o0 is the index to our PLT slot */
156	sub	%o2, 4, %o2		/* XXX:	4 entries are reserved */
157
158	sllx	%o2, 1, %o3		/* Each element is an Elf_Rela which */
159	add	%o3, %o2, %o2		/*  is 3 longwords or 24 bytes. */
160	sllx	%o2, 3, %o2		/*  So multiply by 24. */
161
162	ldx	[%o1 + 8], %o0		/* The object pointer is at [%o1 + 8] */
163
164	call	_rtld_bind		/* Call _rtld_bind(obj, offset) */
165	 mov	%o2, %o1
166
167	jmp	%o0			/* return value == function address */
168	 restore			/* Dump our stack frame */
169END(_rtld_bind_start_1)
170