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",
367 if ((odp
->type
== TYPE_PASCAL
) || (odp
->type
== TYPE_PASCAL_16
))
369 fprintf( stderr
, "%s:%d: 'pascal' not supported for Win32\n",
381 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
382 SpecName
, Line
, token
);
386 for (i
= 0; i
< sizeof(odp
->u
.func
.arg_types
)-1; i
++)
392 if (!strcmp(token
, "word"))
393 odp
->u
.func
.arg_types
[i
] = 'w';
394 else if (!strcmp(token
, "s_word"))
395 odp
->u
.func
.arg_types
[i
] = 's';
396 else if (!strcmp(token
, "long") || !strcmp(token
, "segptr"))
397 odp
->u
.func
.arg_types
[i
] = 'l';
398 else if (!strcmp(token
, "ptr"))
399 odp
->u
.func
.arg_types
[i
] = 'p';
400 else if (!strcmp(token
, "str"))
401 odp
->u
.func
.arg_types
[i
] = 't';
402 else if (!strcmp(token
, "wstr"))
403 odp
->u
.func
.arg_types
[i
] = 'W';
404 else if (!strcmp(token
, "segstr"))
405 odp
->u
.func
.arg_types
[i
] = 'T';
406 else if (!strcmp(token
, "double"))
408 odp
->u
.func
.arg_types
[i
++] = 'l';
409 odp
->u
.func
.arg_types
[i
] = 'l';
413 fprintf(stderr
, "%s:%d: Unknown variable type '%s'\n",
414 SpecName
, Line
, token
);
417 if (SpecType
== SPEC_WIN32
)
419 if (strcmp(token
, "long") &&
420 strcmp(token
, "ptr") &&
421 strcmp(token
, "str") &&
422 strcmp(token
, "wstr") &&
423 strcmp(token
, "double"))
425 fprintf( stderr
, "%s:%d: Type '%s' not supported for Win32\n",
426 SpecName
, Line
, token
);
431 if ((*token
!= ')') || (i
>= sizeof(odp
->u
.func
.arg_types
)))
433 fprintf( stderr
, "%s:%d: Too many arguments\n", SpecName
, Line
);
436 odp
->u
.func
.arg_types
[i
] = '\0';
437 if ((odp
->type
== TYPE_STDCALL
) && !i
)
438 odp
->type
= TYPE_CDECL
; /* stdcall is the same as cdecl for 0 args */
439 if ((odp
->type
== TYPE_REGISTER
) && (SpecType
== SPEC_WIN32
) && i
)
441 fprintf( stderr
, "%s:%d: register functions cannot have arguments in Win32\n",
445 strcpy(odp
->u
.func
.link_name
, GetToken());
450 /*******************************************************************
453 * Parse an 'equate' definition.
455 static int ParseEquate( ORDDEF
*odp
)
459 char *token
= GetToken();
460 int value
= strtol(token
, &endptr
, 0);
461 if (endptr
== NULL
|| *endptr
!= '\0')
463 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
464 SpecName
, Line
, token
);
468 if (SpecType
== SPEC_WIN32
)
470 fprintf( stderr
, "%s:%d: 'equate' not supported for Win32\n",
475 odp
->u
.abs
.value
= value
;
480 /*******************************************************************
483 * Parse a 'return' definition.
485 static int ParseReturn( ORDDEF
*odp
)
491 odp
->u
.ret
.arg_size
= strtol(token
, &endptr
, 0);
492 if (endptr
== NULL
|| *endptr
!= '\0')
494 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
495 SpecName
, Line
, token
);
500 odp
->u
.ret
.ret_value
= strtol(token
, &endptr
, 0);
501 if (endptr
== NULL
|| *endptr
!= '\0')
503 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
504 SpecName
, Line
, token
);
508 if (SpecType
== SPEC_WIN32
)
510 fprintf( stderr
, "%s:%d: 'return' not supported for Win32\n",
519 /*******************************************************************
522 * Parse a 'stub' definition.
524 static int ParseStub( ORDDEF
*odp
)
526 odp
->u
.func
.arg_types
[0] = '\0';
527 strcpy( odp
->u
.func
.link_name
, STUB_CALLBACK
);
532 /*******************************************************************
535 * Parse an 'varargs' definition.
537 static int ParseVarargs( ORDDEF
*odp
)
541 if (SpecType
== SPEC_WIN16
)
543 fprintf( stderr
, "%s:%d: 'varargs' not supported for Win16\n",
551 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
552 SpecName
, Line
, token
);
558 fprintf(stderr
, "%s:%d: Expected ')' got '%s'\n",
559 SpecName
, Line
, token
);
563 strcpy( odp
->u
.vargs
.link_name
, GetToken() );
568 /*******************************************************************
571 * Parse an 'extern' definition.
573 static int ParseExtern( ORDDEF
*odp
)
575 if (SpecType
== SPEC_WIN16
)
577 fprintf( stderr
, "%s:%d: 'extern' not supported for Win16\n",
581 strcpy( odp
->u
.ext
.link_name
, GetToken() );
586 /*******************************************************************
589 * Parse an ordinal definition.
591 static int ParseOrdinal(int ordinal
)
596 if (ordinal
>= MAX_ORDINALS
)
598 fprintf(stderr
, "%s:%d: Ordinal number too large\n", SpecName
, Line
);
601 if (ordinal
> Limit
) Limit
= ordinal
;
602 if (ordinal
< Base
) Base
= ordinal
;
604 odp
= &OrdinalDefinitions
[ordinal
];
605 if (!(token
= GetToken()))
607 fprintf(stderr
, "%s:%d: Expected type after ordinal\n", SpecName
, Line
);
611 for (odp
->type
= 0; odp
->type
< TYPE_NBTYPES
; odp
->type
++)
612 if (TypeNames
[odp
->type
] && !strcmp( token
, TypeNames
[odp
->type
] ))
615 if (odp
->type
>= TYPE_NBTYPES
)
618 "%s:%d: Expected type after ordinal, found '%s' instead\n",
619 SpecName
, Line
, token
);
623 if (!(token
= GetToken()))
625 fprintf( stderr
, "%s:%d: Expected name after type\n", SpecName
, Line
);
628 strcpy( odp
->name
, token
);
636 return ParseVariable( odp
);
642 return ParseExportFunction( odp
);
644 return ParseEquate( odp
);
646 return ParseReturn( odp
);
648 return ParseStub( odp
);
650 return ParseVarargs( odp
);
652 return ParseExtern( odp
);
654 fprintf( stderr
, "Should not happen\n" );
660 /*******************************************************************
665 static int ParseTopLevel(void)
669 while ((token
= GetToken()) != NULL
)
671 if (strcmp(token
, "name") == 0)
673 strcpy(DLLName
, GetToken());
675 if (!DLLFileName
[0]) sprintf( DLLFileName
, "%s.DLL", DLLName
);
677 else if (strcmp(token
, "file") == 0)
679 strcpy(DLLFileName
, GetToken());
680 strupper(DLLFileName
);
682 else if (strcmp(token
, "type") == 0)
685 if (!strcmp(token
, "win16" )) SpecType
= SPEC_WIN16
;
686 else if (!strcmp(token
, "win32" )) SpecType
= SPEC_WIN32
;
689 fprintf(stderr
, "%s:%d: Type must be 'win16' or 'win32'\n",
694 else if (strcmp(token
, "heap") == 0)
697 if (!IsNumberString(token
))
699 fprintf(stderr
, "%s:%d: Expected number after heap\n",
703 DLLHeapSize
= atoi(token
);
705 else if (IsNumberString(token
))
710 ordinal
= atoi(token
);
711 if ((rv
= ParseOrdinal(ordinal
)) < 0)
717 "%s:%d: Expected name, id, length or ordinal\n",
727 /*******************************************************************
730 * Store a list of ints into a byte array.
732 static int StoreVariableCode( unsigned char *buffer
, int size
, ORDDEF
*odp
)
739 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
740 buffer
[i
] = odp
->u
.var
.values
[i
];
743 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
744 ((unsigned short *)buffer
)[i
] = odp
->u
.var
.values
[i
];
747 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
748 ((unsigned int *)buffer
)[i
] = odp
->u
.var
.values
[i
];
751 return odp
->u
.var
.n_values
* size
;
755 /*******************************************************************
758 * Dump a byte stream into the assembly code.
760 static void DumpBytes( FILE *outfile
, const unsigned char *data
, int len
,
761 const char *section
, const char *label_start
)
764 if (section
) fprintf( outfile
, "\t%s\n", section
);
765 if (label_start
) fprintf( outfile
, "%s:\n", label_start
);
766 for (i
= 0; i
< len
; i
++)
768 if (!(i
& 0x0f)) fprintf( outfile
, "\t.byte " );
769 fprintf( outfile
, "%d", *data
++ );
771 fprintf( outfile
, "%c", ((i
& 0x0f) != 0x0f) ? ',' : '\n' );
773 fprintf( outfile
, "\n" );
777 /*******************************************************************
780 * Build the in-memory representation of a 16-bit NE module, and dump it
781 * as a byte stream into the assembly code.
783 static int BuildModule16( FILE *outfile
, int max_code_offset
,
784 int max_data_offset
)
790 SEGTABLEENTRY
*pSegment
;
797 * OFSTRUCT File information
798 * SEGTABLEENTRY Segment 1 (code)
799 * SEGTABLEENTRY Segment 2 (data)
800 * WORD[2] Resource table (empty)
801 * BYTE[2] Imported names (empty)
802 * BYTE[n] Resident names table
803 * BYTE[n] Entry table
806 buffer
= xmalloc( 0x10000 );
808 pModule
= (NE_MODULE
*)buffer
;
809 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
812 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_BUILTIN
| NE_FFLAGS_LIBMODULE
;
814 pModule
->heap_size
= DLLHeapSize
;
815 pModule
->stack_size
= 0;
820 pModule
->seg_count
= 2;
821 pModule
->modref_count
= 0;
822 pModule
->nrname_size
= 0;
823 pModule
->modref_table
= 0;
824 pModule
->nrname_fpos
= 0;
825 pModule
->moveable_entries
= 0;
826 pModule
->alignment
= 0;
827 pModule
->truetype
= 0;
828 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
829 pModule
->misc_flags
= 0;
830 pModule
->dlls_to_init
= 0;
831 pModule
->nrname_handle
= 0;
832 pModule
->min_swap_area
= 0;
833 pModule
->expected_version
= 0x030a;
834 pModule
->module32
= 0;
836 pModule
->self_loading_sel
= 0;
838 /* File information */
840 pFileInfo
= (OFSTRUCT
*)(pModule
+ 1);
841 pModule
->fileinfo
= (int)pFileInfo
- (int)pModule
;
842 memset( pFileInfo
, 0, sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
) );
843 pFileInfo
->cBytes
= sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
)
844 + strlen(DLLFileName
);
845 strcpy( pFileInfo
->szPathName
, DLLFileName
);
846 pstr
= (char *)pFileInfo
+ pFileInfo
->cBytes
+ 1;
850 pSegment
= (SEGTABLEENTRY
*)pstr
;
851 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
852 pSegment
->filepos
= 0;
853 pSegment
->size
= max_code_offset
;
855 pSegment
->minsize
= max_code_offset
;
856 pSegment
->selector
= 0;
859 pModule
->dgroup_entry
= (int)pSegment
- (int)pModule
;
860 pSegment
->filepos
= 0;
861 pSegment
->size
= max_data_offset
;
862 pSegment
->flags
= NE_SEGFLAGS_DATA
;
863 pSegment
->minsize
= max_data_offset
;
864 pSegment
->selector
= 0;
869 pword
= (WORD
*)pSegment
;
870 pModule
->res_table
= (int)pword
- (int)pModule
;
874 /* Imported names table */
876 pstr
= (char *)pword
;
877 pModule
->import_table
= (int)pstr
- (int)pModule
;
881 /* Resident names table */
883 pModule
->name_table
= (int)pstr
- (int)pModule
;
884 /* First entry is module name */
885 *pstr
= strlen(DLLName
);
886 strcpy( pstr
+ 1, DLLName
);
889 pstr
+= sizeof(WORD
);
890 /* Store all ordinals */
891 odp
= OrdinalDefinitions
+ 1;
892 for (i
= 1; i
<= Limit
; i
++, odp
++)
894 if (!odp
->name
[0]) continue;
895 *pstr
= strlen( odp
->name
);
896 strcpy( pstr
+ 1, odp
->name
);
897 strupper( pstr
+ 1 );
900 pstr
+= sizeof(WORD
);
906 pModule
->entry_table
= (int)pstr
- (int)pModule
;
908 odp
= OrdinalDefinitions
+ 1;
909 for (i
= 1; i
<= Limit
; i
++, odp
++)
921 selector
= 1; /* Code selector */
927 selector
= 2; /* Data selector */
931 selector
= 0xfe; /* Constant selector */
935 selector
= 0; /* Invalid selector */
939 /* create a new bundle if necessary */
940 if (!bundle
|| (bundle
[0] >= 254) || (bundle
[1] != selector
))
944 bundle
[1] = selector
;
952 *(WORD
*)pstr
= odp
->offset
;
953 pstr
+= sizeof(WORD
);
958 /* Dump the module content */
960 DumpBytes( outfile
, (char *)pModule
, (int)pstr
- (int)pModule
,
961 ".data", "Module_Start" );
962 return (int)pstr
- (int)pModule
;
966 /*******************************************************************
969 * Build a Win32 C file from a spec file.
971 static int BuildSpec32File( char * specfile
, FILE *outfile
)
974 int i
, nb_names
, nb_reg_funcs
= 0;
976 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
978 fprintf( outfile
, "#include \"builtin32.h\"\n\n" );
980 /* Output code for all stubs functions */
982 fprintf( outfile
, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
984 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
986 if (odp
->type
!= TYPE_STUB
) continue;
987 fprintf( outfile
, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
991 /* Output the DLL functions prototypes */
993 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
998 fprintf( outfile
, "extern void %s();\n", odp
->u
.vargs
.link_name
);
1001 fprintf( outfile
, "extern void %s();\n", odp
->u
.ext
.link_name
);
1006 fprintf( outfile
, "extern void %s();\n", odp
->u
.func
.link_name
);
1012 fprintf(stderr
,"build: function type %d not available for Win32\n",
1018 /* Output the DLL functions table */
1020 fprintf( outfile
, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1022 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1027 fprintf( outfile
, " 0" );
1030 fprintf( outfile
, " %s", odp
->u
.vargs
.link_name
);
1033 fprintf( outfile
, " %s", odp
->u
.ext
.link_name
);
1038 fprintf( outfile
, " %s", odp
->u
.func
.link_name
);
1041 fprintf( outfile
, " __stub_%d", i
);
1046 if (i
< Limit
) fprintf( outfile
, ",\n" );
1048 fprintf( outfile
, "\n};\n\n" );
1050 /* Output the DLL names table */
1053 fprintf( outfile
, "static const char * const FuncNames[] =\n{\n" );
1054 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1056 if (odp
->type
== TYPE_INVALID
) continue;
1057 if (nb_names
++) fprintf( outfile
, ",\n" );
1058 fprintf( outfile
, " \"%s\"", odp
->name
);
1060 fprintf( outfile
, "\n};\n\n" );
1062 /* Output the DLL argument types */
1064 fprintf( outfile
, "static const unsigned int ArgTypes[%d] =\n{\n",
1066 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1068 unsigned int j
, mask
= 0;
1069 if ((odp
->type
== TYPE_STDCALL
) || (odp
->type
== TYPE_CDECL
))
1070 for (j
= 0; odp
->u
.func
.arg_types
[j
]; j
++)
1072 if (odp
->u
.func
.arg_types
[j
] == 't') mask
|= 1<< (j
*2);
1073 if (odp
->u
.func
.arg_types
[j
] == 'W') mask
|= 2<< (j
*2);
1075 fprintf( outfile
, " %d", mask
);
1076 if (i
< Limit
) fprintf( outfile
, ",\n" );
1078 fprintf( outfile
, "\n};\n\n" );
1080 /* Output the DLL ordinals table */
1082 fprintf( outfile
, "static const unsigned short FuncOrdinals[] =\n{\n" );
1084 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1086 if (odp
->type
== TYPE_INVALID
) continue;
1087 if (nb_names
++) fprintf( outfile
, ",\n" );
1088 fprintf( outfile
, " %d", i
- Base
);
1090 fprintf( outfile
, "\n};\n\n" );
1092 /* Output the DLL functions arguments */
1094 fprintf( outfile
, "static const unsigned char FuncArgs[%d] =\n{\n",
1096 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1102 args
= (unsigned char)strlen(odp
->u
.func
.arg_types
);
1105 args
= 0x80 | (unsigned char)strlen(odp
->u
.func
.arg_types
);
1115 fprintf( outfile
, " 0x%02x", args
);
1116 if (i
< Limit
) fprintf( outfile
, ",\n" );
1118 fprintf( outfile
, "\n};\n\n" );
1120 /* Output the DLL descriptor */
1122 fprintf( outfile
, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1124 fprintf( outfile
, " \"%s\",\n", DLLName
);
1125 fprintf( outfile
, " %d,\n", Base
);
1126 fprintf( outfile
, " %d,\n", Limit
- Base
+ 1 );
1127 fprintf( outfile
, " %d,\n", nb_names
);
1128 fprintf( outfile
, " %d,\n", nb_reg_funcs
);
1140 /*******************************************************************
1143 * Build a Win16 assembly file from a spec file.
1145 static int BuildSpec16File( char * specfile
, FILE *outfile
)
1149 int code_offset
, data_offset
, module_size
;
1150 unsigned char *data
;
1152 data
= (unsigned char *)xmalloc( 0x10000 );
1153 memset( data
, 0, 16 );
1156 fprintf( outfile
, "/* File generated automatically; do not edit! */\n" );
1157 fprintf( outfile
, "\t.text\n" );
1158 fprintf( outfile
, "Code_Start:\n" );
1161 odp
= OrdinalDefinitions
;
1162 for (i
= 0; i
<= Limit
; i
++, odp
++)
1167 odp
->offset
= 0xffff;
1171 odp
->offset
= LOWORD(odp
->u
.abs
.value
);
1175 odp
->offset
= data_offset
;
1176 data_offset
+= StoreVariableCode( data
+ data_offset
, 1, odp
);
1180 odp
->offset
= data_offset
;
1181 data_offset
+= StoreVariableCode( data
+ data_offset
, 2, odp
);
1185 odp
->offset
= data_offset
;
1186 data_offset
+= StoreVariableCode( data
+ data_offset
, 4, odp
);
1190 fprintf( outfile
,"/* %s.%d */\n", DLLName
, i
);
1191 fprintf( outfile
,"\tmovw $%d,%%ax\n",LOWORD(odp
->u
.ret
.ret_value
));
1192 fprintf( outfile
,"\tmovw $%d,%%dx\n",HIWORD(odp
->u
.ret
.ret_value
));
1193 fprintf( outfile
,"\t.byte 0x66\n");
1194 if (odp
->u
.ret
.arg_size
!= 0)
1195 fprintf( outfile
, "\tlret $%d\n\n", odp
->u
.ret
.arg_size
);
1198 fprintf( outfile
, "\tlret\n");
1199 fprintf( outfile
, "\tnop\n");
1200 fprintf( outfile
, "\tnop\n\n");
1202 odp
->offset
= code_offset
;
1203 code_offset
+= 12; /* Assembly code is 12 bytes long */
1209 case TYPE_PASCAL_16
:
1211 fprintf( outfile
, "/* %s.%d */\n", DLLName
, i
);
1212 fprintf( outfile
, "\tpushw %%bp\n" );
1213 fprintf( outfile
, "\tpushl $" PREFIX
"%s\n",odp
->u
.func
.link_name
);
1214 /* FreeBSD does not understand lcall, so do it the hard way */
1215 fprintf( outfile
, "\t.byte 0x9a\n" );
1216 fprintf( outfile
, "\t.long " PREFIX
"CallFrom16_%s_%s_%s\n",
1217 (odp
->type
== TYPE_CDECL
) ? "c" : "p",
1218 (odp
->type
== TYPE_REGISTER
) ? "regs" :
1219 (odp
->type
== TYPE_PASCAL_16
) ? "word" : "long",
1220 odp
->u
.func
.arg_types
);
1221 fprintf( outfile
, "\t.long 0x%08lx\n",
1222 MAKELONG( Code_Selector
, 0x9090 /* nop ; nop */ ) );
1223 odp
->offset
= code_offset
;
1224 code_offset
+= 16; /* Assembly code is 16 bytes long */
1228 fprintf(stderr
,"build: function type %d not available for Win16\n",
1234 if (!code_offset
) /* Make sure the code segment is not empty */
1236 fprintf( outfile
, "\t.byte 0\n" );
1240 /* Output data segment */
1242 DumpBytes( outfile
, data
, data_offset
, NULL
, "Data_Start" );
1244 /* Build the module */
1246 module_size
= BuildModule16( outfile
, code_offset
, data_offset
);
1248 /* Output the DLL descriptor */
1250 fprintf( outfile
, "\t.text\n" );
1251 fprintf( outfile
, "DLLName:\t" STRING
" \"%s\\0\"\n", DLLName
);
1252 fprintf( outfile
, "\t.align 4\n" );
1253 fprintf( outfile
, "\t.globl " PREFIX
"%s_Descriptor\n", DLLName
);
1254 fprintf( outfile
, PREFIX
"%s_Descriptor:\n", DLLName
);
1255 fprintf( outfile
, "\t.long DLLName\n" ); /* Name */
1256 fprintf( outfile
, "\t.long Module_Start\n" ); /* Module start */
1257 fprintf( outfile
, "\t.long %d\n", module_size
); /* Module size */
1258 fprintf( outfile
, "\t.long Code_Start\n" ); /* Code start */
1259 fprintf( outfile
, "\t.long Data_Start\n" ); /* Data start */
1264 /*******************************************************************
1267 * Build an assembly file from a spec file.
1269 static int BuildSpecFile( FILE *outfile
, char *specname
)
1271 SpecName
= specname
;
1272 SpecFp
= fopen( specname
, "r");
1275 fprintf(stderr
, "Could not open specification file, '%s'\n", specname
);
1279 if (ParseTopLevel() < 0) return -1;
1284 return BuildSpec16File( specname
, outfile
);
1286 return BuildSpec32File( specname
, outfile
);
1288 fprintf( stderr
, "%s: Missing 'type' declaration\n", specname
);
1294 /*******************************************************************
1295 * TransferArgs16To32
1297 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1298 * The 16-bit stack layout is:
1306 * For 'cdecl' argn up to arg1 are reversed.
1308 static int TransferArgs16To32( FILE *outfile
, char *args
, int usecdecl
)
1310 int i
, pos16
, pos32
;
1312 /* Copy the arguments */
1314 pos16
= 6; /* skip bp and return address */
1315 pos32
= usecdecl
? -(strlen(args
) * 4) : 0;
1317 for (i
= strlen(args
); i
> 0; i
--)
1319 if (!usecdecl
) pos32
-= 4;
1322 case 'w': /* word */
1323 fprintf( outfile
, "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1324 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1328 case 's': /* s_word */
1329 fprintf( outfile
, "\tmovswl %d(%%ebp),%%eax\n", pos16
);
1330 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1334 case 'l': /* long or segmented pointer */
1335 case 'T': /* segmented pointer to null-terminated string */
1336 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", pos16
);
1337 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1341 case 'p': /* linear pointer */
1342 case 't': /* linear pointer to null-terminated string */
1343 /* Get the selector */
1344 fprintf( outfile
, "\tmovw %d(%%ebp),%%ax\n", pos16
+ 2 );
1345 /* Get the selector base */
1346 fprintf( outfile
, "\tandl $0xfff8,%%eax\n" );
1347 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy(%%eax),%%eax\n" );
1348 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1349 /* Add the offset */
1350 fprintf( outfile
, "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1351 fprintf( outfile
, "\taddl %%eax,%d(%%ebx)\n", pos32
);
1356 fprintf( stderr
, "Unknown arg type '%c'\n", args
[i
-1] );
1358 if (usecdecl
) pos32
+= 4;
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
, "\tleal 2(%%ebp),%%eax\n" ); /* Get initial %sp */
1404 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1405 CONTEXTOFFSET(Esp
) - sizeof(CONTEXT
) );
1406 fprintf( outfile
, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1407 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1408 CONTEXTOFFSET(SegCs
) - sizeof(CONTEXT
) );
1409 fprintf( outfile
, "\tmovw %%fs,%%ax\n" );
1410 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1411 CONTEXTOFFSET(SegFs
) - sizeof(CONTEXT
) );
1412 fprintf( outfile
, "\tmovw %%gs,%%ax\n" );
1413 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1414 CONTEXTOFFSET(SegGs
) - sizeof(CONTEXT
) );
1415 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
1416 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1417 CONTEXTOFFSET(SegSs
) - sizeof(CONTEXT
) );
1419 fprintf( outfile
, "\tfsave %d(%%ebx)\n",
1420 CONTEXTOFFSET(FloatSave
) - sizeof(CONTEXT
) );
1425 /*******************************************************************
1428 * Restore the registers from the context structure.
1430 static void RestoreContext16( FILE *outfile
)
1432 /* Get the 32-bit stack pointer */
1434 fprintf( outfile
, "\tleal -%d(%%ebp),%%ebx\n",
1435 STRUCTOFFSET(STACK32FRAME
,ebp
) );
1437 /* Remove everything up to (including) the return address
1438 * from the 16-bit stack */
1440 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n",
1441 CONTEXTOFFSET(SegSs
) - sizeof(CONTEXT
) );
1442 fprintf( outfile
, "\tmovw %%ax,%%ss\n" );
1443 fprintf( outfile
, "\tmovl %d(%%ebx),%%esp\n",
1444 CONTEXTOFFSET(Esp
) - sizeof(CONTEXT
) );
1445 fprintf( outfile
, "\taddl $4,%%esp\n" ); /* Remove return address */
1447 /* Restore the registers */
1449 fprintf( outfile
, "\tmovl %d(%%ebx),%%ecx\n",
1450 CONTEXTOFFSET(Ecx
) - sizeof(CONTEXT
) );
1451 fprintf( outfile
, "\tmovl %d(%%ebx),%%edx\n",
1452 CONTEXTOFFSET(Edx
) - sizeof(CONTEXT
) );
1453 fprintf( outfile
, "\tmovl %d(%%ebx),%%esi\n",
1454 CONTEXTOFFSET(Esi
) - sizeof(CONTEXT
) );
1455 fprintf( outfile
, "\tmovl %d(%%ebx),%%edi\n",
1456 CONTEXTOFFSET(Edi
) - sizeof(CONTEXT
) );
1457 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebp\n",
1458 CONTEXTOFFSET(Ebp
) - sizeof(CONTEXT
) );
1459 fprintf( outfile
, "\tpushw %d(%%ebx)\n", /* Push new cs */
1460 CONTEXTOFFSET(SegCs
) - sizeof(CONTEXT
) );
1461 fprintf( outfile
, "\tpushw %d(%%ebx)\n", /* Push new ip */
1462 CONTEXTOFFSET(Eip
) - sizeof(CONTEXT
) );
1463 fprintf( outfile
, "\tpushl %d(%%ebx)\n", /* Push new ds */
1464 CONTEXTOFFSET(SegDs
) - sizeof(CONTEXT
) );
1465 fprintf( outfile
, "\tpushl %d(%%ebx)\n", /* Push new es */
1466 CONTEXTOFFSET(SegEs
) - sizeof(CONTEXT
) );
1467 fprintf( outfile
, "\tpushl %d(%%ebx)\n",
1468 CONTEXTOFFSET(EFlags
) - sizeof(CONTEXT
) );
1469 fprintf( outfile
, "\tpopfl\n" );
1470 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n",
1471 CONTEXTOFFSET(Eax
) - sizeof(CONTEXT
) );
1472 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebx\n",
1473 CONTEXTOFFSET(Ebx
) - sizeof(CONTEXT
) );
1474 fprintf( outfile
, "\tpopl %%es\n" ); /* Set es */
1475 fprintf( outfile
, "\tpopl %%ds\n" ); /* Set ds */
1479 /*******************************************************************
1480 * BuildCallFrom16Func
1482 * Build a 16-bit-to-Wine callback function. The syntax of the function
1483 * profile is: call_type_xxxxx, where 'call' is the letter 'c' or 'p' for C or
1484 * Pascal calling convention, 'type' is one of 'regs', 'word' or
1485 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1486 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1487 * 'T'=segmented pointer to null-terminated string).
1488 * For register functions, the arguments are ignored, but they are still
1489 * removed from the stack upon return.
1491 * Stack layout upon entry to the callback function:
1493 * (sp+18) word first 16-bit arg
1497 * (sp+8) long 32-bit entry point (used to store edx)
1498 * (sp+6) word high word of cs (always 0, used to store es)
1499 * (sp+4) word low word of cs of 16-bit entry point
1500 * (sp+2) word high word of ip (always 0, used to store ds)
1501 * (sp) word low word of ip of 16-bit entry point
1503 * Added on the stack:
1505 * (sp-8) long saved previous stack
1507 static void BuildCallFrom16Func( FILE *outfile
, char *profile
)
1513 char *args
= profile
+ 7;
1515 /* Parse function type */
1517 if (!strncmp( "c_", profile
, 2 )) cdecl = 1;
1518 else if (strncmp( "p_", profile
, 2 ))
1520 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1524 if (!strncmp( "word_", profile
+ 2, 5 )) short_ret
= 1;
1525 else if (!strncmp( "regs_", profile
+ 2, 5 )) reg_func
= 1;
1526 else if (strncmp( "long_", profile
+ 2, 5 ))
1528 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1532 /* Function header */
1534 fprintf( outfile
, "\n\t.align 4\n" );
1536 fprintf( outfile
, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX
"CallFrom16_%s\n",
1539 fprintf( outfile
, "\t.globl " PREFIX
"CallFrom16_%s\n", profile
);
1540 fprintf( outfile
, PREFIX
"CallFrom16_%s:\n", profile
);
1542 /* Setup bp to point to its copy on the stack */
1544 fprintf( outfile
, "\tpushl %%ebp\n" ); /* Save the full 32-bit ebp */
1545 fprintf( outfile
, "\tmovzwl %%sp,%%ebp\n" );
1546 fprintf( outfile
, "\taddw $16,%%bp\n" );
1548 /* Save 16-bit ds and es */
1550 /* Stupid FreeBSD assembler doesn't know these either */
1551 /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1552 fprintf( outfile
, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1553 /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1554 fprintf( outfile
, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1558 fprintf( outfile
, "\tpushl %%ebx\n" );
1560 /* Restore 32-bit segment registers */
1562 fprintf( outfile
, "\tmovw $0x%04x,%%bx\n", Data_Selector
);
1564 fprintf( outfile
, "\tdata16\n");
1566 fprintf( outfile
, "\tmovw %%bx,%%ds\n" );
1568 fprintf( outfile
, "\tdata16\n");
1570 fprintf( outfile
, "\tmovw %%bx,%%es\n" );
1571 fprintf( outfile
, "\tmovw " PREFIX
"CALLTO16_Current_fs,%%fs\n" );
1573 /* Get the 32-bit stack pointer from the TEB */
1575 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET
);
1577 /* Save the 16-bit stack */
1580 fprintf( outfile
,"\tdata16\n");
1582 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%ss,(%d)\n", STACKOFFSET
+ 2 );
1583 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%sp,(%d)\n", STACKOFFSET
);
1585 /* Transfer the arguments */
1587 if (reg_func
) BuildContext16( outfile
);
1588 else if (*args
) argsize
= TransferArgs16To32( outfile
, args
, cdecl );
1590 /* Get the address of the API function */
1592 fprintf( outfile
, "\tmovl -4(%%ebp),%%eax\n" );
1594 /* If necessary, save %edx over the API function address */
1596 if (!reg_func
&& short_ret
)
1597 fprintf( outfile
, "\tmovl %%edx,-4(%%ebp)\n" );
1599 /* Restore %ebx and store the 32-bit stack pointer instead */
1601 fprintf( outfile
, "\tmovl %%ebx,%%ebp\n" );
1602 fprintf( outfile
, "\tpopl %%ebx\n" );
1603 fprintf( outfile
, "\tpushl %%ebp\n" );
1605 /* Switch to the 32-bit stack */
1607 fprintf( outfile
, "\tpushl %%ds\n" );
1608 fprintf( outfile
, "\tpopl %%ss\n" );
1609 fprintf( outfile
, "\tleal -%d(%%ebp),%%esp\n",
1610 reg_func
? sizeof(CONTEXT
) : 4 * strlen(args
) );
1611 if (reg_func
) /* Push the address of the context struct */
1612 fprintf( outfile
, "\tpushl %%esp\n" );
1614 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1616 fprintf( outfile
, "\taddl $%d,%%ebp\n", STRUCTOFFSET(STACK32FRAME
,ebp
) );
1618 /* Print the debug information before the call */
1624 if (cdecl) ftype
|= 4;
1625 if (reg_func
) ftype
|= 2;
1626 if (short_ret
) ftype
|= 1;
1628 fprintf( outfile
, "\tpushl %%eax\n" );
1629 fprintf( outfile
, "\tpushl $Profile_%s\n", profile
);
1630 fprintf( outfile
, "\tpushl $%d\n", ftype
);
1631 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16\n" );
1632 fprintf( outfile
, "\tpopl %%eax\n" );
1633 fprintf( outfile
, "\tpopl %%eax\n" );
1634 fprintf( outfile
, "\tpopl %%eax\n" );
1637 /* Call the entry point */
1639 fprintf( outfile
, "\tcall *%%eax\n" );
1641 /* Print the debug information after the call */
1647 /* Push again the address of the context struct in case */
1648 /* it has been removed by an stdcall function */
1649 fprintf( outfile
, "\tleal -%d(%%ebp),%%esp\n",
1650 sizeof(CONTEXT
) + STRUCTOFFSET(STACK32FRAME
,ebp
) );
1651 fprintf( outfile
, "\tpushl %%esp\n" );
1653 fprintf( outfile
, "\tpushl %%eax\n" );
1654 fprintf( outfile
, "\tpushl $%d\n", reg_func
? 2 : (short_ret
? 1 : 0));
1655 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret\n" );
1656 fprintf( outfile
, "\tpopl %%eax\n" );
1657 fprintf( outfile
, "\tpopl %%eax\n" );
1660 /* Restore the 16-bit stack */
1663 fprintf( outfile
, "\tdata16\n");
1665 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2 );
1666 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET
);
1667 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
1668 fprintf( outfile
, "\tmovw %%fs," PREFIX
"CALLTO16_Current_fs\n" );
1672 /* Calc the arguments size */
1688 fprintf( stderr
, "Unknown arg type '%c'\n", *args
);
1693 /* Restore registers from the context structure */
1694 RestoreContext16( outfile
);
1698 /* Restore high 16 bits of ebp */
1699 fprintf( outfile
, "\tpopl %%ebp\n" );
1701 /* Restore ds and es */
1702 fprintf( outfile
, "\tincl %%esp\n" ); /* Remove ip */
1703 fprintf( outfile
, "\tincl %%esp\n" );
1704 fprintf( outfile
, "\tpopl %%edx\n" ); /* Remove cs and ds */
1705 fprintf( outfile
, "\tmovw %%dx,%%ds\n" ); /* and restore ds */
1706 fprintf( outfile
, "\t.byte 0x66\n\tpopl %%es\n" ); /* Restore es */
1708 if (short_ret
) fprintf( outfile
, "\tpopl %%edx\n" ); /* Restore edx */
1711 /* Get the return value into dx:ax */
1712 fprintf( outfile
, "\tmovl %%eax,%%edx\n" );
1713 fprintf( outfile
, "\tshrl $16,%%edx\n" );
1714 /* Remove API entry point */
1715 fprintf( outfile
, "\taddl $4,%%esp\n" );
1718 /* Restore low 16 bits of ebp */
1719 fprintf( outfile
, "\tpopw %%bp\n" );
1722 /* Remove the arguments and return */
1724 if (argsize
&& !cdecl)
1726 fprintf( outfile
, "\t.byte 0x66\n" );
1727 fprintf( outfile
, "\tlret $%d\n", argsize
);
1731 fprintf( outfile
, "\t.byte 0x66\n" );
1732 fprintf( outfile
, "\tlret\n" );
1737 /*******************************************************************
1740 * Build a Wine-to-16-bit callback function.
1742 * Stack frame of the callback function:
1746 * (ebp+8) func to call
1747 * (ebp+4) return address
1748 * (ebp) previous ebp
1750 * Prototypes for the CallTo16 functions:
1751 * extern WINAPI WORD CallTo16_word_xxx( FARPROC16 func, args... );
1752 * extern WINAPI LONG CallTo16_long_xxx( FARPROC16 func, args... );
1753 * extern WINAPI void CallTo16_sreg_( const CONTEXT *context, int nb_args );
1754 * extern WINAPI void CallTo16_lreg_( const CONTEXT *context, int nb_args );
1756 static void BuildCallTo16Func( FILE *outfile
, char *profile
)
1760 char *args
= profile
+ 5;
1762 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1763 else if (!strncmp( "sreg_", profile
, 5 )) reg_func
= 1;
1764 else if (!strncmp( "lreg_", profile
, 5 )) reg_func
= 2;
1765 else if (strncmp( "long_", profile
, 5 ))
1767 fprintf( stderr
, "Invalid function name '%s'.\n", profile
);
1771 /* Function header */
1773 fprintf( outfile
, "\n\t.align 4\n" );
1775 fprintf( outfile
, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX
"CallTo16_%s\n",
1778 fprintf( outfile
, "\t.globl " PREFIX
"CallTo16_%s\n", profile
);
1779 fprintf( outfile
, PREFIX
"CallTo16_%s:\n", profile
);
1783 fprintf( outfile
, "\tpushl %%ebp\n" );
1784 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
1786 /* Call the actual CallTo16 routine (simulate a lcall) */
1788 fprintf( outfile
, "\tpushl %%cs\n" );
1789 fprintf( outfile
, "\tcall do_callto16_%s\n", profile
);
1793 /* FIXME: this is a hack because of task.c */
1794 if (!strcmp( profile
, "word_" ))
1796 fprintf( outfile
, ".globl " PREFIX
"CALLTO16_Restore\n" );
1797 fprintf( outfile
, PREFIX
"CALLTO16_Restore:\n" );
1799 fprintf( outfile
, "\tpopl %%ebp\n" );
1800 fprintf( outfile
, "\tret $%d\n", 4 * strlen(args
) + 4 );
1802 /* Start of the actual CallTo16 routine */
1804 /* Save the 32-bit registers */
1806 fprintf( outfile
, "do_callto16_%s:\n", profile
);
1807 fprintf( outfile
, "\tpushl %%ebx\n" );
1808 fprintf( outfile
, "\tpushl %%ecx\n" );
1809 fprintf( outfile
, "\tpushl %%edx\n" );
1810 fprintf( outfile
, "\tpushl %%esi\n" );
1811 fprintf( outfile
, "\tpushl %%edi\n" );
1813 /* Print debugging info */
1817 /* Push the address of the first argument */
1818 fprintf( outfile
, "\tleal 8(%%ebp),%%eax\n" );
1819 fprintf( outfile
, "\tpushl $%d\n", reg_func
? -1 : strlen(args
) );
1820 fprintf( outfile
, "\tpushl %%eax\n" );
1821 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16\n" );
1822 fprintf( outfile
, "\tpopl %%eax\n" );
1823 fprintf( outfile
, "\tpopl %%eax\n" );
1826 /* Save the 32-bit stack and %fs */
1828 fprintf( outfile
, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET
);
1829 fprintf( outfile
, "\tmovl %%ebp,%%ebx\n" );
1830 fprintf( outfile
, "\tmovl %%esp,%%edx\n" );
1831 fprintf( outfile
, "\tmovw %%fs," PREFIX
"CALLTO16_Current_fs\n" );
1835 /* Switch to the 16-bit stack, saving the current %%esp, */
1836 /* and adding the specified offset to the new sp */
1837 fprintf( outfile
, "\t.byte 0x64\n\tmovzwl (%d),%%eax\n", STACKOFFSET
);
1838 fprintf( outfile
, "\tsubl 12(%%ebx),%%eax\n" ); /* Get the offset */
1840 fprintf( outfile
,"\tdata16\n");
1842 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2);
1843 fprintf( outfile
, "\tmovl %%eax,%%esp\n" );
1844 fprintf( outfile
, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET
);
1846 /* Get the registers. ebx is handled later on. */
1848 fprintf( outfile
, "\tmovl 8(%%ebx),%%ebx\n" );
1849 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs
) );
1850 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
1851 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp
) );
1852 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax
) );
1853 fprintf( outfile
, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx
) );
1854 fprintf( outfile
, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx
) );
1855 fprintf( outfile
, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi
) );
1856 fprintf( outfile
, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi
) );
1858 /* Push the return address
1859 * With sreg suffix, we push 16:16 address (normal lret)
1860 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
1863 fprintf( outfile
, "\tpushl " PREFIX
"CALLTO16_RetAddr_long\n" );
1866 fprintf( outfile
, "\tpushw $0\n" );
1867 fprintf( outfile
, "\tpushw " PREFIX
"CALLTO16_RetAddr_long+2\n" );
1868 fprintf( outfile
, "\tpushw $0\n" );
1869 fprintf( outfile
, "\tpushw " PREFIX
"CALLTO16_RetAddr_long\n" );
1872 /* Push the called routine address */
1874 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs
) );
1875 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip
) );
1877 /* Get the 16-bit ds */
1879 fprintf( outfile
, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs
) );
1880 /* Get ebx from the 32-bit stack */
1881 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(Ebx
) );
1882 fprintf( outfile
, "\tpopl %%ds\n" );
1884 else /* not a register function */
1886 int pos
= 12; /* first argument position */
1888 /* Switch to the 16-bit stack */
1890 fprintf( outfile
,"\tdata16\n");
1892 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2);
1893 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET
);
1894 fprintf( outfile
, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET
);
1896 /* Make %bp point to the previous stackframe (built by CallFrom16) */
1897 fprintf( outfile
, "\tmovzwl %%sp,%%ebp\n" );
1898 fprintf( outfile
, "\tleal %d(%%ebp),%%ebp\n",
1899 STRUCTOFFSET(STACK16FRAME
,bp
) );
1901 /* Transfer the arguments */
1907 case 'w': /* word */
1908 fprintf( outfile
, "\tpushw %d(%%ebx)\n", pos
);
1910 case 'l': /* long */
1911 fprintf( outfile
, "\tpushl %d(%%ebx)\n", pos
);
1914 fprintf( stderr
, "Unexpected case '%c' in BuildCallTo16Func\n",
1920 /* Push the return address */
1922 fprintf( outfile
, "\tpushl " PREFIX
"CALLTO16_RetAddr_%s\n",
1923 short_ret
? "word" : "long" );
1925 /* Push the called routine address */
1927 fprintf( outfile
, "\tpushl 8(%%ebx)\n" );
1929 /* Set %ds and %es (and %ax just in case) equal to %ss */
1931 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
1932 fprintf( outfile
, "\tmovw %%ax,%%ds\n" );
1933 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
1936 /* Jump to the called routine */
1938 fprintf( outfile
, "\t.byte 0x66\n" );
1939 fprintf( outfile
, "\tlret\n" );
1943 /*******************************************************************
1946 * Build the return code for 16-bit callbacks
1948 static void BuildRet16Func( FILE *outfile
)
1950 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Ret_word\n" );
1951 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Ret_long\n" );
1953 fprintf( outfile
, PREFIX
"CALLTO16_Ret_word:\n" );
1954 fprintf( outfile
, "\txorl %%edx,%%edx\n" );
1956 /* Put return value into %eax */
1958 fprintf( outfile
, PREFIX
"CALLTO16_Ret_long:\n" );
1959 fprintf( outfile
, "\tshll $16,%%edx\n" );
1960 fprintf( outfile
, "\tmovw %%ax,%%dx\n" );
1961 fprintf( outfile
, "\tmovl %%edx,%%eax\n" );
1963 /* Restore 32-bit segment registers */
1965 fprintf( outfile
, "\tmovw $0x%04x,%%bx\n", Data_Selector
);
1967 fprintf( outfile
, "\tdata16\n");
1969 fprintf( outfile
, "\tmovw %%bx,%%ds\n" );
1971 fprintf( outfile
, "\tdata16\n");
1973 fprintf( outfile
, "\tmovw %%bx,%%es\n" );
1974 fprintf( outfile
, "\tmovw " PREFIX
"CALLTO16_Current_fs,%%fs\n" );
1976 /* Restore the 32-bit stack */
1979 fprintf( outfile
, "\tdata16\n");
1981 fprintf( outfile
, "\tmovw %%bx,%%ss\n" );
1982 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET
);
1983 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
1985 /* Restore the 32-bit registers */
1987 fprintf( outfile
, "\tpopl %%edi\n" );
1988 fprintf( outfile
, "\tpopl %%esi\n" );
1989 fprintf( outfile
, "\tpopl %%edx\n" );
1990 fprintf( outfile
, "\tpopl %%ecx\n" );
1991 fprintf( outfile
, "\tpopl %%ebx\n" );
1993 /* Return to caller */
1995 fprintf( outfile
, "\tlret\n" );
1997 /* Declare the return address variables */
1999 fprintf( outfile
, "\t.data\n" );
2000 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_RetAddr_word\n" );
2001 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_RetAddr_long\n" );
2002 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Current_fs\n" );
2003 fprintf( outfile
, PREFIX
"CALLTO16_RetAddr_word:\t.long 0\n" );
2004 fprintf( outfile
, PREFIX
"CALLTO16_RetAddr_long:\t.long 0\n" );
2005 fprintf( outfile
, PREFIX
"CALLTO16_Current_fs:\t.long 0\n" );
2006 fprintf( outfile
, "\t.text\n" );
2010 /*******************************************************************
2011 * BuildCallTo32LargeStack
2013 * Build the function used to switch to the original 32-bit stack
2014 * before calling a 32-bit function from 32-bit code. This is used for
2015 * functions that need a large stack, like X bitmaps functions.
2017 * The generated function has the following prototype:
2018 * int xxx( int (*func)(), void *arg );
2020 * The pointer to the function can be retrieved by calling CALL32_Init,
2021 * which also takes care of saving the current 32-bit stack pointer.
2030 static void BuildCallTo32LargeStack( FILE *outfile
)
2032 /* Initialization function */
2034 fprintf( outfile
, "\n\t.align 4\n" );
2036 fprintf( outfile
, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX
"CALL32_Init\n");
2038 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Init\n" );
2039 fprintf( outfile
, "\t.type " PREFIX
"CALL32_Init,@function\n" );
2040 fprintf( outfile
, PREFIX
"CALL32_Init:\n" );
2041 fprintf( outfile
, "\tleal -256(%%esp),%%eax\n" );
2042 fprintf( outfile
, "\tmovl %%eax,CALL32_Original32_esp\n" );
2043 fprintf( outfile
, "\tmovl $CALL32_LargeStack,%%eax\n" );
2044 fprintf( outfile
, "\tret\n" );
2046 /* Function header */
2048 fprintf( outfile
, "\n\t.align 4\n" );
2050 fprintf( outfile
, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2052 fprintf( outfile
, "CALL32_LargeStack:\n" );
2056 fprintf( outfile
, "\tpushl %%ebp\n" );
2057 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2059 /* Switch to the original 32-bit stack pointer */
2061 fprintf( outfile
, "\tmovl CALL32_Original32_esp, %%esp\n" );
2063 /* Transfer the argument and call the function */
2065 fprintf( outfile
, "\tpushl 12(%%ebp)\n" );
2066 fprintf( outfile
, "\tcall *8(%%ebp)\n" );
2068 /* Restore registers and return */
2070 fprintf( outfile
, "\tmovl %%ebp,%%esp\n" );
2071 fprintf( outfile
, "\tpopl %%ebp\n" );
2072 fprintf( outfile
, "\tret\n" );
2076 fprintf( outfile
, "\t.data\n" );
2077 fprintf( outfile
, "CALL32_Original32_esp:\t.long 0\n" );
2078 fprintf( outfile
, "\t.text\n" );
2082 /*******************************************************************
2083 * BuildCallFrom32Regs
2085 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2086 * 'args' is the number of dword arguments.
2090 * (esp+208) ret addr (or relay addr when debugging_relay is on)
2091 * (esp+204) entry point
2092 * (esp+0) CONTEXT struct
2094 static void BuildCallFrom32Regs( FILE *outfile
)
2096 /* Function header */
2098 fprintf( outfile
, "\n\t.align 4\n" );
2100 fprintf( outfile
, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX
"CALL32_Regs\n" );
2102 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Regs\n" );
2103 fprintf( outfile
, PREFIX
"CALL32_Regs:\n" );
2105 /* Build the context structure */
2107 fprintf( outfile
, "\tpushw $0\n" );
2108 fprintf( outfile
, "\t.byte 0x66\n\tpushl %%ss\n" );
2109 fprintf( outfile
, "\tpushl %%eax\n" ); /* %esp place holder */
2110 fprintf( outfile
, "\tpushfl\n" );
2111 fprintf( outfile
, "\tpushw $0\n" );
2112 fprintf( outfile
, "\t.byte 0x66\n\tpushl %%cs\n" );
2113 fprintf( outfile
, "\tpushl 20(%%esp)\n" ); /* %eip at time of call */
2114 fprintf( outfile
, "\tpushl %%ebp\n" );
2116 fprintf( outfile
, "\tpushl %%eax\n" );
2117 fprintf( outfile
, "\tpushl %%ecx\n" );
2118 fprintf( outfile
, "\tpushl %%edx\n" );
2119 fprintf( outfile
, "\tpushl %%ebx\n" );
2120 fprintf( outfile
, "\tpushl %%esi\n" );
2121 fprintf( outfile
, "\tpushl %%edi\n" );
2123 fprintf( outfile
, "\txorl %%eax,%%eax\n" );
2124 fprintf( outfile
, "\tmovw %%ds,%%ax\n" );
2125 fprintf( outfile
, "\tpushl %%eax\n" );
2126 fprintf( outfile
, "\tmovw %%es,%%ax\n" );
2127 fprintf( outfile
, "\tpushl %%eax\n" );
2128 fprintf( outfile
, "\tmovw %%fs,%%ax\n" );
2129 fprintf( outfile
, "\tpushl %%eax\n" );
2130 fprintf( outfile
, "\tmovw %%gs,%%ax\n" );
2131 fprintf( outfile
, "\tpushl %%eax\n" );
2133 fprintf( outfile
, "\tleal -%d(%%esp),%%esp\n",
2134 sizeof(FLOATING_SAVE_AREA
) + 6 * sizeof(DWORD
) /* DR regs */ );
2135 fprintf( outfile
, "\tpushl $0x0001001f\n" ); /* ContextFlags */
2137 fprintf( outfile
, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave
) );
2139 fprintf( outfile
, "\tleal %d(%%esp),%%eax\n",
2140 sizeof(CONTEXT
) + 4 ); /* %esp at time of call */
2141 fprintf( outfile
, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Esp
) );
2143 fprintf( outfile
, "\tcall " PREFIX
"RELAY_CallFrom32Regs\n" );
2145 /* Restore the context structure */
2147 fprintf( outfile
, "\tfrstor %d(%%esp)\n", CONTEXTOFFSET(FloatSave
) );
2149 /* Store %eip value onto the new stack */
2151 fprintf( outfile
, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Eip
) );
2152 fprintf( outfile
, "\tmovl %d(%%esp),%%ebx\n", CONTEXTOFFSET(Esp
) );
2153 fprintf( outfile
, "\tmovl %%eax,0(%%ebx)\n" );
2155 /* Restore all registers */
2157 fprintf( outfile
, "\tleal %d(%%esp),%%esp\n",
2158 sizeof(FLOATING_SAVE_AREA
) + 7 * sizeof(DWORD
) );
2159 fprintf( outfile
, "\tpopl %%eax\n" );
2160 fprintf( outfile
, "\tmovw %%ax,%%gs\n" );
2161 fprintf( outfile
, "\tpopl %%eax\n" );
2162 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2163 fprintf( outfile
, "\tpopl %%eax\n" );
2164 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2165 fprintf( outfile
, "\tpopl %%eax\n" );
2166 fprintf( outfile
, "\tmovw %%ax,%%ds\n" );
2168 fprintf( outfile
, "\tpopl %%edi\n" );
2169 fprintf( outfile
, "\tpopl %%esi\n" );
2170 fprintf( outfile
, "\tpopl %%ebx\n" );
2171 fprintf( outfile
, "\tpopl %%edx\n" );
2172 fprintf( outfile
, "\tpopl %%ecx\n" );
2173 fprintf( outfile
, "\tpopl %%eax\n" );
2174 fprintf( outfile
, "\tpopl %%ebp\n" );
2175 fprintf( outfile
, "\tleal 8(%%esp),%%esp\n" ); /* skip %eip and %cs */
2176 fprintf( outfile
, "\tpopfl\n" );
2177 fprintf( outfile
, "\tpopl %%esp\n" );
2178 fprintf( outfile
, "\tret\n" );
2182 /*******************************************************************
2185 * Build the spec files
2187 static int BuildSpec( FILE *outfile
, int argc
, char *argv
[] )
2190 for (i
= 2; i
< argc
; i
++)
2191 if (BuildSpecFile( outfile
, argv
[i
] ) < 0) return -1;
2196 /*******************************************************************
2199 * Build the 16-bit-to-Wine callbacks
2201 static int BuildCallFrom16( FILE *outfile
, char * outname
, int argc
, char *argv
[] )
2208 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2209 fprintf( outfile
, "\t.text\n" );
2212 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2213 getcwd(buffer
, sizeof(buffer
));
2216 * The stabs help the internal debugger as they are an indication that it
2217 * is sensible to step into a thunk/trampoline.
2219 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2220 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2221 fprintf( outfile
, "\t.text\n" );
2222 fprintf( outfile
, "\t.align 4\n" );
2223 fprintf( outfile
, "Code_Start:\n\n" );
2226 /* Build the callback functions */
2228 for (i
= 2; i
< argc
; i
++) BuildCallFrom16Func( outfile
, argv
[i
] );
2230 /* Output the argument debugging strings */
2234 fprintf( outfile
, "/* Argument strings */\n" );
2235 for (i
= 2; i
< argc
; i
++)
2237 fprintf( outfile
, "Profile_%s:\t", argv
[i
] );
2238 fprintf( outfile
, STRING
" \"%s\\0\"\n", argv
[i
] + 7 );
2243 fprintf( outfile
, "\t.text\n");
2244 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2245 fprintf( outfile
, ".Letext:\n");
2252 /*******************************************************************
2255 * Build the Wine-to-16-bit callbacks
2257 static int BuildCallTo16( FILE *outfile
, char * outname
, int argc
, char *argv
[] )
2264 infile
= fopen( argv
[2], "r" );
2271 else infile
= stdin
;
2275 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2276 fprintf( outfile
, "\t.text\n" );
2279 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2280 getcwd(buffer
, sizeof(buffer
));
2283 * The stabs help the internal debugger as they are an indication that it
2284 * is sensible to step into a thunk/trampoline.
2286 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2287 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2288 fprintf( outfile
, "\t.text\n" );
2289 fprintf( outfile
, "\t.align 4\n" );
2290 fprintf( outfile
, "Code_Start:\n\n" );
2293 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Start\n" );
2294 fprintf( outfile
, PREFIX
"CALLTO16_Start:\n" );
2296 /* Build the callback functions */
2298 while (fgets( buffer
, sizeof(buffer
), infile
))
2300 if (strstr( buffer
, "### start build ###" )) break;
2302 while (fgets( buffer
, sizeof(buffer
), infile
))
2304 char *p
= strstr( buffer
, "CallTo16_" );
2307 char *profile
= p
+ strlen( "CallTo16_" );
2309 while ((*p
== '_') || isalpha(*p
)) p
++;
2311 BuildCallTo16Func( outfile
, profile
);
2313 if (strstr( buffer
, "### stop build ###" )) break;
2316 /* Output the 16-bit return code */
2318 BuildRet16Func( outfile
);
2320 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_End\n" );
2321 fprintf( outfile
, PREFIX
"CALLTO16_End:\n" );
2324 fprintf( outfile
, "\t.text\n");
2325 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2326 fprintf( outfile
, ".Letext:\n");
2334 /*******************************************************************
2337 * Build the 32-bit callbacks
2339 static int BuildCall32( FILE *outfile
, char * outname
)
2345 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2346 fprintf( outfile
, "\t.text\n" );
2351 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2352 getcwd(buffer
, sizeof(buffer
));
2355 * The stabs help the internal debugger as they are an indication that it
2356 * is sensible to step into a thunk/trampoline.
2358 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2359 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2360 fprintf( outfile
, "\t.text\n" );
2361 fprintf( outfile
, "\t.align 4\n" );
2362 fprintf( outfile
, "Code_Start:\n" );
2365 /* Build the 32-bit large stack callback */
2367 BuildCallTo32LargeStack( outfile
);
2369 /* Build the register callback function */
2371 BuildCallFrom32Regs( outfile
);
2374 fprintf( outfile
, "\t.text\n");
2375 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2376 fprintf( outfile
, ".Letext:\n");
2379 #else /* __i386__ */
2381 /* Just to avoid an empty file */
2382 fprintf( outfile
, "\t.long 0\n" );
2384 #endif /* __i386__ */
2389 /*******************************************************************
2392 static void usage(void)
2395 "usage: build [-o outfile] -spec SPECNAMES\n"
2396 " build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2397 " build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2398 " build [-o outfile] -call32\n" );
2403 /*******************************************************************
2406 int main(int argc
, char **argv
)
2408 char *outname
= NULL
;
2409 FILE *outfile
= stdout
;
2412 if (argc
< 2) usage();
2414 if (!strcmp( argv
[1], "-o" ))
2419 if (argc
< 2) usage();
2420 if (!(outfile
= fopen( outname
, "w" )))
2422 fprintf( stderr
, "Unable to create output file '%s'\n", outname
);
2427 /* Retrieve the selector values; this assumes that we are building
2428 * the asm files on the platform that will also run them. Probably
2429 * a safe assumption to make.
2431 GET_CS( Code_Selector
);
2432 GET_DS( Data_Selector
);
2434 if (!strcmp( argv
[1], "-spec" ))
2435 res
= BuildSpec( outfile
, argc
, argv
);
2436 else if (!strcmp( argv
[1], "-callfrom16" ))
2437 res
= BuildCallFrom16( outfile
, outname
, argc
, argv
);
2438 else if (!strcmp( argv
[1], "-callto16" ))
2439 res
= BuildCallTo16( outfile
, outname
, argc
, argv
);
2440 else if (!strcmp( argv
[1], "-call32" ))
2441 res
= BuildCall32( outfile
, outname
);