2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Martin von Loewis
4 * Copyright 1995, 1996, 1997 Alexandre Julliard
5 * Copyright 1997 Eric Youngdale
19 #include "selectors.h"
20 #include "stackframe.h"
23 #ifdef NEED_UNDERSCORE_PREFIX
29 #ifdef HAVE_ASM_STRING
30 # define STRING ".string"
32 # define STRING ".ascii"
35 #if defined(__GNUC__) && !defined(__svr4__)
44 TYPE_BYTE, /* byte variable (Win16) */
45 TYPE_WORD, /* word variable (Win16) */
46 TYPE_LONG, /* long variable (Win16) */
47 TYPE_PASCAL_16, /* pascal function with 16-bit return (Win16) */
48 TYPE_PASCAL, /* pascal function with 32-bit return (Win16) */
49 TYPE_ABS, /* absolute value (Win16) */
50 TYPE_RETURN, /* simple return value function (Win16) */
51 TYPE_REGISTER, /* register function */
52 TYPE_STUB, /* unimplemented stub */
53 TYPE_STDCALL, /* stdcall function (Win32) */
54 TYPE_CDECL, /* cdecl function (Win32) */
55 TYPE_VARARGS, /* varargs function (Win32) */
56 TYPE_EXTERN, /* external symbol (Win32) */
60 static const char * const TypeNames[TYPE_NBTYPES] =
63 "byte", /* TYPE_BYTE */
64 "word", /* TYPE_WORD */
65 "long", /* TYPE_LONG */
66 "pascal16", /* TYPE_PASCAL_16 */
67 "pascal", /* TYPE_PASCAL */
68 "equate", /* TYPE_ABS */
69 "return", /* TYPE_RETURN */
70 "register", /* TYPE_REGISTER */
71 "stub", /* TYPE_STUB */
72 "stdcall", /* TYPE_STDCALL */
73 "cdecl", /* TYPE_CDECL */
74 "varargs", /* TYPE_VARARGS */
75 "extern" /* TYPE_EXTERN */
78 #define MAX_ORDINALS 2048
80 /* Callback function used for stub functions */
81 #define STUB_CALLBACK \
82 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
142 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
144 static SPEC_TYPE SpecType = SPEC_INVALID;
145 static char DLLName[80];
146 static char DLLFileName[80];
147 static int Limit = 0;
148 static int Base = MAX_ORDINALS;
149 static int DLLHeapSize = 0;
150 static char *SpecName;
152 static WORD Code_Selector, Data_Selector;
153 static char DLLInitFunc[80];
155 char *ParseBuffer = NULL;
160 static int debugging = 1;
162 /* Offset of a structure field relative to the start of the struct */
163 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
165 /* Offset of register relative to the start of the CONTEXT struct */
166 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT,reg)
168 /* Offset of the stack pointer relative to %fs:(0) */
169 #define STACKOFFSET (STRUCTOFFSET(THDB,cur_stack) - STRUCTOFFSET(THDB,teb))
172 static void *xmalloc (size_t size)
176 res = malloc (size ? size : 1);
179 fprintf (stderr, "Virtual memory exhausted.\n");
186 static void *xrealloc (void *ptr, size_t size)
188 void *res = realloc (ptr, size);
191 fprintf (stderr, "Virtual memory exhausted.\n");
198 static int IsNumberString(char *s)
207 static char *strupper(char *s)
211 for(p = s; *p != '\0'; p++)
217 static char * GetTokenInLine(void)
222 if (ParseNext != ParseBuffer)
224 if (ParseSaveChar == '\0')
226 *ParseNext = ParseSaveChar;
230 * Remove initial white space.
232 for (p = ParseNext; isspace(*p); p++)
235 if ((*p == '\0') || (*p == '#'))
242 if (*token != '(' && *token != ')')
243 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
253 static char * GetToken(void)
257 if (ParseBuffer == NULL)
259 ParseBuffer = xmalloc(512);
260 ParseNext = ParseBuffer;
264 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
266 if (ParseBuffer[0] != '#')
271 while ((token = GetTokenInLine()) == NULL)
273 ParseNext = ParseBuffer;
277 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
279 if (ParseBuffer[0] != '#')
288 /*******************************************************************
291 * Parse a variable definition.
293 static int ParseVariable( ORDDEF *odp )
298 int value_array_size;
300 char *token = GetToken();
303 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
304 SpecName, Line, token);
309 value_array_size = 25;
310 value_array = xmalloc(sizeof(*value_array) * value_array_size);
312 while ((token = GetToken()) != NULL)
317 value_array[n_values++] = strtol(token, &endptr, 0);
318 if (n_values == value_array_size)
320 value_array_size += 25;
321 value_array = xrealloc(value_array,
322 sizeof(*value_array) * value_array_size);
325 if (endptr == NULL || *endptr != '\0')
327 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
328 SpecName, Line, token);
335 fprintf(stderr, "%s:%d: End of file in variable declaration\n",
340 odp->u.var.n_values = n_values;
341 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
347 /*******************************************************************
348 * ParseExportFunction
350 * Parse a function definition.
352 static int ParseExportFunction( ORDDEF *odp )
360 if (odp->type == TYPE_STDCALL)
362 fprintf( stderr, "%s:%d: 'stdcall' not supported for Win16\n",
368 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
370 fprintf( stderr, "%s:%d: 'pascal' not supported for Win32\n",
382 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
383 SpecName, Line, token);
387 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
393 if (!strcmp(token, "word"))
394 odp->u.func.arg_types[i] = 'w';
395 else if (!strcmp(token, "s_word"))
396 odp->u.func.arg_types[i] = 's';
397 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
398 odp->u.func.arg_types[i] = 'l';
399 else if (!strcmp(token, "ptr"))
400 odp->u.func.arg_types[i] = 'p';
401 else if (!strcmp(token, "str"))
402 odp->u.func.arg_types[i] = 't';
403 else if (!strcmp(token, "wstr"))
404 odp->u.func.arg_types[i] = 'W';
405 else if (!strcmp(token, "segstr"))
406 odp->u.func.arg_types[i] = 'T';
407 else if (!strcmp(token, "double"))
409 odp->u.func.arg_types[i++] = 'l';
410 odp->u.func.arg_types[i] = 'l';
414 fprintf(stderr, "%s:%d: Unknown variable type '%s'\n",
415 SpecName, Line, token);
418 if (SpecType == SPEC_WIN32)
420 if (strcmp(token, "long") &&
421 strcmp(token, "ptr") &&
422 strcmp(token, "str") &&
423 strcmp(token, "wstr") &&
424 strcmp(token, "double"))
426 fprintf( stderr, "%s:%d: Type '%s' not supported for Win32\n",
427 SpecName, Line, token );
432 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
434 fprintf( stderr, "%s:%d: Too many arguments\n", SpecName, Line );
437 odp->u.func.arg_types[i] = '\0';
438 if ((odp->type == TYPE_STDCALL) && !i)
439 odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
440 if ((odp->type == TYPE_REGISTER) && (SpecType == SPEC_WIN32) && i)
442 fprintf( stderr, "%s:%d: register functions cannot have arguments in Win32\n",
446 strcpy(odp->u.func.link_name, GetToken());
451 /*******************************************************************
454 * Parse an 'equate' definition.
456 static int ParseEquate( ORDDEF *odp )
460 char *token = GetToken();
461 int value = strtol(token, &endptr, 0);
462 if (endptr == NULL || *endptr != '\0')
464 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
465 SpecName, Line, token);
469 if (SpecType == SPEC_WIN32)
471 fprintf( stderr, "%s:%d: 'equate' not supported for Win32\n",
476 odp->u.abs.value = value;
481 /*******************************************************************
484 * Parse a 'return' definition.
486 static int ParseReturn( ORDDEF *odp )
492 odp->u.ret.arg_size = strtol(token, &endptr, 0);
493 if (endptr == NULL || *endptr != '\0')
495 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
496 SpecName, Line, token);
501 odp->u.ret.ret_value = strtol(token, &endptr, 0);
502 if (endptr == NULL || *endptr != '\0')
504 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
505 SpecName, Line, token);
509 if (SpecType == SPEC_WIN32)
511 fprintf( stderr, "%s:%d: 'return' not supported for Win32\n",
520 /*******************************************************************
523 * Parse a 'stub' definition.
525 static int ParseStub( ORDDEF *odp )
527 odp->u.func.arg_types[0] = '\0';
528 strcpy( odp->u.func.link_name, STUB_CALLBACK );
533 /*******************************************************************
536 * Parse an 'varargs' definition.
538 static int ParseVarargs( ORDDEF *odp )
542 if (SpecType == SPEC_WIN16)
544 fprintf( stderr, "%s:%d: 'varargs' not supported for Win16\n",
552 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
553 SpecName, Line, token);
559 fprintf(stderr, "%s:%d: Expected ')' got '%s'\n",
560 SpecName, Line, token);
564 strcpy( odp->u.vargs.link_name, GetToken() );
569 /*******************************************************************
572 * Parse an 'extern' definition.
574 static int ParseExtern( ORDDEF *odp )
576 if (SpecType == SPEC_WIN16)
578 fprintf( stderr, "%s:%d: 'extern' not supported for Win16\n",
582 strcpy( odp->u.ext.link_name, GetToken() );
587 /*******************************************************************
590 * Parse an ordinal definition.
592 static int ParseOrdinal(int ordinal)
597 if (ordinal >= MAX_ORDINALS)
599 fprintf(stderr, "%s:%d: Ordinal number too large\n", SpecName, Line );
602 if (ordinal > Limit) Limit = ordinal;
603 if (ordinal < Base) Base = ordinal;
605 odp = &OrdinalDefinitions[ordinal];
606 if (!(token = GetToken()))
608 fprintf(stderr, "%s:%d: Expected type after ordinal\n", SpecName, Line);
612 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
613 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
616 if (odp->type >= TYPE_NBTYPES)
619 "%s:%d: Expected type after ordinal, found '%s' instead\n",
620 SpecName, Line, token );
624 if (!(token = GetToken()))
626 fprintf( stderr, "%s:%d: Expected name after type\n", SpecName, Line );
629 strcpy( odp->name, token );
637 return ParseVariable( odp );
643 return ParseExportFunction( odp );
645 return ParseEquate( odp );
647 return ParseReturn( odp );
649 return ParseStub( odp );
651 return ParseVarargs( odp );
653 return ParseExtern( odp );
655 fprintf( stderr, "Should not happen\n" );
661 /*******************************************************************
666 static int ParseTopLevel(void)
670 while ((token = GetToken()) != NULL)
672 if (strcmp(token, "name") == 0)
674 strcpy(DLLName, GetToken());
676 if (!DLLFileName[0]) sprintf( DLLFileName, "%s.DLL", DLLName );
678 else if (strcmp(token, "file") == 0)
680 strcpy(DLLFileName, GetToken());
681 strupper(DLLFileName);
683 else if (strcmp(token, "type") == 0)
686 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
687 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
690 fprintf(stderr, "%s:%d: Type must be 'win16' or 'win32'\n",
695 else if (strcmp(token, "heap") == 0)
698 if (!IsNumberString(token))
700 fprintf(stderr, "%s:%d: Expected number after heap\n",
704 DLLHeapSize = atoi(token);
706 else if (strcmp(token, "init") == 0)
708 strcpy(DLLInitFunc, GetToken());
710 fprintf(stderr, "%s:%d: Expected function name after init\n", SpecName, Line);
712 else if (IsNumberString(token))
717 ordinal = atoi(token);
718 if ((rv = ParseOrdinal(ordinal)) < 0)
724 "%s:%d: Expected name, id, length or ordinal\n",
734 /*******************************************************************
737 * Store a list of ints into a byte array.
739 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
746 for (i = 0; i < odp->u.var.n_values; i++)
747 buffer[i] = odp->u.var.values[i];
750 for (i = 0; i < odp->u.var.n_values; i++)
751 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
754 for (i = 0; i < odp->u.var.n_values; i++)
755 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
758 return odp->u.var.n_values * size;
762 /*******************************************************************
765 * Dump a byte stream into the assembly code.
767 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
768 const char *section, const char *label_start )
771 if (section) fprintf( outfile, "\t%s\n", section );
772 if (label_start) fprintf( outfile, "%s:\n", label_start );
773 for (i = 0; i < len; i++)
775 if (!(i & 0x0f)) fprintf( outfile, "\t.byte " );
776 fprintf( outfile, "%d", *data++ );
778 fprintf( outfile, "%c", ((i & 0x0f) != 0x0f) ? ',' : '\n' );
780 fprintf( outfile, "\n" );
784 /*******************************************************************
787 * Build the in-memory representation of a 16-bit NE module, and dump it
788 * as a byte stream into the assembly code.
790 static int BuildModule16( FILE *outfile, int max_code_offset,
791 int max_data_offset )
797 SEGTABLEENTRY *pSegment;
804 * OFSTRUCT File information
805 * SEGTABLEENTRY Segment 1 (code)
806 * SEGTABLEENTRY Segment 2 (data)
807 * WORD[2] Resource table (empty)
808 * BYTE[2] Imported names (empty)
809 * BYTE[n] Resident names table
810 * BYTE[n] Entry table
813 buffer = xmalloc( 0x10000 );
815 pModule = (NE_MODULE *)buffer;
816 pModule->magic = IMAGE_OS2_SIGNATURE;
819 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
821 pModule->heap_size = DLLHeapSize;
822 pModule->stack_size = 0;
827 pModule->seg_count = 2;
828 pModule->modref_count = 0;
829 pModule->nrname_size = 0;
830 pModule->modref_table = 0;
831 pModule->nrname_fpos = 0;
832 pModule->moveable_entries = 0;
833 pModule->alignment = 0;
834 pModule->truetype = 0;
835 pModule->os_flags = NE_OSFLAGS_WINDOWS;
836 pModule->misc_flags = 0;
837 pModule->dlls_to_init = 0;
838 pModule->nrname_handle = 0;
839 pModule->min_swap_area = 0;
840 pModule->expected_version = 0x030a;
841 pModule->module32 = 0;
843 pModule->self_loading_sel = 0;
845 /* File information */
847 pFileInfo = (OFSTRUCT *)(pModule + 1);
848 pModule->fileinfo = (int)pFileInfo - (int)pModule;
849 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
850 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
851 + strlen(DLLFileName);
852 strcpy( pFileInfo->szPathName, DLLFileName );
853 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
857 pSegment = (SEGTABLEENTRY *)pstr;
858 pModule->seg_table = (int)pSegment - (int)pModule;
859 pSegment->filepos = 0;
860 pSegment->size = max_code_offset;
862 pSegment->minsize = max_code_offset;
866 pModule->dgroup_entry = (int)pSegment - (int)pModule;
867 pSegment->filepos = 0;
868 pSegment->size = max_data_offset;
869 pSegment->flags = NE_SEGFLAGS_DATA;
870 pSegment->minsize = max_data_offset;
876 pword = (WORD *)pSegment;
877 pModule->res_table = (int)pword - (int)pModule;
881 /* Imported names table */
883 pstr = (char *)pword;
884 pModule->import_table = (int)pstr - (int)pModule;
888 /* Resident names table */
890 pModule->name_table = (int)pstr - (int)pModule;
891 /* First entry is module name */
892 *pstr = strlen(DLLName );
893 strcpy( pstr + 1, DLLName );
896 pstr += sizeof(WORD);
897 /* Store all ordinals */
898 odp = OrdinalDefinitions + 1;
899 for (i = 1; i <= Limit; i++, odp++)
901 if (!odp->name[0]) continue;
902 *pstr = strlen( odp->name );
903 strcpy( pstr + 1, odp->name );
904 strupper( pstr + 1 );
907 pstr += sizeof(WORD);
913 pModule->entry_table = (int)pstr - (int)pModule;
915 odp = OrdinalDefinitions + 1;
916 for (i = 1; i <= Limit; i++, odp++)
928 selector = 1; /* Code selector */
934 selector = 2; /* Data selector */
938 selector = 0xfe; /* Constant selector */
942 selector = 0; /* Invalid selector */
946 /* create a new bundle if necessary */
947 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
951 bundle[1] = selector;
959 *(WORD *)pstr = odp->offset;
960 pstr += sizeof(WORD);
965 /* Dump the module content */
967 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
968 ".data", "Module_Start" );
969 return (int)pstr - (int)pModule;
973 /*******************************************************************
976 * Build a Win32 C file from a spec file.
978 static int BuildSpec32File( char * specfile, FILE *outfile )
983 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
985 fprintf( outfile, "#include \"builtin32.h\"\n\n" );
987 /* Output code for all stubs functions */
989 fprintf( outfile, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
991 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
993 if (odp->type != TYPE_STUB) continue;
994 fprintf( outfile, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
998 /* Output code for all register functions */
1000 fprintf( outfile, "#ifdef __i386__\n" );
1001 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1003 if (odp->type != TYPE_REGISTER) continue;
1005 "__asm__(\".align 4\\n\\t\"\n"
1006 " \".globl " PREFIX "%s\\n\\t\"\n"
1007 " \".type " PREFIX "%s,@function\\n\\t\"\n"
1008 " \"" PREFIX "%s:\\n\\t\"\n"
1009 " \"pushl $" PREFIX "__regs_%s\\n\\t\"\n"
1010 " \"pushl $" PREFIX "CALL32_Regs\\n\\t\"\n"
1012 odp->u.func.link_name, odp->u.func.link_name,
1013 odp->u.func.link_name, odp->u.func.link_name );
1015 fprintf( outfile, "#endif\n" );
1017 /* Output the DLL functions prototypes */
1019 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1024 fprintf( outfile, "extern void %s();\n", odp->u.vargs.link_name );
1027 fprintf( outfile, "extern void %s();\n", odp->u.ext.link_name );
1032 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1038 fprintf(stderr,"build: function type %d not available for Win32\n",
1044 /* Output LibMain function */
1045 if (DLLInitFunc[0]) fprintf( outfile, "extern void %s();\n", DLLInitFunc );
1048 /* Output the DLL functions table */
1050 fprintf( outfile, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1052 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1057 fprintf( outfile, " 0" );
1060 fprintf( outfile, " %s", odp->u.vargs.link_name );
1063 fprintf( outfile, " %s", odp->u.ext.link_name );
1068 fprintf( outfile, " %s", odp->u.func.link_name);
1071 fprintf( outfile, " __stub_%d", i );
1076 if (i < Limit) fprintf( outfile, ",\n" );
1078 fprintf( outfile, "\n};\n\n" );
1080 /* Output the DLL names table */
1083 fprintf( outfile, "static const char * const FuncNames[] =\n{\n" );
1084 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1086 if (odp->type == TYPE_INVALID) continue;
1087 if (nb_names++) fprintf( outfile, ",\n" );
1088 fprintf( outfile, " \"%s\"", odp->name );
1090 fprintf( outfile, "\n};\n\n" );
1092 /* Output the DLL argument types */
1094 fprintf( outfile, "static const unsigned int ArgTypes[%d] =\n{\n",
1096 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1098 unsigned int j, mask = 0;
1099 if ((odp->type == TYPE_STDCALL) || (odp->type == TYPE_CDECL))
1100 for (j = 0; odp->u.func.arg_types[j]; j++)
1102 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
1103 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
1105 fprintf( outfile, " %d", mask );
1106 if (i < Limit) fprintf( outfile, ",\n" );
1108 fprintf( outfile, "\n};\n\n" );
1110 /* Output the DLL ordinals table */
1112 fprintf( outfile, "static const unsigned short FuncOrdinals[] =\n{\n" );
1114 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1116 if (odp->type == TYPE_INVALID) continue;
1117 if (nb_names++) fprintf( outfile, ",\n" );
1118 fprintf( outfile, " %d", i - Base );
1120 fprintf( outfile, "\n};\n\n" );
1122 /* Output the DLL functions arguments */
1124 fprintf( outfile, "static const unsigned char FuncArgs[%d] =\n{\n",
1126 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1132 args = (unsigned char)strlen(odp->u.func.arg_types);
1135 args = 0x80 | (unsigned char)strlen(odp->u.func.arg_types);
1144 fprintf( outfile, " 0x%02x", args );
1145 if (i < Limit) fprintf( outfile, ",\n" );
1147 fprintf( outfile, "\n};\n\n" );
1149 /* Output the DLL descriptor */
1151 fprintf( outfile, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1153 fprintf( outfile, " \"%s\",\n", DLLName );
1154 fprintf( outfile, " %d,\n", Base );
1155 fprintf( outfile, " %d,\n", Limit - Base + 1 );
1156 fprintf( outfile, " %d,\n", nb_names );
1163 fprintf( outfile, " %s\n", DLLInitFunc[0] ? DLLInitFunc : "0" );
1164 fprintf( outfile, "};\n" );
1169 /*******************************************************************
1172 * Build a Win16 assembly file from a spec file.
1174 static int BuildSpec16File( char * specfile, FILE *outfile )
1178 int code_offset, data_offset, module_size;
1179 unsigned char *data;
1181 data = (unsigned char *)xmalloc( 0x10000 );
1182 memset( data, 0, 16 );
1185 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
1186 fprintf( outfile, "\t.text\n" );
1187 fprintf( outfile, "Code_Start:\n" );
1190 odp = OrdinalDefinitions;
1191 for (i = 0; i <= Limit; i++, odp++)
1196 odp->offset = 0xffff;
1200 odp->offset = LOWORD(odp->u.abs.value);
1204 odp->offset = data_offset;
1205 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1209 odp->offset = data_offset;
1210 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1214 odp->offset = data_offset;
1215 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1219 fprintf( outfile,"/* %s.%d */\n", DLLName, i);
1220 fprintf( outfile,"\tmovw $%d,%%ax\n",LOWORD(odp->u.ret.ret_value));
1221 fprintf( outfile,"\tmovw $%d,%%dx\n",HIWORD(odp->u.ret.ret_value));
1222 fprintf( outfile,"\t.byte 0x66\n");
1223 if (odp->u.ret.arg_size != 0)
1224 fprintf( outfile, "\tlret $%d\n\n", odp->u.ret.arg_size);
1227 fprintf( outfile, "\tlret\n");
1228 fprintf( outfile, "\tnop\n");
1229 fprintf( outfile, "\tnop\n\n");
1231 odp->offset = code_offset;
1232 code_offset += 12; /* Assembly code is 12 bytes long */
1238 case TYPE_PASCAL_16:
1240 fprintf( outfile, "/* %s.%d */\n", DLLName, i);
1241 fprintf( outfile, "\tpushw %%bp\n" );
1242 fprintf( outfile, "\tpushl $" PREFIX "%s\n",odp->u.func.link_name);
1243 /* FreeBSD does not understand lcall, so do it the hard way */
1244 fprintf( outfile, "\t.byte 0x9a\n" );
1245 fprintf( outfile, "\t.long " PREFIX "CallFrom16_%s_%s_%s\n",
1246 (odp->type == TYPE_CDECL) ? "c" : "p",
1247 (odp->type == TYPE_REGISTER) ? "regs" :
1248 (odp->type == TYPE_PASCAL_16) ? "word" : "long",
1249 odp->u.func.arg_types );
1250 fprintf( outfile, "\t.long 0x%08lx\n",
1251 MAKELONG( Code_Selector, 0x9090 /* nop ; nop */ ) );
1252 odp->offset = code_offset;
1253 code_offset += 16; /* Assembly code is 16 bytes long */
1257 fprintf(stderr,"build: function type %d not available for Win16\n",
1263 if (!code_offset) /* Make sure the code segment is not empty */
1265 fprintf( outfile, "\t.byte 0\n" );
1269 /* Output data segment */
1271 DumpBytes( outfile, data, data_offset, NULL, "Data_Start" );
1273 /* Build the module */
1275 module_size = BuildModule16( outfile, code_offset, data_offset );
1277 /* Output the DLL descriptor */
1279 fprintf( outfile, "\t.text\n" );
1280 fprintf( outfile, "DLLName:\t" STRING " \"%s\\0\"\n", DLLName );
1281 fprintf( outfile, "\t.align 4\n" );
1282 fprintf( outfile, "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1283 fprintf( outfile, PREFIX "%s_Descriptor:\n", DLLName );
1284 fprintf( outfile, "\t.long DLLName\n" ); /* Name */
1285 fprintf( outfile, "\t.long Module_Start\n" ); /* Module start */
1286 fprintf( outfile, "\t.long %d\n", module_size ); /* Module size */
1287 fprintf( outfile, "\t.long Code_Start\n" ); /* Code start */
1288 fprintf( outfile, "\t.long Data_Start\n" ); /* Data start */
1293 /*******************************************************************
1296 * Build an assembly file from a spec file.
1298 static int BuildSpecFile( FILE *outfile, char *specname )
1300 SpecName = specname;
1301 SpecFp = fopen( specname, "r");
1304 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1308 if (ParseTopLevel() < 0) return -1;
1313 return BuildSpec16File( specname, outfile );
1315 return BuildSpec32File( specname, outfile );
1317 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1323 /*******************************************************************
1324 * TransferArgs16To32
1326 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1327 * The 16-bit stack layout is:
1335 * For 'cdecl' argn up to arg1 are reversed.
1337 static int TransferArgs16To32( FILE *outfile, char *args, int usecdecl )
1339 int i, pos16, pos32;
1342 /* Copy the arguments */
1344 pos16 = 6; /* skip bp and return address */
1345 pos32 = usecdecl ? -(strlen(args) * 4) : 0;
1346 xargs = usecdecl ? args:args+strlen(args);
1348 for (i = strlen(args); i > 0; i--)
1356 case 'w': /* word */
1357 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1358 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1362 case 's': /* s_word */
1363 fprintf( outfile, "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1364 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1368 case 'l': /* long or segmented pointer */
1369 case 'T': /* segmented pointer to null-terminated string */
1370 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", pos16 );
1371 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1375 case 'p': /* linear pointer */
1376 case 't': /* linear pointer to null-terminated string */
1377 /* Get the selector */
1378 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1379 /* Get the selector base */
1380 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1381 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1382 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1383 /* Add the offset */
1384 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1385 fprintf( outfile, "\taddl %%eax,%d(%%ebx)\n", pos32 );
1390 fprintf( stderr, "Unknown arg type '%c'\n", *xargs );
1398 return pos16 - 6; /* Return the size of the 16-bit args */
1402 /*******************************************************************
1405 * Build the context structure on the 32-bit stack.
1407 static void BuildContext16( FILE *outfile )
1409 /* Store the registers */
1411 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1412 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1413 fprintf( outfile, "\tmovl %%ecx,%d(%%ebx)\n",
1414 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1415 fprintf( outfile, "\tmovl %%edx,%d(%%ebx)\n",
1416 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1417 fprintf( outfile, "\tmovl %%esi,%d(%%ebx)\n",
1418 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1419 fprintf( outfile, "\tmovl %%edi,%d(%%ebx)\n",
1420 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1422 fprintf( outfile, "\tmovl -24(%%ebp),%%eax\n" ); /* Get %ebx from stack*/
1423 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1424 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1425 fprintf( outfile, "\tmovzwl -10(%%ebp),%%eax\n" ); /* Get %ds from stack*/
1426 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1427 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1428 fprintf( outfile, "\tmovzwl -6(%%ebp),%%eax\n" ); /* Get %es from stack*/
1429 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1430 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1431 fprintf( outfile, "\tpushfl\n" );
1432 fprintf( outfile, "\tpopl %d(%%ebx)\n",
1433 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1434 fprintf( outfile, "\tmovl -20(%%ebp),%%eax\n" ); /* Get %ebp from stack */
1435 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1436 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1437 fprintf( outfile, "\tmovzwl 2(%%ebp),%%eax\n" ); /* Get %ip from stack */
1438 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1439 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1440 fprintf( outfile, "\tleal 2(%%ebp),%%eax\n" ); /* Get initial %sp */
1441 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1442 CONTEXTOFFSET(Esp) - sizeof(CONTEXT) );
1443 fprintf( outfile, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1444 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1445 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1446 fprintf( outfile, "\tmovzwl -14(%%ebp),%%eax\n" ); /* Get %fs from stack */
1447 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1448 CONTEXTOFFSET(SegFs) - sizeof(CONTEXT) );
1449 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1450 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1451 CONTEXTOFFSET(SegGs) - sizeof(CONTEXT) );
1452 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1453 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1454 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1456 fprintf( outfile, "\tfsave %d(%%ebx)\n",
1457 CONTEXTOFFSET(FloatSave) - sizeof(CONTEXT) );
1462 /*******************************************************************
1465 * Restore the registers from the context structure.
1467 static void RestoreContext16( FILE *outfile )
1469 /* Get the 32-bit stack pointer */
1471 fprintf( outfile, "\tleal -%d(%%ebp),%%ebx\n",
1472 STRUCTOFFSET(STACK32FRAME,ebp) );
1474 /* Remove everything up to (including) the return address
1475 * from the 16-bit stack */
1477 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1478 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1479 fprintf( outfile, "\tmovw %%ax,%%ss\n" );
1480 fprintf( outfile, "\tmovl %d(%%ebx),%%esp\n",
1481 CONTEXTOFFSET(Esp) - sizeof(CONTEXT) );
1482 fprintf( outfile, "\taddl $4,%%esp\n" ); /* Remove return address */
1484 /* Restore the registers */
1486 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n",
1487 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1488 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n",
1489 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1490 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n",
1491 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1492 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n",
1493 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1494 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n",
1495 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1496 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new cs */
1497 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1498 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new ip */
1499 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1500 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new ds */
1501 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1502 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new es */
1503 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1504 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new fs */
1505 CONTEXTOFFSET(SegFs) - sizeof(CONTEXT) );
1506 fprintf( outfile, "\tpushl %d(%%ebx)\n",
1507 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1508 fprintf( outfile, "\tpopfl\n" );
1509 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1510 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1511 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n",
1512 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1513 fprintf( outfile, "\tpopl %%fs\n" ); /* Set fs */
1514 fprintf( outfile, "\tpopl %%es\n" ); /* Set es */
1515 fprintf( outfile, "\tpopl %%ds\n" ); /* Set ds */
1519 /*******************************************************************
1520 * BuildCallFrom16Func
1522 * Build a 16-bit-to-Wine callback function. The syntax of the function
1523 * profile is: call_type_xxxxx, where 'call' is the letter 'c' or 'p' for C or
1524 * Pascal calling convention, 'type' is one of 'regs', 'word' or
1525 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1526 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1527 * 'T'=segmented pointer to null-terminated string).
1528 * For register functions, the arguments are ignored, but they are still
1529 * removed from the stack upon return.
1531 * A special variant of the callback function is generated by the function
1532 * profile "t_long_". This is used by the Win95 16->32 thunk
1533 * functions C16ThkSL and C16ThkSL01 and is implemented as follows:
1534 * On entry, the EBX register is set up to contain a flat pointer to the
1535 * 16-bit stack such that EBX+22 points to the first argument.
1536 * Then, the entry point is called, while EBP is set up to point
1537 * to the return address (on the 32-bit stack).
1538 * The called function returns with CX set to the number of bytes
1539 * to be popped of the caller's stack.
1541 * Stack layout upon entry to the callback function:
1543 * (sp+18) word first 16-bit arg
1547 * (sp+8) long 32-bit entry point (used to store edx)
1548 * (sp+6) word high word of cs (always 0, used to store es)
1549 * (sp+4) word low word of cs of 16-bit entry point
1550 * (sp+2) word high word of ip (always 0, used to store ds)
1551 * (sp) word low word of ip of 16-bit entry point
1553 * Added on the stack:
1554 * (sp-2) word saved fs
1555 * (sp-4) word buffer for Win16Mutex recursion count
1557 * (sp-12) long saved previous stack
1559 static void BuildCallFrom16Func( FILE *outfile, char *profile )
1566 char *args = profile + 7;
1568 /* Parse function type */
1570 if (!strncmp( "c_", profile, 2 )) cdecl = 1;
1571 else if (!strncmp( "t_", profile, 2 )) thunk = 1;
1572 else if (strncmp( "p_", profile, 2 ))
1574 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1578 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
1579 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
1580 else if (strncmp( "long_", profile + 2, 5 ))
1582 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1586 /* Function header */
1588 fprintf( outfile, "\n\t.align 4\n" );
1590 fprintf( outfile, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX "CallFrom16_%s\n",
1593 fprintf( outfile, "\t.globl " PREFIX "CallFrom16_%s\n", profile );
1594 fprintf( outfile, PREFIX "CallFrom16_%s:\n", profile );
1596 /* Save 16-bit fs and leave room for Win16Mutex recursion count */
1598 fprintf( outfile, "\t.byte 0x66\n\tpushl %%fs\n" );
1599 fprintf( outfile, "\tpushw $0\n" );
1601 /* Setup bp to point to its copy on the stack */
1603 fprintf( outfile, "\tpushl %%ebp\n" ); /* Save the full 32-bit ebp */
1604 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1605 fprintf( outfile, "\taddw $20,%%bp\n" );
1607 /* Save 16-bit ds and es */
1609 /* Stupid FreeBSD assembler doesn't know these either */
1610 /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1611 fprintf( outfile, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1612 /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1613 fprintf( outfile, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1617 fprintf( outfile, "\tpushl %%ebx\n" );
1619 /* Restore 32-bit segment registers */
1621 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
1623 fprintf( outfile, "\tdata16\n");
1625 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
1627 fprintf( outfile, "\tdata16\n");
1629 fprintf( outfile, "\tmovw %%bx,%%es\n" );
1631 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
1633 /* Get the 32-bit stack pointer from the TEB */
1635 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET );
1637 /* Save the 16-bit stack */
1640 fprintf( outfile,"\tdata16\n");
1642 fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss,(%d)\n", STACKOFFSET + 2 );
1643 fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp,(%d)\n", STACKOFFSET );
1645 /* Transfer the arguments */
1647 if (reg_func) BuildContext16( outfile );
1648 else if (*args) argsize = TransferArgs16To32( outfile, args, cdecl );
1651 /* Get the stack selector base */
1652 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1653 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1654 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1655 fprintf( outfile, "\tmovl %%eax,-24(%%ebp)\n" );
1656 /* Add the offset */
1657 fprintf( outfile, "\tleal -16(%%ebp),%%eax\n" );
1658 fprintf( outfile, "\taddl %%eax,-24(%%ebp)\n" );
1661 /* Get the address of the API function */
1663 fprintf( outfile, "\tmovl -4(%%ebp),%%eax\n" );
1665 /* If necessary, save %edx over the API function address */
1667 if (!reg_func && short_ret)
1668 fprintf( outfile, "\tmovl %%edx,-4(%%ebp)\n" );
1670 /* Restore %ebx and store the 32-bit stack pointer instead */
1672 fprintf( outfile, "\tmovl %%ebx,%%ebp\n" );
1673 fprintf( outfile, "\tpopl %%ebx\n" );
1674 fprintf( outfile, "\tpushl %%ebp\n" );
1676 /* Switch to the 32-bit stack */
1678 fprintf( outfile, "\tpushl %%ds\n" );
1679 fprintf( outfile, "\tpopl %%ss\n" );
1680 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1681 reg_func ? sizeof(CONTEXT) : 4 * strlen(args) );
1682 if (reg_func) /* Push the address of the context struct */
1683 fprintf( outfile, "\tpushl %%esp\n" );
1685 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1687 fprintf( outfile, "\taddl $%d,%%ebp\n", STRUCTOFFSET(STACK32FRAME,ebp) );
1689 /* Print the debug information before the call */
1691 if (debugging && !thunk)
1695 if (cdecl) ftype |= 4;
1696 if (reg_func) ftype |= 2;
1697 if (short_ret) ftype |= 1;
1699 fprintf( outfile, "\tpushl %%eax\n" );
1700 fprintf( outfile, "\tpushl $Profile_%s\n", profile );
1701 fprintf( outfile, "\tpushl $%d\n", ftype );
1702 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n" );
1703 fprintf( outfile, "\tpopl %%eax\n" );
1704 fprintf( outfile, "\tpopl %%eax\n" );
1705 fprintf( outfile, "\tpopl %%eax\n" );
1708 /* Call the entry point */
1712 fprintf( outfile, "\tpushl %%ebp\n" );
1713 fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
1714 fprintf( outfile, "\tcall *%%eax\n" );
1715 fprintf( outfile, "\tpopl %%ebp\n" );
1718 fprintf( outfile, "\tcall *%%eax\n" );
1721 /* Print the debug information after the call */
1723 if (debugging && !thunk)
1727 /* Push again the address of the context struct in case */
1728 /* it has been removed by an stdcall function */
1729 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1730 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME,ebp) );
1731 fprintf( outfile, "\tpushl %%esp\n" );
1733 fprintf( outfile, "\tpushl %%eax\n" );
1734 fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1735 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n" );
1736 fprintf( outfile, "\tpopl %%eax\n" );
1737 fprintf( outfile, "\tpopl %%eax\n" );
1740 /* Restore the 16-bit stack */
1743 fprintf( outfile, "\tdata16\n");
1745 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2 );
1746 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
1747 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
1751 /* Calc the arguments size */
1767 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1772 /* Restore registers from the context structure */
1773 RestoreContext16( outfile );
1777 /* Restore high 16 bits of ebp */
1778 fprintf( outfile, "\tpopl %%ebp\n" );
1780 /* Restore ds and es */
1781 fprintf( outfile, "\tincl %%esp\n" ); /* Remove mutex count */
1782 fprintf( outfile, "\tincl %%esp\n" );
1783 fprintf( outfile, "\tpopl %%edx\n" ); /* Remove ip and fs */
1784 fprintf( outfile, "\tmovw %%dx,%%fs\n" ); /* and restore fs */
1785 fprintf( outfile, "\tpopl %%edx\n" ); /* Remove cs and ds */
1786 fprintf( outfile, "\tmovw %%dx,%%ds\n" ); /* and restore ds */
1787 fprintf( outfile, "\t.byte 0x66\n\tpopl %%es\n" ); /* Restore es */
1789 if (short_ret) fprintf( outfile, "\tpopl %%edx\n" ); /* Restore edx */
1792 /* Get the return value into dx:ax */
1793 fprintf( outfile, "\tmovl %%eax,%%edx\n" );
1794 fprintf( outfile, "\tshrl $16,%%edx\n" );
1795 /* Remove API entry point */
1796 fprintf( outfile, "\taddl $4,%%esp\n" );
1799 /* Restore low 16 bits of ebp */
1800 fprintf( outfile, "\tpopw %%bp\n" );
1803 /* Remove the arguments and return */
1807 fprintf( outfile, "\tpopl %%ebx\n" );
1808 fprintf( outfile, "\txorb %%ch,%%ch\n" );
1809 fprintf( outfile, "\taddw %%cx, %%sp\n" );
1810 fprintf( outfile, "\tpushl %%ebx\n" );
1811 fprintf( outfile, "\t.byte 0x66\n" );
1812 fprintf( outfile, "\tlret\n" );
1814 else if (argsize && !cdecl)
1816 fprintf( outfile, "\t.byte 0x66\n" );
1817 fprintf( outfile, "\tlret $%d\n", argsize );
1821 fprintf( outfile, "\t.byte 0x66\n" );
1822 fprintf( outfile, "\tlret\n" );
1827 /*******************************************************************
1830 * Build a Wine-to-16-bit callback function.
1832 * Stack frame of the callback function:
1836 * (ebp+8) func to call
1837 * (ebp+4) return address
1838 * (ebp) previous ebp
1840 * Prototypes for the CallTo16 functions:
1841 * extern WINAPI WORD CallTo16_word_xxx( FARPROC16 func, args... );
1842 * extern WINAPI LONG CallTo16_long_xxx( FARPROC16 func, args... );
1843 * extern WINAPI void CallTo16_sreg_( const CONTEXT *context, int nb_args );
1844 * extern WINAPI void CallTo16_lreg_( const CONTEXT *context, int nb_args );
1846 static void BuildCallTo16Func( FILE *outfile, char *profile )
1850 char *args = profile + 5;
1852 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1853 else if (!strncmp( "sreg_", profile, 5 )) reg_func = 1;
1854 else if (!strncmp( "lreg_", profile, 5 )) reg_func = 2;
1855 else if (strncmp( "long_", profile, 5 ))
1857 fprintf( stderr, "Invalid function name '%s'.\n", profile );
1861 /* Function header */
1863 fprintf( outfile, "\n\t.align 4\n" );
1865 fprintf( outfile, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX "CallTo16_%s\n",
1868 fprintf( outfile, "\t.globl " PREFIX "CallTo16_%s\n", profile );
1869 fprintf( outfile, PREFIX "CallTo16_%s:\n", profile );
1873 fprintf( outfile, "\tpushl %%ebp\n" );
1874 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
1876 /* Save the 32-bit registers */
1878 fprintf( outfile, "\tpushl %%ebx\n" );
1879 fprintf( outfile, "\tpushl %%ecx\n" );
1880 fprintf( outfile, "\tpushl %%edx\n" );
1881 fprintf( outfile, "\tpushl %%esi\n" );
1882 fprintf( outfile, "\tpushl %%edi\n" );
1884 /* Enter Win16 Mutex */
1886 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock\n" );
1888 /* Print debugging info */
1892 /* Push the address of the first argument */
1893 fprintf( outfile, "\tleal 8(%%ebp),%%eax\n" );
1894 fprintf( outfile, "\tpushl $%d\n", reg_func ? -1 : strlen(args) );
1895 fprintf( outfile, "\tpushl %%eax\n" );
1896 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
1897 fprintf( outfile, "\tpopl %%eax\n" );
1898 fprintf( outfile, "\tpopl %%eax\n" );
1901 /* Call the actual CallTo16 routine (simulate a lcall) */
1903 fprintf( outfile, "\tpushl %%cs\n" );
1904 fprintf( outfile, "\tcall do_callto16_%s\n", profile );
1906 fprintf( outfile, "\tpushl %%eax\n" );
1908 /* Print debugging info */
1912 fprintf( outfile, "\tpushl %%eax\n" );
1913 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret\n" );
1914 fprintf( outfile, "\tpopl %%eax\n" );
1917 /* Leave Win16 Mutex */
1919 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock\n" );
1921 /* Restore the 32-bit registers */
1923 fprintf( outfile, "\tpopl %%eax\n" );
1924 fprintf( outfile, "\tpopl %%edi\n" );
1925 fprintf( outfile, "\tpopl %%esi\n" );
1926 fprintf( outfile, "\tpopl %%edx\n" );
1927 fprintf( outfile, "\tpopl %%ecx\n" );
1928 fprintf( outfile, "\tpopl %%ebx\n" );
1933 /* FIXME: this is a hack because of task.c */
1934 if (!strcmp( profile, "word_" ))
1936 fprintf( outfile, ".globl " PREFIX "CALLTO16_Restore\n" );
1937 fprintf( outfile, PREFIX "CALLTO16_Restore:\n" );
1940 fprintf( outfile, "\tpopl %%ebp\n" );
1941 fprintf( outfile, "\tret $%d\n", 4 * strlen(args) + 4 );
1944 /* Start of the actual CallTo16 routine */
1946 fprintf( outfile, "do_callto16_%s:\n", profile );
1948 /* Save the 32-bit stack */
1950 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
1951 fprintf( outfile, "\tmovl %%ebp,%%ebx\n" );
1952 fprintf( outfile, "\tmovl %%esp,%%edx\n" );
1956 /* Switch to the 16-bit stack, saving the current %%esp, */
1957 /* and adding the specified offset to the new sp */
1958 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d),%%eax\n", STACKOFFSET );
1959 fprintf( outfile, "\tsubl 12(%%ebx),%%eax\n" ); /* Get the offset */
1961 fprintf( outfile,"\tdata16\n");
1963 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
1964 fprintf( outfile, "\tmovl %%eax,%%esp\n" );
1965 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
1967 /* Get the registers. ebx is handled later on. */
1969 fprintf( outfile, "\tmovl 8(%%ebx),%%ebx\n" );
1970 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs) );
1971 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1972 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegFs) );
1973 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
1974 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp) );
1975 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax) );
1976 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx) );
1977 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx) );
1978 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi) );
1979 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi) );
1981 /* Push the return address
1982 * With sreg suffix, we push 16:16 address (normal lret)
1983 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
1986 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_long\n" );
1989 fprintf( outfile, "\tpushw $0\n" );
1990 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_eax+2\n" );
1991 fprintf( outfile, "\tpushw $0\n" );
1992 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_eax\n" );
1995 /* Push the called routine address */
1997 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
1998 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
2000 /* Get the 16-bit ds */
2002 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
2003 /* Get ebx from the 32-bit stack */
2004 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(Ebx) );
2005 fprintf( outfile, "\tpopl %%ds\n" );
2007 else /* not a register function */
2009 int pos = 12; /* first argument position */
2011 /* Switch to the 16-bit stack */
2013 fprintf( outfile,"\tdata16\n");
2015 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
2016 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
2017 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
2019 /* Make %bp point to the previous stackframe (built by CallFrom16) */
2020 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
2021 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n",
2022 STRUCTOFFSET(STACK16FRAME,bp) );
2024 /* Transfer the arguments */
2030 case 'w': /* word */
2031 fprintf( outfile, "\tpushw %d(%%ebx)\n", pos );
2033 case 'l': /* long */
2034 fprintf( outfile, "\tpushl %d(%%ebx)\n", pos );
2037 fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
2043 /* Push the return address */
2045 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_%s\n",
2046 short_ret ? "word" : "long" );
2048 /* Push the called routine address */
2050 fprintf( outfile, "\tpushl 8(%%ebx)\n" );
2052 /* Set %fs to the value saved by the last CallFrom16 */
2054 fprintf( outfile, "\tmovw -14(%%ebp),%%ax\n" );
2055 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2057 /* Set %ds and %es (and %ax just in case) equal to %ss */
2059 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
2060 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2061 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2064 /* Jump to the called routine */
2066 fprintf( outfile, "\t.byte 0x66\n" );
2067 fprintf( outfile, "\tlret\n" );
2071 /*******************************************************************
2074 * Build the return code for 16-bit callbacks
2076 static void BuildRet16Func( FILE *outfile )
2078 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_word\n" );
2079 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_long\n" );
2080 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_eax\n" );
2082 fprintf( outfile, PREFIX "CALLTO16_Ret_word:\n" );
2083 fprintf( outfile, "\txorl %%edx,%%edx\n" );
2085 /* Put return value into %eax */
2087 fprintf( outfile, PREFIX "CALLTO16_Ret_long:\n" );
2088 fprintf( outfile, "\tshll $16,%%edx\n" );
2089 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
2090 fprintf( outfile, "\tmovl %%edx,%%eax\n" );
2091 fprintf( outfile, PREFIX "CALLTO16_Ret_eax:\n" );
2093 /* Restore 32-bit segment registers */
2095 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
2097 fprintf( outfile, "\tdata16\n");
2099 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
2101 fprintf( outfile, "\tdata16\n");
2103 fprintf( outfile, "\tmovw %%bx,%%es\n" );
2105 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
2107 /* Restore the 32-bit stack */
2110 fprintf( outfile, "\tdata16\n");
2112 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
2113 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
2114 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2116 /* Return to caller */
2118 fprintf( outfile, "\tlret\n" );
2120 /* Declare the return address variables */
2122 fprintf( outfile, "\t.data\n" );
2123 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_word\n" );
2124 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_long\n" );
2125 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_eax\n" );
2126 fprintf( outfile, PREFIX "CALLTO16_RetAddr_word:\t.long 0\n" );
2127 fprintf( outfile, PREFIX "CALLTO16_RetAddr_long:\t.long 0\n" );
2128 fprintf( outfile, PREFIX "CALLTO16_RetAddr_eax:\t.long 0\n" );
2129 fprintf( outfile, "\t.text\n" );
2132 /*******************************************************************
2133 * BuildCallTo32CBClient
2135 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
2137 * Since the relay stub is itself 32-bit, this should not be a problem;
2138 * unfortunately, the relay stubs are expected to switch back to a
2139 * 16-bit stack (and 16-bit code) after completion :-(
2141 * This would conflict with our 16- vs. 32-bit stack handling, so
2142 * we simply switch *back* to our 32-bit stack before returning to
2145 * The CBClient relay stub expects to be called with:
2146 * - ebp pointing to the 16-bit stack at ss:bp
2147 * - ebx pointing to a buffer containing the saved 16-bit ss:sp
2149 * After completion, the stub will load ss:sp from the buffer at ebx
2150 * and perform a far return to 16-bit code.
2152 * To trick the relay stub into returning to us, we push a 16-bit
2153 * cs:ip pair pointing to our return entry point onto the 16-bit stack,
2154 * followed by a ss:sp pair pointing to *that* cs:ip pair.
2155 * Our return stub thus called will then reload the 32-bit ss:esp and
2156 * return to 32-bit code (by using and ss:esp value that we have also
2157 * pushed onto the 16-bit stack before and a cs:eip values found at
2158 * that position on the 32-bit stack). The layout of our
2159 * temporary area used on the 16-bit stack is thus as follows:
2161 * (ebx+12) 32-bit ss (flat)
2162 * (ebx+8) 32-bit sp (32-bit stack pointer)
2163 * (ebx+6) 16-bit cs (this segment)
2164 * (ebx+4) 16-bit ip ('16-bit' return entry point)
2165 * (ebx+2) 16-bit ss (16-bit stack segment)
2166 * (ebx+0) 16-bit sp (points to ebx+4)
2168 * The second variant of this routine, CALL32_CBClientEx, which is used
2169 * to implement KERNEL.621, has to cope with yet another problem: Here,
2170 * the 32-bit side directly returns to the caller of the CBClient thunklet,
2171 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
2172 * As we have to return to our 32-bit code first, we have to adapt the
2173 * layout of our temporary area so as to include values for the registers
2174 * that are to be restored, and later (in the implementation of KERNEL.621)
2175 * we *really* restore them. The return stub restores DS, DI, SI, and BP
2176 * from the stack, skips the next 8 bytes (CBClient relay code / target),
2177 * and then performs a lret NN, where NN is the number of arguments to be
2178 * removed. Thus, we prepare our temporary area as follows:
2180 * (ebx+22) 16-bit cs (this segment)
2181 * (ebx+20) 16-bit ip ('16-bit' return entry point)
2182 * (ebx+16) 32-bit ss (flat)
2183 * (ebx+12) 32-bit sp (32-bit stack pointer)
2184 * (ebx+10) 16-bit bp (points to ebx+24)
2185 * (ebx+8) 16-bit si (ignored)
2186 * (ebx+6) 16-bit di (ignored)
2187 * (ebx+4) 16-bit ds (we actually use the flat DS here)
2188 * (ebx+2) 16-bit ss (16-bit stack segment)
2189 * (ebx+0) 16-bit sp (points to ebx+4)
2191 * Note that we ensure that DS is not changed and remains the flat segment,
2192 * and the 32-bit stack pointer our own return stub needs fits just
2193 * perfectly into the 8 bytes that are skipped by the Windows stub.
2194 * One problem is that we have to determine the number of removed arguments,
2195 * as these have to be really removed in KERNEL.621. Thus, the BP value
2196 * that we place in the temporary area to be restored, contains the value
2197 * that SP would have if no arguments were removed. By comparing the actual
2198 * value of SP with this value in our return stub we can compute the number
2199 * of removed arguments. This is then returned to KERNEL.621.
2201 * The stack layout of this function:
2202 * (ebp+16) nArgs pointer to variable receiving nr. of args (Ex only)
2203 * (ebp+12) arg ebp value to be set for relay stub
2204 * (ebp+8) func CBClient relay stub address
2208 static void BuildCallTo32CBClient( FILE *outfile, BOOL isEx )
2210 char *name = isEx? "CBClientEx" : "CBClient";
2211 int size = isEx? 24 : 16;
2213 /* Function header */
2215 fprintf( outfile, "\n\t.align 4\n" );
2217 fprintf( outfile, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX "CALL32_%s\n",
2220 fprintf( outfile, "\t.globl " PREFIX "CALL32_%s\n", name );
2221 fprintf( outfile, PREFIX "CALL32_%s:\n", name );
2225 fprintf( outfile, "\tpushl %%ebp\n" );
2226 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2227 fprintf( outfile, "\tpushl %%edi\n" );
2228 fprintf( outfile, "\tpushl %%esi\n" );
2229 fprintf( outfile, "\tpushl %%ebx\n" );
2231 /* Get the 16-bit stack */
2233 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET);
2235 /* Convert it to a flat address */
2237 fprintf( outfile, "\tshldl $16,%%ebx,%%eax\n" );
2238 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
2239 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%esi\n" );
2240 fprintf( outfile, "\tmovw %%bx,%%ax\n" );
2241 fprintf( outfile, "\taddl %%eax,%%esi\n" );
2243 /* Allocate temporary area (simulate STACK16_PUSH) */
2245 fprintf( outfile, "\tpushf\n" );
2246 fprintf( outfile, "\tcld\n" );
2247 fprintf( outfile, "\tleal -%d(%%esi), %%edi\n", size );
2248 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2249 fprintf( outfile, "\trep\n\tmovsb\n" );
2250 fprintf( outfile, "\tpopf\n" );
2252 fprintf( outfile, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size, STACKOFFSET );
2254 fprintf( outfile, "\tpushl %%edi\n" ); /* remember address */
2256 /* Set up temporary area */
2258 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 );
2259 fprintf( outfile, "\tmovl %%ebx, (%%edi)\n" );
2263 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
2264 fprintf( outfile, "\tmovl %%eax, 4(%%edi)\n" );
2266 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2267 fprintf( outfile, "\tmovl %%eax, 8(%%edi)\n" );
2269 fprintf( outfile, "\tmovl %%ss, %%ax\n" );
2270 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2271 fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
2275 fprintf( outfile, "\tmovl %%ds, %%ax\n" );
2276 fprintf( outfile, "\tmovw %%ax, 4(%%edi)\n" );
2278 fprintf( outfile, "\taddl $20, %%ebx\n" );
2279 fprintf( outfile, "\tmovw %%bx, 10(%%edi)\n" );
2281 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2282 fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
2284 fprintf( outfile, "\tmovl %%ss, %%ax\n" );
2285 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2286 fprintf( outfile, "\tmovl %%eax, 16(%%edi)\n" );
2288 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
2289 fprintf( outfile, "\tmovl %%eax, 20(%%edi)\n" );
2292 /* Setup registers and call CBClient relay stub (simulating a far call) */
2294 fprintf( outfile, "\tmovl %%edi, %%ebx\n" );
2295 fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
2296 fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
2298 fprintf( outfile, "\tpushl %%cs\n" );
2299 fprintf( outfile, "\tcall *%%eax\n" );
2301 /* Cleanup temporary area (simulate STACK16_POP) */
2303 fprintf( outfile, "\tpop %%esi\n" );
2305 fprintf( outfile, "\tpushf\n" );
2306 fprintf( outfile, "\tstd\n" );
2307 fprintf( outfile, "\tdec %%esi\n" );
2308 fprintf( outfile, "\tleal %d(%%esi), %%edi\n", size );
2309 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2310 fprintf( outfile, "\trep\n\tmovsb\n" );
2311 fprintf( outfile, "\tpopf\n" );
2313 fprintf( outfile, "\t.byte 0x64\n\taddw $%d,(%d)\n", size, STACKOFFSET );
2315 /* Return argument size to caller */
2318 fprintf( outfile, "\tmovl 28(%%esp), %%ebx\n" );
2319 fprintf( outfile, "\tmovl %%ebp, (%%ebx)\n" );
2322 /* Restore registers and return */
2324 fprintf( outfile, "\tpopl %%ebx\n" );
2325 fprintf( outfile, "\tpopl %%esi\n" );
2326 fprintf( outfile, "\tpopl %%edi\n" );
2327 fprintf( outfile, "\tpopl %%ebp\n" );
2328 fprintf( outfile, "\tret\n" );
2330 /* '16-bit' return stub */
2332 fprintf( outfile, "\t.globl " PREFIX "CALL32_%s_Ret\n", name );
2333 fprintf( outfile, PREFIX "CALL32_%s_Ret:\n", name );
2337 fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
2338 fprintf( outfile, "\tlssl %%ss:(%%ebx), %%esp\n" );
2342 fprintf( outfile, "\tmovzwl %%bp, %%ebx\n" );
2343 fprintf( outfile, "\tsubw %%bp, %%sp\n" );
2344 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
2345 fprintf( outfile, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
2347 fprintf( outfile, "\tlret\n" );
2349 /* Declare the return address variable */
2351 fprintf( outfile, "\t.data\n" );
2352 fprintf( outfile, "\t.globl " PREFIX "CALL32_%s_RetAddr\n", name );
2353 fprintf( outfile, PREFIX "CALL32_%s_RetAddr:\t.long 0\n", name );
2354 fprintf( outfile, "\t.text\n" );
2359 /*******************************************************************
2360 * BuildCallTo32LargeStack
2362 * Build the function used to switch to the original 32-bit stack
2363 * before calling a 32-bit function from 32-bit code. This is used for
2364 * functions that need a large stack, like X bitmaps functions.
2366 * The generated function has the following prototype:
2367 * int xxx( int (*func)(), void *arg );
2369 * The pointer to the function can be retrieved by calling CALL32_Init,
2370 * which also takes care of saving the current 32-bit stack pointer.
2372 * NOTE: The CALL32_LargeStack routine may be recursively entered by the
2373 * same thread, but not concurrently entered by several threads.
2382 static void BuildCallTo32LargeStack( FILE *outfile )
2384 /* Initialization function */
2386 fprintf( outfile, "\n\t.align 4\n" );
2388 fprintf( outfile, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX "CALL32_Init\n");
2390 fprintf( outfile, "\t.globl " PREFIX "CALL32_Init\n" );
2391 fprintf( outfile, "\t.type " PREFIX "CALL32_Init,@function\n" );
2392 fprintf( outfile, PREFIX "CALL32_Init:\n" );
2393 fprintf( outfile, "\tleal -256(%%esp),%%eax\n" );
2394 fprintf( outfile, "\tmovl %%eax,CALL32_Original32_esp\n" );
2395 fprintf( outfile, "\tmovl $CALL32_LargeStack,%%eax\n" );
2396 fprintf( outfile, "\tret\n" );
2398 /* Function header */
2400 fprintf( outfile, "\n\t.align 4\n" );
2402 fprintf( outfile, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2404 fprintf( outfile, "CALL32_LargeStack:\n" );
2408 fprintf( outfile, "\tpushl %%ebp\n" );
2409 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2411 /* Switch to the original 32-bit stack pointer */
2413 fprintf( outfile, "\tcmpl $0, CALL32_RecursionCount\n" );
2414 fprintf( outfile, "\tjne CALL32_skip\n" );
2415 fprintf( outfile, "\tmovl CALL32_Original32_esp, %%esp\n" );
2416 fprintf( outfile, "CALL32_skip:\n" );
2418 fprintf( outfile, "\tincl CALL32_RecursionCount\n" );
2420 /* Transfer the argument and call the function */
2422 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2423 fprintf( outfile, "\tcall *8(%%ebp)\n" );
2425 /* Restore registers and return */
2427 fprintf( outfile, "\tdecl CALL32_RecursionCount\n" );
2429 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2430 fprintf( outfile, "\tpopl %%ebp\n" );
2431 fprintf( outfile, "\tret\n" );
2435 fprintf( outfile, "\t.data\n" );
2436 fprintf( outfile, "CALL32_Original32_esp:\t.long 0\n" );
2437 fprintf( outfile, "CALL32_RecursionCount:\t.long 0\n" );
2438 fprintf( outfile, "\t.text\n" );
2442 /*******************************************************************
2443 * BuildCallFrom32Regs
2445 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2446 * 'args' is the number of dword arguments.
2450 * (esp+336) ret addr (or relay addr when debugging(relay) is on)
2451 * (esp+332) entry point
2452 * (esp+204) buffer area to allow stack frame manipulation
2453 * (esp+0) CONTEXT struct
2455 static void BuildCallFrom32Regs( FILE *outfile )
2457 #define STACK_SPACE 128
2459 /* Function header */
2461 fprintf( outfile, "\n\t.align 4\n" );
2463 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
2465 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
2466 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
2468 /* Allocate some buffer space on the stack */
2470 fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
2472 /* Build the context structure */
2474 fprintf( outfile, "\tpushw $0\n" );
2475 fprintf( outfile, "\t.byte 0x66\n\tpushl %%ss\n" );
2476 fprintf( outfile, "\tpushl %%eax\n" ); /* %esp place holder */
2477 fprintf( outfile, "\tpushfl\n" );
2478 fprintf( outfile, "\tpushw $0\n" );
2479 fprintf( outfile, "\t.byte 0x66\n\tpushl %%cs\n" );
2480 fprintf( outfile, "\tpushl %d(%%esp)\n", 16+STACK_SPACE+4 ); /* %eip at time of call */
2481 fprintf( outfile, "\tpushl %%ebp\n" );
2483 fprintf( outfile, "\tpushl %%eax\n" );
2484 fprintf( outfile, "\tpushl %%ecx\n" );
2485 fprintf( outfile, "\tpushl %%edx\n" );
2486 fprintf( outfile, "\tpushl %%ebx\n" );
2487 fprintf( outfile, "\tpushl %%esi\n" );
2488 fprintf( outfile, "\tpushl %%edi\n" );
2490 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2491 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2492 fprintf( outfile, "\tpushl %%eax\n" );
2493 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2494 fprintf( outfile, "\tpushl %%eax\n" );
2495 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2496 fprintf( outfile, "\tpushl %%eax\n" );
2497 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2498 fprintf( outfile, "\tpushl %%eax\n" );
2500 fprintf( outfile, "\tleal -%d(%%esp),%%esp\n",
2501 sizeof(FLOATING_SAVE_AREA) + 6 * sizeof(DWORD) /* DR regs */ );
2502 fprintf( outfile, "\tpushl $0x0001001f\n" ); /* ContextFlags */
2504 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2506 fprintf( outfile, "\tleal %d(%%esp),%%eax\n",
2507 sizeof(CONTEXT) + STACK_SPACE + 4 ); /* %esp at time of call */
2508 fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Esp) );
2510 fprintf( outfile, "\tcall " PREFIX "RELAY_CallFrom32Regs\n" );
2512 /* Restore the context structure */
2514 fprintf( outfile, "\tfrstor %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2516 /* Store %eip value onto the new stack */
2518 fprintf( outfile, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Eip) );
2519 fprintf( outfile, "\tmovl %d(%%esp),%%ebx\n", CONTEXTOFFSET(Esp) );
2520 fprintf( outfile, "\tmovl %%eax,0(%%ebx)\n" );
2522 /* Restore all registers */
2524 fprintf( outfile, "\tleal %d(%%esp),%%esp\n",
2525 sizeof(FLOATING_SAVE_AREA) + 7 * sizeof(DWORD) );
2526 fprintf( outfile, "\tpopl %%eax\n" );
2527 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2528 fprintf( outfile, "\tpopl %%eax\n" );
2529 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2530 fprintf( outfile, "\tpopl %%eax\n" );
2531 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2532 fprintf( outfile, "\tpopl %%eax\n" );
2533 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2535 fprintf( outfile, "\tpopl %%edi\n" );
2536 fprintf( outfile, "\tpopl %%esi\n" );
2537 fprintf( outfile, "\tpopl %%ebx\n" );
2538 fprintf( outfile, "\tpopl %%edx\n" );
2539 fprintf( outfile, "\tpopl %%ecx\n" );
2540 fprintf( outfile, "\tpopl %%eax\n" );
2541 fprintf( outfile, "\tpopl %%ebp\n" );
2542 fprintf( outfile, "\tleal 8(%%esp),%%esp\n" ); /* skip %eip and %cs */
2543 fprintf( outfile, "\tpopfl\n" );
2544 fprintf( outfile, "\tpopl %%esp\n" );
2545 fprintf( outfile, "\tret\n" );
2551 /*******************************************************************
2554 * Build the spec files
2556 static int BuildSpec( FILE *outfile, int argc, char *argv[] )
2559 for (i = 2; i < argc; i++)
2560 if (BuildSpecFile( outfile, argv[i] ) < 0) return -1;
2565 /*******************************************************************
2568 * Build the 16-bit-to-Wine callbacks
2570 static int BuildCallFrom16( FILE *outfile, char * outname, int argc, char *argv[] )
2577 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2578 fprintf( outfile, "\t.text\n" );
2581 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2582 getcwd(buffer, sizeof(buffer));
2585 * The stabs help the internal debugger as they are an indication that it
2586 * is sensible to step into a thunk/trampoline.
2588 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2589 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2590 fprintf( outfile, "\t.text\n" );
2591 fprintf( outfile, "\t.align 4\n" );
2592 fprintf( outfile, "Code_Start:\n\n" );
2594 fprintf( outfile, PREFIX"CallFrom16_Start:\n" );
2595 fprintf( outfile, "\t.globl "PREFIX"CallFrom16_Start\n" );
2597 /* Build the callback functions */
2599 for (i = 2; i < argc; i++) BuildCallFrom16Func( outfile, argv[i] );
2601 /* Build the thunk callback function */
2603 BuildCallFrom16Func( outfile, "t_long_" );
2605 /* Output the argument debugging strings */
2609 fprintf( outfile, "/* Argument strings */\n" );
2610 for (i = 2; i < argc; i++)
2612 fprintf( outfile, "Profile_%s:\t", argv[i] );
2613 fprintf( outfile, STRING " \"%s\\0\"\n", argv[i] + 7 );
2616 fprintf( outfile, PREFIX"CallFrom16_End:\n" );
2617 fprintf( outfile, "\t.globl "PREFIX"CallFrom16_End\n" );
2620 fprintf( outfile, "\t.text\n");
2621 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2622 fprintf( outfile, ".Letext:\n");
2629 /*******************************************************************
2632 * Build the Wine-to-16-bit callbacks
2634 static int BuildCallTo16( FILE *outfile, char * outname, int argc, char *argv[] )
2641 infile = fopen( argv[2], "r" );
2648 else infile = stdin;
2652 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2653 fprintf( outfile, "\t.text\n" );
2656 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2657 getcwd(buffer, sizeof(buffer));
2660 * The stabs help the internal debugger as they are an indication that it
2661 * is sensible to step into a thunk/trampoline.
2663 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2664 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2665 fprintf( outfile, "\t.text\n" );
2666 fprintf( outfile, "\t.align 4\n" );
2667 fprintf( outfile, "Code_Start:\n\n" );
2670 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Start\n" );
2671 fprintf( outfile, PREFIX "CALLTO16_Start:\n" );
2673 /* Build the callback functions */
2675 while (fgets( buffer, sizeof(buffer), infile ))
2677 if (strstr( buffer, "### start build ###" )) break;
2679 while (fgets( buffer, sizeof(buffer), infile ))
2681 char *p = strstr( buffer, "CallTo16_" );
2684 char *profile = p + strlen( "CallTo16_" );
2686 while ((*p == '_') || isalpha(*p)) p++;
2688 BuildCallTo16Func( outfile, profile );
2690 if (strstr( buffer, "### stop build ###" )) break;
2693 /* Output the 16-bit return code */
2695 BuildRet16Func( outfile );
2697 /* Output the CBClient callback functions
2698 * (while this does not really 'call to 16-bit' code, it is placed
2699 * here so that its 16-bit return stub is defined within the CALLTO16
2702 BuildCallTo32CBClient( outfile, FALSE );
2703 BuildCallTo32CBClient( outfile, TRUE );
2706 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_End\n" );
2707 fprintf( outfile, PREFIX "CALLTO16_End:\n" );
2710 fprintf( outfile, "\t.text\n");
2711 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2712 fprintf( outfile, ".Letext:\n");
2720 /*******************************************************************
2723 * Build the 32-bit callbacks
2725 static int BuildCall32( FILE *outfile, char * outname )
2731 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2732 fprintf( outfile, "\t.text\n" );
2737 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2738 getcwd(buffer, sizeof(buffer));
2741 * The stabs help the internal debugger as they are an indication that it
2742 * is sensible to step into a thunk/trampoline.
2744 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2745 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2746 fprintf( outfile, "\t.text\n" );
2747 fprintf( outfile, "\t.align 4\n" );
2748 fprintf( outfile, "Code_Start:\n" );
2751 /* Build the 32-bit large stack callback */
2753 BuildCallTo32LargeStack( outfile );
2755 /* Build the register callback function */
2757 BuildCallFrom32Regs( outfile );
2760 fprintf( outfile, "\t.text\n");
2761 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2762 fprintf( outfile, ".Letext:\n");
2765 #else /* __i386__ */
2767 /* Just to avoid an empty file */
2768 fprintf( outfile, "\t.long 0\n" );
2770 #endif /* __i386__ */
2775 /*******************************************************************
2778 static void usage(void)
2781 "usage: build [-o outfile] -spec SPECNAMES\n"
2782 " build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2783 " build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2784 " build [-o outfile] -call32\n" );
2789 /*******************************************************************
2792 int main(int argc, char **argv)
2794 char *outname = NULL;
2795 FILE *outfile = stdout;
2798 if (argc < 2) usage();
2800 if (!strcmp( argv[1], "-o" ))
2805 if (argc < 2) usage();
2806 if (!(outfile = fopen( outname, "w" )))
2808 fprintf( stderr, "Unable to create output file '%s'\n", outname );
2813 /* Retrieve the selector values; this assumes that we are building
2814 * the asm files on the platform that will also run them. Probably
2815 * a safe assumption to make.
2817 GET_CS( Code_Selector );
2818 GET_DS( Data_Selector );
2820 if (!strcmp( argv[1], "-spec" ))
2821 res = BuildSpec( outfile, argc, argv );
2822 else if (!strcmp( argv[1], "-callfrom16" ))
2823 res = BuildCallFrom16( outfile, outname, argc, argv );
2824 else if (!strcmp( argv[1], "-callto16" ))
2825 res = BuildCallTo16( outfile, outname, argc, argv );
2826 else if (!strcmp( argv[1], "-call32" ))
2827 res = BuildCall32( outfile, outname );