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>
29 ELF_CreateDummyModule( LPCSTR libname, LPCSTR modname, PDB32 *process )
32 PIMAGE_NT_HEADERS nth;
33 PIMAGE_SECTION_HEADER sh;
37 wm=(WINE_MODREF*)HeapAlloc(process->heap,HEAP_ZERO_MEMORY,sizeof(*wm));
38 wm->type = MODULE32_ELF;
40 /* FIXME: hmm, order? */
41 wm->next = process->modref_list;
42 process->modref_list = wm;
44 wm->modname = HEAP_strdupA(process->heap,0,modname);
45 wm->longname = HEAP_strdupA(process->heap,0,libname);
47 hmod = (HMODULE32)HeapAlloc(process->heap,HEAP_ZERO_MEMORY,sizeof(IMAGE_DOS_HEADER)+sizeof(IMAGE_NT_HEADERS)+sizeof(IMAGE_SECTION_HEADER)+100);
48 dh = (PIMAGE_DOS_HEADER)hmod;
49 dh->e_magic = IMAGE_DOS_SIGNATURE;
50 dh->e_lfanew = sizeof(IMAGE_DOS_HEADER);
51 nth = PE_HEADER(hmod);
52 nth->Signature = IMAGE_NT_SIGNATURE;
53 nth->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
54 nth->FileHeader.NumberOfSections = 1;
55 nth->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
56 nth->FileHeader.Characteristics =
57 IMAGE_FILE_RELOCS_STRIPPED|IMAGE_FILE_LINE_NUMS_STRIPPED|
58 IMAGE_FILE_LOCAL_SYMS_STRIPPED|IMAGE_FILE_32BIT_MACHINE|
59 IMAGE_FILE_DLL|IMAGE_FILE_DEBUG_STRIPPED;
60 nth->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
61 nth->OptionalHeader.SizeOfCode = 0;
62 nth->OptionalHeader.SizeOfInitializedData = 0;
63 nth->OptionalHeader.SizeOfUninitializedData = 0;
64 nth->OptionalHeader.AddressOfEntryPoint = 0;
65 nth->OptionalHeader.BaseOfCode = 0;
66 nth->OptionalHeader.MajorOperatingSystemVersion = 4;
67 nth->OptionalHeader.MajorImageVersion = 4;
68 nth->OptionalHeader.SizeOfImage = 0;
69 nth->OptionalHeader.SizeOfHeaders = 0;
70 nth->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_NATIVE;
71 nth->OptionalHeader.DllCharacteristics = 0;
72 nth->OptionalHeader.NumberOfRvaAndSizes = 0;
74 /* allocate one code section that crosses the whole process range
75 * (we could find out from internal tables ... hmm )
77 sh=(PIMAGE_SECTION_HEADER)(nth+1);
78 strcpy(sh->Name,".text");
79 sh->Misc.VirtualSize = 0x7fffffff;
80 sh->VirtualAddress = 0x42; /* so snoop can use it ... */
81 sh->SizeOfRawData = 0x7fffffff;
82 sh->PointerToRawData = 0;
83 sh->Characteristics = IMAGE_SCN_CNT_CODE|IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_EXECUTE|IMAGE_SCN_MEM_READ;
89 #if defined(HAVE_LIBDL) && defined(HAVE_DLFCN_H)
91 #define UNIX_DLL_ENDING "so"
98 ELF_LoadLibraryEx32A(LPCSTR libname,PDB32 *process,HANDLE32 hf,DWORD flags) {
100 char *modname,*s,*t,*x;
103 t = HeapAlloc(process->heap,HEAP_ZERO_MEMORY,strlen(libname)+strlen("lib.so")+1);
105 /* copy path to tempvar ... */
106 s=strrchr(libname,'/');
108 s=strrchr(libname,'\\');
110 strncpy(t,libname,s-libname+1);
111 t[s-libname+1]= '\0';
115 /* append "lib" foo ".so" */
121 if (!strcasecmp(s,".dll")) {
122 strcpy(s+1,UNIX_DLL_ENDING);
128 /* FIXME: make UNIX filename from DOS fn? */
130 /* ... and open it */
131 dlhandle = dlopen(t,RTLD_NOW);
133 HeapFree(process->heap,0,t);
137 wm = ELF_CreateDummyModule( t, modname, process );
138 wm->binfmt.elf.dlhandle = dlhandle;
140 SNOOP_RegisterDLL(wm->module,libname,STUBSIZE/sizeof(ELF_STDCALL_STUB));
145 ELF_FindExportedFunction( PDB32 *process,WINE_MODREF *wm, LPCSTR funcName) {
148 ELF_STDCALL_STUB *stub;
150 assert(wm->type == MODULE32_ELF);
151 if (!HIWORD(funcName)) {
152 ERR(win32,"Can't import from UNIX dynamic libs by ordinal, sorry.\n");
155 fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
156 /* we sometimes have an excess '_' at the beginning of the name */
157 if (!fun && (funcName[0]=='_')) {
159 fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
162 /* Function@nrofargs usually marks a stdcall function
163 * with nrofargs bytes that are popped at the end
165 if (strchr(funcName,'@')) {
166 LPSTR t,fn = HEAP_strdupA(process->heap,0,funcName);
171 sscanf(t+1,"%d",&nrofargs);
172 fun = dlsym(wm->binfmt.elf.dlhandle,fn);
173 HeapFree(process->heap,0,fn);
176 /* We sometimes have Win32 dlls implemented using stdcall but UNIX
177 * dlls using cdecl. If we find out the number of args the function
178 * uses, we remove them from the stack using two small stubs.
180 if (!wm->binfmt.elf.stubs) {
181 /* one page should suffice */
182 wm->binfmt.elf.stubs = VirtualAlloc(NULL,STUBSIZE,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
183 memset(wm->binfmt.elf.stubs,0,STUBSIZE);
185 stub = wm->binfmt.elf.stubs;
186 for (i=0;i<STUBSIZE/sizeof(ELF_STDCALL_STUB);i++) {
189 if (stub->origfun == (DWORD)fun)
193 if (i==STUBSIZE/sizeof(ELF_STDCALL_STUB)) {
194 ERR(win32,"please report, that there are not enough slots for stdcall stubs in the ELF loader.\n");
195 assert(i<STUBSIZE/sizeof(ELF_STDCALL_STUB));
198 stub->origfun=(DWORD)fun; /* just a marker */
200 if (fun && nrofargs) { /* we don't need it for 0 args */
201 /* Selfmodifying entry/return stub for stdcall -> cdecl
203 * - Pop returnaddress directly into our return code
204 * popl <into code below>
205 * - Replace it by pointer to start of our returncode
207 * - And call the original function
209 * - Remove the arguments no longer needed
210 * newret: add esp, <nrofargs>
211 * - Push the original returnvalue on the stack
212 * pushl <poppedvalue>
213 * - And return to it.
217 /* FIXME: The function stub is not reentrant. */
219 ((LPBYTE)&(stub->popl))[0] = 0x8f;
220 ((LPBYTE)&(stub->popl))[1] = 0x05;
221 stub->addr_popped = (DWORD)&(stub->oldret);
223 stub->newret = (DWORD)&(stub->addesp);
225 stub->origfun = (DWORD)fun;
227 ((LPBYTE)&(stub->addesp))[0]=0x83;
228 ((LPBYTE)&(stub->addesp))[1]=0xc4;
229 stub->nrofargs = nrofargs;
231 /* filled out by entrycode */
232 stub->oldret = 0xdeadbeef;
237 FIXME(win32,"function %s not found: %s\n",funcName,dlerror());
240 fun = SNOOP_GetProcAddress32(wm->module,funcName,stub-wm->binfmt.elf.stubs,fun);
241 return (FARPROC32)fun;
246 ELF_LoadLibraryEx32A(LPCSTR libname,PDB32 *process,HANDLE32 hf,DWORD flags) {
250 ELF_FindExportedFunction( PDB32 *process,WINE_MODREF *wm, LPCSTR funcName) {