Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | 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. };
  23.  
  24. int    _argc;
  25. char **_argv;
  26.  
  27.  
  28. void    __fastcall init_loader(void *libc_image);
  29. void*   __fastcall create_image(void *raw);
  30. int     __fastcall link_image(void *img_base);
  31. void* get_entry_point(void *raw);
  32. int (*entry)(int, char **, char **);
  33.  
  34.  
  35. void init_reent();
  36.  
  37. jmp_buf loader_env;
  38.  
  39. void  __attribute__((noreturn))
  40. __thread_startup (int (*entry)(void*), void *param,
  41.                   void *stacklow, void *stackhigh)
  42. {
  43.     int retval;
  44.  
  45.     asm volatile ( "xchgw %bx, %bx");
  46.  
  47.     __asm__ __volatile__(               // save stack limits
  48.     "movl %0, %%fs:4    \n\t"           // use TLS
  49.     "movl %1, %%fs:8    \n\t"
  50.     ::"r"(stacklow), "r"(stackhigh));
  51.  
  52.     init_reent();                       // initialize thread reentry structure
  53.  
  54.     retval = entry(param);              // call user thread function
  55.  
  56.     _exit(retval);
  57. };
  58.  
  59. char * __libc_getenv(const char *name)
  60. {
  61.     return NULL;
  62. }
  63.  
  64. char  __appcwd[1024];
  65. int   __appcwdlen;
  66. char* __appenv;
  67. int   __appenv_size;
  68.  
  69. void  __attribute__((noreturn))
  70. crt_startup (void *libc_base, void *obj_base, uint32_t *params)
  71. {
  72.     struct   app_hdr *header;
  73.     char *arg[2];
  74.  
  75.     int len;
  76.     char *p;
  77.  
  78.     void *my_app;
  79.     int retval = 0;
  80.  
  81. //    user_free(obj_base);
  82.  
  83.     init_reent();
  84.     __initPOSIXHandles();
  85.     __appenv = load_file("/sys/system.env", &__appenv_size);
  86.  
  87.     init_loader(libc_base);
  88.  
  89.     my_app = create_image((void*)(params[0]));
  90.  
  91.     if( link_image(my_app)==0)
  92.         goto done;
  93.  
  94.     header = (struct app_hdr*)NULL;
  95.  
  96.     __appcwdlen = strrchr(header->path, '/') - header->path;
  97.     __appcwdlen = __appcwdlen > 1022 ? 1022 : __appcwdlen;
  98.     memcpy(__appcwd, header->path, __appcwdlen);
  99.     set_cwd(__appcwd);
  100.  
  101. #ifdef BRAVE_NEW_WORLD
  102.     len = strlen(header->path);
  103.     p = alloca(len+1);
  104.     memcpy(p, header->path, len);
  105.     p[len]=0;
  106.  
  107.     arg[0] = p;
  108. #else
  109.     arg[0] = header->path;
  110. #endif
  111.  
  112.     _argc = 1;
  113.  
  114.     if( header->cmdline != 0)
  115.     {
  116. #ifdef BRAVE_NEW_WORLD
  117.         len = strlen(header->cmdline);
  118.         if(len)
  119.         {
  120.             p = alloca(len+1);
  121.             memcpy(p, header->cmdline, len);
  122.             p[len]=0;
  123.             _argc = 2;
  124.             arg[1] = p;
  125.         };
  126. #else
  127.         _argc = 2;
  128.         arg[1] = header->cmdline;
  129. #endif
  130.     };
  131.  
  132.     _argv = arg;
  133.  
  134.     entry = get_entry_point(my_app);
  135.  
  136. //    __asm__ __volatile__("int3");
  137.  
  138.     retval = entry(_argc, _argv, NULL);
  139.  
  140. done:
  141.     exit (retval);
  142. }
  143.  
  144.  
  145.