Release 981025.
[wine/gsoc-2012-control.git] / relay32 / builtin32.c
blob270548536daaba4f4e3f4a343432ccb25af8e5f6
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 AVIFIL32_Descriptor;
33 extern const BUILTIN32_DESCRIPTOR COMCTL32_Descriptor;
34 extern const BUILTIN32_DESCRIPTOR COMDLG32_Descriptor;
35 extern const BUILTIN32_DESCRIPTOR CRTDLL_Descriptor;
36 extern const BUILTIN32_DESCRIPTOR DCIMAN32_Descriptor;
37 extern const BUILTIN32_DESCRIPTOR DDRAW_Descriptor;
38 extern const BUILTIN32_DESCRIPTOR DINPUT_Descriptor;
39 extern const BUILTIN32_DESCRIPTOR DPLAY_Descriptor;
40 extern const BUILTIN32_DESCRIPTOR DPLAYX_Descriptor;
41 extern const BUILTIN32_DESCRIPTOR DSOUND_Descriptor;
42 extern const BUILTIN32_DESCRIPTOR GDI32_Descriptor;
43 extern const BUILTIN32_DESCRIPTOR IMAGEHLP_Descriptor;
44 extern const BUILTIN32_DESCRIPTOR IMM32_Descriptor;
45 extern const BUILTIN32_DESCRIPTOR KERNEL32_Descriptor;
46 extern const BUILTIN32_DESCRIPTOR LZ32_Descriptor;
47 extern const BUILTIN32_DESCRIPTOR MPR_Descriptor;
48 extern const BUILTIN32_DESCRIPTOR MSACM32_Descriptor;
49 extern const BUILTIN32_DESCRIPTOR MSNET32_Descriptor;
50 extern const BUILTIN32_DESCRIPTOR MSVFW32_Descriptor;
51 extern const BUILTIN32_DESCRIPTOR NTDLL_Descriptor;
52 extern const BUILTIN32_DESCRIPTOR OLE32_Descriptor;
53 extern const BUILTIN32_DESCRIPTOR OLEAUT32_Descriptor;
54 extern const BUILTIN32_DESCRIPTOR OLECLI32_Descriptor;
55 extern const BUILTIN32_DESCRIPTOR OLEDLG_Descriptor;
56 extern const BUILTIN32_DESCRIPTOR OLESVR32_Descriptor;
57 extern const BUILTIN32_DESCRIPTOR PSAPI_Descriptor;
58 extern const BUILTIN32_DESCRIPTOR RASAPI32_Descriptor;
59 extern const BUILTIN32_DESCRIPTOR SHELL32_Descriptor;
60 extern const BUILTIN32_DESCRIPTOR TAPI32_Descriptor;
61 extern const BUILTIN32_DESCRIPTOR USER32_Descriptor;
62 extern const BUILTIN32_DESCRIPTOR VERSION_Descriptor;
63 extern const BUILTIN32_DESCRIPTOR W32SKRNL_Descriptor;
64 extern const BUILTIN32_DESCRIPTOR WINMM_Descriptor;
65 extern const BUILTIN32_DESCRIPTOR WINSPOOL_Descriptor;
66 extern const BUILTIN32_DESCRIPTOR WNASPI32_Descriptor;
67 extern const BUILTIN32_DESCRIPTOR WOW32_Descriptor;
68 extern const BUILTIN32_DESCRIPTOR WSOCK32_Descriptor;
70 static BUILTIN32_DLL BuiltinDLLs[] =
72 { &ADVAPI32_Descriptor, TRUE },
73 { &AVIFIL32_Descriptor, FALSE },
74 { &COMCTL32_Descriptor, FALSE },
75 { &COMDLG32_Descriptor, TRUE },
76 { &CRTDLL_Descriptor, TRUE },
77 { &DCIMAN32_Descriptor, FALSE },
78 { &DDRAW_Descriptor, TRUE },
79 { &DINPUT_Descriptor, TRUE },
80 { &DPLAY_Descriptor, TRUE },
81 { &DPLAYX_Descriptor, TRUE },
82 { &DSOUND_Descriptor, TRUE },
83 { &GDI32_Descriptor, TRUE },
84 { &IMAGEHLP_Descriptor, FALSE },
85 { &IMM32_Descriptor, FALSE },
86 { &KERNEL32_Descriptor, TRUE },
87 { &LZ32_Descriptor, TRUE },
88 { &MPR_Descriptor, TRUE },
89 { &MSACM32_Descriptor, FALSE },
90 { &MSNET32_Descriptor, FALSE },
91 { &MSVFW32_Descriptor, FALSE },
92 { &NTDLL_Descriptor, TRUE },
93 { &OLE32_Descriptor, FALSE },
94 { &OLEAUT32_Descriptor, FALSE },
95 { &OLECLI32_Descriptor, FALSE },
96 { &OLEDLG_Descriptor, FALSE },
97 { &OLESVR32_Descriptor, FALSE },
98 { &PSAPI_Descriptor, FALSE },
99 { &RASAPI32_Descriptor, FALSE },
100 { &SHELL32_Descriptor, TRUE },
101 { &TAPI32_Descriptor, FALSE },
102 { &USER32_Descriptor, TRUE },
103 { &VERSION_Descriptor, TRUE },
104 { &W32SKRNL_Descriptor, TRUE },
105 { &WINMM_Descriptor, TRUE },
106 { &WINSPOOL_Descriptor, TRUE },
107 { &WNASPI32_Descriptor, TRUE },
108 { &WOW32_Descriptor, TRUE },
109 { &WSOCK32_Descriptor, TRUE },
110 /* Last entry */
111 { NULL, FALSE }
115 /***********************************************************************
116 * BUILTIN32_DoLoadModule
118 * Load a built-in Win32 module. Helper function for BUILTIN32_LoadModule.
120 static HMODULE32 BUILTIN32_DoLoadModule( BUILTIN32_DLL *dll, PDB32 *pdb )
122 extern void RELAY_CallFrom32();
124 HMODULE16 hModule;
125 NE_MODULE *pModule;
126 OFSTRUCT ofs;
127 IMAGE_DATA_DIRECTORY *dir;
128 IMAGE_DOS_HEADER *dos;
129 IMAGE_NT_HEADERS *nt;
130 IMAGE_SECTION_HEADER *sec;
131 IMAGE_EXPORT_DIRECTORY *exp;
132 LPVOID *funcs;
133 LPSTR *names;
134 DEBUG_ENTRY_POINT *debug;
135 WINE_MODREF *wm;
136 PE_MODREF *pem;
137 INT32 i, size;
138 BYTE *addr;
140 /* Allocate the module */
142 size = (sizeof(IMAGE_DOS_HEADER)
143 + sizeof(IMAGE_NT_HEADERS)
144 + 2 * sizeof(IMAGE_SECTION_HEADER)
145 + sizeof(IMAGE_EXPORT_DIRECTORY)
146 + dll->descr->nb_funcs * sizeof(LPVOID)
147 + dll->descr->nb_names * sizeof(LPSTR));
148 #ifdef __i386__
149 if (TRACE_ON(relay))
150 size += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
151 #endif
152 addr = VirtualAlloc( NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
153 if (!addr) return 0;
154 dos = (IMAGE_DOS_HEADER *)addr;
155 nt = (IMAGE_NT_HEADERS *)(dos + 1);
156 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
157 exp = (IMAGE_EXPORT_DIRECTORY *)(sec + 2);
158 funcs = (LPVOID *)(exp + 1);
159 names = (LPSTR *)(funcs + dll->descr->nb_funcs);
160 debug = (DEBUG_ENTRY_POINT *)(names + dll->descr->nb_names);
162 /* Build the DOS and NT headers */
164 dos->e_magic = IMAGE_DOS_SIGNATURE;
165 dos->e_lfanew = sizeof(*dos);
167 nt->Signature = IMAGE_NT_SIGNATURE;
168 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
169 nt->FileHeader.NumberOfSections = 2; /* exports + code */
170 nt->FileHeader.SizeOfOptionalHeader = sizeof(nt->OptionalHeader);
171 nt->FileHeader.Characteristics = IMAGE_FILE_DLL;
173 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
174 nt->OptionalHeader.SizeOfCode = 0x1000;
175 nt->OptionalHeader.SizeOfInitializedData = 0;
176 nt->OptionalHeader.SizeOfUninitializedData = 0;
177 nt->OptionalHeader.ImageBase = (DWORD)addr;
178 nt->OptionalHeader.SectionAlignment = 0x1000;
179 nt->OptionalHeader.FileAlignment = 0x1000;
180 nt->OptionalHeader.MajorOperatingSystemVersion = 1;
181 nt->OptionalHeader.MinorOperatingSystemVersion = 0;
182 nt->OptionalHeader.MajorSubsystemVersion = 4;
183 nt->OptionalHeader.MinorSubsystemVersion = 0;
184 nt->OptionalHeader.SizeOfImage = size;
185 nt->OptionalHeader.SizeOfHeaders = (BYTE *)exp - addr;
186 nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
187 if (dll->descr->dllentrypoint)
188 nt->OptionalHeader.AddressOfEntryPoint = (DWORD)dll->descr->dllentrypoint - (DWORD)addr;
190 /* Build the export directory */
192 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
193 dir->VirtualAddress = (BYTE *)exp - addr;
194 dir->Size = sizeof(*exp)
195 + dll->descr->nb_funcs * sizeof(LPVOID)
196 + dll->descr->nb_names * sizeof(LPSTR);
198 /* Build the exports section */
200 strcpy( sec->Name, ".edata" );
201 sec->Misc.VirtualSize = dir->Size;
202 sec->VirtualAddress = (BYTE *)exp - addr;
203 sec->SizeOfRawData = dir->Size;
204 sec->PointerToRawData = (BYTE *)exp - addr;
205 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
206 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ |
207 IMAGE_SCN_MEM_WRITE);
209 /* Build the code section */
211 sec++;
212 strcpy( sec->Name, ".code" );
213 sec->SizeOfRawData = 0;
214 #ifdef __i386__
215 if (TRACE_ON(relay))
216 sec->SizeOfRawData += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
217 #endif
218 sec->Misc.VirtualSize = sec->SizeOfRawData;
219 sec->VirtualAddress = (BYTE *)debug - addr;
220 sec->PointerToRawData = (BYTE *)debug - addr;
221 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
222 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
224 /* Build the exports section data */
226 exp->Name = ((BYTE *)dll->descr->name) - addr; /*??*/
227 exp->Base = dll->descr->base;
228 exp->NumberOfFunctions = dll->descr->nb_funcs;
229 exp->NumberOfNames = dll->descr->nb_names;
230 exp->AddressOfFunctions = (LPDWORD *)((BYTE *)funcs - addr);
231 exp->AddressOfNames = (LPDWORD *)((BYTE *)names - addr);
232 exp->AddressOfNameOrdinals = (LPWORD *)((BYTE *)dll->descr->ordinals - addr);
234 /* Build the funcs table */
236 for (i = 0; i < dll->descr->nb_funcs; i++, funcs++, debug++)
238 BYTE args = dll->descr->args[i];
239 if (!dll->descr->functions[i]) continue;
240 *funcs = (LPVOID)((BYTE *)dll->descr->functions[i] - addr);
241 #ifdef __i386__
242 if (!TRACE_ON(relay)) continue;
243 switch(args)
245 case 0xfe: /* register func */
246 debug->call = 0xe8;
247 debug->callfrom32 = (DWORD)dll->descr->functions[i] -
248 (DWORD)&debug->ret;
249 debug->ret = 0x90; /* nop */
250 debug->args = 0;
251 *funcs = (LPVOID)((BYTE *)debug - addr);
252 break;
253 case 0xff: /* stub or extern */
254 break;
255 default: /* normal function (stdcall or cdecl) */
256 debug->call = 0xe8;
257 debug->callfrom32 = (DWORD)RELAY_CallFrom32 -
258 (DWORD)&debug->ret;
259 debug->ret = (args & 0x80) ? 0xc3 : 0xc2; /*ret/ret $n*/
260 debug->args = (args & 0x7f) * sizeof(int);
261 *funcs = (LPVOID)((BYTE *)debug - addr);
262 break;
264 #endif /* __i386__ */
267 /* Build the names table */
269 for (i = 0; i < exp->NumberOfNames; i++, names++)
270 if (dll->descr->names[i])
271 *names = (LPSTR)((BYTE *)dll->descr->names[i] - addr);
273 /* Create a modref */
274 wm = (WINE_MODREF *)HeapAlloc( pdb->heap, HEAP_ZERO_MEMORY, sizeof(*wm) );
275 wm->type = MODULE32_PE;
276 pem = &(wm->binfmt.pe);
277 wm->module = (HMODULE32)addr;
278 wm->next = pdb->modref_list;
279 pdb->modref_list = wm;
280 wm->modname = HEAP_strdupA(pdb->heap,0,dll->descr->name);
281 /* FIXME: hmm ... probably add windows directory? don't know ... -MM */
282 wm->shortname = HEAP_strdupA(pdb->heap,0,wm->modname);
283 wm->longname = HEAP_strdupA(pdb->heap,0,wm->modname);
285 pem->pe_export = exp;
286 pem->flags = PE_MODREF_INTERNAL;
288 /* Create a Win16 dummy module */
290 sprintf( ofs.szPathName, "%s.DLL", dll->descr->name );
291 hModule = MODULE_CreateDummyModule( &ofs );
292 pModule = (NE_MODULE *)GlobalLock16( hModule );
293 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN |
294 NE_FFLAGS_LIBMODULE | NE_FFLAGS_WIN32;
295 pModule->module32 = (HMODULE32)addr;
296 return pModule->module32;
300 /***********************************************************************
301 * BUILTIN32_LoadModule
303 * Load a built-in module. If the 'force' parameter is FALSE, we only
304 * load the module if it has not been disabled via the -dll option.
306 HMODULE32 BUILTIN32_LoadModule( LPCSTR name, BOOL32 force, PDB32 *process )
308 BUILTIN32_DLL *table;
309 char dllname[16], *p;
311 /* Fix the name in case we have a full path and extension */
313 if ((p = strrchr( name, '\\' ))) name = p + 1;
314 lstrcpyn32A( dllname, name, sizeof(dllname) );
315 if ((p = strrchr( dllname, '.' ))) *p = '\0';
317 for (table = BuiltinDLLs; table->descr; table++)
318 if (!lstrcmpi32A( table->descr->name, dllname )) break;
319 if (!table->descr) return 0;
320 if (!table->used)
322 if (!force) return 0;
323 table->used = TRUE; /* So next time we use it at once */
325 return BUILTIN32_DoLoadModule( table, process );
329 /***********************************************************************
330 * BUILTIN32_GetEntryPoint
332 * Return the name of the DLL entry point corresponding
333 * to a relay entry point address. This is used only by relay debugging.
335 * This function _must_ return the real entry point to call
336 * after the debug info is printed.
338 ENTRYPOINT32 BUILTIN32_GetEntryPoint( char *buffer, void *relay,
339 unsigned int *typemask )
341 BUILTIN32_DLL *dll;
342 HMODULE32 hModule;
343 int ordinal = 0, i;
345 /* First find the module */
347 for (dll = BuiltinDLLs; dll->descr; dll++)
348 if (dll->used
349 && ((hModule = GetModuleHandle32A(dll->descr->name)) != 0))
351 IMAGE_SECTION_HEADER *sec = PE_SECTIONS(hModule);
352 DEBUG_ENTRY_POINT *debug =
353 (DEBUG_ENTRY_POINT *)((DWORD)hModule + sec[1].VirtualAddress);
354 DEBUG_ENTRY_POINT *func = (DEBUG_ENTRY_POINT *)relay;
356 if (debug <= func && func < debug + dll->descr->nb_funcs)
358 ordinal = func - debug;
359 break;
363 if (!dll->descr)
364 return (ENTRYPOINT32)NULL;
366 /* Now find the function */
368 for (i = 0; i < dll->descr->nb_names; i++)
369 if (dll->descr->ordinals[i] == ordinal) break;
370 assert( i < dll->descr->nb_names );
372 sprintf( buffer, "%s.%d: %s", dll->descr->name, ordinal + dll->descr->base,
373 dll->descr->names[i] );
374 *typemask = dll->descr->argtypes[ordinal];
375 return dll->descr->functions[ordinal];
379 /***********************************************************************
380 * BUILTIN32_Unimplemented
382 * This function is called for unimplemented 32-bit entry points (declared
383 * as 'stub' in the spec file).
385 void BUILTIN32_Unimplemented( const BUILTIN32_DESCRIPTOR *descr, int ordinal )
387 const char *func_name = "???";
388 int i;
390 __RESTORE_ES; /* Just in case */
392 for (i = 0; i < descr->nb_names; i++)
393 if (descr->ordinals[i] + descr->base == ordinal) break;
394 if (i < descr->nb_names) func_name = descr->names[i];
396 MSG( "No handler for Win32 routine %s.%d: %s",
397 descr->name, ordinal, func_name );
398 #ifdef __GNUC__
399 MSG( " (called from %p)", __builtin_return_address(1) );
400 #endif
401 MSG( "\n" );
402 ExitProcess(1);
406 /***********************************************************************
407 * BUILTIN32_EnableDLL
409 * Enable or disable a built-in DLL.
411 int BUILTIN32_EnableDLL( const char *name, int len, int enable )
413 int i;
414 BUILTIN32_DLL *dll;
416 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
418 if (!lstrncmpi32A( name, dll->descr->name, len ))
420 dll->used = enable;
421 return TRUE;
424 return FALSE;
428 /***********************************************************************
429 * BUILTIN32_PrintDLLs
431 * Print the list of built-in DLLs that can be disabled.
433 void BUILTIN32_PrintDLLs(void)
435 int i;
436 BUILTIN32_DLL *dll;
438 MSG("Available Win32 DLLs:\n");
439 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
440 MSG("%-9s%c", dll->descr->name,
441 ((++i) % 8) ? ' ' : '\n' );
442 MSG("\n");