Fixed memory freeing.
[wine/wine-kai.git] / loader / loadorder.c
blob31c55ab34cd5ceb1b45ddcd6be2c5d5a6b845f75
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"
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
32 * entries.
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;
49 char *cptr;
51 if(!str && (!buf || !index))
52 return NULL;
54 if(str && buf)
56 HeapFree(SystemHeap, 0, buf);
57 buf = NULL;
60 if(str && !buf)
62 buf = HEAP_strdupA(SystemHeap, 0, str);
63 cptr = strtok(buf, delim);
65 else
67 cptr = strtok(NULL, delim);
70 if(!cptr)
72 HeapFree(SystemHeap, 0, buf);
73 buf = NULL;
75 return cptr;
79 /***************************************************************************
80 * ParseLoadOrder (internal, static)
82 * Parses the loadorder options from the configuration and puts it into
83 * a structure.
85 static BOOL ParseLoadOrder(char *order, module_loadorder_t *mlo)
87 char *cptr;
88 int n = 0;
90 memset(mlo->loadorder, 0, sizeof(mlo->loadorder));
92 cptr = get_tok(order, ", \t");
93 while(cptr)
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);
100 break;
103 switch(*cptr)
105 case 'N': /* Native */
106 case 'n': type = MODULE_LOADORDER_DLL; break;
108 case 'E': /* Elfdll */
109 case 'e': type = MODULE_LOADORDER_ELFDLL; break;
111 case 'S': /* So */
112 case 's': type = MODULE_LOADORDER_SO; break;
114 case 'B': /* Builtin */
115 case 'b': type = MODULE_LOADORDER_BI; break;
117 default:
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");
127 return TRUE;
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)
139 int i;
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]))
147 if(!override)
148 ERR(module, "Module '%s' is already in the list of overrides, using first definition\n", plo->modulename);
149 else
150 memcpy(module_loadorder[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
151 return TRUE;
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,
161 module_loadorder,
162 nmodule_loadorder_alloc * sizeof(module_loadorder_t));
163 if(!module_loadorder)
165 MSG("Virtual memory exhausted\n");
166 exit(1);
169 memcpy(module_loadorder[nmodule_loadorder].loadorder, plo->loadorder, sizeof(plo->loadorder));
170 module_loadorder[nmodule_loadorder].modulename = HEAP_strdupA(SystemHeap, 0, plo->modulename);
171 nmodule_loadorder++;
172 return TRUE;
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;
186 char *cptr;
188 /* Parse the loadorder before the rest because strtok is not reentrant */
189 if(!ParseLoadOrder(order, &ldo))
190 return FALSE;
192 cptr = get_tok(key, ", \t");
193 while(cptr)
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))
200 return FALSE;
201 cptr = get_tok(NULL, ", \t");
203 return TRUE;
207 /***************************************************************************
208 * ParseCommandlineOverrides (internal, static)
210 * The commandline is in the form:
211 * name[,name,...]=native[,b,...][:...]
213 static BOOL ParseCommandlineOverrides(void)
215 char *cpy;
216 char *key;
217 char *next;
218 char *value;
219 BOOL retval = TRUE;
221 if(!Options.dllFlags)
222 return TRUE;
224 cpy = HEAP_strdupA(SystemHeap, 0, Options.dllFlags);
225 key = cpy;
226 next = key;
227 for(; next; key = next)
229 next = strchr(key, ':');
230 if(next)
232 *next = '\0';
233 next++;
235 value = strchr(key, '=');
236 if(!value)
238 retval = FALSE;
239 goto endit;
241 *value = '\0';
242 value++;
244 TRACE(module, "Commandline override '%s' = '%s'\n", key, value);
246 if(!AddLoadOrderSet(key, value, TRUE))
248 retval = FALSE;
249 goto endit;
252 endit:
253 HeapFree(SystemHeap, 0, cpy);
254 return retval;;
258 /***************************************************************************
259 * MODULE_InitLoadOrder (internal)
261 * Initialize the load order from the wine.conf file.
262 * The section has tyhe following format:
263 * Section:
264 * [DllDefaults]
266 * Keys:
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
284 * ignored.
285 * E.g.:
286 * n,el ,s , b
287 * is equal to:
288 * native,elfdll,so,builtin
290 * Section:
291 * [DllOverrides]
293 * Keys:
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.
298 * Examples:
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
305 * Section:
306 * [DllPairs]
308 * Keys:
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.
318 * Examples:
319 * kernel = kernel32
320 * commdlg = comdlg32
324 #define BUFFERSIZE 1024
326 BOOL MODULE_InitLoadOrder(void)
328 char buffer[BUFFERSIZE];
329 int nbuffer;
331 /* Get/set the new LD_LIBRARY_PATH */
332 nbuffer = PROFILE_GetWineIniString("DllDefaults", "EXTRA_LD_LIBRARY_PATH", "", buffer, sizeof(buffer));
334 if(nbuffer)
336 extern char *_dl_library_path;
337 char *ld_lib_path = getenv("LD_LIBRARY_PATH");
338 if(ld_lib_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));
364 if(!nbuffer)
366 MSG("MODULE_InitLoadOrder: misteriously read nothing from default loadorder\n");
367 return FALSE;
370 TRACE(module, "Setting default loadorder=%s\n", buffer);
372 if(!ParseLoadOrder(buffer, &default_loadorder))
373 return FALSE;
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);
381 return FALSE;
383 if(nbuffer)
385 /* We only have the keys in the buffer, not the values */
386 char *key;
387 char value[BUFFERSIZE];
388 char *next;
390 for(key = buffer; *key; key = next)
392 next = key + strlen(key) + 1;
394 nbuffer = PROFILE_GetWineIniString("DllOverrides", key, "", value, sizeof(value));
395 if(!nbuffer)
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);
403 return FALSE;
406 TRACE(module, "Key '%s' uses override '%s'\n", key, value);
408 if(!AddLoadOrderSet(key, value, FALSE))
409 return 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"
422 " Example:\n"
423 " -dll comdlg32,commdlg=n:shell,shell32=b\n"
425 return FALSE;
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);
436 return FALSE;
438 if(nbuffer)
440 /* We only have the keys in the buffer, not the values */
441 char *key;
442 char value[BUFFERSIZE];
443 char *next;
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));
452 if(!nbuffer)
454 ERR(module, "Module pair '%s' is not associated with another module?\n", key);
455 continue;
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);
460 return FALSE;
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);
472 if(TRACE_ON(module))
474 int i, j;
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)]);
482 DPRINTF("\n");
486 return TRUE;
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;
501 char fname[256];
502 char *cptr;
503 char *name;
504 int len;
506 assert(path != NULL);
508 /* Strip path information */
509 cptr = strrchr(path, '\\');
510 if(!cptr)
511 name = strrchr(path, '/');
512 else
513 name = strrchr(cptr, '/');
515 if(!name)
516 name = cptr ? cptr+1 : (char *)path;
517 else
518 name++;
520 if((cptr = strchr(name, ':')) != NULL) /* Also strip drive if in format 'C:MODULE.DLL' */
521 name = cptr+1;
523 len = strlen(name);
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;
530 strcpy(fname, name);
531 if(len >= 4 && (!lstrcmpiA(fname+len-4, ".dll") || !lstrcmpiA(fname+len-4, ".exe")))
532 fname[len-4] = '\0';
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>");
539 if(!tmp)
540 return &default_loadorder;
541 return tmp;