Release 980301
[wine.git] / if1632 / builtin.c
blobe56183d0ccf2a25269e562df801f5d97eaef24fe
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 OLE2CONV_Descriptor;
57 extern const WIN16_DESCRIPTOR OLE2DISP_Descriptor;
58 extern const WIN16_DESCRIPTOR OLE2NLS_Descriptor;
59 extern const WIN16_DESCRIPTOR OLE2PROX_Descriptor;
60 extern const WIN16_DESCRIPTOR OLE2THK_Descriptor;
61 extern const WIN16_DESCRIPTOR OLE2_Descriptor;
62 extern const WIN16_DESCRIPTOR OLECLI_Descriptor;
63 extern const WIN16_DESCRIPTOR OLESVR_Descriptor;
64 extern const WIN16_DESCRIPTOR SHELL_Descriptor;
65 extern const WIN16_DESCRIPTOR SOUND_Descriptor;
66 extern const WIN16_DESCRIPTOR STORAGE_Descriptor;
67 extern const WIN16_DESCRIPTOR STRESS_Descriptor;
68 extern const WIN16_DESCRIPTOR SYSTEM_Descriptor;
69 extern const WIN16_DESCRIPTOR TOOLHELP_Descriptor;
70 extern const WIN16_DESCRIPTOR USER_Descriptor;
71 extern const WIN16_DESCRIPTOR VER_Descriptor;
72 extern const WIN16_DESCRIPTOR W32SYS_Descriptor;
73 extern const WIN16_DESCRIPTOR WIN32S16_Descriptor;
74 extern const WIN16_DESCRIPTOR WIN87EM_Descriptor;
75 extern const WIN16_DESCRIPTOR WINASPI_Descriptor;
76 extern const WIN16_DESCRIPTOR WINDEBUG_Descriptor;
77 extern const WIN16_DESCRIPTOR WING_Descriptor;
78 extern const WIN16_DESCRIPTOR WINSOCK_Descriptor;
79 extern const WIN16_DESCRIPTOR WPROCS_Descriptor;
81 /* Table of all built-in DLLs */
83 static BUILTIN16_DLL BuiltinDLLs[] =
85 { &KERNEL_Descriptor, DLL_FLAG_ALWAYS_USED },
86 { &USER_Descriptor, DLL_FLAG_ALWAYS_USED },
87 { &GDI_Descriptor, DLL_FLAG_ALWAYS_USED },
88 { &SYSTEM_Descriptor, DLL_FLAG_ALWAYS_USED },
89 { &WPROCS_Descriptor, DLL_FLAG_ALWAYS_USED },
90 { &WINDEBUG_Descriptor, DLL_FLAG_ALWAYS_USED },
91 { &COMMDLG_Descriptor, DLL_FLAG_NOT_USED },
92 { &COMPOBJ_Descriptor, DLL_FLAG_NOT_USED },
93 { &DDEML_Descriptor, DLL_FLAG_NOT_USED },
94 { &KEYBOARD_Descriptor, 0 },
95 { &LZEXPAND_Descriptor, 0 },
96 { &MMSYSTEM_Descriptor, 0 },
97 { &MOUSE_Descriptor, 0 },
98 { &OLE2CONV_Descriptor, DLL_FLAG_NOT_USED },
99 { &OLE2DISP_Descriptor, DLL_FLAG_NOT_USED },
100 { &OLE2NLS_Descriptor, DLL_FLAG_NOT_USED },
101 { &OLE2PROX_Descriptor, DLL_FLAG_NOT_USED },
102 { &OLE2THK_Descriptor, DLL_FLAG_NOT_USED },
103 { &OLE2_Descriptor, DLL_FLAG_NOT_USED },
104 { &OLECLI_Descriptor, DLL_FLAG_NOT_USED },
105 { &OLESVR_Descriptor, DLL_FLAG_NOT_USED },
106 { &SHELL_Descriptor, 0 },
107 { &SOUND_Descriptor, 0 },
108 { &STORAGE_Descriptor, DLL_FLAG_NOT_USED },
109 { &STRESS_Descriptor, 0 },
110 { &TOOLHELP_Descriptor, 0 },
111 { &VER_Descriptor, 0 },
112 { &W32SYS_Descriptor, 0 },
113 { &WIN32S16_Descriptor, 0 },
114 { &WIN87EM_Descriptor, DLL_FLAG_NOT_USED },
115 { &WINASPI_Descriptor, 0 },
116 { &WING_Descriptor, 0 },
117 { &WINSOCK_Descriptor, 0 },
118 /* Last entry */
119 { NULL, 0 }
122 /* Ordinal number for interrupt 0 handler in WPROCS.DLL */
123 #define FIRST_INTERRUPT_ORDINAL 100
126 /***********************************************************************
127 * BUILTIN_DoLoadModule16
129 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule
130 * and BUILTIN_Init.
132 static HMODULE16 BUILTIN_DoLoadModule16( const WIN16_DESCRIPTOR *descr )
134 NE_MODULE *pModule;
135 int minsize;
136 SEGTABLEENTRY *pSegTable;
138 HMODULE16 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
139 descr->module_size, 0,
140 FALSE, FALSE, FALSE, NULL );
141 if (!hModule) return 0;
142 FarSetOwner( hModule, hModule );
144 dprintf_info(module, "Built-in %s: hmodule=%04x\n",
145 descr->name, hModule );
146 pModule = (NE_MODULE *)GlobalLock16( hModule );
147 pModule->self = hModule;
149 /* Allocate the code segment */
151 pSegTable = NE_SEG_TABLE( pModule );
152 pSegTable->selector = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
153 pSegTable->minsize, hModule,
154 TRUE, TRUE, FALSE, NULL );
155 if (!pSegTable->selector) return 0;
156 pSegTable++;
158 /* Allocate the data segment */
160 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
161 minsize += pModule->heap_size;
162 if (minsize > 0x10000) minsize = 0x10000;
163 pSegTable->selector = GLOBAL_Alloc( GMEM_FIXED, minsize,
164 hModule, FALSE, FALSE, FALSE );
165 if (!pSegTable->selector) return 0;
166 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->selector ),
167 descr->data_start, pSegTable->minsize);
168 if (pModule->heap_size)
169 LocalInit( pSegTable->selector, pSegTable->minsize, minsize );
171 MODULE_RegisterModule( pModule );
172 return hModule;
176 /***********************************************************************
177 * BUILTIN_Init
179 * Load all built-in modules marked as 'always used'.
181 BOOL32 BUILTIN_Init(void)
183 BUILTIN16_DLL *dll;
184 NE_MODULE *pModule;
185 WORD vector;
186 HMODULE16 hModule;
188 fnBUILTIN_LoadModule = BUILTIN_LoadModule;
190 for (dll = BuiltinDLLs; dll->descr; dll++)
192 if (dll->flags & DLL_FLAG_ALWAYS_USED)
193 if (!BUILTIN_DoLoadModule16( dll->descr )) return FALSE;
196 /* Set the USER and GDI heap selectors */
198 pModule = MODULE_GetPtr( GetModuleHandle16( "USER" ));
199 USER_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
200 pModule = MODULE_GetPtr( GetModuleHandle16( "GDI" ));
201 GDI_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
203 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
205 hModule = GetModuleHandle16( "KERNEL" );
206 MODULE_SetEntryPoint( hModule, 178, GetWinFlags() );
208 /* Initialize the real-mode selector entry points */
210 DOSMEM_InitExports( hModule );
212 /* Set interrupt vectors from entry points in WPROCS.DLL */
214 hModule = GetModuleHandle16( "WPROCS" );
215 for (vector = 0; vector < 256; vector++)
217 FARPROC16 proc = MODULE_GetEntryPoint( hModule,
218 FIRST_INTERRUPT_ORDINAL+vector);
219 assert(proc);
220 INT_SetHandler( vector, proc );
223 return TRUE;
227 /***********************************************************************
228 * BUILTIN_LoadModule
230 * Load a built-in module. If the 'force' parameter is FALSE, we only
231 * load the module if it has not been disabled via the -dll option.
233 HMODULE32 BUILTIN_LoadModule( LPCSTR name, BOOL32 force )
235 BUILTIN16_DLL *table;
236 char dllname[16], *p;
238 /* Fix the name in case we have a full path and extension */
240 if ((p = strrchr( name, '\\' ))) name = p + 1;
241 lstrcpyn32A( dllname, name, sizeof(dllname) );
242 if ((p = strrchr( dllname, '.' ))) *p = '\0';
244 for (table = BuiltinDLLs; table->descr; table++)
245 if (!lstrcmpi32A( table->descr->name, dllname )) break;
246 if (!table->descr) return BUILTIN32_LoadModule( name, force,
247 PROCESS_Current() );
248 if ((table->flags & DLL_FLAG_NOT_USED) && !force) return 0;
250 return BUILTIN_DoLoadModule16( table->descr );
254 /***********************************************************************
255 * BUILTIN_GetEntryPoint16
257 * Return the ordinal and name corresponding to a CS:IP address.
258 * This is used only by relay debugging.
260 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
262 static char buffer[80];
263 WORD ordinal, i, max_offset;
264 register BYTE *p;
265 NE_MODULE *pModule;
267 if (!(pModule = MODULE_GetPtr( FarGetOwner( GlobalHandle16(cs) ))))
268 return NULL;
270 /* Search for the ordinal */
272 p = (BYTE *)pModule + pModule->entry_table;
273 max_offset = 0;
274 ordinal = 1;
275 *pOrd = 0;
276 while (*p)
278 switch(p[1])
280 case 0: /* unused */
281 ordinal += *p;
282 p += 2;
283 break;
284 case 1: /* code segment */
285 i = *p;
286 p += 2;
287 while (i-- > 0)
289 p++;
290 if ((*(WORD *)p <= ip) && (*(WORD *)p >= max_offset))
292 max_offset = *(WORD *)p;
293 *pOrd = ordinal;
295 p += 2;
296 ordinal++;
298 break;
299 case 0xff: /* moveable (should not happen in built-in modules) */
300 fprintf( stderr, "Built-in module has moveable entry\n" );
301 ordinal += *p;
302 p += 2 + *p * 6;
303 break;
304 default: /* other segment */
305 ordinal += *p;
306 p += 2 + *p * 3;
307 break;
311 /* Search for the name in the resident names table */
312 /* (built-in modules have no non-resident table) */
314 p = (BYTE *)pModule + pModule->name_table;
315 while (*p)
317 p += *p + 1 + sizeof(WORD);
318 if (*(WORD *)(p + *p + 1) == *pOrd) break;
321 sprintf( buffer, "%.*s.%d: %.*s",
322 *((BYTE *)pModule + pModule->name_table),
323 (char *)pModule + pModule->name_table + 1,
324 *pOrd, *p, (char *)(p + 1) );
325 return buffer;
329 /**********************************************************************
330 * BUILTIN_DefaultIntHandler
332 * Default interrupt handler.
334 void BUILTIN_DefaultIntHandler( CONTEXT *context )
336 WORD ordinal;
337 STACK16FRAME *frame = CURRENT_STACK16;
338 BUILTIN_GetEntryPoint16( frame->entry_cs, frame->entry_ip, &ordinal );
339 INT_BARF( context, ordinal - FIRST_INTERRUPT_ORDINAL );
343 /***********************************************************************
344 * BUILTIN_ParseDLLOptions
346 * Set runtime DLL usage flags
348 BOOL32 BUILTIN_ParseDLLOptions( const char *str )
350 BUILTIN16_DLL *dll;
351 const char *p;
353 while (*str)
355 while (*str && isspace(*str)) str++;
356 if (!*str) return TRUE;
357 if ((*str != '+') && (*str != '-')) return FALSE;
358 str++;
359 if (!(p = strchr( str, ',' ))) p = str + strlen(str);
360 while ((p > str) && isspace(p[-1])) p--;
361 if (p == str) return FALSE;
362 for (dll = BuiltinDLLs; dll->descr; dll++)
364 if (!lstrncmpi32A( str, dll->descr->name, (int)(p - str) ))
366 if (dll->descr->name[(int)(p-str)]) /* only partial match */
367 continue;
368 if (str[-1] == '-')
370 if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
371 dll->flags |= DLL_FLAG_NOT_USED;
373 else dll->flags &= ~DLL_FLAG_NOT_USED;
374 break;
377 if (!dll->descr)
378 if (!BUILTIN32_EnableDLL( str, (int)(p - str), (str[-1] == '+') ))
379 return FALSE;
380 str = p;
381 while (*str && (isspace(*str) || (*str == ','))) str++;
383 return TRUE;
387 /***********************************************************************
388 * BUILTIN_PrintDLLs
390 * Print the list of built-in DLLs that can be disabled.
392 void BUILTIN_PrintDLLs(void)
394 int i;
395 BUILTIN16_DLL *dll;
397 fprintf(stderr,"Example: -dll -ole2 Do not use emulated OLE2.DLL\n");
398 fprintf(stderr,"Available Win16 DLLs:\n");
399 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
401 if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
402 fprintf( stderr, "%-9s%c", dll->descr->name,
403 ((++i) % 8) ? ' ' : '\n' );
405 fprintf(stderr,"\n");
406 BUILTIN32_PrintDLLs();