2 * 386-specific Win16 dll<->dll snooping functions
4 * Copyright 1998 Marcus Meissner
13 #include "selectors.h"
14 #include "stackframe.h"
19 DEFAULT_DEBUG_CHANNEL(snoop
)
25 void WINAPI
SNOOP16_Entry(CONTEXT
*context
);
26 void WINAPI
SNOOP16_Return(CONTEXT
*context
);
27 extern void CallFrom16_p_regs_();
29 /* Generic callfrom16_p_regs function entry.
31 * pushl $DOS3Call DWORD fun32
33 * .long CallFrom16_p_regs_ DWORD addr
34 * .long 0x90900023 WORD seg;nop;nop
37 typedef struct tagSNOOP16_FUN
{
39 BYTE lcall
; /* 0x9a call absolute with segment */
47 typedef struct tagSNOOP16_DLL
{
52 struct tagSNOOP16_DLL
*next
;
55 typedef struct tagSNOOP16_RETURNENTRY
{
57 BYTE lcall
; /* 0x9a call absolute with segment */
64 WORD
*args
; /* saved args across a stdcall */
65 } SNOOP16_RETURNENTRY
;
67 typedef struct tagSNOOP16_RETURNENTRIES
{
68 SNOOP16_RETURNENTRY entry
[65500/sizeof(SNOOP16_RETURNENTRY
)];
70 struct tagSNOOP16_RETURNENTRIES
*next
;
71 } SNOOP16_RETURNENTRIES
;
73 typedef struct tagSNOOP16_RELAY
{
75 BYTE prefix
; /* 0x66 , 32bit prefix */
76 BYTE pushbp
; /* 0x55 */
77 BYTE pushl
; /* 0x68 */
78 DWORD realfun
; /* SNOOP16_Return */
79 BYTE lcall
; /* 0x9a call absolute with segment */
87 static SNOOP16_DLL
*firstdll
= NULL
;
88 static SNOOP16_RETURNENTRIES
*firstrets
= NULL
;
89 static SNOOP16_RELAY
*snr
;
90 static HANDLE16 xsnr
= 0;
93 SNOOP16_RegisterDLL(NE_MODULE
*pModule
,LPCSTR name
) {
94 SNOOP16_DLL
**dll
= &(firstdll
);
97 if (!TRACE_ON(snoop
)) return;
99 xsnr
=GLOBAL_Alloc(GMEM_ZEROINIT
,2*sizeof(*snr
),0,TRUE
,TRUE
,FALSE
);
100 snr
= GlobalLock16(xsnr
);
101 snr
[0].prefix
= 0x66;
102 snr
[0].pushbp
= 0x55;
104 snr
[0].realfun
= (DWORD
)SNOOP16_Entry
;
106 snr
[0].callfromregs
= (DWORD
)CallFrom16_p_regs_
;
108 snr
[1].prefix
= 0x66;
109 snr
[1].pushbp
= 0x55;
111 snr
[1].realfun
= (DWORD
)SNOOP16_Return
;
113 snr
[1].callfromregs
= (DWORD
)CallFrom16_p_regs_
;
117 if ((*dll
)->hmod
== pModule
->self
)
118 return; /* already registered */
119 dll
= &((*dll
)->next
);
121 *dll
= (SNOOP16_DLL
*)HeapAlloc(SystemHeap
,HEAP_ZERO_MEMORY
,sizeof(SNOOP16_DLL
));
123 (*dll
)->hmod
= pModule
->self
;
124 if ((s
=strrchr(name
,'\\')))
126 (*dll
)->name
= HEAP_strdupA(SystemHeap
,0,name
);
127 if ((s
=strrchr((*dll
)->name
,'.')))
129 (*dll
)->funhandle
= GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT
,65535,0,TRUE
,FALSE
,FALSE
));
130 (*dll
)->funs
= GlobalLock16((*dll
)->funhandle
);
132 HeapFree(SystemHeap
,0,*dll
);
133 FIXME(snoop
,"out of memory\n");
136 memset((*dll
)->funs
,0,65535);
140 SNOOP16_GetProcAddress16(HMODULE16 hmod
,DWORD ordinal
,FARPROC16 origfun
) {
141 SNOOP16_DLL
*dll
= firstdll
;
143 NE_MODULE
*pModule
= NE_GetPtr(hmod
);
147 if (!TRACE_ON(snoop
) || !pModule
|| !HIWORD(origfun
))
149 if (!*(LPBYTE
)PTR_SEG_TO_LIN(origfun
)) /* 0x00 is an imposs. opcode, poss. dataref. */
152 if (hmod
== dll
->hmod
)
156 if (!dll
) /* probably internal */
158 if (ordinal
>65535/sizeof(SNOOP16_FUN
))
160 fun
= dll
->funs
+ordinal
;
163 fun
->snr
= MAKELONG(0,xsnr
);
164 fun
->origfun
= origfun
;
166 return (FARPROC16
)(SEGPTR
)MAKELONG(((char*)fun
-(char*)dll
->funs
),dll
->funhandle
);
167 cpnt
= (unsigned char *)pModule
+ pModule
->name_table
;
169 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
170 if (*(WORD
*)(cpnt
+*cpnt
+1) == ordinal
) {
171 sprintf(name
,"%.*s",*cpnt
,cpnt
+1);
175 /* Now search the non-resident names table */
177 if (!*cpnt
&& pModule
->nrname_handle
) {
178 cpnt
= (char *)GlobalLock16( pModule
->nrname_handle
);
180 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
181 if (*(WORD
*)(cpnt
+*cpnt
+1) == ordinal
) {
182 sprintf(name
,"%.*s",*cpnt
,cpnt
+1);
188 fun
->name
= HEAP_strdupA(SystemHeap
,0,name
);
190 fun
->name
= HEAP_strdupA(SystemHeap
,0,"");
191 if (!SNOOP_ShowDebugmsgSnoop(dll
->name
, ordinal
, fun
->name
))
194 /* more magic. do not try to snoop thunk data entries (MMSYSTEM) */
195 if (strchr(fun
->name
,'_')) {
196 char *s
=strchr(fun
->name
,'_');
198 if (!strncasecmp(s
,"_thunkdata",10)) {
199 HeapFree(SystemHeap
,0,fun
->name
);
205 fun
->snr
= MAKELONG(0,xsnr
);
206 fun
->origfun
= origfun
;
208 return (FARPROC16
)(SEGPTR
)MAKELONG(((char*)fun
-(char*)dll
->funs
),dll
->funhandle
);
211 #define CALLER1REF (*(DWORD*)(PTR_SEG_OFF_TO_LIN(SS_reg(context),SP_reg(context)+4)))
212 void WINAPI
SNOOP16_Entry(CONTEXT
*context
) {
214 DWORD entry
=(DWORD
)PTR_SEG_OFF_TO_LIN(CS_reg(context
),IP_reg(context
))-5;
215 WORD xcs
= CS_reg(context
);
216 SNOOP16_DLL
*dll
= firstdll
;
217 SNOOP16_FUN
*fun
= NULL
;
218 SNOOP16_RETURNENTRIES
**rets
= &firstrets
;
219 SNOOP16_RETURNENTRY
*ret
;
223 if (xcs
== dll
->funhandle
) {
224 fun
= (SNOOP16_FUN
*)entry
;
225 ordinal
= fun
-dll
->funs
;
231 FIXME(snoop
,"entrypoint 0x%08lx not found\n",entry
);
235 for (i
=0;i
<sizeof((*rets
)->entry
)/sizeof((*rets
)->entry
[0]);i
++)
236 if (!(*rets
)->entry
[i
].origreturn
)
238 if (i
!=sizeof((*rets
)->entry
)/sizeof((*rets
)->entry
[0]))
240 rets
= &((*rets
)->next
);
243 HANDLE16 hand
= GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT
,65535,0,TRUE
,FALSE
,FALSE
));
244 *rets
= GlobalLock16(hand
);
245 memset(*rets
,0,65535);
246 (*rets
)->rethandle
= hand
;
247 i
= 0; /* entry 0 is free */
249 ret
= &((*rets
)->entry
[i
]);
251 ret
->snr
= MAKELONG(sizeof(SNOOP16_RELAY
),xsnr
);
252 ret
->origreturn
= (FARPROC16
)CALLER1REF
;
253 CALLER1REF
= MAKELONG((char*)&(ret
->lcall
)-(char*)((*rets
)->entry
),(*rets
)->rethandle
);
256 ret
->ordinal
= ordinal
;
257 ret
->origSP
= SP_reg(context
);
259 IP_reg(context
)= LOWORD(fun
->origfun
);
260 CS_reg(context
)= HIWORD(fun
->origfun
);
263 DPRINTF("Call %s.%ld: %s(",dll
->name
,ordinal
,fun
->name
);
264 if (fun
->nrofargs
>0) {
268 DPRINTF("%04x%s",*(WORD
*)(PTR_SEG_OFF_TO_LIN(SS_reg(context
),SP_reg(context
))+8+sizeof(WORD
)*i
),i
?",":"");
269 if (max
!=fun
->nrofargs
)
271 } else if (fun
->nrofargs
<0) {
272 DPRINTF("<unknown, check return>");
273 ret
->args
= HeapAlloc(SystemHeap
,0,16*sizeof(WORD
));
274 memcpy(ret
->args
,(LPBYTE
)(PTR_SEG_OFF_TO_LIN(SS_reg(context
),SP_reg(context
))+8),sizeof(WORD
)*16);
276 DPRINTF(") ret=%04x:%04x\n",HIWORD(ret
->origreturn
),LOWORD(ret
->origreturn
));
279 void WINAPI
SNOOP16_Return(CONTEXT
*context
) {
280 SNOOP16_RETURNENTRY
*ret
= (SNOOP16_RETURNENTRY
*)(PTR_SEG_OFF_TO_LIN(CS_reg(context
),IP_reg(context
))-5);
282 /* We haven't found out the nrofargs yet. If we called a cdecl
283 * function it is too late anyway and we can just set '0' (which
284 * will be the difference between orig and current SP
285 * If pascal -> everything ok.
287 if (ret
->dll
->funs
[ret
->ordinal
].nrofargs
<0) {
288 ret
->dll
->funs
[ret
->ordinal
].nrofargs
=(SP_reg(context
)-ret
->origSP
-4)/2;
290 IP_reg(context
) = LOWORD(ret
->origreturn
);
291 CS_reg(context
) = HIWORD(ret
->origreturn
);
295 DPRINTF("Ret %s.%ld: %s(",ret
->dll
->name
,ret
->ordinal
,ret
->dll
->funs
[ret
->ordinal
].name
);
296 max
= ret
->dll
->funs
[ret
->ordinal
].nrofargs
;
303 DPRINTF("%04x%s",ret
->args
[i
],i
?",":"");
304 if (max
!=ret
->dll
->funs
[ret
->ordinal
].nrofargs
)
306 DPRINTF(") retval = %04x:%04x ret=%04x:%04x\n",
307 DX_reg(context
),AX_reg(context
),HIWORD(ret
->origreturn
),LOWORD(ret
->origreturn
)
309 HeapFree(SystemHeap
,0,ret
->args
);
312 DPRINTF("Ret %s.%ld: %s() retval = %04x:%04x ret=%04x:%04x\n",
313 ret
->dll
->name
,ret
->ordinal
,ret
->dll
->funs
[ret
->ordinal
].name
,
314 DX_reg(context
),AX_reg(context
),HIWORD(ret
->origreturn
),LOWORD(ret
->origreturn
)
316 ret
->origreturn
= NULL
; /* mark as empty */
318 #else /* !__i386__ */
319 void SNOOP16_RegisterDLL(NE_MODULE
*pModule
,LPCSTR name
) {
320 FIXME(snoop
,"snooping works only on i386 for now.\n");
324 FARPROC16
SNOOP16_GetProcAddress16(HMODULE16 hmod
,DWORD ordinal
,FARPROC16 origfun
) {
327 #endif /* !__i386__ */
331 fnSNOOP16_GetProcAddress16
=SNOOP16_GetProcAddress16
;
332 fnSNOOP16_RegisterDLL
=SNOOP16_RegisterDLL
;