Added -dll option for winelib programs. (Note: will not print warnings
[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 "winbase.h"
11 #include "wine/winbase16.h"
12 #include "wine/winestring.h"
13 #include "builtin32.h"
14 #include "global.h"
15 #include "heap.h"
16 #include "module.h"
17 #include "miscemu.h"
18 #include "neexe.h"
19 #include "stackframe.h"
20 #include "user.h"
21 #include "process.h"
22 #include "snoop.h"
23 #include "task.h"
24 #include "debug.h"
25
26 /* Built-in modules descriptors */
27 /* Don't change these structures! (see tools/build.c) */
28
29 typedef struct
30 {
31     const char *name;              /* DLL name */
32     void       *module_start;      /* 32-bit address of the module data */
33     int         module_size;       /* Size of the module data */
34     const BYTE *code_start;        /* 32-bit address of DLL code */
35     const BYTE *data_start;        /* 32-bit address of DLL data */
36 } WIN16_DESCRIPTOR;
37
38 typedef struct
39 {
40     const WIN16_DESCRIPTOR *descr;     /* DLL descriptor */
41     int                     flags;     /* flags (see below) */
42 } BUILTIN16_DLL;
43
44 /* DLL flags */
45 #define DLL_FLAG_NOT_USED    0x01  /* Use original Windows DLL if possible */
46 #define DLL_FLAG_ALWAYS_USED 0x02  /* Always use built-in DLL */
47
48 /* 16-bit DLLs */
49
50 extern const WIN16_DESCRIPTOR AVIFILE_Descriptor;
51 extern const WIN16_DESCRIPTOR COMM_Descriptor;
52 extern const WIN16_DESCRIPTOR COMMDLG_Descriptor;
53 extern const WIN16_DESCRIPTOR COMPOBJ_Descriptor;
54 extern const WIN16_DESCRIPTOR DDEML_Descriptor;
55 extern const WIN16_DESCRIPTOR DISPDIB_Descriptor;
56 extern const WIN16_DESCRIPTOR DISPLAY_Descriptor;
57 extern const WIN16_DESCRIPTOR GDI_Descriptor;
58 extern const WIN16_DESCRIPTOR KERNEL_Descriptor;
59 extern const WIN16_DESCRIPTOR KEYBOARD_Descriptor;
60 extern const WIN16_DESCRIPTOR LZEXPAND_Descriptor;
61 extern const WIN16_DESCRIPTOR MMSYSTEM_Descriptor;
62 extern const WIN16_DESCRIPTOR MOUSE_Descriptor;
63 extern const WIN16_DESCRIPTOR MSACM_Descriptor;
64 extern const WIN16_DESCRIPTOR MSVIDEO_Descriptor;
65 extern const WIN16_DESCRIPTOR OLE2CONV_Descriptor;
66 extern const WIN16_DESCRIPTOR OLE2DISP_Descriptor;
67 extern const WIN16_DESCRIPTOR OLE2NLS_Descriptor;
68 extern const WIN16_DESCRIPTOR OLE2PROX_Descriptor;
69 extern const WIN16_DESCRIPTOR OLE2THK_Descriptor;
70 extern const WIN16_DESCRIPTOR OLE2_Descriptor;
71 extern const WIN16_DESCRIPTOR OLECLI_Descriptor;
72 extern const WIN16_DESCRIPTOR OLESVR_Descriptor;
73 extern const WIN16_DESCRIPTOR RASAPI16_Descriptor;
74 extern const WIN16_DESCRIPTOR SHELL_Descriptor;
75 extern const WIN16_DESCRIPTOR SOUND_Descriptor;
76 extern const WIN16_DESCRIPTOR STORAGE_Descriptor;
77 extern const WIN16_DESCRIPTOR STRESS_Descriptor;
78 extern const WIN16_DESCRIPTOR SYSTEM_Descriptor;
79 extern const WIN16_DESCRIPTOR TOOLHELP_Descriptor;
80 extern const WIN16_DESCRIPTOR TYPELIB_Descriptor;
81 extern const WIN16_DESCRIPTOR USER_Descriptor;
82 extern const WIN16_DESCRIPTOR VER_Descriptor;
83 extern const WIN16_DESCRIPTOR W32SYS_Descriptor;
84 extern const WIN16_DESCRIPTOR WIN32S16_Descriptor;
85 extern const WIN16_DESCRIPTOR WIN87EM_Descriptor;
86 extern const WIN16_DESCRIPTOR WINASPI_Descriptor;
87 extern const WIN16_DESCRIPTOR WINDEBUG_Descriptor;
88 extern const WIN16_DESCRIPTOR WINEPS_Descriptor;
89 extern const WIN16_DESCRIPTOR WING_Descriptor;
90 extern const WIN16_DESCRIPTOR WINSOCK_Descriptor;
91 extern const WIN16_DESCRIPTOR WPROCS_Descriptor;
92
93 /* Table of all built-in DLLs */
94
95 static BUILTIN16_DLL BuiltinDLLs[] =
96 {
97     { &KERNEL_Descriptor,   0 },
98     { &USER_Descriptor,     0 },
99     { &GDI_Descriptor,      0 },
100     { &SYSTEM_Descriptor,   DLL_FLAG_ALWAYS_USED },
101     { &DISPLAY_Descriptor,  DLL_FLAG_ALWAYS_USED },
102     { &WPROCS_Descriptor,   DLL_FLAG_ALWAYS_USED },
103     { &WINDEBUG_Descriptor, DLL_FLAG_NOT_USED },
104     { &AVIFILE_Descriptor,  DLL_FLAG_NOT_USED },
105     { &COMMDLG_Descriptor,  DLL_FLAG_NOT_USED },
106     { &COMPOBJ_Descriptor,  DLL_FLAG_NOT_USED },
107     { &DDEML_Descriptor,    DLL_FLAG_NOT_USED },
108     { &DISPDIB_Descriptor,  0 },
109     { &KEYBOARD_Descriptor, 0 },
110     { &COMM_Descriptor,     0 },
111     { &LZEXPAND_Descriptor, 0 },
112     { &MMSYSTEM_Descriptor, 0 },
113     { &MOUSE_Descriptor,    0 },
114     { &MSACM_Descriptor,    0 },
115     { &MSVIDEO_Descriptor,  0 },
116     { &OLE2CONV_Descriptor, DLL_FLAG_NOT_USED },
117     { &OLE2DISP_Descriptor, DLL_FLAG_NOT_USED },
118     { &OLE2NLS_Descriptor,  DLL_FLAG_NOT_USED },
119     { &OLE2PROX_Descriptor, DLL_FLAG_NOT_USED },
120     { &OLE2THK_Descriptor,  DLL_FLAG_NOT_USED },
121     { &OLE2_Descriptor,     DLL_FLAG_NOT_USED },
122     { &OLECLI_Descriptor,   DLL_FLAG_NOT_USED },
123     { &OLESVR_Descriptor,   DLL_FLAG_NOT_USED },
124     { &RASAPI16_Descriptor, 0 },
125     { &SHELL_Descriptor,    0 },
126     { &SOUND_Descriptor,    0 },
127     { &STORAGE_Descriptor,  DLL_FLAG_NOT_USED },
128     { &STRESS_Descriptor,   0 },
129     { &TOOLHELP_Descriptor, 0 },
130     { &TYPELIB_Descriptor,  DLL_FLAG_NOT_USED },
131     { &VER_Descriptor,      0 },
132     { &W32SYS_Descriptor,   DLL_FLAG_NOT_USED },
133     { &WIN32S16_Descriptor, DLL_FLAG_NOT_USED },
134     { &WIN87EM_Descriptor,  DLL_FLAG_NOT_USED },
135     { &WINASPI_Descriptor,  0 },
136     { &WINEPS_Descriptor,   DLL_FLAG_ALWAYS_USED },
137     { &WING_Descriptor,     0 },
138     { &WINSOCK_Descriptor,  0 },
139     /* Last entry */
140     { NULL, 0 }
141 };
142
143   /* Ordinal number for interrupt 0 handler in WPROCS.DLL */
144 #define FIRST_INTERRUPT_ORDINAL 100
145
146
147 /***********************************************************************
148  *           BUILTIN_DoLoadModule16
149  *
150  * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule
151  * and BUILTIN_Init.
152  */
153 static HMODULE16 BUILTIN_DoLoadModule16( const WIN16_DESCRIPTOR *descr )
154 {
155     NE_MODULE *pModule;
156     int minsize;
157     SEGTABLEENTRY *pSegTable;
158
159     HMODULE16 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
160                                             descr->module_size, 0,
161                                             FALSE, FALSE, FALSE, NULL );
162     if (!hModule) return 0;
163     FarSetOwner16( hModule, hModule );
164
165     TRACE(module, "Built-in %s: hmodule=%04x\n",
166                     descr->name, hModule );
167     pModule = (NE_MODULE *)GlobalLock16( hModule );
168     pModule->self = hModule;
169
170     /* Allocate the code segment */
171
172     pSegTable = NE_SEG_TABLE( pModule );
173     pSegTable->hSeg = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
174                                               pSegTable->minsize, hModule,
175                                               TRUE, TRUE, FALSE, NULL );
176     if (!pSegTable->hSeg) return 0;
177     pSegTable++;
178
179     /* Allocate the data segment */
180
181     minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
182     minsize += pModule->heap_size;
183     if (minsize > 0x10000) minsize = 0x10000;
184     pSegTable->hSeg = GLOBAL_Alloc( GMEM_FIXED, minsize,
185                                         hModule, FALSE, FALSE, FALSE );
186     if (!pSegTable->hSeg) return 0;
187     if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->hSeg ),
188                                     descr->data_start, pSegTable->minsize);
189     if (pModule->heap_size)
190         LocalInit16( GlobalHandleToSel16(pSegTable->hSeg),
191                 pSegTable->minsize, minsize );
192
193     NE_RegisterModule( pModule );
194     return hModule;
195 }
196
197
198 /***********************************************************************
199  *           BUILTIN_Init
200  *
201  * Load all built-in modules marked as 'always used'.
202  */
203 BOOL BUILTIN_Init(void)
204 {
205     BUILTIN16_DLL *dll;
206     WORD vector;
207     HMODULE16 hModule;
208
209     fnBUILTIN_LoadModule = BUILTIN_LoadModule;
210
211     for (dll = BuiltinDLLs; dll->descr; dll++)
212     {
213         if (dll->flags & DLL_FLAG_ALWAYS_USED)
214             if (!BUILTIN_DoLoadModule16( dll->descr )) return FALSE;
215     }
216
217     /* Set interrupt vectors from entry points in WPROCS.DLL */
218
219     hModule = GetModuleHandle16( "WPROCS" );
220     for (vector = 0; vector < 256; vector++)
221     {
222         FARPROC16 proc = NE_GetEntryPoint( hModule,
223                                            FIRST_INTERRUPT_ORDINAL + vector );
224         assert(proc);
225         INT_SetPMHandler( vector, proc );
226     }
227
228     SNOOP16_Init();
229
230     return TRUE;
231 }
232
233
234 /***********************************************************************
235  *           BUILTIN_LoadModule
236  *
237  * Load a built-in module. If the 'force' parameter is FALSE, we only
238  * load the module if it has not been disabled via the -dll option.
239  */
240 HMODULE16 BUILTIN_LoadModule( LPCSTR name, BOOL force )
241 {
242     BUILTIN16_DLL *table;
243     char dllname[16], *p;
244
245     /* Fix the name in case we have a full path and extension */
246
247     if ((p = strrchr( name, '\\' ))) name = p + 1;
248     lstrcpynA( dllname, name, sizeof(dllname) );
249     if ((p = strrchr( dllname, '.' ))) *p = '\0';
250
251     for (table = BuiltinDLLs; table->descr; table++)
252         if (!lstrcmpiA( table->descr->name, dllname )) break;
253     if (!table->descr) return 0;
254     if ((table->flags & DLL_FLAG_NOT_USED) && !force) return 0;
255
256     return BUILTIN_DoLoadModule16( table->descr );
257 }
258
259
260 /***********************************************************************
261  *           BUILTIN_GetEntryPoint16
262  *
263  * Return the ordinal and name corresponding to a CS:IP address.
264  * This is used only by relay debugging.
265  */
266 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
267 {
268     static char buffer[80];
269     WORD ordinal, i, max_offset;
270     register BYTE *p;
271     NE_MODULE *pModule;
272
273     if (!(pModule = NE_GetPtr( FarGetOwner16( GlobalHandle16(cs) ))))
274         return NULL;
275
276     /* Search for the ordinal */
277
278     p = (BYTE *)pModule + pModule->entry_table;
279     max_offset = 0;
280     ordinal = 1;
281     *pOrd = 0;
282     while (*p)
283     {
284         switch(p[1])
285         {
286         case 0:    /* unused */
287             ordinal += *p;
288             p += 2;
289             break;
290         case 1:    /* code segment */
291             i = *p;
292             p += 2;
293             while (i-- > 0)
294             {
295                 p++;
296                 if ((*(WORD *)p <= ip) && (*(WORD *)p >= max_offset))
297                 {
298                     max_offset = *(WORD *)p;
299                     *pOrd = ordinal;
300                 }
301                 p += 2;
302                 ordinal++;
303             }
304             break;
305         case 0xff: /* moveable (should not happen in built-in modules) */
306             TRACE( relay, "Built-in module has moveable entry\n" );
307             ordinal += *p;
308             p += 2 + *p * 6;
309             break;
310         default:   /* other segment */
311             ordinal += *p;
312             p += 2 + *p * 3;
313             break;
314         }
315     }
316
317     /* Search for the name in the resident names table */
318     /* (built-in modules have no non-resident table)   */
319     
320     p = (BYTE *)pModule + pModule->name_table;
321     while (*p)
322     {
323         p += *p + 1 + sizeof(WORD);
324         if (*(WORD *)(p + *p + 1) == *pOrd) break;
325     }
326
327     sprintf( buffer, "%.*s.%d: %.*s",
328              *((BYTE *)pModule + pModule->name_table),
329              (char *)pModule + pModule->name_table + 1,
330              *pOrd, *p, (char *)(p + 1) );
331     return buffer;
332 }
333
334
335 /**********************************************************************
336  *          BUILTIN_DefaultIntHandler
337  *
338  * Default interrupt handler.
339  */
340 void BUILTIN_DefaultIntHandler( CONTEXT *context )
341 {
342     WORD ordinal;
343     STACK16FRAME *frame = CURRENT_STACK16;
344     BUILTIN_GetEntryPoint16( frame->entry_cs, frame->entry_ip, &ordinal );
345     INT_BARF( context, ordinal - FIRST_INTERRUPT_ORDINAL );
346 }
347
348
349 /***********************************************************************
350  *           BUILTIN_ParseDLLOptions
351  *
352  * Set runtime DLL usage flags
353  */
354 BOOL BUILTIN_ParseDLLOptions( char *str )
355 {
356     BUILTIN16_DLL *dll;
357     char *p,*last;
358
359
360     last = str;
361     while (*str)
362     {
363         while (*str && (*str==',' || isspace(*str))) str++;
364         if (!*str) {
365             *last = '\0'; /* cut off garbage at end at */
366             return TRUE;
367         }
368         if ((*str != '+') && (*str != '-')) return FALSE;
369         str++;
370         if (!(p = strchr( str, ',' ))) p = str + strlen(str);
371         while ((p > str) && isspace(p[-1])) p--;
372         if (p == str) return FALSE;
373         for (dll = BuiltinDLLs; dll->descr; dll++)
374         {
375             if (!lstrncmpiA( str, dll->descr->name, (int)(p - str) ))
376             {
377                 if (dll->descr->name[(int)(p-str)])  /* only partial match */
378                         continue;
379                 if (str[-1] == '-')
380                 {
381                     if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
382                     dll->flags |= DLL_FLAG_NOT_USED;
383                 }
384                 else dll->flags &= ~DLL_FLAG_NOT_USED;
385                 break;
386             }
387         }
388         if (!dll->descr) {
389             /* not found, but could get handled by BUILTIN32_, so move last */
390             last = p;
391             str = p;
392         } else {
393             /* handled. cut out the "[+-]DLL," string, so it isn't handled
394              * by BUILTIN32
395              */
396             if (*p) {
397                 memcpy(last,p,strlen(p)+1);
398                 str = last;
399             } else {
400                 *last = '\0';
401                 break;
402             }
403         }
404     }
405     return TRUE;
406 }
407
408
409 /***********************************************************************
410  *           BUILTIN_PrintDLLs
411  *
412  * Print the list of built-in DLLs that can be disabled.
413  */
414 void BUILTIN_PrintDLLs(void)
415 {
416     int i;
417     BUILTIN16_DLL *dll;
418
419     MSG("Example: -dll -ole2    Do not use emulated OLE2.DLL\n");
420     MSG("Available Win16 DLLs:\n");
421     for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
422     {
423         if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
424             MSG("%-9s%c", dll->descr->name,
425                      ((++i) % 8) ? ' ' : '\n' );
426     }
427     MSG("\n");
428     BUILTIN32_PrintDLLs();
429 }