Set the default menu item of the window menu
[wine/wine64.git] / loader / elfdll.c
blob30f510231d86080d74c503c8719c65908c41fd9a
1 /*
2 * Elf-dll loader functions
4 * Copyright 1999 Bertho A. Stultiens
5 */
7 #include <string.h>
8 #include <ctype.h>
9 #include <stdlib.h>
11 #include "config.h"
12 #include "windef.h"
13 #include "global.h"
14 #include "process.h"
15 #include "module.h"
16 #include "neexe.h"
17 #include "heap.h"
18 #include "wine/winbase16.h"
19 #include "elfdll.h"
20 #include "debugtools.h"
21 #include "winerror.h"
23 DECLARE_DEBUG_CHANNEL(elfdll)
25 #if defined(HAVE_DL_API)
26 #include <dlfcn.h>
28 /*------------------ HACKS -----------------*/
29 extern DWORD fixup_imports(WINE_MODREF *wm);
30 /*---------------- END HACKS ---------------*/
32 char *extra_ld_library_path = NULL; /* The extra search-path set in wine.conf */
34 struct elfdll_image
36 HMODULE pe_module_start;
37 DWORD pe_module_size;
38 NE_MODULE *ne_module_start;
39 DWORD ne_module_size;
43 /****************************************************************************
44 * ELFDLL_dlopen
46 * Wrapper for dlopen to search the EXTRA_LD_LIBRARY_PATH from wine.conf
47 * manually because libdl.so caches the environment and does not accept our
48 * changes.
50 void *ELFDLL_dlopen(const char *libname, int flags)
52 char buffer[256];
53 int namelen;
54 void *handle;
55 char *ldpath;
57 /* First try the default path search of dlopen() */
58 handle = dlopen(libname, flags);
59 if(handle)
60 return handle;
62 /* Now try to construct searches through our extra search-path */
63 namelen = strlen(libname);
64 ldpath = extra_ld_library_path;
65 while(ldpath && *ldpath)
67 int len;
68 char *cptr;
69 char *from;
71 from = ldpath;
72 cptr = strchr(ldpath, ':');
73 if(!cptr)
75 len = strlen(ldpath);
76 ldpath = NULL;
78 else
80 len = cptr - ldpath;
81 ldpath = cptr + 1;
84 if(len + namelen + 1 >= sizeof(buffer))
86 ERR_(elfdll)("Buffer overflow! Check EXTRA_LD_LIBRARY_PATH or increase buffer size.\n");
87 return NULL;
90 strncpy(buffer, from, len);
91 if(len)
93 buffer[len] = '/';
94 strcpy(buffer + len + 1, libname);
96 else
97 strcpy(buffer + len, libname);
99 TRACE_(elfdll)("Trying dlopen('%s', %d)\n", buffer, flags);
101 handle = dlopen(buffer, flags);
102 if(handle)
103 return handle;
105 return NULL;
109 /****************************************************************************
110 * get_sobasename (internal)
113 static LPSTR get_sobasename(LPCSTR path, LPSTR name)
115 char *cptr;
117 /* Strip the path from the library name */
118 if((cptr = strrchr(path, '/')))
120 char *cp = strrchr(cptr+1, '\\');
121 if(cp && cp > cptr)
122 cptr = cp;
124 else
125 cptr = strrchr(path, '\\');
127 if(!cptr)
128 cptr = (char *)path; /* No '/' nor '\\' in path */
129 else
130 cptr++;
132 strcpy(name, cptr);
133 cptr = strrchr(name, '.');
134 if(cptr)
135 *cptr = '\0'; /* Strip extension */
137 /* Convert to lower case.
138 * This must be done manually because it is not sure that
139 * other modules are accessible.
141 for(cptr = name; *cptr; cptr++)
142 *cptr = tolower(*cptr);
144 return name;
148 /****************************************************************************
149 * ELFDLL_CreateModref (internal)
151 * INPUT
152 * hModule - the header from the elf-dll's data-segment
153 * path - requested path from original call
155 * OUTPUT
156 * A WINE_MODREF pointer to the new object
158 * BUGS
159 * - Does not handle errors due to dependencies correctly
160 * - path can be wrong
162 #define RVA(base, va) (((DWORD)base) + ((DWORD)va))
164 static WINE_MODREF *ELFDLL_CreateModref(HMODULE hModule, LPCSTR path)
166 IMAGE_NT_HEADERS *nt = PE_HEADER(hModule);
167 IMAGE_DATA_DIRECTORY *dir;
168 IMAGE_IMPORT_DESCRIPTOR *pe_import = NULL;
169 WINE_MODREF *wm;
170 int len;
171 HANDLE procheap = GetProcessHeap();
173 wm = (WINE_MODREF *)HeapAlloc(procheap, HEAP_ZERO_MEMORY, sizeof(*wm));
174 if(!wm)
175 return NULL;
177 wm->module = hModule;
178 wm->type = MODULE32_PE; /* FIXME */
180 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXPORT;
181 if(dir->Size)
182 wm->binfmt.pe.pe_export = (PIMAGE_EXPORT_DIRECTORY)RVA(hModule, dir->VirtualAddress);
184 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IMPORT;
185 if(dir->Size)
186 pe_import = wm->binfmt.pe.pe_import = (PIMAGE_IMPORT_DESCRIPTOR)RVA(hModule, dir->VirtualAddress);
188 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
189 if(dir->Size)
190 wm->binfmt.pe.pe_resource = (PIMAGE_RESOURCE_DIRECTORY)RVA(hModule, dir->VirtualAddress);
192 wm->modname = HEAP_strdupA(procheap, 0, (char *)RVA(hModule, wm->binfmt.pe.pe_export->Name));
194 len = GetLongPathNameA(path, NULL, 0);
195 wm->longname = (char *)HeapAlloc(procheap, 0, len+1);
196 GetLongPathNameA(path, wm->longname, len+1);
198 wm->shortname = HEAP_strdupA(procheap, 0, path);
200 /* Link MODREF into process list */
201 wm->next = PROCESS_Current()->modref_list;
202 PROCESS_Current()->modref_list = wm;
204 if(!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
206 if(PROCESS_Current()->exe_modref)
207 FIXME_(elfdll)("overwriting old exe_modref... arrgh\n");
208 PROCESS_Current()->exe_modref = wm;
211 /* Fixup Imports */
212 if(pe_import && fixup_imports(wm))
214 /* Error in this module or its dependencies
215 * remove entry from modref chain
217 WINE_MODREF **xwm;
218 for(xwm = &(PROCESS_Current()->modref_list); *xwm; xwm = &((*xwm)->next))
220 if ( *xwm == wm )
222 *xwm = wm->next;
223 break;
226 if(wm == PROCESS_Current()->exe_modref)
227 ERR_(elfdll)("Have to delete current exe_modref. Expect crash now\n");
228 HeapFree(procheap, 0, wm->shortname);
229 HeapFree(procheap, 0, wm->longname);
230 HeapFree(procheap, 0, wm->modname);
231 HeapFree(procheap, 0, wm);
232 return NULL;
234 /* FIXME: We should traverse back in the recursion
235 * with an error to unload everything that got loaded
236 * before this error occurred.
237 * Too dificult for now though and we don't care at the
238 * moment. But, it *MUST* be implemented someday because
239 * we won't be able to map the elf-dll twice in this
240 * address-space which can cause some unexpected and
241 * weird problems later on.
245 return wm;
249 /***********************************************************************
250 * ELFDLL_CreateNEModule
252 * Create a dummy NE module for the win32 elf-dll based on the supplied
253 * NE header in the elf-dll.
255 static HMODULE16 ELFDLL_CreateNEModule(NE_MODULE *ne_image, DWORD size)
257 HMODULE16 hModule = GLOBAL_CreateBlock(GMEM_MOVEABLE, ne_image, size, 0,
258 FALSE, FALSE, FALSE, NULL);
259 if(!hModule)
260 return (HMODULE16)0;
262 FarSetOwner16(hModule, hModule);
263 NE_RegisterModule(ne_image);
264 return hModule;
268 /****************************************************************************
269 * ELFDLL_LoadLibraryExA (internal)
271 * Implementation of elf-dll loading for PE modules
273 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR path, DWORD flags, DWORD *err)
275 LPVOID dlhandle;
276 struct elfdll_image *image;
277 char name[129];
278 char soname[129];
279 HMODULE16 hmod16;
280 WINE_MODREF *wm;
282 get_sobasename(path, name);
283 strcpy(soname, name);
284 strcat(soname, ".so");
286 /* Try to open the elf-dll */
287 dlhandle = ELFDLL_dlopen(soname, RTLD_LAZY);
288 if(!dlhandle)
290 WARN_(elfdll)("Could not load %s (%s)\n", soname, dlerror());
291 *err = ERROR_FILE_NOT_FOUND;
292 return NULL;
295 /* Get the 'dllname_elfdll_image' variable */
296 strcpy(soname, name);
297 strcat(soname, "_elfdll_image");
298 image = (struct elfdll_image *)dlsym(dlhandle, soname);
299 if(!image)
301 ERR_(elfdll)("Could not get elfdll image descriptor %s (%s)\n", soname, dlerror());
302 dlclose(dlhandle);
303 *err = ERROR_BAD_FORMAT;
304 return NULL;
307 /* Create a win16 dummy module */
308 hmod16 = ELFDLL_CreateNEModule(image->ne_module_start, image->ne_module_size);
309 if(!hmod16)
311 ERR_(elfdll)("Could not create win16 dummy module for %s\n", path);
312 dlclose(dlhandle);
313 *err = ERROR_OUTOFMEMORY;
314 return NULL;
317 image->ne_module_start->module32 = image->pe_module_start;
319 wm = ELFDLL_CreateModref(image->pe_module_start, path);
320 if(!wm)
322 ERR_(elfdll)("Could not create WINE_MODREF for %s\n", path);
323 GLOBAL_FreeBlock((HGLOBAL16)hmod16);
324 dlclose(dlhandle);
325 *err = ERROR_OUTOFMEMORY;
326 return NULL;
329 *err = 0;
330 return wm;
334 /****************************************************************************
335 * ELFDLL_UnloadLibrary (internal)
337 * Unload an elf-dll completely from memory and deallocate the modref
339 void ELFDLL_UnloadLibrary(WINE_MODREF *wm)
344 /****************************************************************************
345 * ELFDLL_LoadModule16 (internal)
347 * Implementation of elf-dll loading for NE modules
349 HINSTANCE16 ELFDLL_LoadModule16(LPCSTR libname, BOOL implicit)
351 return (HINSTANCE16)ERROR_FILE_NOT_FOUND;
354 #else
357 * No elfdlls possible
358 * Just put stubs in here.
361 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR libname, DWORD flags, DWORD *err)
363 *err = ERROR_FILE_NOT_FOUND;
364 return NULL;
367 void ELFDLL_UnloadLibrary(WINE_MODREF *wm)
371 HINSTANCE16 ELFDLL_LoadModule16(LPCSTR libname, BOOL implicit)
373 return (HINSTANCE16)ERROR_FILE_NOT_FOUND;
376 #endif