Fixed other bugs within MMIO implementation. Now, it's possible to
[wine] / loader / elf.c
1 /* 
2  *      UNIX dynamic loader
3  * 
4  * Currently only supports stuff using the dl* API.
5  *
6  * Copyright 1998 Marcus Meissner
7  *
8  * FIXME:       Small reentrancy problem.
9  * IDEA(s):     could be used to split up shell32,comctl32... 
10  */
11
12 #include "config.h"
13
14 #include <assert.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <sys/types.h>
18
19 #include "snoop.h"
20 #include "process.h"
21 #include "neexe.h"
22 #include "peexe.h"
23 #include "heap.h"
24 #include "module.h"
25 #include "pe_image.h"
26 #include "debugtools.h"
27 #include "winerror.h"
28 #include "elfdll.h"
29
30 DEFAULT_DEBUG_CHANNEL(win32)
31
32 #if defined(HAVE_DL_API)
33
34 #define UNIX_DLL_ENDING         "so"
35
36 #define STUBSIZE                4095
37
38 #include <dlfcn.h>
39
40
41 static WINE_MODREF *ELF_CreateDummyModule( LPCSTR libname, LPCSTR modname )
42 {
43         PIMAGE_DOS_HEADER       dh;
44         PIMAGE_NT_HEADERS       nth;
45         PIMAGE_SECTION_HEADER   sh;
46         WINE_MODREF *wm;
47         HMODULE hmod;
48
49         wm=(WINE_MODREF*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wm) );
50         wm->type = MODULE32_ELF;
51
52         /* FIXME: hmm, order? */
53         wm->next = PROCESS_Current()->modref_list;
54         PROCESS_Current()->modref_list = wm;
55
56         wm->modname = HEAP_strdupA( GetProcessHeap(), 0, modname );
57         wm->filename = HEAP_strdupA( GetProcessHeap(), 0, libname );
58         wm->short_modname = wm->modname;
59         wm->short_filename = wm->filename;
60
61         hmod = (HMODULE)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
62                                      sizeof(IMAGE_DOS_HEADER) + 
63                                      sizeof(IMAGE_NT_HEADERS) +
64                                      sizeof(IMAGE_SECTION_HEADER) + 100 );
65         dh = (PIMAGE_DOS_HEADER)hmod;
66         dh->e_magic = IMAGE_DOS_SIGNATURE;
67         dh->e_lfanew = sizeof(IMAGE_DOS_HEADER);
68         nth = PE_HEADER(hmod);
69         nth->Signature = IMAGE_NT_SIGNATURE; 
70         nth->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
71         nth->FileHeader.NumberOfSections = 1;
72         nth->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
73         nth->FileHeader.Characteristics = 
74                 IMAGE_FILE_RELOCS_STRIPPED|IMAGE_FILE_LINE_NUMS_STRIPPED|
75                 IMAGE_FILE_LOCAL_SYMS_STRIPPED|IMAGE_FILE_32BIT_MACHINE|
76                 IMAGE_FILE_DLL|IMAGE_FILE_DEBUG_STRIPPED;
77         nth->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
78         nth->OptionalHeader.SizeOfCode = 0;
79         nth->OptionalHeader.SizeOfInitializedData = 0;
80         nth->OptionalHeader.SizeOfUninitializedData = 0;
81         nth->OptionalHeader.AddressOfEntryPoint = 0;
82         nth->OptionalHeader.BaseOfCode          = 0;
83         nth->OptionalHeader.MajorOperatingSystemVersion = 4;
84         nth->OptionalHeader.MajorImageVersion   = 4;
85         nth->OptionalHeader.SizeOfImage         = 0;
86         nth->OptionalHeader.SizeOfHeaders       = 0;
87         nth->OptionalHeader.Subsystem           = IMAGE_SUBSYSTEM_NATIVE;
88         nth->OptionalHeader.DllCharacteristics  = 0;
89         nth->OptionalHeader.NumberOfRvaAndSizes = 0;
90
91         /* allocate one code section that crosses the whole process range
92          * (we could find out from internal tables ... hmm )
93          */
94         sh=(PIMAGE_SECTION_HEADER)(nth+1);
95         strcpy(sh->Name,".text");
96         sh->Misc.VirtualSize    = 0x7fffffff;
97         sh->VirtualAddress      = 0x42; /* so snoop can use it ... */
98         sh->SizeOfRawData       = 0x7fffffff;
99         sh->PointerToRawData    = 0;
100         sh->Characteristics     = IMAGE_SCN_CNT_CODE|IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_EXECUTE|IMAGE_SCN_MEM_READ;
101         wm->module = hmod;
102         return wm;
103 }
104
105 WINE_MODREF *ELF_LoadLibraryExA( LPCSTR libname, DWORD flags)
106 {
107         WINE_MODREF     *wm;
108         char            *modname,*s,*t,*x;
109         LPVOID          *dlhandle;
110
111         t = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
112                        strlen(libname) + strlen("lib.so") + 1 );
113         *t = '\0';
114         /* copy path to tempvar ... */
115         s=strrchr(libname,'/');
116         if (!s)
117                 s=strrchr(libname,'\\');
118         if (s) {
119                 s++; /* skip / or \ */
120                 /* copy everything up to s-1 */
121                 memcpy(t,libname,s-libname);
122                 t[s-libname]= '\0';
123         } else
124                 s = (LPSTR)libname;
125         modname = s;
126         /* append "lib" foo ".so" */
127         strcat(t,"lib");
128         x = t+strlen(t);
129         strcat(t,s);
130         s = strchr(x,'.');
131         if (s) {
132             while (s) {
133                 if (!strcasecmp(s,".dll")) {
134                     strcpy(s+1,UNIX_DLL_ENDING);
135                     break;
136                 }
137                 s=strchr(s+1,'.');
138             }
139         } else {
140                 strcat(x,"."UNIX_DLL_ENDING);
141         }
142
143         /* FIXME: make UNIX filename from DOS fn? */
144
145         /* ... and open it */
146         dlhandle = ELFDLL_dlopen(t,RTLD_NOW);
147         if (!dlhandle) {
148                 HeapFree( GetProcessHeap(), 0, t );
149                 SetLastError( ERROR_FILE_NOT_FOUND );
150                 return NULL;
151         }
152
153         wm = ELF_CreateDummyModule( t, modname );
154         wm->binfmt.elf.dlhandle = dlhandle;
155
156         SNOOP_RegisterDLL(wm->module,libname,STUBSIZE/sizeof(ELF_STDCALL_STUB));
157         return wm;
158 }
159
160 FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName) 
161 {
162         LPVOID                  fun;
163         int                     i,nrofargs = 0;
164         ELF_STDCALL_STUB        *stub;
165
166         assert(wm->type == MODULE32_ELF);
167         if (!HIWORD(funcName)) {
168                 ERR("Can't import from UNIX dynamic libs by ordinal, sorry.\n");
169                 return (FARPROC)0;
170         }
171         fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
172         /* we sometimes have an excess '_' at the beginning of the name */
173         if (!fun && (funcName[0]=='_')) {
174                 funcName++ ;
175                 fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
176         }
177         if (!fun) {
178                 /* Function@nrofargs usually marks a stdcall function 
179                  * with nrofargs bytes that are popped at the end
180                  */
181                 if (strchr(funcName,'@')) {
182                         LPSTR   t,fn = HEAP_strdupA( GetProcessHeap(), 0, funcName );
183
184                         t = strchr(fn,'@');
185                         *t = '\0';
186                         nrofargs = 0;
187                         sscanf(t+1,"%d",&nrofargs);
188                         fun = dlsym(wm->binfmt.elf.dlhandle,fn);
189                         HeapFree( GetProcessHeap(), 0, fn );
190                 }
191         }
192         /* We sometimes have Win32 dlls implemented using stdcall but UNIX 
193          * dlls using cdecl. If we find out the number of args the function
194          * uses, we remove them from the stack using two small stubs.
195          */
196         if (!wm->binfmt.elf.stubs) {
197                 /* one page should suffice */
198                 wm->binfmt.elf.stubs = VirtualAlloc(NULL,STUBSIZE,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
199                 memset(wm->binfmt.elf.stubs,0,STUBSIZE);
200         }
201         stub = wm->binfmt.elf.stubs;
202         for (i=0;i<STUBSIZE/sizeof(ELF_STDCALL_STUB);i++) {
203                 if (!stub->origfun)
204                         break;
205                 if (stub->origfun == (DWORD)fun)
206                         break;
207                 stub++;
208         }
209         if (i==STUBSIZE/sizeof(ELF_STDCALL_STUB)) {
210                 ERR("please report, that there are not enough slots for stdcall stubs in the ELF loader.\n");
211                 assert(i<STUBSIZE/sizeof(ELF_STDCALL_STUB));
212         }
213         if (!stub->origfun)
214                 stub->origfun=(DWORD)fun; /* just a marker */
215
216         if (fun && nrofargs) { /* we don't need it for 0 args */
217                 /* Selfmodifying entry/return stub for stdcall -> cdecl 
218                  * conversion.
219                  *  - Pop returnaddress directly into our return code
220                  *              popl <into code below>
221                  *  - Replace it by pointer to start of our returncode
222                  *              push $newret
223                  *  - And call the original function
224                  *              jmp $orgfun
225                  *  - Remove the arguments no longer needed
226                  * newret:      add esp, <nrofargs>
227                  *  - Push the original returnvalue on the stack
228                  *              pushl <poppedvalue>
229                  *  - And return to it.
230                  *              ret
231                  */
232
233                 /* FIXME: The function stub is not reentrant. */
234
235                 ((LPBYTE)&(stub->popl))[0]        = 0x8f;
236                 ((LPBYTE)&(stub->popl))[1]        = 0x05;
237                 stub->addr_popped = (DWORD)&(stub->oldret);
238                 stub->pushl1      = 0x68;
239                 stub->newret      = (DWORD)&(stub->addesp);
240                 stub->pushl2      = 0x68;
241                 stub->origfun     = (DWORD)fun;
242                 stub->ret1        = 0xc3;
243                 ((LPBYTE)&(stub->addesp))[0]=0x83;
244                 ((LPBYTE)&(stub->addesp))[1]=0xc4;
245                 stub->nrofargs    = nrofargs;
246                 stub->pushl3      = 0x68;
247                         /* filled out by entrycode */
248                 stub->oldret      = 0xdeadbeef;
249                 stub->ret2        = 0xc3;
250                 fun=(FARPROC)stub;
251         }
252         if (!fun) {
253                 FIXME("function %s not found: %s\n",funcName,dlerror());
254                 return fun;
255         }
256         fun = SNOOP_GetProcAddress(wm->module,funcName,stub-wm->binfmt.elf.stubs,fun);
257         return (FARPROC)fun;
258 }
259
260
261 /***************************************************************************
262  *      ELF_UnloadLibrary
263  *
264  * Unload the elf library and free the modref
265  */
266 void ELF_UnloadLibrary(WINE_MODREF *wm)
267 {
268         /* FIXME: do something here */
269 }
270
271 #else
272
273 WINE_MODREF *ELF_LoadLibraryExA( LPCSTR libname, DWORD flags)
274 {
275         return NULL;
276 }
277
278 void ELF_UnloadLibrary(WINE_MODREF *wm)
279 {
280 }
281
282 FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName) 
283 {
284         return (FARPROC)0;
285 }
286
287 #endif