Rev 8724 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 8724 | Rev 9669 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | ; |
1 | ; |
2 | ; KolibriOS Dll load support |
2 | ; Support for Dll auto load & linking |
3 | ; |
3 | ; |
4 | ; (C) 2020-2021 Coldy |
4 | ; (C) 2020-2022 Coldy |
5 | ; Thank's you for use this code and software based on it! |
5 | ; Thank's you for use this code and software based on it! |
6 | ; I will glad if it's will be helpful. |
6 | ; I will glad if it's will be helpful. |
7 | ; |
7 | ; |
8 | ; Distributed under terms of GPL |
8 | ; Distributed under terms of GPL |
9 | ; |
9 | ; |
- | 10 | ||
10 | format MS COFF |
11 | format MS COFF |
11 | public @EXPORT as 'EXPORTS' |
12 | public @EXPORT as 'EXPORTS' |
- | 13 | section '.flat' code readable align 16 |
|
Line 12... | Line 14... | ||
12 | 14 | ||
13 | include '../../../proc32.inc' |
15 | include 'external.inc' |
Line -... | Line 16... | ||
- | 16 | include 'dll.inc' |
|
- | 17 | ||
- | 18 | ; This need for @notyfy pre/postformat |
|
- | 19 | STR_BUILD_OFFSET = 1 |
|
14 | include '../../../macros.inc' |
20 | STR_BUILD_EXTRA = 5 |
- | 21 | ||
- | 22 | mem_alloc = mem.Alloc |
|
Line 15... | Line 23... | ||
15 | 23 | ||
16 | section '.flat' code readable align 16 |
24 | include 'strhlp.inc' |
Line 17... | Line 25... | ||
17 | 25 | ||
Line -... | Line 26... | ||
- | 26 | app_version equ word[8] |
|
- | 27 | i_table_min_size = 1 |
|
18 | app_version equ word[8] |
28 | |
19 | i_table_min_size = 1 |
29 | sizeof.kx_header = 8 |
20 | 30 | ||
21 | sizeof.kx_header = 8 |
31 | ERROR_BAD_IMAGE = 0x010 |
22 | 32 | ||
Line 39... | Line 49... | ||
39 | ;{ Test KX header |
49 | ;{ Test KX header |
40 | ;xor eax, eax |
50 | ;xor eax, eax |
41 | mov esi,0x24 |
51 | mov esi,0x24 |
42 | lodsw |
52 | lodsw |
43 | cmp ax, 'KX' |
53 | cmp ax, 'KX' |
44 | jne @f ; Not KX |
54 | jne .image_error ; Not KX |
45 | lodsw |
55 | lodsw |
46 | cmp ax, 0 |
56 | cmp ax, 0 |
47 | jne @f ; Bad magic |
57 | jne .image_error ; Bad magic |
48 | lodsw |
58 | lodsw |
Line 49... | Line 59... | ||
49 | 59 | ||
50 | bt ax, 6 ; Have import? |
60 | bt ax, 6 ; Have import? |
51 | jnc .app_start |
61 | jnc .app_start |
Line 52... | Line 62... | ||
52 | ;} |
62 | ;} |
53 | 63 | ||
54 | ; Test import table (use legacy style) |
64 | ; Test import table (use legacy style) |
55 | mov eax, [sizeof.kx_header + 0x24] ; i_table_ptr |
65 | mov eax, [sizeof.kx_header + 0x24] ; i_table_ptr |
56 | test eax, eax |
66 | test eax, eax |
57 | jz .app_start ; i_table_ptr = 0 ? |
67 | jz .image_error;.import_error;.app_start ; i_table_ptr = 0 ? => Bad image |
58 | ;js .error |
68 | ;js .error |
59 | mov esi, [0x10] |
69 | mov esi, [0x10] |
60 | cmp esi, eax |
70 | cmp esi, eax |
61 | jbe @f ; i_table_ptr >= img_size ? |
71 | jbe .image_error;@f ; i_table_ptr >= img_size ? |
62 | mov ebx, eax |
72 | mov ebx, eax |
63 | add ebx, i_table_min_size |
73 | add ebx, i_table_min_size |
Line 64... | Line 74... | ||
64 | cmp esi, ebx |
74 | cmp esi, ebx |
65 | jb @f ; i_table_ptr + i_table_min_size > img_size ? |
75 | jb .image_error;@f ; i_table_ptr + i_table_min_size > img_size ? |
Line 66... | Line 76... | ||
66 | 76 | ||
67 | ; Link app/dependent libs import tables with libs export table |
77 | ; Link app/dependent libs import tables with libs export table |
68 | ; TODO: need revision of the exists lib format and dll.Load (for libs import binds) |
78 | ; TODO: need revision of the exists lib format and dll.Load (for libs import binds) |
69 | 79 | ||
70 | stdcall dll.Load,eax |
80 | stdcall dll.Load,eax |
71 | test eax, eax |
81 | test eax, eax |
72 | jnz .import_error |
82 | jnz .link_error;.import_error |
73 | .app_start: |
83 | .app_start: |
74 | ; Start of app code |
84 | ; Start of app code |
75 | mov eax, [0x0C] |
85 | mov eax, [0x0C] |
76 | ; TODO: test start_ptr + min_code_size < img_size |
86 | ; TODO: test start_ptr + min_code_size < img_size |
77 | call eax |
87 | call eax |
- | 88 | @@: |
|
- | 89 | mov eax, -1 |
|
78 | @@: |
90 | int 0x40 |
79 | mov eax, -1 |
91 | .image_error: |
80 | int 0x40 |
92 | mov eax, ERROR_BAD_IMAGE |
81 | .import_error: |
93 | .link_error: |
82 | ; Run @NOTIFY and tell user then error occured |
94 | ; Run @NOTIFY and tell user then error occurred |
83 | ; BOARD will contaits details |
95 | call show_error |
84 | jmp @b |
96 | jmp @b |
85 | .denied: |
97 | .denied: |
86 | ; Kolibri has no ability kill app if this enter from no from main thread |
98 | ; Kolibri has no ability kill app if this enter from no from main thread |
Line 87... | Line 99... | ||
87 | ; So just alert and return |
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 | ; |
|
88 | ;DEBUGF 1, 'APP_STARTUP_THUNK@dll.obj: App twice/with app_version < 2 has entered!\n' |
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 | ||
89 | ret |
160 | stdcall mem.Free, esi |
90 | ; } APP_STARTUP_THUNK |
161 | |
91 | 162 | .exit: |
|
92 | 163 | ret |
|
93 | ; WARNING! This code must be after app initialization thunk! |
164 | |
94 | include '../../../dll.inc' |
165 | |
95 | align 4 |
166 | align 4 |
96 | ;dd 0xdeadbeef |
- | |
97 | dd APP_STARTUP_THUNK |
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 |
|
98 | @EXPORT: |
195 | szLibraryNotLoaded db "$ - $Can't load library $", 0 |