Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. ;============================================================================
  9. ;
  10. ;   External kernel dependencies (libraries) loading
  11. ;
  12. ;============================================================================
  13.  
  14. $Revision: 1289 $
  15.  
  16. if 0
  17. ; The code currently does not work. Kill "if 0/end if" only after correcting
  18. ; to current kernel (dll.inc).
  19. macro library [name,fname]
  20. {
  21.   forward
  22.     dd __#name#_library_table__,__#name#_library_name__
  23.   common
  24.     dd 0
  25.   forward
  26.     __#name#_library_name__ db fname,0
  27. }
  28.  
  29. macro import lname,[name,sname]
  30. {
  31.   common
  32.     align 4
  33.     __#lname#_library_table__:
  34.   forward
  35.     name dd __#name#_import_name__
  36.   common
  37.     dd 0
  38.   forward
  39.     __#name#_import_name__ db sname,0
  40. }
  41.  
  42. macro export [name,sname]
  43. {
  44. align 4
  45.   forward
  46.     dd __#name#_export_name__,name
  47.   common
  48.     dd 0
  49.   forward
  50.     __#name#_export_name__ db sname,0
  51. }
  52.  
  53.  
  54.  
  55. align 4            ; loading library (use kernel functions)
  56. proc load_k_library stdcall, file_name:dword
  57.            locals
  58.              coff      dd ?
  59.              sym       dd ?
  60.              strings   dd ?
  61.              img_size  dd ?
  62.              img_base  dd ?
  63.              exports   dd ?
  64.            endl
  65.  
  66.                    cli
  67.  
  68.            stdcall load_file, [file_name]
  69.            test eax, eax
  70.            jz .fail
  71.  
  72.            mov [coff], eax
  73.            movzx ecx, [eax+CFH.nSections]
  74.            xor ebx, ebx
  75.  
  76.            lea edx, [eax+20]
  77. @@:
  78.            add ebx, [edx+CFS.SizeOfRawData]
  79.            add ebx, 15
  80.            and ebx, not 15
  81.            add edx, COFF_SECTION_SIZE
  82.            dec ecx
  83.            jnz @B
  84.            mov [img_size], ebx
  85.  
  86.            stdcall kernel_alloc, [img_size]
  87.  
  88.            test eax, eax
  89.            jz .fail
  90.            mov [img_base], eax
  91.  
  92.            mov edx, [coff]
  93.            movzx ebx, [edx+CFH.nSections]
  94.            mov edi, [img_base]
  95.            lea eax, [edx+20]
  96. @@:
  97.            mov [eax+CFS.VirtualAddress], edi
  98.            mov esi, [eax+CFS.PtrRawData]
  99.            test esi, esi
  100.            jnz .copy
  101.            add edi, [eax+CFS.SizeOfRawData]
  102.            jmp .next
  103. .copy:
  104.            add esi, edx
  105.            mov ecx, [eax+CFS.SizeOfRawData]
  106.            cld
  107.            rep movsb
  108. .next:
  109.            add edi, 15
  110.            and edi, not 15
  111.            add eax, COFF_SECTION_SIZE
  112.            dec ebx
  113.            jnz @B
  114.  
  115.            mov ebx, [edx+CFH.pSymTable]
  116.            add ebx, edx
  117.            mov [sym], ebx
  118.            mov ecx, [edx+CFH.nSymbols]
  119.            add ecx,ecx
  120.            lea ecx,[ecx+ecx*8] ;ecx*=18 = nSymbols*CSYM_SIZE
  121.            add ecx, [sym]
  122.            mov [strings], ecx
  123.  
  124.            lea eax, [edx+20]
  125.  
  126.            stdcall fix_coff_symbols, eax, [sym], [edx+CFH.nSymbols],\
  127.                                      [strings], dword 0
  128.            test eax, eax
  129.            jnz @F
  130.  
  131. @@:
  132.            mov edx, [coff]
  133.            movzx ebx, [edx+CFH.nSections]
  134.            mov edi, 0
  135.            lea eax, [edx+20]
  136. @@:
  137.            add [eax+CFS.VirtualAddress], edi  ;patch user space offset
  138.            add eax, COFF_SECTION_SIZE
  139.            dec ebx
  140.            jnz @B
  141.  
  142.            add edx, 20
  143.            stdcall fix_coff_relocs, [coff], edx, [sym]
  144.  
  145.            mov ebx, [coff]
  146.            stdcall get_coff_sym,[sym],[ebx+CFH.nSymbols],szEXPORTS
  147.            mov [exports], eax
  148.  
  149.            stdcall kernel_free, [coff]
  150.  
  151.            mov eax, [exports]
  152.            ret
  153. .fail:
  154.            xor eax, eax
  155.            ret
  156. endp
  157.  
  158.  
  159. proc dll.Load, import_table:dword
  160.                 mov     esi,[import_table]
  161.   .next_lib:    mov     edx,[esi]
  162.                 or      edx,edx
  163.                 jz      .exit
  164.                 push    esi
  165.  
  166.                 mov     edi,s_libname
  167.  
  168.                 mov     al, '/'
  169.                 stosb
  170.                 mov esi,sysdir_path
  171.             @@: lodsb
  172.                 stosb
  173.                 or      al,al
  174.                 jnz     @b
  175.                 dec edi
  176.                 mov [edi], dword '/lib'
  177.                 mov [edi+4],byte '/'
  178.                 add edi,5
  179.                 pop esi
  180.                 push esi
  181.                 mov     esi,[esi+4]
  182.             @@: lodsb
  183.                 stosb
  184.                 or      al,al
  185.                 jnz     @b
  186.  
  187.                 pushad
  188.                 stdcall load_k_library,s_libname
  189.                 mov [esp+28],eax
  190.                 popad
  191.                 or      eax,eax
  192.                 jz      .fail
  193.                 stdcall dll.Link,eax,edx
  194.                 stdcall dll.Init,[eax+4]
  195.                 pop     esi
  196.                 add     esi,8
  197.                 jmp     .next_lib
  198.   .exit:        xor     eax,eax
  199.                 ret
  200.   .fail:        add     esp,4
  201.                 xor     eax,eax
  202.                 inc     eax
  203.                 ret
  204. endp
  205.  
  206. proc dll.Link, exp:dword,imp:dword
  207.                 push    eax
  208.                 mov     esi,[imp]
  209.                 test    esi,esi
  210.                 jz      .done
  211.   .next:        lodsd
  212.                 test    eax,eax
  213.                 jz      .done
  214.                 stdcall dll.GetProcAddress,[exp],eax
  215.                 or      eax,eax
  216.                 jz      @f
  217.                 mov     [esi-4],eax
  218.                 jmp     .next
  219.             @@: mov     dword[esp],0
  220.   .done:        pop     eax
  221.                 ret
  222. endp
  223.  
  224. proc dll.Init, dllentry:dword
  225.                 pushad
  226.                 mov     eax,mem.Alloc
  227.                 mov     ebx,mem.Free
  228.                 mov     ecx,mem.ReAlloc
  229.                 mov     edx,dll.Load
  230.                 stdcall [dllentry]
  231.                 popad
  232.                 ret
  233. endp
  234.  
  235. proc dll.GetProcAddress, exp:dword,sz_name:dword
  236.                 mov     edx,[exp]
  237.   .next:        test    edx,edx
  238.                 jz      .end
  239.                 stdcall strncmp,[edx],[sz_name], dword -1
  240.                 test    eax,eax
  241.                 jz      .ok
  242.                 add     edx,8
  243.                 jmp     .next
  244.   .ok:          mov     eax,[edx+4]
  245.   .end:         ret
  246. endp
  247.  
  248. ;-----------------------------------------------------------------------------
  249. proc mem.Alloc size ;/////////////////////////////////////////////////////////
  250. ;-----------------------------------------------------------------------------
  251.         push    ebx ecx
  252. ;       mov     eax,[size]
  253. ;       lea     ecx,[eax+4+4095]
  254. ;       and     ecx,not 4095
  255. ;       stdcall kernel_alloc, ecx
  256. ;       add     ecx,-4
  257. ;       mov     [eax],ecx
  258. ;       add     eax,4
  259.  
  260.         stdcall kernel_alloc, [size]
  261.  
  262.         pop     ecx ebx
  263.         ret
  264. endp
  265.  
  266. ;-----------------------------------------------------------------------------
  267. proc mem.ReAlloc mptr,size;///////////////////////////////////////////////////
  268. ;-----------------------------------------------------------------------------
  269.         push    ebx ecx esi edi eax
  270.         mov     eax,[mptr]
  271.         mov     ebx,[size]
  272.         or      eax,eax
  273.         jz      @f
  274.         lea     ecx,[ebx+4+4095]
  275.         and     ecx,not 4095
  276.         add     ecx,-4
  277.         cmp     ecx,[eax-4]
  278.         je      .exit
  279.     @@: mov     eax,ebx
  280.         call    mem.Alloc
  281.         xchg    eax,[esp]
  282.         or      eax,eax
  283.         jz      .exit
  284.         mov     esi,eax
  285.         xchg    eax,[esp]
  286.         mov     edi,eax
  287.         mov     ecx,[esi-4]
  288.         cmp     ecx,[edi-4]
  289.         jbe     @f
  290.         mov     ecx,[edi-4]
  291.     @@: add     ecx,3
  292.         shr     ecx,2
  293.         cld
  294.         rep     movsd
  295.         xchg    eax,[esp]
  296.         call    mem.Free
  297.   .exit:
  298.         pop     eax edi esi ecx ebx
  299.         ret
  300. endp
  301.  
  302. ;-----------------------------------------------------------------------------
  303. proc mem.Free mptr ;//////////////////////////////////////////////////////////
  304. ;-----------------------------------------------------------------------------
  305. ;       mov     eax,[mptr]
  306. ;       or      eax,eax
  307. ;       jz      @f
  308. ;       push    ebx ecx
  309. ;       lea     ecx,[eax-4]
  310. ;       stdcall kernel_free, ecx
  311. ;       pop     ecx ebx
  312. ;    @@: ret
  313.         stdcall kernel_free, [mptr]
  314.         ret
  315. endp
  316.  
  317. uglobal
  318. s_libname  db 64 dup (0)
  319. endg
  320. end if
  321.