1112177Stjr/*	$NetBSD: rtld_start.S,v 1.16 2003/03/01 15:14:59 mycroft Exp $	*/
2112177Stjr
3112177Stjr/*-
4112177Stjr * Copyright (c) 2000 Eduardo Horvath.
5227753Stheraven * Copyright (c) 1999, 2002, 2003 The NetBSD Foundation, Inc.
6227753Stheraven * All rights reserved.
7227753Stheraven *
8227753Stheraven * This code is derived from software contributed to The NetBSD Foundation
9227753Stheraven * by Christos Zoulas, Paul Kranenburg and by Charles M. Hannum.
10112177Stjr *
11112177Stjr * Redistribution and use in source and binary forms, with or without
12112177Stjr * modification, are permitted provided that the following conditions
13112177Stjr * are met:
14112177Stjr * 1. Redistributions of source code must retain the above copyright
15112177Stjr *    notice, this list of conditions and the following disclaimer.
16112177Stjr * 2. Redistributions in binary form must reproduce the above copyright
17112177Stjr *    notice, this list of conditions and the following disclaimer in the
18112177Stjr *    documentation and/or other materials provided with the distribution.
19112177Stjr *
20112177Stjr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21112177Stjr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22112177Stjr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23112177Stjr * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24112177Stjr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25112177Stjr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26112177Stjr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27112177Stjr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28112177Stjr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29112177Stjr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30112177Stjr * POSSIBILITY OF SUCH DAMAGE.
31112177Stjr */
32112177Stjr
33112177Stjr#include <machine/asm.h>
34112177Stjr#define	_LOCORE
35112177Stjr#include <machine/frame.h>
36112177Stjr
37112177Stjr/*
38227753Stheraven * ELF:
39112177Stjr *	On startup the stack should contain 16 extended word register save area,
40112177Stjr *	followed by the arg count, etc.
41112177Stjr *
42112177Stjr * _rtld() expects the stack pointer to point to two longwords for argument
43112177Stjr *	return followed by argc, etc.  We need to create a pointer to
44227753Stheraven *	&argc + 16 and pass that in.  The return args will be in those locations.
45227753Stheraven *
46112177Stjr * NB:	We are violating the ELF spec by passing a pointer to the ps strings in
47127998Stjr * 	%g1 instead of a termination routine.
48127998Stjr */
49112177Stjr
50124174Snectar	.register	%g2,#scratch
51309334Svangyzen	.register	%g3,#scratch
52124174Snectar
53309334Svangyzen/* Offset of ARGC from bottom of stack */
54227753Stheraven#define	ARGC	176
55112177Stjr
56309334Svangyzen	.section	".text"
57309334Svangyzen	.align	4
58227753Stheraven	.global	_rtld_start
59227753Stheraven	.type	_rtld_start,@function
60227753Stheraven_rtld_start:
61227753Stheraven	mov	0, %fp			/* Erect a fence post for ourselves */
62112177Stjr	mov	%g1, %l1		/* save ps_strings */
63127998Stjr	sub	%sp, 48+16, %sp		/* Make room for return args */
64227753Stheraven
65112177Stjr	sethi	%hi(_GLOBAL_OFFSET_TABLE_-4), %l7
66112177Stjr	call	0f
67112177Stjr	 add	%l7, %lo(_GLOBAL_OFFSET_TABLE_+4), %l7
68112177Stjr	call	_DYNAMIC+8
69309334Svangyzen0:	add	%l7, %o7, %l7		/* real &_GLOBAL_OFFSET_TABLE_ */
70309334Svangyzen	ld	[%o7+8], %o0		/* load stub call instruction */
71309334Svangyzen	ldx	[%l7], %l0		/* base-relative &_DYNAMIC */
72112177Stjr	sll	%o0, 2, %o0		/* extract PC offset */
73309334Svangyzen	sra	%o0, 0, %o0		/* sign-extend */
74127998Stjr
75227753Stheraven	add	%o0, %o7, %o0		/* real &_DYNAMIC */
76112177Stjr	sub	%o0, %l0, %l0		/* relocbase */
77227753Stheraven	call	_rtld_relocate_nonplt_self
78112177Stjr	 mov	%l0, %o1		/* relocbase */
79227753Stheraven
80112177Stjr	mov	%l0, %o1		/* relocbase */
81227753Stheraven	call	_rtld
82227753Stheraven	 add	%sp, BIAS + ARGC, %o0	/* &argc - 16 */
83227753Stheraven
84112177Stjr	ldx	[%sp + BIAS + ARGC], %g3	/* arg: cleanup */
85112177Stjr	ldx	[%sp + BIAS + ARGC + 8], %g2	/* arg: obj */
86112177Stjr	add	%sp, 48+16, %sp		/* restore stack pointer */
87112177Stjr
88112177Stjr	jmp	%o0
89227753Stheraven	 mov	%l1, %g1		/* restore ps_strings */
90227753Stheraven
91227753Stheraven
92227753Stheraven	/*
93227753Stheraven	 * We have two separate entry points to the runtime linker.
94	 * I'm implementing this following the SPARC v9 ABI spec.
95	 *
96	 * _rtld_bind_start_0(y, x) is called from .PLT0, and is used for
97	 * PLT entries above 32768.
98	 *
99	 * _rtld_bind_start_1(y, x) is called from .PLT1, and is used for
100	 * PLT entries below 32768.
101	 *
102	 * The first two entries of PLT2 contain the xword object pointer.
103	 *
104	 * These routines are called with two longword arguments,
105	 * x and y.  To calculate the address of the entry,
106	 * _rtld_bind_start_1(y, x) does:
107	 *
108	 *	n = x >> 15;
109	 *
110	 * and _rtld_bind_start_0(y, x) does:
111	 *
112	 *	i = x - y + 8 - 32768*32;
113	 *	n = 32768 + (i/5120)*160 + (i%5120)/24;
114	 *
115	 * Neither routine needs to issue a save since it's already been
116	 * done in the PLT entry.
117	 */
118
119	.section	".text"
120	.align	4
121	.global	_rtld_bind_start_0
122	.type	_rtld_bind_start_0,@function
123_rtld_bind_start_0:	# (y, x)
124	/* %o0 = obj->pltgot[6] */
125	/* %o1 = plt[4] */
126	/* %o1 - %o0 + 8 == offset of plt[] from obj->pltgot[] */
127	/* -32768*32 to get offset from beginning of upper PLT section */
128
129	sethi	%hi(32768*32-8), %l1
130	sub	%o1, %o0, %l0		/* i = x - y */
131	or	%l1, %lo(32768*32-8), %l1
132	sub	%l0, %l1, %l0		/* i = x - y + 8 - 32768*32 */
133
134	ldx	[%o0 + (10*4)], %o0	/* Load object pointer from PLT2 */
135
136	sethi	%hi(5120), %l1
137	sdivx	%l0, %l1, %l1		/* Calculate i/5120 */
138	sllx	%l1, 2, %l2
139	add	%l2, %l1, %l2
140	sllx	%l2, 10, %l2
141	sub	%l0, %l2, %l2		/* And i%5120 */
142
143	/* Let the division churn for a bit. */
144	sdivx	%l2, 24, %l4		/* (i%5120)/24 */
145
146	/* 160 is (32 * 5) or (32 * (4 + 1)) */
147	sllx	%l1, 2, %l3		/* 4 * (i/5120) */
148	add	%l1, %l3, %l3		/* 5 * (i/5120) */
149	sllx	%l3, 5, %l3		/* 32 * 5 * (i/5120) */
150
151	sethi	%hi(32768), %l6
152	add	%l3, %l4, %l5		/* %l5 = (i/5120)*160 + (i%5120)/24; */
153
154	call	_rtld_bind		/* Call _rtld_bind(obj, offset) */
155	 add	%l5, %l6, %o1		/* %o1 = 32768 + ... */
156
157	jmp	%o0			/* return value == function address */
158	 restore			/* Dump our stack frame */
159
160	.section	".text"
161	.align	4
162	.global	_rtld_bind_start_1
163	.type	_rtld_bind_start_1,@function
164_rtld_bind_start_1:	# (y, x)
165	ldx	[%o0 + (2*4)], %o0	/* Load object pointer from PLT2 */
166
167	call	_rtld_bind		/* Call _rtld_bind(obj, offset) */
168	 srax	%o1, 15, %o1		/* %o1 is the index to our PLT slot */
169
170	jmp	%o0			/* return value == function address */
171	 restore			/* Dump our stack frame */
172
173