Update the modemn status bit that indicates whether the RLSD line is
[wine.git] / if1632 / builtin.c
blobbe10b5226b41949dc7d5ad05bfe3da8952760db7
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 AVIFILE_Descriptor;
50 extern const WIN16_DESCRIPTOR COMMDLG_Descriptor;
51 extern const WIN16_DESCRIPTOR COMPOBJ_Descriptor;
52 extern const WIN16_DESCRIPTOR DDEML_Descriptor;
53 extern const WIN16_DESCRIPTOR DISPLAY_Descriptor;
54 extern const WIN16_DESCRIPTOR GDI_Descriptor;
55 extern const WIN16_DESCRIPTOR KERNEL_Descriptor;
56 extern const WIN16_DESCRIPTOR KEYBOARD_Descriptor;
57 extern const WIN16_DESCRIPTOR LZEXPAND_Descriptor;
58 extern const WIN16_DESCRIPTOR MMSYSTEM_Descriptor;
59 extern const WIN16_DESCRIPTOR MOUSE_Descriptor;
60 extern const WIN16_DESCRIPTOR MSVIDEO_Descriptor;
61 extern const WIN16_DESCRIPTOR OLE2CONV_Descriptor;
62 extern const WIN16_DESCRIPTOR OLE2DISP_Descriptor;
63 extern const WIN16_DESCRIPTOR OLE2NLS_Descriptor;
64 extern const WIN16_DESCRIPTOR OLE2PROX_Descriptor;
65 extern const WIN16_DESCRIPTOR OLE2THK_Descriptor;
66 extern const WIN16_DESCRIPTOR OLE2_Descriptor;
67 extern const WIN16_DESCRIPTOR OLECLI_Descriptor;
68 extern const WIN16_DESCRIPTOR OLESVR_Descriptor;
69 extern const WIN16_DESCRIPTOR RASAPI16_Descriptor;
70 extern const WIN16_DESCRIPTOR SHELL_Descriptor;
71 extern const WIN16_DESCRIPTOR SOUND_Descriptor;
72 extern const WIN16_DESCRIPTOR STORAGE_Descriptor;
73 extern const WIN16_DESCRIPTOR STRESS_Descriptor;
74 extern const WIN16_DESCRIPTOR SYSTEM_Descriptor;
75 extern const WIN16_DESCRIPTOR TOOLHELP_Descriptor;
76 extern const WIN16_DESCRIPTOR TYPELIB_Descriptor;
77 extern const WIN16_DESCRIPTOR USER_Descriptor;
78 extern const WIN16_DESCRIPTOR VER_Descriptor;
79 extern const WIN16_DESCRIPTOR W32SYS_Descriptor;
80 extern const WIN16_DESCRIPTOR WIN32S16_Descriptor;
81 extern const WIN16_DESCRIPTOR WIN87EM_Descriptor;
82 extern const WIN16_DESCRIPTOR WINASPI_Descriptor;
83 extern const WIN16_DESCRIPTOR WINDEBUG_Descriptor;
84 extern const WIN16_DESCRIPTOR WINEPS_Descriptor;
85 extern const WIN16_DESCRIPTOR WING_Descriptor;
86 extern const WIN16_DESCRIPTOR WINSOCK_Descriptor;
87 extern const WIN16_DESCRIPTOR WPROCS_Descriptor;
89 /* Table of all built-in DLLs */
91 static BUILTIN16_DLL BuiltinDLLs[] =
93 { &KERNEL_Descriptor, DLL_FLAG_ALWAYS_USED },
94 { &USER_Descriptor, DLL_FLAG_ALWAYS_USED },
95 { &GDI_Descriptor, DLL_FLAG_ALWAYS_USED },
96 { &SYSTEM_Descriptor, DLL_FLAG_ALWAYS_USED },
97 { &DISPLAY_Descriptor, DLL_FLAG_ALWAYS_USED },
98 { &WPROCS_Descriptor, DLL_FLAG_ALWAYS_USED },
99 { &WINDEBUG_Descriptor, DLL_FLAG_NOT_USED },
100 { &AVIFILE_Descriptor, DLL_FLAG_NOT_USED },
101 { &COMMDLG_Descriptor, DLL_FLAG_NOT_USED },
102 { &COMPOBJ_Descriptor, DLL_FLAG_NOT_USED },
103 { &DDEML_Descriptor, DLL_FLAG_NOT_USED },
104 { &KEYBOARD_Descriptor, 0 },
105 { &LZEXPAND_Descriptor, 0 },
106 { &MMSYSTEM_Descriptor, 0 },
107 { &MOUSE_Descriptor, 0 },
108 { &MSVIDEO_Descriptor, 0 },
109 { &OLE2CONV_Descriptor, DLL_FLAG_NOT_USED },
110 { &OLE2DISP_Descriptor, DLL_FLAG_NOT_USED },
111 { &OLE2NLS_Descriptor, DLL_FLAG_NOT_USED },
112 { &OLE2PROX_Descriptor, DLL_FLAG_NOT_USED },
113 { &OLE2THK_Descriptor, DLL_FLAG_NOT_USED },
114 { &OLE2_Descriptor, DLL_FLAG_NOT_USED },
115 { &OLECLI_Descriptor, DLL_FLAG_NOT_USED },
116 { &OLESVR_Descriptor, DLL_FLAG_NOT_USED },
117 { &RASAPI16_Descriptor, 0 },
118 { &SHELL_Descriptor, 0 },
119 { &SOUND_Descriptor, 0 },
120 { &STORAGE_Descriptor, DLL_FLAG_NOT_USED },
121 { &STRESS_Descriptor, 0 },
122 { &TOOLHELP_Descriptor, 0 },
123 { &TYPELIB_Descriptor, DLL_FLAG_NOT_USED },
124 { &VER_Descriptor, 0 },
125 { &W32SYS_Descriptor, DLL_FLAG_NOT_USED },
126 { &WIN32S16_Descriptor, DLL_FLAG_NOT_USED },
127 { &WIN87EM_Descriptor, DLL_FLAG_NOT_USED },
128 { &WINASPI_Descriptor, 0 },
129 { &WINEPS_Descriptor, DLL_FLAG_ALWAYS_USED },
130 { &WING_Descriptor, 0 },
131 { &WINSOCK_Descriptor, 0 },
132 /* Last entry */
133 { NULL, 0 }
136 /* Ordinal number for interrupt 0 handler in WPROCS.DLL */
137 #define FIRST_INTERRUPT_ORDINAL 100
140 /***********************************************************************
141 * BUILTIN_DoLoadModule16
143 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule
144 * and BUILTIN_Init.
146 static HMODULE16 BUILTIN_DoLoadModule16( const WIN16_DESCRIPTOR *descr )
148 NE_MODULE *pModule;
149 int minsize;
150 SEGTABLEENTRY *pSegTable;
152 HMODULE16 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
153 descr->module_size, 0,
154 FALSE, FALSE, FALSE, NULL );
155 if (!hModule) return 0;
156 FarSetOwner( hModule, hModule );
158 TRACE(module, "Built-in %s: hmodule=%04x\n",
159 descr->name, hModule );
160 pModule = (NE_MODULE *)GlobalLock16( hModule );
161 pModule->self = hModule;
163 /* Allocate the code segment */
165 pSegTable = NE_SEG_TABLE( pModule );
166 pSegTable->hSeg = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
167 pSegTable->minsize, hModule,
168 TRUE, TRUE, FALSE, NULL );
169 if (!pSegTable->hSeg) return 0;
170 pSegTable++;
172 /* Allocate the data segment */
174 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
175 minsize += pModule->heap_size;
176 if (minsize > 0x10000) minsize = 0x10000;
177 pSegTable->hSeg = GLOBAL_Alloc( GMEM_FIXED, minsize,
178 hModule, FALSE, FALSE, FALSE );
179 if (!pSegTable->hSeg) return 0;
180 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->hSeg ),
181 descr->data_start, pSegTable->minsize);
182 if (pModule->heap_size)
183 LocalInit( GlobalHandleToSel(pSegTable->hSeg),
184 pSegTable->minsize, minsize );
186 NE_RegisterModule( pModule );
187 return hModule;
191 /***********************************************************************
192 * BUILTIN_Init
194 * Load all built-in modules marked as 'always used'.
196 BOOL32 BUILTIN_Init(void)
198 BUILTIN16_DLL *dll;
199 NE_MODULE *pModule;
200 WORD vector;
201 HMODULE16 hModule;
202 WORD cs, ds;
204 fnBUILTIN_LoadModule = BUILTIN_LoadModule;
206 for (dll = BuiltinDLLs; dll->descr; dll++)
208 if (dll->flags & DLL_FLAG_ALWAYS_USED)
209 if (!BUILTIN_DoLoadModule16( dll->descr )) return FALSE;
212 /* Set the USER and GDI heap selectors */
214 pModule = NE_GetPtr( GetModuleHandle16( "USER" ));
215 USER_HeapSel = pModule ? GlobalHandleToSel((NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->hSeg) : 0;
216 pModule = NE_GetPtr( GetModuleHandle16( "GDI" ));
217 GDI_HeapSel = pModule ? GlobalHandleToSel((NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->hSeg) : 0;
219 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
221 hModule = GetModuleHandle16( "KERNEL" );
222 NE_SetEntryPoint( hModule, 178, GetWinFlags() );
224 /* Initialize KERNEL.454/455 (__FLATCS/__FLATDS) */
226 GET_CS(cs); GET_DS(ds);
227 NE_SetEntryPoint( hModule, 454, cs );
228 NE_SetEntryPoint( hModule, 455, ds );
230 /* Initialize KERNEL.THHOOK */
232 TASK_InstallTHHook((THHOOK *)PTR_SEG_TO_LIN(
233 (SEGPTR)NE_GetEntryPoint( hModule, 332 )));
235 /* Initialize the real-mode selector entry points */
237 #define SET_ENTRY_POINT( num, addr ) \
238 NE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
239 DOSMEM_MapDosToLinear(addr), 0x10000, hModule, \
240 FALSE, FALSE, FALSE, NULL ))
242 SET_ENTRY_POINT( 183, 0x00000 ); /* KERNEL.183: __0000H */
243 SET_ENTRY_POINT( 174, 0xa0000 ); /* KERNEL.174: __A000H */
244 SET_ENTRY_POINT( 181, 0xb0000 ); /* KERNEL.181: __B000H */
245 SET_ENTRY_POINT( 182, 0xb8000 ); /* KERNEL.182: __B800H */
246 SET_ENTRY_POINT( 195, 0xc0000 ); /* KERNEL.195: __C000H */
247 SET_ENTRY_POINT( 179, 0xd0000 ); /* KERNEL.179: __D000H */
248 SET_ENTRY_POINT( 190, 0xe0000 ); /* KERNEL.190: __E000H */
249 SET_ENTRY_POINT( 173, 0xf0000 ); /* KERNEL.173: __ROMBIOS */
250 SET_ENTRY_POINT( 194, 0xf0000 ); /* KERNEL.194: __F000H */
251 NE_SetEntryPoint( hModule, 193, DOSMEM_BiosSeg ); /* KERNEL.193: __0040H */
252 #undef SET_ENTRY_POINT
254 /* Set interrupt vectors from entry points in WPROCS.DLL */
256 hModule = GetModuleHandle16( "WPROCS" );
257 for (vector = 0; vector < 256; vector++)
259 FARPROC16 proc = NE_GetEntryPoint( hModule,
260 FIRST_INTERRUPT_ORDINAL + vector );
261 assert(proc);
262 INT_SetPMHandler( vector, proc );
265 SNOOP16_Init();
267 return TRUE;
271 /***********************************************************************
272 * BUILTIN_LoadModule
274 * Load a built-in module. If the 'force' parameter is FALSE, we only
275 * load the module if it has not been disabled via the -dll option.
277 HMODULE16 BUILTIN_LoadModule( LPCSTR name, BOOL32 force )
279 BUILTIN16_DLL *table;
280 char dllname[16], *p;
282 /* Fix the name in case we have a full path and extension */
284 if ((p = strrchr( name, '\\' ))) name = p + 1;
285 lstrcpyn32A( dllname, name, sizeof(dllname) );
286 if ((p = strrchr( dllname, '.' ))) *p = '\0';
288 for (table = BuiltinDLLs; table->descr; table++)
289 if (!lstrcmpi32A( table->descr->name, dllname )) break;
290 if (!table->descr) return 0;
291 if ((table->flags & DLL_FLAG_NOT_USED) && !force) return 0;
293 return BUILTIN_DoLoadModule16( table->descr );
297 /***********************************************************************
298 * BUILTIN_GetEntryPoint16
300 * Return the ordinal and name corresponding to a CS:IP address.
301 * This is used only by relay debugging.
303 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
305 static char buffer[80];
306 WORD ordinal, i, max_offset;
307 register BYTE *p;
308 NE_MODULE *pModule;
310 if (!(pModule = NE_GetPtr( FarGetOwner( GlobalHandle16(cs) ))))
311 return NULL;
313 /* Search for the ordinal */
315 p = (BYTE *)pModule + pModule->entry_table;
316 max_offset = 0;
317 ordinal = 1;
318 *pOrd = 0;
319 while (*p)
321 switch(p[1])
323 case 0: /* unused */
324 ordinal += *p;
325 p += 2;
326 break;
327 case 1: /* code segment */
328 i = *p;
329 p += 2;
330 while (i-- > 0)
332 p++;
333 if ((*(WORD *)p <= ip) && (*(WORD *)p >= max_offset))
335 max_offset = *(WORD *)p;
336 *pOrd = ordinal;
338 p += 2;
339 ordinal++;
341 break;
342 case 0xff: /* moveable (should not happen in built-in modules) */
343 TRACE( relay, "Built-in module has moveable entry\n" );
344 ordinal += *p;
345 p += 2 + *p * 6;
346 break;
347 default: /* other segment */
348 ordinal += *p;
349 p += 2 + *p * 3;
350 break;
354 /* Search for the name in the resident names table */
355 /* (built-in modules have no non-resident table) */
357 p = (BYTE *)pModule + pModule->name_table;
358 while (*p)
360 p += *p + 1 + sizeof(WORD);
361 if (*(WORD *)(p + *p + 1) == *pOrd) break;
364 sprintf( buffer, "%.*s.%d: %.*s",
365 *((BYTE *)pModule + pModule->name_table),
366 (char *)pModule + pModule->name_table + 1,
367 *pOrd, *p, (char *)(p + 1) );
368 return buffer;
372 /**********************************************************************
373 * BUILTIN_DefaultIntHandler
375 * Default interrupt handler.
377 void BUILTIN_DefaultIntHandler( CONTEXT *context )
379 WORD ordinal;
380 STACK16FRAME *frame = CURRENT_STACK16;
381 BUILTIN_GetEntryPoint16( frame->entry_cs, frame->entry_ip, &ordinal );
382 INT_BARF( context, ordinal - FIRST_INTERRUPT_ORDINAL );
386 /***********************************************************************
387 * BUILTIN_ParseDLLOptions
389 * Set runtime DLL usage flags
391 BOOL32 BUILTIN_ParseDLLOptions( const char *str )
393 BUILTIN16_DLL *dll;
394 const char *p;
396 while (*str)
398 while (*str && isspace(*str)) str++;
399 if (!*str) return TRUE;
400 if ((*str != '+') && (*str != '-')) return FALSE;
401 str++;
402 if (!(p = strchr( str, ',' ))) p = str + strlen(str);
403 while ((p > str) && isspace(p[-1])) p--;
404 if (p == str) return FALSE;
405 for (dll = BuiltinDLLs; dll->descr; dll++)
407 if (!lstrncmpi32A( str, dll->descr->name, (int)(p - str) ))
409 if (dll->descr->name[(int)(p-str)]) /* only partial match */
410 continue;
411 if (str[-1] == '-')
413 if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
414 dll->flags |= DLL_FLAG_NOT_USED;
416 else dll->flags &= ~DLL_FLAG_NOT_USED;
417 break;
420 if (!dll->descr)
421 if (!BUILTIN32_EnableDLL( str, (int)(p - str), (str[-1] == '+') ))
422 return FALSE;
423 str = p;
424 while (*str && (isspace(*str) || (*str == ','))) str++;
426 return TRUE;
430 /***********************************************************************
431 * BUILTIN_PrintDLLs
433 * Print the list of built-in DLLs that can be disabled.
435 void BUILTIN_PrintDLLs(void)
437 int i;
438 BUILTIN16_DLL *dll;
440 MSG("Example: -dll -ole2 Do not use emulated OLE2.DLL\n");
441 MSG("Available Win16 DLLs:\n");
442 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
444 if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
445 MSG("%-9s%c", dll->descr->name,
446 ((++i) % 8) ? ' ' : '\n' );
448 MSG("\n");
449 BUILTIN32_PrintDLLs();