Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | 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 <stdio.h>
  20. #include <sys/kos_io.h>
  21.  
  22. #include "cpu_features.h"
  23.  
  24.  
  25. /* NOTE: The code for initializing the _argv, _argc, and environ variables
  26.  *       has been moved to a separate .c file which is included in both
  27.  *       crt1.c and dllcrt1.c. This means changes in the code don't have to
  28.  *       be manually synchronized, but it does lead to this not-generally-
  29.  *       a-good-idea use of include. */
  30.  
  31.  
  32. extern char __cmdline[];
  33. extern char __pgmname[];
  34.  
  35. extern int main (int, char **, char **);
  36.  
  37. int   _errno;
  38. int   _fmode;
  39.  
  40. char  __appcwd[1024];
  41. int   __appcwdlen;
  42.  
  43. int    _argc;
  44. char **_argv;
  45.  
  46. static char *arg[2];
  47.  
  48. void   _exit(int __status) __attribute__((noreturn));
  49.  
  50.  
  51. char * __libc_getenv(const char *name)
  52. {
  53.     return NULL;
  54. }
  55.  
  56. void __main (){};
  57.  
  58. struct app_hdr
  59. {
  60.     char  banner[8];
  61.     int   version;
  62.     int   start;
  63.     int   iend;
  64.     int   memsize;
  65.     int   stacktop;
  66.     char  *cmdline;
  67.     char  *path;
  68. };
  69.  
  70. typedef void (*ctp)();
  71. static void __do_global_ctors ()
  72. {
  73.   extern int __CTOR_LIST__;
  74.   int *c = &__CTOR_LIST__;
  75.   c++;
  76.   while (*c)
  77.     {
  78.       ctp d = (ctp)*c;
  79.       (d)();
  80.       c++;
  81.     }
  82. }
  83.  
  84. void  __attribute__((noreturn))
  85. __crt_startup (void)
  86. {
  87.     int nRet;
  88.     struct   app_hdr *header;
  89.  
  90.  
  91.     init_global_reent();
  92.  
  93.   /*
  94.    * Initialize floating point unit.
  95.    */
  96.     __cpu_features_init ();   /* Do we have SSE, etc.*/
  97. //  _fpreset ();              /* Supplied by the runtime library. */
  98.  
  99.     __do_global_ctors();
  100.  
  101.     __appcwdlen = strrchr(&__pgmname[0], '/') - &__pgmname[0] + 1;
  102.     __appcwdlen = __appcwdlen > 1023 ? 1023 : __appcwdlen;
  103.     memcpy(__appcwd, &__pgmname, __appcwdlen);
  104.     __appcwd[__appcwdlen] = 0;
  105.  
  106.     set_cwd(__appcwd);
  107.  
  108.     arg[0] = &__pgmname[0];
  109.  
  110.     if( __cmdline[0] != 0)
  111.     {
  112.         _argc = 2;
  113.         arg[1] = &__cmdline[0];
  114.     } else _argc = 1;
  115.  
  116.     _argv = arg;
  117.  
  118.   /*
  119.    * Sets the default file mode.
  120.    * If _CRT_fmode is set, also set mode for stdin, stdout
  121.    * and stderr, as well
  122.    * NOTE: DLLs don't do this because that would be rude!
  123.    */
  124. //  _mingw32_init_fmode ();
  125.  
  126.  
  127.     nRet = main (_argc, _argv, NULL);
  128.  
  129.   /*
  130.    * Perform exit processing for the C library. This means
  131.    * flushing output and calling 'atexit' registered functions.
  132.    */
  133.     exit (nRet);
  134. }
  135.  
  136.  
  137.  
  138.