2 * Win32 builtin functions
4 * Copyright 1997 Alexandre Julliard
16 #include <sys/types.h>
17 #ifdef HAVE_SYS_MMAN_H
22 #include "wine/winbase16.h"
25 #include "builtin32.h"
35 #include "debugtools.h"
36 #include "options.h" /* for argv0 */
38 DEFAULT_DEBUG_CHANNEL(module
);
39 DECLARE_DEBUG_CHANNEL(relay
);
46 IMAGE_RESOURCE_DATA_ENTRY
*entries
;
51 static const BUILTIN32_DESCRIPTOR
*builtin_dlls
[MAX_DLLS
];
52 static HMODULE dll_modules
[MAX_DLLS
];
56 /***********************************************************************
57 * BUILTIN32_WarnSecondInstance
59 * Emit a warning when we are creating a second instance for a DLL
60 * that is known to not support this.
62 static void BUILTIN32_WarnSecondInstance( const char *name
)
64 static const char * const warning_list
[] =
65 { "comctl32.dll", "comdlg32.dll", "crtdll.dll",
66 "imagehlp.dll", "msacm32.dll", "shell32.dll", NULL
};
68 const char * const *ptr
= warning_list
;
72 if (!strcasecmp( *ptr
, name
))
74 ERR( "Attempt to instantiate built-in dll '%s' twice "
75 "in the same address space. Expect trouble!\n", name
);
82 /***********************************************************************
85 void *BUILTIN32_dlopen( const char *name
)
90 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
91 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
92 sprintf( buffer
, "lib%s", name
);
93 for (p
= buffer
; *p
; p
++) *p
= tolower(*p
);
94 if ((p
= strrchr( buffer
, '.' )) && (!strcmp( p
, ".dll" ) || !strcmp( p
, ".exe" ))) *p
= 0;
95 strcat( buffer
, ".so" );
97 if (!(handle
= ELFDLL_dlopen( buffer
, RTLD_NOW
)))
98 ERR( "failed to load %s: %s\n", buffer
, dlerror() );
105 /***********************************************************************
108 int BUILTIN32_dlclose( void *handle
)
111 /* FIXME: should unregister descriptors first */
112 /* return dlclose( handle ); */
118 /***********************************************************************
121 * Adjust an array of pointers to make them into RVAs.
123 static inline void fixup_rva_ptrs( void *array
, void *base
, int count
)
125 void **ptr
= (void **)array
;
128 if (*ptr
) *ptr
= (void *)((char *)*ptr
- (char *)base
);
134 /***********************************************************************
135 * BUILTIN32_DoLoadImage
137 * Load a built-in Win32 module. Helper function for BUILTIN32_LoadImage.
139 static HMODULE
BUILTIN32_DoLoadImage( const BUILTIN32_DESCRIPTOR
*descr
)
141 IMAGE_DATA_DIRECTORY
*dir
;
142 IMAGE_DOS_HEADER
*dos
;
143 IMAGE_NT_HEADERS
*nt
;
144 IMAGE_SECTION_HEADER
*sec
;
145 IMAGE_IMPORT_DESCRIPTOR
*imp
;
146 IMAGE_EXPORT_DIRECTORY
*exports
= descr
->exports
;
147 INT i
, size
, nb_sections
;
148 BYTE
*addr
, *code_start
, *data_start
;
151 int page_size
= VIRTUAL_GetPageSize();
153 /* Allocate the module */
155 nb_sections
= 2; /* code + data */
157 if (!strcmp(descr
->filename
, "kernel32.dll")) {
159 xcnsize
= sizeof(DWORD
);
161 size
= (sizeof(IMAGE_DOS_HEADER
)
162 + sizeof(IMAGE_NT_HEADERS
)
163 + nb_sections
* sizeof(IMAGE_SECTION_HEADER
)
164 + (descr
->nb_imports
+1) * sizeof(IMAGE_IMPORT_DESCRIPTOR
)
167 assert( size
<= page_size
);
169 if (descr
->pe_header
)
171 if ((addr
= FILE_dommap( -1, descr
->pe_header
, 0, page_size
, 0, 0,
172 PROT_READ
|PROT_WRITE
, MAP_FIXED
)) != descr
->pe_header
)
174 ERR("failed to map over PE header for %s at %p\n", descr
->filename
, descr
->pe_header
);
180 if (!(addr
= VirtualAlloc( NULL
, page_size
, MEM_COMMIT
, PAGE_READWRITE
))) return 0;
183 dos
= (IMAGE_DOS_HEADER
*)addr
;
184 nt
= (IMAGE_NT_HEADERS
*)(dos
+ 1);
185 sec
= (IMAGE_SECTION_HEADER
*)(nt
+ 1);
186 imp
= (IMAGE_IMPORT_DESCRIPTOR
*)(sec
+ nb_sections
);
187 xcnlnk
= (char *)(imp
+ descr
->nb_imports
+ 1);
188 code_start
= addr
+ page_size
;
191 data_start
= code_start
+ page_size
;
193 /* Build the DOS and NT headers */
195 dos
->e_magic
= IMAGE_DOS_SIGNATURE
;
196 dos
->e_lfanew
= sizeof(*dos
);
198 nt
->Signature
= IMAGE_NT_SIGNATURE
;
199 nt
->FileHeader
.Machine
= IMAGE_FILE_MACHINE_I386
;
200 nt
->FileHeader
.NumberOfSections
= nb_sections
;
201 nt
->FileHeader
.SizeOfOptionalHeader
= sizeof(nt
->OptionalHeader
);
202 nt
->FileHeader
.Characteristics
= descr
->characteristics
;
204 nt
->OptionalHeader
.Magic
= IMAGE_NT_OPTIONAL_HDR_MAGIC
;
205 nt
->OptionalHeader
.SizeOfCode
= data_start
- code_start
;
206 nt
->OptionalHeader
.SizeOfInitializedData
= 0;
207 nt
->OptionalHeader
.SizeOfUninitializedData
= 0;
208 nt
->OptionalHeader
.ImageBase
= (DWORD
)addr
;
209 nt
->OptionalHeader
.SectionAlignment
= page_size
;
210 nt
->OptionalHeader
.FileAlignment
= page_size
;
211 nt
->OptionalHeader
.MajorOperatingSystemVersion
= 1;
212 nt
->OptionalHeader
.MinorOperatingSystemVersion
= 0;
213 nt
->OptionalHeader
.MajorSubsystemVersion
= 4;
214 nt
->OptionalHeader
.MinorSubsystemVersion
= 0;
215 nt
->OptionalHeader
.SizeOfImage
= page_size
;
216 nt
->OptionalHeader
.SizeOfHeaders
= page_size
;
217 nt
->OptionalHeader
.NumberOfRvaAndSizes
= IMAGE_NUMBEROF_DIRECTORY_ENTRIES
;
218 if (descr
->dllentrypoint
)
219 nt
->OptionalHeader
.AddressOfEntryPoint
= (DWORD
)descr
->dllentrypoint
- (DWORD
)addr
;
221 /* Build the code section */
223 strcpy( sec
->Name
, ".text" );
224 sec
->SizeOfRawData
= data_start
- code_start
;
225 sec
->Misc
.VirtualSize
= sec
->SizeOfRawData
;
226 sec
->VirtualAddress
= code_start
- addr
;
227 sec
->PointerToRawData
= code_start
- addr
;
228 sec
->Characteristics
= (IMAGE_SCN_CNT_CODE
| IMAGE_SCN_MEM_EXECUTE
| IMAGE_SCN_MEM_READ
);
231 /* Build the data section */
233 strcpy( sec
->Name
, ".data" );
234 sec
->SizeOfRawData
= 0;
235 sec
->Misc
.VirtualSize
= sec
->SizeOfRawData
;
236 sec
->VirtualAddress
= data_start
- addr
;
237 sec
->PointerToRawData
= data_start
- addr
;
238 sec
->Characteristics
= (IMAGE_SCN_CNT_INITIALIZED_DATA
|
239 IMAGE_SCN_MEM_WRITE
| IMAGE_SCN_MEM_READ
);
242 /* Build the import directory */
244 if (descr
->nb_imports
)
246 dir
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_FILE_IMPORT_DIRECTORY
];
247 dir
->VirtualAddress
= (BYTE
*)imp
- addr
;
248 dir
->Size
= sizeof(*imp
) * (descr
->nb_imports
+ 1);
250 /* Build the imports */
251 for (i
= 0; i
< descr
->nb_imports
; i
++)
253 imp
[i
].u
.Characteristics
= 0;
254 imp
[i
].ForwarderChain
= -1;
255 imp
[i
].Name
= (BYTE
*)descr
->imports
[i
] - addr
;
256 /* hack: make first thunk point to some zero value */
257 imp
[i
].FirstThunk
= (PIMAGE_THUNK_DATA
)((BYTE
*)&imp
[i
].u
.Characteristics
- addr
);
261 /* Build Wine's .so link section. Those sections are used by the wine debugger to
262 * link a builtin PE header with the corresponding ELF module (from either a
263 * shared library, or the main executable - wine emulator or any winelib program
267 strcpy( sec
->Name
, ".xcnlnk" );
268 sec
->Misc
.VirtualSize
= xcnsize
;
269 sec
->VirtualAddress
= (BYTE
*)xcnlnk
- addr
;
270 sec
->SizeOfRawData
= sec
->Misc
.VirtualSize
;
271 sec
->PointerToRawData
= (BYTE
*)xcnlnk
- addr
;
272 sec
->Characteristics
= (IMAGE_SCN_CNT_INITIALIZED_DATA
| IMAGE_SCN_MEM_READ
);
275 *(const char**)xcnlnk
= argv0
;
278 /* Build the resource directory */
282 BUILTIN32_RESOURCE
*rsrc
= descr
->rsrc
;
283 IMAGE_RESOURCE_DATA_ENTRY
*rdep
;
284 dir
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_FILE_RESOURCE_DIRECTORY
];
285 dir
->VirtualAddress
= (BYTE
*)rsrc
->restab
- addr
;
286 dir
->Size
= rsrc
->restabsize
;
287 rdep
= rsrc
->entries
;
288 for (i
= 0; i
< rsrc
->nresources
; i
++) rdep
[i
].OffsetToData
+= dir
->VirtualAddress
;
291 /* Build the export directory */
295 dir
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_FILE_EXPORT_DIRECTORY
];
296 dir
->VirtualAddress
= (BYTE
*)exports
- addr
;
297 dir
->Size
= descr
->exports_size
;
299 fixup_rva_ptrs( (void *)exports
->AddressOfFunctions
, addr
, exports
->NumberOfFunctions
);
300 fixup_rva_ptrs( (void *)exports
->AddressOfNames
, addr
, exports
->NumberOfNames
);
301 fixup_rva_ptrs( &exports
->Name
, addr
, 1 );
302 fixup_rva_ptrs( &exports
->AddressOfFunctions
, addr
, 1 );
303 fixup_rva_ptrs( &exports
->AddressOfNames
, addr
, 1 );
304 fixup_rva_ptrs( &exports
->AddressOfNameOrdinals
, addr
, 1 );
306 /* Setup relay debugging entry points */
307 if (WARN_ON(relay
) || TRACE_ON(relay
)) RELAY_SetupDLL( addr
);
310 return (HMODULE
)addr
;
313 /***********************************************************************
314 * BUILTIN32_LoadLibraryExA
316 * Partly copied from the original PE_ version.
319 WINE_MODREF
*BUILTIN32_LoadLibraryExA(LPCSTR path
, DWORD flags
)
321 struct load_dll_request
*req
= get_req_buffer();
325 char dllname
[MAX_PATH
], *p
;
329 /* Fix the name in case we have a full path and extension */
330 if ((p
= strrchr( path
, '\\' ))) path
= p
+ 1;
331 lstrcpynA( dllname
, path
, sizeof(dllname
) );
333 p
= strrchr( dllname
, '.' );
334 if (!p
) strcat( dllname
, ".dll" );
336 /* Search built-in descriptor */
337 for (i
= 0; i
< nb_dlls
; i
++)
338 if (!strcasecmp( builtin_dlls
[i
]->filename
, dllname
)) goto found
;
340 if ((handle
= BUILTIN32_dlopen( dllname
)))
342 for (i
= 0; i
< nb_dlls
; i
++)
343 if (!strcasecmp( builtin_dlls
[i
]->filename
, dllname
)) goto found
;
344 ERR( "loaded .so but dll %s still not found\n", dllname
);
345 BUILTIN32_dlclose( handle
);
348 SetLastError( ERROR_FILE_NOT_FOUND
);
352 /* Load built-in module */
355 if (!(dll_modules
[i
] = BUILTIN32_DoLoadImage( builtin_dlls
[i
] ))) return NULL
;
357 else BUILTIN32_WarnSecondInstance( builtin_dlls
[i
]->filename
);
359 /* Create 16-bit dummy module */
360 if ((hModule16
= MODULE_CreateDummyModule( dllname
, dll_modules
[i
] )) < 32)
362 SetLastError( (DWORD
)hModule16
);
363 return NULL
; /* FIXME: Should unload the builtin module */
365 pModule
= (NE_MODULE
*)GlobalLock16( hModule16
);
366 pModule
->flags
= NE_FFLAGS_LIBMODULE
| NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_WIN32
| NE_FFLAGS_BUILTIN
;
368 /* Create 32-bit MODREF */
369 if ( !(wm
= PE_CreateModule( pModule
->module32
, dllname
, flags
, TRUE
)) )
371 ERR( "can't load %s\n", path
);
372 FreeLibrary16( hModule16
); /* FIXME: Should unload the builtin module */
373 SetLastError( ERROR_OUTOFMEMORY
);
377 if (wm
->binfmt
.pe
.pe_export
)
378 SNOOP_RegisterDLL(wm
->module
,wm
->modname
,wm
->binfmt
.pe
.pe_export
->NumberOfFunctions
);
381 req
->base
= (void *)pModule
->module32
;
384 req
->name
= &wm
->modname
;
385 server_call_noerr( REQ_LOAD_DLL
);
389 /***********************************************************************
390 * BUILTIN32_LoadExeModule
392 HMODULE
BUILTIN32_LoadExeModule( LPCSTR
*filename
)
396 /* Search built-in EXE descriptor */
397 for ( i
= 0; i
< nb_dlls
; i
++ )
398 if ( !(builtin_dlls
[i
]->characteristics
& IMAGE_FILE_DLL
) )
402 MESSAGE( "More than one built-in EXE module loaded!\n" );
411 MESSAGE( "No built-in EXE module loaded! Did you create a .spec file?\n" );
415 /* Load built-in module */
416 if ( !dll_modules
[exe
] )
417 if ( !(dll_modules
[exe
] = BUILTIN32_DoLoadImage( builtin_dlls
[exe
] )) )
420 *filename
= builtin_dlls
[exe
]->filename
;
421 return dll_modules
[exe
];
425 /***********************************************************************
426 * BUILTIN32_UnloadLibrary
428 * Unload the built-in library and free the modref.
430 void BUILTIN32_UnloadLibrary(WINE_MODREF
*wm
)
432 /* FIXME: do something here */
435 /***********************************************************************
436 * BUILTIN32_RegisterDLL
438 * Register a built-in DLL descriptor.
440 void BUILTIN32_RegisterDLL( const BUILTIN32_DESCRIPTOR
*descr
)
442 assert( nb_dlls
< MAX_DLLS
);
443 builtin_dlls
[nb_dlls
++] = descr
;
446 /***********************************************************************
447 * BUILTIN32_Unimplemented
449 * This function is called for unimplemented 32-bit entry points (declared
450 * as 'stub' in the spec file).
452 void BUILTIN32_Unimplemented( const char *dllname
, const char *funcname
)
454 __RESTORE_ES
; /* Just in case */
456 MESSAGE( "No handler for Win32 routine %s.%s", dllname
, funcname
);
458 MESSAGE( " (called from %p)", __builtin_return_address(1) );