xref: /minix3/libexec/ld.elf_so/arch/sparc64/rtld_start.S (revision e83f7ba2c93894ce4409c1a9136af3d6d8ad48cf)
1*e83f7ba2SBen Gras/*	$NetBSD: rtld_start.S,v 1.17 2008/04/28 20:23:04 martin Exp $	*/
2*e83f7ba2SBen Gras
3*e83f7ba2SBen Gras/*-
4*e83f7ba2SBen Gras * Copyright (c) 2000 Eduardo Horvath.
5*e83f7ba2SBen Gras * Copyright (c) 1999, 2002, 2003 The NetBSD Foundation, Inc.
6*e83f7ba2SBen Gras * All rights reserved.
7*e83f7ba2SBen Gras *
8*e83f7ba2SBen Gras * This code is derived from software contributed to The NetBSD Foundation
9*e83f7ba2SBen Gras * by Christos Zoulas, Paul Kranenburg and by Charles M. Hannum.
10*e83f7ba2SBen Gras *
11*e83f7ba2SBen Gras * Redistribution and use in source and binary forms, with or without
12*e83f7ba2SBen Gras * modification, are permitted provided that the following conditions
13*e83f7ba2SBen Gras * are met:
14*e83f7ba2SBen Gras * 1. Redistributions of source code must retain the above copyright
15*e83f7ba2SBen Gras *    notice, this list of conditions and the following disclaimer.
16*e83f7ba2SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
17*e83f7ba2SBen Gras *    notice, this list of conditions and the following disclaimer in the
18*e83f7ba2SBen Gras *    documentation and/or other materials provided with the distribution.
19*e83f7ba2SBen Gras *
20*e83f7ba2SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21*e83f7ba2SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22*e83f7ba2SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23*e83f7ba2SBen Gras * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24*e83f7ba2SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25*e83f7ba2SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26*e83f7ba2SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27*e83f7ba2SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28*e83f7ba2SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*e83f7ba2SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30*e83f7ba2SBen Gras * POSSIBILITY OF SUCH DAMAGE.
31*e83f7ba2SBen Gras */
32*e83f7ba2SBen Gras
33*e83f7ba2SBen Gras#include <machine/asm.h>
34*e83f7ba2SBen Gras#define	_LOCORE
35*e83f7ba2SBen Gras#include <machine/frame.h>
36*e83f7ba2SBen Gras
37*e83f7ba2SBen Gras/*
38*e83f7ba2SBen Gras * ELF:
39*e83f7ba2SBen Gras *	On startup the stack should contain 16 extended word register save area,
40*e83f7ba2SBen Gras *	followed by the arg count, etc.
41*e83f7ba2SBen Gras *
42*e83f7ba2SBen Gras * _rtld() expects the stack pointer to point to two longwords for argument
43*e83f7ba2SBen Gras *	return followed by argc, etc.  We need to create a pointer to
44*e83f7ba2SBen Gras *	&argc + 16 and pass that in.  The return args will be in those locations.
45*e83f7ba2SBen Gras *
46*e83f7ba2SBen Gras * NB:	We are violating the ELF spec by passing a pointer to the ps strings in
47*e83f7ba2SBen Gras * 	%g1 instead of a termination routine.
48*e83f7ba2SBen Gras */
49*e83f7ba2SBen Gras
50*e83f7ba2SBen Gras	.register	%g2,#scratch
51*e83f7ba2SBen Gras	.register	%g3,#scratch
52*e83f7ba2SBen Gras
53*e83f7ba2SBen Gras/* Offset of ARGC from bottom of stack */
54*e83f7ba2SBen Gras#define	ARGC	176
55*e83f7ba2SBen Gras
56*e83f7ba2SBen Gras	.section	".text"
57*e83f7ba2SBen Gras	.align	4
58*e83f7ba2SBen Gras	.global	_rtld_start
59*e83f7ba2SBen Gras	.type	_rtld_start,@function
60*e83f7ba2SBen Gras_rtld_start:
61*e83f7ba2SBen Gras	mov	0, %fp			/* Erect a fence post for ourselves */
62*e83f7ba2SBen Gras	mov	%g1, %l1		/* save ps_strings */
63*e83f7ba2SBen Gras	sub	%sp, 48+16, %sp		/* Make room for return args */
64*e83f7ba2SBen Gras
65*e83f7ba2SBen Gras	sethi	%hi(_GLOBAL_OFFSET_TABLE_-4), %l7
66*e83f7ba2SBen Gras	call	0f
67*e83f7ba2SBen Gras	 add	%l7, %lo(_GLOBAL_OFFSET_TABLE_+4), %l7
68*e83f7ba2SBen Gras	call	_DYNAMIC+8
69*e83f7ba2SBen Gras0:	add	%l7, %o7, %l7		/* real &_GLOBAL_OFFSET_TABLE_ */
70*e83f7ba2SBen Gras	ld	[%o7+8], %o0		/* load stub call instruction */
71*e83f7ba2SBen Gras	ldx	[%l7], %l0		/* base-relative &_DYNAMIC */
72*e83f7ba2SBen Gras	sll	%o0, 2, %o0		/* extract PC offset */
73*e83f7ba2SBen Gras	sra	%o0, 0, %o0		/* sign-extend */
74*e83f7ba2SBen Gras
75*e83f7ba2SBen Gras	add	%o0, %o7, %o0		/* real &_DYNAMIC */
76*e83f7ba2SBen Gras	sub	%o0, %l0, %l0		/* relocbase */
77*e83f7ba2SBen Gras	call	_rtld_relocate_nonplt_self
78*e83f7ba2SBen Gras	 mov	%l0, %o1		/* relocbase */
79*e83f7ba2SBen Gras
80*e83f7ba2SBen Gras	mov	%l0, %o1		/* relocbase */
81*e83f7ba2SBen Gras	call	_rtld
82*e83f7ba2SBen Gras	 add	%sp, BIAS + ARGC, %o0	/* &argc - 16 */
83*e83f7ba2SBen Gras
84*e83f7ba2SBen Gras	ldx	[%sp + BIAS + ARGC], %g3	/* arg: cleanup */
85*e83f7ba2SBen Gras	ldx	[%sp + BIAS + ARGC + 8], %g2	/* arg: obj */
86*e83f7ba2SBen Gras	add	%sp, 48+16, %sp		/* restore stack pointer */
87*e83f7ba2SBen Gras
88*e83f7ba2SBen Gras	jmp	%o0
89*e83f7ba2SBen Gras	 mov	%l1, %g1		/* restore ps_strings */
90*e83f7ba2SBen Gras
91*e83f7ba2SBen Gras
92*e83f7ba2SBen Gras	/*
93*e83f7ba2SBen Gras	 * We have two separate entry points to the runtime linker.
94*e83f7ba2SBen Gras	 * I'm implementing this following the SPARC v9 ABI spec.
95*e83f7ba2SBen Gras	 *
96*e83f7ba2SBen Gras	 * _rtld_bind_start_0(y, x) is called from .PLT0, and is used for
97*e83f7ba2SBen Gras	 * PLT entries above 32768.
98*e83f7ba2SBen Gras	 *
99*e83f7ba2SBen Gras	 * _rtld_bind_start_1(y, x) is called from .PLT1, and is used for
100*e83f7ba2SBen Gras	 * PLT entries below 32768.
101*e83f7ba2SBen Gras	 *
102*e83f7ba2SBen Gras	 * The first two entries of PLT2 contain the xword object pointer.
103*e83f7ba2SBen Gras	 *
104*e83f7ba2SBen Gras	 * These routines are called with two longword arguments,
105*e83f7ba2SBen Gras	 * x and y.  To calculate the address of the entry,
106*e83f7ba2SBen Gras	 * _rtld_bind_start_1(y, x) does:
107*e83f7ba2SBen Gras	 *
108*e83f7ba2SBen Gras	 *	n = x >> 15;
109*e83f7ba2SBen Gras	 *
110*e83f7ba2SBen Gras	 * and _rtld_bind_start_0(y, x) does:
111*e83f7ba2SBen Gras	 *
112*e83f7ba2SBen Gras	 *	i = x - y + 8 - 32768*32;
113*e83f7ba2SBen Gras	 *	n = 32768 + (i/5120)*160 + (i%5120)/24;
114*e83f7ba2SBen Gras	 *
115*e83f7ba2SBen Gras	 * Neither routine needs to issue a save since it's already been
116*e83f7ba2SBen Gras	 * done in the PLT entry.
117*e83f7ba2SBen Gras	 */
118*e83f7ba2SBen Gras
119*e83f7ba2SBen Gras	.section	".text"
120*e83f7ba2SBen Gras	.align	4
121*e83f7ba2SBen Gras	.global	_rtld_bind_start_0
122*e83f7ba2SBen Gras	.type	_rtld_bind_start_0,@function
123*e83f7ba2SBen Gras_rtld_bind_start_0:	# (y, x)
124*e83f7ba2SBen Gras	/* %o0 = obj->pltgot[6] */
125*e83f7ba2SBen Gras	/* %o1 = plt[4] */
126*e83f7ba2SBen Gras	/* %o1 - %o0 + 8 == offset of plt[] from obj->pltgot[] */
127*e83f7ba2SBen Gras	/* -32768*32 to get offset from beginning of upper PLT section */
128*e83f7ba2SBen Gras
129*e83f7ba2SBen Gras	sethi	%hi(32768*32-8), %l1
130*e83f7ba2SBen Gras	sub	%o1, %o0, %l0		/* i = x - y */
131*e83f7ba2SBen Gras	or	%l1, %lo(32768*32-8), %l1
132*e83f7ba2SBen Gras	sub	%l0, %l1, %l0		/* i = x - y + 8 - 32768*32 */
133*e83f7ba2SBen Gras
134*e83f7ba2SBen Gras	ldx	[%o0 + (10*4)], %o0	/* Load object pointer from PLT2 */
135*e83f7ba2SBen Gras
136*e83f7ba2SBen Gras	sethi	%hi(5120), %l1
137*e83f7ba2SBen Gras	sdivx	%l0, %l1, %l1		/* Calculate i/5120 */
138*e83f7ba2SBen Gras	sllx	%l1, 2, %l2
139*e83f7ba2SBen Gras	add	%l2, %l1, %l2
140*e83f7ba2SBen Gras	sllx	%l2, 10, %l2
141*e83f7ba2SBen Gras	sub	%l0, %l2, %l2		/* And i%5120 */
142*e83f7ba2SBen Gras
143*e83f7ba2SBen Gras	/* Let the division churn for a bit. */
144*e83f7ba2SBen Gras	sdivx	%l2, 24, %l4		/* (i%5120)/24 */
145*e83f7ba2SBen Gras
146*e83f7ba2SBen Gras	/* 160 is (32 * 5) or (32 * (4 + 1)) */
147*e83f7ba2SBen Gras	sllx	%l1, 2, %l3		/* 4 * (i/5120) */
148*e83f7ba2SBen Gras	add	%l1, %l3, %l3		/* 5 * (i/5120) */
149*e83f7ba2SBen Gras	sllx	%l3, 5, %l3		/* 32 * 5 * (i/5120) */
150*e83f7ba2SBen Gras
151*e83f7ba2SBen Gras	sethi	%hi(32768), %l6
152*e83f7ba2SBen Gras	add	%l3, %l4, %l5		/* %l5 = (i/5120)*160 + (i%5120)/24; */
153*e83f7ba2SBen Gras
154*e83f7ba2SBen Gras	call	_rtld_bind		/* Call _rtld_bind(obj, offset) */
155*e83f7ba2SBen Gras	 add	%l5, %l6, %o1		/* %o1 = 32768 + ... */
156*e83f7ba2SBen Gras
157*e83f7ba2SBen Gras	jmp	%o0			/* return value == function address */
158*e83f7ba2SBen Gras	 restore			/* Dump our stack frame */
159*e83f7ba2SBen Gras
160*e83f7ba2SBen Gras	.section	".text"
161*e83f7ba2SBen Gras	.align	4
162*e83f7ba2SBen Gras	.global	_rtld_bind_start_1
163*e83f7ba2SBen Gras	.type	_rtld_bind_start_1,@function
164*e83f7ba2SBen Gras_rtld_bind_start_1:	# (y, x)
165*e83f7ba2SBen Gras	ldx	[%o0 + (2*4)], %o0	/* Load object pointer from PLT2 */
166*e83f7ba2SBen Gras
167*e83f7ba2SBen Gras	call	_rtld_bind		/* Call _rtld_bind(obj, offset) */
168*e83f7ba2SBen Gras	 srax	%o1, 15, %o1		/* %o1 is the index to our PLT slot */
169*e83f7ba2SBen Gras
170*e83f7ba2SBen Gras	jmp	%o0			/* return value == function address */
171*e83f7ba2SBen Gras	 restore			/* Dump our stack frame */
172*e83f7ba2SBen Gras
173