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 $
31 * $DragonFly: src/lib/csu/i386/Attic/crt0.c,v 1.2 2003/06/17 04:26:41 dillon Exp $
34 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/syscall.h>
47 * This is gross, ld.so is a ZMAGIC a.out, but has `sizeof(hdr)' for
48 * an entry point and not at PAGSIZ as the N_*ADDR macros assume.
51 #define N_DATADDR(x) ((x).a_text)
54 #define N_BSSADDR(x) ((x).a_text + (x).a_data)
57 #define N_GETMAGIC(x) ((x).a_magic)
58 #endif /* N_GETMAGIC */
61 #define MAP_PRIVATE MAP_COPY
62 #endif /* MAP_PRIVATE */
74 * We need these two because we are going to call them before the ld.so is
75 * finished (as a matter of fact before we know if it exists !) so we must
76 * provide these versions for them
78 static char *_getenv();
79 static int _strncmp();
83 #define LDSO "/usr/libexec/ld.so"
86 extern struct _dynamic _DYNAMIC;
87 static void __do_dynamic_link(char **argv);
93 char *__progname = empty;
96 /* Globals used by dlopen() and related functions in libc */
97 struct ld_entry *__ldso_entry;
100 extern unsigned char etext;
101 extern unsigned char eprol asm ("eprol");
102 extern start() asm("start");
103 extern mcount() asm ("mcount");
104 extern int main(int argc, char **argv, char **envp);
105 int __syscall(int syscall,...);
107 void monstartup(void *low, void *high);
112 * We need these system calls, but can't use library stubs because the are
113 * not accessible until we have done the ld.so stunt.
117 __syscall(SYS_exit, (int)(v))
118 #define _open(name, f, m) \
119 __syscall(SYS_open, (const char *)(name), (int)(f), (int)(m))
120 #define _read(fd, s, n) \
121 __syscall(SYS_read, (int)(fd), (void *)(s), (size_t)(n))
122 #define _write(fd, s, n) \
123 __syscall(SYS_write, (int)(fd), (const void *)(s), (size_t)(n))
124 #define _mmap(addr, len, prot, flags, fd, off) \
125 (void *) __syscall(SYS_mmap, (void *)(addr), (size_t)(len), \
126 (int)(prot), (int)(flags), (int)(fd), 0, (off_t)(off))
128 #define _PUTNMSG(str, len) _write(2, (str), (len))
129 #define _PUTMSG(str) _PUTNMSG((str), sizeof (str) - 1)
130 #define _FATAL(str) ( _PUTMSG(str), _exit(1) )
138 char *kargv[1]; /* size depends on kargc */
139 char kargstr[1]; /* size varies */
140 char kenvstr[1]; /* size varies */
143 * ALL REGISTER VARIABLES!!!
145 register struct kframe *kfp;
146 register char **targv;
147 register char **argv;
148 extern void _mcleanup();
156 /* just above the saved frame pointer */
157 asm ("lea 4(%%ebp), %0" : "=r" (kfp) );
158 #endif /* not lint */
159 for (argv = targv = &kfp->kargv[0]; *targv++; /* void */)
161 if (targv >= (char **)(*argv))
167 __progname = argv[0];
168 for (s=__progname; *s != '\0'; s++)
174 /* ld(1) convention: if DYNAMIC = 0 then statically linked */
175 /* sometimes GCC is too smart/stupid for its own good */
176 x = (caddr_t)&_DYNAMIC;
178 __do_dynamic_link(argv);
185 monstartup(&eprol, &etext);
188 asm ("__callmain:"); /* Defined for the benefit of debuggers */
189 exit(main(kfp->kargc, argv, environ));
194 __do_dynamic_link(argv)
203 /* Provision for alternate ld.so - security risk! */
204 if (!(ldso = _getenv("LDSO")))
208 crt.crt_ldfd = _open(ldso, 0, 0);
209 if (crt.crt_ldfd == -1) {
210 _PUTMSG("Couldn't open ");
215 /* Read LDSO exec header */
216 if (_read(crt.crt_ldfd, &hdr, sizeof hdr) < sizeof hdr) {
217 _FATAL("Failure reading ld.so\n");
219 if ((N_GETMAGIC_NET(hdr) != ZMAGIC) && (N_GETMAGIC(hdr) != QMAGIC)) {
220 _FATAL("Bad magic: ld.so\n");
223 /* We use MAP_ANON */
227 crt.crt_ba = (int)_mmap(0, hdr.a_text,
229 MAP_FILE|MAP_PRIVATE,
230 crt.crt_ldfd, N_TXTOFF(hdr));
231 if (crt.crt_ba == -1) {
232 _FATAL("Cannot map ld.so (text)\n");
235 /* Map in data segment of ld.so writable */
236 if ((int)_mmap((caddr_t)(crt.crt_ba+N_DATADDR(hdr)), hdr.a_data,
237 PROT_READ|PROT_WRITE,
238 MAP_FIXED|MAP_FILE|MAP_PRIVATE,
239 crt.crt_ldfd, N_DATOFF(hdr)) == -1) {
240 _FATAL("Cannot map ld.so (data)\n");
243 /* Map bss segment of ld.so zero */
244 if (hdr.a_bss && (int)_mmap((caddr_t)(crt.crt_ba+N_BSSADDR(hdr)),
246 PROT_READ|PROT_WRITE,
247 MAP_FIXED|MAP_ANON|MAP_PRIVATE,
248 crt.crt_dzfd, 0) == -1) {
249 _FATAL("Cannot map ld.so (bss)\n");
252 crt.crt_dp = &_DYNAMIC;
253 crt.crt_ep = environ;
254 crt.crt_bp = (caddr_t)_callmain;
255 crt.crt_prog = __progname;
257 crt.crt_ldentry = NULL;
260 entry = (int (*)())(crt.crt_ba + sizeof hdr);
261 __ldso_version = (*entry)(CRT_VERSION_BSD_5, &crt);
262 __ldso_entry = crt.crt_ldentry;
263 if (__ldso_version == -1 && __ldso_entry == NULL) {
264 /* If version 5 not recognised, try version 4 */
265 __ldso_version = (*entry)(CRT_VERSION_BSD_4, &crt);
266 __ldso_entry = crt.crt_ldentry;
267 if (__ldso_version == -1 && __ldso_entry == NULL) {
268 /* if version 4 not recognised, try version 3 */
269 __ldso_version = (*entry)(CRT_VERSION_BSD_3, &crt);
270 __ldso_entry = _DYNAMIC.d_entry;
273 if (__ldso_version == -1) {
274 _PUTMSG("ld.so failed");
275 if (__ldso_entry != NULL) {
276 const char *msg = (__ldso_entry->dlerror)();
280 for(endp = msg; *endp != '\0'; ++endp)
282 _PUTNMSG(msg, endp - msg);
289 if (__ldso_version >= LDSO_VERSION_HAS_DLEXIT)
290 atexit(__ldso_entry->dlexit);
302 register char *s1, *s2;
310 return (*(unsigned char *)s1 - *(unsigned char *)--s2);
321 extern char **environ;
323 register char **P, *C;
325 for (C = name, len = 0; *C && *C != '='; ++C, ++len);
326 for (P = environ; *P; ++P)
327 if (!_strncmp(*P, name, len))
328 if (*(C = *P + len) == '=') {
347 asm(" movl $-1,%eax");