2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Martin von Loewis
4 * Copyright 1995, 1996, 1997 Alexandre Julliard
5 * Copyright 1997 Eric Youngdale
19 #include "selectors.h"
20 #include "stackframe.h"
23 #ifdef NEED_UNDERSCORE_PREFIX
29 #ifdef HAVE_ASM_STRING
30 # define STRING ".string"
32 # define STRING ".ascii"
35 #if defined(__GNUC__) && !defined(__svr4__)
44 TYPE_BYTE
, /* byte variable (Win16) */
45 TYPE_WORD
, /* word variable (Win16) */
46 TYPE_LONG
, /* long variable (Win16) */
47 TYPE_PASCAL_16
, /* pascal function with 16-bit return (Win16) */
48 TYPE_PASCAL
, /* pascal function with 32-bit return (Win16) */
49 TYPE_ABS
, /* absolute value (Win16) */
50 TYPE_RETURN
, /* simple return value function (Win16) */
51 TYPE_REGISTER
, /* register function */
52 TYPE_STUB
, /* unimplemented stub */
53 TYPE_STDCALL
, /* stdcall function (Win32) */
54 TYPE_CDECL
, /* cdecl function (Win32) */
55 TYPE_VARARGS
, /* varargs function (Win32) */
56 TYPE_EXTERN
, /* external symbol (Win32) */
60 static const char * const TypeNames
[TYPE_NBTYPES
] =
63 "byte", /* TYPE_BYTE */
64 "word", /* TYPE_WORD */
65 "long", /* TYPE_LONG */
66 "pascal16", /* TYPE_PASCAL_16 */
67 "pascal", /* TYPE_PASCAL */
68 "equate", /* TYPE_ABS */
69 "return", /* TYPE_RETURN */
70 "register", /* TYPE_REGISTER */
71 "stub", /* TYPE_STUB */
72 "stdcall", /* TYPE_STDCALL */
73 "cdecl", /* TYPE_CDECL */
74 "varargs", /* TYPE_VARARGS */
75 "extern" /* TYPE_EXTERN */
78 #define MAX_ORDINALS 2048
80 /* Callback function used for stub functions */
81 #define STUB_CALLBACK \
82 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
142 static ORDDEF OrdinalDefinitions
[MAX_ORDINALS
];
144 static SPEC_TYPE SpecType
= SPEC_INVALID
;
145 static char DLLName
[80];
146 static char DLLFileName
[80];
147 static int Limit
= 0;
148 static int Base
= MAX_ORDINALS
;
149 static int DLLHeapSize
= 0;
150 static char *SpecName
;
152 static WORD Code_Selector
, Data_Selector
;
154 char *ParseBuffer
= NULL
;
159 static int debugging
= 1;
161 /* Offset of a structure field relative to the start of the struct */
162 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
164 /* Offset of register relative to the start of the CONTEXT struct */
165 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT,reg)
167 /* Offset of the stack pointer relative to %fs:(0) */
168 #define STACKOFFSET (STRUCTOFFSET(THDB,cur_stack) - STRUCTOFFSET(THDB,teb))
171 static void *xmalloc (size_t size
)
175 res
= malloc (size
? size
: 1);
178 fprintf (stderr
, "Virtual memory exhausted.\n");
185 static void *xrealloc (void *ptr
, size_t size
)
187 void *res
= realloc (ptr
, size
);
190 fprintf (stderr
, "Virtual memory exhausted.\n");
197 static int IsNumberString(char *s
)
206 static char *strupper(char *s
)
210 for(p
= s
; *p
!= '\0'; p
++)
216 static char * GetTokenInLine(void)
221 if (ParseNext
!= ParseBuffer
)
223 if (ParseSaveChar
== '\0')
225 *ParseNext
= ParseSaveChar
;
229 * Remove initial white space.
231 for (p
= ParseNext
; isspace(*p
); p
++)
234 if ((*p
== '\0') || (*p
== '#'))
241 if (*token
!= '(' && *token
!= ')')
242 while (*p
!= '\0' && *p
!= '(' && *p
!= ')' && !isspace(*p
))
252 static char * GetToken(void)
256 if (ParseBuffer
== NULL
)
258 ParseBuffer
= xmalloc(512);
259 ParseNext
= ParseBuffer
;
263 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
265 if (ParseBuffer
[0] != '#')
270 while ((token
= GetTokenInLine()) == NULL
)
272 ParseNext
= ParseBuffer
;
276 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
278 if (ParseBuffer
[0] != '#')
287 /*******************************************************************
290 * Parse a variable definition.
292 static int ParseVariable( ORDDEF
*odp
)
297 int value_array_size
;
299 char *token
= GetToken();
302 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
303 SpecName
, Line
, token
);
308 value_array_size
= 25;
309 value_array
= xmalloc(sizeof(*value_array
) * value_array_size
);
311 while ((token
= GetToken()) != NULL
)
316 value_array
[n_values
++] = strtol(token
, &endptr
, 0);
317 if (n_values
== value_array_size
)
319 value_array_size
+= 25;
320 value_array
= xrealloc(value_array
,
321 sizeof(*value_array
) * value_array_size
);
324 if (endptr
== NULL
|| *endptr
!= '\0')
326 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
327 SpecName
, Line
, token
);
334 fprintf(stderr
, "%s:%d: End of file in variable declaration\n",
339 odp
->u
.var
.n_values
= n_values
;
340 odp
->u
.var
.values
= xrealloc(value_array
, sizeof(*value_array
) * n_values
);
346 /*******************************************************************
347 * ParseExportFunction
349 * Parse a function definition.
351 static int ParseExportFunction( ORDDEF
*odp
)
359 if (odp
->type
== TYPE_STDCALL
)
361 fprintf( stderr
, "%s:%d: 'stdcall' not supported for Win16\n",
365 if (odp
->type
== TYPE_CDECL
)
367 fprintf( stderr
, "%s:%d: 'cdecl' not supported for Win16\n",
373 if ((odp
->type
== TYPE_PASCAL
) || (odp
->type
== TYPE_PASCAL_16
))
375 fprintf( stderr
, "%s:%d: 'pascal' not supported for Win32\n",
387 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
388 SpecName
, Line
, token
);
392 for (i
= 0; i
< sizeof(odp
->u
.func
.arg_types
)-1; i
++)
398 if (!strcmp(token
, "word"))
399 odp
->u
.func
.arg_types
[i
] = 'w';
400 else if (!strcmp(token
, "s_word"))
401 odp
->u
.func
.arg_types
[i
] = 's';
402 else if (!strcmp(token
, "long") || !strcmp(token
, "segptr"))
403 odp
->u
.func
.arg_types
[i
] = 'l';
404 else if (!strcmp(token
, "ptr"))
405 odp
->u
.func
.arg_types
[i
] = 'p';
406 else if (!strcmp(token
, "str"))
407 odp
->u
.func
.arg_types
[i
] = 't';
408 else if (!strcmp(token
, "wstr"))
409 odp
->u
.func
.arg_types
[i
] = 'W';
410 else if (!strcmp(token
, "segstr"))
411 odp
->u
.func
.arg_types
[i
] = 'T';
412 else if (!strcmp(token
, "double"))
414 odp
->u
.func
.arg_types
[i
++] = 'l';
415 odp
->u
.func
.arg_types
[i
] = 'l';
419 fprintf(stderr
, "%s:%d: Unknown variable type '%s'\n",
420 SpecName
, Line
, token
);
423 if (SpecType
== SPEC_WIN32
)
425 if (strcmp(token
, "long") &&
426 strcmp(token
, "ptr") &&
427 strcmp(token
, "str") &&
428 strcmp(token
, "wstr") &&
429 strcmp(token
, "double"))
431 fprintf( stderr
, "%s:%d: Type '%s' not supported for Win32\n",
432 SpecName
, Line
, token
);
437 if ((*token
!= ')') || (i
>= sizeof(odp
->u
.func
.arg_types
)))
439 fprintf( stderr
, "%s:%d: Too many arguments\n", SpecName
, Line
);
442 odp
->u
.func
.arg_types
[i
] = '\0';
443 if ((odp
->type
== TYPE_STDCALL
) && !i
)
444 odp
->type
= TYPE_CDECL
; /* stdcall is the same as cdecl for 0 args */
445 if ((odp
->type
== TYPE_REGISTER
) && (SpecType
== SPEC_WIN32
) && i
)
447 fprintf( stderr
, "%s:%d: register functions cannot have arguments in Win32\n",
451 strcpy(odp
->u
.func
.link_name
, GetToken());
456 /*******************************************************************
459 * Parse an 'equate' definition.
461 static int ParseEquate( ORDDEF
*odp
)
465 char *token
= GetToken();
466 int value
= strtol(token
, &endptr
, 0);
467 if (endptr
== NULL
|| *endptr
!= '\0')
469 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
470 SpecName
, Line
, token
);
474 if (SpecType
== SPEC_WIN32
)
476 fprintf( stderr
, "%s:%d: 'equate' not supported for Win32\n",
481 odp
->u
.abs
.value
= value
;
486 /*******************************************************************
489 * Parse a 'return' definition.
491 static int ParseReturn( ORDDEF
*odp
)
497 odp
->u
.ret
.arg_size
= strtol(token
, &endptr
, 0);
498 if (endptr
== NULL
|| *endptr
!= '\0')
500 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
501 SpecName
, Line
, token
);
506 odp
->u
.ret
.ret_value
= strtol(token
, &endptr
, 0);
507 if (endptr
== NULL
|| *endptr
!= '\0')
509 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
510 SpecName
, Line
, token
);
514 if (SpecType
== SPEC_WIN32
)
516 fprintf( stderr
, "%s:%d: 'return' not supported for Win32\n",
525 /*******************************************************************
528 * Parse a 'stub' definition.
530 static int ParseStub( ORDDEF
*odp
)
532 odp
->u
.func
.arg_types
[0] = '\0';
533 strcpy( odp
->u
.func
.link_name
, STUB_CALLBACK
);
538 /*******************************************************************
541 * Parse an 'varargs' definition.
543 static int ParseVarargs( ORDDEF
*odp
)
547 if (SpecType
== SPEC_WIN16
)
549 fprintf( stderr
, "%s:%d: 'varargs' not supported for Win16\n",
557 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
558 SpecName
, Line
, token
);
564 fprintf(stderr
, "%s:%d: Expected ')' got '%s'\n",
565 SpecName
, Line
, token
);
569 strcpy( odp
->u
.vargs
.link_name
, GetToken() );
574 /*******************************************************************
577 * Parse an 'extern' definition.
579 static int ParseExtern( ORDDEF
*odp
)
581 if (SpecType
== SPEC_WIN16
)
583 fprintf( stderr
, "%s:%d: 'extern' not supported for Win16\n",
587 strcpy( odp
->u
.ext
.link_name
, GetToken() );
592 /*******************************************************************
595 * Parse an ordinal definition.
597 static int ParseOrdinal(int ordinal
)
602 if (ordinal
>= MAX_ORDINALS
)
604 fprintf(stderr
, "%s:%d: Ordinal number too large\n", SpecName
, Line
);
607 if (ordinal
> Limit
) Limit
= ordinal
;
608 if (ordinal
< Base
) Base
= ordinal
;
610 odp
= &OrdinalDefinitions
[ordinal
];
611 if (!(token
= GetToken()))
613 fprintf(stderr
, "%s:%d: Expected type after ordinal\n", SpecName
, Line
);
617 for (odp
->type
= 0; odp
->type
< TYPE_NBTYPES
; odp
->type
++)
618 if (TypeNames
[odp
->type
] && !strcmp( token
, TypeNames
[odp
->type
] ))
621 if (odp
->type
>= TYPE_NBTYPES
)
624 "%s:%d: Expected type after ordinal, found '%s' instead\n",
625 SpecName
, Line
, token
);
629 if (!(token
= GetToken()))
631 fprintf( stderr
, "%s:%d: Expected name after type\n", SpecName
, Line
);
634 strcpy( odp
->name
, token
);
642 return ParseVariable( odp
);
648 return ParseExportFunction( odp
);
650 return ParseEquate( odp
);
652 return ParseReturn( odp
);
654 return ParseStub( odp
);
656 return ParseVarargs( odp
);
658 return ParseExtern( odp
);
660 fprintf( stderr
, "Should not happen\n" );
666 /*******************************************************************
671 static int ParseTopLevel(void)
675 while ((token
= GetToken()) != NULL
)
677 if (strcmp(token
, "name") == 0)
679 strcpy(DLLName
, GetToken());
681 if (!DLLFileName
[0]) sprintf( DLLFileName
, "%s.DLL", DLLName
);
683 else if (strcmp(token
, "file") == 0)
685 strcpy(DLLFileName
, GetToken());
686 strupper(DLLFileName
);
688 else if (strcmp(token
, "type") == 0)
691 if (!strcmp(token
, "win16" )) SpecType
= SPEC_WIN16
;
692 else if (!strcmp(token
, "win32" )) SpecType
= SPEC_WIN32
;
695 fprintf(stderr
, "%s:%d: Type must be 'win16' or 'win32'\n",
700 else if (strcmp(token
, "heap") == 0)
703 if (!IsNumberString(token
))
705 fprintf(stderr
, "%s:%d: Expected number after heap\n",
709 DLLHeapSize
= atoi(token
);
711 else if (IsNumberString(token
))
716 ordinal
= atoi(token
);
717 if ((rv
= ParseOrdinal(ordinal
)) < 0)
723 "%s:%d: Expected name, id, length or ordinal\n",
733 /*******************************************************************
736 * Store a list of ints into a byte array.
738 static int StoreVariableCode( unsigned char *buffer
, int size
, ORDDEF
*odp
)
745 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
746 buffer
[i
] = odp
->u
.var
.values
[i
];
749 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
750 ((unsigned short *)buffer
)[i
] = odp
->u
.var
.values
[i
];
753 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
754 ((unsigned int *)buffer
)[i
] = odp
->u
.var
.values
[i
];
757 return odp
->u
.var
.n_values
* size
;
761 /*******************************************************************
764 * Dump a byte stream into the assembly code.
766 static void DumpBytes( FILE *outfile
, const unsigned char *data
, int len
,
767 const char *section
, const char *label_start
)
770 if (section
) fprintf( outfile
, "\t%s\n", section
);
771 if (label_start
) fprintf( outfile
, "%s:\n", label_start
);
772 for (i
= 0; i
< len
; i
++)
774 if (!(i
& 0x0f)) fprintf( outfile
, "\t.byte " );
775 fprintf( outfile
, "%d", *data
++ );
777 fprintf( outfile
, "%c", ((i
& 0x0f) != 0x0f) ? ',' : '\n' );
779 fprintf( outfile
, "\n" );
783 /*******************************************************************
786 * Build the in-memory representation of a 16-bit NE module, and dump it
787 * as a byte stream into the assembly code.
789 static int BuildModule16( FILE *outfile
, int max_code_offset
,
790 int max_data_offset
)
796 SEGTABLEENTRY
*pSegment
;
803 * OFSTRUCT File information
804 * SEGTABLEENTRY Segment 1 (code)
805 * SEGTABLEENTRY Segment 2 (data)
806 * WORD[2] Resource table (empty)
807 * BYTE[2] Imported names (empty)
808 * BYTE[n] Resident names table
809 * BYTE[n] Entry table
812 buffer
= xmalloc( 0x10000 );
814 pModule
= (NE_MODULE
*)buffer
;
815 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
818 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_BUILTIN
| NE_FFLAGS_LIBMODULE
;
820 pModule
->heap_size
= DLLHeapSize
;
821 pModule
->stack_size
= 0;
826 pModule
->seg_count
= 2;
827 pModule
->modref_count
= 0;
828 pModule
->nrname_size
= 0;
829 pModule
->modref_table
= 0;
830 pModule
->nrname_fpos
= 0;
831 pModule
->moveable_entries
= 0;
832 pModule
->alignment
= 0;
833 pModule
->truetype
= 0;
834 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
835 pModule
->misc_flags
= 0;
836 pModule
->dlls_to_init
= 0;
837 pModule
->nrname_handle
= 0;
838 pModule
->min_swap_area
= 0;
839 pModule
->expected_version
= 0x030a;
840 pModule
->module32
= 0;
842 pModule
->self_loading_sel
= 0;
844 /* File information */
846 pFileInfo
= (OFSTRUCT
*)(pModule
+ 1);
847 pModule
->fileinfo
= (int)pFileInfo
- (int)pModule
;
848 memset( pFileInfo
, 0, sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
) );
849 pFileInfo
->cBytes
= sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
)
850 + strlen(DLLFileName
);
851 strcpy( pFileInfo
->szPathName
, DLLFileName
);
852 pstr
= (char *)pFileInfo
+ pFileInfo
->cBytes
+ 1;
856 pSegment
= (SEGTABLEENTRY
*)pstr
;
857 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
858 pSegment
->filepos
= 0;
859 pSegment
->size
= max_code_offset
;
861 pSegment
->minsize
= max_code_offset
;
862 pSegment
->selector
= 0;
865 pModule
->dgroup_entry
= (int)pSegment
- (int)pModule
;
866 pSegment
->filepos
= 0;
867 pSegment
->size
= max_data_offset
;
868 pSegment
->flags
= NE_SEGFLAGS_DATA
;
869 pSegment
->minsize
= max_data_offset
;
870 pSegment
->selector
= 0;
875 pword
= (WORD
*)pSegment
;
876 pModule
->res_table
= (int)pword
- (int)pModule
;
880 /* Imported names table */
882 pstr
= (char *)pword
;
883 pModule
->import_table
= (int)pstr
- (int)pModule
;
887 /* Resident names table */
889 pModule
->name_table
= (int)pstr
- (int)pModule
;
890 /* First entry is module name */
891 *pstr
= strlen(DLLName
);
892 strcpy( pstr
+ 1, DLLName
);
895 pstr
+= sizeof(WORD
);
896 /* Store all ordinals */
897 odp
= OrdinalDefinitions
+ 1;
898 for (i
= 1; i
<= Limit
; i
++, odp
++)
900 if (!odp
->name
[0]) continue;
901 *pstr
= strlen( odp
->name
);
902 strcpy( pstr
+ 1, odp
->name
);
903 strupper( pstr
+ 1 );
906 pstr
+= sizeof(WORD
);
912 pModule
->entry_table
= (int)pstr
- (int)pModule
;
914 odp
= OrdinalDefinitions
+ 1;
915 for (i
= 1; i
<= Limit
; i
++, odp
++)
926 selector
= 1; /* Code selector */
932 selector
= 2; /* Data selector */
936 selector
= 0xfe; /* Constant selector */
940 selector
= 0; /* Invalid selector */
944 /* create a new bundle if necessary */
945 if (!bundle
|| (bundle
[0] >= 254) || (bundle
[1] != selector
))
949 bundle
[1] = selector
;
957 *(WORD
*)pstr
= odp
->offset
;
958 pstr
+= sizeof(WORD
);
963 /* Dump the module content */
965 DumpBytes( outfile
, (char *)pModule
, (int)pstr
- (int)pModule
,
966 ".data", "Module_Start" );
967 return (int)pstr
- (int)pModule
;
971 /*******************************************************************
974 * Build a Win32 C file from a spec file.
976 static int BuildSpec32File( char * specfile
, FILE *outfile
)
979 int i
, nb_names
, nb_reg_funcs
= 0;
981 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
983 fprintf( outfile
, "#include \"builtin32.h\"\n\n" );
985 /* Output code for all stubs functions */
987 fprintf( outfile
, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
989 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
991 if (odp
->type
!= TYPE_STUB
) continue;
992 fprintf( outfile
, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
996 /* Output the DLL functions prototypes */
998 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1003 fprintf( outfile
, "extern void %s();\n", odp
->u
.vargs
.link_name
);
1006 fprintf( outfile
, "extern void %s();\n", odp
->u
.ext
.link_name
);
1011 fprintf( outfile
, "extern void %s();\n", odp
->u
.func
.link_name
);
1017 fprintf(stderr
,"build: function type %d not available for Win32\n",
1023 /* Output the DLL functions table */
1025 fprintf( outfile
, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1027 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1032 fprintf( outfile
, " 0" );
1035 fprintf( outfile
, " %s", odp
->u
.vargs
.link_name
);
1038 fprintf( outfile
, " %s", odp
->u
.ext
.link_name
);
1043 fprintf( outfile
, " %s", odp
->u
.func
.link_name
);
1046 fprintf( outfile
, " __stub_%d", i
);
1051 if (i
< Limit
) fprintf( outfile
, ",\n" );
1053 fprintf( outfile
, "\n};\n\n" );
1055 /* Output the DLL names table */
1058 fprintf( outfile
, "static const char * const FuncNames[] =\n{\n" );
1059 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1061 if (odp
->type
== TYPE_INVALID
) continue;
1062 if (nb_names
++) fprintf( outfile
, ",\n" );
1063 fprintf( outfile
, " \"%s\"", odp
->name
);
1065 fprintf( outfile
, "\n};\n\n" );
1067 /* Output the DLL argument types */
1069 fprintf( outfile
, "static const unsigned int ArgTypes[%d] =\n{\n",
1071 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1073 unsigned int j
, mask
= 0;
1074 if ((odp
->type
== TYPE_STDCALL
) || (odp
->type
== TYPE_CDECL
))
1075 for (j
= 0; odp
->u
.func
.arg_types
[j
]; j
++)
1077 if (odp
->u
.func
.arg_types
[j
] == 't') mask
|= 1<< (j
*2);
1078 if (odp
->u
.func
.arg_types
[j
] == 'W') mask
|= 2<< (j
*2);
1080 fprintf( outfile
, " %d", mask
);
1081 if (i
< Limit
) fprintf( outfile
, ",\n" );
1083 fprintf( outfile
, "\n};\n\n" );
1085 /* Output the DLL ordinals table */
1087 fprintf( outfile
, "static const unsigned short FuncOrdinals[] =\n{\n" );
1089 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1091 if (odp
->type
== TYPE_INVALID
) continue;
1092 if (nb_names
++) fprintf( outfile
, ",\n" );
1093 fprintf( outfile
, " %d", i
- Base
);
1095 fprintf( outfile
, "\n};\n\n" );
1097 /* Output the DLL functions arguments */
1099 fprintf( outfile
, "static const unsigned char FuncArgs[%d] =\n{\n",
1101 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1107 args
= (unsigned char)strlen(odp
->u
.func
.arg_types
);
1110 args
= 0x80 | (unsigned char)strlen(odp
->u
.func
.arg_types
);
1120 fprintf( outfile
, " 0x%02x", args
);
1121 if (i
< Limit
) fprintf( outfile
, ",\n" );
1123 fprintf( outfile
, "\n};\n\n" );
1125 /* Output the DLL descriptor */
1127 fprintf( outfile
, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1129 fprintf( outfile
, " \"%s\",\n", DLLName
);
1130 fprintf( outfile
, " %d,\n", Base
);
1131 fprintf( outfile
, " %d,\n", Limit
- Base
+ 1 );
1132 fprintf( outfile
, " %d,\n", nb_names
);
1133 fprintf( outfile
, " %d,\n", nb_reg_funcs
);
1145 /*******************************************************************
1148 * Build a Win16 assembly file from a spec file.
1150 static int BuildSpec16File( char * specfile
, FILE *outfile
)
1154 int code_offset
, data_offset
, module_size
;
1155 unsigned char *data
;
1157 data
= (unsigned char *)xmalloc( 0x10000 );
1158 memset( data
, 0, 16 );
1161 fprintf( outfile
, "/* File generated automatically; do not edit! */\n" );
1162 fprintf( outfile
, "\t.text\n" );
1163 fprintf( outfile
, "Code_Start:\n" );
1166 odp
= OrdinalDefinitions
;
1167 for (i
= 0; i
<= Limit
; i
++, odp
++)
1172 odp
->offset
= 0xffff;
1176 odp
->offset
= LOWORD(odp
->u
.abs
.value
);
1180 odp
->offset
= data_offset
;
1181 data_offset
+= StoreVariableCode( data
+ data_offset
, 1, odp
);
1185 odp
->offset
= data_offset
;
1186 data_offset
+= StoreVariableCode( data
+ data_offset
, 2, odp
);
1190 odp
->offset
= data_offset
;
1191 data_offset
+= StoreVariableCode( data
+ data_offset
, 4, odp
);
1195 fprintf( outfile
,"/* %s.%d */\n", DLLName
, i
);
1196 fprintf( outfile
,"\tmovw $%d,%%ax\n",LOWORD(odp
->u
.ret
.ret_value
));
1197 fprintf( outfile
,"\tmovw $%d,%%dx\n",HIWORD(odp
->u
.ret
.ret_value
));
1198 fprintf( outfile
,"\t.byte 0x66\n");
1199 if (odp
->u
.ret
.arg_size
!= 0)
1200 fprintf( outfile
, "\tlret $%d\n\n", odp
->u
.ret
.arg_size
);
1203 fprintf( outfile
, "\tlret\n");
1204 fprintf( outfile
, "\tnop\n");
1205 fprintf( outfile
, "\tnop\n\n");
1207 odp
->offset
= code_offset
;
1208 code_offset
+= 12; /* Assembly code is 12 bytes long */
1213 case TYPE_PASCAL_16
:
1215 fprintf( outfile
, "/* %s.%d */\n", DLLName
, i
);
1216 fprintf( outfile
, "\tpushw %%bp\n" );
1217 fprintf( outfile
, "\tpushl $" PREFIX
"%s\n",odp
->u
.func
.link_name
);
1218 /* FreeBSD does not understand lcall, so do it the hard way */
1219 fprintf( outfile
, "\t.byte 0x9a\n" );
1220 fprintf( outfile
, "\t.long " PREFIX
"CallFrom16_%s_%s\n",
1221 (odp
->type
== TYPE_REGISTER
) ? "regs" :
1222 (odp
->type
== TYPE_PASCAL
) ? "long" : "word",
1223 odp
->u
.func
.arg_types
);
1224 fprintf( outfile
, "\t.long 0x%08lx\n",
1225 MAKELONG( Code_Selector
, 0x9090 /* nop ; nop */ ) );
1226 odp
->offset
= code_offset
;
1227 code_offset
+= 16; /* Assembly code is 16 bytes long */
1231 fprintf(stderr
,"build: function type %d not available for Win16\n",
1237 if (!code_offset
) /* Make sure the code segment is not empty */
1239 fprintf( outfile
, "\t.byte 0\n" );
1243 /* Output data segment */
1245 DumpBytes( outfile
, data
, data_offset
, NULL
, "Data_Start" );
1247 /* Build the module */
1249 module_size
= BuildModule16( outfile
, code_offset
, data_offset
);
1251 /* Output the DLL descriptor */
1253 fprintf( outfile
, "\t.text\n" );
1254 fprintf( outfile
, "DLLName:\t" STRING
" \"%s\\0\"\n", DLLName
);
1255 fprintf( outfile
, "\t.align 4\n" );
1256 fprintf( outfile
, "\t.globl " PREFIX
"%s_Descriptor\n", DLLName
);
1257 fprintf( outfile
, PREFIX
"%s_Descriptor:\n", DLLName
);
1258 fprintf( outfile
, "\t.long DLLName\n" ); /* Name */
1259 fprintf( outfile
, "\t.long Module_Start\n" ); /* Module start */
1260 fprintf( outfile
, "\t.long %d\n", module_size
); /* Module size */
1261 fprintf( outfile
, "\t.long Code_Start\n" ); /* Code start */
1262 fprintf( outfile
, "\t.long Data_Start\n" ); /* Data start */
1267 /*******************************************************************
1270 * Build an assembly file from a spec file.
1272 static int BuildSpecFile( FILE *outfile
, char *specname
)
1274 SpecName
= specname
;
1275 SpecFp
= fopen( specname
, "r");
1278 fprintf(stderr
, "Could not open specification file, '%s'\n", specname
);
1282 if (ParseTopLevel() < 0) return -1;
1287 return BuildSpec16File( specname
, outfile
);
1289 return BuildSpec32File( specname
, outfile
);
1291 fprintf( stderr
, "%s: Missing 'type' declaration\n", specname
);
1297 /*******************************************************************
1298 * TransferArgs16To32
1300 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1301 * The 16-bit stack layout is:
1309 static int TransferArgs16To32( FILE *outfile
, char *args
)
1311 int i
, pos16
, pos32
;
1313 /* Copy the arguments */
1315 pos16
= 6; /* skip bp and return address */
1318 for (i
= strlen(args
); i
> 0; i
--)
1323 case 'w': /* word */
1324 fprintf( outfile
, "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1325 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1329 case 's': /* s_word */
1330 fprintf( outfile
, "\tmovswl %d(%%ebp),%%eax\n", pos16
);
1331 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1335 case 'l': /* long or segmented pointer */
1336 case 'T': /* segmented pointer to null-terminated string */
1337 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", pos16
);
1338 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1342 case 'p': /* linear pointer */
1343 case 't': /* linear pointer to null-terminated string */
1344 /* Get the selector */
1345 fprintf( outfile
, "\tmovw %d(%%ebp),%%ax\n", pos16
+ 2 );
1346 /* Get the selector base */
1347 fprintf( outfile
, "\tandl $0xfff8,%%eax\n" );
1348 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy(%%eax),%%eax\n" );
1349 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1350 /* Add the offset */
1351 fprintf( outfile
, "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1352 fprintf( outfile
, "\taddl %%eax,%d(%%ebx)\n", pos32
);
1357 fprintf( stderr
, "Unknown arg type '%c'\n", args
[i
-1] );
1361 return pos16
- 6; /* Return the size of the 16-bit args */
1365 /*******************************************************************
1368 * Build the context structure on the 32-bit stack.
1370 static void BuildContext16( FILE *outfile
)
1372 /* Store the registers */
1374 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1375 CONTEXTOFFSET(Eax
) - sizeof(CONTEXT
) );
1376 fprintf( outfile
, "\tmovl %%ecx,%d(%%ebx)\n",
1377 CONTEXTOFFSET(Ecx
) - sizeof(CONTEXT
) );
1378 fprintf( outfile
, "\tmovl %%edx,%d(%%ebx)\n",
1379 CONTEXTOFFSET(Edx
) - sizeof(CONTEXT
) );
1380 fprintf( outfile
, "\tmovl %%esi,%d(%%ebx)\n",
1381 CONTEXTOFFSET(Esi
) - sizeof(CONTEXT
) );
1382 fprintf( outfile
, "\tmovl %%edi,%d(%%ebx)\n",
1383 CONTEXTOFFSET(Edi
) - sizeof(CONTEXT
) );
1385 fprintf( outfile
, "\tmovl -20(%%ebp),%%eax\n" ); /* Get %ebx from stack*/
1386 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1387 CONTEXTOFFSET(Ebx
) - sizeof(CONTEXT
) );
1388 fprintf( outfile
, "\tmovzwl -10(%%ebp),%%eax\n" ); /* Get %ds from stack*/
1389 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1390 CONTEXTOFFSET(SegDs
) - sizeof(CONTEXT
) );
1391 fprintf( outfile
, "\tmovzwl -6(%%ebp),%%eax\n" ); /* Get %es from stack*/
1392 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1393 CONTEXTOFFSET(SegEs
) - sizeof(CONTEXT
) );
1394 fprintf( outfile
, "\tpushfl\n" );
1395 fprintf( outfile
, "\tpopl %d(%%ebx)\n",
1396 CONTEXTOFFSET(EFlags
) - sizeof(CONTEXT
) );
1397 fprintf( outfile
, "\tmovl -16(%%ebp),%%eax\n" ); /* Get %ebp from stack */
1398 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1399 CONTEXTOFFSET(Ebp
) - sizeof(CONTEXT
) );
1400 fprintf( outfile
, "\tmovzwl 2(%%ebp),%%eax\n" ); /* Get %ip from stack */
1401 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1402 CONTEXTOFFSET(Eip
) - sizeof(CONTEXT
) );
1403 fprintf( outfile
, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1404 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1405 CONTEXTOFFSET(SegCs
) - sizeof(CONTEXT
) );
1406 fprintf( outfile
, "\tmovw %%fs,%%ax\n" );
1407 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1408 CONTEXTOFFSET(SegFs
) - sizeof(CONTEXT
) );
1409 fprintf( outfile
, "\tmovw %%gs,%%ax\n" );
1410 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1411 CONTEXTOFFSET(SegGs
) - sizeof(CONTEXT
) );
1412 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
1413 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1414 CONTEXTOFFSET(SegSs
) - sizeof(CONTEXT
) );
1416 fprintf( outfile
, "\tfsave %d(%%ebx)\n",
1417 CONTEXTOFFSET(FloatSave
) - sizeof(CONTEXT
) );
1422 /*******************************************************************
1425 * Restore the registers from the context structure.
1426 * %edx must point to the 32-bit stack top.
1428 static void RestoreContext16( FILE *outfile
)
1430 /* Get the 32-bit stack pointer */
1432 fprintf( outfile
, "\tleal -%d(%%ebp),%%ebx\n",
1433 STRUCTOFFSET(STACK32FRAME
,ebp
) );
1435 /* Remove everything up to the return address from the 16-bit stack */
1437 fprintf( outfile
, "\taddl $22,%%esp\n" );
1439 /* Restore the registers */
1441 fprintf( outfile
, "\tmovl %d(%%ebx),%%ecx\n",
1442 CONTEXTOFFSET(Ecx
) - sizeof(CONTEXT
) );
1443 fprintf( outfile
, "\tmovl %d(%%ebx),%%edx\n",
1444 CONTEXTOFFSET(Edx
) - sizeof(CONTEXT
) );
1445 fprintf( outfile
, "\tmovl %d(%%ebx),%%esi\n",
1446 CONTEXTOFFSET(Esi
) - sizeof(CONTEXT
) );
1447 fprintf( outfile
, "\tmovl %d(%%ebx),%%edi\n",
1448 CONTEXTOFFSET(Edi
) - sizeof(CONTEXT
) );
1449 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebp\n",
1450 CONTEXTOFFSET(Ebp
) - sizeof(CONTEXT
) );
1451 fprintf( outfile
, "\tpushw %d(%%ebx)\n", /* Push new cs */
1452 CONTEXTOFFSET(SegCs
) - sizeof(CONTEXT
) );
1453 fprintf( outfile
, "\tpushw %d(%%ebx)\n", /* Push new ip */
1454 CONTEXTOFFSET(Eip
) - sizeof(CONTEXT
) );
1455 fprintf( outfile
, "\tpushl %d(%%ebx)\n", /* Push new ds */
1456 CONTEXTOFFSET(SegDs
) - sizeof(CONTEXT
) );
1457 fprintf( outfile
, "\tpushl %d(%%ebx)\n", /* Push new es */
1458 CONTEXTOFFSET(SegEs
) - sizeof(CONTEXT
) );
1459 fprintf( outfile
, "\tpushl %d(%%ebx)\n",
1460 CONTEXTOFFSET(EFlags
) - sizeof(CONTEXT
) );
1461 fprintf( outfile
, "\tpopfl\n" );
1462 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n",
1463 CONTEXTOFFSET(Eax
) - sizeof(CONTEXT
) );
1464 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebx\n",
1465 CONTEXTOFFSET(Ebx
) - sizeof(CONTEXT
) );
1466 fprintf( outfile
, "\tpopl %%es\n" ); /* Set es */
1467 fprintf( outfile
, "\tpopl %%ds\n" ); /* Set ds */
1471 /*******************************************************************
1472 * BuildCallFrom16Func
1474 * Build a 16-bit-to-Wine callback function. The syntax of the function
1475 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1476 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1477 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1478 * 'T'=segmented pointer to null-terminated string).
1479 * For register functions, the arguments are ignored, but they are still
1480 * removed from the stack upon return.
1482 * Stack layout upon entry to the callback function:
1484 * (sp+18) word first 16-bit arg
1488 * (sp+8) long 32-bit entry point (used to store edx)
1489 * (sp+6) word high word of cs (always 0, used to store es)
1490 * (sp+4) word low word of cs of 16-bit entry point
1491 * (sp+2) word high word of ip (always 0, used to store ds)
1492 * (sp) word low word of ip of 16-bit entry point
1494 * Added on the stack:
1496 * (sp-8) long saved previous stack
1498 static void BuildCallFrom16Func( FILE *outfile
, char *profile
)
1503 char *args
= profile
+ 5;
1505 /* Parse function type */
1507 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1508 else if (!strncmp( "regs_", profile
, 5 )) reg_func
= 1;
1509 else if (strncmp( "long_", profile
, 5 ))
1511 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1515 /* Function header */
1517 fprintf( outfile
, "\n\t.align 4\n" );
1519 fprintf( outfile
, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX
"CallFrom16_%s\n",
1522 fprintf( outfile
, "\t.globl " PREFIX
"CallFrom16_%s\n", profile
);
1523 fprintf( outfile
, PREFIX
"CallFrom16_%s:\n", profile
);
1525 /* Setup bp to point to its copy on the stack */
1527 fprintf( outfile
, "\tpushl %%ebp\n" ); /* Save the full 32-bit ebp */
1528 fprintf( outfile
, "\tmovzwl %%sp,%%ebp\n" );
1529 fprintf( outfile
, "\taddw $16,%%bp\n" );
1531 /* Save 16-bit ds and es */
1533 /* Stupid FreeBSD assembler doesn't know these either */
1534 /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1535 fprintf( outfile
, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1536 /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1537 fprintf( outfile
, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1541 fprintf( outfile
, "\tpushl %%ebx\n" );
1543 /* Restore 32-bit segment registers */
1545 fprintf( outfile
, "\tmovw $0x%04x,%%bx\n", Data_Selector
);
1547 fprintf( outfile
, "\tdata16\n");
1549 fprintf( outfile
, "\tmovw %%bx,%%ds\n" );
1551 fprintf( outfile
, "\tdata16\n");
1553 fprintf( outfile
, "\tmovw %%bx,%%es\n" );
1554 fprintf( outfile
, "\tmovw " PREFIX
"CALLTO16_Current_fs,%%fs\n" );
1556 /* Get the 32-bit stack pointer from the TEB */
1558 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET
);
1560 /* Save the 16-bit stack */
1563 fprintf( outfile
,"\tdata16\n");
1565 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%ss,(%d)\n", STACKOFFSET
+ 2 );
1566 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%sp,(%d)\n", STACKOFFSET
);
1568 /* Transfer the arguments */
1570 if (reg_func
) BuildContext16( outfile
);
1571 else if (*args
) argsize
= TransferArgs16To32( outfile
, args
);
1573 /* Get the address of the API function */
1575 fprintf( outfile
, "\tmovl -4(%%ebp),%%eax\n" );
1577 /* If necessary, save %edx over the API function address */
1579 if (!reg_func
&& short_ret
)
1580 fprintf( outfile
, "\tmovl %%edx,-4(%%ebp)\n" );
1582 /* Restore %ebx and store the 32-bit stack pointer instead */
1584 fprintf( outfile
, "\tmovl %%ebx,%%ebp\n" );
1585 fprintf( outfile
, "\tpopl %%ebx\n" );
1586 fprintf( outfile
, "\tpushl %%ebp\n" );
1588 /* Switch to the 32-bit stack */
1590 fprintf( outfile
, "\tpushl %%ds\n" );
1591 fprintf( outfile
, "\tpopl %%ss\n" );
1592 fprintf( outfile
, "\tleal -%d(%%ebp),%%esp\n",
1593 reg_func
? sizeof(CONTEXT
) : 4 * strlen(args
) );
1594 if (reg_func
) /* Push the address of the context struct */
1595 fprintf( outfile
, "\tpushl %%esp\n" );
1597 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1599 fprintf( outfile
, "\taddl $%d,%%ebp\n", STRUCTOFFSET(STACK32FRAME
,ebp
) );
1601 /* Print the debug information before the call */
1605 fprintf( outfile
, "\tpushl %%eax\n" );
1606 fprintf( outfile
, "\tpushl $Profile_%s\n", profile
);
1607 fprintf( outfile
, "\tpushl $%d\n", reg_func
? 2 : (short_ret
? 1 : 0));
1608 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16\n" );
1609 fprintf( outfile
, "\tpopl %%eax\n" );
1610 fprintf( outfile
, "\tpopl %%eax\n" );
1611 fprintf( outfile
, "\tpopl %%eax\n" );
1614 /* Call the entry point */
1616 fprintf( outfile
, "\tcall *%%eax\n" );
1618 /* Print the debug information after the call */
1624 /* Push again the address of the context struct in case */
1625 /* it has been removed by an stdcall function */
1626 fprintf( outfile
, "\tleal -%d(%%ebp),%%esp\n",
1627 sizeof(CONTEXT
) + STRUCTOFFSET(STACK32FRAME
,ebp
) );
1628 fprintf( outfile
, "\tpushl %%esp\n" );
1630 fprintf( outfile
, "\tpushl %%eax\n" );
1631 fprintf( outfile
, "\tpushl $%d\n", reg_func
? 2 : (short_ret
? 1 : 0));
1632 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret\n" );
1633 fprintf( outfile
, "\tpopl %%eax\n" );
1634 fprintf( outfile
, "\tpopl %%eax\n" );
1637 /* Restore the 16-bit stack */
1640 fprintf( outfile
, "\tdata16\n");
1642 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2 );
1643 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET
);
1644 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
1645 fprintf( outfile
, "\tmovw %%fs," PREFIX
"CALLTO16_Current_fs\n" );
1649 /* Calc the arguments size */
1665 fprintf( stderr
, "Unknown arg type '%c'\n", *args
);
1670 /* Restore registers from the context structure */
1671 RestoreContext16( outfile
);
1675 /* Restore high 16 bits of ebp */
1676 fprintf( outfile
, "\tpopl %%ebp\n" );
1678 /* Restore ds and es */
1679 fprintf( outfile
, "\tincl %%esp\n" ); /* Remove ip */
1680 fprintf( outfile
, "\tincl %%esp\n" );
1681 fprintf( outfile
, "\tpopl %%edx\n" ); /* Remove cs and ds */
1682 fprintf( outfile
, "\tmovw %%dx,%%ds\n" ); /* and restore ds */
1683 fprintf( outfile
, "\t.byte 0x66\n\tpopl %%es\n" ); /* Restore es */
1685 if (short_ret
) fprintf( outfile
, "\tpopl %%edx\n" ); /* Restore edx */
1688 /* Get the return value into dx:ax */
1689 fprintf( outfile
, "\tmovl %%eax,%%edx\n" );
1690 fprintf( outfile
, "\tshrl $16,%%edx\n" );
1691 /* Remove API entry point */
1692 fprintf( outfile
, "\taddl $4,%%esp\n" );
1695 /* Restore low 16 bits of ebp */
1696 fprintf( outfile
, "\tpopw %%bp\n" );
1699 /* Remove the arguments and return */
1703 fprintf( outfile
, "\t.byte 0x66\n" );
1704 fprintf( outfile
, "\tlret $%d\n", argsize
);
1708 fprintf( outfile
, "\t.byte 0x66\n" );
1709 fprintf( outfile
, "\tlret\n" );
1714 /*******************************************************************
1717 * Build a Wine-to-16-bit callback function.
1719 * Stack frame of the callback function:
1723 * (ebp+8) func to call
1724 * (ebp+4) return address
1725 * (ebp) previous ebp
1727 * Prototypes for the CallTo16 functions:
1728 * extern WINAPI WORD CallTo16_word_xxx( FARPROC16 func, args... );
1729 * extern WINAPI LONG CallTo16_long_xxx( FARPROC16 func, args... );
1730 * extern WINAPI void CallTo16_sreg_( const CONTEXT *context, int nb_args );
1731 * extern WINAPI void CallTo16_lreg_( const CONTEXT *context, int nb_args );
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( "sreg_", profile
, 5 )) reg_func
= 1;
1741 else if (!strncmp( "lreg_", profile
, 5 )) reg_func
= 2;
1742 else if (strncmp( "long_", profile
, 5 ))
1744 fprintf( stderr
, "Invalid function name '%s'.\n", profile
);
1748 /* Function header */
1750 fprintf( outfile
, "\n\t.align 4\n" );
1752 fprintf( outfile
, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX
"CallTo16_%s\n",
1755 fprintf( outfile
, "\t.globl " PREFIX
"CallTo16_%s\n", profile
);
1756 fprintf( outfile
, PREFIX
"CallTo16_%s:\n", profile
);
1760 fprintf( outfile
, "\tpushl %%ebp\n" );
1761 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
1763 /* Call the actual CallTo16 routine (simulate a lcall) */
1765 fprintf( outfile
, "\tpushl %%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", 4 * strlen(args
) + 4 );
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 /* Print debugging info */
1794 /* Push the address of the first argument */
1795 fprintf( outfile
, "\tleal 8(%%ebp),%%eax\n" );
1796 fprintf( outfile
, "\tpushl $%d\n", reg_func
? -1 : strlen(args
) );
1797 fprintf( outfile
, "\tpushl %%eax\n" );
1798 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16\n" );
1799 fprintf( outfile
, "\tpopl %%eax\n" );
1800 fprintf( outfile
, "\tpopl %%eax\n" );
1803 /* Save the 32-bit stack and %fs */
1805 fprintf( outfile
, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET
);
1806 fprintf( outfile
, "\tmovl %%ebp,%%ebx\n" );
1807 fprintf( outfile
, "\tmovl %%esp,%%edx\n" );
1808 fprintf( outfile
, "\tmovw %%fs," PREFIX
"CALLTO16_Current_fs\n" );
1812 /* Switch to the 16-bit stack, saving the current %%esp, */
1813 /* and adding the specified offset to the new sp */
1814 fprintf( outfile
, "\t.byte 0x64\n\tmovzwl (%d),%%eax\n", STACKOFFSET
);
1815 fprintf( outfile
, "\tsubl 12(%%ebx),%%eax\n" ); /* Get the offset */
1817 fprintf( outfile
,"\tdata16\n");
1819 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2);
1820 fprintf( outfile
, "\tmovl %%eax,%%esp\n" );
1821 fprintf( outfile
, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET
);
1823 /* Get the registers. ebx is handled later on. */
1825 fprintf( outfile
, "\tmovl 8(%%ebx),%%ebx\n" );
1826 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs
) );
1827 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
1828 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp
) );
1829 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax
) );
1830 fprintf( outfile
, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx
) );
1831 fprintf( outfile
, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx
) );
1832 fprintf( outfile
, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi
) );
1833 fprintf( outfile
, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi
) );
1835 /* Push the return address
1836 * With sreg suffix, we push 16:16 address (normal lret)
1837 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
1840 fprintf( outfile
, "\tpushl " PREFIX
"CALLTO16_RetAddr_long\n" );
1843 fprintf( outfile
, "\tpushw $0\n" );
1844 fprintf( outfile
, "\tpushw " PREFIX
"CALLTO16_RetAddr_long+2\n" );
1845 fprintf( outfile
, "\tpushw $0\n" );
1846 fprintf( outfile
, "\tpushw " PREFIX
"CALLTO16_RetAddr_long\n" );
1849 /* Push the called routine address */
1851 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs
) );
1852 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip
) );
1854 /* Get the 16-bit ds */
1856 fprintf( outfile
, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs
) );
1857 /* Get ebx from the 32-bit stack */
1858 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(Ebx
) );
1859 fprintf( outfile
, "\tpopl %%ds\n" );
1861 else /* not a register function */
1863 int pos
= 12; /* first argument position */
1865 /* Switch to the 16-bit stack */
1867 fprintf( outfile
,"\tdata16\n");
1869 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2);
1870 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET
);
1871 fprintf( outfile
, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET
);
1873 /* Make %bp point to the previous stackframe (built by CallFrom16) */
1874 fprintf( outfile
, "\tmovzwl %%sp,%%ebp\n" );
1875 fprintf( outfile
, "\tleal %d(%%ebp),%%ebp\n",
1876 STRUCTOFFSET(STACK16FRAME
,bp
) );
1878 /* Transfer the arguments */
1884 case 'w': /* word */
1885 fprintf( outfile
, "\tpushw %d(%%ebx)\n", pos
);
1887 case 'l': /* long */
1888 fprintf( outfile
, "\tpushl %d(%%ebx)\n", pos
);
1891 fprintf( stderr
, "Unexpected case '%c' in BuildCallTo16Func\n",
1897 /* Push the return address */
1899 fprintf( outfile
, "\tpushl " PREFIX
"CALLTO16_RetAddr_%s\n",
1900 short_ret
? "word" : "long" );
1902 /* Push the called routine address */
1904 fprintf( outfile
, "\tpushl 8(%%ebx)\n" );
1906 /* Set %ds and %es (and %ax just in case) equal to %ss */
1908 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
1909 fprintf( outfile
, "\tmovw %%ax,%%ds\n" );
1910 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
1913 /* Jump to the called routine */
1915 fprintf( outfile
, "\t.byte 0x66\n" );
1916 fprintf( outfile
, "\tlret\n" );
1920 /*******************************************************************
1923 * Build the return code for 16-bit callbacks
1925 static void BuildRet16Func( FILE *outfile
)
1927 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Ret_word\n" );
1928 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Ret_long\n" );
1930 fprintf( outfile
, PREFIX
"CALLTO16_Ret_word:\n" );
1931 fprintf( outfile
, "\txorl %%edx,%%edx\n" );
1933 /* Put return value into %eax */
1935 fprintf( outfile
, PREFIX
"CALLTO16_Ret_long:\n" );
1936 fprintf( outfile
, "\tshll $16,%%edx\n" );
1937 fprintf( outfile
, "\tmovw %%ax,%%dx\n" );
1938 fprintf( outfile
, "\tmovl %%edx,%%eax\n" );
1940 /* Restore 32-bit segment registers */
1942 fprintf( outfile
, "\tmovw $0x%04x,%%bx\n", 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" );
1951 fprintf( outfile
, "\tmovw " PREFIX
"CALLTO16_Current_fs,%%fs\n" );
1953 /* Restore the 32-bit stack */
1956 fprintf( outfile
, "\tdata16\n");
1958 fprintf( outfile
, "\tmovw %%bx,%%ss\n" );
1959 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET
);
1960 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
1962 /* Restore the 32-bit registers */
1964 fprintf( outfile
, "\tpopl %%edi\n" );
1965 fprintf( outfile
, "\tpopl %%esi\n" );
1966 fprintf( outfile
, "\tpopl %%edx\n" );
1967 fprintf( outfile
, "\tpopl %%ecx\n" );
1968 fprintf( outfile
, "\tpopl %%ebx\n" );
1970 /* Return to caller */
1972 fprintf( outfile
, "\tlret\n" );
1974 /* Declare the return address variables */
1976 fprintf( outfile
, "\t.data\n" );
1977 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_RetAddr_word\n" );
1978 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_RetAddr_long\n" );
1979 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Current_fs\n" );
1980 fprintf( outfile
, PREFIX
"CALLTO16_RetAddr_word:\t.long 0\n" );
1981 fprintf( outfile
, PREFIX
"CALLTO16_RetAddr_long:\t.long 0\n" );
1982 fprintf( outfile
, PREFIX
"CALLTO16_Current_fs:\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
, "\t.type " PREFIX
"CALL32_Init,@function\n" );
2017 fprintf( outfile
, PREFIX
"CALL32_Init:\n" );
2018 fprintf( outfile
, "\tleal -256(%%esp),%%eax\n" );
2019 fprintf( outfile
, "\tmovl %%eax,CALL32_Original32_esp\n" );
2020 fprintf( outfile
, "\tmovl $CALL32_LargeStack,%%eax\n" );
2021 fprintf( outfile
, "\tret\n" );
2023 /* Function header */
2025 fprintf( outfile
, "\n\t.align 4\n" );
2027 fprintf( outfile
, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2029 fprintf( outfile
, "CALL32_LargeStack:\n" );
2033 fprintf( outfile
, "\tpushl %%ebp\n" );
2034 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2036 /* Switch to the original 32-bit stack pointer */
2038 fprintf( outfile
, "\tmovl CALL32_Original32_esp, %%esp\n" );
2040 /* Transfer the argument and call the function */
2042 fprintf( outfile
, "\tpushl 12(%%ebp)\n" );
2043 fprintf( outfile
, "\tcall *8(%%ebp)\n" );
2045 /* Restore registers and return */
2047 fprintf( outfile
, "\tmovl %%ebp,%%esp\n" );
2048 fprintf( outfile
, "\tpopl %%ebp\n" );
2049 fprintf( outfile
, "\tret\n" );
2053 fprintf( outfile
, "\t.data\n" );
2054 fprintf( outfile
, "CALL32_Original32_esp:\t.long 0\n" );
2055 fprintf( outfile
, "\t.text\n" );
2059 /*******************************************************************
2060 * BuildCallFrom32Regs
2062 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2063 * 'args' is the number of dword arguments.
2067 * (esp+208) ret addr (or relay addr when debugging_relay is on)
2068 * (esp+204) entry point
2069 * (esp+0) CONTEXT struct
2071 static void BuildCallFrom32Regs( FILE *outfile
)
2073 /* Function header */
2075 fprintf( outfile
, "\n\t.align 4\n" );
2077 fprintf( outfile
, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX
"CALL32_Regs\n" );
2079 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Regs\n" );
2080 fprintf( outfile
, PREFIX
"CALL32_Regs:\n" );
2082 /* Build the context structure */
2084 fprintf( outfile
, "\tpushw $0\n" );
2085 fprintf( outfile
, "\t.byte 0x66\n\tpushl %%ss\n" );
2086 fprintf( outfile
, "\tpushl %%eax\n" ); /* %esp place holder */
2087 fprintf( outfile
, "\tpushfl\n" );
2088 fprintf( outfile
, "\tpushw $0\n" );
2089 fprintf( outfile
, "\t.byte 0x66\n\tpushl %%cs\n" );
2090 fprintf( outfile
, "\tpushl 20(%%esp)\n" ); /* %eip at time of call */
2091 fprintf( outfile
, "\tpushl %%ebp\n" );
2093 fprintf( outfile
, "\tpushl %%eax\n" );
2094 fprintf( outfile
, "\tpushl %%ecx\n" );
2095 fprintf( outfile
, "\tpushl %%edx\n" );
2096 fprintf( outfile
, "\tpushl %%ebx\n" );
2097 fprintf( outfile
, "\tpushl %%esi\n" );
2098 fprintf( outfile
, "\tpushl %%edi\n" );
2100 fprintf( outfile
, "\txorl %%eax,%%eax\n" );
2101 fprintf( outfile
, "\tmovw %%ds,%%ax\n" );
2102 fprintf( outfile
, "\tpushl %%eax\n" );
2103 fprintf( outfile
, "\tmovw %%es,%%ax\n" );
2104 fprintf( outfile
, "\tpushl %%eax\n" );
2105 fprintf( outfile
, "\tmovw %%fs,%%ax\n" );
2106 fprintf( outfile
, "\tpushl %%eax\n" );
2107 fprintf( outfile
, "\tmovw %%gs,%%ax\n" );
2108 fprintf( outfile
, "\tpushl %%eax\n" );
2110 fprintf( outfile
, "\tleal -%d(%%esp),%%esp\n",
2111 sizeof(FLOATING_SAVE_AREA
) + 6 * sizeof(DWORD
) /* DR regs */ );
2112 fprintf( outfile
, "\tpushl $0x0001001f\n" ); /* ContextFlags */
2114 fprintf( outfile
, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave
) );
2116 fprintf( outfile
, "\tleal %d(%%esp),%%eax\n",
2117 sizeof(CONTEXT
) + 4 ); /* %esp at time of call */
2118 fprintf( outfile
, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Esp
) );
2120 fprintf( outfile
, "\tcall " PREFIX
"RELAY_CallFrom32Regs\n" );
2122 /* Restore the context structure */
2124 fprintf( outfile
, "\tfrstor %d(%%esp)\n", CONTEXTOFFSET(FloatSave
) );
2126 /* Store %eip value onto the new stack */
2128 fprintf( outfile
, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Eip
) );
2129 fprintf( outfile
, "\tmovl %d(%%esp),%%ebx\n", CONTEXTOFFSET(Esp
) );
2130 fprintf( outfile
, "\tmovl %%eax,0(%%ebx)\n" );
2132 /* Restore all registers */
2134 fprintf( outfile
, "\tleal %d(%%esp),%%esp\n",
2135 sizeof(FLOATING_SAVE_AREA
) + 7 * sizeof(DWORD
) );
2136 fprintf( outfile
, "\tpopl %%eax\n" );
2137 fprintf( outfile
, "\tmovw %%ax,%%gs\n" );
2138 fprintf( outfile
, "\tpopl %%eax\n" );
2139 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2140 fprintf( outfile
, "\tpopl %%eax\n" );
2141 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2142 fprintf( outfile
, "\tpopl %%eax\n" );
2143 fprintf( outfile
, "\tmovw %%ax,%%ds\n" );
2145 fprintf( outfile
, "\tpopl %%edi\n" );
2146 fprintf( outfile
, "\tpopl %%esi\n" );
2147 fprintf( outfile
, "\tpopl %%ebx\n" );
2148 fprintf( outfile
, "\tpopl %%edx\n" );
2149 fprintf( outfile
, "\tpopl %%ecx\n" );
2150 fprintf( outfile
, "\tpopl %%eax\n" );
2151 fprintf( outfile
, "\tpopl %%ebp\n" );
2152 fprintf( outfile
, "\tleal 8(%%esp),%%esp\n" ); /* skip %eip and %cs */
2153 fprintf( outfile
, "\tpopfl\n" );
2154 fprintf( outfile
, "\tpopl %%esp\n" );
2155 fprintf( outfile
, "\tret\n" );
2159 /*******************************************************************
2162 * Build the spec files
2164 static int BuildSpec( FILE *outfile
, int argc
, char *argv
[] )
2167 for (i
= 2; i
< argc
; i
++)
2168 if (BuildSpecFile( outfile
, argv
[i
] ) < 0) return -1;
2173 /*******************************************************************
2176 * Build the 16-bit-to-Wine callbacks
2178 static int BuildCallFrom16( FILE *outfile
, char * outname
, int argc
, char *argv
[] )
2185 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2186 fprintf( outfile
, "\t.text\n" );
2189 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2190 getcwd(buffer
, sizeof(buffer
));
2193 * The stabs help the internal debugger as they are an indication that it
2194 * is sensible to step into a thunk/trampoline.
2196 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2197 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2198 fprintf( outfile
, "\t.text\n" );
2199 fprintf( outfile
, "\t.align 4\n" );
2200 fprintf( outfile
, "Code_Start:\n\n" );
2203 /* Build the callback functions */
2205 for (i
= 2; i
< argc
; i
++) BuildCallFrom16Func( outfile
, argv
[i
] );
2207 /* Output the argument debugging strings */
2211 fprintf( outfile
, "/* Argument strings */\n" );
2212 for (i
= 2; i
< argc
; i
++)
2214 fprintf( outfile
, "Profile_%s:\t", argv
[i
] );
2215 fprintf( outfile
, STRING
" \"%s\\0\"\n", argv
[i
] + 5 );
2220 fprintf( outfile
, "\t.text\n");
2221 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2222 fprintf( outfile
, ".Letext:\n");
2229 /*******************************************************************
2232 * Build the Wine-to-16-bit callbacks
2234 static int BuildCallTo16( FILE *outfile
, char * outname
, int argc
, char *argv
[] )
2241 infile
= fopen( argv
[2], "r" );
2248 else infile
= stdin
;
2252 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2253 fprintf( outfile
, "\t.text\n" );
2256 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2257 getcwd(buffer
, sizeof(buffer
));
2260 * The stabs help the internal debugger as they are an indication that it
2261 * is sensible to step into a thunk/trampoline.
2263 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2264 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2265 fprintf( outfile
, "\t.text\n" );
2266 fprintf( outfile
, "\t.align 4\n" );
2267 fprintf( outfile
, "Code_Start:\n\n" );
2270 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Start\n" );
2271 fprintf( outfile
, PREFIX
"CALLTO16_Start:\n" );
2273 /* Build the callback functions */
2275 while (fgets( buffer
, sizeof(buffer
), infile
))
2277 if (strstr( buffer
, "### start build ###" )) break;
2279 while (fgets( buffer
, sizeof(buffer
), infile
))
2281 char *p
= strstr( buffer
, "CallTo16_" );
2284 char *profile
= p
+ strlen( "CallTo16_" );
2286 while ((*p
== '_') || isalpha(*p
)) p
++;
2288 BuildCallTo16Func( outfile
, profile
);
2290 if (strstr( buffer
, "### stop build ###" )) break;
2293 /* Output the 16-bit return code */
2295 BuildRet16Func( outfile
);
2297 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_End\n" );
2298 fprintf( outfile
, PREFIX
"CALLTO16_End:\n" );
2301 fprintf( outfile
, "\t.text\n");
2302 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2303 fprintf( outfile
, ".Letext:\n");
2311 /*******************************************************************
2314 * Build the 32-bit callbacks
2316 static int BuildCall32( FILE *outfile
, char * outname
)
2322 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2323 fprintf( outfile
, "\t.text\n" );
2328 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2329 getcwd(buffer
, sizeof(buffer
));
2332 * The stabs help the internal debugger as they are an indication that it
2333 * is sensible to step into a thunk/trampoline.
2335 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2336 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2337 fprintf( outfile
, "\t.text\n" );
2338 fprintf( outfile
, "\t.align 4\n" );
2339 fprintf( outfile
, "Code_Start:\n" );
2342 /* Build the 32-bit large stack callback */
2344 BuildCallTo32LargeStack( outfile
);
2346 /* Build the register callback function */
2348 BuildCallFrom32Regs( outfile
);
2351 fprintf( outfile
, "\t.text\n");
2352 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2353 fprintf( outfile
, ".Letext:\n");
2356 #else /* __i386__ */
2358 /* Just to avoid an empty file */
2359 fprintf( outfile
, "\t.long 0\n" );
2361 #endif /* __i386__ */
2366 /*******************************************************************
2369 static void usage(void)
2372 "usage: build [-o outfile] -spec SPECNAMES\n"
2373 " build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2374 " build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2375 " build [-o outfile] -call32\n" );
2380 /*******************************************************************
2383 int main(int argc
, char **argv
)
2385 char *outname
= NULL
;
2386 FILE *outfile
= stdout
;
2389 if (argc
< 2) usage();
2391 if (!strcmp( argv
[1], "-o" ))
2396 if (argc
< 2) usage();
2397 if (!(outfile
= fopen( outname
, "w" )))
2399 fprintf( stderr
, "Unable to create output file '%s'\n", outname
);
2404 /* Retrieve the selector values; this assumes that we are building
2405 * the asm files on the platform that will also run them. Probably
2406 * a safe assumption to make.
2408 GET_CS( Code_Selector
);
2409 GET_DS( Data_Selector
);
2411 if (!strcmp( argv
[1], "-spec" ))
2412 res
= BuildSpec( outfile
, argc
, argv
);
2413 else if (!strcmp( argv
[1], "-callfrom16" ))
2414 res
= BuildCallFrom16( outfile
, outname
, argc
, argv
);
2415 else if (!strcmp( argv
[1], "-callto16" ))
2416 res
= BuildCallTo16( outfile
, outname
, argc
, argv
);
2417 else if (!strcmp( argv
[1], "-call32" ))
2418 res
= BuildCall32( outfile
, outname
);