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 {"odbc32", "builtin"},
62 {"opengl32", "builtin,native"},
66 static const struct tagDllPair
{
67 const char *dll1
, *dll2
;
69 { "krnl386", "kernel32" },
72 { "commdlg", "comdlg32" },
73 { "commctrl", "comctl32" },
75 { "shell", "shell32" },
76 { "lzexpand", "lz32" },
77 { "mmsystem", "winmm" },
78 { "msvideo", "msvfw32" },
79 { "winsock", "wsock32" },
83 /***************************************************************************
84 * cmp_sort_func (internal, static)
86 * Sorting and comparing function used in sort and search of loadorder
89 static int cmp_sort_func(const void *s1
, const void *s2
)
91 return strcasecmp(((module_loadorder_t
*)s1
)->modulename
, ((module_loadorder_t
*)s2
)->modulename
);
95 /***************************************************************************
96 * get_tok (internal, static)
98 * strtok wrapper for non-destructive buffer writing.
99 * NOTE: strtok is not reentrant and therefore this code is neither.
101 static char *get_tok(const char *str
, const char *delim
)
103 static char *buf
= NULL
;
111 HeapFree(GetProcessHeap(), 0, buf
);
117 buf
= HEAP_strdupA(GetProcessHeap(), 0, str
);
118 cptr
= strtok(buf
, delim
);
122 cptr
= strtok(NULL
, delim
);
127 HeapFree(GetProcessHeap(), 0, buf
);
134 /***************************************************************************
135 * ParseLoadOrder (internal, static)
137 * Parses the loadorder options from the configuration and puts it into
140 static BOOL
ParseLoadOrder(char *order
, module_loadorder_t
*mlo
)
145 memset(mlo
->loadorder
, 0, sizeof(mlo
->loadorder
));
147 cptr
= get_tok(order
, ", \t");
150 char type
= MODULE_LOADORDER_INVALID
;
152 if(n
>= MODULE_LOADORDER_NTYPES
)
154 ERR("More than existing %d module-types specified, rest ignored", MODULE_LOADORDER_NTYPES
);
160 case 'N': /* Native */
161 case 'n': type
= MODULE_LOADORDER_DLL
; break;
163 case 'E': /* Elfdll */
164 case 'e': type
= MODULE_LOADORDER_ELFDLL
; break;
167 case 's': type
= MODULE_LOADORDER_SO
; break;
169 case 'B': /* Builtin */
170 case 'b': type
= MODULE_LOADORDER_BI
; break;
173 ERR("Invalid load order module-type '%s', ignored\n", cptr
);
176 if(type
!= MODULE_LOADORDER_INVALID
)
178 mlo
->loadorder
[n
++] = type
;
180 cptr
= get_tok(NULL
, ", \t");
186 /***************************************************************************
187 * AddLoadOrder (internal, static)
189 * Adds an entry in the list of overrides. If the entry exists, then the
190 * override parameter determines whether it will be overwritten.
192 static BOOL
AddLoadOrder(module_loadorder_t
*plo
, BOOL override
)
196 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
198 for(i
= 0; i
< nmodule_loadorder
; i
++)
200 if(!cmp_sort_func(plo
, &module_loadorder
[i
]))
203 ERR("Module '%s' is already in the list of overrides, using first definition\n", plo
->modulename
);
205 memcpy(module_loadorder
[i
].loadorder
, plo
->loadorder
, sizeof(plo
->loadorder
));
210 if(nmodule_loadorder
>= nmodule_loadorder_alloc
)
212 /* No space in current array, make it larger */
213 nmodule_loadorder_alloc
+= LOADORDER_ALLOC_CLUSTER
;
214 module_loadorder
= (module_loadorder_t
*)HeapReAlloc(GetProcessHeap(),
217 nmodule_loadorder_alloc
* sizeof(module_loadorder_t
));
218 if(!module_loadorder
)
220 MESSAGE("Virtual memory exhausted\n");
224 memcpy(module_loadorder
[nmodule_loadorder
].loadorder
, plo
->loadorder
, sizeof(plo
->loadorder
));
225 module_loadorder
[nmodule_loadorder
].modulename
= HEAP_strdupA(GetProcessHeap(), 0, plo
->modulename
);
231 /***************************************************************************
232 * AddLoadOrderSet (internal, static)
234 * Adds a set of entries in the list of overrides from the key parameter.
235 * If the entry exists, then the override parameter determines whether it
236 * will be overwritten.
238 static BOOL
AddLoadOrderSet(char *key
, char *order
, BOOL override
)
240 module_loadorder_t ldo
;
243 /* Parse the loadorder before the rest because strtok is not reentrant */
244 if(!ParseLoadOrder(order
, &ldo
))
247 cptr
= get_tok(key
, ", \t");
250 char *ext
= strrchr(cptr
, '.');
253 if(strlen(ext
) == 4 && (!strcasecmp(ext
, ".dll") || !strcasecmp(ext
, ".exe")))
254 MESSAGE("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr
);
257 ldo
.modulename
= cptr
;
258 if(!AddLoadOrder(&ldo
, override
))
260 cptr
= get_tok(NULL
, ", \t");
266 /***************************************************************************
267 * ParseCommandlineOverrides (internal, static)
269 * The commandline is in the form:
270 * name[,name,...]=native[,b,...][:...]
272 static BOOL
ParseCommandlineOverrides(void)
280 if(!Options
.dllFlags
)
283 cpy
= HEAP_strdupA(GetProcessHeap(), 0, Options
.dllFlags
);
286 for(; next
; key
= next
)
288 next
= strchr(key
, ':');
294 value
= strchr(key
, '=');
303 TRACE("Commandline override '%s' = '%s'\n", key
, value
);
305 if(!AddLoadOrderSet(key
, value
, TRUE
))
312 HeapFree(GetProcessHeap(), 0, cpy
);
317 /***************************************************************************
318 * MODULE_InitLoadOrder (internal)
320 * Initialize the load order from the wine.conf file.
321 * The section has the following format:
326 * EXTRA_LD_LIBRARY_PATH=/usr/local/lib/wine[:/more/path/to/search[:...]]
327 * The path will be appended to any existing LD_LIBRARY_PATH from the
328 * environment (see note in code below).
330 * DefaultLoadOrder=native,elfdll,so,builtin
331 * A comma separated list of module types to try to load in that specific
332 * order. The DefaultLoadOrder key is used as a fallback when a module is
333 * not specified explicitly. If the DefaultLoadOrder key is not found,
334 * then the order "dll,elfdll,so,bi" is used
335 * The possible module types are:
336 * - native Native windows dll files
337 * - elfdll Dlls encapsulated in .so libraries
338 * - so Native .so libraries mapped to dlls
339 * - builtin Built-in modules
341 * Case is not important and only the first letter of each type is enough to
342 * identify the type n[ative], e[lfdll], s[o], b[uiltin]. Also whitespace is
347 * native,elfdll,so,builtin
353 * There are no explicit keys defined other than module/library names. A comma
354 * separated list of modules is followed by an assignment of the load-order
355 * for these specific modules. See above for possible types. You should not
356 * specify an extension.
358 * kernel32, gdi32, user32 = builtin
359 * kernel, gdi, user = builtin
360 * comdlg32 = elfdll, native, builtin
361 * commdlg = native, builtin
362 * version, ver = elfdll, native, builtin
366 #define BUFFERSIZE 1024
368 BOOL
MODULE_InitLoadOrder(void)
370 char buffer
[BUFFERSIZE
];
374 const struct tagDllPair
*dllpair
;
376 #if defined(HAVE_DL_API)
377 /* Get/set the new LD_LIBRARY_PATH */
378 nbuffer
= PROFILE_GetWineIniString("DllDefaults", "EXTRA_LD_LIBRARY_PATH", "", buffer
, sizeof(buffer
));
382 extra_ld_library_path
= HEAP_strdupA(GetProcessHeap(), 0, buffer
);
383 TRACE("Setting extra LD_LIBRARY_PATH=%s\n", buffer
);
387 /* Get the default load order */
388 nbuffer
= PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,b,e,s", buffer
, sizeof(buffer
));
391 MESSAGE("MODULE_InitLoadOrder: mysteriously read nothing from default loadorder\n");
395 TRACE("Setting default loadorder=%s\n", buffer
);
397 if(!ParseLoadOrder(buffer
, &default_loadorder
))
399 default_loadorder
.modulename
= "<none>";
403 for (i
=0;DefaultDllOverrides
[i
].key
;i
++)
405 DefaultDllOverrides
[i
].key
,
406 DefaultDllOverrides
[i
].value
,
411 /* Read the explicitely defined orders for specific modules as an entire section */
413 while (PROFILE_EnumWineIniString( "DllOverrides", idx
++, key
, sizeof(key
),
414 buffer
, sizeof(buffer
)))
416 TRACE("Key '%s' uses override '%s'\n", key
, buffer
);
417 if(!AddLoadOrderSet(key
, buffer
, TRUE
))
421 /* Add the commandline overrides to the pool */
422 if(!ParseCommandlineOverrides())
424 MESSAGE( "Syntax: -dll name[,name[,...]]={native|elfdll|so|builtin}[,{n|e|s|b}[,...]][:...]\n"
425 " - 'name' is the name of any dll without extension\n"
426 " - the order of loading (native, elfdll, so and builtin) can be abbreviated\n"
427 " with the first letter\n"
428 " - different loadorders for different dlls can be specified by seperating the\n"
429 " commandline entries with a ':'\n"
431 " -dll comdlg32,commdlg=n:shell,shell32=b\n"
436 /* Sort the array for quick lookup */
437 qsort(module_loadorder
, nmodule_loadorder
, sizeof(module_loadorder
[0]), cmp_sort_func
);
439 /* Check the pairs of dlls */
441 while (dllpair
->dll1
)
443 module_loadorder_t
*plo1
, *plo2
;
444 plo1
= MODULE_GetLoadOrder(dllpair
->dll1
);
445 plo2
= MODULE_GetLoadOrder(dllpair
->dll2
);
446 assert(plo1
&& plo2
);
447 if(memcmp(plo1
->loadorder
, plo2
->loadorder
, sizeof(plo1
->loadorder
)))
448 MESSAGE("Warning: Modules '%s' and '%s' have different loadorder which may cause trouble\n", dllpair
->dll1
, dllpair
->dll2
);
455 static char types
[6] = "-NESB";
457 for(i
= 0; i
< nmodule_loadorder
; i
++)
459 DPRINTF("%3d: %-12s:", i
, module_loadorder
[i
].modulename
);
460 for(j
= 0; j
< MODULE_LOADORDER_NTYPES
; j
++)
461 DPRINTF(" %c", types
[module_loadorder
[i
].loadorder
[j
] % (MODULE_LOADORDER_NTYPES
+1)]);
470 /***************************************************************************
471 * MODULE_GetLoadOrder (internal)
473 * Locate the loadorder of a module.
474 * Any path is stripped from the path-argument and so are the extension
475 * '.dll' and '.exe'. A lookup in the table can yield an override for
476 * the specific dll. Otherwise the default load order is returned.
478 module_loadorder_t
*MODULE_GetLoadOrder(const char *path
)
480 module_loadorder_t lo
, *tmp
;
486 TRACE("looking for %s\n", path
);
488 assert(path
!= NULL
);
490 /* Strip path information */
491 cptr
= strrchr(path
, '\\');
493 name
= strrchr(path
, '/');
495 name
= strrchr(cptr
, '/');
498 name
= cptr
? cptr
+1 : (char *)path
;
502 if((cptr
= strchr(name
, ':')) != NULL
) /* Also strip drive if in format 'C:MODULE.DLL' */
506 if(len
>= sizeof(fname
) || len
<= 0)
508 ERR("Path '%s' -> '%s' reduces to zilch or just too large...\n", path
, name
);
509 return &default_loadorder
;
513 if(len
>= 4 && (!lstrcmpiA(fname
+len
-4, ".dll") || !lstrcmpiA(fname
+len
-4, ".exe")))
516 lo
.modulename
= fname
;
517 tmp
= bsearch(&lo
, module_loadorder
, nmodule_loadorder
, sizeof(module_loadorder
[0]), cmp_sort_func
);
519 TRACE("Looking for '%s' (%s), found '%s'\n", path
, fname
, tmp
? tmp
->modulename
: "<nothing>");
522 return &default_loadorder
;