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