Fixed the case of "Winelib".
[wine.git] / loader / loadorder.c
blob87680bfbb8708951897a8d7e2160af8b07f6fb39
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 /* DLL order is irrelevant ! Gets sorted later. */
33 static struct tagDllOverride {
34 char *key,*value;
35 } DefaultDllOverrides[] = {
36 /* "system" DLLs */
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", "builtin,native"},
46 /* "new" interface */
47 {"comdlg32,commdlg", "builtin,native"},
48 {"shell32,shell", "builtin,native"},
49 {"shlwapi", "native,builtin"},
50 {"shfolder", "builtin,native"},
51 {"comctl32,commctrl", "builtin,native"},
52 /* network */
53 {"wsock32,ws2_32,winsock", "builtin"},
54 {"icmp", "builtin"},
55 /* multimedia */
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"},
66 /* other stuff */
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"},
73 {NULL,NULL},
76 static const struct tagDllPair {
77 const char *dll1, *dll2;
78 } DllPairs[] = {
79 { "krnl386", "kernel32" },
80 { "gdi", "gdi32" },
81 { "user", "user32" },
82 { "commdlg", "comdlg32" },
83 { "commctrl", "comctl32" },
84 { "ver", "version" },
85 { "shell", "shell32" },
86 { "lzexpand", "lz32" },
87 { "mmsystem", "winmm" },
88 { "msvideo", "msvfw32" },
89 { "winsock", "wsock32" },
90 { NULL, NULL }
93 /***************************************************************************
94 * cmp_sort_func (internal, static)
96 * Sorting and comparing function used in sort and search of loadorder
97 * entries.
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;
114 char *cptr;
116 if(!str && !buf)
117 return NULL;
119 if(str && buf)
121 HeapFree(GetProcessHeap(), 0, buf);
122 buf = NULL;
125 if(str && !buf)
127 buf = HEAP_strdupA(GetProcessHeap(), 0, str);
128 cptr = strtok(buf, delim);
130 else
132 cptr = strtok(NULL, delim);
135 if(!cptr)
137 HeapFree(GetProcessHeap(), 0, buf);
138 buf = NULL;
140 return cptr;
144 /***************************************************************************
145 * ParseLoadOrder (internal, static)
147 * Parses the loadorder options from the configuration and puts it into
148 * a structure.
150 static BOOL ParseLoadOrder(char *order, module_loadorder_t *mlo)
152 static int warn;
153 char *cptr;
154 int n = 0;
156 memset(mlo->loadorder, 0, sizeof(mlo->loadorder));
158 cptr = get_tok(order, ", \t");
159 while(cptr)
161 char type = MODULE_LOADORDER_INVALID;
163 if(n >= MODULE_LOADORDER_NTYPES)
165 ERR("More than existing %d module-types specified, rest ignored", MODULE_LOADORDER_NTYPES);
166 break;
169 switch(*cptr)
171 case 'N': /* Native */
172 case 'n': type = MODULE_LOADORDER_DLL; break;
174 case 'E': /* Elfdll */
175 case 'e':
176 if (!warn++) MESSAGE("Load order 'elfdll' no longer support, ignored\n");
177 break;
178 case 'S': /* So */
179 case 's': type = MODULE_LOADORDER_SO; break;
181 case 'B': /* Builtin */
182 case 'b': type = MODULE_LOADORDER_BI; break;
184 default:
185 ERR("Invalid load order module-type '%s', ignored\n", cptr);
188 if(type != MODULE_LOADORDER_INVALID)
190 mlo->loadorder[n++] = type;
192 cptr = get_tok(NULL, ", \t");
194 return TRUE;
198 /***************************************************************************
199 * AddLoadOrder (internal, static)
201 * Adds an entry in the list of overrides. If the entry exists, then the
202 * override parameter determines whether it will be overwritten.
204 static BOOL AddLoadOrder(module_loadorder_t *plo, BOOL override)
206 int i;
208 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
210 for(i = 0; i < nmodule_loadorder; i++)
212 if(!cmp_sort_func(plo, &module_loadorder[i]))
214 if(!override)
215 ERR("Module '%s' is already in the list of overrides, using first definition\n", plo->modulename);
216 else
217 memcpy(module_loadorder[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
218 return TRUE;
222 if(nmodule_loadorder >= nmodule_loadorder_alloc)
224 /* No space in current array, make it larger */
225 nmodule_loadorder_alloc += LOADORDER_ALLOC_CLUSTER;
226 module_loadorder = (module_loadorder_t *)HeapReAlloc(GetProcessHeap(),
228 module_loadorder,
229 nmodule_loadorder_alloc * sizeof(module_loadorder_t));
230 if(!module_loadorder)
232 MESSAGE("Virtual memory exhausted\n");
233 exit(1);
236 memcpy(module_loadorder[nmodule_loadorder].loadorder, plo->loadorder, sizeof(plo->loadorder));
237 module_loadorder[nmodule_loadorder].modulename = HEAP_strdupA(GetProcessHeap(), 0, plo->modulename);
238 nmodule_loadorder++;
239 return TRUE;
243 /***************************************************************************
244 * AddLoadOrderSet (internal, static)
246 * Adds a set of entries in the list of overrides from the key parameter.
247 * If the entry exists, then the override parameter determines whether it
248 * will be overwritten.
250 static BOOL AddLoadOrderSet(char *key, char *order, BOOL override)
252 module_loadorder_t ldo;
253 char *cptr;
255 /* Parse the loadorder before the rest because strtok is not reentrant */
256 if(!ParseLoadOrder(order, &ldo))
257 return FALSE;
259 cptr = get_tok(key, ", \t");
260 while(cptr)
262 char *ext = strrchr(cptr, '.');
263 if(ext)
265 if(strlen(ext) == 4 && (!strcasecmp(ext, ".dll") || !strcasecmp(ext, ".exe")))
266 MESSAGE("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr);
269 ldo.modulename = cptr;
270 if(!AddLoadOrder(&ldo, override))
271 return FALSE;
272 cptr = get_tok(NULL, ", \t");
274 return TRUE;
278 /***************************************************************************
279 * ParseCommandlineOverrides (internal, static)
281 * The commandline is in the form:
282 * name[,name,...]=native[,b,...][+...]
284 static BOOL ParseCommandlineOverrides(void)
286 char *cpy;
287 char *key;
288 char *next;
289 char *value;
290 BOOL retval = TRUE;
292 if(!Options.dllFlags)
293 return TRUE;
295 cpy = HEAP_strdupA(GetProcessHeap(), 0, Options.dllFlags);
296 key = cpy;
297 next = key;
298 for(; next; key = next)
300 next = strchr(key, '+');
301 if(next)
303 *next = '\0';
304 next++;
306 value = strchr(key, '=');
307 if(!value)
309 retval = FALSE;
310 goto endit;
312 *value = '\0';
313 value++;
315 TRACE("Commandline override '%s' = '%s'\n", key, value);
317 if(!AddLoadOrderSet(key, value, TRUE))
319 retval = FALSE;
320 goto endit;
323 endit:
324 HeapFree(GetProcessHeap(), 0, cpy);
325 return retval;;
329 /***************************************************************************
330 * MODULE_InitLoadOrder (internal)
332 * Initialize the load order from the wine.conf file.
333 * The section has the following format:
334 * Section:
335 * [DllDefaults]
337 * Keys:
338 * EXTRA_LD_LIBRARY_PATH=/usr/local/lib/wine[:/more/path/to/search[:...]]
339 * The path will be appended to any existing LD_LIBRARY_PATH from the
340 * environment (see note in code below).
342 * DefaultLoadOrder=native,so,builtin
343 * A comma separated list of module types to try to load in that specific
344 * order. The DefaultLoadOrder key is used as a fallback when a module is
345 * not specified explicitly. If the DefaultLoadOrder key is not found,
346 * then the order "dll,so,bi" is used
347 * The possible module types are:
348 * - native Native windows dll files
349 * - so Native .so libraries mapped to dlls
350 * - builtin Built-in modules
352 * Case is not important and only the first letter of each type is enough to
353 * identify the type n[ative], s[o], b[uiltin]. Also whitespace is
354 * ignored.
355 * E.g.:
356 * n,s , b
357 * is equal to:
358 * native,so,builtin
360 * Section:
361 * [DllOverrides]
363 * Keys:
364 * There are no explicit keys defined other than module/library names. A comma
365 * separated list of modules is followed by an assignment of the load-order
366 * for these specific modules. See above for possible types. You should not
367 * specify an extension.
368 * Examples:
369 * kernel32, gdi32, user32 = builtin
370 * kernel, gdi, user = builtin
371 * comdlg32 = native, builtin
372 * commdlg = native, builtin
373 * version, ver = native, builtin
377 #define BUFFERSIZE 1024
379 BOOL MODULE_InitLoadOrder(void)
381 char buffer[BUFFERSIZE];
382 char key[256];
383 int nbuffer;
384 int idx;
385 const struct tagDllPair *dllpair;
387 #if defined(HAVE_DL_API)
388 /* Get/set the new LD_LIBRARY_PATH */
389 nbuffer = PROFILE_GetWineIniString("DllDefaults", "EXTRA_LD_LIBRARY_PATH", "", buffer, sizeof(buffer));
391 if(nbuffer)
393 extra_ld_library_path = HEAP_strdupA(GetProcessHeap(), 0, buffer);
394 TRACE("Setting extra LD_LIBRARY_PATH=%s\n", buffer);
396 #endif
398 /* Get the default load order */
399 nbuffer = PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,b,s", buffer, sizeof(buffer));
400 if(!nbuffer)
402 MESSAGE("MODULE_InitLoadOrder: mysteriously read nothing from default loadorder\n");
403 return FALSE;
406 TRACE("Setting default loadorder=%s\n", buffer);
408 if(!ParseLoadOrder(buffer, &default_loadorder))
409 return FALSE;
410 default_loadorder.modulename = "<none>";
413 int i;
414 for (i=0;DefaultDllOverrides[i].key;i++)
415 AddLoadOrderSet(
416 DefaultDllOverrides[i].key,
417 DefaultDllOverrides[i].value,
418 FALSE
422 /* Read the explicitely defined orders for specific modules as an entire section */
423 idx = 0;
424 while (PROFILE_EnumWineIniString( "DllOverrides", idx++, key, sizeof(key),
425 buffer, sizeof(buffer)))
427 TRACE("Key '%s' uses override '%s'\n", key, buffer);
428 if(!AddLoadOrderSet(key, buffer, TRUE))
429 return FALSE;
432 /* Add the commandline overrides to the pool */
433 if(!ParseCommandlineOverrides())
435 MESSAGE( "Syntax: -dll name[,name[,...]]={native|so|builtin}[,{n|s|b}[,...]][+...]\n"
436 " - 'name' is the name of any dll without extension\n"
437 " - the order of loading (native, so and builtin) can be abbreviated\n"
438 " with the first letter\n"
439 " - different loadorders for different dlls can be specified by seperating the\n"
440 " commandline entries with a '+'\n"
441 " Example:\n"
442 " -dll comdlg32,commdlg=n+shell,shell32=b\n"
444 return FALSE;
447 /* Sort the array for quick lookup */
448 qsort(module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
450 /* Check the pairs of dlls */
451 dllpair = DllPairs;
452 while (dllpair->dll1)
454 module_loadorder_t *plo1, *plo2;
455 plo1 = MODULE_GetLoadOrder(dllpair->dll1, FALSE);
456 plo2 = MODULE_GetLoadOrder(dllpair->dll2, FALSE);
457 assert(plo1 && plo2);
458 if(memcmp(plo1->loadorder, plo2->loadorder, sizeof(plo1->loadorder)))
459 MESSAGE("Warning: Modules '%s' and '%s' have different loadorder which may cause trouble\n", dllpair->dll1, dllpair->dll2);
460 dllpair++;
463 if(TRACE_ON(module))
465 int i, j;
466 static char types[] = "-NSB";
468 for(i = 0; i < nmodule_loadorder; i++)
470 DPRINTF("%3d: %-12s:", i, module_loadorder[i].modulename);
471 for(j = 0; j < MODULE_LOADORDER_NTYPES; j++)
472 DPRINTF(" %c", types[module_loadorder[i].loadorder[j] % (MODULE_LOADORDER_NTYPES+1)]);
473 DPRINTF("\n");
477 return TRUE;
481 /***************************************************************************
482 * MODULE_GetLoadOrder (internal)
484 * Locate the loadorder of a module.
485 * Any path is stripped from the path-argument and so are the extension
486 * '.dll' and '.exe'. A lookup in the table can yield an override for
487 * the specific dll. Otherwise the default load order is returned.
489 module_loadorder_t *MODULE_GetLoadOrder(const char *path, BOOL win32 )
491 module_loadorder_t lo, *tmp;
492 char fname[256];
493 char sysdir[MAX_PATH+1];
494 char *cptr;
495 char *name;
496 int len;
498 TRACE("looking for %s\n", path);
500 assert(path != NULL);
502 if ( ! GetSystemDirectoryA ( sysdir, MAX_PATH ) )
503 return &default_loadorder; /* Hmmm ... */
505 /* Strip path information for 16 bit modules or if the module
506 resides in the system directory */
507 if ( !win32 || !strncasecmp ( sysdir, path, strlen (sysdir) ) )
510 cptr = strrchr(path, '\\');
511 if(!cptr)
512 name = strrchr(path, '/');
513 else
514 name = strrchr(cptr, '/');
516 if(!name)
517 name = cptr ? cptr+1 : (char *)path;
518 else
519 name++;
521 if((cptr = strchr(name, ':')) != NULL) /* Also strip drive if in format 'C:MODULE.DLL' */
522 name = cptr+1;
524 else
525 name = (char *)path;
527 len = strlen(name);
528 if(len >= sizeof(fname) || len <= 0)
530 ERR("Path '%s' -> '%s' reduces to zilch or just too large...\n", path, name);
531 return &default_loadorder;
534 strcpy(fname, name);
535 if(len >= 4 && (!strcasecmp(fname+len-4, ".dll") || !strcasecmp(fname+len-4, ".exe")))
536 fname[len-4] = '\0';
538 lo.modulename = fname;
539 tmp = bsearch(&lo, module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
541 TRACE("Looking for '%s' (%s), found '%s'\n", path, fname, tmp ? tmp->modulename : "<nothing>");
543 if(!tmp)
544 return &default_loadorder;
545 return tmp;