Release 980913
[wine/multimedia.git] / relay32 / builtin32.c
blob18081a987e4c52721dab73a2a0858e282f5487d5
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 "module.h"
11 #include "heap.h"
12 #include "task.h"
13 #include "process.h"
14 #include "debug.h"
16 typedef struct
18 BYTE call; /* 0xe8 call callfrom32 (relative) */
19 DWORD callfrom32 WINE_PACKED; /* RELAY_CallFrom32 relative addr */
20 BYTE ret; /* 0xc2 ret $n or 0xc3 ret */
21 WORD args; /* nb of args to remove from the stack */
22 } DEBUG_ENTRY_POINT;
24 typedef struct
26 const BUILTIN32_DESCRIPTOR *descr; /* DLL descriptor */
27 BOOL32 used; /* Used by default */
28 } BUILTIN32_DLL;
31 extern const BUILTIN32_DESCRIPTOR ADVAPI32_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 IMM32_Descriptor;
43 extern const BUILTIN32_DESCRIPTOR KERNEL32_Descriptor;
44 extern const BUILTIN32_DESCRIPTOR LZ32_Descriptor;
45 extern const BUILTIN32_DESCRIPTOR MPR_Descriptor;
46 extern const BUILTIN32_DESCRIPTOR MSNET32_Descriptor;
47 extern const BUILTIN32_DESCRIPTOR MSVFW32_Descriptor;
48 extern const BUILTIN32_DESCRIPTOR NTDLL_Descriptor;
49 extern const BUILTIN32_DESCRIPTOR OLE32_Descriptor;
50 extern const BUILTIN32_DESCRIPTOR OLEAUT32_Descriptor;
51 extern const BUILTIN32_DESCRIPTOR OLECLI32_Descriptor;
52 extern const BUILTIN32_DESCRIPTOR OLEDLG_Descriptor;
53 extern const BUILTIN32_DESCRIPTOR OLESVR32_Descriptor;
54 extern const BUILTIN32_DESCRIPTOR RASAPI32_Descriptor;
55 extern const BUILTIN32_DESCRIPTOR SHELL32_Descriptor;
56 extern const BUILTIN32_DESCRIPTOR TAPI32_Descriptor;
57 extern const BUILTIN32_DESCRIPTOR USER32_Descriptor;
58 extern const BUILTIN32_DESCRIPTOR VERSION_Descriptor;
59 extern const BUILTIN32_DESCRIPTOR W32SKRNL_Descriptor;
60 extern const BUILTIN32_DESCRIPTOR WINMM_Descriptor;
61 extern const BUILTIN32_DESCRIPTOR WINSPOOL_Descriptor;
62 extern const BUILTIN32_DESCRIPTOR WNASPI32_Descriptor;
63 extern const BUILTIN32_DESCRIPTOR WOW32_Descriptor;
64 extern const BUILTIN32_DESCRIPTOR WSOCK32_Descriptor;
66 static BUILTIN32_DLL BuiltinDLLs[] =
68 { &ADVAPI32_Descriptor, TRUE },
69 { &COMCTL32_Descriptor, FALSE },
70 { &COMDLG32_Descriptor, TRUE },
71 { &CRTDLL_Descriptor, TRUE },
72 { &DCIMAN32_Descriptor, FALSE },
73 { &DDRAW_Descriptor, TRUE },
74 { &DINPUT_Descriptor, TRUE },
75 { &DPLAY_Descriptor, TRUE },
76 { &DPLAYX_Descriptor, TRUE },
77 { &DSOUND_Descriptor, TRUE },
78 { &GDI32_Descriptor, TRUE },
79 { &IMM32_Descriptor, FALSE },
80 { &KERNEL32_Descriptor, TRUE },
81 { &LZ32_Descriptor, TRUE },
82 { &MPR_Descriptor, TRUE },
83 { &MSNET32_Descriptor, FALSE },
84 { &MSVFW32_Descriptor, FALSE },
85 { &NTDLL_Descriptor, TRUE },
86 { &OLE32_Descriptor, FALSE },
87 { &OLEAUT32_Descriptor, FALSE },
88 { &OLECLI32_Descriptor, FALSE },
89 { &OLEDLG_Descriptor, FALSE },
90 { &OLESVR32_Descriptor, FALSE },
91 { &RASAPI32_Descriptor, FALSE },
92 { &SHELL32_Descriptor, TRUE },
93 { &TAPI32_Descriptor, FALSE },
94 { &USER32_Descriptor, TRUE },
95 { &VERSION_Descriptor, TRUE },
96 { &W32SKRNL_Descriptor, TRUE },
97 { &WINMM_Descriptor, TRUE },
98 { &WINSPOOL_Descriptor, TRUE },
99 { &WNASPI32_Descriptor, TRUE },
100 { &WOW32_Descriptor, TRUE },
101 { &WSOCK32_Descriptor, TRUE },
102 /* Last entry */
103 { NULL, FALSE }
107 /***********************************************************************
108 * BUILTIN32_DoLoadModule
110 * Load a built-in Win32 module. Helper function for BUILTIN32_LoadModule.
112 static HMODULE32 BUILTIN32_DoLoadModule( BUILTIN32_DLL *dll, PDB32 *pdb )
114 extern void RELAY_CallFrom32();
116 HMODULE16 hModule;
117 NE_MODULE *pModule;
118 OFSTRUCT ofs;
119 IMAGE_DATA_DIRECTORY *dir;
120 IMAGE_DOS_HEADER *dos;
121 IMAGE_NT_HEADERS *nt;
122 IMAGE_SECTION_HEADER *sec;
123 IMAGE_EXPORT_DIRECTORY *exp;
124 LPVOID *funcs;
125 LPSTR *names;
126 DEBUG_ENTRY_POINT *debug;
127 WINE_MODREF *wm;
128 PE_MODREF *pem;
129 INT32 i, size;
130 BYTE *addr;
132 /* Allocate the module */
134 size = (sizeof(IMAGE_DOS_HEADER)
135 + sizeof(IMAGE_NT_HEADERS)
136 + 2 * sizeof(IMAGE_SECTION_HEADER)
137 + sizeof(IMAGE_EXPORT_DIRECTORY)
138 + dll->descr->nb_funcs * sizeof(LPVOID)
139 + dll->descr->nb_names * sizeof(LPSTR));
140 #ifdef __i386__
141 if (TRACE_ON(relay))
142 size += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
143 #endif
144 addr = VirtualAlloc( NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
145 if (!addr) return 0;
146 dos = (IMAGE_DOS_HEADER *)addr;
147 nt = (IMAGE_NT_HEADERS *)(dos + 1);
148 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
149 exp = (IMAGE_EXPORT_DIRECTORY *)(sec + 2);
150 funcs = (LPVOID *)(exp + 1);
151 names = (LPSTR *)(funcs + dll->descr->nb_funcs);
152 debug = (DEBUG_ENTRY_POINT *)(names + dll->descr->nb_names);
154 /* Build the DOS and NT headers */
156 dos->e_magic = IMAGE_DOS_SIGNATURE;
157 dos->e_lfanew = sizeof(*dos);
159 nt->Signature = IMAGE_NT_SIGNATURE;
160 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
161 nt->FileHeader.NumberOfSections = 2; /* exports + code */
162 nt->FileHeader.SizeOfOptionalHeader = sizeof(nt->OptionalHeader);
163 nt->FileHeader.Characteristics = IMAGE_FILE_DLL;
165 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
166 nt->OptionalHeader.SizeOfCode = 0x1000;
167 nt->OptionalHeader.SizeOfInitializedData = 0;
168 nt->OptionalHeader.SizeOfUninitializedData = 0;
169 nt->OptionalHeader.ImageBase = (DWORD)addr;
170 nt->OptionalHeader.SectionAlignment = 0x1000;
171 nt->OptionalHeader.FileAlignment = 0x1000;
172 nt->OptionalHeader.MajorOperatingSystemVersion = 1;
173 nt->OptionalHeader.MinorOperatingSystemVersion = 0;
174 nt->OptionalHeader.MajorSubsystemVersion = 4;
175 nt->OptionalHeader.MinorSubsystemVersion = 0;
176 nt->OptionalHeader.SizeOfImage = size;
177 nt->OptionalHeader.SizeOfHeaders = (BYTE *)exp - addr;
178 nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
179 if (dll->descr->dllentrypoint)
180 nt->OptionalHeader.AddressOfEntryPoint = (DWORD)dll->descr->dllentrypoint - (DWORD)addr;
182 /* Build the export directory */
184 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
185 dir->VirtualAddress = (BYTE *)exp - addr;
186 dir->Size = sizeof(*exp)
187 + dll->descr->nb_funcs * sizeof(LPVOID)
188 + dll->descr->nb_names * sizeof(LPSTR);
190 /* Build the exports section */
192 strcpy( sec->Name, ".edata" );
193 sec->Misc.VirtualSize = dir->Size;
194 sec->VirtualAddress = (BYTE *)exp - addr;
195 sec->SizeOfRawData = dir->Size;
196 sec->PointerToRawData = (BYTE *)exp - addr;
197 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
198 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ |
199 IMAGE_SCN_MEM_WRITE);
201 /* Build the code section */
203 sec++;
204 strcpy( sec->Name, ".code" );
205 sec->SizeOfRawData = 0;
206 #ifdef __i386__
207 if (TRACE_ON(relay))
208 sec->SizeOfRawData += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
209 #endif
210 sec->Misc.VirtualSize = sec->SizeOfRawData;
211 sec->VirtualAddress = (BYTE *)debug - addr;
212 sec->PointerToRawData = (BYTE *)debug - addr;
213 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
214 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
216 /* Build the exports section data */
218 exp->Name = ((BYTE *)dll->descr->name) - addr; /*??*/
219 exp->Base = dll->descr->base;
220 exp->NumberOfFunctions = dll->descr->nb_funcs;
221 exp->NumberOfNames = dll->descr->nb_names;
222 exp->AddressOfFunctions = (LPDWORD *)((BYTE *)funcs - addr);
223 exp->AddressOfNames = (LPDWORD *)((BYTE *)names - addr);
224 exp->AddressOfNameOrdinals = (LPWORD *)((BYTE *)dll->descr->ordinals - addr);
226 /* Build the funcs table */
228 for (i = 0; i < dll->descr->nb_funcs; i++, funcs++, debug++)
230 BYTE args = dll->descr->args[i];
231 if (!dll->descr->functions[i]) continue;
232 *funcs = (LPVOID)((BYTE *)dll->descr->functions[i] - addr);
233 #ifdef __i386__
234 if (!TRACE_ON(relay)) continue;
235 switch(args)
237 case 0xfe: /* register func */
238 debug->call = 0xe8;
239 debug->callfrom32 = (DWORD)dll->descr->functions[i] -
240 (DWORD)&debug->ret;
241 debug->ret = 0x90; /* nop */
242 debug->args = 0;
243 *funcs = (LPVOID)((BYTE *)debug - addr);
244 break;
245 case 0xff: /* stub or extern */
246 break;
247 default: /* normal function (stdcall or cdecl) */
248 debug->call = 0xe8;
249 debug->callfrom32 = (DWORD)RELAY_CallFrom32 -
250 (DWORD)&debug->ret;
251 debug->ret = (args & 0x80) ? 0xc3 : 0xc2; /*ret/ret $n*/
252 debug->args = (args & 0x7f) * sizeof(int);
253 *funcs = (LPVOID)((BYTE *)debug - addr);
254 break;
256 #endif /* __i386__ */
259 /* Build the names table */
261 for (i = 0; i < exp->NumberOfNames; i++, names++)
262 if (dll->descr->names[i])
263 *names = (LPSTR)((BYTE *)dll->descr->names[i] - addr);
265 /* Create a modref */
266 wm = (WINE_MODREF *)HeapAlloc( pdb->heap, HEAP_ZERO_MEMORY, sizeof(*wm) );
267 wm->type = MODULE32_PE;
268 pem = &(wm->binfmt.pe);
269 wm->module = (HMODULE32)addr;
270 wm->next = pdb->modref_list;
271 pdb->modref_list = wm;
272 wm->modname = HEAP_strdupA(pdb->heap,0,dll->descr->name);
273 /* FIXME: hmm ... probably add windows directory? don't know ... -MM */
274 wm->shortname = HEAP_strdupA(pdb->heap,0,wm->modname);
275 wm->longname = HEAP_strdupA(pdb->heap,0,wm->modname);
277 pem->pe_export = exp;
278 pem->flags = PE_MODREF_INTERNAL;
280 /* Create a Win16 dummy module */
282 sprintf( ofs.szPathName, "%s.DLL", dll->descr->name );
283 hModule = MODULE_CreateDummyModule( &ofs );
284 pModule = (NE_MODULE *)GlobalLock16( hModule );
285 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN |
286 NE_FFLAGS_LIBMODULE | NE_FFLAGS_WIN32;
287 pModule->module32 = (HMODULE32)addr;
288 return pModule->module32;
292 /***********************************************************************
293 * BUILTIN32_LoadModule
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 HMODULE32 BUILTIN32_LoadModule( LPCSTR name, BOOL32 force, PDB32 *process )
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 lstrcpyn32A( dllname, name, sizeof(dllname) );
307 if ((p = strrchr( dllname, '.' ))) *p = '\0';
309 for (table = BuiltinDLLs; table->descr; table++)
310 if (!lstrcmpi32A( 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 */
317 return BUILTIN32_DoLoadModule( table, process );
321 /***********************************************************************
322 * BUILTIN32_GetEntryPoint
324 * Return the name of the DLL entry point corresponding
325 * to a relay entry point address. This is used only by relay debugging.
327 * This function _must_ return the real entry point to call
328 * after the debug info is printed.
330 ENTRYPOINT32 BUILTIN32_GetEntryPoint( char *buffer, void *relay,
331 unsigned int *typemask )
333 BUILTIN32_DLL *dll;
334 HMODULE32 hModule;
335 int ordinal = 0, i;
337 /* First find the module */
339 for (dll = BuiltinDLLs; dll->descr; dll++)
340 if (dll->used
341 && ((hModule = GetModuleHandle32A(dll->descr->name)) != 0))
343 IMAGE_SECTION_HEADER *sec = PE_SECTIONS(hModule);
344 DEBUG_ENTRY_POINT *debug =
345 (DEBUG_ENTRY_POINT *)((DWORD)hModule + sec[1].VirtualAddress);
346 DEBUG_ENTRY_POINT *func = (DEBUG_ENTRY_POINT *)relay;
348 if (debug <= func && func < debug + dll->descr->nb_funcs)
350 ordinal = func - debug;
351 break;
355 if (!dll->descr)
356 return (ENTRYPOINT32)NULL;
358 /* Now find the function */
360 for (i = 0; i < dll->descr->nb_names; i++)
361 if (dll->descr->ordinals[i] == ordinal) break;
362 assert( i < dll->descr->nb_names );
364 sprintf( buffer, "%s.%d: %s", dll->descr->name, ordinal + dll->descr->base,
365 dll->descr->names[i] );
366 *typemask = dll->descr->argtypes[ordinal];
367 return dll->descr->functions[ordinal];
371 /***********************************************************************
372 * BUILTIN32_Unimplemented
374 * This function is called for unimplemented 32-bit entry points (declared
375 * as 'stub' in the spec file).
377 void BUILTIN32_Unimplemented( const BUILTIN32_DESCRIPTOR *descr, int ordinal )
379 const char *func_name = "???";
380 int i;
382 __RESTORE_ES; /* Just in case */
384 for (i = 0; i < descr->nb_names; i++)
385 if (descr->ordinals[i] + descr->base == ordinal) break;
386 if (i < descr->nb_names) func_name = descr->names[i];
388 MSG( "No handler for Win32 routine %s.%d: %s",
389 descr->name, ordinal, func_name );
390 #ifdef __GNUC__
391 MSG( " (called from %p)", __builtin_return_address(1) );
392 #endif
393 MSG( "\n" );
394 ExitProcess(1);
398 /***********************************************************************
399 * BUILTIN32_EnableDLL
401 * Enable or disable a built-in DLL.
403 int BUILTIN32_EnableDLL( const char *name, int len, int enable )
405 int i;
406 BUILTIN32_DLL *dll;
408 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
410 if (!lstrncmpi32A( name, dll->descr->name, len ))
412 dll->used = enable;
413 return TRUE;
416 return FALSE;
420 /***********************************************************************
421 * BUILTIN32_PrintDLLs
423 * Print the list of built-in DLLs that can be disabled.
425 void BUILTIN32_PrintDLLs(void)
427 int i;
428 BUILTIN32_DLL *dll;
430 MSG("Available Win32 DLLs:\n");
431 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
432 MSG("%-9s%c", dll->descr->name,
433 ((++i) % 8) ? ' ' : '\n' );
434 MSG("\n");