2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Martin von Loewis
4 * Copyright 1995, 1996, 1997 Alexandre Julliard
5 * Copyright 1997 Eric Youngdale
6 * Copyright 1999 Ulrich Weigand
23 #include "selectors.h"
24 #include "stackframe.h"
25 #include "builtin16.h"
28 #ifdef NEED_UNDERSCORE_PREFIX
34 #ifdef HAVE_ASM_STRING
35 # define STRING ".string"
37 # define STRING ".ascii"
40 #if defined(__GNUC__) && !defined(__svr4__)
48 TYPE_BYTE
, /* byte variable (Win16) */
49 TYPE_WORD
, /* word variable (Win16) */
50 TYPE_LONG
, /* long variable (Win16) */
51 TYPE_PASCAL_16
, /* pascal function with 16-bit return (Win16) */
52 TYPE_PASCAL
, /* pascal function with 32-bit return (Win16) */
53 TYPE_ABS
, /* absolute value (Win16) */
54 TYPE_REGISTER
, /* register function */
55 TYPE_INTERRUPT
, /* interrupt handler function (Win16) */
56 TYPE_STUB
, /* unimplemented stub */
57 TYPE_STDCALL
, /* stdcall function (Win32) */
58 TYPE_CDECL
, /* cdecl function (Win32) */
59 TYPE_VARARGS
, /* varargs function (Win32) */
60 TYPE_EXTERN
, /* external symbol (Win32) */
61 TYPE_FORWARD
, /* forwarded function (Win32) */
65 static const char * const TypeNames
[TYPE_NBTYPES
] =
67 "byte", /* TYPE_BYTE */
68 "word", /* TYPE_WORD */
69 "long", /* TYPE_LONG */
70 "pascal16", /* TYPE_PASCAL_16 */
71 "pascal", /* TYPE_PASCAL */
72 "equate", /* TYPE_ABS */
73 "register", /* TYPE_REGISTER */
74 "interrupt", /* TYPE_INTERRUPT */
75 "stub", /* TYPE_STUB */
76 "stdcall", /* TYPE_STDCALL */
77 "cdecl", /* TYPE_CDECL */
78 "varargs", /* TYPE_VARARGS */
79 "extern", /* TYPE_EXTERN */
80 "forward" /* TYPE_FORWARD */
83 #define MAX_ORDINALS 2048
84 #define MAX_IMPORTS 16
86 /* Callback function used for stub functions */
87 #define STUB_CALLBACK \
88 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
142 static ORDDEF EntryPoints
[MAX_ORDINALS
];
143 static ORDDEF
*Ordinals
[MAX_ORDINALS
];
144 static ORDDEF
*Names
[MAX_ORDINALS
];
146 static SPEC_TYPE SpecType
= SPEC_INVALID
;
147 static char DLLName
[80];
148 static char DLLFileName
[80];
149 static int Limit
= 0;
150 static int Base
= MAX_ORDINALS
;
151 static int DLLHeapSize
= 0;
153 static WORD Code_Selector
, Data_Selector
;
154 static char DLLInitFunc
[80];
155 static char *DLLImports
[MAX_IMPORTS
];
156 static char rsrc_name
[80];
157 static int nb_imports
;
158 static int nb_entry_points
;
160 static const char *input_file_name
;
161 static const char *output_file_name
;
163 char *ParseBuffer
= NULL
;
168 static int UsePIC
= 0;
170 static int debugging
= 1;
172 /* Offset of a structure field relative to the start of the struct */
173 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
175 /* Offset of register relative to the start of the CONTEXT struct */
176 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT86,reg)
178 /* Offset of register relative to the start of the STACK16FRAME struct */
179 #define STACK16OFFSET(reg) STRUCTOFFSET(STACK16FRAME,reg)
181 /* Offset of register relative to the start of the STACK32FRAME struct */
182 #define STACK32OFFSET(reg) STRUCTOFFSET(STACK32FRAME,reg)
184 /* Offset of the stack pointer relative to %fs:(0) */
185 #define STACKOFFSET (STRUCTOFFSET(TEB,cur_stack))
187 static void BuildCallFrom16Func( FILE *outfile
, char *profile
, char *prefix
, int local
);
189 static void *xmalloc (size_t size
)
193 res
= malloc (size
? size
: 1);
196 fprintf (stderr
, "Virtual memory exhausted.\n");
197 if (output_file_name
) unlink( output_file_name
);
204 static void *xrealloc (void *ptr
, size_t size
)
206 void *res
= realloc (ptr
, size
);
209 fprintf (stderr
, "Virtual memory exhausted.\n");
210 if (output_file_name
) unlink( output_file_name
);
216 static char *xstrdup( const char *str
)
218 char *res
= strdup( str
);
221 fprintf (stderr
, "Virtual memory exhausted.\n");
222 if (output_file_name
) unlink( output_file_name
);
228 static void fatal_error( const char *msg
, ... )
231 va_start( valist
, msg
);
232 fprintf( stderr
, "%s:%d: ", input_file_name
, Line
);
233 vfprintf( stderr
, msg
, valist
);
235 if (output_file_name
) unlink( output_file_name
);
239 static int IsNumberString(char *s
)
248 static char *strupper(char *s
)
252 for(p
= s
; *p
!= '\0'; p
++)
258 static char * GetTokenInLine(void)
263 if (ParseNext
!= ParseBuffer
)
265 if (ParseSaveChar
== '\0')
267 *ParseNext
= ParseSaveChar
;
271 * Remove initial white space.
273 for (p
= ParseNext
; isspace(*p
); p
++)
276 if ((*p
== '\0') || (*p
== '#'))
283 if (*token
!= '(' && *token
!= ')')
284 while (*p
!= '\0' && *p
!= '(' && *p
!= ')' && !isspace(*p
))
294 static char * GetToken(void)
298 if (ParseBuffer
== NULL
)
300 ParseBuffer
= xmalloc(512);
301 ParseNext
= ParseBuffer
;
305 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
307 if (ParseBuffer
[0] != '#')
312 while ((token
= GetTokenInLine()) == NULL
)
314 ParseNext
= ParseBuffer
;
318 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
320 if (ParseBuffer
[0] != '#')
329 static int name_compare( const void *name1
, const void *name2
)
331 ORDDEF
*odp1
= *(ORDDEF
**)name1
;
332 ORDDEF
*odp2
= *(ORDDEF
**)name2
;
333 return strcmp( odp1
->name
, odp2
->name
);
336 /*******************************************************************
339 * Assign ordinals to all entry points.
341 static void AssignOrdinals(void)
345 /* sort the list of names */
346 qsort( Names
, nb_names
, sizeof(Names
[0]), name_compare
);
348 /* check for duplicate names */
349 for (i
= 0; i
< nb_names
- 1; i
++)
351 if (!strcmp( Names
[i
]->name
, Names
[i
+1]->name
))
353 Line
= MAX( Names
[i
]->lineno
, Names
[i
+1]->lineno
);
354 fatal_error( "'%s' redefined (previous definition at line %d)\n",
355 Names
[i
]->name
, MIN( Names
[i
]->lineno
, Names
[i
+1]->lineno
) );
359 /* start assigning from Base, or from 1 if no ordinal defined yet */
360 if (Base
== MAX_ORDINALS
) Base
= 1;
361 for (i
= 0, ordinal
= Base
; i
< nb_names
; i
++)
363 if (Names
[i
]->ordinal
!= -1) continue; /* already has an ordinal */
364 while (Ordinals
[ordinal
]) ordinal
++;
365 if (ordinal
>= MAX_ORDINALS
)
367 Line
= Names
[i
]->lineno
;
368 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS
);
370 Names
[i
]->ordinal
= ordinal
;
371 Ordinals
[ordinal
] = Names
[i
];
373 if (ordinal
> Limit
) Limit
= ordinal
;
377 /*******************************************************************
380 * Parse a variable definition.
382 static void ParseVariable( ORDDEF
*odp
)
387 int value_array_size
;
389 char *token
= GetToken();
390 if (*token
!= '(') fatal_error( "Expected '(' got '%s'\n", token
);
393 value_array_size
= 25;
394 value_array
= xmalloc(sizeof(*value_array
) * value_array_size
);
396 while ((token
= GetToken()) != NULL
)
401 value_array
[n_values
++] = strtol(token
, &endptr
, 0);
402 if (n_values
== value_array_size
)
404 value_array_size
+= 25;
405 value_array
= xrealloc(value_array
,
406 sizeof(*value_array
) * value_array_size
);
409 if (endptr
== NULL
|| *endptr
!= '\0')
410 fatal_error( "Expected number value, got '%s'\n", token
);
414 fatal_error( "End of file in variable declaration\n" );
416 odp
->u
.var
.n_values
= n_values
;
417 odp
->u
.var
.values
= xrealloc(value_array
, sizeof(*value_array
) * n_values
);
421 /*******************************************************************
422 * ParseExportFunction
424 * Parse a function definition.
426 static void ParseExportFunction( ORDDEF
*odp
)
434 if (odp
->type
== TYPE_STDCALL
)
435 fatal_error( "'stdcall' not supported for Win16\n" );
436 if (odp
->type
== TYPE_VARARGS
)
437 fatal_error( "'varargs' not supported for Win16\n" );
440 if ((odp
->type
== TYPE_PASCAL
) || (odp
->type
== TYPE_PASCAL_16
))
441 fatal_error( "'pascal' not supported for Win32\n" );
448 if (*token
!= '(') fatal_error( "Expected '(' got '%s'\n", token
);
450 for (i
= 0; i
< sizeof(odp
->u
.func
.arg_types
)-1; i
++)
456 if (!strcmp(token
, "word"))
457 odp
->u
.func
.arg_types
[i
] = 'w';
458 else if (!strcmp(token
, "s_word"))
459 odp
->u
.func
.arg_types
[i
] = 's';
460 else if (!strcmp(token
, "long") || !strcmp(token
, "segptr"))
461 odp
->u
.func
.arg_types
[i
] = 'l';
462 else if (!strcmp(token
, "ptr"))
463 odp
->u
.func
.arg_types
[i
] = 'p';
464 else if (!strcmp(token
, "str"))
465 odp
->u
.func
.arg_types
[i
] = 't';
466 else if (!strcmp(token
, "wstr"))
467 odp
->u
.func
.arg_types
[i
] = 'W';
468 else if (!strcmp(token
, "segstr"))
469 odp
->u
.func
.arg_types
[i
] = 'T';
470 else if (!strcmp(token
, "double"))
472 odp
->u
.func
.arg_types
[i
++] = 'l';
473 odp
->u
.func
.arg_types
[i
] = 'l';
475 else fatal_error( "Unknown variable type '%s'\n", token
);
477 if (SpecType
== SPEC_WIN32
)
479 if (strcmp(token
, "long") &&
480 strcmp(token
, "ptr") &&
481 strcmp(token
, "str") &&
482 strcmp(token
, "wstr") &&
483 strcmp(token
, "double"))
485 fatal_error( "Type '%s' not supported for Win32\n", token
);
489 if ((*token
!= ')') || (i
>= sizeof(odp
->u
.func
.arg_types
)))
490 fatal_error( "Too many arguments\n" );
492 odp
->u
.func
.arg_types
[i
] = '\0';
493 if ((odp
->type
== TYPE_STDCALL
) && !i
)
494 odp
->type
= TYPE_CDECL
; /* stdcall is the same as cdecl for 0 args */
495 strcpy(odp
->u
.func
.link_name
, GetToken());
499 /*******************************************************************
502 * Parse an 'equate' definition.
504 static void ParseEquate( ORDDEF
*odp
)
508 char *token
= GetToken();
509 int value
= strtol(token
, &endptr
, 0);
510 if (endptr
== NULL
|| *endptr
!= '\0')
511 fatal_error( "Expected number value, got '%s'\n", token
);
512 if (SpecType
== SPEC_WIN32
)
513 fatal_error( "'equate' not supported for Win32\n" );
514 odp
->u
.abs
.value
= value
;
518 /*******************************************************************
521 * Parse a 'stub' definition.
523 static void ParseStub( ORDDEF
*odp
)
525 odp
->u
.func
.arg_types
[0] = '\0';
526 strcpy( odp
->u
.func
.link_name
, STUB_CALLBACK
);
530 /*******************************************************************
533 * Parse an 'interrupt' definition.
535 static void ParseInterrupt( ORDDEF
*odp
)
539 if (SpecType
== SPEC_WIN32
)
540 fatal_error( "'interrupt' not supported for Win32\n" );
543 if (*token
!= '(') fatal_error( "Expected '(' got '%s'\n", token
);
546 if (*token
!= ')') fatal_error( "Expected ')' got '%s'\n", token
);
548 odp
->u
.func
.arg_types
[0] = '\0';
549 strcpy( odp
->u
.func
.link_name
, GetToken() );
553 /*******************************************************************
556 * Parse an 'extern' definition.
558 static void ParseExtern( ORDDEF
*odp
)
560 if (SpecType
== SPEC_WIN16
) fatal_error( "'extern' not supported for Win16\n" );
561 strcpy( odp
->u
.ext
.link_name
, GetToken() );
565 /*******************************************************************
568 * Parse a 'forward' definition.
570 static void ParseForward( ORDDEF
*odp
)
572 if (SpecType
== SPEC_WIN16
) fatal_error( "'forward' not supported for Win16\n" );
573 strcpy( odp
->u
.fwd
.link_name
, GetToken() );
577 /*******************************************************************
580 * Parse an ordinal definition.
582 static void ParseOrdinal(int ordinal
)
586 ORDDEF
*odp
= &EntryPoints
[nb_entry_points
++];
588 if (!(token
= GetToken())) fatal_error( "Expected type after ordinal\n" );
590 for (odp
->type
= 0; odp
->type
< TYPE_NBTYPES
; odp
->type
++)
591 if (TypeNames
[odp
->type
] && !strcmp( token
, TypeNames
[odp
->type
] ))
594 if (odp
->type
>= TYPE_NBTYPES
)
595 fatal_error( "Expected type after ordinal, found '%s' instead\n", token
);
597 if (!(token
= GetToken())) fatal_error( "Expected name after type\n" );
599 strcpy( odp
->name
, token
);
601 odp
->ordinal
= ordinal
;
608 ParseVariable( odp
);
611 ParseExportFunction( odp
);
613 /* ignore Win32 'register' routines on non-Intel archs */
614 if (SpecType
== SPEC_WIN32
)
626 ParseExportFunction( odp
);
629 ParseInterrupt( odp
);
649 if (ordinal
>= MAX_ORDINALS
) fatal_error( "Ordinal number %d too large\n", ordinal
);
650 if (ordinal
> Limit
) Limit
= ordinal
;
651 if (ordinal
< Base
) Base
= ordinal
;
652 odp
->ordinal
= ordinal
;
653 Ordinals
[ordinal
] = odp
;
656 if (!strcmp( odp
->name
, "@" ))
659 fatal_error( "Nameless function needs an explicit ordinal number\n" );
660 if (SpecType
!= SPEC_WIN32
)
661 fatal_error( "Nameless functions not supported for Win16\n" );
664 else Names
[nb_names
++] = odp
;
668 /*******************************************************************
673 static void ParseTopLevel(void)
677 while ((token
= GetToken()) != NULL
)
679 if (strcmp(token
, "name") == 0)
681 strcpy(DLLName
, GetToken());
683 if (!DLLFileName
[0]) sprintf( DLLFileName
, "%s.DLL", DLLName
);
685 else if (strcmp(token
, "file") == 0)
687 strcpy(DLLFileName
, GetToken());
688 strupper(DLLFileName
);
690 else if (strcmp(token
, "type") == 0)
693 if (!strcmp(token
, "win16" )) SpecType
= SPEC_WIN16
;
694 else if (!strcmp(token
, "win32" )) SpecType
= SPEC_WIN32
;
695 else fatal_error( "Type must be 'win16' or 'win32'\n" );
697 else if (strcmp(token
, "heap") == 0)
700 if (!IsNumberString(token
)) fatal_error( "Expected number after heap\n" );
701 DLLHeapSize
= atoi(token
);
703 else if (strcmp(token
, "init") == 0)
705 strcpy(DLLInitFunc
, GetToken());
706 if (SpecType
== SPEC_WIN16
)
707 fatal_error( "init cannot be used for Win16 spec files\n" );
709 fatal_error( "Expected function name after init\n" );
711 else if (strcmp(token
, "import") == 0)
713 if (nb_imports
>= MAX_IMPORTS
)
714 fatal_error( "Too many imports (limit %d)\n", MAX_IMPORTS
);
715 if (SpecType
!= SPEC_WIN32
)
716 fatal_error( "Imports not supported for Win16\n" );
717 DLLImports
[nb_imports
++] = xstrdup(GetToken());
719 else if (strcmp(token
, "rsrc") == 0)
721 strcpy( rsrc_name
, GetToken() );
722 strcat( rsrc_name
, "_ResourceDescriptor" );
724 else if (strcmp(token
, "@") == 0)
726 if (SpecType
!= SPEC_WIN32
)
727 fatal_error( "'@' ordinals not supported for Win16\n" );
730 else if (IsNumberString(token
))
732 ParseOrdinal( atoi(token
) );
735 fatal_error( "Expected name, id, length or ordinal\n" );
740 /*******************************************************************
743 * Store a list of ints into a byte array.
745 static int StoreVariableCode( unsigned char *buffer
, int size
, ORDDEF
*odp
)
752 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
753 buffer
[i
] = odp
->u
.var
.values
[i
];
756 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
757 ((unsigned short *)buffer
)[i
] = odp
->u
.var
.values
[i
];
760 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
761 ((unsigned int *)buffer
)[i
] = odp
->u
.var
.values
[i
];
764 return odp
->u
.var
.n_values
* size
;
768 /*******************************************************************
771 * Dump a byte stream into the assembly code.
773 static void DumpBytes( FILE *outfile
, const unsigned char *data
, int len
,
778 fprintf( outfile
, "\nstatic BYTE %s[] = \n{", label
);
780 for (i
= 0; i
< len
; i
++)
782 if (!(i
& 0x0f)) fprintf( outfile
, "\n " );
783 fprintf( outfile
, "%d", *data
++ );
784 if (i
< len
- 1) fprintf( outfile
, ", " );
786 fprintf( outfile
, "\n};\n" );
790 /*******************************************************************
793 * Build the in-memory representation of a 16-bit NE module, and dump it
794 * as a byte stream into the assembly code.
796 static int BuildModule16( FILE *outfile
, int max_code_offset
,
797 int max_data_offset
)
802 SEGTABLEENTRY
*pSegment
;
806 ET_BUNDLE
*bundle
= 0;
811 * OFSTRUCT File information
812 * SEGTABLEENTRY Segment 1 (code)
813 * SEGTABLEENTRY Segment 2 (data)
814 * WORD[2] Resource table (empty)
815 * BYTE[2] Imported names (empty)
816 * BYTE[n] Resident names table
817 * BYTE[n] Entry table
820 buffer
= xmalloc( 0x10000 );
822 pModule
= (NE_MODULE
*)buffer
;
823 memset( pModule
, 0, sizeof(*pModule
) );
824 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
827 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_BUILTIN
| NE_FFLAGS_LIBMODULE
;
829 pModule
->heap_size
= DLLHeapSize
;
830 pModule
->stack_size
= 0;
835 pModule
->seg_count
= 2;
836 pModule
->modref_count
= 0;
837 pModule
->nrname_size
= 0;
838 pModule
->modref_table
= 0;
839 pModule
->nrname_fpos
= 0;
840 pModule
->moveable_entries
= 0;
841 pModule
->alignment
= 0;
842 pModule
->truetype
= 0;
843 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
844 pModule
->misc_flags
= 0;
845 pModule
->dlls_to_init
= 0;
846 pModule
->nrname_handle
= 0;
847 pModule
->min_swap_area
= 0;
848 pModule
->expected_version
= 0;
849 pModule
->module32
= 0;
851 pModule
->self_loading_sel
= 0;
853 /* File information */
855 pFileInfo
= (OFSTRUCT
*)(pModule
+ 1);
856 pModule
->fileinfo
= (int)pFileInfo
- (int)pModule
;
857 memset( pFileInfo
, 0, sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
) );
858 pFileInfo
->cBytes
= sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
)
859 + strlen(DLLFileName
);
860 strcpy( pFileInfo
->szPathName
, DLLFileName
);
861 pstr
= (char *)pFileInfo
+ pFileInfo
->cBytes
+ 1;
863 #ifdef __i386__ /* FIXME: Alignment problems! */
867 pSegment
= (SEGTABLEENTRY
*)pstr
;
868 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
869 pSegment
->filepos
= 0;
870 pSegment
->size
= max_code_offset
;
872 pSegment
->minsize
= max_code_offset
;
876 pModule
->dgroup_entry
= (int)pSegment
- (int)pModule
;
877 pSegment
->filepos
= 0;
878 pSegment
->size
= max_data_offset
;
879 pSegment
->flags
= NE_SEGFLAGS_DATA
;
880 pSegment
->minsize
= max_data_offset
;
886 pword
= (WORD
*)pSegment
;
887 pModule
->res_table
= (int)pword
- (int)pModule
;
891 /* Imported names table */
893 pstr
= (char *)pword
;
894 pModule
->import_table
= (int)pstr
- (int)pModule
;
898 /* Resident names table */
900 pModule
->name_table
= (int)pstr
- (int)pModule
;
901 /* First entry is module name */
902 *pstr
= strlen(DLLName
);
903 strcpy( pstr
+ 1, DLLName
);
906 pstr
+= sizeof(WORD
);
907 /* Store all ordinals */
908 for (i
= 1; i
<= Limit
; i
++)
910 ORDDEF
*odp
= Ordinals
[i
];
911 if (!odp
|| !odp
->name
[0]) continue;
912 *pstr
= strlen( odp
->name
);
913 strcpy( pstr
+ 1, odp
->name
);
914 strupper( pstr
+ 1 );
917 pstr
+= sizeof(WORD
);
923 pModule
->entry_table
= (int)pstr
- (int)pModule
;
924 for (i
= 1; i
<= Limit
; i
++)
927 ORDDEF
*odp
= Ordinals
[i
];
938 selector
= 1; /* Code selector */
944 selector
= 2; /* Data selector */
948 selector
= 0xfe; /* Constant selector */
952 selector
= 0; /* Invalid selector */
959 if ( bundle
&& bundle
->last
+1 == i
)
964 bundle
->next
= (char *)pstr
- (char *)pModule
;
966 bundle
= (ET_BUNDLE
*)pstr
;
970 pstr
+= sizeof(ET_BUNDLE
);
973 /* FIXME: is this really correct ?? */
974 entry
= (ET_ENTRY
*)pstr
;
975 entry
->type
= 0xff; /* movable */
976 entry
->flags
= 3; /* exported & public data */
977 entry
->segnum
= selector
;
978 entry
->offs
= odp
->offset
;
979 pstr
+= sizeof(ET_ENTRY
);
984 /* Dump the module content */
986 DumpBytes( outfile
, (char *)pModule
, (int)pstr
- (int)pModule
,
988 return (int)pstr
- (int)pModule
;
992 /*******************************************************************
995 * Build a Win32 C file from a spec file.
997 static int BuildSpec32File( FILE *outfile
)
1000 int i
, fwd_size
= 0, have_regs
= FALSE
;
1004 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
1006 fprintf( outfile
, "#include \"builtin32.h\"\n\n" );
1007 fprintf( outfile
, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
1010 /* Output the DLL functions prototypes */
1012 for (i
= 0, odp
= EntryPoints
; i
< nb_entry_points
; i
++, odp
++)
1017 fprintf( outfile
, "extern void %s();\n", odp
->u
.ext
.link_name
);
1022 fprintf( outfile
, "extern void %s();\n", odp
->u
.func
.link_name
);
1025 fwd_size
+= strlen(odp
->u
.fwd
.link_name
) + 1;
1028 fprintf( outfile
, "extern void __regs_%d();\n", odp
->ordinal
);
1032 fprintf( outfile
, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
1033 odp
->ordinal
, DLLName
, odp
->ordinal
);
1036 fprintf(stderr
,"build: function type %d not available for Win32\n",
1042 /* Output LibMain function */
1043 if (DLLInitFunc
[0]) fprintf( outfile
, "extern void %s();\n", DLLInitFunc
);
1046 /* Output code for all register functions */
1050 fprintf( outfile
, "#ifndef __GNUC__\n" );
1051 fprintf( outfile
, "static void __asm__dummy() {\n" );
1052 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
1053 for (i
= 0, odp
= EntryPoints
; i
< nb_entry_points
; i
++, odp
++)
1055 if (odp
->type
!= TYPE_REGISTER
) continue;
1057 "__asm__(\".align 4\\n\\t\"\n"
1058 " \".type " PREFIX
"__regs_%d,@function\\n\\t\"\n"
1059 " \"" PREFIX
"__regs_%d:\\n\\t\"\n"
1060 " \"call " PREFIX
"CALL32_Regs\\n\\t\"\n"
1061 " \".long " PREFIX
"%s\\n\\t\"\n"
1062 " \".byte %d,%d\");\n",
1063 odp
->ordinal
, odp
->ordinal
, odp
->u
.func
.link_name
,
1064 4 * strlen(odp
->u
.func
.arg_types
),
1065 4 * strlen(odp
->u
.func
.arg_types
) );
1067 fprintf( outfile
, "#ifndef __GNUC__\n" );
1068 fprintf( outfile
, "}\n" );
1069 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
1072 /* Output the DLL functions table */
1074 fprintf( outfile
, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1076 for (i
= Base
; i
<= Limit
; i
++)
1078 ORDDEF
*odp
= Ordinals
[i
];
1079 if (!odp
) fprintf( outfile
, " 0" );
1080 else switch(odp
->type
)
1083 fprintf( outfile
, " %s", odp
->u
.ext
.link_name
);
1088 fprintf( outfile
, " %s", odp
->u
.func
.link_name
);
1091 fprintf( outfile
, " __stub_%d", i
);
1094 fprintf( outfile
, " __regs_%d", i
);
1097 fprintf( outfile
, " (ENTRYPOINT32)\"%s\"", odp
->u
.fwd
.link_name
);
1102 if (i
< Limit
) fprintf( outfile
, ",\n" );
1104 fprintf( outfile
, "\n};\n\n" );
1106 /* Output the DLL names table */
1108 fprintf( outfile
, "static const char * const FuncNames[%d] =\n{\n", nb_names
);
1109 for (i
= 0; i
< nb_names
; i
++)
1111 if (i
) fprintf( outfile
, ",\n" );
1112 fprintf( outfile
, " \"%s\"", Names
[i
]->name
);
1114 fprintf( outfile
, "\n};\n\n" );
1116 /* Output the DLL ordinals table */
1118 fprintf( outfile
, "static const unsigned short FuncOrdinals[%d] =\n{\n", nb_names
);
1119 for (i
= 0; i
< nb_names
; i
++)
1121 if (i
) fprintf( outfile
, ",\n" );
1122 fprintf( outfile
, " %d", Names
[i
]->ordinal
- Base
);
1124 fprintf( outfile
, "\n};\n\n" );
1126 /* Output the DLL argument types */
1128 fprintf( outfile
, "static const unsigned int ArgTypes[%d] =\n{\n",
1130 for (i
= Base
; i
<= Limit
; i
++)
1132 ORDDEF
*odp
= Ordinals
[i
];
1133 unsigned int j
, mask
= 0;
1135 ((odp
->type
== TYPE_STDCALL
) || (odp
->type
== TYPE_CDECL
) ||
1136 (odp
->type
== TYPE_REGISTER
)))
1137 for (j
= 0; odp
->u
.func
.arg_types
[j
]; j
++)
1139 if (odp
->u
.func
.arg_types
[j
] == 't') mask
|= 1<< (j
*2);
1140 if (odp
->u
.func
.arg_types
[j
] == 'W') mask
|= 2<< (j
*2);
1142 fprintf( outfile
, " %d", mask
);
1143 if (i
< Limit
) fprintf( outfile
, ",\n" );
1145 fprintf( outfile
, "\n};\n\n" );
1147 /* Output the DLL functions arguments */
1149 fprintf( outfile
, "static const unsigned char FuncArgs[%d] =\n{\n",
1151 for (i
= Base
; i
<= Limit
; i
++)
1153 unsigned char args
= 0xff;
1154 ORDDEF
*odp
= Ordinals
[i
];
1155 if (odp
) switch(odp
->type
)
1158 args
= (unsigned char)strlen(odp
->u
.func
.arg_types
);
1161 args
= 0x80 | (unsigned char)strlen(odp
->u
.func
.arg_types
);
1164 args
= 0x40 | (unsigned char)strlen(odp
->u
.func
.arg_types
);
1173 fprintf( outfile
, " 0x%02x", args
);
1174 if (i
< Limit
) fprintf( outfile
, ",\n" );
1176 fprintf( outfile
, "\n};\n\n" );
1178 /* Output the DLL imports */
1182 fprintf( outfile
, "static const char * const Imports[%d] =\n{\n", nb_imports
);
1183 for (i
= 0; i
< nb_imports
; i
++)
1185 fprintf( outfile
, " \"%s\"", DLLImports
[i
] );
1186 if (i
< nb_imports
-1) fprintf( outfile
, ",\n" );
1188 fprintf( outfile
, "\n};\n\n" );
1191 /* Output the DLL descriptor */
1193 if (rsrc_name
[0]) fprintf( outfile
, "extern const char %s[];\n\n", rsrc_name
);
1195 fprintf( outfile
, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1197 fprintf( outfile
, " \"%s\",\n", DLLName
);
1198 fprintf( outfile
, " \"%s\",\n", DLLFileName
);
1199 fprintf( outfile
, " %d,\n", Base
);
1200 fprintf( outfile
, " %d,\n", Limit
- Base
+ 1 );
1201 fprintf( outfile
, " %d,\n", nb_names
);
1202 fprintf( outfile
, " %d,\n", nb_imports
);
1203 fprintf( outfile
, " %d,\n", (fwd_size
+ 3) & ~3 );
1210 fprintf( outfile
, " %s,\n", nb_imports
? "Imports" : "0" );
1211 fprintf( outfile
, " %s,\n", DLLInitFunc
[0] ? DLLInitFunc
: "0" );
1212 fprintf( outfile
, " %s\n", rsrc_name
[0] ? rsrc_name
: "0" );
1213 fprintf( outfile
, "};\n" );
1217 /*******************************************************************
1220 static int Spec16TypeCompare( const void *e1
, const void *e2
)
1222 const ORDDEF
*odp1
= *(const ORDDEF
**)e1
;
1223 const ORDDEF
*odp2
= *(const ORDDEF
**)e2
;
1225 int type1
= (odp1
->type
== TYPE_CDECL
) ? 0
1226 : (odp1
->type
== TYPE_REGISTER
) ? 3
1227 : (odp1
->type
== TYPE_INTERRUPT
) ? 4
1228 : (odp1
->type
== TYPE_PASCAL_16
) ? 1 : 2;
1230 int type2
= (odp2
->type
== TYPE_CDECL
) ? 0
1231 : (odp2
->type
== TYPE_REGISTER
) ? 3
1232 : (odp2
->type
== TYPE_INTERRUPT
) ? 4
1233 : (odp2
->type
== TYPE_PASCAL_16
) ? 1 : 2;
1235 int retval
= type1
- type2
;
1237 retval
= strcmp( odp1
->u
.func
.arg_types
, odp2
->u
.func
.arg_types
);
1242 /*******************************************************************
1245 * Build a Win16 assembly file from a spec file.
1247 static int BuildSpec16File( FILE *outfile
)
1249 ORDDEF
**type
, **typelist
;
1250 int i
, nFuncs
, nTypes
;
1251 int code_offset
, data_offset
, module_size
;
1252 unsigned char *data
;
1256 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
1258 fprintf( outfile
, "#define __FLATCS__ 0x%04x\n", Code_Selector
);
1259 fprintf( outfile
, "#include \"builtin16.h\"\n\n" );
1261 data
= (unsigned char *)xmalloc( 0x10000 );
1262 memset( data
, 0, 16 );
1266 /* Build sorted list of all argument types, without duplicates */
1268 typelist
= (ORDDEF
**)calloc( Limit
+1, sizeof(ORDDEF
*) );
1270 for (i
= nFuncs
= 0; i
<= Limit
; i
++)
1272 ORDDEF
*odp
= Ordinals
[i
];
1277 case TYPE_INTERRUPT
:
1280 case TYPE_PASCAL_16
:
1282 typelist
[nFuncs
++] = odp
;
1289 qsort( typelist
, nFuncs
, sizeof(ORDDEF
*), Spec16TypeCompare
);
1292 while ( i
< nFuncs
)
1294 typelist
[nTypes
++] = typelist
[i
++];
1295 while ( i
< nFuncs
&& Spec16TypeCompare( typelist
+ i
, typelist
+ nTypes
-1 ) == 0 )
1299 /* Output CallFrom16 routines needed by this .spec file */
1301 for ( i
= 0; i
< nTypes
; i
++ )
1305 sprintf( profile
, "%s_%s_%s",
1306 (typelist
[i
]->type
== TYPE_CDECL
) ? "c" : "p",
1307 (typelist
[i
]->type
== TYPE_REGISTER
) ? "regs" :
1308 (typelist
[i
]->type
== TYPE_INTERRUPT
) ? "intr" :
1309 (typelist
[i
]->type
== TYPE_PASCAL_16
) ? "word" : "long",
1310 typelist
[i
]->u
.func
.arg_types
);
1312 BuildCallFrom16Func( outfile
, profile
, DLLName
, TRUE
);
1315 /* Output the DLL functions prototypes */
1317 for (i
= 0; i
<= Limit
; i
++)
1319 ORDDEF
*odp
= Ordinals
[i
];
1324 case TYPE_INTERRUPT
:
1327 case TYPE_PASCAL_16
:
1328 fprintf( outfile
, "extern void %s();\n", odp
->u
.func
.link_name
);
1335 /* Output code segment */
1337 fprintf( outfile
, "\nstatic struct\n{\n CALLFROM16 call[%d];\n"
1338 " ENTRYPOINT16 entry[%d];\n} Code_Segment = \n{\n {\n",
1342 for ( i
= 0; i
< nTypes
; i
++ )
1344 char profile
[101], *arg
;
1347 sprintf( profile
, "%s_%s_%s",
1348 (typelist
[i
]->type
== TYPE_CDECL
) ? "c" : "p",
1349 (typelist
[i
]->type
== TYPE_REGISTER
) ? "regs" :
1350 (typelist
[i
]->type
== TYPE_INTERRUPT
) ? "intr" :
1351 (typelist
[i
]->type
== TYPE_PASCAL_16
) ? "word" : "long",
1352 typelist
[i
]->u
.func
.arg_types
);
1354 if ( typelist
[i
]->type
!= TYPE_CDECL
)
1355 for ( arg
= typelist
[i
]->u
.func
.arg_types
; *arg
; arg
++ )
1358 case 'w': /* word */
1359 case 's': /* s_word */
1363 case 'l': /* long or segmented pointer */
1364 case 'T': /* segmented pointer to null-terminated string */
1365 case 'p': /* linear pointer */
1366 case 't': /* linear pointer to null-terminated string */
1371 if ( typelist
[i
]->type
== TYPE_INTERRUPT
)
1374 fprintf( outfile
, " CF16_%s( %s_CallFrom16_%s, %d, \"%s\" ),\n",
1375 ( typelist
[i
]->type
== TYPE_REGISTER
1376 || typelist
[i
]->type
== TYPE_INTERRUPT
)? "REGS":
1377 typelist
[i
]->type
== TYPE_PASCAL_16
? "WORD" : "LONG",
1378 DLLName
, profile
, argsize
, profile
);
1380 code_offset
+= sizeof(CALLFROM16
);
1382 fprintf( outfile
, " },\n {\n" );
1384 for (i
= 0; i
<= Limit
; i
++)
1386 ORDDEF
*odp
= Ordinals
[i
];
1391 odp
->offset
= LOWORD(odp
->u
.abs
.value
);
1395 odp
->offset
= data_offset
;
1396 data_offset
+= StoreVariableCode( data
+ data_offset
, 1, odp
);
1400 odp
->offset
= data_offset
;
1401 data_offset
+= StoreVariableCode( data
+ data_offset
, 2, odp
);
1405 odp
->offset
= data_offset
;
1406 data_offset
+= StoreVariableCode( data
+ data_offset
, 4, odp
);
1410 case TYPE_INTERRUPT
:
1413 case TYPE_PASCAL_16
:
1415 type
= bsearch( &odp
, typelist
, nTypes
, sizeof(ORDDEF
*), Spec16TypeCompare
);
1418 fprintf( outfile
, " /* %s.%d */ ", DLLName
, i
);
1419 fprintf( outfile
, "EP( %s, %d /* %s_%s_%s */ ),\n",
1420 odp
->u
.func
.link_name
,
1421 (type
-typelist
)*sizeof(CALLFROM16
) -
1422 (code_offset
+ sizeof(ENTRYPOINT16
)),
1423 (odp
->type
== TYPE_CDECL
) ? "c" : "p",
1424 (odp
->type
== TYPE_REGISTER
) ? "regs" :
1425 (odp
->type
== TYPE_INTERRUPT
) ? "intr" :
1426 (odp
->type
== TYPE_PASCAL_16
) ? "word" : "long",
1427 odp
->u
.func
.arg_types
);
1429 odp
->offset
= code_offset
;
1430 code_offset
+= sizeof(ENTRYPOINT16
);
1434 fprintf(stderr
,"build: function type %d not available for Win16\n",
1440 fprintf( outfile
, " }\n};\n" );
1442 /* Output data segment */
1444 DumpBytes( outfile
, data
, data_offset
, "Data_Segment" );
1446 /* Build the module */
1448 module_size
= BuildModule16( outfile
, code_offset
, data_offset
);
1450 /* Output the DLL descriptor */
1452 if (rsrc_name
[0]) fprintf( outfile
, "extern const char %s[];\n\n", rsrc_name
);
1454 fprintf( outfile
, "\nconst WIN16_DESCRIPTOR %s_Descriptor = \n{\n", DLLName
);
1455 fprintf( outfile
, " \"%s\",\n", DLLName
);
1456 fprintf( outfile
, " Module,\n" );
1457 fprintf( outfile
, " sizeof(Module),\n" );
1458 fprintf( outfile
, " (BYTE *)&Code_Segment,\n" );
1459 fprintf( outfile
, " (BYTE *)Data_Segment,\n" );
1460 fprintf( outfile
, " %s\n", rsrc_name
[0] ? rsrc_name
: "0" );
1461 fprintf( outfile
, "};\n" );
1467 /*******************************************************************
1470 * Build an assembly file from a spec file.
1472 static void BuildSpecFile( FILE *outfile
, FILE *infile
)
1480 BuildSpec16File( outfile
);
1483 BuildSpec32File( outfile
);
1486 fatal_error( "Missing 'type' declaration\n" );
1491 /*******************************************************************
1492 * BuildCallFrom16Func
1494 * Build a 16-bit-to-Wine callback glue function.
1496 * The generated routines are intended to be used as argument conversion
1497 * routines to be called by the CallFrom16... core. Thus, the prototypes of
1498 * the generated routines are (see also CallFrom16):
1500 * extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
1501 * extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
1502 * extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args,
1503 * CONTEXT86 *context );
1504 * extern void WINAPI PREFIX_CallFrom16_C_intr_xxx( FARPROC func, LPBYTE args,
1505 * CONTEXT86 *context );
1507 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
1508 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
1509 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1510 * 'T'=segmented pointer to null-terminated string).
1512 * The generated routines fetch the arguments from the 16-bit stack (pointed
1513 * to by 'args'); the offsets of the single argument values are computed
1514 * according to the calling convention and the argument types. Then, the
1515 * 32-bit entry point is called with these arguments.
1517 * For register functions, the arguments (if present) are converted just
1518 * the same as for normal functions, but in addition the CONTEXT86 pointer
1519 * filled with the current register values is passed to the 32-bit routine.
1520 * (An 'intr' interrupt handler routine is treated exactly like a register
1521 * routine, except that upon return, the flags word pushed onto the stack
1522 * by the interrupt is removed by the 16-bit call stub.)
1525 static void BuildCallFrom16Func( FILE *outfile
, char *profile
, char *prefix
, int local
)
1527 int i
, pos
, argsize
= 0;
1531 char *args
= profile
+ 7;
1534 /* Parse function type */
1536 if (!strncmp( "c_", profile
, 2 )) usecdecl
= 1;
1537 else if (strncmp( "p_", profile
, 2 ))
1539 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1543 if (!strncmp( "word_", profile
+ 2, 5 )) short_ret
= 1;
1544 else if (!strncmp( "regs_", profile
+ 2, 5 )) reg_func
= 1;
1545 else if (!strncmp( "intr_", profile
+ 2, 5 )) reg_func
= 2;
1546 else if (strncmp( "long_", profile
+ 2, 5 ))
1548 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1552 for ( i
= 0; args
[i
]; i
++ )
1555 case 'w': /* word */
1556 case 's': /* s_word */
1560 case 'l': /* long or segmented pointer */
1561 case 'T': /* segmented pointer to null-terminated string */
1562 case 'p': /* linear pointer */
1563 case 't': /* linear pointer to null-terminated string */
1568 ret_type
= reg_func
? "void" : short_ret
? "WORD" : "LONG";
1570 fprintf( outfile
, "typedef %s WINAPI (*proc_%s_t)( ",
1571 ret_type
, profile
);
1573 for ( i
= 0; args
[i
]; i
++ )
1575 if ( i
) fprintf( outfile
, ", " );
1578 case 'w': fprintf( outfile
, "WORD" ); break;
1579 case 's': fprintf( outfile
, "INT16" ); break;
1580 case 'l': case 'T': fprintf( outfile
, "LONG" ); break;
1581 case 'p': case 't': fprintf( outfile
, "LPVOID" ); break;
1585 fprintf( outfile
, "%sstruct _CONTEXT86 *", i
? ", " : "" );
1587 fprintf( outfile
, "void" );
1588 fprintf( outfile
, " );\n" );
1590 fprintf( outfile
, "%s%s WINAPI %s_CallFrom16_%s( FARPROC proc, LPBYTE args%s )\n{\n",
1591 local
? "static " : "", ret_type
, prefix
, profile
,
1592 reg_func
? ", struct _CONTEXT86 *context" : "" );
1594 fprintf( outfile
, " %s((proc_%s_t) proc) (\n",
1595 reg_func
? "" : "return ", profile
);
1597 pos
= !usecdecl
? argsize
: 0;
1598 for ( i
= 0; args
[i
]; i
++ )
1600 if ( i
) fprintf( outfile
, ",\n" );
1601 fprintf( outfile
, " " );
1604 case 'w': /* word */
1605 if ( !usecdecl
) pos
-= 2;
1606 fprintf( outfile
, "*(WORD *)(args+%d)", pos
);
1607 if ( usecdecl
) pos
+= 2;
1610 case 's': /* s_word */
1611 if ( !usecdecl
) pos
-= 2;
1612 fprintf( outfile
, "*(INT16 *)(args+%d)", pos
);
1613 if ( usecdecl
) pos
+= 2;
1616 case 'l': /* long or segmented pointer */
1617 case 'T': /* segmented pointer to null-terminated string */
1618 if ( !usecdecl
) pos
-= 4;
1619 fprintf( outfile
, "*(LONG *)(args+%d)", pos
);
1620 if ( usecdecl
) pos
+= 4;
1623 case 'p': /* linear pointer */
1624 case 't': /* linear pointer to null-terminated string */
1625 if ( !usecdecl
) pos
-= 4;
1626 fprintf( outfile
, "PTR_SEG_TO_LIN( *(SEGPTR *)(args+%d) )", pos
);
1627 if ( usecdecl
) pos
+= 4;
1631 fprintf( stderr
, "Unknown arg type '%c'\n", args
[i
] );
1635 fprintf( outfile
, "%s context", i
? ",\n" : "" );
1636 fprintf( outfile
, " );\n}\n\n" );
1639 /*******************************************************************
1642 * Build a Wine-to-16-bit callback glue function.
1644 * Prototypes for the CallTo16 functions:
1645 * extern WORD CALLBACK PREFIX_CallTo16_word_xxx( FARPROC16 func, args... );
1646 * extern LONG CALLBACK PREFIX_CallTo16_long_xxx( FARPROC16 func, args... );
1648 * These routines are provided solely for convenience; they simply
1649 * write the arguments onto the 16-bit stack, and call the appropriate
1650 * CallTo16... core routine.
1652 * If you have more sophisticated argument conversion requirements than
1653 * are provided by these routines, you might as well call the core
1654 * routines by yourself.
1657 static void BuildCallTo16Func( FILE *outfile
, char *profile
, char *prefix
)
1659 char *args
= profile
+ 5;
1660 int i
, argsize
= 0, short_ret
= 0;
1662 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1663 else if (strncmp( "long_", profile
, 5 ))
1665 fprintf( stderr
, "Invalid function name '%s'.\n", profile
);
1669 fprintf( outfile
, "%s %s_CallTo16_%s( FARPROC16 proc",
1670 short_ret
? "WORD" : "LONG", prefix
, profile
);
1672 for ( i
= 0; args
[i
]; i
++ )
1674 fprintf( outfile
, ", " );
1677 case 'w': fprintf( outfile
, "WORD" ); argsize
+= 2; break;
1678 case 'l': fprintf( outfile
, "LONG" ); argsize
+= 4; break;
1680 fprintf( outfile
, " arg%d", i
+1 );
1682 fprintf( outfile
, " )\n{\n" );
1685 fprintf( outfile
, " LPBYTE args = (LPBYTE)CURRENT_STACK16;\n" );
1688 for ( i
= 0; args
[i
]; i
++ )
1692 case 'w': fprintf( outfile
, " args -= sizeof(WORD); *(WORD" ); break;
1693 case 'l': fprintf( outfile
, " args -= sizeof(LONG); *(LONG" ); break;
1694 default: fprintf( stderr
, "Unexpected case '%c' in BuildCallTo16Func\n",
1697 fprintf( outfile
, " *)args = arg%d;\n", i
+1 );
1700 fprintf( outfile
, " return CallTo16%s( proc, %d );\n}\n\n",
1701 short_ret
? "Word" : "Long", argsize
);
1706 /*******************************************************************
1707 * BuildCallFrom16Core
1709 * This routine builds the core routines used in 16->32 thunks:
1710 * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
1712 * These routines are intended to be called via a far call (with 32-bit
1713 * operand size) from 16-bit code. The 16-bit code stub must push %bp,
1714 * the 32-bit entry point to be called, and the argument conversion
1715 * routine to be used (see stack layout below).
1717 * The core routine completes the STACK16FRAME on the 16-bit stack and
1718 * switches to the 32-bit stack. Then, the argument conversion routine
1719 * is called; it gets passed the 32-bit entry point and a pointer to the
1720 * 16-bit arguments (on the 16-bit stack) as parameters. (You can either
1721 * use conversion routines automatically generated by BuildCallFrom16,
1722 * or write your own for special purposes.)
1724 * The conversion routine must call the 32-bit entry point, passing it
1725 * the converted arguments, and return its return value to the core.
1726 * After the conversion routine has returned, the core switches back
1727 * to the 16-bit stack, converts the return value to the DX:AX format
1728 * (CallFrom16Long), and returns to the 16-bit call stub. All parameters,
1729 * including %bp, are popped off the stack.
1731 * The 16-bit call stub now returns to the caller, popping the 16-bit
1732 * arguments if necessary (pascal calling convention).
1734 * In the case of a 'register' function, CallFrom16Register fills a
1735 * CONTEXT86 structure with the values all registers had at the point
1736 * the first instruction of the 16-bit call stub was about to be
1737 * executed. A pointer to this CONTEXT86 is passed as third parameter
1738 * to the argument conversion routine, which typically passes it on
1739 * to the called 32-bit entry point.
1741 * CallFrom16Thunk is a special variant used by the implementation of
1742 * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
1743 * implemented as follows:
1744 * On entry, the EBX register is set up to contain a flat pointer to the
1745 * 16-bit stack such that EBX+22 points to the first argument.
1746 * Then, the entry point is called, while EBP is set up to point
1747 * to the return address (on the 32-bit stack).
1748 * The called function returns with CX set to the number of bytes
1749 * to be popped of the caller's stack.
1751 * Stack layout upon entry to the core routine (STACK16FRAME):
1753 * (sp+24) word first 16-bit arg
1757 * (sp+14) long 32-bit entry point (reused for Win16 mutex recursion count)
1758 * (sp+12) word ip of actual entry point (necessary for relay debugging)
1759 * (sp+8) long relay (argument conversion) function entry point
1760 * (sp+4) long cs of 16-bit entry point
1761 * (sp) long ip of 16-bit entry point
1763 * Added on the stack:
1764 * (sp-2) word saved gs
1765 * (sp-4) word saved fs
1766 * (sp-6) word saved es
1767 * (sp-8) word saved ds
1768 * (sp-12) long saved ebp
1769 * (sp-16) long saved ecx
1770 * (sp-20) long saved edx
1771 * (sp-24) long saved previous stack
1773 static void BuildCallFrom16Core( FILE *outfile
, int reg_func
, int thunk
, int short_ret
)
1775 char *name
= thunk
? "Thunk" : reg_func
? "Register" : short_ret
? "Word" : "Long";
1777 /* Function header */
1778 fprintf( outfile
, "\n\t.align 4\n" );
1780 fprintf( outfile
, ".stabs \"CallFrom16%s:F1\",36,0,0," PREFIX
"CallFrom16%s\n",
1783 fprintf( outfile
, "\t.type " PREFIX
"CallFrom16%s,@function\n", name
);
1784 fprintf( outfile
, "\t.globl " PREFIX
"CallFrom16%s\n", name
);
1785 fprintf( outfile
, PREFIX
"CallFrom16%s:\n", name
);
1787 /* Create STACK16FRAME (except STACK32FRAME link) */
1788 fprintf( outfile
, "\tpushw %%gs\n" );
1789 fprintf( outfile
, "\tpushw %%fs\n" );
1790 fprintf( outfile
, "\tpushw %%es\n" );
1791 fprintf( outfile
, "\tpushw %%ds\n" );
1792 fprintf( outfile
, "\tpushl %%ebp\n" );
1793 fprintf( outfile
, "\tpushl %%ecx\n" );
1794 fprintf( outfile
, "\tpushl %%edx\n" );
1796 /* Save original EFlags register */
1797 fprintf( outfile
, "\tpushfl\n" );
1801 /* Get Global Offset Table into %ecx */
1802 fprintf( outfile
, "\tcall .LCallFrom16%s.getgot1\n", name
);
1803 fprintf( outfile
, ".LCallFrom16%s.getgot1:\n", name
);
1804 fprintf( outfile
, "\tpopl %%ecx\n" );
1805 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot1], %%ecx\n", name
);
1808 /* Load 32-bit segment registers */
1809 fprintf( outfile
, "\tmovw $0x%04x, %%dx\n", Data_Selector
);
1811 fprintf( outfile
, "\tdata16\n");
1813 fprintf( outfile
, "\tmovw %%dx, %%ds\n" );
1815 fprintf( outfile
, "\tdata16\n");
1817 fprintf( outfile
, "\tmovw %%dx, %%es\n" );
1821 fprintf( outfile
, "\tmovl " PREFIX
"SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
1822 fprintf( outfile
, "\tmovw (%%edx), %%fs\n" );
1825 fprintf( outfile
, "\tmovw " PREFIX
"SYSLEVEL_Win16CurrentTeb, %%fs\n" );
1827 /* Get address of ldt_copy array into %ecx */
1829 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy@GOT(%%ecx), %%ecx\n" );
1831 fprintf( outfile
, "\tmovl $" PREFIX
"ldt_copy, %%ecx\n" );
1833 /* Translate STACK16FRAME base to flat offset in %edx */
1834 fprintf( outfile
, "\tmovw %%ss, %%dx\n" );
1835 fprintf( outfile
, "\tandl $0xfff8, %%edx\n" );
1836 fprintf( outfile
, "\tmovl (%%ecx,%%edx), %%edx\n" );
1837 fprintf( outfile
, "\tmovzwl %%sp, %%ebp\n" );
1838 fprintf( outfile
, "\tleal (%%ebp,%%edx), %%edx\n" );
1840 /* Get saved flags into %ecx */
1841 fprintf( outfile
, "\tpopl %%ecx\n" );
1843 /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
1844 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET
);
1845 fprintf( outfile
, "\tpushl %%ebp\n" );
1849 fprintf( outfile
,"\tdata16\n");
1851 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET
+ 2 );
1852 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET
);
1853 fprintf( outfile
, "\tpushl %%ds\n" );
1854 fprintf( outfile
, "\tpopl %%ss\n" );
1855 fprintf( outfile
, "\tmovl %%ebp, %%esp\n" );
1856 fprintf( outfile
, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME
, ebp
) );
1860 STACK16FRAME is completely set up
1861 DS, ES, SS: flat data segment
1863 ESP: points to last STACK32FRAME
1864 EBP: points to ebp member of last STACK32FRAME
1865 EDX: points to current STACK16FRAME
1866 ECX: contains saved flags
1867 all other registers: unchanged */
1869 /* Special case: C16ThkSL stub */
1872 /* Set up registers as expected and call thunk */
1873 fprintf( outfile
, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME
)-22 );
1874 fprintf( outfile
, "\tleal -4(%%esp), %%ebp\n" );
1876 fprintf( outfile
, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point
) );
1878 /* Switch stack back */
1879 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
1880 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
1881 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
1883 /* Restore registers and return directly to caller */
1884 fprintf( outfile
, "\taddl $8, %%esp\n" );
1885 fprintf( outfile
, "\tpopl %%ebp\n" );
1886 fprintf( outfile
, "\tpopw %%ds\n" );
1887 fprintf( outfile
, "\tpopw %%es\n" );
1888 fprintf( outfile
, "\tpopw %%fs\n" );
1889 fprintf( outfile
, "\tpopw %%gs\n" );
1890 fprintf( outfile
, "\taddl $20, %%esp\n" );
1892 fprintf( outfile
, "\txorb %%ch, %%ch\n" );
1893 fprintf( outfile
, "\tpopl %%ebx\n" );
1894 fprintf( outfile
, "\taddw %%cx, %%sp\n" );
1895 fprintf( outfile
, "\tpush %%ebx\n" );
1897 fprintf( outfile
, "\t.byte 0x66\n" );
1898 fprintf( outfile
, "\tlret\n" );
1904 /* Build register CONTEXT */
1907 fprintf( outfile
, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86
) );
1909 fprintf( outfile
, "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags
) );
1911 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax
) );
1912 fprintf( outfile
, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx
) );
1913 fprintf( outfile
, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi
) );
1914 fprintf( outfile
, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi
) );
1916 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp
) );
1917 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp
) );
1918 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx
) );
1919 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx
) );
1920 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx
) );
1921 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx
) );
1923 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds
) );
1924 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs
) );
1925 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es
) );
1926 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs
) );
1927 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs
) );
1928 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs
) );
1929 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs
) );
1930 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs
) );
1932 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs
) );
1933 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs
) );
1934 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip
) );
1935 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip
) );
1937 fprintf( outfile
, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET
+2 );
1938 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs
) );
1939 fprintf( outfile
, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET
);
1940 fprintf( outfile
, "\taddl $%d, %%eax\n", STACK16OFFSET(ip
) );
1941 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp
) );
1943 fprintf( outfile
, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave
) );
1946 /* Push address of CONTEXT86 structure -- popped by the relay routine */
1947 fprintf( outfile
, "\tpushl %%esp\n" );
1951 /* Print debug info before call */
1956 fprintf( outfile
, "\tpushl %%ebx\n" );
1958 /* Get Global Offset Table into %ebx (for PLT call) */
1959 fprintf( outfile
, "\tcall .LCallFrom16%s.getgot2\n", name
);
1960 fprintf( outfile
, ".LCallFrom16%s.getgot2:\n", name
);
1961 fprintf( outfile
, "\tpopl %%ebx\n" );
1962 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot2], %%ebx\n", name
);
1965 fprintf( outfile
, "\tpushl %%edx\n" );
1967 fprintf( outfile
, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
1968 sizeof(CONTEXT
) + STRUCTOFFSET(STACK32FRAME
, ebp
) );
1970 fprintf( outfile
, "\tpushl $0\n" );
1973 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16@PLT\n ");
1975 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16\n ");
1977 fprintf( outfile
, "\tpopl %%edx\n" );
1978 fprintf( outfile
, "\tpopl %%edx\n" );
1981 fprintf( outfile
, "\tpopl %%ebx\n" );
1984 /* Call relay routine (which will call the API entry point) */
1985 fprintf( outfile
, "\tleal %d(%%edx), %%eax\n", sizeof(STACK16FRAME
) );
1986 fprintf( outfile
, "\tpushl %%eax\n" );
1987 fprintf( outfile
, "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point
) );
1988 fprintf( outfile
, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay
) );
1990 /* Print debug info after call */
1995 fprintf( outfile
, "\tpushl %%ebx\n" );
1997 /* Get Global Offset Table into %ebx (for PLT call) */
1998 fprintf( outfile
, "\tcall .LCallFrom16%s.getgot3\n", name
);
1999 fprintf( outfile
, ".LCallFrom16%s.getgot3:\n", name
);
2000 fprintf( outfile
, "\tpopl %%ebx\n" );
2001 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot3], %%ebx\n", name
);
2004 fprintf( outfile
, "\tpushl %%eax\n" );
2006 fprintf( outfile
, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2007 sizeof(CONTEXT
) + STRUCTOFFSET(STACK32FRAME
, ebp
) );
2009 fprintf( outfile
, "\tpushl $0\n" );
2012 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret@PLT\n ");
2014 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret\n ");
2016 fprintf( outfile
, "\tpopl %%eax\n" );
2017 fprintf( outfile
, "\tpopl %%eax\n" );
2020 fprintf( outfile
, "\tpopl %%ebx\n" );
2026 fprintf( outfile
, "\tmovl %%esp, %%ebx\n" );
2028 /* Switch stack back */
2029 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2030 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
2031 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2033 /* Get return address to CallFrom16 stub */
2034 fprintf( outfile
, "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip
)-4 );
2035 fprintf( outfile
, "\tpopl %%eax\n" );
2036 fprintf( outfile
, "\tpopl %%edx\n" );
2038 /* Restore all registers from CONTEXT */
2039 fprintf( outfile
, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs
) );
2040 fprintf( outfile
, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp
) );
2041 fprintf( outfile
, "\taddl $4, %%esp\n" ); /* room for final return address */
2043 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs
) );
2044 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip
) );
2045 fprintf( outfile
, "\tpushl %%edx\n" );
2046 fprintf( outfile
, "\tpushl %%eax\n" );
2047 fprintf( outfile
, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags
) );
2048 fprintf( outfile
, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs
) );
2050 fprintf( outfile
, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs
) );
2051 fprintf( outfile
, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs
) );
2052 fprintf( outfile
, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs
) );
2054 fprintf( outfile
, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp
) );
2055 fprintf( outfile
, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi
) );
2056 fprintf( outfile
, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi
) );
2057 fprintf( outfile
, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax
) );
2058 fprintf( outfile
, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx
) );
2059 fprintf( outfile
, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx
) );
2060 fprintf( outfile
, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx
) );
2062 fprintf( outfile
, "\tpopl %%ds\n" );
2063 fprintf( outfile
, "\tpopfl\n" );
2064 fprintf( outfile
, "\tlret\n" );
2068 /* Switch stack back */
2069 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2070 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
2071 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2073 /* Restore registers */
2074 fprintf( outfile
, "\tpopl %%edx\n" );
2075 fprintf( outfile
, "\tpopl %%ecx\n" );
2076 fprintf( outfile
, "\tpopl %%ebp\n" );
2077 fprintf( outfile
, "\tpopw %%ds\n" );
2078 fprintf( outfile
, "\tpopw %%es\n" );
2079 fprintf( outfile
, "\tpopw %%fs\n" );
2080 fprintf( outfile
, "\tpopw %%gs\n" );
2082 /* Prepare return value and set flags accordingly */
2084 fprintf( outfile
, "\tshldl $16, %%eax, %%edx\n" );
2085 fprintf( outfile
, "\torl %%eax, %%eax\n" );
2087 /* Return to return stub which will return to caller */
2088 fprintf( outfile
, "\tlret $12\n" );
2093 /*******************************************************************
2096 * This routine builds the core routines used in 32->16 thunks:
2098 * extern void CALLBACK CallTo16Word( SEGPTR target, int nb_args );
2099 * extern void CALLBACK CallTo16Long( SEGPTR target, int nb_args );
2100 * extern void CALLBACK CallTo16RegisterShort( const CONTEXT86 *context, int nb_args );
2101 * extern void CALLBACK CallTo16RegisterLong ( const CONTEXT86 *context, int nb_args );
2103 * These routines can be called directly from 32-bit code.
2105 * All routines expect that the 16-bit stack contents (arguments) were
2106 * already set up by the caller; nb_args must contain the number of bytes
2107 * to be conserved. The 16-bit SS:SP will be set accordinly.
2109 * All other registers are either taken from the CONTEXT86 structure
2110 * or else set to default values. The target routine address is either
2111 * given directly or taken from the CONTEXT86.
2113 * If you want to call a 16-bit routine taking only standard argument types
2114 * (WORD and LONG), you can also have an appropriate argument conversion
2115 * stub automatically generated (see BuildCallTo16); you'd then call this
2116 * stub, which in turn would prepare the 16-bit stack and call the appropiate
2121 static void BuildCallTo16Core( FILE *outfile
, int short_ret
, int reg_func
)
2123 char *name
= reg_func
== 2 ? "RegisterLong" :
2124 reg_func
== 1 ? "RegisterShort" :
2125 short_ret
? "Word" : "Long";
2127 /* Function header */
2128 fprintf( outfile
, "\n\t.align 4\n" );
2130 fprintf( outfile
, ".stabs \"CallTo16%s:F1\",36,0,0," PREFIX
"CallTo16%s\n",
2133 fprintf( outfile
, "\t.globl " PREFIX
"CallTo16%s\n", name
);
2134 fprintf( outfile
, PREFIX
"CallTo16%s:\n", name
);
2136 /* Function entry sequence */
2137 fprintf( outfile
, "\tpushl %%ebp\n" );
2138 fprintf( outfile
, "\tmovl %%esp, %%ebp\n" );
2140 /* Save the 32-bit registers */
2141 fprintf( outfile
, "\tpushl %%ebx\n" );
2142 fprintf( outfile
, "\tpushl %%ecx\n" );
2143 fprintf( outfile
, "\tpushl %%edx\n" );
2144 fprintf( outfile
, "\tpushl %%esi\n" );
2145 fprintf( outfile
, "\tpushl %%edi\n" );
2149 /* Get Global Offset Table into %ebx */
2150 fprintf( outfile
, "\tcall .LCallTo16%s.getgot1\n", name
);
2151 fprintf( outfile
, ".LCallTo16%s.getgot1:\n", name
);
2152 fprintf( outfile
, "\tpopl %%ebx\n" );
2153 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot1], %%ebx\n", name
);
2156 /* Enter Win16 Mutex */
2158 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_EnterWin16Lock@PLT\n" );
2160 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_EnterWin16Lock\n" );
2162 /* Print debugging info */
2165 /* Push flags, number of arguments, and target */
2166 fprintf( outfile
, "\tpushl $%d\n", reg_func
);
2167 fprintf( outfile
, "\tpushl 12(%%ebp)\n" );
2168 fprintf( outfile
, "\tpushl 8(%%ebp)\n" );
2171 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16@PLT\n" );
2173 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16\n" );
2175 fprintf( outfile
, "\taddl $12, %%esp\n" );
2178 /* Get return address */
2180 fprintf( outfile
, "\tmovl " PREFIX
"CallTo16_RetAddr@GOTOFF(%%ebx), %%ecx\n" );
2182 fprintf( outfile
, "\tmovl " PREFIX
"CallTo16_RetAddr, %%ecx\n" );
2184 /* Call the actual CallTo16 routine (simulate a lcall) */
2185 fprintf( outfile
, "\tpushl %%cs\n" );
2186 fprintf( outfile
, "\tcall .LCallTo16%s\n", name
);
2188 /* Convert and push return value */
2191 fprintf( outfile
, "\tmovzwl %%ax, %%eax\n" );
2192 fprintf( outfile
, "\tpushl %%eax\n" );
2194 else if ( reg_func
!= 2 )
2196 fprintf( outfile
, "\tshll $16,%%edx\n" );
2197 fprintf( outfile
, "\tmovw %%ax,%%dx\n" );
2198 fprintf( outfile
, "\tpushl %%edx\n" );
2201 fprintf( outfile
, "\tpushl %%eax\n" );
2205 /* Get Global Offset Table into %ebx (might have been overwritten) */
2206 fprintf( outfile
, "\tcall .LCallTo16%s.getgot2\n", name
);
2207 fprintf( outfile
, ".LCallTo16%s.getgot2:\n", name
);
2208 fprintf( outfile
, "\tpopl %%ebx\n" );
2209 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot2], %%ebx\n", name
);
2212 /* Print debugging info */
2216 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16Ret@PLT\n" );
2218 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16Ret\n" );
2221 /* Leave Win16 Mutex */
2223 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_LeaveWin16Lock@PLT\n" );
2225 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_LeaveWin16Lock\n" );
2227 /* Get return value */
2228 fprintf( outfile
, "\tpopl %%eax\n" );
2230 /* Restore the 32-bit registers */
2231 fprintf( outfile
, "\tpopl %%edi\n" );
2232 fprintf( outfile
, "\tpopl %%esi\n" );
2233 fprintf( outfile
, "\tpopl %%edx\n" );
2234 fprintf( outfile
, "\tpopl %%ecx\n" );
2235 fprintf( outfile
, "\tpopl %%ebx\n" );
2237 /* Function exit sequence */
2238 fprintf( outfile
, "\tpopl %%ebp\n" );
2239 fprintf( outfile
, "\tret $8\n" );
2242 /* Start of the actual CallTo16 routine */
2244 fprintf( outfile
, ".LCallTo16%s:\n", name
);
2246 /* Complete STACK32FRAME */
2247 fprintf( outfile
, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET
);
2248 fprintf( outfile
, "\tmovl %%esp,%%edx\n" );
2250 /* Switch to the 16-bit stack */
2252 fprintf( outfile
,"\tdata16\n");
2254 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2);
2255 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET
);
2256 fprintf( outfile
, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET
);
2258 /* Make %bp point to the previous stackframe (built by CallFrom16) */
2259 fprintf( outfile
, "\tmovzwl %%sp,%%ebp\n" );
2260 fprintf( outfile
, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp
) );
2262 /* Add the specified offset to the new sp */
2263 fprintf( outfile
, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args
) );
2265 /* Push the return address
2266 * With sreg suffix, we push 16:16 address (normal lret)
2267 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
2270 fprintf( outfile
, "\tpushl %%ecx\n" );
2273 fprintf( outfile
, "\tshldl $16, %%ecx, %%eax\n" );
2274 fprintf( outfile
, "\tpushw $0\n" );
2275 fprintf( outfile
, "\tpushw %%ax\n" );
2276 fprintf( outfile
, "\tpushw $0\n" );
2277 fprintf( outfile
, "\tpushw %%cx\n" );
2282 /* Push the called routine address */
2283 fprintf( outfile
, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target
) );
2284 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs
) );
2285 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip
) );
2287 /* Get the registers */
2288 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs
) );
2289 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs
) );
2290 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2291 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs
) );
2292 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2293 fprintf( outfile
, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp
) );
2294 fprintf( outfile
, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi
) );
2295 fprintf( outfile
, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi
) );
2296 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax
) );
2297 fprintf( outfile
, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx
) );
2298 fprintf( outfile
, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx
) );
2299 fprintf( outfile
, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx
) );
2301 /* Get the 16-bit ds */
2302 fprintf( outfile
, "\tpopw %%ds\n" );
2304 else /* not a register function */
2306 /* Push the called routine address */
2307 fprintf( outfile
, "\tpushl %d(%%edx)\n", STACK32OFFSET(target
) );
2309 /* Set %fs to the value saved by the last CallFrom16 */
2310 fprintf( outfile
, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs
)-STACK16OFFSET(bp
) );
2311 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2313 /* Set %ds and %es (and %ax just in case) equal to %ss */
2314 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
2315 fprintf( outfile
, "\tmovw %%ax,%%ds\n" );
2316 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2319 /* Jump to the called routine */
2320 fprintf( outfile
, "\t.byte 0x66\n" );
2321 fprintf( outfile
, "\tlret\n" );
2324 /*******************************************************************
2327 * Build the return code for 16-bit callbacks
2329 static void BuildRet16Func( FILE *outfile
)
2332 * Note: This must reside in the .data section to allow
2333 * run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
2336 fprintf( outfile
, "\n\t.globl " PREFIX
"CallTo16_Ret\n" );
2337 fprintf( outfile
, PREFIX
"CallTo16_Ret:\n" );
2339 /* Restore 32-bit segment registers */
2341 fprintf( outfile
, "\tmovw $0x%04x,%%bx\n", Data_Selector
);
2343 fprintf( outfile
, "\tdata16\n");
2345 fprintf( outfile
, "\tmovw %%bx,%%ds\n" );
2347 fprintf( outfile
, "\tdata16\n");
2349 fprintf( outfile
, "\tmovw %%bx,%%es\n" );
2351 fprintf( outfile
, "\tmovw " PREFIX
"SYSLEVEL_Win16CurrentTeb,%%fs\n" );
2353 /* Restore the 32-bit stack */
2356 fprintf( outfile
, "\tdata16\n");
2358 fprintf( outfile
, "\tmovw %%bx,%%ss\n" );
2359 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET
);
2360 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2362 /* Return to caller */
2364 fprintf( outfile
, "\tlret\n" );
2366 /* Declare the return address variable */
2368 fprintf( outfile
, "\n\t.globl " PREFIX
"CallTo16_RetAddr\n" );
2369 fprintf( outfile
, PREFIX
"CallTo16_RetAddr:\t.long 0\n" );
2372 /*******************************************************************
2373 * BuildCallTo32CBClient
2375 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
2377 * Since the relay stub is itself 32-bit, this should not be a problem;
2378 * unfortunately, the relay stubs are expected to switch back to a
2379 * 16-bit stack (and 16-bit code) after completion :-(
2381 * This would conflict with our 16- vs. 32-bit stack handling, so
2382 * we simply switch *back* to our 32-bit stack before returning to
2385 * The CBClient relay stub expects to be called with the following
2386 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
2387 * stack at the designated places:
2390 * (ebp+14) original arguments to the callback routine
2391 * (ebp+10) far return address to original caller
2392 * (ebp+6) Thunklet target address
2393 * (ebp+2) Thunklet relay ID code
2394 * (ebp) BP (saved by CBClientGlueSL)
2395 * (ebp-2) SI (saved by CBClientGlueSL)
2396 * (ebp-4) DI (saved by CBClientGlueSL)
2397 * (ebp-6) DS (saved by CBClientGlueSL)
2399 * ... buffer space used by the 16-bit side glue for temp copies
2401 * (ebx+4) far return address to 16-bit side glue code
2402 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
2404 * The 32-bit side glue code accesses both the original arguments (via ebp)
2405 * and the temporary copies prepared by the 16-bit side glue (via ebx).
2406 * After completion, the stub will load ss:sp from the buffer at ebx
2407 * and perform a far return to 16-bit code.
2409 * To trick the relay stub into returning to us, we replace the 16-bit
2410 * return address to the glue code by a cs:ip pair pointing to our
2411 * return entry point (the original return address is saved first).
2412 * Our return stub thus called will then reload the 32-bit ss:esp and
2413 * return to 32-bit code (by using and ss:esp value that we have also
2414 * pushed onto the 16-bit stack before and a cs:eip values found at
2415 * that position on the 32-bit stack). The ss:esp to be restored is
2416 * found relative to the 16-bit stack pointer at:
2419 * (ebx-8) sp (32-bit stack pointer)
2421 * The second variant of this routine, CALL32_CBClientEx, which is used
2422 * to implement KERNEL.621, has to cope with yet another problem: Here,
2423 * the 32-bit side directly returns to the caller of the CBClient thunklet,
2424 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
2425 * As we have to return to our 32-bit code first, we have to adapt the
2426 * layout of our temporary area so as to include values for the registers
2427 * that are to be restored, and later (in the implementation of KERNEL.621)
2428 * we *really* restore them. The return stub restores DS, DI, SI, and BP
2429 * from the stack, skips the next 8 bytes (CBClient relay code / target),
2430 * and then performs a lret NN, where NN is the number of arguments to be
2431 * removed. Thus, we prepare our temporary area as follows:
2433 * (ebx+22) 16-bit cs (this segment)
2434 * (ebx+20) 16-bit ip ('16-bit' return entry point)
2435 * (ebx+16) 32-bit ss (flat)
2436 * (ebx+12) 32-bit sp (32-bit stack pointer)
2437 * (ebx+10) 16-bit bp (points to ebx+24)
2438 * (ebx+8) 16-bit si (ignored)
2439 * (ebx+6) 16-bit di (ignored)
2440 * (ebx+4) 16-bit ds (we actually use the flat DS here)
2441 * (ebx+2) 16-bit ss (16-bit stack segment)
2442 * (ebx+0) 16-bit sp (points to ebx+4)
2444 * Note that we ensure that DS is not changed and remains the flat segment,
2445 * and the 32-bit stack pointer our own return stub needs fits just
2446 * perfectly into the 8 bytes that are skipped by the Windows stub.
2447 * One problem is that we have to determine the number of removed arguments,
2448 * as these have to be really removed in KERNEL.621. Thus, the BP value
2449 * that we place in the temporary area to be restored, contains the value
2450 * that SP would have if no arguments were removed. By comparing the actual
2451 * value of SP with this value in our return stub we can compute the number
2452 * of removed arguments. This is then returned to KERNEL.621.
2454 * The stack layout of this function:
2455 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
2456 * (ebp+16) esi pointer to caller's esi value
2457 * (ebp+12) arg ebp value to be set for relay stub
2458 * (ebp+8) func CBClient relay stub address
2462 static void BuildCallTo32CBClient( FILE *outfile
, BOOL isEx
)
2464 char *name
= isEx
? "CBClientEx" : "CBClient";
2465 int size
= isEx
? 24 : 12;
2467 /* Function header */
2469 fprintf( outfile
, "\n\t.align 4\n" );
2471 fprintf( outfile
, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX
"CALL32_%s\n",
2474 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_%s\n", name
);
2475 fprintf( outfile
, PREFIX
"CALL32_%s:\n", name
);
2479 fprintf( outfile
, "\tpushl %%ebp\n" );
2480 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2481 fprintf( outfile
, "\tpushl %%edi\n" );
2482 fprintf( outfile
, "\tpushl %%esi\n" );
2483 fprintf( outfile
, "\tpushl %%ebx\n" );
2485 /* Get the 16-bit stack */
2487 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET
);
2489 /* Convert it to a flat address */
2491 fprintf( outfile
, "\tshldl $16,%%ebx,%%eax\n" );
2492 fprintf( outfile
, "\tandl $0xfff8,%%eax\n" );
2493 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy(%%eax),%%esi\n" );
2494 fprintf( outfile
, "\tmovw %%bx,%%ax\n" );
2495 fprintf( outfile
, "\taddl %%eax,%%esi\n" );
2497 /* Allocate temporary area (simulate STACK16_PUSH) */
2499 fprintf( outfile
, "\tpushf\n" );
2500 fprintf( outfile
, "\tcld\n" );
2501 fprintf( outfile
, "\tleal -%d(%%esi), %%edi\n", size
);
2502 fprintf( outfile
, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME
) );
2503 fprintf( outfile
, "\trep\n\tmovsb\n" );
2504 fprintf( outfile
, "\tpopf\n" );
2506 fprintf( outfile
, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size
, STACKOFFSET
);
2508 fprintf( outfile
, "\tpushl %%edi\n" ); /* remember address */
2510 /* Set up temporary area */
2514 fprintf( outfile
, "\tleal 4(%%edi), %%edi\n" );
2516 fprintf( outfile
, "\tleal -8(%%esp), %%eax\n" );
2517 fprintf( outfile
, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */
2519 fprintf( outfile
, "\tmovw %%ss, %%ax\n" );
2520 fprintf( outfile
, "\tandl $0x0000ffff, %%eax\n" );
2521 fprintf( outfile
, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */
2523 fprintf( outfile
, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME
)-size
+4 + 4 );
2524 fprintf( outfile
, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */
2526 fprintf( outfile
, "\tmovl " PREFIX
"CALL32_%s_RetAddr, %%eax\n", name
);
2527 fprintf( outfile
, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */
2531 fprintf( outfile
, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME
)-size
+4 );
2532 fprintf( outfile
, "\tmovl %%ebx, 0(%%edi)\n" );
2534 fprintf( outfile
, "\tmovw %%ds, %%ax\n" );
2535 fprintf( outfile
, "\tmovw %%ax, 4(%%edi)\n" );
2537 fprintf( outfile
, "\taddl $20, %%ebx\n" );
2538 fprintf( outfile
, "\tmovw %%bx, 10(%%edi)\n" );
2540 fprintf( outfile
, "\tleal -8(%%esp), %%eax\n" );
2541 fprintf( outfile
, "\tmovl %%eax, 12(%%edi)\n" );
2543 fprintf( outfile
, "\tmovw %%ss, %%ax\n" );
2544 fprintf( outfile
, "\tandl $0x0000ffff, %%eax\n" );
2545 fprintf( outfile
, "\tmovl %%eax, 16(%%edi)\n" );
2547 fprintf( outfile
, "\tmovl " PREFIX
"CALL32_%s_RetAddr, %%eax\n", name
);
2548 fprintf( outfile
, "\tmovl %%eax, 20(%%edi)\n" );
2551 /* Set up registers and call CBClient relay stub (simulating a far call) */
2553 fprintf( outfile
, "\tmovl 16(%%ebp), %%esi\n" );
2554 fprintf( outfile
, "\tmovl (%%esi), %%esi\n" );
2556 fprintf( outfile
, "\tmovl %%edi, %%ebx\n" );
2557 fprintf( outfile
, "\tmovl 8(%%ebp), %%eax\n" );
2558 fprintf( outfile
, "\tmovl 12(%%ebp), %%ebp\n" );
2560 fprintf( outfile
, "\tpushl %%cs\n" );
2561 fprintf( outfile
, "\tcall *%%eax\n" );
2563 /* Return new esi value to caller */
2565 fprintf( outfile
, "\tmovl 32(%%esp), %%edi\n" );
2566 fprintf( outfile
, "\tmovl %%esi, (%%edi)\n" );
2568 /* Cleanup temporary area (simulate STACK16_POP) */
2570 fprintf( outfile
, "\tpop %%esi\n" );
2572 fprintf( outfile
, "\tpushf\n" );
2573 fprintf( outfile
, "\tstd\n" );
2574 fprintf( outfile
, "\tdec %%esi\n" );
2575 fprintf( outfile
, "\tleal %d(%%esi), %%edi\n", size
);
2576 fprintf( outfile
, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME
) );
2577 fprintf( outfile
, "\trep\n\tmovsb\n" );
2578 fprintf( outfile
, "\tpopf\n" );
2580 fprintf( outfile
, "\t.byte 0x64\n\taddw $%d,(%d)\n", size
, STACKOFFSET
);
2582 /* Return argument size to caller */
2585 fprintf( outfile
, "\tmovl 32(%%esp), %%ebx\n" );
2586 fprintf( outfile
, "\tmovl %%ebp, (%%ebx)\n" );
2589 /* Restore registers and return */
2591 fprintf( outfile
, "\tpopl %%ebx\n" );
2592 fprintf( outfile
, "\tpopl %%esi\n" );
2593 fprintf( outfile
, "\tpopl %%edi\n" );
2594 fprintf( outfile
, "\tpopl %%ebp\n" );
2595 fprintf( outfile
, "\tret\n" );
2598 static void BuildCallTo32CBClientRet( FILE *outfile
, BOOL isEx
)
2600 char *name
= isEx
? "CBClientEx" : "CBClient";
2602 /* '16-bit' return stub */
2604 fprintf( outfile
, "\n\t.globl " PREFIX
"CALL32_%s_Ret\n", name
);
2605 fprintf( outfile
, PREFIX
"CALL32_%s_Ret:\n", name
);
2609 fprintf( outfile
, "\tmovzwl %%sp, %%ebx\n" );
2610 fprintf( outfile
, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
2614 fprintf( outfile
, "\tmovzwl %%bp, %%ebx\n" );
2615 fprintf( outfile
, "\tsubw %%bp, %%sp\n" );
2616 fprintf( outfile
, "\tmovzwl %%sp, %%ebp\n" );
2617 fprintf( outfile
, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
2619 fprintf( outfile
, "\tlret\n" );
2621 /* Declare the return address variable */
2623 fprintf( outfile
, "\n\t.globl " PREFIX
"CALL32_%s_RetAddr\n", name
);
2624 fprintf( outfile
, PREFIX
"CALL32_%s_RetAddr:\t.long 0\n", name
);
2628 /*******************************************************************
2629 * BuildCallTo32LargeStack
2631 * Build the function used to switch to the original 32-bit stack
2632 * before calling a 32-bit function from 32-bit code. This is used for
2633 * functions that need a large stack, like X bitmaps functions.
2635 * The generated function has the following prototype:
2636 * int xxx( int (*func)(), void *arg );
2638 * The pointer to the function can be retrieved by calling CALL32_Init,
2639 * which also takes care of saving the current 32-bit stack pointer.
2640 * Furthermore, CALL32_Init switches to a new stack and jumps to the
2641 * specified target address.
2643 * NOTE: The CALL32_LargeStack routine may be recursively entered by the
2644 * same thread, but not concurrently entered by several threads.
2646 * Stack layout of CALL32_Init:
2648 * (esp+12) new stack address
2649 * (esp+8) target address
2650 * (esp+4) pointer to variable to receive CALL32_LargeStack address
2653 * Stack layout of CALL32_LargeStack:
2660 static void BuildCallTo32LargeStack( FILE *outfile
)
2662 /* Initialization function */
2664 fprintf( outfile
, "\n\t.align 4\n" );
2666 fprintf( outfile
, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX
"CALL32_Init\n");
2668 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Init\n" );
2669 fprintf( outfile
, "\t.type " PREFIX
"CALL32_Init,@function\n" );
2670 fprintf( outfile
, PREFIX
"CALL32_Init:\n" );
2671 fprintf( outfile
, "\tmovl %%esp,CALL32_Original32_esp\n" );
2672 fprintf( outfile
, "\tpopl %%eax\n" );
2673 fprintf( outfile
, "\tpopl %%eax\n" );
2674 fprintf( outfile
, "\tmovl $CALL32_LargeStack,(%%eax)\n" );
2675 fprintf( outfile
, "\tpopl %%eax\n" );
2676 fprintf( outfile
, "\tpopl %%esp\n" );
2677 fprintf( outfile
, "\tpushl %%eax\n" );
2678 fprintf( outfile
, "\tret\n" );
2680 /* Function header */
2682 fprintf( outfile
, "\n\t.align 4\n" );
2684 fprintf( outfile
, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2686 fprintf( outfile
, "CALL32_LargeStack:\n" );
2690 fprintf( outfile
, "\tpushl %%ebp\n" );
2691 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2693 /* Switch to the original 32-bit stack pointer */
2695 fprintf( outfile
, "\tcmpl $0, CALL32_RecursionCount\n" );
2696 fprintf( outfile
, "\tjne CALL32_skip\n" );
2697 fprintf( outfile
, "\tmovl CALL32_Original32_esp, %%esp\n" );
2698 fprintf( outfile
, "CALL32_skip:\n" );
2700 fprintf( outfile
, "\tincl CALL32_RecursionCount\n" );
2702 /* Transfer the argument and call the function */
2704 fprintf( outfile
, "\tpushl 12(%%ebp)\n" );
2705 fprintf( outfile
, "\tcall *8(%%ebp)\n" );
2707 /* Restore registers and return */
2709 fprintf( outfile
, "\tdecl CALL32_RecursionCount\n" );
2711 fprintf( outfile
, "\tmovl %%ebp,%%esp\n" );
2712 fprintf( outfile
, "\tpopl %%ebp\n" );
2713 fprintf( outfile
, "\tret\n" );
2717 fprintf( outfile
, "\t.data\n" );
2718 fprintf( outfile
, "CALL32_Original32_esp:\t.long 0\n" );
2719 fprintf( outfile
, "CALL32_RecursionCount:\t.long 0\n" );
2720 fprintf( outfile
, "\t.text\n" );
2724 /*******************************************************************
2725 * BuildCallFrom32Regs
2727 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2728 * 'args' is the number of dword arguments.
2732 * (ebp+12) first arg
2733 * (ebp+8) ret addr to user code
2734 * (ebp+4) ret addr to relay code
2736 * (ebp-128) buffer area to allow stack frame manipulation
2737 * (ebp-332) CONTEXT86 struct
2738 * (ebp-336) CONTEXT86 *argument
2739 * .... other arguments copied from (ebp+12)
2741 * The entry point routine is called with a CONTEXT* extra argument,
2742 * following the normal args. In this context structure, EIP_reg
2743 * contains the return address to user code, and ESP_reg the stack
2744 * pointer on return (with the return address and arguments already
2747 static void BuildCallFrom32Regs( FILE *outfile
)
2749 static const int STACK_SPACE
= 128 + sizeof(CONTEXT86
);
2751 /* Function header */
2753 fprintf( outfile
, "\n\t.align 4\n" );
2755 fprintf( outfile
, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX
"CALL32_Regs\n" );
2757 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Regs\n" );
2758 fprintf( outfile
, PREFIX
"CALL32_Regs:\n" );
2760 /* Allocate some buffer space on the stack */
2762 fprintf( outfile
, "\tpushl %%ebp\n" );
2763 fprintf( outfile
, "\tmovl %%esp,%%ebp\n ");
2764 fprintf( outfile
, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE
);
2766 /* Build the context structure */
2768 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax
) - STACK_SPACE
);
2769 fprintf( outfile
, "\tpushfl\n" );
2770 fprintf( outfile
, "\tpopl %%eax\n" );
2771 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags
) - STACK_SPACE
);
2772 fprintf( outfile
, "\tmovl 0(%%ebp),%%eax\n" );
2773 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp
) - STACK_SPACE
);
2774 fprintf( outfile
, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx
) - STACK_SPACE
);
2775 fprintf( outfile
, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx
) - STACK_SPACE
);
2776 fprintf( outfile
, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx
) - STACK_SPACE
);
2777 fprintf( outfile
, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi
) - STACK_SPACE
);
2778 fprintf( outfile
, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi
) - STACK_SPACE
);
2780 fprintf( outfile
, "\txorl %%eax,%%eax\n" );
2781 fprintf( outfile
, "\tmovw %%cs,%%ax\n" );
2782 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs
) - STACK_SPACE
);
2783 fprintf( outfile
, "\tmovw %%es,%%ax\n" );
2784 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs
) - STACK_SPACE
);
2785 fprintf( outfile
, "\tmovw %%fs,%%ax\n" );
2786 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs
) - STACK_SPACE
);
2787 fprintf( outfile
, "\tmovw %%gs,%%ax\n" );
2788 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs
) - STACK_SPACE
);
2789 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
2790 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs
) - STACK_SPACE
);
2791 fprintf( outfile
, "\tmovw %%ds,%%ax\n" );
2792 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs
) - STACK_SPACE
);
2793 fprintf( outfile
, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
2795 fprintf( outfile
, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL
);
2796 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags
) - STACK_SPACE
);
2798 fprintf( outfile
, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
2799 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip
) - STACK_SPACE
);
2801 /* Transfer the arguments */
2803 fprintf( outfile
, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
2804 fprintf( outfile
, "\tpushl %%esp\n" ); /* push ptr to context struct */
2805 fprintf( outfile
, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
2806 fprintf( outfile
, "\tjecxz 1f\n" );
2807 fprintf( outfile
, "\tsubl %%ecx,%%esp\n" );
2808 fprintf( outfile
, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */
2809 fprintf( outfile
, "\tmovl %%esp,%%edi\n" );
2810 fprintf( outfile
, "\tshrl $2,%%ecx\n" );
2811 fprintf( outfile
, "\tcld\n" );
2812 fprintf( outfile
, "\trep\n\tmovsl\n" ); /* copy args */
2814 fprintf( outfile
, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
2815 fprintf( outfile
, "\tleal 12(%%ebp,%%eax),%%eax\n" );
2816 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2818 /* Call the entry point */
2820 fprintf( outfile
, "\tcall *0(%%ebx)\n" );
2822 /* Store %eip and %ebp onto the new stack */
2824 fprintf( outfile
, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2825 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip
) - STACK_SPACE
);
2826 fprintf( outfile
, "\tmovl %%eax,-4(%%edx)\n" );
2827 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp
) - STACK_SPACE
);
2828 fprintf( outfile
, "\tmovl %%eax,-8(%%edx)\n" );
2830 /* Restore the context structure */
2832 /* Note: we don't bother to restore %cs, %ds and %ss
2833 * changing them in 32-bit code is a recipe for disaster anyway
2835 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs
) - STACK_SPACE
);
2836 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2837 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs
) - STACK_SPACE
);
2838 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2839 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs
) - STACK_SPACE
);
2840 fprintf( outfile
, "\tmovw %%ax,%%gs\n" );
2842 fprintf( outfile
, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi
) - STACK_SPACE
);
2843 fprintf( outfile
, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi
) - STACK_SPACE
);
2844 fprintf( outfile
, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx
) - STACK_SPACE
);
2845 fprintf( outfile
, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx
) - STACK_SPACE
);
2846 fprintf( outfile
, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx
) - STACK_SPACE
);
2848 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags
) - STACK_SPACE
);
2849 fprintf( outfile
, "\tpushl %%eax\n" );
2850 fprintf( outfile
, "\tpopfl\n" );
2851 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax
) - STACK_SPACE
);
2853 fprintf( outfile
, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2854 fprintf( outfile
, "\tleal -8(%%ebp),%%esp\n" );
2855 fprintf( outfile
, "\tpopl %%ebp\n" );
2856 fprintf( outfile
, "\tret\n" );
2860 /*******************************************************************
2863 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
2865 static void BuildGlue( FILE *outfile
, FILE *infile
)
2871 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
2873 fprintf( outfile
, "#include \"builtin16.h\"\n" );
2874 fprintf( outfile
, "#include \"stackframe.h\"\n\n" );
2876 /* Build the callback glue functions */
2878 while (fgets( buffer
, sizeof(buffer
), infile
))
2880 if (strstr( buffer
, "### start build ###" )) break;
2882 while (fgets( buffer
, sizeof(buffer
), infile
))
2885 if ( (p
= strstr( buffer
, "CallFrom16_" )) != NULL
)
2887 char *q
, *profile
= p
+ strlen( "CallFrom16_" );
2888 for (q
= profile
; (*q
== '_') || isalpha(*q
); q
++ )
2891 for (q
= p
-1; q
> buffer
&& ((*q
== '_') || isalnum(*q
)); q
-- )
2893 if ( ++q
< p
) p
[-1] = '\0'; else q
= "";
2894 BuildCallFrom16Func( outfile
, profile
, q
, FALSE
);
2896 if ( (p
= strstr( buffer
, "CallTo16_" )) != NULL
)
2898 char *q
, *profile
= p
+ strlen( "CallTo16_" );
2899 for (q
= profile
; (*q
== '_') || isalpha(*q
); q
++ )
2902 for (q
= p
-1; q
> buffer
&& ((*q
== '_') || isalnum(*q
)); q
-- )
2904 if ( ++q
< p
) p
[-1] = '\0'; else q
= "";
2905 BuildCallTo16Func( outfile
, profile
, q
);
2907 if (strstr( buffer
, "### stop build ###" )) break;
2913 /*******************************************************************
2916 * Build the 16-bit callbacks
2918 static void BuildCall16( FILE *outfile
)
2922 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2923 fprintf( outfile
, "\t.text\n" );
2928 if (output_file_name
)
2931 getcwd(buffer
, sizeof(buffer
));
2932 fprintf( outfile
, "\t.file\t\"%s\"\n", output_file_name
);
2935 * The stabs help the internal debugger as they are an indication that it
2936 * is sensible to step into a thunk/trampoline.
2938 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2939 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name
);
2940 fprintf( outfile
, "Code_Start:\n\n" );
2943 fprintf( outfile
, PREFIX
"Call16_Start:\n" );
2944 fprintf( outfile
, "\t.globl "PREFIX
"Call16_Start\n" );
2945 fprintf( outfile
, "\t.byte 0\n\n" );
2948 /* Standard CallFrom16 routine (WORD return) */
2949 BuildCallFrom16Core( outfile
, FALSE
, FALSE
, TRUE
);
2951 /* Standard CallFrom16 routine (DWORD return) */
2952 BuildCallFrom16Core( outfile
, FALSE
, FALSE
, FALSE
);
2954 /* Register CallFrom16 routine */
2955 BuildCallFrom16Core( outfile
, TRUE
, FALSE
, FALSE
);
2957 /* C16ThkSL CallFrom16 routine */
2958 BuildCallFrom16Core( outfile
, FALSE
, TRUE
, FALSE
);
2960 /* Standard CallTo16 routine (WORD return) */
2961 BuildCallTo16Core( outfile
, TRUE
, FALSE
);
2963 /* Standard CallTo16 routine (DWORD return) */
2964 BuildCallTo16Core( outfile
, FALSE
, FALSE
);
2966 /* Register CallTo16 routine (16:16 retf) */
2967 BuildCallTo16Core( outfile
, FALSE
, 1 );
2969 /* Register CallTo16 routine (16:32 retf) */
2970 BuildCallTo16Core( outfile
, FALSE
, 2 );
2972 /* CBClientThunkSL routine */
2973 BuildCallTo32CBClient( outfile
, FALSE
);
2975 /* CBClientThunkSLEx routine */
2976 BuildCallTo32CBClient( outfile
, TRUE
);
2978 fprintf( outfile
, PREFIX
"Call16_End:\n" );
2979 fprintf( outfile
, "\t.globl "PREFIX
"Call16_End\n" );
2982 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2983 fprintf( outfile
, ".Letext:\n");
2986 /* The whole Call16_Ret segment must lie within the .data section */
2987 fprintf( outfile
, "\n\t.data\n" );
2988 fprintf( outfile
, "\t.globl " PREFIX
"Call16_Ret_Start\n" );
2989 fprintf( outfile
, PREFIX
"Call16_Ret_Start:\n" );
2991 /* Standard CallTo16 return stub */
2992 BuildRet16Func( outfile
);
2994 /* CBClientThunkSL return stub */
2995 BuildCallTo32CBClientRet( outfile
, FALSE
);
2997 /* CBClientThunkSLEx return stub */
2998 BuildCallTo32CBClientRet( outfile
, TRUE
);
3000 /* End of Call16_Ret segment */
3001 fprintf( outfile
, "\n\t.globl " PREFIX
"Call16_Ret_End\n" );
3002 fprintf( outfile
, PREFIX
"Call16_Ret_End:\n" );
3004 #else /* __i386__ */
3006 fprintf( outfile
, PREFIX
"Call16_Start:\n" );
3007 fprintf( outfile
, "\t.globl "PREFIX
"Call16_Start\n" );
3008 fprintf( outfile
, "\t.byte 0\n\n" );
3009 fprintf( outfile
, PREFIX
"Call16_End:\n" );
3010 fprintf( outfile
, "\t.globl "PREFIX
"Call16_End\n" );
3012 fprintf( outfile
, "\t.globl " PREFIX
"Call16_Ret_Start\n" );
3013 fprintf( outfile
, PREFIX
"Call16_Ret_Start:\n" );
3014 fprintf( outfile
, "\t.byte 0\n\n" );
3015 fprintf( outfile
, "\n\t.globl " PREFIX
"Call16_Ret_End\n" );
3016 fprintf( outfile
, PREFIX
"Call16_Ret_End:\n" );
3018 #endif /* __i386__ */
3021 /*******************************************************************
3024 * Build the 32-bit callbacks
3026 static void BuildCall32( FILE *outfile
)
3030 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
3031 fprintf( outfile
, "\t.text\n" );
3036 if (output_file_name
)
3039 getcwd(buffer
, sizeof(buffer
));
3040 fprintf( outfile
, "\t.file\t\"%s\"\n", output_file_name
);
3043 * The stabs help the internal debugger as they are an indication that it
3044 * is sensible to step into a thunk/trampoline.
3046 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
3047 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name
);
3048 fprintf( outfile
, "Code_Start:\n" );
3052 /* Build the 32-bit large stack callback */
3054 BuildCallTo32LargeStack( outfile
);
3056 /* Build the register callback function */
3058 BuildCallFrom32Regs( outfile
);
3061 fprintf( outfile
, "\t.text\n");
3062 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
3063 fprintf( outfile
, ".Letext:\n");
3066 #else /* __i386__ */
3068 /* Just to avoid an empty file */
3069 fprintf( outfile
, "\t.long 0\n" );
3071 #endif /* __i386__ */
3075 /*******************************************************************
3078 static void usage(void)
3081 "usage: build [-pic] [-o outfile] -spec SPEC_FILE\n"
3082 " build [-pic] [-o outfile] -glue SOURCE_FILE\n"
3083 " build [-pic] [-o outfile] -call16\n"
3084 " build [-pic] [-o outfile] -call32\n" );
3085 if (output_file_name
) unlink( output_file_name
);
3090 /*******************************************************************
3093 static FILE *open_input( const char *name
)
3099 input_file_name
= "<stdin>";
3102 input_file_name
= name
;
3103 if (!(f
= fopen( name
, "r" )))
3105 fprintf( stderr
, "Cannot open input file '%s'\n", name
);
3106 if (output_file_name
) unlink( output_file_name
);
3112 /*******************************************************************
3115 int main(int argc
, char **argv
)
3117 FILE *outfile
= stdout
;
3119 if (argc
< 2) usage();
3121 if (!strcmp( argv
[1], "-pic" ))
3126 if (argc
< 2) usage();
3129 if (!strcmp( argv
[1], "-o" ))
3131 output_file_name
= argv
[2];
3134 if (argc
< 2) usage();
3135 if (!(outfile
= fopen( output_file_name
, "w" )))
3137 fprintf( stderr
, "Unable to create output file '%s'\n", output_file_name
);
3142 /* Retrieve the selector values; this assumes that we are building
3143 * the asm files on the platform that will also run them. Probably
3144 * a safe assumption to make.
3146 GET_CS( Code_Selector
);
3147 GET_DS( Data_Selector
);
3149 if (!strcmp( argv
[1], "-spec" )) BuildSpecFile( outfile
, open_input( argv
[2] ) );
3150 else if (!strcmp( argv
[1], "-glue" )) BuildGlue( outfile
, open_input( argv
[2] ) );
3151 else if (!strcmp( argv
[1], "-call16" )) BuildCall16( outfile
);
3152 else if (!strcmp( argv
[1], "-call32" )) BuildCall32( outfile
);