Always pass lower-case filenames to wine_dll_load().
[wine/testsucceed.git] / relay32 / builtin32.c
blobb5dec5ec4d75cd6f76a0919fdec4f2391d4f5afa
1 /*
2 * Win32 builtin functions
4 * Copyright 1997 Alexandre Julliard
5 */
7 #include "config.h"
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
21 #include "windef.h"
22 #include "wine/winbase16.h"
23 #include "wine/library.h"
24 #include "global.h"
25 #include "module.h"
26 #include "file.h"
27 #include "heap.h"
28 #include "main.h"
29 #include "winerror.h"
30 #include "server.h"
31 #include "debugtools.h"
33 DEFAULT_DEBUG_CHANNEL(module);
34 DECLARE_DEBUG_CHANNEL(relay);
36 extern void RELAY_SetupDLL( const char *module );
38 static HMODULE main_module;
40 /***********************************************************************
41 * BUILTIN32_dlopen
43 void *BUILTIN32_dlopen( const char *name )
45 #ifdef HAVE_DL_API
46 void *handle;
48 if (!(handle = wine_dll_load( name )))
50 LPSTR pErr;
51 if ((pErr = dlerror()))
53 if (strstr(pErr, "undefined symbol")) /* 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 );
59 return handle;
60 #else
61 return NULL;
62 #endif
65 /***********************************************************************
66 * BUILTIN32_dlclose
68 int BUILTIN32_dlclose( void *handle )
70 #ifdef HAVE_DL_API
71 /* FIXME: should unregister descriptors first */
72 /* return dlclose( handle ); */
73 #endif
74 return 0;
78 /***********************************************************************
79 * load_library
81 * Load a library in memory; callback function for wine_dll_register
83 static void load_library( void *base, const char *filename )
85 HMODULE module = (HMODULE)base;
86 WINE_MODREF *wm;
88 if (!base)
90 ERR("could not map image for %s\n", filename ? filename : "main exe" );
91 return;
94 if (!(PE_HEADER(module)->FileHeader.Characteristics & IMAGE_FILE_DLL))
96 /* if we already have an executable, ignore this one */
97 if (!main_module) main_module = module;
98 return; /* don't create the modref here, will be done later on */
101 if (GetModuleHandleA( filename ))
102 MESSAGE( "Warning: loading builtin %s, but native version already present. Expect trouble.\n", filename );
104 /* Create 32-bit MODREF */
105 if (!(wm = PE_CreateModule( module, filename, 0, -1, TRUE )))
107 ERR( "can't load %s\n", filename );
108 SetLastError( ERROR_OUTOFMEMORY );
109 return;
111 TRACE( "loaded %s %p %x\n", filename, wm, module );
112 wm->refCount++; /* we don't support freeing builtin dlls (FIXME)*/
114 /* setup relay debugging entry points */
115 if (TRACE_ON(relay)) RELAY_SetupDLL( (void *)module );
119 /***********************************************************************
120 * BUILTIN32_LoadLibraryExA
122 * Partly copied from the original PE_ version.
125 WINE_MODREF *BUILTIN32_LoadLibraryExA(LPCSTR path, DWORD flags)
127 WINE_MODREF *wm;
128 char dllname[20], *p;
129 LPCSTR name;
130 void *handle;
132 /* Fix the name in case we have a full path and extension */
133 name = path;
134 if ((p = strrchr( name, '\\' ))) name = p + 1;
135 if ((p = strrchr( name, '/' ))) name = p + 1;
137 if (strlen(name) >= sizeof(dllname)-4) goto error;
139 strcpy( dllname, name );
140 p = strrchr( dllname, '.' );
141 if (!p) strcat( dllname, ".dll" );
142 for (p = dllname; *p; p++) *p = FILE_tolower(*p);
144 if (!(handle = BUILTIN32_dlopen( dllname ))) goto error;
146 if (!(wm = MODULE_FindModule( path ))) wm = MODULE_FindModule( dllname );
147 if (!wm)
149 ERR( "loaded .so but dll %s still not found\n", dllname );
150 /* wine_dll_unload( handle );*/
151 return NULL;
153 wm->dlhandle = handle;
154 return wm;
156 error:
157 SetLastError( ERROR_FILE_NOT_FOUND );
158 return NULL;
161 /***********************************************************************
162 * BUILTIN32_Init
164 * Initialize loading callbacks and return HMODULE of main exe.
165 * 'main' is the main exe in case if was already loaded from a PE file.
167 HMODULE BUILTIN32_LoadExeModule( HMODULE main )
169 main_module = main;
170 wine_dll_set_callback( load_library );
171 if (!main_module)
172 MESSAGE( "No built-in EXE module loaded! Did you create a .spec file?\n" );
173 return main_module;
177 /***********************************************************************
178 * BUILTIN32_RegisterDLL
180 * Register a built-in DLL descriptor.
182 void BUILTIN32_RegisterDLL( const IMAGE_NT_HEADERS *header, const char *filename )
184 extern void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename );
185 __wine_dll_register( header, filename );