Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / csu / sparc / crt0.c
blobdc228800f9d75629801679d4d4622747c4007b98
1 /* $NetBSD: crt0.c,v 1.28 2005/12/24 21:38:40 perry Exp $ */
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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 <stdlib.h>
34 #include "common.h"
36 extern void start __P((void)) __asm ("start");
38 #if defined(sun) && defined(sparc)
39 static void __call __P((void));
40 #endif
42 #ifdef __NetBSD__
43 #undef mmap
44 #define mmap(addr, len, prot, flags, fd, off) \
45 __syscall2((quad_t)SYS_mmap, (addr), (len), (prot), (flags), \
46 (fd), 0, (off_t)(off))
47 extern int __syscall2 __P((quad_t, ...));
48 #endif
50 __asm("
51 .text
52 .align 4
53 .global start
54 start:
55 mov 0, %fp
56 ld [%sp + 64], %l0 ! get argc
57 add %sp, 68, %l1 ! get argv
58 sll %l0, 2, %l2 !
59 add %l2, 4, %l2 ! envp = argv + (argc << 2) + 4
60 add %l1, %l2, %l2 !
61 sethi %hi(_environ), %l3
62 st %l2, [%l3+%lo(_environ)] ! *environ = l2
63 ");
66 * We get a pointer to PSSTRINGS in %g1.
68 __asm("
69 cmp %g1, 0
70 be 1f
71 sethi %hi(___ps_strings), %l3
72 st %g1, [%l3+%lo(___ps_strings)]
74 ");
77 * Finish diddling with stack.
79 __asm("
80 andn %sp, 7, %sp ! align
81 sub %sp, 24, %sp ! expand to standard stack frame size
82 ");
85 * Set __progname:
86 * if (argv[0])
87 * if ((__progname = _strrchr(argv[0], '/')) == NULL)
88 * __progname = argv[0];
89 * else
90 * ++__progname;
92 __asm("
93 ld [%l1], %o0
94 cmp %o0, 0
95 mov %o0, %l6
96 be 1f
97 sethi %hi(___progname), %l7
98 ");
99 #ifdef DYNAMIC
100 __asm("call __strrchr");
101 #else
102 __asm("call _strrchr");
103 #endif
104 __asm("
105 mov 47, %o1
106 cmp %o0, 0
107 be,a 1f
108 st %l6, [%l7+%lo(___progname)]
109 add %o0, 1, %o0
110 st %o0, [%l7+%lo(___progname)]
114 #ifdef DYNAMIC
115 /* Resolve symbols in dynamic libraries */
116 __asm("
117 sethi %hi(__DYNAMIC), %o0
118 orcc %o0, %lo(__DYNAMIC), %o0
119 be 1f
121 call ___load_rtld
125 #endif
127 /* From here, all symbols should have been resolved, so we can use libc */
128 #ifdef MCRT0
130 * atexit(_mcleanup);
131 * monstartup((u_long)&eprol, (u_long)&etext);
133 __asm("
134 sethi %hi(__mcleanup), %o0
135 call _atexit
136 or %o0, %lo(__mcleanup), %o0
137 sethi %hi(_eprol), %o0
138 or %o0, %lo(_eprol), %o0
139 sethi %hi(_etext), %o1
140 call _monstartup
141 or %o1, %lo(_etext), %o1
143 #endif
145 #ifdef sun
147 * SunOS compatibility
149 __asm("
150 call start_float
153 #endif
156 * Move `argc', `argv', and `envp' from locals to parameters for `main'.
158 __asm("
159 mov %l0,%o0
160 mov %l1,%o1
161 __callmain:
162 call _main
163 mov %l2,%o2
165 call _exit
169 #ifdef DYNAMIC
171 * System call entry
173 __asm("
174 .set SYSCALL_G2RFLAG, 0x400
175 .set SYS___syscall, 198
176 ___syscall2:
177 sethi %hi(SYS___syscall), %g1
178 ba 1f
179 or %g1, %lo(SYS___syscall), %g1
180 ___syscall:
181 clr %g1
183 or %g1, SYSCALL_G2RFLAG, %g1
184 add %o7, 8, %g2
185 ta %g0
186 mov -0x1, %o0
187 jmp %o7 + 0x8
188 mov -0x1, %o1
190 #endif
192 #ifdef sun
193 static
194 __call()
197 * adjust the C generated pointer to the crt struct to the
198 * likings of ld.so, which is an offset relative to its %fp
200 __asm("
201 mov %i0, %o0
202 mov %i1, %o1
203 call %i2
204 sub %o1, %sp, %o1
206 /*NOTREACHED, control is transferred directly to our caller */
208 #endif
210 #include "common.c"
212 #if defined(LIBC_SCCS) && !defined(lint)
213 __RCSID("$NetBSD: crt0.c,v 1.28 2005/12/24 21:38:40 perry Exp $");
214 #endif /* LIBC_SCCS and not lint */
216 #ifdef MCRT0
217 __asm ("
218 .text
219 _eprol:
221 #endif