1997-10-14 21:11:03 +04:00
|
|
|
/* $NetBSD: crt0.c,v 1.21 1997/10/14 17:11:03 christos Exp $ */
|
1996-10-18 09:36:42 +04:00
|
|
|
|
1993-10-17 01:02:58 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1993 Paul Kranenburg
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Paul Kranenburg.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
1994-01-29 04:54:01 +03:00
|
|
|
* derived from this software without specific prior written permission
|
1993-10-17 01:02:58 +03:00
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
1997-10-09 16:29:21 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-10-17 01:02:58 +03:00
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
1997-10-14 21:11:03 +04:00
|
|
|
__RCSID("$NetBSD: crt0.c,v 1.21 1997/10/14 17:11:03 christos Exp $");
|
1993-10-17 01:02:58 +03:00
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
1995-06-03 01:30:47 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
1995-06-03 17:16:03 +04:00
|
|
|
#include "common.h"
|
1995-06-03 01:30:47 +04:00
|
|
|
|
|
|
|
extern unsigned char etext;
|
1996-12-28 00:44:59 +03:00
|
|
|
extern unsigned char eprol __asm__ ("eprol");
|
|
|
|
extern void start __P((void)) __asm__ ("start");
|
1995-06-03 01:30:47 +04:00
|
|
|
|
1993-12-15 12:46:28 +03:00
|
|
|
#if defined(sun) && defined(sparc)
|
1995-06-03 17:16:03 +04:00
|
|
|
static void __call __P((void));
|
1993-10-17 01:02:58 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BSD
|
1995-06-03 17:16:03 +04:00
|
|
|
#undef mmap
|
1993-10-17 01:02:58 +03:00
|
|
|
#define mmap(addr, len, prot, flags, fd, off) \
|
1994-04-19 00:02:58 +04:00
|
|
|
__syscall2((quad_t)SYS_mmap, (addr), (len), (prot), (flags), \
|
1994-04-05 06:26:13 +04:00
|
|
|
(fd), 0, (off_t)(off))
|
1995-06-06 13:04:51 +04:00
|
|
|
extern int __syscall2 __P((quad_t, ...));
|
1993-10-17 01:02:58 +03:00
|
|
|
#endif
|
|
|
|
|
1996-12-28 00:44:59 +03:00
|
|
|
__asm__("
|
|
|
|
.global start
|
1997-10-14 21:11:03 +04:00
|
|
|
.align 4
|
1996-12-28 00:44:59 +03:00
|
|
|
.text
|
|
|
|
start:
|
|
|
|
");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up `argc', `argv', and `envp' into local registers.
|
|
|
|
*/
|
|
|
|
__asm__("
|
|
|
|
mov 0, %fp
|
|
|
|
ld [%sp + 64], %l0 ! get argc
|
|
|
|
add %sp, 68, %l1 ! get argv
|
|
|
|
sll %l0, 2, %l2 !
|
|
|
|
add %l2, 4, %l2 ! envp = argv + (argc << 2) + 4
|
|
|
|
add %l1, %l2, %l2 !
|
|
|
|
sethi %hi(_environ), %l3
|
|
|
|
st %l2, [%l3+%lo(_environ)] ! *environ = l2
|
|
|
|
");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We get a pointer to PSSTRINGS in %g1.
|
|
|
|
*/
|
|
|
|
__asm__("
|
|
|
|
cmp %g1, 0
|
|
|
|
be 1f
|
|
|
|
sethi %hi(___ps_strings), %l3
|
|
|
|
st %g1, [%l3+%lo(___ps_strings)]
|
|
|
|
1:
|
|
|
|
");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finish diddling with stack.
|
|
|
|
*/
|
|
|
|
__asm__("
|
|
|
|
andn %sp, 7, %sp ! align
|
|
|
|
sub %sp, 24, %sp ! expand to standard stack frame size
|
|
|
|
");
|
1993-10-17 01:02:58 +03:00
|
|
|
|
1995-06-03 17:16:03 +04:00
|
|
|
/*
|
|
|
|
* Set __progname:
|
|
|
|
* if (argv[0])
|
|
|
|
* if ((__progname = _strrchr(argv[0], '/')) == NULL)
|
|
|
|
* __progname = argv[0];
|
|
|
|
* else
|
|
|
|
* ++__progname;
|
|
|
|
*/
|
1996-12-28 00:44:59 +03:00
|
|
|
__asm__("
|
|
|
|
ld [%l1], %o0
|
|
|
|
cmp %o0, 0
|
|
|
|
mov %o0, %l6
|
|
|
|
be 1f
|
|
|
|
sethi %hi(___progname), %l7
|
|
|
|
");
|
1995-06-16 01:41:41 +04:00
|
|
|
#ifdef DYNAMIC
|
1996-12-28 00:44:59 +03:00
|
|
|
__asm__("call __strrchr");
|
1995-06-16 01:41:41 +04:00
|
|
|
#else
|
1996-12-28 00:44:59 +03:00
|
|
|
__asm__("call _strrchr");
|
1995-06-16 01:41:41 +04:00
|
|
|
#endif
|
1996-12-28 00:44:59 +03:00
|
|
|
__asm__("
|
|
|
|
mov 47, %o1
|
|
|
|
cmp %o0, 0
|
|
|
|
be,a 1f
|
|
|
|
st %l6, [%l7+%lo(___progname)]
|
|
|
|
add %o0, 1, %o0
|
|
|
|
st %o0, [%l7+%lo(___progname)]
|
|
|
|
1:
|
|
|
|
");
|
1994-02-25 18:02:58 +03:00
|
|
|
|
1993-10-17 01:02:58 +03:00
|
|
|
#ifdef DYNAMIC
|
|
|
|
/* Resolve symbols in dynamic libraries */
|
1996-12-28 00:44:59 +03:00
|
|
|
__asm__("
|
|
|
|
sethi %hi(__DYNAMIC), %o0
|
|
|
|
orcc %o0, %lo(__DYNAMIC), %o0
|
|
|
|
be 1f
|
|
|
|
nop
|
|
|
|
call ___load_rtld
|
|
|
|
nop
|
|
|
|
1:
|
|
|
|
");
|
1993-10-17 01:02:58 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* From here, all symbols should have been resolved, so we can use libc */
|
|
|
|
#ifdef MCRT0
|
1995-06-03 17:16:03 +04:00
|
|
|
/*
|
|
|
|
* atexit(_mcleanup);
|
|
|
|
* monstartup((u_long)&eprol, (u_long)&etext);
|
|
|
|
*/
|
1996-12-28 00:44:59 +03:00
|
|
|
__asm__("
|
|
|
|
sethi %hi(__mcleanup), %o0
|
|
|
|
call _atexit
|
|
|
|
or %o0, %lo(__mcleanup), %o0
|
|
|
|
sethi %hi(_eprol), %o0
|
|
|
|
or %o0, %lo(_eprol), %o0
|
|
|
|
sethi %hi(_etext), %o1
|
|
|
|
call _monstartup
|
|
|
|
or %o1, %lo(_etext), %o1
|
|
|
|
");
|
1993-10-17 01:02:58 +03:00
|
|
|
#endif
|
|
|
|
|
1993-12-15 12:46:28 +03:00
|
|
|
#ifdef sun
|
1996-12-28 00:44:59 +03:00
|
|
|
/*
|
|
|
|
* SunOS compatibility
|
|
|
|
*/
|
|
|
|
__asm__("
|
|
|
|
call start_float
|
|
|
|
nop
|
|
|
|
");
|
1993-12-15 12:46:28 +03:00
|
|
|
#endif
|
1993-10-17 01:02:58 +03:00
|
|
|
|
1996-12-28 00:44:59 +03:00
|
|
|
/*
|
|
|
|
* Move `argc', `argv', and `envp' from locals to parameters for `main'.
|
|
|
|
*/
|
|
|
|
__asm__("
|
|
|
|
mov %l0,%o0
|
|
|
|
mov %l1,%o1
|
|
|
|
__callmain:
|
|
|
|
call _main
|
|
|
|
mov %l2,%o2
|
1993-10-17 01:02:58 +03:00
|
|
|
|
1996-12-28 00:44:59 +03:00
|
|
|
call _exit
|
|
|
|
nop
|
|
|
|
");
|
1993-10-17 01:02:58 +03:00
|
|
|
|
1994-04-19 00:02:58 +04:00
|
|
|
#ifdef DYNAMIC
|
1996-12-28 00:44:59 +03:00
|
|
|
/*
|
|
|
|
* System call entry
|
|
|
|
*/
|
|
|
|
__asm__("
|
|
|
|
.set SYSCALL_G2RFLAG, 0x400
|
|
|
|
.set SYS___syscall, 198
|
|
|
|
___syscall2:
|
|
|
|
sethi %hi(SYS___syscall), %g1
|
|
|
|
ba 1f
|
|
|
|
or %g1, %lo(SYS___syscall), %g1
|
|
|
|
___syscall:
|
|
|
|
clr %g1
|
|
|
|
1:
|
|
|
|
or %g1, SYSCALL_G2RFLAG, %g1
|
|
|
|
add %o7, 8, %g2
|
|
|
|
ta %g0
|
|
|
|
mov -0x1, %o0
|
|
|
|
jmp %o7 + 0x8
|
|
|
|
mov -0x1, %o1
|
|
|
|
");
|
1994-04-19 00:02:58 +04:00
|
|
|
#endif
|
|
|
|
|
1993-12-15 12:46:28 +03:00
|
|
|
#ifdef sun
|
1993-10-17 01:02:58 +03:00
|
|
|
static
|
|
|
|
__call()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* adjust the C generated pointer to the crt struct to the
|
|
|
|
* likings of ld.so, which is an offset relative to its %fp
|
|
|
|
*/
|
1996-12-28 00:44:59 +03:00
|
|
|
__asm__("
|
|
|
|
mov %i0, %o0
|
|
|
|
mov %i1, %o1
|
|
|
|
call %i2
|
|
|
|
sub %o1, %sp, %o1
|
|
|
|
");
|
1993-10-17 01:02:58 +03:00
|
|
|
/*NOTREACHED, control is transferred directly to our caller */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1995-06-03 17:16:03 +04:00
|
|
|
#include "common.c"
|
1993-10-17 01:02:58 +03:00
|
|
|
|
|
|
|
#ifdef MCRT0
|
1996-12-28 00:44:59 +03:00
|
|
|
__asm__ ("
|
|
|
|
.text
|
|
|
|
_eprol:
|
|
|
|
");
|
1993-10-17 01:02:58 +03:00
|
|
|
#endif
|