Make direct console input routines go through int16, rather than
[wine/testsucceed.git] / loader / elfdll.c
blobc4dda2cf0c2e1d150c39dd284409394289a5c0df
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 DEFAULT_DEBUG_CHANNEL(elfdll)
25 #if defined(HAVE_DL_API)
26 #include <dlfcn.h>
28 /*------------------ HACKS -----------------*/
29 extern DWORD fixup_imports(WINE_MODREF *wm);
30 extern void dump_exports(HMODULE hModule);
31 /*---------------- END HACKS ---------------*/
33 char *extra_ld_library_path = NULL; /* The extra search-path set in wine.conf */
35 struct elfdll_image
37 HMODULE pe_module_start;
38 DWORD pe_module_size;
39 NE_MODULE *ne_module_start;
40 DWORD ne_module_size;
44 /****************************************************************************
45 * ELFDLL_dlopen
47 * Wrapper for dlopen to search the EXTRA_LD_LIBRARY_PATH from wine.conf
48 * manually because libdl.so caches the environment and does not accept our
49 * changes.
51 void *ELFDLL_dlopen(const char *libname, int flags)
53 char buffer[256];
54 int namelen;
55 void *handle;
56 char *ldpath;
58 /* First try the default path search of dlopen() */
59 handle = dlopen(libname, flags);
60 if(handle)
61 return handle;
63 /* Now try to construct searches through our extra search-path */
64 namelen = strlen(libname);
65 ldpath = extra_ld_library_path;
66 while(ldpath && *ldpath)
68 int len;
69 char *cptr;
70 char *from;
72 from = ldpath;
73 cptr = strchr(ldpath, ':');
74 if(!cptr)
76 len = strlen(ldpath);
77 ldpath = NULL;
79 else
81 len = cptr - ldpath;
82 ldpath = cptr + 1;
85 if(len + namelen + 1 >= sizeof(buffer))
87 ERR("Buffer overflow! Check EXTRA_LD_LIBRARY_PATH or increase buffer size.\n");
88 return NULL;
91 strncpy(buffer, from, len);
92 if(len)
94 buffer[len] = '/';
95 strcpy(buffer + len + 1, libname);
97 else
98 strcpy(buffer + len, libname);
100 TRACE("Trying dlopen('%s', %d)\n", buffer, flags);
102 handle = dlopen(buffer, flags);
103 if(handle)
104 return handle;
105 else
106 TRACE("possible dlopen() error: %s\n", dlerror());
108 return NULL;
112 /****************************************************************************
113 * get_sobasename (internal)
116 static LPSTR get_sobasename(LPCSTR path, LPSTR name)
118 char *cptr;
120 /* Strip the path from the library name */
121 if((cptr = strrchr(path, '/')))
123 char *cp = strrchr(cptr+1, '\\');
124 if(cp && cp > cptr)
125 cptr = cp;
127 else
128 cptr = strrchr(path, '\\');
130 if(!cptr)
131 cptr = (char *)path; /* No '/' nor '\\' in path */
132 else
133 cptr++;
135 strcpy(name, cptr);
136 cptr = strrchr(name, '.');
137 if(cptr)
138 *cptr = '\0'; /* Strip extension */
140 /* Convert to lower case.
141 * This must be done manually because it is not sure that
142 * other modules are accessible.
144 for(cptr = name; *cptr; cptr++)
145 *cptr = tolower(*cptr);
147 return name;
151 /****************************************************************************
152 * ELFDLL_CreateModref (internal)
154 * INPUT
155 * hModule - the header from the elf-dll's data-segment
156 * path - requested path from original call
158 * OUTPUT
159 * A WINE_MODREF pointer to the new object
161 * BUGS
162 * - Does not handle errors due to dependencies correctly
163 * - path can be wrong
165 #define RVA(base, va) (((DWORD)base) + ((DWORD)va))
167 static WINE_MODREF *ELFDLL_CreateModref(HMODULE hModule, LPCSTR path)
169 IMAGE_NT_HEADERS *nt = PE_HEADER(hModule);
170 IMAGE_DATA_DIRECTORY *dir;
171 IMAGE_IMPORT_DESCRIPTOR *pe_import = NULL;
172 WINE_MODREF *wm;
173 int len;
174 HANDLE procheap = GetProcessHeap();
176 wm = (WINE_MODREF *)HeapAlloc(procheap, HEAP_ZERO_MEMORY, sizeof(*wm));
177 if(!wm)
178 return NULL;
180 wm->module = hModule;
181 wm->type = MODULE32_PE; /* FIXME */
183 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXPORT;
184 if(dir->Size)
185 wm->binfmt.pe.pe_export = (PIMAGE_EXPORT_DIRECTORY)RVA(hModule, dir->VirtualAddress);
187 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IMPORT;
188 if(dir->Size)
189 pe_import = wm->binfmt.pe.pe_import = (PIMAGE_IMPORT_DESCRIPTOR)RVA(hModule, dir->VirtualAddress);
191 dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
192 if(dir->Size)
193 wm->binfmt.pe.pe_resource = (PIMAGE_RESOURCE_DIRECTORY)RVA(hModule, dir->VirtualAddress);
196 wm->filename = HEAP_strdupA( procheap, 0, 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 wm->next = PROCESS_Current()->modref_list;
213 PROCESS_Current()->modref_list = wm;
214 if ( wm->next ) wm->next->prev = wm;
216 if ( !( nt->FileHeader.Characteristics & IMAGE_FILE_DLL )
217 && !( wm->flags & WINE_MODREF_LOAD_AS_DATAFILE ) )
220 if ( PROCESS_Current()->exe_modref )
221 FIXME( "Trying to load second .EXE file: %s\n", path );
222 else
223 PROCESS_Current()->exe_modref = wm;
226 LeaveCriticalSection( &PROCESS_Current()->crit_section );
228 /* Fixup Imports */
230 if ( pe_import
231 && !( wm->flags & WINE_MODREF_LOAD_AS_DATAFILE )
232 && !( wm->flags & WINE_MODREF_DONT_RESOLVE_REFS )
233 && fixup_imports( wm ) )
235 /* remove entry from modref chain */
236 EnterCriticalSection( &PROCESS_Current()->crit_section );
238 if ( !wm->prev )
239 PROCESS_Current()->modref_list = wm->next;
240 else
241 wm->prev->next = wm->next;
243 if ( wm->next ) wm->next->prev = wm->prev;
244 wm->next = wm->prev = NULL;
246 LeaveCriticalSection( &PROCESS_Current()->crit_section );
248 /* FIXME: there are several more dangling references
249 * left. Including dlls loaded by this dll before the
250 * failed one. Unrolling is rather difficult with the
251 * current structure and we can leave it them lying
252 * around with no problems, so we don't care.
253 * As these might reference our wm, we don't free it.
255 return NULL;
258 return wm;
262 /***********************************************************************
263 * ELFDLL_CreateNEModule
265 * Create a dummy NE module for the win32 elf-dll based on the supplied
266 * NE header in the elf-dll.
268 static HMODULE16 ELFDLL_CreateNEModule(NE_MODULE *ne_image, DWORD size)
270 NE_MODULE *pModule;
271 HMODULE16 hModule = GLOBAL_CreateBlock(GMEM_MOVEABLE, ne_image, size, 0,
272 FALSE, FALSE, FALSE, NULL);
273 if(!hModule)
274 return (HMODULE16)0;
276 FarSetOwner16(hModule, hModule);
277 pModule = (NE_MODULE *)GlobalLock16(hModule);
278 pModule->self = hModule;
279 NE_RegisterModule(pModule);
280 return hModule;
284 /****************************************************************************
285 * ELFDLL_LoadLibraryExA (internal)
287 * Implementation of elf-dll loading for PE modules
289 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR path, DWORD flags)
291 LPVOID dlhandle;
292 struct elfdll_image *image;
293 char name[129];
294 char soname[129];
295 HMODULE16 hmod16;
296 WINE_MODREF *wm;
298 get_sobasename(path, name);
299 strcpy(soname, name);
300 strcat(soname, ".so");
302 /* Try to open the elf-dll */
303 dlhandle = ELFDLL_dlopen(soname, RTLD_LAZY);
304 if(!dlhandle)
306 WARN("Could not load %s (%s)\n", soname, dlerror());
307 SetLastError( ERROR_FILE_NOT_FOUND );
308 return NULL;
311 /* Get the 'dllname_elfdll_image' variable */
312 strcpy(soname, name);
313 strcat(soname, "_elfdll_image");
314 image = (struct elfdll_image *)dlsym(dlhandle, soname);
315 if(!image)
317 ERR("Could not get elfdll image descriptor %s (%s)\n", soname, dlerror());
318 dlclose(dlhandle);
319 SetLastError( ERROR_BAD_FORMAT );
320 return NULL;
323 /* Create a win16 dummy module */
324 hmod16 = ELFDLL_CreateNEModule(image->ne_module_start, image->ne_module_size);
325 if(!hmod16)
327 ERR("Could not create win16 dummy module for %s\n", path);
328 dlclose(dlhandle);
329 SetLastError( ERROR_OUTOFMEMORY );
330 return NULL;
333 image->ne_module_start->module32 = image->pe_module_start;
335 wm = ELFDLL_CreateModref(image->pe_module_start, path);
336 if(!wm)
338 ERR("Could not create WINE_MODREF for %s\n", path);
339 GLOBAL_FreeBlock((HGLOBAL16)hmod16);
340 dlclose(dlhandle);
341 SetLastError( ERROR_OUTOFMEMORY );
342 return NULL;
345 dump_exports(image->pe_module_start);
346 return wm;
350 /****************************************************************************
351 * ELFDLL_UnloadLibrary (internal)
353 * Unload an elf-dll completely from memory and deallocate the modref
355 void ELFDLL_UnloadLibrary(WINE_MODREF *wm)
360 /****************************************************************************
361 * ELFDLL_LoadModule16 (internal)
363 * Implementation of elf-dll loading for NE modules
365 HINSTANCE16 ELFDLL_LoadModule16(LPCSTR libname)
367 return (HINSTANCE16)ERROR_FILE_NOT_FOUND;
370 #else
373 * No elfdlls possible
374 * Just put stubs in here.
377 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR libname, DWORD flags)
379 SetLastError( ERROR_FILE_NOT_FOUND );
380 return NULL;
383 void ELFDLL_UnloadLibrary(WINE_MODREF *wm)
387 HINSTANCE16 ELFDLL_LoadModule16(LPCSTR libname)
389 return (HINSTANCE16)ERROR_FILE_NOT_FOUND;
392 #endif