2 * Module/Library loadorder
4 * Copyright 1999 Bertho Stultiens
14 #include "loadorder.h"
19 /* #define DEBUG_LOADORDER */
21 #define LOADORDER_ALLOC_CLUSTER 32 /* Allocate with 32 entries at a time */
23 static module_loadorder_t default_loadorder
;
24 static module_loadorder_t
*module_loadorder
= NULL
;
25 static int nmodule_loadorder
= 0;
26 static int nmodule_loadorder_alloc
= 0;
28 /***************************************************************************
29 * cmp_sort_func (internal, static)
31 * Sorting and comparing function used in sort and search of loadorder
34 static int cmp_sort_func(const void *s1
, const void *s2
)
36 return strcasecmp(((module_loadorder_t
*)s1
)->modulename
, ((module_loadorder_t
*)s2
)->modulename
);
40 /***************************************************************************
41 * get_tok (internal, static)
43 * strtok wrapper for non-destructive buffer writing.
44 * NOTE: strtok is not reentrant and therefore this code is neither.
46 static char *get_tok(const char *str
, const char *delim
)
48 static char *buf
= NULL
;
51 if(!str
&& (!buf
|| !index
))
56 HeapFree(SystemHeap
, 0, buf
);
62 buf
= HEAP_strdupA(SystemHeap
, 0, str
);
63 cptr
= strtok(buf
, delim
);
67 cptr
= strtok(NULL
, delim
);
72 HeapFree(SystemHeap
, 0, buf
);
79 /***************************************************************************
80 * ParseLoadOrder (internal, static)
82 * Parses the loadorder options from the configuration and puts it into
85 static BOOL
ParseLoadOrder(char *order
, module_loadorder_t
*mlo
)
90 memset(mlo
->loadorder
, 0, sizeof(mlo
->loadorder
));
92 cptr
= get_tok(order
, ", \t");
95 char type
= MODULE_LOADORDER_INVALID
;
97 if(n
>= MODULE_LOADORDER_NTYPES
)
99 ERR(module
, "More than existing %d module-types specified, rest ignored", MODULE_LOADORDER_NTYPES
);
105 case 'N': /* Native */
106 case 'n': type
= MODULE_LOADORDER_DLL
; break;
108 case 'E': /* Elfdll */
109 case 'e': type
= MODULE_LOADORDER_ELFDLL
; break;
112 case 's': type
= MODULE_LOADORDER_SO
; break;
114 case 'B': /* Builtin */
115 case 'b': type
= MODULE_LOADORDER_BI
; break;
118 ERR(module
, "Invalid load order module-type '%s', ignored\n", cptr
);
121 if(type
!= MODULE_LOADORDER_INVALID
)
123 mlo
->loadorder
[n
++] = type
;
125 cptr
= get_tok(NULL
, ", \t");
131 /***************************************************************************
132 * AddLoadOrder (internal, static)
134 * Adds an entry in the list of overrides. If the entry exists then the
135 * override parameter determines whether it will be overwriten.
137 static BOOL
AddLoadOrder(module_loadorder_t
*plo
, BOOL override
)
141 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
143 for(i
= 0; i
< nmodule_loadorder
; i
++)
145 if(!cmp_sort_func(plo
, &module_loadorder
[i
]))
148 ERR(module
, "Module '%s' is already in the list of overrides, using first definition\n", plo
->modulename
);
150 memcpy(module_loadorder
[i
].loadorder
, plo
->loadorder
, sizeof(plo
->loadorder
));
155 if(nmodule_loadorder
>= nmodule_loadorder_alloc
)
157 /* No space in current array, make it larger */
158 nmodule_loadorder_alloc
+= LOADORDER_ALLOC_CLUSTER
;
159 module_loadorder
= (module_loadorder_t
*)HeapReAlloc(SystemHeap
,
162 nmodule_loadorder_alloc
* sizeof(module_loadorder_t
));
163 if(!module_loadorder
)
165 MSG("Virtual memory exhausted\n");
169 memcpy(module_loadorder
[nmodule_loadorder
].loadorder
, plo
->loadorder
, sizeof(plo
->loadorder
));
170 module_loadorder
[nmodule_loadorder
].modulename
= HEAP_strdupA(SystemHeap
, 0, plo
->modulename
);
176 /***************************************************************************
177 * AddLoadOrderSet (internal, static)
179 * Adds an set of entries in the list of overrides from the key parameter.
180 * If the entry exists then the override parameter determines whether it
181 * will be overwriten.
183 static BOOL
AddLoadOrderSet(char *key
, char *order
, BOOL override
)
185 module_loadorder_t ldo
;
188 /* Parse the loadorder before the rest because strtok is not reentrant */
189 if(!ParseLoadOrder(order
, &ldo
))
192 cptr
= get_tok(key
, ", \t");
195 if(strchr(cptr
, '.'))
196 MSG("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr
);
198 ldo
.modulename
= cptr
;
199 if(!AddLoadOrder(&ldo
, override
))
201 cptr
= get_tok(NULL
, ", \t");
207 /***************************************************************************
208 * ParseCommandlineOverrides (internal, static)
210 * The commandline is in the form:
211 * name[,name,...]=native[,b,...][:...]
213 static BOOL
ParseCommandlineOverrides(void)
221 if(!Options
.dllFlags
)
224 cpy
= HEAP_strdupA(SystemHeap
, 0, Options
.dllFlags
);
227 for(; next
; key
= next
)
229 next
= strchr(key
, ':');
235 value
= strchr(key
, '=');
244 TRACE(module
, "Commandline override '%s' = '%s'\n", key
, value
);
246 if(!AddLoadOrderSet(key
, value
, TRUE
))
253 HeapFree(SystemHeap
, 0, cpy
);
258 /***************************************************************************
259 * MODULE_InitLoadOrder (internal)
261 * Initialize the load order from the wine.conf file.
262 * The section has tyhe following format:
267 * EXTRA_LD_LIBRARY_PATH=/usr/local/lib/wine[:/more/path/to/search[:...]]
268 * The path will be appended to any existing LD_LIBRARY_PATH from the
269 * environment (see note in code below).
271 * DefaultLoadOrder=native,elfdll,so,builtin
272 * A comma seperated list of module-types to try to load in that specific
273 * order. The DefaultLoadOrder key is used as a fallback when a module is
274 * not specified explicitely. If the DefaultLoadOrder key is not found,
275 * then the order "dll,elfdll,so,bi" is used
276 * The possible module-types are:
277 * - native Native windows dll files
278 * - elfdll Dlls encapsulated in .so libraries
279 * - so Native .so libraries mapped to dlls
280 * - builtin Built-in modules
282 * Case is not important and only the first letter of each type is enough to
283 * identify the type n[ative], e[lfdll], s[o], b[uiltin]. Also whitespace is
288 * native,elfdll,so,builtin
294 * There are no explicit keys defined other than module/library names. A comma
295 * separated list of modules is followed by an assignment of the load-order
296 * for these specific modules. See above for possible types. You should not
297 * specify an extension.
299 * kernel32, gdi32, user32 = builtin
300 * kernel, gdi, user = builtin
301 * comdlg32 = elfdll, native, builtin
302 * commdlg = native, builtin
303 * version, ver = elfdll, native, builtin
309 * This is a simple pairing in the form 'name1 = name2'. It is supposed to
310 * identify the dlls that cannot live without eachother unless they are
311 * loaded in the same format. Examples are common dialogs and controls,
312 * shell, kernel, gdi, user, etc...
313 * The code will issue a warning if the loadorder of these pairs are different
314 * and might cause hard-to-find bugs due to incompatible pairs loaded at
315 * run-time. Note that this pairing gives *no* guarantee that the pairs
316 * actually get loaded as the same type, nor that the correct versions are
317 * loaded (might be implemented later). It merely notes obvious trouble.
324 #define BUFFERSIZE 1024
326 BOOL
MODULE_InitLoadOrder(void)
328 char buffer
[BUFFERSIZE
];
331 /* Get/set the new LD_LIBRARY_PATH */
332 nbuffer
= PROFILE_GetWineIniString("DllDefaults", "EXTRA_LD_LIBRARY_PATH", "", buffer
, sizeof(buffer
));
336 extern char *_dl_library_path
;
337 char *ld_lib_path
= getenv("LD_LIBRARY_PATH");
341 * Append new path to current
343 char *tmp
= HEAP_strdupA(SystemHeap
, 0, buffer
);
344 sprintf(buffer
, "%s:%s", ld_lib_path
, tmp
);
345 HeapFree( SystemHeap
, 0, tmp
);
348 TRACE(module
, "Setting new LD_LIBRARY_PATH=%s\n", buffer
);
350 setenv("LD_LIBRARY_PATH", buffer
, 1);
353 * This is a cruel hack required to have libdl check this path.
354 * The problem is that libdl caches the environment variable
355 * and we won't get our modifications applied. We ensure the
356 * the correct search path by explicitely modifying the libdl
357 * internal variable which holds the path.
359 _dl_library_path
= HEAP_strdupA(SystemHeap
, 0, buffer
);
362 /* Get the default load order */
363 nbuffer
= PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,e,s,b", buffer
, sizeof(buffer
));
366 MSG("MODULE_InitLoadOrder: misteriously read nothing from default loadorder\n");
370 TRACE(module
, "Setting default loadorder=%s\n", buffer
);
372 if(!ParseLoadOrder(buffer
, &default_loadorder
))
374 default_loadorder
.modulename
= "<none>";
376 /* Read the explicitely defined orders for specific modules as an entire section */
377 nbuffer
= PROFILE_GetWineIniString("DllOverrides", NULL
, "", buffer
, sizeof(buffer
));
378 if(nbuffer
== BUFFERSIZE
-2)
380 ERR(module
, "BUFFERSIZE %d is too small to read [DllOverrides]. Needs to grow in the source\n", BUFFERSIZE
);
385 /* We only have the keys in the buffer, not the values */
387 char value
[BUFFERSIZE
];
390 for(key
= buffer
; *key
; key
= next
)
392 next
= key
+ strlen(key
) + 1;
394 nbuffer
= PROFILE_GetWineIniString("DllOverrides", key
, "", value
, sizeof(value
));
397 ERR(module
, "Module(s) '%s' will always fail to load. Are you sure you want this?\n", key
);
398 value
[0] = '\0'; /* Just in case */
400 if(nbuffer
== BUFFERSIZE
-2)
402 ERR(module
, "BUFFERSIZE %d is too small to read [DllOverrides] key '%s'. Needs to grow in the source\n", BUFFERSIZE
, key
);
406 TRACE(module
, "Key '%s' uses override '%s'\n", key
, value
);
408 if(!AddLoadOrderSet(key
, value
, FALSE
))
413 /* Add the commandline overrides to the pool */
414 if(!ParseCommandlineOverrides())
416 MSG( "Syntax: -dll name[,name[,...]]={native|elfdll|so|builtin}[,{n|e|s|b}[,...]][:...]\n"
417 " - 'name' is the name of any dll without extension\n"
418 " - the order of loading (native, elfdll, so and builtin) can be abbreviated\n"
419 " with the first letter\n"
420 " - different loadorders for different dlls can be specified by seperating the\n"
421 " commandline entries with a ':'\n"
423 " -dll comdlg32,commdlg=n:shell,shell32=b\n"
428 /* Sort the array for quick lookup */
429 qsort(module_loadorder
, nmodule_loadorder
, sizeof(module_loadorder
[0]), cmp_sort_func
);
431 /* Check the pairs of dlls */
432 nbuffer
= PROFILE_GetWineIniString("DllPairs", NULL
, "", buffer
, sizeof(buffer
));
433 if(nbuffer
== BUFFERSIZE
-2)
435 ERR(module
, "BUFFERSIZE %d is too small to read [DllPairs]. Needs to grow in the source\n", BUFFERSIZE
);
440 /* We only have the keys in the buffer, not the values */
442 char value
[BUFFERSIZE
];
445 for(key
= buffer
; *key
; key
= next
)
447 module_loadorder_t
*plo1
, *plo2
;
449 next
= key
+ strlen(key
) + 1;
451 nbuffer
= PROFILE_GetWineIniString("DllPairs", key
, "", value
, sizeof(value
));
454 ERR(module
, "Module pair '%s' is not associated with another module?\n", key
);
457 if(nbuffer
== BUFFERSIZE
-2)
459 ERR(module
, "BUFFERSIZE %d is too small to read [DllPairs] key '%s'. Needs to grow in the source\n", BUFFERSIZE
, key
);
463 plo1
= MODULE_GetLoadOrder(key
);
464 plo2
= MODULE_GetLoadOrder(value
);
465 assert(plo1
&& plo2
);
467 if(memcmp(plo1
->loadorder
, plo2
->loadorder
, sizeof(plo1
->loadorder
)))
468 MSG("Warning: Modules '%s' and '%s' have different loadorder which may cause trouble\n", key
, value
);
475 static char types
[6] = "-NESB";
477 for(i
= 0; i
< nmodule_loadorder
; i
++)
479 DPRINTF("%3d: %-12s:", i
, module_loadorder
[i
].modulename
);
480 for(j
= 0; j
< MODULE_LOADORDER_NTYPES
; j
++)
481 DPRINTF(" %c", types
[module_loadorder
[i
].loadorder
[j
] % (MODULE_LOADORDER_NTYPES
+1)]);
490 /***************************************************************************
491 * MODULE_GetLoadOrder (internal)
493 * Locate the loadorder of a module.
494 * Any path is stripped from the path-argument and so are the extension
495 * '.dll' and '.exe'. A lookup in the table can yield an override for the
496 * specific dll. Otherwise the default load order is returned.
498 module_loadorder_t
*MODULE_GetLoadOrder(const char *path
)
500 module_loadorder_t lo
, *tmp
;
506 assert(path
!= NULL
);
508 /* Strip path information */
509 cptr
= strrchr(path
, '\\');
511 name
= strrchr(path
, '/');
513 name
= strrchr(cptr
, '/');
516 name
= cptr
? cptr
+1 : (char *)path
;
520 if((cptr
= strchr(name
, ':')) != NULL
) /* Also strip drive if in format 'C:MODULE.DLL' */
524 if(len
>= sizeof(fname
) || len
<= 0)
526 ERR(module
, "Path '%s' -> '%s' reduces to zilch or just too large...\n", path
, name
);
527 return &default_loadorder
;
531 if(len
>= 4 && (!lstrcmpiA(fname
+len
-4, ".dll") || !lstrcmpiA(fname
+len
-4, ".exe")))
534 lo
.modulename
= fname
;
535 tmp
= bsearch(&lo
, module_loadorder
, nmodule_loadorder
, sizeof(module_loadorder
[0]), cmp_sort_func
);
537 TRACE(module
, "Looking for '%s' (%s), found '%s'\n", path
, fname
, tmp
? tmp
->modulename
: "<nothing>");
540 return &default_loadorder
;