Adapted to new-style Wine thunks.
[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 "builtin16.h"
14 #include "builtin32.h"
15 #include "global.h"
16 #include "heap.h"
17 #include "module.h"
18 #include "miscemu.h"
19 #include "neexe.h"
20 #include "stackframe.h"
21 #include "user.h"
22 #include "process.h"
23 #include "task.h"
24 #include "debugtools.h"
25 #include "toolhelp.h"
26
27 DEFAULT_DEBUG_CHANNEL(module)
28
29 typedef struct
30 {
31     const WIN16_DESCRIPTOR *descr;     /* DLL descriptor */
32     int                     flags;     /* flags (see below) */
33 } BUILTIN16_DLL;
34
35 /* DLL flags */
36 #define DLL_FLAG_NOT_USED    0x01  /* Use original Windows DLL if possible */
37 #define DLL_FLAG_ALWAYS_USED 0x02  /* Always use built-in DLL */
38
39 /* 16-bit DLLs */
40
41 extern const WIN16_DESCRIPTOR AVIFILE_Descriptor;
42 extern const WIN16_DESCRIPTOR COMM_Descriptor;
43 extern const WIN16_DESCRIPTOR COMMDLG_Descriptor;
44 extern const WIN16_DESCRIPTOR COMPOBJ_Descriptor;
45 extern const WIN16_DESCRIPTOR DDEML_Descriptor;
46 extern const WIN16_DESCRIPTOR DISPDIB_Descriptor;
47 extern const WIN16_DESCRIPTOR DISPLAY_Descriptor;
48 extern const WIN16_DESCRIPTOR GDI_Descriptor;
49 extern const WIN16_DESCRIPTOR KERNEL_Descriptor;
50 extern const WIN16_DESCRIPTOR KEYBOARD_Descriptor;
51 extern const WIN16_DESCRIPTOR LZEXPAND_Descriptor;
52 extern const WIN16_DESCRIPTOR MMSYSTEM_Descriptor;
53 extern const WIN16_DESCRIPTOR MOUSE_Descriptor;
54 extern const WIN16_DESCRIPTOR MSACM_Descriptor;
55 extern const WIN16_DESCRIPTOR MSVIDEO_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 RASAPI16_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;
83
84 /* Table of all built-in DLLs */
85
86 static BUILTIN16_DLL BuiltinDLLs[] =
87 {
88     { &KERNEL_Descriptor,   0 },
89     { &USER_Descriptor,     0 },
90     { &GDI_Descriptor,      0 },
91     { &SYSTEM_Descriptor,   DLL_FLAG_ALWAYS_USED },
92     { &DISPLAY_Descriptor,  DLL_FLAG_ALWAYS_USED },
93     { &WPROCS_Descriptor,   DLL_FLAG_ALWAYS_USED },
94     { &WINDEBUG_Descriptor, DLL_FLAG_NOT_USED },
95     { &AVIFILE_Descriptor,  DLL_FLAG_NOT_USED },
96     { &COMMDLG_Descriptor,  DLL_FLAG_NOT_USED },
97     { &COMPOBJ_Descriptor,  DLL_FLAG_NOT_USED },
98     { &DDEML_Descriptor,    DLL_FLAG_NOT_USED },
99     { &DISPDIB_Descriptor,  0 },
100     { &KEYBOARD_Descriptor, 0 },
101     { &COMM_Descriptor,     0 },
102     { &LZEXPAND_Descriptor, 0 },
103     { &MMSYSTEM_Descriptor, 0 },
104     { &MOUSE_Descriptor,    0 },
105     { &MSACM_Descriptor,    0 },
106     { &MSVIDEO_Descriptor,  0 },
107     { &OLE2CONV_Descriptor, DLL_FLAG_NOT_USED },
108     { &OLE2DISP_Descriptor, DLL_FLAG_NOT_USED },
109     { &OLE2NLS_Descriptor,  DLL_FLAG_NOT_USED },
110     { &OLE2PROX_Descriptor, DLL_FLAG_NOT_USED },
111     { &OLE2THK_Descriptor,  DLL_FLAG_NOT_USED },
112     { &OLE2_Descriptor,     DLL_FLAG_NOT_USED },
113     { &OLECLI_Descriptor,   DLL_FLAG_NOT_USED },
114     { &OLESVR_Descriptor,   DLL_FLAG_NOT_USED },
115     { &RASAPI16_Descriptor, 0 },
116     { &SHELL_Descriptor,    0 },
117     { &SOUND_Descriptor,    0 },
118     { &STORAGE_Descriptor,  DLL_FLAG_NOT_USED },
119     { &STRESS_Descriptor,   0 },
120     { &TOOLHELP_Descriptor, 0 },
121     { &TYPELIB_Descriptor,  DLL_FLAG_NOT_USED },
122     { &VER_Descriptor,      0 },
123     { &W32SYS_Descriptor,   DLL_FLAG_NOT_USED },
124     { &WIN32S16_Descriptor, DLL_FLAG_NOT_USED },
125     { &WIN87EM_Descriptor,  DLL_FLAG_NOT_USED },
126     { &WINASPI_Descriptor,  0 },
127     { &WINEPS_Descriptor,   DLL_FLAG_ALWAYS_USED },
128     { &WING_Descriptor,     0 },
129     { &WINSOCK_Descriptor,  0 },
130     /* Last entry */
131     { NULL, 0 }
132 };
133
134   /* Ordinal number for interrupt 0 handler in WPROCS.DLL */
135 #define FIRST_INTERRUPT_ORDINAL 100
136
137
138 /***********************************************************************
139  *           BUILTIN_DoLoadModule16
140  *
141  * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule
142  * and BUILTIN_Init.
143  */
144 static HMODULE16 BUILTIN_DoLoadModule16( const WIN16_DESCRIPTOR *descr )
145 {
146     NE_MODULE *pModule;
147     int minsize;
148     SEGTABLEENTRY *pSegTable;
149
150     HMODULE16 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
151                                             descr->module_size, 0,
152                                             FALSE, FALSE, FALSE, NULL );
153     if (!hModule) return 0;
154     FarSetOwner16( hModule, hModule );
155
156     TRACE("Built-in %s: hmodule=%04x\n",
157                     descr->name, hModule );
158     pModule = (NE_MODULE *)GlobalLock16( hModule );
159     pModule->self = hModule;
160
161     /* Allocate the code segment */
162
163     pSegTable = NE_SEG_TABLE( pModule );
164     pSegTable->hSeg = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
165                                               pSegTable->minsize, hModule,
166                                               TRUE, TRUE, FALSE, NULL );
167     if (!pSegTable->hSeg) return 0;
168     pSegTable++;
169
170     /* Allocate the data segment */
171
172     minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
173     minsize += pModule->heap_size;
174     if (minsize > 0x10000) minsize = 0x10000;
175     pSegTable->hSeg = GLOBAL_Alloc( GMEM_FIXED, minsize,
176                                         hModule, FALSE, FALSE, FALSE );
177     if (!pSegTable->hSeg) return 0;
178     if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->hSeg ),
179                                     descr->data_start, pSegTable->minsize);
180     if (pModule->heap_size)
181         LocalInit16( GlobalHandleToSel16(pSegTable->hSeg),
182                 pSegTable->minsize, minsize );
183
184     NE_RegisterModule( pModule );
185     return hModule;
186 }
187
188
189 /***********************************************************************
190  *           BUILTIN_Init
191  *
192  * Load all built-in modules marked as 'always used'.
193  */
194 BOOL BUILTIN_Init(void)
195 {
196     BUILTIN16_DLL *dll;
197     WORD vector;
198     HMODULE16 hModule;
199
200     for (dll = BuiltinDLLs; dll->descr; dll++)
201     {
202         if (dll->flags & DLL_FLAG_ALWAYS_USED)
203             if (!BUILTIN_DoLoadModule16( dll->descr )) return FALSE;
204     }
205
206     /* Set interrupt vectors from entry points in WPROCS.DLL */
207
208     hModule = GetModuleHandle16( "WPROCS" );
209     for (vector = 0; vector < 256; vector++)
210     {
211         FARPROC16 proc = NE_GetEntryPoint( hModule,
212                                            FIRST_INTERRUPT_ORDINAL + vector );
213         assert(proc);
214         INT_SetPMHandler( vector, proc );
215     }
216
217     return TRUE;
218 }
219
220
221 /***********************************************************************
222  *           BUILTIN_LoadModule
223  *
224  * Load a built-in module. If the 'force' parameter is FALSE, we only
225  * load the module if it has not been disabled via the -dll option.
226  */
227 HMODULE16 BUILTIN_LoadModule( LPCSTR name, BOOL force )
228 {
229     BUILTIN16_DLL *table;
230     char dllname[16], *p;
231
232     /* Fix the name in case we have a full path and extension */
233
234     if ((p = strrchr( name, '\\' ))) name = p + 1;
235     lstrcpynA( dllname, name, sizeof(dllname) );
236     if ((p = strrchr( dllname, '.' ))) *p = '\0';
237
238     for (table = BuiltinDLLs; table->descr; table++)
239         if (!lstrcmpiA( table->descr->name, dllname )) break;
240     if (!table->descr) return (HMODULE16)2;
241     if ((table->flags & DLL_FLAG_NOT_USED) && !force) return (HMODULE16)2;
242
243     return BUILTIN_DoLoadModule16( table->descr );
244 }
245
246
247 /***********************************************************************
248  *           BUILTIN_GetEntryPoint16
249  *
250  * Return the ordinal, name, and type info corresponding to a CS:IP address.
251  * This is used only by relay debugging.
252  */
253 LPCSTR BUILTIN_GetEntryPoint16( STACK16FRAME *frame, LPSTR name, WORD *pOrd )
254 {
255     WORD i, max_offset;
256     register BYTE *p;
257     NE_MODULE *pModule;
258     ET_BUNDLE *bundle;
259     ET_ENTRY *entry;
260
261     if (!(pModule = NE_GetPtr( FarGetOwner16( GlobalHandle16( frame->module_cs ) ))))
262         return NULL;
263
264     max_offset = 0;
265     *pOrd = 0;
266     bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
267     do 
268     {
269         entry = (ET_ENTRY *)((BYTE *)bundle+6);
270         for (i = bundle->first + 1; i <= bundle->last; i++)
271         {
272             if ((entry->offs < frame->entry_ip)
273             && (entry->segnum == 1) /* code segment ? */
274             && (entry->offs >= max_offset))
275             {
276                 max_offset = entry->offs;
277                 *pOrd = i;
278             }
279             entry++;
280         }
281     } while ( (bundle->next)
282            && (bundle = (ET_BUNDLE *)((BYTE *)pModule+bundle->next)));
283
284     /* Search for the name in the resident names table */
285     /* (built-in modules have no non-resident table)   */
286     
287     p = (BYTE *)pModule + pModule->name_table;
288     while (*p)
289     {
290         p += *p + 1 + sizeof(WORD);
291         if (*(WORD *)(p + *p + 1) == *pOrd) break;
292     }
293
294     sprintf( name, "%.*s.%d: %.*s",
295              *((BYTE *)pModule + pModule->name_table),
296              (char *)pModule + pModule->name_table + 1,
297              *pOrd, *p, (char *)(p + 1) );
298
299     /* Retrieve type info string */
300     return *(LPCSTR *)((LPBYTE)PTR_SEG_OFF_TO_LIN( frame->module_cs, frame->callfrom_ip ) + 4);
301 }
302
303
304 /**********************************************************************
305  *          BUILTIN_DefaultIntHandler
306  *
307  * Default interrupt handler.
308  */
309 void WINAPI BUILTIN_DefaultIntHandler( CONTEXT86 *context )
310 {
311     WORD ordinal;
312     char name[80];
313     BUILTIN_GetEntryPoint16( CURRENT_STACK16, name, &ordinal );
314     INT_BARF( context, ordinal - FIRST_INTERRUPT_ORDINAL );
315 }
316