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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
31 #include "wine/winbase16.h"
33 #include "wine/library.h"
34 #include "kernel_private.h"
35 #include "kernel16_private.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(snoop
);
44 static void WINAPI
SNOOP16_Entry(FARPROC proc
, LPBYTE args
, CONTEXT86
*context
);
45 static void WINAPI
SNOOP16_Return(FARPROC proc
, LPBYTE args
, CONTEXT86
*context
);
47 typedef struct tagSNOOP16_FUN
{
49 BYTE lcall
; /* 0x9a call absolute with segment */
57 typedef struct tagSNOOP16_DLL
{
61 struct tagSNOOP16_DLL
*next
;
65 typedef struct tagSNOOP16_RETURNENTRY
{
67 BYTE lcall
; /* 0x9a call absolute with segment */
74 WORD
*args
; /* saved args across a stdcall */
75 } SNOOP16_RETURNENTRY
;
77 typedef struct tagSNOOP16_RETURNENTRIES
{
78 SNOOP16_RETURNENTRY entry
[65500/sizeof(SNOOP16_RETURNENTRY
)];
80 struct tagSNOOP16_RETURNENTRIES
*next
;
81 } SNOOP16_RETURNENTRIES
;
83 typedef struct tagSNOOP16_RELAY
{
84 WORD pushbp
; /* 0x5566 */
85 BYTE pusheax
; /* 0x50 */
86 WORD pushax
; /* 0x5066 */
87 BYTE pushl
; /* 0x68 */
88 DWORD realfun
; /* SNOOP16_Return */
89 BYTE lcall
; /* 0x9a call absolute with segment */
92 WORD lret
; /* 0xcb66 */
97 static SNOOP16_DLL
*firstdll
= NULL
;
98 static SNOOP16_RETURNENTRIES
*firstrets
= NULL
;
99 static SNOOP16_RELAY
*snr
;
100 static HANDLE16 xsnr
= 0;
103 SNOOP16_RegisterDLL(HMODULE16 hModule
,LPCSTR name
) {
104 SNOOP16_DLL
**dll
= &(firstdll
);
107 if (!TRACE_ON(snoop
)) return;
109 TRACE("hmod=%x, name=%s\n", hModule
, name
);
112 xsnr
=GLOBAL_Alloc(GMEM_ZEROINIT
,2*sizeof(*snr
),0,WINE_LDT_FLAGS_CODE
|WINE_LDT_FLAGS_32BIT
);
113 snr
= GlobalLock16(xsnr
);
114 snr
[0].pushbp
= 0x5566;
115 snr
[0].pusheax
= 0x50;
116 snr
[0].pushax
= 0x5066;
118 snr
[0].realfun
= (DWORD
)SNOOP16_Entry
;
120 snr
[0].callfromregs
= (DWORD
)__wine_call_from_16_regs
;
121 snr
[0].seg
= wine_get_cs();
122 snr
[0].lret
= 0xcb66;
124 snr
[1].pushbp
= 0x5566;
125 snr
[1].pusheax
= 0x50;
126 snr
[1].pushax
= 0x5066;
128 snr
[1].realfun
= (DWORD
)SNOOP16_Return
;
130 snr
[1].callfromregs
= (DWORD
)__wine_call_from_16_regs
;
131 snr
[1].seg
= wine_get_cs();
132 snr
[1].lret
= 0xcb66;
135 if ((*dll
)->hmod
== hModule
)
137 /* another dll, loaded at the same address */
138 GlobalUnlock16((*dll
)->funhandle
);
139 GlobalFree16((*dll
)->funhandle
);
142 dll
= &((*dll
)->next
);
146 *dll
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, *dll
, sizeof(SNOOP16_DLL
)+strlen(name
));
148 *dll
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(SNOOP16_DLL
)+strlen(name
));
151 (*dll
)->hmod
= hModule
;
152 if ((s
=strrchr(name
,'\\')))
154 strcpy( (*dll
)->name
, name
);
155 if ((s
=strrchr((*dll
)->name
,'.')))
157 (*dll
)->funhandle
= GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT
,65535,0,WINE_LDT_FLAGS_CODE
));
158 (*dll
)->funs
= GlobalLock16((*dll
)->funhandle
);
160 HeapFree(GetProcessHeap(),0,*dll
);
161 FIXME("out of memory\n");
167 SNOOP16_GetProcAddress16(HMODULE16 hmod
,DWORD ordinal
,FARPROC16 origfun
) {
168 SNOOP16_DLL
*dll
= firstdll
;
170 NE_MODULE
*pModule
= NE_GetPtr(hmod
);
174 if (!TRACE_ON(snoop
) || !pModule
|| !HIWORD(origfun
))
176 if (!*(LPBYTE
)MapSL((SEGPTR
)origfun
)) /* 0x00 is an impossible opcode, possible dataref. */
179 if (hmod
== dll
->hmod
)
183 if (!dll
) /* probably internal */
185 if (ordinal
>65535/sizeof(SNOOP16_FUN
))
187 fun
= dll
->funs
+ordinal
;
190 fun
->snr
= MAKELONG(0,xsnr
);
191 fun
->origfun
= origfun
;
193 return (FARPROC16
)(SEGPTR
)MAKELONG(((char*)fun
-(char*)dll
->funs
),dll
->funhandle
);
194 cpnt
= (unsigned char *)pModule
+ pModule
->ne_restab
;
196 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
197 if (*(WORD
*)(cpnt
+*cpnt
+1) == ordinal
) {
198 sprintf(name
,"%.*s",*cpnt
,cpnt
+1);
202 /* Now search the non-resident names table */
204 if (!*cpnt
&& pModule
->nrname_handle
) {
205 cpnt
= GlobalLock16( pModule
->nrname_handle
);
207 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
208 if (*(WORD
*)(cpnt
+*cpnt
+1) == ordinal
) {
209 sprintf(name
,"%.*s",*cpnt
,cpnt
+1);
216 fun
->name
= HeapAlloc(GetProcessHeap(),0,strlen(name
)+1);
217 strcpy( fun
->name
, name
);
220 fun
->name
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,1); /* empty string */
222 if (!SNOOP16_ShowDebugmsgSnoop(dll
->name
, ordinal
, fun
->name
))
225 /* more magic. do not try to snoop thunk data entries (MMSYSTEM) */
226 if (strchr(fun
->name
,'_')) {
227 char *s
=strchr(fun
->name
,'_');
229 if (!strncasecmp(s
,"_thunkdata",10)) {
230 HeapFree(GetProcessHeap(),0,fun
->name
);
236 fun
->snr
= MAKELONG(0,xsnr
);
237 fun
->origfun
= origfun
;
239 return (FARPROC16
)(SEGPTR
)MAKELONG(((char*)fun
-(char*)dll
->funs
),dll
->funhandle
);
242 #define CALLER1REF (*(DWORD*)(MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)+4))))
243 static void WINAPI
SNOOP16_Entry(FARPROC proc
, LPBYTE args
, CONTEXT86
*context
) {
245 DWORD entry
=(DWORD
)MapSL( MAKESEGPTR(context
->SegCs
,LOWORD(context
->Eip
)) )-5;
246 WORD xcs
= context
->SegCs
;
247 SNOOP16_DLL
*dll
= firstdll
;
248 SNOOP16_FUN
*fun
= NULL
;
249 SNOOP16_RETURNENTRIES
**rets
= &firstrets
;
250 SNOOP16_RETURNENTRY
*ret
;
255 if (xcs
== dll
->funhandle
) {
256 fun
= (SNOOP16_FUN
*)entry
;
257 ordinal
= fun
-dll
->funs
;
263 FIXME("entrypoint 0x%08x not found\n",entry
);
267 for (i
=0;i
<sizeof((*rets
)->entry
)/sizeof((*rets
)->entry
[0]);i
++)
268 if (!(*rets
)->entry
[i
].origreturn
)
270 if (i
!=sizeof((*rets
)->entry
)/sizeof((*rets
)->entry
[0]))
272 rets
= &((*rets
)->next
);
275 HANDLE16 hand
= GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT
,65535,0,WINE_LDT_FLAGS_CODE
));
276 *rets
= GlobalLock16(hand
);
277 (*rets
)->rethandle
= hand
;
278 i
= 0; /* entry 0 is free */
280 ret
= &((*rets
)->entry
[i
]);
282 ret
->snr
= MAKELONG(sizeof(SNOOP16_RELAY
),xsnr
);
283 ret
->origreturn
= (FARPROC16
)CALLER1REF
;
284 CALLER1REF
= MAKELONG((char*)&(ret
->lcall
)-(char*)((*rets
)->entry
),(*rets
)->rethandle
);
287 ret
->ordinal
= ordinal
;
288 ret
->origSP
= LOWORD(context
->Esp
);
290 context
->Eip
= LOWORD(fun
->origfun
);
291 context
->SegCs
= HIWORD(fun
->origfun
);
294 DPRINTF("%04x:CALL %s.%d: %s(",GetCurrentThreadId(), dll
->name
,ordinal
,fun
->name
);
295 if (fun
->nrofargs
>0) {
299 DPRINTF("%04x%s",*(WORD
*)((char *) MapSL( MAKESEGPTR(context
->SegSs
,LOWORD(context
->Esp
)) )+8+sizeof(WORD
)*i
),i
?",":"");
300 if (max
!=fun
->nrofargs
)
302 } else if (fun
->nrofargs
<0) {
303 DPRINTF("<unknown, check return>");
304 ret
->args
= HeapAlloc(GetProcessHeap(),0,16*sizeof(WORD
));
305 memcpy(ret
->args
,(LPBYTE
)((char *) MapSL( MAKESEGPTR(context
->SegSs
,LOWORD(context
->Esp
)) )+8),sizeof(WORD
)*16);
307 DPRINTF(") ret=%04x:%04x\n",HIWORD(ret
->origreturn
),LOWORD(ret
->origreturn
));
310 static void WINAPI
SNOOP16_Return(FARPROC proc
, LPBYTE args
, CONTEXT86
*context
) {
311 SNOOP16_RETURNENTRY
*ret
= (SNOOP16_RETURNENTRY
*)((char *) MapSL( MAKESEGPTR(context
->SegCs
,LOWORD(context
->Eip
)) )-5);
313 /* We haven't found out the nrofargs yet. If we called a cdecl
314 * function it is too late anyway and we can just set '0' (which
315 * will be the difference between orig and current SP
316 * If pascal -> everything ok.
318 if (ret
->dll
->funs
[ret
->ordinal
].nrofargs
<0) {
319 ret
->dll
->funs
[ret
->ordinal
].nrofargs
=(LOWORD(context
->Esp
)-ret
->origSP
-4)/2;
321 context
->Eip
= LOWORD(ret
->origreturn
);
322 context
->SegCs
= HIWORD(ret
->origreturn
);
323 DPRINTF("%04x:RET %s.%d: %s(",
324 GetCurrentThreadId(),ret
->dll
->name
,ret
->ordinal
,
325 ret
->dll
->funs
[ret
->ordinal
].name
);
329 max
= ret
->dll
->funs
[ret
->ordinal
].nrofargs
;
336 DPRINTF("%04x%s",ret
->args
[i
],i
?",":"");
337 if (max
!=ret
->dll
->funs
[ret
->ordinal
].nrofargs
)
339 HeapFree(GetProcessHeap(),0,ret
->args
);
342 DPRINTF(") retval = %04x:%04x ret=%04x:%04x\n",
343 (WORD
)context
->Edx
,(WORD
)context
->Eax
,
344 HIWORD(ret
->origreturn
),LOWORD(ret
->origreturn
));
345 ret
->origreturn
= NULL
; /* mark as empty */
347 #else /* !__i386__ */
348 void SNOOP16_RegisterDLL(HMODULE16 hModule
,LPCSTR name
) {
349 if (!TRACE_ON(snoop
)) return;
350 FIXME("snooping works only on i386 for now.\n");
353 FARPROC16
SNOOP16_GetProcAddress16(HMODULE16 hmod
,DWORD ordinal
,FARPROC16 origfun
) {
356 #endif /* !__i386__ */