Subversion Repositories Kolibri OS

Rev

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

  1. ;
  2. ;  (C) KolibriOS team (original dll.inc)
  3. ;  (C) 2022, Edited by Coldy
  4. ;
  5. ;   This module based on original dll.inc.
  6. ;  
  7. ;   - Improved error handling. Now dll.Load return 0 is success guarantie
  8. ;     Also added corrsponding error codes if one of library or entry not found  
  9. ;
  10.  
  11.  
  12. ERROR_LIBRARY_NOT_LOAD  =   0x100
  13. ERROR_ENTRY_NOT_FOUND   =   0x101
  14.  
  15.  
  16. ;-----------------------------------------------------------------------------
  17. ; load one or more DLL file in COFF format and try to import functions by our list
  18. ; if first function in import list begins with 'lib_', call it as DLL initialization
  19. ; return eax = 1 as fail, if anyone of .obj file not found in /sys/lib
  20. ; return 0 if all fine or error code LIBRARY_NOT_LOAD or ENTRY_NOT_FOUND
  21. ; dirties all registers! eax, ebx, ecx, edx, esi, edi
  22. proc dll.Load, import_table:dword
  23.         mov     esi, [import_table]
  24.   .next_lib:
  25.         mov     edx, [esi]
  26.         or      edx, edx
  27.         jz      .exit
  28.         push    esi
  29.         mov     esi, [esi + 4]
  30.         mov     edi, s_libdir.fname
  31.     @@:
  32.         lodsb
  33.         stosb
  34.         or      al, al
  35.         jnz     @b
  36.         mcall   68, 19, s_libdir
  37.         or      eax, eax
  38.         jz      .fail_load
  39.   push eax
  40.         stdcall dll.Link, eax, edx
  41.   test  eax, eax
  42.         jnz     .fail_link
  43.         ;push   eax
  44.   mov eax, [esp]
  45.         mov     eax, [eax]
  46.         cmp     dword[eax], 'lib_'
  47.         pop     eax
  48.         jnz     @f
  49.         stdcall dll.Init, [eax + 4]
  50.     @@:
  51.         pop     esi
  52.         add     esi, 8
  53.         jmp     .next_lib
  54.   .exit:
  55.         xor     eax, eax
  56.         ret
  57.   .fail_load:
  58.         add     esp, 4
  59.         ;xor    eax, eax
  60.         ;inc    eax
  61.   mov eax, ERROR_LIBRARY_NOT_LOAD
  62.   ret
  63.   .fail_link:
  64.   add   esp, 4
  65.         ret
  66. endp
  67. ;-----------------------------------------------------------------------------
  68. ; scans dll export table for a functions we want to import
  69. ; break scan on first unresolved import
  70. ; return value: 0 - success or ENTRY_NOT_FOUND
  71. proc dll.Link, exp:dword, imp:dword
  72.         ;push   eax
  73.         mov     esi, [imp]
  74.   ; Import table alreary checked in APP_STARTUP_THUNK
  75.         ;test   esi, esi
  76.         ;jz     .fail1;.done
  77.   .next:
  78.         lodsd
  79.         test    eax, eax
  80.         jz      .done
  81.   mov ebx, eax
  82.         stdcall dll.GetProcAddress, [exp], eax
  83.         or      eax, eax
  84.         jz      .fail ;.done
  85.         mov     [esi - 4], eax
  86.         jmp     .next
  87.   ;  @@:
  88.         ;mov    dword[esp], 0
  89.   ;.fail1:
  90.   ; No imports
  91.   ;mov eax, BAD_IMAGE
  92.   ;jmp .done
  93.   .fail:
  94.   mov [szEntryName],ebx
  95.   mov eax, ERROR_ENTRY_NOT_FOUND
  96.   .done:
  97.         ;pop    eax  
  98.         ret
  99. endp
  100. ;-----------------------------------------------------------------------------
  101. ; calls lib_init with predefined parameters
  102. ; no return value
  103. proc dll.Init, dllentry:dword
  104.         pushad
  105.         mov     eax, mem.Alloc
  106.         mov     ebx, mem.Free
  107.         mov     ecx, mem.ReAlloc
  108.         mov     edx, dll.Load
  109.         stdcall [dllentry]
  110.         popad
  111.         ret
  112. endp
  113. ;-----------------------------------------------------------------------------
  114. ; scans export table for a sz_name function
  115. ; returns in eax function address or 0 if not found
  116. proc dll.GetProcAddress, exp:dword, sz_name:dword
  117.         mov     edx, [exp]
  118.         xor     eax, eax
  119.   .next:
  120.         or      edx, edx
  121.         jz      .end
  122.         cmp     dword[edx], 0
  123.         jz      .end
  124.         stdcall strcmp, [edx], [sz_name]
  125.         test    eax, eax
  126.         jz      .ok
  127.         add     edx, 8
  128.         jmp     .next
  129.   .ok:
  130.         mov     eax, [edx + 4]
  131.   .end:
  132.         cmp eax, -1
  133.         jnz @f
  134.         xor eax, eax
  135.   @@:
  136.         ret
  137. endp
  138. ;-----------------------------------------------------------------------------
  139. ; compares strings
  140. ; returns eax = 0 if equal, -1 otherwise
  141. proc strcmp, str1:dword, str2:dword
  142.         push    esi edi
  143.         mov     esi, [str1]
  144.         mov     edi, [str2]
  145.         xor     eax, eax
  146.     @@:
  147.         lodsb
  148.         scasb
  149.         jne     .fail
  150.         or      al, al
  151.         jnz     @b
  152.         jmp     .ok
  153.   .fail:
  154.         or      eax, -1
  155.   .ok:
  156.         pop     edi esi
  157.         ret
  158. endp
  159. ;-----------------------------------------------------------------------------
  160.  
  161. s_libdir:
  162.   db '/sys/lib/'
  163.   .fname rb 32
  164.  
  165. szEntryName dd  0
  166.  
  167. ;-----------------------------------------------------------------------------
  168. proc mem.Alloc, size
  169.         push    ebx ecx
  170.         mov     ecx, [size]
  171.         mcall   68, 12
  172.         pop     ecx ebx
  173.         ret
  174. endp
  175. ;-----------------------------------------------------------------------------
  176. proc mem.ReAlloc, mptr, size
  177.         push    ebx ecx edx
  178.         mov     ecx, [size]
  179.         mov     edx, [mptr]
  180.         mcall   68, 20
  181.         pop     edx ecx ebx
  182.         ret
  183. endp
  184. ;-----------------------------------------------------------------------------
  185. proc mem.Free, mptr
  186.         push    ebx ecx
  187.         mov     ecx,[mptr]
  188.         mcall   68, 13
  189.         pop     ecx ebx
  190.         ret
  191. endp
  192. ;-----------------------------------------------------------------------------
  193.