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" */
125 if (!strcasecmp(s,".dll")) {
126 strcpy(s+1,UNIX_DLL_ENDING);
132 strcat(x,"."UNIX_DLL_ENDING);
135 /* FIXME: make UNIX filename from DOS fn? */
137 /* ... and open it */
138 dlhandle = dlopen(t,RTLD_NOW);
140 HeapFree( GetProcessHeap(), 0, t );
144 wm = ELF_CreateDummyModule( t, modname );
145 wm->binfmt.elf.dlhandle = dlhandle;
147 SNOOP_RegisterDLL(wm->module,libname,STUBSIZE/sizeof(ELF_STDCALL_STUB));
151 FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName)
155 ELF_STDCALL_STUB *stub;
157 assert(wm->type == MODULE32_ELF);
158 if (!HIWORD(funcName)) {
159 ERR(win32,"Can't import from UNIX dynamic libs by ordinal, sorry.\n");
162 fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
163 /* we sometimes have an excess '_' at the beginning of the name */
164 if (!fun && (funcName[0]=='_')) {
166 fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
169 /* Function@nrofargs usually marks a stdcall function
170 * with nrofargs bytes that are popped at the end
172 if (strchr(funcName,'@')) {
173 LPSTR t,fn = HEAP_strdupA( GetProcessHeap(), 0, funcName );
178 sscanf(t+1,"%d",&nrofargs);
179 fun = dlsym(wm->binfmt.elf.dlhandle,fn);
180 HeapFree( GetProcessHeap(), 0, fn );
183 /* We sometimes have Win32 dlls implemented using stdcall but UNIX
184 * dlls using cdecl. If we find out the number of args the function
185 * uses, we remove them from the stack using two small stubs.
187 if (!wm->binfmt.elf.stubs) {
188 /* one page should suffice */
189 wm->binfmt.elf.stubs = VirtualAlloc(NULL,STUBSIZE,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
190 memset(wm->binfmt.elf.stubs,0,STUBSIZE);
192 stub = wm->binfmt.elf.stubs;
193 for (i=0;i<STUBSIZE/sizeof(ELF_STDCALL_STUB);i++) {
196 if (stub->origfun == (DWORD)fun)
200 if (i==STUBSIZE/sizeof(ELF_STDCALL_STUB)) {
201 ERR(win32,"please report, that there are not enough slots for stdcall stubs in the ELF loader.\n");
202 assert(i<STUBSIZE/sizeof(ELF_STDCALL_STUB));
205 stub->origfun=(DWORD)fun; /* just a marker */
207 if (fun && nrofargs) { /* we don't need it for 0 args */
208 /* Selfmodifying entry/return stub for stdcall -> cdecl
210 * - Pop returnaddress directly into our return code
211 * popl <into code below>
212 * - Replace it by pointer to start of our returncode
214 * - And call the original function
216 * - Remove the arguments no longer needed
217 * newret: add esp, <nrofargs>
218 * - Push the original returnvalue on the stack
219 * pushl <poppedvalue>
220 * - And return to it.
224 /* FIXME: The function stub is not reentrant. */
226 ((LPBYTE)&(stub->popl))[0] = 0x8f;
227 ((LPBYTE)&(stub->popl))[1] = 0x05;
228 stub->addr_popped = (DWORD)&(stub->oldret);
230 stub->newret = (DWORD)&(stub->addesp);
232 stub->origfun = (DWORD)fun;
234 ((LPBYTE)&(stub->addesp))[0]=0x83;
235 ((LPBYTE)&(stub->addesp))[1]=0xc4;
236 stub->nrofargs = nrofargs;
238 /* filled out by entrycode */
239 stub->oldret = 0xdeadbeef;
244 FIXME(win32,"function %s not found: %s\n",funcName,dlerror());
247 fun = SNOOP_GetProcAddress(wm->module,funcName,stub-wm->binfmt.elf.stubs,fun);
252 HMODULE ELF_LoadLibraryExA( LPCSTR libname, HANDLE hf, DWORD flags)
256 FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName)