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 static struct tagDllOverride
{
34 } DefaultDllOverrides
[] = {
35 {"kernel32,gdi32,user32", "builtin"},
36 {"krnl386,gdi,user", "builtin"},
37 {"toolhelp", "builtin"},
38 {"comdlg32,commdlg", "elfdll,builtin,native"},
39 {"version,ver", "elfdll,builtin,native"},
40 {"shell32,shell", "builtin,native"},
41 {"shlwapi", "native,builtin"},
42 {"lz32,lzexpand", "builtin,native"},
43 {"commctrl,comctl32", "builtin,native"},
44 {"wsock32,winsock", "builtin"},
45 {"advapi32,crtdll,ntdll", "builtin,native"},
46 {"mpr,winspool.drv", "builtin,native"},
47 {"ddraw,dinput,dsound", "builtin,native"},
48 {"winmm, mmsystem", "builtin"},
49 {"msvideo, msvfw32", "builtin, native"},
50 {"mcicda.drv, mciseq.drv", "builtin, native"},
51 {"mciwave.drv", "builtin, native"},
52 {"mciavi.drv, mcianim.drv", "native, builtin"},
53 {"msacm.drv, midimap.drv", "builtin, native"},
54 {"w32skrnl", "builtin"},
55 {"wnaspi32,wow32", "builtin"},
56 {"system,display,wprocs ", "builtin"},
57 {"wineps", "builtin"},
59 /* we have to use libglide2x.so instead of glide2x.dll ... */
60 {"glide2x", "so,native"},
61 {"glide3x", "so,native"},
62 {"odbc32", "builtin"},
63 {"opengl32", "builtin,native"},
64 {"shfolder", "builtin,native"},
68 static const struct tagDllPair
{
69 const char *dll1
, *dll2
;
71 { "krnl386", "kernel32" },
74 { "commdlg", "comdlg32" },
75 { "commctrl", "comctl32" },
77 { "shell", "shell32" },
78 { "lzexpand", "lz32" },
79 { "mmsystem", "winmm" },
80 { "msvideo", "msvfw32" },
81 { "winsock", "wsock32" },
85 /***************************************************************************
86 * cmp_sort_func (internal, static)
88 * Sorting and comparing function used in sort and search of loadorder
91 static int cmp_sort_func(const void *s1
, const void *s2
)
93 return strcasecmp(((module_loadorder_t
*)s1
)->modulename
, ((module_loadorder_t
*)s2
)->modulename
);
97 /***************************************************************************
98 * get_tok (internal, static)
100 * strtok wrapper for non-destructive buffer writing.
101 * NOTE: strtok is not reentrant and therefore this code is neither.
103 static char *get_tok(const char *str
, const char *delim
)
105 static char *buf
= NULL
;
113 HeapFree(GetProcessHeap(), 0, buf
);
119 buf
= HEAP_strdupA(GetProcessHeap(), 0, str
);
120 cptr
= strtok(buf
, delim
);
124 cptr
= strtok(NULL
, delim
);
129 HeapFree(GetProcessHeap(), 0, buf
);
136 /***************************************************************************
137 * ParseLoadOrder (internal, static)
139 * Parses the loadorder options from the configuration and puts it into
142 static BOOL
ParseLoadOrder(char *order
, module_loadorder_t
*mlo
)
147 memset(mlo
->loadorder
, 0, sizeof(mlo
->loadorder
));
149 cptr
= get_tok(order
, ", \t");
152 char type
= MODULE_LOADORDER_INVALID
;
154 if(n
>= MODULE_LOADORDER_NTYPES
)
156 ERR("More than existing %d module-types specified, rest ignored", MODULE_LOADORDER_NTYPES
);
162 case 'N': /* Native */
163 case 'n': type
= MODULE_LOADORDER_DLL
; break;
165 case 'E': /* Elfdll */
166 case 'e': type
= MODULE_LOADORDER_ELFDLL
; break;
169 case 's': type
= MODULE_LOADORDER_SO
; break;
171 case 'B': /* Builtin */
172 case 'b': type
= MODULE_LOADORDER_BI
; break;
175 ERR("Invalid load order module-type '%s', ignored\n", cptr
);
178 if(type
!= MODULE_LOADORDER_INVALID
)
180 mlo
->loadorder
[n
++] = type
;
182 cptr
= get_tok(NULL
, ", \t");
188 /***************************************************************************
189 * AddLoadOrder (internal, static)
191 * Adds an entry in the list of overrides. If the entry exists, then the
192 * override parameter determines whether it will be overwritten.
194 static BOOL
AddLoadOrder(module_loadorder_t
*plo
, BOOL override
)
198 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
200 for(i
= 0; i
< nmodule_loadorder
; i
++)
202 if(!cmp_sort_func(plo
, &module_loadorder
[i
]))
205 ERR("Module '%s' is already in the list of overrides, using first definition\n", plo
->modulename
);
207 memcpy(module_loadorder
[i
].loadorder
, plo
->loadorder
, sizeof(plo
->loadorder
));
212 if(nmodule_loadorder
>= nmodule_loadorder_alloc
)
214 /* No space in current array, make it larger */
215 nmodule_loadorder_alloc
+= LOADORDER_ALLOC_CLUSTER
;
216 module_loadorder
= (module_loadorder_t
*)HeapReAlloc(GetProcessHeap(),
219 nmodule_loadorder_alloc
* sizeof(module_loadorder_t
));
220 if(!module_loadorder
)
222 MESSAGE("Virtual memory exhausted\n");
226 memcpy(module_loadorder
[nmodule_loadorder
].loadorder
, plo
->loadorder
, sizeof(plo
->loadorder
));
227 module_loadorder
[nmodule_loadorder
].modulename
= HEAP_strdupA(GetProcessHeap(), 0, plo
->modulename
);
233 /***************************************************************************
234 * AddLoadOrderSet (internal, static)
236 * Adds a set of entries in the list of overrides from the key parameter.
237 * If the entry exists, then the override parameter determines whether it
238 * will be overwritten.
240 static BOOL
AddLoadOrderSet(char *key
, char *order
, BOOL override
)
242 module_loadorder_t ldo
;
245 /* Parse the loadorder before the rest because strtok is not reentrant */
246 if(!ParseLoadOrder(order
, &ldo
))
249 cptr
= get_tok(key
, ", \t");
252 char *ext
= strrchr(cptr
, '.');
255 if(strlen(ext
) == 4 && (!strcasecmp(ext
, ".dll") || !strcasecmp(ext
, ".exe")))
256 MESSAGE("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr
);
259 ldo
.modulename
= cptr
;
260 if(!AddLoadOrder(&ldo
, override
))
262 cptr
= get_tok(NULL
, ", \t");
268 /***************************************************************************
269 * ParseCommandlineOverrides (internal, static)
271 * The commandline is in the form:
272 * name[,name,...]=native[,b,...][:...]
274 static BOOL
ParseCommandlineOverrides(void)
282 if(!Options
.dllFlags
)
285 cpy
= HEAP_strdupA(GetProcessHeap(), 0, Options
.dllFlags
);
288 for(; next
; key
= next
)
290 next
= strchr(key
, ':');
296 value
= strchr(key
, '=');
305 TRACE("Commandline override '%s' = '%s'\n", key
, value
);
307 if(!AddLoadOrderSet(key
, value
, TRUE
))
314 HeapFree(GetProcessHeap(), 0, cpy
);
319 /***************************************************************************
320 * MODULE_InitLoadOrder (internal)
322 * Initialize the load order from the wine.conf file.
323 * The section has the following format:
328 * EXTRA_LD_LIBRARY_PATH=/usr/local/lib/wine[:/more/path/to/search[:...]]
329 * The path will be appended to any existing LD_LIBRARY_PATH from the
330 * environment (see note in code below).
332 * DefaultLoadOrder=native,elfdll,so,builtin
333 * A comma separated list of module types to try to load in that specific
334 * order. The DefaultLoadOrder key is used as a fallback when a module is
335 * not specified explicitly. If the DefaultLoadOrder key is not found,
336 * then the order "dll,elfdll,so,bi" is used
337 * The possible module types are:
338 * - native Native windows dll files
339 * - elfdll Dlls encapsulated in .so libraries
340 * - so Native .so libraries mapped to dlls
341 * - builtin Built-in modules
343 * Case is not important and only the first letter of each type is enough to
344 * identify the type n[ative], e[lfdll], s[o], b[uiltin]. Also whitespace is
349 * native,elfdll,so,builtin
355 * There are no explicit keys defined other than module/library names. A comma
356 * separated list of modules is followed by an assignment of the load-order
357 * for these specific modules. See above for possible types. You should not
358 * specify an extension.
360 * kernel32, gdi32, user32 = builtin
361 * kernel, gdi, user = builtin
362 * comdlg32 = elfdll, native, builtin
363 * commdlg = native, builtin
364 * version, ver = elfdll, native, builtin
368 #define BUFFERSIZE 1024
370 BOOL
MODULE_InitLoadOrder(void)
372 char buffer
[BUFFERSIZE
];
376 const struct tagDllPair
*dllpair
;
378 #if defined(HAVE_DL_API)
379 /* Get/set the new LD_LIBRARY_PATH */
380 nbuffer
= PROFILE_GetWineIniString("DllDefaults", "EXTRA_LD_LIBRARY_PATH", "", buffer
, sizeof(buffer
));
384 extra_ld_library_path
= HEAP_strdupA(GetProcessHeap(), 0, buffer
);
385 TRACE("Setting extra LD_LIBRARY_PATH=%s\n", buffer
);
389 /* Get the default load order */
390 nbuffer
= PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,b,e,s", buffer
, sizeof(buffer
));
393 MESSAGE("MODULE_InitLoadOrder: mysteriously read nothing from default loadorder\n");
397 TRACE("Setting default loadorder=%s\n", buffer
);
399 if(!ParseLoadOrder(buffer
, &default_loadorder
))
401 default_loadorder
.modulename
= "<none>";
405 for (i
=0;DefaultDllOverrides
[i
].key
;i
++)
407 DefaultDllOverrides
[i
].key
,
408 DefaultDllOverrides
[i
].value
,
413 /* Read the explicitely defined orders for specific modules as an entire section */
415 while (PROFILE_EnumWineIniString( "DllOverrides", idx
++, key
, sizeof(key
),
416 buffer
, sizeof(buffer
)))
418 TRACE("Key '%s' uses override '%s'\n", key
, buffer
);
419 if(!AddLoadOrderSet(key
, buffer
, TRUE
))
423 /* Add the commandline overrides to the pool */
424 if(!ParseCommandlineOverrides())
426 MESSAGE( "Syntax: -dll name[,name[,...]]={native|elfdll|so|builtin}[,{n|e|s|b}[,...]][:...]\n"
427 " - 'name' is the name of any dll without extension\n"
428 " - the order of loading (native, elfdll, so and builtin) can be abbreviated\n"
429 " with the first letter\n"
430 " - different loadorders for different dlls can be specified by seperating the\n"
431 " commandline entries with a ':'\n"
433 " -dll comdlg32,commdlg=n:shell,shell32=b\n"
438 /* Sort the array for quick lookup */
439 qsort(module_loadorder
, nmodule_loadorder
, sizeof(module_loadorder
[0]), cmp_sort_func
);
441 /* Check the pairs of dlls */
443 while (dllpair
->dll1
)
445 module_loadorder_t
*plo1
, *plo2
;
446 plo1
= MODULE_GetLoadOrder(dllpair
->dll1
);
447 plo2
= MODULE_GetLoadOrder(dllpair
->dll2
);
448 assert(plo1
&& plo2
);
449 if(memcmp(plo1
->loadorder
, plo2
->loadorder
, sizeof(plo1
->loadorder
)))
450 MESSAGE("Warning: Modules '%s' and '%s' have different loadorder which may cause trouble\n", dllpair
->dll1
, dllpair
->dll2
);
457 static char types
[6] = "-NESB";
459 for(i
= 0; i
< nmodule_loadorder
; i
++)
461 DPRINTF("%3d: %-12s:", i
, module_loadorder
[i
].modulename
);
462 for(j
= 0; j
< MODULE_LOADORDER_NTYPES
; j
++)
463 DPRINTF(" %c", types
[module_loadorder
[i
].loadorder
[j
] % (MODULE_LOADORDER_NTYPES
+1)]);
472 /***************************************************************************
473 * MODULE_GetLoadOrder (internal)
475 * Locate the loadorder of a module.
476 * Any path is stripped from the path-argument and so are the extension
477 * '.dll' and '.exe'. A lookup in the table can yield an override for
478 * the specific dll. Otherwise the default load order is returned.
480 module_loadorder_t
*MODULE_GetLoadOrder(const char *path
)
482 module_loadorder_t lo
, *tmp
;
488 TRACE("looking for %s\n", path
);
490 assert(path
!= NULL
);
492 /* Strip path information */
493 cptr
= strrchr(path
, '\\');
495 name
= strrchr(path
, '/');
497 name
= strrchr(cptr
, '/');
500 name
= cptr
? cptr
+1 : (char *)path
;
504 if((cptr
= strchr(name
, ':')) != NULL
) /* Also strip drive if in format 'C:MODULE.DLL' */
508 if(len
>= sizeof(fname
) || len
<= 0)
510 ERR("Path '%s' -> '%s' reduces to zilch or just too large...\n", path
, name
);
511 return &default_loadorder
;
515 if(len
>= 4 && (!lstrcmpiA(fname
+len
-4, ".dll") || !lstrcmpiA(fname
+len
-4, ".exe")))
518 lo
.modulename
= fname
;
519 tmp
= bsearch(&lo
, module_loadorder
, nmodule_loadorder
, sizeof(module_loadorder
[0]), cmp_sort_func
);
521 TRACE("Looking for '%s' (%s), found '%s'\n", path
, fname
, tmp
? tmp
->modulename
: "<nothing>");
524 return &default_loadorder
;