4 * Copyright 1996 Alexandre Julliard
11 #include "wine/winbase16.h"
12 #include "wine/winestring.h"
13 #include "builtin32.h"
19 #include "stackframe.h"
26 /* Built-in modules descriptors */
27 /* Don't change these structures! (see tools/build.c) */
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 */
40 const WIN16_DESCRIPTOR *descr; /* DLL descriptor */
41 int flags; /* flags (see below) */
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 */
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;
93 /* Table of all built-in DLLs */
95 static BUILTIN16_DLL BuiltinDLLs[] =
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 },
143 /* Ordinal number for interrupt 0 handler in WPROCS.DLL */
144 #define FIRST_INTERRUPT_ORDINAL 100
147 /***********************************************************************
148 * BUILTIN_DoLoadModule16
150 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule
153 static HMODULE16 BUILTIN_DoLoadModule16( const WIN16_DESCRIPTOR *descr )
157 SEGTABLEENTRY *pSegTable;
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 );
165 TRACE(module, "Built-in %s: hmodule=%04x\n",
166 descr->name, hModule );
167 pModule = (NE_MODULE *)GlobalLock16( hModule );
168 pModule->self = hModule;
170 /* Allocate the code segment */
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;
179 /* Allocate the data segment */
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 );
193 NE_RegisterModule( pModule );
198 /***********************************************************************
201 * Load all built-in modules marked as 'always used'.
203 BOOL BUILTIN_Init(void)
209 fnBUILTIN_LoadModule = BUILTIN_LoadModule;
211 for (dll = BuiltinDLLs; dll->descr; dll++)
213 if (dll->flags & DLL_FLAG_ALWAYS_USED)
214 if (!BUILTIN_DoLoadModule16( dll->descr )) return FALSE;
217 /* Set interrupt vectors from entry points in WPROCS.DLL */
219 hModule = GetModuleHandle16( "WPROCS" );
220 for (vector = 0; vector < 256; vector++)
222 FARPROC16 proc = NE_GetEntryPoint( hModule,
223 FIRST_INTERRUPT_ORDINAL + vector );
225 INT_SetPMHandler( vector, proc );
234 /***********************************************************************
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.
240 HMODULE16 BUILTIN_LoadModule( LPCSTR name, BOOL force )
242 BUILTIN16_DLL *table;
243 char dllname[16], *p;
245 /* Fix the name in case we have a full path and extension */
247 if ((p = strrchr( name, '\\' ))) name = p + 1;
248 lstrcpynA( dllname, name, sizeof(dllname) );
249 if ((p = strrchr( dllname, '.' ))) *p = '\0';
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;
256 return BUILTIN_DoLoadModule16( table->descr );
260 /***********************************************************************
261 * BUILTIN_GetEntryPoint16
263 * Return the ordinal and name corresponding to a CS:IP address.
264 * This is used only by relay debugging.
266 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
268 static char buffer[80];
269 WORD ordinal, i, max_offset;
273 if (!(pModule = NE_GetPtr( FarGetOwner16( GlobalHandle16(cs) ))))
276 /* Search for the ordinal */
278 p = (BYTE *)pModule + pModule->entry_table;
290 case 1: /* code segment */
296 if ((*(WORD *)p <= ip) && (*(WORD *)p >= max_offset))
298 max_offset = *(WORD *)p;
305 case 0xff: /* moveable (should not happen in built-in modules) */
306 TRACE( relay, "Built-in module has moveable entry\n" );
310 default: /* other segment */
317 /* Search for the name in the resident names table */
318 /* (built-in modules have no non-resident table) */
320 p = (BYTE *)pModule + pModule->name_table;
323 p += *p + 1 + sizeof(WORD);
324 if (*(WORD *)(p + *p + 1) == *pOrd) break;
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) );
335 /**********************************************************************
336 * BUILTIN_DefaultIntHandler
338 * Default interrupt handler.
340 void BUILTIN_DefaultIntHandler( CONTEXT *context )
343 STACK16FRAME *frame = CURRENT_STACK16;
344 BUILTIN_GetEntryPoint16( frame->entry_cs, frame->entry_ip, &ordinal );
345 INT_BARF( context, ordinal - FIRST_INTERRUPT_ORDINAL );
349 /***********************************************************************
350 * BUILTIN_ParseDLLOptions
352 * Set runtime DLL usage flags
354 BOOL BUILTIN_ParseDLLOptions( char *str )
363 while (*str && (*str==',' || isspace(*str))) str++;
365 *last = '\0'; /* cut off garbage at end at */
368 if ((*str != '+') && (*str != '-')) return FALSE;
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++)
375 if (!lstrncmpiA( str, dll->descr->name, (int)(p - str) ))
377 if (dll->descr->name[(int)(p-str)]) /* only partial match */
381 if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
382 dll->flags |= DLL_FLAG_NOT_USED;
384 else dll->flags &= ~DLL_FLAG_NOT_USED;
389 /* not found, but could get handled by BUILTIN32_, so move last */
393 /* handled. cut out the "[+-]DLL," string, so it isn't handled
397 memcpy(last,p,strlen(p)+1);
409 /***********************************************************************
412 * Print the list of built-in DLLs that can be disabled.
414 void BUILTIN_PrintDLLs(void)
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++)
423 if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
424 MSG("%-9s%c", dll->descr->name,
425 ((++i) % 8) ? ' ' : '\n' );
428 BUILTIN32_PrintDLLs();