Release 980726
[wine/multimedia.git] / if1632 / builtin.c
blobc20ca16ce9ce0bf849933193da2754b207ae0839
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 "builtin32.h"
12 #include "gdi.h"
13 #include "global.h"
14 #include "heap.h"
15 #include "module.h"
16 #include "miscemu.h"
17 #include "neexe.h"
18 #include "stackframe.h"
19 #include "user.h"
20 #include "process.h"
21 #include "snoop.h"
22 #include "task.h"
23 #include "debug.h"
25 /* Built-in modules descriptors */
26 /* Don't change these structures! (see tools/build.c) */
28 typedef struct
30 const char *name; /* DLL name */
31 void *module_start; /* 32-bit address of the module data */
32 int module_size; /* Size of the module data */
33 const BYTE *code_start; /* 32-bit address of DLL code */
34 const BYTE *data_start; /* 32-bit address of DLL data */
35 } WIN16_DESCRIPTOR;
37 typedef struct
39 const WIN16_DESCRIPTOR *descr; /* DLL descriptor */
40 int flags; /* flags (see below) */
41 } BUILTIN16_DLL;
43 /* DLL flags */
44 #define DLL_FLAG_NOT_USED 0x01 /* Use original Windows DLL if possible */
45 #define DLL_FLAG_ALWAYS_USED 0x02 /* Always use built-in DLL */
47 /* 16-bit DLLs */
49 extern const WIN16_DESCRIPTOR COMMDLG_Descriptor;
50 extern const WIN16_DESCRIPTOR COMPOBJ_Descriptor;
51 extern const WIN16_DESCRIPTOR DDEML_Descriptor;
52 extern const WIN16_DESCRIPTOR GDI_Descriptor;
53 extern const WIN16_DESCRIPTOR KERNEL_Descriptor;
54 extern const WIN16_DESCRIPTOR KEYBOARD_Descriptor;
55 extern const WIN16_DESCRIPTOR LZEXPAND_Descriptor;
56 extern const WIN16_DESCRIPTOR MMSYSTEM_Descriptor;
57 extern const WIN16_DESCRIPTOR MOUSE_Descriptor;
58 extern const WIN16_DESCRIPTOR MSVIDEO_Descriptor;
59 extern const WIN16_DESCRIPTOR OLE2CONV_Descriptor;
60 extern const WIN16_DESCRIPTOR OLE2DISP_Descriptor;
61 extern const WIN16_DESCRIPTOR OLE2NLS_Descriptor;
62 extern const WIN16_DESCRIPTOR OLE2PROX_Descriptor;
63 extern const WIN16_DESCRIPTOR OLE2THK_Descriptor;
64 extern const WIN16_DESCRIPTOR OLE2_Descriptor;
65 extern const WIN16_DESCRIPTOR OLECLI_Descriptor;
66 extern const WIN16_DESCRIPTOR OLESVR_Descriptor;
67 extern const WIN16_DESCRIPTOR RASAPI16_Descriptor;
68 extern const WIN16_DESCRIPTOR SHELL_Descriptor;
69 extern const WIN16_DESCRIPTOR SOUND_Descriptor;
70 extern const WIN16_DESCRIPTOR STORAGE_Descriptor;
71 extern const WIN16_DESCRIPTOR STRESS_Descriptor;
72 extern const WIN16_DESCRIPTOR SYSTEM_Descriptor;
73 extern const WIN16_DESCRIPTOR TOOLHELP_Descriptor;
74 extern const WIN16_DESCRIPTOR TYPELIB_Descriptor;
75 extern const WIN16_DESCRIPTOR USER_Descriptor;
76 extern const WIN16_DESCRIPTOR VER_Descriptor;
77 extern const WIN16_DESCRIPTOR W32SYS_Descriptor;
78 extern const WIN16_DESCRIPTOR WIN32S16_Descriptor;
79 extern const WIN16_DESCRIPTOR WIN87EM_Descriptor;
80 extern const WIN16_DESCRIPTOR WINASPI_Descriptor;
81 extern const WIN16_DESCRIPTOR WINDEBUG_Descriptor;
82 extern const WIN16_DESCRIPTOR WINEPS_Descriptor;
83 extern const WIN16_DESCRIPTOR WING_Descriptor;
84 extern const WIN16_DESCRIPTOR WINSOCK_Descriptor;
85 extern const WIN16_DESCRIPTOR WPROCS_Descriptor;
87 /* Table of all built-in DLLs */
89 static BUILTIN16_DLL BuiltinDLLs[] =
91 { &KERNEL_Descriptor, DLL_FLAG_ALWAYS_USED },
92 { &USER_Descriptor, DLL_FLAG_ALWAYS_USED },
93 { &GDI_Descriptor, DLL_FLAG_ALWAYS_USED },
94 { &SYSTEM_Descriptor, DLL_FLAG_ALWAYS_USED },
95 { &WPROCS_Descriptor, DLL_FLAG_ALWAYS_USED },
96 { &WINDEBUG_Descriptor, DLL_FLAG_NOT_USED },
97 { &COMMDLG_Descriptor, DLL_FLAG_NOT_USED },
98 { &COMPOBJ_Descriptor, DLL_FLAG_NOT_USED },
99 { &DDEML_Descriptor, DLL_FLAG_NOT_USED },
100 { &KEYBOARD_Descriptor, 0 },
101 { &LZEXPAND_Descriptor, 0 },
102 { &MMSYSTEM_Descriptor, 0 },
103 { &MOUSE_Descriptor, 0 },
104 { &MSVIDEO_Descriptor, 0 },
105 { &OLE2CONV_Descriptor, DLL_FLAG_NOT_USED },
106 { &OLE2DISP_Descriptor, DLL_FLAG_NOT_USED },
107 { &OLE2NLS_Descriptor, DLL_FLAG_NOT_USED },
108 { &OLE2PROX_Descriptor, DLL_FLAG_NOT_USED },
109 { &OLE2THK_Descriptor, DLL_FLAG_NOT_USED },
110 { &OLE2_Descriptor, DLL_FLAG_NOT_USED },
111 { &OLECLI_Descriptor, DLL_FLAG_NOT_USED },
112 { &OLESVR_Descriptor, DLL_FLAG_NOT_USED },
113 { &RASAPI16_Descriptor, 0 },
114 { &SHELL_Descriptor, 0 },
115 { &SOUND_Descriptor, 0 },
116 { &STORAGE_Descriptor, DLL_FLAG_NOT_USED },
117 { &STRESS_Descriptor, 0 },
118 { &TOOLHELP_Descriptor, 0 },
119 { &TYPELIB_Descriptor, DLL_FLAG_NOT_USED },
120 { &VER_Descriptor, 0 },
121 { &W32SYS_Descriptor, DLL_FLAG_NOT_USED },
122 { &WIN32S16_Descriptor, DLL_FLAG_NOT_USED },
123 { &WIN87EM_Descriptor, DLL_FLAG_NOT_USED },
124 { &WINASPI_Descriptor, 0 },
125 { &WINEPS_Descriptor, DLL_FLAG_ALWAYS_USED },
126 { &WING_Descriptor, 0 },
127 { &WINSOCK_Descriptor, 0 },
128 /* Last entry */
129 { NULL, 0 }
132 /* Ordinal number for interrupt 0 handler in WPROCS.DLL */
133 #define FIRST_INTERRUPT_ORDINAL 100
136 /***********************************************************************
137 * BUILTIN_DoLoadModule16
139 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule
140 * and BUILTIN_Init.
142 static HMODULE16 BUILTIN_DoLoadModule16( const WIN16_DESCRIPTOR *descr )
144 NE_MODULE *pModule;
145 int minsize;
146 SEGTABLEENTRY *pSegTable;
148 HMODULE16 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
149 descr->module_size, 0,
150 FALSE, FALSE, FALSE, NULL );
151 if (!hModule) return 0;
152 FarSetOwner( hModule, hModule );
154 TRACE(module, "Built-in %s: hmodule=%04x\n",
155 descr->name, hModule );
156 pModule = (NE_MODULE *)GlobalLock16( hModule );
157 pModule->self = hModule;
159 /* Allocate the code segment */
161 pSegTable = NE_SEG_TABLE( pModule );
162 pSegTable->selector = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
163 pSegTable->minsize, hModule,
164 TRUE, TRUE, FALSE, NULL );
165 if (!pSegTable->selector) return 0;
166 pSegTable++;
168 /* Allocate the data segment */
170 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
171 minsize += pModule->heap_size;
172 if (minsize > 0x10000) minsize = 0x10000;
173 pSegTable->selector = GLOBAL_Alloc( GMEM_FIXED, minsize,
174 hModule, FALSE, FALSE, FALSE );
175 if (!pSegTable->selector) return 0;
176 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->selector ),
177 descr->data_start, pSegTable->minsize);
178 if (pModule->heap_size)
179 LocalInit( pSegTable->selector, pSegTable->minsize, minsize );
181 NE_RegisterModule( pModule );
182 return hModule;
186 /***********************************************************************
187 * BUILTIN_Init
189 * Load all built-in modules marked as 'always used'.
191 BOOL32 BUILTIN_Init(void)
193 BUILTIN16_DLL *dll;
194 NE_MODULE *pModule;
195 WORD vector;
196 HMODULE16 hModule;
197 WORD cs, ds;
199 fnBUILTIN_LoadModule = BUILTIN_LoadModule;
201 for (dll = BuiltinDLLs; dll->descr; dll++)
203 if (dll->flags & DLL_FLAG_ALWAYS_USED)
204 if (!BUILTIN_DoLoadModule16( dll->descr )) return FALSE;
207 /* Set the USER and GDI heap selectors */
209 pModule = NE_GetPtr( GetModuleHandle16( "USER" ));
210 USER_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
211 pModule = NE_GetPtr( GetModuleHandle16( "GDI" ));
212 GDI_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
214 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
216 hModule = GetModuleHandle16( "KERNEL" );
217 NE_SetEntryPoint( hModule, 178, GetWinFlags() );
219 /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
221 GET_CS(cs); GET_DS(ds);
222 NE_SetEntryPoint( hModule, 454, cs );
223 NE_SetEntryPoint( hModule, 455, ds );
225 /* Initialize KERNEL.THHOOK */
227 TASK_InstallTHHook((THHOOK *)PTR_SEG_TO_LIN(
228 (SEGPTR)NE_GetEntryPoint( hModule, 332 )));
230 /* Initialize the real-mode selector entry points */
232 #define SET_ENTRY_POINT( num, addr ) \
233 NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
234 DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
235 FALSE, FALSE, FALSE, NULL ))
237 SET_ENTRY_POINT( 183, 0x00000 ); /* KERNEL.183: __0000H */
238 SET_ENTRY_POINT( 174, 0xa0000 ); /* KERNEL.174: __A000H */
239 SET_ENTRY_POINT( 181, 0xb0000 ); /* KERNEL.181: __B000H */
240 SET_ENTRY_POINT( 182, 0xb8000 ); /* KERNEL.182: __B800H */
241 SET_ENTRY_POINT( 195, 0xc0000 ); /* KERNEL.195: __C000H */
242 SET_ENTRY_POINT( 179, 0xd0000 ); /* KERNEL.179: __D000H */
243 SET_ENTRY_POINT( 190, 0xe0000 ); /* KERNEL.190: __E000H */
244 SET_ENTRY_POINT( 173, 0xf0000 ); /* KERNEL.173: __ROMBIOS */
245 SET_ENTRY_POINT( 194, 0xf0000 ); /* KERNEL.194: __F000H */
246 NE_SetEntryPoint( hModule, 193, DOSMEM_BiosSeg ); /* KERNEL.193: __0040H */
247 #undef SET_ENTRY_POINT
249 /* Set interrupt vectors from entry points in WPROCS.DLL */
251 hModule = GetModuleHandle16( "WPROCS" );
252 for (vector = 0; vector < 256; vector++)
254 FARPROC16 proc = NE_GetEntryPoint( hModule,
255 FIRST_INTERRUPT_ORDINAL + vector );
256 assert(proc);
257 INT_SetHandler( vector, proc );
260 SNOOP16_Init();
262 return TRUE;
266 /***********************************************************************
267 * BUILTIN_LoadModule
269 * Load a built-in module. If the 'force' parameter is FALSE, we only
270 * load the module if it has not been disabled via the -dll option.
272 HMODULE16 BUILTIN_LoadModule( LPCSTR name, BOOL32 force )
274 BUILTIN16_DLL *table;
275 char dllname[16], *p;
277 /* Fix the name in case we have a full path and extension */
279 if ((p = strrchr( name, '\\' ))) name = p + 1;
280 lstrcpyn32A( dllname, name, sizeof(dllname) );
281 if ((p = strrchr( dllname, '.' ))) *p = '\0';
283 for (table = BuiltinDLLs; table->descr; table++)
284 if (!lstrcmpi32A( table->descr->name, dllname )) break;
285 if (!table->descr) return 0;
286 if ((table->flags & DLL_FLAG_NOT_USED) && !force) return 0;
288 return BUILTIN_DoLoadModule16( table->descr );
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 = NE_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 TRACE( relay, "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_DefaultIntHandler
370 * Default interrupt handler.
372 void BUILTIN_DefaultIntHandler( CONTEXT *context )
374 WORD ordinal;
375 STACK16FRAME *frame = CURRENT_STACK16;
376 BUILTIN_GetEntryPoint16( frame->entry_cs, frame->entry_ip, &ordinal );
377 INT_BARF( context, ordinal - FIRST_INTERRUPT_ORDINAL );
381 /***********************************************************************
382 * BUILTIN_ParseDLLOptions
384 * Set runtime DLL usage flags
386 BOOL32 BUILTIN_ParseDLLOptions( const char *str )
388 BUILTIN16_DLL *dll;
389 const char *p;
391 while (*str)
393 while (*str && isspace(*str)) str++;
394 if (!*str) return TRUE;
395 if ((*str != '+') && (*str != '-')) return FALSE;
396 str++;
397 if (!(p = strchr( str, ',' ))) p = str + strlen(str);
398 while ((p > str) && isspace(p[-1])) p--;
399 if (p == str) return FALSE;
400 for (dll = BuiltinDLLs; dll->descr; dll++)
402 if (!lstrncmpi32A( str, dll->descr->name, (int)(p - str) ))
404 if (dll->descr->name[(int)(p-str)]) /* only partial match */
405 continue;
406 if (str[-1] == '-')
408 if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
409 dll->flags |= DLL_FLAG_NOT_USED;
411 else dll->flags &= ~DLL_FLAG_NOT_USED;
412 break;
415 if (!dll->descr)
416 if (!BUILTIN32_EnableDLL( str, (int)(p - str), (str[-1] == '+') ))
417 return FALSE;
418 str = p;
419 while (*str && (isspace(*str) || (*str == ','))) str++;
421 return TRUE;
425 /***********************************************************************
426 * BUILTIN_PrintDLLs
428 * Print the list of built-in DLLs that can be disabled.
430 void BUILTIN_PrintDLLs(void)
432 int i;
433 BUILTIN16_DLL *dll;
435 MSG("Example: -dll -ole2 Do not use emulated OLE2.DLL\n");
436 MSG("Available Win16 DLLs:\n");
437 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
439 if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
440 MSG("%-9s%c", dll->descr->name,
441 ((++i) % 8) ? ' ' : '\n' );
443 MSG("\n");
444 BUILTIN32_PrintDLLs();