Subversion Repositories Kolibri OS

Rev

Rev 9716 | 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_kbhit, 0
  204. public_jmp  con_set_cursor_pos, 8
  205. public_jmp  con_get_cursor_pos, 8
  206. public_jmp  con_write_string, 8
  207. public_jmp  con_write_asciiz, 4
  208. public_jmp  con_get_flags, 0
  209. public_jmp  con_set_flags, 4
  210. public_jmp  con_set_title, 4
  211. public_jmp  con_get_font_height, 0
  212. public_jmp  con_get_cursor_height, 0
  213. public_jmp  con_set_cursor_height, 4
  214.  
  215. section '.data' align 16 readable writable
  216.  
  217. ; -------------------------
  218. macro library [lname,fname]
  219. {
  220.   forward
  221.     dd __#lname#_library_table__,__#lname#_library_name__
  222.   common
  223.     dd 0
  224.   forward
  225.     align 4
  226.     __#lname#_library_name__ db fname,0
  227. }
  228.  
  229. macro import lname,[name,sname]
  230. {
  231.   common
  232.     align 4
  233.     __#lname#_library_table__:
  234.   forward
  235.     if used name
  236.       name dd __#name#_import_name__
  237.     end if
  238.   common
  239.     dd 0
  240.   forward
  241.     if used name
  242.       align 4
  243.       __#name#_import_name__ db sname,0
  244.     end if
  245. }
  246.  
  247. align   4
  248. @IMPORT:
  249.  
  250. library console,        'console.obj'
  251.  
  252. import  console, \
  253.         con_start,      'START', \
  254.         con_init,       'con_init', \
  255.         con_exit,       'con_exit', \
  256.         con_gets,       'con_gets', \
  257.         con_gets2,      'con_gets2', \
  258.         con_cls,        'con_cls', \
  259.         con_getch2,     'con_getch2', \
  260.         con_getch,      'con_getch', \
  261.         con_kbhit,      'con_kbhit', \
  262.         con_set_cursor_pos, 'con_set_cursor_pos',\
  263.         con_get_cursor_pos, 'con_get_cursor_pos', \
  264.         con_write_string, 'con_write_string',\
  265.         con_write_asciiz, 'con_write_asciiz', \
  266.         con_get_flags,  'con_get_flags', \
  267.         con_set_flags,  'con_set_flags', \
  268.         con_set_title,  'con_set_title', \
  269.         con_get_font_height, 'con_get_font_height', \
  270.         con_get_cursor_height, 'con_get_cursor_height', \
  271.         con_set_cursor_height, 'con_set_cursor_height'
  272.        
  273. s_libdir:
  274.   db '/sys/lib/'
  275.   .fname rb 32
  276.