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
28 #include "wine/winbase16.h"
30 #include "kernel16_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(snoop
);
37 static void WINAPI
SNOOP16_Entry(FARPROC proc
, LPBYTE args
, CONTEXT
*context
);
38 static void WINAPI
SNOOP16_Return(FARPROC proc
, LPBYTE args
, CONTEXT
*context
);
40 typedef struct tagSNOOP16_FUN
{
42 BYTE lcall
; /* 0x9a call absolute with segment */
50 typedef struct tagSNOOP16_DLL
{
54 struct tagSNOOP16_DLL
*next
;
58 typedef struct tagSNOOP16_RETURNENTRY
{
60 BYTE lcall
; /* 0x9a call absolute with segment */
67 WORD
*args
; /* saved args across a stdcall */
68 } SNOOP16_RETURNENTRY
;
70 typedef struct tagSNOOP16_RETURNENTRIES
{
71 SNOOP16_RETURNENTRY entry
[65500/sizeof(SNOOP16_RETURNENTRY
)];
73 struct tagSNOOP16_RETURNENTRIES
*next
;
74 } SNOOP16_RETURNENTRIES
;
76 typedef struct tagSNOOP16_RELAY
{
77 WORD pushbp
; /* 0x5566 */
78 BYTE pusheax
; /* 0x50 */
79 WORD pushax
; /* 0x5066 */
80 BYTE pushl
; /* 0x68 */
81 DWORD realfun
; /* SNOOP16_Return */
82 BYTE lcall
; /* 0x9a call absolute with segment */
85 WORD lret
; /* 0xcb66 */
90 static SNOOP16_DLL
*firstdll
= NULL
;
91 static SNOOP16_RETURNENTRIES
*firstrets
= NULL
;
92 static SNOOP16_RELAY
*snr
;
93 static HANDLE16 xsnr
= 0;
96 SNOOP16_RegisterDLL(HMODULE16 hModule
,LPCSTR name
) {
97 SNOOP16_DLL
**dll
= &(firstdll
);
101 if (!TRACE_ON(snoop
)) return;
103 TRACE("hmod=%x, name=%s\n", hModule
, name
);
106 xsnr
=GLOBAL_Alloc(GMEM_ZEROINIT
,2*sizeof(*snr
),0,LDT_FLAGS_CODE
| LDT_FLAGS_32BIT
);
107 snr
= GlobalLock16(xsnr
);
108 snr
[0].pushbp
= 0x5566;
109 snr
[0].pusheax
= 0x50;
110 snr
[0].pushax
= 0x5066;
112 snr
[0].realfun
= (DWORD
)SNOOP16_Entry
;
114 snr
[0].callfromregs
= (DWORD
)__wine_call_from_16_regs
;
115 snr
[0].seg
= get_cs();
116 snr
[0].lret
= 0xcb66;
118 snr
[1].pushbp
= 0x5566;
119 snr
[1].pusheax
= 0x50;
120 snr
[1].pushax
= 0x5066;
122 snr
[1].realfun
= (DWORD
)SNOOP16_Return
;
124 snr
[1].callfromregs
= (DWORD
)__wine_call_from_16_regs
;
125 snr
[1].seg
= get_cs();
126 snr
[1].lret
= 0xcb66;
129 if ((*dll
)->hmod
== hModule
)
131 /* another dll, loaded at the same address */
132 GlobalUnlock16((*dll
)->funhandle
);
133 GlobalFree16((*dll
)->funhandle
);
136 dll
= &((*dll
)->next
);
140 *dll
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, *dll
, sizeof(SNOOP16_DLL
)+strlen(name
));
142 *dll
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(SNOOP16_DLL
)+strlen(name
));
145 (*dll
)->hmod
= hModule
;
146 if ((p
=strrchr(name
,'\\')))
148 strcpy( (*dll
)->name
, name
);
149 if ((q
=strrchr((*dll
)->name
,'.')))
151 (*dll
)->funhandle
= GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT
,65535,0,LDT_FLAGS_CODE
));
152 (*dll
)->funs
= GlobalLock16((*dll
)->funhandle
);
154 HeapFree(GetProcessHeap(),0,*dll
);
155 FIXME("out of memory\n");
161 SNOOP16_GetProcAddress16(HMODULE16 hmod
,DWORD ordinal
,FARPROC16 origfun
) {
162 SNOOP16_DLL
*dll
= firstdll
;
164 NE_MODULE
*pModule
= NE_GetPtr(hmod
);
168 if (!TRACE_ON(snoop
) || !pModule
|| !HIWORD(origfun
))
170 if (!*(LPBYTE
)MapSL((SEGPTR
)origfun
)) /* 0x00 is an impossible opcode, possible dataref. */
173 if (hmod
== dll
->hmod
)
177 if (!dll
) /* probably internal */
179 if (ordinal
>65535/sizeof(SNOOP16_FUN
))
181 fun
= dll
->funs
+ordinal
;
184 fun
->snr
= MAKELONG(0,xsnr
);
185 fun
->origfun
= origfun
;
187 return (FARPROC16
)(SEGPTR
)MAKELONG(((char*)fun
-(char*)dll
->funs
),dll
->funhandle
);
188 cpnt
= (unsigned char *)pModule
+ pModule
->ne_restab
;
190 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
191 if (*(WORD
*)(cpnt
+*cpnt
+1) == ordinal
) {
192 sprintf(name
,"%.*s",*cpnt
,cpnt
+1);
196 /* Now search the non-resident names table */
198 if (!*cpnt
&& pModule
->nrname_handle
) {
199 cpnt
= GlobalLock16( pModule
->nrname_handle
);
201 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
202 if (*(WORD
*)(cpnt
+*cpnt
+1) == ordinal
) {
203 sprintf(name
,"%.*s",*cpnt
,cpnt
+1);
210 fun
->name
= HeapAlloc(GetProcessHeap(),0,strlen(name
)+1);
211 strcpy( fun
->name
, name
);
214 fun
->name
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,1); /* empty string */
216 if (!SNOOP16_ShowDebugmsgSnoop(dll
->name
, ordinal
, fun
->name
))
219 /* more magic. do not try to snoop thunk data entries (MMSYSTEM) */
220 if (strchr(fun
->name
,'_')) {
221 char *s
=strchr(fun
->name
,'_');
223 if (!_strnicmp(s
,"_thunkdata",10)) {
224 HeapFree(GetProcessHeap(),0,fun
->name
);
230 fun
->snr
= MAKELONG(0,xsnr
);
231 fun
->origfun
= origfun
;
233 return (FARPROC16
)(SEGPTR
)MAKELONG(((char*)fun
-(char*)dll
->funs
),dll
->funhandle
);
236 #define CALLER1REF (*(DWORD*)(MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)+4))))
237 static void WINAPI
SNOOP16_Entry(FARPROC proc
, LPBYTE args
, CONTEXT
*context
) {
239 DWORD entry
=(DWORD
)MapSL( MAKESEGPTR(context
->SegCs
,LOWORD(context
->Eip
)) )-5;
240 WORD xcs
= context
->SegCs
;
241 SNOOP16_DLL
*dll
= firstdll
;
242 SNOOP16_FUN
*fun
= NULL
;
243 SNOOP16_RETURNENTRIES
**rets
= &firstrets
;
244 SNOOP16_RETURNENTRY
*ret
;
249 if (xcs
== dll
->funhandle
) {
250 fun
= (SNOOP16_FUN
*)entry
;
251 ordinal
= fun
-dll
->funs
;
257 FIXME("entrypoint 0x%08x not found\n",entry
);
261 for (i
=0;i
<ARRAY_SIZE((*rets
)->entry
);i
++)
262 if (!(*rets
)->entry
[i
].origreturn
)
264 if (i
!=ARRAY_SIZE((*rets
)->entry
))
266 rets
= &((*rets
)->next
);
269 HANDLE16 hand
= GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT
,65535,0,LDT_FLAGS_CODE
));
270 *rets
= GlobalLock16(hand
);
271 (*rets
)->rethandle
= hand
;
272 i
= 0; /* entry 0 is free */
274 ret
= &((*rets
)->entry
[i
]);
276 ret
->snr
= MAKELONG(sizeof(SNOOP16_RELAY
),xsnr
);
277 ret
->origreturn
= (FARPROC16
)CALLER1REF
;
278 CALLER1REF
= MAKELONG((char*)&(ret
->lcall
)-(char*)((*rets
)->entry
),(*rets
)->rethandle
);
281 ret
->ordinal
= ordinal
;
282 ret
->origSP
= LOWORD(context
->Esp
);
284 context
->Eip
= LOWORD(fun
->origfun
);
285 context
->SegCs
= HIWORD(fun
->origfun
);
288 TRACE("\1CALL %s.%d: %s(", dll
->name
, ordinal
, fun
->name
);
289 if (fun
->nrofargs
>0) {
293 TRACE("%04x%s",*(WORD
*)((char *) MapSL( MAKESEGPTR(context
->SegSs
,LOWORD(context
->Esp
)) )+8+sizeof(WORD
)*i
),i
?",":"");
294 if (max
!=fun
->nrofargs
)
296 } else if (fun
->nrofargs
<0) {
297 TRACE("<unknown, check return>");
298 ret
->args
= HeapAlloc(GetProcessHeap(),0,16*sizeof(WORD
));
299 memcpy(ret
->args
,(LPBYTE
)((char *) MapSL( MAKESEGPTR(context
->SegSs
,LOWORD(context
->Esp
)) )+8),sizeof(WORD
)*16);
301 TRACE(") ret=%04x:%04x\n",HIWORD(ret
->origreturn
),LOWORD(ret
->origreturn
));
304 static void WINAPI
SNOOP16_Return(FARPROC proc
, LPBYTE args
, CONTEXT
*context
) {
305 SNOOP16_RETURNENTRY
*ret
= (SNOOP16_RETURNENTRY
*)((char *) MapSL( MAKESEGPTR(context
->SegCs
,LOWORD(context
->Eip
)) )-5);
307 /* We haven't found out the nrofargs yet. If we called a cdecl
308 * function it is too late anyway and we can just set '0' (which
309 * will be the difference between orig and current SP
310 * If pascal -> everything ok.
312 if (ret
->dll
->funs
[ret
->ordinal
].nrofargs
<0) {
313 ret
->dll
->funs
[ret
->ordinal
].nrofargs
=(LOWORD(context
->Esp
)-ret
->origSP
-4)/2;
315 context
->Eip
= LOWORD(ret
->origreturn
);
316 context
->SegCs
= HIWORD(ret
->origreturn
);
317 TRACE("\1RET %s.%d: %s(", ret
->dll
->name
, ret
->ordinal
, ret
->dll
->funs
[ret
->ordinal
].name
);
321 max
= ret
->dll
->funs
[ret
->ordinal
].nrofargs
;
328 TRACE("%04x%s",ret
->args
[i
],i
?",":"");
329 if (max
!=ret
->dll
->funs
[ret
->ordinal
].nrofargs
)
331 HeapFree(GetProcessHeap(),0,ret
->args
);
334 TRACE(") retval = %04x:%04x ret=%04x:%04x\n",
335 (WORD
)context
->Edx
,(WORD
)context
->Eax
,
336 HIWORD(ret
->origreturn
),LOWORD(ret
->origreturn
));
337 ret
->origreturn
= NULL
; /* mark as empty */