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
22 #include "selectors.h"
23 #include "stackframe.h"
24 #include "builtin16.h"
27 #ifdef NEED_UNDERSCORE_PREFIX
33 #ifdef HAVE_ASM_STRING
34 # define STRING ".string"
36 # define STRING ".ascii"
39 #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
] =
68 "byte", /* TYPE_BYTE */
69 "word", /* TYPE_WORD */
70 "long", /* TYPE_LONG */
71 "pascal16", /* TYPE_PASCAL_16 */
72 "pascal", /* TYPE_PASCAL */
73 "equate", /* TYPE_ABS */
74 "register", /* TYPE_REGISTER */
75 "interrupt", /* TYPE_INTERRUPT */
76 "stub", /* TYPE_STUB */
77 "stdcall", /* TYPE_STDCALL */
78 "cdecl", /* TYPE_CDECL */
79 "varargs", /* TYPE_VARARGS */
80 "extern", /* TYPE_EXTERN */
81 "forward" /* TYPE_FORWARD */
84 #define MAX_ORDINALS 2048
85 #define MAX_IMPORTS 16
87 /* Callback function used for stub functions */
88 #define STUB_CALLBACK \
89 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
149 static ORDDEF OrdinalDefinitions
[MAX_ORDINALS
];
151 static SPEC_TYPE SpecType
= SPEC_INVALID
;
152 static char DLLName
[80];
153 static char DLLFileName
[80];
154 static int Limit
= 0;
155 static int Base
= MAX_ORDINALS
;
156 static int DLLHeapSize
= 0;
157 static char *SpecName
;
159 static WORD Code_Selector
, Data_Selector
;
160 static char DLLInitFunc
[80];
161 static char *DLLImports
[MAX_IMPORTS
];
162 static int nb_imports
= 0;
164 char *ParseBuffer
= NULL
;
169 static int UsePIC
= 0;
171 static int debugging
= 1;
173 /* Offset of a structure field relative to the start of the struct */
174 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
176 /* Offset of register relative to the start of the CONTEXT struct */
177 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT86,reg)
179 /* Offset of register relative to the start of the STACK16FRAME struct */
180 #define STACK16OFFSET(reg) STRUCTOFFSET(STACK16FRAME,reg)
182 /* Offset of register relative to the start of the STACK32FRAME struct */
183 #define STACK32OFFSET(reg) STRUCTOFFSET(STACK32FRAME,reg)
185 /* Offset of the stack pointer relative to %fs:(0) */
186 #define STACKOFFSET (STRUCTOFFSET(TEB,cur_stack))
188 static void BuildCallFrom16Func( FILE *outfile
, char *profile
, char *prefix
, int local
);
190 static void *xmalloc (size_t size
)
194 res
= malloc (size
? size
: 1);
197 fprintf (stderr
, "Virtual memory exhausted.\n");
204 static void *xrealloc (void *ptr
, size_t size
)
206 void *res
= realloc (ptr
, size
);
209 fprintf (stderr
, "Virtual memory exhausted.\n");
215 static char *xstrdup( const char *str
)
217 char *res
= strdup( str
);
220 fprintf (stderr
, "Virtual memory exhausted.\n");
226 static int IsNumberString(char *s
)
235 static char *strupper(char *s
)
239 for(p
= s
; *p
!= '\0'; p
++)
245 static char * GetTokenInLine(void)
250 if (ParseNext
!= ParseBuffer
)
252 if (ParseSaveChar
== '\0')
254 *ParseNext
= ParseSaveChar
;
258 * Remove initial white space.
260 for (p
= ParseNext
; isspace(*p
); p
++)
263 if ((*p
== '\0') || (*p
== '#'))
270 if (*token
!= '(' && *token
!= ')')
271 while (*p
!= '\0' && *p
!= '(' && *p
!= ')' && !isspace(*p
))
281 static char * GetToken(void)
285 if (ParseBuffer
== NULL
)
287 ParseBuffer
= xmalloc(512);
288 ParseNext
= ParseBuffer
;
292 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
294 if (ParseBuffer
[0] != '#')
299 while ((token
= GetTokenInLine()) == NULL
)
301 ParseNext
= ParseBuffer
;
305 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
307 if (ParseBuffer
[0] != '#')
316 /*******************************************************************
319 * Parse a variable definition.
321 static int ParseVariable( ORDDEF
*odp
)
326 int value_array_size
;
328 char *token
= GetToken();
331 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
332 SpecName
, Line
, token
);
337 value_array_size
= 25;
338 value_array
= xmalloc(sizeof(*value_array
) * value_array_size
);
340 while ((token
= GetToken()) != NULL
)
345 value_array
[n_values
++] = strtol(token
, &endptr
, 0);
346 if (n_values
== value_array_size
)
348 value_array_size
+= 25;
349 value_array
= xrealloc(value_array
,
350 sizeof(*value_array
) * value_array_size
);
353 if (endptr
== NULL
|| *endptr
!= '\0')
355 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
356 SpecName
, Line
, token
);
363 fprintf(stderr
, "%s:%d: End of file in variable declaration\n",
368 odp
->u
.var
.n_values
= n_values
;
369 odp
->u
.var
.values
= xrealloc(value_array
, sizeof(*value_array
) * n_values
);
375 /*******************************************************************
376 * ParseExportFunction
378 * Parse a function definition.
380 static int ParseExportFunction( ORDDEF
*odp
)
388 if (odp
->type
== TYPE_STDCALL
)
390 fprintf( stderr
, "%s:%d: 'stdcall' not supported for Win16\n",
394 else if (odp
->type
== TYPE_VARARGS
)
396 fprintf( stderr
, "%s:%d: 'varargs' not supported for Win16\n",
402 if ((odp
->type
== TYPE_PASCAL
) || (odp
->type
== TYPE_PASCAL_16
))
404 fprintf( stderr
, "%s:%d: 'pascal' not supported for Win32\n",
416 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
417 SpecName
, Line
, token
);
421 for (i
= 0; i
< sizeof(odp
->u
.func
.arg_types
)-1; i
++)
427 if (!strcmp(token
, "word"))
428 odp
->u
.func
.arg_types
[i
] = 'w';
429 else if (!strcmp(token
, "s_word"))
430 odp
->u
.func
.arg_types
[i
] = 's';
431 else if (!strcmp(token
, "long") || !strcmp(token
, "segptr"))
432 odp
->u
.func
.arg_types
[i
] = 'l';
433 else if (!strcmp(token
, "ptr"))
434 odp
->u
.func
.arg_types
[i
] = 'p';
435 else if (!strcmp(token
, "str"))
436 odp
->u
.func
.arg_types
[i
] = 't';
437 else if (!strcmp(token
, "wstr"))
438 odp
->u
.func
.arg_types
[i
] = 'W';
439 else if (!strcmp(token
, "segstr"))
440 odp
->u
.func
.arg_types
[i
] = 'T';
441 else if (!strcmp(token
, "double"))
443 odp
->u
.func
.arg_types
[i
++] = 'l';
444 odp
->u
.func
.arg_types
[i
] = 'l';
448 fprintf(stderr
, "%s:%d: Unknown variable type '%s'\n",
449 SpecName
, Line
, token
);
452 if (SpecType
== SPEC_WIN32
)
454 if (strcmp(token
, "long") &&
455 strcmp(token
, "ptr") &&
456 strcmp(token
, "str") &&
457 strcmp(token
, "wstr") &&
458 strcmp(token
, "double"))
460 fprintf( stderr
, "%s:%d: Type '%s' not supported for Win32\n",
461 SpecName
, Line
, token
);
466 if ((*token
!= ')') || (i
>= sizeof(odp
->u
.func
.arg_types
)))
468 fprintf( stderr
, "%s:%d: Too many arguments\n", SpecName
, Line
);
471 odp
->u
.func
.arg_types
[i
] = '\0';
472 if ((odp
->type
== TYPE_STDCALL
) && !i
)
473 odp
->type
= TYPE_CDECL
; /* stdcall is the same as cdecl for 0 args */
474 strcpy(odp
->u
.func
.link_name
, GetToken());
476 /* Ignore Win32 'register' routines on non-Intel archs */
478 if ( odp
->type
== TYPE_REGISTER
&& SpecType
== SPEC_WIN32
)
479 odp
->type
= TYPE_INVALID
;
486 /*******************************************************************
489 * Parse an 'equate' definition.
491 static int ParseEquate( ORDDEF
*odp
)
495 char *token
= GetToken();
496 int value
= strtol(token
, &endptr
, 0);
497 if (endptr
== NULL
|| *endptr
!= '\0')
499 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
500 SpecName
, Line
, token
);
504 if (SpecType
== SPEC_WIN32
)
506 fprintf( stderr
, "%s:%d: 'equate' not supported for Win32\n",
511 odp
->u
.abs
.value
= value
;
516 /*******************************************************************
519 * Parse a 'stub' definition.
521 static int ParseStub( ORDDEF
*odp
)
523 odp
->u
.func
.arg_types
[0] = '\0';
524 strcpy( odp
->u
.func
.link_name
, STUB_CALLBACK
);
529 /*******************************************************************
532 * Parse an 'interrupt' definition.
534 static int ParseInterrupt( ORDDEF
*odp
)
538 if (SpecType
== SPEC_WIN32
)
540 fprintf( stderr
, "%s:%d: 'interrupt' not supported for Win32\n",
548 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
549 SpecName
, Line
, token
);
555 fprintf(stderr
, "%s:%d: Expected ')' got '%s'\n",
556 SpecName
, Line
, token
);
560 odp
->u
.func
.arg_types
[0] = '\0';
561 strcpy( odp
->u
.func
.link_name
, GetToken() );
566 /*******************************************************************
569 * Parse an 'extern' definition.
571 static int ParseExtern( ORDDEF
*odp
)
573 if (SpecType
== SPEC_WIN16
)
575 fprintf( stderr
, "%s:%d: 'extern' not supported for Win16\n",
579 strcpy( odp
->u
.ext
.link_name
, GetToken() );
584 /*******************************************************************
587 * Parse a 'forward' definition.
589 static int ParseForward( ORDDEF
*odp
)
591 if (SpecType
== SPEC_WIN16
)
593 fprintf( stderr
, "%s:%d: 'forward' not supported for Win16\n",
597 strcpy( odp
->u
.fwd
.link_name
, GetToken() );
602 /*******************************************************************
605 * Parse an ordinal definition.
607 static int ParseOrdinal(int ordinal
)
612 if (ordinal
>= MAX_ORDINALS
)
614 fprintf(stderr
, "%s:%d: Ordinal number too large\n", SpecName
, Line
);
617 if (ordinal
> Limit
) Limit
= ordinal
;
618 if (ordinal
< Base
) Base
= ordinal
;
620 odp
= &OrdinalDefinitions
[ordinal
];
621 if (!(token
= GetToken()))
623 fprintf(stderr
, "%s:%d: Expected type after ordinal\n", SpecName
, Line
);
627 for (odp
->type
= 0; odp
->type
< TYPE_NBTYPES
; odp
->type
++)
628 if (TypeNames
[odp
->type
] && !strcmp( token
, TypeNames
[odp
->type
] ))
631 if (odp
->type
>= TYPE_NBTYPES
)
634 "%s:%d: Expected type after ordinal, found '%s' instead\n",
635 SpecName
, Line
, token
);
639 if (!(token
= GetToken()))
641 fprintf( stderr
, "%s:%d: Expected name after type\n", SpecName
, Line
);
644 strcpy( odp
->name
, token
);
652 return ParseVariable( odp
);
659 return ParseExportFunction( odp
);
661 return ParseInterrupt( odp
);
663 return ParseEquate( odp
);
665 return ParseStub( odp
);
667 return ParseExtern( odp
);
669 return ParseForward( odp
);
671 fprintf( stderr
, "Should not happen\n" );
677 /*******************************************************************
682 static int ParseTopLevel(void)
686 while ((token
= GetToken()) != NULL
)
688 if (strcmp(token
, "name") == 0)
690 strcpy(DLLName
, GetToken());
692 if (!DLLFileName
[0]) sprintf( DLLFileName
, "%s.DLL", DLLName
);
694 else if (strcmp(token
, "file") == 0)
696 strcpy(DLLFileName
, GetToken());
697 strupper(DLLFileName
);
699 else if (strcmp(token
, "type") == 0)
702 if (!strcmp(token
, "win16" )) SpecType
= SPEC_WIN16
;
703 else if (!strcmp(token
, "win32" )) SpecType
= SPEC_WIN32
;
706 fprintf(stderr
, "%s:%d: Type must be 'win16' or 'win32'\n",
711 else if (strcmp(token
, "heap") == 0)
714 if (!IsNumberString(token
))
716 fprintf(stderr
, "%s:%d: Expected number after heap\n",
720 DLLHeapSize
= atoi(token
);
722 else if (strcmp(token
, "init") == 0)
724 strcpy(DLLInitFunc
, GetToken());
725 if (SpecType
== SPEC_WIN16
)
727 fprintf(stderr
, "%s:%d: init cannot be used for Win16 spec files\n",
733 fprintf(stderr
, "%s:%d: Expected function name after init\n", SpecName
, Line
);
737 else if (strcmp(token
, "import") == 0)
739 if (nb_imports
>= MAX_IMPORTS
)
741 fprintf( stderr
, "%s:%d: Too many imports (limit %d)\n",
742 SpecName
, Line
, MAX_IMPORTS
);
745 if (SpecType
!= SPEC_WIN32
)
747 fprintf( stderr
, "%s:%d: Imports not supported for Win16\n", SpecName
, Line
);
750 DLLImports
[nb_imports
++] = xstrdup(GetToken());
752 else if (IsNumberString(token
))
757 ordinal
= atoi(token
);
758 if ((rv
= ParseOrdinal(ordinal
)) < 0)
764 "%s:%d: Expected name, id, length or ordinal\n",
774 /*******************************************************************
777 * Store a list of ints into a byte array.
779 static int StoreVariableCode( unsigned char *buffer
, int size
, ORDDEF
*odp
)
786 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
787 buffer
[i
] = odp
->u
.var
.values
[i
];
790 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
791 ((unsigned short *)buffer
)[i
] = odp
->u
.var
.values
[i
];
794 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
795 ((unsigned int *)buffer
)[i
] = odp
->u
.var
.values
[i
];
798 return odp
->u
.var
.n_values
* size
;
802 /*******************************************************************
805 * Dump a byte stream into the assembly code.
807 static void DumpBytes( FILE *outfile
, const unsigned char *data
, int len
,
812 fprintf( outfile
, "\nstatic BYTE %s[] = \n{", label
);
814 for (i
= 0; i
< len
; i
++)
816 if (!(i
& 0x0f)) fprintf( outfile
, "\n " );
817 fprintf( outfile
, "%d", *data
++ );
818 if (i
< len
- 1) fprintf( outfile
, ", " );
820 fprintf( outfile
, "\n};\n" );
824 /*******************************************************************
827 * Build the in-memory representation of a 16-bit NE module, and dump it
828 * as a byte stream into the assembly code.
830 static int BuildModule16( FILE *outfile
, int max_code_offset
,
831 int max_data_offset
)
837 SEGTABLEENTRY
*pSegment
;
841 ET_BUNDLE
*bundle
= 0;
846 * OFSTRUCT File information
847 * SEGTABLEENTRY Segment 1 (code)
848 * SEGTABLEENTRY Segment 2 (data)
849 * WORD[2] Resource table (empty)
850 * BYTE[2] Imported names (empty)
851 * BYTE[n] Resident names table
852 * BYTE[n] Entry table
855 buffer
= xmalloc( 0x10000 );
857 pModule
= (NE_MODULE
*)buffer
;
858 memset( pModule
, 0, sizeof(*pModule
) );
859 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
862 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_BUILTIN
| NE_FFLAGS_LIBMODULE
;
864 pModule
->heap_size
= DLLHeapSize
;
865 pModule
->stack_size
= 0;
870 pModule
->seg_count
= 2;
871 pModule
->modref_count
= 0;
872 pModule
->nrname_size
= 0;
873 pModule
->modref_table
= 0;
874 pModule
->nrname_fpos
= 0;
875 pModule
->moveable_entries
= 0;
876 pModule
->alignment
= 0;
877 pModule
->truetype
= 0;
878 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
879 pModule
->misc_flags
= 0;
880 pModule
->dlls_to_init
= 0;
881 pModule
->nrname_handle
= 0;
882 pModule
->min_swap_area
= 0;
883 pModule
->expected_version
= 0;
884 pModule
->module32
= 0;
886 pModule
->self_loading_sel
= 0;
888 /* File information */
890 pFileInfo
= (OFSTRUCT
*)(pModule
+ 1);
891 pModule
->fileinfo
= (int)pFileInfo
- (int)pModule
;
892 memset( pFileInfo
, 0, sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
) );
893 pFileInfo
->cBytes
= sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
)
894 + strlen(DLLFileName
);
895 strcpy( pFileInfo
->szPathName
, DLLFileName
);
896 pstr
= (char *)pFileInfo
+ pFileInfo
->cBytes
+ 1;
898 #ifdef __i386__ /* FIXME: Alignment problems! */
902 pSegment
= (SEGTABLEENTRY
*)pstr
;
903 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
904 pSegment
->filepos
= 0;
905 pSegment
->size
= max_code_offset
;
907 pSegment
->minsize
= max_code_offset
;
911 pModule
->dgroup_entry
= (int)pSegment
- (int)pModule
;
912 pSegment
->filepos
= 0;
913 pSegment
->size
= max_data_offset
;
914 pSegment
->flags
= NE_SEGFLAGS_DATA
;
915 pSegment
->minsize
= max_data_offset
;
921 pword
= (WORD
*)pSegment
;
922 pModule
->res_table
= (int)pword
- (int)pModule
;
926 /* Imported names table */
928 pstr
= (char *)pword
;
929 pModule
->import_table
= (int)pstr
- (int)pModule
;
933 /* Resident names table */
935 pModule
->name_table
= (int)pstr
- (int)pModule
;
936 /* First entry is module name */
937 *pstr
= strlen(DLLName
);
938 strcpy( pstr
+ 1, DLLName
);
941 pstr
+= sizeof(WORD
);
942 /* Store all ordinals */
943 odp
= OrdinalDefinitions
+ 1;
944 for (i
= 1; i
<= Limit
; i
++, odp
++)
946 if (!odp
->name
[0]) continue;
947 *pstr
= strlen( odp
->name
);
948 strcpy( pstr
+ 1, odp
->name
);
949 strupper( pstr
+ 1 );
952 pstr
+= sizeof(WORD
);
958 pModule
->entry_table
= (int)pstr
- (int)pModule
;
959 odp
= OrdinalDefinitions
+ 1;
960 for (i
= 1; i
<= Limit
; i
++, odp
++)
972 selector
= 1; /* Code selector */
978 selector
= 2; /* Data selector */
982 selector
= 0xfe; /* Constant selector */
986 selector
= 0; /* Invalid selector */
993 if ( bundle
&& bundle
->last
+1 == i
)
998 bundle
->next
= (char *)pstr
- (char *)pModule
;
1000 bundle
= (ET_BUNDLE
*)pstr
;
1001 bundle
->first
= i
-1;
1004 pstr
+= sizeof(ET_BUNDLE
);
1007 /* FIXME: is this really correct ?? */
1008 entry
= (ET_ENTRY
*)pstr
;
1009 entry
->type
= 0xff; /* movable */
1010 entry
->flags
= 3; /* exported & public data */
1011 entry
->segnum
= selector
;
1012 entry
->offs
= odp
->offset
;
1013 pstr
+= sizeof(ET_ENTRY
);
1018 /* Dump the module content */
1020 DumpBytes( outfile
, (char *)pModule
, (int)pstr
- (int)pModule
,
1022 return (int)pstr
- (int)pModule
;
1026 /*******************************************************************
1029 * Build a Win32 C file from a spec file.
1031 static int BuildSpec32File( char * specfile
, FILE *outfile
)
1034 int i
, nb_names
, fwd_size
= 0, have_regs
= FALSE
;
1036 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
1038 fprintf( outfile
, "#include \"builtin32.h\"\n\n" );
1040 /* Output code for all stubs functions */
1042 fprintf( outfile
, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
1044 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1046 if (odp
->type
!= TYPE_STUB
) continue;
1047 fprintf( outfile
, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
1051 /* Output the DLL functions prototypes */
1053 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1058 fprintf( outfile
, "extern void %s();\n", odp
->u
.ext
.link_name
);
1063 fprintf( outfile
, "extern void %s();\n", odp
->u
.func
.link_name
);
1066 fwd_size
+= strlen(odp
->u
.fwd
.link_name
) + 1;
1069 fprintf( outfile
, "extern void __regs_%d();\n", i
);
1076 fprintf(stderr
,"build: function type %d not available for Win32\n",
1082 /* Output LibMain function */
1083 if (DLLInitFunc
[0]) fprintf( outfile
, "extern void %s();\n", DLLInitFunc
);
1086 /* Output code for all register functions */
1090 fprintf( outfile
, "#ifndef __GNUC__\n" );
1091 fprintf( outfile
, "static void __asm__dummy() {\n" );
1092 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
1093 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1095 if (odp
->type
!= TYPE_REGISTER
) continue;
1097 "__asm__(\".align 4\\n\\t\"\n"
1098 " \".type " PREFIX
"__regs_%d,@function\\n\\t\"\n"
1099 " \"" PREFIX
"__regs_%d:\\n\\t\"\n"
1100 " \"call " PREFIX
"CALL32_Regs\\n\\t\"\n"
1101 " \".long " PREFIX
"%s\\n\\t\"\n"
1102 " \".byte %d,%d\");\n",
1103 i
, i
, odp
->u
.func
.link_name
,
1104 4 * strlen(odp
->u
.func
.arg_types
),
1105 4 * strlen(odp
->u
.func
.arg_types
) );
1107 fprintf( outfile
, "#ifndef __GNUC__\n" );
1108 fprintf( outfile
, "}\n" );
1109 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
1112 /* Output the DLL functions table */
1114 fprintf( outfile
, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1116 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1121 fprintf( outfile
, " 0" );
1124 fprintf( outfile
, " %s", odp
->u
.ext
.link_name
);
1129 fprintf( outfile
, " %s", odp
->u
.func
.link_name
);
1132 fprintf( outfile
, " __stub_%d", i
);
1135 fprintf( outfile
, " __regs_%d", i
);
1138 fprintf( outfile
, " (ENTRYPOINT32)\"%s\"", odp
->u
.fwd
.link_name
);
1143 if (i
< Limit
) fprintf( outfile
, ",\n" );
1145 fprintf( outfile
, "\n};\n\n" );
1147 /* Output the DLL names table */
1150 fprintf( outfile
, "static const char * const FuncNames[] =\n{\n" );
1151 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1153 if (odp
->type
== TYPE_INVALID
) continue;
1154 if (nb_names
++) fprintf( outfile
, ",\n" );
1155 fprintf( outfile
, " \"%s\"", odp
->name
);
1157 fprintf( outfile
, "\n};\n\n" );
1159 /* Output the DLL argument types */
1161 fprintf( outfile
, "static const unsigned int ArgTypes[%d] =\n{\n",
1163 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1165 unsigned int j
, mask
= 0;
1166 if ((odp
->type
== TYPE_STDCALL
) || (odp
->type
== TYPE_CDECL
) ||
1167 (odp
->type
== TYPE_REGISTER
))
1168 for (j
= 0; odp
->u
.func
.arg_types
[j
]; j
++)
1170 if (odp
->u
.func
.arg_types
[j
] == 't') mask
|= 1<< (j
*2);
1171 if (odp
->u
.func
.arg_types
[j
] == 'W') mask
|= 2<< (j
*2);
1173 fprintf( outfile
, " %d", mask
);
1174 if (i
< Limit
) fprintf( outfile
, ",\n" );
1176 fprintf( outfile
, "\n};\n\n" );
1178 /* Output the DLL ordinals table */
1180 fprintf( outfile
, "static const unsigned short FuncOrdinals[] =\n{\n" );
1182 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1184 if (odp
->type
== TYPE_INVALID
) continue;
1185 if (nb_names
++) fprintf( outfile
, ",\n" );
1186 fprintf( outfile
, " %d", i
- Base
);
1188 fprintf( outfile
, "\n};\n\n" );
1190 /* Output the DLL functions arguments */
1192 fprintf( outfile
, "static const unsigned char FuncArgs[%d] =\n{\n",
1194 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1200 args
= (unsigned char)strlen(odp
->u
.func
.arg_types
);
1203 args
= 0x80 | (unsigned char)strlen(odp
->u
.func
.arg_types
);
1206 args
= 0x40 | (unsigned char)strlen(odp
->u
.func
.arg_types
);
1215 fprintf( outfile
, " 0x%02x", args
);
1216 if (i
< Limit
) fprintf( outfile
, ",\n" );
1218 fprintf( outfile
, "\n};\n\n" );
1220 /* Output the DLL imports */
1224 fprintf( outfile
, "static const char * const Imports[%d] =\n{\n", nb_imports
);
1225 for (i
= 0; i
< nb_imports
; i
++)
1227 fprintf( outfile
, " \"%s\"", DLLImports
[i
] );
1228 if (i
< nb_imports
-1) fprintf( outfile
, ",\n" );
1230 fprintf( outfile
, "\n};\n\n" );
1233 /* Output the DLL descriptor */
1235 fprintf( outfile
, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1237 fprintf( outfile
, " \"%s\",\n", DLLName
);
1238 fprintf( outfile
, " \"%s\",\n", DLLFileName
);
1239 fprintf( outfile
, " %d,\n", Base
);
1240 fprintf( outfile
, " %d,\n", Limit
- Base
+ 1 );
1241 fprintf( outfile
, " %d,\n", nb_names
);
1242 fprintf( outfile
, " %d,\n", nb_imports
);
1243 fprintf( outfile
, " %d,\n", (fwd_size
+ 3) & ~3 );
1250 fprintf( outfile
, " %s,\n", nb_imports
? "Imports" : "0" );
1251 fprintf( outfile
, " %s\n", DLLInitFunc
[0] ? DLLInitFunc
: "0" );
1252 fprintf( outfile
, "};\n" );
1256 /*******************************************************************
1259 static int Spec16TypeCompare( const void *e1
, const void *e2
)
1261 const ORDDEF
*odp1
= *(const ORDDEF
**)e1
;
1262 const ORDDEF
*odp2
= *(const ORDDEF
**)e2
;
1264 int type1
= (odp1
->type
== TYPE_CDECL
) ? 0
1265 : (odp1
->type
== TYPE_REGISTER
) ? 3
1266 : (odp1
->type
== TYPE_INTERRUPT
) ? 4
1267 : (odp1
->type
== TYPE_PASCAL_16
) ? 1 : 2;
1269 int type2
= (odp2
->type
== TYPE_CDECL
) ? 0
1270 : (odp2
->type
== TYPE_REGISTER
) ? 3
1271 : (odp2
->type
== TYPE_INTERRUPT
) ? 4
1272 : (odp2
->type
== TYPE_PASCAL_16
) ? 1 : 2;
1274 int retval
= type1
- type2
;
1276 retval
= strcmp( odp1
->u
.func
.arg_types
, odp2
->u
.func
.arg_types
);
1281 /*******************************************************************
1284 * Build a Win16 assembly file from a spec file.
1286 static int BuildSpec16File( char * specfile
, FILE *outfile
)
1288 ORDDEF
*odp
, **type
, **typelist
;
1289 int i
, nFuncs
, nTypes
;
1290 int code_offset
, data_offset
, module_size
;
1291 unsigned char *data
;
1295 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
1297 fprintf( outfile
, "#define __FLATCS__ 0x%04x\n", Code_Selector
);
1298 fprintf( outfile
, "#include \"builtin16.h\"\n\n" );
1300 data
= (unsigned char *)xmalloc( 0x10000 );
1301 memset( data
, 0, 16 );
1305 /* Build sorted list of all argument types, without duplicates */
1307 typelist
= (ORDDEF
**)calloc( Limit
+1, sizeof(ORDDEF
*) );
1309 odp
= OrdinalDefinitions
;
1310 for (i
= nFuncs
= 0; i
<= Limit
; i
++, odp
++)
1315 case TYPE_INTERRUPT
:
1318 case TYPE_PASCAL_16
:
1320 typelist
[nFuncs
++] = odp
;
1327 qsort( typelist
, nFuncs
, sizeof(ORDDEF
*), Spec16TypeCompare
);
1330 while ( i
< nFuncs
)
1332 typelist
[nTypes
++] = typelist
[i
++];
1333 while ( i
< nFuncs
&& Spec16TypeCompare( typelist
+ i
, typelist
+ nTypes
-1 ) == 0 )
1337 /* Output CallFrom16 routines needed by this .spec file */
1339 for ( i
= 0; i
< nTypes
; i
++ )
1343 sprintf( profile
, "%s_%s_%s",
1344 (typelist
[i
]->type
== TYPE_CDECL
) ? "c" : "p",
1345 (typelist
[i
]->type
== TYPE_REGISTER
) ? "regs" :
1346 (typelist
[i
]->type
== TYPE_INTERRUPT
) ? "intr" :
1347 (typelist
[i
]->type
== TYPE_PASCAL_16
) ? "word" : "long",
1348 typelist
[i
]->u
.func
.arg_types
);
1350 BuildCallFrom16Func( outfile
, profile
, DLLName
, TRUE
);
1353 /* Output the DLL functions prototypes */
1355 odp
= OrdinalDefinitions
;
1356 for (i
= 0; i
<= Limit
; i
++, odp
++)
1361 case TYPE_INTERRUPT
:
1364 case TYPE_PASCAL_16
:
1365 fprintf( outfile
, "extern void %s();\n", odp
->u
.func
.link_name
);
1372 /* Output code segment */
1374 fprintf( outfile
, "\nstatic struct\n{\n CALLFROM16 call[%d];\n"
1375 " ENTRYPOINT16 entry[%d];\n} Code_Segment = \n{\n {\n",
1379 for ( i
= 0; i
< nTypes
; i
++ )
1381 char profile
[101], *arg
;
1384 sprintf( profile
, "%s_%s_%s",
1385 (typelist
[i
]->type
== TYPE_CDECL
) ? "c" : "p",
1386 (typelist
[i
]->type
== TYPE_REGISTER
) ? "regs" :
1387 (typelist
[i
]->type
== TYPE_INTERRUPT
) ? "intr" :
1388 (typelist
[i
]->type
== TYPE_PASCAL_16
) ? "word" : "long",
1389 typelist
[i
]->u
.func
.arg_types
);
1391 if ( typelist
[i
]->type
!= TYPE_CDECL
)
1392 for ( arg
= typelist
[i
]->u
.func
.arg_types
; *arg
; arg
++ )
1395 case 'w': /* word */
1396 case 's': /* s_word */
1400 case 'l': /* long or segmented pointer */
1401 case 'T': /* segmented pointer to null-terminated string */
1402 case 'p': /* linear pointer */
1403 case 't': /* linear pointer to null-terminated string */
1408 if ( typelist
[i
]->type
== TYPE_INTERRUPT
)
1411 fprintf( outfile
, " CF16_%s( %s_CallFrom16_%s, %d, \"%s\" ),\n",
1412 ( typelist
[i
]->type
== TYPE_REGISTER
1413 || typelist
[i
]->type
== TYPE_INTERRUPT
)? "REGS":
1414 typelist
[i
]->type
== TYPE_PASCAL_16
? "WORD" : "LONG",
1415 DLLName
, profile
, argsize
, profile
);
1417 code_offset
+= sizeof(CALLFROM16
);
1419 fprintf( outfile
, " },\n {\n" );
1421 odp
= OrdinalDefinitions
;
1422 for (i
= 0; i
<= Limit
; i
++, odp
++)
1427 odp
->offset
= 0xffff;
1431 odp
->offset
= LOWORD(odp
->u
.abs
.value
);
1435 odp
->offset
= data_offset
;
1436 data_offset
+= StoreVariableCode( data
+ data_offset
, 1, odp
);
1440 odp
->offset
= data_offset
;
1441 data_offset
+= StoreVariableCode( data
+ data_offset
, 2, odp
);
1445 odp
->offset
= data_offset
;
1446 data_offset
+= StoreVariableCode( data
+ data_offset
, 4, odp
);
1450 case TYPE_INTERRUPT
:
1453 case TYPE_PASCAL_16
:
1455 type
= bsearch( &odp
, typelist
, nTypes
, sizeof(ORDDEF
*), Spec16TypeCompare
);
1458 fprintf( outfile
, " /* %s.%d */ ", DLLName
, i
);
1459 fprintf( outfile
, "EP( %s, %d /* %s_%s_%s */ ),\n",
1460 odp
->u
.func
.link_name
,
1461 (type
-typelist
)*sizeof(CALLFROM16
) -
1462 (code_offset
+ sizeof(ENTRYPOINT16
)),
1463 (odp
->type
== TYPE_CDECL
) ? "c" : "p",
1464 (odp
->type
== TYPE_REGISTER
) ? "regs" :
1465 (odp
->type
== TYPE_INTERRUPT
) ? "intr" :
1466 (odp
->type
== TYPE_PASCAL_16
) ? "word" : "long",
1467 odp
->u
.func
.arg_types
);
1469 odp
->offset
= code_offset
;
1470 code_offset
+= sizeof(ENTRYPOINT16
);
1474 fprintf(stderr
,"build: function type %d not available for Win16\n",
1480 fprintf( outfile
, " }\n};\n" );
1482 /* Output data segment */
1484 DumpBytes( outfile
, data
, data_offset
, "Data_Segment" );
1486 /* Build the module */
1488 module_size
= BuildModule16( outfile
, code_offset
, data_offset
);
1490 /* Output the DLL descriptor */
1492 fprintf( outfile
, "\nWIN16_DESCRIPTOR %s_Descriptor = \n{\n", DLLName
);
1493 fprintf( outfile
, " \"%s\",\n", DLLName
);
1494 fprintf( outfile
, " Module,\n" );
1495 fprintf( outfile
, " sizeof(Module),\n" );
1496 fprintf( outfile
, " (BYTE *)&Code_Segment,\n" );
1497 fprintf( outfile
, " (BYTE *)Data_Segment\n" );
1498 fprintf( outfile
, "};\n" );
1504 /*******************************************************************
1507 * Build an assembly file from a spec file.
1509 static int BuildSpecFile( FILE *outfile
, char *specname
)
1511 SpecName
= specname
;
1512 SpecFp
= fopen( specname
, "r");
1515 fprintf(stderr
, "Could not open specification file, '%s'\n", specname
);
1519 if (ParseTopLevel() < 0) return -1;
1524 return BuildSpec16File( specname
, outfile
);
1526 return BuildSpec32File( specname
, outfile
);
1528 fprintf( stderr
, "%s: Missing 'type' declaration\n", specname
);
1534 /*******************************************************************
1535 * BuildCallFrom16Func
1537 * Build a 16-bit-to-Wine callback glue function.
1539 * The generated routines are intended to be used as argument conversion
1540 * routines to be called by the CallFrom16... core. Thus, the prototypes of
1541 * the generated routines are (see also CallFrom16):
1543 * extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
1544 * extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
1545 * extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args,
1546 * CONTEXT86 *context );
1547 * extern void WINAPI PREFIX_CallFrom16_C_intr_xxx( FARPROC func, LPBYTE args,
1548 * CONTEXT86 *context );
1550 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
1551 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
1552 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1553 * 'T'=segmented pointer to null-terminated string).
1555 * The generated routines fetch the arguments from the 16-bit stack (pointed
1556 * to by 'args'); the offsets of the single argument values are computed
1557 * according to the calling convention and the argument types. Then, the
1558 * 32-bit entry point is called with these arguments.
1560 * For register functions, the arguments (if present) are converted just
1561 * the same as for normal functions, but in addition the CONTEXT86 pointer
1562 * filled with the current register values is passed to the 32-bit routine.
1563 * (An 'intr' interrupt handler routine is treated exactly like a register
1564 * routine, except that upon return, the flags word pushed onto the stack
1565 * by the interrupt is removed by the 16-bit call stub.)
1568 static void BuildCallFrom16Func( FILE *outfile
, char *profile
, char *prefix
, int local
)
1570 int i
, pos
, argsize
= 0;
1574 char *args
= profile
+ 7;
1577 /* Parse function type */
1579 if (!strncmp( "c_", profile
, 2 )) usecdecl
= 1;
1580 else if (strncmp( "p_", profile
, 2 ))
1582 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1586 if (!strncmp( "word_", profile
+ 2, 5 )) short_ret
= 1;
1587 else if (!strncmp( "regs_", profile
+ 2, 5 )) reg_func
= 1;
1588 else if (!strncmp( "intr_", profile
+ 2, 5 )) reg_func
= 2;
1589 else if (strncmp( "long_", profile
+ 2, 5 ))
1591 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1595 for ( i
= 0; args
[i
]; i
++ )
1598 case 'w': /* word */
1599 case 's': /* s_word */
1603 case 'l': /* long or segmented pointer */
1604 case 'T': /* segmented pointer to null-terminated string */
1605 case 'p': /* linear pointer */
1606 case 't': /* linear pointer to null-terminated string */
1611 ret_type
= reg_func
? "void" : short_ret
? "WORD" : "LONG";
1613 fprintf( outfile
, "typedef %s WINAPI (*proc_%s_t)( ",
1614 ret_type
, profile
);
1616 for ( i
= 0; args
[i
]; i
++ )
1618 if ( i
) fprintf( outfile
, ", " );
1621 case 'w': fprintf( outfile
, "WORD" ); break;
1622 case 's': fprintf( outfile
, "INT16" ); break;
1623 case 'l': case 'T': fprintf( outfile
, "LONG" ); break;
1624 case 'p': case 't': fprintf( outfile
, "LPVOID" ); break;
1628 fprintf( outfile
, "%sstruct _CONTEXT86 *", i
? ", " : "" );
1630 fprintf( outfile
, "void" );
1631 fprintf( outfile
, " );\n" );
1633 fprintf( outfile
, "%s%s WINAPI %s_CallFrom16_%s( FARPROC proc, LPBYTE args%s )\n{\n",
1634 local
? "static " : "", ret_type
, prefix
, profile
,
1635 reg_func
? ", struct _CONTEXT86 *context" : "" );
1637 fprintf( outfile
, " %s((proc_%s_t) proc) (\n",
1638 reg_func
? "" : "return ", profile
);
1640 pos
= !usecdecl
? argsize
: 0;
1641 for ( i
= 0; args
[i
]; i
++ )
1643 if ( i
) fprintf( outfile
, ",\n" );
1644 fprintf( outfile
, " " );
1647 case 'w': /* word */
1648 if ( !usecdecl
) pos
-= 2;
1649 fprintf( outfile
, "*(WORD *)(args+%d)", pos
);
1650 if ( usecdecl
) pos
+= 2;
1653 case 's': /* s_word */
1654 if ( !usecdecl
) pos
-= 2;
1655 fprintf( outfile
, "*(INT16 *)(args+%d)", pos
);
1656 if ( usecdecl
) pos
+= 2;
1659 case 'l': /* long or segmented pointer */
1660 case 'T': /* segmented pointer to null-terminated string */
1661 if ( !usecdecl
) pos
-= 4;
1662 fprintf( outfile
, "*(LONG *)(args+%d)", pos
);
1663 if ( usecdecl
) pos
+= 4;
1666 case 'p': /* linear pointer */
1667 case 't': /* linear pointer to null-terminated string */
1668 if ( !usecdecl
) pos
-= 4;
1669 fprintf( outfile
, "PTR_SEG_TO_LIN( *(SEGPTR *)(args+%d) )", pos
);
1670 if ( usecdecl
) pos
+= 4;
1674 fprintf( stderr
, "Unknown arg type '%c'\n", args
[i
] );
1678 fprintf( outfile
, "%s context", i
? ",\n" : "" );
1679 fprintf( outfile
, " );\n}\n\n" );
1682 /*******************************************************************
1685 * Build a Wine-to-16-bit callback glue function.
1687 * Prototypes for the CallTo16 functions:
1688 * extern WORD CALLBACK PREFIX_CallTo16_word_xxx( FARPROC16 func, args... );
1689 * extern LONG CALLBACK PREFIX_CallTo16_long_xxx( FARPROC16 func, args... );
1691 * These routines are provided solely for convenience; they simply
1692 * write the arguments onto the 16-bit stack, and call the appropriate
1693 * CallTo16... core routine.
1695 * If you have more sophisticated argument conversion requirements than
1696 * are provided by these routines, you might as well call the core
1697 * routines by yourself.
1700 static void BuildCallTo16Func( FILE *outfile
, char *profile
, char *prefix
)
1702 char *args
= profile
+ 5;
1703 int i
, argsize
= 0, short_ret
= 0;
1705 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1706 else if (strncmp( "long_", profile
, 5 ))
1708 fprintf( stderr
, "Invalid function name '%s'.\n", profile
);
1712 fprintf( outfile
, "%s %s_CallTo16_%s( FARPROC16 proc",
1713 short_ret
? "WORD" : "LONG", prefix
, profile
);
1715 for ( i
= 0; args
[i
]; i
++ )
1717 fprintf( outfile
, ", " );
1720 case 'w': fprintf( outfile
, "WORD" ); argsize
+= 2; break;
1721 case 'l': fprintf( outfile
, "LONG" ); argsize
+= 4; break;
1723 fprintf( outfile
, " arg%d", i
+1 );
1725 fprintf( outfile
, " )\n{\n" );
1728 fprintf( outfile
, " LPBYTE args = (LPBYTE)CURRENT_STACK16;\n" );
1731 for ( i
= 0; args
[i
]; i
++ )
1735 case 'w': fprintf( outfile
, " args -= sizeof(WORD); *(WORD" ); break;
1736 case 'l': fprintf( outfile
, " args -= sizeof(LONG); *(LONG" ); break;
1737 default: fprintf( stderr
, "Unexpected case '%c' in BuildCallTo16Func\n",
1740 fprintf( outfile
, " *)args = arg%d;\n", i
+1 );
1743 fprintf( outfile
, " return CallTo16%s( proc, %d );\n}\n\n",
1744 short_ret
? "Word" : "Long", argsize
);
1749 /*******************************************************************
1750 * BuildCallFrom16Core
1752 * This routine builds the core routines used in 16->32 thunks:
1753 * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
1755 * These routines are intended to be called via a far call (with 32-bit
1756 * operand size) from 16-bit code. The 16-bit code stub must push %bp,
1757 * the 32-bit entry point to be called, and the argument conversion
1758 * routine to be used (see stack layout below).
1760 * The core routine completes the STACK16FRAME on the 16-bit stack and
1761 * switches to the 32-bit stack. Then, the argument conversion routine
1762 * is called; it gets passed the 32-bit entry point and a pointer to the
1763 * 16-bit arguments (on the 16-bit stack) as parameters. (You can either
1764 * use conversion routines automatically generated by BuildCallFrom16,
1765 * or write your own for special purposes.)
1767 * The conversion routine must call the 32-bit entry point, passing it
1768 * the converted arguments, and return its return value to the core.
1769 * After the conversion routine has returned, the core switches back
1770 * to the 16-bit stack, converts the return value to the DX:AX format
1771 * (CallFrom16Long), and returns to the 16-bit call stub. All parameters,
1772 * including %bp, are popped off the stack.
1774 * The 16-bit call stub now returns to the caller, popping the 16-bit
1775 * arguments if necessary (pascal calling convention).
1777 * In the case of a 'register' function, CallFrom16Register fills a
1778 * CONTEXT86 structure with the values all registers had at the point
1779 * the first instruction of the 16-bit call stub was about to be
1780 * executed. A pointer to this CONTEXT86 is passed as third parameter
1781 * to the argument conversion routine, which typically passes it on
1782 * to the called 32-bit entry point.
1784 * CallFrom16Thunk is a special variant used by the implementation of
1785 * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
1786 * implemented as follows:
1787 * On entry, the EBX register is set up to contain a flat pointer to the
1788 * 16-bit stack such that EBX+22 points to the first argument.
1789 * Then, the entry point is called, while EBP is set up to point
1790 * to the return address (on the 32-bit stack).
1791 * The called function returns with CX set to the number of bytes
1792 * to be popped of the caller's stack.
1794 * Stack layout upon entry to the core routine (STACK16FRAME):
1796 * (sp+24) word first 16-bit arg
1800 * (sp+14) long 32-bit entry point (reused for Win16 mutex recursion count)
1801 * (sp+12) word ip of actual entry point (necessary for relay debugging)
1802 * (sp+8) long relay (argument conversion) function entry point
1803 * (sp+4) long cs of 16-bit entry point
1804 * (sp) long ip of 16-bit entry point
1806 * Added on the stack:
1807 * (sp-2) word saved gs
1808 * (sp-4) word saved fs
1809 * (sp-6) word saved es
1810 * (sp-8) word saved ds
1811 * (sp-12) long saved ebp
1812 * (sp-16) long saved ecx
1813 * (sp-20) long saved edx
1814 * (sp-24) long saved previous stack
1816 static void BuildCallFrom16Core( FILE *outfile
, int reg_func
, int thunk
, int short_ret
)
1818 char *name
= thunk
? "Thunk" : reg_func
? "Register" : short_ret
? "Word" : "Long";
1820 /* Function header */
1821 fprintf( outfile
, "\n\t.align 4\n" );
1823 fprintf( outfile
, ".stabs \"CallFrom16%s:F1\",36,0,0," PREFIX
"CallFrom16%s\n",
1826 fprintf( outfile
, "\t.globl " PREFIX
"CallFrom16%s\n", name
);
1827 fprintf( outfile
, PREFIX
"CallFrom16%s:\n", name
);
1829 /* Create STACK16FRAME (except STACK32FRAME link) */
1830 fprintf( outfile
, "\tpushw %%gs\n" );
1831 fprintf( outfile
, "\tpushw %%fs\n" );
1832 fprintf( outfile
, "\tpushw %%es\n" );
1833 fprintf( outfile
, "\tpushw %%ds\n" );
1834 fprintf( outfile
, "\tpushl %%ebp\n" );
1835 fprintf( outfile
, "\tpushl %%ecx\n" );
1836 fprintf( outfile
, "\tpushl %%edx\n" );
1838 /* Save original EFlags register */
1839 fprintf( outfile
, "\tpushfl\n" );
1843 /* Get Global Offset Table into %ecx */
1844 fprintf( outfile
, "\tcall .LCallFrom16%s.getgot1\n", name
);
1845 fprintf( outfile
, ".LCallFrom16%s.getgot1:\n", name
);
1846 fprintf( outfile
, "\tpopl %%ecx\n" );
1847 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot1], %%ecx\n", name
);
1850 /* Load 32-bit segment registers */
1851 fprintf( outfile
, "\tmovw $0x%04x, %%dx\n", Data_Selector
);
1853 fprintf( outfile
, "\tdata16\n");
1855 fprintf( outfile
, "\tmovw %%dx, %%ds\n" );
1857 fprintf( outfile
, "\tdata16\n");
1859 fprintf( outfile
, "\tmovw %%dx, %%es\n" );
1863 fprintf( outfile
, "\tmovl " PREFIX
"SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
1864 fprintf( outfile
, "\tmovw (%%edx), %%fs\n" );
1867 fprintf( outfile
, "\tmovw " PREFIX
"SYSLEVEL_Win16CurrentTeb, %%fs\n" );
1869 /* Get address of ldt_copy array into %ecx */
1871 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy@GOT(%%ecx), %%ecx\n" );
1873 fprintf( outfile
, "\tmovl $" PREFIX
"ldt_copy, %%ecx\n" );
1875 /* Translate STACK16FRAME base to flat offset in %edx */
1876 fprintf( outfile
, "\tmovw %%ss, %%dx\n" );
1877 fprintf( outfile
, "\tandl $0xfff8, %%edx\n" );
1878 fprintf( outfile
, "\tmovl (%%ecx,%%edx), %%edx\n" );
1879 fprintf( outfile
, "\tmovzwl %%sp, %%ebp\n" );
1880 fprintf( outfile
, "\tleal (%%ebp,%%edx), %%edx\n" );
1882 /* Get saved flags into %ecx */
1883 fprintf( outfile
, "\tpopl %%ecx\n" );
1885 /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
1886 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET
);
1887 fprintf( outfile
, "\tpushl %%ebp\n" );
1891 fprintf( outfile
,"\tdata16\n");
1893 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET
+ 2 );
1894 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET
);
1895 fprintf( outfile
, "\tpushl %%ds\n" );
1896 fprintf( outfile
, "\tpopl %%ss\n" );
1897 fprintf( outfile
, "\tmovl %%ebp, %%esp\n" );
1898 fprintf( outfile
, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME
, ebp
) );
1902 STACK16FRAME is completely set up
1903 DS, ES, SS: flat data segment
1905 ESP: points to last STACK32FRAME
1906 EBP: points to ebp member of last STACK32FRAME
1907 EDX: points to current STACK16FRAME
1908 ECX: contains saved flags
1909 all other registers: unchanged */
1911 /* Special case: C16ThkSL stub */
1914 /* Set up registers as expected and call thunk */
1915 fprintf( outfile
, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME
)-22 );
1916 fprintf( outfile
, "\tleal -4(%%esp), %%ebp\n" );
1918 fprintf( outfile
, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point
) );
1920 /* Switch stack back */
1921 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
1922 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
1923 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
1925 /* Restore registers and return directly to caller */
1926 fprintf( outfile
, "\taddl $8, %%esp\n" );
1927 fprintf( outfile
, "\tpopl %%ebp\n" );
1928 fprintf( outfile
, "\tpopw %%ds\n" );
1929 fprintf( outfile
, "\tpopw %%es\n" );
1930 fprintf( outfile
, "\tpopw %%fs\n" );
1931 fprintf( outfile
, "\tpopw %%gs\n" );
1932 fprintf( outfile
, "\taddl $20, %%esp\n" );
1934 fprintf( outfile
, "\txorb %%ch, %%ch\n" );
1935 fprintf( outfile
, "\tpopl %%ebx\n" );
1936 fprintf( outfile
, "\taddw %%cx, %%sp\n" );
1937 fprintf( outfile
, "\tpush %%ebx\n" );
1939 fprintf( outfile
, "\t.byte 0x66\n" );
1940 fprintf( outfile
, "\tlret\n" );
1946 /* Build register CONTEXT */
1949 fprintf( outfile
, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86
) );
1951 fprintf( outfile
, "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags
) );
1953 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax
) );
1954 fprintf( outfile
, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx
) );
1955 fprintf( outfile
, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi
) );
1956 fprintf( outfile
, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi
) );
1958 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp
) );
1959 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp
) );
1960 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx
) );
1961 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx
) );
1962 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx
) );
1963 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx
) );
1965 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds
) );
1966 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs
) );
1967 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es
) );
1968 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs
) );
1969 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs
) );
1970 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs
) );
1971 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs
) );
1972 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs
) );
1974 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs
) );
1975 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs
) );
1976 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip
) );
1977 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip
) );
1979 fprintf( outfile
, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET
+2 );
1980 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs
) );
1981 fprintf( outfile
, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET
);
1982 fprintf( outfile
, "\taddl $%d, %%eax\n", STACK16OFFSET(ip
) );
1983 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp
) );
1985 fprintf( outfile
, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave
) );
1988 /* Push address of CONTEXT86 structure -- popped by the relay routine */
1989 fprintf( outfile
, "\tpushl %%esp\n" );
1993 /* Print debug info before call */
1998 fprintf( outfile
, "\tpushl %%ebx\n" );
2000 /* Get Global Offset Table into %ebx (for PLT call) */
2001 fprintf( outfile
, "\tcall .LCallFrom16%s.getgot2\n", name
);
2002 fprintf( outfile
, ".LCallFrom16%s.getgot2:\n", name
);
2003 fprintf( outfile
, "\tpopl %%ebx\n" );
2004 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot2], %%ebx\n", name
);
2007 fprintf( outfile
, "\tpushl %%edx\n" );
2009 fprintf( outfile
, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2010 sizeof(CONTEXT
) + STRUCTOFFSET(STACK32FRAME
, ebp
) );
2012 fprintf( outfile
, "\tpushl $0\n" );
2015 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16@PLT\n ");
2017 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16\n ");
2019 fprintf( outfile
, "\tpopl %%edx\n" );
2020 fprintf( outfile
, "\tpopl %%edx\n" );
2023 fprintf( outfile
, "\tpopl %%ebx\n" );
2026 /* Call relay routine (which will call the API entry point) */
2027 fprintf( outfile
, "\tleal %d(%%edx), %%eax\n", sizeof(STACK16FRAME
) );
2028 fprintf( outfile
, "\tpushl %%eax\n" );
2029 fprintf( outfile
, "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point
) );
2030 fprintf( outfile
, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay
) );
2032 /* Print debug info after call */
2037 fprintf( outfile
, "\tpushl %%ebx\n" );
2039 /* Get Global Offset Table into %ebx (for PLT call) */
2040 fprintf( outfile
, "\tcall .LCallFrom16%s.getgot3\n", name
);
2041 fprintf( outfile
, ".LCallFrom16%s.getgot3:\n", name
);
2042 fprintf( outfile
, "\tpopl %%ebx\n" );
2043 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot3], %%ebx\n", name
);
2046 fprintf( outfile
, "\tpushl %%eax\n" );
2048 fprintf( outfile
, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2049 sizeof(CONTEXT
) + STRUCTOFFSET(STACK32FRAME
, ebp
) );
2051 fprintf( outfile
, "\tpushl $0\n" );
2054 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret@PLT\n ");
2056 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret\n ");
2058 fprintf( outfile
, "\tpopl %%eax\n" );
2059 fprintf( outfile
, "\tpopl %%eax\n" );
2062 fprintf( outfile
, "\tpopl %%ebx\n" );
2068 fprintf( outfile
, "\tmovl %%esp, %%ebx\n" );
2070 /* Switch stack back */
2071 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2072 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
2073 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2075 /* Get return address to CallFrom16 stub */
2076 fprintf( outfile
, "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip
)-4 );
2077 fprintf( outfile
, "\tpopl %%eax\n" );
2078 fprintf( outfile
, "\tpopl %%edx\n" );
2080 /* Restore all registers from CONTEXT */
2081 fprintf( outfile
, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs
) );
2082 fprintf( outfile
, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp
) );
2083 fprintf( outfile
, "\taddl $4, %%esp\n" ); /* room for final return address */
2085 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs
) );
2086 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip
) );
2087 fprintf( outfile
, "\tpushl %%edx\n" );
2088 fprintf( outfile
, "\tpushl %%eax\n" );
2089 fprintf( outfile
, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags
) );
2090 fprintf( outfile
, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs
) );
2092 fprintf( outfile
, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs
) );
2093 fprintf( outfile
, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs
) );
2094 fprintf( outfile
, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs
) );
2096 fprintf( outfile
, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp
) );
2097 fprintf( outfile
, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi
) );
2098 fprintf( outfile
, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi
) );
2099 fprintf( outfile
, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax
) );
2100 fprintf( outfile
, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx
) );
2101 fprintf( outfile
, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx
) );
2102 fprintf( outfile
, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx
) );
2104 fprintf( outfile
, "\tpopl %%ds\n" );
2105 fprintf( outfile
, "\tpopfl\n" );
2106 fprintf( outfile
, "\tlret\n" );
2110 /* Switch stack back */
2111 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2112 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
2113 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2115 /* Restore registers */
2116 fprintf( outfile
, "\tpopl %%edx\n" );
2117 fprintf( outfile
, "\tpopl %%ecx\n" );
2118 fprintf( outfile
, "\tpopl %%ebp\n" );
2119 fprintf( outfile
, "\tpopw %%ds\n" );
2120 fprintf( outfile
, "\tpopw %%es\n" );
2121 fprintf( outfile
, "\tpopw %%fs\n" );
2122 fprintf( outfile
, "\tpopw %%gs\n" );
2124 /* Prepare return value and set flags accordingly */
2126 fprintf( outfile
, "\tshldl $16, %%eax, %%edx\n" );
2127 fprintf( outfile
, "\torl %%eax, %%eax\n" );
2129 /* Return to return stub which will return to caller */
2130 fprintf( outfile
, "\tlret $12\n" );
2135 /*******************************************************************
2138 * This routine builds the core routines used in 32->16 thunks:
2140 * extern void CALLBACK CallTo16Word( SEGPTR target, int nb_args );
2141 * extern void CALLBACK CallTo16Long( SEGPTR target, int nb_args );
2142 * extern void CALLBACK CallTo16RegisterShort( const CONTEXT86 *context, int nb_args );
2143 * extern void CALLBACK CallTo16RegisterLong ( const CONTEXT86 *context, int nb_args );
2145 * These routines can be called directly from 32-bit code.
2147 * All routines expect that the 16-bit stack contents (arguments) were
2148 * already set up by the caller; nb_args must contain the number of bytes
2149 * to be conserved. The 16-bit SS:SP will be set accordinly.
2151 * All other registers are either taken from the CONTEXT86 structure
2152 * or else set to default values. The target routine address is either
2153 * given directly or taken from the CONTEXT86.
2155 * If you want to call a 16-bit routine taking only standard argument types
2156 * (WORD and LONG), you can also have an appropriate argument conversion
2157 * stub automatically generated (see BuildCallTo16); you'd then call this
2158 * stub, which in turn would prepare the 16-bit stack and call the appropiate
2163 static void BuildCallTo16Core( FILE *outfile
, int short_ret
, int reg_func
)
2165 char *name
= reg_func
== 2 ? "RegisterLong" :
2166 reg_func
== 1 ? "RegisterShort" :
2167 short_ret
? "Word" : "Long";
2169 /* Function header */
2170 fprintf( outfile
, "\n\t.align 4\n" );
2172 fprintf( outfile
, ".stabs \"CallTo16%s:F1\",36,0,0," PREFIX
"CallTo16%s\n",
2175 fprintf( outfile
, "\t.globl " PREFIX
"CallTo16%s\n", name
);
2176 fprintf( outfile
, PREFIX
"CallTo16%s:\n", name
);
2178 /* Function entry sequence */
2179 fprintf( outfile
, "\tpushl %%ebp\n" );
2180 fprintf( outfile
, "\tmovl %%esp, %%ebp\n" );
2182 /* Save the 32-bit registers */
2183 fprintf( outfile
, "\tpushl %%ebx\n" );
2184 fprintf( outfile
, "\tpushl %%ecx\n" );
2185 fprintf( outfile
, "\tpushl %%edx\n" );
2186 fprintf( outfile
, "\tpushl %%esi\n" );
2187 fprintf( outfile
, "\tpushl %%edi\n" );
2191 /* Get Global Offset Table into %ebx */
2192 fprintf( outfile
, "\tcall .LCallTo16%s.getgot1\n", name
);
2193 fprintf( outfile
, ".LCallTo16%s.getgot1:\n", name
);
2194 fprintf( outfile
, "\tpopl %%ebx\n" );
2195 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot1], %%ebx\n", name
);
2198 /* Enter Win16 Mutex */
2200 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_EnterWin16Lock@PLT\n" );
2202 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_EnterWin16Lock\n" );
2204 /* Print debugging info */
2207 /* Push flags, number of arguments, and target */
2208 fprintf( outfile
, "\tpushl $%d\n", reg_func
);
2209 fprintf( outfile
, "\tpushl 12(%%ebp)\n" );
2210 fprintf( outfile
, "\tpushl 8(%%ebp)\n" );
2213 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16@PLT\n" );
2215 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16\n" );
2217 fprintf( outfile
, "\taddl $12, %%esp\n" );
2220 /* Get return address */
2222 fprintf( outfile
, "\tmovl " PREFIX
"CallTo16_RetAddr@GOTOFF(%%ebx), %%ecx\n" );
2224 fprintf( outfile
, "\tmovl " PREFIX
"CallTo16_RetAddr, %%ecx\n" );
2226 /* Call the actual CallTo16 routine (simulate a lcall) */
2227 fprintf( outfile
, "\tpushl %%cs\n" );
2228 fprintf( outfile
, "\tcall .LCallTo16%s\n", name
);
2230 /* Convert and push return value */
2233 fprintf( outfile
, "\tmovzwl %%ax, %%eax\n" );
2234 fprintf( outfile
, "\tpushl %%eax\n" );
2236 else if ( reg_func
!= 2 )
2238 fprintf( outfile
, "\tshll $16,%%edx\n" );
2239 fprintf( outfile
, "\tmovw %%ax,%%dx\n" );
2240 fprintf( outfile
, "\tpushl %%edx\n" );
2243 fprintf( outfile
, "\tpushl %%eax\n" );
2247 /* Get Global Offset Table into %ebx (might have been overwritten) */
2248 fprintf( outfile
, "\tcall .LCallTo16%s.getgot2\n", name
);
2249 fprintf( outfile
, ".LCallTo16%s.getgot2:\n", name
);
2250 fprintf( outfile
, "\tpopl %%ebx\n" );
2251 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot2], %%ebx\n", name
);
2254 /* Print debugging info */
2258 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16Ret@PLT\n" );
2260 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16Ret\n" );
2263 /* Leave Win16 Mutex */
2265 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_LeaveWin16Lock@PLT\n" );
2267 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_LeaveWin16Lock\n" );
2269 /* Get return value */
2270 fprintf( outfile
, "\tpopl %%eax\n" );
2272 /* Restore the 32-bit registers */
2273 fprintf( outfile
, "\tpopl %%edi\n" );
2274 fprintf( outfile
, "\tpopl %%esi\n" );
2275 fprintf( outfile
, "\tpopl %%edx\n" );
2276 fprintf( outfile
, "\tpopl %%ecx\n" );
2277 fprintf( outfile
, "\tpopl %%ebx\n" );
2279 /* Function exit sequence */
2280 fprintf( outfile
, "\tpopl %%ebp\n" );
2281 fprintf( outfile
, "\tret $8\n" );
2284 /* Start of the actual CallTo16 routine */
2286 fprintf( outfile
, ".LCallTo16%s:\n", name
);
2288 /* Complete STACK32FRAME */
2289 fprintf( outfile
, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET
);
2290 fprintf( outfile
, "\tmovl %%esp,%%edx\n" );
2292 /* Switch to the 16-bit stack */
2294 fprintf( outfile
,"\tdata16\n");
2296 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2);
2297 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET
);
2298 fprintf( outfile
, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET
);
2300 /* Make %bp point to the previous stackframe (built by CallFrom16) */
2301 fprintf( outfile
, "\tmovzwl %%sp,%%ebp\n" );
2302 fprintf( outfile
, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp
) );
2304 /* Add the specified offset to the new sp */
2305 fprintf( outfile
, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args
) );
2307 /* Push the return address
2308 * With sreg suffix, we push 16:16 address (normal lret)
2309 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
2312 fprintf( outfile
, "\tpushl %%ecx\n" );
2315 fprintf( outfile
, "\tshldl $16, %%ecx, %%eax\n" );
2316 fprintf( outfile
, "\tpushw $0\n" );
2317 fprintf( outfile
, "\tpushw %%ax\n" );
2318 fprintf( outfile
, "\tpushw $0\n" );
2319 fprintf( outfile
, "\tpushw %%cx\n" );
2324 /* Push the called routine address */
2325 fprintf( outfile
, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target
) );
2326 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs
) );
2327 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip
) );
2329 /* Get the registers */
2330 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs
) );
2331 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs
) );
2332 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2333 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs
) );
2334 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2335 fprintf( outfile
, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp
) );
2336 fprintf( outfile
, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi
) );
2337 fprintf( outfile
, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi
) );
2338 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax
) );
2339 fprintf( outfile
, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx
) );
2340 fprintf( outfile
, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx
) );
2341 fprintf( outfile
, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx
) );
2343 /* Get the 16-bit ds */
2344 fprintf( outfile
, "\tpopw %%ds\n" );
2346 else /* not a register function */
2348 /* Push the called routine address */
2349 fprintf( outfile
, "\tpushl %d(%%edx)\n", STACK32OFFSET(target
) );
2351 /* Set %fs to the value saved by the last CallFrom16 */
2352 fprintf( outfile
, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs
)-STACK16OFFSET(bp
) );
2353 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2355 /* Set %ds and %es (and %ax just in case) equal to %ss */
2356 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
2357 fprintf( outfile
, "\tmovw %%ax,%%ds\n" );
2358 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2361 /* Jump to the called routine */
2362 fprintf( outfile
, "\t.byte 0x66\n" );
2363 fprintf( outfile
, "\tlret\n" );
2366 /*******************************************************************
2369 * Build the return code for 16-bit callbacks
2371 static void BuildRet16Func( FILE *outfile
)
2374 * Note: This must reside in the .data section to allow
2375 * run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
2378 fprintf( outfile
, "\n\t.globl " PREFIX
"CallTo16_Ret\n" );
2379 fprintf( outfile
, PREFIX
"CallTo16_Ret:\n" );
2381 /* Restore 32-bit segment registers */
2383 fprintf( outfile
, "\tmovw $0x%04x,%%bx\n", Data_Selector
);
2385 fprintf( outfile
, "\tdata16\n");
2387 fprintf( outfile
, "\tmovw %%bx,%%ds\n" );
2389 fprintf( outfile
, "\tdata16\n");
2391 fprintf( outfile
, "\tmovw %%bx,%%es\n" );
2393 fprintf( outfile
, "\tmovw " PREFIX
"SYSLEVEL_Win16CurrentTeb,%%fs\n" );
2395 /* Restore the 32-bit stack */
2398 fprintf( outfile
, "\tdata16\n");
2400 fprintf( outfile
, "\tmovw %%bx,%%ss\n" );
2401 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET
);
2402 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2404 /* Return to caller */
2406 fprintf( outfile
, "\tlret\n" );
2408 /* Declare the return address variable */
2410 fprintf( outfile
, "\n\t.globl " PREFIX
"CallTo16_RetAddr\n" );
2411 fprintf( outfile
, PREFIX
"CallTo16_RetAddr:\t.long 0\n" );
2414 /*******************************************************************
2415 * BuildCallTo32CBClient
2417 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
2419 * Since the relay stub is itself 32-bit, this should not be a problem;
2420 * unfortunately, the relay stubs are expected to switch back to a
2421 * 16-bit stack (and 16-bit code) after completion :-(
2423 * This would conflict with our 16- vs. 32-bit stack handling, so
2424 * we simply switch *back* to our 32-bit stack before returning to
2427 * The CBClient relay stub expects to be called with the following
2428 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
2429 * stack at the designated places:
2432 * (ebp+14) original arguments to the callback routine
2433 * (ebp+10) far return address to original caller
2434 * (ebp+6) Thunklet target address
2435 * (ebp+2) Thunklet relay ID code
2436 * (ebp) BP (saved by CBClientGlueSL)
2437 * (ebp-2) SI (saved by CBClientGlueSL)
2438 * (ebp-4) DI (saved by CBClientGlueSL)
2439 * (ebp-6) DS (saved by CBClientGlueSL)
2441 * ... buffer space used by the 16-bit side glue for temp copies
2443 * (ebx+4) far return address to 16-bit side glue code
2444 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
2446 * The 32-bit side glue code accesses both the original arguments (via ebp)
2447 * and the temporary copies prepared by the 16-bit side glue (via ebx).
2448 * After completion, the stub will load ss:sp from the buffer at ebx
2449 * and perform a far return to 16-bit code.
2451 * To trick the relay stub into returning to us, we replace the 16-bit
2452 * return address to the glue code by a cs:ip pair pointing to our
2453 * return entry point (the original return address is saved first).
2454 * Our return stub thus called will then reload the 32-bit ss:esp and
2455 * return to 32-bit code (by using and ss:esp value that we have also
2456 * pushed onto the 16-bit stack before and a cs:eip values found at
2457 * that position on the 32-bit stack). The ss:esp to be restored is
2458 * found relative to the 16-bit stack pointer at:
2461 * (ebx-8) sp (32-bit stack pointer)
2463 * The second variant of this routine, CALL32_CBClientEx, which is used
2464 * to implement KERNEL.621, has to cope with yet another problem: Here,
2465 * the 32-bit side directly returns to the caller of the CBClient thunklet,
2466 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
2467 * As we have to return to our 32-bit code first, we have to adapt the
2468 * layout of our temporary area so as to include values for the registers
2469 * that are to be restored, and later (in the implementation of KERNEL.621)
2470 * we *really* restore them. The return stub restores DS, DI, SI, and BP
2471 * from the stack, skips the next 8 bytes (CBClient relay code / target),
2472 * and then performs a lret NN, where NN is the number of arguments to be
2473 * removed. Thus, we prepare our temporary area as follows:
2475 * (ebx+22) 16-bit cs (this segment)
2476 * (ebx+20) 16-bit ip ('16-bit' return entry point)
2477 * (ebx+16) 32-bit ss (flat)
2478 * (ebx+12) 32-bit sp (32-bit stack pointer)
2479 * (ebx+10) 16-bit bp (points to ebx+24)
2480 * (ebx+8) 16-bit si (ignored)
2481 * (ebx+6) 16-bit di (ignored)
2482 * (ebx+4) 16-bit ds (we actually use the flat DS here)
2483 * (ebx+2) 16-bit ss (16-bit stack segment)
2484 * (ebx+0) 16-bit sp (points to ebx+4)
2486 * Note that we ensure that DS is not changed and remains the flat segment,
2487 * and the 32-bit stack pointer our own return stub needs fits just
2488 * perfectly into the 8 bytes that are skipped by the Windows stub.
2489 * One problem is that we have to determine the number of removed arguments,
2490 * as these have to be really removed in KERNEL.621. Thus, the BP value
2491 * that we place in the temporary area to be restored, contains the value
2492 * that SP would have if no arguments were removed. By comparing the actual
2493 * value of SP with this value in our return stub we can compute the number
2494 * of removed arguments. This is then returned to KERNEL.621.
2496 * The stack layout of this function:
2497 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
2498 * (ebp+16) esi pointer to caller's esi value
2499 * (ebp+12) arg ebp value to be set for relay stub
2500 * (ebp+8) func CBClient relay stub address
2504 static void BuildCallTo32CBClient( FILE *outfile
, BOOL isEx
)
2506 char *name
= isEx
? "CBClientEx" : "CBClient";
2507 int size
= isEx
? 24 : 12;
2509 /* Function header */
2511 fprintf( outfile
, "\n\t.align 4\n" );
2513 fprintf( outfile
, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX
"CALL32_%s\n",
2516 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_%s\n", name
);
2517 fprintf( outfile
, PREFIX
"CALL32_%s:\n", name
);
2521 fprintf( outfile
, "\tpushl %%ebp\n" );
2522 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2523 fprintf( outfile
, "\tpushl %%edi\n" );
2524 fprintf( outfile
, "\tpushl %%esi\n" );
2525 fprintf( outfile
, "\tpushl %%ebx\n" );
2527 /* Get the 16-bit stack */
2529 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET
);
2531 /* Convert it to a flat address */
2533 fprintf( outfile
, "\tshldl $16,%%ebx,%%eax\n" );
2534 fprintf( outfile
, "\tandl $0xfff8,%%eax\n" );
2535 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy(%%eax),%%esi\n" );
2536 fprintf( outfile
, "\tmovw %%bx,%%ax\n" );
2537 fprintf( outfile
, "\taddl %%eax,%%esi\n" );
2539 /* Allocate temporary area (simulate STACK16_PUSH) */
2541 fprintf( outfile
, "\tpushf\n" );
2542 fprintf( outfile
, "\tcld\n" );
2543 fprintf( outfile
, "\tleal -%d(%%esi), %%edi\n", size
);
2544 fprintf( outfile
, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME
) );
2545 fprintf( outfile
, "\trep\n\tmovsb\n" );
2546 fprintf( outfile
, "\tpopf\n" );
2548 fprintf( outfile
, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size
, STACKOFFSET
);
2550 fprintf( outfile
, "\tpushl %%edi\n" ); /* remember address */
2552 /* Set up temporary area */
2556 fprintf( outfile
, "\tleal 4(%%edi), %%edi\n" );
2558 fprintf( outfile
, "\tleal -8(%%esp), %%eax\n" );
2559 fprintf( outfile
, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */
2561 fprintf( outfile
, "\tmovl %%ss, %%ax\n" );
2562 fprintf( outfile
, "\tandl $0x0000ffff, %%eax\n" );
2563 fprintf( outfile
, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */
2565 fprintf( outfile
, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME
)-size
+4 + 4 );
2566 fprintf( outfile
, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */
2568 fprintf( outfile
, "\tmovl " PREFIX
"CALL32_%s_RetAddr, %%eax\n", name
);
2569 fprintf( outfile
, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */
2573 fprintf( outfile
, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME
)-size
+4 );
2574 fprintf( outfile
, "\tmovl %%ebx, 0(%%edi)\n" );
2576 fprintf( outfile
, "\tmovl %%ds, %%ax\n" );
2577 fprintf( outfile
, "\tmovw %%ax, 4(%%edi)\n" );
2579 fprintf( outfile
, "\taddl $20, %%ebx\n" );
2580 fprintf( outfile
, "\tmovw %%bx, 10(%%edi)\n" );
2582 fprintf( outfile
, "\tleal -8(%%esp), %%eax\n" );
2583 fprintf( outfile
, "\tmovl %%eax, 12(%%edi)\n" );
2585 fprintf( outfile
, "\tmovl %%ss, %%ax\n" );
2586 fprintf( outfile
, "\tandl $0x0000ffff, %%eax\n" );
2587 fprintf( outfile
, "\tmovl %%eax, 16(%%edi)\n" );
2589 fprintf( outfile
, "\tmovl " PREFIX
"CALL32_%s_RetAddr, %%eax\n", name
);
2590 fprintf( outfile
, "\tmovl %%eax, 20(%%edi)\n" );
2593 /* Set up registers and call CBClient relay stub (simulating a far call) */
2595 fprintf( outfile
, "\tmovl 16(%%ebp), %%esi\n" );
2596 fprintf( outfile
, "\tmovl (%%esi), %%esi\n" );
2598 fprintf( outfile
, "\tmovl %%edi, %%ebx\n" );
2599 fprintf( outfile
, "\tmovl 8(%%ebp), %%eax\n" );
2600 fprintf( outfile
, "\tmovl 12(%%ebp), %%ebp\n" );
2602 fprintf( outfile
, "\tpushl %%cs\n" );
2603 fprintf( outfile
, "\tcall *%%eax\n" );
2605 /* Return new esi value to caller */
2607 fprintf( outfile
, "\tmovl 32(%%esp), %%edi\n" );
2608 fprintf( outfile
, "\tmovl %%esi, (%%edi)\n" );
2610 /* Cleanup temporary area (simulate STACK16_POP) */
2612 fprintf( outfile
, "\tpop %%esi\n" );
2614 fprintf( outfile
, "\tpushf\n" );
2615 fprintf( outfile
, "\tstd\n" );
2616 fprintf( outfile
, "\tdec %%esi\n" );
2617 fprintf( outfile
, "\tleal %d(%%esi), %%edi\n", size
);
2618 fprintf( outfile
, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME
) );
2619 fprintf( outfile
, "\trep\n\tmovsb\n" );
2620 fprintf( outfile
, "\tpopf\n" );
2622 fprintf( outfile
, "\t.byte 0x64\n\taddw $%d,(%d)\n", size
, STACKOFFSET
);
2624 /* Return argument size to caller */
2627 fprintf( outfile
, "\tmovl 32(%%esp), %%ebx\n" );
2628 fprintf( outfile
, "\tmovl %%ebp, (%%ebx)\n" );
2631 /* Restore registers and return */
2633 fprintf( outfile
, "\tpopl %%ebx\n" );
2634 fprintf( outfile
, "\tpopl %%esi\n" );
2635 fprintf( outfile
, "\tpopl %%edi\n" );
2636 fprintf( outfile
, "\tpopl %%ebp\n" );
2637 fprintf( outfile
, "\tret\n" );
2640 static void BuildCallTo32CBClientRet( FILE *outfile
, BOOL isEx
)
2642 char *name
= isEx
? "CBClientEx" : "CBClient";
2644 /* '16-bit' return stub */
2646 fprintf( outfile
, "\n\t.globl " PREFIX
"CALL32_%s_Ret\n", name
);
2647 fprintf( outfile
, PREFIX
"CALL32_%s_Ret:\n", name
);
2651 fprintf( outfile
, "\tmovzwl %%sp, %%ebx\n" );
2652 fprintf( outfile
, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
2656 fprintf( outfile
, "\tmovzwl %%bp, %%ebx\n" );
2657 fprintf( outfile
, "\tsubw %%bp, %%sp\n" );
2658 fprintf( outfile
, "\tmovzwl %%sp, %%ebp\n" );
2659 fprintf( outfile
, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
2661 fprintf( outfile
, "\tlret\n" );
2663 /* Declare the return address variable */
2665 fprintf( outfile
, "\n\t.globl " PREFIX
"CALL32_%s_RetAddr\n", name
);
2666 fprintf( outfile
, PREFIX
"CALL32_%s_RetAddr:\t.long 0\n", name
);
2670 /*******************************************************************
2671 * BuildCallTo32LargeStack
2673 * Build the function used to switch to the original 32-bit stack
2674 * before calling a 32-bit function from 32-bit code. This is used for
2675 * functions that need a large stack, like X bitmaps functions.
2677 * The generated function has the following prototype:
2678 * int xxx( int (*func)(), void *arg );
2680 * The pointer to the function can be retrieved by calling CALL32_Init,
2681 * which also takes care of saving the current 32-bit stack pointer.
2682 * Furthermore, CALL32_Init switches to a new stack and jumps to the
2683 * specified target address.
2685 * NOTE: The CALL32_LargeStack routine may be recursively entered by the
2686 * same thread, but not concurrently entered by several threads.
2688 * Stack layout of CALL32_Init:
2690 * (esp+12) new stack address
2691 * (esp+8) target address
2692 * (esp+4) pointer to variable to receive CALL32_LargeStack address
2695 * Stack layout of CALL32_LargeStack:
2702 static void BuildCallTo32LargeStack( FILE *outfile
)
2704 /* Initialization function */
2706 fprintf( outfile
, "\n\t.align 4\n" );
2708 fprintf( outfile
, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX
"CALL32_Init\n");
2710 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Init\n" );
2711 fprintf( outfile
, "\t.type " PREFIX
"CALL32_Init,@function\n" );
2712 fprintf( outfile
, PREFIX
"CALL32_Init:\n" );
2713 fprintf( outfile
, "\tmovl %%esp,CALL32_Original32_esp\n" );
2714 fprintf( outfile
, "\tpopl %%eax\n" );
2715 fprintf( outfile
, "\tpopl %%eax\n" );
2716 fprintf( outfile
, "\tmovl $CALL32_LargeStack,(%%eax)\n" );
2717 fprintf( outfile
, "\tpopl %%eax\n" );
2718 fprintf( outfile
, "\tpopl %%esp\n" );
2719 fprintf( outfile
, "\tpushl %%eax\n" );
2720 fprintf( outfile
, "\tret\n" );
2722 /* Function header */
2724 fprintf( outfile
, "\n\t.align 4\n" );
2726 fprintf( outfile
, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2728 fprintf( outfile
, "CALL32_LargeStack:\n" );
2732 fprintf( outfile
, "\tpushl %%ebp\n" );
2733 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2735 /* Switch to the original 32-bit stack pointer */
2737 fprintf( outfile
, "\tcmpl $0, CALL32_RecursionCount\n" );
2738 fprintf( outfile
, "\tjne CALL32_skip\n" );
2739 fprintf( outfile
, "\tmovl CALL32_Original32_esp, %%esp\n" );
2740 fprintf( outfile
, "CALL32_skip:\n" );
2742 fprintf( outfile
, "\tincl CALL32_RecursionCount\n" );
2744 /* Transfer the argument and call the function */
2746 fprintf( outfile
, "\tpushl 12(%%ebp)\n" );
2747 fprintf( outfile
, "\tcall *8(%%ebp)\n" );
2749 /* Restore registers and return */
2751 fprintf( outfile
, "\tdecl CALL32_RecursionCount\n" );
2753 fprintf( outfile
, "\tmovl %%ebp,%%esp\n" );
2754 fprintf( outfile
, "\tpopl %%ebp\n" );
2755 fprintf( outfile
, "\tret\n" );
2759 fprintf( outfile
, "\t.data\n" );
2760 fprintf( outfile
, "CALL32_Original32_esp:\t.long 0\n" );
2761 fprintf( outfile
, "CALL32_RecursionCount:\t.long 0\n" );
2762 fprintf( outfile
, "\t.text\n" );
2766 /*******************************************************************
2767 * BuildCallFrom32Regs
2769 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2770 * 'args' is the number of dword arguments.
2774 * (ebp+12) first arg
2775 * (ebp+8) ret addr to user code
2776 * (ebp+4) ret addr to relay code
2778 * (ebp-128) buffer area to allow stack frame manipulation
2779 * (ebp-332) CONTEXT86 struct
2780 * (ebp-336) CONTEXT86 *argument
2781 * .... other arguments copied from (ebp+12)
2783 * The entry point routine is called with a CONTEXT* extra argument,
2784 * following the normal args. In this context structure, EIP_reg
2785 * contains the return address to user code, and ESP_reg the stack
2786 * pointer on return (with the return address and arguments already
2789 static void BuildCallFrom32Regs( FILE *outfile
)
2791 static const int STACK_SPACE
= 128 + sizeof(CONTEXT86
);
2793 /* Function header */
2795 fprintf( outfile
, "\n\t.align 4\n" );
2797 fprintf( outfile
, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX
"CALL32_Regs\n" );
2799 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Regs\n" );
2800 fprintf( outfile
, PREFIX
"CALL32_Regs:\n" );
2802 /* Allocate some buffer space on the stack */
2804 fprintf( outfile
, "\tpushl %%ebp\n" );
2805 fprintf( outfile
, "\tmovl %%esp,%%ebp\n ");
2806 fprintf( outfile
, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE
);
2808 /* Build the context structure */
2810 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax
) - STACK_SPACE
);
2811 fprintf( outfile
, "\tpushfl\n" );
2812 fprintf( outfile
, "\tpopl %%eax\n" );
2813 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags
) - STACK_SPACE
);
2814 fprintf( outfile
, "\tmovl 0(%%ebp),%%eax\n" );
2815 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp
) - STACK_SPACE
);
2816 fprintf( outfile
, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx
) - STACK_SPACE
);
2817 fprintf( outfile
, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx
) - STACK_SPACE
);
2818 fprintf( outfile
, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx
) - STACK_SPACE
);
2819 fprintf( outfile
, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi
) - STACK_SPACE
);
2820 fprintf( outfile
, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi
) - STACK_SPACE
);
2822 fprintf( outfile
, "\txorl %%eax,%%eax\n" );
2823 fprintf( outfile
, "\tmovw %%cs,%%ax\n" );
2824 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs
) - STACK_SPACE
);
2825 fprintf( outfile
, "\tmovw %%es,%%ax\n" );
2826 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs
) - STACK_SPACE
);
2827 fprintf( outfile
, "\tmovw %%fs,%%ax\n" );
2828 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs
) - STACK_SPACE
);
2829 fprintf( outfile
, "\tmovw %%gs,%%ax\n" );
2830 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs
) - STACK_SPACE
);
2831 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
2832 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs
) - STACK_SPACE
);
2833 fprintf( outfile
, "\tmovw %%ds,%%ax\n" );
2834 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs
) - STACK_SPACE
);
2835 fprintf( outfile
, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
2837 fprintf( outfile
, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL
);
2838 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags
) - STACK_SPACE
);
2840 fprintf( outfile
, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
2841 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip
) - STACK_SPACE
);
2843 /* Transfer the arguments */
2845 fprintf( outfile
, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
2846 fprintf( outfile
, "\tpushl %%esp\n" ); /* push ptr to context struct */
2847 fprintf( outfile
, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
2848 fprintf( outfile
, "\tjecxz 1f\n" );
2849 fprintf( outfile
, "\tsubl %%ecx,%%esp\n" );
2850 fprintf( outfile
, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */
2851 fprintf( outfile
, "\tmovl %%esp,%%edi\n" );
2852 fprintf( outfile
, "\tshrl $2,%%ecx\n" );
2853 fprintf( outfile
, "\tcld\n" );
2854 fprintf( outfile
, "\trep\n\tmovsl\n" ); /* copy args */
2856 fprintf( outfile
, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
2857 fprintf( outfile
, "\tleal 12(%%ebp,%%eax),%%eax\n" );
2858 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2860 /* Call the entry point */
2862 fprintf( outfile
, "\tcall *0(%%ebx)\n" );
2864 /* Store %eip and %ebp onto the new stack */
2866 fprintf( outfile
, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2867 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip
) - STACK_SPACE
);
2868 fprintf( outfile
, "\tmovl %%eax,-4(%%edx)\n" );
2869 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp
) - STACK_SPACE
);
2870 fprintf( outfile
, "\tmovl %%eax,-8(%%edx)\n" );
2872 /* Restore the context structure */
2874 /* Note: we don't bother to restore %cs, %ds and %ss
2875 * changing them in 32-bit code is a recipe for disaster anyway
2877 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs
) - STACK_SPACE
);
2878 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2879 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs
) - STACK_SPACE
);
2880 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2881 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs
) - STACK_SPACE
);
2882 fprintf( outfile
, "\tmovw %%ax,%%gs\n" );
2884 fprintf( outfile
, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi
) - STACK_SPACE
);
2885 fprintf( outfile
, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi
) - STACK_SPACE
);
2886 fprintf( outfile
, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx
) - STACK_SPACE
);
2887 fprintf( outfile
, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx
) - STACK_SPACE
);
2888 fprintf( outfile
, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx
) - STACK_SPACE
);
2890 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags
) - STACK_SPACE
);
2891 fprintf( outfile
, "\tpushl %%eax\n" );
2892 fprintf( outfile
, "\tpopfl\n" );
2893 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax
) - STACK_SPACE
);
2895 fprintf( outfile
, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2896 fprintf( outfile
, "\tleal -8(%%ebp),%%esp\n" );
2897 fprintf( outfile
, "\tpopl %%ebp\n" );
2898 fprintf( outfile
, "\tret\n" );
2902 /*******************************************************************
2905 * Build the spec files
2907 static int BuildSpec( FILE *outfile
, int argc
, char *argv
[] )
2910 for (i
= 2; i
< argc
; i
++)
2911 if (BuildSpecFile( outfile
, argv
[i
] ) < 0) return -1;
2915 /*******************************************************************
2918 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
2920 static int BuildGlue( FILE *outfile
, char * outname
, int argc
, char *argv
[] )
2927 infile
= fopen( argv
[2], "r" );
2934 else infile
= stdin
;
2938 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
2939 argc
> 2? argv
[2] : "<stdin>" );
2940 fprintf( outfile
, "#include \"builtin16.h\"\n" );
2941 fprintf( outfile
, "#include \"stackframe.h\"\n\n" );
2943 /* Build the callback glue functions */
2945 while (fgets( buffer
, sizeof(buffer
), infile
))
2947 if (strstr( buffer
, "### start build ###" )) break;
2949 while (fgets( buffer
, sizeof(buffer
), infile
))
2952 if ( (p
= strstr( buffer
, "CallFrom16_" )) != NULL
)
2954 char *q
, *profile
= p
+ strlen( "CallFrom16_" );
2955 for (q
= profile
; (*q
== '_') || isalpha(*q
); q
++ )
2958 for (q
= p
-1; q
> buffer
&& ((*q
== '_') || isalnum(*q
)); q
-- )
2960 if ( ++q
< p
) p
[-1] = '\0'; else q
= "";
2961 BuildCallFrom16Func( outfile
, profile
, q
, FALSE
);
2963 if ( (p
= strstr( buffer
, "CallTo16_" )) != NULL
)
2965 char *q
, *profile
= p
+ strlen( "CallTo16_" );
2966 for (q
= profile
; (*q
== '_') || isalpha(*q
); q
++ )
2969 for (q
= p
-1; q
> buffer
&& ((*q
== '_') || isalnum(*q
)); q
-- )
2971 if ( ++q
< p
) p
[-1] = '\0'; else q
= "";
2972 BuildCallTo16Func( outfile
, profile
, q
);
2974 if (strstr( buffer
, "### stop build ###" )) break;
2981 /*******************************************************************
2984 * Build the 16-bit callbacks
2986 static int BuildCall16( FILE *outfile
, char * outname
)
2994 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2995 fprintf( outfile
, "\t.text\n" );
3000 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
3001 getcwd(buffer
, sizeof(buffer
));
3004 * The stabs help the internal debugger as they are an indication that it
3005 * is sensible to step into a thunk/trampoline.
3007 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
3008 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
3009 fprintf( outfile
, "\t.text\n" );
3010 fprintf( outfile
, "\t.align 4\n" );
3011 fprintf( outfile
, "Code_Start:\n\n" );
3013 fprintf( outfile
, PREFIX
"Call16_Start:\n" );
3014 fprintf( outfile
, "\t.globl "PREFIX
"Call16_Start\n" );
3015 fprintf( outfile
, "\t.byte 0\n\n" );
3018 /* Standard CallFrom16 routine (WORD return) */
3019 BuildCallFrom16Core( outfile
, FALSE
, FALSE
, TRUE
);
3021 /* Standard CallFrom16 routine (DWORD return) */
3022 BuildCallFrom16Core( outfile
, FALSE
, FALSE
, FALSE
);
3024 /* Register CallFrom16 routine */
3025 BuildCallFrom16Core( outfile
, TRUE
, FALSE
, FALSE
);
3027 /* C16ThkSL CallFrom16 routine */
3028 BuildCallFrom16Core( outfile
, FALSE
, TRUE
, FALSE
);
3030 /* Standard CallTo16 routine (WORD return) */
3031 BuildCallTo16Core( outfile
, TRUE
, FALSE
);
3033 /* Standard CallTo16 routine (DWORD return) */
3034 BuildCallTo16Core( outfile
, FALSE
, FALSE
);
3036 /* Register CallTo16 routine (16:16 retf) */
3037 BuildCallTo16Core( outfile
, FALSE
, 1 );
3039 /* Register CallTo16 routine (16:32 retf) */
3040 BuildCallTo16Core( outfile
, FALSE
, 2 );
3042 /* CBClientThunkSL routine */
3043 BuildCallTo32CBClient( outfile
, FALSE
);
3045 /* CBClientThunkSLEx routine */
3046 BuildCallTo32CBClient( outfile
, TRUE
);
3048 fprintf( outfile
, PREFIX
"Call16_End:\n" );
3049 fprintf( outfile
, "\t.globl "PREFIX
"Call16_End\n" );
3052 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
3053 fprintf( outfile
, ".Letext:\n");
3056 /* The whole Call16_Ret segment must lie within the .data section */
3057 fprintf( outfile
, "\n\t.data\n" );
3058 fprintf( outfile
, "\t.globl " PREFIX
"Call16_Ret_Start\n" );
3059 fprintf( outfile
, PREFIX
"Call16_Ret_Start:\n" );
3061 /* Standard CallTo16 return stub */
3062 BuildRet16Func( outfile
);
3064 /* CBClientThunkSL return stub */
3065 BuildCallTo32CBClientRet( outfile
, FALSE
);
3067 /* CBClientThunkSLEx return stub */
3068 BuildCallTo32CBClientRet( outfile
, TRUE
);
3070 /* End of Call16_Ret segment */
3071 fprintf( outfile
, "\n\t.globl " PREFIX
"Call16_Ret_End\n" );
3072 fprintf( outfile
, PREFIX
"Call16_Ret_End:\n" );
3074 #else /* __i386__ */
3076 fprintf( outfile
, PREFIX
"Call16_Start:\n" );
3077 fprintf( outfile
, "\t.globl "PREFIX
"Call16_Start\n" );
3078 fprintf( outfile
, "\t.byte 0\n\n" );
3079 fprintf( outfile
, PREFIX
"Call16_End:\n" );
3080 fprintf( outfile
, "\t.globl "PREFIX
"Call16_End\n" );
3082 fprintf( outfile
, "\t.globl " PREFIX
"Call16_Ret_Start\n" );
3083 fprintf( outfile
, PREFIX
"Call16_Ret_Start:\n" );
3084 fprintf( outfile
, "\t.byte 0\n\n" );
3085 fprintf( outfile
, "\n\t.globl " PREFIX
"Call16_Ret_End\n" );
3086 fprintf( outfile
, PREFIX
"Call16_Ret_End:\n" );
3088 #endif /* __i386__ */
3093 /*******************************************************************
3096 * Build the 32-bit callbacks
3098 static int BuildCall32( FILE *outfile
, char * outname
)
3106 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
3107 fprintf( outfile
, "\t.text\n" );
3112 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
3113 getcwd(buffer
, sizeof(buffer
));
3116 * The stabs help the internal debugger as they are an indication that it
3117 * is sensible to step into a thunk/trampoline.
3119 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
3120 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
3121 fprintf( outfile
, "\t.text\n" );
3122 fprintf( outfile
, "\t.align 4\n" );
3123 fprintf( outfile
, "Code_Start:\n" );
3126 /* Build the 32-bit large stack callback */
3128 BuildCallTo32LargeStack( outfile
);
3130 /* Build the register callback function */
3132 BuildCallFrom32Regs( outfile
);
3135 fprintf( outfile
, "\t.text\n");
3136 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
3137 fprintf( outfile
, ".Letext:\n");
3140 #else /* __i386__ */
3142 /* Just to avoid an empty file */
3143 fprintf( outfile
, "\t.long 0\n" );
3145 #endif /* __i386__ */
3150 /*******************************************************************
3153 static void usage(void)
3156 "usage: build [-pic] [-o outfile] -spec SPECNAMES\n"
3157 " build [-pic] [-o outfile] -glue SOURCE_FILE\n"
3158 " build [-pic] [-o outfile] -call16\n"
3159 " build [-pic] [-o outfile] -call32\n" );
3164 /*******************************************************************
3167 int main(int argc
, char **argv
)
3169 char *outname
= NULL
;
3170 FILE *outfile
= stdout
;
3173 if (argc
< 2) usage();
3175 if (!strcmp( argv
[1], "-pic" ))
3180 if (argc
< 2) usage();
3183 if (!strcmp( argv
[1], "-o" ))
3188 if (argc
< 2) usage();
3189 if (!(outfile
= fopen( outname
, "w" )))
3191 fprintf( stderr
, "Unable to create output file '%s'\n", outname
);
3196 /* Retrieve the selector values; this assumes that we are building
3197 * the asm files on the platform that will also run them. Probably
3198 * a safe assumption to make.
3200 GET_CS( Code_Selector
);
3201 GET_DS( Data_Selector
);
3203 if (!strcmp( argv
[1], "-spec" ))
3204 res
= BuildSpec( outfile
, argc
, argv
);
3205 else if (!strcmp( argv
[1], "-glue" ))
3206 res
= BuildGlue( outfile
, outname
, argc
, argv
);
3207 else if (!strcmp( argv
[1], "-call16" ))
3208 res
= BuildCall16( outfile
, outname
);
3209 else if (!strcmp( argv
[1], "-call32" ))
3210 res
= BuildCall32( outfile
, outname
);