Release 941227
[wine] / if1632 / relay.c
1 /*
2 static char RCSId[] = "$Id: relay.c,v 1.2 1993/07/04 04:04:21 root Exp root $";
3 static char Copyright[] = "Copyright  Robert J. Amstadt, 1993";
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <errno.h>
14 #ifdef linux
15 #include <linux/unistd.h>
16 #include <linux/head.h>
17 #include <linux/ldt.h>
18 #endif
19
20 #include "neexe.h"
21 #include "segmem.h"
22 #include "prototypes.h"
23 #include "dlls.h"
24 #include "options.h"
25 #include "stddebug.h"
26 /* #define DEBUG_RELAY /* */
27 /* #define DEBUG_STACK /* */
28 #include "debug.h"
29
30 #if 0
31 /* Make make_debug think these were really used */
32 dprintf_relay
33 dprintf_stack
34 #endif
35
36 #ifdef WINELIB
37 #define WineLibSkip(x) 0
38 #else
39 #define WineLibSkip(x) x
40 #endif
41
42 struct dll_name_table_entry_s dll_builtin_table[N_BUILTINS] =
43 {
44     { "KERNEL",  WineLibSkip(KERNEL_table),     410, 1 },
45     { "USER",    WineLibSkip(USER_table),       540, 2 },
46     { "GDI",     WineLibSkip(GDI_table),        490, 3 },
47     { "UNIXLIB", WineLibSkip(UNIXLIB_table),  10, 4 },
48     { "WIN87EM", WineLibSkip(WIN87EM_table),  10, 5 },
49     { "SHELL",   WineLibSkip(SHELL_table),   103, 6 },
50     { "SOUND",   WineLibSkip(SOUND_table),    20, 7 },
51     { "KEYBOARD",WineLibSkip(KEYBOARD_table),137, 8 },
52     { "WINSOCK", WineLibSkip(WINSOCK_table), 155, 9 },
53     { "STRESS",  WineLibSkip(STRESS_table),   15, 10},
54     { "MMSYSTEM",WineLibSkip(MMSYSTEM_table),1226,11},
55     { "SYSTEM",  WineLibSkip(SYSTEM_table),   20 ,12},
56     { "TOOLHELP",WineLibSkip(TOOLHELP_table), 83, 13},
57     { "MOUSE",   WineLibSkip(MOUSE_table),     8, 14},
58     { "EMUCOMMDLG", WineLibSkip(COMMDLG_table),  31, 15},
59 };
60 /* don't forget to increase N_BUILTINS in dll.h if you add a dll */
61
62 #ifndef WINELIB
63 unsigned short *Stack16Frame;
64
65 extern unsigned long  IF1632_Saved16_esp;
66 extern unsigned long  IF1632_Saved16_ebp;
67 extern unsigned short IF1632_Saved16_ss;
68
69 /**********************************************************************
70  *                                      DLLRelay
71  *
72  * We get a stack frame pointer to data that looks like this:
73  *
74  *   Hex Offset Contents
75  *   ---------- -------
76  *      +00     previous saved_16ss
77  *      +02     previous saved_16ebp
78  *      +06     previous saved_16esp
79  *      +0A     16-bit es
80  *      +0C     16-bit ds
81  *      +0E     16-bit ebp
82  *      +12     length of 16-bit arguments
83  *      +14     16-bit ip
84  *      +16     16-bit cs
85  *      +18     arguments
86  */
87 int
88 DLLRelay(unsigned int func_num, unsigned int seg_off)
89 {
90     struct dll_table_entry_s *dll_p;
91     unsigned short *saved_Stack16Frame;
92     unsigned int offset;
93     unsigned int dll_id;
94     unsigned int ordinal;
95     int arg_table[DLL_MAX_ARGS];
96     void *arg_ptr;
97     int (*func_ptr)();
98     int i;
99     int ret_val;
100     
101     /*
102      * Determine address of arguments.
103      */
104     saved_Stack16Frame = Stack16Frame;
105     Stack16Frame = (unsigned short *) seg_off;
106     arg_ptr = (void *) (seg_off + 0x18);
107
108     /*
109      * Extract the DLL number and ordinal number.
110      */
111     dll_id  = ((func_num >> 16) & 0xffff) - 1;
112     ordinal = func_num & 0xffff;
113     dll_p   = &dll_builtin_table[dll_id].dll_table[ordinal];
114
115     if (debugging_relay)
116     {
117         unsigned int *ret_addr;
118         
119         ret_addr = (unsigned int *) ((char *) seg_off + 0x14);
120         printf("Call %s (%s.%d), stack=%04x:%04x, ",
121                dll_p->export_name,
122                dll_builtin_table[dll_id].dll_name, ordinal,
123                seg_off >> 16, seg_off & 0xffff);
124         printf("ret=%08x", *ret_addr);
125         printf("  ESP=%08lx, EBP=%08lx, SS=%04x\n", 
126                IF1632_Saved16_esp, IF1632_Saved16_ebp,
127                IF1632_Saved16_ss);
128
129         if(debugging_stack)
130         {
131             unsigned short *stack_p = (unsigned short *) seg_off;
132             for (i = 0; i < 24; i++, stack_p++)
133             {
134                 printf("%04x ", *stack_p);
135                 if ((i & 7) == 7)
136                     printf("\n");
137             }
138             printf("\n");
139         }
140     } /* DEBUG_RELAY */
141
142     /*
143      * Make sure we have a handler defined for this call.
144      */
145     if (dll_p->handler == NULL)
146     {
147         char buffer[100];
148         
149         sprintf(buffer, "No handler for routine %s.%d", 
150                 dll_builtin_table[dll_id].dll_name, ordinal);
151         myerror(buffer);
152     }
153     func_ptr = dll_p->handler;
154
155     /*
156      * OK, special case.  If the handler is define as taking no arguments
157      * then pass the address of the arguments on the 16-bit stack to the
158      * handler.  It will just ignore the pointer if it really takes no
159      * arguments.  This allows us to write slightly faster library routines
160      * if we choose.
161      */
162     if (dll_p->n_args == 0)
163     {
164         ret_val = (*func_ptr)(arg_ptr);
165         Stack16Frame = saved_Stack16Frame;
166         return ret_val;
167     }
168
169     /*
170      * Getting this far means we need to convert the 16-bit argument stack.
171      */
172     for (i = 0; i < dll_p->n_args; i++)
173     {
174         short *sp;
175         int *ip;
176         
177         offset = dll_p->args[i].dst_arg;
178
179         switch (dll_p->args[i].src_type)
180         {
181           case DLL_ARGTYPE_SIGNEDWORD:
182             sp = (short *) ((char *) arg_ptr + offset);
183             arg_table[i] = *sp;
184             break;
185             
186           case DLL_ARGTYPE_WORD:
187             sp = (short *) ((char *) arg_ptr + offset);
188             arg_table[i] = (int) *sp & 0xffff;
189             break;
190             
191           case DLL_ARGTYPE_LONG:
192             ip = (int *) ((char *) arg_ptr + offset);
193             arg_table[i] = *ip;
194             break;
195
196           case DLL_ARGTYPE_FARPTR:
197             ip = (int *) ((char *) arg_ptr + offset);
198             if (*ip & 0xffff0000)
199                 arg_table[i] = FIXPTR(*ip);
200             else
201                 arg_table[i] = *ip;
202             break;
203         }
204     }
205
206     /*
207      * Call the handler
208      */
209     ret_val = (*func_ptr)(arg_table[0], arg_table[1], arg_table[2], 
210                           arg_table[3], arg_table[4], arg_table[5], 
211                           arg_table[6], arg_table[7], arg_table[8], 
212                           arg_table[9], arg_table[10], arg_table[11],
213                           arg_table[12], arg_table[13], arg_table[14], 
214                           arg_table[15]);
215
216     if (debugging_relay)
217     {
218         printf("Returning %08x from %s (%s.%d)\n",
219                ret_val,
220                dll_p->export_name,
221                dll_builtin_table[dll_id].dll_name, ordinal);
222     }
223
224     Stack16Frame = saved_Stack16Frame;
225     return ret_val;
226 }
227 #endif
228
229 /**********************************************************************
230  *                                      FindDLLTable
231  */
232 struct  dll_table_entry_s *
233 FindDLLTable(char *dll_name)
234 {
235     int i;
236
237     for (i = 0; i < N_BUILTINS; i++)
238         if (strcasecmp(dll_builtin_table[i].dll_name, dll_name) == 0)
239 #ifdef WINELIB
240             return dll_builtin_table[i].dll_number;
241 #else
242             return dll_builtin_table[i].dll_table;
243 #endif
244     return NULL;
245 }
246
247 /**********************************************************************
248  *                                      FindOrdinalFromName
249  */
250 int
251 FindOrdinalFromName(struct dll_table_entry_s *dll_table, char *func_name)
252 {
253     int i, limit;
254
255     for (i = 0; i < N_BUILTINS; i++)
256         if (dll_table == dll_builtin_table[i].dll_table)
257             break;
258     
259     if (i == N_BUILTINS)
260         return 0;
261
262     limit = dll_builtin_table[i].dll_table_length;
263     for (i = 0; i < limit; i++)
264         if (strcasecmp(dll_table[i].export_name, func_name) == 0)
265             return i;
266     
267     return 0;
268 }
269 /**********************************************************************
270  *                                      ReturnArg
271  */
272 int
273 ReturnArg(int arg)
274 {
275     return arg;
276 }
277
278 #ifndef WINELIB
279 #ifdef WINESTAT
280 void winestat(){
281         int i, j;
282         double perc;
283         int used, implemented;
284         int tused, timplemented;
285         struct dll_table_entry_s *table;
286
287         tused = 0;
288         timplemented = 0;
289     for (i = 0; i < N_BUILTINS; i++) {
290             table = dll_builtin_table[i].dll_table;
291             used = 0;
292             implemented = 0;
293             for(j=0; j < dll_builtin_table[i].dll_table_length; j++) {
294                     if(table[j].used){
295                             used++;
296                             if (table[j].handler) implemented++;
297                             else 
298                                     printf("%s.%d not implemented\n",
299                                            dll_builtin_table[i].dll_name,
300                                            j);
301                     };
302             };
303             tused += used;
304             timplemented += implemented;
305             if(used)
306                     perc = implemented * 100.00 / used;
307             else
308                     perc = 0.0;
309             if (used)
310                     printf("%s: %d of %d (%3.1f %%)\n", dll_builtin_table[i].dll_name, implemented, used, perc);
311     };
312         perc = timplemented * 100.00 / tused;
313         printf("TOTAL: %d of %d winapi functions implemented (%3.1f %%)\n",timplemented, tused, perc);
314 }
315 #endif /* WINESTAT */
316 #endif /* !WINELIB */