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>
28 WINE_MODREF *ELF_CreateDummyModule( LPCSTR libname, LPCSTR modname )
31 PIMAGE_NT_HEADERS nth;
32 PIMAGE_SECTION_HEADER sh;
36 wm=(WINE_MODREF*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wm) );
37 wm->type = MODULE32_ELF;
39 /* FIXME: hmm, order? */
40 wm->next = PROCESS_Current()->modref_list;
41 PROCESS_Current()->modref_list = wm;
43 wm->modname = HEAP_strdupA( GetProcessHeap(), 0, modname );
44 wm->longname = HEAP_strdupA( GetProcessHeap(), 0, libname );
46 hmod = (HMODULE)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
47 sizeof(IMAGE_DOS_HEADER) +
48 sizeof(IMAGE_NT_HEADERS) +
49 sizeof(IMAGE_SECTION_HEADER) + 100 );
50 dh = (PIMAGE_DOS_HEADER)hmod;
51 dh->e_magic = IMAGE_DOS_SIGNATURE;
52 dh->e_lfanew = sizeof(IMAGE_DOS_HEADER);
53 nth = PE_HEADER(hmod);
54 nth->Signature = IMAGE_NT_SIGNATURE;
55 nth->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
56 nth->FileHeader.NumberOfSections = 1;
57 nth->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
58 nth->FileHeader.Characteristics =
59 IMAGE_FILE_RELOCS_STRIPPED|IMAGE_FILE_LINE_NUMS_STRIPPED|
60 IMAGE_FILE_LOCAL_SYMS_STRIPPED|IMAGE_FILE_32BIT_MACHINE|
61 IMAGE_FILE_DLL|IMAGE_FILE_DEBUG_STRIPPED;
62 nth->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
63 nth->OptionalHeader.SizeOfCode = 0;
64 nth->OptionalHeader.SizeOfInitializedData = 0;
65 nth->OptionalHeader.SizeOfUninitializedData = 0;
66 nth->OptionalHeader.AddressOfEntryPoint = 0;
67 nth->OptionalHeader.BaseOfCode = 0;
68 nth->OptionalHeader.MajorOperatingSystemVersion = 4;
69 nth->OptionalHeader.MajorImageVersion = 4;
70 nth->OptionalHeader.SizeOfImage = 0;
71 nth->OptionalHeader.SizeOfHeaders = 0;
72 nth->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_NATIVE;
73 nth->OptionalHeader.DllCharacteristics = 0;
74 nth->OptionalHeader.NumberOfRvaAndSizes = 0;
76 /* allocate one code section that crosses the whole process range
77 * (we could find out from internal tables ... hmm )
79 sh=(PIMAGE_SECTION_HEADER)(nth+1);
80 strcpy(sh->Name,".text");
81 sh->Misc.VirtualSize = 0x7fffffff;
82 sh->VirtualAddress = 0x42; /* so snoop can use it ... */
83 sh->SizeOfRawData = 0x7fffffff;
84 sh->PointerToRawData = 0;
85 sh->Characteristics = IMAGE_SCN_CNT_CODE|IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_EXECUTE|IMAGE_SCN_MEM_READ;
91 #if defined(HAVE_LIBDL) && defined(HAVE_DLFCN_H)
93 #define UNIX_DLL_ENDING "so"
99 HMODULE ELF_LoadLibraryExA( LPCSTR libname, HANDLE hf, DWORD flags )
102 char *modname,*s,*t,*x;
105 t = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
106 strlen(libname) + strlen("lib.so") + 1 );
108 /* copy path to tempvar ... */
109 s=strrchr(libname,'/');
111 s=strrchr(libname,'\\');
113 strncpy(t,libname,s-libname+1);
114 t[s-libname+1]= '\0';
118 /* append "lib" foo ".so" */
124 if (!strcasecmp(s,".dll")) {
125 strcpy(s+1,UNIX_DLL_ENDING);
131 /* FIXME: make UNIX filename from DOS fn? */
133 /* ... and open it */
134 dlhandle = dlopen(t,RTLD_NOW);
136 HeapFree( GetProcessHeap(), 0, t );
140 wm = ELF_CreateDummyModule( t, modname );
141 wm->binfmt.elf.dlhandle = dlhandle;
143 SNOOP_RegisterDLL(wm->module,libname,STUBSIZE/sizeof(ELF_STDCALL_STUB));
147 FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName)
151 ELF_STDCALL_STUB *stub;
153 assert(wm->type == MODULE32_ELF);
154 if (!HIWORD(funcName)) {
155 ERR(win32,"Can't import from UNIX dynamic libs by ordinal, sorry.\n");
158 fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
159 /* we sometimes have an excess '_' at the beginning of the name */
160 if (!fun && (funcName[0]=='_')) {
162 fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
165 /* Function@nrofargs usually marks a stdcall function
166 * with nrofargs bytes that are popped at the end
168 if (strchr(funcName,'@')) {
169 LPSTR t,fn = HEAP_strdupA( GetProcessHeap(), 0, funcName );
174 sscanf(t+1,"%d",&nrofargs);
175 fun = dlsym(wm->binfmt.elf.dlhandle,fn);
176 HeapFree( GetProcessHeap(), 0, fn );
179 /* We sometimes have Win32 dlls implemented using stdcall but UNIX
180 * dlls using cdecl. If we find out the number of args the function
181 * uses, we remove them from the stack using two small stubs.
183 if (!wm->binfmt.elf.stubs) {
184 /* one page should suffice */
185 wm->binfmt.elf.stubs = VirtualAlloc(NULL,STUBSIZE,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
186 memset(wm->binfmt.elf.stubs,0,STUBSIZE);
188 stub = wm->binfmt.elf.stubs;
189 for (i=0;i<STUBSIZE/sizeof(ELF_STDCALL_STUB);i++) {
192 if (stub->origfun == (DWORD)fun)
196 if (i==STUBSIZE/sizeof(ELF_STDCALL_STUB)) {
197 ERR(win32,"please report, that there are not enough slots for stdcall stubs in the ELF loader.\n");
198 assert(i<STUBSIZE/sizeof(ELF_STDCALL_STUB));
201 stub->origfun=(DWORD)fun; /* just a marker */
203 if (fun && nrofargs) { /* we don't need it for 0 args */
204 /* Selfmodifying entry/return stub for stdcall -> cdecl
206 * - Pop returnaddress directly into our return code
207 * popl <into code below>
208 * - Replace it by pointer to start of our returncode
210 * - And call the original function
212 * - Remove the arguments no longer needed
213 * newret: add esp, <nrofargs>
214 * - Push the original returnvalue on the stack
215 * pushl <poppedvalue>
216 * - And return to it.
220 /* FIXME: The function stub is not reentrant. */
222 ((LPBYTE)&(stub->popl))[0] = 0x8f;
223 ((LPBYTE)&(stub->popl))[1] = 0x05;
224 stub->addr_popped = (DWORD)&(stub->oldret);
226 stub->newret = (DWORD)&(stub->addesp);
228 stub->origfun = (DWORD)fun;
230 ((LPBYTE)&(stub->addesp))[0]=0x83;
231 ((LPBYTE)&(stub->addesp))[1]=0xc4;
232 stub->nrofargs = nrofargs;
234 /* filled out by entrycode */
235 stub->oldret = 0xdeadbeef;
240 FIXME(win32,"function %s not found: %s\n",funcName,dlerror());
243 fun = SNOOP_GetProcAddress(wm->module,funcName,stub-wm->binfmt.elf.stubs,fun);
248 HMODULE ELF_LoadLibraryExA( LPCSTR libname, HANDLE hf, DWORD flags)
252 FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName)