convert some printfs to mp_msg
[mplayer/glamo.git] / loader / elfdll.c
blob1c19235d9ecafb9edf67998c606d68b292aef805
1 /*
2 * Elf-dll loader functions
4 * Copyright 1999 Bertho A. Stultiens
6 * Modified for use with MPlayer, detailed CVS changelog at
7 * http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
8 * $Id$
11 #include "config.h"
13 #ifdef HAVE_LIBDL
15 #include "wine/windef.h"
16 #include "wine/module.h"
17 #include "wine/heap.h"
18 #include "wine/elfdll.h"
19 #include "wine/debugtools.h"
20 #include "wine/winerror.h"
22 //DEFAULT_DEBUG_CHANNEL(elfdll)
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <dlfcn.h>
31 //WINE_MODREF *local_wm=NULL;
32 extern modref_list* local_wm;
35 /*------------------ HACKS -----------------*/
36 extern DWORD fixup_imports(WINE_MODREF *wm);
37 extern void dump_exports(HMODULE hModule);
38 /*---------------- END HACKS ---------------*/
40 //char *extra_ld_library_path = "/usr/lib/win32";
41 extern char* def_path;
43 struct elfdll_image
45 HMODULE pe_module_start;
46 DWORD pe_module_size;
50 /****************************************************************************
51 * ELFDLL_dlopen
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
55 * changes.
57 void *ELFDLL_dlopen(const char *libname, int flags)
59 char buffer[256];
60 int namelen;
61 void *handle;
62 char *ldpath;
64 /* First try the default path search of dlopen() */
65 handle = dlopen(libname, flags);
66 if(handle)
67 return handle;
69 /* Now try to construct searches through our extra search-path */
70 namelen = strlen(libname);
71 ldpath = def_path;
72 while(ldpath && *ldpath)
74 int len;
75 char *cptr;
76 char *from;
78 from = ldpath;
79 cptr = strchr(ldpath, ':');
80 if(!cptr)
82 len = strlen(ldpath);
83 ldpath = NULL;
85 else
87 len = cptr - ldpath;
88 ldpath = cptr + 1;
91 if(len + namelen + 1 >= sizeof(buffer))
93 ERR("Buffer overflow! Check EXTRA_LD_LIBRARY_PATH or increase buffer size.\n");
94 return NULL;
97 strncpy(buffer, from, len);
98 if(len)
100 buffer[len] = '/';
101 strcpy(buffer + len + 1, libname);
103 else
104 strcpy(buffer + len, libname);
106 TRACE("Trying dlopen('%s', %d)\n", buffer, flags);
108 handle = dlopen(buffer, flags);
109 if(handle)
110 return handle;
112 return NULL;
116 /****************************************************************************
117 * get_sobasename (internal)
120 static LPSTR get_sobasename(LPCSTR path, LPSTR name)
122 char *cptr;
124 /* Strip the path from the library name */
125 if((cptr = strrchr(path, '/')))
127 char *cp = strrchr(cptr+1, '\\');
128 if(cp && cp > cptr)
129 cptr = cp;
131 else
132 cptr = strrchr(path, '\\');
134 if(!cptr)
135 cptr = (char *)path; /* No '/' nor '\\' in path */
136 else
137 cptr++;
139 strcpy(name, cptr);
140 cptr = strrchr(name, '.');
141 if(cptr)
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);
151 return name;
155 /****************************************************************************
156 * ELFDLL_CreateModref (internal)
158 * INPUT
159 * hModule - the header from the elf-dll's data-segment
160 * path - requested path from original call
162 * OUTPUT
163 * A WINE_MODREF pointer to the new object
165 * BUGS
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;
176 WINE_MODREF *wm;
177 int len;
178 HANDLE procheap = GetProcessHeap();
180 wm = (WINE_MODREF *)HeapAlloc(procheap, HEAP_ZERO_MEMORY, sizeof(*wm));
181 if(!wm)
182 return NULL;
184 wm->module = hModule;
185 wm->type = MODULE32_ELF; /* FIXME */
187 // dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXPORT;
188 // if(dir->Size)
189 // wm->binfmt.pe.pe_export = (PIMAGE_EXPORT_DIRECTORY)RVA(hModule, dir->VirtualAddress);
191 // dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IMPORT;
192 // if(dir->Size)
193 // pe_import = wm->binfmt.pe.pe_import = (PIMAGE_IMPORT_DESCRIPTOR)RVA(hModule, dir->VirtualAddress);
195 // dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
196 // if(dir->Size)
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;
204 else wm->modname++;
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 );
217 if(local_wm)
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;
225 else
227 local_wm = (modref_list*) malloc(sizeof(modref_list));
228 local_wm->next=local_wm->prev=NULL;
229 local_wm->wm=wm;
232 // LeaveCriticalSection( &PROCESS_Current()->crit_section );
233 return wm;
236 /****************************************************************************
237 * ELFDLL_LoadLibraryExA (internal)
239 * Implementation of elf-dll loading for PE modules
241 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR path, DWORD flags)
243 LPVOID dlhandle;
244 struct elfdll_image *image;
245 char name[129];
246 char soname[129];
247 WINE_MODREF *wm;
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);
255 if(!dlhandle)
257 WARN("Could not load %s (%s)\n", soname, dlerror());
258 SetLastError( ERROR_FILE_NOT_FOUND );
259 return NULL;
262 /* Get the 'dllname_elfdll_image' variable */
263 /* strcpy(soname, name);
264 strcat(soname, "_elfdll_image");
265 image = (struct elfdll_image *)dlsym(dlhandle, soname);
266 if(!image)
268 ERR("Could not get elfdll image descriptor %s (%s)\n", soname, dlerror());
269 dlclose(dlhandle);
270 SetLastError( ERROR_BAD_FORMAT );
271 return NULL;
275 wm = ELFDLL_CreateModref((int)dlhandle, path);
276 if(!wm)
278 ERR("Could not create WINE_MODREF for %s\n", path);
279 dlclose(dlhandle);
280 SetLastError( ERROR_OUTOFMEMORY );
281 return NULL;
284 return wm;
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*/