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 FILE_strcasecmp(((module_loadorder_t
*)s1
)->modulename
,
102 ((module_loadorder_t
*)s2
)->modulename
);
106 /***************************************************************************
107 * get_tok (internal, static)
109 * strtok wrapper for non-destructive buffer writing.
110 * NOTE: strtok is not reentrant and therefore this code is neither.
112 static char *get_tok(const char *str
, const char *delim
)
114 static char *buf
= NULL
;
122 HeapFree(GetProcessHeap(), 0, buf
);
128 buf
= HEAP_strdupA(GetProcessHeap(), 0, str
);
129 cptr
= strtok(buf
, delim
);
133 cptr
= strtok(NULL
, delim
);
138 HeapFree(GetProcessHeap(), 0, buf
);
145 /***************************************************************************
146 * ParseLoadOrder (internal, static)
148 * Parses the loadorder options from the configuration and puts it into
151 static BOOL
ParseLoadOrder(char *order
, module_loadorder_t
*mlo
)
157 memset(mlo
->loadorder
, 0, sizeof(mlo
->loadorder
));
159 cptr
= get_tok(order
, ", \t");
162 char type
= MODULE_LOADORDER_INVALID
;
164 if(n
>= MODULE_LOADORDER_NTYPES
)
166 ERR("More than existing %d module-types specified, rest ignored", MODULE_LOADORDER_NTYPES
);
172 case 'N': /* Native */
173 case 'n': type
= MODULE_LOADORDER_DLL
; break;
175 case 'E': /* Elfdll */
177 if (!warn
++) MESSAGE("Load order 'elfdll' no longer supported, ignored\n");
180 case 's': type
= MODULE_LOADORDER_SO
; break;
182 case 'B': /* Builtin */
183 case 'b': type
= MODULE_LOADORDER_BI
; break;
186 ERR("Invalid load order module-type '%s', ignored\n", cptr
);
189 if(type
!= MODULE_LOADORDER_INVALID
)
191 mlo
->loadorder
[n
++] = type
;
193 cptr
= get_tok(NULL
, ", \t");
199 /***************************************************************************
200 * AddLoadOrder (internal, static)
202 * Adds an entry in the list of overrides. If the entry exists, then the
203 * override parameter determines whether it will be overwritten.
205 static BOOL
AddLoadOrder(module_loadorder_t
*plo
, BOOL override
)
209 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
211 for(i
= 0; i
< nmodule_loadorder
; i
++)
213 if(!cmp_sort_func(plo
, &module_loadorder
[i
]))
216 ERR("Module '%s' is already in the list of overrides, using first definition\n", plo
->modulename
);
218 memcpy(module_loadorder
[i
].loadorder
, plo
->loadorder
, sizeof(plo
->loadorder
));
223 if(nmodule_loadorder
>= nmodule_loadorder_alloc
)
225 /* No space in current array, make it larger */
226 nmodule_loadorder_alloc
+= LOADORDER_ALLOC_CLUSTER
;
227 module_loadorder
= (module_loadorder_t
*)HeapReAlloc(GetProcessHeap(),
230 nmodule_loadorder_alloc
* sizeof(module_loadorder_t
));
231 if(!module_loadorder
)
233 MESSAGE("Virtual memory exhausted\n");
237 memcpy(module_loadorder
[nmodule_loadorder
].loadorder
, plo
->loadorder
, sizeof(plo
->loadorder
));
238 module_loadorder
[nmodule_loadorder
].modulename
= HEAP_strdupA(GetProcessHeap(), 0, plo
->modulename
);
244 /***************************************************************************
245 * AddLoadOrderSet (internal, static)
247 * Adds a set of entries in the list of overrides from the key parameter.
248 * If the entry exists, then the override parameter determines whether it
249 * will be overwritten.
251 static BOOL
AddLoadOrderSet(char *key
, char *order
, BOOL override
)
253 module_loadorder_t ldo
;
256 /* Parse the loadorder before the rest because strtok is not reentrant */
257 if(!ParseLoadOrder(order
, &ldo
))
260 cptr
= get_tok(key
, ", \t");
263 char *ext
= strrchr(cptr
, '.');
266 if(strlen(ext
) == 4 &&
267 (!FILE_strcasecmp(ext
, ".dll") || !FILE_strcasecmp(ext
, ".exe")))
268 MESSAGE("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr
);
271 ldo
.modulename
= cptr
;
272 if(!AddLoadOrder(&ldo
, override
))
274 cptr
= get_tok(NULL
, ", \t");
280 /***************************************************************************
281 * ParseCommandlineOverrides (internal, static)
283 * The commandline is in the form:
284 * name[,name,...]=native[,b,...][+...]
286 static BOOL
ParseCommandlineOverrides(void)
294 if(!Options
.dllFlags
)
297 cpy
= HEAP_strdupA(GetProcessHeap(), 0, Options
.dllFlags
);
300 for(; next
; key
= next
)
302 next
= strchr(key
, '+');
308 value
= strchr(key
, '=');
317 TRACE("Commandline override '%s' = '%s'\n", key
, value
);
319 if(!AddLoadOrderSet(key
, value
, TRUE
))
326 HeapFree(GetProcessHeap(), 0, cpy
);
331 /***************************************************************************
332 * MODULE_InitLoadOrder (internal)
334 * Initialize the load order from the wine.conf file.
335 * The section has the following format:
340 * DefaultLoadOrder=native,so,builtin
341 * A comma separated list of module types to try to load in that specific
342 * order. The DefaultLoadOrder key is used as a fallback when a module is
343 * not specified explicitly. If the DefaultLoadOrder key is not found,
344 * then the order "dll,so,bi" is used
345 * The possible module types are:
346 * - native Native windows dll files
347 * - so Native .so libraries mapped to dlls
348 * - builtin Built-in modules
350 * Case is not important and only the first letter of each type is enough to
351 * identify the type n[ative], s[o], b[uiltin]. Also whitespace is
362 * There are no explicit keys defined other than module/library names. A comma
363 * separated list of modules is followed by an assignment of the load-order
364 * for these specific modules. See above for possible types. You should not
365 * specify an extension.
367 * kernel32, gdi32, user32 = builtin
368 * kernel, gdi, user = builtin
369 * comdlg32 = native, builtin
370 * commdlg = native, builtin
371 * version, ver = native, builtin
375 #define BUFFERSIZE 1024
377 BOOL
MODULE_InitLoadOrder(void)
379 char buffer
[BUFFERSIZE
];
383 const struct tagDllPair
*dllpair
;
385 /* Get the default load order */
386 nbuffer
= PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,b,s", buffer
, sizeof(buffer
));
389 MESSAGE("MODULE_InitLoadOrder: mysteriously read nothing from default loadorder\n");
393 TRACE("Setting default loadorder=%s\n", buffer
);
395 if(!ParseLoadOrder(buffer
, &default_loadorder
))
397 default_loadorder
.modulename
= "<none>";
401 for (i
=0;DefaultDllOverrides
[i
].key
;i
++)
403 DefaultDllOverrides
[i
].key
,
404 DefaultDllOverrides
[i
].value
,
409 /* Read the explicitely defined orders for specific modules as an entire section */
411 while (PROFILE_EnumWineIniString( "DllOverrides", idx
++, key
, sizeof(key
),
412 buffer
, sizeof(buffer
)))
414 TRACE("Key '%s' uses override '%s'\n", key
, buffer
);
415 if(!AddLoadOrderSet(key
, buffer
, TRUE
))
419 /* Add the commandline overrides to the pool */
420 if(!ParseCommandlineOverrides())
422 MESSAGE( "Syntax: -dll name[,name[,...]]={native|so|builtin}[,{n|s|b}[,...]][+...]\n"
423 " - 'name' is the name of any dll without extension\n"
424 " - the order of loading (native, so and builtin) can be abbreviated\n"
425 " with the first letter\n"
426 " - different loadorders for different dlls can be specified by seperating the\n"
427 " commandline entries with a '+'\n"
429 " -dll comdlg32,commdlg=n+shell,shell32=b\n"
434 /* Sort the array for quick lookup */
435 qsort(module_loadorder
, nmodule_loadorder
, sizeof(module_loadorder
[0]), cmp_sort_func
);
437 /* Check the pairs of dlls */
439 while (dllpair
->dll1
)
441 module_loadorder_t
*plo1
, *plo2
;
442 plo1
= MODULE_GetLoadOrder(dllpair
->dll1
, FALSE
);
443 plo2
= MODULE_GetLoadOrder(dllpair
->dll2
, FALSE
);
444 assert(plo1
&& plo2
);
445 if(memcmp(plo1
->loadorder
, plo2
->loadorder
, sizeof(plo1
->loadorder
)))
446 MESSAGE("Warning: Modules '%s' and '%s' have different loadorder which may cause trouble\n", dllpair
->dll1
, dllpair
->dll2
);
453 static char types
[] = "-NSB";
455 for(i
= 0; i
< nmodule_loadorder
; i
++)
457 DPRINTF("%3d: %-12s:", i
, module_loadorder
[i
].modulename
);
458 for(j
= 0; j
< MODULE_LOADORDER_NTYPES
; j
++)
459 DPRINTF(" %c", types
[module_loadorder
[i
].loadorder
[j
] % (MODULE_LOADORDER_NTYPES
+1)]);
468 /***************************************************************************
469 * MODULE_GetLoadOrder (internal)
471 * Locate the loadorder of a module.
472 * Any path is stripped from the path-argument and so are the extension
473 * '.dll' and '.exe'. A lookup in the table can yield an override for
474 * the specific dll. Otherwise the default load order is returned.
476 module_loadorder_t
*MODULE_GetLoadOrder(const char *path
, BOOL win32
)
478 module_loadorder_t lo
, *tmp
;
480 char sysdir
[MAX_PATH
+1];
485 TRACE("looking for %s\n", path
);
487 assert(path
!= NULL
);
489 if ( ! GetSystemDirectoryA ( sysdir
, MAX_PATH
) )
490 return &default_loadorder
; /* Hmmm ... */
492 /* Strip path information for 16 bit modules or if the module
493 resides in the system directory */
494 if ( !win32
|| !FILE_strncasecmp ( sysdir
, path
, strlen (sysdir
) ) )
497 cptr
= strrchr(path
, '\\');
499 name
= strrchr(path
, '/');
501 name
= strrchr(cptr
, '/');
504 name
= cptr
? cptr
+1 : (char *)path
;
508 if((cptr
= strchr(name
, ':')) != NULL
) /* Also strip drive if in format 'C:MODULE.DLL' */
515 if(len
>= sizeof(fname
) || len
<= 0)
517 ERR("Path '%s' -> '%s' reduces to zilch or just too large...\n", path
, name
);
518 return &default_loadorder
;
522 if(len
>= 4 && (!FILE_strcasecmp(fname
+len
-4, ".dll") || !FILE_strcasecmp(fname
+len
-4, ".exe")))
525 lo
.modulename
= fname
;
526 tmp
= bsearch(&lo
, module_loadorder
, nmodule_loadorder
, sizeof(module_loadorder
[0]), cmp_sort_func
);
528 TRACE("Looking for '%s' (%s), found '%s'\n", path
, fname
, tmp
? tmp
->modulename
: "<nothing>");
531 return &default_loadorder
;