Subversion Repositories Kolibri OS

Rev

Rev 9669 | 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.  
  31.         mov     edi, esi
  32.         cmp     byte[esi], '/'
  33.         jz      .load_lib
  34.  
  35.         mov     edi, s_libdir.fname
  36.     @@:
  37.         lodsb
  38.         stosb
  39.         or      al, al
  40.         jnz     @b
  41.  
  42.         mov     edi, s_libdir
  43.   .load_lib:
  44.         mcall   68, 19, edi ;s_libdir
  45.         or      eax, eax
  46.         jz      .fail_load
  47.         push eax
  48.         stdcall dll.Link, eax, edx
  49.         test  eax, eax
  50.         jnz     .fail_link
  51.         ;push   eax
  52.         mov eax, [esp]
  53.         mov     eax, [eax]
  54.         cmp     dword[eax], 'lib_'
  55.         pop     eax
  56.         jnz     @f
  57.         stdcall dll.Init, [eax + 4]
  58.     @@:
  59.         pop     esi
  60.         add     esi, 8
  61.         jmp     .next_lib
  62.   .exit:
  63.         xor     eax, eax
  64.         ret
  65.   .fail_load:
  66.         add     esp, 4
  67.         ;xor    eax, eax
  68.         ;inc    eax
  69.   mov eax, ERROR_LIBRARY_NOT_LOAD
  70.   ret
  71.   .fail_link:
  72.   add   esp, 4
  73.         ret
  74. endp
  75. ;-----------------------------------------------------------------------------
  76. ; scans dll export table for a functions we want to import
  77. ; break scan on first unresolved import
  78. ; return value: 0 - success or ENTRY_NOT_FOUND
  79. proc dll.Link, exp:dword, imp:dword
  80.         ;push   eax
  81.         mov     esi, [imp]
  82.   ; Import table alreary checked in APP_STARTUP_THUNK
  83.         ;test   esi, esi
  84.         ;jz     .fail1;.done
  85.   .next:
  86.         lodsd
  87.         test    eax, eax
  88.         jz      .done
  89.   mov ebx, eax
  90.         stdcall dll.GetProcAddress, [exp], eax
  91.         or      eax, eax
  92.         jz      .fail ;.done
  93.         mov     [esi - 4], eax
  94.         jmp     .next
  95.   ;  @@:
  96.         ;mov    dword[esp], 0
  97.   ;.fail1:
  98.   ; No imports
  99.   ;mov eax, BAD_IMAGE
  100.   ;jmp .done
  101.   .fail:
  102.   mov [szEntryName],ebx
  103.   mov eax, ERROR_ENTRY_NOT_FOUND
  104.   .done:
  105.         ;pop    eax  
  106.         ret
  107. endp
  108. ;-----------------------------------------------------------------------------
  109. ; calls lib_init with predefined parameters
  110. ; no return value
  111. proc dll.Init, dllentry:dword
  112.         pushad
  113.         mov     eax, mem.Alloc
  114.         mov     ebx, mem.Free
  115.         mov     ecx, mem.ReAlloc
  116.         mov     edx, dll.Load
  117.         stdcall [dllentry]
  118.         popad
  119.         ret
  120. endp
  121. ;-----------------------------------------------------------------------------
  122. ; scans export table for a sz_name function
  123. ; returns in eax function address or 0 if not found
  124. proc dll.GetProcAddress, exp:dword, sz_name:dword
  125.         mov     edx, [exp]
  126.         xor     eax, eax
  127.   .next:
  128.         or      edx, edx
  129.         jz      .end
  130.         cmp     dword[edx], 0
  131.         jz      .end
  132.         stdcall strcmp, [edx], [sz_name]
  133.         test    eax, eax
  134.         jz      .ok
  135.         add     edx, 8
  136.         jmp     .next
  137.   .ok:
  138.         mov     eax, [edx + 4]
  139.   .end:
  140.         cmp eax, -1
  141.         jnz @f
  142.         xor eax, eax
  143.   @@:
  144.         ret
  145. endp
  146. ;-----------------------------------------------------------------------------
  147. ; compares strings
  148. ; returns eax = 0 if equal, -1 otherwise
  149. proc strcmp, str1:dword, str2:dword
  150.         push    esi edi
  151.         mov     esi, [str1]
  152.         mov     edi, [str2]
  153.         xor     eax, eax
  154.     @@:
  155.         lodsb
  156.         scasb
  157.         jne     .fail
  158.         or      al, al
  159.         jnz     @b
  160.         jmp     .ok
  161.   .fail:
  162.         or      eax, -1
  163.   .ok:
  164.         pop     edi esi
  165.         ret
  166. endp
  167. ;-----------------------------------------------------------------------------
  168.  
  169. s_libdir:
  170.   db '/sys/lib/'
  171.   .fname rb 32
  172.  
  173. szEntryName dd  0
  174.  
  175. ;-----------------------------------------------------------------------------
  176. proc mem.Alloc, size
  177.         push    ebx ecx
  178.         mov     ecx, [size]
  179.         mcall   68, 12
  180.         pop     ecx ebx
  181.         ret
  182. endp
  183. ;-----------------------------------------------------------------------------
  184. proc mem.ReAlloc, mptr, size
  185.         push    ebx ecx edx
  186.         mov     ecx, [size]
  187.         mov     edx, [mptr]
  188.         mcall   68, 20
  189.         pop     edx ecx ebx
  190.         ret
  191. endp
  192. ;-----------------------------------------------------------------------------
  193. proc mem.Free, mptr
  194.         push    ebx ecx
  195.         mov     ecx,[mptr]
  196.         mcall   68, 13
  197.         pop     ecx ebx
  198.         ret
  199. endp
  200. ;-----------------------------------------------------------------------------
  201.