2 * 386-specific Win16 dll<->dll snooping functions
4 * Copyright 1998 Marcus Meissner
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "wine/winbase16.h"
28 #include "wine/library.h"
30 #include "stackframe.h"
31 #include "builtin16.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(snoop);
42 void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT86 *context);
43 void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT86 *context);
45 typedef struct tagSNOOP16_FUN {
47 BYTE lcall; /* 0x9a call absolute with segment */
55 typedef struct tagSNOOP16_DLL {
59 struct tagSNOOP16_DLL *next;
63 typedef struct tagSNOOP16_RETURNENTRY {
65 BYTE lcall; /* 0x9a call absolute with segment */
72 WORD *args; /* saved args across a stdcall */
73 } SNOOP16_RETURNENTRY;
75 typedef struct tagSNOOP16_RETURNENTRIES {
76 SNOOP16_RETURNENTRY entry[65500/sizeof(SNOOP16_RETURNENTRY)];
78 struct tagSNOOP16_RETURNENTRIES *next;
79 } SNOOP16_RETURNENTRIES;
81 typedef struct tagSNOOP16_RELAY {
82 WORD pushbp; /* 0x5566 */
83 BYTE pusheax; /* 0x50 */
84 WORD pushax; /* 0x5066 */
85 BYTE pushl; /* 0x68 */
86 DWORD realfun; /* SNOOP16_Return */
87 BYTE lcall; /* 0x9a call absolute with segment */
90 WORD lret; /* 0xcb66 */
95 static SNOOP16_DLL *firstdll = NULL;
96 static SNOOP16_RETURNENTRIES *firstrets = NULL;
97 static SNOOP16_RELAY *snr;
98 static HANDLE16 xsnr = 0;
101 SNOOP16_RegisterDLL(NE_MODULE *pModule,LPCSTR name) {
102 SNOOP16_DLL **dll = &(firstdll);
105 if (!TRACE_ON(snoop)) return;
107 TRACE("hmod=%x, name=%s\n", pModule->self, name);
110 xsnr=GLOBAL_Alloc(GMEM_ZEROINIT,2*sizeof(*snr),0,WINE_LDT_FLAGS_CODE|WINE_LDT_FLAGS_32BIT);
111 snr = GlobalLock16(xsnr);
112 snr[0].pushbp = 0x5566;
113 snr[0].pusheax = 0x50;
114 snr[0].pushax = 0x5066;
116 snr[0].realfun = (DWORD)SNOOP16_Entry;
118 snr[0].callfromregs = (DWORD)__wine_call_from_16_regs;
119 snr[0].seg = wine_get_cs();
120 snr[0].lret = 0xcb66;
122 snr[1].pushbp = 0x5566;
123 snr[1].pusheax = 0x50;
124 snr[1].pushax = 0x5066;
126 snr[1].realfun = (DWORD)SNOOP16_Return;
128 snr[1].callfromregs = (DWORD)__wine_call_from_16_regs;
129 snr[1].seg = wine_get_cs();
130 snr[1].lret = 0xcb66;
133 if ((*dll)->hmod == pModule->self)
135 /* another dll, loaded at the same address */
136 GlobalUnlock16((*dll)->funhandle);
137 GlobalFree16((*dll)->funhandle);
140 dll = &((*dll)->next);
142 *dll = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *dll, sizeof(SNOOP16_DLL)+strlen(name));
144 (*dll)->hmod = pModule->self;
145 if ((s=strrchr(name,'\\')))
147 strcpy( (*dll)->name, name );
148 if ((s=strrchr((*dll)->name,'.')))
150 (*dll)->funhandle = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,WINE_LDT_FLAGS_CODE));
151 (*dll)->funs = GlobalLock16((*dll)->funhandle);
153 HeapFree(GetProcessHeap(),0,*dll);
154 FIXME("out of memory\n");
160 SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
161 SNOOP16_DLL *dll = firstdll;
163 NE_MODULE *pModule = NE_GetPtr(hmod);
167 if (!TRACE_ON(snoop) || !pModule || !HIWORD(origfun))
169 if (!*(LPBYTE)MapSL((SEGPTR)origfun)) /* 0x00 is an imposs. opcode, poss. dataref. */
172 if (hmod == dll->hmod)
176 if (!dll) /* probably internal */
178 if (ordinal>65535/sizeof(SNOOP16_FUN))
180 fun = dll->funs+ordinal;
183 fun->snr = MAKELONG(0,xsnr);
184 fun->origfun = origfun;
186 return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
187 cpnt = (unsigned char *)pModule + pModule->name_table;
189 cpnt += *cpnt + 1 + sizeof(WORD);
190 if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
191 sprintf(name,"%.*s",*cpnt,cpnt+1);
195 /* Now search the non-resident names table */
197 if (!*cpnt && pModule->nrname_handle) {
198 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
200 cpnt += *cpnt + 1 + sizeof(WORD);
201 if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
202 sprintf(name,"%.*s",*cpnt,cpnt+1);
209 fun->name = HeapAlloc(GetProcessHeap(),0,strlen(name)+1);
210 strcpy( fun->name, name );
213 fun->name = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,1); /* empty string */
215 if (!SNOOP_ShowDebugmsgSnoop(dll->name, ordinal, fun->name))
218 /* more magic. do not try to snoop thunk data entries (MMSYSTEM) */
219 if (strchr(fun->name,'_')) {
220 char *s=strchr(fun->name,'_');
222 if (!strncasecmp(s,"_thunkdata",10)) {
223 HeapFree(GetProcessHeap(),0,fun->name);
229 fun->snr = MAKELONG(0,xsnr);
230 fun->origfun = origfun;
232 return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
235 #define CALLER1REF (*(DWORD*)(MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)+4))))
236 void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT86 *context) {
238 DWORD entry=(DWORD)MapSL( MAKESEGPTR(context->SegCs,LOWORD(context->Eip)) )-5;
239 WORD xcs = context->SegCs;
240 SNOOP16_DLL *dll = firstdll;
241 SNOOP16_FUN *fun = NULL;
242 SNOOP16_RETURNENTRIES **rets = &firstrets;
243 SNOOP16_RETURNENTRY *ret;
247 if (xcs == dll->funhandle) {
248 fun = (SNOOP16_FUN*)entry;
249 ordinal = fun-dll->funs;
255 FIXME("entrypoint 0x%08lx not found\n",entry);
259 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
260 if (!(*rets)->entry[i].origreturn)
262 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
264 rets = &((*rets)->next);
267 HANDLE16 hand = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,WINE_LDT_FLAGS_CODE));
268 *rets = GlobalLock16(hand);
269 (*rets)->rethandle = hand;
270 i = 0; /* entry 0 is free */
272 ret = &((*rets)->entry[i]);
274 ret->snr = MAKELONG(sizeof(SNOOP16_RELAY),xsnr);
275 ret->origreturn = (FARPROC16)CALLER1REF;
276 CALLER1REF = MAKELONG((char*)&(ret->lcall)-(char*)((*rets)->entry),(*rets)->rethandle);
279 ret->ordinal = ordinal;
280 ret->origSP = LOWORD(context->Esp);
282 context->Eip= LOWORD(fun->origfun);
283 context->SegCs = HIWORD(fun->origfun);
286 DPRINTF("CALL %s.%ld: %s(",dll->name,ordinal,fun->name);
287 if (fun->nrofargs>0) {
291 DPRINTF("%04x%s",*(WORD*)((char *) MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)) )+8+sizeof(WORD)*i),i?",":"");
292 if (max!=fun->nrofargs)
294 } else if (fun->nrofargs<0) {
295 DPRINTF("<unknown, check return>");
296 ret->args = HeapAlloc(GetProcessHeap(),0,16*sizeof(WORD));
297 memcpy(ret->args,(LPBYTE)((char *) MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)) )+8),sizeof(WORD)*16);
299 DPRINTF(") ret=%04x:%04x\n",HIWORD(ret->origreturn),LOWORD(ret->origreturn));
302 void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT86 *context) {
303 SNOOP16_RETURNENTRY *ret = (SNOOP16_RETURNENTRY*)((char *) MapSL( MAKESEGPTR(context->SegCs,LOWORD(context->Eip)) )-5);
305 /* We haven't found out the nrofargs yet. If we called a cdecl
306 * function it is too late anyway and we can just set '0' (which
307 * will be the difference between orig and current SP
308 * If pascal -> everything ok.
310 if (ret->dll->funs[ret->ordinal].nrofargs<0) {
311 ret->dll->funs[ret->ordinal].nrofargs=(LOWORD(context->Esp)-ret->origSP-4)/2;
313 context->Eip = LOWORD(ret->origreturn);
314 context->SegCs = HIWORD(ret->origreturn);
318 DPRINTF("RET %s.%ld: %s(",ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name);
319 max = ret->dll->funs[ret->ordinal].nrofargs;
326 DPRINTF("%04x%s",ret->args[i],i?",":"");
327 if (max!=ret->dll->funs[ret->ordinal].nrofargs)
329 DPRINTF(") retval = %04x:%04x ret=%04x:%04x\n",
330 DX_reg(context),AX_reg(context),HIWORD(ret->origreturn),LOWORD(ret->origreturn)
332 HeapFree(GetProcessHeap(),0,ret->args);
335 DPRINTF("RET %s.%ld: %s() retval = %04x:%04x ret=%04x:%04x\n",
336 ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name,
337 DX_reg(context),AX_reg(context),HIWORD(ret->origreturn),LOWORD(ret->origreturn)
339 ret->origreturn = NULL; /* mark as empty */
341 #else /* !__i386__ */
342 void SNOOP16_RegisterDLL(NE_MODULE *pModule,LPCSTR name) {
343 if (!TRACE_ON(snoop)) return;
344 FIXME("snooping works only on i386 for now.\n");
347 FARPROC16 SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
350 #endif /* !__i386__ */