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
20 #include "selectors.h"
21 #include "stackframe.h"
22 #include "builtin16.h"
25 #ifdef NEED_UNDERSCORE_PREFIX
31 #ifdef HAVE_ASM_STRING
32 # define STRING ".string"
34 # define STRING ".ascii"
37 #if defined(__GNUC__) && !defined(__svr4__)
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) */
63 static const char * const TypeNames[TYPE_NBTYPES] =
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")
153 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
155 static SPEC_TYPE SpecType = SPEC_INVALID;
156 static char DLLName[80];
157 static char DLLFileName[80];
158 static int Limit = 0;
159 static int Base = MAX_ORDINALS;
160 static int DLLHeapSize = 0;
161 static char *SpecName;
163 static WORD Code_Selector, Data_Selector;
164 static char DLLInitFunc[80];
165 static char *DLLImports[MAX_IMPORTS];
166 static int nb_imports = 0;
168 char *ParseBuffer = NULL;
173 static int UsePIC = 0;
175 static int debugging = 1;
177 /* Offset of a structure field relative to the start of the struct */
178 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
180 /* Offset of register relative to the start of the CONTEXT struct */
181 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT86,reg)
183 /* Offset of register relative to the start of the STACK16FRAME struct */
184 #define STACK16OFFSET(reg) STRUCTOFFSET(STACK16FRAME,reg)
186 /* Offset of register relative to the start of the STACK32FRAME struct */
187 #define STACK32OFFSET(reg) STRUCTOFFSET(STACK32FRAME,reg)
189 /* Offset of the stack pointer relative to %fs:(0) */
190 #define STACKOFFSET (STRUCTOFFSET(TEB,cur_stack))
192 static void BuildCallFrom16Func( FILE *outfile, char *profile, char *prefix, int local );
194 static void *xmalloc (size_t size)
198 res = malloc (size ? size : 1);
201 fprintf (stderr, "Virtual memory exhausted.\n");
208 static void *xrealloc (void *ptr, size_t size)
210 void *res = realloc (ptr, size);
213 fprintf (stderr, "Virtual memory exhausted.\n");
219 static char *xstrdup( const char *str )
221 char *res = strdup( str );
224 fprintf (stderr, "Virtual memory exhausted.\n");
230 static int IsNumberString(char *s)
239 static char *strupper(char *s)
243 for(p = s; *p != '\0'; p++)
249 static char * GetTokenInLine(void)
254 if (ParseNext != ParseBuffer)
256 if (ParseSaveChar == '\0')
258 *ParseNext = ParseSaveChar;
262 * Remove initial white space.
264 for (p = ParseNext; isspace(*p); p++)
267 if ((*p == '\0') || (*p == '#'))
274 if (*token != '(' && *token != ')')
275 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
285 static char * GetToken(void)
289 if (ParseBuffer == NULL)
291 ParseBuffer = xmalloc(512);
292 ParseNext = ParseBuffer;
296 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
298 if (ParseBuffer[0] != '#')
303 while ((token = GetTokenInLine()) == NULL)
305 ParseNext = ParseBuffer;
309 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
311 if (ParseBuffer[0] != '#')
320 /*******************************************************************
323 * Parse a variable definition.
325 static int ParseVariable( ORDDEF *odp )
330 int value_array_size;
332 char *token = GetToken();
335 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
336 SpecName, Line, token);
341 value_array_size = 25;
342 value_array = xmalloc(sizeof(*value_array) * value_array_size);
344 while ((token = GetToken()) != NULL)
349 value_array[n_values++] = strtol(token, &endptr, 0);
350 if (n_values == value_array_size)
352 value_array_size += 25;
353 value_array = xrealloc(value_array,
354 sizeof(*value_array) * value_array_size);
357 if (endptr == NULL || *endptr != '\0')
359 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
360 SpecName, Line, token);
367 fprintf(stderr, "%s:%d: End of file in variable declaration\n",
372 odp->u.var.n_values = n_values;
373 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
379 /*******************************************************************
380 * ParseExportFunction
382 * Parse a function definition.
384 static int ParseExportFunction( ORDDEF *odp )
392 if (odp->type == TYPE_STDCALL)
394 fprintf( stderr, "%s:%d: 'stdcall' not supported for Win16\n",
400 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
402 fprintf( stderr, "%s:%d: 'pascal' not supported for Win32\n",
414 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
415 SpecName, Line, token);
419 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
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';
446 fprintf(stderr, "%s:%d: Unknown variable type '%s'\n",
447 SpecName, Line, token);
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 );
464 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
466 fprintf( stderr, "%s:%d: Too many arguments\n", SpecName, Line );
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());
477 /*******************************************************************
480 * Parse an 'equate' definition.
482 static int ParseEquate( ORDDEF *odp )
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);
495 if (SpecType == SPEC_WIN32)
497 fprintf( stderr, "%s:%d: 'equate' not supported for Win32\n",
502 odp->u.abs.value = value;
507 /*******************************************************************
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 );
520 /*******************************************************************
523 * Parse an 'varargs' definition.
525 static int ParseVarargs( ORDDEF *odp )
529 if (SpecType == SPEC_WIN16)
531 fprintf( stderr, "%s:%d: 'varargs' not supported for Win16\n",
539 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
540 SpecName, Line, token);
546 fprintf(stderr, "%s:%d: Expected ')' got '%s'\n",
547 SpecName, Line, token);
551 strcpy( odp->u.vargs.link_name, GetToken() );
555 /*******************************************************************
558 * Parse an 'interrupt' definition.
560 static int ParseInterrupt( ORDDEF *odp )
564 if (SpecType == SPEC_WIN32)
566 fprintf( stderr, "%s:%d: 'interrupt' not supported for Win32\n",
574 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
575 SpecName, Line, token);
581 fprintf(stderr, "%s:%d: Expected ')' got '%s'\n",
582 SpecName, Line, token);
586 odp->u.func.arg_types[0] = '\0';
587 strcpy( odp->u.func.link_name, GetToken() );
592 /*******************************************************************
595 * Parse an 'extern' definition.
597 static int ParseExtern( ORDDEF *odp )
599 if (SpecType == SPEC_WIN16)
601 fprintf( stderr, "%s:%d: 'extern' not supported for Win16\n",
605 strcpy( odp->u.ext.link_name, GetToken() );
610 /*******************************************************************
613 * Parse a 'forward' definition.
615 static int ParseForward( ORDDEF *odp )
617 if (SpecType == SPEC_WIN16)
619 fprintf( stderr, "%s:%d: 'forward' not supported for Win16\n",
623 strcpy( odp->u.fwd.link_name, GetToken() );
628 /*******************************************************************
631 * Parse an ordinal definition.
633 static int ParseOrdinal(int ordinal)
638 if (ordinal >= MAX_ORDINALS)
640 fprintf(stderr, "%s:%d: Ordinal number too large\n", SpecName, Line );
643 if (ordinal > Limit) Limit = ordinal;
644 if (ordinal < Base) Base = ordinal;
646 odp = &OrdinalDefinitions[ordinal];
647 if (!(token = GetToken()))
649 fprintf(stderr, "%s:%d: Expected type after ordinal\n", SpecName, Line);
653 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
654 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
657 if (odp->type >= TYPE_NBTYPES)
660 "%s:%d: Expected type after ordinal, found '%s' instead\n",
661 SpecName, Line, token );
665 if (!(token = GetToken()))
667 fprintf( stderr, "%s:%d: Expected name after type\n", SpecName, Line );
670 strcpy( odp->name, token );
678 return ParseVariable( odp );
684 return ParseExportFunction( odp );
686 return ParseInterrupt( odp );
688 return ParseEquate( odp );
690 return ParseStub( odp );
692 return ParseVarargs( odp );
694 return ParseExtern( odp );
696 return ParseForward( odp );
698 fprintf( stderr, "Should not happen\n" );
704 /*******************************************************************
709 static int ParseTopLevel(void)
713 while ((token = GetToken()) != NULL)
715 if (strcmp(token, "name") == 0)
717 strcpy(DLLName, GetToken());
719 if (!DLLFileName[0]) sprintf( DLLFileName, "%s.DLL", DLLName );
721 else if (strcmp(token, "file") == 0)
723 strcpy(DLLFileName, GetToken());
724 strupper(DLLFileName);
726 else if (strcmp(token, "type") == 0)
729 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
730 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
733 fprintf(stderr, "%s:%d: Type must be 'win16' or 'win32'\n",
738 else if (strcmp(token, "heap") == 0)
741 if (!IsNumberString(token))
743 fprintf(stderr, "%s:%d: Expected number after heap\n",
747 DLLHeapSize = atoi(token);
749 else if (strcmp(token, "init") == 0)
751 strcpy(DLLInitFunc, GetToken());
752 if (SpecType == SPEC_WIN16)
754 fprintf(stderr, "%s:%d: init cannot be used for Win16 spec files\n",
760 fprintf(stderr, "%s:%d: Expected function name after init\n", SpecName, Line);
764 else if (strcmp(token, "import") == 0)
766 if (nb_imports >= MAX_IMPORTS)
768 fprintf( stderr, "%s:%d: Too many imports (limit %d)\n",
769 SpecName, Line, MAX_IMPORTS );
772 if (SpecType != SPEC_WIN32)
774 fprintf( stderr, "%s:%d: Imports not supported for Win16\n", SpecName, Line );
777 DLLImports[nb_imports++] = xstrdup(GetToken());
779 else if (IsNumberString(token))
784 ordinal = atoi(token);
785 if ((rv = ParseOrdinal(ordinal)) < 0)
791 "%s:%d: Expected name, id, length or ordinal\n",
801 /*******************************************************************
804 * Store a list of ints into a byte array.
806 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
813 for (i = 0; i < odp->u.var.n_values; i++)
814 buffer[i] = odp->u.var.values[i];
817 for (i = 0; i < odp->u.var.n_values; i++)
818 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
821 for (i = 0; i < odp->u.var.n_values; i++)
822 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
825 return odp->u.var.n_values * size;
829 /*******************************************************************
832 * Dump a byte stream into the assembly code.
834 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
839 fprintf( outfile, "\nstatic BYTE %s[] = \n{", label );
841 for (i = 0; i < len; i++)
843 if (!(i & 0x0f)) fprintf( outfile, "\n " );
844 fprintf( outfile, "%d", *data++ );
845 if (i < len - 1) fprintf( outfile, ", " );
847 fprintf( outfile, "\n};\n" );
851 /*******************************************************************
854 * Build the in-memory representation of a 16-bit NE module, and dump it
855 * as a byte stream into the assembly code.
857 static int BuildModule16( FILE *outfile, int max_code_offset,
858 int max_data_offset )
864 SEGTABLEENTRY *pSegment;
868 ET_BUNDLE *bundle = 0;
873 * OFSTRUCT File information
874 * SEGTABLEENTRY Segment 1 (code)
875 * SEGTABLEENTRY Segment 2 (data)
876 * WORD[2] Resource table (empty)
877 * BYTE[2] Imported names (empty)
878 * BYTE[n] Resident names table
879 * BYTE[n] Entry table
882 buffer = xmalloc( 0x10000 );
884 pModule = (NE_MODULE *)buffer;
885 memset( pModule, 0, sizeof(*pModule) );
886 pModule->magic = IMAGE_OS2_SIGNATURE;
889 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
891 pModule->heap_size = DLLHeapSize;
892 pModule->stack_size = 0;
897 pModule->seg_count = 2;
898 pModule->modref_count = 0;
899 pModule->nrname_size = 0;
900 pModule->modref_table = 0;
901 pModule->nrname_fpos = 0;
902 pModule->moveable_entries = 0;
903 pModule->alignment = 0;
904 pModule->truetype = 0;
905 pModule->os_flags = NE_OSFLAGS_WINDOWS;
906 pModule->misc_flags = 0;
907 pModule->dlls_to_init = 0;
908 pModule->nrname_handle = 0;
909 pModule->min_swap_area = 0;
910 pModule->expected_version = 0x030a;
911 pModule->module32 = 0;
913 pModule->self_loading_sel = 0;
915 /* File information */
917 pFileInfo = (OFSTRUCT *)(pModule + 1);
918 pModule->fileinfo = (int)pFileInfo - (int)pModule;
919 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
920 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
921 + strlen(DLLFileName);
922 strcpy( pFileInfo->szPathName, DLLFileName );
923 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
925 #ifdef __i386__ /* FIXME: Alignment problems! */
929 pSegment = (SEGTABLEENTRY *)pstr;
930 pModule->seg_table = (int)pSegment - (int)pModule;
931 pSegment->filepos = 0;
932 pSegment->size = max_code_offset;
934 pSegment->minsize = max_code_offset;
938 pModule->dgroup_entry = (int)pSegment - (int)pModule;
939 pSegment->filepos = 0;
940 pSegment->size = max_data_offset;
941 pSegment->flags = NE_SEGFLAGS_DATA;
942 pSegment->minsize = max_data_offset;
948 pword = (WORD *)pSegment;
949 pModule->res_table = (int)pword - (int)pModule;
953 /* Imported names table */
955 pstr = (char *)pword;
956 pModule->import_table = (int)pstr - (int)pModule;
960 /* Resident names table */
962 pModule->name_table = (int)pstr - (int)pModule;
963 /* First entry is module name */
964 *pstr = strlen(DLLName );
965 strcpy( pstr + 1, DLLName );
968 pstr += sizeof(WORD);
969 /* Store all ordinals */
970 odp = OrdinalDefinitions + 1;
971 for (i = 1; i <= Limit; i++, odp++)
973 if (!odp->name[0]) continue;
974 *pstr = strlen( odp->name );
975 strcpy( pstr + 1, odp->name );
976 strupper( pstr + 1 );
979 pstr += sizeof(WORD);
985 pModule->entry_table = (int)pstr - (int)pModule;
986 odp = OrdinalDefinitions + 1;
987 for (i = 1; i <= Limit; i++, odp++)
999 selector = 1; /* Code selector */
1005 selector = 2; /* Data selector */
1009 selector = 0xfe; /* Constant selector */
1013 selector = 0; /* Invalid selector */
1020 if ( bundle && bundle->last+1 == i )
1025 bundle->next = (char *)pstr - (char *)pModule;
1027 bundle = (ET_BUNDLE *)pstr;
1028 bundle->first = i-1;
1031 pstr += sizeof(ET_BUNDLE);
1034 /* FIXME: is this really correct ?? */
1035 entry = (ET_ENTRY *)pstr;
1036 entry->type = 0xff; /* movable */
1037 entry->flags = 3; /* exported & public data */
1038 entry->segnum = selector;
1039 entry->offs = odp->offset;
1040 pstr += sizeof(ET_ENTRY);
1045 /* Dump the module content */
1047 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
1049 return (int)pstr - (int)pModule;
1053 /*******************************************************************
1056 * Build a Win32 C file from a spec file.
1058 static int BuildSpec32File( char * specfile, FILE *outfile )
1061 int i, nb_names, fwd_size = 0;
1063 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
1065 fprintf( outfile, "#include \"builtin32.h\"\n\n" );
1067 /* Output code for all stubs functions */
1069 fprintf( outfile, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
1071 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1073 if (odp->type != TYPE_STUB) continue;
1074 fprintf( outfile, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
1078 /* Output code for all register functions */
1080 fprintf( outfile, "#ifdef __i386__\n" );
1081 fprintf( outfile, "#ifndef __GNUC__\n" );
1082 fprintf( outfile, "static void __asm__dummy() {\n" );
1083 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
1084 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1086 if (odp->type != TYPE_REGISTER) continue;
1088 "__asm__(\".align 4\\n\\t\"\n"
1089 " \".globl " PREFIX "%s\\n\\t\"\n"
1090 " \".type " PREFIX "%s,@function\\n\\t\"\n"
1091 " \"" PREFIX "%s:\\n\\t\"\n"
1092 " \"call " PREFIX "CALL32_Regs\\n\\t\"\n"
1093 " \".long " PREFIX "__regs_%s\\n\\t\"\n"
1094 " \".byte %d,%d\");\n",
1095 odp->u.func.link_name, odp->u.func.link_name,
1096 odp->u.func.link_name, odp->u.func.link_name,
1097 4 * strlen(odp->u.func.arg_types),
1098 4 * strlen(odp->u.func.arg_types) );
1100 fprintf( outfile, "#ifndef __GNUC__\n" );
1101 fprintf( outfile, "}\n" );
1102 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
1103 fprintf( outfile, "#endif /* defined(__i386__) */\n" );
1105 /* Output the DLL functions prototypes */
1107 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1112 fprintf( outfile, "extern void %s();\n", odp->u.vargs.link_name );
1115 fprintf( outfile, "extern void %s();\n", odp->u.ext.link_name );
1120 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1123 fwd_size += strlen(odp->u.fwd.link_name) + 1;
1129 fprintf(stderr,"build: function type %d not available for Win32\n",
1135 /* Output LibMain function */
1136 if (DLLInitFunc[0]) fprintf( outfile, "extern void %s();\n", DLLInitFunc );
1139 /* Output the DLL functions table */
1141 fprintf( outfile, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1143 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1148 fprintf( outfile, " 0" );
1151 fprintf( outfile, " %s", odp->u.vargs.link_name );
1154 fprintf( outfile, " %s", odp->u.ext.link_name );
1159 fprintf( outfile, " %s", odp->u.func.link_name);
1162 fprintf( outfile, " __stub_%d", i );
1165 fprintf( outfile, " (ENTRYPOINT32)\"%s\"", odp->u.fwd.link_name );
1170 if (i < Limit) fprintf( outfile, ",\n" );
1172 fprintf( outfile, "\n};\n\n" );
1174 /* Output the DLL names table */
1177 fprintf( outfile, "static const char * const FuncNames[] =\n{\n" );
1178 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1180 if (odp->type == TYPE_INVALID) continue;
1181 if (nb_names++) fprintf( outfile, ",\n" );
1182 fprintf( outfile, " \"%s\"", odp->name );
1184 fprintf( outfile, "\n};\n\n" );
1186 /* Output the DLL argument types */
1188 fprintf( outfile, "static const unsigned int ArgTypes[%d] =\n{\n",
1190 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1192 unsigned int j, mask = 0;
1193 if ((odp->type == TYPE_STDCALL) || (odp->type == TYPE_CDECL) ||
1194 (odp->type == TYPE_REGISTER))
1195 for (j = 0; odp->u.func.arg_types[j]; j++)
1197 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
1198 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
1200 fprintf( outfile, " %d", mask );
1201 if (i < Limit) fprintf( outfile, ",\n" );
1203 fprintf( outfile, "\n};\n\n" );
1205 /* Output the DLL ordinals table */
1207 fprintf( outfile, "static const unsigned short FuncOrdinals[] =\n{\n" );
1209 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1211 if (odp->type == TYPE_INVALID) continue;
1212 if (nb_names++) fprintf( outfile, ",\n" );
1213 fprintf( outfile, " %d", i - Base );
1215 fprintf( outfile, "\n};\n\n" );
1217 /* Output the DLL functions arguments */
1219 fprintf( outfile, "static const unsigned char FuncArgs[%d] =\n{\n",
1221 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1227 args = (unsigned char)strlen(odp->u.func.arg_types);
1230 args = 0x80 | (unsigned char)strlen(odp->u.func.arg_types);
1233 args = 0x40 | (unsigned char)strlen(odp->u.func.arg_types);
1242 fprintf( outfile, " 0x%02x", args );
1243 if (i < Limit) fprintf( outfile, ",\n" );
1245 fprintf( outfile, "\n};\n\n" );
1247 /* Output the DLL imports */
1251 fprintf( outfile, "static const char * const Imports[%d] =\n{\n", nb_imports );
1252 for (i = 0; i < nb_imports; i++)
1254 fprintf( outfile, " \"%s\"", DLLImports[i] );
1255 if (i < nb_imports-1) fprintf( outfile, ",\n" );
1257 fprintf( outfile, "\n};\n\n" );
1260 /* Output the DLL descriptor */
1262 fprintf( outfile, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1264 fprintf( outfile, " \"%s\",\n", DLLName );
1265 fprintf( outfile, " %d,\n", Base );
1266 fprintf( outfile, " %d,\n", Limit - Base + 1 );
1267 fprintf( outfile, " %d,\n", nb_names );
1268 fprintf( outfile, " %d,\n", nb_imports );
1269 fprintf( outfile, " %d,\n", (fwd_size + 3) & ~3 );
1276 fprintf( outfile, " %s,\n", nb_imports ? "Imports" : "0" );
1277 fprintf( outfile, " %s\n", DLLInitFunc[0] ? DLLInitFunc : "0" );
1278 fprintf( outfile, "};\n" );
1282 /*******************************************************************
1285 static int Spec16TypeCompare( const void *e1, const void *e2 )
1287 const ORDDEF *odp1 = *(const ORDDEF **)e1;
1288 const ORDDEF *odp2 = *(const ORDDEF **)e2;
1290 int type1 = (odp1->type == TYPE_CDECL) ? 0
1291 : (odp1->type == TYPE_REGISTER) ? 3
1292 : (odp1->type == TYPE_INTERRUPT) ? 4
1293 : (odp1->type == TYPE_PASCAL_16) ? 1 : 2;
1295 int type2 = (odp2->type == TYPE_CDECL) ? 0
1296 : (odp2->type == TYPE_REGISTER) ? 3
1297 : (odp2->type == TYPE_INTERRUPT) ? 4
1298 : (odp2->type == TYPE_PASCAL_16) ? 1 : 2;
1300 int retval = type1 - type2;
1302 retval = strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
1307 /*******************************************************************
1310 * Build a Win16 assembly file from a spec file.
1312 static int BuildSpec16File( char * specfile, FILE *outfile )
1314 ORDDEF *odp, **type, **typelist;
1315 int i, nFuncs, nTypes;
1316 int code_offset, data_offset, module_size;
1317 unsigned char *data;
1321 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
1323 fprintf( outfile, "#define __FLATCS__ 0x%04x\n", Code_Selector );
1324 fprintf( outfile, "#include \"builtin16.h\"\n\n" );
1326 data = (unsigned char *)xmalloc( 0x10000 );
1327 memset( data, 0, 16 );
1331 /* Build sorted list of all argument types, without duplicates */
1333 typelist = (ORDDEF **)calloc( Limit+1, sizeof(ORDDEF *) );
1335 odp = OrdinalDefinitions;
1336 for (i = nFuncs = 0; i <= Limit; i++, odp++)
1341 case TYPE_INTERRUPT:
1344 case TYPE_PASCAL_16:
1346 typelist[nFuncs++] = odp;
1353 qsort( typelist, nFuncs, sizeof(ORDDEF *), Spec16TypeCompare );
1356 while ( i < nFuncs )
1358 typelist[nTypes++] = typelist[i++];
1359 while ( i < nFuncs && Spec16TypeCompare( typelist + i, typelist + nTypes-1 ) == 0 )
1363 /* Output CallFrom16 routines needed by this .spec file */
1365 for ( i = 0; i < nTypes; i++ )
1369 sprintf( profile, "%s_%s_%s",
1370 (typelist[i]->type == TYPE_CDECL) ? "c" : "p",
1371 (typelist[i]->type == TYPE_REGISTER) ? "regs" :
1372 (typelist[i]->type == TYPE_INTERRUPT) ? "intr" :
1373 (typelist[i]->type == TYPE_PASCAL_16) ? "word" : "long",
1374 typelist[i]->u.func.arg_types );
1376 BuildCallFrom16Func( outfile, profile, DLLName, TRUE );
1379 /* Output the DLL functions prototypes */
1381 odp = OrdinalDefinitions;
1382 for (i = 0; i <= Limit; i++, odp++)
1387 case TYPE_INTERRUPT:
1390 case TYPE_PASCAL_16:
1391 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1398 /* Output code segment */
1400 fprintf( outfile, "\nstatic struct\n{\n CALLFROM16 call[%d];\n"
1401 " ENTRYPOINT16 entry[%d];\n} Code_Segment = \n{\n {\n",
1405 for ( i = 0; i < nTypes; i++ )
1407 char profile[101], *arg;
1410 sprintf( profile, "%s_%s_%s",
1411 (typelist[i]->type == TYPE_CDECL) ? "c" : "p",
1412 (typelist[i]->type == TYPE_REGISTER) ? "regs" :
1413 (typelist[i]->type == TYPE_INTERRUPT) ? "intr" :
1414 (typelist[i]->type == TYPE_PASCAL_16) ? "word" : "long",
1415 typelist[i]->u.func.arg_types );
1417 if ( typelist[i]->type != TYPE_CDECL )
1418 for ( arg = typelist[i]->u.func.arg_types; *arg; arg++ )
1421 case 'w': /* word */
1422 case 's': /* s_word */
1426 case 'l': /* long or segmented pointer */
1427 case 'T': /* segmented pointer to null-terminated string */
1428 case 'p': /* linear pointer */
1429 case 't': /* linear pointer to null-terminated string */
1434 if ( typelist[i]->type == TYPE_INTERRUPT )
1437 fprintf( outfile, " CF16_%s( %s_CallFrom16_%s, %d, \"%s\" ),\n",
1438 ( typelist[i]->type == TYPE_REGISTER
1439 || typelist[i]->type == TYPE_INTERRUPT)? "REGS":
1440 typelist[i]->type == TYPE_PASCAL_16? "WORD" : "LONG",
1441 DLLName, profile, argsize, profile );
1443 code_offset += sizeof(CALLFROM16);
1445 fprintf( outfile, " },\n {\n" );
1447 odp = OrdinalDefinitions;
1448 for (i = 0; i <= Limit; i++, odp++)
1453 odp->offset = 0xffff;
1457 odp->offset = LOWORD(odp->u.abs.value);
1461 odp->offset = data_offset;
1462 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1466 odp->offset = data_offset;
1467 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1471 odp->offset = data_offset;
1472 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1476 case TYPE_INTERRUPT:
1479 case TYPE_PASCAL_16:
1481 type = bsearch( &odp, typelist, nTypes, sizeof(ORDDEF *), Spec16TypeCompare );
1484 fprintf( outfile, " /* %s.%d */ ", DLLName, i );
1485 fprintf( outfile, "EP( %s, %d /* %s_%s_%s */ ),\n",
1486 odp->u.func.link_name,
1487 (type-typelist)*sizeof(CALLFROM16) -
1488 (code_offset + sizeof(ENTRYPOINT16)),
1489 (odp->type == TYPE_CDECL) ? "c" : "p",
1490 (odp->type == TYPE_REGISTER) ? "regs" :
1491 (odp->type == TYPE_INTERRUPT) ? "intr" :
1492 (odp->type == TYPE_PASCAL_16) ? "word" : "long",
1493 odp->u.func.arg_types );
1495 odp->offset = code_offset;
1496 code_offset += sizeof(ENTRYPOINT16);
1500 fprintf(stderr,"build: function type %d not available for Win16\n",
1506 fprintf( outfile, " }\n};\n" );
1508 /* Output data segment */
1510 DumpBytes( outfile, data, data_offset, "Data_Segment" );
1512 /* Build the module */
1514 module_size = BuildModule16( outfile, code_offset, data_offset );
1516 /* Output the DLL descriptor */
1518 fprintf( outfile, "\nWIN16_DESCRIPTOR %s_Descriptor = \n{\n", DLLName );
1519 fprintf( outfile, " \"%s\",\n", DLLName );
1520 fprintf( outfile, " Module,\n" );
1521 fprintf( outfile, " sizeof(Module),\n" );
1522 fprintf( outfile, " (BYTE *)&Code_Segment,\n" );
1523 fprintf( outfile, " (BYTE *)Data_Segment\n" );
1524 fprintf( outfile, "};\n" );
1530 /*******************************************************************
1533 * Build an assembly file from a spec file.
1535 static int BuildSpecFile( FILE *outfile, char *specname )
1537 SpecName = specname;
1538 SpecFp = fopen( specname, "r");
1541 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1545 if (ParseTopLevel() < 0) return -1;
1550 return BuildSpec16File( specname, outfile );
1552 return BuildSpec32File( specname, outfile );
1554 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1560 /*******************************************************************
1561 * BuildCallFrom16Func
1563 * Build a 16-bit-to-Wine callback glue function.
1565 * The generated routines are intended to be used as argument conversion
1566 * routines to be called by the CallFrom16... core. Thus, the prototypes of
1567 * the generated routines are (see also CallFrom16):
1569 * extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
1570 * extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
1571 * extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args,
1572 * CONTEXT86 *context );
1573 * extern void WINAPI PREFIX_CallFrom16_C_intr_xxx( FARPROC func, LPBYTE args,
1574 * CONTEXT86 *context );
1576 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
1577 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
1578 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1579 * 'T'=segmented pointer to null-terminated string).
1581 * The generated routines fetch the arguments from the 16-bit stack (pointed
1582 * to by 'args'); the offsets of the single argument values are computed
1583 * according to the calling convention and the argument types. Then, the
1584 * 32-bit entry point is called with these arguments.
1586 * For register functions, the arguments (if present) are converted just
1587 * the same as for normal functions, but in addition the CONTEXT86 pointer
1588 * filled with the current register values is passed to the 32-bit routine.
1589 * (An 'intr' interrupt handler routine is treated exactly like a register
1590 * routine, except that upon return, the flags word pushed onto the stack
1591 * by the interrupt is removed by the 16-bit call stub.)
1594 static void BuildCallFrom16Func( FILE *outfile, char *profile, char *prefix, int local )
1596 int i, pos, argsize = 0;
1600 char *args = profile + 7;
1603 /* Parse function type */
1605 if (!strncmp( "c_", profile, 2 )) usecdecl = 1;
1606 else if (strncmp( "p_", profile, 2 ))
1608 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1612 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
1613 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
1614 else if (!strncmp( "intr_", profile + 2, 5 )) reg_func = 2;
1615 else if (strncmp( "long_", profile + 2, 5 ))
1617 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1621 for ( i = 0; args[i]; i++ )
1624 case 'w': /* word */
1625 case 's': /* s_word */
1629 case 'l': /* long or segmented pointer */
1630 case 'T': /* segmented pointer to null-terminated string */
1631 case 'p': /* linear pointer */
1632 case 't': /* linear pointer to null-terminated string */
1637 ret_type = reg_func? "void" : short_ret? "WORD" : "LONG";
1639 fprintf( outfile, "typedef %s WINAPI (*proc_%s_t)( ",
1640 ret_type, profile );
1642 for ( i = 0; args[i]; i++ )
1644 if ( i ) fprintf( outfile, ", " );
1647 case 'w': fprintf( outfile, "WORD" ); break;
1648 case 's': fprintf( outfile, "INT16" ); break;
1649 case 'l': case 'T': fprintf( outfile, "LONG" ); break;
1650 case 'p': case 't': fprintf( outfile, "LPVOID" ); break;
1654 fprintf( outfile, "%sstruct _CONTEXT86 *", i? ", " : "" );
1656 fprintf( outfile, "void" );
1657 fprintf( outfile, " );\n" );
1659 fprintf( outfile, "%s%s WINAPI %s_CallFrom16_%s( FARPROC proc, LPBYTE args%s )\n{\n",
1660 local? "static " : "", ret_type, prefix, profile,
1661 reg_func? ", struct _CONTEXT86 *context" : "" );
1663 fprintf( outfile, " %s((proc_%s_t) proc) (\n",
1664 reg_func? "" : "return ", profile );
1666 pos = !usecdecl? argsize : 0;
1667 for ( i = 0; args[i]; i++ )
1669 if ( i ) fprintf( outfile, ",\n" );
1670 fprintf( outfile, " " );
1673 case 'w': /* word */
1674 if ( !usecdecl ) pos -= 2;
1675 fprintf( outfile, "*(WORD *)(args+%d)", pos );
1676 if ( usecdecl ) pos += 2;
1679 case 's': /* s_word */
1680 if ( !usecdecl ) pos -= 2;
1681 fprintf( outfile, "*(INT16 *)(args+%d)", pos );
1682 if ( usecdecl ) pos += 2;
1685 case 'l': /* long or segmented pointer */
1686 case 'T': /* segmented pointer to null-terminated string */
1687 if ( !usecdecl ) pos -= 4;
1688 fprintf( outfile, "*(LONG *)(args+%d)", pos );
1689 if ( usecdecl ) pos += 4;
1692 case 'p': /* linear pointer */
1693 case 't': /* linear pointer to null-terminated string */
1694 if ( !usecdecl ) pos -= 4;
1695 fprintf( outfile, "PTR_SEG_TO_LIN( *(SEGPTR *)(args+%d) )", pos );
1696 if ( usecdecl ) pos += 4;
1700 fprintf( stderr, "Unknown arg type '%c'\n", args[i] );
1704 fprintf( outfile, "%s context", i? ",\n" : "" );
1705 fprintf( outfile, " );\n}\n\n" );
1708 /*******************************************************************
1711 * Build a Wine-to-16-bit callback glue function.
1713 * Prototypes for the CallTo16 functions:
1714 * extern WORD CALLBACK PREFIX_CallTo16_word_xxx( FARPROC16 func, args... );
1715 * extern LONG CALLBACK PREFIX_CallTo16_long_xxx( FARPROC16 func, args... );
1717 * These routines are provided solely for convenience; they simply
1718 * write the arguments onto the 16-bit stack, and call the appropriate
1719 * CallTo16... core routine.
1721 * If you have more sophisticated argument conversion requirements than
1722 * are provided by these routines, you might as well call the core
1723 * routines by yourself.
1726 static void BuildCallTo16Func( FILE *outfile, char *profile, char *prefix )
1728 char *args = profile + 5;
1729 int i, argsize = 0, short_ret = 0;
1731 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1732 else if (strncmp( "long_", profile, 5 ))
1734 fprintf( stderr, "Invalid function name '%s'.\n", profile );
1738 fprintf( outfile, "%s %s_CallTo16_%s( FARPROC16 proc",
1739 short_ret? "WORD" : "LONG", prefix, profile );
1741 for ( i = 0; args[i]; i++ )
1743 fprintf( outfile, ", " );
1746 case 'w': fprintf( outfile, "WORD" ); argsize += 2; break;
1747 case 'l': fprintf( outfile, "LONG" ); argsize += 4; break;
1749 fprintf( outfile, " arg%d", i+1 );
1751 fprintf( outfile, " )\n{\n" );
1754 fprintf( outfile, " LPBYTE args = (LPBYTE)CURRENT_STACK16;\n" );
1757 for ( i = 0; args[i]; i++ )
1761 case 'w': fprintf( outfile, " args -= sizeof(WORD); *(WORD" ); break;
1762 case 'l': fprintf( outfile, " args -= sizeof(LONG); *(LONG" ); break;
1763 default: fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
1766 fprintf( outfile, " *)args = arg%d;\n", i+1 );
1769 fprintf( outfile, " return CallTo16%s( proc, %d );\n}\n\n",
1770 short_ret? "Word" : "Long", argsize );
1775 /*******************************************************************
1776 * BuildCallFrom16Core
1778 * This routine builds the core routines used in 16->32 thunks:
1779 * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
1781 * These routines are intended to be called via a far call (with 32-bit
1782 * operand size) from 16-bit code. The 16-bit code stub must push %bp,
1783 * the 32-bit entry point to be called, and the argument conversion
1784 * routine to be used (see stack layout below).
1786 * The core routine completes the STACK16FRAME on the 16-bit stack and
1787 * switches to the 32-bit stack. Then, the argument conversion routine
1788 * is called; it gets passed the 32-bit entry point and a pointer to the
1789 * 16-bit arguments (on the 16-bit stack) as parameters. (You can either
1790 * use conversion routines automatically generated by BuildCallFrom16,
1791 * or write your own for special purposes.)
1793 * The conversion routine must call the 32-bit entry point, passing it
1794 * the converted arguments, and return its return value to the core.
1795 * After the conversion routine has returned, the core switches back
1796 * to the 16-bit stack, converts the return value to the DX:AX format
1797 * (CallFrom16Long), and returns to the 16-bit call stub. All parameters,
1798 * including %bp, are popped off the stack.
1800 * The 16-bit call stub now returns to the caller, popping the 16-bit
1801 * arguments if necessary (pascal calling convention).
1803 * In the case of a 'register' function, CallFrom16Register fills a
1804 * CONTEXT86 structure with the values all registers had at the point
1805 * the first instruction of the 16-bit call stub was about to be
1806 * executed. A pointer to this CONTEXT86 is passed as third parameter
1807 * to the argument conversion routine, which typically passes it on
1808 * to the called 32-bit entry point.
1810 * CallFrom16Thunk is a special variant used by the implementation of
1811 * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
1812 * implemented as follows:
1813 * On entry, the EBX register is set up to contain a flat pointer to the
1814 * 16-bit stack such that EBX+22 points to the first argument.
1815 * Then, the entry point is called, while EBP is set up to point
1816 * to the return address (on the 32-bit stack).
1817 * The called function returns with CX set to the number of bytes
1818 * to be popped of the caller's stack.
1820 * Stack layout upon entry to the core routine (STACK16FRAME):
1822 * (sp+24) word first 16-bit arg
1826 * (sp+14) long 32-bit entry point (reused for Win16 mutex recursion count)
1827 * (sp+12) word ip of actual entry point (necessary for relay debugging)
1828 * (sp+8) long relay (argument conversion) function entry point
1829 * (sp+4) long cs of 16-bit entry point
1830 * (sp) long ip of 16-bit entry point
1832 * Added on the stack:
1833 * (sp-2) word saved gs
1834 * (sp-4) word saved fs
1835 * (sp-6) word saved es
1836 * (sp-8) word saved ds
1837 * (sp-12) long saved ebp
1838 * (sp-16) long saved ecx
1839 * (sp-20) long saved edx
1840 * (sp-24) long saved previous stack
1842 static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk, int short_ret )
1844 char *name = thunk? "Thunk" : reg_func? "Register" : short_ret? "Word" : "Long";
1846 /* Function header */
1847 fprintf( outfile, "\n\t.align 4\n" );
1849 fprintf( outfile, ".stabs \"CallFrom16%s:F1\",36,0,0," PREFIX "CallFrom16%s\n",
1852 fprintf( outfile, "\t.globl " PREFIX "CallFrom16%s\n", name );
1853 fprintf( outfile, PREFIX "CallFrom16%s:\n", name );
1855 /* Create STACK16FRAME (except STACK32FRAME link) */
1856 fprintf( outfile, "\tpushw %%gs\n" );
1857 fprintf( outfile, "\tpushw %%fs\n" );
1858 fprintf( outfile, "\tpushw %%es\n" );
1859 fprintf( outfile, "\tpushw %%ds\n" );
1860 fprintf( outfile, "\tpushl %%ebp\n" );
1861 fprintf( outfile, "\tpushl %%ecx\n" );
1862 fprintf( outfile, "\tpushl %%edx\n" );
1864 /* Save original EFlags register */
1865 fprintf( outfile, "\tpushfl\n" );
1869 /* Get Global Offset Table into %ecx */
1870 fprintf( outfile, "\tcall .LCallFrom16%s.getgot1\n", name );
1871 fprintf( outfile, ".LCallFrom16%s.getgot1:\n", name );
1872 fprintf( outfile, "\tpopl %%ecx\n" );
1873 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot1], %%ecx\n", name );
1876 /* Load 32-bit segment registers */
1877 fprintf( outfile, "\tmovw $0x%04x, %%dx\n", Data_Selector );
1879 fprintf( outfile, "\tdata16\n");
1881 fprintf( outfile, "\tmovw %%dx, %%ds\n" );
1883 fprintf( outfile, "\tdata16\n");
1885 fprintf( outfile, "\tmovw %%dx, %%es\n" );
1889 fprintf( outfile, "\tmovl " PREFIX "SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
1890 fprintf( outfile, "\tmovw (%%edx), %%fs\n" );
1893 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb, %%fs\n" );
1895 /* Get address of ldt_copy array into %ecx */
1897 fprintf( outfile, "\tmovl " PREFIX "ldt_copy@GOT(%%ecx), %%ecx\n" );
1899 fprintf( outfile, "\tmovl $" PREFIX "ldt_copy, %%ecx\n" );
1901 /* Translate STACK16FRAME base to flat offset in %edx */
1902 fprintf( outfile, "\tmovw %%ss, %%dx\n" );
1903 fprintf( outfile, "\tandl $0xfff8, %%edx\n" );
1904 fprintf( outfile, "\tmovl (%%ecx,%%edx), %%edx\n" );
1905 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
1906 fprintf( outfile, "\tleal (%%ebp,%%edx), %%edx\n" );
1908 /* Get saved flags into %ecx */
1909 fprintf( outfile, "\tpopl %%ecx\n" );
1911 /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
1912 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET );
1913 fprintf( outfile, "\tpushl %%ebp\n" );
1917 fprintf( outfile,"\tdata16\n");
1919 fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET + 2 );
1920 fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET );
1921 fprintf( outfile, "\tpushl %%ds\n" );
1922 fprintf( outfile, "\tpopl %%ss\n" );
1923 fprintf( outfile, "\tmovl %%ebp, %%esp\n" );
1924 fprintf( outfile, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME, ebp) );
1928 STACK16FRAME is completely set up
1929 DS, ES, SS: flat data segment
1931 ESP: points to last STACK32FRAME
1932 EBP: points to ebp member of last STACK32FRAME
1933 EDX: points to current STACK16FRAME
1934 ECX: contains saved flags
1935 all other registers: unchanged */
1937 /* Special case: C16ThkSL stub */
1940 /* Set up registers as expected and call thunk */
1941 fprintf( outfile, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME)-22 );
1942 fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
1944 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point) );
1946 /* Switch stack back */
1947 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
1948 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
1949 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
1951 /* Restore registers and return directly to caller */
1952 fprintf( outfile, "\taddl $8, %%esp\n" );
1953 fprintf( outfile, "\tpopl %%ebp\n" );
1954 fprintf( outfile, "\tpopw %%ds\n" );
1955 fprintf( outfile, "\tpopw %%es\n" );
1956 fprintf( outfile, "\tpopw %%fs\n" );
1957 fprintf( outfile, "\tpopw %%gs\n" );
1958 fprintf( outfile, "\taddl $20, %%esp\n" );
1960 fprintf( outfile, "\txorb %%ch, %%ch\n" );
1961 fprintf( outfile, "\tpopl %%ebx\n" );
1962 fprintf( outfile, "\taddw %%cx, %%sp\n" );
1963 fprintf( outfile, "\tpush %%ebx\n" );
1965 fprintf( outfile, "\t.byte 0x66\n" );
1966 fprintf( outfile, "\tlret\n" );
1972 /* Build register CONTEXT */
1975 fprintf( outfile, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86) );
1977 fprintf( outfile, "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags) );
1979 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax) );
1980 fprintf( outfile, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx) );
1981 fprintf( outfile, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi) );
1982 fprintf( outfile, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi) );
1984 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp) );
1985 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp) );
1986 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx) );
1987 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx) );
1988 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx) );
1989 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx) );
1991 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds) );
1992 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs) );
1993 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es) );
1994 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs) );
1995 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs) );
1996 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs) );
1997 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs) );
1998 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs) );
2000 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs) );
2001 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs) );
2002 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip) );
2003 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip) );
2005 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET+2 );
2006 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs) );
2007 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET );
2008 fprintf( outfile, "\taddl $%d, %%eax\n", STACK16OFFSET(ip) );
2009 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp) );
2011 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2014 /* Push address of CONTEXT86 structure -- popped by the relay routine */
2015 fprintf( outfile, "\tpushl %%esp\n" );
2019 /* Print debug info before call */
2024 fprintf( outfile, "\tpushl %%ebx\n" );
2026 /* Get Global Offset Table into %ebx (for PLT call) */
2027 fprintf( outfile, "\tcall .LCallFrom16%s.getgot2\n", name );
2028 fprintf( outfile, ".LCallFrom16%s.getgot2:\n", name );
2029 fprintf( outfile, "\tpopl %%ebx\n" );
2030 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot2], %%ebx\n", name );
2033 fprintf( outfile, "\tpushl %%edx\n" );
2035 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2036 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
2038 fprintf( outfile, "\tpushl $0\n" );
2041 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16@PLT\n ");
2043 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n ");
2045 fprintf( outfile, "\tpopl %%edx\n" );
2046 fprintf( outfile, "\tpopl %%edx\n" );
2049 fprintf( outfile, "\tpopl %%ebx\n" );
2052 /* Call relay routine (which will call the API entry point) */
2053 fprintf( outfile, "\tleal %d(%%edx), %%eax\n", sizeof(STACK16FRAME) );
2054 fprintf( outfile, "\tpushl %%eax\n" );
2055 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point) );
2056 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay) );
2058 /* Print debug info after call */
2063 fprintf( outfile, "\tpushl %%ebx\n" );
2065 /* Get Global Offset Table into %ebx (for PLT call) */
2066 fprintf( outfile, "\tcall .LCallFrom16%s.getgot3\n", name );
2067 fprintf( outfile, ".LCallFrom16%s.getgot3:\n", name );
2068 fprintf( outfile, "\tpopl %%ebx\n" );
2069 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot3], %%ebx\n", name );
2072 fprintf( outfile, "\tpushl %%eax\n" );
2074 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2075 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
2077 fprintf( outfile, "\tpushl $0\n" );
2080 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret@PLT\n ");
2082 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n ");
2084 fprintf( outfile, "\tpopl %%eax\n" );
2085 fprintf( outfile, "\tpopl %%eax\n" );
2088 fprintf( outfile, "\tpopl %%ebx\n" );
2094 fprintf( outfile, "\tmovl %%esp, %%ebx\n" );
2096 /* Switch stack back */
2097 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2098 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
2099 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2101 /* Get return address to CallFrom16 stub */
2102 fprintf( outfile, "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip)-4 );
2103 fprintf( outfile, "\tpopl %%eax\n" );
2104 fprintf( outfile, "\tpopl %%edx\n" );
2106 /* Restore all registers from CONTEXT */
2107 fprintf( outfile, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs) );
2108 fprintf( outfile, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp) );
2109 fprintf( outfile, "\taddl $4, %%esp\n" ); /* room for final return address */
2111 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
2112 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
2113 fprintf( outfile, "\tpushl %%edx\n" );
2114 fprintf( outfile, "\tpushl %%eax\n" );
2115 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags) );
2116 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
2118 fprintf( outfile, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs) );
2119 fprintf( outfile, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs) );
2120 fprintf( outfile, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs) );
2122 fprintf( outfile, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp) );
2123 fprintf( outfile, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi) );
2124 fprintf( outfile, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi) );
2125 fprintf( outfile, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax) );
2126 fprintf( outfile, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx) );
2127 fprintf( outfile, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx) );
2128 fprintf( outfile, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx) );
2130 fprintf( outfile, "\tpopl %%ds\n" );
2131 fprintf( outfile, "\tpopfl\n" );
2132 fprintf( outfile, "\tlret\n" );
2136 /* Switch stack back */
2137 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2138 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
2139 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2141 /* Restore registers */
2142 fprintf( outfile, "\tpopl %%edx\n" );
2143 fprintf( outfile, "\tpopl %%ecx\n" );
2144 fprintf( outfile, "\tpopl %%ebp\n" );
2145 fprintf( outfile, "\tpopw %%ds\n" );
2146 fprintf( outfile, "\tpopw %%es\n" );
2147 fprintf( outfile, "\tpopw %%fs\n" );
2148 fprintf( outfile, "\tpopw %%gs\n" );
2150 /* Prepare return value and set flags accordingly */
2152 fprintf( outfile, "\tshldl $16, %%eax, %%edx\n" );
2153 fprintf( outfile, "\torl %%eax, %%eax\n" );
2155 /* Return to return stub which will return to caller */
2156 fprintf( outfile, "\tlret $12\n" );
2161 /*******************************************************************
2164 * This routine builds the core routines used in 32->16 thunks:
2166 * extern void CALLBACK CallTo16Word( SEGPTR target, int nb_args );
2167 * extern void CALLBACK CallTo16Long( SEGPTR target, int nb_args );
2168 * extern void CALLBACK CallTo16RegisterShort( const CONTEXT86 *context, int nb_args );
2169 * extern void CALLBACK CallTo16RegisterLong ( const CONTEXT86 *context, int nb_args );
2171 * These routines can be called directly from 32-bit code.
2173 * All routines expect that the 16-bit stack contents (arguments) were
2174 * already set up by the caller; nb_args must contain the number of bytes
2175 * to be conserved. The 16-bit SS:SP will be set accordinly.
2177 * All other registers are either taken from the CONTEXT86 structure
2178 * or else set to default values. The target routine address is either
2179 * given directly or taken from the CONTEXT86.
2181 * If you want to call a 16-bit routine taking only standard argument types
2182 * (WORD and LONG), you can also have an appropriate argument conversion
2183 * stub automatically generated (see BuildCallTo16); you'd then call this
2184 * stub, which in turn would prepare the 16-bit stack and call the appropiate
2189 static void BuildCallTo16Core( FILE *outfile, int short_ret, int reg_func )
2191 char *name = reg_func == 2 ? "RegisterLong" :
2192 reg_func == 1 ? "RegisterShort" :
2193 short_ret? "Word" : "Long";
2195 /* Function header */
2196 fprintf( outfile, "\n\t.align 4\n" );
2198 fprintf( outfile, ".stabs \"CallTo16%s:F1\",36,0,0," PREFIX "CallTo16%s\n",
2201 fprintf( outfile, "\t.globl " PREFIX "CallTo16%s\n", name );
2202 fprintf( outfile, PREFIX "CallTo16%s:\n", name );
2204 /* Function entry sequence */
2205 fprintf( outfile, "\tpushl %%ebp\n" );
2206 fprintf( outfile, "\tmovl %%esp, %%ebp\n" );
2208 /* Save the 32-bit registers */
2209 fprintf( outfile, "\tpushl %%ebx\n" );
2210 fprintf( outfile, "\tpushl %%ecx\n" );
2211 fprintf( outfile, "\tpushl %%edx\n" );
2212 fprintf( outfile, "\tpushl %%esi\n" );
2213 fprintf( outfile, "\tpushl %%edi\n" );
2217 /* Get Global Offset Table into %ebx */
2218 fprintf( outfile, "\tcall .LCallTo16%s.getgot1\n", name );
2219 fprintf( outfile, ".LCallTo16%s.getgot1:\n", name );
2220 fprintf( outfile, "\tpopl %%ebx\n" );
2221 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot1], %%ebx\n", name );
2224 /* Enter Win16 Mutex */
2226 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock@PLT\n" );
2228 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock\n" );
2230 /* Print debugging info */
2233 /* Push flags, number of arguments, and target */
2234 fprintf( outfile, "\tpushl $%d\n", reg_func );
2235 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2236 fprintf( outfile, "\tpushl 8(%%ebp)\n" );
2239 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16@PLT\n" );
2241 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
2243 fprintf( outfile, "\taddl $12, %%esp\n" );
2246 /* Get return address */
2248 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr@GOTOFF(%%ebx), %%ecx\n" );
2250 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr, %%ecx\n" );
2252 /* Call the actual CallTo16 routine (simulate a lcall) */
2253 fprintf( outfile, "\tpushl %%cs\n" );
2254 fprintf( outfile, "\tcall .LCallTo16%s\n", name );
2256 /* Convert and push return value */
2259 fprintf( outfile, "\tmovzwl %%ax, %%eax\n" );
2260 fprintf( outfile, "\tpushl %%eax\n" );
2262 else if ( reg_func != 2 )
2264 fprintf( outfile, "\tshll $16,%%edx\n" );
2265 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
2266 fprintf( outfile, "\tpushl %%edx\n" );
2269 fprintf( outfile, "\tpushl %%eax\n" );
2273 /* Get Global Offset Table into %ebx (might have been overwritten) */
2274 fprintf( outfile, "\tcall .LCallTo16%s.getgot2\n", name );
2275 fprintf( outfile, ".LCallTo16%s.getgot2:\n", name );
2276 fprintf( outfile, "\tpopl %%ebx\n" );
2277 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot2], %%ebx\n", name );
2280 /* Print debugging info */
2284 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret@PLT\n" );
2286 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret\n" );
2289 /* Leave Win16 Mutex */
2291 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock@PLT\n" );
2293 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock\n" );
2295 /* Get return value */
2296 fprintf( outfile, "\tpopl %%eax\n" );
2298 /* Restore the 32-bit registers */
2299 fprintf( outfile, "\tpopl %%edi\n" );
2300 fprintf( outfile, "\tpopl %%esi\n" );
2301 fprintf( outfile, "\tpopl %%edx\n" );
2302 fprintf( outfile, "\tpopl %%ecx\n" );
2303 fprintf( outfile, "\tpopl %%ebx\n" );
2305 /* Function exit sequence */
2306 fprintf( outfile, "\tpopl %%ebp\n" );
2307 fprintf( outfile, "\tret $8\n" );
2310 /* Start of the actual CallTo16 routine */
2312 fprintf( outfile, ".LCallTo16%s:\n", name );
2314 /* Complete STACK32FRAME */
2315 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
2316 fprintf( outfile, "\tmovl %%esp,%%edx\n" );
2318 /* Switch to the 16-bit stack */
2320 fprintf( outfile,"\tdata16\n");
2322 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
2323 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
2324 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
2326 /* Make %bp point to the previous stackframe (built by CallFrom16) */
2327 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
2328 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp) );
2330 /* Add the specified offset to the new sp */
2331 fprintf( outfile, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args) );
2333 /* Push the return address
2334 * With sreg suffix, we push 16:16 address (normal lret)
2335 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
2338 fprintf( outfile, "\tpushl %%ecx\n" );
2341 fprintf( outfile, "\tshldl $16, %%ecx, %%eax\n" );
2342 fprintf( outfile, "\tpushw $0\n" );
2343 fprintf( outfile, "\tpushw %%ax\n" );
2344 fprintf( outfile, "\tpushw $0\n" );
2345 fprintf( outfile, "\tpushw %%cx\n" );
2350 /* Push the called routine address */
2351 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target) );
2352 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs) );
2353 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip) );
2355 /* Get the registers */
2356 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs) );
2357 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs) );
2358 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2359 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs) );
2360 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2361 fprintf( outfile, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp) );
2362 fprintf( outfile, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi) );
2363 fprintf( outfile, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi) );
2364 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax) );
2365 fprintf( outfile, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx) );
2366 fprintf( outfile, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx) );
2367 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx) );
2369 /* Get the 16-bit ds */
2370 fprintf( outfile, "\tpopw %%ds\n" );
2372 else /* not a register function */
2374 /* Push the called routine address */
2375 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK32OFFSET(target) );
2377 /* Set %fs to the value saved by the last CallFrom16 */
2378 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs)-STACK16OFFSET(bp) );
2379 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2381 /* Set %ds and %es (and %ax just in case) equal to %ss */
2382 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
2383 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2384 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2387 /* Jump to the called routine */
2388 fprintf( outfile, "\t.byte 0x66\n" );
2389 fprintf( outfile, "\tlret\n" );
2392 /*******************************************************************
2395 * Build the return code for 16-bit callbacks
2397 static void BuildRet16Func( FILE *outfile )
2400 * Note: This must reside in the .data section to allow
2401 * run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
2404 fprintf( outfile, "\n\t.globl " PREFIX "CallTo16_Ret\n" );
2405 fprintf( outfile, PREFIX "CallTo16_Ret:\n" );
2407 /* Restore 32-bit segment registers */
2409 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
2411 fprintf( outfile, "\tdata16\n");
2413 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
2415 fprintf( outfile, "\tdata16\n");
2417 fprintf( outfile, "\tmovw %%bx,%%es\n" );
2419 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
2421 /* Restore the 32-bit stack */
2424 fprintf( outfile, "\tdata16\n");
2426 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
2427 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
2428 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2430 /* Return to caller */
2432 fprintf( outfile, "\tlret\n" );
2434 /* Declare the return address variable */
2436 fprintf( outfile, "\n\t.globl " PREFIX "CallTo16_RetAddr\n" );
2437 fprintf( outfile, PREFIX "CallTo16_RetAddr:\t.long 0\n" );
2440 /*******************************************************************
2441 * BuildCallTo32CBClient
2443 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
2445 * Since the relay stub is itself 32-bit, this should not be a problem;
2446 * unfortunately, the relay stubs are expected to switch back to a
2447 * 16-bit stack (and 16-bit code) after completion :-(
2449 * This would conflict with our 16- vs. 32-bit stack handling, so
2450 * we simply switch *back* to our 32-bit stack before returning to
2453 * The CBClient relay stub expects to be called with the following
2454 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
2455 * stack at the designated places:
2458 * (ebp+14) original arguments to the callback routine
2459 * (ebp+10) far return address to original caller
2460 * (ebp+6) Thunklet target address
2461 * (ebp+2) Thunklet relay ID code
2462 * (ebp) BP (saved by CBClientGlueSL)
2463 * (ebp-2) SI (saved by CBClientGlueSL)
2464 * (ebp-4) DI (saved by CBClientGlueSL)
2465 * (ebp-6) DS (saved by CBClientGlueSL)
2467 * ... buffer space used by the 16-bit side glue for temp copies
2469 * (ebx+4) far return address to 16-bit side glue code
2470 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
2472 * The 32-bit side glue code accesses both the original arguments (via ebp)
2473 * and the temporary copies prepared by the 16-bit side glue (via ebx).
2474 * After completion, the stub will load ss:sp from the buffer at ebx
2475 * and perform a far return to 16-bit code.
2477 * To trick the relay stub into returning to us, we replace the 16-bit
2478 * return address to the glue code by a cs:ip pair pointing to our
2479 * return entry point (the original return address is saved first).
2480 * Our return stub thus called will then reload the 32-bit ss:esp and
2481 * return to 32-bit code (by using and ss:esp value that we have also
2482 * pushed onto the 16-bit stack before and a cs:eip values found at
2483 * that position on the 32-bit stack). The ss:esp to be restored is
2484 * found relative to the 16-bit stack pointer at:
2487 * (ebx-8) sp (32-bit stack pointer)
2489 * The second variant of this routine, CALL32_CBClientEx, which is used
2490 * to implement KERNEL.621, has to cope with yet another problem: Here,
2491 * the 32-bit side directly returns to the caller of the CBClient thunklet,
2492 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
2493 * As we have to return to our 32-bit code first, we have to adapt the
2494 * layout of our temporary area so as to include values for the registers
2495 * that are to be restored, and later (in the implementation of KERNEL.621)
2496 * we *really* restore them. The return stub restores DS, DI, SI, and BP
2497 * from the stack, skips the next 8 bytes (CBClient relay code / target),
2498 * and then performs a lret NN, where NN is the number of arguments to be
2499 * removed. Thus, we prepare our temporary area as follows:
2501 * (ebx+22) 16-bit cs (this segment)
2502 * (ebx+20) 16-bit ip ('16-bit' return entry point)
2503 * (ebx+16) 32-bit ss (flat)
2504 * (ebx+12) 32-bit sp (32-bit stack pointer)
2505 * (ebx+10) 16-bit bp (points to ebx+24)
2506 * (ebx+8) 16-bit si (ignored)
2507 * (ebx+6) 16-bit di (ignored)
2508 * (ebx+4) 16-bit ds (we actually use the flat DS here)
2509 * (ebx+2) 16-bit ss (16-bit stack segment)
2510 * (ebx+0) 16-bit sp (points to ebx+4)
2512 * Note that we ensure that DS is not changed and remains the flat segment,
2513 * and the 32-bit stack pointer our own return stub needs fits just
2514 * perfectly into the 8 bytes that are skipped by the Windows stub.
2515 * One problem is that we have to determine the number of removed arguments,
2516 * as these have to be really removed in KERNEL.621. Thus, the BP value
2517 * that we place in the temporary area to be restored, contains the value
2518 * that SP would have if no arguments were removed. By comparing the actual
2519 * value of SP with this value in our return stub we can compute the number
2520 * of removed arguments. This is then returned to KERNEL.621.
2522 * The stack layout of this function:
2523 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
2524 * (ebp+16) esi pointer to caller's esi value
2525 * (ebp+12) arg ebp value to be set for relay stub
2526 * (ebp+8) func CBClient relay stub address
2530 static void BuildCallTo32CBClient( FILE *outfile, BOOL isEx )
2532 char *name = isEx? "CBClientEx" : "CBClient";
2533 int size = isEx? 24 : 12;
2535 /* Function header */
2537 fprintf( outfile, "\n\t.align 4\n" );
2539 fprintf( outfile, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX "CALL32_%s\n",
2542 fprintf( outfile, "\t.globl " PREFIX "CALL32_%s\n", name );
2543 fprintf( outfile, PREFIX "CALL32_%s:\n", name );
2547 fprintf( outfile, "\tpushl %%ebp\n" );
2548 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2549 fprintf( outfile, "\tpushl %%edi\n" );
2550 fprintf( outfile, "\tpushl %%esi\n" );
2551 fprintf( outfile, "\tpushl %%ebx\n" );
2553 /* Get the 16-bit stack */
2555 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET);
2557 /* Convert it to a flat address */
2559 fprintf( outfile, "\tshldl $16,%%ebx,%%eax\n" );
2560 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
2561 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%esi\n" );
2562 fprintf( outfile, "\tmovw %%bx,%%ax\n" );
2563 fprintf( outfile, "\taddl %%eax,%%esi\n" );
2565 /* Allocate temporary area (simulate STACK16_PUSH) */
2567 fprintf( outfile, "\tpushf\n" );
2568 fprintf( outfile, "\tcld\n" );
2569 fprintf( outfile, "\tleal -%d(%%esi), %%edi\n", size );
2570 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2571 fprintf( outfile, "\trep\n\tmovsb\n" );
2572 fprintf( outfile, "\tpopf\n" );
2574 fprintf( outfile, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size, STACKOFFSET );
2576 fprintf( outfile, "\tpushl %%edi\n" ); /* remember address */
2578 /* Set up temporary area */
2582 fprintf( outfile, "\tleal 4(%%edi), %%edi\n" );
2584 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2585 fprintf( outfile, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */
2587 fprintf( outfile, "\tmovl %%ss, %%ax\n" );
2588 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2589 fprintf( outfile, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */
2591 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 + 4 );
2592 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */
2594 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
2595 fprintf( outfile, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */
2599 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 );
2600 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" );
2602 fprintf( outfile, "\tmovl %%ds, %%ax\n" );
2603 fprintf( outfile, "\tmovw %%ax, 4(%%edi)\n" );
2605 fprintf( outfile, "\taddl $20, %%ebx\n" );
2606 fprintf( outfile, "\tmovw %%bx, 10(%%edi)\n" );
2608 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2609 fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
2611 fprintf( outfile, "\tmovl %%ss, %%ax\n" );
2612 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2613 fprintf( outfile, "\tmovl %%eax, 16(%%edi)\n" );
2615 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
2616 fprintf( outfile, "\tmovl %%eax, 20(%%edi)\n" );
2619 /* Set up registers and call CBClient relay stub (simulating a far call) */
2621 fprintf( outfile, "\tmovl 16(%%ebp), %%esi\n" );
2622 fprintf( outfile, "\tmovl (%%esi), %%esi\n" );
2624 fprintf( outfile, "\tmovl %%edi, %%ebx\n" );
2625 fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
2626 fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
2628 fprintf( outfile, "\tpushl %%cs\n" );
2629 fprintf( outfile, "\tcall *%%eax\n" );
2631 /* Return new esi value to caller */
2633 fprintf( outfile, "\tmovl 32(%%esp), %%edi\n" );
2634 fprintf( outfile, "\tmovl %%esi, (%%edi)\n" );
2636 /* Cleanup temporary area (simulate STACK16_POP) */
2638 fprintf( outfile, "\tpop %%esi\n" );
2640 fprintf( outfile, "\tpushf\n" );
2641 fprintf( outfile, "\tstd\n" );
2642 fprintf( outfile, "\tdec %%esi\n" );
2643 fprintf( outfile, "\tleal %d(%%esi), %%edi\n", size );
2644 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2645 fprintf( outfile, "\trep\n\tmovsb\n" );
2646 fprintf( outfile, "\tpopf\n" );
2648 fprintf( outfile, "\t.byte 0x64\n\taddw $%d,(%d)\n", size, STACKOFFSET );
2650 /* Return argument size to caller */
2653 fprintf( outfile, "\tmovl 32(%%esp), %%ebx\n" );
2654 fprintf( outfile, "\tmovl %%ebp, (%%ebx)\n" );
2657 /* Restore registers and return */
2659 fprintf( outfile, "\tpopl %%ebx\n" );
2660 fprintf( outfile, "\tpopl %%esi\n" );
2661 fprintf( outfile, "\tpopl %%edi\n" );
2662 fprintf( outfile, "\tpopl %%ebp\n" );
2663 fprintf( outfile, "\tret\n" );
2666 static void BuildCallTo32CBClientRet( FILE *outfile, BOOL isEx )
2668 char *name = isEx? "CBClientEx" : "CBClient";
2670 /* '16-bit' return stub */
2672 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_Ret\n", name );
2673 fprintf( outfile, PREFIX "CALL32_%s_Ret:\n", name );
2677 fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
2678 fprintf( outfile, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
2682 fprintf( outfile, "\tmovzwl %%bp, %%ebx\n" );
2683 fprintf( outfile, "\tsubw %%bp, %%sp\n" );
2684 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
2685 fprintf( outfile, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
2687 fprintf( outfile, "\tlret\n" );
2689 /* Declare the return address variable */
2691 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_RetAddr\n", name );
2692 fprintf( outfile, PREFIX "CALL32_%s_RetAddr:\t.long 0\n", name );
2696 /*******************************************************************
2697 * BuildCallTo32LargeStack
2699 * Build the function used to switch to the original 32-bit stack
2700 * before calling a 32-bit function from 32-bit code. This is used for
2701 * functions that need a large stack, like X bitmaps functions.
2703 * The generated function has the following prototype:
2704 * int xxx( int (*func)(), void *arg );
2706 * The pointer to the function can be retrieved by calling CALL32_Init,
2707 * which also takes care of saving the current 32-bit stack pointer.
2708 * Furthermore, CALL32_Init switches to a new stack and jumps to the
2709 * specified target address.
2711 * NOTE: The CALL32_LargeStack routine may be recursively entered by the
2712 * same thread, but not concurrently entered by several threads.
2714 * Stack layout of CALL32_Init:
2716 * (esp+12) new stack address
2717 * (esp+8) target address
2718 * (esp+4) pointer to variable to receive CALL32_LargeStack address
2721 * Stack layout of CALL32_LargeStack:
2728 static void BuildCallTo32LargeStack( FILE *outfile )
2730 /* Initialization function */
2732 fprintf( outfile, "\n\t.align 4\n" );
2734 fprintf( outfile, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX "CALL32_Init\n");
2736 fprintf( outfile, "\t.globl " PREFIX "CALL32_Init\n" );
2737 fprintf( outfile, "\t.type " PREFIX "CALL32_Init,@function\n" );
2738 fprintf( outfile, PREFIX "CALL32_Init:\n" );
2739 fprintf( outfile, "\tmovl %%esp,CALL32_Original32_esp\n" );
2740 fprintf( outfile, "\tpopl %%eax\n" );
2741 fprintf( outfile, "\tpopl %%eax\n" );
2742 fprintf( outfile, "\tmovl $CALL32_LargeStack,(%%eax)\n" );
2743 fprintf( outfile, "\tpopl %%eax\n" );
2744 fprintf( outfile, "\tpopl %%esp\n" );
2745 fprintf( outfile, "\tpushl %%eax\n" );
2746 fprintf( outfile, "\tret\n" );
2748 /* Function header */
2750 fprintf( outfile, "\n\t.align 4\n" );
2752 fprintf( outfile, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2754 fprintf( outfile, "CALL32_LargeStack:\n" );
2758 fprintf( outfile, "\tpushl %%ebp\n" );
2759 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2761 /* Switch to the original 32-bit stack pointer */
2763 fprintf( outfile, "\tcmpl $0, CALL32_RecursionCount\n" );
2764 fprintf( outfile, "\tjne CALL32_skip\n" );
2765 fprintf( outfile, "\tmovl CALL32_Original32_esp, %%esp\n" );
2766 fprintf( outfile, "CALL32_skip:\n" );
2768 fprintf( outfile, "\tincl CALL32_RecursionCount\n" );
2770 /* Transfer the argument and call the function */
2772 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2773 fprintf( outfile, "\tcall *8(%%ebp)\n" );
2775 /* Restore registers and return */
2777 fprintf( outfile, "\tdecl CALL32_RecursionCount\n" );
2779 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2780 fprintf( outfile, "\tpopl %%ebp\n" );
2781 fprintf( outfile, "\tret\n" );
2785 fprintf( outfile, "\t.data\n" );
2786 fprintf( outfile, "CALL32_Original32_esp:\t.long 0\n" );
2787 fprintf( outfile, "CALL32_RecursionCount:\t.long 0\n" );
2788 fprintf( outfile, "\t.text\n" );
2792 /*******************************************************************
2793 * BuildCallFrom32Regs
2795 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2796 * 'args' is the number of dword arguments.
2800 * (ebp+12) first arg
2801 * (ebp+8) ret addr to user code
2802 * (ebp+4) ret addr to relay code
2804 * (ebp-128) buffer area to allow stack frame manipulation
2805 * (ebp-332) CONTEXT86 struct
2806 * (ebp-336) CONTEXT86 *argument
2807 * .... other arguments copied from (ebp+12)
2809 * The entry point routine is called with a CONTEXT* extra argument,
2810 * following the normal args. In this context structure, EIP_reg
2811 * contains the return address to user code, and ESP_reg the stack
2812 * pointer on return (with the return address and arguments already
2815 static void BuildCallFrom32Regs( FILE *outfile )
2817 static const int STACK_SPACE = 128 + sizeof(CONTEXT86);
2819 /* Function header */
2821 fprintf( outfile, "\n\t.align 4\n" );
2823 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
2825 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
2826 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
2828 /* Allocate some buffer space on the stack */
2830 fprintf( outfile, "\tpushl %%ebp\n" );
2831 fprintf( outfile, "\tmovl %%esp,%%ebp\n ");
2832 fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
2834 /* Build the context structure */
2836 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
2837 fprintf( outfile, "\tpushfl\n" );
2838 fprintf( outfile, "\tpopl %%eax\n" );
2839 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
2840 fprintf( outfile, "\tmovl 0(%%ebp),%%eax\n" );
2841 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
2842 fprintf( outfile, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
2843 fprintf( outfile, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
2844 fprintf( outfile, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
2845 fprintf( outfile, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
2846 fprintf( outfile, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
2848 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2849 fprintf( outfile, "\tmovw %%cs,%%ax\n" );
2850 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs) - STACK_SPACE );
2851 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2852 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
2853 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2854 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
2855 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2856 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
2857 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
2858 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs) - STACK_SPACE );
2859 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2860 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs) - STACK_SPACE );
2861 fprintf( outfile, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
2863 fprintf( outfile, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL );
2864 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags) - STACK_SPACE );
2866 fprintf( outfile, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
2867 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
2869 /* Transfer the arguments */
2871 fprintf( outfile, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
2872 fprintf( outfile, "\tpushl %%esp\n" ); /* push ptr to context struct */
2873 fprintf( outfile, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
2874 fprintf( outfile, "\tjecxz 1f\n" );
2875 fprintf( outfile, "\tsubl %%ecx,%%esp\n" );
2876 fprintf( outfile, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */
2877 fprintf( outfile, "\tmovl %%esp,%%edi\n" );
2878 fprintf( outfile, "\tshrl $2,%%ecx\n" );
2879 fprintf( outfile, "\tcld\n" );
2880 fprintf( outfile, "\trep\n\tmovsl\n" ); /* copy args */
2882 fprintf( outfile, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
2883 fprintf( outfile, "\tleal 12(%%ebp,%%eax),%%eax\n" );
2884 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2886 /* Call the entry point */
2888 fprintf( outfile, "\tcall *0(%%ebx)\n" );
2890 /* Store %eip and %ebp onto the new stack */
2892 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2893 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
2894 fprintf( outfile, "\tmovl %%eax,-4(%%edx)\n" );
2895 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
2896 fprintf( outfile, "\tmovl %%eax,-8(%%edx)\n" );
2898 /* Restore the context structure */
2900 /* Note: we don't bother to restore %cs, %ds and %ss
2901 * changing them in 32-bit code is a recipe for disaster anyway
2903 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
2904 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2905 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
2906 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2907 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
2908 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2910 fprintf( outfile, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
2911 fprintf( outfile, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
2912 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
2913 fprintf( outfile, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
2914 fprintf( outfile, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
2916 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
2917 fprintf( outfile, "\tpushl %%eax\n" );
2918 fprintf( outfile, "\tpopfl\n" );
2919 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
2921 fprintf( outfile, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2922 fprintf( outfile, "\tleal -8(%%ebp),%%esp\n" );
2923 fprintf( outfile, "\tpopl %%ebp\n" );
2924 fprintf( outfile, "\tret\n" );
2928 /*******************************************************************
2931 * Build the spec files
2933 static int BuildSpec( FILE *outfile, int argc, char *argv[] )
2936 for (i = 2; i < argc; i++)
2937 if (BuildSpecFile( outfile, argv[i] ) < 0) return -1;
2941 /*******************************************************************
2944 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
2946 static int BuildGlue( FILE *outfile, char * outname, int argc, char *argv[] )
2953 infile = fopen( argv[2], "r" );
2960 else infile = stdin;
2964 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
2965 argc > 2? argv[2] : "<stdin>" );
2966 fprintf( outfile, "#include \"builtin16.h\"\n" );
2967 fprintf( outfile, "#include \"stackframe.h\"\n\n" );
2969 /* Build the callback glue functions */
2971 while (fgets( buffer, sizeof(buffer), infile ))
2973 if (strstr( buffer, "### start build ###" )) break;
2975 while (fgets( buffer, sizeof(buffer), infile ))
2978 if ( (p = strstr( buffer, "CallFrom16_" )) != NULL )
2980 char *q, *profile = p + strlen( "CallFrom16_" );
2981 for (q = profile; (*q == '_') || isalpha(*q); q++ )
2984 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
2986 if ( ++q < p ) p[-1] = '\0'; else q = "";
2987 BuildCallFrom16Func( outfile, profile, q, FALSE );
2989 if ( (p = strstr( buffer, "CallTo16_" )) != NULL )
2991 char *q, *profile = p + strlen( "CallTo16_" );
2992 for (q = profile; (*q == '_') || isalpha(*q); q++ )
2995 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
2997 if ( ++q < p ) p[-1] = '\0'; else q = "";
2998 BuildCallTo16Func( outfile, profile, q );
3000 if (strstr( buffer, "### stop build ###" )) break;
3007 /*******************************************************************
3010 * Build the 16-bit callbacks
3012 static int BuildCall16( FILE *outfile, char * outname )
3020 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
3021 fprintf( outfile, "\t.text\n" );
3026 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
3027 getcwd(buffer, sizeof(buffer));
3030 * The stabs help the internal debugger as they are an indication that it
3031 * is sensible to step into a thunk/trampoline.
3033 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
3034 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
3035 fprintf( outfile, "\t.text\n" );
3036 fprintf( outfile, "\t.align 4\n" );
3037 fprintf( outfile, "Code_Start:\n\n" );
3039 fprintf( outfile, PREFIX"Call16_Start:\n" );
3040 fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
3041 fprintf( outfile, "\t.byte 0\n\n" );
3044 /* Standard CallFrom16 routine (WORD return) */
3045 BuildCallFrom16Core( outfile, FALSE, FALSE, TRUE );
3047 /* Standard CallFrom16 routine (DWORD return) */
3048 BuildCallFrom16Core( outfile, FALSE, FALSE, FALSE );
3050 /* Register CallFrom16 routine */
3051 BuildCallFrom16Core( outfile, TRUE, FALSE, FALSE );
3053 /* C16ThkSL CallFrom16 routine */
3054 BuildCallFrom16Core( outfile, FALSE, TRUE, FALSE );
3056 /* Standard CallTo16 routine (WORD return) */
3057 BuildCallTo16Core( outfile, TRUE, FALSE );
3059 /* Standard CallTo16 routine (DWORD return) */
3060 BuildCallTo16Core( outfile, FALSE, FALSE );
3062 /* Register CallTo16 routine (16:16 retf) */
3063 BuildCallTo16Core( outfile, FALSE, 1 );
3065 /* Register CallTo16 routine (16:32 retf) */
3066 BuildCallTo16Core( outfile, FALSE, 2 );
3068 /* CBClientThunkSL routine */
3069 BuildCallTo32CBClient( outfile, FALSE );
3071 /* CBClientThunkSLEx routine */
3072 BuildCallTo32CBClient( outfile, TRUE );
3074 fprintf( outfile, PREFIX"Call16_End:\n" );
3075 fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
3078 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
3079 fprintf( outfile, ".Letext:\n");
3082 /* The whole Call16_Ret segment must lie within the .data section */
3083 fprintf( outfile, "\n\t.data\n" );
3084 fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
3085 fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
3087 /* Standard CallTo16 return stub */
3088 BuildRet16Func( outfile );
3090 /* CBClientThunkSL return stub */
3091 BuildCallTo32CBClientRet( outfile, FALSE );
3093 /* CBClientThunkSLEx return stub */
3094 BuildCallTo32CBClientRet( outfile, TRUE );
3096 /* End of Call16_Ret segment */
3097 fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
3098 fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
3100 #else /* __i386__ */
3102 fprintf( outfile, PREFIX"Call16_Start:\n" );
3103 fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
3104 fprintf( outfile, "\t.byte 0\n\n" );
3105 fprintf( outfile, PREFIX"Call16_End:\n" );
3106 fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
3108 fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
3109 fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
3110 fprintf( outfile, "\t.byte 0\n\n" );
3111 fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
3112 fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
3114 #endif /* __i386__ */
3119 /*******************************************************************
3122 * Build the 32-bit callbacks
3124 static int BuildCall32( FILE *outfile, char * outname )
3132 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
3133 fprintf( outfile, "\t.text\n" );
3138 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
3139 getcwd(buffer, sizeof(buffer));
3142 * The stabs help the internal debugger as they are an indication that it
3143 * is sensible to step into a thunk/trampoline.
3145 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
3146 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
3147 fprintf( outfile, "\t.text\n" );
3148 fprintf( outfile, "\t.align 4\n" );
3149 fprintf( outfile, "Code_Start:\n" );
3152 /* Build the 32-bit large stack callback */
3154 BuildCallTo32LargeStack( outfile );
3156 /* Build the register callback function */
3158 BuildCallFrom32Regs( outfile );
3161 fprintf( outfile, "\t.text\n");
3162 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
3163 fprintf( outfile, ".Letext:\n");
3166 #else /* __i386__ */
3168 /* Just to avoid an empty file */
3169 fprintf( outfile, "\t.long 0\n" );
3171 #endif /* __i386__ */
3176 /*******************************************************************
3179 static void usage(void)
3182 "usage: build [-pic] [-o outfile] -spec SPECNAMES\n"
3183 " build [-pic] [-o outfile] -glue SOURCE_FILE\n"
3184 " build [-pic] [-o outfile] -call16\n"
3185 " build [-pic] [-o outfile] -call32\n" );
3190 /*******************************************************************
3193 int main(int argc, char **argv)
3195 char *outname = NULL;
3196 FILE *outfile = stdout;
3199 if (argc < 2) usage();
3201 if (!strcmp( argv[1], "-pic" ))
3206 if (argc < 2) usage();
3209 if (!strcmp( argv[1], "-o" ))
3214 if (argc < 2) usage();
3215 if (!(outfile = fopen( outname, "w" )))
3217 fprintf( stderr, "Unable to create output file '%s'\n", outname );
3222 /* Retrieve the selector values; this assumes that we are building
3223 * the asm files on the platform that will also run them. Probably
3224 * a safe assumption to make.
3226 GET_CS( Code_Selector );
3227 GET_DS( Data_Selector );
3229 if (!strcmp( argv[1], "-spec" ))
3230 res = BuildSpec( outfile, argc, argv );
3231 else if (!strcmp( argv[1], "-glue" ))
3232 res = BuildGlue( outfile, outname, argc, argv );
3233 else if (!strcmp( argv[1], "-call16" ))
3234 res = BuildCall16( outfile, outname );
3235 else if (!strcmp( argv[1], "-call32" ))
3236 res = BuildCall32( outfile, outname );