Fixed crtdll compile problem regarding fpos_t in glibc2.2.
[wine/wine64.git] / loader / loadorder.c
blob9e1a1f62199bf04d2d59f9cccc7fef53534c5e72
1 /*
2 * Module/Library loadorder
4 * Copyright 1999 Bertho Stultiens
5 */
7 #include <stdlib.h>
8 #include <string.h>
9 #include <assert.h>
11 #include "config.h"
12 #include "windef.h"
13 #include "options.h"
14 #include "loadorder.h"
15 #include "heap.h"
16 #include "file.h"
17 #include "module.h"
18 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(module);
23 /* #define DEBUG_LOADORDER */
25 #define LOADORDER_ALLOC_CLUSTER 32 /* Allocate with 32 entries at a time */
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;
32 /* DLL order is irrelevant ! Gets sorted later. */
33 static struct tagDllOverride {
34 char *key,*value;
35 } DefaultDllOverrides[] = {
36 /* "system" DLLs */
37 {"kernel32,gdi32,user32", "builtin"},
38 {"krnl386,gdi,user", "builtin"},
39 {"toolhelp", "builtin"},
40 {"windebug", "native,builtin"},
41 {"system,display", "builtin"},
42 {"w32skrnl,wow32", "builtin"},
43 {"advapi32,crtdll,ntdll", "builtin,native"},
44 {"lz32,lzexpand", "builtin,native"},
45 {"version,ver", "builtin,native"},
46 /* "new" interface */
47 {"comdlg32,commdlg", "builtin,native"},
48 {"shell32,shell", "builtin,native"},
49 {"shlwapi", "native,builtin"},
50 {"shfolder", "builtin,native"},
51 {"comctl32,commctrl", "builtin,native"},
52 /* network */
53 {"wsock32,ws2_32,winsock", "builtin"},
54 {"icmp", "builtin"},
55 /* multimedia */
56 {"ddraw,dinput,dsound", "builtin,native"},
57 {"winmm,mmsystem", "builtin"},
58 {"msvfw32,msvideo", "builtin,native"},
59 {"mcicda.drv,mciseq.drv", "builtin,native"},
60 {"mciwave.drv", "builtin,native"},
61 {"mciavi.drv,mcianim.drv", "native,builtin"},
62 {"msacm.drv,midimap.drv", "builtin,native"},
63 {"opengl32", "builtin,native"},
64 /* we have to use libglideXx.so instead of glideXx.dll ... */
65 {"glide2x,glide3x", "so,native"},
66 /* other stuff */
67 {"mpr,winspool.drv", "builtin,native"},
68 {"wnaspi32,winaspi", "builtin"},
69 {"odbc32", "builtin"},
70 {"rpcrt4", "native,builtin"},
71 /* non-windows DLLs */
72 {"wineps,wprocs,x11drv", "builtin"},
73 {NULL,NULL},
76 static const struct tagDllPair {
77 const char *dll1, *dll2;
78 } DllPairs[] = {
79 { "krnl386", "kernel32" },
80 { "gdi", "gdi32" },
81 { "user", "user32" },
82 { "commdlg", "comdlg32" },
83 { "commctrl", "comctl32" },
84 { "ver", "version" },
85 { "shell", "shell32" },
86 { "lzexpand", "lz32" },
87 { "mmsystem", "winmm" },
88 { "msvideo", "msvfw32" },
89 { "winsock", "wsock32" },
90 { NULL, NULL }
93 /***************************************************************************
94 * cmp_sort_func (internal, static)
96 * Sorting and comparing function used in sort and search of loadorder
97 * entries.
99 static int cmp_sort_func(const void *s1, const void *s2)
101 return FILE_strcasecmp(((module_loadorder_t *)s1)->modulename,
102 ((module_loadorder_t *)s2)->modulename);
106 /***************************************************************************
107 * get_tok (internal, static)
109 * strtok wrapper for non-destructive buffer writing.
110 * NOTE: strtok is not reentrant and therefore this code is neither.
112 static char *get_tok(const char *str, const char *delim)
114 static char *buf = NULL;
115 char *cptr;
117 if(!str && !buf)
118 return NULL;
120 if(str && buf)
122 HeapFree(GetProcessHeap(), 0, buf);
123 buf = NULL;
126 if(str && !buf)
128 buf = HEAP_strdupA(GetProcessHeap(), 0, str);
129 cptr = strtok(buf, delim);
131 else
133 cptr = strtok(NULL, delim);
136 if(!cptr)
138 HeapFree(GetProcessHeap(), 0, buf);
139 buf = NULL;
141 return cptr;
145 /***************************************************************************
146 * ParseLoadOrder (internal, static)
148 * Parses the loadorder options from the configuration and puts it into
149 * a structure.
151 static BOOL ParseLoadOrder(char *order, module_loadorder_t *mlo)
153 static int warn;
154 char *cptr;
155 int n = 0;
157 memset(mlo->loadorder, 0, sizeof(mlo->loadorder));
159 cptr = get_tok(order, ", \t");
160 while(cptr)
162 char type = MODULE_LOADORDER_INVALID;
164 if(n >= MODULE_LOADORDER_NTYPES)
166 ERR("More than existing %d module-types specified, rest ignored", MODULE_LOADORDER_NTYPES);
167 break;
170 switch(*cptr)
172 case 'N': /* Native */
173 case 'n': type = MODULE_LOADORDER_DLL; break;
175 case 'E': /* Elfdll */
176 case 'e':
177 if (!warn++) MESSAGE("Load order 'elfdll' no longer supported, ignored\n");
178 break;
179 case 'S': /* So */
180 case 's': type = MODULE_LOADORDER_SO; break;
182 case 'B': /* Builtin */
183 case 'b': type = MODULE_LOADORDER_BI; break;
185 default:
186 ERR("Invalid load order module-type '%s', ignored\n", cptr);
189 if(type != MODULE_LOADORDER_INVALID)
191 mlo->loadorder[n++] = type;
193 cptr = get_tok(NULL, ", \t");
195 return TRUE;
199 /***************************************************************************
200 * AddLoadOrder (internal, static)
202 * Adds an entry in the list of overrides. If the entry exists, then the
203 * override parameter determines whether it will be overwritten.
205 static BOOL AddLoadOrder(module_loadorder_t *plo, BOOL override)
207 int i;
209 /* TRACE(module, "'%s' -> %08lx\n", plo->modulename, *(DWORD *)(plo->loadorder)); */
211 for(i = 0; i < nmodule_loadorder; i++)
213 if(!cmp_sort_func(plo, &module_loadorder[i]))
215 if(!override)
216 ERR("Module '%s' is already in the list of overrides, using first definition\n", plo->modulename);
217 else
218 memcpy(module_loadorder[i].loadorder, plo->loadorder, sizeof(plo->loadorder));
219 return TRUE;
223 if(nmodule_loadorder >= nmodule_loadorder_alloc)
225 /* No space in current array, make it larger */
226 nmodule_loadorder_alloc += LOADORDER_ALLOC_CLUSTER;
227 module_loadorder = (module_loadorder_t *)HeapReAlloc(GetProcessHeap(),
229 module_loadorder,
230 nmodule_loadorder_alloc * sizeof(module_loadorder_t));
231 if(!module_loadorder)
233 MESSAGE("Virtual memory exhausted\n");
234 exit(1);
237 memcpy(module_loadorder[nmodule_loadorder].loadorder, plo->loadorder, sizeof(plo->loadorder));
238 module_loadorder[nmodule_loadorder].modulename = HEAP_strdupA(GetProcessHeap(), 0, plo->modulename);
239 nmodule_loadorder++;
240 return TRUE;
244 /***************************************************************************
245 * AddLoadOrderSet (internal, static)
247 * Adds a set of entries in the list of overrides from the key parameter.
248 * If the entry exists, then the override parameter determines whether it
249 * will be overwritten.
251 static BOOL AddLoadOrderSet(char *key, char *order, BOOL override)
253 module_loadorder_t ldo;
254 char *cptr;
256 /* Parse the loadorder before the rest because strtok is not reentrant */
257 if(!ParseLoadOrder(order, &ldo))
258 return FALSE;
260 cptr = get_tok(key, ", \t");
261 while(cptr)
263 char *ext = strrchr(cptr, '.');
264 if(ext)
266 if(strlen(ext) == 4 &&
267 (!FILE_strcasecmp(ext, ".dll") || !FILE_strcasecmp(ext, ".exe")))
268 MESSAGE("Warning: Loadorder override '%s' contains an extension and might not be found during lookup\n", cptr);
271 ldo.modulename = cptr;
272 if(!AddLoadOrder(&ldo, override))
273 return FALSE;
274 cptr = get_tok(NULL, ", \t");
276 return TRUE;
280 /***************************************************************************
281 * ParseCommandlineOverrides (internal, static)
283 * The commandline is in the form:
284 * name[,name,...]=native[,b,...][+...]
286 static BOOL ParseCommandlineOverrides(void)
288 char *cpy;
289 char *key;
290 char *next;
291 char *value;
292 BOOL retval = TRUE;
294 if(!Options.dllFlags)
295 return TRUE;
297 cpy = HEAP_strdupA(GetProcessHeap(), 0, Options.dllFlags);
298 key = cpy;
299 next = key;
300 for(; next; key = next)
302 next = strchr(key, '+');
303 if(next)
305 *next = '\0';
306 next++;
308 value = strchr(key, '=');
309 if(!value)
311 retval = FALSE;
312 goto endit;
314 *value = '\0';
315 value++;
317 TRACE("Commandline override '%s' = '%s'\n", key, value);
319 if(!AddLoadOrderSet(key, value, TRUE))
321 retval = FALSE;
322 goto endit;
325 endit:
326 HeapFree(GetProcessHeap(), 0, cpy);
327 return retval;;
331 /***************************************************************************
332 * MODULE_InitLoadOrder (internal)
334 * Initialize the load order from the wine.conf file.
335 * The section has the following format:
336 * Section:
337 * [DllDefaults]
339 * Keys:
340 * DefaultLoadOrder=native,so,builtin
341 * A comma separated list of module types to try to load in that specific
342 * order. The DefaultLoadOrder key is used as a fallback when a module is
343 * not specified explicitly. If the DefaultLoadOrder key is not found,
344 * then the order "dll,so,bi" is used
345 * The possible module types are:
346 * - native Native windows dll files
347 * - so Native .so libraries mapped to dlls
348 * - builtin Built-in modules
350 * Case is not important and only the first letter of each type is enough to
351 * identify the type n[ative], s[o], b[uiltin]. Also whitespace is
352 * ignored.
353 * E.g.:
354 * n,s , b
355 * is equal to:
356 * native,so,builtin
358 * Section:
359 * [DllOverrides]
361 * Keys:
362 * There are no explicit keys defined other than module/library names. A comma
363 * separated list of modules is followed by an assignment of the load-order
364 * for these specific modules. See above for possible types. You should not
365 * specify an extension.
366 * Examples:
367 * kernel32, gdi32, user32 = builtin
368 * kernel, gdi, user = builtin
369 * comdlg32 = native, builtin
370 * commdlg = native, builtin
371 * version, ver = native, builtin
375 #define BUFFERSIZE 1024
377 BOOL MODULE_InitLoadOrder(void)
379 char buffer[BUFFERSIZE];
380 char key[256];
381 int nbuffer;
382 int idx;
383 const struct tagDllPair *dllpair;
385 /* Get the default load order */
386 nbuffer = PROFILE_GetWineIniString("DllDefaults", "DefaultLoadOrder", "n,b,s", buffer, sizeof(buffer));
387 if(!nbuffer)
389 MESSAGE("MODULE_InitLoadOrder: mysteriously read nothing from default loadorder\n");
390 return FALSE;
393 TRACE("Setting default loadorder=%s\n", buffer);
395 if(!ParseLoadOrder(buffer, &default_loadorder))
396 return FALSE;
397 default_loadorder.modulename = "<none>";
400 int i;
401 for (i=0;DefaultDllOverrides[i].key;i++)
402 AddLoadOrderSet(
403 DefaultDllOverrides[i].key,
404 DefaultDllOverrides[i].value,
405 FALSE
409 /* Read the explicitely defined orders for specific modules as an entire section */
410 idx = 0;
411 while (PROFILE_EnumWineIniString( "DllOverrides", idx++, key, sizeof(key),
412 buffer, sizeof(buffer)))
414 TRACE("Key '%s' uses override '%s'\n", key, buffer);
415 if(!AddLoadOrderSet(key, buffer, TRUE))
416 return FALSE;
419 /* Add the commandline overrides to the pool */
420 if(!ParseCommandlineOverrides())
422 MESSAGE( "Syntax: -dll name[,name[,...]]={native|so|builtin}[,{n|s|b}[,...]][+...]\n"
423 " - 'name' is the name of any dll without extension\n"
424 " - the order of loading (native, so and builtin) can be abbreviated\n"
425 " with the first letter\n"
426 " - different loadorders for different dlls can be specified by seperating the\n"
427 " commandline entries with a '+'\n"
428 " Example:\n"
429 " -dll comdlg32,commdlg=n+shell,shell32=b\n"
431 return FALSE;
434 /* Sort the array for quick lookup */
435 qsort(module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
437 /* Check the pairs of dlls */
438 dllpair = DllPairs;
439 while (dllpair->dll1)
441 module_loadorder_t *plo1, *plo2;
442 plo1 = MODULE_GetLoadOrder(dllpair->dll1, FALSE);
443 plo2 = MODULE_GetLoadOrder(dllpair->dll2, FALSE);
444 assert(plo1 && plo2);
445 if(memcmp(plo1->loadorder, plo2->loadorder, sizeof(plo1->loadorder)))
446 MESSAGE("Warning: Modules '%s' and '%s' have different loadorder which may cause trouble\n", dllpair->dll1, dllpair->dll2);
447 dllpair++;
450 if(TRACE_ON(module))
452 int i, j;
453 static char types[] = "-NSB";
455 for(i = 0; i < nmodule_loadorder; i++)
457 DPRINTF("%3d: %-12s:", i, module_loadorder[i].modulename);
458 for(j = 0; j < MODULE_LOADORDER_NTYPES; j++)
459 DPRINTF(" %c", types[module_loadorder[i].loadorder[j] % (MODULE_LOADORDER_NTYPES+1)]);
460 DPRINTF("\n");
464 return TRUE;
468 /***************************************************************************
469 * MODULE_GetLoadOrder (internal)
471 * Locate the loadorder of a module.
472 * Any path is stripped from the path-argument and so are the extension
473 * '.dll' and '.exe'. A lookup in the table can yield an override for
474 * the specific dll. Otherwise the default load order is returned.
476 module_loadorder_t *MODULE_GetLoadOrder(const char *path, BOOL win32 )
478 module_loadorder_t lo, *tmp;
479 char fname[256];
480 char sysdir[MAX_PATH+1];
481 char *cptr;
482 char *name;
483 int len;
485 TRACE("looking for %s\n", path);
487 assert(path != NULL);
489 if ( ! GetSystemDirectoryA ( sysdir, MAX_PATH ) )
490 return &default_loadorder; /* Hmmm ... */
492 /* Strip path information for 16 bit modules or if the module
493 resides in the system directory */
494 if ( !win32 || !FILE_strncasecmp ( sysdir, path, strlen (sysdir) ) )
497 cptr = strrchr(path, '\\');
498 if(!cptr)
499 name = strrchr(path, '/');
500 else
501 name = strrchr(cptr, '/');
503 if(!name)
504 name = cptr ? cptr+1 : (char *)path;
505 else
506 name++;
508 if((cptr = strchr(name, ':')) != NULL) /* Also strip drive if in format 'C:MODULE.DLL' */
509 name = cptr+1;
511 else
512 name = (char *)path;
514 len = strlen(name);
515 if(len >= sizeof(fname) || len <= 0)
517 ERR("Path '%s' -> '%s' reduces to zilch or just too large...\n", path, name);
518 return &default_loadorder;
521 strcpy(fname, name);
522 if(len >= 4 && (!FILE_strcasecmp(fname+len-4, ".dll") || !FILE_strcasecmp(fname+len-4, ".exe")))
523 fname[len-4] = '\0';
525 lo.modulename = fname;
526 tmp = bsearch(&lo, module_loadorder, nmodule_loadorder, sizeof(module_loadorder[0]), cmp_sort_func);
528 TRACE("Looking for '%s' (%s), found '%s'\n", path, fname, tmp ? tmp->modulename : "<nothing>");
530 if(!tmp)
531 return &default_loadorder;
532 return tmp;