loadorder.c 20.4 KB
Newer Older
1
/*
2
 * Dlls load order support
3 4
 *
 * Copyright 1999 Bertho Stultiens
5
 * Copyright 2003 Alexandre Julliard
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 21
 */

22
#include "config.h"
23
#include "wine/port.h"
24

25
#include <stdarg.h>
26 27 28 29 30
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "windef.h"
31
#include "winbase.h"
32
#include "winerror.h"
33
#include "winreg.h"
34
#include "winternl.h"
35
#include "ntdll_misc.h"
36
#include "module.h"
37

38
#include "wine/debug.h"
39
#include "wine/unicode.h"
40

41
WINE_DEFAULT_DEBUG_CHANNEL(module);
42

43
#define LOADORDER_ALLOC_CLUSTER	32	/* Allocate with 32 entries at a time */
44

45 46
typedef struct module_loadorder
{
47
    const WCHAR        *modulename;
48 49
    enum loadorder_type loadorder[LOADORDER_NTYPES];
} module_loadorder_t;
50

51 52 53 54 55 56
struct loadorder_list
{
    int                 count;
    int                 alloc;
    module_loadorder_t *order;
};
57

58
/* dll to load as builtins if not explicitly specified otherwise */
59
/* the list must remain sorted by dll name */
60
static const WCHAR default_builtins[][10] =
61
{
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    { 'g','d','i','3','2',0 },
    { 'i','c','m','p',0 },
    { 'k','e','r','n','e','l','3','2',0 },
    { 'n','t','d','l','l',0 },
    { 'o','d','b','c','3','2',0 },
    { 't','t','y','d','r','v',0 },
    { 'u','s','e','r','3','2',0 },
    { 'w','3','2','s','k','r','n','l',0 },
    { 'w','i','n','e','d','o','s',0 },
    { 'w','i','n','e','p','s',0 },
    { 'w','i','n','m','m',0 },
    { 'w','n','a','s','p','i','3','2',0 },
    { 'w','o','w','3','2',0 },
    { 'w','s','2','_','3','2',0 },
    { 'w','s','o','c','k','3','2',0 },
    { 'x','1','1','d','r','v',0 }
78 79
};

80 81 82
/* default if nothing else specified */
static const enum loadorder_type default_loadorder[LOADORDER_NTYPES] =
{
Eric Pouech's avatar
Eric Pouech committed
83
    LOADORDER_BI, LOADORDER_DLL, 0
84 85
};

86 87 88
/* default for modules with an explicit path */
static const enum loadorder_type default_path_loadorder[LOADORDER_NTYPES] =
{
Eric Pouech's avatar
Eric Pouech committed
89
    LOADORDER_DLL, LOADORDER_BI, 0
90 91
};

92 93
static const WCHAR separatorsW[] = {',',' ','\t',0};

94 95
static int init_done;
static struct loadorder_list env_list;
96 97


98 99 100 101 102 103 104 105
/***************************************************************************
 *	cmp_sort_func	(internal, static)
 *
 * Sorting and comparing function used in sort and search of loadorder
 * entries.
 */
static int cmp_sort_func(const void *s1, const void *s2)
{
Eric Pouech's avatar
Eric Pouech committed
106
    return strcmpiW(((const module_loadorder_t *)s1)->modulename, ((const module_loadorder_t *)s2)->modulename);
107 108 109 110 111 112 113 114
}


/***************************************************************************
 *	strcmp_func
 */
static int strcmp_func(const void *s1, const void *s2)
{
Eric Pouech's avatar
Eric Pouech committed
115
    return strcmpiW( (const WCHAR *)s1, (const WCHAR *)s2 );
116 117 118
}


119 120 121 122 123
/***************************************************************************
 *	get_basename
 *
 * Return the base name of a file name (i.e. remove the path components).
 */
124
static const WCHAR *get_basename( const WCHAR *name )
125
{
126
    const WCHAR *ptr;
127 128

    if (name[0] && name[1] == ':') name += 2;  /* strip drive specification */
129 130
    if ((ptr = strrchrW( name, '\\' ))) name = ptr + 1;
    if ((ptr = strrchrW( name, '/' ))) name = ptr + 1;
131 132 133
    return name;
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147
/***************************************************************************
 *	remove_dll_ext
 *
 * Remove extension if it is ".dll".
 */
static inline void remove_dll_ext( WCHAR *ext )
{
    if (ext[0] == '.' &&
        toupperW(ext[1]) == 'D' &&
        toupperW(ext[2]) == 'L' &&
        toupperW(ext[3]) == 'L' &&
        !ext[4]) ext[0] = 0;
}

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

/***************************************************************************
 *	debugstr_loadorder
 *
 * Return a loadorder in printable form.
 */
static const char *debugstr_loadorder( enum loadorder_type lo[] )
{
    int i;
    char buffer[LOADORDER_NTYPES*3+1];

    buffer[0] = 0;
    for(i = 0; i < LOADORDER_NTYPES; i++)
    {
        if (lo[i] == LOADORDER_INVALID) break;
        switch(lo[i])
        {
        case LOADORDER_DLL: strcat( buffer, "n," ); break;
        case LOADORDER_BI:  strcat( buffer, "b," ); break;
        default:            strcat( buffer, "?," ); break;
        }
    }
    if (buffer[0]) buffer[strlen(buffer)-1] = 0;
    return debugstr_a(buffer);
}


175
/***************************************************************************
176
 *	append_load_order
177
 *
178
 * Append a load order to the list if necessary.
179
 */
180
static void append_load_order(enum loadorder_type lo[], enum loadorder_type append)
181
{
182
    int i;
183

184 185 186 187 188 189 190 191 192 193 194 195
    for (i = 0; i < LOADORDER_NTYPES; i++)
    {
        if (lo[i] == LOADORDER_INVALID)  /* append it here */
        {
            lo[i++] = append;
            lo[i] = LOADORDER_INVALID;
            return;
        }
        if (lo[i] == append) return;  /* already in the list */
    }
    assert(0);  /* cannot get here */
}
196 197


198 199 200 201 202 203
/***************************************************************************
 *	parse_load_order
 *
 * Parses the loadorder options from the configuration and puts it into
 * a structure.
 */
204
static void parse_load_order( const WCHAR *order, enum loadorder_type lo[] )
205 206 207 208
{
    lo[0] = LOADORDER_INVALID;
    while (*order)
    {
209
        order += strspnW( order, separatorsW );
210 211 212 213 214 215 216 217 218 219 220
        switch(*order)
        {
        case 'N':	/* Native */
        case 'n':
            append_load_order( lo, LOADORDER_DLL );
            break;
        case 'B':	/* Builtin */
        case 'b':
            append_load_order( lo, LOADORDER_BI );
            break;
        }
221
        order += strcspnW( order, separatorsW );
222
    }
223 224 225 226
}


/***************************************************************************
227
 *	add_load_order
228
 *
229
 * Adds an entry in the list of environment overrides.
230
 */
231
static void add_load_order( const module_loadorder_t *plo )
232
{
233
    int i;
234

235 236 237 238 239 240 241 242 243
    for(i = 0; i < env_list.count; i++)
    {
        if(!cmp_sort_func(plo, &env_list.order[i] ))
        {
            /* replace existing option */
            memcpy( env_list.order[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
            return;
        }
    }
244

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    if (i >= env_list.alloc)
    {
        /* No space in current array, make it larger */
        env_list.alloc += LOADORDER_ALLOC_CLUSTER;
        if (env_list.order)
            env_list.order = RtlReAllocateHeap(GetProcessHeap(), 0, env_list.order,
                                               env_list.alloc * sizeof(module_loadorder_t));
        else
            env_list.order = RtlAllocateHeap(GetProcessHeap(), 0,
                                             env_list.alloc * sizeof(module_loadorder_t));
        if(!env_list.order)
        {
            MESSAGE("Virtual memory exhausted\n");
            exit(1);
        }
    }
    memcpy(env_list.order[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
262
    env_list.order[i].modulename = plo->modulename;
263
    env_list.count++;
264 265 266 267
}


/***************************************************************************
268
 *	add_load_order_set
269
 *
270
 * Adds a set of entries in the list of command-line overrides from the key parameter.
271
 */
272
static void add_load_order_set( WCHAR *entry )
273
{
274
    module_loadorder_t ldo;
275
    WCHAR *end = strchrW( entry, '=' );
276

277 278 279
    if (!end) return;
    *end++ = 0;
    parse_load_order( end, ldo.loadorder );
280

281 282
    while (*entry)
    {
283 284
        entry += strspnW( entry, separatorsW );
        end = entry + strcspnW( entry, separatorsW );
285 286
        if (*end) *end++ = 0;
        if (*entry)
287 288 289
        {
            WCHAR *ext = strrchrW(entry, '.');
            if (ext) remove_dll_ext( ext );
290 291 292
            ldo.modulename = entry;
            add_load_order( &ldo );
            entry = end;
293
        }
294
    }
295 296 297 298
}


/***************************************************************************
299
 *	init_load_order
300
 */
301
static void init_load_order(void)
302
{
303
    const char *order = getenv( "WINEDLLOVERRIDES" );
304 305
    UNICODE_STRING strW;
    WCHAR *entry, *next;
306

307 308
    init_done = 1;
    if (!order) return;
309

310 311 312 313 314 315 316 317 318 319
    if (!strcmp( order, "help" ))
    {
        MESSAGE( "Syntax:\n"
                 "  WINEDLLOVERRIDES=\"entry;entry;entry...\"\n"
                 "    where each entry is of the form:\n"
                 "        module[,module...]={native|builtin}[,{b|n}]\n"
                 "\n"
                 "    Only the first letter of the override (native or builtin)\n"
                 "    is significant.\n\n"
                 "Example:\n"
320
                 "  WINEDLLOVERRIDES=\"comdlg32=n,b;shell32,shlwapi=b\"\n" );
321 322
        exit(0);
    }
323

324 325
    RtlCreateUnicodeStringFromAsciiz( &strW, order );
    entry = strW.Buffer;
326 327 328 329
    while (*entry)
    {
        while (*entry && *entry == ';') entry++;
        if (!*entry) break;
330
        next = strchrW( entry, ';' );
331
        if (next) *next++ = 0;
332
        else next = entry + strlenW(entry);
333 334 335
        add_load_order_set( entry );
        entry = next;
    }
336 337

    /* sort the array for quick lookup */
338 339
    if (env_list.count)
        qsort(env_list.order, env_list.count, sizeof(env_list.order[0]), cmp_sort_func);
340 341 342

    /* Note: we don't free the Unicode string because the
     * stored module names point inside it */
343 344 345
}


346
/***************************************************************************
347
 *	get_env_load_order
348
 *
349
 * Get the load order for a given module from the WINEDLLOVERRIDES environment variable.
350
 */
351
static inline BOOL get_env_load_order( const WCHAR *module, enum loadorder_type lo[] )
352
{
Eric Pouech's avatar
Eric Pouech committed
353
    module_loadorder_t tmp, *res = NULL;
354

355
    tmp.modulename = module;
Eric Pouech's avatar
Eric Pouech committed
356
    /* some bsearch implementations (Solaris) are buggy when the number of items is 0 */
357 358
    if (env_list.count &&
        (res = bsearch(&tmp, env_list.order, env_list.count, sizeof(env_list.order[0]), cmp_sort_func)))
359 360 361
        memcpy( lo, res->loadorder, sizeof(res->loadorder) );
    return (res != NULL);
}
362 363


364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
/***************************************************************************
 *	get_default_load_order
 *
 * Get the load order for a given module from the default list.
 */
static inline BOOL get_default_load_order( const WCHAR *module, enum loadorder_type lo[] )
{
    const int count = sizeof(default_builtins) / sizeof(default_builtins[0]);
    if (!bsearch( module, default_builtins, count, sizeof(default_builtins[0]), strcmp_func ))
        return FALSE;
    lo[0] = LOADORDER_BI;
    lo[1] = LOADORDER_INVALID;
    return TRUE;
}


380
/***************************************************************************
381
 *	open_app_key
382
 *
383
 * Open the registry key to the app-specific DllOverrides list.
384
 */
385
static HKEY open_app_key( const WCHAR *app_name, const WCHAR *module )
386
{
387 388
    OBJECT_ATTRIBUTES attr;
    UNICODE_STRING nameW;
389 390
    HKEY hkey;
    WCHAR *str;
391 392 393 394 395
    static const WCHAR AppDefaultsW[] = {'M','a','c','h','i','n','e','\\',
                                         'S','o','f','t','w','a','r','e','\\',
                                         'W','i','n','e','\\',
                                         'W','i','n','e','\\',
                                         'C','o','n','f','i','g','\\',
396 397
                                         'A','p','p','D','e','f','a','u','l','t','s','\\',0};
    static const WCHAR DllOverridesW[] = {'\\','D','l','l','O','v','e','r','r','i','d','e','s',0};
398

399 400 401 402 403 404 405
    str = RtlAllocateHeap( GetProcessHeap(), 0,
                           sizeof(AppDefaultsW) + sizeof(DllOverridesW) +
                           strlenW(app_name) * sizeof(WCHAR) );
    if (!str) return 0;
    strcpyW( str, AppDefaultsW );
    strcatW( str, app_name );
    strcatW( str, DllOverridesW );
406

407
    TRACE( "searching %s in %s\n", debugstr_w(module), debugstr_w(str) );
408

409 410 411 412 413 414
    attr.Length = sizeof(attr);
    attr.RootDirectory = 0;
    attr.ObjectName = &nameW;
    attr.Attributes = 0;
    attr.SecurityDescriptor = NULL;
    attr.SecurityQualityOfService = NULL;
415 416 417 418 419
    RtlInitUnicodeString( &nameW, str );

    if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) hkey = 0;
    RtlFreeHeap( GetProcessHeap(), 0, str );
    return hkey;
420
}
421

422

423
/***************************************************************************
424
 *	get_registry_value
425
 *
426
 * Load the registry loadorder value for a given module.
427
 */
428
static BOOL get_registry_value( HKEY hkey, const WCHAR *module, enum loadorder_type lo[] )
429
{
430
    UNICODE_STRING valueW;
431
    char buffer[80];
432 433 434
    DWORD count;
    BOOL ret;

435
    RtlInitUnicodeString( &valueW, module );
436 437 438 439 440 441

    if ((ret = !NtQueryValueKey( hkey, &valueW, KeyValuePartialInformation,
                                 buffer, sizeof(buffer), &count )))
    {
        int i, n = 0;
        WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)buffer)->Data;
442

443 444 445 446 447 448 449 450 451 452 453
        while (*str)
        {
            enum loadorder_type type = LOADORDER_INVALID;

            while (*str == ',' || isspaceW(*str)) str++;
            if (!*str) break;

            switch(tolowerW(*str))
            {
            case 'n': type = LOADORDER_DLL; break;
            case 'b': type = LOADORDER_BI; break;
454
            case 's': break;  /* no longer supported, ignore */
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
            case 0:   break;  /* end of string */
            default:
                ERR("Invalid load order module-type %s, ignored\n", debugstr_w(str));
                break;
            }
            if (type != LOADORDER_INVALID)
            {
                for (i = 0; i < n; i++) if (lo[i] == type) break;  /* already specified */
                if (i == n) lo[n++] = type;
            }
            while (*str && *str != ',' && !isspaceW(*str)) str++;
        }
        lo[n] = LOADORDER_INVALID;
    }
    return ret;
470
}
471 472 473


/***************************************************************************
474
 *	MODULE_GetLoadOrderW	(internal)
475 476 477
 *
 * Locate the loadorder of a module.
 * Any path is stripped from the path-argument and so are the extension
478
 * '.dll' and '.exe'. A lookup in the table can yield an override for
479
 * the specific dll. Otherwise the default load order is returned.
480
 */
481
void MODULE_GetLoadOrderW( enum loadorder_type loadorder[], const WCHAR *app_name,
482
                          const WCHAR *path )
483
{
484 485 486 487 488 489 490
    static const WCHAR DllOverridesW[] = {'M','a','c','h','i','n','e','\\',
                                          'S','o','f','t','w','a','r','e','\\',
                                          'W','i','n','e','\\',
                                          'W','i','n','e','\\',
                                          'C','o','n','f','i','g','\\',
                                          'D','l','l','O','v','e','r','r','i','d','e','s',0};

491
    static HKEY std_key = (HKEY)-1;  /* key to standard section, cached */
492

493
    HKEY app_key = 0;
494
    WCHAR *module, *basename;
495
    UNICODE_STRING path_str;
496
    int len;
497

498 499
    if (!init_done) init_load_order();

500
    TRACE("looking for %s\n", debugstr_w(path));
501

502
    loadorder[0] = LOADORDER_INVALID;  /* in case something bad happens below */
503

504 505
    /* Strip path information if the module resides in the system directory
     */
506
    RtlInitUnicodeString( &path_str, path );
507
    if (RtlPrefixUnicodeString( &system_dir, &path_str, TRUE ))
508
    {
509
        const WCHAR *p = path + system_dir.Length / sizeof(WCHAR);
510 511
        while (*p == '\\' || *p == '/') p++;
        if (!strchrW( p, '\\' ) && !strchrW( p, '/' )) path = p;
512 513
    }

514 515 516
    if (!(len = strlenW(path))) return;
    if (!(module = RtlAllocateHeap( GetProcessHeap(), 0, (len + 2) * sizeof(WCHAR) ))) return;
    strcpyW( module+1, path );  /* reserve module[0] for the wildcard char */
517

518
    if (len >= 4) remove_dll_ext( module + 1 + len - 4 );
519

520
    /* check environment variable first */
521
    if (get_env_load_order( module+1, loadorder ))
522
    {
523
        TRACE( "got environment %s for %s\n",
524
               debugstr_loadorder(loadorder), debugstr_w(path) );
525 526
        goto done;
    }
527

528
    /* then explicit module name in AppDefaults */
529
    if (app_name)
530
    {
531 532 533 534
        app_key = open_app_key( app_name, module+1 );
        if (app_key && get_registry_value( app_key, module+1, loadorder ))
        {
            TRACE( "got app defaults %s for %s\n",
535
                   debugstr_loadorder(loadorder), debugstr_w(path) );
536 537
            goto done;
        }
538
    }
539

540 541
    /* then explicit module name in standard section */
    if (std_key == (HKEY)-1)
542 543 544 545 546 547 548 549 550 551 552 553 554 555
    {
        OBJECT_ATTRIBUTES attr;
        UNICODE_STRING nameW;

        attr.Length = sizeof(attr);
        attr.RootDirectory = 0;
        attr.ObjectName = &nameW;
        attr.Attributes = 0;
        attr.SecurityDescriptor = NULL;
        attr.SecurityQualityOfService = NULL;
        RtlInitUnicodeString( &nameW, DllOverridesW );

        if (NtOpenKey( &std_key, KEY_ALL_ACCESS, &attr )) std_key = 0;
    }
556 557 558 559

    if (std_key && get_registry_value( std_key, module+1, loadorder ))
    {
        TRACE( "got standard entry %s for %s\n",
560
               debugstr_loadorder(loadorder), debugstr_w(path) );
561 562
        goto done;
    }
563

564
    /* then module basename preceded by '*' in environment */
565
    basename = (WCHAR *)get_basename( module+1 );
566
    basename[-1] = '*';
567 568 569 570 571 572 573 574
    if (get_env_load_order( basename-1, loadorder ))
    {
        TRACE( "got environment basename %s for %s\n",
               debugstr_loadorder(loadorder), debugstr_w(path) );
        goto done;
    }

    /* then module basename preceded by '*' in AppDefaults */
575 576 577
    if (app_key && get_registry_value( app_key, basename-1, loadorder ))
    {
        TRACE( "got app defaults basename %s for %s\n",
578
               debugstr_loadorder(loadorder), debugstr_w(path) );
579 580
        goto done;
    }
581

582 583 584 585
    /* then module name preceded by '*' in standard section */
    if (std_key && get_registry_value( std_key, basename-1, loadorder ))
    {
        TRACE( "got standard base name %s for %s\n",
586
               debugstr_loadorder(loadorder), debugstr_w(path) );
587 588
        goto done;
    }
589

590
    if (basename == module+1)  /* module doesn't contain a path */
591
    {
592 593
        static const WCHAR wildcardW[] = {'*',0};

594 595 596 597 598 599 600 601
        /* then base name matching compiled-in defaults */
        if (get_default_load_order( basename, loadorder ))
        {
            TRACE( "got compiled-in default %s for %s\n",
                   debugstr_loadorder(loadorder), debugstr_w(path) );
            goto done;
        }

602
        /* then wildcard entry in AppDefaults (only if no explicit path) */
603
        if (app_key && get_registry_value( app_key, wildcardW, loadorder ))
604
        {
605
            TRACE( "got app defaults wildcard %s for %s\n",
606
                   debugstr_loadorder(loadorder), debugstr_w(path) );
607
            goto done;
608
        }
609

610
        /* then wildcard entry in standard section (only if no explicit path) */
611
        if (std_key && get_registry_value( std_key, wildcardW, loadorder ))
612
        {
613
            TRACE( "got standard wildcard %s for %s\n",
614
                   debugstr_loadorder(loadorder), debugstr_w(path) );
615
            goto done;
616
        }
617

618 619 620
        /* and last the hard-coded default */
        memcpy( loadorder, default_loadorder, sizeof(default_loadorder) );
        TRACE( "got hardcoded default %s for %s\n",
621
               debugstr_loadorder(loadorder), debugstr_w(path) );
622 623 624
    }
    else  /* module contains an explicit path */
    {
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
        /* then base name without '*' in AppDefaults */
        if (app_key && get_registry_value( app_key, basename, loadorder ))
        {
            TRACE( "got basename app defaults %s for %s\n",
                   debugstr_loadorder(loadorder), debugstr_w(path) );
            goto done;
        }

        /* then base name without '*' in standard section */
        if (std_key && get_registry_value( std_key, basename, loadorder ))
        {
            TRACE( "got basename standard entry %s for %s\n",
                   debugstr_loadorder(loadorder), debugstr_w(path) );
            goto done;
        }

        /* then base name matching compiled-in defaults */
        if (get_default_load_order( basename, loadorder ))
        {
            TRACE( "got compiled-in default %s for %s\n",
                   debugstr_loadorder(loadorder), debugstr_w(path) );
            goto done;
        }

        /* and last the hard-coded default */
650 651
        memcpy( loadorder, default_path_loadorder, sizeof(default_path_loadorder) );
        TRACE( "got hardcoded path default %s for %s\n",
652
               debugstr_loadorder(loadorder), debugstr_w(path) );
653
    }
654 655

 done:
656
    if (app_key) NtClose( app_key );
657
    RtlFreeHeap( GetProcessHeap(), 0, module );
658
}