Subversion Repositories Kolibri OS

Rev

Rev 5940 | Rev 8227 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. ;-----------------------------------------------------------------------------
  2. ; load one or more DLL file in COFF format and try to import functions by our list
  3. ; if first function in import list begins with 'lib_', call it as DLL initialization
  4. ; return eax = 1 as fail, if anyone of .obj file not found in /sys/lib
  5. ; return 0 if all fine, but 0 not garantees in succesfull import - see dll.Link comment
  6. ; dirties all registers! eax, ebx, ecx, edx, esi, edi
  7. proc dll.Load, import_table:dword
  8.         mov     esi, [import_table]
  9.   .next_lib:
  10.         mov     edx, [esi]
  11.         or      edx, edx
  12.         jz      .exit
  13.         push    esi
  14.         mov     esi, [esi + 4]
  15.         mov     edi, s_libdir.fname
  16.     @@:
  17.         lodsb
  18.         stosb
  19.         or      al, al
  20.         jnz     @b
  21.         mcall   68, 19, s_libdir
  22.         or      eax, eax
  23.         jz      .fail
  24.         stdcall dll.Link, eax, edx
  25.         push    eax
  26.         mov     eax, [eax]
  27.         cmp     dword[eax], 'lib_'
  28.         pop     eax
  29.         jnz     @f
  30.         stdcall dll.Init, [eax + 4]
  31.     @@:
  32.         pop     esi
  33.         add     esi, 8
  34.         jmp     .next_lib
  35.   .exit:
  36.         xor     eax, eax
  37.         ret
  38.   .fail:
  39.         add     esp, 4
  40.         xor     eax, eax
  41.         inc     eax
  42.         ret
  43. endp
  44. ;-----------------------------------------------------------------------------
  45. ; scans dll export table for a functions we want to import
  46. ; break scan on first unresolved import
  47. ; no return value
  48. proc dll.Link, exp:dword, imp:dword
  49.         push    eax
  50.         mov     esi, [imp]
  51.         test    esi, esi
  52.         jz      .done
  53.   .next:
  54.         lodsd
  55.         test    eax, eax
  56.         jz      .done
  57.         stdcall dll.GetProcAddress, [exp], eax
  58.         or      eax, eax
  59.         jz      @f
  60.         mov     [esi - 4], eax
  61.         jmp     .next
  62.     @@:
  63.         mov     dword[esp], 0
  64.   .done:
  65.         pop     eax
  66.         ret
  67. endp
  68. ;-----------------------------------------------------------------------------
  69. ; calls lib_init with predefined parameters  
  70. ; no return value
  71. proc dll.Init, dllentry:dword
  72.         pushad
  73.         mov     eax, mem.Alloc
  74.         mov     ebx, mem.Free
  75.         mov     ecx, mem.ReAlloc
  76.         mov     edx, dll.Load
  77.         stdcall [dllentry]
  78.         popad
  79.         ret
  80. endp
  81. ;-----------------------------------------------------------------------------
  82. ; scans export table for a sz_name function
  83. ; returns in eax function address or 0 if not found
  84. proc dll.GetProcAddress, exp:dword, sz_name:dword
  85.         mov     edx, [exp]
  86.         xor     eax, eax
  87.   .next:
  88.         or      edx, edx
  89.         jz      .end
  90.         cmp     dword[edx], 0
  91.         jz      .end
  92.         stdcall strcmp, [edx], [sz_name]
  93.         test    eax, eax
  94.         jz      .ok
  95.         add     edx, 8
  96.         jmp     .next
  97.   .ok:
  98.         mov     eax, [edx + 4]
  99.   .end:
  100.         cmp eax, -1
  101.         jnz @f
  102.         xor eax, eax
  103.   @@:
  104.         ret
  105. endp
  106. ;-----------------------------------------------------------------------------
  107. ; compares strings
  108. ; returns eax = 0 if equal, -1 otherwise
  109. proc strcmp, str1:dword, str2:dword
  110.         push    esi edi
  111.         mov     esi, [str1]
  112.         mov     edi, [str2]
  113.         xor     eax, eax
  114.     @@:
  115.         lodsb
  116.         scasb
  117.         jne     .fail
  118.         or      al, al
  119.         jnz     @b
  120.         jmp     .ok
  121.   .fail:
  122.         or      eax, -1
  123.   .ok:
  124.         pop     edi esi
  125.         ret
  126. endp
  127. ;-----------------------------------------------------------------------------
  128. s_libdir:
  129.   db '/sys/lib/'
  130.   .fname rb 32
  131. ;-----------------------------------------------------------------------------
  132. proc mem.Alloc, size
  133.         push    ebx ecx
  134.         mov     ecx, [size]
  135.         mcall   68, 12
  136.         pop     ecx ebx
  137.         ret
  138. endp
  139. ;-----------------------------------------------------------------------------
  140. proc mem.ReAlloc, mptr, size
  141.         push    ebx ecx edx
  142.         mov     ecx, [size]
  143.         mov     edx, [mptr]
  144.         mcall   68, 20
  145.         pop     edx ecx ebx
  146.         ret
  147. endp
  148. ;-----------------------------------------------------------------------------
  149. proc mem.Free, mptr
  150.         push    ebx ecx
  151.         mov     ecx,[mptr]
  152.         mcall   68, 13
  153.         pop     ecx ebx
  154.         ret
  155. endp
  156. ;-----------------------------------------------------------------------------
  157.