2011-10-14 23:38:50 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2024-05-22 17:15:14 +02:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2024. All rights reserved. ;;
|
2011-10-14 23:38:50 +02:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
|
|
|
|
include 'export.inc'
|
|
|
|
|
|
|
|
align 4
|
|
|
|
|
2021-12-07 03:49:57 +01:00
|
|
|
; This function load file driver and output
|
|
|
|
; eax = address function START in driver
|
|
|
|
; ebx = address for kernel_free
|
2011-10-14 23:38:50 +02:00
|
|
|
proc load_PE stdcall, file_name:dword
|
|
|
|
locals
|
|
|
|
image dd ?
|
|
|
|
entry dd ?
|
|
|
|
base dd ?
|
|
|
|
endl
|
|
|
|
|
|
|
|
stdcall load_file, [file_name]
|
|
|
|
test eax, eax
|
|
|
|
jz .fail
|
|
|
|
|
|
|
|
mov [image], eax
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
mov edx, [eax + STRIPPED_PE_HEADER.SizeOfImage]
|
|
|
|
; mov cl, [eax + STRIPPED_PE_HEADER.Subsystem]
|
2014-08-18 13:18:15 +02:00
|
|
|
cmp word [eax], STRIPPED_PE_SIGNATURE
|
|
|
|
jz @f
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
mov edx, [eax + 60]
|
|
|
|
; mov cl, [eax + 5Ch + edx]
|
|
|
|
mov edx, [eax + 80 + edx]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2014-08-18 13:18:15 +02:00
|
|
|
@@:
|
|
|
|
mov [entry], 0
|
|
|
|
; cmp cl, 1
|
|
|
|
; jnz .cleanup
|
|
|
|
stdcall kernel_alloc, edx
|
2011-10-14 23:38:50 +02:00
|
|
|
test eax, eax
|
|
|
|
jz .cleanup
|
|
|
|
|
|
|
|
mov [base], eax
|
2015-05-25 17:44:26 +02:00
|
|
|
DEBUGF 1,'K : driver %s mapped to %x\n',[file_name],[base]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2014-08-18 13:18:15 +02:00
|
|
|
push ebx ebp
|
|
|
|
mov ebx, [image]
|
|
|
|
mov ebp, eax
|
|
|
|
call map_PE
|
|
|
|
pop ebp ebx
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
mov [entry], eax
|
|
|
|
test eax, eax
|
|
|
|
jnz .cleanup
|
|
|
|
|
|
|
|
stdcall kernel_free, [base]
|
|
|
|
.cleanup:
|
|
|
|
stdcall kernel_free, [image]
|
|
|
|
mov eax, [entry]
|
2021-12-07 03:49:57 +01:00
|
|
|
mov ebx, [base]
|
2011-10-14 23:38:50 +02:00
|
|
|
ret
|
|
|
|
.fail:
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
2014-08-18 13:18:15 +02:00
|
|
|
map_PE: ;ebp=base:dword, ebx=image:dword
|
2011-10-14 23:38:50 +02:00
|
|
|
push edi
|
|
|
|
push esi
|
2014-08-18 13:18:15 +02:00
|
|
|
sub esp, .locals_size
|
|
|
|
virtual at esp
|
|
|
|
.numsections dd ?
|
|
|
|
.import_names dd ?
|
|
|
|
.import_targets dd ?
|
|
|
|
.peheader dd ?
|
|
|
|
.bad_import dd ?
|
|
|
|
.import_idx dd ?
|
|
|
|
.import_descr dd ?
|
|
|
|
.relocs_rva dd ?
|
|
|
|
.relocs_size dd ?
|
|
|
|
.section_header_size dd ?
|
|
|
|
.AddressOfEntryPoint dd ?
|
|
|
|
.ImageBase dd ?
|
|
|
|
.locals_size = $ - esp
|
|
|
|
end virtual
|
|
|
|
cmp word [ebx], STRIPPED_PE_SIGNATURE
|
|
|
|
jz .stripped
|
|
|
|
|
2011-10-14 23:38:50 +02:00
|
|
|
mov edx, ebx
|
2022-02-12 18:27:41 +01:00
|
|
|
add edx, [ebx + 60]
|
|
|
|
movzx eax, word [edx + 6]
|
2014-08-18 13:18:15 +02:00
|
|
|
mov [.numsections], eax
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [edx + 40]
|
2014-08-18 13:18:15 +02:00
|
|
|
mov [.AddressOfEntryPoint], eax
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [edx + 52]
|
2014-08-18 13:18:15 +02:00
|
|
|
mov [.ImageBase], eax
|
2022-02-12 18:27:41 +01:00
|
|
|
mov ecx, [edx + 84]
|
2014-08-18 13:18:15 +02:00
|
|
|
mov [.section_header_size], 40
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [edx + 128]
|
2014-08-18 13:18:15 +02:00
|
|
|
mov [.import_descr], eax
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [edx + 160]
|
2014-08-18 13:18:15 +02:00
|
|
|
mov [.relocs_rva], eax
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [edx + 164]
|
2014-08-18 13:18:15 +02:00
|
|
|
mov [.relocs_size], eax
|
|
|
|
add edx, 256
|
|
|
|
|
|
|
|
jmp .common
|
|
|
|
.stripped:
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [ebx + STRIPPED_PE_HEADER.AddressOfEntryPoint]
|
2014-08-18 13:18:15 +02:00
|
|
|
mov [.AddressOfEntryPoint], eax
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [ebx + STRIPPED_PE_HEADER.ImageBase]
|
2014-08-18 13:18:15 +02:00
|
|
|
mov [.ImageBase], eax
|
2022-02-12 18:27:41 +01:00
|
|
|
movzx eax, [ebx + STRIPPED_PE_HEADER.NumberOfSections]
|
2014-08-18 13:18:15 +02:00
|
|
|
mov [.numsections], eax
|
2022-02-12 18:27:41 +01:00
|
|
|
movzx ecx, [ebx + STRIPPED_PE_HEADER.NumberOfRvaAndSizes]
|
2014-08-18 13:18:15 +02:00
|
|
|
xor eax, eax
|
|
|
|
mov [.relocs_rva], eax
|
|
|
|
mov [.relocs_size], eax
|
|
|
|
test ecx, ecx
|
|
|
|
jz @f
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [ebx + sizeof.STRIPPED_PE_HEADER + SPE_DIRECTORY_IMPORT*8]
|
2014-08-18 13:18:15 +02:00
|
|
|
@@:
|
|
|
|
mov [.import_descr], eax
|
|
|
|
cmp ecx, SPE_DIRECTORY_BASERELOC
|
|
|
|
jbe @f
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [ebx + sizeof.STRIPPED_PE_HEADER + SPE_DIRECTORY_BASERELOC*8]
|
2014-08-18 13:18:15 +02:00
|
|
|
mov [.relocs_rva], eax
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [ebx + sizeof.STRIPPED_PE_HEADER + SPE_DIRECTORY_BASERELOC*8+4]
|
2014-08-18 13:18:15 +02:00
|
|
|
mov [.relocs_size], eax
|
|
|
|
@@:
|
|
|
|
mov [.section_header_size], 28
|
2022-02-12 18:27:41 +01:00
|
|
|
lea edx, [ebx + ecx*8 + sizeof.STRIPPED_PE_HEADER + 8]
|
|
|
|
mov ecx, [ebx + STRIPPED_PE_HEADER.SizeOfHeaders]
|
2014-08-18 13:18:15 +02:00
|
|
|
|
|
|
|
.common:
|
2011-10-14 23:38:50 +02:00
|
|
|
mov esi, ebx
|
|
|
|
mov edi, ebp
|
|
|
|
shr ecx, 2
|
|
|
|
rep movsd
|
|
|
|
|
2014-08-18 13:18:15 +02:00
|
|
|
cmp [.numsections], 0
|
|
|
|
jz .nosections
|
|
|
|
.copy_sections:
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [edx + 8]
|
2011-10-14 23:38:50 +02:00
|
|
|
test eax, eax
|
2014-08-18 13:18:15 +02:00
|
|
|
je .no_section_data
|
2011-10-14 23:38:50 +02:00
|
|
|
mov esi, ebx
|
|
|
|
mov edi, ebp
|
2022-02-12 18:27:41 +01:00
|
|
|
add esi, [edx + 12]
|
2011-10-14 23:38:50 +02:00
|
|
|
mov ecx, eax
|
2022-02-12 18:27:41 +01:00
|
|
|
add edi, [edx + 4]
|
2011-10-14 23:38:50 +02:00
|
|
|
|
2013-12-30 12:18:33 +01:00
|
|
|
add ecx, 3
|
2011-10-14 23:38:50 +02:00
|
|
|
shr ecx, 2
|
|
|
|
rep movsd
|
|
|
|
|
2014-08-18 13:18:15 +02:00
|
|
|
.no_section_data:
|
|
|
|
mov ecx, [edx]
|
2011-10-14 23:38:50 +02:00
|
|
|
cmp ecx, eax
|
2014-08-18 13:18:15 +02:00
|
|
|
jbe .no_section_fill
|
2011-10-14 23:38:50 +02:00
|
|
|
sub ecx, eax
|
2022-02-12 18:27:41 +01:00
|
|
|
add eax, [edx + 4]
|
2011-10-14 23:38:50 +02:00
|
|
|
lea edi, [eax+ebp]
|
|
|
|
|
|
|
|
xor eax, eax
|
|
|
|
rep stosb
|
|
|
|
|
2014-08-18 13:18:15 +02:00
|
|
|
.no_section_fill:
|
|
|
|
add edx, [.section_header_size]
|
|
|
|
dec [.numsections]
|
|
|
|
jnz .copy_sections
|
|
|
|
.nosections:
|
|
|
|
cmp [.relocs_size], 0
|
|
|
|
je .no_relocations
|
2011-10-14 23:38:50 +02:00
|
|
|
mov esi, ebp
|
|
|
|
mov ecx, ebp
|
2014-08-18 13:18:15 +02:00
|
|
|
sub esi, [.ImageBase]
|
|
|
|
add ecx, [.relocs_rva]
|
|
|
|
.relocs_block:
|
|
|
|
mov edi, [ecx]
|
|
|
|
add edi, ebp
|
2022-02-12 18:27:41 +01:00
|
|
|
mov ebx, [ecx + 4]
|
2014-08-18 13:18:15 +02:00
|
|
|
add ecx, 8
|
|
|
|
sub [.relocs_size], ebx
|
|
|
|
sub ebx, 8
|
2011-10-14 23:38:50 +02:00
|
|
|
shr ebx, 1
|
2014-08-18 13:18:15 +02:00
|
|
|
jz .relocs_next_block
|
|
|
|
.one_reloc:
|
|
|
|
movzx eax, word [ecx]
|
|
|
|
add ecx, 2
|
2011-10-14 23:38:50 +02:00
|
|
|
mov edx, eax
|
|
|
|
shr eax, 12
|
|
|
|
and edx, 4095
|
2014-08-18 13:18:15 +02:00
|
|
|
cmp eax, 3
|
|
|
|
jne @f
|
|
|
|
add [edx+edi], esi
|
|
|
|
@@:
|
|
|
|
dec ebx
|
|
|
|
jnz .one_reloc
|
|
|
|
.relocs_next_block:
|
|
|
|
cmp [.relocs_size], 0
|
|
|
|
jg .relocs_block
|
|
|
|
.no_relocations:
|
|
|
|
cmp [.import_descr], 0
|
|
|
|
je .no_imports
|
|
|
|
add [.import_descr], ebp
|
|
|
|
mov [.bad_import], 0
|
|
|
|
.import_block:
|
|
|
|
mov ecx, [.import_descr]
|
2022-02-12 18:27:41 +01:00
|
|
|
cmp dword [ecx + 4], 0
|
2014-08-18 13:18:15 +02:00
|
|
|
jne @f
|
2022-02-12 18:27:41 +01:00
|
|
|
cmp dword [ecx + 12], 0
|
2014-08-18 13:18:15 +02:00
|
|
|
je .done_imports
|
|
|
|
@@:
|
|
|
|
mov edx, dword [ecx]
|
2022-02-12 18:27:41 +01:00
|
|
|
mov ecx, dword [ecx + 16]
|
2013-12-30 12:18:33 +01:00
|
|
|
test edx, edx
|
|
|
|
jnz @f
|
|
|
|
mov edx, ecx
|
|
|
|
@@:
|
2014-08-18 13:18:15 +02:00
|
|
|
mov [.import_idx], 0
|
2011-10-14 23:38:50 +02:00
|
|
|
add ecx, ebp
|
|
|
|
add edx, ebp
|
2014-08-18 13:18:15 +02:00
|
|
|
mov [.import_names], edx
|
|
|
|
mov [.import_targets], ecx
|
|
|
|
.import_func:
|
|
|
|
mov esi, [.import_idx]
|
|
|
|
mov edi, [.import_names]
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [edi + esi*4]
|
2011-10-14 23:38:50 +02:00
|
|
|
test eax, eax
|
2014-08-18 13:18:15 +02:00
|
|
|
je .next_import_block
|
|
|
|
js .next_import_block
|
2022-02-12 18:27:41 +01:00
|
|
|
lea edi, [ebp + eax]
|
2014-08-18 13:18:15 +02:00
|
|
|
mov eax, [.import_targets]
|
2022-02-12 18:27:41 +01:00
|
|
|
mov dword [eax + esi*4], 0
|
|
|
|
lea esi, [edi + 2]
|
2014-08-18 13:18:15 +02:00
|
|
|
movzx ebx, word [edi]
|
2011-10-14 23:38:50 +02:00
|
|
|
push 32
|
2022-02-12 18:27:41 +01:00
|
|
|
mov ecx, [__exports + 32]
|
|
|
|
mov eax, [ecx + OS_BASE + ebx*4]
|
2011-10-14 23:38:50 +02:00
|
|
|
add eax, OS_BASE
|
|
|
|
push eax
|
|
|
|
push esi
|
|
|
|
call strncmp
|
|
|
|
test eax, eax
|
2014-08-18 13:18:15 +02:00
|
|
|
jz .import_func_found
|
|
|
|
xor ebx, ebx
|
|
|
|
.import_func_candidate:
|
2011-10-14 23:38:50 +02:00
|
|
|
push 32
|
2022-02-12 18:27:41 +01:00
|
|
|
mov ecx, [__exports + 32]
|
|
|
|
mov eax, [ecx + OS_BASE + ebx*4]
|
2011-10-14 23:38:50 +02:00
|
|
|
add eax, OS_BASE
|
|
|
|
push eax
|
|
|
|
push esi
|
|
|
|
call strncmp
|
|
|
|
test eax, eax
|
2014-08-18 13:18:15 +02:00
|
|
|
je .import_func_found
|
2011-10-14 23:38:50 +02:00
|
|
|
inc ebx
|
2022-02-12 18:27:41 +01:00
|
|
|
cmp ebx, [__exports + 24]
|
2014-08-18 13:18:15 +02:00
|
|
|
jb .import_func_candidate
|
2011-10-14 23:38:50 +02:00
|
|
|
|
|
|
|
mov esi, msg_unresolved
|
|
|
|
call sys_msg_board_str
|
2022-02-12 18:27:41 +01:00
|
|
|
lea esi, [edi + 2]
|
2011-10-14 23:38:50 +02:00
|
|
|
call sys_msg_board_str
|
|
|
|
mov esi, msg_CR
|
|
|
|
call sys_msg_board_str
|
|
|
|
|
2014-08-18 13:18:15 +02:00
|
|
|
mov [.bad_import], 1
|
|
|
|
jmp .next_import_func
|
|
|
|
.import_func_found:
|
2022-02-12 18:27:41 +01:00
|
|
|
mov esi, [__exports + 28]
|
2014-08-18 13:18:15 +02:00
|
|
|
mov edx, [.import_idx]
|
|
|
|
mov ecx, [.import_targets]
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [esi + OS_BASE + ebx*4]
|
2011-10-14 23:38:50 +02:00
|
|
|
add eax, OS_BASE
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [ecx + edx*4], eax
|
2014-08-18 13:18:15 +02:00
|
|
|
.next_import_func:
|
|
|
|
inc [.import_idx]
|
|
|
|
jmp .import_func
|
|
|
|
.next_import_block:
|
|
|
|
add [.import_descr], 20
|
|
|
|
jmp .import_block
|
|
|
|
.done_imports:
|
2011-10-14 23:38:50 +02:00
|
|
|
xor eax, eax
|
2014-08-18 13:18:15 +02:00
|
|
|
cmp [.bad_import], 0
|
|
|
|
jne @f
|
|
|
|
.no_imports:
|
2011-10-14 23:38:50 +02:00
|
|
|
mov eax, ebp
|
2014-08-18 13:18:15 +02:00
|
|
|
add eax, [.AddressOfEntryPoint]
|
|
|
|
@@:
|
|
|
|
add esp, .locals_size
|
2011-10-14 23:38:50 +02:00
|
|
|
pop esi
|
|
|
|
pop edi
|
2014-08-18 13:18:15 +02:00
|
|
|
ret
|