manpage: Fix wrong option name in input example
[mplayer/glamo.git] / loader / elfdll.c
blob057fa5b9c8367a61d7d50f11c8f9cbc7cb2cd12b
1 /*
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/
9 */
10 #include "config.h"
12 #ifdef HAVE_LIBDL
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"
20 #include "debug.h"
21 #include "path.h"
23 //DEFAULT_DEBUG_CHANNEL(elfdll)
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <dlfcn.h>
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 ---------------*/
41 struct elfdll_image
43 HMODULE pe_module_start;
44 DWORD pe_module_size;
48 /****************************************************************************
49 * ELFDLL_dlopen
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
53 * changes.
55 void *ELFDLL_dlopen(const char *libname, int flags)
57 char buffer[256];
58 int namelen;
59 void *handle;
60 char *ldpath;
62 /* First try the default path search of dlopen() */
63 handle = dlopen(libname, flags);
64 if(handle)
65 return handle;
67 /* Now try to construct searches through our extra search-path */
68 namelen = strlen(libname);
69 ldpath = codec_path;
70 while(ldpath && *ldpath)
72 int len;
73 char *cptr;
74 char *from;
76 from = ldpath;
77 cptr = strchr(ldpath, ':');
78 if(!cptr)
80 len = strlen(ldpath);
81 ldpath = NULL;
83 else
85 len = cptr - ldpath;
86 ldpath = cptr + 1;
89 if(len + namelen + 1 >= sizeof(buffer))
91 ERR("Buffer overflow! Check EXTRA_LD_LIBRARY_PATH or increase buffer size.\n");
92 return NULL;
95 strncpy(buffer, from, len);
96 if(len)
98 buffer[len] = '/';
99 strcpy(buffer + len + 1, libname);
101 else
102 strcpy(buffer + len, libname);
104 TRACE("Trying dlopen('%s', %d)\n", buffer, flags);
106 handle = dlopen(buffer, flags);
107 if(handle)
108 return handle;
110 return NULL;
114 /****************************************************************************
115 * get_sobasename (internal)
118 static LPSTR get_sobasename(LPCSTR path, LPSTR name)
120 char *cptr;
122 /* Strip the path from the library name */
123 if((cptr = strrchr(path, '/')))
125 char *cp = strrchr(cptr+1, '\\');
126 if(cp && cp > cptr)
127 cptr = cp;
129 else
130 cptr = strrchr(path, '\\');
132 if(!cptr)
133 cptr = (char *)path; /* No '/' nor '\\' in path */
134 else
135 cptr++;
137 strcpy(name, cptr);
138 cptr = strrchr(name, '.');
139 if(cptr)
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);
149 return name;
153 /****************************************************************************
154 * ELFDLL_CreateModref (internal)
156 * INPUT
157 * hModule - the header from the elf-dll's data-segment
158 * path - requested path from original call
160 * OUTPUT
161 * A WINE_MODREF pointer to the new object
163 * BUGS
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;
174 WINE_MODREF *wm;
175 // int len;
176 HANDLE procheap = GetProcessHeap();
178 wm = (WINE_MODREF *)HeapAlloc(procheap, HEAP_ZERO_MEMORY, sizeof(*wm));
179 if(!wm)
180 return NULL;
182 wm->module = hModule;
183 wm->type = MODULE32_ELF; /* FIXME */
185 // dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXPORT;
186 // if(dir->Size)
187 // wm->binfmt.pe.pe_export = (PIMAGE_EXPORT_DIRECTORY)RVA(hModule, dir->VirtualAddress);
189 // dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IMPORT;
190 // if(dir->Size)
191 // pe_import = wm->binfmt.pe.pe_import = (PIMAGE_IMPORT_DESCRIPTOR)RVA(hModule, dir->VirtualAddress);
193 // dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
194 // if(dir->Size)
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;
202 else wm->modname++;
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 );
215 if(local_wm)
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;
223 else
225 local_wm = malloc(sizeof(modref_list));
226 local_wm->next=local_wm->prev=NULL;
227 local_wm->wm=wm;
230 // LeaveCriticalSection( &PROCESS_Current()->crit_section );
231 return wm;
234 /****************************************************************************
235 * ELFDLL_LoadLibraryExA (internal)
237 * Implementation of elf-dll loading for PE modules
239 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR path, DWORD flags)
241 LPVOID dlhandle;
242 // struct elfdll_image *image;
243 char name[129];
244 char soname[129];
245 WINE_MODREF *wm;
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);
253 if(!dlhandle)
255 WARN("Could not load %s (%s)\n", soname, dlerror());
256 SetLastError( ERROR_FILE_NOT_FOUND );
257 return NULL;
260 /* Get the 'dllname_elfdll_image' variable */
261 /* strcpy(soname, name);
262 strcat(soname, "_elfdll_image");
263 image = (struct elfdll_image *)dlsym(dlhandle, soname);
264 if(!image)
266 ERR("Could not get elfdll image descriptor %s (%s)\n", soname, dlerror());
267 dlclose(dlhandle);
268 SetLastError( ERROR_BAD_FORMAT );
269 return NULL;
273 wm = ELFDLL_CreateModref((int)dlhandle, path);
274 if(!wm)
276 ERR("Could not create WINE_MODREF for %s\n", path);
277 dlclose(dlhandle);
278 SetLastError( ERROR_OUTOFMEMORY );
279 return NULL;
282 return wm;
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*/