2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Martin von Loewis
4 * Copyright 1995, 1996 Alexandre Julliard
5 * Copyright 1997 Eric Youngdale
18 #include "selectors.h"
20 #ifdef NEED_UNDERSCORE_PREFIX
26 #if defined(__GNUC__) && !defined(__svr4__)
35 TYPE_BYTE, /* byte variable */
36 TYPE_WORD, /* word variable */
37 TYPE_LONG, /* long variable */
38 TYPE_PASCAL_16, /* pascal function with 16-bit return (Win16) */
39 TYPE_PASCAL, /* pascal function with 32-bit return (Win16) */
40 TYPE_REGISTER, /* register function */
41 TYPE_ABS, /* absolute value */
42 TYPE_RETURN, /* simple return value function */
43 TYPE_STUB, /* unimplemented stub */
44 TYPE_STDCALL, /* stdcall function (Win32) */
45 TYPE_CDECL, /* cdecl function (Win32) */
46 TYPE_EXTERN, /* external symbol (Win32) */
50 static const char * const TypeNames[TYPE_NBTYPES] =
53 "byte", /* TYPE_BYTE */
54 "word", /* TYPE_WORD */
55 "long", /* TYPE_LONG */
56 "pascal16", /* TYPE_PASCAL_16 */
57 "pascal", /* TYPE_PASCAL */
58 "register", /* TYPE_REGISTER */
59 "equate", /* TYPE_ABS */
60 "return", /* TYPE_RETURN */
61 "stub", /* TYPE_STUB */
62 "stdcall", /* TYPE_STDCALL */
63 "cdecl", /* TYPE_CDECL */
64 "extern" /* TYPE_EXTERN */
67 #define MAX_ORDINALS 1299
69 /* Callback function used for stub functions */
70 #define STUB_CALLBACK \
71 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
125 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
127 static SPEC_TYPE SpecType = SPEC_INVALID;
134 char *ParseBuffer = NULL;
139 static int debugging = 1;
141 /* Offset of register relative to the start of the CONTEXT struct */
142 #define CONTEXTOFFSET(reg) ((int)&((CONTEXT *)0)->reg)
144 static void *xmalloc (size_t size)
148 res = malloc (size ? size : 1);
151 fprintf (stderr, "Virtual memory exhausted.\n");
158 static void *xrealloc (void *ptr, size_t size)
160 void *res = realloc (ptr, size);
163 fprintf (stderr, "Virtual memory exhausted.\n");
170 static int IsNumberString(char *s)
179 static char *strupper(char *s)
183 for(p = s; *p != '\0'; p++)
189 static char * GetTokenInLine(void)
194 if (ParseNext != ParseBuffer)
196 if (ParseSaveChar == '\0')
198 *ParseNext = ParseSaveChar;
202 * Remove initial white space.
204 for (p = ParseNext; isspace(*p); p++)
207 if ((*p == '\0') || (*p == '#'))
214 if (*token != '(' && *token != ')')
215 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
225 static char * GetToken(void)
229 if (ParseBuffer == NULL)
231 ParseBuffer = xmalloc(512);
232 ParseNext = ParseBuffer;
236 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
238 if (ParseBuffer[0] != '#')
243 while ((token = GetTokenInLine()) == NULL)
245 ParseNext = ParseBuffer;
249 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
251 if (ParseBuffer[0] != '#')
260 /*******************************************************************
263 * Parse a variable definition.
265 static int ParseVariable( ORDDEF *odp )
270 int value_array_size;
272 char *token = GetToken();
275 fprintf(stderr, "%d: Expected '(' got '%s'\n", Line, token);
280 value_array_size = 25;
281 value_array = xmalloc(sizeof(*value_array) * value_array_size);
283 while ((token = GetToken()) != NULL)
288 value_array[n_values++] = strtol(token, &endptr, 0);
289 if (n_values == value_array_size)
291 value_array_size += 25;
292 value_array = xrealloc(value_array,
293 sizeof(*value_array) * value_array_size);
296 if (endptr == NULL || *endptr != '\0')
298 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
306 fprintf(stderr, "%d: End of file in variable declaration\n", Line);
310 odp->u.var.n_values = n_values;
311 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
317 /*******************************************************************
318 * ParseExportFunction
320 * Parse a function definition.
322 static int ParseExportFunction( ORDDEF *odp )
330 if (odp->type == TYPE_STDCALL)
332 fprintf( stderr, "%d: 'stdcall' not supported for Win16\n", Line );
335 if (odp->type == TYPE_CDECL)
337 fprintf( stderr, "%d: 'cdecl' not supported for Win16\n", Line );
342 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
344 fprintf( stderr, "%d: 'pascal' not supported for Win32\n", Line );
355 fprintf(stderr, "%d: Expected '(' got '%s'\n", Line, token);
359 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
365 if (!strcmp(token, "byte") || !strcmp(token, "word"))
366 odp->u.func.arg_types[i] = 'w';
367 else if (!strcmp(token, "s_byte") || !strcmp(token, "s_word"))
368 odp->u.func.arg_types[i] = 's';
369 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
370 odp->u.func.arg_types[i] = 'l';
371 else if (!strcmp(token, "ptr"))
372 odp->u.func.arg_types[i] = 'p';
373 else if (!strcmp(token, "str"))
374 odp->u.func.arg_types[i] = 't';
375 else if (!strcmp(token, "segstr"))
376 odp->u.func.arg_types[i] = 'T';
379 fprintf(stderr, "%d: Unknown variable type '%s'\n", Line, token);
382 if (SpecType == SPEC_WIN32)
384 if (strcmp(token, "long") && strcmp(token, "ptr"))
386 fprintf( stderr, "%d: Type '%s' not supported for Win32\n",
394 fprintf( stderr, "%d: Too many arguments\n", Line );
397 odp->u.func.arg_types[i] = '\0';
398 if ((odp->type == TYPE_STDCALL) && !i)
399 odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
400 strcpy(odp->u.func.link_name, GetToken());
405 /*******************************************************************
408 * Parse an 'equate' definition.
410 static int ParseEquate( ORDDEF *odp )
414 char *token = GetToken();
415 int value = strtol(token, &endptr, 0);
416 if (endptr == NULL || *endptr != '\0')
418 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
423 odp->u.abs.value = value;
428 /*******************************************************************
431 * Parse a 'return' definition.
433 static int ParseReturn( ORDDEF *odp )
439 odp->u.ret.arg_size = strtol(token, &endptr, 0);
440 if (endptr == NULL || *endptr != '\0')
442 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
448 odp->u.ret.ret_value = strtol(token, &endptr, 0);
449 if (endptr == NULL || *endptr != '\0')
451 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
460 /*******************************************************************
463 * Parse a 'stub' definition.
465 static int ParseStub( ORDDEF *odp )
467 odp->u.func.arg_types[0] = '\0';
468 strcpy( odp->u.func.link_name, STUB_CALLBACK );
473 /*******************************************************************
476 * Parse an 'extern' definition.
478 static int ParseExtern( ORDDEF *odp )
480 if (SpecType == SPEC_WIN16)
482 fprintf( stderr, "%d: 'extern' not supported for Win16\n", Line );
485 strcpy( odp->u.ext.link_name, GetToken() );
490 /*******************************************************************
493 * Parse an ordinal definition.
495 static int ParseOrdinal(int ordinal)
500 if (ordinal >= MAX_ORDINALS)
502 fprintf(stderr, "%d: Ordinal number too large\n", Line);
505 if (ordinal > Limit) Limit = ordinal;
507 odp = &OrdinalDefinitions[ordinal];
508 if (!(token = GetToken()))
510 fprintf(stderr, "%d: Expected type after ordinal\n", Line);
514 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
515 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
518 if (odp->type >= TYPE_NBTYPES)
521 "%d: Expected type after ordinal, found '%s' instead\n",
526 if (!(token = GetToken()))
528 fprintf( stderr, "%d: Expected name after type\n", Line );
531 strcpy( odp->name, token );
539 return ParseVariable( odp );
545 return ParseExportFunction( odp );
547 return ParseEquate( odp );
549 return ParseReturn( odp );
551 return ParseStub( odp );
553 return ParseExtern( odp );
555 fprintf( stderr, "Should not happen\n" );
561 /*******************************************************************
566 static int ParseTopLevel(void)
570 while ((token = GetToken()) != NULL)
572 if (strcmp(token, "name") == 0)
574 strcpy(DLLName, GetToken());
577 else if (strcmp(token, "type") == 0)
580 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
581 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
584 fprintf(stderr, "%d: Type must be 'win16' or 'win32'\n", Line);
588 else if (strcmp(token, "base") == 0)
591 if (!IsNumberString(token))
593 fprintf(stderr, "%d: Expected number after base\n", Line);
598 else if (strcmp(token, "heap") == 0)
601 if (!IsNumberString(token))
603 fprintf(stderr, "%d: Expected number after heap\n", Line);
606 DLLHeapSize = atoi(token);
608 else if (IsNumberString(token))
613 ordinal = atoi(token);
614 if ((rv = ParseOrdinal(ordinal)) < 0)
620 "%d: Expected name, id, length or ordinal\n", Line);
629 /*******************************************************************
632 * Store a list of ints into a byte array.
634 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
641 for (i = 0; i < odp->u.var.n_values; i++)
642 buffer[i] = odp->u.var.values[i];
645 for (i = 0; i < odp->u.var.n_values; i++)
646 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
649 for (i = 0; i < odp->u.var.n_values; i++)
650 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
653 return odp->u.var.n_values * size;
657 /*******************************************************************
660 * Dump a byte stream into the assembly code.
662 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
663 const char *section, const char *label_start )
666 if (section) fprintf( outfile, "\t%s\n", section );
667 if (label_start) fprintf( outfile, "%s:\n", label_start );
668 for (i = 0; i < len; i++)
670 if (!(i & 0x0f)) fprintf( outfile, "\t.byte " );
671 fprintf( outfile, "%d", *data++ );
673 fprintf( outfile, "%c", ((i & 0x0f) != 0x0f) ? ',' : '\n' );
675 fprintf( outfile, "\n" );
679 /*******************************************************************
682 * Build the in-memory representation of a 16-bit NE module, and dump it
683 * as a byte stream into the assembly code.
685 static int BuildModule16( FILE *outfile, int max_code_offset,
686 int max_data_offset )
692 SEGTABLEENTRY *pSegment;
699 * OFSTRUCT File information
700 * SEGTABLEENTRY Segment 1 (code)
701 * SEGTABLEENTRY Segment 2 (data)
702 * WORD[2] Resource table (empty)
703 * BYTE[2] Imported names (empty)
704 * BYTE[n] Resident names table
705 * BYTE[n] Entry table
708 buffer = xmalloc( 0x10000 );
710 pModule = (NE_MODULE *)buffer;
711 pModule->magic = IMAGE_OS2_SIGNATURE;
714 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
716 pModule->heap_size = DLLHeapSize;
717 pModule->stack_size = 0;
722 pModule->seg_count = 2;
723 pModule->modref_count = 0;
724 pModule->nrname_size = 0;
725 pModule->modref_table = 0;
726 pModule->nrname_fpos = 0;
727 pModule->moveable_entries = 0;
728 pModule->alignment = 0;
729 pModule->truetype = 0;
730 pModule->os_flags = NE_OSFLAGS_WINDOWS;
731 pModule->misc_flags = 0;
732 pModule->dlls_to_init = 0;
733 pModule->nrname_handle = 0;
734 pModule->min_swap_area = 0;
735 pModule->expected_version = 0x030a;
736 pModule->pe_module = NULL;
738 pModule->self_loading_sel = 0;
740 /* File information */
742 pFileInfo = (OFSTRUCT *)(pModule + 1);
743 pModule->fileinfo = (int)pFileInfo - (int)pModule;
744 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
745 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
746 + strlen(DLLName) + 4;
747 sprintf( pFileInfo->szPathName, "%s.DLL", DLLName );
748 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
752 pSegment = (SEGTABLEENTRY *)pstr;
753 pModule->seg_table = (int)pSegment - (int)pModule;
754 pSegment->filepos = 0;
755 pSegment->size = max_code_offset;
757 pSegment->minsize = max_code_offset;
758 pSegment->selector = 0;
761 pModule->dgroup_entry = (int)pSegment - (int)pModule;
762 pSegment->filepos = 0;
763 pSegment->size = max_data_offset;
764 pSegment->flags = NE_SEGFLAGS_DATA;
765 pSegment->minsize = max_data_offset;
766 pSegment->selector = 0;
771 pword = (WORD *)pSegment;
772 pModule->res_table = (int)pword - (int)pModule;
776 /* Imported names table */
778 pstr = (char *)pword;
779 pModule->import_table = (int)pstr - (int)pModule;
783 /* Resident names table */
785 pModule->name_table = (int)pstr - (int)pModule;
786 /* First entry is module name */
787 *pstr = strlen(DLLName );
788 strcpy( pstr + 1, DLLName );
791 pstr += sizeof(WORD);
792 /* Store all ordinals */
793 odp = OrdinalDefinitions + 1;
794 for (i = 1; i <= Limit; i++, odp++)
796 if (!odp->name[0]) continue;
797 *pstr = strlen( odp->name );
798 strcpy( pstr + 1, odp->name );
799 strupper( pstr + 1 );
802 pstr += sizeof(WORD);
808 pModule->entry_table = (int)pstr - (int)pModule;
810 odp = OrdinalDefinitions + 1;
811 for (i = 1; i <= Limit; i++, odp++)
822 selector = 1; /* Code selector */
828 selector = 2; /* Data selector */
832 selector = 0xfe; /* Constant selector */
836 selector = 0; /* Invalid selector */
840 /* create a new bundle if necessary */
841 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
845 bundle[1] = selector;
853 *(WORD *)pstr = odp->offset;
854 pstr += sizeof(WORD);
859 /* Dump the module content */
861 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
862 ".data", "Module_Start" );
863 return (int)pstr - (int)pModule;
867 /*******************************************************************
870 * Build the in-memory representation of a 32-bit pseudo-NE module, and dump it
871 * as a byte stream into the assembly code.
873 static int BuildModule32( FILE *outfile )
883 * OFSTRUCT File information
884 * SEGTABLEENTRY Segment table (empty)
885 * WORD[2] Resource table (empty)
886 * BYTE[2] Imported names (empty)
887 * BYTE[n] Resident names table (1 entry)
888 * BYTE[n] Entry table (empty)
891 buffer = xmalloc( 0x10000 );
893 pModule = (NE_MODULE *)buffer;
894 pModule->magic = IMAGE_OS2_SIGNATURE;
897 pModule->dgroup_entry = 0;
898 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN |
899 NE_FFLAGS_LIBMODULE | NE_FFLAGS_WIN32;
901 pModule->heap_size = DLLHeapSize;
902 pModule->stack_size = 0;
907 pModule->seg_count = 0;
908 pModule->modref_count = 0;
909 pModule->nrname_size = 0;
910 pModule->modref_table = 0;
911 pModule->nrname_fpos = 0;
912 pModule->moveable_entries = 0;
913 pModule->alignment = 0;
914 pModule->truetype = 0;
915 pModule->os_flags = NE_OSFLAGS_WINDOWS;
916 pModule->misc_flags = 0;
917 pModule->dlls_to_init = 0;
918 pModule->nrname_handle = 0;
919 pModule->min_swap_area = 0;
920 pModule->expected_version = 0x030a;
921 pModule->pe_module = NULL;
923 pModule->self_loading_sel = 0;
925 /* File information */
927 pFileInfo = (OFSTRUCT *)(pModule + 1);
928 pModule->fileinfo = (int)pFileInfo - (int)pModule;
929 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
930 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
931 + strlen(DLLName) + 4;
932 sprintf( pFileInfo->szPathName, "%s.DLL", DLLName );
933 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
937 pModule->seg_table = (int)pstr - (int)pModule;
941 pword = (WORD *)pstr;
942 pModule->res_table = (int)pword - (int)pModule;
946 /* Imported names table */
948 pstr = (char *)pword;
949 pModule->import_table = (int)pstr - (int)pModule;
953 /* Resident names table */
955 pModule->name_table = (int)pstr - (int)pModule;
956 /* First entry is module name */
957 *pstr = strlen(DLLName );
958 strcpy( pstr + 1, DLLName );
961 pstr += sizeof(WORD);
966 pModule->entry_table = (int)pstr - (int)pModule;
969 /* Dump the module content */
971 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
972 ".data", "Module_Start" );
973 return (int)pstr - (int)pModule;
977 /*******************************************************************
980 * Build a Win32 assembly file from a spec file.
982 static int BuildSpec32File( char * specfile, FILE *outfile )
985 int i, module_size, len;
988 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
989 fprintf( outfile, "\t.file\t\"%s\"\n", specfile );
991 getcwd(buffer, sizeof(buffer));
994 * The stabs help the internal debugger as they are an indication that it
995 * is sensible to step into a thunk/trampoline.
997 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
998 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", specfile);
1001 fprintf( outfile, "\t.text\n" );
1002 fprintf( outfile, "\t.align 4\n" );
1003 fprintf( outfile, "Code_Start:\n\n" );
1005 odp = OrdinalDefinitions;
1006 for (i = 0; i <= Limit; i++, odp++)
1017 fprintf( outfile, "/* %s.%d (%s) */\n", DLLName, i, odp->name);
1019 fprintf( outfile, ".stabs \"%s_%d:F1\",36,0,%d,%s_%d\n",
1020 DLLName, i, odp->lineno, DLLName, i);
1022 fprintf( outfile, "%s_%d:\n", DLLName, i );
1024 fprintf( outfile, ".stabn 68,0,%d,0\n", odp->lineno);
1026 fprintf( outfile, "\tpushl %%ebp\n" );
1027 fprintf( outfile, "\tpushl $" PREFIX "%s\n",odp->u.func.link_name);
1028 fprintf( outfile, "\tcall " PREFIX "CallFrom32_%s_%d\n",
1029 (odp->type == TYPE_REGISTER) ? "regs" :
1030 ((odp->type == TYPE_STDCALL) ? "stdcall" : "cdecl"),
1031 strlen(odp->u.func.arg_types));
1032 fprintf( outfile, "\tnop\n" );
1036 fprintf( outfile, "/* %s.%d (%s) */\n", DLLName, i, odp->name);
1038 fprintf( outfile, ".stabs \"%s_%d:F1\",36,0,%d,%s_%d\n",
1039 DLLName, i, odp->lineno, DLLName, i);
1041 fprintf( outfile, "%s_%d:\n", DLLName, i );
1043 fprintf( outfile, ".stabn 68,0,%d,0\n", odp->lineno);
1045 fprintf( outfile, "\tmovl $%d,%%eax\n", odp->u.ret.ret_value );
1046 if (odp->u.ret.arg_size)
1048 fprintf( outfile, "\tret $%d\n", odp->u.ret.arg_size );
1052 fprintf( outfile, "\tret\n" );
1053 fprintf( outfile, "\tnop\n" );
1054 fprintf( outfile, "\tnop\n" );
1059 fprintf( outfile, "/* %s.%d (%s) */\n", DLLName, i, odp->name);
1060 fprintf( outfile, "\t.data\n" );
1061 fprintf( outfile, "%s_%d:\n", DLLName, i );
1062 len = StoreVariableCode( buffer, 1, odp );
1063 DumpBytes( outfile, buffer, len, NULL, NULL );
1064 fprintf( outfile, "\t.text\n" );
1068 fprintf( outfile, "/* %s.%d (%s) */\n",
1069 DLLName, i, odp->name);
1070 fprintf( outfile, "\t.data\n" );
1071 fprintf( outfile, "%s_%d:\n", DLLName, i );
1072 len = StoreVariableCode( buffer, 2, odp );
1073 DumpBytes( outfile, buffer, len, NULL, NULL );
1074 fprintf( outfile, "\t.text\n" );
1078 fprintf( outfile, "/* %s.%d (%s) */\n",
1079 DLLName, i, odp->name);
1080 fprintf( outfile, "\t.data\n" );
1081 fprintf( outfile, "%s_%d:\n", DLLName, i );
1082 len = StoreVariableCode( buffer, 4, odp );
1083 DumpBytes( outfile, buffer, len, NULL, NULL );
1084 fprintf( outfile, "\t.text\n" );
1091 fprintf(stderr,"build: function type %d not available for Win32\n",
1097 module_size = BuildModule32( outfile );
1099 /* Output the DLL functions table */
1101 fprintf( outfile, "\t.text\n" );
1102 fprintf( outfile, "\t.align 4\n" );
1103 fprintf( outfile, "Functions:\n" );
1104 odp = OrdinalDefinitions;
1105 for (i = 0; i <= Limit; i++, odp++)
1110 fprintf( outfile, "\t.long 0\n" );
1113 fprintf( outfile, "\t.long " PREFIX "%s\n", odp->u.ext.link_name );
1116 fprintf( outfile, "\t.long %s_%d\n", DLLName, i );
1121 /* Output the DLL names table */
1123 fprintf( outfile, "FuncNames:\n" );
1124 odp = OrdinalDefinitions;
1125 for (i = 0; i <= Limit; i++, odp++)
1127 if (odp->type == TYPE_INVALID) fprintf( outfile, "\t.long 0\n" );
1128 else fprintf( outfile, "\t.long Name_%d\n", i );
1131 /* Output the DLL names */
1133 for (i = 0, odp = OrdinalDefinitions; i <= Limit; i++, odp++)
1135 if (odp->type != TYPE_INVALID)
1136 fprintf( outfile, "Name_%d:\t.ascii \"%s\\0\"\n", i, odp->name );
1139 /* Output the DLL descriptor */
1141 fprintf( outfile, "DLLName:\t.ascii \"%s\\0\"\n", DLLName );
1142 fprintf( outfile, "\t.align 4\n" );
1143 fprintf( outfile, "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1144 fprintf( outfile, PREFIX "%s_Descriptor:\n", DLLName );
1145 fprintf( outfile, "\t.long DLLName\n" ); /* Name */
1146 fprintf( outfile, "\t.long Module_Start\n" ); /* Module start */
1147 fprintf( outfile, "\t.long %d\n", module_size ); /* Module size */
1148 fprintf( outfile, "\t.long %d\n", Base ); /* Base */
1149 fprintf( outfile, "\t.long %d\n", Limit+1 ); /* Size */
1150 fprintf( outfile, "\t.long Code_Start\n" ); /* Code start */
1151 fprintf( outfile, "\t.long Functions\n" ); /* Functions */
1152 fprintf( outfile, "\t.long FuncNames\n" ); /* Function names */
1154 fprintf( outfile, "\t.text\n");
1155 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
1156 fprintf( outfile, ".Letext:\n");
1162 /*******************************************************************
1165 * Build a Win16 assembly file from a spec file.
1167 static int BuildSpec16File( char * specfile, FILE *outfile )
1171 int code_offset, data_offset, module_size;
1172 unsigned char *data;
1174 data = (unsigned char *)xmalloc( 0x10000 );
1175 memset( data, 0, 16 );
1178 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
1179 fprintf( outfile, "\t.text\n" );
1180 fprintf( outfile, "Code_Start:\n" );
1183 odp = OrdinalDefinitions;
1184 for (i = 0; i <= Limit; i++, odp++)
1189 odp->offset = 0xffff;
1193 odp->offset = LOWORD(odp->u.abs.value);
1197 odp->offset = data_offset;
1198 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1202 odp->offset = data_offset;
1203 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1207 odp->offset = data_offset;
1208 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1212 fprintf( outfile,"/* %s.%d */\n", DLLName, i);
1213 fprintf( outfile,"\tmovw $%d,%%ax\n",LOWORD(odp->u.ret.ret_value));
1214 fprintf( outfile,"\tmovw $%d,%%dx\n",HIWORD(odp->u.ret.ret_value));
1215 fprintf( outfile,"\t.byte 0x66\n");
1216 if (odp->u.ret.arg_size != 0)
1217 fprintf( outfile, "\tlret $%d\n\n", odp->u.ret.arg_size);
1220 fprintf( outfile, "\tlret\n");
1221 fprintf( outfile, "\tnop\n");
1222 fprintf( outfile, "\tnop\n\n");
1224 odp->offset = code_offset;
1225 code_offset += 12; /* Assembly code is 12 bytes long */
1230 case TYPE_PASCAL_16:
1232 fprintf( outfile, "/* %s.%d */\n", DLLName, i);
1233 fprintf( outfile, "\tpushw %%bp\n" );
1234 fprintf( outfile, "\tpushl $" PREFIX "%s\n",odp->u.func.link_name);
1235 /* FreeBSD does not understand lcall, so do it the hard way */
1236 fprintf( outfile, "\t.byte 0x9a\n" );
1237 fprintf( outfile, "\t.long " PREFIX "CallFrom16_%s_%s\n",
1238 (odp->type == TYPE_REGISTER) ? "regs" :
1239 (odp->type == TYPE_PASCAL) ? "long" : "word",
1240 odp->u.func.arg_types );
1241 fprintf( outfile,"\t.byte 0x%02x,0x%02x\n", /* Some asms don't have .word */
1242 LOBYTE(WINE_CODE_SELECTOR), HIBYTE(WINE_CODE_SELECTOR) );
1243 fprintf( outfile, "\tnop\n" );
1244 fprintf( outfile, "\tnop\n\n" );
1245 odp->offset = code_offset;
1246 code_offset += 16; /* Assembly code is 16 bytes long */
1250 fprintf(stderr,"build: function type %d not available for Win16\n",
1256 if (!code_offset) /* Make sure the code segment is not empty */
1258 fprintf( outfile, "\t.byte 0\n" );
1262 /* Output data segment */
1264 DumpBytes( outfile, data, data_offset, NULL, "Data_Start" );
1266 /* Build the module */
1268 module_size = BuildModule16( outfile, code_offset, data_offset );
1270 /* Output the DLL descriptor */
1272 fprintf( outfile, "\t.text\n" );
1273 fprintf( outfile, "DLLName:\t.ascii \"%s\\0\"\n", DLLName );
1274 fprintf( outfile, "\t.align 4\n" );
1275 fprintf( outfile, "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1276 fprintf( outfile, PREFIX "%s_Descriptor:\n", DLLName );
1277 fprintf( outfile, "\t.long DLLName\n" ); /* Name */
1278 fprintf( outfile, "\t.long Module_Start\n" ); /* Module start */
1279 fprintf( outfile, "\t.long %d\n", module_size ); /* Module size */
1280 fprintf( outfile, "\t.long Code_Start\n" ); /* Code start */
1281 fprintf( outfile, "\t.long Data_Start\n" ); /* Data start */
1286 /*******************************************************************
1289 * Build an assembly file from a spec file.
1291 static int BuildSpecFile( FILE *outfile, char *specname )
1293 SpecFp = fopen( specname, "r");
1296 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1300 if (ParseTopLevel() < 0) return -1;
1305 return BuildSpec16File( specname, outfile );
1307 return BuildSpec32File( specname, outfile );
1309 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1315 /*******************************************************************
1316 * BuildCall32LargeStack
1318 * Build the function used to switch to the original 32-bit stack
1319 * before calling a 32-bit function from 32-bit code. This is used for
1320 * functions that need a large stack, like X bitmaps functions.
1322 * The generated function has the following prototype:
1323 * int CallTo32_LargeStack( int (*func)(), int nbargs, ... )
1334 static void BuildCall32LargeStack( FILE *outfile )
1336 /* Function header */
1338 fprintf( outfile, "\n\t.align 4\n" );
1340 fprintf( outfile, ".stabs \"CallTo32_LargeStack:F1\",36,0,0," PREFIX "CallTo32_LargeStack\n");
1342 fprintf( outfile, "\t.globl " PREFIX "CallTo32_LargeStack\n" );
1343 fprintf( outfile, PREFIX "CallTo32_LargeStack:\n" );
1347 fprintf( outfile, "\tpushl %%ebp\n" );
1348 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
1350 /* Save registers */
1352 fprintf( outfile, "\tpushl %%ecx\n" );
1353 fprintf( outfile, "\tpushl %%esi\n" );
1354 fprintf( outfile, "\tpushl %%edi\n" );
1356 /* Retrieve the original 32-bit stack pointer and switch to it if any */
1358 fprintf( outfile, "\tmovl " PREFIX "IF1632_Original32_esp, %%eax\n" );
1359 fprintf( outfile, "\torl %%eax,%%eax\n" );
1360 fprintf( outfile, "\tje no_orig_esp\n" );
1361 fprintf( outfile, "\tmovl %%eax,%%esp\n" );
1362 fprintf( outfile, "no_orig_esp:\n" );
1364 /* Transfer the arguments */
1366 fprintf( outfile, "\tmovl 12(%%ebp),%%ecx\n" );
1367 fprintf( outfile, "\torl %%ecx,%%ecx\n" );
1368 fprintf( outfile, "\tje no_args\n" );
1369 fprintf( outfile, "\tleal 16(%%ebp),%%esi\n" );
1370 fprintf( outfile, "\tshll $2,%%ecx\n" );
1371 fprintf( outfile, "\tsubl %%ecx,%%esp\n" );
1372 fprintf( outfile, "\tmovl %%esp,%%edi\n" );
1373 fprintf( outfile, "\tshrl $2,%%ecx\n" );
1374 fprintf( outfile, "\tcld\n" );
1375 fprintf( outfile, "\trep; movsl\n" );
1376 fprintf( outfile, "no_args:\n" );
1378 /* Call the function */
1380 fprintf( outfile, "\tcall 8(%%ebp)\n" );
1382 /* Switch back to the normal stack */
1384 fprintf( outfile, "\tleal -12(%%ebp),%%esp\n" );
1386 /* Restore registers and return */
1388 fprintf( outfile, "\tpopl %%edi\n" );
1389 fprintf( outfile, "\tpopl %%esi\n" );
1390 fprintf( outfile, "\tpopl %%ecx\n" );
1391 fprintf( outfile, "\tpopl %%ebp\n" );
1392 fprintf( outfile, "\tret\n" );
1396 /*******************************************************************
1397 * TransferArgs16To32
1399 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1400 * The 16-bit stack layout is:
1408 static int TransferArgs16To32( FILE *outfile, char *args )
1410 int i, pos16, pos32;
1412 /* Save ebx first */
1414 fprintf( outfile, "\tpushl %%ebx\n" );
1416 /* Get the 32-bit stack pointer */
1418 fprintf( outfile, "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1420 /* Copy the arguments */
1422 pos16 = 6; /* skip bp and return address */
1425 for (i = strlen(args); i > 0; i--)
1430 case 'w': /* word */
1431 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1432 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1436 case 's': /* s_word */
1437 fprintf( outfile, "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1438 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1442 case 'l': /* long or segmented pointer */
1443 case 'T': /* segmented pointer to null-terminated string */
1444 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", pos16 );
1445 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1449 case 'p': /* linear pointer */
1450 case 't': /* linear pointer to null-terminated string */
1451 /* Get the selector */
1452 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1453 /* Get the selector base */
1454 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1455 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1456 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1457 /* Add the offset */
1458 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1459 fprintf( outfile, "\taddl %%eax,%d(%%ebx)\n", pos32 );
1464 fprintf( stderr, "Unknown arg type '%c'\n", args[i-1] );
1470 fprintf( outfile, "\tpopl %%ebx\n" );
1472 return pos16 - 6; /* Return the size of the 16-bit args */
1476 /*******************************************************************
1479 * Build the context structure on the 32-bit stack.
1481 static void BuildContext16( FILE *outfile )
1483 /* Save ebx first */
1485 fprintf( outfile, "\tpushl %%ebx\n" );
1487 /* Get the 32-bit stack pointer */
1489 fprintf( outfile, "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1491 /* Store the registers */
1493 fprintf( outfile, "\tpopl %d(%%ebx)\n", /* Get ebx from stack*/
1494 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1495 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1496 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1497 fprintf( outfile, "\tmovl %%ecx,%d(%%ebx)\n",
1498 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1499 fprintf( outfile, "\tmovl %%edx,%d(%%ebx)\n",
1500 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1501 fprintf( outfile, "\tmovl %%esi,%d(%%ebx)\n",
1502 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1503 fprintf( outfile, "\tmovl %%edi,%d(%%ebx)\n",
1504 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1506 fprintf( outfile, "\tmovzwl -10(%%ebp),%%eax\n" ); /* Get %ds from stack*/
1507 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1508 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1509 fprintf( outfile, "\tmovzwl -6(%%ebp),%%eax\n" ); /* Get %es from stack*/
1510 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1511 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1512 fprintf( outfile, "\tpushfl\n" );
1513 fprintf( outfile, "\tpopl %d(%%ebx)\n",
1514 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1515 fprintf( outfile, "\tmovl -16(%%ebp),%%eax\n" ); /* Get %ebp from stack */
1516 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1517 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1518 fprintf( outfile, "\tmovzwl 2(%%ebp),%%eax\n" ); /* Get %ip from stack */
1519 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1520 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1521 fprintf( outfile, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1522 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1523 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1524 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
1525 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1526 CONTEXTOFFSET(SegFs) - sizeof(CONTEXT) );
1527 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1528 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1529 CONTEXTOFFSET(SegGs) - sizeof(CONTEXT) );
1530 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1531 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1532 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1534 fprintf( outfile, "\tfsave %d(%%ebx)\n",
1535 CONTEXTOFFSET(FloatSave) - sizeof(CONTEXT) );
1540 /*******************************************************************
1543 * Restore the registers from the context structure
1545 static void RestoreContext16( FILE *outfile )
1547 /* Get the 32-bit stack pointer */
1549 fprintf( outfile, "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1551 /* Remove everything up to the return address from the 16-bit stack */
1553 fprintf( outfile, "\taddl $18,%%esp\n" );
1555 /* Restore the registers */
1557 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n",
1558 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1559 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n",
1560 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1561 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n",
1562 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1563 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n",
1564 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1565 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n",
1566 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1567 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new ds */
1568 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1569 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new es */
1570 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1571 fprintf( outfile, "\tpushl %d(%%ebx)\n",
1572 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1573 fprintf( outfile, "\tpopfl\n" );
1574 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1575 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1576 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n",
1577 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1578 fprintf( outfile, "\tpopw %%es\n" ); /* Set es */
1579 fprintf( outfile, "\tpopw %%ds\n" ); /* Set ds */
1583 /*******************************************************************
1584 * BuildCallFrom16Func
1586 * Build a 16-bit-to-Wine callback function. The syntax of the function
1587 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1588 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1589 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1590 * 'T'=segmented pointer to null-terminated string).
1591 * For register functions, the arguments are ignored, but they are still
1592 * removed from the stack upon return.
1594 * Stack layout upon entry to the callback function:
1596 * (sp+18) word first 16-bit arg
1600 * (sp+8) long 32-bit entry point (used to store edx)
1601 * (sp+6) word high word of cs (always 0, used to store es)
1602 * (sp+4) word low word of cs of 16-bit entry point
1603 * (sp+2) word high word of ip (always 0, used to store ds)
1604 * (sp) word low word of ip of 16-bit entry point
1606 * Added on the stack:
1608 * (sp-6) word saved previous sp
1609 * (sp-8) word saved previous ss
1611 static void BuildCallFrom16Func( FILE *outfile, char *profile )
1616 char *args = profile + 5;
1618 /* Parse function type */
1620 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1621 else if (!strncmp( "regs_", profile, 5 )) reg_func = 1;
1622 else if (strncmp( "long_", profile, 5 ))
1624 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1628 /* Function header */
1630 fprintf( outfile, "\n\t.align 4\n" );
1632 fprintf( outfile, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX "CallFrom16_%s\n",
1635 fprintf( outfile, "\t.globl " PREFIX "CallFrom16_%s\n", profile );
1636 fprintf( outfile, PREFIX "CallFrom16_%s:\n", profile );
1638 /* Setup bp to point to its copy on the stack */
1640 fprintf( outfile, "\tpushl %%ebp\n" ); /* Save the full 32-bit ebp */
1641 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1642 fprintf( outfile, "\taddw $16,%%bp\n" );
1644 /* Save 16-bit ds and es */
1646 /* Stupid FreeBSD assembler doesn't know these either */
1647 /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1648 fprintf( outfile, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1649 /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1650 fprintf( outfile, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1652 /* Restore 32-bit ds and es */
1654 fprintf( outfile, "\tpushl $0x%04x%04x\n",
1655 WINE_DATA_SELECTOR, WINE_DATA_SELECTOR );
1656 fprintf( outfile, "\tpopw %%ds\n" );
1657 fprintf( outfile, "\tpopw %%es\n" );
1660 /* Save the 16-bit stack */
1662 fprintf( outfile, "\tpushl " PREFIX "IF1632_Saved16_ss_sp\n" );
1664 fprintf( outfile,"\tdata16\n");
1666 fprintf( outfile, "\tmovw %%ss," PREFIX "IF1632_Saved16_ss_sp+2\n" );
1667 fprintf( outfile, "\tmovw %%sp," PREFIX "IF1632_Saved16_ss_sp\n" );
1669 /* Transfer the arguments */
1671 if (reg_func) BuildContext16( outfile );
1672 else if (*args) argsize = TransferArgs16To32( outfile, args );
1674 /* Get the address of the API function */
1676 fprintf( outfile, "\tmovl -4(%%ebp),%%eax\n" );
1678 /* If necessary, save %edx over the API function address */
1680 if (!reg_func && short_ret)
1681 fprintf( outfile, "\tmovl %%edx,-4(%%ebp)\n" );
1683 /* Switch to the 32-bit stack */
1685 fprintf( outfile, "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebp\n" );
1686 fprintf( outfile, "\tpushw %%ds\n" );
1687 fprintf( outfile, "\tpopw %%ss\n" );
1688 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1689 reg_func ? sizeof(CONTEXT) : 4 * strlen(args) );
1690 if (reg_func) /* Push the address of the context struct */
1691 fprintf( outfile, "\tpushl %%esp\n" );
1693 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1695 fprintf( outfile, "\taddl $24,%%ebp\n" );
1697 /* Print the debug information before the call */
1701 fprintf( outfile, "\tpushl %%eax\n" );
1702 fprintf( outfile, "\tpushl $Profile_%s\n", profile );
1703 fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1704 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n" );
1705 fprintf( outfile, "\tpopl %%eax\n" );
1706 fprintf( outfile, "\tpopl %%eax\n" );
1707 fprintf( outfile, "\tpopl %%eax\n" );
1710 /* Call the entry point */
1712 fprintf( outfile, "\tcall %%eax\n" );
1714 /* Print the debug information after the call */
1718 fprintf( outfile, "\tpushl %%eax\n" );
1719 fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1720 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n" );
1721 fprintf( outfile, "\tpopl %%eax\n" );
1722 fprintf( outfile, "\tpopl %%eax\n" );
1725 /* Restore the 16-bit stack */
1728 fprintf( outfile, "\tdata16\n");
1730 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp+2,%%ss\n" );
1731 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp,%%sp\n" );
1732 fprintf( outfile, "\tpopl " PREFIX "IF1632_Saved16_ss_sp\n" );
1736 /* Calc the arguments size */
1752 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1757 /* Restore registers from the context structure */
1758 RestoreContext16( outfile );
1762 /* Restore high 16 bits of ebp */
1763 fprintf( outfile, "\tpopl %%ebp\n" );
1765 /* Restore ds and es */
1766 fprintf( outfile, "\tincl %%esp\n" ); /* Remove ip */
1767 fprintf( outfile, "\tincl %%esp\n" );
1768 fprintf( outfile, "\tpopl %%edx\n" ); /* Remove cs and ds */
1769 fprintf( outfile, "\tmovw %%dx,%%ds\n" ); /* and restore ds */
1770 fprintf( outfile, "\tpopw %%es\n" ); /* Restore es */
1772 if (short_ret) fprintf( outfile, "\tpopl %%edx\n" ); /* Restore edx */
1775 /* Get the return value into dx:ax */
1776 fprintf( outfile, "\tmovl %%eax,%%edx\n" );
1777 fprintf( outfile, "\tshrl $16,%%edx\n" );
1778 /* Remove API entry point */
1779 fprintf( outfile, "\taddl $4,%%esp\n" );
1782 /* Restore low 16 bits of ebp */
1783 fprintf( outfile, "\tpopw %%bp\n" );
1786 /* Remove the arguments and return */
1790 fprintf( outfile, "\t.byte 0x66\n" );
1791 fprintf( outfile, "\tlret $%d\n", argsize );
1795 fprintf( outfile, "\t.byte 0x66\n" );
1796 fprintf( outfile, "\tlret\n" );
1801 /*******************************************************************
1804 * Build a Wine-to-16-bit callback function.
1806 * Stack frame of the callback function:
1810 * (ebp+12) func to call
1811 * (ebp+8) code selector
1812 * (ebp+4) return address
1813 * (ebp) previous ebp
1815 * Prototypes for the CallTo16 functions:
1816 * extern WORD CallTo16_word_xxx( FARPROC16 func, args... );
1817 * extern LONG CallTo16_long_xxx( FARPROC16 func, args... );
1818 * extern void CallTo16_regs_( const CONTEXT *context );
1820 static void BuildCallTo16Func( FILE *outfile, char *profile )
1824 char *args = profile + 5;
1826 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1827 else if (!strncmp( "regs_", profile, 5 )) reg_func = short_ret = 1;
1828 else if (strncmp( "long_", profile, 5 ))
1830 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1834 /* Function header */
1836 fprintf( outfile, "\n\t.align 4\n" );
1838 fprintf( outfile, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX "CallTo16_%s\n",
1841 fprintf( outfile, "\t.globl " PREFIX "CallTo16_%s\n", profile );
1842 fprintf( outfile, PREFIX "CallTo16_%s:\n", profile );
1844 /* Push code selector before return address to simulate a lcall */
1846 fprintf( outfile, "\tpopl %%eax\n" );
1847 fprintf( outfile, "\tpushl $0x%04x\n", WINE_CODE_SELECTOR );
1848 fprintf( outfile, "\tpushl %%eax\n" );
1852 fprintf( outfile, "\tpushl %%ebp\n" );
1853 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
1855 /* Save the 32-bit registers */
1857 fprintf( outfile, "\tpushl %%ebx\n" );
1858 fprintf( outfile, "\tpushl %%ecx\n" );
1859 fprintf( outfile, "\tpushl %%edx\n" );
1860 fprintf( outfile, "\tpushl %%esi\n" );
1861 fprintf( outfile, "\tpushl %%edi\n" );
1863 /* Save the 32-bit stack */
1865 fprintf( outfile, "\tpushl " PREFIX "IF1632_Saved32_esp\n" );
1866 fprintf( outfile, "\tmovl %%esp," PREFIX "IF1632_Saved32_esp\n" );
1867 fprintf( outfile, "\tmovl %%ebp,%%ebx\n" );
1869 /* Print debugging info */
1873 /* Push the address of the first argument */
1874 fprintf( outfile, "\tleal 12(%%ebx),%%eax\n" );
1875 fprintf( outfile, "\tpushl $%d\n", reg_func ? -1 : strlen(args) );
1876 fprintf( outfile, "\tpushl %%eax\n" );
1877 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
1878 fprintf( outfile, "\tpopl %%eax\n" );
1879 fprintf( outfile, "\tpopl %%eax\n" );
1882 /* Switch to the 16-bit stack */
1885 fprintf( outfile,"\tdata16\n");
1887 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp+2,%%ss\n" );
1888 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp,%%sp\n" );
1890 /* Transfer the arguments */
1894 /* Get the registers. ebx is handled later on. */
1895 fprintf( outfile, "\tmovl 12(%%ebx),%%ebx\n" );
1896 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs) );
1897 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1898 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp) );
1899 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax) );
1900 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx) );
1901 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx) );
1902 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi) );
1903 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi) );
1905 else /* not a register function */
1907 int pos = 16; /* first argument position */
1909 /* Make %bp point to the previous stackframe (built by CallFrom16) */
1910 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1911 fprintf( outfile, "\taddw $20,%%bp\n" );
1917 case 'w': /* word */
1918 fprintf( outfile, "\tpushw %d(%%ebx)\n", pos );
1920 case 'l': /* long */
1921 fprintf( outfile, "\tpushl %d(%%ebx)\n", pos );
1924 fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
1931 /* Push the return address */
1933 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_%s\n",
1934 short_ret ? "word" : "long" );
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, "\tpushw %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, "\tpopw %%ds\n" );
1952 /* Push the called routine address */
1954 fprintf( outfile, "\tpushl 12(%%ebx)\n" );
1956 /* Get previous ds from the 16-bit stack and */
1957 /* set ax equal to ds for window procedures. */
1958 fprintf( outfile, "\tmovw -10(%%ebp),%%ax\n" );
1960 fprintf( outfile, "\tdata16\n");
1962 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
1965 /* Jump to the called routine */
1967 fprintf( outfile, "\t.byte 0x66\n" );
1968 fprintf( outfile, "\tlret\n" );
1972 /*******************************************************************
1975 * Build the return code for 16-bit callbacks
1977 static void BuildRet16Func( FILE *outfile )
1979 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_word\n" );
1980 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_long\n" );
1982 /* Put return value into eax */
1984 fprintf( outfile, PREFIX "CALLTO16_Ret_long:\n" );
1985 fprintf( outfile, "\tpushw %%dx\n" );
1986 fprintf( outfile, "\tpushw %%ax\n" );
1987 fprintf( outfile, "\tpopl %%eax\n" );
1988 fprintf( outfile, PREFIX "CALLTO16_Ret_word:\n" );
1990 /* Restore 32-bit segment registers */
1992 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", WINE_DATA_SELECTOR );
1994 fprintf( outfile, "\tdata16\n");
1996 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
1998 fprintf( outfile, "\tdata16\n");
2000 fprintf( outfile, "\tmovw %%bx,%%es\n" );
2002 fprintf( outfile, "\tdata16\n");
2004 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
2006 /* Restore the 32-bit stack */
2008 fprintf( outfile, "\tmovl " PREFIX "IF1632_Saved32_esp,%%esp\n" );
2009 fprintf( outfile, "\tpopl " PREFIX "IF1632_Saved32_esp\n" );
2011 /* Restore the 32-bit registers */
2013 fprintf( outfile, "\tpopl %%edi\n" );
2014 fprintf( outfile, "\tpopl %%esi\n" );
2015 fprintf( outfile, "\tpopl %%edx\n" );
2016 fprintf( outfile, "\tpopl %%ecx\n" );
2017 fprintf( outfile, "\tpopl %%ebx\n" );
2019 /* Return to caller */
2021 fprintf( outfile, "\tpopl %%ebp\n" );
2022 fprintf( outfile, "\tlret\n" );
2024 /* Declare the return address variables */
2026 fprintf( outfile, "\t.data\n" );
2027 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_word\n" );
2028 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_long\n" );
2029 fprintf( outfile, PREFIX "CALLTO16_RetAddr_word:\t.long 0\n" );
2030 fprintf( outfile, PREFIX "CALLTO16_RetAddr_long:\t.long 0\n" );
2031 fprintf( outfile, "\t.text\n" );
2035 /*******************************************************************
2038 * Build the CONTEXT structure on the stack.
2040 static void BuildContext32( FILE *outfile )
2042 /* Build the context structure */
2044 fprintf( outfile, "\tpushw $0\n" );
2045 fprintf( outfile, "\tpushw %%ss\n" );
2046 fprintf( outfile, "\tpushl %%eax\n" ); /* %esp */
2047 fprintf( outfile, "\tpushfl\n" );
2048 fprintf( outfile, "\tpushw $0\n" );
2049 fprintf( outfile, "\tpushw %%cs\n" );
2050 fprintf( outfile, "\tsubl $8,%%esp\n" ); /* %eip + %ebp */
2052 fprintf( outfile, "\tpushl %%eax\n" );
2053 fprintf( outfile, "\tpushl %%ecx\n" );
2054 fprintf( outfile, "\tpushl %%edx\n" );
2055 fprintf( outfile, "\tpushl %%ebx\n" );
2056 fprintf( outfile, "\tpushl %%esi\n" );
2057 fprintf( outfile, "\tpushl %%edi\n" );
2059 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2060 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2061 fprintf( outfile, "\tpushl %%eax\n" );
2062 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2063 fprintf( outfile, "\tpushl %%eax\n" );
2064 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2065 fprintf( outfile, "\tpushl %%eax\n" );
2066 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2067 fprintf( outfile, "\tpushl %%eax\n" );
2069 fprintf( outfile, "\tsubl $%d,%%esp\n",
2070 sizeof(FLOATING_SAVE_AREA) + 6 * sizeof(DWORD) /* DR regs */ );
2071 fprintf( outfile, "\tpushl $0x0001001f\n" ); /* ContextFlags */
2073 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2075 fprintf( outfile, "\tmovl 4(%%ebp),%%eax\n" ); /* %eip at time of call */
2076 fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Eip) );
2077 fprintf( outfile, "\tmovl 0(%%ebp),%%eax\n" ); /* %ebp at time of call */
2078 fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Ebp) );
2079 fprintf( outfile, "\tleal 8(%%ebp),%%eax\n" ); /* %esp at time of call */
2080 fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Esp) );
2082 /* Push pointer to context */
2084 fprintf( outfile, "\tpushl %%esp\n" );
2088 /*******************************************************************
2091 * Restore the registers from the context structure.
2092 * All registers except %cs and %ss are restored.
2094 static void RestoreContext32( FILE *outfile )
2096 /* Restore the context structure */
2098 fprintf( outfile, "\tleal %d(%%ebp),%%esp\n", -sizeof(CONTEXT)-8 );
2099 fprintf( outfile, "\tfrstor %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2101 /* Store flags over the relay addr */
2102 fprintf( outfile, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(EFlags) );
2103 fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", sizeof(CONTEXT) );
2105 /* Get the new stack addr */
2106 fprintf( outfile, "\tmovl %d(%%esp),%%ebx\n", CONTEXTOFFSET(Esp) );
2108 /* Set eip and ebp value onto the new stack */
2109 fprintf( outfile, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Eip) );
2110 fprintf( outfile, "\tmovl %%eax,-4(%%ebx)\n" ); /* %eip at time of call */
2111 fprintf( outfile, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Ebp) );
2112 fprintf( outfile, "\tmovl %%eax,-8(%%ebx)\n" ); /* %ebp at time of call */
2114 /* Set ebp to point to the new stack */
2115 fprintf( outfile, "\tleal -8(%%ebx),%%ebp\n" );
2117 /* Restore all registers */
2118 fprintf( outfile, "\taddl $%d,%%esp\n",
2119 sizeof(FLOATING_SAVE_AREA) + 7 * sizeof(DWORD) );
2120 fprintf( outfile, "\tpopl %%eax\n" );
2121 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2122 fprintf( outfile, "\tpopl %%eax\n" );
2123 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2124 fprintf( outfile, "\tpopl %%eax\n" );
2125 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2126 fprintf( outfile, "\tpopl %%eax\n" );
2127 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2129 fprintf( outfile, "\tpopl %%edi\n" );
2130 fprintf( outfile, "\tpopl %%esi\n" );
2131 fprintf( outfile, "\tpopl %%ebx\n" );
2132 fprintf( outfile, "\tpopl %%edx\n" );
2133 fprintf( outfile, "\tpopl %%ecx\n" );
2134 fprintf( outfile, "\tpopl %%eax\n" );
2136 fprintf( outfile, "\taddl $%d,%%esp\n",
2137 6 * sizeof(DWORD) /* %ebp + %eip + %cs + %efl + %esp + %ss */ );
2138 fprintf( outfile, "\tpopfl\n" );
2142 /*******************************************************************
2143 * BuildCallFrom32Func
2145 * Build a 32-bit-to-Wine call-back function.
2146 * 'args' is the number of dword arguments.
2154 * (ebp-4) entry point
2155 * (ebp-8) relay addr
2157 static void BuildCallFrom32Func( FILE *outfile, const char *profile )
2159 int args, stdcall, reg_func;
2161 if (!strncmp( profile, "stdcall", 7 ))
2165 args = atoi( profile + 8 );
2167 else if (!strncmp( profile, "cdecl", 5 ))
2169 stdcall = reg_func = 0;
2170 args = atoi( profile + 6 );
2172 else if (!strncmp( profile, "regs", 4 ))
2174 stdcall = reg_func = 1;
2175 args = atoi( profile + 5 );
2179 fprintf( stderr, "Invalid function profile '%s', ignored\n", profile );
2183 /* Function header */
2185 fprintf( outfile, "\n\t.align 4\n" );
2187 fprintf( outfile, ".stabs \"CallFrom32_%s:F1\",36,0,0," PREFIX "CallFrom32_%s\n",
2190 fprintf( outfile, "\t.globl " PREFIX "CallFrom32_%s\n", profile );
2191 fprintf( outfile, PREFIX "CallFrom32_%s:\n", profile );
2195 fprintf( outfile, "\tleal 8(%%esp),%%ebp\n" );
2197 /* Transfer the arguments */
2199 if (reg_func) BuildContext32( outfile );
2204 for (i = args; i > 0; i--)
2205 fprintf( outfile, "\tpushl %d(%%ebp)\n", 4 * i + 4 );
2209 /* Push the address of the arguments. The called function will */
2210 /* ignore this if it really takes no arguments. */
2211 fprintf( outfile, "\tleal 8(%%ebp),%%eax\n" );
2212 fprintf( outfile, "\tpushl %%eax\n" );
2217 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2218 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2220 /* Print the debugging info */
2224 fprintf( outfile, "\tpushl $%d\n", /* Nb args */
2225 reg_func ? args | 0x80000000 : args);
2226 fprintf( outfile, "\tpushl %%ebp\n" );
2227 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom32\n" );
2228 fprintf( outfile, "\tadd $8, %%esp\n" );
2231 /* Call the function */
2233 fprintf( outfile, "\tcall -4(%%ebp)\n" );
2235 /* Print the debugging info */
2239 fprintf( outfile, "\tpushl %%eax\n" );
2240 fprintf( outfile, "\tpushl $%d\n", /* Nb args */
2241 reg_func ? args | 0x80000000 : args);
2242 fprintf( outfile, "\tpushl %%ebp\n" );
2243 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom32Ret\n" );
2244 fprintf( outfile, "\tpopl %%eax\n" );
2245 fprintf( outfile, "\tpopl %%eax\n" );
2246 fprintf( outfile, "\tpopl %%eax\n" );
2249 if (reg_func) RestoreContext32( outfile );
2251 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2252 fprintf( outfile, "\tpopl %%ebp\n" );
2254 /* Return, removing arguments */
2256 if (args && stdcall) fprintf( outfile, "\tret $%d\n", args * 4 );
2257 else fprintf( outfile, "\tret\n" );
2261 /*******************************************************************
2264 * Build a Wine-to-32-bit callback function.
2266 * Stack frame of the callback function:
2270 * (ebp+8) func to call
2271 * (ebp+4) return address
2272 * (ebp) previous ebp
2274 * Prototype for the CallTo32 functions:
2275 * extern LONG CallTo32_nn( FARPROC32 func, args... );
2277 static void BuildCallTo32Func( FILE *outfile, int args )
2279 /* Function header */
2281 fprintf( outfile, "\n\t.align 4\n" );
2283 fprintf( outfile, ".stabs \"CallTo32_%d:F1\",36,0,0," PREFIX "CallTo32_%d\n",
2286 fprintf( outfile, "\t.globl " PREFIX "CallTo32_%d\n", args );
2287 fprintf( outfile, PREFIX "CallTo32_%d:\n", args );
2291 fprintf( outfile, "\tpushl %%ebp\n" );
2292 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2294 /* Transfer arguments */
2299 for (i = args; i > 0; i--)
2300 fprintf( outfile, "\tpushl %d(%%ebp)\n", 4 * i + 8 );
2303 /* Print the debugging output */
2307 fprintf( outfile, "\tpushl $%d\n", args );
2308 fprintf( outfile, "\tpushl 8(%%ebp)\n" );
2309 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo32\n" );
2310 fprintf( outfile, "\taddl $8,%%esp\n" );
2313 /* Call the function */
2315 fprintf( outfile, "\tcall 8(%%ebp)\n" );
2317 /* Return to Wine */
2319 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2320 fprintf( outfile, "\tpopl %%ebp\n" );
2321 fprintf( outfile, "\tret\n" );
2325 /*******************************************************************
2328 * Build the spec files
2330 static int BuildSpec( FILE *outfile, int argc, char *argv[] )
2333 for (i = 2; i < argc; i++)
2334 if (BuildSpecFile( outfile, argv[i] ) < 0) return -1;
2339 /*******************************************************************
2342 * Build the 16-bit-to-Wine callbacks
2344 static int BuildCallFrom16( FILE *outfile, char * outname, int argc, char *argv[] )
2351 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2352 fprintf( outfile, "\t.text\n" );
2355 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2356 getcwd(buffer, sizeof(buffer));
2359 * The stabs help the internal debugger as they are an indication that it
2360 * is sensible to step into a thunk/trampoline.
2362 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2363 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2364 fprintf( outfile, "\t.text\n" );
2365 fprintf( outfile, "\t.align 4\n" );
2366 fprintf( outfile, "Code_Start:\n\n" );
2369 /* Build the 32-bit large stack callback */
2371 BuildCall32LargeStack( outfile );
2373 /* Build the callback functions */
2375 for (i = 2; i < argc; i++) BuildCallFrom16Func( outfile, argv[i] );
2377 /* Output the argument debugging strings */
2381 fprintf( outfile, "/* Argument strings */\n" );
2382 for (i = 2; i < argc; i++)
2384 fprintf( outfile, "Profile_%s:\n", argv[i] );
2385 fprintf( outfile, "\t.ascii \"%s\\0\"\n", argv[i] + 5 );
2390 fprintf( outfile, "\t.text\n");
2391 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2392 fprintf( outfile, ".Letext:\n");
2399 /*******************************************************************
2402 * Build the Wine-to-16-bit callbacks
2404 static int BuildCallTo16( FILE *outfile, char * outname, int argc, char *argv[] )
2411 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2412 fprintf( outfile, "\t.text\n" );
2415 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2416 getcwd(buffer, sizeof(buffer));
2419 * The stabs help the internal debugger as they are an indication that it
2420 * is sensible to step into a thunk/trampoline.
2422 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2423 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2424 fprintf( outfile, "\t.text\n" );
2425 fprintf( outfile, "\t.align 4\n" );
2426 fprintf( outfile, "Code_Start:\n\n" );
2429 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Start\n" );
2430 fprintf( outfile, PREFIX "CALLTO16_Start:\n" );
2432 /* Build the callback functions */
2434 for (i = 2; i < argc; i++) BuildCallTo16Func( outfile, argv[i] );
2436 /* Output the 16-bit return code */
2438 BuildRet16Func( outfile );
2440 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_End\n" );
2441 fprintf( outfile, PREFIX "CALLTO16_End:\n" );
2444 fprintf( outfile, "\t.text\n");
2445 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2446 fprintf( outfile, ".Letext:\n");
2453 /*******************************************************************
2456 * Build the 32-bit-to-Wine callbacks
2458 static int BuildCallFrom32( FILE *outfile, char * outname, int argc, char *argv[] )
2465 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2466 fprintf( outfile, "\t.text\n" );
2469 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2470 getcwd(buffer, sizeof(buffer));
2473 * The stabs help the internal debugger as they are an indication that it
2474 * is sensible to step into a thunk/trampoline.
2476 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2477 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2478 fprintf( outfile, "\t.text\n" );
2479 fprintf( outfile, "\t.align 4\n" );
2480 fprintf( outfile, "Code_Start:\n\n" );
2483 /* Build the callback functions */
2485 for (i = 2; i < argc; i++) BuildCallFrom32Func( outfile, argv[i] );
2488 fprintf( outfile, "\t.text\n");
2489 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2490 fprintf( outfile, ".Letext:\n");
2497 /*******************************************************************
2500 * Build the Wine-to-32-bit callbacks
2502 static int BuildCallTo32( FILE *outfile, char * outname,
2503 int argc, char *argv[] )
2510 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2511 fprintf( outfile, "\t.text\n" );
2514 * Throw in a couple of stabs. The internal debugger doesn't really
2515 * care about trying to step through this crap, but we use the file
2516 * names as an indication that we should just step through it to whatever
2517 * is on the other side.
2520 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2521 getcwd(buffer, sizeof(buffer));
2524 * The stabs help the internal debugger as they are an indication that it
2525 * is sensible to step into a thunk/trampoline.
2527 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2528 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2529 fprintf( outfile, "\t.text\n" );
2530 fprintf( outfile, "\t.align 4\n" );
2531 fprintf( outfile, "Code_Start:\n\n" );
2534 /* Build the callback functions */
2536 for (i = 2; i < argc; i++) BuildCallTo32Func( outfile, atoi(argv[i]) );
2539 fprintf( outfile, "\t.text\n");
2540 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2541 fprintf( outfile, ".Letext:\n");
2548 /*******************************************************************
2551 static void usage(void)
2553 fprintf(stderr, "usage: build [-o outfile] -spec SPECNAMES\n"
2554 " build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2555 " build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2556 " build [-o outfile] -callfrom32 FUNCTION_PROFILES\n"
2557 " build [-o outfile] -callto32 FUNCTION_PROFILES\n");
2562 /*******************************************************************
2565 int main(int argc, char **argv)
2567 char *outname = NULL;
2568 FILE *outfile = stdout;
2571 if (argc <= 2) usage();
2573 if (!strcmp( argv[1], "-o" ))
2578 if (argc <= 2) usage();
2579 if (!(outfile = fopen( outname, "w" )))
2581 fprintf( stderr, "Unable to create output file '%s'\n", outname );
2586 if (!strcmp( argv[1], "-spec" ))
2587 res = BuildSpec( outfile, argc, argv );
2588 else if (!strcmp( argv[1], "-callfrom16" ))
2589 res = BuildCallFrom16( outfile, outname, argc, argv );
2590 else if (!strcmp( argv[1], "-callto16" ))
2591 res = BuildCallTo16( outfile, outname, argc, argv );
2592 else if (!strcmp( argv[1], "-callfrom32" ))
2593 res = BuildCallFrom32( outfile, outname, argc, argv );
2594 else if (!strcmp( argv[1], "-callto32" ))
2595 res = BuildCallTo32( outfile, outname, argc, argv );