Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * crt1.c
  3.  * This file has no copyright assigned and is placed in the Public Domain.
  4.  * This file is a part of the mingw-runtime package.
  5.  * No warranty is given; refer to the file DISCLAIMER within the package.
  6.  *
  7.  * Source code for the startup proceedures used by all programs. This code
  8.  * is compiled to make crt1.o, which should be located in the library path.
  9.  *
  10.  */
  11.  
  12. /* Hide the declaration of _fmode with dllimport attribute in stdlib.h to
  13.    avoid problems with older GCC. */
  14.  
  15. #include <newlib.h>
  16. #include <stdint.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "cpu_features.h"
  20.  
  21.  
  22. /* NOTE: The code for initializing the _argv, _argc, and environ variables
  23.  *       has been moved to a separate .c file which is included in both
  24.  *       crt1.c and dllcrt1.c. This means changes in the code don't have to
  25.  *       be manually synchronized, but it does lead to this not-generally-
  26.  *       a-good-idea use of include. */
  27.  
  28.  
  29. extern char __cmdline;
  30. extern char __pgmname;
  31.  
  32. extern int main (int, char **, char **);
  33.  
  34. int   _errno;
  35. int   _fmode;
  36.  
  37. char  __appcwd[1024];
  38. int   __appcwdlen;
  39.  
  40. int    _argc;
  41. char **_argv;
  42.  
  43. static char *arg[2];
  44.  
  45. void   _exit(int __status) __attribute__((noreturn));
  46.  
  47.  
  48. char * __libc_getenv(const char *name)
  49. {
  50.     return NULL;
  51. }
  52.  
  53. void __main (){};
  54.  
  55.  
  56. void init_reent();
  57.  
  58. void  __attribute__((noreturn))
  59. __thread_startup (int (*entry)(void*), void *param,
  60.                   void *stacklow, void *stackhigh)
  61. {
  62.     int retval;
  63.  
  64.     __asm__ __volatile__(               // save stack limits
  65.     "movl %0, %%fs:4    \n\t"           // use TLS
  66.     "movl %1, %%fs:8    \n\t"
  67.     ::"r"(stacklow), "r"(stackhigh));
  68.  
  69.     init_reent();                       // initialize thread reentry structure
  70.  
  71.     retval = entry(param);              // call user thread function
  72.  
  73.     _exit(retval);
  74. };
  75.  
  76. void  __attribute__((noreturn))
  77. __crt_startup (void)
  78. {
  79.     int nRet;
  80.  
  81.     init_reent();
  82.  
  83.   /*
  84.    * Initialize floating point unit.
  85.    */
  86.     __cpu_features_init ();   /* Do we have SSE, etc.*/
  87. //  _fpreset ();              /* Supplied by the runtime library. */
  88.  
  89.     __initPOSIXHandles();
  90.  
  91.     __appcwdlen = strrchr(&__pgmname, '/') - &__pgmname + 1;
  92.  
  93.     __appcwdlen = __appcwdlen > 1023 ? 1023 : __appcwdlen;
  94.  
  95.     strncpy(__appcwd, &__pgmname, __appcwdlen);
  96.     __appcwd[__appcwdlen] = 0;
  97.  
  98.     arg[0] = &__pgmname;
  99.  
  100.     if( __cmdline != 0)
  101.     {
  102.         _argc = 2;
  103.         arg[1] = &__cmdline;
  104.     } else _argc = 1;
  105.  
  106.     _argv = arg;
  107.  
  108.   /*
  109.    * Sets the default file mode.
  110.    * If _CRT_fmode is set, also set mode for stdin, stdout
  111.    * and stderr, as well
  112.    * NOTE: DLLs don't do this because that would be rude!
  113.    */
  114. //  _mingw32_init_fmode ();
  115.  
  116.     nRet = main (_argc, _argv, NULL);
  117.  
  118.   /*
  119.    * Perform exit processing for the C library. This means
  120.    * flushing output and calling 'atexit' registered functions.
  121.    */
  122.     _exit (nRet);
  123. }
  124.  
  125.  
  126.  
  127.