Release 971221
[wine] / if1632 / builtin.c
1 /*
2  * Built-in modules
3  *
4  * Copyright 1996 Alexandre Julliard
5  */
6
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 "stddebug.h"
22 #include "debug.h"
23
24 /* Built-in modules descriptors */
25 /* Don't change these structures! (see tools/build.c) */
26
27 typedef struct
28 {
29     const char *name;              /* DLL name */
30     void       *module_start;      /* 32-bit address of the module data */
31     int         module_size;       /* Size of the module data */
32     const BYTE *code_start;        /* 32-bit address of DLL code */
33     const BYTE *data_start;        /* 32-bit address of DLL data */
34 } WIN16_DESCRIPTOR;
35
36 typedef struct
37 {
38     const WIN16_DESCRIPTOR *descr;     /* DLL descriptor */
39     int                     flags;     /* flags (see below) */
40 } BUILTIN16_DLL;
41
42 /* DLL flags */
43 #define DLL_FLAG_NOT_USED    0x01  /* Use original Windows DLL if possible */
44 #define DLL_FLAG_ALWAYS_USED 0x02  /* Always use built-in DLL */
45
46 /* 16-bit DLLs */
47
48 extern const WIN16_DESCRIPTOR COMMDLG_Descriptor;
49 extern const WIN16_DESCRIPTOR COMPOBJ_Descriptor;
50 extern const WIN16_DESCRIPTOR DDEML_Descriptor;
51 extern const WIN16_DESCRIPTOR GDI_Descriptor;
52 extern const WIN16_DESCRIPTOR KERNEL_Descriptor;
53 extern const WIN16_DESCRIPTOR KEYBOARD_Descriptor;
54 extern const WIN16_DESCRIPTOR LZEXPAND_Descriptor;
55 extern const WIN16_DESCRIPTOR MMSYSTEM_Descriptor;
56 extern const WIN16_DESCRIPTOR MOUSE_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 USER_Descriptor;
72 extern const WIN16_DESCRIPTOR VER_Descriptor;
73 extern const WIN16_DESCRIPTOR W32SYS_Descriptor;
74 extern const WIN16_DESCRIPTOR WIN32S16_Descriptor;
75 extern const WIN16_DESCRIPTOR WIN87EM_Descriptor;
76 extern const WIN16_DESCRIPTOR WINASPI_Descriptor;
77 extern const WIN16_DESCRIPTOR WINDEBUG_Descriptor;
78 extern const WIN16_DESCRIPTOR WING_Descriptor;
79 extern const WIN16_DESCRIPTOR WINSOCK_Descriptor;
80 extern const WIN16_DESCRIPTOR WPROCS_Descriptor;
81
82 /* Table of all built-in DLLs */
83
84 static BUILTIN16_DLL BuiltinDLLs[] =
85 {
86     { &KERNEL_Descriptor,   DLL_FLAG_ALWAYS_USED },
87     { &USER_Descriptor,     DLL_FLAG_ALWAYS_USED },
88     { &GDI_Descriptor,      DLL_FLAG_ALWAYS_USED },
89     { &SYSTEM_Descriptor,   DLL_FLAG_ALWAYS_USED },
90     { &WPROCS_Descriptor,   DLL_FLAG_ALWAYS_USED },
91     { &WINDEBUG_Descriptor, DLL_FLAG_ALWAYS_USED },
92     { &COMMDLG_Descriptor,  DLL_FLAG_NOT_USED },
93     { &COMPOBJ_Descriptor,  DLL_FLAG_NOT_USED },
94     { &DDEML_Descriptor,    DLL_FLAG_NOT_USED },
95     { &KEYBOARD_Descriptor, 0 },
96     { &LZEXPAND_Descriptor, 0 },
97     { &MMSYSTEM_Descriptor, 0 },
98     { &MOUSE_Descriptor,    0 },
99     { &OLE2CONV_Descriptor, DLL_FLAG_NOT_USED },
100     { &OLE2DISP_Descriptor, DLL_FLAG_NOT_USED },
101     { &OLE2NLS_Descriptor,  DLL_FLAG_NOT_USED },
102     { &OLE2PROX_Descriptor, DLL_FLAG_NOT_USED },
103     { &OLE2THK_Descriptor,  DLL_FLAG_NOT_USED },
104     { &OLE2_Descriptor,     DLL_FLAG_NOT_USED },
105     { &OLECLI_Descriptor,   DLL_FLAG_NOT_USED },
106     { &OLESVR_Descriptor,   DLL_FLAG_NOT_USED },
107     { &SHELL_Descriptor,    0 },
108     { &SOUND_Descriptor,    0 },
109     { &STORAGE_Descriptor,  DLL_FLAG_NOT_USED },
110     { &STRESS_Descriptor,   0 },
111     { &TOOLHELP_Descriptor, 0 },
112     { &VER_Descriptor,      0 },
113     { &W32SYS_Descriptor,   0 },
114     { &WIN32S16_Descriptor, 0 },
115     { &WIN87EM_Descriptor,  DLL_FLAG_NOT_USED },
116     { &WINASPI_Descriptor,  0 },
117     { &WING_Descriptor,     0 },
118     { &WINSOCK_Descriptor,  0 },
119     /* Last entry */
120     { NULL, 0 }
121 };
122
123   /* Ordinal number for interrupt 0 handler in WPROCS.DLL */
124 #define FIRST_INTERRUPT_ORDINAL 100
125
126
127 /***********************************************************************
128  *           BUILTIN_DoLoadModule16
129  *
130  * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule
131  * and BUILTIN_Init.
132  */
133 static HMODULE16 BUILTIN_DoLoadModule16( const WIN16_DESCRIPTOR *descr )
134 {
135     NE_MODULE *pModule;
136     int minsize;
137     SEGTABLEENTRY *pSegTable;
138
139     HMODULE16 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
140                                             descr->module_size, 0,
141                                             FALSE, FALSE, FALSE, NULL );
142     if (!hModule) return 0;
143     FarSetOwner( hModule, hModule );
144
145     dprintf_module( stddeb, "Built-in %s: hmodule=%04x\n",
146                     descr->name, hModule );
147     pModule = (NE_MODULE *)GlobalLock16( hModule );
148     pModule->self = hModule;
149
150     /* Allocate the code segment */
151
152     pSegTable = NE_SEG_TABLE( pModule );
153     pSegTable->selector = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
154                                               pSegTable->minsize, hModule,
155                                               TRUE, TRUE, FALSE, NULL );
156     if (!pSegTable->selector) return 0;
157     pSegTable++;
158
159     /* Allocate the data segment */
160
161     minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
162     minsize += pModule->heap_size;
163     if (minsize > 0x10000) minsize = 0x10000;
164     pSegTable->selector = GLOBAL_Alloc( GMEM_FIXED, minsize,
165                                         hModule, FALSE, FALSE, FALSE );
166     if (!pSegTable->selector) return 0;
167     if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->selector ),
168                                     descr->data_start, pSegTable->minsize);
169     if (pModule->heap_size)
170         LocalInit( pSegTable->selector, pSegTable->minsize, minsize );
171
172     MODULE_RegisterModule( pModule );
173     return hModule;
174 }
175
176
177 /***********************************************************************
178  *           BUILTIN_Init
179  *
180  * Load all built-in modules marked as 'always used'.
181  */
182 BOOL32 BUILTIN_Init(void)
183 {
184     BUILTIN16_DLL *dll;
185     NE_MODULE *pModule;
186     WORD vector;
187     HMODULE16 hModule;
188
189     for (dll = BuiltinDLLs; dll->descr; dll++)
190     {
191         if (dll->flags & DLL_FLAG_ALWAYS_USED)
192             if (!BUILTIN_DoLoadModule16( dll->descr )) return FALSE;
193     }
194
195     /* Set the USER and GDI heap selectors */
196
197     pModule      = MODULE_GetPtr( GetModuleHandle16( "USER" ));
198     USER_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
199     pModule      = MODULE_GetPtr( GetModuleHandle16( "GDI" ));
200     GDI_HeapSel  = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
201
202     /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
203
204     hModule = GetModuleHandle16( "KERNEL" );
205     MODULE_SetEntryPoint( hModule, 178, GetWinFlags() );
206
207     /* Initialize the real-mode selector entry points */
208
209     DOSMEM_InitExports( hModule );
210
211     /* Set interrupt vectors from entry points in WPROCS.DLL */
212
213     hModule = GetModuleHandle16( "WPROCS" );
214     for (vector = 0; vector < 256; vector++)
215     {
216         FARPROC16 proc = MODULE_GetEntryPoint( hModule,
217                                                FIRST_INTERRUPT_ORDINAL+vector);
218         assert(proc);
219         INT_SetHandler( vector, proc );
220     }
221
222     return TRUE;
223 }
224
225
226 /***********************************************************************
227  *           BUILTIN_LoadModule
228  *
229  * Load a built-in module. If the 'force' parameter is FALSE, we only
230  * load the module if it has not been disabled via the -dll option.
231  */
232 HMODULE32 BUILTIN_LoadModule( LPCSTR name, BOOL32 force )
233 {
234     BUILTIN16_DLL *table;
235     char dllname[16], *p;
236
237     /* Fix the name in case we have a full path and extension */
238
239     if ((p = strrchr( name, '\\' ))) name = p + 1;
240     lstrcpyn32A( dllname, name, sizeof(dllname) );
241     if ((p = strrchr( dllname, '.' ))) *p = '\0';
242
243     for (table = BuiltinDLLs; table->descr; table++)
244         if (!lstrcmpi32A( table->descr->name, dllname )) break;
245     if (!table->descr) return BUILTIN32_LoadModule( name, force );
246     if ((table->flags & DLL_FLAG_NOT_USED) && !force) return 0;
247
248     return BUILTIN_DoLoadModule16( table->descr );
249 }
250
251
252 /***********************************************************************
253  *           BUILTIN_GetEntryPoint16
254  *
255  * Return the ordinal and name corresponding to a CS:IP address.
256  * This is used only by relay debugging.
257  */
258 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
259 {
260     static char buffer[80];
261     WORD ordinal, i, max_offset;
262     register BYTE *p;
263     NE_MODULE *pModule;
264
265     if (!(pModule = MODULE_GetPtr( FarGetOwner( GlobalHandle16(cs) ))))
266         return NULL;
267
268     /* Search for the ordinal */
269
270     p = (BYTE *)pModule + pModule->entry_table;
271     max_offset = 0;
272     ordinal = 1;
273     *pOrd = 0;
274     while (*p)
275     {
276         switch(p[1])
277         {
278         case 0:    /* unused */
279             ordinal += *p;
280             p += 2;
281             break;
282         case 1:    /* code segment */
283             i = *p;
284             p += 2;
285             while (i-- > 0)
286             {
287                 p++;
288                 if ((*(WORD *)p <= ip) && (*(WORD *)p >= max_offset))
289                 {
290                     max_offset = *(WORD *)p;
291                     *pOrd = ordinal;
292                 }
293                 p += 2;
294                 ordinal++;
295             }
296             break;
297         case 0xff: /* moveable (should not happen in built-in modules) */
298             fprintf( stderr, "Built-in module has moveable entry\n" );
299             ordinal += *p;
300             p += 2 + *p * 6;
301             break;
302         default:   /* other segment */
303             ordinal += *p;
304             p += 2 + *p * 3;
305             break;
306         }
307     }
308
309     /* Search for the name in the resident names table */
310     /* (built-in modules have no non-resident table)   */
311     
312     p = (BYTE *)pModule + pModule->name_table;
313     while (*p)
314     {
315         p += *p + 1 + sizeof(WORD);
316         if (*(WORD *)(p + *p + 1) == *pOrd) break;
317     }
318
319     sprintf( buffer, "%.*s.%d: %.*s",
320              *((BYTE *)pModule + pModule->name_table),
321              (char *)pModule + pModule->name_table + 1,
322              *pOrd, *p, (char *)(p + 1) );
323     return buffer;
324 }
325
326
327 /**********************************************************************
328  *          BUILTIN_DefaultIntHandler
329  *
330  * Default interrupt handler.
331  */
332 void BUILTIN_DefaultIntHandler( CONTEXT *context )
333 {
334     WORD ordinal;
335     STACK16FRAME *frame = CURRENT_STACK16;
336     BUILTIN_GetEntryPoint16( frame->entry_cs, frame->entry_ip, &ordinal );
337     INT_BARF( context, ordinal - FIRST_INTERRUPT_ORDINAL );
338 }
339
340
341 /***********************************************************************
342  *           BUILTIN_ParseDLLOptions
343  *
344  * Set runtime DLL usage flags
345  */
346 BOOL32 BUILTIN_ParseDLLOptions( const char *str )
347 {
348     BUILTIN16_DLL *dll;
349     const char *p;
350
351     while (*str)
352     {
353         while (*str && isspace(*str)) str++;
354         if (!*str) return TRUE;
355         if ((*str != '+') && (*str != '-')) return FALSE;
356         str++;
357         if (!(p = strchr( str, ',' ))) p = str + strlen(str);
358         while ((p > str) && isspace(p[-1])) p--;
359         if (p == str) return FALSE;
360         for (dll = BuiltinDLLs; dll->descr; dll++)
361         {
362             if (!lstrncmpi32A( str, dll->descr->name, (int)(p - str) ))
363             {
364                 if (str[-1] == '-')
365                 {
366                     if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
367                     dll->flags |= DLL_FLAG_NOT_USED;
368                 }
369                 else dll->flags &= ~DLL_FLAG_NOT_USED;
370                 break;
371             }
372         }
373         if (!dll->descr) return FALSE;
374         str = p;
375         while (*str && (isspace(*str) || (*str == ','))) str++;
376     }
377     return TRUE;
378 }
379
380
381 /***********************************************************************
382  *           BUILTIN_PrintDLLs
383  *
384  * Print the list of built-in DLLs that can be disabled.
385  */
386 void BUILTIN_PrintDLLs(void)
387 {
388     int i;
389     BUILTIN16_DLL *dll;
390
391     fprintf(stderr,"Example: -dll -ole2    Do not use emulated OLE2.DLL\n");
392     fprintf(stderr,"Available DLLs:\n");
393     for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
394     {
395         if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
396             fprintf( stderr, "%-9s%c", dll->descr->name,
397                      ((++i) % 8) ? ' ' : '\n' );
398     }
399     fprintf(stderr,"\n");
400 }