2 * Module/Library loadorder
4 * Copyright 1999 Bertho Stultiens
18 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(module
);
22 #define LOADORDER_ALLOC_CLUSTER 32 /* Allocate with 32 entries at a time */
24 typedef struct module_loadorder
26 const char *modulename
;
27 enum loadorder_type loadorder
[LOADORDER_NTYPES
];
34 module_loadorder_t
*order
;
37 /* default load-order if nothing specified */
38 /* the list must remain sorted by dll name */
39 static module_loadorder_t default_order_list
[] =
41 { "display", { LOADORDER_BI
, 0, 0, 0 } },
42 { "gdi", { LOADORDER_BI
, 0, 0, 0 } },
43 { "gdi32", { LOADORDER_BI
, 0, 0, 0 } },
44 { "glide2x", { LOADORDER_SO
, LOADORDER_DLL
, 0, 0 } },
45 { "glide3x", { LOADORDER_SO
, LOADORDER_DLL
, 0, 0 } },
46 { "icmp", { LOADORDER_BI
, 0, 0, 0 } },
47 { "kernel", { LOADORDER_BI
, 0, 0, 0 } },
48 { "kernel32", { LOADORDER_BI
, 0, 0, 0 } },
49 { "keyboard", { LOADORDER_BI
, 0, 0, 0 } },
50 { "krnl386", { LOADORDER_BI
, 0, 0, 0 } },
51 { "mmsystem", { LOADORDER_BI
, 0, 0, 0 } },
52 { "mouse", { LOADORDER_BI
, 0, 0, 0 } },
53 { "ntdll", { LOADORDER_BI
, 0, 0, 0 } },
54 { "odbc32", { LOADORDER_BI
, 0, 0, 0 } },
55 { "system", { LOADORDER_BI
, 0, 0, 0 } },
56 { "toolhelp", { LOADORDER_BI
, 0, 0, 0 } },
57 { "ttydrv", { LOADORDER_BI
, 0, 0, 0 } },
58 { "user", { LOADORDER_BI
, 0, 0, 0 } },
59 { "user32", { LOADORDER_BI
, 0, 0, 0 } },
60 { "w32skrnl", { LOADORDER_BI
, 0, 0, 0 } },
61 { "winaspi", { LOADORDER_BI
, 0, 0, 0 } },
62 { "windebug", { LOADORDER_DLL
, LOADORDER_BI
, 0, 0 } },
63 { "winedos", { LOADORDER_BI
, 0, 0, 0 } },
64 { "wineps", { LOADORDER_BI
, 0, 0, 0 } },
65 { "wing", { LOADORDER_BI
, 0, 0, 0 } },
66 { "winmm", { LOADORDER_BI
, 0, 0, 0 } },
67 { "winsock", { LOADORDER_BI
, 0, 0, 0 } },
68 { "wnaspi32", { LOADORDER_BI
, 0, 0, 0 } },
69 { "wow32", { LOADORDER_BI
, 0, 0, 0 } },
70 { "wprocs", { LOADORDER_BI
, 0, 0, 0 } },
71 { "ws2_32", { LOADORDER_BI
, 0, 0, 0 } },
72 { "wsock32", { LOADORDER_BI
, 0, 0, 0 } },
73 { "x11drv", { LOADORDER_BI
, 0, 0, 0 } }
76 static const struct loadorder_list default_list
=
78 sizeof(default_order_list
)/sizeof(default_order_list
[0]),
79 sizeof(default_order_list
)/sizeof(default_order_list
[0]),
83 static struct loadorder_list cmdline_list
;
86 /***************************************************************************
87 * cmp_sort_func (internal, static)
89 * Sorting and comparing function used in sort and search of loadorder
92 static int cmp_sort_func(const void *s1
, const void *s2
)
94 return FILE_strcasecmp(((module_loadorder_t
*)s1
)->modulename
,
95 ((module_loadorder_t
*)s2
)->modulename
);
99 /***************************************************************************
100 * get_tok (internal, static)
102 * strtok wrapper for non-destructive buffer writing.
103 * NOTE: strtok is not reentrant and therefore this code is neither.
105 static char *get_tok(const char *str
, const char *delim
)
107 static char *buf
= NULL
;
115 HeapFree(GetProcessHeap(), 0, buf
);
121 buf
= HeapAlloc(GetProcessHeap(), 0, strlen(str
)+1);
123 cptr
= strtok(buf
, delim
);
127 cptr
= strtok(NULL
, delim
);
132 HeapFree(GetProcessHeap(), 0, buf
);
139 /***************************************************************************
140 * ParseLoadOrder (internal, static)
142 * Parses the loadorder options from the configuration and puts it into
145 static BOOL
ParseLoadOrder(char *order
, enum loadorder_type lo
[])
151 cptr
= get_tok(order
, ", \t");
154 enum loadorder_type type
= LOADORDER_INVALID
;
156 if(n
>= LOADORDER_NTYPES
-1)
158 ERR("More than existing %d module-types specified, rest ignored\n", LOADORDER_NTYPES
-1);
164 case 'N': /* Native */
165 case 'n': type
= LOADORDER_DLL
; break;
167 case 'E': /* Elfdll */
169 if (!warn
++) MESSAGE("Load order 'elfdll' no longer supported, ignored\n");
172 case 's': type
= LOADORDER_SO
; break;
174 case 'B': /* Builtin */
175 case 'b': type
= LOADORDER_BI
; break;
178 ERR("Invalid load order module-type '%s', ignored\n", cptr
);
181 if(type
!= LOADORDER_INVALID
) lo
[n
++] = type
;
182 cptr
= get_tok(NULL
, ", \t");
184 lo
[n
] = LOADORDER_INVALID
;
189 /***************************************************************************
190 * AddLoadOrder (internal, static)
192 * Adds an entry in the list of command-line overrides.
194 static BOOL
AddLoadOrder(module_loadorder_t
*plo
)
198 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
200 for(i
= 0; i
< cmdline_list
.count
; i
++)
202 if(!cmp_sort_func(plo
, &cmdline_list
.order
[i
] ))
204 /* replace existing option */
205 memcpy( cmdline_list
.order
[i
].loadorder
, plo
->loadorder
, sizeof(plo
->loadorder
));
210 if (i
>= cmdline_list
.alloc
)
212 /* No space in current array, make it larger */
213 cmdline_list
.alloc
+= LOADORDER_ALLOC_CLUSTER
;
214 cmdline_list
.order
= HeapReAlloc(GetProcessHeap(), 0, cmdline_list
.order
,
215 cmdline_list
.alloc
* sizeof(module_loadorder_t
));
216 if(!cmdline_list
.order
)
218 MESSAGE("Virtual memory exhausted\n");
222 memcpy(cmdline_list
.order
[i
].loadorder
, plo
->loadorder
, sizeof(plo
->loadorder
));
223 cmdline_list
.order
[i
].modulename
= HeapAlloc(GetProcessHeap(), 0, strlen(plo
->modulename
)+1);
224 strcpy( (char *)cmdline_list
.order
[i
].modulename
, plo
->modulename
);
225 cmdline_list
.count
++;
230 /***************************************************************************
231 * AddLoadOrderSet (internal, static)
233 * Adds a set of entries in the list of command-line overrides from the key parameter.
235 static BOOL
AddLoadOrderSet(char *key
, char *order
)
237 module_loadorder_t ldo
;
240 /* Parse the loadorder before the rest because strtok is not reentrant */
241 if(!ParseLoadOrder(order
, ldo
.loadorder
))
244 cptr
= get_tok(key
, ", \t");
247 char *ext
= strrchr(cptr
, '.');
250 if(strlen(ext
) == 4 &&
251 (!FILE_strcasecmp(ext
, ".dll") || !FILE_strcasecmp(ext
, ".exe")))
252 MESSAGE("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr
);
255 ldo
.modulename
= cptr
;
256 if(!AddLoadOrder(&ldo
)) return FALSE
;
257 cptr
= get_tok(NULL
, ", \t");
263 /***************************************************************************
264 * MODULE_AddLoadOrderOption
266 * The commandline option is in the form:
267 * name[,name,...]=native[,b,...]
269 void MODULE_AddLoadOrderOption( const char *option
)
271 char *value
, *key
= HeapAlloc(GetProcessHeap(), 0, strlen(option
)+1);
273 strcpy( key
, option
);
274 if (!(value
= strchr(key
, '='))) goto error
;
277 TRACE("Commandline override '%s' = '%s'\n", key
, value
);
279 if (!AddLoadOrderSet(key
, value
)) goto error
;
280 HeapFree(GetProcessHeap(), 0, key
);
282 /* sort the array for quick lookup */
283 qsort(cmdline_list
.order
, cmdline_list
.count
, sizeof(cmdline_list
.order
[0]), cmp_sort_func
);
287 MESSAGE( "Syntax: -dll name[,name[,...]]={native|so|builtin}[,{n|s|b}[,...]]\n"
288 " - 'name' is the name of any dll without extension\n"
289 " - the order of loading (native, so and builtin) can be abbreviated\n"
290 " with the first letter\n"
291 " - the option can be specified multiple times\n"
293 " -dll comdlg32,commdlg=n -dll shell,shell32=b\n" );
298 /***************************************************************************
301 * Set individual registry keys for a multiple dll specification
302 * Helper for MODULE_InitLoadOrder().
304 inline static void set_registry_keys( HKEY hkey
, char *module
, const char *buffer
)
307 char *p
= get_tok( module
, ", \t" );
309 TRACE( "converting \"%s\" = \"%s\"\n", module
, buffer
);
312 MESSAGE( "Warning: setting multiple modules in a single DllOverrides entry is no longer\n"
313 "recommended. It is suggested that you rewrite the configuration file entry:\n\n"
314 "\"%s\" = \"%s\"\n\n"
315 "into something like:\n\n", module
, buffer
);
318 if (!warn
) MESSAGE( "\"%s\" = \"%s\"\n", p
, buffer
);
319 /* only set it if not existing already */
320 if (RegQueryValueExA( hkey
, p
, 0, NULL
, NULL
, NULL
) == ERROR_FILE_NOT_FOUND
)
321 RegSetValueExA( hkey
, p
, 0, REG_SZ
, buffer
, strlen(buffer
)+1 );
322 p
= get_tok( NULL
, ", \t" );
324 if (!warn
) MESSAGE( "\n" );
329 /***************************************************************************
330 * MODULE_InitLoadOrder
332 * Convert entries containing multiple dll names (old syntax) to the
333 * new one dll module per entry syntax
335 void MODULE_InitLoadOrder(void)
343 if (RegOpenKeyA( HKEY_LOCAL_MACHINE
, "Software\\Wine\\Wine\\Config\\DllOverrides", &hkey
))
348 DWORD type
, count
= sizeof(buffer
), name_len
= sizeof(module
);
350 if (RegEnumValueA( hkey
, index
, module
, &name_len
, NULL
, &type
, buffer
, &count
)) break;
352 while (isspace(*p
)) p
++;
353 p
+= strcspn( p
, ", \t" );
354 while (isspace(*p
)) p
++;
357 RegDeleteValueA( hkey
, module
);
358 set_registry_keys( hkey
, module
, buffer
);
366 /***************************************************************************
367 * get_list_load_order
369 * Get the load order for a given module from the command-line or
372 static BOOL
get_list_load_order( const char *module
, const struct loadorder_list
*list
,
373 enum loadorder_type lo
[] )
375 module_loadorder_t tmp
, *res
= NULL
;
377 tmp
.modulename
= module
;
378 /* some bsearch implementations (Solaris) are buggy when the number of items is 0 */
379 if (list
->count
&& (res
= bsearch(&tmp
, list
->order
, list
->count
, sizeof(list
->order
[0]), cmp_sort_func
)))
380 memcpy( lo
, res
->loadorder
, sizeof(res
->loadorder
) );
381 return (res
!= NULL
);
385 /***************************************************************************
388 * Get the load order for a given module from the app-specific DllOverrides list.
389 * Also look for default '*' key if no module key found.
391 static BOOL
get_app_load_order( const char *module
, enum loadorder_type lo
[], BOOL
*got_default
)
394 DWORD count
, type
, res
;
395 char buffer
[MAX_PATH
+16], *appname
, *p
;
397 if (!GetModuleFileName16( GetCurrentTask(), buffer
, MAX_PATH
) &&
398 !GetModuleFileNameA( 0, buffer
, MAX_PATH
))
400 WARN( "could not get module file name loading %s\n", module
);
404 if ((p
= strrchr( appname
, '/' ))) appname
= p
+ 1;
405 if ((p
= strrchr( appname
, '\\' ))) appname
= p
+ 1;
407 TRACE( "searching '%s' in AppDefaults\\%s\\DllOverrides\n", module
, appname
);
409 if (RegOpenKeyA( HKEY_LOCAL_MACHINE
, "Software\\Wine\\Wine\\Config\\AppDefaults", &hkey
))
412 /* open AppDefaults\\appname\\DllOverrides key */
413 strcat( appname
, "\\DllOverrides" );
414 res
= RegOpenKeyA( hkey
, appname
, &appkey
);
416 if (res
) return FALSE
;
418 count
= sizeof(buffer
);
419 if ((res
= RegQueryValueExA( appkey
, module
, NULL
, &type
, buffer
, &count
)))
421 if (!(res
= RegQueryValueExA( appkey
, "*", NULL
, &type
, buffer
, &count
)))
424 else TRACE( "got app loadorder '%s' for '%s'\n", buffer
, module
);
425 RegCloseKey( appkey
);
426 if (res
) return FALSE
;
427 return ParseLoadOrder( buffer
, lo
);
431 /***************************************************************************
432 * get_standard_load_order
434 * Get the load order for a given module from the main DllOverrides list
435 * Also look for default '*' key if no module key found.
437 static BOOL
get_standard_load_order( const char *module
, enum loadorder_type lo
[],
441 DWORD count
, type
, res
;
444 if (RegOpenKeyA( HKEY_LOCAL_MACHINE
, "Software\\Wine\\Wine\\Config\\DllOverrides", &hkey
))
447 count
= sizeof(buffer
);
448 if ((res
= RegQueryValueExA( hkey
, module
, NULL
, &type
, buffer
, &count
)))
450 if (!(res
= RegQueryValueExA( hkey
, "*", NULL
, &type
, buffer
, &count
)))
453 else TRACE( "got standard loadorder '%s' for '%s'\n", buffer
, module
);
455 if (res
) return FALSE
;
456 return ParseLoadOrder( buffer
, lo
);
460 /***************************************************************************
461 * get_default_load_order
463 * Get the default load order if nothing specified for a given dll.
465 static void get_default_load_order( enum loadorder_type lo
[] )
468 static enum loadorder_type default_loadorder
[LOADORDER_NTYPES
];
476 if (!(res
= RegOpenKeyA( HKEY_LOCAL_MACHINE
,
477 "Software\\Wine\\Wine\\Config\\DllDefaults", &hkey
)))
479 DWORD type
, count
= sizeof(buffer
);
481 res
= RegQueryValueExA( hkey
, "DefaultLoadOrder", NULL
, &type
, buffer
, &count
);
484 if (res
) strcpy( buffer
, "n,b,s" );
485 ParseLoadOrder( buffer
, default_loadorder
);
487 TRACE( "got default loadorder '%s'\n", buffer
);
489 memcpy( lo
, default_loadorder
, sizeof(default_loadorder
) );
493 /***************************************************************************
494 * MODULE_GetLoadOrder (internal)
496 * Locate the loadorder of a module.
497 * Any path is stripped from the path-argument and so are the extension
498 * '.dll' and '.exe'. A lookup in the table can yield an override for
499 * the specific dll. Otherwise the default load order is returned.
501 void MODULE_GetLoadOrder( enum loadorder_type loadorder
[], const char *path
, BOOL win32
)
504 char sysdir
[MAX_PATH
+1];
508 BOOL got_app_default
= FALSE
, got_std_default
= FALSE
;
509 enum loadorder_type lo_default
[LOADORDER_NTYPES
];
511 TRACE("looking for %s\n", path
);
513 if ( ! GetSystemDirectoryA ( sysdir
, MAX_PATH
) ) goto done
;
515 /* Strip path information for 16 bit modules or if the module
516 resides in the system directory */
517 if ( !win32
|| !FILE_strncasecmp ( sysdir
, path
, strlen (sysdir
) ) )
520 cptr
= strrchr(path
, '\\');
522 name
= strrchr(path
, '/');
524 name
= strrchr(cptr
, '/');
527 name
= cptr
? cptr
+1 : (char *)path
;
531 if((cptr
= strchr(name
, ':')) != NULL
) /* Also strip drive if in format 'C:MODULE.DLL' */
538 if(len
>= sizeof(fname
) || len
<= 0)
540 WARN("Path '%s' -> '%s' reduces to zilch or just too large...\n", path
, name
);
545 if(len
>= 4 && (!FILE_strcasecmp(fname
+len
-4, ".dll") || !FILE_strcasecmp(fname
+len
-4, ".exe")))
548 /* check command-line first */
549 if (get_list_load_order( fname
, &cmdline_list
, loadorder
)) return;
551 /* then app-specific config */
552 if (get_app_load_order( fname
, loadorder
, &got_app_default
))
554 if (!got_app_default
) return;
555 /* save the default value for later on */
556 memcpy( lo_default
, loadorder
, sizeof(lo_default
) );
559 /* then standard config */
560 if (get_standard_load_order( fname
, loadorder
, &got_std_default
))
562 if (!got_std_default
) return;
563 /* save the default value for later on */
564 if (!got_app_default
) memcpy( lo_default
, loadorder
, sizeof(lo_default
) );
567 /* then compiled-in defaults */
568 if (get_list_load_order( fname
, &default_list
, loadorder
)) return;
571 /* last, return the default */
572 if (got_app_default
|| got_std_default
)
573 memcpy( loadorder
, lo_default
, sizeof(lo_default
) );
575 get_default_load_order( loadorder
);