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;
154 char *ParseBuffer = NULL;
159 static int debugging = 1;
161 /* Offset of a structure field relative to the start of the struct */
162 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
164 /* Offset of register relative to the start of the CONTEXT struct */
165 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT,reg)
167 /* Offset of the stack pointer relative to %fs:(0) */
168 #define STACKOFFSET (STRUCTOFFSET(THDB,cur_stack) - STRUCTOFFSET(THDB,teb))
171 static void *xmalloc (size_t size)
175 res = malloc (size ? size : 1);
178 fprintf (stderr, "Virtual memory exhausted.\n");
185 static void *xrealloc (void *ptr, size_t size)
187 void *res = realloc (ptr, size);
190 fprintf (stderr, "Virtual memory exhausted.\n");
197 static int IsNumberString(char *s)
206 static char *strupper(char *s)
210 for(p = s; *p != '\0'; p++)
216 static char * GetTokenInLine(void)
221 if (ParseNext != ParseBuffer)
223 if (ParseSaveChar == '\0')
225 *ParseNext = ParseSaveChar;
229 * Remove initial white space.
231 for (p = ParseNext; isspace(*p); p++)
234 if ((*p == '\0') || (*p == '#'))
241 if (*token != '(' && *token != ')')
242 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
252 static char * GetToken(void)
256 if (ParseBuffer == NULL)
258 ParseBuffer = xmalloc(512);
259 ParseNext = ParseBuffer;
263 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
265 if (ParseBuffer[0] != '#')
270 while ((token = GetTokenInLine()) == NULL)
272 ParseNext = ParseBuffer;
276 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
278 if (ParseBuffer[0] != '#')
287 /*******************************************************************
290 * Parse a variable definition.
292 static int ParseVariable( ORDDEF *odp )
297 int value_array_size;
299 char *token = GetToken();
302 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
303 SpecName, Line, token);
308 value_array_size = 25;
309 value_array = xmalloc(sizeof(*value_array) * value_array_size);
311 while ((token = GetToken()) != NULL)
316 value_array[n_values++] = strtol(token, &endptr, 0);
317 if (n_values == value_array_size)
319 value_array_size += 25;
320 value_array = xrealloc(value_array,
321 sizeof(*value_array) * value_array_size);
324 if (endptr == NULL || *endptr != '\0')
326 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
327 SpecName, Line, token);
334 fprintf(stderr, "%s:%d: End of file in variable declaration\n",
339 odp->u.var.n_values = n_values;
340 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
346 /*******************************************************************
347 * ParseExportFunction
349 * Parse a function definition.
351 static int ParseExportFunction( ORDDEF *odp )
359 if (odp->type == TYPE_STDCALL)
361 fprintf( stderr, "%s:%d: 'stdcall' not supported for Win16\n",
367 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
369 fprintf( stderr, "%s:%d: 'pascal' not supported for Win32\n",
381 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
382 SpecName, Line, token);
386 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
392 if (!strcmp(token, "word"))
393 odp->u.func.arg_types[i] = 'w';
394 else if (!strcmp(token, "s_word"))
395 odp->u.func.arg_types[i] = 's';
396 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
397 odp->u.func.arg_types[i] = 'l';
398 else if (!strcmp(token, "ptr"))
399 odp->u.func.arg_types[i] = 'p';
400 else if (!strcmp(token, "str"))
401 odp->u.func.arg_types[i] = 't';
402 else if (!strcmp(token, "wstr"))
403 odp->u.func.arg_types[i] = 'W';
404 else if (!strcmp(token, "segstr"))
405 odp->u.func.arg_types[i] = 'T';
406 else if (!strcmp(token, "double"))
408 odp->u.func.arg_types[i++] = 'l';
409 odp->u.func.arg_types[i] = 'l';
413 fprintf(stderr, "%s:%d: Unknown variable type '%s'\n",
414 SpecName, Line, token);
417 if (SpecType == SPEC_WIN32)
419 if (strcmp(token, "long") &&
420 strcmp(token, "ptr") &&
421 strcmp(token, "str") &&
422 strcmp(token, "wstr") &&
423 strcmp(token, "double"))
425 fprintf( stderr, "%s:%d: Type '%s' not supported for Win32\n",
426 SpecName, Line, token );
431 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
433 fprintf( stderr, "%s:%d: Too many arguments\n", SpecName, Line );
436 odp->u.func.arg_types[i] = '\0';
437 if ((odp->type == TYPE_STDCALL) && !i)
438 odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
439 if ((odp->type == TYPE_REGISTER) && (SpecType == SPEC_WIN32) && i)
441 fprintf( stderr, "%s:%d: register functions cannot have arguments in Win32\n",
445 strcpy(odp->u.func.link_name, GetToken());
450 /*******************************************************************
453 * Parse an 'equate' definition.
455 static int ParseEquate( ORDDEF *odp )
459 char *token = GetToken();
460 int value = strtol(token, &endptr, 0);
461 if (endptr == NULL || *endptr != '\0')
463 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
464 SpecName, Line, token);
468 if (SpecType == SPEC_WIN32)
470 fprintf( stderr, "%s:%d: 'equate' not supported for Win32\n",
475 odp->u.abs.value = value;
480 /*******************************************************************
483 * Parse a 'return' definition.
485 static int ParseReturn( ORDDEF *odp )
491 odp->u.ret.arg_size = strtol(token, &endptr, 0);
492 if (endptr == NULL || *endptr != '\0')
494 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
495 SpecName, Line, token);
500 odp->u.ret.ret_value = strtol(token, &endptr, 0);
501 if (endptr == NULL || *endptr != '\0')
503 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
504 SpecName, Line, token);
508 if (SpecType == SPEC_WIN32)
510 fprintf( stderr, "%s:%d: 'return' not supported for Win32\n",
519 /*******************************************************************
522 * Parse a 'stub' definition.
524 static int ParseStub( ORDDEF *odp )
526 odp->u.func.arg_types[0] = '\0';
527 strcpy( odp->u.func.link_name, STUB_CALLBACK );
532 /*******************************************************************
535 * Parse an 'varargs' definition.
537 static int ParseVarargs( ORDDEF *odp )
541 if (SpecType == SPEC_WIN16)
543 fprintf( stderr, "%s:%d: 'varargs' not supported for Win16\n",
551 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
552 SpecName, Line, token);
558 fprintf(stderr, "%s:%d: Expected ')' got '%s'\n",
559 SpecName, Line, token);
563 strcpy( odp->u.vargs.link_name, GetToken() );
568 /*******************************************************************
571 * Parse an 'extern' definition.
573 static int ParseExtern( ORDDEF *odp )
575 if (SpecType == SPEC_WIN16)
577 fprintf( stderr, "%s:%d: 'extern' not supported for Win16\n",
581 strcpy( odp->u.ext.link_name, GetToken() );
586 /*******************************************************************
589 * Parse an ordinal definition.
591 static int ParseOrdinal(int ordinal)
596 if (ordinal >= MAX_ORDINALS)
598 fprintf(stderr, "%s:%d: Ordinal number too large\n", SpecName, Line );
601 if (ordinal > Limit) Limit = ordinal;
602 if (ordinal < Base) Base = ordinal;
604 odp = &OrdinalDefinitions[ordinal];
605 if (!(token = GetToken()))
607 fprintf(stderr, "%s:%d: Expected type after ordinal\n", SpecName, Line);
611 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
612 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
615 if (odp->type >= TYPE_NBTYPES)
618 "%s:%d: Expected type after ordinal, found '%s' instead\n",
619 SpecName, Line, token );
623 if (!(token = GetToken()))
625 fprintf( stderr, "%s:%d: Expected name after type\n", SpecName, Line );
628 strcpy( odp->name, token );
636 return ParseVariable( odp );
642 return ParseExportFunction( odp );
644 return ParseEquate( odp );
646 return ParseReturn( odp );
648 return ParseStub( odp );
650 return ParseVarargs( odp );
652 return ParseExtern( odp );
654 fprintf( stderr, "Should not happen\n" );
660 /*******************************************************************
665 static int ParseTopLevel(void)
669 while ((token = GetToken()) != NULL)
671 if (strcmp(token, "name") == 0)
673 strcpy(DLLName, GetToken());
675 if (!DLLFileName[0]) sprintf( DLLFileName, "%s.DLL", DLLName );
677 else if (strcmp(token, "file") == 0)
679 strcpy(DLLFileName, GetToken());
680 strupper(DLLFileName);
682 else if (strcmp(token, "type") == 0)
685 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
686 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
689 fprintf(stderr, "%s:%d: Type must be 'win16' or 'win32'\n",
694 else if (strcmp(token, "heap") == 0)
697 if (!IsNumberString(token))
699 fprintf(stderr, "%s:%d: Expected number after heap\n",
703 DLLHeapSize = atoi(token);
705 else if (IsNumberString(token))
710 ordinal = atoi(token);
711 if ((rv = ParseOrdinal(ordinal)) < 0)
717 "%s:%d: Expected name, id, length or ordinal\n",
727 /*******************************************************************
730 * Store a list of ints into a byte array.
732 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
739 for (i = 0; i < odp->u.var.n_values; i++)
740 buffer[i] = odp->u.var.values[i];
743 for (i = 0; i < odp->u.var.n_values; i++)
744 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
747 for (i = 0; i < odp->u.var.n_values; i++)
748 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
751 return odp->u.var.n_values * size;
755 /*******************************************************************
758 * Dump a byte stream into the assembly code.
760 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
761 const char *section, const char *label_start )
764 if (section) fprintf( outfile, "\t%s\n", section );
765 if (label_start) fprintf( outfile, "%s:\n", label_start );
766 for (i = 0; i < len; i++)
768 if (!(i & 0x0f)) fprintf( outfile, "\t.byte " );
769 fprintf( outfile, "%d", *data++ );
771 fprintf( outfile, "%c", ((i & 0x0f) != 0x0f) ? ',' : '\n' );
773 fprintf( outfile, "\n" );
777 /*******************************************************************
780 * Build the in-memory representation of a 16-bit NE module, and dump it
781 * as a byte stream into the assembly code.
783 static int BuildModule16( FILE *outfile, int max_code_offset,
784 int max_data_offset )
790 SEGTABLEENTRY *pSegment;
797 * OFSTRUCT File information
798 * SEGTABLEENTRY Segment 1 (code)
799 * SEGTABLEENTRY Segment 2 (data)
800 * WORD[2] Resource table (empty)
801 * BYTE[2] Imported names (empty)
802 * BYTE[n] Resident names table
803 * BYTE[n] Entry table
806 buffer = xmalloc( 0x10000 );
808 pModule = (NE_MODULE *)buffer;
809 pModule->magic = IMAGE_OS2_SIGNATURE;
812 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
814 pModule->heap_size = DLLHeapSize;
815 pModule->stack_size = 0;
820 pModule->seg_count = 2;
821 pModule->modref_count = 0;
822 pModule->nrname_size = 0;
823 pModule->modref_table = 0;
824 pModule->nrname_fpos = 0;
825 pModule->moveable_entries = 0;
826 pModule->alignment = 0;
827 pModule->truetype = 0;
828 pModule->os_flags = NE_OSFLAGS_WINDOWS;
829 pModule->misc_flags = 0;
830 pModule->dlls_to_init = 0;
831 pModule->nrname_handle = 0;
832 pModule->min_swap_area = 0;
833 pModule->expected_version = 0x030a;
834 pModule->module32 = 0;
836 pModule->self_loading_sel = 0;
838 /* File information */
840 pFileInfo = (OFSTRUCT *)(pModule + 1);
841 pModule->fileinfo = (int)pFileInfo - (int)pModule;
842 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
843 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
844 + strlen(DLLFileName);
845 strcpy( pFileInfo->szPathName, DLLFileName );
846 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
850 pSegment = (SEGTABLEENTRY *)pstr;
851 pModule->seg_table = (int)pSegment - (int)pModule;
852 pSegment->filepos = 0;
853 pSegment->size = max_code_offset;
855 pSegment->minsize = max_code_offset;
856 pSegment->selector = 0;
859 pModule->dgroup_entry = (int)pSegment - (int)pModule;
860 pSegment->filepos = 0;
861 pSegment->size = max_data_offset;
862 pSegment->flags = NE_SEGFLAGS_DATA;
863 pSegment->minsize = max_data_offset;
864 pSegment->selector = 0;
869 pword = (WORD *)pSegment;
870 pModule->res_table = (int)pword - (int)pModule;
874 /* Imported names table */
876 pstr = (char *)pword;
877 pModule->import_table = (int)pstr - (int)pModule;
881 /* Resident names table */
883 pModule->name_table = (int)pstr - (int)pModule;
884 /* First entry is module name */
885 *pstr = strlen(DLLName );
886 strcpy( pstr + 1, DLLName );
889 pstr += sizeof(WORD);
890 /* Store all ordinals */
891 odp = OrdinalDefinitions + 1;
892 for (i = 1; i <= Limit; i++, odp++)
894 if (!odp->name[0]) continue;
895 *pstr = strlen( odp->name );
896 strcpy( pstr + 1, odp->name );
897 strupper( pstr + 1 );
900 pstr += sizeof(WORD);
906 pModule->entry_table = (int)pstr - (int)pModule;
908 odp = OrdinalDefinitions + 1;
909 for (i = 1; i <= Limit; i++, odp++)
921 selector = 1; /* Code selector */
927 selector = 2; /* Data selector */
931 selector = 0xfe; /* Constant selector */
935 selector = 0; /* Invalid selector */
939 /* create a new bundle if necessary */
940 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
944 bundle[1] = selector;
952 *(WORD *)pstr = odp->offset;
953 pstr += sizeof(WORD);
958 /* Dump the module content */
960 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
961 ".data", "Module_Start" );
962 return (int)pstr - (int)pModule;
966 /*******************************************************************
969 * Build a Win32 C file from a spec file.
971 static int BuildSpec32File( char * specfile, FILE *outfile )
976 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
978 fprintf( outfile, "#include \"builtin32.h\"\n\n" );
980 /* Output code for all stubs functions */
982 fprintf( outfile, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
984 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
986 if (odp->type != TYPE_STUB) continue;
987 fprintf( outfile, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
991 /* Output code for all register functions */
993 fprintf( outfile, "#ifdef __i386__\n" );
994 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
996 if (odp->type != TYPE_REGISTER) continue;
998 "__asm__(\".align 4\\n\\t\"\n"
999 " \".globl " PREFIX "%s\\n\\t\"\n"
1000 " \".type " PREFIX "%s,@function\\n\\t\"\n"
1001 " \"" PREFIX "%s:\\n\\t\"\n"
1002 " \"pushl $" PREFIX "__regs_%s\\n\\t\"\n"
1003 " \"pushl $" PREFIX "CALL32_Regs\\n\\t\"\n"
1005 odp->u.func.link_name, odp->u.func.link_name,
1006 odp->u.func.link_name, odp->u.func.link_name );
1008 fprintf( outfile, "#endif\n" );
1010 /* Output the DLL functions prototypes */
1012 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1017 fprintf( outfile, "extern void %s();\n", odp->u.vargs.link_name );
1020 fprintf( outfile, "extern void %s();\n", odp->u.ext.link_name );
1025 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1031 fprintf(stderr,"build: function type %d not available for Win32\n",
1037 /* Output the DLL functions table */
1039 fprintf( outfile, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1041 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1046 fprintf( outfile, " 0" );
1049 fprintf( outfile, " %s", odp->u.vargs.link_name );
1052 fprintf( outfile, " %s", odp->u.ext.link_name );
1057 fprintf( outfile, " %s", odp->u.func.link_name);
1060 fprintf( outfile, " __stub_%d", i );
1065 if (i < Limit) fprintf( outfile, ",\n" );
1067 fprintf( outfile, "\n};\n\n" );
1069 /* Output the DLL names table */
1072 fprintf( outfile, "static const char * const FuncNames[] =\n{\n" );
1073 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1075 if (odp->type == TYPE_INVALID) continue;
1076 if (nb_names++) fprintf( outfile, ",\n" );
1077 fprintf( outfile, " \"%s\"", odp->name );
1079 fprintf( outfile, "\n};\n\n" );
1081 /* Output the DLL argument types */
1083 fprintf( outfile, "static const unsigned int ArgTypes[%d] =\n{\n",
1085 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1087 unsigned int j, mask = 0;
1088 if ((odp->type == TYPE_STDCALL) || (odp->type == TYPE_CDECL))
1089 for (j = 0; odp->u.func.arg_types[j]; j++)
1091 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
1092 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
1094 fprintf( outfile, " %d", mask );
1095 if (i < Limit) fprintf( outfile, ",\n" );
1097 fprintf( outfile, "\n};\n\n" );
1099 /* Output the DLL ordinals table */
1101 fprintf( outfile, "static const unsigned short FuncOrdinals[] =\n{\n" );
1103 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1105 if (odp->type == TYPE_INVALID) continue;
1106 if (nb_names++) fprintf( outfile, ",\n" );
1107 fprintf( outfile, " %d", i - Base );
1109 fprintf( outfile, "\n};\n\n" );
1111 /* Output the DLL functions arguments */
1113 fprintf( outfile, "static const unsigned char FuncArgs[%d] =\n{\n",
1115 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1121 args = (unsigned char)strlen(odp->u.func.arg_types);
1124 args = 0x80 | (unsigned char)strlen(odp->u.func.arg_types);
1133 fprintf( outfile, " 0x%02x", args );
1134 if (i < Limit) fprintf( outfile, ",\n" );
1136 fprintf( outfile, "\n};\n\n" );
1138 /* Output the DLL descriptor */
1140 fprintf( outfile, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1142 fprintf( outfile, " \"%s\",\n", DLLName );
1143 fprintf( outfile, " %d,\n", Base );
1144 fprintf( outfile, " %d,\n", Limit - Base + 1 );
1145 fprintf( outfile, " %d,\n", nb_names );
1157 /*******************************************************************
1160 * Build a Win16 assembly file from a spec file.
1162 static int BuildSpec16File( char * specfile, FILE *outfile )
1166 int code_offset, data_offset, module_size;
1167 unsigned char *data;
1169 data = (unsigned char *)xmalloc( 0x10000 );
1170 memset( data, 0, 16 );
1173 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
1174 fprintf( outfile, "\t.text\n" );
1175 fprintf( outfile, "Code_Start:\n" );
1178 odp = OrdinalDefinitions;
1179 for (i = 0; i <= Limit; i++, odp++)
1184 odp->offset = 0xffff;
1188 odp->offset = LOWORD(odp->u.abs.value);
1192 odp->offset = data_offset;
1193 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1197 odp->offset = data_offset;
1198 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1202 odp->offset = data_offset;
1203 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1207 fprintf( outfile,"/* %s.%d */\n", DLLName, i);
1208 fprintf( outfile,"\tmovw $%d,%%ax\n",LOWORD(odp->u.ret.ret_value));
1209 fprintf( outfile,"\tmovw $%d,%%dx\n",HIWORD(odp->u.ret.ret_value));
1210 fprintf( outfile,"\t.byte 0x66\n");
1211 if (odp->u.ret.arg_size != 0)
1212 fprintf( outfile, "\tlret $%d\n\n", odp->u.ret.arg_size);
1215 fprintf( outfile, "\tlret\n");
1216 fprintf( outfile, "\tnop\n");
1217 fprintf( outfile, "\tnop\n\n");
1219 odp->offset = code_offset;
1220 code_offset += 12; /* Assembly code is 12 bytes long */
1226 case TYPE_PASCAL_16:
1228 fprintf( outfile, "/* %s.%d */\n", DLLName, i);
1229 fprintf( outfile, "\tpushw %%bp\n" );
1230 fprintf( outfile, "\tpushl $" PREFIX "%s\n",odp->u.func.link_name);
1231 /* FreeBSD does not understand lcall, so do it the hard way */
1232 fprintf( outfile, "\t.byte 0x9a\n" );
1233 fprintf( outfile, "\t.long " PREFIX "CallFrom16_%s_%s_%s\n",
1234 (odp->type == TYPE_CDECL) ? "c" : "p",
1235 (odp->type == TYPE_REGISTER) ? "regs" :
1236 (odp->type == TYPE_PASCAL_16) ? "word" : "long",
1237 odp->u.func.arg_types );
1238 fprintf( outfile, "\t.long 0x%08lx\n",
1239 MAKELONG( Code_Selector, 0x9090 /* nop ; nop */ ) );
1240 odp->offset = code_offset;
1241 code_offset += 16; /* Assembly code is 16 bytes long */
1245 fprintf(stderr,"build: function type %d not available for Win16\n",
1251 if (!code_offset) /* Make sure the code segment is not empty */
1253 fprintf( outfile, "\t.byte 0\n" );
1257 /* Output data segment */
1259 DumpBytes( outfile, data, data_offset, NULL, "Data_Start" );
1261 /* Build the module */
1263 module_size = BuildModule16( outfile, code_offset, data_offset );
1265 /* Output the DLL descriptor */
1267 fprintf( outfile, "\t.text\n" );
1268 fprintf( outfile, "DLLName:\t" STRING " \"%s\\0\"\n", DLLName );
1269 fprintf( outfile, "\t.align 4\n" );
1270 fprintf( outfile, "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1271 fprintf( outfile, PREFIX "%s_Descriptor:\n", DLLName );
1272 fprintf( outfile, "\t.long DLLName\n" ); /* Name */
1273 fprintf( outfile, "\t.long Module_Start\n" ); /* Module start */
1274 fprintf( outfile, "\t.long %d\n", module_size ); /* Module size */
1275 fprintf( outfile, "\t.long Code_Start\n" ); /* Code start */
1276 fprintf( outfile, "\t.long Data_Start\n" ); /* Data start */
1281 /*******************************************************************
1284 * Build an assembly file from a spec file.
1286 static int BuildSpecFile( FILE *outfile, char *specname )
1288 SpecName = specname;
1289 SpecFp = fopen( specname, "r");
1292 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1296 if (ParseTopLevel() < 0) return -1;
1301 return BuildSpec16File( specname, outfile );
1303 return BuildSpec32File( specname, outfile );
1305 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1311 /*******************************************************************
1312 * TransferArgs16To32
1314 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1315 * The 16-bit stack layout is:
1323 * For 'cdecl' argn up to arg1 are reversed.
1325 static int TransferArgs16To32( FILE *outfile, char *args, int usecdecl )
1327 int i, pos16, pos32;
1330 /* Copy the arguments */
1332 pos16 = 6; /* skip bp and return address */
1333 pos32 = usecdecl ? -(strlen(args) * 4) : 0;
1334 xargs = usecdecl ? args:args+strlen(args);
1336 for (i = strlen(args); i > 0; i--)
1344 case 'w': /* word */
1345 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1346 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1350 case 's': /* s_word */
1351 fprintf( outfile, "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1352 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1356 case 'l': /* long or segmented pointer */
1357 case 'T': /* segmented pointer to null-terminated string */
1358 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", pos16 );
1359 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1363 case 'p': /* linear pointer */
1364 case 't': /* linear pointer to null-terminated string */
1365 /* Get the selector */
1366 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1367 /* Get the selector base */
1368 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1369 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1370 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1371 /* Add the offset */
1372 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1373 fprintf( outfile, "\taddl %%eax,%d(%%ebx)\n", pos32 );
1378 fprintf( stderr, "Unknown arg type '%c'\n", *xargs );
1386 return pos16 - 6; /* Return the size of the 16-bit args */
1390 /*******************************************************************
1393 * Build the context structure on the 32-bit stack.
1395 static void BuildContext16( FILE *outfile )
1397 /* Store the registers */
1399 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1400 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1401 fprintf( outfile, "\tmovl %%ecx,%d(%%ebx)\n",
1402 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1403 fprintf( outfile, "\tmovl %%edx,%d(%%ebx)\n",
1404 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1405 fprintf( outfile, "\tmovl %%esi,%d(%%ebx)\n",
1406 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1407 fprintf( outfile, "\tmovl %%edi,%d(%%ebx)\n",
1408 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1410 fprintf( outfile, "\tmovl -20(%%ebp),%%eax\n" ); /* Get %ebx from stack*/
1411 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1412 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1413 fprintf( outfile, "\tmovzwl -10(%%ebp),%%eax\n" ); /* Get %ds from stack*/
1414 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1415 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1416 fprintf( outfile, "\tmovzwl -6(%%ebp),%%eax\n" ); /* Get %es from stack*/
1417 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1418 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1419 fprintf( outfile, "\tpushfl\n" );
1420 fprintf( outfile, "\tpopl %d(%%ebx)\n",
1421 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1422 fprintf( outfile, "\tmovl -16(%%ebp),%%eax\n" ); /* Get %ebp from stack */
1423 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1424 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1425 fprintf( outfile, "\tmovzwl 2(%%ebp),%%eax\n" ); /* Get %ip from stack */
1426 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1427 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1428 fprintf( outfile, "\tleal 2(%%ebp),%%eax\n" ); /* Get initial %sp */
1429 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1430 CONTEXTOFFSET(Esp) - sizeof(CONTEXT) );
1431 fprintf( outfile, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1432 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1433 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1434 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
1435 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1436 CONTEXTOFFSET(SegFs) - sizeof(CONTEXT) );
1437 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1438 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1439 CONTEXTOFFSET(SegGs) - sizeof(CONTEXT) );
1440 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1441 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1442 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1444 fprintf( outfile, "\tfsave %d(%%ebx)\n",
1445 CONTEXTOFFSET(FloatSave) - sizeof(CONTEXT) );
1450 /*******************************************************************
1453 * Restore the registers from the context structure.
1455 static void RestoreContext16( FILE *outfile )
1457 /* Get the 32-bit stack pointer */
1459 fprintf( outfile, "\tleal -%d(%%ebp),%%ebx\n",
1460 STRUCTOFFSET(STACK32FRAME,ebp) );
1462 /* Remove everything up to (including) the return address
1463 * from the 16-bit stack */
1465 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1466 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1467 fprintf( outfile, "\tmovw %%ax,%%ss\n" );
1468 fprintf( outfile, "\tmovl %d(%%ebx),%%esp\n",
1469 CONTEXTOFFSET(Esp) - sizeof(CONTEXT) );
1470 fprintf( outfile, "\taddl $4,%%esp\n" ); /* Remove return address */
1472 /* Restore the registers */
1474 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n",
1475 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1476 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n",
1477 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1478 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n",
1479 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1480 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n",
1481 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1482 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n",
1483 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1484 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new cs */
1485 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1486 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new ip */
1487 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1488 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new ds */
1489 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1490 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new es */
1491 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1492 fprintf( outfile, "\tpushl %d(%%ebx)\n",
1493 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1494 fprintf( outfile, "\tpopfl\n" );
1495 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1496 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1497 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n",
1498 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1499 fprintf( outfile, "\tpopl %%es\n" ); /* Set es */
1500 fprintf( outfile, "\tpopl %%ds\n" ); /* Set ds */
1504 /*******************************************************************
1505 * BuildCallFrom16Func
1507 * Build a 16-bit-to-Wine callback function. The syntax of the function
1508 * profile is: call_type_xxxxx, where 'call' is the letter 'c' or 'p' for C or
1509 * Pascal calling convention, 'type' is one of 'regs', 'word' or
1510 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1511 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1512 * 'T'=segmented pointer to null-terminated string).
1513 * For register functions, the arguments are ignored, but they are still
1514 * removed from the stack upon return.
1516 * A special variant of the callback function is generated by the function
1517 * profile "t_long_". This is used by the Win95 16->32 thunk
1518 * functions C16ThkSL and C16ThkSL01 and is implemented as follows:
1519 * On entry, the EBX register is set up to contain a flat pointer to the
1520 * 16-bit stack such that EBX+22 points to the first argument.
1521 * Then, the entry point is called, while EBP is set up to point
1522 * to the return address (on the 32-bit stack).
1523 * The called function returns with CX set to the number of bytes
1524 * to be popped of the caller's stack.
1526 * Stack layout upon entry to the callback function:
1528 * (sp+18) word first 16-bit arg
1532 * (sp+8) long 32-bit entry point (used to store edx)
1533 * (sp+6) word high word of cs (always 0, used to store es)
1534 * (sp+4) word low word of cs of 16-bit entry point
1535 * (sp+2) word high word of ip (always 0, used to store ds)
1536 * (sp) word low word of ip of 16-bit entry point
1538 * Added on the stack:
1540 * (sp-8) long saved previous stack
1542 static void BuildCallFrom16Func( FILE *outfile, char *profile )
1549 char *args = profile + 7;
1551 /* Parse function type */
1553 if (!strncmp( "c_", profile, 2 )) cdecl = 1;
1554 else if (!strncmp( "t_", profile, 2 )) thunk = 1;
1555 else if (strncmp( "p_", profile, 2 ))
1557 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1561 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
1562 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
1563 else if (strncmp( "long_", profile + 2, 5 ))
1565 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1569 /* Function header */
1571 fprintf( outfile, "\n\t.align 4\n" );
1573 fprintf( outfile, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX "CallFrom16_%s\n",
1576 fprintf( outfile, "\t.globl " PREFIX "CallFrom16_%s\n", profile );
1577 fprintf( outfile, PREFIX "CallFrom16_%s:\n", profile );
1579 /* Setup bp to point to its copy on the stack */
1581 fprintf( outfile, "\tpushl %%ebp\n" ); /* Save the full 32-bit ebp */
1582 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1583 fprintf( outfile, "\taddw $16,%%bp\n" );
1585 /* Save 16-bit ds and es */
1587 /* Stupid FreeBSD assembler doesn't know these either */
1588 /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1589 fprintf( outfile, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1590 /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1591 fprintf( outfile, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1595 fprintf( outfile, "\tpushl %%ebx\n" );
1597 /* Restore 32-bit segment registers */
1599 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
1601 fprintf( outfile, "\tdata16\n");
1603 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
1605 fprintf( outfile, "\tdata16\n");
1607 fprintf( outfile, "\tmovw %%bx,%%es\n" );
1608 fprintf( outfile, "\tmovw " PREFIX "CALLTO16_Current_fs,%%fs\n" );
1610 /* Get the 32-bit stack pointer from the TEB */
1612 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET );
1614 /* Save the 16-bit stack */
1617 fprintf( outfile,"\tdata16\n");
1619 fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss,(%d)\n", STACKOFFSET + 2 );
1620 fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp,(%d)\n", STACKOFFSET );
1622 /* Transfer the arguments */
1624 if (reg_func) BuildContext16( outfile );
1625 else if (*args) argsize = TransferArgs16To32( outfile, args, cdecl );
1628 /* Get the stack selector base */
1629 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1630 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1631 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1632 fprintf( outfile, "\tmovl %%eax,-20(%%ebp)\n" );
1633 /* Add the offset */
1634 fprintf( outfile, "\tleal -16(%%ebp),%%eax\n" );
1635 fprintf( outfile, "\taddl %%eax,-20(%%ebp)\n" );
1638 /* Get the address of the API function */
1640 fprintf( outfile, "\tmovl -4(%%ebp),%%eax\n" );
1642 /* If necessary, save %edx over the API function address */
1644 if (!reg_func && short_ret)
1645 fprintf( outfile, "\tmovl %%edx,-4(%%ebp)\n" );
1647 /* Restore %ebx and store the 32-bit stack pointer instead */
1649 fprintf( outfile, "\tmovl %%ebx,%%ebp\n" );
1650 fprintf( outfile, "\tpopl %%ebx\n" );
1651 fprintf( outfile, "\tpushl %%ebp\n" );
1653 /* Switch to the 32-bit stack */
1655 fprintf( outfile, "\tpushl %%ds\n" );
1656 fprintf( outfile, "\tpopl %%ss\n" );
1657 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1658 reg_func ? sizeof(CONTEXT) : 4 * strlen(args) );
1659 if (reg_func) /* Push the address of the context struct */
1660 fprintf( outfile, "\tpushl %%esp\n" );
1662 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1664 fprintf( outfile, "\taddl $%d,%%ebp\n", STRUCTOFFSET(STACK32FRAME,ebp) );
1666 /* Print the debug information before the call */
1668 if (debugging && !thunk)
1672 if (cdecl) ftype |= 4;
1673 if (reg_func) ftype |= 2;
1674 if (short_ret) ftype |= 1;
1676 fprintf( outfile, "\tpushl %%eax\n" );
1677 fprintf( outfile, "\tpushl $Profile_%s\n", profile );
1678 fprintf( outfile, "\tpushl $%d\n", ftype );
1679 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n" );
1680 fprintf( outfile, "\tpopl %%eax\n" );
1681 fprintf( outfile, "\tpopl %%eax\n" );
1682 fprintf( outfile, "\tpopl %%eax\n" );
1685 /* Call the entry point */
1689 fprintf( outfile, "\tpushl %%ebp\n" );
1690 fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
1691 fprintf( outfile, "\tcall *%%eax\n" );
1692 fprintf( outfile, "\tpopl %%ebp\n" );
1695 fprintf( outfile, "\tcall *%%eax\n" );
1698 /* Print the debug information after the call */
1700 if (debugging && !thunk)
1704 /* Push again the address of the context struct in case */
1705 /* it has been removed by an stdcall function */
1706 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1707 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME,ebp) );
1708 fprintf( outfile, "\tpushl %%esp\n" );
1710 fprintf( outfile, "\tpushl %%eax\n" );
1711 fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1712 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n" );
1713 fprintf( outfile, "\tpopl %%eax\n" );
1714 fprintf( outfile, "\tpopl %%eax\n" );
1717 /* Restore the 16-bit stack */
1720 fprintf( outfile, "\tdata16\n");
1722 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2 );
1723 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
1724 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
1725 fprintf( outfile, "\tmovw %%fs," PREFIX "CALLTO16_Current_fs\n" );
1729 /* Calc the arguments size */
1745 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1750 /* Restore registers from the context structure */
1751 RestoreContext16( outfile );
1755 /* Restore high 16 bits of ebp */
1756 fprintf( outfile, "\tpopl %%ebp\n" );
1758 /* Restore ds and es */
1759 fprintf( outfile, "\tincl %%esp\n" ); /* Remove ip */
1760 fprintf( outfile, "\tincl %%esp\n" );
1761 fprintf( outfile, "\tpopl %%edx\n" ); /* Remove cs and ds */
1762 fprintf( outfile, "\tmovw %%dx,%%ds\n" ); /* and restore ds */
1763 fprintf( outfile, "\t.byte 0x66\n\tpopl %%es\n" ); /* Restore es */
1765 if (short_ret) fprintf( outfile, "\tpopl %%edx\n" ); /* Restore edx */
1768 /* Get the return value into dx:ax */
1769 fprintf( outfile, "\tmovl %%eax,%%edx\n" );
1770 fprintf( outfile, "\tshrl $16,%%edx\n" );
1771 /* Remove API entry point */
1772 fprintf( outfile, "\taddl $4,%%esp\n" );
1775 /* Restore low 16 bits of ebp */
1776 fprintf( outfile, "\tpopw %%bp\n" );
1779 /* Remove the arguments and return */
1783 fprintf( outfile, "\tpopl %%ebx\n" );
1784 fprintf( outfile, "\txorb %%ch,%%ch\n" );
1785 fprintf( outfile, "\taddw %%cx, %%sp\n" );
1786 fprintf( outfile, "\tpushl %%ebx\n" );
1787 fprintf( outfile, "\t.byte 0x66\n" );
1788 fprintf( outfile, "\tlret\n" );
1790 else if (argsize && !cdecl)
1792 fprintf( outfile, "\t.byte 0x66\n" );
1793 fprintf( outfile, "\tlret $%d\n", argsize );
1797 fprintf( outfile, "\t.byte 0x66\n" );
1798 fprintf( outfile, "\tlret\n" );
1803 /*******************************************************************
1806 * Build a Wine-to-16-bit callback function.
1808 * Stack frame of the callback function:
1812 * (ebp+8) func to call
1813 * (ebp+4) return address
1814 * (ebp) previous ebp
1816 * Prototypes for the CallTo16 functions:
1817 * extern WINAPI WORD CallTo16_word_xxx( FARPROC16 func, args... );
1818 * extern WINAPI LONG CallTo16_long_xxx( FARPROC16 func, args... );
1819 * extern WINAPI void CallTo16_sreg_( const CONTEXT *context, int nb_args );
1820 * extern WINAPI void CallTo16_lreg_( const CONTEXT *context, int nb_args );
1822 static void BuildCallTo16Func( FILE *outfile, char *profile )
1826 char *args = profile + 5;
1828 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1829 else if (!strncmp( "sreg_", profile, 5 )) reg_func = 1;
1830 else if (!strncmp( "lreg_", profile, 5 )) reg_func = 2;
1831 else if (strncmp( "long_", profile, 5 ))
1833 fprintf( stderr, "Invalid function name '%s'.\n", profile );
1837 /* Function header */
1839 fprintf( outfile, "\n\t.align 4\n" );
1841 fprintf( outfile, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX "CallTo16_%s\n",
1844 fprintf( outfile, "\t.globl " PREFIX "CallTo16_%s\n", profile );
1845 fprintf( outfile, PREFIX "CallTo16_%s:\n", profile );
1849 fprintf( outfile, "\tpushl %%ebp\n" );
1850 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
1852 /* Call the actual CallTo16 routine (simulate a lcall) */
1854 fprintf( outfile, "\tpushl %%cs\n" );
1855 fprintf( outfile, "\tcall do_callto16_%s\n", profile );
1859 /* FIXME: this is a hack because of task.c */
1860 if (!strcmp( profile, "word_" ))
1862 fprintf( outfile, ".globl " PREFIX "CALLTO16_Restore\n" );
1863 fprintf( outfile, PREFIX "CALLTO16_Restore:\n" );
1865 fprintf( outfile, "\tpopl %%ebp\n" );
1866 fprintf( outfile, "\tret $%d\n", 4 * strlen(args) + 4 );
1868 /* Start of the actual CallTo16 routine */
1870 /* Save the 32-bit registers */
1872 fprintf( outfile, "do_callto16_%s:\n", profile );
1873 fprintf( outfile, "\tpushl %%ebx\n" );
1874 fprintf( outfile, "\tpushl %%ecx\n" );
1875 fprintf( outfile, "\tpushl %%edx\n" );
1876 fprintf( outfile, "\tpushl %%esi\n" );
1877 fprintf( outfile, "\tpushl %%edi\n" );
1879 /* Print debugging info */
1883 /* Push the address of the first argument */
1884 fprintf( outfile, "\tleal 8(%%ebp),%%eax\n" );
1885 fprintf( outfile, "\tpushl $%d\n", reg_func ? -1 : strlen(args) );
1886 fprintf( outfile, "\tpushl %%eax\n" );
1887 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
1888 fprintf( outfile, "\tpopl %%eax\n" );
1889 fprintf( outfile, "\tpopl %%eax\n" );
1892 /* Save the 32-bit stack and %fs */
1894 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
1895 fprintf( outfile, "\tmovl %%ebp,%%ebx\n" );
1896 fprintf( outfile, "\tmovl %%esp,%%edx\n" );
1897 fprintf( outfile, "\tmovw %%fs," PREFIX "CALLTO16_Current_fs\n" );
1901 /* Switch to the 16-bit stack, saving the current %%esp, */
1902 /* and adding the specified offset to the new sp */
1903 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d),%%eax\n", STACKOFFSET );
1904 fprintf( outfile, "\tsubl 12(%%ebx),%%eax\n" ); /* Get the offset */
1906 fprintf( outfile,"\tdata16\n");
1908 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
1909 fprintf( outfile, "\tmovl %%eax,%%esp\n" );
1910 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
1912 /* Get the registers. ebx is handled later on. */
1914 fprintf( outfile, "\tmovl 8(%%ebx),%%ebx\n" );
1915 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs) );
1916 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1917 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp) );
1918 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax) );
1919 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx) );
1920 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx) );
1921 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi) );
1922 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi) );
1924 /* Push the return address
1925 * With sreg suffix, we push 16:16 address (normal lret)
1926 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
1929 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_long\n" );
1932 fprintf( outfile, "\tpushw $0\n" );
1933 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_eax+2\n" );
1934 fprintf( outfile, "\tpushw $0\n" );
1935 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_eax\n" );
1938 /* Push the called routine address */
1940 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
1941 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
1943 /* Get the 16-bit ds */
1945 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
1946 /* Get ebx from the 32-bit stack */
1947 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(Ebx) );
1948 fprintf( outfile, "\tpopl %%ds\n" );
1950 else /* not a register function */
1952 int pos = 12; /* first argument position */
1954 /* Switch to the 16-bit stack */
1956 fprintf( outfile,"\tdata16\n");
1958 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
1959 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
1960 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
1962 /* Make %bp point to the previous stackframe (built by CallFrom16) */
1963 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1964 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n",
1965 STRUCTOFFSET(STACK16FRAME,bp) );
1967 /* Transfer the arguments */
1973 case 'w': /* word */
1974 fprintf( outfile, "\tpushw %d(%%ebx)\n", pos );
1976 case 'l': /* long */
1977 fprintf( outfile, "\tpushl %d(%%ebx)\n", pos );
1980 fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
1986 /* Push the return address */
1988 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_%s\n",
1989 short_ret ? "word" : "long" );
1991 /* Push the called routine address */
1993 fprintf( outfile, "\tpushl 8(%%ebx)\n" );
1995 /* Set %ds and %es (and %ax just in case) equal to %ss */
1997 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1998 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
1999 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2002 /* Jump to the called routine */
2004 fprintf( outfile, "\t.byte 0x66\n" );
2005 fprintf( outfile, "\tlret\n" );
2009 /*******************************************************************
2012 * Build the return code for 16-bit callbacks
2014 static void BuildRet16Func( FILE *outfile )
2016 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_word\n" );
2017 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_long\n" );
2018 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_eax\n" );
2020 fprintf( outfile, PREFIX "CALLTO16_Ret_word:\n" );
2021 fprintf( outfile, "\txorl %%edx,%%edx\n" );
2023 /* Put return value into %eax */
2025 fprintf( outfile, PREFIX "CALLTO16_Ret_long:\n" );
2026 fprintf( outfile, "\tshll $16,%%edx\n" );
2027 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
2028 fprintf( outfile, "\tmovl %%edx,%%eax\n" );
2029 fprintf( outfile, PREFIX "CALLTO16_Ret_eax:\n" );
2031 /* Restore 32-bit segment registers */
2033 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
2035 fprintf( outfile, "\tdata16\n");
2037 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
2039 fprintf( outfile, "\tdata16\n");
2041 fprintf( outfile, "\tmovw %%bx,%%es\n" );
2042 fprintf( outfile, "\tmovw " PREFIX "CALLTO16_Current_fs,%%fs\n" );
2044 /* Restore the 32-bit stack */
2047 fprintf( outfile, "\tdata16\n");
2049 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
2050 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
2051 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2053 /* Restore the 32-bit registers */
2055 fprintf( outfile, "\tpopl %%edi\n" );
2056 fprintf( outfile, "\tpopl %%esi\n" );
2057 fprintf( outfile, "\tpopl %%edx\n" );
2058 fprintf( outfile, "\tpopl %%ecx\n" );
2059 fprintf( outfile, "\tpopl %%ebx\n" );
2061 /* Return to caller */
2063 fprintf( outfile, "\tlret\n" );
2065 /* Declare the return address variables */
2067 fprintf( outfile, "\t.data\n" );
2068 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_word\n" );
2069 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_long\n" );
2070 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_eax\n" );
2071 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Current_fs\n" );
2072 fprintf( outfile, PREFIX "CALLTO16_RetAddr_word:\t.long 0\n" );
2073 fprintf( outfile, PREFIX "CALLTO16_RetAddr_long:\t.long 0\n" );
2074 fprintf( outfile, PREFIX "CALLTO16_RetAddr_eax:\t.long 0\n" );
2075 fprintf( outfile, PREFIX "CALLTO16_Current_fs:\t.long 0\n" );
2076 fprintf( outfile, "\t.text\n" );
2080 /*******************************************************************
2081 * BuildCallTo32LargeStack
2083 * Build the function used to switch to the original 32-bit stack
2084 * before calling a 32-bit function from 32-bit code. This is used for
2085 * functions that need a large stack, like X bitmaps functions.
2087 * The generated function has the following prototype:
2088 * int xxx( int (*func)(), void *arg );
2090 * The pointer to the function can be retrieved by calling CALL32_Init,
2091 * which also takes care of saving the current 32-bit stack pointer.
2100 static void BuildCallTo32LargeStack( FILE *outfile )
2102 /* Initialization function */
2104 fprintf( outfile, "\n\t.align 4\n" );
2106 fprintf( outfile, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX "CALL32_Init\n");
2108 fprintf( outfile, "\t.globl " PREFIX "CALL32_Init\n" );
2109 fprintf( outfile, "\t.type " PREFIX "CALL32_Init,@function\n" );
2110 fprintf( outfile, PREFIX "CALL32_Init:\n" );
2111 fprintf( outfile, "\tleal -256(%%esp),%%eax\n" );
2112 fprintf( outfile, "\tmovl %%eax,CALL32_Original32_esp\n" );
2113 fprintf( outfile, "\tmovl $CALL32_LargeStack,%%eax\n" );
2114 fprintf( outfile, "\tret\n" );
2116 /* Function header */
2118 fprintf( outfile, "\n\t.align 4\n" );
2120 fprintf( outfile, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2122 fprintf( outfile, "CALL32_LargeStack:\n" );
2126 fprintf( outfile, "\tpushl %%ebp\n" );
2127 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2129 /* Switch to the original 32-bit stack pointer */
2131 fprintf( outfile, "\tmovl CALL32_Original32_esp, %%esp\n" );
2133 /* Transfer the argument and call the function */
2135 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2136 fprintf( outfile, "\tcall *8(%%ebp)\n" );
2138 /* Restore registers and return */
2140 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2141 fprintf( outfile, "\tpopl %%ebp\n" );
2142 fprintf( outfile, "\tret\n" );
2146 fprintf( outfile, "\t.data\n" );
2147 fprintf( outfile, "CALL32_Original32_esp:\t.long 0\n" );
2148 fprintf( outfile, "\t.text\n" );
2152 /*******************************************************************
2153 * BuildCallFrom32Regs
2155 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2156 * 'args' is the number of dword arguments.
2160 * (esp+336) ret addr (or relay addr when debugging(relay) is on)
2161 * (esp+332) entry point
2162 * (esp+204) buffer area to allow stack frame manipulation
2163 * (esp+0) CONTEXT struct
2165 static void BuildCallFrom32Regs( FILE *outfile )
2167 #define STACK_SPACE 128
2169 /* Function header */
2171 fprintf( outfile, "\n\t.align 4\n" );
2173 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
2175 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
2176 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
2178 /* Allocate some buffer space on the stack */
2180 fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
2182 /* Build the context structure */
2184 fprintf( outfile, "\tpushw $0\n" );
2185 fprintf( outfile, "\t.byte 0x66\n\tpushl %%ss\n" );
2186 fprintf( outfile, "\tpushl %%eax\n" ); /* %esp place holder */
2187 fprintf( outfile, "\tpushfl\n" );
2188 fprintf( outfile, "\tpushw $0\n" );
2189 fprintf( outfile, "\t.byte 0x66\n\tpushl %%cs\n" );
2190 fprintf( outfile, "\tpushl %d(%%esp)\n", 16+STACK_SPACE+4 ); /* %eip at time of call */
2191 fprintf( outfile, "\tpushl %%ebp\n" );
2193 fprintf( outfile, "\tpushl %%eax\n" );
2194 fprintf( outfile, "\tpushl %%ecx\n" );
2195 fprintf( outfile, "\tpushl %%edx\n" );
2196 fprintf( outfile, "\tpushl %%ebx\n" );
2197 fprintf( outfile, "\tpushl %%esi\n" );
2198 fprintf( outfile, "\tpushl %%edi\n" );
2200 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2201 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2202 fprintf( outfile, "\tpushl %%eax\n" );
2203 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2204 fprintf( outfile, "\tpushl %%eax\n" );
2205 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2206 fprintf( outfile, "\tpushl %%eax\n" );
2207 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2208 fprintf( outfile, "\tpushl %%eax\n" );
2210 fprintf( outfile, "\tleal -%d(%%esp),%%esp\n",
2211 sizeof(FLOATING_SAVE_AREA) + 6 * sizeof(DWORD) /* DR regs */ );
2212 fprintf( outfile, "\tpushl $0x0001001f\n" ); /* ContextFlags */
2214 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2216 fprintf( outfile, "\tleal %d(%%esp),%%eax\n",
2217 sizeof(CONTEXT) + STACK_SPACE + 4 ); /* %esp at time of call */
2218 fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Esp) );
2220 fprintf( outfile, "\tcall " PREFIX "RELAY_CallFrom32Regs\n" );
2222 /* Restore the context structure */
2224 fprintf( outfile, "\tfrstor %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2226 /* Store %eip value onto the new stack */
2228 fprintf( outfile, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Eip) );
2229 fprintf( outfile, "\tmovl %d(%%esp),%%ebx\n", CONTEXTOFFSET(Esp) );
2230 fprintf( outfile, "\tmovl %%eax,0(%%ebx)\n" );
2232 /* Restore all registers */
2234 fprintf( outfile, "\tleal %d(%%esp),%%esp\n",
2235 sizeof(FLOATING_SAVE_AREA) + 7 * sizeof(DWORD) );
2236 fprintf( outfile, "\tpopl %%eax\n" );
2237 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2238 fprintf( outfile, "\tpopl %%eax\n" );
2239 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2240 fprintf( outfile, "\tpopl %%eax\n" );
2241 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2242 fprintf( outfile, "\tpopl %%eax\n" );
2243 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2245 fprintf( outfile, "\tpopl %%edi\n" );
2246 fprintf( outfile, "\tpopl %%esi\n" );
2247 fprintf( outfile, "\tpopl %%ebx\n" );
2248 fprintf( outfile, "\tpopl %%edx\n" );
2249 fprintf( outfile, "\tpopl %%ecx\n" );
2250 fprintf( outfile, "\tpopl %%eax\n" );
2251 fprintf( outfile, "\tpopl %%ebp\n" );
2252 fprintf( outfile, "\tleal 8(%%esp),%%esp\n" ); /* skip %eip and %cs */
2253 fprintf( outfile, "\tpopfl\n" );
2254 fprintf( outfile, "\tpopl %%esp\n" );
2255 fprintf( outfile, "\tret\n" );
2261 /*******************************************************************
2264 * Build the spec files
2266 static int BuildSpec( FILE *outfile, int argc, char *argv[] )
2269 for (i = 2; i < argc; i++)
2270 if (BuildSpecFile( outfile, argv[i] ) < 0) return -1;
2275 /*******************************************************************
2278 * Build the 16-bit-to-Wine callbacks
2280 static int BuildCallFrom16( FILE *outfile, char * outname, int argc, char *argv[] )
2287 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2288 fprintf( outfile, "\t.text\n" );
2291 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2292 getcwd(buffer, sizeof(buffer));
2295 * The stabs help the internal debugger as they are an indication that it
2296 * is sensible to step into a thunk/trampoline.
2298 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2299 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2300 fprintf( outfile, "\t.text\n" );
2301 fprintf( outfile, "\t.align 4\n" );
2302 fprintf( outfile, "Code_Start:\n\n" );
2305 /* Build the callback functions */
2307 for (i = 2; i < argc; i++) BuildCallFrom16Func( outfile, argv[i] );
2309 /* Build the thunk callback function */
2311 BuildCallFrom16Func( outfile, "t_long_" );
2313 /* Output the argument debugging strings */
2317 fprintf( outfile, "/* Argument strings */\n" );
2318 for (i = 2; i < argc; i++)
2320 fprintf( outfile, "Profile_%s:\t", argv[i] );
2321 fprintf( outfile, STRING " \"%s\\0\"\n", argv[i] + 7 );
2326 fprintf( outfile, "\t.text\n");
2327 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2328 fprintf( outfile, ".Letext:\n");
2335 /*******************************************************************
2338 * Build the Wine-to-16-bit callbacks
2340 static int BuildCallTo16( FILE *outfile, char * outname, int argc, char *argv[] )
2347 infile = fopen( argv[2], "r" );
2354 else infile = stdin;
2358 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2359 fprintf( outfile, "\t.text\n" );
2362 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2363 getcwd(buffer, sizeof(buffer));
2366 * The stabs help the internal debugger as they are an indication that it
2367 * is sensible to step into a thunk/trampoline.
2369 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2370 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2371 fprintf( outfile, "\t.text\n" );
2372 fprintf( outfile, "\t.align 4\n" );
2373 fprintf( outfile, "Code_Start:\n\n" );
2376 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Start\n" );
2377 fprintf( outfile, PREFIX "CALLTO16_Start:\n" );
2379 /* Build the callback functions */
2381 while (fgets( buffer, sizeof(buffer), infile ))
2383 if (strstr( buffer, "### start build ###" )) break;
2385 while (fgets( buffer, sizeof(buffer), infile ))
2387 char *p = strstr( buffer, "CallTo16_" );
2390 char *profile = p + strlen( "CallTo16_" );
2392 while ((*p == '_') || isalpha(*p)) p++;
2394 BuildCallTo16Func( outfile, profile );
2396 if (strstr( buffer, "### stop build ###" )) break;
2399 /* Output the 16-bit return code */
2401 BuildRet16Func( outfile );
2403 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_End\n" );
2404 fprintf( outfile, PREFIX "CALLTO16_End:\n" );
2407 fprintf( outfile, "\t.text\n");
2408 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2409 fprintf( outfile, ".Letext:\n");
2417 /*******************************************************************
2420 * Build the 32-bit callbacks
2422 static int BuildCall32( FILE *outfile, char * outname )
2428 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2429 fprintf( outfile, "\t.text\n" );
2434 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2435 getcwd(buffer, sizeof(buffer));
2438 * The stabs help the internal debugger as they are an indication that it
2439 * is sensible to step into a thunk/trampoline.
2441 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2442 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2443 fprintf( outfile, "\t.text\n" );
2444 fprintf( outfile, "\t.align 4\n" );
2445 fprintf( outfile, "Code_Start:\n" );
2448 /* Build the 32-bit large stack callback */
2450 BuildCallTo32LargeStack( outfile );
2452 /* Build the register callback function */
2454 BuildCallFrom32Regs( outfile );
2457 fprintf( outfile, "\t.text\n");
2458 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2459 fprintf( outfile, ".Letext:\n");
2462 #else /* __i386__ */
2464 /* Just to avoid an empty file */
2465 fprintf( outfile, "\t.long 0\n" );
2467 #endif /* __i386__ */
2472 /*******************************************************************
2475 static void usage(void)
2478 "usage: build [-o outfile] -spec SPECNAMES\n"
2479 " build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2480 " build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2481 " build [-o outfile] -call32\n" );
2486 /*******************************************************************
2489 int main(int argc, char **argv)
2491 char *outname = NULL;
2492 FILE *outfile = stdout;
2495 if (argc < 2) usage();
2497 if (!strcmp( argv[1], "-o" ))
2502 if (argc < 2) usage();
2503 if (!(outfile = fopen( outname, "w" )))
2505 fprintf( stderr, "Unable to create output file '%s'\n", outname );
2510 /* Retrieve the selector values; this assumes that we are building
2511 * the asm files on the platform that will also run them. Probably
2512 * a safe assumption to make.
2514 GET_CS( Code_Selector );
2515 GET_DS( Data_Selector );
2517 if (!strcmp( argv[1], "-spec" ))
2518 res = BuildSpec( outfile, argc, argv );
2519 else if (!strcmp( argv[1], "-callfrom16" ))
2520 res = BuildCallFrom16( outfile, outname, argc, argv );
2521 else if (!strcmp( argv[1], "-callto16" ))
2522 res = BuildCallTo16( outfile, outname, argc, argv );
2523 else if (!strcmp( argv[1], "-call32" ))
2524 res = BuildCall32( outfile, outname );