Enable the functions AddPrinterW, WINSPOOL_GetPrinterDriver,
[wine/multimedia.git] / loader / elfdll.c
blob7100971cb04c62f5ec8d92a5052430f9bcbd8a74
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 "neexe.h"
17 #include "heap.h"
18 #include "wine/winbase16.h"
19 #include "elfdll.h"
20 #include "debugtools.h"
21 #include "winerror.h"
23 DEFAULT_DEBUG_CHANNEL(elfdll)
25 #if defined(HAVE_DL_API)
26 #include <dlfcn.h>
28 /*------------------ HACKS -----------------*/
29 extern DWORD fixup_imports(WINE_MODREF *wm);
30 extern void dump_exports(HMODULE hModule);
31 /*---------------- END HACKS ---------------*/
33 char *extra_ld_library_path = NULL; /* The extra search-path set in wine.conf */
35 struct elfdll_image
37 HMODULE pe_module_start;
38 DWORD pe_module_size;
39 NE_MODULE *ne_module_start;
40 DWORD ne_module_size;
44 /****************************************************************************
45 * ELFDLL_dlopen
47 * Wrapper for dlopen to search the EXTRA_LD_LIBRARY_PATH from wine.conf
48 * manually because libdl.so caches the environment and does not accept our
49 * changes.
51 void *ELFDLL_dlopen(const char *libname, int flags)
53 char buffer[256];
54 int namelen;
55 void *handle;
56 char *ldpath;
58 /* First try the default path search of dlopen() */
59 handle = dlopen(libname, flags);
60 /* do NOT call dlerror() here ! (check after return) */
61 if(handle)
62 return handle;
64 /* Now try to construct searches through our extra search-path */
65 namelen = strlen(libname);
66 ldpath = extra_ld_library_path;
67 while(ldpath && *ldpath)
69 int len;
70 char *cptr;
71 char *from;
73 from = ldpath;
74 cptr = strchr(ldpath, ':');
75 if(!cptr)
77 len = strlen(ldpath);
78 ldpath = NULL;
80 else
82 len = cptr - ldpath;
83 ldpath = cptr + 1;
86 if(len + namelen + 1 >= sizeof(buffer))
88 ERR("Buffer overflow! Check EXTRA_LD_LIBRARY_PATH or increase buffer size.\n");
89 return NULL;
92 strncpy(buffer, from, len);
93 if(len)
95 buffer[len] = '/';
96 strcpy(buffer + len + 1, libname);
98 else
99 strcpy(buffer + len, libname);
101 TRACE("Trying dlopen('%s', %d)\n", buffer, flags);
103 handle = dlopen(buffer, flags);
104 /* do NOT call dlerror() here ! (check after return) */
105 if(handle)
106 return handle;
108 return NULL;
112 /****************************************************************************
113 * get_sobasename (internal)
116 static LPSTR get_sobasename(LPCSTR path, LPSTR name)
118 char *cptr;
120 /* Strip the path from the library name */
121 if((cptr = strrchr(path, '/')))
123 char *cp = strrchr(cptr+1, '\\');
124 if(cp && cp > cptr)
125 cptr = cp;
127 else
128 cptr = strrchr(path, '\\');
130 if(!cptr)
131 cptr = (char *)path; /* No '/' nor '\\' in path */
132 else
133 cptr++;
135 strcpy(name, cptr);
136 cptr = strrchr(name, '.');
137 if(cptr)
138 *cptr = '\0'; /* Strip extension */
140 /* Convert to lower case.
141 * This must be done manually because it is not sure that
142 * other modules are accessible.
144 for(cptr = name; *cptr; cptr++)
145 *cptr = tolower(*cptr);
147 return name;
151 /****************************************************************************
152 * ELFDLL_LoadLibraryExA (internal)
154 * Implementation of elf-dll loading for PE modules
156 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR path, DWORD flags)
158 LPVOID dlhandle;
159 struct elfdll_image *image;
160 char name[129];
161 char soname[129];
162 WINE_MODREF *wm;
164 get_sobasename(path, name);
165 strcpy(soname, name);
166 strcat(soname, ".so");
168 /* Try to open the elf-dll */
169 dlhandle = ELFDLL_dlopen(soname, RTLD_LAZY);
170 if(!dlhandle)
172 WARN("Could not load %s (%s)\n", soname, dlerror());
173 SetLastError( ERROR_FILE_NOT_FOUND );
174 return NULL;
177 /* Get the 'dllname_elfdll_image' variable */
178 strcpy(soname, name);
179 strcat(soname, "_elfdll_image");
180 image = (struct elfdll_image *)dlsym(dlhandle, soname);
181 if(!image)
183 ERR("Could not get elfdll image descriptor %s (%s)\n", soname, dlerror());
184 dlclose(dlhandle);
185 SetLastError( ERROR_BAD_FORMAT );
186 return NULL;
189 wm = PE_CreateModule( image->pe_module_start, path, 0, -1, FALSE );
190 if(!wm)
192 ERR("Could not create WINE_MODREF for %s\n", path);
193 dlclose(dlhandle);
194 SetLastError( ERROR_OUTOFMEMORY );
195 return NULL;
197 wm->dlhandle = dlhandle;
199 dump_exports(image->pe_module_start);
200 return wm;
203 #else
206 * No elfdlls possible
207 * Just put stubs in here.
210 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR libname, DWORD flags)
212 SetLastError( ERROR_FILE_NOT_FOUND );
213 return NULL;
216 #endif