Subversion Repositories Kolibri OS

Rev

Rev 4921 | Rev 5198 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1.  
  2. #include <_ansi.h>
  3. #include <reent.h>
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <setjmp.h>
  10. #include <sys/kos_io.h>
  11.  
  12. struct app_hdr
  13. {
  14.     char  banner[8];
  15.     int   version;
  16.     int   start;
  17.     int   iend;
  18.     int   memsize;
  19.     int   stacktop;
  20.     char  *cmdline;
  21.     char  *path;
  22.     int    reserved;
  23.     void  *__idata_start;
  24.     void  *__idata_end;
  25.     int  (*main)(int argc, char **argv, char **envp);
  26. };
  27.  
  28. void _pei386_runtime_relocator (void);
  29. void init_loader(void *libc_image);
  30. void init_reent();
  31.  
  32. int link_app();
  33. void* get_entry_point(void *raw);
  34. int (*entry)(int, char **, char **);
  35.  
  36. char  __appcwd[1024];
  37. int   __appcwdlen;
  38. char* __appenv;
  39. int   __appenv_size;
  40.  
  41. extern char _tls_map[128];
  42.  
  43. void  __attribute__((noreturn))
  44. __thread_startup (int (*entry)(void*), void *param,
  45.                   void *stacklow, void *stackhigh)
  46. {
  47.     int retval;
  48.  
  49. //    asm volatile ( "xchgw %bx, %bx");
  50.  
  51.     __asm__ __volatile__(               // save stack limits
  52.     "movl %0, %%fs:8    \n\t"           // use TLS
  53.     "movl %1, %%fs:12    \n\t"
  54.     ::"r"(stacklow), "r"(stackhigh));
  55.  
  56.     init_reent();                       // initialize thread reentry structure
  57.  
  58.     retval = entry(param);              // call user thread function
  59.  
  60.     _exit(retval);
  61. };
  62.  
  63. char * __libc_getenv(const char *name)
  64. {
  65.     return NULL;
  66. }
  67.  
  68. static int split_cmdline(char *cmdline, char **argv)
  69. {
  70.     enum quote_state
  71.     {
  72.         QUOTE_NONE,         /* no " active in current parm       */
  73.         QUOTE_DELIMITER,    /* " was first char and must be last */
  74.         QUOTE_STARTED       /* " was seen, look for a match      */
  75.     };
  76.  
  77.     enum quote_state state;
  78.     unsigned int argc;
  79.     char *p = cmdline;
  80.     char *new_arg, *start;
  81.  
  82.     argc = 0;
  83.  
  84.     for(;;)
  85.     {
  86.         /* skip over spaces and tabs */
  87.         if ( *p )
  88.         {
  89.             while (*p == ' ' || *p == '\t')
  90.                 ++p;
  91.         }
  92.  
  93.         if (*p == '\0')
  94.             break;
  95.  
  96.         state = QUOTE_NONE;
  97.         if( *p == '\"' )
  98.         {
  99.             p++;
  100.             state = QUOTE_DELIMITER;
  101.         }
  102.         new_arg = start = p;
  103.         for (;;)
  104.         {
  105.             if( *p == '\"' )
  106.             {
  107.                 p++;
  108.                 if( state == QUOTE_NONE )
  109.                 {
  110.                     state = QUOTE_STARTED;
  111.                 }
  112.                 else
  113.                 {
  114.                     state = QUOTE_NONE;
  115.                 }
  116.                 continue;
  117.             }
  118.  
  119.             if( *p == ' ' || *p == '\t' )
  120.             {
  121.                 if( state == QUOTE_NONE )
  122.                 {
  123.                     break;
  124.                 }
  125.             }
  126.  
  127.             if( *p == '\0' )
  128.                 break;
  129.  
  130.             if( *p == '\\' )
  131.             {
  132.                 if( p[1] == '\"' )
  133.                 {
  134.                     ++p;
  135.                     if( p[-2] == '\\' )
  136.                     {
  137.                         continue;
  138.                     }
  139.                 }
  140.             }
  141.             if( argv )
  142.             {
  143.                 *(new_arg++) = *p;
  144.             }
  145.             ++p;
  146.         };
  147.  
  148.         if( argv )
  149.         {
  150.             argv[ argc ] = start;
  151.             ++argc;
  152.  
  153.             /*
  154.               The *new = '\0' is req'd in case there was a \" to "
  155.               translation. It must be after the *p check against
  156.               '\0' because new and p could point to the same char
  157.               in which case the scan would be terminated too soon.
  158.             */
  159.  
  160.             if( *p == '\0' )
  161.             {
  162.                 *new_arg = '\0';
  163.                 break;
  164.             }
  165.             *new_arg = '\0';
  166.             ++p;
  167.         }
  168.         else
  169.         {
  170.             ++argc;
  171.             if( *p == '\0' )
  172.             {
  173.                 break;
  174.             }
  175.             ++p;
  176.         }
  177.     }
  178.  
  179.     return argc;
  180. };
  181.  
  182. void  __attribute__((noreturn))
  183. libc_crt_startup (void *libc_base)
  184. {
  185.     struct   app_hdr *header = NULL;
  186.     int retval = 0;
  187.  
  188.     char **argv;
  189.     int    argc;
  190.  
  191.     _pei386_runtime_relocator();
  192.  
  193.     memset(_tls_map, 0xFF, 32*4);
  194.     _tls_map[0] = 0xE0;
  195.     init_reent();
  196.     init_stdio();
  197.  
  198.  //   __appenv = load_file("/sys/system.env", &__appenv_size);
  199.  
  200.     init_loader(libc_base);
  201.  
  202.     if( link_app() == 0)
  203.         goto done;
  204.  
  205.     __appcwdlen = strrchr(header->path, '/') - header->path;
  206.     __appcwdlen = __appcwdlen > 1022 ? 1022 : __appcwdlen;
  207.     memcpy(__appcwd, header->path, __appcwdlen);
  208.     set_cwd(__appcwd);
  209.  
  210.     if( header->cmdline[0] != 0)
  211.     {
  212.         argc = split_cmdline(header->cmdline, NULL) + 1;
  213.         argv = alloca((argc+1)*sizeof(char*));
  214.         argv[0] = header->path;
  215.  
  216.         split_cmdline(header->cmdline, argv + 1);
  217.     }
  218.     else
  219.     {
  220.         argc = 1;
  221.         argv = alloca((argc+1)*sizeof(char*));
  222.         argv[0] = header->path;
  223.     }
  224.     argv[argc] = NULL;
  225.  
  226.     retval = header->main(argc, argv, NULL);
  227. done:
  228.     exit (retval);
  229. }
  230.  
  231.  
  232.