Create rpcrt4.dll.
[wine] / loader / loadorder.c
1 /*
2  * Module/Library loadorder
3  *
4  * Copyright 1999 Bertho Stultiens
5  */
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include <assert.h>
10
11 #include "config.h"
12 #include "windef.h"
13 #include "options.h"
14 #include "loadorder.h"
15 #include "heap.h"
16 #include "module.h"
17 #include "elfdll.h"
18 #include "debugtools.h"
19
20 DEFAULT_DEBUG_CHANNEL(module)
21
22
23 /* #define DEBUG_LOADORDER */
24
25 #define LOADORDER_ALLOC_CLUSTER 32      /* Allocate with 32 entries at a time */
26
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;
31
32 static struct tagDllOverride {
33         char *key,*value;
34 } DefaultDllOverrides[] = {
35         {"kernel32,gdi32,user32",       "builtin"},
36         {"krnl386,gdi,user",            "builtin"},
37         {"toolhelp",                    "builtin"},
38         {"comdlg32,commdlg",            "elfdll,builtin,native"},
39         {"version,ver",                 "elfdll,builtin,native"},
40         {"shell32,shell",               "builtin,native"},
41         {"shlwapi",                     "native,builtin"},
42         {"lz32,lzexpand",               "builtin,native"},
43         {"commctrl,comctl32",           "builtin,native"},
44         {"wsock32,winsock",             "builtin"},
45         {"ws2_32",                      "builtin"},
46         {"advapi32,crtdll,ntdll",       "builtin,native"},
47         {"mpr,winspool.drv",            "builtin,native"},
48         {"ddraw,dinput,dsound",         "builtin,native"},
49         {"winmm, mmsystem",             "builtin"},
50         {"msvideo, msvfw32",            "builtin, native"},
51         {"mcicda.drv, mciseq.drv",      "builtin, native"},
52         {"mciwave.drv",                 "builtin, native"},
53         {"mciavi.drv, mcianim.drv",     "native, builtin"},
54         {"msacm.drv, midimap.drv",      "builtin, native"},
55         {"w32skrnl",                    "builtin"},
56         {"wnaspi32,wow32",              "builtin"},
57         {"system,display,wprocs ",      "builtin"},
58         {"wineps",                      "builtin"},
59         {"icmp",                        "builtin"},
60         /* we have to use libglide2x.so instead of glide2x.dll ... */
61         {"glide2x",                     "so,native"},
62         {"glide3x",                     "so,native"},
63         {"odbc32",                      "builtin"},
64         {"opengl32",                    "builtin,native"},
65         {"shfolder",                    "builtin,native"},
66         {"rpcrt4",                      "native,builtin"},
67         {NULL,NULL},
68 };
69
70 static const struct tagDllPair {
71     const char *dll1, *dll2;
72 } DllPairs[] = {
73     { "krnl386",  "kernel32" },
74     { "gdi",      "gdi32" },
75     { "user",     "user32" },
76     { "commdlg",  "comdlg32" },
77     { "commctrl", "comctl32" },
78     { "ver",      "version" },
79     { "shell",    "shell32" },
80     { "lzexpand", "lz32" },
81     { "mmsystem", "winmm" },
82     { "msvideo",  "msvfw32" },
83     { "winsock",  "wsock32" },
84     { NULL,       NULL }
85 };
86
87 /***************************************************************************
88  *      cmp_sort_func   (internal, static)
89  *
90  * Sorting and comparing function used in sort and search of loadorder
91  * entries.
92  */
93 static int cmp_sort_func(const void *s1, const void *s2)
94 {
95         return strcasecmp(((module_loadorder_t *)s1)->modulename, ((module_loadorder_t *)s2)->modulename);
96 }
97
98
99 /***************************************************************************
100  *      get_tok (internal, static)
101  *
102  * strtok wrapper for non-destructive buffer writing.
103  * NOTE: strtok is not reentrant and therefore this code is neither.
104  */
105 static char *get_tok(const char *str, const char *delim)
106 {
107         static char *buf = NULL;
108         char *cptr;
109
110         if(!str && !buf)
111                 return NULL;
112
113         if(str && buf)
114         {
115                 HeapFree(GetProcessHeap(), 0, buf);
116                 buf = NULL;
117         }
118
119         if(str && !buf)
120         {
121                 buf = HEAP_strdupA(GetProcessHeap(), 0, str);
122                 cptr = strtok(buf, delim);
123         }
124         else
125         {
126                 cptr = strtok(NULL, delim);
127         }
128
129         if(!cptr)
130         {
131                 HeapFree(GetProcessHeap(), 0, buf);
132                 buf = NULL;
133         }
134         return cptr;
135 }
136
137
138 /***************************************************************************
139  *      ParseLoadOrder  (internal, static)
140  *
141  * Parses the loadorder options from the configuration and puts it into
142  * a structure.
143  */
144 static BOOL ParseLoadOrder(char *order, module_loadorder_t *mlo)
145 {
146         char *cptr;
147         int n = 0;
148
149         memset(mlo->loadorder, 0, sizeof(mlo->loadorder));
150
151         cptr = get_tok(order, ", \t");
152         while(cptr)
153         {
154                 char type = MODULE_LOADORDER_INVALID;
155
156                 if(n >= MODULE_LOADORDER_NTYPES)
157                 {
158                         ERR("More than existing %d module-types specified, rest ignored", MODULE_LOADORDER_NTYPES);
159                         break;
160                 }
161
162                 switch(*cptr)
163                 {
164                 case 'N':       /* Native */
165                 case 'n': type = MODULE_LOADORDER_DLL; break;
166
167                 case 'E':       /* Elfdll */
168                 case 'e': type = MODULE_LOADORDER_ELFDLL; break;
169
170                 case 'S':       /* So */
171                 case 's': type = MODULE_LOADORDER_SO; break;
172
173                 case 'B':       /* Builtin */
174                 case 'b': type = MODULE_LOADORDER_BI; break;
175
176                 default:
177                         ERR("Invalid load order module-type '%s', ignored\n", cptr);
178                 }
179
180                 if(type != MODULE_LOADORDER_INVALID)
181                 {
182                         mlo->loadorder[n++] = type;
183                 }
184                 cptr = get_tok(NULL, ", \t");
185         }
186         return TRUE;
187 }
188
189
190 /***************************************************************************
191  *      AddLoadOrder    (internal, static)
192  *
193  * Adds an entry in the list of overrides. If the entry exists, then the
194  * override parameter determines whether it will be overwritten.
195  */
196 static BOOL AddLoadOrder(module_loadorder_t *plo, BOOL override)
197 {
198         int i;
199
200         /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
201
202         for(i = 0; i < nmodule_loadorder; i++)
203         {
204                 if(!cmp_sort_func(plo, &module_loadorder[i]))
205                 {
206                         if(!override)
207                                 ERR("Module '%s' is already in the list of overrides, using first definition\n", plo->modulename);
208                         else
209                                 memcpy(module_loadorder[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
210                         return TRUE;
211                 }
212         }
213
214         if(nmodule_loadorder >= nmodule_loadorder_alloc)
215         {
216                 /* No space in current array, make it larger */
217                 nmodule_loadorder_alloc += LOADORDER_ALLOC_CLUSTER;
218                 module_loadorder = (module_loadorder_t *)HeapReAlloc(GetProcessHeap(),
219                                                                      0,
220                                                                      module_loadorder,
221                                                                      nmodule_loadorder_alloc * sizeof(module_loadorder_t));
222                 if(!module_loadorder)
223                 {
224                         MESSAGE("Virtual memory exhausted\n");
225                         exit(1);
226                 }
227         }
228         memcpy(module_loadorder[nmodule_loadorder].loadorder, plo->loadorder, sizeof(plo->loadorder));
229         module_loadorder[nmodule_loadorder].modulename = HEAP_strdupA(GetProcessHeap(), 0, plo->modulename);
230         nmodule_loadorder++;
231         return TRUE;
232 }
233
234
235 /***************************************************************************
236  *      AddLoadOrderSet (internal, static)
237  *
238  * Adds a set of entries in the list of overrides from the key parameter.
239  * If the entry exists, then the override parameter determines whether it
240  * will be overwritten.
241  */
242 static BOOL AddLoadOrderSet(char *key, char *order, BOOL override)
243 {
244         module_loadorder_t ldo;
245         char *cptr;
246
247         /* Parse the loadorder before the rest because strtok is not reentrant */
248         if(!ParseLoadOrder(order, &ldo))
249                 return FALSE;
250
251         cptr = get_tok(key, ", \t");
252         while(cptr)
253         {
254                 char *ext = strrchr(cptr, '.');
255                 if(ext)
256                 {
257                         if(strlen(ext) == 4 && (!strcasecmp(ext, ".dll") || !strcasecmp(ext, ".exe")))
258                                 MESSAGE("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr);
259                 }
260
261                 ldo.modulename = cptr;
262                 if(!AddLoadOrder(&ldo, override))
263                         return FALSE;
264                 cptr = get_tok(NULL, ", \t");
265         }
266         return TRUE;
267 }
268
269
270 /***************************************************************************
271  *      ParseCommandlineOverrides       (internal, static)
272  *
273  * The commandline is in the form:
274  * name[,name,...]=native[,b,...][:...]
275  */
276 static BOOL ParseCommandlineOverrides(void)
277 {
278         char *cpy;
279         char *key;
280         char *next;
281         char *value;
282         BOOL retval = TRUE;
283
284         if(!Options.dllFlags)
285                 return TRUE;
286
287         cpy = HEAP_strdupA(GetProcessHeap(), 0, Options.dllFlags);
288         key = cpy;
289         next = key;
290         for(; next; key = next)
291         {
292                 next = strchr(key, ':');
293                 if(next)
294                 {
295                         *next = '\0';
296                         next++;
297                 }
298                 value = strchr(key, '=');
299                 if(!value)
300                 {
301                         retval = FALSE;
302                         goto endit;
303                 }
304                 *value = '\0';
305                 value++;
306
307                 TRACE("Commandline override '%s' = '%s'\n", key, value);
308                 
309                 if(!AddLoadOrderSet(key, value, TRUE))
310                 {
311                         retval = FALSE;
312                         goto endit;
313                 }
314         }
315 endit:
316         HeapFree(GetProcessHeap(), 0, cpy);
317         return retval;;
318 }
319
320
321 /***************************************************************************
322  *      MODULE_InitLoadOrder    (internal)
323  *
324  * Initialize the load order from the wine.conf file.
325  * The section has the following format:
326  * Section:
327  *      [DllDefaults]
328  *
329  * Keys:
330  *      EXTRA_LD_LIBRARY_PATH=/usr/local/lib/wine[:/more/path/to/search[:...]]
331  * The path will be appended to any existing LD_LIBRARY_PATH from the 
332  * environment (see note in code below).
333  *
334  *      DefaultLoadOrder=native,elfdll,so,builtin
335  * A comma separated list of module types to try to load in that specific
336  * order. The DefaultLoadOrder key is used as a fallback when a module is
337  * not specified explicitly. If the DefaultLoadOrder key is not found, 
338  * then the order "dll,elfdll,so,bi" is used
339  * The possible module types are:
340  *      - native        Native windows dll files
341  *      - elfdll        Dlls encapsulated in .so libraries
342  *      - so            Native .so libraries mapped to dlls
343  *      - builtin       Built-in modules
344  *
345  * Case is not important and only the first letter of each type is enough to
346  * identify the type n[ative], e[lfdll], s[o], b[uiltin]. Also whitespace is
347  * ignored.
348  * E.g.:
349  *      n,el    ,s , b
350  * is equal to:
351  *      native,elfdll,so,builtin
352  *
353  * Section:
354  *      [DllOverrides]
355  *
356  * Keys:
357  * There are no explicit keys defined other than module/library names. A comma
358  * separated list of modules is followed by an assignment of the load-order
359  * for these specific modules. See above for possible types. You should not
360  * specify an extension.
361  * Examples:
362  * kernel32, gdi32, user32 = builtin
363  * kernel, gdi, user = builtin
364  * comdlg32 = elfdll, native, builtin
365  * commdlg = native, builtin
366  * version, ver = elfdll, native, builtin
367  *
368  */
369
370 #define BUFFERSIZE      1024
371
372 BOOL MODULE_InitLoadOrder(void)
373 {
374         char buffer[BUFFERSIZE];
375         char key[256];
376         int nbuffer;
377         int idx;
378         const struct tagDllPair *dllpair;
379
380 #if defined(HAVE_DL_API)
381         /* Get/set the new LD_LIBRARY_PATH */
382         nbuffer = PROFILE_GetWineIniString("DllDefaults", "EXTRA_LD_LIBRARY_PATH", "", buffer, sizeof(buffer));
383
384         if(nbuffer)
385         {
386                 extra_ld_library_path = HEAP_strdupA(GetProcessHeap(), 0, buffer);
387                 TRACE("Setting extra LD_LIBRARY_PATH=%s\n", buffer);
388         }
389 #endif
390
391         /* Get the default load order */
392         nbuffer = PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,b,e,s", buffer, sizeof(buffer));
393         if(!nbuffer)
394         {
395                 MESSAGE("MODULE_InitLoadOrder: mysteriously read nothing from default loadorder\n");
396                 return FALSE;
397         }
398
399         TRACE("Setting default loadorder=%s\n", buffer);
400
401         if(!ParseLoadOrder(buffer, &default_loadorder))
402                 return FALSE;
403         default_loadorder.modulename = "<none>";
404
405         {
406             int i;
407             for (i=0;DefaultDllOverrides[i].key;i++)
408                 AddLoadOrderSet(
409                     DefaultDllOverrides[i].key,
410                     DefaultDllOverrides[i].value,
411                     FALSE
412                 );
413         }
414
415         /* Read the explicitely defined orders for specific modules as an entire section */
416         idx = 0;
417         while (PROFILE_EnumWineIniString( "DllOverrides", idx++, key, sizeof(key),
418                                           buffer, sizeof(buffer)))
419         {
420             TRACE("Key '%s' uses override '%s'\n", key, buffer);
421             if(!AddLoadOrderSet(key, buffer, TRUE))
422                 return FALSE;
423         }
424
425         /* Add the commandline overrides to the pool */
426         if(!ParseCommandlineOverrides())
427         {
428                 MESSAGE(        "Syntax: -dll name[,name[,...]]={native|elfdll|so|builtin}[,{n|e|s|b}[,...]][:...]\n"
429                         "    - 'name' is the name of any dll without extension\n"
430                         "    - the order of loading (native, elfdll, so and builtin) can be abbreviated\n"
431                         "      with the first letter\n"
432                         "    - different loadorders for different dlls can be specified by seperating the\n"
433                         "      commandline entries with a ':'\n"
434                         "    Example:\n"
435                         "    -dll comdlg32,commdlg=n:shell,shell32=b\n"
436                    );
437                 return FALSE;
438         }
439
440         /* Sort the array for quick lookup */
441         qsort(module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
442
443         /* Check the pairs of dlls */
444         dllpair = DllPairs;
445         while (dllpair->dll1)
446         {
447             module_loadorder_t *plo1, *plo2;
448             plo1 = MODULE_GetLoadOrder(dllpair->dll1);
449             plo2 = MODULE_GetLoadOrder(dllpair->dll2);
450             assert(plo1 && plo2);
451             if(memcmp(plo1->loadorder, plo2->loadorder, sizeof(plo1->loadorder)))
452                 MESSAGE("Warning: Modules '%s' and '%s' have different loadorder which may cause trouble\n", dllpair->dll1, dllpair->dll2);
453             dllpair++;
454         }
455
456         if(TRACE_ON(module))
457         {
458                 int i, j;
459                 static char types[6] = "-NESB";
460
461                 for(i = 0; i < nmodule_loadorder; i++)
462                 {
463                         DPRINTF("%3d: %-12s:", i, module_loadorder[i].modulename);
464                         for(j = 0; j < MODULE_LOADORDER_NTYPES; j++)
465                                 DPRINTF(" %c", types[module_loadorder[i].loadorder[j] % (MODULE_LOADORDER_NTYPES+1)]);
466                         DPRINTF("\n");
467                 }
468         }
469
470         return TRUE;
471 }
472
473
474 /***************************************************************************
475  *      MODULE_GetLoadOrder     (internal)
476  *
477  * Locate the loadorder of a module.
478  * Any path is stripped from the path-argument and so are the extension
479  * '.dll' and '.exe'. A lookup in the table can yield an override for
480  * the specific dll. Otherwise the default load order is returned.
481  */
482 module_loadorder_t *MODULE_GetLoadOrder(const char *path)
483 {
484         module_loadorder_t lo, *tmp;
485         char fname[256];
486         char *cptr;
487         char *name;
488         int len;
489
490         TRACE("looking for %s\n", path);
491         
492         assert(path != NULL);
493
494         /* Strip path information */
495         cptr = strrchr(path, '\\');
496         if(!cptr)
497                 name = strrchr(path, '/');
498         else
499                 name = strrchr(cptr, '/');
500
501         if(!name)
502                 name = cptr ? cptr+1 : (char *)path;
503         else
504                 name++;
505
506         if((cptr = strchr(name, ':')) != NULL)  /* Also strip drive if in format 'C:MODULE.DLL' */
507                 name = cptr+1;
508
509         len = strlen(name);
510         if(len >= sizeof(fname) || len <= 0)
511         {
512                 ERR("Path '%s' -> '%s' reduces to zilch or just too large...\n", path, name);
513                 return &default_loadorder;
514         }
515
516         strcpy(fname, name);
517         if(len >= 4 && (!lstrcmpiA(fname+len-4, ".dll") || !lstrcmpiA(fname+len-4, ".exe")))
518                 fname[len-4] = '\0';
519
520         lo.modulename = fname;
521         tmp = bsearch(&lo, module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
522
523         TRACE("Looking for '%s' (%s), found '%s'\n", path, fname, tmp ? tmp->modulename : "<nothing>");
524
525         if(!tmp)
526                 return &default_loadorder;
527         return tmp;
528 }
529