Release 970804
[wine/multimedia.git] / if1632 / builtin.c
blobebe48adb275b591fc78aea409459f4e1c9d92c00
1 /*
2 * Built-in modules
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <ctype.h>
9 #include <string.h>
10 #include "windows.h"
11 #include "gdi.h"
12 #include "global.h"
13 #include "module.h"
14 #include "miscemu.h"
15 #include "neexe.h"
16 #include "stackframe.h"
17 #include "user.h"
18 #include "stddebug.h"
19 #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 { &SYSTEM_Descriptor, DLL_FLAG_ALWAYS_USED },
123 { &WIN87EM_Descriptor, DLL_FLAG_NOT_USED },
124 { &SHELL_Descriptor, 0 },
125 { &SOUND_Descriptor, 0 },
126 { &KEYBOARD_Descriptor, 0 },
127 { &WINSOCK_Descriptor, 0 },
128 { &STRESS_Descriptor, 0 },
129 { &MMSYSTEM_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, DLL_FLAG_NOT_USED },
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 }
169 /* Ordinal number for interrupt 0 handler in WPROCS.DLL */
170 #define FIRST_INTERRUPT_ORDINAL 100
172 /***********************************************************************
173 * BUILTIN_Init
175 * Load all built-in modules marked as 'always used'.
177 BOOL16 BUILTIN_Init(void)
179 BUILTIN_DLL *dll;
180 NE_MODULE *pModule;
181 WORD vector;
182 HMODULE16 hModule;
184 for (dll = BuiltinDLLs; dll->descr; dll++)
185 if (dll->flags & DLL_FLAG_ALWAYS_USED)
186 if (!BUILTIN_LoadModule(dll->descr->name, TRUE)) return FALSE;
188 /* Set the USER and GDI heap selectors */
190 pModule = MODULE_GetPtr( GetModuleHandle16( "USER" ));
191 USER_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
192 pModule = MODULE_GetPtr( GetModuleHandle16( "GDI" ));
193 GDI_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
195 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
197 hModule = GetModuleHandle16( "KERNEL" );
198 MODULE_SetEntryPoint( hModule, 178, GetWinFlags() );
200 /* Initialize the real-mode selector entry points */
202 DOSMEM_InitExports( hModule );
204 /* Set interrupt vectors from entry points in WPROCS.DLL */
206 hModule = GetModuleHandle16( "WPROCS" );
207 for (vector = 0; vector < 256; vector++)
209 FARPROC16 proc = MODULE_GetEntryPoint( hModule,
210 FIRST_INTERRUPT_ORDINAL+vector);
211 assert(proc);
212 INT_SetHandler( vector, proc );
215 return TRUE;
219 /***********************************************************************
220 * BUILTIN_LoadModule
222 * Load a built-in module. If the 'force' parameter is FALSE, we only
223 * load the module if it has not been disabled via the -dll option.
225 HMODULE16 BUILTIN_LoadModule( LPCSTR name, BOOL16 force )
227 HMODULE16 hModule;
228 NE_MODULE *pModule;
229 BUILTIN_DLL *table;
230 char dllname[16], *p;
232 /* Fix the name in case we have a full path and extension */
234 if ((p = strrchr( name, '\\' ))) name = p + 1;
235 lstrcpyn32A( dllname, name, sizeof(dllname) );
236 if ((p = strrchr( dllname, '.' ))) *p = '\0';
238 for (table = BuiltinDLLs; table->descr; table++)
239 if (!lstrcmpi32A( table->descr->name, dllname )) break;
240 if (!table->descr) return 0;
241 if ((table->flags & DLL_FLAG_NOT_USED) && !force) return 0;
243 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, table->descr->module_start,
244 table->descr->module_size, 0,
245 FALSE, FALSE, FALSE, NULL );
246 if (!hModule) return 0;
247 FarSetOwner( hModule, hModule );
249 dprintf_module( stddeb, "Built-in %s: hmodule=%04x\n",
250 table->descr->name, hModule );
251 pModule = (NE_MODULE *)GlobalLock16( hModule );
252 pModule->self = hModule;
254 if (pModule->flags & NE_FFLAGS_WIN32)
256 pModule->pe_module = (PE_MODULE *)table;
257 table->flags |= DLL_FLAG_WIN32;
259 else /* Win16 module */
261 const WIN16_DESCRIPTOR *descr = &table->descr->u.win16;
262 int minsize;
264 /* Allocate the code segment */
266 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
267 pSegTable->selector = GLOBAL_CreateBlock(GMEM_FIXED, descr->code_start,
268 pSegTable->minsize, hModule,
269 TRUE, TRUE, FALSE, NULL );
270 if (!pSegTable->selector) return 0;
271 pSegTable++;
273 /* Allocate the data segment */
275 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
276 minsize += pModule->heap_size;
277 if (minsize > 0x10000) minsize = 0x10000;
278 pSegTable->selector = GLOBAL_Alloc( GMEM_FIXED, minsize,
279 hModule, FALSE, FALSE, FALSE );
280 if (!pSegTable->selector) return 0;
281 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->selector ),
282 descr->data_start, pSegTable->minsize);
283 if (pModule->heap_size)
284 LocalInit( pSegTable->selector, pSegTable->minsize, minsize );
287 MODULE_RegisterModule( pModule );
288 return hModule;
292 /***********************************************************************
293 * BUILTIN_GetEntryPoint16
295 * Return the ordinal and name corresponding to a CS:IP address.
296 * This is used only by relay debugging.
298 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
300 static char buffer[80];
301 WORD ordinal, i, max_offset;
302 register BYTE *p;
303 NE_MODULE *pModule;
305 if (!(pModule = MODULE_GetPtr( FarGetOwner( GlobalHandle16(cs) ))))
306 return NULL;
308 /* Search for the ordinal */
310 p = (BYTE *)pModule + pModule->entry_table;
311 max_offset = 0;
312 ordinal = 1;
313 *pOrd = 0;
314 while (*p)
316 switch(p[1])
318 case 0: /* unused */
319 ordinal += *p;
320 p += 2;
321 break;
322 case 1: /* code segment */
323 i = *p;
324 p += 2;
325 while (i-- > 0)
327 p++;
328 if ((*(WORD *)p <= ip) && (*(WORD *)p >= max_offset))
330 max_offset = *(WORD *)p;
331 *pOrd = ordinal;
333 p += 2;
334 ordinal++;
336 break;
337 case 0xff: /* moveable (should not happen in built-in modules) */
338 fprintf( stderr, "Built-in module has moveable entry\n" );
339 ordinal += *p;
340 p += 2 + *p * 6;
341 break;
342 default: /* other segment */
343 ordinal += *p;
344 p += 2 + *p * 3;
345 break;
349 /* Search for the name in the resident names table */
350 /* (built-in modules have no non-resident table) */
352 p = (BYTE *)pModule + pModule->name_table;
353 while (*p)
355 p += *p + 1 + sizeof(WORD);
356 if (*(WORD *)(p + *p + 1) == *pOrd) break;
359 sprintf( buffer, "%.*s.%d: %.*s",
360 *((BYTE *)pModule + pModule->name_table),
361 (char *)pModule + pModule->name_table + 1,
362 *pOrd, *p, (char *)(p + 1) );
363 return buffer;
367 /***********************************************************************
368 * BUILTIN_GetEntryPoint32
370 * Return the name of the DLL entry point corresponding
371 * to a relay entry point address. This is used only by relay debugging.
373 LPCSTR BUILTIN_GetEntryPoint32( void *relay )
375 static char buffer[80];
376 BUILTIN_DLL *dll;
377 const void **funcs;
378 int i;
380 /* First find the module */
382 for (dll = BuiltinDLLs; dll->descr; dll++)
383 if ((dll->flags & DLL_FLAG_WIN32) &&
384 (dll->descr->u.win32.code_start <= relay) &&
385 ((void *)dll->descr->u.win32.functions > relay))
386 break;
387 if (!dll->descr)
389 sprintf( buffer, "???.???: %08x", (UINT32)relay );
390 return buffer;
393 /* Now find the function */
395 relay = (BYTE *)relay - 11; /* The relay entry point is 11 bytes long */
396 funcs = dll->descr->u.win32.functions;
397 for (i = 0; i < dll->descr->u.win32.size;i++) if (*funcs++ == relay) break;
398 sprintf( buffer, "%s.%d: %s",
399 dll->descr->name, i, dll->descr->u.win32.names[i] );
400 return buffer;
404 /***********************************************************************
405 * BUILTIN_GetProcAddress32
407 * Implementation of GetProcAddress() for built-in Win32 modules.
408 * FIXME: this should be unified with the real GetProcAddress32().
410 FARPROC32 BUILTIN_GetProcAddress32( NE_MODULE *pModule, LPCSTR function )
412 BUILTIN_DLL *dll = (BUILTIN_DLL *)pModule->pe_module;
413 const WIN32_DESCRIPTOR *info = &dll->descr->u.win32;
415 if (!dll) return NULL;
417 if (HIWORD(function)) /* Find function by name */
419 int i;
421 dprintf_module( stddeb, "Looking for function %s in %s\n",
422 function, dll->descr->name );
423 for (i = 0; i < info->size; i++)
424 if (info->names[i] && !strcmp( function, info->names[i] ))
425 return (FARPROC32)info->functions[i];
427 else /* Find function by ordinal */
429 WORD ordinal = LOWORD(function);
430 dprintf_module( stddeb, "Looking for ordinal %d in %s\n",
431 ordinal, dll->descr->name );
432 if (ordinal && ordinal < info->size)
433 return (FARPROC32)info->functions[ordinal - info->base];
435 return NULL;
439 /**********************************************************************
440 * BUILTIN_DefaultIntHandler
442 * Default interrupt handler.
444 void BUILTIN_DefaultIntHandler( CONTEXT *context )
446 WORD ordinal;
447 STACK16FRAME *frame = CURRENT_STACK16;
448 BUILTIN_GetEntryPoint16( frame->entry_cs, frame->entry_ip, &ordinal );
449 INT_BARF( context, ordinal - FIRST_INTERRUPT_ORDINAL );
453 /***********************************************************************
454 * BUILTIN_ParseDLLOptions
456 * Set runtime DLL usage flags
458 BOOL16 BUILTIN_ParseDLLOptions( const char *str )
460 BUILTIN_DLL *dll;
461 const char *p;
463 while (*str)
465 while (*str && isspace(*str)) str++;
466 if (!*str) return TRUE;
467 if ((*str != '+') && (*str != '-')) return FALSE;
468 str++;
469 if (!(p = strchr( str, ',' ))) p = str + strlen(str);
470 while ((p > str) && isspace(p[-1])) p--;
471 if (p == str) return FALSE;
472 for (dll = BuiltinDLLs; dll->descr; dll++)
474 if (!lstrncmpi32A( str, dll->descr->name, (int)(p - str) ))
476 if (str[-1] == '-')
478 if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
479 dll->flags |= DLL_FLAG_NOT_USED;
481 else dll->flags &= ~DLL_FLAG_NOT_USED;
482 break;
485 if (!dll->descr) return FALSE;
486 str = p;
487 while (*str && (isspace(*str) || (*str == ','))) str++;
489 return TRUE;
493 /***********************************************************************
494 * BUILTIN_PrintDLLs
496 * Print the list of built-in DLLs that can be disabled.
498 void BUILTIN_PrintDLLs(void)
500 int i;
501 BUILTIN_DLL *dll;
503 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
505 if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
506 fprintf( stderr, "%-9s%c", dll->descr->name,
507 ((++i) % 8) ? ' ' : '\n' );
509 fprintf(stderr,"\n");
510 exit(1);