2 * 386-specific Win16 dll<->dll snooping functions
4 * Copyright 1998 Marcus Meissner
15 #include "selectors.h"
16 #include "stackframe.h"
23 void WINAPI
SNOOP16_Entry(CONTEXT
*context
);
24 void WINAPI
SNOOP16_Return(CONTEXT
*context
);
25 extern void CallFrom16_p_regs_();
27 /* Generic callfrom16_p_regs function entry.
29 * pushl $DOS3Call DWORD fun32
31 * .long CallFrom16_p_regs_ DWORD addr
32 * .long 0x90900023 WORD seg;nop;nop
35 typedef struct tagSNOOP16_FUN
{
37 BYTE lcall
; /* 0x9a call absolute with segment */
45 typedef struct tagSNOOP16_DLL
{
50 struct tagSNOOP16_DLL
*next
;
53 typedef struct tagSNOOP16_RETURNENTRY
{
55 BYTE lcall
; /* 0x9a call absolute with segment */
62 WORD
*args
; /* saved args across a stdcall */
63 } SNOOP16_RETURNENTRY
;
65 typedef struct tagSNOOP16_RETURNENTRIES
{
66 SNOOP16_RETURNENTRY entry
[65500/sizeof(SNOOP16_RETURNENTRY
)];
68 struct tagSNOOP16_RETURNENTRIES
*next
;
69 } SNOOP16_RETURNENTRIES
;
71 typedef struct tagSNOOP16_RELAY
{
73 BYTE prefix
; /* 0x66 , 32bit prefix */
74 BYTE pushbp
; /* 0x55 */
75 BYTE pushl
; /* 0x68 */
76 DWORD realfun
; /* SNOOP16_Return */
77 BYTE lcall
; /* 0x9a call absolute with segment */
85 static SNOOP16_DLL
*firstdll
= NULL
;
86 static SNOOP16_RETURNENTRIES
*firstrets
= NULL
;
87 static SNOOP16_RELAY
*snr
;
88 static HANDLE16 xsnr
= 0;
91 SNOOP16_RegisterDLL(NE_MODULE
*pModule
,LPCSTR name
) {
92 SNOOP16_DLL
**dll
= &(firstdll
);
95 if (!TRACE_ON(snoop
)) return;
97 xsnr
=GLOBAL_Alloc(GMEM_ZEROINIT
,2*sizeof(*snr
),0,TRUE
,TRUE
,FALSE
);
98 snr
= GlobalLock16(xsnr
);
100 snr
[0].pushbp
= 0x55;
102 snr
[0].realfun
= (DWORD
)SNOOP16_Entry
;
104 snr
[0].callfromregs
= (DWORD
)CallFrom16_p_regs_
;
106 snr
[1].prefix
= 0x66;
107 snr
[1].pushbp
= 0x55;
109 snr
[1].realfun
= (DWORD
)SNOOP16_Return
;
111 snr
[1].callfromregs
= (DWORD
)CallFrom16_p_regs_
;
115 if ((*dll
)->hmod
== pModule
->self
)
116 return; /* already registered */
117 dll
= &((*dll
)->next
);
119 *dll
= (SNOOP16_DLL
*)HeapAlloc(SystemHeap
,HEAP_ZERO_MEMORY
,sizeof(SNOOP16_DLL
));
121 (*dll
)->hmod
= pModule
->self
;
122 if ((s
=strrchr(name
,'\\')))
124 (*dll
)->name
= HEAP_strdupA(SystemHeap
,0,name
);
125 if ((s
=strrchr((*dll
)->name
,'.')))
127 (*dll
)->funhandle
= GlobalHandleToSel(GLOBAL_Alloc(GMEM_ZEROINIT
,65535,0,TRUE
,FALSE
,FALSE
));
128 (*dll
)->funs
= GlobalLock16((*dll
)->funhandle
);
130 HeapFree(SystemHeap
,0,*dll
);
131 FIXME(snoop
,"out of memory\n");
134 memset((*dll
)->funs
,0,65535);
138 SNOOP16_GetProcAddress16(HMODULE16 hmod
,DWORD ordinal
,FARPROC16 origfun
) {
139 SNOOP16_DLL
*dll
= firstdll
;
141 NE_MODULE
*pModule
= NE_GetPtr(hmod
);
145 if (!TRACE_ON(snoop
) || !pModule
|| !HIWORD(origfun
))
147 if (!*(LPBYTE
)PTR_SEG_TO_LIN(origfun
)) /* 0x00 is an imposs. opcode, poss. dataref. */
150 if (hmod
== dll
->hmod
)
154 if (!dll
) /* probably internal */
156 if (ordinal
>65535/sizeof(SNOOP16_FUN
))
158 fun
= dll
->funs
+ordinal
;
161 fun
->snr
= MAKELONG(0,xsnr
);
162 fun
->origfun
= origfun
;
164 return (FARPROC16
)(SEGPTR
)MAKELONG(((char*)fun
-(char*)dll
->funs
),dll
->funhandle
);
165 cpnt
= (unsigned char *)pModule
+ pModule
->name_table
;
167 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
168 if (*(WORD
*)(cpnt
+*cpnt
+1) == ordinal
) {
169 sprintf(name
,"%.*s",*cpnt
,cpnt
+1);
173 /* Now search the non-resident names table */
175 if (!*cpnt
&& pModule
->nrname_handle
) {
176 cpnt
= (char *)GlobalLock16( pModule
->nrname_handle
);
178 cpnt
+= *cpnt
+ 1 + sizeof(WORD
);
179 if (*(WORD
*)(cpnt
+*cpnt
+1) == ordinal
) {
180 sprintf(name
,"%.*s",*cpnt
,cpnt
+1);
186 fun
->name
= HEAP_strdupA(SystemHeap
,0,name
);
188 fun
->name
= HEAP_strdupA(SystemHeap
,0,"");
190 fun
->snr
= MAKELONG(0,xsnr
);
191 fun
->origfun
= origfun
;
193 return (FARPROC16
)(SEGPTR
)MAKELONG(((char*)fun
-(char*)dll
->funs
),dll
->funhandle
);
196 #define CALLER1REF (*(DWORD*)(PTR_SEG_OFF_TO_LIN(SS_reg(context),SP_reg(context)+4)))
197 void WINAPI
SNOOP16_Entry(CONTEXT
*context
) {
199 DWORD entry
=(DWORD
)PTR_SEG_OFF_TO_LIN(CS_reg(context
),IP_reg(context
))-5;
200 WORD xcs
= CS_reg(context
);
201 SNOOP16_DLL
*dll
= firstdll
;
202 SNOOP16_FUN
*fun
= NULL
;
203 SNOOP16_RETURNENTRIES
**rets
= &firstrets
;
204 SNOOP16_RETURNENTRY
*ret
;
208 if (xcs
== dll
->funhandle
) {
209 fun
= (SNOOP16_FUN
*)entry
;
210 ordinal
= fun
-dll
->funs
;
216 FIXME(snoop
,"entrypoint 0x%08lx not found\n",entry
);
219 /* guess cdecl ... */
220 if (fun
->nrofargs
<0) {
221 /* Typical cdecl return frame is:
223 * which has (for xxxxxxxx up to 255 the opcode "83 C4 xx".
225 LPBYTE reteip
= (LPBYTE
)PTR_SEG_TO_LIN(CALLER1REF
);
227 if ((reteip
[0]==0x83)&&(reteip
[1]==0xc4))
228 fun
->nrofargs
=reteip
[2]/2;
232 for (i
=0;i
<sizeof((*rets
)->entry
)/sizeof((*rets
)->entry
[0]);i
++)
233 if (!(*rets
)->entry
[i
].origreturn
)
235 if (i
!=sizeof((*rets
)->entry
)/sizeof((*rets
)->entry
[0]))
237 rets
= &((*rets
)->next
);
240 HANDLE16 hand
= GlobalHandleToSel(GLOBAL_Alloc(GMEM_ZEROINIT
,65535,0,TRUE
,FALSE
,FALSE
));
241 *rets
= GlobalLock16(hand
);
242 memset(*rets
,0,65535);
243 (*rets
)->rethandle
= hand
;
244 i
= 0; /* entry 0 is free */
246 ret
= &((*rets
)->entry
[i
]);
248 ret
->snr
= MAKELONG(sizeof(SNOOP16_RELAY
),xsnr
);
249 ret
->origreturn
= (FARPROC16
)CALLER1REF
;
250 CALLER1REF
= MAKELONG((char*)&(ret
->lcall
)-(char*)((*rets
)->entry
),(*rets
)->rethandle
);
253 ret
->ordinal
= ordinal
;
254 ret
->origSP
= SP_reg(context
);
256 IP_reg(context
)= LOWORD(fun
->origfun
);
257 CS_reg(context
)= HIWORD(fun
->origfun
);
259 DPRINTF("Call %s.%ld: %s(",dll
->name
,ordinal
,fun
->name
);
260 if (fun
->nrofargs
>0) {
261 max
= fun
->nrofargs
; if (max
>16) max
=16;
263 DPRINTF("%04x%s",*(WORD
*)(PTR_SEG_OFF_TO_LIN(SS_reg(context
),SP_reg(context
))+8+sizeof(WORD
)*i
),i
?",":"");
264 if (max
!=fun
->nrofargs
)
266 } else if (fun
->nrofargs
<0) {
267 DPRINTF("<unknown, check return>");
268 ret
->args
= HeapAlloc(SystemHeap
,0,16*sizeof(WORD
));
269 memcpy(ret
->args
,(LPBYTE
)(PTR_SEG_OFF_TO_LIN(SS_reg(context
),SP_reg(context
))+8),sizeof(WORD
)*16);
271 DPRINTF(") ret=%04x:%04x\n",HIWORD((DWORD
)(*rets
)->entry
[i
].origreturn
),LOWORD((DWORD
)(*rets
)->entry
[i
].origreturn
));
274 void WINAPI
SNOOP16_Return(CONTEXT
*context
) {
275 SNOOP16_RETURNENTRY
*ret
= (SNOOP16_RETURNENTRY
*)(PTR_SEG_OFF_TO_LIN(CS_reg(context
),IP_reg(context
))-5);
277 /* We haven't found out the nrofargs yet. If we called a cdecl
278 * function it is too late anyway and we can just set '0' (which
279 * will be the difference between orig and current SP
280 * If pascal -> everything ok.
282 if (ret
->dll
->funs
[ret
->ordinal
].nrofargs
<0)
283 ret
->dll
->funs
[ret
->ordinal
].nrofargs
=(SP_reg(context
)-ret
->origSP
-4)/2;
284 IP_reg(context
) = LOWORD(ret
->origreturn
);
285 CS_reg(context
) = HIWORD(ret
->origreturn
);
289 DPRINTF("Ret %s.%ld: %s(",ret
->dll
->name
,ret
->ordinal
,ret
->dll
->funs
[ret
->ordinal
].name
);
290 max
= ret
->dll
->funs
[ret
->ordinal
].nrofargs
;
294 DPRINTF("%04x%s",ret
->args
[i
],i
?",":"");
295 DPRINTF(") retval = %04x:%04x ret=%04x:%04x\n",
296 DX_reg(context
),AX_reg(context
),HIWORD(ret
->origreturn
),LOWORD(ret
->origreturn
)
298 HeapFree(SystemHeap
,0,ret
->args
);
301 DPRINTF("Ret %s.%ld: %s() retval = %04x:%04x ret=%04x:%04x\n",
302 ret
->dll
->name
,ret
->ordinal
,ret
->dll
->funs
[ret
->ordinal
].name
,
303 DX_reg(context
),AX_reg(context
),HIWORD(ret
->origreturn
),LOWORD(ret
->origreturn
)
305 ret
->origreturn
= NULL
; /* mark as empty */
307 #else /* !__i386__ */
308 void SNOOP16_RegisterDLL(NE_MODULE
*pModule
,LPCSTR name
) {
309 FIXME(snoop
,"snooping works only on i386 for now.\n");
313 FARPROC16
SNOOP16_GetProcAddress16(HMODULE16 hmod
,DWORD ordinal
,FARPROC16 origfun
) {
316 #endif /* !__i386__ */