Added regedit unit test, a couple minor changes to regedit.
[wine] / relay32 / builtin32.c
1 /*
2  * Win32 builtin functions
3  *
4  * Copyright 1997 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <assert.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <sys/types.h>
28 #ifdef HAVE_SYS_MMAN_H
29 #include <sys/mman.h>
30 #endif
31
32 #include "windef.h"
33 #include "wine/winbase16.h"
34 #include "wine/library.h"
35 #include "module.h"
36 #include "file.h"
37 #include "winerror.h"
38 #include "wine/server.h"
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(module);
42 WINE_DECLARE_DEBUG_CHANNEL(relay);
43
44 extern void RELAY_SetupDLL( const char *module );
45
46 static HMODULE main_module;
47
48 /***********************************************************************
49  *           BUILTIN32_dlopen
50  */
51 void *BUILTIN32_dlopen( const char *name )
52 {
53     void *handle;
54     char error[256];
55
56     if (!(handle = wine_dll_load( name, error, sizeof(error) )))
57     {
58         if (strstr(error, "cannot open") || strstr(error, "open failed") ||
59             (strstr(error, "Shared object") && strstr(error, "not found"))) {
60             /* The file does not exist -> WARN() */
61             WARN("cannot open .so lib for builtin %s: %s\n", name, error);
62         } else {
63             /* ERR() for all other errors (missing functions, ...) */
64             ERR("failed to load .so lib for builtin %s: %s\n", name, error );
65         }
66     }
67     return handle;
68 }
69
70 /***********************************************************************
71  *           BUILTIN32_dlclose
72  */
73 int BUILTIN32_dlclose( void *handle )
74 {
75     /* FIXME: should unregister descriptors first */
76     /* wine_dll_unload( handle ); */
77     return 0;
78 }
79
80
81 /***********************************************************************
82  *           load_library
83  *
84  * Load a library in memory; callback function for wine_dll_register
85  */
86 static void load_library( void *base, const char *filename )
87 {
88     HMODULE module = (HMODULE)base;
89     WINE_MODREF *wm;
90     char *fullname;
91     DWORD len;
92
93     if (!base)
94     {
95         ERR("could not map image for %s\n", filename ? filename : "main exe" );
96         return;
97     }
98
99     if (!(PE_HEADER(module)->FileHeader.Characteristics & IMAGE_FILE_DLL))
100     {
101         /* if we already have an executable, ignore this one */
102         if (!main_module) main_module = module;
103         return; /* don't create the modref here, will be done later on */
104     }
105
106     if (GetModuleHandleA( filename ))
107         MESSAGE( "Warning: loading builtin %s, but native version already present. Expect trouble.\n", filename );
108
109     len = GetSystemDirectoryA( NULL, 0 );
110     if (!(fullname = HeapAlloc( GetProcessHeap(), 0, len + strlen(filename) + 1 )))
111     {
112         ERR( "can't load %s\n", filename );
113         SetLastError( ERROR_OUTOFMEMORY );
114         return;
115     }
116     GetSystemDirectoryA( fullname, len );
117     strcat( fullname, "\\" );
118     strcat( fullname, filename );
119
120     /* Create 32-bit MODREF */
121     if (!(wm = PE_CreateModule( module, fullname, 0, 0, TRUE )))
122     {
123         ERR( "can't load %s\n", filename );
124         HeapFree( GetProcessHeap(), 0, fullname );
125         SetLastError( ERROR_OUTOFMEMORY );
126         return;
127     }
128     TRACE( "loaded %s %p %x\n", fullname, wm, module );
129     HeapFree( GetProcessHeap(), 0, fullname );
130     wm->refCount++;  /* we don't support freeing builtin dlls (FIXME)*/
131
132     /* setup relay debugging entry points */
133     if (TRACE_ON(relay)) RELAY_SetupDLL( (void *)module );
134 }
135
136
137 /***********************************************************************
138  *           BUILTIN32_LoadLibraryExA
139  *
140  * Partly copied from the original PE_ version.
141  *
142  */
143 WINE_MODREF *BUILTIN32_LoadLibraryExA(LPCSTR path, DWORD flags)
144 {
145     WINE_MODREF   *wm;
146     char dllname[20], *p;
147     LPCSTR name;
148     void *handle;
149
150     /* Fix the name in case we have a full path and extension */
151     name = path;
152     if ((p = strrchr( name, '\\' ))) name = p + 1;
153     if ((p = strrchr( name, '/' ))) name = p + 1;
154
155     if (strlen(name) >= sizeof(dllname)-4) goto error;
156
157     strcpy( dllname, name );
158     p = strrchr( dllname, '.' );
159     if (!p) strcat( dllname, ".dll" );
160     for (p = dllname; *p; p++) *p = FILE_tolower(*p);
161
162     if (!(handle = BUILTIN32_dlopen( dllname ))) goto error;
163
164     if (!(wm = MODULE_FindModule( path ))) wm = MODULE_FindModule( dllname );
165     if (!wm)
166     {
167         ERR( "loaded .so but dll %s still not found - library environment problem or version conflict, check your setup.\n", dllname );
168         /* wine_dll_unload( handle );*/
169         return NULL;
170     }
171     wm->dlhandle = handle;
172     return wm;
173
174  error:
175     SetLastError( ERROR_FILE_NOT_FOUND );
176     return NULL;
177 }
178
179 /***********************************************************************
180  *           BUILTIN32_Init
181  *
182  * Initialize loading callbacks and return HMODULE of main exe.
183  * 'main' is the main exe in case it was already loaded from a PE file.
184  */
185 HMODULE BUILTIN32_LoadExeModule( HMODULE main )
186 {
187     main_module = main;
188     wine_dll_set_callback( load_library );
189     if (!main_module)
190         MESSAGE( "No built-in EXE module loaded!  Did you create a .spec file?\n" );
191     return main_module;
192 }
193
194
195 /***********************************************************************
196  *           BUILTIN32_RegisterDLL
197  *
198  * Register a built-in DLL descriptor.
199  */
200 void BUILTIN32_RegisterDLL( const IMAGE_NT_HEADERS *header, const char *filename )
201 {
202     extern void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename );
203     __wine_dll_register( header, filename );
204 }