2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Martin von Loewis
4 * Copyright 1995, 1996, 1997 Alexandre Julliard
5 * Copyright 1997 Eric Youngdale
19 #include "selectors.h"
20 #include "stackframe.h"
23 #ifdef NEED_UNDERSCORE_PREFIX
29 #ifdef HAVE_ASM_STRING
30 # define STRING ".string"
32 # define STRING ".ascii"
35 #if defined(__GNUC__) && !defined(__svr4__)
44 TYPE_BYTE
, /* byte variable (Win16) */
45 TYPE_WORD
, /* word variable (Win16) */
46 TYPE_LONG
, /* long variable (Win16) */
47 TYPE_PASCAL_16
, /* pascal function with 16-bit return (Win16) */
48 TYPE_PASCAL
, /* pascal function with 32-bit return (Win16) */
49 TYPE_ABS
, /* absolute value (Win16) */
50 TYPE_RETURN
, /* simple return value function (Win16) */
51 TYPE_REGISTER
, /* register function */
52 TYPE_STUB
, /* unimplemented stub */
53 TYPE_STDCALL
, /* stdcall function (Win32) */
54 TYPE_CDECL
, /* cdecl function (Win32) */
55 TYPE_VARARGS
, /* varargs function (Win32) */
56 TYPE_EXTERN
, /* external symbol (Win32) */
60 static const char * const TypeNames
[TYPE_NBTYPES
] =
63 "byte", /* TYPE_BYTE */
64 "word", /* TYPE_WORD */
65 "long", /* TYPE_LONG */
66 "pascal16", /* TYPE_PASCAL_16 */
67 "pascal", /* TYPE_PASCAL */
68 "equate", /* TYPE_ABS */
69 "return", /* TYPE_RETURN */
70 "register", /* TYPE_REGISTER */
71 "stub", /* TYPE_STUB */
72 "stdcall", /* TYPE_STDCALL */
73 "cdecl", /* TYPE_CDECL */
74 "varargs", /* TYPE_VARARGS */
75 "extern" /* TYPE_EXTERN */
78 #define MAX_ORDINALS 2048
80 /* Callback function used for stub functions */
81 #define STUB_CALLBACK \
82 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
142 static ORDDEF OrdinalDefinitions
[MAX_ORDINALS
];
144 static SPEC_TYPE SpecType
= SPEC_INVALID
;
145 static char DLLName
[80];
146 static char DLLFileName
[80];
147 static int Limit
= 0;
148 static int Base
= MAX_ORDINALS
;
149 static int DLLHeapSize
= 0;
150 static char *SpecName
;
152 static WORD Code_Selector
, Data_Selector
;
153 static char DLLInitFunc
[80];
155 char *ParseBuffer
= NULL
;
160 static int debugging
= 1;
162 /* Offset of a structure field relative to the start of the struct */
163 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
165 /* Offset of register relative to the start of the CONTEXT struct */
166 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT,reg)
168 /* Offset of the stack pointer relative to %fs:(0) */
169 #define STACKOFFSET (STRUCTOFFSET(THDB,cur_stack) - STRUCTOFFSET(THDB,teb))
172 static void *xmalloc (size_t size
)
176 res
= malloc (size
? size
: 1);
179 fprintf (stderr
, "Virtual memory exhausted.\n");
186 static void *xrealloc (void *ptr
, size_t size
)
188 void *res
= realloc (ptr
, size
);
191 fprintf (stderr
, "Virtual memory exhausted.\n");
198 static int IsNumberString(char *s
)
207 static char *strupper(char *s
)
211 for(p
= s
; *p
!= '\0'; p
++)
217 static char * GetTokenInLine(void)
222 if (ParseNext
!= ParseBuffer
)
224 if (ParseSaveChar
== '\0')
226 *ParseNext
= ParseSaveChar
;
230 * Remove initial white space.
232 for (p
= ParseNext
; isspace(*p
); p
++)
235 if ((*p
== '\0') || (*p
== '#'))
242 if (*token
!= '(' && *token
!= ')')
243 while (*p
!= '\0' && *p
!= '(' && *p
!= ')' && !isspace(*p
))
253 static char * GetToken(void)
257 if (ParseBuffer
== NULL
)
259 ParseBuffer
= xmalloc(512);
260 ParseNext
= ParseBuffer
;
264 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
266 if (ParseBuffer
[0] != '#')
271 while ((token
= GetTokenInLine()) == NULL
)
273 ParseNext
= ParseBuffer
;
277 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
279 if (ParseBuffer
[0] != '#')
288 /*******************************************************************
291 * Parse a variable definition.
293 static int ParseVariable( ORDDEF
*odp
)
298 int value_array_size
;
300 char *token
= GetToken();
303 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
304 SpecName
, Line
, token
);
309 value_array_size
= 25;
310 value_array
= xmalloc(sizeof(*value_array
) * value_array_size
);
312 while ((token
= GetToken()) != NULL
)
317 value_array
[n_values
++] = strtol(token
, &endptr
, 0);
318 if (n_values
== value_array_size
)
320 value_array_size
+= 25;
321 value_array
= xrealloc(value_array
,
322 sizeof(*value_array
) * value_array_size
);
325 if (endptr
== NULL
|| *endptr
!= '\0')
327 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
328 SpecName
, Line
, token
);
335 fprintf(stderr
, "%s:%d: End of file in variable declaration\n",
340 odp
->u
.var
.n_values
= n_values
;
341 odp
->u
.var
.values
= xrealloc(value_array
, sizeof(*value_array
) * n_values
);
347 /*******************************************************************
348 * ParseExportFunction
350 * Parse a function definition.
352 static int ParseExportFunction( ORDDEF
*odp
)
360 if (odp
->type
== TYPE_STDCALL
)
362 fprintf( stderr
, "%s:%d: 'stdcall' not supported for Win16\n",
368 if ((odp
->type
== TYPE_PASCAL
) || (odp
->type
== TYPE_PASCAL_16
))
370 fprintf( stderr
, "%s:%d: 'pascal' not supported for Win32\n",
382 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
383 SpecName
, Line
, token
);
387 for (i
= 0; i
< sizeof(odp
->u
.func
.arg_types
)-1; i
++)
393 if (!strcmp(token
, "word"))
394 odp
->u
.func
.arg_types
[i
] = 'w';
395 else if (!strcmp(token
, "s_word"))
396 odp
->u
.func
.arg_types
[i
] = 's';
397 else if (!strcmp(token
, "long") || !strcmp(token
, "segptr"))
398 odp
->u
.func
.arg_types
[i
] = 'l';
399 else if (!strcmp(token
, "ptr"))
400 odp
->u
.func
.arg_types
[i
] = 'p';
401 else if (!strcmp(token
, "str"))
402 odp
->u
.func
.arg_types
[i
] = 't';
403 else if (!strcmp(token
, "wstr"))
404 odp
->u
.func
.arg_types
[i
] = 'W';
405 else if (!strcmp(token
, "segstr"))
406 odp
->u
.func
.arg_types
[i
] = 'T';
407 else if (!strcmp(token
, "double"))
409 odp
->u
.func
.arg_types
[i
++] = 'l';
410 odp
->u
.func
.arg_types
[i
] = 'l';
414 fprintf(stderr
, "%s:%d: Unknown variable type '%s'\n",
415 SpecName
, Line
, token
);
418 if (SpecType
== SPEC_WIN32
)
420 if (strcmp(token
, "long") &&
421 strcmp(token
, "ptr") &&
422 strcmp(token
, "str") &&
423 strcmp(token
, "wstr") &&
424 strcmp(token
, "double"))
426 fprintf( stderr
, "%s:%d: Type '%s' not supported for Win32\n",
427 SpecName
, Line
, token
);
432 if ((*token
!= ')') || (i
>= sizeof(odp
->u
.func
.arg_types
)))
434 fprintf( stderr
, "%s:%d: Too many arguments\n", SpecName
, Line
);
437 odp
->u
.func
.arg_types
[i
] = '\0';
438 if ((odp
->type
== TYPE_STDCALL
) && !i
)
439 odp
->type
= TYPE_CDECL
; /* stdcall is the same as cdecl for 0 args */
440 if ((odp
->type
== TYPE_REGISTER
) && (SpecType
== SPEC_WIN32
) && i
)
442 fprintf( stderr
, "%s:%d: register functions cannot have arguments in Win32\n",
446 strcpy(odp
->u
.func
.link_name
, GetToken());
451 /*******************************************************************
454 * Parse an 'equate' definition.
456 static int ParseEquate( ORDDEF
*odp
)
460 char *token
= GetToken();
461 int value
= strtol(token
, &endptr
, 0);
462 if (endptr
== NULL
|| *endptr
!= '\0')
464 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
465 SpecName
, Line
, token
);
469 if (SpecType
== SPEC_WIN32
)
471 fprintf( stderr
, "%s:%d: 'equate' not supported for Win32\n",
476 odp
->u
.abs
.value
= value
;
481 /*******************************************************************
484 * Parse a 'return' definition.
486 static int ParseReturn( ORDDEF
*odp
)
492 odp
->u
.ret
.arg_size
= strtol(token
, &endptr
, 0);
493 if (endptr
== NULL
|| *endptr
!= '\0')
495 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
496 SpecName
, Line
, token
);
501 odp
->u
.ret
.ret_value
= strtol(token
, &endptr
, 0);
502 if (endptr
== NULL
|| *endptr
!= '\0')
504 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
505 SpecName
, Line
, token
);
509 if (SpecType
== SPEC_WIN32
)
511 fprintf( stderr
, "%s:%d: 'return' not supported for Win32\n",
520 /*******************************************************************
523 * Parse a 'stub' definition.
525 static int ParseStub( ORDDEF
*odp
)
527 odp
->u
.func
.arg_types
[0] = '\0';
528 strcpy( odp
->u
.func
.link_name
, STUB_CALLBACK
);
533 /*******************************************************************
536 * Parse an 'varargs' definition.
538 static int ParseVarargs( ORDDEF
*odp
)
542 if (SpecType
== SPEC_WIN16
)
544 fprintf( stderr
, "%s:%d: 'varargs' not supported for Win16\n",
552 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
553 SpecName
, Line
, token
);
559 fprintf(stderr
, "%s:%d: Expected ')' got '%s'\n",
560 SpecName
, Line
, token
);
564 strcpy( odp
->u
.vargs
.link_name
, GetToken() );
569 /*******************************************************************
572 * Parse an 'extern' definition.
574 static int ParseExtern( ORDDEF
*odp
)
576 if (SpecType
== SPEC_WIN16
)
578 fprintf( stderr
, "%s:%d: 'extern' not supported for Win16\n",
582 strcpy( odp
->u
.ext
.link_name
, GetToken() );
587 /*******************************************************************
590 * Parse an ordinal definition.
592 static int ParseOrdinal(int ordinal
)
597 if (ordinal
>= MAX_ORDINALS
)
599 fprintf(stderr
, "%s:%d: Ordinal number too large\n", SpecName
, Line
);
602 if (ordinal
> Limit
) Limit
= ordinal
;
603 if (ordinal
< Base
) Base
= ordinal
;
605 odp
= &OrdinalDefinitions
[ordinal
];
606 if (!(token
= GetToken()))
608 fprintf(stderr
, "%s:%d: Expected type after ordinal\n", SpecName
, Line
);
612 for (odp
->type
= 0; odp
->type
< TYPE_NBTYPES
; odp
->type
++)
613 if (TypeNames
[odp
->type
] && !strcmp( token
, TypeNames
[odp
->type
] ))
616 if (odp
->type
>= TYPE_NBTYPES
)
619 "%s:%d: Expected type after ordinal, found '%s' instead\n",
620 SpecName
, Line
, token
);
624 if (!(token
= GetToken()))
626 fprintf( stderr
, "%s:%d: Expected name after type\n", SpecName
, Line
);
629 strcpy( odp
->name
, token
);
637 return ParseVariable( odp
);
643 return ParseExportFunction( odp
);
645 return ParseEquate( odp
);
647 return ParseReturn( odp
);
649 return ParseStub( odp
);
651 return ParseVarargs( odp
);
653 return ParseExtern( odp
);
655 fprintf( stderr
, "Should not happen\n" );
661 /*******************************************************************
666 static int ParseTopLevel(void)
670 while ((token
= GetToken()) != NULL
)
672 if (strcmp(token
, "name") == 0)
674 strcpy(DLLName
, GetToken());
676 if (!DLLFileName
[0]) sprintf( DLLFileName
, "%s.DLL", DLLName
);
678 else if (strcmp(token
, "file") == 0)
680 strcpy(DLLFileName
, GetToken());
681 strupper(DLLFileName
);
683 else if (strcmp(token
, "type") == 0)
686 if (!strcmp(token
, "win16" )) SpecType
= SPEC_WIN16
;
687 else if (!strcmp(token
, "win32" )) SpecType
= SPEC_WIN32
;
690 fprintf(stderr
, "%s:%d: Type must be 'win16' or 'win32'\n",
695 else if (strcmp(token
, "heap") == 0)
698 if (!IsNumberString(token
))
700 fprintf(stderr
, "%s:%d: Expected number after heap\n",
704 DLLHeapSize
= atoi(token
);
706 else if (strcmp(token
, "init") == 0)
708 strcpy(DLLInitFunc
, GetToken());
710 fprintf(stderr
, "%s:%d: Expected function name after init\n", SpecName
, Line
);
712 else if (IsNumberString(token
))
717 ordinal
= atoi(token
);
718 if ((rv
= ParseOrdinal(ordinal
)) < 0)
724 "%s:%d: Expected name, id, length or ordinal\n",
734 /*******************************************************************
737 * Store a list of ints into a byte array.
739 static int StoreVariableCode( unsigned char *buffer
, int size
, ORDDEF
*odp
)
746 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
747 buffer
[i
] = odp
->u
.var
.values
[i
];
750 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
751 ((unsigned short *)buffer
)[i
] = odp
->u
.var
.values
[i
];
754 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
755 ((unsigned int *)buffer
)[i
] = odp
->u
.var
.values
[i
];
758 return odp
->u
.var
.n_values
* size
;
762 /*******************************************************************
765 * Dump a byte stream into the assembly code.
767 static void DumpBytes( FILE *outfile
, const unsigned char *data
, int len
,
768 const char *section
, const char *label_start
)
771 if (section
) fprintf( outfile
, "\t%s\n", section
);
772 if (label_start
) fprintf( outfile
, "%s:\n", label_start
);
773 for (i
= 0; i
< len
; i
++)
775 if (!(i
& 0x0f)) fprintf( outfile
, "\t.byte " );
776 fprintf( outfile
, "%d", *data
++ );
778 fprintf( outfile
, "%c", ((i
& 0x0f) != 0x0f) ? ',' : '\n' );
780 fprintf( outfile
, "\n" );
784 /*******************************************************************
787 * Build the in-memory representation of a 16-bit NE module, and dump it
788 * as a byte stream into the assembly code.
790 static int BuildModule16( FILE *outfile
, int max_code_offset
,
791 int max_data_offset
)
797 SEGTABLEENTRY
*pSegment
;
801 ET_BUNDLE
*bundle
= 0;
806 * OFSTRUCT File information
807 * SEGTABLEENTRY Segment 1 (code)
808 * SEGTABLEENTRY Segment 2 (data)
809 * WORD[2] Resource table (empty)
810 * BYTE[2] Imported names (empty)
811 * BYTE[n] Resident names table
812 * BYTE[n] Entry table
815 buffer
= xmalloc( 0x10000 );
817 pModule
= (NE_MODULE
*)buffer
;
818 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
821 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_BUILTIN
| NE_FFLAGS_LIBMODULE
;
823 pModule
->heap_size
= DLLHeapSize
;
824 pModule
->stack_size
= 0;
829 pModule
->seg_count
= 2;
830 pModule
->modref_count
= 0;
831 pModule
->nrname_size
= 0;
832 pModule
->modref_table
= 0;
833 pModule
->nrname_fpos
= 0;
834 pModule
->moveable_entries
= 0;
835 pModule
->alignment
= 0;
836 pModule
->truetype
= 0;
837 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
838 pModule
->misc_flags
= 0;
839 pModule
->dlls_to_init
= 0;
840 pModule
->nrname_handle
= 0;
841 pModule
->min_swap_area
= 0;
842 pModule
->expected_version
= 0x030a;
843 pModule
->module32
= 0;
845 pModule
->self_loading_sel
= 0;
847 /* File information */
849 pFileInfo
= (OFSTRUCT
*)(pModule
+ 1);
850 pModule
->fileinfo
= (int)pFileInfo
- (int)pModule
;
851 memset( pFileInfo
, 0, sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
) );
852 pFileInfo
->cBytes
= sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
)
853 + strlen(DLLFileName
);
854 strcpy( pFileInfo
->szPathName
, DLLFileName
);
855 pstr
= (char *)pFileInfo
+ pFileInfo
->cBytes
+ 1;
859 pSegment
= (SEGTABLEENTRY
*)pstr
;
860 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
861 pSegment
->filepos
= 0;
862 pSegment
->size
= max_code_offset
;
864 pSegment
->minsize
= max_code_offset
;
868 pModule
->dgroup_entry
= (int)pSegment
- (int)pModule
;
869 pSegment
->filepos
= 0;
870 pSegment
->size
= max_data_offset
;
871 pSegment
->flags
= NE_SEGFLAGS_DATA
;
872 pSegment
->minsize
= max_data_offset
;
878 pword
= (WORD
*)pSegment
;
879 pModule
->res_table
= (int)pword
- (int)pModule
;
883 /* Imported names table */
885 pstr
= (char *)pword
;
886 pModule
->import_table
= (int)pstr
- (int)pModule
;
890 /* Resident names table */
892 pModule
->name_table
= (int)pstr
- (int)pModule
;
893 /* First entry is module name */
894 *pstr
= strlen(DLLName
);
895 strcpy( pstr
+ 1, DLLName
);
898 pstr
+= sizeof(WORD
);
899 /* Store all ordinals */
900 odp
= OrdinalDefinitions
+ 1;
901 for (i
= 1; i
<= Limit
; i
++, odp
++)
903 if (!odp
->name
[0]) continue;
904 *pstr
= strlen( odp
->name
);
905 strcpy( pstr
+ 1, odp
->name
);
906 strupper( pstr
+ 1 );
909 pstr
+= sizeof(WORD
);
915 pModule
->entry_table
= (int)pstr
- (int)pModule
;
916 odp
= OrdinalDefinitions
+ 1;
917 for (i
= 1; i
<= Limit
; i
++, odp
++)
929 selector
= 1; /* Code selector */
935 selector
= 2; /* Data selector */
939 selector
= 0xfe; /* Constant selector */
943 selector
= 0; /* Invalid selector */
950 if ( bundle
&& bundle
->last
+1 == i
)
955 bundle
->next
= (char *)pstr
- (char *)pModule
;
957 bundle
= (ET_BUNDLE
*)pstr
;
961 pstr
+= sizeof(ET_BUNDLE
);
964 /* FIXME: is this really correct ?? */
965 entry
= (ET_ENTRY
*)pstr
;
966 entry
->type
= 0xff; /* movable */
967 entry
->flags
= 3; /* exported & public data */
968 entry
->segnum
= selector
;
969 entry
->offs
= odp
->offset
;
970 pstr
+= sizeof(ET_ENTRY
);
974 /* Dump the module content */
976 DumpBytes( outfile
, (char *)pModule
, (int)pstr
- (int)pModule
,
977 ".data", "Module_Start" );
978 return (int)pstr
- (int)pModule
;
982 /*******************************************************************
985 * Build a Win32 C file from a spec file.
987 static int BuildSpec32File( char * specfile
, FILE *outfile
)
992 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
994 fprintf( outfile
, "#include \"builtin32.h\"\n\n" );
996 /* Output code for all stubs functions */
998 fprintf( outfile
, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
1000 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1002 if (odp
->type
!= TYPE_STUB
) continue;
1003 fprintf( outfile
, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
1007 /* Output code for all register functions */
1009 fprintf( outfile
, "#ifdef __i386__\n" );
1010 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1012 if (odp
->type
!= TYPE_REGISTER
) continue;
1014 "__asm__(\".align 4\\n\\t\"\n"
1015 " \".globl " PREFIX
"%s\\n\\t\"\n"
1016 " \".type " PREFIX
"%s,@function\\n\\t\"\n"
1017 " \"" PREFIX
"%s:\\n\\t\"\n"
1018 " \"pushl $" PREFIX
"__regs_%s\\n\\t\"\n"
1019 " \"pushl $" PREFIX
"CALL32_Regs\\n\\t\"\n"
1021 odp
->u
.func
.link_name
, odp
->u
.func
.link_name
,
1022 odp
->u
.func
.link_name
, odp
->u
.func
.link_name
);
1024 fprintf( outfile
, "#endif\n" );
1026 /* Output the DLL functions prototypes */
1028 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1033 fprintf( outfile
, "extern void %s();\n", odp
->u
.vargs
.link_name
);
1036 fprintf( outfile
, "extern void %s();\n", odp
->u
.ext
.link_name
);
1041 fprintf( outfile
, "extern void %s();\n", odp
->u
.func
.link_name
);
1047 fprintf(stderr
,"build: function type %d not available for Win32\n",
1053 /* Output LibMain function */
1054 if (DLLInitFunc
[0]) fprintf( outfile
, "extern void %s();\n", DLLInitFunc
);
1057 /* Output the DLL functions table */
1059 fprintf( outfile
, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1061 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1066 fprintf( outfile
, " 0" );
1069 fprintf( outfile
, " %s", odp
->u
.vargs
.link_name
);
1072 fprintf( outfile
, " %s", odp
->u
.ext
.link_name
);
1077 fprintf( outfile
, " %s", odp
->u
.func
.link_name
);
1080 fprintf( outfile
, " __stub_%d", i
);
1085 if (i
< Limit
) fprintf( outfile
, ",\n" );
1087 fprintf( outfile
, "\n};\n\n" );
1089 /* Output the DLL names table */
1092 fprintf( outfile
, "static const char * const FuncNames[] =\n{\n" );
1093 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1095 if (odp
->type
== TYPE_INVALID
) continue;
1096 if (nb_names
++) fprintf( outfile
, ",\n" );
1097 fprintf( outfile
, " \"%s\"", odp
->name
);
1099 fprintf( outfile
, "\n};\n\n" );
1101 /* Output the DLL argument types */
1103 fprintf( outfile
, "static const unsigned int ArgTypes[%d] =\n{\n",
1105 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1107 unsigned int j
, mask
= 0;
1108 if ((odp
->type
== TYPE_STDCALL
) || (odp
->type
== TYPE_CDECL
))
1109 for (j
= 0; odp
->u
.func
.arg_types
[j
]; j
++)
1111 if (odp
->u
.func
.arg_types
[j
] == 't') mask
|= 1<< (j
*2);
1112 if (odp
->u
.func
.arg_types
[j
] == 'W') mask
|= 2<< (j
*2);
1114 fprintf( outfile
, " %d", mask
);
1115 if (i
< Limit
) fprintf( outfile
, ",\n" );
1117 fprintf( outfile
, "\n};\n\n" );
1119 /* Output the DLL ordinals table */
1121 fprintf( outfile
, "static const unsigned short FuncOrdinals[] =\n{\n" );
1123 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1125 if (odp
->type
== TYPE_INVALID
) continue;
1126 if (nb_names
++) fprintf( outfile
, ",\n" );
1127 fprintf( outfile
, " %d", i
- Base
);
1129 fprintf( outfile
, "\n};\n\n" );
1131 /* Output the DLL functions arguments */
1133 fprintf( outfile
, "static const unsigned char FuncArgs[%d] =\n{\n",
1135 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1141 args
= (unsigned char)strlen(odp
->u
.func
.arg_types
);
1144 args
= 0x80 | (unsigned char)strlen(odp
->u
.func
.arg_types
);
1153 fprintf( outfile
, " 0x%02x", args
);
1154 if (i
< Limit
) fprintf( outfile
, ",\n" );
1156 fprintf( outfile
, "\n};\n\n" );
1158 /* Output the DLL descriptor */
1160 fprintf( outfile
, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1162 fprintf( outfile
, " \"%s\",\n", DLLName
);
1163 fprintf( outfile
, " %d,\n", Base
);
1164 fprintf( outfile
, " %d,\n", Limit
- Base
+ 1 );
1165 fprintf( outfile
, " %d,\n", nb_names
);
1172 fprintf( outfile
, " %s\n", DLLInitFunc
[0] ? DLLInitFunc
: "0" );
1173 fprintf( outfile
, "};\n" );
1178 /*******************************************************************
1181 * Build a Win16 assembly file from a spec file.
1183 static int BuildSpec16File( char * specfile
, FILE *outfile
)
1187 int code_offset
, data_offset
, module_size
;
1188 unsigned char *data
;
1190 data
= (unsigned char *)xmalloc( 0x10000 );
1191 memset( data
, 0, 16 );
1194 fprintf( outfile
, "/* File generated automatically; do not edit! */\n" );
1195 fprintf( outfile
, "\t.text\n" );
1196 fprintf( outfile
, "Code_Start:\n" );
1199 odp
= OrdinalDefinitions
;
1200 for (i
= 0; i
<= Limit
; i
++, odp
++)
1205 odp
->offset
= 0xffff;
1209 odp
->offset
= LOWORD(odp
->u
.abs
.value
);
1213 odp
->offset
= data_offset
;
1214 data_offset
+= StoreVariableCode( data
+ data_offset
, 1, odp
);
1218 odp
->offset
= data_offset
;
1219 data_offset
+= StoreVariableCode( data
+ data_offset
, 2, odp
);
1223 odp
->offset
= data_offset
;
1224 data_offset
+= StoreVariableCode( data
+ data_offset
, 4, odp
);
1228 fprintf( outfile
,"/* %s.%d */\n", DLLName
, i
);
1229 fprintf( outfile
,"\tmovw $%d,%%ax\n",LOWORD(odp
->u
.ret
.ret_value
));
1230 fprintf( outfile
,"\tmovw $%d,%%dx\n",HIWORD(odp
->u
.ret
.ret_value
));
1231 fprintf( outfile
,"\t.byte 0x66\n");
1232 if (odp
->u
.ret
.arg_size
!= 0)
1233 fprintf( outfile
, "\tlret $%d\n\n", odp
->u
.ret
.arg_size
);
1236 fprintf( outfile
, "\tlret\n");
1237 fprintf( outfile
, "\tnop\n");
1238 fprintf( outfile
, "\tnop\n\n");
1240 odp
->offset
= code_offset
;
1241 code_offset
+= 12; /* Assembly code is 12 bytes long */
1247 case TYPE_PASCAL_16
:
1249 fprintf( outfile
, "/* %s.%d */\n", DLLName
, i
);
1250 fprintf( outfile
, "\tpushw %%bp\n" );
1251 fprintf( outfile
, "\tpushl $" PREFIX
"%s\n",odp
->u
.func
.link_name
);
1252 /* FreeBSD does not understand lcall, so do it the hard way */
1253 fprintf( outfile
, "\t.byte 0x9a\n" );
1254 fprintf( outfile
, "\t.long " PREFIX
"CallFrom16_%s_%s_%s\n",
1255 (odp
->type
== TYPE_CDECL
) ? "c" : "p",
1256 (odp
->type
== TYPE_REGISTER
) ? "regs" :
1257 (odp
->type
== TYPE_PASCAL_16
) ? "word" : "long",
1258 odp
->u
.func
.arg_types
);
1259 fprintf( outfile
, "\t.long 0x%08lx\n",
1260 MAKELONG( Code_Selector
, 0x9090 /* nop ; nop */ ) );
1261 odp
->offset
= code_offset
;
1262 code_offset
+= 16; /* Assembly code is 16 bytes long */
1266 fprintf(stderr
,"build: function type %d not available for Win16\n",
1272 if (!code_offset
) /* Make sure the code segment is not empty */
1274 fprintf( outfile
, "\t.byte 0\n" );
1278 /* Output data segment */
1280 DumpBytes( outfile
, data
, data_offset
, NULL
, "Data_Start" );
1282 /* Build the module */
1284 module_size
= BuildModule16( outfile
, code_offset
, data_offset
);
1286 /* Output the DLL descriptor */
1288 fprintf( outfile
, "\t.text\n" );
1289 fprintf( outfile
, "DLLName:\t" STRING
" \"%s\\0\"\n", DLLName
);
1290 fprintf( outfile
, "\t.align 4\n" );
1291 fprintf( outfile
, "\t.globl " PREFIX
"%s_Descriptor\n", DLLName
);
1292 fprintf( outfile
, PREFIX
"%s_Descriptor:\n", DLLName
);
1293 fprintf( outfile
, "\t.long DLLName\n" ); /* Name */
1294 fprintf( outfile
, "\t.long Module_Start\n" ); /* Module start */
1295 fprintf( outfile
, "\t.long %d\n", module_size
); /* Module size */
1296 fprintf( outfile
, "\t.long Code_Start\n" ); /* Code start */
1297 fprintf( outfile
, "\t.long Data_Start\n" ); /* Data start */
1302 /*******************************************************************
1305 * Build an assembly file from a spec file.
1307 static int BuildSpecFile( FILE *outfile
, char *specname
)
1309 SpecName
= specname
;
1310 SpecFp
= fopen( specname
, "r");
1313 fprintf(stderr
, "Could not open specification file, '%s'\n", specname
);
1317 if (ParseTopLevel() < 0) return -1;
1322 return BuildSpec16File( specname
, outfile
);
1324 return BuildSpec32File( specname
, outfile
);
1326 fprintf( stderr
, "%s: Missing 'type' declaration\n", specname
);
1332 /*******************************************************************
1333 * TransferArgs16To32
1335 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1336 * The 16-bit stack layout is:
1344 * For 'cdecl' argn up to arg1 are reversed.
1346 static int TransferArgs16To32( FILE *outfile
, char *args
, int usecdecl
)
1348 int i
, pos16
, pos32
;
1351 /* Copy the arguments */
1353 pos16
= 6; /* skip bp and return address */
1354 pos32
= usecdecl
? -(strlen(args
) * 4) : 0;
1355 xargs
= usecdecl
? args
:args
+strlen(args
);
1357 for (i
= strlen(args
); i
> 0; i
--)
1365 case 'w': /* word */
1366 fprintf( outfile
, "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1367 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1371 case 's': /* s_word */
1372 fprintf( outfile
, "\tmovswl %d(%%ebp),%%eax\n", pos16
);
1373 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1377 case 'l': /* long or segmented pointer */
1378 case 'T': /* segmented pointer to null-terminated string */
1379 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", pos16
);
1380 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1384 case 'p': /* linear pointer */
1385 case 't': /* linear pointer to null-terminated string */
1386 /* Get the selector */
1387 fprintf( outfile
, "\tmovw %d(%%ebp),%%ax\n", pos16
+ 2 );
1388 /* Get the selector base */
1389 fprintf( outfile
, "\tandl $0xfff8,%%eax\n" );
1390 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy(%%eax),%%eax\n" );
1391 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1392 /* Add the offset */
1393 fprintf( outfile
, "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1394 fprintf( outfile
, "\taddl %%eax,%d(%%ebx)\n", pos32
);
1399 fprintf( stderr
, "Unknown arg type '%c'\n", *xargs
);
1407 return pos16
- 6; /* Return the size of the 16-bit args */
1411 /*******************************************************************
1414 * Build the context structure on the 32-bit stack.
1416 static void BuildContext16( FILE *outfile
)
1418 /* Store the registers */
1420 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1421 CONTEXTOFFSET(Eax
) - sizeof(CONTEXT
) );
1422 fprintf( outfile
, "\tmovl %%ecx,%d(%%ebx)\n",
1423 CONTEXTOFFSET(Ecx
) - sizeof(CONTEXT
) );
1424 fprintf( outfile
, "\tmovl %%edx,%d(%%ebx)\n",
1425 CONTEXTOFFSET(Edx
) - sizeof(CONTEXT
) );
1426 fprintf( outfile
, "\tmovl %%esi,%d(%%ebx)\n",
1427 CONTEXTOFFSET(Esi
) - sizeof(CONTEXT
) );
1428 fprintf( outfile
, "\tmovl %%edi,%d(%%ebx)\n",
1429 CONTEXTOFFSET(Edi
) - sizeof(CONTEXT
) );
1431 fprintf( outfile
, "\tmovl -24(%%ebp),%%eax\n" ); /* Get %ebx from stack*/
1432 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1433 CONTEXTOFFSET(Ebx
) - sizeof(CONTEXT
) );
1434 fprintf( outfile
, "\tmovzwl -10(%%ebp),%%eax\n" ); /* Get %ds from stack*/
1435 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1436 CONTEXTOFFSET(SegDs
) - sizeof(CONTEXT
) );
1437 fprintf( outfile
, "\tmovzwl -6(%%ebp),%%eax\n" ); /* Get %es from stack*/
1438 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1439 CONTEXTOFFSET(SegEs
) - sizeof(CONTEXT
) );
1440 fprintf( outfile
, "\tpushfl\n" );
1441 fprintf( outfile
, "\tpopl %d(%%ebx)\n",
1442 CONTEXTOFFSET(EFlags
) - sizeof(CONTEXT
) );
1443 fprintf( outfile
, "\tmovl -20(%%ebp),%%eax\n" ); /* Get %ebp from stack */
1444 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1445 CONTEXTOFFSET(Ebp
) - sizeof(CONTEXT
) );
1446 fprintf( outfile
, "\tmovzwl 2(%%ebp),%%eax\n" ); /* Get %ip from stack */
1447 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1448 CONTEXTOFFSET(Eip
) - sizeof(CONTEXT
) );
1449 fprintf( outfile
, "\tleal 2(%%ebp),%%eax\n" ); /* Get initial %sp */
1450 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1451 CONTEXTOFFSET(Esp
) - sizeof(CONTEXT
) );
1452 fprintf( outfile
, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1453 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1454 CONTEXTOFFSET(SegCs
) - sizeof(CONTEXT
) );
1455 fprintf( outfile
, "\tmovzwl -14(%%ebp),%%eax\n" ); /* Get %fs from stack */
1456 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1457 CONTEXTOFFSET(SegFs
) - sizeof(CONTEXT
) );
1458 fprintf( outfile
, "\tmovw %%gs,%%ax\n" );
1459 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1460 CONTEXTOFFSET(SegGs
) - sizeof(CONTEXT
) );
1461 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
1462 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1463 CONTEXTOFFSET(SegSs
) - sizeof(CONTEXT
) );
1465 fprintf( outfile
, "\tfsave %d(%%ebx)\n",
1466 CONTEXTOFFSET(FloatSave
) - sizeof(CONTEXT
) );
1471 /*******************************************************************
1474 * Restore the registers from the context structure.
1476 static void RestoreContext16( FILE *outfile
)
1478 /* Get the 32-bit stack pointer */
1480 fprintf( outfile
, "\tleal -%d(%%ebp),%%ebx\n",
1481 STRUCTOFFSET(STACK32FRAME
,ebp
) );
1483 /* Remove everything up to (including) the return address
1484 * from the 16-bit stack */
1486 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n",
1487 CONTEXTOFFSET(SegSs
) - sizeof(CONTEXT
) );
1488 fprintf( outfile
, "\tmovw %%ax,%%ss\n" );
1489 fprintf( outfile
, "\tmovl %d(%%ebx),%%esp\n",
1490 CONTEXTOFFSET(Esp
) - sizeof(CONTEXT
) );
1491 fprintf( outfile
, "\taddl $4,%%esp\n" ); /* Remove return address */
1493 /* Restore the registers */
1495 fprintf( outfile
, "\tmovl %d(%%ebx),%%ecx\n",
1496 CONTEXTOFFSET(Ecx
) - sizeof(CONTEXT
) );
1497 fprintf( outfile
, "\tmovl %d(%%ebx),%%edx\n",
1498 CONTEXTOFFSET(Edx
) - sizeof(CONTEXT
) );
1499 fprintf( outfile
, "\tmovl %d(%%ebx),%%esi\n",
1500 CONTEXTOFFSET(Esi
) - sizeof(CONTEXT
) );
1501 fprintf( outfile
, "\tmovl %d(%%ebx),%%edi\n",
1502 CONTEXTOFFSET(Edi
) - sizeof(CONTEXT
) );
1503 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebp\n",
1504 CONTEXTOFFSET(Ebp
) - sizeof(CONTEXT
) );
1505 fprintf( outfile
, "\tpushw %d(%%ebx)\n", /* Push new cs */
1506 CONTEXTOFFSET(SegCs
) - sizeof(CONTEXT
) );
1507 fprintf( outfile
, "\tpushw %d(%%ebx)\n", /* Push new ip */
1508 CONTEXTOFFSET(Eip
) - sizeof(CONTEXT
) );
1509 fprintf( outfile
, "\tpushl %d(%%ebx)\n", /* Push new ds */
1510 CONTEXTOFFSET(SegDs
) - sizeof(CONTEXT
) );
1511 fprintf( outfile
, "\tpushl %d(%%ebx)\n", /* Push new es */
1512 CONTEXTOFFSET(SegEs
) - sizeof(CONTEXT
) );
1513 fprintf( outfile
, "\tpushl %d(%%ebx)\n", /* Push new fs */
1514 CONTEXTOFFSET(SegFs
) - sizeof(CONTEXT
) );
1515 fprintf( outfile
, "\tpushl %d(%%ebx)\n",
1516 CONTEXTOFFSET(EFlags
) - sizeof(CONTEXT
) );
1517 fprintf( outfile
, "\tpopfl\n" );
1518 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n",
1519 CONTEXTOFFSET(Eax
) - sizeof(CONTEXT
) );
1520 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebx\n",
1521 CONTEXTOFFSET(Ebx
) - sizeof(CONTEXT
) );
1522 fprintf( outfile
, "\tpopl %%fs\n" ); /* Set fs */
1523 fprintf( outfile
, "\tpopl %%es\n" ); /* Set es */
1524 fprintf( outfile
, "\tpopl %%ds\n" ); /* Set ds */
1528 /*******************************************************************
1529 * BuildCallFrom16Func
1531 * Build a 16-bit-to-Wine callback function. The syntax of the function
1532 * profile is: call_type_xxxxx, where 'call' is the letter 'c' or 'p' for C or
1533 * Pascal calling convention, 'type' is one of 'regs', 'word' or
1534 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1535 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1536 * 'T'=segmented pointer to null-terminated string).
1537 * For register functions, the arguments are ignored, but they are still
1538 * removed from the stack upon return.
1540 * A special variant of the callback function is generated by the function
1541 * profile "t_long_". This is used by the Win95 16->32 thunk
1542 * functions C16ThkSL and C16ThkSL01 and is implemented as follows:
1543 * On entry, the EBX register is set up to contain a flat pointer to the
1544 * 16-bit stack such that EBX+22 points to the first argument.
1545 * Then, the entry point is called, while EBP is set up to point
1546 * to the return address (on the 32-bit stack).
1547 * The called function returns with CX set to the number of bytes
1548 * to be popped of the caller's stack.
1550 * Stack layout upon entry to the callback function:
1552 * (sp+18) word first 16-bit arg
1556 * (sp+8) long 32-bit entry point (used to store edx)
1557 * (sp+6) word high word of cs (always 0, used to store es)
1558 * (sp+4) word low word of cs of 16-bit entry point
1559 * (sp+2) word high word of ip (always 0, used to store ds)
1560 * (sp) word low word of ip of 16-bit entry point
1562 * Added on the stack:
1563 * (sp-2) word saved fs
1564 * (sp-4) word buffer for Win16Mutex recursion count
1566 * (sp-12) long saved previous stack
1568 static void BuildCallFrom16Func( FILE *outfile
, char *profile
)
1575 char *args
= profile
+ 7;
1577 /* Parse function type */
1579 if (!strncmp( "c_", profile
, 2 )) Cdecl
= 1;
1580 else if (!strncmp( "t_", profile
, 2 )) thunk
= 1;
1581 else if (strncmp( "p_", profile
, 2 ))
1583 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1587 if (!strncmp( "word_", profile
+ 2, 5 )) short_ret
= 1;
1588 else if (!strncmp( "regs_", profile
+ 2, 5 )) reg_func
= 1;
1589 else if (strncmp( "long_", profile
+ 2, 5 ))
1591 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1595 /* Function header */
1597 fprintf( outfile
, "\n\t.align 4\n" );
1599 fprintf( outfile
, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX
"CallFrom16_%s\n",
1602 fprintf( outfile
, "\t.globl " PREFIX
"CallFrom16_%s\n", profile
);
1603 fprintf( outfile
, PREFIX
"CallFrom16_%s:\n", profile
);
1605 /* Save 16-bit fs and leave room for Win16Mutex recursion count */
1607 fprintf( outfile
, "\t.byte 0x66\n\tpushl %%fs\n" );
1608 fprintf( outfile
, "\tpushw $0\n" );
1610 /* Setup bp to point to its copy on the stack */
1612 fprintf( outfile
, "\tpushl %%ebp\n" ); /* Save the full 32-bit ebp */
1613 fprintf( outfile
, "\tmovzwl %%sp,%%ebp\n" );
1614 fprintf( outfile
, "\taddw $20,%%bp\n" );
1616 /* Save 16-bit ds and es */
1618 /* Stupid FreeBSD assembler doesn't know these either */
1619 /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1620 fprintf( outfile
, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1621 /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1622 fprintf( outfile
, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1626 fprintf( outfile
, "\tpushl %%ebx\n" );
1628 /* Restore 32-bit segment registers */
1630 fprintf( outfile
, "\tmovw $0x%04x,%%bx\n", Data_Selector
);
1632 fprintf( outfile
, "\tdata16\n");
1634 fprintf( outfile
, "\tmovw %%bx,%%ds\n" );
1636 fprintf( outfile
, "\tdata16\n");
1638 fprintf( outfile
, "\tmovw %%bx,%%es\n" );
1640 fprintf( outfile
, "\tmovw " PREFIX
"SYSLEVEL_Win16CurrentTeb,%%fs\n" );
1642 /* Get the 32-bit stack pointer from the TEB */
1644 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET
);
1646 /* Save the 16-bit stack */
1649 fprintf( outfile
,"\tdata16\n");
1651 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%ss,(%d)\n", STACKOFFSET
+ 2 );
1652 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%sp,(%d)\n", STACKOFFSET
);
1654 /* Transfer the arguments */
1656 if (reg_func
) BuildContext16( outfile
);
1657 else if (*args
) argsize
= TransferArgs16To32( outfile
, args
, Cdecl
);
1660 /* Get the stack selector base */
1661 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
1662 fprintf( outfile
, "\tandl $0xfff8,%%eax\n" );
1663 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy(%%eax),%%eax\n" );
1664 fprintf( outfile
, "\tmovl %%eax,-24(%%ebp)\n" );
1665 /* Add the offset */
1666 fprintf( outfile
, "\tleal -16(%%ebp),%%eax\n" );
1667 fprintf( outfile
, "\taddl %%eax,-24(%%ebp)\n" );
1670 /* Get the address of the API function */
1672 fprintf( outfile
, "\tmovl -4(%%ebp),%%eax\n" );
1674 /* If necessary, save %edx over the API function address */
1676 if (!reg_func
&& short_ret
)
1677 fprintf( outfile
, "\tmovl %%edx,-4(%%ebp)\n" );
1679 /* Restore %ebx and store the 32-bit stack pointer instead */
1681 fprintf( outfile
, "\tmovl %%ebx,%%ebp\n" );
1682 fprintf( outfile
, "\tpopl %%ebx\n" );
1683 fprintf( outfile
, "\tpushl %%ebp\n" );
1685 /* Switch to the 32-bit stack */
1687 fprintf( outfile
, "\tpushl %%ds\n" );
1688 fprintf( outfile
, "\tpopl %%ss\n" );
1689 fprintf( outfile
, "\tleal -%d(%%ebp),%%esp\n",
1690 reg_func
? sizeof(CONTEXT
) : 4 * strlen(args
) );
1691 if (reg_func
) /* Push the address of the context struct */
1692 fprintf( outfile
, "\tpushl %%esp\n" );
1694 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1696 fprintf( outfile
, "\taddl $%d,%%ebp\n", STRUCTOFFSET(STACK32FRAME
,ebp
) );
1698 /* Print the debug information before the call */
1700 if (debugging
&& !thunk
)
1704 if (Cdecl
) ftype
|= 4;
1705 if (reg_func
) ftype
|= 2;
1706 if (short_ret
) ftype
|= 1;
1708 fprintf( outfile
, "\tpushl %%eax\n" );
1709 fprintf( outfile
, "\tpushl $Profile_%s\n", profile
);
1710 fprintf( outfile
, "\tpushl $%d\n", ftype
);
1711 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16\n" );
1712 fprintf( outfile
, "\tpopl %%eax\n" );
1713 fprintf( outfile
, "\tpopl %%eax\n" );
1714 fprintf( outfile
, "\tpopl %%eax\n" );
1717 /* Call the entry point */
1721 fprintf( outfile
, "\tpushl %%ebp\n" );
1722 fprintf( outfile
, "\tleal -4(%%esp), %%ebp\n" );
1723 fprintf( outfile
, "\tcall *%%eax\n" );
1724 fprintf( outfile
, "\tpopl %%ebp\n" );
1727 fprintf( outfile
, "\tcall *%%eax\n" );
1730 /* Print the debug information after the call */
1732 if (debugging
&& !thunk
)
1736 /* Push again the address of the context struct in case */
1737 /* it has been removed by an stdcall function */
1738 fprintf( outfile
, "\tleal -%d(%%ebp),%%esp\n",
1739 sizeof(CONTEXT
) + STRUCTOFFSET(STACK32FRAME
,ebp
) );
1740 fprintf( outfile
, "\tpushl %%esp\n" );
1742 fprintf( outfile
, "\tpushl %%eax\n" );
1743 fprintf( outfile
, "\tpushl $%d\n", reg_func
? 2 : (short_ret
? 1 : 0));
1744 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret\n" );
1745 fprintf( outfile
, "\tpopl %%eax\n" );
1746 fprintf( outfile
, "\tpopl %%eax\n" );
1749 /* Restore the 16-bit stack */
1752 fprintf( outfile
, "\tdata16\n");
1754 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2 );
1755 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET
);
1756 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
1760 /* Calc the arguments size */
1776 fprintf( stderr
, "Unknown arg type '%c'\n", *args
);
1781 /* Restore registers from the context structure */
1782 RestoreContext16( outfile
);
1786 /* Restore high 16 bits of ebp */
1787 fprintf( outfile
, "\tpopl %%ebp\n" );
1789 /* Restore ds and es */
1790 fprintf( outfile
, "\tincl %%esp\n" ); /* Remove mutex count */
1791 fprintf( outfile
, "\tincl %%esp\n" );
1792 fprintf( outfile
, "\tpopl %%edx\n" ); /* Remove ip and fs */
1793 fprintf( outfile
, "\tmovw %%dx,%%fs\n" ); /* and restore fs */
1794 fprintf( outfile
, "\tpopl %%edx\n" ); /* Remove cs and ds */
1795 fprintf( outfile
, "\tmovw %%dx,%%ds\n" ); /* and restore ds */
1796 fprintf( outfile
, "\t.byte 0x66\n\tpopl %%es\n" ); /* Restore es */
1798 if (short_ret
) fprintf( outfile
, "\tpopl %%edx\n" ); /* Restore edx */
1801 /* Get the return value into dx:ax */
1802 fprintf( outfile
, "\tmovl %%eax,%%edx\n" );
1803 fprintf( outfile
, "\tshrl $16,%%edx\n" );
1804 /* Remove API entry point */
1805 fprintf( outfile
, "\taddl $4,%%esp\n" );
1808 /* Restore low 16 bits of ebp */
1809 fprintf( outfile
, "\tpopw %%bp\n" );
1812 /* Remove the arguments and return */
1816 fprintf( outfile
, "\tpopl %%ebx\n" );
1817 fprintf( outfile
, "\txorb %%ch,%%ch\n" );
1818 fprintf( outfile
, "\taddw %%cx, %%sp\n" );
1819 fprintf( outfile
, "\tpushl %%ebx\n" );
1820 fprintf( outfile
, "\t.byte 0x66\n" );
1821 fprintf( outfile
, "\tlret\n" );
1823 else if (argsize
&& !Cdecl
)
1825 fprintf( outfile
, "\t.byte 0x66\n" );
1826 fprintf( outfile
, "\tlret $%d\n", argsize
);
1830 fprintf( outfile
, "\t.byte 0x66\n" );
1831 fprintf( outfile
, "\tlret\n" );
1836 /*******************************************************************
1839 * Build a Wine-to-16-bit callback function.
1841 * Stack frame of the callback function:
1845 * (ebp+8) func to call
1846 * (ebp+4) return address
1847 * (ebp) previous ebp
1849 * Prototypes for the CallTo16 functions:
1850 * extern WINAPI WORD CallTo16_word_xxx( FARPROC16 func, args... );
1851 * extern WINAPI LONG CallTo16_long_xxx( FARPROC16 func, args... );
1852 * extern WINAPI void CallTo16_sreg_( const CONTEXT *context, int nb_args );
1853 * extern WINAPI void CallTo16_lreg_( const CONTEXT *context, int nb_args );
1855 static void BuildCallTo16Func( FILE *outfile
, char *profile
)
1859 char *args
= profile
+ 5;
1861 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1862 else if (!strncmp( "sreg_", profile
, 5 )) reg_func
= 1;
1863 else if (!strncmp( "lreg_", profile
, 5 )) reg_func
= 2;
1864 else if (strncmp( "long_", profile
, 5 ))
1866 fprintf( stderr
, "Invalid function name '%s'.\n", profile
);
1870 /* Function header */
1872 fprintf( outfile
, "\n\t.align 4\n" );
1874 fprintf( outfile
, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX
"CallTo16_%s\n",
1877 fprintf( outfile
, "\t.globl " PREFIX
"CallTo16_%s\n", profile
);
1878 fprintf( outfile
, PREFIX
"CallTo16_%s:\n", profile
);
1882 fprintf( outfile
, "\tpushl %%ebp\n" );
1883 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
1885 /* Save the 32-bit registers */
1887 fprintf( outfile
, "\tpushl %%ebx\n" );
1888 fprintf( outfile
, "\tpushl %%ecx\n" );
1889 fprintf( outfile
, "\tpushl %%edx\n" );
1890 fprintf( outfile
, "\tpushl %%esi\n" );
1891 fprintf( outfile
, "\tpushl %%edi\n" );
1893 /* Enter Win16 Mutex */
1895 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_EnterWin16Lock\n" );
1897 /* Print debugging info */
1901 /* Push the address of the first argument */
1902 fprintf( outfile
, "\tleal 8(%%ebp),%%eax\n" );
1903 fprintf( outfile
, "\tpushl $%d\n", reg_func
? -1 : strlen(args
) );
1904 fprintf( outfile
, "\tpushl %%eax\n" );
1905 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16\n" );
1906 fprintf( outfile
, "\tpopl %%eax\n" );
1907 fprintf( outfile
, "\tpopl %%eax\n" );
1910 /* Call the actual CallTo16 routine (simulate a lcall) */
1912 fprintf( outfile
, "\tpushl %%cs\n" );
1913 fprintf( outfile
, "\tcall do_callto16_%s\n", profile
);
1915 fprintf( outfile
, "\tpushl %%eax\n" );
1917 /* Print debugging info */
1921 fprintf( outfile
, "\tpushl %%eax\n" );
1922 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16Ret\n" );
1923 fprintf( outfile
, "\tpopl %%eax\n" );
1926 /* Leave Win16 Mutex */
1928 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_LeaveWin16Lock\n" );
1930 /* Restore the 32-bit registers */
1932 fprintf( outfile
, "\tpopl %%eax\n" );
1933 fprintf( outfile
, "\tpopl %%edi\n" );
1934 fprintf( outfile
, "\tpopl %%esi\n" );
1935 fprintf( outfile
, "\tpopl %%edx\n" );
1936 fprintf( outfile
, "\tpopl %%ecx\n" );
1937 fprintf( outfile
, "\tpopl %%ebx\n" );
1942 /* FIXME: this is a hack because of task.c */
1943 if (!strcmp( profile
, "word_" ))
1945 fprintf( outfile
, ".globl " PREFIX
"CALLTO16_Restore\n" );
1946 fprintf( outfile
, PREFIX
"CALLTO16_Restore:\n" );
1949 fprintf( outfile
, "\tpopl %%ebp\n" );
1950 fprintf( outfile
, "\tret $%d\n", 4 * strlen(args
) + 4 );
1953 /* Start of the actual CallTo16 routine */
1955 fprintf( outfile
, "do_callto16_%s:\n", profile
);
1957 /* Save the 32-bit stack */
1959 fprintf( outfile
, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET
);
1960 fprintf( outfile
, "\tmovl %%ebp,%%ebx\n" );
1961 fprintf( outfile
, "\tmovl %%esp,%%edx\n" );
1965 /* Switch to the 16-bit stack, saving the current %%esp, */
1966 /* and adding the specified offset to the new sp */
1967 fprintf( outfile
, "\t.byte 0x64\n\tmovzwl (%d),%%eax\n", STACKOFFSET
);
1968 fprintf( outfile
, "\tsubl 12(%%ebx),%%eax\n" ); /* Get the offset */
1970 fprintf( outfile
,"\tdata16\n");
1972 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2);
1973 fprintf( outfile
, "\tmovl %%eax,%%esp\n" );
1974 fprintf( outfile
, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET
);
1976 /* Get the registers. ebx is handled later on. */
1978 fprintf( outfile
, "\tmovl 8(%%ebx),%%ebx\n" );
1979 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs
) );
1980 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
1981 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegFs
) );
1982 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
1983 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp
) );
1984 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax
) );
1985 fprintf( outfile
, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx
) );
1986 fprintf( outfile
, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx
) );
1987 fprintf( outfile
, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi
) );
1988 fprintf( outfile
, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi
) );
1990 /* Push the return address
1991 * With sreg suffix, we push 16:16 address (normal lret)
1992 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
1995 fprintf( outfile
, "\tpushl " PREFIX
"CALLTO16_RetAddr_long\n" );
1998 fprintf( outfile
, "\tpushw $0\n" );
1999 fprintf( outfile
, "\tpushw " PREFIX
"CALLTO16_RetAddr_eax+2\n" );
2000 fprintf( outfile
, "\tpushw $0\n" );
2001 fprintf( outfile
, "\tpushw " PREFIX
"CALLTO16_RetAddr_eax\n" );
2004 /* Push the called routine address */
2006 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs
) );
2007 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip
) );
2009 /* Get the 16-bit ds */
2011 fprintf( outfile
, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs
) );
2012 /* Get ebx from the 32-bit stack */
2013 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(Ebx
) );
2014 fprintf( outfile
, "\tpopl %%ds\n" );
2016 else /* not a register function */
2018 int pos
= 12; /* first argument position */
2020 /* Switch to the 16-bit stack */
2022 fprintf( outfile
,"\tdata16\n");
2024 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2);
2025 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET
);
2026 fprintf( outfile
, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET
);
2028 /* Make %bp point to the previous stackframe (built by CallFrom16) */
2029 fprintf( outfile
, "\tmovzwl %%sp,%%ebp\n" );
2030 fprintf( outfile
, "\tleal %d(%%ebp),%%ebp\n",
2031 STRUCTOFFSET(STACK16FRAME
,bp
) );
2033 /* Transfer the arguments */
2039 case 'w': /* word */
2040 fprintf( outfile
, "\tpushw %d(%%ebx)\n", pos
);
2042 case 'l': /* long */
2043 fprintf( outfile
, "\tpushl %d(%%ebx)\n", pos
);
2046 fprintf( stderr
, "Unexpected case '%c' in BuildCallTo16Func\n",
2052 /* Push the return address */
2054 fprintf( outfile
, "\tpushl " PREFIX
"CALLTO16_RetAddr_%s\n",
2055 short_ret
? "word" : "long" );
2057 /* Push the called routine address */
2059 fprintf( outfile
, "\tpushl 8(%%ebx)\n" );
2061 /* Set %fs to the value saved by the last CallFrom16 */
2063 fprintf( outfile
, "\tmovw -14(%%ebp),%%ax\n" );
2064 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2066 /* Set %ds and %es (and %ax just in case) equal to %ss */
2068 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
2069 fprintf( outfile
, "\tmovw %%ax,%%ds\n" );
2070 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2073 /* Jump to the called routine */
2075 fprintf( outfile
, "\t.byte 0x66\n" );
2076 fprintf( outfile
, "\tlret\n" );
2080 /*******************************************************************
2083 * Build the return code for 16-bit callbacks
2085 static void BuildRet16Func( FILE *outfile
)
2087 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Ret_word\n" );
2088 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Ret_long\n" );
2089 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Ret_eax\n" );
2091 fprintf( outfile
, PREFIX
"CALLTO16_Ret_word:\n" );
2092 fprintf( outfile
, "\txorl %%edx,%%edx\n" );
2094 /* Put return value into %eax */
2096 fprintf( outfile
, PREFIX
"CALLTO16_Ret_long:\n" );
2097 fprintf( outfile
, "\tshll $16,%%edx\n" );
2098 fprintf( outfile
, "\tmovw %%ax,%%dx\n" );
2099 fprintf( outfile
, "\tmovl %%edx,%%eax\n" );
2100 fprintf( outfile
, PREFIX
"CALLTO16_Ret_eax:\n" );
2102 /* Restore 32-bit segment registers */
2104 fprintf( outfile
, "\tmovw $0x%04x,%%bx\n", Data_Selector
);
2106 fprintf( outfile
, "\tdata16\n");
2108 fprintf( outfile
, "\tmovw %%bx,%%ds\n" );
2110 fprintf( outfile
, "\tdata16\n");
2112 fprintf( outfile
, "\tmovw %%bx,%%es\n" );
2114 fprintf( outfile
, "\tmovw " PREFIX
"SYSLEVEL_Win16CurrentTeb,%%fs\n" );
2116 /* Restore the 32-bit stack */
2119 fprintf( outfile
, "\tdata16\n");
2121 fprintf( outfile
, "\tmovw %%bx,%%ss\n" );
2122 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET
);
2123 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2125 /* Return to caller */
2127 fprintf( outfile
, "\tlret\n" );
2129 /* Declare the return address variables */
2131 fprintf( outfile
, "\t.data\n" );
2132 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_RetAddr_word\n" );
2133 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_RetAddr_long\n" );
2134 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_RetAddr_eax\n" );
2135 fprintf( outfile
, PREFIX
"CALLTO16_RetAddr_word:\t.long 0\n" );
2136 fprintf( outfile
, PREFIX
"CALLTO16_RetAddr_long:\t.long 0\n" );
2137 fprintf( outfile
, PREFIX
"CALLTO16_RetAddr_eax:\t.long 0\n" );
2138 fprintf( outfile
, "\t.text\n" );
2141 /*******************************************************************
2142 * BuildCallTo32CBClient
2144 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
2146 * Since the relay stub is itself 32-bit, this should not be a problem;
2147 * unfortunately, the relay stubs are expected to switch back to a
2148 * 16-bit stack (and 16-bit code) after completion :-(
2150 * This would conflict with our 16- vs. 32-bit stack handling, so
2151 * we simply switch *back* to our 32-bit stack before returning to
2154 * The CBClient relay stub expects to be called with:
2155 * - ebp pointing to the 16-bit stack at ss:bp
2156 * - ebx pointing to a buffer containing the saved 16-bit ss:sp
2158 * After completion, the stub will load ss:sp from the buffer at ebx
2159 * and perform a far return to 16-bit code.
2161 * To trick the relay stub into returning to us, we push a 16-bit
2162 * cs:ip pair pointing to our return entry point onto the 16-bit stack,
2163 * followed by a ss:sp pair pointing to *that* cs:ip pair.
2164 * Our return stub thus called will then reload the 32-bit ss:esp and
2165 * return to 32-bit code (by using and ss:esp value that we have also
2166 * pushed onto the 16-bit stack before and a cs:eip values found at
2167 * that position on the 32-bit stack). The layout of our
2168 * temporary area used on the 16-bit stack is thus as follows:
2170 * (ebx+12) 32-bit ss (flat)
2171 * (ebx+8) 32-bit sp (32-bit stack pointer)
2172 * (ebx+6) 16-bit cs (this segment)
2173 * (ebx+4) 16-bit ip ('16-bit' return entry point)
2174 * (ebx+2) 16-bit ss (16-bit stack segment)
2175 * (ebx+0) 16-bit sp (points to ebx+4)
2177 * The second variant of this routine, CALL32_CBClientEx, which is used
2178 * to implement KERNEL.621, has to cope with yet another problem: Here,
2179 * the 32-bit side directly returns to the caller of the CBClient thunklet,
2180 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
2181 * As we have to return to our 32-bit code first, we have to adapt the
2182 * layout of our temporary area so as to include values for the registers
2183 * that are to be restored, and later (in the implementation of KERNEL.621)
2184 * we *really* restore them. The return stub restores DS, DI, SI, and BP
2185 * from the stack, skips the next 8 bytes (CBClient relay code / target),
2186 * and then performs a lret NN, where NN is the number of arguments to be
2187 * removed. Thus, we prepare our temporary area as follows:
2189 * (ebx+22) 16-bit cs (this segment)
2190 * (ebx+20) 16-bit ip ('16-bit' return entry point)
2191 * (ebx+16) 32-bit ss (flat)
2192 * (ebx+12) 32-bit sp (32-bit stack pointer)
2193 * (ebx+10) 16-bit bp (points to ebx+24)
2194 * (ebx+8) 16-bit si (ignored)
2195 * (ebx+6) 16-bit di (ignored)
2196 * (ebx+4) 16-bit ds (we actually use the flat DS here)
2197 * (ebx+2) 16-bit ss (16-bit stack segment)
2198 * (ebx+0) 16-bit sp (points to ebx+4)
2200 * Note that we ensure that DS is not changed and remains the flat segment,
2201 * and the 32-bit stack pointer our own return stub needs fits just
2202 * perfectly into the 8 bytes that are skipped by the Windows stub.
2203 * One problem is that we have to determine the number of removed arguments,
2204 * as these have to be really removed in KERNEL.621. Thus, the BP value
2205 * that we place in the temporary area to be restored, contains the value
2206 * that SP would have if no arguments were removed. By comparing the actual
2207 * value of SP with this value in our return stub we can compute the number
2208 * of removed arguments. This is then returned to KERNEL.621.
2210 * The stack layout of this function:
2211 * (ebp+16) nArgs pointer to variable receiving nr. of args (Ex only)
2212 * (ebp+12) arg ebp value to be set for relay stub
2213 * (ebp+8) func CBClient relay stub address
2217 static void BuildCallTo32CBClient( FILE *outfile
, BOOL isEx
)
2219 char *name
= isEx
? "CBClientEx" : "CBClient";
2220 int size
= isEx
? 24 : 16;
2222 /* Function header */
2224 fprintf( outfile
, "\n\t.align 4\n" );
2226 fprintf( outfile
, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX
"CALL32_%s\n",
2229 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_%s\n", name
);
2230 fprintf( outfile
, PREFIX
"CALL32_%s:\n", name
);
2234 fprintf( outfile
, "\tpushl %%ebp\n" );
2235 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2236 fprintf( outfile
, "\tpushl %%edi\n" );
2237 fprintf( outfile
, "\tpushl %%esi\n" );
2238 fprintf( outfile
, "\tpushl %%ebx\n" );
2240 /* Get the 16-bit stack */
2242 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET
);
2244 /* Convert it to a flat address */
2246 fprintf( outfile
, "\tshldl $16,%%ebx,%%eax\n" );
2247 fprintf( outfile
, "\tandl $0xfff8,%%eax\n" );
2248 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy(%%eax),%%esi\n" );
2249 fprintf( outfile
, "\tmovw %%bx,%%ax\n" );
2250 fprintf( outfile
, "\taddl %%eax,%%esi\n" );
2252 /* Allocate temporary area (simulate STACK16_PUSH) */
2254 fprintf( outfile
, "\tpushf\n" );
2255 fprintf( outfile
, "\tcld\n" );
2256 fprintf( outfile
, "\tleal -%d(%%esi), %%edi\n", size
);
2257 fprintf( outfile
, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME
) );
2258 fprintf( outfile
, "\trep\n\tmovsb\n" );
2259 fprintf( outfile
, "\tpopf\n" );
2261 fprintf( outfile
, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size
, STACKOFFSET
);
2263 fprintf( outfile
, "\tpushl %%edi\n" ); /* remember address */
2265 /* Set up temporary area */
2267 fprintf( outfile
, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME
)-size
+4 );
2268 fprintf( outfile
, "\tmovl %%ebx, (%%edi)\n" );
2272 fprintf( outfile
, "\tmovl " PREFIX
"CALL32_%s_RetAddr, %%eax\n", name
);
2273 fprintf( outfile
, "\tmovl %%eax, 4(%%edi)\n" );
2275 fprintf( outfile
, "\tleal -8(%%esp), %%eax\n" );
2276 fprintf( outfile
, "\tmovl %%eax, 8(%%edi)\n" );
2278 fprintf( outfile
, "\tmovl %%ss, %%ax\n" );
2279 fprintf( outfile
, "\tandl $0x0000ffff, %%eax\n" );
2280 fprintf( outfile
, "\tmovl %%eax, 12(%%edi)\n" );
2284 fprintf( outfile
, "\tmovl %%ds, %%ax\n" );
2285 fprintf( outfile
, "\tmovw %%ax, 4(%%edi)\n" );
2287 fprintf( outfile
, "\taddl $20, %%ebx\n" );
2288 fprintf( outfile
, "\tmovw %%bx, 10(%%edi)\n" );
2290 fprintf( outfile
, "\tleal -8(%%esp), %%eax\n" );
2291 fprintf( outfile
, "\tmovl %%eax, 12(%%edi)\n" );
2293 fprintf( outfile
, "\tmovl %%ss, %%ax\n" );
2294 fprintf( outfile
, "\tandl $0x0000ffff, %%eax\n" );
2295 fprintf( outfile
, "\tmovl %%eax, 16(%%edi)\n" );
2297 fprintf( outfile
, "\tmovl " PREFIX
"CALL32_%s_RetAddr, %%eax\n", name
);
2298 fprintf( outfile
, "\tmovl %%eax, 20(%%edi)\n" );
2301 /* Setup registers and call CBClient relay stub (simulating a far call) */
2303 fprintf( outfile
, "\tmovl %%edi, %%ebx\n" );
2304 fprintf( outfile
, "\tmovl 8(%%ebp), %%eax\n" );
2305 fprintf( outfile
, "\tmovl 12(%%ebp), %%ebp\n" );
2307 fprintf( outfile
, "\tpushl %%cs\n" );
2308 fprintf( outfile
, "\tcall *%%eax\n" );
2310 /* Cleanup temporary area (simulate STACK16_POP) */
2312 fprintf( outfile
, "\tpop %%esi\n" );
2314 fprintf( outfile
, "\tpushf\n" );
2315 fprintf( outfile
, "\tstd\n" );
2316 fprintf( outfile
, "\tdec %%esi\n" );
2317 fprintf( outfile
, "\tleal %d(%%esi), %%edi\n", size
);
2318 fprintf( outfile
, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME
) );
2319 fprintf( outfile
, "\trep\n\tmovsb\n" );
2320 fprintf( outfile
, "\tpopf\n" );
2322 fprintf( outfile
, "\t.byte 0x64\n\taddw $%d,(%d)\n", size
, STACKOFFSET
);
2324 /* Return argument size to caller */
2327 fprintf( outfile
, "\tmovl 28(%%esp), %%ebx\n" );
2328 fprintf( outfile
, "\tmovl %%ebp, (%%ebx)\n" );
2331 /* Restore registers and return */
2333 fprintf( outfile
, "\tpopl %%ebx\n" );
2334 fprintf( outfile
, "\tpopl %%esi\n" );
2335 fprintf( outfile
, "\tpopl %%edi\n" );
2336 fprintf( outfile
, "\tpopl %%ebp\n" );
2337 fprintf( outfile
, "\tret\n" );
2339 /* '16-bit' return stub */
2341 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_%s_Ret\n", name
);
2342 fprintf( outfile
, PREFIX
"CALL32_%s_Ret:\n", name
);
2346 fprintf( outfile
, "\tmovzwl %%sp, %%ebx\n" );
2347 fprintf( outfile
, "\tlssl %%ss:(%%ebx), %%esp\n" );
2351 fprintf( outfile
, "\tmovzwl %%bp, %%ebx\n" );
2352 fprintf( outfile
, "\tsubw %%bp, %%sp\n" );
2353 fprintf( outfile
, "\tmovzwl %%sp, %%ebp\n" );
2354 fprintf( outfile
, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
2356 fprintf( outfile
, "\tlret\n" );
2358 /* Declare the return address variable */
2360 fprintf( outfile
, "\t.data\n" );
2361 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_%s_RetAddr\n", name
);
2362 fprintf( outfile
, PREFIX
"CALL32_%s_RetAddr:\t.long 0\n", name
);
2363 fprintf( outfile
, "\t.text\n" );
2368 /*******************************************************************
2369 * BuildCallTo32LargeStack
2371 * Build the function used to switch to the original 32-bit stack
2372 * before calling a 32-bit function from 32-bit code. This is used for
2373 * functions that need a large stack, like X bitmaps functions.
2375 * The generated function has the following prototype:
2376 * int xxx( int (*func)(), void *arg );
2378 * The pointer to the function can be retrieved by calling CALL32_Init,
2379 * which also takes care of saving the current 32-bit stack pointer.
2381 * NOTE: The CALL32_LargeStack routine may be recursively entered by the
2382 * same thread, but not concurrently entered by several threads.
2391 static void BuildCallTo32LargeStack( FILE *outfile
)
2393 /* Initialization function */
2395 fprintf( outfile
, "\n\t.align 4\n" );
2397 fprintf( outfile
, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX
"CALL32_Init\n");
2399 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Init\n" );
2400 fprintf( outfile
, "\t.type " PREFIX
"CALL32_Init,@function\n" );
2401 fprintf( outfile
, PREFIX
"CALL32_Init:\n" );
2402 fprintf( outfile
, "\tleal -256(%%esp),%%eax\n" );
2403 fprintf( outfile
, "\tmovl %%eax,CALL32_Original32_esp\n" );
2404 fprintf( outfile
, "\tmovl $CALL32_LargeStack,%%eax\n" );
2405 fprintf( outfile
, "\tret\n" );
2407 /* Function header */
2409 fprintf( outfile
, "\n\t.align 4\n" );
2411 fprintf( outfile
, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2413 fprintf( outfile
, "CALL32_LargeStack:\n" );
2417 fprintf( outfile
, "\tpushl %%ebp\n" );
2418 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2420 /* Switch to the original 32-bit stack pointer */
2422 fprintf( outfile
, "\tcmpl $0, CALL32_RecursionCount\n" );
2423 fprintf( outfile
, "\tjne CALL32_skip\n" );
2424 fprintf( outfile
, "\tmovl CALL32_Original32_esp, %%esp\n" );
2425 fprintf( outfile
, "CALL32_skip:\n" );
2427 fprintf( outfile
, "\tincl CALL32_RecursionCount\n" );
2429 /* Transfer the argument and call the function */
2431 fprintf( outfile
, "\tpushl 12(%%ebp)\n" );
2432 fprintf( outfile
, "\tcall *8(%%ebp)\n" );
2434 /* Restore registers and return */
2436 fprintf( outfile
, "\tdecl CALL32_RecursionCount\n" );
2438 fprintf( outfile
, "\tmovl %%ebp,%%esp\n" );
2439 fprintf( outfile
, "\tpopl %%ebp\n" );
2440 fprintf( outfile
, "\tret\n" );
2444 fprintf( outfile
, "\t.data\n" );
2445 fprintf( outfile
, "CALL32_Original32_esp:\t.long 0\n" );
2446 fprintf( outfile
, "CALL32_RecursionCount:\t.long 0\n" );
2447 fprintf( outfile
, "\t.text\n" );
2451 /*******************************************************************
2452 * BuildCallFrom32Regs
2454 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2455 * 'args' is the number of dword arguments.
2459 * (esp+336) ret addr (or relay addr when debugging(relay) is on)
2460 * (esp+332) entry point
2461 * (esp+204) buffer area to allow stack frame manipulation
2462 * (esp+0) CONTEXT struct
2464 static void BuildCallFrom32Regs( FILE *outfile
)
2466 #define STACK_SPACE 128
2468 /* Function header */
2470 fprintf( outfile
, "\n\t.align 4\n" );
2472 fprintf( outfile
, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX
"CALL32_Regs\n" );
2474 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Regs\n" );
2475 fprintf( outfile
, PREFIX
"CALL32_Regs:\n" );
2477 /* Allocate some buffer space on the stack */
2479 fprintf( outfile
, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE
);
2481 /* Build the context structure */
2483 fprintf( outfile
, "\tpushw $0\n" );
2484 fprintf( outfile
, "\t.byte 0x66\n\tpushl %%ss\n" );
2485 fprintf( outfile
, "\tpushl %%eax\n" ); /* %esp place holder */
2486 fprintf( outfile
, "\tpushfl\n" );
2487 fprintf( outfile
, "\tpushw $0\n" );
2488 fprintf( outfile
, "\t.byte 0x66\n\tpushl %%cs\n" );
2489 fprintf( outfile
, "\tpushl %d(%%esp)\n", 16+STACK_SPACE
+4 ); /* %eip at time of call */
2490 fprintf( outfile
, "\tpushl %%ebp\n" );
2492 fprintf( outfile
, "\tpushl %%eax\n" );
2493 fprintf( outfile
, "\tpushl %%ecx\n" );
2494 fprintf( outfile
, "\tpushl %%edx\n" );
2495 fprintf( outfile
, "\tpushl %%ebx\n" );
2496 fprintf( outfile
, "\tpushl %%esi\n" );
2497 fprintf( outfile
, "\tpushl %%edi\n" );
2499 fprintf( outfile
, "\txorl %%eax,%%eax\n" );
2500 fprintf( outfile
, "\tmovw %%ds,%%ax\n" );
2501 fprintf( outfile
, "\tpushl %%eax\n" );
2502 fprintf( outfile
, "\tmovw %%es,%%ax\n" );
2503 fprintf( outfile
, "\tpushl %%eax\n" );
2504 fprintf( outfile
, "\tmovw %%fs,%%ax\n" );
2505 fprintf( outfile
, "\tpushl %%eax\n" );
2506 fprintf( outfile
, "\tmovw %%gs,%%ax\n" );
2507 fprintf( outfile
, "\tpushl %%eax\n" );
2509 fprintf( outfile
, "\tleal -%d(%%esp),%%esp\n",
2510 sizeof(FLOATING_SAVE_AREA
) + 6 * sizeof(DWORD
) /* DR regs */ );
2511 fprintf( outfile
, "\tpushl $0x0001001f\n" ); /* ContextFlags */
2513 fprintf( outfile
, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave
) );
2515 fprintf( outfile
, "\tleal %d(%%esp),%%eax\n",
2516 sizeof(CONTEXT
) + STACK_SPACE
+ 4 ); /* %esp at time of call */
2517 fprintf( outfile
, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Esp
) );
2519 fprintf( outfile
, "\tcall " PREFIX
"RELAY_CallFrom32Regs\n" );
2521 /* Restore the context structure */
2523 fprintf( outfile
, "\tfrstor %d(%%esp)\n", CONTEXTOFFSET(FloatSave
) );
2525 /* Store %eip value onto the new stack */
2527 fprintf( outfile
, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Eip
) );
2528 fprintf( outfile
, "\tmovl %d(%%esp),%%ebx\n", CONTEXTOFFSET(Esp
) );
2529 fprintf( outfile
, "\tmovl %%eax,0(%%ebx)\n" );
2531 /* Restore all registers */
2533 fprintf( outfile
, "\tleal %d(%%esp),%%esp\n",
2534 sizeof(FLOATING_SAVE_AREA
) + 7 * sizeof(DWORD
) );
2535 fprintf( outfile
, "\tpopl %%eax\n" );
2536 fprintf( outfile
, "\tmovw %%ax,%%gs\n" );
2537 fprintf( outfile
, "\tpopl %%eax\n" );
2538 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2539 fprintf( outfile
, "\tpopl %%eax\n" );
2540 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2541 fprintf( outfile
, "\tpopl %%eax\n" );
2542 fprintf( outfile
, "\tmovw %%ax,%%ds\n" );
2544 fprintf( outfile
, "\tpopl %%edi\n" );
2545 fprintf( outfile
, "\tpopl %%esi\n" );
2546 fprintf( outfile
, "\tpopl %%ebx\n" );
2547 fprintf( outfile
, "\tpopl %%edx\n" );
2548 fprintf( outfile
, "\tpopl %%ecx\n" );
2549 fprintf( outfile
, "\tpopl %%eax\n" );
2550 fprintf( outfile
, "\tpopl %%ebp\n" );
2551 fprintf( outfile
, "\tleal 8(%%esp),%%esp\n" ); /* skip %eip and %cs */
2552 fprintf( outfile
, "\tpopfl\n" );
2553 fprintf( outfile
, "\tpopl %%esp\n" );
2554 fprintf( outfile
, "\tret\n" );
2560 /*******************************************************************
2563 * Build the spec files
2565 static int BuildSpec( FILE *outfile
, int argc
, char *argv
[] )
2568 for (i
= 2; i
< argc
; i
++)
2569 if (BuildSpecFile( outfile
, argv
[i
] ) < 0) return -1;
2574 /*******************************************************************
2577 * Build the 16-bit-to-Wine callbacks
2579 static int BuildCallFrom16( FILE *outfile
, char * outname
, int argc
, char *argv
[] )
2586 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2587 fprintf( outfile
, "\t.text\n" );
2590 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2591 getcwd(buffer
, sizeof(buffer
));
2594 * The stabs help the internal debugger as they are an indication that it
2595 * is sensible to step into a thunk/trampoline.
2597 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2598 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2599 fprintf( outfile
, "\t.text\n" );
2600 fprintf( outfile
, "\t.align 4\n" );
2601 fprintf( outfile
, "Code_Start:\n\n" );
2603 fprintf( outfile
, PREFIX
"CallFrom16_Start:\n" );
2604 fprintf( outfile
, "\t.globl "PREFIX
"CallFrom16_Start\n" );
2606 /* Build the callback functions */
2608 for (i
= 2; i
< argc
; i
++) BuildCallFrom16Func( outfile
, argv
[i
] );
2610 /* Build the thunk callback function */
2612 BuildCallFrom16Func( outfile
, "t_long_" );
2614 /* Output the argument debugging strings */
2618 fprintf( outfile
, "/* Argument strings */\n" );
2619 for (i
= 2; i
< argc
; i
++)
2621 fprintf( outfile
, "Profile_%s:\t", argv
[i
] );
2622 fprintf( outfile
, STRING
" \"%s\\0\"\n", argv
[i
] + 7 );
2625 fprintf( outfile
, PREFIX
"CallFrom16_End:\n" );
2626 fprintf( outfile
, "\t.globl "PREFIX
"CallFrom16_End\n" );
2629 fprintf( outfile
, "\t.text\n");
2630 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2631 fprintf( outfile
, ".Letext:\n");
2638 /*******************************************************************
2641 * Build the Wine-to-16-bit callbacks
2643 static int BuildCallTo16( FILE *outfile
, char * outname
, int argc
, char *argv
[] )
2650 infile
= fopen( argv
[2], "r" );
2657 else infile
= stdin
;
2661 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2662 fprintf( outfile
, "\t.text\n" );
2665 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2666 getcwd(buffer
, sizeof(buffer
));
2669 * The stabs help the internal debugger as they are an indication that it
2670 * is sensible to step into a thunk/trampoline.
2672 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2673 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2674 fprintf( outfile
, "\t.text\n" );
2675 fprintf( outfile
, "\t.align 4\n" );
2676 fprintf( outfile
, "Code_Start:\n\n" );
2679 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Start\n" );
2680 fprintf( outfile
, PREFIX
"CALLTO16_Start:\n" );
2682 /* Build the callback functions */
2684 while (fgets( buffer
, sizeof(buffer
), infile
))
2686 if (strstr( buffer
, "### start build ###" )) break;
2688 while (fgets( buffer
, sizeof(buffer
), infile
))
2690 char *p
= strstr( buffer
, "CallTo16_" );
2693 char *profile
= p
+ strlen( "CallTo16_" );
2695 while ((*p
== '_') || isalpha(*p
)) p
++;
2697 BuildCallTo16Func( outfile
, profile
);
2699 if (strstr( buffer
, "### stop build ###" )) break;
2702 /* Output the 16-bit return code */
2704 BuildRet16Func( outfile
);
2706 /* Output the CBClient callback functions
2707 * (while this does not really 'call to 16-bit' code, it is placed
2708 * here so that its 16-bit return stub is defined within the CALLTO16
2711 BuildCallTo32CBClient( outfile
, FALSE
);
2712 BuildCallTo32CBClient( outfile
, TRUE
);
2715 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_End\n" );
2716 fprintf( outfile
, PREFIX
"CALLTO16_End:\n" );
2719 fprintf( outfile
, "\t.text\n");
2720 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2721 fprintf( outfile
, ".Letext:\n");
2729 /*******************************************************************
2732 * Build the 32-bit callbacks
2734 static int BuildCall32( FILE *outfile
, char * outname
)
2740 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2741 fprintf( outfile
, "\t.text\n" );
2746 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2747 getcwd(buffer
, sizeof(buffer
));
2750 * The stabs help the internal debugger as they are an indication that it
2751 * is sensible to step into a thunk/trampoline.
2753 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2754 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2755 fprintf( outfile
, "\t.text\n" );
2756 fprintf( outfile
, "\t.align 4\n" );
2757 fprintf( outfile
, "Code_Start:\n" );
2760 /* Build the 32-bit large stack callback */
2762 BuildCallTo32LargeStack( outfile
);
2764 /* Build the register callback function */
2766 BuildCallFrom32Regs( outfile
);
2769 fprintf( outfile
, "\t.text\n");
2770 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2771 fprintf( outfile
, ".Letext:\n");
2774 #else /* __i386__ */
2776 /* Just to avoid an empty file */
2777 fprintf( outfile
, "\t.long 0\n" );
2779 #endif /* __i386__ */
2784 /*******************************************************************
2787 static void usage(void)
2790 "usage: build [-o outfile] -spec SPECNAMES\n"
2791 " build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2792 " build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2793 " build [-o outfile] -call32\n" );
2798 /*******************************************************************
2801 int main(int argc
, char **argv
)
2803 char *outname
= NULL
;
2804 FILE *outfile
= stdout
;
2807 if (argc
< 2) usage();
2809 if (!strcmp( argv
[1], "-o" ))
2814 if (argc
< 2) usage();
2815 if (!(outfile
= fopen( outname
, "w" )))
2817 fprintf( stderr
, "Unable to create output file '%s'\n", outname
);
2822 /* Retrieve the selector values; this assumes that we are building
2823 * the asm files on the platform that will also run them. Probably
2824 * a safe assumption to make.
2826 GET_CS( Code_Selector
);
2827 GET_DS( Data_Selector
);
2829 if (!strcmp( argv
[1], "-spec" ))
2830 res
= BuildSpec( outfile
, argc
, argv
);
2831 else if (!strcmp( argv
[1], "-callfrom16" ))
2832 res
= BuildCallFrom16( outfile
, outname
, argc
, argv
);
2833 else if (!strcmp( argv
[1], "-callto16" ))
2834 res
= BuildCallTo16( outfile
, outname
, argc
, argv
);
2835 else if (!strcmp( argv
[1], "-call32" ))
2836 res
= BuildCall32( outfile
, outname
);