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...
27 #include "wine/port.h"
32 #include <sys/types.h>
37 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(win32);
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 */
57 #define UNIX_DLL_ENDING "so"
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 )
69 PIMAGE_NT_HEADERS nth;
70 PIMAGE_SECTION_HEADER sh;
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;
116 WINE_MODREF *ELF_LoadLibraryExA( LPCSTR libname, DWORD flags)
120 char *modname,*s,*t,*x;
124 t = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
125 strlen(libname) + strlen("lib.so") + 1 );
127 /* copy path to tempvar ... */
128 s=strrchr(libname,'/');
130 s=strrchr(libname,'\\');
132 s++; /* skip / or \ */
133 /* copy everything up to s-1 */
134 memcpy(t,libname,s-libname);
139 /* append "lib" foo ".so" */
146 if (!FILE_strcasecmp(s,".dll")) {
147 strcpy(s+1,UNIX_DLL_ENDING);
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 */
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));
168 WARN("failed to load %s: %s\n", s, error);
169 HeapFree( GetProcessHeap(), 0, t );
170 SetLastError( ERROR_FILE_NOT_FOUND );
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;
184 static FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName, BOOL snoop )
188 ELF_STDCALL_STUB *stub, *first_stub;
191 if (!HIWORD(funcName)) {
192 ERR("Can't import from UNIX dynamic libs by ordinal, sorry.\n");
195 fun = wine_dlsym(wm->dlhandle,funcName,error,sizeof(error));
198 /* we sometimes have an excess '_' at the beginning of the name */
199 if (funcName[0]=='_')
202 fun = wine_dlsym(wm->dlhandle,funcName,error,sizeof(error));
206 /* Function@nrofargs usually marks a stdcall function
207 * with nrofargs bytes that are popped at the end
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;
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++) {
229 if (stub->origfun == (DWORD)fun)
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));
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
243 * - Pop returnaddress directly into our return code
244 * popl <into code below>
245 * - Replace it by pointer to start of our returncode
247 * - And call the original function
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.
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);
263 stub->newret = (DWORD)&(stub->addesp);
265 stub->origfun = (DWORD)fun;
267 ((LPBYTE)&(stub->addesp))[0]=0x83;
268 ((LPBYTE)&(stub->addesp))[1]=0xc4;
269 stub->nrofargs = nrofargs;
271 /* filled out by entrycode */
272 stub->oldret = 0xdeadbeef;
277 FIXME("function %s not found: %s\n",funcName,error);
280 fun = SNOOP_GetProcAddress(wm->module,funcName,stub-first_stub,fun);