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"
24 //DEFAULT_DEBUG_CHANNEL(elfdll)
33 /*------------------ HACKS -----------------*/
34 DWORD
fixup_imports(WINE_MODREF
*wm
);
35 void dump_exports(HMODULE hModule
);
36 /*---------------- END HACKS ---------------*/
40 HMODULE pe_module_start
;
45 /****************************************************************************
48 * Wrapper for dlopen to search the EXTRA_LD_LIBRARY_PATH from wine.conf
49 * manually because libdl.so caches the environment and does not accept our
52 void *ELFDLL_dlopen(const char *libname
, int flags
)
59 /* First try the default path search of dlopen() */
60 handle
= dlopen(libname
, flags
);
64 /* Now try to construct searches through our extra search-path */
65 namelen
= strlen(libname
);
67 while(ldpath
&& *ldpath
)
74 cptr
= strchr(ldpath
, ':');
86 if(len
+ namelen
+ 1 >= sizeof(buffer
))
88 ERR("Buffer overflow! Check EXTRA_LD_LIBRARY_PATH or increase buffer size.\n");
92 strncpy(buffer
, from
, len
);
96 strcpy(buffer
+ len
+ 1, libname
);
99 strcpy(buffer
+ len
, libname
);
101 TRACE("Trying dlopen('%s', %d)\n", buffer
, flags
);
103 handle
= dlopen(buffer
, flags
);
111 /****************************************************************************
112 * get_sobasename (internal)
115 static LPSTR
get_sobasename(LPCSTR path
, LPSTR name
)
119 /* Strip the path from the library name */
120 if((cptr
= strrchr(path
, '/')))
122 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_ELF
; /* 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);
195 wm
->filename
= malloc(strlen(path
)+1);
196 strcpy(wm
->filename
, 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 );
214 local_wm
->next
= malloc(sizeof(modref_list
));
215 local_wm
->next
->prev
=local_wm
;
216 local_wm
->next
->next
=NULL
;
217 local_wm
->next
->wm
=wm
;
218 local_wm
=local_wm
->next
;
222 local_wm
= malloc(sizeof(modref_list
));
223 local_wm
->next
=local_wm
->prev
=NULL
;
227 // LeaveCriticalSection( &PROCESS_Current()->crit_section );
231 /****************************************************************************
232 * ELFDLL_LoadLibraryExA (internal)
234 * Implementation of elf-dll loading for PE modules
236 WINE_MODREF
*ELFDLL_LoadLibraryExA(LPCSTR path
, DWORD flags
)
239 // struct elfdll_image *image;
244 get_sobasename(path
, name
);
245 strcpy(soname
, name
);
246 strcat(soname
, ".so");
248 /* Try to open the elf-dll */
249 dlhandle
= ELFDLL_dlopen(soname
, RTLD_LAZY
);
252 WARN("Could not load %s (%s)\n", soname
, dlerror());
253 SetLastError( ERROR_FILE_NOT_FOUND
);
257 /* Get the 'dllname_elfdll_image' variable */
258 /* strcpy(soname, name);
259 strcat(soname, "_elfdll_image");
260 image = (struct elfdll_image *)dlsym(dlhandle, soname);
263 ERR("Could not get elfdll image descriptor %s (%s)\n", soname, dlerror());
265 SetLastError( ERROR_BAD_FORMAT );
270 wm
= ELFDLL_CreateModref((int)dlhandle
, path
);
273 ERR("Could not create WINE_MODREF for %s\n", path
);
275 SetLastError( ERROR_OUTOFMEMORY
);
283 /****************************************************************************
284 * ELFDLL_UnloadLibrary (internal)
286 * Unload an elf-dll completely from memory and deallocate the modref
288 void ELFDLL_UnloadLibrary(WINE_MODREF
*wm
)
292 #endif /*HAVE_LIBDL*/