2 * Win32 builtin functions
4 * Copyright 1997 Alexandre Julliard
14 #include "builtin32.h"
22 #include "debugtools.h"
23 #include "options.h" /* for argv0 */
25 DEFAULT_DEBUG_CHANNEL(module
);
26 DECLARE_DEBUG_CHANNEL(relay
);
30 BYTE call
; /* 0xe8 call callfrom32 (relative) */
31 DWORD callfrom32 WINE_PACKED
; /* RELAY_CallFrom32 relative addr */
32 BYTE ret
; /* 0xc2 ret $n or 0xc3 ret */
33 WORD args
; /* nb of args to remove from the stack */
39 const DWORD nresources
;
40 const DWORD restabsize
;
41 const IMAGE_RESOURCE_DATA_ENTRY
*entries
;
46 static const BUILTIN32_DESCRIPTOR
*builtin_dlls
[MAX_DLLS
];
47 static HMODULE dll_modules
[MAX_DLLS
];
50 extern void RELAY_CallFrom32();
51 extern void RELAY_CallFrom32Regs();
53 /***********************************************************************
54 * BUILTIN32_WarnSecondInstance
56 * Emit a warning when we are creating a second instance for a DLL
57 * that is known to not support this.
59 static void BUILTIN32_WarnSecondInstance( const char *name
)
61 static const char * const warning_list
[] =
62 { "comctl32", "comdlg32", "crtdll", "imagehlp", "msacm32", "shell32", NULL
};
64 const char * const *ptr
= warning_list
;
68 if (!strcasecmp( *ptr
, name
))
70 ERR( "Attempt to instantiate built-in dll '%s' twice "
71 "in the same address space. Expect trouble!\n", name
);
78 /***********************************************************************
79 * BUILTIN32_DoLoadImage
81 * Load a built-in Win32 module. Helper function for BUILTIN32_LoadImage.
83 static HMODULE
BUILTIN32_DoLoadImage( const BUILTIN32_DESCRIPTOR
*descr
)
86 IMAGE_DATA_DIRECTORY
*dir
;
87 IMAGE_DOS_HEADER
*dos
;
89 IMAGE_SECTION_HEADER
*sec
;
90 IMAGE_EXPORT_DIRECTORY
*exp
;
91 IMAGE_IMPORT_DESCRIPTOR
*imp
;
92 const BUILTIN32_RESOURCE
*rsrc
= descr
->rsrc
;
96 DEBUG_ENTRY_POINT
*debug
;
97 INT i
, size
, nb_sections
;
102 /* Allocate the module */
104 nb_sections
= 2; /* exports + code */
105 if (descr
->nb_imports
) nb_sections
++;
107 if (!strcmp(descr
->name
, "KERNEL32")) {
109 xcnsize
= sizeof(DWORD
);
111 size
= (sizeof(IMAGE_DOS_HEADER
)
112 + sizeof(IMAGE_NT_HEADERS
)
113 + nb_sections
* sizeof(IMAGE_SECTION_HEADER
)
114 + (descr
->nb_imports
+1) * sizeof(IMAGE_IMPORT_DESCRIPTOR
)
115 + sizeof(IMAGE_EXPORT_DIRECTORY
)
116 + descr
->nb_funcs
* sizeof(LPVOID
)
117 + descr
->nb_names
* sizeof(LPSTR
)
122 if (WARN_ON(relay
) || TRACE_ON(relay
))
123 size
+= descr
->nb_funcs
* sizeof(DEBUG_ENTRY_POINT
);
125 if (rsrc
) size
+= rsrc
->restabsize
;
126 addr
= VirtualAlloc( NULL
, size
, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
128 dos
= (IMAGE_DOS_HEADER
*)addr
;
129 nt
= (IMAGE_NT_HEADERS
*)(dos
+ 1);
130 sec
= (IMAGE_SECTION_HEADER
*)(nt
+ 1);
131 imp
= (IMAGE_IMPORT_DESCRIPTOR
*)(sec
+ nb_sections
);
132 exp
= (IMAGE_EXPORT_DIRECTORY
*)(imp
+ descr
->nb_imports
+ 1);
133 funcs
= (LPVOID
*)(exp
+ 1);
134 names
= (LPSTR
*)(funcs
+ descr
->nb_funcs
);
135 pfwd
= (LPSTR
)(names
+ descr
->nb_names
);
136 xcnlnk
= pfwd
+ descr
->fwd_size
;
137 rtab
= xcnlnk
+ xcnsize
;
138 debug
= (DEBUG_ENTRY_POINT
*)(rtab
+ (rsrc
? rsrc
->restabsize
: 0));
140 /* Build the DOS and NT headers */
142 dos
->e_magic
= IMAGE_DOS_SIGNATURE
;
143 dos
->e_lfanew
= sizeof(*dos
);
145 nt
->Signature
= IMAGE_NT_SIGNATURE
;
146 nt
->FileHeader
.Machine
= IMAGE_FILE_MACHINE_I386
;
147 nt
->FileHeader
.NumberOfSections
= nb_sections
;
148 nt
->FileHeader
.SizeOfOptionalHeader
= sizeof(nt
->OptionalHeader
);
149 nt
->FileHeader
.Characteristics
= descr
->characteristics
;
151 nt
->OptionalHeader
.Magic
= IMAGE_NT_OPTIONAL_HDR_MAGIC
;
152 nt
->OptionalHeader
.SizeOfCode
= 0x1000;
153 nt
->OptionalHeader
.SizeOfInitializedData
= 0;
154 nt
->OptionalHeader
.SizeOfUninitializedData
= 0;
155 nt
->OptionalHeader
.ImageBase
= (DWORD
)addr
;
156 nt
->OptionalHeader
.SectionAlignment
= 0x1000;
157 nt
->OptionalHeader
.FileAlignment
= 0x1000;
158 nt
->OptionalHeader
.MajorOperatingSystemVersion
= 1;
159 nt
->OptionalHeader
.MinorOperatingSystemVersion
= 0;
160 nt
->OptionalHeader
.MajorSubsystemVersion
= 4;
161 nt
->OptionalHeader
.MinorSubsystemVersion
= 0;
162 nt
->OptionalHeader
.SizeOfImage
= size
;
163 nt
->OptionalHeader
.SizeOfHeaders
= (BYTE
*)exp
- addr
;
164 nt
->OptionalHeader
.NumberOfRvaAndSizes
= IMAGE_NUMBEROF_DIRECTORY_ENTRIES
;
165 if (descr
->dllentrypoint
)
166 nt
->OptionalHeader
.AddressOfEntryPoint
= (DWORD
)descr
->dllentrypoint
- (DWORD
)addr
;
168 /* Build the code section */
170 strcpy( sec
->Name
, ".code" );
171 sec
->SizeOfRawData
= 0;
173 if (WARN_ON(relay
) || TRACE_ON(relay
))
174 sec
->SizeOfRawData
+= descr
->nb_funcs
* sizeof(DEBUG_ENTRY_POINT
);
176 sec
->Misc
.VirtualSize
= sec
->SizeOfRawData
;
177 sec
->VirtualAddress
= (BYTE
*)debug
- addr
;
178 sec
->PointerToRawData
= (BYTE
*)debug
- addr
;
179 sec
->Characteristics
= (IMAGE_SCN_CNT_INITIALIZED_DATA
|
180 IMAGE_SCN_MEM_EXECUTE
| IMAGE_SCN_MEM_READ
);
183 /* Build the import directory */
185 if (descr
->nb_imports
)
187 dir
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_FILE_IMPORT_DIRECTORY
];
188 dir
->VirtualAddress
= (BYTE
*)imp
- addr
;
189 dir
->Size
= sizeof(*imp
) * (descr
->nb_imports
+ 1);
191 /* Build the imports section */
192 strcpy( sec
->Name
, ".idata" );
193 sec
->Misc
.VirtualSize
= dir
->Size
;
194 sec
->VirtualAddress
= (BYTE
*)imp
- addr
;
195 sec
->SizeOfRawData
= dir
->Size
;
196 sec
->PointerToRawData
= (BYTE
*)imp
- addr
;
197 sec
->Characteristics
= (IMAGE_SCN_CNT_INITIALIZED_DATA
|
198 IMAGE_SCN_MEM_EXECUTE
| IMAGE_SCN_MEM_READ
|
199 IMAGE_SCN_MEM_WRITE
);
202 /* Build the imports */
203 for (i
= 0; i
< descr
->nb_imports
; i
++)
205 imp
[i
].u
.Characteristics
= 0;
206 imp
[i
].ForwarderChain
= -1;
207 imp
[i
].Name
= (BYTE
*)descr
->imports
[i
] - addr
;
208 /* hack: make first thunk point to some zero value */
209 imp
[i
].FirstThunk
= (PIMAGE_THUNK_DATA
)((BYTE
*)&imp
[i
].u
.Characteristics
- addr
);
213 /* Build the export directory */
215 dir
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_FILE_EXPORT_DIRECTORY
];
216 dir
->VirtualAddress
= (BYTE
*)exp
- addr
;
217 dir
->Size
= sizeof(*exp
)
218 + descr
->nb_funcs
* sizeof(LPVOID
)
219 + descr
->nb_names
* sizeof(LPSTR
)
222 /* Build the exports section */
224 strcpy( sec
->Name
, ".edata" );
225 sec
->Misc
.VirtualSize
= dir
->Size
;
226 sec
->VirtualAddress
= (BYTE
*)exp
- addr
;
227 sec
->SizeOfRawData
= dir
->Size
;
228 sec
->PointerToRawData
= (BYTE
*)exp
- addr
;
229 sec
->Characteristics
= (IMAGE_SCN_CNT_INITIALIZED_DATA
|
230 IMAGE_SCN_MEM_EXECUTE
| IMAGE_SCN_MEM_READ
|
231 IMAGE_SCN_MEM_WRITE
);
233 /* Build Wine's .so link section. Those sections are used by the wine debugger to
234 * link a builtin PE header with the corresponding ELF module (from either a
235 * shared library, or the main executable - wine emulator or any winelib program
240 strcpy( sec
->Name
, ".xcnlnk" );
241 sec
->Misc
.VirtualSize
= xcnsize
;
242 sec
->VirtualAddress
= (BYTE
*)xcnlnk
- addr
;
243 sec
->SizeOfRawData
= sec
->Misc
.VirtualSize
;
244 sec
->PointerToRawData
= (BYTE
*)xcnlnk
- addr
;
245 sec
->Characteristics
= (IMAGE_SCN_CNT_INITIALIZED_DATA
| IMAGE_SCN_MEM_READ
);
247 *(const char**)xcnlnk
= argv0
;
250 /* Build the resource directory */
254 IMAGE_RESOURCE_DATA_ENTRY
*rdep
;
257 * The resource directory has to be copied because it contains
258 * RVAs. These would be invalid if the dll is instantiated twice.
260 memcpy(rtab
, rsrc
->restab
, rsrc
->restabsize
);
262 dir
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_FILE_RESOURCE_DIRECTORY
];
263 dir
->VirtualAddress
= (BYTE
*)rtab
- addr
;
264 dir
->Size
= rsrc
->restabsize
;
265 rdep
= (IMAGE_RESOURCE_DATA_ENTRY
*)((DWORD
)rtab
+ (DWORD
)rsrc
->entries
- (DWORD
)rsrc
->restab
);
266 for(i
= 0; i
< rsrc
->nresources
; i
++)
268 rdep
[i
].OffsetToData
+= (DWORD
)rsrc
->restab
- (DWORD
)addr
;
272 /* Build the exports section data */
274 exp
->Name
= ((BYTE
*)descr
->name
) - addr
; /*??*/
275 exp
->Base
= descr
->base
;
276 exp
->NumberOfFunctions
= descr
->nb_funcs
;
277 exp
->NumberOfNames
= descr
->nb_names
;
278 exp
->AddressOfFunctions
= (LPDWORD
*)((BYTE
*)funcs
- addr
);
279 exp
->AddressOfNames
= (LPDWORD
*)((BYTE
*)names
- addr
);
280 exp
->AddressOfNameOrdinals
= (LPWORD
*)((BYTE
*)descr
->ordinals
- addr
);
282 /* Build the funcs table */
284 for (i
= 0; i
< descr
->nb_funcs
; i
++, funcs
++, debug
++)
286 BYTE args
= descr
->args
[i
];
289 if (!descr
->functions
[i
]) continue;
291 if (args
== 0xfd) /* forward func */
293 strcpy( pfwd
, (LPSTR
)descr
->functions
[i
] );
294 *funcs
= (LPVOID
)((BYTE
*)pfwd
- addr
);
295 pfwd
+= strlen(pfwd
) + 1;
297 else *funcs
= (LPVOID
)((BYTE
*)descr
->functions
[i
] - addr
);
300 if (!(WARN_ON(relay
) || TRACE_ON(relay
))) continue;
301 for (j
=0;j
<descr
->nb_names
;j
++)
302 if (descr
->ordinals
[j
] == i
)
304 if (j
<descr
->nb_names
) {
305 if (descr
->names
[j
]) {
307 sprintf(buffer
,"%s.%d: %s",descr
->name
,i
,descr
->names
[j
]);
308 if (!RELAY_ShowDebugmsgRelay(buffer
))
314 case 0xfd: /* forward */
315 case 0xff: /* stub or extern */
317 default: /* normal function (stdcall or cdecl or register) */
318 if (TRACE_ON(relay
)) {
319 debug
->call
= 0xe8; /* lcall relative */
320 if (args
& 0x40) /* register func */
321 debug
->callfrom32
= (DWORD
)RELAY_CallFrom32Regs
-
324 debug
->callfrom32
= (DWORD
)RELAY_CallFrom32
-
327 debug
->call
= 0xe9; /* ljmp relative */
328 debug
->callfrom32
= (DWORD
)descr
->functions
[i
] -
331 debug
->ret
= (args
& 0x80) ? 0xc3 : 0xc2; /*ret/ret $n*/
332 debug
->args
= (args
& 0x3f) * sizeof(int);
333 *funcs
= (LPVOID
)((BYTE
*)debug
- addr
);
336 #endif /* __i386__ */
339 /* Build the names table */
341 for (i
= 0; i
< exp
->NumberOfNames
; i
++, names
++)
343 *names
= (LPSTR
)((BYTE
*)descr
->names
[i
] - addr
);
345 return (HMODULE
)addr
;
348 /***********************************************************************
349 * BUILTIN32_LoadLibraryExA
351 * Partly copied from the original PE_ version.
354 WINE_MODREF
*BUILTIN32_LoadLibraryExA(LPCSTR path
, DWORD flags
)
356 struct load_dll_request
*req
= get_req_buffer();
360 char dllname
[MAX_PATH
], *p
;
363 /* Fix the name in case we have a full path and extension */
364 if ((p
= strrchr( path
, '\\' ))) path
= p
+ 1;
365 lstrcpynA( dllname
, path
, sizeof(dllname
) );
367 p
= strrchr( dllname
, '.' );
368 if (!p
) strcat( dllname
, ".dll" );
370 /* Search built-in descriptor */
371 for (i
= 0; i
< nb_dlls
; i
++)
372 if (!lstrcmpiA( builtin_dlls
[i
]->filename
, dllname
)) break;
376 SetLastError( ERROR_FILE_NOT_FOUND
);
380 /* Load built-in module */
383 if (!(dll_modules
[i
] = BUILTIN32_DoLoadImage( builtin_dlls
[i
] ))) return NULL
;
385 else BUILTIN32_WarnSecondInstance( builtin_dlls
[i
]->name
);
387 /* Create 16-bit dummy module */
388 if ((hModule16
= MODULE_CreateDummyModule( dllname
, 0 )) < 32)
390 SetLastError( (DWORD
)hModule16
);
391 return NULL
; /* FIXME: Should unload the builtin module */
394 pModule
= (NE_MODULE
*)GlobalLock16( hModule16
);
395 pModule
->flags
= NE_FFLAGS_LIBMODULE
| NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_WIN32
| NE_FFLAGS_BUILTIN
;
396 pModule
->module32
= dll_modules
[i
];
398 /* Create 32-bit MODREF */
399 if ( !(wm
= PE_CreateModule( pModule
->module32
, dllname
, flags
, TRUE
)) )
401 ERR( "can't load %s\n", path
);
402 FreeLibrary16( hModule16
); /* FIXME: Should unload the builtin module */
403 SetLastError( ERROR_OUTOFMEMORY
);
407 if (wm
->binfmt
.pe
.pe_export
)
408 SNOOP_RegisterDLL(wm
->module
,wm
->modname
,wm
->binfmt
.pe
.pe_export
->NumberOfFunctions
);
411 req
->base
= (void *)pModule
->module32
;
414 req
->name
= &wm
->modname
;
415 server_call_noerr( REQ_LOAD_DLL
);
419 /***********************************************************************
420 * BUILTIN32_LoadExeModule
422 HMODULE16
BUILTIN32_LoadExeModule( void )
428 /* Search built-in EXE descriptor */
429 for ( i
= 0; i
< nb_dlls
; i
++ )
430 if ( !(builtin_dlls
[i
]->characteristics
& IMAGE_FILE_DLL
) )
434 MESSAGE( "More than one built-in EXE module loaded!\n" );
443 MESSAGE( "No built-in EXE module loaded! Did you create a .spec file?\n" );
447 /* Load built-in module */
448 if ( !dll_modules
[exe
] )
449 if ( !(dll_modules
[exe
] = BUILTIN32_DoLoadImage( builtin_dlls
[exe
] )) )
452 /* Create 16-bit dummy module */
453 hModule16
= MODULE_CreateDummyModule( builtin_dlls
[exe
]->filename
, 0 );
454 if ( hModule16
< 32 ) return 0;
455 pModule
= (NE_MODULE
*)GlobalLock16( hModule16
);
456 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_WIN32
| NE_FFLAGS_BUILTIN
;
457 pModule
->module32
= dll_modules
[exe
];
463 /***********************************************************************
464 * BUILTIN32_UnloadLibrary
466 * Unload the built-in library and free the modref.
468 void BUILTIN32_UnloadLibrary(WINE_MODREF
*wm
)
470 /* FIXME: do something here */
474 /***********************************************************************
475 * BUILTIN32_GetEntryPoint
477 * Return the name of the DLL entry point corresponding
478 * to a relay entry point address. This is used only by relay debugging.
480 * This function _must_ return the real entry point to call
481 * after the debug info is printed.
483 ENTRYPOINT32
BUILTIN32_GetEntryPoint( char *buffer
, void *relay
,
484 unsigned int *typemask
)
486 const BUILTIN32_DESCRIPTOR
*descr
= NULL
;
489 /* First find the module */
491 for (i
= 0; i
< nb_dlls
; i
++)
494 IMAGE_SECTION_HEADER
*sec
= PE_SECTIONS(dll_modules
[i
]);
495 DEBUG_ENTRY_POINT
*debug
=
496 (DEBUG_ENTRY_POINT
*)((DWORD
)dll_modules
[i
] + sec
[0].VirtualAddress
);
497 DEBUG_ENTRY_POINT
*func
= (DEBUG_ENTRY_POINT
*)relay
;
498 descr
= builtin_dlls
[i
];
499 if (debug
<= func
&& func
< debug
+ descr
->nb_funcs
)
501 ordinal
= func
- debug
;
506 if (!descr
) return NULL
;
508 /* Now find the function */
510 for (i
= 0; i
< descr
->nb_names
; i
++)
511 if (descr
->ordinals
[i
] == ordinal
) break;
513 sprintf( buffer
, "%s.%d: %s", descr
->name
, ordinal
+ descr
->base
,
514 (i
< descr
->nb_names
) ? descr
->names
[i
] : "@" );
515 *typemask
= descr
->argtypes
[ordinal
];
516 return descr
->functions
[ordinal
];
519 /***********************************************************************
520 * BUILTIN32_SwitchRelayDebug
522 * FIXME: enhance to do it module relative.
524 void BUILTIN32_SwitchRelayDebug(BOOL onoff
)
526 const BUILTIN32_DESCRIPTOR
*descr
;
527 IMAGE_SECTION_HEADER
*sec
;
528 DEBUG_ENTRY_POINT
*debug
;
532 if (!(TRACE_ON(relay
) || WARN_ON(relay
)))
534 for (j
= 0; j
< nb_dlls
; j
++)
536 if (!dll_modules
[j
]) continue;
537 sec
= PE_SECTIONS(dll_modules
[j
]);
538 debug
= (DEBUG_ENTRY_POINT
*)((DWORD
)dll_modules
[j
] + sec
[1].VirtualAddress
);
539 descr
= builtin_dlls
[j
];
540 for (i
= 0; i
< descr
->nb_funcs
; i
++,debug
++) {
541 if (!descr
->functions
[i
]) continue;
542 if ((descr
->args
[i
]==0xff) || (descr
->args
[i
]==0xfe))
545 debug
->call
= 0xe8; /* lcall relative */
546 debug
->callfrom32
= (DWORD
)RELAY_CallFrom32
-
549 debug
->call
= 0xe9; /* ljmp relative */
550 debug
->callfrom32
= (DWORD
)descr
->functions
[i
] -
555 #endif /* __i386__ */
559 /***********************************************************************
560 * BUILTIN32_RegisterDLL
562 * Register a built-in DLL descriptor.
564 void BUILTIN32_RegisterDLL( const BUILTIN32_DESCRIPTOR
*descr
)
566 assert( nb_dlls
< MAX_DLLS
);
567 builtin_dlls
[nb_dlls
++] = descr
;
570 /***********************************************************************
571 * BUILTIN32_Unimplemented
573 * This function is called for unimplemented 32-bit entry points (declared
574 * as 'stub' in the spec file).
576 void BUILTIN32_Unimplemented( const BUILTIN32_DESCRIPTOR
*descr
, int ordinal
)
578 const char *func_name
= "???";
581 __RESTORE_ES
; /* Just in case */
583 for (i
= 0; i
< descr
->nb_names
; i
++)
584 if (descr
->ordinals
[i
] + descr
->base
== ordinal
) break;
585 if (i
< descr
->nb_names
) func_name
= descr
->names
[i
];
587 MESSAGE( "No handler for Win32 routine %s.%d: %s",
588 descr
->name
, ordinal
, func_name
);
590 MESSAGE( " (called from %p)", __builtin_return_address(1) );