2 * Win32 builtin functions
4 * Copyright 1997 Alexandre Julliard
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 */
26 const BUILTIN32_DESCRIPTOR *descr; /* DLL descriptor */
27 BOOL32 used; /* Used by default */
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 IMAGEHLP_Descriptor;
43 extern const BUILTIN32_DESCRIPTOR IMM32_Descriptor;
44 extern const BUILTIN32_DESCRIPTOR KERNEL32_Descriptor;
45 extern const BUILTIN32_DESCRIPTOR LZ32_Descriptor;
46 extern const BUILTIN32_DESCRIPTOR MPR_Descriptor;
47 extern const BUILTIN32_DESCRIPTOR MSACM32_Descriptor;
48 extern const BUILTIN32_DESCRIPTOR MSNET32_Descriptor;
49 extern const BUILTIN32_DESCRIPTOR MSVFW32_Descriptor;
50 extern const BUILTIN32_DESCRIPTOR NTDLL_Descriptor;
51 extern const BUILTIN32_DESCRIPTOR OLE32_Descriptor;
52 extern const BUILTIN32_DESCRIPTOR OLEAUT32_Descriptor;
53 extern const BUILTIN32_DESCRIPTOR OLECLI32_Descriptor;
54 extern const BUILTIN32_DESCRIPTOR OLEDLG_Descriptor;
55 extern const BUILTIN32_DESCRIPTOR OLESVR32_Descriptor;
56 extern const BUILTIN32_DESCRIPTOR PSAPI_Descriptor;
57 extern const BUILTIN32_DESCRIPTOR RASAPI32_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 WNASPI32_Descriptor;
66 extern const BUILTIN32_DESCRIPTOR WOW32_Descriptor;
67 extern const BUILTIN32_DESCRIPTOR WSOCK32_Descriptor;
69 static BUILTIN32_DLL BuiltinDLLs[] =
71 { &ADVAPI32_Descriptor, TRUE },
72 { &COMCTL32_Descriptor, FALSE },
73 { &COMDLG32_Descriptor, TRUE },
74 { &CRTDLL_Descriptor, TRUE },
75 { &DCIMAN32_Descriptor, FALSE },
76 { &DDRAW_Descriptor, TRUE },
77 { &DINPUT_Descriptor, TRUE },
78 { &DPLAY_Descriptor, TRUE },
79 { &DPLAYX_Descriptor, TRUE },
80 { &DSOUND_Descriptor, TRUE },
81 { &GDI32_Descriptor, TRUE },
82 { &IMAGEHLP_Descriptor, FALSE },
83 { &IMM32_Descriptor, FALSE },
84 { &KERNEL32_Descriptor, TRUE },
85 { &LZ32_Descriptor, TRUE },
86 { &MPR_Descriptor, TRUE },
87 { &MSACM32_Descriptor, FALSE },
88 { &MSNET32_Descriptor, FALSE },
89 { &MSVFW32_Descriptor, FALSE },
90 { &NTDLL_Descriptor, TRUE },
91 { &OLE32_Descriptor, FALSE },
92 { &OLEAUT32_Descriptor, FALSE },
93 { &OLECLI32_Descriptor, FALSE },
94 { &OLEDLG_Descriptor, FALSE },
95 { &OLESVR32_Descriptor, FALSE },
96 { &PSAPI_Descriptor, FALSE },
97 { &RASAPI32_Descriptor, FALSE },
98 { &SHELL32_Descriptor, TRUE },
99 { &TAPI32_Descriptor, FALSE },
100 { &USER32_Descriptor, TRUE },
101 { &VERSION_Descriptor, TRUE },
102 { &W32SKRNL_Descriptor, TRUE },
103 { &WINMM_Descriptor, TRUE },
104 { &WINSPOOL_Descriptor, TRUE },
105 { &WNASPI32_Descriptor, TRUE },
106 { &WOW32_Descriptor, TRUE },
107 { &WSOCK32_Descriptor, TRUE },
113 /***********************************************************************
114 * BUILTIN32_DoLoadModule
116 * Load a built-in Win32 module. Helper function for BUILTIN32_LoadModule.
118 static HMODULE32 BUILTIN32_DoLoadModule( BUILTIN32_DLL *dll, PDB32 *pdb )
120 extern void RELAY_CallFrom32();
125 IMAGE_DATA_DIRECTORY *dir;
126 IMAGE_DOS_HEADER *dos;
127 IMAGE_NT_HEADERS *nt;
128 IMAGE_SECTION_HEADER *sec;
129 IMAGE_EXPORT_DIRECTORY *exp;
132 DEBUG_ENTRY_POINT *debug;
138 /* Allocate the module */
140 size = (sizeof(IMAGE_DOS_HEADER)
141 + sizeof(IMAGE_NT_HEADERS)
142 + 2 * sizeof(IMAGE_SECTION_HEADER)
143 + sizeof(IMAGE_EXPORT_DIRECTORY)
144 + dll->descr->nb_funcs * sizeof(LPVOID)
145 + dll->descr->nb_names * sizeof(LPSTR));
148 size += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
150 addr = VirtualAlloc( NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
152 dos = (IMAGE_DOS_HEADER *)addr;
153 nt = (IMAGE_NT_HEADERS *)(dos + 1);
154 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
155 exp = (IMAGE_EXPORT_DIRECTORY *)(sec + 2);
156 funcs = (LPVOID *)(exp + 1);
157 names = (LPSTR *)(funcs + dll->descr->nb_funcs);
158 debug = (DEBUG_ENTRY_POINT *)(names + dll->descr->nb_names);
160 /* Build the DOS and NT headers */
162 dos->e_magic = IMAGE_DOS_SIGNATURE;
163 dos->e_lfanew = sizeof(*dos);
165 nt->Signature = IMAGE_NT_SIGNATURE;
166 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
167 nt->FileHeader.NumberOfSections = 2; /* exports + code */
168 nt->FileHeader.SizeOfOptionalHeader = sizeof(nt->OptionalHeader);
169 nt->FileHeader.Characteristics = IMAGE_FILE_DLL;
171 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
172 nt->OptionalHeader.SizeOfCode = 0x1000;
173 nt->OptionalHeader.SizeOfInitializedData = 0;
174 nt->OptionalHeader.SizeOfUninitializedData = 0;
175 nt->OptionalHeader.ImageBase = (DWORD)addr;
176 nt->OptionalHeader.SectionAlignment = 0x1000;
177 nt->OptionalHeader.FileAlignment = 0x1000;
178 nt->OptionalHeader.MajorOperatingSystemVersion = 1;
179 nt->OptionalHeader.MinorOperatingSystemVersion = 0;
180 nt->OptionalHeader.MajorSubsystemVersion = 4;
181 nt->OptionalHeader.MinorSubsystemVersion = 0;
182 nt->OptionalHeader.SizeOfImage = size;
183 nt->OptionalHeader.SizeOfHeaders = (BYTE *)exp - addr;
184 nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
185 if (dll->descr->dllentrypoint)
186 nt->OptionalHeader.AddressOfEntryPoint = (DWORD)dll->descr->dllentrypoint - (DWORD)addr;
188 /* Build the export directory */
190 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
191 dir->VirtualAddress = (BYTE *)exp - addr;
192 dir->Size = sizeof(*exp)
193 + dll->descr->nb_funcs * sizeof(LPVOID)
194 + dll->descr->nb_names * sizeof(LPSTR);
196 /* Build the exports section */
198 strcpy( sec->Name, ".edata" );
199 sec->Misc.VirtualSize = dir->Size;
200 sec->VirtualAddress = (BYTE *)exp - addr;
201 sec->SizeOfRawData = dir->Size;
202 sec->PointerToRawData = (BYTE *)exp - addr;
203 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
204 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ |
205 IMAGE_SCN_MEM_WRITE);
207 /* Build the code section */
210 strcpy( sec->Name, ".code" );
211 sec->SizeOfRawData = 0;
214 sec->SizeOfRawData += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
216 sec->Misc.VirtualSize = sec->SizeOfRawData;
217 sec->VirtualAddress = (BYTE *)debug - addr;
218 sec->PointerToRawData = (BYTE *)debug - addr;
219 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
220 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
222 /* Build the exports section data */
224 exp->Name = ((BYTE *)dll->descr->name) - addr; /*??*/
225 exp->Base = dll->descr->base;
226 exp->NumberOfFunctions = dll->descr->nb_funcs;
227 exp->NumberOfNames = dll->descr->nb_names;
228 exp->AddressOfFunctions = (LPDWORD *)((BYTE *)funcs - addr);
229 exp->AddressOfNames = (LPDWORD *)((BYTE *)names - addr);
230 exp->AddressOfNameOrdinals = (LPWORD *)((BYTE *)dll->descr->ordinals - addr);
232 /* Build the funcs table */
234 for (i = 0; i < dll->descr->nb_funcs; i++, funcs++, debug++)
236 BYTE args = dll->descr->args[i];
237 if (!dll->descr->functions[i]) continue;
238 *funcs = (LPVOID)((BYTE *)dll->descr->functions[i] - addr);
240 if (!TRACE_ON(relay)) continue;
243 case 0xfe: /* register func */
245 debug->callfrom32 = (DWORD)dll->descr->functions[i] -
247 debug->ret = 0x90; /* nop */
249 *funcs = (LPVOID)((BYTE *)debug - addr);
251 case 0xff: /* stub or extern */
253 default: /* normal function (stdcall or cdecl) */
255 debug->callfrom32 = (DWORD)RELAY_CallFrom32 -
257 debug->ret = (args & 0x80) ? 0xc3 : 0xc2; /*ret/ret $n*/
258 debug->args = (args & 0x7f) * sizeof(int);
259 *funcs = (LPVOID)((BYTE *)debug - addr);
262 #endif /* __i386__ */
265 /* Build the names table */
267 for (i = 0; i < exp->NumberOfNames; i++, names++)
268 if (dll->descr->names[i])
269 *names = (LPSTR)((BYTE *)dll->descr->names[i] - addr);
271 /* Create a modref */
272 wm = (WINE_MODREF *)HeapAlloc( pdb->heap, HEAP_ZERO_MEMORY, sizeof(*wm) );
273 wm->type = MODULE32_PE;
274 pem = &(wm->binfmt.pe);
275 wm->module = (HMODULE32)addr;
276 wm->next = pdb->modref_list;
277 pdb->modref_list = wm;
278 wm->modname = HEAP_strdupA(pdb->heap,0,dll->descr->name);
279 /* FIXME: hmm ... probably add windows directory? don't know ... -MM */
280 wm->shortname = HEAP_strdupA(pdb->heap,0,wm->modname);
281 wm->longname = HEAP_strdupA(pdb->heap,0,wm->modname);
283 pem->pe_export = exp;
284 pem->flags = PE_MODREF_INTERNAL;
286 /* Create a Win16 dummy module */
288 sprintf( ofs.szPathName, "%s.DLL", dll->descr->name );
289 hModule = MODULE_CreateDummyModule( &ofs );
290 pModule = (NE_MODULE *)GlobalLock16( hModule );
291 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN |
292 NE_FFLAGS_LIBMODULE | NE_FFLAGS_WIN32;
293 pModule->module32 = (HMODULE32)addr;
294 return pModule->module32;
298 /***********************************************************************
299 * BUILTIN32_LoadModule
301 * Load a built-in module. If the 'force' parameter is FALSE, we only
302 * load the module if it has not been disabled via the -dll option.
304 HMODULE32 BUILTIN32_LoadModule( LPCSTR name, BOOL32 force, PDB32 *process )
306 BUILTIN32_DLL *table;
307 char dllname[16], *p;
309 /* Fix the name in case we have a full path and extension */
311 if ((p = strrchr( name, '\\' ))) name = p + 1;
312 lstrcpyn32A( dllname, name, sizeof(dllname) );
313 if ((p = strrchr( dllname, '.' ))) *p = '\0';
315 for (table = BuiltinDLLs; table->descr; table++)
316 if (!lstrcmpi32A( table->descr->name, dllname )) break;
317 if (!table->descr) return 0;
320 if (!force) return 0;
321 table->used = TRUE; /* So next time we use it at once */
323 return BUILTIN32_DoLoadModule( table, process );
327 /***********************************************************************
328 * BUILTIN32_GetEntryPoint
330 * Return the name of the DLL entry point corresponding
331 * to a relay entry point address. This is used only by relay debugging.
333 * This function _must_ return the real entry point to call
334 * after the debug info is printed.
336 ENTRYPOINT32 BUILTIN32_GetEntryPoint( char *buffer, void *relay,
337 unsigned int *typemask )
343 /* First find the module */
345 for (dll = BuiltinDLLs; dll->descr; dll++)
347 && ((hModule = GetModuleHandle32A(dll->descr->name)) != 0))
349 IMAGE_SECTION_HEADER *sec = PE_SECTIONS(hModule);
350 DEBUG_ENTRY_POINT *debug =
351 (DEBUG_ENTRY_POINT *)((DWORD)hModule + sec[1].VirtualAddress);
352 DEBUG_ENTRY_POINT *func = (DEBUG_ENTRY_POINT *)relay;
354 if (debug <= func && func < debug + dll->descr->nb_funcs)
356 ordinal = func - debug;
362 return (ENTRYPOINT32)NULL;
364 /* Now find the function */
366 for (i = 0; i < dll->descr->nb_names; i++)
367 if (dll->descr->ordinals[i] == ordinal) break;
368 assert( i < dll->descr->nb_names );
370 sprintf( buffer, "%s.%d: %s", dll->descr->name, ordinal + dll->descr->base,
371 dll->descr->names[i] );
372 *typemask = dll->descr->argtypes[ordinal];
373 return dll->descr->functions[ordinal];
377 /***********************************************************************
378 * BUILTIN32_Unimplemented
380 * This function is called for unimplemented 32-bit entry points (declared
381 * as 'stub' in the spec file).
383 void BUILTIN32_Unimplemented( const BUILTIN32_DESCRIPTOR *descr, int ordinal )
385 const char *func_name = "???";
388 __RESTORE_ES; /* Just in case */
390 for (i = 0; i < descr->nb_names; i++)
391 if (descr->ordinals[i] + descr->base == ordinal) break;
392 if (i < descr->nb_names) func_name = descr->names[i];
394 MSG( "No handler for Win32 routine %s.%d: %s",
395 descr->name, ordinal, func_name );
397 MSG( " (called from %p)", __builtin_return_address(1) );
404 /***********************************************************************
405 * BUILTIN32_EnableDLL
407 * Enable or disable a built-in DLL.
409 int BUILTIN32_EnableDLL( const char *name, int len, int enable )
414 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
416 if (!lstrncmpi32A( name, dll->descr->name, len ))
426 /***********************************************************************
427 * BUILTIN32_PrintDLLs
429 * Print the list of built-in DLLs that can be disabled.
431 void BUILTIN32_PrintDLLs(void)
436 MSG("Available Win32 DLLs:\n");
437 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
438 MSG("%-9s%c", dll->descr->name,
439 ((++i) % 8) ? ' ' : '\n' );