Subversion Repositories Kolibri OS

Rev

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

  1. #include <stdlib.h>
  2. #include <string.h>
  3.  
  4. #include "kosnet/dlfcn.h"
  5. #include "kosnet/kos32sys1.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 int __attribute__ ((stdcall)) dll_Load(KosImp *importTableEntry);
  18.  
  19. static const char *__error;
  20.  
  21. static int __attribute__ ((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.     // load library using syscall
  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.     // call anything starting with "lib_"
  54.     for (KosExp *export = exports; export->name; export++) {
  55.         if (!memcmp(export->name, "lib_", 4)) {
  56.             asm volatile (
  57.                 "call *%4" ::
  58.                 "a"(0),
  59.                 "b"(0),
  60.                 "c"(0),
  61.                 "d"(dll_Load),
  62.                 "r"(export->ptr));
  63.             // was asm volatile ("call *%4" ::"a"(sysmalloc),"b"(sysfree),"c"(sysrealloc),"d"(dll_Load),"r"(export->ptr));
  64.         }
  65.     }
  66.     return exports;
  67. }
  68.  
  69. // https://pubs.opengroup.org/onlinepubs/007908799/xsh/dlsym.html
  70. void *dlsym(void *handle, const char *name) {
  71.     KosExp *exp = handle;
  72.  
  73.     for (; exp->name; exp++) {
  74.         if (!strcmp(exp->name, name)) {
  75.             return exp->ptr;
  76.         }
  77.     }
  78.     __error = "Symbol not found";
  79.     return NULL;
  80. }
  81.  
  82. // https://pubs.opengroup.org/onlinepubs/007908799/xsh/dlclose.html
  83. int dlclose(void *handle) {
  84.     return 0;
  85. }
  86.  
  87. // https://pubs.opengroup.org/onlinepubs/007908799/xsh/dlerror.html
  88. char *dlerror(void) {
  89.     char *ret = __error ? strdup(__error) : NULL;
  90.     __error = NULL;
  91.     return ret;
  92. }
  93.