Added to project. Currently incomplete but will update weekly.
[wine.git] / loader / loadorder.c
blob2a974f1dc7193f331c1c633a837c4e616bca4613
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 "module.h"
17 #include "elfdll.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 {
33 char *key,*value;
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"},
58 {"icmp", "builtin"},
59 /* we have to use libglide2x.so instead of glide2x.dll ... */
60 {"glide2x", "so,native"},
61 {"odbc32", "builtin"},
62 {"opengl32", "builtin,native"},
63 {NULL,NULL},
66 static const struct tagDllPair {
67 const char *dll1, *dll2;
68 } DllPairs[] = {
69 { "krnl386", "kernel32" },
70 { "gdi", "gdi32" },
71 { "user", "user32" },
72 { "commdlg", "comdlg32" },
73 { "commctrl", "comctl32" },
74 { "ver", "version" },
75 { "shell", "shell32" },
76 { "lzexpand", "lz32" },
77 { "mmsystem", "winmm" },
78 { "msvideo", "msvfw32" },
79 { "winsock", "wsock32" },
80 { NULL, NULL }
83 /***************************************************************************
84 * cmp_sort_func (internal, static)
86 * Sorting and comparing function used in sort and search of loadorder
87 * entries.
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;
104 char *cptr;
106 if(!str && !buf)
107 return NULL;
109 if(str && buf)
111 HeapFree(GetProcessHeap(), 0, buf);
112 buf = NULL;
115 if(str && !buf)
117 buf = HEAP_strdupA(GetProcessHeap(), 0, str);
118 cptr = strtok(buf, delim);
120 else
122 cptr = strtok(NULL, delim);
125 if(!cptr)
127 HeapFree(GetProcessHeap(), 0, buf);
128 buf = NULL;
130 return cptr;
134 /***************************************************************************
135 * ParseLoadOrder (internal, static)
137 * Parses the loadorder options from the configuration and puts it into
138 * a structure.
140 static BOOL ParseLoadOrder(char *order, module_loadorder_t *mlo)
142 char *cptr;
143 int n = 0;
145 memset(mlo->loadorder, 0, sizeof(mlo->loadorder));
147 cptr = get_tok(order, ", \t");
148 while(cptr)
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);
155 break;
158 switch(*cptr)
160 case 'N': /* Native */
161 case 'n': type = MODULE_LOADORDER_DLL; break;
163 case 'E': /* Elfdll */
164 case 'e': type = MODULE_LOADORDER_ELFDLL; break;
166 case 'S': /* So */
167 case 's': type = MODULE_LOADORDER_SO; break;
169 case 'B': /* Builtin */
170 case 'b': type = MODULE_LOADORDER_BI; break;
172 default:
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");
182 return TRUE;
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)
194 int i;
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]))
202 if(!override)
203 ERR("Module '%s' is already in the list of overrides, using first definition\n", plo->modulename);
204 else
205 memcpy(module_loadorder[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
206 return TRUE;
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(),
216 module_loadorder,
217 nmodule_loadorder_alloc * sizeof(module_loadorder_t));
218 if(!module_loadorder)
220 MESSAGE("Virtual memory exhausted\n");
221 exit(1);
224 memcpy(module_loadorder[nmodule_loadorder].loadorder, plo->loadorder, sizeof(plo->loadorder));
225 module_loadorder[nmodule_loadorder].modulename = HEAP_strdupA(GetProcessHeap(), 0, plo->modulename);
226 nmodule_loadorder++;
227 return TRUE;
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;
241 char *cptr;
243 /* Parse the loadorder before the rest because strtok is not reentrant */
244 if(!ParseLoadOrder(order, &ldo))
245 return FALSE;
247 cptr = get_tok(key, ", \t");
248 while(cptr)
250 char *ext = strrchr(cptr, '.');
251 if(ext)
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))
259 return FALSE;
260 cptr = get_tok(NULL, ", \t");
262 return TRUE;
266 /***************************************************************************
267 * ParseCommandlineOverrides (internal, static)
269 * The commandline is in the form:
270 * name[,name,...]=native[,b,...][:...]
272 static BOOL ParseCommandlineOverrides(void)
274 char *cpy;
275 char *key;
276 char *next;
277 char *value;
278 BOOL retval = TRUE;
280 if(!Options.dllFlags)
281 return TRUE;
283 cpy = HEAP_strdupA(GetProcessHeap(), 0, Options.dllFlags);
284 key = cpy;
285 next = key;
286 for(; next; key = next)
288 next = strchr(key, ':');
289 if(next)
291 *next = '\0';
292 next++;
294 value = strchr(key, '=');
295 if(!value)
297 retval = FALSE;
298 goto endit;
300 *value = '\0';
301 value++;
303 TRACE("Commandline override '%s' = '%s'\n", key, value);
305 if(!AddLoadOrderSet(key, value, TRUE))
307 retval = FALSE;
308 goto endit;
311 endit:
312 HeapFree(GetProcessHeap(), 0, cpy);
313 return retval;;
317 /***************************************************************************
318 * MODULE_InitLoadOrder (internal)
320 * Initialize the load order from the wine.conf file.
321 * The section has the following format:
322 * Section:
323 * [DllDefaults]
325 * Keys:
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
343 * ignored.
344 * E.g.:
345 * n,el ,s , b
346 * is equal to:
347 * native,elfdll,so,builtin
349 * Section:
350 * [DllOverrides]
352 * Keys:
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.
357 * Examples:
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];
371 char key[256];
372 int nbuffer;
373 int idx;
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));
380 if(nbuffer)
382 extra_ld_library_path = HEAP_strdupA(GetProcessHeap(), 0, buffer);
383 TRACE("Setting extra LD_LIBRARY_PATH=%s\n", buffer);
385 #endif
387 /* Get the default load order */
388 nbuffer = PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,b,e,s", buffer, sizeof(buffer));
389 if(!nbuffer)
391 MESSAGE("MODULE_InitLoadOrder: mysteriously read nothing from default loadorder\n");
392 return FALSE;
395 TRACE("Setting default loadorder=%s\n", buffer);
397 if(!ParseLoadOrder(buffer, &default_loadorder))
398 return FALSE;
399 default_loadorder.modulename = "<none>";
402 int i;
403 for (i=0;DefaultDllOverrides[i].key;i++)
404 AddLoadOrderSet(
405 DefaultDllOverrides[i].key,
406 DefaultDllOverrides[i].value,
407 FALSE
411 /* Read the explicitely defined orders for specific modules as an entire section */
412 idx = 0;
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))
418 return FALSE;
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"
430 " Example:\n"
431 " -dll comdlg32,commdlg=n:shell,shell32=b\n"
433 return FALSE;
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 */
440 dllpair = DllPairs;
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);
449 dllpair++;
452 if(TRACE_ON(module))
454 int i, j;
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)]);
462 DPRINTF("\n");
466 return TRUE;
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;
481 char fname[256];
482 char *cptr;
483 char *name;
484 int len;
486 TRACE("looking for %s\n", path);
488 assert(path != NULL);
490 /* Strip path information */
491 cptr = strrchr(path, '\\');
492 if(!cptr)
493 name = strrchr(path, '/');
494 else
495 name = strrchr(cptr, '/');
497 if(!name)
498 name = cptr ? cptr+1 : (char *)path;
499 else
500 name++;
502 if((cptr = strchr(name, ':')) != NULL) /* Also strip drive if in format 'C:MODULE.DLL' */
503 name = cptr+1;
505 len = strlen(name);
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;
512 strcpy(fname, name);
513 if(len >= 4 && (!lstrcmpiA(fname+len-4, ".dll") || !lstrcmpiA(fname+len-4, ".exe")))
514 fname[len-4] = '\0';
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>");
521 if(!tmp)
522 return &default_loadorder;
523 return tmp;