rtld_start.S revision 1.2 1 1.2 skrll /* $NetBSD: rtld_start.S,v 1.2 2018/02/04 21:49:51 skrll 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.2 skrll RCSID("$NetBSD: rtld_start.S,v 1.2 2018/02/04 21:49:51 skrll 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 ENTRY_NP(_rtld_start)
47 1.1 matt mov x24, x2 /* save ps_strings */
48 1.1 matt
49 1.1 matt adrp x1, :got:_DYNAMIC /* load _DYNAMIC offset from GOT */
50 1.1 matt ldr x1, [x1, #:got_lo12:_DYNAMIC]
51 1.1 matt
52 1.1 matt adrp x0, _DYNAMIC /* get &_DYNAMIC */
53 1.1 matt add x0, x0, #:lo12:_DYNAMIC
54 1.2 skrll
55 1.1 matt sub x25, x0, x1 /* relocbase = &_DYNAMIC - GOT:_DYNAMIC */
56 1.1 matt mov x1, x25 /* pass as 2nd argument */
57 1.2 skrll bl _C_LABEL(_rtld_relocate_nonplt_self)
58 1.1 matt
59 1.1 matt sub sp, sp, #16 /* reserve space for returns */
60 1.1 matt mov x0, sp /* pointer to reserved space */
61 1.1 matt mov x1, x25 /* pass relocbase */
62 1.2 skrll bl _C_LABEL(_rtld)
63 1.1 matt mov x17, x0 /* save entry point */
64 1.1 matt
65 1.1 matt ldp x0, x1, [sp], #16 /* pop cleanup & obj_main */
66 1.1 matt mov x2, x24 /* restore ps_strings */
67 1.1 matt
68 1.1 matt br x17 /* call saved entry point */
69 1.1 matt END(_rtld_start)
70 1.1 matt
71 1.1 matt /*
72 1.1 matt * Upon entry from plt0 entry:
73 1.2 skrll *
74 1.2 skrll * SP+0 = &PLTGOT[n + 3]
75 1.2 skrll * SP+8 = return addr
76 1.2 skrll * X16 = &PLTGOT[2]
77 1.1 matt */
78 1.1 matt ENTRY_NP(_rtld_bind_start)
79 1.2 skrll ldr x9, [sp] /* x9 = &PLTGOT[n+3] */
80 1.2 skrll
81 1.2 skrll /* save x0-x8 for arguments */
82 1.2 skrll stp x0, x1, [sp, #-16]!
83 1.2 skrll stp x2, x3, [sp, #-16]!
84 1.2 skrll stp x4, x5, [sp, #-16]!
85 1.2 skrll stp x6, x7, [sp, #-16]!
86 1.2 skrll stp x8, xzr, [sp, #-16]!
87 1.2 skrll
88 1.2 skrll /* save q0-q7 for arguments */
89 1.2 skrll stp q0, q1, [sp, #-32]!
90 1.2 skrll stp q2, q3, [sp, #-32]!
91 1.2 skrll stp q4, q5, [sp, #-32]!
92 1.2 skrll stp q6, q7, [sp, #-32]!
93 1.2 skrll
94 1.2 skrll ldr x0, [x16, #-8] /* x0 = PLTGOT[1] */
95 1.2 skrll sub x1, x9, x16 /* x1 = &PLTGOT[n+3] - &PLTGOT[1] = offset+8 */
96 1.2 skrll sub x1, x1, #8 /* x1 = offset */
97 1.2 skrll lsr x1, x1, #3 /* x1 /= sizeof(void *) */
98 1.2 skrll
99 1.2 skrll bl _C_LABEL(_rtld_bind)
100 1.2 skrll mov x17, x0 /* save result */
101 1.2 skrll
102 1.2 skrll /* restore q0-q7 for arguments */
103 1.2 skrll ldp q6, q7, [sp], #32
104 1.2 skrll ldp q4, q5, [sp], #32
105 1.2 skrll ldp q2, q3, [sp], #32
106 1.2 skrll ldp q0, q1, [sp], #32
107 1.2 skrll
108 1.2 skrll /* restore x0-x8 for arguments */
109 1.2 skrll ldp x8, xzr, [sp], #16
110 1.2 skrll ldp x6, x7, [sp], #16
111 1.2 skrll ldp x4, x5, [sp], #16
112 1.2 skrll ldp x2, x3, [sp], #16
113 1.2 skrll ldp x0, x1, [sp], #16
114 1.2 skrll
115 1.2 skrll ldp xzr, lr, [sp], #16 /* restore original lr pushed by plt0 */
116 1.1 matt br x17 /* call bound function */
117 1.1 matt END(_rtld_bind_start)
118 1.2 skrll
119 1.2 skrll
120 1.2 skrll ENTRY(_rtld_tlsdesc)
121 1.2 skrll ldr x0, [x0, #8]
122 1.2 skrll ret
123 1.2 skrll END(_rtld_tlsdesc)
124