Release 961222
[wine/gsoc-2012-control.git] / loader / builtin.c
blob0a12bc617acf64130934588e4bf55b5c05f4773e
1 /*
2 * Built-in modules
4 * Copyright 1996 Alexandre Julliard
5 */
7 #ifndef WINELIB
9 #include <ctype.h>
10 #include <string.h>
11 #include "windows.h"
12 #include "gdi.h"
13 #include "global.h"
14 #include "module.h"
15 #include "neexe.h"
16 #include "user.h"
17 #include "stddebug.h"
18 #include "debug.h"
21 /* Built-in modules descriptors */
22 /* Don't change these structures! (see tools/build.c) */
24 typedef struct
26 const BYTE *code_start; /* 32-bit address of DLL code */
27 const BYTE *data_start; /* 32-bit address of DLL data */
28 } WIN16_DESCRIPTOR;
30 typedef struct
32 int base; /* Ordinal base */
33 int size; /* Number of functions */
34 const void *code_start; /* Start of DLL code */
35 const void **functions; /* Pointer to functions table */
36 const char * const *names; /* Pointer to names table */
37 } WIN32_DESCRIPTOR;
39 typedef struct
41 const char *name; /* DLL name */
42 void *module_start; /* 32-bit address of the module data */
43 int module_size; /* Size of the module data */
44 union
46 WIN16_DESCRIPTOR win16; /* Descriptor for Win16 DLL */
47 WIN32_DESCRIPTOR win32; /* Descriptor for Win32 DLL */
48 } u;
49 } DLL_DESCRIPTOR;
51 typedef struct
53 const DLL_DESCRIPTOR *descr; /* DLL descriptor */
54 int flags; /* flags (see below) */
55 } BUILTIN_DLL;
58 /* DLL flags */
59 #define DLL_FLAG_NOT_USED 0x01 /* Use original Windows DLL if possible */
60 #define DLL_FLAG_ALWAYS_USED 0x02 /* Always use built-in DLL */
61 #define DLL_FLAG_WIN32 0x04 /* DLL is a Win32 DLL */
63 /* 16-bit DLLs */
65 extern const DLL_DESCRIPTOR KERNEL_Descriptor;
66 extern const DLL_DESCRIPTOR USER_Descriptor;
67 extern const DLL_DESCRIPTOR GDI_Descriptor;
68 extern const DLL_DESCRIPTOR WIN87EM_Descriptor;
69 extern const DLL_DESCRIPTOR MMSYSTEM_Descriptor;
70 extern const DLL_DESCRIPTOR SHELL_Descriptor;
71 extern const DLL_DESCRIPTOR SOUND_Descriptor;
72 extern const DLL_DESCRIPTOR KEYBOARD_Descriptor;
73 extern const DLL_DESCRIPTOR WINSOCK_Descriptor;
74 extern const DLL_DESCRIPTOR STRESS_Descriptor;
75 extern const DLL_DESCRIPTOR SYSTEM_Descriptor;
76 extern const DLL_DESCRIPTOR TOOLHELP_Descriptor;
77 extern const DLL_DESCRIPTOR MOUSE_Descriptor;
78 extern const DLL_DESCRIPTOR COMMDLG_Descriptor;
79 extern const DLL_DESCRIPTOR OLE2_Descriptor;
80 extern const DLL_DESCRIPTOR OLE2CONV_Descriptor;
81 extern const DLL_DESCRIPTOR OLE2DISP_Descriptor;
82 extern const DLL_DESCRIPTOR OLE2NLS_Descriptor;
83 extern const DLL_DESCRIPTOR OLE2PROX_Descriptor;
84 extern const DLL_DESCRIPTOR OLECLI_Descriptor;
85 extern const DLL_DESCRIPTOR OLESVR_Descriptor;
86 extern const DLL_DESCRIPTOR COMPOBJ_Descriptor;
87 extern const DLL_DESCRIPTOR STORAGE_Descriptor;
88 extern const DLL_DESCRIPTOR WPROCS_Descriptor;
89 extern const DLL_DESCRIPTOR DDEML_Descriptor;
90 extern const DLL_DESCRIPTOR LZEXPAND_Descriptor;
91 extern const DLL_DESCRIPTOR VER_Descriptor;
92 extern const DLL_DESCRIPTOR W32SYS_Descriptor;
93 extern const DLL_DESCRIPTOR WING_Descriptor;
95 /* 32-bit DLLs */
97 extern const DLL_DESCRIPTOR ADVAPI32_Descriptor;
98 extern const DLL_DESCRIPTOR COMCTL32_Descriptor;
99 extern const DLL_DESCRIPTOR COMDLG32_Descriptor;
100 extern const DLL_DESCRIPTOR CRTDLL_Descriptor;
101 extern const DLL_DESCRIPTOR OLE32_Descriptor;
102 extern const DLL_DESCRIPTOR GDI32_Descriptor;
103 extern const DLL_DESCRIPTOR KERNEL32_Descriptor;
104 extern const DLL_DESCRIPTOR LZ32_Descriptor;
105 extern const DLL_DESCRIPTOR MPR_Descriptor;
106 extern const DLL_DESCRIPTOR NTDLL_Descriptor;
107 extern const DLL_DESCRIPTOR SHELL32_Descriptor;
108 extern const DLL_DESCRIPTOR USER32_Descriptor;
109 extern const DLL_DESCRIPTOR VERSION_Descriptor;
110 extern const DLL_DESCRIPTOR WINMM_Descriptor;
111 extern const DLL_DESCRIPTOR WINSPOOL_Descriptor;
112 extern const DLL_DESCRIPTOR WSOCK32_Descriptor;
114 /* Table of all built-in DLLs */
116 static BUILTIN_DLL BuiltinDLLs[] =
118 /* Win16 DLLs */
119 { &KERNEL_Descriptor, DLL_FLAG_ALWAYS_USED },
120 { &USER_Descriptor, DLL_FLAG_ALWAYS_USED },
121 { &GDI_Descriptor, DLL_FLAG_ALWAYS_USED },
122 { &WIN87EM_Descriptor, DLL_FLAG_NOT_USED },
123 { &SHELL_Descriptor, 0 },
124 { &SOUND_Descriptor, 0 },
125 { &KEYBOARD_Descriptor, 0 },
126 { &WINSOCK_Descriptor, 0 },
127 { &STRESS_Descriptor, 0 },
128 { &MMSYSTEM_Descriptor, 0 },
129 { &SYSTEM_Descriptor, 0 },
130 { &TOOLHELP_Descriptor, 0 },
131 { &MOUSE_Descriptor, 0 },
132 { &COMMDLG_Descriptor, DLL_FLAG_NOT_USED },
133 { &OLE2_Descriptor, DLL_FLAG_NOT_USED },
134 { &OLE2CONV_Descriptor, DLL_FLAG_NOT_USED },
135 { &OLE2DISP_Descriptor, DLL_FLAG_NOT_USED },
136 { &OLE2NLS_Descriptor, DLL_FLAG_NOT_USED },
137 { &OLE2PROX_Descriptor, DLL_FLAG_NOT_USED },
138 { &OLECLI_Descriptor, DLL_FLAG_NOT_USED },
139 { &OLESVR_Descriptor, DLL_FLAG_NOT_USED },
140 { &COMPOBJ_Descriptor, DLL_FLAG_NOT_USED },
141 { &STORAGE_Descriptor, DLL_FLAG_NOT_USED },
142 { &WPROCS_Descriptor, DLL_FLAG_ALWAYS_USED },
143 { &DDEML_Descriptor, DLL_FLAG_NOT_USED },
144 { &LZEXPAND_Descriptor, 0 },
145 { &VER_Descriptor, 0 },
146 { &W32SYS_Descriptor, 0 },
147 { &WING_Descriptor, 0 },
148 /* Win32 DLLs */
149 { &ADVAPI32_Descriptor, 0 },
150 { &COMCTL32_Descriptor, DLL_FLAG_NOT_USED },
151 { &COMDLG32_Descriptor, 0 },
152 { &CRTDLL_Descriptor, 0 },
153 { &OLE32_Descriptor, 0 },
154 { &GDI32_Descriptor, 0 },
155 { &KERNEL32_Descriptor, DLL_FLAG_ALWAYS_USED },
156 { &LZ32_Descriptor, 0 },
157 { &MPR_Descriptor, 0 },
158 { &NTDLL_Descriptor, 0 },
159 { &SHELL32_Descriptor, 0 },
160 { &USER32_Descriptor, 0 },
161 { &VERSION_Descriptor, 0 },
162 { &WINMM_Descriptor, 0 },
163 { &WINSPOOL_Descriptor, 0 },
164 { &WSOCK32_Descriptor, 0 },
165 /* Last entry */
166 { NULL, 0 }
170 /***********************************************************************
171 * BUILTIN_Init
173 * Load all built-in modules marked as 'always used'.
175 BOOL16 BUILTIN_Init(void)
177 BUILTIN_DLL *dll;
178 NE_MODULE *pModule;
180 for (dll = BuiltinDLLs; dll->descr; dll++)
181 if (dll->flags & DLL_FLAG_ALWAYS_USED)
182 if (!BUILTIN_LoadModule(dll->descr->name, TRUE)) return FALSE;
184 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
186 MODULE_SetEntryPoint( GetModuleHandle( "KERNEL" ), 178, GetWinFlags() );
188 /* Set the USER and GDI heap selectors */
190 pModule = MODULE_GetPtr( GetModuleHandle( "USER" ));
191 USER_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
192 pModule = MODULE_GetPtr( GetModuleHandle( "GDI" ));
193 GDI_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
195 return TRUE;
199 /***********************************************************************
200 * BUILTIN_LoadModule
202 * Load a built-in module. If the 'force' parameter is FALSE, we only
203 * load the module if it has not been disabled via the -dll option.
205 HMODULE16 BUILTIN_LoadModule( LPCSTR name, BOOL16 force )
207 HMODULE16 hModule;
208 NE_MODULE *pModule;
209 BUILTIN_DLL *table;
210 char dllname[16], *p;
212 /* Fix the name in case we have a full path and extension */
214 if ((p = strrchr( name, '\\' ))) name = p + 1;
215 lstrcpyn32A( dllname, name, sizeof(dllname) );
216 if ((p = strrchr( dllname, '.' ))) *p = '\0';
218 for (table = BuiltinDLLs; table->descr; table++)
219 if (!lstrcmpi32A( table->descr->name, dllname )) break;
220 if (!table->descr) return 0;
221 if ((table->flags & DLL_FLAG_NOT_USED) && !force) return 0;
223 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, table->descr->module_start,
224 table->descr->module_size, 0,
225 FALSE, FALSE, FALSE, NULL );
226 if (!hModule) return 0;
227 FarSetOwner( hModule, hModule );
229 dprintf_module( stddeb, "Built-in %s: hmodule=%04x\n",
230 table->descr->name, hModule );
231 pModule = (NE_MODULE *)GlobalLock16( hModule );
232 pModule->self = hModule;
234 if (pModule->flags & NE_FFLAGS_WIN32)
236 pModule->pe_module = (PE_MODULE *)table;
237 table->flags |= DLL_FLAG_WIN32;
239 else /* Win16 module */
241 const WIN16_DESCRIPTOR *descr = &table->descr->u.win16;
242 int minsize;
244 /* Allocate the code segment */
246 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
247 pSegTable->selector = GLOBAL_CreateBlock(GMEM_FIXED, descr->code_start,
248 pSegTable->minsize, hModule,
249 TRUE, TRUE, FALSE, NULL );
250 if (!pSegTable->selector) return 0;
251 pSegTable++;
253 /* Allocate the data segment */
255 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
256 minsize += pModule->heap_size;
257 if (minsize > 0x10000) minsize = 0x10000;
258 pSegTable->selector = GLOBAL_Alloc( GMEM_FIXED, minsize,
259 hModule, FALSE, FALSE, FALSE );
260 if (!pSegTable->selector) return 0;
261 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->selector ),
262 descr->data_start, pSegTable->minsize);
263 if (pModule->heap_size)
264 LocalInit( pSegTable->selector, pSegTable->minsize, minsize );
267 MODULE_RegisterModule( pModule );
268 return hModule;
272 /***********************************************************************
273 * BUILTIN_GetEntryPoint16
275 * Return the ordinal and name corresponding to a CS:IP address.
276 * This is used only by relay debugging.
278 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
280 static char buffer[80];
281 WORD ordinal, i, max_offset;
282 register BYTE *p;
283 NE_MODULE *pModule;
285 if (!(pModule = MODULE_GetPtr( FarGetOwner( GlobalHandle16(cs) ))))
286 return NULL;
288 /* Search for the ordinal */
290 p = (BYTE *)pModule + pModule->entry_table;
291 max_offset = 0;
292 ordinal = 1;
293 *pOrd = 0;
294 while (*p)
296 switch(p[1])
298 case 0: /* unused */
299 ordinal += *p;
300 p += 2;
301 break;
302 case 1: /* code segment */
303 i = *p;
304 p += 2;
305 while (i-- > 0)
307 p++;
308 if ((*(WORD *)p <= ip) && (*(WORD *)p >= max_offset))
310 max_offset = *(WORD *)p;
311 *pOrd = ordinal;
313 p += 2;
314 ordinal++;
316 break;
317 case 0xff: /* moveable (should not happen in built-in modules) */
318 fprintf( stderr, "Built-in module has moveable entry\n" );
319 ordinal += *p;
320 p += 2 + *p * 6;
321 break;
322 default: /* other segment */
323 ordinal += *p;
324 p += 2 + *p * 3;
325 break;
329 /* Search for the name in the resident names table */
330 /* (built-in modules have no non-resident table) */
332 p = (BYTE *)pModule + pModule->name_table;
333 while (*p)
335 p += *p + 1 + sizeof(WORD);
336 if (*(WORD *)(p + *p + 1) == *pOrd) break;
339 sprintf( buffer, "%.*s.%d: %.*s",
340 *((BYTE *)pModule + pModule->name_table),
341 (char *)pModule + pModule->name_table + 1,
342 *pOrd, *p, (char *)(p + 1) );
343 return buffer;
347 /***********************************************************************
348 * BUILTIN_GetEntryPoint32
350 * Return the name of the DLL entry point corresponding
351 * to a relay entry point address. This is used only by relay debugging.
353 LPCSTR BUILTIN_GetEntryPoint32( void *relay )
355 static char buffer[80];
356 BUILTIN_DLL *dll;
357 const void **funcs;
358 int i;
360 /* First find the module */
362 for (dll = BuiltinDLLs; dll->descr; dll++)
363 if ((dll->flags & DLL_FLAG_WIN32) &&
364 (dll->descr->u.win32.code_start <= relay) &&
365 ((void *)dll->descr->u.win32.functions > relay))
366 break;
367 if (!dll->descr)
369 sprintf( buffer, "???.???: %08x", (UINT32)relay );
370 return buffer;
373 /* Now find the function */
375 relay = (BYTE *)relay - 11; /* The relay entry point is 11 bytes long */
376 funcs = dll->descr->u.win32.functions;
377 for (i = 0; i < dll->descr->u.win32.size;i++) if (*funcs++ == relay) break;
378 sprintf( buffer, "%s.%d: %s",
379 dll->descr->name, i, dll->descr->u.win32.names[i] );
380 return buffer;
384 /***********************************************************************
385 * BUILTIN_GetProcAddress32
387 * Implementation of GetProcAddress() for built-in Win32 modules.
388 * FIXME: this should be unified with the real GetProcAddress32().
390 FARPROC32 BUILTIN_GetProcAddress32( NE_MODULE *pModule, LPCSTR function )
392 BUILTIN_DLL *dll = (BUILTIN_DLL *)pModule->pe_module;
393 const WIN32_DESCRIPTOR *info = &dll->descr->u.win32;
395 if (!dll) return NULL;
397 if (HIWORD(function)) /* Find function by name */
399 int i;
401 dprintf_module( stddeb, "Looking for function %s in %s\n",
402 function, dll->descr->name );
403 for (i = 0; i < info->size; i++)
404 if (info->names[i] && !strcmp( function, info->names[i] ))
405 return (FARPROC32)info->functions[i];
407 else /* Find function by ordinal */
409 WORD ordinal = LOWORD(function);
410 dprintf_module( stddeb, "Looking for ordinal %d in %s\n",
411 ordinal, dll->descr->name );
412 if (ordinal && ordinal < info->size)
413 return (FARPROC32)info->functions[ordinal - info->base];
415 return NULL;
419 /***********************************************************************
420 * BUILTIN_ParseDLLOptions
422 * Set runtime DLL usage flags
424 BOOL16 BUILTIN_ParseDLLOptions( const char *str )
426 BUILTIN_DLL *dll;
427 const char *p;
429 while (*str)
431 while (*str && isspace(*str)) str++;
432 if (!*str) return TRUE;
433 if ((*str != '+') && (*str != '-')) return FALSE;
434 str++;
435 if (!(p = strchr( str, ',' ))) p = str + strlen(str);
436 while ((p > str) && isspace(p[-1])) p--;
437 if (p == str) return FALSE;
438 for (dll = BuiltinDLLs; dll->descr; dll++)
440 if (!lstrncmpi32A( str, dll->descr->name, (int)(p - str) ))
442 if (str[-1] == '-')
444 if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
445 dll->flags |= DLL_FLAG_NOT_USED;
447 else dll->flags &= ~DLL_FLAG_NOT_USED;
448 break;
451 if (!dll->descr) return FALSE;
452 str = p;
453 while (*str && (isspace(*str) || (*str == ','))) str++;
455 return TRUE;
459 /***********************************************************************
460 * BUILTIN_PrintDLLs
462 * Print the list of built-in DLLs that can be disabled.
464 void BUILTIN_PrintDLLs(void)
466 int i;
467 BUILTIN_DLL *dll;
469 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
471 if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
472 fprintf( stderr, "%-9s%c", dll->descr->name,
473 ((++i) % 8) ? ' ' : '\n' );
475 fprintf(stderr,"\n");
476 exit(1);
479 #endif /* WINELIB */