2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Martin von Loewis
4 * Copyright 1995, 1996 Alexandre Julliard
5 * Copyright 1997 Eric Youngdale
19 #include "selectors.h"
20 #include "stackframe.h"
22 #ifdef NEED_UNDERSCORE_PREFIX
28 #if defined(__GNUC__) && !defined(__svr4__)
37 TYPE_BYTE, /* byte variable (Win16) */
38 TYPE_WORD, /* word variable (Win16) */
39 TYPE_LONG, /* long variable (Win16) */
40 TYPE_PASCAL_16, /* pascal function with 16-bit return (Win16) */
41 TYPE_PASCAL, /* pascal function with 32-bit return (Win16) */
42 TYPE_ABS, /* absolute value (Win16) */
43 TYPE_RETURN, /* simple return value function (Win16) */
44 TYPE_REGISTER, /* register function */
45 TYPE_STUB, /* unimplemented stub */
46 TYPE_STDCALL, /* stdcall function (Win32) */
47 TYPE_CDECL, /* cdecl function (Win32) */
48 TYPE_VARARGS, /* varargs function (Win32) */
49 TYPE_EXTERN, /* external symbol (Win32) */
53 static const char * const TypeNames[TYPE_NBTYPES] =
56 "byte", /* TYPE_BYTE */
57 "word", /* TYPE_WORD */
58 "long", /* TYPE_LONG */
59 "pascal16", /* TYPE_PASCAL_16 */
60 "pascal", /* TYPE_PASCAL */
61 "equate", /* TYPE_ABS */
62 "return", /* TYPE_RETURN */
63 "register", /* TYPE_REGISTER */
64 "stub", /* TYPE_STUB */
65 "stdcall", /* TYPE_STDCALL */
66 "cdecl", /* TYPE_CDECL */
67 "varargs", /* TYPE_VARARGS */
68 "extern" /* TYPE_EXTERN */
71 #define MAX_ORDINALS 1299
73 /* Callback function used for stub functions */
74 #define STUB_CALLBACK \
75 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
135 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
137 static SPEC_TYPE SpecType = SPEC_INVALID;
138 static char DLLName[80];
139 static char DLLFileName[80];
141 int Base = MAX_ORDINALS;
146 char *ParseBuffer = NULL;
151 static int debugging = 1;
153 /* Offset of a structure field relative to the start of the struct */
154 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
156 /* Offset of register relative to the start of the CONTEXT struct */
157 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT,reg)
159 static void *xmalloc (size_t size)
163 res = malloc (size ? size : 1);
166 fprintf (stderr, "Virtual memory exhausted.\n");
173 static void *xrealloc (void *ptr, size_t size)
175 void *res = realloc (ptr, size);
178 fprintf (stderr, "Virtual memory exhausted.\n");
185 static int IsNumberString(char *s)
194 static char *strupper(char *s)
198 for(p = s; *p != '\0'; p++)
204 static char * GetTokenInLine(void)
209 if (ParseNext != ParseBuffer)
211 if (ParseSaveChar == '\0')
213 *ParseNext = ParseSaveChar;
217 * Remove initial white space.
219 for (p = ParseNext; isspace(*p); p++)
222 if ((*p == '\0') || (*p == '#'))
229 if (*token != '(' && *token != ')')
230 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
240 static char * GetToken(void)
244 if (ParseBuffer == NULL)
246 ParseBuffer = xmalloc(512);
247 ParseNext = ParseBuffer;
251 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
253 if (ParseBuffer[0] != '#')
258 while ((token = GetTokenInLine()) == NULL)
260 ParseNext = ParseBuffer;
264 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
266 if (ParseBuffer[0] != '#')
275 /*******************************************************************
278 * Parse a variable definition.
280 static int ParseVariable( ORDDEF *odp )
285 int value_array_size;
287 char *token = GetToken();
290 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
291 SpecName, Line, token);
296 value_array_size = 25;
297 value_array = xmalloc(sizeof(*value_array) * value_array_size);
299 while ((token = GetToken()) != NULL)
304 value_array[n_values++] = strtol(token, &endptr, 0);
305 if (n_values == value_array_size)
307 value_array_size += 25;
308 value_array = xrealloc(value_array,
309 sizeof(*value_array) * value_array_size);
312 if (endptr == NULL || *endptr != '\0')
314 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
315 SpecName, Line, token);
322 fprintf(stderr, "%s:%d: End of file in variable declaration\n",
327 odp->u.var.n_values = n_values;
328 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
334 /*******************************************************************
335 * ParseExportFunction
337 * Parse a function definition.
339 static int ParseExportFunction( ORDDEF *odp )
347 if (odp->type == TYPE_STDCALL)
349 fprintf( stderr, "%s:%d: 'stdcall' not supported for Win16\n",
353 if (odp->type == TYPE_CDECL)
355 fprintf( stderr, "%s:%d: 'cdecl' not supported for Win16\n",
361 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
363 fprintf( stderr, "%s:%d: 'pascal' not supported for Win32\n",
375 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
376 SpecName, Line, token);
380 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
386 if (!strcmp(token, "word"))
387 odp->u.func.arg_types[i] = 'w';
388 else if (!strcmp(token, "s_word"))
389 odp->u.func.arg_types[i] = 's';
390 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
391 odp->u.func.arg_types[i] = 'l';
392 else if (!strcmp(token, "ptr"))
393 odp->u.func.arg_types[i] = 'p';
394 else if (!strcmp(token, "str"))
395 odp->u.func.arg_types[i] = 't';
396 else if (!strcmp(token, "segstr"))
397 odp->u.func.arg_types[i] = 'T';
398 else if (!strcmp(token, "double"))
400 odp->u.func.arg_types[i++] = 'l';
401 odp->u.func.arg_types[i] = 'l';
405 fprintf(stderr, "%s:%d: Unknown variable type '%s'\n",
406 SpecName, Line, token);
409 if (SpecType == SPEC_WIN32)
411 if (strcmp(token, "long") &&
412 strcmp(token, "ptr") &&
413 strcmp(token, "double"))
415 fprintf( stderr, "%s:%d: Type '%s' not supported for Win32\n",
416 SpecName, Line, token );
421 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
423 fprintf( stderr, "%s:%d: Too many arguments\n", SpecName, Line );
426 odp->u.func.arg_types[i] = '\0';
427 if ((odp->type == TYPE_STDCALL) && !i)
428 odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
429 if ((odp->type == TYPE_REGISTER) && (SpecType == SPEC_WIN32) && i)
431 fprintf( stderr, "%s:%d: register functions cannot have arguments in Win32\n",
435 strcpy(odp->u.func.link_name, GetToken());
440 /*******************************************************************
443 * Parse an 'equate' definition.
445 static int ParseEquate( ORDDEF *odp )
449 char *token = GetToken();
450 int value = strtol(token, &endptr, 0);
451 if (endptr == NULL || *endptr != '\0')
453 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
454 SpecName, Line, token);
458 if (SpecType == SPEC_WIN32)
460 fprintf( stderr, "%s:%d: 'equate' not supported for Win32\n",
465 odp->u.abs.value = value;
470 /*******************************************************************
473 * Parse a 'return' definition.
475 static int ParseReturn( ORDDEF *odp )
481 odp->u.ret.arg_size = strtol(token, &endptr, 0);
482 if (endptr == NULL || *endptr != '\0')
484 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
485 SpecName, Line, token);
490 odp->u.ret.ret_value = strtol(token, &endptr, 0);
491 if (endptr == NULL || *endptr != '\0')
493 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
494 SpecName, Line, token);
498 if (SpecType == SPEC_WIN32)
500 fprintf( stderr, "%s:%d: 'return' not supported for Win32\n",
509 /*******************************************************************
512 * Parse a 'stub' definition.
514 static int ParseStub( ORDDEF *odp )
516 odp->u.func.arg_types[0] = '\0';
517 strcpy( odp->u.func.link_name, STUB_CALLBACK );
522 /*******************************************************************
525 * Parse an 'varargs' definition.
527 static int ParseVarargs( ORDDEF *odp )
531 if (SpecType == SPEC_WIN16)
533 fprintf( stderr, "%s:%d: 'varargs' not supported for Win16\n",
541 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
542 SpecName, Line, token);
548 fprintf(stderr, "%s:%d: Expected ')' got '%s'\n",
549 SpecName, Line, token);
553 strcpy( odp->u.vargs.link_name, GetToken() );
558 /*******************************************************************
561 * Parse an 'extern' definition.
563 static int ParseExtern( ORDDEF *odp )
565 if (SpecType == SPEC_WIN16)
567 fprintf( stderr, "%s:%d: 'extern' not supported for Win16\n",
571 strcpy( odp->u.ext.link_name, GetToken() );
576 /*******************************************************************
579 * Parse an ordinal definition.
581 static int ParseOrdinal(int ordinal)
586 if (ordinal >= MAX_ORDINALS)
588 fprintf(stderr, "%s:%d: Ordinal number too large\n", SpecName, Line );
591 if (ordinal > Limit) Limit = ordinal;
592 if (ordinal < Base) Base = ordinal;
594 odp = &OrdinalDefinitions[ordinal];
595 if (!(token = GetToken()))
597 fprintf(stderr, "%s:%d: Expected type after ordinal\n", SpecName, Line);
601 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
602 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
605 if (odp->type >= TYPE_NBTYPES)
608 "%s:%d: Expected type after ordinal, found '%s' instead\n",
609 SpecName, Line, token );
613 if (!(token = GetToken()))
615 fprintf( stderr, "%s:%d: Expected name after type\n", SpecName, Line );
618 strcpy( odp->name, token );
626 return ParseVariable( odp );
632 return ParseExportFunction( odp );
634 return ParseEquate( odp );
636 return ParseReturn( odp );
638 return ParseStub( odp );
640 return ParseVarargs( odp );
642 return ParseExtern( odp );
644 fprintf( stderr, "Should not happen\n" );
650 /*******************************************************************
655 static int ParseTopLevel(void)
659 while ((token = GetToken()) != NULL)
661 if (strcmp(token, "name") == 0)
663 strcpy(DLLName, GetToken());
665 if (!DLLFileName[0]) sprintf( DLLFileName, "%s.DLL", DLLName );
667 else if (strcmp(token, "file") == 0)
669 strcpy(DLLFileName, GetToken());
670 strupper(DLLFileName);
672 else if (strcmp(token, "type") == 0)
675 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
676 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
679 fprintf(stderr, "%s:%d: Type must be 'win16' or 'win32'\n",
684 else if (strcmp(token, "heap") == 0)
687 if (!IsNumberString(token))
689 fprintf(stderr, "%s:%d: Expected number after heap\n",
693 DLLHeapSize = atoi(token);
695 else if (IsNumberString(token))
700 ordinal = atoi(token);
701 if ((rv = ParseOrdinal(ordinal)) < 0)
707 "%s:%d: Expected name, id, length or ordinal\n",
717 /*******************************************************************
720 * Store a list of ints into a byte array.
722 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
729 for (i = 0; i < odp->u.var.n_values; i++)
730 buffer[i] = odp->u.var.values[i];
733 for (i = 0; i < odp->u.var.n_values; i++)
734 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
737 for (i = 0; i < odp->u.var.n_values; i++)
738 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
741 return odp->u.var.n_values * size;
745 /*******************************************************************
748 * Dump a byte stream into the assembly code.
750 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
751 const char *section, const char *label_start )
754 if (section) fprintf( outfile, "\t%s\n", section );
755 if (label_start) fprintf( outfile, "%s:\n", label_start );
756 for (i = 0; i < len; i++)
758 if (!(i & 0x0f)) fprintf( outfile, "\t.byte " );
759 fprintf( outfile, "%d", *data++ );
761 fprintf( outfile, "%c", ((i & 0x0f) != 0x0f) ? ',' : '\n' );
763 fprintf( outfile, "\n" );
767 /*******************************************************************
770 * Build the in-memory representation of a 16-bit NE module, and dump it
771 * as a byte stream into the assembly code.
773 static int BuildModule16( FILE *outfile, int max_code_offset,
774 int max_data_offset )
780 SEGTABLEENTRY *pSegment;
787 * OFSTRUCT File information
788 * SEGTABLEENTRY Segment 1 (code)
789 * SEGTABLEENTRY Segment 2 (data)
790 * WORD[2] Resource table (empty)
791 * BYTE[2] Imported names (empty)
792 * BYTE[n] Resident names table
793 * BYTE[n] Entry table
796 buffer = xmalloc( 0x10000 );
798 pModule = (NE_MODULE *)buffer;
799 pModule->magic = IMAGE_OS2_SIGNATURE;
802 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
804 pModule->heap_size = DLLHeapSize;
805 pModule->stack_size = 0;
810 pModule->seg_count = 2;
811 pModule->modref_count = 0;
812 pModule->nrname_size = 0;
813 pModule->modref_table = 0;
814 pModule->nrname_fpos = 0;
815 pModule->moveable_entries = 0;
816 pModule->alignment = 0;
817 pModule->truetype = 0;
818 pModule->os_flags = NE_OSFLAGS_WINDOWS;
819 pModule->misc_flags = 0;
820 pModule->dlls_to_init = 0;
821 pModule->nrname_handle = 0;
822 pModule->min_swap_area = 0;
823 pModule->expected_version = 0x030a;
824 pModule->pe_module = NULL;
826 pModule->self_loading_sel = 0;
828 /* File information */
830 pFileInfo = (OFSTRUCT *)(pModule + 1);
831 pModule->fileinfo = (int)pFileInfo - (int)pModule;
832 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
833 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
834 + strlen(DLLFileName);
835 strcpy( pFileInfo->szPathName, DLLFileName );
836 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
840 pSegment = (SEGTABLEENTRY *)pstr;
841 pModule->seg_table = (int)pSegment - (int)pModule;
842 pSegment->filepos = 0;
843 pSegment->size = max_code_offset;
845 pSegment->minsize = max_code_offset;
846 pSegment->selector = 0;
849 pModule->dgroup_entry = (int)pSegment - (int)pModule;
850 pSegment->filepos = 0;
851 pSegment->size = max_data_offset;
852 pSegment->flags = NE_SEGFLAGS_DATA;
853 pSegment->minsize = max_data_offset;
854 pSegment->selector = 0;
859 pword = (WORD *)pSegment;
860 pModule->res_table = (int)pword - (int)pModule;
864 /* Imported names table */
866 pstr = (char *)pword;
867 pModule->import_table = (int)pstr - (int)pModule;
871 /* Resident names table */
873 pModule->name_table = (int)pstr - (int)pModule;
874 /* First entry is module name */
875 *pstr = strlen(DLLName );
876 strcpy( pstr + 1, DLLName );
879 pstr += sizeof(WORD);
880 /* Store all ordinals */
881 odp = OrdinalDefinitions + 1;
882 for (i = 1; i <= Limit; i++, odp++)
884 if (!odp->name[0]) continue;
885 *pstr = strlen( odp->name );
886 strcpy( pstr + 1, odp->name );
887 strupper( pstr + 1 );
890 pstr += sizeof(WORD);
896 pModule->entry_table = (int)pstr - (int)pModule;
898 odp = OrdinalDefinitions + 1;
899 for (i = 1; i <= Limit; i++, odp++)
910 selector = 1; /* Code selector */
916 selector = 2; /* Data selector */
920 selector = 0xfe; /* Constant selector */
924 selector = 0; /* Invalid selector */
928 /* create a new bundle if necessary */
929 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
933 bundle[1] = selector;
941 *(WORD *)pstr = odp->offset;
942 pstr += sizeof(WORD);
947 /* Dump the module content */
949 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
950 ".data", "Module_Start" );
951 return (int)pstr - (int)pModule;
955 /*******************************************************************
958 * Build a Win32 assembly file from a spec file.
960 static int BuildSpec32File( char * specfile, FILE *outfile )
963 int i, nb_names, nb_stubs;
965 unsigned char *args, *p;
967 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
968 fprintf( outfile, "\t.file\t\"%s\"\n", specfile );
970 getcwd(buffer, sizeof(buffer));
973 * The stabs help the internal debugger as they are an indication that it
974 * is sensible to step into a thunk/trampoline.
976 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
977 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", specfile);
980 fprintf( outfile, "\t.text\n" );
981 fprintf( outfile, "\t.align 4\n" );
982 fprintf( outfile, "Code_Start:\n" );
984 /* Output code for all register functions */
986 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
988 if (odp->type != TYPE_REGISTER) continue;
989 fprintf( outfile, "\n/* %s.%d (%s) */\n", DLLName, i, odp->name);
990 fprintf( outfile, "\t.align 4\n" );
992 fprintf( outfile, ".stabs \"%s_%d:F1\",36,0,%d,%s_%d\n",
993 DLLName, i, odp->lineno, DLLName, i);
995 fprintf( outfile, "%s_%d:\n", DLLName, i );
997 fprintf( outfile, ".stabn 68,0,%d,0\n", odp->lineno);
999 fprintf( outfile, "\tpushl $" PREFIX "%s\n",odp->u.func.link_name);
1000 fprintf( outfile, "\tjmp " PREFIX "CALL32_Regs\n" );
1003 /* Output code for all stubs functions */
1006 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1008 if (odp->type != TYPE_STUB) continue;
1009 fprintf( outfile, "\n/* %s.%d (%s) */\n", DLLName, i, odp->name);
1011 fprintf( outfile, ".stabs \"%s_%d:F1\",36,0,%d,%s_%d\n",
1012 DLLName, i, odp->lineno, DLLName, i);
1014 fprintf( outfile, "%s_%d:\n", DLLName, i );
1016 fprintf( outfile, ".stabn 68,0,%d,0\n", odp->lineno);
1018 fprintf( outfile, "\tpushl $Name_%d\n", i );
1019 fprintf( outfile, "\tpushl $%d\n", i );
1020 if (++nb_stubs == 1)
1022 fprintf( outfile, "DoStub:\n" );
1023 fprintf( outfile, "\tpushl $DLLName\n" );
1024 fprintf( outfile, "\tcall " PREFIX "RELAY_Unimplemented32\n" );
1026 else fprintf( outfile, "\tjmp DoStub\n" );
1029 /* Output the DLL functions table */
1031 fprintf( outfile, "\t.text\n" );
1032 fprintf( outfile, "\t.align 4\n" );
1033 fprintf( outfile, "Functions:\n" );
1034 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1039 fprintf( outfile, "\t.long 0\n" );
1042 fprintf( outfile, "\t.long " PREFIX "%s\n",odp->u.vargs.link_name);
1045 fprintf( outfile, "\t.long " PREFIX "%s\n", odp->u.ext.link_name );
1049 fprintf( outfile, "\t.long " PREFIX "%s\n", odp->u.func.link_name);
1053 fprintf( outfile, "\t.long %s_%d\n", DLLName, i );
1056 fprintf(stderr,"build: function type %d not available for Win32\n",
1062 /* Output the DLL names table */
1064 fprintf( outfile, "FuncNames:\n" );
1065 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1067 if (odp->type != TYPE_INVALID)
1068 fprintf( outfile, "\t.long Name_%d\n", i );
1071 /* Output the DLL ordinals table */
1073 fprintf( outfile, "FuncOrdinals:\n" );
1075 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1077 if (odp->type == TYPE_INVALID) continue;
1079 /* Some assemblers do not have .word */
1080 fprintf( outfile, "\t.byte %d,%d\n", LOBYTE(i), HIBYTE(i) );
1083 /* Output the DLL names */
1085 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1087 if (odp->type != TYPE_INVALID)
1088 fprintf( outfile, "Name_%d:\t.ascii \"%s\\0\"\n", i, odp->name );
1091 /* Output the DLL functions arguments */
1093 args = p = (unsigned char *)xmalloc( Limit - Base + 1 );
1094 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1099 *p++ = (unsigned char)strlen(odp->u.func.arg_types);
1102 *p++ = 0x80 | (unsigned char)strlen(odp->u.func.arg_types);
1112 DumpBytes( outfile, args, Limit - Base + 1, NULL, "FuncArgs" );
1114 /* Output the DLL descriptor */
1116 fprintf( outfile, "DLLName:\t.ascii \"%s\\0\"\n", DLLName );
1117 fprintf( outfile, "\t.align 4\n" );
1118 fprintf( outfile, "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1119 fprintf( outfile, PREFIX "%s_Descriptor:\n", DLLName );
1120 fprintf( outfile, "\t.long DLLName\n" ); /* Name */
1121 fprintf( outfile, "\t.long %d\n", Base ); /* Base */
1122 fprintf( outfile, "\t.long %d\n", Limit+1-Base ); /* Number of functions */
1123 fprintf( outfile, "\t.long %d\n", nb_names ); /* Number of names */
1124 fprintf( outfile, "\t.long Functions\n" ); /* Functions */
1125 fprintf( outfile, "\t.long FuncNames\n" ); /* Function names */
1126 fprintf( outfile, "\t.long FuncOrdinals\n" ); /* Function ordinals */
1127 fprintf( outfile, "\t.long FuncArgs\n" ); /* Function arguments */
1129 fprintf( outfile, "\t.text\n");
1130 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
1131 fprintf( outfile, ".Letext:\n");
1137 /*******************************************************************
1140 * Build a Win16 assembly file from a spec file.
1142 static int BuildSpec16File( char * specfile, FILE *outfile )
1146 int code_offset, data_offset, module_size;
1147 unsigned char *data;
1149 data = (unsigned char *)xmalloc( 0x10000 );
1150 memset( data, 0, 16 );
1153 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
1154 fprintf( outfile, "\t.text\n" );
1155 fprintf( outfile, "Code_Start:\n" );
1158 odp = OrdinalDefinitions;
1159 for (i = 0; i <= Limit; i++, odp++)
1164 odp->offset = 0xffff;
1168 odp->offset = LOWORD(odp->u.abs.value);
1172 odp->offset = data_offset;
1173 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1177 odp->offset = data_offset;
1178 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1182 odp->offset = data_offset;
1183 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1187 fprintf( outfile,"/* %s.%d */\n", DLLName, i);
1188 fprintf( outfile,"\tmovw $%d,%%ax\n",LOWORD(odp->u.ret.ret_value));
1189 fprintf( outfile,"\tmovw $%d,%%dx\n",HIWORD(odp->u.ret.ret_value));
1190 fprintf( outfile,"\t.byte 0x66\n");
1191 if (odp->u.ret.arg_size != 0)
1192 fprintf( outfile, "\tlret $%d\n\n", odp->u.ret.arg_size);
1195 fprintf( outfile, "\tlret\n");
1196 fprintf( outfile, "\tnop\n");
1197 fprintf( outfile, "\tnop\n\n");
1199 odp->offset = code_offset;
1200 code_offset += 12; /* Assembly code is 12 bytes long */
1205 case TYPE_PASCAL_16:
1207 fprintf( outfile, "/* %s.%d */\n", DLLName, i);
1208 fprintf( outfile, "\tpushw %%bp\n" );
1209 fprintf( outfile, "\tpushl $" PREFIX "%s\n",odp->u.func.link_name);
1210 /* FreeBSD does not understand lcall, so do it the hard way */
1211 fprintf( outfile, "\t.byte 0x9a\n" );
1212 fprintf( outfile, "\t.long " PREFIX "CallFrom16_%s_%s\n",
1213 (odp->type == TYPE_REGISTER) ? "regs" :
1214 (odp->type == TYPE_PASCAL) ? "long" : "word",
1215 odp->u.func.arg_types );
1216 fprintf( outfile,"\t.byte 0x%02x,0x%02x\n", /* Some asms don't have .word */
1217 LOBYTE(WINE_CODE_SELECTOR), HIBYTE(WINE_CODE_SELECTOR) );
1218 fprintf( outfile, "\tnop\n" );
1219 fprintf( outfile, "\tnop\n\n" );
1220 odp->offset = code_offset;
1221 code_offset += 16; /* Assembly code is 16 bytes long */
1225 fprintf(stderr,"build: function type %d not available for Win16\n",
1231 if (!code_offset) /* Make sure the code segment is not empty */
1233 fprintf( outfile, "\t.byte 0\n" );
1237 /* Output data segment */
1239 DumpBytes( outfile, data, data_offset, NULL, "Data_Start" );
1241 /* Build the module */
1243 module_size = BuildModule16( outfile, code_offset, data_offset );
1245 /* Output the DLL descriptor */
1247 fprintf( outfile, "\t.text\n" );
1248 fprintf( outfile, "DLLName:\t.ascii \"%s\\0\"\n", DLLName );
1249 fprintf( outfile, "\t.align 4\n" );
1250 fprintf( outfile, "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1251 fprintf( outfile, PREFIX "%s_Descriptor:\n", DLLName );
1252 fprintf( outfile, "\t.long DLLName\n" ); /* Name */
1253 fprintf( outfile, "\t.long Module_Start\n" ); /* Module start */
1254 fprintf( outfile, "\t.long %d\n", module_size ); /* Module size */
1255 fprintf( outfile, "\t.long Code_Start\n" ); /* Code start */
1256 fprintf( outfile, "\t.long Data_Start\n" ); /* Data start */
1261 /*******************************************************************
1264 * Build an assembly file from a spec file.
1266 static int BuildSpecFile( FILE *outfile, char *specname )
1268 SpecName = specname;
1269 SpecFp = fopen( specname, "r");
1272 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1276 if (ParseTopLevel() < 0) return -1;
1281 return BuildSpec16File( specname, outfile );
1283 return BuildSpec32File( specname, outfile );
1285 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1291 /*******************************************************************
1292 * TransferArgs16To32
1294 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1295 * The 16-bit stack layout is:
1303 static int TransferArgs16To32( FILE *outfile, char *args )
1305 int i, pos16, pos32;
1307 /* Save ebx first */
1309 fprintf( outfile, "\tpushl %%ebx\n" );
1311 /* Get the 32-bit stack pointer */
1313 fprintf( outfile, "\tmovl " PREFIX "CALLTO16_Saved32_esp,%%ebx\n" );
1315 /* Copy the arguments */
1317 pos16 = 6; /* skip bp and return address */
1320 for (i = strlen(args); i > 0; i--)
1325 case 'w': /* word */
1326 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1327 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1331 case 's': /* s_word */
1332 fprintf( outfile, "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1333 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1337 case 'l': /* long or segmented pointer */
1338 case 'T': /* segmented pointer to null-terminated string */
1339 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", pos16 );
1340 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1344 case 'p': /* linear pointer */
1345 case 't': /* linear pointer to null-terminated string */
1346 /* Get the selector */
1347 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1348 /* Get the selector base */
1349 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1350 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1351 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1352 /* Add the offset */
1353 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1354 fprintf( outfile, "\taddl %%eax,%d(%%ebx)\n", pos32 );
1359 fprintf( stderr, "Unknown arg type '%c'\n", args[i-1] );
1365 fprintf( outfile, "\tpopl %%ebx\n" );
1367 return pos16 - 6; /* Return the size of the 16-bit args */
1371 /*******************************************************************
1374 * Build the context structure on the 32-bit stack.
1376 static void BuildContext16( FILE *outfile )
1378 /* Save ebx first */
1380 fprintf( outfile, "\tpushl %%ebx\n" );
1382 /* Get the 32-bit stack pointer */
1384 fprintf( outfile, "\tmovl " PREFIX "CALLTO16_Saved32_esp,%%ebx\n" );
1386 /* Store the registers */
1388 fprintf( outfile, "\tpopl %d(%%ebx)\n", /* Get ebx from stack*/
1389 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1390 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1391 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1392 fprintf( outfile, "\tmovl %%ecx,%d(%%ebx)\n",
1393 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1394 fprintf( outfile, "\tmovl %%edx,%d(%%ebx)\n",
1395 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1396 fprintf( outfile, "\tmovl %%esi,%d(%%ebx)\n",
1397 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1398 fprintf( outfile, "\tmovl %%edi,%d(%%ebx)\n",
1399 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1401 fprintf( outfile, "\tmovzwl -10(%%ebp),%%eax\n" ); /* Get %ds from stack*/
1402 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1403 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1404 fprintf( outfile, "\tmovzwl -6(%%ebp),%%eax\n" ); /* Get %es from stack*/
1405 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1406 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1407 fprintf( outfile, "\tpushfl\n" );
1408 fprintf( outfile, "\tpopl %d(%%ebx)\n",
1409 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1410 fprintf( outfile, "\tmovl -16(%%ebp),%%eax\n" ); /* Get %ebp from stack */
1411 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1412 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1413 fprintf( outfile, "\tmovzwl 2(%%ebp),%%eax\n" ); /* Get %ip from stack */
1414 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1415 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1416 fprintf( outfile, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1417 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1418 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1419 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
1420 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1421 CONTEXTOFFSET(SegFs) - sizeof(CONTEXT) );
1422 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1423 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1424 CONTEXTOFFSET(SegGs) - sizeof(CONTEXT) );
1425 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1426 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1427 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1429 fprintf( outfile, "\tfsave %d(%%ebx)\n",
1430 CONTEXTOFFSET(FloatSave) - sizeof(CONTEXT) );
1435 /*******************************************************************
1438 * Restore the registers from the context structure.
1439 * %edx must point to the 32-bit stack top.
1441 static void RestoreContext16( FILE *outfile )
1443 /* Get the 32-bit stack pointer */
1445 fprintf( outfile, "\tmovl %%edx,%%ebx\n" );
1447 /* Remove everything up to the return address from the 16-bit stack */
1449 fprintf( outfile, "\taddl $22,%%esp\n" );
1451 /* Restore the registers */
1453 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n",
1454 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1455 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n",
1456 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1457 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n",
1458 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1459 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n",
1460 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1461 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n",
1462 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1463 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new cs */
1464 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1465 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new ip */
1466 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1467 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new ds */
1468 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1469 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new es */
1470 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1471 fprintf( outfile, "\tpushl %d(%%ebx)\n",
1472 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1473 fprintf( outfile, "\tpopfl\n" );
1474 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1475 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1476 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n",
1477 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1478 fprintf( outfile, "\tpopw %%es\n" ); /* Set es */
1479 fprintf( outfile, "\tpopw %%ds\n" ); /* Set ds */
1483 /*******************************************************************
1484 * BuildCallFrom16Func
1486 * Build a 16-bit-to-Wine callback function. The syntax of the function
1487 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1488 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1489 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1490 * 'T'=segmented pointer to null-terminated string).
1491 * For register functions, the arguments are ignored, but they are still
1492 * removed from the stack upon return.
1494 * Stack layout upon entry to the callback function:
1496 * (sp+18) word first 16-bit arg
1500 * (sp+8) long 32-bit entry point (used to store edx)
1501 * (sp+6) word high word of cs (always 0, used to store es)
1502 * (sp+4) word low word of cs of 16-bit entry point
1503 * (sp+2) word high word of ip (always 0, used to store ds)
1504 * (sp) word low word of ip of 16-bit entry point
1506 * Added on the stack:
1508 * (sp-6) word saved previous sp
1509 * (sp-8) word saved previous ss
1511 static void BuildCallFrom16Func( FILE *outfile, char *profile )
1516 char *args = profile + 5;
1518 /* Parse function type */
1520 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1521 else if (!strncmp( "regs_", profile, 5 )) reg_func = 1;
1522 else if (strncmp( "long_", profile, 5 ))
1524 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1528 /* Function header */
1530 fprintf( outfile, "\n\t.align 4\n" );
1532 fprintf( outfile, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX "CallFrom16_%s\n",
1535 fprintf( outfile, "\t.globl " PREFIX "CallFrom16_%s\n", profile );
1536 fprintf( outfile, PREFIX "CallFrom16_%s:\n", profile );
1538 /* Setup bp to point to its copy on the stack */
1540 fprintf( outfile, "\tpushl %%ebp\n" ); /* Save the full 32-bit ebp */
1541 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1542 fprintf( outfile, "\taddw $16,%%bp\n" );
1544 /* Save 16-bit ds and es */
1546 /* Stupid FreeBSD assembler doesn't know these either */
1547 /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1548 fprintf( outfile, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1549 /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1550 fprintf( outfile, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1552 /* Restore 32-bit ds and es */
1554 fprintf( outfile, "\tpushl $0x%04x%04x\n",
1555 WINE_DATA_SELECTOR, WINE_DATA_SELECTOR );
1556 fprintf( outfile, "\tpopw %%ds\n" );
1557 fprintf( outfile, "\tpopw %%es\n" );
1560 /* Save the 16-bit stack */
1562 fprintf( outfile, "\tpushl " PREFIX "IF1632_Saved16_ss_sp\n" );
1564 fprintf( outfile,"\tdata16\n");
1566 fprintf( outfile, "\tmovw %%ss," PREFIX "IF1632_Saved16_ss_sp+2\n" );
1567 fprintf( outfile, "\tmovw %%sp," PREFIX "IF1632_Saved16_ss_sp\n" );
1569 /* Transfer the arguments */
1571 if (reg_func) BuildContext16( outfile );
1572 else if (*args) argsize = TransferArgs16To32( outfile, args );
1574 /* Get the address of the API function */
1576 fprintf( outfile, "\tmovl -4(%%ebp),%%eax\n" );
1578 /* If necessary, save %edx over the API function address */
1580 if (!reg_func && short_ret)
1581 fprintf( outfile, "\tmovl %%edx,-4(%%ebp)\n" );
1583 /* Switch to the 32-bit stack */
1585 fprintf( outfile, "\tmovl " PREFIX "CALLTO16_Saved32_esp,%%ebp\n" );
1586 fprintf( outfile, "\tpushw %%ds\n" );
1587 fprintf( outfile, "\tpopw %%ss\n" );
1588 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1589 reg_func ? sizeof(CONTEXT) : 4 * strlen(args) );
1590 if (reg_func) /* Push the address of the context struct */
1591 fprintf( outfile, "\tpushl %%esp\n" );
1593 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1595 fprintf( outfile, "\taddl $%d,%%ebp\n", STRUCTOFFSET(STACK32FRAME,ebp) );
1597 /* Print the debug information before the call */
1601 fprintf( outfile, "\tpushl %%eax\n" );
1602 fprintf( outfile, "\tpushl $Profile_%s\n", profile );
1603 fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1604 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n" );
1605 fprintf( outfile, "\tpopl %%eax\n" );
1606 fprintf( outfile, "\tpopl %%eax\n" );
1607 fprintf( outfile, "\tpopl %%eax\n" );
1610 /* Call the entry point */
1612 fprintf( outfile, "\tcall %%eax\n" );
1614 /* Print the debug information after the call */
1620 /* Push again the address of the context struct in case */
1621 /* it has been removed by an stdcall function */
1622 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1623 sizeof(CONTEXT) + 32 );
1624 fprintf( outfile, "\tpushl %%esp\n" );
1626 fprintf( outfile, "\tpushl %%eax\n" );
1627 fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1628 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n" );
1629 fprintf( outfile, "\tpopl %%eax\n" );
1630 fprintf( outfile, "\tpopl %%eax\n" );
1633 /* Restore the value of the saved 32-bit stack pointer */
1635 fprintf( outfile, "\tleal -%d(%%ebp),%%edx\n",
1636 STRUCTOFFSET(STACK32FRAME,ebp) );
1637 fprintf( outfile, "movl %%edx," PREFIX "CALLTO16_Saved32_esp\n" );
1639 /* Restore the 16-bit stack */
1642 fprintf( outfile, "\tdata16\n");
1644 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp+2,%%ss\n" );
1645 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp,%%sp\n" );
1646 fprintf( outfile, "\tpopl " PREFIX "IF1632_Saved16_ss_sp\n" );
1650 /* Calc the arguments size */
1666 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1671 /* Restore registers from the context structure */
1672 RestoreContext16( outfile );
1676 /* Restore high 16 bits of ebp */
1677 fprintf( outfile, "\tpopl %%ebp\n" );
1679 /* Restore ds and es */
1680 fprintf( outfile, "\tincl %%esp\n" ); /* Remove ip */
1681 fprintf( outfile, "\tincl %%esp\n" );
1682 fprintf( outfile, "\tpopl %%edx\n" ); /* Remove cs and ds */
1683 fprintf( outfile, "\tmovw %%dx,%%ds\n" ); /* and restore ds */
1684 fprintf( outfile, "\tpopw %%es\n" ); /* Restore es */
1686 if (short_ret) fprintf( outfile, "\tpopl %%edx\n" ); /* Restore edx */
1689 /* Get the return value into dx:ax */
1690 fprintf( outfile, "\tmovl %%eax,%%edx\n" );
1691 fprintf( outfile, "\tshrl $16,%%edx\n" );
1692 /* Remove API entry point */
1693 fprintf( outfile, "\taddl $4,%%esp\n" );
1696 /* Restore low 16 bits of ebp */
1697 fprintf( outfile, "\tpopw %%bp\n" );
1700 /* Remove the arguments and return */
1704 fprintf( outfile, "\t.byte 0x66\n" );
1705 fprintf( outfile, "\tlret $%d\n", argsize );
1709 fprintf( outfile, "\t.byte 0x66\n" );
1710 fprintf( outfile, "\tlret\n" );
1715 /*******************************************************************
1718 * Build a Wine-to-16-bit callback function.
1720 * Stack frame of the callback function:
1724 * (ebp+8) func to call
1725 * (ebp+4) return address
1726 * (ebp) previous ebp
1728 * Prototypes for the CallTo16 functions:
1729 * extern WINAPI WORD CallTo16_word_xxx( FARPROC16 func, args... );
1730 * extern WINAPI LONG CallTo16_long_xxx( FARPROC16 func, args... );
1731 * extern WINAPI void CallTo16_regs_( const CONTEXT *context );
1733 static void BuildCallTo16Func( FILE *outfile, char *profile )
1737 char *args = profile + 5;
1739 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1740 else if (!strncmp( "regs_", profile, 5 )) reg_func = 1;
1741 else if (strncmp( "long_", profile, 5 ))
1743 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1747 /* Function header */
1749 fprintf( outfile, "\n\t.align 4\n" );
1751 fprintf( outfile, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX "CallTo16_%s\n",
1754 fprintf( outfile, "\t.globl " PREFIX "CallTo16_%s\n", profile );
1755 fprintf( outfile, PREFIX "CallTo16_%s:\n", profile );
1759 fprintf( outfile, "\tpushl %%ebp\n" );
1760 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
1762 /* Call the actual CallTo16 routine (simulate a lcall) */
1764 fprintf( outfile, "\tpushw $0\n" );
1765 fprintf( outfile, "\tpushw %%cs\n" );
1766 fprintf( outfile, "\tcall do_callto16_%s\n", profile );
1770 /* FIXME: this is a hack because of task.c */
1771 if (!strcmp( profile, "word_" ))
1773 fprintf( outfile, ".globl " PREFIX "CALLTO16_Restore\n" );
1774 fprintf( outfile, PREFIX "CALLTO16_Restore:\n" );
1776 fprintf( outfile, "\tpopl %%ebp\n" );
1777 fprintf( outfile, "\tret $%d\n", strlen(args) + 1 );
1779 /* Start of the actual CallTo16 routine */
1781 /* Save the 32-bit registers */
1783 fprintf( outfile, "do_callto16_%s:\n", profile );
1784 fprintf( outfile, "\tpushl %%ebx\n" );
1785 fprintf( outfile, "\tpushl %%ecx\n" );
1786 fprintf( outfile, "\tpushl %%edx\n" );
1787 fprintf( outfile, "\tpushl %%esi\n" );
1788 fprintf( outfile, "\tpushl %%edi\n" );
1790 /* Save the 32-bit stack */
1792 fprintf( outfile, "\tmovl %%esp," PREFIX "CALLTO16_Saved32_esp\n" );
1793 fprintf( outfile, "\tmovl %%ebp,%%ebx\n" );
1795 /* Print debugging info */
1799 /* Push the address of the first argument */
1800 fprintf( outfile, "\tleal 8(%%ebp),%%eax\n" );
1801 fprintf( outfile, "\tpushl $%d\n", reg_func ? -1 : strlen(args) );
1802 fprintf( outfile, "\tpushl %%eax\n" );
1803 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
1804 fprintf( outfile, "\tpopl %%eax\n" );
1805 fprintf( outfile, "\tpopl %%eax\n" );
1810 /* Switch to the 16-bit stack, saving the current %%esp, */
1811 /* and adding the specified offset to the new sp */
1812 fprintf( outfile, "\tmovzwl " PREFIX "IF1632_Saved16_ss_sp,%%edx\n" );
1813 fprintf( outfile, "\tleal -4(%%edx),%%edx\n" );
1814 fprintf( outfile, "\tmovl 12(%%ebx),%%eax\n" ); /* Get the offset */
1815 fprintf( outfile, "\taddl %%edx,%%eax\n" );
1817 fprintf( outfile,"\tdata16\n");
1819 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp+2,%%ss\n" );
1820 fprintf( outfile, "\txchgl %%esp,%%eax\n" );
1821 fprintf( outfile, "\t.byte 0x36\n" /* %ss: */ );
1822 fprintf( outfile, "\tmovl %%eax,0(%%edx)\n" );
1824 /* Get the registers. ebx is handled later on. */
1826 fprintf( outfile, "\tmovl 8(%%ebx),%%ebx\n" );
1827 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs) );
1828 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1829 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp) );
1830 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax) );
1831 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx) );
1832 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx) );
1833 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi) );
1834 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi) );
1836 /* Push the return address */
1838 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_regs\n" );
1840 /* Push the called routine address */
1842 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
1843 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
1845 /* Get the 16-bit ds */
1847 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
1848 /* Get ebx from the 32-bit stack */
1849 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(Ebx) );
1850 fprintf( outfile, "\tpopw %%ds\n" );
1852 else /* not a register function */
1854 int pos = 12; /* first argument position */
1856 /* Switch to the 16-bit stack, saving the current %%esp */
1857 fprintf( outfile, "\tmovl %%esp,%%eax\n" );
1859 fprintf( outfile,"\tdata16\n");
1861 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp+2,%%ss\n" );
1862 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp,%%sp\n" );
1863 fprintf( outfile, "\tpushl %%eax\n" );
1865 /* Make %bp point to the previous stackframe (built by CallFrom16) */
1866 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1867 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n",
1868 STRUCTOFFSET(STACK16FRAME,bp) + 4 /* for saved %%esp */ );
1870 /* Transfer the arguments */
1876 case 'w': /* word */
1877 fprintf( outfile, "\tpushw %d(%%ebx)\n", pos );
1879 case 'l': /* long */
1880 fprintf( outfile, "\tpushl %d(%%ebx)\n", pos );
1883 fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
1889 /* Push the return address */
1891 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_%s\n",
1892 short_ret ? "word" : "long" );
1894 /* Push the called routine address */
1896 fprintf( outfile, "\tpushl 8(%%ebx)\n" );
1898 /* Set %ds and %es (and %ax just in case) equal to %ss */
1900 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1901 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
1902 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1905 /* Jump to the called routine */
1907 fprintf( outfile, "\t.byte 0x66\n" );
1908 fprintf( outfile, "\tlret\n" );
1912 /*******************************************************************
1915 * Build the return code for 16-bit callbacks
1917 static void BuildRet16Func( FILE *outfile )
1919 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_word\n" );
1920 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_long\n" );
1921 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_regs\n" );
1923 fprintf( outfile, PREFIX "CALLTO16_Ret_word:\n" );
1924 fprintf( outfile, "\txorl %%edx,%%edx\n" );
1926 /* Remove the arguments just in case */
1928 fprintf( outfile, PREFIX "CALLTO16_Ret_long:\n" );
1929 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1930 STRUCTOFFSET(STACK16FRAME,bp) + 4 /* for saved %%esp */ );
1932 /* Put return value into %eax */
1934 fprintf( outfile, PREFIX "CALLTO16_Ret_regs:\n" );
1935 fprintf( outfile, "\tshll $16,%%edx\n" );
1936 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
1937 fprintf( outfile, "\tmovl %%edx,%%eax\n" );
1939 /* Restore 32-bit segment registers and stack*/
1941 fprintf( outfile, "\tpopl %%ecx\n" ); /* Get the saved %%esp */
1942 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", WINE_DATA_SELECTOR );
1944 fprintf( outfile, "\tdata16\n");
1946 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
1948 fprintf( outfile, "\tdata16\n");
1950 fprintf( outfile, "\tmovw %%bx,%%es\n" );
1952 /* Restore the 32-bit stack */
1955 fprintf( outfile, "\tdata16\n");
1957 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
1958 fprintf( outfile, "\tmovl %%ecx,%%esp\n" );
1960 /* Restore the 32-bit registers */
1962 fprintf( outfile, "\tpopl %%edi\n" );
1963 fprintf( outfile, "\tpopl %%esi\n" );
1964 fprintf( outfile, "\tpopl %%edx\n" );
1965 fprintf( outfile, "\tpopl %%ecx\n" );
1966 fprintf( outfile, "\tpopl %%ebx\n" );
1968 /* Return to caller */
1970 fprintf( outfile, "\tlret\n" );
1972 /* Declare the return address variables */
1974 fprintf( outfile, "\t.data\n" );
1975 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_word\n" );
1976 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_long\n" );
1977 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_regs\n" );
1978 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Saved32_esp\n" );
1979 fprintf( outfile, PREFIX "CALLTO16_RetAddr_word:\t.long 0\n" );
1980 fprintf( outfile, PREFIX "CALLTO16_RetAddr_long:\t.long 0\n" );
1981 fprintf( outfile, PREFIX "CALLTO16_RetAddr_regs:\t.long 0\n" );
1982 fprintf( outfile, PREFIX "CALLTO16_Saved32_esp:\t.long 0\n" );
1983 fprintf( outfile, "\t.text\n" );
1987 /*******************************************************************
1988 * BuildCallTo32LargeStack
1990 * Build the function used to switch to the original 32-bit stack
1991 * before calling a 32-bit function from 32-bit code. This is used for
1992 * functions that need a large stack, like X bitmaps functions.
1994 * The generated function has the following prototype:
1995 * int xxx( int (*func)(), void *arg );
1997 * The pointer to the function can be retrieved by calling CALL32_Init,
1998 * which also takes care of saving the current 32-bit stack pointer.
2007 static void BuildCallTo32LargeStack( FILE *outfile )
2009 /* Initialization function */
2011 fprintf( outfile, "\n\t.align 4\n" );
2013 fprintf( outfile, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX "CALL32_Init\n");
2015 fprintf( outfile, "\t.globl " PREFIX "CALL32_Init\n" );
2016 fprintf( outfile, PREFIX "CALL32_Init:\n" );
2017 fprintf( outfile, "\tleal -256(%%esp),%%eax\n" );
2018 fprintf( outfile, "\tmovl %%eax,CALL32_Original32_esp\n" );
2019 fprintf( outfile, "\tmovl $CALL32_LargeStack,%%eax\n" );
2020 fprintf( outfile, "\tret\n" );
2022 /* Function header */
2024 fprintf( outfile, "\n\t.align 4\n" );
2026 fprintf( outfile, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2028 fprintf( outfile, "CALL32_LargeStack:\n" );
2032 fprintf( outfile, "\tpushl %%ebp\n" );
2033 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2035 /* Switch to the original 32-bit stack pointer */
2037 fprintf( outfile, "\tmovl CALL32_Original32_esp, %%esp\n" );
2039 /* Transfer the argument and call the function */
2041 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2042 fprintf( outfile, "\tcall 8(%%ebp)\n" );
2044 /* Restore registers and return */
2046 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2047 fprintf( outfile, "\tpopl %%ebp\n" );
2048 fprintf( outfile, "\tret\n" );
2052 fprintf( outfile, "\t.data\n" );
2053 fprintf( outfile, "CALL32_Original32_esp:\t.long 0\n" );
2054 fprintf( outfile, "\t.text\n" );
2058 /*******************************************************************
2059 * BuildCallFrom32Regs
2061 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2062 * 'args' is the number of dword arguments.
2066 * (esp+208) ret addr (or relay addr when debugging_relay is on)
2067 * (esp+204) entry point
2068 * (esp+0) CONTEXT struct
2070 static void BuildCallFrom32Regs( FILE *outfile )
2072 /* Function header */
2074 fprintf( outfile, "\n\t.align 4\n" );
2076 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
2078 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
2079 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
2081 /* Build the context structure */
2083 fprintf( outfile, "\tpushw $0\n" );
2084 fprintf( outfile, "\tpushw %%ss\n" );
2085 fprintf( outfile, "\tpushl %%eax\n" ); /* %esp place holder */
2086 fprintf( outfile, "\tpushfl\n" );
2087 fprintf( outfile, "\tpushw $0\n" );
2088 fprintf( outfile, "\tpushw %%cs\n" );
2089 fprintf( outfile, "\tpushl 20(%%esp)\n" ); /* %eip at time of call */
2090 fprintf( outfile, "\tpushl %%ebp\n" );
2092 fprintf( outfile, "\tpushl %%eax\n" );
2093 fprintf( outfile, "\tpushl %%ecx\n" );
2094 fprintf( outfile, "\tpushl %%edx\n" );
2095 fprintf( outfile, "\tpushl %%ebx\n" );
2096 fprintf( outfile, "\tpushl %%esi\n" );
2097 fprintf( outfile, "\tpushl %%edi\n" );
2099 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2100 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2101 fprintf( outfile, "\tpushl %%eax\n" );
2102 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2103 fprintf( outfile, "\tpushl %%eax\n" );
2104 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2105 fprintf( outfile, "\tpushl %%eax\n" );
2106 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2107 fprintf( outfile, "\tpushl %%eax\n" );
2109 fprintf( outfile, "\tleal -%d(%%esp),%%esp\n",
2110 sizeof(FLOATING_SAVE_AREA) + 6 * sizeof(DWORD) /* DR regs */ );
2111 fprintf( outfile, "\tpushl $0x0001001f\n" ); /* ContextFlags */
2113 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2115 fprintf( outfile, "\tleal %d(%%esp),%%eax\n",
2116 sizeof(CONTEXT) + 4 ); /* %esp at time of call */
2117 fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Esp) );
2119 fprintf( outfile, "\tcall " PREFIX "RELAY_CallFrom32Regs\n" );
2121 /* Restore the context structure */
2123 fprintf( outfile, "\tfrstor %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2125 /* Store %eip value onto the new stack */
2127 fprintf( outfile, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Eip) );
2128 fprintf( outfile, "\tmovl %d(%%esp),%%ebx\n", CONTEXTOFFSET(Esp) );
2129 fprintf( outfile, "\tmovl %%eax,0(%%ebx)\n" );
2131 /* Restore all registers */
2133 fprintf( outfile, "\tleal %d(%%esp),%%esp\n",
2134 sizeof(FLOATING_SAVE_AREA) + 7 * sizeof(DWORD) );
2135 fprintf( outfile, "\tpopl %%eax\n" );
2136 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2137 fprintf( outfile, "\tpopl %%eax\n" );
2138 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2139 fprintf( outfile, "\tpopl %%eax\n" );
2140 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2141 fprintf( outfile, "\tpopl %%eax\n" );
2142 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2144 fprintf( outfile, "\tpopl %%edi\n" );
2145 fprintf( outfile, "\tpopl %%esi\n" );
2146 fprintf( outfile, "\tpopl %%ebx\n" );
2147 fprintf( outfile, "\tpopl %%edx\n" );
2148 fprintf( outfile, "\tpopl %%ecx\n" );
2149 fprintf( outfile, "\tpopl %%eax\n" );
2150 fprintf( outfile, "\tpopl %%ebp\n" );
2151 fprintf( outfile, "\tleal 8(%%esp),%%esp\n" ); /* skip %eip and %cs */
2152 fprintf( outfile, "\tpopfl\n" );
2153 fprintf( outfile, "\tpopl %%esp\n" );
2154 fprintf( outfile, "\tret\n" );
2158 /*******************************************************************
2161 * Build the spec files
2163 static int BuildSpec( FILE *outfile, int argc, char *argv[] )
2166 for (i = 2; i < argc; i++)
2167 if (BuildSpecFile( outfile, argv[i] ) < 0) return -1;
2172 /*******************************************************************
2175 * Build the 16-bit-to-Wine callbacks
2177 static int BuildCallFrom16( FILE *outfile, char * outname, int argc, char *argv[] )
2184 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2185 fprintf( outfile, "\t.text\n" );
2188 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2189 getcwd(buffer, sizeof(buffer));
2192 * The stabs help the internal debugger as they are an indication that it
2193 * is sensible to step into a thunk/trampoline.
2195 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2196 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2197 fprintf( outfile, "\t.text\n" );
2198 fprintf( outfile, "\t.align 4\n" );
2199 fprintf( outfile, "Code_Start:\n\n" );
2202 /* Build the callback functions */
2204 for (i = 2; i < argc; i++) BuildCallFrom16Func( outfile, argv[i] );
2206 /* Output the argument debugging strings */
2210 fprintf( outfile, "/* Argument strings */\n" );
2211 for (i = 2; i < argc; i++)
2213 fprintf( outfile, "Profile_%s:\n", argv[i] );
2214 fprintf( outfile, "\t.ascii \"%s\\0\"\n", argv[i] + 5 );
2219 fprintf( outfile, "\t.text\n");
2220 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2221 fprintf( outfile, ".Letext:\n");
2228 /*******************************************************************
2231 * Build the Wine-to-16-bit callbacks
2233 static int BuildCallTo16( FILE *outfile, char * outname, int argc, char *argv[] )
2240 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2241 fprintf( outfile, "\t.text\n" );
2244 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2245 getcwd(buffer, sizeof(buffer));
2248 * The stabs help the internal debugger as they are an indication that it
2249 * is sensible to step into a thunk/trampoline.
2251 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2252 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2253 fprintf( outfile, "\t.text\n" );
2254 fprintf( outfile, "\t.align 4\n" );
2255 fprintf( outfile, "Code_Start:\n\n" );
2258 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Start\n" );
2259 fprintf( outfile, PREFIX "CALLTO16_Start:\n" );
2261 /* Build the callback functions */
2263 for (i = 2; i < argc; i++) BuildCallTo16Func( outfile, argv[i] );
2265 /* Output the 16-bit return code */
2267 BuildRet16Func( outfile );
2269 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_End\n" );
2270 fprintf( outfile, PREFIX "CALLTO16_End:\n" );
2273 fprintf( outfile, "\t.text\n");
2274 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2275 fprintf( outfile, ".Letext:\n");
2282 /*******************************************************************
2285 * Build the 32-bit callbacks
2287 static int BuildCall32( FILE *outfile, char * outname )
2293 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2294 fprintf( outfile, "\t.text\n" );
2297 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2298 getcwd(buffer, sizeof(buffer));
2301 * The stabs help the internal debugger as they are an indication that it
2302 * is sensible to step into a thunk/trampoline.
2304 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2305 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2306 fprintf( outfile, "\t.text\n" );
2307 fprintf( outfile, "\t.align 4\n" );
2308 fprintf( outfile, "Code_Start:\n" );
2311 /* Build the 32-bit large stack callback */
2313 BuildCallTo32LargeStack( outfile );
2315 /* Build the register callback function */
2317 BuildCallFrom32Regs( outfile );
2320 fprintf( outfile, "\t.text\n");
2321 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2322 fprintf( outfile, ".Letext:\n");
2329 /*******************************************************************
2332 static void usage(void)
2335 "usage: build [-o outfile] -spec SPECNAMES\n"
2336 " build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2337 " build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2338 " build [-o outfile] -call32\n" );
2343 /*******************************************************************
2346 int main(int argc, char **argv)
2348 char *outname = NULL;
2349 FILE *outfile = stdout;
2352 if (argc < 2) usage();
2354 if (!strcmp( argv[1], "-o" ))
2359 if (argc < 2) usage();
2360 if (!(outfile = fopen( outname, "w" )))
2362 fprintf( stderr, "Unable to create output file '%s'\n", outname );
2367 if (!strcmp( argv[1], "-spec" ))
2368 res = BuildSpec( outfile, argc, argv );
2369 else if (!strcmp( argv[1], "-callfrom16" ))
2370 res = BuildCallFrom16( outfile, outname, argc, argv );
2371 else if (!strcmp( argv[1], "-callto16" ))
2372 res = BuildCallTo16( outfile, outname, argc, argv );
2373 else if (!strcmp( argv[1], "-call32" ))
2374 res = BuildCall32( outfile, outname );