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"},
65 static const struct tagDllPair {
66 const char *dll1, *dll2;
68 { "krnl386", "kernel32" },
71 { "commdlg", "comdlg32" },
72 { "commctrl", "comctl32" },
74 { "shell", "shell32" },
75 { "lzexpand", "lz32" },
76 { "mmsystem", "winmm" },
77 { "msvideo", "msvfw32" },
78 { "winsock", "wsock32" },
82 /***************************************************************************
83 * cmp_sort_func (internal, static)
85 * Sorting and comparing function used in sort and search of loadorder
88 static int cmp_sort_func(const void *s1, const void *s2)
90 return strcasecmp(((module_loadorder_t *)s1)->modulename, ((module_loadorder_t *)s2)->modulename);
94 /***************************************************************************
95 * get_tok (internal, static)
97 * strtok wrapper for non-destructive buffer writing.
98 * NOTE: strtok is not reentrant and therefore this code is neither.
100 static char *get_tok(const char *str, const char *delim)
102 static char *buf = NULL;
110 HeapFree(GetProcessHeap(), 0, buf);
116 buf = HEAP_strdupA(GetProcessHeap(), 0, str);
117 cptr = strtok(buf, delim);
121 cptr = strtok(NULL, delim);
126 HeapFree(GetProcessHeap(), 0, buf);
133 /***************************************************************************
134 * ParseLoadOrder (internal, static)
136 * Parses the loadorder options from the configuration and puts it into
139 static BOOL ParseLoadOrder(char *order, module_loadorder_t *mlo)
144 memset(mlo->loadorder, 0, sizeof(mlo->loadorder));
146 cptr = get_tok(order, ", \t");
149 char type = MODULE_LOADORDER_INVALID;
151 if(n >= MODULE_LOADORDER_NTYPES)
153 ERR("More than existing %d module-types specified, rest ignored", MODULE_LOADORDER_NTYPES);
159 case 'N': /* Native */
160 case 'n': type = MODULE_LOADORDER_DLL; break;
162 case 'E': /* Elfdll */
163 case 'e': type = MODULE_LOADORDER_ELFDLL; break;
166 case 's': type = MODULE_LOADORDER_SO; break;
168 case 'B': /* Builtin */
169 case 'b': type = MODULE_LOADORDER_BI; break;
172 ERR("Invalid load order module-type '%s', ignored\n", cptr);
175 if(type != MODULE_LOADORDER_INVALID)
177 mlo->loadorder[n++] = type;
179 cptr = get_tok(NULL, ", \t");
185 /***************************************************************************
186 * AddLoadOrder (internal, static)
188 * Adds an entry in the list of overrides. If the entry exists, then the
189 * override parameter determines whether it will be overwritten.
191 static BOOL AddLoadOrder(module_loadorder_t *plo, BOOL override)
195 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
197 for(i = 0; i < nmodule_loadorder; i++)
199 if(!cmp_sort_func(plo, &module_loadorder[i]))
202 ERR("Module '%s' is already in the list of overrides, using first definition\n", plo->modulename);
204 memcpy(module_loadorder[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
209 if(nmodule_loadorder >= nmodule_loadorder_alloc)
211 /* No space in current array, make it larger */
212 nmodule_loadorder_alloc += LOADORDER_ALLOC_CLUSTER;
213 module_loadorder = (module_loadorder_t *)HeapReAlloc(GetProcessHeap(),
216 nmodule_loadorder_alloc * sizeof(module_loadorder_t));
217 if(!module_loadorder)
219 MESSAGE("Virtual memory exhausted\n");
223 memcpy(module_loadorder[nmodule_loadorder].loadorder, plo->loadorder, sizeof(plo->loadorder));
224 module_loadorder[nmodule_loadorder].modulename = HEAP_strdupA(GetProcessHeap(), 0, plo->modulename);
230 /***************************************************************************
231 * AddLoadOrderSet (internal, static)
233 * Adds a set of entries in the list of overrides from the key parameter.
234 * If the entry exists, then the override parameter determines whether it
235 * will be overwritten.
237 static BOOL AddLoadOrderSet(char *key, char *order, BOOL override)
239 module_loadorder_t ldo;
242 /* Parse the loadorder before the rest because strtok is not reentrant */
243 if(!ParseLoadOrder(order, &ldo))
246 cptr = get_tok(key, ", \t");
249 char *ext = strrchr(cptr, '.');
252 if(strlen(ext) == 4 && (!strcasecmp(ext, ".dll") || !strcasecmp(ext, ".exe")))
253 MESSAGE("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr);
256 ldo.modulename = cptr;
257 if(!AddLoadOrder(&ldo, override))
259 cptr = get_tok(NULL, ", \t");
265 /***************************************************************************
266 * ParseCommandlineOverrides (internal, static)
268 * The commandline is in the form:
269 * name[,name,...]=native[,b,...][:...]
271 static BOOL ParseCommandlineOverrides(void)
279 if(!Options.dllFlags)
282 cpy = HEAP_strdupA(GetProcessHeap(), 0, Options.dllFlags);
285 for(; next; key = next)
287 next = strchr(key, ':');
293 value = strchr(key, '=');
302 TRACE("Commandline override '%s' = '%s'\n", key, value);
304 if(!AddLoadOrderSet(key, value, TRUE))
311 HeapFree(GetProcessHeap(), 0, cpy);
316 /***************************************************************************
317 * MODULE_InitLoadOrder (internal)
319 * Initialize the load order from the wine.conf file.
320 * The section has the following format:
325 * EXTRA_LD_LIBRARY_PATH=/usr/local/lib/wine[:/more/path/to/search[:...]]
326 * The path will be appended to any existing LD_LIBRARY_PATH from the
327 * environment (see note in code below).
329 * DefaultLoadOrder=native,elfdll,so,builtin
330 * A comma separated list of module types to try to load in that specific
331 * order. The DefaultLoadOrder key is used as a fallback when a module is
332 * not specified explicitly. If the DefaultLoadOrder key is not found,
333 * then the order "dll,elfdll,so,bi" is used
334 * The possible module types are:
335 * - native Native windows dll files
336 * - elfdll Dlls encapsulated in .so libraries
337 * - so Native .so libraries mapped to dlls
338 * - builtin Built-in modules
340 * Case is not important and only the first letter of each type is enough to
341 * identify the type n[ative], e[lfdll], s[o], b[uiltin]. Also whitespace is
346 * native,elfdll,so,builtin
352 * There are no explicit keys defined other than module/library names. A comma
353 * separated list of modules is followed by an assignment of the load-order
354 * for these specific modules. See above for possible types. You should not
355 * specify an extension.
357 * kernel32, gdi32, user32 = builtin
358 * kernel, gdi, user = builtin
359 * comdlg32 = elfdll, native, builtin
360 * commdlg = native, builtin
361 * version, ver = elfdll, native, builtin
365 #define BUFFERSIZE 1024
367 BOOL MODULE_InitLoadOrder(void)
369 char buffer[BUFFERSIZE];
373 const struct tagDllPair *dllpair;
375 #if defined(HAVE_DL_API)
376 /* Get/set the new LD_LIBRARY_PATH */
377 nbuffer = PROFILE_GetWineIniString("DllDefaults", "EXTRA_LD_LIBRARY_PATH", "", buffer, sizeof(buffer));
381 extra_ld_library_path = HEAP_strdupA(GetProcessHeap(), 0, buffer);
382 TRACE("Setting extra LD_LIBRARY_PATH=%s\n", buffer);
386 /* Get the default load order */
387 nbuffer = PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,b,e,s", buffer, sizeof(buffer));
390 MESSAGE("MODULE_InitLoadOrder: mysteriously read nothing from default loadorder\n");
394 TRACE("Setting default loadorder=%s\n", buffer);
396 if(!ParseLoadOrder(buffer, &default_loadorder))
398 default_loadorder.modulename = "<none>";
402 for (i=0;DefaultDllOverrides[i].key;i++)
404 DefaultDllOverrides[i].key,
405 DefaultDllOverrides[i].value,
410 /* Read the explicitely defined orders for specific modules as an entire section */
412 while (PROFILE_EnumWineIniString( "DllOverrides", idx++, key, sizeof(key),
413 buffer, sizeof(buffer)))
415 TRACE("Key '%s' uses override '%s'\n", key, buffer);
416 if(!AddLoadOrderSet(key, buffer, TRUE))
420 /* Add the commandline overrides to the pool */
421 if(!ParseCommandlineOverrides())
423 MESSAGE( "Syntax: -dll name[,name[,...]]={native|elfdll|so|builtin}[,{n|e|s|b}[,...]][:...]\n"
424 " - 'name' is the name of any dll without extension\n"
425 " - the order of loading (native, elfdll, so and builtin) can be abbreviated\n"
426 " with the first letter\n"
427 " - different loadorders for different dlls can be specified by seperating the\n"
428 " commandline entries with a ':'\n"
430 " -dll comdlg32,commdlg=n:shell,shell32=b\n"
435 /* Sort the array for quick lookup */
436 qsort(module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
438 /* Check the pairs of dlls */
440 while (dllpair->dll1)
442 module_loadorder_t *plo1, *plo2;
443 plo1 = MODULE_GetLoadOrder(dllpair->dll1);
444 plo2 = MODULE_GetLoadOrder(dllpair->dll2);
445 assert(plo1 && plo2);
446 if(memcmp(plo1->loadorder, plo2->loadorder, sizeof(plo1->loadorder)))
447 MESSAGE("Warning: Modules '%s' and '%s' have different loadorder which may cause trouble\n", dllpair->dll1, dllpair->dll2);
454 static char types[6] = "-NESB";
456 for(i = 0; i < nmodule_loadorder; i++)
458 DPRINTF("%3d: %-12s:", i, module_loadorder[i].modulename);
459 for(j = 0; j < MODULE_LOADORDER_NTYPES; j++)
460 DPRINTF(" %c", types[module_loadorder[i].loadorder[j] % (MODULE_LOADORDER_NTYPES+1)]);
469 /***************************************************************************
470 * MODULE_GetLoadOrder (internal)
472 * Locate the loadorder of a module.
473 * Any path is stripped from the path-argument and so are the extension
474 * '.dll' and '.exe'. A lookup in the table can yield an override for
475 * the specific dll. Otherwise the default load order is returned.
477 module_loadorder_t *MODULE_GetLoadOrder(const char *path)
479 module_loadorder_t lo, *tmp;
485 assert(path != NULL);
487 /* Strip path information */
488 cptr = strrchr(path, '\\');
490 name = strrchr(path, '/');
492 name = strrchr(cptr, '/');
495 name = cptr ? cptr+1 : (char *)path;
499 if((cptr = strchr(name, ':')) != NULL) /* Also strip drive if in format 'C:MODULE.DLL' */
503 if(len >= sizeof(fname) || len <= 0)
505 ERR("Path '%s' -> '%s' reduces to zilch or just too large...\n", path, name);
506 return &default_loadorder;
510 if(len >= 4 && (!lstrcmpiA(fname+len-4, ".dll") || !lstrcmpiA(fname+len-4, ".exe")))
513 lo.modulename = fname;
514 tmp = bsearch(&lo, module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
516 TRACE("Looking for '%s' (%s), found '%s'\n", path, fname, tmp ? tmp->modulename : "<nothing>");
519 return &default_loadorder;