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"
23 //DEFAULT_DEBUG_CHANNEL(elfdll)
32 //WINE_MODREF *local_wm=NULL;
33 extern modref_list
* local_wm
;
36 /*------------------ HACKS -----------------*/
37 DWORD
fixup_imports(WINE_MODREF
*wm
);
38 void dump_exports(HMODULE hModule
);
39 /*---------------- END HACKS ---------------*/
43 HMODULE pe_module_start
;
48 /****************************************************************************
51 * Wrapper for dlopen to search the EXTRA_LD_LIBRARY_PATH from wine.conf
52 * manually because libdl.so caches the environment and does not accept our
55 void *ELFDLL_dlopen(const char *libname
, int flags
)
62 /* First try the default path search of dlopen() */
63 handle
= dlopen(libname
, flags
);
67 /* Now try to construct searches through our extra search-path */
68 namelen
= strlen(libname
);
70 while(ldpath
&& *ldpath
)
77 cptr
= strchr(ldpath
, ':');
89 if(len
+ namelen
+ 1 >= sizeof(buffer
))
91 ERR("Buffer overflow! Check EXTRA_LD_LIBRARY_PATH or increase buffer size.\n");
95 strncpy(buffer
, from
, len
);
99 strcpy(buffer
+ len
+ 1, libname
);
102 strcpy(buffer
+ len
, libname
);
104 TRACE("Trying dlopen('%s', %d)\n", buffer
, flags
);
106 handle
= dlopen(buffer
, flags
);
114 /****************************************************************************
115 * get_sobasename (internal)
118 static LPSTR
get_sobasename(LPCSTR path
, LPSTR name
)
122 /* Strip the path from the library name */
123 if((cptr
= strrchr(path
, '/')))
125 char *cp
= strrchr(cptr
+1, '\\');
130 cptr
= strrchr(path
, '\\');
133 cptr
= (char *)path
; /* No '/' nor '\\' in path */
138 cptr
= strrchr(name
, '.');
140 *cptr
= '\0'; /* Strip extension */
142 /* Convert to lower case.
143 * This must be done manually because it is not sure that
144 * other modules are accessible.
146 for(cptr
= name
; *cptr
; cptr
++)
147 *cptr
= tolower(*cptr
);
153 /****************************************************************************
154 * ELFDLL_CreateModref (internal)
157 * hModule - the header from the elf-dll's data-segment
158 * path - requested path from original call
161 * A WINE_MODREF pointer to the new object
164 * - Does not handle errors due to dependencies correctly
165 * - path can be wrong
167 #define RVA(base, va) (((DWORD)base) + ((DWORD)va))
169 static WINE_MODREF
*ELFDLL_CreateModref(HMODULE hModule
, LPCSTR path
)
171 // IMAGE_NT_HEADERS *nt = PE_HEADER(hModule);
172 // IMAGE_DATA_DIRECTORY *dir;
173 // IMAGE_IMPORT_DESCRIPTOR *pe_import = NULL;
176 HANDLE procheap
= GetProcessHeap();
178 wm
= (WINE_MODREF
*)HeapAlloc(procheap
, HEAP_ZERO_MEMORY
, sizeof(*wm
));
182 wm
->module
= hModule
;
183 wm
->type
= MODULE32_ELF
; /* FIXME */
185 // dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXPORT;
187 // wm->binfmt.pe.pe_export = (PIMAGE_EXPORT_DIRECTORY)RVA(hModule, dir->VirtualAddress);
189 // dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IMPORT;
191 // pe_import = wm->binfmt.pe.pe_import = (PIMAGE_IMPORT_DESCRIPTOR)RVA(hModule, dir->VirtualAddress);
193 // dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
195 // wm->binfmt.pe.pe_resource = (PIMAGE_RESOURCE_DIRECTORY)RVA(hModule, dir->VirtualAddress);
198 wm
->filename
= malloc(strlen(path
)+1);
199 strcpy(wm
->filename
, path
);
200 wm
->modname
= strrchr( wm
->filename
, '\\' );
201 if (!wm
->modname
) wm
->modname
= wm
->filename
;
204 len = GetShortPathNameA( wm->filename, NULL, 0 );
205 wm->short_filename = (char *)HeapAlloc( procheap, 0, len+1 );
206 GetShortPathNameA( wm->filename, wm->short_filename, len+1 );
207 wm->short_modname = strrchr( wm->short_filename, '\\' );
208 if (!wm->short_modname) wm->short_modname = wm->short_filename;
209 else wm->short_modname++;
211 /* Link MODREF into process list */
213 // EnterCriticalSection( &PROCESS_Current()->crit_section );
217 local_wm
->next
= malloc(sizeof(modref_list
));
218 local_wm
->next
->prev
=local_wm
;
219 local_wm
->next
->next
=NULL
;
220 local_wm
->next
->wm
=wm
;
221 local_wm
=local_wm
->next
;
225 local_wm
= malloc(sizeof(modref_list
));
226 local_wm
->next
=local_wm
->prev
=NULL
;
230 // LeaveCriticalSection( &PROCESS_Current()->crit_section );
234 /****************************************************************************
235 * ELFDLL_LoadLibraryExA (internal)
237 * Implementation of elf-dll loading for PE modules
239 WINE_MODREF
*ELFDLL_LoadLibraryExA(LPCSTR path
, DWORD flags
)
242 // struct elfdll_image *image;
247 get_sobasename(path
, name
);
248 strcpy(soname
, name
);
249 strcat(soname
, ".so");
251 /* Try to open the elf-dll */
252 dlhandle
= ELFDLL_dlopen(soname
, RTLD_LAZY
);
255 WARN("Could not load %s (%s)\n", soname
, dlerror());
256 SetLastError( ERROR_FILE_NOT_FOUND
);
260 /* Get the 'dllname_elfdll_image' variable */
261 /* strcpy(soname, name);
262 strcat(soname, "_elfdll_image");
263 image = (struct elfdll_image *)dlsym(dlhandle, soname);
266 ERR("Could not get elfdll image descriptor %s (%s)\n", soname, dlerror());
268 SetLastError( ERROR_BAD_FORMAT );
273 wm
= ELFDLL_CreateModref((int)dlhandle
, path
);
276 ERR("Could not create WINE_MODREF for %s\n", path
);
278 SetLastError( ERROR_OUTOFMEMORY
);
286 /****************************************************************************
287 * ELFDLL_UnloadLibrary (internal)
289 * Unload an elf-dll completely from memory and deallocate the modref
291 void ELFDLL_UnloadLibrary(WINE_MODREF
*wm
)
295 #endif /*HAVE_LIBDL*/