1 /*
2 * Dlls load order support
3 *
4 * Copyright 1999 Bertho Stultiens
5 * Copyright 2003 Alexandre Julliard
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29
30 #include "windef.h"
31 #include "winternl.h"
32 #include "ntdll_misc.h"
33
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(module);
38
39 #define LOADORDER_ALLOC_CLUSTER 32 /* Allocate with 32 entries at a time */
40
41 typedef struct module_loadorder
42 {
43 const WCHAR *modulename;
44 enum loadorder loadorder;
45 } module_loadorder_t;
46
47 struct loadorder_list
48 {
49 int count;
50 int alloc;
51 module_loadorder_t *order;
52 };
53
54 static const WCHAR separatorsW[] = {',',' ','\t',0};
55
56 static int init_done;
57 static struct loadorder_list env_list;
58
59
60 /***************************************************************************
61 * cmp_sort_func (internal, static)
62 *
63 * Sorting and comparing function used in sort and search of loadorder
64 * entries.
65 */
66 static int cmp_sort_func(const void *s1, const void *s2)
67 {
68 return strcmpiW(((const module_loadorder_t *)s1)->modulename, ((const module_loadorder_t *)s2)->modulename);
69 }
70
71
72 /***************************************************************************
73 * get_basename
74 *
75 * Return the base name of a file name (i.e. remove the path components).
76 */
77 static const WCHAR *get_basename( const WCHAR *name )
78 {
79 const WCHAR *ptr;
80
81 if (name[0] && name[1] == ':') name += 2; /* strip drive specification */
82 if ((ptr = strrchrW( name, '\\' ))) name = ptr + 1;
83 if ((ptr = strrchrW( name, '/' ))) name = ptr + 1;
84 return name;
85 }
86
87 /***************************************************************************
88 * remove_dll_ext
89 *
90 * Remove extension if it is ".dll".
91 */
92 static inline void remove_dll_ext( WCHAR *ext )
93 {
94 if (ext[0] == '.' &&
95 toupperW(ext[1]) == 'D' &&
96 toupperW(ext[2]) == 'L' &&
97 toupperW(ext[3]) == 'L' &&
98 !ext[4]) ext[0] = 0;
99 }
100
101
102 /***************************************************************************
103 * debugstr_loadorder
104 *
105 * Return a loadorder in printable form.
106 */
107 static const char *debugstr_loadorder( enum loadorder lo )
108 {
109 switch(lo)
110 {
111 case LO_DISABLED: return "";
112 case LO_NATIVE: return "n";
113 case LO_BUILTIN: return "b";
114 case LO_NATIVE_BUILTIN: return "n,b";
115 case LO_BUILTIN_NATIVE: return "b,n";
116 case LO_DEFAULT: return "default";
117 default: return "??";
118 }
119 }
120
121
122 /***************************************************************************
123 * parse_load_order
124 *
125 * Parses the loadorder options from the configuration and puts it into
126 * a structure.
127 */
128 static enum loadorder parse_load_order( const WCHAR *order )
129 {
130 enum loadorder ret = LO_DISABLED;
131
132 while (*order)
133 {
134 order += strspnW( order, separatorsW );
135 switch(*order)
136 {
137 case 'N': /* native */
138 case 'n':
139 if (ret == LO_DISABLED) ret = LO_NATIVE;
140 else if (ret == LO_BUILTIN) return LO_BUILTIN_NATIVE;
141 break;
142 case 'B': /* builtin */
143 case 'b':
144 if (ret == LO_DISABLED) ret = LO_BUILTIN;
145 else if (ret == LO_NATIVE) return LO_NATIVE_BUILTIN;
146 break;
147 }
148 order += strcspnW( order, separatorsW );
149 }
150 return ret;
151 }
152
153
154 /***************************************************************************
155 * add_load_order
156 *
157 * Adds an entry in the list of environment overrides.
158 */
159 static void add_load_order( const module_loadorder_t *plo )
160 {
161 int i;
162
163 for(i = 0; i < env_list.count; i++)
164 {
165 if(!cmp_sort_func(plo, &env_list.order[i] ))
166 {
167 /* replace existing option */
168 env_list.order[i].loadorder = plo->loadorder;
169 return;
170 }
171 }
172
173 if (i >= env_list.alloc)
174 {
175 /* No space in current array, make it larger */
176 env_list.alloc += LOADORDER_ALLOC_CLUSTER;
177 if (env_list.order)
178 env_list.order = RtlReAllocateHeap(GetProcessHeap(), 0, env_list.order,
179 env_list.alloc * sizeof(module_loadorder_t));
180 else
181 env_list.order = RtlAllocateHeap(GetProcessHeap(), 0,
182 env_list.alloc * sizeof(module_loadorder_t));
183 if(!env_list.order)
184 {
185 MESSAGE("Virtual memory exhausted\n");
186 exit(1);
187 }
188 }
189 env_list.order[i].loadorder = plo->loadorder;
190 env_list.order[i].modulename = plo->modulename;
191 env_list.count++;
192 }
193
194
195 /***************************************************************************
196 * add_load_order_set
197 *
198 * Adds a set of entries in the list of command-line overrides from the key parameter.
199 */
200 static void add_load_order_set( WCHAR *entry )
201 {
202 module_loadorder_t ldo;
203 WCHAR *end = strchrW( entry, '=' );
204
205 if (!end) return;
206 *end++ = 0;
207 ldo.loadorder = parse_load_order( end );
208
209 while (*entry)
210 {
211 entry += strspnW( entry, separatorsW );
212 end = entry + strcspnW( entry, separatorsW );
213 if (*end) *end++ = 0;
214 if (*entry)
215 {
216 WCHAR *ext = strrchrW(entry, '.');
217 if (ext) remove_dll_ext( ext );
218 ldo.modulename = entry;
219 add_load_order( &ldo );
220 entry = end;
221 }
222 }
223 }
224
225
226 /***************************************************************************
227 * init_load_order
228 */
229 static void init_load_order(void)
230 {
231 const char *order = getenv( "WINEDLLOVERRIDES" );
232 UNICODE_STRING strW;
233 WCHAR *entry, *next;
234
235 init_done = 1;
236 if (!order) return;
237
238 if (!strcmp( order, "help" ))
239 {
240 MESSAGE( "Syntax:\n"
241 " WINEDLLOVERRIDES=\"entry;entry;entry...\"\n"
242 " where each entry is of the form:\n"
243 " module[,module...]={native|builtin}[,{b|n}]\n"
244 "\n"
245 " Only the first letter of the override (native or builtin)\n"
246 " is significant.\n\n"
247 "Example:\n"
248 " WINEDLLOVERRIDES=\"comdlg32=n,b;shell32,shlwapi=b\"\n" );
249 exit(0);
250 }
251
252 RtlCreateUnicodeStringFromAsciiz( &strW, order );
253 entry = strW.Buffer;
254 while (*entry)
255 {
256 while (*entry && *entry == ';') entry++;
257 if (!*entry) break;
258 next = strchrW( entry, ';' );
259 if (next) *next++ = 0;
260 else next = entry + strlenW(entry);
261 add_load_order_set( entry );
262 entry = next;
263 }
264
265 /* sort the array for quick lookup */
266 if (env_list.count)
267 qsort(env_list.order, env_list.count, sizeof(env_list.order[0]), cmp_sort_func);
268
269 /* Note: we don't free the Unicode string because the
270 * stored module names point inside it */
271 }
272
273
274 /***************************************************************************
275 * get_env_load_order
276 *
277 * Get the load order for a given module from the WINEDLLOVERRIDES environment variable.
278 */
279 static inline enum loadorder get_env_load_order( const WCHAR *module )
280 {
281 module_loadorder_t tmp, *res;
282
283 tmp.modulename = module;
284 /* some bsearch implementations (Solaris) are buggy when the number of items is 0 */
285 if (env_list.count &&
286 (res = bsearch(&tmp, env_list.order, env_list.count, sizeof(env_list.order[0]), cmp_sort_func)))
287 return res->loadorder;
288 return LO_INVALID;
289 }
290
291
292 /***************************************************************************
293 * get_standard_key
294 *
295 * Return a handle to the standard DllOverrides registry section.
296 */
297 static HANDLE get_standard_key(void)
298 {
299 static const WCHAR DllOverridesW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
300 'D','l','l','O','v','e','r','r','i','d','e','s',0};
301 static HANDLE std_key = (HANDLE)-1;
302
303 if (std_key == (HANDLE)-1)
304 {
305 OBJECT_ATTRIBUTES attr;
306 UNICODE_STRING nameW;
307 HANDLE root;
308
309 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
310 attr.Length = sizeof(attr);
311 attr.RootDirectory = root;
312 attr.ObjectName = &nameW;
313 attr.Attributes = 0;
314 attr.SecurityDescriptor = NULL;
315 attr.SecurityQualityOfService = NULL;
316 RtlInitUnicodeString( &nameW, DllOverridesW );
317
318 /* @@ Wine registry key: HKCU\Software\Wine\DllOverrides */
319 if (NtOpenKey( &std_key, KEY_ALL_ACCESS, &attr )) std_key = 0;
320 NtClose( root );
321 }
322 return std_key;
323 }
324
325
326 /***************************************************************************
327 * get_app_key
328 *
329 * Get the registry key for the app-specific DllOverrides list.
330 */
331 static HANDLE get_app_key( const WCHAR *app_name )
332 {
333 OBJECT_ATTRIBUTES attr;
334 UNICODE_STRING nameW;
335 HANDLE root;
336 WCHAR *str;
337 static const WCHAR AppDefaultsW[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
338 'A','p','p','D','e','f','a','u','l','t','s','\\',0};
339 static const WCHAR DllOverridesW[] = {'\\','D','l','l','O','v','e','r','r','i','d','e','s',0};
340 static HANDLE app_key = (HANDLE)-1;
341
342 if (app_key != (HANDLE)-1) return app_key;
343
344 str = RtlAllocateHeap( GetProcessHeap(), 0,
345 sizeof(AppDefaultsW) + sizeof(DllOverridesW) +
346 strlenW(app_name) * sizeof(WCHAR) );
347 if (!str) return 0;
348 strcpyW( str, AppDefaultsW );
349 strcatW( str, app_name );
350 strcatW( str, DllOverridesW );
351
352 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
353 attr.Length = sizeof(attr);
354 attr.RootDirectory = root;
355 attr.ObjectName = &nameW;
356 attr.Attributes = 0;
357 attr.SecurityDescriptor = NULL;
358 attr.SecurityQualityOfService = NULL;
359 RtlInitUnicodeString( &nameW, str );
360
361 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DllOverrides */
362 if (NtOpenKey( &app_key, KEY_ALL_ACCESS, &attr )) app_key = 0;
363 NtClose( root );
364 RtlFreeHeap( GetProcessHeap(), 0, str );
365 return app_key;
366 }
367
368
369 /***************************************************************************
370 * get_registry_value
371 *
372 * Load the registry loadorder value for a given module.
373 */
374 static enum loadorder get_registry_value( HANDLE hkey, const WCHAR *module )
375 {
376 UNICODE_STRING valueW;
377 char buffer[80];
378 DWORD count;
379
380 RtlInitUnicodeString( &valueW, module );
381
382 if (!NtQueryValueKey( hkey, &valueW, KeyValuePartialInformation,
383 buffer, sizeof(buffer), &count ))
384 {
385 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
386 return parse_load_order( str );
387 }
388 return LO_INVALID;
389 }
390
391
392 /***************************************************************************
393 * get_load_order_value
394 *
395 * Get the load order for the exact specified module string, looking in:
396 * 1. The WINEDLLOVERRIDES environment variable
397 * 2. The per-application DllOverrides key
398 * 3. The standard DllOverrides key
399 */
400 static enum loadorder get_load_order_value( HANDLE std_key, HANDLE app_key, const WCHAR *module )
401 {
402 enum loadorder ret;
403
404 if ((ret = get_env_load_order( module )) != LO_INVALID)
405 {
406 TRACE( "got environment %s for %s\n", debugstr_loadorder(ret), debugstr_w(module) );
407 return ret;
408 }
409
410 if (app_key && ((ret = get_registry_value( app_key, module )) != LO_INVALID))
411 {
412 TRACE( "got app defaults %s for %s\n", debugstr_loadorder(ret), debugstr_w(module) );
413 return ret;
414 }
415
416 if (std_key && ((ret = get_registry_value( std_key, module )) != LO_INVALID))
417 {
418 TRACE( "got standard key %s for %s\n", debugstr_loadorder(ret), debugstr_w(module) );
419 return ret;
420 }
421
422 return ret;
423 }
424
425
426 /***************************************************************************
427 * get_load_order (internal)
428 *
429 * Return the loadorder of a module.
430 * The system directory and '.dll' extension is stripped from the path.
431 */
432 enum loadorder get_load_order( const WCHAR *app_name, const WCHAR *path )
433 {
434 enum loadorder ret = LO_INVALID;
435 HANDLE std_key, app_key = 0;
436 WCHAR *module, *basename;
437 UNICODE_STRING path_str;
438 int len;
439
440 if (!init_done) init_load_order();
441 std_key = get_standard_key();
442 if (app_name) app_key = get_app_key( app_name );
443
444 TRACE("looking for %s\n", debugstr_w(path));
445
446 /* Strip path information if the module resides in the system directory
447 */
448 RtlInitUnicodeString( &path_str, path );
449 if (RtlPrefixUnicodeString( &system_dir, &path_str, TRUE ))
450 {
451 const WCHAR *p = path + system_dir.Length / sizeof(WCHAR);
452 while (*p == '\\' || *p == '/') p++;
453 if (!strchrW( p, '\\' ) && !strchrW( p, '/' )) path = p;
454 }
455
456 if (!(len = strlenW(path))) return ret;
457 if (!(module = RtlAllocateHeap( GetProcessHeap(), 0, (len + 2) * sizeof(WCHAR) ))) return ret;
458 strcpyW( module+1, path ); /* reserve module[0] for the wildcard char */
459 basename = (WCHAR *)get_basename( module+1 );
460
461 if (len >= 4) remove_dll_ext( module + 1 + len - 4 );
462
463 /* first explicit module name */
464 if ((ret = get_load_order_value( std_key, app_key, module+1 )) != LO_INVALID)
465 goto done;
466
467 /* then module basename preceded by '*' */
468 basename[-1] = '*';
469 if ((ret = get_load_order_value( std_key, app_key, basename-1 )) != LO_INVALID)
470 goto done;
471
472 /* then module basename without '*' (only if explicit path) */
473 if (basename != module+1 && ((ret = get_load_order_value( std_key, app_key, basename )) != LO_INVALID))
474 goto done;
475
476 /* if loading the main exe with an explicit path, try native first */
477 if (!app_name && basename != module+1)
478 {
479 ret = LO_NATIVE_BUILTIN;
480 TRACE( "got main exe default %s for %s\n", debugstr_loadorder(ret), debugstr_w(path) );
481 goto done;
482 }
483
484 /* and last the hard-coded default */
485 ret = LO_DEFAULT;
486 TRACE( "got hardcoded %s for %s\n", debugstr_loadorder(ret), debugstr_w(path) );
487
488 done:
489 RtlFreeHeap( GetProcessHeap(), 0, module );
490 return ret;
491 }
492
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.