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" );
1744 /* Return to caller (using STDCALL calling convention) */
1745 if ( strlen( args
) > 0 )
1746 fprintf( outfile
, "\tret $%d\n", strlen( args
) * 4 );
1749 fprintf( outfile
, "\tret\n" );
1751 /* Note: the arg transfer routine must start exactly three bytes
1752 after the return stub, hence the nop's */
1753 fprintf( outfile
, "\tnop\n" );
1754 fprintf( outfile
, "\tnop\n" );
1758 * The core routine will call here with registers set up as follows:
1760 * SS:SP points to the 16-bit stack
1761 * SS:BP points to the bp member of last STACK16FRAME
1762 * EDX points to the current STACK32FRAME
1763 * ECX contains the 16:16 return address
1764 * FS contains the last 16-bit %fs value
1767 /* Transfer the arguments */
1769 pos
= STACK32OFFSET(args
) + 4; /* first arg is target address */
1774 case 'w': /* word */
1775 fprintf( outfile
, "\tpushw %d(%%edx)\n", pos
);
1777 case 'l': /* long */
1778 fprintf( outfile
, "\tpushl %d(%%edx)\n", pos
);
1781 fprintf( stderr
, "Unexpected case '%c' in BuildCallTo16Func\n",
1787 /* Push the return address */
1789 fprintf( outfile
, "\tpushl %%ecx\n" );
1791 /* Push the called routine address */
1793 fprintf( outfile
, "\tpushl %d(%%edx)\n", STACK32OFFSET(args
) );
1795 /* Set %ds and %es (and %ax just in case) equal to %ss */
1797 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
1798 fprintf( outfile
, "\tmovw %%ax,%%ds\n" );
1799 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
1801 /* Jump to the called routine */
1803 fprintf( outfile
, "\t.byte 0x66\n" );
1804 fprintf( outfile
, "\tlret\n" );
1809 /*******************************************************************
1810 * BuildCallFrom16Core
1812 * This routine builds the core routines used in 16->32 thunks:
1813 * CallFrom16, CallFrom16Register, and CallFrom16Thunk.
1815 * CallFrom16 and CallFrom16Register are used by the 16->32 glue code
1816 * as described above. CallFrom16Thunk is a special variant used by
1817 * the implementation of the Win95 16->32 thunk functions C16ThkSL and
1818 * C16ThkSL01 and is implemented as follows:
1819 * On entry, the EBX register is set up to contain a flat pointer to the
1820 * 16-bit stack such that EBX+22 points to the first argument.
1821 * Then, the entry point is called, while EBP is set up to point
1822 * to the return address (on the 32-bit stack).
1823 * The called function returns with CX set to the number of bytes
1824 * to be popped of the caller's stack.
1826 * Stack layout upon entry to the core routine (STACK16FRAME):
1828 * (sp+22) word first 16-bit arg
1832 * (sp+12) long 32-bit entry point (reused for Win16 mutex recursion count)
1833 * (sp+8) long cs of 16-bit entry point
1834 * (sp+4) long ip of 16-bit entry point
1835 * (sp) long auxillary relay function address
1837 * Added on the stack:
1838 * (sp-2) word saved gs
1839 * (sp-4) word saved fs
1840 * (sp-6) word saved es
1841 * (sp-8) word saved ds
1842 * (sp-12) long saved ebp
1843 * (sp-16) long saved ecx
1844 * (sp-20) long saved edx
1845 * (sp-24) long saved previous stack
1847 static void BuildCallFrom16Core( FILE *outfile
, int reg_func
, int thunk
)
1849 char *name
= thunk
? "Thunk" : reg_func
? "Register" : "";
1851 /* Function header */
1852 fprintf( outfile
, "\n\t.align 4\n" );
1854 fprintf( outfile
, ".stabs \"CallFrom16%s:F1\",36,0,0," PREFIX
"CallFrom16%s\n",
1857 fprintf( outfile
, "\t.globl " PREFIX
"CallFrom16%s\n", name
);
1858 fprintf( outfile
, PREFIX
"CallFrom16%s:\n", name
);
1860 /* No relay function for 'thunk' */
1862 fprintf( outfile
, "\tpushl $0\n" );
1864 /* Create STACK16FRAME (except STACK32FRAME link) */
1865 fprintf( outfile
, "\tpushw %%gs\n" );
1866 fprintf( outfile
, "\tpushw %%fs\n" );
1867 fprintf( outfile
, "\tpushw %%es\n" );
1868 fprintf( outfile
, "\tpushw %%ds\n" );
1869 fprintf( outfile
, "\tpushl %%ebp\n" );
1870 fprintf( outfile
, "\tpushl %%ecx\n" );
1871 fprintf( outfile
, "\tpushl %%edx\n" );
1875 /* Get Global Offset Table into %ecx */
1876 fprintf( outfile
, "\tcall .LCallFrom16%s.getgot1\n", name
);
1877 fprintf( outfile
, ".LCallFrom16%s.getgot1:\n", name
);
1878 fprintf( outfile
, "\tpopl %%ecx\n" );
1879 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot1], %%ecx\n", name
);
1882 /* Load 32-bit segment registers */
1883 fprintf( outfile
, "\tmovw $0x%04x, %%dx\n", Data_Selector
);
1885 fprintf( outfile
, "\tdata16\n");
1887 fprintf( outfile
, "\tmovw %%dx, %%ds\n" );
1889 fprintf( outfile
, "\tdata16\n");
1891 fprintf( outfile
, "\tmovw %%dx, %%es\n" );
1895 fprintf( outfile
, "\tmovl " PREFIX
"SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
1896 fprintf( outfile
, "\tmovw (%%edx), %%fs\n" );
1899 fprintf( outfile
, "\tmovw " PREFIX
"SYSLEVEL_Win16CurrentTeb, %%fs\n" );
1901 /* Get address of ldt_copy array into %ecx */
1903 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy@GOT(%%ecx), %%ecx\n" );
1905 fprintf( outfile
, "\tmovl $" PREFIX
"ldt_copy, %%ecx\n" );
1907 /* Translate STACK16FRAME base to flat offset in %edx */
1908 fprintf( outfile
, "\tmovw %%ss, %%dx\n" );
1909 fprintf( outfile
, "\tandl $0xfff8, %%edx\n" );
1910 fprintf( outfile
, "\tmovl (%%ecx,%%edx), %%edx\n" );
1911 fprintf( outfile
, "\tmovzwl %%sp, %%ebp\n" );
1912 fprintf( outfile
, "\tleal -4(%%ebp,%%edx), %%edx\n" );
1913 /* -4 since STACK16FRAME not yet complete! */
1915 /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
1916 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET
);
1917 fprintf( outfile
, "\tpushl %%ebp\n" );
1921 fprintf( outfile
,"\tdata16\n");
1923 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET
+ 2 );
1924 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET
);
1925 fprintf( outfile
, "\tpushl %%ds\n" );
1926 fprintf( outfile
, "\tpopl %%ss\n" );
1927 fprintf( outfile
, "\tmovl %%ebp, %%esp\n" );
1928 fprintf( outfile
, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME
, ebp
) );
1932 STACK16FRAME is completely set up
1933 DS, ES, SS: flat data segment
1935 ESP: points to last STACK32FRAME
1936 EBP: points to ebp member of last STACK32FRAME
1937 EDX: points to current STACK16FRAME
1938 ECX: points to ldt_copy
1939 all other registers: unchanged */
1941 /* Special case: C16ThkSL stub */
1944 /* Set up registers as expected and call thunk */
1945 fprintf( outfile
, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME
)-22 );
1946 fprintf( outfile
, "\tleal -4(%%esp), %%ebp\n" );
1948 fprintf( outfile
, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point
) );
1950 /* Switch stack back */
1951 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
1952 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
1953 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
1955 /* Restore registers and return directly to caller */
1956 fprintf( outfile
, "\taddl $8, %%esp\n" );
1957 fprintf( outfile
, "\tpopl %%ebp\n" );
1958 fprintf( outfile
, "\tpopw %%ds\n" );
1959 fprintf( outfile
, "\tpopw %%es\n" );
1960 fprintf( outfile
, "\tpopw %%fs\n" );
1961 fprintf( outfile
, "\tpopw %%gs\n" );
1962 fprintf( outfile
, "\taddl $18, %%esp\n" );
1964 fprintf( outfile
, "\txorb %%ch, %%ch\n" );
1965 fprintf( outfile
, "\tpopl %%ebx\n" );
1966 fprintf( outfile
, "\taddw %%cx, %%sp\n" );
1967 fprintf( outfile
, "\tpush %%ebx\n" );
1969 fprintf( outfile
, "\t.byte 0x66\n" );
1970 fprintf( outfile
, "\tlret\n" );
1976 /* Build register CONTEXT */
1979 fprintf( outfile
, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86
) );
1981 fprintf( outfile
, "\tpushfl\n" );
1982 fprintf( outfile
, "\tpopl %d(%%esp)\n", CONTEXTOFFSET(EFlags
) );
1984 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax
) );
1985 fprintf( outfile
, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx
) );
1986 fprintf( outfile
, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi
) );
1987 fprintf( outfile
, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi
) );
1989 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp
) );
1990 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp
) );
1991 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx
) );
1992 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx
) );
1993 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx
) );
1994 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx
) );
1996 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds
) );
1997 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs
) );
1998 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es
) );
1999 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs
) );
2000 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs
) );
2001 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs
) );
2002 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs
) );
2003 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs
) );
2005 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs
) );
2006 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs
) );
2007 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip
) );
2008 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip
) );
2010 fprintf( outfile
, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET
+2 );
2011 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs
) );
2012 fprintf( outfile
, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET
);
2013 fprintf( outfile
, "\taddl $%d, %%eax\n", STACK16OFFSET(ip
) );
2014 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp
) );
2016 fprintf( outfile
, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave
) );
2021 /* Print debug info before call */
2026 fprintf( outfile
, "\tpushl %%ebx\n" );
2028 /* Get Global Offset Table into %ebx (for PLT call) */
2029 fprintf( outfile
, "\tcall .LCallFrom16%s.getgot2\n", name
);
2030 fprintf( outfile
, ".LCallFrom16%s.getgot2:\n", name
);
2031 fprintf( outfile
, "\tpopl %%ebx\n" );
2032 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot2], %%ebx\n", name
);
2035 fprintf( outfile
, "\tpushl %%ecx\n" );
2036 fprintf( outfile
, "\tpushl %%edx\n" );
2038 fprintf( outfile
, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2039 sizeof(CONTEXT
) + STRUCTOFFSET(STACK32FRAME
, ebp
) );
2041 fprintf( outfile
, "\tpushl $0\n" );
2044 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16@PLT\n ");
2046 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16\n ");
2048 fprintf( outfile
, "\tpopl %%edx\n" );
2049 fprintf( outfile
, "\tpopl %%edx\n" );
2050 fprintf( outfile
, "\tpopl %%ecx\n" );
2053 fprintf( outfile
, "\tpopl %%ebx\n" );
2056 /* Call *Thunk* relay routine (which will call the API entry point) */
2057 fprintf( outfile
, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay
) );
2059 /* Print debug info after call */
2064 fprintf( outfile
, "\tpushl %%ebx\n" );
2066 /* Get Global Offset Table into %ebx (for PLT call) */
2067 fprintf( outfile
, "\tcall .LCallFrom16%s.getgot3\n", name
);
2068 fprintf( outfile
, ".LCallFrom16%s.getgot3:\n", name
);
2069 fprintf( outfile
, "\tpopl %%ebx\n" );
2070 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot3], %%ebx\n", name
);
2073 fprintf( outfile
, "\tpushl %%eax\n" );
2075 fprintf( outfile
, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2076 sizeof(CONTEXT
) + STRUCTOFFSET(STACK32FRAME
, ebp
) );
2078 fprintf( outfile
, "\tpushl $0\n" );
2081 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret@PLT\n ");
2083 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret\n ");
2085 fprintf( outfile
, "\tpopl %%eax\n" );
2086 fprintf( outfile
, "\tpopl %%eax\n" );
2089 fprintf( outfile
, "\tpopl %%ebx\n" );
2095 fprintf( outfile
, "\tmovl %%esp, %%ebx\n" );
2097 /* Switch stack back */
2098 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2099 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
2100 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2102 /* Restore all registers from CONTEXT */
2103 fprintf( outfile
, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs
) );
2104 fprintf( outfile
, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp
) );
2105 fprintf( outfile
, "\tleal 4(%%esp, %%eax), %%esp\n" );
2107 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs
) );
2108 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip
) );
2109 fprintf( outfile
, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags
) );
2110 fprintf( outfile
, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs
) );
2112 fprintf( outfile
, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs
) );
2113 fprintf( outfile
, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs
) );
2114 fprintf( outfile
, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs
) );
2116 fprintf( outfile
, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp
) );
2117 fprintf( outfile
, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi
) );
2118 fprintf( outfile
, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi
) );
2119 fprintf( outfile
, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax
) );
2120 fprintf( outfile
, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx
) );
2121 fprintf( outfile
, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx
) );
2122 fprintf( outfile
, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx
) );
2124 fprintf( outfile
, "\tpopl %%ds\n" );
2125 fprintf( outfile
, "\tpopfl\n" );
2126 fprintf( outfile
, "\t.byte 0x66\n" );
2127 fprintf( outfile
, "\tlret\n" );
2131 /* Switch stack back */
2132 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2133 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
2134 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2136 /* Set flags according to return value */
2137 fprintf( outfile
, "\torl %%eax, %%eax\n" );
2139 /* Restore registers and return to *ThunkRet* routine */
2140 fprintf( outfile
, "\tpopl %%edx\n" );
2141 fprintf( outfile
, "\tpopl %%ecx\n" );
2142 fprintf( outfile
, "\tpopl %%ebp\n" );
2143 fprintf( outfile
, "\tpopw %%ds\n" );
2144 fprintf( outfile
, "\tpopw %%es\n" );
2145 fprintf( outfile
, "\tpopw %%fs\n" );
2146 fprintf( outfile
, "\tpopw %%gs\n" );
2147 fprintf( outfile
, "\tret $14\n" );
2152 /*******************************************************************
2155 * This routine builds the core routines used in 32->16 thunks:
2156 * CallTo16Word, CallTo16Long, CallTo16RegisterShort, and
2157 * CallTo16RegisterLong.
2159 * CallTo16Word and CallTo16Long are used by the 32->16 glue code
2160 * as described above. The register functions can be called directly:
2162 * extern void CALLBACK CallTo16RegisterShort( const CONTEXT86 *context, int nb_args );
2163 * extern void CALLBACK CallTo16RegisterLong ( const CONTEXT86 *context, int nb_args );
2165 * They call to 16-bit code with all registers except SS:SP set up as specified
2166 * by the 'context' structure, and SS:SP set to point to the current 16-bit
2167 * stack, decremented by the value specified in the 'nb_args' argument.
2170 static void BuildCallTo16Core( FILE *outfile
, int short_ret
, int reg_func
)
2172 char *name
= reg_func
== 2 ? "RegisterLong" :
2173 reg_func
== 1 ? "RegisterShort" :
2174 short_ret
? "Word" : "Long";
2176 /* Function header */
2177 fprintf( outfile
, "\n\t.align 4\n" );
2179 fprintf( outfile
, ".stabs \"CallTo16%s:F1\",36,0,0," PREFIX
"CallTo16%s\n",
2182 fprintf( outfile
, "\t.globl " PREFIX
"CallTo16%s\n", name
);
2183 fprintf( outfile
, PREFIX
"CallTo16%s:\n", name
);
2185 /* No relay stub for 'register' functions */
2187 fprintf( outfile
, "\tpushl $0\n" );
2189 /* Function entry sequence */
2190 fprintf( outfile
, "\tpushl %%ebp\n" );
2191 fprintf( outfile
, "\tmovl %%esp, %%ebp\n" );
2193 /* Save the 32-bit registers */
2194 fprintf( outfile
, "\tpushl %%ebx\n" );
2195 fprintf( outfile
, "\tpushl %%ecx\n" );
2196 fprintf( outfile
, "\tpushl %%edx\n" );
2197 fprintf( outfile
, "\tpushl %%esi\n" );
2198 fprintf( outfile
, "\tpushl %%edi\n" );
2202 /* Get Global Offset Table into %ebx */
2203 fprintf( outfile
, "\tcall .LCallTo16%s.getgot1\n", name
);
2204 fprintf( outfile
, ".LCallTo16%s.getgot1:\n", name
);
2205 fprintf( outfile
, "\tpopl %%ebx\n" );
2206 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot1], %%ebx\n", name
);
2209 /* Move relay target address to %edi */
2212 fprintf( outfile
, "\tmovl 4(%%ebp), %%edi\n" );
2213 fprintf( outfile
, "\taddl $3, %%edi\n" );
2216 /* Enter Win16 Mutex */
2218 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_EnterWin16Lock@PLT\n" );
2220 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_EnterWin16Lock\n" );
2222 /* Print debugging info */
2225 /* Push number of arguments (from relay stub) */
2227 fprintf( outfile
, "\tpushl $-1\n" );
2229 fprintf( outfile
, "\tpushl -12(%%edi)\n" );
2231 /* Push the address of the first argument */
2232 fprintf( outfile
, "\tleal 12(%%ebp),%%eax\n" );
2233 fprintf( outfile
, "\tpushl %%eax\n" );
2236 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16@PLT\n" );
2238 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16\n" );
2240 fprintf( outfile
, "\tpopl %%eax\n" );
2241 fprintf( outfile
, "\tpopl %%eax\n" );
2244 /* Get return address */
2246 fprintf( outfile
, "\tmovl " PREFIX
"CallTo16_RetAddr@GOTOFF(%%ebx), %%ecx\n" );
2248 fprintf( outfile
, "\tmovl " PREFIX
"CallTo16_RetAddr, %%ecx\n" );
2250 /* Call the actual CallTo16 routine (simulate a lcall) */
2251 fprintf( outfile
, "\tpushl %%cs\n" );
2252 fprintf( outfile
, "\tcall .LCallTo16%s\n", name
);
2254 /* Convert and push return value */
2257 fprintf( outfile
, "\tmovzwl %%ax, %%eax\n" );
2258 fprintf( outfile
, "\tpushl %%eax\n" );
2260 else if ( reg_func
!= 2 )
2262 fprintf( outfile
, "\tshll $16,%%edx\n" );
2263 fprintf( outfile
, "\tmovw %%ax,%%dx\n" );
2264 fprintf( outfile
, "\tpushl %%edx\n" );
2267 fprintf( outfile
, "\tpushl %%eax\n" );
2271 /* Get Global Offset Table into %ebx (might have been overwritten) */
2272 fprintf( outfile
, "\tcall .LCallTo16%s.getgot2\n", name
);
2273 fprintf( outfile
, ".LCallTo16%s.getgot2:\n", name
);
2274 fprintf( outfile
, "\tpopl %%ebx\n" );
2275 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot2], %%ebx\n", name
);
2278 /* Print debugging info */
2282 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16Ret@PLT\n" );
2284 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16Ret\n" );
2287 /* Leave Win16 Mutex */
2289 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_LeaveWin16Lock@PLT\n" );
2291 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_LeaveWin16Lock\n" );
2293 /* Get return value */
2294 fprintf( outfile
, "\tpopl %%eax\n" );
2296 /* Restore the 32-bit registers */
2297 fprintf( outfile
, "\tpopl %%edi\n" );
2298 fprintf( outfile
, "\tpopl %%esi\n" );
2299 fprintf( outfile
, "\tpopl %%edx\n" );
2300 fprintf( outfile
, "\tpopl %%ecx\n" );
2301 fprintf( outfile
, "\tpopl %%ebx\n" );
2303 /* Function exit sequence */
2304 fprintf( outfile
, "\tpopl %%ebp\n" );
2307 fprintf( outfile
, "\tret\n" ); /* return to relay return stub */
2310 fprintf( outfile
, "\taddl $4, %%esp\n" );
2311 fprintf( outfile
, "\tret $8\n" );
2315 /* Start of the actual CallTo16 routine */
2317 fprintf( outfile
, ".LCallTo16%s:\n", name
);
2319 /* Complete STACK32FRAME */
2320 fprintf( outfile
, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET
);
2321 fprintf( outfile
, "\tmovl %%esp,%%edx\n" );
2323 /* Switch to the 16-bit stack */
2325 fprintf( outfile
,"\tdata16\n");
2327 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2);
2328 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET
);
2329 fprintf( outfile
, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET
);
2333 /* Add the specified offset to the new sp */
2334 fprintf( outfile
, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(args
)+4 );
2336 /* Push the return address
2337 * With sreg suffix, we push 16:16 address (normal lret)
2338 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
2341 fprintf( outfile
, "\tpushl %%ecx\n" );
2344 fprintf( outfile
, "\tshldl $16, %%ecx, %%eax\n" );
2345 fprintf( outfile
, "\tpushw $0\n" );
2346 fprintf( outfile
, "\tpushw %%ax\n" );
2347 fprintf( outfile
, "\tpushw $0\n" );
2348 fprintf( outfile
, "\tpushw %%cx\n" );
2351 /* Push the called routine address */
2352 fprintf( outfile
, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(args
) );
2353 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs
) );
2354 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip
) );
2356 /* Get the registers */
2357 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs
) );
2358 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs
) );
2359 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2360 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs
) );
2361 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2362 fprintf( outfile
, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp
) );
2363 fprintf( outfile
, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi
) );
2364 fprintf( outfile
, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi
) );
2365 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax
) );
2366 fprintf( outfile
, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx
) );
2367 fprintf( outfile
, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx
) );
2368 fprintf( outfile
, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx
) );
2370 /* Get the 16-bit ds */
2371 fprintf( outfile
, "\tpopw %%ds\n" );
2373 /* Jump to the called routine */
2374 fprintf( outfile
, "\t.byte 0x66\n" );
2375 fprintf( outfile
, "\tlret\n" );
2377 else /* not a register function */
2379 /* Make %bp point to the previous stackframe (built by CallFrom16) */
2380 fprintf( outfile
, "\tmovzwl %%sp,%%ebp\n" );
2381 fprintf( outfile
, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp
) );
2383 /* Set %fs to the value saved by the last CallFrom16 */
2384 fprintf( outfile
, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs
)-STACK16OFFSET(bp
) );
2385 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2387 /* Jump to the relay code */
2388 fprintf( outfile
, "\tjmp *%%edi\n" );
2392 /*******************************************************************
2395 * Build the return code for 16-bit callbacks
2397 static void BuildRet16Func( FILE *outfile
)
2400 * Note: This must reside in the .data section to allow
2401 * run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
2404 fprintf( outfile
, "\n\t.globl " PREFIX
"CallTo16_Ret\n" );
2405 fprintf( outfile
, PREFIX
"CallTo16_Ret:\n" );
2407 /* Restore 32-bit segment registers */
2409 fprintf( outfile
, "\tmovw $0x%04x,%%bx\n", Data_Selector
);
2411 fprintf( outfile
, "\tdata16\n");
2413 fprintf( outfile
, "\tmovw %%bx,%%ds\n" );
2415 fprintf( outfile
, "\tdata16\n");
2417 fprintf( outfile
, "\tmovw %%bx,%%es\n" );
2419 fprintf( outfile
, "\tmovw " PREFIX
"SYSLEVEL_Win16CurrentTeb,%%fs\n" );
2421 /* Restore the 32-bit stack */
2424 fprintf( outfile
, "\tdata16\n");
2426 fprintf( outfile
, "\tmovw %%bx,%%ss\n" );
2427 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET
);
2428 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2430 /* Return to caller */
2432 fprintf( outfile
, "\tlret\n" );
2434 /* Declare the return address variable */
2436 fprintf( outfile
, "\n\t.globl " PREFIX
"CallTo16_RetAddr\n" );
2437 fprintf( outfile
, PREFIX
"CallTo16_RetAddr:\t.long 0\n" );
2440 /*******************************************************************
2441 * BuildCallTo32CBClient
2443 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
2445 * Since the relay stub is itself 32-bit, this should not be a problem;
2446 * unfortunately, the relay stubs are expected to switch back to a
2447 * 16-bit stack (and 16-bit code) after completion :-(
2449 * This would conflict with our 16- vs. 32-bit stack handling, so
2450 * we simply switch *back* to our 32-bit stack before returning to
2453 * The CBClient relay stub expects to be called with the following
2454 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
2455 * stack at the designated places:
2458 * (ebp+14) original arguments to the callback routine
2459 * (ebp+10) far return address to original caller
2460 * (ebp+6) Thunklet target address
2461 * (ebp+2) Thunklet relay ID code
2462 * (ebp) BP (saved by CBClientGlueSL)
2463 * (ebp-2) SI (saved by CBClientGlueSL)
2464 * (ebp-4) DI (saved by CBClientGlueSL)
2465 * (ebp-6) DS (saved by CBClientGlueSL)
2467 * ... buffer space used by the 16-bit side glue for temp copies
2469 * (ebx+4) far return address to 16-bit side glue code
2470 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
2472 * The 32-bit side glue code accesses both the original arguments (via ebp)
2473 * and the temporary copies prepared by the 16-bit side glue (via ebx).
2474 * After completion, the stub will load ss:sp from the buffer at ebx
2475 * and perform a far return to 16-bit code.
2477 * To trick the relay stub into returning to us, we replace the 16-bit
2478 * return address to the glue code by a cs:ip pair pointing to our
2479 * return entry point (the original return address is saved first).
2480 * Our return stub thus called will then reload the 32-bit ss:esp and
2481 * return to 32-bit code (by using and ss:esp value that we have also
2482 * pushed onto the 16-bit stack before and a cs:eip values found at
2483 * that position on the 32-bit stack). The ss:esp to be restored is
2484 * found relative to the 16-bit stack pointer at:
2487 * (ebx-8) sp (32-bit stack pointer)
2489 * The second variant of this routine, CALL32_CBClientEx, which is used
2490 * to implement KERNEL.621, has to cope with yet another problem: Here,
2491 * the 32-bit side directly returns to the caller of the CBClient thunklet,
2492 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
2493 * As we have to return to our 32-bit code first, we have to adapt the
2494 * layout of our temporary area so as to include values for the registers
2495 * that are to be restored, and later (in the implementation of KERNEL.621)
2496 * we *really* restore them. The return stub restores DS, DI, SI, and BP
2497 * from the stack, skips the next 8 bytes (CBClient relay code / target),
2498 * and then performs a lret NN, where NN is the number of arguments to be
2499 * removed. Thus, we prepare our temporary area as follows:
2501 * (ebx+22) 16-bit cs (this segment)
2502 * (ebx+20) 16-bit ip ('16-bit' return entry point)
2503 * (ebx+16) 32-bit ss (flat)
2504 * (ebx+12) 32-bit sp (32-bit stack pointer)
2505 * (ebx+10) 16-bit bp (points to ebx+24)
2506 * (ebx+8) 16-bit si (ignored)
2507 * (ebx+6) 16-bit di (ignored)
2508 * (ebx+4) 16-bit ds (we actually use the flat DS here)
2509 * (ebx+2) 16-bit ss (16-bit stack segment)
2510 * (ebx+0) 16-bit sp (points to ebx+4)
2512 * Note that we ensure that DS is not changed and remains the flat segment,
2513 * and the 32-bit stack pointer our own return stub needs fits just
2514 * perfectly into the 8 bytes that are skipped by the Windows stub.
2515 * One problem is that we have to determine the number of removed arguments,
2516 * as these have to be really removed in KERNEL.621. Thus, the BP value
2517 * that we place in the temporary area to be restored, contains the value
2518 * that SP would have if no arguments were removed. By comparing the actual
2519 * value of SP with this value in our return stub we can compute the number
2520 * of removed arguments. This is then returned to KERNEL.621.
2522 * The stack layout of this function:
2523 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
2524 * (ebp+16) esi pointer to caller's esi value
2525 * (ebp+12) arg ebp value to be set for relay stub
2526 * (ebp+8) func CBClient relay stub address
2530 static void BuildCallTo32CBClient( FILE *outfile
, BOOL isEx
)
2532 char *name
= isEx
? "CBClientEx" : "CBClient";
2533 int size
= isEx
? 24 : 12;
2535 /* Function header */
2537 fprintf( outfile
, "\n\t.align 4\n" );
2539 fprintf( outfile
, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX
"CALL32_%s\n",
2542 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_%s\n", name
);
2543 fprintf( outfile
, PREFIX
"CALL32_%s:\n", name
);
2547 fprintf( outfile
, "\tpushl %%ebp\n" );
2548 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2549 fprintf( outfile
, "\tpushl %%edi\n" );
2550 fprintf( outfile
, "\tpushl %%esi\n" );
2551 fprintf( outfile
, "\tpushl %%ebx\n" );
2553 /* Get the 16-bit stack */
2555 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET
);
2557 /* Convert it to a flat address */
2559 fprintf( outfile
, "\tshldl $16,%%ebx,%%eax\n" );
2560 fprintf( outfile
, "\tandl $0xfff8,%%eax\n" );
2561 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy(%%eax),%%esi\n" );
2562 fprintf( outfile
, "\tmovw %%bx,%%ax\n" );
2563 fprintf( outfile
, "\taddl %%eax,%%esi\n" );
2565 /* Allocate temporary area (simulate STACK16_PUSH) */
2567 fprintf( outfile
, "\tpushf\n" );
2568 fprintf( outfile
, "\tcld\n" );
2569 fprintf( outfile
, "\tleal -%d(%%esi), %%edi\n", size
);
2570 fprintf( outfile
, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME
) );
2571 fprintf( outfile
, "\trep\n\tmovsb\n" );
2572 fprintf( outfile
, "\tpopf\n" );
2574 fprintf( outfile
, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size
, STACKOFFSET
);
2576 fprintf( outfile
, "\tpushl %%edi\n" ); /* remember address */
2578 /* Set up temporary area */
2582 fprintf( outfile
, "\tleal 4(%%edi), %%edi\n" );
2584 fprintf( outfile
, "\tleal -8(%%esp), %%eax\n" );
2585 fprintf( outfile
, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */
2587 fprintf( outfile
, "\tmovl %%ss, %%ax\n" );
2588 fprintf( outfile
, "\tandl $0x0000ffff, %%eax\n" );
2589 fprintf( outfile
, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */
2591 fprintf( outfile
, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME
)-size
+4 + 4 );
2592 fprintf( outfile
, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */
2594 fprintf( outfile
, "\tmovl " PREFIX
"CALL32_%s_RetAddr, %%eax\n", name
);
2595 fprintf( outfile
, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */
2599 fprintf( outfile
, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME
)-size
+4 );
2600 fprintf( outfile
, "\tmovl %%ebx, 0(%%edi)\n" );
2602 fprintf( outfile
, "\tmovl %%ds, %%ax\n" );
2603 fprintf( outfile
, "\tmovw %%ax, 4(%%edi)\n" );
2605 fprintf( outfile
, "\taddl $20, %%ebx\n" );
2606 fprintf( outfile
, "\tmovw %%bx, 10(%%edi)\n" );
2608 fprintf( outfile
, "\tleal -8(%%esp), %%eax\n" );
2609 fprintf( outfile
, "\tmovl %%eax, 12(%%edi)\n" );
2611 fprintf( outfile
, "\tmovl %%ss, %%ax\n" );
2612 fprintf( outfile
, "\tandl $0x0000ffff, %%eax\n" );
2613 fprintf( outfile
, "\tmovl %%eax, 16(%%edi)\n" );
2615 fprintf( outfile
, "\tmovl " PREFIX
"CALL32_%s_RetAddr, %%eax\n", name
);
2616 fprintf( outfile
, "\tmovl %%eax, 20(%%edi)\n" );
2619 /* Set up registers and call CBClient relay stub (simulating a far call) */
2621 fprintf( outfile
, "\tmovl 16(%%ebp), %%esi\n" );
2622 fprintf( outfile
, "\tmovl (%%esi), %%esi\n" );
2624 fprintf( outfile
, "\tmovl %%edi, %%ebx\n" );
2625 fprintf( outfile
, "\tmovl 8(%%ebp), %%eax\n" );
2626 fprintf( outfile
, "\tmovl 12(%%ebp), %%ebp\n" );
2628 fprintf( outfile
, "\tpushl %%cs\n" );
2629 fprintf( outfile
, "\tcall *%%eax\n" );
2631 /* Return new esi value to caller */
2633 fprintf( outfile
, "\tmovl 32(%%esp), %%edi\n" );
2634 fprintf( outfile
, "\tmovl %%esi, (%%edi)\n" );
2636 /* Cleanup temporary area (simulate STACK16_POP) */
2638 fprintf( outfile
, "\tpop %%esi\n" );
2640 fprintf( outfile
, "\tpushf\n" );
2641 fprintf( outfile
, "\tstd\n" );
2642 fprintf( outfile
, "\tdec %%esi\n" );
2643 fprintf( outfile
, "\tleal %d(%%esi), %%edi\n", size
);
2644 fprintf( outfile
, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME
) );
2645 fprintf( outfile
, "\trep\n\tmovsb\n" );
2646 fprintf( outfile
, "\tpopf\n" );
2648 fprintf( outfile
, "\t.byte 0x64\n\taddw $%d,(%d)\n", size
, STACKOFFSET
);
2650 /* Return argument size to caller */
2653 fprintf( outfile
, "\tmovl 32(%%esp), %%ebx\n" );
2654 fprintf( outfile
, "\tmovl %%ebp, (%%ebx)\n" );
2657 /* Restore registers and return */
2659 fprintf( outfile
, "\tpopl %%ebx\n" );
2660 fprintf( outfile
, "\tpopl %%esi\n" );
2661 fprintf( outfile
, "\tpopl %%edi\n" );
2662 fprintf( outfile
, "\tpopl %%ebp\n" );
2663 fprintf( outfile
, "\tret\n" );
2666 static void BuildCallTo32CBClientRet( FILE *outfile
, BOOL isEx
)
2668 char *name
= isEx
? "CBClientEx" : "CBClient";
2670 /* '16-bit' return stub */
2672 fprintf( outfile
, "\n\t.globl " PREFIX
"CALL32_%s_Ret\n", name
);
2673 fprintf( outfile
, PREFIX
"CALL32_%s_Ret:\n", name
);
2677 fprintf( outfile
, "\tmovzwl %%sp, %%ebx\n" );
2678 fprintf( outfile
, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
2682 fprintf( outfile
, "\tmovzwl %%bp, %%ebx\n" );
2683 fprintf( outfile
, "\tsubw %%bp, %%sp\n" );
2684 fprintf( outfile
, "\tmovzwl %%sp, %%ebp\n" );
2685 fprintf( outfile
, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
2687 fprintf( outfile
, "\tlret\n" );
2689 /* Declare the return address variable */
2691 fprintf( outfile
, "\n\t.globl " PREFIX
"CALL32_%s_RetAddr\n", name
);
2692 fprintf( outfile
, PREFIX
"CALL32_%s_RetAddr:\t.long 0\n", name
);
2696 /*******************************************************************
2697 * BuildCallTo32LargeStack
2699 * Build the function used to switch to the original 32-bit stack
2700 * before calling a 32-bit function from 32-bit code. This is used for
2701 * functions that need a large stack, like X bitmaps functions.
2703 * The generated function has the following prototype:
2704 * int xxx( int (*func)(), void *arg );
2706 * The pointer to the function can be retrieved by calling CALL32_Init,
2707 * which also takes care of saving the current 32-bit stack pointer.
2708 * Furthermore, CALL32_Init switches to a new stack and jumps to the
2709 * specified target address.
2711 * NOTE: The CALL32_LargeStack routine may be recursively entered by the
2712 * same thread, but not concurrently entered by several threads.
2714 * Stack layout of CALL32_Init:
2716 * (esp+12) new stack address
2717 * (esp+8) target address
2718 * (esp+4) pointer to variable to receive CALL32_LargeStack address
2721 * Stack layout of CALL32_LargeStack:
2728 static void BuildCallTo32LargeStack( FILE *outfile
)
2730 /* Initialization function */
2732 fprintf( outfile
, "\n\t.align 4\n" );
2734 fprintf( outfile
, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX
"CALL32_Init\n");
2736 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Init\n" );
2737 fprintf( outfile
, "\t.type " PREFIX
"CALL32_Init,@function\n" );
2738 fprintf( outfile
, PREFIX
"CALL32_Init:\n" );
2739 fprintf( outfile
, "\tmovl %%esp,CALL32_Original32_esp\n" );
2740 fprintf( outfile
, "\tpopl %%eax\n" );
2741 fprintf( outfile
, "\tpopl %%eax\n" );
2742 fprintf( outfile
, "\tmovl $CALL32_LargeStack,(%%eax)\n" );
2743 fprintf( outfile
, "\tpopl %%eax\n" );
2744 fprintf( outfile
, "\tpopl %%esp\n" );
2745 fprintf( outfile
, "\tpushl %%eax\n" );
2746 fprintf( outfile
, "\tret\n" );
2748 /* Function header */
2750 fprintf( outfile
, "\n\t.align 4\n" );
2752 fprintf( outfile
, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2754 fprintf( outfile
, "CALL32_LargeStack:\n" );
2758 fprintf( outfile
, "\tpushl %%ebp\n" );
2759 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2761 /* Switch to the original 32-bit stack pointer */
2763 fprintf( outfile
, "\tcmpl $0, CALL32_RecursionCount\n" );
2764 fprintf( outfile
, "\tjne CALL32_skip\n" );
2765 fprintf( outfile
, "\tmovl CALL32_Original32_esp, %%esp\n" );
2766 fprintf( outfile
, "CALL32_skip:\n" );
2768 fprintf( outfile
, "\tincl CALL32_RecursionCount\n" );
2770 /* Transfer the argument and call the function */
2772 fprintf( outfile
, "\tpushl 12(%%ebp)\n" );
2773 fprintf( outfile
, "\tcall *8(%%ebp)\n" );
2775 /* Restore registers and return */
2777 fprintf( outfile
, "\tdecl CALL32_RecursionCount\n" );
2779 fprintf( outfile
, "\tmovl %%ebp,%%esp\n" );
2780 fprintf( outfile
, "\tpopl %%ebp\n" );
2781 fprintf( outfile
, "\tret\n" );
2785 fprintf( outfile
, "\t.data\n" );
2786 fprintf( outfile
, "CALL32_Original32_esp:\t.long 0\n" );
2787 fprintf( outfile
, "CALL32_RecursionCount:\t.long 0\n" );
2788 fprintf( outfile
, "\t.text\n" );
2792 /*******************************************************************
2793 * BuildCallFrom32Regs
2795 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2796 * 'args' is the number of dword arguments.
2800 * (ebp+12) first arg
2801 * (ebp+8) ret addr to user code
2802 * (ebp+4) ret addr to relay code
2804 * (ebp-128) buffer area to allow stack frame manipulation
2805 * (ebp-332) CONTEXT86 struct
2806 * (ebp-336) CONTEXT86 *argument
2807 * .... other arguments copied from (ebp+12)
2809 * The entry point routine is called with a CONTEXT* extra argument,
2810 * following the normal args. In this context structure, EIP_reg
2811 * contains the return address to user code, and ESP_reg the stack
2812 * pointer on return (with the return address and arguments already
2815 static void BuildCallFrom32Regs( FILE *outfile
)
2817 static const int STACK_SPACE
= 128 + sizeof(CONTEXT86
);
2819 /* Function header */
2821 fprintf( outfile
, "\n\t.align 4\n" );
2823 fprintf( outfile
, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX
"CALL32_Regs\n" );
2825 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Regs\n" );
2826 fprintf( outfile
, PREFIX
"CALL32_Regs:\n" );
2828 /* Allocate some buffer space on the stack */
2830 fprintf( outfile
, "\tpushl %%ebp\n" );
2831 fprintf( outfile
, "\tmovl %%esp,%%ebp\n ");
2832 fprintf( outfile
, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE
);
2834 /* Build the context structure */
2836 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax
) - STACK_SPACE
);
2837 fprintf( outfile
, "\tpushfl\n" );
2838 fprintf( outfile
, "\tpopl %%eax\n" );
2839 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags
) - STACK_SPACE
);
2840 fprintf( outfile
, "\tmovl 0(%%ebp),%%eax\n" );
2841 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp
) - STACK_SPACE
);
2842 fprintf( outfile
, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx
) - STACK_SPACE
);
2843 fprintf( outfile
, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx
) - STACK_SPACE
);
2844 fprintf( outfile
, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx
) - STACK_SPACE
);
2845 fprintf( outfile
, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi
) - STACK_SPACE
);
2846 fprintf( outfile
, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi
) - STACK_SPACE
);
2848 fprintf( outfile
, "\txorl %%eax,%%eax\n" );
2849 fprintf( outfile
, "\tmovw %%cs,%%ax\n" );
2850 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs
) - STACK_SPACE
);
2851 fprintf( outfile
, "\tmovw %%es,%%ax\n" );
2852 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs
) - STACK_SPACE
);
2853 fprintf( outfile
, "\tmovw %%fs,%%ax\n" );
2854 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs
) - STACK_SPACE
);
2855 fprintf( outfile
, "\tmovw %%gs,%%ax\n" );
2856 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs
) - STACK_SPACE
);
2857 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
2858 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs
) - STACK_SPACE
);
2859 fprintf( outfile
, "\tmovw %%ds,%%ax\n" );
2860 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs
) - STACK_SPACE
);
2861 fprintf( outfile
, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
2863 fprintf( outfile
, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL
);
2864 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags
) - STACK_SPACE
);
2866 fprintf( outfile
, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
2867 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip
) - STACK_SPACE
);
2869 /* Transfer the arguments */
2871 fprintf( outfile
, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
2872 fprintf( outfile
, "\tpushl %%esp\n" ); /* push ptr to context struct */
2873 fprintf( outfile
, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
2874 fprintf( outfile
, "\tjecxz 1f\n" );
2875 fprintf( outfile
, "\tsubl %%ecx,%%esp\n" );
2876 fprintf( outfile
, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */
2877 fprintf( outfile
, "\tmovl %%esp,%%edi\n" );
2878 fprintf( outfile
, "\tshrl $2,%%ecx\n" );
2879 fprintf( outfile
, "\tcld\n" );
2880 fprintf( outfile
, "\trep\n\tmovsl\n" ); /* copy args */
2882 fprintf( outfile
, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
2883 fprintf( outfile
, "\tleal 12(%%ebp,%%eax),%%eax\n" );
2884 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2886 /* Call the entry point */
2888 fprintf( outfile
, "\tcall *0(%%ebx)\n" );
2890 /* Store %eip and %ebp onto the new stack */
2892 fprintf( outfile
, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2893 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip
) - STACK_SPACE
);
2894 fprintf( outfile
, "\tmovl %%eax,-4(%%edx)\n" );
2895 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp
) - STACK_SPACE
);
2896 fprintf( outfile
, "\tmovl %%eax,-8(%%edx)\n" );
2898 /* Restore the context structure */
2900 /* Note: we don't bother to restore %cs, %ds and %ss
2901 * changing them in 32-bit code is a recipe for disaster anyway
2903 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs
) - STACK_SPACE
);
2904 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2905 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs
) - STACK_SPACE
);
2906 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2907 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs
) - STACK_SPACE
);
2908 fprintf( outfile
, "\tmovw %%ax,%%gs\n" );
2910 fprintf( outfile
, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi
) - STACK_SPACE
);
2911 fprintf( outfile
, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi
) - STACK_SPACE
);
2912 fprintf( outfile
, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx
) - STACK_SPACE
);
2913 fprintf( outfile
, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx
) - STACK_SPACE
);
2914 fprintf( outfile
, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx
) - STACK_SPACE
);
2916 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags
) - STACK_SPACE
);
2917 fprintf( outfile
, "\tpushl %%eax\n" );
2918 fprintf( outfile
, "\tpopfl\n" );
2919 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax
) - STACK_SPACE
);
2921 fprintf( outfile
, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2922 fprintf( outfile
, "\tleal -8(%%ebp),%%esp\n" );
2923 fprintf( outfile
, "\tpopl %%ebp\n" );
2924 fprintf( outfile
, "\tret\n" );
2928 /*******************************************************************
2931 * Build the spec files
2933 static int BuildSpec( FILE *outfile
, int argc
, char *argv
[] )
2936 for (i
= 2; i
< argc
; i
++)
2937 if (BuildSpecFile( outfile
, argv
[i
] ) < 0) return -1;
2941 /*******************************************************************
2944 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
2946 static int BuildGlue( FILE *outfile
, char * outname
, int argc
, char *argv
[] )
2953 infile
= fopen( argv
[2], "r" );
2960 else infile
= stdin
;
2964 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2965 fprintf( outfile
, "\t.text\n" );
2970 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2971 getcwd(buffer
, sizeof(buffer
));
2974 * The stabs help the internal debugger as they are an indication that it
2975 * is sensible to step into a thunk/trampoline.
2977 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2978 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2979 fprintf( outfile
, "\t.text\n" );
2980 fprintf( outfile
, "\t.align 4\n" );
2981 fprintf( outfile
, "Code_Start:\n\n" );
2984 /* Build the callback glue functions */
2986 while (fgets( buffer
, sizeof(buffer
), infile
))
2988 if (strstr( buffer
, "### start build ###" )) break;
2990 while (fgets( buffer
, sizeof(buffer
), infile
))
2993 if ( (p
= strstr( buffer
, "CallFrom16_" )) != NULL
)
2995 char *q
, *profile
= p
+ strlen( "CallFrom16_" );
2996 for (q
= profile
; (*q
== '_') || isalpha(*q
); q
++ )
2999 for (q
= p
-1; q
> buffer
&& ((*q
== '_') || isalnum(*q
)); q
-- )
3001 if ( ++q
< p
) p
[-1] = '\0'; else q
= "";
3002 BuildCallFrom16Func( outfile
, profile
, q
);
3004 if ( (p
= strstr( buffer
, "CallTo16_" )) != NULL
)
3006 char *q
, *profile
= p
+ strlen( "CallTo16_" );
3007 for (q
= profile
; (*q
== '_') || isalpha(*q
); q
++ )
3010 for (q
= p
-1; q
> buffer
&& ((*q
== '_') || isalnum(*q
)); q
-- )
3012 if ( ++q
< p
) p
[-1] = '\0'; else q
= "";
3013 BuildCallTo16Func( outfile
, profile
, q
);
3015 if (strstr( buffer
, "### stop build ###" )) break;
3020 fprintf( outfile
, "\t.text\n");
3021 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
3022 fprintf( outfile
, ".Letext:\n");
3025 #else /* __i386__ */
3027 /* Just to avoid an empty file */
3028 fprintf( outfile
, "\t.long 0\n" );
3030 #endif /* __i386__ */
3036 /*******************************************************************
3039 * Build the 16-bit callbacks
3041 static int BuildCall16( FILE *outfile
, char * outname
)
3049 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
3050 fprintf( outfile
, "\t.text\n" );
3055 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
3056 getcwd(buffer
, sizeof(buffer
));
3059 * The stabs help the internal debugger as they are an indication that it
3060 * is sensible to step into a thunk/trampoline.
3062 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
3063 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
3064 fprintf( outfile
, "\t.text\n" );
3065 fprintf( outfile
, "\t.align 4\n" );
3066 fprintf( outfile
, "Code_Start:\n\n" );
3068 fprintf( outfile
, PREFIX
"Call16_Start:\n" );
3069 fprintf( outfile
, "\t.globl "PREFIX
"Call16_Start\n" );
3070 fprintf( outfile
, "\t.byte 0\n\n" );
3073 /* Standard CallFrom16 routine */
3074 BuildCallFrom16Core( outfile
, FALSE
, FALSE
);
3076 /* Register CallFrom16 routine */
3077 BuildCallFrom16Core( outfile
, TRUE
, FALSE
);
3079 /* C16ThkSL CallFrom16 routine */
3080 BuildCallFrom16Core( outfile
, FALSE
, TRUE
);
3082 /* Standard CallTo16 routine (WORD return) */
3083 BuildCallTo16Core( outfile
, TRUE
, FALSE
);
3085 /* Standard CallTo16 routine (DWORD return) */
3086 BuildCallTo16Core( outfile
, FALSE
, FALSE
);
3088 /* Register CallTo16 routine (16:16 retf) */
3089 BuildCallTo16Core( outfile
, FALSE
, 1 );
3091 /* Register CallTo16 routine (16:32 retf) */
3092 BuildCallTo16Core( outfile
, FALSE
, 2 );
3094 /* CBClientThunkSL routine */
3095 BuildCallTo32CBClient( outfile
, FALSE
);
3097 /* CBClientThunkSLEx routine */
3098 BuildCallTo32CBClient( outfile
, TRUE
);
3100 fprintf( outfile
, PREFIX
"Call16_End:\n" );
3101 fprintf( outfile
, "\t.globl "PREFIX
"Call16_End\n" );
3104 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
3105 fprintf( outfile
, ".Letext:\n");
3108 /* The whole Call16_Ret segment must lie within the .data section */
3109 fprintf( outfile
, "\n\t.data\n" );
3110 fprintf( outfile
, "\t.globl " PREFIX
"Call16_Ret_Start\n" );
3111 fprintf( outfile
, PREFIX
"Call16_Ret_Start:\n" );
3113 /* Standard CallTo16 return stub */
3114 BuildRet16Func( outfile
);
3116 /* CBClientThunkSL return stub */
3117 BuildCallTo32CBClientRet( outfile
, FALSE
);
3119 /* CBClientThunkSLEx return stub */
3120 BuildCallTo32CBClientRet( outfile
, TRUE
);
3122 /* End of Call16_Ret segment */
3123 fprintf( outfile
, "\n\t.globl " PREFIX
"Call16_Ret_End\n" );
3124 fprintf( outfile
, PREFIX
"Call16_Ret_End:\n" );
3126 #else /* __i386__ */
3128 fprintf( outfile
, PREFIX
"Call16_Start:\n" );
3129 fprintf( outfile
, "\t.globl "PREFIX
"Call16_Start\n" );
3130 fprintf( outfile
, "\t.byte 0\n\n" );
3131 fprintf( outfile
, PREFIX
"Call16_End:\n" );
3132 fprintf( outfile
, "\t.globl "PREFIX
"Call16_End\n" );
3134 fprintf( outfile
, "\t.globl " PREFIX
"Call16_Ret_Start\n" );
3135 fprintf( outfile
, PREFIX
"Call16_Ret_Start:\n" );
3136 fprintf( outfile
, "\t.byte 0\n\n" );
3137 fprintf( outfile
, "\n\t.globl " PREFIX
"Call16_Ret_End\n" );
3138 fprintf( outfile
, PREFIX
"Call16_Ret_End:\n" );
3140 #endif /* __i386__ */
3145 /*******************************************************************
3148 * Build the 32-bit callbacks
3150 static int BuildCall32( FILE *outfile
, char * outname
)
3158 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
3159 fprintf( outfile
, "\t.text\n" );
3164 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
3165 getcwd(buffer
, sizeof(buffer
));
3168 * The stabs help the internal debugger as they are an indication that it
3169 * is sensible to step into a thunk/trampoline.
3171 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
3172 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
3173 fprintf( outfile
, "\t.text\n" );
3174 fprintf( outfile
, "\t.align 4\n" );
3175 fprintf( outfile
, "Code_Start:\n" );
3178 /* Build the 32-bit large stack callback */
3180 BuildCallTo32LargeStack( outfile
);
3182 /* Build the register callback function */
3184 BuildCallFrom32Regs( outfile
);
3187 fprintf( outfile
, "\t.text\n");
3188 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
3189 fprintf( outfile
, ".Letext:\n");
3192 #else /* __i386__ */
3194 /* Just to avoid an empty file */
3195 fprintf( outfile
, "\t.long 0\n" );
3197 #endif /* __i386__ */
3202 /*******************************************************************
3205 static void usage(void)
3208 "usage: build [-pic] [-o outfile] -spec SPECNAMES\n"
3209 " build [-pic] [-o outfile] -glue SOURCE_FILE\n"
3210 " build [-pic] [-o outfile] -call16\n"
3211 " build [-pic] [-o outfile] -call32\n" );
3216 /*******************************************************************
3219 int main(int argc
, char **argv
)
3221 char *outname
= NULL
;
3222 FILE *outfile
= stdout
;
3225 if (argc
< 2) usage();
3227 if (!strcmp( argv
[1], "-pic" ))
3232 if (argc
< 2) usage();
3235 if (!strcmp( argv
[1], "-o" ))
3240 if (argc
< 2) usage();
3241 if (!(outfile
= fopen( outname
, "w" )))
3243 fprintf( stderr
, "Unable to create output file '%s'\n", outname
);
3248 /* Retrieve the selector values; this assumes that we are building
3249 * the asm files on the platform that will also run them. Probably
3250 * a safe assumption to make.
3252 GET_CS( Code_Selector
);
3253 GET_DS( Data_Selector
);
3255 if (!strcmp( argv
[1], "-spec" ))
3256 res
= BuildSpec( outfile
, argc
, argv
);
3257 else if (!strcmp( argv
[1], "-glue" ))
3258 res
= BuildGlue( outfile
, outname
, argc
, argv
);
3259 else if (!strcmp( argv
[1], "-call16" ))
3260 res
= BuildCall16( outfile
, outname
);
3261 else if (!strcmp( argv
[1], "-call32" ))
3262 res
= BuildCall32( outfile
, outname
);