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
22 #include "wine/port.h"
31 #include "wine/winbase16.h"
32 #include "wine/library.h"
35 #include "stackframe.h"
36 #include "builtin16.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(snoop);
46 extern int SNOOP_ShowDebugmsgSnoop(const char *dll,int ord,const char *fname); /* FIXME */
48 void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT86 *context);
49 void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT86 *context);
51 typedef struct tagSNOOP16_FUN {
53 BYTE lcall; /* 0x9a call absolute with segment */
61 typedef struct tagSNOOP16_DLL {
65 struct tagSNOOP16_DLL *next;
69 typedef struct tagSNOOP16_RETURNENTRY {
71 BYTE lcall; /* 0x9a call absolute with segment */
78 WORD *args; /* saved args across a stdcall */
79 } SNOOP16_RETURNENTRY;
81 typedef struct tagSNOOP16_RETURNENTRIES {
82 SNOOP16_RETURNENTRY entry[65500/sizeof(SNOOP16_RETURNENTRY)];
84 struct tagSNOOP16_RETURNENTRIES *next;
85 } SNOOP16_RETURNENTRIES;
87 typedef struct tagSNOOP16_RELAY {
88 WORD pushbp; /* 0x5566 */
89 BYTE pusheax; /* 0x50 */
90 WORD pushax; /* 0x5066 */
91 BYTE pushl; /* 0x68 */
92 DWORD realfun; /* SNOOP16_Return */
93 BYTE lcall; /* 0x9a call absolute with segment */
96 WORD lret; /* 0xcb66 */
101 static SNOOP16_DLL *firstdll = NULL;
102 static SNOOP16_RETURNENTRIES *firstrets = NULL;
103 static SNOOP16_RELAY *snr;
104 static HANDLE16 xsnr = 0;
107 SNOOP16_RegisterDLL(NE_MODULE *pModule,LPCSTR name) {
108 SNOOP16_DLL **dll = &(firstdll);
111 if (!TRACE_ON(snoop)) return;
113 TRACE("hmod=%x, name=%s\n", pModule->self, name);
116 xsnr=GLOBAL_Alloc(GMEM_ZEROINIT,2*sizeof(*snr),0,WINE_LDT_FLAGS_CODE|WINE_LDT_FLAGS_32BIT);
117 snr = GlobalLock16(xsnr);
118 snr[0].pushbp = 0x5566;
119 snr[0].pusheax = 0x50;
120 snr[0].pushax = 0x5066;
122 snr[0].realfun = (DWORD)SNOOP16_Entry;
124 snr[0].callfromregs = (DWORD)__wine_call_from_16_regs;
125 snr[0].seg = wine_get_cs();
126 snr[0].lret = 0xcb66;
128 snr[1].pushbp = 0x5566;
129 snr[1].pusheax = 0x50;
130 snr[1].pushax = 0x5066;
132 snr[1].realfun = (DWORD)SNOOP16_Return;
134 snr[1].callfromregs = (DWORD)__wine_call_from_16_regs;
135 snr[1].seg = wine_get_cs();
136 snr[1].lret = 0xcb66;
139 if ((*dll)->hmod == pModule->self)
141 /* another dll, loaded at the same address */
142 GlobalUnlock16((*dll)->funhandle);
143 GlobalFree16((*dll)->funhandle);
146 dll = &((*dll)->next);
148 *dll = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *dll, sizeof(SNOOP16_DLL)+strlen(name));
150 (*dll)->hmod = pModule->self;
151 if ((s=strrchr(name,'\\')))
153 strcpy( (*dll)->name, name );
154 if ((s=strrchr((*dll)->name,'.')))
156 (*dll)->funhandle = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,WINE_LDT_FLAGS_CODE));
157 (*dll)->funs = GlobalLock16((*dll)->funhandle);
159 HeapFree(GetProcessHeap(),0,*dll);
160 FIXME("out of memory\n");
166 SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
167 SNOOP16_DLL *dll = firstdll;
169 NE_MODULE *pModule = NE_GetPtr(hmod);
173 if (!TRACE_ON(snoop) || !pModule || !HIWORD(origfun))
175 if (!*(LPBYTE)MapSL((SEGPTR)origfun)) /* 0x00 is an imposs. opcode, poss. dataref. */
178 if (hmod == dll->hmod)
182 if (!dll) /* probably internal */
184 if (ordinal>65535/sizeof(SNOOP16_FUN))
186 fun = dll->funs+ordinal;
189 fun->snr = MAKELONG(0,xsnr);
190 fun->origfun = origfun;
192 return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
193 cpnt = (unsigned char *)pModule + pModule->name_table;
195 cpnt += *cpnt + 1 + sizeof(WORD);
196 if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
197 sprintf(name,"%.*s",*cpnt,cpnt+1);
201 /* Now search the non-resident names table */
203 if (!*cpnt && pModule->nrname_handle) {
204 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
206 cpnt += *cpnt + 1 + sizeof(WORD);
207 if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
208 sprintf(name,"%.*s",*cpnt,cpnt+1);
215 fun->name = HeapAlloc(GetProcessHeap(),0,strlen(name)+1);
216 strcpy( fun->name, name );
219 fun->name = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,1); /* empty string */
221 if (!SNOOP_ShowDebugmsgSnoop(dll->name, ordinal, fun->name))
224 /* more magic. do not try to snoop thunk data entries (MMSYSTEM) */
225 if (strchr(fun->name,'_')) {
226 char *s=strchr(fun->name,'_');
228 if (!strncasecmp(s,"_thunkdata",10)) {
229 HeapFree(GetProcessHeap(),0,fun->name);
235 fun->snr = MAKELONG(0,xsnr);
236 fun->origfun = origfun;
238 return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
241 #define CALLER1REF (*(DWORD*)(MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)+4))))
242 void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT86 *context) {
244 DWORD entry=(DWORD)MapSL( MAKESEGPTR(context->SegCs,LOWORD(context->Eip)) )-5;
245 WORD xcs = context->SegCs;
246 SNOOP16_DLL *dll = firstdll;
247 SNOOP16_FUN *fun = NULL;
248 SNOOP16_RETURNENTRIES **rets = &firstrets;
249 SNOOP16_RETURNENTRY *ret;
253 if (xcs == dll->funhandle) {
254 fun = (SNOOP16_FUN*)entry;
255 ordinal = fun-dll->funs;
261 FIXME("entrypoint 0x%08lx not found\n",entry);
265 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
266 if (!(*rets)->entry[i].origreturn)
268 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
270 rets = &((*rets)->next);
273 HANDLE16 hand = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,WINE_LDT_FLAGS_CODE));
274 *rets = GlobalLock16(hand);
275 (*rets)->rethandle = hand;
276 i = 0; /* entry 0 is free */
278 ret = &((*rets)->entry[i]);
280 ret->snr = MAKELONG(sizeof(SNOOP16_RELAY),xsnr);
281 ret->origreturn = (FARPROC16)CALLER1REF;
282 CALLER1REF = MAKELONG((char*)&(ret->lcall)-(char*)((*rets)->entry),(*rets)->rethandle);
285 ret->ordinal = ordinal;
286 ret->origSP = LOWORD(context->Esp);
288 context->Eip= LOWORD(fun->origfun);
289 context->SegCs = HIWORD(fun->origfun);
292 DPRINTF("CALL %s.%ld: %s(",dll->name,ordinal,fun->name);
293 if (fun->nrofargs>0) {
297 DPRINTF("%04x%s",*(WORD*)((char *) MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)) )+8+sizeof(WORD)*i),i?",":"");
298 if (max!=fun->nrofargs)
300 } else if (fun->nrofargs<0) {
301 DPRINTF("<unknown, check return>");
302 ret->args = HeapAlloc(GetProcessHeap(),0,16*sizeof(WORD));
303 memcpy(ret->args,(LPBYTE)((char *) MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)) )+8),sizeof(WORD)*16);
305 DPRINTF(") ret=%04x:%04x\n",HIWORD(ret->origreturn),LOWORD(ret->origreturn));
308 void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT86 *context) {
309 SNOOP16_RETURNENTRY *ret = (SNOOP16_RETURNENTRY*)((char *) MapSL( MAKESEGPTR(context->SegCs,LOWORD(context->Eip)) )-5);
311 /* We haven't found out the nrofargs yet. If we called a cdecl
312 * function it is too late anyway and we can just set '0' (which
313 * will be the difference between orig and current SP
314 * If pascal -> everything ok.
316 if (ret->dll->funs[ret->ordinal].nrofargs<0) {
317 ret->dll->funs[ret->ordinal].nrofargs=(LOWORD(context->Esp)-ret->origSP-4)/2;
319 context->Eip = LOWORD(ret->origreturn);
320 context->SegCs = HIWORD(ret->origreturn);
324 DPRINTF("RET %s.%ld: %s(",ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name);
325 max = ret->dll->funs[ret->ordinal].nrofargs;
332 DPRINTF("%04x%s",ret->args[i],i?",":"");
333 if (max!=ret->dll->funs[ret->ordinal].nrofargs)
335 DPRINTF(") retval = %04x:%04x ret=%04x:%04x\n",
336 (WORD)context->Edx,(WORD)context->Eax,
337 HIWORD(ret->origreturn),LOWORD(ret->origreturn)
339 HeapFree(GetProcessHeap(),0,ret->args);
342 DPRINTF("RET %s.%ld: %s() retval = %04x:%04x ret=%04x:%04x\n",
343 ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name,
344 (WORD)context->Edx,(WORD)context->Eax,
345 HIWORD(ret->origreturn),LOWORD(ret->origreturn)
347 ret->origreturn = NULL; /* mark as empty */
349 #else /* !__i386__ */
350 void SNOOP16_RegisterDLL(NE_MODULE *pModule,LPCSTR name) {
351 if (!TRACE_ON(snoop)) return;
352 FIXME("snooping works only on i386 for now.\n");
355 FARPROC16 SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
358 #endif /* !__i386__ */