No longer directly accessing debuggee memory.
[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         }
106         return NULL;
107 }
108
109
110 /****************************************************************************
111  *      get_sobasename  (internal)
112  *
113  */
114 static LPSTR get_sobasename(LPCSTR path, LPSTR name)
115 {
116         char *cptr;
117
118         /* Strip the path from the library name */
119         if((cptr = strrchr(path, '/')))
120         {
121                 char *cp = strrchr(cptr+1, '\\');
122                 if(cp && cp > cptr)
123                         cptr = cp;
124         }
125         else
126                 cptr = strrchr(path, '\\');
127
128         if(!cptr)
129                 cptr = (char *)path;    /* No '/' nor '\\' in path */
130         else
131                 cptr++;
132
133         strcpy(name, cptr);
134         cptr = strrchr(name, '.');
135         if(cptr)
136                 *cptr = '\0';   /* Strip extension */
137
138         /* Convert to lower case.
139          * This must be done manually because it is not sure that
140          * other modules are accessible.
141          */
142         for(cptr = name; *cptr; cptr++)
143                 *cptr = tolower(*cptr);
144
145         return name;
146 }
147
148
149 /****************************************************************************
150  *      ELFDLL_CreateModref     (internal)
151  *
152  * INPUT
153  *      hModule - the header from the elf-dll's data-segment
154  *      path    - requested path from original call
155  *
156  * OUTPUT
157  *      A WINE_MODREF pointer to the new object
158  *
159  * BUGS
160  *      - Does not handle errors due to dependencies correctly
161  *      - path can be wrong
162  */
163 #define RVA(base, va)   (((DWORD)base) + ((DWORD)va))
164
165 static WINE_MODREF *ELFDLL_CreateModref(HMODULE hModule, LPCSTR path)
166 {
167         IMAGE_NT_HEADERS *nt = PE_HEADER(hModule);
168         IMAGE_DATA_DIRECTORY *dir;
169         IMAGE_IMPORT_DESCRIPTOR *pe_import = NULL;
170         WINE_MODREF *wm;
171         int len;
172         HANDLE procheap = GetProcessHeap();
173
174         wm = (WINE_MODREF *)HeapAlloc(procheap, HEAP_ZERO_MEMORY, sizeof(*wm));
175         if(!wm)
176                 return NULL;
177
178         wm->module = hModule;
179         wm->type = MODULE32_PE;         /* FIXME */
180
181         dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXPORT;
182         if(dir->Size)
183                 wm->binfmt.pe.pe_export = (PIMAGE_EXPORT_DIRECTORY)RVA(hModule, dir->VirtualAddress);
184
185         dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IMPORT;
186         if(dir->Size)
187                 pe_import = wm->binfmt.pe.pe_import = (PIMAGE_IMPORT_DESCRIPTOR)RVA(hModule, dir->VirtualAddress);
188
189         dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
190         if(dir->Size)
191                 wm->binfmt.pe.pe_resource = (PIMAGE_RESOURCE_DIRECTORY)RVA(hModule, dir->VirtualAddress);
192
193
194         wm->filename = HEAP_strdupA( procheap, 0, path );
195         wm->modname = strrchr( wm->filename, '\\' );
196         if (!wm->modname) wm->modname = wm->filename;
197         else wm->modname++;
198
199         len = GetShortPathNameA( wm->filename, NULL, 0 );
200         wm->short_filename = (char *)HeapAlloc( procheap, 0, len+1 );
201         GetShortPathNameA( wm->filename, wm->short_filename, len+1 );
202         wm->short_modname = strrchr( wm->short_filename, '\\' );
203         if (!wm->short_modname) wm->short_modname = wm->short_filename;
204         else wm->short_modname++;
205
206         /* Link MODREF into process list */
207
208         EnterCriticalSection( &PROCESS_Current()->crit_section );
209
210         wm->next = PROCESS_Current()->modref_list;
211         PROCESS_Current()->modref_list = wm;
212         if ( wm->next ) wm->next->prev = wm;
213
214         if (    !( nt->FileHeader.Characteristics & IMAGE_FILE_DLL )
215              && !( wm->flags & WINE_MODREF_LOAD_AS_DATAFILE ) )
216
217         {
218                 if ( PROCESS_Current()->exe_modref )
219                         FIXME( "Trying to load second .EXE file: %s\n", path );
220                 else
221                         PROCESS_Current()->exe_modref = wm;
222         }
223
224         LeaveCriticalSection( &PROCESS_Current()->crit_section );
225
226         /* Fixup Imports */
227
228         if (    pe_import
229              && !( wm->flags & WINE_MODREF_LOAD_AS_DATAFILE )
230              && !( wm->flags & WINE_MODREF_DONT_RESOLVE_REFS )
231              && fixup_imports( wm ) )
232         {
233                 /* remove entry from modref chain */
234                 EnterCriticalSection( &PROCESS_Current()->crit_section );
235
236                 if ( !wm->prev )
237                         PROCESS_Current()->modref_list = wm->next;
238                 else
239                         wm->prev->next = wm->next;
240
241                 if ( wm->next ) wm->next->prev = wm->prev;
242                 wm->next = wm->prev = NULL;
243
244                 LeaveCriticalSection( &PROCESS_Current()->crit_section );
245
246                 /* FIXME: there are several more dangling references
247                  * left. Including dlls loaded by this dll before the
248                  * failed one. Unrolling is rather difficult with the
249                  * current structure and we can leave it them lying
250                  * around with no problems, so we don't care.
251                  * As these might reference our wm, we don't free it.
252                  */
253                 return NULL;
254         }
255
256         return wm;
257 }
258
259
260 /***********************************************************************
261  *           ELFDLL_CreateNEModule
262  *
263  * Create a dummy NE module for the win32 elf-dll based on the supplied
264  * NE header in the elf-dll.
265  */
266 static HMODULE16 ELFDLL_CreateNEModule(NE_MODULE *ne_image, DWORD size)
267 {
268         NE_MODULE *pModule;
269         HMODULE16 hModule = GLOBAL_CreateBlock(GMEM_MOVEABLE, ne_image, size, 0,
270                                                 FALSE, FALSE, FALSE, NULL);
271         if(!hModule)
272                 return (HMODULE16)0;
273
274         FarSetOwner16(hModule, hModule);
275         pModule = (NE_MODULE *)GlobalLock16(hModule);
276         pModule->self = hModule;
277         NE_RegisterModule(pModule);
278         return hModule;
279 }
280
281
282 /****************************************************************************
283  *      ELFDLL_LoadLibraryExA   (internal)
284  *
285  * Implementation of elf-dll loading for PE modules
286  */
287 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR path, DWORD flags, DWORD *err)
288 {
289         LPVOID dlhandle;
290         struct elfdll_image *image;
291         char name[129];
292         char soname[129];
293         HMODULE16 hmod16;
294         WINE_MODREF *wm;
295
296         get_sobasename(path, name);
297         strcpy(soname, name);
298         strcat(soname, ".so");
299
300         /* Try to open the elf-dll */
301         dlhandle = ELFDLL_dlopen(soname, RTLD_LAZY);
302         if(!dlhandle)
303         {
304                 WARN("Could not load %s (%s)\n", soname, dlerror());
305                 *err = ERROR_FILE_NOT_FOUND;
306                 return NULL;
307         }
308
309         /* Get the 'dllname_elfdll_image' variable */
310         strcpy(soname, name);
311         strcat(soname, "_elfdll_image");
312         image = (struct elfdll_image *)dlsym(dlhandle, soname);
313         if(!image) 
314         {
315                 ERR("Could not get elfdll image descriptor %s (%s)\n", soname, dlerror());
316                 dlclose(dlhandle);
317                 *err = ERROR_BAD_FORMAT;
318                 return NULL;
319         }
320
321         /* Create a win16 dummy module */
322         hmod16 = ELFDLL_CreateNEModule(image->ne_module_start, image->ne_module_size);
323         if(!hmod16)
324         {
325                 ERR("Could not create win16 dummy module for %s\n", path);
326                 dlclose(dlhandle);
327                 *err = ERROR_OUTOFMEMORY;
328                 return NULL;
329         }
330
331         image->ne_module_start->module32 = image->pe_module_start;
332
333         wm = ELFDLL_CreateModref(image->pe_module_start, path);
334         if(!wm)
335         {
336                 ERR("Could not create WINE_MODREF for %s\n", path);
337                 GLOBAL_FreeBlock((HGLOBAL16)hmod16);
338                 dlclose(dlhandle);
339                 *err = ERROR_OUTOFMEMORY;
340                 return NULL;
341         }
342
343         dump_exports(image->pe_module_start);
344
345         *err = 0;
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, DWORD *err)
378 {
379         *err = 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