Implemented thread and (partial) module snapshots, based on the work
[wine.git] / relay32 / builtin32.c
blob6930f42a008dbe98dde74297c83ecc1ebb2b9ad7
1 /*
2 * Win32 builtin functions
4 * Copyright 1997 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include <ctype.h>
11 #include "windef.h"
12 #include "wingdi.h"
13 #include "winuser.h"
14 #include "builtin32.h"
15 #include "peexe.h"
16 #include "neexe.h"
17 #include "heap.h"
18 #include "main.h"
19 #include "snoop.h"
20 #include "winerror.h"
21 #include "server.h"
22 #include "debugtools.h"
23 #include "options.h" /* for argv0 */
25 DEFAULT_DEBUG_CHANNEL(module);
26 DECLARE_DEBUG_CHANNEL(relay);
28 typedef struct
30 BYTE call; /* 0xe8 call callfrom32 (relative) */
31 DWORD callfrom32 WINE_PACKED; /* RELAY_CallFrom32 relative addr */
32 BYTE ret; /* 0xc2 ret $n or 0xc3 ret */
33 WORD args; /* nb of args to remove from the stack */
34 } DEBUG_ENTRY_POINT;
36 typedef struct
38 const BYTE *restab;
39 const DWORD nresources;
40 const DWORD restabsize;
41 const IMAGE_RESOURCE_DATA_ENTRY *entries;
42 } BUILTIN32_RESOURCE;
44 #define MAX_DLLS 60
46 static const BUILTIN32_DESCRIPTOR *builtin_dlls[MAX_DLLS];
47 static HMODULE dll_modules[MAX_DLLS];
48 static int nb_dlls;
50 extern void RELAY_CallFrom32();
51 extern void RELAY_CallFrom32Regs();
53 /***********************************************************************
54 * BUILTIN32_WarnSecondInstance
56 * Emit a warning when we are creating a second instance for a DLL
57 * that is known to not support this.
59 static void BUILTIN32_WarnSecondInstance( const char *name )
61 static const char * const warning_list[] =
62 { "comctl32", "comdlg32", "crtdll", "imagehlp", "msacm32", "shell32", NULL };
64 const char * const *ptr = warning_list;
66 while (*ptr)
68 if (!strcasecmp( *ptr, name ))
70 ERR( "Attempt to instantiate built-in dll '%s' twice "
71 "in the same address space. Expect trouble!\n", name );
72 return;
74 ptr++;
78 /***********************************************************************
79 * BUILTIN32_DoLoadImage
81 * Load a built-in Win32 module. Helper function for BUILTIN32_LoadImage.
83 static HMODULE BUILTIN32_DoLoadImage( const BUILTIN32_DESCRIPTOR *descr )
86 IMAGE_DATA_DIRECTORY *dir;
87 IMAGE_DOS_HEADER *dos;
88 IMAGE_NT_HEADERS *nt;
89 IMAGE_SECTION_HEADER *sec;
90 IMAGE_EXPORT_DIRECTORY *exp;
91 IMAGE_IMPORT_DESCRIPTOR *imp;
92 const BUILTIN32_RESOURCE *rsrc = descr->rsrc;
93 LPVOID *funcs;
94 LPSTR *names;
95 LPSTR pfwd, rtab;
96 DEBUG_ENTRY_POINT *debug;
97 INT i, size, nb_sections;
98 BYTE *addr;
99 BYTE* xcnlnk;
100 DWORD xcnsize = 0;
102 /* Allocate the module */
104 nb_sections = 2; /* exports + code */
105 if (descr->nb_imports) nb_sections++;
107 if (!strcmp(descr->name, "KERNEL32")) {
108 nb_sections++;
109 xcnsize = sizeof(DWORD);
111 size = (sizeof(IMAGE_DOS_HEADER)
112 + sizeof(IMAGE_NT_HEADERS)
113 + nb_sections * sizeof(IMAGE_SECTION_HEADER)
114 + (descr->nb_imports+1) * sizeof(IMAGE_IMPORT_DESCRIPTOR)
115 + sizeof(IMAGE_EXPORT_DIRECTORY)
116 + descr->nb_funcs * sizeof(LPVOID)
117 + descr->nb_names * sizeof(LPSTR)
118 + descr->fwd_size
119 + xcnsize);
121 #ifdef __i386__
122 if (WARN_ON(relay) || TRACE_ON(relay))
123 size += descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
124 #endif
125 if (rsrc) size += rsrc->restabsize;
126 addr = VirtualAlloc( NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
127 if (!addr) return 0;
128 dos = (IMAGE_DOS_HEADER *)addr;
129 nt = (IMAGE_NT_HEADERS *)(dos + 1);
130 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
131 imp = (IMAGE_IMPORT_DESCRIPTOR *)(sec + nb_sections);
132 exp = (IMAGE_EXPORT_DIRECTORY *)(imp + descr->nb_imports + 1);
133 funcs = (LPVOID *)(exp + 1);
134 names = (LPSTR *)(funcs + descr->nb_funcs);
135 pfwd = (LPSTR)(names + descr->nb_names);
136 xcnlnk= pfwd + descr->fwd_size;
137 rtab = xcnlnk + xcnsize;
138 debug = (DEBUG_ENTRY_POINT *)(rtab + (rsrc ? rsrc->restabsize : 0));
140 /* Build the DOS and NT headers */
142 dos->e_magic = IMAGE_DOS_SIGNATURE;
143 dos->e_lfanew = sizeof(*dos);
145 nt->Signature = IMAGE_NT_SIGNATURE;
146 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
147 nt->FileHeader.NumberOfSections = nb_sections;
148 nt->FileHeader.SizeOfOptionalHeader = sizeof(nt->OptionalHeader);
149 nt->FileHeader.Characteristics = descr->characteristics;
151 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
152 nt->OptionalHeader.SizeOfCode = 0x1000;
153 nt->OptionalHeader.SizeOfInitializedData = 0;
154 nt->OptionalHeader.SizeOfUninitializedData = 0;
155 nt->OptionalHeader.ImageBase = (DWORD)addr;
156 nt->OptionalHeader.SectionAlignment = 0x1000;
157 nt->OptionalHeader.FileAlignment = 0x1000;
158 nt->OptionalHeader.MajorOperatingSystemVersion = 1;
159 nt->OptionalHeader.MinorOperatingSystemVersion = 0;
160 nt->OptionalHeader.MajorSubsystemVersion = 4;
161 nt->OptionalHeader.MinorSubsystemVersion = 0;
162 nt->OptionalHeader.SizeOfImage = size;
163 nt->OptionalHeader.SizeOfHeaders = (BYTE *)exp - addr;
164 nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
165 if (descr->dllentrypoint)
166 nt->OptionalHeader.AddressOfEntryPoint = (DWORD)descr->dllentrypoint - (DWORD)addr;
168 /* Build the code section */
170 strcpy( sec->Name, ".code" );
171 sec->SizeOfRawData = 0;
172 #ifdef __i386__
173 if (WARN_ON(relay) || TRACE_ON(relay))
174 sec->SizeOfRawData += descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
175 #endif
176 sec->Misc.VirtualSize = sec->SizeOfRawData;
177 sec->VirtualAddress = (BYTE *)debug - addr;
178 sec->PointerToRawData = (BYTE *)debug - addr;
179 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
180 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
181 sec++;
183 /* Build the import directory */
185 if (descr->nb_imports)
187 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
188 dir->VirtualAddress = (BYTE *)imp - addr;
189 dir->Size = sizeof(*imp) * (descr->nb_imports + 1);
191 /* Build the imports section */
192 strcpy( sec->Name, ".idata" );
193 sec->Misc.VirtualSize = dir->Size;
194 sec->VirtualAddress = (BYTE *)imp - addr;
195 sec->SizeOfRawData = dir->Size;
196 sec->PointerToRawData = (BYTE *)imp - addr;
197 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
198 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ |
199 IMAGE_SCN_MEM_WRITE);
200 sec++;
202 /* Build the imports */
203 for (i = 0; i < descr->nb_imports; i++)
205 imp[i].u.Characteristics = 0;
206 imp[i].ForwarderChain = -1;
207 imp[i].Name = (BYTE *)descr->imports[i] - addr;
208 /* hack: make first thunk point to some zero value */
209 imp[i].FirstThunk = (PIMAGE_THUNK_DATA)((BYTE *)&imp[i].u.Characteristics - addr);
213 /* Build the export directory */
215 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
216 dir->VirtualAddress = (BYTE *)exp - addr;
217 dir->Size = sizeof(*exp)
218 + descr->nb_funcs * sizeof(LPVOID)
219 + descr->nb_names * sizeof(LPSTR)
220 + descr->fwd_size;
222 /* Build the exports section */
224 strcpy( sec->Name, ".edata" );
225 sec->Misc.VirtualSize = dir->Size;
226 sec->VirtualAddress = (BYTE *)exp - addr;
227 sec->SizeOfRawData = dir->Size;
228 sec->PointerToRawData = (BYTE *)exp - addr;
229 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
230 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ |
231 IMAGE_SCN_MEM_WRITE);
233 /* Build Wine's .so link section. Those sections are used by the wine debugger to
234 * link a builtin PE header with the corresponding ELF module (from either a
235 * shared library, or the main executable - wine emulator or any winelib program
237 if (xcnsize)
239 sec++;
240 strcpy( sec->Name, ".xcnlnk" );
241 sec->Misc.VirtualSize = xcnsize;
242 sec->VirtualAddress = (BYTE *)xcnlnk - addr;
243 sec->SizeOfRawData = sec->Misc.VirtualSize;
244 sec->PointerToRawData = (BYTE *)xcnlnk - addr;
245 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ);
247 *(const char**)xcnlnk = argv0;
250 /* Build the resource directory */
252 if (rsrc)
254 IMAGE_RESOURCE_DATA_ENTRY *rdep;
257 * The resource directory has to be copied because it contains
258 * RVAs. These would be invalid if the dll is instantiated twice.
260 memcpy(rtab, rsrc->restab, rsrc->restabsize);
262 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
263 dir->VirtualAddress = (BYTE *)rtab - addr;
264 dir->Size = rsrc->restabsize;
265 rdep = (IMAGE_RESOURCE_DATA_ENTRY *)((DWORD)rtab + (DWORD)rsrc->entries - (DWORD)rsrc->restab);
266 for(i = 0; i < rsrc->nresources; i++)
268 rdep[i].OffsetToData += (DWORD)rsrc->restab - (DWORD)addr;
272 /* Build the exports section data */
274 exp->Name = ((BYTE *)descr->name) - addr; /*??*/
275 exp->Base = descr->base;
276 exp->NumberOfFunctions = descr->nb_funcs;
277 exp->NumberOfNames = descr->nb_names;
278 exp->AddressOfFunctions = (LPDWORD *)((BYTE *)funcs - addr);
279 exp->AddressOfNames = (LPDWORD *)((BYTE *)names - addr);
280 exp->AddressOfNameOrdinals = (LPWORD *)((BYTE *)descr->ordinals - addr);
282 /* Build the funcs table */
284 for (i = 0; i < descr->nb_funcs; i++, funcs++, debug++)
286 BYTE args = descr->args[i];
287 int j;
289 if (!descr->functions[i]) continue;
291 if (args == 0xfd) /* forward func */
293 strcpy( pfwd, (LPSTR)descr->functions[i] );
294 *funcs = (LPVOID)((BYTE *)pfwd - addr);
295 pfwd += strlen(pfwd) + 1;
297 else *funcs = (LPVOID)((BYTE *)descr->functions[i] - addr);
299 #ifdef __i386__
300 if (!(WARN_ON(relay) || TRACE_ON(relay))) continue;
301 for (j=0;j<descr->nb_names;j++)
302 if (descr->ordinals[j] == i)
303 break;
304 if (j<descr->nb_names) {
305 if (descr->names[j]) {
306 char buffer[200];
307 sprintf(buffer,"%s.%d: %s",descr->name,i,descr->names[j]);
308 if (!RELAY_ShowDebugmsgRelay(buffer))
309 continue;
312 switch(args)
314 case 0xfd: /* forward */
315 case 0xff: /* stub or extern */
316 break;
317 default: /* normal function (stdcall or cdecl or register) */
318 if (TRACE_ON(relay)) {
319 debug->call = 0xe8; /* lcall relative */
320 if (args & 0x40) /* register func */
321 debug->callfrom32 = (DWORD)RELAY_CallFrom32Regs -
322 (DWORD)&debug->ret;
323 else
324 debug->callfrom32 = (DWORD)RELAY_CallFrom32 -
325 (DWORD)&debug->ret;
326 } else {
327 debug->call = 0xe9; /* ljmp relative */
328 debug->callfrom32 = (DWORD)descr->functions[i] -
329 (DWORD)&debug->ret;
331 debug->ret = (args & 0x80) ? 0xc3 : 0xc2; /*ret/ret $n*/
332 debug->args = (args & 0x3f) * sizeof(int);
333 *funcs = (LPVOID)((BYTE *)debug - addr);
334 break;
336 #endif /* __i386__ */
339 /* Build the names table */
341 for (i = 0; i < exp->NumberOfNames; i++, names++)
342 if (descr->names[i])
343 *names = (LPSTR)((BYTE *)descr->names[i] - addr);
345 return (HMODULE)addr;
348 /***********************************************************************
349 * BUILTIN32_LoadLibraryExA
351 * Partly copied from the original PE_ version.
354 WINE_MODREF *BUILTIN32_LoadLibraryExA(LPCSTR path, DWORD flags)
356 struct load_dll_request *req = get_req_buffer();
357 HMODULE16 hModule16;
358 NE_MODULE *pModule;
359 WINE_MODREF *wm;
360 char dllname[MAX_PATH], *p;
361 int i;
363 /* Fix the name in case we have a full path and extension */
364 if ((p = strrchr( path, '\\' ))) path = p + 1;
365 lstrcpynA( dllname, path, sizeof(dllname) );
367 p = strrchr( dllname, '.' );
368 if (!p) strcat( dllname, ".dll" );
370 /* Search built-in descriptor */
371 for (i = 0; i < nb_dlls; i++)
372 if (!lstrcmpiA( builtin_dlls[i]->filename, dllname )) break;
374 if (i == nb_dlls)
376 SetLastError( ERROR_FILE_NOT_FOUND );
377 return NULL;
380 /* Load built-in module */
381 if (!dll_modules[i])
383 if (!(dll_modules[i] = BUILTIN32_DoLoadImage( builtin_dlls[i] ))) return NULL;
385 else BUILTIN32_WarnSecondInstance( builtin_dlls[i]->name );
387 /* Create 16-bit dummy module */
388 if ((hModule16 = MODULE_CreateDummyModule( dllname, dll_modules[i] )) < 32)
390 SetLastError( (DWORD)hModule16 );
391 return NULL; /* FIXME: Should unload the builtin module */
393 pModule = (NE_MODULE *)GlobalLock16( hModule16 );
394 pModule->flags = NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA | NE_FFLAGS_WIN32 | NE_FFLAGS_BUILTIN;
396 /* Create 32-bit MODREF */
397 if ( !(wm = PE_CreateModule( pModule->module32, dllname, flags, TRUE )) )
399 ERR( "can't load %s\n", path );
400 FreeLibrary16( hModule16 ); /* FIXME: Should unload the builtin module */
401 SetLastError( ERROR_OUTOFMEMORY );
402 return NULL;
405 if (wm->binfmt.pe.pe_export)
406 SNOOP_RegisterDLL(wm->module,wm->modname,wm->binfmt.pe.pe_export->NumberOfFunctions);
408 req->handle = -1;
409 req->base = (void *)pModule->module32;
410 req->dbg_offset = 0;
411 req->dbg_size = 0;
412 req->name = &wm->modname;
413 server_call_noerr( REQ_LOAD_DLL );
414 return wm;
417 /***********************************************************************
418 * BUILTIN32_LoadExeModule
420 HMODULE BUILTIN32_LoadExeModule( LPCSTR *filename )
422 HMODULE16 hModule16;
423 NE_MODULE *pModule;
424 int i, exe = -1;
426 /* Search built-in EXE descriptor */
427 for ( i = 0; i < nb_dlls; i++ )
428 if ( !(builtin_dlls[i]->characteristics & IMAGE_FILE_DLL) )
430 if ( exe != -1 )
432 MESSAGE( "More than one built-in EXE module loaded!\n" );
433 break;
436 exe = i;
439 if ( exe == -1 )
441 MESSAGE( "No built-in EXE module loaded! Did you create a .spec file?\n" );
442 return 0;
445 /* Load built-in module */
446 if ( !dll_modules[exe] )
447 if ( !(dll_modules[exe] = BUILTIN32_DoLoadImage( builtin_dlls[exe] )) )
448 return 0;
450 *filename = builtin_dlls[exe]->filename;
451 return dll_modules[exe];
455 /***********************************************************************
456 * BUILTIN32_UnloadLibrary
458 * Unload the built-in library and free the modref.
460 void BUILTIN32_UnloadLibrary(WINE_MODREF *wm)
462 /* FIXME: do something here */
466 /***********************************************************************
467 * BUILTIN32_GetEntryPoint
469 * Return the name of the DLL entry point corresponding
470 * to a relay entry point address. This is used only by relay debugging.
472 * This function _must_ return the real entry point to call
473 * after the debug info is printed.
475 ENTRYPOINT32 BUILTIN32_GetEntryPoint( char *buffer, void *relay,
476 unsigned int *typemask )
478 const BUILTIN32_DESCRIPTOR *descr = NULL;
479 int ordinal = 0, i;
481 /* First find the module */
483 for (i = 0; i < nb_dlls; i++)
484 if (dll_modules[i])
486 IMAGE_SECTION_HEADER *sec = PE_SECTIONS(dll_modules[i]);
487 DEBUG_ENTRY_POINT *debug =
488 (DEBUG_ENTRY_POINT *)((DWORD)dll_modules[i] + sec[0].VirtualAddress);
489 DEBUG_ENTRY_POINT *func = (DEBUG_ENTRY_POINT *)relay;
490 descr = builtin_dlls[i];
491 if (debug <= func && func < debug + descr->nb_funcs)
493 ordinal = func - debug;
494 break;
498 if (!descr) return NULL;
500 /* Now find the function */
502 for (i = 0; i < descr->nb_names; i++)
503 if (descr->ordinals[i] == ordinal) break;
505 sprintf( buffer, "%s.%d: %s", descr->name, ordinal + descr->base,
506 (i < descr->nb_names) ? descr->names[i] : "@" );
507 *typemask = descr->argtypes[ordinal];
508 return descr->functions[ordinal];
511 /***********************************************************************
512 * BUILTIN32_SwitchRelayDebug
514 * FIXME: enhance to do it module relative.
516 void BUILTIN32_SwitchRelayDebug(BOOL onoff)
518 const BUILTIN32_DESCRIPTOR *descr;
519 IMAGE_SECTION_HEADER *sec;
520 DEBUG_ENTRY_POINT *debug;
521 int i, j;
523 #ifdef __i386__
524 if (!(TRACE_ON(relay) || WARN_ON(relay)))
525 return;
526 for (j = 0; j < nb_dlls; j++)
528 if (!dll_modules[j]) continue;
529 sec = PE_SECTIONS(dll_modules[j]);
530 debug = (DEBUG_ENTRY_POINT *)((DWORD)dll_modules[j] + sec[1].VirtualAddress);
531 descr = builtin_dlls[j];
532 for (i = 0; i < descr->nb_funcs; i++,debug++) {
533 if (!descr->functions[i]) continue;
534 if ((descr->args[i]==0xff) || (descr->args[i]==0xfe))
535 continue;
536 if (onoff) {
537 debug->call = 0xe8; /* lcall relative */
538 debug->callfrom32 = (DWORD)RELAY_CallFrom32 -
539 (DWORD)&debug->ret;
540 } else {
541 debug->call = 0xe9; /* ljmp relative */
542 debug->callfrom32 = (DWORD)descr->functions[i] -
543 (DWORD)&debug->ret;
547 #endif /* __i386__ */
548 return;
551 /***********************************************************************
552 * BUILTIN32_RegisterDLL
554 * Register a built-in DLL descriptor.
556 void BUILTIN32_RegisterDLL( const BUILTIN32_DESCRIPTOR *descr )
558 assert( nb_dlls < MAX_DLLS );
559 builtin_dlls[nb_dlls++] = descr;
562 /***********************************************************************
563 * BUILTIN32_Unimplemented
565 * This function is called for unimplemented 32-bit entry points (declared
566 * as 'stub' in the spec file).
568 void BUILTIN32_Unimplemented( const BUILTIN32_DESCRIPTOR *descr, int ordinal )
570 const char *func_name = "???";
571 int i;
573 __RESTORE_ES; /* Just in case */
575 for (i = 0; i < descr->nb_names; i++)
576 if (descr->ordinals[i] + descr->base == ordinal) break;
577 if (i < descr->nb_names) func_name = descr->names[i];
579 MESSAGE( "No handler for Win32 routine %s.%d: %s",
580 descr->name, ordinal, func_name );
581 #ifdef __GNUC__
582 MESSAGE( " (called from %p)", __builtin_return_address(1) );
583 #endif
584 MESSAGE( "\n" );
585 ExitProcess(1);