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