Dll autoload

Kernel
- removed unnecessary and rewrote/add some comments, no code changes
User (lib/dll.obj)   
- Introduced new KX header as extension for existing formats
- Added staring KX header processing
- Improved import table test logic, no reason to kill app for import absence - skip 
  import processing (tnx ProMiNick)
- Add ReadMe.txt


git-svn-id: svn://kolibrios.org@8709 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Coldy 2021-05-01 16:17:27 +00:00
parent 43506beeb7
commit ed25f2c1ca
3 changed files with 167 additions and 51 deletions

View File

@ -834,6 +834,8 @@ common_app_entry:
mov ebp, [ebp+APPDATA.exec_params]
test ebp, ebp
jz .exit
; APPDATA.exec_params have first thread only,
; so second and next threads don't get here (they jump to .exit)
stdcall map_process_image, [ebp+APP_HDR._emem],\
[ebp+APP_HDR.img_base], [ebp+APP_HDR.img_size]
mov esi, [ebp+APP_HDR.path_string]
@ -892,54 +894,35 @@ common_app_entry:
mov [tls_data_l+7], ah
mov dx, app_tls
mov fs, dx
; { Patch by Coldy, For DLL autoload
; if APP_HEADER.version = 2 => lib/load dll.obj & change eip to APP_START_THUNK)
.try_load_dll:
; TODO: It;s app, not thread?
; Test app header version (
; { Patch by Coldy, For DLL autoload
.try_load_dll:
; Test app header version
mov ecx, dword[ebp+APP_HDR.img_base]
;DEBUGF 1, "K : ecx = %d\n", [ecx+8]
cmp dword[ecx+8], 2
jne .cleanup
;if APP_HEADER.version = 2 => load lib/dll.obj & change eip to APP_STARTUP_THUNK
DEBUGF 1, 'K : App header version 2\n'
;DEBUGF 1, "K : DLL.OBJ path: %s\n", dll_lib_path
stdcall load_library, dll_lib_path, 0
;DEBUGF 1, "K : DLL.OBJ exp ptr: %x\n", eax
;test eax, eax
;jnz @f
cmp eax, 0
jne @f
; Something went wrong
; Something went wrong (TODO: Next 2 line is code copy after .cleanup)
stdcall free_kernel_space, [ebp+APP_HDR.img_base]
stdcall kernel_free, ebp
DEBUGF 1, 'K : DLL.OBJ not found! Terminate application!'
DEBUGF 1, 'K : DLL.OBJ not found! Terminate application!\n'
mov ebx, dll_error_msg
mov ebp, notifyapp
call fs_execute_from_sysdir_param
; Terminate process
; Terminate process (TODO: Need jump to .cleanup after sys_end ?)
call sys_end
@@:
; Find base of DLL.OBJ
;mov ebx, eax
;cdq
;mov ecx, 0x00000400
;div ecx
;sub ebx, edx
; Find APP_STARTUP_THUNK in DLL.OBJ
sub eax, 4
mov eax, [eax]
;DEBUGF 1, "K : DLL.OBJ base ptr: %x\n", eax
; load_library don't map coff header,
; so we may change entry point for app APP_START_THUNK
; to base of DLL.OBJ
mov eax, [eax]
;.change_eip:
mov ecx, [current_slot]
mov ecx, [ecx+APPDATA.pl0_stack]
;DEBUGF 1, "K : EIP = %x\n", ebx
mov [ecx+REG_EIP], eax
; } End patch by Coldy, For DLL autoload

View File

@ -0,0 +1,105 @@
---
History
0.1 + First realised, kernel load dll.obj at runtime as starting point berfore app startup
dll.obj process app import table, but not depended librarys, after that app gots control in his starting point
0.2 + Introduced new KX header as extension for current format (see decription below)
+ Add KX header processing
+ Improved import table test logic, no reason to kill app for import absence - skip import processing (tnx ProMiNick)
---
Purpose
Automatically libraries loads and linking imports.
---
Limitations
1) No error messages are issued if the library or symbol in the library is not found or somthing went wrong
2) There is no autoloading of dependent libraries (the library format needs to be improved, see intorduction of KX header extension bellow)
---
How to use
- in app:
1) In the version field of a header, (after MENUET0x) you must specify the number 2
2) After existing header add KX header extension as descriprion bellow
3) Specify imported libraries. Currentry format of import table same as in case of using dll.Load
4) Add code, without connecting dll.inc and, accordingly, without calling dll.Load. The heap initialization function (f. 68.11) does not need to be called either.
5) Compile the app and run. If everything is done correctly, then on startup the debug board will display the message "App header version 2"
If the DLL.OBJ library is missing, a message will be displayed, incl. via @NOTIFY. If you get a page error make sure you have completed steps 2 and 3
- in lib (obj):
Not supported yet, will be realized later.
1) Field optHeader of COFF header need set in 8+n*4, where n is count of fields after KX header
2) After COFF header add KX header extension (in general same as in case for app)
---
Descriprion of KX header extension (alpha).
TBD is meaning that this feature to be determined leter, and not supported yet.
By default all offsets and sizes given in bytes, for Flags field offsets and size given in bits.
Offset 4.x meaning offset 4 bit x
Fields between offset 8 and at end of KX header may be added later.
Offset Size Field Meaning
Signature:
0 2 SigMagic Module identifier with the value "KX"
2 1 SigRevision This field should be 0.
In the future, it can take on the revision value
(but can't take values higher than 64)
3 1 SigArch This field should be 0.
4 2 Flags:
4.0 2b F_ImageType TBD, this field should be 0
4.2 1b F_SectionMode TBD, this field should be 0
4.3 1b F_Const TBD, this field should be 0
4.4 1b F_Data TBD, this field should be 0
4.5 1b F_Export Module has export table, and pointer after header (see below)
4.6 1b F_Import Module has import table
4.7 1b F_Reserved Reserved, this field should be 0
4.8 1b F_BoundImport TBD, this field should be 0
4.9 1b F_BSS TBD, this field should be 0
4.10 1b F_TLS TBD, this field should be 0
1.11 5b F_Reserved Reserved, this field should be 0
6 2 Reserved Reserved, this field should be 0
... TBD Fields in this place may be added later
if(F_Export) {
? 4 ExportsHeader Pointer to export header (exists if F_Export=1), KX style export table not supported yet. Currently by backward compatibility reason used legacy style of export table, this field pointed to it
}
if(F_Import) {
? 4 ImportsHeader Pointer to imports header (exists if F_Import=1), KX style import table not supported yet. Currently by backward compatibility reason used legacy style of import table, this field pointed to it
}
... TBD Fields in this place may be added later
---
EOF

View File

@ -10,19 +10,21 @@
format MS COFF
public @EXPORT as 'EXPORTS'
include '../../../proc32.inc'
include '../../../macros.inc'
include '../../proc32.inc'
include '../../macros.inc'
section '.flat' code readable align 16
app_version equ word[8]
app_version equ word[8]
i_table_min_size = 1
APP_START_THUNK:
sizeof.kx_header = 8
APP_STARTUP_THUNK:
; First make shure that app
; have header version 2.0 or more
cmp app_version,2
jl .denied
jl .denied ; App with app_version < 2 shouldn't be here
; Then make shure that we first
mov eax, @EXPORT
@ -30,45 +32,71 @@ APP_START_THUNK:
je .denied
; Don't allow second time
mov dword[eax-4],0
mov dword[eax-4],0
; Early app initialization
;{ Test KX header
;xor eax, eax
mov esi,0x24
lodsw
cmp ax, 'KX'
jne @f ; Not KX
lodsw
cmp ax, 0
jne @f ; Bad magic
lodsw
; Test import table
mov eax, [0x24] ; i_table_ptr
bt ax, 6 ; Have import?
jnc .app_start
;}
; Test import table (use legacy style)
mov eax, [sizeof.kx_header + 0x24] ; i_table_ptr
test eax, eax
jz @f
jz .app_start ; i_table_ptr = 0 ?
;js .error
mov esi, [0x10]
cmp esi, eax
jbe @f ; i_table_ptr >= img_size ?
jbe @f ; i_table_ptr >= img_size ?
mov ebx, eax
add ebx, i_table_min_size
cmp esi, ebx
jbe @f ; i_table_ptr + i_table_min_size >= img_size ?
jb @f ; i_table_ptr + i_table_min_size > img_size ?
; Link app import table with DLL's exoport table
; 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)
stdcall dll.Load,eax
test eax, eax
jnz @f
test eax, eax
jnz .import_error
.app_start:
; Start of app code
mov eax, [0x0C]
; TODO: test start_ptr + min_code_size < img_size
mov eax, [0x0C]
; TODO: test start_ptr + min_code_size < img_size
call eax
@@:
mov eax, -1
int 0x40
.import_error:
; Run @NOTIFY and tell user then error occured
; BOARD will contaits details
jmp @b
.denied:
ret
; } APP_START_THUNK
; 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
; WARNING! This code must be after app initialization thunk!
include '../../../dll.inc'
include '../../dll.inc'
align 4
;dd 0xdeadbeef
dd APP_START_THUNK
dd APP_STARTUP_THUNK
@EXPORT:
export \
dll.Load, 'dll_load', \
dll.Link, 'dll_link', \
export \
dll.Load, 'dll_load', \
dll.Link, 'dll_link', \
dll.GetProcAddress, 'dll_sym' ;