Subversion Repositories Kolibri OS

Rev

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

Rev 7219 Rev 7246
1
#ifndef INCLUDE_DLL_H
1
#ifndef INCLUDE_DLL_H
2
#define INCLUDE_DLL_H
2
#define INCLUDE_DLL_H
3
#print "[include ]\n"
3
#print "[include ]\n"
4
 
4
 
5
#ifndef INCLUDE_FILESYSTEM_H
5
#ifndef INCLUDE_FILESYSTEM_H
6
#include "../lib/fs.h"
6
#include "../lib/fs.h"
7
#endif
7
#endif
8
 
8
 
9
#ifdef LANG_RUS
9
#ifdef LANG_RUS
10
	#define _TEXT_ERROR_ADD "'Žè¨¡ª  ¯à¨ § £à㧪¥ ¡¨¡«¨®â¥ª¨"
10
	#define _TEXT_ERROR_ADD "'Žè¨¡ª  ¯à¨ § £à㧪¥ ¡¨¡«¨®â¥ª¨"
11
#elif LANG_EST
11
#elif LANG_EST
12
	#define _TEXT_ERROR_ADD "'Viga teegi laadimisel"
12
	#define _TEXT_ERROR_ADD "'Viga teegi laadimisel"
13
#else
13
#else
14
	#define _TEXT_ERROR_ADD "'Error while loading library"
14
	#define _TEXT_ERROR_ADD "'Error while loading library"
15
#endif
15
#endif
16
 
16
 
17
char a_libdir[43]  = "/sys/lib/\0";
17
char a_libdir[43]  = "/sys/lib/\0";
18
 
-
 
19
dword GLOBAL_FUNC_LIB = 0;
-
 
20
dword G_FUNC_LOAD = 0;
-
 
21
dword G_FUNC_GET = 0;
-
 
22
dword PATH_LIBRARY_LOADING = "/rd/1/lib/library.obj";
-
 
23
 
18
 
24
:inline void error_init(dword text)
19
:inline void error_init(dword text)
25
{
20
{
26
	dword TEXT_ERROR = malloc(1024);
21
	dword TEXT_ERROR[1024];
27
	sprintf(TEXT_ERROR, "%s `%s`' -E",_TEXT_ERROR_ADD,text);
22
	sprintf(TEXT_ERROR, "%s `%s`' -E",_TEXT_ERROR_ADD,text);
28
	notify(TEXT_ERROR);
-
 
29
	free(TEXT_ERROR);
23
	notify(TEXT_ERROR);
30
}
24
}
31
 
25
 
32
// stdcall with 1 parameter
26
// stdcall with 1 parameter
33
:void dll_Load() {
27
:void dll_Load() {
34
asm {
28
asm {
35
        push    ebp
29
        push    ebp
36
        mov     ebp, esp
30
        mov     ebp, esp
37
        mov     esi, SSDWORD[EBP+8]
31
        mov     esi, SSDWORD[EBP+8]
38
		@next_lib:    
32
		@next_lib:    
39
        mov     edx, DSDWORD[ESI]
33
        mov     edx, DSDWORD[ESI]
40
        or      edx, edx
34
        or      edx, edx
41
        jz      exit
35
        jz      exit
42
        push    esi
36
        push    esi
43
        mov     esi, DSDWORD[ESI+4]
37
        mov     esi, DSDWORD[ESI+4]
44
        mov     edi, #a_libdir+9
38
        mov     edi, #a_libdir+9
45
 
39
 
46
@loc01: 
40
@loc01: 
47
        lodsb
41
        lodsb
48
        stosb
42
        stosb
49
        or      al, al
43
        or      al, al
50
        jnz     loc01
44
        jnz     loc01
51
 
45
 
52
        mov     eax, 68
46
        mov     eax, 68
53
        mov     ebx, 19
47
        mov     ebx, 19
54
        mov     ecx, #a_libdir
48
        mov     ecx, #a_libdir
55
        int     0x40
49
        int     0x40
56
        or      eax, eax
50
        or      eax, eax
57
        jz      fail
51
        jz      fail
58
 
52
 
59
        push    edx
53
        push    edx
60
        push    eax
54
        push    eax
61
        call    dll_Link
55
        call    dll_Link
62
 
56
 
63
        push    eax
57
        push    eax
64
        mov     eax, DSDWORD[eax]
58
        mov     eax, DSDWORD[eax]
65
        cmp     DSDWORD[EAX], '_bil'    // somehow this needs to be reversed..
59
        cmp     DSDWORD[EAX], '_bil'    // somehow this needs to be reversed..
66
        pop     eax
60
        pop     eax
67
        jnz     loc02
61
        jnz     loc02
68
 
62
 
69
        push    DSDWORD[EAX+4]
63
        push    DSDWORD[EAX+4]
70
        call    dll_Init
64
        call    dll_Init
71
@loc02:
65
@loc02:
72
 
66
 
73
        pop     esi
67
        pop     esi
74
        add     esi, 8
68
        add     esi, 8
75
        jmp     next_lib
69
        jmp     next_lib
76
@exit:
70
@exit:
77
	xor     eax, eax
71
	xor     eax, eax
78
        leave
72
        leave
79
        ret     4
73
        ret     4
80
        
74
        
81
@fail:        
75
@fail:        
82
        add     esp, 4
76
        add     esp, 4
83
        xor     eax, eax
77
        xor     eax, eax
84
        inc     eax
78
        inc     eax
85
        leave
79
        leave
86
        ret     4
80
        ret     4
87
    }
81
    }
88
}
82
}
89
 
83
 
90
//stdcall with 2 parameters
84
//stdcall with 2 parameters
91
:void dll_Link() {
85
:void dll_Link() {
92
asm {
86
asm {
93
        push    ebp
87
        push    ebp
94
        mov     ebp, esp
88
        mov     ebp, esp
95
        push    eax
89
        push    eax
96
        mov     esi, SSDWORD[EBP+12]
90
        mov     esi, SSDWORD[EBP+12]
97
        test    esi, esi
91
        test    esi, esi
98
        jz      done
92
        jz      done
99
@next:        
93
@next:        
100
        lodsd
94
        lodsd
101
        test    eax, eax
95
        test    eax, eax
102
        jz      done
96
        jz      done
103
        push    eax
97
        push    eax
104
        push    SSDWORD[EBP+8]
98
        push    SSDWORD[EBP+8]
105
        call    dll_GetProcAddress
99
        call    dll_GetProcAddress
106
        or      eax, eax
100
        or      eax, eax
107
        jz      loc03
101
        jz      loc03
108
        mov     DSDWORD[esi-4], eax
102
        mov     DSDWORD[esi-4], eax
109
        jmp     next
103
        jmp     next
110
@loc03: 
104
@loc03: 
111
        mov     SSDWORD[esp], 0
105
        mov     SSDWORD[esp], 0
112
@done:
106
@done:
113
        pop     eax
107
        pop     eax
114
        leave
108
        leave
115
        ret     8
109
        ret     8
116
    }
110
    }
117
}
111
}
118
 
112
 
119
 
113
 
120
//stdcall with 1 parameter
114
//stdcall with 1 parameter
121
:void dll_Init() {
115
:void dll_Init() {
122
asm {
116
asm {
123
        push    ebp
117
        push    ebp
124
        mov     ebp, esp
118
        mov     ebp, esp
125
        pushad
119
        pushad
126
        mov     eax, #malloc
120
        mov     eax, #malloc
127
        mov     ebx, #free;
121
        mov     ebx, #free;
128
        mov     ecx, #realloc;
122
        mov     ecx, #realloc;
129
        mov     edx, #dll_Load;
123
        mov     edx, #dll_Load;
130
        call    SSDWORD[EBP+8]
124
        call    SSDWORD[EBP+8]
131
        popad
125
        popad
132
        leave
126
        leave
133
        ret     4
127
        ret     4
134
    }
128
    }
135
}
129
}
136
 
130
 
137
 
131
 
138
// stdcall with 2 parameters
132
// stdcall with 2 parameters
139
:void dll_GetProcAddress(){
133
:void dll_GetProcAddress(){
140
asm {
134
asm {
141
        push    ebp
135
        push    ebp
142
        mov     ebp, esp
136
        mov     ebp, esp
143
        mov     edx, CSDWORD[EBP+8]
137
        mov     edx, CSDWORD[EBP+8]
144
        xor     eax, eax
138
        xor     eax, eax
145
 
139
 
146
@next:        
140
@next:        
147
        or      edx, edx
141
        or      edx, edx
148
        jz      end
142
        jz      end
149
        cmp     CSDWORD[edx], 0
143
        cmp     CSDWORD[edx], 0
150
        jz      end
144
        jz      end
151
 
145
 
152
        push    CSDWORD[EBP+12]
146
        push    CSDWORD[EBP+12]
153
        push    CSDWORD[EDX]
147
        push    CSDWORD[EDX]
154
        call    dll_StrCmp
148
        call    dll_StrCmp
155
        test    eax, eax
149
        test    eax, eax
156
        jz      ok
150
        jz      ok
157
        add     edx, 8
151
        add     edx, 8
158
        jmp     next
152
        jmp     next
159
@ok:
153
@ok:
160
        mov     eax, DSDWORD[EDX+4]
154
        mov     eax, DSDWORD[EDX+4]
161
@end:
155
@end:
162
        leave
156
        leave
163
        ret     8
157
        ret     8
164
    }
158
    }
165
}
159
}
166
 
160
 
167
 
161
 
168
// stdcall with 2 parameters
162
// stdcall with 2 parameters
169
:void dll_StrCmp() {
163
:void dll_StrCmp() {
170
asm {
164
asm {
171
        push    ebp
165
        push    ebp
172
        mov     ebp, esp
166
        mov     ebp, esp
173
        push    esi
167
        push    esi
174
        push    edi
168
        push    edi
175
        mov     esi, CSDWORD[EBP+8]
169
        mov     esi, CSDWORD[EBP+8]
176
        mov     edi, CSDWORD[EBP+12]
170
        mov     edi, CSDWORD[EBP+12]
177
        xor     eax, eax
171
        xor     eax, eax
178
@label1:
172
@label1:
179
        lodsb
173
        lodsb
180
        scasb
174
        scasb
181
        jne     fail
175
        jne     fail
182
        or      al, al
176
        or      al, al
183
        jnz     label1
177
        jnz     label1
184
        jmp     label_ok
178
        jmp     label_ok
185
@fail:
179
@fail:
186
        or      eax, -1
180
        or      eax, -1
187
@label_ok:
181
@label_ok:
188
        pop     edi
182
        pop     edi
189
        pop     esi
183
        pop     esi
190
        leave
184
        leave
191
        ret     8
185
        ret     8
192
    }
186
    }
193
}
187
}
194
 
188
 
195
:int load_dll2(dword dllname, import_table, byte need_init)
189
:int load_dll2(dword dllname, import_table, byte need_init)
196
{
190
{
197
   //dword dllentry=0;
191
   //dword dllentry=0;
198
// load DLL
192
// load DLL
199
        $mov     eax, 68
193
        $mov     eax, 68
200
        $mov     ebx, 19
194
        $mov     ebx, 19
201
        ECX=dllname;
195
        ECX=dllname;
202
        $int     0x40
196
        $int     0x40
203
        $test    eax, eax
197
        $test    eax, eax
204
        $jz      exit01
198
        $jz      exit01
205
 
199
 
206
// initialize import
200
// initialize import
207
        $mov     edx,eax
201
        $mov     edx,eax
208
        ESI=import_table;
202
        ESI=import_table;
209
 
203
 
210
@import_loop01:
204
@import_loop01:
211
        $lodsd
205
        $lodsd
212
        $test    eax,eax
206
        $test    eax,eax
213
        $jz      import_done01
207
        $jz      import_done01
214
        $push    edx
208
        $push    edx
215
@import_find01:
209
@import_find01:
216
        $mov     ebx,DSDWORD[EDX]
210
        $mov     ebx,DSDWORD[EDX]
217
        $test    ebx, ebx
211
        $test    ebx, ebx
218
        $jz      exit01
212
        $jz      exit01
219
        $push    eax
213
        $push    eax
220
@nex101:
214
@nex101:
221
        $mov     cl,DSBYTE[EAX];
215
        $mov     cl,DSBYTE[EAX];
222
        $cmp     cl,DSBYTE[EBX];
216
        $cmp     cl,DSBYTE[EBX];
223
        $jnz     import_find_next01
217
        $jnz     import_find_next01
224
        $test    cl,cl
218
        $test    cl,cl
225
        $jz      import_found01
219
        $jz      import_found01
226
        $inc     eax
220
        $inc     eax
227
        $inc     ebx
221
        $inc     ebx
228
        $jmp     nex101
222
        $jmp     nex101
229
@import_find_next01:
223
@import_find_next01:
230
        $pop     eax
224
        $pop     eax
231
        $add     edx, 8
225
        $add     edx, 8
232
        $jmp     import_find01
226
        $jmp     import_find01
233
@import_found01:
227
@import_found01:
234
        $pop     eax
228
        $pop     eax
235
        $mov     eax,DSDWORD[edx+4]
229
        $mov     eax,DSDWORD[edx+4]
236
        $mov     DSDWORD[esi-4],eax
230
        $mov     DSDWORD[esi-4],eax
237
        $pop     edx
231
        $pop     edx
238
       
232
       
239
        $jmp     import_loop01
233
        $jmp     import_loop01
240
@import_done01:
234
@import_done01:
241
        IF (need_init) dll_Init (DSDWORD[EDX+4]);
235
        IF (need_init) dll_Init (DSDWORD[EDX+4]);
242
        return 0;
236
        return 0;
243
@exit01:
237
@exit01:
244
        return -1;
238
        return -1;
245
}
239
}
246
 
240
 
247
:byte load_dll(dword dllname, import_table, byte need_init)
241
:byte load_dll(dword dllname, import_table, byte need_init)
248
{
242
{
249
	if (load_dll2(dllname, import_table, need_init))
243
	if (load_dll2(dllname, import_table, need_init))
250
	{
244
	{
251
		error_init(dllname);
245
		error_init(dllname);
252
		return false;
246
		return false;
253
	}
247
	}
254
	return true;
248
	return true;
255
}
249
}
256
 
-
 
257
:struct OBJECT
-
 
258
{
-
 
259
	void load(dword dllname);
-
 
260
	dword get(dword fname);
-
 
261
} library;
-
 
262
 
-
 
263
:void OBJECT::load(dword dllname)
-
 
264
{
-
 
265
	dword tmp;
-
 
266
	IF(!GLOBAL_FUNC_LIB)
-
 
267
	{
-
 
268
		$mov eax, 68
-
 
269
		$mov ebx, 19
-
 
270
		ECX=#PATH_LIBRARY_LOADING;
-
 
271
		$int 0x40
-
 
272
		tmp = EAX;
-
 
273
		GLOBAL_FUNC_LIB = tmp;
-
 
274
		tmp+=4;
-
 
275
		G_FUNC_LOAD = DSDWORD[tmp];
-
 
276
		tmp+=8;
-
 
277
		G_FUNC_GET = DSDWORD[tmp];
-
 
278
	}
-
 
279
	G_FUNC_LOAD stdcall(dllname);
-
 
280
}
-
 
281
 
-
 
282
:dword OBJECT::get(dword fname)
-
 
283
{
-
 
284
	dword tmp=fname;
-
 
285
	G_FUNC_GET stdcall(tmp);
-
 
286
	return EAX;
-
 
287
}
-
 
288
 
-
 
289
/*
-
 
290
#define INIT_(name) byte init_#name(){object.load(name);
-
 
291
#define IMPORT(name) name = object.get(name);
-
 
292
#define _INIT return 1;}
-
 
293
*/
250
 
294
 
251
 
295
#endif
252
#endif