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;
128 static char DLLName[80];
129 static char DLLFileName[80];
135 char *ParseBuffer = NULL;
140 static int debugging = 1;
142 /* Offset of register relative to the start of the CONTEXT struct */
143 #define CONTEXTOFFSET(reg) ((int)&((CONTEXT *)0)->reg)
145 static void *xmalloc (size_t size)
149 res = malloc (size ? size : 1);
152 fprintf (stderr, "Virtual memory exhausted.\n");
159 static void *xrealloc (void *ptr, size_t size)
161 void *res = realloc (ptr, size);
164 fprintf (stderr, "Virtual memory exhausted.\n");
171 static int IsNumberString(char *s)
180 static char *strupper(char *s)
184 for(p = s; *p != '\0'; p++)
190 static char * GetTokenInLine(void)
195 if (ParseNext != ParseBuffer)
197 if (ParseSaveChar == '\0')
199 *ParseNext = ParseSaveChar;
203 * Remove initial white space.
205 for (p = ParseNext; isspace(*p); p++)
208 if ((*p == '\0') || (*p == '#'))
215 if (*token != '(' && *token != ')')
216 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
226 static char * GetToken(void)
230 if (ParseBuffer == NULL)
232 ParseBuffer = xmalloc(512);
233 ParseNext = ParseBuffer;
237 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
239 if (ParseBuffer[0] != '#')
244 while ((token = GetTokenInLine()) == NULL)
246 ParseNext = ParseBuffer;
250 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
252 if (ParseBuffer[0] != '#')
261 /*******************************************************************
264 * Parse a variable definition.
266 static int ParseVariable( ORDDEF *odp )
271 int value_array_size;
273 char *token = GetToken();
276 fprintf(stderr, "%d: Expected '(' got '%s'\n", Line, token);
281 value_array_size = 25;
282 value_array = xmalloc(sizeof(*value_array) * value_array_size);
284 while ((token = GetToken()) != NULL)
289 value_array[n_values++] = strtol(token, &endptr, 0);
290 if (n_values == value_array_size)
292 value_array_size += 25;
293 value_array = xrealloc(value_array,
294 sizeof(*value_array) * value_array_size);
297 if (endptr == NULL || *endptr != '\0')
299 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
307 fprintf(stderr, "%d: End of file in variable declaration\n", Line);
311 odp->u.var.n_values = n_values;
312 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
318 /*******************************************************************
319 * ParseExportFunction
321 * Parse a function definition.
323 static int ParseExportFunction( ORDDEF *odp )
331 if (odp->type == TYPE_STDCALL)
333 fprintf( stderr, "%d: 'stdcall' not supported for Win16\n", Line );
336 if (odp->type == TYPE_CDECL)
338 fprintf( stderr, "%d: 'cdecl' not supported for Win16\n", Line );
343 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
345 fprintf( stderr, "%d: 'pascal' not supported for Win32\n", Line );
356 fprintf(stderr, "%d: Expected '(' got '%s'\n", Line, token);
360 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
366 if (!strcmp(token, "byte") || !strcmp(token, "word"))
367 odp->u.func.arg_types[i] = 'w';
368 else if (!strcmp(token, "s_byte") || !strcmp(token, "s_word"))
369 odp->u.func.arg_types[i] = 's';
370 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
371 odp->u.func.arg_types[i] = 'l';
372 else if (!strcmp(token, "ptr"))
373 odp->u.func.arg_types[i] = 'p';
374 else if (!strcmp(token, "str"))
375 odp->u.func.arg_types[i] = 't';
376 else if (!strcmp(token, "segstr"))
377 odp->u.func.arg_types[i] = 'T';
380 fprintf(stderr, "%d: Unknown variable type '%s'\n", Line, token);
383 if (SpecType == SPEC_WIN32)
385 if (strcmp(token, "long") && strcmp(token, "ptr"))
387 fprintf( stderr, "%d: Type '%s' not supported for Win32\n",
395 fprintf( stderr, "%d: Too many arguments\n", Line );
398 odp->u.func.arg_types[i] = '\0';
399 if ((odp->type == TYPE_STDCALL) && !i)
400 odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
401 strcpy(odp->u.func.link_name, GetToken());
406 /*******************************************************************
409 * Parse an 'equate' definition.
411 static int ParseEquate( ORDDEF *odp )
415 char *token = GetToken();
416 int value = strtol(token, &endptr, 0);
417 if (endptr == NULL || *endptr != '\0')
419 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
424 odp->u.abs.value = value;
429 /*******************************************************************
432 * Parse a 'return' definition.
434 static int ParseReturn( ORDDEF *odp )
440 odp->u.ret.arg_size = strtol(token, &endptr, 0);
441 if (endptr == NULL || *endptr != '\0')
443 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
449 odp->u.ret.ret_value = strtol(token, &endptr, 0);
450 if (endptr == NULL || *endptr != '\0')
452 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
461 /*******************************************************************
464 * Parse a 'stub' definition.
466 static int ParseStub( ORDDEF *odp )
468 odp->u.func.arg_types[0] = '\0';
469 strcpy( odp->u.func.link_name, STUB_CALLBACK );
474 /*******************************************************************
477 * Parse an 'extern' definition.
479 static int ParseExtern( ORDDEF *odp )
481 if (SpecType == SPEC_WIN16)
483 fprintf( stderr, "%d: 'extern' not supported for Win16\n", Line );
486 strcpy( odp->u.ext.link_name, GetToken() );
491 /*******************************************************************
494 * Parse an ordinal definition.
496 static int ParseOrdinal(int ordinal)
501 if (ordinal >= MAX_ORDINALS)
503 fprintf(stderr, "%d: Ordinal number too large\n", Line);
506 if (ordinal > Limit) Limit = ordinal;
508 odp = &OrdinalDefinitions[ordinal];
509 if (!(token = GetToken()))
511 fprintf(stderr, "%d: Expected type after ordinal\n", Line);
515 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
516 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
519 if (odp->type >= TYPE_NBTYPES)
522 "%d: Expected type after ordinal, found '%s' instead\n",
527 if (!(token = GetToken()))
529 fprintf( stderr, "%d: Expected name after type\n", Line );
532 strcpy( odp->name, token );
540 return ParseVariable( odp );
546 return ParseExportFunction( odp );
548 return ParseEquate( odp );
550 return ParseReturn( odp );
552 return ParseStub( odp );
554 return ParseExtern( odp );
556 fprintf( stderr, "Should not happen\n" );
562 /*******************************************************************
567 static int ParseTopLevel(void)
571 while ((token = GetToken()) != NULL)
573 if (strcmp(token, "name") == 0)
575 strcpy(DLLName, GetToken());
577 if (!DLLFileName[0]) sprintf( DLLFileName, "%s.DLL", DLLName );
579 else if (strcmp(token, "file") == 0)
581 strcpy(DLLFileName, GetToken());
582 strupper(DLLFileName);
584 else if (strcmp(token, "type") == 0)
587 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
588 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
591 fprintf(stderr, "%d: Type must be 'win16' or 'win32'\n", Line);
595 else if (strcmp(token, "base") == 0)
598 if (!IsNumberString(token))
600 fprintf(stderr, "%d: Expected number after base\n", Line);
605 else if (strcmp(token, "heap") == 0)
608 if (!IsNumberString(token))
610 fprintf(stderr, "%d: Expected number after heap\n", Line);
613 DLLHeapSize = atoi(token);
615 else if (IsNumberString(token))
620 ordinal = atoi(token);
621 if ((rv = ParseOrdinal(ordinal)) < 0)
627 "%d: Expected name, id, length or ordinal\n", Line);
636 /*******************************************************************
639 * Store a list of ints into a byte array.
641 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
648 for (i = 0; i < odp->u.var.n_values; i++)
649 buffer[i] = odp->u.var.values[i];
652 for (i = 0; i < odp->u.var.n_values; i++)
653 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
656 for (i = 0; i < odp->u.var.n_values; i++)
657 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
660 return odp->u.var.n_values * size;
664 /*******************************************************************
667 * Dump a byte stream into the assembly code.
669 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
670 const char *section, const char *label_start )
673 if (section) fprintf( outfile, "\t%s\n", section );
674 if (label_start) fprintf( outfile, "%s:\n", label_start );
675 for (i = 0; i < len; i++)
677 if (!(i & 0x0f)) fprintf( outfile, "\t.byte " );
678 fprintf( outfile, "%d", *data++ );
680 fprintf( outfile, "%c", ((i & 0x0f) != 0x0f) ? ',' : '\n' );
682 fprintf( outfile, "\n" );
686 /*******************************************************************
689 * Build the in-memory representation of a 16-bit NE module, and dump it
690 * as a byte stream into the assembly code.
692 static int BuildModule16( FILE *outfile, int max_code_offset,
693 int max_data_offset )
699 SEGTABLEENTRY *pSegment;
706 * OFSTRUCT File information
707 * SEGTABLEENTRY Segment 1 (code)
708 * SEGTABLEENTRY Segment 2 (data)
709 * WORD[2] Resource table (empty)
710 * BYTE[2] Imported names (empty)
711 * BYTE[n] Resident names table
712 * BYTE[n] Entry table
715 buffer = xmalloc( 0x10000 );
717 pModule = (NE_MODULE *)buffer;
718 pModule->magic = IMAGE_OS2_SIGNATURE;
721 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
723 pModule->heap_size = DLLHeapSize;
724 pModule->stack_size = 0;
729 pModule->seg_count = 2;
730 pModule->modref_count = 0;
731 pModule->nrname_size = 0;
732 pModule->modref_table = 0;
733 pModule->nrname_fpos = 0;
734 pModule->moveable_entries = 0;
735 pModule->alignment = 0;
736 pModule->truetype = 0;
737 pModule->os_flags = NE_OSFLAGS_WINDOWS;
738 pModule->misc_flags = 0;
739 pModule->dlls_to_init = 0;
740 pModule->nrname_handle = 0;
741 pModule->min_swap_area = 0;
742 pModule->expected_version = 0x030a;
743 pModule->pe_module = NULL;
745 pModule->self_loading_sel = 0;
747 /* File information */
749 pFileInfo = (OFSTRUCT *)(pModule + 1);
750 pModule->fileinfo = (int)pFileInfo - (int)pModule;
751 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
752 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
753 + strlen(DLLFileName);
754 strcpy( pFileInfo->szPathName, DLLFileName );
755 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
759 pSegment = (SEGTABLEENTRY *)pstr;
760 pModule->seg_table = (int)pSegment - (int)pModule;
761 pSegment->filepos = 0;
762 pSegment->size = max_code_offset;
764 pSegment->minsize = max_code_offset;
765 pSegment->selector = 0;
768 pModule->dgroup_entry = (int)pSegment - (int)pModule;
769 pSegment->filepos = 0;
770 pSegment->size = max_data_offset;
771 pSegment->flags = NE_SEGFLAGS_DATA;
772 pSegment->minsize = max_data_offset;
773 pSegment->selector = 0;
778 pword = (WORD *)pSegment;
779 pModule->res_table = (int)pword - (int)pModule;
783 /* Imported names table */
785 pstr = (char *)pword;
786 pModule->import_table = (int)pstr - (int)pModule;
790 /* Resident names table */
792 pModule->name_table = (int)pstr - (int)pModule;
793 /* First entry is module name */
794 *pstr = strlen(DLLName );
795 strcpy( pstr + 1, DLLName );
798 pstr += sizeof(WORD);
799 /* Store all ordinals */
800 odp = OrdinalDefinitions + 1;
801 for (i = 1; i <= Limit; i++, odp++)
803 if (!odp->name[0]) continue;
804 *pstr = strlen( odp->name );
805 strcpy( pstr + 1, odp->name );
806 strupper( pstr + 1 );
809 pstr += sizeof(WORD);
815 pModule->entry_table = (int)pstr - (int)pModule;
817 odp = OrdinalDefinitions + 1;
818 for (i = 1; i <= Limit; i++, odp++)
829 selector = 1; /* Code selector */
835 selector = 2; /* Data selector */
839 selector = 0xfe; /* Constant selector */
843 selector = 0; /* Invalid selector */
847 /* create a new bundle if necessary */
848 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
852 bundle[1] = selector;
860 *(WORD *)pstr = odp->offset;
861 pstr += sizeof(WORD);
866 /* Dump the module content */
868 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
869 ".data", "Module_Start" );
870 return (int)pstr - (int)pModule;
874 /*******************************************************************
877 * Build the in-memory representation of a 32-bit pseudo-NE module, and dump it
878 * as a byte stream into the assembly code.
880 static int BuildModule32( FILE *outfile )
890 * OFSTRUCT File information
891 * SEGTABLEENTRY Segment table (empty)
892 * WORD[2] Resource table (empty)
893 * BYTE[2] Imported names (empty)
894 * BYTE[n] Resident names table (1 entry)
895 * BYTE[n] Entry table (empty)
898 buffer = xmalloc( 0x10000 );
900 pModule = (NE_MODULE *)buffer;
901 pModule->magic = IMAGE_OS2_SIGNATURE;
904 pModule->dgroup_entry = 0;
905 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN |
906 NE_FFLAGS_LIBMODULE | NE_FFLAGS_WIN32;
908 pModule->heap_size = DLLHeapSize;
909 pModule->stack_size = 0;
914 pModule->seg_count = 0;
915 pModule->modref_count = 0;
916 pModule->nrname_size = 0;
917 pModule->modref_table = 0;
918 pModule->nrname_fpos = 0;
919 pModule->moveable_entries = 0;
920 pModule->alignment = 0;
921 pModule->truetype = 0;
922 pModule->os_flags = NE_OSFLAGS_WINDOWS;
923 pModule->misc_flags = 0;
924 pModule->dlls_to_init = 0;
925 pModule->nrname_handle = 0;
926 pModule->min_swap_area = 0;
927 pModule->expected_version = 0x030a;
928 pModule->pe_module = NULL;
930 pModule->self_loading_sel = 0;
932 /* File information */
934 pFileInfo = (OFSTRUCT *)(pModule + 1);
935 pModule->fileinfo = (int)pFileInfo - (int)pModule;
936 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
937 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
938 + strlen(DLLFileName);
939 strcpy( pFileInfo->szPathName, DLLFileName );
940 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
944 pModule->seg_table = (int)pstr - (int)pModule;
948 pword = (WORD *)pstr;
949 pModule->res_table = (int)pword - (int)pModule;
953 /* Imported names table */
955 pstr = (char *)pword;
956 pModule->import_table = (int)pstr - (int)pModule;
960 /* Resident names table */
962 pModule->name_table = (int)pstr - (int)pModule;
963 /* First entry is module name */
964 *pstr = strlen(DLLName );
965 strcpy( pstr + 1, DLLName );
968 pstr += sizeof(WORD);
973 pModule->entry_table = (int)pstr - (int)pModule;
976 /* Dump the module content */
978 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
979 ".data", "Module_Start" );
980 return (int)pstr - (int)pModule;
984 /*******************************************************************
987 * Build a Win32 assembly file from a spec file.
989 static int BuildSpec32File( char * specfile, FILE *outfile )
992 int i, module_size, len;
995 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
996 fprintf( outfile, "\t.file\t\"%s\"\n", specfile );
998 getcwd(buffer, sizeof(buffer));
1001 * The stabs help the internal debugger as they are an indication that it
1002 * is sensible to step into a thunk/trampoline.
1004 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
1005 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", specfile);
1008 fprintf( outfile, "\t.text\n" );
1009 fprintf( outfile, "\t.align 4\n" );
1010 fprintf( outfile, "Code_Start:\n\n" );
1012 odp = OrdinalDefinitions;
1013 for (i = 0; i <= Limit; i++, odp++)
1024 fprintf( outfile, "/* %s.%d (%s) */\n", DLLName, i, odp->name);
1026 fprintf( outfile, ".stabs \"%s_%d:F1\",36,0,%d,%s_%d\n",
1027 DLLName, i, odp->lineno, DLLName, i);
1029 fprintf( outfile, "%s_%d:\n", DLLName, i );
1031 fprintf( outfile, ".stabn 68,0,%d,0\n", odp->lineno);
1033 fprintf( outfile, "\tpushl %%ebp\n" );
1034 fprintf( outfile, "\tpushl $" PREFIX "%s\n",odp->u.func.link_name);
1035 fprintf( outfile, "\tcall " PREFIX "CallFrom32_%s_%d\n",
1036 (odp->type == TYPE_REGISTER) ? "regs" :
1037 ((odp->type == TYPE_STDCALL) ? "stdcall" : "cdecl"),
1038 strlen(odp->u.func.arg_types));
1039 fprintf( outfile, "\tnop\n" );
1043 fprintf( outfile, "/* %s.%d (%s) */\n", DLLName, i, odp->name);
1045 fprintf( outfile, ".stabs \"%s_%d:F1\",36,0,%d,%s_%d\n",
1046 DLLName, i, odp->lineno, DLLName, i);
1048 fprintf( outfile, "%s_%d:\n", DLLName, i );
1050 fprintf( outfile, ".stabn 68,0,%d,0\n", odp->lineno);
1052 fprintf( outfile, "\tmovl $%d,%%eax\n", odp->u.ret.ret_value );
1053 if (odp->u.ret.arg_size)
1055 fprintf( outfile, "\tret $%d\n", odp->u.ret.arg_size );
1059 fprintf( outfile, "\tret\n" );
1060 fprintf( outfile, "\tnop\n" );
1061 fprintf( outfile, "\tnop\n" );
1066 fprintf( outfile, "/* %s.%d (%s) */\n", DLLName, i, odp->name);
1067 fprintf( outfile, "\t.data\n" );
1068 fprintf( outfile, "%s_%d:\n", DLLName, i );
1069 len = StoreVariableCode( buffer, 1, odp );
1070 DumpBytes( outfile, buffer, len, NULL, NULL );
1071 fprintf( outfile, "\t.text\n" );
1075 fprintf( outfile, "/* %s.%d (%s) */\n",
1076 DLLName, i, odp->name);
1077 fprintf( outfile, "\t.data\n" );
1078 fprintf( outfile, "%s_%d:\n", DLLName, i );
1079 len = StoreVariableCode( buffer, 2, odp );
1080 DumpBytes( outfile, buffer, len, NULL, NULL );
1081 fprintf( outfile, "\t.text\n" );
1085 fprintf( outfile, "/* %s.%d (%s) */\n",
1086 DLLName, i, odp->name);
1087 fprintf( outfile, "\t.data\n" );
1088 fprintf( outfile, "%s_%d:\n", DLLName, i );
1089 len = StoreVariableCode( buffer, 4, odp );
1090 DumpBytes( outfile, buffer, len, NULL, NULL );
1091 fprintf( outfile, "\t.text\n" );
1098 fprintf(stderr,"build: function type %d not available for Win32\n",
1104 module_size = BuildModule32( outfile );
1106 /* Output the DLL functions table */
1108 fprintf( outfile, "\t.text\n" );
1109 fprintf( outfile, "\t.align 4\n" );
1110 fprintf( outfile, "Functions:\n" );
1111 odp = OrdinalDefinitions;
1112 for (i = 0; i <= Limit; i++, odp++)
1117 fprintf( outfile, "\t.long 0\n" );
1120 fprintf( outfile, "\t.long " PREFIX "%s\n", odp->u.ext.link_name );
1123 fprintf( outfile, "\t.long %s_%d\n", DLLName, i );
1128 /* Output the DLL names table */
1130 fprintf( outfile, "FuncNames:\n" );
1131 odp = OrdinalDefinitions;
1132 for (i = 0; i <= Limit; i++, odp++)
1134 if (odp->type == TYPE_INVALID) fprintf( outfile, "\t.long 0\n" );
1135 else fprintf( outfile, "\t.long Name_%d\n", i );
1138 /* Output the DLL names */
1140 for (i = 0, odp = OrdinalDefinitions; i <= Limit; i++, odp++)
1142 if (odp->type != TYPE_INVALID)
1143 fprintf( outfile, "Name_%d:\t.ascii \"%s\\0\"\n", i, odp->name );
1146 /* Output the DLL descriptor */
1148 fprintf( outfile, "DLLName:\t.ascii \"%s\\0\"\n", DLLName );
1149 fprintf( outfile, "\t.align 4\n" );
1150 fprintf( outfile, "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1151 fprintf( outfile, PREFIX "%s_Descriptor:\n", DLLName );
1152 fprintf( outfile, "\t.long DLLName\n" ); /* Name */
1153 fprintf( outfile, "\t.long Module_Start\n" ); /* Module start */
1154 fprintf( outfile, "\t.long %d\n", module_size ); /* Module size */
1155 fprintf( outfile, "\t.long %d\n", Base ); /* Base */
1156 fprintf( outfile, "\t.long %d\n", Limit+1 ); /* Size */
1157 fprintf( outfile, "\t.long Code_Start\n" ); /* Code start */
1158 fprintf( outfile, "\t.long Functions\n" ); /* Functions */
1159 fprintf( outfile, "\t.long FuncNames\n" ); /* Function names */
1161 fprintf( outfile, "\t.text\n");
1162 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
1163 fprintf( outfile, ".Letext:\n");
1169 /*******************************************************************
1172 * Build a Win16 assembly file from a spec file.
1174 static int BuildSpec16File( char * specfile, FILE *outfile )
1178 int code_offset, data_offset, module_size;
1179 unsigned char *data;
1181 data = (unsigned char *)xmalloc( 0x10000 );
1182 memset( data, 0, 16 );
1185 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
1186 fprintf( outfile, "\t.text\n" );
1187 fprintf( outfile, "Code_Start:\n" );
1190 odp = OrdinalDefinitions;
1191 for (i = 0; i <= Limit; i++, odp++)
1196 odp->offset = 0xffff;
1200 odp->offset = LOWORD(odp->u.abs.value);
1204 odp->offset = data_offset;
1205 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1209 odp->offset = data_offset;
1210 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1214 odp->offset = data_offset;
1215 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1219 fprintf( outfile,"/* %s.%d */\n", DLLName, i);
1220 fprintf( outfile,"\tmovw $%d,%%ax\n",LOWORD(odp->u.ret.ret_value));
1221 fprintf( outfile,"\tmovw $%d,%%dx\n",HIWORD(odp->u.ret.ret_value));
1222 fprintf( outfile,"\t.byte 0x66\n");
1223 if (odp->u.ret.arg_size != 0)
1224 fprintf( outfile, "\tlret $%d\n\n", odp->u.ret.arg_size);
1227 fprintf( outfile, "\tlret\n");
1228 fprintf( outfile, "\tnop\n");
1229 fprintf( outfile, "\tnop\n\n");
1231 odp->offset = code_offset;
1232 code_offset += 12; /* Assembly code is 12 bytes long */
1237 case TYPE_PASCAL_16:
1239 fprintf( outfile, "/* %s.%d */\n", DLLName, i);
1240 fprintf( outfile, "\tpushw %%bp\n" );
1241 fprintf( outfile, "\tpushl $" PREFIX "%s\n",odp->u.func.link_name);
1242 /* FreeBSD does not understand lcall, so do it the hard way */
1243 fprintf( outfile, "\t.byte 0x9a\n" );
1244 fprintf( outfile, "\t.long " PREFIX "CallFrom16_%s_%s\n",
1245 (odp->type == TYPE_REGISTER) ? "regs" :
1246 (odp->type == TYPE_PASCAL) ? "long" : "word",
1247 odp->u.func.arg_types );
1248 fprintf( outfile,"\t.byte 0x%02x,0x%02x\n", /* Some asms don't have .word */
1249 LOBYTE(WINE_CODE_SELECTOR), HIBYTE(WINE_CODE_SELECTOR) );
1250 fprintf( outfile, "\tnop\n" );
1251 fprintf( outfile, "\tnop\n\n" );
1252 odp->offset = code_offset;
1253 code_offset += 16; /* Assembly code is 16 bytes long */
1257 fprintf(stderr,"build: function type %d not available for Win16\n",
1263 if (!code_offset) /* Make sure the code segment is not empty */
1265 fprintf( outfile, "\t.byte 0\n" );
1269 /* Output data segment */
1271 DumpBytes( outfile, data, data_offset, NULL, "Data_Start" );
1273 /* Build the module */
1275 module_size = BuildModule16( outfile, code_offset, data_offset );
1277 /* Output the DLL descriptor */
1279 fprintf( outfile, "\t.text\n" );
1280 fprintf( outfile, "DLLName:\t.ascii \"%s\\0\"\n", DLLName );
1281 fprintf( outfile, "\t.align 4\n" );
1282 fprintf( outfile, "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1283 fprintf( outfile, PREFIX "%s_Descriptor:\n", DLLName );
1284 fprintf( outfile, "\t.long DLLName\n" ); /* Name */
1285 fprintf( outfile, "\t.long Module_Start\n" ); /* Module start */
1286 fprintf( outfile, "\t.long %d\n", module_size ); /* Module size */
1287 fprintf( outfile, "\t.long Code_Start\n" ); /* Code start */
1288 fprintf( outfile, "\t.long Data_Start\n" ); /* Data start */
1293 /*******************************************************************
1296 * Build an assembly file from a spec file.
1298 static int BuildSpecFile( FILE *outfile, char *specname )
1300 SpecFp = fopen( specname, "r");
1303 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1307 if (ParseTopLevel() < 0) return -1;
1312 return BuildSpec16File( specname, outfile );
1314 return BuildSpec32File( specname, outfile );
1316 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1322 /*******************************************************************
1323 * BuildCall32LargeStack
1325 * Build the function used to switch to the original 32-bit stack
1326 * before calling a 32-bit function from 32-bit code. This is used for
1327 * functions that need a large stack, like X bitmaps functions.
1329 * The generated function has the following prototype:
1330 * int CALLTO32_LargeStack( int (*func)(), void *arg );
1339 static void BuildCall32LargeStack( FILE *outfile )
1341 /* Function header */
1343 fprintf( outfile, "\n\t.align 4\n" );
1345 fprintf( outfile, ".stabs \"CALLTO32_LargeStack:F1\",36,0,0," PREFIX "CALLTO32_LargeStack\n");
1347 fprintf( outfile, "\t.globl " PREFIX "CALLTO32_LargeStack\n" );
1348 fprintf( outfile, PREFIX "CALLTO32_LargeStack:\n" );
1352 fprintf( outfile, "\tpushl %%ebp\n" );
1353 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
1355 /* Retrieve the original 32-bit stack pointer and switch to it if any */
1357 fprintf( outfile, "\tmovl " PREFIX "IF1632_Original32_esp, %%eax\n" );
1358 fprintf( outfile, "\torl %%eax,%%eax\n" );
1359 fprintf( outfile, "\tje no_orig_esp\n" );
1360 fprintf( outfile, "\tmovl %%eax,%%esp\n" );
1361 fprintf( outfile, "no_orig_esp:\n" );
1363 /* Transfer the argument and call the function */
1365 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
1366 fprintf( outfile, "\tcall 8(%%ebp)\n" );
1368 /* Restore registers and return */
1370 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
1371 fprintf( outfile, "\tpopl %%ebp\n" );
1372 fprintf( outfile, "\tret\n" );
1376 /*******************************************************************
1377 * TransferArgs16To32
1379 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1380 * The 16-bit stack layout is:
1388 static int TransferArgs16To32( FILE *outfile, char *args )
1390 int i, pos16, pos32;
1392 /* Save ebx first */
1394 fprintf( outfile, "\tpushl %%ebx\n" );
1396 /* Get the 32-bit stack pointer */
1398 fprintf( outfile, "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1400 /* Copy the arguments */
1402 pos16 = 6; /* skip bp and return address */
1405 for (i = strlen(args); i > 0; i--)
1410 case 'w': /* word */
1411 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1412 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1416 case 's': /* s_word */
1417 fprintf( outfile, "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1418 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1422 case 'l': /* long or segmented pointer */
1423 case 'T': /* segmented pointer to null-terminated string */
1424 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", pos16 );
1425 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1429 case 'p': /* linear pointer */
1430 case 't': /* linear pointer to null-terminated string */
1431 /* Get the selector */
1432 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1433 /* Get the selector base */
1434 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1435 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1436 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1437 /* Add the offset */
1438 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1439 fprintf( outfile, "\taddl %%eax,%d(%%ebx)\n", pos32 );
1444 fprintf( stderr, "Unknown arg type '%c'\n", args[i-1] );
1450 fprintf( outfile, "\tpopl %%ebx\n" );
1452 return pos16 - 6; /* Return the size of the 16-bit args */
1456 /*******************************************************************
1459 * Build the context structure on the 32-bit stack.
1461 static void BuildContext16( FILE *outfile )
1463 /* Save ebx first */
1465 fprintf( outfile, "\tpushl %%ebx\n" );
1467 /* Get the 32-bit stack pointer */
1469 fprintf( outfile, "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1471 /* Store the registers */
1473 fprintf( outfile, "\tpopl %d(%%ebx)\n", /* Get ebx from stack*/
1474 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1475 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1476 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1477 fprintf( outfile, "\tmovl %%ecx,%d(%%ebx)\n",
1478 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1479 fprintf( outfile, "\tmovl %%edx,%d(%%ebx)\n",
1480 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1481 fprintf( outfile, "\tmovl %%esi,%d(%%ebx)\n",
1482 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1483 fprintf( outfile, "\tmovl %%edi,%d(%%ebx)\n",
1484 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1486 fprintf( outfile, "\tmovzwl -10(%%ebp),%%eax\n" ); /* Get %ds from stack*/
1487 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1488 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1489 fprintf( outfile, "\tmovzwl -6(%%ebp),%%eax\n" ); /* Get %es from stack*/
1490 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1491 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1492 fprintf( outfile, "\tpushfl\n" );
1493 fprintf( outfile, "\tpopl %d(%%ebx)\n",
1494 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1495 fprintf( outfile, "\tmovl -16(%%ebp),%%eax\n" ); /* Get %ebp from stack */
1496 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1497 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1498 fprintf( outfile, "\tmovzwl 2(%%ebp),%%eax\n" ); /* Get %ip from stack */
1499 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1500 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1501 fprintf( outfile, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1502 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1503 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1504 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
1505 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1506 CONTEXTOFFSET(SegFs) - sizeof(CONTEXT) );
1507 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1508 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1509 CONTEXTOFFSET(SegGs) - sizeof(CONTEXT) );
1510 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1511 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1512 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1514 fprintf( outfile, "\tfsave %d(%%ebx)\n",
1515 CONTEXTOFFSET(FloatSave) - sizeof(CONTEXT) );
1520 /*******************************************************************
1523 * Restore the registers from the context structure
1525 static void RestoreContext16( FILE *outfile )
1527 /* Get the 32-bit stack pointer */
1529 fprintf( outfile, "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1531 /* Remove everything up to the return address from the 16-bit stack */
1533 fprintf( outfile, "\taddl $18,%%esp\n" );
1535 /* Restore the registers */
1537 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n",
1538 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1539 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n",
1540 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1541 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n",
1542 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1543 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n",
1544 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1545 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n",
1546 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1547 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new ds */
1548 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1549 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new es */
1550 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1551 fprintf( outfile, "\tpushl %d(%%ebx)\n",
1552 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1553 fprintf( outfile, "\tpopfl\n" );
1554 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1555 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1556 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n",
1557 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1558 fprintf( outfile, "\tpopw %%es\n" ); /* Set es */
1559 fprintf( outfile, "\tpopw %%ds\n" ); /* Set ds */
1563 /*******************************************************************
1564 * BuildCallFrom16Func
1566 * Build a 16-bit-to-Wine callback function. The syntax of the function
1567 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1568 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1569 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1570 * 'T'=segmented pointer to null-terminated string).
1571 * For register functions, the arguments are ignored, but they are still
1572 * removed from the stack upon return.
1574 * Stack layout upon entry to the callback function:
1576 * (sp+18) word first 16-bit arg
1580 * (sp+8) long 32-bit entry point (used to store edx)
1581 * (sp+6) word high word of cs (always 0, used to store es)
1582 * (sp+4) word low word of cs of 16-bit entry point
1583 * (sp+2) word high word of ip (always 0, used to store ds)
1584 * (sp) word low word of ip of 16-bit entry point
1586 * Added on the stack:
1588 * (sp-6) word saved previous sp
1589 * (sp-8) word saved previous ss
1591 static void BuildCallFrom16Func( FILE *outfile, char *profile )
1596 char *args = profile + 5;
1598 /* Parse function type */
1600 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1601 else if (!strncmp( "regs_", profile, 5 )) reg_func = 1;
1602 else if (strncmp( "long_", profile, 5 ))
1604 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1608 /* Function header */
1610 fprintf( outfile, "\n\t.align 4\n" );
1612 fprintf( outfile, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX "CallFrom16_%s\n",
1615 fprintf( outfile, "\t.globl " PREFIX "CallFrom16_%s\n", profile );
1616 fprintf( outfile, PREFIX "CallFrom16_%s:\n", profile );
1618 /* Setup bp to point to its copy on the stack */
1620 fprintf( outfile, "\tpushl %%ebp\n" ); /* Save the full 32-bit ebp */
1621 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1622 fprintf( outfile, "\taddw $16,%%bp\n" );
1624 /* Save 16-bit ds and es */
1626 /* Stupid FreeBSD assembler doesn't know these either */
1627 /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1628 fprintf( outfile, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1629 /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1630 fprintf( outfile, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1632 /* Restore 32-bit ds and es */
1634 fprintf( outfile, "\tpushl $0x%04x%04x\n",
1635 WINE_DATA_SELECTOR, WINE_DATA_SELECTOR );
1636 fprintf( outfile, "\tpopw %%ds\n" );
1637 fprintf( outfile, "\tpopw %%es\n" );
1640 /* Save the 16-bit stack */
1642 fprintf( outfile, "\tpushl " PREFIX "IF1632_Saved16_ss_sp\n" );
1644 fprintf( outfile,"\tdata16\n");
1646 fprintf( outfile, "\tmovw %%ss," PREFIX "IF1632_Saved16_ss_sp+2\n" );
1647 fprintf( outfile, "\tmovw %%sp," PREFIX "IF1632_Saved16_ss_sp\n" );
1649 /* Transfer the arguments */
1651 if (reg_func) BuildContext16( outfile );
1652 else if (*args) argsize = TransferArgs16To32( outfile, args );
1654 /* Get the address of the API function */
1656 fprintf( outfile, "\tmovl -4(%%ebp),%%eax\n" );
1658 /* If necessary, save %edx over the API function address */
1660 if (!reg_func && short_ret)
1661 fprintf( outfile, "\tmovl %%edx,-4(%%ebp)\n" );
1663 /* Switch to the 32-bit stack */
1665 fprintf( outfile, "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebp\n" );
1666 fprintf( outfile, "\tpushw %%ds\n" );
1667 fprintf( outfile, "\tpopw %%ss\n" );
1668 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1669 reg_func ? sizeof(CONTEXT) : 4 * strlen(args) );
1670 if (reg_func) /* Push the address of the context struct */
1671 fprintf( outfile, "\tpushl %%esp\n" );
1673 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1675 fprintf( outfile, "\taddl $24,%%ebp\n" );
1677 /* Print the debug information before the call */
1681 fprintf( outfile, "\tpushl %%eax\n" );
1682 fprintf( outfile, "\tpushl $Profile_%s\n", profile );
1683 fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1684 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n" );
1685 fprintf( outfile, "\tpopl %%eax\n" );
1686 fprintf( outfile, "\tpopl %%eax\n" );
1687 fprintf( outfile, "\tpopl %%eax\n" );
1690 /* Call the entry point */
1692 fprintf( outfile, "\tcall %%eax\n" );
1694 /* Print the debug information after the call */
1698 fprintf( outfile, "\tpushl %%eax\n" );
1699 fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1700 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n" );
1701 fprintf( outfile, "\tpopl %%eax\n" );
1702 fprintf( outfile, "\tpopl %%eax\n" );
1705 /* Restore the 16-bit stack */
1708 fprintf( outfile, "\tdata16\n");
1710 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp+2,%%ss\n" );
1711 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp,%%sp\n" );
1712 fprintf( outfile, "\tpopl " PREFIX "IF1632_Saved16_ss_sp\n" );
1716 /* Calc the arguments size */
1732 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1737 /* Restore registers from the context structure */
1738 RestoreContext16( outfile );
1742 /* Restore high 16 bits of ebp */
1743 fprintf( outfile, "\tpopl %%ebp\n" );
1745 /* Restore ds and es */
1746 fprintf( outfile, "\tincl %%esp\n" ); /* Remove ip */
1747 fprintf( outfile, "\tincl %%esp\n" );
1748 fprintf( outfile, "\tpopl %%edx\n" ); /* Remove cs and ds */
1749 fprintf( outfile, "\tmovw %%dx,%%ds\n" ); /* and restore ds */
1750 fprintf( outfile, "\tpopw %%es\n" ); /* Restore es */
1752 if (short_ret) fprintf( outfile, "\tpopl %%edx\n" ); /* Restore edx */
1755 /* Get the return value into dx:ax */
1756 fprintf( outfile, "\tmovl %%eax,%%edx\n" );
1757 fprintf( outfile, "\tshrl $16,%%edx\n" );
1758 /* Remove API entry point */
1759 fprintf( outfile, "\taddl $4,%%esp\n" );
1762 /* Restore low 16 bits of ebp */
1763 fprintf( outfile, "\tpopw %%bp\n" );
1766 /* Remove the arguments and return */
1770 fprintf( outfile, "\t.byte 0x66\n" );
1771 fprintf( outfile, "\tlret $%d\n", argsize );
1775 fprintf( outfile, "\t.byte 0x66\n" );
1776 fprintf( outfile, "\tlret\n" );
1781 /*******************************************************************
1784 * Build a Wine-to-16-bit callback function.
1786 * Stack frame of the callback function:
1790 * (ebp+12) func to call
1791 * (ebp+8) code selector
1792 * (ebp+4) return address
1793 * (ebp) previous ebp
1795 * Prototypes for the CallTo16 functions:
1796 * extern WORD CallTo16_word_xxx( FARPROC16 func, args... );
1797 * extern LONG CallTo16_long_xxx( FARPROC16 func, args... );
1798 * extern void CallTo16_regs_( const CONTEXT *context );
1800 static void BuildCallTo16Func( FILE *outfile, char *profile )
1804 char *args = profile + 5;
1806 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1807 else if (!strncmp( "regs_", profile, 5 )) reg_func = short_ret = 1;
1808 else if (strncmp( "long_", profile, 5 ))
1810 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1814 /* Function header */
1816 fprintf( outfile, "\n\t.align 4\n" );
1818 fprintf( outfile, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX "CallTo16_%s\n",
1821 fprintf( outfile, "\t.globl " PREFIX "CallTo16_%s\n", profile );
1822 fprintf( outfile, PREFIX "CallTo16_%s:\n", profile );
1824 /* Push code selector before return address to simulate a lcall */
1826 fprintf( outfile, "\tpopl %%eax\n" );
1827 fprintf( outfile, "\tpushl $0x%04x\n", WINE_CODE_SELECTOR );
1828 fprintf( outfile, "\tpushl %%eax\n" );
1832 fprintf( outfile, "\tpushl %%ebp\n" );
1833 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
1835 /* Save the 32-bit registers */
1837 fprintf( outfile, "\tpushl %%ebx\n" );
1838 fprintf( outfile, "\tpushl %%ecx\n" );
1839 fprintf( outfile, "\tpushl %%edx\n" );
1840 fprintf( outfile, "\tpushl %%esi\n" );
1841 fprintf( outfile, "\tpushl %%edi\n" );
1843 /* Save the 32-bit stack */
1845 fprintf( outfile, "\tpushl " PREFIX "IF1632_Saved32_esp\n" );
1846 fprintf( outfile, "\tmovl %%esp," PREFIX "IF1632_Saved32_esp\n" );
1847 fprintf( outfile, "\tmovl %%ebp,%%ebx\n" );
1849 /* Print debugging info */
1853 /* Push the address of the first argument */
1854 fprintf( outfile, "\tleal 12(%%ebx),%%eax\n" );
1855 fprintf( outfile, "\tpushl $%d\n", reg_func ? -1 : strlen(args) );
1856 fprintf( outfile, "\tpushl %%eax\n" );
1857 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
1858 fprintf( outfile, "\tpopl %%eax\n" );
1859 fprintf( outfile, "\tpopl %%eax\n" );
1862 /* Switch to the 16-bit stack */
1865 fprintf( outfile,"\tdata16\n");
1867 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp+2,%%ss\n" );
1868 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp,%%sp\n" );
1870 /* Transfer the arguments */
1874 /* Get the registers. ebx is handled later on. */
1875 fprintf( outfile, "\tmovl 12(%%ebx),%%ebx\n" );
1876 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs) );
1877 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1878 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp) );
1879 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax) );
1880 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx) );
1881 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx) );
1882 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi) );
1883 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi) );
1885 else /* not a register function */
1887 int pos = 16; /* first argument position */
1889 /* Make %bp point to the previous stackframe (built by CallFrom16) */
1890 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1891 fprintf( outfile, "\taddw $20,%%bp\n" );
1897 case 'w': /* word */
1898 fprintf( outfile, "\tpushw %d(%%ebx)\n", pos );
1900 case 'l': /* long */
1901 fprintf( outfile, "\tpushl %d(%%ebx)\n", pos );
1904 fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
1911 /* Push the return address */
1913 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_%s\n",
1914 short_ret ? "word" : "long" );
1918 /* Push the called routine address */
1920 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
1921 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
1923 /* Get the 16-bit ds */
1925 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
1926 /* Get ebx from the 32-bit stack */
1927 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(Ebx) );
1928 fprintf( outfile, "\tpopw %%ds\n" );
1932 /* Push the called routine address */
1934 fprintf( outfile, "\tpushl 12(%%ebx)\n" );
1936 /* Get previous ds from the 16-bit stack and */
1937 /* set ax equal to ds for window procedures. */
1938 fprintf( outfile, "\tmovw -10(%%ebp),%%ax\n" );
1940 fprintf( outfile, "\tdata16\n");
1942 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
1945 /* Jump to the called routine */
1947 fprintf( outfile, "\t.byte 0x66\n" );
1948 fprintf( outfile, "\tlret\n" );
1952 /*******************************************************************
1955 * Build the return code for 16-bit callbacks
1957 static void BuildRet16Func( FILE *outfile )
1959 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_word\n" );
1960 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_long\n" );
1962 /* Put return value into eax */
1964 fprintf( outfile, PREFIX "CALLTO16_Ret_long:\n" );
1965 fprintf( outfile, "\tpushw %%dx\n" );
1966 fprintf( outfile, "\tpushw %%ax\n" );
1967 fprintf( outfile, "\tpopl %%eax\n" );
1968 fprintf( outfile, PREFIX "CALLTO16_Ret_word:\n" );
1970 /* Restore 32-bit segment registers */
1972 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", WINE_DATA_SELECTOR );
1974 fprintf( outfile, "\tdata16\n");
1976 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
1978 fprintf( outfile, "\tdata16\n");
1980 fprintf( outfile, "\tmovw %%bx,%%es\n" );
1982 fprintf( outfile, "\tdata16\n");
1984 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
1986 /* Restore the 32-bit stack */
1988 fprintf( outfile, "\tmovl " PREFIX "IF1632_Saved32_esp,%%esp\n" );
1989 fprintf( outfile, "\tpopl " PREFIX "IF1632_Saved32_esp\n" );
1991 /* Restore the 32-bit registers */
1993 fprintf( outfile, "\tpopl %%edi\n" );
1994 fprintf( outfile, "\tpopl %%esi\n" );
1995 fprintf( outfile, "\tpopl %%edx\n" );
1996 fprintf( outfile, "\tpopl %%ecx\n" );
1997 fprintf( outfile, "\tpopl %%ebx\n" );
1999 /* Return to caller */
2001 fprintf( outfile, "\tpopl %%ebp\n" );
2002 fprintf( outfile, "\tlret\n" );
2004 /* Declare the return address variables */
2006 fprintf( outfile, "\t.data\n" );
2007 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_word\n" );
2008 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_long\n" );
2009 fprintf( outfile, PREFIX "CALLTO16_RetAddr_word:\t.long 0\n" );
2010 fprintf( outfile, PREFIX "CALLTO16_RetAddr_long:\t.long 0\n" );
2011 fprintf( outfile, "\t.text\n" );
2015 /*******************************************************************
2018 * Build the CONTEXT structure on the stack.
2020 static void BuildContext32( FILE *outfile )
2022 /* Build the context structure */
2024 fprintf( outfile, "\tpushw $0\n" );
2025 fprintf( outfile, "\tpushw %%ss\n" );
2026 fprintf( outfile, "\tpushl %%eax\n" ); /* %esp */
2027 fprintf( outfile, "\tpushfl\n" );
2028 fprintf( outfile, "\tpushw $0\n" );
2029 fprintf( outfile, "\tpushw %%cs\n" );
2030 fprintf( outfile, "\tsubl $8,%%esp\n" ); /* %eip + %ebp */
2032 fprintf( outfile, "\tpushl %%eax\n" );
2033 fprintf( outfile, "\tpushl %%ecx\n" );
2034 fprintf( outfile, "\tpushl %%edx\n" );
2035 fprintf( outfile, "\tpushl %%ebx\n" );
2036 fprintf( outfile, "\tpushl %%esi\n" );
2037 fprintf( outfile, "\tpushl %%edi\n" );
2039 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2040 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2041 fprintf( outfile, "\tpushl %%eax\n" );
2042 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2043 fprintf( outfile, "\tpushl %%eax\n" );
2044 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2045 fprintf( outfile, "\tpushl %%eax\n" );
2046 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2047 fprintf( outfile, "\tpushl %%eax\n" );
2049 fprintf( outfile, "\tsubl $%d,%%esp\n",
2050 sizeof(FLOATING_SAVE_AREA) + 6 * sizeof(DWORD) /* DR regs */ );
2051 fprintf( outfile, "\tpushl $0x0001001f\n" ); /* ContextFlags */
2053 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2055 fprintf( outfile, "\tmovl 4(%%ebp),%%eax\n" ); /* %eip at time of call */
2056 fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Eip) );
2057 fprintf( outfile, "\tmovl 0(%%ebp),%%eax\n" ); /* %ebp at time of call */
2058 fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Ebp) );
2059 fprintf( outfile, "\tleal 8(%%ebp),%%eax\n" ); /* %esp at time of call */
2060 fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Esp) );
2062 /* Push pointer to context */
2064 fprintf( outfile, "\tpushl %%esp\n" );
2068 /*******************************************************************
2071 * Restore the registers from the context structure.
2072 * All registers except %cs and %ss are restored.
2074 static void RestoreContext32( FILE *outfile )
2076 /* Restore the context structure */
2078 fprintf( outfile, "\tleal %d(%%ebp),%%esp\n", -sizeof(CONTEXT)-8 );
2079 fprintf( outfile, "\tfrstor %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2081 /* Store flags over the relay addr */
2082 fprintf( outfile, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(EFlags) );
2083 fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", sizeof(CONTEXT) );
2085 /* Get the new stack addr */
2086 fprintf( outfile, "\tmovl %d(%%esp),%%ebx\n", CONTEXTOFFSET(Esp) );
2088 /* Set eip and ebp value onto the new stack */
2089 fprintf( outfile, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Eip) );
2090 fprintf( outfile, "\tmovl %%eax,-4(%%ebx)\n" ); /* %eip at time of call */
2091 fprintf( outfile, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Ebp) );
2092 fprintf( outfile, "\tmovl %%eax,-8(%%ebx)\n" ); /* %ebp at time of call */
2094 /* Set ebp to point to the new stack */
2095 fprintf( outfile, "\tleal -8(%%ebx),%%ebp\n" );
2097 /* Restore all registers */
2098 fprintf( outfile, "\taddl $%d,%%esp\n",
2099 sizeof(FLOATING_SAVE_AREA) + 7 * sizeof(DWORD) );
2100 fprintf( outfile, "\tpopl %%eax\n" );
2101 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2102 fprintf( outfile, "\tpopl %%eax\n" );
2103 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2104 fprintf( outfile, "\tpopl %%eax\n" );
2105 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2106 fprintf( outfile, "\tpopl %%eax\n" );
2107 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2109 fprintf( outfile, "\tpopl %%edi\n" );
2110 fprintf( outfile, "\tpopl %%esi\n" );
2111 fprintf( outfile, "\tpopl %%ebx\n" );
2112 fprintf( outfile, "\tpopl %%edx\n" );
2113 fprintf( outfile, "\tpopl %%ecx\n" );
2114 fprintf( outfile, "\tpopl %%eax\n" );
2116 fprintf( outfile, "\taddl $%d,%%esp\n",
2117 6 * sizeof(DWORD) /* %ebp + %eip + %cs + %efl + %esp + %ss */ );
2118 fprintf( outfile, "\tpopfl\n" );
2122 /*******************************************************************
2123 * BuildCallFrom32Func
2125 * Build a 32-bit-to-Wine call-back function.
2126 * 'args' is the number of dword arguments.
2134 * (ebp-4) entry point
2135 * (ebp-8) relay addr
2137 static void BuildCallFrom32Func( FILE *outfile, const char *profile )
2139 int args, stdcall, reg_func;
2141 if (!strncmp( profile, "stdcall", 7 ))
2145 args = atoi( profile + 8 );
2147 else if (!strncmp( profile, "cdecl", 5 ))
2149 stdcall = reg_func = 0;
2150 args = atoi( profile + 6 );
2152 else if (!strncmp( profile, "regs", 4 ))
2154 stdcall = reg_func = 1;
2155 args = atoi( profile + 5 );
2159 fprintf( stderr, "Invalid function profile '%s', ignored\n", profile );
2163 /* Function header */
2165 fprintf( outfile, "\n\t.align 4\n" );
2167 fprintf( outfile, ".stabs \"CallFrom32_%s:F1\",36,0,0," PREFIX "CallFrom32_%s\n",
2170 fprintf( outfile, "\t.globl " PREFIX "CallFrom32_%s\n", profile );
2171 fprintf( outfile, PREFIX "CallFrom32_%s:\n", profile );
2174 fprintf( outfile, "\tleal 8(%%esp),%%ebp\n" );
2175 fprintf( outfile, "\tpushl $%d\n", /* Nb args */
2176 reg_func ? args | 0x80000000 : args);
2177 fprintf( outfile, "\tpushl %%ebp\n" );
2178 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom32\n" );
2179 fprintf( outfile, "\tadd $8, %%esp\n" );
2180 fprintf( outfile, "\tpopl %%eax\n" );
2181 fprintf( outfile, "\tpopl %%eax\n" );
2182 fprintf( outfile, "\tpopl %%ebp\n" );
2183 fprintf( outfile, "\tjmp %%eax\n" );
2188 fprintf( outfile, "\tleal 8(%%esp),%%ebp\n" );
2190 /* Transfer the arguments */
2192 if (reg_func) BuildContext32( outfile );
2197 for (i = args; i > 0; i--)
2198 fprintf( outfile, "\tpushl %d(%%ebp)\n", 4 * i + 4 );
2202 /* Push the address of the arguments. The called function will */
2203 /* ignore this if it really takes no arguments. */
2204 fprintf( outfile, "\tleal 8(%%ebp),%%eax\n" );
2205 fprintf( outfile, "\tpushl %%eax\n" );
2210 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2211 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2213 /* Print the debugging info */
2217 fprintf( outfile, "\tpushl $%d\n", /* Nb args */
2218 reg_func ? args | 0x80000000 : args);
2219 fprintf( outfile, "\tpushl %%ebp\n" );
2220 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom32\n" );
2221 fprintf( outfile, "\tadd $8, %%esp\n" );
2224 /* Call the function */
2226 fprintf( outfile, "\tcall -4(%%ebp)\n" );
2228 /* Print the debugging info */
2232 fprintf( outfile, "\tpushl %%eax\n" );
2233 fprintf( outfile, "\tpushl $%d\n", /* Nb args */
2234 reg_func ? args | 0x80000000 : args);
2235 fprintf( outfile, "\tpushl %%ebp\n" );
2236 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom32Ret\n" );
2237 fprintf( outfile, "\tpopl %%eax\n" );
2238 fprintf( outfile, "\tpopl %%eax\n" );
2239 fprintf( outfile, "\tpopl %%eax\n" );
2242 if (reg_func) RestoreContext32( outfile );
2244 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2245 fprintf( outfile, "\tpopl %%ebp\n" );
2247 /* Return, removing arguments */
2249 if (args && stdcall) fprintf( outfile, "\tret $%d\n", args * 4 );
2250 else fprintf( outfile, "\tret\n" );
2254 /*******************************************************************
2257 * Build a Wine-to-32-bit callback function.
2259 * Stack frame of the callback function:
2263 * (ebp+8) func to call
2264 * (ebp+4) return address
2265 * (ebp) previous ebp
2267 * Prototype for the CallTo32 functions:
2268 * extern LONG CallTo32_nn( FARPROC32 func, args... );
2270 static void BuildCallTo32Func( FILE *outfile, int args )
2272 /* Function header */
2274 fprintf( outfile, "\n\t.align 4\n" );
2276 fprintf( outfile, ".stabs \"CallTo32_%d:F1\",36,0,0," PREFIX "CallTo32_%d\n",
2279 fprintf( outfile, "\t.globl " PREFIX "CallTo32_%d\n", args );
2280 fprintf( outfile, PREFIX "CallTo32_%d:\n", args );
2284 fprintf( outfile, "\tpushl %%ebp\n" );
2285 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2287 /* Transfer arguments */
2292 for (i = args; i > 0; i--)
2293 fprintf( outfile, "\tpushl %d(%%ebp)\n", 4 * i + 8 );
2296 /* Print the debugging output */
2300 fprintf( outfile, "\tpushl $%d\n", args );
2301 fprintf( outfile, "\tpushl 8(%%ebp)\n" );
2302 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo32\n" );
2303 fprintf( outfile, "\taddl $8,%%esp\n" );
2306 /* Call the function */
2308 fprintf( outfile, "\tcall 8(%%ebp)\n" );
2310 /* Return to Wine */
2312 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2313 fprintf( outfile, "\tpopl %%ebp\n" );
2314 fprintf( outfile, "\tret\n" );
2318 /*******************************************************************
2321 * Build the spec files
2323 static int BuildSpec( FILE *outfile, int argc, char *argv[] )
2326 for (i = 2; i < argc; i++)
2327 if (BuildSpecFile( outfile, argv[i] ) < 0) return -1;
2332 /*******************************************************************
2335 * Build the 16-bit-to-Wine callbacks
2337 static int BuildCallFrom16( FILE *outfile, char * outname, int argc, char *argv[] )
2344 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2345 fprintf( outfile, "\t.text\n" );
2348 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2349 getcwd(buffer, sizeof(buffer));
2352 * The stabs help the internal debugger as they are an indication that it
2353 * is sensible to step into a thunk/trampoline.
2355 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2356 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2357 fprintf( outfile, "\t.text\n" );
2358 fprintf( outfile, "\t.align 4\n" );
2359 fprintf( outfile, "Code_Start:\n\n" );
2362 /* Build the callback functions */
2364 for (i = 2; i < argc; i++) BuildCallFrom16Func( outfile, argv[i] );
2366 /* Output the argument debugging strings */
2370 fprintf( outfile, "/* Argument strings */\n" );
2371 for (i = 2; i < argc; i++)
2373 fprintf( outfile, "Profile_%s:\n", argv[i] );
2374 fprintf( outfile, "\t.ascii \"%s\\0\"\n", argv[i] + 5 );
2379 fprintf( outfile, "\t.text\n");
2380 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2381 fprintf( outfile, ".Letext:\n");
2388 /*******************************************************************
2391 * Build the Wine-to-16-bit callbacks
2393 static int BuildCallTo16( FILE *outfile, char * outname, int argc, char *argv[] )
2400 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2401 fprintf( outfile, "\t.text\n" );
2404 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2405 getcwd(buffer, sizeof(buffer));
2408 * The stabs help the internal debugger as they are an indication that it
2409 * is sensible to step into a thunk/trampoline.
2411 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2412 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2413 fprintf( outfile, "\t.text\n" );
2414 fprintf( outfile, "\t.align 4\n" );
2415 fprintf( outfile, "Code_Start:\n\n" );
2418 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Start\n" );
2419 fprintf( outfile, PREFIX "CALLTO16_Start:\n" );
2421 /* Build the callback functions */
2423 for (i = 2; i < argc; i++) BuildCallTo16Func( outfile, argv[i] );
2425 /* Output the 16-bit return code */
2427 BuildRet16Func( outfile );
2429 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_End\n" );
2430 fprintf( outfile, PREFIX "CALLTO16_End:\n" );
2433 fprintf( outfile, "\t.text\n");
2434 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2435 fprintf( outfile, ".Letext:\n");
2442 /*******************************************************************
2445 * Build the 32-bit-to-Wine callbacks
2447 static int BuildCallFrom32( FILE *outfile, char * outname, int argc, char *argv[] )
2454 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2455 fprintf( outfile, "\t.text\n" );
2458 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2459 getcwd(buffer, sizeof(buffer));
2462 * The stabs help the internal debugger as they are an indication that it
2463 * is sensible to step into a thunk/trampoline.
2465 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2466 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2467 fprintf( outfile, "\t.text\n" );
2468 fprintf( outfile, "\t.align 4\n" );
2469 fprintf( outfile, "Code_Start:\n\n" );
2472 /* Build the callback functions */
2474 for (i = 2; i < argc; i++) BuildCallFrom32Func( outfile, argv[i] );
2477 fprintf( outfile, "\t.text\n");
2478 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2479 fprintf( outfile, ".Letext:\n");
2486 /*******************************************************************
2489 * Build the Wine-to-32-bit callbacks
2491 static int BuildCallTo32( FILE *outfile, char * outname,
2492 int argc, char *argv[] )
2499 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2500 fprintf( outfile, "\t.text\n" );
2503 * Throw in a couple of stabs. The internal debugger doesn't really
2504 * care about trying to step through this crap, but we use the file
2505 * names as an indication that we should just step through it to whatever
2506 * is on the other side.
2509 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2510 getcwd(buffer, sizeof(buffer));
2513 * The stabs help the internal debugger as they are an indication that it
2514 * is sensible to step into a thunk/trampoline.
2516 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2517 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2518 fprintf( outfile, "\t.text\n" );
2519 fprintf( outfile, "\t.align 4\n" );
2520 fprintf( outfile, "Code_Start:\n\n" );
2523 /* Build the 32-bit large stack callback */
2525 BuildCall32LargeStack( outfile );
2527 /* Build the callback functions */
2529 for (i = 2; i < argc; i++) BuildCallTo32Func( outfile, atoi(argv[i]) );
2532 fprintf( outfile, "\t.text\n");
2533 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2534 fprintf( outfile, ".Letext:\n");
2541 /*******************************************************************
2544 static void usage(void)
2546 fprintf(stderr, "usage: build [-o outfile] -spec SPECNAMES\n"
2547 " build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2548 " build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2549 " build [-o outfile] -callfrom32 FUNCTION_PROFILES\n"
2550 " build [-o outfile] -callto32 FUNCTION_PROFILES\n");
2555 /*******************************************************************
2558 int main(int argc, char **argv)
2560 char *outname = NULL;
2561 FILE *outfile = stdout;
2564 if (argc <= 2) usage();
2566 if (!strcmp( argv[1], "-o" ))
2571 if (argc <= 2) usage();
2572 if (!(outfile = fopen( outname, "w" )))
2574 fprintf( stderr, "Unable to create output file '%s'\n", outname );
2579 if (!strcmp( argv[1], "-spec" ))
2580 res = BuildSpec( outfile, argc, argv );
2581 else if (!strcmp( argv[1], "-callfrom16" ))
2582 res = BuildCallFrom16( outfile, outname, argc, argv );
2583 else if (!strcmp( argv[1], "-callto16" ))
2584 res = BuildCallTo16( outfile, outname, argc, argv );
2585 else if (!strcmp( argv[1], "-callfrom32" ))
2586 res = BuildCallFrom32( outfile, outname, argc, argv );
2587 else if (!strcmp( argv[1], "-callto32" ))
2588 res = BuildCallTo32( outfile, outname, argc, argv );