Added some rpcrt4 stubs.
[wine/wine-kai.git] / loader / elfdll.c
blobfaacd9d90b7b149caa7b561c90c9e58fb89a8866
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 && !strcasecmp(cptr,".dll")) *cptr = '\0'; /* Strip extension */
139 /* Convert to lower case.
140 * This must be done manually because it is not sure that
141 * other modules are accessible.
143 for(cptr = name; *cptr; cptr++)
144 *cptr = tolower(*cptr);
146 return name;
150 /****************************************************************************
151 * ELFDLL_LoadLibraryExA (internal)
153 * Implementation of elf-dll loading for PE modules
155 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR path, DWORD flags)
157 LPVOID dlhandle;
158 struct elfdll_image *image;
159 char name[129];
160 char soname[129];
161 WINE_MODREF *wm;
163 get_sobasename(path, name);
164 strcpy(soname, name);
165 strcat(soname, ".so");
167 /* Try to open the elf-dll */
168 dlhandle = ELFDLL_dlopen(soname, RTLD_LAZY);
169 if(!dlhandle)
171 WARN("Could not load %s (%s)\n", soname, dlerror());
172 SetLastError( ERROR_FILE_NOT_FOUND );
173 return NULL;
176 /* Get the 'dllname_elfdll_image' variable */
177 strcpy(soname, name);
178 strcat(soname, "_elfdll_image");
179 image = (struct elfdll_image *)dlsym(dlhandle, soname);
180 if(!image)
182 ERR("Could not get elfdll image descriptor %s (%s)\n", soname, dlerror());
183 dlclose(dlhandle);
184 SetLastError( ERROR_BAD_FORMAT );
185 return NULL;
188 wm = PE_CreateModule( image->pe_module_start, path, 0, -1, FALSE );
189 if(!wm)
191 ERR("Could not create WINE_MODREF for %s\n", path);
192 dlclose(dlhandle);
193 SetLastError( ERROR_OUTOFMEMORY );
194 return NULL;
196 wm->dlhandle = dlhandle;
198 dump_exports(image->pe_module_start);
199 return wm;
202 #else
205 * No elfdlls possible
206 * Just put stubs in here.
209 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR libname, DWORD flags)
211 SetLastError( ERROR_FILE_NOT_FOUND );
212 return NULL;
215 #endif