Release 960606
[wine.git] / loader / builtin.c
blob76c0d29a54cb6f80df4994ef78132ed4ea44d50c
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 */
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 NTDLL_Descriptor;
104 extern const DLL_DESCRIPTOR SHELL32_Descriptor;
105 extern const DLL_DESCRIPTOR USER32_Descriptor;
106 extern const DLL_DESCRIPTOR WINSPOOL_Descriptor;
107 extern const DLL_DESCRIPTOR WSOCK32_Descriptor;
109 /* Table of all built-in DLLs */
111 static BUILTIN_DLL BuiltinDLLs[] =
113 /* Win16 DLLs */
114 { &KERNEL_Descriptor, DLL_FLAG_ALWAYS_USED },
115 { &USER_Descriptor, DLL_FLAG_ALWAYS_USED },
116 { &GDI_Descriptor, DLL_FLAG_ALWAYS_USED },
117 { &WIN87EM_Descriptor, DLL_FLAG_NOT_USED },
118 { &SHELL_Descriptor, 0 },
119 { &SOUND_Descriptor, 0 },
120 { &KEYBOARD_Descriptor, 0 },
121 { &WINSOCK_Descriptor, 0 },
122 { &STRESS_Descriptor, 0 },
123 { &MMSYSTEM_Descriptor, 0 },
124 { &SYSTEM_Descriptor, 0 },
125 { &TOOLHELP_Descriptor, 0 },
126 { &MOUSE_Descriptor, 0 },
127 { &COMMDLG_Descriptor, DLL_FLAG_NOT_USED },
128 { &OLE2_Descriptor, DLL_FLAG_NOT_USED },
129 { &OLE2CONV_Descriptor, DLL_FLAG_NOT_USED },
130 { &OLE2DISP_Descriptor, DLL_FLAG_NOT_USED },
131 { &OLE2NLS_Descriptor, DLL_FLAG_NOT_USED },
132 { &OLE2PROX_Descriptor, DLL_FLAG_NOT_USED },
133 { &OLECLI_Descriptor, DLL_FLAG_NOT_USED },
134 { &OLESVR_Descriptor, DLL_FLAG_NOT_USED },
135 { &COMPOBJ_Descriptor, DLL_FLAG_NOT_USED },
136 { &STORAGE_Descriptor, DLL_FLAG_NOT_USED },
137 { &WPROCS_Descriptor, DLL_FLAG_ALWAYS_USED },
138 { &DDEML_Descriptor, DLL_FLAG_NOT_USED },
139 { &LZEXPAND_Descriptor, 0 },
140 { &VER_Descriptor, 0 },
141 { &W32SYS_Descriptor, 0 },
142 /* Win32 DLLs */
143 { &ADVAPI32_Descriptor, 0 },
144 { &COMCTL32_Descriptor, 0 },
145 { &COMDLG32_Descriptor, 0 },
146 { &CRTDLL_Descriptor, 0 },
147 { &OLE32_Descriptor, 0 },
148 { &GDI32_Descriptor, 0 },
149 { &KERNEL32_Descriptor, DLL_FLAG_ALWAYS_USED },
150 { &NTDLL_Descriptor, 0 },
151 { &SHELL32_Descriptor, 0 },
152 { &USER32_Descriptor, 0 },
153 { &WINSPOOL_Descriptor, 0 },
154 { &WSOCK32_Descriptor, 0 },
155 /* Last entry */
156 { NULL, 0 }
160 /***********************************************************************
161 * BUILTIN_Init
163 * Load all built-in modules marked as 'always used'.
165 BOOL BUILTIN_Init(void)
167 BUILTIN_DLL *dll;
168 NE_MODULE *pModule;
170 for (dll = BuiltinDLLs; dll->descr; dll++)
171 if (dll->flags & DLL_FLAG_ALWAYS_USED)
172 if (!BUILTIN_LoadModule(dll->descr->name, TRUE)) return FALSE;
174 /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
176 MODULE_SetEntryPoint( GetModuleHandle( "KERNEL" ), 178, GetWinFlags() );
178 /* Set the USER and GDI heap selectors */
180 pModule = MODULE_GetPtr( GetModuleHandle( "USER" ));
181 USER_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
182 pModule = MODULE_GetPtr( GetModuleHandle( "GDI" ));
183 GDI_HeapSel = (NE_SEG_TABLE( pModule ) + pModule->dgroup - 1)->selector;
185 return TRUE;
189 /***********************************************************************
190 * BUILTIN_LoadModule
192 * Load a built-in module. If the 'force' parameter is FALSE, we only
193 * load the module if it has not been disabled via the -dll option.
195 HMODULE BUILTIN_LoadModule( LPCSTR name, BOOL force )
197 HMODULE hModule;
198 NE_MODULE *pModule;
199 BUILTIN_DLL *table;
200 char dllname[16], *p;
202 /* Fix the name in case we have a full path and extension */
204 if ((p = strrchr( name, '\\' ))) name = p + 1;
205 lstrcpyn( dllname, name, sizeof(dllname) );
206 if ((p = strrchr( dllname, '.' ))) *p = '\0';
208 for (table = BuiltinDLLs; table->descr; table++)
209 if (!lstrcmpi( table->descr->name, dllname )) break;
210 if (!table->descr) return 0;
211 if ((table->flags & DLL_FLAG_NOT_USED) && !force) return 0;
213 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, table->descr->module_start,
214 table->descr->module_size, 0,
215 FALSE, FALSE, FALSE, NULL );
216 if (!hModule) return 0;
217 FarSetOwner( hModule, hModule );
219 dprintf_module( stddeb, "Built-in %s: hmodule=%04x\n",
220 table->descr->name, hModule );
221 pModule = (NE_MODULE *)GlobalLock16( hModule );
222 pModule->self = hModule;
224 if (pModule->flags & NE_FFLAGS_WIN32)
226 pModule->pe_module = (PE_MODULE *)table;
228 else /* Win16 module */
230 const WIN16_DESCRIPTOR *descr = &table->descr->u.win16;
231 int minsize;
233 /* Allocate the code segment */
235 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
236 pSegTable->selector = GLOBAL_CreateBlock(GMEM_FIXED, descr->code_start,
237 pSegTable->minsize, hModule,
238 TRUE, TRUE, FALSE, NULL );
239 if (!pSegTable->selector) return 0;
240 pSegTable++;
242 /* Allocate the data segment */
244 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
245 minsize += pModule->heap_size;
246 if (minsize > 0x10000) minsize = 0x10000;
247 pSegTable->selector = GLOBAL_Alloc( GMEM_FIXED, minsize,
248 hModule, FALSE, FALSE, FALSE );
249 if (!pSegTable->selector) return 0;
250 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->selector ),
251 descr->data_start, pSegTable->minsize);
252 if (pModule->heap_size)
253 LocalInit( pSegTable->selector, pSegTable->minsize, minsize );
256 MODULE_RegisterModule( pModule );
257 return hModule;
261 /***********************************************************************
262 * BUILTIN_GetEntryPoint
264 * Return the built-in module, ordinal and name corresponding
265 * to a CS:IP address. This is used only by relay debugging.
267 NE_MODULE *BUILTIN_GetEntryPoint( WORD cs, WORD ip, WORD *pOrd, char **ppName )
269 WORD ordinal, i, max_offset;
270 register BYTE *p;
271 NE_MODULE *pModule;
273 if (!(pModule = MODULE_GetPtr( FarGetOwner( GlobalHandle16(cs) ))))
274 return NULL;
276 /* Search for the ordinal */
278 p = (BYTE *)pModule + pModule->entry_table;
279 max_offset = 0;
280 ordinal = 1;
281 *pOrd = 0;
282 while (*p)
284 switch(p[1])
286 case 0: /* unused */
287 ordinal += *p;
288 p += 2;
289 break;
290 case 1: /* code segment */
291 i = *p;
292 p += 2;
293 while (i-- > 0)
295 p++;
296 if ((*(WORD *)p <= ip) && (*(WORD *)p >= max_offset))
298 max_offset = *(WORD *)p;
299 *pOrd = ordinal;
301 p += 2;
302 ordinal++;
304 break;
305 case 0xff: /* moveable (should not happen in built-in modules) */
306 fprintf( stderr, "Built-in module has moveable entry\n" );
307 ordinal += *p;
308 p += 2 + *p * 6;
309 break;
310 default: /* other segment */
311 ordinal += *p;
312 p += 2 + *p * 3;
313 break;
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;
321 *ppName = "???";
322 while (*p)
324 p += *p + 1 + sizeof(WORD);
325 if (*(WORD *)(p + *p + 1) == *pOrd)
327 *ppName = (char *)p;
328 break;
332 return pModule;
336 /***********************************************************************
337 * BUILTIN_GetProcAddress32
339 * Implementation of GetProcAddress() for built-in Win32 modules.
340 * FIXME: this should be unified with the real GetProcAddress32().
342 DWORD BUILTIN_GetProcAddress32( NE_MODULE *pModule, char *function )
344 BUILTIN_DLL *dll = (BUILTIN_DLL *)pModule->pe_module;
345 const WIN32_DESCRIPTOR *info = &dll->descr->u.win32;
347 if (!dll) return 0;
349 if (HIWORD(function)) /* Find function by name */
351 int i;
353 dprintf_module( stddeb, "Looking for function %s in %s\n",
354 function, dll->descr->name );
355 for (i = 0; i < info->size; i++)
356 if (info->names[i] && !strcmp( function, info->names[i] ))
357 return (DWORD)info->functions[i];
359 else /* Find function by ordinal */
361 WORD ordinal = LOWORD(function);
362 dprintf_module( stddeb, "Looking for ordinal %d in %s\n",
363 ordinal, dll->descr->name );
364 if (ordinal && ordinal < info->size)
365 return (DWORD)info->functions[ordinal - info->base];
367 return 0;
371 /***********************************************************************
372 * BUILTIN_ParseDLLOptions
374 * Set runtime DLL usage flags
376 BOOL BUILTIN_ParseDLLOptions( const char *str )
378 BUILTIN_DLL *dll;
379 const char *p;
381 while (*str)
383 while (*str && isspace(*str)) str++;
384 if (!*str) return TRUE;
385 if ((*str != '+') && (*str != '-')) return FALSE;
386 str++;
387 if (!(p = strchr( str, ',' ))) p = str + strlen(str);
388 while ((p > str) && isspace(p[-1])) p--;
389 if (p == str) return FALSE;
390 for (dll = BuiltinDLLs; dll->descr; dll++)
392 if (!lstrncmpi( str, dll->descr->name, (int)(p - str) ))
394 if (str[-1] == '-')
396 if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
397 dll->flags |= DLL_FLAG_NOT_USED;
399 else dll->flags &= ~DLL_FLAG_NOT_USED;
400 break;
403 if (!dll->descr) return FALSE;
404 str = p;
405 while (*str && (isspace(*str) || (*str == ','))) str++;
407 return TRUE;
411 /***********************************************************************
412 * BUILTIN_PrintDLLs
414 * Print the list of built-in DLLs that can be disabled.
416 void BUILTIN_PrintDLLs(void)
418 int i;
419 BUILTIN_DLL *dll;
421 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
423 if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
424 fprintf( stderr, "%-9s%c", dll->descr->name,
425 ((++i) % 8) ? ' ' : '\n' );
427 fprintf(stderr,"\n");
428 exit(1);
431 #endif /* WINELIB */