Added HKEY_LOCAL_MACHINE\...\ProgramFilesDir registry key.
[wine.git] / relay32 / builtin32.c
blobbdfa707084eef10d31d26e949b36cb5d0327eb61
1 /*
2 * Win32 builtin functions
4 * Copyright 1997 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <string.h>
9 #include <ctype.h>
10 #include "winuser.h"
11 #include "builtin32.h"
12 #include "peexe.h"
13 #include "neexe.h"
14 #include "heap.h"
15 #include "main.h"
16 #include "snoop.h"
17 #include "winerror.h"
18 #include "debugtools.h"
20 DECLARE_DEBUG_CHANNEL(relay)
21 DECLARE_DEBUG_CHANNEL(win32)
22 DECLARE_DEBUG_CHANNEL(module)
24 typedef struct
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 */
30 } DEBUG_ENTRY_POINT;
32 typedef struct
34 const BYTE *restab;
35 const DWORD nresources;
36 const DWORD restabsize;
37 const IMAGE_RESOURCE_DATA_ENTRY *entries;
38 } BUILTIN32_RESOURCE;
40 typedef struct
42 const BUILTIN32_DESCRIPTOR *descr; /* DLL descriptor */
43 DWORD flags;
44 HMODULE hModule;
45 const BUILTIN32_RESOURCE *rsc;
46 } BUILTIN32_DLL;
48 #define BI32_INSTANTIATED 0x01
49 #define BI32_DANGER 0x02
51 extern const BUILTIN32_DESCRIPTOR ADVAPI32_Descriptor;
52 extern const BUILTIN32_DESCRIPTOR AVIFIL32_Descriptor;
53 extern const BUILTIN32_DESCRIPTOR COMCTL32_Descriptor;
54 extern const BUILTIN32_DESCRIPTOR COMDLG32_Descriptor;
55 extern const BUILTIN32_DESCRIPTOR CRTDLL_Descriptor;
56 extern const BUILTIN32_DESCRIPTOR DCIMAN32_Descriptor;
57 extern const BUILTIN32_DESCRIPTOR DDRAW_Descriptor;
58 extern const BUILTIN32_DESCRIPTOR DINPUT_Descriptor;
59 extern const BUILTIN32_DESCRIPTOR DPLAY_Descriptor;
60 extern const BUILTIN32_DESCRIPTOR DPLAYX_Descriptor;
61 extern const BUILTIN32_DESCRIPTOR DSOUND_Descriptor;
62 extern const BUILTIN32_DESCRIPTOR GDI32_Descriptor;
63 extern const BUILTIN32_DESCRIPTOR IMAGEHLP_Descriptor;
64 extern const BUILTIN32_DESCRIPTOR IMM32_Descriptor;
65 extern const BUILTIN32_DESCRIPTOR KERNEL32_Descriptor;
66 extern const BUILTIN32_DESCRIPTOR LZ32_Descriptor;
67 extern const BUILTIN32_DESCRIPTOR MPR_Descriptor;
68 extern const BUILTIN32_DESCRIPTOR MCIANIM_Descriptor;
69 extern const BUILTIN32_DESCRIPTOR MCIAVI_Descriptor;
70 extern const BUILTIN32_DESCRIPTOR MCICDA_Descriptor;
71 extern const BUILTIN32_DESCRIPTOR MCISEQ_Descriptor;
72 extern const BUILTIN32_DESCRIPTOR MCIWAVE_Descriptor;
73 extern const BUILTIN32_DESCRIPTOR MSACM32_Descriptor;
74 extern const BUILTIN32_DESCRIPTOR MSNET32_Descriptor;
75 extern const BUILTIN32_DESCRIPTOR MSVFW32_Descriptor;
76 extern const BUILTIN32_DESCRIPTOR NTDLL_Descriptor;
77 extern const BUILTIN32_DESCRIPTOR OLE32_Descriptor;
78 extern const BUILTIN32_DESCRIPTOR OLEAUT32_Descriptor;
79 extern const BUILTIN32_DESCRIPTOR OLECLI32_Descriptor;
80 extern const BUILTIN32_DESCRIPTOR OLEDLG_Descriptor;
81 extern const BUILTIN32_DESCRIPTOR OLESVR32_Descriptor;
82 extern const BUILTIN32_DESCRIPTOR PSAPI_Descriptor;
83 extern const BUILTIN32_DESCRIPTOR RASAPI32_Descriptor;
84 extern const BUILTIN32_DESCRIPTOR SHELL32_Descriptor;
85 extern const BUILTIN32_DESCRIPTOR SHLWAPI_Descriptor;
86 extern const BUILTIN32_DESCRIPTOR TAPI32_Descriptor;
87 extern const BUILTIN32_DESCRIPTOR USER32_Descriptor;
88 extern const BUILTIN32_DESCRIPTOR VERSION_Descriptor;
89 extern const BUILTIN32_DESCRIPTOR W32SKRNL_Descriptor;
90 extern const BUILTIN32_DESCRIPTOR WINMM_Descriptor;
91 extern const BUILTIN32_DESCRIPTOR WINSPOOL_Descriptor;
92 extern const BUILTIN32_DESCRIPTOR WNASPI32_Descriptor;
93 extern const BUILTIN32_DESCRIPTOR WOW32_Descriptor;
94 extern const BUILTIN32_DESCRIPTOR WSOCK32_Descriptor;
96 extern const BUILTIN32_RESOURCE comctl32_ResourceDescriptor;
97 extern const BUILTIN32_RESOURCE comdlg32_ResourceDescriptor;
98 extern const BUILTIN32_RESOURCE shell32_ResourceDescriptor;
99 extern const BUILTIN32_RESOURCE user32_ResourceDescriptor;
100 extern const BUILTIN32_RESOURCE winmm_ResourceDescriptor;
102 static BUILTIN32_DLL BuiltinDLLs[] =
104 { &ADVAPI32_Descriptor, 0, 0, NULL },
105 { &AVIFIL32_Descriptor, 0, 0, NULL },
106 { &COMCTL32_Descriptor, BI32_DANGER, 0, &comctl32_ResourceDescriptor },
107 { &COMDLG32_Descriptor, BI32_DANGER, 0, &comdlg32_ResourceDescriptor },
108 { &CRTDLL_Descriptor, BI32_DANGER, 0, NULL },
109 { &DCIMAN32_Descriptor, 0, 0, NULL },
110 { &DDRAW_Descriptor, 0, 0, NULL },
111 { &DINPUT_Descriptor, 0, 0, NULL },
112 { &DPLAY_Descriptor, 0, 0, NULL },
113 { &DPLAYX_Descriptor, 0, 0, NULL },
114 { &DSOUND_Descriptor, 0, 0, NULL },
115 { &GDI32_Descriptor, 0, 0, NULL },
116 { &IMAGEHLP_Descriptor, BI32_DANGER, 0, NULL },
117 { &IMM32_Descriptor, 0, 0, NULL },
118 { &KERNEL32_Descriptor, 0, 0, NULL },
119 { &LZ32_Descriptor, 0, 0, NULL },
120 { &MCIANIM_Descriptor, 0, 0, NULL },
121 { &MCIAVI_Descriptor, 0, 0, NULL },
122 { &MCICDA_Descriptor, 0, 0, NULL },
123 { &MCISEQ_Descriptor, 0, 0, NULL },
124 { &MCIWAVE_Descriptor, 0, 0, NULL },
125 { &MPR_Descriptor, 0, 0, NULL },
126 { &MSACM32_Descriptor, BI32_DANGER, 0, NULL },
127 { &MSNET32_Descriptor, 0, 0, NULL },
128 { &MSVFW32_Descriptor, 0, 0, NULL },
129 { &NTDLL_Descriptor, 0, 0, NULL },
130 { &OLE32_Descriptor, 0, 0, NULL },
131 { &OLEAUT32_Descriptor, 0, 0, NULL },
132 { &OLECLI32_Descriptor, 0, 0, NULL },
133 { &OLEDLG_Descriptor, 0, 0, NULL },
134 { &OLESVR32_Descriptor, 0, 0, NULL },
135 { &PSAPI_Descriptor, 0, 0, NULL },
136 { &RASAPI32_Descriptor, 0, 0, NULL },
137 { &SHELL32_Descriptor, BI32_DANGER, 0, &shell32_ResourceDescriptor },
138 { &SHLWAPI_Descriptor, 0, 0, NULL },
139 { &TAPI32_Descriptor, 0, 0, NULL },
140 { &USER32_Descriptor, 0, 0, &user32_ResourceDescriptor },
141 { &VERSION_Descriptor, 0, 0, NULL },
142 { &W32SKRNL_Descriptor, 0, 0, NULL },
143 { &WINMM_Descriptor, 0, 0, &winmm_ResourceDescriptor },
144 { &WINSPOOL_Descriptor, 0, 0, NULL },
145 { &WNASPI32_Descriptor, 0, 0, NULL },
146 { &WOW32_Descriptor, 0, 0, NULL },
147 { &WSOCK32_Descriptor, 0, 0, NULL },
148 /* Last entry */
149 { NULL, 0, 0, NULL }
152 extern void RELAY_CallFrom32();
153 extern void RELAY_CallFrom32Regs();
155 /***********************************************************************
156 * BUILTIN32_DoLoadImage
158 * Load a built-in Win32 module. Helper function for BUILTIN32_LoadImage.
160 static HMODULE BUILTIN32_DoLoadImage( BUILTIN32_DLL *dll )
163 IMAGE_DATA_DIRECTORY *dir;
164 IMAGE_DOS_HEADER *dos;
165 IMAGE_NT_HEADERS *nt;
166 IMAGE_SECTION_HEADER *sec;
167 IMAGE_EXPORT_DIRECTORY *exp;
168 IMAGE_IMPORT_DESCRIPTOR *imp;
169 LPVOID *funcs;
170 LPSTR *names;
171 LPSTR pfwd;
172 DEBUG_ENTRY_POINT *debug;
173 INT i, size, nb_sections;
174 BYTE *addr;
176 /* Allocate the module */
178 nb_sections = 2; /* exports + code */
179 if (dll->descr->nb_imports) nb_sections++;
180 size = (sizeof(IMAGE_DOS_HEADER)
181 + sizeof(IMAGE_NT_HEADERS)
182 + nb_sections * sizeof(IMAGE_SECTION_HEADER)
183 + (dll->descr->nb_imports+1) * sizeof(IMAGE_IMPORT_DESCRIPTOR)
184 + sizeof(IMAGE_EXPORT_DIRECTORY)
185 + dll->descr->nb_funcs * sizeof(LPVOID)
186 + dll->descr->nb_names * sizeof(LPSTR)
187 + dll->descr->fwd_size);
188 #ifdef __i386__
189 if (WARN_ON(relay) || TRACE_ON(relay))
190 size += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
191 #endif
192 addr = VirtualAlloc( NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
193 if (!addr) return 0;
194 dos = (IMAGE_DOS_HEADER *)addr;
195 nt = (IMAGE_NT_HEADERS *)(dos + 1);
196 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
197 imp = (IMAGE_IMPORT_DESCRIPTOR *)(sec + nb_sections);
198 exp = (IMAGE_EXPORT_DIRECTORY *)(imp + dll->descr->nb_imports + 1);
199 funcs = (LPVOID *)(exp + 1);
200 names = (LPSTR *)(funcs + dll->descr->nb_funcs);
201 pfwd = (LPSTR)(names + dll->descr->nb_names);
202 debug = (DEBUG_ENTRY_POINT *)(pfwd + dll->descr->fwd_size);
204 /* Build the DOS and NT headers */
206 dos->e_magic = IMAGE_DOS_SIGNATURE;
207 dos->e_lfanew = sizeof(*dos);
209 nt->Signature = IMAGE_NT_SIGNATURE;
210 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
211 nt->FileHeader.NumberOfSections = nb_sections;
212 nt->FileHeader.SizeOfOptionalHeader = sizeof(nt->OptionalHeader);
213 nt->FileHeader.Characteristics = IMAGE_FILE_DLL;
215 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
216 nt->OptionalHeader.SizeOfCode = 0x1000;
217 nt->OptionalHeader.SizeOfInitializedData = 0;
218 nt->OptionalHeader.SizeOfUninitializedData = 0;
219 nt->OptionalHeader.ImageBase = (DWORD)addr;
220 nt->OptionalHeader.SectionAlignment = 0x1000;
221 nt->OptionalHeader.FileAlignment = 0x1000;
222 nt->OptionalHeader.MajorOperatingSystemVersion = 1;
223 nt->OptionalHeader.MinorOperatingSystemVersion = 0;
224 nt->OptionalHeader.MajorSubsystemVersion = 4;
225 nt->OptionalHeader.MinorSubsystemVersion = 0;
226 nt->OptionalHeader.SizeOfImage = size;
227 nt->OptionalHeader.SizeOfHeaders = (BYTE *)exp - addr;
228 nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
229 if (dll->descr->dllentrypoint)
230 nt->OptionalHeader.AddressOfEntryPoint = (DWORD)dll->descr->dllentrypoint - (DWORD)addr;
232 /* Build the code section */
234 strcpy( sec->Name, ".code" );
235 sec->SizeOfRawData = 0;
236 #ifdef __i386__
237 if (WARN_ON(relay) || TRACE_ON(relay))
238 sec->SizeOfRawData += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
239 #endif
240 sec->Misc.VirtualSize = sec->SizeOfRawData;
241 sec->VirtualAddress = (BYTE *)debug - addr;
242 sec->PointerToRawData = (BYTE *)debug - addr;
243 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
244 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
245 sec++;
247 /* Build the import directory */
249 if (dll->descr->nb_imports)
251 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
252 dir->VirtualAddress = (BYTE *)imp - addr;
253 dir->Size = sizeof(*imp) * (dll->descr->nb_imports + 1);
255 /* Build the imports section */
256 strcpy( sec->Name, ".idata" );
257 sec->Misc.VirtualSize = dir->Size;
258 sec->VirtualAddress = (BYTE *)imp - addr;
259 sec->SizeOfRawData = dir->Size;
260 sec->PointerToRawData = (BYTE *)imp - addr;
261 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
262 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ |
263 IMAGE_SCN_MEM_WRITE);
264 sec++;
266 /* Build the imports */
267 for (i = 0; i < dll->descr->nb_imports; i++)
269 imp[i].u.Characteristics = 0;
270 imp[i].ForwarderChain = -1;
271 imp[i].Name = (BYTE *)dll->descr->imports[i] - addr;
272 /* hack: make first thunk point to some zero value */
273 imp[i].FirstThunk = (PIMAGE_THUNK_DATA)((BYTE *)&imp[i].u.Characteristics - addr);
277 /* Build the export directory */
279 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
280 dir->VirtualAddress = (BYTE *)exp - addr;
281 dir->Size = sizeof(*exp)
282 + dll->descr->nb_funcs * sizeof(LPVOID)
283 + dll->descr->nb_names * sizeof(LPSTR)
284 + dll->descr->fwd_size;
286 /* Build the exports section */
288 strcpy( sec->Name, ".edata" );
289 sec->Misc.VirtualSize = dir->Size;
290 sec->VirtualAddress = (BYTE *)exp - addr;
291 sec->SizeOfRawData = dir->Size;
292 sec->PointerToRawData = (BYTE *)exp - addr;
293 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
294 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ |
295 IMAGE_SCN_MEM_WRITE);
296 sec++;
298 /* Build the resource directory */
299 if(dll->rsc)
301 int i;
302 void *rtab;
303 IMAGE_RESOURCE_DATA_ENTRY *rdep;
305 rtab = HeapAlloc(GetProcessHeap(), 0, dll->rsc->restabsize);
306 if(!rtab)
308 ERR_(module)("Failed to get memory for resource directory\n");
309 VirtualFree(addr, size, MEM_RELEASE);
310 return 0;
314 * The resource directory has to be copied because it contains
315 * RVAs. These would be invalid if the dll is instantiated twice.
317 memcpy(rtab, dll->rsc->restab, dll->rsc->restabsize);
319 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
320 dir->VirtualAddress = (DWORD)rtab - (DWORD)addr;
321 dir->Size = dll->rsc->restabsize;
322 rdep = (IMAGE_RESOURCE_DATA_ENTRY *)((DWORD)rtab + (DWORD)dll->rsc->entries - (DWORD)dll->rsc->restab);
323 for(i = 0; i < dll->rsc->nresources; i++)
325 rdep[i].OffsetToData += (DWORD)dll->rsc->restab - (DWORD)addr;
329 /* Build the exports section data */
331 exp->Name = ((BYTE *)dll->descr->name) - addr; /*??*/
332 exp->Base = dll->descr->base;
333 exp->NumberOfFunctions = dll->descr->nb_funcs;
334 exp->NumberOfNames = dll->descr->nb_names;
335 exp->AddressOfFunctions = (LPDWORD *)((BYTE *)funcs - addr);
336 exp->AddressOfNames = (LPDWORD *)((BYTE *)names - addr);
337 exp->AddressOfNameOrdinals = (LPWORD *)((BYTE *)dll->descr->ordinals - addr);
339 /* Build the funcs table */
341 for (i = 0; i < dll->descr->nb_funcs; i++, funcs++, debug++)
343 BYTE args = dll->descr->args[i];
344 int j;
346 if (!dll->descr->functions[i]) continue;
348 if (args == 0xfd) /* forward func */
350 strcpy( pfwd, (LPSTR)dll->descr->functions[i] );
351 *funcs = (LPVOID)((BYTE *)pfwd - addr);
352 pfwd += strlen(pfwd) + 1;
354 else *funcs = (LPVOID)((BYTE *)dll->descr->functions[i] - addr);
356 #ifdef __i386__
357 if (!(WARN_ON(relay) || TRACE_ON(relay))) continue;
358 for (j=0;j<dll->descr->nb_names;j++)
359 if (dll->descr->ordinals[j] == i)
360 break;
361 if (j<dll->descr->nb_names) {
362 if (dll->descr->names[j]) {
363 char buffer[200];
364 sprintf(buffer,"%s.%d: %s",dll->descr->name,i,dll->descr->names[j]);
365 if (!RELAY_ShowDebugmsgRelay(buffer))
366 continue;
369 switch(args)
371 case 0xfd: /* forward */
372 case 0xff: /* stub or extern */
373 break;
374 default: /* normal function (stdcall or cdecl or register) */
375 if (TRACE_ON(relay)) {
376 debug->call = 0xe8; /* lcall relative */
377 if (args & 0x40) /* register func */
378 debug->callfrom32 = (DWORD)RELAY_CallFrom32Regs -
379 (DWORD)&debug->ret;
380 else
381 debug->callfrom32 = (DWORD)RELAY_CallFrom32 -
382 (DWORD)&debug->ret;
383 } else {
384 debug->call = 0xe9; /* ljmp relative */
385 debug->callfrom32 = (DWORD)dll->descr->functions[i] -
386 (DWORD)&debug->ret;
388 debug->ret = (args & 0x80) ? 0xc3 : 0xc2; /*ret/ret $n*/
389 debug->args = (args & 0x3f) * sizeof(int);
390 *funcs = (LPVOID)((BYTE *)debug - addr);
391 break;
393 #endif /* __i386__ */
396 /* Build the names table */
398 for (i = 0; i < exp->NumberOfNames; i++, names++)
399 if (dll->descr->names[i])
400 *names = (LPSTR)((BYTE *)dll->descr->names[i] - addr);
402 return (HMODULE)addr;
405 /***********************************************************************
406 * BUILTIN32_LoadImage
408 * Load a built-in module.
410 HMODULE BUILTIN32_LoadImage( LPCSTR name, OFSTRUCT *ofs)
412 BUILTIN32_DLL *table;
413 char dllname[16], *p;
415 /* Fix the name in case we have a full path and extension */
417 if ((p = strrchr( name, '\\' ))) name = p + 1;
418 lstrcpynA( dllname, name, sizeof(dllname) );
419 if ((p = strrchr( dllname, '.' ))) *p = '\0';
421 for (table = BuiltinDLLs; table->descr; table++)
422 if (!lstrcmpiA( table->descr->name, dllname )) break;
423 if (!table->descr) return 0;
425 if ( (table->flags & BI32_INSTANTIATED) && (table->flags & BI32_DANGER) )
427 ERR_(module)("Attemp to instantiate built-in dll '%s' twice in the same address-space. Expect trouble!\n",
428 table->descr->name);
431 sprintf( ofs->szPathName, "%s.DLL", table->descr->name );
433 if ( !table->hModule )
434 table->hModule = BUILTIN32_DoLoadImage( table );
436 if ( table->hModule )
437 table->flags |= BI32_INSTANTIATED;
439 return table->hModule;
443 /***********************************************************************
444 * BUILTIN32_LoadLibraryExA
446 * Partly copied from the original PE_ version.
448 * Note: This implementation is not very nice and should be one with
449 * the BUILTIN32_LoadImage function. But, we don't care too much
450 * because this code will obsolete itself shortly when we get the
451 * modularization of wine implemented (BS 05-Mar-1999).
453 WINE_MODREF *BUILTIN32_LoadLibraryExA(LPCSTR path, DWORD flags, DWORD *err)
455 LPCSTR modName = NULL;
456 OFSTRUCT ofs;
457 HMODULE hModule32;
458 HMODULE16 hModule16;
459 NE_MODULE *pModule;
460 WINE_MODREF *wm;
461 char dllname[256], *p;
463 /* Append .DLL to name if no extension present */
464 strcpy( dllname, path );
465 if (!(p = strrchr( dllname, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
466 strcat( dllname, ".DLL" );
468 hModule32 = BUILTIN32_LoadImage(path, &ofs);
469 if(!hModule32)
471 *err = ERROR_FILE_NOT_FOUND;
472 return NULL;
475 /* Create 16-bit dummy module */
476 if ((hModule16 = MODULE_CreateDummyModule( &ofs, modName )) < 32)
478 *err = (DWORD)hModule16;
479 return NULL; /* FIXME: Should unload the builtin module */
482 pModule = (NE_MODULE *)GlobalLock16( hModule16 );
483 pModule->flags = NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA | NE_FFLAGS_WIN32 | NE_FFLAGS_BUILTIN;
484 pModule->module32 = hModule32;
486 /* Create 32-bit MODREF */
487 if ( !(wm = PE_CreateModule( hModule32, &ofs, flags, TRUE )) )
489 ERR_(win32)("can't load %s\n",ofs.szPathName);
490 FreeLibrary16( hModule16 ); /* FIXME: Should unload the builtin module */
491 *err = ERROR_OUTOFMEMORY;
492 return NULL;
495 if (wm->binfmt.pe.pe_export)
496 SNOOP_RegisterDLL(wm->module,wm->modname,wm->binfmt.pe.pe_export->NumberOfFunctions);
498 *err = 0;
499 return wm;
503 /***********************************************************************
504 * BUILTIN32_UnloadLibrary
506 * Unload the built-in library and free the modref.
508 void BUILTIN32_UnloadLibrary(WINE_MODREF *wm)
510 /* FIXME: do something here */
514 /***********************************************************************
515 * BUILTIN32_GetEntryPoint
517 * Return the name of the DLL entry point corresponding
518 * to a relay entry point address. This is used only by relay debugging.
520 * This function _must_ return the real entry point to call
521 * after the debug info is printed.
523 ENTRYPOINT32 BUILTIN32_GetEntryPoint( char *buffer, void *relay,
524 unsigned int *typemask )
526 BUILTIN32_DLL *dll;
527 int ordinal = 0, i;
529 /* First find the module */
531 for (dll = BuiltinDLLs; dll->descr; dll++)
532 if ( dll->flags & BI32_INSTANTIATED )
534 IMAGE_SECTION_HEADER *sec = PE_SECTIONS(dll->hModule);
535 DEBUG_ENTRY_POINT *debug =
536 (DEBUG_ENTRY_POINT *)((DWORD)dll->hModule + sec[0].VirtualAddress);
537 DEBUG_ENTRY_POINT *func = (DEBUG_ENTRY_POINT *)relay;
539 if (debug <= func && func < debug + dll->descr->nb_funcs)
541 ordinal = func - debug;
542 break;
546 if (!dll->descr)
547 return (ENTRYPOINT32)NULL;
549 /* Now find the function */
551 for (i = 0; i < dll->descr->nb_names; i++)
552 if (dll->descr->ordinals[i] == ordinal) break;
553 assert( i < dll->descr->nb_names );
555 sprintf( buffer, "%s.%d: %s", dll->descr->name, ordinal + dll->descr->base,
556 dll->descr->names[i] );
557 *typemask = dll->descr->argtypes[ordinal];
558 return dll->descr->functions[ordinal];
561 /***********************************************************************
562 * BUILTIN32_SwitchRelayDebug
564 * FIXME: enhance to do it module relative.
566 void BUILTIN32_SwitchRelayDebug(BOOL onoff) {
567 BUILTIN32_DLL *dll;
568 int i;
570 #ifdef __i386__
571 if (!(TRACE_ON(relay) || WARN_ON(relay)))
572 return;
573 for (dll = BuiltinDLLs; dll->descr; dll++) {
574 IMAGE_SECTION_HEADER *sec;
575 DEBUG_ENTRY_POINT *debug;
576 if (!(dll->flags & BI32_INSTANTIATED))
577 continue;
579 sec = PE_SECTIONS(dll->hModule);
580 debug = (DEBUG_ENTRY_POINT *)((DWORD)dll->hModule + sec[1].VirtualAddress);
581 for (i = 0; i < dll->descr->nb_funcs; i++,debug++) {
582 if (!dll->descr->functions[i]) continue;
583 if ((dll->descr->args[i]==0xff) || (dll->descr->args[i]==0xfe))
584 continue;
585 if (onoff) {
586 debug->call = 0xe8; /* lcall relative */
587 debug->callfrom32 = (DWORD)RELAY_CallFrom32 -
588 (DWORD)&debug->ret;
589 } else {
590 debug->call = 0xe9; /* ljmp relative */
591 debug->callfrom32 = (DWORD)dll->descr->functions[i] -
592 (DWORD)&debug->ret;
596 #endif /* __i386__ */
597 return;
600 /***********************************************************************
601 * BUILTIN32_Unimplemented
603 * This function is called for unimplemented 32-bit entry points (declared
604 * as 'stub' in the spec file).
606 void BUILTIN32_Unimplemented( const BUILTIN32_DESCRIPTOR *descr, int ordinal )
608 const char *func_name = "???";
609 int i;
611 __RESTORE_ES; /* Just in case */
613 for (i = 0; i < descr->nb_names; i++)
614 if (descr->ordinals[i] + descr->base == ordinal) break;
615 if (i < descr->nb_names) func_name = descr->names[i];
617 MESSAGE( "No handler for Win32 routine %s.%d: %s",
618 descr->name, ordinal, func_name );
619 #ifdef __GNUC__
620 MESSAGE( " (called from %p)", __builtin_return_address(1) );
621 #endif
622 MESSAGE( "\n" );
623 ExitProcess(1);