Makefile: don't list dependencies for codec-cfg.d
[mplayer/greg.git] / loader / elfdll.c
blob650596ebd14fa1f52ad59762393a61b5c3cfdf49
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 "loader.h"
22 #include "path.h"
24 //DEFAULT_DEBUG_CHANNEL(elfdll)
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <dlfcn.h>
33 /*------------------ HACKS -----------------*/
34 DWORD fixup_imports(WINE_MODREF *wm);
35 void dump_exports(HMODULE hModule);
36 /*---------------- END HACKS ---------------*/
38 struct elfdll_image
40 HMODULE pe_module_start;
41 DWORD pe_module_size;
45 /****************************************************************************
46 * ELFDLL_dlopen
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
50 * changes.
52 void *ELFDLL_dlopen(const char *libname, int flags)
54 char buffer[256];
55 int namelen;
56 void *handle;
57 char *ldpath;
59 /* First try the default path search of dlopen() */
60 handle = dlopen(libname, flags);
61 if(handle)
62 return handle;
64 /* Now try to construct searches through our extra search-path */
65 namelen = strlen(libname);
66 ldpath = codec_path;
67 while(ldpath && *ldpath)
69 int len;
70 char *cptr;
71 char *from;
73 from = ldpath;
74 cptr = strchr(ldpath, ':');
75 if(!cptr)
77 len = strlen(ldpath);
78 ldpath = NULL;
80 else
82 len = cptr - ldpath;
83 ldpath = cptr + 1;
86 if(len + namelen + 1 >= sizeof(buffer))
88 ERR("Buffer overflow! Check EXTRA_LD_LIBRARY_PATH or increase buffer size.\n");
89 return NULL;
92 strncpy(buffer, from, len);
93 if(len)
95 buffer[len] = '/';
96 strcpy(buffer + len + 1, libname);
98 else
99 strcpy(buffer + len, libname);
101 TRACE("Trying dlopen('%s', %d)\n", buffer, flags);
103 handle = dlopen(buffer, flags);
104 if(handle)
105 return handle;
107 return NULL;
111 /****************************************************************************
112 * get_sobasename (internal)
115 static LPSTR get_sobasename(LPCSTR path, LPSTR name)
117 char *cptr;
119 /* Strip the path from the library name */
120 if((cptr = strrchr(path, '/')))
122 char *cp = strrchr(cptr+1, '\\');
123 if(cp && cp > cptr)
124 cptr = cp;
126 else
127 cptr = strrchr(path, '\\');
129 if(!cptr)
130 cptr = (char *)path; /* No '/' nor '\\' in path */
131 else
132 cptr++;
134 strcpy(name, cptr);
135 cptr = strrchr(name, '.');
136 if(cptr)
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);
146 return name;
150 /****************************************************************************
151 * ELFDLL_CreateModref (internal)
153 * INPUT
154 * hModule - the header from the elf-dll's data-segment
155 * path - requested path from original call
157 * OUTPUT
158 * A WINE_MODREF pointer to the new object
160 * BUGS
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;
171 WINE_MODREF *wm;
172 // int len;
173 HANDLE procheap = GetProcessHeap();
175 wm = (WINE_MODREF *)HeapAlloc(procheap, HEAP_ZERO_MEMORY, sizeof(*wm));
176 if(!wm)
177 return NULL;
179 wm->module = hModule;
180 wm->type = MODULE32_ELF; /* FIXME */
182 // dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXPORT;
183 // if(dir->Size)
184 // wm->binfmt.pe.pe_export = (PIMAGE_EXPORT_DIRECTORY)RVA(hModule, dir->VirtualAddress);
186 // dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IMPORT;
187 // if(dir->Size)
188 // pe_import = wm->binfmt.pe.pe_import = (PIMAGE_IMPORT_DESCRIPTOR)RVA(hModule, dir->VirtualAddress);
190 // dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
191 // if(dir->Size)
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;
199 else wm->modname++;
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 );
212 if(local_wm)
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;
220 else
222 local_wm = malloc(sizeof(modref_list));
223 local_wm->next=local_wm->prev=NULL;
224 local_wm->wm=wm;
227 // LeaveCriticalSection( &PROCESS_Current()->crit_section );
228 return wm;
231 /****************************************************************************
232 * ELFDLL_LoadLibraryExA (internal)
234 * Implementation of elf-dll loading for PE modules
236 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR path, DWORD flags)
238 LPVOID dlhandle;
239 // struct elfdll_image *image;
240 char name[129];
241 char soname[129];
242 WINE_MODREF *wm;
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);
250 if(!dlhandle)
252 WARN("Could not load %s (%s)\n", soname, dlerror());
253 SetLastError( ERROR_FILE_NOT_FOUND );
254 return NULL;
257 /* Get the 'dllname_elfdll_image' variable */
258 /* strcpy(soname, name);
259 strcat(soname, "_elfdll_image");
260 image = (struct elfdll_image *)dlsym(dlhandle, soname);
261 if(!image)
263 ERR("Could not get elfdll image descriptor %s (%s)\n", soname, dlerror());
264 dlclose(dlhandle);
265 SetLastError( ERROR_BAD_FORMAT );
266 return NULL;
270 wm = ELFDLL_CreateModref((int)dlhandle, path);
271 if(!wm)
273 ERR("Could not create WINE_MODREF for %s\n", path);
274 dlclose(dlhandle);
275 SetLastError( ERROR_OUTOFMEMORY );
276 return NULL;
279 return wm;
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*/