Fixed some issues found by winapi_check.
[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 "heap.h"
17 #include "wine/winbase16.h"
18 #include "elfdll.h"
19 #include "debugtools.h"
20 #include "winerror.h"
21
22 DEFAULT_DEBUG_CHANNEL(elfdll);
23
24 #if defined(HAVE_DL_API)
25 #include <dlfcn.h>
26
27 /*------------------ HACKS -----------------*/
28 extern DWORD fixup_imports(WINE_MODREF *wm);
29 extern void dump_exports(HMODULE hModule);
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         /* do NOT call dlerror() here ! (check after return) */
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                 /* do NOT call dlerror() here ! (check after return) */
104                 if(handle)
105                         return handle;
106         }
107         return NULL;
108 }
109
110
111 /****************************************************************************
112  *      get_sobasename  (internal)
113  *
114  */
115 static LPSTR get_sobasename(LPCSTR path, LPSTR name)
116 {
117         char *cptr;
118
119         /* Strip the path from the library name */
120         if((cptr = strrchr(path, '/')))
121         {
122                 char *cp = strrchr(cptr+1, '\\');
123                 if(cp && cp > cptr)
124                         cptr = cp;
125         }
126         else
127                 cptr = strrchr(path, '\\');
128
129         if(!cptr)
130                 cptr = (char *)path;    /* No '/' nor '\\' in path */
131         else
132                 cptr++;
133
134         strcpy(name, cptr);
135         cptr = strrchr(name, '.');
136         if(cptr && !strcasecmp(cptr,".dll")) *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_LoadLibraryExA   (internal)
151  *
152  * Implementation of elf-dll loading for PE modules
153  */
154 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR path, DWORD flags)
155 {
156         LPVOID dlhandle;
157         struct elfdll_image *image;
158         char name[129];
159         char soname[129];
160         WINE_MODREF *wm;
161
162         get_sobasename(path, name);
163         strcpy(soname, name);
164         strcat(soname, ".so");
165
166         /* Try to open the elf-dll */
167         dlhandle = ELFDLL_dlopen(soname, RTLD_LAZY);
168         if(!dlhandle)
169         {
170                 WARN("Could not load %s (%s)\n", soname, dlerror());
171                 SetLastError( ERROR_FILE_NOT_FOUND );
172                 return NULL;
173         }
174
175         /* Get the 'dllname_elfdll_image' variable */
176         strcpy(soname, name);
177         strcat(soname, "_elfdll_image");
178         image = (struct elfdll_image *)dlsym(dlhandle, soname);
179         if(!image) 
180         {
181                 ERR("Could not get elfdll image descriptor %s (%s)\n", soname, dlerror());
182                 dlclose(dlhandle);
183                 SetLastError( ERROR_BAD_FORMAT );
184                 return NULL;
185         }
186
187         wm = PE_CreateModule( image->pe_module_start, path, 0, -1, FALSE );
188         if(!wm)
189         {
190                 ERR("Could not create WINE_MODREF for %s\n", path);
191                 dlclose(dlhandle);
192                 SetLastError( ERROR_OUTOFMEMORY );
193                 return NULL;
194         }
195         wm->dlhandle = dlhandle;
196
197         dump_exports(image->pe_module_start);
198         return wm;
199 }
200
201 #else
202
203 /*
204  * No elfdlls possible 
205  * Just put stubs in here.
206  */
207
208 WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR libname, DWORD flags)
209 {
210         SetLastError( ERROR_FILE_NOT_FOUND );
211         return NULL;
212 }
213
214 #endif