rtld_start.S revision 1.6 1 /* $NetBSD: rtld_start.S,v 1.6 2023/05/07 12:41:48 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 .globl _C_LABEL(_rtld_relocate_nonplt_self)
35 .globl _C_LABEL(_rtld)
36
37 #define PTRSZ __SIZEOF_POINTER__
38
39 /*
40 * void
41 * ___start(
42 * void (*cleanup)(void),
43 * struct ps_strings *ps_strings
44 * )
45 */
46
47 ENTRY(_rtld_start)
48 mv s0, sp # save stack pointer
49 addi sp, sp, -4 * PTRSZ # adjust stack pointer
50 # -> 2 * PTR_SIZE(sp) for atexit
51 # -> 3 * PTR_SIZE(sp) for obj_main
52 mv s1, a1 # save ps_strings pointer
53
54 .L0: auipc gp, %pcrel_hi(_GLOBAL_OFFSET_TABLE_)
55 PTR_L t0, %pcrel_lo(.L0)(gp) # &_DYNAMIC
56 .L1: auipc a0, %pcrel_hi(_DYNAMIC)
57 addi a0, a0, %pcrel_lo(.L1)
58 sub s2, a0, t0 # save for _rtld
59 mv a1, s2
60 call _C_LABEL(_rtld_relocate_nonplt_self)
61
62 mv a1, s2 # relocbase
63 addi a0, sp, 2 * PTRSZ # sp
64 call _C_LABEL(_rtld) # a0 = _rtld(sp, relocbase)
65 mv t0, a0
66
67 PTR_L a0, 2 * PTRSZ(sp) # cleanup function
68 // PTR_L a1, 3 * PTRSZ(sp) # obj_main pointer (not used)
69 mv a1, s1 # restore ps_strings
70 mv sp, s0 # restore stack pointer
71 mv s0, zero # break stack chain
72 jr t0 # _start(cleanup, ps_strings);
73 END(_rtld_start)
74
75 /*
76 * Needs to be 12 so that stack alignment is preserved on both RV32 and RV64.
77 */
78
79 #define XCALLFRAME_SIZ (12 * SZREG)
80 #define XCALLFRAME_RA ( 8 * SZREG)
81 #define XCALLFRAME_A7 ( 7 * SZREG)
82 #define XCALLFRAME_A6 ( 6 * SZREG)
83 #define XCALLFRAME_A5 ( 5 * SZREG)
84 #define XCALLFRAME_A4 ( 4 * SZREG)
85 #define XCALLFRAME_A3 ( 3 * SZREG)
86 #define XCALLFRAME_A2 ( 2 * SZREG)
87 #define XCALLFRAME_A1 ( 1 * SZREG)
88 #define XCALLFRAME_A0 ( 0 * SZREG)
89
90 /*
91 * t0 = obj pointer
92 * t1 = reloc offset
93 */
94 ENTRY_NP(_rtld_bind_start)
95 addi sp, sp, -XCALLFRAME_SIZ // save arguments on stack
96 REG_S a0, XCALLFRAME_A0(sp)
97 REG_S a1, XCALLFRAME_A1(sp)
98 REG_S a2, XCALLFRAME_A2(sp)
99 REG_S a3, XCALLFRAME_A3(sp)
100 REG_S a4, XCALLFRAME_A4(sp)
101 REG_S a5, XCALLFRAME_A5(sp)
102 REG_S a6, XCALLFRAME_A6(sp)
103 REG_S a7, XCALLFRAME_A7(sp)
104 REG_S ra, XCALLFRAME_RA(sp)
105
106 mv a0, t0 /* object from got.plt[1] */
107 mv a1, t1 /* reloc offset */
108
109 call _C_LABEL(_rtld_bind)
110 mv t0, a0 /* save function pointer */
111
112 REG_L a0, XCALLFRAME_A0(sp)
113 REG_L a1, XCALLFRAME_A1(sp)
114 REG_L a2, XCALLFRAME_A2(sp)
115 REG_L a3, XCALLFRAME_A3(sp)
116 REG_L a4, XCALLFRAME_A4(sp)
117 REG_L a5, XCALLFRAME_A5(sp)
118 REG_L a6, XCALLFRAME_A6(sp)
119 REG_L a7, XCALLFRAME_A7(sp)
120 REG_L ra, XCALLFRAME_RA(sp)
121 addi sp, sp, XCALLFRAME_SIZ
122 jr t0
123 END(_rtld_bind_start)
124