Home | History | Annotate | Line # | Download | only in aarch64
rtld_start.S revision 1.2
      1 /* $NetBSD: rtld_start.S,v 1.2 2018/02/04 21:49:51 skrll Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matt Thomas of 3am Software Foundry.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <machine/asm.h>
     33 
     34 RCSID("$NetBSD: rtld_start.S,v 1.2 2018/02/04 21:49:51 skrll Exp $")
     35 
     36 /*
     37  * void _rtld_start(void (*cleanup)(void), const Obj_Entry *obj,
     38  *    struct ps_strings *ps_strings);
     39  *
     40  * X0		= NULL
     41  * X1		= NULL
     42  * X2		= ps_strings
     43  * X30 (LR)	= 0
     44  * X29 (FP)	= 0
     45  */
     46 ENTRY_NP(_rtld_start)
     47 	mov	x24, x2			/* save ps_strings */
     48 
     49 	adrp	x1, :got:_DYNAMIC	/* load _DYNAMIC offset from GOT */
     50 	ldr	x1, [x1, #:got_lo12:_DYNAMIC]
     51 
     52 	adrp	x0, _DYNAMIC		/* get &_DYNAMIC */
     53 	add	x0, x0, #:lo12:_DYNAMIC
     54 
     55 	sub	x25, x0, x1		/* relocbase = &_DYNAMIC - GOT:_DYNAMIC */
     56 	mov	x1, x25			/* pass as 2nd argument */
     57 	bl	_C_LABEL(_rtld_relocate_nonplt_self)
     58 
     59 	sub	sp, sp, #16		/* reserve space for returns */
     60 	mov	x0, sp			/* pointer to reserved space */
     61 	mov	x1, x25			/* pass relocbase */
     62 	bl	_C_LABEL(_rtld)
     63 	mov	x17, x0			/* save entry point */
     64 
     65 	ldp	x0, x1, [sp], #16	/* pop cleanup & obj_main */
     66 	mov	x2, x24			/* restore ps_strings */
     67 
     68 	br	x17			/* call saved entry point */
     69 END(_rtld_start)
     70 
     71 /*
     72  * Upon entry from plt0 entry:
     73  *
     74  * SP+0		= &PLTGOT[n + 3]
     75  * SP+8		= return addr
     76  * X16		= &PLTGOT[2]
     77  */
     78 ENTRY_NP(_rtld_bind_start)
     79 	ldr	x9, [sp]		/* x9 = &PLTGOT[n+3] */
     80 
     81 	/* save x0-x8 for arguments */
     82 	stp	x0, x1, [sp, #-16]!
     83 	stp	x2, x3, [sp, #-16]!
     84 	stp	x4, x5, [sp, #-16]!
     85 	stp	x6, x7, [sp, #-16]!
     86 	stp	x8, xzr, [sp, #-16]!
     87 
     88 	/* save q0-q7 for arguments */
     89 	stp	q0, q1, [sp, #-32]!
     90 	stp	q2, q3, [sp, #-32]!
     91 	stp	q4, q5, [sp, #-32]!
     92 	stp	q6, q7, [sp, #-32]!
     93 
     94 	ldr	x0, [x16, #-8]	/* x0 = PLTGOT[1] */
     95 	sub	x1, x9, x16	/* x1 = &PLTGOT[n+3] - &PLTGOT[1] = offset+8 */
     96 	sub	x1, x1, #8	/* x1 = offset */
     97 	lsr	x1, x1, #3	/* x1 /= sizeof(void *) */
     98 
     99 	bl	_C_LABEL(_rtld_bind)
    100 	mov	x17, x0		/* save result */
    101 
    102 	/* restore q0-q7 for arguments */
    103 	ldp	q6, q7, [sp], #32
    104 	ldp	q4, q5, [sp], #32
    105 	ldp	q2, q3, [sp], #32
    106 	ldp	q0, q1, [sp], #32
    107 
    108 	/* restore x0-x8 for arguments */
    109 	ldp	x8, xzr, [sp], #16
    110 	ldp	x6, x7, [sp], #16
    111 	ldp	x4, x5, [sp], #16
    112 	ldp	x2, x3, [sp], #16
    113 	ldp	x0, x1, [sp], #16
    114 
    115 	ldp	xzr, lr, [sp], #16	/* restore original lr pushed by plt0 */
    116 	br	x17			/* call bound function */
    117 END(_rtld_bind_start)
    118 
    119 
    120 ENTRY(_rtld_tlsdesc)
    121 	ldr	x0, [x0, #8]
    122 	ret
    123 END(_rtld_tlsdesc)
    124