Release 960717
[wine/multimedia.git] / loader / builtin.c
blob4717f66a6eab1f495b8b3f536b8edc565e83d142
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 **functions; /* Pointer to functions table */
35 const char * const *names; /* Pointer to names table */
36 } WIN32_DESCRIPTOR;
38 typedef struct
40 const char *name; /* DLL name */
41 void *module_start; /* 32-bit address of the module data */
42 int module_size; /* Size of the module data */
43 union
45 WIN16_DESCRIPTOR win16; /* Descriptor for Win16 DLL */
46 WIN32_DESCRIPTOR win32; /* Descriptor for Win32 DLL */
47 } u;
48 } DLL_DESCRIPTOR;
50 typedef struct
52 const DLL_DESCRIPTOR *descr; /* DLL descriptor */
53 int flags; /* flags (see below) */
54 } BUILTIN_DLL;
57 /* DLL flags */
58 #define DLL_FLAG_NOT_USED 0x01 /* Use original Windows DLL if possible */
59 #define DLL_FLAG_ALWAYS_USED 0x02 /* Always use built-in DLL */
60 #define DLL_FLAG_WIN32 0x04 /* DLL is a Win32 DLL */
62 /* 16-bit DLLs */
64 extern const DLL_DESCRIPTOR KERNEL_Descriptor;
65 extern const DLL_DESCRIPTOR USER_Descriptor;
66 extern const DLL_DESCRIPTOR GDI_Descriptor;
67 extern const DLL_DESCRIPTOR WIN87EM_Descriptor;
68 extern const DLL_DESCRIPTOR MMSYSTEM_Descriptor;
69 extern const DLL_DESCRIPTOR SHELL_Descriptor;
70 extern const DLL_DESCRIPTOR SOUND_Descriptor;
71 extern const DLL_DESCRIPTOR KEYBOARD_Descriptor;
72 extern const DLL_DESCRIPTOR WINSOCK_Descriptor;
73 extern const DLL_DESCRIPTOR STRESS_Descriptor;
74 extern const DLL_DESCRIPTOR SYSTEM_Descriptor;
75 extern const DLL_DESCRIPTOR TOOLHELP_Descriptor;
76 extern const DLL_DESCRIPTOR MOUSE_Descriptor;
77 extern const DLL_DESCRIPTOR COMMDLG_Descriptor;
78 extern const DLL_DESCRIPTOR OLE2_Descriptor;
79 extern const DLL_DESCRIPTOR OLE2CONV_Descriptor;
80 extern const DLL_DESCRIPTOR OLE2DISP_Descriptor;
81 extern const DLL_DESCRIPTOR OLE2NLS_Descriptor;
82 extern const DLL_DESCRIPTOR OLE2PROX_Descriptor;
83 extern const DLL_DESCRIPTOR OLECLI_Descriptor;
84 extern const DLL_DESCRIPTOR OLESVR_Descriptor;
85 extern const DLL_DESCRIPTOR COMPOBJ_Descriptor;
86 extern const DLL_DESCRIPTOR STORAGE_Descriptor;
87 extern const DLL_DESCRIPTOR WPROCS_Descriptor;
88 extern const DLL_DESCRIPTOR DDEML_Descriptor;
89 extern const DLL_DESCRIPTOR LZEXPAND_Descriptor;
90 extern const DLL_DESCRIPTOR VER_Descriptor;
91 extern const DLL_DESCRIPTOR W32SYS_Descriptor;
93 /* 32-bit DLLs */
95 extern const DLL_DESCRIPTOR ADVAPI32_Descriptor;
96 extern const DLL_DESCRIPTOR COMCTL32_Descriptor;
97 extern const DLL_DESCRIPTOR COMDLG32_Descriptor;
98 extern const DLL_DESCRIPTOR CRTDLL_Descriptor;
99 extern const DLL_DESCRIPTOR OLE32_Descriptor;
100 extern const DLL_DESCRIPTOR GDI32_Descriptor;
101 extern const DLL_DESCRIPTOR KERNEL32_Descriptor;
102 extern const DLL_DESCRIPTOR LZ32_Descriptor;
103 extern const DLL_DESCRIPTOR NTDLL_Descriptor;
104 extern const DLL_DESCRIPTOR SHELL32_Descriptor;
105 extern const DLL_DESCRIPTOR USER32_Descriptor;
106 extern const DLL_DESCRIPTOR VERSION_Descriptor;
107 extern const DLL_DESCRIPTOR WINMM_Descriptor;
108 extern const DLL_DESCRIPTOR WINSPOOL_Descriptor;
109 extern const DLL_DESCRIPTOR WSOCK32_Descriptor;
111 /* Table of all built-in DLLs */
113 static BUILTIN_DLL BuiltinDLLs[] =
115 /* Win16 DLLs */
116 { &KERNEL_Descriptor, DLL_FLAG_ALWAYS_USED },
117 { &USER_Descriptor, DLL_FLAG_ALWAYS_USED },
118 { &GDI_Descriptor, DLL_FLAG_ALWAYS_USED },
119 { &WIN87EM_Descriptor, DLL_FLAG_NOT_USED },
120 { &SHELL_Descriptor, 0 },
121 { &SOUND_Descriptor, 0 },
122 { &KEYBOARD_Descriptor, 0 },
123 { &WINSOCK_Descriptor, 0 },
124 { &STRESS_Descriptor, 0 },
125 { &MMSYSTEM_Descriptor, 0 },
126 { &SYSTEM_Descriptor, 0 },
127 { &TOOLHELP_Descriptor, 0 },
128 { &MOUSE_Descriptor, 0 },
129 { &COMMDLG_Descriptor, DLL_FLAG_NOT_USED },
130 { &OLE2_Descriptor, DLL_FLAG_NOT_USED },
131 { &OLE2CONV_Descriptor, DLL_FLAG_NOT_USED },
132 { &OLE2DISP_Descriptor, DLL_FLAG_NOT_USED },
133 { &OLE2NLS_Descriptor, DLL_FLAG_NOT_USED },
134 { &OLE2PROX_Descriptor, DLL_FLAG_NOT_USED },
135 { &OLECLI_Descriptor, DLL_FLAG_NOT_USED },
136 { &OLESVR_Descriptor, DLL_FLAG_NOT_USED },
137 { &COMPOBJ_Descriptor, DLL_FLAG_NOT_USED },
138 { &STORAGE_Descriptor, DLL_FLAG_NOT_USED },
139 { &WPROCS_Descriptor, DLL_FLAG_ALWAYS_USED },
140 { &DDEML_Descriptor, DLL_FLAG_NOT_USED },
141 { &LZEXPAND_Descriptor, 0 },
142 { &VER_Descriptor, 0 },
143 { &W32SYS_Descriptor, 0 },
144 /* Win32 DLLs */
145 { &ADVAPI32_Descriptor, 0 },
146 { &COMCTL32_Descriptor, 0 },
147 { &COMDLG32_Descriptor, 0 },
148 { &CRTDLL_Descriptor, 0 },
149 { &OLE32_Descriptor, 0 },
150 { &GDI32_Descriptor, 0 },
151 { &KERNEL32_Descriptor, DLL_FLAG_ALWAYS_USED },
152 { &LZ32_Descriptor, 0 },
153 { &NTDLL_Descriptor, 0 },
154 { &SHELL32_Descriptor, 0 },
155 { &USER32_Descriptor, 0 },
156 { &VERSION_Descriptor, 0 },
157 { &WINMM_Descriptor, 0 },
158 { &WINSPOOL_Descriptor, 0 },
159 { &WSOCK32_Descriptor, 0 },
160 /* Last entry */
161 { NULL, 0 }
165 /***********************************************************************
166 * BUILTIN_Init
168 * Load all built-in modules marked as 'always used'.
170 BOOL16 BUILTIN_Init(void)
172 BUILTIN_DLL *dll;
173 NE_MODULE *pModule;
175 for (dll = BuiltinDLLs; dll->descr; dll++)
176 if (dll->flags & DLL_FLAG_ALWAYS_USED)
177 if (!BUILTIN_LoadModule(dll->descr->name, TRUE)) return FALSE;
179 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
181 MODULE_SetEntryPoint( GetModuleHandle( "KERNEL" ), 178, GetWinFlags() );
183 /* Set the USER and GDI heap selectors */
185 pModule = MODULE_GetPtr( GetModuleHandle( "USER" ));
186 USER_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
187 pModule = MODULE_GetPtr( GetModuleHandle( "GDI" ));
188 GDI_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
190 return TRUE;
194 /***********************************************************************
195 * BUILTIN_LoadModule
197 * Load a built-in module. If the 'force' parameter is FALSE, we only
198 * load the module if it has not been disabled via the -dll option.
200 HMODULE16 BUILTIN_LoadModule( LPCSTR name, BOOL16 force )
202 HMODULE16 hModule;
203 NE_MODULE *pModule;
204 BUILTIN_DLL *table;
205 char dllname[16], *p;
207 /* Fix the name in case we have a full path and extension */
209 if ((p = strrchr( name, '\\' ))) name = p + 1;
210 lstrcpyn32A( dllname, name, sizeof(dllname) );
211 if ((p = strrchr( dllname, '.' ))) *p = '\0';
213 for (table = BuiltinDLLs; table->descr; table++)
214 if (!lstrcmpi32A( table->descr->name, dllname )) break;
215 if (!table->descr) return 0;
216 if ((table->flags & DLL_FLAG_NOT_USED) && !force) return 0;
218 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, table->descr->module_start,
219 table->descr->module_size, 0,
220 FALSE, FALSE, FALSE, NULL );
221 if (!hModule) return 0;
222 FarSetOwner( hModule, hModule );
224 dprintf_module( stddeb, "Built-in %s: hmodule=%04x\n",
225 table->descr->name, hModule );
226 pModule = (NE_MODULE *)GlobalLock16( hModule );
227 pModule->self = hModule;
229 if (pModule->flags & NE_FFLAGS_WIN32)
231 pModule->pe_module = (PE_MODULE *)table;
232 table->flags |= DLL_FLAG_WIN32;
234 else /* Win16 module */
236 const WIN16_DESCRIPTOR *descr = &table->descr->u.win16;
237 int minsize;
239 /* Allocate the code segment */
241 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
242 pSegTable->selector = GLOBAL_CreateBlock(GMEM_FIXED, descr->code_start,
243 pSegTable->minsize, hModule,
244 TRUE, TRUE, FALSE, NULL );
245 if (!pSegTable->selector) return 0;
246 pSegTable++;
248 /* Allocate the data segment */
250 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
251 minsize += pModule->heap_size;
252 if (minsize > 0x10000) minsize = 0x10000;
253 pSegTable->selector = GLOBAL_Alloc( GMEM_FIXED, minsize,
254 hModule, FALSE, FALSE, FALSE );
255 if (!pSegTable->selector) return 0;
256 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->selector ),
257 descr->data_start, pSegTable->minsize);
258 if (pModule->heap_size)
259 LocalInit( pSegTable->selector, pSegTable->minsize, minsize );
262 MODULE_RegisterModule( pModule );
263 return hModule;
267 /***********************************************************************
268 * BUILTIN_GetEntryPoint16
270 * Return the ordinal and name corresponding to a CS:IP address.
271 * This is used only by relay debugging.
273 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
275 static char buffer[80];
276 WORD ordinal, i, max_offset;
277 register BYTE *p;
278 NE_MODULE *pModule;
280 if (!(pModule = MODULE_GetPtr( FarGetOwner( GlobalHandle16(cs) ))))
281 return NULL;
283 /* Search for the ordinal */
285 p = (BYTE *)pModule + pModule->entry_table;
286 max_offset = 0;
287 ordinal = 1;
288 *pOrd = 0;
289 while (*p)
291 switch(p[1])
293 case 0: /* unused */
294 ordinal += *p;
295 p += 2;
296 break;
297 case 1: /* code segment */
298 i = *p;
299 p += 2;
300 while (i-- > 0)
302 p++;
303 if ((*(WORD *)p <= ip) && (*(WORD *)p >= max_offset))
305 max_offset = *(WORD *)p;
306 *pOrd = ordinal;
308 p += 2;
309 ordinal++;
311 break;
312 case 0xff: /* moveable (should not happen in built-in modules) */
313 fprintf( stderr, "Built-in module has moveable entry\n" );
314 ordinal += *p;
315 p += 2 + *p * 6;
316 break;
317 default: /* other segment */
318 ordinal += *p;
319 p += 2 + *p * 3;
320 break;
324 /* Search for the name in the resident names table */
325 /* (built-in modules have no non-resident table) */
327 p = (BYTE *)pModule + pModule->name_table;
328 while (*p)
330 p += *p + 1 + sizeof(WORD);
331 if (*(WORD *)(p + *p + 1) == *pOrd) break;
334 sprintf( buffer, "%.*s.%d: %.*s",
335 *((BYTE *)pModule + pModule->name_table),
336 (char *)pModule + pModule->name_table + 1,
337 *pOrd, *p, (char *)(p + 1) );
338 return buffer;
342 /***********************************************************************
343 * BUILTIN_GetEntryPoint32
345 * Return the name of the DLL entry point corresponding
346 * to a relay entry point address. This is used only by relay debugging.
348 LPCSTR BUILTIN_GetEntryPoint32( void *relay )
350 static char buffer[80];
351 BUILTIN_DLL *dll;
352 const void **funcs;
353 int first, i, size;
355 /* First find the module */
357 for (dll = BuiltinDLLs; dll->descr; dll++)
358 if ((dll->flags & DLL_FLAG_WIN32) &&
359 (dll->descr->u.win32.functions[0] <= relay) &&
360 (dll->descr->u.win32.functions[dll->descr->u.win32.size-1] >relay))
361 break;
362 if (!dll->descr)
364 sprintf( buffer, "???.???: %08x", (UINT32)relay );
365 return buffer;
368 /* Do a binary search for the function */
370 relay = (BYTE *)relay - 11; /* The relay entry point is 11 bytes long */
371 funcs = dll->descr->u.win32.functions;
372 first = i = 0;
373 size = dll->descr->u.win32.size;
374 while (first < size)
376 i = (first + size) / 2;
377 if (funcs[i] == relay) break;
378 if (funcs[i] > relay) size = i;
379 else first = i + 1;
381 sprintf( buffer, "%s.%d: %s",
382 dll->descr->name, i, dll->descr->u.win32.names[i] );
383 return buffer;
387 /***********************************************************************
388 * BUILTIN_GetProcAddress32
390 * Implementation of GetProcAddress() for built-in Win32 modules.
391 * FIXME: this should be unified with the real GetProcAddress32().
393 FARPROC32 BUILTIN_GetProcAddress32( NE_MODULE *pModule, LPCSTR function )
395 BUILTIN_DLL *dll = (BUILTIN_DLL *)pModule->pe_module;
396 const WIN32_DESCRIPTOR *info = &dll->descr->u.win32;
398 if (!dll) return NULL;
400 if (HIWORD(function)) /* Find function by name */
402 int i;
404 dprintf_module( stddeb, "Looking for function %s in %s\n",
405 function, dll->descr->name );
406 for (i = 0; i < info->size; i++)
407 if (info->names[i] && !strcmp( function, info->names[i] ))
408 return (FARPROC32)info->functions[i];
410 else /* Find function by ordinal */
412 WORD ordinal = LOWORD(function);
413 dprintf_module( stddeb, "Looking for ordinal %d in %s\n",
414 ordinal, dll->descr->name );
415 if (ordinal && ordinal < info->size)
416 return (FARPROC32)info->functions[ordinal - info->base];
418 return NULL;
422 /***********************************************************************
423 * BUILTIN_ParseDLLOptions
425 * Set runtime DLL usage flags
427 BOOL16 BUILTIN_ParseDLLOptions( const char *str )
429 BUILTIN_DLL *dll;
430 const char *p;
432 while (*str)
434 while (*str && isspace(*str)) str++;
435 if (!*str) return TRUE;
436 if ((*str != '+') && (*str != '-')) return FALSE;
437 str++;
438 if (!(p = strchr( str, ',' ))) p = str + strlen(str);
439 while ((p > str) && isspace(p[-1])) p--;
440 if (p == str) return FALSE;
441 for (dll = BuiltinDLLs; dll->descr; dll++)
443 if (!lstrncmpi32A( str, dll->descr->name, (int)(p - str) ))
445 if (str[-1] == '-')
447 if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
448 dll->flags |= DLL_FLAG_NOT_USED;
450 else dll->flags &= ~DLL_FLAG_NOT_USED;
451 break;
454 if (!dll->descr) return FALSE;
455 str = p;
456 while (*str && (isspace(*str) || (*str == ','))) str++;
458 return TRUE;
462 /***********************************************************************
463 * BUILTIN_PrintDLLs
465 * Print the list of built-in DLLs that can be disabled.
467 void BUILTIN_PrintDLLs(void)
469 int i;
470 BUILTIN_DLL *dll;
472 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
474 if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
475 fprintf( stderr, "%-9s%c", dll->descr->name,
476 ((++i) % 8) ? ' ' : '\n' );
478 fprintf(stderr,"\n");
479 exit(1);
482 #endif /* WINELIB */