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