Subversion Repositories Kolibri OS

Rev

Rev 9406 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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