Subversion Repositories Kolibri OS

Rev

Rev 5363 | Rev 9715 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2288 clevermous 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
5363 yogev_ezra 3
;; Copyright (C) KolibriOS team 2004-2015. All rights reserved. ;;
2288 clevermous 4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
6502 pathoswith 8
; External kernel dependencies (libraries) loading.
9
; The code currently does not work, requires correcting dll.inc.
2288 clevermous 10
 
11
$Revision: 6502 $
12
 
13
if 0
6502 pathoswith 14
iglobal
15
tmp_file_name_size  dd  1
16
endg
17
 
18
uglobal
19
tmp_file_name_table dd  ?
20
s_libname   rb  64
21
def_val_1   db  ?
22
endg
23
 
2288 clevermous 24
macro library [name,fname]
25
{
26
  forward
27
    dd __#name#_library_table__,__#name#_library_name__
28
  common
29
    dd 0
30
  forward
31
    __#name#_library_name__ db fname,0
32
}
33
 
34
macro import lname,[name,sname]
35
{
36
  common
37
    align 4
38
    __#lname#_library_table__:
39
  forward
40
    name dd __#name#_import_name__
41
  common
42
    dd 0
43
  forward
44
    __#name#_import_name__ db sname,0
45
}
46
 
47
macro export [name,sname]
48
{
49
align 4
50
  forward
51
    dd __#name#_export_name__,name
52
  common
53
    dd 0
54
  forward
55
    __#name#_export_name__ db sname,0
56
}
57
 
58
 
59
 
60
align 4            ; loading library (use kernel functions)
61
proc load_k_library stdcall, file_name:dword
62
           locals
63
             coff      dd ?
64
             sym       dd ?
65
             strings   dd ?
66
             img_size  dd ?
67
             img_base  dd ?
68
             exports   dd ?
69
           endl
70
 
71
        cli
72
 
73
        stdcall load_file, [file_name]
74
        test    eax, eax
75
        jz      .fail
76
 
77
        mov     [coff], eax
78
        movzx   ecx, [eax+CFH.nSections]
79
        xor     ebx, ebx
80
 
81
        lea     edx, [eax+20]
82
@@:
83
        add     ebx, [edx+CFS.SizeOfRawData]
84
        add     ebx, 15
85
        and     ebx, not 15
86
        add     edx, COFF_SECTION_SIZE
87
        dec     ecx
88
        jnz     @B
89
        mov     [img_size], ebx
90
 
91
        stdcall kernel_alloc, [img_size]
92
 
93
        test    eax, eax
94
        jz      .fail
95
        mov     [img_base], eax
96
 
97
        mov     edx, [coff]
98
        movzx   ebx, [edx+CFH.nSections]
99
        mov     edi, [img_base]
100
        lea     eax, [edx+20]
101
@@:
102
        mov     [eax+CFS.VirtualAddress], edi
103
        mov     esi, [eax+CFS.PtrRawData]
104
        test    esi, esi
105
        jnz     .copy
106
        add     edi, [eax+CFS.SizeOfRawData]
107
        jmp     .next
108
.copy:
109
        add     esi, edx
110
        mov     ecx, [eax+CFS.SizeOfRawData]
111
        cld
112
        rep movsb
113
.next:
114
        add     edi, 15
115
        and     edi, not 15
116
        add     eax, COFF_SECTION_SIZE
117
        dec     ebx
118
        jnz     @B
119
 
120
        mov     ebx, [edx+CFH.pSymTable]
121
        add     ebx, edx
122
        mov     [sym], ebx
123
        mov     ecx, [edx+CFH.nSymbols]
124
        add     ecx, ecx
125
        lea     ecx, [ecx+ecx*8];ecx*=18 = nSymbols*CSYM_SIZE
126
        add     ecx, [sym]
127
        mov     [strings], ecx
128
 
129
        lea     eax, [edx+20]
130
 
131
        stdcall fix_coff_symbols, eax, [sym], [edx+CFH.nSymbols], \
132
                [strings], dword 0
133
        test    eax, eax
134
        jnz     @F
135
 
136
@@:
137
        mov     edx, [coff]
138
        movzx   ebx, [edx+CFH.nSections]
139
        mov     edi, 0
140
        lea     eax, [edx+20]
141
@@:
142
        add     [eax+CFS.VirtualAddress], edi ;patch user space offset
143
        add     eax, COFF_SECTION_SIZE
144
        dec     ebx
145
        jnz     @B
146
 
147
        add     edx, 20
148
        stdcall fix_coff_relocs, [coff], edx, [sym]
149
 
150
        mov     ebx, [coff]
151
        stdcall get_coff_sym, [sym], [ebx+CFH.nSymbols], szEXPORTS
152
        mov     [exports], eax
153
 
154
        stdcall kernel_free, [coff]
155
 
156
        mov     eax, [exports]
157
        ret
158
.fail:
159
        xor     eax, eax
160
        ret
161
endp
162
 
163
 
164
proc dll.Load, import_table:dword
165
        mov     esi, [import_table]
166
  .next_lib:
167
        mov     edx, [esi]
168
        or      edx, edx
169
        jz      .exit
170
        push    esi
171
 
172
        mov     edi, s_libname
173
 
174
        mov     al, '/'
175
        stosb
176
        mov     esi, sysdir_path
177
            @@:
178
        lodsb
179
        stosb
180
        or      al, al
181
        jnz     @b
182
        dec     edi
183
        mov     [edi], dword '/lib'
184
        mov     [edi+4], byte '/'
185
        add     edi, 5
186
        pop     esi
187
        push    esi
188
        mov     esi, [esi+4]
189
            @@:
190
        lodsb
191
        stosb
192
        or      al, al
193
        jnz     @b
194
 
195
        pushad
196
        stdcall load_k_library, s_libname
197
        mov     [esp+28], eax
198
        popad
199
        or      eax, eax
200
        jz      .fail
201
        stdcall dll.Link, eax, edx
202
        stdcall dll.Init, [eax+4]
203
        pop     esi
204
        add     esi, 8
205
        jmp     .next_lib
206
  .exit:
207
        xor     eax, eax
208
        ret
209
  .fail:
210
        add     esp, 4
211
        xor     eax, eax
212
        inc     eax
213
        ret
214
endp
215
 
216
proc dll.Link, exp:dword,imp:dword
217
        push    eax
218
        mov     esi, [imp]
219
        test    esi, esi
220
        jz      .done
221
  .next:
222
        lodsd
223
        test    eax, eax
224
        jz      .done
225
        stdcall dll.GetProcAddress, [exp], eax
226
        or      eax, eax
227
        jz      @f
228
        mov     [esi-4], eax
229
        jmp     .next
230
            @@:
231
        mov     dword[esp], 0
232
  .done:
233
        pop     eax
234
        ret
235
endp
236
 
237
proc dll.Init, dllentry:dword
238
        pushad
239
        mov     eax, mem.Alloc
240
        mov     ebx, mem.Free
241
        mov     ecx, mem.ReAlloc
242
        mov     edx, dll.Load
243
        stdcall [dllentry]
244
        popad
245
        ret
246
endp
247
 
248
proc dll.GetProcAddress, exp:dword,sz_name:dword
249
        mov     edx, [exp]
250
  .next:
251
        test    edx, edx
252
        jz      .end
253
        stdcall strncmp, [edx], [sz_name], dword -1
254
        test    eax, eax
255
        jz      .ok
256
        add     edx, 8
257
        jmp     .next
258
  .ok:
259
        mov     eax, [edx+4]
260
  .end:
261
        ret
262
endp
263
 
264
;-----------------------------------------------------------------------------
265
proc mem.Alloc size ;/////////////////////////////////////////////////////////
266
;-----------------------------------------------------------------------------
267
        push    ebx ecx
268
;       mov     eax,[size]
269
;       lea     ecx,[eax+4+4095]
270
;       and     ecx,not 4095
271
;       stdcall kernel_alloc, ecx
272
;       add     ecx,-4
273
;       mov     [eax],ecx
274
;       add     eax,4
275
 
276
        stdcall kernel_alloc, [size]
277
 
278
        pop     ecx ebx
279
        ret
280
endp
281
 
282
;-----------------------------------------------------------------------------
283
proc mem.ReAlloc mptr,size;///////////////////////////////////////////////////
284
;-----------------------------------------------------------------------------
285
        push    ebx ecx esi edi eax
286
        mov     eax, [mptr]
287
        mov     ebx, [size]
288
        or      eax, eax
289
        jz      @f
290
        lea     ecx, [ebx+4+4095]
291
        and     ecx, not 4095
292
        add     ecx, -4
293
        cmp     ecx, [eax-4]
294
        je      .exit
295
    @@:
296
        mov     eax, ebx
297
        call    mem.Alloc
298
        xchg    eax, [esp]
299
        or      eax, eax
300
        jz      .exit
301
        mov     esi, eax
302
        xchg    eax, [esp]
303
        mov     edi, eax
304
        mov     ecx, [esi-4]
305
        cmp     ecx, [edi-4]
306
        jbe     @f
307
        mov     ecx, [edi-4]
308
    @@:
309
        add     ecx, 3
310
        shr     ecx, 2
311
        cld
312
        rep movsd
313
        xchg    eax, [esp]
314
        call    mem.Free
315
  .exit:
316
        pop     eax edi esi ecx ebx
317
        ret
318
endp
319
 
320
;-----------------------------------------------------------------------------
321
proc mem.Free mptr ;//////////////////////////////////////////////////////////
322
;-----------------------------------------------------------------------------
323
;       mov     eax,[mptr]
324
;       or      eax,eax
325
;       jz      @f
326
;       push    ebx ecx
327
;       lea     ecx,[eax-4]
328
;       stdcall kernel_free, ecx
329
;       pop     ecx ebx
330
;    @@: ret
331
        stdcall kernel_free, [mptr]
332
        ret
333
endp
334
 
6502 pathoswith 335
proc load_file_parse_table
336
        stdcall kernel_alloc, 0x1000
337
        mov     [tmp_file_name_table], eax
338
        mov     edi, eax
339
        mov     esi, sysdir_name
340
        mov     ecx, 128/4
341
        rep movsd
342
        invoke  ini.enum_keys, conf_fname, conf_path_sect, get_every_key
343
        mov     eax, [tmp_file_name_table]
344
        mov     [full_file_name_table], eax
345
        mov     eax, [tmp_file_name_size]
346
        mov     [full_file_name_table.size], eax
347
        ret
348
endp
349
 
350
proc get_every_key stdcall, f_name, sec_name, key_name
351
        mov     esi, [key_name]
352
        mov     ecx, esi
353
        cmp     byte [esi], '/'
354
        jnz     @f
355
        inc     esi
356
@@:
357
        mov     edi, [tmp_file_name_size]
358
        shl     edi, 7
359
        cmp     edi, 0x1000
360
        jae     .stop_parse
361
        add     edi, [tmp_file_name_table]
362
        lea     ebx, [edi+64]
363
@@:
364
        cmp     edi, ebx
365
        jae     .skip_this_key
366
        lodsb
367
        test    al, al
368
        jz      @f
369
        or      al, 20h
370
        stosb
371
        jmp     @b
372
 
373
.stop_parse:
374
        xor     eax, eax
375
        ret
376
 
377
@@:
378
        stosb
379
        invoke  ini.get_str, [f_name], [sec_name], ecx, ebx, 64, def_val_1
380
        cmp     byte [ebx], '/'
381
        jnz     @f
382
        lea     esi, [ebx+1]
383
        mov     edi, ebx
384
        mov     ecx, 63
385
        rep movsb
386
@@:
387
        push    ebp
388
        mov     ebp, [tmp_file_name_table]
389
        mov     ecx, [tmp_file_name_size]
390
        jecxz   .noreplace
391
        mov     eax, ecx
392
        dec     eax
393
        shl     eax, 7
394
        add     ebp, eax
395
.replace_loop:
396
        mov     edi, ebx
397
        mov     esi, ebp
398
@@:
399
        lodsb
400
        test    al, al
401
        jz      .doreplace
402
        mov     dl, [edi]
403
        inc     edi
404
        test    dl, dl
405
        jz      .replace_loop_cont
406
        or      dl, 20h
407
        cmp     al, dl
408
        jz      @b
409
        jmp     .replace_loop_cont
410
 
411
.doreplace:
412
        cmp     byte [edi], 0
413
        jz      @f
414
        cmp     byte [edi], '/'
415
        jnz     .replace_loop_cont
416
@@:
417
        lea     esi, [ebp+64]
418
        call    .replace
419
        jc      .skip_this_key2
420
.replace_loop_cont:
421
        sub     ebp, 128
422
        loop    .replace_loop
423
.noreplace:
424
        pop     ebp
425
        inc     [tmp_file_name_size]
426
.skip_this_key:
427
        xor     eax, eax
428
        inc     eax
429
        ret
430
 
431
.skip_this_key2:
432
        pop     ebp
433
        jmp     .skip_this_key
434
endp
435
 
436
proc get_every_key.replace
437
; in: ebx->destination, esi->first part of name, edi->second part of name
438
; maximum length is 64 bytes
439
; out: CF=1 <=> overflow
440
        sub     esp, 64 ; allocate temporary buffer in stack
441
        push    esi
442
        lea     esi, [esp+4]    ; esi->tmp buffer
443
        xchg    esi, edi        ; edi->tmp buffer, esi->source
444
@@: ; save second part of name to temporary buffer
445
        lodsb
446
        stosb
447
        test    al, al
448
        jnz     @b
449
        pop     esi
450
        mov     edi, ebx
451
@@: ; copy first part of name to destination
452
        lodsb
453
        test    al, al
454
        jz      @f
455
        stosb
456
        jmp     @b
457
 
458
@@: ; restore second part of name from temporary buffer to destination
459
        lea     edx, [ebx+64]   ; limit of destination
460
        mov     esi, esp
461
@@:
462
        cmp     edi, edx
463
        jae     .overflow
464
        lodsb
465
        stosb
466
        test    al, al
467
        jnz     @b
468
        add     esp, 64 ; CF is cleared
469
        ret
470
 
471
.overflow:  ; name is too long
472
        add     esp, 64
473
        stc
474
        ret
475
endp
2288 clevermous 476
end if