Fixed small memory corruption.
[wine/dcerpc.git] / relay32 / builtin32.c
blobc82c124e0de0649c754607cbcdb5117e5cf8a9d4
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 const BUILTIN32_RESOURCE *rsc;
45 } BUILTIN32_DLL;
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 MCIAVI_Descriptor;
67 extern const BUILTIN32_DESCRIPTOR MCIANIM_Descriptor;
68 extern const BUILTIN32_DESCRIPTOR MCICDA_Descriptor;
69 extern const BUILTIN32_DESCRIPTOR MCISEQ_Descriptor;
70 extern const BUILTIN32_DESCRIPTOR MCIWAVE_Descriptor;
71 extern const BUILTIN32_DESCRIPTOR MSACM32_Descriptor;
72 extern const BUILTIN32_DESCRIPTOR MSNET32_Descriptor;
73 extern const BUILTIN32_DESCRIPTOR MSVFW32_Descriptor;
74 extern const BUILTIN32_DESCRIPTOR NTDLL_Descriptor;
75 extern const BUILTIN32_DESCRIPTOR OLE32_Descriptor;
76 extern const BUILTIN32_DESCRIPTOR OLEAUT32_Descriptor;
77 extern const BUILTIN32_DESCRIPTOR OLECLI32_Descriptor;
78 extern const BUILTIN32_DESCRIPTOR OLEDLG_Descriptor;
79 extern const BUILTIN32_DESCRIPTOR OLESVR32_Descriptor;
80 extern const BUILTIN32_DESCRIPTOR PSAPI_Descriptor;
81 extern const BUILTIN32_DESCRIPTOR RASAPI32_Descriptor;
82 extern const BUILTIN32_DESCRIPTOR SHELL32_Descriptor;
83 extern const BUILTIN32_DESCRIPTOR TAPI32_Descriptor;
84 extern const BUILTIN32_DESCRIPTOR USER32_Descriptor;
85 extern const BUILTIN32_DESCRIPTOR VERSION_Descriptor;
86 extern const BUILTIN32_DESCRIPTOR W32SKRNL_Descriptor;
87 extern const BUILTIN32_DESCRIPTOR WINMM_Descriptor;
88 extern const BUILTIN32_DESCRIPTOR WINSPOOL_Descriptor;
89 extern const BUILTIN32_DESCRIPTOR WNASPI32_Descriptor;
90 extern const BUILTIN32_DESCRIPTOR WOW32_Descriptor;
91 extern const BUILTIN32_DESCRIPTOR WSOCK32_Descriptor;
93 extern const BUILTIN32_RESOURCE comctl32_ResourceDescriptor;
94 extern const BUILTIN32_RESOURCE comdlg32_ResourceDescriptor;
95 extern const BUILTIN32_RESOURCE shell32_ResourceDescriptor;
96 extern const BUILTIN32_RESOURCE user32_ResourceDescriptor;
98 static BUILTIN32_DLL BuiltinDLLs[] =
100 { &ADVAPI32_Descriptor, 0, NULL },
101 { &AVIFIL32_Descriptor, 0, NULL },
102 { &COMCTL32_Descriptor, 0, &comctl32_ResourceDescriptor },
103 { &COMDLG32_Descriptor, 0, &comdlg32_ResourceDescriptor },
104 { &CRTDLL_Descriptor, 0, NULL },
105 { &DCIMAN32_Descriptor, 0, NULL },
106 { &DDRAW_Descriptor, 0, NULL },
107 { &DINPUT_Descriptor, 0, NULL },
108 { &DPLAY_Descriptor, 0, NULL },
109 { &DPLAYX_Descriptor, 0, NULL },
110 { &DSOUND_Descriptor, 0, NULL },
111 { &GDI32_Descriptor, 0, NULL },
112 { &IMAGEHLP_Descriptor, 0, NULL },
113 { &IMM32_Descriptor, 0, NULL },
114 { &KERNEL32_Descriptor, 0, NULL },
115 { &LZ32_Descriptor, 0, NULL },
116 { &MCIANIM_Descriptor, 0, NULL },
117 { &MCIAVI_Descriptor, 0, NULL },
118 { &MCICDA_Descriptor, 0, NULL },
119 { &MCISEQ_Descriptor, 0, NULL },
120 { &MCIWAVE_Descriptor, 0, NULL },
121 { &MPR_Descriptor, 0, NULL },
122 { &MSACM32_Descriptor, 0, NULL },
123 { &MSNET32_Descriptor, 0, NULL },
124 { &MSVFW32_Descriptor, 0, NULL },
125 { &NTDLL_Descriptor, 0, NULL },
126 { &OLE32_Descriptor, 0, NULL },
127 { &OLEAUT32_Descriptor, 0, NULL },
128 { &OLECLI32_Descriptor, 0, NULL },
129 { &OLEDLG_Descriptor, 0, NULL },
130 { &OLESVR32_Descriptor, 0, NULL },
131 { &PSAPI_Descriptor, 0, NULL },
132 { &RASAPI32_Descriptor, 0, NULL },
133 { &SHELL32_Descriptor, 0, &shell32_ResourceDescriptor },
134 { &TAPI32_Descriptor, 0, NULL },
135 { &USER32_Descriptor, 0, &user32_ResourceDescriptor },
136 { &VERSION_Descriptor, 0, NULL },
137 { &W32SKRNL_Descriptor, 0, NULL },
138 { &WINMM_Descriptor, 0, NULL },
139 { &WINSPOOL_Descriptor, 0, NULL },
140 { &WNASPI32_Descriptor, 0, NULL },
141 { &WOW32_Descriptor, 0, NULL },
142 { &WSOCK32_Descriptor, 0, NULL },
143 /* Last entry */
144 { NULL, 0, NULL }
147 extern void RELAY_CallFrom32();
148 extern void RELAY_CallFrom32Regs();
150 /***********************************************************************
151 * BUILTIN32_DoLoadImage
153 * Load a built-in Win32 module. Helper function for BUILTIN32_LoadImage.
155 static HMODULE BUILTIN32_DoLoadImage( BUILTIN32_DLL *dll )
158 IMAGE_DATA_DIRECTORY *dir;
159 IMAGE_DOS_HEADER *dos;
160 IMAGE_NT_HEADERS *nt;
161 IMAGE_SECTION_HEADER *sec;
162 IMAGE_EXPORT_DIRECTORY *exp;
163 IMAGE_IMPORT_DESCRIPTOR *imp;
164 LPVOID *funcs;
165 LPSTR *names;
166 LPSTR pfwd;
167 DEBUG_ENTRY_POINT *debug;
168 INT i, size, nb_sections;
169 BYTE *addr;
171 /* Allocate the module */
173 nb_sections = 2; /* exports + code */
174 if (dll->descr->nb_imports) nb_sections++;
175 size = (sizeof(IMAGE_DOS_HEADER)
176 + sizeof(IMAGE_NT_HEADERS)
177 + nb_sections * sizeof(IMAGE_SECTION_HEADER)
178 + (dll->descr->nb_imports+1) * sizeof(IMAGE_IMPORT_DESCRIPTOR)
179 + sizeof(IMAGE_EXPORT_DIRECTORY)
180 + dll->descr->nb_funcs * sizeof(LPVOID)
181 + dll->descr->nb_names * sizeof(LPSTR)
182 + dll->descr->fwd_size);
183 #ifdef __i386__
184 if (WARN_ON(relay) || TRACE_ON(relay))
185 size += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
186 #endif
187 addr = VirtualAlloc( NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
188 if (!addr) return 0;
189 dos = (IMAGE_DOS_HEADER *)addr;
190 nt = (IMAGE_NT_HEADERS *)(dos + 1);
191 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
192 imp = (IMAGE_IMPORT_DESCRIPTOR *)(sec + nb_sections);
193 exp = (IMAGE_EXPORT_DIRECTORY *)(imp + dll->descr->nb_imports + 1);
194 funcs = (LPVOID *)(exp + 1);
195 names = (LPSTR *)(funcs + dll->descr->nb_funcs);
196 pfwd = (LPSTR)(names + dll->descr->nb_names);
197 debug = (DEBUG_ENTRY_POINT *)(pfwd + dll->descr->fwd_size);
199 /* Build the DOS and NT headers */
201 dos->e_magic = IMAGE_DOS_SIGNATURE;
202 dos->e_lfanew = sizeof(*dos);
204 nt->Signature = IMAGE_NT_SIGNATURE;
205 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
206 nt->FileHeader.NumberOfSections = nb_sections;
207 nt->FileHeader.SizeOfOptionalHeader = sizeof(nt->OptionalHeader);
208 nt->FileHeader.Characteristics = IMAGE_FILE_DLL;
210 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
211 nt->OptionalHeader.SizeOfCode = 0x1000;
212 nt->OptionalHeader.SizeOfInitializedData = 0;
213 nt->OptionalHeader.SizeOfUninitializedData = 0;
214 nt->OptionalHeader.ImageBase = (DWORD)addr;
215 nt->OptionalHeader.SectionAlignment = 0x1000;
216 nt->OptionalHeader.FileAlignment = 0x1000;
217 nt->OptionalHeader.MajorOperatingSystemVersion = 1;
218 nt->OptionalHeader.MinorOperatingSystemVersion = 0;
219 nt->OptionalHeader.MajorSubsystemVersion = 4;
220 nt->OptionalHeader.MinorSubsystemVersion = 0;
221 nt->OptionalHeader.SizeOfImage = size;
222 nt->OptionalHeader.SizeOfHeaders = (BYTE *)exp - addr;
223 nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
224 if (dll->descr->dllentrypoint)
225 nt->OptionalHeader.AddressOfEntryPoint = (DWORD)dll->descr->dllentrypoint - (DWORD)addr;
227 /* Build the code section */
229 strcpy( sec->Name, ".code" );
230 sec->SizeOfRawData = 0;
231 #ifdef __i386__
232 if (WARN_ON(relay) || TRACE_ON(relay))
233 sec->SizeOfRawData += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
234 #endif
235 sec->Misc.VirtualSize = sec->SizeOfRawData;
236 sec->VirtualAddress = (BYTE *)debug - addr;
237 sec->PointerToRawData = (BYTE *)debug - addr;
238 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
239 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
240 sec++;
242 /* Build the import directory */
244 if (dll->descr->nb_imports)
246 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
247 dir->VirtualAddress = (BYTE *)imp - addr;
248 dir->Size = sizeof(*imp) * (dll->descr->nb_imports + 1);
250 /* Build the imports section */
251 strcpy( sec->Name, ".idata" );
252 sec->Misc.VirtualSize = dir->Size;
253 sec->VirtualAddress = (BYTE *)imp - addr;
254 sec->SizeOfRawData = dir->Size;
255 sec->PointerToRawData = (BYTE *)imp - addr;
256 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
257 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ |
258 IMAGE_SCN_MEM_WRITE);
259 sec++;
261 /* Build the imports */
262 for (i = 0; i < dll->descr->nb_imports; i++)
264 imp[i].u.Characteristics = 0;
265 imp[i].ForwarderChain = -1;
266 imp[i].Name = (BYTE *)dll->descr->imports[i] - addr;
267 /* hack: make first thunk point to some zero value */
268 imp[i].FirstThunk = (PIMAGE_THUNK_DATA)((BYTE *)&imp[i].u.Characteristics - addr);
272 /* Build the export directory */
274 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
275 dir->VirtualAddress = (BYTE *)exp - addr;
276 dir->Size = sizeof(*exp)
277 + dll->descr->nb_funcs * sizeof(LPVOID)
278 + dll->descr->nb_names * sizeof(LPSTR)
279 + dll->descr->fwd_size;
281 /* Build the exports section */
283 strcpy( sec->Name, ".edata" );
284 sec->Misc.VirtualSize = dir->Size;
285 sec->VirtualAddress = (BYTE *)exp - addr;
286 sec->SizeOfRawData = dir->Size;
287 sec->PointerToRawData = (BYTE *)exp - addr;
288 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
289 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ |
290 IMAGE_SCN_MEM_WRITE);
291 sec++;
293 /* Build the resource directory */
294 if(dll->rsc)
296 int i;
297 void *rtab;
298 IMAGE_RESOURCE_DATA_ENTRY *rdep;
300 rtab = HeapAlloc(GetProcessHeap(), 0, dll->rsc->restabsize);
301 if(!rtab)
303 ERR_(module)("Failed to get memory for resource directory\n");
304 VirtualFree(addr, size, MEM_RELEASE);
305 return 0;
309 * The resource directory has to be copied because it contains
310 * RVAs. These would be invalid if the dll is instantiated twice.
312 memcpy(rtab, dll->rsc->restab, dll->rsc->restabsize);
314 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
315 dir->VirtualAddress = (DWORD)rtab - (DWORD)addr;
316 dir->Size = dll->rsc->restabsize;
317 rdep = (IMAGE_RESOURCE_DATA_ENTRY *)((DWORD)rtab + (DWORD)dll->rsc->entries - (DWORD)dll->rsc->restab);
318 for(i = 0; i < dll->rsc->nresources; i++)
320 rdep[i].OffsetToData += (DWORD)dll->rsc->restab - (DWORD)addr;
324 /* Build the exports section data */
326 exp->Name = ((BYTE *)dll->descr->name) - addr; /*??*/
327 exp->Base = dll->descr->base;
328 exp->NumberOfFunctions = dll->descr->nb_funcs;
329 exp->NumberOfNames = dll->descr->nb_names;
330 exp->AddressOfFunctions = (LPDWORD *)((BYTE *)funcs - addr);
331 exp->AddressOfNames = (LPDWORD *)((BYTE *)names - addr);
332 exp->AddressOfNameOrdinals = (LPWORD *)((BYTE *)dll->descr->ordinals - addr);
334 /* Build the funcs table */
336 for (i = 0; i < dll->descr->nb_funcs; i++, funcs++, debug++)
338 BYTE args = dll->descr->args[i];
339 int j;
341 if (!dll->descr->functions[i]) continue;
343 if (args == 0xfd) /* forward func */
345 strcpy( pfwd, (LPSTR)dll->descr->functions[i] );
346 *funcs = (LPVOID)((BYTE *)pfwd - addr);
347 pfwd += strlen(pfwd) + 1;
349 else *funcs = (LPVOID)((BYTE *)dll->descr->functions[i] - addr);
351 #ifdef __i386__
352 if (!(WARN_ON(relay) || TRACE_ON(relay))) continue;
353 for (j=0;j<dll->descr->nb_names;j++)
354 if (dll->descr->ordinals[j] == i)
355 break;
356 if (j<dll->descr->nb_names) {
357 if (dll->descr->names[j]) {
358 char buffer[200];
359 sprintf(buffer,"%s.%d: %s",dll->descr->name,i,dll->descr->names[j]);
360 if (!RELAY_ShowDebugmsgRelay(buffer))
361 continue;
364 switch(args)
366 case 0xfd: /* forward */
367 case 0xff: /* stub or extern */
368 break;
369 default: /* normal function (stdcall or cdecl or register) */
370 if (TRACE_ON(relay)) {
371 debug->call = 0xe8; /* lcall relative */
372 if (args & 0x40) /* register func */
373 debug->callfrom32 = (DWORD)RELAY_CallFrom32Regs -
374 (DWORD)&debug->ret;
375 else
376 debug->callfrom32 = (DWORD)RELAY_CallFrom32 -
377 (DWORD)&debug->ret;
378 } else {
379 debug->call = 0xe9; /* ljmp relative */
380 debug->callfrom32 = (DWORD)dll->descr->functions[i] -
381 (DWORD)&debug->ret;
383 debug->ret = (args & 0x80) ? 0xc3 : 0xc2; /*ret/ret $n*/
384 debug->args = (args & 0x3f) * sizeof(int);
385 *funcs = (LPVOID)((BYTE *)debug - addr);
386 break;
388 #endif /* __i386__ */
391 /* Build the names table */
393 for (i = 0; i < exp->NumberOfNames; i++, names++)
394 if (dll->descr->names[i])
395 *names = (LPSTR)((BYTE *)dll->descr->names[i] - addr);
397 return (HMODULE)addr;
400 /***********************************************************************
401 * BUILTIN32_LoadImage
403 * Load a built-in module.
405 HMODULE BUILTIN32_LoadImage( LPCSTR name, OFSTRUCT *ofs)
407 BUILTIN32_DLL *table;
408 char dllname[16], *p;
409 HMODULE hmod;
411 /* Fix the name in case we have a full path and extension */
413 if ((p = strrchr( name, '\\' ))) name = p + 1;
414 lstrcpynA( dllname, name, sizeof(dllname) );
415 if ((p = strrchr( dllname, '.' ))) *p = '\0';
417 for (table = BuiltinDLLs; table->descr; table++)
418 if (!lstrcmpiA( table->descr->name, dllname )) break;
419 if (!table->descr) return 0;
421 if(table->flags & BI32_INSTANTIATED)
423 ERR_(module)("Attemp to instantiate built-in dll '%s' twice in the same address-space. Expect trouble!\n",
424 table->descr->name);
427 sprintf( ofs->szPathName, "%s.DLL", table->descr->name );
428 hmod = BUILTIN32_DoLoadImage( table );
429 if(hmod)
430 table->flags |= BI32_INSTANTIATED;
432 return hmod;
436 /***********************************************************************
437 * BUILTIN32_LoadLibraryExA
439 * Partly copied from the original PE_ version.
441 * Note: This implementation is not very nice and should be one with
442 * the BUILTIN32_LoadImage function. But, we don't care too much
443 * because this code will obsolete itself shortly when we get the
444 * modularization of wine implemented (BS 05-Mar-1999).
446 WINE_MODREF *BUILTIN32_LoadLibraryExA(LPCSTR path, DWORD flags, DWORD *err)
448 LPCSTR modName = NULL;
449 OFSTRUCT ofs;
450 HMODULE hModule32;
451 HMODULE16 hModule16;
452 NE_MODULE *pModule;
453 WINE_MODREF *wm;
454 char dllname[256], *p;
456 /* Append .DLL to name if no extension present */
457 strcpy( dllname, path );
458 if (!(p = strrchr( dllname, '.')) || strchr( p, '/' ) || strchr( p, '\\'))
459 strcat( dllname, ".DLL" );
461 hModule32 = BUILTIN32_LoadImage(path, &ofs);
462 if(!hModule32)
464 *err = ERROR_FILE_NOT_FOUND;
465 return NULL;
468 /* Create 16-bit dummy module */
469 if ((hModule16 = MODULE_CreateDummyModule( &ofs, modName )) < 32)
471 *err = (DWORD)hModule16;
472 return NULL; /* FIXME: Should unload the builtin module */
475 pModule = (NE_MODULE *)GlobalLock16( hModule16 );
476 pModule->flags = NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA | NE_FFLAGS_WIN32 | NE_FFLAGS_BUILTIN;
477 pModule->module32 = hModule32;
479 /* Create 32-bit MODREF */
480 if ( !(wm = PE_CreateModule( hModule32, &ofs, flags, TRUE )) )
482 ERR_(win32)("can't load %s\n",ofs.szPathName);
483 FreeLibrary16( hModule16 ); /* FIXME: Should unload the builtin module */
484 *err = ERROR_OUTOFMEMORY;
485 return NULL;
488 if (wm->binfmt.pe.pe_export)
489 SNOOP_RegisterDLL(wm->module,wm->modname,wm->binfmt.pe.pe_export->NumberOfFunctions);
491 *err = 0;
492 return wm;
496 /***********************************************************************
497 * BUILTIN32_UnloadLibrary
499 * Unload the built-in library and free the modref.
501 void BUILTIN32_UnloadLibrary(WINE_MODREF *wm)
503 /* FIXME: do something here */
507 /***********************************************************************
508 * BUILTIN32_GetEntryPoint
510 * Return the name of the DLL entry point corresponding
511 * to a relay entry point address. This is used only by relay debugging.
513 * This function _must_ return the real entry point to call
514 * after the debug info is printed.
516 ENTRYPOINT32 BUILTIN32_GetEntryPoint( char *buffer, void *relay,
517 unsigned int *typemask )
519 BUILTIN32_DLL *dll;
520 HMODULE hModule;
521 int ordinal = 0, i;
523 /* First find the module */
525 for (dll = BuiltinDLLs; dll->descr; dll++)
526 if ((dll->flags & BI32_INSTANTIATED)
527 && ((hModule = GetModuleHandleA(dll->descr->name)) != 0))
529 IMAGE_SECTION_HEADER *sec = PE_SECTIONS(hModule);
530 DEBUG_ENTRY_POINT *debug =
531 (DEBUG_ENTRY_POINT *)((DWORD)hModule + sec[0].VirtualAddress);
532 DEBUG_ENTRY_POINT *func = (DEBUG_ENTRY_POINT *)relay;
534 if (debug <= func && func < debug + dll->descr->nb_funcs)
536 ordinal = func - debug;
537 break;
541 if (!dll->descr)
542 return (ENTRYPOINT32)NULL;
544 /* Now find the function */
546 for (i = 0; i < dll->descr->nb_names; i++)
547 if (dll->descr->ordinals[i] == ordinal) break;
548 assert( i < dll->descr->nb_names );
550 sprintf( buffer, "%s.%d: %s", dll->descr->name, ordinal + dll->descr->base,
551 dll->descr->names[i] );
552 *typemask = dll->descr->argtypes[ordinal];
553 return dll->descr->functions[ordinal];
556 /***********************************************************************
557 * BUILTIN32_SwitchRelayDebug
559 * FIXME: enhance to do it module relative.
561 void BUILTIN32_SwitchRelayDebug(BOOL onoff) {
562 BUILTIN32_DLL *dll;
563 HMODULE hModule;
564 int i;
566 #ifdef __i386__
567 if (!(TRACE_ON(relay) || WARN_ON(relay)))
568 return;
569 for (dll = BuiltinDLLs; dll->descr; dll++) {
570 IMAGE_SECTION_HEADER *sec;
571 DEBUG_ENTRY_POINT *debug;
572 if (!(dll->flags & BI32_INSTANTIATED) || !(hModule = GetModuleHandleA(dll->descr->name)))
573 continue;
575 sec = PE_SECTIONS(hModule);
576 debug = (DEBUG_ENTRY_POINT *)((DWORD)hModule + sec[1].VirtualAddress);
577 for (i = 0; i < dll->descr->nb_funcs; i++,debug++) {
578 if (!dll->descr->functions[i]) continue;
579 if ((dll->descr->args[i]==0xff) || (dll->descr->args[i]==0xfe))
580 continue;
581 if (onoff) {
582 debug->call = 0xe8; /* lcall relative */
583 debug->callfrom32 = (DWORD)RELAY_CallFrom32 -
584 (DWORD)&debug->ret;
585 } else {
586 debug->call = 0xe9; /* ljmp relative */
587 debug->callfrom32 = (DWORD)dll->descr->functions[i] -
588 (DWORD)&debug->ret;
592 #endif /* __i386__ */
593 return;
596 /***********************************************************************
597 * BUILTIN32_Unimplemented
599 * This function is called for unimplemented 32-bit entry points (declared
600 * as 'stub' in the spec file).
602 void BUILTIN32_Unimplemented( const BUILTIN32_DESCRIPTOR *descr, int ordinal )
604 const char *func_name = "???";
605 int i;
607 __RESTORE_ES; /* Just in case */
609 for (i = 0; i < descr->nb_names; i++)
610 if (descr->ordinals[i] + descr->base == ordinal) break;
611 if (i < descr->nb_names) func_name = descr->names[i];
613 MESSAGE( "No handler for Win32 routine %s.%d: %s",
614 descr->name, ordinal, func_name );
615 #ifdef __GNUC__
616 MESSAGE( " (called from %p)", __builtin_return_address(1) );
617 #endif
618 MESSAGE( "\n" );
619 ExitProcess(1);