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