2 * Module/Library loadorder
4 * Copyright 1999 Bertho Stultiens
14 #include "loadorder.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
{
35 } DefaultDllOverrides
[] = {
37 {"kernel32,gdi32,user32", "builtin"},
38 {"krnl386,gdi,user", "builtin"},
39 {"toolhelp", "builtin"},
40 {"system,display", "builtin"},
41 {"w32skrnl,wow32", "builtin"},
42 {"advapi32,crtdll,ntdll", "builtin,native"},
43 {"lz32,lzexpand", "builtin,native"},
44 {"version,ver", "elfdll,builtin,native"},
46 {"comdlg32,commdlg", "elfdll,builtin,native"},
47 {"shell32,shell", "builtin,native"},
48 {"shlwapi", "native,builtin"},
49 {"shfolder", "builtin,native"},
50 {"comctl32,commctrl", "builtin,native"},
52 {"wsock32,winsock", "builtin"},
53 {"ws2_32", "builtin"},
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 libglide2x.so instead of glide2x.dll ... */
65 {"glide2x", "so,native"},
66 {"glide3x", "so,native"},
68 {"mpr,winspool.drv", "builtin,native"},
69 {"wnaspi32,winaspi", "builtin"},
70 {"odbc32", "builtin"},
71 {"rpcrt4", "native,builtin"},
72 /* non-windows DLLs */
73 {"wineps,wprocs,x11drv", "builtin"},
77 static const struct tagDllPair
{
78 const char *dll1
, *dll2
;
80 { "krnl386", "kernel32" },
83 { "commdlg", "comdlg32" },
84 { "commctrl", "comctl32" },
86 { "shell", "shell32" },
87 { "lzexpand", "lz32" },
88 { "mmsystem", "winmm" },
89 { "msvideo", "msvfw32" },
90 { "winsock", "wsock32" },
94 /***************************************************************************
95 * cmp_sort_func (internal, static)
97 * Sorting and comparing function used in sort and search of loadorder
100 static int cmp_sort_func(const void *s1
, const void *s2
)
102 return strcasecmp(((module_loadorder_t
*)s1
)->modulename
, ((module_loadorder_t
*)s2
)->modulename
);
106 /***************************************************************************
107 * get_tok (internal, static)
109 * strtok wrapper for non-destructive buffer writing.
110 * NOTE: strtok is not reentrant and therefore this code is neither.
112 static char *get_tok(const char *str
, const char *delim
)
114 static char *buf
= NULL
;
122 HeapFree(GetProcessHeap(), 0, buf
);
128 buf
= HEAP_strdupA(GetProcessHeap(), 0, str
);
129 cptr
= strtok(buf
, delim
);
133 cptr
= strtok(NULL
, delim
);
138 HeapFree(GetProcessHeap(), 0, buf
);
145 /***************************************************************************
146 * ParseLoadOrder (internal, static)
148 * Parses the loadorder options from the configuration and puts it into
151 static BOOL
ParseLoadOrder(char *order
, module_loadorder_t
*mlo
)
156 memset(mlo
->loadorder
, 0, sizeof(mlo
->loadorder
));
158 cptr
= get_tok(order
, ", \t");
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
);
171 case 'N': /* Native */
172 case 'n': type
= MODULE_LOADORDER_DLL
; break;
174 case 'E': /* Elfdll */
175 case 'e': type
= MODULE_LOADORDER_ELFDLL
; break;
178 case 's': type
= MODULE_LOADORDER_SO
; break;
180 case 'B': /* Builtin */
181 case 'b': type
= MODULE_LOADORDER_BI
; break;
184 ERR("Invalid load order module-type '%s', ignored\n", cptr
);
187 if(type
!= MODULE_LOADORDER_INVALID
)
189 mlo
->loadorder
[n
++] = type
;
191 cptr
= get_tok(NULL
, ", \t");
197 /***************************************************************************
198 * AddLoadOrder (internal, static)
200 * Adds an entry in the list of overrides. If the entry exists, then the
201 * override parameter determines whether it will be overwritten.
203 static BOOL
AddLoadOrder(module_loadorder_t
*plo
, BOOL override
)
207 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
209 for(i
= 0; i
< nmodule_loadorder
; i
++)
211 if(!cmp_sort_func(plo
, &module_loadorder
[i
]))
214 ERR("Module '%s' is already in the list of overrides, using first definition\n", plo
->modulename
);
216 memcpy(module_loadorder
[i
].loadorder
, plo
->loadorder
, sizeof(plo
->loadorder
));
221 if(nmodule_loadorder
>= nmodule_loadorder_alloc
)
223 /* No space in current array, make it larger */
224 nmodule_loadorder_alloc
+= LOADORDER_ALLOC_CLUSTER
;
225 module_loadorder
= (module_loadorder_t
*)HeapReAlloc(GetProcessHeap(),
228 nmodule_loadorder_alloc
* sizeof(module_loadorder_t
));
229 if(!module_loadorder
)
231 MESSAGE("Virtual memory exhausted\n");
235 memcpy(module_loadorder
[nmodule_loadorder
].loadorder
, plo
->loadorder
, sizeof(plo
->loadorder
));
236 module_loadorder
[nmodule_loadorder
].modulename
= HEAP_strdupA(GetProcessHeap(), 0, plo
->modulename
);
242 /***************************************************************************
243 * AddLoadOrderSet (internal, static)
245 * Adds a set of entries in the list of overrides from the key parameter.
246 * If the entry exists, then the override parameter determines whether it
247 * will be overwritten.
249 static BOOL
AddLoadOrderSet(char *key
, char *order
, BOOL override
)
251 module_loadorder_t ldo
;
254 /* Parse the loadorder before the rest because strtok is not reentrant */
255 if(!ParseLoadOrder(order
, &ldo
))
258 cptr
= get_tok(key
, ", \t");
261 char *ext
= strrchr(cptr
, '.');
264 if(strlen(ext
) == 4 && (!strcasecmp(ext
, ".dll") || !strcasecmp(ext
, ".exe")))
265 MESSAGE("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr
);
268 ldo
.modulename
= cptr
;
269 if(!AddLoadOrder(&ldo
, override
))
271 cptr
= get_tok(NULL
, ", \t");
277 /***************************************************************************
278 * ParseCommandlineOverrides (internal, static)
280 * The commandline is in the form:
281 * name[,name,...]=native[,b,...][+...]
283 static BOOL
ParseCommandlineOverrides(void)
291 if(!Options
.dllFlags
)
294 cpy
= HEAP_strdupA(GetProcessHeap(), 0, Options
.dllFlags
);
297 for(; next
; key
= next
)
299 next
= strchr(key
, '+');
305 value
= strchr(key
, '=');
314 TRACE("Commandline override '%s' = '%s'\n", key
, value
);
316 if(!AddLoadOrderSet(key
, value
, TRUE
))
323 HeapFree(GetProcessHeap(), 0, cpy
);
328 /***************************************************************************
329 * MODULE_InitLoadOrder (internal)
331 * Initialize the load order from the wine.conf file.
332 * The section has the following format:
337 * EXTRA_LD_LIBRARY_PATH=/usr/local/lib/wine[:/more/path/to/search[:...]]
338 * The path will be appended to any existing LD_LIBRARY_PATH from the
339 * environment (see note in code below).
341 * DefaultLoadOrder=native,elfdll,so,builtin
342 * A comma separated list of module types to try to load in that specific
343 * order. The DefaultLoadOrder key is used as a fallback when a module is
344 * not specified explicitly. If the DefaultLoadOrder key is not found,
345 * then the order "dll,elfdll,so,bi" is used
346 * The possible module types are:
347 * - native Native windows dll files
348 * - elfdll Dlls encapsulated in .so libraries
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], e[lfdll], s[o], b[uiltin]. Also whitespace is
358 * native,elfdll,so,builtin
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.
369 * kernel32, gdi32, user32 = builtin
370 * kernel, gdi, user = builtin
371 * comdlg32 = elfdll, native, builtin
372 * commdlg = native, builtin
373 * version, ver = elfdll, native, builtin
377 #define BUFFERSIZE 1024
379 BOOL
MODULE_InitLoadOrder(void)
381 char buffer
[BUFFERSIZE
];
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
));
393 extra_ld_library_path
= HEAP_strdupA(GetProcessHeap(), 0, buffer
);
394 TRACE("Setting extra LD_LIBRARY_PATH=%s\n", buffer
);
398 /* Get the default load order */
399 nbuffer
= PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,b,e,s", buffer
, sizeof(buffer
));
402 MESSAGE("MODULE_InitLoadOrder: mysteriously read nothing from default loadorder\n");
406 TRACE("Setting default loadorder=%s\n", buffer
);
408 if(!ParseLoadOrder(buffer
, &default_loadorder
))
410 default_loadorder
.modulename
= "<none>";
414 for (i
=0;DefaultDllOverrides
[i
].key
;i
++)
416 DefaultDllOverrides
[i
].key
,
417 DefaultDllOverrides
[i
].value
,
422 /* Read the explicitely defined orders for specific modules as an entire section */
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
))
432 /* Add the commandline overrides to the pool */
433 if(!ParseCommandlineOverrides())
435 MESSAGE( "Syntax: -dll name[,name[,...]]={native|elfdll|so|builtin}[,{n|e|s|b}[,...]][+...]\n"
436 " - 'name' is the name of any dll without extension\n"
437 " - the order of loading (native, elfdll, 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"
442 " -dll comdlg32,commdlg=n+shell,shell32=b\n"
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 */
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
);
466 static char types
[6] = "-NESB";
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)]);
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
;
493 char sysdir
[MAX_PATH
+1];
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
, '\\');
512 name
= strrchr(path
, '/');
514 name
= strrchr(cptr
, '/');
517 name
= cptr
? cptr
+1 : (char *)path
;
521 if((cptr
= strchr(name
, ':')) != NULL
) /* Also strip drive if in format 'C:MODULE.DLL' */
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
;
535 if(len
>= 4 && (!strcasecmp(fname
+len
-4, ".dll") || !strcasecmp(fname
+len
-4, ".exe")))
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>");
544 return &default_loadorder
;