2 * Module/Library loadorder
4 * Copyright 1999 Bertho Stultiens
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(module);
23 #define LOADORDER_ALLOC_CLUSTER 32 /* Allocate with 32 entries at a time */
25 typedef struct module_loadorder
27 const char *modulename;
28 enum loadorder_type loadorder[LOADORDER_NTYPES];
35 module_loadorder_t *order;
38 /* default load-order if nothing specified */
39 /* the list must remain sorted by dll name */
40 static module_loadorder_t default_order_list[] =
42 { "display", { LOADORDER_BI, 0, 0, 0 } },
43 { "gdi", { LOADORDER_BI, 0, 0, 0 } },
44 { "gdi32", { LOADORDER_BI, 0, 0, 0 } },
45 { "glide2x", { LOADORDER_SO, LOADORDER_DLL, 0, 0 } },
46 { "glide3x", { LOADORDER_SO, LOADORDER_DLL, 0, 0 } },
47 { "icmp", { LOADORDER_BI, 0, 0, 0 } },
48 { "kernel", { LOADORDER_BI, 0, 0, 0 } },
49 { "kernel32", { LOADORDER_BI, 0, 0, 0 } },
50 { "keyboard", { LOADORDER_BI, 0, 0, 0 } },
51 { "krnl386", { LOADORDER_BI, 0, 0, 0 } },
52 { "mmsystem", { LOADORDER_BI, 0, 0, 0 } },
53 { "mouse", { LOADORDER_BI, 0, 0, 0 } },
54 { "ntdll", { LOADORDER_BI, 0, 0, 0 } },
55 { "odbc32", { LOADORDER_BI, 0, 0, 0 } },
56 { "system", { LOADORDER_BI, 0, 0, 0 } },
57 { "toolhelp", { LOADORDER_BI, 0, 0, 0 } },
58 { "ttydrv", { LOADORDER_BI, 0, 0, 0 } },
59 { "user", { LOADORDER_BI, 0, 0, 0 } },
60 { "user32", { LOADORDER_BI, 0, 0, 0 } },
61 { "w32skrnl", { LOADORDER_BI, 0, 0, 0 } },
62 { "winaspi", { LOADORDER_BI, 0, 0, 0 } },
63 { "windebug", { LOADORDER_DLL, LOADORDER_BI, 0, 0 } },
64 { "winedos", { LOADORDER_BI, 0, 0, 0 } },
65 { "wineps", { LOADORDER_BI, 0, 0, 0 } },
66 { "wing", { LOADORDER_BI, 0, 0, 0 } },
67 { "winmm", { LOADORDER_BI, 0, 0, 0 } },
68 { "winsock", { LOADORDER_BI, 0, 0, 0 } },
69 { "wnaspi32", { LOADORDER_BI, 0, 0, 0 } },
70 { "wow32", { LOADORDER_BI, 0, 0, 0 } },
71 { "wprocs", { LOADORDER_BI, 0, 0, 0 } },
72 { "ws2_32", { LOADORDER_BI, 0, 0, 0 } },
73 { "wsock32", { LOADORDER_BI, 0, 0, 0 } },
74 { "x11drv", { LOADORDER_BI, 0, 0, 0 } }
77 static const struct loadorder_list default_list =
79 sizeof(default_order_list)/sizeof(default_order_list[0]),
80 sizeof(default_order_list)/sizeof(default_order_list[0]),
84 static struct loadorder_list cmdline_list;
87 /***************************************************************************
88 * cmp_sort_func (internal, static)
90 * Sorting and comparing function used in sort and search of loadorder
93 static int cmp_sort_func(const void *s1, const void *s2)
95 return FILE_strcasecmp(((module_loadorder_t *)s1)->modulename,
96 ((module_loadorder_t *)s2)->modulename);
100 /***************************************************************************
101 * get_tok (internal, static)
103 * strtok wrapper for non-destructive buffer writing.
104 * NOTE: strtok is not reentrant and therefore this code is neither.
106 static char *get_tok(const char *str, const char *delim)
108 static char *buf = NULL;
116 HeapFree(GetProcessHeap(), 0, buf);
122 buf = HeapAlloc(GetProcessHeap(), 0, strlen(str)+1);
124 cptr = strtok(buf, delim);
128 cptr = strtok(NULL, delim);
133 HeapFree(GetProcessHeap(), 0, buf);
140 /***************************************************************************
141 * ParseLoadOrder (internal, static)
143 * Parses the loadorder options from the configuration and puts it into
146 static BOOL ParseLoadOrder(char *order, enum loadorder_type lo[])
152 cptr = get_tok(order, ", \t");
155 enum loadorder_type type = LOADORDER_INVALID;
157 if(n >= LOADORDER_NTYPES-1)
159 ERR("More than existing %d module-types specified, rest ignored\n", LOADORDER_NTYPES-1);
165 case 'N': /* Native */
166 case 'n': type = LOADORDER_DLL; break;
168 case 'E': /* Elfdll */
170 if (!warn++) MESSAGE("Load order 'elfdll' no longer supported, ignored\n");
173 case 's': type = LOADORDER_SO; break;
175 case 'B': /* Builtin */
176 case 'b': type = LOADORDER_BI; break;
179 ERR("Invalid load order module-type '%s', ignored\n", cptr);
182 if(type != LOADORDER_INVALID) lo[n++] = type;
183 cptr = get_tok(NULL, ", \t");
185 lo[n] = LOADORDER_INVALID;
190 /***************************************************************************
191 * AddLoadOrder (internal, static)
193 * Adds an entry in the list of command-line overrides.
195 static BOOL AddLoadOrder(module_loadorder_t *plo)
199 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
201 for(i = 0; i < cmdline_list.count; i++)
203 if(!cmp_sort_func(plo, &cmdline_list.order[i] ))
205 /* replace existing option */
206 memcpy( cmdline_list.order[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
211 if (i >= cmdline_list.alloc)
213 /* No space in current array, make it larger */
214 cmdline_list.alloc += LOADORDER_ALLOC_CLUSTER;
215 cmdline_list.order = HeapReAlloc(GetProcessHeap(), 0, cmdline_list.order,
216 cmdline_list.alloc * sizeof(module_loadorder_t));
217 if(!cmdline_list.order)
219 MESSAGE("Virtual memory exhausted\n");
223 memcpy(cmdline_list.order[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
224 cmdline_list.order[i].modulename = HeapAlloc(GetProcessHeap(), 0, strlen(plo->modulename)+1);
225 strcpy( (char *)cmdline_list.order[i].modulename, plo->modulename );
226 cmdline_list.count++;
231 /***************************************************************************
232 * AddLoadOrderSet (internal, static)
234 * Adds a set of entries in the list of command-line overrides from the key parameter.
236 static BOOL AddLoadOrderSet(char *key, char *order)
238 module_loadorder_t ldo;
241 /* Parse the loadorder before the rest because strtok is not reentrant */
242 if(!ParseLoadOrder(order, ldo.loadorder))
245 cptr = get_tok(key, ", \t");
248 char *ext = strrchr(cptr, '.');
251 if(strlen(ext) == 4 &&
252 (!FILE_strcasecmp(ext, ".dll") || !FILE_strcasecmp(ext, ".exe")))
253 MESSAGE("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr);
256 ldo.modulename = cptr;
257 if(!AddLoadOrder(&ldo)) return FALSE;
258 cptr = get_tok(NULL, ", \t");
264 /***************************************************************************
265 * MODULE_AddLoadOrderOption
267 * The commandline option is in the form:
268 * name[,name,...]=native[,b,...]
270 void MODULE_AddLoadOrderOption( const char *option )
272 char *value, *key = HeapAlloc(GetProcessHeap(), 0, strlen(option)+1);
274 strcpy( key, option );
275 if (!(value = strchr(key, '='))) goto error;
278 TRACE("Commandline override '%s' = '%s'\n", key, value);
280 if (!AddLoadOrderSet(key, value)) goto error;
281 HeapFree(GetProcessHeap(), 0, key);
283 /* sort the array for quick lookup */
284 qsort(cmdline_list.order, cmdline_list.count, sizeof(cmdline_list.order[0]), cmp_sort_func);
288 MESSAGE( "Syntax: -dll name[,name[,...]]={native|so|builtin}[,{n|s|b}[,...]]\n"
289 " - 'name' is the name of any dll without extension\n"
290 " - the order of loading (native, so and builtin) can be abbreviated\n"
291 " with the first letter\n"
292 " - the option can be specified multiple times\n"
294 " -dll comdlg32,commdlg=n -dll shell,shell32=b\n" );
299 /***************************************************************************
302 * Set individual registry keys for a multiple dll specification
303 * Helper for MODULE_InitLoadOrder().
305 inline static void set_registry_keys( HKEY hkey, char *module, const char *buffer )
308 char *p = get_tok( module, ", \t" );
310 TRACE( "converting \"%s\" = \"%s\"\n", module, buffer );
313 MESSAGE( "Warning: setting multiple modules in a single DllOverrides entry is no longer\n"
314 "recommended. It is suggested that you rewrite the configuration file entry:\n\n"
315 "\"%s\" = \"%s\"\n\n"
316 "into something like:\n\n", module, buffer );
319 if (!warn) MESSAGE( "\"%s\" = \"%s\"\n", p, buffer );
320 /* only set it if not existing already */
321 if (RegQueryValueExA( hkey, p, 0, NULL, NULL, NULL ) == ERROR_FILE_NOT_FOUND)
322 RegSetValueExA( hkey, p, 0, REG_SZ, buffer, strlen(buffer)+1 );
323 p = get_tok( NULL, ", \t" );
325 if (!warn) MESSAGE( "\n" );
330 /***************************************************************************
331 * MODULE_InitLoadOrder
333 * Convert entries containing multiple dll names (old syntax) to the
334 * new one dll module per entry syntax
336 void MODULE_InitLoadOrder(void)
344 if (RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\DllOverrides", &hkey ))
349 DWORD type, count = sizeof(buffer), name_len = sizeof(module);
351 if (RegEnumValueA( hkey, index, module, &name_len, NULL, &type, buffer, &count )) break;
353 while (isspace(*p)) p++;
354 p += strcspn( p, ", \t" );
355 while (isspace(*p)) p++;
358 RegDeleteValueA( hkey, module );
359 set_registry_keys( hkey, module, buffer );
367 /***************************************************************************
368 * get_list_load_order
370 * Get the load order for a given module from the command-line or
373 static BOOL get_list_load_order( const char *module, const struct loadorder_list *list,
374 enum loadorder_type lo[] )
376 module_loadorder_t tmp, *res = NULL;
378 tmp.modulename = module;
379 /* some bsearch implementations (Solaris) are buggy when the number of items is 0 */
380 if (list->count && (res = bsearch(&tmp, list->order, list->count, sizeof(list->order[0]), cmp_sort_func)))
381 memcpy( lo, res->loadorder, sizeof(res->loadorder) );
382 return (res != NULL);
386 /***************************************************************************
389 * Get the load order for a given module from the app-specific DllOverrides list.
390 * Also look for default '*' key if no module key found.
392 static BOOL get_app_load_order( const char *module, enum loadorder_type lo[], BOOL *got_default )
395 DWORD count, type, res;
396 char buffer[MAX_PATH+16], *appname, *p;
398 if (!GetModuleFileName16( GetCurrentTask(), buffer, MAX_PATH ) &&
399 !GetModuleFileNameA( 0, buffer, MAX_PATH ))
401 WARN( "could not get module file name loading %s\n", module );
405 if ((p = strrchr( appname, '/' ))) appname = p + 1;
406 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
408 TRACE( "searching '%s' in AppDefaults\\%s\\DllOverrides\n", module, appname );
410 if (RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\AppDefaults", &hkey ))
413 /* open AppDefaults\\appname\\DllOverrides key */
414 strcat( appname, "\\DllOverrides" );
415 res = RegOpenKeyA( hkey, appname, &appkey );
417 if (res) return FALSE;
419 count = sizeof(buffer);
420 if ((res = RegQueryValueExA( appkey, module, NULL, &type, buffer, &count )))
422 if (!(res = RegQueryValueExA( appkey, "*", NULL, &type, buffer, &count )))
425 else TRACE( "got app loadorder '%s' for '%s'\n", buffer, module );
426 RegCloseKey( appkey );
427 if (res) return FALSE;
428 return ParseLoadOrder( buffer, lo );
432 /***************************************************************************
433 * get_standard_load_order
435 * Get the load order for a given module from the main DllOverrides list
436 * Also look for default '*' key if no module key found.
438 static BOOL get_standard_load_order( const char *module, enum loadorder_type lo[],
442 DWORD count, type, res;
445 if (RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\DllOverrides", &hkey ))
448 count = sizeof(buffer);
449 if ((res = RegQueryValueExA( hkey, module, NULL, &type, buffer, &count )))
451 if (!(res = RegQueryValueExA( hkey, "*", NULL, &type, buffer, &count )))
454 else TRACE( "got standard loadorder '%s' for '%s'\n", buffer, module );
456 if (res) return FALSE;
457 return ParseLoadOrder( buffer, lo );
461 /***************************************************************************
462 * get_default_load_order
464 * Get the default load order if nothing specified for a given dll.
466 static void get_default_load_order( enum loadorder_type lo[] )
469 static enum loadorder_type default_loadorder[LOADORDER_NTYPES];
477 if (!(res = RegOpenKeyA( HKEY_LOCAL_MACHINE,
478 "Software\\Wine\\Wine\\Config\\DllDefaults", &hkey )))
480 DWORD type, count = sizeof(buffer);
482 res = RegQueryValueExA( hkey, "DefaultLoadOrder", NULL, &type, buffer, &count );
485 if (res) strcpy( buffer, "n,b,s" );
486 ParseLoadOrder( buffer, default_loadorder );
488 TRACE( "got default loadorder '%s'\n", buffer );
490 memcpy( lo, default_loadorder, sizeof(default_loadorder) );
494 /***************************************************************************
495 * MODULE_GetLoadOrder (internal)
497 * Locate the loadorder of a module.
498 * Any path is stripped from the path-argument and so are the extension
499 * '.dll' and '.exe'. A lookup in the table can yield an override for
500 * the specific dll. Otherwise the default load order is returned.
502 void MODULE_GetLoadOrder( enum loadorder_type loadorder[], const char *path, BOOL win32 )
505 char sysdir[MAX_PATH+1];
509 BOOL got_app_default = FALSE, got_std_default = FALSE;
510 enum loadorder_type lo_default[LOADORDER_NTYPES];
512 TRACE("looking for %s\n", path);
514 if ( ! GetSystemDirectoryA ( sysdir, MAX_PATH ) ) goto done;
516 /* Strip path information for 16 bit modules or if the module
517 resides in the system directory */
518 if ( !win32 || !FILE_strncasecmp ( sysdir, path, strlen (sysdir) ) )
521 cptr = strrchr(path, '\\');
523 name = strrchr(path, '/');
525 name = strrchr(cptr, '/');
528 name = cptr ? cptr+1 : (char *)path;
532 if((cptr = strchr(name, ':')) != NULL) /* Also strip drive if in format 'C:MODULE.DLL' */
539 if(len >= sizeof(fname) || len <= 0)
541 WARN("Path '%s' -> '%s' reduces to zilch or just too large...\n", path, name);
546 if(len >= 4 && (!FILE_strcasecmp(fname+len-4, ".dll") || !FILE_strcasecmp(fname+len-4, ".exe")))
549 /* check command-line first */
550 if (get_list_load_order( fname, &cmdline_list, loadorder )) return;
552 /* then app-specific config */
553 if (get_app_load_order( fname, loadorder, &got_app_default ))
555 if (!got_app_default) return;
556 /* save the default value for later on */
557 memcpy( lo_default, loadorder, sizeof(lo_default) );
560 /* then standard config */
561 if (get_standard_load_order( fname, loadorder, &got_std_default ))
563 if (!got_std_default) return;
564 /* save the default value for later on */
565 if (!got_app_default) memcpy( lo_default, loadorder, sizeof(lo_default) );
568 /* then compiled-in defaults */
569 if (get_list_load_order( fname, &default_list, loadorder )) return;
572 /* last, return the default */
573 if (got_app_default || got_std_default)
574 memcpy( loadorder, lo_default, sizeof(lo_default) );
576 get_default_load_order( loadorder );