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) */
57 TYPE_FORWARD
, /* forwarded function (Win32) */
61 static const char * const TypeNames
[TYPE_NBTYPES
] =
64 "byte", /* TYPE_BYTE */
65 "word", /* TYPE_WORD */
66 "long", /* TYPE_LONG */
67 "pascal16", /* TYPE_PASCAL_16 */
68 "pascal", /* TYPE_PASCAL */
69 "equate", /* TYPE_ABS */
70 "return", /* TYPE_RETURN */
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 debugging
= 1;
173 /* Offset of a structure field relative to the start of the struct */
174 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
176 /* Offset of register relative to the start of the CONTEXT struct */
177 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT,reg)
179 /* Offset of the stack pointer relative to %fs:(0) */
180 #define STACKOFFSET (STRUCTOFFSET(THDB,cur_stack) - STRUCTOFFSET(THDB,teb))
183 static void *xmalloc (size_t size
)
187 res
= malloc (size
? size
: 1);
190 fprintf (stderr
, "Virtual memory exhausted.\n");
197 static void *xrealloc (void *ptr
, size_t size
)
199 void *res
= realloc (ptr
, size
);
202 fprintf (stderr
, "Virtual memory exhausted.\n");
208 static char *xstrdup( const char *str
)
210 char *res
= strdup( str
);
213 fprintf (stderr
, "Virtual memory exhausted.\n");
219 static int IsNumberString(char *s
)
228 static char *strupper(char *s
)
232 for(p
= s
; *p
!= '\0'; p
++)
238 static char * GetTokenInLine(void)
243 if (ParseNext
!= ParseBuffer
)
245 if (ParseSaveChar
== '\0')
247 *ParseNext
= ParseSaveChar
;
251 * Remove initial white space.
253 for (p
= ParseNext
; isspace(*p
); p
++)
256 if ((*p
== '\0') || (*p
== '#'))
263 if (*token
!= '(' && *token
!= ')')
264 while (*p
!= '\0' && *p
!= '(' && *p
!= ')' && !isspace(*p
))
274 static char * GetToken(void)
278 if (ParseBuffer
== NULL
)
280 ParseBuffer
= xmalloc(512);
281 ParseNext
= ParseBuffer
;
285 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
287 if (ParseBuffer
[0] != '#')
292 while ((token
= GetTokenInLine()) == NULL
)
294 ParseNext
= ParseBuffer
;
298 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
300 if (ParseBuffer
[0] != '#')
309 /*******************************************************************
312 * Parse a variable definition.
314 static int ParseVariable( ORDDEF
*odp
)
319 int value_array_size
;
321 char *token
= GetToken();
324 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
325 SpecName
, Line
, token
);
330 value_array_size
= 25;
331 value_array
= xmalloc(sizeof(*value_array
) * value_array_size
);
333 while ((token
= GetToken()) != NULL
)
338 value_array
[n_values
++] = strtol(token
, &endptr
, 0);
339 if (n_values
== value_array_size
)
341 value_array_size
+= 25;
342 value_array
= xrealloc(value_array
,
343 sizeof(*value_array
) * value_array_size
);
346 if (endptr
== NULL
|| *endptr
!= '\0')
348 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
349 SpecName
, Line
, token
);
356 fprintf(stderr
, "%s:%d: End of file in variable declaration\n",
361 odp
->u
.var
.n_values
= n_values
;
362 odp
->u
.var
.values
= xrealloc(value_array
, sizeof(*value_array
) * n_values
);
368 /*******************************************************************
369 * ParseExportFunction
371 * Parse a function definition.
373 static int ParseExportFunction( ORDDEF
*odp
)
381 if (odp
->type
== TYPE_STDCALL
)
383 fprintf( stderr
, "%s:%d: 'stdcall' not supported for Win16\n",
389 if ((odp
->type
== TYPE_PASCAL
) || (odp
->type
== TYPE_PASCAL_16
))
391 fprintf( stderr
, "%s:%d: 'pascal' not supported for Win32\n",
403 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
404 SpecName
, Line
, token
);
408 for (i
= 0; i
< sizeof(odp
->u
.func
.arg_types
)-1; i
++)
414 if (!strcmp(token
, "word"))
415 odp
->u
.func
.arg_types
[i
] = 'w';
416 else if (!strcmp(token
, "s_word"))
417 odp
->u
.func
.arg_types
[i
] = 's';
418 else if (!strcmp(token
, "long") || !strcmp(token
, "segptr"))
419 odp
->u
.func
.arg_types
[i
] = 'l';
420 else if (!strcmp(token
, "ptr"))
421 odp
->u
.func
.arg_types
[i
] = 'p';
422 else if (!strcmp(token
, "str"))
423 odp
->u
.func
.arg_types
[i
] = 't';
424 else if (!strcmp(token
, "wstr"))
425 odp
->u
.func
.arg_types
[i
] = 'W';
426 else if (!strcmp(token
, "segstr"))
427 odp
->u
.func
.arg_types
[i
] = 'T';
428 else if (!strcmp(token
, "double"))
430 odp
->u
.func
.arg_types
[i
++] = 'l';
431 odp
->u
.func
.arg_types
[i
] = 'l';
435 fprintf(stderr
, "%s:%d: Unknown variable type '%s'\n",
436 SpecName
, Line
, token
);
439 if (SpecType
== SPEC_WIN32
)
441 if (strcmp(token
, "long") &&
442 strcmp(token
, "ptr") &&
443 strcmp(token
, "str") &&
444 strcmp(token
, "wstr") &&
445 strcmp(token
, "double"))
447 fprintf( stderr
, "%s:%d: Type '%s' not supported for Win32\n",
448 SpecName
, Line
, token
);
453 if ((*token
!= ')') || (i
>= sizeof(odp
->u
.func
.arg_types
)))
455 fprintf( stderr
, "%s:%d: Too many arguments\n", SpecName
, Line
);
458 odp
->u
.func
.arg_types
[i
] = '\0';
459 if ((odp
->type
== TYPE_STDCALL
) && !i
)
460 odp
->type
= TYPE_CDECL
; /* stdcall is the same as cdecl for 0 args */
461 strcpy(odp
->u
.func
.link_name
, GetToken());
466 /*******************************************************************
469 * Parse an 'equate' definition.
471 static int ParseEquate( ORDDEF
*odp
)
475 char *token
= GetToken();
476 int value
= strtol(token
, &endptr
, 0);
477 if (endptr
== NULL
|| *endptr
!= '\0')
479 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
480 SpecName
, Line
, token
);
484 if (SpecType
== SPEC_WIN32
)
486 fprintf( stderr
, "%s:%d: 'equate' not supported for Win32\n",
491 odp
->u
.abs
.value
= value
;
496 /*******************************************************************
499 * Parse a 'return' definition.
501 static int ParseReturn( ORDDEF
*odp
)
507 odp
->u
.ret
.arg_size
= strtol(token
, &endptr
, 0);
508 if (endptr
== NULL
|| *endptr
!= '\0')
510 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
511 SpecName
, Line
, token
);
516 odp
->u
.ret
.ret_value
= strtol(token
, &endptr
, 0);
517 if (endptr
== NULL
|| *endptr
!= '\0')
519 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
520 SpecName
, Line
, token
);
524 if (SpecType
== SPEC_WIN32
)
526 fprintf( stderr
, "%s:%d: 'return' not supported for Win32\n",
535 /*******************************************************************
538 * Parse a 'stub' definition.
540 static int ParseStub( ORDDEF
*odp
)
542 odp
->u
.func
.arg_types
[0] = '\0';
543 strcpy( odp
->u
.func
.link_name
, STUB_CALLBACK
);
548 /*******************************************************************
551 * Parse an 'varargs' definition.
553 static int ParseVarargs( ORDDEF
*odp
)
557 if (SpecType
== SPEC_WIN16
)
559 fprintf( stderr
, "%s:%d: 'varargs' not supported for Win16\n",
567 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
568 SpecName
, Line
, token
);
574 fprintf(stderr
, "%s:%d: Expected ')' got '%s'\n",
575 SpecName
, Line
, token
);
579 strcpy( odp
->u
.vargs
.link_name
, GetToken() );
584 /*******************************************************************
587 * Parse an 'extern' definition.
589 static int ParseExtern( ORDDEF
*odp
)
591 if (SpecType
== SPEC_WIN16
)
593 fprintf( stderr
, "%s:%d: 'extern' not supported for Win16\n",
597 strcpy( odp
->u
.ext
.link_name
, GetToken() );
602 /*******************************************************************
605 * Parse a 'forward' definition.
607 static int ParseForward( ORDDEF
*odp
)
609 if (SpecType
== SPEC_WIN16
)
611 fprintf( stderr
, "%s:%d: 'forward' not supported for Win16\n",
615 strcpy( odp
->u
.fwd
.link_name
, GetToken() );
620 /*******************************************************************
623 * Parse an ordinal definition.
625 static int ParseOrdinal(int ordinal
)
630 if (ordinal
>= MAX_ORDINALS
)
632 fprintf(stderr
, "%s:%d: Ordinal number too large\n", SpecName
, Line
);
635 if (ordinal
> Limit
) Limit
= ordinal
;
636 if (ordinal
< Base
) Base
= ordinal
;
638 odp
= &OrdinalDefinitions
[ordinal
];
639 if (!(token
= GetToken()))
641 fprintf(stderr
, "%s:%d: Expected type after ordinal\n", SpecName
, Line
);
645 for (odp
->type
= 0; odp
->type
< TYPE_NBTYPES
; odp
->type
++)
646 if (TypeNames
[odp
->type
] && !strcmp( token
, TypeNames
[odp
->type
] ))
649 if (odp
->type
>= TYPE_NBTYPES
)
652 "%s:%d: Expected type after ordinal, found '%s' instead\n",
653 SpecName
, Line
, token
);
657 if (!(token
= GetToken()))
659 fprintf( stderr
, "%s:%d: Expected name after type\n", SpecName
, Line
);
662 strcpy( odp
->name
, token
);
670 return ParseVariable( odp
);
676 return ParseExportFunction( odp
);
678 return ParseEquate( odp
);
680 return ParseReturn( odp
);
682 return ParseStub( odp
);
684 return ParseVarargs( odp
);
686 return ParseExtern( odp
);
688 return ParseForward( odp
);
690 fprintf( stderr
, "Should not happen\n" );
696 /*******************************************************************
701 static int ParseTopLevel(void)
705 while ((token
= GetToken()) != NULL
)
707 if (strcmp(token
, "name") == 0)
709 strcpy(DLLName
, GetToken());
711 if (!DLLFileName
[0]) sprintf( DLLFileName
, "%s.DLL", DLLName
);
713 else if (strcmp(token
, "file") == 0)
715 strcpy(DLLFileName
, GetToken());
716 strupper(DLLFileName
);
718 else if (strcmp(token
, "type") == 0)
721 if (!strcmp(token
, "win16" )) SpecType
= SPEC_WIN16
;
722 else if (!strcmp(token
, "win32" )) SpecType
= SPEC_WIN32
;
725 fprintf(stderr
, "%s:%d: Type must be 'win16' or 'win32'\n",
730 else if (strcmp(token
, "heap") == 0)
733 if (!IsNumberString(token
))
735 fprintf(stderr
, "%s:%d: Expected number after heap\n",
739 DLLHeapSize
= atoi(token
);
741 else if (strcmp(token
, "init") == 0)
743 strcpy(DLLInitFunc
, GetToken());
745 fprintf(stderr
, "%s:%d: Expected function name after init\n", SpecName
, Line
);
747 else if (strcmp(token
, "import") == 0)
749 if (nb_imports
>= MAX_IMPORTS
)
751 fprintf( stderr
, "%s:%d: Too many imports (limit %d)\n",
752 SpecName
, Line
, MAX_IMPORTS
);
755 if (SpecType
!= SPEC_WIN32
)
757 fprintf( stderr
, "%s:%d: Imports not supported for Win16\n", SpecName
, Line
);
760 DLLImports
[nb_imports
++] = xstrdup(GetToken());
762 else if (IsNumberString(token
))
767 ordinal
= atoi(token
);
768 if ((rv
= ParseOrdinal(ordinal
)) < 0)
774 "%s:%d: Expected name, id, length or ordinal\n",
784 /*******************************************************************
787 * Store a list of ints into a byte array.
789 static int StoreVariableCode( unsigned char *buffer
, int size
, ORDDEF
*odp
)
796 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
797 buffer
[i
] = odp
->u
.var
.values
[i
];
800 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
801 ((unsigned short *)buffer
)[i
] = odp
->u
.var
.values
[i
];
804 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
805 ((unsigned int *)buffer
)[i
] = odp
->u
.var
.values
[i
];
808 return odp
->u
.var
.n_values
* size
;
812 /*******************************************************************
815 * Dump a byte stream into the assembly code.
817 static void DumpBytes( FILE *outfile
, const unsigned char *data
, int len
,
818 const char *section
, const char *label_start
)
821 if (section
) fprintf( outfile
, "\t%s\n", section
);
822 if (label_start
) fprintf( outfile
, "%s:\n", label_start
);
823 for (i
= 0; i
< len
; i
++)
825 if (!(i
& 0x0f)) fprintf( outfile
, "\t.byte " );
826 fprintf( outfile
, "%d", *data
++ );
828 fprintf( outfile
, "%c", ((i
& 0x0f) != 0x0f) ? ',' : '\n' );
830 fprintf( outfile
, "\n" );
834 /*******************************************************************
837 * Build the in-memory representation of a 16-bit NE module, and dump it
838 * as a byte stream into the assembly code.
840 static int BuildModule16( FILE *outfile
, int max_code_offset
,
841 int max_data_offset
)
847 SEGTABLEENTRY
*pSegment
;
851 ET_BUNDLE
*bundle
= 0;
856 * OFSTRUCT File information
857 * SEGTABLEENTRY Segment 1 (code)
858 * SEGTABLEENTRY Segment 2 (data)
859 * WORD[2] Resource table (empty)
860 * BYTE[2] Imported names (empty)
861 * BYTE[n] Resident names table
862 * BYTE[n] Entry table
865 buffer
= xmalloc( 0x10000 );
867 pModule
= (NE_MODULE
*)buffer
;
868 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
871 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_BUILTIN
| NE_FFLAGS_LIBMODULE
;
873 pModule
->heap_size
= DLLHeapSize
;
874 pModule
->stack_size
= 0;
879 pModule
->seg_count
= 2;
880 pModule
->modref_count
= 0;
881 pModule
->nrname_size
= 0;
882 pModule
->modref_table
= 0;
883 pModule
->nrname_fpos
= 0;
884 pModule
->moveable_entries
= 0;
885 pModule
->alignment
= 0;
886 pModule
->truetype
= 0;
887 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
888 pModule
->misc_flags
= 0;
889 pModule
->dlls_to_init
= 0;
890 pModule
->nrname_handle
= 0;
891 pModule
->min_swap_area
= 0;
892 pModule
->expected_version
= 0x030a;
893 pModule
->module32
= 0;
895 pModule
->self_loading_sel
= 0;
897 /* File information */
899 pFileInfo
= (OFSTRUCT
*)(pModule
+ 1);
900 pModule
->fileinfo
= (int)pFileInfo
- (int)pModule
;
901 memset( pFileInfo
, 0, sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
) );
902 pFileInfo
->cBytes
= sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
)
903 + strlen(DLLFileName
);
904 strcpy( pFileInfo
->szPathName
, DLLFileName
);
905 pstr
= (char *)pFileInfo
+ pFileInfo
->cBytes
+ 1;
909 pSegment
= (SEGTABLEENTRY
*)pstr
;
910 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
911 pSegment
->filepos
= 0;
912 pSegment
->size
= max_code_offset
;
914 pSegment
->minsize
= max_code_offset
;
918 pModule
->dgroup_entry
= (int)pSegment
- (int)pModule
;
919 pSegment
->filepos
= 0;
920 pSegment
->size
= max_data_offset
;
921 pSegment
->flags
= NE_SEGFLAGS_DATA
;
922 pSegment
->minsize
= max_data_offset
;
928 pword
= (WORD
*)pSegment
;
929 pModule
->res_table
= (int)pword
- (int)pModule
;
933 /* Imported names table */
935 pstr
= (char *)pword
;
936 pModule
->import_table
= (int)pstr
- (int)pModule
;
940 /* Resident names table */
942 pModule
->name_table
= (int)pstr
- (int)pModule
;
943 /* First entry is module name */
944 *pstr
= strlen(DLLName
);
945 strcpy( pstr
+ 1, DLLName
);
948 pstr
+= sizeof(WORD
);
949 /* Store all ordinals */
950 odp
= OrdinalDefinitions
+ 1;
951 for (i
= 1; i
<= Limit
; i
++, odp
++)
953 if (!odp
->name
[0]) continue;
954 *pstr
= strlen( odp
->name
);
955 strcpy( pstr
+ 1, odp
->name
);
956 strupper( pstr
+ 1 );
959 pstr
+= sizeof(WORD
);
965 pModule
->entry_table
= (int)pstr
- (int)pModule
;
966 odp
= OrdinalDefinitions
+ 1;
967 for (i
= 1; i
<= Limit
; i
++, odp
++)
979 selector
= 1; /* Code selector */
985 selector
= 2; /* Data selector */
989 selector
= 0xfe; /* Constant selector */
993 selector
= 0; /* Invalid selector */
1000 if ( bundle
&& bundle
->last
+1 == i
)
1005 bundle
->next
= (char *)pstr
- (char *)pModule
;
1007 bundle
= (ET_BUNDLE
*)pstr
;
1008 bundle
->first
= i
-1;
1011 pstr
+= sizeof(ET_BUNDLE
);
1014 /* FIXME: is this really correct ?? */
1015 entry
= (ET_ENTRY
*)pstr
;
1016 entry
->type
= 0xff; /* movable */
1017 entry
->flags
= 3; /* exported & public data */
1018 entry
->segnum
= selector
;
1019 entry
->offs
= odp
->offset
;
1020 pstr
+= sizeof(ET_ENTRY
);
1024 /* Dump the module content */
1026 DumpBytes( outfile
, (char *)pModule
, (int)pstr
- (int)pModule
,
1027 ".data", "Module_Start" );
1028 return (int)pstr
- (int)pModule
;
1032 /*******************************************************************
1035 * Build a Win32 C file from a spec file.
1037 static int BuildSpec32File( char * specfile
, FILE *outfile
)
1040 int i
, nb_names
, fwd_size
= 0;
1042 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
1044 fprintf( outfile
, "#include \"builtin32.h\"\n\n" );
1046 /* Output code for all stubs functions */
1048 fprintf( outfile
, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
1050 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1052 if (odp
->type
!= TYPE_STUB
) continue;
1053 fprintf( outfile
, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
1057 /* Output code for all register functions */
1059 fprintf( outfile
, "#ifdef __i386__\n" );
1060 fprintf( outfile
, "#ifndef __GNUC__\n" );
1061 fprintf( outfile
, "static void __asm__dummy() {\n" );
1062 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
1063 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1065 if (odp
->type
!= TYPE_REGISTER
) continue;
1067 "__asm__(\".align 4\\n\\t\"\n"
1068 " \".globl " PREFIX
"%s\\n\\t\"\n"
1069 " \".type " PREFIX
"%s,@function\\n\\t\"\n"
1070 " \"" PREFIX
"%s:\\n\\t\"\n"
1071 " \"call " PREFIX
"CALL32_Regs\\n\\t\"\n"
1072 " \".long " PREFIX
"__regs_%s\\n\\t\"\n"
1073 " \".byte %d,%d\");\n",
1074 odp
->u
.func
.link_name
, odp
->u
.func
.link_name
,
1075 odp
->u
.func
.link_name
, odp
->u
.func
.link_name
,
1076 4 * strlen(odp
->u
.func
.arg_types
),
1077 4 * strlen(odp
->u
.func
.arg_types
) );
1079 fprintf( outfile
, "#ifndef __GNUC__\n" );
1080 fprintf( outfile
, "}\n" );
1081 fprintf( outfile
, "#endif /* !defined(__GNUC__) */\n" );
1082 fprintf( outfile
, "#endif /* defined(__i386__) */\n" );
1084 /* Output the DLL functions prototypes */
1086 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1091 fprintf( outfile
, "extern void %s();\n", odp
->u
.vargs
.link_name
);
1094 fprintf( outfile
, "extern void %s();\n", odp
->u
.ext
.link_name
);
1099 fprintf( outfile
, "extern void %s();\n", odp
->u
.func
.link_name
);
1102 fwd_size
+= strlen(odp
->u
.fwd
.link_name
) + 1;
1108 fprintf(stderr
,"build: function type %d not available for Win32\n",
1114 /* Output LibMain function */
1115 if (DLLInitFunc
[0]) fprintf( outfile
, "extern void %s();\n", DLLInitFunc
);
1118 /* Output the DLL functions table */
1120 fprintf( outfile
, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1122 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1127 fprintf( outfile
, " 0" );
1130 fprintf( outfile
, " %s", odp
->u
.vargs
.link_name
);
1133 fprintf( outfile
, " %s", odp
->u
.ext
.link_name
);
1138 fprintf( outfile
, " %s", odp
->u
.func
.link_name
);
1141 fprintf( outfile
, " __stub_%d", i
);
1144 fprintf( outfile
, " (ENTRYPOINT32)\"%s\"", odp
->u
.fwd
.link_name
);
1149 if (i
< Limit
) fprintf( outfile
, ",\n" );
1151 fprintf( outfile
, "\n};\n\n" );
1153 /* Output the DLL names table */
1156 fprintf( outfile
, "static const char * const FuncNames[] =\n{\n" );
1157 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1159 if (odp
->type
== TYPE_INVALID
) continue;
1160 if (nb_names
++) fprintf( outfile
, ",\n" );
1161 fprintf( outfile
, " \"%s\"", odp
->name
);
1163 fprintf( outfile
, "\n};\n\n" );
1165 /* Output the DLL argument types */
1167 fprintf( outfile
, "static const unsigned int ArgTypes[%d] =\n{\n",
1169 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1171 unsigned int j
, mask
= 0;
1172 if ((odp
->type
== TYPE_STDCALL
) || (odp
->type
== TYPE_CDECL
) ||
1173 (odp
->type
== TYPE_REGISTER
))
1174 for (j
= 0; odp
->u
.func
.arg_types
[j
]; j
++)
1176 if (odp
->u
.func
.arg_types
[j
] == 't') mask
|= 1<< (j
*2);
1177 if (odp
->u
.func
.arg_types
[j
] == 'W') mask
|= 2<< (j
*2);
1179 fprintf( outfile
, " %d", mask
);
1180 if (i
< Limit
) fprintf( outfile
, ",\n" );
1182 fprintf( outfile
, "\n};\n\n" );
1184 /* Output the DLL ordinals table */
1186 fprintf( outfile
, "static const unsigned short FuncOrdinals[] =\n{\n" );
1188 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1190 if (odp
->type
== TYPE_INVALID
) continue;
1191 if (nb_names
++) fprintf( outfile
, ",\n" );
1192 fprintf( outfile
, " %d", i
- Base
);
1194 fprintf( outfile
, "\n};\n\n" );
1196 /* Output the DLL functions arguments */
1198 fprintf( outfile
, "static const unsigned char FuncArgs[%d] =\n{\n",
1200 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1206 args
= (unsigned char)strlen(odp
->u
.func
.arg_types
);
1209 args
= 0x80 | (unsigned char)strlen(odp
->u
.func
.arg_types
);
1212 args
= 0x40 | (unsigned char)strlen(odp
->u
.func
.arg_types
);
1221 fprintf( outfile
, " 0x%02x", args
);
1222 if (i
< Limit
) fprintf( outfile
, ",\n" );
1224 fprintf( outfile
, "\n};\n\n" );
1226 /* Output the DLL imports */
1230 fprintf( outfile
, "static const char * const Imports[%d] =\n{\n", nb_imports
);
1231 for (i
= 0; i
< nb_imports
; i
++)
1233 fprintf( outfile
, " \"%s\"", DLLImports
[i
] );
1234 if (i
< nb_imports
-1) fprintf( outfile
, ",\n" );
1236 fprintf( outfile
, "\n};\n\n" );
1239 /* Output the DLL descriptor */
1241 fprintf( outfile
, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1243 fprintf( outfile
, " \"%s\",\n", DLLName
);
1244 fprintf( outfile
, " %d,\n", Base
);
1245 fprintf( outfile
, " %d,\n", Limit
- Base
+ 1 );
1246 fprintf( outfile
, " %d,\n", nb_names
);
1247 fprintf( outfile
, " %d,\n", nb_imports
);
1248 fprintf( outfile
, " %d,\n", (fwd_size
+ 3) & ~3 );
1255 fprintf( outfile
, " %s,\n", nb_imports
? "Imports" : "0" );
1256 fprintf( outfile
, " %s\n", DLLInitFunc
[0] ? DLLInitFunc
: "0" );
1257 fprintf( outfile
, "};\n" );
1262 /*******************************************************************
1265 * Build a Win16 assembly file from a spec file.
1267 static int BuildSpec16File( char * specfile
, FILE *outfile
)
1271 int code_offset
, data_offset
, module_size
;
1272 unsigned char *data
;
1274 data
= (unsigned char *)xmalloc( 0x10000 );
1275 memset( data
, 0, 16 );
1278 fprintf( outfile
, "/* File generated automatically; do not edit! */\n" );
1279 fprintf( outfile
, "\t.text\n" );
1280 fprintf( outfile
, "Code_Start:\n" );
1283 odp
= OrdinalDefinitions
;
1284 for (i
= 0; i
<= Limit
; i
++, odp
++)
1289 odp
->offset
= 0xffff;
1293 odp
->offset
= LOWORD(odp
->u
.abs
.value
);
1297 odp
->offset
= data_offset
;
1298 data_offset
+= StoreVariableCode( data
+ data_offset
, 1, odp
);
1302 odp
->offset
= data_offset
;
1303 data_offset
+= StoreVariableCode( data
+ data_offset
, 2, odp
);
1307 odp
->offset
= data_offset
;
1308 data_offset
+= StoreVariableCode( data
+ data_offset
, 4, odp
);
1312 fprintf( outfile
,"/* %s.%d */\n", DLLName
, i
);
1313 fprintf( outfile
,"\tmovw $%d,%%ax\n",LOWORD(odp
->u
.ret
.ret_value
));
1314 fprintf( outfile
,"\tmovw $%d,%%dx\n",HIWORD(odp
->u
.ret
.ret_value
));
1315 fprintf( outfile
,"\t.byte 0x66\n");
1316 if (odp
->u
.ret
.arg_size
!= 0)
1317 fprintf( outfile
, "\tlret $%d\n\n", odp
->u
.ret
.arg_size
);
1320 fprintf( outfile
, "\tlret\n");
1321 fprintf( outfile
, "\tnop\n");
1322 fprintf( outfile
, "\tnop\n\n");
1324 odp
->offset
= code_offset
;
1325 code_offset
+= 12; /* Assembly code is 12 bytes long */
1331 case TYPE_PASCAL_16
:
1333 fprintf( outfile
, "/* %s.%d */\n", DLLName
, i
);
1334 fprintf( outfile
, "\tpushw %%bp\n" );
1335 fprintf( outfile
, "\tpushl $" PREFIX
"%s\n",odp
->u
.func
.link_name
);
1336 /* FreeBSD does not understand lcall, so do it the hard way */
1337 fprintf( outfile
, "\t.byte 0x9a\n" );
1338 fprintf( outfile
, "\t.long " PREFIX
"CallFrom16_%s_%s_%s\n",
1339 (odp
->type
== TYPE_CDECL
) ? "c" : "p",
1340 (odp
->type
== TYPE_REGISTER
) ? "regs" :
1341 (odp
->type
== TYPE_PASCAL_16
) ? "word" : "long",
1342 odp
->u
.func
.arg_types
);
1343 fprintf( outfile
, "\t.long 0x%08lx\n",
1344 MAKELONG( Code_Selector
, 0x9090 /* nop ; nop */ ) );
1345 odp
->offset
= code_offset
;
1346 code_offset
+= 16; /* Assembly code is 16 bytes long */
1350 fprintf(stderr
,"build: function type %d not available for Win16\n",
1356 if (!code_offset
) /* Make sure the code segment is not empty */
1358 fprintf( outfile
, "\t.byte 0\n" );
1362 /* Output data segment */
1364 DumpBytes( outfile
, data
, data_offset
, NULL
, "Data_Start" );
1366 /* Build the module */
1368 module_size
= BuildModule16( outfile
, code_offset
, data_offset
);
1370 /* Output the DLL descriptor */
1372 fprintf( outfile
, "\t.text\n" );
1373 fprintf( outfile
, "DLLName:\t" STRING
" \"%s\\0\"\n", DLLName
);
1374 fprintf( outfile
, "\t.align 4\n" );
1375 fprintf( outfile
, "\t.globl " PREFIX
"%s_Descriptor\n", DLLName
);
1376 fprintf( outfile
, PREFIX
"%s_Descriptor:\n", DLLName
);
1377 fprintf( outfile
, "\t.long DLLName\n" ); /* Name */
1378 fprintf( outfile
, "\t.long Module_Start\n" ); /* Module start */
1379 fprintf( outfile
, "\t.long %d\n", module_size
); /* Module size */
1380 fprintf( outfile
, "\t.long Code_Start\n" ); /* Code start */
1381 fprintf( outfile
, "\t.long Data_Start\n" ); /* Data start */
1386 /*******************************************************************
1389 * Build an assembly file from a spec file.
1391 static int BuildSpecFile( FILE *outfile
, char *specname
)
1393 SpecName
= specname
;
1394 SpecFp
= fopen( specname
, "r");
1397 fprintf(stderr
, "Could not open specification file, '%s'\n", specname
);
1401 if (ParseTopLevel() < 0) return -1;
1406 return BuildSpec16File( specname
, outfile
);
1408 return BuildSpec32File( specname
, outfile
);
1410 fprintf( stderr
, "%s: Missing 'type' declaration\n", specname
);
1416 /*******************************************************************
1417 * TransferArgs16To32
1419 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1420 * The 16-bit stack layout is:
1428 * For 'cdecl' argn up to arg1 are reversed.
1430 static int TransferArgs16To32( FILE *outfile
, char *args
, int usecdecl
)
1432 int i
, pos16
, pos32
;
1435 /* Copy the arguments */
1437 pos16
= 6; /* skip bp and return address */
1438 pos32
= usecdecl
? -(strlen(args
) * 4) : 0;
1439 xargs
= usecdecl
? args
:args
+strlen(args
);
1441 for (i
= strlen(args
); i
> 0; i
--)
1449 case 'w': /* word */
1450 fprintf( outfile
, "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1451 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1455 case 's': /* s_word */
1456 fprintf( outfile
, "\tmovswl %d(%%ebp),%%eax\n", pos16
);
1457 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1461 case 'l': /* long or segmented pointer */
1462 case 'T': /* segmented pointer to null-terminated string */
1463 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", pos16
);
1464 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1468 case 'p': /* linear pointer */
1469 case 't': /* linear pointer to null-terminated string */
1470 /* Get the selector */
1471 fprintf( outfile
, "\tmovw %d(%%ebp),%%ax\n", pos16
+ 2 );
1472 /* Get the selector base */
1473 fprintf( outfile
, "\tandl $0xfff8,%%eax\n" );
1474 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy(%%eax),%%eax\n" );
1475 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1476 /* Add the offset */
1477 fprintf( outfile
, "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1478 fprintf( outfile
, "\taddl %%eax,%d(%%ebx)\n", pos32
);
1483 fprintf( stderr
, "Unknown arg type '%c'\n", *xargs
);
1491 return pos16
- 6; /* Return the size of the 16-bit args */
1495 /*******************************************************************
1498 * Build the context structure on the 32-bit stack.
1500 static void BuildContext16( FILE *outfile
)
1502 /* Store the registers */
1504 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1505 CONTEXTOFFSET(Eax
) - sizeof(CONTEXT
) );
1506 fprintf( outfile
, "\tmovl %%ecx,%d(%%ebx)\n",
1507 CONTEXTOFFSET(Ecx
) - sizeof(CONTEXT
) );
1508 fprintf( outfile
, "\tmovl %%edx,%d(%%ebx)\n",
1509 CONTEXTOFFSET(Edx
) - sizeof(CONTEXT
) );
1510 fprintf( outfile
, "\tmovl %%esi,%d(%%ebx)\n",
1511 CONTEXTOFFSET(Esi
) - sizeof(CONTEXT
) );
1512 fprintf( outfile
, "\tmovl %%edi,%d(%%ebx)\n",
1513 CONTEXTOFFSET(Edi
) - sizeof(CONTEXT
) );
1515 fprintf( outfile
, "\tmovl -24(%%ebp),%%eax\n" ); /* Get %ebx from stack*/
1516 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1517 CONTEXTOFFSET(Ebx
) - sizeof(CONTEXT
) );
1518 fprintf( outfile
, "\tmovzwl -10(%%ebp),%%eax\n" ); /* Get %ds from stack*/
1519 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1520 CONTEXTOFFSET(SegDs
) - sizeof(CONTEXT
) );
1521 fprintf( outfile
, "\tmovzwl -6(%%ebp),%%eax\n" ); /* Get %es from stack*/
1522 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1523 CONTEXTOFFSET(SegEs
) - sizeof(CONTEXT
) );
1524 fprintf( outfile
, "\tpushfl\n" );
1525 fprintf( outfile
, "\tpopl %d(%%ebx)\n",
1526 CONTEXTOFFSET(EFlags
) - sizeof(CONTEXT
) );
1527 fprintf( outfile
, "\tmovl -20(%%ebp),%%eax\n" ); /* Get %ebp from stack */
1528 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1529 CONTEXTOFFSET(Ebp
) - sizeof(CONTEXT
) );
1530 fprintf( outfile
, "\tmovzwl 2(%%ebp),%%eax\n" ); /* Get %ip from stack */
1531 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1532 CONTEXTOFFSET(Eip
) - sizeof(CONTEXT
) );
1533 fprintf( outfile
, "\tleal 2(%%ebp),%%eax\n" ); /* Get initial %sp */
1534 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1535 CONTEXTOFFSET(Esp
) - sizeof(CONTEXT
) );
1536 fprintf( outfile
, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1537 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1538 CONTEXTOFFSET(SegCs
) - sizeof(CONTEXT
) );
1539 fprintf( outfile
, "\tmovzwl -14(%%ebp),%%eax\n" ); /* Get %fs from stack */
1540 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1541 CONTEXTOFFSET(SegFs
) - sizeof(CONTEXT
) );
1542 fprintf( outfile
, "\tmovw %%gs,%%ax\n" );
1543 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1544 CONTEXTOFFSET(SegGs
) - sizeof(CONTEXT
) );
1545 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
1546 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1547 CONTEXTOFFSET(SegSs
) - sizeof(CONTEXT
) );
1549 fprintf( outfile
, "\tfsave %d(%%ebx)\n",
1550 CONTEXTOFFSET(FloatSave
) - sizeof(CONTEXT
) );
1555 /*******************************************************************
1558 * Restore the registers from the context structure.
1560 static void RestoreContext16( FILE *outfile
)
1562 /* Get the 32-bit stack pointer */
1564 fprintf( outfile
, "\tleal -%d(%%ebp),%%ebx\n",
1565 STRUCTOFFSET(STACK32FRAME
,ebp
) );
1567 /* Remove everything up to (including) the return address
1568 * from the 16-bit stack */
1570 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n",
1571 CONTEXTOFFSET(SegSs
) - sizeof(CONTEXT
) );
1572 fprintf( outfile
, "\tmovw %%ax,%%ss\n" );
1573 fprintf( outfile
, "\tmovl %d(%%ebx),%%esp\n",
1574 CONTEXTOFFSET(Esp
) - sizeof(CONTEXT
) );
1575 fprintf( outfile
, "\taddl $4,%%esp\n" ); /* Remove return address */
1577 /* Restore the registers */
1579 fprintf( outfile
, "\tmovl %d(%%ebx),%%ecx\n",
1580 CONTEXTOFFSET(Ecx
) - sizeof(CONTEXT
) );
1581 fprintf( outfile
, "\tmovl %d(%%ebx),%%edx\n",
1582 CONTEXTOFFSET(Edx
) - sizeof(CONTEXT
) );
1583 fprintf( outfile
, "\tmovl %d(%%ebx),%%esi\n",
1584 CONTEXTOFFSET(Esi
) - sizeof(CONTEXT
) );
1585 fprintf( outfile
, "\tmovl %d(%%ebx),%%edi\n",
1586 CONTEXTOFFSET(Edi
) - sizeof(CONTEXT
) );
1587 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebp\n",
1588 CONTEXTOFFSET(Ebp
) - sizeof(CONTEXT
) );
1589 fprintf( outfile
, "\tpushw %d(%%ebx)\n", /* Push new cs */
1590 CONTEXTOFFSET(SegCs
) - sizeof(CONTEXT
) );
1591 fprintf( outfile
, "\tpushw %d(%%ebx)\n", /* Push new ip */
1592 CONTEXTOFFSET(Eip
) - sizeof(CONTEXT
) );
1593 fprintf( outfile
, "\tpushl %d(%%ebx)\n", /* Push new ds */
1594 CONTEXTOFFSET(SegDs
) - sizeof(CONTEXT
) );
1595 fprintf( outfile
, "\tpushl %d(%%ebx)\n", /* Push new es */
1596 CONTEXTOFFSET(SegEs
) - sizeof(CONTEXT
) );
1597 fprintf( outfile
, "\tpushl %d(%%ebx)\n", /* Push new fs */
1598 CONTEXTOFFSET(SegFs
) - sizeof(CONTEXT
) );
1599 fprintf( outfile
, "\tpushl %d(%%ebx)\n",
1600 CONTEXTOFFSET(EFlags
) - sizeof(CONTEXT
) );
1601 fprintf( outfile
, "\tpopfl\n" );
1602 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n",
1603 CONTEXTOFFSET(Eax
) - sizeof(CONTEXT
) );
1604 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebx\n",
1605 CONTEXTOFFSET(Ebx
) - sizeof(CONTEXT
) );
1606 fprintf( outfile
, "\tpopl %%fs\n" ); /* Set fs */
1607 fprintf( outfile
, "\tpopl %%es\n" ); /* Set es */
1608 fprintf( outfile
, "\tpopl %%ds\n" ); /* Set ds */
1612 /*******************************************************************
1613 * BuildCallFrom16Func
1615 * Build a 16-bit-to-Wine callback function. The syntax of the function
1616 * profile is: call_type_xxxxx, where 'call' is the letter 'c' or 'p' for C or
1617 * Pascal calling convention, 'type' is one of 'regs', 'word' or
1618 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1619 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1620 * 'T'=segmented pointer to null-terminated string).
1621 * For register functions, the arguments are ignored, but they are still
1622 * removed from the stack upon return.
1624 * A special variant of the callback function is generated by the function
1625 * profile "t_long_". This is used by the Win95 16->32 thunk
1626 * functions C16ThkSL and C16ThkSL01 and is implemented as follows:
1627 * On entry, the EBX register is set up to contain a flat pointer to the
1628 * 16-bit stack such that EBX+22 points to the first argument.
1629 * Then, the entry point is called, while EBP is set up to point
1630 * to the return address (on the 32-bit stack).
1631 * The called function returns with CX set to the number of bytes
1632 * to be popped of the caller's stack.
1634 * Stack layout upon entry to the callback function:
1636 * (sp+18) word first 16-bit arg
1640 * (sp+8) long 32-bit entry point (used to store edx)
1641 * (sp+6) word high word of cs (always 0, used to store es)
1642 * (sp+4) word low word of cs of 16-bit entry point
1643 * (sp+2) word high word of ip (always 0, used to store ds)
1644 * (sp) word low word of ip of 16-bit entry point
1646 * Added on the stack:
1647 * (sp-2) word saved fs
1648 * (sp-4) word buffer for Win16Mutex recursion count
1650 * (sp-12) long saved previous stack
1652 static void BuildCallFrom16Func( FILE *outfile
, char *profile
)
1659 char *args
= profile
+ 7;
1661 /* Parse function type */
1663 if (!strncmp( "c_", profile
, 2 )) Cdecl
= 1;
1664 else if (!strncmp( "t_", profile
, 2 )) thunk
= 1;
1665 else if (strncmp( "p_", profile
, 2 ))
1667 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1671 if (!strncmp( "word_", profile
+ 2, 5 )) short_ret
= 1;
1672 else if (!strncmp( "regs_", profile
+ 2, 5 )) reg_func
= 1;
1673 else if (strncmp( "long_", profile
+ 2, 5 ))
1675 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1679 /* Function header */
1681 fprintf( outfile
, "\n\t.align 4\n" );
1683 fprintf( outfile
, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX
"CallFrom16_%s\n",
1686 fprintf( outfile
, "\t.globl " PREFIX
"CallFrom16_%s\n", profile
);
1687 fprintf( outfile
, PREFIX
"CallFrom16_%s:\n", profile
);
1689 /* Save 16-bit fs and leave room for Win16Mutex recursion count */
1691 fprintf( outfile
, "\t.byte 0x66\n\tpushl %%fs\n" );
1692 fprintf( outfile
, "\tpushw $0\n" );
1694 /* Setup bp to point to its copy on the stack */
1696 fprintf( outfile
, "\tpushl %%ebp\n" ); /* Save the full 32-bit ebp */
1697 fprintf( outfile
, "\tmovzwl %%sp,%%ebp\n" );
1698 fprintf( outfile
, "\taddw $20,%%bp\n" );
1700 /* Save 16-bit ds and es */
1702 /* Stupid FreeBSD assembler doesn't know these either */
1703 /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1704 fprintf( outfile
, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1705 /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1706 fprintf( outfile
, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1710 fprintf( outfile
, "\tpushl %%ebx\n" );
1712 /* Restore 32-bit segment registers */
1714 fprintf( outfile
, "\tmovw $0x%04x,%%bx\n", Data_Selector
);
1716 fprintf( outfile
, "\tdata16\n");
1718 fprintf( outfile
, "\tmovw %%bx,%%ds\n" );
1720 fprintf( outfile
, "\tdata16\n");
1722 fprintf( outfile
, "\tmovw %%bx,%%es\n" );
1724 fprintf( outfile
, "\tmovw " PREFIX
"SYSLEVEL_Win16CurrentTeb,%%fs\n" );
1726 /* Get the 32-bit stack pointer from the TEB */
1728 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET
);
1730 /* Save the 16-bit stack */
1733 fprintf( outfile
,"\tdata16\n");
1735 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%ss,(%d)\n", STACKOFFSET
+ 2 );
1736 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%sp,(%d)\n", STACKOFFSET
);
1738 /* Transfer the arguments */
1740 if (reg_func
) BuildContext16( outfile
);
1741 else if (*args
) argsize
= TransferArgs16To32( outfile
, args
, Cdecl
);
1744 /* Get the stack selector base */
1745 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
1746 fprintf( outfile
, "\tandl $0xfff8,%%eax\n" );
1747 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy(%%eax),%%eax\n" );
1748 fprintf( outfile
, "\tmovl %%eax,-24(%%ebp)\n" );
1749 /* Add the offset */
1750 fprintf( outfile
, "\tleal -16(%%ebp),%%eax\n" );
1751 fprintf( outfile
, "\taddl %%eax,-24(%%ebp)\n" );
1754 /* Get the address of the API function */
1756 fprintf( outfile
, "\tmovl -4(%%ebp),%%eax\n" );
1758 /* If necessary, save %edx over the API function address */
1760 if (!reg_func
&& short_ret
)
1761 fprintf( outfile
, "\tmovl %%edx,-4(%%ebp)\n" );
1763 /* Restore %ebx and store the 32-bit stack pointer instead */
1765 fprintf( outfile
, "\tmovl %%ebx,%%ebp\n" );
1766 fprintf( outfile
, "\tpopl %%ebx\n" );
1767 fprintf( outfile
, "\tpushl %%ebp\n" );
1769 /* Switch to the 32-bit stack */
1771 fprintf( outfile
, "\tpushl %%ds\n" );
1772 fprintf( outfile
, "\tpopl %%ss\n" );
1773 fprintf( outfile
, "\tleal -%d(%%ebp),%%esp\n",
1774 reg_func
? sizeof(CONTEXT
) : 4 * strlen(args
) );
1775 if (reg_func
) /* Push the address of the context struct */
1776 fprintf( outfile
, "\tpushl %%esp\n" );
1778 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1780 fprintf( outfile
, "\taddl $%d,%%ebp\n", STRUCTOFFSET(STACK32FRAME
,ebp
) );
1782 /* Print the debug information before the call */
1784 if (debugging
&& !thunk
)
1788 if (Cdecl
) ftype
|= 4;
1789 if (reg_func
) ftype
|= 2;
1790 if (short_ret
) ftype
|= 1;
1792 fprintf( outfile
, "\tpushl %%eax\n" );
1793 fprintf( outfile
, "\tpushl $Profile_%s\n", profile
);
1794 fprintf( outfile
, "\tpushl $%d\n", ftype
);
1795 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16\n" );
1796 fprintf( outfile
, "\tpopl %%eax\n" );
1797 fprintf( outfile
, "\tpopl %%eax\n" );
1798 fprintf( outfile
, "\tpopl %%eax\n" );
1801 /* Call the entry point */
1805 fprintf( outfile
, "\tpushl %%ebp\n" );
1806 fprintf( outfile
, "\tleal -4(%%esp), %%ebp\n" );
1807 fprintf( outfile
, "\tcall *%%eax\n" );
1808 fprintf( outfile
, "\tpopl %%ebp\n" );
1811 fprintf( outfile
, "\tcall *%%eax\n" );
1814 /* Print the debug information after the call */
1816 if (debugging
&& !thunk
)
1820 /* Push again the address of the context struct in case */
1821 /* it has been removed by an stdcall function */
1822 fprintf( outfile
, "\tleal -%d(%%ebp),%%esp\n",
1823 sizeof(CONTEXT
) + STRUCTOFFSET(STACK32FRAME
,ebp
) );
1824 fprintf( outfile
, "\tpushl %%esp\n" );
1826 fprintf( outfile
, "\tpushl %%eax\n" );
1827 fprintf( outfile
, "\tpushl $%d\n", reg_func
? 2 : (short_ret
? 1 : 0));
1828 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret\n" );
1829 fprintf( outfile
, "\tpopl %%eax\n" );
1830 fprintf( outfile
, "\tpopl %%eax\n" );
1833 /* Restore the 16-bit stack */
1836 fprintf( outfile
, "\tdata16\n");
1838 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2 );
1839 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET
);
1840 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
1844 /* Calc the arguments size */
1860 fprintf( stderr
, "Unknown arg type '%c'\n", *args
);
1865 /* Restore registers from the context structure */
1866 RestoreContext16( outfile
);
1870 /* Restore high 16 bits of ebp */
1871 fprintf( outfile
, "\tpopl %%ebp\n" );
1873 /* Restore ds and es */
1874 fprintf( outfile
, "\tincl %%esp\n" ); /* Remove mutex count */
1875 fprintf( outfile
, "\tincl %%esp\n" );
1876 fprintf( outfile
, "\tpopl %%edx\n" ); /* Remove ip and fs */
1877 fprintf( outfile
, "\tmovw %%dx,%%fs\n" ); /* and restore fs */
1878 fprintf( outfile
, "\tpopl %%edx\n" ); /* Remove cs and ds */
1879 fprintf( outfile
, "\tmovw %%dx,%%ds\n" ); /* and restore ds */
1880 fprintf( outfile
, "\t.byte 0x66\n\tpopl %%es\n" ); /* Restore es */
1882 if (short_ret
) fprintf( outfile
, "\tpopl %%edx\n" ); /* Restore edx */
1885 /* Get the return value into dx:ax */
1886 fprintf( outfile
, "\tmovl %%eax,%%edx\n" );
1887 fprintf( outfile
, "\tshrl $16,%%edx\n" );
1888 /* Remove API entry point */
1889 fprintf( outfile
, "\taddl $4,%%esp\n" );
1892 /* Restore low 16 bits of ebp */
1893 fprintf( outfile
, "\tpopw %%bp\n" );
1896 /* Remove the arguments and return */
1900 fprintf( outfile
, "\tpopl %%ebx\n" );
1901 fprintf( outfile
, "\txorb %%ch,%%ch\n" );
1902 fprintf( outfile
, "\taddw %%cx, %%sp\n" );
1903 fprintf( outfile
, "\tpushl %%ebx\n" );
1904 fprintf( outfile
, "\t.byte 0x66\n" );
1905 fprintf( outfile
, "\tlret\n" );
1907 else if (argsize
&& !Cdecl
)
1909 fprintf( outfile
, "\t.byte 0x66\n" );
1910 fprintf( outfile
, "\tlret $%d\n", argsize
);
1914 fprintf( outfile
, "\t.byte 0x66\n" );
1915 fprintf( outfile
, "\tlret\n" );
1920 /*******************************************************************
1923 * Build a Wine-to-16-bit callback function.
1925 * Stack frame of the callback function:
1929 * (ebp+8) func to call
1930 * (ebp+4) return address
1931 * (ebp) previous ebp
1933 * Prototypes for the CallTo16 functions:
1934 * extern WINAPI WORD CallTo16_word_xxx( FARPROC16 func, args... );
1935 * extern WINAPI LONG CallTo16_long_xxx( FARPROC16 func, args... );
1936 * extern WINAPI void CallTo16_sreg_( const CONTEXT *context, int nb_args );
1937 * extern WINAPI void CallTo16_lreg_( const CONTEXT *context, int nb_args );
1939 static void BuildCallTo16Func( FILE *outfile
, char *profile
)
1943 char *args
= profile
+ 5;
1945 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1946 else if (!strncmp( "sreg_", profile
, 5 )) reg_func
= 1;
1947 else if (!strncmp( "lreg_", profile
, 5 )) reg_func
= 2;
1948 else if (strncmp( "long_", profile
, 5 ))
1950 fprintf( stderr
, "Invalid function name '%s'.\n", profile
);
1954 /* Function header */
1956 fprintf( outfile
, "\n\t.align 4\n" );
1958 fprintf( outfile
, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX
"CallTo16_%s\n",
1961 fprintf( outfile
, "\t.globl " PREFIX
"CallTo16_%s\n", profile
);
1962 fprintf( outfile
, PREFIX
"CallTo16_%s:\n", profile
);
1966 fprintf( outfile
, "\tpushl %%ebp\n" );
1967 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
1969 /* Save the 32-bit registers */
1971 fprintf( outfile
, "\tpushl %%ebx\n" );
1972 fprintf( outfile
, "\tpushl %%ecx\n" );
1973 fprintf( outfile
, "\tpushl %%edx\n" );
1974 fprintf( outfile
, "\tpushl %%esi\n" );
1975 fprintf( outfile
, "\tpushl %%edi\n" );
1977 /* Enter Win16 Mutex */
1979 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_EnterWin16Lock\n" );
1981 /* Print debugging info */
1985 /* Push the address of the first argument */
1986 fprintf( outfile
, "\tleal 8(%%ebp),%%eax\n" );
1987 fprintf( outfile
, "\tpushl $%d\n", reg_func
? -1 : strlen(args
) );
1988 fprintf( outfile
, "\tpushl %%eax\n" );
1989 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16\n" );
1990 fprintf( outfile
, "\tpopl %%eax\n" );
1991 fprintf( outfile
, "\tpopl %%eax\n" );
1994 /* Call the actual CallTo16 routine (simulate a lcall) */
1996 fprintf( outfile
, "\tpushl %%cs\n" );
1997 fprintf( outfile
, "\tcall do_callto16_%s\n", profile
);
1999 fprintf( outfile
, "\tpushl %%eax\n" );
2001 /* Print debugging info */
2005 fprintf( outfile
, "\tpushl %%eax\n" );
2006 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16Ret\n" );
2007 fprintf( outfile
, "\tpopl %%eax\n" );
2010 /* Leave Win16 Mutex */
2012 fprintf( outfile
, "\tcall " PREFIX
"SYSLEVEL_LeaveWin16Lock\n" );
2014 /* Restore the 32-bit registers */
2016 fprintf( outfile
, "\tpopl %%eax\n" );
2017 fprintf( outfile
, "\tpopl %%edi\n" );
2018 fprintf( outfile
, "\tpopl %%esi\n" );
2019 fprintf( outfile
, "\tpopl %%edx\n" );
2020 fprintf( outfile
, "\tpopl %%ecx\n" );
2021 fprintf( outfile
, "\tpopl %%ebx\n" );
2026 /* FIXME: this is a hack because of task.c */
2027 if (!strcmp( profile
, "word_" ))
2029 fprintf( outfile
, ".globl " PREFIX
"CALLTO16_Restore\n" );
2030 fprintf( outfile
, PREFIX
"CALLTO16_Restore:\n" );
2033 fprintf( outfile
, "\tpopl %%ebp\n" );
2034 fprintf( outfile
, "\tret $%d\n", 4 * strlen(args
) + 4 );
2037 /* Start of the actual CallTo16 routine */
2039 fprintf( outfile
, "do_callto16_%s:\n", profile
);
2041 /* Save the 32-bit stack */
2043 fprintf( outfile
, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET
);
2044 fprintf( outfile
, "\tmovl %%ebp,%%ebx\n" );
2045 fprintf( outfile
, "\tmovl %%esp,%%edx\n" );
2049 /* Switch to the 16-bit stack, saving the current %%esp, */
2050 /* and adding the specified offset to the new sp */
2051 fprintf( outfile
, "\t.byte 0x64\n\tmovzwl (%d),%%eax\n", STACKOFFSET
);
2052 fprintf( outfile
, "\tsubl 12(%%ebx),%%eax\n" ); /* Get the offset */
2054 fprintf( outfile
,"\tdata16\n");
2056 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2);
2057 fprintf( outfile
, "\tmovl %%eax,%%esp\n" );
2058 fprintf( outfile
, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET
);
2060 /* Get the registers. ebx is handled later on. */
2062 fprintf( outfile
, "\tmovl 8(%%ebx),%%ebx\n" );
2063 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs
) );
2064 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2065 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegFs
) );
2066 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2067 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp
) );
2068 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax
) );
2069 fprintf( outfile
, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx
) );
2070 fprintf( outfile
, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx
) );
2071 fprintf( outfile
, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi
) );
2072 fprintf( outfile
, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi
) );
2074 /* Push the return address
2075 * With sreg suffix, we push 16:16 address (normal lret)
2076 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
2079 fprintf( outfile
, "\tpushl " PREFIX
"CALLTO16_RetAddr_long\n" );
2082 fprintf( outfile
, "\tpushw $0\n" );
2083 fprintf( outfile
, "\tpushw " PREFIX
"CALLTO16_RetAddr_eax+2\n" );
2084 fprintf( outfile
, "\tpushw $0\n" );
2085 fprintf( outfile
, "\tpushw " PREFIX
"CALLTO16_RetAddr_eax\n" );
2088 /* Push the called routine address */
2090 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs
) );
2091 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip
) );
2093 /* Get the 16-bit ds */
2095 fprintf( outfile
, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs
) );
2096 /* Get ebx from the 32-bit stack */
2097 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(Ebx
) );
2098 fprintf( outfile
, "\tpopl %%ds\n" );
2100 else /* not a register function */
2102 int pos
= 12; /* first argument position */
2104 /* Switch to the 16-bit stack */
2106 fprintf( outfile
,"\tdata16\n");
2108 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2);
2109 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET
);
2110 fprintf( outfile
, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET
);
2112 /* Make %bp point to the previous stackframe (built by CallFrom16) */
2113 fprintf( outfile
, "\tmovzwl %%sp,%%ebp\n" );
2114 fprintf( outfile
, "\tleal %d(%%ebp),%%ebp\n",
2115 STRUCTOFFSET(STACK16FRAME
,bp
) );
2117 /* Transfer the arguments */
2123 case 'w': /* word */
2124 fprintf( outfile
, "\tpushw %d(%%ebx)\n", pos
);
2126 case 'l': /* long */
2127 fprintf( outfile
, "\tpushl %d(%%ebx)\n", pos
);
2130 fprintf( stderr
, "Unexpected case '%c' in BuildCallTo16Func\n",
2136 /* Push the return address */
2138 fprintf( outfile
, "\tpushl " PREFIX
"CALLTO16_RetAddr_%s\n",
2139 short_ret
? "word" : "long" );
2141 /* Push the called routine address */
2143 fprintf( outfile
, "\tpushl 8(%%ebx)\n" );
2145 /* Set %fs to the value saved by the last CallFrom16 */
2147 fprintf( outfile
, "\tmovw -14(%%ebp),%%ax\n" );
2148 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2150 /* Set %ds and %es (and %ax just in case) equal to %ss */
2152 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
2153 fprintf( outfile
, "\tmovw %%ax,%%ds\n" );
2154 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2157 /* Jump to the called routine */
2159 fprintf( outfile
, "\t.byte 0x66\n" );
2160 fprintf( outfile
, "\tlret\n" );
2164 /*******************************************************************
2167 * Build the return code for 16-bit callbacks
2169 static void BuildRet16Func( FILE *outfile
)
2171 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Ret_word\n" );
2172 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Ret_long\n" );
2173 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Ret_eax\n" );
2175 fprintf( outfile
, PREFIX
"CALLTO16_Ret_word:\n" );
2176 fprintf( outfile
, "\txorl %%edx,%%edx\n" );
2178 /* Put return value into %eax */
2180 fprintf( outfile
, PREFIX
"CALLTO16_Ret_long:\n" );
2181 fprintf( outfile
, "\tshll $16,%%edx\n" );
2182 fprintf( outfile
, "\tmovw %%ax,%%dx\n" );
2183 fprintf( outfile
, "\tmovl %%edx,%%eax\n" );
2184 fprintf( outfile
, PREFIX
"CALLTO16_Ret_eax:\n" );
2186 /* Restore 32-bit segment registers */
2188 fprintf( outfile
, "\tmovw $0x%04x,%%bx\n", Data_Selector
);
2190 fprintf( outfile
, "\tdata16\n");
2192 fprintf( outfile
, "\tmovw %%bx,%%ds\n" );
2194 fprintf( outfile
, "\tdata16\n");
2196 fprintf( outfile
, "\tmovw %%bx,%%es\n" );
2198 fprintf( outfile
, "\tmovw " PREFIX
"SYSLEVEL_Win16CurrentTeb,%%fs\n" );
2200 /* Restore the 32-bit stack */
2203 fprintf( outfile
, "\tdata16\n");
2205 fprintf( outfile
, "\tmovw %%bx,%%ss\n" );
2206 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET
);
2207 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
2209 /* Return to caller */
2211 fprintf( outfile
, "\tlret\n" );
2213 /* Declare the return address variables */
2215 fprintf( outfile
, "\t.data\n" );
2216 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_RetAddr_word\n" );
2217 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_RetAddr_long\n" );
2218 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_RetAddr_eax\n" );
2219 fprintf( outfile
, PREFIX
"CALLTO16_RetAddr_word:\t.long 0\n" );
2220 fprintf( outfile
, PREFIX
"CALLTO16_RetAddr_long:\t.long 0\n" );
2221 fprintf( outfile
, PREFIX
"CALLTO16_RetAddr_eax:\t.long 0\n" );
2222 fprintf( outfile
, "\t.text\n" );
2225 /*******************************************************************
2226 * BuildCallTo32CBClient
2228 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
2230 * Since the relay stub is itself 32-bit, this should not be a problem;
2231 * unfortunately, the relay stubs are expected to switch back to a
2232 * 16-bit stack (and 16-bit code) after completion :-(
2234 * This would conflict with our 16- vs. 32-bit stack handling, so
2235 * we simply switch *back* to our 32-bit stack before returning to
2238 * The CBClient relay stub expects to be called with the following
2239 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
2240 * stack at the designated places:
2243 * (ebp+14) original arguments to the callback routine
2244 * (ebp+10) far return address to original caller
2245 * (ebp+6) Thunklet target address
2246 * (ebp+2) Thunklet relay ID code
2247 * (ebp) BP (saved by CBClientGlueSL)
2248 * (ebp-2) SI (saved by CBClientGlueSL)
2249 * (ebp-4) DI (saved by CBClientGlueSL)
2250 * (ebp-6) DS (saved by CBClientGlueSL)
2252 * ... buffer space used by the 16-bit side glue for temp copies
2254 * (ebx+4) far return address to 16-bit side glue code
2255 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
2257 * The 32-bit side glue code accesses both the original arguments (via ebp)
2258 * and the temporary copies prepared by the 16-bit side glue (via ebx).
2259 * After completion, the stub will load ss:sp from the buffer at ebx
2260 * and perform a far return to 16-bit code.
2262 * To trick the relay stub into returning to us, we replace the 16-bit
2263 * return address to the glue code by a cs:ip pair pointing to our
2264 * return entry point (the original return address is saved first).
2265 * Our return stub thus called will then reload the 32-bit ss:esp and
2266 * return to 32-bit code (by using and ss:esp value that we have also
2267 * pushed onto the 16-bit stack before and a cs:eip values found at
2268 * that position on the 32-bit stack). The ss:esp to be restored is
2269 * found relative to the 16-bit stack pointer at:
2272 * (ebx-8) sp (32-bit stack pointer)
2274 * The second variant of this routine, CALL32_CBClientEx, which is used
2275 * to implement KERNEL.621, has to cope with yet another problem: Here,
2276 * the 32-bit side directly returns to the caller of the CBClient thunklet,
2277 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
2278 * As we have to return to our 32-bit code first, we have to adapt the
2279 * layout of our temporary area so as to include values for the registers
2280 * that are to be restored, and later (in the implementation of KERNEL.621)
2281 * we *really* restore them. The return stub restores DS, DI, SI, and BP
2282 * from the stack, skips the next 8 bytes (CBClient relay code / target),
2283 * and then performs a lret NN, where NN is the number of arguments to be
2284 * removed. Thus, we prepare our temporary area as follows:
2286 * (ebx+22) 16-bit cs (this segment)
2287 * (ebx+20) 16-bit ip ('16-bit' return entry point)
2288 * (ebx+16) 32-bit ss (flat)
2289 * (ebx+12) 32-bit sp (32-bit stack pointer)
2290 * (ebx+10) 16-bit bp (points to ebx+24)
2291 * (ebx+8) 16-bit si (ignored)
2292 * (ebx+6) 16-bit di (ignored)
2293 * (ebx+4) 16-bit ds (we actually use the flat DS here)
2294 * (ebx+2) 16-bit ss (16-bit stack segment)
2295 * (ebx+0) 16-bit sp (points to ebx+4)
2297 * Note that we ensure that DS is not changed and remains the flat segment,
2298 * and the 32-bit stack pointer our own return stub needs fits just
2299 * perfectly into the 8 bytes that are skipped by the Windows stub.
2300 * One problem is that we have to determine the number of removed arguments,
2301 * as these have to be really removed in KERNEL.621. Thus, the BP value
2302 * that we place in the temporary area to be restored, contains the value
2303 * that SP would have if no arguments were removed. By comparing the actual
2304 * value of SP with this value in our return stub we can compute the number
2305 * of removed arguments. This is then returned to KERNEL.621.
2307 * The stack layout of this function:
2308 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
2309 * (ebp+16) esi pointer to caller's esi value
2310 * (ebp+12) arg ebp value to be set for relay stub
2311 * (ebp+8) func CBClient relay stub address
2315 static void BuildCallTo32CBClient( FILE *outfile
, BOOL isEx
)
2317 char *name
= isEx
? "CBClientEx" : "CBClient";
2318 int size
= isEx
? 24 : 12;
2320 /* Function header */
2322 fprintf( outfile
, "\n\t.align 4\n" );
2324 fprintf( outfile
, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX
"CALL32_%s\n",
2327 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_%s\n", name
);
2328 fprintf( outfile
, PREFIX
"CALL32_%s:\n", name
);
2332 fprintf( outfile
, "\tpushl %%ebp\n" );
2333 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2334 fprintf( outfile
, "\tpushl %%edi\n" );
2335 fprintf( outfile
, "\tpushl %%esi\n" );
2336 fprintf( outfile
, "\tpushl %%ebx\n" );
2338 /* Get the 16-bit stack */
2340 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET
);
2342 /* Convert it to a flat address */
2344 fprintf( outfile
, "\tshldl $16,%%ebx,%%eax\n" );
2345 fprintf( outfile
, "\tandl $0xfff8,%%eax\n" );
2346 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy(%%eax),%%esi\n" );
2347 fprintf( outfile
, "\tmovw %%bx,%%ax\n" );
2348 fprintf( outfile
, "\taddl %%eax,%%esi\n" );
2350 /* Allocate temporary area (simulate STACK16_PUSH) */
2352 fprintf( outfile
, "\tpushf\n" );
2353 fprintf( outfile
, "\tcld\n" );
2354 fprintf( outfile
, "\tleal -%d(%%esi), %%edi\n", size
);
2355 fprintf( outfile
, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME
) );
2356 fprintf( outfile
, "\trep\n\tmovsb\n" );
2357 fprintf( outfile
, "\tpopf\n" );
2359 fprintf( outfile
, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size
, STACKOFFSET
);
2361 fprintf( outfile
, "\tpushl %%edi\n" ); /* remember address */
2363 /* Set up temporary area */
2367 fprintf( outfile
, "\tleal 4(%%edi), %%edi\n" );
2369 fprintf( outfile
, "\tleal -8(%%esp), %%eax\n" );
2370 fprintf( outfile
, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */
2372 fprintf( outfile
, "\tmovl %%ss, %%ax\n" );
2373 fprintf( outfile
, "\tandl $0x0000ffff, %%eax\n" );
2374 fprintf( outfile
, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */
2376 fprintf( outfile
, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME
)-size
+4 + 4 );
2377 fprintf( outfile
, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */
2379 fprintf( outfile
, "\tmovl " PREFIX
"CALL32_%s_RetAddr, %%eax\n", name
);
2380 fprintf( outfile
, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */
2384 fprintf( outfile
, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME
)-size
+4 );
2385 fprintf( outfile
, "\tmovl %%ebx, 0(%%edi)\n" );
2387 fprintf( outfile
, "\tmovl %%ds, %%ax\n" );
2388 fprintf( outfile
, "\tmovw %%ax, 4(%%edi)\n" );
2390 fprintf( outfile
, "\taddl $20, %%ebx\n" );
2391 fprintf( outfile
, "\tmovw %%bx, 10(%%edi)\n" );
2393 fprintf( outfile
, "\tleal -8(%%esp), %%eax\n" );
2394 fprintf( outfile
, "\tmovl %%eax, 12(%%edi)\n" );
2396 fprintf( outfile
, "\tmovl %%ss, %%ax\n" );
2397 fprintf( outfile
, "\tandl $0x0000ffff, %%eax\n" );
2398 fprintf( outfile
, "\tmovl %%eax, 16(%%edi)\n" );
2400 fprintf( outfile
, "\tmovl " PREFIX
"CALL32_%s_RetAddr, %%eax\n", name
);
2401 fprintf( outfile
, "\tmovl %%eax, 20(%%edi)\n" );
2404 /* Set up registers and call CBClient relay stub (simulating a far call) */
2406 fprintf( outfile
, "\tmovl 16(%%ebp), %%esi\n" );
2407 fprintf( outfile
, "\tmovl (%%esi), %%esi\n" );
2409 fprintf( outfile
, "\tmovl %%edi, %%ebx\n" );
2410 fprintf( outfile
, "\tmovl 8(%%ebp), %%eax\n" );
2411 fprintf( outfile
, "\tmovl 12(%%ebp), %%ebp\n" );
2413 fprintf( outfile
, "\tpushl %%cs\n" );
2414 fprintf( outfile
, "\tcall *%%eax\n" );
2416 /* Return new esi value to caller */
2418 fprintf( outfile
, "\tmovl 32(%%esp), %%edi\n" );
2419 fprintf( outfile
, "\tmovl %%esi, (%%edi)\n" );
2421 /* Cleanup temporary area (simulate STACK16_POP) */
2423 fprintf( outfile
, "\tpop %%esi\n" );
2425 fprintf( outfile
, "\tpushf\n" );
2426 fprintf( outfile
, "\tstd\n" );
2427 fprintf( outfile
, "\tdec %%esi\n" );
2428 fprintf( outfile
, "\tleal %d(%%esi), %%edi\n", size
);
2429 fprintf( outfile
, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME
) );
2430 fprintf( outfile
, "\trep\n\tmovsb\n" );
2431 fprintf( outfile
, "\tpopf\n" );
2433 fprintf( outfile
, "\t.byte 0x64\n\taddw $%d,(%d)\n", size
, STACKOFFSET
);
2435 /* Return argument size to caller */
2438 fprintf( outfile
, "\tmovl 32(%%esp), %%ebx\n" );
2439 fprintf( outfile
, "\tmovl %%ebp, (%%ebx)\n" );
2442 /* Restore registers and return */
2444 fprintf( outfile
, "\tpopl %%ebx\n" );
2445 fprintf( outfile
, "\tpopl %%esi\n" );
2446 fprintf( outfile
, "\tpopl %%edi\n" );
2447 fprintf( outfile
, "\tpopl %%ebp\n" );
2448 fprintf( outfile
, "\tret\n" );
2450 /* '16-bit' return stub */
2452 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_%s_Ret\n", name
);
2453 fprintf( outfile
, PREFIX
"CALL32_%s_Ret:\n", name
);
2457 fprintf( outfile
, "\tmovzwl %%sp, %%ebx\n" );
2458 fprintf( outfile
, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
2462 fprintf( outfile
, "\tmovzwl %%bp, %%ebx\n" );
2463 fprintf( outfile
, "\tsubw %%bp, %%sp\n" );
2464 fprintf( outfile
, "\tmovzwl %%sp, %%ebp\n" );
2465 fprintf( outfile
, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
2467 fprintf( outfile
, "\tlret\n" );
2469 /* Declare the return address variable */
2471 fprintf( outfile
, "\t.data\n" );
2472 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_%s_RetAddr\n", name
);
2473 fprintf( outfile
, PREFIX
"CALL32_%s_RetAddr:\t.long 0\n", name
);
2474 fprintf( outfile
, "\t.text\n" );
2479 /*******************************************************************
2480 * BuildCallTo32LargeStack
2482 * Build the function used to switch to the original 32-bit stack
2483 * before calling a 32-bit function from 32-bit code. This is used for
2484 * functions that need a large stack, like X bitmaps functions.
2486 * The generated function has the following prototype:
2487 * int xxx( int (*func)(), void *arg );
2489 * The pointer to the function can be retrieved by calling CALL32_Init,
2490 * which also takes care of saving the current 32-bit stack pointer.
2491 * Furthermore, CALL32_Init switches to a new stack and jumps to the
2492 * specified target address.
2494 * NOTE: The CALL32_LargeStack routine may be recursively entered by the
2495 * same thread, but not concurrently entered by several threads.
2497 * Stack layout of CALL32_Init:
2499 * (esp+12) new stack address
2500 * (esp+8) target address
2501 * (esp+4) pointer to variable to receive CALL32_LargeStack address
2504 * Stack layout of CALL32_LargeStack:
2511 static void BuildCallTo32LargeStack( FILE *outfile
)
2513 /* Initialization function */
2515 fprintf( outfile
, "\n\t.align 4\n" );
2517 fprintf( outfile
, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX
"CALL32_Init\n");
2519 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Init\n" );
2520 fprintf( outfile
, "\t.type " PREFIX
"CALL32_Init,@function\n" );
2521 fprintf( outfile
, PREFIX
"CALL32_Init:\n" );
2522 fprintf( outfile
, "\tmovl %%esp,CALL32_Original32_esp\n" );
2523 fprintf( outfile
, "\tpopl %%eax\n" );
2524 fprintf( outfile
, "\tpopl %%eax\n" );
2525 fprintf( outfile
, "\tmovl $CALL32_LargeStack,(%%eax)\n" );
2526 fprintf( outfile
, "\tpopl %%eax\n" );
2527 fprintf( outfile
, "\tpopl %%esp\n" );
2528 fprintf( outfile
, "\tpushl %%eax\n" );
2529 fprintf( outfile
, "\tret\n" );
2531 /* Function header */
2533 fprintf( outfile
, "\n\t.align 4\n" );
2535 fprintf( outfile
, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2537 fprintf( outfile
, "CALL32_LargeStack:\n" );
2541 fprintf( outfile
, "\tpushl %%ebp\n" );
2542 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2544 /* Switch to the original 32-bit stack pointer */
2546 fprintf( outfile
, "\tcmpl $0, CALL32_RecursionCount\n" );
2547 fprintf( outfile
, "\tjne CALL32_skip\n" );
2548 fprintf( outfile
, "\tmovl CALL32_Original32_esp, %%esp\n" );
2549 fprintf( outfile
, "CALL32_skip:\n" );
2551 fprintf( outfile
, "\tincl CALL32_RecursionCount\n" );
2553 /* Transfer the argument and call the function */
2555 fprintf( outfile
, "\tpushl 12(%%ebp)\n" );
2556 fprintf( outfile
, "\tcall *8(%%ebp)\n" );
2558 /* Restore registers and return */
2560 fprintf( outfile
, "\tdecl CALL32_RecursionCount\n" );
2562 fprintf( outfile
, "\tmovl %%ebp,%%esp\n" );
2563 fprintf( outfile
, "\tpopl %%ebp\n" );
2564 fprintf( outfile
, "\tret\n" );
2568 fprintf( outfile
, "\t.data\n" );
2569 fprintf( outfile
, "CALL32_Original32_esp:\t.long 0\n" );
2570 fprintf( outfile
, "CALL32_RecursionCount:\t.long 0\n" );
2571 fprintf( outfile
, "\t.text\n" );
2575 /*******************************************************************
2576 * BuildCallFrom32Regs
2578 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2579 * 'args' is the number of dword arguments.
2583 * (ebp+12) first arg
2584 * (ebp+8) ret addr to user code
2585 * (ebp+4) ret addr to relay code
2587 * (ebp-128) buffer area to allow stack frame manipulation
2588 * (ebp-332) CONTEXT struct
2589 * (ebp-336) CONTEXT *argument
2590 * .... other arguments copied from (ebp+12)
2592 * The entry point routine is called with a CONTEXT* extra argument,
2593 * following the normal args. In this context structure, EIP_reg
2594 * contains the return address to user code, and ESP_reg the stack
2595 * pointer on return (with the return address and arguments already
2598 static void BuildCallFrom32Regs( FILE *outfile
)
2600 static const int STACK_SPACE
= 128 + sizeof(CONTEXT
);
2602 /* Function header */
2604 fprintf( outfile
, "\n\t.align 4\n" );
2606 fprintf( outfile
, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX
"CALL32_Regs\n" );
2608 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Regs\n" );
2609 fprintf( outfile
, PREFIX
"CALL32_Regs:\n" );
2611 /* Allocate some buffer space on the stack */
2613 fprintf( outfile
, "\tpushl %%ebp\n" );
2614 fprintf( outfile
, "\tmovl %%esp,%%ebp\n ");
2615 fprintf( outfile
, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE
);
2617 /* Build the context structure */
2619 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax
) - STACK_SPACE
);
2620 fprintf( outfile
, "\tpushfl\n" );
2621 fprintf( outfile
, "\tpopl %%eax\n" );
2622 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags
) - STACK_SPACE
);
2623 fprintf( outfile
, "\tmovl 0(%%ebp),%%eax\n" );
2624 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp
) - STACK_SPACE
);
2625 fprintf( outfile
, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx
) - STACK_SPACE
);
2626 fprintf( outfile
, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx
) - STACK_SPACE
);
2627 fprintf( outfile
, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx
) - STACK_SPACE
);
2628 fprintf( outfile
, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi
) - STACK_SPACE
);
2629 fprintf( outfile
, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi
) - STACK_SPACE
);
2631 fprintf( outfile
, "\txorl %%eax,%%eax\n" );
2632 fprintf( outfile
, "\tmovw %%cs,%%ax\n" );
2633 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs
) - STACK_SPACE
);
2634 fprintf( outfile
, "\tmovw %%es,%%ax\n" );
2635 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs
) - STACK_SPACE
);
2636 fprintf( outfile
, "\tmovw %%fs,%%ax\n" );
2637 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs
) - STACK_SPACE
);
2638 fprintf( outfile
, "\tmovw %%gs,%%ax\n" );
2639 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs
) - STACK_SPACE
);
2640 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
2641 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs
) - STACK_SPACE
);
2642 fprintf( outfile
, "\tmovw %%ds,%%ax\n" );
2643 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs
) - STACK_SPACE
);
2644 fprintf( outfile
, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
2646 fprintf( outfile
, "\tmovl $0x%x,%%eax\n", CONTEXT_FULL
);
2647 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags
) - STACK_SPACE
);
2649 fprintf( outfile
, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
2650 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip
) - STACK_SPACE
);
2652 /* Transfer the arguments */
2654 fprintf( outfile
, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
2655 fprintf( outfile
, "\tpushl %%esp\n" ); /* push ptr to context struct */
2656 fprintf( outfile
, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
2657 fprintf( outfile
, "\tjecxz 1f\n" );
2658 fprintf( outfile
, "\tsubl %%ecx,%%esp\n" );
2659 fprintf( outfile
, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */
2660 fprintf( outfile
, "\tmovl %%esp,%%edi\n" );
2661 fprintf( outfile
, "\tshrl $2,%%ecx\n" );
2662 fprintf( outfile
, "\tcld\n" );
2663 fprintf( outfile
, "\trep\n\tmovsl\n" ); /* copy args */
2665 fprintf( outfile
, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
2666 fprintf( outfile
, "\tleal 12(%%ebp,%%eax),%%eax\n" );
2667 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2669 /* Call the entry point */
2671 fprintf( outfile
, "\tcall *0(%%ebx)\n" );
2673 /* Store %eip and %ebp onto the new stack */
2675 fprintf( outfile
, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2676 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip
) - STACK_SPACE
);
2677 fprintf( outfile
, "\tmovl %%eax,-4(%%edx)\n" );
2678 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp
) - STACK_SPACE
);
2679 fprintf( outfile
, "\tmovl %%eax,-8(%%edx)\n" );
2681 /* Restore the context structure */
2683 /* Note: we don't bother to restore %cs, %ds and %ss
2684 * changing them in 32-bit code is a recipe for disaster anyway
2686 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs
) - STACK_SPACE
);
2687 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2688 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs
) - STACK_SPACE
);
2689 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2690 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs
) - STACK_SPACE
);
2691 fprintf( outfile
, "\tmovw %%ax,%%gs\n" );
2693 fprintf( outfile
, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi
) - STACK_SPACE
);
2694 fprintf( outfile
, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi
) - STACK_SPACE
);
2695 fprintf( outfile
, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx
) - STACK_SPACE
);
2696 fprintf( outfile
, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx
) - STACK_SPACE
);
2697 fprintf( outfile
, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx
) - STACK_SPACE
);
2699 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags
) - STACK_SPACE
);
2700 fprintf( outfile
, "\tpushl %%eax\n" );
2701 fprintf( outfile
, "\tpopfl\n" );
2702 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax
) - STACK_SPACE
);
2704 fprintf( outfile
, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
2705 fprintf( outfile
, "\tleal -8(%%ebp),%%esp\n" );
2706 fprintf( outfile
, "\tpopl %%ebp\n" );
2707 fprintf( outfile
, "\tret\n" );
2711 /*******************************************************************
2714 * Build the spec files
2716 static int BuildSpec( FILE *outfile
, int argc
, char *argv
[] )
2719 for (i
= 2; i
< argc
; i
++)
2720 if (BuildSpecFile( outfile
, argv
[i
] ) < 0) return -1;
2725 /*******************************************************************
2728 * Build the 16-bit-to-Wine callbacks
2730 static int BuildCallFrom16( FILE *outfile
, char * outname
, int argc
, char *argv
[] )
2737 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2738 fprintf( outfile
, "\t.text\n" );
2741 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2742 getcwd(buffer
, sizeof(buffer
));
2745 * The stabs help the internal debugger as they are an indication that it
2746 * is sensible to step into a thunk/trampoline.
2748 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2749 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2750 fprintf( outfile
, "\t.text\n" );
2751 fprintf( outfile
, "\t.align 4\n" );
2752 fprintf( outfile
, "Code_Start:\n\n" );
2754 fprintf( outfile
, PREFIX
"CallFrom16_Start:\n" );
2755 fprintf( outfile
, "\t.globl "PREFIX
"CallFrom16_Start\n" );
2757 /* Build the callback functions */
2759 for (i
= 2; i
< argc
; i
++) BuildCallFrom16Func( outfile
, argv
[i
] );
2761 /* Build the thunk callback function */
2763 BuildCallFrom16Func( outfile
, "t_long_" );
2765 /* Output the argument debugging strings */
2769 fprintf( outfile
, "/* Argument strings */\n" );
2770 for (i
= 2; i
< argc
; i
++)
2772 fprintf( outfile
, "Profile_%s:\t", argv
[i
] );
2773 fprintf( outfile
, STRING
" \"%s\\0\"\n", argv
[i
] + 7 );
2776 fprintf( outfile
, PREFIX
"CallFrom16_End:\n" );
2777 fprintf( outfile
, "\t.globl "PREFIX
"CallFrom16_End\n" );
2780 fprintf( outfile
, "\t.text\n");
2781 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2782 fprintf( outfile
, ".Letext:\n");
2789 /*******************************************************************
2792 * Build the Wine-to-16-bit callbacks
2794 static int BuildCallTo16( FILE *outfile
, char * outname
, int argc
, char *argv
[] )
2801 infile
= fopen( argv
[2], "r" );
2808 else infile
= stdin
;
2812 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2813 fprintf( outfile
, "\t.text\n" );
2816 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2817 getcwd(buffer
, sizeof(buffer
));
2820 * The stabs help the internal debugger as they are an indication that it
2821 * is sensible to step into a thunk/trampoline.
2823 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2824 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2825 fprintf( outfile
, "\t.text\n" );
2826 fprintf( outfile
, "\t.align 4\n" );
2827 fprintf( outfile
, "Code_Start:\n\n" );
2830 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Start\n" );
2831 fprintf( outfile
, PREFIX
"CALLTO16_Start:\n" );
2833 /* Build the callback functions */
2835 while (fgets( buffer
, sizeof(buffer
), infile
))
2837 if (strstr( buffer
, "### start build ###" )) break;
2839 while (fgets( buffer
, sizeof(buffer
), infile
))
2841 char *p
= strstr( buffer
, "CallTo16_" );
2844 char *profile
= p
+ strlen( "CallTo16_" );
2846 while ((*p
== '_') || isalpha(*p
)) p
++;
2848 BuildCallTo16Func( outfile
, profile
);
2850 if (strstr( buffer
, "### stop build ###" )) break;
2853 /* Output the 16-bit return code */
2855 BuildRet16Func( outfile
);
2857 /* Output the CBClient callback functions
2858 * (while this does not really 'call to 16-bit' code, it is placed
2859 * here so that its 16-bit return stub is defined within the CALLTO16
2862 BuildCallTo32CBClient( outfile
, FALSE
);
2863 BuildCallTo32CBClient( outfile
, TRUE
);
2866 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_End\n" );
2867 fprintf( outfile
, PREFIX
"CALLTO16_End:\n" );
2870 fprintf( outfile
, "\t.text\n");
2871 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2872 fprintf( outfile
, ".Letext:\n");
2880 /*******************************************************************
2883 * Build the 32-bit callbacks
2885 static int BuildCall32( FILE *outfile
, char * outname
)
2891 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2892 fprintf( outfile
, "\t.text\n" );
2897 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2898 getcwd(buffer
, sizeof(buffer
));
2901 * The stabs help the internal debugger as they are an indication that it
2902 * is sensible to step into a thunk/trampoline.
2904 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2905 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2906 fprintf( outfile
, "\t.text\n" );
2907 fprintf( outfile
, "\t.align 4\n" );
2908 fprintf( outfile
, "Code_Start:\n" );
2911 /* Build the 32-bit large stack callback */
2913 BuildCallTo32LargeStack( outfile
);
2915 /* Build the register callback function */
2917 BuildCallFrom32Regs( outfile
);
2920 fprintf( outfile
, "\t.text\n");
2921 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2922 fprintf( outfile
, ".Letext:\n");
2925 #else /* __i386__ */
2927 /* Just to avoid an empty file */
2928 fprintf( outfile
, "\t.long 0\n" );
2930 #endif /* __i386__ */
2935 /*******************************************************************
2938 static void usage(void)
2941 "usage: build [-o outfile] -spec SPECNAMES\n"
2942 " build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2943 " build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2944 " build [-o outfile] -call32\n" );
2949 /*******************************************************************
2952 int main(int argc
, char **argv
)
2954 char *outname
= NULL
;
2955 FILE *outfile
= stdout
;
2958 if (argc
< 2) usage();
2960 if (!strcmp( argv
[1], "-o" ))
2965 if (argc
< 2) usage();
2966 if (!(outfile
= fopen( outname
, "w" )))
2968 fprintf( stderr
, "Unable to create output file '%s'\n", outname
);
2973 /* Retrieve the selector values; this assumes that we are building
2974 * the asm files on the platform that will also run them. Probably
2975 * a safe assumption to make.
2977 GET_CS( Code_Selector
);
2978 GET_DS( Data_Selector
);
2980 if (!strcmp( argv
[1], "-spec" ))
2981 res
= BuildSpec( outfile
, argc
, argv
);
2982 else if (!strcmp( argv
[1], "-callfrom16" ))
2983 res
= BuildCallFrom16( outfile
, outname
, argc
, argv
);
2984 else if (!strcmp( argv
[1], "-callto16" ))
2985 res
= BuildCallTo16( outfile
, outname
, argc
, argv
);
2986 else if (!strcmp( argv
[1], "-call32" ))
2987 res
= BuildCall32( outfile
, outname
);