Subversion Repositories Kolibri OS

Rev

Rev 4874 | Rev 5190 | 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. int    _argc;
  29. char **_argv;
  30.  
  31.  
  32. void* get_entry_point(void *raw);
  33. int (*entry)(int, char **, char **);
  34.  
  35. void init_loader(void *libc_image);
  36.  
  37.  
  38. void init_reent();
  39.  
  40. jmp_buf loader_env;
  41.  
  42. void  __attribute__((noreturn))
  43. __thread_startup (int (*entry)(void*), void *param,
  44.                   void *stacklow, void *stackhigh)
  45. {
  46.     int retval;
  47.  
  48.     asm volatile ( "xchgw %bx, %bx");
  49.  
  50.     __asm__ __volatile__(               // save stack limits
  51.     "movl %0, %%fs:8    \n\t"           // use TLS
  52.     "movl %1, %%fs:12    \n\t"
  53.     ::"r"(stacklow), "r"(stackhigh));
  54.  
  55.     init_reent();                       // initialize thread reentry structure
  56.  
  57.     retval = entry(param);              // call user thread function
  58.  
  59.     _exit(retval);
  60. };
  61.  
  62. char * __libc_getenv(const char *name)
  63. {
  64.     return NULL;
  65. }
  66.  
  67. void _pei386_runtime_relocator (void);
  68. int link_app();
  69.  
  70. char  __appcwd[1024];
  71. int   __appcwdlen;
  72. char* __appenv;
  73. int   __appenv_size;
  74.  
  75. static char *arg[2];
  76.  
  77. extern char _tls_map[128];
  78.  
  79. void  __attribute__((noreturn))
  80. libc_crt_startup (void *libc_base)
  81. {
  82.     struct   app_hdr *header = NULL;
  83.  
  84.     int len;
  85.     char *p;
  86.  
  87.     void *my_app;
  88.     int retval = 0;
  89.  
  90.     _pei386_runtime_relocator();
  91.  
  92.     memset(_tls_map, 0xFF, 32*4);
  93.     _tls_map[0] = 0xE0;
  94.     init_reent();
  95.     init_stdio();
  96.  
  97.  //   __appenv = load_file("/sys/system.env", &__appenv_size);
  98.  
  99.     init_loader(libc_base);
  100.  
  101.     if( link_app() == 0)
  102.         goto done;
  103.  
  104.     __appcwdlen = strrchr(header->path, '/') - header->path;
  105.     __appcwdlen = __appcwdlen > 1022 ? 1022 : __appcwdlen;
  106.     memcpy(__appcwd, header->path, __appcwdlen);
  107.     set_cwd(__appcwd);
  108.  
  109.     arg[0] = header->path;
  110.  
  111.     if( header->cmdline[0] != 0)
  112.     {
  113.         _argc = 2;
  114.         arg[1] = header->cmdline;
  115.     }
  116.     else _argc = 1;
  117.  
  118.     _argv = arg;
  119.  
  120.     retval = header->main(_argc, _argv, NULL);
  121. done:
  122.     exit (retval);
  123. }
  124.  
  125.  
  126.