Rev 6614 | Rev 6810 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 6614 | Rev 6767 | ||
---|---|---|---|
Line 8... | Line 8... | ||
8 | section '.text' code readable executable |
8 | section '.text' code readable executable |
Line 9... | Line 9... | ||
9 | 9 | ||
10 | FS_STACK_MAX equ dword [fs:4] |
10 | FS_STACK_MAX equ dword [fs:4] |
11 | FS_STACK_MIN equ dword [fs:8] |
11 | FS_STACK_MIN equ dword [fs:8] |
12 | FS_SELF_PTR equ dword [fs:0x18] |
- | |
13 | FS_PROCESS_DATA equ dword [fs:0x30] |
12 | FS_SELF_PTR equ dword [fs:0x18] |
14 | FS_ERRNO equ dword [fs:0x34] |
13 | FS_ERRNO equ dword [fs:0x34] |
Line 15... | Line 14... | ||
15 | FS_SYSCALL_PTR equ dword [fs:0xC0] |
14 | FS_SYSCALL_PTR equ dword [fs:0xC0] |
Line 38... | Line 37... | ||
38 | exe_path dd ? |
37 | exe_path dd ? |
39 | command_line dd ? |
38 | command_line dd ? |
40 | environment dd ? |
39 | environment dd ? |
41 | ends |
40 | ends |
Line -... | Line 41... | ||
- | 41 | ||
42 | 42 | include 'sync.inc' |
|
43 | include 'malloc.inc' |
43 | include 'malloc.inc' |
- | 44 | include 'peloader.inc' |
|
44 | include 'peloader.inc' |
45 | include 'modules.inc' |
Line 45... | Line 46... | ||
45 | include 'cmdline.inc' |
46 | include 'cmdline.inc' |
46 | 47 | ||
47 | proc syscall_int40 |
48 | proc syscall_int40 |
Line 73... | Line 74... | ||
73 | 74 | ||
74 | prologue@proc equ fpo_prologue |
75 | prologue@proc equ fpo_prologue |
Line 75... | Line 76... | ||
75 | epilogue@proc equ fpo_epilogue |
76 | epilogue@proc equ fpo_epilogue |
76 | - | ||
77 | proc start stdcall, dll_base, reason, reserved |
- | |
78 | locals |
- | |
79 | exe_base dd ? |
- | |
80 | exe_path_size dd ? |
77 | |
81 | endl |
78 | proc start stdcall, dll_base, reason, reserved |
82 | ; 1. Do nothing unless called by the kernel for DLL_PROCESS_ATTACH. |
79 | ; 1. Do nothing unless called by the kernel for DLL_PROCESS_ATTACH. |
83 | cmp [reason], DLL_PROCESS_ATTACH |
80 | cmp [reason], DLL_PROCESS_ATTACH |
84 | jnz .nothing |
81 | jnz .nothing |
Line 110... | Line 107... | ||
110 | ; configuring FS_SYSCALL_PTR at step 2b. |
107 | ; configuring FS_SYSCALL_PTR at step 2b. |
111 | push kolibri_dll |
108 | push kolibri_dll |
112 | call fixup_pe_relocations |
109 | call fixup_pe_relocations |
113 | pop ecx |
110 | pop ecx |
114 | jc .die |
111 | jc .die |
115 | ; 2d. Allocate process data. |
- | |
116 | mov eax, 68 |
- | |
117 | mov ebx, 12 |
- | |
118 | mov ecx, 0x1000 |
- | |
119 | call FS_SYSCALL_PTR |
- | |
120 | mov FS_PROCESS_DATA, eax |
- | |
121 | ; 2e. Initialize process heap. |
112 | ; 2d. Initialize process heap. |
122 | mov eax, [ebp+kernel_init_data.exe_base] |
113 | mov eax, [ebp+kernel_init_data.exe_base] |
123 | mov [exe_base], eax |
- | |
124 | mov edx, [eax+STRIPPED_PE_HEADER.SizeOfHeapReserve] |
114 | mov edx, [eax+STRIPPED_PE_HEADER.SizeOfHeapReserve] |
125 | cmp word [eax], 'MZ' |
115 | cmp word [eax], 'MZ' |
126 | jnz @f |
116 | jnz @f |
127 | add eax, [eax+IMAGE_DOS_HEADER.e_lfanew] |
117 | add eax, [eax+IMAGE_DOS_HEADER.e_lfanew] |
128 | mov edx, [eax+IMAGE_NT_HEADERS.OptionalHeader.SizeOfHeapReserve] |
118 | mov edx, [eax+IMAGE_NT_HEADERS.OptionalHeader.SizeOfHeapReserve] |
129 | @@: |
119 | @@: |
130 | malloc_init |
120 | malloc_init |
- | 121 | ; 2e. Allocate and fill MODULE structs for main exe and kolibri.dll. |
|
- | 122 | mov eax, [ebp+kernel_init_data.exe_path] |
|
- | 123 | @@: |
|
- | 124 | inc eax |
|
- | 125 | cmp byte [eax-1], 0 |
|
- | 126 | jnz @b |
|
- | 127 | sub eax, [ebp+kernel_init_data.exe_path] |
|
- | 128 | push eax |
|
- | 129 | add eax, sizeof.MODULE |
|
- | 130 | stdcall malloc, eax |
|
- | 131 | test eax, eax |
|
- | 132 | jz .die |
|
- | 133 | mov ebx, eax |
|
- | 134 | stdcall malloc, sizeof.MODULE + kolibri_dll.size |
|
- | 135 | test eax, eax |
|
- | 136 | jz .die |
|
- | 137 | mov edx, modules_list |
|
- | 138 | mov [edx+MODULE.next], ebx |
|
- | 139 | mov [ebx+MODULE.next], eax |
|
- | 140 | mov [eax+MODULE.next], edx |
|
- | 141 | mov [edx+MODULE.prev], eax |
|
- | 142 | mov [eax+MODULE.prev], ebx |
|
- | 143 | mov [ebx+MODULE.prev], edx |
|
- | 144 | push esi |
|
- | 145 | mov esi, kolibri_dll |
|
- | 146 | mov ecx, kolibri_dll.size |
|
- | 147 | lea edi, [eax+MODULE.path] |
|
- | 148 | rep movsb |
|
- | 149 | pop esi |
|
- | 150 | call init_module_struct |
|
- | 151 | mov eax, ebx |
|
- | 152 | mov esi, [ebp+kernel_init_data.exe_path] |
|
- | 153 | pop ecx |
|
- | 154 | lea edi, [ebx+MODULE.path] |
|
- | 155 | rep movsb |
|
- | 156 | mov esi, [ebp+kernel_init_data.exe_base] |
|
- | 157 | call init_module_struct |
|
131 | ; 2f. Copy rest of init struct and free memory. |
158 | ; 2f. Copy rest of init struct and free memory. |
132 | ; Parse command line to argc/argv here and move arguments to the heap |
159 | ; Parse command line to argc/argv here and move arguments to the heap |
133 | ; in order to save memory: init struct and heap use different pages, |
160 | ; in order to save memory: init struct and heap use different pages, |
134 | ; but typically data from init struct are far from the entire page, |
161 | ; but typically data from init struct are far from the entire page, |
135 | ; so moving it to heap does not increase actual physical heap size |
162 | ; so moving it to heap does not increase actual physical heap size |
136 | ; and allows to free init struct. |
163 | ; and allows to free init struct. |
137 | mov eax, [ebp+kernel_init_data.stack_base] |
164 | mov eax, [ebp+kernel_init_data.stack_base] |
138 | mov FS_STACK_MIN, eax |
165 | mov FS_STACK_MIN, eax |
139 | add eax, [ebp+kernel_init_data.stack_size] |
166 | add eax, [ebp+kernel_init_data.stack_size] |
140 | mov FS_STACK_MAX, eax |
167 | mov FS_STACK_MAX, eax |
141 | mov eax, [ebp+kernel_init_data.exe_path] |
- | |
142 | @@: |
- | |
143 | inc eax |
- | |
144 | cmp byte [eax-1], 0 |
- | |
145 | jnz @b |
- | |
146 | sub eax, [ebp+kernel_init_data.exe_path] |
- | |
147 | mov [exe_path_size], eax |
- | |
148 | mov esi, [ebp+kernel_init_data.command_line] |
168 | mov esi, [ebp+kernel_init_data.command_line] |
149 | xor edx, edx |
169 | xor edx, edx |
150 | xor edi, edi |
170 | xor edi, edi |
151 | call parse_cmdline |
171 | call parse_cmdline |
152 | inc ebx ; argv[0] = exe path |
172 | inc ebx ; argv[0] = exe path |
Line 154... | Line 174... | ||
154 | .argv equ reason |
174 | .argv equ reason |
155 | .envp equ reserved |
175 | .envp equ reserved |
156 | mov [.argc], ebx |
176 | mov [.argc], ebx |
157 | sub esi, [ebp+kernel_init_data.command_line] |
177 | sub esi, [ebp+kernel_init_data.command_line] |
158 | lea esi, [esi+(ebx+1)*4] |
178 | lea esi, [esi+(ebx+1)*4] |
159 | add esi, [exe_path_size] |
- | |
160 | stdcall malloc, esi |
179 | stdcall malloc, esi |
- | 180 | test eax, eax |
|
- | 181 | jz .die |
|
161 | mov [.argv], eax |
182 | mov [.argv], eax |
162 | mov edx, eax |
183 | mov edx, eax |
163 | lea edi, [eax+ebx*4] |
184 | lea edi, [eax+(ebx+1)*4] |
164 | mov esi, [ebp+kernel_init_data.exe_path] |
185 | mov eax, [modules_list + MODULE.next] |
- | 186 | add eax, MODULE.path |
|
165 | mov [edx], edi |
187 | mov [edx], eax |
166 | add edx, 4 |
188 | add edx, 4 |
167 | mov ecx, [exe_path_size] |
- | |
168 | rep movsb |
- | |
169 | mov esi, [ebp+kernel_init_data.command_line] |
189 | mov esi, [ebp+kernel_init_data.command_line] |
170 | call parse_cmdline |
190 | call parse_cmdline |
171 | and dword [edx], 0 ; argv[argc] = NULL |
191 | and dword [edx], 0 ; argv[argc] = NULL |
172 | and [.envp], 0 |
192 | and [.envp], 0 |
173 | mov eax, 68 |
193 | mov eax, 68 |
174 | mov ebx, 13 |
194 | mov ebx, 13 |
175 | mov ecx, ebp |
195 | mov ecx, ebp |
176 | call FS_SYSCALL_PTR |
196 | call FS_SYSCALL_PTR |
- | 197 | ; 2g. Initialize mutex for list of MODULEs. |
|
- | 198 | mov ecx, modules_mutex |
|
- | 199 | call mutex_init |
|
- | 200 | ; 2h. For console applications, call console.dll!con_init with default parameters. |
|
- | 201 | mov eax, [modules_list + MODULE.next] |
|
- | 202 | mov esi, [eax+MODULE.base] |
|
- | 203 | mov al, [esi+STRIPPED_PE_HEADER.Subsystem] |
|
- | 204 | cmp byte [esi], 'M' |
|
- | 205 | jnz @f |
|
- | 206 | mov eax, [esi+3Ch] |
|
- | 207 | mov al, byte [esi+eax+IMAGE_NT_HEADERS.OptionalHeader.Subsystem] |
|
- | 208 | @@: |
|
- | 209 | cmp al, IMAGE_SUBSYSTEM_WINDOWS_CUI |
|
- | 210 | jnz .noconsole |
|
- | 211 | stdcall dlopen, console_dll, 0 |
|
- | 212 | test eax, eax |
|
- | 213 | jz .noconsole |
|
- | 214 | stdcall dlsym, eax, con_init_str |
|
- | 215 | test eax, eax |
|
- | 216 | jz .noconsole |
|
- | 217 | mov edx, [modules_list + MODULE.next] |
|
- | 218 | stdcall eax, -1, -1, -1, -1, [edx+MODULE.filename] |
|
- | 219 | .noconsole: |
|
177 | ; 3. Configure modules: main EXE and possible statically linked DLLs. |
220 | ; 3. Configure modules: main EXE and possible statically linked DLLs. |
- | 221 | mov eax, [modules_list + MODULE.next] |
|
178 | mov esi, [exe_base] |
222 | mov esi, [eax+MODULE.base] |
179 | mov eax, [.argv] |
223 | add eax, MODULE.path |
180 | pushd [eax] |
224 | push eax |
181 | call fixup_pe_relocations |
225 | call fixup_pe_relocations |
182 | pop ecx |
226 | pop ecx |
183 | jc .die |
227 | jc .die |
- | 228 | mutex_lock modules_mutex |
|
- | 229 | mov esi, [modules_list + MODULE.next] |
|
- | 230 | call resolve_pe_imports |
|
- | 231 | mov ebx, eax |
|
- | 232 | mutex_unlock modules_mutex |
|
- | 233 | test ebx, ebx |
|
- | 234 | jnz .die |
|
184 | ; 4. Call exe entry point. |
235 | ; 4. Call exe entry point. |
- | 236 | mov esi, [esi+MODULE.base] |
|
185 | mov edx, [esi+STRIPPED_PE_HEADER.AddressOfEntryPoint] |
237 | mov edx, [esi+STRIPPED_PE_HEADER.AddressOfEntryPoint] |
186 | cmp word [esi], 'MZ' |
238 | cmp byte [esi], 'M' |
187 | jnz @f |
239 | jnz @f |
188 | mov ecx, [esi+IMAGE_DOS_HEADER.e_lfanew] |
240 | mov ecx, [esi+IMAGE_DOS_HEADER.e_lfanew] |
189 | add ecx, esi |
241 | add ecx, esi |
190 | mov edx, [ecx+IMAGE_NT_HEADERS.OptionalHeader.AddressOfEntryPoint] |
242 | mov edx, [ecx+IMAGE_NT_HEADERS.OptionalHeader.AddressOfEntryPoint] |
191 | @@: |
243 | @@: |
192 | add edx, esi |
244 | add edx, esi |
- | 245 | pop ecx |
|
193 | add esp, fpo_localsize+4 |
246 | mov [process_initialized], 1 |
194 | call edx |
247 | call edx |
195 | ; If exe entry point has returned control, die. |
248 | ; If exe entry point has returned control, die. |
196 | jmp .die |
249 | jmp .die |
197 | .version_mismatch: |
250 | .version_mismatch: |
198 | lea eax, [esi + rva syscall_int40] |
251 | lea eax, [esi + rva syscall_int40] |
Line 244... | Line 297... | ||
244 | , mspace_free, 'mspace_free' \ |
297 | , mspace_free, 'mspace_free' \ |
245 | , mspace_calloc, 'mspace_calloc' \ |
298 | , mspace_calloc, 'mspace_calloc' \ |
246 | , mspace_realloc, 'mspace_realloc' \ |
299 | , mspace_realloc, 'mspace_realloc' \ |
247 | , mspace_realloc_in_place, 'mspace_realloc_in_place' \ |
300 | , mspace_realloc_in_place, 'mspace_realloc_in_place' \ |
248 | , mspace_memalign, 'mspace_memalign' \ |
301 | , mspace_memalign, 'mspace_memalign' \ |
- | 302 | , dlopen, 'dlopen' \ |
|
- | 303 | , dlclose, 'dlclose' \ |
|
- | 304 | , dlsym, 'dlsym' \ |
|
Line 249... | Line 305... | ||
249 | 305 | ||
Line 250... | Line 306... | ||
250 | end data |
306 | end data |
- | 307 | ||
- | 308 | kolibri_dll db '/rd/1/lib/kolibri.dll',0 |
|
- | 309 | .size = $ - kolibri_dll |
|
- | 310 | ||
Line 251... | Line 311... | ||
251 | 311 | console_dll db 'console.dll',0 |
|
252 | kolibri_dll db 'kolibri.dll',0 |
312 | con_init_str db 'con_init',0 |
253 | 313 | ||
254 | msg_version_mismatch db 'S : Version mismatch between kernel and kolibri.dll',13,10,0 |
314 | msg_version_mismatch db 'S : Version mismatch between kernel and kolibri.dll',13,10,0 |
255 | msg_bad_relocation1 db 'S : Bad relocation type in ',0 |
315 | msg_bad_relocation db 'Bad relocation type in ',0 |
- | 316 | msg_newline db 13,10,0 |
|
- | 317 | msg_relocated1 db 'S : fixups for ',0 |
|
- | 318 | msg_relocated2 db ' applied',13,10,0 |
|
- | 319 | msg_noreloc1 db 'Module ',0 |
|
- | 320 | msg_noreloc2 db ' is not at preferred base and has no fixups',0 |
|
- | 321 | loader_debugboard_prefix db 'S : ',0 |
|
- | 322 | notify_program db '/rd/1/@notify',0 |
|
- | 323 | msg_cannot_open db 'Cannot open ',0 |
|
- | 324 | msg_paths_begin db ' in any of ' |
|
- | 325 | ||
- | 326 | module_path1 db '/rd/1/lib/' |
|
- | 327 | .size = $ - module_path1 |
|
- | 328 | db ', ' |
|
- | 329 | module_path2 db '/kolibrios/lib/' |
|
- | 330 | .size = $ - module_path2 |
|
- | 331 | db ', ',0 |
|
- | 332 | msg_export_name_not_found db 'Exported function ',0 |
|
Line 256... | Line -... | ||
256 | msg_newline db 13,10,0 |
- | |
257 | msg_relocated1 db 'S : fixups for ',0 |
333 | msg_export_ordinal_not_found db 'Exported ordinal #',0 |
- | 334 | msg_export_not_found db ' not found in module ',0 |
|
258 | msg_relocated2 db ' applied',13,10,0 |
335 | msg_unknown db ' |
259 | 336 | ||
- | 337 | section '.data' data readable writable |
|
- | 338 | if FOOTERS |
|
- | 339 | malloc_magic dd ? |
|
- | 340 | end if |