Test for static was reversed.
[wine/wine-kai.git] / loader / loadorder.c
blob365befc69e3e370f7e0242069044c75fdaafd9d6
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 "windef.h"
12 #include "options.h"
13 #include "debug.h"
14 #include "loadorder.h"
15 #include "heap.h"
16 #include "options.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
34 * entries.
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;
51 char *cptr;
53 if(!str && (!buf || !index))
54 return NULL;
56 if(str && buf)
58 HeapFree(SystemHeap, 0, buf);
59 buf = NULL;
62 if(str && !buf)
64 buf = HEAP_strdupA(SystemHeap, 0, str);
65 cptr = strtok(buf, delim);
67 else
69 cptr = strtok(NULL, delim);
72 if(!cptr)
74 HeapFree(SystemHeap, 0, buf);
75 buf = NULL;
77 return cptr;
81 /***************************************************************************
82 * ParseLoadOrder (internal, static)
84 * Parses the loadorder options from the configuration and puts it into
85 * a structure.
87 static BOOL ParseLoadOrder(char *order, module_loadorder_t *mlo)
89 char *cptr;
90 int n = 0;
92 memset(mlo->loadorder, 0, sizeof(mlo->loadorder));
94 cptr = get_tok(order, ", \t");
95 while(cptr)
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);
102 break;
105 switch(*cptr)
107 case 'N': /* Native */
108 case 'n': type = MODULE_LOADORDER_DLL; break;
110 case 'E': /* Elfdll */
111 case 'e': type = MODULE_LOADORDER_ELFDLL; break;
113 case 'S': /* So */
114 case 's': type = MODULE_LOADORDER_SO; break;
116 case 'B': /* Builtin */
117 case 'b': type = MODULE_LOADORDER_BI; break;
119 default:
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");
129 return TRUE;
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)
141 int i;
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]))
149 if(!override)
150 ERR(module, "Module '%s' is already in the list of overrides, using first definition\n", plo->modulename);
151 else
152 memcpy(module_loadorder[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
153 return TRUE;
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,
163 module_loadorder,
164 nmodule_loadorder_alloc * sizeof(module_loadorder_t));
165 if(!module_loadorder)
167 MSG("Virtual memory exhausted\n");
168 exit(1);
171 memcpy(module_loadorder[nmodule_loadorder].loadorder, plo->loadorder, sizeof(plo->loadorder));
172 module_loadorder[nmodule_loadorder].modulename = HEAP_strdupA(SystemHeap, 0, plo->modulename);
173 nmodule_loadorder++;
174 return TRUE;
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;
188 char *cptr;
190 /* Parse the loadorder before the rest because strtok is not reentrant */
191 if(!ParseLoadOrder(order, &ldo))
192 return FALSE;
194 cptr = get_tok(key, ", \t");
195 while(cptr)
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))
202 return FALSE;
203 cptr = get_tok(NULL, ", \t");
205 return TRUE;
209 /***************************************************************************
210 * ParseCommandlineOverrides (internal, static)
212 * The commandline is in the form:
213 * name[,name,...]=native[,b,...][:...]
215 static BOOL ParseCommandlineOverrides(void)
217 char *cpy;
218 char *key;
219 char *next;
220 char *value;
221 BOOL retval = TRUE;
223 if(!Options.dllFlags)
224 return TRUE;
226 cpy = HEAP_strdupA(SystemHeap, 0, Options.dllFlags);
227 key = cpy;
228 next = key;
229 for(; next; key = next)
231 next = strchr(key, ':');
232 if(next)
234 *next = '\0';
235 next++;
237 value = strchr(key, '=');
238 if(!value)
240 retval = FALSE;
241 goto endit;
243 *value = '\0';
244 value++;
246 TRACE(module, "Commandline override '%s' = '%s'\n", key, value);
248 if(!AddLoadOrderSet(key, value, TRUE))
250 retval = FALSE;
251 goto endit;
254 endit:
255 HeapFree(SystemHeap, 0, cpy);
256 return retval;;
260 /***************************************************************************
261 * MODULE_InitLoadOrder (internal)
263 * Initialize the load order from the wine.conf file.
264 * The section has tyhe following format:
265 * Section:
266 * [DllDefaults]
268 * Keys:
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
286 * ignored.
287 * E.g.:
288 * n,el ,s , b
289 * is equal to:
290 * native,elfdll,so,builtin
292 * Section:
293 * [DllOverrides]
295 * Keys:
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.
300 * Examples:
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
307 * Section:
308 * [DllPairs]
310 * Keys:
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.
320 * Examples:
321 * kernel = kernel32
322 * commdlg = comdlg32
326 #define BUFFERSIZE 1024
328 BOOL MODULE_InitLoadOrder(void)
330 char buffer[BUFFERSIZE];
331 int nbuffer;
333 /* Get/set the new LD_LIBRARY_PATH */
334 nbuffer = PROFILE_GetWineIniString("DllDefaults", "EXTRA_LD_LIBRARY_PATH", "", buffer, sizeof(buffer));
336 if(nbuffer)
338 extern char *_dl_library_path;
339 char *ld_lib_path = getenv("LD_LIBRARY_PATH");
340 if(ld_lib_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));
366 if(!nbuffer)
368 MSG("MODULE_InitLoadOrder: misteriously read nothing from default loadorder\n");
369 return FALSE;
372 TRACE(module, "Setting default loadorder=%s\n", buffer);
374 if(!ParseLoadOrder(buffer, &default_loadorder))
375 return FALSE;
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);
383 return FALSE;
385 if(nbuffer)
387 /* We only have the keys in the buffer, not the values */
388 char *key;
389 char value[BUFFERSIZE];
390 char *next;
392 for(key = buffer; *key; key = next)
394 next = key + strlen(key) + 1;
396 nbuffer = PROFILE_GetWineIniString("DllOverrides", key, "", value, sizeof(value));
397 if(!nbuffer)
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);
405 return FALSE;
408 TRACE(module, "Key '%s' uses override '%s'\n", key, value);
410 if(!AddLoadOrderSet(key, value, FALSE))
411 return 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"
424 " Example:\n"
425 " -dll comdlg32,commdlg=n:shell,shell32=b\n"
427 return FALSE;
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);
438 return FALSE;
440 if(nbuffer)
442 /* We only have the keys in the buffer, not the values */
443 char *key;
444 char value[BUFFERSIZE];
445 char *next;
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));
454 if(!nbuffer)
456 ERR(module, "Module pair '%s' is not associated with another module?\n", key);
457 continue;
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);
462 return FALSE;
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);
474 if(TRACE_ON(module))
476 int i, j;
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)]);
484 DPRINTF("\n");
488 return TRUE;
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;
503 char fname[256];
504 char *cptr;
505 char *name;
506 int len;
508 assert(path != NULL);
510 /* Strip path information */
511 cptr = strrchr(path, '\\');
512 if(!cptr)
513 name = strrchr(path, '/');
514 else
515 name = strrchr(cptr, '/');
517 if(!name)
518 name = cptr ? cptr+1 : (char *)path;
519 else
520 name++;
522 if((cptr = strchr(name, ':')) != NULL) /* Also strip drive if in format 'C:MODULE.DLL' */
523 name = cptr+1;
525 len = strlen(name);
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;
532 strcpy(fname, name);
533 if(len >= 4 && (!lstrcmpiA(fname+len-4, ".dll") || !lstrcmpiA(fname+len-4, ".exe")))
534 fname[len-4] = '\0';
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>");
541 if(!tmp)
542 return &default_loadorder;
543 return tmp;