Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
750 victor 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
2465 Serge 3
;; Copyright (C) KolibriOS team 2004-2011. All rights reserved. ;;
750 victor 4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
488 spraid 8
;============================================================================
9
;
530 mikedld 10
;   External kernel dependencies (libraries) loading
488 spraid 11
;
12
;============================================================================
593 mikedld 13
 
14
$Revision: 2465 $
15
 
1289 diamond 16
if 0
17
; The code currently does not work. Kill "if 0/end if" only after correcting
18
; to current kernel (dll.inc).
488 spraid 19
macro library [name,fname]
20
{
21
  forward
22
    dd __#name#_library_table__,__#name#_library_name__
23
  common
24
    dd 0
25
  forward
26
    __#name#_library_name__ db fname,0
27
}
28
 
29
macro import lname,[name,sname]
30
{
31
  common
32
    align 4
33
    __#lname#_library_table__:
34
  forward
35
    name dd __#name#_import_name__
36
  common
37
    dd 0
38
  forward
39
    __#name#_import_name__ db sname,0
40
}
41
 
42
macro export [name,sname]
43
{
521 diamond 44
align 4
488 spraid 45
  forward
46
    dd __#name#_export_name__,name
47
  common
48
    dd 0
49
  forward
50
    __#name#_export_name__ db sname,0
51
}
52
 
53
 
54
 
535 spraid 55
align 4            ; loading library (use kernel functions)
488 spraid 56
proc load_k_library stdcall, file_name:dword
57
           locals
58
             coff      dd ?
59
             sym       dd ?
60
             strings   dd ?
61
             img_size  dd ?
62
             img_base  dd ?
63
             exports   dd ?
64
           endl
519 serge 65
 
2434 Serge 66
        cli
519 serge 67
 
2434 Serge 68
        stdcall load_file, [file_name]
69
        test    eax, eax
70
        jz      .fail
488 spraid 71
 
2434 Serge 72
        mov     [coff], eax
73
        movzx   ecx, [eax+CFH.nSections]
74
        xor     ebx, ebx
488 spraid 75
 
2434 Serge 76
        lea     edx, [eax+20]
488 spraid 77
@@:
2434 Serge 78
        add     ebx, [edx+CFS.SizeOfRawData]
79
        add     ebx, 15
80
        and     ebx, not 15
81
        add     edx, COFF_SECTION_SIZE
82
        dec     ecx
83
        jnz     @B
84
        mov     [img_size], ebx
488 spraid 85
 
2434 Serge 86
        stdcall kernel_alloc, [img_size]
488 spraid 87
 
2434 Serge 88
        test    eax, eax
89
        jz      .fail
90
        mov     [img_base], eax
488 spraid 91
 
2434 Serge 92
        mov     edx, [coff]
93
        movzx   ebx, [edx+CFH.nSections]
94
        mov     edi, [img_base]
95
        lea     eax, [edx+20]
488 spraid 96
@@:
2434 Serge 97
        mov     [eax+CFS.VirtualAddress], edi
98
        mov     esi, [eax+CFS.PtrRawData]
99
        test    esi, esi
100
        jnz     .copy
101
        add     edi, [eax+CFS.SizeOfRawData]
102
        jmp     .next
488 spraid 103
.copy:
2434 Serge 104
        add     esi, edx
105
        mov     ecx, [eax+CFS.SizeOfRawData]
106
        cld
107
        rep movsb
488 spraid 108
.next:
2434 Serge 109
        add     edi, 15
110
        and     edi, not 15
111
        add     eax, COFF_SECTION_SIZE
112
        dec     ebx
113
        jnz     @B
488 spraid 114
 
2434 Serge 115
        mov     ebx, [edx+CFH.pSymTable]
116
        add     ebx, edx
117
        mov     [sym], ebx
118
        mov     ecx, [edx+CFH.nSymbols]
119
        add     ecx, ecx
120
        lea     ecx, [ecx+ecx*8];ecx*=18 = nSymbols*CSYM_SIZE
121
        add     ecx, [sym]
122
        mov     [strings], ecx
488 spraid 123
 
2434 Serge 124
        lea     eax, [edx+20]
519 serge 125
 
2434 Serge 126
        stdcall fix_coff_symbols, eax, [sym], [edx+CFH.nSymbols], \
127
                [strings], dword 0
128
        test    eax, eax
129
        jnz     @F
488 spraid 130
 
131
@@:
2434 Serge 132
        mov     edx, [coff]
133
        movzx   ebx, [edx+CFH.nSections]
134
        mov     edi, 0
135
        lea     eax, [edx+20]
488 spraid 136
@@:
2434 Serge 137
        add     [eax+CFS.VirtualAddress], edi ;patch user space offset
138
        add     eax, COFF_SECTION_SIZE
139
        dec     ebx
140
        jnz     @B
488 spraid 141
 
2434 Serge 142
        add     edx, 20
143
        stdcall fix_coff_relocs, [coff], edx, [sym]
488 spraid 144
 
2434 Serge 145
        mov     ebx, [coff]
146
        stdcall get_coff_sym, [sym], [ebx+CFH.nSymbols], szEXPORTS
147
        mov     [exports], eax
488 spraid 148
 
2434 Serge 149
        stdcall kernel_free, [coff]
488 spraid 150
 
2434 Serge 151
        mov     eax, [exports]
152
        ret
488 spraid 153
.fail:
2434 Serge 154
        xor     eax, eax
155
        ret
488 spraid 156
endp
157
 
158
 
159
proc dll.Load, import_table:dword
2434 Serge 160
        mov     esi, [import_table]
161
  .next_lib:
162
        mov     edx, [esi]
163
        or      edx, edx
164
        jz      .exit
165
        push    esi
488 spraid 166
 
2434 Serge 167
        mov     edi, s_libname
519 serge 168
 
2434 Serge 169
        mov     al, '/'
170
        stosb
171
        mov     esi, sysdir_path
172
            @@:
173
        lodsb
174
        stosb
175
        or      al, al
176
        jnz     @b
177
        dec     edi
178
        mov     [edi], dword '/lib'
179
        mov     [edi+4], byte '/'
180
        add     edi, 5
181
        pop     esi
182
        push    esi
183
        mov     esi, [esi+4]
184
            @@:
185
        lodsb
186
        stosb
187
        or      al, al
188
        jnz     @b
488 spraid 189
 
2434 Serge 190
        pushad
191
        stdcall load_k_library, s_libname
192
        mov     [esp+28], eax
193
        popad
194
        or      eax, eax
195
        jz      .fail
196
        stdcall dll.Link, eax, edx
197
        stdcall dll.Init, [eax+4]
198
        pop     esi
199
        add     esi, 8
200
        jmp     .next_lib
201
  .exit:
202
        xor     eax, eax
203
        ret
204
  .fail:
205
        add     esp, 4
206
        xor     eax, eax
207
        inc     eax
208
        ret
488 spraid 209
endp
210
 
211
proc dll.Link, exp:dword,imp:dword
2434 Serge 212
        push    eax
213
        mov     esi, [imp]
214
        test    esi, esi
215
        jz      .done
216
  .next:
217
        lodsd
218
        test    eax, eax
219
        jz      .done
220
        stdcall dll.GetProcAddress, [exp], eax
221
        or      eax, eax
222
        jz      @f
223
        mov     [esi-4], eax
224
        jmp     .next
225
            @@:
226
        mov     dword[esp], 0
227
  .done:
228
        pop     eax
229
        ret
488 spraid 230
endp
231
 
232
proc dll.Init, dllentry:dword
2434 Serge 233
        pushad
234
        mov     eax, mem.Alloc
235
        mov     ebx, mem.Free
236
        mov     ecx, mem.ReAlloc
237
        mov     edx, dll.Load
238
        stdcall [dllentry]
239
        popad
240
        ret
488 spraid 241
endp
242
 
243
proc dll.GetProcAddress, exp:dword,sz_name:dword
2434 Serge 244
        mov     edx, [exp]
245
  .next:
246
        test    edx, edx
247
        jz      .end
248
        stdcall strncmp, [edx], [sz_name], dword -1
249
        test    eax, eax
250
        jz      .ok
251
        add     edx, 8
252
        jmp     .next
253
  .ok:
254
        mov     eax, [edx+4]
255
  .end:
256
        ret
488 spraid 257
endp
258
 
259
;-----------------------------------------------------------------------------
260
proc mem.Alloc size ;/////////////////////////////////////////////////////////
261
;-----------------------------------------------------------------------------
2434 Serge 262
        push    ebx ecx
263
;       mov     eax,[size]
264
;       lea     ecx,[eax+4+4095]
265
;       and     ecx,not 4095
266
;       stdcall kernel_alloc, ecx
267
;       add     ecx,-4
268
;       mov     [eax],ecx
269
;       add     eax,4
488 spraid 270
 
2434 Serge 271
        stdcall kernel_alloc, [size]
488 spraid 272
 
2434 Serge 273
        pop     ecx ebx
274
        ret
488 spraid 275
endp
276
 
277
;-----------------------------------------------------------------------------
278
proc mem.ReAlloc mptr,size;///////////////////////////////////////////////////
279
;-----------------------------------------------------------------------------
2434 Serge 280
        push    ebx ecx esi edi eax
281
        mov     eax, [mptr]
282
        mov     ebx, [size]
283
        or      eax, eax
284
        jz      @f
285
        lea     ecx, [ebx+4+4095]
286
        and     ecx, not 4095
287
        add     ecx, -4
288
        cmp     ecx, [eax-4]
289
        je      .exit
290
    @@:
291
        mov     eax, ebx
292
        call    mem.Alloc
293
        xchg    eax, [esp]
294
        or      eax, eax
295
        jz      .exit
296
        mov     esi, eax
297
        xchg    eax, [esp]
298
        mov     edi, eax
299
        mov     ecx, [esi-4]
300
        cmp     ecx, [edi-4]
301
        jbe     @f
302
        mov     ecx, [edi-4]
303
    @@:
304
        add     ecx, 3
305
        shr     ecx, 2
306
        cld
307
        rep movsd
308
        xchg    eax, [esp]
309
        call    mem.Free
488 spraid 310
  .exit:
2434 Serge 311
        pop     eax edi esi ecx ebx
312
        ret
488 spraid 313
endp
314
 
315
;-----------------------------------------------------------------------------
316
proc mem.Free mptr ;//////////////////////////////////////////////////////////
317
;-----------------------------------------------------------------------------
2434 Serge 318
;       mov     eax,[mptr]
319
;       or      eax,eax
320
;       jz      @f
321
;       push    ebx ecx
322
;       lea     ecx,[eax-4]
323
;       stdcall kernel_free, ecx
324
;       pop     ecx ebx
488 spraid 325
;    @@: ret
2434 Serge 326
        stdcall kernel_free, [mptr]
327
        ret
488 spraid 328
endp
329
 
521 diamond 330
uglobal
488 spraid 331
s_libname  db 64 dup (0)
521 diamond 332
endg
1289 diamond 333
end if