Release 970202
[wine/multimedia.git] / if1632 / builtin.c
bloba023ef37c6344f47c60fc6bc7b83342412d26008
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 "windows.h"
11 #include "gdi.h"
12 #include "global.h"
13 #include "module.h"
14 #include "miscemu.h"
15 #include "neexe.h"
16 #include "stackframe.h"
17 #include "user.h"
18 #include "stddebug.h"
19 #include "debug.h"
21 /* Built-in modules descriptors */
22 /* Don't change these structures! (see tools/build.c) */
24 typedef struct
26 const BYTE *code_start; /* 32-bit address of DLL code */
27 const BYTE *data_start; /* 32-bit address of DLL data */
28 } WIN16_DESCRIPTOR;
30 typedef struct
32 int base; /* Ordinal base */
33 int size; /* Number of functions */
34 const void *code_start; /* Start of DLL code */
35 const void **functions; /* Pointer to functions table */
36 const char * const *names; /* Pointer to names table */
37 } WIN32_DESCRIPTOR;
39 typedef struct
41 const char *name; /* DLL name */
42 void *module_start; /* 32-bit address of the module data */
43 int module_size; /* Size of the module data */
44 union
46 WIN16_DESCRIPTOR win16; /* Descriptor for Win16 DLL */
47 WIN32_DESCRIPTOR win32; /* Descriptor for Win32 DLL */
48 } u;
49 } DLL_DESCRIPTOR;
51 typedef struct
53 const DLL_DESCRIPTOR *descr; /* DLL descriptor */
54 int flags; /* flags (see below) */
55 } BUILTIN_DLL;
58 /* DLL flags */
59 #define DLL_FLAG_NOT_USED 0x01 /* Use original Windows DLL if possible */
60 #define DLL_FLAG_ALWAYS_USED 0x02 /* Always use built-in DLL */
61 #define DLL_FLAG_WIN32 0x04 /* DLL is a Win32 DLL */
63 /* 16-bit DLLs */
65 extern const DLL_DESCRIPTOR KERNEL_Descriptor;
66 extern const DLL_DESCRIPTOR USER_Descriptor;
67 extern const DLL_DESCRIPTOR GDI_Descriptor;
68 extern const DLL_DESCRIPTOR WIN87EM_Descriptor;
69 extern const DLL_DESCRIPTOR MMSYSTEM_Descriptor;
70 extern const DLL_DESCRIPTOR SHELL_Descriptor;
71 extern const DLL_DESCRIPTOR SOUND_Descriptor;
72 extern const DLL_DESCRIPTOR KEYBOARD_Descriptor;
73 extern const DLL_DESCRIPTOR WINSOCK_Descriptor;
74 extern const DLL_DESCRIPTOR STRESS_Descriptor;
75 extern const DLL_DESCRIPTOR SYSTEM_Descriptor;
76 extern const DLL_DESCRIPTOR TOOLHELP_Descriptor;
77 extern const DLL_DESCRIPTOR MOUSE_Descriptor;
78 extern const DLL_DESCRIPTOR COMMDLG_Descriptor;
79 extern const DLL_DESCRIPTOR OLE2_Descriptor;
80 extern const DLL_DESCRIPTOR OLE2CONV_Descriptor;
81 extern const DLL_DESCRIPTOR OLE2DISP_Descriptor;
82 extern const DLL_DESCRIPTOR OLE2NLS_Descriptor;
83 extern const DLL_DESCRIPTOR OLE2PROX_Descriptor;
84 extern const DLL_DESCRIPTOR OLECLI_Descriptor;
85 extern const DLL_DESCRIPTOR OLESVR_Descriptor;
86 extern const DLL_DESCRIPTOR COMPOBJ_Descriptor;
87 extern const DLL_DESCRIPTOR STORAGE_Descriptor;
88 extern const DLL_DESCRIPTOR WPROCS_Descriptor;
89 extern const DLL_DESCRIPTOR DDEML_Descriptor;
90 extern const DLL_DESCRIPTOR LZEXPAND_Descriptor;
91 extern const DLL_DESCRIPTOR VER_Descriptor;
92 extern const DLL_DESCRIPTOR W32SYS_Descriptor;
93 extern const DLL_DESCRIPTOR WING_Descriptor;
95 /* 32-bit DLLs */
97 extern const DLL_DESCRIPTOR ADVAPI32_Descriptor;
98 extern const DLL_DESCRIPTOR COMCTL32_Descriptor;
99 extern const DLL_DESCRIPTOR COMDLG32_Descriptor;
100 extern const DLL_DESCRIPTOR CRTDLL_Descriptor;
101 extern const DLL_DESCRIPTOR OLE32_Descriptor;
102 extern const DLL_DESCRIPTOR GDI32_Descriptor;
103 extern const DLL_DESCRIPTOR KERNEL32_Descriptor;
104 extern const DLL_DESCRIPTOR LZ32_Descriptor;
105 extern const DLL_DESCRIPTOR MPR_Descriptor;
106 extern const DLL_DESCRIPTOR NTDLL_Descriptor;
107 extern const DLL_DESCRIPTOR SHELL32_Descriptor;
108 extern const DLL_DESCRIPTOR USER32_Descriptor;
109 extern const DLL_DESCRIPTOR VERSION_Descriptor;
110 extern const DLL_DESCRIPTOR WINMM_Descriptor;
111 extern const DLL_DESCRIPTOR WINSPOOL_Descriptor;
112 extern const DLL_DESCRIPTOR WSOCK32_Descriptor;
114 /* Table of all built-in DLLs */
116 static BUILTIN_DLL BuiltinDLLs[] =
118 /* Win16 DLLs */
119 { &KERNEL_Descriptor, DLL_FLAG_ALWAYS_USED },
120 { &USER_Descriptor, DLL_FLAG_ALWAYS_USED },
121 { &GDI_Descriptor, DLL_FLAG_ALWAYS_USED },
122 { &WIN87EM_Descriptor, DLL_FLAG_NOT_USED },
123 { &SHELL_Descriptor, 0 },
124 { &SOUND_Descriptor, 0 },
125 { &KEYBOARD_Descriptor, 0 },
126 { &WINSOCK_Descriptor, 0 },
127 { &STRESS_Descriptor, 0 },
128 { &MMSYSTEM_Descriptor, 0 },
129 { &SYSTEM_Descriptor, 0 },
130 { &TOOLHELP_Descriptor, 0 },
131 { &MOUSE_Descriptor, 0 },
132 { &COMMDLG_Descriptor, DLL_FLAG_NOT_USED },
133 { &OLE2_Descriptor, DLL_FLAG_NOT_USED },
134 { &OLE2CONV_Descriptor, DLL_FLAG_NOT_USED },
135 { &OLE2DISP_Descriptor, DLL_FLAG_NOT_USED },
136 { &OLE2NLS_Descriptor, DLL_FLAG_NOT_USED },
137 { &OLE2PROX_Descriptor, DLL_FLAG_NOT_USED },
138 { &OLECLI_Descriptor, DLL_FLAG_NOT_USED },
139 { &OLESVR_Descriptor, DLL_FLAG_NOT_USED },
140 { &COMPOBJ_Descriptor, DLL_FLAG_NOT_USED },
141 { &STORAGE_Descriptor, DLL_FLAG_NOT_USED },
142 { &WPROCS_Descriptor, DLL_FLAG_ALWAYS_USED },
143 { &DDEML_Descriptor, DLL_FLAG_NOT_USED },
144 { &LZEXPAND_Descriptor, 0 },
145 { &VER_Descriptor, 0 },
146 { &W32SYS_Descriptor, 0 },
147 { &WING_Descriptor, 0 },
148 /* Win32 DLLs */
149 { &ADVAPI32_Descriptor, 0 },
150 { &COMCTL32_Descriptor, DLL_FLAG_NOT_USED },
151 { &COMDLG32_Descriptor, 0 },
152 { &CRTDLL_Descriptor, 0 },
153 { &OLE32_Descriptor, 0 },
154 { &GDI32_Descriptor, 0 },
155 { &KERNEL32_Descriptor, DLL_FLAG_ALWAYS_USED },
156 { &LZ32_Descriptor, 0 },
157 { &MPR_Descriptor, 0 },
158 { &NTDLL_Descriptor, 0 },
159 { &SHELL32_Descriptor, 0 },
160 { &USER32_Descriptor, 0 },
161 { &VERSION_Descriptor, 0 },
162 { &WINMM_Descriptor, 0 },
163 { &WINSPOOL_Descriptor, 0 },
164 { &WSOCK32_Descriptor, 0 },
165 /* Last entry */
166 { NULL, 0 }
169 /* Ordinal number for interrupt 0 handler in WPROCS.DLL */
170 #define FIRST_INTERRUPT_ORDINAL 100
172 /***********************************************************************
173 * BUILTIN_Init
175 * Load all built-in modules marked as 'always used'.
177 BOOL16 BUILTIN_Init(void)
179 BUILTIN_DLL *dll;
180 NE_MODULE *pModule;
181 WORD vector;
182 HMODULE16 hModule;
184 for (dll = BuiltinDLLs; dll->descr; dll++)
185 if (dll->flags & DLL_FLAG_ALWAYS_USED)
186 if (!BUILTIN_LoadModule(dll->descr->name, TRUE)) return FALSE;
188 /* Set the USER and GDI heap selectors */
190 pModule = MODULE_GetPtr( GetModuleHandle( "USER" ));
191 USER_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
192 pModule = MODULE_GetPtr( GetModuleHandle( "GDI" ));
193 GDI_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
195 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
197 hModule = GetModuleHandle( "KERNEL" );
198 MODULE_SetEntryPoint( hModule, 178, GetWinFlags() );
200 /* Initialize the real-mode selector entry points */
202 #define SET_ENTRY_POINT(num,addr) \
203 MODULE_SetEntryPoint( hModule, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
204 DOSMEM_dosmem+(addr), 0x10000, hModule, \
205 FALSE, FALSE, FALSE, NULL ))
207 SET_ENTRY_POINT( 174, 0xa0000 ); /* KERNEL.174: __A000H */
208 SET_ENTRY_POINT( 181, 0xb0000 ); /* KERNEL.181: __B000H */
209 SET_ENTRY_POINT( 182, 0xb8000 ); /* KERNEL.182: __B800H */
210 SET_ENTRY_POINT( 195, 0xc0000 ); /* KERNEL.195: __C000H */
211 SET_ENTRY_POINT( 179, 0xd0000 ); /* KERNEL.179: __D000H */
212 SET_ENTRY_POINT( 190, 0xe0000 ); /* KERNEL.190: __E000H */
213 SET_ENTRY_POINT( 173, 0xf0000 ); /* KERNEL.173: __ROMBIOS */
214 SET_ENTRY_POINT( 194, 0xf0000 ); /* KERNEL.194: __F000H */
215 MODULE_SetEntryPoint(hModule,193,DOSMEM_BiosSeg); /* KERNEL.193: __0040H */
216 #undef SET_ENTRY_POINT
218 /* Set interrupt vectors from entry points in WPROCS.DLL */
220 hModule = GetModuleHandle( "WPROCS" );
221 for (vector = 0; vector < 256; vector++)
223 FARPROC16 proc = MODULE_GetEntryPoint( hModule,
224 FIRST_INTERRUPT_ORDINAL+vector);
225 assert(proc);
226 INT_SetHandler( vector, proc );
229 return TRUE;
233 /***********************************************************************
234 * BUILTIN_LoadModule
236 * Load a built-in module. If the 'force' parameter is FALSE, we only
237 * load the module if it has not been disabled via the -dll option.
239 HMODULE16 BUILTIN_LoadModule( LPCSTR name, BOOL16 force )
241 HMODULE16 hModule;
242 NE_MODULE *pModule;
243 BUILTIN_DLL *table;
244 char dllname[16], *p;
246 /* Fix the name in case we have a full path and extension */
248 if ((p = strrchr( name, '\\' ))) name = p + 1;
249 lstrcpyn32A( dllname, name, sizeof(dllname) );
250 if ((p = strrchr( dllname, '.' ))) *p = '\0';
252 for (table = BuiltinDLLs; table->descr; table++)
253 if (!lstrcmpi32A( table->descr->name, dllname )) break;
254 if (!table->descr) return 0;
255 if ((table->flags & DLL_FLAG_NOT_USED) && !force) return 0;
257 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, table->descr->module_start,
258 table->descr->module_size, 0,
259 FALSE, FALSE, FALSE, NULL );
260 if (!hModule) return 0;
261 FarSetOwner( hModule, hModule );
263 dprintf_module( stddeb, "Built-in %s: hmodule=%04x\n",
264 table->descr->name, hModule );
265 pModule = (NE_MODULE *)GlobalLock16( hModule );
266 pModule->self = hModule;
268 if (pModule->flags & NE_FFLAGS_WIN32)
270 pModule->pe_module = (PE_MODULE *)table;
271 table->flags |= DLL_FLAG_WIN32;
273 else /* Win16 module */
275 const WIN16_DESCRIPTOR *descr = &table->descr->u.win16;
276 int minsize;
278 /* Allocate the code segment */
280 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
281 pSegTable->selector = GLOBAL_CreateBlock(GMEM_FIXED, descr->code_start,
282 pSegTable->minsize, hModule,
283 TRUE, TRUE, FALSE, NULL );
284 if (!pSegTable->selector) return 0;
285 pSegTable++;
287 /* Allocate the data segment */
289 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
290 minsize += pModule->heap_size;
291 if (minsize > 0x10000) minsize = 0x10000;
292 pSegTable->selector = GLOBAL_Alloc( GMEM_FIXED, minsize,
293 hModule, FALSE, FALSE, FALSE );
294 if (!pSegTable->selector) return 0;
295 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->selector ),
296 descr->data_start, pSegTable->minsize);
297 if (pModule->heap_size)
298 LocalInit( pSegTable->selector, pSegTable->minsize, minsize );
301 MODULE_RegisterModule( pModule );
302 return hModule;
306 /***********************************************************************
307 * BUILTIN_GetEntryPoint16
309 * Return the ordinal and name corresponding to a CS:IP address.
310 * This is used only by relay debugging.
312 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
314 static char buffer[80];
315 WORD ordinal, i, max_offset;
316 register BYTE *p;
317 NE_MODULE *pModule;
319 if (!(pModule = MODULE_GetPtr( FarGetOwner( GlobalHandle16(cs) ))))
320 return NULL;
322 /* Search for the ordinal */
324 p = (BYTE *)pModule + pModule->entry_table;
325 max_offset = 0;
326 ordinal = 1;
327 *pOrd = 0;
328 while (*p)
330 switch(p[1])
332 case 0: /* unused */
333 ordinal += *p;
334 p += 2;
335 break;
336 case 1: /* code segment */
337 i = *p;
338 p += 2;
339 while (i-- > 0)
341 p++;
342 if ((*(WORD *)p <= ip) && (*(WORD *)p >= max_offset))
344 max_offset = *(WORD *)p;
345 *pOrd = ordinal;
347 p += 2;
348 ordinal++;
350 break;
351 case 0xff: /* moveable (should not happen in built-in modules) */
352 fprintf( stderr, "Built-in module has moveable entry\n" );
353 ordinal += *p;
354 p += 2 + *p * 6;
355 break;
356 default: /* other segment */
357 ordinal += *p;
358 p += 2 + *p * 3;
359 break;
363 /* Search for the name in the resident names table */
364 /* (built-in modules have no non-resident table) */
366 p = (BYTE *)pModule + pModule->name_table;
367 while (*p)
369 p += *p + 1 + sizeof(WORD);
370 if (*(WORD *)(p + *p + 1) == *pOrd) break;
373 sprintf( buffer, "%.*s.%d: %.*s",
374 *((BYTE *)pModule + pModule->name_table),
375 (char *)pModule + pModule->name_table + 1,
376 *pOrd, *p, (char *)(p + 1) );
377 return buffer;
381 /***********************************************************************
382 * BUILTIN_GetEntryPoint32
384 * Return the name of the DLL entry point corresponding
385 * to a relay entry point address. This is used only by relay debugging.
387 LPCSTR BUILTIN_GetEntryPoint32( void *relay )
389 static char buffer[80];
390 BUILTIN_DLL *dll;
391 const void **funcs;
392 int i;
394 /* First find the module */
396 for (dll = BuiltinDLLs; dll->descr; dll++)
397 if ((dll->flags & DLL_FLAG_WIN32) &&
398 (dll->descr->u.win32.code_start <= relay) &&
399 ((void *)dll->descr->u.win32.functions > relay))
400 break;
401 if (!dll->descr)
403 sprintf( buffer, "???.???: %08x", (UINT32)relay );
404 return buffer;
407 /* Now find the function */
409 relay = (BYTE *)relay - 11; /* The relay entry point is 11 bytes long */
410 funcs = dll->descr->u.win32.functions;
411 for (i = 0; i < dll->descr->u.win32.size;i++) if (*funcs++ == relay) break;
412 sprintf( buffer, "%s.%d: %s",
413 dll->descr->name, i, dll->descr->u.win32.names[i] );
414 return buffer;
418 /***********************************************************************
419 * BUILTIN_GetProcAddress32
421 * Implementation of GetProcAddress() for built-in Win32 modules.
422 * FIXME: this should be unified with the real GetProcAddress32().
424 FARPROC32 BUILTIN_GetProcAddress32( NE_MODULE *pModule, LPCSTR function )
426 BUILTIN_DLL *dll = (BUILTIN_DLL *)pModule->pe_module;
427 const WIN32_DESCRIPTOR *info = &dll->descr->u.win32;
429 if (!dll) return NULL;
431 if (HIWORD(function)) /* Find function by name */
433 int i;
435 dprintf_module( stddeb, "Looking for function %s in %s\n",
436 function, dll->descr->name );
437 for (i = 0; i < info->size; i++)
438 if (info->names[i] && !strcmp( function, info->names[i] ))
439 return (FARPROC32)info->functions[i];
441 else /* Find function by ordinal */
443 WORD ordinal = LOWORD(function);
444 dprintf_module( stddeb, "Looking for ordinal %d in %s\n",
445 ordinal, dll->descr->name );
446 if (ordinal && ordinal < info->size)
447 return (FARPROC32)info->functions[ordinal - info->base];
449 return NULL;
453 /**********************************************************************
454 * BUILTIN_DefaultIntHandler
456 * Default interrupt handler.
458 void BUILTIN_DefaultIntHandler( CONTEXT *context )
460 WORD ordinal;
461 STACK16FRAME *frame = CURRENT_STACK16;
462 BUILTIN_GetEntryPoint16( frame->entry_cs, frame->entry_ip, &ordinal );
463 INT_BARF( context, ordinal - FIRST_INTERRUPT_ORDINAL );
467 /***********************************************************************
468 * BUILTIN_ParseDLLOptions
470 * Set runtime DLL usage flags
472 BOOL16 BUILTIN_ParseDLLOptions( const char *str )
474 BUILTIN_DLL *dll;
475 const char *p;
477 while (*str)
479 while (*str && isspace(*str)) str++;
480 if (!*str) return TRUE;
481 if ((*str != '+') && (*str != '-')) return FALSE;
482 str++;
483 if (!(p = strchr( str, ',' ))) p = str + strlen(str);
484 while ((p > str) && isspace(p[-1])) p--;
485 if (p == str) return FALSE;
486 for (dll = BuiltinDLLs; dll->descr; dll++)
488 if (!lstrncmpi32A( str, dll->descr->name, (int)(p - str) ))
490 if (str[-1] == '-')
492 if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
493 dll->flags |= DLL_FLAG_NOT_USED;
495 else dll->flags &= ~DLL_FLAG_NOT_USED;
496 break;
499 if (!dll->descr) return FALSE;
500 str = p;
501 while (*str && (isspace(*str) || (*str == ','))) str++;
503 return TRUE;
507 /***********************************************************************
508 * BUILTIN_PrintDLLs
510 * Print the list of built-in DLLs that can be disabled.
512 void BUILTIN_PrintDLLs(void)
514 int i;
515 BUILTIN_DLL *dll;
517 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
519 if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
520 fprintf( stderr, "%-9s%c", dll->descr->name,
521 ((++i) % 8) ? ' ' : '\n' );
523 fprintf(stderr,"\n");
524 exit(1);