blob: 4d26789f8e14eb4b29df09bc1e94e7fa7616b669 [file] [log] [blame]
Alexandre Julliard44ed71f1997-12-21 19:17:50 +00001/*
2 * Win32 builtin functions
3 *
4 * Copyright 1997 Alexandre Julliard
5 */
6
Alexandre Julliarde1e75372000-04-24 17:17:49 +00007#include "config.h"
8
Alexandre Julliard44ed71f1997-12-21 19:17:50 +00009#include <assert.h>
Alexandre Julliard44ed71f1997-12-21 19:17:50 +000010#include <string.h>
Jeremy Whited3e22d92000-02-10 19:03:02 +000011#include <stdio.h>
Marcus Meissner317af321999-02-17 13:51:06 +000012#include <ctype.h>
Alexandre Julliarde1e75372000-04-24 17:17:49 +000013#ifdef HAVE_DL_API
14#include <dlfcn.h>
15#endif
Alexandre Julliard246c3602000-05-10 03:48:00 +000016#ifdef HAVE_SYS_MMAN_H
17#include <sys/mman.h>
18#endif
Alexandre Julliarde1e75372000-04-24 17:17:49 +000019
Jeremy Whited3e22d92000-02-10 19:03:02 +000020#include "windef.h"
Alexandre Julliard680919c2000-05-07 18:39:28 +000021#include "wine/winbase16.h"
Jeremy Whited3e22d92000-02-10 19:03:02 +000022#include "wingdi.h"
Michael Vekslerf935c591999-02-09 15:49:39 +000023#include "winuser.h"
Alexandre Julliard44ed71f1997-12-21 19:17:50 +000024#include "builtin32.h"
Alexandre Julliarde1e75372000-04-24 17:17:49 +000025#include "elfdll.h"
Alexandre Julliard246c3602000-05-10 03:48:00 +000026#include "file.h"
27#include "global.h"
Marcus Meissner317af321999-02-17 13:51:06 +000028#include "neexe.h"
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000029#include "heap.h"
Alexandre Julliard638f1691999-01-17 16:32:32 +000030#include "main.h"
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +000031#include "snoop.h"
32#include "winerror.h"
Alexandre Julliard05f0b712000-03-09 18:18:41 +000033#include "server.h"
Alexandre Julliard06c275a1999-05-02 14:32:27 +000034#include "debugtools.h"
Eric Pouech87031a42000-04-13 19:27:52 +000035#include "options.h" /* for argv0 */
Alexandre Julliard44ed71f1997-12-21 19:17:50 +000036
Alexandre Julliard74e4a282000-01-09 04:20:31 +000037DEFAULT_DEBUG_CHANNEL(module);
38DECLARE_DEBUG_CHANNEL(relay);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000039
Alexandre Julliard44ed71f1997-12-21 19:17:50 +000040typedef struct
41{
Alexandre Julliard246c3602000-05-10 03:48:00 +000042 BYTE *restab;
43 DWORD nresources;
44 DWORD restabsize;
45 IMAGE_RESOURCE_DATA_ENTRY *entries;
Bertho Stultiensd1895a71999-04-25 18:31:35 +000046} BUILTIN32_RESOURCE;
47
Alexandre Julliard112d3072000-01-16 03:37:05 +000048#define MAX_DLLS 60
Alexandre Julliard44ed71f1997-12-21 19:17:50 +000049
Alexandre Julliard112d3072000-01-16 03:37:05 +000050static const BUILTIN32_DESCRIPTOR *builtin_dlls[MAX_DLLS];
51static HMODULE dll_modules[MAX_DLLS];
52static int nb_dlls;
Alexandre Julliard44ed71f1997-12-21 19:17:50 +000053
Alexandre Julliard44ed71f1997-12-21 19:17:50 +000054
Alexandre Julliard74e4a282000-01-09 04:20:31 +000055/***********************************************************************
56 * BUILTIN32_WarnSecondInstance
57 *
58 * Emit a warning when we are creating a second instance for a DLL
59 * that is known to not support this.
60 */
61static void BUILTIN32_WarnSecondInstance( const char *name )
62{
63 static const char * const warning_list[] =
Alexandre Julliard246c3602000-05-10 03:48:00 +000064 { "comctl32.dll", "comdlg32.dll", "crtdll.dll",
65 "imagehlp.dll", "msacm32.dll", "shell32.dll", NULL };
Alexandre Julliard74e4a282000-01-09 04:20:31 +000066
67 const char * const *ptr = warning_list;
68
69 while (*ptr)
70 {
71 if (!strcasecmp( *ptr, name ))
72 {
73 ERR( "Attempt to instantiate built-in dll '%s' twice "
74 "in the same address space. Expect trouble!\n", name );
75 return;
76 }
77 ptr++;
78 }
79}
80
Alexandre Julliard44ed71f1997-12-21 19:17:50 +000081/***********************************************************************
Alexandre Julliarde1e75372000-04-24 17:17:49 +000082 * BUILTIN32_dlopen
83 */
84void *BUILTIN32_dlopen( const char *name )
85{
86#ifdef HAVE_DL_API
87 void *handle;
88 char buffer[128], *p;
89 if ((p = strrchr( name, '/' ))) name = p + 1;
90 if ((p = strrchr( name, '\\' ))) name = p + 1;
91 sprintf( buffer, "lib%s", name );
92 for (p = buffer; *p; p++) *p = tolower(*p);
93 if ((p = strrchr( buffer, '.' )) && (!strcmp( p, ".dll" ) || !strcmp( p, ".exe" ))) *p = 0;
94 strcat( buffer, ".so" );
95
96 if (!(handle = ELFDLL_dlopen( buffer, RTLD_NOW )))
97 ERR( "failed to load %s: %s\n", buffer, dlerror() );
98 return handle;
99#else
100 return NULL;
101#endif
102}
103
104/***********************************************************************
105 * BUILTIN32_dlclose
106 */
107int BUILTIN32_dlclose( void *handle )
108{
109#ifdef HAVE_DL_API
Alexandre Julliard246c3602000-05-10 03:48:00 +0000110 /* FIXME: should unregister descriptors first */
111 /* return dlclose( handle ); */
Alexandre Julliarde1e75372000-04-24 17:17:49 +0000112#endif
Alexandre Julliard246c3602000-05-10 03:48:00 +0000113 return 0;
Alexandre Julliarde1e75372000-04-24 17:17:49 +0000114}
115
Alexandre Julliard246c3602000-05-10 03:48:00 +0000116
117/***********************************************************************
118 * fixup_rva_ptrs
119 *
120 * Adjust an array of pointers to make them into RVAs.
121 */
122static inline void fixup_rva_ptrs( void *array, void *base, int count )
123{
124 void **ptr = (void **)array;
125 while (count--)
126 {
127 if (*ptr) *ptr = (void *)((char *)*ptr - (char *)base);
128 ptr++;
129 }
130}
131
132
Alexandre Julliarde1e75372000-04-24 17:17:49 +0000133/***********************************************************************
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000134 * BUILTIN32_DoLoadImage
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000135 *
Ulrich Weigandffa2c6f1998-12-18 15:38:15 +0000136 * Load a built-in Win32 module. Helper function for BUILTIN32_LoadImage.
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000137 */
Alexandre Julliard112d3072000-01-16 03:37:05 +0000138static HMODULE BUILTIN32_DoLoadImage( const BUILTIN32_DESCRIPTOR *descr )
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000139{
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000140 IMAGE_DATA_DIRECTORY *dir;
141 IMAGE_DOS_HEADER *dos;
142 IMAGE_NT_HEADERS *nt;
143 IMAGE_SECTION_HEADER *sec;
Alexandre Julliarde4177e61999-05-12 09:57:37 +0000144 IMAGE_IMPORT_DESCRIPTOR *imp;
Alexandre Julliard246c3602000-05-10 03:48:00 +0000145 IMAGE_EXPORT_DIRECTORY *exports = descr->exports;
Alexandre Julliarde4177e61999-05-12 09:57:37 +0000146 INT i, size, nb_sections;
Alexandre Julliard246c3602000-05-10 03:48:00 +0000147 BYTE *addr, *code_start, *data_start;
Eric Pouech87031a42000-04-13 19:27:52 +0000148 BYTE* xcnlnk;
149 DWORD xcnsize = 0;
Alexandre Julliard246c3602000-05-10 03:48:00 +0000150 int page_size = VIRTUAL_GetPageSize();
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000151
152 /* Allocate the module */
153
Alexandre Julliard246c3602000-05-10 03:48:00 +0000154 nb_sections = 2; /* code + data */
Eric Pouech87031a42000-04-13 19:27:52 +0000155
Alexandre Julliard246c3602000-05-10 03:48:00 +0000156 if (!strcmp(descr->filename, "kernel32.dll")) {
Eric Pouech87031a42000-04-13 19:27:52 +0000157 nb_sections++;
158 xcnsize = sizeof(DWORD);
159 }
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000160 size = (sizeof(IMAGE_DOS_HEADER)
161 + sizeof(IMAGE_NT_HEADERS)
Alexandre Julliarde4177e61999-05-12 09:57:37 +0000162 + nb_sections * sizeof(IMAGE_SECTION_HEADER)
Alexandre Julliard112d3072000-01-16 03:37:05 +0000163 + (descr->nb_imports+1) * sizeof(IMAGE_IMPORT_DESCRIPTOR)
Eric Pouech87031a42000-04-13 19:27:52 +0000164 + xcnsize);
165
Alexandre Julliard246c3602000-05-10 03:48:00 +0000166 assert( size <= page_size );
167
168 if (descr->pe_header)
169 {
170 if ((addr = FILE_dommap( -1, descr->pe_header, 0, page_size, 0, 0,
171 PROT_READ|PROT_WRITE, MAP_FIXED )) != descr->pe_header)
172 {
173 ERR("failed to map over PE header for %s at %p\n", descr->filename, descr->pe_header );
174 return 0;
175 }
176 }
177 else
178 {
179 if (!(addr = VirtualAlloc( NULL, page_size, MEM_COMMIT, PAGE_READWRITE ))) return 0;
180 }
181
182 dos = (IMAGE_DOS_HEADER *)addr;
183 nt = (IMAGE_NT_HEADERS *)(dos + 1);
184 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
185 imp = (IMAGE_IMPORT_DESCRIPTOR *)(sec + nb_sections);
186 xcnlnk = (char *)(imp + descr->nb_imports + 1);
187 code_start = addr + page_size;
188
189 /* HACK! */
190 data_start = code_start + page_size;
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000191
192 /* Build the DOS and NT headers */
193
194 dos->e_magic = IMAGE_DOS_SIGNATURE;
195 dos->e_lfanew = sizeof(*dos);
196
197 nt->Signature = IMAGE_NT_SIGNATURE;
198 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
Alexandre Julliarde4177e61999-05-12 09:57:37 +0000199 nt->FileHeader.NumberOfSections = nb_sections;
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000200 nt->FileHeader.SizeOfOptionalHeader = sizeof(nt->OptionalHeader);
Ulrich Weigand9ffd4032000-02-03 01:33:48 +0000201 nt->FileHeader.Characteristics = descr->characteristics;
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000202
203 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
Alexandre Julliard246c3602000-05-10 03:48:00 +0000204 nt->OptionalHeader.SizeOfCode = data_start - code_start;
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000205 nt->OptionalHeader.SizeOfInitializedData = 0;
206 nt->OptionalHeader.SizeOfUninitializedData = 0;
207 nt->OptionalHeader.ImageBase = (DWORD)addr;
Alexandre Julliard246c3602000-05-10 03:48:00 +0000208 nt->OptionalHeader.SectionAlignment = page_size;
209 nt->OptionalHeader.FileAlignment = page_size;
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000210 nt->OptionalHeader.MajorOperatingSystemVersion = 1;
211 nt->OptionalHeader.MinorOperatingSystemVersion = 0;
212 nt->OptionalHeader.MajorSubsystemVersion = 4;
213 nt->OptionalHeader.MinorSubsystemVersion = 0;
Alexandre Julliard246c3602000-05-10 03:48:00 +0000214 nt->OptionalHeader.SizeOfImage = page_size;
215 nt->OptionalHeader.SizeOfHeaders = page_size;
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000216 nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
Alexandre Julliard112d3072000-01-16 03:37:05 +0000217 if (descr->dllentrypoint)
218 nt->OptionalHeader.AddressOfEntryPoint = (DWORD)descr->dllentrypoint - (DWORD)addr;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000219
Alexandre Julliard936d6631999-05-12 16:10:53 +0000220 /* Build the code section */
221
Alexandre Julliard246c3602000-05-10 03:48:00 +0000222 strcpy( sec->Name, ".text" );
223 sec->SizeOfRawData = data_start - code_start;
Alexandre Julliard936d6631999-05-12 16:10:53 +0000224 sec->Misc.VirtualSize = sec->SizeOfRawData;
Alexandre Julliard246c3602000-05-10 03:48:00 +0000225 sec->VirtualAddress = code_start - addr;
226 sec->PointerToRawData = code_start - addr;
227 sec->Characteristics = (IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
228 sec++;
229
230 /* Build the data section */
231
232 strcpy( sec->Name, ".data" );
233 sec->SizeOfRawData = 0;
234 sec->Misc.VirtualSize = sec->SizeOfRawData;
235 sec->VirtualAddress = data_start - addr;
236 sec->PointerToRawData = data_start - addr;
Alexandre Julliard936d6631999-05-12 16:10:53 +0000237 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
Alexandre Julliard246c3602000-05-10 03:48:00 +0000238 IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ);
Alexandre Julliard936d6631999-05-12 16:10:53 +0000239 sec++;
240
Alexandre Julliarde4177e61999-05-12 09:57:37 +0000241 /* Build the import directory */
242
Alexandre Julliard112d3072000-01-16 03:37:05 +0000243 if (descr->nb_imports)
Alexandre Julliarde4177e61999-05-12 09:57:37 +0000244 {
245 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
246 dir->VirtualAddress = (BYTE *)imp - addr;
Alexandre Julliard112d3072000-01-16 03:37:05 +0000247 dir->Size = sizeof(*imp) * (descr->nb_imports + 1);
Alexandre Julliarde4177e61999-05-12 09:57:37 +0000248
Alexandre Julliarde4177e61999-05-12 09:57:37 +0000249 /* Build the imports */
Alexandre Julliard112d3072000-01-16 03:37:05 +0000250 for (i = 0; i < descr->nb_imports; i++)
Alexandre Julliarde4177e61999-05-12 09:57:37 +0000251 {
252 imp[i].u.Characteristics = 0;
253 imp[i].ForwarderChain = -1;
Alexandre Julliard112d3072000-01-16 03:37:05 +0000254 imp[i].Name = (BYTE *)descr->imports[i] - addr;
Alexandre Julliarde4177e61999-05-12 09:57:37 +0000255 /* hack: make first thunk point to some zero value */
256 imp[i].FirstThunk = (PIMAGE_THUNK_DATA)((BYTE *)&imp[i].u.Characteristics - addr);
257 }
258 }
259
Eric Pouech87031a42000-04-13 19:27:52 +0000260 /* Build Wine's .so link section. Those sections are used by the wine debugger to
261 * link a builtin PE header with the corresponding ELF module (from either a
262 * shared library, or the main executable - wine emulator or any winelib program
263 */
264 if (xcnsize)
265 {
Eric Pouech87031a42000-04-13 19:27:52 +0000266 strcpy( sec->Name, ".xcnlnk" );
267 sec->Misc.VirtualSize = xcnsize;
268 sec->VirtualAddress = (BYTE *)xcnlnk - addr;
269 sec->SizeOfRawData = sec->Misc.VirtualSize;
270 sec->PointerToRawData = (BYTE *)xcnlnk - addr;
271 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ);
Alexandre Julliard246c3602000-05-10 03:48:00 +0000272 sec++;
Eric Pouech87031a42000-04-13 19:27:52 +0000273
274 *(const char**)xcnlnk = argv0;
275 }
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000276
Bertho Stultiensd1895a71999-04-25 18:31:35 +0000277 /* Build the resource directory */
Bertho Stultiensd1895a71999-04-25 18:31:35 +0000278
Alexandre Julliard246c3602000-05-10 03:48:00 +0000279 if (descr->rsrc)
Alexandre Julliardaabef022000-03-17 15:12:06 +0000280 {
Alexandre Julliard246c3602000-05-10 03:48:00 +0000281 BUILTIN32_RESOURCE *rsrc = descr->rsrc;
Alexandre Julliardaabef022000-03-17 15:12:06 +0000282 IMAGE_RESOURCE_DATA_ENTRY *rdep;
Bertho Stultiensd1895a71999-04-25 18:31:35 +0000283 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
Alexandre Julliard246c3602000-05-10 03:48:00 +0000284 dir->VirtualAddress = (BYTE *)rsrc->restab - addr;
Alexandre Julliard74e4a282000-01-09 04:20:31 +0000285 dir->Size = rsrc->restabsize;
Alexandre Julliard246c3602000-05-10 03:48:00 +0000286 rdep = rsrc->entries;
287 for (i = 0; i < rsrc->nresources; i++) rdep[i].OffsetToData += dir->VirtualAddress;
Bertho Stultiensd1895a71999-04-25 18:31:35 +0000288 }
289
Alexandre Julliard246c3602000-05-10 03:48:00 +0000290 /* Build the export directory */
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000291
Alexandre Julliard246c3602000-05-10 03:48:00 +0000292 if (exports)
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000293 {
Alexandre Julliard246c3602000-05-10 03:48:00 +0000294 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
295 dir->VirtualAddress = (BYTE *)exports - addr;
296 dir->Size = descr->exports_size;
Marcus Meissner30c112e1998-11-14 17:06:55 +0000297
Alexandre Julliard246c3602000-05-10 03:48:00 +0000298 fixup_rva_ptrs( (void *)exports->AddressOfFunctions, addr, exports->NumberOfFunctions );
299 fixup_rva_ptrs( (void *)exports->AddressOfNames, addr, exports->NumberOfNames );
300 fixup_rva_ptrs( &exports->Name, addr, 1 );
301 fixup_rva_ptrs( &exports->AddressOfFunctions, addr, 1 );
302 fixup_rva_ptrs( &exports->AddressOfNames, addr, 1 );
303 fixup_rva_ptrs( &exports->AddressOfNameOrdinals, addr, 1 );
Alexandre Julliard3eb441c1999-05-10 14:44:47 +0000304
Alexandre Julliard246c3602000-05-10 03:48:00 +0000305 /* Setup relay debugging entry points */
306 if (WARN_ON(relay) || TRACE_ON(relay)) RELAY_SetupDLL( addr );
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000307 }
308
Alexandre Julliarda3960291999-02-26 11:11:13 +0000309 return (HMODULE)addr;
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000310}
311
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000312/***********************************************************************
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000313 * BUILTIN32_LoadLibraryExA
314 *
315 * Partly copied from the original PE_ version.
316 *
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000317 */
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000318WINE_MODREF *BUILTIN32_LoadLibraryExA(LPCSTR path, DWORD flags)
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000319{
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000320 struct load_dll_request *req = get_req_buffer();
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000321 HMODULE16 hModule16;
322 NE_MODULE *pModule;
323 WINE_MODREF *wm;
Francois Gougetbaa9bf91999-12-27 05:24:06 +0000324 char dllname[MAX_PATH], *p;
Alexandre Julliarde1e75372000-04-24 17:17:49 +0000325 void *handle;
Alexandre Julliard112d3072000-01-16 03:37:05 +0000326 int i;
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000327
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000328 /* Fix the name in case we have a full path and extension */
329 if ((p = strrchr( path, '\\' ))) path = p + 1;
330 lstrcpynA( dllname, path, sizeof(dllname) );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000331
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000332 p = strrchr( dllname, '.' );
333 if (!p) strcat( dllname, ".dll" );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000334
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000335 /* Search built-in descriptor */
Alexandre Julliard112d3072000-01-16 03:37:05 +0000336 for (i = 0; i < nb_dlls; i++)
Alexandre Julliarde1e75372000-04-24 17:17:49 +0000337 if (!strcasecmp( builtin_dlls[i]->filename, dllname )) goto found;
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000338
Alexandre Julliarde1e75372000-04-24 17:17:49 +0000339 if ((handle = BUILTIN32_dlopen( dllname )))
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000340 {
Alexandre Julliarde1e75372000-04-24 17:17:49 +0000341 for (i = 0; i < nb_dlls; i++)
342 if (!strcasecmp( builtin_dlls[i]->filename, dllname )) goto found;
343 ERR( "loaded .so but dll %s still not found\n", dllname );
344 BUILTIN32_dlclose( handle );
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000345 }
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000346
Alexandre Julliarde1e75372000-04-24 17:17:49 +0000347 SetLastError( ERROR_FILE_NOT_FOUND );
348 return NULL;
349
350 found:
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000351 /* Load built-in module */
Alexandre Julliard112d3072000-01-16 03:37:05 +0000352 if (!dll_modules[i])
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000353 {
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000354 if (!(dll_modules[i] = BUILTIN32_DoLoadImage( builtin_dlls[i] ))) return NULL;
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000355 }
Alexandre Julliard246c3602000-05-10 03:48:00 +0000356 else BUILTIN32_WarnSecondInstance( builtin_dlls[i]->filename );
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000357
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000358 /* Create 16-bit dummy module */
Alexandre Julliardb4459522000-04-15 21:00:55 +0000359 if ((hModule16 = MODULE_CreateDummyModule( dllname, dll_modules[i] )) < 32)
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000360 {
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000361 SetLastError( (DWORD)hModule16 );
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000362 return NULL; /* FIXME: Should unload the builtin module */
363 }
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000364 pModule = (NE_MODULE *)GlobalLock16( hModule16 );
365 pModule->flags = NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA | NE_FFLAGS_WIN32 | NE_FFLAGS_BUILTIN;
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000366
367 /* Create 32-bit MODREF */
Alexandre Julliard112d3072000-01-16 03:37:05 +0000368 if ( !(wm = PE_CreateModule( pModule->module32, dllname, flags, TRUE )) )
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000369 {
Alexandre Julliard74e4a282000-01-09 04:20:31 +0000370 ERR( "can't load %s\n", path );
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000371 FreeLibrary16( hModule16 ); /* FIXME: Should unload the builtin module */
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000372 SetLastError( ERROR_OUTOFMEMORY );
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000373 return NULL;
374 }
375
376 if (wm->binfmt.pe.pe_export)
377 SNOOP_RegisterDLL(wm->module,wm->modname,wm->binfmt.pe.pe_export->NumberOfFunctions);
378
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000379 req->handle = -1;
380 req->base = (void *)pModule->module32;
381 req->dbg_offset = 0;
382 req->dbg_size = 0;
383 req->name = &wm->modname;
384 server_call_noerr( REQ_LOAD_DLL );
Ulrich Weigand237e8e91999-12-04 04:04:58 +0000385 return wm;
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000386}
387
Ulrich Weigand9ffd4032000-02-03 01:33:48 +0000388/***********************************************************************
389 * BUILTIN32_LoadExeModule
390 */
Alexandre Julliardb4459522000-04-15 21:00:55 +0000391HMODULE BUILTIN32_LoadExeModule( LPCSTR *filename )
Ulrich Weigand9ffd4032000-02-03 01:33:48 +0000392{
Ulrich Weigand9ffd4032000-02-03 01:33:48 +0000393 int i, exe = -1;
394
395 /* Search built-in EXE descriptor */
396 for ( i = 0; i < nb_dlls; i++ )
397 if ( !(builtin_dlls[i]->characteristics & IMAGE_FILE_DLL) )
398 {
399 if ( exe != -1 )
400 {
401 MESSAGE( "More than one built-in EXE module loaded!\n" );
402 break;
403 }
404
405 exe = i;
406 }
407
408 if ( exe == -1 )
409 {
410 MESSAGE( "No built-in EXE module loaded! Did you create a .spec file?\n" );
411 return 0;
412 }
413
414 /* Load built-in module */
415 if ( !dll_modules[exe] )
416 if ( !(dll_modules[exe] = BUILTIN32_DoLoadImage( builtin_dlls[exe] )) )
417 return 0;
418
Alexandre Julliardb4459522000-04-15 21:00:55 +0000419 *filename = builtin_dlls[exe]->filename;
420 return dll_modules[exe];
Ulrich Weigand9ffd4032000-02-03 01:33:48 +0000421}
422
Bertho Stultiensc1d1cfe1999-04-18 12:14:06 +0000423
424/***********************************************************************
425 * BUILTIN32_UnloadLibrary
426 *
427 * Unload the built-in library and free the modref.
428 */
429void BUILTIN32_UnloadLibrary(WINE_MODREF *wm)
430{
431 /* FIXME: do something here */
432}
433
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000434/***********************************************************************
Alexandre Julliard112d3072000-01-16 03:37:05 +0000435 * BUILTIN32_RegisterDLL
436 *
437 * Register a built-in DLL descriptor.
438 */
439void BUILTIN32_RegisterDLL( const BUILTIN32_DESCRIPTOR *descr )
440{
441 assert( nb_dlls < MAX_DLLS );
442 builtin_dlls[nb_dlls++] = descr;
443}
444
445/***********************************************************************
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000446 * BUILTIN32_Unimplemented
447 *
448 * This function is called for unimplemented 32-bit entry points (declared
449 * as 'stub' in the spec file).
450 */
Alexandre Julliard246c3602000-05-10 03:48:00 +0000451void BUILTIN32_Unimplemented( const char *dllname, const char *funcname )
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000452{
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000453 __RESTORE_ES; /* Just in case */
454
Alexandre Julliard246c3602000-05-10 03:48:00 +0000455 MESSAGE( "No handler for Win32 routine %s.%s", dllname, funcname );
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000456#ifdef __GNUC__
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000457 MESSAGE( " (called from %p)", __builtin_return_address(1) );
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000458#endif
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000459 MESSAGE( "\n" );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000460 ExitProcess(1);
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000461}