Subversion Repositories Kolibri OS

Rev

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

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