rtld_start.S revision 1.11 1 1.11 mycroft /* $NetBSD: rtld_start.S,v 1.11 2002/09/11 18:18:37 mycroft Exp $ */
2 1.1 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
5 1.1 cgd * All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. The name of the author may not be used to endorse or promote products
16 1.1 cgd * derived from this software without specific prior written permission.
17 1.1 cgd *
18 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 cgd */
29 1.1 cgd
30 1.1 cgd #include <machine/asm.h>
31 1.1 cgd
32 1.2 thorpej .extern _GLOBAL_OFFSET_TABLE_
33 1.1 cgd
34 1.2 thorpej /*
35 1.2 thorpej * Note: we can call ourselves LEAF even though we use callee-saved
36 1.2 thorpej * registers because we're the root of the call graph.
37 1.2 thorpej */
38 1.2 thorpej LEAF_NOPROFILE(_rtld_start, 0)
39 1.1 cgd .set noreorder
40 1.6 thorpej br pv, 1f
41 1.6 thorpej 1: LDGP(pv)
42 1.1 cgd
43 1.11 mycroft /*
44 1.11 mycroft * Relocate ourself.
45 1.11 mycroft */
46 1.6 thorpej br t2, 2f /* get our PC */
47 1.6 thorpej 2: ldiq t3, 2b /* get where the linker thought we were */
48 1.11 mycroft subq t2, t3, a1 /* calculate the displacement */
49 1.1 cgd
50 1.11 mycroft lda t5, _DYNAMIC
51 1.11 mycroft addq a1, t5, a0 /* add the displacement */
52 1.1 cgd
53 1.11 mycroft bsr ra, _rtld_relocate_nonplt_self
54 1.11 mycroft LDGP(ra)
55 1.3 thorpej
56 1.3 thorpej /*
57 1.3 thorpej * Allocate space on the stack for the cleanup and obj_main
58 1.3 thorpej * entries that _rtld() will provide for us.
59 1.3 thorpej */
60 1.3 thorpej lda sp, -16(sp)
61 1.3 thorpej
62 1.3 thorpej mov sp, a0 /* v0 = _rtld(sp); */
63 1.3 thorpej CALL(_rtld)
64 1.3 thorpej
65 1.3 thorpej ldq a1, 0(sp) /* cleanup */
66 1.3 thorpej ldq a2, 8(sp) /* obj_main */
67 1.3 thorpej lda sp, 16(sp) /* pop stack */
68 1.3 thorpej
69 1.3 thorpej mov sp, a0 /* stack pointer */
70 1.3 thorpej mov s0, a3 /* ps_strings */
71 1.3 thorpej
72 1.3 thorpej mov v0, pv /* set up PV for entry point */
73 1.1 cgd
74 1.2 thorpej jsr ra, (v0), 0 /* (*_start)(sp, cleanup, obj, ps_strings); */
75 1.1 cgd ldgp gp, 0(ra)
76 1.1 cgd
77 1.1 cgd CALL(exit)
78 1.1 cgd halt
79 1.1 cgd END(_rtld_start)
80 1.1 cgd
81 1.7 thorpej #define RTLD_BIND_START_PROLOGUE \
82 1.7 thorpej /* at_reg already used by PLT code. */ \
83 1.7 thorpej .set noat ; \
84 1.7 thorpej \
85 1.7 thorpej /* \
86 1.7 thorpej * Allocate stack frame and preserve all registers that the \
87 1.7 thorpej * caller would have normally saved themselves. \
88 1.7 thorpej */ \
89 1.7 thorpej lda sp, -168(sp) ; \
90 1.7 thorpej stq ra, 0(sp) ; \
91 1.7 thorpej stq v0, 8(sp) ; \
92 1.7 thorpej stq t0, 16(sp) ; \
93 1.7 thorpej stq t1, 24(sp) ; \
94 1.7 thorpej stq t2, 32(sp) ; \
95 1.7 thorpej stq t3, 40(sp) ; \
96 1.7 thorpej stq t4, 48(sp) ; \
97 1.7 thorpej stq t5, 56(sp) ; \
98 1.7 thorpej stq t6, 64(sp) ; \
99 1.7 thorpej stq t7, 72(sp) ; \
100 1.7 thorpej stq a0, 80(sp) ; \
101 1.7 thorpej stq a1, 88(sp) ; \
102 1.7 thorpej stq a2, 96(sp) ; \
103 1.7 thorpej stq a3, 104(sp) ; \
104 1.7 thorpej stq a4, 112(sp) ; \
105 1.7 thorpej stq a5, 120(sp) ; \
106 1.7 thorpej stq t8, 128(sp) ; \
107 1.7 thorpej stq t9, 136(sp) ; \
108 1.7 thorpej stq t10, 144(sp) ; \
109 1.7 thorpej stq t11, 152(sp) ; \
110 1.7 thorpej stq gp, 160(sp) ; \
111 1.7 thorpej \
112 1.7 thorpej /* \
113 1.7 thorpej * Load our global pointer. Note, can't use pv, since it is \
114 1.7 thorpej * already used by the PLT code. \
115 1.7 thorpej */ \
116 1.7 thorpej br t0, 1f ; \
117 1.7 thorpej 1: LDGP(t0)
118 1.7 thorpej
119 1.7 thorpej #define RTLD_BIND_START_EPILOGUE \
120 1.7 thorpej /* Move the destination address into position. */ \
121 1.7 thorpej mov v0, pv ; \
122 1.7 thorpej \
123 1.7 thorpej /* Restore program registers. */ \
124 1.7 thorpej ldq ra, 0(sp) ; \
125 1.7 thorpej ldq v0, 8(sp) ; \
126 1.7 thorpej ldq t0, 16(sp) ; \
127 1.7 thorpej ldq t1, 24(sp) ; \
128 1.7 thorpej ldq t2, 32(sp) ; \
129 1.7 thorpej ldq t3, 40(sp) ; \
130 1.7 thorpej ldq t4, 48(sp) ; \
131 1.7 thorpej ldq t5, 56(sp) ; \
132 1.7 thorpej ldq t6, 64(sp) ; \
133 1.7 thorpej ldq t7, 72(sp) ; \
134 1.7 thorpej ldq a0, 80(sp) ; \
135 1.7 thorpej ldq a1, 88(sp) ; \
136 1.7 thorpej ldq a2, 96(sp) ; \
137 1.7 thorpej ldq a3, 104(sp) ; \
138 1.7 thorpej ldq a4, 112(sp) ; \
139 1.7 thorpej ldq a5, 120(sp) ; \
140 1.7 thorpej ldq t8, 128(sp) ; \
141 1.7 thorpej ldq t9, 136(sp) ; \
142 1.7 thorpej ldq t10, 144(sp) ; \
143 1.7 thorpej ldq t11, 152(sp) ; \
144 1.7 thorpej ldq gp, 160(sp) ; \
145 1.7 thorpej /* XXX LDGP? */ \
146 1.7 thorpej \
147 1.10 thorpej /* \
148 1.10 thorpej * We've patched the PLT; sync the I-stream. \
149 1.10 thorpej */ \
150 1.10 thorpej imb ; \
151 1.10 thorpej \
152 1.7 thorpej /* Pop the stack frame and turn control to the destination. */ \
153 1.7 thorpej lda sp, 168(sp) ; \
154 1.7 thorpej jmp zero, (pv)
155 1.7 thorpej
156 1.2 thorpej /*
157 1.2 thorpej * Lazy binding entry point, called via PLT.
158 1.2 thorpej */
159 1.2 thorpej NESTED_NOPROFILE(_rtld_bind_start, 0, 168, ra, 0, 0)
160 1.2 thorpej
161 1.7 thorpej RTLD_BIND_START_PROLOGUE
162 1.2 thorpej
163 1.1 cgd /* Set up the arguments for _rtld_bind. */
164 1.5 thorpej subq at_reg, pv, a1 /* calculate offset of reloc entry */
165 1.5 thorpej ldq a0, 8(pv) /* object structure */
166 1.5 thorpej subq a1, 20, a1 /* = (at - pv - 20) / 12 * 24 */
167 1.5 thorpej addq a1, a1, a1
168 1.7 thorpej
169 1.7 thorpej CALL(_rtld_bind)
170 1.7 thorpej
171 1.7 thorpej RTLD_BIND_START_EPILOGUE
172 1.7 thorpej
173 1.7 thorpej END(_rtld_bind_start)
174 1.7 thorpej
175 1.7 thorpej /*
176 1.7 thorpej * Lazy binding entry point, called via PLT. This version is for the
177 1.7 thorpej * old PLT entry format.
178 1.7 thorpej */
179 1.7 thorpej NESTED_NOPROFILE(_rtld_bind_start_old, 0, 168, ra, 0, 0)
180 1.7 thorpej
181 1.7 thorpej RTLD_BIND_START_PROLOGUE
182 1.7 thorpej
183 1.7 thorpej /* Set up the arguments for _rtld_bind. */
184 1.2 thorpej ldq a0, 8(pv) /* object structure */
185 1.1 cgd mov at_reg, a1 /* offset of reloc entry */
186 1.7 thorpej
187 1.1 cgd CALL(_rtld_bind)
188 1.1 cgd
189 1.7 thorpej RTLD_BIND_START_EPILOGUE
190 1.1 cgd
191 1.7 thorpej END(_rtld_bind_start_old)
192