1 /* C code startup routine.
2 Copyright (C) 1985, 1986, 1992, 1993 Free Software Foundation, Inc.
4 This file is part of SXEmacs
6 SXEmacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 SXEmacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* Synched up with: FSF 19.30. */
22 /* The standard Vax 4.2 Unix crt0.c cannot be used for Emacs
23 because it makes `environ' an initialized variable.
24 It is easiest to have a special crt0.c on all machines
25 though I don't know whether other machines actually need it. */
27 /* On the vax and 68000, in BSD4.2 and USG5.2,
28 this is the data format on startup:
29 (vax) ap and fp are unpredictable as far as I know; don't use them.
30 sp -> word containing argc
31 word pointing to first arg string
32 [word pointing to next arg string]... 0 or more times
35 [word pointing to environment variable]... 1 or more times
40 [next arg string]... 0 or more times
43 /* On the 16000, at least in the one 4.2 system I know about,
44 the initial data format is
45 sp -> word containing argc
47 word pointing to first arg string, and so on as above
58 /* Workaround for Sun cc 3.0, which doesn't handle asm's outside a fn. */
59 #if __SUNPRO_C >= 0x300
60 #define no_toplevel_asm
63 /* ******** WARNING ********
64 Do not insert any data definitions before data_start!
65 Since this is the first file linked, the address of the following
66 variable should correspond to the start of initialized data space.
67 On some systems this is a constant that is independent of the text
68 size for shared executables. On others, it is a function of the
69 text size. In short, this seems to be the most portable way to
70 discover the start of initialized data space dynamically at runtime,
71 for either shared or unshared executables, on either swapping or
72 virtual systems. It only requires that the linker allocate objects
73 in the order encountered, a reasonable model for most Unix systems.
74 Similarly, note that the address of _start() should be the start
75 of text space. Fred Fish, UniSoft Systems Inc. */
83 #ifndef DONT_NEED_ENVIRON
88 /* On systems where the static storage class is usable, this function
89 should be declared as static. Otherwise, the static keyword has
90 been defined to be something else, and code for those systems must
91 take care of this declaration appropriately. */
96 extern char *malloc(), *realloc(), *(*_libc_malloc) (), *(*_libc_realloc) ();
97 extern void free(), (*_libc_free) ();
99 std_$call void unix_$main();
103 _libc_malloc = malloc;
104 _libc_realloc = realloc;
106 unix_$main(main); /* no return */
110 #if defined(orion) || defined(pyramid) || defined(celerity) || defined(ALLIANT) || defined(clipper) || defined(sps7)
112 #if defined(sps7) && defined(V3x)
118 /* _start must initialize _curbrk and _minbrk on the first startup;
119 when starting up after dumping, it must initialize them to what they were
120 before the dumping, since they are in the shared library and
121 are not dumped. See ADJUST_EXEC_HEADER in m-alliant.h. */
122 extern unsigned char *_curbrk, *_minbrk;
123 extern unsigned char end;
124 unsigned char *_setbrk = &end;
126 unsigned char *_end = &end;
134 _start(DUMMIES argc, argv, envp)
150 exit(main(argc, argv, envp));
153 #endif /* orion or pyramid or celerity or alliant or clipper */
155 #if defined (ns16000) && !defined (sequent) && !defined (UMAX) && !defined (CRT0_DUMMIES)
159 /* On 16000, _start pushes fp onto stack */
163 /* ignore takes care of skipping the fp value pushed in start. */
164 static start1(ignore, argc, argv)
169 environ = argv + argc + 1;
171 if (environ == *argv)
173 exit(main(argc, argv, environ));
175 #endif /* ns16000, not sequent and not UMAX, and not the CRT0_DUMMIES method */
180 asm(" exit [] # undo enter");
181 asm(" .set exitsc,1");
182 asm(" .set sigcatchall,0x400");
184 asm(" .globl _exit");
185 asm(" .globl start");
186 asm(" .globl __start");
187 asm(" .globl _main");
188 asm(" .globl _environ");
189 asm(" .globl _sigvec");
190 asm(" .globl sigentry");
195 asm(" .double p_glbl,0,0xf00000,0");
199 asm(" movd 8(sp),0(sp) # argc");
200 asm(" addr 12(sp),r0");
201 asm(" movd r0,4(sp) # argv");
205 asm(" cmpqd $0,0(r1) # null args term ?");
207 asm(" cmpd r0,0(4(sp)) # end of 'env' or 'argv' ?");
209 asm(" addqd $-4,r0 # envp's are in list");
211 asm(" movd r0,8(sp) # env");
212 asm(" movd r0,@_environ # indir is 0 if no env ; not 0 if env");
213 asm(" movqd $0,tos # setup intermediate signal handler");
214 asm(" addr @sv,tos");
215 asm(" movzwd $sigcatchall,tos");
216 asm(" jsr @_sigvec");
223 asm(" addr @exitsc,r0");
225 asm(" .align 4 # sigvec arg");
227 asm(" .double sigentry");
231 asm(" .comm p_glbl,1");
237 /* Define symbol "start": here; some systems want that symbol. */
238 #ifdef DOT_GLOBAL_START
240 asm(" .globl start ");
242 #endif /* DOT_GLOBAL_START */
244 #ifdef NODOT_GLOBAL_START
246 asm(" global start ");
248 #endif /* NODOT_GLOBAL_START */
252 /* GCC 2.1, when optimization is turned off, seems to want to push a
253 word of garbage on the stack, which screws up the CRT0_DUMMIES
254 hack. So we hand-code _start in assembly language. */
258 #else /* sony_news (not gas) */
260 #endif /* sony_news (not gas) */
261 asm(".globl __start ");
264 asm(" jbsr _start1 ");
268 #else /* not m68000 */
272 /* On vax, nothing is pushed here */
273 /* On sequent, bogus fp is pushed here */
277 #endif /* possibly m68000 */
279 #ifdef __bsdi__ /* for version number */
280 #include <sys/param.h>
282 #if defined(_BSDI_VERSION) && (_BSDI_VERSION >= 199501)
285 static int start1(CRT0_DUMMIES int argc, char *xargv)
287 char **argv = &xargv;
288 environ = argv + argc + 1;
289 #if defined(_BSDI_VERSION) && (_BSDI_VERSION >= 199501)
290 __progname = argv[0];
293 if ((char *)environ == xargv)
295 exit(main(argc, argv, environ));
297 /* Refer to `start1' so GCC will not think it is never called
298 and optimize it out. */
301 #else /* not CRT0_DUMMIES */
303 /* "m68k" and "m68000" both stand for m68000 processors,
304 but with different program-entry conventions.
305 This is a kludge. Now that the CRT0_DUMMIES mechanism above exists,
306 most of these machines could use the vax code above
307 with some suitable definition of CRT0_DUMMIES.
308 Then the symbol m68k could be flushed.
309 But I don't want to risk breaking these machines
310 in a version 17 patch release, so that change is being put off. */
312 #ifdef m68k /* Can't do it all from C */
313 asm(" global _start");
318 asm(" comm havefpu%,2");
319 #else /* m68k, not STRIDE */
320 asm(" comm splimit%,4");
326 asm(" mov.w %d0,havefpu%");
327 #else /* m68k, not STRIDE */
328 asm(" mov.l %d0,splimit%");
332 asm(" mov.l %d0,(%sp)");
334 asm(" mov.l &1,%d0"); /* d0 = 1 => exit */
336 #else /* m68000, not m68k */
341 /* Added by ESM Sun May 24 12:44:02 1987 to get new ISI library to work */
342 /* Edited by Ray Mon May 15 15:59:56 EST 1989 so we can compile with gcc */
343 #if defined(BSD4_3) && !defined(__GNUC__)
347 asm(" .globl is68020");
350 asm(" .long 0x00000000");
351 asm(" .long 0xffffffff");
352 /* End of stuff added by ESM */
355 asm(" .globl __start");
359 asm(" jbsr _start1");
362 #if defined(BSD4_3) && !defined(__GNUC__)
365 #else /* not ISI68K */
374 /* On 68000, _start pushes a6 onto stack */
377 #endif /* not ISI68k */
381 #if defined(m68k) || defined(m68000)
382 /* ignore takes care of skipping the a6 value pushed in start. */
387 start1(ignore, argc, xargv)
392 char **argv = &xargv;
393 environ = argv + argc + 1;
395 if ((char *)environ == xargv)
398 asm(" jsr f68881_used");
401 asm(" jsr ffpa_used");
404 asm(" jsr start_float");
406 exit(main(argc, argv, environ));
409 #endif /* m68k or m68000 */
411 #endif /* not CRT0_DUMMIES */
416 #ifdef OLD_HP_ASSEMBLER
418 asm(" globl __start");
423 asm(" subq.w #0x1,d0");
424 asm(" move.w d0,float_soft");
425 asm(" move.l 0x4(a7),d0");
426 asm(" beq.s skip_1");
427 asm(" move.l d0,a0");
428 asm(" clr.l -0x4(a0)");
430 asm(" move.l a7,a0");
431 asm(" subq.l #0x8,a7");
432 asm(" move.l (a0),(a7)");
433 asm(" move.l (a0),_argc_value");
434 asm(" addq.l #0x4,a0");
435 asm(" move.l a0,0x4(a7)");
436 asm(" move.l a0,_argv_value");
439 asm(" bne.s incr_loop");
440 asm(" move.l 0x4(a7),a1");
441 asm(" cmp.l (a1),a0");
442 asm(" blt.s skip_2");
443 asm(" subq.l #0x4,a0");
445 asm(" move.l a0,0x8(a7)");
446 asm(" move.l a0,_environ");
448 asm(" addq.l #0x8,a7");
449 asm(" move.l d0,-(a7)");
451 asm(" move.w #0x1,d0");
453 asm(" comm float_soft,4");
454 /* float_soft is allocated in this way because C would
455 put an underscore character in its name otherwise. */
457 #else /* new hp assembler */
460 asm(" global float_loc");
461 asm(" set float_loc,0xFFFFB000");
462 asm(" global fpa_loc");
463 asm(" set fpa_loc,0xfff08000");
464 asm(" global __start");
465 asm(" global _exit");
466 asm(" global _main");
468 asm(" byte 0,0,0,0");
469 asm(" subq.w &1,%d0");
470 asm(" mov.w %d0,float_soft");
471 asm(" mov.w %d1,flag_68881");
473 asm(" beq.b skip_float");
474 asm(" fmov.l &0x7400,%fpcr");
475 /* asm(" fmov.l &0x7480,%fpcr"); */
476 #endif /* HPUX_68010 */
478 asm(" mov.l %a0,%d0");
479 asm(" add.l %d0,%d0");
480 asm(" subx.w %d1,%d1");
481 asm(" mov.w %d1,flag_68010");
482 asm(" add.l %d0,%d0");
483 asm(" subx.w %d1,%d1");
484 asm(" mov.w %d1,flag_fpa");
486 asm(" ble.b skip_3");
487 asm(" lsl flag_68881");
488 asm(" lsl flag_fpa");
490 asm(" mov.l 4(%a7),%d0");
491 asm(" beq.b skip_1");
492 asm(" mov.l %d0,%a0");
493 asm(" clr.l -4(%a0)");
495 asm(" mov.l %a7,%a0");
496 asm(" subq.l &8,%a7");
497 asm(" mov.l (%a0),(%a7)");
498 asm(" mov.l (%a0),_argc_value");
499 asm(" addq.l &4,%a0");
500 asm(" mov.l %a0,4(%a7)");
501 asm(" mov.l %a0,_argv_value");
503 asm(" tst.l (%a0)+");
504 asm(" bne.b incr_loop");
505 asm(" mov.l 4(%a7),%a1");
506 asm(" cmp.l %a0,(%a1)");
507 asm(" blt.b skip_2");
508 asm(" subq.l &4,%a0");
510 asm(" mov.l %a0,8(%a7)");
511 asm(" mov.l %a0,_environ");
513 asm(" addq.l &8,%a7");
514 asm(" mov.l %d0,-(%a7)");
516 asm(" mov.w &1,%d0");
518 asm(" comm float_soft, 4");
519 asm(" comm flag_68881, 4");
520 asm(" comm flag_68010, 4");
521 asm(" comm flag_68040, 4");
522 asm(" comm flag_fpa, 4");
524 #endif /* new hp assembler */
525 #endif /* hp9000s300 */
529 /* startup code has to be in near text rather
530 than fartext as allocated by the C compiler. */
533 asm(" .globl __start");
536 /* setup base register b1 (function base). */
539 /* setup base registers b3 through b7 (data references). */
540 asm(" file basevals,b3");
541 /* setup base register b2 (stack pointer); it should be
542 aligned on a 8-word boundary; but because it is pointing
543 to argc, its value should be remembered (in r5). */
546 asm(" andw #~0x1f,r4");
548 /* allocate stack frame to do some work. */
549 asm(" subea 16w,b2");
550 /* initialize signal catching for UTX/32 1.2; this is
551 necessary to make restart from saved image work. */
552 asm(" movea sigcatch,r1");
553 asm(" movw r1,8w[b2]");
555 /* setup address of argc for start1. */
556 asm(" movw r5,8w[b2]");
557 asm(" func #1,_start1");
559 /* space for ld to store base register initial values. */
562 asm(" .word __base3,__base4,__base5,__base6,__base7");
571 argv = (char **)(xargc) + 1;
572 environ = argv + argc + 1;
576 exit(main(argc, argv, environ));
583 #include <elxsi/argvcache.h>
585 extern char **environ;
587 extern void _init_doscan(), _init_iob();
589 char *_init_brk = end;
593 environ = exec_cache.ac_envp;
598 _exit(exit(main(exec_cache.ac_argc,
599 exec_cache.ac_argv, exec_cache.ac_envp)));
604 #ifdef no_toplevel_asm
605 static no_toplevel_asm_wrapper()
608 asm(".pushsection \".text\"");
610 #endif /* no_toplevel_asm */
612 asm(".global _start");
616 asm(" ld [%sp + 64], %o0");
617 asm(" add %sp, 68, %o1");
618 asm(" sll %o0, 2, %o2");
619 asm(" add %o2, 4, %o2");
620 asm(" add %o1, %o2, %o2");
621 asm(" sethi %hi(_environ), %o3");
622 asm(" st %o2, [%o3+%lo(_environ)]");
623 asm(" andn %sp, 7, %sp");
625 asm(" sub %sp, 24, %sp");
629 asm(".global __start");
633 asm(" ld [%sp + 64], %o0");
634 asm(" add %sp, 68, %o1");
635 asm(" sll %o0, 2, %o2");
636 asm(" add %o2, 4, %o2");
637 asm(" add %o1, %o2, %o2");
638 asm(" sethi %hi(_environ), %o3");
639 asm(" st %o2, [%o3+%lo(_environ)]");
640 asm(" andn %sp, 7, %sp");
642 asm(" sub %sp, 24, %sp");
646 #ifdef no_toplevel_asm
650 } /* no_toplevel_asm_wrapper() */
651 #endif /* no_toplevel_asm */
658 #include <sys/param.h> /* for version number */
659 #if defined(_BSDI_VERSION) && (_BSDI_VERSION >= 199501)
662 #endif /* __bsdi__ */