wine.conf should not overrule content of burned in data in
[wine.git] / loader / loadorder.c
blob8d823de9367c9e4c5631903dbc3c38d360b8228b
1 /*
2 * Module/Library loadorder
4 * Copyright 1999 Bertho Stultiens
5 */
7 #include <stdlib.h>
8 #include <string.h>
9 #include <assert.h>
11 #include "config.h"
12 #include "windef.h"
13 #include "options.h"
14 #include "loadorder.h"
15 #include "heap.h"
16 #include "options.h"
17 #include "module.h"
18 #include "elfdll.h"
19 #include "debug.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 {
34 char *key,*value;
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 {NULL,NULL},
60 /***************************************************************************
61 * cmp_sort_func (internal, static)
63 * Sorting and comparing function used in sort and search of loadorder
64 * entries.
66 static int cmp_sort_func(const void *s1, const void *s2)
68 return strcasecmp(((module_loadorder_t *)s1)->modulename, ((module_loadorder_t *)s2)->modulename);
72 /***************************************************************************
73 * get_tok (internal, static)
75 * strtok wrapper for non-destructive buffer writing.
76 * NOTE: strtok is not reentrant and therefore this code is neither.
78 static char *get_tok(const char *str, const char *delim)
80 static char *buf = NULL;
81 char *cptr;
83 if(!str && !buf)
84 return NULL;
86 if(str && buf)
88 HeapFree(SystemHeap, 0, buf);
89 buf = NULL;
92 if(str && !buf)
94 buf = HEAP_strdupA(SystemHeap, 0, str);
95 cptr = strtok(buf, delim);
97 else
99 cptr = strtok(NULL, delim);
102 if(!cptr)
104 HeapFree(SystemHeap, 0, buf);
105 buf = NULL;
107 return cptr;
111 /***************************************************************************
112 * ParseLoadOrder (internal, static)
114 * Parses the loadorder options from the configuration and puts it into
115 * a structure.
117 static BOOL ParseLoadOrder(char *order, module_loadorder_t *mlo)
119 char *cptr;
120 int n = 0;
122 memset(mlo->loadorder, 0, sizeof(mlo->loadorder));
124 cptr = get_tok(order, ", \t");
125 while(cptr)
127 char type = MODULE_LOADORDER_INVALID;
129 if(n >= MODULE_LOADORDER_NTYPES)
131 ERR(module, "More than existing %d module-types specified, rest ignored", MODULE_LOADORDER_NTYPES);
132 break;
135 switch(*cptr)
137 case 'N': /* Native */
138 case 'n': type = MODULE_LOADORDER_DLL; break;
140 case 'E': /* Elfdll */
141 case 'e': type = MODULE_LOADORDER_ELFDLL; break;
143 case 'S': /* So */
144 case 's': type = MODULE_LOADORDER_SO; break;
146 case 'B': /* Builtin */
147 case 'b': type = MODULE_LOADORDER_BI; break;
149 default:
150 ERR(module, "Invalid load order module-type '%s', ignored\n", cptr);
153 if(type != MODULE_LOADORDER_INVALID)
155 mlo->loadorder[n++] = type;
157 cptr = get_tok(NULL, ", \t");
159 return TRUE;
163 /***************************************************************************
164 * AddLoadOrder (internal, static)
166 * Adds an entry in the list of overrides. If the entry exists then the
167 * override parameter determines whether it will be overwriten.
169 static BOOL AddLoadOrder(module_loadorder_t *plo, BOOL override)
171 int i;
173 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
175 for(i = 0; i < nmodule_loadorder; i++)
177 if(!cmp_sort_func(plo, &module_loadorder[i]))
179 if(!override)
180 ERR(module, "Module '%s' is already in the list of overrides, using first definition\n", plo->modulename);
181 else
182 memcpy(module_loadorder[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
183 return TRUE;
187 if(nmodule_loadorder >= nmodule_loadorder_alloc)
189 /* No space in current array, make it larger */
190 nmodule_loadorder_alloc += LOADORDER_ALLOC_CLUSTER;
191 module_loadorder = (module_loadorder_t *)HeapReAlloc(SystemHeap,
193 module_loadorder,
194 nmodule_loadorder_alloc * sizeof(module_loadorder_t));
195 if(!module_loadorder)
197 MSG("Virtual memory exhausted\n");
198 exit(1);
201 memcpy(module_loadorder[nmodule_loadorder].loadorder, plo->loadorder, sizeof(plo->loadorder));
202 module_loadorder[nmodule_loadorder].modulename = HEAP_strdupA(SystemHeap, 0, plo->modulename);
203 nmodule_loadorder++;
204 return TRUE;
208 /***************************************************************************
209 * AddLoadOrderSet (internal, static)
211 * Adds an set of entries in the list of overrides from the key parameter.
212 * If the entry exists then the override parameter determines whether it
213 * will be overwriten.
215 static BOOL AddLoadOrderSet(char *key, char *order, BOOL override)
217 module_loadorder_t ldo;
218 char *cptr;
220 /* Parse the loadorder before the rest because strtok is not reentrant */
221 if(!ParseLoadOrder(order, &ldo))
222 return FALSE;
224 cptr = get_tok(key, ", \t");
225 while(cptr)
227 if(strchr(cptr, '.'))
228 MSG("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr);
230 ldo.modulename = cptr;
231 if(!AddLoadOrder(&ldo, override))
232 return FALSE;
233 cptr = get_tok(NULL, ", \t");
235 return TRUE;
239 /***************************************************************************
240 * ParseCommandlineOverrides (internal, static)
242 * The commandline is in the form:
243 * name[,name,...]=native[,b,...][:...]
245 static BOOL ParseCommandlineOverrides(void)
247 char *cpy;
248 char *key;
249 char *next;
250 char *value;
251 BOOL retval = TRUE;
253 if(!Options.dllFlags)
254 return TRUE;
256 cpy = HEAP_strdupA(SystemHeap, 0, Options.dllFlags);
257 key = cpy;
258 next = key;
259 for(; next; key = next)
261 next = strchr(key, ':');
262 if(next)
264 *next = '\0';
265 next++;
267 value = strchr(key, '=');
268 if(!value)
270 retval = FALSE;
271 goto endit;
273 *value = '\0';
274 value++;
276 TRACE(module, "Commandline override '%s' = '%s'\n", key, value);
278 if(!AddLoadOrderSet(key, value, TRUE))
280 retval = FALSE;
281 goto endit;
284 endit:
285 HeapFree(SystemHeap, 0, cpy);
286 return retval;;
290 /***************************************************************************
291 * MODULE_InitLoadOrder (internal)
293 * Initialize the load order from the wine.conf file.
294 * The section has tyhe following format:
295 * Section:
296 * [DllDefaults]
298 * Keys:
299 * EXTRA_LD_LIBRARY_PATH=/usr/local/lib/wine[:/more/path/to/search[:...]]
300 * The path will be appended to any existing LD_LIBRARY_PATH from the
301 * environment (see note in code below).
303 * DefaultLoadOrder=native,elfdll,so,builtin
304 * A comma seperated list of module-types to try to load in that specific
305 * order. The DefaultLoadOrder key is used as a fallback when a module is
306 * not specified explicitely. If the DefaultLoadOrder key is not found,
307 * then the order "dll,elfdll,so,bi" is used
308 * The possible module-types are:
309 * - native Native windows dll files
310 * - elfdll Dlls encapsulated in .so libraries
311 * - so Native .so libraries mapped to dlls
312 * - builtin Built-in modules
314 * Case is not important and only the first letter of each type is enough to
315 * identify the type n[ative], e[lfdll], s[o], b[uiltin]. Also whitespace is
316 * ignored.
317 * E.g.:
318 * n,el ,s , b
319 * is equal to:
320 * native,elfdll,so,builtin
322 * Section:
323 * [DllOverrides]
325 * Keys:
326 * There are no explicit keys defined other than module/library names. A comma
327 * separated list of modules is followed by an assignment of the load-order
328 * for these specific modules. See above for possible types. You should not
329 * specify an extension.
330 * Examples:
331 * kernel32, gdi32, user32 = builtin
332 * kernel, gdi, user = builtin
333 * comdlg32 = elfdll, native, builtin
334 * commdlg = native, builtin
335 * version, ver = elfdll, native, builtin
337 * Section:
338 * [DllPairs]
340 * Keys:
341 * This is a simple pairing in the form 'name1 = name2'. It is supposed to
342 * identify the dlls that cannot live without eachother unless they are
343 * loaded in the same format. Examples are common dialogs and controls,
344 * shell, kernel, gdi, user, etc...
345 * The code will issue a warning if the loadorder of these pairs are different
346 * and might cause hard-to-find bugs due to incompatible pairs loaded at
347 * run-time. Note that this pairing gives *no* guarantee that the pairs
348 * actually get loaded as the same type, nor that the correct versions are
349 * loaded (might be implemented later). It merely notes obvious trouble.
350 * Examples:
351 * kernel = kernel32
352 * commdlg = comdlg32
356 #define BUFFERSIZE 1024
358 BOOL MODULE_InitLoadOrder(void)
360 char buffer[BUFFERSIZE];
361 int nbuffer;
363 #if defined(HAVE_DL_API)
364 /* Get/set the new LD_LIBRARY_PATH */
365 nbuffer = PROFILE_GetWineIniString("DllDefaults", "EXTRA_LD_LIBRARY_PATH", "", buffer, sizeof(buffer));
367 if(nbuffer)
369 extra_ld_library_path = HEAP_strdupA(SystemHeap, 0, buffer);
370 TRACE(module, "Setting extra LD_LIBRARY_PATH=%s\n", buffer);
372 #endif
374 /* Get the default load order */
375 nbuffer = PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,e,s,b", buffer, sizeof(buffer));
376 if(!nbuffer)
378 MSG("MODULE_InitLoadOrder: misteriously read nothing from default loadorder\n");
379 return FALSE;
382 TRACE(module, "Setting default loadorder=%s\n", buffer);
384 if(!ParseLoadOrder(buffer, &default_loadorder))
385 return FALSE;
386 default_loadorder.modulename = "<none>";
389 int i;
390 for (i=0;DefaultDllOverrides[i].key;i++)
391 AddLoadOrderSet(
392 DefaultDllOverrides[i].key,
393 DefaultDllOverrides[i].value,
394 FALSE
398 /* Read the explicitely defined orders for specific modules as an entire section */
399 nbuffer = PROFILE_GetWineIniString("DllOverrides", NULL, "", buffer, sizeof(buffer));
400 if(nbuffer == BUFFERSIZE-2)
402 ERR(module, "BUFFERSIZE %d is too small to read [DllOverrides]. Needs to grow in the source\n", BUFFERSIZE);
403 return FALSE;
405 if(nbuffer)
407 /* We only have the keys in the buffer, not the values */
408 char *key;
409 char value[BUFFERSIZE];
410 char *next;
412 for(key = buffer; *key; key = next)
414 next = key + strlen(key) + 1;
416 nbuffer = PROFILE_GetWineIniString("DllOverrides", key, "", value, sizeof(value));
417 if(!nbuffer)
419 ERR(module, "Module(s) '%s' will always fail to load. Are you sure you want this?\n", key);
420 value[0] = '\0'; /* Just in case */
422 if(nbuffer == BUFFERSIZE-2)
424 ERR(module, "BUFFERSIZE %d is too small to read [DllOverrides] key '%s'. Needs to grow in the source\n", BUFFERSIZE, key);
425 return FALSE;
428 TRACE(module, "Key '%s' uses override '%s'\n", key, value);
430 if(!AddLoadOrderSet(key, value, TRUE))
431 return FALSE;
435 /* Add the commandline overrides to the pool */
436 if(!ParseCommandlineOverrides())
438 MSG( "Syntax: -dll name[,name[,...]]={native|elfdll|so|builtin}[,{n|e|s|b}[,...]][:...]\n"
439 " - 'name' is the name of any dll without extension\n"
440 " - the order of loading (native, elfdll, so and builtin) can be abbreviated\n"
441 " with the first letter\n"
442 " - different loadorders for different dlls can be specified by seperating the\n"
443 " commandline entries with a ':'\n"
444 " Example:\n"
445 " -dll comdlg32,commdlg=n:shell,shell32=b\n"
447 return FALSE;
450 /* Sort the array for quick lookup */
451 qsort(module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
453 /* Check the pairs of dlls */
454 nbuffer = PROFILE_GetWineIniString("DllPairs", NULL, "", buffer, sizeof(buffer));
455 if(nbuffer == BUFFERSIZE-2)
457 ERR(module, "BUFFERSIZE %d is too small to read [DllPairs]. Needs to grow in the source\n", BUFFERSIZE);
458 return FALSE;
460 if(nbuffer)
462 /* We only have the keys in the buffer, not the values */
463 char *key;
464 char value[BUFFERSIZE];
465 char *next;
467 for(key = buffer; *key; key = next)
469 module_loadorder_t *plo1, *plo2;
471 next = key + strlen(key) + 1;
473 nbuffer = PROFILE_GetWineIniString("DllPairs", key, "", value, sizeof(value));
474 if(!nbuffer)
476 ERR(module, "Module pair '%s' is not associated with another module?\n", key);
477 continue;
479 if(nbuffer == BUFFERSIZE-2)
481 ERR(module, "BUFFERSIZE %d is too small to read [DllPairs] key '%s'. Needs to grow in the source\n", BUFFERSIZE, key);
482 return FALSE;
485 plo1 = MODULE_GetLoadOrder(key);
486 plo2 = MODULE_GetLoadOrder(value);
487 assert(plo1 && plo2);
489 if(memcmp(plo1->loadorder, plo2->loadorder, sizeof(plo1->loadorder)))
490 MSG("Warning: Modules '%s' and '%s' have different loadorder which may cause trouble\n", key, value);
494 if(TRACE_ON(module))
496 int i, j;
497 static char types[6] = "-NESB";
499 for(i = 0; i < nmodule_loadorder; i++)
501 DPRINTF("%3d: %-12s:", i, module_loadorder[i].modulename);
502 for(j = 0; j < MODULE_LOADORDER_NTYPES; j++)
503 DPRINTF(" %c", types[module_loadorder[i].loadorder[j] % (MODULE_LOADORDER_NTYPES+1)]);
504 DPRINTF("\n");
508 return TRUE;
512 /***************************************************************************
513 * MODULE_GetLoadOrder (internal)
515 * Locate the loadorder of a module.
516 * Any path is stripped from the path-argument and so are the extension
517 * '.dll' and '.exe'. A lookup in the table can yield an override for the
518 * specific dll. Otherwise the default load order is returned.
520 module_loadorder_t *MODULE_GetLoadOrder(const char *path)
522 module_loadorder_t lo, *tmp;
523 char fname[256];
524 char *cptr;
525 char *name;
526 int len;
528 assert(path != NULL);
530 /* Strip path information */
531 cptr = strrchr(path, '\\');
532 if(!cptr)
533 name = strrchr(path, '/');
534 else
535 name = strrchr(cptr, '/');
537 if(!name)
538 name = cptr ? cptr+1 : (char *)path;
539 else
540 name++;
542 if((cptr = strchr(name, ':')) != NULL) /* Also strip drive if in format 'C:MODULE.DLL' */
543 name = cptr+1;
545 len = strlen(name);
546 if(len >= sizeof(fname) || len <= 0)
548 ERR(module, "Path '%s' -> '%s' reduces to zilch or just too large...\n", path, name);
549 return &default_loadorder;
552 strcpy(fname, name);
553 if(len >= 4 && (!lstrcmpiA(fname+len-4, ".dll") || !lstrcmpiA(fname+len-4, ".exe")))
554 fname[len-4] = '\0';
556 lo.modulename = fname;
557 tmp = bsearch(&lo, module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
559 TRACE(module, "Looking for '%s' (%s), found '%s'\n", path, fname, tmp ? tmp->modulename : "<nothing>");
561 if(!tmp)
562 return &default_loadorder;
563 return tmp;