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>
26 #include "debugtools.h"
30 DEFAULT_DEBUG_CHANNEL(win32)
32 WINE_MODREF *ELF_CreateDummyModule( LPCSTR libname, LPCSTR modname )
35 PIMAGE_NT_HEADERS nth;
36 PIMAGE_SECTION_HEADER sh;
40 wm=(WINE_MODREF*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wm) );
41 wm->type = MODULE32_ELF;
43 /* FIXME: hmm, order? */
44 wm->next = PROCESS_Current()->modref_list;
45 PROCESS_Current()->modref_list = wm;
47 wm->modname = HEAP_strdupA( GetProcessHeap(), 0, modname );
48 wm->longname = HEAP_strdupA( GetProcessHeap(), 0, libname );
50 hmod = (HMODULE)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
51 sizeof(IMAGE_DOS_HEADER) +
52 sizeof(IMAGE_NT_HEADERS) +
53 sizeof(IMAGE_SECTION_HEADER) + 100 );
54 dh = (PIMAGE_DOS_HEADER)hmod;
55 dh->e_magic = IMAGE_DOS_SIGNATURE;
56 dh->e_lfanew = sizeof(IMAGE_DOS_HEADER);
57 nth = PE_HEADER(hmod);
58 nth->Signature = IMAGE_NT_SIGNATURE;
59 nth->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
60 nth->FileHeader.NumberOfSections = 1;
61 nth->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
62 nth->FileHeader.Characteristics =
63 IMAGE_FILE_RELOCS_STRIPPED|IMAGE_FILE_LINE_NUMS_STRIPPED|
64 IMAGE_FILE_LOCAL_SYMS_STRIPPED|IMAGE_FILE_32BIT_MACHINE|
65 IMAGE_FILE_DLL|IMAGE_FILE_DEBUG_STRIPPED;
66 nth->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
67 nth->OptionalHeader.SizeOfCode = 0;
68 nth->OptionalHeader.SizeOfInitializedData = 0;
69 nth->OptionalHeader.SizeOfUninitializedData = 0;
70 nth->OptionalHeader.AddressOfEntryPoint = 0;
71 nth->OptionalHeader.BaseOfCode = 0;
72 nth->OptionalHeader.MajorOperatingSystemVersion = 4;
73 nth->OptionalHeader.MajorImageVersion = 4;
74 nth->OptionalHeader.SizeOfImage = 0;
75 nth->OptionalHeader.SizeOfHeaders = 0;
76 nth->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_NATIVE;
77 nth->OptionalHeader.DllCharacteristics = 0;
78 nth->OptionalHeader.NumberOfRvaAndSizes = 0;
80 /* allocate one code section that crosses the whole process range
81 * (we could find out from internal tables ... hmm )
83 sh=(PIMAGE_SECTION_HEADER)(nth+1);
84 strcpy(sh->Name,".text");
85 sh->Misc.VirtualSize = 0x7fffffff;
86 sh->VirtualAddress = 0x42; /* so snoop can use it ... */
87 sh->SizeOfRawData = 0x7fffffff;
88 sh->PointerToRawData = 0;
89 sh->Characteristics = IMAGE_SCN_CNT_CODE|IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_EXECUTE|IMAGE_SCN_MEM_READ;
95 #if defined(HAVE_DL_API)
97 #define UNIX_DLL_ENDING "so"
103 WINE_MODREF *ELF_LoadLibraryExA( LPCSTR libname, DWORD flags, DWORD *err)
106 char *modname,*s,*t,*x;
109 t = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
110 strlen(libname) + strlen("lib.so") + 1 );
112 /* copy path to tempvar ... */
113 s=strrchr(libname,'/');
115 s=strrchr(libname,'\\');
117 s++; /* skip / or \ */
118 /* copy everything up to s-1 */
119 memcpy(t,libname,s-libname);
124 /* append "lib" foo ".so" */
131 if (!strcasecmp(s,".dll")) {
132 strcpy(s+1,UNIX_DLL_ENDING);
138 strcat(x,"."UNIX_DLL_ENDING);
141 /* FIXME: make UNIX filename from DOS fn? */
143 /* ... and open it */
144 dlhandle = ELFDLL_dlopen(t,RTLD_NOW);
146 HeapFree( GetProcessHeap(), 0, t );
147 *err = ERROR_FILE_NOT_FOUND;
151 wm = ELF_CreateDummyModule( t, modname );
152 wm->binfmt.elf.dlhandle = dlhandle;
154 SNOOP_RegisterDLL(wm->module,libname,STUBSIZE/sizeof(ELF_STDCALL_STUB));
159 FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName)
163 ELF_STDCALL_STUB *stub;
165 assert(wm->type == MODULE32_ELF);
166 if (!HIWORD(funcName)) {
167 ERR("Can't import from UNIX dynamic libs by ordinal, sorry.\n");
170 fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
171 /* we sometimes have an excess '_' at the beginning of the name */
172 if (!fun && (funcName[0]=='_')) {
174 fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
177 /* Function@nrofargs usually marks a stdcall function
178 * with nrofargs bytes that are popped at the end
180 if (strchr(funcName,'@')) {
181 LPSTR t,fn = HEAP_strdupA( GetProcessHeap(), 0, funcName );
186 sscanf(t+1,"%d",&nrofargs);
187 fun = dlsym(wm->binfmt.elf.dlhandle,fn);
188 HeapFree( GetProcessHeap(), 0, fn );
191 /* We sometimes have Win32 dlls implemented using stdcall but UNIX
192 * dlls using cdecl. If we find out the number of args the function
193 * uses, we remove them from the stack using two small stubs.
195 if (!wm->binfmt.elf.stubs) {
196 /* one page should suffice */
197 wm->binfmt.elf.stubs = VirtualAlloc(NULL,STUBSIZE,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
198 memset(wm->binfmt.elf.stubs,0,STUBSIZE);
200 stub = wm->binfmt.elf.stubs;
201 for (i=0;i<STUBSIZE/sizeof(ELF_STDCALL_STUB);i++) {
204 if (stub->origfun == (DWORD)fun)
208 if (i==STUBSIZE/sizeof(ELF_STDCALL_STUB)) {
209 ERR("please report, that there are not enough slots for stdcall stubs in the ELF loader.\n");
210 assert(i<STUBSIZE/sizeof(ELF_STDCALL_STUB));
213 stub->origfun=(DWORD)fun; /* just a marker */
215 if (fun && nrofargs) { /* we don't need it for 0 args */
216 /* Selfmodifying entry/return stub for stdcall -> cdecl
218 * - Pop returnaddress directly into our return code
219 * popl <into code below>
220 * - Replace it by pointer to start of our returncode
222 * - And call the original function
224 * - Remove the arguments no longer needed
225 * newret: add esp, <nrofargs>
226 * - Push the original returnvalue on the stack
227 * pushl <poppedvalue>
228 * - And return to it.
232 /* FIXME: The function stub is not reentrant. */
234 ((LPBYTE)&(stub->popl))[0] = 0x8f;
235 ((LPBYTE)&(stub->popl))[1] = 0x05;
236 stub->addr_popped = (DWORD)&(stub->oldret);
238 stub->newret = (DWORD)&(stub->addesp);
240 stub->origfun = (DWORD)fun;
242 ((LPBYTE)&(stub->addesp))[0]=0x83;
243 ((LPBYTE)&(stub->addesp))[1]=0xc4;
244 stub->nrofargs = nrofargs;
246 /* filled out by entrycode */
247 stub->oldret = 0xdeadbeef;
252 FIXME("function %s not found: %s\n",funcName,dlerror());
255 fun = SNOOP_GetProcAddress(wm->module,funcName,stub-wm->binfmt.elf.stubs,fun);
260 /***************************************************************************
263 * Unload the elf library and free the modref
265 void ELF_UnloadLibrary(WINE_MODREF *wm)
267 /* FIXME: do something here */
272 WINE_MODREF *ELF_LoadLibraryExA( LPCSTR libname, DWORD flags, DWORD *err)
277 void ELF_UnloadLibrary(WINE_MODREF *wm)
281 FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName)