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] mov ebp, [ebp+APPDATA.exec_params]
test ebp, ebp test ebp, ebp
jz .exit 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],\ stdcall map_process_image, [ebp+APP_HDR._emem],\
[ebp+APP_HDR.img_base], [ebp+APP_HDR.img_size] [ebp+APP_HDR.img_base], [ebp+APP_HDR.img_size]
mov esi, [ebp+APP_HDR.path_string] mov esi, [ebp+APP_HDR.path_string]
@ -893,53 +895,34 @@ common_app_entry:
mov dx, app_tls mov dx, app_tls
mov fs, dx mov fs, dx
; { Patch by Coldy, For DLL autoload ; { Patch by Coldy, For DLL autoload
; if APP_HEADER.version = 2 => lib/load dll.obj & change eip to APP_START_THUNK)
.try_load_dll: .try_load_dll:
; TODO: It;s app, not thread? ; Test app header version
; Test app header version (
mov ecx, dword[ebp+APP_HDR.img_base] mov ecx, dword[ebp+APP_HDR.img_base]
;DEBUGF 1, "K : ecx = %d\n", [ecx+8]
cmp dword[ecx+8], 2 cmp dword[ecx+8], 2
jne .cleanup 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 : App header version 2\n'
;DEBUGF 1, "K : DLL.OBJ path: %s\n", dll_lib_path
stdcall load_library, dll_lib_path, 0 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 cmp eax, 0
jne @f 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 free_kernel_space, [ebp+APP_HDR.img_base]
stdcall kernel_free, ebp 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 ebx, dll_error_msg
mov ebp, notifyapp mov ebp, notifyapp
call fs_execute_from_sysdir_param call fs_execute_from_sysdir_param
; Terminate process ; Terminate process (TODO: Need jump to .cleanup after sys_end ?)
call sys_end call sys_end
@@: @@:
; Find base of DLL.OBJ ; Find APP_STARTUP_THUNK in DLL.OBJ
;mov ebx, eax
;cdq
;mov ecx, 0x00000400
;div ecx
;sub ebx, edx
sub eax, 4 sub eax, 4
mov eax, [eax] 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
;.change_eip: ;.change_eip:
mov ecx, [current_slot] mov ecx, [current_slot]
mov ecx, [ecx+APPDATA.pl0_stack] mov ecx, [ecx+APPDATA.pl0_stack]
;DEBUGF 1, "K : EIP = %x\n", ebx
mov [ecx+REG_EIP], eax mov [ecx+REG_EIP], eax
; } End patch by Coldy, For DLL autoload ; } 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 format MS COFF
public @EXPORT as 'EXPORTS' public @EXPORT as 'EXPORTS'
include '../../../proc32.inc' include '../../proc32.inc'
include '../../../macros.inc' include '../../macros.inc'
section '.flat' code readable align 16 section '.flat' code readable align 16
app_version equ word[8] app_version equ word[8]
i_table_min_size = 1 i_table_min_size = 1
APP_START_THUNK: sizeof.kx_header = 8
APP_STARTUP_THUNK:
; First make shure that app ; First make shure that app
; have header version 2.0 or more ; have header version 2.0 or more
cmp app_version,2 cmp app_version,2
jl .denied jl .denied ; App with app_version < 2 shouldn't be here
; Then make shure that we first ; Then make shure that we first
mov eax, @EXPORT mov eax, @EXPORT
@ -34,41 +36,67 @@ APP_START_THUNK:
; Early app initialization ; Early app initialization
; Test import table ;{ Test KX header
mov eax, [0x24] ; i_table_ptr ;xor eax, eax
mov esi,0x24
lodsw
cmp ax, 'KX'
jne @f ; Not KX
lodsw
cmp ax, 0
jne @f ; Bad magic
lodsw
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 test eax, eax
jz @f jz .app_start ; i_table_ptr = 0 ?
;js .error
mov esi, [0x10] mov esi, [0x10]
cmp esi, eax cmp esi, eax
jbe @f ; i_table_ptr >= img_size ? jbe @f ; i_table_ptr >= img_size ?
mov ebx, eax mov ebx, eax
add ebx, i_table_min_size add ebx, i_table_min_size
cmp esi, ebx 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/dependent libs import tables with libs export table
; TODO: need revision of the exists lib format and dll.Load (for libs import binds)
; Link app import table with DLL's exoport table
stdcall dll.Load,eax stdcall dll.Load,eax
test eax, eax test eax, eax
jnz @f jnz .import_error
.app_start:
; Start of app code ; Start of app code
mov eax, [0x0C] mov eax, [0x0C]
; TODO: test start_ptr + min_code_size < img_size ; TODO: test start_ptr + min_code_size < img_size
call eax call eax
@@: @@:
mov eax, -1 mov eax, -1
int 0x40 int 0x40
.import_error:
; Run @NOTIFY and tell user then error occured
; BOARD will contaits details
jmp @b
.denied: .denied:
ret ; Kolibri has no ability kill app if this enter from no from main thread
; } APP_START_THUNK ; 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! ; WARNING! This code must be after app initialization thunk!
include '../../../dll.inc' include '../../dll.inc'
align 4 align 4
;dd 0xdeadbeef ;dd 0xdeadbeef
dd APP_START_THUNK dd APP_STARTUP_THUNK
@EXPORT: @EXPORT:
export \ export \
dll.Load, 'dll_load', \ dll.Load, 'dll_load', \
dll.Link, 'dll_link', \ dll.Link, 'dll_link', \
dll.GetProcAddress, 'dll_sym' ; dll.GetProcAddress, 'dll_sym' ;