2 * Elf-dll loader functions
4 * Copyright 1999 Bertho A. Stultiens
6 * Modified for use with MPlayer, detailed changelog at
7 * http://svn.mplayerhq.hu/mplayer/trunk/
14 #include "wine/windef.h"
15 #include "wine/module.h"
16 #include "wine/heap.h"
17 #include "wine/elfdll.h"
18 #include "wine/debugtools.h"
19 #include "wine/winerror.h"
22 //DEFAULT_DEBUG_CHANNEL(elfdll)
31 //WINE_MODREF *local_wm=NULL;
32 extern modref_list
* local_wm
;
35 /*------------------ HACKS -----------------*/
36 DWORD
fixup_imports(WINE_MODREF
*wm
);
37 void dump_exports(HMODULE hModule
);
38 /*---------------- END HACKS ---------------*/
40 //char *extra_ld_library_path = "/usr/lib/win32";
41 extern char* def_path
;
45 HMODULE pe_module_start
;
50 /****************************************************************************
53 * Wrapper for dlopen to search the EXTRA_LD_LIBRARY_PATH from wine.conf
54 * manually because libdl.so caches the environment and does not accept our
57 void *ELFDLL_dlopen(const char *libname
, int flags
)
64 /* First try the default path search of dlopen() */
65 handle
= dlopen(libname
, flags
);
69 /* Now try to construct searches through our extra search-path */
70 namelen
= strlen(libname
);
72 while(ldpath
&& *ldpath
)
79 cptr
= strchr(ldpath
, ':');
91 if(len
+ namelen
+ 1 >= sizeof(buffer
))
93 ERR("Buffer overflow! Check EXTRA_LD_LIBRARY_PATH or increase buffer size.\n");
97 strncpy(buffer
, from
, len
);
101 strcpy(buffer
+ len
+ 1, libname
);
104 strcpy(buffer
+ len
, libname
);
106 TRACE("Trying dlopen('%s', %d)\n", buffer
, flags
);
108 handle
= dlopen(buffer
, flags
);
116 /****************************************************************************
117 * get_sobasename (internal)
120 static LPSTR
get_sobasename(LPCSTR path
, LPSTR name
)
124 /* Strip the path from the library name */
125 if((cptr
= strrchr(path
, '/')))
127 char *cp
= strrchr(cptr
+1, '\\');
132 cptr
= strrchr(path
, '\\');
135 cptr
= (char *)path
; /* No '/' nor '\\' in path */
140 cptr
= strrchr(name
, '.');
142 *cptr
= '\0'; /* Strip extension */
144 /* Convert to lower case.
145 * This must be done manually because it is not sure that
146 * other modules are accessible.
148 for(cptr
= name
; *cptr
; cptr
++)
149 *cptr
= tolower(*cptr
);
155 /****************************************************************************
156 * ELFDLL_CreateModref (internal)
159 * hModule - the header from the elf-dll's data-segment
160 * path - requested path from original call
163 * A WINE_MODREF pointer to the new object
166 * - Does not handle errors due to dependencies correctly
167 * - path can be wrong
169 #define RVA(base, va) (((DWORD)base) + ((DWORD)va))
171 static WINE_MODREF
*ELFDLL_CreateModref(HMODULE hModule
, LPCSTR path
)
173 // IMAGE_NT_HEADERS *nt = PE_HEADER(hModule);
174 // IMAGE_DATA_DIRECTORY *dir;
175 // IMAGE_IMPORT_DESCRIPTOR *pe_import = NULL;
178 HANDLE procheap
= GetProcessHeap();
180 wm
= (WINE_MODREF
*)HeapAlloc(procheap
, HEAP_ZERO_MEMORY
, sizeof(*wm
));
184 wm
->module
= hModule
;
185 wm
->type
= MODULE32_ELF
; /* FIXME */
187 // dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXPORT;
189 // wm->binfmt.pe.pe_export = (PIMAGE_EXPORT_DIRECTORY)RVA(hModule, dir->VirtualAddress);
191 // dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IMPORT;
193 // pe_import = wm->binfmt.pe.pe_import = (PIMAGE_IMPORT_DESCRIPTOR)RVA(hModule, dir->VirtualAddress);
195 // dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
197 // wm->binfmt.pe.pe_resource = (PIMAGE_RESOURCE_DIRECTORY)RVA(hModule, dir->VirtualAddress);
200 wm
->filename
= (char*) malloc(strlen(path
)+1);
201 strcpy(wm
->filename
, path
);
202 wm
->modname
= strrchr( wm
->filename
, '\\' );
203 if (!wm
->modname
) wm
->modname
= wm
->filename
;
206 len = GetShortPathNameA( wm->filename, NULL, 0 );
207 wm->short_filename = (char *)HeapAlloc( procheap, 0, len+1 );
208 GetShortPathNameA( wm->filename, wm->short_filename, len+1 );
209 wm->short_modname = strrchr( wm->short_filename, '\\' );
210 if (!wm->short_modname) wm->short_modname = wm->short_filename;
211 else wm->short_modname++;
213 /* Link MODREF into process list */
215 // EnterCriticalSection( &PROCESS_Current()->crit_section );
219 local_wm
->next
= (modref_list
*) malloc(sizeof(modref_list
));
220 local_wm
->next
->prev
=local_wm
;
221 local_wm
->next
->next
=NULL
;
222 local_wm
->next
->wm
=wm
;
223 local_wm
=local_wm
->next
;
227 local_wm
= (modref_list
*) malloc(sizeof(modref_list
));
228 local_wm
->next
=local_wm
->prev
=NULL
;
232 // LeaveCriticalSection( &PROCESS_Current()->crit_section );
236 /****************************************************************************
237 * ELFDLL_LoadLibraryExA (internal)
239 * Implementation of elf-dll loading for PE modules
241 WINE_MODREF
*ELFDLL_LoadLibraryExA(LPCSTR path
, DWORD flags
)
244 // struct elfdll_image *image;
249 get_sobasename(path
, name
);
250 strcpy(soname
, name
);
251 strcat(soname
, ".so");
253 /* Try to open the elf-dll */
254 dlhandle
= ELFDLL_dlopen(soname
, RTLD_LAZY
);
257 WARN("Could not load %s (%s)\n", soname
, dlerror());
258 SetLastError( ERROR_FILE_NOT_FOUND
);
262 /* Get the 'dllname_elfdll_image' variable */
263 /* strcpy(soname, name);
264 strcat(soname, "_elfdll_image");
265 image = (struct elfdll_image *)dlsym(dlhandle, soname);
268 ERR("Could not get elfdll image descriptor %s (%s)\n", soname, dlerror());
270 SetLastError( ERROR_BAD_FORMAT );
275 wm
= ELFDLL_CreateModref((int)dlhandle
, path
);
278 ERR("Could not create WINE_MODREF for %s\n", path
);
280 SetLastError( ERROR_OUTOFMEMORY
);
288 /****************************************************************************
289 * ELFDLL_UnloadLibrary (internal)
291 * Unload an elf-dll completely from memory and deallocate the modref
293 void ELFDLL_UnloadLibrary(WINE_MODREF
*wm
)
297 #endif /*HAVE_LIBDL*/