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>
30 ELF_CreateDummyModule( LPCSTR libname
, LPCSTR modname
, PDB32
*process
)
33 PIMAGE_NT_HEADERS nth
;
34 PIMAGE_SECTION_HEADER sh
;
38 wm
=(WINE_MODREF
*)HeapAlloc(process
->heap
,HEAP_ZERO_MEMORY
,sizeof(*wm
));
39 wm
->type
= MODULE32_ELF
;
41 /* FIXME: hmm, order? */
42 wm
->next
= process
->modref_list
;
43 process
->modref_list
= wm
;
45 wm
->modname
= HEAP_strdupA(process
->heap
,0,modname
);
46 wm
->longname
= HEAP_strdupA(process
->heap
,0,libname
);
48 hmod
= (HMODULE32
)HeapAlloc(process
->heap
,HEAP_ZERO_MEMORY
,sizeof(IMAGE_DOS_HEADER
)+sizeof(IMAGE_NT_HEADERS
)+sizeof(IMAGE_SECTION_HEADER
)+100);
49 dh
= (PIMAGE_DOS_HEADER
)hmod
;
50 dh
->e_magic
= IMAGE_DOS_SIGNATURE
;
51 dh
->e_lfanew
= sizeof(IMAGE_DOS_HEADER
);
52 nth
= PE_HEADER(hmod
);
53 nth
->Signature
= IMAGE_NT_SIGNATURE
;
54 nth
->FileHeader
.Machine
= IMAGE_FILE_MACHINE_I386
;
55 nth
->FileHeader
.NumberOfSections
= 1;
56 nth
->FileHeader
.SizeOfOptionalHeader
= sizeof(IMAGE_OPTIONAL_HEADER
);
57 nth
->FileHeader
.Characteristics
=
58 IMAGE_FILE_RELOCS_STRIPPED
|IMAGE_FILE_LINE_NUMS_STRIPPED
|
59 IMAGE_FILE_LOCAL_SYMS_STRIPPED
|IMAGE_FILE_32BIT_MACHINE
|
60 IMAGE_FILE_DLL
|IMAGE_FILE_DEBUG_STRIPPED
;
61 nth
->OptionalHeader
.Magic
= IMAGE_NT_OPTIONAL_HDR_MAGIC
;
62 nth
->OptionalHeader
.SizeOfCode
= 0;
63 nth
->OptionalHeader
.SizeOfInitializedData
= 0;
64 nth
->OptionalHeader
.SizeOfUninitializedData
= 0;
65 nth
->OptionalHeader
.AddressOfEntryPoint
= 0;
66 nth
->OptionalHeader
.BaseOfCode
= 0;
67 nth
->OptionalHeader
.MajorOperatingSystemVersion
= 4;
68 nth
->OptionalHeader
.MajorImageVersion
= 4;
69 nth
->OptionalHeader
.SizeOfImage
= 0;
70 nth
->OptionalHeader
.SizeOfHeaders
= 0;
71 nth
->OptionalHeader
.Subsystem
= IMAGE_SUBSYSTEM_NATIVE
;
72 nth
->OptionalHeader
.DllCharacteristics
= 0;
73 nth
->OptionalHeader
.NumberOfRvaAndSizes
= 0;
75 /* allocate one code section that crosses the whole process range
76 * (we could find out from internal tables ... hmm )
78 sh
=(PIMAGE_SECTION_HEADER
)(nth
+1);
79 strcpy(sh
->Name
,".text");
80 sh
->Misc
.VirtualSize
= 0x7fffffff;
81 sh
->VirtualAddress
= 0x42; /* so snoop can use it ... */
82 sh
->SizeOfRawData
= 0x7fffffff;
83 sh
->PointerToRawData
= 0;
84 sh
->Characteristics
= IMAGE_SCN_CNT_CODE
|IMAGE_SCN_CNT_INITIALIZED_DATA
|IMAGE_SCN_MEM_EXECUTE
|IMAGE_SCN_MEM_READ
;
90 #if defined(HAVE_LIBDL) && defined(HAVE_DLFCN_H)
92 #define UNIX_DLL_ENDING "so"
99 ELF_LoadLibraryEx32A(LPCSTR libname
,PDB32
*process
,HANDLE32 hf
,DWORD flags
) {
101 char *modname
,*s
,*t
,*x
;
104 t
= HeapAlloc(process
->heap
,HEAP_ZERO_MEMORY
,strlen(libname
)+strlen("lib.so")+1);
106 /* copy path to tempvar ... */
107 s
=strrchr(libname
,'/');
109 s
=strrchr(libname
,'\\');
111 strncpy(t
,libname
,s
-libname
+1);
112 t
[s
-libname
+1]= '\0';
116 /* append "lib" foo ".so" */
122 if (!strcasecmp(s
,".dll")) {
123 strcpy(s
+1,UNIX_DLL_ENDING
);
129 /* FIXME: make UNIX filename from DOS fn? */
131 /* ... and open it */
132 dlhandle
= dlopen(t
,RTLD_NOW
);
134 HeapFree(process
->heap
,0,t
);
138 wm
= ELF_CreateDummyModule( t
, modname
, process
);
139 wm
->binfmt
.elf
.dlhandle
= dlhandle
;
141 SNOOP_RegisterDLL(wm
->module
,libname
,STUBSIZE
/sizeof(ELF_STDCALL_STUB
));
146 ELF_FindExportedFunction( PDB32
*process
,WINE_MODREF
*wm
, LPCSTR funcName
) {
149 ELF_STDCALL_STUB
*stub
;
151 assert(wm
->type
== MODULE32_ELF
);
152 if (!HIWORD(funcName
)) {
153 ERR(win32
,"Can't import from UNIX dynamic libs by ordinal, sorry.\n");
156 fun
= dlsym(wm
->binfmt
.elf
.dlhandle
,funcName
);
157 /* we sometimes have an excess '_' at the beginning of the name */
158 if (!fun
&& (funcName
[0]=='_')) {
160 fun
= dlsym(wm
->binfmt
.elf
.dlhandle
,funcName
);
163 /* Function@nrofargs usually marks a stdcall function
164 * with nrofargs bytes that are popped at the end
166 if (strchr(funcName
,'@')) {
167 LPSTR t
,fn
= HEAP_strdupA(process
->heap
,0,funcName
);
172 sscanf(t
+1,"%d",&nrofargs
);
173 fun
= dlsym(wm
->binfmt
.elf
.dlhandle
,fn
);
174 HeapFree(process
->heap
,0,fn
);
177 /* We sometimes have Win32 dlls implemented using stdcall but UNIX
178 * dlls using cdecl. If we find out the number of args the function
179 * uses, we remove them from the stack using two small stubs.
181 if (!wm
->binfmt
.elf
.stubs
) {
182 /* one page should suffice */
183 wm
->binfmt
.elf
.stubs
= VirtualAlloc(NULL
,STUBSIZE
,MEM_COMMIT
|MEM_RESERVE
,PAGE_EXECUTE_READWRITE
);
184 memset(wm
->binfmt
.elf
.stubs
,0,STUBSIZE
);
186 stub
= wm
->binfmt
.elf
.stubs
;
187 for (i
=0;i
<STUBSIZE
/sizeof(ELF_STDCALL_STUB
);i
++) {
190 if (stub
->origfun
== (DWORD
)fun
)
194 if (i
==STUBSIZE
/sizeof(ELF_STDCALL_STUB
)) {
195 ERR(win32
,"please report, that there are not enough slots for stdcall stubs in the ELF loader.\n");
196 assert(i
<STUBSIZE
/sizeof(ELF_STDCALL_STUB
));
199 stub
->origfun
=(DWORD
)fun
; /* just a marker */
201 if (fun
&& nrofargs
) { /* we don't need it for 0 args */
202 /* Selfmodifying entry/return stub for stdcall -> cdecl
204 * - Pop returnaddress directly into our return code
205 * popl <into code below>
206 * - Replace it by pointer to start of our returncode
208 * - And call the original function
210 * - Remove the arguments no longer needed
211 * newret: add esp, <nrofargs>
212 * - Push the original returnvalue on the stack
213 * pushl <poppedvalue>
214 * - And return to it.
218 /* FIXME: The function stub is not reentrant. */
220 ((LPBYTE
)&(stub
->popl
))[0] = 0x8f;
221 ((LPBYTE
)&(stub
->popl
))[1] = 0x05;
222 stub
->addr_popped
= (DWORD
)&(stub
->oldret
);
224 stub
->newret
= (DWORD
)&(stub
->addesp
);
226 stub
->origfun
= (DWORD
)fun
;
228 ((LPBYTE
)&(stub
->addesp
))[0]=0x83;
229 ((LPBYTE
)&(stub
->addesp
))[1]=0xc4;
230 stub
->nrofargs
= nrofargs
;
232 /* filled out by entrycode */
233 stub
->oldret
= 0xdeadbeef;
238 FIXME(win32
,"function %s not found: %s\n",funcName
,dlerror());
241 fun
= SNOOP_GetProcAddress32(wm
->module
,funcName
,stub
-wm
->binfmt
.elf
.stubs
,fun
);
242 return (FARPROC32
)fun
;
247 ELF_LoadLibraryEx32A(LPCSTR libname
,PDB32
*process
,HANDLE32 hf
,DWORD flags
) {
251 ELF_FindExportedFunction( PDB32
*process
,WINE_MODREF
*wm
, LPCSTR funcName
) {