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
20 #include "selectors.h"
21 #include "stackframe.h"
22 #include "builtin16.h"
25 #ifdef NEED_UNDERSCORE_PREFIX
31 #ifdef HAVE_ASM_STRING
32 # define STRING ".string"
34 # define STRING ".ascii"
37 #if defined(__GNUC__) && !defined(__svr4__)
46 TYPE_BYTE
, /* byte variable (Win16) */
47 TYPE_WORD
, /* word variable (Win16) */
48 TYPE_LONG
, /* long variable (Win16) */
49 TYPE_PASCAL_16
, /* pascal function with 16-bit return (Win16) */
50 TYPE_PASCAL
, /* pascal function with 32-bit return (Win16) */
51 TYPE_ABS
, /* absolute value (Win16) */
52 TYPE_REGISTER
, /* register function */
53 TYPE_INTERRUPT
, /* interrupt handler function (Win16) */
54 TYPE_STUB
, /* unimplemented stub */
55 TYPE_STDCALL
, /* stdcall function (Win32) */
56 TYPE_CDECL
, /* cdecl function (Win32) */
57 TYPE_VARARGS
, /* varargs function (Win32) */
58 TYPE_EXTERN
, /* external symbol (Win32) */
59 TYPE_FORWARD
, /* forwarded function (Win32) */
63 static const char * const TypeNames
[TYPE_NBTYPES
] =
66 "byte", /* TYPE_BYTE */
67 "word", /* TYPE_WORD */
68 "long", /* TYPE_LONG */
69 "pascal16", /* TYPE_PASCAL_16 */
70 "pascal", /* TYPE_PASCAL */
71 "equate", /* TYPE_ABS */
72 "register", /* TYPE_REGISTER */
73 "interrupt", /* TYPE_INTERRUPT */
74 "stub", /* TYPE_STUB */
75 "stdcall", /* TYPE_STDCALL */
76 "cdecl", /* TYPE_CDECL */
77 "varargs", /* TYPE_VARARGS */
78 "extern", /* TYPE_EXTERN */
79 "forward" /* TYPE_FORWARD */
82 #define MAX_ORDINALS 2048
83 #define MAX_IMPORTS 16
85 /* Callback function used for stub functions */
86 #define STUB_CALLBACK \
87 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
153 static ORDDEF OrdinalDefinitions
[MAX_ORDINALS
];
155 static SPEC_TYPE SpecType
= SPEC_INVALID
;
156 static char DLLName
[80];
157 static char DLLFileName
[80];
158 static int Limit
= 0;
159 static int Base
= MAX_ORDINALS
;
160 static int DLLHeapSize
= 0;
161 static char *SpecName
;
163 static WORD Code_Selector
, Data_Selector
;
164 static char DLLInitFunc
[80];
165 static char *DLLImports
[MAX_IMPORTS
];
166 static int nb_imports
= 0;
168 char *ParseBuffer
= NULL
;
173 static int UsePIC
= 0;
175 static int debugging
= 1;
177 /* Offset of a structure field relative to the start of the struct */
178 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
180 /* Offset of register relative to the start of the CONTEXT struct */
181 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT86,reg)
183 /* Offset of register relative to the start of the STACK16FRAME struct */
184 #define STACK16OFFSET(reg) STRUCTOFFSET(STACK16FRAME,reg)
186 /* Offset of register relative to the start of the STACK32FRAME struct */
187 #define STACK32OFFSET(reg) STRUCTOFFSET(STACK32FRAME,reg)
189 /* Offset of the stack pointer relative to %fs:(0) */
190 #define STACKOFFSET (STRUCTOFFSET(TEB,cur_stack))
193 static void *xmalloc (size_t size
)
197 res
= malloc (size
? size
: 1);
200 fprintf (stderr
, "Virtual memory exhausted.\n");
207 static void *xrealloc (void *ptr
, size_t size
)
209 void *res
= realloc (ptr
, size
);
212 fprintf (stderr
, "Virtual memory exhausted.\n");
218 static char *xstrdup( const char *str
)
220 char *res
= strdup( str
);
223 fprintf (stderr
, "Virtual memory exhausted.\n");
229 static int IsNumberString(char *s
)
238 static char *strupper(char *s
)
242 for(p
= s
; *p
!= '\0'; p
++)
248 static char * GetTokenInLine(void)
253 if (ParseNext
!= ParseBuffer
)
255 if (ParseSaveChar
== '\0')
257 *ParseNext
= ParseSaveChar
;
261 * Remove initial white space.
263 for (p
= ParseNext
; isspace(*p
); p
++)
266 if ((*p
== '\0') || (*p
== '#'))
273 if (*token
!= '(' && *token
!= ')')
274 while (*p
!= '\0' && *p
!= '(' && *p
!= ')' && !isspace(*p
))
284 static char * GetToken(void)
288 if (ParseBuffer
== NULL
)
290 ParseBuffer
= xmalloc(512);
291 ParseNext
= ParseBuffer
;
295 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
297 if (ParseBuffer
[0] != '#')
302 while ((token
= GetTokenInLine()) == NULL
)
304 ParseNext
= ParseBuffer
;
308 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
310 if (ParseBuffer
[0] != '#')
319 /*******************************************************************
322 * Parse a variable definition.
324 static int ParseVariable( ORDDEF
*odp
)
329 int value_array_size
;
331 char *token
= GetToken();
334 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
335 SpecName
, Line
, token
);
340 value_array_size
= 25;
341 value_array
= xmalloc(sizeof(*value_array
) * value_array_size
);
343 while ((token
= GetToken()) != NULL
)
348 value_array
[n_values
++] = strtol(token
, &endptr
, 0);
349 if (n_values
== value_array_size
)
351 value_array_size
+= 25;
352 value_array
= xrealloc(value_array
,
353 sizeof(*value_array
) * value_array_size
);
356 if (endptr
== NULL
|| *endptr
!= '\0')
358 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
359 SpecName
, Line
, token
);
366 fprintf(stderr
, "%s:%d: End of file in variable declaration\n",
371 odp
->u
.var
.n_values
= n_values
;
372 odp
->u
.var
.values
= xrealloc(value_array
, sizeof(*value_array
) * n_values
);
378 /*******************************************************************
379 * ParseExportFunction
381 * Parse a function definition.
383 static int ParseExportFunction( ORDDEF
*odp
)
391 if (odp
->type
== TYPE_STDCALL
)
393 fprintf( stderr
, "%s:%d: 'stdcall' not supported for Win16\n",
399 if ((odp
->type
== TYPE_PASCAL
) || (odp
->type
== TYPE_PASCAL_16
))
401 fprintf( stderr
, "%s:%d: 'pascal' not supported for Win32\n",
413 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
414 SpecName
, Line
, token
);
418 for (i
= 0; i
< sizeof(odp
->u
.func
.arg_types
)-1; i
++)
424 if (!strcmp(token
, "word"))
425 odp
->u
.func
.arg_types
[i
] = 'w';
426 else if (!strcmp(token
, "s_word"))
427 odp
->u
.func
.arg_types
[i
] = 's';
428 else if (!strcmp(token
, "long") || !strcmp(token
, "segptr"))
429 odp
->u
.func
.arg_types
[i
] = 'l';
430 else if (!strcmp(token
, "ptr"))
431 odp
->u
.func
.arg_types
[i
] = 'p';
432 else if (!strcmp(token
, "str"))
433 odp
->u
.func
.arg_types
[i
] = 't';
434 else if (!strcmp(token
, "wstr"))
435 odp
->u
.func
.arg_types
[i
] = 'W';
436 else if (!strcmp(token
, "segstr"))
437 odp
->u
.func
.arg_types
[i
] = 'T';
438 else if (!strcmp(token
, "double"))
440 odp
->u
.func
.arg_types
[i
++] = 'l';
441 odp
->u
.func
.arg_types
[i
] = 'l';
445 fprintf(stderr
, "%s:%d: Unknown variable type '%s'\n",
446 SpecName
, Line
, token
);
449 if (SpecType
== SPEC_WIN32
)
451 if (strcmp(token
, "long") &&
452 strcmp(token
, "ptr") &&
453 strcmp(token
, "str") &&
454 strcmp(token
, "wstr") &&
455 strcmp(token
, "double"))
457 fprintf( stderr
, "%s:%d: Type '%s' not supported for Win32\n",
458 SpecName
, Line
, token
);
463 if ((*token
!= ')') || (i
>= sizeof(odp
->u
.func
.arg_types
)))
465 fprintf( stderr
, "%s:%d: Too many arguments\n", SpecName
, Line
);
468 odp
->u
.func
.arg_types
[i
] = '\0';
469 if ((odp
->type
== TYPE_STDCALL
) && !i
)
470 odp
->type
= TYPE_CDECL
; /* stdcall is the same as cdecl for 0 args */
471 strcpy(odp
->u
.func
.link_name
, GetToken());
476 /*******************************************************************
479 * Parse an 'equate' definition.
481 static int ParseEquate( ORDDEF
*odp
)
485 char *token
= GetToken();
486 int value
= strtol(token
, &endptr
, 0);
487 if (endptr
== NULL
|| *endptr
!= '\0')
489 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
490 SpecName
, Line
, token
);
494 if (SpecType
== SPEC_WIN32
)
496 fprintf( stderr
, "%s:%d: 'equate' not supported for Win32\n",
501 odp
->u
.abs
.value
= value
;
506 /*******************************************************************
509 * Parse a 'stub' definition.
511 static int ParseStub( ORDDEF
*odp
)
513 odp
->u
.func
.arg_types
[0] = '\0';
514 strcpy( odp
->u
.func
.link_name
, STUB_CALLBACK
);
519 /*******************************************************************
522 * Parse an 'varargs' definition.
524 static int ParseVarargs( ORDDEF
*odp
)
528 if (SpecType
== SPEC_WIN16
)
530 fprintf( stderr
, "%s:%d: 'varargs' not supported for Win16\n",
538 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
539 SpecName
, Line
, token
);
545 fprintf(stderr
, "%s:%d: Expected ')' got '%s'\n",
546 SpecName
, Line
, token
);
550 strcpy( odp
->u
.vargs
.link_name
, GetToken() );
554 /*******************************************************************
557 * Parse an 'interrupt' definition.
559 static int ParseInterrupt( ORDDEF
*odp
)
563 if (SpecType
== SPEC_WIN32
)
565 fprintf( stderr
, "%s:%d: 'interrupt' not supported for Win32\n",
573 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
574 SpecName
, Line
, token
);
580 fprintf(stderr
, "%s:%d: Expected ')' got '%s'\n",
581 SpecName
, Line
, token
);
585 odp
->u
.func
.arg_types
[0] = '\0';
586 strcpy( odp
->u
.func
.link_name
, GetToken() );
591 /*******************************************************************
594 * Parse an 'extern' definition.
596 static int ParseExtern( ORDDEF
*odp
)
598 if (SpecType
== SPEC_WIN16
)
600 fprintf( stderr
, "%s:%d: 'extern' not supported for Win16\n",
604 strcpy( odp
->u
.ext
.link_name
, GetToken() );
609 /*******************************************************************
612 * Parse a 'forward' definition.
614 static int ParseForward( ORDDEF
*odp
)
616 if (SpecType
== SPEC_WIN16
)
618 fprintf( stderr
, "%s:%d: 'forward' not supported for Win16\n",
622 strcpy( odp
->u
.fwd
.link_name
, GetToken() );
627 /*******************************************************************
630 * Parse an ordinal definition.
632 static int ParseOrdinal(int ordinal
)
637 if (ordinal
>= MAX_ORDINALS
)
639 fprintf(stderr
, "%s:%d: Ordinal number too large\n", SpecName
, Line
);
642 if (ordinal
> Limit
) Limit
= ordinal
;
643 if (ordinal
< Base
) Base
= ordinal
;
645 odp
= &OrdinalDefinitions
[ordinal
];
646 if (!(token
= GetToken()))
648 fprintf(stderr
, "%s:%d: Expected type after ordinal\n", SpecName
, Line
);
652 for (odp
->type
= 0; odp
->type
< TYPE_NBTYPES
; odp
->type
++)
653 if (TypeNames
[odp
->type
] && !strcmp( token
, TypeNames
[odp
->type
] ))
656 if (odp
->type
>= TYPE_NBTYPES
)
659 "%s:%d: Expected type after ordinal, found '%s' instead\n",
660 SpecName
, Line
, token
);
664 if (!(token
= GetToken()))
666 fprintf( stderr
, "%s:%d: Expected name after type\n", SpecName
, Line
);
669 strcpy( odp
->name
, token
);
677 return ParseVariable( odp
);
683 return ParseExportFunction( odp
);
685 return ParseInterrupt( odp
);
687 return ParseEquate( odp
);
689 return ParseStub( odp
);
691 return ParseVarargs( odp
);
693 return ParseExtern( odp
);
695 return ParseForward( odp
);
697 fprintf( stderr
, "Should not happen\n" );
703 /*******************************************************************
708 static int ParseTopLevel(void)
712 while ((token
= GetToken()) != NULL
)
714 if (strcmp(token
, "name") == 0)
716 strcpy(DLLName
, GetToken());
718 if (!DLLFileName
[0]) sprintf( DLLFileName
, "%s.DLL", DLLName
);
720 else if (strcmp(token
, "file") == 0)
722 strcpy(DLLFileName
, GetToken());
723 strupper(DLLFileName
);
725 else if (strcmp(token
, "type") == 0)
728 if (!strcmp(token
, "win16" )) SpecType
= SPEC_WIN16
;
729 else if (!strcmp(token
, "win32" )) SpecType
= SPEC_WIN32
;
732 fprintf(stderr
, "%s:%d: Type must be 'win16' or 'win32'\n",
737 else if (strcmp(token
, "heap") == 0)
740 if (!IsNumberString(token
))
742 fprintf(stderr
, "%s:%d: Expected number after heap\n",
746 DLLHeapSize
= atoi(token
);
748 else if (strcmp(token
, "init") == 0)
750 strcpy(DLLInitFunc
, GetToken());
752 fprintf(stderr
, "%s:%d: Expected function name after init\n", SpecName
, Line
);
754 else if (strcmp(token
, "import") == 0)
756 if (nb_imports
>= MAX_IMPORTS
)
758 fprintf( stderr
, "%s:%d: Too many imports (limit %d)\n",
759 SpecName
, Line
, MAX_IMPORTS
);
762 if (SpecType
!= SPEC_WIN32
)
764 fprintf( stderr
, "%s:%d: Imports not supported for Win16\n", SpecName
, Line
);
767 DLLImports
[nb_imports
++] = xstrdup(GetToken());
769 else if (IsNumberString(token
))
774 ordinal
= atoi(token
);
775 if ((rv
= ParseOrdinal(ordinal
)) < 0)
781 "%s:%d: Expected name, id, length or ordinal\n",
791 /*******************************************************************
794 * Store a list of ints into a byte array.
796 static int StoreVariableCode( unsigned char *buffer
, int size
, ORDDEF
*odp
)
803 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
804 buffer
[i
] = odp
->u
.var
.values
[i
];
807 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
808 ((unsigned short *)buffer
)[i
] = odp
->u
.var
.values
[i
];
811 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
812 ((unsigned int *)buffer
)[i
] = odp
->u
.var
.values
[i
];
815 return odp
->u
.var
.n_values
* size
;
819 /*******************************************************************
822 * Dump a byte stream into the assembly code.
824 static void DumpBytes( FILE *outfile
, const unsigned char *data
, int len
,
829 fprintf( outfile
, "\nstatic BYTE %s[] = \n{", label
);
831 for (i
= 0; i
< len
; i
++)
833 if (!(i
& 0x0f)) fprintf( outfile
, "\n " );
834 fprintf( outfile
, "%d", *data
++ );
835 if (i
< len
- 1) fprintf( outfile
, ", " );
837 fprintf( outfile
, "\n};\n" );
841 /*******************************************************************
844 * Build the in-memory representation of a 16-bit NE module, and dump it
845 * as a byte stream into the assembly code.
847 static int BuildModule16( FILE *outfile
, int max_code_offset
,
848 int max_data_offset
)
854 SEGTABLEENTRY
*pSegment
;
858 ET_BUNDLE
*bundle
= 0;
863 * OFSTRUCT File information
864 * SEGTABLEENTRY Segment 1 (code)
865 * SEGTABLEENTRY Segment 2 (data)
866 * WORD[2] Resource table (empty)
867 * BYTE[2] Imported names (empty)
868 * BYTE[n] Resident names table
869 * BYTE[n] Entry table
872 buffer
= xmalloc( 0x10000 );
874 pModule
= (NE_MODULE
*)buffer
;
875 memset( pModule
, 0, sizeof(*pModule
) );
876 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
879 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_BUILTIN
| NE_FFLAGS_LIBMODULE
;
881 pModule
->heap_size
= DLLHeapSize
;
882 pModule
->stack_size
= 0;
887 pModule
->seg_count
= 2;
888 pModule
->modref_count
= 0;
889 pModule
->nrname_size
= 0;
890 pModule
->modref_table
= 0;
891 pModule
->nrname_fpos
= 0;
892 pModule
->moveable_entries
= 0;
893 pModule
->alignment
= 0;
894 pModule
->truetype
= 0;
895 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
896 pModule
->misc_flags
= 0;
897 pModule
->dlls_to_init
= 0;
898 pModule
->nrname_handle
= 0;
899 pModule
->min_swap_area
= 0;
900 pModule
->expected_version
= 0x030a;
901 pModule
->module32
= 0;
903 pModule
->self_loading_sel
= 0;
905 /* File information */
907 pFileInfo
= (OFSTRUCT
*)(pModule
+ 1);
908 pModule
->fileinfo
= (int)pFileInfo
- (int)pModule
;
909 memset( pFileInfo
, 0, sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
) );
910 pFileInfo
->cBytes
= sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
)
911 + strlen(DLLFileName
);
912 strcpy( pFileInfo
->szPathName
, DLLFileName
);
913 pstr
= (char *)pFileInfo
+ pFileInfo
->cBytes
+ 1;
915 #ifdef __i386__ /* FIXME: Alignment problems! */
919 pSegment
= (SEGTABLEENTRY
*)pstr
;
920 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
921 pSegment
->filepos
= 0;
922 pSegment
->size
= max_code_offset
;
924 pSegment
->minsize
= max_code_offset
;
928 pModule
->dgroup_entry
= (int)pSegment
- (int)pModule
;
929 pSegment
->filepos
= 0;
930 pSegment
->size
= max_data_offset
;
931 pSegment
->flags
= NE_SEGFLAGS_DATA
;
932 pSegment
->minsize
= max_data_offset
;
938 pword
= (WORD
*)pSegment
;
939 pModule
->res_table
= (int)pword
- (int)pModule
;
943 /* Imported names table */
945 pstr
= (char *)pword
;
946 pModule
->import_table
= (int)pstr
- (int)pModule
;
950 /* Resident names table */
952 pModule
->name_table
= (int)pstr
- (int)pModule
;
953 /* First entry is module name */
954 *pstr
= strlen(DLLName
);
955 strcpy( pstr
+ 1, DLLName
);
958 pstr
+= sizeof(WORD
);
959 /* Store all ordinals */
960 odp
= OrdinalDefinitions
+ 1;
961 for (i
= 1; i
<= Limit
; i
++, odp
++)
963 if (!odp
->name
[0]) continue;
964 *pstr
= strlen( odp
->name
);
965 strcpy( pstr
+ 1, odp
->name
);
966 strupper( pstr
+ 1 );
969 pstr
+= sizeof(WORD
);
975 pModule
->entry_table
= (int)pstr
- (int)pModule
;
976 odp
= OrdinalDefinitions
+ 1;
977 for (i
= 1; i
<= Limit
; i
++, odp
++)
989 selector
= 1; /* Code selector */
995 selector
= 2; /* Data selector */
999 selector
= 0xfe; /* Constant selector */
1003 selector
= 0; /* Invalid selector */
1010 if ( bundle
&& bundle
->last
+1 == i
)
1015 bundle
->next
= (char *)pstr
- (char *)pModule
;
1017 bundle
= (ET_BUNDLE
*)pstr
;
1018 bundle
->first
= i
-1;
1021 pstr
+= sizeof(ET_BUNDLE
);
1024 /* FIXME: is this really correct ?? */
1025 entry
= (ET_ENTRY
*)pstr
;
1026 entry
->type
= 0xff; /* movable */
1027 entry
->flags
= 3; /* exported & public data */
1028 entry
->segnum
= selector
;
1029 entry
->offs
= odp
->offset
;
1030 pstr
+= sizeof(ET_ENTRY
);
1035 /* Dump the module content */
1037 DumpBytes( outfile
, (char *)pModule
, (int)pstr
- (int)pModule
,
1039 return (int)pstr
- (int)pModule
;
1043 /*******************************************************************
1046 * Build a Win32 C file from a spec file.
1048 static int BuildSpec32File( char * specfile
, FILE *outfile
)
1051 int i
, nb_names
, fwd_size
= 0;
1053 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
1055 fprintf( outfile
, "#include \"builtin32.h\"\n\n" );
1057 /* Output code for all stubs functions */
1059 fprintf( outfile
, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
1061 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1063 if (odp
->type
!= TYPE_STUB
) continue;
1064 fprintf( outfile
, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
1068 /* Output code for all register functions */
1070 fprintf( outfile
, "#ifdef __i386__\n" );
1071 fprintf( outfile
, "#ifndef __GNUC__\n" );
1072 fprintf( outfile
, "static void __asm__dummy() {\n" );
1073 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
1074 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1076 if (odp
->type
!= TYPE_REGISTER
) continue;
1078 "__asm__(\".align 4\\n\\t\"\n"
1079 " \".globl " PREFIX
"%s\\n\\t\"\n"
1080 " \".type " PREFIX
"%s,@function\\n\\t\"\n"
1081 " \"" PREFIX
"%s:\\n\\t\"\n"
1082 " \"call " PREFIX
"CALL32_Regs\\n\\t\"\n"
1083 " \".long " PREFIX
"__regs_%s\\n\\t\"\n"
1084 " \".byte %d,%d\");\n",
1085 odp
->u
.func
.link_name
, odp
->u
.func
.link_name
,
1086 odp
->u
.func
.link_name
, odp
->u
.func
.link_name
,
1087 4 * strlen(odp
->u
.func
.arg_types
),
1088 4 * strlen(odp
->u
.func
.arg_types
) );
1090 fprintf( outfile
, "#ifndef __GNUC__\n" );
1091 fprintf( outfile
, "}\n" );
1092 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
1093 fprintf( outfile
, "#endif /* defined(__i386__) */\n" );
1095 /* Output the DLL functions prototypes */
1097 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1102 fprintf( outfile
, "extern void %s();\n", odp
->u
.vargs
.link_name
);
1105 fprintf( outfile
, "extern void %s();\n", odp
->u
.ext
.link_name
);
1110 fprintf( outfile
, "extern void %s();\n", odp
->u
.func
.link_name
);
1113 fwd_size
+= strlen(odp
->u
.fwd
.link_name
) + 1;
1119 fprintf(stderr
,"build: function type %d not available for Win32\n",
1125 /* Output LibMain function */
1126 if (DLLInitFunc
[0]) fprintf( outfile
, "extern void %s();\n", DLLInitFunc
);
1129 /* Output the DLL functions table */
1131 fprintf( outfile
, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1133 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1138 fprintf( outfile
, " 0" );
1141 fprintf( outfile
, " %s", odp
->u
.vargs
.link_name
);
1144 fprintf( outfile
, " %s", odp
->u
.ext
.link_name
);
1149 fprintf( outfile
, " %s", odp
->u
.func
.link_name
);
1152 fprintf( outfile
, " __stub_%d", i
);
1155 fprintf( outfile
, " (ENTRYPOINT32)\"%s\"", odp
->u
.fwd
.link_name
);
1160 if (i
< Limit
) fprintf( outfile
, ",\n" );
1162 fprintf( outfile
, "\n};\n\n" );
1164 /* Output the DLL names table */
1167 fprintf( outfile
, "static const char * const FuncNames[] =\n{\n" );
1168 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1170 if (odp
->type
== TYPE_INVALID
) continue;
1171 if (nb_names
++) fprintf( outfile
, ",\n" );
1172 fprintf( outfile
, " \"%s\"", odp
->name
);
1174 fprintf( outfile
, "\n};\n\n" );
1176 /* Output the DLL argument types */
1178 fprintf( outfile
, "static const unsigned int ArgTypes[%d] =\n{\n",
1180 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1182 unsigned int j
, mask
= 0;
1183 if ((odp
->type
== TYPE_STDCALL
) || (odp
->type
== TYPE_CDECL
) ||
1184 (odp
->type
== TYPE_REGISTER
))
1185 for (j
= 0; odp
->u
.func
.arg_types
[j
]; j
++)
1187 if (odp
->u
.func
.arg_types
[j
] == 't') mask
|= 1<< (j
*2);
1188 if (odp
->u
.func
.arg_types
[j
] == 'W') mask
|= 2<< (j
*2);
1190 fprintf( outfile
, " %d", mask
);
1191 if (i
< Limit
) fprintf( outfile
, ",\n" );
1193 fprintf( outfile
, "\n};\n\n" );
1195 /* Output the DLL ordinals table */
1197 fprintf( outfile
, "static const unsigned short FuncOrdinals[] =\n{\n" );
1199 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1201 if (odp
->type
== TYPE_INVALID
) continue;
1202 if (nb_names
++) fprintf( outfile
, ",\n" );
1203 fprintf( outfile
, " %d", i
- Base
);
1205 fprintf( outfile
, "\n};\n\n" );
1207 /* Output the DLL functions arguments */
1209 fprintf( outfile
, "static const unsigned char FuncArgs[%d] =\n{\n",
1211 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1217 args
= (unsigned char)strlen(odp
->u
.func
.arg_types
);
1220 args
= 0x80 | (unsigned char)strlen(odp
->u
.func
.arg_types
);
1223 args
= 0x40 | (unsigned char)strlen(odp
->u
.func
.arg_types
);
1232 fprintf( outfile
, " 0x%02x", args
);
1233 if (i
< Limit
) fprintf( outfile
, ",\n" );
1235 fprintf( outfile
, "\n};\n\n" );
1237 /* Output the DLL imports */
1241 fprintf( outfile
, "static const char * const Imports[%d] =\n{\n", nb_imports
);
1242 for (i
= 0; i
< nb_imports
; i
++)
1244 fprintf( outfile
, " \"%s\"", DLLImports
[i
] );
1245 if (i
< nb_imports
-1) fprintf( outfile
, ",\n" );
1247 fprintf( outfile
, "\n};\n\n" );
1250 /* Output the DLL descriptor */
1252 fprintf( outfile
, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1254 fprintf( outfile
, " \"%s\",\n", DLLName
);
1255 fprintf( outfile
, " %d,\n", Base
);
1256 fprintf( outfile
, " %d,\n", Limit
- Base
+ 1 );
1257 fprintf( outfile
, " %d,\n", nb_names
);
1258 fprintf( outfile
, " %d,\n", nb_imports
);
1259 fprintf( outfile
, " %d,\n", (fwd_size
+ 3) & ~3 );
1266 fprintf( outfile
, " %s,\n", nb_imports
? "Imports" : "0" );
1267 fprintf( outfile
, " %s\n", DLLInitFunc
[0] ? DLLInitFunc
: "0" );
1268 fprintf( outfile
, "};\n" );
1272 /*******************************************************************
1275 static int Spec16TypeCompare( const void *e1
, const void *e2
)
1277 const ORDDEF
*odp1
= *(const ORDDEF
**)e1
;
1278 const ORDDEF
*odp2
= *(const ORDDEF
**)e2
;
1280 int type1
= (odp1
->type
== TYPE_CDECL
) ? 0
1281 : (odp1
->type
== TYPE_REGISTER
) ? 3
1282 : (odp1
->type
== TYPE_INTERRUPT
) ? 4
1283 : (odp1
->type
== TYPE_PASCAL_16
) ? 1 : 2;
1285 int type2
= (odp2
->type
== TYPE_CDECL
) ? 0
1286 : (odp2
->type
== TYPE_REGISTER
) ? 3
1287 : (odp2
->type
== TYPE_INTERRUPT
) ? 4
1288 : (odp2
->type
== TYPE_PASCAL_16
) ? 1 : 2;
1290 int retval
= type1
- type2
;
1292 retval
= strcmp( odp1
->u
.func
.arg_types
, odp2
->u
.func
.arg_types
);
1297 /*******************************************************************
1300 * Build a Win16 assembly file from a spec file.
1302 static int BuildSpec16File( char * specfile
, FILE *outfile
)
1304 ORDDEF
*odp
, **typelist
;
1306 int code_offset
, data_offset
, module_size
;
1307 unsigned char *data
;
1311 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
1313 fprintf( outfile
, "#define __FLATCS__ 0x%04x\n", Code_Selector
);
1314 fprintf( outfile
, "#include \"builtin16.h\"\n\n" );
1316 data
= (unsigned char *)xmalloc( 0x10000 );
1317 memset( data
, 0, 16 );
1321 /* Build sorted list of all argument types, without duplicates */
1323 typelist
= (ORDDEF
**)calloc( Limit
+1, sizeof(ORDDEF
*) );
1325 odp
= OrdinalDefinitions
;
1326 for (i
= j
= 0; i
<= Limit
; i
++, odp
++)
1331 case TYPE_INTERRUPT
:
1334 case TYPE_PASCAL_16
:
1336 typelist
[j
++] = odp
;
1343 qsort( typelist
, j
, sizeof(ORDDEF
*), Spec16TypeCompare
);
1348 typelist
[k
++] = typelist
[i
++];
1349 while ( i
< j
&& Spec16TypeCompare( typelist
+ i
, typelist
+ k
-1 ) == 0 )
1353 /* Output CallFrom16 profiles needed by this .spec file */
1355 fprintf( outfile
, "\n/* ### start build ### */\n" );
1357 for ( i
= 0; i
< k
; i
++ )
1358 fprintf( outfile
, "extern void %s_CallFrom16_%s_%s_%s();\n",
1360 (typelist
[i
]->type
== TYPE_CDECL
) ? "c" : "p",
1361 (typelist
[i
]->type
== TYPE_REGISTER
) ? "regs" :
1362 (typelist
[i
]->type
== TYPE_INTERRUPT
) ? "intr" :
1363 (typelist
[i
]->type
== TYPE_PASCAL_16
) ? "word" : "long",
1364 typelist
[i
]->u
.func
.arg_types
);
1366 fprintf( outfile
, "/* ### stop build ### */\n\n" );
1368 /* Output the DLL functions prototypes */
1370 odp
= OrdinalDefinitions
;
1371 for (i
= 0; i
<= Limit
; i
++, odp
++)
1376 case TYPE_INTERRUPT
:
1379 case TYPE_PASCAL_16
:
1380 fprintf( outfile
, "extern void %s();\n", odp
->u
.func
.link_name
);
1387 /* Output code segment */
1389 fprintf( outfile
, "\nstatic ENTRYPOINT16 Code_Segment[] = \n{\n" );
1392 odp
= OrdinalDefinitions
;
1393 for (i
= 0; i
<= Limit
; i
++, odp
++)
1398 odp
->offset
= 0xffff;
1402 odp
->offset
= LOWORD(odp
->u
.abs
.value
);
1406 odp
->offset
= data_offset
;
1407 data_offset
+= StoreVariableCode( data
+ data_offset
, 1, odp
);
1411 odp
->offset
= data_offset
;
1412 data_offset
+= StoreVariableCode( data
+ data_offset
, 2, odp
);
1416 odp
->offset
= data_offset
;
1417 data_offset
+= StoreVariableCode( data
+ data_offset
, 4, odp
);
1421 case TYPE_INTERRUPT
:
1424 case TYPE_PASCAL_16
:
1426 fprintf( outfile
, " /* %s.%d */ ", DLLName
, i
);
1427 fprintf( outfile
, "EP( %s, %s_CallFrom16_%s_%s_%s ),\n",
1428 odp
->u
.func
.link_name
,
1430 (odp
->type
== TYPE_CDECL
) ? "c" : "p",
1431 (odp
->type
== TYPE_REGISTER
) ? "regs" :
1432 (odp
->type
== TYPE_INTERRUPT
) ? "intr" :
1433 (odp
->type
== TYPE_PASCAL_16
) ? "word" : "long",
1434 odp
->u
.func
.arg_types
);
1436 odp
->offset
= code_offset
;
1437 code_offset
+= sizeof(ENTRYPOINT16
);
1441 fprintf(stderr
,"build: function type %d not available for Win16\n",
1447 if (!code_offset
) /* Make sure the code segment is not empty */
1449 fprintf( outfile
, " { },\n" );
1450 code_offset
+= sizeof(ENTRYPOINT16
);
1453 fprintf( outfile
, "};\n" );
1455 /* Output data segment */
1457 DumpBytes( outfile
, data
, data_offset
, "Data_Segment" );
1459 /* Build the module */
1461 module_size
= BuildModule16( outfile
, code_offset
, data_offset
);
1463 /* Output the DLL descriptor */
1465 fprintf( outfile
, "\nWIN16_DESCRIPTOR %s_Descriptor = \n{\n", DLLName
);
1466 fprintf( outfile
, " \"%s\",\n", DLLName
);
1467 fprintf( outfile
, " Module,\n" );
1468 fprintf( outfile
, " sizeof(Module),\n" );
1469 fprintf( outfile
, " (BYTE *)Code_Segment,\n" );
1470 fprintf( outfile
, " (BYTE *)Data_Segment\n" );
1471 fprintf( outfile
, "};\n" );
1477 /*******************************************************************
1480 * Build an assembly file from a spec file.
1482 static int BuildSpecFile( FILE *outfile
, char *specname
)
1484 SpecName
= specname
;
1485 SpecFp
= fopen( specname
, "r");
1488 fprintf(stderr
, "Could not open specification file, '%s'\n", specname
);
1492 if (ParseTopLevel() < 0) return -1;
1497 return BuildSpec16File( specname
, outfile
);
1499 return BuildSpec32File( specname
, outfile
);
1501 fprintf( stderr
, "%s: Missing 'type' declaration\n", specname
);
1507 /*******************************************************************
1508 * BuildCallFrom16Func
1510 * Build a 16-bit-to-Wine callback glue function. The syntax of the function
1511 * profile is: call_type_xxxxx, where 'call' is the letter 'c' or 'p' for C or
1512 * Pascal calling convention, 'type' is one of 'regs', 'intr', 'word' or
1513 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1514 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1515 * 'T'=segmented pointer to null-terminated string).
1516 * For register functions, the arguments (if present) are converted just
1517 * the same as for normal functions, but in addition a CONTEXT pointer
1518 * filled with the current register values is passed to the 32-bit routine.
1519 * A 'intr' interrupt handler routine is treated like a register routine
1520 * without arguments, except that upon return, the flags word pushed onto
1521 * the stack by the interrupt is removed.
1523 * This glue function contains only that part of the 16->32 thunk that is
1524 * variable (depending on the type and number of arguments); the glue routine
1525 * is part of a particular DLL and uses a routine provided by the core to
1526 * perform those actions that do not depend on the argument type.
1528 * The 16->32 glue routine consists of a main entry point (which must be part
1529 * of the ELF .data section) and two auxillary routines (in the .text section).
1530 * The main entry point pushes address of the 'Thunk' auxillary routine onto
1531 * the stack and then jumps to the appropriate core CallFrom16... routine.
1532 * Furthermore, at a fixed position relative to the main entry point, the
1533 * function profile string is stored if relay debugging is active; this string
1534 * will be consulted by the debugging routines in the core to correctly
1535 * display the arguments.
1537 * The core will perform the switch to 32-bit, and then call back to the
1538 * 'Thunk' auxillary routine. This routine is expected to perform the
1540 * - modify the auxillary routine address in the STACK16FRAME to now
1541 * point to the 'ThunkRet' routine
1542 * - convert arguemnts and push them onto the 32-bit stack
1543 * - call the 32-bit target routine
1545 * After the target routine (and then the 'Thunk' routine) have returned,
1546 * the core part will switch back to 16-bit, and finally jump to the
1547 * 'ThunkRet' auxillary routine. This routine is expected to convert the
1548 * return value if necessary (%eax -> %dx:%ax), and perform the appropriate
1549 * return to the 16-bit caller (lret or lret $argsize).
1551 * In the case of a 'register' routine, there is no 'ThunkRet' auxillary
1552 * routine, as the reloading of all registers and return to 16-bit code
1553 * is done by the core routine. The number of arguments to be popped off
1554 * the caller's stack must be returned (in %eax) by the 'Thunk' routine.
1556 * NOTE: The generated routines contain only proper position-independent
1557 * code and may thus be used within shared objects (e.g. libwine.so
1558 * or elfdlls). The exception is the main entry point, which must
1559 * contain absolute relocations but cannot yet use the GOT; thus
1560 * this entry point is made part of the ELF .data section.
1562 static void BuildCallFrom16Func( FILE *outfile
, char *profile
, char *prefix
)
1564 int i
, pos
, argsize
= 0;
1568 char *args
= profile
+ 7;
1570 /* Parse function type */
1572 if (!strncmp( "c_", profile
, 2 )) usecdecl
= 1;
1573 else if (strncmp( "p_", profile
, 2 ))
1575 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1579 if (!strncmp( "word_", profile
+ 2, 5 )) short_ret
= 1;
1580 else if (!strncmp( "regs_", profile
+ 2, 5 )) reg_func
= 1;
1581 else if (!strncmp( "intr_", profile
+ 2, 5 )) reg_func
= 2;
1582 else if (strncmp( "long_", profile
+ 2, 5 ))
1584 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1588 for ( i
= 0; args
[i
]; i
++ )
1591 case 'w': /* word */
1592 case 's': /* s_word */
1596 case 'l': /* long or segmented pointer */
1597 case 'T': /* segmented pointer to null-terminated string */
1598 case 'p': /* linear pointer */
1599 case 't': /* linear pointer to null-terminated string */
1605 * Build main entry point (in .data section)
1607 * NOTE: If you change this, you must also change the retrieval of
1608 * the profile string relative to the main entry point address
1609 * (see BUILTIN_GetEntryPoint16 in if1632/builtin.c).
1611 fprintf( outfile
, "\t.data\n" );
1612 fprintf( outfile
, "\t.globl " PREFIX
"%s_CallFrom16_%s\n", prefix
, profile
);
1613 fprintf( outfile
, PREFIX
"%s_CallFrom16_%s:\n", prefix
, profile
);
1614 fprintf( outfile
, "\tpushl $.L%s_Thunk_%s\n", prefix
, profile
);
1615 fprintf( outfile
, "\tjmp " PREFIX
"%s\n",
1616 !reg_func
? "CallFrom16" : "CallFrom16Register" );
1618 fprintf( outfile
, "\t" STRING
" \"%s\\0\"\n", profile
);
1621 * Build *Thunk* routine
1623 * This routine gets called by the main CallFrom16 routine with
1624 * registers set up as follows:
1626 * STACK16FRAME is completely set up on the 16-bit stack
1627 * DS, ES, SS: flat data segment
1629 * ESP: points to 32-bit stack
1630 * EBP: points to ebp member of last STACK32FRAME
1631 * EDX: points to current STACK16FRAME
1632 * ECX: points to ldt_copy
1635 fprintf( outfile
, "\t.text\n" );
1636 fprintf( outfile
, ".L%s_Thunk_%s:\n", prefix
, profile
);
1639 fprintf( outfile
, "\tleal 4(%%esp), %%eax\n"
1640 "\tpushl %%eax\n" );
1642 fprintf( outfile
, "\taddl $[.L%s_ThunkRet_%s - .L%s_Thunk_%s], %d(%%edx)\n",
1643 prefix
, profile
, prefix
, profile
,
1644 STACK16OFFSET(relay
));
1646 /* Copy the arguments */
1647 pos
= (usecdecl
? argsize
: 0) + sizeof( STACK16FRAME
);
1649 for ( i
= strlen(args
)-1; i
>= 0; i
-- )
1652 case 'w': /* word */
1653 if ( usecdecl
) pos
-= 2;
1654 fprintf( outfile
, "\tmovzwl %d(%%edx),%%eax\n", pos
);
1655 fprintf( outfile
, "\tpushl %%eax\n" );
1656 if ( !usecdecl
) pos
+= 2;
1659 case 's': /* s_word */
1660 if ( usecdecl
) pos
-= 2;
1661 fprintf( outfile
, "\tmovswl %d(%%edx),%%eax\n", pos
);
1662 fprintf( outfile
, "\tpushl %%eax\n" );
1663 if ( !usecdecl
) pos
+= 2;
1666 case 'l': /* long or segmented pointer */
1667 case 'T': /* segmented pointer to null-terminated string */
1668 if ( usecdecl
) pos
-= 4;
1669 fprintf( outfile
, "\tpushl %d(%%edx)\n", pos
);
1670 if ( !usecdecl
) pos
+= 4;
1673 case 'p': /* linear pointer */
1674 case 't': /* linear pointer to null-terminated string */
1675 if ( usecdecl
) pos
-= 4;
1676 /* Get the selector */
1677 fprintf( outfile
, "\tmovzwl %d(%%edx),%%eax\n", pos
+ 2 );
1678 /* Get the selector base */
1679 fprintf( outfile
, "\tandl $0xfff8,%%eax\n" );
1680 fprintf( outfile
, "\tpushl (%%ecx,%%eax)\n" );
1681 /* Add the offset */
1682 fprintf( outfile
, "\tmovzwl %d(%%edx),%%eax\n", pos
);
1683 fprintf( outfile
, "\taddl %%eax,(%%esp)\n" );
1684 if ( !usecdecl
) pos
+= 4;
1688 fprintf( stderr
, "Unknown arg type '%c'\n", args
[i
] );
1691 /* Call entry point */
1692 fprintf( outfile
, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point
) );
1695 fprintf( outfile
, "\tmovl $%d, %%eax\n", reg_func
== 2 ? 2 : argsize
);
1697 fprintf( outfile
, "\tret\n" );
1701 * Build *ThunkRet* routine
1703 * At this point, all registers are set up for return to 16-bit code.
1704 * EAX contains the function return value.
1705 * SS:SP point to the return address to the caller (on 16-bit stack).
1709 fprintf( outfile
, ".L%s_ThunkRet_%s:\n", prefix
, profile
);
1712 fprintf( outfile
, "\tshldl $16, %%eax, %%edx\n" );
1714 if ( !usecdecl
&& argsize
)
1716 fprintf( outfile
, "\t.byte 0x66\n" );
1717 fprintf( outfile
, "\tlret $%d\n", argsize
);
1721 fprintf( outfile
, "\t.byte 0x66\n" );
1722 fprintf( outfile
, "\tlret\n" );
1725 fprintf( outfile
, "\n" );
1728 /*******************************************************************
1731 * Build a Wine-to-16-bit callback glue function. As above, this glue
1732 * routine will only contain the argument-type dependent part of the
1733 * thunk; the rest will be done by a routine provided by the core.
1735 * Prototypes for the CallTo16 functions:
1736 * extern WORD PREFIX_CallTo16_word_xxx( FARPROC16 func, args... );
1737 * extern LONG PREFIX_CallTo16_long_xxx( FARPROC16 func, args... );
1739 * The main entry point of the glue routine simply performs a call to
1740 * the proper core routine depending on the return type (WORD/LONG).
1741 * Note that the core will never perform a return from this call; however,
1742 * it will use the return address on the stack to access the other
1743 * argument-type dependent parts of the thunk. (If relay debugging is
1744 * active, the core routine will access the number of arguments stored
1745 * in the code section immediately precending the main entry point).
1747 * After the core routine has performed the switch to 16-bit code, it
1748 * will call the argument-transfer routine provided by the glue code
1749 * immediately after the main entry point. This routine is expected
1750 * to transfer the arguments to the 16-bit stack, finish loading the
1751 * register for 16-bit code (%ds and %es must be loaded from %ss),
1752 * and call the 16-bit target.
1754 * The target will return to a 16:16 return address provided by the core.
1755 * The core will finalize the switch back to 32-bit and the return to
1756 * the caller without additional support by the glue code. Note that
1757 * the 32-bit arguments will not be popped off the stack (hence the
1758 * CallTo... routine must *not* be declared WINAPI/CALLBACK).
1761 static void BuildCallTo16Func( FILE *outfile
, char *profile
, char *prefix
)
1763 char *args
= profile
+ 5;
1764 int pos
, short_ret
= 0;
1766 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1767 else if (strncmp( "long_", profile
, 5 ))
1769 fprintf( stderr
, "Invalid function name '%s'.\n", profile
);
1775 /* Number of arguments (for relay debugging) */
1776 fprintf( outfile
, "\n\t.align 4\n" );
1777 fprintf( outfile
, "\t.long %d\n", strlen(args
) );
1780 /* Main entry point */
1783 fprintf( outfile
, ".stabs \"%s_CallTo16_%s:F1\",36,0,0," PREFIX
"%s_CallTo16_%s\n",
1784 prefix
, profile
, prefix
, profile
);
1786 fprintf( outfile
, "\t.globl " PREFIX
"%s_CallTo16_%s\n", prefix
, profile
);
1787 fprintf( outfile
, PREFIX
"%s_CallTo16_%s:\n", prefix
, profile
);
1790 fprintf( outfile
, "\tcall " PREFIX
"CallTo16Word\n" );
1792 fprintf( outfile
, "\tcall " PREFIX
"CallTo16Long\n" );
1794 /* Return to caller (using STDCALL calling convention) */
1795 if ( strlen( args
) > 0 )
1796 fprintf( outfile
, "\tret $%d\n", strlen( args
) * 4 );
1799 fprintf( outfile
, "\tret\n" );
1801 /* Note: the arg transfer routine must start exactly three bytes
1802 after the return stub, hence the nop's */
1803 fprintf( outfile
, "\tnop\n" );
1804 fprintf( outfile
, "\tnop\n" );
1808 * The core routine will call here with registers set up as follows:
1810 * SS:SP points to the 16-bit stack
1811 * SS:BP points to the bp member of last STACK16FRAME
1812 * EDX points to the current STACK32FRAME
1813 * ECX contains the 16:16 return address
1814 * FS contains the last 16-bit %fs value
1817 /* Transfer the arguments */
1819 pos
= STACK32OFFSET(args
) + 4; /* first arg is target address */
1824 case 'w': /* word */
1825 fprintf( outfile
, "\tpushw %d(%%edx)\n", pos
);
1827 case 'l': /* long */
1828 fprintf( outfile
, "\tpushl %d(%%edx)\n", pos
);
1831 fprintf( stderr
, "Unexpected case '%c' in BuildCallTo16Func\n",
1837 /* Push the return address */
1839 fprintf( outfile
, "\tpushl %%ecx\n" );
1841 /* Push the called routine address */
1843 fprintf( outfile
, "\tpushl %d(%%edx)\n", STACK32OFFSET(args
) );
1845 /* Set %ds and %es (and %ax just in case) equal to %ss */
1847 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
1848 fprintf( outfile
, "\tmovw %%ax,%%ds\n" );
1849 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
1851 /* Jump to the called routine */
1853 fprintf( outfile
, "\t.byte 0x66\n" );
1854 fprintf( outfile
, "\tlret\n" );
1859 /*******************************************************************
1860 * BuildCallFrom16Core
1862 * This routine builds the core routines used in 16->32 thunks:
1863 * CallFrom16, CallFrom16Register, and CallFrom16Thunk.
1865 * CallFrom16 and CallFrom16Register are used by the 16->32 glue code
1866 * as described above. CallFrom16Thunk is a special variant used by
1867 * the implementation of the Win95 16->32 thunk functions C16ThkSL and
1868 * C16ThkSL01 and is implemented as follows:
1869 * On entry, the EBX register is set up to contain a flat pointer to the
1870 * 16-bit stack such that EBX+22 points to the first argument.
1871 * Then, the entry point is called, while EBP is set up to point
1872 * to the return address (on the 32-bit stack).
1873 * The called function returns with CX set to the number of bytes
1874 * to be popped of the caller's stack.
1876 * Stack layout upon entry to the core routine (STACK16FRAME):
1878 * (sp+22) word first 16-bit arg
1882 * (sp+12) long 32-bit entry point (reused for Win16 mutex recursion count)
1883 * (sp+8) long cs of 16-bit entry point
1884 * (sp+4) long ip of 16-bit entry point
1885 * (sp) long auxillary relay function address
1887 * Added on the stack:
1888 * (sp-2) word saved gs
1889 * (sp-4) word saved fs
1890 * (sp-6) word saved es
1891 * (sp-8) word saved ds
1892 * (sp-12) long saved ebp
1893 * (sp-16) long saved ecx
1894 * (sp-20) long saved edx
1895 * (sp-24) long saved previous stack
1897 static void BuildCallFrom16Core( FILE *outfile
, int reg_func
, int thunk
)
1899 char *name
= thunk
? "Thunk" : reg_func
? "Register" : "";
1901 /* Function header */
1902 fprintf( outfile
, "\n\t.align 4\n" );
1904 fprintf( outfile
, ".stabs \"CallFrom16%s:F1\",36,0,0," PREFIX
"CallFrom16%s\n",
1907 fprintf( outfile
, "\t.globl " PREFIX
"CallFrom16%s\n", name
);
1908 fprintf( outfile
, PREFIX
"CallFrom16%s:\n", name
);
1910 /* No relay function for 'thunk' */
1912 fprintf( outfile
, "\tpushl $0\n" );
1914 /* Create STACK16FRAME (except STACK32FRAME link) */
1915 fprintf( outfile
, "\tpushw %%gs\n" );
1916 fprintf( outfile
, "\tpushw %%fs\n" );
1917 fprintf( outfile
, "\tpushw %%es\n" );
1918 fprintf( outfile
, "\tpushw %%ds\n" );
1919 fprintf( outfile
, "\tpushl %%ebp\n" );
1920 fprintf( outfile
, "\tpushl %%ecx\n" );
1921 fprintf( outfile
, "\tpushl %%edx\n" );
1925 /* Get Global Offset Table into %ecx */
1926 fprintf( outfile
, "\tcall .LCallFrom16%s.getgot1\n", name
);
1927 fprintf( outfile
, ".LCallFrom16%s.getgot1:\n", name
);
1928 fprintf( outfile
, "\tpopl %%ecx\n" );
1929 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot1], %%ecx\n", name
);
1932 /* Load 32-bit segment registers */
1933 fprintf( outfile
, "\tmovw $0x%04x, %%dx\n", Data_Selector
);
1935 fprintf( outfile
, "\tdata16\n");
1937 fprintf( outfile
, "\tmovw %%dx, %%ds\n" );
1939 fprintf( outfile
, "\tdata16\n");
1941 fprintf( outfile
, "\tmovw %%dx, %%es\n" );
1945 fprintf( outfile
, "\tmovl " PREFIX
"SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
1946 fprintf( outfile
, "\tmovw (%%edx), %%fs\n" );
1949 fprintf( outfile
, "\tmovw " PREFIX
"SYSLEVEL_Win16CurrentTeb, %%fs\n" );
1951 /* Get address of ldt_copy array into %ecx */
1953 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy@GOT(%%ecx), %%ecx\n" );
1955 fprintf( outfile
, "\tmovl $" PREFIX
"ldt_copy, %%ecx\n" );
1957 /* Translate STACK16FRAME base to flat offset in %edx */
1958 fprintf( outfile
, "\tmovw %%ss, %%dx\n" );
1959 fprintf( outfile
, "\tandl $0xfff8, %%edx\n" );
1960 fprintf( outfile
, "\tmovl (%%ecx,%%edx), %%edx\n" );
1961 fprintf( outfile
, "\tmovzwl %%sp, %%ebp\n" );
1962 fprintf( outfile
, "\tleal -4(%%ebp,%%edx), %%edx\n" );
1963 /* -4 since STACK16FRAME not yet complete! */
1965 /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
1966 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET
);
1967 fprintf( outfile
, "\tpushl %%ebp\n" );
1971 fprintf( outfile
,"\tdata16\n");
1973 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET
+ 2 );
1974 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET
);
1975 fprintf( outfile
, "\tpushl %%ds\n" );
1976 fprintf( outfile
, "\tpopl %%ss\n" );
1977 fprintf( outfile
, "\tmovl %%ebp, %%esp\n" );
1978 fprintf( outfile
, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME
, ebp
) );
1982 STACK16FRAME is completely set up
1983 DS, ES, SS: flat data segment
1985 ESP: points to last STACK32FRAME
1986 EBP: points to ebp member of last STACK32FRAME
1987 EDX: points to current STACK16FRAME
1988 ECX: points to ldt_copy
1989 all other registers: unchanged */
1991 /* Special case: C16ThkSL stub */
1994 /* Set up registers as expected and call thunk */
1995 fprintf( outfile
, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME
)-22 );
1996 fprintf( outfile
, "\tleal -4(%%esp), %%ebp\n" );
1998 fprintf( outfile
, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point
) );
2000 /* Switch stack back */
2001 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2002 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
2003 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2005 /* Restore registers and return directly to caller */
2006 fprintf( outfile
, "\taddl $8, %%esp\n" );
2007 fprintf( outfile
, "\tpopl %%ebp\n" );
2008 fprintf( outfile
, "\tpopw %%ds\n" );
2009 fprintf( outfile
, "\tpopw %%es\n" );
2010 fprintf( outfile
, "\tpopw %%fs\n" );
2011 fprintf( outfile
, "\tpopw %%gs\n" );
2012 fprintf( outfile
, "\taddl $18, %%esp\n" );
2014 fprintf( outfile
, "\txorb %%ch, %%ch\n" );
2015 fprintf( outfile
, "\tpopl %%ebx\n" );
2016 fprintf( outfile
, "\taddw %%cx, %%sp\n" );
2017 fprintf( outfile
, "\tpush %%ebx\n" );
2019 fprintf( outfile
, "\t.byte 0x66\n" );
2020 fprintf( outfile
, "\tlret\n" );
2026 /* Build register CONTEXT */
2029 fprintf( outfile
, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86
) );
2031 fprintf( outfile
, "\tpushfl\n" );
2032 fprintf( outfile
, "\tpopl %d(%%esp)\n", CONTEXTOFFSET(EFlags
) );
2034 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax
) );
2035 fprintf( outfile
, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx
) );
2036 fprintf( outfile
, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi
) );
2037 fprintf( outfile
, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi
) );
2039 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp
) );
2040 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp
) );
2041 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx
) );
2042 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx
) );
2043 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx
) );
2044 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx
) );
2046 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds
) );
2047 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs
) );
2048 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es
) );
2049 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs
) );
2050 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs
) );
2051 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs
) );
2052 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs
) );
2053 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs
) );
2055 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs
) );
2056 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs
) );
2057 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip
) );
2058 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip
) );
2060 fprintf( outfile
, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET
+2 );
2061 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs
) );
2062 fprintf( outfile
, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET
);
2063 fprintf( outfile
, "\taddl $%d, %%eax\n", STACK16OFFSET(ip
) );
2064 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp
) );
2066 fprintf( outfile
, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave
) );
2071 /* Print debug info before call */
2076 fprintf( outfile
, "\tpushl %%ebx\n" );
2078 /* Get Global Offset Table into %ebx (for PLT call) */
2079 fprintf( outfile
, "\tcall .LCallFrom16%s.getgot2\n", name
);
2080 fprintf( outfile
, ".LCallFrom16%s.getgot2:\n", name
);
2081 fprintf( outfile
, "\tpopl %%ebx\n" );
2082 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot2], %%ebx\n", name
);
2085 fprintf( outfile
, "\tpushl %%ecx\n" );
2086 fprintf( outfile
, "\tpushl %%edx\n" );
2088 fprintf( outfile
, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2089 sizeof(CONTEXT
) + STRUCTOFFSET(STACK32FRAME
, ebp
) );
2091 fprintf( outfile
, "\tpushl $0\n" );
2094 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16@PLT\n ");
2096 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16\n ");
2098 fprintf( outfile
, "\tpopl %%edx\n" );
2099 fprintf( outfile
, "\tpopl %%edx\n" );
2100 fprintf( outfile
, "\tpopl %%ecx\n" );
2103 fprintf( outfile
, "\tpopl %%ebx\n" );
2106 /* Call *Thunk* relay routine (which will call the API entry point) */
2107 fprintf( outfile
, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay
) );
2109 /* Print debug info after call */
2114 fprintf( outfile
, "\tpushl %%ebx\n" );
2116 /* Get Global Offset Table into %ebx (for PLT call) */
2117 fprintf( outfile
, "\tcall .LCallFrom16%s.getgot3\n", name
);
2118 fprintf( outfile
, ".LCallFrom16%s.getgot3:\n", name
);
2119 fprintf( outfile
, "\tpopl %%ebx\n" );
2120 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot3], %%ebx\n", name
);
2123 fprintf( outfile
, "\tpushl %%eax\n" );
2125 fprintf( outfile
, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2126 sizeof(CONTEXT
) + STRUCTOFFSET(STACK32FRAME
, ebp
) );
2128 fprintf( outfile
, "\tpushl $0\n" );
2131 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret@PLT\n ");
2133 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret\n ");
2135 fprintf( outfile
, "\tpopl %%eax\n" );
2136 fprintf( outfile
, "\tpopl %%eax\n" );
2139 fprintf( outfile
, "\tpopl %%ebx\n" );
2145 fprintf( outfile
, "\tmovl %%esp, %%ebx\n" );
2147 /* Switch stack back */
2148 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2149 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
2150 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2152 /* Restore all registers from CONTEXT */
2153 fprintf( outfile
, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs
) );
2154 fprintf( outfile
, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp
) );
2155 fprintf( outfile
, "\tleal 4(%%esp, %%eax), %%esp\n" );
2157 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs
) );
2158 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip
) );
2159 fprintf( outfile
, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags
) );
2160 fprintf( outfile
, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs
) );
2162 fprintf( outfile
, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs
) );
2163 fprintf( outfile
, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs
) );
2164 fprintf( outfile
, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs
) );
2166 fprintf( outfile
, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp
) );
2167 fprintf( outfile
, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi
) );
2168 fprintf( outfile
, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi
) );
2169 fprintf( outfile
, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax
) );
2170 fprintf( outfile
, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx
) );
2171 fprintf( outfile
, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx
) );
2172 fprintf( outfile
, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx
) );
2174 fprintf( outfile
, "\tpopl %%ds\n" );
2175 fprintf( outfile
, "\tpopfl\n" );
2176 fprintf( outfile
, "\t.byte 0x66\n" );
2177 fprintf( outfile
, "\tlret\n" );
2181 /* Switch stack back */
2182 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2183 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
2184 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2186 /* Set flags according to return value */
2187 fprintf( outfile
, "\torl %%eax, %%eax\n" );
2189 /* Restore registers and return to *ThunkRet* routine */
2190 fprintf( outfile
, "\tpopl %%edx\n" );
2191 fprintf( outfile
, "\tpopl %%ecx\n" );
2192 fprintf( outfile
, "\tpopl %%ebp\n" );
2193 fprintf( outfile
, "\tpopw %%ds\n" );
2194 fprintf( outfile
, "\tpopw %%es\n" );
2195 fprintf( outfile
, "\tpopw %%fs\n" );
2196 fprintf( outfile
, "\tpopw %%gs\n" );
2197 fprintf( outfile
, "\tret $14\n" );
2202 /*******************************************************************
2205 * This routine builds the core routines used in 32->16 thunks:
2206 * CallTo16Word, CallTo16Long, CallTo16RegisterShort, and
2207 * CallTo16RegisterLong.
2209 * CallTo16Word and CallTo16Long are used by the 32->16 glue code
2210 * as described above. The register functions can be called directly:
2212 * extern void CALLBACK CallTo16RegisterShort( const CONTEXT86 *context, int nb_args );
2213 * extern void CALLBACK CallTo16RegisterLong ( const CONTEXT86 *context, int nb_args );
2215 * They call to 16-bit code with all registers except SS:SP set up as specified
2216 * by the 'context' structure, and SS:SP set to point to the current 16-bit
2217 * stack, decremented by the value specified in the 'nb_args' argument.
2220 static void BuildCallTo16Core( FILE *outfile
, int short_ret
, int reg_func
)
2222 char *name
= reg_func
== 2 ? "RegisterLong" :
2223 reg_func
== 1 ? "RegisterShort" :
2224 short_ret
? "Word" : "Long";
2226 /* Function header */
2227 fprintf( outfile
, "\n\t.align 4\n" );
2229 fprintf( outfile
, ".stabs \"CallTo16%s:F1\",36,0,0," PREFIX
"CallTo16%s\n",
2232 fprintf( outfile
, "\t.globl " PREFIX
"CallTo16%s\n", name
);
2233 fprintf( outfile
, PREFIX
"CallTo16%s:\n", name
);
2235 /* No relay stub for 'register' functions */
2237 fprintf( outfile
, "\tpushl $0\n" );
2239 /* Function entry sequence */
2240 fprintf( outfile
, "\tpushl %%ebp\n" );
2241 fprintf( outfile
, "\tmovl %%esp, %%ebp\n" );
2243 /* Save the 32-bit registers */
2244 fprintf( outfile
, "\tpushl %%ebx\n" );
2245 fprintf( outfile
, "\tpushl %%ecx\n" );
2246 fprintf( outfile
, "\tpushl %%edx\n" );
2247 fprintf( outfile
, "\tpushl %%esi\n" );
2248 fprintf( outfile
, "\tpushl %%edi\n" );
2252 /* Get Global Offset Table into %ebx */
2253 fprintf( outfile
, "\tcall .LCallTo16%s.getgot1\n", name
);
2254 fprintf( outfile
, ".LCallTo16%s.getgot1:\n", name
);
2255 fprintf( outfile
, "\tpopl %%ebx\n" );
2256 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot1], %%ebx\n", name
);
2259 /* Move relay target address to %edi */
2262 fprintf( outfile
, "\tmovl 4(%%ebp), %%edi\n" );
2263 fprintf( outfile
, "\taddl $3, %%edi\n" );
2266 /* Enter Win16 Mutex */
2268 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_EnterWin16Lock@PLT\n" );
2270 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_EnterWin16Lock\n" );
2272 /* Print debugging info */
2275 /* Push number of arguments (from relay stub) */
2277 fprintf( outfile
, "\tpushl $-1\n" );
2279 fprintf( outfile
, "\tpushl -12(%%edi)\n" );
2281 /* Push the address of the first argument */
2282 fprintf( outfile
, "\tleal 12(%%ebp),%%eax\n" );
2283 fprintf( outfile
, "\tpushl %%eax\n" );
2286 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16@PLT\n" );
2288 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16\n" );
2290 fprintf( outfile
, "\tpopl %%eax\n" );
2291 fprintf( outfile
, "\tpopl %%eax\n" );
2294 /* Get return address */
2296 fprintf( outfile
, "\tmovl " PREFIX
"CallTo16_RetAddr@GOTOFF(%%ebx), %%ecx\n" );
2298 fprintf( outfile
, "\tmovl " PREFIX
"CallTo16_RetAddr, %%ecx\n" );
2300 /* Call the actual CallTo16 routine (simulate a lcall) */
2301 fprintf( outfile
, "\tpushl %%cs\n" );
2302 fprintf( outfile
, "\tcall .LCallTo16%s\n", name
);
2304 /* Convert and push return value */
2307 fprintf( outfile
, "\tmovzwl %%ax, %%eax\n" );
2308 fprintf( outfile
, "\tpushl %%eax\n" );
2310 else if ( reg_func
!= 2 )
2312 fprintf( outfile
, "\tshll $16,%%edx\n" );
2313 fprintf( outfile
, "\tmovw %%ax,%%dx\n" );
2314 fprintf( outfile
, "\tpushl %%edx\n" );
2317 fprintf( outfile
, "\tpushl %%eax\n" );
2321 /* Get Global Offset Table into %ebx (might have been overwritten) */
2322 fprintf( outfile
, "\tcall .LCallTo16%s.getgot2\n", name
);
2323 fprintf( outfile
, ".LCallTo16%s.getgot2:\n", name
);
2324 fprintf( outfile
, "\tpopl %%ebx\n" );
2325 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot2], %%ebx\n", name
);
2328 /* Print debugging info */
2332 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16Ret@PLT\n" );
2334 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16Ret\n" );
2337 /* Leave Win16 Mutex */
2339 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_LeaveWin16Lock@PLT\n" );
2341 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_LeaveWin16Lock\n" );
2343 /* Get return value */
2344 fprintf( outfile
, "\tpopl %%eax\n" );
2346 /* Restore the 32-bit registers */
2347 fprintf( outfile
, "\tpopl %%edi\n" );
2348 fprintf( outfile
, "\tpopl %%esi\n" );
2349 fprintf( outfile
, "\tpopl %%edx\n" );
2350 fprintf( outfile
, "\tpopl %%ecx\n" );
2351 fprintf( outfile
, "\tpopl %%ebx\n" );
2353 /* Function exit sequence */
2354 fprintf( outfile
, "\tpopl %%ebp\n" );
2357 fprintf( outfile
, "\tret\n" ); /* return to relay return stub */
2360 fprintf( outfile
, "\taddl $4, %%esp\n" );
2361 fprintf( outfile
, "\tret $8\n" );
2365 /* Start of the actual CallTo16 routine */
2367 fprintf( outfile
, ".LCallTo16%s:\n", name
);
2369 /* Complete STACK32FRAME */
2370 fprintf( outfile
, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET
);
2371 fprintf( outfile
, "\tmovl %%esp,%%edx\n" );
2373 /* Switch to the 16-bit stack */
2375 fprintf( outfile
,"\tdata16\n");
2377 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2);
2378 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET
);
2379 fprintf( outfile
, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET
);
2383 /* Add the specified offset to the new sp */
2384 fprintf( outfile
, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(args
)+4 );
2386 /* Push the return address
2387 * With sreg suffix, we push 16:16 address (normal lret)
2388 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
2391 fprintf( outfile
, "\tpushl %%ecx\n" );
2394 fprintf( outfile
, "\tshldl $16, %%ecx, %%eax\n" );
2395 fprintf( outfile
, "\tpushw $0\n" );
2396 fprintf( outfile
, "\tpushw %%ax\n" );
2397 fprintf( outfile
, "\tpushw $0\n" );
2398 fprintf( outfile
, "\tpushw %%cx\n" );
2401 /* Push the called routine address */
2402 fprintf( outfile
, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(args
) );
2403 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs
) );
2404 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip
) );
2406 /* Get the registers */
2407 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs
) );
2408 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs
) );
2409 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2410 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs
) );
2411 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2412 fprintf( outfile
, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp
) );
2413 fprintf( outfile
, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi
) );
2414 fprintf( outfile
, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi
) );
2415 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax
) );
2416 fprintf( outfile
, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx
) );
2417 fprintf( outfile
, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx
) );
2418 fprintf( outfile
, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx
) );
2420 /* Get the 16-bit ds */
2421 fprintf( outfile
, "\tpopw %%ds\n" );
2423 /* Jump to the called routine */
2424 fprintf( outfile
, "\t.byte 0x66\n" );
2425 fprintf( outfile
, "\tlret\n" );
2427 else /* not a register function */
2429 /* Make %bp point to the previous stackframe (built by CallFrom16) */
2430 fprintf( outfile
, "\tmovzwl %%sp,%%ebp\n" );
2431 fprintf( outfile
, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp
) );
2433 /* Set %fs to the value saved by the last CallFrom16 */
2434 fprintf( outfile
, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs
)-STACK16OFFSET(bp
) );
2435 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2437 /* Jump to the relay code */
2438 fprintf( outfile
, "\tjmp *%%edi\n" );
2442 /*******************************************************************
2445 * Build the return code for 16-bit callbacks
2447 static void BuildRet16Func( FILE *outfile
)
2450 * Note: This must reside in the .data section to allow
2451 * run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
2454 fprintf( outfile
, "\n\t.globl " PREFIX
"CallTo16_Ret\n" );
2455 fprintf( outfile
, PREFIX
"CallTo16_Ret:\n" );
2457 /* Restore 32-bit segment registers */
2459 fprintf( outfile
, "\tmovw $0x%04x,%%bx\n", Data_Selector
);
2461 fprintf( outfile
, "\tdata16\n");
2463 fprintf( outfile
, "\tmovw %%bx,%%ds\n" );
2465 fprintf( outfile
, "\tdata16\n");
2467 fprintf( outfile
, "\tmovw %%bx,%%es\n" );
2469 fprintf( outfile
, "\tmovw " PREFIX
"SYSLEVEL_Win16CurrentTeb,%%fs\n" );
2471 /* Restore the 32-bit stack */
2474 fprintf( outfile
, "\tdata16\n");
2476 fprintf( outfile
, "\tmovw %%bx,%%ss\n" );
2477 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET
);
2478 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2480 /* Return to caller */
2482 fprintf( outfile
, "\tlret\n" );
2484 /* Declare the return address variable */
2486 fprintf( outfile
, "\n\t.globl " PREFIX
"CallTo16_RetAddr\n" );
2487 fprintf( outfile
, PREFIX
"CallTo16_RetAddr:\t.long 0\n" );
2490 /*******************************************************************
2491 * BuildCallTo32CBClient
2493 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
2495 * Since the relay stub is itself 32-bit, this should not be a problem;
2496 * unfortunately, the relay stubs are expected to switch back to a
2497 * 16-bit stack (and 16-bit code) after completion :-(
2499 * This would conflict with our 16- vs. 32-bit stack handling, so
2500 * we simply switch *back* to our 32-bit stack before returning to
2503 * The CBClient relay stub expects to be called with the following
2504 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
2505 * stack at the designated places:
2508 * (ebp+14) original arguments to the callback routine
2509 * (ebp+10) far return address to original caller
2510 * (ebp+6) Thunklet target address
2511 * (ebp+2) Thunklet relay ID code
2512 * (ebp) BP (saved by CBClientGlueSL)
2513 * (ebp-2) SI (saved by CBClientGlueSL)
2514 * (ebp-4) DI (saved by CBClientGlueSL)
2515 * (ebp-6) DS (saved by CBClientGlueSL)
2517 * ... buffer space used by the 16-bit side glue for temp copies
2519 * (ebx+4) far return address to 16-bit side glue code
2520 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
2522 * The 32-bit side glue code accesses both the original arguments (via ebp)
2523 * and the temporary copies prepared by the 16-bit side glue (via ebx).
2524 * After completion, the stub will load ss:sp from the buffer at ebx
2525 * and perform a far return to 16-bit code.
2527 * To trick the relay stub into returning to us, we replace the 16-bit
2528 * return address to the glue code by a cs:ip pair pointing to our
2529 * return entry point (the original return address is saved first).
2530 * Our return stub thus called will then reload the 32-bit ss:esp and
2531 * return to 32-bit code (by using and ss:esp value that we have also
2532 * pushed onto the 16-bit stack before and a cs:eip values found at
2533 * that position on the 32-bit stack). The ss:esp to be restored is
2534 * found relative to the 16-bit stack pointer at:
2537 * (ebx-8) sp (32-bit stack pointer)
2539 * The second variant of this routine, CALL32_CBClientEx, which is used
2540 * to implement KERNEL.621, has to cope with yet another problem: Here,
2541 * the 32-bit side directly returns to the caller of the CBClient thunklet,
2542 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
2543 * As we have to return to our 32-bit code first, we have to adapt the
2544 * layout of our temporary area so as to include values for the registers
2545 * that are to be restored, and later (in the implementation of KERNEL.621)
2546 * we *really* restore them. The return stub restores DS, DI, SI, and BP
2547 * from the stack, skips the next 8 bytes (CBClient relay code / target),
2548 * and then performs a lret NN, where NN is the number of arguments to be
2549 * removed. Thus, we prepare our temporary area as follows:
2551 * (ebx+22) 16-bit cs (this segment)
2552 * (ebx+20) 16-bit ip ('16-bit' return entry point)
2553 * (ebx+16) 32-bit ss (flat)
2554 * (ebx+12) 32-bit sp (32-bit stack pointer)
2555 * (ebx+10) 16-bit bp (points to ebx+24)
2556 * (ebx+8) 16-bit si (ignored)
2557 * (ebx+6) 16-bit di (ignored)
2558 * (ebx+4) 16-bit ds (we actually use the flat DS here)
2559 * (ebx+2) 16-bit ss (16-bit stack segment)
2560 * (ebx+0) 16-bit sp (points to ebx+4)
2562 * Note that we ensure that DS is not changed and remains the flat segment,
2563 * and the 32-bit stack pointer our own return stub needs fits just
2564 * perfectly into the 8 bytes that are skipped by the Windows stub.
2565 * One problem is that we have to determine the number of removed arguments,
2566 * as these have to be really removed in KERNEL.621. Thus, the BP value
2567 * that we place in the temporary area to be restored, contains the value
2568 * that SP would have if no arguments were removed. By comparing the actual
2569 * value of SP with this value in our return stub we can compute the number
2570 * of removed arguments. This is then returned to KERNEL.621.
2572 * The stack layout of this function:
2573 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
2574 * (ebp+16) esi pointer to caller's esi value
2575 * (ebp+12) arg ebp value to be set for relay stub
2576 * (ebp+8) func CBClient relay stub address
2580 static void BuildCallTo32CBClient( FILE *outfile
, BOOL isEx
)
2582 char *name
= isEx
? "CBClientEx" : "CBClient";
2583 int size
= isEx
? 24 : 12;
2585 /* Function header */
2587 fprintf( outfile
, "\n\t.align 4\n" );
2589 fprintf( outfile
, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX
"CALL32_%s\n",
2592 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_%s\n", name
);
2593 fprintf( outfile
, PREFIX
"CALL32_%s:\n", name
);
2597 fprintf( outfile
, "\tpushl %%ebp\n" );
2598 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2599 fprintf( outfile
, "\tpushl %%edi\n" );
2600 fprintf( outfile
, "\tpushl %%esi\n" );
2601 fprintf( outfile
, "\tpushl %%ebx\n" );
2603 /* Get the 16-bit stack */
2605 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET
);
2607 /* Convert it to a flat address */
2609 fprintf( outfile
, "\tshldl $16,%%ebx,%%eax\n" );
2610 fprintf( outfile
, "\tandl $0xfff8,%%eax\n" );
2611 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy(%%eax),%%esi\n" );
2612 fprintf( outfile
, "\tmovw %%bx,%%ax\n" );
2613 fprintf( outfile
, "\taddl %%eax,%%esi\n" );
2615 /* Allocate temporary area (simulate STACK16_PUSH) */
2617 fprintf( outfile
, "\tpushf\n" );
2618 fprintf( outfile
, "\tcld\n" );
2619 fprintf( outfile
, "\tleal -%d(%%esi), %%edi\n", size
);
2620 fprintf( outfile
, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME
) );
2621 fprintf( outfile
, "\trep\n\tmovsb\n" );
2622 fprintf( outfile
, "\tpopf\n" );
2624 fprintf( outfile
, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size
, STACKOFFSET
);
2626 fprintf( outfile
, "\tpushl %%edi\n" ); /* remember address */
2628 /* Set up temporary area */
2632 fprintf( outfile
, "\tleal 4(%%edi), %%edi\n" );
2634 fprintf( outfile
, "\tleal -8(%%esp), %%eax\n" );
2635 fprintf( outfile
, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */
2637 fprintf( outfile
, "\tmovl %%ss, %%ax\n" );
2638 fprintf( outfile
, "\tandl $0x0000ffff, %%eax\n" );
2639 fprintf( outfile
, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */
2641 fprintf( outfile
, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME
)-size
+4 + 4 );
2642 fprintf( outfile
, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */
2644 fprintf( outfile
, "\tmovl " PREFIX
"CALL32_%s_RetAddr, %%eax\n", name
);
2645 fprintf( outfile
, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */
2649 fprintf( outfile
, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME
)-size
+4 );
2650 fprintf( outfile
, "\tmovl %%ebx, 0(%%edi)\n" );
2652 fprintf( outfile
, "\tmovl %%ds, %%ax\n" );
2653 fprintf( outfile
, "\tmovw %%ax, 4(%%edi)\n" );
2655 fprintf( outfile
, "\taddl $20, %%ebx\n" );
2656 fprintf( outfile
, "\tmovw %%bx, 10(%%edi)\n" );
2658 fprintf( outfile
, "\tleal -8(%%esp), %%eax\n" );
2659 fprintf( outfile
, "\tmovl %%eax, 12(%%edi)\n" );
2661 fprintf( outfile
, "\tmovl %%ss, %%ax\n" );
2662 fprintf( outfile
, "\tandl $0x0000ffff, %%eax\n" );
2663 fprintf( outfile
, "\tmovl %%eax, 16(%%edi)\n" );
2665 fprintf( outfile
, "\tmovl " PREFIX
"CALL32_%s_RetAddr, %%eax\n", name
);
2666 fprintf( outfile
, "\tmovl %%eax, 20(%%edi)\n" );
2669 /* Set up registers and call CBClient relay stub (simulating a far call) */
2671 fprintf( outfile
, "\tmovl 16(%%ebp), %%esi\n" );
2672 fprintf( outfile
, "\tmovl (%%esi), %%esi\n" );
2674 fprintf( outfile
, "\tmovl %%edi, %%ebx\n" );
2675 fprintf( outfile
, "\tmovl 8(%%ebp), %%eax\n" );
2676 fprintf( outfile
, "\tmovl 12(%%ebp), %%ebp\n" );
2678 fprintf( outfile
, "\tpushl %%cs\n" );
2679 fprintf( outfile
, "\tcall *%%eax\n" );
2681 /* Return new esi value to caller */
2683 fprintf( outfile
, "\tmovl 32(%%esp), %%edi\n" );
2684 fprintf( outfile
, "\tmovl %%esi, (%%edi)\n" );
2686 /* Cleanup temporary area (simulate STACK16_POP) */
2688 fprintf( outfile
, "\tpop %%esi\n" );
2690 fprintf( outfile
, "\tpushf\n" );
2691 fprintf( outfile
, "\tstd\n" );
2692 fprintf( outfile
, "\tdec %%esi\n" );
2693 fprintf( outfile
, "\tleal %d(%%esi), %%edi\n", size
);
2694 fprintf( outfile
, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME
) );
2695 fprintf( outfile
, "\trep\n\tmovsb\n" );
2696 fprintf( outfile
, "\tpopf\n" );
2698 fprintf( outfile
, "\t.byte 0x64\n\taddw $%d,(%d)\n", size
, STACKOFFSET
);
2700 /* Return argument size to caller */
2703 fprintf( outfile
, "\tmovl 32(%%esp), %%ebx\n" );
2704 fprintf( outfile
, "\tmovl %%ebp, (%%ebx)\n" );
2707 /* Restore registers and return */
2709 fprintf( outfile
, "\tpopl %%ebx\n" );
2710 fprintf( outfile
, "\tpopl %%esi\n" );
2711 fprintf( outfile
, "\tpopl %%edi\n" );
2712 fprintf( outfile
, "\tpopl %%ebp\n" );
2713 fprintf( outfile
, "\tret\n" );
2716 static void BuildCallTo32CBClientRet( FILE *outfile
, BOOL isEx
)
2718 char *name
= isEx
? "CBClientEx" : "CBClient";
2720 /* '16-bit' return stub */
2722 fprintf( outfile
, "\n\t.globl " PREFIX
"CALL32_%s_Ret\n", name
);
2723 fprintf( outfile
, PREFIX
"CALL32_%s_Ret:\n", name
);
2727 fprintf( outfile
, "\tmovzwl %%sp, %%ebx\n" );
2728 fprintf( outfile
, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
2732 fprintf( outfile
, "\tmovzwl %%bp, %%ebx\n" );
2733 fprintf( outfile
, "\tsubw %%bp, %%sp\n" );
2734 fprintf( outfile
, "\tmovzwl %%sp, %%ebp\n" );
2735 fprintf( outfile
, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
2737 fprintf( outfile
, "\tlret\n" );
2739 /* Declare the return address variable */
2741 fprintf( outfile
, "\n\t.globl " PREFIX
"CALL32_%s_RetAddr\n", name
);
2742 fprintf( outfile
, PREFIX
"CALL32_%s_RetAddr:\t.long 0\n", name
);
2746 /*******************************************************************
2747 * BuildCallTo32LargeStack
2749 * Build the function used to switch to the original 32-bit stack
2750 * before calling a 32-bit function from 32-bit code. This is used for
2751 * functions that need a large stack, like X bitmaps functions.
2753 * The generated function has the following prototype:
2754 * int xxx( int (*func)(), void *arg );
2756 * The pointer to the function can be retrieved by calling CALL32_Init,
2757 * which also takes care of saving the current 32-bit stack pointer.
2758 * Furthermore, CALL32_Init switches to a new stack and jumps to the
2759 * specified target address.
2761 * NOTE: The CALL32_LargeStack routine may be recursively entered by the
2762 * same thread, but not concurrently entered by several threads.
2764 * Stack layout of CALL32_Init:
2766 * (esp+12) new stack address
2767 * (esp+8) target address
2768 * (esp+4) pointer to variable to receive CALL32_LargeStack address
2771 * Stack layout of CALL32_LargeStack:
2778 static void BuildCallTo32LargeStack( FILE *outfile
)
2780 /* Initialization function */
2782 fprintf( outfile
, "\n\t.align 4\n" );
2784 fprintf( outfile
, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX
"CALL32_Init\n");
2786 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Init\n" );
2787 fprintf( outfile
, "\t.type " PREFIX
"CALL32_Init,@function\n" );
2788 fprintf( outfile
, PREFIX
"CALL32_Init:\n" );
2789 fprintf( outfile
, "\tmovl %%esp,CALL32_Original32_esp\n" );
2790 fprintf( outfile
, "\tpopl %%eax\n" );
2791 fprintf( outfile
, "\tpopl %%eax\n" );
2792 fprintf( outfile
, "\tmovl $CALL32_LargeStack,(%%eax)\n" );
2793 fprintf( outfile
, "\tpopl %%eax\n" );
2794 fprintf( outfile
, "\tpopl %%esp\n" );
2795 fprintf( outfile
, "\tpushl %%eax\n" );
2796 fprintf( outfile
, "\tret\n" );
2798 /* Function header */
2800 fprintf( outfile
, "\n\t.align 4\n" );
2802 fprintf( outfile
, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2804 fprintf( outfile
, "CALL32_LargeStack:\n" );
2808 fprintf( outfile
, "\tpushl %%ebp\n" );
2809 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2811 /* Switch to the original 32-bit stack pointer */
2813 fprintf( outfile
, "\tcmpl $0, CALL32_RecursionCount\n" );
2814 fprintf( outfile
, "\tjne CALL32_skip\n" );
2815 fprintf( outfile
, "\tmovl CALL32_Original32_esp, %%esp\n" );
2816 fprintf( outfile
, "CALL32_skip:\n" );
2818 fprintf( outfile
, "\tincl CALL32_RecursionCount\n" );
2820 /* Transfer the argument and call the function */
2822 fprintf( outfile
, "\tpushl 12(%%ebp)\n" );
2823 fprintf( outfile
, "\tcall *8(%%ebp)\n" );
2825 /* Restore registers and return */
2827 fprintf( outfile
, "\tdecl CALL32_RecursionCount\n" );
2829 fprintf( outfile
, "\tmovl %%ebp,%%esp\n" );
2830 fprintf( outfile
, "\tpopl %%ebp\n" );
2831 fprintf( outfile
, "\tret\n" );
2835 fprintf( outfile
, "\t.data\n" );
2836 fprintf( outfile
, "CALL32_Original32_esp:\t.long 0\n" );
2837 fprintf( outfile
, "CALL32_RecursionCount:\t.long 0\n" );
2838 fprintf( outfile
, "\t.text\n" );
2842 /*******************************************************************
2843 * BuildCallFrom32Regs
2845 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2846 * 'args' is the number of dword arguments.
2850 * (ebp+12) first arg
2851 * (ebp+8) ret addr to user code
2852 * (ebp+4) ret addr to relay code
2854 * (ebp-128) buffer area to allow stack frame manipulation
2855 * (ebp-332) CONTEXT86 struct
2856 * (ebp-336) CONTEXT86 *argument
2857 * .... other arguments copied from (ebp+12)
2859 * The entry point routine is called with a CONTEXT* extra argument,
2860 * following the normal args. In this context structure, EIP_reg
2861 * contains the return address to user code, and ESP_reg the stack
2862 * pointer on return (with the return address and arguments already
2865 static void BuildCallFrom32Regs( FILE *outfile
)
2867 static const int STACK_SPACE
= 128 + sizeof(CONTEXT86
);
2869 /* Function header */
2871 fprintf( outfile
, "\n\t.align 4\n" );
2873 fprintf( outfile
, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX
"CALL32_Regs\n" );
2875 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Regs\n" );
2876 fprintf( outfile
, PREFIX
"CALL32_Regs:\n" );
2878 /* Allocate some buffer space on the stack */
2880 fprintf( outfile
, "\tpushl %%ebp\n" );
2881 fprintf( outfile
, "\tmovl %%esp,%%ebp\n ");
2882 fprintf( outfile
, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE
);
2884 /* Build the context structure */
2886 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax
) - STACK_SPACE
);
2887 fprintf( outfile
, "\tpushfl\n" );
2888 fprintf( outfile
, "\tpopl %%eax\n" );
2889 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags
) - STACK_SPACE
);
2890 fprintf( outfile
, "\tmovl 0(%%ebp),%%eax\n" );
2891 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp
) - STACK_SPACE
);
2892 fprintf( outfile
, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx
) - STACK_SPACE
);
2893 fprintf( outfile
, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx
) - STACK_SPACE
);
2894 fprintf( outfile
, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx
) - STACK_SPACE
);
2895 fprintf( outfile
, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi
) - STACK_SPACE
);
2896 fprintf( outfile
, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi
) - STACK_SPACE
);
2898 fprintf( outfile
, "\txorl %%eax,%%eax\n" );
2899 fprintf( outfile
, "\tmovw %%cs,%%ax\n" );
2900 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs
) - STACK_SPACE
);
2901 fprintf( outfile
, "\tmovw %%es,%%ax\n" );
2902 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs
) - STACK_SPACE
);
2903 fprintf( outfile
, "\tmovw %%fs,%%ax\n" );
2904 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs
) - STACK_SPACE
);
2905 fprintf( outfile
, "\tmovw %%gs,%%ax\n" );
2906 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs
) - STACK_SPACE
);
2907 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
2908 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs
) - STACK_SPACE
);
2909 fprintf( outfile
, "\tmovw %%ds,%%ax\n" );
2910 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs
) - STACK_SPACE
);
2911 fprintf( outfile
, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
2913 fprintf( outfile
, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL
);
2914 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags
) - STACK_SPACE
);
2916 fprintf( outfile
, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
2917 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip
) - STACK_SPACE
);
2919 /* Transfer the arguments */
2921 fprintf( outfile
, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
2922 fprintf( outfile
, "\tpushl %%esp\n" ); /* push ptr to context struct */
2923 fprintf( outfile
, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
2924 fprintf( outfile
, "\tjecxz 1f\n" );
2925 fprintf( outfile
, "\tsubl %%ecx,%%esp\n" );
2926 fprintf( outfile
, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */
2927 fprintf( outfile
, "\tmovl %%esp,%%edi\n" );
2928 fprintf( outfile
, "\tshrl $2,%%ecx\n" );
2929 fprintf( outfile
, "\tcld\n" );
2930 fprintf( outfile
, "\trep\n\tmovsl\n" ); /* copy args */
2932 fprintf( outfile
, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
2933 fprintf( outfile
, "\tleal 12(%%ebp,%%eax),%%eax\n" );
2934 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2936 /* Call the entry point */
2938 fprintf( outfile
, "\tcall *0(%%ebx)\n" );
2940 /* Store %eip and %ebp onto the new stack */
2942 fprintf( outfile
, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2943 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip
) - STACK_SPACE
);
2944 fprintf( outfile
, "\tmovl %%eax,-4(%%edx)\n" );
2945 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp
) - STACK_SPACE
);
2946 fprintf( outfile
, "\tmovl %%eax,-8(%%edx)\n" );
2948 /* Restore the context structure */
2950 /* Note: we don't bother to restore %cs, %ds and %ss
2951 * changing them in 32-bit code is a recipe for disaster anyway
2953 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs
) - STACK_SPACE
);
2954 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2955 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs
) - STACK_SPACE
);
2956 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2957 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs
) - STACK_SPACE
);
2958 fprintf( outfile
, "\tmovw %%ax,%%gs\n" );
2960 fprintf( outfile
, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi
) - STACK_SPACE
);
2961 fprintf( outfile
, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi
) - STACK_SPACE
);
2962 fprintf( outfile
, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx
) - STACK_SPACE
);
2963 fprintf( outfile
, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx
) - STACK_SPACE
);
2964 fprintf( outfile
, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx
) - STACK_SPACE
);
2966 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags
) - STACK_SPACE
);
2967 fprintf( outfile
, "\tpushl %%eax\n" );
2968 fprintf( outfile
, "\tpopfl\n" );
2969 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax
) - STACK_SPACE
);
2971 fprintf( outfile
, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2972 fprintf( outfile
, "\tleal -8(%%ebp),%%esp\n" );
2973 fprintf( outfile
, "\tpopl %%ebp\n" );
2974 fprintf( outfile
, "\tret\n" );
2978 /*******************************************************************
2981 * Build the spec files
2983 static int BuildSpec( FILE *outfile
, int argc
, char *argv
[] )
2986 for (i
= 2; i
< argc
; i
++)
2987 if (BuildSpecFile( outfile
, argv
[i
] ) < 0) return -1;
2991 /*******************************************************************
2994 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
2996 static int BuildGlue( FILE *outfile
, char * outname
, int argc
, char *argv
[] )
3003 infile
= fopen( argv
[2], "r" );
3010 else infile
= stdin
;
3014 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
3015 fprintf( outfile
, "\t.text\n" );
3020 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
3021 getcwd(buffer
, sizeof(buffer
));
3024 * The stabs help the internal debugger as they are an indication that it
3025 * is sensible to step into a thunk/trampoline.
3027 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
3028 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
3029 fprintf( outfile
, "\t.text\n" );
3030 fprintf( outfile
, "\t.align 4\n" );
3031 fprintf( outfile
, "Code_Start:\n\n" );
3034 /* Build the callback glue functions */
3036 while (fgets( buffer
, sizeof(buffer
), infile
))
3038 if (strstr( buffer
, "### start build ###" )) break;
3040 while (fgets( buffer
, sizeof(buffer
), infile
))
3043 if ( (p
= strstr( buffer
, "CallFrom16_" )) != NULL
)
3045 char *q
, *profile
= p
+ strlen( "CallFrom16_" );
3046 for (q
= profile
; (*q
== '_') || isalpha(*q
); q
++ )
3049 for (q
= p
-1; q
> buffer
&& ((*q
== '_') || isalnum(*q
)); q
-- )
3051 if ( ++q
< p
) p
[-1] = '\0'; else q
= "";
3052 BuildCallFrom16Func( outfile
, profile
, q
);
3054 if ( (p
= strstr( buffer
, "CallTo16_" )) != NULL
)
3056 char *q
, *profile
= p
+ strlen( "CallTo16_" );
3057 for (q
= profile
; (*q
== '_') || isalpha(*q
); q
++ )
3060 for (q
= p
-1; q
> buffer
&& ((*q
== '_') || isalnum(*q
)); q
-- )
3062 if ( ++q
< p
) p
[-1] = '\0'; else q
= "";
3063 BuildCallTo16Func( outfile
, profile
, q
);
3065 if (strstr( buffer
, "### stop build ###" )) break;
3070 fprintf( outfile
, "\t.text\n");
3071 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
3072 fprintf( outfile
, ".Letext:\n");
3075 #else /* __i386__ */
3077 /* Just to avoid an empty file */
3078 fprintf( outfile
, "\t.long 0\n" );
3080 #endif /* __i386__ */
3086 /*******************************************************************
3089 * Build the 16-bit callbacks
3091 static int BuildCall16( FILE *outfile
, char * outname
)
3099 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
3100 fprintf( outfile
, "\t.text\n" );
3105 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
3106 getcwd(buffer
, sizeof(buffer
));
3109 * The stabs help the internal debugger as they are an indication that it
3110 * is sensible to step into a thunk/trampoline.
3112 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
3113 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
3114 fprintf( outfile
, "\t.text\n" );
3115 fprintf( outfile
, "\t.align 4\n" );
3116 fprintf( outfile
, "Code_Start:\n\n" );
3118 fprintf( outfile
, PREFIX
"Call16_Start:\n" );
3119 fprintf( outfile
, "\t.globl "PREFIX
"Call16_Start\n" );
3120 fprintf( outfile
, "\t.byte 0\n\n" );
3123 /* Standard CallFrom16 routine */
3124 BuildCallFrom16Core( outfile
, FALSE
, FALSE
);
3126 /* Register CallFrom16 routine */
3127 BuildCallFrom16Core( outfile
, TRUE
, FALSE
);
3129 /* C16ThkSL CallFrom16 routine */
3130 BuildCallFrom16Core( outfile
, FALSE
, TRUE
);
3132 /* Standard CallTo16 routine (WORD return) */
3133 BuildCallTo16Core( outfile
, TRUE
, FALSE
);
3135 /* Standard CallTo16 routine (DWORD return) */
3136 BuildCallTo16Core( outfile
, FALSE
, FALSE
);
3138 /* Register CallTo16 routine (16:16 retf) */
3139 BuildCallTo16Core( outfile
, FALSE
, 1 );
3141 /* Register CallTo16 routine (16:32 retf) */
3142 BuildCallTo16Core( outfile
, FALSE
, 2 );
3144 /* CBClientThunkSL routine */
3145 BuildCallTo32CBClient( outfile
, FALSE
);
3147 /* CBClientThunkSLEx routine */
3148 BuildCallTo32CBClient( outfile
, TRUE
);
3150 fprintf( outfile
, PREFIX
"Call16_End:\n" );
3151 fprintf( outfile
, "\t.globl "PREFIX
"Call16_End\n" );
3154 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
3155 fprintf( outfile
, ".Letext:\n");
3158 /* The whole Call16_Ret segment must lie within the .data section */
3159 fprintf( outfile
, "\n\t.data\n" );
3160 fprintf( outfile
, "\t.globl " PREFIX
"Call16_Ret_Start\n" );
3161 fprintf( outfile
, PREFIX
"Call16_Ret_Start:\n" );
3163 /* Standard CallTo16 return stub */
3164 BuildRet16Func( outfile
);
3166 /* CBClientThunkSL return stub */
3167 BuildCallTo32CBClientRet( outfile
, FALSE
);
3169 /* CBClientThunkSLEx return stub */
3170 BuildCallTo32CBClientRet( outfile
, TRUE
);
3172 /* End of Call16_Ret segment */
3173 fprintf( outfile
, "\n\t.globl " PREFIX
"Call16_Ret_End\n" );
3174 fprintf( outfile
, PREFIX
"Call16_Ret_End:\n" );
3176 #else /* __i386__ */
3178 fprintf( outfile
, PREFIX
"Call16_Start:\n" );
3179 fprintf( outfile
, "\t.globl "PREFIX
"Call16_Start\n" );
3180 fprintf( outfile
, "\t.byte 0\n\n" );
3181 fprintf( outfile
, PREFIX
"Call16_End:\n" );
3182 fprintf( outfile
, "\t.globl "PREFIX
"Call16_End\n" );
3184 fprintf( outfile
, "\t.globl " PREFIX
"Call16_Ret_Start\n" );
3185 fprintf( outfile
, PREFIX
"Call16_Ret_Start:\n" );
3186 fprintf( outfile
, "\t.byte 0\n\n" );
3187 fprintf( outfile
, "\n\t.globl " PREFIX
"Call16_Ret_End\n" );
3188 fprintf( outfile
, PREFIX
"Call16_Ret_End:\n" );
3190 #endif /* __i386__ */
3195 /*******************************************************************
3198 * Build the 32-bit callbacks
3200 static int BuildCall32( FILE *outfile
, char * outname
)
3208 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
3209 fprintf( outfile
, "\t.text\n" );
3214 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
3215 getcwd(buffer
, sizeof(buffer
));
3218 * The stabs help the internal debugger as they are an indication that it
3219 * is sensible to step into a thunk/trampoline.
3221 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
3222 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
3223 fprintf( outfile
, "\t.text\n" );
3224 fprintf( outfile
, "\t.align 4\n" );
3225 fprintf( outfile
, "Code_Start:\n" );
3228 /* Build the 32-bit large stack callback */
3230 BuildCallTo32LargeStack( outfile
);
3232 /* Build the register callback function */
3234 BuildCallFrom32Regs( outfile
);
3237 fprintf( outfile
, "\t.text\n");
3238 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
3239 fprintf( outfile
, ".Letext:\n");
3242 #else /* __i386__ */
3244 /* Just to avoid an empty file */
3245 fprintf( outfile
, "\t.long 0\n" );
3247 #endif /* __i386__ */
3252 /*******************************************************************
3255 static void usage(void)
3258 "usage: build [-pic] [-o outfile] -spec SPECNAMES\n"
3259 " build [-pic] [-o outfile] -glue SOURCE_FILE\n"
3260 " build [-pic] [-o outfile] -call16\n"
3261 " build [-pic] [-o outfile] -call32\n" );
3266 /*******************************************************************
3269 int main(int argc
, char **argv
)
3271 char *outname
= NULL
;
3272 FILE *outfile
= stdout
;
3275 if (argc
< 2) usage();
3277 if (!strcmp( argv
[1], "-pic" ))
3282 if (argc
< 2) usage();
3285 if (!strcmp( argv
[1], "-o" ))
3290 if (argc
< 2) usage();
3291 if (!(outfile
= fopen( outname
, "w" )))
3293 fprintf( stderr
, "Unable to create output file '%s'\n", outname
);
3298 /* Retrieve the selector values; this assumes that we are building
3299 * the asm files on the platform that will also run them. Probably
3300 * a safe assumption to make.
3302 GET_CS( Code_Selector
);
3303 GET_DS( Data_Selector
);
3305 if (!strcmp( argv
[1], "-spec" ))
3306 res
= BuildSpec( outfile
, argc
, argv
);
3307 else if (!strcmp( argv
[1], "-glue" ))
3308 res
= BuildGlue( outfile
, outname
, argc
, argv
);
3309 else if (!strcmp( argv
[1], "-call16" ))
3310 res
= BuildCall16( outfile
, outname
);
3311 else if (!strcmp( argv
[1], "-call32" ))
3312 res
= BuildCall32( outfile
, outname
);