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