Home | History | Annotate | Line # | Download | only in sparc64
rtld_start.S revision 1.1
      1 /*	$NetBSD: rtld_start.S,v 1.1 2000/07/13 23:14:18 eeh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 Eduardo Horvath.
      5  * Copyright (c) 1999 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 and Paul Kranenburg.
     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 
     42 /*
     43  * ELF:
     44  *	On startup the stack should contain 16 extended word register save area,
     45  *	followed by the arg count, etc.
     46  *
     47  * _rtld() expects the stack pointer to point to two longwords for argument
     48  *	return followed by argc, etc.  We need to create a pointer to
     49  *	&argc + 16 and pass that in.  The return args will be in those locations.
     50  *
     51  * NB:	We are violating the ELF spec by passing a pointer to the ps strings in
     52  * 	%g1 instead of a termination routine.
     53  */
     54 
     55 /* Offset of ARGC from bottom of stack */
     56 #define	ARGC	(16*8)
     57 
     58 	.section	".text"
     59 	.align	16
     60 	.global	_rtld_start
     61 	.type	_rtld_start,@function
     62 _rtld_start:
     63 	mov	0, %fp			/* Erect a fence post for ourselves */
     64 	mov	%g1, %l1		/* save ps_strings */
     65 	sub	%sp, 16, %sp		/* Make room for return args */
     66 	add	%sp, BIAS+(16*8), %l0	/* Now %l0 points at our return arg space. */
     67 
     68 	call	_rtld
     69 	 mov	%l0, %o0
     70 
     71 	ldx	[%l0], %g3		/* arg: cleanup */
     72 	ldx	[%l0 + 8], %g2		/* arg: obj */
     73 	add	%sp, 16, %sp		/* restore stack pointer */
     74 
     75 	jmp	%o0
     76 	 mov	%l1, %g1		/* restore ps_strings */
     77 
     78 
     79 	/*
     80 	 * We have two separate entry points to the runtime linker.
     81 	 * I'm implementing this following the SPARC
     82 	 *
     83 	 * _rtld_bind_start_0(x, y) is called from .PLT0, and is used for
     84 	 * PLT entries above 32768.
     85 	 *
     86 	 * _rtld_bind_start_1(x, y) is called from .PLT1, and is used for
     87 	 * PLT entries below 32768.
     88 	 *
     89 	 * The first two entries of PLT2 contain the xword object pointer.
     90 	 *
     91 	 * These routines are called with two longword arguments,
     92 	 * x and y.  To calculate the address of the entry,
     93 	 * _rtld_bind_start_1(x, y) does:
     94 	 *
     95 	 *	n = x >> 15;
     96 	 *
     97 	 * and _rtld_bind_start_0(x, y) does:
     98 	 *
     99 	 *	i = x - y + 1048596;
    100 	 *	n = 32768 + (i/5120)*160 + (i%5120)/24;
    101 	 *
    102 	 * Neither routine needs to issue a save since it's already been
    103 	 * done in the PLT entry.
    104 	 */
    105 
    106 	.section	".text"
    107 	.align	4
    108 	.global	_rtld_bind_start
    109 	.type	_rtld_bind_start,@function
    110 _rtld_bind_start_0:	# (obj, reloff)
    111 	sethi	%hi(1048596), %l1
    112 	sub	%o0, %o1, %l0		/* x - y */
    113 	or	%l1, %lo(1048596), %l1
    114 	add	%l0, %l1, %l0		/* x - y + 1048596 */
    115 
    116 	sdivx	%l0, 5120, %l1		/* Calculate i/5120 */
    117 	ldx	[%o1 + (10*4)], %o0	/* Load object pointer from PLT2 */
    118 	sub	%l0, %l1, %l2		/* And i%5120 */
    119 
    120 	/* Let the division churn for a bit. */
    121 	sdivx	%l2, 14, %l4		/* (i%5120)/24 */
    122 
    123 	/* 160 is (32 * 5) or (32 * (4 + 1)) */
    124 	sllx	%l1, 2, %l3		/* 4 * (i/5120) */
    125 	add	%l1, %l3, %l3		/* 5 * (i/5120) */
    126 	sllx	%l3, 5, %l3		/* 32 * 5 * (i/5120) */
    127 
    128 	sethi	%hi(32768), %l6
    129 	add	%l3, %l4, %l5		/* %l5 = (i/5120)*160 + (i%5120)/24; */
    130 	add	%l5, %l6, %l5
    131 	call	_rtld_bind		/* Call _rtld_bind(obj, offset) */
    132 	 sub	%l5, 4, %o1		/* The first 4 entries are reserved. */
    133 	jmp	%o0			/* return value == function address */
    134 	 restore			/* Dump our stack frame */
    135 
    136 
    137 	.section	".text"
    138 	.align	4
    139 	.global	_rtld_bind_start
    140 	.type	_rtld_bind_start,@function
    141 _rtld_bind_start_1:	# (obj, reloff)
    142 	srax	%o0, 15, %o2		/* %o0 points to our PLT slot */
    143 	ldx	[%o1 + 8], %o0		/* The object pointer is at [%o1 + 8] */
    144 	call	_rtld_bind		/* Call _rtld_bind(obj, offset) */
    145 	 sub	%o2, 4*32, %o1		/* The first 4 entries are reserved. */
    146 	jmp	%o0			/* return value == function address */
    147 	 restore			/* Dump our stack frame */
    148 
    149