Changed some error handling a bit.
[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 DECLARE_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 /*---------------- END HACKS ---------------*/
31
32 char *extra_ld_library_path = NULL;     /* The extra search-path set in wine.conf */
33
34 struct elfdll_image
35 {
36         HMODULE         pe_module_start;
37         DWORD           pe_module_size;
38         NE_MODULE       *ne_module_start;
39         DWORD           ne_module_size;
40 };
41
42
43 /****************************************************************************
44  *      ELFDLL_dlopen
45  *
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.
49  */
50 void *ELFDLL_dlopen(const char *libname, int flags)
51 {
52         char buffer[256];
53         int namelen;
54         void *handle;
55         char *ldpath;
56
57         /* First try the default path search of dlopen() */
58         handle = dlopen(libname, flags);
59         if(handle)
60                 return handle;
61
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)
66         {
67                 int len;
68                 char *cptr;
69                 char *from;
70
71                 from = ldpath;
72                 cptr = strchr(ldpath, ':');
73                 if(!cptr)
74                 {
75                         len = strlen(ldpath);
76                         ldpath = NULL;
77                 }
78                 else
79                 {
80                         len = cptr - ldpath;
81                         ldpath = cptr + 1;
82                 }
83
84                 if(len + namelen + 1 >= sizeof(buffer))
85                 {
86                         ERR_(elfdll)("Buffer overflow! Check EXTRA_LD_LIBRARY_PATH or increase buffer size.\n");
87                         return NULL;
88                 }
89
90                 strncpy(buffer, from, len);
91                 if(len)
92                 {
93                         buffer[len] = '/';
94                         strcpy(buffer + len + 1, libname);
95                 }
96                 else
97                         strcpy(buffer + len, libname);
98
99                 TRACE_(elfdll)("Trying dlopen('%s', %d)\n", buffer, flags);
100
101                 handle = dlopen(buffer, flags);
102                 if(handle)
103                         return handle;
104         }
105         return NULL;
106 }
107
108
109 /****************************************************************************
110  *      get_sobasename  (internal)
111  *
112  */
113 static LPSTR get_sobasename(LPCSTR path, LPSTR name)
114 {
115         char *cptr;
116
117         /* Strip the path from the library name */
118         if((cptr = strrchr(path, '/')))
119         {
120                 char *cp = strrchr(cptr+1, '\\');
121                 if(cp && cp > cptr)
122                         cptr = cp;
123         }
124         else
125                 cptr = strrchr(path, '\\');
126
127         if(!cptr)
128                 cptr = (char *)path;    /* No '/' nor '\\' in path */
129         else
130                 cptr++;
131
132         strcpy(name, cptr);
133         cptr = strrchr(name, '.');
134         if(cptr)
135                 *cptr = '\0';   /* Strip extension */
136
137         /* Convert to lower case.
138          * This must be done manually because it is not sure that
139          * other modules are accessible.
140          */
141         for(cptr = name; *cptr; cptr++)
142                 *cptr = tolower(*cptr);
143
144         return name;
145 }
146
147
148 /****************************************************************************
149  *      ELFDLL_CreateModref     (internal)
150  *
151  * INPUT
152  *      hModule - the header from the elf-dll's data-segment
153  *      path    - requested path from original call
154  *
155  * OUTPUT
156  *      A WINE_MODREF pointer to the new object
157  *
158  * BUGS
159  *      - Does not handle errors due to dependencies correctly
160  *      - path can be wrong
161  */
162 #define RVA(base, va)   (((DWORD)base) + ((DWORD)va))
163
164 static WINE_MODREF *ELFDLL_CreateModref(HMODULE hModule, LPCSTR path)
165 {
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();
172
173         wm = (WINE_MODREF *)HeapAlloc(procheap, HEAP_ZERO_MEMORY, sizeof(*wm));
174         if(!wm)
175                 return NULL;
176
177         wm->module = hModule;
178         wm->type = MODULE32_PE;         /* FIXME */
179
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);
183
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);
187
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);
191
192         wm->modname = HEAP_strdupA(procheap, 0, (char *)RVA(hModule, wm->binfmt.pe.pe_export->Name));
193
194         len = GetLongPathNameA(path, NULL, 0);
195         wm->longname = (char *)HeapAlloc(procheap, 0, len+1);
196         GetLongPathNameA(path, wm->longname, len+1);
197
198         wm->shortname = HEAP_strdupA(procheap, 0, path);
199
200         /* Link MODREF into process list */
201         wm->next = PROCESS_Current()->modref_list;
202         PROCESS_Current()->modref_list = wm;
203
204         if(!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
205         {
206                 if(PROCESS_Current()->exe_modref)
207                         FIXME_(elfdll)("overwriting old exe_modref... arrgh\n");
208                 PROCESS_Current()->exe_modref = wm;
209         }
210
211         /* Fixup Imports */
212         if(pe_import && fixup_imports(wm)) 
213         {
214                 /* Error in this module or its dependencies
215                  * remove entry from modref chain
216                  */
217                 WINE_MODREF **xwm;
218                 for(xwm = &(PROCESS_Current()->modref_list); *xwm; xwm = &((*xwm)->next))
219                 {
220                         if ( *xwm == wm )
221                         {
222                                 *xwm = wm->next;
223                                 break;
224                         }
225                 }
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;
233
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.
242                  */
243         }
244
245         return wm;
246 }
247
248
249 /***********************************************************************
250  *           ELFDLL_CreateNEModule
251  *
252  * Create a dummy NE module for the win32 elf-dll based on the supplied
253  * NE header in the elf-dll.
254  */
255 static HMODULE16 ELFDLL_CreateNEModule(NE_MODULE *ne_image, DWORD size)
256 {
257         HMODULE16 hModule = GLOBAL_CreateBlock(GMEM_MOVEABLE, ne_image, size, 0,
258                                                 FALSE, FALSE, FALSE, NULL);
259         if(!hModule)
260                 return (HMODULE16)0;
261
262         FarSetOwner16(hModule, hModule);
263         NE_RegisterModule(ne_image);
264         return hModule;
265 }
266
267
268 /****************************************************************************
269  *      ELFDLL_LoadLibraryExA   (internal)
270  *
271  * Implementation of elf-dll loading for PE modules
272  */
273 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR path, DWORD flags, DWORD *err)
274 {
275         LPVOID dlhandle;
276         struct elfdll_image *image;
277         char name[129];
278         char soname[129];
279         HMODULE16 hmod16;
280         WINE_MODREF *wm;
281
282         get_sobasename(path, name);
283         strcpy(soname, name);
284         strcat(soname, ".so");
285
286         /* Try to open the elf-dll */
287         dlhandle = ELFDLL_dlopen(soname, RTLD_LAZY);
288         if(!dlhandle)
289         {
290                 WARN_(elfdll)("Could not load %s (%s)\n", soname, dlerror());
291                 *err = ERROR_FILE_NOT_FOUND;
292                 return NULL;
293         }
294
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) 
300         {
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;
305         }
306
307         /* Create a win16 dummy module */
308         hmod16 = ELFDLL_CreateNEModule(image->ne_module_start, image->ne_module_size);
309         if(!hmod16)
310         {
311                 ERR_(elfdll)("Could not create win16 dummy module for %s\n", path);
312                 dlclose(dlhandle);
313                 *err = ERROR_OUTOFMEMORY;
314                 return NULL;
315         }
316
317         image->ne_module_start->module32 = image->pe_module_start;
318
319         wm = ELFDLL_CreateModref(image->pe_module_start, path);
320         if(!wm)
321         {
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;
327         }
328
329         *err = 0;
330         return wm;
331 }
332
333
334 /****************************************************************************
335  *      ELFDLL_UnloadLibrary    (internal)
336  *
337  * Unload an elf-dll completely from memory and deallocate the modref
338  */
339 void ELFDLL_UnloadLibrary(WINE_MODREF *wm)
340 {
341 }
342
343
344 /****************************************************************************
345  *      ELFDLL_LoadModule16     (internal)
346  *
347  * Implementation of elf-dll loading for NE modules
348  */
349 HINSTANCE16 ELFDLL_LoadModule16(LPCSTR libname, BOOL implicit)
350 {
351         return (HINSTANCE16)ERROR_FILE_NOT_FOUND;
352 }
353
354 #else
355
356 /*
357  * No elfdlls possible 
358  * Just put stubs in here.
359  */
360
361 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR libname, DWORD flags, DWORD *err)
362 {
363         *err = ERROR_FILE_NOT_FOUND;
364         return NULL;
365 }
366
367 void ELFDLL_UnloadLibrary(WINE_MODREF *wm)
368 {
369 }
370
371 HINSTANCE16 ELFDLL_LoadModule16(LPCSTR libname, BOOL implicit)
372 {
373         return (HINSTANCE16)ERROR_FILE_NOT_FOUND;
374 }
375
376 #endif