Home | History | Annotate | Line # | Download | only in aarch64
rtld_start.S revision 1.3
      1 /* $NetBSD: rtld_start.S,v 1.3 2018/09/20 18:41:05 jakllsch 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 /*-
     33  * Copyright (c) 2014 The FreeBSD Foundation
     34  * All rights reserved.
     35  *
     36  * This software was developed by Andrew Turner under
     37  * sponsorship from the FreeBSD Foundation.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  */
     60 
     61 #include <machine/asm.h>
     62 
     63 RCSID("$NetBSD: rtld_start.S,v 1.3 2018/09/20 18:41:05 jakllsch Exp $")
     64 
     65 /*
     66  * void _rtld_start(void (*cleanup)(void), const Obj_Entry *obj,
     67  *    struct ps_strings *ps_strings);
     68  *
     69  * X0		= NULL
     70  * X1		= NULL
     71  * X2		= ps_strings
     72  * X30 (LR)	= 0
     73  * X29 (FP)	= 0
     74  */
     75 ENTRY_NP(_rtld_start)
     76 	mov	x24, x2			/* save ps_strings */
     77 
     78 	adrp	x1, :got:_DYNAMIC	/* load _DYNAMIC offset from GOT */
     79 	ldr	x1, [x1, #:got_lo12:_DYNAMIC]
     80 
     81 	adrp	x0, _DYNAMIC		/* get &_DYNAMIC */
     82 	add	x0, x0, #:lo12:_DYNAMIC
     83 
     84 	sub	x25, x0, x1		/* relocbase = &_DYNAMIC - GOT:_DYNAMIC */
     85 	mov	x1, x25			/* pass as 2nd argument */
     86 	bl	_C_LABEL(_rtld_relocate_nonplt_self)
     87 
     88 	sub	sp, sp, #16		/* reserve space for returns */
     89 	mov	x0, sp			/* pointer to reserved space */
     90 	mov	x1, x25			/* pass relocbase */
     91 	bl	_C_LABEL(_rtld)
     92 	mov	x17, x0			/* save entry point */
     93 
     94 	ldp	x0, x1, [sp], #16	/* pop cleanup & obj_main */
     95 	mov	x2, x24			/* restore ps_strings */
     96 
     97 	br	x17			/* call saved entry point */
     98 END(_rtld_start)
     99 
    100 /*
    101  * Upon entry from plt0 entry:
    102  *
    103  * SP+0		= &PLTGOT[n + 3]
    104  * SP+8		= return addr
    105  * X16		= &PLTGOT[2]
    106  */
    107 ENTRY_NP(_rtld_bind_start)
    108 	ldr	x9, [sp]		/* x9 = &PLTGOT[n+3] */
    109 
    110 	/* save x0-x8 for arguments */
    111 	stp	x0, x1, [sp, #-16]!
    112 	stp	x2, x3, [sp, #-16]!
    113 	stp	x4, x5, [sp, #-16]!
    114 	stp	x6, x7, [sp, #-16]!
    115 	stp	x8, xzr, [sp, #-16]!
    116 
    117 	/* save q0-q7 for arguments */
    118 	stp	q0, q1, [sp, #-32]!
    119 	stp	q2, q3, [sp, #-32]!
    120 	stp	q4, q5, [sp, #-32]!
    121 	stp	q6, q7, [sp, #-32]!
    122 
    123 	ldr	x0, [x16, #-8]	/* x0 = PLTGOT[1] */
    124 	sub	x1, x9, x16	/* x1 = &PLTGOT[n+3] - &PLTGOT[1] = offset+8 */
    125 	sub	x1, x1, #8	/* x1 = offset */
    126 	lsr	x1, x1, #3	/* x1 /= sizeof(void *) */
    127 
    128 	bl	_C_LABEL(_rtld_bind)
    129 	mov	x17, x0		/* save result */
    130 
    131 	/* restore q0-q7 for arguments */
    132 	ldp	q6, q7, [sp], #32
    133 	ldp	q4, q5, [sp], #32
    134 	ldp	q2, q3, [sp], #32
    135 	ldp	q0, q1, [sp], #32
    136 
    137 	/* restore x0-x8 for arguments */
    138 	ldp	x8, xzr, [sp], #16
    139 	ldp	x6, x7, [sp], #16
    140 	ldp	x4, x5, [sp], #16
    141 	ldp	x2, x3, [sp], #16
    142 	ldp	x0, x1, [sp], #16
    143 
    144 	ldp	xzr, lr, [sp], #16	/* restore original lr pushed by plt0 */
    145 	br	x17			/* call bound function */
    146 END(_rtld_bind_start)
    147 
    148 
    149 ENTRY(_rtld_tlsdesc)
    150 	ldr	x0, [x0, #8]
    151 	ret
    152 END(_rtld_tlsdesc)
    153 
    154 /*
    155  * uint64_t _rtld_tlsdesc_dynamic(struct tlsdesc *);
    156  *
    157  * TODO: We could lookup the saved index here to skip saving the entire stack.
    158  */
    159 ENTRY(_rtld_tlsdesc_dynamic)
    160 	/* Store any registers we may use in rtld_tlsdesc_handle */
    161 	stp	x29, x30, [sp, #-(10 * 16)]!
    162 	mov	x29, sp
    163 	stp	x1, x2,   [sp, #(1 * 16)]
    164 	stp	x3, x4,   [sp, #(2 * 16)]
    165 	stp	x5, x6,   [sp, #(3 * 16)]
    166 	stp	x7, x8,   [sp, #(4 * 16)]
    167 	stp	x9, x10,  [sp, #(5 * 16)]
    168 	stp	x11, x12, [sp, #(6 * 16)]
    169 	stp	x13, x14, [sp, #(7 * 16)]
    170 	stp	x15, x16, [sp, #(8 * 16)]
    171 	stp	x17, x18, [sp, #(9 * 16)]
    172 
    173 	/* Find the tls offset */
    174 	ldr	x0, [x0, #8]
    175 	mov	x1, #1
    176 	bl	_rtld_tlsdesc_handle
    177 
    178 	/* Restore the registers */
    179 	ldp	x17, x18, [sp, #(9 * 16)]
    180 	ldp	x15, x16, [sp, #(8 * 16)]
    181 	ldp	x13, x14, [sp, #(7 * 16)]
    182 	ldp	x11, x12, [sp, #(6 * 16)]
    183 	ldp	x9, x10,  [sp, #(5 * 16)]
    184 	ldp	x7, x8,   [sp, #(4 * 16)]
    185 	ldp	x5, x6,   [sp, #(3 * 16)]
    186 	ldp	x3, x4,   [sp, #(2 * 16)]
    187 	ldp	x1, x2,   [sp, #(1 * 16)]
    188 	ldp	x29, x30, [sp], #(10 * 16)
    189 
    190 	ret
    191 END(_rtld_tlsdesc_dynamic)
    192