Subversion Repositories Kolibri OS

Rev

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

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