2010-10-01 11:21:55 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2012-03-15 13:41:29 +01:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;;
|
2010-10-01 11:21:55 +02: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
|
|
|
|
|
2012-03-08 09:33:38 +01:00
|
|
|
stdcall load_file, [file_name]
|
|
|
|
test eax, eax
|
|
|
|
jz .fail
|
2010-10-01 11:21:55 +02:00
|
|
|
|
2012-03-08 09:33:38 +01:00
|
|
|
mov [image], eax
|
2010-10-01 11:21:55 +02:00
|
|
|
|
2014-12-01 04:51:07 +01:00
|
|
|
mov edx, [eax+STRIPPED_PE_HEADER.SizeOfImage]
|
|
|
|
; mov cl, [eax+STRIPPED_PE_HEADER.Subsystem]
|
|
|
|
cmp word [eax], STRIPPED_PE_SIGNATURE
|
|
|
|
jz @f
|
|
|
|
|
2012-03-08 09:33:38 +01:00
|
|
|
mov edx, [eax+60]
|
2014-12-01 04:51:07 +01:00
|
|
|
; mov cl, [eax+5Ch+edx]
|
|
|
|
mov edx, [eax+80+edx]
|
2010-10-01 11:21:55 +02:00
|
|
|
|
2014-12-01 04:51:07 +01:00
|
|
|
@@:
|
|
|
|
mov [entry], 0
|
|
|
|
; cmp cl, 1
|
|
|
|
; jnz .cleanup
|
|
|
|
stdcall kernel_alloc, edx
|
2012-03-08 09:33:38 +01:00
|
|
|
test eax, eax
|
|
|
|
jz .cleanup
|
2010-10-01 11:21:55 +02:00
|
|
|
|
2012-03-08 09:33:38 +01:00
|
|
|
mov [base], eax
|
2010-10-01 11:21:55 +02:00
|
|
|
|
2014-12-01 04:51:07 +01:00
|
|
|
push ebx ebp
|
|
|
|
mov ebx, [image]
|
|
|
|
mov ebp, eax
|
|
|
|
call map_PE
|
|
|
|
pop ebp ebx
|
2010-10-01 11:21:55 +02:00
|
|
|
|
2012-03-08 09:33:38 +01:00
|
|
|
mov [entry], eax
|
|
|
|
test eax, eax
|
|
|
|
jnz .cleanup
|
2010-10-01 11:21:55 +02:00
|
|
|
|
2012-03-08 09:33:38 +01:00
|
|
|
stdcall kernel_free, [base]
|
2010-10-01 11:21:55 +02:00
|
|
|
.cleanup:
|
2012-03-08 09:33:38 +01:00
|
|
|
stdcall kernel_free, [image]
|
|
|
|
mov eax, [entry]
|
|
|
|
ret
|
2010-10-01 11:21:55 +02:00
|
|
|
.fail:
|
2012-03-08 09:33:38 +01:00
|
|
|
xor eax, eax
|
|
|
|
ret
|
2010-10-01 11:21:55 +02:00
|
|
|
endp
|
|
|
|
|
2014-12-01 04:51:07 +01:00
|
|
|
map_PE: ;ebp=base:dword, ebx=image:dword
|
2012-03-08 09:33:38 +01:00
|
|
|
push edi
|
|
|
|
push esi
|
2014-12-01 04:51:07 +01: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
|
|
|
|
|
2012-03-08 09:33:38 +01:00
|
|
|
mov edx, ebx
|
2014-12-01 04:51:07 +01: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:
|
2012-03-08 09:33:38 +01:00
|
|
|
mov esi, ebx
|
|
|
|
mov edi, ebp
|
|
|
|
shr ecx, 2
|
|
|
|
rep movsd
|
2010-10-01 11:21:55 +02:00
|
|
|
|
2014-12-01 04:51:07 +01:00
|
|
|
cmp [.numsections], 0
|
|
|
|
jz .nosections
|
|
|
|
.copy_sections:
|
|
|
|
mov eax, [edx+8]
|
2012-03-08 09:33:38 +01:00
|
|
|
test eax, eax
|
2014-12-01 04:51:07 +01:00
|
|
|
je .no_section_data
|
2012-03-08 09:33:38 +01:00
|
|
|
mov esi, ebx
|
|
|
|
mov edi, ebp
|
2014-12-01 04:51:07 +01:00
|
|
|
add esi, [edx+12]
|
2012-03-08 09:33:38 +01:00
|
|
|
mov ecx, eax
|
2014-12-01 04:51:07 +01:00
|
|
|
add edi, [edx+4]
|
2010-10-01 11:21:55 +02:00
|
|
|
|
2014-01-01 22:16:27 +01:00
|
|
|
add ecx, 3
|
2012-03-08 09:33:38 +01:00
|
|
|
shr ecx, 2
|
|
|
|
rep movsd
|
2010-10-01 11:21:55 +02:00
|
|
|
|
2014-12-01 04:51:07 +01:00
|
|
|
.no_section_data:
|
|
|
|
mov ecx, [edx]
|
2012-03-08 09:33:38 +01:00
|
|
|
cmp ecx, eax
|
2014-12-01 04:51:07 +01:00
|
|
|
jbe .no_section_fill
|
2012-03-08 09:33:38 +01:00
|
|
|
sub ecx, eax
|
2014-12-01 04:51:07 +01:00
|
|
|
add eax, [edx+4]
|
2012-03-08 09:33:38 +01:00
|
|
|
lea edi, [eax+ebp]
|
2010-10-01 11:21:55 +02:00
|
|
|
|
2012-03-08 09:33:38 +01:00
|
|
|
xor eax, eax
|
|
|
|
rep stosb
|
2010-10-01 11:21:55 +02:00
|
|
|
|
2014-12-01 04:51:07 +01:00
|
|
|
.no_section_fill:
|
|
|
|
add edx, [.section_header_size]
|
|
|
|
dec [.numsections]
|
|
|
|
jnz .copy_sections
|
|
|
|
.nosections:
|
|
|
|
cmp [.relocs_size], 0
|
|
|
|
je .no_relocations
|
2012-03-08 09:33:38 +01:00
|
|
|
mov esi, ebp
|
|
|
|
mov ecx, ebp
|
2014-12-01 04:51:07 +01: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
|
2012-03-08 09:33:38 +01:00
|
|
|
shr ebx, 1
|
2014-12-01 04:51:07 +01:00
|
|
|
jz .relocs_next_block
|
|
|
|
.one_reloc:
|
|
|
|
movzx eax, word [ecx]
|
|
|
|
add ecx, 2
|
2012-03-08 09:33:38 +01:00
|
|
|
mov edx, eax
|
|
|
|
shr eax, 12
|
|
|
|
and edx, 4095
|
2014-12-01 04:51:07 +01: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]
|
2014-01-01 22:16:27 +01:00
|
|
|
test edx, edx
|
|
|
|
jnz @f
|
|
|
|
mov edx, ecx
|
|
|
|
@@:
|
2014-12-01 04:51:07 +01:00
|
|
|
mov [.import_idx], 0
|
2012-03-08 09:33:38 +01:00
|
|
|
add ecx, ebp
|
|
|
|
add edx, ebp
|
2014-12-01 04:51:07 +01:00
|
|
|
mov [.import_names], edx
|
|
|
|
mov [.import_targets], ecx
|
|
|
|
.import_func:
|
|
|
|
mov esi, [.import_idx]
|
|
|
|
mov edi, [.import_names]
|
|
|
|
mov eax, [edi+esi*4]
|
2012-03-08 09:33:38 +01:00
|
|
|
test eax, eax
|
2014-12-01 04:51:07 +01:00
|
|
|
je .next_import_block
|
|
|
|
js .next_import_block
|
2012-03-08 09:33:38 +01:00
|
|
|
lea edi, [ebp+eax]
|
2014-12-01 04:51:07 +01:00
|
|
|
mov eax, [.import_targets]
|
|
|
|
mov dword [eax+esi*4], 0
|
2012-03-08 09:33:38 +01:00
|
|
|
lea esi, [edi+2]
|
2014-12-01 04:51:07 +01:00
|
|
|
movzx ebx, word [edi]
|
2012-03-08 09:33:38 +01:00
|
|
|
push 32
|
2014-12-01 04:51:07 +01:00
|
|
|
mov ecx, [__exports+32]
|
|
|
|
mov eax, [ecx+OS_BASE+ebx*4]
|
2010-10-01 11:21:55 +02:00
|
|
|
add eax, OS_BASE
|
2012-03-08 09:33:38 +01:00
|
|
|
push eax
|
|
|
|
push esi
|
|
|
|
call strncmp
|
|
|
|
test eax, eax
|
2014-12-01 04:51:07 +01:00
|
|
|
jz .import_func_found
|
|
|
|
xor ebx, ebx
|
|
|
|
.import_func_candidate:
|
2012-03-08 09:33:38 +01:00
|
|
|
push 32
|
2014-12-01 04:51:07 +01:00
|
|
|
mov ecx, [__exports+32]
|
|
|
|
mov eax, [ecx+OS_BASE+ebx*4]
|
2010-10-01 11:21:55 +02:00
|
|
|
add eax, OS_BASE
|
2012-03-08 09:33:38 +01:00
|
|
|
push eax
|
|
|
|
push esi
|
|
|
|
call strncmp
|
|
|
|
test eax, eax
|
2014-12-01 04:51:07 +01:00
|
|
|
je .import_func_found
|
2012-03-08 09:33:38 +01:00
|
|
|
inc ebx
|
2014-12-01 04:51:07 +01:00
|
|
|
cmp ebx, [__exports+24]
|
|
|
|
jb .import_func_candidate
|
2010-10-01 11:21:55 +02:00
|
|
|
|
|
|
|
mov esi, msg_unresolved
|
|
|
|
call sys_msg_board_str
|
2012-03-08 09:33:38 +01:00
|
|
|
lea esi, [edi+2]
|
2010-10-01 11:21:55 +02:00
|
|
|
call sys_msg_board_str
|
|
|
|
mov esi, msg_CR
|
|
|
|
call sys_msg_board_str
|
|
|
|
|
2014-12-01 04:51:07 +01: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]
|
2010-10-01 11:21:55 +02:00
|
|
|
add eax, OS_BASE
|
2014-12-01 04:51:07 +01: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:
|
2012-03-08 09:33:38 +01:00
|
|
|
xor eax, eax
|
2014-12-01 04:51:07 +01:00
|
|
|
cmp [.bad_import], 0
|
|
|
|
jne @f
|
|
|
|
.no_imports:
|
2012-03-08 09:33:38 +01:00
|
|
|
mov eax, ebp
|
2014-12-01 04:51:07 +01:00
|
|
|
add eax, [.AddressOfEntryPoint]
|
|
|
|
@@:
|
|
|
|
add esp, .locals_size
|
2012-03-08 09:33:38 +01:00
|
|
|
pop esi
|
|
|
|
pop edi
|
2014-12-01 04:51:07 +01:00
|
|
|
ret
|