Release 980104
[wine/multimedia.git] / relay32 / builtin32.c
blob764acad4f802b90bba1363636f40bc3db0200881
1 /*
2 * Win32 builtin functions
4 * Copyright 1997 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include "builtin32.h"
11 #include "module.h"
12 #include "task.h"
13 #include "process.h"
14 #include "stddebug.h"
15 #include "debug.h"
17 typedef struct
19 BYTE call; /* 0xe8 call callfrom32 (relative) */
20 DWORD callfrom32 WINE_PACKED; /* RELAY_CallFrom32 relative addr */
21 BYTE ret; /* 0xc2 ret $n or 0xc3 ret */
22 WORD args; /* nb of args to remove from the stack */
23 } DEBUG_ENTRY_POINT;
25 typedef struct
27 BYTE pushl; /* 0x68 pushl $func_to_call */
28 DWORD func WINE_PACKED; /* func to call */
29 BYTE jmp; /* 0xe9 jmp CALL32_Regs (relative) */
30 DWORD call32_regs WINE_PACKED; /* CALL32_Regs relative addr */
31 WORD nop; /* 0x9090 nop;nop */
32 } REG_ENTRY_POINT;
34 typedef struct
36 const BUILTIN32_DESCRIPTOR *descr; /* DLL descriptor */
37 DEBUG_ENTRY_POINT *dbg_funcs; /* Relay debugging functions table*/
38 BOOL32 used; /* Used by default */
39 } BUILTIN32_DLL;
42 extern const BUILTIN32_DESCRIPTOR ADVAPI32_Descriptor;
43 extern const BUILTIN32_DESCRIPTOR COMCTL32_Descriptor;
44 extern const BUILTIN32_DESCRIPTOR COMDLG32_Descriptor;
45 extern const BUILTIN32_DESCRIPTOR CRTDLL_Descriptor;
46 extern const BUILTIN32_DESCRIPTOR DCIMAN32_Descriptor;
47 extern const BUILTIN32_DESCRIPTOR DDRAW_Descriptor;
48 extern const BUILTIN32_DESCRIPTOR DSOUND_Descriptor;
49 extern const BUILTIN32_DESCRIPTOR GDI32_Descriptor;
50 extern const BUILTIN32_DESCRIPTOR KERNEL32_Descriptor;
51 extern const BUILTIN32_DESCRIPTOR LZ32_Descriptor;
52 extern const BUILTIN32_DESCRIPTOR MPR_Descriptor;
53 extern const BUILTIN32_DESCRIPTOR MSVFW32_Descriptor;
54 extern const BUILTIN32_DESCRIPTOR NTDLL_Descriptor;
55 extern const BUILTIN32_DESCRIPTOR OLE32_Descriptor;
56 extern const BUILTIN32_DESCRIPTOR OLECLI32_Descriptor;
57 extern const BUILTIN32_DESCRIPTOR OLESVR32_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 WOW32_Descriptor;
66 extern const BUILTIN32_DESCRIPTOR WSOCK32_Descriptor;
68 static BUILTIN32_DLL BuiltinDLLs[] =
70 { &ADVAPI32_Descriptor, NULL, TRUE },
71 { &COMCTL32_Descriptor, NULL, FALSE },
72 { &COMDLG32_Descriptor, NULL, TRUE },
73 { &CRTDLL_Descriptor, NULL, TRUE },
74 { &DCIMAN32_Descriptor, NULL, TRUE },
75 { &DDRAW_Descriptor, NULL, TRUE },
76 { &DSOUND_Descriptor, NULL, TRUE },
77 { &GDI32_Descriptor, NULL, TRUE },
78 { &KERNEL32_Descriptor, NULL, TRUE },
79 { &LZ32_Descriptor, NULL, TRUE },
80 { &MPR_Descriptor, NULL, TRUE },
81 { &MSVFW32_Descriptor, NULL, TRUE },
82 { &NTDLL_Descriptor, NULL, TRUE },
83 { &OLE32_Descriptor, NULL, FALSE },
84 { &OLECLI32_Descriptor, NULL, FALSE },
85 { &OLESVR32_Descriptor, NULL, FALSE },
86 { &SHELL32_Descriptor, NULL, TRUE },
87 { &TAPI32_Descriptor, NULL, TRUE },
88 { &USER32_Descriptor, NULL, TRUE },
89 { &VERSION_Descriptor, NULL, TRUE },
90 { &W32SKRNL_Descriptor, NULL, TRUE },
91 { &WINMM_Descriptor, NULL, TRUE },
92 { &WINSPOOL_Descriptor, NULL, TRUE },
93 { &WOW32_Descriptor, NULL, TRUE },
94 { &WSOCK32_Descriptor, NULL, TRUE },
95 /* Last entry */
96 { NULL, NULL, FALSE }
100 /***********************************************************************
101 * BUILTIN32_DoLoadModule
103 * Load a built-in Win32 module. Helper function for BUILTIN32_LoadModule.
105 static HMODULE32 BUILTIN32_DoLoadModule( BUILTIN32_DLL *dll )
107 extern void RELAY_CallFrom32();
108 extern void CALL32_Regs();
110 HMODULE16 hModule;
111 NE_MODULE *pModule;
112 OFSTRUCT ofs;
113 IMAGE_DATA_DIRECTORY *dir;
114 IMAGE_DOS_HEADER *dos;
115 IMAGE_NT_HEADERS *nt;
116 IMAGE_SECTION_HEADER *sec;
117 IMAGE_EXPORT_DIRECTORY *exp;
118 LPVOID *funcs;
119 LPSTR *names;
120 DEBUG_ENTRY_POINT *debug;
121 REG_ENTRY_POINT *regs;
122 PE_MODREF *pem;
123 INT32 i, size;
124 BYTE *addr;
126 /* Allocate the module */
128 size = (sizeof(IMAGE_DOS_HEADER)
129 + sizeof(IMAGE_NT_HEADERS)
130 + 2 * sizeof(IMAGE_SECTION_HEADER)
131 + sizeof(IMAGE_EXPORT_DIRECTORY)
132 + dll->descr->nb_funcs * sizeof(LPVOID)
133 + dll->descr->nb_names * sizeof(LPSTR)
134 + dll->descr->nb_reg_funcs * sizeof(REG_ENTRY_POINT));
135 #ifdef __i386__
136 if (debugging_relay)
137 size += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
138 #endif
139 addr = VirtualAlloc( NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
140 if (!addr) return 0;
141 dos = (IMAGE_DOS_HEADER *)addr;
142 nt = (IMAGE_NT_HEADERS *)(dos + 1);
143 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
144 exp = (IMAGE_EXPORT_DIRECTORY *)(sec + 2);
145 funcs = (LPVOID *)(exp + 1);
146 names = (LPSTR *)(funcs + dll->descr->nb_funcs);
147 regs = (REG_ENTRY_POINT *)(names + dll->descr->nb_names);
148 debug = (DEBUG_ENTRY_POINT *)(regs + dll->descr->nb_reg_funcs);
150 /* Build the DOS and NT headers */
152 dos->e_magic = IMAGE_DOS_SIGNATURE;
153 dos->e_lfanew = sizeof(*dos);
155 nt->Signature = IMAGE_NT_SIGNATURE;
156 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
157 nt->FileHeader.NumberOfSections = 2; /* exports + code */
158 nt->FileHeader.SizeOfOptionalHeader = sizeof(nt->OptionalHeader);
159 nt->FileHeader.Characteristics = IMAGE_FILE_DLL;
161 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
162 nt->OptionalHeader.SizeOfCode = 0x1000;
163 nt->OptionalHeader.SizeOfInitializedData = 0;
164 nt->OptionalHeader.SizeOfUninitializedData = 0;
165 nt->OptionalHeader.ImageBase = (DWORD)addr;
166 nt->OptionalHeader.SectionAlignment = 0x1000;
167 nt->OptionalHeader.FileAlignment = 0x1000;
168 nt->OptionalHeader.MajorOperatingSystemVersion = 1;
169 nt->OptionalHeader.MinorOperatingSystemVersion = 0;
170 nt->OptionalHeader.MajorSubsystemVersion = 4;
171 nt->OptionalHeader.MinorSubsystemVersion = 0;
172 nt->OptionalHeader.SizeOfImage = size;
173 nt->OptionalHeader.SizeOfHeaders = (BYTE *)exp - addr;
174 nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
176 /* Build the export directory */
178 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
179 dir->VirtualAddress = (BYTE *)exp - addr;
180 dir->Size = sizeof(*exp)
181 + dll->descr->nb_funcs * sizeof(LPVOID)
182 + dll->descr->nb_names * sizeof(LPSTR);
184 /* Build the exports section */
186 strcpy( sec->Name, ".edata" );
187 sec->Misc.VirtualSize = dir->Size;
188 sec->VirtualAddress = (BYTE *)exp - addr;
189 sec->SizeOfRawData = dir->Size;
190 sec->PointerToRawData = (BYTE *)exp - addr;
191 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
192 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ |
193 IMAGE_SCN_MEM_WRITE);
195 /* Build the code section */
197 sec++;
198 strcpy( sec->Name, ".code" );
199 sec->SizeOfRawData = dll->descr->nb_reg_funcs * sizeof(REG_ENTRY_POINT);
200 #ifdef __i386__
201 if (debugging_relay)
202 sec->SizeOfRawData += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
203 #endif
204 sec->Misc.VirtualSize = sec->SizeOfRawData;
205 sec->VirtualAddress = (BYTE *)regs - addr;
206 sec->PointerToRawData = (BYTE *)regs - addr;
207 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
208 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
210 /* Build the exports section data */
212 exp->Name = (BYTE *)dll->descr->name - addr; /*??*/
213 exp->Base = dll->descr->base;
214 exp->NumberOfFunctions = dll->descr->nb_funcs;
215 exp->NumberOfNames = dll->descr->nb_names;
216 exp->AddressOfFunctions = (LPDWORD *)((BYTE *)funcs - addr);
217 exp->AddressOfNames = (LPDWORD *)((BYTE *)names - addr);
218 exp->AddressOfNameOrdinals = (LPWORD *)((BYTE *)dll->descr->ordinals - addr);
220 /* Build the funcs table */
222 if (debugging_relay) dll->dbg_funcs = debug;
223 for (i = 0; i < dll->descr->nb_funcs; i++, funcs++, debug++)
225 BYTE args = dll->descr->args[i];
226 if (!dll->descr->functions[i]) continue;
227 #ifdef __i386__
228 switch(args)
230 case 0xfe: /* register func */
231 regs->pushl = 0x68;
232 regs->func = (DWORD)dll->descr->functions[i];
233 regs->jmp = 0xe9;
234 regs->call32_regs = (DWORD)CALL32_Regs - (DWORD)&regs->nop;
235 regs->nop = 0x9090;
236 if (debugging_relay)
238 debug->call = 0xe8;
239 debug->callfrom32 = (DWORD)regs - (DWORD)&debug->ret;
240 debug->ret = 0x90; /* nop */
241 debug->args = 0;
242 *funcs = (LPVOID)((BYTE *)debug - addr);
244 else *funcs = (LPVOID)((BYTE *)regs - addr);
245 regs++;
246 break;
247 case 0xff: /* stub or extern */
248 *funcs = (LPVOID)((BYTE *)dll->descr->functions[i] - addr);
249 break;
250 default: /* normal function (stdcall or cdecl) */
251 if (debugging_relay)
253 debug->call = 0xe8;
254 debug->callfrom32 = (DWORD)RELAY_CallFrom32 -
255 (DWORD)&debug->ret;
256 debug->ret = (args & 0x80) ? 0xc3 : 0xc2; /*ret/ret $n*/
257 debug->args = (args & 0x7f) * sizeof(int);
258 *funcs = (LPVOID)((BYTE *)debug - addr);
260 else
261 *funcs = (LPVOID)((BYTE *)dll->descr->functions[i] - addr);
262 break;
264 #else /* __i386__ */
265 *funcs = (LPVOID)((BYTE *)dll->descr->functions[i] - addr);
266 #endif /* __i386__ */
269 /* Build the names table */
271 for (i = 0; i < exp->NumberOfNames; i++, names++)
272 if (dll->descr->names[i])
273 *names = (LPSTR)((BYTE *)dll->descr->names[i] - addr);
275 /* Create a modref */
277 pem = (PE_MODREF *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
278 sizeof(*pem) );
279 pem->module = (HMODULE32)addr;
280 pem->pe_export = exp;
281 pem->next = pCurrentProcess->modref_list;
282 pCurrentProcess->modref_list = pem;
284 /* Create a Win16 dummy module */
286 sprintf( ofs.szPathName, "%s.DLL", dll->descr->name );
287 hModule = MODULE_CreateDummyModule( &ofs );
288 pModule = (NE_MODULE *)GlobalLock16( hModule );
289 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN |
290 NE_FFLAGS_LIBMODULE | NE_FFLAGS_WIN32;
291 pModule->module32 = (HMODULE32)addr;
292 return pModule->module32;
296 /***********************************************************************
297 * BUILTIN32_LoadModule
299 * Load a built-in module. If the 'force' parameter is FALSE, we only
300 * load the module if it has not been disabled via the -dll option.
302 HMODULE32 BUILTIN32_LoadModule( LPCSTR name, BOOL32 force )
304 BUILTIN32_DLL *table;
305 char dllname[16], *p;
307 /* Fix the name in case we have a full path and extension */
309 if ((p = strrchr( name, '\\' ))) name = p + 1;
310 lstrcpyn32A( dllname, name, sizeof(dllname) );
311 if ((p = strrchr( dllname, '.' ))) *p = '\0';
313 for (table = BuiltinDLLs; table->descr; table++)
314 if (!lstrcmpi32A( table->descr->name, dllname )) break;
315 if (!table->descr) return 0;
316 if (!table->used && !force) return 0;
318 return BUILTIN32_DoLoadModule( table );
322 /***********************************************************************
323 * BUILTIN32_GetEntryPoint
325 * Return the name of the DLL entry point corresponding
326 * to a relay entry point address. This is used only by relay debugging.
328 * This function _must_ return the real entry point to call
329 * after the debug info is printed.
331 ENTRYPOINT32 BUILTIN32_GetEntryPoint( char *buffer, void *relay,
332 unsigned int *typemask )
334 BUILTIN32_DLL *dll;
335 int ordinal, i;
337 /* First find the module */
339 for (dll = BuiltinDLLs; dll->descr; dll++)
340 if (((void *)dll->dbg_funcs <= relay) &&
341 ((void *)(dll->dbg_funcs + dll->descr->nb_funcs) > relay))
342 break;
343 assert(dll->descr);
345 /* Now find the function */
347 ordinal = ((DWORD)relay-(DWORD)dll->dbg_funcs) / sizeof(DEBUG_ENTRY_POINT);
348 for (i = 0; i < dll->descr->nb_names; i++)
349 if (dll->descr->ordinals[i] == ordinal) break;
350 assert( i < dll->descr->nb_names );
352 sprintf( buffer, "%s.%d: %s", dll->descr->name, ordinal + dll->descr->base,
353 dll->descr->names[i] );
354 *typemask = dll->descr->argtypes[ordinal];
355 return dll->descr->functions[ordinal];
359 /***********************************************************************
360 * BUILTIN32_Unimplemented
362 * This function is called for unimplemented 32-bit entry points (declared
363 * as 'stub' in the spec file).
365 void BUILTIN32_Unimplemented( const BUILTIN32_DESCRIPTOR *descr, int ordinal )
367 const char *func_name = "???";
368 int i;
370 __RESTORE_ES; /* Just in case */
372 for (i = 0; i < descr->nb_names; i++)
373 if (descr->ordinals[i] + descr->base == ordinal) break;
374 if (i < descr->nb_names) func_name = descr->names[i];
376 fprintf( stderr, "No handler for Win32 routine %s.%d: %s",
377 descr->name, ordinal, func_name );
378 #ifdef __GNUC__
379 fprintf( stderr, " (called from %p)", __builtin_return_address(1) );
380 #endif
381 fprintf( stderr, "\n" );
382 TASK_KillCurrentTask(1);
386 /***********************************************************************
387 * BUILTIN32_EnableDLL
389 * Enable or disable a built-in DLL.
391 int BUILTIN32_EnableDLL( const char *name, int len, int enable )
393 int i;
394 BUILTIN32_DLL *dll;
396 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
398 if (!lstrncmpi32A( name, dll->descr->name, len ))
400 dll->used = enable;
401 return TRUE;
404 return FALSE;
408 /***********************************************************************
409 * BUILTIN32_PrintDLLs
411 * Print the list of built-in DLLs that can be disabled.
413 void BUILTIN32_PrintDLLs(void)
415 int i;
416 BUILTIN32_DLL *dll;
418 fprintf(stderr,"Available Win32 DLLs:\n");
419 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
420 fprintf( stderr, "%-9s%c", dll->descr->name,
421 ((++i) % 8) ? ' ' : '\n' );
422 fprintf(stderr,"\n");