- Try to match the native control for the SetBkColor message.
[wine/multimedia.git] / loader / elf.c
blob8dabd901a1092d7ca56ae7aa7fcbf7936d94a77a
1 /*
2 * UNIX dynamic loader
3 *
4 * Currently only supports stuff using the dl* API.
6 * Copyright 1998 Marcus Meissner
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * FIXME: Small reentrancy problem.
23 * IDEA(s): could be used to split up shell32,comctl32...
26 #include "config.h"
27 #include "wine/port.h"
29 #include <assert.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <sys/types.h>
34 #include "snoop.h"
35 #include "file.h"
36 #include "module.h"
37 #include "wine/debug.h"
38 #include "winerror.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(win32);
42 typedef struct {
43 WORD popl WINE_PACKED; /* 0x8f 0x05 */
44 DWORD addr_popped WINE_PACKED;/* ... */
45 BYTE pushl1 WINE_PACKED; /* 0x68 */
46 DWORD newret WINE_PACKED; /* ... */
47 BYTE pushl2 WINE_PACKED; /* 0x68 */
48 DWORD origfun WINE_PACKED; /* original function */
49 BYTE ret1 WINE_PACKED; /* 0xc3 */
50 WORD addesp WINE_PACKED; /* 0x83 0xc4 */
51 BYTE nrofargs WINE_PACKED; /* nr of arguments to add esp, */
52 BYTE pushl3 WINE_PACKED; /* 0x68 */
53 DWORD oldret WINE_PACKED; /* Filled out from popl above */
54 BYTE ret2 WINE_PACKED; /* 0xc3 */
55 } ELF_STDCALL_STUB;
57 #define UNIX_DLL_ENDING "so"
59 #define STUBSIZE 4095
60 #define STUBOFFSET (sizeof(IMAGE_DOS_HEADER) + \
61 sizeof(IMAGE_NT_HEADERS) + \
62 sizeof(IMAGE_SECTION_HEADER))
64 static FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName, BOOL snoop );
66 static HMODULE ELF_CreateDummyModule( LPCSTR libname, LPCSTR modname )
68 PIMAGE_DOS_HEADER dh;
69 PIMAGE_NT_HEADERS nth;
70 PIMAGE_SECTION_HEADER sh;
71 HMODULE hmod;
73 hmod = (HMODULE)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
74 sizeof(IMAGE_DOS_HEADER) +
75 sizeof(IMAGE_NT_HEADERS) +
76 sizeof(IMAGE_SECTION_HEADER) + STUBSIZE );
77 dh = (PIMAGE_DOS_HEADER)hmod;
78 dh->e_magic = IMAGE_DOS_SIGNATURE;
79 dh->e_lfanew = sizeof(IMAGE_DOS_HEADER);
80 nth = PE_HEADER(hmod);
81 nth->Signature = IMAGE_NT_SIGNATURE;
82 nth->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
83 nth->FileHeader.NumberOfSections = 1;
84 nth->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
85 nth->FileHeader.Characteristics =
86 IMAGE_FILE_RELOCS_STRIPPED|IMAGE_FILE_LINE_NUMS_STRIPPED|
87 IMAGE_FILE_LOCAL_SYMS_STRIPPED|IMAGE_FILE_32BIT_MACHINE|
88 IMAGE_FILE_DLL|IMAGE_FILE_DEBUG_STRIPPED;
89 nth->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
90 nth->OptionalHeader.SizeOfCode = 0;
91 nth->OptionalHeader.SizeOfInitializedData = 0;
92 nth->OptionalHeader.SizeOfUninitializedData = 0;
93 nth->OptionalHeader.AddressOfEntryPoint = 0;
94 nth->OptionalHeader.BaseOfCode = 0;
95 nth->OptionalHeader.MajorOperatingSystemVersion = 4;
96 nth->OptionalHeader.MajorImageVersion = 4;
97 nth->OptionalHeader.SizeOfImage = 0;
98 nth->OptionalHeader.SizeOfHeaders = 0;
99 nth->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_NATIVE;
100 nth->OptionalHeader.DllCharacteristics = 0;
101 nth->OptionalHeader.NumberOfRvaAndSizes = 0;
103 /* allocate one code section that crosses the whole process range
104 * (we could find out from internal tables ... hmm )
106 sh=(PIMAGE_SECTION_HEADER)(nth+1);
107 strcpy(sh->Name,".text");
108 sh->Misc.VirtualSize = STUBSIZE;
109 sh->VirtualAddress = STUBOFFSET; /* so snoop can use it ... */
110 sh->SizeOfRawData = STUBSIZE;
111 sh->PointerToRawData = 0;
112 sh->Characteristics = IMAGE_SCN_CNT_CODE|IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_EXECUTE|IMAGE_SCN_MEM_READ;
113 return hmod;
116 WINE_MODREF *ELF_LoadLibraryExA( LPCSTR libname, DWORD flags)
118 WINE_MODREF *wm;
119 HMODULE hmod;
120 char *modname,*s,*t,*x;
121 LPVOID *dlhandle;
122 char error[256];
124 t = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
125 strlen(libname) + strlen("lib.so") + 1 );
126 *t = '\0';
127 /* copy path to tempvar ... */
128 s=strrchr(libname,'/');
129 if (!s)
130 s=strrchr(libname,'\\');
131 if (s) {
132 s++; /* skip / or \ */
133 /* copy everything up to s-1 */
134 memcpy(t,libname,s-libname);
135 t[s-libname]= '\0';
136 } else
137 s = (LPSTR)libname;
138 modname = s;
139 /* append "lib" foo ".so" */
140 strcat(t,"lib");
141 x = t+strlen(t);
142 strcat(t,s);
143 s = strchr(x,'.');
144 if (s) {
145 while (s) {
146 if (!FILE_strcasecmp(s,".dll")) {
147 strcpy(s+1,UNIX_DLL_ENDING);
148 break;
150 s=strchr(s+1,'.');
152 } else {
153 strcat(x,"."UNIX_DLL_ENDING);
156 /* grab just the last piece of the path/filename
157 which should be the name of the library we are
158 looking to load. increment by 1 to skip the DOS slash */
159 s = strrchr(t,'\\');
160 s++;
162 /* ... and open the library pointed by s, while t points
163 points to the ENTIRE DOS filename of the library
164 t is returned by HeapAlloc() above and so is also used
165 with HeapFree() below */
166 dlhandle = wine_dlopen(s,RTLD_NOW,error,sizeof(error));
167 if (!dlhandle) {
168 WARN("failed to load %s: %s\n", s, error);
169 HeapFree( GetProcessHeap(), 0, t );
170 SetLastError( ERROR_FILE_NOT_FOUND );
171 return NULL;
174 hmod = ELF_CreateDummyModule( t, modname );
176 SNOOP_RegisterDLL(hmod,libname,0,STUBSIZE/sizeof(ELF_STDCALL_STUB));
178 wm = PE_CreateModule( hmod, libname, 0, 0, FALSE );
179 wm->find_export = ELF_FindExportedFunction;
180 wm->dlhandle = dlhandle;
181 return wm;
184 static FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName, BOOL snoop )
186 LPVOID fun;
187 int i,nrofargs = 0;
188 ELF_STDCALL_STUB *stub, *first_stub;
189 char error[256];
191 if (!HIWORD(funcName)) {
192 ERR("Can't import from UNIX dynamic libs by ordinal, sorry.\n");
193 return (FARPROC)0;
195 fun = wine_dlsym(wm->dlhandle,funcName,error,sizeof(error));
196 if (!fun)
198 /* we sometimes have an excess '_' at the beginning of the name */
199 if (funcName[0]=='_')
201 funcName++ ;
202 fun = wine_dlsym(wm->dlhandle,funcName,error,sizeof(error));
205 if (!fun) {
206 /* Function@nrofargs usually marks a stdcall function
207 * with nrofargs bytes that are popped at the end
209 LPCSTR t;
210 if ((t = strchr(funcName,'@')))
212 LPSTR fn = HeapAlloc( GetProcessHeap(), 0, t - funcName + 1 );
213 memcpy( fn, funcName, t - funcName );
214 fn[t - funcName] = 0;
215 nrofargs = 0;
216 sscanf(t+1,"%d",&nrofargs);
217 fun = wine_dlsym(wm->dlhandle,fn,error,sizeof(error));
218 HeapFree( GetProcessHeap(), 0, fn );
221 /* We sometimes have Win32 dlls implemented using stdcall but UNIX
222 * dlls using cdecl. If we find out the number of args the function
223 * uses, we remove them from the stack using two small stubs.
225 stub = first_stub = (ELF_STDCALL_STUB *)((char *)wm->module + STUBOFFSET);
226 for (i=0;i<STUBSIZE/sizeof(ELF_STDCALL_STUB);i++) {
227 if (!stub->origfun)
228 break;
229 if (stub->origfun == (DWORD)fun)
230 break;
231 stub++;
233 if (i==STUBSIZE/sizeof(ELF_STDCALL_STUB)) {
234 ERR("please report, that there are not enough slots for stdcall stubs in the ELF loader.\n");
235 assert(i<STUBSIZE/sizeof(ELF_STDCALL_STUB));
237 if (!stub->origfun)
238 stub->origfun=(DWORD)fun; /* just a marker */
240 if (fun && nrofargs) { /* we don't need it for 0 args */
241 /* Selfmodifying entry/return stub for stdcall -> cdecl
242 * conversion.
243 * - Pop returnaddress directly into our return code
244 * popl <into code below>
245 * - Replace it by pointer to start of our returncode
246 * push $newret
247 * - And call the original function
248 * jmp $orgfun
249 * - Remove the arguments no longer needed
250 * newret: add esp, <nrofargs>
251 * - Push the original returnvalue on the stack
252 * pushl <poppedvalue>
253 * - And return to it.
254 * ret
257 /* FIXME: The function stub is not reentrant. */
259 ((LPBYTE)&(stub->popl))[0] = 0x8f;
260 ((LPBYTE)&(stub->popl))[1] = 0x05;
261 stub->addr_popped = (DWORD)&(stub->oldret);
262 stub->pushl1 = 0x68;
263 stub->newret = (DWORD)&(stub->addesp);
264 stub->pushl2 = 0x68;
265 stub->origfun = (DWORD)fun;
266 stub->ret1 = 0xc3;
267 ((LPBYTE)&(stub->addesp))[0]=0x83;
268 ((LPBYTE)&(stub->addesp))[1]=0xc4;
269 stub->nrofargs = nrofargs;
270 stub->pushl3 = 0x68;
271 /* filled out by entrycode */
272 stub->oldret = 0xdeadbeef;
273 stub->ret2 = 0xc3;
274 fun=(FARPROC)stub;
276 if (!fun) {
277 FIXME("function %s not found: %s\n",funcName,error);
278 return fun;
280 fun = SNOOP_GetProcAddress(wm->module,funcName,stub-first_stub,fun);
281 return (FARPROC)fun;