4 * Currently only supports stuff using the dl* API.
6 * Copyright 1998 Marcus Meissner
8 * FIXME: Small reentrancy problem.
9 * IDEA(s): could be used to split up shell32,comctl32...
17 #include <sys/types.h>
23 #include "debugtools.h"
25 #include "wine/port.h"
27 DEFAULT_DEBUG_CHANNEL(win32
);
32 WORD popl WINE_PACKED
; /* 0x8f 0x05 */
33 DWORD addr_popped WINE_PACKED
;/* ... */
34 BYTE pushl1 WINE_PACKED
; /* 0x68 */
35 DWORD newret WINE_PACKED
; /* ... */
36 BYTE pushl2 WINE_PACKED
; /* 0x68 */
37 DWORD origfun WINE_PACKED
; /* original function */
38 BYTE ret1 WINE_PACKED
; /* 0xc3 */
39 WORD addesp WINE_PACKED
; /* 0x83 0xc4 */
40 BYTE nrofargs WINE_PACKED
; /* nr of arguments to add esp, */
41 BYTE pushl3 WINE_PACKED
; /* 0x68 */
42 DWORD oldret WINE_PACKED
; /* Filled out from popl above */
43 BYTE ret2 WINE_PACKED
; /* 0xc3 */
46 #define UNIX_DLL_ENDING "so"
49 #define STUBOFFSET (sizeof(IMAGE_DOS_HEADER) + \
50 sizeof(IMAGE_NT_HEADERS) + \
51 sizeof(IMAGE_SECTION_HEADER))
53 static FARPROC
ELF_FindExportedFunction( WINE_MODREF
*wm
, LPCSTR funcName
, BOOL snoop
);
55 static HMODULE
ELF_CreateDummyModule( LPCSTR libname
, LPCSTR modname
)
58 PIMAGE_NT_HEADERS nth
;
59 PIMAGE_SECTION_HEADER sh
;
62 hmod
= (HMODULE
)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
63 sizeof(IMAGE_DOS_HEADER
) +
64 sizeof(IMAGE_NT_HEADERS
) +
65 sizeof(IMAGE_SECTION_HEADER
) + STUBSIZE
);
66 dh
= (PIMAGE_DOS_HEADER
)hmod
;
67 dh
->e_magic
= IMAGE_DOS_SIGNATURE
;
68 dh
->e_lfanew
= sizeof(IMAGE_DOS_HEADER
);
69 nth
= PE_HEADER(hmod
);
70 nth
->Signature
= IMAGE_NT_SIGNATURE
;
71 nth
->FileHeader
.Machine
= IMAGE_FILE_MACHINE_I386
;
72 nth
->FileHeader
.NumberOfSections
= 1;
73 nth
->FileHeader
.SizeOfOptionalHeader
= sizeof(IMAGE_OPTIONAL_HEADER
);
74 nth
->FileHeader
.Characteristics
=
75 IMAGE_FILE_RELOCS_STRIPPED
|IMAGE_FILE_LINE_NUMS_STRIPPED
|
76 IMAGE_FILE_LOCAL_SYMS_STRIPPED
|IMAGE_FILE_32BIT_MACHINE
|
77 IMAGE_FILE_DLL
|IMAGE_FILE_DEBUG_STRIPPED
;
78 nth
->OptionalHeader
.Magic
= IMAGE_NT_OPTIONAL_HDR_MAGIC
;
79 nth
->OptionalHeader
.SizeOfCode
= 0;
80 nth
->OptionalHeader
.SizeOfInitializedData
= 0;
81 nth
->OptionalHeader
.SizeOfUninitializedData
= 0;
82 nth
->OptionalHeader
.AddressOfEntryPoint
= 0;
83 nth
->OptionalHeader
.BaseOfCode
= 0;
84 nth
->OptionalHeader
.MajorOperatingSystemVersion
= 4;
85 nth
->OptionalHeader
.MajorImageVersion
= 4;
86 nth
->OptionalHeader
.SizeOfImage
= 0;
87 nth
->OptionalHeader
.SizeOfHeaders
= 0;
88 nth
->OptionalHeader
.Subsystem
= IMAGE_SUBSYSTEM_NATIVE
;
89 nth
->OptionalHeader
.DllCharacteristics
= 0;
90 nth
->OptionalHeader
.NumberOfRvaAndSizes
= 0;
92 /* allocate one code section that crosses the whole process range
93 * (we could find out from internal tables ... hmm )
95 sh
=(PIMAGE_SECTION_HEADER
)(nth
+1);
96 strcpy(sh
->Name
,".text");
97 sh
->Misc
.VirtualSize
= STUBSIZE
;
98 sh
->VirtualAddress
= STUBOFFSET
; /* so snoop can use it ... */
99 sh
->SizeOfRawData
= STUBSIZE
;
100 sh
->PointerToRawData
= 0;
101 sh
->Characteristics
= IMAGE_SCN_CNT_CODE
|IMAGE_SCN_CNT_INITIALIZED_DATA
|IMAGE_SCN_MEM_EXECUTE
|IMAGE_SCN_MEM_READ
;
105 WINE_MODREF
*ELF_LoadLibraryExA( LPCSTR libname
, DWORD flags
)
109 char *modname
,*s
,*t
,*x
;
113 t
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
114 strlen(libname
) + strlen("lib.so") + 1 );
116 /* copy path to tempvar ... */
117 s
=strrchr(libname
,'/');
119 s
=strrchr(libname
,'\\');
121 s
++; /* skip / or \ */
122 /* copy everything up to s-1 */
123 memcpy(t
,libname
,s
-libname
);
128 /* append "lib" foo ".so" */
135 if (!FILE_strcasecmp(s
,".dll")) {
136 strcpy(s
+1,UNIX_DLL_ENDING
);
142 strcat(x
,"."UNIX_DLL_ENDING
);
145 /* grab just the last piece of the path/filename
146 which should be the name of the library we are
147 looking to load. increment by 1 to skip the DOS slash */
151 /* ... and open the library pointed by s, while t points
152 points to the ENTIRE DOS filename of the library
153 t is returned by HeapAlloc() above and so is also used
154 with HeapFree() below */
155 dlhandle
= wine_dlopen(s
,RTLD_NOW
,error
,sizeof(error
));
157 WARN("failed to load %s: %s\n", s
, error
);
158 HeapFree( GetProcessHeap(), 0, t
);
159 SetLastError( ERROR_FILE_NOT_FOUND
);
163 hmod
= ELF_CreateDummyModule( t
, modname
);
165 SNOOP_RegisterDLL(hmod
,libname
,STUBSIZE
/sizeof(ELF_STDCALL_STUB
));
167 wm
= PE_CreateModule( hmod
, libname
, 0, -1, FALSE
);
168 wm
->find_export
= ELF_FindExportedFunction
;
169 wm
->dlhandle
= dlhandle
;
173 static FARPROC
ELF_FindExportedFunction( WINE_MODREF
*wm
, LPCSTR funcName
, BOOL snoop
)
177 ELF_STDCALL_STUB
*stub
, *first_stub
;
180 if (!HIWORD(funcName
)) {
181 ERR("Can't import from UNIX dynamic libs by ordinal, sorry.\n");
184 fun
= wine_dlsym(wm
->dlhandle
,funcName
,error
,sizeof(error
));
187 /* we sometimes have an excess '_' at the beginning of the name */
188 if (funcName
[0]=='_')
191 fun
= wine_dlsym(wm
->dlhandle
,funcName
,error
,sizeof(error
));
195 /* Function@nrofargs usually marks a stdcall function
196 * with nrofargs bytes that are popped at the end
198 if (strchr(funcName
,'@')) {
199 LPSTR t
,fn
= HEAP_strdupA( GetProcessHeap(), 0, funcName
);
204 sscanf(t
+1,"%d",&nrofargs
);
205 fun
= wine_dlsym(wm
->dlhandle
,fn
,error
,sizeof(error
));
206 HeapFree( GetProcessHeap(), 0, fn
);
209 /* We sometimes have Win32 dlls implemented using stdcall but UNIX
210 * dlls using cdecl. If we find out the number of args the function
211 * uses, we remove them from the stack using two small stubs.
213 stub
= first_stub
= (ELF_STDCALL_STUB
*)((char *)wm
->module
+ STUBOFFSET
);
214 for (i
=0;i
<STUBSIZE
/sizeof(ELF_STDCALL_STUB
);i
++) {
217 if (stub
->origfun
== (DWORD
)fun
)
221 if (i
==STUBSIZE
/sizeof(ELF_STDCALL_STUB
)) {
222 ERR("please report, that there are not enough slots for stdcall stubs in the ELF loader.\n");
223 assert(i
<STUBSIZE
/sizeof(ELF_STDCALL_STUB
));
226 stub
->origfun
=(DWORD
)fun
; /* just a marker */
228 if (fun
&& nrofargs
) { /* we don't need it for 0 args */
229 /* Selfmodifying entry/return stub for stdcall -> cdecl
231 * - Pop returnaddress directly into our return code
232 * popl <into code below>
233 * - Replace it by pointer to start of our returncode
235 * - And call the original function
237 * - Remove the arguments no longer needed
238 * newret: add esp, <nrofargs>
239 * - Push the original returnvalue on the stack
240 * pushl <poppedvalue>
241 * - And return to it.
245 /* FIXME: The function stub is not reentrant. */
247 ((LPBYTE
)&(stub
->popl
))[0] = 0x8f;
248 ((LPBYTE
)&(stub
->popl
))[1] = 0x05;
249 stub
->addr_popped
= (DWORD
)&(stub
->oldret
);
251 stub
->newret
= (DWORD
)&(stub
->addesp
);
253 stub
->origfun
= (DWORD
)fun
;
255 ((LPBYTE
)&(stub
->addesp
))[0]=0x83;
256 ((LPBYTE
)&(stub
->addesp
))[1]=0xc4;
257 stub
->nrofargs
= nrofargs
;
259 /* filled out by entrycode */
260 stub
->oldret
= 0xdeadbeef;
265 FIXME("function %s not found: %s\n",funcName
,error
);
268 fun
= SNOOP_GetProcAddress(wm
->module
,funcName
,stub
-first_stub
,fun
);
272 #else /* HAVE_DL_API */
274 WINE_MODREF
*ELF_LoadLibraryExA( LPCSTR libname
, DWORD flags
)
279 #endif /* HAVE_DL_API */