2 * Elf-dll loader functions
4 * Copyright 1999 Bertho A. Stultiens
18 #include "wine/winbase16.h"
20 #include "debugtools.h"
23 DECLARE_DEBUG_CHANNEL(elfdll)
25 #if defined(HAVE_DL_API)
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 */
37 HMODULE pe_module_start;
39 NE_MODULE *ne_module_start;
44 /****************************************************************************
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
51 void *ELFDLL_dlopen(const char *libname, int flags)
58 /* First try the default path search of dlopen() */
59 handle = dlopen(libname, flags);
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)
73 cptr = strchr(ldpath, ':');
85 if(len + namelen + 1 >= sizeof(buffer))
87 ERR_(elfdll)("Buffer overflow! Check EXTRA_LD_LIBRARY_PATH or increase buffer size.\n");
91 strncpy(buffer, from, len);
95 strcpy(buffer + len + 1, libname);
98 strcpy(buffer + len, libname);
100 TRACE_(elfdll)("Trying dlopen('%s', %d)\n", buffer, flags);
102 handle = dlopen(buffer, flags);
110 /****************************************************************************
111 * get_sobasename (internal)
114 static LPSTR get_sobasename(LPCSTR path, LPSTR name)
118 /* Strip the path from the library name */
119 if((cptr = strrchr(path, '/')))
121 char *cp = strrchr(cptr+1, '\\');
126 cptr = strrchr(path, '\\');
129 cptr = (char *)path; /* No '/' nor '\\' in path */
134 cptr = strrchr(name, '.');
136 *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);
149 /****************************************************************************
150 * ELFDLL_CreateModref (internal)
153 * hModule - the header from the elf-dll's data-segment
154 * path - requested path from original call
157 * A WINE_MODREF pointer to the new object
160 * - Does not handle errors due to dependencies correctly
161 * - path can be wrong
163 #define RVA(base, va) (((DWORD)base) + ((DWORD)va))
165 static WINE_MODREF *ELFDLL_CreateModref(HMODULE hModule, LPCSTR path)
167 IMAGE_NT_HEADERS *nt = PE_HEADER(hModule);
168 IMAGE_DATA_DIRECTORY *dir;
169 IMAGE_IMPORT_DESCRIPTOR *pe_import = NULL;
172 HANDLE procheap = GetProcessHeap();
174 wm = (WINE_MODREF *)HeapAlloc(procheap, HEAP_ZERO_MEMORY, sizeof(*wm));
178 wm->module = hModule;
179 wm->type = MODULE32_PE; /* FIXME */
181 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXPORT;
183 wm->binfmt.pe.pe_export = (PIMAGE_EXPORT_DIRECTORY)RVA(hModule, dir->VirtualAddress);
185 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IMPORT;
187 pe_import = wm->binfmt.pe.pe_import = (PIMAGE_IMPORT_DESCRIPTOR)RVA(hModule, dir->VirtualAddress);
189 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
191 wm->binfmt.pe.pe_resource = (PIMAGE_RESOURCE_DIRECTORY)RVA(hModule, dir->VirtualAddress);
193 wm->modname = HEAP_strdupA(procheap, 0, (char *)RVA(hModule, wm->binfmt.pe.pe_export->Name));
195 len = GetLongPathNameA(path, NULL, 0);
196 wm->longname = (char *)HeapAlloc(procheap, 0, len+1);
197 GetLongPathNameA(path, wm->longname, len+1);
199 wm->shortname = HEAP_strdupA(procheap, 0, path);
201 /* Link MODREF into process list */
202 wm->next = PROCESS_Current()->modref_list;
203 PROCESS_Current()->modref_list = wm;
205 if(!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
207 if(PROCESS_Current()->exe_modref)
208 FIXME_(elfdll)("overwriting old exe_modref... arrgh\n");
209 PROCESS_Current()->exe_modref = wm;
213 if(pe_import && fixup_imports(wm))
215 /* Error in this module or its dependencies
216 * remove entry from modref chain
219 for(xwm = &(PROCESS_Current()->modref_list); *xwm; xwm = &((*xwm)->next))
227 if(wm == PROCESS_Current()->exe_modref)
228 ERR_(elfdll)("Have to delete current exe_modref. Expect crash now\n");
229 HeapFree(procheap, 0, wm->shortname);
230 HeapFree(procheap, 0, wm->longname);
231 HeapFree(procheap, 0, wm->modname);
232 HeapFree(procheap, 0, wm);
235 /* FIXME: We should traverse back in the recursion
236 * with an error to unload everything that got loaded
237 * before this error occurred.
238 * Too dificult for now though and we don't care at the
239 * moment. But, it *MUST* be implemented someday because
240 * we won't be able to map the elf-dll twice in this
241 * address-space which can cause some unexpected and
242 * weird problems later on.
250 /***********************************************************************
251 * ELFDLL_CreateNEModule
253 * Create a dummy NE module for the win32 elf-dll based on the supplied
254 * NE header in the elf-dll.
256 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);
264 FarSetOwner16(hModule, hModule);
265 pModule = (NE_MODULE *)GlobalLock16(hModule);
266 pModule->self = hModule;
267 NE_RegisterModule(pModule);
272 /****************************************************************************
273 * ELFDLL_LoadLibraryExA (internal)
275 * Implementation of elf-dll loading for PE modules
277 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR path, DWORD flags, DWORD *err)
280 struct elfdll_image *image;
286 get_sobasename(path, name);
287 strcpy(soname, name);
288 strcat(soname, ".so");
290 /* Try to open the elf-dll */
291 dlhandle = ELFDLL_dlopen(soname, RTLD_LAZY);
294 WARN_(elfdll)("Could not load %s (%s)\n", soname, dlerror());
295 *err = ERROR_FILE_NOT_FOUND;
299 /* Get the 'dllname_elfdll_image' variable */
300 strcpy(soname, name);
301 strcat(soname, "_elfdll_image");
302 image = (struct elfdll_image *)dlsym(dlhandle, soname);
305 ERR_(elfdll)("Could not get elfdll image descriptor %s (%s)\n", soname, dlerror());
307 *err = ERROR_BAD_FORMAT;
311 /* Create a win16 dummy module */
312 hmod16 = ELFDLL_CreateNEModule(image->ne_module_start, image->ne_module_size);
315 ERR_(elfdll)("Could not create win16 dummy module for %s\n", path);
317 *err = ERROR_OUTOFMEMORY;
321 image->ne_module_start->module32 = image->pe_module_start;
323 wm = ELFDLL_CreateModref(image->pe_module_start, path);
326 ERR_(elfdll)("Could not create WINE_MODREF for %s\n", path);
327 GLOBAL_FreeBlock((HGLOBAL16)hmod16);
329 *err = ERROR_OUTOFMEMORY;
333 dump_exports(image->pe_module_start);
340 /****************************************************************************
341 * ELFDLL_UnloadLibrary (internal)
343 * Unload an elf-dll completely from memory and deallocate the modref
345 void ELFDLL_UnloadLibrary(WINE_MODREF *wm)
350 /****************************************************************************
351 * ELFDLL_LoadModule16 (internal)
353 * Implementation of elf-dll loading for NE modules
355 HINSTANCE16 ELFDLL_LoadModule16(LPCSTR libname, BOOL implicit)
357 return (HINSTANCE16)ERROR_FILE_NOT_FOUND;
363 * No elfdlls possible
364 * Just put stubs in here.
367 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR libname, DWORD flags, DWORD *err)
369 *err = ERROR_FILE_NOT_FOUND;
373 void ELFDLL_UnloadLibrary(WINE_MODREF *wm)
377 HINSTANCE16 ELFDLL_LoadModule16(LPCSTR libname, BOOL implicit)
379 return (HINSTANCE16)ERROR_FILE_NOT_FOUND;