2 * Copyright (c) 1993 Paul Kranenburg
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Paul Kranenburg.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * $FreeBSD: src/lib/csu/i386/crt0.c,v 1.36 1999/08/27 23:57:55 peter Exp $
33 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/syscall.h>
46 * This is gross, ld.so is a ZMAGIC a.out, but has `sizeof(hdr)' for
47 * an entry point and not at PAGSIZ as the N_*ADDR macros assume.
50 #define N_DATADDR(x) ((x).a_text)
53 #define N_BSSADDR(x) ((x).a_text + (x).a_data)
56 #define N_GETMAGIC(x) ((x).a_magic)
57 #endif /* N_GETMAGIC */
60 #define MAP_PRIVATE MAP_COPY
61 #endif /* MAP_PRIVATE */
73 * We need these two because we are going to call them before the ld.so is
74 * finished (as a matter of fact before we know if it exists !) so we must
75 * provide these versions for them
77 static char *_getenv();
78 static int _strncmp();
82 #define LDSO "/usr/libexec/ld.so"
85 extern struct _dynamic _DYNAMIC;
86 static void __do_dynamic_link(char **argv);
92 char *__progname = empty;
95 /* Globals used by dlopen() and related functions in libc */
96 struct ld_entry *__ldso_entry;
99 extern unsigned char etext;
100 extern unsigned char eprol asm ("eprol");
101 extern start() asm("start");
102 extern mcount() asm ("mcount");
103 extern int main(int argc, char **argv, char **envp);
104 int __syscall(int syscall,...);
106 void monstartup(void *low, void *high);
111 * We need these system calls, but can't use library stubs because the are
112 * not accessible until we have done the ld.so stunt.
116 __syscall(SYS_exit, (int)(v))
117 #define _open(name, f, m) \
118 __syscall(SYS_open, (const char *)(name), (int)(f), (int)(m))
119 #define _read(fd, s, n) \
120 __syscall(SYS_read, (int)(fd), (void *)(s), (size_t)(n))
121 #define _write(fd, s, n) \
122 __syscall(SYS_write, (int)(fd), (const void *)(s), (size_t)(n))
123 #define _mmap(addr, len, prot, flags, fd, off) \
124 (void *) __syscall(SYS_mmap, (void *)(addr), (size_t)(len), \
125 (int)(prot), (int)(flags), (int)(fd), 0, (off_t)(off))
127 #define _PUTNMSG(str, len) _write(2, (str), (len))
128 #define _PUTMSG(str) _PUTNMSG((str), sizeof (str) - 1)
129 #define _FATAL(str) ( _PUTMSG(str), _exit(1) )
137 char *kargv[1]; /* size depends on kargc */
138 char kargstr[1]; /* size varies */
139 char kenvstr[1]; /* size varies */
142 * ALL REGISTER VARIABLES!!!
144 register struct kframe *kfp;
145 register char **targv;
146 register char **argv;
147 extern void _mcleanup();
155 /* just above the saved frame pointer */
156 asm ("lea 4(%%ebp), %0" : "=r" (kfp) );
157 #endif /* not lint */
158 for (argv = targv = &kfp->kargv[0]; *targv++; /* void */)
160 if (targv >= (char **)(*argv))
166 __progname = argv[0];
167 for (s=__progname; *s != '\0'; s++)
173 /* ld(1) convention: if DYNAMIC = 0 then statically linked */
174 /* sometimes GCC is too smart/stupid for its own good */
175 x = (caddr_t)&_DYNAMIC;
177 __do_dynamic_link(argv);
184 monstartup(&eprol, &etext);
187 asm ("__callmain:"); /* Defined for the benefit of debuggers */
188 exit(main(kfp->kargc, argv, environ));
193 __do_dynamic_link(argv)
202 /* Provision for alternate ld.so - security risk! */
203 if (!(ldso = _getenv("LDSO")))
207 crt.crt_ldfd = _open(ldso, 0, 0);
208 if (crt.crt_ldfd == -1) {
209 _PUTMSG("Couldn't open ");
214 /* Read LDSO exec header */
215 if (_read(crt.crt_ldfd, &hdr, sizeof hdr) < sizeof hdr) {
216 _FATAL("Failure reading ld.so\n");
218 if ((N_GETMAGIC_NET(hdr) != ZMAGIC) && (N_GETMAGIC(hdr) != QMAGIC)) {
219 _FATAL("Bad magic: ld.so\n");
222 /* We use MAP_ANON */
226 crt.crt_ba = (int)_mmap(0, hdr.a_text,
228 MAP_FILE|MAP_PRIVATE,
229 crt.crt_ldfd, N_TXTOFF(hdr));
230 if (crt.crt_ba == -1) {
231 _FATAL("Cannot map ld.so (text)\n");
234 /* Map in data segment of ld.so writable */
235 if ((int)_mmap((caddr_t)(crt.crt_ba+N_DATADDR(hdr)), hdr.a_data,
236 PROT_READ|PROT_WRITE,
237 MAP_FIXED|MAP_FILE|MAP_PRIVATE,
238 crt.crt_ldfd, N_DATOFF(hdr)) == -1) {
239 _FATAL("Cannot map ld.so (data)\n");
242 /* Map bss segment of ld.so zero */
243 if (hdr.a_bss && (int)_mmap((caddr_t)(crt.crt_ba+N_BSSADDR(hdr)),
245 PROT_READ|PROT_WRITE,
246 MAP_FIXED|MAP_ANON|MAP_PRIVATE,
247 crt.crt_dzfd, 0) == -1) {
248 _FATAL("Cannot map ld.so (bss)\n");
251 crt.crt_dp = &_DYNAMIC;
252 crt.crt_ep = environ;
253 crt.crt_bp = (caddr_t)_callmain;
254 crt.crt_prog = __progname;
256 crt.crt_ldentry = NULL;
259 entry = (int (*)())(crt.crt_ba + sizeof hdr);
260 __ldso_version = (*entry)(CRT_VERSION_BSD_5, &crt);
261 __ldso_entry = crt.crt_ldentry;
262 if (__ldso_version == -1 && __ldso_entry == NULL) {
263 /* If version 5 not recognised, try version 4 */
264 __ldso_version = (*entry)(CRT_VERSION_BSD_4, &crt);
265 __ldso_entry = crt.crt_ldentry;
266 if (__ldso_version == -1 && __ldso_entry == NULL) {
267 /* if version 4 not recognised, try version 3 */
268 __ldso_version = (*entry)(CRT_VERSION_BSD_3, &crt);
269 __ldso_entry = _DYNAMIC.d_entry;
272 if (__ldso_version == -1) {
273 _PUTMSG("ld.so failed");
274 if (__ldso_entry != NULL) {
275 const char *msg = (__ldso_entry->dlerror)();
279 for(endp = msg; *endp != '\0'; ++endp)
281 _PUTNMSG(msg, endp - msg);
288 if (__ldso_version >= LDSO_VERSION_HAS_DLEXIT)
289 atexit(__ldso_entry->dlexit);
301 register char *s1, *s2;
309 return (*(unsigned char *)s1 - *(unsigned char *)--s2);
320 extern char **environ;
322 register char **P, *C;
324 for (C = name, len = 0; *C && *C != '='; ++C, ++len);
325 for (P = environ; *P; ++P)
326 if (!_strncmp(*P, name, len))
327 if (*(C = *P + len) == '=') {
346 asm(" movl $-1,%eax");