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