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