Subversion Repositories Kolibri OS

Rev

Rev 9406 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2288 clevermous 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
9715 Doczom 3
;; Copyright (C) KolibriOS team 2004-2022. All rights reserved. ;;
2288 clevermous 4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
8
$Revision: 9715 $
9
 
10
include 'export.inc'
11
 
12
align 4
13
 
9406 Doczom 14
; This function load file driver and output
15
;  eax = address function START in driver
16
;  ebx = address for kernel_free
2288 clevermous 17
proc load_PE stdcall, file_name:dword
18
           locals
19
             image  dd ?
20
             entry  dd ?
21
             base   dd ?
22
           endl
23
 
24
        stdcall load_file, [file_name]
25
        test    eax, eax
26
        jz      .fail
27
 
28
        mov     [image], eax
29
 
9715 Doczom 30
        mov     edx, [eax + STRIPPED_PE_HEADER.SizeOfImage]
31
;        mov     cl, [eax + STRIPPED_PE_HEADER.Subsystem]
5039 clevermous 32
        cmp     word [eax], STRIPPED_PE_SIGNATURE
33
        jz      @f
34
 
9715 Doczom 35
        mov     edx, [eax + 60]
36
;        mov     cl, [eax + 5Ch + edx]
37
        mov     edx, [eax + 80 + edx]
2288 clevermous 38
 
5039 clevermous 39
@@:
40
        mov     [entry], 0
41
;        cmp     cl, 1
42
;        jnz     .cleanup
43
        stdcall kernel_alloc, edx
2288 clevermous 44
        test    eax, eax
45
        jz      .cleanup
46
 
47
        mov     [base], eax
5559 clevermous 48
        DEBUGF 1,'K : driver %s mapped to %x\n',[file_name],[base]
2288 clevermous 49
 
5039 clevermous 50
        push    ebx ebp
51
        mov     ebx, [image]
52
        mov     ebp, eax
53
        call    map_PE
54
        pop     ebp ebx
2288 clevermous 55
 
56
        mov     [entry], eax
57
        test    eax, eax
58
        jnz     .cleanup
59
 
60
        stdcall kernel_free, [base]
61
.cleanup:
62
        stdcall kernel_free, [image]
63
        mov     eax, [entry]
9406 Doczom 64
        mov     ebx, [base]
2288 clevermous 65
        ret
66
.fail:
67
        xor     eax, eax
68
        ret
69
endp
70
 
5039 clevermous 71
map_PE:                    ;ebp=base:dword, ebx=image:dword
2288 clevermous 72
        push    edi
73
        push    esi
5039 clevermous 74
        sub     esp, .locals_size
75
virtual at esp
76
.numsections    dd      ?
77
.import_names   dd      ?
78
.import_targets dd      ?
79
.peheader       dd      ?
80
.bad_import     dd      ?
81
.import_idx     dd      ?
82
.import_descr   dd      ?
83
.relocs_rva     dd      ?
84
.relocs_size    dd      ?
85
.section_header_size dd ?
86
.AddressOfEntryPoint    dd ?
87
.ImageBase      dd      ?
88
.locals_size = $ - esp
89
end virtual
90
        cmp     word [ebx], STRIPPED_PE_SIGNATURE
91
        jz      .stripped
92
 
2288 clevermous 93
        mov     edx, ebx
9715 Doczom 94
        add     edx, [ebx + 60]
95
        movzx   eax, word [edx + 6]
5039 clevermous 96
        mov     [.numsections], eax
9715 Doczom 97
        mov     eax, [edx + 40]
5039 clevermous 98
        mov     [.AddressOfEntryPoint], eax
9715 Doczom 99
        mov     eax, [edx + 52]
5039 clevermous 100
        mov     [.ImageBase], eax
9715 Doczom 101
        mov     ecx, [edx + 84]
5039 clevermous 102
        mov     [.section_header_size], 40
9715 Doczom 103
        mov     eax, [edx + 128]
5039 clevermous 104
        mov     [.import_descr], eax
9715 Doczom 105
        mov     eax, [edx + 160]
5039 clevermous 106
        mov     [.relocs_rva], eax
9715 Doczom 107
        mov     eax, [edx + 164]
5039 clevermous 108
        mov     [.relocs_size], eax
109
        add     edx, 256
110
 
111
        jmp     .common
112
.stripped:
9715 Doczom 113
        mov     eax, [ebx + STRIPPED_PE_HEADER.AddressOfEntryPoint]
5039 clevermous 114
        mov     [.AddressOfEntryPoint], eax
9715 Doczom 115
        mov     eax, [ebx + STRIPPED_PE_HEADER.ImageBase]
5039 clevermous 116
        mov     [.ImageBase], eax
9715 Doczom 117
        movzx   eax, [ebx + STRIPPED_PE_HEADER.NumberOfSections]
5039 clevermous 118
        mov     [.numsections], eax
9715 Doczom 119
        movzx   ecx, [ebx + STRIPPED_PE_HEADER.NumberOfRvaAndSizes]
5039 clevermous 120
        xor     eax, eax
121
        mov     [.relocs_rva], eax
122
        mov     [.relocs_size], eax
123
        test    ecx, ecx
124
        jz      @f
9715 Doczom 125
        mov     eax, [ebx + sizeof.STRIPPED_PE_HEADER + SPE_DIRECTORY_IMPORT*8]
5039 clevermous 126
@@:
127
        mov     [.import_descr], eax
128
        cmp     ecx, SPE_DIRECTORY_BASERELOC
129
        jbe     @f
9715 Doczom 130
        mov     eax, [ebx + sizeof.STRIPPED_PE_HEADER + SPE_DIRECTORY_BASERELOC*8]
5039 clevermous 131
        mov     [.relocs_rva], eax
9715 Doczom 132
        mov     eax, [ebx + sizeof.STRIPPED_PE_HEADER + SPE_DIRECTORY_BASERELOC*8+4]
5039 clevermous 133
        mov     [.relocs_size], eax
134
@@:
135
        mov     [.section_header_size], 28
9715 Doczom 136
        lea     edx, [ebx + ecx*8 + sizeof.STRIPPED_PE_HEADER + 8]
137
        mov     ecx, [ebx + STRIPPED_PE_HEADER.SizeOfHeaders]
5039 clevermous 138
 
139
.common:
2288 clevermous 140
        mov     esi, ebx
141
        mov     edi, ebp
142
        shr     ecx, 2
143
        rep movsd
144
 
5039 clevermous 145
        cmp     [.numsections], 0
146
        jz      .nosections
147
.copy_sections:
9715 Doczom 148
        mov     eax, [edx + 8]
2288 clevermous 149
        test    eax, eax
5039 clevermous 150
        je      .no_section_data
2288 clevermous 151
        mov     esi, ebx
152
        mov     edi, ebp
9715 Doczom 153
        add     esi, [edx + 12]
2288 clevermous 154
        mov     ecx, eax
9715 Doczom 155
        add     edi, [edx + 4]
2288 clevermous 156
 
4418 clevermous 157
        add     ecx, 3
2288 clevermous 158
        shr     ecx, 2
159
        rep movsd
160
 
5039 clevermous 161
.no_section_data:
162
        mov     ecx, [edx]
2288 clevermous 163
        cmp     ecx, eax
5039 clevermous 164
        jbe     .no_section_fill
2288 clevermous 165
        sub     ecx, eax
9715 Doczom 166
        add     eax, [edx + 4]
2288 clevermous 167
        lea     edi, [eax+ebp]
168
 
169
        xor     eax, eax
170
        rep stosb
171
 
5039 clevermous 172
.no_section_fill:
173
        add     edx, [.section_header_size]
174
        dec     [.numsections]
175
        jnz     .copy_sections
176
.nosections:
177
        cmp     [.relocs_size], 0
178
        je      .no_relocations
2288 clevermous 179
        mov     esi, ebp
180
        mov     ecx, ebp
5039 clevermous 181
        sub     esi, [.ImageBase]
182
        add     ecx, [.relocs_rva]
183
.relocs_block:
184
        mov     edi, [ecx]
185
        add     edi, ebp
9715 Doczom 186
        mov     ebx, [ecx + 4]
5039 clevermous 187
        add     ecx, 8
188
        sub     [.relocs_size], ebx
189
        sub     ebx, 8
2288 clevermous 190
        shr     ebx, 1
5039 clevermous 191
        jz      .relocs_next_block
192
.one_reloc:
193
        movzx   eax, word [ecx]
194
        add     ecx, 2
2288 clevermous 195
        mov     edx, eax
196
        shr     eax, 12
197
        and     edx, 4095
5039 clevermous 198
        cmp     eax, 3
199
        jne     @f
200
        add     [edx+edi], esi
201
@@:
202
        dec     ebx
203
        jnz     .one_reloc
204
.relocs_next_block:
205
        cmp     [.relocs_size], 0
206
        jg      .relocs_block
207
.no_relocations:
208
        cmp     [.import_descr], 0
209
        je      .no_imports
210
        add     [.import_descr], ebp
211
        mov     [.bad_import], 0
212
.import_block:
213
        mov     ecx, [.import_descr]
9715 Doczom 214
        cmp     dword [ecx + 4], 0
5039 clevermous 215
        jne     @f
9715 Doczom 216
        cmp     dword [ecx + 12], 0
5039 clevermous 217
        je      .done_imports
218
@@:
219
        mov     edx, dword [ecx]
9715 Doczom 220
        mov     ecx, dword [ecx + 16]
4418 clevermous 221
        test    edx, edx
222
        jnz     @f
223
        mov     edx, ecx
224
@@:
5039 clevermous 225
        mov     [.import_idx], 0
2288 clevermous 226
        add     ecx, ebp
227
        add     edx, ebp
5039 clevermous 228
        mov     [.import_names], edx
229
        mov     [.import_targets], ecx
230
.import_func:
231
        mov     esi, [.import_idx]
232
        mov     edi, [.import_names]
9715 Doczom 233
        mov     eax, [edi + esi*4]
2288 clevermous 234
        test    eax, eax
5039 clevermous 235
        je      .next_import_block
236
        js      .next_import_block
9715 Doczom 237
        lea     edi, [ebp + eax]
5039 clevermous 238
        mov     eax, [.import_targets]
9715 Doczom 239
        mov     dword [eax + esi*4], 0
240
        lea     esi, [edi + 2]
5039 clevermous 241
        movzx   ebx, word [edi]
2288 clevermous 242
        push    32
9715 Doczom 243
        mov     ecx, [__exports + 32]
244
        mov     eax, [ecx + OS_BASE + ebx*4]
2288 clevermous 245
        add     eax, OS_BASE
246
        push    eax
247
        push    esi
248
        call    strncmp
5039 clevermous 249
        test    eax, eax
250
        jz      .import_func_found
2288 clevermous 251
        xor     ebx, ebx
5039 clevermous 252
.import_func_candidate:
2288 clevermous 253
        push    32
9715 Doczom 254
        mov     ecx, [__exports + 32]
255
        mov     eax, [ecx + OS_BASE + ebx*4]
2288 clevermous 256
        add     eax, OS_BASE
257
        push    eax
258
        push    esi
259
        call    strncmp
260
        test    eax, eax
5039 clevermous 261
        je      .import_func_found
2288 clevermous 262
        inc     ebx
9715 Doczom 263
        cmp     ebx, [__exports + 24]
5039 clevermous 264
        jb      .import_func_candidate
2288 clevermous 265
 
266
        mov     esi, msg_unresolved
267
        call    sys_msg_board_str
9715 Doczom 268
        lea     esi, [edi + 2]
2288 clevermous 269
        call    sys_msg_board_str
270
        mov     esi, msg_CR
271
        call    sys_msg_board_str
272
 
5039 clevermous 273
        mov     [.bad_import], 1
274
        jmp     .next_import_func
275
.import_func_found:
9715 Doczom 276
        mov     esi, [__exports + 28]
5039 clevermous 277
        mov     edx, [.import_idx]
278
        mov     ecx, [.import_targets]
9715 Doczom 279
        mov     eax, [esi + OS_BASE + ebx*4]
2288 clevermous 280
        add     eax, OS_BASE
9715 Doczom 281
        mov     [ecx + edx*4], eax
5039 clevermous 282
.next_import_func:
283
        inc     [.import_idx]
284
        jmp     .import_func
285
.next_import_block:
286
        add     [.import_descr], 20
287
        jmp     .import_block
288
.done_imports:
2288 clevermous 289
        xor     eax, eax
5039 clevermous 290
        cmp     [.bad_import], 0
291
        jne     @f
292
.no_imports:
2288 clevermous 293
        mov     eax, ebp
5039 clevermous 294
        add     eax, [.AddressOfEntryPoint]
295
@@:
296
        add     esp, .locals_size
2288 clevermous 297
        pop     esi
298
        pop     edi
5039 clevermous 299
        ret