Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2011. 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: 2465 $
  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:
  162.         mov     edx, [esi]
  163.         or      edx, edx
  164.         jz      .exit
  165.         push    esi
  166.  
  167.         mov     edi, s_libname
  168.  
  169.         mov     al, '/'
  170.         stosb
  171.         mov     esi, sysdir_path
  172.             @@:
  173.         lodsb
  174.         stosb
  175.         or      al, al
  176.         jnz     @b
  177.         dec     edi
  178.         mov     [edi], dword '/lib'
  179.         mov     [edi+4], byte '/'
  180.         add     edi, 5
  181.         pop     esi
  182.         push    esi
  183.         mov     esi, [esi+4]
  184.             @@:
  185.         lodsb
  186.         stosb
  187.         or      al, al
  188.         jnz     @b
  189.  
  190.         pushad
  191.         stdcall load_k_library, s_libname
  192.         mov     [esp+28], eax
  193.         popad
  194.         or      eax, eax
  195.         jz      .fail
  196.         stdcall dll.Link, eax, edx
  197.         stdcall dll.Init, [eax+4]
  198.         pop     esi
  199.         add     esi, 8
  200.         jmp     .next_lib
  201.   .exit:
  202.         xor     eax, eax
  203.         ret
  204.   .fail:
  205.         add     esp, 4
  206.         xor     eax, eax
  207.         inc     eax
  208.         ret
  209. endp
  210.  
  211. proc dll.Link, exp:dword,imp:dword
  212.         push    eax
  213.         mov     esi, [imp]
  214.         test    esi, esi
  215.         jz      .done
  216.   .next:
  217.         lodsd
  218.         test    eax, eax
  219.         jz      .done
  220.         stdcall dll.GetProcAddress, [exp], eax
  221.         or      eax, eax
  222.         jz      @f
  223.         mov     [esi-4], eax
  224.         jmp     .next
  225.             @@:
  226.         mov     dword[esp], 0
  227.   .done:
  228.         pop     eax
  229.         ret
  230. endp
  231.  
  232. proc dll.Init, dllentry:dword
  233.         pushad
  234.         mov     eax, mem.Alloc
  235.         mov     ebx, mem.Free
  236.         mov     ecx, mem.ReAlloc
  237.         mov     edx, dll.Load
  238.         stdcall [dllentry]
  239.         popad
  240.         ret
  241. endp
  242.  
  243. proc dll.GetProcAddress, exp:dword,sz_name:dword
  244.         mov     edx, [exp]
  245.   .next:
  246.         test    edx, edx
  247.         jz      .end
  248.         stdcall strncmp, [edx], [sz_name], dword -1
  249.         test    eax, eax
  250.         jz      .ok
  251.         add     edx, 8
  252.         jmp     .next
  253.   .ok:
  254.         mov     eax, [edx+4]
  255.   .end:
  256.         ret
  257. endp
  258.  
  259. ;-----------------------------------------------------------------------------
  260. proc mem.Alloc size ;/////////////////////////////////////////////////////////
  261. ;-----------------------------------------------------------------------------
  262.         push    ebx ecx
  263. ;       mov     eax,[size]
  264. ;       lea     ecx,[eax+4+4095]
  265. ;       and     ecx,not 4095
  266. ;       stdcall kernel_alloc, ecx
  267. ;       add     ecx,-4
  268. ;       mov     [eax],ecx
  269. ;       add     eax,4
  270.  
  271.         stdcall kernel_alloc, [size]
  272.  
  273.         pop     ecx ebx
  274.         ret
  275. endp
  276.  
  277. ;-----------------------------------------------------------------------------
  278. proc mem.ReAlloc mptr,size;///////////////////////////////////////////////////
  279. ;-----------------------------------------------------------------------------
  280.         push    ebx ecx esi edi eax
  281.         mov     eax, [mptr]
  282.         mov     ebx, [size]
  283.         or      eax, eax
  284.         jz      @f
  285.         lea     ecx, [ebx+4+4095]
  286.         and     ecx, not 4095
  287.         add     ecx, -4
  288.         cmp     ecx, [eax-4]
  289.         je      .exit
  290.     @@:
  291.         mov     eax, ebx
  292.         call    mem.Alloc
  293.         xchg    eax, [esp]
  294.         or      eax, eax
  295.         jz      .exit
  296.         mov     esi, eax
  297.         xchg    eax, [esp]
  298.         mov     edi, eax
  299.         mov     ecx, [esi-4]
  300.         cmp     ecx, [edi-4]
  301.         jbe     @f
  302.         mov     ecx, [edi-4]
  303.     @@:
  304.         add     ecx, 3
  305.         shr     ecx, 2
  306.         cld
  307.         rep movsd
  308.         xchg    eax, [esp]
  309.         call    mem.Free
  310.   .exit:
  311.         pop     eax edi esi ecx ebx
  312.         ret
  313. endp
  314.  
  315. ;-----------------------------------------------------------------------------
  316. proc mem.Free mptr ;//////////////////////////////////////////////////////////
  317. ;-----------------------------------------------------------------------------
  318. ;       mov     eax,[mptr]
  319. ;       or      eax,eax
  320. ;       jz      @f
  321. ;       push    ebx ecx
  322. ;       lea     ecx,[eax-4]
  323. ;       stdcall kernel_free, ecx
  324. ;       pop     ecx ebx
  325. ;    @@: ret
  326.         stdcall kernel_free, [mptr]
  327.         ret
  328. endp
  329.  
  330. uglobal
  331. s_libname  db 64 dup (0)
  332. endg
  333. end if
  334.