2 * Module/Library loadorder
4 * Copyright 1999 Bertho Stultiens
14 #include "loadorder.h"
18 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(module
);
23 /* #define DEBUG_LOADORDER */
25 #define LOADORDER_ALLOC_CLUSTER 32 /* Allocate with 32 entries at a time */
27 static module_loadorder_t default_loadorder
;
28 static module_loadorder_t
*module_loadorder
= NULL
;
29 static int nmodule_loadorder
= 0;
30 static int nmodule_loadorder_alloc
= 0;
32 /* DLL order is irrelevant ! Gets sorted later. */
33 static struct tagDllOverride
{
35 } DefaultDllOverrides
[] = {
37 {"kernel32,gdi32,user32", "builtin"},
38 {"krnl386,gdi,user", "builtin"},
39 {"toolhelp", "builtin"},
40 {"windebug", "native,builtin"},
41 {"system,display", "builtin"},
42 {"w32skrnl,wow32", "builtin"},
43 {"advapi32,crtdll,ntdll", "builtin,native"},
44 {"lz32,lzexpand", "builtin,native"},
45 {"version,ver", "builtin,native"},
47 {"comdlg32,commdlg", "builtin,native"},
48 {"shell32,shell", "builtin,native"},
49 {"shlwapi", "native,builtin"},
50 {"shfolder", "builtin,native"},
51 {"comctl32,commctrl", "builtin,native"},
53 {"wsock32,ws2_32,winsock", "builtin"},
56 {"ddraw,dinput,dsound", "builtin,native"},
57 {"winmm,mmsystem", "builtin"},
58 {"msvfw32,msvideo", "builtin,native"},
59 {"mcicda.drv,mciseq.drv", "builtin,native"},
60 {"mciwave.drv", "builtin,native"},
61 {"mciavi.drv,mcianim.drv", "native,builtin"},
62 {"msacm.drv,midimap.drv", "builtin,native"},
63 {"opengl32", "builtin,native"},
64 /* we have to use libglideXx.so instead of glideXx.dll ... */
65 {"glide2x,glide3x", "so,native"},
67 {"mpr,winspool.drv", "builtin,native"},
68 {"wnaspi32,winaspi", "builtin"},
69 {"odbc32", "builtin"},
70 {"rpcrt4", "native,builtin"},
71 /* non-windows DLLs */
72 {"wineps,wprocs,x11drv", "builtin"},
76 static const struct tagDllPair
{
77 const char *dll1
, *dll2
;
79 { "krnl386", "kernel32" },
82 { "commdlg", "comdlg32" },
83 { "commctrl", "comctl32" },
85 { "shell", "shell32" },
86 { "lzexpand", "lz32" },
87 { "mmsystem", "winmm" },
88 { "msvideo", "msvfw32" },
89 { "winsock", "wsock32" },
93 /***************************************************************************
94 * cmp_sort_func (internal, static)
96 * Sorting and comparing function used in sort and search of loadorder
99 static int cmp_sort_func(const void *s1
, const void *s2
)
101 return strcasecmp(((module_loadorder_t
*)s1
)->modulename
, ((module_loadorder_t
*)s2
)->modulename
);
105 /***************************************************************************
106 * get_tok (internal, static)
108 * strtok wrapper for non-destructive buffer writing.
109 * NOTE: strtok is not reentrant and therefore this code is neither.
111 static char *get_tok(const char *str
, const char *delim
)
113 static char *buf
= NULL
;
121 HeapFree(GetProcessHeap(), 0, buf
);
127 buf
= HEAP_strdupA(GetProcessHeap(), 0, str
);
128 cptr
= strtok(buf
, delim
);
132 cptr
= strtok(NULL
, delim
);
137 HeapFree(GetProcessHeap(), 0, buf
);
144 /***************************************************************************
145 * ParseLoadOrder (internal, static)
147 * Parses the loadorder options from the configuration and puts it into
150 static BOOL
ParseLoadOrder(char *order
, module_loadorder_t
*mlo
)
156 memset(mlo
->loadorder
, 0, sizeof(mlo
->loadorder
));
158 cptr
= get_tok(order
, ", \t");
161 char type
= MODULE_LOADORDER_INVALID
;
163 if(n
>= MODULE_LOADORDER_NTYPES
)
165 ERR("More than existing %d module-types specified, rest ignored", MODULE_LOADORDER_NTYPES
);
171 case 'N': /* Native */
172 case 'n': type
= MODULE_LOADORDER_DLL
; break;
174 case 'E': /* Elfdll */
176 if (!warn
++) MESSAGE("Load order 'elfdll' no longer support, ignored\n");
179 case 's': type
= MODULE_LOADORDER_SO
; break;
181 case 'B': /* Builtin */
182 case 'b': type
= MODULE_LOADORDER_BI
; break;
185 ERR("Invalid load order module-type '%s', ignored\n", cptr
);
188 if(type
!= MODULE_LOADORDER_INVALID
)
190 mlo
->loadorder
[n
++] = type
;
192 cptr
= get_tok(NULL
, ", \t");
198 /***************************************************************************
199 * AddLoadOrder (internal, static)
201 * Adds an entry in the list of overrides. If the entry exists, then the
202 * override parameter determines whether it will be overwritten.
204 static BOOL
AddLoadOrder(module_loadorder_t
*plo
, BOOL override
)
208 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
210 for(i
= 0; i
< nmodule_loadorder
; i
++)
212 if(!cmp_sort_func(plo
, &module_loadorder
[i
]))
215 ERR("Module '%s' is already in the list of overrides, using first definition\n", plo
->modulename
);
217 memcpy(module_loadorder
[i
].loadorder
, plo
->loadorder
, sizeof(plo
->loadorder
));
222 if(nmodule_loadorder
>= nmodule_loadorder_alloc
)
224 /* No space in current array, make it larger */
225 nmodule_loadorder_alloc
+= LOADORDER_ALLOC_CLUSTER
;
226 module_loadorder
= (module_loadorder_t
*)HeapReAlloc(GetProcessHeap(),
229 nmodule_loadorder_alloc
* sizeof(module_loadorder_t
));
230 if(!module_loadorder
)
232 MESSAGE("Virtual memory exhausted\n");
236 memcpy(module_loadorder
[nmodule_loadorder
].loadorder
, plo
->loadorder
, sizeof(plo
->loadorder
));
237 module_loadorder
[nmodule_loadorder
].modulename
= HEAP_strdupA(GetProcessHeap(), 0, plo
->modulename
);
243 /***************************************************************************
244 * AddLoadOrderSet (internal, static)
246 * Adds a set of entries in the list of overrides from the key parameter.
247 * If the entry exists, then the override parameter determines whether it
248 * will be overwritten.
250 static BOOL
AddLoadOrderSet(char *key
, char *order
, BOOL override
)
252 module_loadorder_t ldo
;
255 /* Parse the loadorder before the rest because strtok is not reentrant */
256 if(!ParseLoadOrder(order
, &ldo
))
259 cptr
= get_tok(key
, ", \t");
262 char *ext
= strrchr(cptr
, '.');
265 if(strlen(ext
) == 4 && (!strcasecmp(ext
, ".dll") || !strcasecmp(ext
, ".exe")))
266 MESSAGE("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr
);
269 ldo
.modulename
= cptr
;
270 if(!AddLoadOrder(&ldo
, override
))
272 cptr
= get_tok(NULL
, ", \t");
278 /***************************************************************************
279 * ParseCommandlineOverrides (internal, static)
281 * The commandline is in the form:
282 * name[,name,...]=native[,b,...][+...]
284 static BOOL
ParseCommandlineOverrides(void)
292 if(!Options
.dllFlags
)
295 cpy
= HEAP_strdupA(GetProcessHeap(), 0, Options
.dllFlags
);
298 for(; next
; key
= next
)
300 next
= strchr(key
, '+');
306 value
= strchr(key
, '=');
315 TRACE("Commandline override '%s' = '%s'\n", key
, value
);
317 if(!AddLoadOrderSet(key
, value
, TRUE
))
324 HeapFree(GetProcessHeap(), 0, cpy
);
329 /***************************************************************************
330 * MODULE_InitLoadOrder (internal)
332 * Initialize the load order from the wine.conf file.
333 * The section has the following format:
338 * EXTRA_LD_LIBRARY_PATH=/usr/local/lib/wine[:/more/path/to/search[:...]]
339 * The path will be appended to any existing LD_LIBRARY_PATH from the
340 * environment (see note in code below).
342 * DefaultLoadOrder=native,so,builtin
343 * A comma separated list of module types to try to load in that specific
344 * order. The DefaultLoadOrder key is used as a fallback when a module is
345 * not specified explicitly. If the DefaultLoadOrder key is not found,
346 * then the order "dll,so,bi" is used
347 * The possible module types are:
348 * - native Native windows dll files
349 * - so Native .so libraries mapped to dlls
350 * - builtin Built-in modules
352 * Case is not important and only the first letter of each type is enough to
353 * identify the type n[ative], s[o], b[uiltin]. Also whitespace is
364 * There are no explicit keys defined other than module/library names. A comma
365 * separated list of modules is followed by an assignment of the load-order
366 * for these specific modules. See above for possible types. You should not
367 * specify an extension.
369 * kernel32, gdi32, user32 = builtin
370 * kernel, gdi, user = builtin
371 * comdlg32 = native, builtin
372 * commdlg = native, builtin
373 * version, ver = native, builtin
377 #define BUFFERSIZE 1024
379 BOOL
MODULE_InitLoadOrder(void)
381 char buffer
[BUFFERSIZE
];
385 const struct tagDllPair
*dllpair
;
387 #if defined(HAVE_DL_API)
388 /* Get/set the new LD_LIBRARY_PATH */
389 nbuffer
= PROFILE_GetWineIniString("DllDefaults", "EXTRA_LD_LIBRARY_PATH", "", buffer
, sizeof(buffer
));
393 extra_ld_library_path
= HEAP_strdupA(GetProcessHeap(), 0, buffer
);
394 TRACE("Setting extra LD_LIBRARY_PATH=%s\n", buffer
);
398 /* Get the default load order */
399 nbuffer
= PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,b,s", buffer
, sizeof(buffer
));
402 MESSAGE("MODULE_InitLoadOrder: mysteriously read nothing from default loadorder\n");
406 TRACE("Setting default loadorder=%s\n", buffer
);
408 if(!ParseLoadOrder(buffer
, &default_loadorder
))
410 default_loadorder
.modulename
= "<none>";
414 for (i
=0;DefaultDllOverrides
[i
].key
;i
++)
416 DefaultDllOverrides
[i
].key
,
417 DefaultDllOverrides
[i
].value
,
422 /* Read the explicitely defined orders for specific modules as an entire section */
424 while (PROFILE_EnumWineIniString( "DllOverrides", idx
++, key
, sizeof(key
),
425 buffer
, sizeof(buffer
)))
427 TRACE("Key '%s' uses override '%s'\n", key
, buffer
);
428 if(!AddLoadOrderSet(key
, buffer
, TRUE
))
432 /* Add the commandline overrides to the pool */
433 if(!ParseCommandlineOverrides())
435 MESSAGE( "Syntax: -dll name[,name[,...]]={native|so|builtin}[,{n|s|b}[,...]][+...]\n"
436 " - 'name' is the name of any dll without extension\n"
437 " - the order of loading (native, so and builtin) can be abbreviated\n"
438 " with the first letter\n"
439 " - different loadorders for different dlls can be specified by seperating the\n"
440 " commandline entries with a '+'\n"
442 " -dll comdlg32,commdlg=n+shell,shell32=b\n"
447 /* Sort the array for quick lookup */
448 qsort(module_loadorder
, nmodule_loadorder
, sizeof(module_loadorder
[0]), cmp_sort_func
);
450 /* Check the pairs of dlls */
452 while (dllpair
->dll1
)
454 module_loadorder_t
*plo1
, *plo2
;
455 plo1
= MODULE_GetLoadOrder(dllpair
->dll1
, FALSE
);
456 plo2
= MODULE_GetLoadOrder(dllpair
->dll2
, FALSE
);
457 assert(plo1
&& plo2
);
458 if(memcmp(plo1
->loadorder
, plo2
->loadorder
, sizeof(plo1
->loadorder
)))
459 MESSAGE("Warning: Modules '%s' and '%s' have different loadorder which may cause trouble\n", dllpair
->dll1
, dllpair
->dll2
);
466 static char types
[] = "-NSB";
468 for(i
= 0; i
< nmodule_loadorder
; i
++)
470 DPRINTF("%3d: %-12s:", i
, module_loadorder
[i
].modulename
);
471 for(j
= 0; j
< MODULE_LOADORDER_NTYPES
; j
++)
472 DPRINTF(" %c", types
[module_loadorder
[i
].loadorder
[j
] % (MODULE_LOADORDER_NTYPES
+1)]);
481 /***************************************************************************
482 * MODULE_GetLoadOrder (internal)
484 * Locate the loadorder of a module.
485 * Any path is stripped from the path-argument and so are the extension
486 * '.dll' and '.exe'. A lookup in the table can yield an override for
487 * the specific dll. Otherwise the default load order is returned.
489 module_loadorder_t
*MODULE_GetLoadOrder(const char *path
, BOOL win32
)
491 module_loadorder_t lo
, *tmp
;
493 char sysdir
[MAX_PATH
+1];
498 TRACE("looking for %s\n", path
);
500 assert(path
!= NULL
);
502 if ( ! GetSystemDirectoryA ( sysdir
, MAX_PATH
) )
503 return &default_loadorder
; /* Hmmm ... */
505 /* Strip path information for 16 bit modules or if the module
506 resides in the system directory */
507 if ( !win32
|| !strncasecmp ( sysdir
, path
, strlen (sysdir
) ) )
510 cptr
= strrchr(path
, '\\');
512 name
= strrchr(path
, '/');
514 name
= strrchr(cptr
, '/');
517 name
= cptr
? cptr
+1 : (char *)path
;
521 if((cptr
= strchr(name
, ':')) != NULL
) /* Also strip drive if in format 'C:MODULE.DLL' */
528 if(len
>= sizeof(fname
) || len
<= 0)
530 ERR("Path '%s' -> '%s' reduces to zilch or just too large...\n", path
, name
);
531 return &default_loadorder
;
535 if(len
>= 4 && (!strcasecmp(fname
+len
-4, ".dll") || !strcasecmp(fname
+len
-4, ".exe")))
538 lo
.modulename
= fname
;
539 tmp
= bsearch(&lo
, module_loadorder
, nmodule_loadorder
, sizeof(module_loadorder
[0]), cmp_sort_func
);
541 TRACE("Looking for '%s' (%s), found '%s'\n", path
, fname
, tmp
? tmp
->modulename
: "<nothing>");
544 return &default_loadorder
;