Subversion Repositories Kolibri OS

Rev

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

Rev 5690 Rev 5883
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/file_system.h"
6
#include "../lib/file_system.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";
18
 
23
 
19
:inline void error_init(dword text)
24
:inline void error_init(dword text)
20
{
25
{
21
	dword TEXT_ERROR = malloc(1024);
26
	dword TEXT_ERROR = malloc(1024);
22
	sprintf(TEXT_ERROR, "%s `%s`' -E",_TEXT_ERROR_ADD,text);
27
	sprintf(TEXT_ERROR, "%s `%s`' -E",_TEXT_ERROR_ADD,text);
23
	notify(TEXT_ERROR);
28
	notify(TEXT_ERROR);
24
	free(TEXT_ERROR);
29
	free(TEXT_ERROR);
25
}
30
}
26
 
31
 
27
// stdcall with 1 parameter
32
// stdcall with 1 parameter
28
void dll_Load() {
33
:void dll_Load() {
29
asm {
34
asm {
30
        push    ebp
35
        push    ebp
31
        mov     ebp, esp
36
        mov     ebp, esp
32
        mov     esi, SSDWORD[EBP+8]
37
        mov     esi, SSDWORD[EBP+8]
33
		@next_lib:    
38
		@next_lib:    
34
        mov     edx, DSDWORD[ESI]
39
        mov     edx, DSDWORD[ESI]
35
        or      edx, edx
40
        or      edx, edx
36
        jz      exit
41
        jz      exit
37
        push    esi
42
        push    esi
38
        mov     esi, DSDWORD[ESI+4]
43
        mov     esi, DSDWORD[ESI+4]
39
        mov     edi, #a_libdir+9
44
        mov     edi, #a_libdir+9
40
 
45
 
41
@loc01: 
46
@loc01: 
42
        lodsb
47
        lodsb
43
        stosb
48
        stosb
44
        or      al, al
49
        or      al, al
45
        jnz     loc01
50
        jnz     loc01
46
 
51
 
47
        mov     eax, 68
52
        mov     eax, 68
48
        mov     ebx, 19
53
        mov     ebx, 19
49
        mov     ecx, #a_libdir
54
        mov     ecx, #a_libdir
50
        int     0x40
55
        int     0x40
51
        or      eax, eax
56
        or      eax, eax
52
        jz      fail
57
        jz      fail
53
 
58
 
54
        push    edx
59
        push    edx
55
        push    eax
60
        push    eax
56
        call    dll_Link
61
        call    dll_Link
57
 
62
 
58
        push    eax
63
        push    eax
59
        mov     eax, DSDWORD[eax]
64
        mov     eax, DSDWORD[eax]
60
        cmp     DSDWORD[EAX], '_bil'    // somehow this needs to be reversed..
65
        cmp     DSDWORD[EAX], '_bil'    // somehow this needs to be reversed..
61
        pop     eax
66
        pop     eax
62
        jnz     loc02
67
        jnz     loc02
63
 
68
 
64
        push    DSDWORD[EAX+4]
69
        push    DSDWORD[EAX+4]
65
        call    dll_Init
70
        call    dll_Init
66
@loc02:
71
@loc02:
67
 
72
 
68
        pop     esi
73
        pop     esi
69
        add     esi, 8
74
        add     esi, 8
70
        jmp     next_lib
75
        jmp     next_lib
71
@exit:
76
@exit:
72
	xor     eax, eax
77
	xor     eax, eax
73
        leave
78
        leave
74
        ret     4
79
        ret     4
75
        
80
        
76
@fail:        
81
@fail:        
77
        add     esp, 4
82
        add     esp, 4
78
        xor     eax, eax
83
        xor     eax, eax
79
        inc     eax
84
        inc     eax
80
        leave
85
        leave
81
        ret     4
86
        ret     4
82
    }
87
    }
83
}
88
}
84
 
89
 
85
//stdcall with 2 parameters
90
//stdcall with 2 parameters
86
void dll_Link() {
91
:void dll_Link() {
87
asm {
92
asm {
88
        push    ebp
93
        push    ebp
89
        mov     ebp, esp
94
        mov     ebp, esp
90
        push    eax
95
        push    eax
91
        mov     esi, SSDWORD[EBP+12]
96
        mov     esi, SSDWORD[EBP+12]
92
        test    esi, esi
97
        test    esi, esi
93
        jz      done
98
        jz      done
94
@next:        
99
@next:        
95
        lodsd
100
        lodsd
96
        test    eax, eax
101
        test    eax, eax
97
        jz      done
102
        jz      done
98
        push    eax
103
        push    eax
99
        push    SSDWORD[EBP+8]
104
        push    SSDWORD[EBP+8]
100
        call    dll_GetProcAddress
105
        call    dll_GetProcAddress
101
        or      eax, eax
106
        or      eax, eax
102
        jz      loc03
107
        jz      loc03
103
        mov     DSDWORD[esi-4], eax
108
        mov     DSDWORD[esi-4], eax
104
        jmp     next
109
        jmp     next
105
@loc03: 
110
@loc03: 
106
        mov     SSDWORD[esp], 0
111
        mov     SSDWORD[esp], 0
107
@done:
112
@done:
108
        pop     eax
113
        pop     eax
109
        leave
114
        leave
110
        ret     8
115
        ret     8
111
    }
116
    }
112
}
117
}
113
 
118
 
114
 
119
 
115
//stdcall with 1 parameter
120
//stdcall with 1 parameter
116
void dll_Init() {
121
:void dll_Init() {
117
asm {
122
asm {
118
        push    ebp
123
        push    ebp
119
        mov     ebp, esp
124
        mov     ebp, esp
120
        pushad
125
        pushad
121
        mov     eax, #malloc
126
        mov     eax, #malloc
122
        mov     ebx, #free;
127
        mov     ebx, #free;
123
        mov     ecx, #realloc;
128
        mov     ecx, #realloc;
124
        mov     edx, #dll_Load;
129
        mov     edx, #dll_Load;
125
        call    SSDWORD[EBP+8]
130
        call    SSDWORD[EBP+8]
126
        popad
131
        popad
127
        leave
132
        leave
128
        ret     4
133
        ret     4
129
    }
134
    }
130
}
135
}
131
 
136
 
132
 
137
 
133
// stdcall with 2 parameters
138
// stdcall with 2 parameters
134
void dll_GetProcAddress(){
139
:void dll_GetProcAddress(){
135
asm {
140
asm {
136
        push    ebp
141
        push    ebp
137
        mov     ebp, esp
142
        mov     ebp, esp
138
        mov     edx, CSDWORD[EBP+8]
143
        mov     edx, CSDWORD[EBP+8]
139
        xor     eax, eax
144
        xor     eax, eax
140
 
145
 
141
@next:        
146
@next:        
142
        or      edx, edx
147
        or      edx, edx
143
        jz      end
148
        jz      end
144
        cmp     CSDWORD[edx], 0
149
        cmp     CSDWORD[edx], 0
145
        jz      end
150
        jz      end
146
 
151
 
147
        push    CSDWORD[EBP+12]
152
        push    CSDWORD[EBP+12]
148
        push    CSDWORD[EDX]
153
        push    CSDWORD[EDX]
149
        call    dll_StrCmp
154
        call    dll_StrCmp
150
        test    eax, eax
155
        test    eax, eax
151
        jz      ok
156
        jz      ok
152
        add     edx, 8
157
        add     edx, 8
153
        jmp     next
158
        jmp     next
154
@ok:
159
@ok:
155
        mov     eax, DSDWORD[EDX+4]
160
        mov     eax, DSDWORD[EDX+4]
156
@end:
161
@end:
157
        leave
162
        leave
158
        ret     8
163
        ret     8
159
    }
164
    }
160
}
165
}
161
 
166
 
162
 
167
 
163
// stdcall with 2 parameters
168
// stdcall with 2 parameters
164
void dll_StrCmp() {
169
:void dll_StrCmp() {
165
asm {
170
asm {
166
        push    ebp
171
        push    ebp
167
        mov     ebp, esp
172
        mov     ebp, esp
168
        push    esi
173
        push    esi
169
        push    edi
174
        push    edi
170
        mov     esi, CSDWORD[EBP+8]
175
        mov     esi, CSDWORD[EBP+8]
171
        mov     edi, CSDWORD[EBP+12]
176
        mov     edi, CSDWORD[EBP+12]
172
        xor     eax, eax
177
        xor     eax, eax
173
@label1:
178
@label1:
174
        lodsb
179
        lodsb
175
        scasb
180
        scasb
176
        jne     fail
181
        jne     fail
177
        or      al, al
182
        or      al, al
178
        jnz     label1
183
        jnz     label1
179
        jmp     label_ok
184
        jmp     label_ok
180
@fail:
185
@fail:
181
        or      eax, -1
186
        or      eax, -1
182
@label_ok:
187
@label_ok:
183
        pop     edi
188
        pop     edi
184
        pop     esi
189
        pop     esi
185
        leave
190
        leave
186
        ret     8
191
        ret     8
187
    }
192
    }
188
}
193
}
189
 
194
 
190
int load_dll2(dword dllname, import_table, byte need_init)
195
:int load_dll2(dword dllname, import_table, byte need_init)
191
{
196
{
192
   //dword dllentry=0;
197
   //dword dllentry=0;
193
// load DLL
198
// load DLL
194
        $mov     eax, 68
199
        $mov     eax, 68
195
        $mov     ebx, 19
200
        $mov     ebx, 19
196
        ECX=dllname;
201
        ECX=dllname;
197
        $int     0x40
202
        $int     0x40
198
        $test    eax, eax
203
        $test    eax, eax
199
        $jz      exit01
204
        $jz      exit01
200
 
205
 
201
// initialize import
206
// initialize import
202
        $mov     edx,eax
207
        $mov     edx,eax
203
        ESI=import_table;
208
        ESI=import_table;
204
 
209
 
205
@import_loop01:
210
@import_loop01:
206
        $lodsd
211
        $lodsd
207
        $test    eax,eax
212
        $test    eax,eax
208
        $jz      import_done01
213
        $jz      import_done01
209
        $push    edx
214
        $push    edx
210
@import_find01:
215
@import_find01:
211
        $mov     ebx,DSDWORD[EDX]
216
        $mov     ebx,DSDWORD[EDX]
212
        $test    ebx, ebx
217
        $test    ebx, ebx
213
        $jz      exit01
218
        $jz      exit01
214
        $push    eax
219
        $push    eax
215
@nex101:
220
@nex101:
216
        $mov     cl,DSBYTE[EAX];
221
        $mov     cl,DSBYTE[EAX];
217
        $cmp     cl,DSBYTE[EBX];
222
        $cmp     cl,DSBYTE[EBX];
218
        $jnz     import_find_next01
223
        $jnz     import_find_next01
219
        $test    cl,cl
224
        $test    cl,cl
220
        $jz      import_found01
225
        $jz      import_found01
221
        $inc     eax
226
        $inc     eax
222
        $inc     ebx
227
        $inc     ebx
223
        $jmp     nex101
228
        $jmp     nex101
224
@import_find_next01:
229
@import_find_next01:
225
        $pop     eax
230
        $pop     eax
226
        $add     edx, 8
231
        $add     edx, 8
227
        $jmp     import_find01
232
        $jmp     import_find01
228
@import_found01:
233
@import_found01:
229
        $pop     eax
234
        $pop     eax
230
        $mov     eax,DSDWORD[edx+4]
235
        $mov     eax,DSDWORD[edx+4]
231
        $mov     DSDWORD[esi-4],eax
236
        $mov     DSDWORD[esi-4],eax
232
        $pop     edx
237
        $pop     edx
233
       
238
       
234
        $jmp     import_loop01
239
        $jmp     import_loop01
235
@import_done01:
240
@import_done01:
236
        IF (need_init) dll_Init (DSDWORD[EDX+4]);
241
        IF (need_init) dll_Init (DSDWORD[EDX+4]);
237
        return 0;
242
        return 0;
238
@exit01:
243
@exit01:
239
        return -1;
244
        return -1;
240
}
245
}
241
 
246
 
242
byte load_dll(dword dllname, import_table, byte need_init)
247
:byte load_dll(dword dllname, import_table, byte need_init)
243
{
248
{
244
	if (load_dll2(dllname, import_table, need_init))
249
	if (load_dll2(dllname, import_table, need_init))
245
	{
250
	{
246
		error_init(dllname);
251
		error_init(dllname);
247
		return false;
252
		return false;
248
	}
253
	}
249
	return true;
254
	return true;
250
}
255
}
-
 
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;}
251
 
293
*/
252
 
294
 
253
#endif
295
#endif