Subversion Repositories Kolibri OS

Rev

Rev 7219 | Rev 7984 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #ifndef INCLUDE_DLL_H
  2. #define INCLUDE_DLL_H
  3. #print "[include <dll.h>]\n"
  4.  
  5. #ifndef INCLUDE_FILESYSTEM_H
  6. #include "../lib/fs.h"
  7. #endif
  8.  
  9. #ifdef LANG_RUS
  10.         #define _TEXT_ERROR_ADD "'Žè¨¡ª  ¯à¨ § £à㧪¥ ¡¨¡«¨®â¥ª¨"
  11. #elif LANG_EST
  12.         #define _TEXT_ERROR_ADD "'Viga teegi laadimisel"
  13. #else
  14.         #define _TEXT_ERROR_ADD "'Error while loading library"
  15. #endif
  16.  
  17. char a_libdir[43]  = "/sys/lib/\0";
  18.  
  19. :inline void error_init(dword text)
  20. {
  21.         dword TEXT_ERROR[1024];
  22.         sprintf(TEXT_ERROR, "%s `%s`' -E",_TEXT_ERROR_ADD,text);
  23.         notify(TEXT_ERROR);
  24. }
  25.  
  26. // stdcall with 1 parameter
  27. :void dll_Load() {
  28. asm {
  29.         push    ebp
  30.         mov     ebp, esp
  31.         mov     esi, SSDWORD[EBP+8]
  32.                 @next_lib:    
  33.         mov     edx, DSDWORD[ESI]
  34.         or      edx, edx
  35.         jz      exit
  36.         push    esi
  37.         mov     esi, DSDWORD[ESI+4]
  38.         mov     edi, #a_libdir+9
  39.  
  40. @loc01:
  41.         lodsb
  42.         stosb
  43.         or      al, al
  44.         jnz     loc01
  45.  
  46.         mov     eax, 68
  47.         mov     ebx, 19
  48.         mov     ecx, #a_libdir
  49.         int     0x40
  50.         or      eax, eax
  51.         jz      fail
  52.  
  53.         push    edx
  54.         push    eax
  55.         call    dll_Link
  56.  
  57.         push    eax
  58.         mov     eax, DSDWORD[eax]
  59.         cmp     DSDWORD[EAX], '_bil'    // somehow this needs to be reversed..
  60.         pop     eax
  61.         jnz     loc02
  62.  
  63.         push    DSDWORD[EAX+4]
  64.         call    dll_Init
  65. @loc02:
  66.  
  67.         pop     esi
  68.         add     esi, 8
  69.         jmp     next_lib
  70. @exit:
  71.         xor     eax, eax
  72.         leave
  73.         ret     4
  74.        
  75. @fail:        
  76.         add     esp, 4
  77.         xor     eax, eax
  78.         inc     eax
  79.         leave
  80.         ret     4
  81.     }
  82. }
  83.  
  84. //stdcall with 2 parameters
  85. :void dll_Link() {
  86. asm {
  87.         push    ebp
  88.         mov     ebp, esp
  89.         push    eax
  90.         mov     esi, SSDWORD[EBP+12]
  91.         test    esi, esi
  92.         jz      done
  93. @next:        
  94.         lodsd
  95.         test    eax, eax
  96.         jz      done
  97.         push    eax
  98.         push    SSDWORD[EBP+8]
  99.         call    dll_GetProcAddress
  100.         or      eax, eax
  101.         jz      loc03
  102.         mov     DSDWORD[esi-4], eax
  103.         jmp     next
  104. @loc03:
  105.         mov     SSDWORD[esp], 0
  106. @done:
  107.         pop     eax
  108.         leave
  109.         ret     8
  110.     }
  111. }
  112.  
  113.  
  114. //stdcall with 1 parameter
  115. :void dll_Init() {
  116. asm {
  117.         push    ebp
  118.         mov     ebp, esp
  119.         pushad
  120.         mov     eax, #malloc
  121.         mov     ebx, #free;
  122.         mov     ecx, #realloc;
  123.         mov     edx, #dll_Load;
  124.         call    SSDWORD[EBP+8]
  125.         popad
  126.         leave
  127.         ret     4
  128.     }
  129. }
  130.  
  131.  
  132. // stdcall with 2 parameters
  133. :void dll_GetProcAddress(){
  134. asm {
  135.         push    ebp
  136.         mov     ebp, esp
  137.         mov     edx, CSDWORD[EBP+8]
  138.         xor     eax, eax
  139.  
  140. @next:        
  141.         or      edx, edx
  142.         jz      end
  143.         cmp     CSDWORD[edx], 0
  144.         jz      end
  145.  
  146.         push    CSDWORD[EBP+12]
  147.         push    CSDWORD[EDX]
  148.         call    dll_StrCmp
  149.         test    eax, eax
  150.         jz      ok
  151.         add     edx, 8
  152.         jmp     next
  153. @ok:
  154.         mov     eax, DSDWORD[EDX+4]
  155. @end:
  156.         leave
  157.         ret     8
  158.     }
  159. }
  160.  
  161.  
  162. // stdcall with 2 parameters
  163. :void dll_StrCmp() {
  164. asm {
  165.         push    ebp
  166.         mov     ebp, esp
  167.         push    esi
  168.         push    edi
  169.         mov     esi, CSDWORD[EBP+8]
  170.         mov     edi, CSDWORD[EBP+12]
  171.         xor     eax, eax
  172. @label1:
  173.         lodsb
  174.         scasb
  175.         jne     fail
  176.         or      al, al
  177.         jnz     label1
  178.         jmp     label_ok
  179. @fail:
  180.         or      eax, -1
  181. @label_ok:
  182.         pop     edi
  183.         pop     esi
  184.         leave
  185.         ret     8
  186.     }
  187. }
  188.  
  189. :int load_dll2(dword dllname, import_table, byte need_init)
  190. {
  191.    //dword dllentry=0;
  192. // load DLL
  193.         $mov     eax, 68
  194.         $mov     ebx, 19
  195.         ECX=dllname;
  196.         $int     0x40
  197.         $test    eax, eax
  198.         $jz      exit01
  199.  
  200. // initialize import
  201.         $mov     edx,eax
  202.         ESI=import_table;
  203.  
  204. @import_loop01:
  205.         $lodsd
  206.         $test    eax,eax
  207.         $jz      import_done01
  208.         $push    edx
  209. @import_find01:
  210.         $mov     ebx,DSDWORD[EDX]
  211.         $test    ebx, ebx
  212.         $jz      exit01
  213.         $push    eax
  214. @nex101:
  215.         $mov     cl,DSBYTE[EAX];
  216.         $cmp     cl,DSBYTE[EBX];
  217.         $jnz     import_find_next01
  218.         $test    cl,cl
  219.         $jz      import_found01
  220.         $inc     eax
  221.         $inc     ebx
  222.         $jmp     nex101
  223. @import_find_next01:
  224.         $pop     eax
  225.         $add     edx, 8
  226.         $jmp     import_find01
  227. @import_found01:
  228.         $pop     eax
  229.         $mov     eax,DSDWORD[edx+4]
  230.         $mov     DSDWORD[esi-4],eax
  231.         $pop     edx
  232.        
  233.         $jmp     import_loop01
  234. @import_done01:
  235.         IF (need_init) dll_Init (DSDWORD[EDX+4]);
  236.         return 0;
  237. @exit01:
  238.         return -1;
  239. }
  240.  
  241. :byte load_dll(dword dllname, import_table, byte need_init)
  242. {
  243.         if (load_dll2(dllname, import_table, need_init))
  244.         {
  245.                 error_init(dllname);
  246.                 return false;
  247.         }
  248.         return true;
  249. }
  250.  
  251.  
  252. #endif