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>
36 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(win32
);
42 WORD popl WINE_PACKED
; /* 0x8f 0x05 */
43 DWORD addr_popped WINE_PACKED
;/* ... */
44 BYTE pushl1 WINE_PACKED
; /* 0x68 */
45 DWORD newret WINE_PACKED
; /* ... */
46 BYTE pushl2 WINE_PACKED
; /* 0x68 */
47 DWORD origfun WINE_PACKED
; /* original function */
48 BYTE ret1 WINE_PACKED
; /* 0xc3 */
49 WORD addesp WINE_PACKED
; /* 0x83 0xc4 */
50 BYTE nrofargs WINE_PACKED
; /* nr of arguments to add esp, */
51 BYTE pushl3 WINE_PACKED
; /* 0x68 */
52 DWORD oldret WINE_PACKED
; /* Filled out from popl above */
53 BYTE ret2 WINE_PACKED
; /* 0xc3 */
56 #define UNIX_DLL_ENDING "so"
59 #define STUBOFFSET (sizeof(IMAGE_DOS_HEADER) + \
60 sizeof(IMAGE_NT_HEADERS) + \
61 sizeof(IMAGE_SECTION_HEADER))
63 static FARPROC
ELF_FindExportedFunction( WINE_MODREF
*wm
, LPCSTR funcName
, int hint
, BOOL snoop
);
65 static HMODULE
ELF_CreateDummyModule( LPCSTR libname
, LPCSTR modname
)
68 PIMAGE_NT_HEADERS nth
;
69 PIMAGE_SECTION_HEADER sh
;
72 hmod
= (HMODULE
)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
73 sizeof(IMAGE_DOS_HEADER
) +
74 sizeof(IMAGE_NT_HEADERS
) +
75 sizeof(IMAGE_SECTION_HEADER
) + STUBSIZE
);
76 dh
= (PIMAGE_DOS_HEADER
)hmod
;
77 dh
->e_magic
= IMAGE_DOS_SIGNATURE
;
78 dh
->e_lfanew
= sizeof(IMAGE_DOS_HEADER
);
79 nth
= (IMAGE_NT_HEADERS
*)(dh
+ 1);
80 nth
->Signature
= IMAGE_NT_SIGNATURE
;
81 nth
->FileHeader
.Machine
= IMAGE_FILE_MACHINE_I386
;
82 nth
->FileHeader
.NumberOfSections
= 1;
83 nth
->FileHeader
.SizeOfOptionalHeader
= sizeof(IMAGE_OPTIONAL_HEADER
);
84 nth
->FileHeader
.Characteristics
=
85 IMAGE_FILE_RELOCS_STRIPPED
|IMAGE_FILE_LINE_NUMS_STRIPPED
|
86 IMAGE_FILE_LOCAL_SYMS_STRIPPED
|IMAGE_FILE_32BIT_MACHINE
|
87 IMAGE_FILE_DLL
|IMAGE_FILE_DEBUG_STRIPPED
;
88 nth
->OptionalHeader
.Magic
= IMAGE_NT_OPTIONAL_HDR_MAGIC
;
89 nth
->OptionalHeader
.SizeOfCode
= 0;
90 nth
->OptionalHeader
.SizeOfInitializedData
= 0;
91 nth
->OptionalHeader
.SizeOfUninitializedData
= 0;
92 nth
->OptionalHeader
.AddressOfEntryPoint
= 0;
93 nth
->OptionalHeader
.BaseOfCode
= 0;
94 nth
->OptionalHeader
.MajorOperatingSystemVersion
= 4;
95 nth
->OptionalHeader
.MajorImageVersion
= 4;
96 nth
->OptionalHeader
.SizeOfImage
= 0;
97 nth
->OptionalHeader
.SizeOfHeaders
= 0;
98 nth
->OptionalHeader
.Subsystem
= IMAGE_SUBSYSTEM_NATIVE
;
99 nth
->OptionalHeader
.DllCharacteristics
= 0;
100 nth
->OptionalHeader
.NumberOfRvaAndSizes
= 0;
102 /* allocate one code section that crosses the whole process range
103 * (we could find out from internal tables ... hmm )
105 sh
=(PIMAGE_SECTION_HEADER
)(nth
+1);
106 strcpy(sh
->Name
,".text");
107 sh
->Misc
.VirtualSize
= STUBSIZE
;
108 sh
->VirtualAddress
= STUBOFFSET
; /* so snoop can use it ... */
109 sh
->SizeOfRawData
= STUBSIZE
;
110 sh
->PointerToRawData
= 0;
111 sh
->Characteristics
= IMAGE_SCN_CNT_CODE
|IMAGE_SCN_CNT_INITIALIZED_DATA
|IMAGE_SCN_MEM_EXECUTE
|IMAGE_SCN_MEM_READ
;
115 WINE_MODREF
*ELF_LoadLibraryExA( LPCSTR libname
, DWORD flags
)
119 char *modname
,*s
,*t
,*x
;
123 t
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
124 strlen(libname
) + strlen("lib.so") + 1 );
126 /* copy path to tempvar ... */
127 s
=strrchr(libname
,'/');
129 s
=strrchr(libname
,'\\');
131 s
++; /* skip / or \ */
132 /* copy everything up to s-1 */
133 memcpy(t
,libname
,s
-libname
);
138 /* append "lib" foo ".so" */
145 if (!FILE_strcasecmp(s
,".dll")) {
146 strcpy(s
+1,UNIX_DLL_ENDING
);
152 strcat(x
,"."UNIX_DLL_ENDING
);
155 /* grab just the last piece of the path/filename
156 which should be the name of the library we are
157 looking to load. increment by 1 to skip the DOS slash */
161 /* ... and open the library pointed by s, while t points
162 points to the ENTIRE DOS filename of the library
163 t is returned by HeapAlloc() above and so is also used
164 with HeapFree() below */
165 dlhandle
= wine_dlopen(s
,RTLD_NOW
,error
,sizeof(error
));
167 WARN("failed to load %s: %s\n", s
, error
);
168 HeapFree( GetProcessHeap(), 0, t
);
169 SetLastError( ERROR_FILE_NOT_FOUND
);
173 hmod
= ELF_CreateDummyModule( t
, modname
);
175 SNOOP_RegisterDLL(hmod
,libname
,0,STUBSIZE
/sizeof(ELF_STDCALL_STUB
));
177 wm
= PE_CreateModule( hmod
, libname
, 0, 0, FALSE
);
178 wm
->find_export
= ELF_FindExportedFunction
;
179 wm
->dlhandle
= dlhandle
;
183 static FARPROC
ELF_FindExportedFunction( WINE_MODREF
*wm
, LPCSTR funcName
, int hint
, BOOL snoop
)
187 ELF_STDCALL_STUB
*stub
, *first_stub
;
190 if (!HIWORD(funcName
)) {
191 ERR("Can't import from UNIX dynamic libs by ordinal, sorry.\n");
194 fun
= wine_dlsym(wm
->dlhandle
,funcName
,error
,sizeof(error
));
197 /* we sometimes have an excess '_' at the beginning of the name */
198 if (funcName
[0]=='_')
201 fun
= wine_dlsym(wm
->dlhandle
,funcName
,error
,sizeof(error
));
205 /* Function@nrofargs usually marks a stdcall function
206 * with nrofargs bytes that are popped at the end
209 if ((t
= strchr(funcName
,'@')))
211 LPSTR fn
= HeapAlloc( GetProcessHeap(), 0, t
- funcName
+ 1 );
212 memcpy( fn
, funcName
, t
- funcName
);
213 fn
[t
- funcName
] = 0;
215 sscanf(t
+1,"%d",&nrofargs
);
216 fun
= wine_dlsym(wm
->dlhandle
,fn
,error
,sizeof(error
));
217 HeapFree( GetProcessHeap(), 0, fn
);
220 /* We sometimes have Win32 dlls implemented using stdcall but UNIX
221 * dlls using cdecl. If we find out the number of args the function
222 * uses, we remove them from the stack using two small stubs.
224 stub
= first_stub
= (ELF_STDCALL_STUB
*)((char *)wm
->module
+ STUBOFFSET
);
225 for (i
=0;i
<STUBSIZE
/sizeof(ELF_STDCALL_STUB
);i
++) {
228 if (stub
->origfun
== (DWORD
)fun
)
232 if (i
==STUBSIZE
/sizeof(ELF_STDCALL_STUB
)) {
233 ERR("please report, that there are not enough slots for stdcall stubs in the ELF loader.\n");
234 assert(i
<STUBSIZE
/sizeof(ELF_STDCALL_STUB
));
237 stub
->origfun
=(DWORD
)fun
; /* just a marker */
239 if (fun
&& nrofargs
) { /* we don't need it for 0 args */
240 /* Selfmodifying entry/return stub for stdcall -> cdecl
242 * - Pop returnaddress directly into our return code
243 * popl <into code below>
244 * - Replace it by pointer to start of our returncode
246 * - And call the original function
248 * - Remove the arguments no longer needed
249 * newret: add esp, <nrofargs>
250 * - Push the original returnvalue on the stack
251 * pushl <poppedvalue>
252 * - And return to it.
256 /* FIXME: The function stub is not reentrant. */
258 ((LPBYTE
)&(stub
->popl
))[0] = 0x8f;
259 ((LPBYTE
)&(stub
->popl
))[1] = 0x05;
260 stub
->addr_popped
= (DWORD
)&(stub
->oldret
);
262 stub
->newret
= (DWORD
)&(stub
->addesp
);
264 stub
->origfun
= (DWORD
)fun
;
266 ((LPBYTE
)&(stub
->addesp
))[0]=0x83;
267 ((LPBYTE
)&(stub
->addesp
))[1]=0xc4;
268 stub
->nrofargs
= nrofargs
;
270 /* filled out by entrycode */
271 stub
->oldret
= 0xdeadbeef;
276 FIXME("function %s not found: %s\n",funcName
,error
);
279 fun
= SNOOP_GetProcAddress(wm
->module
,funcName
,stub
-first_stub
,fun
);