Release 960928
[wine/multimedia.git] / loader / builtin.c
blob0b67254b5137de314c37f463b485d290583f0d19
1 /*
2 * Built-in modules
4 * Copyright 1996 Alexandre Julliard
5 */
7 #ifndef WINELIB
9 #include <ctype.h>
10 #include <string.h>
11 #include "windows.h"
12 #include "gdi.h"
13 #include "global.h"
14 #include "module.h"
15 #include "neexe.h"
16 #include "user.h"
17 #include "stddebug.h"
18 #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;
94 /* 32-bit DLLs */
96 extern const DLL_DESCRIPTOR ADVAPI32_Descriptor;
97 extern const DLL_DESCRIPTOR COMCTL32_Descriptor;
98 extern const DLL_DESCRIPTOR COMDLG32_Descriptor;
99 extern const DLL_DESCRIPTOR CRTDLL_Descriptor;
100 extern const DLL_DESCRIPTOR OLE32_Descriptor;
101 extern const DLL_DESCRIPTOR GDI32_Descriptor;
102 extern const DLL_DESCRIPTOR KERNEL32_Descriptor;
103 extern const DLL_DESCRIPTOR LZ32_Descriptor;
104 extern const DLL_DESCRIPTOR MPR_Descriptor;
105 extern const DLL_DESCRIPTOR NTDLL_Descriptor;
106 extern const DLL_DESCRIPTOR SHELL32_Descriptor;
107 extern const DLL_DESCRIPTOR USER32_Descriptor;
108 extern const DLL_DESCRIPTOR VERSION_Descriptor;
109 extern const DLL_DESCRIPTOR WINMM_Descriptor;
110 extern const DLL_DESCRIPTOR WINSPOOL_Descriptor;
111 extern const DLL_DESCRIPTOR WSOCK32_Descriptor;
113 /* Table of all built-in DLLs */
115 static BUILTIN_DLL BuiltinDLLs[] =
117 /* Win16 DLLs */
118 { &KERNEL_Descriptor, DLL_FLAG_ALWAYS_USED },
119 { &USER_Descriptor, DLL_FLAG_ALWAYS_USED },
120 { &GDI_Descriptor, DLL_FLAG_ALWAYS_USED },
121 { &WIN87EM_Descriptor, DLL_FLAG_NOT_USED },
122 { &SHELL_Descriptor, 0 },
123 { &SOUND_Descriptor, 0 },
124 { &KEYBOARD_Descriptor, 0 },
125 { &WINSOCK_Descriptor, 0 },
126 { &STRESS_Descriptor, 0 },
127 { &MMSYSTEM_Descriptor, 0 },
128 { &SYSTEM_Descriptor, 0 },
129 { &TOOLHELP_Descriptor, 0 },
130 { &MOUSE_Descriptor, 0 },
131 { &COMMDLG_Descriptor, DLL_FLAG_NOT_USED },
132 { &OLE2_Descriptor, DLL_FLAG_NOT_USED },
133 { &OLE2CONV_Descriptor, DLL_FLAG_NOT_USED },
134 { &OLE2DISP_Descriptor, DLL_FLAG_NOT_USED },
135 { &OLE2NLS_Descriptor, DLL_FLAG_NOT_USED },
136 { &OLE2PROX_Descriptor, DLL_FLAG_NOT_USED },
137 { &OLECLI_Descriptor, DLL_FLAG_NOT_USED },
138 { &OLESVR_Descriptor, DLL_FLAG_NOT_USED },
139 { &COMPOBJ_Descriptor, DLL_FLAG_NOT_USED },
140 { &STORAGE_Descriptor, DLL_FLAG_NOT_USED },
141 { &WPROCS_Descriptor, DLL_FLAG_ALWAYS_USED },
142 { &DDEML_Descriptor, DLL_FLAG_NOT_USED },
143 { &LZEXPAND_Descriptor, 0 },
144 { &VER_Descriptor, 0 },
145 { &W32SYS_Descriptor, 0 },
146 /* Win32 DLLs */
147 { &ADVAPI32_Descriptor, 0 },
148 { &COMCTL32_Descriptor, DLL_FLAG_NOT_USED },
149 { &COMDLG32_Descriptor, 0 },
150 { &CRTDLL_Descriptor, 0 },
151 { &OLE32_Descriptor, 0 },
152 { &GDI32_Descriptor, 0 },
153 { &KERNEL32_Descriptor, DLL_FLAG_ALWAYS_USED },
154 { &LZ32_Descriptor, 0 },
155 { &MPR_Descriptor, 0 },
156 { &NTDLL_Descriptor, 0 },
157 { &SHELL32_Descriptor, 0 },
158 { &USER32_Descriptor, 0 },
159 { &VERSION_Descriptor, 0 },
160 { &WINMM_Descriptor, 0 },
161 { &WINSPOOL_Descriptor, 0 },
162 { &WSOCK32_Descriptor, 0 },
163 /* Last entry */
164 { NULL, 0 }
168 /***********************************************************************
169 * BUILTIN_Init
171 * Load all built-in modules marked as 'always used'.
173 BOOL16 BUILTIN_Init(void)
175 BUILTIN_DLL *dll;
176 NE_MODULE *pModule;
178 for (dll = BuiltinDLLs; dll->descr; dll++)
179 if (dll->flags & DLL_FLAG_ALWAYS_USED)
180 if (!BUILTIN_LoadModule(dll->descr->name, TRUE)) return FALSE;
182 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
184 MODULE_SetEntryPoint( GetModuleHandle( "KERNEL" ), 178, GetWinFlags() );
186 /* Set the USER and GDI heap selectors */
188 pModule = MODULE_GetPtr( GetModuleHandle( "USER" ));
189 USER_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
190 pModule = MODULE_GetPtr( GetModuleHandle( "GDI" ));
191 GDI_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
193 return TRUE;
197 /***********************************************************************
198 * BUILTIN_LoadModule
200 * Load a built-in module. If the 'force' parameter is FALSE, we only
201 * load the module if it has not been disabled via the -dll option.
203 HMODULE16 BUILTIN_LoadModule( LPCSTR name, BOOL16 force )
205 HMODULE16 hModule;
206 NE_MODULE *pModule;
207 BUILTIN_DLL *table;
208 char dllname[16], *p;
210 /* Fix the name in case we have a full path and extension */
212 if ((p = strrchr( name, '\\' ))) name = p + 1;
213 lstrcpyn32A( dllname, name, sizeof(dllname) );
214 if ((p = strrchr( dllname, '.' ))) *p = '\0';
216 for (table = BuiltinDLLs; table->descr; table++)
217 if (!lstrcmpi32A( table->descr->name, dllname )) break;
218 if (!table->descr) return 0;
219 if ((table->flags & DLL_FLAG_NOT_USED) && !force) return 0;
221 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, table->descr->module_start,
222 table->descr->module_size, 0,
223 FALSE, FALSE, FALSE, NULL );
224 if (!hModule) return 0;
225 FarSetOwner( hModule, hModule );
227 dprintf_module( stddeb, "Built-in %s: hmodule=%04x\n",
228 table->descr->name, hModule );
229 pModule = (NE_MODULE *)GlobalLock16( hModule );
230 pModule->self = hModule;
232 if (pModule->flags & NE_FFLAGS_WIN32)
234 pModule->pe_module = (PE_MODULE *)table;
235 table->flags |= DLL_FLAG_WIN32;
237 else /* Win16 module */
239 const WIN16_DESCRIPTOR *descr = &table->descr->u.win16;
240 int minsize;
242 /* Allocate the code segment */
244 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
245 pSegTable->selector = GLOBAL_CreateBlock(GMEM_FIXED, descr->code_start,
246 pSegTable->minsize, hModule,
247 TRUE, TRUE, FALSE, NULL );
248 if (!pSegTable->selector) return 0;
249 pSegTable++;
251 /* Allocate the data segment */
253 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
254 minsize += pModule->heap_size;
255 if (minsize > 0x10000) minsize = 0x10000;
256 pSegTable->selector = GLOBAL_Alloc( GMEM_FIXED, minsize,
257 hModule, FALSE, FALSE, FALSE );
258 if (!pSegTable->selector) return 0;
259 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->selector ),
260 descr->data_start, pSegTable->minsize);
261 if (pModule->heap_size)
262 LocalInit( pSegTable->selector, pSegTable->minsize, minsize );
265 MODULE_RegisterModule( pModule );
266 return hModule;
270 /***********************************************************************
271 * BUILTIN_GetEntryPoint16
273 * Return the ordinal and name corresponding to a CS:IP address.
274 * This is used only by relay debugging.
276 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
278 static char buffer[80];
279 WORD ordinal, i, max_offset;
280 register BYTE *p;
281 NE_MODULE *pModule;
283 if (!(pModule = MODULE_GetPtr( FarGetOwner( GlobalHandle16(cs) ))))
284 return NULL;
286 /* Search for the ordinal */
288 p = (BYTE *)pModule + pModule->entry_table;
289 max_offset = 0;
290 ordinal = 1;
291 *pOrd = 0;
292 while (*p)
294 switch(p[1])
296 case 0: /* unused */
297 ordinal += *p;
298 p += 2;
299 break;
300 case 1: /* code segment */
301 i = *p;
302 p += 2;
303 while (i-- > 0)
305 p++;
306 if ((*(WORD *)p <= ip) && (*(WORD *)p >= max_offset))
308 max_offset = *(WORD *)p;
309 *pOrd = ordinal;
311 p += 2;
312 ordinal++;
314 break;
315 case 0xff: /* moveable (should not happen in built-in modules) */
316 fprintf( stderr, "Built-in module has moveable entry\n" );
317 ordinal += *p;
318 p += 2 + *p * 6;
319 break;
320 default: /* other segment */
321 ordinal += *p;
322 p += 2 + *p * 3;
323 break;
327 /* Search for the name in the resident names table */
328 /* (built-in modules have no non-resident table) */
330 p = (BYTE *)pModule + pModule->name_table;
331 while (*p)
333 p += *p + 1 + sizeof(WORD);
334 if (*(WORD *)(p + *p + 1) == *pOrd) break;
337 sprintf( buffer, "%.*s.%d: %.*s",
338 *((BYTE *)pModule + pModule->name_table),
339 (char *)pModule + pModule->name_table + 1,
340 *pOrd, *p, (char *)(p + 1) );
341 return buffer;
345 /***********************************************************************
346 * BUILTIN_GetEntryPoint32
348 * Return the name of the DLL entry point corresponding
349 * to a relay entry point address. This is used only by relay debugging.
351 LPCSTR BUILTIN_GetEntryPoint32( void *relay )
353 static char buffer[80];
354 BUILTIN_DLL *dll;
355 const void **funcs;
356 int i;
358 /* First find the module */
360 for (dll = BuiltinDLLs; dll->descr; dll++)
361 if ((dll->flags & DLL_FLAG_WIN32) &&
362 (dll->descr->u.win32.code_start <= relay) &&
363 ((void *)dll->descr->u.win32.functions > relay))
364 break;
365 if (!dll->descr)
367 sprintf( buffer, "???.???: %08x", (UINT32)relay );
368 return buffer;
371 /* Now find the function */
373 relay = (BYTE *)relay - 11; /* The relay entry point is 11 bytes long */
374 funcs = dll->descr->u.win32.functions;
375 for (i = 0; i < dll->descr->u.win32.size;i++) if (*funcs++ == relay) break;
376 sprintf( buffer, "%s.%d: %s",
377 dll->descr->name, i, dll->descr->u.win32.names[i] );
378 return buffer;
382 /***********************************************************************
383 * BUILTIN_GetProcAddress32
385 * Implementation of GetProcAddress() for built-in Win32 modules.
386 * FIXME: this should be unified with the real GetProcAddress32().
388 FARPROC32 BUILTIN_GetProcAddress32( NE_MODULE *pModule, LPCSTR function )
390 BUILTIN_DLL *dll = (BUILTIN_DLL *)pModule->pe_module;
391 const WIN32_DESCRIPTOR *info = &dll->descr->u.win32;
393 if (!dll) return NULL;
395 if (HIWORD(function)) /* Find function by name */
397 int i;
399 dprintf_module( stddeb, "Looking for function %s in %s\n",
400 function, dll->descr->name );
401 for (i = 0; i < info->size; i++)
402 if (info->names[i] && !strcmp( function, info->names[i] ))
403 return (FARPROC32)info->functions[i];
405 else /* Find function by ordinal */
407 WORD ordinal = LOWORD(function);
408 dprintf_module( stddeb, "Looking for ordinal %d in %s\n",
409 ordinal, dll->descr->name );
410 if (ordinal && ordinal < info->size)
411 return (FARPROC32)info->functions[ordinal - info->base];
413 return NULL;
417 /***********************************************************************
418 * BUILTIN_ParseDLLOptions
420 * Set runtime DLL usage flags
422 BOOL16 BUILTIN_ParseDLLOptions( const char *str )
424 BUILTIN_DLL *dll;
425 const char *p;
427 while (*str)
429 while (*str && isspace(*str)) str++;
430 if (!*str) return TRUE;
431 if ((*str != '+') && (*str != '-')) return FALSE;
432 str++;
433 if (!(p = strchr( str, ',' ))) p = str + strlen(str);
434 while ((p > str) && isspace(p[-1])) p--;
435 if (p == str) return FALSE;
436 for (dll = BuiltinDLLs; dll->descr; dll++)
438 if (!lstrncmpi32A( str, dll->descr->name, (int)(p - str) ))
440 if (str[-1] == '-')
442 if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
443 dll->flags |= DLL_FLAG_NOT_USED;
445 else dll->flags &= ~DLL_FLAG_NOT_USED;
446 break;
449 if (!dll->descr) return FALSE;
450 str = p;
451 while (*str && (isspace(*str) || (*str == ','))) str++;
453 return TRUE;
457 /***********************************************************************
458 * BUILTIN_PrintDLLs
460 * Print the list of built-in DLLs that can be disabled.
462 void BUILTIN_PrintDLLs(void)
464 int i;
465 BUILTIN_DLL *dll;
467 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
469 if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
470 fprintf( stderr, "%-9s%c", dll->descr->name,
471 ((++i) % 8) ? ' ' : '\n' );
473 fprintf(stderr,"\n");
474 exit(1);
477 #endif /* WINELIB */