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