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