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)(), int nbargs, ... )
1341 static void BuildCall32LargeStack( FILE *outfile
)
1343 /* Function header */
1345 fprintf( outfile
, "\n\t.align 4\n" );
1347 fprintf( outfile
, ".stabs \"CallTo32_LargeStack:F1\",36,0,0," PREFIX
"CallTo32_LargeStack\n");
1349 fprintf( outfile
, "\t.globl " PREFIX
"CallTo32_LargeStack\n" );
1350 fprintf( outfile
, PREFIX
"CallTo32_LargeStack:\n" );
1354 fprintf( outfile
, "\tpushl %%ebp\n" );
1355 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
1357 /* Save registers */
1359 fprintf( outfile
, "\tpushl %%ecx\n" );
1360 fprintf( outfile
, "\tpushl %%esi\n" );
1361 fprintf( outfile
, "\tpushl %%edi\n" );
1363 /* Retrieve the original 32-bit stack pointer and switch to it if any */
1365 fprintf( outfile
, "\tmovl " PREFIX
"IF1632_Original32_esp, %%eax\n" );
1366 fprintf( outfile
, "\torl %%eax,%%eax\n" );
1367 fprintf( outfile
, "\tje no_orig_esp\n" );
1368 fprintf( outfile
, "\tmovl %%eax,%%esp\n" );
1369 fprintf( outfile
, "no_orig_esp:\n" );
1371 /* Transfer the arguments */
1373 fprintf( outfile
, "\tmovl 12(%%ebp),%%ecx\n" );
1374 fprintf( outfile
, "\torl %%ecx,%%ecx\n" );
1375 fprintf( outfile
, "\tje no_args\n" );
1376 fprintf( outfile
, "\tleal 16(%%ebp),%%esi\n" );
1377 fprintf( outfile
, "\tshll $2,%%ecx\n" );
1378 fprintf( outfile
, "\tsubl %%ecx,%%esp\n" );
1379 fprintf( outfile
, "\tmovl %%esp,%%edi\n" );
1380 fprintf( outfile
, "\tshrl $2,%%ecx\n" );
1381 fprintf( outfile
, "\tcld\n" );
1382 fprintf( outfile
, "\trep; movsl\n" );
1383 fprintf( outfile
, "no_args:\n" );
1385 /* Call the function */
1387 fprintf( outfile
, "\tcall 8(%%ebp)\n" );
1389 /* Switch back to the normal stack */
1391 fprintf( outfile
, "\tleal -12(%%ebp),%%esp\n" );
1393 /* Restore registers and return */
1395 fprintf( outfile
, "\tpopl %%edi\n" );
1396 fprintf( outfile
, "\tpopl %%esi\n" );
1397 fprintf( outfile
, "\tpopl %%ecx\n" );
1398 fprintf( outfile
, "\tpopl %%ebp\n" );
1399 fprintf( outfile
, "\tret\n" );
1403 /*******************************************************************
1404 * TransferArgs16To32
1406 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1407 * The 16-bit stack layout is:
1415 static int TransferArgs16To32( FILE *outfile
, char *args
)
1417 int i
, pos16
, pos32
;
1419 /* Save ebx first */
1421 fprintf( outfile
, "\tpushl %%ebx\n" );
1423 /* Get the 32-bit stack pointer */
1425 fprintf( outfile
, "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebx\n" );
1427 /* Copy the arguments */
1429 pos16
= 6; /* skip bp and return address */
1432 for (i
= strlen(args
); i
> 0; i
--)
1437 case 'w': /* word */
1438 fprintf( outfile
, "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1439 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1443 case 's': /* s_word */
1444 fprintf( outfile
, "\tmovswl %d(%%ebp),%%eax\n", pos16
);
1445 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1449 case 'l': /* long or segmented pointer */
1450 case 'T': /* segmented pointer to null-terminated string */
1451 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", pos16
);
1452 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1456 case 'p': /* linear pointer */
1457 case 't': /* linear pointer to null-terminated string */
1458 /* Get the selector */
1459 fprintf( outfile
, "\tmovw %d(%%ebp),%%ax\n", pos16
+ 2 );
1460 /* Get the selector base */
1461 fprintf( outfile
, "\tandl $0xfff8,%%eax\n" );
1462 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy(%%eax),%%eax\n" );
1463 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1464 /* Add the offset */
1465 fprintf( outfile
, "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1466 fprintf( outfile
, "\taddl %%eax,%d(%%ebx)\n", pos32
);
1471 fprintf( stderr
, "Unknown arg type '%c'\n", args
[i
-1] );
1477 fprintf( outfile
, "\tpopl %%ebx\n" );
1479 return pos16
- 6; /* Return the size of the 16-bit args */
1483 /*******************************************************************
1486 * Build the context structure on the 32-bit stack.
1488 static void BuildContext16( FILE *outfile
)
1490 /* Save ebx first */
1492 fprintf( outfile
, "\tpushl %%ebx\n" );
1494 /* Get the 32-bit stack pointer */
1496 fprintf( outfile
, "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebx\n" );
1498 /* Store the registers */
1500 fprintf( outfile
, "\tpopl %d(%%ebx)\n", /* Get ebx from stack*/
1501 CONTEXTOFFSET(Ebx
) - sizeof(CONTEXT
) );
1502 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1503 CONTEXTOFFSET(Eax
) - sizeof(CONTEXT
) );
1504 fprintf( outfile
, "\tmovl %%ecx,%d(%%ebx)\n",
1505 CONTEXTOFFSET(Ecx
) - sizeof(CONTEXT
) );
1506 fprintf( outfile
, "\tmovl %%edx,%d(%%ebx)\n",
1507 CONTEXTOFFSET(Edx
) - sizeof(CONTEXT
) );
1508 fprintf( outfile
, "\tmovl %%esi,%d(%%ebx)\n",
1509 CONTEXTOFFSET(Esi
) - sizeof(CONTEXT
) );
1510 fprintf( outfile
, "\tmovl %%edi,%d(%%ebx)\n",
1511 CONTEXTOFFSET(Edi
) - sizeof(CONTEXT
) );
1513 fprintf( outfile
, "\tmovzwl -10(%%ebp),%%eax\n" ); /* Get %ds from stack*/
1514 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1515 CONTEXTOFFSET(SegDs
) - sizeof(CONTEXT
) );
1516 fprintf( outfile
, "\tmovzwl -6(%%ebp),%%eax\n" ); /* Get %es from stack*/
1517 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1518 CONTEXTOFFSET(SegEs
) - sizeof(CONTEXT
) );
1519 fprintf( outfile
, "\tpushfl\n" );
1520 fprintf( outfile
, "\tpopl %d(%%ebx)\n",
1521 CONTEXTOFFSET(EFlags
) - sizeof(CONTEXT
) );
1522 fprintf( outfile
, "\tmovl -16(%%ebp),%%eax\n" ); /* Get %ebp from stack */
1523 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1524 CONTEXTOFFSET(Ebp
) - sizeof(CONTEXT
) );
1525 fprintf( outfile
, "\tmovzwl 2(%%ebp),%%eax\n" ); /* Get %ip from stack */
1526 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1527 CONTEXTOFFSET(Eip
) - sizeof(CONTEXT
) );
1528 fprintf( outfile
, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1529 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1530 CONTEXTOFFSET(SegCs
) - sizeof(CONTEXT
) );
1531 fprintf( outfile
, "\tmovw %%fs,%%ax\n" );
1532 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1533 CONTEXTOFFSET(SegFs
) - sizeof(CONTEXT
) );
1534 fprintf( outfile
, "\tmovw %%gs,%%ax\n" );
1535 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1536 CONTEXTOFFSET(SegGs
) - sizeof(CONTEXT
) );
1537 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
1538 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1539 CONTEXTOFFSET(SegSs
) - sizeof(CONTEXT
) );
1541 fprintf( outfile
, "\tfsave %d(%%ebx)\n",
1542 CONTEXTOFFSET(FloatSave
) - sizeof(CONTEXT
) );
1547 /*******************************************************************
1550 * Restore the registers from the context structure
1552 static void RestoreContext16( FILE *outfile
)
1554 /* Get the 32-bit stack pointer */
1556 fprintf( outfile
, "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebx\n" );
1558 /* Remove everything up to the return address from the 16-bit stack */
1560 fprintf( outfile
, "\taddl $18,%%esp\n" );
1562 /* Restore the registers */
1564 fprintf( outfile
, "\tmovl %d(%%ebx),%%ecx\n",
1565 CONTEXTOFFSET(Ecx
) - sizeof(CONTEXT
) );
1566 fprintf( outfile
, "\tmovl %d(%%ebx),%%edx\n",
1567 CONTEXTOFFSET(Edx
) - sizeof(CONTEXT
) );
1568 fprintf( outfile
, "\tmovl %d(%%ebx),%%esi\n",
1569 CONTEXTOFFSET(Esi
) - sizeof(CONTEXT
) );
1570 fprintf( outfile
, "\tmovl %d(%%ebx),%%edi\n",
1571 CONTEXTOFFSET(Edi
) - sizeof(CONTEXT
) );
1572 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebp\n",
1573 CONTEXTOFFSET(Ebp
) - sizeof(CONTEXT
) );
1574 fprintf( outfile
, "\tpushw %d(%%ebx)\n", /* Push new ds */
1575 CONTEXTOFFSET(SegDs
) - sizeof(CONTEXT
) );
1576 fprintf( outfile
, "\tpushw %d(%%ebx)\n", /* Push new es */
1577 CONTEXTOFFSET(SegEs
) - sizeof(CONTEXT
) );
1578 fprintf( outfile
, "\tpushl %d(%%ebx)\n",
1579 CONTEXTOFFSET(EFlags
) - sizeof(CONTEXT
) );
1580 fprintf( outfile
, "\tpopfl\n" );
1581 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n",
1582 CONTEXTOFFSET(Eax
) - sizeof(CONTEXT
) );
1583 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebx\n",
1584 CONTEXTOFFSET(Ebx
) - sizeof(CONTEXT
) );
1585 fprintf( outfile
, "\tpopw %%es\n" ); /* Set es */
1586 fprintf( outfile
, "\tpopw %%ds\n" ); /* Set ds */
1590 /*******************************************************************
1591 * BuildCallFrom16Func
1593 * Build a 16-bit-to-Wine callback function. The syntax of the function
1594 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1595 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1596 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1597 * 'T'=segmented pointer to null-terminated string).
1598 * For register functions, the arguments are ignored, but they are still
1599 * removed from the stack upon return.
1601 * Stack layout upon entry to the callback function:
1603 * (sp+18) word first 16-bit arg
1607 * (sp+8) long 32-bit entry point (used to store edx)
1608 * (sp+6) word high word of cs (always 0, used to store es)
1609 * (sp+4) word low word of cs of 16-bit entry point
1610 * (sp+2) word high word of ip (always 0, used to store ds)
1611 * (sp) word low word of ip of 16-bit entry point
1613 * Added on the stack:
1615 * (sp-6) word saved previous sp
1616 * (sp-8) word saved previous ss
1618 static void BuildCallFrom16Func( FILE *outfile
, char *profile
)
1623 char *args
= profile
+ 5;
1625 /* Parse function type */
1627 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1628 else if (!strncmp( "regs_", profile
, 5 )) reg_func
= 1;
1629 else if (strncmp( "long_", profile
, 5 ))
1631 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1635 /* Function header */
1637 fprintf( outfile
, "\n\t.align 4\n" );
1639 fprintf( outfile
, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX
"CallFrom16_%s\n",
1642 fprintf( outfile
, "\t.globl " PREFIX
"CallFrom16_%s\n", profile
);
1643 fprintf( outfile
, PREFIX
"CallFrom16_%s:\n", profile
);
1645 /* Setup bp to point to its copy on the stack */
1647 fprintf( outfile
, "\tpushl %%ebp\n" ); /* Save the full 32-bit ebp */
1648 fprintf( outfile
, "\tmovzwl %%sp,%%ebp\n" );
1649 fprintf( outfile
, "\taddw $16,%%bp\n" );
1651 /* Save 16-bit ds and es */
1653 /* Stupid FreeBSD assembler doesn't know these either */
1654 /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1655 fprintf( outfile
, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1656 /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1657 fprintf( outfile
, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1659 /* Restore 32-bit ds and es */
1661 fprintf( outfile
, "\tpushl $0x%04x%04x\n",
1662 WINE_DATA_SELECTOR
, WINE_DATA_SELECTOR
);
1663 fprintf( outfile
, "\tpopw %%ds\n" );
1664 fprintf( outfile
, "\tpopw %%es\n" );
1667 /* Save the 16-bit stack */
1669 fprintf( outfile
, "\tpushl " PREFIX
"IF1632_Saved16_ss_sp\n" );
1671 fprintf( outfile
,"\tdata16\n");
1673 fprintf( outfile
, "\tmovw %%ss," PREFIX
"IF1632_Saved16_ss_sp+2\n" );
1674 fprintf( outfile
, "\tmovw %%sp," PREFIX
"IF1632_Saved16_ss_sp\n" );
1676 /* Transfer the arguments */
1678 if (reg_func
) BuildContext16( outfile
);
1679 else if (*args
) argsize
= TransferArgs16To32( outfile
, args
);
1681 /* Get the address of the API function */
1683 fprintf( outfile
, "\tmovl -4(%%ebp),%%eax\n" );
1685 /* If necessary, save %edx over the API function address */
1687 if (!reg_func
&& short_ret
)
1688 fprintf( outfile
, "\tmovl %%edx,-4(%%ebp)\n" );
1690 /* Switch to the 32-bit stack */
1692 fprintf( outfile
, "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebp\n" );
1693 fprintf( outfile
, "\tpushw %%ds\n" );
1694 fprintf( outfile
, "\tpopw %%ss\n" );
1695 fprintf( outfile
, "\tleal -%d(%%ebp),%%esp\n",
1696 reg_func
? sizeof(CONTEXT
) : 4 * strlen(args
) );
1697 if (reg_func
) /* Push the address of the context struct */
1698 fprintf( outfile
, "\tpushl %%esp\n" );
1700 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1702 fprintf( outfile
, "\taddl $24,%%ebp\n" );
1704 /* Print the debug information before the call */
1708 fprintf( outfile
, "\tpushl %%eax\n" );
1709 fprintf( outfile
, "\tpushl $Profile_%s\n", profile
);
1710 fprintf( outfile
, "\tpushl $%d\n", reg_func
? 2 : (short_ret
? 1 : 0));
1711 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16\n" );
1712 fprintf( outfile
, "\tpopl %%eax\n" );
1713 fprintf( outfile
, "\tpopl %%eax\n" );
1714 fprintf( outfile
, "\tpopl %%eax\n" );
1717 /* Call the entry point */
1719 fprintf( outfile
, "\tcall %%eax\n" );
1721 /* Print the debug information after the call */
1725 fprintf( outfile
, "\tpushl %%eax\n" );
1726 fprintf( outfile
, "\tpushl $%d\n", reg_func
? 2 : (short_ret
? 1 : 0));
1727 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret\n" );
1728 fprintf( outfile
, "\tpopl %%eax\n" );
1729 fprintf( outfile
, "\tpopl %%eax\n" );
1732 /* Restore the 16-bit stack */
1735 fprintf( outfile
, "\tdata16\n");
1737 fprintf( outfile
, "\tmovw " PREFIX
"IF1632_Saved16_ss_sp+2,%%ss\n" );
1738 fprintf( outfile
, "\tmovw " PREFIX
"IF1632_Saved16_ss_sp,%%sp\n" );
1739 fprintf( outfile
, "\tpopl " PREFIX
"IF1632_Saved16_ss_sp\n" );
1743 /* Calc the arguments size */
1759 fprintf( stderr
, "Unknown arg type '%c'\n", *args
);
1764 /* Restore registers from the context structure */
1765 RestoreContext16( outfile
);
1769 /* Restore high 16 bits of ebp */
1770 fprintf( outfile
, "\tpopl %%ebp\n" );
1772 /* Restore ds and es */
1773 fprintf( outfile
, "\tincl %%esp\n" ); /* Remove ip */
1774 fprintf( outfile
, "\tincl %%esp\n" );
1775 fprintf( outfile
, "\tpopl %%edx\n" ); /* Remove cs and ds */
1776 fprintf( outfile
, "\tmovw %%dx,%%ds\n" ); /* and restore ds */
1777 fprintf( outfile
, "\tpopw %%es\n" ); /* Restore es */
1779 if (short_ret
) fprintf( outfile
, "\tpopl %%edx\n" ); /* Restore edx */
1782 /* Get the return value into dx:ax */
1783 fprintf( outfile
, "\tmovl %%eax,%%edx\n" );
1784 fprintf( outfile
, "\tshrl $16,%%edx\n" );
1785 /* Remove API entry point */
1786 fprintf( outfile
, "\taddl $4,%%esp\n" );
1789 /* Restore low 16 bits of ebp */
1790 fprintf( outfile
, "\tpopw %%bp\n" );
1793 /* Remove the arguments and return */
1797 fprintf( outfile
, "\t.byte 0x66\n" );
1798 fprintf( outfile
, "\tlret $%d\n", argsize
);
1802 fprintf( outfile
, "\t.byte 0x66\n" );
1803 fprintf( outfile
, "\tlret\n" );
1808 /*******************************************************************
1811 * Build a Wine-to-16-bit callback function.
1813 * Stack frame of the callback function:
1817 * (ebp+12) func to call
1818 * (ebp+8) code selector
1819 * (ebp+4) return address
1820 * (ebp) previous ebp
1822 * Prototypes for the CallTo16 functions:
1823 * extern WORD CallTo16_word_xxx( FARPROC16 func, args... );
1824 * extern LONG CallTo16_long_xxx( FARPROC16 func, args... );
1825 * extern void CallTo16_regs_( const CONTEXT *context );
1827 static void BuildCallTo16Func( FILE *outfile
, char *profile
)
1831 char *args
= profile
+ 5;
1833 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1834 else if (!strncmp( "regs_", profile
, 5 )) reg_func
= short_ret
= 1;
1835 else if (strncmp( "long_", profile
, 5 ))
1837 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1841 /* Function header */
1843 fprintf( outfile
, "\n\t.align 4\n" );
1845 fprintf( outfile
, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX
"CallTo16_%s\n",
1848 fprintf( outfile
, "\t.globl " PREFIX
"CallTo16_%s\n", profile
);
1849 fprintf( outfile
, PREFIX
"CallTo16_%s:\n", profile
);
1851 /* Push code selector before return address to simulate a lcall */
1853 fprintf( outfile
, "\tpopl %%eax\n" );
1854 fprintf( outfile
, "\tpushl $0x%04x\n", WINE_CODE_SELECTOR
);
1855 fprintf( outfile
, "\tpushl %%eax\n" );
1859 fprintf( outfile
, "\tpushl %%ebp\n" );
1860 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
1862 /* Save the 32-bit registers */
1864 fprintf( outfile
, "\tpushl %%ebx\n" );
1865 fprintf( outfile
, "\tpushl %%ecx\n" );
1866 fprintf( outfile
, "\tpushl %%edx\n" );
1867 fprintf( outfile
, "\tpushl %%esi\n" );
1868 fprintf( outfile
, "\tpushl %%edi\n" );
1870 /* Save the 32-bit stack */
1872 fprintf( outfile
, "\tpushl " PREFIX
"IF1632_Saved32_esp\n" );
1873 fprintf( outfile
, "\tmovl %%esp," PREFIX
"IF1632_Saved32_esp\n" );
1874 fprintf( outfile
, "\tmovl %%ebp,%%ebx\n" );
1876 /* Print debugging info */
1880 /* Push the address of the first argument */
1881 fprintf( outfile
, "\tleal 12(%%ebx),%%eax\n" );
1882 fprintf( outfile
, "\tpushl $%d\n", reg_func
? -1 : strlen(args
) );
1883 fprintf( outfile
, "\tpushl %%eax\n" );
1884 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16\n" );
1885 fprintf( outfile
, "\tpopl %%eax\n" );
1886 fprintf( outfile
, "\tpopl %%eax\n" );
1889 /* Switch to the 16-bit stack */
1892 fprintf( outfile
,"\tdata16\n");
1894 fprintf( outfile
, "\tmovw " PREFIX
"IF1632_Saved16_ss_sp+2,%%ss\n" );
1895 fprintf( outfile
, "\tmovw " PREFIX
"IF1632_Saved16_ss_sp,%%sp\n" );
1897 /* Transfer the arguments */
1901 /* Get the registers. ebx is handled later on. */
1902 fprintf( outfile
, "\tmovl 12(%%ebx),%%ebx\n" );
1903 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs
) );
1904 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
1905 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp
) );
1906 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax
) );
1907 fprintf( outfile
, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx
) );
1908 fprintf( outfile
, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx
) );
1909 fprintf( outfile
, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi
) );
1910 fprintf( outfile
, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi
) );
1912 else /* not a register function */
1914 int pos
= 16; /* first argument position */
1916 /* Make %bp point to the previous stackframe (built by CallFrom16) */
1917 fprintf( outfile
, "\tmovzwl %%sp,%%ebp\n" );
1918 fprintf( outfile
, "\taddw $20,%%bp\n" );
1924 case 'w': /* word */
1925 fprintf( outfile
, "\tpushw %d(%%ebx)\n", pos
);
1927 case 'l': /* long */
1928 fprintf( outfile
, "\tpushl %d(%%ebx)\n", pos
);
1931 fprintf( stderr
, "Unexpected case '%c' in BuildCallTo16Func\n",
1938 /* Push the return address */
1940 fprintf( outfile
, "\tpushl " PREFIX
"CALLTO16_RetAddr_%s\n",
1941 short_ret
? "word" : "long" );
1945 /* Push the called routine address */
1947 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs
) );
1948 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip
) );
1950 /* Get the 16-bit ds */
1952 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegDs
) );
1953 /* Get ebx from the 32-bit stack */
1954 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(Ebx
) );
1955 fprintf( outfile
, "\tpopw %%ds\n" );
1959 /* Push the called routine address */
1961 fprintf( outfile
, "\tpushl 12(%%ebx)\n" );
1963 /* Get previous ds from the 16-bit stack and */
1964 /* set ax equal to ds for window procedures. */
1965 fprintf( outfile
, "\tmovw -10(%%ebp),%%ax\n" );
1967 fprintf( outfile
, "\tdata16\n");
1969 fprintf( outfile
, "\tmovw %%ax,%%ds\n" );
1972 /* Jump to the called routine */
1974 fprintf( outfile
, "\t.byte 0x66\n" );
1975 fprintf( outfile
, "\tlret\n" );
1979 /*******************************************************************
1982 * Build the return code for 16-bit callbacks
1984 static void BuildRet16Func( FILE *outfile
)
1986 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Ret_word\n" );
1987 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Ret_long\n" );
1989 /* Put return value into eax */
1991 fprintf( outfile
, PREFIX
"CALLTO16_Ret_long:\n" );
1992 fprintf( outfile
, "\tpushw %%dx\n" );
1993 fprintf( outfile
, "\tpushw %%ax\n" );
1994 fprintf( outfile
, "\tpopl %%eax\n" );
1995 fprintf( outfile
, PREFIX
"CALLTO16_Ret_word:\n" );
1997 /* Restore 32-bit segment registers */
1999 fprintf( outfile
, "\tmovw $0x%04x,%%bx\n", WINE_DATA_SELECTOR
);
2001 fprintf( outfile
, "\tdata16\n");
2003 fprintf( outfile
, "\tmovw %%bx,%%ds\n" );
2005 fprintf( outfile
, "\tdata16\n");
2007 fprintf( outfile
, "\tmovw %%bx,%%es\n" );
2009 fprintf( outfile
, "\tdata16\n");
2011 fprintf( outfile
, "\tmovw %%bx,%%ss\n" );
2013 /* Restore the 32-bit stack */
2015 fprintf( outfile
, "\tmovl " PREFIX
"IF1632_Saved32_esp,%%esp\n" );
2016 fprintf( outfile
, "\tpopl " PREFIX
"IF1632_Saved32_esp\n" );
2018 /* Restore the 32-bit registers */
2020 fprintf( outfile
, "\tpopl %%edi\n" );
2021 fprintf( outfile
, "\tpopl %%esi\n" );
2022 fprintf( outfile
, "\tpopl %%edx\n" );
2023 fprintf( outfile
, "\tpopl %%ecx\n" );
2024 fprintf( outfile
, "\tpopl %%ebx\n" );
2026 /* Return to caller */
2028 fprintf( outfile
, "\tpopl %%ebp\n" );
2029 fprintf( outfile
, "\tlret\n" );
2031 /* Declare the return address variables */
2033 fprintf( outfile
, "\t.data\n" );
2034 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_RetAddr_word\n" );
2035 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_RetAddr_long\n" );
2036 fprintf( outfile
, PREFIX
"CALLTO16_RetAddr_word:\t.long 0\n" );
2037 fprintf( outfile
, PREFIX
"CALLTO16_RetAddr_long:\t.long 0\n" );
2038 fprintf( outfile
, "\t.text\n" );
2042 /*******************************************************************
2045 * Build the CONTEXT structure on the stack.
2047 static void BuildContext32( FILE *outfile
)
2049 /* Build the context structure */
2051 fprintf( outfile
, "\tpushw $0\n" );
2052 fprintf( outfile
, "\tpushw %%ss\n" );
2053 fprintf( outfile
, "\tpushl %%eax\n" ); /* %esp */
2054 fprintf( outfile
, "\tpushfl\n" );
2055 fprintf( outfile
, "\tpushw $0\n" );
2056 fprintf( outfile
, "\tpushw %%cs\n" );
2057 fprintf( outfile
, "\tsubl $8,%%esp\n" ); /* %eip + %ebp */
2059 fprintf( outfile
, "\tpushl %%eax\n" );
2060 fprintf( outfile
, "\tpushl %%ecx\n" );
2061 fprintf( outfile
, "\tpushl %%edx\n" );
2062 fprintf( outfile
, "\tpushl %%ebx\n" );
2063 fprintf( outfile
, "\tpushl %%esi\n" );
2064 fprintf( outfile
, "\tpushl %%edi\n" );
2066 fprintf( outfile
, "\txorl %%eax,%%eax\n" );
2067 fprintf( outfile
, "\tmovw %%ds,%%ax\n" );
2068 fprintf( outfile
, "\tpushl %%eax\n" );
2069 fprintf( outfile
, "\tmovw %%es,%%ax\n" );
2070 fprintf( outfile
, "\tpushl %%eax\n" );
2071 fprintf( outfile
, "\tmovw %%fs,%%ax\n" );
2072 fprintf( outfile
, "\tpushl %%eax\n" );
2073 fprintf( outfile
, "\tmovw %%gs,%%ax\n" );
2074 fprintf( outfile
, "\tpushl %%eax\n" );
2076 fprintf( outfile
, "\tsubl $%d,%%esp\n",
2077 sizeof(FLOATING_SAVE_AREA
) + 6 * sizeof(DWORD
) /* DR regs */ );
2078 fprintf( outfile
, "\tpushl $0x0001001f\n" ); /* ContextFlags */
2080 fprintf( outfile
, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave
) );
2082 fprintf( outfile
, "\tmovl 4(%%ebp),%%eax\n" ); /* %eip at time of call */
2083 fprintf( outfile
, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Eip
) );
2084 fprintf( outfile
, "\tmovl 0(%%ebp),%%eax\n" ); /* %ebp at time of call */
2085 fprintf( outfile
, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Ebp
) );
2086 fprintf( outfile
, "\tleal 8(%%ebp),%%eax\n" ); /* %esp at time of call */
2087 fprintf( outfile
, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Esp
) );
2089 /* Push pointer to context */
2091 fprintf( outfile
, "\tpushl %%esp\n" );
2095 /*******************************************************************
2098 * Restore the registers from the context structure.
2099 * All registers except %cs and %ss are restored.
2101 static void RestoreContext32( FILE *outfile
)
2103 /* Restore the context structure */
2105 fprintf( outfile
, "\tleal %d(%%ebp),%%esp\n", -sizeof(CONTEXT
)-8 );
2106 fprintf( outfile
, "\tfrstor %d(%%esp)\n", CONTEXTOFFSET(FloatSave
) );
2108 /* Store flags over the relay addr */
2109 fprintf( outfile
, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(EFlags
) );
2110 fprintf( outfile
, "\tmovl %%eax,%d(%%esp)\n", sizeof(CONTEXT
) );
2112 /* Get the new stack addr */
2113 fprintf( outfile
, "\tmovl %d(%%esp),%%ebx\n", CONTEXTOFFSET(Esp
) );
2115 /* Set eip and ebp value onto the new stack */
2116 fprintf( outfile
, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Eip
) );
2117 fprintf( outfile
, "\tmovl %%eax,-4(%%ebx)\n" ); /* %eip at time of call */
2118 fprintf( outfile
, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Ebp
) );
2119 fprintf( outfile
, "\tmovl %%eax,-8(%%ebx)\n" ); /* %ebp at time of call */
2121 /* Set ebp to point to the new stack */
2122 fprintf( outfile
, "\tleal -8(%%ebx),%%ebp\n" );
2124 /* Restore all registers */
2125 fprintf( outfile
, "\taddl $%d,%%esp\n",
2126 sizeof(FLOATING_SAVE_AREA
) + 7 * sizeof(DWORD
) );
2127 fprintf( outfile
, "\tpopl %%eax\n" );
2128 fprintf( outfile
, "\tmovw %%ax,%%gs\n" );
2129 fprintf( outfile
, "\tpopl %%eax\n" );
2130 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2131 fprintf( outfile
, "\tpopl %%eax\n" );
2132 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2133 fprintf( outfile
, "\tpopl %%eax\n" );
2134 fprintf( outfile
, "\tmovw %%ax,%%ds\n" );
2136 fprintf( outfile
, "\tpopl %%edi\n" );
2137 fprintf( outfile
, "\tpopl %%esi\n" );
2138 fprintf( outfile
, "\tpopl %%ebx\n" );
2139 fprintf( outfile
, "\tpopl %%edx\n" );
2140 fprintf( outfile
, "\tpopl %%ecx\n" );
2141 fprintf( outfile
, "\tpopl %%eax\n" );
2143 fprintf( outfile
, "\taddl $%d,%%esp\n",
2144 6 * sizeof(DWORD
) /* %ebp + %eip + %cs + %efl + %esp + %ss */ );
2145 fprintf( outfile
, "\tpopfl\n" );
2149 /*******************************************************************
2150 * BuildCallFrom32Func
2152 * Build a 32-bit-to-Wine call-back function.
2153 * 'args' is the number of dword arguments.
2161 * (ebp-4) entry point
2162 * (ebp-8) relay addr
2164 static void BuildCallFrom32Func( FILE *outfile
, const char *profile
)
2166 int args
, stdcall, reg_func
;
2168 if (!strncmp( profile
, "stdcall", 7 ))
2172 args
= atoi( profile
+ 8 );
2174 else if (!strncmp( profile
, "cdecl", 5 ))
2176 stdcall = reg_func
= 0;
2177 args
= atoi( profile
+ 6 );
2179 else if (!strncmp( profile
, "regs", 4 ))
2181 stdcall = reg_func
= 1;
2182 args
= atoi( profile
+ 5 );
2186 fprintf( stderr
, "Invalid function profile '%s', ignored\n", profile
);
2190 /* Function header */
2192 fprintf( outfile
, "\n\t.align 4\n" );
2194 fprintf( outfile
, ".stabs \"CallFrom32_%s:F1\",36,0,0," PREFIX
"CallFrom32_%s\n",
2197 fprintf( outfile
, "\t.globl " PREFIX
"CallFrom32_%s\n", profile
);
2198 fprintf( outfile
, PREFIX
"CallFrom32_%s:\n", profile
);
2202 fprintf( outfile
, "\tleal 8(%%esp),%%ebp\n" );
2204 /* Transfer the arguments */
2206 if (reg_func
) BuildContext32( outfile
);
2211 for (i
= args
; i
> 0; i
--)
2212 fprintf( outfile
, "\tpushl %d(%%ebp)\n", 4 * i
+ 4 );
2216 /* Push the address of the arguments. The called function will */
2217 /* ignore this if it really takes no arguments. */
2218 fprintf( outfile
, "\tleal 8(%%ebp),%%eax\n" );
2219 fprintf( outfile
, "\tpushl %%eax\n" );
2224 fprintf( outfile
, "\tmovw %%ds,%%ax\n" );
2225 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2227 /* Print the debugging info */
2231 fprintf( outfile
, "\tpushl $%d\n", /* Nb args */
2232 reg_func
? args
| 0x80000000 : args
);
2233 fprintf( outfile
, "\tpushl %%ebp\n" );
2234 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom32\n" );
2235 fprintf( outfile
, "\tadd $8, %%esp\n" );
2238 /* Call the function */
2240 fprintf( outfile
, "\tcall -4(%%ebp)\n" );
2242 /* Print the debugging info */
2246 fprintf( outfile
, "\tpushl %%eax\n" );
2247 fprintf( outfile
, "\tpushl $%d\n", /* Nb args */
2248 reg_func
? args
| 0x80000000 : args
);
2249 fprintf( outfile
, "\tpushl %%ebp\n" );
2250 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom32Ret\n" );
2251 fprintf( outfile
, "\tpopl %%eax\n" );
2252 fprintf( outfile
, "\tpopl %%eax\n" );
2253 fprintf( outfile
, "\tpopl %%eax\n" );
2256 if (reg_func
) RestoreContext32( outfile
);
2258 fprintf( outfile
, "\tmovl %%ebp,%%esp\n" );
2259 fprintf( outfile
, "\tpopl %%ebp\n" );
2261 /* Return, removing arguments */
2263 if (args
&& stdcall) fprintf( outfile
, "\tret $%d\n", args
* 4 );
2264 else fprintf( outfile
, "\tret\n" );
2268 /*******************************************************************
2271 * Build a Wine-to-32-bit callback function.
2273 * Stack frame of the callback function:
2277 * (ebp+8) func to call
2278 * (ebp+4) return address
2279 * (ebp) previous ebp
2281 * Prototype for the CallTo32 functions:
2282 * extern LONG CallTo32_nn( FARPROC32 func, args... );
2284 static void BuildCallTo32Func( FILE *outfile
, int args
)
2286 /* Function header */
2288 fprintf( outfile
, "\n\t.align 4\n" );
2290 fprintf( outfile
, ".stabs \"CallTo32_%d:F1\",36,0,0," PREFIX
"CallTo32_%d\n",
2293 fprintf( outfile
, "\t.globl " PREFIX
"CallTo32_%d\n", args
);
2294 fprintf( outfile
, PREFIX
"CallTo32_%d:\n", args
);
2298 fprintf( outfile
, "\tpushl %%ebp\n" );
2299 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2301 /* Transfer arguments */
2306 for (i
= args
; i
> 0; i
--)
2307 fprintf( outfile
, "\tpushl %d(%%ebp)\n", 4 * i
+ 8 );
2310 /* Print the debugging output */
2314 fprintf( outfile
, "\tpushl $%d\n", args
);
2315 fprintf( outfile
, "\tpushl 8(%%ebp)\n" );
2316 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo32\n" );
2317 fprintf( outfile
, "\taddl $8,%%esp\n" );
2320 /* Call the function */
2322 fprintf( outfile
, "\tcall 8(%%ebp)\n" );
2324 /* Return to Wine */
2326 fprintf( outfile
, "\tmovl %%ebp,%%esp\n" );
2327 fprintf( outfile
, "\tpopl %%ebp\n" );
2328 fprintf( outfile
, "\tret\n" );
2332 /*******************************************************************
2335 * Build the spec files
2337 static int BuildSpec( FILE *outfile
, int argc
, char *argv
[] )
2340 for (i
= 2; i
< argc
; i
++)
2341 if (BuildSpecFile( outfile
, argv
[i
] ) < 0) return -1;
2346 /*******************************************************************
2349 * Build the 16-bit-to-Wine callbacks
2351 static int BuildCallFrom16( FILE *outfile
, char * outname
, int argc
, char *argv
[] )
2358 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2359 fprintf( outfile
, "\t.text\n" );
2362 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2363 getcwd(buffer
, sizeof(buffer
));
2366 * The stabs help the internal debugger as they are an indication that it
2367 * is sensible to step into a thunk/trampoline.
2369 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2370 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2371 fprintf( outfile
, "\t.text\n" );
2372 fprintf( outfile
, "\t.align 4\n" );
2373 fprintf( outfile
, "Code_Start:\n\n" );
2376 /* Build the 32-bit large stack callback */
2378 BuildCall32LargeStack( outfile
);
2380 /* Build the callback functions */
2382 for (i
= 2; i
< argc
; i
++) BuildCallFrom16Func( outfile
, argv
[i
] );
2384 /* Output the argument debugging strings */
2388 fprintf( outfile
, "/* Argument strings */\n" );
2389 for (i
= 2; i
< argc
; i
++)
2391 fprintf( outfile
, "Profile_%s:\n", argv
[i
] );
2392 fprintf( outfile
, "\t.ascii \"%s\\0\"\n", argv
[i
] + 5 );
2397 fprintf( outfile
, "\t.text\n");
2398 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2399 fprintf( outfile
, ".Letext:\n");
2406 /*******************************************************************
2409 * Build the Wine-to-16-bit callbacks
2411 static int BuildCallTo16( FILE *outfile
, char * outname
, int argc
, char *argv
[] )
2418 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2419 fprintf( outfile
, "\t.text\n" );
2422 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2423 getcwd(buffer
, sizeof(buffer
));
2426 * The stabs help the internal debugger as they are an indication that it
2427 * is sensible to step into a thunk/trampoline.
2429 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2430 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2431 fprintf( outfile
, "\t.text\n" );
2432 fprintf( outfile
, "\t.align 4\n" );
2433 fprintf( outfile
, "Code_Start:\n\n" );
2436 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Start\n" );
2437 fprintf( outfile
, PREFIX
"CALLTO16_Start:\n" );
2439 /* Build the callback functions */
2441 for (i
= 2; i
< argc
; i
++) BuildCallTo16Func( outfile
, argv
[i
] );
2443 /* Output the 16-bit return code */
2445 BuildRet16Func( outfile
);
2447 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_End\n" );
2448 fprintf( outfile
, PREFIX
"CALLTO16_End:\n" );
2451 fprintf( outfile
, "\t.text\n");
2452 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2453 fprintf( outfile
, ".Letext:\n");
2460 /*******************************************************************
2463 * Build the 32-bit-to-Wine callbacks
2465 static int BuildCallFrom32( FILE *outfile
, char * outname
, int argc
, char *argv
[] )
2472 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2473 fprintf( outfile
, "\t.text\n" );
2476 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2477 getcwd(buffer
, sizeof(buffer
));
2480 * The stabs help the internal debugger as they are an indication that it
2481 * is sensible to step into a thunk/trampoline.
2483 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2484 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2485 fprintf( outfile
, "\t.text\n" );
2486 fprintf( outfile
, "\t.align 4\n" );
2487 fprintf( outfile
, "Code_Start:\n\n" );
2490 /* Build the callback functions */
2492 for (i
= 2; i
< argc
; i
++) BuildCallFrom32Func( outfile
, argv
[i
] );
2495 fprintf( outfile
, "\t.text\n");
2496 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2497 fprintf( outfile
, ".Letext:\n");
2504 /*******************************************************************
2507 * Build the Wine-to-32-bit callbacks
2509 static int BuildCallTo32( FILE *outfile
, char * outname
,
2510 int argc
, char *argv
[] )
2517 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2518 fprintf( outfile
, "\t.text\n" );
2521 * Throw in a couple of stabs. The internal debugger doesn't really
2522 * care about trying to step through this crap, but we use the file
2523 * names as an indication that we should just step through it to whatever
2524 * is on the other side.
2527 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2528 getcwd(buffer
, sizeof(buffer
));
2531 * The stabs help the internal debugger as they are an indication that it
2532 * is sensible to step into a thunk/trampoline.
2534 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2535 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2536 fprintf( outfile
, "\t.text\n" );
2537 fprintf( outfile
, "\t.align 4\n" );
2538 fprintf( outfile
, "Code_Start:\n\n" );
2541 /* Build the callback functions */
2543 for (i
= 2; i
< argc
; i
++) BuildCallTo32Func( outfile
, atoi(argv
[i
]) );
2546 fprintf( outfile
, "\t.text\n");
2547 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2548 fprintf( outfile
, ".Letext:\n");
2555 /*******************************************************************
2558 static void usage(void)
2560 fprintf(stderr
, "usage: build [-o outfile] -spec SPECNAMES\n"
2561 " build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2562 " build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2563 " build [-o outfile] -callfrom32 FUNCTION_PROFILES\n"
2564 " build [-o outfile] -callto32 FUNCTION_PROFILES\n");
2569 /*******************************************************************
2572 int main(int argc
, char **argv
)
2574 char *outname
= NULL
;
2575 FILE *outfile
= stdout
;
2578 if (argc
<= 2) usage();
2580 if (!strcmp( argv
[1], "-o" ))
2585 if (argc
<= 2) usage();
2586 if (!(outfile
= fopen( outname
, "w" )))
2588 fprintf( stderr
, "Unable to create output file '%s'\n", outname
);
2593 if (!strcmp( argv
[1], "-spec" ))
2594 res
= BuildSpec( outfile
, argc
, argv
);
2595 else if (!strcmp( argv
[1], "-callfrom16" ))
2596 res
= BuildCallFrom16( outfile
, outname
, argc
, argv
);
2597 else if (!strcmp( argv
[1], "-callto16" ))
2598 res
= BuildCallTo16( outfile
, outname
, argc
, argv
);
2599 else if (!strcmp( argv
[1], "-callfrom32" ))
2600 res
= BuildCallFrom32( outfile
, outname
, argc
, argv
);
2601 else if (!strcmp( argv
[1], "-callto32" ))
2602 res
= BuildCallTo32( outfile
, outname
, argc
, argv
);