4 * Copyright 1996 Alexandre Julliard
11 #include "wine/winbase16.h"
12 #include "wine/winestring.h"
13 #include "builtin32.h"
19 #include "stackframe.h"
26 DEFAULT_DEBUG_CHANNEL(module)
28 /* Built-in modules descriptors */
29 /* Don't change these structures! (see tools/build.c) */
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 */
42 const WIN16_DESCRIPTOR *descr; /* DLL descriptor */
43 int flags; /* flags (see below) */
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 */
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 },
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
155 static HMODULE16 BUILTIN_DoLoadModule16( const WIN16_DESCRIPTOR *descr )
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;
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 );
200 /***********************************************************************
203 * Load all built-in modules marked as 'always used'.
205 BOOL BUILTIN_Init(void)
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 );
227 INT_SetPMHandler( vector, proc );
236 /***********************************************************************
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 0;
256 if ((table->flags & DLL_FLAG_NOT_USED) && !force) return 0;
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];
277 if (!(pModule = NE_GetPtr( FarGetOwner16( GlobalHandle16(cs) ))))
282 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
283 entry = (ET_ENTRY *)((BYTE *)bundle+6);
285 for (i = bundle->first + 1; i < bundle->last; i++)
287 if ((entry->offs <= ip)
288 && (entry->type == 1) /* code segment ? */
289 && (entry->offs >= max_offset))
291 max_offset = entry->offs;
296 } while ( (bundle->next)
297 && (bundle = (ET_BUNDLE *)((BYTE *)pModule+bundle->next)));
299 /* Search for the name in the resident names table */
300 /* (built-in modules have no non-resident table) */
302 p = (BYTE *)pModule + pModule->name_table;
305 p += *p + 1 + sizeof(WORD);
306 if (*(WORD *)(p + *p + 1) == *pOrd) break;
309 sprintf( buffer, "%.*s.%d: %.*s",
310 *((BYTE *)pModule + pModule->name_table),
311 (char *)pModule + pModule->name_table + 1,
312 *pOrd, *p, (char *)(p + 1) );
317 /**********************************************************************
318 * BUILTIN_DefaultIntHandler
320 * Default interrupt handler.
322 void BUILTIN_DefaultIntHandler( CONTEXT *context )
325 STACK16FRAME *frame = CURRENT_STACK16;
326 BUILTIN_GetEntryPoint16( frame->entry_cs, frame->entry_ip, &ordinal );
327 INT_BARF( context, ordinal - FIRST_INTERRUPT_ORDINAL );