Recovery of release 990110 after disk crash.
[wine/multimedia.git] / relay32 / builtin32.c
blobc066e22399ba07048c68c9002137e96c1172b231
1 /*
2 * Win32 builtin functions
4 * Copyright 1997 Alexandre Julliard
5 */
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"
15 typedef struct
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;
23 typedef struct
25 const BUILTIN32_DESCRIPTOR *descr; /* DLL descriptor */
26 BOOL32 used; /* Used by default */
27 } BUILTIN32_DLL;
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;
69 static BUILTIN32_DLL BuiltinDLLs[] =
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, TRUE },
80 { &DPLAYX_Descriptor, TRUE },
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 }
114 /***********************************************************************
115 * BUILTIN32_DoLoadImage
117 * Load a built-in Win32 module. Helper function for BUILTIN32_LoadImage.
119 static HMODULE32 BUILTIN32_DoLoadImage( BUILTIN32_DLL *dll )
121 extern void RELAY_CallFrom32();
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;
134 /* Allocate the module */
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 (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);
156 /* Build the DOS and NT headers */
158 dos->e_magic = IMAGE_DOS_SIGNATURE;
159 dos->e_lfanew = sizeof(*dos);
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;
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;
184 /* Build the export directory */
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);
192 /* Build the exports section */
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);
203 /* Build the code section */
205 sec++;
206 strcpy( sec->Name, ".code" );
207 sec->SizeOfRawData = 0;
208 #ifdef __i386__
209 if (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);
218 /* Build the exports section data */
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);
228 /* Build the funcs table */
230 for (i = 0; i < dll->descr->nb_funcs; i++, funcs++, debug++)
232 BYTE args = dll->descr->args[i];
233 int j;
235 if (!dll->descr->functions[i]) continue;
236 *funcs = (LPVOID)((BYTE *)dll->descr->functions[i] - addr);
237 #ifdef __i386__
238 if (!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;
250 switch(args)
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 debug->call = 0xe8;
264 debug->callfrom32 = (DWORD)RELAY_CallFrom32 -
265 (DWORD)&debug->ret;
266 debug->ret = (args & 0x80) ? 0xc3 : 0xc2; /*ret/ret $n*/
267 debug->args = (args & 0x7f) * sizeof(int);
268 *funcs = (LPVOID)((BYTE *)debug - addr);
269 break;
271 #endif /* __i386__ */
274 /* Build the names table */
276 for (i = 0; i < exp->NumberOfNames; i++, names++)
277 if (dll->descr->names[i])
278 *names = (LPSTR)((BYTE *)dll->descr->names[i] - addr);
280 return (HMODULE32)addr;
283 /***********************************************************************
284 * BUILTIN32_LoadImage
286 * Load a built-in module. If the 'force' parameter is FALSE, we only
287 * load the module if it has not been disabled via the -dll option.
289 HMODULE32 BUILTIN32_LoadImage( LPCSTR name, OFSTRUCT *ofs, BOOL32 force )
291 BUILTIN32_DLL *table;
292 char dllname[16], *p;
294 /* Fix the name in case we have a full path and extension */
296 if ((p = strrchr( name, '\\' ))) name = p + 1;
297 lstrcpyn32A( dllname, name, sizeof(dllname) );
298 if ((p = strrchr( dllname, '.' ))) *p = '\0';
300 for (table = BuiltinDLLs; table->descr; table++)
301 if (!lstrcmpi32A( table->descr->name, dllname )) break;
302 if (!table->descr) return 0;
303 if (!table->used)
305 if (!force) return 0;
306 table->used = TRUE; /* So next time we use it at once */
309 sprintf( ofs->szPathName, "%s.DLL", table->descr->name );
310 return BUILTIN32_DoLoadImage( table );
314 /***********************************************************************
315 * BUILTIN32_GetEntryPoint
317 * Return the name of the DLL entry point corresponding
318 * to a relay entry point address. This is used only by relay debugging.
320 * This function _must_ return the real entry point to call
321 * after the debug info is printed.
323 ENTRYPOINT32 BUILTIN32_GetEntryPoint( char *buffer, void *relay,
324 unsigned int *typemask )
326 BUILTIN32_DLL *dll;
327 HMODULE32 hModule;
328 int ordinal = 0, i;
330 /* First find the module */
332 for (dll = BuiltinDLLs; dll->descr; dll++)
333 if (dll->used
334 && ((hModule = GetModuleHandle32A(dll->descr->name)) != 0))
336 IMAGE_SECTION_HEADER *sec = PE_SECTIONS(hModule);
337 DEBUG_ENTRY_POINT *debug =
338 (DEBUG_ENTRY_POINT *)((DWORD)hModule + sec[1].VirtualAddress);
339 DEBUG_ENTRY_POINT *func = (DEBUG_ENTRY_POINT *)relay;
341 if (debug <= func && func < debug + dll->descr->nb_funcs)
343 ordinal = func - debug;
344 break;
348 if (!dll->descr)
349 return (ENTRYPOINT32)NULL;
351 /* Now find the function */
353 for (i = 0; i < dll->descr->nb_names; i++)
354 if (dll->descr->ordinals[i] == ordinal) break;
355 assert( i < dll->descr->nb_names );
357 sprintf( buffer, "%s.%d: %s", dll->descr->name, ordinal + dll->descr->base,
358 dll->descr->names[i] );
359 *typemask = dll->descr->argtypes[ordinal];
360 return dll->descr->functions[ordinal];
364 /***********************************************************************
365 * BUILTIN32_Unimplemented
367 * This function is called for unimplemented 32-bit entry points (declared
368 * as 'stub' in the spec file).
370 void BUILTIN32_Unimplemented( const BUILTIN32_DESCRIPTOR *descr, int ordinal )
372 const char *func_name = "???";
373 int i;
375 __RESTORE_ES; /* Just in case */
377 for (i = 0; i < descr->nb_names; i++)
378 if (descr->ordinals[i] + descr->base == ordinal) break;
379 if (i < descr->nb_names) func_name = descr->names[i];
381 MSG( "No handler for Win32 routine %s.%d: %s",
382 descr->name, ordinal, func_name );
383 #ifdef __GNUC__
384 MSG( " (called from %p)", __builtin_return_address(1) );
385 #endif
386 MSG( "\n" );
387 ExitProcess(1);
391 /***********************************************************************
392 * BUILTIN32_EnableDLL
394 * Enable or disable a built-in DLL.
396 int BUILTIN32_EnableDLL( const char *name, int len, int enable )
398 int i;
399 BUILTIN32_DLL *dll;
401 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
403 if (!lstrncmpi32A( name, dll->descr->name, len ))
405 dll->used = enable;
406 return TRUE;
409 return FALSE;
413 /***********************************************************************
414 * BUILTIN32_PrintDLLs
416 * Print the list of built-in DLLs that can be disabled.
418 void BUILTIN32_PrintDLLs(void)
420 int i;
421 BUILTIN32_DLL *dll;
423 MSG("Available Win32 DLLs:\n");
424 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
425 MSG("%-9s%c", dll->descr->name,
426 ((++i) % 8) ? ' ' : '\n' );
427 MSG("\n");