Subversion Repositories Kolibri OS

Rev

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