2 * Elf-dll loader functions
4 * Copyright 1999 Bertho A. Stultiens
18 #include "wine/winbase16.h"
20 #include "debugtools.h"
23 DEFAULT_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("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("Trying dlopen('%s', %d)\n", buffer
, flags
);
102 handle
= dlopen(buffer
, flags
);
106 TRACE("possible dlopen() error: %s\n", dlerror());
112 /****************************************************************************
113 * get_sobasename (internal)
116 static LPSTR
get_sobasename(LPCSTR path
, LPSTR name
)
120 /* Strip the path from the library name */
121 if((cptr
= strrchr(path
, '/')))
123 char *cp
= strrchr(cptr
+1, '\\');
128 cptr
= strrchr(path
, '\\');
131 cptr
= (char *)path
; /* No '/' nor '\\' in path */
136 cptr
= strrchr(name
, '.');
138 *cptr
= '\0'; /* Strip extension */
140 /* Convert to lower case.
141 * This must be done manually because it is not sure that
142 * other modules are accessible.
144 for(cptr
= name
; *cptr
; cptr
++)
145 *cptr
= tolower(*cptr
);
151 /****************************************************************************
152 * ELFDLL_CreateModref (internal)
155 * hModule - the header from the elf-dll's data-segment
156 * path - requested path from original call
159 * A WINE_MODREF pointer to the new object
162 * - Does not handle errors due to dependencies correctly
163 * - path can be wrong
165 #define RVA(base, va) (((DWORD)base) + ((DWORD)va))
167 static WINE_MODREF
*ELFDLL_CreateModref(HMODULE hModule
, LPCSTR path
)
169 IMAGE_NT_HEADERS
*nt
= PE_HEADER(hModule
);
170 IMAGE_DATA_DIRECTORY
*dir
;
171 IMAGE_IMPORT_DESCRIPTOR
*pe_import
= NULL
;
174 HANDLE procheap
= GetProcessHeap();
176 wm
= (WINE_MODREF
*)HeapAlloc(procheap
, HEAP_ZERO_MEMORY
, sizeof(*wm
));
180 wm
->module
= hModule
;
181 wm
->type
= MODULE32_PE
; /* FIXME */
183 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_EXPORT
;
185 wm
->binfmt
.pe
.pe_export
= (PIMAGE_EXPORT_DIRECTORY
)RVA(hModule
, dir
->VirtualAddress
);
187 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_IMPORT
;
189 pe_import
= wm
->binfmt
.pe
.pe_import
= (PIMAGE_IMPORT_DESCRIPTOR
)RVA(hModule
, dir
->VirtualAddress
);
191 dir
= nt
->OptionalHeader
.DataDirectory
+IMAGE_DIRECTORY_ENTRY_RESOURCE
;
193 wm
->binfmt
.pe
.pe_resource
= (PIMAGE_RESOURCE_DIRECTORY
)RVA(hModule
, dir
->VirtualAddress
);
196 wm
->filename
= HEAP_strdupA( procheap
, 0, path
);
197 wm
->modname
= strrchr( wm
->filename
, '\\' );
198 if (!wm
->modname
) wm
->modname
= wm
->filename
;
201 len
= GetShortPathNameA( wm
->filename
, NULL
, 0 );
202 wm
->short_filename
= (char *)HeapAlloc( procheap
, 0, len
+1 );
203 GetShortPathNameA( wm
->filename
, wm
->short_filename
, len
+1 );
204 wm
->short_modname
= strrchr( wm
->short_filename
, '\\' );
205 if (!wm
->short_modname
) wm
->short_modname
= wm
->short_filename
;
206 else wm
->short_modname
++;
208 /* Link MODREF into process list */
210 EnterCriticalSection( &PROCESS_Current()->crit_section
);
212 wm
->next
= PROCESS_Current()->modref_list
;
213 PROCESS_Current()->modref_list
= wm
;
214 if ( wm
->next
) wm
->next
->prev
= wm
;
216 if ( !( nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
217 && !( wm
->flags
& WINE_MODREF_LOAD_AS_DATAFILE
) )
220 if ( PROCESS_Current()->exe_modref
)
221 FIXME( "Trying to load second .EXE file: %s\n", path
);
223 PROCESS_Current()->exe_modref
= wm
;
226 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
231 && !( wm
->flags
& WINE_MODREF_LOAD_AS_DATAFILE
)
232 && !( wm
->flags
& WINE_MODREF_DONT_RESOLVE_REFS
)
233 && fixup_imports( wm
) )
235 /* remove entry from modref chain */
236 EnterCriticalSection( &PROCESS_Current()->crit_section
);
239 PROCESS_Current()->modref_list
= wm
->next
;
241 wm
->prev
->next
= wm
->next
;
243 if ( wm
->next
) wm
->next
->prev
= wm
->prev
;
244 wm
->next
= wm
->prev
= NULL
;
246 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
248 /* FIXME: there are several more dangling references
249 * left. Including dlls loaded by this dll before the
250 * failed one. Unrolling is rather difficult with the
251 * current structure and we can leave it them lying
252 * around with no problems, so we don't care.
253 * As these might reference our wm, we don't free it.
262 /***********************************************************************
263 * ELFDLL_CreateNEModule
265 * Create a dummy NE module for the win32 elf-dll based on the supplied
266 * NE header in the elf-dll.
268 static HMODULE16
ELFDLL_CreateNEModule(NE_MODULE
*ne_image
, DWORD size
)
271 HMODULE16 hModule
= GLOBAL_CreateBlock(GMEM_MOVEABLE
, ne_image
, size
, 0,
272 FALSE
, FALSE
, FALSE
, NULL
);
276 FarSetOwner16(hModule
, hModule
);
277 pModule
= (NE_MODULE
*)GlobalLock16(hModule
);
278 pModule
->self
= hModule
;
279 NE_RegisterModule(pModule
);
284 /****************************************************************************
285 * ELFDLL_LoadLibraryExA (internal)
287 * Implementation of elf-dll loading for PE modules
289 WINE_MODREF
*ELFDLL_LoadLibraryExA(LPCSTR path
, DWORD flags
)
292 struct elfdll_image
*image
;
298 get_sobasename(path
, name
);
299 strcpy(soname
, name
);
300 strcat(soname
, ".so");
302 /* Try to open the elf-dll */
303 dlhandle
= ELFDLL_dlopen(soname
, RTLD_LAZY
);
306 WARN("Could not load %s (%s)\n", soname
, dlerror());
307 SetLastError( ERROR_FILE_NOT_FOUND
);
311 /* Get the 'dllname_elfdll_image' variable */
312 strcpy(soname
, name
);
313 strcat(soname
, "_elfdll_image");
314 image
= (struct elfdll_image
*)dlsym(dlhandle
, soname
);
317 ERR("Could not get elfdll image descriptor %s (%s)\n", soname
, dlerror());
319 SetLastError( ERROR_BAD_FORMAT
);
323 /* Create a win16 dummy module */
324 hmod16
= ELFDLL_CreateNEModule(image
->ne_module_start
, image
->ne_module_size
);
327 ERR("Could not create win16 dummy module for %s\n", path
);
329 SetLastError( ERROR_OUTOFMEMORY
);
333 image
->ne_module_start
->module32
= image
->pe_module_start
;
335 wm
= ELFDLL_CreateModref(image
->pe_module_start
, path
);
338 ERR("Could not create WINE_MODREF for %s\n", path
);
339 GLOBAL_FreeBlock((HGLOBAL16
)hmod16
);
341 SetLastError( ERROR_OUTOFMEMORY
);
345 dump_exports(image
->pe_module_start
);
350 /****************************************************************************
351 * ELFDLL_UnloadLibrary (internal)
353 * Unload an elf-dll completely from memory and deallocate the modref
355 void ELFDLL_UnloadLibrary(WINE_MODREF
*wm
)
360 /****************************************************************************
361 * ELFDLL_LoadModule16 (internal)
363 * Implementation of elf-dll loading for NE modules
365 HINSTANCE16
ELFDLL_LoadModule16(LPCSTR libname
)
367 return (HINSTANCE16
)ERROR_FILE_NOT_FOUND
;
373 * No elfdlls possible
374 * Just put stubs in here.
377 WINE_MODREF
*ELFDLL_LoadLibraryExA(LPCSTR libname
, DWORD flags
)
379 SetLastError( ERROR_FILE_NOT_FOUND
);
383 void ELFDLL_UnloadLibrary(WINE_MODREF
*wm
)
387 HINSTANCE16
ELFDLL_LoadModule16(LPCSTR libname
)
389 return (HINSTANCE16
)ERROR_FILE_NOT_FOUND
;