Fixed some deadlock(s) in message sending.
[wine.git] / loader / elfdll.c
blobb6164134b10fc595d5d32c133ec8f80554654ba1
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 DECLARE_DEBUG_CHANNEL(elfdll)
25 #if defined(HAVE_DL_API)
26 #include <dlfcn.h>
28 /*------------------ HACKS -----------------*/
29 extern DWORD fixup_imports(WINE_MODREF *wm);
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 if(handle)
60 return handle;
62 /* Now try to construct searches through our extra search-path */
63 namelen = strlen(libname);
64 ldpath = extra_ld_library_path;
65 while(ldpath && *ldpath)
67 int len;
68 char *cptr;
69 char *from;
71 from = ldpath;
72 cptr = strchr(ldpath, ':');
73 if(!cptr)
75 len = strlen(ldpath);
76 ldpath = NULL;
78 else
80 len = cptr - ldpath;
81 ldpath = cptr + 1;
84 if(len + namelen + 1 >= sizeof(buffer))
86 ERR_(elfdll)("Buffer overflow! Check EXTRA_LD_LIBRARY_PATH or increase buffer size.\n");
87 return NULL;
90 strncpy(buffer, from, len);
91 if(len)
93 buffer[len] = '/';
94 strcpy(buffer + len + 1, libname);
96 else
97 strcpy(buffer + len, libname);
99 TRACE_(elfdll)("Trying dlopen('%s', %d)\n", buffer, flags);
101 handle = dlopen(buffer, flags);
102 if(handle)
103 return handle;
105 return NULL;
109 /****************************************************************************
110 * get_sobasename (internal)
113 static LPSTR get_sobasename(LPCSTR path, LPSTR name)
115 char *cptr;
117 /* Strip the path from the library name */
118 if((cptr = strrchr(path, '/')))
120 char *cp = strrchr(cptr+1, '\\');
121 if(cp && cp > cptr)
122 cptr = cp+1;
123 else
124 cptr++;
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)
137 *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_CreateModref (internal)
153 * INPUT
154 * hModule - the header from the elf-dll's data-segment
155 * path - requested path from original call
157 * OUTPUT
158 * A WINE_MODREF pointer to the new object
160 * BUGS
161 * - Does not handle errors due to dependencies correctly
162 * - path can be wrong
164 #define RVA(base, va) (((DWORD)base) + ((DWORD)va))
166 static WINE_MODREF *ELFDLL_CreateModref(HMODULE hModule, LPCSTR path)
168 IMAGE_NT_HEADERS *nt = PE_HEADER(hModule);
169 IMAGE_DATA_DIRECTORY *dir;
170 IMAGE_IMPORT_DESCRIPTOR *pe_import = NULL;
171 WINE_MODREF *wm;
172 int len;
173 HANDLE procheap = GetProcessHeap();
175 wm = (WINE_MODREF *)HeapAlloc(procheap, HEAP_ZERO_MEMORY, sizeof(*wm));
176 if(!wm)
177 return NULL;
179 wm->module = hModule;
180 wm->type = MODULE32_PE; /* FIXME */
182 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXPORT;
183 if(dir->Size)
184 wm->binfmt.pe.pe_export = (PIMAGE_EXPORT_DIRECTORY)RVA(hModule, dir->VirtualAddress);
186 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IMPORT;
187 if(dir->Size)
188 pe_import = wm->binfmt.pe.pe_import = (PIMAGE_IMPORT_DESCRIPTOR)RVA(hModule, dir->VirtualAddress);
190 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
191 if(dir->Size)
192 wm->binfmt.pe.pe_resource = (PIMAGE_RESOURCE_DIRECTORY)RVA(hModule, dir->VirtualAddress);
194 wm->modname = HEAP_strdupA(procheap, 0, (char *)RVA(hModule, wm->binfmt.pe.pe_export->Name));
196 len = GetLongPathNameA(path, NULL, 0);
197 wm->longname = (char *)HeapAlloc(procheap, 0, len+1);
198 GetLongPathNameA(path, wm->longname, len+1);
200 wm->shortname = HEAP_strdupA(procheap, 0, path);
202 /* Link MODREF into process list */
203 wm->next = PROCESS_Current()->modref_list;
204 PROCESS_Current()->modref_list = wm;
206 if(!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
208 if(PROCESS_Current()->exe_modref)
209 FIXME_(elfdll)("overwriting old exe_modref... arrgh\n");
210 PROCESS_Current()->exe_modref = wm;
213 /* Fixup Imports */
214 if(pe_import && fixup_imports(wm))
216 /* Error in this module or its dependencies
217 * remove entry from modref chain
219 WINE_MODREF **xwm;
220 for(xwm = &(PROCESS_Current()->modref_list); *xwm; xwm = &((*xwm)->next))
222 if ( *xwm == wm )
224 *xwm = wm->next;
225 break;
228 if(wm == PROCESS_Current()->exe_modref)
229 ERR_(elfdll)("Have to delete current exe_modref. Expect crash now\n");
230 HeapFree(procheap, 0, wm->shortname);
231 HeapFree(procheap, 0, wm->longname);
232 HeapFree(procheap, 0, wm->modname);
233 HeapFree(procheap, 0, wm);
234 return NULL;
236 /* FIXME: We should traverse back in the recursion
237 * with an error to unload everything that got loaded
238 * before this error occurred.
239 * Too dificult for now though and we don't care at the
240 * moment. But, it *MUST* be implemented someday because
241 * we won't be able to map the elf-dll twice in this
242 * address-space which can cause some unexpected and
243 * weird problems later on.
247 return wm;
251 /***********************************************************************
252 * ELFDLL_CreateNEModule
254 * Create a dummy NE module for the win32 elf-dll based on the supplied
255 * NE header in the elf-dll.
257 static HMODULE16 ELFDLL_CreateNEModule(NE_MODULE *ne_image, DWORD size)
259 HMODULE16 hModule = GLOBAL_CreateBlock(GMEM_MOVEABLE, ne_image, size, 0,
260 FALSE, FALSE, FALSE, NULL);
261 if(!hModule)
262 return (HMODULE16)0;
264 FarSetOwner16(hModule, hModule);
265 NE_RegisterModule(ne_image);
266 return hModule;
270 /****************************************************************************
271 * ELFDLL_LoadLibraryExA (internal)
273 * Implementation of elf-dll loading for PE modules
275 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR path, DWORD flags, DWORD *err)
277 LPVOID dlhandle;
278 struct elfdll_image *image;
279 char name[129];
280 char soname[129];
281 HMODULE16 hmod16;
282 WINE_MODREF *wm;
284 get_sobasename(path, name);
285 strcpy(soname, name);
286 strcat(soname, ".so");
288 /* Try to open the elf-dll */
289 dlhandle = ELFDLL_dlopen(soname, RTLD_LAZY);
290 if(!dlhandle)
292 WARN_(elfdll)("Could not load %s (%s)\n", soname, dlerror());
293 *err = ERROR_FILE_NOT_FOUND;
294 return NULL;
297 /* Get the 'dllname_elfdll_image' variable */
298 strcpy(soname, name);
299 strcat(soname, "_elfdll_image");
300 image = (struct elfdll_image *)dlsym(dlhandle, soname);
301 if(!image)
303 ERR_(elfdll)("Could not get elfdll image descriptor %s (%s)\n", soname, dlerror());
304 dlclose(dlhandle);
305 *err = ERROR_BAD_FORMAT;
306 return NULL;
309 /* Create a win16 dummy module */
310 hmod16 = ELFDLL_CreateNEModule(image->ne_module_start, image->ne_module_size);
311 if(!hmod16)
313 ERR_(elfdll)("Could not create win16 dummy module for %s\n", path);
314 dlclose(dlhandle);
315 *err = ERROR_OUTOFMEMORY;
316 return NULL;
319 image->ne_module_start->module32 = image->pe_module_start;
321 wm = ELFDLL_CreateModref(image->pe_module_start, path);
322 if(!wm)
324 ERR_(elfdll)("Could not create WINE_MODREF for %s\n", path);
325 GLOBAL_FreeBlock((HGLOBAL16)hmod16);
326 dlclose(dlhandle);
327 *err = ERROR_OUTOFMEMORY;
328 return NULL;
331 *err = 0;
332 return wm;
336 /****************************************************************************
337 * ELFDLL_UnloadLibrary (internal)
339 * Unload an elf-dll completely from memory and deallocate the modref
341 void ELFDLL_UnloadLibrary(WINE_MODREF *wm)
346 /****************************************************************************
347 * ELFDLL_LoadModule16 (internal)
349 * Implementation of elf-dll loading for NE modules
351 HINSTANCE16 ELFDLL_LoadModule16(LPCSTR libname, BOOL implicit)
353 return (HINSTANCE16)ERROR_FILE_NOT_FOUND;
356 #else
359 * No elfdlls possible
360 * Just put stubs in here.
363 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR libname, DWORD flags, DWORD *err)
365 *err = ERROR_FILE_NOT_FOUND;
366 return NULL;
369 void ELFDLL_UnloadLibrary(WINE_MODREF *wm)
373 HINSTANCE16 ELFDLL_LoadModule16(LPCSTR libname, BOOL implicit)
375 return (HINSTANCE16)ERROR_FILE_NOT_FOUND;
378 #endif