1 /* $NetBSD: rtld_start.S,v 1.2 2015/03/27 23:14:53 matt Exp $ */
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas of 3am Software Foundry.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <machine/asm.h>
34 .globl _C_LABEL(_rtld_relocate_nonplt_self)
35 .globl _C_LABEL(_rtld)
39 * ___start(void (*cleanup)(void),
40 * const Obj_Entry *obj,
41 * struct ps_strings *ps_strings)
44 move s0, sp # save stack pointer
45 addi sp, sp, -4*__SIZEOF_POINTER__
46 # adjust stack pointer
47 # -> 2*PTR_SIZE(sp) for atexit
48 # -> 3*PTR_SIZE(sp) for obj_main
49 move s1, a2 # save ps_strings pointer
51 .L0: auipc gp, %pcrel_hi(_GLOBAL_OFFSET_TABLE_)
52 PTR_L t0, %pcrel_lo(.L0)(gp) # &_DYNAMIC
53 .L1: auipc a0, %pcrel_hi(_DYNAMIC)
54 addi a0, a0, %pcrel_lo(.L1)
55 sub s2, a0, t0 # save for _rtld
57 call _C_LABEL(_rtld_relocate_nonplt_self)
59 move a1, s2 # relocbase
60 addi a0, sp, 2*__SIZEOF_POINTER__ # sp
61 call _C_LABEL(_rtld) # a0 = _rtld(sp, relocbase)
64 PTR_L a0, 2*__SIZEOF_POINTER__(sp) # cleanup function
65 PTR_L a1, 3*__SIZEOF_POINTER__(sp) # obj_main entry
66 move a2, s1 # restore ps_strings
67 move sp, s0 # readjust stack
68 move s0, zero # break stack chain
69 jr t0 # _start(cleanup, obj_main, ps_strings);
72 #define XCALLFRAME_SIZ (12*SZREG)
73 #define XCALLFRAME_RA (8*SZREG)
74 #define XCALLFRAME_A7 (7*SZREG)
75 #define XCALLFRAME_A6 (6*SZREG)
76 #define XCALLFRAME_A5 (5*SZREG)
77 #define XCALLFRAME_A4 (4*SZREG)
78 #define XCALLFRAME_A3 (3*SZREG)
79 #define XCALLFRAME_A2 (2*SZREG)
80 #define XCALLFRAME_A1 (1*SZREG)
81 #define XCALLFRAME_A0 (0*SZREG)
87 ENTRY_NP(_rtld_bind_start)
88 addi sp, sp, -XCALLFRAME_SIZ // save arguments on stack
89 REG_S a0, XCALLFRAME_A0(sp)
90 REG_S a1, XCALLFRAME_A1(sp)
91 REG_S a2, XCALLFRAME_A2(sp)
92 REG_S a3, XCALLFRAME_A3(sp)
93 REG_S a4, XCALLFRAME_A4(sp)
94 REG_S a5, XCALLFRAME_A5(sp)
95 REG_S a6, XCALLFRAME_A6(sp)
96 REG_S a7, XCALLFRAME_A7(sp)
97 REG_S ra, XCALLFRAME_RA(sp)
99 mv a0, t0 /* object from got.plt[1] */
100 mv a1, t1 /* reloc offset */
102 call _C_LABEL(_rtld_bind)
103 mv t0, a0 /* save function pointer */
105 REG_L a0, XCALLFRAME_A0(sp)
106 REG_L a1, XCALLFRAME_A1(sp)
107 REG_L a2, XCALLFRAME_A2(sp)
108 REG_L a3, XCALLFRAME_A3(sp)
109 REG_L a4, XCALLFRAME_A4(sp)
110 REG_L a5, XCALLFRAME_A5(sp)
111 REG_L a6, XCALLFRAME_A6(sp)
112 REG_L a7, XCALLFRAME_A7(sp)
113 REG_L ra, XCALLFRAME_RA(sp)
114 addi sp, sp, XCALLFRAME_SIZ
116 END(_rtld_bind_start)