Added filename to 32 bit builtin module descriptors.
[wine/dcerpc.git] / tools / build.c
blob65e710646bb96d885893003beff42325f6a47acc
1 /*
2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Martin von Loewis
4 * Copyright 1995, 1996, 1997 Alexandre Julliard
5 * Copyright 1997 Eric Youngdale
6 * Copyright 1999 Ulrich Weigand
7 */
9 #include <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <unistd.h>
16 #include "winbase.h"
17 #include "winnt.h"
18 #include "module.h"
19 #include "neexe.h"
20 #include "selectors.h"
21 #include "stackframe.h"
22 #include "builtin16.h"
23 #include "thread.h"
25 #ifdef NEED_UNDERSCORE_PREFIX
26 # define PREFIX "_"
27 #else
28 # define PREFIX
29 #endif
31 #ifdef HAVE_ASM_STRING
32 # define STRING ".string"
33 #else
34 # define STRING ".ascii"
35 #endif
37 #if defined(__GNUC__) && !defined(__svr4__)
38 # define USE_STABS
39 #else
40 # undef USE_STABS
41 #endif
43 typedef enum
45 TYPE_INVALID,
46 TYPE_BYTE, /* byte variable (Win16) */
47 TYPE_WORD, /* word variable (Win16) */
48 TYPE_LONG, /* long variable (Win16) */
49 TYPE_PASCAL_16, /* pascal function with 16-bit return (Win16) */
50 TYPE_PASCAL, /* pascal function with 32-bit return (Win16) */
51 TYPE_ABS, /* absolute value (Win16) */
52 TYPE_REGISTER, /* register function */
53 TYPE_INTERRUPT, /* interrupt handler function (Win16) */
54 TYPE_STUB, /* unimplemented stub */
55 TYPE_STDCALL, /* stdcall function (Win32) */
56 TYPE_CDECL, /* cdecl function (Win32) */
57 TYPE_VARARGS, /* varargs function (Win32) */
58 TYPE_EXTERN, /* external symbol (Win32) */
59 TYPE_FORWARD, /* forwarded function (Win32) */
60 TYPE_NBTYPES
61 } ORD_TYPE;
63 static const char * const TypeNames[TYPE_NBTYPES] =
65 NULL,
66 "byte", /* TYPE_BYTE */
67 "word", /* TYPE_WORD */
68 "long", /* TYPE_LONG */
69 "pascal16", /* TYPE_PASCAL_16 */
70 "pascal", /* TYPE_PASCAL */
71 "equate", /* TYPE_ABS */
72 "register", /* TYPE_REGISTER */
73 "interrupt", /* TYPE_INTERRUPT */
74 "stub", /* TYPE_STUB */
75 "stdcall", /* TYPE_STDCALL */
76 "cdecl", /* TYPE_CDECL */
77 "varargs", /* TYPE_VARARGS */
78 "extern", /* TYPE_EXTERN */
79 "forward" /* TYPE_FORWARD */
82 #define MAX_ORDINALS 2048
83 #define MAX_IMPORTS 16
85 /* Callback function used for stub functions */
86 #define STUB_CALLBACK \
87 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
89 typedef enum
91 SPEC_INVALID,
92 SPEC_WIN16,
93 SPEC_WIN32
94 } SPEC_TYPE;
96 typedef struct
98 int n_values;
99 int *values;
100 } ORD_VARIABLE;
102 typedef struct
104 int n_args;
105 char arg_types[32];
106 char link_name[80];
107 } ORD_FUNCTION;
109 typedef struct
111 int arg_size;
112 int ret_value;
113 } ORD_RETURN;
115 typedef struct
117 int value;
118 } ORD_ABS;
120 typedef struct
122 char link_name[80];
123 } ORD_EXTERN;
125 typedef struct
127 char link_name[80];
128 } ORD_FORWARD;
130 typedef struct
132 ORD_TYPE type;
133 int offset;
134 int lineno;
135 char name[80];
136 union
138 ORD_VARIABLE var;
139 ORD_FUNCTION func;
140 ORD_RETURN ret;
141 ORD_ABS abs;
142 ORD_EXTERN ext;
143 ORD_FORWARD fwd;
144 } u;
145 } ORDDEF;
147 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
149 static SPEC_TYPE SpecType = SPEC_INVALID;
150 static char DLLName[80];
151 static char DLLFileName[80];
152 static int Limit = 0;
153 static int Base = MAX_ORDINALS;
154 static int DLLHeapSize = 0;
155 static char *SpecName;
156 static FILE *SpecFp;
157 static WORD Code_Selector, Data_Selector;
158 static char DLLInitFunc[80];
159 static char *DLLImports[MAX_IMPORTS];
160 static int nb_imports = 0;
162 char *ParseBuffer = NULL;
163 char *ParseNext;
164 char ParseSaveChar;
165 int Line;
167 static int UsePIC = 0;
169 static int debugging = 1;
171 /* Offset of a structure field relative to the start of the struct */
172 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
174 /* Offset of register relative to the start of the CONTEXT struct */
175 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT86,reg)
177 /* Offset of register relative to the start of the STACK16FRAME struct */
178 #define STACK16OFFSET(reg) STRUCTOFFSET(STACK16FRAME,reg)
180 /* Offset of register relative to the start of the STACK32FRAME struct */
181 #define STACK32OFFSET(reg) STRUCTOFFSET(STACK32FRAME,reg)
183 /* Offset of the stack pointer relative to %fs:(0) */
184 #define STACKOFFSET (STRUCTOFFSET(TEB,cur_stack))
186 static void BuildCallFrom16Func( FILE *outfile, char *profile, char *prefix, int local );
188 static void *xmalloc (size_t size)
190 void *res;
192 res = malloc (size ? size : 1);
193 if (res == NULL)
195 fprintf (stderr, "Virtual memory exhausted.\n");
196 exit (1);
198 return res;
202 static void *xrealloc (void *ptr, size_t size)
204 void *res = realloc (ptr, size);
205 if (res == NULL)
207 fprintf (stderr, "Virtual memory exhausted.\n");
208 exit (1);
210 return res;
213 static char *xstrdup( const char *str )
215 char *res = strdup( str );
216 if (!res)
218 fprintf (stderr, "Virtual memory exhausted.\n");
219 exit (1);
221 return res;
224 static int IsNumberString(char *s)
226 while (*s != '\0')
227 if (!isdigit(*s++))
228 return 0;
230 return 1;
233 static char *strupper(char *s)
235 char *p;
237 for(p = s; *p != '\0'; p++)
238 *p = toupper(*p);
240 return s;
243 static char * GetTokenInLine(void)
245 char *p;
246 char *token;
248 if (ParseNext != ParseBuffer)
250 if (ParseSaveChar == '\0')
251 return NULL;
252 *ParseNext = ParseSaveChar;
256 * Remove initial white space.
258 for (p = ParseNext; isspace(*p); p++)
261 if ((*p == '\0') || (*p == '#'))
262 return NULL;
265 * Find end of token.
267 token = p++;
268 if (*token != '(' && *token != ')')
269 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
270 p++;
272 ParseSaveChar = *p;
273 ParseNext = p;
274 *p = '\0';
276 return token;
279 static char * GetToken(void)
281 char *token;
283 if (ParseBuffer == NULL)
285 ParseBuffer = xmalloc(512);
286 ParseNext = ParseBuffer;
287 while (1)
289 Line++;
290 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
291 return NULL;
292 if (ParseBuffer[0] != '#')
293 break;
297 while ((token = GetTokenInLine()) == NULL)
299 ParseNext = ParseBuffer;
300 while (1)
302 Line++;
303 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
304 return NULL;
305 if (ParseBuffer[0] != '#')
306 break;
310 return token;
314 /*******************************************************************
315 * ParseVariable
317 * Parse a variable definition.
319 static int ParseVariable( ORDDEF *odp )
321 char *endptr;
322 int *value_array;
323 int n_values;
324 int value_array_size;
326 char *token = GetToken();
327 if (*token != '(')
329 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
330 SpecName, Line, token);
331 return -1;
334 n_values = 0;
335 value_array_size = 25;
336 value_array = xmalloc(sizeof(*value_array) * value_array_size);
338 while ((token = GetToken()) != NULL)
340 if (*token == ')')
341 break;
343 value_array[n_values++] = strtol(token, &endptr, 0);
344 if (n_values == value_array_size)
346 value_array_size += 25;
347 value_array = xrealloc(value_array,
348 sizeof(*value_array) * value_array_size);
351 if (endptr == NULL || *endptr != '\0')
353 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
354 SpecName, Line, token);
355 return -1;
359 if (token == NULL)
361 fprintf(stderr, "%s:%d: End of file in variable declaration\n",
362 SpecName, Line);
363 return -1;
366 odp->u.var.n_values = n_values;
367 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
369 return 0;
373 /*******************************************************************
374 * ParseExportFunction
376 * Parse a function definition.
378 static int ParseExportFunction( ORDDEF *odp )
380 char *token;
381 int i;
383 switch(SpecType)
385 case SPEC_WIN16:
386 if (odp->type == TYPE_STDCALL)
388 fprintf( stderr, "%s:%d: 'stdcall' not supported for Win16\n",
389 SpecName, Line );
390 return -1;
392 else if (odp->type == TYPE_VARARGS)
394 fprintf( stderr, "%s:%d: 'varargs' not supported for Win16\n",
395 SpecName, Line );
396 return -1;
398 break;
399 case SPEC_WIN32:
400 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
402 fprintf( stderr, "%s:%d: 'pascal' not supported for Win32\n",
403 SpecName, Line );
404 return -1;
406 break;
407 default:
408 break;
411 token = GetToken();
412 if (*token != '(')
414 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
415 SpecName, Line, token);
416 return -1;
419 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
421 token = GetToken();
422 if (*token == ')')
423 break;
425 if (!strcmp(token, "word"))
426 odp->u.func.arg_types[i] = 'w';
427 else if (!strcmp(token, "s_word"))
428 odp->u.func.arg_types[i] = 's';
429 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
430 odp->u.func.arg_types[i] = 'l';
431 else if (!strcmp(token, "ptr"))
432 odp->u.func.arg_types[i] = 'p';
433 else if (!strcmp(token, "str"))
434 odp->u.func.arg_types[i] = 't';
435 else if (!strcmp(token, "wstr"))
436 odp->u.func.arg_types[i] = 'W';
437 else if (!strcmp(token, "segstr"))
438 odp->u.func.arg_types[i] = 'T';
439 else if (!strcmp(token, "double"))
441 odp->u.func.arg_types[i++] = 'l';
442 odp->u.func.arg_types[i] = 'l';
444 else
446 fprintf(stderr, "%s:%d: Unknown variable type '%s'\n",
447 SpecName, Line, token);
448 return -1;
450 if (SpecType == SPEC_WIN32)
452 if (strcmp(token, "long") &&
453 strcmp(token, "ptr") &&
454 strcmp(token, "str") &&
455 strcmp(token, "wstr") &&
456 strcmp(token, "double"))
458 fprintf( stderr, "%s:%d: Type '%s' not supported for Win32\n",
459 SpecName, Line, token );
460 return -1;
464 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
466 fprintf( stderr, "%s:%d: Too many arguments\n", SpecName, Line );
467 return -1;
469 odp->u.func.arg_types[i] = '\0';
470 if ((odp->type == TYPE_STDCALL) && !i)
471 odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
472 strcpy(odp->u.func.link_name, GetToken());
473 return 0;
477 /*******************************************************************
478 * ParseEquate
480 * Parse an 'equate' definition.
482 static int ParseEquate( ORDDEF *odp )
484 char *endptr;
486 char *token = GetToken();
487 int value = strtol(token, &endptr, 0);
488 if (endptr == NULL || *endptr != '\0')
490 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
491 SpecName, Line, token);
492 return -1;
495 if (SpecType == SPEC_WIN32)
497 fprintf( stderr, "%s:%d: 'equate' not supported for Win32\n",
498 SpecName, Line );
499 return -1;
502 odp->u.abs.value = value;
503 return 0;
507 /*******************************************************************
508 * ParseStub
510 * Parse a 'stub' definition.
512 static int ParseStub( ORDDEF *odp )
514 odp->u.func.arg_types[0] = '\0';
515 strcpy( odp->u.func.link_name, STUB_CALLBACK );
516 return 0;
520 /*******************************************************************
521 * ParseInterrupt
523 * Parse an 'interrupt' definition.
525 static int ParseInterrupt( ORDDEF *odp )
527 char *token;
529 if (SpecType == SPEC_WIN32)
531 fprintf( stderr, "%s:%d: 'interrupt' not supported for Win32\n",
532 SpecName, Line );
533 return -1;
536 token = GetToken();
537 if (*token != '(')
539 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
540 SpecName, Line, token);
541 return -1;
543 token = GetToken();
544 if (*token != ')')
546 fprintf(stderr, "%s:%d: Expected ')' got '%s'\n",
547 SpecName, Line, token);
548 return -1;
551 odp->u.func.arg_types[0] = '\0';
552 strcpy( odp->u.func.link_name, GetToken() );
553 return 0;
557 /*******************************************************************
558 * ParseExtern
560 * Parse an 'extern' definition.
562 static int ParseExtern( ORDDEF *odp )
564 if (SpecType == SPEC_WIN16)
566 fprintf( stderr, "%s:%d: 'extern' not supported for Win16\n",
567 SpecName, Line );
568 return -1;
570 strcpy( odp->u.ext.link_name, GetToken() );
571 return 0;
575 /*******************************************************************
576 * ParseForward
578 * Parse a 'forward' definition.
580 static int ParseForward( ORDDEF *odp )
582 if (SpecType == SPEC_WIN16)
584 fprintf( stderr, "%s:%d: 'forward' not supported for Win16\n",
585 SpecName, Line );
586 return -1;
588 strcpy( odp->u.fwd.link_name, GetToken() );
589 return 0;
593 /*******************************************************************
594 * ParseOrdinal
596 * Parse an ordinal definition.
598 static int ParseOrdinal(int ordinal)
600 ORDDEF *odp;
601 char *token;
603 if (ordinal >= MAX_ORDINALS)
605 fprintf(stderr, "%s:%d: Ordinal number too large\n", SpecName, Line );
606 return -1;
608 if (ordinal > Limit) Limit = ordinal;
609 if (ordinal < Base) Base = ordinal;
611 odp = &OrdinalDefinitions[ordinal];
612 if (!(token = GetToken()))
614 fprintf(stderr, "%s:%d: Expected type after ordinal\n", SpecName, Line);
615 return -1;
618 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
619 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
620 break;
622 if (odp->type >= TYPE_NBTYPES)
624 fprintf( stderr,
625 "%s:%d: Expected type after ordinal, found '%s' instead\n",
626 SpecName, Line, token );
627 return -1;
630 if (!(token = GetToken()))
632 fprintf( stderr, "%s:%d: Expected name after type\n", SpecName, Line );
633 return -1;
635 strcpy( odp->name, token );
636 odp->lineno = Line;
638 switch(odp->type)
640 case TYPE_BYTE:
641 case TYPE_WORD:
642 case TYPE_LONG:
643 return ParseVariable( odp );
644 case TYPE_PASCAL_16:
645 case TYPE_PASCAL:
646 case TYPE_REGISTER:
647 case TYPE_STDCALL:
648 case TYPE_VARARGS:
649 case TYPE_CDECL:
650 return ParseExportFunction( odp );
651 case TYPE_INTERRUPT:
652 return ParseInterrupt( odp );
653 case TYPE_ABS:
654 return ParseEquate( odp );
655 case TYPE_STUB:
656 return ParseStub( odp );
657 case TYPE_EXTERN:
658 return ParseExtern( odp );
659 case TYPE_FORWARD:
660 return ParseForward( odp );
661 default:
662 fprintf( stderr, "Should not happen\n" );
663 return -1;
668 /*******************************************************************
669 * ParseTopLevel
671 * Parse a spec file.
673 static int ParseTopLevel(void)
675 char *token;
677 while ((token = GetToken()) != NULL)
679 if (strcmp(token, "name") == 0)
681 strcpy(DLLName, GetToken());
682 strupper(DLLName);
683 if (!DLLFileName[0]) sprintf( DLLFileName, "%s.DLL", DLLName );
685 else if (strcmp(token, "file") == 0)
687 strcpy(DLLFileName, GetToken());
688 strupper(DLLFileName);
690 else if (strcmp(token, "type") == 0)
692 token = GetToken();
693 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
694 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
695 else
697 fprintf(stderr, "%s:%d: Type must be 'win16' or 'win32'\n",
698 SpecName, Line);
699 return -1;
702 else if (strcmp(token, "heap") == 0)
704 token = GetToken();
705 if (!IsNumberString(token))
707 fprintf(stderr, "%s:%d: Expected number after heap\n",
708 SpecName, Line);
709 return -1;
711 DLLHeapSize = atoi(token);
713 else if (strcmp(token, "init") == 0)
715 strcpy(DLLInitFunc, GetToken());
716 if (SpecType == SPEC_WIN16)
718 fprintf(stderr, "%s:%d: init cannot be used for Win16 spec files\n",
719 SpecName, Line);
720 return -1;
722 if (!DLLInitFunc[0])
724 fprintf(stderr, "%s:%d: Expected function name after init\n", SpecName, Line);
725 return -1;
728 else if (strcmp(token, "import") == 0)
730 if (nb_imports >= MAX_IMPORTS)
732 fprintf( stderr, "%s:%d: Too many imports (limit %d)\n",
733 SpecName, Line, MAX_IMPORTS );
734 return -1;
736 if (SpecType != SPEC_WIN32)
738 fprintf( stderr, "%s:%d: Imports not supported for Win16\n", SpecName, Line );
739 return -1;
741 DLLImports[nb_imports++] = xstrdup(GetToken());
743 else if (IsNumberString(token))
745 int ordinal;
746 int rv;
748 ordinal = atoi(token);
749 if ((rv = ParseOrdinal(ordinal)) < 0)
750 return rv;
752 else
754 fprintf(stderr,
755 "%s:%d: Expected name, id, length or ordinal\n",
756 SpecName, Line);
757 return -1;
761 return 0;
765 /*******************************************************************
766 * StoreVariableCode
768 * Store a list of ints into a byte array.
770 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
772 int i;
774 switch(size)
776 case 1:
777 for (i = 0; i < odp->u.var.n_values; i++)
778 buffer[i] = odp->u.var.values[i];
779 break;
780 case 2:
781 for (i = 0; i < odp->u.var.n_values; i++)
782 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
783 break;
784 case 4:
785 for (i = 0; i < odp->u.var.n_values; i++)
786 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
787 break;
789 return odp->u.var.n_values * size;
793 /*******************************************************************
794 * DumpBytes
796 * Dump a byte stream into the assembly code.
798 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
799 const char *label )
801 int i;
803 fprintf( outfile, "\nstatic BYTE %s[] = \n{", label );
805 for (i = 0; i < len; i++)
807 if (!(i & 0x0f)) fprintf( outfile, "\n " );
808 fprintf( outfile, "%d", *data++ );
809 if (i < len - 1) fprintf( outfile, ", " );
811 fprintf( outfile, "\n};\n" );
815 /*******************************************************************
816 * BuildModule16
818 * Build the in-memory representation of a 16-bit NE module, and dump it
819 * as a byte stream into the assembly code.
821 static int BuildModule16( FILE *outfile, int max_code_offset,
822 int max_data_offset )
824 ORDDEF *odp;
825 int i;
826 char *buffer;
827 NE_MODULE *pModule;
828 SEGTABLEENTRY *pSegment;
829 OFSTRUCT *pFileInfo;
830 BYTE *pstr;
831 WORD *pword;
832 ET_BUNDLE *bundle = 0;
833 ET_ENTRY *entry = 0;
835 /* Module layout:
836 * NE_MODULE Module
837 * OFSTRUCT File information
838 * SEGTABLEENTRY Segment 1 (code)
839 * SEGTABLEENTRY Segment 2 (data)
840 * WORD[2] Resource table (empty)
841 * BYTE[2] Imported names (empty)
842 * BYTE[n] Resident names table
843 * BYTE[n] Entry table
846 buffer = xmalloc( 0x10000 );
848 pModule = (NE_MODULE *)buffer;
849 memset( pModule, 0, sizeof(*pModule) );
850 pModule->magic = IMAGE_OS2_SIGNATURE;
851 pModule->count = 1;
852 pModule->next = 0;
853 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
854 pModule->dgroup = 2;
855 pModule->heap_size = DLLHeapSize;
856 pModule->stack_size = 0;
857 pModule->ip = 0;
858 pModule->cs = 0;
859 pModule->sp = 0;
860 pModule->ss = 0;
861 pModule->seg_count = 2;
862 pModule->modref_count = 0;
863 pModule->nrname_size = 0;
864 pModule->modref_table = 0;
865 pModule->nrname_fpos = 0;
866 pModule->moveable_entries = 0;
867 pModule->alignment = 0;
868 pModule->truetype = 0;
869 pModule->os_flags = NE_OSFLAGS_WINDOWS;
870 pModule->misc_flags = 0;
871 pModule->dlls_to_init = 0;
872 pModule->nrname_handle = 0;
873 pModule->min_swap_area = 0;
874 pModule->expected_version = 0x030a;
875 pModule->module32 = 0;
876 pModule->self = 0;
877 pModule->self_loading_sel = 0;
879 /* File information */
881 pFileInfo = (OFSTRUCT *)(pModule + 1);
882 pModule->fileinfo = (int)pFileInfo - (int)pModule;
883 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
884 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
885 + strlen(DLLFileName);
886 strcpy( pFileInfo->szPathName, DLLFileName );
887 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
889 #ifdef __i386__ /* FIXME: Alignment problems! */
891 /* Segment table */
893 pSegment = (SEGTABLEENTRY *)pstr;
894 pModule->seg_table = (int)pSegment - (int)pModule;
895 pSegment->filepos = 0;
896 pSegment->size = max_code_offset;
897 pSegment->flags = 0;
898 pSegment->minsize = max_code_offset;
899 pSegment->hSeg = 0;
900 pSegment++;
902 pModule->dgroup_entry = (int)pSegment - (int)pModule;
903 pSegment->filepos = 0;
904 pSegment->size = max_data_offset;
905 pSegment->flags = NE_SEGFLAGS_DATA;
906 pSegment->minsize = max_data_offset;
907 pSegment->hSeg = 0;
908 pSegment++;
910 /* Resource table */
912 pword = (WORD *)pSegment;
913 pModule->res_table = (int)pword - (int)pModule;
914 *pword++ = 0;
915 *pword++ = 0;
917 /* Imported names table */
919 pstr = (char *)pword;
920 pModule->import_table = (int)pstr - (int)pModule;
921 *pstr++ = 0;
922 *pstr++ = 0;
924 /* Resident names table */
926 pModule->name_table = (int)pstr - (int)pModule;
927 /* First entry is module name */
928 *pstr = strlen(DLLName );
929 strcpy( pstr + 1, DLLName );
930 pstr += *pstr + 1;
931 *(WORD *)pstr = 0;
932 pstr += sizeof(WORD);
933 /* Store all ordinals */
934 odp = OrdinalDefinitions + 1;
935 for (i = 1; i <= Limit; i++, odp++)
937 if (!odp->name[0]) continue;
938 *pstr = strlen( odp->name );
939 strcpy( pstr + 1, odp->name );
940 strupper( pstr + 1 );
941 pstr += *pstr + 1;
942 *(WORD *)pstr = i;
943 pstr += sizeof(WORD);
945 *pstr++ = 0;
947 /* Entry table */
949 pModule->entry_table = (int)pstr - (int)pModule;
950 odp = OrdinalDefinitions + 1;
951 for (i = 1; i <= Limit; i++, odp++)
953 int selector = 0;
955 switch (odp->type)
957 case TYPE_CDECL:
958 case TYPE_PASCAL:
959 case TYPE_PASCAL_16:
960 case TYPE_REGISTER:
961 case TYPE_INTERRUPT:
962 case TYPE_STUB:
963 selector = 1; /* Code selector */
964 break;
966 case TYPE_BYTE:
967 case TYPE_WORD:
968 case TYPE_LONG:
969 selector = 2; /* Data selector */
970 break;
972 case TYPE_ABS:
973 selector = 0xfe; /* Constant selector */
974 break;
976 default:
977 selector = 0; /* Invalid selector */
978 break;
981 if ( !selector )
982 continue;
984 if ( bundle && bundle->last+1 == i )
985 bundle->last++;
986 else
988 if ( bundle )
989 bundle->next = (char *)pstr - (char *)pModule;
991 bundle = (ET_BUNDLE *)pstr;
992 bundle->first = i-1;
993 bundle->last = i;
994 bundle->next = 0;
995 pstr += sizeof(ET_BUNDLE);
998 /* FIXME: is this really correct ?? */
999 entry = (ET_ENTRY *)pstr;
1000 entry->type = 0xff; /* movable */
1001 entry->flags = 3; /* exported & public data */
1002 entry->segnum = selector;
1003 entry->offs = odp->offset;
1004 pstr += sizeof(ET_ENTRY);
1006 *pstr++ = 0;
1007 #endif
1009 /* Dump the module content */
1011 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
1012 "Module" );
1013 return (int)pstr - (int)pModule;
1017 /*******************************************************************
1018 * BuildSpec32File
1020 * Build a Win32 C file from a spec file.
1022 static int BuildSpec32File( char * specfile, FILE *outfile )
1024 ORDDEF *odp;
1025 int i, nb_names, fwd_size = 0;
1027 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
1028 specfile );
1029 fprintf( outfile, "#include \"builtin32.h\"\n\n" );
1031 /* Output code for all stubs functions */
1033 fprintf( outfile, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
1034 DLLName );
1035 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1037 if (odp->type != TYPE_STUB) continue;
1038 fprintf( outfile, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
1039 i, DLLName, i );
1042 /* Output code for all register functions */
1044 fprintf( outfile, "#ifdef __i386__\n" );
1045 fprintf( outfile, "#ifndef __GNUC__\n" );
1046 fprintf( outfile, "static void __asm__dummy() {\n" );
1047 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
1048 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1050 if (odp->type != TYPE_REGISTER) continue;
1051 fprintf( outfile,
1052 "__asm__(\".align 4\\n\\t\"\n"
1053 " \".globl " PREFIX "%s\\n\\t\"\n"
1054 " \".type " PREFIX "%s,@function\\n\\t\"\n"
1055 " \"" PREFIX "%s:\\n\\t\"\n"
1056 " \"call " PREFIX "CALL32_Regs\\n\\t\"\n"
1057 " \".long " PREFIX "__regs_%s\\n\\t\"\n"
1058 " \".byte %d,%d\");\n",
1059 odp->u.func.link_name, odp->u.func.link_name,
1060 odp->u.func.link_name, odp->u.func.link_name,
1061 4 * strlen(odp->u.func.arg_types),
1062 4 * strlen(odp->u.func.arg_types) );
1064 fprintf( outfile, "#ifndef __GNUC__\n" );
1065 fprintf( outfile, "}\n" );
1066 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
1067 fprintf( outfile, "#endif /* defined(__i386__) */\n" );
1069 /* Output the DLL functions prototypes */
1071 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1073 switch(odp->type)
1075 case TYPE_EXTERN:
1076 fprintf( outfile, "extern void %s();\n", odp->u.ext.link_name );
1077 break;
1078 case TYPE_REGISTER:
1079 case TYPE_STDCALL:
1080 case TYPE_VARARGS:
1081 case TYPE_CDECL:
1082 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1083 break;
1084 case TYPE_FORWARD:
1085 fwd_size += strlen(odp->u.fwd.link_name) + 1;
1086 break;
1087 case TYPE_INVALID:
1088 case TYPE_STUB:
1089 break;
1090 default:
1091 fprintf(stderr,"build: function type %d not available for Win32\n",
1092 odp->type);
1093 return -1;
1097 /* Output LibMain function */
1098 if (DLLInitFunc[0]) fprintf( outfile, "extern void %s();\n", DLLInitFunc );
1101 /* Output the DLL functions table */
1103 fprintf( outfile, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1104 Limit - Base + 1 );
1105 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1107 switch(odp->type)
1109 case TYPE_INVALID:
1110 fprintf( outfile, " 0" );
1111 break;
1112 case TYPE_EXTERN:
1113 fprintf( outfile, " %s", odp->u.ext.link_name );
1114 break;
1115 case TYPE_REGISTER:
1116 case TYPE_STDCALL:
1117 case TYPE_VARARGS:
1118 case TYPE_CDECL:
1119 fprintf( outfile, " %s", odp->u.func.link_name);
1120 break;
1121 case TYPE_STUB:
1122 fprintf( outfile, " __stub_%d", i );
1123 break;
1124 case TYPE_FORWARD:
1125 fprintf( outfile, " (ENTRYPOINT32)\"%s\"", odp->u.fwd.link_name );
1126 break;
1127 default:
1128 return -1;
1130 if (i < Limit) fprintf( outfile, ",\n" );
1132 fprintf( outfile, "\n};\n\n" );
1134 /* Output the DLL names table */
1136 nb_names = 0;
1137 fprintf( outfile, "static const char * const FuncNames[] =\n{\n" );
1138 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1140 if (odp->type == TYPE_INVALID) continue;
1141 if (nb_names++) fprintf( outfile, ",\n" );
1142 fprintf( outfile, " \"%s\"", odp->name );
1144 fprintf( outfile, "\n};\n\n" );
1146 /* Output the DLL argument types */
1148 fprintf( outfile, "static const unsigned int ArgTypes[%d] =\n{\n",
1149 Limit - Base + 1 );
1150 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1152 unsigned int j, mask = 0;
1153 if ((odp->type == TYPE_STDCALL) || (odp->type == TYPE_CDECL) ||
1154 (odp->type == TYPE_REGISTER))
1155 for (j = 0; odp->u.func.arg_types[j]; j++)
1157 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
1158 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
1160 fprintf( outfile, " %d", mask );
1161 if (i < Limit) fprintf( outfile, ",\n" );
1163 fprintf( outfile, "\n};\n\n" );
1165 /* Output the DLL ordinals table */
1167 fprintf( outfile, "static const unsigned short FuncOrdinals[] =\n{\n" );
1168 nb_names = 0;
1169 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1171 if (odp->type == TYPE_INVALID) continue;
1172 if (nb_names++) fprintf( outfile, ",\n" );
1173 fprintf( outfile, " %d", i - Base );
1175 fprintf( outfile, "\n};\n\n" );
1177 /* Output the DLL functions arguments */
1179 fprintf( outfile, "static const unsigned char FuncArgs[%d] =\n{\n",
1180 Limit - Base + 1 );
1181 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1183 unsigned char args;
1184 switch(odp->type)
1186 case TYPE_STDCALL:
1187 args = (unsigned char)strlen(odp->u.func.arg_types);
1188 break;
1189 case TYPE_CDECL:
1190 args = 0x80 | (unsigned char)strlen(odp->u.func.arg_types);
1191 break;
1192 case TYPE_REGISTER:
1193 args = 0x40 | (unsigned char)strlen(odp->u.func.arg_types);
1194 break;
1195 case TYPE_FORWARD:
1196 args = 0xfd;
1197 break;
1198 default:
1199 args = 0xff;
1200 break;
1202 fprintf( outfile, " 0x%02x", args );
1203 if (i < Limit) fprintf( outfile, ",\n" );
1205 fprintf( outfile, "\n};\n\n" );
1207 /* Output the DLL imports */
1209 if (nb_imports)
1211 fprintf( outfile, "static const char * const Imports[%d] =\n{\n", nb_imports );
1212 for (i = 0; i < nb_imports; i++)
1214 fprintf( outfile, " \"%s\"", DLLImports[i] );
1215 if (i < nb_imports-1) fprintf( outfile, ",\n" );
1217 fprintf( outfile, "\n};\n\n" );
1220 /* Output the DLL descriptor */
1222 fprintf( outfile, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1223 DLLName );
1224 fprintf( outfile, " \"%s\",\n", DLLName );
1225 fprintf( outfile, " \"%s\",\n", DLLFileName );
1226 fprintf( outfile, " %d,\n", Base );
1227 fprintf( outfile, " %d,\n", Limit - Base + 1 );
1228 fprintf( outfile, " %d,\n", nb_names );
1229 fprintf( outfile, " %d,\n", nb_imports );
1230 fprintf( outfile, " %d,\n", (fwd_size + 3) & ~3 );
1231 fprintf( outfile,
1232 " Functions,\n"
1233 " FuncNames,\n"
1234 " FuncOrdinals,\n"
1235 " FuncArgs,\n"
1236 " ArgTypes,\n");
1237 fprintf( outfile, " %s,\n", nb_imports ? "Imports" : "0" );
1238 fprintf( outfile, " %s\n", DLLInitFunc[0] ? DLLInitFunc : "0" );
1239 fprintf( outfile, "};\n" );
1240 return 0;
1243 /*******************************************************************
1244 * Spec16TypeCompare
1246 static int Spec16TypeCompare( const void *e1, const void *e2 )
1248 const ORDDEF *odp1 = *(const ORDDEF **)e1;
1249 const ORDDEF *odp2 = *(const ORDDEF **)e2;
1251 int type1 = (odp1->type == TYPE_CDECL) ? 0
1252 : (odp1->type == TYPE_REGISTER) ? 3
1253 : (odp1->type == TYPE_INTERRUPT) ? 4
1254 : (odp1->type == TYPE_PASCAL_16) ? 1 : 2;
1256 int type2 = (odp2->type == TYPE_CDECL) ? 0
1257 : (odp2->type == TYPE_REGISTER) ? 3
1258 : (odp2->type == TYPE_INTERRUPT) ? 4
1259 : (odp2->type == TYPE_PASCAL_16) ? 1 : 2;
1261 int retval = type1 - type2;
1262 if ( !retval )
1263 retval = strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
1265 return retval;
1268 /*******************************************************************
1269 * BuildSpec16File
1271 * Build a Win16 assembly file from a spec file.
1273 static int BuildSpec16File( char * specfile, FILE *outfile )
1275 ORDDEF *odp, **type, **typelist;
1276 int i, nFuncs, nTypes;
1277 int code_offset, data_offset, module_size;
1278 unsigned char *data;
1280 /* File header */
1282 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
1283 specfile );
1284 fprintf( outfile, "#define __FLATCS__ 0x%04x\n", Code_Selector );
1285 fprintf( outfile, "#include \"builtin16.h\"\n\n" );
1287 data = (unsigned char *)xmalloc( 0x10000 );
1288 memset( data, 0, 16 );
1289 data_offset = 16;
1292 /* Build sorted list of all argument types, without duplicates */
1294 typelist = (ORDDEF **)calloc( Limit+1, sizeof(ORDDEF *) );
1296 odp = OrdinalDefinitions;
1297 for (i = nFuncs = 0; i <= Limit; i++, odp++)
1299 switch (odp->type)
1301 case TYPE_REGISTER:
1302 case TYPE_INTERRUPT:
1303 case TYPE_CDECL:
1304 case TYPE_PASCAL:
1305 case TYPE_PASCAL_16:
1306 case TYPE_STUB:
1307 typelist[nFuncs++] = odp;
1309 default:
1310 break;
1314 qsort( typelist, nFuncs, sizeof(ORDDEF *), Spec16TypeCompare );
1316 i = nTypes = 0;
1317 while ( i < nFuncs )
1319 typelist[nTypes++] = typelist[i++];
1320 while ( i < nFuncs && Spec16TypeCompare( typelist + i, typelist + nTypes-1 ) == 0 )
1321 i++;
1324 /* Output CallFrom16 routines needed by this .spec file */
1326 for ( i = 0; i < nTypes; i++ )
1328 char profile[101];
1330 sprintf( profile, "%s_%s_%s",
1331 (typelist[i]->type == TYPE_CDECL) ? "c" : "p",
1332 (typelist[i]->type == TYPE_REGISTER) ? "regs" :
1333 (typelist[i]->type == TYPE_INTERRUPT) ? "intr" :
1334 (typelist[i]->type == TYPE_PASCAL_16) ? "word" : "long",
1335 typelist[i]->u.func.arg_types );
1337 BuildCallFrom16Func( outfile, profile, DLLName, TRUE );
1340 /* Output the DLL functions prototypes */
1342 odp = OrdinalDefinitions;
1343 for (i = 0; i <= Limit; i++, odp++)
1345 switch(odp->type)
1347 case TYPE_REGISTER:
1348 case TYPE_INTERRUPT:
1349 case TYPE_CDECL:
1350 case TYPE_PASCAL:
1351 case TYPE_PASCAL_16:
1352 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1353 break;
1354 default:
1355 break;
1359 /* Output code segment */
1361 fprintf( outfile, "\nstatic struct\n{\n CALLFROM16 call[%d];\n"
1362 " ENTRYPOINT16 entry[%d];\n} Code_Segment = \n{\n {\n",
1363 nTypes, nFuncs );
1364 code_offset = 0;
1366 for ( i = 0; i < nTypes; i++ )
1368 char profile[101], *arg;
1369 int argsize = 0;
1371 sprintf( profile, "%s_%s_%s",
1372 (typelist[i]->type == TYPE_CDECL) ? "c" : "p",
1373 (typelist[i]->type == TYPE_REGISTER) ? "regs" :
1374 (typelist[i]->type == TYPE_INTERRUPT) ? "intr" :
1375 (typelist[i]->type == TYPE_PASCAL_16) ? "word" : "long",
1376 typelist[i]->u.func.arg_types );
1378 if ( typelist[i]->type != TYPE_CDECL )
1379 for ( arg = typelist[i]->u.func.arg_types; *arg; arg++ )
1380 switch ( *arg )
1382 case 'w': /* word */
1383 case 's': /* s_word */
1384 argsize += 2;
1385 break;
1387 case 'l': /* long or segmented pointer */
1388 case 'T': /* segmented pointer to null-terminated string */
1389 case 'p': /* linear pointer */
1390 case 't': /* linear pointer to null-terminated string */
1391 argsize += 4;
1392 break;
1395 if ( typelist[i]->type == TYPE_INTERRUPT )
1396 argsize += 2;
1398 fprintf( outfile, " CF16_%s( %s_CallFrom16_%s, %d, \"%s\" ),\n",
1399 ( typelist[i]->type == TYPE_REGISTER
1400 || typelist[i]->type == TYPE_INTERRUPT)? "REGS":
1401 typelist[i]->type == TYPE_PASCAL_16? "WORD" : "LONG",
1402 DLLName, profile, argsize, profile );
1404 code_offset += sizeof(CALLFROM16);
1406 fprintf( outfile, " },\n {\n" );
1408 odp = OrdinalDefinitions;
1409 for (i = 0; i <= Limit; i++, odp++)
1411 switch (odp->type)
1413 case TYPE_INVALID:
1414 odp->offset = 0xffff;
1415 break;
1417 case TYPE_ABS:
1418 odp->offset = LOWORD(odp->u.abs.value);
1419 break;
1421 case TYPE_BYTE:
1422 odp->offset = data_offset;
1423 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1424 break;
1426 case TYPE_WORD:
1427 odp->offset = data_offset;
1428 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1429 break;
1431 case TYPE_LONG:
1432 odp->offset = data_offset;
1433 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1434 break;
1436 case TYPE_REGISTER:
1437 case TYPE_INTERRUPT:
1438 case TYPE_CDECL:
1439 case TYPE_PASCAL:
1440 case TYPE_PASCAL_16:
1441 case TYPE_STUB:
1442 type = bsearch( &odp, typelist, nTypes, sizeof(ORDDEF *), Spec16TypeCompare );
1443 assert( type );
1445 fprintf( outfile, " /* %s.%d */ ", DLLName, i );
1446 fprintf( outfile, "EP( %s, %d /* %s_%s_%s */ ),\n",
1447 odp->u.func.link_name,
1448 (type-typelist)*sizeof(CALLFROM16) -
1449 (code_offset + sizeof(ENTRYPOINT16)),
1450 (odp->type == TYPE_CDECL) ? "c" : "p",
1451 (odp->type == TYPE_REGISTER) ? "regs" :
1452 (odp->type == TYPE_INTERRUPT) ? "intr" :
1453 (odp->type == TYPE_PASCAL_16) ? "word" : "long",
1454 odp->u.func.arg_types );
1456 odp->offset = code_offset;
1457 code_offset += sizeof(ENTRYPOINT16);
1458 break;
1460 default:
1461 fprintf(stderr,"build: function type %d not available for Win16\n",
1462 odp->type);
1463 return -1;
1467 fprintf( outfile, " }\n};\n" );
1469 /* Output data segment */
1471 DumpBytes( outfile, data, data_offset, "Data_Segment" );
1473 /* Build the module */
1475 module_size = BuildModule16( outfile, code_offset, data_offset );
1477 /* Output the DLL descriptor */
1479 fprintf( outfile, "\nWIN16_DESCRIPTOR %s_Descriptor = \n{\n", DLLName );
1480 fprintf( outfile, " \"%s\",\n", DLLName );
1481 fprintf( outfile, " Module,\n" );
1482 fprintf( outfile, " sizeof(Module),\n" );
1483 fprintf( outfile, " (BYTE *)&Code_Segment,\n" );
1484 fprintf( outfile, " (BYTE *)Data_Segment\n" );
1485 fprintf( outfile, "};\n" );
1487 return 0;
1491 /*******************************************************************
1492 * BuildSpecFile
1494 * Build an assembly file from a spec file.
1496 static int BuildSpecFile( FILE *outfile, char *specname )
1498 SpecName = specname;
1499 SpecFp = fopen( specname, "r");
1500 if (SpecFp == NULL)
1502 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1503 return -1;
1506 if (ParseTopLevel() < 0) return -1;
1508 switch(SpecType)
1510 case SPEC_WIN16:
1511 return BuildSpec16File( specname, outfile );
1512 case SPEC_WIN32:
1513 return BuildSpec32File( specname, outfile );
1514 default:
1515 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1516 return -1;
1521 /*******************************************************************
1522 * BuildCallFrom16Func
1524 * Build a 16-bit-to-Wine callback glue function.
1526 * The generated routines are intended to be used as argument conversion
1527 * routines to be called by the CallFrom16... core. Thus, the prototypes of
1528 * the generated routines are (see also CallFrom16):
1530 * extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
1531 * extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
1532 * extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args,
1533 * CONTEXT86 *context );
1534 * extern void WINAPI PREFIX_CallFrom16_C_intr_xxx( FARPROC func, LPBYTE args,
1535 * CONTEXT86 *context );
1537 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
1538 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
1539 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1540 * 'T'=segmented pointer to null-terminated string).
1542 * The generated routines fetch the arguments from the 16-bit stack (pointed
1543 * to by 'args'); the offsets of the single argument values are computed
1544 * according to the calling convention and the argument types. Then, the
1545 * 32-bit entry point is called with these arguments.
1547 * For register functions, the arguments (if present) are converted just
1548 * the same as for normal functions, but in addition the CONTEXT86 pointer
1549 * filled with the current register values is passed to the 32-bit routine.
1550 * (An 'intr' interrupt handler routine is treated exactly like a register
1551 * routine, except that upon return, the flags word pushed onto the stack
1552 * by the interrupt is removed by the 16-bit call stub.)
1555 static void BuildCallFrom16Func( FILE *outfile, char *profile, char *prefix, int local )
1557 int i, pos, argsize = 0;
1558 int short_ret = 0;
1559 int reg_func = 0;
1560 int usecdecl = 0;
1561 char *args = profile + 7;
1562 char *ret_type;
1564 /* Parse function type */
1566 if (!strncmp( "c_", profile, 2 )) usecdecl = 1;
1567 else if (strncmp( "p_", profile, 2 ))
1569 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1570 return;
1573 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
1574 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
1575 else if (!strncmp( "intr_", profile + 2, 5 )) reg_func = 2;
1576 else if (strncmp( "long_", profile + 2, 5 ))
1578 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1579 return;
1582 for ( i = 0; args[i]; i++ )
1583 switch ( args[i] )
1585 case 'w': /* word */
1586 case 's': /* s_word */
1587 argsize += 2;
1588 break;
1590 case 'l': /* long or segmented pointer */
1591 case 'T': /* segmented pointer to null-terminated string */
1592 case 'p': /* linear pointer */
1593 case 't': /* linear pointer to null-terminated string */
1594 argsize += 4;
1595 break;
1598 ret_type = reg_func? "void" : short_ret? "WORD" : "LONG";
1600 fprintf( outfile, "typedef %s WINAPI (*proc_%s_t)( ",
1601 ret_type, profile );
1602 args = profile + 7;
1603 for ( i = 0; args[i]; i++ )
1605 if ( i ) fprintf( outfile, ", " );
1606 switch (args[i])
1608 case 'w': fprintf( outfile, "WORD" ); break;
1609 case 's': fprintf( outfile, "INT16" ); break;
1610 case 'l': case 'T': fprintf( outfile, "LONG" ); break;
1611 case 'p': case 't': fprintf( outfile, "LPVOID" ); break;
1614 if ( reg_func )
1615 fprintf( outfile, "%sstruct _CONTEXT86 *", i? ", " : "" );
1616 else if ( !i )
1617 fprintf( outfile, "void" );
1618 fprintf( outfile, " );\n" );
1620 fprintf( outfile, "%s%s WINAPI %s_CallFrom16_%s( FARPROC proc, LPBYTE args%s )\n{\n",
1621 local? "static " : "", ret_type, prefix, profile,
1622 reg_func? ", struct _CONTEXT86 *context" : "" );
1624 fprintf( outfile, " %s((proc_%s_t) proc) (\n",
1625 reg_func? "" : "return ", profile );
1626 args = profile + 7;
1627 pos = !usecdecl? argsize : 0;
1628 for ( i = 0; args[i]; i++ )
1630 if ( i ) fprintf( outfile, ",\n" );
1631 fprintf( outfile, " " );
1632 switch (args[i])
1634 case 'w': /* word */
1635 if ( !usecdecl ) pos -= 2;
1636 fprintf( outfile, "*(WORD *)(args+%d)", pos );
1637 if ( usecdecl ) pos += 2;
1638 break;
1640 case 's': /* s_word */
1641 if ( !usecdecl ) pos -= 2;
1642 fprintf( outfile, "*(INT16 *)(args+%d)", pos );
1643 if ( usecdecl ) pos += 2;
1644 break;
1646 case 'l': /* long or segmented pointer */
1647 case 'T': /* segmented pointer to null-terminated string */
1648 if ( !usecdecl ) pos -= 4;
1649 fprintf( outfile, "*(LONG *)(args+%d)", pos );
1650 if ( usecdecl ) pos += 4;
1651 break;
1653 case 'p': /* linear pointer */
1654 case 't': /* linear pointer to null-terminated string */
1655 if ( !usecdecl ) pos -= 4;
1656 fprintf( outfile, "PTR_SEG_TO_LIN( *(SEGPTR *)(args+%d) )", pos );
1657 if ( usecdecl ) pos += 4;
1658 break;
1660 default:
1661 fprintf( stderr, "Unknown arg type '%c'\n", args[i] );
1664 if ( reg_func )
1665 fprintf( outfile, "%s context", i? ",\n" : "" );
1666 fprintf( outfile, " );\n}\n\n" );
1669 /*******************************************************************
1670 * BuildCallTo16Func
1672 * Build a Wine-to-16-bit callback glue function.
1674 * Prototypes for the CallTo16 functions:
1675 * extern WORD CALLBACK PREFIX_CallTo16_word_xxx( FARPROC16 func, args... );
1676 * extern LONG CALLBACK PREFIX_CallTo16_long_xxx( FARPROC16 func, args... );
1678 * These routines are provided solely for convenience; they simply
1679 * write the arguments onto the 16-bit stack, and call the appropriate
1680 * CallTo16... core routine.
1682 * If you have more sophisticated argument conversion requirements than
1683 * are provided by these routines, you might as well call the core
1684 * routines by yourself.
1687 static void BuildCallTo16Func( FILE *outfile, char *profile, char *prefix )
1689 char *args = profile + 5;
1690 int i, argsize = 0, short_ret = 0;
1692 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1693 else if (strncmp( "long_", profile, 5 ))
1695 fprintf( stderr, "Invalid function name '%s'.\n", profile );
1696 exit(1);
1699 fprintf( outfile, "%s %s_CallTo16_%s( FARPROC16 proc",
1700 short_ret? "WORD" : "LONG", prefix, profile );
1701 args = profile + 5;
1702 for ( i = 0; args[i]; i++ )
1704 fprintf( outfile, ", " );
1705 switch (args[i])
1707 case 'w': fprintf( outfile, "WORD" ); argsize += 2; break;
1708 case 'l': fprintf( outfile, "LONG" ); argsize += 4; break;
1710 fprintf( outfile, " arg%d", i+1 );
1712 fprintf( outfile, " )\n{\n" );
1714 if ( argsize > 0 )
1715 fprintf( outfile, " LPBYTE args = (LPBYTE)CURRENT_STACK16;\n" );
1717 args = profile + 5;
1718 for ( i = 0; args[i]; i++ )
1720 switch (args[i])
1722 case 'w': fprintf( outfile, " args -= sizeof(WORD); *(WORD" ); break;
1723 case 'l': fprintf( outfile, " args -= sizeof(LONG); *(LONG" ); break;
1724 default: fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
1725 args[i] );
1727 fprintf( outfile, " *)args = arg%d;\n", i+1 );
1730 fprintf( outfile, " return CallTo16%s( proc, %d );\n}\n\n",
1731 short_ret? "Word" : "Long", argsize );
1736 /*******************************************************************
1737 * BuildCallFrom16Core
1739 * This routine builds the core routines used in 16->32 thunks:
1740 * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
1742 * These routines are intended to be called via a far call (with 32-bit
1743 * operand size) from 16-bit code. The 16-bit code stub must push %bp,
1744 * the 32-bit entry point to be called, and the argument conversion
1745 * routine to be used (see stack layout below).
1747 * The core routine completes the STACK16FRAME on the 16-bit stack and
1748 * switches to the 32-bit stack. Then, the argument conversion routine
1749 * is called; it gets passed the 32-bit entry point and a pointer to the
1750 * 16-bit arguments (on the 16-bit stack) as parameters. (You can either
1751 * use conversion routines automatically generated by BuildCallFrom16,
1752 * or write your own for special purposes.)
1754 * The conversion routine must call the 32-bit entry point, passing it
1755 * the converted arguments, and return its return value to the core.
1756 * After the conversion routine has returned, the core switches back
1757 * to the 16-bit stack, converts the return value to the DX:AX format
1758 * (CallFrom16Long), and returns to the 16-bit call stub. All parameters,
1759 * including %bp, are popped off the stack.
1761 * The 16-bit call stub now returns to the caller, popping the 16-bit
1762 * arguments if necessary (pascal calling convention).
1764 * In the case of a 'register' function, CallFrom16Register fills a
1765 * CONTEXT86 structure with the values all registers had at the point
1766 * the first instruction of the 16-bit call stub was about to be
1767 * executed. A pointer to this CONTEXT86 is passed as third parameter
1768 * to the argument conversion routine, which typically passes it on
1769 * to the called 32-bit entry point.
1771 * CallFrom16Thunk is a special variant used by the implementation of
1772 * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
1773 * implemented as follows:
1774 * On entry, the EBX register is set up to contain a flat pointer to the
1775 * 16-bit stack such that EBX+22 points to the first argument.
1776 * Then, the entry point is called, while EBP is set up to point
1777 * to the return address (on the 32-bit stack).
1778 * The called function returns with CX set to the number of bytes
1779 * to be popped of the caller's stack.
1781 * Stack layout upon entry to the core routine (STACK16FRAME):
1782 * ... ...
1783 * (sp+24) word first 16-bit arg
1784 * (sp+22) word cs
1785 * (sp+20) word ip
1786 * (sp+18) word bp
1787 * (sp+14) long 32-bit entry point (reused for Win16 mutex recursion count)
1788 * (sp+12) word ip of actual entry point (necessary for relay debugging)
1789 * (sp+8) long relay (argument conversion) function entry point
1790 * (sp+4) long cs of 16-bit entry point
1791 * (sp) long ip of 16-bit entry point
1793 * Added on the stack:
1794 * (sp-2) word saved gs
1795 * (sp-4) word saved fs
1796 * (sp-6) word saved es
1797 * (sp-8) word saved ds
1798 * (sp-12) long saved ebp
1799 * (sp-16) long saved ecx
1800 * (sp-20) long saved edx
1801 * (sp-24) long saved previous stack
1803 static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk, int short_ret )
1805 char *name = thunk? "Thunk" : reg_func? "Register" : short_ret? "Word" : "Long";
1807 /* Function header */
1808 fprintf( outfile, "\n\t.align 4\n" );
1809 #ifdef USE_STABS
1810 fprintf( outfile, ".stabs \"CallFrom16%s:F1\",36,0,0," PREFIX "CallFrom16%s\n",
1811 name, name);
1812 #endif
1813 fprintf( outfile, "\t.globl " PREFIX "CallFrom16%s\n", name );
1814 fprintf( outfile, PREFIX "CallFrom16%s:\n", name );
1816 /* Create STACK16FRAME (except STACK32FRAME link) */
1817 fprintf( outfile, "\tpushw %%gs\n" );
1818 fprintf( outfile, "\tpushw %%fs\n" );
1819 fprintf( outfile, "\tpushw %%es\n" );
1820 fprintf( outfile, "\tpushw %%ds\n" );
1821 fprintf( outfile, "\tpushl %%ebp\n" );
1822 fprintf( outfile, "\tpushl %%ecx\n" );
1823 fprintf( outfile, "\tpushl %%edx\n" );
1825 /* Save original EFlags register */
1826 fprintf( outfile, "\tpushfl\n" );
1828 if ( UsePIC )
1830 /* Get Global Offset Table into %ecx */
1831 fprintf( outfile, "\tcall .LCallFrom16%s.getgot1\n", name );
1832 fprintf( outfile, ".LCallFrom16%s.getgot1:\n", name );
1833 fprintf( outfile, "\tpopl %%ecx\n" );
1834 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot1], %%ecx\n", name );
1837 /* Load 32-bit segment registers */
1838 fprintf( outfile, "\tmovw $0x%04x, %%dx\n", Data_Selector );
1839 #ifdef __svr4__
1840 fprintf( outfile, "\tdata16\n");
1841 #endif
1842 fprintf( outfile, "\tmovw %%dx, %%ds\n" );
1843 #ifdef __svr4__
1844 fprintf( outfile, "\tdata16\n");
1845 #endif
1846 fprintf( outfile, "\tmovw %%dx, %%es\n" );
1848 if ( UsePIC )
1850 fprintf( outfile, "\tmovl " PREFIX "SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
1851 fprintf( outfile, "\tmovw (%%edx), %%fs\n" );
1853 else
1854 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb, %%fs\n" );
1856 /* Get address of ldt_copy array into %ecx */
1857 if ( UsePIC )
1858 fprintf( outfile, "\tmovl " PREFIX "ldt_copy@GOT(%%ecx), %%ecx\n" );
1859 else
1860 fprintf( outfile, "\tmovl $" PREFIX "ldt_copy, %%ecx\n" );
1862 /* Translate STACK16FRAME base to flat offset in %edx */
1863 fprintf( outfile, "\tmovw %%ss, %%dx\n" );
1864 fprintf( outfile, "\tandl $0xfff8, %%edx\n" );
1865 fprintf( outfile, "\tmovl (%%ecx,%%edx), %%edx\n" );
1866 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
1867 fprintf( outfile, "\tleal (%%ebp,%%edx), %%edx\n" );
1869 /* Get saved flags into %ecx */
1870 fprintf( outfile, "\tpopl %%ecx\n" );
1872 /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
1873 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET );
1874 fprintf( outfile, "\tpushl %%ebp\n" );
1876 /* Switch stacks */
1877 #ifdef __svr4__
1878 fprintf( outfile,"\tdata16\n");
1879 #endif
1880 fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET + 2 );
1881 fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET );
1882 fprintf( outfile, "\tpushl %%ds\n" );
1883 fprintf( outfile, "\tpopl %%ss\n" );
1884 fprintf( outfile, "\tmovl %%ebp, %%esp\n" );
1885 fprintf( outfile, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME, ebp) );
1888 /* At this point:
1889 STACK16FRAME is completely set up
1890 DS, ES, SS: flat data segment
1891 FS: current TEB
1892 ESP: points to last STACK32FRAME
1893 EBP: points to ebp member of last STACK32FRAME
1894 EDX: points to current STACK16FRAME
1895 ECX: contains saved flags
1896 all other registers: unchanged */
1898 /* Special case: C16ThkSL stub */
1899 if ( thunk )
1901 /* Set up registers as expected and call thunk */
1902 fprintf( outfile, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME)-22 );
1903 fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
1905 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point) );
1907 /* Switch stack back */
1908 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
1909 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
1910 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
1912 /* Restore registers and return directly to caller */
1913 fprintf( outfile, "\taddl $8, %%esp\n" );
1914 fprintf( outfile, "\tpopl %%ebp\n" );
1915 fprintf( outfile, "\tpopw %%ds\n" );
1916 fprintf( outfile, "\tpopw %%es\n" );
1917 fprintf( outfile, "\tpopw %%fs\n" );
1918 fprintf( outfile, "\tpopw %%gs\n" );
1919 fprintf( outfile, "\taddl $20, %%esp\n" );
1921 fprintf( outfile, "\txorb %%ch, %%ch\n" );
1922 fprintf( outfile, "\tpopl %%ebx\n" );
1923 fprintf( outfile, "\taddw %%cx, %%sp\n" );
1924 fprintf( outfile, "\tpush %%ebx\n" );
1926 fprintf( outfile, "\t.byte 0x66\n" );
1927 fprintf( outfile, "\tlret\n" );
1929 return;
1933 /* Build register CONTEXT */
1934 if ( reg_func )
1936 fprintf( outfile, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86) );
1938 fprintf( outfile, "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags) );
1940 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax) );
1941 fprintf( outfile, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx) );
1942 fprintf( outfile, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi) );
1943 fprintf( outfile, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi) );
1945 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp) );
1946 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp) );
1947 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx) );
1948 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx) );
1949 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx) );
1950 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx) );
1952 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds) );
1953 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs) );
1954 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es) );
1955 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs) );
1956 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs) );
1957 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs) );
1958 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs) );
1959 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs) );
1961 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs) );
1962 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs) );
1963 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip) );
1964 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip) );
1966 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET+2 );
1967 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs) );
1968 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET );
1969 fprintf( outfile, "\taddl $%d, %%eax\n", STACK16OFFSET(ip) );
1970 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp) );
1971 #if 0
1972 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
1973 #endif
1975 /* Push address of CONTEXT86 structure -- popped by the relay routine */
1976 fprintf( outfile, "\tpushl %%esp\n" );
1980 /* Print debug info before call */
1981 if ( debugging )
1983 if ( UsePIC )
1985 fprintf( outfile, "\tpushl %%ebx\n" );
1987 /* Get Global Offset Table into %ebx (for PLT call) */
1988 fprintf( outfile, "\tcall .LCallFrom16%s.getgot2\n", name );
1989 fprintf( outfile, ".LCallFrom16%s.getgot2:\n", name );
1990 fprintf( outfile, "\tpopl %%ebx\n" );
1991 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot2], %%ebx\n", name );
1994 fprintf( outfile, "\tpushl %%edx\n" );
1995 if ( reg_func )
1996 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
1997 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
1998 else
1999 fprintf( outfile, "\tpushl $0\n" );
2001 if ( UsePIC )
2002 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16@PLT\n ");
2003 else
2004 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n ");
2006 fprintf( outfile, "\tpopl %%edx\n" );
2007 fprintf( outfile, "\tpopl %%edx\n" );
2009 if ( UsePIC )
2010 fprintf( outfile, "\tpopl %%ebx\n" );
2013 /* Call relay routine (which will call the API entry point) */
2014 fprintf( outfile, "\tleal %d(%%edx), %%eax\n", sizeof(STACK16FRAME) );
2015 fprintf( outfile, "\tpushl %%eax\n" );
2016 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point) );
2017 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay) );
2019 /* Print debug info after call */
2020 if ( debugging )
2022 if ( UsePIC )
2024 fprintf( outfile, "\tpushl %%ebx\n" );
2026 /* Get Global Offset Table into %ebx (for PLT call) */
2027 fprintf( outfile, "\tcall .LCallFrom16%s.getgot3\n", name );
2028 fprintf( outfile, ".LCallFrom16%s.getgot3:\n", name );
2029 fprintf( outfile, "\tpopl %%ebx\n" );
2030 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot3], %%ebx\n", name );
2033 fprintf( outfile, "\tpushl %%eax\n" );
2034 if ( reg_func )
2035 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2036 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
2037 else
2038 fprintf( outfile, "\tpushl $0\n" );
2040 if ( UsePIC )
2041 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret@PLT\n ");
2042 else
2043 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n ");
2045 fprintf( outfile, "\tpopl %%eax\n" );
2046 fprintf( outfile, "\tpopl %%eax\n" );
2048 if ( UsePIC )
2049 fprintf( outfile, "\tpopl %%ebx\n" );
2053 if ( reg_func )
2055 fprintf( outfile, "\tmovl %%esp, %%ebx\n" );
2057 /* Switch stack back */
2058 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2059 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
2060 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2062 /* Get return address to CallFrom16 stub */
2063 fprintf( outfile, "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip)-4 );
2064 fprintf( outfile, "\tpopl %%eax\n" );
2065 fprintf( outfile, "\tpopl %%edx\n" );
2067 /* Restore all registers from CONTEXT */
2068 fprintf( outfile, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs) );
2069 fprintf( outfile, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp) );
2070 fprintf( outfile, "\taddl $4, %%esp\n" ); /* room for final return address */
2072 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
2073 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
2074 fprintf( outfile, "\tpushl %%edx\n" );
2075 fprintf( outfile, "\tpushl %%eax\n" );
2076 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags) );
2077 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
2079 fprintf( outfile, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs) );
2080 fprintf( outfile, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs) );
2081 fprintf( outfile, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs) );
2083 fprintf( outfile, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp) );
2084 fprintf( outfile, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi) );
2085 fprintf( outfile, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi) );
2086 fprintf( outfile, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax) );
2087 fprintf( outfile, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx) );
2088 fprintf( outfile, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx) );
2089 fprintf( outfile, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx) );
2091 fprintf( outfile, "\tpopl %%ds\n" );
2092 fprintf( outfile, "\tpopfl\n" );
2093 fprintf( outfile, "\tlret\n" );
2095 else
2097 /* Switch stack back */
2098 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2099 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
2100 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2102 /* Restore registers */
2103 fprintf( outfile, "\tpopl %%edx\n" );
2104 fprintf( outfile, "\tpopl %%ecx\n" );
2105 fprintf( outfile, "\tpopl %%ebp\n" );
2106 fprintf( outfile, "\tpopw %%ds\n" );
2107 fprintf( outfile, "\tpopw %%es\n" );
2108 fprintf( outfile, "\tpopw %%fs\n" );
2109 fprintf( outfile, "\tpopw %%gs\n" );
2111 /* Prepare return value and set flags accordingly */
2112 if ( !short_ret )
2113 fprintf( outfile, "\tshldl $16, %%eax, %%edx\n" );
2114 fprintf( outfile, "\torl %%eax, %%eax\n" );
2116 /* Return to return stub which will return to caller */
2117 fprintf( outfile, "\tlret $12\n" );
2122 /*******************************************************************
2123 * BuildCallTo16Core
2125 * This routine builds the core routines used in 32->16 thunks:
2127 * extern void CALLBACK CallTo16Word( SEGPTR target, int nb_args );
2128 * extern void CALLBACK CallTo16Long( SEGPTR target, int nb_args );
2129 * extern void CALLBACK CallTo16RegisterShort( const CONTEXT86 *context, int nb_args );
2130 * extern void CALLBACK CallTo16RegisterLong ( const CONTEXT86 *context, int nb_args );
2132 * These routines can be called directly from 32-bit code.
2134 * All routines expect that the 16-bit stack contents (arguments) were
2135 * already set up by the caller; nb_args must contain the number of bytes
2136 * to be conserved. The 16-bit SS:SP will be set accordinly.
2138 * All other registers are either taken from the CONTEXT86 structure
2139 * or else set to default values. The target routine address is either
2140 * given directly or taken from the CONTEXT86.
2142 * If you want to call a 16-bit routine taking only standard argument types
2143 * (WORD and LONG), you can also have an appropriate argument conversion
2144 * stub automatically generated (see BuildCallTo16); you'd then call this
2145 * stub, which in turn would prepare the 16-bit stack and call the appropiate
2146 * core routine.
2150 static void BuildCallTo16Core( FILE *outfile, int short_ret, int reg_func )
2152 char *name = reg_func == 2 ? "RegisterLong" :
2153 reg_func == 1 ? "RegisterShort" :
2154 short_ret? "Word" : "Long";
2156 /* Function header */
2157 fprintf( outfile, "\n\t.align 4\n" );
2158 #ifdef USE_STABS
2159 fprintf( outfile, ".stabs \"CallTo16%s:F1\",36,0,0," PREFIX "CallTo16%s\n",
2160 name, name);
2161 #endif
2162 fprintf( outfile, "\t.globl " PREFIX "CallTo16%s\n", name );
2163 fprintf( outfile, PREFIX "CallTo16%s:\n", name );
2165 /* Function entry sequence */
2166 fprintf( outfile, "\tpushl %%ebp\n" );
2167 fprintf( outfile, "\tmovl %%esp, %%ebp\n" );
2169 /* Save the 32-bit registers */
2170 fprintf( outfile, "\tpushl %%ebx\n" );
2171 fprintf( outfile, "\tpushl %%ecx\n" );
2172 fprintf( outfile, "\tpushl %%edx\n" );
2173 fprintf( outfile, "\tpushl %%esi\n" );
2174 fprintf( outfile, "\tpushl %%edi\n" );
2176 if ( UsePIC )
2178 /* Get Global Offset Table into %ebx */
2179 fprintf( outfile, "\tcall .LCallTo16%s.getgot1\n", name );
2180 fprintf( outfile, ".LCallTo16%s.getgot1:\n", name );
2181 fprintf( outfile, "\tpopl %%ebx\n" );
2182 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot1], %%ebx\n", name );
2185 /* Enter Win16 Mutex */
2186 if ( UsePIC )
2187 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock@PLT\n" );
2188 else
2189 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock\n" );
2191 /* Print debugging info */
2192 if (debugging)
2194 /* Push flags, number of arguments, and target */
2195 fprintf( outfile, "\tpushl $%d\n", reg_func );
2196 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2197 fprintf( outfile, "\tpushl 8(%%ebp)\n" );
2199 if ( UsePIC )
2200 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16@PLT\n" );
2201 else
2202 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
2204 fprintf( outfile, "\taddl $12, %%esp\n" );
2207 /* Get return address */
2208 if ( UsePIC )
2209 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr@GOTOFF(%%ebx), %%ecx\n" );
2210 else
2211 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr, %%ecx\n" );
2213 /* Call the actual CallTo16 routine (simulate a lcall) */
2214 fprintf( outfile, "\tpushl %%cs\n" );
2215 fprintf( outfile, "\tcall .LCallTo16%s\n", name );
2217 /* Convert and push return value */
2218 if ( short_ret )
2220 fprintf( outfile, "\tmovzwl %%ax, %%eax\n" );
2221 fprintf( outfile, "\tpushl %%eax\n" );
2223 else if ( reg_func != 2 )
2225 fprintf( outfile, "\tshll $16,%%edx\n" );
2226 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
2227 fprintf( outfile, "\tpushl %%edx\n" );
2229 else
2230 fprintf( outfile, "\tpushl %%eax\n" );
2232 if ( UsePIC )
2234 /* Get Global Offset Table into %ebx (might have been overwritten) */
2235 fprintf( outfile, "\tcall .LCallTo16%s.getgot2\n", name );
2236 fprintf( outfile, ".LCallTo16%s.getgot2:\n", name );
2237 fprintf( outfile, "\tpopl %%ebx\n" );
2238 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot2], %%ebx\n", name );
2241 /* Print debugging info */
2242 if (debugging)
2244 if ( UsePIC )
2245 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret@PLT\n" );
2246 else
2247 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret\n" );
2250 /* Leave Win16 Mutex */
2251 if ( UsePIC )
2252 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock@PLT\n" );
2253 else
2254 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock\n" );
2256 /* Get return value */
2257 fprintf( outfile, "\tpopl %%eax\n" );
2259 /* Restore the 32-bit registers */
2260 fprintf( outfile, "\tpopl %%edi\n" );
2261 fprintf( outfile, "\tpopl %%esi\n" );
2262 fprintf( outfile, "\tpopl %%edx\n" );
2263 fprintf( outfile, "\tpopl %%ecx\n" );
2264 fprintf( outfile, "\tpopl %%ebx\n" );
2266 /* Function exit sequence */
2267 fprintf( outfile, "\tpopl %%ebp\n" );
2268 fprintf( outfile, "\tret $8\n" );
2271 /* Start of the actual CallTo16 routine */
2273 fprintf( outfile, ".LCallTo16%s:\n", name );
2275 /* Complete STACK32FRAME */
2276 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
2277 fprintf( outfile, "\tmovl %%esp,%%edx\n" );
2279 /* Switch to the 16-bit stack */
2280 #ifdef __svr4__
2281 fprintf( outfile,"\tdata16\n");
2282 #endif
2283 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
2284 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
2285 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
2287 /* Make %bp point to the previous stackframe (built by CallFrom16) */
2288 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
2289 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp) );
2291 /* Add the specified offset to the new sp */
2292 fprintf( outfile, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args) );
2294 /* Push the return address
2295 * With sreg suffix, we push 16:16 address (normal lret)
2296 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
2298 if (reg_func != 2)
2299 fprintf( outfile, "\tpushl %%ecx\n" );
2300 else
2302 fprintf( outfile, "\tshldl $16, %%ecx, %%eax\n" );
2303 fprintf( outfile, "\tpushw $0\n" );
2304 fprintf( outfile, "\tpushw %%ax\n" );
2305 fprintf( outfile, "\tpushw $0\n" );
2306 fprintf( outfile, "\tpushw %%cx\n" );
2309 if (reg_func)
2311 /* Push the called routine address */
2312 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target) );
2313 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs) );
2314 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip) );
2316 /* Get the registers */
2317 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs) );
2318 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs) );
2319 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2320 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs) );
2321 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2322 fprintf( outfile, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp) );
2323 fprintf( outfile, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi) );
2324 fprintf( outfile, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi) );
2325 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax) );
2326 fprintf( outfile, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx) );
2327 fprintf( outfile, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx) );
2328 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx) );
2330 /* Get the 16-bit ds */
2331 fprintf( outfile, "\tpopw %%ds\n" );
2333 else /* not a register function */
2335 /* Push the called routine address */
2336 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK32OFFSET(target) );
2338 /* Set %fs to the value saved by the last CallFrom16 */
2339 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs)-STACK16OFFSET(bp) );
2340 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2342 /* Set %ds and %es (and %ax just in case) equal to %ss */
2343 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
2344 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2345 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2348 /* Jump to the called routine */
2349 fprintf( outfile, "\t.byte 0x66\n" );
2350 fprintf( outfile, "\tlret\n" );
2353 /*******************************************************************
2354 * BuildRet16Func
2356 * Build the return code for 16-bit callbacks
2358 static void BuildRet16Func( FILE *outfile )
2361 * Note: This must reside in the .data section to allow
2362 * run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
2365 fprintf( outfile, "\n\t.globl " PREFIX "CallTo16_Ret\n" );
2366 fprintf( outfile, PREFIX "CallTo16_Ret:\n" );
2368 /* Restore 32-bit segment registers */
2370 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
2371 #ifdef __svr4__
2372 fprintf( outfile, "\tdata16\n");
2373 #endif
2374 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
2375 #ifdef __svr4__
2376 fprintf( outfile, "\tdata16\n");
2377 #endif
2378 fprintf( outfile, "\tmovw %%bx,%%es\n" );
2380 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
2382 /* Restore the 32-bit stack */
2384 #ifdef __svr4__
2385 fprintf( outfile, "\tdata16\n");
2386 #endif
2387 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
2388 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
2389 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2391 /* Return to caller */
2393 fprintf( outfile, "\tlret\n" );
2395 /* Declare the return address variable */
2397 fprintf( outfile, "\n\t.globl " PREFIX "CallTo16_RetAddr\n" );
2398 fprintf( outfile, PREFIX "CallTo16_RetAddr:\t.long 0\n" );
2401 /*******************************************************************
2402 * BuildCallTo32CBClient
2404 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
2406 * Since the relay stub is itself 32-bit, this should not be a problem;
2407 * unfortunately, the relay stubs are expected to switch back to a
2408 * 16-bit stack (and 16-bit code) after completion :-(
2410 * This would conflict with our 16- vs. 32-bit stack handling, so
2411 * we simply switch *back* to our 32-bit stack before returning to
2412 * the caller ...
2414 * The CBClient relay stub expects to be called with the following
2415 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
2416 * stack at the designated places:
2418 * ...
2419 * (ebp+14) original arguments to the callback routine
2420 * (ebp+10) far return address to original caller
2421 * (ebp+6) Thunklet target address
2422 * (ebp+2) Thunklet relay ID code
2423 * (ebp) BP (saved by CBClientGlueSL)
2424 * (ebp-2) SI (saved by CBClientGlueSL)
2425 * (ebp-4) DI (saved by CBClientGlueSL)
2426 * (ebp-6) DS (saved by CBClientGlueSL)
2428 * ... buffer space used by the 16-bit side glue for temp copies
2430 * (ebx+4) far return address to 16-bit side glue code
2431 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
2433 * The 32-bit side glue code accesses both the original arguments (via ebp)
2434 * and the temporary copies prepared by the 16-bit side glue (via ebx).
2435 * After completion, the stub will load ss:sp from the buffer at ebx
2436 * and perform a far return to 16-bit code.
2438 * To trick the relay stub into returning to us, we replace the 16-bit
2439 * return address to the glue code by a cs:ip pair pointing to our
2440 * return entry point (the original return address is saved first).
2441 * Our return stub thus called will then reload the 32-bit ss:esp and
2442 * return to 32-bit code (by using and ss:esp value that we have also
2443 * pushed onto the 16-bit stack before and a cs:eip values found at
2444 * that position on the 32-bit stack). The ss:esp to be restored is
2445 * found relative to the 16-bit stack pointer at:
2447 * (ebx-4) ss (flat)
2448 * (ebx-8) sp (32-bit stack pointer)
2450 * The second variant of this routine, CALL32_CBClientEx, which is used
2451 * to implement KERNEL.621, has to cope with yet another problem: Here,
2452 * the 32-bit side directly returns to the caller of the CBClient thunklet,
2453 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
2454 * As we have to return to our 32-bit code first, we have to adapt the
2455 * layout of our temporary area so as to include values for the registers
2456 * that are to be restored, and later (in the implementation of KERNEL.621)
2457 * we *really* restore them. The return stub restores DS, DI, SI, and BP
2458 * from the stack, skips the next 8 bytes (CBClient relay code / target),
2459 * and then performs a lret NN, where NN is the number of arguments to be
2460 * removed. Thus, we prepare our temporary area as follows:
2462 * (ebx+22) 16-bit cs (this segment)
2463 * (ebx+20) 16-bit ip ('16-bit' return entry point)
2464 * (ebx+16) 32-bit ss (flat)
2465 * (ebx+12) 32-bit sp (32-bit stack pointer)
2466 * (ebx+10) 16-bit bp (points to ebx+24)
2467 * (ebx+8) 16-bit si (ignored)
2468 * (ebx+6) 16-bit di (ignored)
2469 * (ebx+4) 16-bit ds (we actually use the flat DS here)
2470 * (ebx+2) 16-bit ss (16-bit stack segment)
2471 * (ebx+0) 16-bit sp (points to ebx+4)
2473 * Note that we ensure that DS is not changed and remains the flat segment,
2474 * and the 32-bit stack pointer our own return stub needs fits just
2475 * perfectly into the 8 bytes that are skipped by the Windows stub.
2476 * One problem is that we have to determine the number of removed arguments,
2477 * as these have to be really removed in KERNEL.621. Thus, the BP value
2478 * that we place in the temporary area to be restored, contains the value
2479 * that SP would have if no arguments were removed. By comparing the actual
2480 * value of SP with this value in our return stub we can compute the number
2481 * of removed arguments. This is then returned to KERNEL.621.
2483 * The stack layout of this function:
2484 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
2485 * (ebp+16) esi pointer to caller's esi value
2486 * (ebp+12) arg ebp value to be set for relay stub
2487 * (ebp+8) func CBClient relay stub address
2488 * (ebp+4) ret addr
2489 * (ebp) ebp
2491 static void BuildCallTo32CBClient( FILE *outfile, BOOL isEx )
2493 char *name = isEx? "CBClientEx" : "CBClient";
2494 int size = isEx? 24 : 12;
2496 /* Function header */
2498 fprintf( outfile, "\n\t.align 4\n" );
2499 #ifdef USE_STABS
2500 fprintf( outfile, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX "CALL32_%s\n",
2501 name, name );
2502 #endif
2503 fprintf( outfile, "\t.globl " PREFIX "CALL32_%s\n", name );
2504 fprintf( outfile, PREFIX "CALL32_%s:\n", name );
2506 /* Entry code */
2508 fprintf( outfile, "\tpushl %%ebp\n" );
2509 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2510 fprintf( outfile, "\tpushl %%edi\n" );
2511 fprintf( outfile, "\tpushl %%esi\n" );
2512 fprintf( outfile, "\tpushl %%ebx\n" );
2514 /* Get the 16-bit stack */
2516 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET);
2518 /* Convert it to a flat address */
2520 fprintf( outfile, "\tshldl $16,%%ebx,%%eax\n" );
2521 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
2522 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%esi\n" );
2523 fprintf( outfile, "\tmovw %%bx,%%ax\n" );
2524 fprintf( outfile, "\taddl %%eax,%%esi\n" );
2526 /* Allocate temporary area (simulate STACK16_PUSH) */
2528 fprintf( outfile, "\tpushf\n" );
2529 fprintf( outfile, "\tcld\n" );
2530 fprintf( outfile, "\tleal -%d(%%esi), %%edi\n", size );
2531 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2532 fprintf( outfile, "\trep\n\tmovsb\n" );
2533 fprintf( outfile, "\tpopf\n" );
2535 fprintf( outfile, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size, STACKOFFSET );
2537 fprintf( outfile, "\tpushl %%edi\n" ); /* remember address */
2539 /* Set up temporary area */
2541 if ( !isEx )
2543 fprintf( outfile, "\tleal 4(%%edi), %%edi\n" );
2545 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2546 fprintf( outfile, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */
2548 fprintf( outfile, "\tmovl %%ss, %%ax\n" );
2549 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2550 fprintf( outfile, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */
2552 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 + 4 );
2553 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */
2555 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
2556 fprintf( outfile, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */
2558 else
2560 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 );
2561 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" );
2563 fprintf( outfile, "\tmovl %%ds, %%ax\n" );
2564 fprintf( outfile, "\tmovw %%ax, 4(%%edi)\n" );
2566 fprintf( outfile, "\taddl $20, %%ebx\n" );
2567 fprintf( outfile, "\tmovw %%bx, 10(%%edi)\n" );
2569 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2570 fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
2572 fprintf( outfile, "\tmovl %%ss, %%ax\n" );
2573 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2574 fprintf( outfile, "\tmovl %%eax, 16(%%edi)\n" );
2576 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
2577 fprintf( outfile, "\tmovl %%eax, 20(%%edi)\n" );
2580 /* Set up registers and call CBClient relay stub (simulating a far call) */
2582 fprintf( outfile, "\tmovl 16(%%ebp), %%esi\n" );
2583 fprintf( outfile, "\tmovl (%%esi), %%esi\n" );
2585 fprintf( outfile, "\tmovl %%edi, %%ebx\n" );
2586 fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
2587 fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
2589 fprintf( outfile, "\tpushl %%cs\n" );
2590 fprintf( outfile, "\tcall *%%eax\n" );
2592 /* Return new esi value to caller */
2594 fprintf( outfile, "\tmovl 32(%%esp), %%edi\n" );
2595 fprintf( outfile, "\tmovl %%esi, (%%edi)\n" );
2597 /* Cleanup temporary area (simulate STACK16_POP) */
2599 fprintf( outfile, "\tpop %%esi\n" );
2601 fprintf( outfile, "\tpushf\n" );
2602 fprintf( outfile, "\tstd\n" );
2603 fprintf( outfile, "\tdec %%esi\n" );
2604 fprintf( outfile, "\tleal %d(%%esi), %%edi\n", size );
2605 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2606 fprintf( outfile, "\trep\n\tmovsb\n" );
2607 fprintf( outfile, "\tpopf\n" );
2609 fprintf( outfile, "\t.byte 0x64\n\taddw $%d,(%d)\n", size, STACKOFFSET );
2611 /* Return argument size to caller */
2612 if ( isEx )
2614 fprintf( outfile, "\tmovl 32(%%esp), %%ebx\n" );
2615 fprintf( outfile, "\tmovl %%ebp, (%%ebx)\n" );
2618 /* Restore registers and return */
2620 fprintf( outfile, "\tpopl %%ebx\n" );
2621 fprintf( outfile, "\tpopl %%esi\n" );
2622 fprintf( outfile, "\tpopl %%edi\n" );
2623 fprintf( outfile, "\tpopl %%ebp\n" );
2624 fprintf( outfile, "\tret\n" );
2627 static void BuildCallTo32CBClientRet( FILE *outfile, BOOL isEx )
2629 char *name = isEx? "CBClientEx" : "CBClient";
2631 /* '16-bit' return stub */
2633 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_Ret\n", name );
2634 fprintf( outfile, PREFIX "CALL32_%s_Ret:\n", name );
2636 if ( !isEx )
2638 fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
2639 fprintf( outfile, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
2641 else
2643 fprintf( outfile, "\tmovzwl %%bp, %%ebx\n" );
2644 fprintf( outfile, "\tsubw %%bp, %%sp\n" );
2645 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
2646 fprintf( outfile, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
2648 fprintf( outfile, "\tlret\n" );
2650 /* Declare the return address variable */
2652 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_RetAddr\n", name );
2653 fprintf( outfile, PREFIX "CALL32_%s_RetAddr:\t.long 0\n", name );
2657 /*******************************************************************
2658 * BuildCallTo32LargeStack
2660 * Build the function used to switch to the original 32-bit stack
2661 * before calling a 32-bit function from 32-bit code. This is used for
2662 * functions that need a large stack, like X bitmaps functions.
2664 * The generated function has the following prototype:
2665 * int xxx( int (*func)(), void *arg );
2667 * The pointer to the function can be retrieved by calling CALL32_Init,
2668 * which also takes care of saving the current 32-bit stack pointer.
2669 * Furthermore, CALL32_Init switches to a new stack and jumps to the
2670 * specified target address.
2672 * NOTE: The CALL32_LargeStack routine may be recursively entered by the
2673 * same thread, but not concurrently entered by several threads.
2675 * Stack layout of CALL32_Init:
2677 * (esp+12) new stack address
2678 * (esp+8) target address
2679 * (esp+4) pointer to variable to receive CALL32_LargeStack address
2680 * (esp) ret addr
2682 * Stack layout of CALL32_LargeStack:
2683 * ... ...
2684 * (ebp+12) arg
2685 * (ebp+8) func
2686 * (ebp+4) ret addr
2687 * (ebp) ebp
2689 static void BuildCallTo32LargeStack( FILE *outfile )
2691 /* Initialization function */
2693 fprintf( outfile, "\n\t.align 4\n" );
2694 #ifdef USE_STABS
2695 fprintf( outfile, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX "CALL32_Init\n");
2696 #endif
2697 fprintf( outfile, "\t.globl " PREFIX "CALL32_Init\n" );
2698 fprintf( outfile, "\t.type " PREFIX "CALL32_Init,@function\n" );
2699 fprintf( outfile, PREFIX "CALL32_Init:\n" );
2700 fprintf( outfile, "\tmovl %%esp,CALL32_Original32_esp\n" );
2701 fprintf( outfile, "\tpopl %%eax\n" );
2702 fprintf( outfile, "\tpopl %%eax\n" );
2703 fprintf( outfile, "\tmovl $CALL32_LargeStack,(%%eax)\n" );
2704 fprintf( outfile, "\tpopl %%eax\n" );
2705 fprintf( outfile, "\tpopl %%esp\n" );
2706 fprintf( outfile, "\tpushl %%eax\n" );
2707 fprintf( outfile, "\tret\n" );
2709 /* Function header */
2711 fprintf( outfile, "\n\t.align 4\n" );
2712 #ifdef USE_STABS
2713 fprintf( outfile, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2714 #endif
2715 fprintf( outfile, "CALL32_LargeStack:\n" );
2717 /* Entry code */
2719 fprintf( outfile, "\tpushl %%ebp\n" );
2720 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2722 /* Switch to the original 32-bit stack pointer */
2724 fprintf( outfile, "\tcmpl $0, CALL32_RecursionCount\n" );
2725 fprintf( outfile, "\tjne CALL32_skip\n" );
2726 fprintf( outfile, "\tmovl CALL32_Original32_esp, %%esp\n" );
2727 fprintf( outfile, "CALL32_skip:\n" );
2729 fprintf( outfile, "\tincl CALL32_RecursionCount\n" );
2731 /* Transfer the argument and call the function */
2733 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2734 fprintf( outfile, "\tcall *8(%%ebp)\n" );
2736 /* Restore registers and return */
2738 fprintf( outfile, "\tdecl CALL32_RecursionCount\n" );
2740 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2741 fprintf( outfile, "\tpopl %%ebp\n" );
2742 fprintf( outfile, "\tret\n" );
2744 /* Data */
2746 fprintf( outfile, "\t.data\n" );
2747 fprintf( outfile, "CALL32_Original32_esp:\t.long 0\n" );
2748 fprintf( outfile, "CALL32_RecursionCount:\t.long 0\n" );
2749 fprintf( outfile, "\t.text\n" );
2753 /*******************************************************************
2754 * BuildCallFrom32Regs
2756 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2757 * 'args' is the number of dword arguments.
2759 * Stack layout:
2760 * ...
2761 * (ebp+12) first arg
2762 * (ebp+8) ret addr to user code
2763 * (ebp+4) ret addr to relay code
2764 * (ebp+0) saved ebp
2765 * (ebp-128) buffer area to allow stack frame manipulation
2766 * (ebp-332) CONTEXT86 struct
2767 * (ebp-336) CONTEXT86 *argument
2768 * .... other arguments copied from (ebp+12)
2770 * The entry point routine is called with a CONTEXT* extra argument,
2771 * following the normal args. In this context structure, EIP_reg
2772 * contains the return address to user code, and ESP_reg the stack
2773 * pointer on return (with the return address and arguments already
2774 * removed).
2776 static void BuildCallFrom32Regs( FILE *outfile )
2778 static const int STACK_SPACE = 128 + sizeof(CONTEXT86);
2780 /* Function header */
2782 fprintf( outfile, "\n\t.align 4\n" );
2783 #ifdef USE_STABS
2784 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
2785 #endif
2786 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
2787 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
2789 /* Allocate some buffer space on the stack */
2791 fprintf( outfile, "\tpushl %%ebp\n" );
2792 fprintf( outfile, "\tmovl %%esp,%%ebp\n ");
2793 fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
2795 /* Build the context structure */
2797 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
2798 fprintf( outfile, "\tpushfl\n" );
2799 fprintf( outfile, "\tpopl %%eax\n" );
2800 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
2801 fprintf( outfile, "\tmovl 0(%%ebp),%%eax\n" );
2802 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
2803 fprintf( outfile, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
2804 fprintf( outfile, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
2805 fprintf( outfile, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
2806 fprintf( outfile, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
2807 fprintf( outfile, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
2809 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2810 fprintf( outfile, "\tmovw %%cs,%%ax\n" );
2811 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs) - STACK_SPACE );
2812 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2813 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
2814 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2815 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
2816 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2817 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
2818 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
2819 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs) - STACK_SPACE );
2820 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2821 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs) - STACK_SPACE );
2822 fprintf( outfile, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
2824 fprintf( outfile, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL );
2825 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags) - STACK_SPACE );
2827 fprintf( outfile, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
2828 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
2830 /* Transfer the arguments */
2832 fprintf( outfile, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
2833 fprintf( outfile, "\tpushl %%esp\n" ); /* push ptr to context struct */
2834 fprintf( outfile, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
2835 fprintf( outfile, "\tjecxz 1f\n" );
2836 fprintf( outfile, "\tsubl %%ecx,%%esp\n" );
2837 fprintf( outfile, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */
2838 fprintf( outfile, "\tmovl %%esp,%%edi\n" );
2839 fprintf( outfile, "\tshrl $2,%%ecx\n" );
2840 fprintf( outfile, "\tcld\n" );
2841 fprintf( outfile, "\trep\n\tmovsl\n" ); /* copy args */
2843 fprintf( outfile, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
2844 fprintf( outfile, "\tleal 12(%%ebp,%%eax),%%eax\n" );
2845 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2847 /* Call the entry point */
2849 fprintf( outfile, "\tcall *0(%%ebx)\n" );
2851 /* Store %eip and %ebp onto the new stack */
2853 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2854 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
2855 fprintf( outfile, "\tmovl %%eax,-4(%%edx)\n" );
2856 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
2857 fprintf( outfile, "\tmovl %%eax,-8(%%edx)\n" );
2859 /* Restore the context structure */
2861 /* Note: we don't bother to restore %cs, %ds and %ss
2862 * changing them in 32-bit code is a recipe for disaster anyway
2864 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
2865 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2866 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
2867 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2868 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
2869 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2871 fprintf( outfile, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
2872 fprintf( outfile, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
2873 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
2874 fprintf( outfile, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
2875 fprintf( outfile, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
2877 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
2878 fprintf( outfile, "\tpushl %%eax\n" );
2879 fprintf( outfile, "\tpopfl\n" );
2880 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
2882 fprintf( outfile, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2883 fprintf( outfile, "\tleal -8(%%ebp),%%esp\n" );
2884 fprintf( outfile, "\tpopl %%ebp\n" );
2885 fprintf( outfile, "\tret\n" );
2889 /*******************************************************************
2890 * BuildSpec
2892 * Build the spec files
2894 static int BuildSpec( FILE *outfile, int argc, char *argv[] )
2896 int i;
2897 for (i = 2; i < argc; i++)
2898 if (BuildSpecFile( outfile, argv[i] ) < 0) return -1;
2899 return 0;
2902 /*******************************************************************
2903 * BuildGlue
2905 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
2907 static int BuildGlue( FILE *outfile, char * outname, int argc, char *argv[] )
2909 char buffer[1024];
2910 FILE *infile;
2912 if (argc > 2)
2914 infile = fopen( argv[2], "r" );
2915 if (!infile)
2917 perror( argv[2] );
2918 exit( 1 );
2921 else infile = stdin;
2923 /* File header */
2925 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
2926 argc > 2? argv[2] : "<stdin>" );
2927 fprintf( outfile, "#include \"builtin16.h\"\n" );
2928 fprintf( outfile, "#include \"stackframe.h\"\n\n" );
2930 /* Build the callback glue functions */
2932 while (fgets( buffer, sizeof(buffer), infile ))
2934 if (strstr( buffer, "### start build ###" )) break;
2936 while (fgets( buffer, sizeof(buffer), infile ))
2938 char *p;
2939 if ( (p = strstr( buffer, "CallFrom16_" )) != NULL )
2941 char *q, *profile = p + strlen( "CallFrom16_" );
2942 for (q = profile; (*q == '_') || isalpha(*q); q++ )
2944 *q = '\0';
2945 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
2947 if ( ++q < p ) p[-1] = '\0'; else q = "";
2948 BuildCallFrom16Func( outfile, profile, q, FALSE );
2950 if ( (p = strstr( buffer, "CallTo16_" )) != NULL )
2952 char *q, *profile = p + strlen( "CallTo16_" );
2953 for (q = profile; (*q == '_') || isalpha(*q); q++ )
2955 *q = '\0';
2956 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
2958 if ( ++q < p ) p[-1] = '\0'; else q = "";
2959 BuildCallTo16Func( outfile, profile, q );
2961 if (strstr( buffer, "### stop build ###" )) break;
2964 fclose( infile );
2965 return 0;
2968 /*******************************************************************
2969 * BuildCall16
2971 * Build the 16-bit callbacks
2973 static int BuildCall16( FILE *outfile, char * outname )
2975 #ifdef USE_STABS
2976 char buffer[1024];
2977 #endif
2979 /* File header */
2981 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2982 fprintf( outfile, "\t.text\n" );
2984 #ifdef __i386__
2986 #ifdef USE_STABS
2987 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2988 getcwd(buffer, sizeof(buffer));
2991 * The stabs help the internal debugger as they are an indication that it
2992 * is sensible to step into a thunk/trampoline.
2994 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2995 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2996 fprintf( outfile, "\t.text\n" );
2997 fprintf( outfile, "\t.align 4\n" );
2998 fprintf( outfile, "Code_Start:\n\n" );
2999 #endif
3000 fprintf( outfile, PREFIX"Call16_Start:\n" );
3001 fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
3002 fprintf( outfile, "\t.byte 0\n\n" );
3005 /* Standard CallFrom16 routine (WORD return) */
3006 BuildCallFrom16Core( outfile, FALSE, FALSE, TRUE );
3008 /* Standard CallFrom16 routine (DWORD return) */
3009 BuildCallFrom16Core( outfile, FALSE, FALSE, FALSE );
3011 /* Register CallFrom16 routine */
3012 BuildCallFrom16Core( outfile, TRUE, FALSE, FALSE );
3014 /* C16ThkSL CallFrom16 routine */
3015 BuildCallFrom16Core( outfile, FALSE, TRUE, FALSE );
3017 /* Standard CallTo16 routine (WORD return) */
3018 BuildCallTo16Core( outfile, TRUE, FALSE );
3020 /* Standard CallTo16 routine (DWORD return) */
3021 BuildCallTo16Core( outfile, FALSE, FALSE );
3023 /* Register CallTo16 routine (16:16 retf) */
3024 BuildCallTo16Core( outfile, FALSE, 1 );
3026 /* Register CallTo16 routine (16:32 retf) */
3027 BuildCallTo16Core( outfile, FALSE, 2 );
3029 /* CBClientThunkSL routine */
3030 BuildCallTo32CBClient( outfile, FALSE );
3032 /* CBClientThunkSLEx routine */
3033 BuildCallTo32CBClient( outfile, TRUE );
3035 fprintf( outfile, PREFIX"Call16_End:\n" );
3036 fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
3038 #ifdef USE_STABS
3039 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
3040 fprintf( outfile, ".Letext:\n");
3041 #endif
3043 /* The whole Call16_Ret segment must lie within the .data section */
3044 fprintf( outfile, "\n\t.data\n" );
3045 fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
3046 fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
3048 /* Standard CallTo16 return stub */
3049 BuildRet16Func( outfile );
3051 /* CBClientThunkSL return stub */
3052 BuildCallTo32CBClientRet( outfile, FALSE );
3054 /* CBClientThunkSLEx return stub */
3055 BuildCallTo32CBClientRet( outfile, TRUE );
3057 /* End of Call16_Ret segment */
3058 fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
3059 fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
3061 #else /* __i386__ */
3063 fprintf( outfile, PREFIX"Call16_Start:\n" );
3064 fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
3065 fprintf( outfile, "\t.byte 0\n\n" );
3066 fprintf( outfile, PREFIX"Call16_End:\n" );
3067 fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
3069 fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
3070 fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
3071 fprintf( outfile, "\t.byte 0\n\n" );
3072 fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
3073 fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
3075 #endif /* __i386__ */
3077 return 0;
3080 /*******************************************************************
3081 * BuildCall32
3083 * Build the 32-bit callbacks
3085 static int BuildCall32( FILE *outfile, char * outname )
3087 #ifdef USE_STABS
3088 char buffer[1024];
3089 #endif
3091 /* File header */
3093 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
3094 fprintf( outfile, "\t.text\n" );
3096 #ifdef __i386__
3098 #ifdef USE_STABS
3099 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
3100 getcwd(buffer, sizeof(buffer));
3103 * The stabs help the internal debugger as they are an indication that it
3104 * is sensible to step into a thunk/trampoline.
3106 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
3107 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
3108 fprintf( outfile, "\t.text\n" );
3109 fprintf( outfile, "\t.align 4\n" );
3110 fprintf( outfile, "Code_Start:\n" );
3111 #endif
3113 /* Build the 32-bit large stack callback */
3115 BuildCallTo32LargeStack( outfile );
3117 /* Build the register callback function */
3119 BuildCallFrom32Regs( outfile );
3121 #ifdef USE_STABS
3122 fprintf( outfile, "\t.text\n");
3123 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
3124 fprintf( outfile, ".Letext:\n");
3125 #endif
3127 #else /* __i386__ */
3129 /* Just to avoid an empty file */
3130 fprintf( outfile, "\t.long 0\n" );
3132 #endif /* __i386__ */
3133 return 0;
3137 /*******************************************************************
3138 * usage
3140 static void usage(void)
3142 fprintf( stderr,
3143 "usage: build [-pic] [-o outfile] -spec SPECNAMES\n"
3144 " build [-pic] [-o outfile] -glue SOURCE_FILE\n"
3145 " build [-pic] [-o outfile] -call16\n"
3146 " build [-pic] [-o outfile] -call32\n" );
3147 exit(1);
3151 /*******************************************************************
3152 * main
3154 int main(int argc, char **argv)
3156 char *outname = NULL;
3157 FILE *outfile = stdout;
3158 int res = -1;
3160 if (argc < 2) usage();
3162 if (!strcmp( argv[1], "-pic" ))
3164 UsePIC = 1;
3165 argv += 1;
3166 argc -= 1;
3167 if (argc < 2) usage();
3170 if (!strcmp( argv[1], "-o" ))
3172 outname = argv[2];
3173 argv += 2;
3174 argc -= 2;
3175 if (argc < 2) usage();
3176 if (!(outfile = fopen( outname, "w" )))
3178 fprintf( stderr, "Unable to create output file '%s'\n", outname );
3179 exit(1);
3183 /* Retrieve the selector values; this assumes that we are building
3184 * the asm files on the platform that will also run them. Probably
3185 * a safe assumption to make.
3187 GET_CS( Code_Selector );
3188 GET_DS( Data_Selector );
3190 if (!strcmp( argv[1], "-spec" ))
3191 res = BuildSpec( outfile, argc, argv );
3192 else if (!strcmp( argv[1], "-glue" ))
3193 res = BuildGlue( outfile, outname, argc, argv );
3194 else if (!strcmp( argv[1], "-call16" ))
3195 res = BuildCall16( outfile, outname );
3196 else if (!strcmp( argv[1], "-call32" ))
3197 res = BuildCall32( outfile, outname );
3198 else
3200 fclose( outfile );
3201 unlink( outname );
3202 usage();
3205 fclose( outfile );
3206 if (res < 0)
3208 unlink( outname );
3209 return 1;
3211 return 0;