1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
/* $OpenBSD: ldasm.S,v 1.2 2001/09/23 00:14:56 drahn Exp $ */
/* $NetBSD: rtld_start.S,v 1.5 2001/08/14 22:17:48 eeh Exp $ */
/*
* Copyright (c) 2001 Jason L. Wright (jason@thought.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Jason L. Wright
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*-
* Copyright (c) 2000 Eduardo Horvath.
* Copyright (c) 1999 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Christos Zoulas and Paul Kranenburg.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/syscall.h>
#include <machine/trap.h>
#include <machine/asm.h>
#define _LOCORE
#include <machine/frame.h>
/*
* ELF:
* On startup the stack should contain 16 extended word register save area,
* followed by the arg count, etc.
*
* _rtld() expects the stack pointer to point to two longwords for argument
* return followed by argc, etc. We need to create a pointer to
* &argc + 16 and pass that in. The return args will be in those locations.
*
* NB: We are violating the ELF spec by passing a pointer to the ps strings in
* %g1 instead of a termination routine.
*/
/* Offset of ARGC from bottom of stack */
#define ARGC (16*8)
.section ".text"
.align 16
.global _dl_start
.type _dl_start,@function
_dl_start:
sub %g0, %g0, %fp ! clear frame
mov %g1, %l1 ! save ps_strings
sub %sp, 16, %sp ! make room for return args
add %sp, BIAS+ARGC, %l0 ! l0 points to bottom of stack
ldx [%l0], %l3 ! argc
sllx %l3, 3, %l3 ! argc *= sizeof(long)
addx %l0, 8, %o0 ! argv = [%l0 + 8]
addx %l0, 16, %o1 ! envp = %l0 + 16 +
addx %o1, %l3, %o1 ! + argc
! XXX loff ???
mov %o1, %o4 ! %o3 = my_envp
1:
ldx [%o4 + 0], %l2 ! while ((*my_envp)
subxcc %l2, %g0, %g0 ! != NULL)
bnz 1b ! my_envp++;
addx %o4, 8, %o4 ! %o4 == dynp
call _dl_boot ! _dl_boot(argv,envp,loff,dynp,dl_data)
nop
add %sp, 16, %sp ! restore stack
mov %l1, %g1 ! restore ps_strings
jmp %o0
nop
/*
* We have two separate entry points to the runtime linker.
* I'm implementing this following the SPARC v9 ABI spec.
*
* _dl_bind_start_0(x, y) is called from .PLT0, and is used for
* PLT entries above 32768.
*
* _dl_bind_start_1(x, y) is called from .PLT1, and is used for
* PLT entries below 32768.
*
* The first two entries of PLT2 contain the xword object pointer.
*
* These routines are called with two longword arguments,
* x and y. To calculate the address of the entry,
* _dl_bind_start_1(x, y) does:
*
* n = x >> 15;
*
* and _dl_bind_start_0(x, y) does:
*
* i = x - y + 1048596;
* n = 32768 + (i/5120)*160 + (i%5120)/24;
*
* Neither routine needs to issue a save since it's already been
* done in the PLT entry.
*/
/* NOTE: _dl_bind_start_0 is untested. Hence the debug stuff */
.section ".text"
.align 4
.global _dl_bind_start_0
.type _dl_bind_start_0,@function
_dl_bind_start_0: # (x, y)
#if 0
call _dl_bind_start_0_stub
ldx [%o1 + (10*4)], %l7
mov %o0, %l5
sllx %l5, 1, %l6 /* Each element is an Elf_Rela which */
add %l6, %l5, %l5 /* is 3 longwords or 24 bytes. */
sllx %l5, 3, %o1 /* So multiply by 24. */
call _dl_bind
mov %l7, %o0
#else
sethi %hi(1048596), %l1
sub %o0, %o1, %l0 /* x - y */
or %l1, %lo(1048596), %l1
add %l0, %l1, %l0 /* x - y + 1048596 */
sdivx %l0, 5120, %l1 /* Calculate i/5120 */
ldx [%o1 + (10*4)], %o0 /* Load object pointer from PLT2 */
sub %l0, %l1, %l2 /* And i%5120 */
/* Let the division churn for a bit. */
sdivx %l2, 14, %l4 /* (i%5120)/24 */
/* 160 is (32 * 5) or (32 * (4 + 1)) */
sllx %l1, 2, %l3 /* 4 * (i/5120) */
add %l1, %l3, %l3 /* 5 * (i/5120) */
sllx %l3, 5, %l3 /* 32 * 5 * (i/5120) */
sethi %hi(32768), %l6
add %l3, %l4, %l5 /* %l5 = (i/5120)*160 + (i%5120)/24; */
add %l5, %l6, %l5
sllx %l5, 1, %l6 /* Each element is an Elf_Rela which */
add %l6, %l5, %l5 /* is 3 longwords or 24 bytes. */
sllx %l5, 3, %l5 /* So multiply by 24. */
call _dl_bind /* Call _dl_bind(obj, offset) */
mov %l5, %o1
jmp %o0 /* return value == function address */
restore /* Dump our stack frame */
#endif
.section ".text"
.align 4
.global _dl_bind_start_1
.type _dl_bind_start_1,@function
_dl_bind_start_1: # (x, y)
srax %o0, 15, %o2 /* %o0 is the index to our PLT slot */
sllx %o2, 1, %o3 /* Each element is an Elf_Rela which */
add %o3, %o2, %o2 /* is 3 longwords or 24 bytes. */
sllx %o2, 3, %o2 /* So multiply by 24. */
ldx [%o1 + 8], %o0 /* The object pointer is at [%o1 + 8] */
call _dl_bind /* Call _dl_bind(obj, offset) */
mov %o2, %o1
jmp %o0 /* return value == function address */
restore /* Dump our stack frame */
.section ".text"
.align 4
.global _dl_close
.type _dl_close,@function
_dl_close:
mov SYS_close | SYSCALL_G2RFLAG, %g1 ! call sys_close
add %o7, 8, %g2 ! just return on sucess
t ST_SYSCALL ! off to wonderland
retl
sub %g0, %o0, %o0 ! error: result = -errno
.section ".text"
.align 4
.global _dl_exit
.type _dl_exit,@function
_dl_exit:
mov SYS_exit | SYSCALL_G2RFLAG, %g1 ! call sys_exit
add %o7, 8, %g2 ! just return on sucess
t ST_SYSCALL ! off to wonderland
retl
sub %g0, %o0, %o0 ! error: result = -errno
.section ".text"
.align 4
.global _dl_getegid
.type _dl_getegid,@function
_dl_getegid:
mov SYS_getegid | SYSCALL_G2RFLAG, %g1 ! call sys_exit
add %o7, 8, %g2 ! just return on sucess
t ST_SYSCALL ! off to wonderland
retl
sub %g0, %o0, %o0 ! error: result = -errno
.section ".text"
.align 4
.global _dl_geteuid
.type _dl_geteuid,@function
_dl_geteuid:
mov SYS_geteuid | SYSCALL_G2RFLAG, %g1 ! call sys_exit
add %o7, 8, %g2 ! just return on sucess
t ST_SYSCALL ! off to wonderland
retl
sub %g0, %o0, %o0 ! error: result = -errno
.section ".text"
.align 4
.global _dl_getgid
.type _dl_getgid,@function
_dl_getgid:
mov SYS_getgid | SYSCALL_G2RFLAG, %g1 ! call sys_exit
add %o7, 8, %g2 ! just return on sucess
t ST_SYSCALL ! off to wonderland
retl
sub %g0, %o0, %o0 ! error: result = -errno
.section ".text"
.align 4
.global _dl_getuid
.type _dl_getuid,@function
_dl_getuid:
mov SYS_getuid | SYSCALL_G2RFLAG, %g1 ! call sys_exit
add %o7, 8, %g2 ! just return on sucess
t ST_SYSCALL ! off to wonderland
retl
sub %g0, %o0, %o0 ! error: result = -errno
.section ".text"
.align 4
.global _dl_mmap
.type _dl_mmap,@function
_dl_mmap:
mov SYS_mmap | SYSCALL_G2RFLAG, %g1 ! calling sys_mmap
add %o7, 8, %g2 ! just return on sucess
t ST_SYSCALL ! off to wonderland
retl
sub %g0, %o0, %o0 ! error: result = -errno
.section ".text"
.align 4
.global _dl_munmap
.type _dl_munmap,@function
_dl_munmap:
mov SYS_munmap | SYSCALL_G2RFLAG, %g1 ! calling sys_mmap
add %o7, 8, %g2 ! just return on sucess
t ST_SYSCALL ! off to wonderland
retl
sub %g0, %o0, %o0 ! error: result = -errno
.section ".text"
.align 4
.global _dl_mprotect
.type _dl_mprotect,@function
_dl_mprotect:
mov SYS_mprotect | SYSCALL_G2RFLAG, %g1 ! calling sys_mprotect
add %o7, 8, %g2 ! just return on sucess
t ST_SYSCALL ! off to wonderland
retl
sub %g0, %o0, %o0 ! error: result = -errno
.section ".text"
.align 4
.global _dl_open
.type _dl_open,@function
_dl_open:
mov SYS_open | SYSCALL_G2RFLAG, %g1 ! calling sys_open
add %o7, 8, %g2 ! just return on sucess
t ST_SYSCALL ! off to wonderland
retl
sub %g0, %o0, %o0 ! error: result = -errno
.section ".text"
.align 4
.global _dl_read
.type _dl_read,@function
_dl_read:
mov SYS_read | SYSCALL_G2RFLAG, %g1 ! calling sys_read
add %o7, 8, %g2 ! just return on sucess
t ST_SYSCALL ! off to wonderland
retl
sub %g0, %o0, %o0 ! error: result = -errno
.section ".text"
.align 4
.global _dl_write
.type _dl_write,@function
_dl_write:
mov SYS_write | SYSCALL_G2RFLAG, %g1 ! calling sys_write
add %o7, 8, %g2 ! just return on sucess
t ST_SYSCALL ! off to wonderland
retl
sub %g0, %o0, %o0 ! error: result = -errno
.section ".text"
.align 4
.global _dl_stat
.type _dl_stat,@function
_dl_stat:
mov SYS_stat | SYSCALL_G2RFLAG, %g1 ! call sys_stat
add %o7, 8, %g2 ! just return on sucess
t ST_SYSCALL ! off to wonderland
retl
sub %g0, %o0, %o0 ! error: result = -errno
|