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", "elfdll,builtin,native"},
47 {"comdlg32,commdlg", "elfdll,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)
155 memset(mlo->loadorder, 0, sizeof(mlo->loadorder));
157 cptr = get_tok(order, ", \t");
160 char type = MODULE_LOADORDER_INVALID;
162 if(n >= MODULE_LOADORDER_NTYPES)
164 ERR("More than existing %d module-types specified, rest ignored", MODULE_LOADORDER_NTYPES);
170 case 'N': /* Native */
171 case 'n': type = MODULE_LOADORDER_DLL; break;
173 case 'E': /* Elfdll */
174 case 'e': type = MODULE_LOADORDER_ELFDLL; break;
177 case 's': type = MODULE_LOADORDER_SO; break;
179 case 'B': /* Builtin */
180 case 'b': type = MODULE_LOADORDER_BI; break;
183 ERR("Invalid load order module-type '%s', ignored\n", cptr);
186 if(type != MODULE_LOADORDER_INVALID)
188 mlo->loadorder[n++] = type;
190 cptr = get_tok(NULL, ", \t");
196 /***************************************************************************
197 * AddLoadOrder (internal, static)
199 * Adds an entry in the list of overrides. If the entry exists, then the
200 * override parameter determines whether it will be overwritten.
202 static BOOL AddLoadOrder(module_loadorder_t *plo, BOOL override)
206 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
208 for(i = 0; i < nmodule_loadorder; i++)
210 if(!cmp_sort_func(plo, &module_loadorder[i]))
213 ERR("Module '%s' is already in the list of overrides, using first definition\n", plo->modulename);
215 memcpy(module_loadorder[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
220 if(nmodule_loadorder >= nmodule_loadorder_alloc)
222 /* No space in current array, make it larger */
223 nmodule_loadorder_alloc += LOADORDER_ALLOC_CLUSTER;
224 module_loadorder = (module_loadorder_t *)HeapReAlloc(GetProcessHeap(),
227 nmodule_loadorder_alloc * sizeof(module_loadorder_t));
228 if(!module_loadorder)
230 MESSAGE("Virtual memory exhausted\n");
234 memcpy(module_loadorder[nmodule_loadorder].loadorder, plo->loadorder, sizeof(plo->loadorder));
235 module_loadorder[nmodule_loadorder].modulename = HEAP_strdupA(GetProcessHeap(), 0, plo->modulename);
241 /***************************************************************************
242 * AddLoadOrderSet (internal, static)
244 * Adds a set of entries in the list of overrides from the key parameter.
245 * If the entry exists, then the override parameter determines whether it
246 * will be overwritten.
248 static BOOL AddLoadOrderSet(char *key, char *order, BOOL override)
250 module_loadorder_t ldo;
253 /* Parse the loadorder before the rest because strtok is not reentrant */
254 if(!ParseLoadOrder(order, &ldo))
257 cptr = get_tok(key, ", \t");
260 char *ext = strrchr(cptr, '.');
263 if(strlen(ext) == 4 && (!strcasecmp(ext, ".dll") || !strcasecmp(ext, ".exe")))
264 MESSAGE("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr);
267 ldo.modulename = cptr;
268 if(!AddLoadOrder(&ldo, override))
270 cptr = get_tok(NULL, ", \t");
276 /***************************************************************************
277 * ParseCommandlineOverrides (internal, static)
279 * The commandline is in the form:
280 * name[,name,...]=native[,b,...][+...]
282 static BOOL ParseCommandlineOverrides(void)
290 if(!Options.dllFlags)
293 cpy = HEAP_strdupA(GetProcessHeap(), 0, Options.dllFlags);
296 for(; next; key = next)
298 next = strchr(key, '+');
304 value = strchr(key, '=');
313 TRACE("Commandline override '%s' = '%s'\n", key, value);
315 if(!AddLoadOrderSet(key, value, TRUE))
322 HeapFree(GetProcessHeap(), 0, cpy);
327 /***************************************************************************
328 * MODULE_InitLoadOrder (internal)
330 * Initialize the load order from the wine.conf file.
331 * The section has the following format:
336 * EXTRA_LD_LIBRARY_PATH=/usr/local/lib/wine[:/more/path/to/search[:...]]
337 * The path will be appended to any existing LD_LIBRARY_PATH from the
338 * environment (see note in code below).
340 * DefaultLoadOrder=native,elfdll,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,elfdll,so,bi" is used
345 * The possible module types are:
346 * - native Native windows dll files
347 * - elfdll Dlls encapsulated in .so libraries
348 * - so Native .so libraries mapped to dlls
349 * - builtin Built-in modules
351 * Case is not important and only the first letter of each type is enough to
352 * identify the type n[ative], e[lfdll], s[o], b[uiltin]. Also whitespace is
357 * native,elfdll,so,builtin
363 * There are no explicit keys defined other than module/library names. A comma
364 * separated list of modules is followed by an assignment of the load-order
365 * for these specific modules. See above for possible types. You should not
366 * specify an extension.
368 * kernel32, gdi32, user32 = builtin
369 * kernel, gdi, user = builtin
370 * comdlg32 = elfdll, native, builtin
371 * commdlg = native, builtin
372 * version, ver = elfdll, native, builtin
376 #define BUFFERSIZE 1024
378 BOOL MODULE_InitLoadOrder(void)
380 char buffer[BUFFERSIZE];
384 const struct tagDllPair *dllpair;
386 #if defined(HAVE_DL_API)
387 /* Get/set the new LD_LIBRARY_PATH */
388 nbuffer = PROFILE_GetWineIniString("DllDefaults", "EXTRA_LD_LIBRARY_PATH", "", buffer, sizeof(buffer));
392 extra_ld_library_path = HEAP_strdupA(GetProcessHeap(), 0, buffer);
393 TRACE("Setting extra LD_LIBRARY_PATH=%s\n", buffer);
397 /* Get the default load order */
398 nbuffer = PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,b,e,s", buffer, sizeof(buffer));
401 MESSAGE("MODULE_InitLoadOrder: mysteriously read nothing from default loadorder\n");
405 TRACE("Setting default loadorder=%s\n", buffer);
407 if(!ParseLoadOrder(buffer, &default_loadorder))
409 default_loadorder.modulename = "<none>";
413 for (i=0;DefaultDllOverrides[i].key;i++)
415 DefaultDllOverrides[i].key,
416 DefaultDllOverrides[i].value,
421 /* Read the explicitely defined orders for specific modules as an entire section */
423 while (PROFILE_EnumWineIniString( "DllOverrides", idx++, key, sizeof(key),
424 buffer, sizeof(buffer)))
426 TRACE("Key '%s' uses override '%s'\n", key, buffer);
427 if(!AddLoadOrderSet(key, buffer, TRUE))
431 /* Add the commandline overrides to the pool */
432 if(!ParseCommandlineOverrides())
434 MESSAGE( "Syntax: -dll name[,name[,...]]={native|elfdll|so|builtin}[,{n|e|s|b}[,...]][+...]\n"
435 " - 'name' is the name of any dll without extension\n"
436 " - the order of loading (native, elfdll, so and builtin) can be abbreviated\n"
437 " with the first letter\n"
438 " - different loadorders for different dlls can be specified by seperating the\n"
439 " commandline entries with a '+'\n"
441 " -dll comdlg32,commdlg=n+shell,shell32=b\n"
446 /* Sort the array for quick lookup */
447 qsort(module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
449 /* Check the pairs of dlls */
451 while (dllpair->dll1)
453 module_loadorder_t *plo1, *plo2;
454 plo1 = MODULE_GetLoadOrder(dllpair->dll1, FALSE);
455 plo2 = MODULE_GetLoadOrder(dllpair->dll2, FALSE);
456 assert(plo1 && plo2);
457 if(memcmp(plo1->loadorder, plo2->loadorder, sizeof(plo1->loadorder)))
458 MESSAGE("Warning: Modules '%s' and '%s' have different loadorder which may cause trouble\n", dllpair->dll1, dllpair->dll2);
465 static char types[6] = "-NESB";
467 for(i = 0; i < nmodule_loadorder; i++)
469 DPRINTF("%3d: %-12s:", i, module_loadorder[i].modulename);
470 for(j = 0; j < MODULE_LOADORDER_NTYPES; j++)
471 DPRINTF(" %c", types[module_loadorder[i].loadorder[j] % (MODULE_LOADORDER_NTYPES+1)]);
480 /***************************************************************************
481 * MODULE_GetLoadOrder (internal)
483 * Locate the loadorder of a module.
484 * Any path is stripped from the path-argument and so are the extension
485 * '.dll' and '.exe'. A lookup in the table can yield an override for
486 * the specific dll. Otherwise the default load order is returned.
488 module_loadorder_t *MODULE_GetLoadOrder(const char *path, BOOL win32 )
490 module_loadorder_t lo, *tmp;
492 char sysdir[MAX_PATH+1];
497 TRACE("looking for %s\n", path);
499 assert(path != NULL);
501 if ( ! GetSystemDirectoryA ( sysdir, MAX_PATH ) )
502 return &default_loadorder; /* Hmmm ... */
504 /* Strip path information for 16 bit modules or if the module
505 resides in the system directory */
506 if ( !win32 || !strncasecmp ( sysdir, path, strlen (sysdir) ) )
509 cptr = strrchr(path, '\\');
511 name = strrchr(path, '/');
513 name = strrchr(cptr, '/');
516 name = cptr ? cptr+1 : (char *)path;
520 if((cptr = strchr(name, ':')) != NULL) /* Also strip drive if in format 'C:MODULE.DLL' */
527 if(len >= sizeof(fname) || len <= 0)
529 ERR("Path '%s' -> '%s' reduces to zilch or just too large...\n", path, name);
530 return &default_loadorder;
534 if(len >= 4 && (!strcasecmp(fname+len-4, ".dll") || !strcasecmp(fname+len-4, ".exe")))
537 lo.modulename = fname;
538 tmp = bsearch(&lo, module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
540 TRACE("Looking for '%s' (%s), found '%s'\n", path, fname, tmp ? tmp->modulename : "<nothing>");
543 return &default_loadorder;