Subversion Repositories Kolibri OS

Rev

Rev 8724 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;
  2. ;  Support for Dll auto load & linking
  3. ;
  4. ;  (C) 2020-2022 Coldy 
  5. ;  Thank's you for use this code and software based on it!
  6. ;  I will glad if it's will be helpful.
  7. ;
  8. ;  Distributed under terms of GPL
  9. ;
  10.  
  11. format MS COFF
  12. public @EXPORT as 'EXPORTS'
  13. section '.flat' code readable align 16
  14.  
  15. include 'external.inc'
  16. include 'dll.inc'
  17.  
  18. ; This need for @notyfy pre/postformat
  19. STR_BUILD_OFFSET  = 1
  20. STR_BUILD_EXTRA   = 5
  21.  
  22. mem_alloc          = mem.Alloc
  23.  
  24. include 'strhlp.inc'
  25.  
  26. app_version       equ word[8]
  27. i_table_min_size  =   1
  28.  
  29. sizeof.kx_header  =   8
  30.  
  31. ERROR_BAD_IMAGE   =   0x010
  32.  
  33. APP_STARTUP_THUNK:
  34.     ; First make shure that app
  35.     ; have header version 2.0 or more
  36.     cmp app_version,2
  37.     jl .denied            ; App with app_version < 2 shouldn't be here
  38.    
  39.     ; Then make shure that we first
  40.     mov eax, @EXPORT
  41.     cmp dword[eax-4],0
  42.     je .denied
  43.    
  44.     ; Don't allow second time
  45.     mov dword[eax-4],0  
  46.    
  47.    ; Early app initialization
  48.  
  49. ;{ Test KX header
  50.    ;xor  eax, eax
  51.    mov  esi,0x24
  52.    lodsw
  53.    cmp ax, 'KX'
  54.    jne .image_error ; Not KX
  55.    lodsw
  56.    cmp ax, 0
  57.    jne .image_error ; Bad magic
  58.    lodsw
  59.    
  60.    bt ax, 6 ; Have import?
  61.    jnc .app_start
  62. ;}
  63.    
  64.    ; Test import table (use legacy style)  
  65.    mov  eax, [sizeof.kx_header + 0x24] ; i_table_ptr
  66.    test eax, eax
  67.    jz  .image_error;.import_error;.app_start           ; i_table_ptr = 0 ? => Bad image
  68.    ;js      .error
  69.    mov esi, [0x10]
  70.    cmp esi, eax
  71.    jbe .image_error;@f           ; i_table_ptr >= img_size ?
  72.    mov ebx, eax
  73.    add ebx, i_table_min_size
  74.    cmp esi, ebx
  75.    jb .image_error;@f           ; i_table_ptr + i_table_min_size > img_size ?
  76.    
  77.    ; Link app/dependent libs import tables with libs export table
  78.    ; TODO: need revision of the exists lib format and dll.Load (for libs import binds)
  79.            
  80.    stdcall dll.Load,eax
  81.    test eax, eax
  82.    jnz  .link_error;.import_error
  83. .app_start:  
  84.    ; Start of app code
  85.    mov  eax, [0x0C]
  86.    ; TODO: test start_ptr + min_code_size < img_size    
  87.    call eax
  88. @@:
  89.    mov eax, -1
  90.    int 0x40
  91. .image_error:
  92.    mov eax, ERROR_BAD_IMAGE
  93. .link_error:
  94.   ; Run @NOTIFY and tell user then error occurred
  95.   call show_error
  96.   jmp @b
  97. .denied:
  98.     ; Kolibri has no ability kill app if this enter from no from main thread
  99.     ; So just alert and return
  100.     ;DEBUGF     1, 'APP_STARTUP_THUNK@dll.obj: App twice/with app_version < 2 has entered!\n'
  101.     ret  
  102. ; } APP_STARTUP_THUNK
  103.  
  104.  
  105. ; eax = error code  (see ERROR_xxx above in this and dll.inc files)
  106.  
  107. show_error:  
  108.  
  109.   ; Store error code
  110.   mov    edx, eax
  111.  
  112.    ; Get app name
  113.    sub  esp,1024
  114.    mov  eax, 9
  115.    mov  ebx, esp
  116.    mov  ecx, -1
  117.    int    0x40
  118.    
  119.    ;
  120.    
  121.    mov esi, esp
  122.    add esi, 10       ; esi = app name
  123.  
  124.    
  125.    cmp    edx, ERROR_ENTRY_NOT_FOUND
  126.    je     .entry_not_found
  127.    
  128.    ; Init heap not needed
  129.    ; (kernel already initialized heap implicitly when load dll.obj)
  130.    
  131.    cmp    edx, ERROR_LIBRARY_NOT_LOAD
  132.    je     .library_not_loaded  
  133.    
  134.    ccall  str_build, szWrongFormat, esi, szBanner
  135.    jmp @f
  136.  
  137. .library_not_loaded:
  138.   ccall  str_build, szLibraryNotLoaded, esi, szBanner, s_libdir.fname
  139.   jmp @f
  140.  
  141. .entry_not_found:
  142.   mov eax, [szEntryName]
  143.   ccall  str_build, szEntryNotFound, esi, szBanner, eax, s_libdir.fname
  144.    
  145. @@:
  146.   add esp, 1024
  147.  
  148.    mov    byte[eax],'"'
  149.    mov    byte[edi],'"'
  150.    mov    dword[edi+1],"-tdE" ; with title, disable autoclose, error icon
  151.    
  152.    mov    esi, eax    
  153.    
  154.    ; Display error
  155.     mov   [pNotify.params], eax  
  156.     mov   ebx, pNotify
  157.     mov   eax, 70
  158.     int   0x40
  159.    
  160.     stdcall mem.Free, esi
  161.    
  162. .exit:    
  163.     ret
  164.  
  165.  
  166. align 4
  167. ;dd 0xdeadbeef
  168. dd APP_STARTUP_THUNK
  169. @EXPORT:
  170. export                              \
  171.     dll.Load,           'dll_load',  \        
  172.     dll.Link,           'dll_link',  \        
  173.     dll.GetProcAddress, 'dll_sym'    
  174.                          
  175.                          
  176. pNotify:
  177.           dd        7, 0
  178.   .params dd      0      
  179.           dd      0, 0
  180.           db        "/sys/@notify", 0
  181.          
  182. ; { Strings
  183. if defined LANG_RUS
  184. include 'strings.rus'
  185. ;elseif defined LANG_xxx
  186. ; TODO:   Add another supported languges here
  187. ;       - Copy 'elseif defined LANG_xxx', change xxx here and below to the
  188. ;         corresponded constat of you languge
  189. ;       - Create strings.xxx in the root of this project and do translate,
  190. ;         follow the format message below.
  191. ;       - add include 'strings.xxx'  
  192. else ; Default languge (English)
  193. szBanner                db "Error!\n",0
  194. szWrongFormat           db "$ - $Application has wrong format!",0
  195. szLibraryNotLoaded      db "$ - $Can't load library $", 0
  196. szEntryNotFound         db "$ - $Entry $\nin library $\nnot found!",0
  197.  
  198. end if
  199. ; } Strings