1998-03-07 20:27:11 +00:00
|
|
|
/*-
|
|
|
|
* Copyright 1996-1998 John D. Polstra.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2002-07-16 12:28:50 +00:00
|
|
|
#ifndef lint
|
1998-03-07 20:27:11 +00:00
|
|
|
#ifndef __GNUC__
|
|
|
|
#error "GCC is needed to compile this file"
|
|
|
|
#endif
|
2002-07-16 12:28:50 +00:00
|
|
|
#endif /* lint */
|
1998-03-07 20:27:11 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2002-07-03 14:42:39 +00:00
|
|
|
|
2002-03-29 22:43:43 +00:00
|
|
|
#include "libc_private.h"
|
2000-10-28 21:26:48 +00:00
|
|
|
#include "crtbrand.c"
|
1998-03-07 20:27:11 +00:00
|
|
|
|
2002-07-16 12:28:50 +00:00
|
|
|
extern int _DYNAMIC;
|
|
|
|
#pragma weak _DYNAMIC
|
|
|
|
|
1998-03-07 20:27:11 +00:00
|
|
|
typedef void (*fptr)(void);
|
|
|
|
|
|
|
|
extern void _fini(void);
|
|
|
|
extern void _init(void);
|
|
|
|
extern int main(int, char **, char **);
|
2002-07-16 12:28:50 +00:00
|
|
|
extern void _start(char *, ...);
|
1998-03-07 20:27:11 +00:00
|
|
|
|
1998-09-07 23:32:00 +00:00
|
|
|
#ifdef GCRT
|
|
|
|
extern void _mcleanup(void);
|
|
|
|
extern void monstartup(void *, void *);
|
|
|
|
extern int eprol;
|
|
|
|
extern int etext;
|
|
|
|
#endif
|
|
|
|
|
2002-07-16 12:28:50 +00:00
|
|
|
char **environ;
|
|
|
|
const char *__progname = "";
|
1998-03-07 20:27:11 +00:00
|
|
|
|
2002-07-16 12:28:50 +00:00
|
|
|
static __inline fptr
|
|
|
|
get_rtld_cleanup(void)
|
|
|
|
{
|
|
|
|
fptr retval;
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
__asm__("movl %%edx,%0" : "=rm"(retval));
|
1998-03-07 20:27:11 +00:00
|
|
|
#else
|
2002-07-16 12:28:50 +00:00
|
|
|
retval = (fptr)0; /* XXXX Fix this for other compilers */
|
1998-03-07 20:27:11 +00:00
|
|
|
#endif
|
2002-07-16 12:28:50 +00:00
|
|
|
return(retval);
|
|
|
|
}
|
1998-03-07 20:27:11 +00:00
|
|
|
|
2002-07-16 12:28:50 +00:00
|
|
|
/* The entry function. */
|
1998-03-07 20:27:11 +00:00
|
|
|
void
|
2002-07-16 12:28:50 +00:00
|
|
|
_start(char *ap, ...)
|
1998-03-07 20:27:11 +00:00
|
|
|
{
|
2002-07-16 12:28:50 +00:00
|
|
|
fptr cleanup;
|
2002-07-03 14:42:39 +00:00
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
char **env;
|
|
|
|
const char *s;
|
1998-03-07 20:27:11 +00:00
|
|
|
|
2002-07-16 12:28:50 +00:00
|
|
|
cleanup = get_rtld_cleanup();
|
|
|
|
argv = ≈
|
|
|
|
argc = *(long *)(void *)(argv - 1);
|
2002-07-03 14:42:39 +00:00
|
|
|
env = argv + argc + 1;
|
|
|
|
environ = env;
|
|
|
|
if (argc > 0 && argv[0] != NULL) {
|
|
|
|
__progname = argv[0];
|
|
|
|
for (s = __progname; *s != '\0'; s++)
|
|
|
|
if (*s == '/')
|
|
|
|
__progname = s + 1;
|
|
|
|
}
|
1998-03-07 20:27:11 +00:00
|
|
|
|
2002-07-03 14:42:39 +00:00
|
|
|
if (&_DYNAMIC != NULL)
|
2002-07-16 12:28:50 +00:00
|
|
|
atexit(cleanup);
|
1998-03-07 20:27:11 +00:00
|
|
|
|
1998-09-07 23:32:00 +00:00
|
|
|
#ifdef GCRT
|
2002-07-03 14:42:39 +00:00
|
|
|
atexit(_mcleanup);
|
1998-09-07 23:32:00 +00:00
|
|
|
#endif
|
2002-07-03 14:42:39 +00:00
|
|
|
atexit(_fini);
|
1998-09-07 23:32:00 +00:00
|
|
|
#ifdef GCRT
|
2002-07-03 14:42:39 +00:00
|
|
|
monstartup(&eprol, &etext);
|
1998-09-07 23:32:00 +00:00
|
|
|
#endif
|
2002-07-03 14:42:39 +00:00
|
|
|
_init();
|
2002-09-29 13:42:27 +00:00
|
|
|
#ifndef __GNUC__
|
2002-07-03 14:42:39 +00:00
|
|
|
exit( main(argc, argv, env) );
|
2002-09-29 13:42:27 +00:00
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* Some versions of gcc-2 expect the stack frame to be aligned as
|
|
|
|
* follows after it is set up in main():
|
|
|
|
*
|
|
|
|
* +--------------+ <--- aligned by PREFERRED_STACK_BOUNDARY
|
|
|
|
* +%ebp (if any) +
|
|
|
|
* +--------------+
|
|
|
|
* |return address|
|
|
|
|
* +--------------+
|
|
|
|
* | arguments |
|
|
|
|
* | : |
|
|
|
|
* | : |
|
|
|
|
* +--------------+
|
|
|
|
*
|
|
|
|
* We implement the above to fix just the usual case in FreeBSD-4.
|
|
|
|
* Alignment for main() is too compiler-dependent to handle correctly
|
|
|
|
* in all cases here (or in the kernel). E.g., a different alignment
|
|
|
|
* is required for at least gcc-2.95.4 even for the small variation
|
|
|
|
* of compiling main() with -fomit-frame-pointer.
|
|
|
|
*/
|
|
|
|
__asm__("
|
|
|
|
andl $~0xf, %%esp # align stack to 16-byte boundary
|
|
|
|
subl $12+12, %%esp # space for args and padding
|
|
|
|
movl %0, 0(%%esp)
|
|
|
|
movl %1, 4(%%esp)
|
|
|
|
movl %2, 8(%%esp)
|
|
|
|
call main
|
|
|
|
movl %%eax, 0(%%esp)
|
|
|
|
call exit
|
|
|
|
" : : "r" (argc), "r" (argv), "r" (env) : "ax", "cx", "dx", "memory");
|
|
|
|
#endif
|
1998-03-07 20:27:11 +00:00
|
|
|
}
|
1998-09-07 23:32:00 +00:00
|
|
|
|
|
|
|
#ifdef GCRT
|
|
|
|
__asm__(".text");
|
|
|
|
__asm__("eprol:");
|
|
|
|
__asm__(".previous");
|
|
|
|
#endif
|
2002-02-27 22:13:02 +00:00
|
|
|
|
|
|
|
__asm__(".ident\t\"$FreeBSD$\"");
|