rtld_start.S revision 1.8 1 /* $NetBSD: rtld_start.S,v 1.8 2002/09/12 17:07:27 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 2000 Eduardo Horvath.
5 * Copyright (c) 1999 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Christos Zoulas and Paul Kranenburg.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <machine/asm.h>
41 #define _LOCORE
42 #include <machine/frame.h>
43
44 /*
45 * ELF:
46 * On startup the stack should contain 16 extended word register save area,
47 * followed by the arg count, etc.
48 *
49 * _rtld() expects the stack pointer to point to two longwords for argument
50 * return followed by argc, etc. We need to create a pointer to
51 * &argc + 16 and pass that in. The return args will be in those locations.
52 *
53 * NB: We are violating the ELF spec by passing a pointer to the ps strings in
54 * %g1 instead of a termination routine.
55 */
56
57 .register %g2,#scratch
58 .register %g3,#scratch
59
60 /* Offset of ARGC from bottom of stack */
61 #define ARGC (16*8)
62
63 .section ".text"
64 .align 4
65 .LLGETPC0:
66 retl
67 add %o7,%l7,%l7
68
69 .section ".text"
70 .align 4
71 .global _rtld_start
72 .type _rtld_start,@function
73 _rtld_start:
74 mov 0, %fp /* Erect a fence post for ourselves */
75 mov %g1, %l1 /* save ps_strings */
76 sub %sp, 16, %sp /* Make room for return args */
77
78 sethi %hi(_GLOBAL_OFFSET_TABLE_-4), %l7
79 call .LLGETPC0
80 add %l7, %lo(_GLOBAL_OFFSET_TABLE_+4), %l7
81 ldx [%l7+_DYNAMIC], %o0
82 ldx [%l7+_GLOBAL_OFFSET_TABLE_], %l0
83
84 sub %l7, %l0, %o1 /* relocbase */
85 call _rtld_relocate_nonplt_self
86 add %o0, %o1, %o0 /* &_DYNAMIC */
87
88 sub %l7, %l0, %o1 /* relocbase */
89 call _rtld
90 add %sp, BIAS + ARGC, %o0 /* &argc - 16 */
91
92 ldx [%sp + BIAS + ARGC], %g3 /* arg: cleanup */
93 ldx [%sp + BIAS + ARGC + 8], %g2 /* arg: obj */
94 add %sp, 16, %sp /* restore stack pointer */
95
96 jmp %o0
97 mov %l1, %g1 /* restore ps_strings */
98
99
100 /*
101 * We have two separate entry points to the runtime linker.
102 * I'm implementing this following the SPARC v9 ABI spec.
103 *
104 * _rtld_bind_start_0(x, y) is called from .PLT0, and is used for
105 * PLT entries above 32768.
106 *
107 * _rtld_bind_start_1(x, y) is called from .PLT1, and is used for
108 * PLT entries below 32768.
109 *
110 * The first two entries of PLT2 contain the xword object pointer.
111 *
112 * These routines are called with two longword arguments,
113 * x and y. To calculate the address of the entry,
114 * _rtld_bind_start_1(x, y) does:
115 *
116 * n = x >> 15;
117 *
118 * and _rtld_bind_start_0(x, y) does:
119 *
120 * i = x - y + 1048596;
121 * n = 32768 + (i/5120)*160 + (i%5120)/24;
122 *
123 * Neither routine needs to issue a save since it's already been
124 * done in the PLT entry.
125 */
126
127 /* NOTE: _rtld_bind_start_0 is untested. Hence the debug stuff */
128
129 .section ".text"
130 .align 4
131 .global _rtld_bind_start_0
132 .type _rtld_bind_start_0,@function
133 _rtld_bind_start_0: # (x, y)
134 #if 0
135 call _rtld_bind_start_0_stub
136 ldx [%o1 + (10*4)], %l7
137 mov %o0, %l5
138
139 sllx %l5, 1, %l6 /* Each element is an Elf_Rela which */
140 add %l6, %l5, %l5 /* is 3 longwords or 24 bytes. */
141 sllx %l5, 3, %o1 /* So multiply by 24. */
142
143 call _rtld_bind
144 mov %l7, %o0
145 #else
146 sethi %hi(1048596), %l1
147 sub %o0, %o1, %l0 /* x - y */
148 or %l1, %lo(1048596), %l1
149 add %l0, %l1, %l0 /* x - y + 1048596 */
150
151 sdivx %l0, 5120, %l1 /* Calculate i/5120 */
152 ldx [%o1 + (10*4)], %o0 /* Load object pointer from PLT2 */
153 sub %l0, %l1, %l2 /* And i%5120 */
154
155 /* Let the division churn for a bit. */
156 sdivx %l2, 14, %l4 /* (i%5120)/24 */
157
158 /* 160 is (32 * 5) or (32 * (4 + 1)) */
159 sllx %l1, 2, %l3 /* 4 * (i/5120) */
160 add %l1, %l3, %l3 /* 5 * (i/5120) */
161 sllx %l3, 5, %l3 /* 32 * 5 * (i/5120) */
162
163 sethi %hi(32768), %l6
164 add %l3, %l4, %l5 /* %l5 = (i/5120)*160 + (i%5120)/24; */
165 add %l5, %l6, %l5
166
167 sllx %l5, 1, %l6 /* Each element is an Elf_Rela which */
168 add %l6, %l5, %l5 /* is 3 longwords or 24 bytes. */
169 sllx %l5, 3, %l5 /* So multiply by 24. */
170
171 call _rtld_bind /* Call _rtld_bind(obj, offset) */
172 mov %l5, %o1
173
174 jmp %o0 /* return value == function address */
175 restore /* Dump our stack frame */
176 #endif
177
178 .section ".text"
179 .align 4
180 .global _rtld_bind_start_1
181 .type _rtld_bind_start_1,@function
182 _rtld_bind_start_1: # (x, y)
183 srax %o0, 15, %o2 /* %o0 is the index to our PLT slot */
184
185 sllx %o2, 1, %o3 /* Each element is an Elf_Rela which */
186 add %o3, %o2, %o2 /* is 3 longwords or 24 bytes. */
187 sllx %o2, 3, %o2 /* So multiply by 24. */
188
189 ldx [%o1 + 8], %o0 /* The object pointer is at [%o1 + 8] */
190
191 call _rtld_bind /* Call _rtld_bind(obj, offset) */
192 mov %o2, %o1
193
194 jmp %o0 /* return value == function address */
195 restore /* Dump our stack frame */
196
197