Subversion Repositories Kolibri OS

Rev

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

  1. #include <kos32sys1.h>
  2.  
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <dlfcn.h>
  6.  
  7. typedef struct {
  8.     char *name;
  9.     void *ptr;
  10. } KosExp;
  11.  
  12. typedef struct {
  13.     void **importNames;
  14.     char * libraryName;
  15. } KosImp;
  16.  
  17. static const char *__error;
  18.  
  19. static int stdcall dll_Load(KosImp *importTable);
  20.  
  21. static int stdcall dll_Load(KosImp *importTableEntry) {
  22.     for (; importTableEntry->importNames; importTableEntry++) {
  23.         char libPath[256] = "/sys/lib/";
  24.         KosExp *exports = NULL;
  25.         void **libImports = importTableEntry->importNames;
  26.        
  27.         strcat(libPath, importTableEntry->libraryName);
  28.         if (!(exports = dlopen(libPath, 0))) { return 1; }
  29.         for (; *libImports; libImports++) {
  30.             if (!(*libImports = dlsym(exports, *libImports))) { return 1; }
  31.         }
  32.     }
  33.     return 0;
  34. }
  35.  
  36. // https://pubs.opengroup.org/onlinepubs/007908799/xsh/dlopen.html
  37. // Current implementation fully ignores "mode" parameter
  38. void *dlopen(const char *name, int mode) {
  39.     KosExp *exports = NULL;
  40.  
  41.     // загрузить либу сискаллом
  42.     asm volatile ("int $0x40":"=a"(exports):"a"(68), "b"(19), "c"(name));
  43.     if (!exports) {
  44.         char libPath[256] = "/sys/lib/";
  45.  
  46.         strcat(libPath, name);
  47.         asm volatile ("int $0x40":"=a"(exports):"a"(68), "b"(19), "c"(libPath));
  48.         if (!exports) {
  49.             __error = "Library not found in \"/sys/lib/\" nor current folder";
  50.             return NULL;
  51.         }
  52.     }
  53.     // Вызвать что-нибудь что начинается с "lib_"
  54.     for (KosExp *export = exports; export->name; export++) {
  55.         if (!memcmp(export->name, "lib_", 4)) {
  56.             asm volatile (
  57.                 "call *%4" ::
  58.                 "a"(sysmalloc),
  59.                 "b"(sysfree),
  60.                 "c"(sysrealloc),
  61.                 "d"(dll_Load),
  62.                 "r"(export->ptr));
  63.         }
  64.     }
  65.     return exports;
  66. }
  67.  
  68. // https://pubs.opengroup.org/onlinepubs/007908799/xsh/dlsym.html
  69. void *dlsym(void *handle, const char *name) {
  70.     KosExp *exp = handle;
  71.  
  72.     for (; exp->name; exp++) {
  73.         if (!strcmp(exp->name, name)) {
  74.             return exp->ptr;
  75.         }
  76.     }
  77.     __error = "Symbol not found";
  78.     return NULL;
  79. }
  80.  
  81. // https://pubs.opengroup.org/onlinepubs/007908799/xsh/dlclose.html
  82. int dlclose(void *handle) {
  83.     return 0;
  84. }
  85.  
  86. // https://pubs.opengroup.org/onlinepubs/007908799/xsh/dlerror.html
  87. char *dlerror(void) {
  88.     char *ret = __error ? strdup(__error) : NULL;
  89.     __error = NULL;
  90.     return ret;
  91. }
  92.