2 * Module/Library loadorder
4 * Copyright 1999 Bertho Stultiens
14 #include "loadorder.h"
18 DEFAULT_DEBUG_CHANNEL(module
)
21 /* #define DEBUG_LOADORDER */
23 #define LOADORDER_ALLOC_CLUSTER 32 /* Allocate with 32 entries at a time */
25 static module_loadorder_t default_loadorder
;
26 static module_loadorder_t
*module_loadorder
= NULL
;
27 static int nmodule_loadorder
= 0;
28 static int nmodule_loadorder_alloc
= 0;
30 /***************************************************************************
31 * cmp_sort_func (internal, static)
33 * Sorting and comparing function used in sort and search of loadorder
36 static int cmp_sort_func(const void *s1
, const void *s2
)
38 return strcasecmp(((module_loadorder_t
*)s1
)->modulename
, ((module_loadorder_t
*)s2
)->modulename
);
42 /***************************************************************************
43 * get_tok (internal, static)
45 * strtok wrapper for non-destructive buffer writing.
46 * NOTE: strtok is not reentrant and therefore this code is neither.
48 static char *get_tok(const char *str
, const char *delim
)
50 static char *buf
= NULL
;
53 if(!str
&& (!buf
|| !index
))
58 HeapFree(SystemHeap
, 0, buf
);
64 buf
= HEAP_strdupA(SystemHeap
, 0, str
);
65 cptr
= strtok(buf
, delim
);
69 cptr
= strtok(NULL
, delim
);
74 HeapFree(SystemHeap
, 0, buf
);
81 /***************************************************************************
82 * ParseLoadOrder (internal, static)
84 * Parses the loadorder options from the configuration and puts it into
87 static BOOL
ParseLoadOrder(char *order
, module_loadorder_t
*mlo
)
92 memset(mlo
->loadorder
, 0, sizeof(mlo
->loadorder
));
94 cptr
= get_tok(order
, ", \t");
97 char type
= MODULE_LOADORDER_INVALID
;
99 if(n
>= MODULE_LOADORDER_NTYPES
)
101 ERR(module
, "More than existing %d module-types specified, rest ignored", MODULE_LOADORDER_NTYPES
);
107 case 'N': /* Native */
108 case 'n': type
= MODULE_LOADORDER_DLL
; break;
110 case 'E': /* Elfdll */
111 case 'e': type
= MODULE_LOADORDER_ELFDLL
; break;
114 case 's': type
= MODULE_LOADORDER_SO
; break;
116 case 'B': /* Builtin */
117 case 'b': type
= MODULE_LOADORDER_BI
; break;
120 ERR(module
, "Invalid load order module-type '%s', ignored\n", cptr
);
123 if(type
!= MODULE_LOADORDER_INVALID
)
125 mlo
->loadorder
[n
++] = type
;
127 cptr
= get_tok(NULL
, ", \t");
133 /***************************************************************************
134 * AddLoadOrder (internal, static)
136 * Adds an entry in the list of overrides. If the entry exists then the
137 * override parameter determines whether it will be overwriten.
139 static BOOL
AddLoadOrder(module_loadorder_t
*plo
, BOOL override
)
143 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
145 for(i
= 0; i
< nmodule_loadorder
; i
++)
147 if(!cmp_sort_func(plo
, &module_loadorder
[i
]))
150 ERR(module
, "Module '%s' is already in the list of overrides, using first definition\n", plo
->modulename
);
152 memcpy(module_loadorder
[i
].loadorder
, plo
->loadorder
, sizeof(plo
->loadorder
));
157 if(nmodule_loadorder
>= nmodule_loadorder_alloc
)
159 /* No space in current array, make it larger */
160 nmodule_loadorder_alloc
+= LOADORDER_ALLOC_CLUSTER
;
161 module_loadorder
= (module_loadorder_t
*)HeapReAlloc(SystemHeap
,
164 nmodule_loadorder_alloc
* sizeof(module_loadorder_t
));
165 if(!module_loadorder
)
167 MSG("Virtual memory exhausted\n");
171 memcpy(module_loadorder
[nmodule_loadorder
].loadorder
, plo
->loadorder
, sizeof(plo
->loadorder
));
172 module_loadorder
[nmodule_loadorder
].modulename
= HEAP_strdupA(SystemHeap
, 0, plo
->modulename
);
178 /***************************************************************************
179 * AddLoadOrderSet (internal, static)
181 * Adds an set of entries in the list of overrides from the key parameter.
182 * If the entry exists then the override parameter determines whether it
183 * will be overwriten.
185 static BOOL
AddLoadOrderSet(char *key
, char *order
, BOOL override
)
187 module_loadorder_t ldo
;
190 /* Parse the loadorder before the rest because strtok is not reentrant */
191 if(!ParseLoadOrder(order
, &ldo
))
194 cptr
= get_tok(key
, ", \t");
197 if(strchr(cptr
, '.'))
198 MSG("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr
);
200 ldo
.modulename
= cptr
;
201 if(!AddLoadOrder(&ldo
, override
))
203 cptr
= get_tok(NULL
, ", \t");
209 /***************************************************************************
210 * ParseCommandlineOverrides (internal, static)
212 * The commandline is in the form:
213 * name[,name,...]=native[,b,...][:...]
215 static BOOL
ParseCommandlineOverrides(void)
223 if(!Options
.dllFlags
)
226 cpy
= HEAP_strdupA(SystemHeap
, 0, Options
.dllFlags
);
229 for(; next
; key
= next
)
231 next
= strchr(key
, ':');
237 value
= strchr(key
, '=');
246 TRACE(module
, "Commandline override '%s' = '%s'\n", key
, value
);
248 if(!AddLoadOrderSet(key
, value
, TRUE
))
255 HeapFree(SystemHeap
, 0, cpy
);
260 /***************************************************************************
261 * MODULE_InitLoadOrder (internal)
263 * Initialize the load order from the wine.conf file.
264 * The section has tyhe following format:
269 * EXTRA_LD_LIBRARY_PATH=/usr/local/lib/wine[:/more/path/to/search[:...]]
270 * The path will be appended to any existing LD_LIBRARY_PATH from the
271 * environment (see note in code below).
273 * DefaultLoadOrder=native,elfdll,so,builtin
274 * A comma seperated list of module-types to try to load in that specific
275 * order. The DefaultLoadOrder key is used as a fallback when a module is
276 * not specified explicitely. If the DefaultLoadOrder key is not found,
277 * then the order "dll,elfdll,so,bi" is used
278 * The possible module-types are:
279 * - native Native windows dll files
280 * - elfdll Dlls encapsulated in .so libraries
281 * - so Native .so libraries mapped to dlls
282 * - builtin Built-in modules
284 * Case is not important and only the first letter of each type is enough to
285 * identify the type n[ative], e[lfdll], s[o], b[uiltin]. Also whitespace is
290 * native,elfdll,so,builtin
296 * There are no explicit keys defined other than module/library names. A comma
297 * separated list of modules is followed by an assignment of the load-order
298 * for these specific modules. See above for possible types. You should not
299 * specify an extension.
301 * kernel32, gdi32, user32 = builtin
302 * kernel, gdi, user = builtin
303 * comdlg32 = elfdll, native, builtin
304 * commdlg = native, builtin
305 * version, ver = elfdll, native, builtin
311 * This is a simple pairing in the form 'name1 = name2'. It is supposed to
312 * identify the dlls that cannot live without eachother unless they are
313 * loaded in the same format. Examples are common dialogs and controls,
314 * shell, kernel, gdi, user, etc...
315 * The code will issue a warning if the loadorder of these pairs are different
316 * and might cause hard-to-find bugs due to incompatible pairs loaded at
317 * run-time. Note that this pairing gives *no* guarantee that the pairs
318 * actually get loaded as the same type, nor that the correct versions are
319 * loaded (might be implemented later). It merely notes obvious trouble.
326 #define BUFFERSIZE 1024
328 BOOL
MODULE_InitLoadOrder(void)
330 char buffer
[BUFFERSIZE
];
333 /* Get/set the new LD_LIBRARY_PATH */
334 nbuffer
= PROFILE_GetWineIniString("DllDefaults", "EXTRA_LD_LIBRARY_PATH", "", buffer
, sizeof(buffer
));
338 extern char *_dl_library_path
;
339 char *ld_lib_path
= getenv("LD_LIBRARY_PATH");
343 * Append new path to current
345 char *tmp
= HEAP_strdupA(SystemHeap
, 0, buffer
);
346 sprintf(buffer
, "%s:%s", ld_lib_path
, tmp
);
347 HeapFree( SystemHeap
, 0, tmp
);
350 TRACE(module
, "Setting new LD_LIBRARY_PATH=%s\n", buffer
);
352 setenv("LD_LIBRARY_PATH", buffer
, 1);
355 * This is a cruel hack required to have libdl check this path.
356 * The problem is that libdl caches the environment variable
357 * and we won't get our modifications applied. We ensure the
358 * the correct search path by explicitely modifying the libdl
359 * internal variable which holds the path.
361 _dl_library_path
= HEAP_strdupA(SystemHeap
, 0, buffer
);
364 /* Get the default load order */
365 nbuffer
= PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,e,s,b", buffer
, sizeof(buffer
));
368 MSG("MODULE_InitLoadOrder: misteriously read nothing from default loadorder\n");
372 TRACE(module
, "Setting default loadorder=%s\n", buffer
);
374 if(!ParseLoadOrder(buffer
, &default_loadorder
))
376 default_loadorder
.modulename
= "<none>";
378 /* Read the explicitely defined orders for specific modules as an entire section */
379 nbuffer
= PROFILE_GetWineIniString("DllOverrides", NULL
, "", buffer
, sizeof(buffer
));
380 if(nbuffer
== BUFFERSIZE
-2)
382 ERR(module
, "BUFFERSIZE %d is too small to read [DllOverrides]. Needs to grow in the source\n", BUFFERSIZE
);
387 /* We only have the keys in the buffer, not the values */
389 char value
[BUFFERSIZE
];
392 for(key
= buffer
; *key
; key
= next
)
394 next
= key
+ strlen(key
) + 1;
396 nbuffer
= PROFILE_GetWineIniString("DllOverrides", key
, "", value
, sizeof(value
));
399 ERR(module
, "Module(s) '%s' will always fail to load. Are you sure you want this?\n", key
);
400 value
[0] = '\0'; /* Just in case */
402 if(nbuffer
== BUFFERSIZE
-2)
404 ERR(module
, "BUFFERSIZE %d is too small to read [DllOverrides] key '%s'. Needs to grow in the source\n", BUFFERSIZE
, key
);
408 TRACE(module
, "Key '%s' uses override '%s'\n", key
, value
);
410 if(!AddLoadOrderSet(key
, value
, FALSE
))
415 /* Add the commandline overrides to the pool */
416 if(!ParseCommandlineOverrides())
418 MSG( "Syntax: -dll name[,name[,...]]={native|elfdll|so|builtin}[,{n|e|s|b}[,...]][:...]\n"
419 " - 'name' is the name of any dll without extension\n"
420 " - the order of loading (native, elfdll, so and builtin) can be abbreviated\n"
421 " with the first letter\n"
422 " - different loadorders for different dlls can be specified by seperating the\n"
423 " commandline entries with a ':'\n"
425 " -dll comdlg32,commdlg=n:shell,shell32=b\n"
430 /* Sort the array for quick lookup */
431 qsort(module_loadorder
, nmodule_loadorder
, sizeof(module_loadorder
[0]), cmp_sort_func
);
433 /* Check the pairs of dlls */
434 nbuffer
= PROFILE_GetWineIniString("DllPairs", NULL
, "", buffer
, sizeof(buffer
));
435 if(nbuffer
== BUFFERSIZE
-2)
437 ERR(module
, "BUFFERSIZE %d is too small to read [DllPairs]. Needs to grow in the source\n", BUFFERSIZE
);
442 /* We only have the keys in the buffer, not the values */
444 char value
[BUFFERSIZE
];
447 for(key
= buffer
; *key
; key
= next
)
449 module_loadorder_t
*plo1
, *plo2
;
451 next
= key
+ strlen(key
) + 1;
453 nbuffer
= PROFILE_GetWineIniString("DllPairs", key
, "", value
, sizeof(value
));
456 ERR(module
, "Module pair '%s' is not associated with another module?\n", key
);
459 if(nbuffer
== BUFFERSIZE
-2)
461 ERR(module
, "BUFFERSIZE %d is too small to read [DllPairs] key '%s'. Needs to grow in the source\n", BUFFERSIZE
, key
);
465 plo1
= MODULE_GetLoadOrder(key
);
466 plo2
= MODULE_GetLoadOrder(value
);
467 assert(plo1
&& plo2
);
469 if(memcmp(plo1
->loadorder
, plo2
->loadorder
, sizeof(plo1
->loadorder
)))
470 MSG("Warning: Modules '%s' and '%s' have different loadorder which may cause trouble\n", key
, value
);
477 static char types
[6] = "-NESB";
479 for(i
= 0; i
< nmodule_loadorder
; i
++)
481 DPRINTF("%3d: %-12s:", i
, module_loadorder
[i
].modulename
);
482 for(j
= 0; j
< MODULE_LOADORDER_NTYPES
; j
++)
483 DPRINTF(" %c", types
[module_loadorder
[i
].loadorder
[j
] % (MODULE_LOADORDER_NTYPES
+1)]);
492 /***************************************************************************
493 * MODULE_GetLoadOrder (internal)
495 * Locate the loadorder of a module.
496 * Any path is stripped from the path-argument and so are the extension
497 * '.dll' and '.exe'. A lookup in the table can yield an override for the
498 * specific dll. Otherwise the default load order is returned.
500 module_loadorder_t
*MODULE_GetLoadOrder(const char *path
)
502 module_loadorder_t lo
, *tmp
;
508 assert(path
!= NULL
);
510 /* Strip path information */
511 cptr
= strrchr(path
, '\\');
513 name
= strrchr(path
, '/');
515 name
= strrchr(cptr
, '/');
518 name
= cptr
? cptr
+1 : (char *)path
;
522 if((cptr
= strchr(name
, ':')) != NULL
) /* Also strip drive if in format 'C:MODULE.DLL' */
526 if(len
>= sizeof(fname
) || len
<= 0)
528 ERR(module
, "Path '%s' -> '%s' reduces to zilch or just too large...\n", path
, name
);
529 return &default_loadorder
;
533 if(len
>= 4 && (!lstrcmpiA(fname
+len
-4, ".dll") || !lstrcmpiA(fname
+len
-4, ".exe")))
536 lo
.modulename
= fname
;
537 tmp
= bsearch(&lo
, module_loadorder
, nmodule_loadorder
, sizeof(module_loadorder
[0]), cmp_sort_func
);
539 TRACE(module
, "Looking for '%s' (%s), found '%s'\n", path
, fname
, tmp
? tmp
->modulename
: "<nothing>");
542 return &default_loadorder
;