Release 980628
[wine/multimedia.git] / if1632 / builtin.c
blob40c3652dd7ff218db753a83ea46bd5c2ea45cb8a
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 "debug.h"
23 /* Built-in modules descriptors */
24 /* Don't change these structures! (see tools/build.c) */
26 typedef struct
28 const char *name; /* DLL name */
29 void *module_start; /* 32-bit address of the module data */
30 int module_size; /* Size of the module data */
31 const BYTE *code_start; /* 32-bit address of DLL code */
32 const BYTE *data_start; /* 32-bit address of DLL data */
33 } WIN16_DESCRIPTOR;
35 typedef struct
37 const WIN16_DESCRIPTOR *descr; /* DLL descriptor */
38 int flags; /* flags (see below) */
39 } BUILTIN16_DLL;
41 /* DLL flags */
42 #define DLL_FLAG_NOT_USED 0x01 /* Use original Windows DLL if possible */
43 #define DLL_FLAG_ALWAYS_USED 0x02 /* Always use built-in DLL */
45 /* 16-bit DLLs */
47 extern const WIN16_DESCRIPTOR COMMDLG_Descriptor;
48 extern const WIN16_DESCRIPTOR COMPOBJ_Descriptor;
49 extern const WIN16_DESCRIPTOR DDEML_Descriptor;
50 extern const WIN16_DESCRIPTOR GDI_Descriptor;
51 extern const WIN16_DESCRIPTOR KERNEL_Descriptor;
52 extern const WIN16_DESCRIPTOR KEYBOARD_Descriptor;
53 extern const WIN16_DESCRIPTOR LZEXPAND_Descriptor;
54 extern const WIN16_DESCRIPTOR MMSYSTEM_Descriptor;
55 extern const WIN16_DESCRIPTOR MOUSE_Descriptor;
56 extern const WIN16_DESCRIPTOR MSVIDEO_Descriptor;
57 extern const WIN16_DESCRIPTOR OLE2CONV_Descriptor;
58 extern const WIN16_DESCRIPTOR OLE2DISP_Descriptor;
59 extern const WIN16_DESCRIPTOR OLE2NLS_Descriptor;
60 extern const WIN16_DESCRIPTOR OLE2PROX_Descriptor;
61 extern const WIN16_DESCRIPTOR OLE2THK_Descriptor;
62 extern const WIN16_DESCRIPTOR OLE2_Descriptor;
63 extern const WIN16_DESCRIPTOR OLECLI_Descriptor;
64 extern const WIN16_DESCRIPTOR OLESVR_Descriptor;
65 extern const WIN16_DESCRIPTOR SHELL_Descriptor;
66 extern const WIN16_DESCRIPTOR SOUND_Descriptor;
67 extern const WIN16_DESCRIPTOR STORAGE_Descriptor;
68 extern const WIN16_DESCRIPTOR STRESS_Descriptor;
69 extern const WIN16_DESCRIPTOR SYSTEM_Descriptor;
70 extern const WIN16_DESCRIPTOR TOOLHELP_Descriptor;
71 extern const WIN16_DESCRIPTOR TYPELIB_Descriptor;
72 extern const WIN16_DESCRIPTOR USER_Descriptor;
73 extern const WIN16_DESCRIPTOR VER_Descriptor;
74 extern const WIN16_DESCRIPTOR W32SYS_Descriptor;
75 extern const WIN16_DESCRIPTOR WIN32S16_Descriptor;
76 extern const WIN16_DESCRIPTOR WIN87EM_Descriptor;
77 extern const WIN16_DESCRIPTOR WINASPI_Descriptor;
78 extern const WIN16_DESCRIPTOR WINDEBUG_Descriptor;
79 extern const WIN16_DESCRIPTOR WINEPS_Descriptor;
80 extern const WIN16_DESCRIPTOR WING_Descriptor;
81 extern const WIN16_DESCRIPTOR WINSOCK_Descriptor;
82 extern const WIN16_DESCRIPTOR WPROCS_Descriptor;
84 /* Table of all built-in DLLs */
86 static BUILTIN16_DLL BuiltinDLLs[] =
88 { &KERNEL_Descriptor, DLL_FLAG_ALWAYS_USED },
89 { &USER_Descriptor, DLL_FLAG_ALWAYS_USED },
90 { &GDI_Descriptor, DLL_FLAG_ALWAYS_USED },
91 { &SYSTEM_Descriptor, DLL_FLAG_ALWAYS_USED },
92 { &WPROCS_Descriptor, DLL_FLAG_ALWAYS_USED },
93 { &WINDEBUG_Descriptor, DLL_FLAG_NOT_USED },
94 { &COMMDLG_Descriptor, DLL_FLAG_NOT_USED },
95 { &COMPOBJ_Descriptor, DLL_FLAG_NOT_USED },
96 { &DDEML_Descriptor, DLL_FLAG_NOT_USED },
97 { &KEYBOARD_Descriptor, 0 },
98 { &LZEXPAND_Descriptor, 0 },
99 { &MMSYSTEM_Descriptor, 0 },
100 { &MOUSE_Descriptor, 0 },
101 { &MSVIDEO_Descriptor, 0 },
102 { &OLE2CONV_Descriptor, DLL_FLAG_NOT_USED },
103 { &OLE2DISP_Descriptor, DLL_FLAG_NOT_USED },
104 { &OLE2NLS_Descriptor, DLL_FLAG_NOT_USED },
105 { &OLE2PROX_Descriptor, DLL_FLAG_NOT_USED },
106 { &OLE2THK_Descriptor, DLL_FLAG_NOT_USED },
107 { &OLE2_Descriptor, DLL_FLAG_NOT_USED },
108 { &OLECLI_Descriptor, DLL_FLAG_NOT_USED },
109 { &OLESVR_Descriptor, DLL_FLAG_NOT_USED },
110 { &SHELL_Descriptor, 0 },
111 { &SOUND_Descriptor, 0 },
112 { &STORAGE_Descriptor, DLL_FLAG_NOT_USED },
113 { &STRESS_Descriptor, 0 },
114 { &TOOLHELP_Descriptor, 0 },
115 { &TYPELIB_Descriptor, DLL_FLAG_NOT_USED },
116 { &VER_Descriptor, 0 },
117 { &W32SYS_Descriptor, 0 },
118 { &WIN32S16_Descriptor, 0 },
119 { &WIN87EM_Descriptor, DLL_FLAG_NOT_USED },
120 { &WINASPI_Descriptor, 0 },
121 { &WINEPS_Descriptor, DLL_FLAG_ALWAYS_USED },
122 { &WING_Descriptor, 0 },
123 { &WINSOCK_Descriptor, 0 },
124 /* Last entry */
125 { NULL, 0 }
128 /* Ordinal number for interrupt 0 handler in WPROCS.DLL */
129 #define FIRST_INTERRUPT_ORDINAL 100
132 /***********************************************************************
133 * BUILTIN_DoLoadModule16
135 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule
136 * and BUILTIN_Init.
138 static HMODULE16 BUILTIN_DoLoadModule16( const WIN16_DESCRIPTOR *descr )
140 NE_MODULE *pModule;
141 int minsize;
142 SEGTABLEENTRY *pSegTable;
144 HMODULE16 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
145 descr->module_size, 0,
146 FALSE, FALSE, FALSE, NULL );
147 if (!hModule) return 0;
148 FarSetOwner( hModule, hModule );
150 TRACE(module, "Built-in %s: hmodule=%04x\n",
151 descr->name, hModule );
152 pModule = (NE_MODULE *)GlobalLock16( hModule );
153 pModule->self = hModule;
155 /* Allocate the code segment */
157 pSegTable = NE_SEG_TABLE( pModule );
158 pSegTable->selector = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
159 pSegTable->minsize, hModule,
160 TRUE, TRUE, FALSE, NULL );
161 if (!pSegTable->selector) return 0;
162 pSegTable++;
164 /* Allocate the data segment */
166 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
167 minsize += pModule->heap_size;
168 if (minsize > 0x10000) minsize = 0x10000;
169 pSegTable->selector = GLOBAL_Alloc( GMEM_FIXED, minsize,
170 hModule, FALSE, FALSE, FALSE );
171 if (!pSegTable->selector) return 0;
172 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->selector ),
173 descr->data_start, pSegTable->minsize);
174 if (pModule->heap_size)
175 LocalInit( pSegTable->selector, pSegTable->minsize, minsize );
177 NE_RegisterModule( pModule );
178 return hModule;
182 /***********************************************************************
183 * BUILTIN_Init
185 * Load all built-in modules marked as 'always used'.
187 BOOL32 BUILTIN_Init(void)
189 BUILTIN16_DLL *dll;
190 NE_MODULE *pModule;
191 WORD vector;
192 HMODULE16 hModule;
193 WORD cs, ds;
195 fnBUILTIN_LoadModule = BUILTIN_LoadModule;
197 for (dll = BuiltinDLLs; dll->descr; dll++)
199 if (dll->flags & DLL_FLAG_ALWAYS_USED)
200 if (!BUILTIN_DoLoadModule16( dll->descr )) return FALSE;
203 /* Set the USER and GDI heap selectors */
205 pModule = NE_GetPtr( GetModuleHandle16( "USER" ));
206 USER_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
207 pModule = NE_GetPtr( GetModuleHandle16( "GDI" ));
208 GDI_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
210 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
212 hModule = GetModuleHandle16( "KERNEL" );
213 NE_SetEntryPoint( hModule, 178, GetWinFlags() );
215 /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
217 GET_CS(cs); GET_DS(ds);
218 NE_SetEntryPoint( hModule, 454, cs );
219 NE_SetEntryPoint( hModule, 455, ds );
221 /* Initialize the real-mode selector entry points */
223 #define SET_ENTRY_POINT( num, addr ) \
224 NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
225 DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
226 FALSE, FALSE, FALSE, NULL ))
228 SET_ENTRY_POINT( 183, 0x00000 ); /* KERNEL.183: __0000H */
229 SET_ENTRY_POINT( 174, 0xa0000 ); /* KERNEL.174: __A000H */
230 SET_ENTRY_POINT( 181, 0xb0000 ); /* KERNEL.181: __B000H */
231 SET_ENTRY_POINT( 182, 0xb8000 ); /* KERNEL.182: __B800H */
232 SET_ENTRY_POINT( 195, 0xc0000 ); /* KERNEL.195: __C000H */
233 SET_ENTRY_POINT( 179, 0xd0000 ); /* KERNEL.179: __D000H */
234 SET_ENTRY_POINT( 190, 0xe0000 ); /* KERNEL.190: __E000H */
235 SET_ENTRY_POINT( 173, 0xf0000 ); /* KERNEL.173: __ROMBIOS */
236 SET_ENTRY_POINT( 194, 0xf0000 ); /* KERNEL.194: __F000H */
237 NE_SetEntryPoint( hModule, 193, DOSMEM_BiosSeg ); /* KERNEL.193: __0040H */
238 #undef SET_ENTRY_POINT
240 /* Set interrupt vectors from entry points in WPROCS.DLL */
242 hModule = GetModuleHandle16( "WPROCS" );
243 for (vector = 0; vector < 256; vector++)
245 FARPROC16 proc = NE_GetEntryPoint( hModule,
246 FIRST_INTERRUPT_ORDINAL + vector );
247 assert(proc);
248 INT_SetHandler( vector, proc );
251 return TRUE;
255 /***********************************************************************
256 * BUILTIN_LoadModule
258 * Load a built-in module. If the 'force' parameter is FALSE, we only
259 * load the module if it has not been disabled via the -dll option.
261 HMODULE16 BUILTIN_LoadModule( LPCSTR name, BOOL32 force )
263 BUILTIN16_DLL *table;
264 char dllname[16], *p;
266 /* Fix the name in case we have a full path and extension */
268 if ((p = strrchr( name, '\\' ))) name = p + 1;
269 lstrcpyn32A( dllname, name, sizeof(dllname) );
270 if ((p = strrchr( dllname, '.' ))) *p = '\0';
272 for (table = BuiltinDLLs; table->descr; table++)
273 if (!lstrcmpi32A( table->descr->name, dllname )) break;
274 if (!table->descr) return 0;
275 if ((table->flags & DLL_FLAG_NOT_USED) && !force) return 0;
277 return BUILTIN_DoLoadModule16( table->descr );
281 /***********************************************************************
282 * BUILTIN_GetEntryPoint16
284 * Return the ordinal and name corresponding to a CS:IP address.
285 * This is used only by relay debugging.
287 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
289 static char buffer[80];
290 WORD ordinal, i, max_offset;
291 register BYTE *p;
292 NE_MODULE *pModule;
294 if (!(pModule = NE_GetPtr( FarGetOwner( GlobalHandle16(cs) ))))
295 return NULL;
297 /* Search for the ordinal */
299 p = (BYTE *)pModule + pModule->entry_table;
300 max_offset = 0;
301 ordinal = 1;
302 *pOrd = 0;
303 while (*p)
305 switch(p[1])
307 case 0: /* unused */
308 ordinal += *p;
309 p += 2;
310 break;
311 case 1: /* code segment */
312 i = *p;
313 p += 2;
314 while (i-- > 0)
316 p++;
317 if ((*(WORD *)p <= ip) && (*(WORD *)p >= max_offset))
319 max_offset = *(WORD *)p;
320 *pOrd = ordinal;
322 p += 2;
323 ordinal++;
325 break;
326 case 0xff: /* moveable (should not happen in built-in modules) */
327 TRACE( relay, "Built-in module has moveable entry\n" );
328 ordinal += *p;
329 p += 2 + *p * 6;
330 break;
331 default: /* other segment */
332 ordinal += *p;
333 p += 2 + *p * 3;
334 break;
338 /* Search for the name in the resident names table */
339 /* (built-in modules have no non-resident table) */
341 p = (BYTE *)pModule + pModule->name_table;
342 while (*p)
344 p += *p + 1 + sizeof(WORD);
345 if (*(WORD *)(p + *p + 1) == *pOrd) break;
348 sprintf( buffer, "%.*s.%d: %.*s",
349 *((BYTE *)pModule + pModule->name_table),
350 (char *)pModule + pModule->name_table + 1,
351 *pOrd, *p, (char *)(p + 1) );
352 return buffer;
356 /**********************************************************************
357 * BUILTIN_DefaultIntHandler
359 * Default interrupt handler.
361 void BUILTIN_DefaultIntHandler( CONTEXT *context )
363 WORD ordinal;
364 STACK16FRAME *frame = CURRENT_STACK16;
365 BUILTIN_GetEntryPoint16( frame->entry_cs, frame->entry_ip, &ordinal );
366 INT_BARF( context, ordinal - FIRST_INTERRUPT_ORDINAL );
370 /***********************************************************************
371 * BUILTIN_ParseDLLOptions
373 * Set runtime DLL usage flags
375 BOOL32 BUILTIN_ParseDLLOptions( const char *str )
377 BUILTIN16_DLL *dll;
378 const char *p;
380 while (*str)
382 while (*str && isspace(*str)) str++;
383 if (!*str) return TRUE;
384 if ((*str != '+') && (*str != '-')) return FALSE;
385 str++;
386 if (!(p = strchr( str, ',' ))) p = str + strlen(str);
387 while ((p > str) && isspace(p[-1])) p--;
388 if (p == str) return FALSE;
389 for (dll = BuiltinDLLs; dll->descr; dll++)
391 if (!lstrncmpi32A( str, dll->descr->name, (int)(p - str) ))
393 if (dll->descr->name[(int)(p-str)]) /* only partial match */
394 continue;
395 if (str[-1] == '-')
397 if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
398 dll->flags |= DLL_FLAG_NOT_USED;
400 else dll->flags &= ~DLL_FLAG_NOT_USED;
401 break;
404 if (!dll->descr)
405 if (!BUILTIN32_EnableDLL( str, (int)(p - str), (str[-1] == '+') ))
406 return FALSE;
407 str = p;
408 while (*str && (isspace(*str) || (*str == ','))) str++;
410 return TRUE;
414 /***********************************************************************
415 * BUILTIN_PrintDLLs
417 * Print the list of built-in DLLs that can be disabled.
419 void BUILTIN_PrintDLLs(void)
421 int i;
422 BUILTIN16_DLL *dll;
424 MSG("Example: -dll -ole2 Do not use emulated OLE2.DLL\n");
425 MSG("Available Win16 DLLs:\n");
426 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
428 if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
429 MSG("%-9s%c", dll->descr->name,
430 ((++i) % 8) ? ' ' : '\n' );
432 MSG("\n");
433 BUILTIN32_PrintDLLs();