initial import
[glibc.git] / sysdeps / unix / sparc / start.c
blobe0f39f5b7380bb884331f17937cdc84667b0f044
1 /* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <ansidecl.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <fcntl.h>
25 #ifndef NO_SHLIB
26 #include <sys/exec.h>
27 #include <sys/types.h>
28 #include <sys/mman.h>
29 #include <link.h>
30 #include <syscall.h>
31 #endif
33 #if !defined (__GNUC__) || __GNUC__ < 2
34 #error This file uses GNU C extensions; you must compile with GCC version 2.
35 #endif
37 /* The first piece of initialized data. */
38 int __data_start = 0;
39 #ifdef HAVE_WEAK_SYMBOLS
40 weak_alias (__data_start, data_start)
41 #endif
43 VOLATILE int errno;
45 #ifdef HAVE_WEAK_SYMBOLS
46 weak_alias (__environ, environ)
47 #else
48 #undef environ
49 #define __environ environ
50 #endif
52 char **__environ;
54 extern void EXFUN(__libc_init, (int argc, char **argv, char **envp));
55 extern int EXFUN(main, (int argc, char **argv, char **envp));
57 register long int sp asm("%sp"), fp asm("%fp");
59 #ifndef NO_SHLIB
60 static void EXFUN(init_shlib, (NOARGS));
61 #endif
63 #ifndef NO_EXPLICIT_START
64 /* Declare _start with an explicit assembly symbol name of `start'
65 (note no leading underscore). This is the name Sun's crt0.o uses,
66 and programs are often linked with `ld -e start'. */
67 void _start (void) asm ("start");
68 #endif
70 void
71 _start (void)
73 /* It is important that these be declared `register'.
74 Otherwise, when compiled without optimization, they are put on the
75 stack, which loses completely after we zero the FP. */
76 register int argc;
77 register char **argv, **envp;
79 /* Unwind the frame built when we entered the function. */
80 asm("restore");
82 /* And clear the frame pointer. */
83 fp = 0;
85 /* The argument info starts after one register
86 window (64 bytes) past the SP. */
87 argc = ((int *) sp)[16];
88 argv = (char **) &((int *) sp)[17];
89 envp = &argv[argc + 1];
90 __environ = envp;
92 #ifndef NO_SHLIB
93 init_shlib ();
94 #endif
96 /* Allocate 24 bytes of stack space for the register save area. */
97 sp -= 24;
98 __libc_init (argc, argv, envp);
100 exit (main (argc, argv, envp));
103 #ifndef NO_SHLIB
105 /* System calls for use by the bootstrap routine.
106 These are defined here since the usual calls may be dynamically linked. */
108 int syscall (int sysno, ...) asm ("init_syscall");
109 asm ("init_syscall:\n"
110 " clr %g1\n"
111 " ta 0\n"
112 " bcc 1f\n"
113 " sethi %hi(_errno), %g1\n"
114 " st %o0, [%g1 + %lo(_errno)]\n"
115 " sub %g0, 1, %o0\n"
116 "1:retl\n"
117 " nop");
119 static void
120 DEFUN_VOID(init_shlib)
122 extern struct link_dynamic _DYNAMIC;
123 int so, zf;
124 caddr_t somap;
125 caddr_t sodmap;
126 caddr_t sobssmap;
127 void (*ldstart) (int, int);
128 struct exec soexec;
129 struct
131 caddr_t crt_ba;
132 int crt_dzfd;
133 int crt_ldfd;
134 struct link_dynamic *crt_dp;
135 char **crt_ep;
136 caddr_t crt_bp;
137 } soarg;
139 /* If not dynamically linked, do nothing. */
140 if (&_DYNAMIC == 0)
141 return;
143 /* Map in the dynamic linker. */
144 so = syscall (SYS_open, "/usr/lib/ld.so", O_RDONLY);
145 if (syscall (SYS_read, so, &soexec, sizeof (soexec)) != sizeof (soexec)
146 || soexec.a_magic != ZMAGIC)
148 static CONST char emsg[] = "crt0: no /usr/lib/ld.so\n";
150 syscall (SYS_write, 2, emsg, sizeof (emsg) - 1);
151 syscall (SYS_exit, 127);
153 somap = (caddr_t) syscall (SYS_mmap, 0,
154 soexec.a_text + soexec.a_data + soexec.a_bss,
155 PROT_READ | PROT_EXEC, _MAP_NEW | MAP_PRIVATE,
156 so, 0);
157 sodmap = (caddr_t) syscall (SYS_mmap, somap + soexec.a_text, soexec.a_data,
158 PROT_READ | PROT_WRITE | PROT_EXEC,
159 _MAP_NEW | MAP_FIXED | MAP_PRIVATE,
160 so, soexec.a_text);
161 zf = syscall (SYS_open, "/dev/zero", O_RDONLY);
162 if (soexec.a_bss != 0)
163 sobssmap = (caddr_t) syscall (SYS_mmap,
164 somap + soexec.a_text + soexec.a_data,
165 soexec.a_bss,
166 PROT_READ | PROT_WRITE | PROT_EXEC,
167 _MAP_NEW | MAP_FIXED | MAP_PRIVATE,
168 zf, 0);
170 /* Call the entry point of the dynamic linker. */
171 soarg.crt_ba = somap;
172 soarg.crt_dzfd = zf;
173 soarg.crt_ldfd = so;
174 soarg.crt_dp = &_DYNAMIC;
175 soarg.crt_ep = __environ;
176 soarg.crt_bp = (caddr_t) &&retaddr;
178 ldstart = (__typeof (ldstart)) (somap + soexec.a_entry);
179 (*ldstart) (1, (char *) &soarg - (char *) sp);
181 retaddr:
184 #endif