Subversion Repositories Kolibri OS

Rev

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

  1. format MS COFF
  2.  
  3. include '../proc32.inc'
  4.  
  5. public _load_libconsole
  6.  
  7. macro public_jmp name, size
  8. {
  9.   public _#name#@#size
  10.   _#name#@#size: jmp [name]
  11. }
  12.  
  13. section '.text' align 16 code readable executable
  14.  
  15. ;void* __fastcall getprocaddr(export, name)
  16. align 4
  17. getprocaddress:
  18.         push    esi
  19.         push    edi
  20.  
  21.         xor     eax, eax
  22.         test    ecx, ecx        ; If hlib = 0 then goto .end
  23.         jz      .end
  24. .next:
  25.         cmp     [ecx], dword 0  ; If end of export table then goto .end
  26.         jz      .end
  27.  
  28.         xor     eax, eax
  29.         mov     esi, [ecx]
  30.         mov     edi, edx        ; name
  31. .next_:
  32.         lodsb
  33.         scasb
  34.         jne     .fail
  35.         or      al, al
  36.         jnz     .next_
  37.         jmp     .ok
  38. .fail:
  39.         add     ecx, 8
  40.         jmp     .next
  41. .ok:
  42.         mov eax, [ecx + 4]      ; return address
  43. .end:
  44.         pop     edi
  45.         pop     esi
  46.         ret
  47.  
  48.  
  49. ;void fastcall dll_link(export, import)
  50.  
  51. align 4
  52. dll_link:
  53.         push    esi
  54.         push    ecx
  55.         mov     esi, edx
  56.         test    esi, esi
  57.         jz      .done
  58. .next:
  59.         mov     edx, [esi]
  60.         test    edx, edx
  61.         jz      .done
  62.         mov     ecx, [esp]
  63.         call    getprocaddress
  64.         test    eax, eax
  65.         jz      .done
  66.         mov     [esi], eax
  67.         add     esi, 4
  68.         jmp     .next
  69. .done:
  70.         pop     ecx
  71.         pop     esi
  72.         ret
  73.  
  74. align 4
  75. dll_load:
  76.         push    ebp
  77.         push    ebx
  78.         push    esi
  79.         push    edi
  80.  
  81.         mov     ebp, [esp+20]
  82. .next_lib:
  83.         mov     edx, [ebp]
  84.         test    edx, edx
  85.         jz      .exit
  86.  
  87.         mov     esi, [ebp+4]
  88.         mov     edi, s_libdir.fname
  89. @@:
  90.         lodsb
  91.         stosb
  92.         test  al, al
  93.         jnz @b
  94.  
  95.         mov     eax, 68
  96.         mov     ebx, 19
  97.         mov     ecx, s_libdir
  98.         int     0x40
  99.         test    eax, eax
  100.         jz      .fail
  101.  
  102.         mov     ecx, eax
  103.         call    dll_link
  104.         mov     eax, [ecx]
  105.         cmp     dword[eax], 'lib_'
  106.         jnz     @f
  107.  
  108.         mov     esi, [ecx+4]
  109.  
  110.         pushad
  111.         mov     eax, mem.Alloc
  112.         mov     ebx, mem.Free
  113.         mov     ecx, mem.ReAlloc
  114.         mov     edx, dll_load
  115.         call    esi
  116.         popad
  117. @@:
  118.         add     ebp, 8
  119.         jmp     .next_lib
  120. .exit:
  121.         pop     edi
  122.         pop     esi
  123.         pop     ebx
  124.         pop     ebp
  125.         xor     eax, eax
  126.         ret 4
  127. .fail:
  128.         pop     edi
  129.         pop     esi
  130.         pop     ebx
  131.         pop     ebp
  132.         inc     eax
  133.         ret 4
  134.  
  135. align 4
  136. _load_libconsole:
  137.         push    ebx
  138.         mov     eax, 40
  139.         mov     ebx, 1 shl 8
  140.         int     0x40
  141.         pop     ebx
  142.  
  143.         push    @IMPORT
  144.         call    dll_load
  145.         test    eax, eax
  146.         jnz     .fail
  147.         push    1
  148.         call    [con_start]
  149.         xor     eax, eax
  150. .fail:
  151.         ret
  152.  
  153. proc mem.Alloc, size
  154.         push    ebx ecx
  155.         mov     ecx, [size]
  156.         mov     eax, 68
  157.         mov     ebx, 12
  158.         int     0x40
  159.         pop     ecx ebx
  160.         ret
  161. endp
  162. ;-----------------------------------------------------------------------------
  163. proc mem.ReAlloc, mptr, size
  164.         push    ebx ecx edx
  165.         mov     ecx, [size]
  166.         test    ecx, ecx
  167.         jz      @f
  168. @@:
  169.         mov     edx, [mptr]
  170.         test    edx, edx
  171.         jz      @f
  172. @@:
  173.         mov     eax, 68
  174.         mov     ebx, 20
  175.         int     0x40
  176.         test    eax, eax
  177.         jz  @f
  178. @@:
  179.         pop edx ecx ebx
  180.         ret
  181. endp
  182. ;-----------------------------------------------------------------------------
  183. proc mem.Free, mptr
  184.         push    ebx ecx
  185.         mov     ecx,[mptr]
  186.         test    ecx,ecx
  187.         jz  @f
  188. @@:
  189.         mov     eax, 68
  190.         mov     ebx, 13
  191.         int     0x40
  192.         pop ecx ebx
  193.         ret
  194. endp
  195.  
  196. public_jmp  con_init, 20
  197. public_jmp  con_exit, 4
  198. public_jmp  con_gets, 8
  199. public_jmp  con_gets2, 12
  200. public_jmp  con_cls, 0
  201. public_jmp  con_getch2, 0
  202. public_jmp  con_getch, 0
  203. public_jmp  con_set_cursor_pos, 8
  204. public_jmp  con_get_cursor_pos, 8
  205. public_jmp  con_write_string, 8
  206. public_jmp  con_write_asciiz, 4
  207. public_jmp  con_get_flags, 0
  208. public_jmp  con_set_flags, 4
  209. public_jmp  con_set_title, 4
  210. public_jmp  con_get_font_height, 0
  211. public_jmp  con_get_cursor_height, 0
  212. public_jmp  con_set_cursor_height, 4
  213.  
  214. section '.data' align 16 readable writable
  215.  
  216. ; -------------------------
  217. macro library [lname,fname]
  218. {
  219.   forward
  220.     dd __#lname#_library_table__,__#lname#_library_name__
  221.   common
  222.     dd 0
  223.   forward
  224.     align 4
  225.     __#lname#_library_name__ db fname,0
  226. }
  227.  
  228. macro import lname,[name,sname]
  229. {
  230.   common
  231.     align 4
  232.     __#lname#_library_table__:
  233.   forward
  234.     if used name
  235.       name dd __#name#_import_name__
  236.     end if
  237.   common
  238.     dd 0
  239.   forward
  240.     if used name
  241.       align 4
  242.       __#name#_import_name__ db sname,0
  243.     end if
  244. }
  245.  
  246. align   4
  247. @IMPORT:
  248.  
  249. library console,        'console.obj'
  250.  
  251. import  console, \
  252.         con_start,      'START', \
  253.         con_init,       'con_init', \
  254.         con_exit,       'con_exit', \
  255.         con_gets,       'con_gets', \
  256.         con_gets2,      'con_gets2', \
  257.         con_cls,        'con_cls', \
  258.         con_getch2,     'con_getch2', \
  259.         con_getch,      'con_getch', \
  260.         con_set_cursor_pos, 'con_set_cursor_pos',\
  261.         con_get_cursor_pos, 'con_get_cursor_pos', \
  262.         con_write_string, 'con_write_string',\
  263.         con_write_asciiz, 'con_write_asciiz', \
  264.         con_get_flags,  'con_get_flags', \
  265.         con_set_flags,  'con_set_flags', \
  266.         con_set_title,  'con_set_title', \
  267.         con_get_font_height, 'con_get_font_height', \
  268.         con_get_cursor_height, 'con_get_cursor_height', \
  269.         con_set_cursor_height, 'con_set_cursor_height'
  270.        
  271. s_libdir:
  272.   db '/sys/lib/'
  273.   .fname rb 32
  274.