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, '\\');
127 cptr
= strrchr(path
, '\\');
130 cptr
= (char *)path
; /* No '/' nor '\\' in path */
135 cptr
= strrchr(name
, '.');
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
);
150 /****************************************************************************
151 * ELFDLL_CreateModref (internal)
154 * hModule - the header from the elf-dll's data-segment
155 * path - requested path from original call
158 * A WINE_MODREF pointer to the new object
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
;
173 HANDLE procheap
= GetProcessHeap();
175 wm
= (WINE_MODREF
*)HeapAlloc(procheap
, HEAP_ZERO_MEMORY
, sizeof(*wm
));
179 wm
->module
= hModule
;
180 wm
->type
= MODULE32_PE
; /* FIXME */
182 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_EXPORT
;
184 wm
->binfmt
.pe
.pe_export
= (PIMAGE_EXPORT_DIRECTORY
)RVA(hModule
, dir
->VirtualAddress
);
186 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_IMPORT
;
188 pe_import
= wm
->binfmt
.pe
.pe_import
= (PIMAGE_IMPORT_DESCRIPTOR
)RVA(hModule
, dir
->VirtualAddress
);
190 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_RESOURCE
;
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
;
214 if(pe_import
&& fixup_imports(wm
))
216 /* Error in this module or its dependencies
217 * remove entry from modref chain
220 for(xwm
= &(PROCESS_Current()->modref_list
); *xwm
; xwm
= &((*xwm
)->next
))
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
);
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.
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
);
264 FarSetOwner16(hModule
, hModule
);
265 NE_RegisterModule(ne_image
);
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
)
278 struct elfdll_image
*image
;
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
);
292 WARN_(elfdll
)("Could not load %s (%s)\n", soname
, dlerror());
293 *err
= ERROR_FILE_NOT_FOUND
;
297 /* Get the 'dllname_elfdll_image' variable */
298 strcpy(soname
, name
);
299 strcat(soname
, "_elfdll_image");
300 image
= (struct elfdll_image
*)dlsym(dlhandle
, soname
);
303 ERR_(elfdll
)("Could not get elfdll image descriptor %s (%s)\n", soname
, dlerror());
305 *err
= ERROR_BAD_FORMAT
;
309 /* Create a win16 dummy module */
310 hmod16
= ELFDLL_CreateNEModule(image
->ne_module_start
, image
->ne_module_size
);
313 ERR_(elfdll
)("Could not create win16 dummy module for %s\n", path
);
315 *err
= ERROR_OUTOFMEMORY
;
319 image
->ne_module_start
->module32
= image
->pe_module_start
;
321 wm
= ELFDLL_CreateModref(image
->pe_module_start
, path
);
324 ERR_(elfdll
)("Could not create WINE_MODREF for %s\n", path
);
325 GLOBAL_FreeBlock((HGLOBAL16
)hmod16
);
327 *err
= ERROR_OUTOFMEMORY
;
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
;
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
;
369 void ELFDLL_UnloadLibrary(WINE_MODREF
*wm
)
373 HINSTANCE16
ELFDLL_LoadModule16(LPCSTR libname
, BOOL implicit
)
375 return (HINSTANCE16
)ERROR_FILE_NOT_FOUND
;