.spec.c files are no longer used for 32-bit dlls.
[wine] / dlls / kernel / snoop16.c
1 /*
2  * 386-specific Win16 dll<->dll snooping functions
3  *
4  * Copyright 1998 Marcus Meissner
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winnt.h"
31 #include "wine/winbase16.h"
32 #include "wine/library.h"
33 #include "kernel_private.h"
34 #include "module.h"
35 #include "stackframe.h"
36 #include "builtin16.h"
37 #include "toolhelp.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(snoop);
41
42 #ifdef __i386__
43
44 #include "pshpack1.h"
45
46 extern int SNOOP16_ShowDebugmsgSnoop(const char *dll,int ord,const char *fname);
47
48 void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT86 *context);
49 void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT86 *context);
50
51 typedef struct tagSNOOP16_FUN {
52         /* code part */
53         BYTE            lcall;          /* 0x9a call absolute with segment */
54         DWORD           snr;
55         /* unreached */
56         int             nrofargs;
57         FARPROC16       origfun;
58         char            *name;
59 } SNOOP16_FUN;
60
61 typedef struct tagSNOOP16_DLL {
62         HMODULE16       hmod;
63         HANDLE16        funhandle;
64         SNOOP16_FUN     *funs;
65         struct tagSNOOP16_DLL   *next;
66         char name[1];
67 } SNOOP16_DLL;
68
69 typedef struct tagSNOOP16_RETURNENTRY {
70         /* code part */
71         BYTE            lcall;          /* 0x9a call absolute with segment */
72         DWORD           snr;
73         /* unreached */
74         FARPROC16       origreturn;
75         SNOOP16_DLL     *dll;
76         DWORD           ordinal;
77         WORD            origSP;
78         WORD            *args;          /* saved args across a stdcall */
79 } SNOOP16_RETURNENTRY;
80
81 typedef struct tagSNOOP16_RETURNENTRIES {
82         SNOOP16_RETURNENTRY entry[65500/sizeof(SNOOP16_RETURNENTRY)];
83         HANDLE16        rethandle;
84         struct tagSNOOP16_RETURNENTRIES *next;
85 } SNOOP16_RETURNENTRIES;
86
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 */
94         DWORD           callfromregs;
95         WORD            seg;
96         WORD            lret;           /* 0xcb66 */
97 } SNOOP16_RELAY;
98
99 #include "poppack.h"
100
101 static  SNOOP16_DLL             *firstdll = NULL;
102 static  SNOOP16_RETURNENTRIES   *firstrets = NULL;
103 static  SNOOP16_RELAY           *snr;
104 static  HANDLE16                xsnr = 0;
105
106 void
107 SNOOP16_RegisterDLL(NE_MODULE *pModule,LPCSTR name) {
108         SNOOP16_DLL     **dll = &(firstdll);
109         char            *s;
110
111         if (!TRACE_ON(snoop)) return;
112
113         TRACE("hmod=%x, name=%s\n", pModule->self, name);
114
115         if (!snr) {
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;
121                 snr[0].pushl    = 0x68;
122                 snr[0].realfun  = (DWORD)SNOOP16_Entry;
123                 snr[0].lcall    = 0x9a;
124                 snr[0].callfromregs = (DWORD)__wine_call_from_16_regs;
125                 snr[0].seg      = wine_get_cs();
126                 snr[0].lret     = 0xcb66;
127
128                 snr[1].pushbp   = 0x5566;
129                 snr[1].pusheax  = 0x50;
130                 snr[1].pushax   = 0x5066;
131                 snr[1].pushl    = 0x68;
132                 snr[1].realfun  = (DWORD)SNOOP16_Return;
133                 snr[1].lcall    = 0x9a;
134                 snr[1].callfromregs = (DWORD)__wine_call_from_16_regs;
135                 snr[1].seg      = wine_get_cs();
136                 snr[1].lret     = 0xcb66;
137         }
138         while (*dll) {
139                 if ((*dll)->hmod == pModule->self)
140                 {
141                     /* another dll, loaded at the same address */
142                     GlobalUnlock16((*dll)->funhandle);
143                     GlobalFree16((*dll)->funhandle);
144                     break;
145                 }
146                 dll = &((*dll)->next);
147         }
148
149         if (*dll)
150                 *dll = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *dll, sizeof(SNOOP16_DLL)+strlen(name));
151         else
152                 *dll = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SNOOP16_DLL)+strlen(name)); 
153
154         (*dll)->next    = NULL;
155         (*dll)->hmod    = pModule->self;
156         if ((s=strrchr(name,'\\')))
157                 name = s+1;
158         strcpy( (*dll)->name, name );
159         if ((s=strrchr((*dll)->name,'.')))
160                 *s='\0';
161         (*dll)->funhandle = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,WINE_LDT_FLAGS_CODE));
162         (*dll)->funs = GlobalLock16((*dll)->funhandle);
163         if (!(*dll)->funs) {
164                 HeapFree(GetProcessHeap(),0,*dll);
165                 FIXME("out of memory\n");
166                 return;
167         }
168 }
169
170 FARPROC16
171 SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
172         SNOOP16_DLL                     *dll = firstdll;
173         SNOOP16_FUN                     *fun;
174         NE_MODULE                       *pModule = NE_GetPtr(hmod);
175         unsigned char                   *cpnt;
176         char                            name[200];
177
178         if (!TRACE_ON(snoop) || !pModule || !HIWORD(origfun))
179                 return origfun;
180         if (!*(LPBYTE)MapSL((SEGPTR)origfun)) /* 0x00 is an imposs. opcode, poss. dataref. */
181                 return origfun;
182         while (dll) {
183                 if (hmod == dll->hmod)
184                         break;
185                 dll=dll->next;
186         }
187         if (!dll)       /* probably internal */
188                 return origfun;
189         if (ordinal>65535/sizeof(SNOOP16_FUN))
190                 return origfun;
191         fun = dll->funs+ordinal;
192         /* already done? */
193         fun->lcall      = 0x9a;
194         fun->snr        = MAKELONG(0,xsnr);
195         fun->origfun    = origfun;
196         if (fun->name)
197                 return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
198         cpnt = (unsigned char *)pModule + pModule->name_table;
199         while (*cpnt) {
200                 cpnt += *cpnt + 1 + sizeof(WORD);
201                 if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
202                         sprintf(name,"%.*s",*cpnt,cpnt+1);
203                         break;
204                 }
205         }
206         /* Now search the non-resident names table */
207
208         if (!*cpnt && pModule->nrname_handle) {
209                 cpnt = (char *)GlobalLock16( pModule->nrname_handle );
210                 while (*cpnt) {
211                         cpnt += *cpnt + 1 + sizeof(WORD);
212                         if (*(WORD*)(cpnt+*cpnt+1) == ordinal) {
213                                     sprintf(name,"%.*s",*cpnt,cpnt+1);
214                                     break;
215                         }
216                 }
217         }
218         if (*cpnt)
219         {
220             fun->name = HeapAlloc(GetProcessHeap(),0,strlen(name)+1);
221             strcpy( fun->name, name );
222         }
223         else
224             fun->name = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,1); /* empty string */
225
226         if (!SNOOP16_ShowDebugmsgSnoop(dll->name, ordinal, fun->name))
227                 return origfun;
228
229         /* more magic. do not try to snoop thunk data entries (MMSYSTEM) */
230         if (strchr(fun->name,'_')) {
231                 char *s=strchr(fun->name,'_');
232
233                 if (!strncasecmp(s,"_thunkdata",10)) {
234                         HeapFree(GetProcessHeap(),0,fun->name);
235                         fun->name = NULL;
236                         return origfun;
237                 }
238         }
239         fun->lcall      = 0x9a;
240         fun->snr        = MAKELONG(0,xsnr);
241         fun->origfun    = origfun;
242         fun->nrofargs   = -1;
243         return (FARPROC16)(SEGPTR)MAKELONG(((char*)fun-(char*)dll->funs),dll->funhandle);
244 }
245
246 #define CALLER1REF (*(DWORD*)(MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)+4))))
247 void WINAPI SNOOP16_Entry(FARPROC proc, LPBYTE args, CONTEXT86 *context) {
248         DWORD           ordinal=0;
249         DWORD           entry=(DWORD)MapSL( MAKESEGPTR(context->SegCs,LOWORD(context->Eip)) )-5;
250         WORD            xcs = context->SegCs;
251         SNOOP16_DLL     *dll = firstdll;
252         SNOOP16_FUN     *fun = NULL;
253         SNOOP16_RETURNENTRIES   **rets = &firstrets;
254         SNOOP16_RETURNENTRY     *ret;
255         int             i=0, max;
256
257         while (dll) {
258                 if (xcs == dll->funhandle) {
259                         fun = (SNOOP16_FUN*)entry;
260                         ordinal = fun-dll->funs;
261                         break;
262                 }
263                 dll=dll->next;
264         }
265         if (!dll) {
266                 FIXME("entrypoint 0x%08lx not found\n",entry);
267                 return; /* oops */
268         }
269         while (*rets) {
270                 for (i=0;i<sizeof((*rets)->entry)/sizeof((*rets)->entry[0]);i++)
271                         if (!(*rets)->entry[i].origreturn)
272                                 break;
273                 if (i!=sizeof((*rets)->entry)/sizeof((*rets)->entry[0]))
274                         break;
275                 rets = &((*rets)->next);
276         }
277         if (!*rets) {
278                 HANDLE16 hand = GlobalHandleToSel16(GLOBAL_Alloc(GMEM_ZEROINIT,65535,0,WINE_LDT_FLAGS_CODE));
279                 *rets = GlobalLock16(hand);
280                 (*rets)->rethandle = hand;
281                 i = 0;  /* entry 0 is free */
282         }
283         ret = &((*rets)->entry[i]);
284         ret->lcall      = 0x9a;
285         ret->snr        = MAKELONG(sizeof(SNOOP16_RELAY),xsnr);
286         ret->origreturn = (FARPROC16)CALLER1REF;
287         CALLER1REF      = MAKELONG((char*)&(ret->lcall)-(char*)((*rets)->entry),(*rets)->rethandle);
288         ret->dll        = dll;
289         ret->args       = NULL;
290         ret->ordinal    = ordinal;
291         ret->origSP     = LOWORD(context->Esp);
292
293         context->Eip= LOWORD(fun->origfun);
294         context->SegCs = HIWORD(fun->origfun);
295
296
297         DPRINTF("%04lx:CALL %s.%ld: %s(",GetCurrentThreadId(), dll->name,ordinal,fun->name);
298         if (fun->nrofargs>0) {
299                 max = fun->nrofargs;
300                 if (max>16) max=16;
301                 for (i=max;i--;)
302                         DPRINTF("%04x%s",*(WORD*)((char *) MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)) )+8+sizeof(WORD)*i),i?",":"");
303                 if (max!=fun->nrofargs)
304                         DPRINTF(" ...");
305         } else if (fun->nrofargs<0) {
306                 DPRINTF("<unknown, check return>");
307                 ret->args = HeapAlloc(GetProcessHeap(),0,16*sizeof(WORD));
308                 memcpy(ret->args,(LPBYTE)((char *) MapSL( MAKESEGPTR(context->SegSs,LOWORD(context->Esp)) )+8),sizeof(WORD)*16);
309         }
310         DPRINTF(") ret=%04x:%04x\n",HIWORD(ret->origreturn),LOWORD(ret->origreturn));
311 }
312
313 void WINAPI SNOOP16_Return(FARPROC proc, LPBYTE args, CONTEXT86 *context) {
314         SNOOP16_RETURNENTRY     *ret = (SNOOP16_RETURNENTRY*)((char *) MapSL( MAKESEGPTR(context->SegCs,LOWORD(context->Eip)) )-5);
315
316         /* We haven't found out the nrofargs yet. If we called a cdecl
317          * function it is too late anyway and we can just set '0' (which
318          * will be the difference between orig and current SP
319          * If pascal -> everything ok.
320          */
321         if (ret->dll->funs[ret->ordinal].nrofargs<0) {
322                 ret->dll->funs[ret->ordinal].nrofargs=(LOWORD(context->Esp)-ret->origSP-4)/2;
323         }
324         context->Eip = LOWORD(ret->origreturn);
325         context->SegCs  = HIWORD(ret->origreturn);
326         if (ret->args) {
327                 int     i,max;
328
329                 DPRINTF("%04lx:RET  %s.%ld: %s(",
330                         GetCurrentThreadId(),ret->dll->name,ret->ordinal,
331                         ret->dll->funs[ret->ordinal].name);
332                 max = ret->dll->funs[ret->ordinal].nrofargs;
333                 if (max>16)
334                         max=16;
335                 if (max<0)
336                         max=0;
337
338                 for (i=max;i--;)
339                         DPRINTF("%04x%s",ret->args[i],i?",":"");
340                 if (max!=ret->dll->funs[ret->ordinal].nrofargs)
341                         DPRINTF(" ...");
342                 DPRINTF(") retval = %04x:%04x ret=%04x:%04x\n",
343                         (WORD)context->Edx,(WORD)context->Eax,
344                         HIWORD(ret->origreturn),LOWORD(ret->origreturn)
345                 );
346                 HeapFree(GetProcessHeap(),0,ret->args);
347                 ret->args = NULL;
348         } else
349                 DPRINTF("RET  %s.%ld: %s() retval = %04x:%04x ret=%04x:%04x\n",
350                         ret->dll->name,ret->ordinal,ret->dll->funs[ret->ordinal].name,
351                         (WORD)context->Edx,(WORD)context->Eax,
352                         HIWORD(ret->origreturn),LOWORD(ret->origreturn)
353                 );
354         ret->origreturn = NULL; /* mark as empty */
355 }
356 #else   /* !__i386__ */
357 void SNOOP16_RegisterDLL(NE_MODULE *pModule,LPCSTR name) {
358         if (!TRACE_ON(snoop)) return;
359         FIXME("snooping works only on i386 for now.\n");
360 }
361
362 FARPROC16 SNOOP16_GetProcAddress16(HMODULE16 hmod,DWORD ordinal,FARPROC16 origfun) {
363         return origfun;
364 }
365 #endif  /* !__i386__ */