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 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 },
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();
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;
134 DEBUG_ENTRY_POINT *debug;
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));
150 size += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
152 addr = VirtualAlloc( NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
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 */
212 strcpy( sec->Name, ".code" );
213 sec->SizeOfRawData = 0;
216 sec->SizeOfRawData += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
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];
241 if (!dll->descr->functions[i]) continue;
242 *funcs = (LPVOID)((BYTE *)dll->descr->functions[i] - addr);
244 if (!TRACE_ON(relay)) continue;
245 for (j=0;j<dll->descr->nb_names;j++)
246 if (dll->descr->ordinals[j] == i)
248 if (j<dll->descr->nb_names) {
249 if (dll->descr->names[j]) {
251 sprintf(buffer,"%s.%d: %s",dll->descr->name,i,dll->descr->names[j]);
252 if (!RELAY_ShowDebugmsgRelay(buffer))
258 case 0xfe: /* register func */
260 debug->callfrom32 = (DWORD)dll->descr->functions[i] -
262 debug->ret = 0x90; /* nop */
264 *funcs = (LPVOID)((BYTE *)debug - addr);
266 case 0xff: /* stub or extern */
268 default: /* normal function (stdcall or cdecl) */
270 debug->callfrom32 = (DWORD)RELAY_CallFrom32 -
272 debug->ret = (args & 0x80) ? 0xc3 : 0xc2; /*ret/ret $n*/
273 debug->args = (args & 0x7f) * sizeof(int);
274 *funcs = (LPVOID)((BYTE *)debug - addr);
277 #endif /* __i386__ */
280 /* Build the names table */
282 for (i = 0; i < exp->NumberOfNames; i++, names++)
283 if (dll->descr->names[i])
284 *names = (LPSTR)((BYTE *)dll->descr->names[i] - addr);
286 /* Create a modref */
287 wm = (WINE_MODREF *)HeapAlloc( pdb->heap, HEAP_ZERO_MEMORY, sizeof(*wm) );
288 wm->type = MODULE32_PE;
289 pem = &(wm->binfmt.pe);
290 wm->module = (HMODULE32)addr;
291 wm->next = pdb->modref_list;
292 pdb->modref_list = wm;
293 wm->modname = HEAP_strdupA(pdb->heap,0,dll->descr->name);
294 /* FIXME: hmm ... probably add windows directory? don't know ... -MM */
295 wm->shortname = HEAP_strdupA(pdb->heap,0,wm->modname);
296 wm->longname = HEAP_strdupA(pdb->heap,0,wm->modname);
298 pem->pe_export = exp;
299 pem->flags = PE_MODREF_INTERNAL;
301 /* Create a Win16 dummy module */
303 sprintf( ofs.szPathName, "%s.DLL", dll->descr->name );
304 hModule = MODULE_CreateDummyModule( &ofs );
305 pModule = (NE_MODULE *)GlobalLock16( hModule );
306 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN |
307 NE_FFLAGS_LIBMODULE | NE_FFLAGS_WIN32;
308 pModule->module32 = (HMODULE32)addr;
309 return pModule->module32;
313 /***********************************************************************
314 * BUILTIN32_LoadModule
316 * Load a built-in module. If the 'force' parameter is FALSE, we only
317 * load the module if it has not been disabled via the -dll option.
319 HMODULE32 BUILTIN32_LoadModule( LPCSTR name, BOOL32 force, PDB32 *process )
321 BUILTIN32_DLL *table;
322 char dllname[16], *p;
324 /* Fix the name in case we have a full path and extension */
326 if ((p = strrchr( name, '\\' ))) name = p + 1;
327 lstrcpyn32A( dllname, name, sizeof(dllname) );
328 if ((p = strrchr( dllname, '.' ))) *p = '\0';
330 for (table = BuiltinDLLs; table->descr; table++)
331 if (!lstrcmpi32A( table->descr->name, dllname )) break;
332 if (!table->descr) return 0;
335 if (!force) return 0;
336 table->used = TRUE; /* So next time we use it at once */
338 return BUILTIN32_DoLoadModule( table, process );
342 /***********************************************************************
343 * BUILTIN32_GetEntryPoint
345 * Return the name of the DLL entry point corresponding
346 * to a relay entry point address. This is used only by relay debugging.
348 * This function _must_ return the real entry point to call
349 * after the debug info is printed.
351 ENTRYPOINT32 BUILTIN32_GetEntryPoint( char *buffer, void *relay,
352 unsigned int *typemask )
358 /* First find the module */
360 for (dll = BuiltinDLLs; dll->descr; dll++)
362 && ((hModule = GetModuleHandle32A(dll->descr->name)) != 0))
364 IMAGE_SECTION_HEADER *sec = PE_SECTIONS(hModule);
365 DEBUG_ENTRY_POINT *debug =
366 (DEBUG_ENTRY_POINT *)((DWORD)hModule + sec[1].VirtualAddress);
367 DEBUG_ENTRY_POINT *func = (DEBUG_ENTRY_POINT *)relay;
369 if (debug <= func && func < debug + dll->descr->nb_funcs)
371 ordinal = func - debug;
377 return (ENTRYPOINT32)NULL;
379 /* Now find the function */
381 for (i = 0; i < dll->descr->nb_names; i++)
382 if (dll->descr->ordinals[i] == ordinal) break;
383 assert( i < dll->descr->nb_names );
385 sprintf( buffer, "%s.%d: %s", dll->descr->name, ordinal + dll->descr->base,
386 dll->descr->names[i] );
387 *typemask = dll->descr->argtypes[ordinal];
388 return dll->descr->functions[ordinal];
392 /***********************************************************************
393 * BUILTIN32_Unimplemented
395 * This function is called for unimplemented 32-bit entry points (declared
396 * as 'stub' in the spec file).
398 void BUILTIN32_Unimplemented( const BUILTIN32_DESCRIPTOR *descr, int ordinal )
400 const char *func_name = "???";
403 __RESTORE_ES; /* Just in case */
405 for (i = 0; i < descr->nb_names; i++)
406 if (descr->ordinals[i] + descr->base == ordinal) break;
407 if (i < descr->nb_names) func_name = descr->names[i];
409 MSG( "No handler for Win32 routine %s.%d: %s",
410 descr->name, ordinal, func_name );
412 MSG( " (called from %p)", __builtin_return_address(1) );
419 /***********************************************************************
420 * BUILTIN32_EnableDLL
422 * Enable or disable a built-in DLL.
424 int BUILTIN32_EnableDLL( const char *name, int len, int enable )
429 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
431 if (!lstrncmpi32A( name, dll->descr->name, len ))
441 /***********************************************************************
442 * BUILTIN32_PrintDLLs
444 * Print the list of built-in DLLs that can be disabled.
446 void BUILTIN32_PrintDLLs(void)
451 MSG("Available Win32 DLLs:\n");
452 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
453 MSG("%-9s%c", dll->descr->name,
454 ((++i) % 8) ? ' ' : '\n' );