2021-04-17 11:40:01 +02:00
|
|
|
;
|
2022-01-25 19:04:10 +01:00
|
|
|
; Support for Dll auto load & linking
|
2021-04-17 11:40:01 +02:00
|
|
|
;
|
2022-01-25 19:04:10 +01:00
|
|
|
; (C) 2020-2022 Coldy
|
2021-04-17 11:40:01 +02:00
|
|
|
; Thank's you for use this code and software based on it!
|
|
|
|
; I will glad if it's will be helpful.
|
|
|
|
;
|
|
|
|
; Distributed under terms of GPL
|
|
|
|
;
|
2022-01-25 19:04:10 +01:00
|
|
|
|
2021-04-17 11:40:01 +02:00
|
|
|
format MS COFF
|
|
|
|
public @EXPORT as 'EXPORTS'
|
2022-01-25 19:04:10 +01:00
|
|
|
section '.flat' code readable align 16
|
2021-04-17 11:40:01 +02:00
|
|
|
|
2022-01-25 19:04:10 +01:00
|
|
|
include 'external.inc'
|
|
|
|
include 'dll.inc'
|
2021-04-17 11:40:01 +02:00
|
|
|
|
2022-01-25 19:04:10 +01:00
|
|
|
; This need for @notyfy pre/postformat
|
|
|
|
STR_BUILD_OFFSET = 1
|
|
|
|
STR_BUILD_EXTRA = 5
|
|
|
|
|
|
|
|
mem_alloc = mem.Alloc
|
|
|
|
|
|
|
|
include 'strhlp.inc'
|
2021-04-17 11:40:01 +02:00
|
|
|
|
2021-05-01 18:17:27 +02:00
|
|
|
app_version equ word[8]
|
2021-04-17 11:40:01 +02:00
|
|
|
i_table_min_size = 1
|
|
|
|
|
2021-05-01 18:17:27 +02:00
|
|
|
sizeof.kx_header = 8
|
|
|
|
|
2022-01-25 19:04:10 +01:00
|
|
|
ERROR_BAD_IMAGE = 0x010
|
|
|
|
|
2021-05-01 18:17:27 +02:00
|
|
|
APP_STARTUP_THUNK:
|
2021-04-17 11:40:01 +02:00
|
|
|
; First make shure that app
|
|
|
|
; have header version 2.0 or more
|
|
|
|
cmp app_version,2
|
2021-05-01 18:17:27 +02:00
|
|
|
jl .denied ; App with app_version < 2 shouldn't be here
|
2021-04-17 11:40:01 +02:00
|
|
|
|
|
|
|
; Then make shure that we first
|
|
|
|
mov eax, @EXPORT
|
|
|
|
cmp dword[eax-4],0
|
|
|
|
je .denied
|
|
|
|
|
|
|
|
; Don't allow second time
|
2021-05-01 18:17:27 +02:00
|
|
|
mov dword[eax-4],0
|
2021-04-17 11:40:01 +02:00
|
|
|
|
|
|
|
; Early app initialization
|
2021-05-01 18:17:27 +02:00
|
|
|
|
|
|
|
;{ Test KX header
|
|
|
|
;xor eax, eax
|
|
|
|
mov esi,0x24
|
|
|
|
lodsw
|
|
|
|
cmp ax, 'KX'
|
2022-01-25 19:04:10 +01:00
|
|
|
jne .image_error ; Not KX
|
2021-05-01 18:17:27 +02:00
|
|
|
lodsw
|
|
|
|
cmp ax, 0
|
2022-01-25 19:04:10 +01:00
|
|
|
jne .image_error ; Bad magic
|
2021-05-01 18:17:27 +02:00
|
|
|
lodsw
|
2021-04-17 11:40:01 +02:00
|
|
|
|
2021-05-01 18:17:27 +02:00
|
|
|
bt ax, 6 ; Have import?
|
|
|
|
jnc .app_start
|
|
|
|
;}
|
|
|
|
|
|
|
|
; Test import table (use legacy style)
|
|
|
|
mov eax, [sizeof.kx_header + 0x24] ; i_table_ptr
|
2021-04-17 11:40:01 +02:00
|
|
|
test eax, eax
|
2022-01-25 19:04:10 +01:00
|
|
|
jz .image_error;.import_error;.app_start ; i_table_ptr = 0 ? => Bad image
|
2021-05-01 18:17:27 +02:00
|
|
|
;js .error
|
2021-04-17 11:40:01 +02:00
|
|
|
mov esi, [0x10]
|
|
|
|
cmp esi, eax
|
2022-01-25 19:04:10 +01:00
|
|
|
jbe .image_error;@f ; i_table_ptr >= img_size ?
|
2021-04-17 11:40:01 +02:00
|
|
|
mov ebx, eax
|
|
|
|
add ebx, i_table_min_size
|
|
|
|
cmp esi, ebx
|
2022-01-25 19:04:10 +01:00
|
|
|
jb .image_error;@f ; i_table_ptr + i_table_min_size > img_size ?
|
2021-04-17 11:40:01 +02:00
|
|
|
|
2021-05-01 18:17:27 +02:00
|
|
|
; Link app/dependent libs import tables with libs export table
|
|
|
|
; TODO: need revision of the exists lib format and dll.Load (for libs import binds)
|
|
|
|
|
2021-04-17 11:40:01 +02:00
|
|
|
stdcall dll.Load,eax
|
2021-05-01 18:17:27 +02:00
|
|
|
test eax, eax
|
2022-01-25 19:04:10 +01:00
|
|
|
jnz .link_error;.import_error
|
2021-05-01 18:17:27 +02:00
|
|
|
.app_start:
|
2021-04-17 11:40:01 +02:00
|
|
|
; Start of app code
|
2021-05-01 18:17:27 +02:00
|
|
|
mov eax, [0x0C]
|
|
|
|
; TODO: test start_ptr + min_code_size < img_size
|
2021-04-17 11:40:01 +02:00
|
|
|
call eax
|
|
|
|
@@:
|
|
|
|
mov eax, -1
|
|
|
|
int 0x40
|
2022-01-25 19:04:10 +01:00
|
|
|
.image_error:
|
|
|
|
mov eax, ERROR_BAD_IMAGE
|
|
|
|
.link_error:
|
|
|
|
; Run @NOTIFY and tell user then error occurred
|
|
|
|
call show_error
|
2021-05-01 18:17:27 +02:00
|
|
|
jmp @b
|
2021-04-17 11:40:01 +02:00
|
|
|
.denied:
|
2021-05-01 18:17:27 +02:00
|
|
|
; Kolibri has no ability kill app if this enter from no from main thread
|
|
|
|
; So just alert and return
|
|
|
|
;DEBUGF 1, 'APP_STARTUP_THUNK@dll.obj: App twice/with app_version < 2 has entered!\n'
|
|
|
|
ret
|
|
|
|
; } APP_STARTUP_THUNK
|
2021-04-17 11:40:01 +02:00
|
|
|
|
|
|
|
|
2022-01-25 19:04:10 +01:00
|
|
|
; eax = error code (see ERROR_xxx above in this and dll.inc files)
|
|
|
|
|
|
|
|
show_error:
|
|
|
|
|
|
|
|
; Store error code
|
|
|
|
mov edx, eax
|
|
|
|
|
|
|
|
; Get app name
|
|
|
|
sub esp,1024
|
|
|
|
mov eax, 9
|
|
|
|
mov ebx, esp
|
|
|
|
mov ecx, -1
|
|
|
|
int 0x40
|
|
|
|
|
|
|
|
;
|
|
|
|
|
|
|
|
mov esi, esp
|
|
|
|
add esi, 10 ; esi = app name
|
|
|
|
|
|
|
|
|
|
|
|
cmp edx, ERROR_ENTRY_NOT_FOUND
|
|
|
|
je .entry_not_found
|
|
|
|
|
|
|
|
; Init heap not needed
|
|
|
|
; (kernel already initialized heap implicitly when load dll.obj)
|
|
|
|
|
|
|
|
cmp edx, ERROR_LIBRARY_NOT_LOAD
|
|
|
|
je .library_not_loaded
|
|
|
|
|
|
|
|
ccall str_build, szWrongFormat, esi, szBanner
|
|
|
|
jmp @f
|
|
|
|
|
|
|
|
.library_not_loaded:
|
|
|
|
ccall str_build, szLibraryNotLoaded, esi, szBanner, s_libdir.fname
|
|
|
|
jmp @f
|
|
|
|
|
|
|
|
.entry_not_found:
|
|
|
|
mov eax, [szEntryName]
|
|
|
|
ccall str_build, szEntryNotFound, esi, szBanner, eax, s_libdir.fname
|
|
|
|
|
|
|
|
@@:
|
|
|
|
add esp, 1024
|
|
|
|
|
|
|
|
mov byte[eax],'"'
|
|
|
|
mov byte[edi],'"'
|
|
|
|
mov dword[edi+1],"-tdE" ; with title, disable autoclose, error icon
|
|
|
|
|
|
|
|
mov esi, eax
|
|
|
|
|
|
|
|
; Display error
|
|
|
|
mov [pNotify.params], eax
|
|
|
|
mov ebx, pNotify
|
|
|
|
mov eax, 70
|
|
|
|
int 0x40
|
|
|
|
|
|
|
|
stdcall mem.Free, esi
|
|
|
|
|
|
|
|
.exit:
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2021-04-17 11:40:01 +02:00
|
|
|
align 4
|
|
|
|
;dd 0xdeadbeef
|
2021-05-01 18:17:27 +02:00
|
|
|
dd APP_STARTUP_THUNK
|
2021-04-17 11:40:01 +02:00
|
|
|
@EXPORT:
|
2021-05-01 18:17:27 +02:00
|
|
|
export \
|
2022-01-25 19:04:10 +01:00
|
|
|
dll.Load, 'dll_load', \
|
|
|
|
dll.Link, 'dll_link', \
|
|
|
|
dll.GetProcAddress, 'dll_sym'
|
|
|
|
|
|
|
|
|
|
|
|
pNotify:
|
|
|
|
dd 7, 0
|
|
|
|
.params dd 0
|
|
|
|
dd 0, 0
|
|
|
|
db "/sys/@notify", 0
|
|
|
|
|
|
|
|
; { Strings
|
|
|
|
if defined LANG_RUS
|
|
|
|
include 'strings.rus'
|
|
|
|
;elseif defined LANG_xxx
|
|
|
|
; TODO: Add another supported languges here
|
|
|
|
; - Copy 'elseif defined LANG_xxx', change xxx here and below to the
|
|
|
|
; corresponded constat of you languge
|
|
|
|
; - Create strings.xxx in the root of this project and do translate,
|
|
|
|
; follow the format message below.
|
|
|
|
; - add include 'strings.xxx'
|
|
|
|
else ; Default languge (English)
|
|
|
|
szBanner db "Error!\n",0
|
|
|
|
szWrongFormat db "$ - $Application has wrong format!",0
|
|
|
|
szLibraryNotLoaded db "$ - $Can't load library $", 0
|
|
|
|
szEntryNotFound db "$ - $Entry $\nin library $\nnot found!",0
|
|
|
|
|
|
|
|
end if
|
|
|
|
; } Strings
|