2 * Win32 builtin functions
4 * Copyright 1997 Alexandre Julliard
11 #include "builtin32.h"
20 DECLARE_DEBUG_CHANNEL(relay)
21 DECLARE_DEBUG_CHANNEL(win32)
22 DECLARE_DEBUG_CHANNEL(module)
26 BYTE call; /* 0xe8 call callfrom32 (relative) */
27 DWORD callfrom32 WINE_PACKED; /* RELAY_CallFrom32 relative addr */
28 BYTE ret; /* 0xc2 ret $n or 0xc3 ret */
29 WORD args; /* nb of args to remove from the stack */
35 const DWORD nresources;
36 const DWORD restabsize;
37 const IMAGE_RESOURCE_DATA_ENTRY *entries;
42 const BUILTIN32_DESCRIPTOR *descr; /* DLL descriptor */
44 const BUILTIN32_RESOURCE *rsc;
47 #define BI32_INSTANTIATED 0x01
49 extern const BUILTIN32_DESCRIPTOR ADVAPI32_Descriptor;
50 extern const BUILTIN32_DESCRIPTOR AVIFIL32_Descriptor;
51 extern const BUILTIN32_DESCRIPTOR COMCTL32_Descriptor;
52 extern const BUILTIN32_DESCRIPTOR COMDLG32_Descriptor;
53 extern const BUILTIN32_DESCRIPTOR CRTDLL_Descriptor;
54 extern const BUILTIN32_DESCRIPTOR DCIMAN32_Descriptor;
55 extern const BUILTIN32_DESCRIPTOR DDRAW_Descriptor;
56 extern const BUILTIN32_DESCRIPTOR DINPUT_Descriptor;
57 extern const BUILTIN32_DESCRIPTOR DPLAY_Descriptor;
58 extern const BUILTIN32_DESCRIPTOR DPLAYX_Descriptor;
59 extern const BUILTIN32_DESCRIPTOR DSOUND_Descriptor;
60 extern const BUILTIN32_DESCRIPTOR GDI32_Descriptor;
61 extern const BUILTIN32_DESCRIPTOR IMAGEHLP_Descriptor;
62 extern const BUILTIN32_DESCRIPTOR IMM32_Descriptor;
63 extern const BUILTIN32_DESCRIPTOR KERNEL32_Descriptor;
64 extern const BUILTIN32_DESCRIPTOR LZ32_Descriptor;
65 extern const BUILTIN32_DESCRIPTOR MPR_Descriptor;
66 extern const BUILTIN32_DESCRIPTOR MSACM32_Descriptor;
67 extern const BUILTIN32_DESCRIPTOR MSNET32_Descriptor;
68 extern const BUILTIN32_DESCRIPTOR MSVFW32_Descriptor;
69 extern const BUILTIN32_DESCRIPTOR NTDLL_Descriptor;
70 extern const BUILTIN32_DESCRIPTOR OLE32_Descriptor;
71 extern const BUILTIN32_DESCRIPTOR OLEAUT32_Descriptor;
72 extern const BUILTIN32_DESCRIPTOR OLECLI32_Descriptor;
73 extern const BUILTIN32_DESCRIPTOR OLEDLG_Descriptor;
74 extern const BUILTIN32_DESCRIPTOR OLESVR32_Descriptor;
75 extern const BUILTIN32_DESCRIPTOR PSAPI_Descriptor;
76 extern const BUILTIN32_DESCRIPTOR RASAPI32_Descriptor;
77 extern const BUILTIN32_DESCRIPTOR SHELL32_Descriptor;
78 extern const BUILTIN32_DESCRIPTOR TAPI32_Descriptor;
79 extern const BUILTIN32_DESCRIPTOR USER32_Descriptor;
80 extern const BUILTIN32_DESCRIPTOR VERSION_Descriptor;
81 extern const BUILTIN32_DESCRIPTOR W32SKRNL_Descriptor;
82 extern const BUILTIN32_DESCRIPTOR WINMM_Descriptor;
83 extern const BUILTIN32_DESCRIPTOR WINSPOOL_Descriptor;
84 extern const BUILTIN32_DESCRIPTOR WNASPI32_Descriptor;
85 extern const BUILTIN32_DESCRIPTOR WOW32_Descriptor;
86 extern const BUILTIN32_DESCRIPTOR WSOCK32_Descriptor;
88 extern const BUILTIN32_RESOURCE comctl32_ResourceDescriptor;
89 extern const BUILTIN32_RESOURCE comdlg32_ResourceDescriptor;
90 extern const BUILTIN32_RESOURCE shell32_ResourceDescriptor;
91 extern const BUILTIN32_RESOURCE user32_ResourceDescriptor;
93 static BUILTIN32_DLL BuiltinDLLs[] =
95 { &ADVAPI32_Descriptor, 0, NULL },
96 { &AVIFIL32_Descriptor, 0, NULL },
97 { &COMCTL32_Descriptor, 0, &comctl32_ResourceDescriptor },
98 { &COMDLG32_Descriptor, 0, &comdlg32_ResourceDescriptor },
99 { &CRTDLL_Descriptor, 0, NULL },
100 { &DCIMAN32_Descriptor, 0, NULL },
101 { &DDRAW_Descriptor, 0, NULL },
102 { &DINPUT_Descriptor, 0, NULL },
103 { &DPLAY_Descriptor, 0, NULL },
104 { &DPLAYX_Descriptor, 0, NULL },
105 { &DSOUND_Descriptor, 0, NULL },
106 { &GDI32_Descriptor, 0, NULL },
107 { &IMAGEHLP_Descriptor, 0, NULL },
108 { &IMM32_Descriptor, 0, NULL },
109 { &KERNEL32_Descriptor, 0, NULL },
110 { &LZ32_Descriptor, 0, NULL },
111 { &MPR_Descriptor, 0, NULL },
112 { &MSACM32_Descriptor, 0, NULL },
113 { &MSNET32_Descriptor, 0, NULL },
114 { &MSVFW32_Descriptor, 0, NULL },
115 { &NTDLL_Descriptor, 0, NULL },
116 { &OLE32_Descriptor, 0, NULL },
117 { &OLEAUT32_Descriptor, 0, NULL },
118 { &OLECLI32_Descriptor, 0, NULL },
119 { &OLEDLG_Descriptor, 0, NULL },
120 { &OLESVR32_Descriptor, 0, NULL },
121 { &PSAPI_Descriptor, 0, NULL },
122 { &RASAPI32_Descriptor, 0, NULL },
123 { &SHELL32_Descriptor, 0, &shell32_ResourceDescriptor },
124 { &TAPI32_Descriptor, 0, NULL },
125 { &USER32_Descriptor, 0, &user32_ResourceDescriptor },
126 { &VERSION_Descriptor, 0, NULL },
127 { &W32SKRNL_Descriptor, 0, NULL },
128 { &WINMM_Descriptor, 0, NULL },
129 { &WINSPOOL_Descriptor, 0, NULL },
130 { &WNASPI32_Descriptor, 0, NULL },
131 { &WOW32_Descriptor, 0, NULL },
132 { &WSOCK32_Descriptor, 0, NULL },
137 extern void RELAY_CallFrom32();
139 /***********************************************************************
140 * BUILTIN32_DoLoadImage
142 * Load a built-in Win32 module. Helper function for BUILTIN32_LoadImage.
144 static HMODULE BUILTIN32_DoLoadImage( BUILTIN32_DLL *dll )
147 IMAGE_DATA_DIRECTORY *dir;
148 IMAGE_DOS_HEADER *dos;
149 IMAGE_NT_HEADERS *nt;
150 IMAGE_SECTION_HEADER *sec;
151 IMAGE_EXPORT_DIRECTORY *exp;
154 DEBUG_ENTRY_POINT *debug;
158 /* Allocate the module */
160 size = (sizeof(IMAGE_DOS_HEADER)
161 + sizeof(IMAGE_NT_HEADERS)
162 + 2 * sizeof(IMAGE_SECTION_HEADER)
163 + sizeof(IMAGE_EXPORT_DIRECTORY)
164 + dll->descr->nb_funcs * sizeof(LPVOID)
165 + dll->descr->nb_names * sizeof(LPSTR));
167 if (WARN_ON(relay) || TRACE_ON(relay))
168 size += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
170 addr = VirtualAlloc( NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
172 dos = (IMAGE_DOS_HEADER *)addr;
173 nt = (IMAGE_NT_HEADERS *)(dos + 1);
174 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
175 exp = (IMAGE_EXPORT_DIRECTORY *)(sec + 2);
176 funcs = (LPVOID *)(exp + 1);
177 names = (LPSTR *)(funcs + dll->descr->nb_funcs);
178 debug = (DEBUG_ENTRY_POINT *)(names + dll->descr->nb_names);
180 /* Build the DOS and NT headers */
182 dos->e_magic = IMAGE_DOS_SIGNATURE;
183 dos->e_lfanew = sizeof(*dos);
185 nt->Signature = IMAGE_NT_SIGNATURE;
186 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
187 nt->FileHeader.NumberOfSections = 2; /* exports + code */
188 nt->FileHeader.SizeOfOptionalHeader = sizeof(nt->OptionalHeader);
189 nt->FileHeader.Characteristics = IMAGE_FILE_DLL;
191 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
192 nt->OptionalHeader.SizeOfCode = 0x1000;
193 nt->OptionalHeader.SizeOfInitializedData = 0;
194 nt->OptionalHeader.SizeOfUninitializedData = 0;
195 nt->OptionalHeader.ImageBase = (DWORD)addr;
196 nt->OptionalHeader.SectionAlignment = 0x1000;
197 nt->OptionalHeader.FileAlignment = 0x1000;
198 nt->OptionalHeader.MajorOperatingSystemVersion = 1;
199 nt->OptionalHeader.MinorOperatingSystemVersion = 0;
200 nt->OptionalHeader.MajorSubsystemVersion = 4;
201 nt->OptionalHeader.MinorSubsystemVersion = 0;
202 nt->OptionalHeader.SizeOfImage = size;
203 nt->OptionalHeader.SizeOfHeaders = (BYTE *)exp - addr;
204 nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
205 if (dll->descr->dllentrypoint)
206 nt->OptionalHeader.AddressOfEntryPoint = (DWORD)dll->descr->dllentrypoint - (DWORD)addr;
208 /* Build the export directory */
210 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
211 dir->VirtualAddress = (BYTE *)exp - addr;
212 dir->Size = sizeof(*exp)
213 + dll->descr->nb_funcs * sizeof(LPVOID)
214 + dll->descr->nb_names * sizeof(LPSTR);
216 /* Build the exports section */
218 strcpy( sec->Name, ".edata" );
219 sec->Misc.VirtualSize = dir->Size;
220 sec->VirtualAddress = (BYTE *)exp - addr;
221 sec->SizeOfRawData = dir->Size;
222 sec->PointerToRawData = (BYTE *)exp - addr;
223 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
224 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ |
225 IMAGE_SCN_MEM_WRITE);
227 /* Build the resource directory */
232 IMAGE_RESOURCE_DATA_ENTRY *rdep;
234 rtab = HeapAlloc(GetProcessHeap(), 0, dll->rsc->restabsize);
237 ERR(module, "Failed to get memory for resource directory\n");
238 VirtualFree(addr, size, MEM_RELEASE);
243 * The resource directory has to be copied because it contains
244 * RVAs. These would be invalid if the dll is instantiated twice.
246 memcpy(rtab, dll->rsc->restab, dll->rsc->restabsize);
248 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
249 dir->VirtualAddress = (DWORD)rtab - (DWORD)addr;
250 dir->Size = dll->rsc->restabsize;
251 rdep = (IMAGE_RESOURCE_DATA_ENTRY *)((DWORD)rtab + (DWORD)dll->rsc->entries - (DWORD)dll->rsc->restab);
252 for(i = 0; i < dll->rsc->nresources; i++)
254 rdep[i].OffsetToData += (DWORD)dll->rsc->restab - (DWORD)addr;
258 /* Build the code section */
261 strcpy( sec->Name, ".code" );
262 sec->SizeOfRawData = 0;
264 if (WARN_ON(relay) || TRACE_ON(relay))
265 sec->SizeOfRawData += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
267 sec->Misc.VirtualSize = sec->SizeOfRawData;
268 sec->VirtualAddress = (BYTE *)debug - addr;
269 sec->PointerToRawData = (BYTE *)debug - addr;
270 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
271 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
273 /* Build the exports section data */
275 exp->Name = ((BYTE *)dll->descr->name) - addr; /*??*/
276 exp->Base = dll->descr->base;
277 exp->NumberOfFunctions = dll->descr->nb_funcs;
278 exp->NumberOfNames = dll->descr->nb_names;
279 exp->AddressOfFunctions = (LPDWORD *)((BYTE *)funcs - addr);
280 exp->AddressOfNames = (LPDWORD *)((BYTE *)names - addr);
281 exp->AddressOfNameOrdinals = (LPWORD *)((BYTE *)dll->descr->ordinals - addr);
283 /* Build the funcs table */
285 for (i = 0; i < dll->descr->nb_funcs; i++, funcs++, debug++)
287 BYTE args = dll->descr->args[i];
290 if (!dll->descr->functions[i]) continue;
291 *funcs = (LPVOID)((BYTE *)dll->descr->functions[i] - addr);
293 if (!(WARN_ON(relay) || TRACE_ON(relay))) continue;
294 for (j=0;j<dll->descr->nb_names;j++)
295 if (dll->descr->ordinals[j] == i)
297 if (j<dll->descr->nb_names) {
298 if (dll->descr->names[j]) {
300 sprintf(buffer,"%s.%d: %s",dll->descr->name,i,dll->descr->names[j]);
301 if (!RELAY_ShowDebugmsgRelay(buffer))
307 case 0xfe: /* register func */
309 debug->callfrom32 = (DWORD)dll->descr->functions[i] -
311 debug->ret = 0x90; /* nop */
313 *funcs = (LPVOID)((BYTE *)debug - addr);
315 case 0xff: /* stub or extern */
317 default: /* normal function (stdcall or cdecl) */
318 if (TRACE_ON(relay)) {
319 debug->call = 0xe8; /* lcall relative */
320 debug->callfrom32 = (DWORD)RELAY_CallFrom32 -
323 debug->call = 0xe9; /* ljmp relative */
324 debug->callfrom32 = (DWORD)dll->descr->functions[i] -
327 debug->ret = (args & 0x80) ? 0xc3 : 0xc2; /*ret/ret $n*/
328 debug->args = (args & 0x7f) * sizeof(int);
329 *funcs = (LPVOID)((BYTE *)debug - addr);
332 #endif /* __i386__ */
335 /* Build the names table */
337 for (i = 0; i < exp->NumberOfNames; i++, names++)
338 if (dll->descr->names[i])
339 *names = (LPSTR)((BYTE *)dll->descr->names[i] - addr);
341 return (HMODULE)addr;
344 /***********************************************************************
345 * BUILTIN32_LoadImage
347 * Load a built-in module.
349 HMODULE BUILTIN32_LoadImage( LPCSTR name, OFSTRUCT *ofs)
351 BUILTIN32_DLL *table;
352 char dllname[16], *p;
355 /* Fix the name in case we have a full path and extension */
357 if ((p = strrchr( name, '\\' ))) name = p + 1;
358 lstrcpynA( dllname, name, sizeof(dllname) );
359 if ((p = strrchr( dllname, '.' ))) *p = '\0';
361 for (table = BuiltinDLLs; table->descr; table++)
362 if (!lstrcmpiA( table->descr->name, dllname )) break;
363 if (!table->descr) return 0;
365 if(table->flags && BI32_INSTANTIATED)
367 ERR(module, "Attemp to instantiate built-in dll '%s' twice in the same address-space. Expect trouble!\n",
371 sprintf( ofs->szPathName, "%s.DLL", table->descr->name );
372 hmod = BUILTIN32_DoLoadImage( table );
374 table->flags |= BI32_INSTANTIATED;
380 /***********************************************************************
381 * BUILTIN32_LoadLibraryExA
383 * Partly copied from the original PE_ version.
385 * Note: This implementation is not very nice and should be one with
386 * the BUILTIN32_LoadImage function. But, we don't care too much
387 * because this code will obsolete itself shortly when we get the
388 * modularization of wine implemented (BS 05-Mar-1999).
390 WINE_MODREF *BUILTIN32_LoadLibraryExA(LPCSTR path, DWORD flags, DWORD *err)
392 LPCSTR modName = NULL;
398 char dllname[256], *p;
400 /* Append .DLL to name if no extension present */
401 strcpy( dllname, path );
402 if (!(p = strrchr( dllname, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
403 strcat( dllname, ".DLL" );
405 hModule32 = BUILTIN32_LoadImage(path, &ofs);
408 *err = ERROR_FILE_NOT_FOUND;
412 /* Create 16-bit dummy module */
413 if ((hModule16 = MODULE_CreateDummyModule( &ofs, modName )) < 32)
415 *err = (DWORD)hModule16;
416 return NULL; /* FIXME: Should unload the builtin module */
419 pModule = (NE_MODULE *)GlobalLock16( hModule16 );
420 pModule->flags = NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA | NE_FFLAGS_WIN32 | NE_FFLAGS_BUILTIN;
421 pModule->module32 = hModule32;
423 /* Create 32-bit MODREF */
424 if ( !(wm = PE_CreateModule( hModule32, &ofs, flags, TRUE )) )
426 ERR(win32,"can't load %s\n",ofs.szPathName);
427 FreeLibrary16( hModule16 ); /* FIXME: Should unload the builtin module */
428 *err = ERROR_OUTOFMEMORY;
432 if (wm->binfmt.pe.pe_export)
433 SNOOP_RegisterDLL(wm->module,wm->modname,wm->binfmt.pe.pe_export->NumberOfFunctions);
440 /***********************************************************************
441 * BUILTIN32_UnloadLibrary
443 * Unload the built-in library and free the modref.
445 void BUILTIN32_UnloadLibrary(WINE_MODREF *wm)
447 /* FIXME: do something here */
451 /***********************************************************************
452 * BUILTIN32_GetEntryPoint
454 * Return the name of the DLL entry point corresponding
455 * to a relay entry point address. This is used only by relay debugging.
457 * This function _must_ return the real entry point to call
458 * after the debug info is printed.
460 ENTRYPOINT32 BUILTIN32_GetEntryPoint( char *buffer, void *relay,
461 unsigned int *typemask )
467 /* First find the module */
469 for (dll = BuiltinDLLs; dll->descr; dll++)
470 if ((dll->flags & BI32_INSTANTIATED)
471 && ((hModule = GetModuleHandleA(dll->descr->name)) != 0))
473 IMAGE_SECTION_HEADER *sec = PE_SECTIONS(hModule);
474 DEBUG_ENTRY_POINT *debug =
475 (DEBUG_ENTRY_POINT *)((DWORD)hModule + sec[1].VirtualAddress);
476 DEBUG_ENTRY_POINT *func = (DEBUG_ENTRY_POINT *)relay;
478 if (debug <= func && func < debug + dll->descr->nb_funcs)
480 ordinal = func - debug;
486 return (ENTRYPOINT32)NULL;
488 /* Now find the function */
490 for (i = 0; i < dll->descr->nb_names; i++)
491 if (dll->descr->ordinals[i] == ordinal) break;
492 assert( i < dll->descr->nb_names );
494 sprintf( buffer, "%s.%d: %s", dll->descr->name, ordinal + dll->descr->base,
495 dll->descr->names[i] );
496 *typemask = dll->descr->argtypes[ordinal];
497 return dll->descr->functions[ordinal];
500 /***********************************************************************
501 * BUILTIN32_SwitchRelayDebug
503 * FIXME: enhance to do it module relative.
505 void BUILTIN32_SwitchRelayDebug(BOOL onoff) {
511 if (!(TRACE_ON(relay) || WARN_ON(relay)))
513 for (dll = BuiltinDLLs; dll->descr; dll++) {
514 IMAGE_SECTION_HEADER *sec;
515 DEBUG_ENTRY_POINT *debug;
516 if (!(dll->flags & BI32_INSTANTIATED) || !(hModule = GetModuleHandleA(dll->descr->name)))
519 sec = PE_SECTIONS(hModule);
520 debug = (DEBUG_ENTRY_POINT *)((DWORD)hModule + sec[1].VirtualAddress);
521 for (i = 0; i < dll->descr->nb_funcs; i++,debug++) {
522 if (!dll->descr->functions[i]) continue;
523 if ((dll->descr->args[i]==0xff) || (dll->descr->args[i]==0xfe))
526 debug->call = 0xe8; /* lcall relative */
527 debug->callfrom32 = (DWORD)RELAY_CallFrom32 -
530 debug->call = 0xe9; /* ljmp relative */
531 debug->callfrom32 = (DWORD)dll->descr->functions[i] -
536 #endif /* __i386__ */
540 /***********************************************************************
541 * BUILTIN32_Unimplemented
543 * This function is called for unimplemented 32-bit entry points (declared
544 * as 'stub' in the spec file).
546 void BUILTIN32_Unimplemented( const BUILTIN32_DESCRIPTOR *descr, int ordinal )
548 const char *func_name = "???";
551 __RESTORE_ES; /* Just in case */
553 for (i = 0; i < descr->nb_names; i++)
554 if (descr->ordinals[i] + descr->base == ordinal) break;
555 if (i < descr->nb_names) func_name = descr->names[i];
557 MSG( "No handler for Win32 routine %s.%d: %s",
558 descr->name, ordinal, func_name );
560 MSG( " (called from %p)", __builtin_return_address(1) );