blob: fad87e25339e10f496b674c96d815ec083a3350a [file] [log] [blame]
Bertho Stultienscebb2f11999-04-18 12:09:21 +00001/*
2 * Module/Library loadorder
3 *
4 * Copyright 1999 Bertho Stultiens
5 */
6
7#include <stdlib.h>
8#include <string.h>
9#include <assert.h>
10
Bertho Stultiens3b5c29f1999-04-22 09:13:38 +000011#include "config.h"
Bertho Stultienscebb2f11999-04-18 12:09:21 +000012#include "windef.h"
13#include "options.h"
Bertho Stultienscebb2f11999-04-18 12:09:21 +000014#include "loadorder.h"
15#include "heap.h"
Bertho Stultiens3b5c29f1999-04-22 09:13:38 +000016#include "module.h"
17#include "elfdll.h"
Alexandre Julliarda099a551999-06-12 15:45:58 +000018#include "debugtools.h"
Bertho Stultienscebb2f11999-04-18 12:09:21 +000019
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000020DEFAULT_DEBUG_CHANNEL(module)
21
Bertho Stultienscebb2f11999-04-18 12:09:21 +000022
23/* #define DEBUG_LOADORDER */
24
25#define LOADORDER_ALLOC_CLUSTER 32 /* Allocate with 32 entries at a time */
26
27static module_loadorder_t default_loadorder;
28static module_loadorder_t *module_loadorder = NULL;
29static int nmodule_loadorder = 0;
30static int nmodule_loadorder_alloc = 0;
31
Marcus Meissnera739a691999-04-25 09:15:25 +000032static struct tagDllOverride {
33 char *key,*value;
34} DefaultDllOverrides[] = {
35 {"kernel32,gdi32,user32", "builtin"},
Eric Pouech448b34a1999-10-13 16:02:40 +000036 {"krnl386,gdi,user", "builtin"},
Marcus Meissnera739a691999-04-25 09:15:25 +000037 {"toolhelp", "builtin"},
38 {"comdlg32,commdlg", "elfdll,builtin,native"},
39 {"version,ver", "elfdll,builtin,native"},
40 {"shell32,shell", "builtin,native"},
Juergen Schmied1e5f0fe1999-08-15 17:03:37 +000041 {"shlwapi", "native,builtin"},
Marcus Meissnera739a691999-04-25 09:15:25 +000042 {"lz32,lzexpand", "builtin,native"},
43 {"commctrl,comctl32", "builtin,native"},
44 {"wsock32,winsock", "builtin"},
45 {"advapi32,crtdll,ntdll", "builtin,native"},
Huw D M Davies8bd75851999-09-04 11:05:41 +000046 {"mpr,winspool.drv", "builtin,native"},
Marcus Meissnera739a691999-04-25 09:15:25 +000047 {"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"},
Eric Pouechabe72271999-10-31 02:23:49 +000053 {"msacm.drv, midimap.drv", "builtin, native"},
Marcus Meissnera739a691999-04-25 09:15:25 +000054 {"w32skrnl", "builtin"},
55 {"wnaspi32,wow32", "builtin"},
56 {"system,display,wprocs ", "builtin"},
57 {"wineps", "builtin"},
Francois Gougetedf3e431999-11-07 21:22:17 +000058 {"icmp", "builtin"},
Marcus Meissner2b898801999-05-17 16:06:42 +000059 /* we have to use libglide2x.so instead of glide2x.dll ... */
60 {"glide2x", "so,native"},
Alexandre Julliard8551f8c1999-12-11 23:56:46 +000061 {"odbc32", "builtin"},
Marcus Meissnera739a691999-04-25 09:15:25 +000062 {NULL,NULL},
63};
64
Alexandre Julliard431cf322000-02-19 20:51:01 +000065static const struct tagDllPair {
66 const char *dll1, *dll2;
67} DllPairs[] = {
68 { "krnl386", "kernel32" },
69 { "gdi", "gdi32" },
70 { "user", "user32" },
71 { "commdlg", "comdlg32" },
72 { "commctrl", "comctl32" },
73 { "ver", "version" },
74 { "shell", "shell32" },
75 { "lzexpand", "lz32" },
76 { "mmsystem", "winmm" },
77 { "msvideo", "msvfw32" },
78 { "winsock", "wsock32" },
79 { NULL, NULL }
80};
81
Bertho Stultienscebb2f11999-04-18 12:09:21 +000082/***************************************************************************
83 * cmp_sort_func (internal, static)
84 *
85 * Sorting and comparing function used in sort and search of loadorder
86 * entries.
87 */
88static int cmp_sort_func(const void *s1, const void *s2)
89{
90 return strcasecmp(((module_loadorder_t *)s1)->modulename, ((module_loadorder_t *)s2)->modulename);
91}
92
93
94/***************************************************************************
95 * get_tok (internal, static)
96 *
97 * strtok wrapper for non-destructive buffer writing.
98 * NOTE: strtok is not reentrant and therefore this code is neither.
99 */
100static char *get_tok(const char *str, const char *delim)
101{
102 static char *buf = NULL;
103 char *cptr;
104
Bertho Stultiens94bb5bb1999-04-19 16:44:22 +0000105 if(!str && !buf)
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000106 return NULL;
107
108 if(str && buf)
109 {
Alexandre Julliard90476d62000-02-16 22:47:24 +0000110 HeapFree(GetProcessHeap(), 0, buf);
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000111 buf = NULL;
112 }
113
114 if(str && !buf)
115 {
Alexandre Julliard90476d62000-02-16 22:47:24 +0000116 buf = HEAP_strdupA(GetProcessHeap(), 0, str);
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000117 cptr = strtok(buf, delim);
118 }
119 else
120 {
121 cptr = strtok(NULL, delim);
122 }
123
124 if(!cptr)
125 {
Alexandre Julliard90476d62000-02-16 22:47:24 +0000126 HeapFree(GetProcessHeap(), 0, buf);
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000127 buf = NULL;
128 }
129 return cptr;
130}
131
132
133/***************************************************************************
134 * ParseLoadOrder (internal, static)
135 *
136 * Parses the loadorder options from the configuration and puts it into
137 * a structure.
138 */
139static BOOL ParseLoadOrder(char *order, module_loadorder_t *mlo)
140{
141 char *cptr;
142 int n = 0;
143
144 memset(mlo->loadorder, 0, sizeof(mlo->loadorder));
145
146 cptr = get_tok(order, ", \t");
147 while(cptr)
148 {
149 char type = MODULE_LOADORDER_INVALID;
150
151 if(n >= MODULE_LOADORDER_NTYPES)
152 {
Alexandre Julliarda099a551999-06-12 15:45:58 +0000153 ERR("More than existing %d module-types specified, rest ignored", MODULE_LOADORDER_NTYPES);
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000154 break;
155 }
156
157 switch(*cptr)
158 {
159 case 'N': /* Native */
160 case 'n': type = MODULE_LOADORDER_DLL; break;
161
162 case 'E': /* Elfdll */
163 case 'e': type = MODULE_LOADORDER_ELFDLL; break;
164
165 case 'S': /* So */
166 case 's': type = MODULE_LOADORDER_SO; break;
167
168 case 'B': /* Builtin */
169 case 'b': type = MODULE_LOADORDER_BI; break;
170
171 default:
Alexandre Julliarda099a551999-06-12 15:45:58 +0000172 ERR("Invalid load order module-type '%s', ignored\n", cptr);
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000173 }
174
175 if(type != MODULE_LOADORDER_INVALID)
176 {
177 mlo->loadorder[n++] = type;
178 }
179 cptr = get_tok(NULL, ", \t");
180 }
181 return TRUE;
182}
183
184
185/***************************************************************************
186 * AddLoadOrder (internal, static)
187 *
Andreas Mohr8cd93512000-01-29 21:12:58 +0000188 * Adds an entry in the list of overrides. If the entry exists, then the
189 * override parameter determines whether it will be overwritten.
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000190 */
191static BOOL AddLoadOrder(module_loadorder_t *plo, BOOL override)
192{
193 int i;
194
195 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
196
197 for(i = 0; i < nmodule_loadorder; i++)
198 {
199 if(!cmp_sort_func(plo, &module_loadorder[i]))
200 {
201 if(!override)
Alexandre Julliarda099a551999-06-12 15:45:58 +0000202 ERR("Module '%s' is already in the list of overrides, using first definition\n", plo->modulename);
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000203 else
204 memcpy(module_loadorder[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
205 return TRUE;
206 }
207 }
208
209 if(nmodule_loadorder >= nmodule_loadorder_alloc)
210 {
211 /* No space in current array, make it larger */
212 nmodule_loadorder_alloc += LOADORDER_ALLOC_CLUSTER;
Alexandre Julliard90476d62000-02-16 22:47:24 +0000213 module_loadorder = (module_loadorder_t *)HeapReAlloc(GetProcessHeap(),
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000214 0,
215 module_loadorder,
216 nmodule_loadorder_alloc * sizeof(module_loadorder_t));
217 if(!module_loadorder)
218 {
Alexandre Julliarda099a551999-06-12 15:45:58 +0000219 MESSAGE("Virtual memory exhausted\n");
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000220 exit(1);
221 }
222 }
223 memcpy(module_loadorder[nmodule_loadorder].loadorder, plo->loadorder, sizeof(plo->loadorder));
Alexandre Julliard90476d62000-02-16 22:47:24 +0000224 module_loadorder[nmodule_loadorder].modulename = HEAP_strdupA(GetProcessHeap(), 0, plo->modulename);
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000225 nmodule_loadorder++;
226 return TRUE;
227}
228
229
230/***************************************************************************
231 * AddLoadOrderSet (internal, static)
232 *
Andreas Mohr8cd93512000-01-29 21:12:58 +0000233 * Adds a set of entries in the list of overrides from the key parameter.
234 * If the entry exists, then the override parameter determines whether it
235 * will be overwritten.
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000236 */
237static BOOL AddLoadOrderSet(char *key, char *order, BOOL override)
238{
239 module_loadorder_t ldo;
240 char *cptr;
241
242 /* Parse the loadorder before the rest because strtok is not reentrant */
243 if(!ParseLoadOrder(order, &ldo))
244 return FALSE;
245
246 cptr = get_tok(key, ", \t");
247 while(cptr)
248 {
Bertho Stultiens71680701999-04-26 14:55:24 +0000249 char *ext = strrchr(cptr, '.');
250 if(ext)
251 {
252 if(strlen(ext) == 4 && (!strcasecmp(ext, ".dll") || !strcasecmp(ext, ".exe")))
Alexandre Julliarda099a551999-06-12 15:45:58 +0000253 MESSAGE("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr);
Bertho Stultiens71680701999-04-26 14:55:24 +0000254 }
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000255
256 ldo.modulename = cptr;
257 if(!AddLoadOrder(&ldo, override))
258 return FALSE;
259 cptr = get_tok(NULL, ", \t");
260 }
261 return TRUE;
262}
263
264
265/***************************************************************************
266 * ParseCommandlineOverrides (internal, static)
267 *
268 * The commandline is in the form:
269 * name[,name,...]=native[,b,...][:...]
270 */
271static BOOL ParseCommandlineOverrides(void)
272{
273 char *cpy;
274 char *key;
275 char *next;
276 char *value;
277 BOOL retval = TRUE;
278
279 if(!Options.dllFlags)
280 return TRUE;
281
Alexandre Julliard90476d62000-02-16 22:47:24 +0000282 cpy = HEAP_strdupA(GetProcessHeap(), 0, Options.dllFlags);
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000283 key = cpy;
284 next = key;
285 for(; next; key = next)
286 {
287 next = strchr(key, ':');
288 if(next)
289 {
290 *next = '\0';
291 next++;
292 }
293 value = strchr(key, '=');
294 if(!value)
295 {
296 retval = FALSE;
297 goto endit;
298 }
299 *value = '\0';
300 value++;
301
Alexandre Julliarda099a551999-06-12 15:45:58 +0000302 TRACE("Commandline override '%s' = '%s'\n", key, value);
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000303
304 if(!AddLoadOrderSet(key, value, TRUE))
305 {
306 retval = FALSE;
307 goto endit;
308 }
309 }
310endit:
Alexandre Julliard90476d62000-02-16 22:47:24 +0000311 HeapFree(GetProcessHeap(), 0, cpy);
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000312 return retval;;
313}
314
315
316/***************************************************************************
317 * MODULE_InitLoadOrder (internal)
318 *
319 * Initialize the load order from the wine.conf file.
Andreas Mohr8cd93512000-01-29 21:12:58 +0000320 * The section has the following format:
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000321 * Section:
322 * [DllDefaults]
323 *
324 * Keys:
325 * EXTRA_LD_LIBRARY_PATH=/usr/local/lib/wine[:/more/path/to/search[:...]]
326 * The path will be appended to any existing LD_LIBRARY_PATH from the
327 * environment (see note in code below).
328 *
329 * DefaultLoadOrder=native,elfdll,so,builtin
Andreas Mohr8cd93512000-01-29 21:12:58 +0000330 * A comma separated list of module types to try to load in that specific
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000331 * order. The DefaultLoadOrder key is used as a fallback when a module is
Andreas Mohr8cd93512000-01-29 21:12:58 +0000332 * not specified explicitly. If the DefaultLoadOrder key is not found,
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000333 * then the order "dll,elfdll,so,bi" is used
Andreas Mohr8cd93512000-01-29 21:12:58 +0000334 * The possible module types are:
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000335 * - native Native windows dll files
336 * - elfdll Dlls encapsulated in .so libraries
337 * - so Native .so libraries mapped to dlls
338 * - builtin Built-in modules
339 *
340 * Case is not important and only the first letter of each type is enough to
341 * identify the type n[ative], e[lfdll], s[o], b[uiltin]. Also whitespace is
342 * ignored.
343 * E.g.:
344 * n,el ,s , b
345 * is equal to:
346 * native,elfdll,so,builtin
347 *
348 * Section:
349 * [DllOverrides]
350 *
351 * Keys:
352 * There are no explicit keys defined other than module/library names. A comma
353 * separated list of modules is followed by an assignment of the load-order
354 * for these specific modules. See above for possible types. You should not
355 * specify an extension.
356 * Examples:
357 * kernel32, gdi32, user32 = builtin
358 * kernel, gdi, user = builtin
359 * comdlg32 = elfdll, native, builtin
360 * commdlg = native, builtin
361 * version, ver = elfdll, native, builtin
362 *
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000363 */
364
365#define BUFFERSIZE 1024
366
367BOOL MODULE_InitLoadOrder(void)
368{
369 char buffer[BUFFERSIZE];
Alexandre Julliard431cf322000-02-19 20:51:01 +0000370 char key[256];
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000371 int nbuffer;
Alexandre Julliard431cf322000-02-19 20:51:01 +0000372 int idx;
373 const struct tagDllPair *dllpair;
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000374
Marcus Meissnerf070fda1999-04-24 12:02:14 +0000375#if defined(HAVE_DL_API)
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000376 /* Get/set the new LD_LIBRARY_PATH */
377 nbuffer = PROFILE_GetWineIniString("DllDefaults", "EXTRA_LD_LIBRARY_PATH", "", buffer, sizeof(buffer));
378
379 if(nbuffer)
380 {
Alexandre Julliard90476d62000-02-16 22:47:24 +0000381 extra_ld_library_path = HEAP_strdupA(GetProcessHeap(), 0, buffer);
Alexandre Julliarda099a551999-06-12 15:45:58 +0000382 TRACE("Setting extra LD_LIBRARY_PATH=%s\n", buffer);
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000383 }
Bertho Stultiens3b5c29f1999-04-22 09:13:38 +0000384#endif
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000385
386 /* Get the default load order */
Alexandre Julliard318f4ce2000-01-31 05:02:49 +0000387 nbuffer = PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,b,e,s", buffer, sizeof(buffer));
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000388 if(!nbuffer)
389 {
Andreas Mohr8cd93512000-01-29 21:12:58 +0000390 MESSAGE("MODULE_InitLoadOrder: mysteriously read nothing from default loadorder\n");
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000391 return FALSE;
392 }
393
Alexandre Julliarda099a551999-06-12 15:45:58 +0000394 TRACE("Setting default loadorder=%s\n", buffer);
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000395
396 if(!ParseLoadOrder(buffer, &default_loadorder))
397 return FALSE;
398 default_loadorder.modulename = "<none>";
399
Marcus Meissnera739a691999-04-25 09:15:25 +0000400 {
401 int i;
402 for (i=0;DefaultDllOverrides[i].key;i++)
403 AddLoadOrderSet(
404 DefaultDllOverrides[i].key,
405 DefaultDllOverrides[i].value,
406 FALSE
407 );
408 }
409
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000410 /* Read the explicitely defined orders for specific modules as an entire section */
Alexandre Julliard431cf322000-02-19 20:51:01 +0000411 idx = 0;
412 while (PROFILE_EnumWineIniString( "DllOverrides", idx++, key, sizeof(key),
413 buffer, sizeof(buffer)))
414 {
415 TRACE("Key '%s' uses override '%s'\n", key, buffer);
416 if(!AddLoadOrderSet(key, buffer, TRUE))
417 return FALSE;
418 }
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000419
420 /* Add the commandline overrides to the pool */
421 if(!ParseCommandlineOverrides())
422 {
Alexandre Julliarda099a551999-06-12 15:45:58 +0000423 MESSAGE( "Syntax: -dll name[,name[,...]]={native|elfdll|so|builtin}[,{n|e|s|b}[,...]][:...]\n"
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000424 " - 'name' is the name of any dll without extension\n"
425 " - the order of loading (native, elfdll, so and builtin) can be abbreviated\n"
426 " with the first letter\n"
427 " - different loadorders for different dlls can be specified by seperating the\n"
428 " commandline entries with a ':'\n"
429 " Example:\n"
430 " -dll comdlg32,commdlg=n:shell,shell32=b\n"
431 );
432 return FALSE;
433 }
434
435 /* Sort the array for quick lookup */
436 qsort(module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
437
438 /* Check the pairs of dlls */
Alexandre Julliard431cf322000-02-19 20:51:01 +0000439 dllpair = DllPairs;
440 while (dllpair->dll1)
441 {
442 module_loadorder_t *plo1, *plo2;
443 plo1 = MODULE_GetLoadOrder(dllpair->dll1);
444 plo2 = MODULE_GetLoadOrder(dllpair->dll2);
445 assert(plo1 && plo2);
446 if(memcmp(plo1->loadorder, plo2->loadorder, sizeof(plo1->loadorder)))
447 MESSAGE("Warning: Modules '%s' and '%s' have different loadorder which may cause trouble\n", dllpair->dll1, dllpair->dll2);
448 dllpair++;
449 }
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000450
451 if(TRACE_ON(module))
452 {
453 int i, j;
454 static char types[6] = "-NESB";
455
456 for(i = 0; i < nmodule_loadorder; i++)
457 {
458 DPRINTF("%3d: %-12s:", i, module_loadorder[i].modulename);
459 for(j = 0; j < MODULE_LOADORDER_NTYPES; j++)
460 DPRINTF(" %c", types[module_loadorder[i].loadorder[j] % (MODULE_LOADORDER_NTYPES+1)]);
461 DPRINTF("\n");
462 }
463 }
464
465 return TRUE;
466}
467
468
469/***************************************************************************
470 * MODULE_GetLoadOrder (internal)
471 *
472 * Locate the loadorder of a module.
473 * Any path is stripped from the path-argument and so are the extension
Eric Pouechc672c001999-09-03 12:36:20 +0000474 * '.dll' and '.exe'. A lookup in the table can yield an override for
Huw D M Davies5fce6fb1999-07-30 18:06:35 +0000475 * the specific dll. Otherwise the default load order is returned.
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000476 */
477module_loadorder_t *MODULE_GetLoadOrder(const char *path)
478{
479 module_loadorder_t lo, *tmp;
480 char fname[256];
481 char *cptr;
482 char *name;
483 int len;
484
485 assert(path != NULL);
486
487 /* Strip path information */
488 cptr = strrchr(path, '\\');
489 if(!cptr)
490 name = strrchr(path, '/');
491 else
492 name = strrchr(cptr, '/');
493
494 if(!name)
495 name = cptr ? cptr+1 : (char *)path;
496 else
497 name++;
498
499 if((cptr = strchr(name, ':')) != NULL) /* Also strip drive if in format 'C:MODULE.DLL' */
500 name = cptr+1;
501
502 len = strlen(name);
503 if(len >= sizeof(fname) || len <= 0)
504 {
Alexandre Julliarda099a551999-06-12 15:45:58 +0000505 ERR("Path '%s' -> '%s' reduces to zilch or just too large...\n", path, name);
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000506 return &default_loadorder;
507 }
508
509 strcpy(fname, name);
Eric Pouechc672c001999-09-03 12:36:20 +0000510 if(len >= 4 && (!lstrcmpiA(fname+len-4, ".dll") || !lstrcmpiA(fname+len-4, ".exe")))
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000511 fname[len-4] = '\0';
512
513 lo.modulename = fname;
514 tmp = bsearch(&lo, module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
515
Alexandre Julliarda099a551999-06-12 15:45:58 +0000516 TRACE("Looking for '%s' (%s), found '%s'\n", path, fname, tmp ? tmp->modulename : "<nothing>");
Bertho Stultienscebb2f11999-04-18 12:09:21 +0000517
518 if(!tmp)
519 return &default_loadorder;
520 return tmp;
521}
522