Rev 9556 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 9556 | Rev 9558 | ||
---|---|---|---|
Line 1... | Line -... | ||
1 | ; |
- | |
2 | ; 2021, Edited by Coldy |
- | |
3 | ; |
- | |
4 | ; This module same as original crt0.asm, but cut: |
- | |
5 | ; 1. virtual header block (hparams change to __app_params, hpath change to __app_path) |
- | |
6 | ; 2. init heap of memory - not needed because 68.18 (68.19) init heap implicitly |
- | |
7 | ; (it is does dll.obj) |
- | |
8 | ; 3. loader (he lives in dll.obj) |
- | |
9 | ; |
- | |
10 | - | ||
11 | format ELF |
1 | format ELF |
12 | section '.text' executable |
2 | section '.text' executable |
13 | public start |
3 | public start |
14 | public start as '_start' |
4 | public start as '_start' |
15 | 5 | ;extrn mf_init |
|
16 | extrn main |
6 | extrn main |
17 | ;include 'debug2.inc' |
7 | include '../../../../../proc32.inc' |
18 | include '../../../../../../proc32.inc' |
8 | include '../../../../../macros.inc' |
19 | include '../../../../../../macros.inc' |
9 | include '../../../../../dll.inc' |
20 | __DEBUG__ = 0 |
10 | ;include '../../../../../debug.inc' |
21 | 11 | ||
- | 12 | ;start_: |
|
- | 13 | virtual at 0 |
|
- | 14 | db 'MENUET01' ; 1. Magic number (8 bytes) |
|
- | 15 | dd 0x01 ; 2. Version of executable file |
|
- | 16 | dd start ; 3. Start address |
|
- | 17 | imgsz dd 0x0 ; 4. Size of image |
|
- | 18 | dd 0x100000 ; 5. Size of needed memory |
|
- | 19 | dd 0x100000 ; 6. Pointer to stack |
|
22 | __app_params equ 0x1C ; Pointer to program arguments |
20 | hparams dd 0x0 ; 7. Pointer to program arguments |
23 | ;__app_path equ 0x20 ; Pointer to program path |
21 | hpath dd 0x0 ; 8. Pointer to program path |
- | 22 | end virtual |
|
Line 24... | Line 23... | ||
24 | 23 | ||
25 | start: |
24 | start: |
- | 25 | ;DEBUGF 'Start programm\n' |
|
- | 26 | ;init heap of memory |
|
- | 27 | mov eax,68 |
|
- | 28 | mov ebx,11 |
|
Line 26... | Line 29... | ||
26 | ;DEBUGF 'Start programm\n' |
29 | int 0x40 |
27 | 30 | ||
28 | mov [argc], 0 |
31 | mov [argc], 0 |
29 | mov eax, [__app_params] |
32 | mov eax, [hparams] |
30 | test eax, eax |
33 | test eax, eax |
31 | jz .without_path |
34 | jz .without_path |
32 | mov eax, path |
35 | mov eax, path |
Line 80... | Line 83... | ||
80 | mov [ebx], ch |
83 | mov [ebx], ch |
81 | mov dl, ch |
84 | mov dl, ch |
82 | jmp .parse |
85 | jmp .parse |
Line 83... | Line 86... | ||
83 | 86 | ||
- | 87 | .run: |
|
84 | .run: |
88 | call load_imports |
85 | push argv |
89 | push argv |
86 | push [argc] |
90 | push [argc] |
87 | call main |
91 | call main |
88 | .exit: |
92 | .exit: |
Line 106... | Line 110... | ||
106 | jae .dont_add |
110 | jae .dont_add |
107 | mov [argv+4*ebx], esi |
111 | mov [argv+4*ebx], esi |
108 | inc [argc] |
112 | inc [argc] |
109 | .dont_add: |
113 | .dont_add: |
110 | ret |
114 | ret |
- | 115 | ;============================== |
|
Line -... | Line 116... | ||
- | 116 | ||
- | 117 | ;============================== |
|
- | 118 | load_imports: |
|
- | 119 | ;============================== |
|
- | 120 | ;parameters |
|
- | 121 | ; none |
|
- | 122 | ;description |
|
- | 123 | ; imports must be located at end of image (but before BSS sections) |
|
- | 124 | ; the address of end of imports (next byte after imports) is located in imgsz |
|
- | 125 | ; look at each import from that address up to illegal import |
|
- | 126 | ; legal import is such that: |
|
- | 127 | ; first pointer points to procedure name |
|
- | 128 | ; and is smaller than imgsz |
|
- | 129 | ; second pointer points lo library name, starting with 0x55, 0xAA |
|
- | 130 | ; and is smaller than imgsz |
|
- | 131 | ; each library should be initialized as appropriate, once |
|
- | 132 | ; so as library is initialized, its name will be replaced 0x00 |
|
- | 133 | mov ebx, [imgsz] ; byte after imports |
|
- | 134 | .handle_next_import: |
|
- | 135 | sub ebx, 4 ; ebx = pointer to pointer to library name |
|
- | 136 | mov esi, dword[ebx] ; esi = pointer to library name |
|
- | 137 | push ebx |
|
- | 138 | push esi |
|
- | 139 | call load_library ; eax = pointer to library exports |
|
- | 140 | pop esi |
|
- | 141 | pop ebx |
|
- | 142 | test eax, eax |
|
- | 143 | jz .done |
|
- | 144 | sub ebx, 4 ; ebx = pointer to pointer to symbol name |
|
- | 145 | push ebx |
|
- | 146 | stdcall dll.GetProcAddress, eax, dword[ebx] |
|
- | 147 | pop ebx |
|
- | 148 | test eax, eax |
|
- | 149 | jz .fail |
|
- | 150 | mov dword[ebx], eax |
|
- | 151 | jmp .handle_next_import |
|
- | 152 | .done: |
|
- | 153 | ;DEBUGF 1, "Library: %s not loaded!\n", esi |
|
- | 154 | ;mcall -1 |
|
- | 155 | ret |
|
- | 156 | .fail: |
|
Line 111... | Line 157... | ||
111 | 157 | ret |
|
- | 158 | ||
- | 159 | ;============================== |
|
- | 160 | ||
- | 161 | ;============================== |
|
- | 162 | load_library: |
|
- | 163 | ;============================== |
|
- | 164 | ;parameters |
|
- | 165 | ; ebx: library name address |
|
- | 166 | ;description |
|
- | 167 | ; each library should be initialized as appropriate, once |
|
- | 168 | ; so as library is initialized, its name will be replaced 0x00 |
|
- | 169 | ; and 4 next bytes will be set to address of library |
|
- | 170 | ; first two bytes of library name must be 0x55, 0xAA (is like a magic) |
|
- | 171 | cld ; move esi further, not back |
|
- | 172 | cmp esi, [imgsz] |
|
- | 173 | ja .fail |
|
- | 174 | lodsb ; al = first byte of library name |
|
- | 175 | cmp al, 0x55 |
|
- | 176 | jne .fail |
|
- | 177 | lodsb ; al = second byte of library name |
|
- | 178 | cmp al, 0xAA |
|
- | 179 | jne .fail |
|
- | 180 | lodsb ; al = third byte of library name (0x00 if the library is already loaded) |
|
- | 181 | test al, al |
|
- | 182 | jnz .load |
|
- | 183 | lodsd ; if we here, then third byte is 0x00 => address of library is in next 4 bytes |
|
- | 184 | ; now eax contains address of library |
|
- | 185 | ret |
|
- | 186 | .load: |
|
- | 187 | dec esi ; we checked on 0 before, let's go back |
|
- | 188 | mov eax, 68 |
|
- | 189 | mov ebx, 19 |
|
- | 190 | mov ecx, esi |
|
- | 191 | int 0x40 ; eax = address of exports |
|
- | 192 | mov byte[esi], 0 ; library is loaded, let's place 0 in first byte of name |
|
- | 193 | mov [esi + 1], eax ; now next 4 bytes of library name are replaced by address of library |
|
- | 194 | ; call lib_init |
|
- | 195 | stdcall dll.GetProcAddress, eax, lib_init_str ; eax = address of lib_init |
|
- | 196 | test eax, eax |
|
- | 197 | jz .ret |
|
- | 198 | stdcall dll.Init, eax |
|
- | 199 | .ret: |
|
- | 200 | mov eax, [esi + 1] ; put address of library into eax |
|
- | 201 | ret |
|
- | 202 | .fail: |
|
- | 203 | mov eax, 0 |
|
- | 204 | ret |
|
- | 205 | ||
- | 206 | ;============================== |
|
- | 207 | ||
112 | 208 | lib_init_str db 'lib_init', 0 |
|
113 | ;============================== |
209 | |
114 | public argc as '__argc' |
210 | public argc as '__argc' |
Line 115... | Line 211... | ||
115 | public params as '__argv' |
211 | public params as '__argv' |
Line 121... | Line 217... | ||
121 | argc rd 1 |
217 | argc rd 1 |
122 | argv rd max_parameters |
218 | argv rd max_parameters |
123 | path rb buf_len |
219 | path rb buf_len |
124 | params rb buf_len |
220 | params rb buf_len |
Line 125... | Line -... | ||
125 | - | ||
126 | ;section '.data' |
- |