Subversion Repositories Kolibri OS

Rev

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

Rev 5940 Rev 6823
1
;-----------------------------------------------------------------------------
1
;-----------------------------------------------------------------------------
-
 
2
; load one or more DLL file in COFF format and try to import functions by our list
-
 
3
; if first function in import list begins with 'lib_', call it as DLL initialization
-
 
4
; return eax = 1 as fail, if anyone of .obj file not found in /sys/lib
-
 
5
; return 0 if all fine, but 0 not garantees in succesfull import - see dll.Link comment
-
 
6
; dirties all registers! eax, ebx, ecx, edx, esi, edi
2
proc dll.Load, import_table:dword
7
proc dll.Load, import_table:dword
3
	mov	esi, [import_table]
8
	mov	esi, [import_table]
4
  .next_lib:
9
  .next_lib:
5
	mov	edx, [esi]
10
	mov	edx, [esi]
6
	or	edx, edx
11
	or	edx, edx
7
	jz	.exit
12
	jz	.exit
8
	push	esi
13
	push	esi
9
	mov	esi, [esi + 4]
14
	mov	esi, [esi + 4]
10
	mov	edi, s_libdir.fname
15
	mov	edi, s_libdir.fname
11
    @@:
16
    @@:
12
	lodsb
17
	lodsb
13
	stosb
18
	stosb
14
	or	al, al
19
	or	al, al
15
	jnz	@b
20
	jnz	@b
16
	mcall	68, 19, s_libdir
21
	mcall	68, 19, s_libdir
17
	or	eax, eax
22
	or	eax, eax
18
	jz	.fail
23
	jz	.fail
19
	stdcall	dll.Link, eax, edx
24
	stdcall	dll.Link, eax, edx
20
	push	eax
25
	push	eax
21
	mov	eax, [eax]
26
	mov	eax, [eax]
22
	cmp	dword[eax], 'lib_'
27
	cmp	dword[eax], 'lib_'
23
	pop	eax
28
	pop	eax
24
	jnz	@f
29
	jnz	@f
25
	stdcall	dll.Init, [eax + 4]
30
	stdcall	dll.Init, [eax + 4]
26
    @@:
31
    @@:
27
	pop	esi
32
	pop	esi
28
	add	esi, 8
33
	add	esi, 8
29
	jmp	.next_lib
34
	jmp	.next_lib
30
  .exit:
35
  .exit:
31
	xor	eax, eax
36
	xor	eax, eax
32
	ret
37
	ret
33
  .fail:
38
  .fail:
34
	add	esp, 4
39
	add	esp, 4
35
	xor	eax, eax
40
	xor	eax, eax
36
	inc	eax
41
	inc	eax
37
	ret
42
	ret
38
endp
43
endp
39
;-----------------------------------------------------------------------------
44
;-----------------------------------------------------------------------------
-
 
45
; scans dll export table for a functions we want to import
-
 
46
; break scan on first unresolved import
-
 
47
; no return value
40
proc dll.Link, exp:dword, imp:dword
48
proc dll.Link, exp:dword, imp:dword
41
	push	eax
49
	push	eax
42
	mov	esi, [imp]
50
	mov	esi, [imp]
43
	test	esi, esi
51
	test	esi, esi
44
	jz	.done
52
	jz	.done
45
  .next:
53
  .next:
46
	lodsd
54
	lodsd
47
	test	eax, eax
55
	test	eax, eax
48
	jz	.done
56
	jz	.done
49
	stdcall	dll.GetProcAddress, [exp], eax
57
	stdcall	dll.GetProcAddress, [exp], eax
50
	or	eax, eax
58
	or	eax, eax
51
	jz	@f
59
	jz	@f
52
	mov	[esi - 4], eax
60
	mov	[esi - 4], eax
53
	jmp	.next
61
	jmp	.next
54
    @@:
62
    @@:
55
	mov	dword[esp], 0
63
	mov	dword[esp], 0
56
  .done:
64
  .done:
57
	pop	eax
65
	pop	eax
58
	ret
66
	ret
59
endp
67
endp
60
;-----------------------------------------------------------------------------
68
;-----------------------------------------------------------------------------
-
 
69
; calls lib_init with predefined parameters  
-
 
70
; no return value
61
proc dll.Init, dllentry:dword
71
proc dll.Init, dllentry:dword
62
	pushad
72
	pushad
63
	mov	eax, mem.Alloc
73
	mov	eax, mem.Alloc
64
	mov	ebx, mem.Free
74
	mov	ebx, mem.Free
65
	mov	ecx, mem.ReAlloc
75
	mov	ecx, mem.ReAlloc
66
	mov	edx, dll.Load
76
	mov	edx, dll.Load
67
	stdcall	[dllentry]
77
	stdcall	[dllentry]
68
	popad
78
	popad
69
	ret
79
	ret
70
endp
80
endp
71
;-----------------------------------------------------------------------------
81
;-----------------------------------------------------------------------------
-
 
82
; scans export table for a sz_name function
-
 
83
; returns in eax function address or 0 if not found
72
proc dll.GetProcAddress, exp:dword, sz_name:dword
84
proc dll.GetProcAddress, exp:dword, sz_name:dword
73
	mov	edx, [exp]
85
	mov	edx, [exp]
74
	xor	eax, eax
86
	xor	eax, eax
75
  .next:
87
  .next:
76
	or	edx, edx
88
	or	edx, edx
77
	jz	.end
89
	jz	.end
78
	cmp	dword[edx], 0
90
	cmp	dword[edx], 0
79
	jz	.end
91
	jz	.end
80
	stdcall	strcmp, [edx], [sz_name]
92
	stdcall	strcmp, [edx], [sz_name]
81
	test	eax, eax
93
	test	eax, eax
82
	jz	.ok
94
	jz	.ok
83
	add	edx, 8
95
	add	edx, 8
84
	jmp	.next
96
	jmp	.next
85
  .ok:
97
  .ok:
86
	mov	eax, [edx + 4]
98
	mov	eax, [edx + 4]
87
  .end:
99
  .end:
-
 
100
	cmp eax, -1
-
 
101
	jnz @f
-
 
102
	xor eax, eax
-
 
103
  @@:
88
	ret
104
	ret
89
endp
105
endp
90
;-----------------------------------------------------------------------------
106
;-----------------------------------------------------------------------------
-
 
107
; compares strings 
-
 
108
; returns eax = 0 if equal, -1 otherwise
91
proc strcmp, str1:dword, str2:dword
109
proc strcmp, str1:dword, str2:dword
92
	push	esi edi
110
	push	esi edi
93
	mov	esi, [str1]
111
	mov	esi, [str1]
94
	mov	edi, [str2]
112
	mov	edi, [str2]
95
	xor	eax, eax
113
	xor	eax, eax
96
    @@:
114
    @@:
97
	lodsb
115
	lodsb
98
	scasb
116
	scasb
99
	jne	.fail
117
	jne	.fail
100
	or	al, al
118
	or	al, al
101
	jnz	@b
119
	jnz	@b
102
	jmp	.ok
120
	jmp	.ok
103
  .fail:
121
  .fail:
104
	or	eax, -1
122
	or	eax, -1
105
  .ok:
123
  .ok:
106
	pop	edi esi
124
	pop	edi esi
107
	ret
125
	ret
108
endp
126
endp
109
;-----------------------------------------------------------------------------
127
;-----------------------------------------------------------------------------
110
s_libdir:
128
s_libdir:
111
  db '/sys/lib/'
129
  db '/sys/lib/'
112
  .fname rb 32
130
  .fname rb 32
113
;-----------------------------------------------------------------------------
131
;-----------------------------------------------------------------------------
114
proc mem.Alloc, size
132
proc mem.Alloc, size
115
	push	ebx ecx
133
	push	ebx ecx
116
	mov	ecx, [size]
134
	mov	ecx, [size]
117
	mcall	68, 12
135
	mcall	68, 12
118
	pop	ecx ebx
136
	pop	ecx ebx
119
	ret
137
	ret
120
endp
138
endp
121
;-----------------------------------------------------------------------------
139
;-----------------------------------------------------------------------------
122
proc mem.ReAlloc, mptr, size
140
proc mem.ReAlloc, mptr, size
123
	push	ebx ecx edx
141
	push	ebx ecx edx
124
	mov	ecx, [size]
142
	mov	ecx, [size]
125
	mov	edx, [mptr]
143
	mov	edx, [mptr]
126
	mcall	68, 20
144
	mcall	68, 20
127
	pop	edx ecx ebx
145
	pop	edx ecx ebx
128
	ret
146
	ret
129
endp
147
endp
130
;-----------------------------------------------------------------------------
148
;-----------------------------------------------------------------------------
131
proc mem.Free, mptr
149
proc mem.Free, mptr
132
	push	ebx ecx
150
	push	ebx ecx
133
	mov	ecx,[mptr]
151
	mov	ecx,[mptr]
134
	mcall	68, 13
152
	mcall	68, 13
135
	pop	ecx ebx
153
	pop	ecx ebx
136
	ret
154
	ret
137
endp
155
endp
138
;-----------------------------------------------------------------------------
156
;-----------------------------------------------------------------------------