Added -dll option for winelib programs. (Note: will not print warnings
[wine] / relay32 / builtin32.c
1 /*
2  * Win32 builtin functions
3  *
4  * Copyright 1997 Alexandre Julliard
5  */
6
7 #include <assert.h>
8 #include <string.h>
9 #include <ctype.h>
10 #include "winuser.h"
11 #include "builtin32.h"
12 #include "peexe.h"
13 #include "neexe.h"
14 #include "heap.h"
15 #include "debug.h"
16 #include "main.h"
17
18 typedef struct
19 {
20     BYTE  call;                    /* 0xe8 call callfrom32 (relative) */
21     DWORD callfrom32 WINE_PACKED;  /* RELAY_CallFrom32 relative addr */
22     BYTE  ret;                     /* 0xc2 ret $n  or  0xc3 ret */
23     WORD  args;                    /* nb of args to remove from the stack */
24 } DEBUG_ENTRY_POINT;
25
26 typedef struct
27 {
28     const BUILTIN32_DESCRIPTOR *descr;     /* DLL descriptor */
29     BOOL                      used;      /* Used by default */
30 } BUILTIN32_DLL;
31
32
33 extern const BUILTIN32_DESCRIPTOR ADVAPI32_Descriptor;
34 extern const BUILTIN32_DESCRIPTOR AVIFIL32_Descriptor;
35 extern const BUILTIN32_DESCRIPTOR COMCTL32_Descriptor;
36 extern const BUILTIN32_DESCRIPTOR COMDLG32_Descriptor;
37 extern const BUILTIN32_DESCRIPTOR CRTDLL_Descriptor;
38 extern const BUILTIN32_DESCRIPTOR DCIMAN32_Descriptor;
39 extern const BUILTIN32_DESCRIPTOR DDRAW_Descriptor;
40 extern const BUILTIN32_DESCRIPTOR DINPUT_Descriptor;
41 extern const BUILTIN32_DESCRIPTOR DPLAY_Descriptor;
42 extern const BUILTIN32_DESCRIPTOR DPLAYX_Descriptor;
43 extern const BUILTIN32_DESCRIPTOR DSOUND_Descriptor;
44 extern const BUILTIN32_DESCRIPTOR GDI32_Descriptor;
45 extern const BUILTIN32_DESCRIPTOR IMAGEHLP_Descriptor;
46 extern const BUILTIN32_DESCRIPTOR IMM32_Descriptor;
47 extern const BUILTIN32_DESCRIPTOR KERNEL32_Descriptor;
48 extern const BUILTIN32_DESCRIPTOR LZ32_Descriptor;
49 extern const BUILTIN32_DESCRIPTOR MPR_Descriptor;
50 extern const BUILTIN32_DESCRIPTOR MSACM32_Descriptor;
51 extern const BUILTIN32_DESCRIPTOR MSNET32_Descriptor;
52 extern const BUILTIN32_DESCRIPTOR MSVFW32_Descriptor;
53 extern const BUILTIN32_DESCRIPTOR NTDLL_Descriptor;
54 extern const BUILTIN32_DESCRIPTOR OLE32_Descriptor;
55 extern const BUILTIN32_DESCRIPTOR OLEAUT32_Descriptor;
56 extern const BUILTIN32_DESCRIPTOR OLECLI32_Descriptor;
57 extern const BUILTIN32_DESCRIPTOR OLEDLG_Descriptor;
58 extern const BUILTIN32_DESCRIPTOR OLESVR32_Descriptor;
59 extern const BUILTIN32_DESCRIPTOR PSAPI_Descriptor;
60 extern const BUILTIN32_DESCRIPTOR RASAPI32_Descriptor;
61 extern const BUILTIN32_DESCRIPTOR SHELL32_Descriptor;
62 extern const BUILTIN32_DESCRIPTOR TAPI32_Descriptor;
63 extern const BUILTIN32_DESCRIPTOR USER32_Descriptor;
64 extern const BUILTIN32_DESCRIPTOR VERSION_Descriptor;
65 extern const BUILTIN32_DESCRIPTOR W32SKRNL_Descriptor;
66 extern const BUILTIN32_DESCRIPTOR WINMM_Descriptor;
67 extern const BUILTIN32_DESCRIPTOR WINSPOOL_Descriptor;
68 extern const BUILTIN32_DESCRIPTOR WNASPI32_Descriptor;
69 extern const BUILTIN32_DESCRIPTOR WOW32_Descriptor;
70 extern const BUILTIN32_DESCRIPTOR WSOCK32_Descriptor;
71
72 static BUILTIN32_DLL BuiltinDLLs[] =
73 {
74     { &ADVAPI32_Descriptor, TRUE  },
75     { &AVIFIL32_Descriptor, FALSE },
76     { &COMCTL32_Descriptor, FALSE },
77     { &COMDLG32_Descriptor, TRUE  },
78     { &CRTDLL_Descriptor,   TRUE  },
79     { &DCIMAN32_Descriptor, FALSE },
80     { &DDRAW_Descriptor,    TRUE  },
81     { &DINPUT_Descriptor,   TRUE  },
82     { &DPLAY_Descriptor,    FALSE },
83     { &DPLAYX_Descriptor,   FALSE },
84     { &DSOUND_Descriptor,   TRUE  },
85     { &GDI32_Descriptor,    TRUE  },
86     { &IMAGEHLP_Descriptor, FALSE },
87     { &IMM32_Descriptor,    FALSE },
88     { &KERNEL32_Descriptor, TRUE  },
89     { &LZ32_Descriptor,     TRUE  },
90     { &MPR_Descriptor,      TRUE  },
91     { &MSACM32_Descriptor,  FALSE },
92     { &MSNET32_Descriptor,  FALSE },
93     { &MSVFW32_Descriptor,  TRUE  },
94     { &NTDLL_Descriptor,    TRUE  },
95     { &OLE32_Descriptor,    FALSE },
96     { &OLEAUT32_Descriptor, FALSE },
97     { &OLECLI32_Descriptor, FALSE },
98     { &OLEDLG_Descriptor,   FALSE },
99     { &OLESVR32_Descriptor, FALSE },
100     { &PSAPI_Descriptor,    FALSE },
101     { &RASAPI32_Descriptor, FALSE },
102     { &SHELL32_Descriptor,  TRUE  },
103     { &TAPI32_Descriptor,   FALSE },
104     { &USER32_Descriptor,   TRUE  },
105     { &VERSION_Descriptor,  TRUE  },
106     { &W32SKRNL_Descriptor, TRUE  },
107     { &WINMM_Descriptor,    TRUE  },
108     { &WINSPOOL_Descriptor, TRUE  },
109     { &WNASPI32_Descriptor, TRUE  },
110     { &WOW32_Descriptor,    TRUE  },
111     { &WSOCK32_Descriptor,  TRUE  },
112     /* Last entry */
113     { NULL, FALSE }
114 };
115
116 extern void RELAY_CallFrom32();
117
118 /***********************************************************************
119  *           BUILTIN32_DoLoadImage
120  *
121  * Load a built-in Win32 module. Helper function for BUILTIN32_LoadImage.
122  */
123 static HMODULE BUILTIN32_DoLoadImage( BUILTIN32_DLL *dll )
124 {
125
126     IMAGE_DATA_DIRECTORY *dir;
127     IMAGE_DOS_HEADER *dos;
128     IMAGE_NT_HEADERS *nt;
129     IMAGE_SECTION_HEADER *sec;
130     IMAGE_EXPORT_DIRECTORY *exp;
131     LPVOID *funcs;
132     LPSTR *names;
133     DEBUG_ENTRY_POINT *debug;
134     INT i, size;
135     BYTE *addr;
136
137     /* Allocate the module */
138
139     size = (sizeof(IMAGE_DOS_HEADER)
140             + sizeof(IMAGE_NT_HEADERS)
141             + 2 * sizeof(IMAGE_SECTION_HEADER)
142             + sizeof(IMAGE_EXPORT_DIRECTORY)
143             + dll->descr->nb_funcs * sizeof(LPVOID)
144             + dll->descr->nb_names * sizeof(LPSTR));
145 #ifdef __i386__
146     if (WARN_ON(relay) || TRACE_ON(relay))
147         size += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
148 #endif
149     addr  = VirtualAlloc( NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
150     if (!addr) return 0;
151     dos   = (IMAGE_DOS_HEADER *)addr;
152     nt    = (IMAGE_NT_HEADERS *)(dos + 1);
153     sec   = (IMAGE_SECTION_HEADER *)(nt + 1);
154     exp   = (IMAGE_EXPORT_DIRECTORY *)(sec + 2);
155     funcs = (LPVOID *)(exp + 1);
156     names = (LPSTR *)(funcs + dll->descr->nb_funcs);
157     debug = (DEBUG_ENTRY_POINT *)(names + dll->descr->nb_names);
158
159     /* Build the DOS and NT headers */
160
161     dos->e_magic  = IMAGE_DOS_SIGNATURE;
162     dos->e_lfanew = sizeof(*dos);
163
164     nt->Signature                       = IMAGE_NT_SIGNATURE;
165     nt->FileHeader.Machine              = IMAGE_FILE_MACHINE_I386;
166     nt->FileHeader.NumberOfSections     = 2;  /* exports + code */
167     nt->FileHeader.SizeOfOptionalHeader = sizeof(nt->OptionalHeader);
168     nt->FileHeader.Characteristics      = IMAGE_FILE_DLL;
169
170     nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
171     nt->OptionalHeader.SizeOfCode                  = 0x1000;
172     nt->OptionalHeader.SizeOfInitializedData       = 0;
173     nt->OptionalHeader.SizeOfUninitializedData     = 0;
174     nt->OptionalHeader.ImageBase                   = (DWORD)addr;
175     nt->OptionalHeader.SectionAlignment            = 0x1000;
176     nt->OptionalHeader.FileAlignment               = 0x1000;
177     nt->OptionalHeader.MajorOperatingSystemVersion = 1;
178     nt->OptionalHeader.MinorOperatingSystemVersion = 0;
179     nt->OptionalHeader.MajorSubsystemVersion       = 4;
180     nt->OptionalHeader.MinorSubsystemVersion       = 0;
181     nt->OptionalHeader.SizeOfImage                 = size;
182     nt->OptionalHeader.SizeOfHeaders               = (BYTE *)exp - addr;
183     nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
184     if (dll->descr->dllentrypoint) 
185         nt->OptionalHeader.AddressOfEntryPoint = (DWORD)dll->descr->dllentrypoint - (DWORD)addr;
186     
187     /* Build the export directory */
188
189     dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
190     dir->VirtualAddress = (BYTE *)exp - addr;
191     dir->Size = sizeof(*exp)
192                 + dll->descr->nb_funcs * sizeof(LPVOID)
193                 + dll->descr->nb_names * sizeof(LPSTR);
194
195     /* Build the exports section */
196
197     strcpy( sec->Name, ".edata" );
198     sec->Misc.VirtualSize = dir->Size;
199     sec->VirtualAddress   = (BYTE *)exp - addr;
200     sec->SizeOfRawData    = dir->Size;
201     sec->PointerToRawData = (BYTE *)exp - addr;
202     sec->Characteristics  = (IMAGE_SCN_CNT_INITIALIZED_DATA |
203                              IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ |
204                              IMAGE_SCN_MEM_WRITE);
205
206     /* Build the code section */
207
208     sec++;
209     strcpy( sec->Name, ".code" );
210     sec->SizeOfRawData = 0;
211 #ifdef __i386__
212     if (WARN_ON(relay) || TRACE_ON(relay))
213         sec->SizeOfRawData += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
214 #endif
215     sec->Misc.VirtualSize = sec->SizeOfRawData;
216     sec->VirtualAddress   = (BYTE *)debug - addr;
217     sec->PointerToRawData = (BYTE *)debug - addr;
218     sec->Characteristics  = (IMAGE_SCN_CNT_INITIALIZED_DATA |
219                              IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
220
221     /* Build the exports section data */
222
223     exp->Name                  = ((BYTE *)dll->descr->name) - addr;  /*??*/
224     exp->Base                  = dll->descr->base;
225     exp->NumberOfFunctions     = dll->descr->nb_funcs;
226     exp->NumberOfNames         = dll->descr->nb_names;
227     exp->AddressOfFunctions    = (LPDWORD *)((BYTE *)funcs - addr);
228     exp->AddressOfNames        = (LPDWORD *)((BYTE *)names - addr);
229     exp->AddressOfNameOrdinals = (LPWORD *)((BYTE *)dll->descr->ordinals - addr);
230
231     /* Build the funcs table */
232
233     for (i = 0; i < dll->descr->nb_funcs; i++, funcs++, debug++)
234     {
235         BYTE args = dll->descr->args[i];
236         int j;
237
238         if (!dll->descr->functions[i]) continue;
239         *funcs = (LPVOID)((BYTE *)dll->descr->functions[i] - addr);
240 #ifdef __i386__
241         if (!(WARN_ON(relay) || TRACE_ON(relay))) continue;
242         for (j=0;j<dll->descr->nb_names;j++)
243             if (dll->descr->ordinals[j] == i)
244                 break;
245         if (j<dll->descr->nb_names) {
246             if (dll->descr->names[j]) {
247                 char buffer[200];
248                 sprintf(buffer,"%s.%d: %s",dll->descr->name,i,dll->descr->names[j]);
249                 if (!RELAY_ShowDebugmsgRelay(buffer))
250                     continue;
251             }
252         }
253         switch(args)
254         {
255         case 0xfe:  /* register func */
256             debug->call       = 0xe8;
257             debug->callfrom32 = (DWORD)dll->descr->functions[i] -
258                                 (DWORD)&debug->ret;
259             debug->ret        = 0x90;  /* nop */
260             debug->args       = 0;
261             *funcs = (LPVOID)((BYTE *)debug - addr);
262             break;
263         case 0xff:  /* stub or extern */
264             break;
265         default:  /* normal function (stdcall or cdecl) */
266             if (TRACE_ON(relay)) {
267                 debug->call       = 0xe8; /* lcall relative */
268                 debug->callfrom32 = (DWORD)RELAY_CallFrom32 -
269                                     (DWORD)&debug->ret;
270             } else {
271                 debug->call       = 0xe9; /* ljmp relative */
272                 debug->callfrom32 = (DWORD)dll->descr->functions[i] -
273                                     (DWORD)&debug->ret;
274             }
275             debug->ret        = (args & 0x80) ? 0xc3 : 0xc2; /*ret/ret $n*/
276             debug->args       = (args & 0x7f) * sizeof(int);
277             *funcs = (LPVOID)((BYTE *)debug - addr);
278             break;
279         }
280 #endif  /* __i386__ */
281     }
282
283     /* Build the names table */
284
285     for (i = 0; i < exp->NumberOfNames; i++, names++)
286         if (dll->descr->names[i])
287             *names = (LPSTR)((BYTE *)dll->descr->names[i] - addr);
288
289     return (HMODULE)addr;
290 }
291
292 /***********************************************************************
293  *           BUILTIN32_LoadImage
294  *
295  * Load a built-in module. If the 'force' parameter is FALSE, we only
296  * load the module if it has not been disabled via the -dll option.
297  */
298 HMODULE BUILTIN32_LoadImage( LPCSTR name, OFSTRUCT *ofs, BOOL force )
299 {
300     BUILTIN32_DLL *table;
301     char dllname[16], *p;
302
303     /* Fix the name in case we have a full path and extension */
304
305     if ((p = strrchr( name, '\\' ))) name = p + 1;
306     lstrcpynA( dllname, name, sizeof(dllname) );
307     if ((p = strrchr( dllname, '.' ))) *p = '\0';
308
309     for (table = BuiltinDLLs; table->descr; table++)
310         if (!lstrcmpiA( table->descr->name, dllname )) break;
311     if (!table->descr) return 0;
312     if (!table->used)
313     {
314         if (!force) return 0;
315         table->used = TRUE;  /* So next time we use it at once */
316     }
317
318     sprintf( ofs->szPathName, "%s.DLL", table->descr->name );
319     return BUILTIN32_DoLoadImage( table );
320 }
321
322
323 /***********************************************************************
324  *           BUILTIN32_GetEntryPoint
325  *
326  * Return the name of the DLL entry point corresponding
327  * to a relay entry point address. This is used only by relay debugging.
328  *
329  * This function _must_ return the real entry point to call
330  * after the debug info is printed.
331  */
332 ENTRYPOINT32 BUILTIN32_GetEntryPoint( char *buffer, void *relay,
333                                       unsigned int *typemask )
334 {
335     BUILTIN32_DLL *dll;
336     HMODULE hModule;
337     int ordinal = 0, i;
338
339     /* First find the module */
340
341     for (dll = BuiltinDLLs; dll->descr; dll++)
342         if (dll->used 
343             && ((hModule = GetModuleHandleA(dll->descr->name)) != 0))
344         {
345             IMAGE_SECTION_HEADER *sec = PE_SECTIONS(hModule);
346             DEBUG_ENTRY_POINT *debug = 
347                  (DEBUG_ENTRY_POINT *)((DWORD)hModule + sec[1].VirtualAddress);
348             DEBUG_ENTRY_POINT *func = (DEBUG_ENTRY_POINT *)relay;
349
350             if (debug <= func && func < debug + dll->descr->nb_funcs)
351             {
352                 ordinal = func - debug;
353                 break;
354             }
355         }
356     
357     if (!dll->descr)
358         return (ENTRYPOINT32)NULL;
359
360     /* Now find the function */
361
362     for (i = 0; i < dll->descr->nb_names; i++)
363         if (dll->descr->ordinals[i] == ordinal) break;
364     assert( i < dll->descr->nb_names );
365
366     sprintf( buffer, "%s.%d: %s", dll->descr->name, ordinal + dll->descr->base,
367              dll->descr->names[i] );
368     *typemask = dll->descr->argtypes[ordinal];
369     return dll->descr->functions[ordinal];
370 }
371
372 /***********************************************************************
373  *           BUILTIN32_SwitchRelayDebug
374  *
375  * FIXME: enhance to do it module relative.
376  */
377 void BUILTIN32_SwitchRelayDebug(BOOL onoff) {
378     BUILTIN32_DLL *dll;
379     HMODULE hModule;
380     int i;
381
382 #ifdef __i386__
383     if (!(TRACE_ON(relay) || WARN_ON(relay)))
384         return;
385     for (dll = BuiltinDLLs; dll->descr; dll++) {
386         IMAGE_SECTION_HEADER *sec;
387         DEBUG_ENTRY_POINT *debug;
388         if (!dll->used || !(hModule = GetModuleHandleA(dll->descr->name)))
389             continue;
390
391         sec = PE_SECTIONS(hModule);
392         debug = (DEBUG_ENTRY_POINT *)((DWORD)hModule + sec[1].VirtualAddress);
393         for (i = 0; i < dll->descr->nb_funcs; i++,debug++) {
394             if (!dll->descr->functions[i]) continue;
395             if ((dll->descr->args[i]==0xff) || (dll->descr->args[i]==0xfe))
396                 continue;
397             if (onoff) {
398                 debug->call       = 0xe8; /* lcall relative */
399                 debug->callfrom32 = (DWORD)RELAY_CallFrom32 -
400                                     (DWORD)&debug->ret;
401             } else {
402                 debug->call       = 0xe9; /* ljmp relative */
403                 debug->callfrom32 = (DWORD)dll->descr->functions[i] -
404                                     (DWORD)&debug->ret;
405             }
406         }
407     }
408 #endif /* __i386__ */
409     return;
410 }
411
412 /***********************************************************************
413  *           BUILTIN32_Unimplemented
414  *
415  * This function is called for unimplemented 32-bit entry points (declared
416  * as 'stub' in the spec file).
417  */
418 void BUILTIN32_Unimplemented( const BUILTIN32_DESCRIPTOR *descr, int ordinal )
419 {
420     const char *func_name = "???";
421     int i;
422
423     __RESTORE_ES;  /* Just in case */
424
425     for (i = 0; i < descr->nb_names; i++)
426         if (descr->ordinals[i] + descr->base == ordinal) break;
427     if (i < descr->nb_names) func_name = descr->names[i];
428
429     MSG( "No handler for Win32 routine %s.%d: %s",
430              descr->name, ordinal, func_name );
431 #ifdef __GNUC__
432     MSG( " (called from %p)", __builtin_return_address(1) );
433 #endif
434     MSG( "\n" );
435     ExitProcess(1);
436 }
437
438 /***********************************************************************
439  *           BUILTIN32_ParseDLLOptions
440  *
441  * Set runtime DLL usage flags
442  */
443 BOOL BUILTIN32_ParseDLLOptions( char *str )
444 {
445     BUILTIN32_DLL *dll;
446     char *p,*last;
447
448     last = str;
449     while (*str)
450     {
451         while (*str && (*str==',' || isspace(*str))) str++;
452         if (!*str) {
453             *last = '\0'; /* cut off garbage at end at */
454             return TRUE;
455         }
456         if ((*str != '+') && (*str != '-')) return FALSE;
457         str++;
458         if (!(p = strchr( str, ',' ))) p = str + strlen(str);
459         while ((p > str) && isspace(p[-1])) p--;
460         if (p == str) return FALSE;
461         for (dll = BuiltinDLLs; dll->descr; dll++)
462         {
463             if (!lstrncmpiA( dll->descr->name, str, (int)(p-str) ))
464             {
465                 if (dll->descr->name[p-str]) /* partial match - skip */
466                         continue;
467                 dll->used = (str[-1]!='-');
468                 break;
469             }
470         }
471         if (!dll->descr) {
472             /* not found, but could get handled by BUILTIN_, so move last */
473             last = p;
474             str = p;
475         } else {
476             /* handled. cut out the "[+-]DLL," string, so it isn't handled
477              * by BUILTIN
478              */
479             if (*p) {
480                 memcpy(last,p,strlen(p)+1);
481                 str = last;
482             } else {
483                 *last = '\0';
484                 break;
485             }
486         }
487     }
488     return TRUE;
489 }
490
491
492
493 /***********************************************************************
494  *           BUILTIN32_PrintDLLs
495  *
496  * Print the list of built-in DLLs that can be disabled.
497  */
498 void BUILTIN32_PrintDLLs(void)
499 {
500     int i;
501     BUILTIN32_DLL *dll;
502
503     MSG("Available Win32 DLLs:\n");
504     for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
505         MSG("%-9s%c", dll->descr->name,
506                  ((++i) % 8) ? ' ' : '\n' );
507     MSG("\n");
508 }