Mask out the upper 12 bits from the resourceid, just like win95 does.
[wine/multimedia.git] / relay32 / builtin32.c
blobf2742854263c24651ffb3d5872acb3375149c8c8
1 /*
2 * Win32 builtin functions
4 * Copyright 1997 Alexandre Julliard
5 */
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"
18 typedef struct
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;
26 typedef struct
28 const BUILTIN32_DESCRIPTOR *descr; /* DLL descriptor */
29 BOOL used; /* Used by default */
30 } BUILTIN32_DLL;
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;
72 static BUILTIN32_DLL BuiltinDLLs[] =
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 }
116 extern void RELAY_CallFrom32();
118 /***********************************************************************
119 * BUILTIN32_DoLoadImage
121 * Load a built-in Win32 module. Helper function for BUILTIN32_LoadImage.
123 static HMODULE BUILTIN32_DoLoadImage( BUILTIN32_DLL *dll )
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;
137 /* Allocate the module */
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);
159 /* Build the DOS and NT headers */
161 dos->e_magic = IMAGE_DOS_SIGNATURE;
162 dos->e_lfanew = sizeof(*dos);
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;
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;
187 /* Build the export directory */
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);
195 /* Build the exports section */
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);
206 /* Build the code section */
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);
221 /* Build the exports section data */
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);
231 /* Build the funcs table */
233 for (i = 0; i < dll->descr->nb_funcs; i++, funcs++, debug++)
235 BYTE args = dll->descr->args[i];
236 int j;
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;
253 switch(args)
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;
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;
280 #endif /* __i386__ */
283 /* Build the names table */
285 for (i = 0; i < exp->NumberOfNames; i++, names++)
286 if (dll->descr->names[i])
287 *names = (LPSTR)((BYTE *)dll->descr->names[i] - addr);
289 return (HMODULE)addr;
292 /***********************************************************************
293 * BUILTIN32_LoadImage
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.
298 HMODULE BUILTIN32_LoadImage( LPCSTR name, OFSTRUCT *ofs, BOOL force )
300 BUILTIN32_DLL *table;
301 char dllname[16], *p;
303 /* Fix the name in case we have a full path and extension */
305 if ((p = strrchr( name, '\\' ))) name = p + 1;
306 lstrcpynA( dllname, name, sizeof(dllname) );
307 if ((p = strrchr( dllname, '.' ))) *p = '\0';
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)
314 if (!force) return 0;
315 table->used = TRUE; /* So next time we use it at once */
318 sprintf( ofs->szPathName, "%s.DLL", table->descr->name );
319 return BUILTIN32_DoLoadImage( table );
323 /***********************************************************************
324 * BUILTIN32_GetEntryPoint
326 * Return the name of the DLL entry point corresponding
327 * to a relay entry point address. This is used only by relay debugging.
329 * This function _must_ return the real entry point to call
330 * after the debug info is printed.
332 ENTRYPOINT32 BUILTIN32_GetEntryPoint( char *buffer, void *relay,
333 unsigned int *typemask )
335 BUILTIN32_DLL *dll;
336 HMODULE hModule;
337 int ordinal = 0, i;
339 /* First find the module */
341 for (dll = BuiltinDLLs; dll->descr; dll++)
342 if (dll->used
343 && ((hModule = GetModuleHandleA(dll->descr->name)) != 0))
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;
350 if (debug <= func && func < debug + dll->descr->nb_funcs)
352 ordinal = func - debug;
353 break;
357 if (!dll->descr)
358 return (ENTRYPOINT32)NULL;
360 /* Now find the function */
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 );
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];
372 /***********************************************************************
373 * BUILTIN32_SwitchRelayDebug
375 * FIXME: enhance to do it module relative.
377 void BUILTIN32_SwitchRelayDebug(BOOL onoff) {
378 BUILTIN32_DLL *dll;
379 HMODULE hModule;
380 int i;
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;
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;
408 #endif /* __i386__ */
409 return;
412 /***********************************************************************
413 * BUILTIN32_Unimplemented
415 * This function is called for unimplemented 32-bit entry points (declared
416 * as 'stub' in the spec file).
418 void BUILTIN32_Unimplemented( const BUILTIN32_DESCRIPTOR *descr, int ordinal )
420 const char *func_name = "???";
421 int i;
423 __RESTORE_ES; /* Just in case */
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];
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);
439 /***********************************************************************
440 * BUILTIN32_EnableDLL
442 * Enable or disable a built-in DLL.
444 int BUILTIN32_EnableDLL( const char *name, int len, int enable )
446 int i;
447 BUILTIN32_DLL *dll;
449 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
451 if (!lstrncmpiA( name, dll->descr->name, len ))
453 dll->used = enable;
454 return TRUE;
457 return FALSE;
461 /***********************************************************************
462 * BUILTIN32_PrintDLLs
464 * Print the list of built-in DLLs that can be disabled.
466 void BUILTIN32_PrintDLLs(void)
468 int i;
469 BUILTIN32_DLL *dll;
471 MSG("Available Win32 DLLs:\n");
472 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
473 MSG("%-9s%c", dll->descr->name,
474 ((++i) % 8) ? ' ' : '\n' );
475 MSG("\n");