Added loadorder entry for the Linux glide3x library.
[wine/gsoc_dplay.git] / loader / loadorder.c
blob4cf8997a6f0c14e034269101e095a30c550c7de7
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 {"glide3x", "so,native"},
62 {"odbc32", "builtin"},
63 {"opengl32", "builtin,native"},
64 {"shfolder", "builtin,native"},
65 {NULL,NULL},
68 static const struct tagDllPair {
69 const char *dll1, *dll2;
70 } DllPairs[] = {
71 { "krnl386", "kernel32" },
72 { "gdi", "gdi32" },
73 { "user", "user32" },
74 { "commdlg", "comdlg32" },
75 { "commctrl", "comctl32" },
76 { "ver", "version" },
77 { "shell", "shell32" },
78 { "lzexpand", "lz32" },
79 { "mmsystem", "winmm" },
80 { "msvideo", "msvfw32" },
81 { "winsock", "wsock32" },
82 { NULL, NULL }
85 /***************************************************************************
86 * cmp_sort_func (internal, static)
88 * Sorting and comparing function used in sort and search of loadorder
89 * entries.
91 static int cmp_sort_func(const void *s1, const void *s2)
93 return strcasecmp(((module_loadorder_t *)s1)->modulename, ((module_loadorder_t *)s2)->modulename);
97 /***************************************************************************
98 * get_tok (internal, static)
100 * strtok wrapper for non-destructive buffer writing.
101 * NOTE: strtok is not reentrant and therefore this code is neither.
103 static char *get_tok(const char *str, const char *delim)
105 static char *buf = NULL;
106 char *cptr;
108 if(!str && !buf)
109 return NULL;
111 if(str && buf)
113 HeapFree(GetProcessHeap(), 0, buf);
114 buf = NULL;
117 if(str && !buf)
119 buf = HEAP_strdupA(GetProcessHeap(), 0, str);
120 cptr = strtok(buf, delim);
122 else
124 cptr = strtok(NULL, delim);
127 if(!cptr)
129 HeapFree(GetProcessHeap(), 0, buf);
130 buf = NULL;
132 return cptr;
136 /***************************************************************************
137 * ParseLoadOrder (internal, static)
139 * Parses the loadorder options from the configuration and puts it into
140 * a structure.
142 static BOOL ParseLoadOrder(char *order, module_loadorder_t *mlo)
144 char *cptr;
145 int n = 0;
147 memset(mlo->loadorder, 0, sizeof(mlo->loadorder));
149 cptr = get_tok(order, ", \t");
150 while(cptr)
152 char type = MODULE_LOADORDER_INVALID;
154 if(n >= MODULE_LOADORDER_NTYPES)
156 ERR("More than existing %d module-types specified, rest ignored", MODULE_LOADORDER_NTYPES);
157 break;
160 switch(*cptr)
162 case 'N': /* Native */
163 case 'n': type = MODULE_LOADORDER_DLL; break;
165 case 'E': /* Elfdll */
166 case 'e': type = MODULE_LOADORDER_ELFDLL; break;
168 case 'S': /* So */
169 case 's': type = MODULE_LOADORDER_SO; break;
171 case 'B': /* Builtin */
172 case 'b': type = MODULE_LOADORDER_BI; break;
174 default:
175 ERR("Invalid load order module-type '%s', ignored\n", cptr);
178 if(type != MODULE_LOADORDER_INVALID)
180 mlo->loadorder[n++] = type;
182 cptr = get_tok(NULL, ", \t");
184 return TRUE;
188 /***************************************************************************
189 * AddLoadOrder (internal, static)
191 * Adds an entry in the list of overrides. If the entry exists, then the
192 * override parameter determines whether it will be overwritten.
194 static BOOL AddLoadOrder(module_loadorder_t *plo, BOOL override)
196 int i;
198 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
200 for(i = 0; i < nmodule_loadorder; i++)
202 if(!cmp_sort_func(plo, &module_loadorder[i]))
204 if(!override)
205 ERR("Module '%s' is already in the list of overrides, using first definition\n", plo->modulename);
206 else
207 memcpy(module_loadorder[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
208 return TRUE;
212 if(nmodule_loadorder >= nmodule_loadorder_alloc)
214 /* No space in current array, make it larger */
215 nmodule_loadorder_alloc += LOADORDER_ALLOC_CLUSTER;
216 module_loadorder = (module_loadorder_t *)HeapReAlloc(GetProcessHeap(),
218 module_loadorder,
219 nmodule_loadorder_alloc * sizeof(module_loadorder_t));
220 if(!module_loadorder)
222 MESSAGE("Virtual memory exhausted\n");
223 exit(1);
226 memcpy(module_loadorder[nmodule_loadorder].loadorder, plo->loadorder, sizeof(plo->loadorder));
227 module_loadorder[nmodule_loadorder].modulename = HEAP_strdupA(GetProcessHeap(), 0, plo->modulename);
228 nmodule_loadorder++;
229 return TRUE;
233 /***************************************************************************
234 * AddLoadOrderSet (internal, static)
236 * Adds a set of entries in the list of overrides from the key parameter.
237 * If the entry exists, then the override parameter determines whether it
238 * will be overwritten.
240 static BOOL AddLoadOrderSet(char *key, char *order, BOOL override)
242 module_loadorder_t ldo;
243 char *cptr;
245 /* Parse the loadorder before the rest because strtok is not reentrant */
246 if(!ParseLoadOrder(order, &ldo))
247 return FALSE;
249 cptr = get_tok(key, ", \t");
250 while(cptr)
252 char *ext = strrchr(cptr, '.');
253 if(ext)
255 if(strlen(ext) == 4 && (!strcasecmp(ext, ".dll") || !strcasecmp(ext, ".exe")))
256 MESSAGE("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr);
259 ldo.modulename = cptr;
260 if(!AddLoadOrder(&ldo, override))
261 return FALSE;
262 cptr = get_tok(NULL, ", \t");
264 return TRUE;
268 /***************************************************************************
269 * ParseCommandlineOverrides (internal, static)
271 * The commandline is in the form:
272 * name[,name,...]=native[,b,...][:...]
274 static BOOL ParseCommandlineOverrides(void)
276 char *cpy;
277 char *key;
278 char *next;
279 char *value;
280 BOOL retval = TRUE;
282 if(!Options.dllFlags)
283 return TRUE;
285 cpy = HEAP_strdupA(GetProcessHeap(), 0, Options.dllFlags);
286 key = cpy;
287 next = key;
288 for(; next; key = next)
290 next = strchr(key, ':');
291 if(next)
293 *next = '\0';
294 next++;
296 value = strchr(key, '=');
297 if(!value)
299 retval = FALSE;
300 goto endit;
302 *value = '\0';
303 value++;
305 TRACE("Commandline override '%s' = '%s'\n", key, value);
307 if(!AddLoadOrderSet(key, value, TRUE))
309 retval = FALSE;
310 goto endit;
313 endit:
314 HeapFree(GetProcessHeap(), 0, cpy);
315 return retval;;
319 /***************************************************************************
320 * MODULE_InitLoadOrder (internal)
322 * Initialize the load order from the wine.conf file.
323 * The section has the following format:
324 * Section:
325 * [DllDefaults]
327 * Keys:
328 * EXTRA_LD_LIBRARY_PATH=/usr/local/lib/wine[:/more/path/to/search[:...]]
329 * The path will be appended to any existing LD_LIBRARY_PATH from the
330 * environment (see note in code below).
332 * DefaultLoadOrder=native,elfdll,so,builtin
333 * A comma separated list of module types to try to load in that specific
334 * order. The DefaultLoadOrder key is used as a fallback when a module is
335 * not specified explicitly. If the DefaultLoadOrder key is not found,
336 * then the order "dll,elfdll,so,bi" is used
337 * The possible module types are:
338 * - native Native windows dll files
339 * - elfdll Dlls encapsulated in .so libraries
340 * - so Native .so libraries mapped to dlls
341 * - builtin Built-in modules
343 * Case is not important and only the first letter of each type is enough to
344 * identify the type n[ative], e[lfdll], s[o], b[uiltin]. Also whitespace is
345 * ignored.
346 * E.g.:
347 * n,el ,s , b
348 * is equal to:
349 * native,elfdll,so,builtin
351 * Section:
352 * [DllOverrides]
354 * Keys:
355 * There are no explicit keys defined other than module/library names. A comma
356 * separated list of modules is followed by an assignment of the load-order
357 * for these specific modules. See above for possible types. You should not
358 * specify an extension.
359 * Examples:
360 * kernel32, gdi32, user32 = builtin
361 * kernel, gdi, user = builtin
362 * comdlg32 = elfdll, native, builtin
363 * commdlg = native, builtin
364 * version, ver = elfdll, native, builtin
368 #define BUFFERSIZE 1024
370 BOOL MODULE_InitLoadOrder(void)
372 char buffer[BUFFERSIZE];
373 char key[256];
374 int nbuffer;
375 int idx;
376 const struct tagDllPair *dllpair;
378 #if defined(HAVE_DL_API)
379 /* Get/set the new LD_LIBRARY_PATH */
380 nbuffer = PROFILE_GetWineIniString("DllDefaults", "EXTRA_LD_LIBRARY_PATH", "", buffer, sizeof(buffer));
382 if(nbuffer)
384 extra_ld_library_path = HEAP_strdupA(GetProcessHeap(), 0, buffer);
385 TRACE("Setting extra LD_LIBRARY_PATH=%s\n", buffer);
387 #endif
389 /* Get the default load order */
390 nbuffer = PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,b,e,s", buffer, sizeof(buffer));
391 if(!nbuffer)
393 MESSAGE("MODULE_InitLoadOrder: mysteriously read nothing from default loadorder\n");
394 return FALSE;
397 TRACE("Setting default loadorder=%s\n", buffer);
399 if(!ParseLoadOrder(buffer, &default_loadorder))
400 return FALSE;
401 default_loadorder.modulename = "<none>";
404 int i;
405 for (i=0;DefaultDllOverrides[i].key;i++)
406 AddLoadOrderSet(
407 DefaultDllOverrides[i].key,
408 DefaultDllOverrides[i].value,
409 FALSE
413 /* Read the explicitely defined orders for specific modules as an entire section */
414 idx = 0;
415 while (PROFILE_EnumWineIniString( "DllOverrides", idx++, key, sizeof(key),
416 buffer, sizeof(buffer)))
418 TRACE("Key '%s' uses override '%s'\n", key, buffer);
419 if(!AddLoadOrderSet(key, buffer, TRUE))
420 return FALSE;
423 /* Add the commandline overrides to the pool */
424 if(!ParseCommandlineOverrides())
426 MESSAGE( "Syntax: -dll name[,name[,...]]={native|elfdll|so|builtin}[,{n|e|s|b}[,...]][:...]\n"
427 " - 'name' is the name of any dll without extension\n"
428 " - the order of loading (native, elfdll, so and builtin) can be abbreviated\n"
429 " with the first letter\n"
430 " - different loadorders for different dlls can be specified by seperating the\n"
431 " commandline entries with a ':'\n"
432 " Example:\n"
433 " -dll comdlg32,commdlg=n:shell,shell32=b\n"
435 return FALSE;
438 /* Sort the array for quick lookup */
439 qsort(module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
441 /* Check the pairs of dlls */
442 dllpair = DllPairs;
443 while (dllpair->dll1)
445 module_loadorder_t *plo1, *plo2;
446 plo1 = MODULE_GetLoadOrder(dllpair->dll1);
447 plo2 = MODULE_GetLoadOrder(dllpair->dll2);
448 assert(plo1 && plo2);
449 if(memcmp(plo1->loadorder, plo2->loadorder, sizeof(plo1->loadorder)))
450 MESSAGE("Warning: Modules '%s' and '%s' have different loadorder which may cause trouble\n", dllpair->dll1, dllpair->dll2);
451 dllpair++;
454 if(TRACE_ON(module))
456 int i, j;
457 static char types[6] = "-NESB";
459 for(i = 0; i < nmodule_loadorder; i++)
461 DPRINTF("%3d: %-12s:", i, module_loadorder[i].modulename);
462 for(j = 0; j < MODULE_LOADORDER_NTYPES; j++)
463 DPRINTF(" %c", types[module_loadorder[i].loadorder[j] % (MODULE_LOADORDER_NTYPES+1)]);
464 DPRINTF("\n");
468 return TRUE;
472 /***************************************************************************
473 * MODULE_GetLoadOrder (internal)
475 * Locate the loadorder of a module.
476 * Any path is stripped from the path-argument and so are the extension
477 * '.dll' and '.exe'. A lookup in the table can yield an override for
478 * the specific dll. Otherwise the default load order is returned.
480 module_loadorder_t *MODULE_GetLoadOrder(const char *path)
482 module_loadorder_t lo, *tmp;
483 char fname[256];
484 char *cptr;
485 char *name;
486 int len;
488 TRACE("looking for %s\n", path);
490 assert(path != NULL);
492 /* Strip path information */
493 cptr = strrchr(path, '\\');
494 if(!cptr)
495 name = strrchr(path, '/');
496 else
497 name = strrchr(cptr, '/');
499 if(!name)
500 name = cptr ? cptr+1 : (char *)path;
501 else
502 name++;
504 if((cptr = strchr(name, ':')) != NULL) /* Also strip drive if in format 'C:MODULE.DLL' */
505 name = cptr+1;
507 len = strlen(name);
508 if(len >= sizeof(fname) || len <= 0)
510 ERR("Path '%s' -> '%s' reduces to zilch or just too large...\n", path, name);
511 return &default_loadorder;
514 strcpy(fname, name);
515 if(len >= 4 && (!lstrcmpiA(fname+len-4, ".dll") || !lstrcmpiA(fname+len-4, ".exe")))
516 fname[len-4] = '\0';
518 lo.modulename = fname;
519 tmp = bsearch(&lo, module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
521 TRACE("Looking for '%s' (%s), found '%s'\n", path, fname, tmp ? tmp->modulename : "<nothing>");
523 if(!tmp)
524 return &default_loadorder;
525 return tmp;