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 int nb_imports
;
157 static int nb_entry_points
;
159 static const char *input_file_name
;
160 static const char *output_file_name
;
162 char *ParseBuffer
= NULL
;
167 static int UsePIC
= 0;
169 static int debugging
= 1;
171 /* Offset of a structure field relative to the start of the struct */
172 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
174 /* Offset of register relative to the start of the CONTEXT struct */
175 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT86,reg)
177 /* Offset of register relative to the start of the STACK16FRAME struct */
178 #define STACK16OFFSET(reg) STRUCTOFFSET(STACK16FRAME,reg)
180 /* Offset of register relative to the start of the STACK32FRAME struct */
181 #define STACK32OFFSET(reg) STRUCTOFFSET(STACK32FRAME,reg)
183 /* Offset of the stack pointer relative to %fs:(0) */
184 #define STACKOFFSET (STRUCTOFFSET(TEB,cur_stack))
186 static void BuildCallFrom16Func( FILE *outfile
, char *profile
, char *prefix
, int local
);
188 static void *xmalloc (size_t size
)
192 res
= malloc (size
? size
: 1);
195 fprintf (stderr
, "Virtual memory exhausted.\n");
196 if (output_file_name
) unlink( output_file_name
);
203 static void *xrealloc (void *ptr
, size_t size
)
205 void *res
= realloc (ptr
, size
);
208 fprintf (stderr
, "Virtual memory exhausted.\n");
209 if (output_file_name
) unlink( output_file_name
);
215 static char *xstrdup( const char *str
)
217 char *res
= strdup( str
);
220 fprintf (stderr
, "Virtual memory exhausted.\n");
221 if (output_file_name
) unlink( output_file_name
);
227 static void fatal_error( const char *msg
, ... )
230 va_start( valist
, msg
);
231 fprintf( stderr
, "%s:%d: ", input_file_name
, Line
);
232 vfprintf( stderr
, msg
, valist
);
234 if (output_file_name
) unlink( output_file_name
);
238 static int IsNumberString(char *s
)
247 static char *strupper(char *s
)
251 for(p
= s
; *p
!= '\0'; p
++)
257 static char * GetTokenInLine(void)
262 if (ParseNext
!= ParseBuffer
)
264 if (ParseSaveChar
== '\0')
266 *ParseNext
= ParseSaveChar
;
270 * Remove initial white space.
272 for (p
= ParseNext
; isspace(*p
); p
++)
275 if ((*p
== '\0') || (*p
== '#'))
282 if (*token
!= '(' && *token
!= ')')
283 while (*p
!= '\0' && *p
!= '(' && *p
!= ')' && !isspace(*p
))
293 static char * GetToken(void)
297 if (ParseBuffer
== NULL
)
299 ParseBuffer
= xmalloc(512);
300 ParseNext
= ParseBuffer
;
304 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
306 if (ParseBuffer
[0] != '#')
311 while ((token
= GetTokenInLine()) == NULL
)
313 ParseNext
= ParseBuffer
;
317 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
319 if (ParseBuffer
[0] != '#')
328 static int name_compare( const void *name1
, const void *name2
)
330 ORDDEF
*odp1
= *(ORDDEF
**)name1
;
331 ORDDEF
*odp2
= *(ORDDEF
**)name2
;
332 return strcmp( odp1
->name
, odp2
->name
);
335 /*******************************************************************
338 * Assign ordinals to all entry points.
340 static void AssignOrdinals(void)
344 /* sort the list of names */
345 qsort( Names
, nb_names
, sizeof(Names
[0]), name_compare
);
347 /* check for duplicate names */
348 for (i
= 0; i
< nb_names
- 1; i
++)
350 if (!strcmp( Names
[i
]->name
, Names
[i
+1]->name
))
352 Line
= MAX( Names
[i
]->lineno
, Names
[i
+1]->lineno
);
353 fatal_error( "'%s' redefined (previous definition at line %d)\n",
354 Names
[i
]->name
, MIN( Names
[i
]->lineno
, Names
[i
+1]->lineno
) );
358 /* start assigning from Base, or from 1 if no ordinal defined yet */
359 if (Base
== MAX_ORDINALS
) Base
= 1;
360 for (i
= 0, ordinal
= Base
; i
< nb_names
; i
++)
362 if (Names
[i
]->ordinal
!= -1) continue; /* already has an ordinal */
363 while (Ordinals
[ordinal
]) ordinal
++;
364 if (ordinal
>= MAX_ORDINALS
)
366 Line
= Names
[i
]->lineno
;
367 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS
);
369 Names
[i
]->ordinal
= ordinal
;
370 Ordinals
[ordinal
] = Names
[i
];
372 if (ordinal
> Limit
) Limit
= ordinal
;
376 /*******************************************************************
379 * Parse a variable definition.
381 static void ParseVariable( ORDDEF
*odp
)
386 int value_array_size
;
388 char *token
= GetToken();
389 if (*token
!= '(') fatal_error( "Expected '(' got '%s'\n", token
);
392 value_array_size
= 25;
393 value_array
= xmalloc(sizeof(*value_array
) * value_array_size
);
395 while ((token
= GetToken()) != NULL
)
400 value_array
[n_values
++] = strtol(token
, &endptr
, 0);
401 if (n_values
== value_array_size
)
403 value_array_size
+= 25;
404 value_array
= xrealloc(value_array
,
405 sizeof(*value_array
) * value_array_size
);
408 if (endptr
== NULL
|| *endptr
!= '\0')
409 fatal_error( "Expected number value, got '%s'\n", token
);
413 fatal_error( "End of file in variable declaration\n" );
415 odp
->u
.var
.n_values
= n_values
;
416 odp
->u
.var
.values
= xrealloc(value_array
, sizeof(*value_array
) * n_values
);
420 /*******************************************************************
421 * ParseExportFunction
423 * Parse a function definition.
425 static void ParseExportFunction( ORDDEF
*odp
)
433 if (odp
->type
== TYPE_STDCALL
)
434 fatal_error( "'stdcall' not supported for Win16\n" );
435 if (odp
->type
== TYPE_VARARGS
)
436 fatal_error( "'varargs' not supported for Win16\n" );
439 if ((odp
->type
== TYPE_PASCAL
) || (odp
->type
== TYPE_PASCAL_16
))
440 fatal_error( "'pascal' not supported for Win32\n" );
447 if (*token
!= '(') fatal_error( "Expected '(' got '%s'\n", token
);
449 for (i
= 0; i
< sizeof(odp
->u
.func
.arg_types
)-1; i
++)
455 if (!strcmp(token
, "word"))
456 odp
->u
.func
.arg_types
[i
] = 'w';
457 else if (!strcmp(token
, "s_word"))
458 odp
->u
.func
.arg_types
[i
] = 's';
459 else if (!strcmp(token
, "long") || !strcmp(token
, "segptr"))
460 odp
->u
.func
.arg_types
[i
] = 'l';
461 else if (!strcmp(token
, "ptr"))
462 odp
->u
.func
.arg_types
[i
] = 'p';
463 else if (!strcmp(token
, "str"))
464 odp
->u
.func
.arg_types
[i
] = 't';
465 else if (!strcmp(token
, "wstr"))
466 odp
->u
.func
.arg_types
[i
] = 'W';
467 else if (!strcmp(token
, "segstr"))
468 odp
->u
.func
.arg_types
[i
] = 'T';
469 else if (!strcmp(token
, "double"))
471 odp
->u
.func
.arg_types
[i
++] = 'l';
472 odp
->u
.func
.arg_types
[i
] = 'l';
474 else fatal_error( "Unknown variable type '%s'\n", token
);
476 if (SpecType
== SPEC_WIN32
)
478 if (strcmp(token
, "long") &&
479 strcmp(token
, "ptr") &&
480 strcmp(token
, "str") &&
481 strcmp(token
, "wstr") &&
482 strcmp(token
, "double"))
484 fatal_error( "Type '%s' not supported for Win32\n", token
);
488 if ((*token
!= ')') || (i
>= sizeof(odp
->u
.func
.arg_types
)))
489 fatal_error( "Too many arguments\n" );
491 odp
->u
.func
.arg_types
[i
] = '\0';
492 if ((odp
->type
== TYPE_STDCALL
) && !i
)
493 odp
->type
= TYPE_CDECL
; /* stdcall is the same as cdecl for 0 args */
494 strcpy(odp
->u
.func
.link_name
, GetToken());
498 /*******************************************************************
501 * Parse an 'equate' definition.
503 static void ParseEquate( ORDDEF
*odp
)
507 char *token
= GetToken();
508 int value
= strtol(token
, &endptr
, 0);
509 if (endptr
== NULL
|| *endptr
!= '\0')
510 fatal_error( "Expected number value, got '%s'\n", token
);
511 if (SpecType
== SPEC_WIN32
)
512 fatal_error( "'equate' not supported for Win32\n" );
513 odp
->u
.abs
.value
= value
;
517 /*******************************************************************
520 * Parse a 'stub' definition.
522 static void ParseStub( ORDDEF
*odp
)
524 odp
->u
.func
.arg_types
[0] = '\0';
525 strcpy( odp
->u
.func
.link_name
, STUB_CALLBACK
);
529 /*******************************************************************
532 * Parse an 'interrupt' definition.
534 static void ParseInterrupt( ORDDEF
*odp
)
538 if (SpecType
== SPEC_WIN32
)
539 fatal_error( "'interrupt' not supported for Win32\n" );
542 if (*token
!= '(') fatal_error( "Expected '(' got '%s'\n", token
);
545 if (*token
!= ')') fatal_error( "Expected ')' got '%s'\n", token
);
547 odp
->u
.func
.arg_types
[0] = '\0';
548 strcpy( odp
->u
.func
.link_name
, GetToken() );
552 /*******************************************************************
555 * Parse an 'extern' definition.
557 static void ParseExtern( ORDDEF
*odp
)
559 if (SpecType
== SPEC_WIN16
) fatal_error( "'extern' not supported for Win16\n" );
560 strcpy( odp
->u
.ext
.link_name
, GetToken() );
564 /*******************************************************************
567 * Parse a 'forward' definition.
569 static void ParseForward( ORDDEF
*odp
)
571 if (SpecType
== SPEC_WIN16
) fatal_error( "'forward' not supported for Win16\n" );
572 strcpy( odp
->u
.fwd
.link_name
, GetToken() );
576 /*******************************************************************
579 * Parse an ordinal definition.
581 static void ParseOrdinal(int ordinal
)
585 ORDDEF
*odp
= &EntryPoints
[nb_entry_points
++];
587 if (!(token
= GetToken())) fatal_error( "Expected type after ordinal\n" );
589 for (odp
->type
= 0; odp
->type
< TYPE_NBTYPES
; odp
->type
++)
590 if (TypeNames
[odp
->type
] && !strcmp( token
, TypeNames
[odp
->type
] ))
593 if (odp
->type
>= TYPE_NBTYPES
)
594 fatal_error( "Expected type after ordinal, found '%s' instead\n", token
);
596 if (!(token
= GetToken())) fatal_error( "Expected name after type\n" );
598 strcpy( odp
->name
, token
);
600 odp
->ordinal
= ordinal
;
607 ParseVariable( odp
);
610 ParseExportFunction( odp
);
612 /* ignore Win32 'register' routines on non-Intel archs */
613 if (SpecType
== SPEC_WIN32
)
625 ParseExportFunction( odp
);
628 ParseInterrupt( odp
);
648 if (ordinal
>= MAX_ORDINALS
) fatal_error( "Ordinal number %d too large\n", ordinal
);
649 if (ordinal
> Limit
) Limit
= ordinal
;
650 if (ordinal
< Base
) Base
= ordinal
;
651 odp
->ordinal
= ordinal
;
652 Ordinals
[ordinal
] = odp
;
655 if (!strcmp( odp
->name
, "@" ))
658 fatal_error( "Nameless function needs an explicit ordinal number\n" );
659 if (SpecType
!= SPEC_WIN32
)
660 fatal_error( "Nameless functions not supported for Win16\n" );
663 else Names
[nb_names
++] = odp
;
667 /*******************************************************************
672 static void ParseTopLevel(void)
676 while ((token
= GetToken()) != NULL
)
678 if (strcmp(token
, "name") == 0)
680 strcpy(DLLName
, GetToken());
682 if (!DLLFileName
[0]) sprintf( DLLFileName
, "%s.DLL", DLLName
);
684 else if (strcmp(token
, "file") == 0)
686 strcpy(DLLFileName
, GetToken());
687 strupper(DLLFileName
);
689 else if (strcmp(token
, "type") == 0)
692 if (!strcmp(token
, "win16" )) SpecType
= SPEC_WIN16
;
693 else if (!strcmp(token
, "win32" )) SpecType
= SPEC_WIN32
;
694 else fatal_error( "Type must be 'win16' or 'win32'\n" );
696 else if (strcmp(token
, "heap") == 0)
699 if (!IsNumberString(token
)) fatal_error( "Expected number after heap\n" );
700 DLLHeapSize
= atoi(token
);
702 else if (strcmp(token
, "init") == 0)
704 strcpy(DLLInitFunc
, GetToken());
705 if (SpecType
== SPEC_WIN16
)
706 fatal_error( "init cannot be used for Win16 spec files\n" );
708 fatal_error( "Expected function name after init\n" );
710 else if (strcmp(token
, "import") == 0)
712 if (nb_imports
>= MAX_IMPORTS
)
713 fatal_error( "Too many imports (limit %d)\n", MAX_IMPORTS
);
714 if (SpecType
!= SPEC_WIN32
)
715 fatal_error( "Imports not supported for Win16\n" );
716 DLLImports
[nb_imports
++] = xstrdup(GetToken());
718 else if (strcmp(token
, "@") == 0)
720 if (SpecType
!= SPEC_WIN32
)
721 fatal_error( "'@' ordinals not supported for Win16\n" );
724 else if (IsNumberString(token
))
726 ParseOrdinal( atoi(token
) );
729 fatal_error( "Expected name, id, length or ordinal\n" );
734 /*******************************************************************
737 * Store a list of ints into a byte array.
739 static int StoreVariableCode( unsigned char *buffer
, int size
, ORDDEF
*odp
)
746 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
747 buffer
[i
] = odp
->u
.var
.values
[i
];
750 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
751 ((unsigned short *)buffer
)[i
] = odp
->u
.var
.values
[i
];
754 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
755 ((unsigned int *)buffer
)[i
] = odp
->u
.var
.values
[i
];
758 return odp
->u
.var
.n_values
* size
;
762 /*******************************************************************
765 * Dump a byte stream into the assembly code.
767 static void DumpBytes( FILE *outfile
, const unsigned char *data
, int len
,
772 fprintf( outfile
, "\nstatic BYTE %s[] = \n{", label
);
774 for (i
= 0; i
< len
; i
++)
776 if (!(i
& 0x0f)) fprintf( outfile
, "\n " );
777 fprintf( outfile
, "%d", *data
++ );
778 if (i
< len
- 1) fprintf( outfile
, ", " );
780 fprintf( outfile
, "\n};\n" );
784 /*******************************************************************
787 * Build the in-memory representation of a 16-bit NE module, and dump it
788 * as a byte stream into the assembly code.
790 static int BuildModule16( FILE *outfile
, int max_code_offset
,
791 int max_data_offset
)
796 SEGTABLEENTRY
*pSegment
;
800 ET_BUNDLE
*bundle
= 0;
805 * OFSTRUCT File information
806 * SEGTABLEENTRY Segment 1 (code)
807 * SEGTABLEENTRY Segment 2 (data)
808 * WORD[2] Resource table (empty)
809 * BYTE[2] Imported names (empty)
810 * BYTE[n] Resident names table
811 * BYTE[n] Entry table
814 buffer
= xmalloc( 0x10000 );
816 pModule
= (NE_MODULE
*)buffer
;
817 memset( pModule
, 0, sizeof(*pModule
) );
818 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
821 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_BUILTIN
| NE_FFLAGS_LIBMODULE
;
823 pModule
->heap_size
= DLLHeapSize
;
824 pModule
->stack_size
= 0;
829 pModule
->seg_count
= 2;
830 pModule
->modref_count
= 0;
831 pModule
->nrname_size
= 0;
832 pModule
->modref_table
= 0;
833 pModule
->nrname_fpos
= 0;
834 pModule
->moveable_entries
= 0;
835 pModule
->alignment
= 0;
836 pModule
->truetype
= 0;
837 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
838 pModule
->misc_flags
= 0;
839 pModule
->dlls_to_init
= 0;
840 pModule
->nrname_handle
= 0;
841 pModule
->min_swap_area
= 0;
842 pModule
->expected_version
= 0;
843 pModule
->module32
= 0;
845 pModule
->self_loading_sel
= 0;
847 /* File information */
849 pFileInfo
= (OFSTRUCT
*)(pModule
+ 1);
850 pModule
->fileinfo
= (int)pFileInfo
- (int)pModule
;
851 memset( pFileInfo
, 0, sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
) );
852 pFileInfo
->cBytes
= sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
)
853 + strlen(DLLFileName
);
854 strcpy( pFileInfo
->szPathName
, DLLFileName
);
855 pstr
= (char *)pFileInfo
+ pFileInfo
->cBytes
+ 1;
857 #ifdef __i386__ /* FIXME: Alignment problems! */
861 pSegment
= (SEGTABLEENTRY
*)pstr
;
862 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
863 pSegment
->filepos
= 0;
864 pSegment
->size
= max_code_offset
;
866 pSegment
->minsize
= max_code_offset
;
870 pModule
->dgroup_entry
= (int)pSegment
- (int)pModule
;
871 pSegment
->filepos
= 0;
872 pSegment
->size
= max_data_offset
;
873 pSegment
->flags
= NE_SEGFLAGS_DATA
;
874 pSegment
->minsize
= max_data_offset
;
880 pword
= (WORD
*)pSegment
;
881 pModule
->res_table
= (int)pword
- (int)pModule
;
885 /* Imported names table */
887 pstr
= (char *)pword
;
888 pModule
->import_table
= (int)pstr
- (int)pModule
;
892 /* Resident names table */
894 pModule
->name_table
= (int)pstr
- (int)pModule
;
895 /* First entry is module name */
896 *pstr
= strlen(DLLName
);
897 strcpy( pstr
+ 1, DLLName
);
900 pstr
+= sizeof(WORD
);
901 /* Store all ordinals */
902 for (i
= 1; i
<= Limit
; i
++)
904 ORDDEF
*odp
= Ordinals
[i
];
905 if (!odp
|| !odp
->name
[0]) continue;
906 *pstr
= strlen( odp
->name
);
907 strcpy( pstr
+ 1, odp
->name
);
908 strupper( pstr
+ 1 );
911 pstr
+= sizeof(WORD
);
917 pModule
->entry_table
= (int)pstr
- (int)pModule
;
918 for (i
= 1; i
<= Limit
; i
++)
921 ORDDEF
*odp
= Ordinals
[i
];
932 selector
= 1; /* Code selector */
938 selector
= 2; /* Data selector */
942 selector
= 0xfe; /* Constant selector */
946 selector
= 0; /* Invalid selector */
953 if ( bundle
&& bundle
->last
+1 == i
)
958 bundle
->next
= (char *)pstr
- (char *)pModule
;
960 bundle
= (ET_BUNDLE
*)pstr
;
964 pstr
+= sizeof(ET_BUNDLE
);
967 /* FIXME: is this really correct ?? */
968 entry
= (ET_ENTRY
*)pstr
;
969 entry
->type
= 0xff; /* movable */
970 entry
->flags
= 3; /* exported & public data */
971 entry
->segnum
= selector
;
972 entry
->offs
= odp
->offset
;
973 pstr
+= sizeof(ET_ENTRY
);
978 /* Dump the module content */
980 DumpBytes( outfile
, (char *)pModule
, (int)pstr
- (int)pModule
,
982 return (int)pstr
- (int)pModule
;
986 /*******************************************************************
989 * Build a Win32 C file from a spec file.
991 static int BuildSpec32File( FILE *outfile
)
994 int i
, fwd_size
= 0, have_regs
= FALSE
;
998 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
1000 fprintf( outfile
, "#include \"builtin32.h\"\n\n" );
1001 fprintf( outfile
, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
1004 /* Output the DLL functions prototypes */
1006 for (i
= 0, odp
= EntryPoints
; i
< nb_entry_points
; i
++, odp
++)
1011 fprintf( outfile
, "extern void %s();\n", odp
->u
.ext
.link_name
);
1016 fprintf( outfile
, "extern void %s();\n", odp
->u
.func
.link_name
);
1019 fwd_size
+= strlen(odp
->u
.fwd
.link_name
) + 1;
1022 fprintf( outfile
, "extern void __regs_%d();\n", odp
->ordinal
);
1026 fprintf( outfile
, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
1027 odp
->ordinal
, DLLName
, odp
->ordinal
);
1030 fprintf(stderr
,"build: function type %d not available for Win32\n",
1036 /* Output LibMain function */
1037 if (DLLInitFunc
[0]) fprintf( outfile
, "extern void %s();\n", DLLInitFunc
);
1040 /* Output code for all register functions */
1044 fprintf( outfile
, "#ifndef __GNUC__\n" );
1045 fprintf( outfile
, "static void __asm__dummy() {\n" );
1046 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
1047 for (i
= 0, odp
= EntryPoints
; i
< nb_entry_points
; i
++, odp
++)
1049 if (odp
->type
!= TYPE_REGISTER
) continue;
1051 "__asm__(\".align 4\\n\\t\"\n"
1052 " \".type " PREFIX
"__regs_%d,@function\\n\\t\"\n"
1053 " \"" PREFIX
"__regs_%d:\\n\\t\"\n"
1054 " \"call " PREFIX
"CALL32_Regs\\n\\t\"\n"
1055 " \".long " PREFIX
"%s\\n\\t\"\n"
1056 " \".byte %d,%d\");\n",
1057 odp
->ordinal
, odp
->ordinal
, odp
->u
.func
.link_name
,
1058 4 * strlen(odp
->u
.func
.arg_types
),
1059 4 * strlen(odp
->u
.func
.arg_types
) );
1061 fprintf( outfile
, "#ifndef __GNUC__\n" );
1062 fprintf( outfile
, "}\n" );
1063 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
1066 /* Output the DLL functions table */
1068 fprintf( outfile
, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1070 for (i
= Base
; i
<= Limit
; i
++)
1072 ORDDEF
*odp
= Ordinals
[i
];
1073 if (!odp
) fprintf( outfile
, " 0" );
1074 else switch(odp
->type
)
1077 fprintf( outfile
, " %s", odp
->u
.ext
.link_name
);
1082 fprintf( outfile
, " %s", odp
->u
.func
.link_name
);
1085 fprintf( outfile
, " __stub_%d", i
);
1088 fprintf( outfile
, " __regs_%d", i
);
1091 fprintf( outfile
, " (ENTRYPOINT32)\"%s\"", odp
->u
.fwd
.link_name
);
1096 if (i
< Limit
) fprintf( outfile
, ",\n" );
1098 fprintf( outfile
, "\n};\n\n" );
1100 /* Output the DLL names table */
1102 fprintf( outfile
, "static const char * const FuncNames[%d] =\n{\n", nb_names
);
1103 for (i
= 0; i
< nb_names
; i
++)
1105 if (i
) fprintf( outfile
, ",\n" );
1106 fprintf( outfile
, " \"%s\"", Names
[i
]->name
);
1108 fprintf( outfile
, "\n};\n\n" );
1110 /* Output the DLL ordinals table */
1112 fprintf( outfile
, "static const unsigned short FuncOrdinals[%d] =\n{\n", nb_names
);
1113 for (i
= 0; i
< nb_names
; i
++)
1115 if (i
) fprintf( outfile
, ",\n" );
1116 fprintf( outfile
, " %d", Names
[i
]->ordinal
- Base
);
1118 fprintf( outfile
, "\n};\n\n" );
1120 /* Output the DLL argument types */
1122 fprintf( outfile
, "static const unsigned int ArgTypes[%d] =\n{\n",
1124 for (i
= Base
; i
<= Limit
; i
++)
1126 ORDDEF
*odp
= Ordinals
[i
];
1127 unsigned int j
, mask
= 0;
1129 ((odp
->type
== TYPE_STDCALL
) || (odp
->type
== TYPE_CDECL
) ||
1130 (odp
->type
== TYPE_REGISTER
)))
1131 for (j
= 0; odp
->u
.func
.arg_types
[j
]; j
++)
1133 if (odp
->u
.func
.arg_types
[j
] == 't') mask
|= 1<< (j
*2);
1134 if (odp
->u
.func
.arg_types
[j
] == 'W') mask
|= 2<< (j
*2);
1136 fprintf( outfile
, " %d", mask
);
1137 if (i
< Limit
) fprintf( outfile
, ",\n" );
1139 fprintf( outfile
, "\n};\n\n" );
1141 /* Output the DLL functions arguments */
1143 fprintf( outfile
, "static const unsigned char FuncArgs[%d] =\n{\n",
1145 for (i
= Base
; i
<= Limit
; i
++)
1147 unsigned char args
= 0xff;
1148 ORDDEF
*odp
= Ordinals
[i
];
1149 if (odp
) switch(odp
->type
)
1152 args
= (unsigned char)strlen(odp
->u
.func
.arg_types
);
1155 args
= 0x80 | (unsigned char)strlen(odp
->u
.func
.arg_types
);
1158 args
= 0x40 | (unsigned char)strlen(odp
->u
.func
.arg_types
);
1167 fprintf( outfile
, " 0x%02x", args
);
1168 if (i
< Limit
) fprintf( outfile
, ",\n" );
1170 fprintf( outfile
, "\n};\n\n" );
1172 /* Output the DLL imports */
1176 fprintf( outfile
, "static const char * const Imports[%d] =\n{\n", nb_imports
);
1177 for (i
= 0; i
< nb_imports
; i
++)
1179 fprintf( outfile
, " \"%s\"", DLLImports
[i
] );
1180 if (i
< nb_imports
-1) fprintf( outfile
, ",\n" );
1182 fprintf( outfile
, "\n};\n\n" );
1185 /* Output the DLL descriptor */
1187 fprintf( outfile
, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1189 fprintf( outfile
, " \"%s\",\n", DLLName
);
1190 fprintf( outfile
, " \"%s\",\n", DLLFileName
);
1191 fprintf( outfile
, " %d,\n", Base
);
1192 fprintf( outfile
, " %d,\n", Limit
- Base
+ 1 );
1193 fprintf( outfile
, " %d,\n", nb_names
);
1194 fprintf( outfile
, " %d,\n", nb_imports
);
1195 fprintf( outfile
, " %d,\n", (fwd_size
+ 3) & ~3 );
1202 fprintf( outfile
, " %s,\n", nb_imports
? "Imports" : "0" );
1203 fprintf( outfile
, " %s\n", DLLInitFunc
[0] ? DLLInitFunc
: "0" );
1204 fprintf( outfile
, "};\n" );
1208 /*******************************************************************
1211 static int Spec16TypeCompare( const void *e1
, const void *e2
)
1213 const ORDDEF
*odp1
= *(const ORDDEF
**)e1
;
1214 const ORDDEF
*odp2
= *(const ORDDEF
**)e2
;
1216 int type1
= (odp1
->type
== TYPE_CDECL
) ? 0
1217 : (odp1
->type
== TYPE_REGISTER
) ? 3
1218 : (odp1
->type
== TYPE_INTERRUPT
) ? 4
1219 : (odp1
->type
== TYPE_PASCAL_16
) ? 1 : 2;
1221 int type2
= (odp2
->type
== TYPE_CDECL
) ? 0
1222 : (odp2
->type
== TYPE_REGISTER
) ? 3
1223 : (odp2
->type
== TYPE_INTERRUPT
) ? 4
1224 : (odp2
->type
== TYPE_PASCAL_16
) ? 1 : 2;
1226 int retval
= type1
- type2
;
1228 retval
= strcmp( odp1
->u
.func
.arg_types
, odp2
->u
.func
.arg_types
);
1233 /*******************************************************************
1236 * Build a Win16 assembly file from a spec file.
1238 static int BuildSpec16File( FILE *outfile
)
1240 ORDDEF
**type
, **typelist
;
1241 int i
, nFuncs
, nTypes
;
1242 int code_offset
, data_offset
, module_size
;
1243 unsigned char *data
;
1247 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
1249 fprintf( outfile
, "#define __FLATCS__ 0x%04x\n", Code_Selector
);
1250 fprintf( outfile
, "#include \"builtin16.h\"\n\n" );
1252 data
= (unsigned char *)xmalloc( 0x10000 );
1253 memset( data
, 0, 16 );
1257 /* Build sorted list of all argument types, without duplicates */
1259 typelist
= (ORDDEF
**)calloc( Limit
+1, sizeof(ORDDEF
*) );
1261 for (i
= nFuncs
= 0; i
<= Limit
; i
++)
1263 ORDDEF
*odp
= Ordinals
[i
];
1268 case TYPE_INTERRUPT
:
1271 case TYPE_PASCAL_16
:
1273 typelist
[nFuncs
++] = odp
;
1280 qsort( typelist
, nFuncs
, sizeof(ORDDEF
*), Spec16TypeCompare
);
1283 while ( i
< nFuncs
)
1285 typelist
[nTypes
++] = typelist
[i
++];
1286 while ( i
< nFuncs
&& Spec16TypeCompare( typelist
+ i
, typelist
+ nTypes
-1 ) == 0 )
1290 /* Output CallFrom16 routines needed by this .spec file */
1292 for ( i
= 0; i
< nTypes
; i
++ )
1296 sprintf( profile
, "%s_%s_%s",
1297 (typelist
[i
]->type
== TYPE_CDECL
) ? "c" : "p",
1298 (typelist
[i
]->type
== TYPE_REGISTER
) ? "regs" :
1299 (typelist
[i
]->type
== TYPE_INTERRUPT
) ? "intr" :
1300 (typelist
[i
]->type
== TYPE_PASCAL_16
) ? "word" : "long",
1301 typelist
[i
]->u
.func
.arg_types
);
1303 BuildCallFrom16Func( outfile
, profile
, DLLName
, TRUE
);
1306 /* Output the DLL functions prototypes */
1308 for (i
= 0; i
<= Limit
; i
++)
1310 ORDDEF
*odp
= Ordinals
[i
];
1315 case TYPE_INTERRUPT
:
1318 case TYPE_PASCAL_16
:
1319 fprintf( outfile
, "extern void %s();\n", odp
->u
.func
.link_name
);
1326 /* Output code segment */
1328 fprintf( outfile
, "\nstatic struct\n{\n CALLFROM16 call[%d];\n"
1329 " ENTRYPOINT16 entry[%d];\n} Code_Segment = \n{\n {\n",
1333 for ( i
= 0; i
< nTypes
; i
++ )
1335 char profile
[101], *arg
;
1338 sprintf( profile
, "%s_%s_%s",
1339 (typelist
[i
]->type
== TYPE_CDECL
) ? "c" : "p",
1340 (typelist
[i
]->type
== TYPE_REGISTER
) ? "regs" :
1341 (typelist
[i
]->type
== TYPE_INTERRUPT
) ? "intr" :
1342 (typelist
[i
]->type
== TYPE_PASCAL_16
) ? "word" : "long",
1343 typelist
[i
]->u
.func
.arg_types
);
1345 if ( typelist
[i
]->type
!= TYPE_CDECL
)
1346 for ( arg
= typelist
[i
]->u
.func
.arg_types
; *arg
; arg
++ )
1349 case 'w': /* word */
1350 case 's': /* s_word */
1354 case 'l': /* long or segmented pointer */
1355 case 'T': /* segmented pointer to null-terminated string */
1356 case 'p': /* linear pointer */
1357 case 't': /* linear pointer to null-terminated string */
1362 if ( typelist
[i
]->type
== TYPE_INTERRUPT
)
1365 fprintf( outfile
, " CF16_%s( %s_CallFrom16_%s, %d, \"%s\" ),\n",
1366 ( typelist
[i
]->type
== TYPE_REGISTER
1367 || typelist
[i
]->type
== TYPE_INTERRUPT
)? "REGS":
1368 typelist
[i
]->type
== TYPE_PASCAL_16
? "WORD" : "LONG",
1369 DLLName
, profile
, argsize
, profile
);
1371 code_offset
+= sizeof(CALLFROM16
);
1373 fprintf( outfile
, " },\n {\n" );
1375 for (i
= 0; i
<= Limit
; i
++)
1377 ORDDEF
*odp
= Ordinals
[i
];
1382 odp
->offset
= LOWORD(odp
->u
.abs
.value
);
1386 odp
->offset
= data_offset
;
1387 data_offset
+= StoreVariableCode( data
+ data_offset
, 1, odp
);
1391 odp
->offset
= data_offset
;
1392 data_offset
+= StoreVariableCode( data
+ data_offset
, 2, odp
);
1396 odp
->offset
= data_offset
;
1397 data_offset
+= StoreVariableCode( data
+ data_offset
, 4, odp
);
1401 case TYPE_INTERRUPT
:
1404 case TYPE_PASCAL_16
:
1406 type
= bsearch( &odp
, typelist
, nTypes
, sizeof(ORDDEF
*), Spec16TypeCompare
);
1409 fprintf( outfile
, " /* %s.%d */ ", DLLName
, i
);
1410 fprintf( outfile
, "EP( %s, %d /* %s_%s_%s */ ),\n",
1411 odp
->u
.func
.link_name
,
1412 (type
-typelist
)*sizeof(CALLFROM16
) -
1413 (code_offset
+ sizeof(ENTRYPOINT16
)),
1414 (odp
->type
== TYPE_CDECL
) ? "c" : "p",
1415 (odp
->type
== TYPE_REGISTER
) ? "regs" :
1416 (odp
->type
== TYPE_INTERRUPT
) ? "intr" :
1417 (odp
->type
== TYPE_PASCAL_16
) ? "word" : "long",
1418 odp
->u
.func
.arg_types
);
1420 odp
->offset
= code_offset
;
1421 code_offset
+= sizeof(ENTRYPOINT16
);
1425 fprintf(stderr
,"build: function type %d not available for Win16\n",
1431 fprintf( outfile
, " }\n};\n" );
1433 /* Output data segment */
1435 DumpBytes( outfile
, data
, data_offset
, "Data_Segment" );
1437 /* Build the module */
1439 module_size
= BuildModule16( outfile
, code_offset
, data_offset
);
1441 /* Output the DLL descriptor */
1443 fprintf( outfile
, "\nWIN16_DESCRIPTOR %s_Descriptor = \n{\n", DLLName
);
1444 fprintf( outfile
, " \"%s\",\n", DLLName
);
1445 fprintf( outfile
, " Module,\n" );
1446 fprintf( outfile
, " sizeof(Module),\n" );
1447 fprintf( outfile
, " (BYTE *)&Code_Segment,\n" );
1448 fprintf( outfile
, " (BYTE *)Data_Segment\n" );
1449 fprintf( outfile
, "};\n" );
1455 /*******************************************************************
1458 * Build an assembly file from a spec file.
1460 static void BuildSpecFile( FILE *outfile
, FILE *infile
)
1468 BuildSpec16File( outfile
);
1471 BuildSpec32File( outfile
);
1474 fatal_error( "Missing 'type' declaration\n" );
1479 /*******************************************************************
1480 * BuildCallFrom16Func
1482 * Build a 16-bit-to-Wine callback glue function.
1484 * The generated routines are intended to be used as argument conversion
1485 * routines to be called by the CallFrom16... core. Thus, the prototypes of
1486 * the generated routines are (see also CallFrom16):
1488 * extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
1489 * extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
1490 * extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args,
1491 * CONTEXT86 *context );
1492 * extern void WINAPI PREFIX_CallFrom16_C_intr_xxx( FARPROC func, LPBYTE args,
1493 * CONTEXT86 *context );
1495 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
1496 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
1497 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1498 * 'T'=segmented pointer to null-terminated string).
1500 * The generated routines fetch the arguments from the 16-bit stack (pointed
1501 * to by 'args'); the offsets of the single argument values are computed
1502 * according to the calling convention and the argument types. Then, the
1503 * 32-bit entry point is called with these arguments.
1505 * For register functions, the arguments (if present) are converted just
1506 * the same as for normal functions, but in addition the CONTEXT86 pointer
1507 * filled with the current register values is passed to the 32-bit routine.
1508 * (An 'intr' interrupt handler routine is treated exactly like a register
1509 * routine, except that upon return, the flags word pushed onto the stack
1510 * by the interrupt is removed by the 16-bit call stub.)
1513 static void BuildCallFrom16Func( FILE *outfile
, char *profile
, char *prefix
, int local
)
1515 int i
, pos
, argsize
= 0;
1519 char *args
= profile
+ 7;
1522 /* Parse function type */
1524 if (!strncmp( "c_", profile
, 2 )) usecdecl
= 1;
1525 else if (strncmp( "p_", profile
, 2 ))
1527 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1531 if (!strncmp( "word_", profile
+ 2, 5 )) short_ret
= 1;
1532 else if (!strncmp( "regs_", profile
+ 2, 5 )) reg_func
= 1;
1533 else if (!strncmp( "intr_", profile
+ 2, 5 )) reg_func
= 2;
1534 else if (strncmp( "long_", profile
+ 2, 5 ))
1536 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1540 for ( i
= 0; args
[i
]; i
++ )
1543 case 'w': /* word */
1544 case 's': /* s_word */
1548 case 'l': /* long or segmented pointer */
1549 case 'T': /* segmented pointer to null-terminated string */
1550 case 'p': /* linear pointer */
1551 case 't': /* linear pointer to null-terminated string */
1556 ret_type
= reg_func
? "void" : short_ret
? "WORD" : "LONG";
1558 fprintf( outfile
, "typedef %s WINAPI (*proc_%s_t)( ",
1559 ret_type
, profile
);
1561 for ( i
= 0; args
[i
]; i
++ )
1563 if ( i
) fprintf( outfile
, ", " );
1566 case 'w': fprintf( outfile
, "WORD" ); break;
1567 case 's': fprintf( outfile
, "INT16" ); break;
1568 case 'l': case 'T': fprintf( outfile
, "LONG" ); break;
1569 case 'p': case 't': fprintf( outfile
, "LPVOID" ); break;
1573 fprintf( outfile
, "%sstruct _CONTEXT86 *", i
? ", " : "" );
1575 fprintf( outfile
, "void" );
1576 fprintf( outfile
, " );\n" );
1578 fprintf( outfile
, "%s%s WINAPI %s_CallFrom16_%s( FARPROC proc, LPBYTE args%s )\n{\n",
1579 local
? "static " : "", ret_type
, prefix
, profile
,
1580 reg_func
? ", struct _CONTEXT86 *context" : "" );
1582 fprintf( outfile
, " %s((proc_%s_t) proc) (\n",
1583 reg_func
? "" : "return ", profile
);
1585 pos
= !usecdecl
? argsize
: 0;
1586 for ( i
= 0; args
[i
]; i
++ )
1588 if ( i
) fprintf( outfile
, ",\n" );
1589 fprintf( outfile
, " " );
1592 case 'w': /* word */
1593 if ( !usecdecl
) pos
-= 2;
1594 fprintf( outfile
, "*(WORD *)(args+%d)", pos
);
1595 if ( usecdecl
) pos
+= 2;
1598 case 's': /* s_word */
1599 if ( !usecdecl
) pos
-= 2;
1600 fprintf( outfile
, "*(INT16 *)(args+%d)", pos
);
1601 if ( usecdecl
) pos
+= 2;
1604 case 'l': /* long or segmented pointer */
1605 case 'T': /* segmented pointer to null-terminated string */
1606 if ( !usecdecl
) pos
-= 4;
1607 fprintf( outfile
, "*(LONG *)(args+%d)", pos
);
1608 if ( usecdecl
) pos
+= 4;
1611 case 'p': /* linear pointer */
1612 case 't': /* linear pointer to null-terminated string */
1613 if ( !usecdecl
) pos
-= 4;
1614 fprintf( outfile
, "PTR_SEG_TO_LIN( *(SEGPTR *)(args+%d) )", pos
);
1615 if ( usecdecl
) pos
+= 4;
1619 fprintf( stderr
, "Unknown arg type '%c'\n", args
[i
] );
1623 fprintf( outfile
, "%s context", i
? ",\n" : "" );
1624 fprintf( outfile
, " );\n}\n\n" );
1627 /*******************************************************************
1630 * Build a Wine-to-16-bit callback glue function.
1632 * Prototypes for the CallTo16 functions:
1633 * extern WORD CALLBACK PREFIX_CallTo16_word_xxx( FARPROC16 func, args... );
1634 * extern LONG CALLBACK PREFIX_CallTo16_long_xxx( FARPROC16 func, args... );
1636 * These routines are provided solely for convenience; they simply
1637 * write the arguments onto the 16-bit stack, and call the appropriate
1638 * CallTo16... core routine.
1640 * If you have more sophisticated argument conversion requirements than
1641 * are provided by these routines, you might as well call the core
1642 * routines by yourself.
1645 static void BuildCallTo16Func( FILE *outfile
, char *profile
, char *prefix
)
1647 char *args
= profile
+ 5;
1648 int i
, argsize
= 0, short_ret
= 0;
1650 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1651 else if (strncmp( "long_", profile
, 5 ))
1653 fprintf( stderr
, "Invalid function name '%s'.\n", profile
);
1657 fprintf( outfile
, "%s %s_CallTo16_%s( FARPROC16 proc",
1658 short_ret
? "WORD" : "LONG", prefix
, profile
);
1660 for ( i
= 0; args
[i
]; i
++ )
1662 fprintf( outfile
, ", " );
1665 case 'w': fprintf( outfile
, "WORD" ); argsize
+= 2; break;
1666 case 'l': fprintf( outfile
, "LONG" ); argsize
+= 4; break;
1668 fprintf( outfile
, " arg%d", i
+1 );
1670 fprintf( outfile
, " )\n{\n" );
1673 fprintf( outfile
, " LPBYTE args = (LPBYTE)CURRENT_STACK16;\n" );
1676 for ( i
= 0; args
[i
]; i
++ )
1680 case 'w': fprintf( outfile
, " args -= sizeof(WORD); *(WORD" ); break;
1681 case 'l': fprintf( outfile
, " args -= sizeof(LONG); *(LONG" ); break;
1682 default: fprintf( stderr
, "Unexpected case '%c' in BuildCallTo16Func\n",
1685 fprintf( outfile
, " *)args = arg%d;\n", i
+1 );
1688 fprintf( outfile
, " return CallTo16%s( proc, %d );\n}\n\n",
1689 short_ret
? "Word" : "Long", argsize
);
1694 /*******************************************************************
1695 * BuildCallFrom16Core
1697 * This routine builds the core routines used in 16->32 thunks:
1698 * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
1700 * These routines are intended to be called via a far call (with 32-bit
1701 * operand size) from 16-bit code. The 16-bit code stub must push %bp,
1702 * the 32-bit entry point to be called, and the argument conversion
1703 * routine to be used (see stack layout below).
1705 * The core routine completes the STACK16FRAME on the 16-bit stack and
1706 * switches to the 32-bit stack. Then, the argument conversion routine
1707 * is called; it gets passed the 32-bit entry point and a pointer to the
1708 * 16-bit arguments (on the 16-bit stack) as parameters. (You can either
1709 * use conversion routines automatically generated by BuildCallFrom16,
1710 * or write your own for special purposes.)
1712 * The conversion routine must call the 32-bit entry point, passing it
1713 * the converted arguments, and return its return value to the core.
1714 * After the conversion routine has returned, the core switches back
1715 * to the 16-bit stack, converts the return value to the DX:AX format
1716 * (CallFrom16Long), and returns to the 16-bit call stub. All parameters,
1717 * including %bp, are popped off the stack.
1719 * The 16-bit call stub now returns to the caller, popping the 16-bit
1720 * arguments if necessary (pascal calling convention).
1722 * In the case of a 'register' function, CallFrom16Register fills a
1723 * CONTEXT86 structure with the values all registers had at the point
1724 * the first instruction of the 16-bit call stub was about to be
1725 * executed. A pointer to this CONTEXT86 is passed as third parameter
1726 * to the argument conversion routine, which typically passes it on
1727 * to the called 32-bit entry point.
1729 * CallFrom16Thunk is a special variant used by the implementation of
1730 * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
1731 * implemented as follows:
1732 * On entry, the EBX register is set up to contain a flat pointer to the
1733 * 16-bit stack such that EBX+22 points to the first argument.
1734 * Then, the entry point is called, while EBP is set up to point
1735 * to the return address (on the 32-bit stack).
1736 * The called function returns with CX set to the number of bytes
1737 * to be popped of the caller's stack.
1739 * Stack layout upon entry to the core routine (STACK16FRAME):
1741 * (sp+24) word first 16-bit arg
1745 * (sp+14) long 32-bit entry point (reused for Win16 mutex recursion count)
1746 * (sp+12) word ip of actual entry point (necessary for relay debugging)
1747 * (sp+8) long relay (argument conversion) function entry point
1748 * (sp+4) long cs of 16-bit entry point
1749 * (sp) long ip of 16-bit entry point
1751 * Added on the stack:
1752 * (sp-2) word saved gs
1753 * (sp-4) word saved fs
1754 * (sp-6) word saved es
1755 * (sp-8) word saved ds
1756 * (sp-12) long saved ebp
1757 * (sp-16) long saved ecx
1758 * (sp-20) long saved edx
1759 * (sp-24) long saved previous stack
1761 static void BuildCallFrom16Core( FILE *outfile
, int reg_func
, int thunk
, int short_ret
)
1763 char *name
= thunk
? "Thunk" : reg_func
? "Register" : short_ret
? "Word" : "Long";
1765 /* Function header */
1766 fprintf( outfile
, "\n\t.align 4\n" );
1768 fprintf( outfile
, ".stabs \"CallFrom16%s:F1\",36,0,0," PREFIX
"CallFrom16%s\n",
1771 fprintf( outfile
, "\t.globl " PREFIX
"CallFrom16%s\n", name
);
1772 fprintf( outfile
, PREFIX
"CallFrom16%s:\n", name
);
1774 /* Create STACK16FRAME (except STACK32FRAME link) */
1775 fprintf( outfile
, "\tpushw %%gs\n" );
1776 fprintf( outfile
, "\tpushw %%fs\n" );
1777 fprintf( outfile
, "\tpushw %%es\n" );
1778 fprintf( outfile
, "\tpushw %%ds\n" );
1779 fprintf( outfile
, "\tpushl %%ebp\n" );
1780 fprintf( outfile
, "\tpushl %%ecx\n" );
1781 fprintf( outfile
, "\tpushl %%edx\n" );
1783 /* Save original EFlags register */
1784 fprintf( outfile
, "\tpushfl\n" );
1788 /* Get Global Offset Table into %ecx */
1789 fprintf( outfile
, "\tcall .LCallFrom16%s.getgot1\n", name
);
1790 fprintf( outfile
, ".LCallFrom16%s.getgot1:\n", name
);
1791 fprintf( outfile
, "\tpopl %%ecx\n" );
1792 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot1], %%ecx\n", name
);
1795 /* Load 32-bit segment registers */
1796 fprintf( outfile
, "\tmovw $0x%04x, %%dx\n", Data_Selector
);
1798 fprintf( outfile
, "\tdata16\n");
1800 fprintf( outfile
, "\tmovw %%dx, %%ds\n" );
1802 fprintf( outfile
, "\tdata16\n");
1804 fprintf( outfile
, "\tmovw %%dx, %%es\n" );
1808 fprintf( outfile
, "\tmovl " PREFIX
"SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
1809 fprintf( outfile
, "\tmovw (%%edx), %%fs\n" );
1812 fprintf( outfile
, "\tmovw " PREFIX
"SYSLEVEL_Win16CurrentTeb, %%fs\n" );
1814 /* Get address of ldt_copy array into %ecx */
1816 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy@GOT(%%ecx), %%ecx\n" );
1818 fprintf( outfile
, "\tmovl $" PREFIX
"ldt_copy, %%ecx\n" );
1820 /* Translate STACK16FRAME base to flat offset in %edx */
1821 fprintf( outfile
, "\tmovw %%ss, %%dx\n" );
1822 fprintf( outfile
, "\tandl $0xfff8, %%edx\n" );
1823 fprintf( outfile
, "\tmovl (%%ecx,%%edx), %%edx\n" );
1824 fprintf( outfile
, "\tmovzwl %%sp, %%ebp\n" );
1825 fprintf( outfile
, "\tleal (%%ebp,%%edx), %%edx\n" );
1827 /* Get saved flags into %ecx */
1828 fprintf( outfile
, "\tpopl %%ecx\n" );
1830 /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
1831 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET
);
1832 fprintf( outfile
, "\tpushl %%ebp\n" );
1836 fprintf( outfile
,"\tdata16\n");
1838 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET
+ 2 );
1839 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET
);
1840 fprintf( outfile
, "\tpushl %%ds\n" );
1841 fprintf( outfile
, "\tpopl %%ss\n" );
1842 fprintf( outfile
, "\tmovl %%ebp, %%esp\n" );
1843 fprintf( outfile
, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME
, ebp
) );
1847 STACK16FRAME is completely set up
1848 DS, ES, SS: flat data segment
1850 ESP: points to last STACK32FRAME
1851 EBP: points to ebp member of last STACK32FRAME
1852 EDX: points to current STACK16FRAME
1853 ECX: contains saved flags
1854 all other registers: unchanged */
1856 /* Special case: C16ThkSL stub */
1859 /* Set up registers as expected and call thunk */
1860 fprintf( outfile
, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME
)-22 );
1861 fprintf( outfile
, "\tleal -4(%%esp), %%ebp\n" );
1863 fprintf( outfile
, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point
) );
1865 /* Switch stack back */
1866 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
1867 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
1868 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
1870 /* Restore registers and return directly to caller */
1871 fprintf( outfile
, "\taddl $8, %%esp\n" );
1872 fprintf( outfile
, "\tpopl %%ebp\n" );
1873 fprintf( outfile
, "\tpopw %%ds\n" );
1874 fprintf( outfile
, "\tpopw %%es\n" );
1875 fprintf( outfile
, "\tpopw %%fs\n" );
1876 fprintf( outfile
, "\tpopw %%gs\n" );
1877 fprintf( outfile
, "\taddl $20, %%esp\n" );
1879 fprintf( outfile
, "\txorb %%ch, %%ch\n" );
1880 fprintf( outfile
, "\tpopl %%ebx\n" );
1881 fprintf( outfile
, "\taddw %%cx, %%sp\n" );
1882 fprintf( outfile
, "\tpush %%ebx\n" );
1884 fprintf( outfile
, "\t.byte 0x66\n" );
1885 fprintf( outfile
, "\tlret\n" );
1891 /* Build register CONTEXT */
1894 fprintf( outfile
, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86
) );
1896 fprintf( outfile
, "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags
) );
1898 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax
) );
1899 fprintf( outfile
, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx
) );
1900 fprintf( outfile
, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi
) );
1901 fprintf( outfile
, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi
) );
1903 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp
) );
1904 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp
) );
1905 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx
) );
1906 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx
) );
1907 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx
) );
1908 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx
) );
1910 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds
) );
1911 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs
) );
1912 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es
) );
1913 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs
) );
1914 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs
) );
1915 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs
) );
1916 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs
) );
1917 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs
) );
1919 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs
) );
1920 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs
) );
1921 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip
) );
1922 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip
) );
1924 fprintf( outfile
, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET
+2 );
1925 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs
) );
1926 fprintf( outfile
, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET
);
1927 fprintf( outfile
, "\taddl $%d, %%eax\n", STACK16OFFSET(ip
) );
1928 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp
) );
1930 fprintf( outfile
, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave
) );
1933 /* Push address of CONTEXT86 structure -- popped by the relay routine */
1934 fprintf( outfile
, "\tpushl %%esp\n" );
1938 /* Print debug info before call */
1943 fprintf( outfile
, "\tpushl %%ebx\n" );
1945 /* Get Global Offset Table into %ebx (for PLT call) */
1946 fprintf( outfile
, "\tcall .LCallFrom16%s.getgot2\n", name
);
1947 fprintf( outfile
, ".LCallFrom16%s.getgot2:\n", name
);
1948 fprintf( outfile
, "\tpopl %%ebx\n" );
1949 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot2], %%ebx\n", name
);
1952 fprintf( outfile
, "\tpushl %%edx\n" );
1954 fprintf( outfile
, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
1955 sizeof(CONTEXT
) + STRUCTOFFSET(STACK32FRAME
, ebp
) );
1957 fprintf( outfile
, "\tpushl $0\n" );
1960 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16@PLT\n ");
1962 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16\n ");
1964 fprintf( outfile
, "\tpopl %%edx\n" );
1965 fprintf( outfile
, "\tpopl %%edx\n" );
1968 fprintf( outfile
, "\tpopl %%ebx\n" );
1971 /* Call relay routine (which will call the API entry point) */
1972 fprintf( outfile
, "\tleal %d(%%edx), %%eax\n", sizeof(STACK16FRAME
) );
1973 fprintf( outfile
, "\tpushl %%eax\n" );
1974 fprintf( outfile
, "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point
) );
1975 fprintf( outfile
, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay
) );
1977 /* Print debug info after call */
1982 fprintf( outfile
, "\tpushl %%ebx\n" );
1984 /* Get Global Offset Table into %ebx (for PLT call) */
1985 fprintf( outfile
, "\tcall .LCallFrom16%s.getgot3\n", name
);
1986 fprintf( outfile
, ".LCallFrom16%s.getgot3:\n", name
);
1987 fprintf( outfile
, "\tpopl %%ebx\n" );
1988 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot3], %%ebx\n", name
);
1991 fprintf( outfile
, "\tpushl %%eax\n" );
1993 fprintf( outfile
, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
1994 sizeof(CONTEXT
) + STRUCTOFFSET(STACK32FRAME
, ebp
) );
1996 fprintf( outfile
, "\tpushl $0\n" );
1999 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret@PLT\n ");
2001 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret\n ");
2003 fprintf( outfile
, "\tpopl %%eax\n" );
2004 fprintf( outfile
, "\tpopl %%eax\n" );
2007 fprintf( outfile
, "\tpopl %%ebx\n" );
2013 fprintf( outfile
, "\tmovl %%esp, %%ebx\n" );
2015 /* Switch stack back */
2016 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2017 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
2018 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2020 /* Get return address to CallFrom16 stub */
2021 fprintf( outfile
, "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip
)-4 );
2022 fprintf( outfile
, "\tpopl %%eax\n" );
2023 fprintf( outfile
, "\tpopl %%edx\n" );
2025 /* Restore all registers from CONTEXT */
2026 fprintf( outfile
, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs
) );
2027 fprintf( outfile
, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp
) );
2028 fprintf( outfile
, "\taddl $4, %%esp\n" ); /* room for final return address */
2030 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs
) );
2031 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip
) );
2032 fprintf( outfile
, "\tpushl %%edx\n" );
2033 fprintf( outfile
, "\tpushl %%eax\n" );
2034 fprintf( outfile
, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags
) );
2035 fprintf( outfile
, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs
) );
2037 fprintf( outfile
, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs
) );
2038 fprintf( outfile
, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs
) );
2039 fprintf( outfile
, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs
) );
2041 fprintf( outfile
, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp
) );
2042 fprintf( outfile
, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi
) );
2043 fprintf( outfile
, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi
) );
2044 fprintf( outfile
, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax
) );
2045 fprintf( outfile
, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx
) );
2046 fprintf( outfile
, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx
) );
2047 fprintf( outfile
, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx
) );
2049 fprintf( outfile
, "\tpopl %%ds\n" );
2050 fprintf( outfile
, "\tpopfl\n" );
2051 fprintf( outfile
, "\tlret\n" );
2055 /* Switch stack back */
2056 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2057 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
2058 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2060 /* Restore registers */
2061 fprintf( outfile
, "\tpopl %%edx\n" );
2062 fprintf( outfile
, "\tpopl %%ecx\n" );
2063 fprintf( outfile
, "\tpopl %%ebp\n" );
2064 fprintf( outfile
, "\tpopw %%ds\n" );
2065 fprintf( outfile
, "\tpopw %%es\n" );
2066 fprintf( outfile
, "\tpopw %%fs\n" );
2067 fprintf( outfile
, "\tpopw %%gs\n" );
2069 /* Prepare return value and set flags accordingly */
2071 fprintf( outfile
, "\tshldl $16, %%eax, %%edx\n" );
2072 fprintf( outfile
, "\torl %%eax, %%eax\n" );
2074 /* Return to return stub which will return to caller */
2075 fprintf( outfile
, "\tlret $12\n" );
2080 /*******************************************************************
2083 * This routine builds the core routines used in 32->16 thunks:
2085 * extern void CALLBACK CallTo16Word( SEGPTR target, int nb_args );
2086 * extern void CALLBACK CallTo16Long( SEGPTR target, int nb_args );
2087 * extern void CALLBACK CallTo16RegisterShort( const CONTEXT86 *context, int nb_args );
2088 * extern void CALLBACK CallTo16RegisterLong ( const CONTEXT86 *context, int nb_args );
2090 * These routines can be called directly from 32-bit code.
2092 * All routines expect that the 16-bit stack contents (arguments) were
2093 * already set up by the caller; nb_args must contain the number of bytes
2094 * to be conserved. The 16-bit SS:SP will be set accordinly.
2096 * All other registers are either taken from the CONTEXT86 structure
2097 * or else set to default values. The target routine address is either
2098 * given directly or taken from the CONTEXT86.
2100 * If you want to call a 16-bit routine taking only standard argument types
2101 * (WORD and LONG), you can also have an appropriate argument conversion
2102 * stub automatically generated (see BuildCallTo16); you'd then call this
2103 * stub, which in turn would prepare the 16-bit stack and call the appropiate
2108 static void BuildCallTo16Core( FILE *outfile
, int short_ret
, int reg_func
)
2110 char *name
= reg_func
== 2 ? "RegisterLong" :
2111 reg_func
== 1 ? "RegisterShort" :
2112 short_ret
? "Word" : "Long";
2114 /* Function header */
2115 fprintf( outfile
, "\n\t.align 4\n" );
2117 fprintf( outfile
, ".stabs \"CallTo16%s:F1\",36,0,0," PREFIX
"CallTo16%s\n",
2120 fprintf( outfile
, "\t.globl " PREFIX
"CallTo16%s\n", name
);
2121 fprintf( outfile
, PREFIX
"CallTo16%s:\n", name
);
2123 /* Function entry sequence */
2124 fprintf( outfile
, "\tpushl %%ebp\n" );
2125 fprintf( outfile
, "\tmovl %%esp, %%ebp\n" );
2127 /* Save the 32-bit registers */
2128 fprintf( outfile
, "\tpushl %%ebx\n" );
2129 fprintf( outfile
, "\tpushl %%ecx\n" );
2130 fprintf( outfile
, "\tpushl %%edx\n" );
2131 fprintf( outfile
, "\tpushl %%esi\n" );
2132 fprintf( outfile
, "\tpushl %%edi\n" );
2136 /* Get Global Offset Table into %ebx */
2137 fprintf( outfile
, "\tcall .LCallTo16%s.getgot1\n", name
);
2138 fprintf( outfile
, ".LCallTo16%s.getgot1:\n", name
);
2139 fprintf( outfile
, "\tpopl %%ebx\n" );
2140 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot1], %%ebx\n", name
);
2143 /* Enter Win16 Mutex */
2145 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_EnterWin16Lock@PLT\n" );
2147 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_EnterWin16Lock\n" );
2149 /* Print debugging info */
2152 /* Push flags, number of arguments, and target */
2153 fprintf( outfile
, "\tpushl $%d\n", reg_func
);
2154 fprintf( outfile
, "\tpushl 12(%%ebp)\n" );
2155 fprintf( outfile
, "\tpushl 8(%%ebp)\n" );
2158 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16@PLT\n" );
2160 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16\n" );
2162 fprintf( outfile
, "\taddl $12, %%esp\n" );
2165 /* Get return address */
2167 fprintf( outfile
, "\tmovl " PREFIX
"CallTo16_RetAddr@GOTOFF(%%ebx), %%ecx\n" );
2169 fprintf( outfile
, "\tmovl " PREFIX
"CallTo16_RetAddr, %%ecx\n" );
2171 /* Call the actual CallTo16 routine (simulate a lcall) */
2172 fprintf( outfile
, "\tpushl %%cs\n" );
2173 fprintf( outfile
, "\tcall .LCallTo16%s\n", name
);
2175 /* Convert and push return value */
2178 fprintf( outfile
, "\tmovzwl %%ax, %%eax\n" );
2179 fprintf( outfile
, "\tpushl %%eax\n" );
2181 else if ( reg_func
!= 2 )
2183 fprintf( outfile
, "\tshll $16,%%edx\n" );
2184 fprintf( outfile
, "\tmovw %%ax,%%dx\n" );
2185 fprintf( outfile
, "\tpushl %%edx\n" );
2188 fprintf( outfile
, "\tpushl %%eax\n" );
2192 /* Get Global Offset Table into %ebx (might have been overwritten) */
2193 fprintf( outfile
, "\tcall .LCallTo16%s.getgot2\n", name
);
2194 fprintf( outfile
, ".LCallTo16%s.getgot2:\n", name
);
2195 fprintf( outfile
, "\tpopl %%ebx\n" );
2196 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot2], %%ebx\n", name
);
2199 /* Print debugging info */
2203 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16Ret@PLT\n" );
2205 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16Ret\n" );
2208 /* Leave Win16 Mutex */
2210 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_LeaveWin16Lock@PLT\n" );
2212 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_LeaveWin16Lock\n" );
2214 /* Get return value */
2215 fprintf( outfile
, "\tpopl %%eax\n" );
2217 /* Restore the 32-bit registers */
2218 fprintf( outfile
, "\tpopl %%edi\n" );
2219 fprintf( outfile
, "\tpopl %%esi\n" );
2220 fprintf( outfile
, "\tpopl %%edx\n" );
2221 fprintf( outfile
, "\tpopl %%ecx\n" );
2222 fprintf( outfile
, "\tpopl %%ebx\n" );
2224 /* Function exit sequence */
2225 fprintf( outfile
, "\tpopl %%ebp\n" );
2226 fprintf( outfile
, "\tret $8\n" );
2229 /* Start of the actual CallTo16 routine */
2231 fprintf( outfile
, ".LCallTo16%s:\n", name
);
2233 /* Complete STACK32FRAME */
2234 fprintf( outfile
, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET
);
2235 fprintf( outfile
, "\tmovl %%esp,%%edx\n" );
2237 /* Switch to the 16-bit stack */
2239 fprintf( outfile
,"\tdata16\n");
2241 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2);
2242 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET
);
2243 fprintf( outfile
, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET
);
2245 /* Make %bp point to the previous stackframe (built by CallFrom16) */
2246 fprintf( outfile
, "\tmovzwl %%sp,%%ebp\n" );
2247 fprintf( outfile
, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp
) );
2249 /* Add the specified offset to the new sp */
2250 fprintf( outfile
, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args
) );
2252 /* Push the return address
2253 * With sreg suffix, we push 16:16 address (normal lret)
2254 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
2257 fprintf( outfile
, "\tpushl %%ecx\n" );
2260 fprintf( outfile
, "\tshldl $16, %%ecx, %%eax\n" );
2261 fprintf( outfile
, "\tpushw $0\n" );
2262 fprintf( outfile
, "\tpushw %%ax\n" );
2263 fprintf( outfile
, "\tpushw $0\n" );
2264 fprintf( outfile
, "\tpushw %%cx\n" );
2269 /* Push the called routine address */
2270 fprintf( outfile
, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target
) );
2271 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs
) );
2272 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip
) );
2274 /* Get the registers */
2275 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs
) );
2276 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs
) );
2277 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2278 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs
) );
2279 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2280 fprintf( outfile
, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp
) );
2281 fprintf( outfile
, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi
) );
2282 fprintf( outfile
, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi
) );
2283 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax
) );
2284 fprintf( outfile
, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx
) );
2285 fprintf( outfile
, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx
) );
2286 fprintf( outfile
, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx
) );
2288 /* Get the 16-bit ds */
2289 fprintf( outfile
, "\tpopw %%ds\n" );
2291 else /* not a register function */
2293 /* Push the called routine address */
2294 fprintf( outfile
, "\tpushl %d(%%edx)\n", STACK32OFFSET(target
) );
2296 /* Set %fs to the value saved by the last CallFrom16 */
2297 fprintf( outfile
, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs
)-STACK16OFFSET(bp
) );
2298 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2300 /* Set %ds and %es (and %ax just in case) equal to %ss */
2301 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
2302 fprintf( outfile
, "\tmovw %%ax,%%ds\n" );
2303 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2306 /* Jump to the called routine */
2307 fprintf( outfile
, "\t.byte 0x66\n" );
2308 fprintf( outfile
, "\tlret\n" );
2311 /*******************************************************************
2314 * Build the return code for 16-bit callbacks
2316 static void BuildRet16Func( FILE *outfile
)
2319 * Note: This must reside in the .data section to allow
2320 * run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
2323 fprintf( outfile
, "\n\t.globl " PREFIX
"CallTo16_Ret\n" );
2324 fprintf( outfile
, PREFIX
"CallTo16_Ret:\n" );
2326 /* Restore 32-bit segment registers */
2328 fprintf( outfile
, "\tmovw $0x%04x,%%bx\n", Data_Selector
);
2330 fprintf( outfile
, "\tdata16\n");
2332 fprintf( outfile
, "\tmovw %%bx,%%ds\n" );
2334 fprintf( outfile
, "\tdata16\n");
2336 fprintf( outfile
, "\tmovw %%bx,%%es\n" );
2338 fprintf( outfile
, "\tmovw " PREFIX
"SYSLEVEL_Win16CurrentTeb,%%fs\n" );
2340 /* Restore the 32-bit stack */
2343 fprintf( outfile
, "\tdata16\n");
2345 fprintf( outfile
, "\tmovw %%bx,%%ss\n" );
2346 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET
);
2347 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2349 /* Return to caller */
2351 fprintf( outfile
, "\tlret\n" );
2353 /* Declare the return address variable */
2355 fprintf( outfile
, "\n\t.globl " PREFIX
"CallTo16_RetAddr\n" );
2356 fprintf( outfile
, PREFIX
"CallTo16_RetAddr:\t.long 0\n" );
2359 /*******************************************************************
2360 * BuildCallTo32CBClient
2362 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
2364 * Since the relay stub is itself 32-bit, this should not be a problem;
2365 * unfortunately, the relay stubs are expected to switch back to a
2366 * 16-bit stack (and 16-bit code) after completion :-(
2368 * This would conflict with our 16- vs. 32-bit stack handling, so
2369 * we simply switch *back* to our 32-bit stack before returning to
2372 * The CBClient relay stub expects to be called with the following
2373 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
2374 * stack at the designated places:
2377 * (ebp+14) original arguments to the callback routine
2378 * (ebp+10) far return address to original caller
2379 * (ebp+6) Thunklet target address
2380 * (ebp+2) Thunklet relay ID code
2381 * (ebp) BP (saved by CBClientGlueSL)
2382 * (ebp-2) SI (saved by CBClientGlueSL)
2383 * (ebp-4) DI (saved by CBClientGlueSL)
2384 * (ebp-6) DS (saved by CBClientGlueSL)
2386 * ... buffer space used by the 16-bit side glue for temp copies
2388 * (ebx+4) far return address to 16-bit side glue code
2389 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
2391 * The 32-bit side glue code accesses both the original arguments (via ebp)
2392 * and the temporary copies prepared by the 16-bit side glue (via ebx).
2393 * After completion, the stub will load ss:sp from the buffer at ebx
2394 * and perform a far return to 16-bit code.
2396 * To trick the relay stub into returning to us, we replace the 16-bit
2397 * return address to the glue code by a cs:ip pair pointing to our
2398 * return entry point (the original return address is saved first).
2399 * Our return stub thus called will then reload the 32-bit ss:esp and
2400 * return to 32-bit code (by using and ss:esp value that we have also
2401 * pushed onto the 16-bit stack before and a cs:eip values found at
2402 * that position on the 32-bit stack). The ss:esp to be restored is
2403 * found relative to the 16-bit stack pointer at:
2406 * (ebx-8) sp (32-bit stack pointer)
2408 * The second variant of this routine, CALL32_CBClientEx, which is used
2409 * to implement KERNEL.621, has to cope with yet another problem: Here,
2410 * the 32-bit side directly returns to the caller of the CBClient thunklet,
2411 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
2412 * As we have to return to our 32-bit code first, we have to adapt the
2413 * layout of our temporary area so as to include values for the registers
2414 * that are to be restored, and later (in the implementation of KERNEL.621)
2415 * we *really* restore them. The return stub restores DS, DI, SI, and BP
2416 * from the stack, skips the next 8 bytes (CBClient relay code / target),
2417 * and then performs a lret NN, where NN is the number of arguments to be
2418 * removed. Thus, we prepare our temporary area as follows:
2420 * (ebx+22) 16-bit cs (this segment)
2421 * (ebx+20) 16-bit ip ('16-bit' return entry point)
2422 * (ebx+16) 32-bit ss (flat)
2423 * (ebx+12) 32-bit sp (32-bit stack pointer)
2424 * (ebx+10) 16-bit bp (points to ebx+24)
2425 * (ebx+8) 16-bit si (ignored)
2426 * (ebx+6) 16-bit di (ignored)
2427 * (ebx+4) 16-bit ds (we actually use the flat DS here)
2428 * (ebx+2) 16-bit ss (16-bit stack segment)
2429 * (ebx+0) 16-bit sp (points to ebx+4)
2431 * Note that we ensure that DS is not changed and remains the flat segment,
2432 * and the 32-bit stack pointer our own return stub needs fits just
2433 * perfectly into the 8 bytes that are skipped by the Windows stub.
2434 * One problem is that we have to determine the number of removed arguments,
2435 * as these have to be really removed in KERNEL.621. Thus, the BP value
2436 * that we place in the temporary area to be restored, contains the value
2437 * that SP would have if no arguments were removed. By comparing the actual
2438 * value of SP with this value in our return stub we can compute the number
2439 * of removed arguments. This is then returned to KERNEL.621.
2441 * The stack layout of this function:
2442 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
2443 * (ebp+16) esi pointer to caller's esi value
2444 * (ebp+12) arg ebp value to be set for relay stub
2445 * (ebp+8) func CBClient relay stub address
2449 static void BuildCallTo32CBClient( FILE *outfile
, BOOL isEx
)
2451 char *name
= isEx
? "CBClientEx" : "CBClient";
2452 int size
= isEx
? 24 : 12;
2454 /* Function header */
2456 fprintf( outfile
, "\n\t.align 4\n" );
2458 fprintf( outfile
, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX
"CALL32_%s\n",
2461 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_%s\n", name
);
2462 fprintf( outfile
, PREFIX
"CALL32_%s:\n", name
);
2466 fprintf( outfile
, "\tpushl %%ebp\n" );
2467 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2468 fprintf( outfile
, "\tpushl %%edi\n" );
2469 fprintf( outfile
, "\tpushl %%esi\n" );
2470 fprintf( outfile
, "\tpushl %%ebx\n" );
2472 /* Get the 16-bit stack */
2474 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET
);
2476 /* Convert it to a flat address */
2478 fprintf( outfile
, "\tshldl $16,%%ebx,%%eax\n" );
2479 fprintf( outfile
, "\tandl $0xfff8,%%eax\n" );
2480 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy(%%eax),%%esi\n" );
2481 fprintf( outfile
, "\tmovw %%bx,%%ax\n" );
2482 fprintf( outfile
, "\taddl %%eax,%%esi\n" );
2484 /* Allocate temporary area (simulate STACK16_PUSH) */
2486 fprintf( outfile
, "\tpushf\n" );
2487 fprintf( outfile
, "\tcld\n" );
2488 fprintf( outfile
, "\tleal -%d(%%esi), %%edi\n", size
);
2489 fprintf( outfile
, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME
) );
2490 fprintf( outfile
, "\trep\n\tmovsb\n" );
2491 fprintf( outfile
, "\tpopf\n" );
2493 fprintf( outfile
, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size
, STACKOFFSET
);
2495 fprintf( outfile
, "\tpushl %%edi\n" ); /* remember address */
2497 /* Set up temporary area */
2501 fprintf( outfile
, "\tleal 4(%%edi), %%edi\n" );
2503 fprintf( outfile
, "\tleal -8(%%esp), %%eax\n" );
2504 fprintf( outfile
, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */
2506 fprintf( outfile
, "\tmovl %%ss, %%ax\n" );
2507 fprintf( outfile
, "\tandl $0x0000ffff, %%eax\n" );
2508 fprintf( outfile
, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */
2510 fprintf( outfile
, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME
)-size
+4 + 4 );
2511 fprintf( outfile
, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */
2513 fprintf( outfile
, "\tmovl " PREFIX
"CALL32_%s_RetAddr, %%eax\n", name
);
2514 fprintf( outfile
, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */
2518 fprintf( outfile
, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME
)-size
+4 );
2519 fprintf( outfile
, "\tmovl %%ebx, 0(%%edi)\n" );
2521 fprintf( outfile
, "\tmovl %%ds, %%ax\n" );
2522 fprintf( outfile
, "\tmovw %%ax, 4(%%edi)\n" );
2524 fprintf( outfile
, "\taddl $20, %%ebx\n" );
2525 fprintf( outfile
, "\tmovw %%bx, 10(%%edi)\n" );
2527 fprintf( outfile
, "\tleal -8(%%esp), %%eax\n" );
2528 fprintf( outfile
, "\tmovl %%eax, 12(%%edi)\n" );
2530 fprintf( outfile
, "\tmovl %%ss, %%ax\n" );
2531 fprintf( outfile
, "\tandl $0x0000ffff, %%eax\n" );
2532 fprintf( outfile
, "\tmovl %%eax, 16(%%edi)\n" );
2534 fprintf( outfile
, "\tmovl " PREFIX
"CALL32_%s_RetAddr, %%eax\n", name
);
2535 fprintf( outfile
, "\tmovl %%eax, 20(%%edi)\n" );
2538 /* Set up registers and call CBClient relay stub (simulating a far call) */
2540 fprintf( outfile
, "\tmovl 16(%%ebp), %%esi\n" );
2541 fprintf( outfile
, "\tmovl (%%esi), %%esi\n" );
2543 fprintf( outfile
, "\tmovl %%edi, %%ebx\n" );
2544 fprintf( outfile
, "\tmovl 8(%%ebp), %%eax\n" );
2545 fprintf( outfile
, "\tmovl 12(%%ebp), %%ebp\n" );
2547 fprintf( outfile
, "\tpushl %%cs\n" );
2548 fprintf( outfile
, "\tcall *%%eax\n" );
2550 /* Return new esi value to caller */
2552 fprintf( outfile
, "\tmovl 32(%%esp), %%edi\n" );
2553 fprintf( outfile
, "\tmovl %%esi, (%%edi)\n" );
2555 /* Cleanup temporary area (simulate STACK16_POP) */
2557 fprintf( outfile
, "\tpop %%esi\n" );
2559 fprintf( outfile
, "\tpushf\n" );
2560 fprintf( outfile
, "\tstd\n" );
2561 fprintf( outfile
, "\tdec %%esi\n" );
2562 fprintf( outfile
, "\tleal %d(%%esi), %%edi\n", size
);
2563 fprintf( outfile
, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME
) );
2564 fprintf( outfile
, "\trep\n\tmovsb\n" );
2565 fprintf( outfile
, "\tpopf\n" );
2567 fprintf( outfile
, "\t.byte 0x64\n\taddw $%d,(%d)\n", size
, STACKOFFSET
);
2569 /* Return argument size to caller */
2572 fprintf( outfile
, "\tmovl 32(%%esp), %%ebx\n" );
2573 fprintf( outfile
, "\tmovl %%ebp, (%%ebx)\n" );
2576 /* Restore registers and return */
2578 fprintf( outfile
, "\tpopl %%ebx\n" );
2579 fprintf( outfile
, "\tpopl %%esi\n" );
2580 fprintf( outfile
, "\tpopl %%edi\n" );
2581 fprintf( outfile
, "\tpopl %%ebp\n" );
2582 fprintf( outfile
, "\tret\n" );
2585 static void BuildCallTo32CBClientRet( FILE *outfile
, BOOL isEx
)
2587 char *name
= isEx
? "CBClientEx" : "CBClient";
2589 /* '16-bit' return stub */
2591 fprintf( outfile
, "\n\t.globl " PREFIX
"CALL32_%s_Ret\n", name
);
2592 fprintf( outfile
, PREFIX
"CALL32_%s_Ret:\n", name
);
2596 fprintf( outfile
, "\tmovzwl %%sp, %%ebx\n" );
2597 fprintf( outfile
, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
2601 fprintf( outfile
, "\tmovzwl %%bp, %%ebx\n" );
2602 fprintf( outfile
, "\tsubw %%bp, %%sp\n" );
2603 fprintf( outfile
, "\tmovzwl %%sp, %%ebp\n" );
2604 fprintf( outfile
, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
2606 fprintf( outfile
, "\tlret\n" );
2608 /* Declare the return address variable */
2610 fprintf( outfile
, "\n\t.globl " PREFIX
"CALL32_%s_RetAddr\n", name
);
2611 fprintf( outfile
, PREFIX
"CALL32_%s_RetAddr:\t.long 0\n", name
);
2615 /*******************************************************************
2616 * BuildCallTo32LargeStack
2618 * Build the function used to switch to the original 32-bit stack
2619 * before calling a 32-bit function from 32-bit code. This is used for
2620 * functions that need a large stack, like X bitmaps functions.
2622 * The generated function has the following prototype:
2623 * int xxx( int (*func)(), void *arg );
2625 * The pointer to the function can be retrieved by calling CALL32_Init,
2626 * which also takes care of saving the current 32-bit stack pointer.
2627 * Furthermore, CALL32_Init switches to a new stack and jumps to the
2628 * specified target address.
2630 * NOTE: The CALL32_LargeStack routine may be recursively entered by the
2631 * same thread, but not concurrently entered by several threads.
2633 * Stack layout of CALL32_Init:
2635 * (esp+12) new stack address
2636 * (esp+8) target address
2637 * (esp+4) pointer to variable to receive CALL32_LargeStack address
2640 * Stack layout of CALL32_LargeStack:
2647 static void BuildCallTo32LargeStack( FILE *outfile
)
2649 /* Initialization function */
2651 fprintf( outfile
, "\n\t.align 4\n" );
2653 fprintf( outfile
, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX
"CALL32_Init\n");
2655 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Init\n" );
2656 fprintf( outfile
, "\t.type " PREFIX
"CALL32_Init,@function\n" );
2657 fprintf( outfile
, PREFIX
"CALL32_Init:\n" );
2658 fprintf( outfile
, "\tmovl %%esp,CALL32_Original32_esp\n" );
2659 fprintf( outfile
, "\tpopl %%eax\n" );
2660 fprintf( outfile
, "\tpopl %%eax\n" );
2661 fprintf( outfile
, "\tmovl $CALL32_LargeStack,(%%eax)\n" );
2662 fprintf( outfile
, "\tpopl %%eax\n" );
2663 fprintf( outfile
, "\tpopl %%esp\n" );
2664 fprintf( outfile
, "\tpushl %%eax\n" );
2665 fprintf( outfile
, "\tret\n" );
2667 /* Function header */
2669 fprintf( outfile
, "\n\t.align 4\n" );
2671 fprintf( outfile
, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2673 fprintf( outfile
, "CALL32_LargeStack:\n" );
2677 fprintf( outfile
, "\tpushl %%ebp\n" );
2678 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2680 /* Switch to the original 32-bit stack pointer */
2682 fprintf( outfile
, "\tcmpl $0, CALL32_RecursionCount\n" );
2683 fprintf( outfile
, "\tjne CALL32_skip\n" );
2684 fprintf( outfile
, "\tmovl CALL32_Original32_esp, %%esp\n" );
2685 fprintf( outfile
, "CALL32_skip:\n" );
2687 fprintf( outfile
, "\tincl CALL32_RecursionCount\n" );
2689 /* Transfer the argument and call the function */
2691 fprintf( outfile
, "\tpushl 12(%%ebp)\n" );
2692 fprintf( outfile
, "\tcall *8(%%ebp)\n" );
2694 /* Restore registers and return */
2696 fprintf( outfile
, "\tdecl CALL32_RecursionCount\n" );
2698 fprintf( outfile
, "\tmovl %%ebp,%%esp\n" );
2699 fprintf( outfile
, "\tpopl %%ebp\n" );
2700 fprintf( outfile
, "\tret\n" );
2704 fprintf( outfile
, "\t.data\n" );
2705 fprintf( outfile
, "CALL32_Original32_esp:\t.long 0\n" );
2706 fprintf( outfile
, "CALL32_RecursionCount:\t.long 0\n" );
2707 fprintf( outfile
, "\t.text\n" );
2711 /*******************************************************************
2712 * BuildCallFrom32Regs
2714 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2715 * 'args' is the number of dword arguments.
2719 * (ebp+12) first arg
2720 * (ebp+8) ret addr to user code
2721 * (ebp+4) ret addr to relay code
2723 * (ebp-128) buffer area to allow stack frame manipulation
2724 * (ebp-332) CONTEXT86 struct
2725 * (ebp-336) CONTEXT86 *argument
2726 * .... other arguments copied from (ebp+12)
2728 * The entry point routine is called with a CONTEXT* extra argument,
2729 * following the normal args. In this context structure, EIP_reg
2730 * contains the return address to user code, and ESP_reg the stack
2731 * pointer on return (with the return address and arguments already
2734 static void BuildCallFrom32Regs( FILE *outfile
)
2736 static const int STACK_SPACE
= 128 + sizeof(CONTEXT86
);
2738 /* Function header */
2740 fprintf( outfile
, "\n\t.align 4\n" );
2742 fprintf( outfile
, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX
"CALL32_Regs\n" );
2744 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Regs\n" );
2745 fprintf( outfile
, PREFIX
"CALL32_Regs:\n" );
2747 /* Allocate some buffer space on the stack */
2749 fprintf( outfile
, "\tpushl %%ebp\n" );
2750 fprintf( outfile
, "\tmovl %%esp,%%ebp\n ");
2751 fprintf( outfile
, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE
);
2753 /* Build the context structure */
2755 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax
) - STACK_SPACE
);
2756 fprintf( outfile
, "\tpushfl\n" );
2757 fprintf( outfile
, "\tpopl %%eax\n" );
2758 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags
) - STACK_SPACE
);
2759 fprintf( outfile
, "\tmovl 0(%%ebp),%%eax\n" );
2760 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp
) - STACK_SPACE
);
2761 fprintf( outfile
, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx
) - STACK_SPACE
);
2762 fprintf( outfile
, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx
) - STACK_SPACE
);
2763 fprintf( outfile
, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx
) - STACK_SPACE
);
2764 fprintf( outfile
, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi
) - STACK_SPACE
);
2765 fprintf( outfile
, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi
) - STACK_SPACE
);
2767 fprintf( outfile
, "\txorl %%eax,%%eax\n" );
2768 fprintf( outfile
, "\tmovw %%cs,%%ax\n" );
2769 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs
) - STACK_SPACE
);
2770 fprintf( outfile
, "\tmovw %%es,%%ax\n" );
2771 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs
) - STACK_SPACE
);
2772 fprintf( outfile
, "\tmovw %%fs,%%ax\n" );
2773 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs
) - STACK_SPACE
);
2774 fprintf( outfile
, "\tmovw %%gs,%%ax\n" );
2775 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs
) - STACK_SPACE
);
2776 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
2777 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs
) - STACK_SPACE
);
2778 fprintf( outfile
, "\tmovw %%ds,%%ax\n" );
2779 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs
) - STACK_SPACE
);
2780 fprintf( outfile
, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
2782 fprintf( outfile
, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL
);
2783 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags
) - STACK_SPACE
);
2785 fprintf( outfile
, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
2786 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip
) - STACK_SPACE
);
2788 /* Transfer the arguments */
2790 fprintf( outfile
, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
2791 fprintf( outfile
, "\tpushl %%esp\n" ); /* push ptr to context struct */
2792 fprintf( outfile
, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
2793 fprintf( outfile
, "\tjecxz 1f\n" );
2794 fprintf( outfile
, "\tsubl %%ecx,%%esp\n" );
2795 fprintf( outfile
, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */
2796 fprintf( outfile
, "\tmovl %%esp,%%edi\n" );
2797 fprintf( outfile
, "\tshrl $2,%%ecx\n" );
2798 fprintf( outfile
, "\tcld\n" );
2799 fprintf( outfile
, "\trep\n\tmovsl\n" ); /* copy args */
2801 fprintf( outfile
, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
2802 fprintf( outfile
, "\tleal 12(%%ebp,%%eax),%%eax\n" );
2803 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2805 /* Call the entry point */
2807 fprintf( outfile
, "\tcall *0(%%ebx)\n" );
2809 /* Store %eip and %ebp onto the new stack */
2811 fprintf( outfile
, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2812 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip
) - STACK_SPACE
);
2813 fprintf( outfile
, "\tmovl %%eax,-4(%%edx)\n" );
2814 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp
) - STACK_SPACE
);
2815 fprintf( outfile
, "\tmovl %%eax,-8(%%edx)\n" );
2817 /* Restore the context structure */
2819 /* Note: we don't bother to restore %cs, %ds and %ss
2820 * changing them in 32-bit code is a recipe for disaster anyway
2822 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs
) - STACK_SPACE
);
2823 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2824 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs
) - STACK_SPACE
);
2825 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2826 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs
) - STACK_SPACE
);
2827 fprintf( outfile
, "\tmovw %%ax,%%gs\n" );
2829 fprintf( outfile
, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi
) - STACK_SPACE
);
2830 fprintf( outfile
, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi
) - STACK_SPACE
);
2831 fprintf( outfile
, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx
) - STACK_SPACE
);
2832 fprintf( outfile
, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx
) - STACK_SPACE
);
2833 fprintf( outfile
, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx
) - STACK_SPACE
);
2835 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags
) - STACK_SPACE
);
2836 fprintf( outfile
, "\tpushl %%eax\n" );
2837 fprintf( outfile
, "\tpopfl\n" );
2838 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax
) - STACK_SPACE
);
2840 fprintf( outfile
, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2841 fprintf( outfile
, "\tleal -8(%%ebp),%%esp\n" );
2842 fprintf( outfile
, "\tpopl %%ebp\n" );
2843 fprintf( outfile
, "\tret\n" );
2847 /*******************************************************************
2850 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
2852 static void BuildGlue( FILE *outfile
, FILE *infile
)
2858 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
2860 fprintf( outfile
, "#include \"builtin16.h\"\n" );
2861 fprintf( outfile
, "#include \"stackframe.h\"\n\n" );
2863 /* Build the callback glue functions */
2865 while (fgets( buffer
, sizeof(buffer
), infile
))
2867 if (strstr( buffer
, "### start build ###" )) break;
2869 while (fgets( buffer
, sizeof(buffer
), infile
))
2872 if ( (p
= strstr( buffer
, "CallFrom16_" )) != NULL
)
2874 char *q
, *profile
= p
+ strlen( "CallFrom16_" );
2875 for (q
= profile
; (*q
== '_') || isalpha(*q
); q
++ )
2878 for (q
= p
-1; q
> buffer
&& ((*q
== '_') || isalnum(*q
)); q
-- )
2880 if ( ++q
< p
) p
[-1] = '\0'; else q
= "";
2881 BuildCallFrom16Func( outfile
, profile
, q
, FALSE
);
2883 if ( (p
= strstr( buffer
, "CallTo16_" )) != NULL
)
2885 char *q
, *profile
= p
+ strlen( "CallTo16_" );
2886 for (q
= profile
; (*q
== '_') || isalpha(*q
); q
++ )
2889 for (q
= p
-1; q
> buffer
&& ((*q
== '_') || isalnum(*q
)); q
-- )
2891 if ( ++q
< p
) p
[-1] = '\0'; else q
= "";
2892 BuildCallTo16Func( outfile
, profile
, q
);
2894 if (strstr( buffer
, "### stop build ###" )) break;
2900 /*******************************************************************
2903 * Build the 16-bit callbacks
2905 static void BuildCall16( FILE *outfile
)
2909 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2910 fprintf( outfile
, "\t.text\n" );
2915 if (output_file_name
)
2918 getcwd(buffer
, sizeof(buffer
));
2919 fprintf( outfile
, "\t.file\t\"%s\"\n", output_file_name
);
2922 * The stabs help the internal debugger as they are an indication that it
2923 * is sensible to step into a thunk/trampoline.
2925 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2926 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name
);
2927 fprintf( outfile
, "Code_Start:\n\n" );
2930 fprintf( outfile
, PREFIX
"Call16_Start:\n" );
2931 fprintf( outfile
, "\t.globl "PREFIX
"Call16_Start\n" );
2932 fprintf( outfile
, "\t.byte 0\n\n" );
2935 /* Standard CallFrom16 routine (WORD return) */
2936 BuildCallFrom16Core( outfile
, FALSE
, FALSE
, TRUE
);
2938 /* Standard CallFrom16 routine (DWORD return) */
2939 BuildCallFrom16Core( outfile
, FALSE
, FALSE
, FALSE
);
2941 /* Register CallFrom16 routine */
2942 BuildCallFrom16Core( outfile
, TRUE
, FALSE
, FALSE
);
2944 /* C16ThkSL CallFrom16 routine */
2945 BuildCallFrom16Core( outfile
, FALSE
, TRUE
, FALSE
);
2947 /* Standard CallTo16 routine (WORD return) */
2948 BuildCallTo16Core( outfile
, TRUE
, FALSE
);
2950 /* Standard CallTo16 routine (DWORD return) */
2951 BuildCallTo16Core( outfile
, FALSE
, FALSE
);
2953 /* Register CallTo16 routine (16:16 retf) */
2954 BuildCallTo16Core( outfile
, FALSE
, 1 );
2956 /* Register CallTo16 routine (16:32 retf) */
2957 BuildCallTo16Core( outfile
, FALSE
, 2 );
2959 /* CBClientThunkSL routine */
2960 BuildCallTo32CBClient( outfile
, FALSE
);
2962 /* CBClientThunkSLEx routine */
2963 BuildCallTo32CBClient( outfile
, TRUE
);
2965 fprintf( outfile
, PREFIX
"Call16_End:\n" );
2966 fprintf( outfile
, "\t.globl "PREFIX
"Call16_End\n" );
2969 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2970 fprintf( outfile
, ".Letext:\n");
2973 /* The whole Call16_Ret segment must lie within the .data section */
2974 fprintf( outfile
, "\n\t.data\n" );
2975 fprintf( outfile
, "\t.globl " PREFIX
"Call16_Ret_Start\n" );
2976 fprintf( outfile
, PREFIX
"Call16_Ret_Start:\n" );
2978 /* Standard CallTo16 return stub */
2979 BuildRet16Func( outfile
);
2981 /* CBClientThunkSL return stub */
2982 BuildCallTo32CBClientRet( outfile
, FALSE
);
2984 /* CBClientThunkSLEx return stub */
2985 BuildCallTo32CBClientRet( outfile
, TRUE
);
2987 /* End of Call16_Ret segment */
2988 fprintf( outfile
, "\n\t.globl " PREFIX
"Call16_Ret_End\n" );
2989 fprintf( outfile
, PREFIX
"Call16_Ret_End:\n" );
2991 #else /* __i386__ */
2993 fprintf( outfile
, PREFIX
"Call16_Start:\n" );
2994 fprintf( outfile
, "\t.globl "PREFIX
"Call16_Start\n" );
2995 fprintf( outfile
, "\t.byte 0\n\n" );
2996 fprintf( outfile
, PREFIX
"Call16_End:\n" );
2997 fprintf( outfile
, "\t.globl "PREFIX
"Call16_End\n" );
2999 fprintf( outfile
, "\t.globl " PREFIX
"Call16_Ret_Start\n" );
3000 fprintf( outfile
, PREFIX
"Call16_Ret_Start:\n" );
3001 fprintf( outfile
, "\t.byte 0\n\n" );
3002 fprintf( outfile
, "\n\t.globl " PREFIX
"Call16_Ret_End\n" );
3003 fprintf( outfile
, PREFIX
"Call16_Ret_End:\n" );
3005 #endif /* __i386__ */
3008 /*******************************************************************
3011 * Build the 32-bit callbacks
3013 static void BuildCall32( FILE *outfile
)
3017 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
3018 fprintf( outfile
, "\t.text\n" );
3023 if (output_file_name
)
3026 getcwd(buffer
, sizeof(buffer
));
3027 fprintf( outfile
, "\t.file\t\"%s\"\n", output_file_name
);
3030 * The stabs help the internal debugger as they are an indication that it
3031 * is sensible to step into a thunk/trampoline.
3033 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
3034 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name
);
3035 fprintf( outfile
, "Code_Start:\n" );
3039 /* Build the 32-bit large stack callback */
3041 BuildCallTo32LargeStack( outfile
);
3043 /* Build the register callback function */
3045 BuildCallFrom32Regs( outfile
);
3048 fprintf( outfile
, "\t.text\n");
3049 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
3050 fprintf( outfile
, ".Letext:\n");
3053 #else /* __i386__ */
3055 /* Just to avoid an empty file */
3056 fprintf( outfile
, "\t.long 0\n" );
3058 #endif /* __i386__ */
3062 /*******************************************************************
3065 static void usage(void)
3068 "usage: build [-pic] [-o outfile] -spec SPEC_FILE\n"
3069 " build [-pic] [-o outfile] -glue SOURCE_FILE\n"
3070 " build [-pic] [-o outfile] -call16\n"
3071 " build [-pic] [-o outfile] -call32\n" );
3072 if (output_file_name
) unlink( output_file_name
);
3077 /*******************************************************************
3080 static FILE *open_input( const char *name
)
3086 input_file_name
= "<stdin>";
3089 input_file_name
= name
;
3090 if (!(f
= fopen( name
, "r" )))
3092 fprintf( stderr
, "Cannot open input file '%s'\n", name
);
3093 if (output_file_name
) unlink( output_file_name
);
3099 /*******************************************************************
3102 int main(int argc
, char **argv
)
3104 FILE *outfile
= stdout
;
3106 if (argc
< 2) usage();
3108 if (!strcmp( argv
[1], "-pic" ))
3113 if (argc
< 2) usage();
3116 if (!strcmp( argv
[1], "-o" ))
3118 output_file_name
= argv
[2];
3121 if (argc
< 2) usage();
3122 if (!(outfile
= fopen( output_file_name
, "w" )))
3124 fprintf( stderr
, "Unable to create output file '%s'\n", output_file_name
);
3129 /* Retrieve the selector values; this assumes that we are building
3130 * the asm files on the platform that will also run them. Probably
3131 * a safe assumption to make.
3133 GET_CS( Code_Selector
);
3134 GET_DS( Data_Selector
);
3136 if (!strcmp( argv
[1], "-spec" )) BuildSpecFile( outfile
, open_input( argv
[2] ) );
3137 else if (!strcmp( argv
[1], "-glue" )) BuildGlue( outfile
, open_input( argv
[2] ) );
3138 else if (!strcmp( argv
[1], "-call16" )) BuildCall16( outfile
);
3139 else if (!strcmp( argv
[1], "-call32" )) BuildCall32( outfile
);