Added a first-cut version of MapVirtualKeyExW() that has the same
[wine] / relay32 / builtin32.c
1 /*
2  * Win32 builtin functions
3  *
4  * Copyright 1997 Alexandre Julliard
5  */
6
7 #include "config.h"
8
9 #include <assert.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <ctype.h>
13 #ifdef HAVE_DL_API
14 #include <dlfcn.h>
15 #endif
16 #include <sys/types.h>
17 #ifdef HAVE_SYS_MMAN_H
18 #include <sys/mman.h>
19 #endif
20
21 #include "windef.h"
22 #include "wine/winbase16.h"
23 #include "wine/library.h"
24 #include "global.h"
25 #include "module.h"
26 #include "heap.h"
27 #include "main.h"
28 #include "winerror.h"
29 #include "server.h"
30 #include "debugtools.h"
31
32 DEFAULT_DEBUG_CHANNEL(module);
33 DECLARE_DEBUG_CHANNEL(relay);
34
35 extern void RELAY_SetupDLL( const char *module );
36
37 static HMODULE main_module;
38
39 /***********************************************************************
40  *           BUILTIN32_dlopen
41  */
42 void *BUILTIN32_dlopen( const char *name )
43 {
44 #ifdef HAVE_DL_API
45     void *handle;
46
47     if (!(handle = wine_dll_load( name )))
48     {
49         LPSTR pErr, p;
50         pErr = dlerror();
51         p = strchr(pErr, ':');
52         if ((p) && 
53            (!strncmp(p, ": undefined symbol", 18))) /* undef symbol -> ERR() */
54             ERR("failed to load %s: %s\n", name, pErr);
55         else /* WARN() for libraries that are supposed to be native */
56             WARN("failed to load %s: %s\n", name, pErr );
57     }
58     return handle;
59 #else
60     return NULL;
61 #endif
62 }
63
64 /***********************************************************************
65  *           BUILTIN32_dlclose
66  */
67 int BUILTIN32_dlclose( void *handle )
68 {
69 #ifdef HAVE_DL_API
70     /* FIXME: should unregister descriptors first */
71     /* return dlclose( handle ); */
72 #endif
73     return 0;
74 }
75
76
77 /***********************************************************************
78  *           load_library
79  *
80  * Load a library in memory; callback function for wine_dll_register
81  */
82 static void load_library( void *base, const char *filename )
83 {
84     HMODULE module = (HMODULE)base;
85     WINE_MODREF *wm;
86
87     if (!base)
88     {
89         ERR("could not map image for %s\n", filename ? filename : "main exe" );
90         return;
91     }
92
93     if (!(PE_HEADER(module)->FileHeader.Characteristics & IMAGE_FILE_DLL))
94     {
95         /* if we already have an executable, ignore this one */
96         if (!main_module) main_module = module;
97         return; /* don't create the modref here, will be done later on */
98     }
99
100     if (GetModuleHandleA( filename ))
101         MESSAGE( "Warning: loading builtin %s, but native version already present. Expect trouble.\n", filename );
102
103     /* Create 32-bit MODREF */
104     if (!(wm = PE_CreateModule( module, filename, 0, -1, TRUE )))
105     {
106         ERR( "can't load %s\n", filename );
107         SetLastError( ERROR_OUTOFMEMORY );
108         return;
109     }
110     TRACE( "loaded %s %p %x\n", filename, wm, module );
111     wm->refCount++;  /* we don't support freeing builtin dlls (FIXME)*/
112
113     /* setup relay debugging entry points */
114     if (TRACE_ON(relay)) RELAY_SetupDLL( (void *)module );
115 }
116
117
118 /***********************************************************************
119  *           BUILTIN32_LoadLibraryExA
120  *
121  * Partly copied from the original PE_ version.
122  *
123  */
124 WINE_MODREF *BUILTIN32_LoadLibraryExA(LPCSTR path, DWORD flags)
125 {
126     WINE_MODREF   *wm;
127     char dllname[20], *p;
128     LPCSTR name;
129     void *handle;
130
131     /* Fix the name in case we have a full path and extension */
132     name = path;
133     if ((p = strrchr( name, '\\' ))) name = p + 1;
134     if ((p = strrchr( name, '/' ))) name = p + 1;
135
136     if (strlen(name) >= sizeof(dllname)-4) goto error;
137
138     strcpy( dllname, name );
139     p = strrchr( dllname, '.' );
140     if (!p) strcat( dllname, ".dll" );
141
142     if (!(handle = BUILTIN32_dlopen( dllname ))) goto error;
143
144     if (!(wm = MODULE_FindModule( path ))) wm = MODULE_FindModule( dllname );
145     if (!wm)
146     {
147         ERR( "loaded .so but dll %s still not found\n", dllname );
148         /* wine_dll_unload( handle );*/
149         return NULL;
150     }
151     wm->dlhandle = handle;
152     return wm;
153
154  error:
155     SetLastError( ERROR_FILE_NOT_FOUND );
156     return NULL;
157 }
158
159 /***********************************************************************
160  *           BUILTIN32_Init
161  *
162  * Initialize loading callbacks and return HMODULE of main exe.
163  * 'main' is the main exe in case if was already loaded from a PE file.
164  */
165 HMODULE BUILTIN32_LoadExeModule( HMODULE main )
166 {
167     main_module = main;
168     wine_dll_set_callback( load_library );
169     if (!main_module)
170         MESSAGE( "No built-in EXE module loaded!  Did you create a .spec file?\n" );
171     return main_module;
172 }
173
174
175 /***********************************************************************
176  *           BUILTIN32_RegisterDLL
177  *
178  * Register a built-in DLL descriptor.
179  */
180 void BUILTIN32_RegisterDLL( const IMAGE_NT_HEADERS *header, const char *filename )
181 {
182     extern void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename );
183     __wine_dll_register( header, filename );
184 }