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 /*---------------- END HACKS ---------------*/
32 char *extra_ld_library_path = NULL; /* The extra search-path set in wine.conf */
36 HMODULE pe_module_start;
38 NE_MODULE *ne_module_start;
43 /****************************************************************************
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
50 void *ELFDLL_dlopen(const char *libname, int flags)
57 /* First try the default path search of dlopen() */
58 handle = dlopen(libname, flags);
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)
72 cptr = strchr(ldpath, ':');
84 if(len + namelen + 1 >= sizeof(buffer))
86 ERR_(elfdll)("Buffer overflow! Check EXTRA_LD_LIBRARY_PATH or increase buffer size.\n");
90 strncpy(buffer, from, len);
94 strcpy(buffer + len + 1, libname);
97 strcpy(buffer + len, libname);
99 TRACE_(elfdll)("Trying dlopen('%s', %d)\n", buffer, flags);
101 handle = dlopen(buffer, flags);
109 /****************************************************************************
110 * get_sobasename (internal)
113 static LPSTR get_sobasename(LPCSTR path, LPSTR name)
117 /* Strip the path from the library name */
118 if((cptr = strrchr(path, '/')))
120 char *cp = strrchr(cptr+1, '\\');
125 cptr = strrchr(path, '\\');
128 cptr = (char *)path; /* No '/' nor '\\' in path */
133 cptr = strrchr(name, '.');
135 *cptr = '\0'; /* Strip extension */
137 /* Convert to lower case.
138 * This must be done manually because it is not sure that
139 * other modules are accessible.
141 for(cptr = name; *cptr; cptr++)
142 *cptr = tolower(*cptr);
148 /****************************************************************************
149 * ELFDLL_CreateModref (internal)
152 * hModule - the header from the elf-dll's data-segment
153 * path - requested path from original call
156 * A WINE_MODREF pointer to the new object
159 * - Does not handle errors due to dependencies correctly
160 * - path can be wrong
162 #define RVA(base, va) (((DWORD)base) + ((DWORD)va))
164 static WINE_MODREF *ELFDLL_CreateModref(HMODULE hModule, LPCSTR path)
166 IMAGE_NT_HEADERS *nt = PE_HEADER(hModule);
167 IMAGE_DATA_DIRECTORY *dir;
168 IMAGE_IMPORT_DESCRIPTOR *pe_import = NULL;
171 HANDLE procheap = GetProcessHeap();
173 wm = (WINE_MODREF *)HeapAlloc(procheap, HEAP_ZERO_MEMORY, sizeof(*wm));
177 wm->module = hModule;
178 wm->type = MODULE32_PE; /* FIXME */
180 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXPORT;
182 wm->binfmt.pe.pe_export = (PIMAGE_EXPORT_DIRECTORY)RVA(hModule, dir->VirtualAddress);
184 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IMPORT;
186 pe_import = wm->binfmt.pe.pe_import = (PIMAGE_IMPORT_DESCRIPTOR)RVA(hModule, dir->VirtualAddress);
188 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
190 wm->binfmt.pe.pe_resource = (PIMAGE_RESOURCE_DIRECTORY)RVA(hModule, dir->VirtualAddress);
192 wm->modname = HEAP_strdupA(procheap, 0, (char *)RVA(hModule, wm->binfmt.pe.pe_export->Name));
194 len = GetLongPathNameA(path, NULL, 0);
195 wm->longname = (char *)HeapAlloc(procheap, 0, len+1);
196 GetLongPathNameA(path, wm->longname, len+1);
198 wm->shortname = HEAP_strdupA(procheap, 0, path);
200 /* Link MODREF into process list */
201 wm->next = PROCESS_Current()->modref_list;
202 PROCESS_Current()->modref_list = wm;
204 if(!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
206 if(PROCESS_Current()->exe_modref)
207 FIXME_(elfdll)("overwriting old exe_modref... arrgh\n");
208 PROCESS_Current()->exe_modref = wm;
212 if(pe_import && fixup_imports(wm))
214 /* Error in this module or its dependencies
215 * remove entry from modref chain
218 for(xwm = &(PROCESS_Current()->modref_list); *xwm; xwm = &((*xwm)->next))
226 if(wm == PROCESS_Current()->exe_modref)
227 ERR_(elfdll)("Have to delete current exe_modref. Expect crash now\n");
228 HeapFree(procheap, 0, wm->shortname);
229 HeapFree(procheap, 0, wm->longname);
230 HeapFree(procheap, 0, wm->modname);
231 HeapFree(procheap, 0, wm);
234 /* FIXME: We should traverse back in the recursion
235 * with an error to unload everything that got loaded
236 * before this error occurred.
237 * Too dificult for now though and we don't care at the
238 * moment. But, it *MUST* be implemented someday because
239 * we won't be able to map the elf-dll twice in this
240 * address-space which can cause some unexpected and
241 * weird problems later on.
249 /***********************************************************************
250 * ELFDLL_CreateNEModule
252 * Create a dummy NE module for the win32 elf-dll based on the supplied
253 * NE header in the elf-dll.
255 static HMODULE16 ELFDLL_CreateNEModule(NE_MODULE *ne_image, DWORD size)
257 HMODULE16 hModule = GLOBAL_CreateBlock(GMEM_MOVEABLE, ne_image, size, 0,
258 FALSE, FALSE, FALSE, NULL);
262 FarSetOwner16(hModule, hModule);
263 NE_RegisterModule(ne_image);
268 /****************************************************************************
269 * ELFDLL_LoadLibraryExA (internal)
271 * Implementation of elf-dll loading for PE modules
273 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR path, DWORD flags, DWORD *err)
276 struct elfdll_image *image;
282 get_sobasename(path, name);
283 strcpy(soname, name);
284 strcat(soname, ".so");
286 /* Try to open the elf-dll */
287 dlhandle = ELFDLL_dlopen(soname, RTLD_LAZY);
290 WARN_(elfdll)("Could not load %s (%s)\n", soname, dlerror());
291 *err = ERROR_FILE_NOT_FOUND;
295 /* Get the 'dllname_elfdll_image' variable */
296 strcpy(soname, name);
297 strcat(soname, "_elfdll_image");
298 image = (struct elfdll_image *)dlsym(dlhandle, soname);
301 ERR_(elfdll)("Could not get elfdll image descriptor %s (%s)\n", soname, dlerror());
303 *err = ERROR_BAD_FORMAT;
307 /* Create a win16 dummy module */
308 hmod16 = ELFDLL_CreateNEModule(image->ne_module_start, image->ne_module_size);
311 ERR_(elfdll)("Could not create win16 dummy module for %s\n", path);
313 *err = ERROR_OUTOFMEMORY;
317 image->ne_module_start->module32 = image->pe_module_start;
319 wm = ELFDLL_CreateModref(image->pe_module_start, path);
322 ERR_(elfdll)("Could not create WINE_MODREF for %s\n", path);
323 GLOBAL_FreeBlock((HGLOBAL16)hmod16);
325 *err = ERROR_OUTOFMEMORY;
334 /****************************************************************************
335 * ELFDLL_UnloadLibrary (internal)
337 * Unload an elf-dll completely from memory and deallocate the modref
339 void ELFDLL_UnloadLibrary(WINE_MODREF *wm)
344 /****************************************************************************
345 * ELFDLL_LoadModule16 (internal)
347 * Implementation of elf-dll loading for NE modules
349 HINSTANCE16 ELFDLL_LoadModule16(LPCSTR libname, BOOL implicit)
351 return (HINSTANCE16)ERROR_FILE_NOT_FOUND;
357 * No elfdlls possible
358 * Just put stubs in here.
361 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR libname, DWORD flags, DWORD *err)
363 *err = ERROR_FILE_NOT_FOUND;
367 void ELFDLL_UnloadLibrary(WINE_MODREF *wm)
371 HINSTANCE16 ELFDLL_LoadModule16(LPCSTR libname, BOOL implicit)
373 return (HINSTANCE16)ERROR_FILE_NOT_FOUND;