1
|
1 /*
|
|
2 * Modules
|
|
3 *
|
|
4 * Copyright 1995 Alexandre Julliard
|
|
5 */
|
|
6 #include <config.h>
|
|
7
|
|
8 #include <assert.h>
|
|
9 #include <errno.h>
|
|
10 #include <fcntl.h>
|
|
11 #include <stdio.h>
|
|
12 #include <stdlib.h>
|
|
13 #include <string.h>
|
|
14 #include <unistd.h>
|
|
15
|
|
16 #include <sys/mman.h>
|
|
17 #include <sys/types.h>
|
|
18 /*
|
|
19 #ifdef __linux__
|
|
20 #include <asm/unistd.h>
|
|
21 #include <asm/ldt.h>
|
|
22 #else
|
|
23 #define LDT_ENTRIES 8192
|
|
24 #define LDT_ENTRY_SIZE 8
|
|
25
|
|
26 struct modify_ldt_ldt_s {
|
|
27 unsigned int entry_number;
|
|
28 unsigned long base_addr;
|
|
29 unsigned int limit;
|
|
30 unsigned int seg_32bit:1;
|
|
31 unsigned int contents:2;
|
|
32 unsigned int read_exec_only:1;
|
|
33 unsigned int limit_in_pages:1;
|
|
34 unsigned int seg_not_present:1;
|
|
35 unsigned int useable:1;
|
|
36 };
|
|
37
|
|
38 #define MODIFY_LDT_CONTENTS_DATA 0
|
|
39 #define MODIFY_LDT_CONTENTS_STACK 1
|
|
40 #define MODIFY_LDT_CONTENTS_CODE 2
|
|
41 #define __NR_modify_ldt 123
|
|
42 #endif
|
|
43
|
|
44 */
|
|
45 #include <wine/windef.h>
|
|
46 #include <wine/winerror.h>
|
|
47 #include <wine/heap.h>
|
|
48 #include <wine/module.h>
|
|
49 #include <wine/pe_image.h>
|
|
50 #include <wine/debugtools.h>
|
|
51
|
|
52 struct modref_list_t;
|
|
53
|
|
54 typedef struct modref_list_t
|
|
55 {
|
|
56 WINE_MODREF* wm;
|
|
57 struct modref_list_t *next;
|
|
58 struct modref_list_t *prev;
|
|
59 }
|
|
60 modref_list;
|
|
61
|
|
62 //WINE_MODREF *local_wm=NULL;
|
|
63 modref_list* local_wm=NULL;
|
|
64
|
|
65 WINE_MODREF *MODULE_FindModule(LPCSTR m)
|
|
66 {
|
|
67 modref_list* list=local_wm;
|
|
68 TRACE("Module %s request\n", m);
|
|
69 if(list==NULL)
|
|
70 return NULL;
|
|
71 while(strcmp(m, list->wm->filename))
|
|
72 {
|
128
|
73 TRACE("%s: %x\n", list->wm->filename, list->wm->module);
|
1
|
74 list=list->prev;
|
|
75 if(list==NULL)
|
|
76 return NULL;
|
|
77 }
|
|
78 TRACE("Resolved to %s\n", list->wm->filename);
|
|
79 return list->wm;
|
|
80 }
|
|
81
|
|
82 void MODULE_RemoveFromList(WINE_MODREF *mod)
|
|
83 {
|
|
84 modref_list* list=local_wm;
|
|
85 if(list==0)
|
|
86 return;
|
|
87 if(mod==0)
|
|
88 return;
|
|
89 if((list->prev==NULL)&&(list->next==NULL))
|
|
90 {
|
|
91 free(list);
|
|
92 local_wm=NULL;
|
|
93 // uninstall_fs();
|
|
94 return;
|
|
95 }
|
|
96 for(;list;list=list->prev)
|
|
97 {
|
|
98 if(list->wm==mod)
|
|
99 {
|
|
100 if(list->prev)
|
|
101 list->prev->next=list->next;
|
|
102 if(list->next)
|
|
103 list->next->prev=list->prev;
|
|
104 if(list==local_wm)
|
|
105 local_wm=list->prev;
|
|
106 free(list);
|
|
107 return;
|
|
108 }
|
|
109 }
|
|
110 }
|
|
111
|
|
112 WINE_MODREF *MODULE32_LookupHMODULE(HMODULE m)
|
|
113 {
|
|
114 modref_list* list=local_wm;
|
|
115 TRACE("Module %X request\n", m);
|
|
116 if(list==NULL)
|
|
117 return NULL;
|
|
118 while(m!=list->wm->module)
|
|
119 {
|
|
120 // printf("Checking list %X wm %X module %X\n",
|
|
121 // list, list->wm, list->wm->module);
|
|
122 list=list->prev;
|
|
123 if(list==NULL)
|
|
124 return NULL;
|
|
125 }
|
|
126 TRACE("LookupHMODULE hit %X\n", list->wm);
|
|
127 return list->wm;
|
|
128 }
|
|
129
|
|
130 /*************************************************************************
|
|
131 * MODULE_InitDll
|
|
132 */
|
|
133 static WIN_BOOL MODULE_InitDll( WINE_MODREF *wm, DWORD type, LPVOID lpReserved )
|
|
134 {
|
|
135 WIN_BOOL retv = TRUE;
|
|
136
|
|
137 static LPCSTR typeName[] = { "PROCESS_DETACH", "PROCESS_ATTACH",
|
|
138 "THREAD_ATTACH", "THREAD_DETACH" };
|
|
139 assert( wm );
|
|
140
|
|
141
|
|
142 /* Skip calls for modules loaded with special load flags */
|
|
143
|
|
144 if ( ( wm->flags & WINE_MODREF_DONT_RESOLVE_REFS )
|
|
145 || ( wm->flags & WINE_MODREF_LOAD_AS_DATAFILE ) )
|
|
146 return TRUE;
|
|
147
|
|
148
|
|
149 TRACE("(%s,%s,%p) - CALL\n", wm->modname, typeName[type], lpReserved );
|
|
150
|
|
151 /* Call the initialization routine */
|
|
152 switch ( wm->type )
|
|
153 {
|
|
154 case MODULE32_PE:
|
|
155 retv = PE_InitDLL( wm, type, lpReserved );
|
|
156 break;
|
|
157
|
|
158 case MODULE32_ELF:
|
|
159 /* no need to do that, dlopen() already does */
|
|
160 break;
|
|
161
|
|
162 default:
|
|
163 ERR("wine_modref type %d not handled.\n", wm->type );
|
|
164 retv = FALSE;
|
|
165 break;
|
|
166 }
|
|
167
|
|
168 /* The state of the module list may have changed due to the call
|
|
169 to PE_InitDLL. We cannot assume that this module has not been
|
|
170 deleted. */
|
|
171 TRACE("(%p,%s,%p) - RETURN %d\n", wm, typeName[type], lpReserved, retv );
|
|
172
|
|
173 return retv;
|
|
174 }
|
|
175
|
|
176 /*************************************************************************
|
|
177 * MODULE_DllProcessAttach
|
|
178 *
|
|
179 * Send the process attach notification to all DLLs the given module
|
|
180 * depends on (recursively). This is somewhat complicated due to the fact that
|
|
181 *
|
|
182 * - we have to respect the module dependencies, i.e. modules implicitly
|
|
183 * referenced by another module have to be initialized before the module
|
|
184 * itself can be initialized
|
|
185 *
|
|
186 * - the initialization routine of a DLL can itself call LoadLibrary,
|
|
187 * thereby introducing a whole new set of dependencies (even involving
|
|
188 * the 'old' modules) at any time during the whole process
|
|
189 *
|
|
190 * (Note that this routine can be recursively entered not only directly
|
|
191 * from itself, but also via LoadLibrary from one of the called initialization
|
|
192 * routines.)
|
|
193 *
|
|
194 * Furthermore, we need to rearrange the main WINE_MODREF list to allow
|
|
195 * the process *detach* notifications to be sent in the correct order.
|
|
196 * This must not only take into account module dependencies, but also
|
|
197 * 'hidden' dependencies created by modules calling LoadLibrary in their
|
|
198 * attach notification routine.
|
|
199 *
|
|
200 * The strategy is rather simple: we move a WINE_MODREF to the head of the
|
|
201 * list after the attach notification has returned. This implies that the
|
|
202 * detach notifications are called in the reverse of the sequence the attach
|
|
203 * notifications *returned*.
|
|
204 *
|
|
205 * NOTE: Assumes that the process critical section is held!
|
|
206 *
|
|
207 */
|
|
208 WIN_BOOL MODULE_DllProcessAttach( WINE_MODREF *wm, LPVOID lpReserved )
|
|
209 {
|
|
210 WIN_BOOL retv = TRUE;
|
|
211 int i;
|
|
212 assert( wm );
|
|
213
|
|
214 /* prevent infinite recursion in case of cyclical dependencies */
|
|
215 if ( ( wm->flags & WINE_MODREF_MARKER )
|
|
216 || ( wm->flags & WINE_MODREF_PROCESS_ATTACHED ) )
|
|
217 return retv;
|
|
218
|
|
219 TRACE("(%s,%p) - START\n", wm->modname, lpReserved );
|
|
220
|
|
221 /* Tag current MODREF to prevent recursive loop */
|
|
222 wm->flags |= WINE_MODREF_MARKER;
|
|
223
|
|
224 /* Recursively attach all DLLs this one depends on */
|
|
225 /* for ( i = 0; retv && i < wm->nDeps; i++ )
|
|
226 if ( wm->deps[i] )
|
|
227 retv = MODULE_DllProcessAttach( wm->deps[i], lpReserved );
|
|
228 */
|
|
229 /* Call DLL entry point */
|
|
230
|
|
231 //local_wm=wm;
|
|
232 if(local_wm)
|
|
233 {
|
|
234 local_wm->next=malloc(sizeof(modref_list));
|
|
235 local_wm->next->prev=local_wm;
|
|
236 local_wm->next->next=NULL;
|
|
237 local_wm->next->wm=wm;
|
|
238 local_wm=local_wm->next;
|
|
239 }
|
|
240 else
|
|
241 {
|
|
242 local_wm=malloc(sizeof(modref_list));
|
|
243 local_wm->next=local_wm->prev=NULL;
|
|
244 local_wm->wm=wm;
|
|
245 }
|
|
246 /* Remove recursion flag */
|
|
247 wm->flags &= ~WINE_MODREF_MARKER;
|
|
248
|
|
249 if ( retv )
|
|
250 {
|
|
251 retv = MODULE_InitDll( wm, DLL_PROCESS_ATTACH, lpReserved );
|
|
252 if ( retv )
|
|
253 wm->flags |= WINE_MODREF_PROCESS_ATTACHED;
|
|
254 }
|
|
255
|
|
256
|
|
257 TRACE("(%s,%p) - END\n", wm->modname, lpReserved );
|
|
258
|
|
259 return retv;
|
|
260 }
|
|
261
|
|
262 /*************************************************************************
|
|
263 * MODULE_DllProcessDetach
|
|
264 *
|
|
265 * Send DLL process detach notifications. See the comment about calling
|
|
266 * sequence at MODULE_DllProcessAttach. Unless the bForceDetach flag
|
|
267 * is set, only DLLs with zero refcount are notified.
|
|
268 */
|
|
269 void MODULE_DllProcessDetach( WINE_MODREF* wm, WIN_BOOL bForceDetach, LPVOID lpReserved )
|
|
270 {
|
|
271 // WINE_MODREF *wm=local_wm;
|
|
272 wm->flags &= ~WINE_MODREF_PROCESS_ATTACHED;
|
|
273 MODULE_InitDll( wm, DLL_PROCESS_DETACH, lpReserved );
|
|
274 }
|
|
275
|
|
276
|
|
277 /***********************************************************************
|
|
278 * LoadLibraryExA (KERNEL32)
|
|
279 */
|
|
280 HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
|
|
281 {
|
|
282 WINE_MODREF *wm;
|
|
283
|
|
284 if(!libname)
|
|
285 {
|
|
286 SetLastError(ERROR_INVALID_PARAMETER);
|
|
287 return 0;
|
|
288 }
|
|
289 // if(fs_installed==0)
|
|
290 // install_fs();
|
|
291
|
|
292
|
|
293 wm = MODULE_LoadLibraryExA( libname, hfile, flags );
|
|
294 if ( wm )
|
|
295 {
|
|
296 if ( !MODULE_DllProcessAttach( wm, NULL ) )
|
|
297 {
|
|
298 WARN_(module)("Attach failed for module '%s', \n", libname);
|
|
299 MODULE_FreeLibrary(wm);
|
|
300 SetLastError(ERROR_DLL_INIT_FAILED);
|
|
301 MODULE_RemoveFromList(wm);
|
|
302 wm = NULL;
|
|
303 }
|
|
304 }
|
|
305
|
|
306 return wm ? wm->module : 0;
|
|
307 }
|
|
308
|
|
309
|
|
310 /***********************************************************************
|
|
311 * MODULE_LoadLibraryExA (internal)
|
|
312 *
|
|
313 * Load a PE style module according to the load order.
|
|
314 *
|
|
315 * The HFILE parameter is not used and marked reserved in the SDK. I can
|
|
316 * only guess that it should force a file to be mapped, but I rather
|
|
317 * ignore the parameter because it would be extremely difficult to
|
|
318 * integrate this with different types of module represenations.
|
|
319 *
|
|
320 */
|
|
321 WINE_MODREF *MODULE_LoadLibraryExA( LPCSTR libname, HFILE hfile, DWORD flags )
|
|
322 {
|
|
323 DWORD err = GetLastError();
|
|
324 WINE_MODREF *pwm;
|
|
325 int i;
|
|
326 // module_loadorder_t *plo;
|
|
327
|
|
328
|
|
329 SetLastError( ERROR_FILE_NOT_FOUND );
|
|
330 TRACE("Trying native dll '%s'\n", libname);
|
|
331 pwm = PE_LoadLibraryExA(libname, flags);
|
|
332 #ifdef HAVE_LIBDL
|
|
333 if(!pwm)
|
|
334 {
|
|
335 TRACE("Trying ELF dll '%s'\n", libname);
|
|
336 pwm=(WINE_MODREF*)ELFDLL_LoadLibraryExA(libname, flags);
|
|
337 }
|
|
338 #endif
|
|
339 // printf("0x%08x\n", pwm);
|
|
340 // break;
|
|
341 if(pwm)
|
|
342 {
|
|
343 /* Initialize DLL just loaded */
|
|
344 TRACE("Loaded module '%s' at 0x%08x, \n", libname, pwm->module);
|
|
345 /* Set the refCount here so that an attach failure will */
|
|
346 /* decrement the dependencies through the MODULE_FreeLibrary call. */
|
|
347 pwm->refCount++;
|
|
348
|
|
349 SetLastError( err ); /* restore last error */
|
|
350 return pwm;
|
|
351 }
|
|
352
|
|
353
|
|
354 WARN("Failed to load module '%s'; error=0x%08lx, \n", libname, GetLastError());
|
|
355 return NULL;
|
|
356 }
|
|
357
|
|
358 /***********************************************************************
|
|
359 * LoadLibraryA (KERNEL32)
|
|
360 */
|
|
361 HMODULE WINAPI LoadLibraryA(LPCSTR libname) {
|
|
362 return LoadLibraryExA(libname,0,0);
|
|
363 }
|
|
364
|
|
365
|
|
366 /***********************************************************************
|
|
367 * FreeLibrary
|
|
368 */
|
|
369 WIN_BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
|
|
370 {
|
|
371 WIN_BOOL retv = FALSE;
|
|
372 WINE_MODREF *wm;
|
|
373
|
|
374 wm=MODULE32_LookupHMODULE(hLibModule);
|
|
375 // wm=local_wm;
|
|
376
|
|
377 if ( !wm || !hLibModule )
|
|
378 {
|
|
379 SetLastError( ERROR_INVALID_HANDLE );
|
|
380 return 0;
|
|
381 }
|
|
382 else
|
|
383 retv = MODULE_FreeLibrary( wm );
|
|
384
|
|
385 MODULE_RemoveFromList(wm);
|
|
386
|
128
|
387 /* garbage... */
|
|
388 if (local_wm == NULL) my_garbagecollection();
|
|
389
|
1
|
390 return retv;
|
|
391 }
|
|
392
|
|
393 /***********************************************************************
|
|
394 * MODULE_DecRefCount
|
|
395 *
|
|
396 * NOTE: Assumes that the process critical section is held!
|
|
397 */
|
|
398 static void MODULE_DecRefCount( WINE_MODREF *wm )
|
|
399 {
|
|
400 int i;
|
|
401
|
|
402 if ( wm->flags & WINE_MODREF_MARKER )
|
|
403 return;
|
|
404
|
|
405 if ( wm->refCount <= 0 )
|
|
406 return;
|
|
407
|
|
408 --wm->refCount;
|
|
409 TRACE("(%s) refCount: %d\n", wm->modname, wm->refCount );
|
|
410
|
|
411 if ( wm->refCount == 0 )
|
|
412 {
|
|
413 wm->flags |= WINE_MODREF_MARKER;
|
|
414
|
|
415 for ( i = 0; i < wm->nDeps; i++ )
|
|
416 if ( wm->deps[i] )
|
|
417 MODULE_DecRefCount( wm->deps[i] );
|
|
418
|
|
419 wm->flags &= ~WINE_MODREF_MARKER;
|
|
420 }
|
|
421 }
|
|
422
|
|
423 /***********************************************************************
|
|
424 * MODULE_FreeLibrary
|
|
425 *
|
|
426 * NOTE: Assumes that the process critical section is held!
|
|
427 */
|
|
428 WIN_BOOL MODULE_FreeLibrary( WINE_MODREF *wm )
|
|
429 {
|
|
430 TRACE("(%s) - START\n", wm->modname );
|
|
431
|
|
432 /* Recursively decrement reference counts */
|
|
433 //MODULE_DecRefCount( wm );
|
|
434
|
|
435 /* Call process detach notifications */
|
|
436 MODULE_DllProcessDetach( wm, FALSE, NULL );
|
|
437
|
|
438 PE_UnloadLibrary(wm);
|
|
439
|
|
440 TRACE("END\n");
|
|
441
|
|
442 return TRUE;
|
|
443 }
|
|
444
|
|
445 /***********************************************************************
|
|
446 * GetProcAddress (KERNEL32.257)
|
|
447 */
|
|
448 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
|
|
449 {
|
|
450 return MODULE_GetProcAddress( hModule, function, TRUE );
|
|
451 }
|
|
452
|
|
453 /***********************************************************************
|
|
454 * MODULE_GetProcAddress (internal)
|
|
455 */
|
|
456 FARPROC MODULE_GetProcAddress(
|
|
457 HMODULE hModule, /* [in] current module handle */
|
|
458 LPCSTR function, /* [in] function to be looked up */
|
|
459 WIN_BOOL snoop )
|
|
460 {
|
|
461 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
|
|
462 // WINE_MODREF *wm=local_wm;
|
|
463 FARPROC retproc;
|
|
464
|
|
465 if (HIWORD(function))
|
|
466 TRACE_(win32)("(%08lx,%s)\n",(DWORD)hModule,function);
|
|
467 else
|
|
468 TRACE_(win32)("(%08lx,%p)\n",(DWORD)hModule,function);
|
|
469 if (!wm) {
|
|
470 SetLastError(ERROR_INVALID_HANDLE);
|
|
471 return (FARPROC)0;
|
|
472 }
|
|
473 switch (wm->type)
|
|
474 {
|
|
475 case MODULE32_PE:
|
|
476 retproc = PE_FindExportedFunction( wm, function, snoop );
|
|
477 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
|
|
478 return retproc;
|
|
479 #ifdef HAVE_LIBDL
|
|
480 case MODULE32_ELF:
|
|
481 retproc = (FARPROC) dlsym( wm->module, function);
|
|
482 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
|
|
483 return retproc;
|
|
484 #endif
|
|
485 default:
|
|
486 ERR("wine_modref type %d not handled.\n",wm->type);
|
|
487 SetLastError(ERROR_INVALID_HANDLE);
|
|
488 return (FARPROC)0;
|
|
489 }
|
|
490 }
|
|
491
|