Rev 535 | Rev 750 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
488 | spraid | 1 | ;============================================================================ |
2 | ; |
||
530 | mikedld | 3 | ; External kernel dependencies (libraries) loading |
488 | spraid | 4 | ; |
5 | ;============================================================================ |
||
593 | mikedld | 6 | |
7 | $Revision: 593 $ |
||
8 | |||
9 | |||
488 | spraid | 10 | macro library [name,fname] |
11 | { |
||
12 | forward |
||
13 | dd __#name#_library_table__,__#name#_library_name__ |
||
14 | common |
||
15 | dd 0 |
||
16 | forward |
||
17 | __#name#_library_name__ db fname,0 |
||
18 | } |
||
19 | |||
20 | macro import lname,[name,sname] |
||
21 | { |
||
22 | common |
||
23 | align 4 |
||
24 | __#lname#_library_table__: |
||
25 | forward |
||
26 | name dd __#name#_import_name__ |
||
27 | common |
||
28 | dd 0 |
||
29 | forward |
||
30 | __#name#_import_name__ db sname,0 |
||
31 | } |
||
32 | |||
33 | macro export [name,sname] |
||
34 | { |
||
521 | diamond | 35 | align 4 |
488 | spraid | 36 | forward |
37 | dd __#name#_export_name__,name |
||
38 | common |
||
39 | dd 0 |
||
40 | forward |
||
41 | __#name#_export_name__ db sname,0 |
||
42 | } |
||
43 | |||
44 | |||
45 | |||
535 | spraid | 46 | align 4 ; loading library (use kernel functions) |
488 | spraid | 47 | proc load_k_library stdcall, file_name:dword |
48 | locals |
||
49 | coff dd ? |
||
50 | sym dd ? |
||
51 | strings dd ? |
||
52 | img_size dd ? |
||
53 | img_base dd ? |
||
54 | exports dd ? |
||
55 | endl |
||
519 | serge | 56 | |
488 | spraid | 57 | cli |
519 | serge | 58 | |
488 | spraid | 59 | stdcall load_file, [file_name] |
60 | test eax, eax |
||
61 | jz .fail |
||
62 | |||
63 | mov [coff], eax |
||
64 | movzx ecx, [eax+CFH.nSections] |
||
65 | xor ebx, ebx |
||
66 | |||
67 | lea edx, [eax+20] |
||
68 | @@: |
||
69 | add ebx, [edx+CFS.SizeOfRawData] |
||
70 | add ebx, 15 |
||
71 | and ebx, not 15 |
||
72 | add edx, COFF_SECTION_SIZE |
||
73 | dec ecx |
||
74 | jnz @B |
||
75 | mov [img_size], ebx |
||
76 | |||
77 | stdcall kernel_alloc, [img_size] |
||
78 | |||
79 | test eax, eax |
||
80 | jz .fail |
||
81 | mov [img_base], eax |
||
82 | |||
83 | mov edx, [coff] |
||
84 | movzx ebx, [edx+CFH.nSections] |
||
85 | mov edi, [img_base] |
||
86 | lea eax, [edx+20] |
||
87 | @@: |
||
88 | mov [eax+CFS.VirtualAddress], edi |
||
89 | mov esi, [eax+CFS.PtrRawData] |
||
90 | test esi, esi |
||
91 | jnz .copy |
||
92 | add edi, [eax+CFS.SizeOfRawData] |
||
93 | jmp .next |
||
94 | .copy: |
||
95 | add esi, edx |
||
96 | mov ecx, [eax+CFS.SizeOfRawData] |
||
97 | cld |
||
98 | rep movsb |
||
99 | .next: |
||
100 | add edi, 15 |
||
101 | and edi, not 15 |
||
102 | add eax, COFF_SECTION_SIZE |
||
103 | dec ebx |
||
104 | jnz @B |
||
105 | |||
106 | mov ebx, [edx+CFH.pSymTable] |
||
107 | add ebx, edx |
||
108 | mov [sym], ebx |
||
109 | mov ecx, [edx+CFH.nSymbols] |
||
110 | add ecx,ecx |
||
111 | lea ecx,[ecx+ecx*8] ;ecx*=18 = nSymbols*CSYM_SIZE |
||
112 | add ecx, [sym] |
||
113 | mov [strings], ecx |
||
114 | |||
115 | lea eax, [edx+20] |
||
519 | serge | 116 | |
488 | spraid | 117 | stdcall fix_coff_symbols, eax, [sym], [edx+CFH.nSymbols],\ |
118 | [strings], dword 0 |
||
119 | test eax, eax |
||
120 | jnz @F |
||
121 | |||
122 | @@: |
||
123 | mov edx, [coff] |
||
124 | movzx ebx, [edx+CFH.nSections] |
||
125 | mov edi, 0 |
||
126 | lea eax, [edx+20] |
||
127 | @@: |
||
128 | add [eax+CFS.VirtualAddress], edi ;patch user space offset |
||
129 | add eax, COFF_SECTION_SIZE |
||
130 | dec ebx |
||
131 | jnz @B |
||
132 | |||
133 | add edx, 20 |
||
134 | stdcall fix_coff_relocs, [coff], edx, [sym] |
||
135 | |||
136 | mov ebx, [coff] |
||
137 | stdcall get_coff_sym,[sym],[ebx+CFH.nSymbols],szEXPORTS |
||
138 | mov [exports], eax |
||
139 | |||
140 | stdcall kernel_free, [coff] |
||
141 | |||
142 | mov eax, [exports] |
||
143 | ret |
||
144 | .fail: |
||
145 | xor eax, eax |
||
146 | ret |
||
147 | endp |
||
148 | |||
149 | |||
150 | proc dll.Load, import_table:dword |
||
151 | mov esi,[import_table] |
||
152 | .next_lib: mov edx,[esi] |
||
153 | or edx,edx |
||
154 | jz .exit |
||
155 | push esi |
||
156 | |||
157 | mov edi,s_libname |
||
519 | serge | 158 | |
521 | diamond | 159 | mov al, '/' |
160 | stosb |
||
161 | mov esi,sysdir_path |
||
488 | spraid | 162 | @@: lodsb |
163 | stosb |
||
164 | or al,al |
||
165 | jnz @b |
||
166 | dec edi |
||
167 | mov [edi], dword '/lib' |
||
168 | mov [edi+4],byte '/' |
||
169 | add edi,5 |
||
170 | pop esi |
||
171 | push esi |
||
172 | mov esi,[esi+4] |
||
173 | @@: lodsb |
||
174 | stosb |
||
175 | or al,al |
||
176 | jnz @b |
||
177 | |||
178 | pushad |
||
179 | stdcall load_k_library,s_libname |
||
180 | mov [esp+28],eax |
||
181 | popad |
||
182 | or eax,eax |
||
183 | jz .fail |
||
184 | stdcall dll.Link,eax,edx |
||
185 | stdcall dll.Init,[eax+4] |
||
186 | pop esi |
||
187 | add esi,8 |
||
188 | jmp .next_lib |
||
189 | .exit: xor eax,eax |
||
190 | ret |
||
191 | .fail: add esp,4 |
||
192 | xor eax,eax |
||
193 | inc eax |
||
194 | ret |
||
195 | endp |
||
196 | |||
197 | proc dll.Link, exp:dword,imp:dword |
||
198 | push eax |
||
199 | mov esi,[imp] |
||
200 | test esi,esi |
||
201 | jz .done |
||
202 | .next: lodsd |
||
203 | test eax,eax |
||
204 | jz .done |
||
205 | stdcall dll.GetProcAddress,[exp],eax |
||
206 | or eax,eax |
||
207 | jz @f |
||
208 | mov [esi-4],eax |
||
209 | jmp .next |
||
210 | @@: mov dword[esp],0 |
||
211 | .done: pop eax |
||
212 | ret |
||
213 | endp |
||
214 | |||
215 | proc dll.Init, dllentry:dword |
||
216 | pushad |
||
217 | mov eax,mem.Alloc |
||
218 | mov ebx,mem.Free |
||
219 | mov ecx,mem.ReAlloc |
||
220 | mov edx,dll.Load |
||
221 | stdcall [dllentry] |
||
222 | popad |
||
223 | ret |
||
224 | endp |
||
225 | |||
226 | proc dll.GetProcAddress, exp:dword,sz_name:dword |
||
227 | mov edx,[exp] |
||
228 | .next: test edx,edx |
||
229 | jz .end |
||
519 | serge | 230 | stdcall strncmp,[edx],[sz_name], dword -1 |
488 | spraid | 231 | test eax,eax |
232 | jz .ok |
||
233 | add edx,8 |
||
234 | jmp .next |
||
235 | .ok: mov eax,[edx+4] |
||
236 | .end: ret |
||
237 | endp |
||
238 | |||
239 | ;----------------------------------------------------------------------------- |
||
240 | proc mem.Alloc size ;///////////////////////////////////////////////////////// |
||
241 | ;----------------------------------------------------------------------------- |
||
242 | push ebx ecx |
||
243 | ; mov eax,[size] |
||
244 | ; lea ecx,[eax+4+4095] |
||
245 | ; and ecx,not 4095 |
||
246 | ; stdcall kernel_alloc, ecx |
||
247 | ; add ecx,-4 |
||
248 | ; mov [eax],ecx |
||
249 | ; add eax,4 |
||
250 | |||
251 | stdcall kernel_alloc, [size] |
||
252 | |||
253 | pop ecx ebx |
||
254 | ret |
||
255 | endp |
||
256 | |||
257 | ;----------------------------------------------------------------------------- |
||
258 | proc mem.ReAlloc mptr,size;/////////////////////////////////////////////////// |
||
259 | ;----------------------------------------------------------------------------- |
||
260 | push ebx ecx esi edi eax |
||
261 | mov eax,[mptr] |
||
262 | mov ebx,[size] |
||
263 | or eax,eax |
||
264 | jz @f |
||
265 | lea ecx,[ebx+4+4095] |
||
266 | and ecx,not 4095 |
||
267 | add ecx,-4 |
||
268 | cmp ecx,[eax-4] |
||
269 | je .exit |
||
270 | @@: mov eax,ebx |
||
271 | call mem.Alloc |
||
272 | xchg eax,[esp] |
||
273 | or eax,eax |
||
274 | jz .exit |
||
275 | mov esi,eax |
||
276 | xchg eax,[esp] |
||
277 | mov edi,eax |
||
278 | mov ecx,[esi-4] |
||
279 | cmp ecx,[edi-4] |
||
280 | jbe @f |
||
281 | mov ecx,[edi-4] |
||
282 | @@: add ecx,3 |
||
283 | shr ecx,2 |
||
284 | cld |
||
285 | rep movsd |
||
286 | xchg eax,[esp] |
||
287 | call mem.Free |
||
288 | .exit: |
||
289 | pop eax edi esi ecx ebx |
||
290 | ret |
||
291 | endp |
||
292 | |||
293 | ;----------------------------------------------------------------------------- |
||
294 | proc mem.Free mptr ;////////////////////////////////////////////////////////// |
||
295 | ;----------------------------------------------------------------------------- |
||
296 | ; mov eax,[mptr] |
||
297 | ; or eax,eax |
||
298 | ; jz @f |
||
299 | ; push ebx ecx |
||
300 | ; lea ecx,[eax-4] |
||
301 | ; stdcall kernel_free, ecx |
||
302 | ; pop ecx ebx |
||
303 | ; @@: ret |
||
304 | stdcall kernel_free, [mptr] |
||
305 | ret |
||
306 | endp |
||
307 | |||
521 | diamond | 308 | uglobal |
488 | spraid | 309 | s_libname db 64 dup (0) |
521 | diamond | 310 | endg |