Use the exe name and file handle we got from the server also when
[wine/dcerpc.git] / loader / loadorder.c
blob5d5e0fcecbdbe5b1ba21d3005e9024a0f22db6b6
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 "config.h"
12 #include "windef.h"
13 #include "options.h"
14 #include "loadorder.h"
15 #include "heap.h"
16 #include "file.h"
17 #include "module.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 {
34 char *key,*value;
35 } DefaultDllOverrides[] = {
36 /* "system" DLLs */
37 {"kernel32,gdi32,user32", "builtin"},
38 {"krnl386,gdi,user", "builtin"},
39 {"toolhelp", "builtin"},
40 {"windebug", "native,builtin"},
41 {"system,display", "builtin"},
42 {"w32skrnl,wow32", "builtin"},
43 {"advapi32,crtdll,ntdll", "builtin,native"},
44 {"lz32,lzexpand", "builtin,native"},
45 {"version,ver", "builtin,native"},
46 /* "new" interface */
47 {"comdlg32,commdlg", "builtin,native"},
48 {"shell32,shell", "builtin,native"},
49 {"shlwapi", "native,builtin"},
50 {"shfolder", "builtin,native"},
51 {"comctl32,commctrl", "builtin,native"},
52 /* network */
53 {"wsock32,ws2_32,winsock", "builtin"},
54 {"icmp", "builtin"},
55 /* multimedia */
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 {"msacm,msacm32", "builtin,native"},
64 {"opengl32", "builtin,native"},
65 /* we have to use libglideXx.so instead of glideXx.dll ... */
66 {"glide2x,glide3x", "so,native"},
67 /* other stuff */
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"},
74 {NULL,NULL},
77 static const struct tagDllPair {
78 const char *dll1, *dll2;
79 } DllPairs[] = {
80 { "krnl386", "kernel32" },
81 { "gdi", "gdi32" },
82 { "user", "user32" },
83 { "commdlg", "comdlg32" },
84 { "commctrl", "comctl32" },
85 { "ver", "version" },
86 { "shell", "shell32" },
87 { "lzexpand", "lz32" },
88 { "mmsystem", "winmm" },
89 { "msvideo", "msvfw32" },
90 { "msacm", "msacm32" },
91 { "winsock", "wsock32" },
92 { NULL, NULL }
95 /***************************************************************************
96 * cmp_sort_func (internal, static)
98 * Sorting and comparing function used in sort and search of loadorder
99 * entries.
101 static int cmp_sort_func(const void *s1, const void *s2)
103 return FILE_strcasecmp(((module_loadorder_t *)s1)->modulename,
104 ((module_loadorder_t *)s2)->modulename);
108 /***************************************************************************
109 * get_tok (internal, static)
111 * strtok wrapper for non-destructive buffer writing.
112 * NOTE: strtok is not reentrant and therefore this code is neither.
114 static char *get_tok(const char *str, const char *delim)
116 static char *buf = NULL;
117 char *cptr;
119 if(!str && !buf)
120 return NULL;
122 if(str && buf)
124 HeapFree(GetProcessHeap(), 0, buf);
125 buf = NULL;
128 if(str && !buf)
130 buf = HEAP_strdupA(GetProcessHeap(), 0, str);
131 cptr = strtok(buf, delim);
133 else
135 cptr = strtok(NULL, delim);
138 if(!cptr)
140 HeapFree(GetProcessHeap(), 0, buf);
141 buf = NULL;
143 return cptr;
147 /***************************************************************************
148 * ParseLoadOrder (internal, static)
150 * Parses the loadorder options from the configuration and puts it into
151 * a structure.
153 static BOOL ParseLoadOrder(char *order, module_loadorder_t *mlo)
155 static int warn;
156 char *cptr;
157 int n = 0;
159 memset(mlo->loadorder, 0, sizeof(mlo->loadorder));
161 cptr = get_tok(order, ", \t");
162 while(cptr)
164 char type = MODULE_LOADORDER_INVALID;
166 if(n >= MODULE_LOADORDER_NTYPES)
168 ERR("More than existing %d module-types specified, rest ignored", MODULE_LOADORDER_NTYPES);
169 break;
172 switch(*cptr)
174 case 'N': /* Native */
175 case 'n': type = MODULE_LOADORDER_DLL; break;
177 case 'E': /* Elfdll */
178 case 'e':
179 if (!warn++) MESSAGE("Load order 'elfdll' no longer supported, ignored\n");
180 break;
181 case 'S': /* So */
182 case 's': type = MODULE_LOADORDER_SO; break;
184 case 'B': /* Builtin */
185 case 'b': type = MODULE_LOADORDER_BI; break;
187 default:
188 ERR("Invalid load order module-type '%s', ignored\n", cptr);
191 if(type != MODULE_LOADORDER_INVALID)
193 mlo->loadorder[n++] = type;
195 cptr = get_tok(NULL, ", \t");
197 return TRUE;
201 /***************************************************************************
202 * AddLoadOrder (internal, static)
204 * Adds an entry in the list of overrides. If the entry exists, then the
205 * override parameter determines whether it will be overwritten.
207 static BOOL AddLoadOrder(module_loadorder_t *plo, BOOL override)
209 int i;
211 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
213 for(i = 0; i < nmodule_loadorder; i++)
215 if(!cmp_sort_func(plo, &module_loadorder[i]))
217 if(!override)
218 ERR("Module '%s' is already in the list of overrides, using first definition\n", plo->modulename);
219 else
220 memcpy(module_loadorder[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
221 return TRUE;
225 if(nmodule_loadorder >= nmodule_loadorder_alloc)
227 /* No space in current array, make it larger */
228 nmodule_loadorder_alloc += LOADORDER_ALLOC_CLUSTER;
229 module_loadorder = (module_loadorder_t *)HeapReAlloc(GetProcessHeap(),
231 module_loadorder,
232 nmodule_loadorder_alloc * sizeof(module_loadorder_t));
233 if(!module_loadorder)
235 MESSAGE("Virtual memory exhausted\n");
236 exit(1);
239 memcpy(module_loadorder[nmodule_loadorder].loadorder, plo->loadorder, sizeof(plo->loadorder));
240 module_loadorder[nmodule_loadorder].modulename = HEAP_strdupA(GetProcessHeap(), 0, plo->modulename);
241 nmodule_loadorder++;
242 return TRUE;
246 /***************************************************************************
247 * AddLoadOrderSet (internal, static)
249 * Adds a set of entries in the list of overrides from the key parameter.
250 * If the entry exists, then the override parameter determines whether it
251 * will be overwritten.
253 static BOOL AddLoadOrderSet(char *key, char *order, BOOL override)
255 module_loadorder_t ldo;
256 char *cptr;
258 /* Parse the loadorder before the rest because strtok is not reentrant */
259 if(!ParseLoadOrder(order, &ldo))
260 return FALSE;
262 cptr = get_tok(key, ", \t");
263 while(cptr)
265 char *ext = strrchr(cptr, '.');
266 if(ext)
268 if(strlen(ext) == 4 &&
269 (!FILE_strcasecmp(ext, ".dll") || !FILE_strcasecmp(ext, ".exe")))
270 MESSAGE("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr);
273 ldo.modulename = cptr;
274 if(!AddLoadOrder(&ldo, override))
275 return FALSE;
276 cptr = get_tok(NULL, ", \t");
278 return TRUE;
282 /***************************************************************************
283 * ParseCommandlineOverrides (internal, static)
285 * The commandline is in the form:
286 * name[,name,...]=native[,b,...][+...]
288 static BOOL ParseCommandlineOverrides(void)
290 char *cpy;
291 char *key;
292 char *next;
293 char *value;
294 BOOL retval = TRUE;
296 if(!Options.dllFlags)
297 return TRUE;
299 cpy = HEAP_strdupA(GetProcessHeap(), 0, Options.dllFlags);
300 key = cpy;
301 next = key;
302 for(; next; key = next)
304 next = strchr(key, '+');
305 if(next)
307 *next = '\0';
308 next++;
310 value = strchr(key, '=');
311 if(!value)
313 retval = FALSE;
314 goto endit;
316 *value = '\0';
317 value++;
319 TRACE("Commandline override '%s' = '%s'\n", key, value);
321 if(!AddLoadOrderSet(key, value, TRUE))
323 retval = FALSE;
324 goto endit;
327 endit:
328 HeapFree(GetProcessHeap(), 0, cpy);
329 return retval;;
333 /***************************************************************************
334 * MODULE_InitLoadOrder (internal)
336 * Initialize the load order from the wine.conf file.
337 * The section has the following format:
338 * Section:
339 * [DllDefaults]
341 * Keys:
342 * DefaultLoadOrder=native,so,builtin
343 * A comma separated list of module types to try to load in that specific
344 * order. The DefaultLoadOrder key is used as a fallback when a module is
345 * not specified explicitly. If the DefaultLoadOrder key is not found,
346 * then the order "dll,so,bi" is used
347 * The possible module types are:
348 * - native Native windows dll files
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], s[o], b[uiltin]. Also whitespace is
354 * ignored.
355 * E.g.:
356 * n,s , b
357 * is equal to:
358 * native,so,builtin
360 * Section:
361 * [DllOverrides]
363 * Keys:
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.
368 * Examples:
369 * kernel32, gdi32, user32 = builtin
370 * kernel, gdi, user = builtin
371 * comdlg32 = native, builtin
372 * commdlg = native, builtin
373 * version, ver = native, builtin
377 #define BUFFERSIZE 1024
379 BOOL MODULE_InitLoadOrder(void)
381 char buffer[BUFFERSIZE];
382 char key[256];
383 int nbuffer;
384 int idx;
385 const struct tagDllPair *dllpair;
387 /* Get the default load order */
388 nbuffer = PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,b,s", buffer, sizeof(buffer));
389 if(!nbuffer)
391 MESSAGE("MODULE_InitLoadOrder: mysteriously read nothing from default loadorder\n");
392 return FALSE;
395 TRACE("Setting default loadorder=%s\n", buffer);
397 if(!ParseLoadOrder(buffer, &default_loadorder))
398 return FALSE;
399 default_loadorder.modulename = "<none>";
402 int i;
403 for (i=0;DefaultDllOverrides[i].key;i++)
404 AddLoadOrderSet(
405 DefaultDllOverrides[i].key,
406 DefaultDllOverrides[i].value,
407 FALSE
411 /* Read the explicitely defined orders for specific modules as an entire section */
412 idx = 0;
413 while (PROFILE_EnumWineIniString( "DllOverrides", idx++, key, sizeof(key),
414 buffer, sizeof(buffer)))
416 TRACE("Key '%s' uses override '%s'\n", key, buffer);
417 if(!AddLoadOrderSet(key, buffer, TRUE))
418 return FALSE;
421 /* Add the commandline overrides to the pool */
422 if(!ParseCommandlineOverrides())
424 MESSAGE( "Syntax: -dll name[,name[,...]]={native|so|builtin}[,{n|s|b}[,...]][+...]\n"
425 " - 'name' is the name of any dll without extension\n"
426 " - the order of loading (native, so and builtin) can be abbreviated\n"
427 " with the first letter\n"
428 " - different loadorders for different dlls can be specified by seperating the\n"
429 " commandline entries with a '+'\n"
430 " Example:\n"
431 " -dll comdlg32,commdlg=n+shell,shell32=b\n"
433 return FALSE;
436 /* Sort the array for quick lookup */
437 qsort(module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
439 /* Check the pairs of dlls */
440 dllpair = DllPairs;
441 while (dllpair->dll1)
443 module_loadorder_t *plo1, *plo2;
444 plo1 = MODULE_GetLoadOrder(dllpair->dll1, FALSE);
445 plo2 = MODULE_GetLoadOrder(dllpair->dll2, FALSE);
446 assert(plo1 && plo2);
447 if(memcmp(plo1->loadorder, plo2->loadorder, sizeof(plo1->loadorder)))
448 MESSAGE("Warning: Modules '%s' and '%s' have different loadorder which may cause trouble\n", dllpair->dll1, dllpair->dll2);
449 dllpair++;
452 if(TRACE_ON(module))
454 int i, j;
455 static char types[] = "-NSB";
457 for(i = 0; i < nmodule_loadorder; i++)
459 DPRINTF("%3d: %-12s:", i, module_loadorder[i].modulename);
460 for(j = 0; j < MODULE_LOADORDER_NTYPES; j++)
461 DPRINTF(" %c", types[module_loadorder[i].loadorder[j] % (MODULE_LOADORDER_NTYPES+1)]);
462 DPRINTF("\n");
466 return TRUE;
470 /***************************************************************************
471 * MODULE_GetLoadOrder (internal)
473 * Locate the loadorder of a module.
474 * Any path is stripped from the path-argument and so are the extension
475 * '.dll' and '.exe'. A lookup in the table can yield an override for
476 * the specific dll. Otherwise the default load order is returned.
478 module_loadorder_t *MODULE_GetLoadOrder(const char *path, BOOL win32 )
480 module_loadorder_t lo, *tmp;
481 char fname[256];
482 char sysdir[MAX_PATH+1];
483 char *cptr;
484 char *name;
485 int len;
487 TRACE("looking for %s\n", path);
489 assert(path != NULL);
491 if ( ! GetSystemDirectoryA ( sysdir, MAX_PATH ) )
492 return &default_loadorder; /* Hmmm ... */
494 /* Strip path information for 16 bit modules or if the module
495 resides in the system directory */
496 if ( !win32 || !FILE_strncasecmp ( sysdir, path, strlen (sysdir) ) )
499 cptr = strrchr(path, '\\');
500 if(!cptr)
501 name = strrchr(path, '/');
502 else
503 name = strrchr(cptr, '/');
505 if(!name)
506 name = cptr ? cptr+1 : (char *)path;
507 else
508 name++;
510 if((cptr = strchr(name, ':')) != NULL) /* Also strip drive if in format 'C:MODULE.DLL' */
511 name = cptr+1;
513 else
514 name = (char *)path;
516 len = strlen(name);
517 if(len >= sizeof(fname) || len <= 0)
519 ERR("Path '%s' -> '%s' reduces to zilch or just too large...\n", path, name);
520 return &default_loadorder;
523 strcpy(fname, name);
524 if(len >= 4 && (!FILE_strcasecmp(fname+len-4, ".dll") || !FILE_strcasecmp(fname+len-4, ".exe")))
525 fname[len-4] = '\0';
527 lo.modulename = fname;
528 tmp = bsearch(&lo, module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
530 TRACE("Looking for '%s' (%s), found '%s'\n", path, fname, tmp ? tmp->modulename : "<nothing>");
532 if(!tmp)
533 return &default_loadorder;
534 return tmp;