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