Removed non needed include of module.h.
[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 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <sys/types.h>
29 #ifdef HAVE_SYS_MMAN_H
30 #include <sys/mman.h>
31 #endif
32
33 #include "windef.h"
34 #include "wine/winbase16.h"
35 #include "wine/library.h"
36 #include "module.h"
37 #include "file.h"
38 #include "winerror.h"
39 #include "wine/server.h"
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(module);
43 WINE_DECLARE_DEBUG_CHANNEL(relay);
44
45 extern void RELAY_SetupDLL( const char *module );
46
47 static HMODULE main_module;
48
49 /***********************************************************************
50  *           BUILTIN32_dlopen
51  */
52 void *BUILTIN32_dlopen( const char *name )
53 {
54     void *handle;
55     char error[256];
56
57     if (!(handle = wine_dll_load( name, error, sizeof(error) )))
58     {
59         if (strstr(error, "cannot open") || strstr(error, "open failed") ||
60             (strstr(error, "Shared object") && strstr(error, "not found"))) {
61             /* The file does not exist -> WARN() */
62             WARN("cannot open .so lib for builtin %s: %s\n", name, error);
63         } else {
64             /* ERR() for all other errors (missing functions, ...) */
65             ERR("failed to load .so lib for builtin %s: %s\n", name, error );
66         }
67     }
68     return handle;
69 }
70
71 /***********************************************************************
72  *           BUILTIN32_dlclose
73  */
74 int BUILTIN32_dlclose( void *handle )
75 {
76     /* FIXME: should unregister descriptors first */
77     /* wine_dll_unload( handle ); */
78     return 0;
79 }
80
81
82 /***********************************************************************
83  *           load_library
84  *
85  * Load a library in memory; callback function for wine_dll_register
86  */
87 static void load_library( void *base, const char *filename )
88 {
89     HMODULE module = (HMODULE)base;
90     IMAGE_NT_HEADERS *nt;
91     WINE_MODREF *wm;
92     char *fullname;
93     DWORD len;
94
95     if (!base)
96     {
97         ERR("could not map image for %s\n", filename ? filename : "main exe" );
98         return;
99     }
100     if (!(nt = RtlImageNtHeader( module )))
101     {
102         ERR( "bad module for %s\n", filename ? filename : "main exe" );
103         return;
104     }
105
106     if (!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
107     {
108         /* if we already have an executable, ignore this one */
109         if (!main_module) main_module = module;
110         return; /* don't create the modref here, will be done later on */
111     }
112
113     if (GetModuleHandleA( filename ))
114         MESSAGE( "Warning: loading builtin %s, but native version already present. Expect trouble.\n", filename );
115
116     len = GetSystemDirectoryA( NULL, 0 );
117     if (!(fullname = HeapAlloc( GetProcessHeap(), 0, len + strlen(filename) + 1 )))
118     {
119         ERR( "can't load %s\n", filename );
120         SetLastError( ERROR_OUTOFMEMORY );
121         return;
122     }
123     GetSystemDirectoryA( fullname, len );
124     strcat( fullname, "\\" );
125     strcat( fullname, filename );
126
127     /* Create 32-bit MODREF */
128     if (!(wm = PE_CreateModule( module, fullname, 0, 0, TRUE )))
129     {
130         ERR( "can't load %s\n", filename );
131         HeapFree( GetProcessHeap(), 0, fullname );
132         SetLastError( ERROR_OUTOFMEMORY );
133         return;
134     }
135     TRACE( "loaded %s %p %p\n", fullname, wm, module );
136     HeapFree( GetProcessHeap(), 0, fullname );
137     wm->refCount++;  /* we don't support freeing builtin dlls (FIXME)*/
138
139     /* setup relay debugging entry points */
140     if (TRACE_ON(relay)) RELAY_SetupDLL( (void *)module );
141 }
142
143
144 /***********************************************************************
145  *           BUILTIN32_LoadLibraryExA
146  *
147  * Partly copied from the original PE_ version.
148  *
149  */
150 WINE_MODREF *BUILTIN32_LoadLibraryExA(LPCSTR path, DWORD flags)
151 {
152     WINE_MODREF   *wm;
153     char dllname[20], *p;
154     LPCSTR name;
155     void *handle;
156
157     /* Fix the name in case we have a full path and extension */
158     name = path;
159     if ((p = strrchr( name, '\\' ))) name = p + 1;
160     if ((p = strrchr( name, '/' ))) name = p + 1;
161
162     if (strlen(name) >= sizeof(dllname)-4) goto error;
163
164     strcpy( dllname, name );
165     p = strrchr( dllname, '.' );
166     if (!p) strcat( dllname, ".dll" );
167     for (p = dllname; *p; p++) *p = FILE_tolower(*p);
168
169     if (!(handle = BUILTIN32_dlopen( dllname ))) goto error;
170
171     if (!(wm = MODULE_FindModule( path ))) wm = MODULE_FindModule( dllname );
172     if (!wm)
173     {
174         ERR( "loaded .so but dll %s still not found - 16-bit dll or version conflict.\n", dllname );
175         /* wine_dll_unload( handle );*/
176         SetLastError( ERROR_BAD_EXE_FORMAT );
177         return NULL;
178     }
179     wm->dlhandle = handle;
180     return wm;
181
182  error:
183     SetLastError( ERROR_FILE_NOT_FOUND );
184     return NULL;
185 }
186
187 /***********************************************************************
188  *           BUILTIN32_Init
189  *
190  * Initialize loading callbacks and return HMODULE of main exe.
191  * 'main' is the main exe in case it was already loaded from a PE file.
192  */
193 HMODULE BUILTIN32_LoadExeModule( HMODULE main )
194 {
195     main_module = main;
196     wine_dll_set_callback( load_library );
197     if (!main_module)
198         MESSAGE( "No built-in EXE module loaded!  Did you create a .spec file?\n" );
199     return main_module;
200 }
201
202
203 /***********************************************************************
204  *           BUILTIN32_RegisterDLL
205  *
206  * Register a built-in DLL descriptor.
207  */
208 void BUILTIN32_RegisterDLL( const IMAGE_NT_HEADERS *header, const char *filename )
209 {
210     extern void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename );
211     __wine_dll_register( header, filename );
212 }