Started implementing Enhanced MetaFile driver.
[wine.git] / if1632 / builtin.c
bloba986ff6c5b2c0d3b68dbbe18f1a8463027fe5ee6
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 "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"
26 DEFAULT_DEBUG_CHANNEL(module)
28 /* Built-in modules descriptors */
29 /* Don't change these structures! (see tools/build.c) */
31 typedef struct
33 const char *name; /* DLL name */
34 void *module_start; /* 32-bit address of the module data */
35 int module_size; /* Size of the module data */
36 const BYTE *code_start; /* 32-bit address of DLL code */
37 const BYTE *data_start; /* 32-bit address of DLL data */
38 } WIN16_DESCRIPTOR;
40 typedef struct
42 const WIN16_DESCRIPTOR *descr; /* DLL descriptor */
43 int flags; /* flags (see below) */
44 } BUILTIN16_DLL;
46 /* DLL flags */
47 #define DLL_FLAG_NOT_USED 0x01 /* Use original Windows DLL if possible */
48 #define DLL_FLAG_ALWAYS_USED 0x02 /* Always use built-in DLL */
50 /* 16-bit DLLs */
52 extern const WIN16_DESCRIPTOR AVIFILE_Descriptor;
53 extern const WIN16_DESCRIPTOR COMM_Descriptor;
54 extern const WIN16_DESCRIPTOR COMMDLG_Descriptor;
55 extern const WIN16_DESCRIPTOR COMPOBJ_Descriptor;
56 extern const WIN16_DESCRIPTOR DDEML_Descriptor;
57 extern const WIN16_DESCRIPTOR DISPDIB_Descriptor;
58 extern const WIN16_DESCRIPTOR DISPLAY_Descriptor;
59 extern const WIN16_DESCRIPTOR GDI_Descriptor;
60 extern const WIN16_DESCRIPTOR KERNEL_Descriptor;
61 extern const WIN16_DESCRIPTOR KEYBOARD_Descriptor;
62 extern const WIN16_DESCRIPTOR LZEXPAND_Descriptor;
63 extern const WIN16_DESCRIPTOR MMSYSTEM_Descriptor;
64 extern const WIN16_DESCRIPTOR MOUSE_Descriptor;
65 extern const WIN16_DESCRIPTOR MSACM_Descriptor;
66 extern const WIN16_DESCRIPTOR MSVIDEO_Descriptor;
67 extern const WIN16_DESCRIPTOR OLE2CONV_Descriptor;
68 extern const WIN16_DESCRIPTOR OLE2DISP_Descriptor;
69 extern const WIN16_DESCRIPTOR OLE2NLS_Descriptor;
70 extern const WIN16_DESCRIPTOR OLE2PROX_Descriptor;
71 extern const WIN16_DESCRIPTOR OLE2THK_Descriptor;
72 extern const WIN16_DESCRIPTOR OLE2_Descriptor;
73 extern const WIN16_DESCRIPTOR OLECLI_Descriptor;
74 extern const WIN16_DESCRIPTOR OLESVR_Descriptor;
75 extern const WIN16_DESCRIPTOR RASAPI16_Descriptor;
76 extern const WIN16_DESCRIPTOR SHELL_Descriptor;
77 extern const WIN16_DESCRIPTOR SOUND_Descriptor;
78 extern const WIN16_DESCRIPTOR STORAGE_Descriptor;
79 extern const WIN16_DESCRIPTOR STRESS_Descriptor;
80 extern const WIN16_DESCRIPTOR SYSTEM_Descriptor;
81 extern const WIN16_DESCRIPTOR TOOLHELP_Descriptor;
82 extern const WIN16_DESCRIPTOR TYPELIB_Descriptor;
83 extern const WIN16_DESCRIPTOR USER_Descriptor;
84 extern const WIN16_DESCRIPTOR VER_Descriptor;
85 extern const WIN16_DESCRIPTOR W32SYS_Descriptor;
86 extern const WIN16_DESCRIPTOR WIN32S16_Descriptor;
87 extern const WIN16_DESCRIPTOR WIN87EM_Descriptor;
88 extern const WIN16_DESCRIPTOR WINASPI_Descriptor;
89 extern const WIN16_DESCRIPTOR WINDEBUG_Descriptor;
90 extern const WIN16_DESCRIPTOR WINEPS_Descriptor;
91 extern const WIN16_DESCRIPTOR WING_Descriptor;
92 extern const WIN16_DESCRIPTOR WINSOCK_Descriptor;
93 extern const WIN16_DESCRIPTOR WPROCS_Descriptor;
95 /* Table of all built-in DLLs */
97 static BUILTIN16_DLL BuiltinDLLs[] =
99 { &KERNEL_Descriptor, 0 },
100 { &USER_Descriptor, 0 },
101 { &GDI_Descriptor, 0 },
102 { &SYSTEM_Descriptor, DLL_FLAG_ALWAYS_USED },
103 { &DISPLAY_Descriptor, DLL_FLAG_ALWAYS_USED },
104 { &WPROCS_Descriptor, DLL_FLAG_ALWAYS_USED },
105 { &WINDEBUG_Descriptor, DLL_FLAG_NOT_USED },
106 { &AVIFILE_Descriptor, DLL_FLAG_NOT_USED },
107 { &COMMDLG_Descriptor, DLL_FLAG_NOT_USED },
108 { &COMPOBJ_Descriptor, DLL_FLAG_NOT_USED },
109 { &DDEML_Descriptor, DLL_FLAG_NOT_USED },
110 { &DISPDIB_Descriptor, 0 },
111 { &KEYBOARD_Descriptor, 0 },
112 { &COMM_Descriptor, 0 },
113 { &LZEXPAND_Descriptor, 0 },
114 { &MMSYSTEM_Descriptor, 0 },
115 { &MOUSE_Descriptor, 0 },
116 { &MSACM_Descriptor, 0 },
117 { &MSVIDEO_Descriptor, 0 },
118 { &OLE2CONV_Descriptor, DLL_FLAG_NOT_USED },
119 { &OLE2DISP_Descriptor, DLL_FLAG_NOT_USED },
120 { &OLE2NLS_Descriptor, DLL_FLAG_NOT_USED },
121 { &OLE2PROX_Descriptor, DLL_FLAG_NOT_USED },
122 { &OLE2THK_Descriptor, DLL_FLAG_NOT_USED },
123 { &OLE2_Descriptor, DLL_FLAG_NOT_USED },
124 { &OLECLI_Descriptor, DLL_FLAG_NOT_USED },
125 { &OLESVR_Descriptor, DLL_FLAG_NOT_USED },
126 { &RASAPI16_Descriptor, 0 },
127 { &SHELL_Descriptor, 0 },
128 { &SOUND_Descriptor, 0 },
129 { &STORAGE_Descriptor, DLL_FLAG_NOT_USED },
130 { &STRESS_Descriptor, 0 },
131 { &TOOLHELP_Descriptor, 0 },
132 { &TYPELIB_Descriptor, DLL_FLAG_NOT_USED },
133 { &VER_Descriptor, 0 },
134 { &W32SYS_Descriptor, DLL_FLAG_NOT_USED },
135 { &WIN32S16_Descriptor, DLL_FLAG_NOT_USED },
136 { &WIN87EM_Descriptor, DLL_FLAG_NOT_USED },
137 { &WINASPI_Descriptor, 0 },
138 { &WINEPS_Descriptor, DLL_FLAG_ALWAYS_USED },
139 { &WING_Descriptor, 0 },
140 { &WINSOCK_Descriptor, 0 },
141 /* Last entry */
142 { NULL, 0 }
145 /* Ordinal number for interrupt 0 handler in WPROCS.DLL */
146 #define FIRST_INTERRUPT_ORDINAL 100
149 /***********************************************************************
150 * BUILTIN_DoLoadModule16
152 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule
153 * and BUILTIN_Init.
155 static HMODULE16 BUILTIN_DoLoadModule16( const WIN16_DESCRIPTOR *descr )
157 NE_MODULE *pModule;
158 int minsize;
159 SEGTABLEENTRY *pSegTable;
161 HMODULE16 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
162 descr->module_size, 0,
163 FALSE, FALSE, FALSE, NULL );
164 if (!hModule) return 0;
165 FarSetOwner16( hModule, hModule );
167 TRACE(module, "Built-in %s: hmodule=%04x\n",
168 descr->name, hModule );
169 pModule = (NE_MODULE *)GlobalLock16( hModule );
170 pModule->self = hModule;
172 /* Allocate the code segment */
174 pSegTable = NE_SEG_TABLE( pModule );
175 pSegTable->hSeg = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
176 pSegTable->minsize, hModule,
177 TRUE, TRUE, FALSE, NULL );
178 if (!pSegTable->hSeg) return 0;
179 pSegTable++;
181 /* Allocate the data segment */
183 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
184 minsize += pModule->heap_size;
185 if (minsize > 0x10000) minsize = 0x10000;
186 pSegTable->hSeg = GLOBAL_Alloc( GMEM_FIXED, minsize,
187 hModule, FALSE, FALSE, FALSE );
188 if (!pSegTable->hSeg) return 0;
189 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->hSeg ),
190 descr->data_start, pSegTable->minsize);
191 if (pModule->heap_size)
192 LocalInit16( GlobalHandleToSel16(pSegTable->hSeg),
193 pSegTable->minsize, minsize );
195 NE_RegisterModule( pModule );
196 return hModule;
200 /***********************************************************************
201 * BUILTIN_Init
203 * Load all built-in modules marked as 'always used'.
205 BOOL BUILTIN_Init(void)
207 BUILTIN16_DLL *dll;
208 WORD vector;
209 HMODULE16 hModule;
211 fnBUILTIN_LoadModule = BUILTIN_LoadModule;
213 for (dll = BuiltinDLLs; dll->descr; dll++)
215 if (dll->flags & DLL_FLAG_ALWAYS_USED)
216 if (!BUILTIN_DoLoadModule16( dll->descr )) return FALSE;
219 /* Set interrupt vectors from entry points in WPROCS.DLL */
221 hModule = GetModuleHandle16( "WPROCS" );
222 for (vector = 0; vector < 256; vector++)
224 FARPROC16 proc = NE_GetEntryPoint( hModule,
225 FIRST_INTERRUPT_ORDINAL + vector );
226 assert(proc);
227 INT_SetPMHandler( vector, proc );
230 SNOOP16_Init();
232 return TRUE;
236 /***********************************************************************
237 * BUILTIN_LoadModule
239 * Load a built-in module. If the 'force' parameter is FALSE, we only
240 * load the module if it has not been disabled via the -dll option.
242 HMODULE16 BUILTIN_LoadModule( LPCSTR name, BOOL force )
244 BUILTIN16_DLL *table;
245 char dllname[16], *p;
247 /* Fix the name in case we have a full path and extension */
249 if ((p = strrchr( name, '\\' ))) name = p + 1;
250 lstrcpynA( dllname, name, sizeof(dllname) );
251 if ((p = strrchr( dllname, '.' ))) *p = '\0';
253 for (table = BuiltinDLLs; table->descr; table++)
254 if (!lstrcmpiA( table->descr->name, dllname )) break;
255 if (!table->descr) return (HMODULE16)2;
256 if ((table->flags & DLL_FLAG_NOT_USED) && !force) return (HMODULE16)2;
258 return BUILTIN_DoLoadModule16( table->descr );
262 /***********************************************************************
263 * BUILTIN_GetEntryPoint16
265 * Return the ordinal and name corresponding to a CS:IP address.
266 * This is used only by relay debugging.
268 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
270 static char buffer[80];
271 WORD i, max_offset;
272 register BYTE *p;
273 NE_MODULE *pModule;
274 ET_BUNDLE *bundle;
275 ET_ENTRY *entry;
277 if (!(pModule = NE_GetPtr( FarGetOwner16( GlobalHandle16(cs) ))))
278 return NULL;
280 max_offset = 0;
281 *pOrd = 0;
282 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
285 entry = (ET_ENTRY *)((BYTE *)bundle+6);
286 for (i = bundle->first + 1; i <= bundle->last; i++)
288 if ((entry->offs <= ip)
289 && (entry->segnum == 1) /* code segment ? */
290 && (entry->offs >= max_offset))
292 max_offset = entry->offs;
293 *pOrd = i;
295 entry++;
297 } while ( (bundle->next)
298 && (bundle = (ET_BUNDLE *)((BYTE *)pModule+bundle->next)));
300 /* Search for the name in the resident names table */
301 /* (built-in modules have no non-resident table) */
303 p = (BYTE *)pModule + pModule->name_table;
304 while (*p)
306 p += *p + 1 + sizeof(WORD);
307 if (*(WORD *)(p + *p + 1) == *pOrd) break;
310 sprintf( buffer, "%.*s.%d: %.*s",
311 *((BYTE *)pModule + pModule->name_table),
312 (char *)pModule + pModule->name_table + 1,
313 *pOrd, *p, (char *)(p + 1) );
314 return buffer;
318 /**********************************************************************
319 * BUILTIN_DefaultIntHandler
321 * Default interrupt handler.
323 void BUILTIN_DefaultIntHandler( CONTEXT *context )
325 WORD ordinal;
326 STACK16FRAME *frame = CURRENT_STACK16;
327 BUILTIN_GetEntryPoint16( frame->entry_cs, frame->entry_ip, &ordinal );
328 INT_BARF( context, ordinal - FIRST_INTERRUPT_ORDINAL );