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