Release 960805
[wine] / loader / builtin.c
1 /*
2  * Built-in modules
3  *
4  * Copyright 1996 Alexandre Julliard
5  */
6
7 #ifndef WINELIB
8
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"
19
20
21 /* Built-in modules descriptors */
22 /* Don't change these structures! (see tools/build.c) */
23
24 typedef struct
25 {
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;
29
30 typedef struct
31 {
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;
37
38 typedef struct
39 {
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
44     {
45         WIN16_DESCRIPTOR win16;    /* Descriptor for Win16 DLL */
46         WIN32_DESCRIPTOR win32;    /* Descriptor for Win32 DLL */
47     } u;
48 } DLL_DESCRIPTOR;
49
50 typedef struct
51 {
52     const DLL_DESCRIPTOR *descr;   /* DLL descriptor */
53     int                   flags;   /* flags (see below) */
54 } BUILTIN_DLL;
55
56
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 */
61
62 /* 16-bit DLLs */
63
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;
92
93 /* 32-bit DLLs */
94
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;
110
111 /* Table of all built-in DLLs */
112
113 static BUILTIN_DLL BuiltinDLLs[] =
114 {
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 }
162 };
163
164
165 /***********************************************************************
166  *           BUILTIN_Init
167  *
168  * Load all built-in modules marked as 'always used'.
169  */
170 BOOL16 BUILTIN_Init(void)
171 {
172     BUILTIN_DLL *dll;
173     NE_MODULE *pModule;
174
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;
178
179     /* Initialize KERNEL.178 (__WINFLAGS) with the correct flags value */
180
181     MODULE_SetEntryPoint( GetModuleHandle( "KERNEL" ), 178, GetWinFlags() );
182
183     /* Set the USER and GDI heap selectors */
184
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;
189
190     return TRUE;
191 }
192
193
194 /***********************************************************************
195  *           BUILTIN_LoadModule
196  *
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.
199  */
200 HMODULE16 BUILTIN_LoadModule( LPCSTR name, BOOL16 force )
201 {
202     HMODULE16 hModule;
203     NE_MODULE *pModule;
204     BUILTIN_DLL *table;
205     char dllname[16], *p;
206
207     /* Fix the name in case we have a full path and extension */
208
209     if ((p = strrchr( name, '\\' ))) name = p + 1;
210     lstrcpyn32A( dllname, name, sizeof(dllname) );
211     if ((p = strrchr( dllname, '.' ))) *p = '\0';
212
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;
217
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 );
223
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;
228
229     if (pModule->flags & NE_FFLAGS_WIN32)
230     {
231         pModule->pe_module = (PE_MODULE *)table;
232         table->flags |= DLL_FLAG_WIN32;
233     }
234     else  /* Win16 module */
235     {
236         const WIN16_DESCRIPTOR *descr = &table->descr->u.win16;
237         int minsize;
238
239         /* Allocate the code segment */
240
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++;
247
248         /* Allocate the data segment */
249
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 );
260     }
261
262     MODULE_RegisterModule( pModule );
263     return hModule;
264 }
265
266
267 /***********************************************************************
268  *           BUILTIN_GetEntryPoint16
269  *
270  * Return the ordinal and name corresponding to a CS:IP address.
271  * This is used only by relay debugging.
272  */
273 LPCSTR BUILTIN_GetEntryPoint16( WORD cs, WORD ip, WORD *pOrd )
274 {
275     static char buffer[80];
276     WORD ordinal, i, max_offset;
277     register BYTE *p;
278     NE_MODULE *pModule;
279
280     if (!(pModule = MODULE_GetPtr( FarGetOwner( GlobalHandle16(cs) ))))
281         return NULL;
282
283     /* Search for the ordinal */
284
285     p = (BYTE *)pModule + pModule->entry_table;
286     max_offset = 0;
287     ordinal = 1;
288     *pOrd = 0;
289     while (*p)
290     {
291         switch(p[1])
292         {
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)
301             {
302                 p++;
303                 if ((*(WORD *)p <= ip) && (*(WORD *)p >= max_offset))
304                 {
305                     max_offset = *(WORD *)p;
306                     *pOrd = ordinal;
307                 }
308                 p += 2;
309                 ordinal++;
310             }
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;
321         }
322     }
323
324     /* Search for the name in the resident names table */
325     /* (built-in modules have no non-resident table)   */
326     
327     p = (BYTE *)pModule + pModule->name_table;
328     while (*p)
329     {
330         p += *p + 1 + sizeof(WORD);
331         if (*(WORD *)(p + *p + 1) == *pOrd) break;
332     }
333
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;
339 }
340
341
342 /***********************************************************************
343  *           BUILTIN_GetEntryPoint32
344  *
345  * Return the name of the DLL entry point corresponding
346  * to a relay entry point address. This is used only by relay debugging.
347  */
348 LPCSTR BUILTIN_GetEntryPoint32( void *relay )
349 {
350     static char buffer[80];
351     BUILTIN_DLL *dll;
352     const void **funcs;
353     int i;
354
355     /* First find the module */
356
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)
363     {
364         sprintf( buffer, "???.???: %08x", (UINT32)relay );
365         return buffer;
366     }
367
368     /* Now find the function */
369
370     relay = (BYTE *)relay - 11;  /* The relay entry point is 11 bytes long */
371     funcs = dll->descr->u.win32.functions;
372     for (i = 0; i < dll->descr->u.win32.size;i++) if (*funcs++ == relay) break;
373     sprintf( buffer, "%s.%d: %s",
374              dll->descr->name, i, dll->descr->u.win32.names[i] );
375     return buffer;
376 }
377
378
379 /***********************************************************************
380  *           BUILTIN_GetProcAddress32
381  *
382  * Implementation of GetProcAddress() for built-in Win32 modules.
383  * FIXME: this should be unified with the real GetProcAddress32().
384  */
385 FARPROC32 BUILTIN_GetProcAddress32( NE_MODULE *pModule, LPCSTR function )
386 {
387     BUILTIN_DLL *dll = (BUILTIN_DLL *)pModule->pe_module;
388     const WIN32_DESCRIPTOR *info = &dll->descr->u.win32;
389
390     if (!dll) return NULL;
391
392     if (HIWORD(function))  /* Find function by name */
393     {
394         int i;
395
396         dprintf_module( stddeb, "Looking for function %s in %s\n",
397                         function, dll->descr->name );
398         for (i = 0; i < info->size; i++)
399             if (info->names[i] && !strcmp( function, info->names[i] ))
400                 return (FARPROC32)info->functions[i];
401     }
402     else  /* Find function by ordinal */
403     {
404         WORD ordinal = LOWORD(function);
405         dprintf_module( stddeb, "Looking for ordinal %d in %s\n",
406                         ordinal, dll->descr->name );
407         if (ordinal && ordinal < info->size)
408             return (FARPROC32)info->functions[ordinal - info->base];
409     }
410     return NULL;
411 }
412
413
414 /***********************************************************************
415  *           BUILTIN_ParseDLLOptions
416  *
417  * Set runtime DLL usage flags
418  */
419 BOOL16 BUILTIN_ParseDLLOptions( const char *str )
420 {
421     BUILTIN_DLL *dll;
422     const char *p;
423
424     while (*str)
425     {
426         while (*str && isspace(*str)) str++;
427         if (!*str) return TRUE;
428         if ((*str != '+') && (*str != '-')) return FALSE;
429         str++;
430         if (!(p = strchr( str, ',' ))) p = str + strlen(str);
431         while ((p > str) && isspace(p[-1])) p--;
432         if (p == str) return FALSE;
433         for (dll = BuiltinDLLs; dll->descr; dll++)
434         {
435             if (!lstrncmpi32A( str, dll->descr->name, (int)(p - str) ))
436             {
437                 if (str[-1] == '-')
438                 {
439                     if (dll->flags & DLL_FLAG_ALWAYS_USED) return FALSE;
440                     dll->flags |= DLL_FLAG_NOT_USED;
441                 }
442                 else dll->flags &= ~DLL_FLAG_NOT_USED;
443                 break;
444             }
445         }
446         if (!dll->descr) return FALSE;
447         str = p;
448         while (*str && (isspace(*str) || (*str == ','))) str++;
449     }
450     return TRUE;
451 }
452
453
454 /***********************************************************************
455  *           BUILTIN_PrintDLLs
456  *
457  * Print the list of built-in DLLs that can be disabled.
458  */
459 void BUILTIN_PrintDLLs(void)
460 {
461     int i;
462     BUILTIN_DLL *dll;
463
464     for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
465     {
466         if (!(dll->flags & DLL_FLAG_ALWAYS_USED))
467             fprintf( stderr, "%-9s%c", dll->descr->name,
468                      ((++i) % 8) ? ' ' : '\n' );
469     }
470     fprintf(stderr,"\n");
471     exit(1);
472 }
473
474 #endif  /* WINELIB */