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_STUB
, /* unimplemented stub */
54 TYPE_STDCALL
, /* stdcall function (Win32) */
55 TYPE_CDECL
, /* cdecl function (Win32) */
56 TYPE_VARARGS
, /* varargs function (Win32) */
57 TYPE_EXTERN
, /* external symbol (Win32) */
58 TYPE_FORWARD
, /* forwarded function (Win32) */
62 static const char * const TypeNames
[TYPE_NBTYPES
] =
65 "byte", /* TYPE_BYTE */
66 "word", /* TYPE_WORD */
67 "long", /* TYPE_LONG */
68 "pascal16", /* TYPE_PASCAL_16 */
69 "pascal", /* TYPE_PASCAL */
70 "equate", /* TYPE_ABS */
71 "register", /* TYPE_REGISTER */
72 "stub", /* TYPE_STUB */
73 "stdcall", /* TYPE_STDCALL */
74 "cdecl", /* TYPE_CDECL */
75 "varargs", /* TYPE_VARARGS */
76 "extern", /* TYPE_EXTERN */
77 "forward" /* TYPE_FORWARD */
80 #define MAX_ORDINALS 2048
81 #define MAX_IMPORTS 16
83 /* Callback function used for stub functions */
84 #define STUB_CALLBACK \
85 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
151 static ORDDEF OrdinalDefinitions
[MAX_ORDINALS
];
153 static SPEC_TYPE SpecType
= SPEC_INVALID
;
154 static char DLLName
[80];
155 static char DLLFileName
[80];
156 static int Limit
= 0;
157 static int Base
= MAX_ORDINALS
;
158 static int DLLHeapSize
= 0;
159 static char *SpecName
;
161 static WORD Code_Selector
, Data_Selector
;
162 static char DLLInitFunc
[80];
163 static char *DLLImports
[MAX_IMPORTS
];
164 static int nb_imports
= 0;
166 char *ParseBuffer
= NULL
;
171 static int UsePIC
= 0;
173 static int debugging
= 1;
175 /* Offset of a structure field relative to the start of the struct */
176 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
178 /* Offset of register relative to the start of the CONTEXT struct */
179 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT86,reg)
181 /* Offset of register relative to the start of the STACK16FRAME struct */
182 #define STACK16OFFSET(reg) STRUCTOFFSET(STACK16FRAME,reg)
184 /* Offset of register relative to the start of the STACK32FRAME struct */
185 #define STACK32OFFSET(reg) STRUCTOFFSET(STACK32FRAME,reg)
187 /* Offset of the stack pointer relative to %fs:(0) */
188 #define STACKOFFSET (STRUCTOFFSET(TEB,cur_stack))
191 static void *xmalloc (size_t size
)
195 res
= malloc (size
? size
: 1);
198 fprintf (stderr
, "Virtual memory exhausted.\n");
205 static void *xrealloc (void *ptr
, size_t size
)
207 void *res
= realloc (ptr
, size
);
210 fprintf (stderr
, "Virtual memory exhausted.\n");
216 static char *xstrdup( const char *str
)
218 char *res
= strdup( str
);
221 fprintf (stderr
, "Virtual memory exhausted.\n");
227 static int IsNumberString(char *s
)
236 static char *strupper(char *s
)
240 for(p
= s
; *p
!= '\0'; p
++)
246 static char * GetTokenInLine(void)
251 if (ParseNext
!= ParseBuffer
)
253 if (ParseSaveChar
== '\0')
255 *ParseNext
= ParseSaveChar
;
259 * Remove initial white space.
261 for (p
= ParseNext
; isspace(*p
); p
++)
264 if ((*p
== '\0') || (*p
== '#'))
271 if (*token
!= '(' && *token
!= ')')
272 while (*p
!= '\0' && *p
!= '(' && *p
!= ')' && !isspace(*p
))
282 static char * GetToken(void)
286 if (ParseBuffer
== NULL
)
288 ParseBuffer
= xmalloc(512);
289 ParseNext
= ParseBuffer
;
293 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
295 if (ParseBuffer
[0] != '#')
300 while ((token
= GetTokenInLine()) == NULL
)
302 ParseNext
= ParseBuffer
;
306 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
308 if (ParseBuffer
[0] != '#')
317 /*******************************************************************
320 * Parse a variable definition.
322 static int ParseVariable( ORDDEF
*odp
)
327 int value_array_size
;
329 char *token
= GetToken();
332 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
333 SpecName
, Line
, token
);
338 value_array_size
= 25;
339 value_array
= xmalloc(sizeof(*value_array
) * value_array_size
);
341 while ((token
= GetToken()) != NULL
)
346 value_array
[n_values
++] = strtol(token
, &endptr
, 0);
347 if (n_values
== value_array_size
)
349 value_array_size
+= 25;
350 value_array
= xrealloc(value_array
,
351 sizeof(*value_array
) * value_array_size
);
354 if (endptr
== NULL
|| *endptr
!= '\0')
356 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
357 SpecName
, Line
, token
);
364 fprintf(stderr
, "%s:%d: End of file in variable declaration\n",
369 odp
->u
.var
.n_values
= n_values
;
370 odp
->u
.var
.values
= xrealloc(value_array
, sizeof(*value_array
) * n_values
);
376 /*******************************************************************
377 * ParseExportFunction
379 * Parse a function definition.
381 static int ParseExportFunction( ORDDEF
*odp
)
389 if (odp
->type
== TYPE_STDCALL
)
391 fprintf( stderr
, "%s:%d: 'stdcall' not supported for Win16\n",
397 if ((odp
->type
== TYPE_PASCAL
) || (odp
->type
== TYPE_PASCAL_16
))
399 fprintf( stderr
, "%s:%d: 'pascal' not supported for Win32\n",
411 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
412 SpecName
, Line
, token
);
416 for (i
= 0; i
< sizeof(odp
->u
.func
.arg_types
)-1; i
++)
422 if (!strcmp(token
, "word"))
423 odp
->u
.func
.arg_types
[i
] = 'w';
424 else if (!strcmp(token
, "s_word"))
425 odp
->u
.func
.arg_types
[i
] = 's';
426 else if (!strcmp(token
, "long") || !strcmp(token
, "segptr"))
427 odp
->u
.func
.arg_types
[i
] = 'l';
428 else if (!strcmp(token
, "ptr"))
429 odp
->u
.func
.arg_types
[i
] = 'p';
430 else if (!strcmp(token
, "str"))
431 odp
->u
.func
.arg_types
[i
] = 't';
432 else if (!strcmp(token
, "wstr"))
433 odp
->u
.func
.arg_types
[i
] = 'W';
434 else if (!strcmp(token
, "segstr"))
435 odp
->u
.func
.arg_types
[i
] = 'T';
436 else if (!strcmp(token
, "double"))
438 odp
->u
.func
.arg_types
[i
++] = 'l';
439 odp
->u
.func
.arg_types
[i
] = 'l';
443 fprintf(stderr
, "%s:%d: Unknown variable type '%s'\n",
444 SpecName
, Line
, token
);
447 if (SpecType
== SPEC_WIN32
)
449 if (strcmp(token
, "long") &&
450 strcmp(token
, "ptr") &&
451 strcmp(token
, "str") &&
452 strcmp(token
, "wstr") &&
453 strcmp(token
, "double"))
455 fprintf( stderr
, "%s:%d: Type '%s' not supported for Win32\n",
456 SpecName
, Line
, token
);
461 if ((*token
!= ')') || (i
>= sizeof(odp
->u
.func
.arg_types
)))
463 fprintf( stderr
, "%s:%d: Too many arguments\n", SpecName
, Line
);
466 odp
->u
.func
.arg_types
[i
] = '\0';
467 if ((odp
->type
== TYPE_STDCALL
) && !i
)
468 odp
->type
= TYPE_CDECL
; /* stdcall is the same as cdecl for 0 args */
469 strcpy(odp
->u
.func
.link_name
, GetToken());
474 /*******************************************************************
477 * Parse an 'equate' definition.
479 static int ParseEquate( ORDDEF
*odp
)
483 char *token
= GetToken();
484 int value
= strtol(token
, &endptr
, 0);
485 if (endptr
== NULL
|| *endptr
!= '\0')
487 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
488 SpecName
, Line
, token
);
492 if (SpecType
== SPEC_WIN32
)
494 fprintf( stderr
, "%s:%d: 'equate' not supported for Win32\n",
499 odp
->u
.abs
.value
= value
;
504 /*******************************************************************
507 * Parse a 'stub' definition.
509 static int ParseStub( ORDDEF
*odp
)
511 odp
->u
.func
.arg_types
[0] = '\0';
512 strcpy( odp
->u
.func
.link_name
, STUB_CALLBACK
);
517 /*******************************************************************
520 * Parse an 'varargs' definition.
522 static int ParseVarargs( ORDDEF
*odp
)
526 if (SpecType
== SPEC_WIN16
)
528 fprintf( stderr
, "%s:%d: 'varargs' not supported for Win16\n",
536 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
537 SpecName
, Line
, token
);
543 fprintf(stderr
, "%s:%d: Expected ')' got '%s'\n",
544 SpecName
, Line
, token
);
548 strcpy( odp
->u
.vargs
.link_name
, GetToken() );
553 /*******************************************************************
556 * Parse an 'extern' definition.
558 static int ParseExtern( ORDDEF
*odp
)
560 if (SpecType
== SPEC_WIN16
)
562 fprintf( stderr
, "%s:%d: 'extern' not supported for Win16\n",
566 strcpy( odp
->u
.ext
.link_name
, GetToken() );
571 /*******************************************************************
574 * Parse a 'forward' definition.
576 static int ParseForward( ORDDEF
*odp
)
578 if (SpecType
== SPEC_WIN16
)
580 fprintf( stderr
, "%s:%d: 'forward' not supported for Win16\n",
584 strcpy( odp
->u
.fwd
.link_name
, GetToken() );
589 /*******************************************************************
592 * Parse an ordinal definition.
594 static int ParseOrdinal(int ordinal
)
599 if (ordinal
>= MAX_ORDINALS
)
601 fprintf(stderr
, "%s:%d: Ordinal number too large\n", SpecName
, Line
);
604 if (ordinal
> Limit
) Limit
= ordinal
;
605 if (ordinal
< Base
) Base
= ordinal
;
607 odp
= &OrdinalDefinitions
[ordinal
];
608 if (!(token
= GetToken()))
610 fprintf(stderr
, "%s:%d: Expected type after ordinal\n", SpecName
, Line
);
614 for (odp
->type
= 0; odp
->type
< TYPE_NBTYPES
; odp
->type
++)
615 if (TypeNames
[odp
->type
] && !strcmp( token
, TypeNames
[odp
->type
] ))
618 if (odp
->type
>= TYPE_NBTYPES
)
621 "%s:%d: Expected type after ordinal, found '%s' instead\n",
622 SpecName
, Line
, token
);
626 if (!(token
= GetToken()))
628 fprintf( stderr
, "%s:%d: Expected name after type\n", SpecName
, Line
);
631 strcpy( odp
->name
, token
);
639 return ParseVariable( odp
);
645 return ParseExportFunction( odp
);
647 return ParseEquate( odp
);
649 return ParseStub( odp
);
651 return ParseVarargs( odp
);
653 return ParseExtern( odp
);
655 return ParseForward( odp
);
657 fprintf( stderr
, "Should not happen\n" );
663 /*******************************************************************
668 static int ParseTopLevel(void)
672 while ((token
= GetToken()) != NULL
)
674 if (strcmp(token
, "name") == 0)
676 strcpy(DLLName
, GetToken());
678 if (!DLLFileName
[0]) sprintf( DLLFileName
, "%s.DLL", DLLName
);
680 else if (strcmp(token
, "file") == 0)
682 strcpy(DLLFileName
, GetToken());
683 strupper(DLLFileName
);
685 else if (strcmp(token
, "type") == 0)
688 if (!strcmp(token
, "win16" )) SpecType
= SPEC_WIN16
;
689 else if (!strcmp(token
, "win32" )) SpecType
= SPEC_WIN32
;
692 fprintf(stderr
, "%s:%d: Type must be 'win16' or 'win32'\n",
697 else if (strcmp(token
, "heap") == 0)
700 if (!IsNumberString(token
))
702 fprintf(stderr
, "%s:%d: Expected number after heap\n",
706 DLLHeapSize
= atoi(token
);
708 else if (strcmp(token
, "init") == 0)
710 strcpy(DLLInitFunc
, GetToken());
712 fprintf(stderr
, "%s:%d: Expected function name after init\n", SpecName
, Line
);
714 else if (strcmp(token
, "import") == 0)
716 if (nb_imports
>= MAX_IMPORTS
)
718 fprintf( stderr
, "%s:%d: Too many imports (limit %d)\n",
719 SpecName
, Line
, MAX_IMPORTS
);
722 if (SpecType
!= SPEC_WIN32
)
724 fprintf( stderr
, "%s:%d: Imports not supported for Win16\n", SpecName
, Line
);
727 DLLImports
[nb_imports
++] = xstrdup(GetToken());
729 else if (IsNumberString(token
))
734 ordinal
= atoi(token
);
735 if ((rv
= ParseOrdinal(ordinal
)) < 0)
741 "%s:%d: Expected name, id, length or ordinal\n",
751 /*******************************************************************
754 * Store a list of ints into a byte array.
756 static int StoreVariableCode( unsigned char *buffer
, int size
, ORDDEF
*odp
)
763 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
764 buffer
[i
] = odp
->u
.var
.values
[i
];
767 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
768 ((unsigned short *)buffer
)[i
] = odp
->u
.var
.values
[i
];
771 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
772 ((unsigned int *)buffer
)[i
] = odp
->u
.var
.values
[i
];
775 return odp
->u
.var
.n_values
* size
;
779 /*******************************************************************
782 * Dump a byte stream into the assembly code.
784 static void DumpBytes( FILE *outfile
, const unsigned char *data
, int len
,
789 fprintf( outfile
, "\nstatic BYTE %s[] = \n{", label
);
791 for (i
= 0; i
< len
; i
++)
793 if (!(i
& 0x0f)) fprintf( outfile
, "\n " );
794 fprintf( outfile
, "%d", *data
++ );
795 if (i
< len
- 1) fprintf( outfile
, ", " );
797 fprintf( outfile
, "\n};\n" );
801 /*******************************************************************
804 * Build the in-memory representation of a 16-bit NE module, and dump it
805 * as a byte stream into the assembly code.
807 static int BuildModule16( FILE *outfile
, int max_code_offset
,
808 int max_data_offset
)
814 SEGTABLEENTRY
*pSegment
;
818 ET_BUNDLE
*bundle
= 0;
823 * OFSTRUCT File information
824 * SEGTABLEENTRY Segment 1 (code)
825 * SEGTABLEENTRY Segment 2 (data)
826 * WORD[2] Resource table (empty)
827 * BYTE[2] Imported names (empty)
828 * BYTE[n] Resident names table
829 * BYTE[n] Entry table
832 buffer
= xmalloc( 0x10000 );
834 pModule
= (NE_MODULE
*)buffer
;
835 memset( pModule
, 0, sizeof(*pModule
) );
836 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
839 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_BUILTIN
| NE_FFLAGS_LIBMODULE
;
841 pModule
->heap_size
= DLLHeapSize
;
842 pModule
->stack_size
= 0;
847 pModule
->seg_count
= 2;
848 pModule
->modref_count
= 0;
849 pModule
->nrname_size
= 0;
850 pModule
->modref_table
= 0;
851 pModule
->nrname_fpos
= 0;
852 pModule
->moveable_entries
= 0;
853 pModule
->alignment
= 0;
854 pModule
->truetype
= 0;
855 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
856 pModule
->misc_flags
= 0;
857 pModule
->dlls_to_init
= 0;
858 pModule
->nrname_handle
= 0;
859 pModule
->min_swap_area
= 0;
860 pModule
->expected_version
= 0x030a;
861 pModule
->module32
= 0;
863 pModule
->self_loading_sel
= 0;
865 /* File information */
867 pFileInfo
= (OFSTRUCT
*)(pModule
+ 1);
868 pModule
->fileinfo
= (int)pFileInfo
- (int)pModule
;
869 memset( pFileInfo
, 0, sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
) );
870 pFileInfo
->cBytes
= sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
)
871 + strlen(DLLFileName
);
872 strcpy( pFileInfo
->szPathName
, DLLFileName
);
873 pstr
= (char *)pFileInfo
+ pFileInfo
->cBytes
+ 1;
875 #ifdef __i386__ /* FIXME: Alignment problems! */
879 pSegment
= (SEGTABLEENTRY
*)pstr
;
880 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
881 pSegment
->filepos
= 0;
882 pSegment
->size
= max_code_offset
;
884 pSegment
->minsize
= max_code_offset
;
888 pModule
->dgroup_entry
= (int)pSegment
- (int)pModule
;
889 pSegment
->filepos
= 0;
890 pSegment
->size
= max_data_offset
;
891 pSegment
->flags
= NE_SEGFLAGS_DATA
;
892 pSegment
->minsize
= max_data_offset
;
898 pword
= (WORD
*)pSegment
;
899 pModule
->res_table
= (int)pword
- (int)pModule
;
903 /* Imported names table */
905 pstr
= (char *)pword
;
906 pModule
->import_table
= (int)pstr
- (int)pModule
;
910 /* Resident names table */
912 pModule
->name_table
= (int)pstr
- (int)pModule
;
913 /* First entry is module name */
914 *pstr
= strlen(DLLName
);
915 strcpy( pstr
+ 1, DLLName
);
918 pstr
+= sizeof(WORD
);
919 /* Store all ordinals */
920 odp
= OrdinalDefinitions
+ 1;
921 for (i
= 1; i
<= Limit
; i
++, odp
++)
923 if (!odp
->name
[0]) continue;
924 *pstr
= strlen( odp
->name
);
925 strcpy( pstr
+ 1, odp
->name
);
926 strupper( pstr
+ 1 );
929 pstr
+= sizeof(WORD
);
935 pModule
->entry_table
= (int)pstr
- (int)pModule
;
936 odp
= OrdinalDefinitions
+ 1;
937 for (i
= 1; i
<= Limit
; i
++, odp
++)
948 selector
= 1; /* Code selector */
954 selector
= 2; /* Data selector */
958 selector
= 0xfe; /* Constant selector */
962 selector
= 0; /* Invalid selector */
969 if ( bundle
&& bundle
->last
+1 == i
)
974 bundle
->next
= (char *)pstr
- (char *)pModule
;
976 bundle
= (ET_BUNDLE
*)pstr
;
980 pstr
+= sizeof(ET_BUNDLE
);
983 /* FIXME: is this really correct ?? */
984 entry
= (ET_ENTRY
*)pstr
;
985 entry
->type
= 0xff; /* movable */
986 entry
->flags
= 3; /* exported & public data */
987 entry
->segnum
= selector
;
988 entry
->offs
= odp
->offset
;
989 pstr
+= sizeof(ET_ENTRY
);
994 /* Dump the module content */
996 DumpBytes( outfile
, (char *)pModule
, (int)pstr
- (int)pModule
,
998 return (int)pstr
- (int)pModule
;
1002 /*******************************************************************
1005 * Build a Win32 C file from a spec file.
1007 static int BuildSpec32File( char * specfile
, FILE *outfile
)
1010 int i
, nb_names
, fwd_size
= 0;
1012 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
1014 fprintf( outfile
, "#include \"builtin32.h\"\n\n" );
1016 /* Output code for all stubs functions */
1018 fprintf( outfile
, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
1020 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1022 if (odp
->type
!= TYPE_STUB
) continue;
1023 fprintf( outfile
, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
1027 /* Output code for all register functions */
1029 fprintf( outfile
, "#ifdef __i386__\n" );
1030 fprintf( outfile
, "#ifndef __GNUC__\n" );
1031 fprintf( outfile
, "static void __asm__dummy() {\n" );
1032 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
1033 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1035 if (odp
->type
!= TYPE_REGISTER
) continue;
1037 "__asm__(\".align 4\\n\\t\"\n"
1038 " \".globl " PREFIX
"%s\\n\\t\"\n"
1039 " \".type " PREFIX
"%s,@function\\n\\t\"\n"
1040 " \"" PREFIX
"%s:\\n\\t\"\n"
1041 " \"call " PREFIX
"CALL32_Regs\\n\\t\"\n"
1042 " \".long " PREFIX
"__regs_%s\\n\\t\"\n"
1043 " \".byte %d,%d\");\n",
1044 odp
->u
.func
.link_name
, odp
->u
.func
.link_name
,
1045 odp
->u
.func
.link_name
, odp
->u
.func
.link_name
,
1046 4 * strlen(odp
->u
.func
.arg_types
),
1047 4 * strlen(odp
->u
.func
.arg_types
) );
1049 fprintf( outfile
, "#ifndef __GNUC__\n" );
1050 fprintf( outfile
, "}\n" );
1051 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
1052 fprintf( outfile
, "#endif /* defined(__i386__) */\n" );
1054 /* Output the DLL functions prototypes */
1056 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1061 fprintf( outfile
, "extern void %s();\n", odp
->u
.vargs
.link_name
);
1064 fprintf( outfile
, "extern void %s();\n", odp
->u
.ext
.link_name
);
1069 fprintf( outfile
, "extern void %s();\n", odp
->u
.func
.link_name
);
1072 fwd_size
+= strlen(odp
->u
.fwd
.link_name
) + 1;
1078 fprintf(stderr
,"build: function type %d not available for Win32\n",
1084 /* Output LibMain function */
1085 if (DLLInitFunc
[0]) fprintf( outfile
, "extern void %s();\n", DLLInitFunc
);
1088 /* Output the DLL functions table */
1090 fprintf( outfile
, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1092 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1097 fprintf( outfile
, " 0" );
1100 fprintf( outfile
, " %s", odp
->u
.vargs
.link_name
);
1103 fprintf( outfile
, " %s", odp
->u
.ext
.link_name
);
1108 fprintf( outfile
, " %s", odp
->u
.func
.link_name
);
1111 fprintf( outfile
, " __stub_%d", i
);
1114 fprintf( outfile
, " (ENTRYPOINT32)\"%s\"", odp
->u
.fwd
.link_name
);
1119 if (i
< Limit
) fprintf( outfile
, ",\n" );
1121 fprintf( outfile
, "\n};\n\n" );
1123 /* Output the DLL names table */
1126 fprintf( outfile
, "static const char * const FuncNames[] =\n{\n" );
1127 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1129 if (odp
->type
== TYPE_INVALID
) continue;
1130 if (nb_names
++) fprintf( outfile
, ",\n" );
1131 fprintf( outfile
, " \"%s\"", odp
->name
);
1133 fprintf( outfile
, "\n};\n\n" );
1135 /* Output the DLL argument types */
1137 fprintf( outfile
, "static const unsigned int ArgTypes[%d] =\n{\n",
1139 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1141 unsigned int j
, mask
= 0;
1142 if ((odp
->type
== TYPE_STDCALL
) || (odp
->type
== TYPE_CDECL
) ||
1143 (odp
->type
== TYPE_REGISTER
))
1144 for (j
= 0; odp
->u
.func
.arg_types
[j
]; j
++)
1146 if (odp
->u
.func
.arg_types
[j
] == 't') mask
|= 1<< (j
*2);
1147 if (odp
->u
.func
.arg_types
[j
] == 'W') mask
|= 2<< (j
*2);
1149 fprintf( outfile
, " %d", mask
);
1150 if (i
< Limit
) fprintf( outfile
, ",\n" );
1152 fprintf( outfile
, "\n};\n\n" );
1154 /* Output the DLL ordinals table */
1156 fprintf( outfile
, "static const unsigned short FuncOrdinals[] =\n{\n" );
1158 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1160 if (odp
->type
== TYPE_INVALID
) continue;
1161 if (nb_names
++) fprintf( outfile
, ",\n" );
1162 fprintf( outfile
, " %d", i
- Base
);
1164 fprintf( outfile
, "\n};\n\n" );
1166 /* Output the DLL functions arguments */
1168 fprintf( outfile
, "static const unsigned char FuncArgs[%d] =\n{\n",
1170 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1176 args
= (unsigned char)strlen(odp
->u
.func
.arg_types
);
1179 args
= 0x80 | (unsigned char)strlen(odp
->u
.func
.arg_types
);
1182 args
= 0x40 | (unsigned char)strlen(odp
->u
.func
.arg_types
);
1191 fprintf( outfile
, " 0x%02x", args
);
1192 if (i
< Limit
) fprintf( outfile
, ",\n" );
1194 fprintf( outfile
, "\n};\n\n" );
1196 /* Output the DLL imports */
1200 fprintf( outfile
, "static const char * const Imports[%d] =\n{\n", nb_imports
);
1201 for (i
= 0; i
< nb_imports
; i
++)
1203 fprintf( outfile
, " \"%s\"", DLLImports
[i
] );
1204 if (i
< nb_imports
-1) fprintf( outfile
, ",\n" );
1206 fprintf( outfile
, "\n};\n\n" );
1209 /* Output the DLL descriptor */
1211 fprintf( outfile
, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1213 fprintf( outfile
, " \"%s\",\n", DLLName
);
1214 fprintf( outfile
, " %d,\n", Base
);
1215 fprintf( outfile
, " %d,\n", Limit
- Base
+ 1 );
1216 fprintf( outfile
, " %d,\n", nb_names
);
1217 fprintf( outfile
, " %d,\n", nb_imports
);
1218 fprintf( outfile
, " %d,\n", (fwd_size
+ 3) & ~3 );
1225 fprintf( outfile
, " %s,\n", nb_imports
? "Imports" : "0" );
1226 fprintf( outfile
, " %s\n", DLLInitFunc
[0] ? DLLInitFunc
: "0" );
1227 fprintf( outfile
, "};\n" );
1231 /*******************************************************************
1234 static int Spec16TypeCompare( const void *e1
, const void *e2
)
1236 const ORDDEF
*odp1
= *(const ORDDEF
**)e1
;
1237 const ORDDEF
*odp2
= *(const ORDDEF
**)e2
;
1239 int type1
= (odp1
->type
== TYPE_CDECL
) ? 0
1240 : (odp1
->type
== TYPE_REGISTER
) ? 3
1241 : (odp1
->type
== TYPE_PASCAL_16
) ? 1 : 2;
1243 int type2
= (odp2
->type
== TYPE_CDECL
) ? 0
1244 : (odp2
->type
== TYPE_REGISTER
) ? 3
1245 : (odp2
->type
== TYPE_PASCAL_16
) ? 1 : 2;
1247 int retval
= type1
- type2
;
1249 retval
= strcmp( odp1
->u
.func
.arg_types
, odp2
->u
.func
.arg_types
);
1254 /*******************************************************************
1257 * Build a Win16 assembly file from a spec file.
1259 static int BuildSpec16File( char * specfile
, FILE *outfile
)
1261 ORDDEF
*odp
, **typelist
;
1263 int code_offset
, data_offset
, module_size
;
1264 unsigned char *data
;
1268 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
1270 fprintf( outfile
, "#define __FLATCS__ 0x%04x\n", Code_Selector
);
1271 fprintf( outfile
, "#include \"builtin16.h\"\n\n" );
1273 data
= (unsigned char *)xmalloc( 0x10000 );
1274 memset( data
, 0, 16 );
1278 /* Build sorted list of all argument types, without duplicates */
1280 typelist
= (ORDDEF
**)calloc( Limit
+1, sizeof(ORDDEF
*) );
1282 odp
= OrdinalDefinitions
;
1283 for (i
= j
= 0; i
<= Limit
; i
++, odp
++)
1290 case TYPE_PASCAL_16
:
1292 typelist
[j
++] = odp
;
1299 qsort( typelist
, j
, sizeof(ORDDEF
*), Spec16TypeCompare
);
1304 typelist
[k
++] = typelist
[i
++];
1305 while ( i
< j
&& Spec16TypeCompare( typelist
+ i
, typelist
+ k
-1 ) == 0 )
1309 /* Output CallFrom16 profiles needed by this .spec file */
1311 fprintf( outfile
, "\n/* ### start build ### */\n" );
1313 for ( i
= 0; i
< k
; i
++ )
1314 fprintf( outfile
, "extern void %s_CallFrom16_%s_%s_%s();\n",
1316 (typelist
[i
]->type
== TYPE_CDECL
) ? "c" : "p",
1317 (typelist
[i
]->type
== TYPE_REGISTER
) ? "regs" :
1318 (typelist
[i
]->type
== TYPE_PASCAL_16
) ? "word" : "long",
1319 typelist
[i
]->u
.func
.arg_types
);
1321 fprintf( outfile
, "/* ### stop build ### */\n\n" );
1323 /* Output the DLL functions prototypes */
1325 odp
= OrdinalDefinitions
;
1326 for (i
= 0; i
<= Limit
; i
++, odp
++)
1333 case TYPE_PASCAL_16
:
1334 fprintf( outfile
, "extern void %s();\n", odp
->u
.func
.link_name
);
1341 /* Output code segment */
1343 fprintf( outfile
, "\nstatic ENTRYPOINT16 Code_Segment[] = \n{\n" );
1346 odp
= OrdinalDefinitions
;
1347 for (i
= 0; i
<= Limit
; i
++, odp
++)
1352 odp
->offset
= 0xffff;
1356 odp
->offset
= LOWORD(odp
->u
.abs
.value
);
1360 odp
->offset
= data_offset
;
1361 data_offset
+= StoreVariableCode( data
+ data_offset
, 1, odp
);
1365 odp
->offset
= data_offset
;
1366 data_offset
+= StoreVariableCode( data
+ data_offset
, 2, odp
);
1370 odp
->offset
= data_offset
;
1371 data_offset
+= StoreVariableCode( data
+ data_offset
, 4, odp
);
1377 case TYPE_PASCAL_16
:
1379 fprintf( outfile
, " /* %s.%d */ ", DLLName
, i
);
1380 fprintf( outfile
, "EP( %s, %s_CallFrom16_%s_%s_%s ),\n",
1381 odp
->u
.func
.link_name
,
1383 (odp
->type
== TYPE_CDECL
) ? "c" : "p",
1384 (odp
->type
== TYPE_REGISTER
) ? "regs" :
1385 (odp
->type
== TYPE_PASCAL_16
) ? "word" : "long",
1386 odp
->u
.func
.arg_types
);
1388 odp
->offset
= code_offset
;
1389 code_offset
+= sizeof(ENTRYPOINT16
);
1393 fprintf(stderr
,"build: function type %d not available for Win16\n",
1399 if (!code_offset
) /* Make sure the code segment is not empty */
1401 fprintf( outfile
, " { },\n" );
1402 code_offset
+= sizeof(ENTRYPOINT16
);
1405 fprintf( outfile
, "};\n" );
1407 /* Output data segment */
1409 DumpBytes( outfile
, data
, data_offset
, "Data_Segment" );
1411 /* Build the module */
1413 module_size
= BuildModule16( outfile
, code_offset
, data_offset
);
1415 /* Output the DLL descriptor */
1417 fprintf( outfile
, "\nWIN16_DESCRIPTOR %s_Descriptor = \n{\n", DLLName
);
1418 fprintf( outfile
, " \"%s\",\n", DLLName
);
1419 fprintf( outfile
, " Module,\n" );
1420 fprintf( outfile
, " sizeof(Module),\n" );
1421 fprintf( outfile
, " (BYTE *)Code_Segment,\n" );
1422 fprintf( outfile
, " (BYTE *)Data_Segment\n" );
1423 fprintf( outfile
, "};\n" );
1429 /*******************************************************************
1432 * Build an assembly file from a spec file.
1434 static int BuildSpecFile( FILE *outfile
, char *specname
)
1436 SpecName
= specname
;
1437 SpecFp
= fopen( specname
, "r");
1440 fprintf(stderr
, "Could not open specification file, '%s'\n", specname
);
1444 if (ParseTopLevel() < 0) return -1;
1449 return BuildSpec16File( specname
, outfile
);
1451 return BuildSpec32File( specname
, outfile
);
1453 fprintf( stderr
, "%s: Missing 'type' declaration\n", specname
);
1459 /*******************************************************************
1460 * BuildCallFrom16Func
1462 * Build a 16-bit-to-Wine callback glue function. The syntax of the function
1463 * profile is: call_type_xxxxx, where 'call' is the letter 'c' or 'p' for C or
1464 * Pascal calling convention, 'type' is one of 'regs', 'word' or
1465 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1466 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1467 * 'T'=segmented pointer to null-terminated string).
1468 * For register functions, the arguments are ignored, but they are still
1469 * removed from the stack upon return. !! FIXME !!
1471 * This glue function contains only that part of the 16->32 thunk that is
1472 * variable (depending on the type and number of arguments); the glue routine
1473 * is part of a particular DLL and uses a routine provided by the core to
1474 * perform those actions that do not depend on the argument type.
1476 * The 16->32 glue routine consists of a main entry point (which must be part
1477 * of the ELF .data section) and two auxillary routines (in the .text section).
1478 * The main entry point pushes address of the 'Thunk' auxillary routine onto
1479 * the stack and then jumps to the appropriate core CallFrom16... routine.
1480 * Furthermore, at a fixed position relative to the main entry point, the
1481 * function profile string is stored if relay debugging is active; this string
1482 * will be consulted by the debugging routines in the core to correctly
1483 * display the arguments.
1485 * The core will perform the switch to 32-bit, and then call back to the
1486 * 'Thunk' auxillary routine. This routine is expected to perform the
1488 * - modify the auxillary routine address in the STACK16FRAME to now
1489 * point to the 'ThunkRet' routine
1490 * - convert arguemnts and push them onto the 32-bit stack
1491 * - call the 32-bit target routine
1493 * After the target routine (and then the 'Thunk' routine) have returned,
1494 * the core part will switch back to 16-bit, and finally jump to the
1495 * 'ThunkRet' auxillary routine. This routine is expected to convert the
1496 * return value if necessary (%eax -> %dx:%ax), and perform the appropriate
1497 * return to the 16-bit caller (lret or lret $argsize).
1499 * In the case of a 'register' routine, there is no 'ThunkRet' auxillary
1500 * routine, as the reloading of all registers and return to 16-bit code
1501 * is done by the core routine. The number of arguments to be popped off
1502 * the caller's stack must be returned (in %eax) by the 'Thunk' routine.
1504 * NOTE: The generated routines contain only proper position-independent
1505 * code and may thus be used within shared objects (e.g. libwine.so
1506 * or elfdlls). The exception is the main entry point, which must
1507 * contain absolute relocations but cannot yet use the GOT; thus
1508 * this entry point is made part of the ELF .data section.
1510 static void BuildCallFrom16Func( FILE *outfile
, char *profile
, char *prefix
)
1512 int i
, pos
, argsize
= 0;
1516 char *args
= profile
+ 7;
1518 /* Parse function type */
1520 if (!strncmp( "c_", profile
, 2 )) usecdecl
= 1;
1521 else if (strncmp( "p_", profile
, 2 ))
1523 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1527 if (!strncmp( "word_", profile
+ 2, 5 )) short_ret
= 1;
1528 else if (!strncmp( "regs_", profile
+ 2, 5 )) reg_func
= 1;
1529 else if (strncmp( "long_", profile
+ 2, 5 ))
1531 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1535 for ( i
= 0; args
[i
]; i
++ )
1538 case 'w': /* word */
1539 case 's': /* s_word */
1543 case 'l': /* long or segmented pointer */
1544 case 'T': /* segmented pointer to null-terminated string */
1545 case 'p': /* linear pointer */
1546 case 't': /* linear pointer to null-terminated string */
1552 * Build main entry point (in .data section)
1554 * NOTE: If you change this, you must also change the retrieval of
1555 * the profile string relative to the main entry point address
1556 * (see BUILTIN_GetEntryPoint16 in if1632/builtin.c).
1558 fprintf( outfile
, "\t.data\n" );
1559 fprintf( outfile
, "\t.globl " PREFIX
"%s_CallFrom16_%s\n", prefix
, profile
);
1560 fprintf( outfile
, PREFIX
"%s_CallFrom16_%s:\n", prefix
, profile
);
1561 fprintf( outfile
, "\tpushl $.L%s_Thunk_%s\n", prefix
, profile
);
1562 fprintf( outfile
, "\tjmp " PREFIX
"%s\n",
1563 !reg_func
? "CallFrom16" : "CallFrom16Register" );
1565 fprintf( outfile
, "\t" STRING
" \"%s\\0\"\n", profile
);
1568 * Build *Thunk* routine
1570 * This routine gets called by the main CallFrom16 routine with
1571 * registers set up as follows:
1573 * STACK16FRAME is completely set up on the 16-bit stack
1574 * DS, ES, SS: flat data segment
1576 * ESP: points to 32-bit stack
1577 * EBP: points to ebp member of last STACK32FRAME
1578 * EDX: points to current STACK16FRAME
1579 * ECX: points to ldt_copy
1582 fprintf( outfile
, "\t.text\n" );
1583 fprintf( outfile
, ".L%s_Thunk_%s:\n", prefix
, profile
);
1586 fprintf( outfile
, "\tleal 4(%%esp), %%eax\n"
1587 "\tpushl %%eax\n" );
1589 fprintf( outfile
, "\taddl $[.L%s_ThunkRet_%s - .L%s_Thunk_%s], %d(%%edx)\n",
1590 prefix
, profile
, prefix
, profile
,
1591 STACK16OFFSET(relay
));
1593 if ( !reg_func
) /* FIXME */
1595 /* Copy the arguments */
1596 pos
= (usecdecl
? argsize
: 0) + sizeof( STACK16FRAME
);
1598 for ( i
= strlen(args
)-1; i
>= 0; i
-- )
1601 case 'w': /* word */
1602 if ( usecdecl
) pos
-= 2;
1603 fprintf( outfile
, "\tmovzwl %d(%%edx),%%eax\n", pos
);
1604 fprintf( outfile
, "\tpushl %%eax\n" );
1605 if ( !usecdecl
) pos
+= 2;
1608 case 's': /* s_word */
1609 if ( usecdecl
) pos
-= 2;
1610 fprintf( outfile
, "\tmovswl %d(%%edx),%%eax\n", pos
);
1611 fprintf( outfile
, "\tpushl %%eax\n" );
1612 if ( !usecdecl
) pos
+= 2;
1615 case 'l': /* long or segmented pointer */
1616 case 'T': /* segmented pointer to null-terminated string */
1617 if ( usecdecl
) pos
-= 4;
1618 fprintf( outfile
, "\tpushl %d(%%edx)\n", pos
);
1619 if ( !usecdecl
) pos
+= 4;
1622 case 'p': /* linear pointer */
1623 case 't': /* linear pointer to null-terminated string */
1624 if ( usecdecl
) pos
-= 4;
1625 /* Get the selector */
1626 fprintf( outfile
, "\tmovzwl %d(%%edx),%%eax\n", pos
+ 2 );
1627 /* Get the selector base */
1628 fprintf( outfile
, "\tandl $0xfff8,%%eax\n" );
1629 fprintf( outfile
, "\tpushl (%%ecx,%%eax)\n" );
1630 /* Add the offset */
1631 fprintf( outfile
, "\tmovzwl %d(%%edx),%%eax\n", pos
);
1632 fprintf( outfile
, "\taddl %%eax,(%%esp)\n" );
1633 if ( !usecdecl
) pos
+= 4;
1637 fprintf( stderr
, "Unknown arg type '%c'\n", args
[i
] );
1641 /* Call entry point */
1642 fprintf( outfile
, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point
) );
1645 fprintf( outfile
, "\tmovl $%d, %%eax\n", argsize
);
1647 fprintf( outfile
, "\tret\n" );
1651 * Build *ThunkRet* routine
1653 * At this point, all registers are set up for return to 16-bit code.
1654 * EAX contains the function return value.
1655 * SS:SP point to the return address to the caller (on 16-bit stack).
1659 fprintf( outfile
, ".L%s_ThunkRet_%s:\n", prefix
, profile
);
1662 fprintf( outfile
, "\tshldl $16, %%eax, %%edx\n" );
1664 if ( !usecdecl
&& argsize
)
1666 fprintf( outfile
, "\t.byte 0x66\n" );
1667 fprintf( outfile
, "\tlret $%d\n", argsize
);
1671 fprintf( outfile
, "\t.byte 0x66\n" );
1672 fprintf( outfile
, "\tlret\n" );
1675 fprintf( outfile
, "\n" );
1678 /*******************************************************************
1681 * Build a Wine-to-16-bit callback glue function. As above, this glue
1682 * routine will only contain the argument-type dependent part of the
1683 * thunk; the rest will be done by a routine provided by the core.
1685 * Prototypes for the CallTo16 functions:
1686 * extern WORD PREFIX_CallTo16_word_xxx( FARPROC16 func, args... );
1687 * extern LONG PREFIX_CallTo16_long_xxx( FARPROC16 func, args... );
1689 * The main entry point of the glue routine simply performs a call to
1690 * the proper core routine depending on the return type (WORD/LONG).
1691 * Note that the core will never perform a return from this call; however,
1692 * it will use the return address on the stack to access the other
1693 * argument-type dependent parts of the thunk. (If relay debugging is
1694 * active, the core routine will access the number of arguments stored
1695 * in the code section immediately precending the main entry point).
1697 * After the core routine has performed the switch to 16-bit code, it
1698 * will call the argument-transfer routine provided by the glue code
1699 * immediately after the main entry point. This routine is expected
1700 * to transfer the arguments to the 16-bit stack, finish loading the
1701 * register for 16-bit code (%ds and %es must be loaded from %ss),
1702 * and call the 16-bit target.
1704 * The target will return to a 16:16 return address provided by the core.
1705 * The core will finalize the switch back to 32-bit and the return to
1706 * the caller without additional support by the glue code. Note that
1707 * the 32-bit arguments will not be popped off the stack (hence the
1708 * CallTo... routine must *not* be declared WINAPI/CALLBACK).
1711 static void BuildCallTo16Func( FILE *outfile
, char *profile
, char *prefix
)
1713 char *args
= profile
+ 5;
1714 int pos
, short_ret
= 0;
1716 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1717 else if (strncmp( "long_", profile
, 5 ))
1719 fprintf( stderr
, "Invalid function name '%s'.\n", profile
);
1725 /* Number of arguments (for relay debugging) */
1726 fprintf( outfile
, "\n\t.align 4\n" );
1727 fprintf( outfile
, "\t.long %d\n", strlen(args
) );
1730 /* Main entry point */
1733 fprintf( outfile
, ".stabs \"%s_CallTo16_%s:F1\",36,0,0," PREFIX
"%s_CallTo16_%s\n",
1734 prefix
, profile
, prefix
, profile
);
1736 fprintf( outfile
, "\t.globl " PREFIX
"%s_CallTo16_%s\n", prefix
, profile
);
1737 fprintf( outfile
, PREFIX
"%s_CallTo16_%s:\n", prefix
, profile
);
1740 fprintf( outfile
, "\tcall " PREFIX
"CallTo16Word\n" );
1742 fprintf( outfile
, "\tcall " PREFIX
"CallTo16Long\n" );
1745 * The core routine will call here with registers set up as follows:
1747 * SS:SP points to the 16-bit stack
1748 * SS:BP points to the bp member of last STACK16FRAME
1749 * EDX points to the current STACK32FRAME
1750 * ECX contains the 16:16 return address
1751 * FS contains the last 16-bit %fs value
1754 /* Transfer the arguments */
1756 pos
= STACK32OFFSET(args
) + 4; /* first arg is target address */
1761 case 'w': /* word */
1762 fprintf( outfile
, "\tpushw %d(%%edx)\n", pos
);
1764 case 'l': /* long */
1765 fprintf( outfile
, "\tpushl %d(%%edx)\n", pos
);
1768 fprintf( stderr
, "Unexpected case '%c' in BuildCallTo16Func\n",
1774 /* Push the return address */
1776 fprintf( outfile
, "\tpushl %%ecx\n" );
1778 /* Push the called routine address */
1780 fprintf( outfile
, "\tpushl %d(%%edx)\n", STACK32OFFSET(args
) );
1782 /* Set %ds and %es (and %ax just in case) equal to %ss */
1784 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
1785 fprintf( outfile
, "\tmovw %%ax,%%ds\n" );
1786 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
1788 /* Jump to the called routine */
1790 fprintf( outfile
, "\t.byte 0x66\n" );
1791 fprintf( outfile
, "\tlret\n" );
1796 /*******************************************************************
1797 * BuildCallFrom16Core
1799 * This routine builds the core routines used in 16->32 thunks:
1800 * CallFrom16, CallFrom16Register, and CallFrom16Thunk.
1802 * CallFrom16 and CallFrom16Register are used by the 16->32 glue code
1803 * as described above. CallFrom16Thunk is a special variant used by
1804 * the implementation of the Win95 16->32 thunk functions C16ThkSL and
1805 * C16ThkSL01 and is implemented as follows:
1806 * On entry, the EBX register is set up to contain a flat pointer to the
1807 * 16-bit stack such that EBX+22 points to the first argument.
1808 * Then, the entry point is called, while EBP is set up to point
1809 * to the return address (on the 32-bit stack).
1810 * The called function returns with CX set to the number of bytes
1811 * to be popped of the caller's stack.
1813 * Stack layout upon entry to the core routine (STACK16FRAME):
1815 * (sp+22) word first 16-bit arg
1819 * (sp+12) long 32-bit entry point (reused for Win16 mutex recursion count)
1820 * (sp+8) long cs of 16-bit entry point
1821 * (sp+4) long ip of 16-bit entry point
1822 * (sp) long auxillary relay function address
1824 * Added on the stack:
1825 * (sp-2) word saved gs
1826 * (sp-4) word saved fs
1827 * (sp-6) word saved es
1828 * (sp-8) word saved ds
1829 * (sp-12) long saved ebp
1830 * (sp-16) long saved ecx
1831 * (sp-20) long saved edx
1832 * (sp-24) long saved previous stack
1834 static void BuildCallFrom16Core( FILE *outfile
, int reg_func
, int thunk
)
1836 char *name
= thunk
? "Thunk" : reg_func
? "Register" : "";
1838 /* Function header */
1839 fprintf( outfile
, "\n\t.align 4\n" );
1841 fprintf( outfile
, ".stabs \"CallFrom16%s:F1\",36,0,0," PREFIX
"CallFrom16%s\n",
1844 fprintf( outfile
, "\t.globl " PREFIX
"CallFrom16%s\n", name
);
1845 fprintf( outfile
, PREFIX
"CallFrom16%s:\n", name
);
1847 /* No relay function for 'thunk' */
1849 fprintf( outfile
, "\tpushl $0\n" );
1851 /* Create STACK16FRAME (except STACK32FRAME link) */
1852 fprintf( outfile
, "\tpushw %%gs\n" );
1853 fprintf( outfile
, "\tpushw %%fs\n" );
1854 fprintf( outfile
, "\tpushw %%es\n" );
1855 fprintf( outfile
, "\tpushw %%ds\n" );
1856 fprintf( outfile
, "\tpushl %%ebp\n" );
1857 fprintf( outfile
, "\tpushl %%ecx\n" );
1858 fprintf( outfile
, "\tpushl %%edx\n" );
1862 /* Get Global Offset Table into %ecx */
1863 fprintf( outfile
, "\tcall .LCallFrom16%s.getgot1\n", name
);
1864 fprintf( outfile
, ".LCallFrom16%s.getgot1:\n", name
);
1865 fprintf( outfile
, "\tpopl %%ecx\n" );
1866 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot1], %%ecx\n", name
);
1869 /* Load 32-bit segment registers */
1870 fprintf( outfile
, "\tmovw $0x%04x, %%dx\n", Data_Selector
);
1872 fprintf( outfile
, "\tdata16\n");
1874 fprintf( outfile
, "\tmovw %%dx, %%ds\n" );
1876 fprintf( outfile
, "\tdata16\n");
1878 fprintf( outfile
, "\tmovw %%dx, %%es\n" );
1882 fprintf( outfile
, "\tmovl " PREFIX
"SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
1883 fprintf( outfile
, "\tmovw (%%edx), %%fs\n" );
1886 fprintf( outfile
, "\tmovw " PREFIX
"SYSLEVEL_Win16CurrentTeb, %%fs\n" );
1888 /* Get address of ldt_copy array into %ecx */
1890 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy@GOT(%%ecx), %%ecx\n" );
1892 fprintf( outfile
, "\tmovl $" PREFIX
"ldt_copy, %%ecx\n" );
1894 /* Translate STACK16FRAME base to flat offset in %edx */
1895 fprintf( outfile
, "\tmovw %%ss, %%dx\n" );
1896 fprintf( outfile
, "\tandl $0xfff8, %%edx\n" );
1897 fprintf( outfile
, "\tmovl (%%ecx,%%edx), %%edx\n" );
1898 fprintf( outfile
, "\tmovzwl %%sp, %%ebp\n" );
1899 fprintf( outfile
, "\tleal -4(%%ebp,%%edx), %%edx\n" );
1900 /* -4 since STACK16FRAME not yet complete! */
1902 /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
1903 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET
);
1904 fprintf( outfile
, "\tpushl %%ebp\n" );
1908 fprintf( outfile
,"\tdata16\n");
1910 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET
+ 2 );
1911 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET
);
1912 fprintf( outfile
, "\tpushl %%ds\n" );
1913 fprintf( outfile
, "\tpopl %%ss\n" );
1914 fprintf( outfile
, "\tmovl %%ebp, %%esp\n" );
1915 fprintf( outfile
, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME
, ebp
) );
1919 STACK16FRAME is completely set up
1920 DS, ES, SS: flat data segment
1922 ESP: points to last STACK32FRAME
1923 EBP: points to ebp member of last STACK32FRAME
1924 EDX: points to current STACK16FRAME
1925 ECX: points to ldt_copy
1926 all other registers: unchanged */
1928 /* Special case: C16ThkSL stub */
1931 /* Set up registers as expected and call thunk */
1932 fprintf( outfile
, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME
)-22 );
1933 fprintf( outfile
, "\tleal -4(%%esp), %%ebp\n" );
1935 fprintf( outfile
, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point
) );
1937 /* Switch stack back */
1938 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
1939 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
1940 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
1942 /* Restore registers and return directly to caller */
1943 fprintf( outfile
, "\taddl $8, %%esp\n" );
1944 fprintf( outfile
, "\tpopl %%ebp\n" );
1945 fprintf( outfile
, "\tpopw %%ds\n" );
1946 fprintf( outfile
, "\tpopw %%es\n" );
1947 fprintf( outfile
, "\tpopw %%fs\n" );
1948 fprintf( outfile
, "\tpopw %%gs\n" );
1949 fprintf( outfile
, "\taddl $18, %%esp\n" );
1951 fprintf( outfile
, "\txorb %%ch, %%ch\n" );
1952 fprintf( outfile
, "\tpopl %%ebx\n" );
1953 fprintf( outfile
, "\taddw %%cx, %%sp\n" );
1954 fprintf( outfile
, "\tpush %%ebx\n" );
1956 fprintf( outfile
, "\t.byte 0x66\n" );
1957 fprintf( outfile
, "\tlret\n" );
1963 /* Build register CONTEXT */
1966 fprintf( outfile
, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86
) );
1968 fprintf( outfile
, "\tpushfl\n" );
1969 fprintf( outfile
, "\tpopl %d(%%esp)\n", CONTEXTOFFSET(EFlags
) );
1971 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax
) );
1972 fprintf( outfile
, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx
) );
1973 fprintf( outfile
, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi
) );
1974 fprintf( outfile
, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi
) );
1976 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp
) );
1977 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp
) );
1978 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx
) );
1979 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx
) );
1980 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx
) );
1981 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx
) );
1983 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds
) );
1984 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs
) );
1985 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es
) );
1986 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs
) );
1987 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs
) );
1988 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs
) );
1989 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs
) );
1990 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs
) );
1992 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs
) );
1993 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs
) );
1994 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip
) );
1995 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip
) );
1997 fprintf( outfile
, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET
+2 );
1998 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs
) );
1999 fprintf( outfile
, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET
);
2000 fprintf( outfile
, "\taddl $%d, %%eax\n", STACK16OFFSET(ip
) );
2001 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp
) );
2003 fprintf( outfile
, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave
) );
2008 /* Print debug info before call */
2013 fprintf( outfile
, "\tpushl %%ebx\n" );
2015 /* Get Global Offset Table into %ebx (for PLT call) */
2016 fprintf( outfile
, "\tcall .LCallFrom16%s.getgot2\n", name
);
2017 fprintf( outfile
, ".LCallFrom16%s.getgot2:\n", name
);
2018 fprintf( outfile
, "\tpopl %%ebx\n" );
2019 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot2], %%ebx\n", name
);
2022 fprintf( outfile
, "\tpushl %%ecx\n" );
2023 fprintf( outfile
, "\tpushl %%edx\n" );
2025 fprintf( outfile
, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2026 sizeof(CONTEXT
) + STRUCTOFFSET(STACK32FRAME
, ebp
) );
2028 fprintf( outfile
, "\tpushl $0\n" );
2031 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16@PLT\n ");
2033 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16\n ");
2035 fprintf( outfile
, "\tpopl %%edx\n" );
2036 fprintf( outfile
, "\tpopl %%edx\n" );
2037 fprintf( outfile
, "\tpopl %%ecx\n" );
2040 fprintf( outfile
, "\tpopl %%ebx\n" );
2043 /* Call *Thunk* relay routine (which will call the API entry point) */
2044 fprintf( outfile
, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay
) );
2046 /* Print debug info after call */
2051 fprintf( outfile
, "\tpushl %%ebx\n" );
2053 /* Get Global Offset Table into %ebx (for PLT call) */
2054 fprintf( outfile
, "\tcall .LCallFrom16%s.getgot3\n", name
);
2055 fprintf( outfile
, ".LCallFrom16%s.getgot3:\n", name
);
2056 fprintf( outfile
, "\tpopl %%ebx\n" );
2057 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot3], %%ebx\n", name
);
2060 fprintf( outfile
, "\tpushl %%eax\n" );
2062 fprintf( outfile
, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2063 sizeof(CONTEXT
) + STRUCTOFFSET(STACK32FRAME
, ebp
) );
2065 fprintf( outfile
, "\tpushl $0\n" );
2068 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret@PLT\n ");
2070 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret\n ");
2072 fprintf( outfile
, "\tpopl %%eax\n" );
2073 fprintf( outfile
, "\tpopl %%eax\n" );
2076 fprintf( outfile
, "\tpopl %%ebx\n" );
2082 fprintf( outfile
, "\tmovl %%esp, %%ebx\n" );
2084 /* Switch stack back */
2085 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2086 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
2087 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2089 /* Restore all registers from CONTEXT */
2090 fprintf( outfile
, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs
) );
2091 fprintf( outfile
, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp
) );
2092 fprintf( outfile
, "\tleal 4(%%esp, %%eax), %%esp\n" );
2094 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs
) );
2095 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip
) );
2096 fprintf( outfile
, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags
) );
2097 fprintf( outfile
, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs
) );
2099 fprintf( outfile
, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs
) );
2100 fprintf( outfile
, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs
) );
2101 fprintf( outfile
, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs
) );
2103 fprintf( outfile
, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp
) );
2104 fprintf( outfile
, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi
) );
2105 fprintf( outfile
, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi
) );
2106 fprintf( outfile
, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax
) );
2107 fprintf( outfile
, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx
) );
2108 fprintf( outfile
, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx
) );
2109 fprintf( outfile
, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx
) );
2111 fprintf( outfile
, "\tpopl %%ds\n" );
2112 fprintf( outfile
, "\tpopfl\n" );
2113 fprintf( outfile
, "\t.byte 0x66\n" );
2114 fprintf( outfile
, "\tlret\n" );
2118 /* Switch stack back */
2119 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2120 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
2121 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2123 /* Set flags according to return value */
2124 fprintf( outfile
, "\torl %%eax, %%eax\n" );
2126 /* Restore registers and return to *ThunkRet* routine */
2127 fprintf( outfile
, "\tpopl %%edx\n" );
2128 fprintf( outfile
, "\tpopl %%ecx\n" );
2129 fprintf( outfile
, "\tpopl %%ebp\n" );
2130 fprintf( outfile
, "\tpopw %%ds\n" );
2131 fprintf( outfile
, "\tpopw %%es\n" );
2132 fprintf( outfile
, "\tpopw %%fs\n" );
2133 fprintf( outfile
, "\tpopw %%gs\n" );
2134 fprintf( outfile
, "\tret $14\n" );
2139 /*******************************************************************
2142 * This routine builds the core routines used in 32->16 thunks:
2143 * CallTo16Word, CallTo16Long, CallTo16RegisterShort, and
2144 * CallTo16RegisterLong.
2146 * CallTo16Word and CallTo16Long are used by the 32->16 glue code
2147 * as described above. The register functions can be called directly:
2149 * extern void CallTo16RegisterShort( const CONTEXT86 *context, int nb_args );
2150 * extern void CallTo16RegisterLong ( const CONTEXT86 *context, int nb_args );
2152 * They call to 16-bit code with all registers except SS:SP set up as specified
2153 * by the 'context' structure, and SS:SP set to point to the current 16-bit
2154 * stack, decremented by the value specified in the 'nb_args' argument.
2157 static void BuildCallTo16Core( FILE *outfile
, int short_ret
, int reg_func
)
2159 char *name
= reg_func
== 2 ? "RegisterLong" :
2160 reg_func
== 1 ? "RegisterShort" :
2161 short_ret
? "Word" : "Long";
2163 /* Function header */
2164 fprintf( outfile
, "\n\t.align 4\n" );
2166 fprintf( outfile
, ".stabs \"CallTo16%s:F1\",36,0,0," PREFIX
"CallTo16%s\n",
2169 fprintf( outfile
, "\t.globl " PREFIX
"CallTo16%s\n", name
);
2170 fprintf( outfile
, PREFIX
"CallTo16%s:\n", name
);
2172 /* Retrieve relay target address */
2174 fprintf( outfile
, "\tpopl %%eax\n" );
2176 /* Function entry sequence */
2177 fprintf( outfile
, "\tpushl %%ebp\n" );
2178 fprintf( outfile
, "\tmovl %%esp, %%ebp\n" );
2180 /* Save the 32-bit registers */
2181 fprintf( outfile
, "\tpushl %%ebx\n" );
2182 fprintf( outfile
, "\tpushl %%ecx\n" );
2183 fprintf( outfile
, "\tpushl %%edx\n" );
2184 fprintf( outfile
, "\tpushl %%esi\n" );
2185 fprintf( outfile
, "\tpushl %%edi\n" );
2189 /* Get Global Offset Table into %ebx */
2190 fprintf( outfile
, "\tcall .LCallTo16%s.getgot1\n", name
);
2191 fprintf( outfile
, ".LCallTo16%s.getgot1:\n", name
);
2192 fprintf( outfile
, "\tpopl %%ebx\n" );
2193 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot1], %%ebx\n", name
);
2196 /* Move relay target address to %edi */
2198 fprintf( outfile
, "\tmovl %%eax, %%edi\n" );
2200 /* Enter Win16 Mutex */
2202 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_EnterWin16Lock@PLT\n" );
2204 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_EnterWin16Lock\n" );
2206 /* Print debugging info */
2209 /* Push number of arguments (from relay stub) */
2211 fprintf( outfile
, "\tpushl $-1\n" );
2213 fprintf( outfile
, "\tpushl -9(%%edi)\n" );
2215 /* Push the address of the first argument */
2216 fprintf( outfile
, "\tleal 8(%%ebp),%%eax\n" );
2217 fprintf( outfile
, "\tpushl %%eax\n" );
2220 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16@PLT\n" );
2222 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16\n" );
2224 fprintf( outfile
, "\tpopl %%eax\n" );
2225 fprintf( outfile
, "\tpopl %%eax\n" );
2228 /* Get return address */
2230 fprintf( outfile
, "\tmovl " PREFIX
"CallTo16_RetAddr@GOTOFF(%%ebx), %%ecx\n" );
2232 fprintf( outfile
, "\tmovl " PREFIX
"CallTo16_RetAddr, %%ecx\n" );
2234 /* Call the actual CallTo16 routine (simulate a lcall) */
2235 fprintf( outfile
, "\tpushl %%cs\n" );
2236 fprintf( outfile
, "\tcall .LCallTo16%s\n", name
);
2238 /* Convert and push return value */
2241 fprintf( outfile
, "\tmovzwl %%ax, %%eax\n" );
2242 fprintf( outfile
, "\tpushl %%eax\n" );
2244 else if ( reg_func
!= 2 )
2246 fprintf( outfile
, "\tshll $16,%%edx\n" );
2247 fprintf( outfile
, "\tmovw %%ax,%%dx\n" );
2248 fprintf( outfile
, "\tpushl %%edx\n" );
2251 fprintf( outfile
, "\tpushl %%eax\n" );
2255 /* Get Global Offset Table into %ebx (might have been overwritten) */
2256 fprintf( outfile
, "\tcall .LCallTo16%s.getgot2\n", name
);
2257 fprintf( outfile
, ".LCallTo16%s.getgot2:\n", name
);
2258 fprintf( outfile
, "\tpopl %%ebx\n" );
2259 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot2], %%ebx\n", name
);
2262 /* Print debugging info */
2266 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16Ret@PLT\n" );
2268 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16Ret\n" );
2271 /* Leave Win16 Mutex */
2273 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_LeaveWin16Lock@PLT\n" );
2275 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_LeaveWin16Lock\n" );
2277 /* Get return value */
2278 fprintf( outfile
, "\tpopl %%eax\n" );
2280 /* Restore the 32-bit registers */
2281 fprintf( outfile
, "\tpopl %%edi\n" );
2282 fprintf( outfile
, "\tpopl %%esi\n" );
2283 fprintf( outfile
, "\tpopl %%edx\n" );
2284 fprintf( outfile
, "\tpopl %%ecx\n" );
2285 fprintf( outfile
, "\tpopl %%ebx\n" );
2287 /* Function exit sequence */
2288 fprintf( outfile
, "\tpopl %%ebp\n" );
2289 fprintf( outfile
, "\tret\n" );
2292 /* Start of the actual CallTo16 routine */
2294 fprintf( outfile
, ".LCallTo16%s:\n", name
);
2296 /* Complete STACK32FRAME */
2297 fprintf( outfile
, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET
);
2298 fprintf( outfile
, "\tmovl %%esp,%%edx\n" );
2300 /* Switch to the 16-bit stack */
2302 fprintf( outfile
,"\tdata16\n");
2304 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2);
2305 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET
);
2306 fprintf( outfile
, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET
);
2310 /* Add the specified offset to the new sp */
2311 fprintf( outfile
, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(args
)+4 );
2313 /* Push the return address
2314 * With sreg suffix, we push 16:16 address (normal lret)
2315 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
2318 fprintf( outfile
, "\tpushl %%ecx\n" );
2321 fprintf( outfile
, "\tshldl $16, %%ecx, %%eax\n" );
2322 fprintf( outfile
, "\tpushw $0\n" );
2323 fprintf( outfile
, "\tpushw %%ax\n" );
2324 fprintf( outfile
, "\tpushw $0\n" );
2325 fprintf( outfile
, "\tpushw %%cx\n" );
2328 /* Push the called routine address */
2329 fprintf( outfile
, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(args
) );
2330 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs
) );
2331 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip
) );
2333 /* Get the registers */
2334 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs
) );
2335 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs
) );
2336 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2337 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs
) );
2338 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2339 fprintf( outfile
, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp
) );
2340 fprintf( outfile
, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi
) );
2341 fprintf( outfile
, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi
) );
2342 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax
) );
2343 fprintf( outfile
, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx
) );
2344 fprintf( outfile
, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx
) );
2345 fprintf( outfile
, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx
) );
2347 /* Get the 16-bit ds */
2348 fprintf( outfile
, "\tpopw %%ds\n" );
2350 /* Jump to the called routine */
2351 fprintf( outfile
, "\t.byte 0x66\n" );
2352 fprintf( outfile
, "\tlret\n" );
2354 else /* not a register function */
2356 /* Make %bp point to the previous stackframe (built by CallFrom16) */
2357 fprintf( outfile
, "\tmovzwl %%sp,%%ebp\n" );
2358 fprintf( outfile
, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp
) );
2360 /* Set %fs to the value saved by the last CallFrom16 */
2361 fprintf( outfile
, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs
)-STACK16OFFSET(bp
) );
2362 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2364 /* Jump to the relay code */
2365 fprintf( outfile
, "\tjmp *%%edi\n" );
2369 /*******************************************************************
2372 * Build the return code for 16-bit callbacks
2374 static void BuildRet16Func( FILE *outfile
)
2377 * Note: This must reside in the .data section to allow
2378 * run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
2381 fprintf( outfile
, "\n\t.globl " PREFIX
"CallTo16_Ret\n" );
2382 fprintf( outfile
, PREFIX
"CallTo16_Ret:\n" );
2384 /* Restore 32-bit segment registers */
2386 fprintf( outfile
, "\tmovw $0x%04x,%%bx\n", Data_Selector
);
2388 fprintf( outfile
, "\tdata16\n");
2390 fprintf( outfile
, "\tmovw %%bx,%%ds\n" );
2392 fprintf( outfile
, "\tdata16\n");
2394 fprintf( outfile
, "\tmovw %%bx,%%es\n" );
2396 fprintf( outfile
, "\tmovw " PREFIX
"SYSLEVEL_Win16CurrentTeb,%%fs\n" );
2398 /* Restore the 32-bit stack */
2401 fprintf( outfile
, "\tdata16\n");
2403 fprintf( outfile
, "\tmovw %%bx,%%ss\n" );
2404 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET
);
2405 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2407 /* Return to caller */
2409 fprintf( outfile
, "\tlret\n" );
2411 /* Declare the return address variable */
2413 fprintf( outfile
, "\n\t.globl " PREFIX
"CallTo16_RetAddr\n" );
2414 fprintf( outfile
, PREFIX
"CallTo16_RetAddr:\t.long 0\n" );
2417 /*******************************************************************
2418 * BuildCallTo32CBClient
2420 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
2422 * Since the relay stub is itself 32-bit, this should not be a problem;
2423 * unfortunately, the relay stubs are expected to switch back to a
2424 * 16-bit stack (and 16-bit code) after completion :-(
2426 * This would conflict with our 16- vs. 32-bit stack handling, so
2427 * we simply switch *back* to our 32-bit stack before returning to
2430 * The CBClient relay stub expects to be called with the following
2431 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
2432 * stack at the designated places:
2435 * (ebp+14) original arguments to the callback routine
2436 * (ebp+10) far return address to original caller
2437 * (ebp+6) Thunklet target address
2438 * (ebp+2) Thunklet relay ID code
2439 * (ebp) BP (saved by CBClientGlueSL)
2440 * (ebp-2) SI (saved by CBClientGlueSL)
2441 * (ebp-4) DI (saved by CBClientGlueSL)
2442 * (ebp-6) DS (saved by CBClientGlueSL)
2444 * ... buffer space used by the 16-bit side glue for temp copies
2446 * (ebx+4) far return address to 16-bit side glue code
2447 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
2449 * The 32-bit side glue code accesses both the original arguments (via ebp)
2450 * and the temporary copies prepared by the 16-bit side glue (via ebx).
2451 * After completion, the stub will load ss:sp from the buffer at ebx
2452 * and perform a far return to 16-bit code.
2454 * To trick the relay stub into returning to us, we replace the 16-bit
2455 * return address to the glue code by a cs:ip pair pointing to our
2456 * return entry point (the original return address is saved first).
2457 * Our return stub thus called will then reload the 32-bit ss:esp and
2458 * return to 32-bit code (by using and ss:esp value that we have also
2459 * pushed onto the 16-bit stack before and a cs:eip values found at
2460 * that position on the 32-bit stack). The ss:esp to be restored is
2461 * found relative to the 16-bit stack pointer at:
2464 * (ebx-8) sp (32-bit stack pointer)
2466 * The second variant of this routine, CALL32_CBClientEx, which is used
2467 * to implement KERNEL.621, has to cope with yet another problem: Here,
2468 * the 32-bit side directly returns to the caller of the CBClient thunklet,
2469 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
2470 * As we have to return to our 32-bit code first, we have to adapt the
2471 * layout of our temporary area so as to include values for the registers
2472 * that are to be restored, and later (in the implementation of KERNEL.621)
2473 * we *really* restore them. The return stub restores DS, DI, SI, and BP
2474 * from the stack, skips the next 8 bytes (CBClient relay code / target),
2475 * and then performs a lret NN, where NN is the number of arguments to be
2476 * removed. Thus, we prepare our temporary area as follows:
2478 * (ebx+22) 16-bit cs (this segment)
2479 * (ebx+20) 16-bit ip ('16-bit' return entry point)
2480 * (ebx+16) 32-bit ss (flat)
2481 * (ebx+12) 32-bit sp (32-bit stack pointer)
2482 * (ebx+10) 16-bit bp (points to ebx+24)
2483 * (ebx+8) 16-bit si (ignored)
2484 * (ebx+6) 16-bit di (ignored)
2485 * (ebx+4) 16-bit ds (we actually use the flat DS here)
2486 * (ebx+2) 16-bit ss (16-bit stack segment)
2487 * (ebx+0) 16-bit sp (points to ebx+4)
2489 * Note that we ensure that DS is not changed and remains the flat segment,
2490 * and the 32-bit stack pointer our own return stub needs fits just
2491 * perfectly into the 8 bytes that are skipped by the Windows stub.
2492 * One problem is that we have to determine the number of removed arguments,
2493 * as these have to be really removed in KERNEL.621. Thus, the BP value
2494 * that we place in the temporary area to be restored, contains the value
2495 * that SP would have if no arguments were removed. By comparing the actual
2496 * value of SP with this value in our return stub we can compute the number
2497 * of removed arguments. This is then returned to KERNEL.621.
2499 * The stack layout of this function:
2500 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
2501 * (ebp+16) esi pointer to caller's esi value
2502 * (ebp+12) arg ebp value to be set for relay stub
2503 * (ebp+8) func CBClient relay stub address
2507 static void BuildCallTo32CBClient( FILE *outfile
, BOOL isEx
)
2509 char *name
= isEx
? "CBClientEx" : "CBClient";
2510 int size
= isEx
? 24 : 12;
2512 /* Function header */
2514 fprintf( outfile
, "\n\t.align 4\n" );
2516 fprintf( outfile
, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX
"CALL32_%s\n",
2519 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_%s\n", name
);
2520 fprintf( outfile
, PREFIX
"CALL32_%s:\n", name
);
2524 fprintf( outfile
, "\tpushl %%ebp\n" );
2525 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2526 fprintf( outfile
, "\tpushl %%edi\n" );
2527 fprintf( outfile
, "\tpushl %%esi\n" );
2528 fprintf( outfile
, "\tpushl %%ebx\n" );
2530 /* Get the 16-bit stack */
2532 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET
);
2534 /* Convert it to a flat address */
2536 fprintf( outfile
, "\tshldl $16,%%ebx,%%eax\n" );
2537 fprintf( outfile
, "\tandl $0xfff8,%%eax\n" );
2538 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy(%%eax),%%esi\n" );
2539 fprintf( outfile
, "\tmovw %%bx,%%ax\n" );
2540 fprintf( outfile
, "\taddl %%eax,%%esi\n" );
2542 /* Allocate temporary area (simulate STACK16_PUSH) */
2544 fprintf( outfile
, "\tpushf\n" );
2545 fprintf( outfile
, "\tcld\n" );
2546 fprintf( outfile
, "\tleal -%d(%%esi), %%edi\n", size
);
2547 fprintf( outfile
, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME
) );
2548 fprintf( outfile
, "\trep\n\tmovsb\n" );
2549 fprintf( outfile
, "\tpopf\n" );
2551 fprintf( outfile
, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size
, STACKOFFSET
);
2553 fprintf( outfile
, "\tpushl %%edi\n" ); /* remember address */
2555 /* Set up temporary area */
2559 fprintf( outfile
, "\tleal 4(%%edi), %%edi\n" );
2561 fprintf( outfile
, "\tleal -8(%%esp), %%eax\n" );
2562 fprintf( outfile
, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */
2564 fprintf( outfile
, "\tmovl %%ss, %%ax\n" );
2565 fprintf( outfile
, "\tandl $0x0000ffff, %%eax\n" );
2566 fprintf( outfile
, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */
2568 fprintf( outfile
, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME
)-size
+4 + 4 );
2569 fprintf( outfile
, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */
2571 fprintf( outfile
, "\tmovl " PREFIX
"CALL32_%s_RetAddr, %%eax\n", name
);
2572 fprintf( outfile
, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */
2576 fprintf( outfile
, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME
)-size
+4 );
2577 fprintf( outfile
, "\tmovl %%ebx, 0(%%edi)\n" );
2579 fprintf( outfile
, "\tmovl %%ds, %%ax\n" );
2580 fprintf( outfile
, "\tmovw %%ax, 4(%%edi)\n" );
2582 fprintf( outfile
, "\taddl $20, %%ebx\n" );
2583 fprintf( outfile
, "\tmovw %%bx, 10(%%edi)\n" );
2585 fprintf( outfile
, "\tleal -8(%%esp), %%eax\n" );
2586 fprintf( outfile
, "\tmovl %%eax, 12(%%edi)\n" );
2588 fprintf( outfile
, "\tmovl %%ss, %%ax\n" );
2589 fprintf( outfile
, "\tandl $0x0000ffff, %%eax\n" );
2590 fprintf( outfile
, "\tmovl %%eax, 16(%%edi)\n" );
2592 fprintf( outfile
, "\tmovl " PREFIX
"CALL32_%s_RetAddr, %%eax\n", name
);
2593 fprintf( outfile
, "\tmovl %%eax, 20(%%edi)\n" );
2596 /* Set up registers and call CBClient relay stub (simulating a far call) */
2598 fprintf( outfile
, "\tmovl 16(%%ebp), %%esi\n" );
2599 fprintf( outfile
, "\tmovl (%%esi), %%esi\n" );
2601 fprintf( outfile
, "\tmovl %%edi, %%ebx\n" );
2602 fprintf( outfile
, "\tmovl 8(%%ebp), %%eax\n" );
2603 fprintf( outfile
, "\tmovl 12(%%ebp), %%ebp\n" );
2605 fprintf( outfile
, "\tpushl %%cs\n" );
2606 fprintf( outfile
, "\tcall *%%eax\n" );
2608 /* Return new esi value to caller */
2610 fprintf( outfile
, "\tmovl 32(%%esp), %%edi\n" );
2611 fprintf( outfile
, "\tmovl %%esi, (%%edi)\n" );
2613 /* Cleanup temporary area (simulate STACK16_POP) */
2615 fprintf( outfile
, "\tpop %%esi\n" );
2617 fprintf( outfile
, "\tpushf\n" );
2618 fprintf( outfile
, "\tstd\n" );
2619 fprintf( outfile
, "\tdec %%esi\n" );
2620 fprintf( outfile
, "\tleal %d(%%esi), %%edi\n", size
);
2621 fprintf( outfile
, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME
) );
2622 fprintf( outfile
, "\trep\n\tmovsb\n" );
2623 fprintf( outfile
, "\tpopf\n" );
2625 fprintf( outfile
, "\t.byte 0x64\n\taddw $%d,(%d)\n", size
, STACKOFFSET
);
2627 /* Return argument size to caller */
2630 fprintf( outfile
, "\tmovl 32(%%esp), %%ebx\n" );
2631 fprintf( outfile
, "\tmovl %%ebp, (%%ebx)\n" );
2634 /* Restore registers and return */
2636 fprintf( outfile
, "\tpopl %%ebx\n" );
2637 fprintf( outfile
, "\tpopl %%esi\n" );
2638 fprintf( outfile
, "\tpopl %%edi\n" );
2639 fprintf( outfile
, "\tpopl %%ebp\n" );
2640 fprintf( outfile
, "\tret\n" );
2643 static void BuildCallTo32CBClientRet( FILE *outfile
, BOOL isEx
)
2645 char *name
= isEx
? "CBClientEx" : "CBClient";
2647 /* '16-bit' return stub */
2649 fprintf( outfile
, "\n\t.globl " PREFIX
"CALL32_%s_Ret\n", name
);
2650 fprintf( outfile
, PREFIX
"CALL32_%s_Ret:\n", name
);
2654 fprintf( outfile
, "\tmovzwl %%sp, %%ebx\n" );
2655 fprintf( outfile
, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
2659 fprintf( outfile
, "\tmovzwl %%bp, %%ebx\n" );
2660 fprintf( outfile
, "\tsubw %%bp, %%sp\n" );
2661 fprintf( outfile
, "\tmovzwl %%sp, %%ebp\n" );
2662 fprintf( outfile
, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
2664 fprintf( outfile
, "\tlret\n" );
2666 /* Declare the return address variable */
2668 fprintf( outfile
, "\n\t.globl " PREFIX
"CALL32_%s_RetAddr\n", name
);
2669 fprintf( outfile
, PREFIX
"CALL32_%s_RetAddr:\t.long 0\n", name
);
2673 /*******************************************************************
2674 * BuildCallTo32LargeStack
2676 * Build the function used to switch to the original 32-bit stack
2677 * before calling a 32-bit function from 32-bit code. This is used for
2678 * functions that need a large stack, like X bitmaps functions.
2680 * The generated function has the following prototype:
2681 * int xxx( int (*func)(), void *arg );
2683 * The pointer to the function can be retrieved by calling CALL32_Init,
2684 * which also takes care of saving the current 32-bit stack pointer.
2685 * Furthermore, CALL32_Init switches to a new stack and jumps to the
2686 * specified target address.
2688 * NOTE: The CALL32_LargeStack routine may be recursively entered by the
2689 * same thread, but not concurrently entered by several threads.
2691 * Stack layout of CALL32_Init:
2693 * (esp+12) new stack address
2694 * (esp+8) target address
2695 * (esp+4) pointer to variable to receive CALL32_LargeStack address
2698 * Stack layout of CALL32_LargeStack:
2705 static void BuildCallTo32LargeStack( FILE *outfile
)
2707 /* Initialization function */
2709 fprintf( outfile
, "\n\t.align 4\n" );
2711 fprintf( outfile
, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX
"CALL32_Init\n");
2713 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Init\n" );
2714 fprintf( outfile
, "\t.type " PREFIX
"CALL32_Init,@function\n" );
2715 fprintf( outfile
, PREFIX
"CALL32_Init:\n" );
2716 fprintf( outfile
, "\tmovl %%esp,CALL32_Original32_esp\n" );
2717 fprintf( outfile
, "\tpopl %%eax\n" );
2718 fprintf( outfile
, "\tpopl %%eax\n" );
2719 fprintf( outfile
, "\tmovl $CALL32_LargeStack,(%%eax)\n" );
2720 fprintf( outfile
, "\tpopl %%eax\n" );
2721 fprintf( outfile
, "\tpopl %%esp\n" );
2722 fprintf( outfile
, "\tpushl %%eax\n" );
2723 fprintf( outfile
, "\tret\n" );
2725 /* Function header */
2727 fprintf( outfile
, "\n\t.align 4\n" );
2729 fprintf( outfile
, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2731 fprintf( outfile
, "CALL32_LargeStack:\n" );
2735 fprintf( outfile
, "\tpushl %%ebp\n" );
2736 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2738 /* Switch to the original 32-bit stack pointer */
2740 fprintf( outfile
, "\tcmpl $0, CALL32_RecursionCount\n" );
2741 fprintf( outfile
, "\tjne CALL32_skip\n" );
2742 fprintf( outfile
, "\tmovl CALL32_Original32_esp, %%esp\n" );
2743 fprintf( outfile
, "CALL32_skip:\n" );
2745 fprintf( outfile
, "\tincl CALL32_RecursionCount\n" );
2747 /* Transfer the argument and call the function */
2749 fprintf( outfile
, "\tpushl 12(%%ebp)\n" );
2750 fprintf( outfile
, "\tcall *8(%%ebp)\n" );
2752 /* Restore registers and return */
2754 fprintf( outfile
, "\tdecl CALL32_RecursionCount\n" );
2756 fprintf( outfile
, "\tmovl %%ebp,%%esp\n" );
2757 fprintf( outfile
, "\tpopl %%ebp\n" );
2758 fprintf( outfile
, "\tret\n" );
2762 fprintf( outfile
, "\t.data\n" );
2763 fprintf( outfile
, "CALL32_Original32_esp:\t.long 0\n" );
2764 fprintf( outfile
, "CALL32_RecursionCount:\t.long 0\n" );
2765 fprintf( outfile
, "\t.text\n" );
2769 /*******************************************************************
2770 * BuildCallFrom32Regs
2772 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2773 * 'args' is the number of dword arguments.
2777 * (ebp+12) first arg
2778 * (ebp+8) ret addr to user code
2779 * (ebp+4) ret addr to relay code
2781 * (ebp-128) buffer area to allow stack frame manipulation
2782 * (ebp-332) CONTEXT86 struct
2783 * (ebp-336) CONTEXT86 *argument
2784 * .... other arguments copied from (ebp+12)
2786 * The entry point routine is called with a CONTEXT* extra argument,
2787 * following the normal args. In this context structure, EIP_reg
2788 * contains the return address to user code, and ESP_reg the stack
2789 * pointer on return (with the return address and arguments already
2792 static void BuildCallFrom32Regs( FILE *outfile
)
2794 static const int STACK_SPACE
= 128 + sizeof(CONTEXT86
);
2796 /* Function header */
2798 fprintf( outfile
, "\n\t.align 4\n" );
2800 fprintf( outfile
, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX
"CALL32_Regs\n" );
2802 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Regs\n" );
2803 fprintf( outfile
, PREFIX
"CALL32_Regs:\n" );
2805 /* Allocate some buffer space on the stack */
2807 fprintf( outfile
, "\tpushl %%ebp\n" );
2808 fprintf( outfile
, "\tmovl %%esp,%%ebp\n ");
2809 fprintf( outfile
, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE
);
2811 /* Build the context structure */
2813 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax
) - STACK_SPACE
);
2814 fprintf( outfile
, "\tpushfl\n" );
2815 fprintf( outfile
, "\tpopl %%eax\n" );
2816 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags
) - STACK_SPACE
);
2817 fprintf( outfile
, "\tmovl 0(%%ebp),%%eax\n" );
2818 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp
) - STACK_SPACE
);
2819 fprintf( outfile
, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx
) - STACK_SPACE
);
2820 fprintf( outfile
, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx
) - STACK_SPACE
);
2821 fprintf( outfile
, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx
) - STACK_SPACE
);
2822 fprintf( outfile
, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi
) - STACK_SPACE
);
2823 fprintf( outfile
, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi
) - STACK_SPACE
);
2825 fprintf( outfile
, "\txorl %%eax,%%eax\n" );
2826 fprintf( outfile
, "\tmovw %%cs,%%ax\n" );
2827 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs
) - STACK_SPACE
);
2828 fprintf( outfile
, "\tmovw %%es,%%ax\n" );
2829 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs
) - STACK_SPACE
);
2830 fprintf( outfile
, "\tmovw %%fs,%%ax\n" );
2831 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs
) - STACK_SPACE
);
2832 fprintf( outfile
, "\tmovw %%gs,%%ax\n" );
2833 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs
) - STACK_SPACE
);
2834 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
2835 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs
) - STACK_SPACE
);
2836 fprintf( outfile
, "\tmovw %%ds,%%ax\n" );
2837 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs
) - STACK_SPACE
);
2838 fprintf( outfile
, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
2840 fprintf( outfile
, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL
);
2841 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags
) - STACK_SPACE
);
2843 fprintf( outfile
, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
2844 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip
) - STACK_SPACE
);
2846 /* Transfer the arguments */
2848 fprintf( outfile
, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
2849 fprintf( outfile
, "\tpushl %%esp\n" ); /* push ptr to context struct */
2850 fprintf( outfile
, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
2851 fprintf( outfile
, "\tjecxz 1f\n" );
2852 fprintf( outfile
, "\tsubl %%ecx,%%esp\n" );
2853 fprintf( outfile
, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */
2854 fprintf( outfile
, "\tmovl %%esp,%%edi\n" );
2855 fprintf( outfile
, "\tshrl $2,%%ecx\n" );
2856 fprintf( outfile
, "\tcld\n" );
2857 fprintf( outfile
, "\trep\n\tmovsl\n" ); /* copy args */
2859 fprintf( outfile
, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
2860 fprintf( outfile
, "\tleal 12(%%ebp,%%eax),%%eax\n" );
2861 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2863 /* Call the entry point */
2865 fprintf( outfile
, "\tcall *0(%%ebx)\n" );
2867 /* Store %eip and %ebp onto the new stack */
2869 fprintf( outfile
, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2870 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip
) - STACK_SPACE
);
2871 fprintf( outfile
, "\tmovl %%eax,-4(%%edx)\n" );
2872 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp
) - STACK_SPACE
);
2873 fprintf( outfile
, "\tmovl %%eax,-8(%%edx)\n" );
2875 /* Restore the context structure */
2877 /* Note: we don't bother to restore %cs, %ds and %ss
2878 * changing them in 32-bit code is a recipe for disaster anyway
2880 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs
) - STACK_SPACE
);
2881 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2882 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs
) - STACK_SPACE
);
2883 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2884 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs
) - STACK_SPACE
);
2885 fprintf( outfile
, "\tmovw %%ax,%%gs\n" );
2887 fprintf( outfile
, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi
) - STACK_SPACE
);
2888 fprintf( outfile
, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi
) - STACK_SPACE
);
2889 fprintf( outfile
, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx
) - STACK_SPACE
);
2890 fprintf( outfile
, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx
) - STACK_SPACE
);
2891 fprintf( outfile
, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx
) - STACK_SPACE
);
2893 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags
) - STACK_SPACE
);
2894 fprintf( outfile
, "\tpushl %%eax\n" );
2895 fprintf( outfile
, "\tpopfl\n" );
2896 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax
) - STACK_SPACE
);
2898 fprintf( outfile
, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2899 fprintf( outfile
, "\tleal -8(%%ebp),%%esp\n" );
2900 fprintf( outfile
, "\tpopl %%ebp\n" );
2901 fprintf( outfile
, "\tret\n" );
2905 /*******************************************************************
2908 * Build the spec files
2910 static int BuildSpec( FILE *outfile
, int argc
, char *argv
[] )
2913 for (i
= 2; i
< argc
; i
++)
2914 if (BuildSpecFile( outfile
, argv
[i
] ) < 0) return -1;
2918 /*******************************************************************
2921 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
2923 static int BuildGlue( FILE *outfile
, char * outname
, int argc
, char *argv
[] )
2930 infile
= fopen( argv
[2], "r" );
2937 else infile
= stdin
;
2941 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2942 fprintf( outfile
, "\t.text\n" );
2947 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2948 getcwd(buffer
, sizeof(buffer
));
2951 * The stabs help the internal debugger as they are an indication that it
2952 * is sensible to step into a thunk/trampoline.
2954 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2955 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2956 fprintf( outfile
, "\t.text\n" );
2957 fprintf( outfile
, "\t.align 4\n" );
2958 fprintf( outfile
, "Code_Start:\n\n" );
2961 /* Build the callback glue functions */
2963 while (fgets( buffer
, sizeof(buffer
), infile
))
2965 if (strstr( buffer
, "### start build ###" )) break;
2967 while (fgets( buffer
, sizeof(buffer
), infile
))
2970 if ( (p
= strstr( buffer
, "CallFrom16_" )) != NULL
)
2972 char *q
, *profile
= p
+ strlen( "CallFrom16_" );
2973 for (q
= profile
; (*q
== '_') || isalpha(*q
); q
++ )
2976 for (q
= p
-1; q
> buffer
&& ((*q
== '_') || isalnum(*q
)); q
-- )
2978 if ( ++q
< p
) p
[-1] = '\0'; else q
= "";
2979 BuildCallFrom16Func( outfile
, profile
, q
);
2981 if ( (p
= strstr( buffer
, "CallTo16_" )) != NULL
)
2983 char *q
, *profile
= p
+ strlen( "CallTo16_" );
2984 for (q
= profile
; (*q
== '_') || isalpha(*q
); q
++ )
2987 for (q
= p
-1; q
> buffer
&& ((*q
== '_') || isalnum(*q
)); q
-- )
2989 if ( ++q
< p
) p
[-1] = '\0'; else q
= "";
2990 BuildCallTo16Func( outfile
, profile
, q
);
2992 if (strstr( buffer
, "### stop build ###" )) break;
2997 fprintf( outfile
, "\t.text\n");
2998 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2999 fprintf( outfile
, ".Letext:\n");
3002 #else /* __i386__ */
3004 /* Just to avoid an empty file */
3005 fprintf( outfile
, "\t.long 0\n" );
3007 #endif /* __i386__ */
3013 /*******************************************************************
3016 * Build the 16-bit callbacks
3018 static int BuildCall16( FILE *outfile
, char * outname
)
3026 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
3027 fprintf( outfile
, "\t.text\n" );
3032 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
3033 getcwd(buffer
, sizeof(buffer
));
3036 * The stabs help the internal debugger as they are an indication that it
3037 * is sensible to step into a thunk/trampoline.
3039 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
3040 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
3041 fprintf( outfile
, "\t.text\n" );
3042 fprintf( outfile
, "\t.align 4\n" );
3043 fprintf( outfile
, "Code_Start:\n\n" );
3045 fprintf( outfile
, PREFIX
"Call16_Start:\n" );
3046 fprintf( outfile
, "\t.globl "PREFIX
"Call16_Start\n" );
3047 fprintf( outfile
, "\t.byte 0\n\n" );
3050 /* Standard CallFrom16 routine */
3051 BuildCallFrom16Core( outfile
, FALSE
, FALSE
);
3053 /* Register CallFrom16 routine */
3054 BuildCallFrom16Core( outfile
, TRUE
, FALSE
);
3056 /* C16ThkSL CallFrom16 routine */
3057 BuildCallFrom16Core( outfile
, FALSE
, TRUE
);
3059 /* Standard CallTo16 routine (WORD return) */
3060 BuildCallTo16Core( outfile
, TRUE
, FALSE
);
3062 /* Standard CallTo16 routine (DWORD return) */
3063 BuildCallTo16Core( outfile
, FALSE
, FALSE
);
3065 /* Register CallTo16 routine (16:16 retf) */
3066 BuildCallTo16Core( outfile
, FALSE
, 1 );
3068 /* Register CallTo16 routine (16:32 retf) */
3069 BuildCallTo16Core( outfile
, FALSE
, 2 );
3071 /* CBClientThunkSL routine */
3072 BuildCallTo32CBClient( outfile
, FALSE
);
3074 /* CBClientThunkSLEx routine */
3075 BuildCallTo32CBClient( outfile
, TRUE
);
3077 fprintf( outfile
, PREFIX
"Call16_End:\n" );
3078 fprintf( outfile
, "\t.globl "PREFIX
"Call16_End\n" );
3081 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
3082 fprintf( outfile
, ".Letext:\n");
3085 /* The whole Call16_Ret segment must lie within the .data section */
3086 fprintf( outfile
, "\n\t.data\n" );
3087 fprintf( outfile
, "\t.globl " PREFIX
"Call16_Ret_Start\n" );
3088 fprintf( outfile
, PREFIX
"Call16_Ret_Start:\n" );
3090 /* Standard CallTo16 return stub */
3091 BuildRet16Func( outfile
);
3093 /* CBClientThunkSL return stub */
3094 BuildCallTo32CBClientRet( outfile
, FALSE
);
3096 /* CBClientThunkSLEx return stub */
3097 BuildCallTo32CBClientRet( outfile
, TRUE
);
3099 /* End of Call16_Ret segment */
3100 fprintf( outfile
, "\n\t.globl " PREFIX
"Call16_Ret_End\n" );
3101 fprintf( outfile
, PREFIX
"Call16_Ret_End:\n" );
3103 #else /* __i386__ */
3105 fprintf( outfile
, PREFIX
"Call16_Start:\n" );
3106 fprintf( outfile
, "\t.globl "PREFIX
"Call16_Start\n" );
3107 fprintf( outfile
, "\t.byte 0\n\n" );
3108 fprintf( outfile
, PREFIX
"Call16_End:\n" );
3109 fprintf( outfile
, "\t.globl "PREFIX
"Call16_End\n" );
3111 fprintf( outfile
, "\t.globl " PREFIX
"Call16_Ret_Start\n" );
3112 fprintf( outfile
, PREFIX
"Call16_Ret_Start:\n" );
3113 fprintf( outfile
, "\t.byte 0\n\n" );
3114 fprintf( outfile
, "\n\t.globl " PREFIX
"Call16_Ret_End\n" );
3115 fprintf( outfile
, PREFIX
"Call16_Ret_End:\n" );
3117 #endif /* __i386__ */
3122 /*******************************************************************
3125 * Build the 32-bit callbacks
3127 static int BuildCall32( FILE *outfile
, char * outname
)
3135 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
3136 fprintf( outfile
, "\t.text\n" );
3141 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
3142 getcwd(buffer
, sizeof(buffer
));
3145 * The stabs help the internal debugger as they are an indication that it
3146 * is sensible to step into a thunk/trampoline.
3148 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
3149 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
3150 fprintf( outfile
, "\t.text\n" );
3151 fprintf( outfile
, "\t.align 4\n" );
3152 fprintf( outfile
, "Code_Start:\n" );
3155 /* Build the 32-bit large stack callback */
3157 BuildCallTo32LargeStack( outfile
);
3159 /* Build the register callback function */
3161 BuildCallFrom32Regs( outfile
);
3164 fprintf( outfile
, "\t.text\n");
3165 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
3166 fprintf( outfile
, ".Letext:\n");
3169 #else /* __i386__ */
3171 /* Just to avoid an empty file */
3172 fprintf( outfile
, "\t.long 0\n" );
3174 #endif /* __i386__ */
3179 /*******************************************************************
3182 static void usage(void)
3185 "usage: build [-pic] [-o outfile] -spec SPECNAMES\n"
3186 " build [-pic] [-o outfile] -glue SOURCE_FILE\n"
3187 " build [-pic] [-o outfile] -call16\n"
3188 " build [-pic] [-o outfile] -call32\n" );
3193 /*******************************************************************
3196 int main(int argc
, char **argv
)
3198 char *outname
= NULL
;
3199 FILE *outfile
= stdout
;
3202 if (argc
< 2) usage();
3204 if (!strcmp( argv
[1], "-pic" ))
3209 if (argc
< 2) usage();
3212 if (!strcmp( argv
[1], "-o" ))
3217 if (argc
< 2) usage();
3218 if (!(outfile
= fopen( outname
, "w" )))
3220 fprintf( stderr
, "Unable to create output file '%s'\n", outname
);
3225 /* Retrieve the selector values; this assumes that we are building
3226 * the asm files on the platform that will also run them. Probably
3227 * a safe assumption to make.
3229 GET_CS( Code_Selector
);
3230 GET_DS( Data_Selector
);
3232 if (!strcmp( argv
[1], "-spec" ))
3233 res
= BuildSpec( outfile
, argc
, argv
);
3234 else if (!strcmp( argv
[1], "-glue" ))
3235 res
= BuildGlue( outfile
, outname
, argc
, argv
);
3236 else if (!strcmp( argv
[1], "-call16" ))
3237 res
= BuildCall16( outfile
, outname
);
3238 else if (!strcmp( argv
[1], "-call32" ))
3239 res
= BuildCall32( outfile
, outname
);