2 * Module/Library loadorder
4 * Copyright 1999 Bertho Stultiens
14 #include "loadorder.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(module)
24 /* #define DEBUG_LOADORDER */
26 #define LOADORDER_ALLOC_CLUSTER 32 /* Allocate with 32 entries at a time */
28 static module_loadorder_t default_loadorder;
29 static module_loadorder_t *module_loadorder = NULL;
30 static int nmodule_loadorder = 0;
31 static int nmodule_loadorder_alloc = 0;
33 static struct tagDllOverride {
35 } DefaultDllOverrides[] = {
36 {"kernel32,gdi32,user32", "builtin"},
37 {"kernel,gdi,user", "builtin"},
38 {"toolhelp", "builtin"},
39 {"comdlg32,commdlg", "elfdll,builtin,native"},
40 {"version,ver", "elfdll,builtin,native"},
41 {"shell32,shell", "builtin,native"},
42 {"lz32,lzexpand", "builtin,native"},
43 {"commctrl,comctl32", "builtin,native"},
44 {"wsock32,winsock", "builtin"},
45 {"advapi32,crtdll,ntdll", "builtin,native"},
46 {"mpr,winspool", "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 {"w32skrnl", "builtin"},
54 {"wnaspi32,wow32", "builtin"},
55 {"system,display,wprocs ", "builtin"},
56 {"wineps", "builtin"},
57 /* we have to use libglide2x.so instead of glide2x.dll ... */
58 {"glide2x", "so,native"},
62 /***************************************************************************
63 * cmp_sort_func (internal, static)
65 * Sorting and comparing function used in sort and search of loadorder
68 static int cmp_sort_func(const void *s1, const void *s2)
70 return strcasecmp(((module_loadorder_t *)s1)->modulename, ((module_loadorder_t *)s2)->modulename);
74 /***************************************************************************
75 * get_tok (internal, static)
77 * strtok wrapper for non-destructive buffer writing.
78 * NOTE: strtok is not reentrant and therefore this code is neither.
80 static char *get_tok(const char *str, const char *delim)
82 static char *buf = NULL;
90 HeapFree(SystemHeap, 0, buf);
96 buf = HEAP_strdupA(SystemHeap, 0, str);
97 cptr = strtok(buf, delim);
101 cptr = strtok(NULL, delim);
106 HeapFree(SystemHeap, 0, buf);
113 /***************************************************************************
114 * ParseLoadOrder (internal, static)
116 * Parses the loadorder options from the configuration and puts it into
119 static BOOL ParseLoadOrder(char *order, module_loadorder_t *mlo)
124 memset(mlo->loadorder, 0, sizeof(mlo->loadorder));
126 cptr = get_tok(order, ", \t");
129 char type = MODULE_LOADORDER_INVALID;
131 if(n >= MODULE_LOADORDER_NTYPES)
133 ERR("More than existing %d module-types specified, rest ignored", MODULE_LOADORDER_NTYPES);
139 case 'N': /* Native */
140 case 'n': type = MODULE_LOADORDER_DLL; break;
142 case 'E': /* Elfdll */
143 case 'e': type = MODULE_LOADORDER_ELFDLL; break;
146 case 's': type = MODULE_LOADORDER_SO; break;
148 case 'B': /* Builtin */
149 case 'b': type = MODULE_LOADORDER_BI; break;
152 ERR("Invalid load order module-type '%s', ignored\n", cptr);
155 if(type != MODULE_LOADORDER_INVALID)
157 mlo->loadorder[n++] = type;
159 cptr = get_tok(NULL, ", \t");
165 /***************************************************************************
166 * AddLoadOrder (internal, static)
168 * Adds an entry in the list of overrides. If the entry exists then the
169 * override parameter determines whether it will be overwriten.
171 static BOOL AddLoadOrder(module_loadorder_t *plo, BOOL override)
175 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
177 for(i = 0; i < nmodule_loadorder; i++)
179 if(!cmp_sort_func(plo, &module_loadorder[i]))
182 ERR("Module '%s' is already in the list of overrides, using first definition\n", plo->modulename);
184 memcpy(module_loadorder[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
189 if(nmodule_loadorder >= nmodule_loadorder_alloc)
191 /* No space in current array, make it larger */
192 nmodule_loadorder_alloc += LOADORDER_ALLOC_CLUSTER;
193 module_loadorder = (module_loadorder_t *)HeapReAlloc(SystemHeap,
196 nmodule_loadorder_alloc * sizeof(module_loadorder_t));
197 if(!module_loadorder)
199 MESSAGE("Virtual memory exhausted\n");
203 memcpy(module_loadorder[nmodule_loadorder].loadorder, plo->loadorder, sizeof(plo->loadorder));
204 module_loadorder[nmodule_loadorder].modulename = HEAP_strdupA(SystemHeap, 0, plo->modulename);
210 /***************************************************************************
211 * AddLoadOrderSet (internal, static)
213 * Adds an set of entries in the list of overrides from the key parameter.
214 * If the entry exists then the override parameter determines whether it
215 * will be overwriten.
217 static BOOL AddLoadOrderSet(char *key, char *order, BOOL override)
219 module_loadorder_t ldo;
222 /* Parse the loadorder before the rest because strtok is not reentrant */
223 if(!ParseLoadOrder(order, &ldo))
226 cptr = get_tok(key, ", \t");
229 char *ext = strrchr(cptr, '.');
232 if(strlen(ext) == 4 && (!strcasecmp(ext, ".dll") || !strcasecmp(ext, ".exe")))
233 MESSAGE("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr);
236 ldo.modulename = cptr;
237 if(!AddLoadOrder(&ldo, override))
239 cptr = get_tok(NULL, ", \t");
245 /***************************************************************************
246 * ParseCommandlineOverrides (internal, static)
248 * The commandline is in the form:
249 * name[,name,...]=native[,b,...][:...]
251 static BOOL ParseCommandlineOverrides(void)
259 if(!Options.dllFlags)
262 cpy = HEAP_strdupA(SystemHeap, 0, Options.dllFlags);
265 for(; next; key = next)
267 next = strchr(key, ':');
273 value = strchr(key, '=');
282 TRACE("Commandline override '%s' = '%s'\n", key, value);
284 if(!AddLoadOrderSet(key, value, TRUE))
291 HeapFree(SystemHeap, 0, cpy);
296 /***************************************************************************
297 * MODULE_InitLoadOrder (internal)
299 * Initialize the load order from the wine.conf file.
300 * The section has tyhe following format:
305 * EXTRA_LD_LIBRARY_PATH=/usr/local/lib/wine[:/more/path/to/search[:...]]
306 * The path will be appended to any existing LD_LIBRARY_PATH from the
307 * environment (see note in code below).
309 * DefaultLoadOrder=native,elfdll,so,builtin
310 * A comma seperated list of module-types to try to load in that specific
311 * order. The DefaultLoadOrder key is used as a fallback when a module is
312 * not specified explicitely. If the DefaultLoadOrder key is not found,
313 * then the order "dll,elfdll,so,bi" is used
314 * The possible module-types are:
315 * - native Native windows dll files
316 * - elfdll Dlls encapsulated in .so libraries
317 * - so Native .so libraries mapped to dlls
318 * - builtin Built-in modules
320 * Case is not important and only the first letter of each type is enough to
321 * identify the type n[ative], e[lfdll], s[o], b[uiltin]. Also whitespace is
326 * native,elfdll,so,builtin
332 * There are no explicit keys defined other than module/library names. A comma
333 * separated list of modules is followed by an assignment of the load-order
334 * for these specific modules. See above for possible types. You should not
335 * specify an extension.
337 * kernel32, gdi32, user32 = builtin
338 * kernel, gdi, user = builtin
339 * comdlg32 = elfdll, native, builtin
340 * commdlg = native, builtin
341 * version, ver = elfdll, native, builtin
347 * This is a simple pairing in the form 'name1 = name2'. It is supposed to
348 * identify the dlls that cannot live without eachother unless they are
349 * loaded in the same format. Examples are common dialogs and controls,
350 * shell, kernel, gdi, user, etc...
351 * The code will issue a warning if the loadorder of these pairs are different
352 * and might cause hard-to-find bugs due to incompatible pairs loaded at
353 * run-time. Note that this pairing gives *no* guarantee that the pairs
354 * actually get loaded as the same type, nor that the correct versions are
355 * loaded (might be implemented later). It merely notes obvious trouble.
362 #define BUFFERSIZE 1024
364 BOOL MODULE_InitLoadOrder(void)
366 char buffer[BUFFERSIZE];
369 #if defined(HAVE_DL_API)
370 /* Get/set the new LD_LIBRARY_PATH */
371 nbuffer = PROFILE_GetWineIniString("DllDefaults", "EXTRA_LD_LIBRARY_PATH", "", buffer, sizeof(buffer));
375 extra_ld_library_path = HEAP_strdupA(SystemHeap, 0, buffer);
376 TRACE("Setting extra LD_LIBRARY_PATH=%s\n", buffer);
380 /* Get the default load order */
381 nbuffer = PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,e,s,b", buffer, sizeof(buffer));
384 MESSAGE("MODULE_InitLoadOrder: misteriously read nothing from default loadorder\n");
388 TRACE("Setting default loadorder=%s\n", buffer);
390 if(!ParseLoadOrder(buffer, &default_loadorder))
392 default_loadorder.modulename = "<none>";
396 for (i=0;DefaultDllOverrides[i].key;i++)
398 DefaultDllOverrides[i].key,
399 DefaultDllOverrides[i].value,
404 /* Read the explicitely defined orders for specific modules as an entire section */
405 nbuffer = PROFILE_GetWineIniString("DllOverrides", NULL, "", buffer, sizeof(buffer));
406 if(nbuffer == BUFFERSIZE-2)
408 ERR("BUFFERSIZE %d is too small to read [DllOverrides]. Needs to grow in the source\n", BUFFERSIZE);
413 /* We only have the keys in the buffer, not the values */
415 char value[BUFFERSIZE];
418 for(key = buffer; *key; key = next)
420 next = key + strlen(key) + 1;
422 nbuffer = PROFILE_GetWineIniString("DllOverrides", key, "", value, sizeof(value));
425 ERR("Module(s) '%s' will always fail to load. Are you sure you want this?\n", key);
426 value[0] = '\0'; /* Just in case */
428 if(nbuffer == BUFFERSIZE-2)
430 ERR("BUFFERSIZE %d is too small to read [DllOverrides] key '%s'. Needs to grow in the source\n", BUFFERSIZE, key);
434 TRACE("Key '%s' uses override '%s'\n", key, value);
436 if(!AddLoadOrderSet(key, value, TRUE))
441 /* Add the commandline overrides to the pool */
442 if(!ParseCommandlineOverrides())
444 MESSAGE( "Syntax: -dll name[,name[,...]]={native|elfdll|so|builtin}[,{n|e|s|b}[,...]][:...]\n"
445 " - 'name' is the name of any dll without extension\n"
446 " - the order of loading (native, elfdll, so and builtin) can be abbreviated\n"
447 " with the first letter\n"
448 " - different loadorders for different dlls can be specified by seperating the\n"
449 " commandline entries with a ':'\n"
451 " -dll comdlg32,commdlg=n:shell,shell32=b\n"
456 /* Sort the array for quick lookup */
457 qsort(module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
459 /* Check the pairs of dlls */
460 nbuffer = PROFILE_GetWineIniString("DllPairs", NULL, "", buffer, sizeof(buffer));
461 if(nbuffer == BUFFERSIZE-2)
463 ERR("BUFFERSIZE %d is too small to read [DllPairs]. Needs to grow in the source\n", BUFFERSIZE);
468 /* We only have the keys in the buffer, not the values */
470 char value[BUFFERSIZE];
473 for(key = buffer; *key; key = next)
475 module_loadorder_t *plo1, *plo2;
477 next = key + strlen(key) + 1;
479 nbuffer = PROFILE_GetWineIniString("DllPairs", key, "", value, sizeof(value));
482 ERR("Module pair '%s' is not associated with another module?\n", key);
485 if(nbuffer == BUFFERSIZE-2)
487 ERR("BUFFERSIZE %d is too small to read [DllPairs] key '%s'. Needs to grow in the source\n", BUFFERSIZE, key);
491 plo1 = MODULE_GetLoadOrder(key);
492 plo2 = MODULE_GetLoadOrder(value);
493 assert(plo1 && plo2);
495 if(memcmp(plo1->loadorder, plo2->loadorder, sizeof(plo1->loadorder)))
496 MESSAGE("Warning: Modules '%s' and '%s' have different loadorder which may cause trouble\n", key, value);
503 static char types[6] = "-NESB";
505 for(i = 0; i < nmodule_loadorder; i++)
507 DPRINTF("%3d: %-12s:", i, module_loadorder[i].modulename);
508 for(j = 0; j < MODULE_LOADORDER_NTYPES; j++)
509 DPRINTF(" %c", types[module_loadorder[i].loadorder[j] % (MODULE_LOADORDER_NTYPES+1)]);
518 /***************************************************************************
519 * MODULE_GetLoadOrder (internal)
521 * Locate the loadorder of a module.
522 * Any path is stripped from the path-argument and so are the extension
523 * '.dll' and '.exe'. A lookup in the table can yield an override for the
524 * specific dll. Otherwise the default load order is returned.
526 module_loadorder_t *MODULE_GetLoadOrder(const char *path)
528 module_loadorder_t lo, *tmp;
534 assert(path != NULL);
536 /* Strip path information */
537 cptr = strrchr(path, '\\');
539 name = strrchr(path, '/');
541 name = strrchr(cptr, '/');
544 name = cptr ? cptr+1 : (char *)path;
548 if((cptr = strchr(name, ':')) != NULL) /* Also strip drive if in format 'C:MODULE.DLL' */
552 if(len >= sizeof(fname) || len <= 0)
554 ERR("Path '%s' -> '%s' reduces to zilch or just too large...\n", path, name);
555 return &default_loadorder;
559 if(len >= 4 && (!lstrcmpiA(fname+len-4, ".dll") || !lstrcmpiA(fname+len-4, ".exe")))
562 lo.modulename = fname;
563 tmp = bsearch(&lo, module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
565 TRACE("Looking for '%s' (%s), found '%s'\n", path, fname, tmp ? tmp->modulename : "<nothing>");
568 return &default_loadorder;