Perform NE_InitProcess in the context of the new task.
[wine] / loader / elfdll.c
1 /*
2  * Elf-dll loader functions
3  *
4  * Copyright 1999 Bertho A. Stultiens
5  */
6
7 #include <string.h>
8 #include <ctype.h>
9 #include <stdlib.h>
10
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"
22
23 DEFAULT_DEBUG_CHANNEL(elfdll)
24
25 #if defined(HAVE_DL_API)
26 #include <dlfcn.h>
27
28 /*------------------ HACKS -----------------*/
29 extern DWORD fixup_imports(WINE_MODREF *wm);
30 extern void dump_exports(HMODULE hModule);
31 /*---------------- END HACKS ---------------*/
32
33 char *extra_ld_library_path = NULL;     /* The extra search-path set in wine.conf */
34
35 struct elfdll_image
36 {
37         HMODULE         pe_module_start;
38         DWORD           pe_module_size;
39         NE_MODULE       *ne_module_start;
40         DWORD           ne_module_size;
41 };
42
43
44 /****************************************************************************
45  *      ELFDLL_dlopen
46  *
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.
50  */
51 void *ELFDLL_dlopen(const char *libname, int flags)
52 {
53         char buffer[256];
54         int namelen;
55         void *handle;
56         char *ldpath;
57
58         /* First try the default path search of dlopen() */
59         handle = dlopen(libname, flags);
60         if(handle)
61                 return handle;
62
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)
67         {
68                 int len;
69                 char *cptr;
70                 char *from;
71
72                 from = ldpath;
73                 cptr = strchr(ldpath, ':');
74                 if(!cptr)
75                 {
76                         len = strlen(ldpath);
77                         ldpath = NULL;
78                 }
79                 else
80                 {
81                         len = cptr - ldpath;
82                         ldpath = cptr + 1;
83                 }
84
85                 if(len + namelen + 1 >= sizeof(buffer))
86                 {
87                         ERR("Buffer overflow! Check EXTRA_LD_LIBRARY_PATH or increase buffer size.\n");
88                         return NULL;
89                 }
90
91                 strncpy(buffer, from, len);
92                 if(len)
93                 {
94                         buffer[len] = '/';
95                         strcpy(buffer + len + 1, libname);
96                 }
97                 else
98                         strcpy(buffer + len, libname);
99
100                 TRACE("Trying dlopen('%s', %d)\n", buffer, flags);
101
102                 handle = dlopen(buffer, flags);
103                 if(handle)
104                         return handle;
105                 else
106                         TRACE("possible dlopen() error: %s\n", dlerror());
107         }
108         return NULL;
109 }
110
111
112 /****************************************************************************
113  *      get_sobasename  (internal)
114  *
115  */
116 static LPSTR get_sobasename(LPCSTR path, LPSTR name)
117 {
118         char *cptr;
119
120         /* Strip the path from the library name */
121         if((cptr = strrchr(path, '/')))
122         {
123                 char *cp = strrchr(cptr+1, '\\');
124                 if(cp && cp > cptr)
125                         cptr = cp;
126         }
127         else
128                 cptr = strrchr(path, '\\');
129
130         if(!cptr)
131                 cptr = (char *)path;    /* No '/' nor '\\' in path */
132         else
133                 cptr++;
134
135         strcpy(name, cptr);
136         cptr = strrchr(name, '.');
137         if(cptr)
138                 *cptr = '\0';   /* Strip extension */
139
140         /* Convert to lower case.
141          * This must be done manually because it is not sure that
142          * other modules are accessible.
143          */
144         for(cptr = name; *cptr; cptr++)
145                 *cptr = tolower(*cptr);
146
147         return name;
148 }
149
150
151 /****************************************************************************
152  *      ELFDLL_CreateModref     (internal)
153  *
154  * INPUT
155  *      hModule - the header from the elf-dll's data-segment
156  *      path    - requested path from original call
157  *
158  * OUTPUT
159  *      A WINE_MODREF pointer to the new object
160  *
161  * BUGS
162  *      - Does not handle errors due to dependencies correctly
163  *      - path can be wrong
164  */
165 #define RVA(base, va)   (((DWORD)base) + ((DWORD)va))
166
167 static WINE_MODREF *ELFDLL_CreateModref(HMODULE hModule, LPCSTR path)
168 {
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();
175
176         wm = (WINE_MODREF *)HeapAlloc(procheap, HEAP_ZERO_MEMORY, sizeof(*wm));
177         if(!wm)
178                 return NULL;
179
180         wm->module = hModule;
181         wm->type = MODULE32_PE;         /* FIXME */
182
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);
186
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);
190
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);
194
195
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++;
200
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++;
207
208         /* Link MODREF into process list */
209
210         EnterCriticalSection( &PROCESS_Current()->crit_section );
211
212         wm->next = PROCESS_Current()->modref_list;
213         PROCESS_Current()->modref_list = wm;
214         if ( wm->next ) wm->next->prev = wm;
215
216         if (    !( nt->FileHeader.Characteristics & IMAGE_FILE_DLL )
217              && !( wm->flags & WINE_MODREF_LOAD_AS_DATAFILE ) )
218
219         {
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;
224         }
225
226         LeaveCriticalSection( &PROCESS_Current()->crit_section );
227
228         /* Fixup Imports */
229
230         if (    pe_import
231              && !( wm->flags & WINE_MODREF_LOAD_AS_DATAFILE )
232              && !( wm->flags & WINE_MODREF_DONT_RESOLVE_REFS )
233              && fixup_imports( wm ) )
234         {
235                 /* remove entry from modref chain */
236                 EnterCriticalSection( &PROCESS_Current()->crit_section );
237
238                 if ( !wm->prev )
239                         PROCESS_Current()->modref_list = wm->next;
240                 else
241                         wm->prev->next = wm->next;
242
243                 if ( wm->next ) wm->next->prev = wm->prev;
244                 wm->next = wm->prev = NULL;
245
246                 LeaveCriticalSection( &PROCESS_Current()->crit_section );
247
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.
254                  */
255                 return NULL;
256         }
257
258         return wm;
259 }
260
261
262 /***********************************************************************
263  *           ELFDLL_CreateNEModule
264  *
265  * Create a dummy NE module for the win32 elf-dll based on the supplied
266  * NE header in the elf-dll.
267  */
268 static HMODULE16 ELFDLL_CreateNEModule(NE_MODULE *ne_image, DWORD size)
269 {
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;
275
276         FarSetOwner16(hModule, hModule);
277         pModule = (NE_MODULE *)GlobalLock16(hModule);
278         pModule->self = hModule;
279         NE_RegisterModule(pModule);
280         return hModule;
281 }
282
283
284 /****************************************************************************
285  *      ELFDLL_LoadLibraryExA   (internal)
286  *
287  * Implementation of elf-dll loading for PE modules
288  */
289 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR path, DWORD flags)
290 {
291         LPVOID dlhandle;
292         struct elfdll_image *image;
293         char name[129];
294         char soname[129];
295         HMODULE16 hmod16;
296         WINE_MODREF *wm;
297
298         get_sobasename(path, name);
299         strcpy(soname, name);
300         strcat(soname, ".so");
301
302         /* Try to open the elf-dll */
303         dlhandle = ELFDLL_dlopen(soname, RTLD_LAZY);
304         if(!dlhandle)
305         {
306                 WARN("Could not load %s (%s)\n", soname, dlerror());
307                 SetLastError( ERROR_FILE_NOT_FOUND );
308                 return NULL;
309         }
310
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) 
316         {
317                 ERR("Could not get elfdll image descriptor %s (%s)\n", soname, dlerror());
318                 dlclose(dlhandle);
319                 SetLastError( ERROR_BAD_FORMAT );
320                 return NULL;
321         }
322
323         /* Create a win16 dummy module */
324         hmod16 = ELFDLL_CreateNEModule(image->ne_module_start, image->ne_module_size);
325         if(!hmod16)
326         {
327                 ERR("Could not create win16 dummy module for %s\n", path);
328                 dlclose(dlhandle);
329                 SetLastError( ERROR_OUTOFMEMORY );
330                 return NULL;
331         }
332
333         image->ne_module_start->module32 = image->pe_module_start;
334
335         wm = ELFDLL_CreateModref(image->pe_module_start, path);
336         if(!wm)
337         {
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;
343         }
344
345         dump_exports(image->pe_module_start);
346         return wm;
347 }
348
349
350 /****************************************************************************
351  *      ELFDLL_UnloadLibrary    (internal)
352  *
353  * Unload an elf-dll completely from memory and deallocate the modref
354  */
355 void ELFDLL_UnloadLibrary(WINE_MODREF *wm)
356 {
357 }
358
359
360 /****************************************************************************
361  *      ELFDLL_LoadModule16     (internal)
362  *
363  * Implementation of elf-dll loading for NE modules
364  */
365 HINSTANCE16 ELFDLL_LoadModule16(LPCSTR libname)
366 {
367         return (HINSTANCE16)ERROR_FILE_NOT_FOUND;
368 }
369
370 #else
371
372 /*
373  * No elfdlls possible 
374  * Just put stubs in here.
375  */
376
377 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR libname, DWORD flags)
378 {
379         SetLastError( ERROR_FILE_NOT_FOUND );
380         return NULL;
381 }
382
383 void ELFDLL_UnloadLibrary(WINE_MODREF *wm)
384 {
385 }
386
387 HINSTANCE16 ELFDLL_LoadModule16(LPCSTR libname)
388 {
389         return (HINSTANCE16)ERROR_FILE_NOT_FOUND;
390 }
391
392 #endif