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