Don't forget to map the message in WINPROC_MapMsg16To32W().
[wine.git] / loader / elfdll.c
blob5398c212ea91d709af0e48eecb750f246bf9bb13
1 /*
2 * Elf-dll loader functions
4 * Copyright 1999 Bertho A. Stultiens
5 */
7 #include <string.h>
8 #include <ctype.h>
9 #include <stdlib.h>
11 #include "config.h"
12 #include "windef.h"
13 #include "global.h"
14 #include "process.h"
15 #include "module.h"
16 #include "heap.h"
17 #include "wine/winbase16.h"
18 #include "elfdll.h"
19 #include "debugtools.h"
20 #include "winerror.h"
22 DEFAULT_DEBUG_CHANNEL(elfdll);
24 #if defined(HAVE_DL_API)
25 #include <dlfcn.h>
27 /*------------------ HACKS -----------------*/
28 extern DWORD fixup_imports(WINE_MODREF *wm);
29 extern void dump_exports(HMODULE hModule);
30 /*---------------- END HACKS ---------------*/
32 char *extra_ld_library_path = NULL; /* The extra search-path set in wine.conf */
34 struct elfdll_image
36 HMODULE pe_module_start;
37 DWORD pe_module_size;
38 NE_MODULE *ne_module_start;
39 DWORD ne_module_size;
43 /****************************************************************************
44 * ELFDLL_dlopen
46 * Wrapper for dlopen to search the EXTRA_LD_LIBRARY_PATH from wine.conf
47 * manually because libdl.so caches the environment and does not accept our
48 * changes.
50 void *ELFDLL_dlopen(const char *libname, int flags)
52 char buffer[256];
53 int namelen;
54 void *handle;
55 char *ldpath;
57 /* First try the default path search of dlopen() */
58 handle = dlopen(libname, flags);
59 /* do NOT call dlerror() here ! (check after return) */
60 if(handle)
61 return handle;
63 /* Now try to construct searches through our extra search-path */
64 namelen = strlen(libname);
65 ldpath = extra_ld_library_path;
66 while(ldpath && *ldpath)
68 int len;
69 char *cptr;
70 char *from;
72 from = ldpath;
73 cptr = strchr(ldpath, ':');
74 if(!cptr)
76 len = strlen(ldpath);
77 ldpath = NULL;
79 else
81 len = cptr - ldpath;
82 ldpath = cptr + 1;
85 if(len + namelen + 1 >= sizeof(buffer))
87 ERR("Buffer overflow! Check EXTRA_LD_LIBRARY_PATH or increase buffer size.\n");
88 return NULL;
91 strncpy(buffer, from, len);
92 if(len)
94 buffer[len] = '/';
95 strcpy(buffer + len + 1, libname);
97 else
98 strcpy(buffer + len, libname);
100 TRACE("Trying dlopen('%s', %d)\n", buffer, flags);
102 handle = dlopen(buffer, flags);
103 /* do NOT call dlerror() here ! (check after return) */
104 if(handle)
105 return handle;
107 return NULL;
111 /****************************************************************************
112 * get_sobasename (internal)
115 static LPSTR get_sobasename(LPCSTR path, LPSTR name)
117 char *cptr;
119 /* Strip the path from the library name */
120 if((cptr = strrchr(path, '/')))
122 char *cp = strrchr(cptr+1, '\\');
123 if(cp && cp > cptr)
124 cptr = cp;
126 else
127 cptr = strrchr(path, '\\');
129 if(!cptr)
130 cptr = (char *)path; /* No '/' nor '\\' in path */
131 else
132 cptr++;
134 strcpy(name, cptr);
135 cptr = strrchr(name, '.');
136 if(cptr && !strcasecmp(cptr,".dll")) *cptr = '\0'; /* Strip extension */
138 /* Convert to lower case.
139 * This must be done manually because it is not sure that
140 * other modules are accessible.
142 for(cptr = name; *cptr; cptr++)
143 *cptr = tolower(*cptr);
145 return name;
149 /****************************************************************************
150 * ELFDLL_LoadLibraryExA (internal)
152 * Implementation of elf-dll loading for PE modules
154 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR path, DWORD flags)
156 LPVOID dlhandle;
157 struct elfdll_image *image;
158 char name[129];
159 char soname[129];
160 WINE_MODREF *wm;
162 get_sobasename(path, name);
163 strcpy(soname, name);
164 strcat(soname, ".so");
166 /* Try to open the elf-dll */
167 dlhandle = ELFDLL_dlopen(soname, RTLD_LAZY);
168 if(!dlhandle)
170 WARN("Could not load %s (%s)\n", soname, dlerror());
171 SetLastError( ERROR_FILE_NOT_FOUND );
172 return NULL;
175 /* Get the 'dllname_elfdll_image' variable */
176 strcpy(soname, name);
177 strcat(soname, "_elfdll_image");
178 image = (struct elfdll_image *)dlsym(dlhandle, soname);
179 if(!image)
181 ERR("Could not get elfdll image descriptor %s (%s)\n", soname, dlerror());
182 dlclose(dlhandle);
183 SetLastError( ERROR_BAD_FORMAT );
184 return NULL;
187 wm = PE_CreateModule( image->pe_module_start, path, 0, -1, FALSE );
188 if(!wm)
190 ERR("Could not create WINE_MODREF for %s\n", path);
191 dlclose(dlhandle);
192 SetLastError( ERROR_OUTOFMEMORY );
193 return NULL;
195 wm->dlhandle = dlhandle;
197 dump_exports(image->pe_module_start);
198 return wm;
201 #else
204 * No elfdlls possible
205 * Just put stubs in here.
208 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR libname, DWORD flags)
210 SetLastError( ERROR_FILE_NOT_FOUND );
211 return NULL;
214 #endif