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"
22 #ifdef NEED_UNDERSCORE_PREFIX
28 #if defined(__GNUC__) && !defined(__svr4__)
37 TYPE_BYTE
, /* byte variable (Win16) */
38 TYPE_WORD
, /* word variable (Win16) */
39 TYPE_LONG
, /* long variable (Win16) */
40 TYPE_PASCAL_16
, /* pascal function with 16-bit return (Win16) */
41 TYPE_PASCAL
, /* pascal function with 32-bit return (Win16) */
42 TYPE_ABS
, /* absolute value (Win16) */
43 TYPE_RETURN
, /* simple return value function (Win16) */
44 TYPE_REGISTER
, /* register function */
45 TYPE_STUB
, /* unimplemented stub */
46 TYPE_STDCALL
, /* stdcall function (Win32) */
47 TYPE_CDECL
, /* cdecl function (Win32) */
48 TYPE_VARARGS
, /* varargs function (Win32) */
49 TYPE_EXTERN
, /* external symbol (Win32) */
53 static const char * const TypeNames
[TYPE_NBTYPES
] =
56 "byte", /* TYPE_BYTE */
57 "word", /* TYPE_WORD */
58 "long", /* TYPE_LONG */
59 "pascal16", /* TYPE_PASCAL_16 */
60 "pascal", /* TYPE_PASCAL */
61 "equate", /* TYPE_ABS */
62 "return", /* TYPE_RETURN */
63 "register", /* TYPE_REGISTER */
64 "stub", /* TYPE_STUB */
65 "stdcall", /* TYPE_STDCALL */
66 "cdecl", /* TYPE_CDECL */
67 "varargs", /* TYPE_VARARGS */
68 "extern" /* TYPE_EXTERN */
71 #define MAX_ORDINALS 1299
73 /* Callback function used for stub functions */
74 #define STUB_CALLBACK \
75 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
135 static ORDDEF OrdinalDefinitions
[MAX_ORDINALS
];
137 static SPEC_TYPE SpecType
= SPEC_INVALID
;
138 static char DLLName
[80];
139 static char DLLFileName
[80];
140 static int Limit
= 0;
141 static int Base
= MAX_ORDINALS
;
142 static int DLLHeapSize
= 0;
143 static char *SpecName
;
145 static WORD Code_Selector
, Data_Selector
;
147 char *ParseBuffer
= NULL
;
152 static int debugging
= 1;
154 /* Offset of a structure field relative to the start of the struct */
155 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
157 /* Offset of register relative to the start of the CONTEXT struct */
158 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT,reg)
160 static void *xmalloc (size_t size
)
164 res
= malloc (size
? size
: 1);
167 fprintf (stderr
, "Virtual memory exhausted.\n");
174 static void *xrealloc (void *ptr
, size_t size
)
176 void *res
= realloc (ptr
, size
);
179 fprintf (stderr
, "Virtual memory exhausted.\n");
186 static int IsNumberString(char *s
)
195 static char *strupper(char *s
)
199 for(p
= s
; *p
!= '\0'; p
++)
205 static char * GetTokenInLine(void)
210 if (ParseNext
!= ParseBuffer
)
212 if (ParseSaveChar
== '\0')
214 *ParseNext
= ParseSaveChar
;
218 * Remove initial white space.
220 for (p
= ParseNext
; isspace(*p
); p
++)
223 if ((*p
== '\0') || (*p
== '#'))
230 if (*token
!= '(' && *token
!= ')')
231 while (*p
!= '\0' && *p
!= '(' && *p
!= ')' && !isspace(*p
))
241 static char * GetToken(void)
245 if (ParseBuffer
== NULL
)
247 ParseBuffer
= xmalloc(512);
248 ParseNext
= ParseBuffer
;
252 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
254 if (ParseBuffer
[0] != '#')
259 while ((token
= GetTokenInLine()) == NULL
)
261 ParseNext
= ParseBuffer
;
265 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
267 if (ParseBuffer
[0] != '#')
276 /*******************************************************************
279 * Parse a variable definition.
281 static int ParseVariable( ORDDEF
*odp
)
286 int value_array_size
;
288 char *token
= GetToken();
291 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
292 SpecName
, Line
, token
);
297 value_array_size
= 25;
298 value_array
= xmalloc(sizeof(*value_array
) * value_array_size
);
300 while ((token
= GetToken()) != NULL
)
305 value_array
[n_values
++] = strtol(token
, &endptr
, 0);
306 if (n_values
== value_array_size
)
308 value_array_size
+= 25;
309 value_array
= xrealloc(value_array
,
310 sizeof(*value_array
) * value_array_size
);
313 if (endptr
== NULL
|| *endptr
!= '\0')
315 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
316 SpecName
, Line
, token
);
323 fprintf(stderr
, "%s:%d: End of file in variable declaration\n",
328 odp
->u
.var
.n_values
= n_values
;
329 odp
->u
.var
.values
= xrealloc(value_array
, sizeof(*value_array
) * n_values
);
335 /*******************************************************************
336 * ParseExportFunction
338 * Parse a function definition.
340 static int ParseExportFunction( ORDDEF
*odp
)
348 if (odp
->type
== TYPE_STDCALL
)
350 fprintf( stderr
, "%s:%d: 'stdcall' not supported for Win16\n",
354 if (odp
->type
== TYPE_CDECL
)
356 fprintf( stderr
, "%s:%d: 'cdecl' not supported for Win16\n",
362 if ((odp
->type
== TYPE_PASCAL
) || (odp
->type
== TYPE_PASCAL_16
))
364 fprintf( stderr
, "%s:%d: 'pascal' not supported for Win32\n",
376 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
377 SpecName
, Line
, token
);
381 for (i
= 0; i
< sizeof(odp
->u
.func
.arg_types
)-1; i
++)
387 if (!strcmp(token
, "word"))
388 odp
->u
.func
.arg_types
[i
] = 'w';
389 else if (!strcmp(token
, "s_word"))
390 odp
->u
.func
.arg_types
[i
] = 's';
391 else if (!strcmp(token
, "long") || !strcmp(token
, "segptr"))
392 odp
->u
.func
.arg_types
[i
] = 'l';
393 else if (!strcmp(token
, "ptr"))
394 odp
->u
.func
.arg_types
[i
] = 'p';
395 else if (!strcmp(token
, "str"))
396 odp
->u
.func
.arg_types
[i
] = 't';
397 else if (!strcmp(token
, "wstr"))
398 odp
->u
.func
.arg_types
[i
] = 'W';
399 else if (!strcmp(token
, "segstr"))
400 odp
->u
.func
.arg_types
[i
] = 'T';
401 else if (!strcmp(token
, "double"))
403 odp
->u
.func
.arg_types
[i
++] = 'l';
404 odp
->u
.func
.arg_types
[i
] = 'l';
408 fprintf(stderr
, "%s:%d: Unknown variable type '%s'\n",
409 SpecName
, Line
, token
);
412 if (SpecType
== SPEC_WIN32
)
414 if (strcmp(token
, "long") &&
415 strcmp(token
, "ptr") &&
416 strcmp(token
, "str") &&
417 strcmp(token
, "wstr") &&
418 strcmp(token
, "double"))
420 fprintf( stderr
, "%s:%d: Type '%s' not supported for Win32\n",
421 SpecName
, Line
, token
);
426 if ((*token
!= ')') || (i
>= sizeof(odp
->u
.func
.arg_types
)))
428 fprintf( stderr
, "%s:%d: Too many arguments\n", SpecName
, Line
);
431 odp
->u
.func
.arg_types
[i
] = '\0';
432 if ((odp
->type
== TYPE_STDCALL
) && !i
)
433 odp
->type
= TYPE_CDECL
; /* stdcall is the same as cdecl for 0 args */
434 if ((odp
->type
== TYPE_REGISTER
) && (SpecType
== SPEC_WIN32
) && i
)
436 fprintf( stderr
, "%s:%d: register functions cannot have arguments in Win32\n",
440 strcpy(odp
->u
.func
.link_name
, GetToken());
445 /*******************************************************************
448 * Parse an 'equate' definition.
450 static int ParseEquate( ORDDEF
*odp
)
454 char *token
= GetToken();
455 int value
= strtol(token
, &endptr
, 0);
456 if (endptr
== NULL
|| *endptr
!= '\0')
458 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
459 SpecName
, Line
, token
);
463 if (SpecType
== SPEC_WIN32
)
465 fprintf( stderr
, "%s:%d: 'equate' not supported for Win32\n",
470 odp
->u
.abs
.value
= value
;
475 /*******************************************************************
478 * Parse a 'return' definition.
480 static int ParseReturn( ORDDEF
*odp
)
486 odp
->u
.ret
.arg_size
= strtol(token
, &endptr
, 0);
487 if (endptr
== NULL
|| *endptr
!= '\0')
489 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
490 SpecName
, Line
, token
);
495 odp
->u
.ret
.ret_value
= strtol(token
, &endptr
, 0);
496 if (endptr
== NULL
|| *endptr
!= '\0')
498 fprintf(stderr
, "%s:%d: Expected number value, got '%s'\n",
499 SpecName
, Line
, token
);
503 if (SpecType
== SPEC_WIN32
)
505 fprintf( stderr
, "%s:%d: 'return' not supported for Win32\n",
514 /*******************************************************************
517 * Parse a 'stub' definition.
519 static int ParseStub( ORDDEF
*odp
)
521 odp
->u
.func
.arg_types
[0] = '\0';
522 strcpy( odp
->u
.func
.link_name
, STUB_CALLBACK
);
527 /*******************************************************************
530 * Parse an 'varargs' definition.
532 static int ParseVarargs( ORDDEF
*odp
)
536 if (SpecType
== SPEC_WIN16
)
538 fprintf( stderr
, "%s:%d: 'varargs' not supported for Win16\n",
546 fprintf(stderr
, "%s:%d: Expected '(' got '%s'\n",
547 SpecName
, Line
, token
);
553 fprintf(stderr
, "%s:%d: Expected ')' got '%s'\n",
554 SpecName
, Line
, token
);
558 strcpy( odp
->u
.vargs
.link_name
, GetToken() );
563 /*******************************************************************
566 * Parse an 'extern' definition.
568 static int ParseExtern( ORDDEF
*odp
)
570 if (SpecType
== SPEC_WIN16
)
572 fprintf( stderr
, "%s:%d: 'extern' not supported for Win16\n",
576 strcpy( odp
->u
.ext
.link_name
, GetToken() );
581 /*******************************************************************
584 * Parse an ordinal definition.
586 static int ParseOrdinal(int ordinal
)
591 if (ordinal
>= MAX_ORDINALS
)
593 fprintf(stderr
, "%s:%d: Ordinal number too large\n", SpecName
, Line
);
596 if (ordinal
> Limit
) Limit
= ordinal
;
597 if (ordinal
< Base
) Base
= ordinal
;
599 odp
= &OrdinalDefinitions
[ordinal
];
600 if (!(token
= GetToken()))
602 fprintf(stderr
, "%s:%d: Expected type after ordinal\n", SpecName
, Line
);
606 for (odp
->type
= 0; odp
->type
< TYPE_NBTYPES
; odp
->type
++)
607 if (TypeNames
[odp
->type
] && !strcmp( token
, TypeNames
[odp
->type
] ))
610 if (odp
->type
>= TYPE_NBTYPES
)
613 "%s:%d: Expected type after ordinal, found '%s' instead\n",
614 SpecName
, Line
, token
);
618 if (!(token
= GetToken()))
620 fprintf( stderr
, "%s:%d: Expected name after type\n", SpecName
, Line
);
623 strcpy( odp
->name
, token
);
631 return ParseVariable( odp
);
637 return ParseExportFunction( odp
);
639 return ParseEquate( odp
);
641 return ParseReturn( odp
);
643 return ParseStub( odp
);
645 return ParseVarargs( odp
);
647 return ParseExtern( odp
);
649 fprintf( stderr
, "Should not happen\n" );
655 /*******************************************************************
660 static int ParseTopLevel(void)
664 while ((token
= GetToken()) != NULL
)
666 if (strcmp(token
, "name") == 0)
668 strcpy(DLLName
, GetToken());
670 if (!DLLFileName
[0]) sprintf( DLLFileName
, "%s.DLL", DLLName
);
672 else if (strcmp(token
, "file") == 0)
674 strcpy(DLLFileName
, GetToken());
675 strupper(DLLFileName
);
677 else if (strcmp(token
, "type") == 0)
680 if (!strcmp(token
, "win16" )) SpecType
= SPEC_WIN16
;
681 else if (!strcmp(token
, "win32" )) SpecType
= SPEC_WIN32
;
684 fprintf(stderr
, "%s:%d: Type must be 'win16' or 'win32'\n",
689 else if (strcmp(token
, "heap") == 0)
692 if (!IsNumberString(token
))
694 fprintf(stderr
, "%s:%d: Expected number after heap\n",
698 DLLHeapSize
= atoi(token
);
700 else if (IsNumberString(token
))
705 ordinal
= atoi(token
);
706 if ((rv
= ParseOrdinal(ordinal
)) < 0)
712 "%s:%d: Expected name, id, length or ordinal\n",
722 /*******************************************************************
725 * Store a list of ints into a byte array.
727 static int StoreVariableCode( unsigned char *buffer
, int size
, ORDDEF
*odp
)
734 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
735 buffer
[i
] = odp
->u
.var
.values
[i
];
738 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
739 ((unsigned short *)buffer
)[i
] = odp
->u
.var
.values
[i
];
742 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
743 ((unsigned int *)buffer
)[i
] = odp
->u
.var
.values
[i
];
746 return odp
->u
.var
.n_values
* size
;
750 /*******************************************************************
753 * Dump a byte stream into the assembly code.
755 static void DumpBytes( FILE *outfile
, const unsigned char *data
, int len
,
756 const char *section
, const char *label_start
)
759 if (section
) fprintf( outfile
, "\t%s\n", section
);
760 if (label_start
) fprintf( outfile
, "%s:\n", label_start
);
761 for (i
= 0; i
< len
; i
++)
763 if (!(i
& 0x0f)) fprintf( outfile
, "\t.byte " );
764 fprintf( outfile
, "%d", *data
++ );
766 fprintf( outfile
, "%c", ((i
& 0x0f) != 0x0f) ? ',' : '\n' );
768 fprintf( outfile
, "\n" );
772 /*******************************************************************
775 * Build the in-memory representation of a 16-bit NE module, and dump it
776 * as a byte stream into the assembly code.
778 static int BuildModule16( FILE *outfile
, int max_code_offset
,
779 int max_data_offset
)
785 SEGTABLEENTRY
*pSegment
;
792 * OFSTRUCT File information
793 * SEGTABLEENTRY Segment 1 (code)
794 * SEGTABLEENTRY Segment 2 (data)
795 * WORD[2] Resource table (empty)
796 * BYTE[2] Imported names (empty)
797 * BYTE[n] Resident names table
798 * BYTE[n] Entry table
801 buffer
= xmalloc( 0x10000 );
803 pModule
= (NE_MODULE
*)buffer
;
804 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
807 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_BUILTIN
| NE_FFLAGS_LIBMODULE
;
809 pModule
->heap_size
= DLLHeapSize
;
810 pModule
->stack_size
= 0;
815 pModule
->seg_count
= 2;
816 pModule
->modref_count
= 0;
817 pModule
->nrname_size
= 0;
818 pModule
->modref_table
= 0;
819 pModule
->nrname_fpos
= 0;
820 pModule
->moveable_entries
= 0;
821 pModule
->alignment
= 0;
822 pModule
->truetype
= 0;
823 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
824 pModule
->misc_flags
= 0;
825 pModule
->dlls_to_init
= 0;
826 pModule
->nrname_handle
= 0;
827 pModule
->min_swap_area
= 0;
828 pModule
->expected_version
= 0x030a;
829 pModule
->pe_module
= NULL
;
831 pModule
->self_loading_sel
= 0;
833 /* File information */
835 pFileInfo
= (OFSTRUCT
*)(pModule
+ 1);
836 pModule
->fileinfo
= (int)pFileInfo
- (int)pModule
;
837 memset( pFileInfo
, 0, sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
) );
838 pFileInfo
->cBytes
= sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
)
839 + strlen(DLLFileName
);
840 strcpy( pFileInfo
->szPathName
, DLLFileName
);
841 pstr
= (char *)pFileInfo
+ pFileInfo
->cBytes
+ 1;
845 pSegment
= (SEGTABLEENTRY
*)pstr
;
846 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
847 pSegment
->filepos
= 0;
848 pSegment
->size
= max_code_offset
;
850 pSegment
->minsize
= max_code_offset
;
851 pSegment
->selector
= 0;
854 pModule
->dgroup_entry
= (int)pSegment
- (int)pModule
;
855 pSegment
->filepos
= 0;
856 pSegment
->size
= max_data_offset
;
857 pSegment
->flags
= NE_SEGFLAGS_DATA
;
858 pSegment
->minsize
= max_data_offset
;
859 pSegment
->selector
= 0;
864 pword
= (WORD
*)pSegment
;
865 pModule
->res_table
= (int)pword
- (int)pModule
;
869 /* Imported names table */
871 pstr
= (char *)pword
;
872 pModule
->import_table
= (int)pstr
- (int)pModule
;
876 /* Resident names table */
878 pModule
->name_table
= (int)pstr
- (int)pModule
;
879 /* First entry is module name */
880 *pstr
= strlen(DLLName
);
881 strcpy( pstr
+ 1, DLLName
);
884 pstr
+= sizeof(WORD
);
885 /* Store all ordinals */
886 odp
= OrdinalDefinitions
+ 1;
887 for (i
= 1; i
<= Limit
; i
++, odp
++)
889 if (!odp
->name
[0]) continue;
890 *pstr
= strlen( odp
->name
);
891 strcpy( pstr
+ 1, odp
->name
);
892 strupper( pstr
+ 1 );
895 pstr
+= sizeof(WORD
);
901 pModule
->entry_table
= (int)pstr
- (int)pModule
;
903 odp
= OrdinalDefinitions
+ 1;
904 for (i
= 1; i
<= Limit
; i
++, odp
++)
915 selector
= 1; /* Code selector */
921 selector
= 2; /* Data selector */
925 selector
= 0xfe; /* Constant selector */
929 selector
= 0; /* Invalid selector */
933 /* create a new bundle if necessary */
934 if (!bundle
|| (bundle
[0] >= 254) || (bundle
[1] != selector
))
938 bundle
[1] = selector
;
946 *(WORD
*)pstr
= odp
->offset
;
947 pstr
+= sizeof(WORD
);
952 /* Dump the module content */
954 DumpBytes( outfile
, (char *)pModule
, (int)pstr
- (int)pModule
,
955 ".data", "Module_Start" );
956 return (int)pstr
- (int)pModule
;
960 /*******************************************************************
963 * Build a Win32 assembly file from a spec file.
965 static int BuildSpec32File( char * specfile
, FILE *outfile
)
968 int i
, nb_names
, nb_stubs
;
970 unsigned char *args
, *p
;
972 fprintf( outfile
, "/* File generated automatically; do not edit! */\n" );
973 fprintf( outfile
, "\t.file\t\"%s\"\n", specfile
);
975 getcwd(buffer
, sizeof(buffer
));
978 * The stabs help the internal debugger as they are an indication that it
979 * is sensible to step into a thunk/trampoline.
981 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
982 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", specfile
);
985 fprintf( outfile
, "\t.text\n" );
986 fprintf( outfile
, "\t.align 4\n" );
987 fprintf( outfile
, "Code_Start:\n" );
989 /* Output code for all register functions */
991 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
993 if (odp
->type
!= TYPE_REGISTER
) continue;
994 fprintf( outfile
, "\n/* %s.%d (%s) */\n", DLLName
, i
, odp
->name
);
995 fprintf( outfile
, "\t.align 4\n" );
997 fprintf( outfile
, ".stabs \"%s_%d:F1\",36,0,%d,%s_%d\n",
998 DLLName
, i
, odp
->lineno
, DLLName
, i
);
1000 fprintf( outfile
, "%s_%d:\n", DLLName
, i
);
1002 fprintf( outfile
, ".stabn 68,0,%d,0\n", odp
->lineno
);
1004 fprintf( outfile
, "\tpushl $" PREFIX
"%s\n",odp
->u
.func
.link_name
);
1005 fprintf( outfile
, "\tjmp " PREFIX
"CALL32_Regs\n" );
1008 /* Output code for all stubs functions */
1011 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1013 if (odp
->type
!= TYPE_STUB
) continue;
1014 fprintf( outfile
, "\n/* %s.%d (%s) */\n", DLLName
, i
, odp
->name
);
1016 fprintf( outfile
, ".stabs \"%s_%d:F1\",36,0,%d,%s_%d\n",
1017 DLLName
, i
, odp
->lineno
, DLLName
, i
);
1019 fprintf( outfile
, "%s_%d:\n", DLLName
, i
);
1021 fprintf( outfile
, ".stabn 68,0,%d,0\n", odp
->lineno
);
1023 fprintf( outfile
, "\tpushl $Name_%d\n", i
);
1024 fprintf( outfile
, "\tpushl $%d\n", i
);
1025 if (++nb_stubs
== 1)
1027 fprintf( outfile
, "DoStub:\n" );
1028 fprintf( outfile
, "\tpushl $DLLName\n" );
1029 fprintf( outfile
, "\tcall " PREFIX
"RELAY_Unimplemented32\n" );
1031 else fprintf( outfile
, "\tjmp DoStub\n" );
1034 /* Output the DLL functions table */
1036 fprintf( outfile
, "\t.text\n" );
1037 fprintf( outfile
, "\t.align 4\n" );
1038 fprintf( outfile
, "Functions:\n" );
1039 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1044 fprintf( outfile
, "\t.long 0\n" );
1047 fprintf( outfile
, "\t.long " PREFIX
"%s\n",odp
->u
.vargs
.link_name
);
1050 fprintf( outfile
, "\t.long " PREFIX
"%s\n", odp
->u
.ext
.link_name
);
1054 fprintf( outfile
, "\t.long " PREFIX
"%s\n", odp
->u
.func
.link_name
);
1058 fprintf( outfile
, "\t.long %s_%d\n", DLLName
, i
);
1061 fprintf(stderr
,"build: function type %d not available for Win32\n",
1067 /* Output the DLL names table */
1069 fprintf( outfile
, "FuncNames:\n" );
1070 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1072 if (odp
->type
!= TYPE_INVALID
)
1073 fprintf( outfile
, "\t.long Name_%d\n", i
);
1076 /* Output the DLL argument types */
1078 fprintf( outfile
, "ArgTypes:\n" );
1079 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1081 unsigned int j
, mask
= 0;
1082 if ((odp
->type
== TYPE_STDCALL
) || (odp
->type
== TYPE_CDECL
))
1083 for (j
= 0; odp
->u
.func
.arg_types
[j
]; j
++)
1085 if (odp
->u
.func
.arg_types
[j
] == 't') mask
|= 1<< (j
*2);
1086 if (odp
->u
.func
.arg_types
[j
] == 'W') mask
|= 2<< (j
*2);
1088 fprintf( outfile
, "\t.long %d\n",mask
);
1091 /* Output the DLL ordinals table */
1093 fprintf( outfile
, "FuncOrdinals:\n" );
1095 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1097 if (odp
->type
== TYPE_INVALID
) continue;
1099 /* Some assemblers do not have .word */
1100 fprintf( outfile
, "\t.byte %d,%d\n", LOBYTE(i
), HIBYTE(i
) );
1103 /* Output the DLL names */
1105 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1107 if (odp
->type
!= TYPE_INVALID
)
1108 fprintf( outfile
, "Name_%d:\t.ascii \"%s\\0\"\n", i
, odp
->name
);
1111 /* Output the DLL functions arguments */
1113 args
= p
= (unsigned char *)xmalloc( Limit
- Base
+ 1 );
1114 for (i
= Base
, odp
= OrdinalDefinitions
+ Base
; i
<= Limit
; i
++, odp
++)
1119 *p
++ = (unsigned char)strlen(odp
->u
.func
.arg_types
);
1122 *p
++ = 0x80 | (unsigned char)strlen(odp
->u
.func
.arg_types
);
1132 DumpBytes( outfile
, args
, Limit
- Base
+ 1, NULL
, "FuncArgs" );
1134 /* Output the DLL descriptor */
1136 fprintf( outfile
, "DLLName:\t.ascii \"%s\\0\"\n", DLLName
);
1137 fprintf( outfile
, "\t.align 4\n" );
1138 fprintf( outfile
, "\t.globl " PREFIX
"%s_Descriptor\n", DLLName
);
1139 fprintf( outfile
, PREFIX
"%s_Descriptor:\n", DLLName
);
1140 fprintf( outfile
, "\t.long DLLName\n" ); /* Name */
1141 fprintf( outfile
, "\t.long %d\n", Base
); /* Base */
1142 fprintf( outfile
, "\t.long %d\n", Limit
+1-Base
); /* Number of functions */
1143 fprintf( outfile
, "\t.long %d\n", nb_names
); /* Number of names */
1144 fprintf( outfile
, "\t.long Functions\n" ); /* Functions */
1145 fprintf( outfile
, "\t.long FuncNames\n" ); /* Function names */
1146 fprintf( outfile
, "\t.long FuncOrdinals\n" ); /* Function ordinals */
1147 fprintf( outfile
, "\t.long FuncArgs\n" ); /* Function arguments */
1148 fprintf( outfile
, "\t.long ArgTypes\n" ); /* Function argtypes */
1150 fprintf( outfile
, "\t.text\n");
1151 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
1152 fprintf( outfile
, ".Letext:\n");
1158 /*******************************************************************
1161 * Build a Win16 assembly file from a spec file.
1163 static int BuildSpec16File( char * specfile
, FILE *outfile
)
1167 int code_offset
, data_offset
, module_size
;
1168 unsigned char *data
;
1170 data
= (unsigned char *)xmalloc( 0x10000 );
1171 memset( data
, 0, 16 );
1174 fprintf( outfile
, "/* File generated automatically; do not edit! */\n" );
1175 fprintf( outfile
, "\t.text\n" );
1176 fprintf( outfile
, "Code_Start:\n" );
1179 odp
= OrdinalDefinitions
;
1180 for (i
= 0; i
<= Limit
; i
++, odp
++)
1185 odp
->offset
= 0xffff;
1189 odp
->offset
= LOWORD(odp
->u
.abs
.value
);
1193 odp
->offset
= data_offset
;
1194 data_offset
+= StoreVariableCode( data
+ data_offset
, 1, odp
);
1198 odp
->offset
= data_offset
;
1199 data_offset
+= StoreVariableCode( data
+ data_offset
, 2, odp
);
1203 odp
->offset
= data_offset
;
1204 data_offset
+= StoreVariableCode( data
+ data_offset
, 4, odp
);
1208 fprintf( outfile
,"/* %s.%d */\n", DLLName
, i
);
1209 fprintf( outfile
,"\tmovw $%d,%%ax\n",LOWORD(odp
->u
.ret
.ret_value
));
1210 fprintf( outfile
,"\tmovw $%d,%%dx\n",HIWORD(odp
->u
.ret
.ret_value
));
1211 fprintf( outfile
,"\t.byte 0x66\n");
1212 if (odp
->u
.ret
.arg_size
!= 0)
1213 fprintf( outfile
, "\tlret $%d\n\n", odp
->u
.ret
.arg_size
);
1216 fprintf( outfile
, "\tlret\n");
1217 fprintf( outfile
, "\tnop\n");
1218 fprintf( outfile
, "\tnop\n\n");
1220 odp
->offset
= code_offset
;
1221 code_offset
+= 12; /* Assembly code is 12 bytes long */
1226 case TYPE_PASCAL_16
:
1228 fprintf( outfile
, "/* %s.%d */\n", DLLName
, i
);
1229 fprintf( outfile
, "\tpushw %%bp\n" );
1230 fprintf( outfile
, "\tpushl $" PREFIX
"%s\n",odp
->u
.func
.link_name
);
1231 /* FreeBSD does not understand lcall, so do it the hard way */
1232 fprintf( outfile
, "\t.byte 0x9a\n" );
1233 fprintf( outfile
, "\t.long " PREFIX
"CallFrom16_%s_%s\n",
1234 (odp
->type
== TYPE_REGISTER
) ? "regs" :
1235 (odp
->type
== TYPE_PASCAL
) ? "long" : "word",
1236 odp
->u
.func
.arg_types
);
1237 fprintf( outfile
, "\t.long 0x%08lx\n",
1238 MAKELONG( Code_Selector
, 0x9090 /* nop ; nop */ ) );
1239 odp
->offset
= code_offset
;
1240 code_offset
+= 16; /* Assembly code is 16 bytes long */
1244 fprintf(stderr
,"build: function type %d not available for Win16\n",
1250 if (!code_offset
) /* Make sure the code segment is not empty */
1252 fprintf( outfile
, "\t.byte 0\n" );
1256 /* Output data segment */
1258 DumpBytes( outfile
, data
, data_offset
, NULL
, "Data_Start" );
1260 /* Build the module */
1262 module_size
= BuildModule16( outfile
, code_offset
, data_offset
);
1264 /* Output the DLL descriptor */
1266 fprintf( outfile
, "\t.text\n" );
1267 fprintf( outfile
, "DLLName:\t.ascii \"%s\\0\"\n", DLLName
);
1268 fprintf( outfile
, "\t.align 4\n" );
1269 fprintf( outfile
, "\t.globl " PREFIX
"%s_Descriptor\n", DLLName
);
1270 fprintf( outfile
, PREFIX
"%s_Descriptor:\n", DLLName
);
1271 fprintf( outfile
, "\t.long DLLName\n" ); /* Name */
1272 fprintf( outfile
, "\t.long Module_Start\n" ); /* Module start */
1273 fprintf( outfile
, "\t.long %d\n", module_size
); /* Module size */
1274 fprintf( outfile
, "\t.long Code_Start\n" ); /* Code start */
1275 fprintf( outfile
, "\t.long Data_Start\n" ); /* Data start */
1280 /*******************************************************************
1283 * Build an assembly file from a spec file.
1285 static int BuildSpecFile( FILE *outfile
, char *specname
)
1287 SpecName
= specname
;
1288 SpecFp
= fopen( specname
, "r");
1291 fprintf(stderr
, "Could not open specification file, '%s'\n", specname
);
1295 if (ParseTopLevel() < 0) return -1;
1300 return BuildSpec16File( specname
, outfile
);
1302 return BuildSpec32File( specname
, outfile
);
1304 fprintf( stderr
, "%s: Missing 'type' declaration\n", specname
);
1310 /*******************************************************************
1311 * TransferArgs16To32
1313 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1314 * The 16-bit stack layout is:
1322 static int TransferArgs16To32( FILE *outfile
, char *args
)
1324 int i
, pos16
, pos32
;
1326 /* Save ebx first */
1328 fprintf( outfile
, "\tpushl %%ebx\n" );
1330 /* Get the 32-bit stack pointer */
1332 fprintf( outfile
, "\tmovl " PREFIX
"CALLTO16_Saved32_esp,%%ebx\n" );
1334 /* Copy the arguments */
1336 pos16
= 6; /* skip bp and return address */
1339 for (i
= strlen(args
); i
> 0; i
--)
1344 case 'w': /* word */
1345 fprintf( outfile
, "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1346 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1350 case 's': /* s_word */
1351 fprintf( outfile
, "\tmovswl %d(%%ebp),%%eax\n", pos16
);
1352 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1356 case 'l': /* long or segmented pointer */
1357 case 'T': /* segmented pointer to null-terminated string */
1358 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", pos16
);
1359 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1363 case 'p': /* linear pointer */
1364 case 't': /* linear pointer to null-terminated string */
1365 /* Get the selector */
1366 fprintf( outfile
, "\tmovw %d(%%ebp),%%ax\n", pos16
+ 2 );
1367 /* Get the selector base */
1368 fprintf( outfile
, "\tandl $0xfff8,%%eax\n" );
1369 fprintf( outfile
, "\tmovl " PREFIX
"ldt_copy(%%eax),%%eax\n" );
1370 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1371 /* Add the offset */
1372 fprintf( outfile
, "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1373 fprintf( outfile
, "\taddl %%eax,%d(%%ebx)\n", pos32
);
1378 fprintf( stderr
, "Unknown arg type '%c'\n", args
[i
-1] );
1384 fprintf( outfile
, "\tpopl %%ebx\n" );
1386 return pos16
- 6; /* Return the size of the 16-bit args */
1390 /*******************************************************************
1393 * Build the context structure on the 32-bit stack.
1395 static void BuildContext16( FILE *outfile
)
1397 /* Save ebx first */
1399 fprintf( outfile
, "\tpushl %%ebx\n" );
1401 /* Get the 32-bit stack pointer */
1403 fprintf( outfile
, "\tmovl " PREFIX
"CALLTO16_Saved32_esp,%%ebx\n" );
1405 /* Store the registers */
1407 fprintf( outfile
, "\tpopl %d(%%ebx)\n", /* Get ebx from stack*/
1408 CONTEXTOFFSET(Ebx
) - sizeof(CONTEXT
) );
1409 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1410 CONTEXTOFFSET(Eax
) - sizeof(CONTEXT
) );
1411 fprintf( outfile
, "\tmovl %%ecx,%d(%%ebx)\n",
1412 CONTEXTOFFSET(Ecx
) - sizeof(CONTEXT
) );
1413 fprintf( outfile
, "\tmovl %%edx,%d(%%ebx)\n",
1414 CONTEXTOFFSET(Edx
) - sizeof(CONTEXT
) );
1415 fprintf( outfile
, "\tmovl %%esi,%d(%%ebx)\n",
1416 CONTEXTOFFSET(Esi
) - sizeof(CONTEXT
) );
1417 fprintf( outfile
, "\tmovl %%edi,%d(%%ebx)\n",
1418 CONTEXTOFFSET(Edi
) - sizeof(CONTEXT
) );
1420 fprintf( outfile
, "\tmovzwl -10(%%ebp),%%eax\n" ); /* Get %ds from stack*/
1421 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1422 CONTEXTOFFSET(SegDs
) - sizeof(CONTEXT
) );
1423 fprintf( outfile
, "\tmovzwl -6(%%ebp),%%eax\n" ); /* Get %es from stack*/
1424 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1425 CONTEXTOFFSET(SegEs
) - sizeof(CONTEXT
) );
1426 fprintf( outfile
, "\tpushfl\n" );
1427 fprintf( outfile
, "\tpopl %d(%%ebx)\n",
1428 CONTEXTOFFSET(EFlags
) - sizeof(CONTEXT
) );
1429 fprintf( outfile
, "\tmovl -16(%%ebp),%%eax\n" ); /* Get %ebp from stack */
1430 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1431 CONTEXTOFFSET(Ebp
) - sizeof(CONTEXT
) );
1432 fprintf( outfile
, "\tmovzwl 2(%%ebp),%%eax\n" ); /* Get %ip from stack */
1433 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1434 CONTEXTOFFSET(Eip
) - sizeof(CONTEXT
) );
1435 fprintf( outfile
, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1436 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1437 CONTEXTOFFSET(SegCs
) - sizeof(CONTEXT
) );
1438 fprintf( outfile
, "\tmovw %%fs,%%ax\n" );
1439 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1440 CONTEXTOFFSET(SegFs
) - sizeof(CONTEXT
) );
1441 fprintf( outfile
, "\tmovw %%gs,%%ax\n" );
1442 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1443 CONTEXTOFFSET(SegGs
) - sizeof(CONTEXT
) );
1444 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
1445 fprintf( outfile
, "\tmovl %%eax,%d(%%ebx)\n",
1446 CONTEXTOFFSET(SegSs
) - sizeof(CONTEXT
) );
1448 fprintf( outfile
, "\tfsave %d(%%ebx)\n",
1449 CONTEXTOFFSET(FloatSave
) - sizeof(CONTEXT
) );
1454 /*******************************************************************
1457 * Restore the registers from the context structure.
1458 * %edx must point to the 32-bit stack top.
1460 static void RestoreContext16( FILE *outfile
)
1462 /* Get the 32-bit stack pointer */
1464 fprintf( outfile
, "\tmovl %%edx,%%ebx\n" );
1466 /* Remove everything up to the return address from the 16-bit stack */
1468 fprintf( outfile
, "\taddl $22,%%esp\n" );
1470 /* Restore the registers */
1472 fprintf( outfile
, "\tmovl %d(%%ebx),%%ecx\n",
1473 CONTEXTOFFSET(Ecx
) - sizeof(CONTEXT
) );
1474 fprintf( outfile
, "\tmovl %d(%%ebx),%%edx\n",
1475 CONTEXTOFFSET(Edx
) - sizeof(CONTEXT
) );
1476 fprintf( outfile
, "\tmovl %d(%%ebx),%%esi\n",
1477 CONTEXTOFFSET(Esi
) - sizeof(CONTEXT
) );
1478 fprintf( outfile
, "\tmovl %d(%%ebx),%%edi\n",
1479 CONTEXTOFFSET(Edi
) - sizeof(CONTEXT
) );
1480 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebp\n",
1481 CONTEXTOFFSET(Ebp
) - sizeof(CONTEXT
) );
1482 fprintf( outfile
, "\tpushw %d(%%ebx)\n", /* Push new cs */
1483 CONTEXTOFFSET(SegCs
) - sizeof(CONTEXT
) );
1484 fprintf( outfile
, "\tpushw %d(%%ebx)\n", /* Push new ip */
1485 CONTEXTOFFSET(Eip
) - sizeof(CONTEXT
) );
1486 fprintf( outfile
, "\tpushw %d(%%ebx)\n", /* Push new ds */
1487 CONTEXTOFFSET(SegDs
) - sizeof(CONTEXT
) );
1488 fprintf( outfile
, "\tpushw %d(%%ebx)\n", /* Push new es */
1489 CONTEXTOFFSET(SegEs
) - sizeof(CONTEXT
) );
1490 fprintf( outfile
, "\tpushl %d(%%ebx)\n",
1491 CONTEXTOFFSET(EFlags
) - sizeof(CONTEXT
) );
1492 fprintf( outfile
, "\tpopfl\n" );
1493 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n",
1494 CONTEXTOFFSET(Eax
) - sizeof(CONTEXT
) );
1495 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebx\n",
1496 CONTEXTOFFSET(Ebx
) - sizeof(CONTEXT
) );
1497 fprintf( outfile
, "\tpopw %%es\n" ); /* Set es */
1498 fprintf( outfile
, "\tpopw %%ds\n" ); /* Set ds */
1502 /*******************************************************************
1503 * BuildCallFrom16Func
1505 * Build a 16-bit-to-Wine callback function. The syntax of the function
1506 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1507 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1508 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1509 * 'T'=segmented pointer to null-terminated string).
1510 * For register functions, the arguments are ignored, but they are still
1511 * removed from the stack upon return.
1513 * Stack layout upon entry to the callback function:
1515 * (sp+18) word first 16-bit arg
1519 * (sp+8) long 32-bit entry point (used to store edx)
1520 * (sp+6) word high word of cs (always 0, used to store es)
1521 * (sp+4) word low word of cs of 16-bit entry point
1522 * (sp+2) word high word of ip (always 0, used to store ds)
1523 * (sp) word low word of ip of 16-bit entry point
1525 * Added on the stack:
1527 * (sp-6) word saved previous sp
1528 * (sp-8) word saved previous ss
1530 static void BuildCallFrom16Func( FILE *outfile
, char *profile
)
1535 char *args
= profile
+ 5;
1537 /* Parse function type */
1539 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1540 else if (!strncmp( "regs_", profile
, 5 )) reg_func
= 1;
1541 else if (strncmp( "long_", profile
, 5 ))
1543 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1547 /* Function header */
1549 fprintf( outfile
, "\n\t.align 4\n" );
1551 fprintf( outfile
, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX
"CallFrom16_%s\n",
1554 fprintf( outfile
, "\t.globl " PREFIX
"CallFrom16_%s\n", profile
);
1555 fprintf( outfile
, PREFIX
"CallFrom16_%s:\n", profile
);
1557 /* Setup bp to point to its copy on the stack */
1559 fprintf( outfile
, "\tpushl %%ebp\n" ); /* Save the full 32-bit ebp */
1560 fprintf( outfile
, "\tmovzwl %%sp,%%ebp\n" );
1561 fprintf( outfile
, "\taddw $16,%%bp\n" );
1563 /* Save 16-bit ds and es */
1565 /* Stupid FreeBSD assembler doesn't know these either */
1566 /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1567 fprintf( outfile
, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1568 /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1569 fprintf( outfile
, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1571 /* Restore 32-bit ds and es */
1573 fprintf( outfile
, "\tpushl $0x%04x%04x\n", Data_Selector
, Data_Selector
);
1574 fprintf( outfile
, "\tpopw %%ds\n" );
1575 fprintf( outfile
, "\tpopw %%es\n" );
1578 /* Save the 16-bit stack */
1580 fprintf( outfile
, "\tpushl " PREFIX
"IF1632_Saved16_ss_sp\n" );
1582 fprintf( outfile
,"\tdata16\n");
1584 fprintf( outfile
, "\tmovw %%ss," PREFIX
"IF1632_Saved16_ss_sp+2\n" );
1585 fprintf( outfile
, "\tmovw %%sp," PREFIX
"IF1632_Saved16_ss_sp\n" );
1587 /* Transfer the arguments */
1589 if (reg_func
) BuildContext16( outfile
);
1590 else if (*args
) argsize
= TransferArgs16To32( outfile
, args
);
1592 /* Get the address of the API function */
1594 fprintf( outfile
, "\tmovl -4(%%ebp),%%eax\n" );
1596 /* If necessary, save %edx over the API function address */
1598 if (!reg_func
&& short_ret
)
1599 fprintf( outfile
, "\tmovl %%edx,-4(%%ebp)\n" );
1601 /* Switch to the 32-bit stack */
1603 fprintf( outfile
, "\tmovl " PREFIX
"CALLTO16_Saved32_esp,%%ebp\n" );
1604 fprintf( outfile
, "\tpushw %%ds\n" );
1605 fprintf( outfile
, "\tpopw %%ss\n" );
1606 fprintf( outfile
, "\tleal -%d(%%ebp),%%esp\n",
1607 reg_func
? sizeof(CONTEXT
) : 4 * strlen(args
) );
1608 if (reg_func
) /* Push the address of the context struct */
1609 fprintf( outfile
, "\tpushl %%esp\n" );
1611 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1613 fprintf( outfile
, "\taddl $%d,%%ebp\n", STRUCTOFFSET(STACK32FRAME
,ebp
) );
1615 /* Print the debug information before the call */
1619 fprintf( outfile
, "\tpushl %%eax\n" );
1620 fprintf( outfile
, "\tpushl $Profile_%s\n", profile
);
1621 fprintf( outfile
, "\tpushl $%d\n", reg_func
? 2 : (short_ret
? 1 : 0));
1622 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16\n" );
1623 fprintf( outfile
, "\tpopl %%eax\n" );
1624 fprintf( outfile
, "\tpopl %%eax\n" );
1625 fprintf( outfile
, "\tpopl %%eax\n" );
1628 /* Call the entry point */
1630 fprintf( outfile
, "\tcall %%eax\n" );
1632 /* Print the debug information after the call */
1638 /* Push again the address of the context struct in case */
1639 /* it has been removed by an stdcall function */
1640 fprintf( outfile
, "\tleal -%d(%%ebp),%%esp\n",
1641 sizeof(CONTEXT
) + STRUCTOFFSET(STACK32FRAME
,ebp
) );
1642 fprintf( outfile
, "\tpushl %%esp\n" );
1644 fprintf( outfile
, "\tpushl %%eax\n" );
1645 fprintf( outfile
, "\tpushl $%d\n", reg_func
? 2 : (short_ret
? 1 : 0));
1646 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret\n" );
1647 fprintf( outfile
, "\tpopl %%eax\n" );
1648 fprintf( outfile
, "\tpopl %%eax\n" );
1651 /* Restore the value of the saved 32-bit stack pointer */
1653 fprintf( outfile
, "\tleal -%d(%%ebp),%%edx\n",
1654 STRUCTOFFSET(STACK32FRAME
,ebp
) );
1655 fprintf( outfile
, "movl %%edx," PREFIX
"CALLTO16_Saved32_esp\n" );
1657 /* Restore the 16-bit stack */
1660 fprintf( outfile
, "\tdata16\n");
1662 fprintf( outfile
, "\tmovw " PREFIX
"IF1632_Saved16_ss_sp+2,%%ss\n" );
1663 fprintf( outfile
, "\tmovw " PREFIX
"IF1632_Saved16_ss_sp,%%sp\n" );
1664 fprintf( outfile
, "\tpopl " PREFIX
"IF1632_Saved16_ss_sp\n" );
1668 /* Calc the arguments size */
1684 fprintf( stderr
, "Unknown arg type '%c'\n", *args
);
1689 /* Restore registers from the context structure */
1690 RestoreContext16( outfile
);
1694 /* Restore high 16 bits of ebp */
1695 fprintf( outfile
, "\tpopl %%ebp\n" );
1697 /* Restore ds and es */
1698 fprintf( outfile
, "\tincl %%esp\n" ); /* Remove ip */
1699 fprintf( outfile
, "\tincl %%esp\n" );
1700 fprintf( outfile
, "\tpopl %%edx\n" ); /* Remove cs and ds */
1701 fprintf( outfile
, "\tmovw %%dx,%%ds\n" ); /* and restore ds */
1702 fprintf( outfile
, "\tpopw %%es\n" ); /* Restore es */
1704 if (short_ret
) fprintf( outfile
, "\tpopl %%edx\n" ); /* Restore edx */
1707 /* Get the return value into dx:ax */
1708 fprintf( outfile
, "\tmovl %%eax,%%edx\n" );
1709 fprintf( outfile
, "\tshrl $16,%%edx\n" );
1710 /* Remove API entry point */
1711 fprintf( outfile
, "\taddl $4,%%esp\n" );
1714 /* Restore low 16 bits of ebp */
1715 fprintf( outfile
, "\tpopw %%bp\n" );
1718 /* Remove the arguments and return */
1722 fprintf( outfile
, "\t.byte 0x66\n" );
1723 fprintf( outfile
, "\tlret $%d\n", argsize
);
1727 fprintf( outfile
, "\t.byte 0x66\n" );
1728 fprintf( outfile
, "\tlret\n" );
1733 /*******************************************************************
1736 * Build a Wine-to-16-bit callback function.
1738 * Stack frame of the callback function:
1742 * (ebp+8) func to call
1743 * (ebp+4) return address
1744 * (ebp) previous ebp
1746 * Prototypes for the CallTo16 functions:
1747 * extern WINAPI WORD CallTo16_word_xxx( FARPROC16 func, args... );
1748 * extern WINAPI LONG CallTo16_long_xxx( FARPROC16 func, args... );
1749 * extern WINAPI void CallTo16_regs_{short,long}( const CONTEXT *context );
1751 static void BuildCallTo16Func( FILE *outfile
, char *profile
)
1755 char *args
= profile
+ 5;
1757 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1758 else if (!strncmp( "regs_", profile
, 5 )) reg_func
= 1;
1759 else if (strncmp( "long_", profile
, 5 ))
1761 fprintf( stderr
, "Invalid function name '%s'.\n", profile
);
1765 /* Function header */
1767 fprintf( outfile
, "\n\t.align 4\n" );
1769 fprintf( outfile
, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX
"CallTo16_%s\n",
1772 fprintf( outfile
, "\t.globl " PREFIX
"CallTo16_%s\n", profile
);
1773 fprintf( outfile
, PREFIX
"CallTo16_%s:\n", profile
);
1777 fprintf( outfile
, "\tpushl %%ebp\n" );
1778 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
1780 /* Call the actual CallTo16 routine (simulate a lcall) */
1782 fprintf( outfile
, "\tpushw $0\n" );
1783 fprintf( outfile
, "\tpushw %%cs\n" );
1784 fprintf( outfile
, "\tcall do_callto16_%s\n", profile
);
1788 /* FIXME: this is a hack because of task.c */
1789 if (!strcmp( profile
, "word_" ))
1791 fprintf( outfile
, ".globl " PREFIX
"CALLTO16_Restore\n" );
1792 fprintf( outfile
, PREFIX
"CALLTO16_Restore:\n" );
1794 fprintf( outfile
, "\tpopl %%ebp\n" );
1795 fprintf( outfile
, "\tret $%d\n", strlen(args
) + 1 );
1797 /* Start of the actual CallTo16 routine */
1799 /* Save the 32-bit registers */
1801 fprintf( outfile
, "do_callto16_%s:\n", profile
);
1802 fprintf( outfile
, "\tpushl %%ebx\n" );
1803 fprintf( outfile
, "\tpushl %%ecx\n" );
1804 fprintf( outfile
, "\tpushl %%edx\n" );
1805 fprintf( outfile
, "\tpushl %%esi\n" );
1806 fprintf( outfile
, "\tpushl %%edi\n" );
1808 /* Save the 32-bit stack */
1810 fprintf( outfile
, "\tmovl %%esp," PREFIX
"CALLTO16_Saved32_esp\n" );
1811 fprintf( outfile
, "\tmovl %%ebp,%%ebx\n" );
1813 /* Print debugging info */
1817 /* Push the address of the first argument */
1818 fprintf( outfile
, "\tleal 8(%%ebp),%%eax\n" );
1819 fprintf( outfile
, "\tpushl $%d\n", reg_func
? -1 : strlen(args
) );
1820 fprintf( outfile
, "\tpushl %%eax\n" );
1821 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16\n" );
1822 fprintf( outfile
, "\tpopl %%eax\n" );
1823 fprintf( outfile
, "\tpopl %%eax\n" );
1828 /* Switch to the 16-bit stack, saving the current %%esp, */
1829 /* and adding the specified offset to the new sp */
1830 fprintf( outfile
, "\tmovzwl " PREFIX
"IF1632_Saved16_ss_sp,%%edx\n" );
1831 fprintf( outfile
, "\tleal -4(%%edx),%%edx\n" );
1832 fprintf( outfile
, "\tmovl 12(%%ebx),%%eax\n" ); /* Get the offset */
1833 fprintf( outfile
, "\taddl %%edx,%%eax\n" );
1835 fprintf( outfile
,"\tdata16\n");
1837 fprintf( outfile
, "\tmovw " PREFIX
"IF1632_Saved16_ss_sp+2,%%ss\n" );
1838 fprintf( outfile
, "\txchgl %%esp,%%eax\n" );
1839 fprintf( outfile
, "\t.byte 0x36\n" /* %ss: */ );
1840 fprintf( outfile
, "\tmovl %%eax,0(%%edx)\n" );
1842 /* Get the registers. ebx is handled later on. */
1844 fprintf( outfile
, "\tmovl 8(%%ebx),%%ebx\n" );
1845 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs
) );
1846 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
1847 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp
) );
1848 fprintf( outfile
, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax
) );
1849 fprintf( outfile
, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx
) );
1850 fprintf( outfile
, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx
) );
1851 fprintf( outfile
, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi
) );
1852 fprintf( outfile
, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi
) );
1854 /* Push the return address
1855 * With _short suffix, we push 16:16 address (normal lret)
1856 * With _long suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
1858 if (!strncmp(profile
,"regs_short",10))
1859 fprintf( outfile
, "\tpushl " PREFIX
"CALLTO16_RetAddr_regs\n" );
1862 fprintf( outfile
, "\tpushw $0\n" );
1863 fprintf( outfile
, "\tpushw " PREFIX
"CALLTO16_RetAddr_regs+2\n" );
1864 fprintf( outfile
, "\tpushw $0\n" );
1865 fprintf( outfile
, "\tpushw " PREFIX
"CALLTO16_RetAddr_regs\n" );
1868 /* Push the called routine address */
1870 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs
) );
1871 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip
) );
1873 /* Get the 16-bit ds */
1875 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegDs
) );
1876 /* Get ebx from the 32-bit stack */
1877 fprintf( outfile
, "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(Ebx
) );
1878 fprintf( outfile
, "\tpopw %%ds\n" );
1880 else /* not a register function */
1882 int pos
= 12; /* first argument position */
1884 /* Switch to the 16-bit stack, saving the current %%esp */
1885 fprintf( outfile
, "\tmovl %%esp,%%eax\n" );
1887 fprintf( outfile
,"\tdata16\n");
1889 fprintf( outfile
, "\tmovw " PREFIX
"IF1632_Saved16_ss_sp+2,%%ss\n" );
1890 fprintf( outfile
, "\tmovw " PREFIX
"IF1632_Saved16_ss_sp,%%sp\n" );
1891 fprintf( outfile
, "\tpushl %%eax\n" );
1893 /* Make %bp point to the previous stackframe (built by CallFrom16) */
1894 fprintf( outfile
, "\tmovzwl %%sp,%%ebp\n" );
1895 fprintf( outfile
, "\tleal %d(%%ebp),%%ebp\n",
1896 STRUCTOFFSET(STACK16FRAME
,bp
) + 4 /* for saved %%esp */ );
1898 /* Transfer the arguments */
1904 case 'w': /* word */
1905 fprintf( outfile
, "\tpushw %d(%%ebx)\n", pos
);
1907 case 'l': /* long */
1908 fprintf( outfile
, "\tpushl %d(%%ebx)\n", pos
);
1911 fprintf( stderr
, "Unexpected case '%c' in BuildCallTo16Func\n",
1917 /* Push the return address */
1919 fprintf( outfile
, "\tpushl " PREFIX
"CALLTO16_RetAddr_%s\n",
1920 short_ret
? "word" : "long" );
1922 /* Push the called routine address */
1924 fprintf( outfile
, "\tpushl 8(%%ebx)\n" );
1926 /* Set %ds and %es (and %ax just in case) equal to %ss */
1928 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
1929 fprintf( outfile
, "\tmovw %%ax,%%ds\n" );
1930 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
1933 /* Jump to the called routine */
1935 fprintf( outfile
, "\t.byte 0x66\n" );
1936 fprintf( outfile
, "\tlret\n" );
1940 /*******************************************************************
1943 * Build the return code for 16-bit callbacks
1945 static void BuildRet16Func( FILE *outfile
)
1947 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Ret_word\n" );
1948 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Ret_long\n" );
1949 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Ret_regs\n" );
1951 fprintf( outfile
, PREFIX
"CALLTO16_Ret_word:\n" );
1952 fprintf( outfile
, "\txorl %%edx,%%edx\n" );
1954 /* Remove the arguments just in case */
1956 fprintf( outfile
, PREFIX
"CALLTO16_Ret_long:\n" );
1957 fprintf( outfile
, "\tleal -%d(%%ebp),%%esp\n",
1958 STRUCTOFFSET(STACK16FRAME
,bp
) + 4 /* for saved %%esp */ );
1960 /* Put return value into %eax */
1962 fprintf( outfile
, PREFIX
"CALLTO16_Ret_regs:\n" );
1963 fprintf( outfile
, "\tshll $16,%%edx\n" );
1964 fprintf( outfile
, "\tmovw %%ax,%%dx\n" );
1965 fprintf( outfile
, "\tmovl %%edx,%%eax\n" );
1967 /* Restore 32-bit segment registers */
1969 fprintf( outfile
, "\tpopl %%ecx\n" ); /* Get the saved %%esp */
1970 fprintf( outfile
, "\tmovw $0x%04x,%%bx\n", Data_Selector
);
1972 fprintf( outfile
, "\tdata16\n");
1974 fprintf( outfile
, "\tmovw %%bx,%%ds\n" );
1976 fprintf( outfile
, "\tdata16\n");
1978 fprintf( outfile
, "\tmovw %%bx,%%es\n" );
1980 /* Restore the 32-bit stack */
1983 fprintf( outfile
, "\tdata16\n");
1985 fprintf( outfile
, "\tmovw %%bx,%%ss\n" );
1986 fprintf( outfile
, "\tmovl %%ecx,%%esp\n" );
1988 /* Restore the 32-bit registers */
1990 fprintf( outfile
, "\tpopl %%edi\n" );
1991 fprintf( outfile
, "\tpopl %%esi\n" );
1992 fprintf( outfile
, "\tpopl %%edx\n" );
1993 fprintf( outfile
, "\tpopl %%ecx\n" );
1994 fprintf( outfile
, "\tpopl %%ebx\n" );
1996 /* Return to caller */
1998 fprintf( outfile
, "\tlret\n" );
2000 /* Declare the return address variables */
2002 fprintf( outfile
, "\t.data\n" );
2003 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_RetAddr_word\n" );
2004 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_RetAddr_long\n" );
2005 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_RetAddr_regs\n" );
2006 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Saved32_esp\n" );
2007 fprintf( outfile
, PREFIX
"CALLTO16_RetAddr_word:\t.long 0\n" );
2008 fprintf( outfile
, PREFIX
"CALLTO16_RetAddr_long:\t.long 0\n" );
2009 fprintf( outfile
, PREFIX
"CALLTO16_RetAddr_regs:\t.long 0\n" );
2010 fprintf( outfile
, PREFIX
"CALLTO16_Saved32_esp:\t.long 0\n" );
2011 fprintf( outfile
, "\t.text\n" );
2015 /*******************************************************************
2016 * BuildCallTo32LargeStack
2018 * Build the function used to switch to the original 32-bit stack
2019 * before calling a 32-bit function from 32-bit code. This is used for
2020 * functions that need a large stack, like X bitmaps functions.
2022 * The generated function has the following prototype:
2023 * int xxx( int (*func)(), void *arg );
2025 * The pointer to the function can be retrieved by calling CALL32_Init,
2026 * which also takes care of saving the current 32-bit stack pointer.
2035 static void BuildCallTo32LargeStack( FILE *outfile
)
2037 /* Initialization function */
2039 fprintf( outfile
, "\n\t.align 4\n" );
2041 fprintf( outfile
, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX
"CALL32_Init\n");
2043 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Init\n" );
2044 fprintf( outfile
, PREFIX
"CALL32_Init:\n" );
2045 fprintf( outfile
, "\tleal -256(%%esp),%%eax\n" );
2046 fprintf( outfile
, "\tmovl %%eax,CALL32_Original32_esp\n" );
2047 fprintf( outfile
, "\tmovl $CALL32_LargeStack,%%eax\n" );
2048 fprintf( outfile
, "\tret\n" );
2050 /* Function header */
2052 fprintf( outfile
, "\n\t.align 4\n" );
2054 fprintf( outfile
, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2056 fprintf( outfile
, "CALL32_LargeStack:\n" );
2060 fprintf( outfile
, "\tpushl %%ebp\n" );
2061 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
2063 /* Switch to the original 32-bit stack pointer */
2065 fprintf( outfile
, "\tmovl CALL32_Original32_esp, %%esp\n" );
2067 /* Transfer the argument and call the function */
2069 fprintf( outfile
, "\tpushl 12(%%ebp)\n" );
2070 fprintf( outfile
, "\tcall 8(%%ebp)\n" );
2072 /* Restore registers and return */
2074 fprintf( outfile
, "\tmovl %%ebp,%%esp\n" );
2075 fprintf( outfile
, "\tpopl %%ebp\n" );
2076 fprintf( outfile
, "\tret\n" );
2080 fprintf( outfile
, "\t.data\n" );
2081 fprintf( outfile
, "CALL32_Original32_esp:\t.long 0\n" );
2082 fprintf( outfile
, "\t.text\n" );
2086 /*******************************************************************
2087 * BuildCallFrom32Regs
2089 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2090 * 'args' is the number of dword arguments.
2094 * (esp+208) ret addr (or relay addr when debugging_relay is on)
2095 * (esp+204) entry point
2096 * (esp+0) CONTEXT struct
2098 static void BuildCallFrom32Regs( FILE *outfile
)
2100 /* Function header */
2102 fprintf( outfile
, "\n\t.align 4\n" );
2104 fprintf( outfile
, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX
"CALL32_Regs\n" );
2106 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Regs\n" );
2107 fprintf( outfile
, PREFIX
"CALL32_Regs:\n" );
2109 /* Build the context structure */
2111 fprintf( outfile
, "\tpushw $0\n" );
2112 fprintf( outfile
, "\tpushw %%ss\n" );
2113 fprintf( outfile
, "\tpushl %%eax\n" ); /* %esp place holder */
2114 fprintf( outfile
, "\tpushfl\n" );
2115 fprintf( outfile
, "\tpushw $0\n" );
2116 fprintf( outfile
, "\tpushw %%cs\n" );
2117 fprintf( outfile
, "\tpushl 20(%%esp)\n" ); /* %eip at time of call */
2118 fprintf( outfile
, "\tpushl %%ebp\n" );
2120 fprintf( outfile
, "\tpushl %%eax\n" );
2121 fprintf( outfile
, "\tpushl %%ecx\n" );
2122 fprintf( outfile
, "\tpushl %%edx\n" );
2123 fprintf( outfile
, "\tpushl %%ebx\n" );
2124 fprintf( outfile
, "\tpushl %%esi\n" );
2125 fprintf( outfile
, "\tpushl %%edi\n" );
2127 fprintf( outfile
, "\txorl %%eax,%%eax\n" );
2128 fprintf( outfile
, "\tmovw %%ds,%%ax\n" );
2129 fprintf( outfile
, "\tpushl %%eax\n" );
2130 fprintf( outfile
, "\tmovw %%es,%%ax\n" );
2131 fprintf( outfile
, "\tpushl %%eax\n" );
2132 fprintf( outfile
, "\tmovw %%fs,%%ax\n" );
2133 fprintf( outfile
, "\tpushl %%eax\n" );
2134 fprintf( outfile
, "\tmovw %%gs,%%ax\n" );
2135 fprintf( outfile
, "\tpushl %%eax\n" );
2137 fprintf( outfile
, "\tleal -%d(%%esp),%%esp\n",
2138 sizeof(FLOATING_SAVE_AREA
) + 6 * sizeof(DWORD
) /* DR regs */ );
2139 fprintf( outfile
, "\tpushl $0x0001001f\n" ); /* ContextFlags */
2141 fprintf( outfile
, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave
) );
2143 fprintf( outfile
, "\tleal %d(%%esp),%%eax\n",
2144 sizeof(CONTEXT
) + 4 ); /* %esp at time of call */
2145 fprintf( outfile
, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Esp
) );
2147 fprintf( outfile
, "\tcall " PREFIX
"RELAY_CallFrom32Regs\n" );
2149 /* Restore the context structure */
2151 fprintf( outfile
, "\tfrstor %d(%%esp)\n", CONTEXTOFFSET(FloatSave
) );
2153 /* Store %eip value onto the new stack */
2155 fprintf( outfile
, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Eip
) );
2156 fprintf( outfile
, "\tmovl %d(%%esp),%%ebx\n", CONTEXTOFFSET(Esp
) );
2157 fprintf( outfile
, "\tmovl %%eax,0(%%ebx)\n" );
2159 /* Restore all registers */
2161 fprintf( outfile
, "\tleal %d(%%esp),%%esp\n",
2162 sizeof(FLOATING_SAVE_AREA
) + 7 * sizeof(DWORD
) );
2163 fprintf( outfile
, "\tpopl %%eax\n" );
2164 fprintf( outfile
, "\tmovw %%ax,%%gs\n" );
2165 fprintf( outfile
, "\tpopl %%eax\n" );
2166 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
2167 fprintf( outfile
, "\tpopl %%eax\n" );
2168 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
2169 fprintf( outfile
, "\tpopl %%eax\n" );
2170 fprintf( outfile
, "\tmovw %%ax,%%ds\n" );
2172 fprintf( outfile
, "\tpopl %%edi\n" );
2173 fprintf( outfile
, "\tpopl %%esi\n" );
2174 fprintf( outfile
, "\tpopl %%ebx\n" );
2175 fprintf( outfile
, "\tpopl %%edx\n" );
2176 fprintf( outfile
, "\tpopl %%ecx\n" );
2177 fprintf( outfile
, "\tpopl %%eax\n" );
2178 fprintf( outfile
, "\tpopl %%ebp\n" );
2179 fprintf( outfile
, "\tleal 8(%%esp),%%esp\n" ); /* skip %eip and %cs */
2180 fprintf( outfile
, "\tpopfl\n" );
2181 fprintf( outfile
, "\tpopl %%esp\n" );
2182 fprintf( outfile
, "\tret\n" );
2186 /*******************************************************************
2189 * Build the spec files
2191 static int BuildSpec( FILE *outfile
, int argc
, char *argv
[] )
2194 for (i
= 2; i
< argc
; i
++)
2195 if (BuildSpecFile( outfile
, argv
[i
] ) < 0) return -1;
2200 /*******************************************************************
2203 * Build the 16-bit-to-Wine callbacks
2205 static int BuildCallFrom16( FILE *outfile
, char * outname
, int argc
, char *argv
[] )
2212 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2213 fprintf( outfile
, "\t.text\n" );
2216 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2217 getcwd(buffer
, sizeof(buffer
));
2220 * The stabs help the internal debugger as they are an indication that it
2221 * is sensible to step into a thunk/trampoline.
2223 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2224 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2225 fprintf( outfile
, "\t.text\n" );
2226 fprintf( outfile
, "\t.align 4\n" );
2227 fprintf( outfile
, "Code_Start:\n\n" );
2230 /* Build the callback functions */
2232 for (i
= 2; i
< argc
; i
++) BuildCallFrom16Func( outfile
, argv
[i
] );
2234 /* Output the argument debugging strings */
2238 fprintf( outfile
, "/* Argument strings */\n" );
2239 for (i
= 2; i
< argc
; i
++)
2241 fprintf( outfile
, "Profile_%s:\n", argv
[i
] );
2242 fprintf( outfile
, "\t.ascii \"%s\\0\"\n", argv
[i
] + 5 );
2247 fprintf( outfile
, "\t.text\n");
2248 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2249 fprintf( outfile
, ".Letext:\n");
2256 /*******************************************************************
2259 * Build the Wine-to-16-bit callbacks
2261 static int BuildCallTo16( FILE *outfile
, char * outname
, int argc
, char *argv
[] )
2268 infile
= fopen( argv
[2], "r" );
2275 else infile
= stdin
;
2279 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2280 fprintf( outfile
, "\t.text\n" );
2283 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2284 getcwd(buffer
, sizeof(buffer
));
2287 * The stabs help the internal debugger as they are an indication that it
2288 * is sensible to step into a thunk/trampoline.
2290 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2291 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2292 fprintf( outfile
, "\t.text\n" );
2293 fprintf( outfile
, "\t.align 4\n" );
2294 fprintf( outfile
, "Code_Start:\n\n" );
2297 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_Start\n" );
2298 fprintf( outfile
, PREFIX
"CALLTO16_Start:\n" );
2300 /* Build the callback functions */
2302 while (fgets( buffer
, sizeof(buffer
), infile
))
2304 if (strstr( buffer
, "### start build ###" )) break;
2306 while (fgets( buffer
, sizeof(buffer
), infile
))
2308 char *p
= strstr( buffer
, "CallTo16_" );
2311 char *profile
= p
+ strlen( "CallTo16_" );
2313 while ((*p
== '_') || isalpha(*p
)) p
++;
2315 BuildCallTo16Func( outfile
, profile
);
2317 if (strstr( buffer
, "### stop build ###" )) break;
2320 /* Output the 16-bit return code */
2322 BuildRet16Func( outfile
);
2324 fprintf( outfile
, "\t.globl " PREFIX
"CALLTO16_End\n" );
2325 fprintf( outfile
, PREFIX
"CALLTO16_End:\n" );
2328 fprintf( outfile
, "\t.text\n");
2329 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2330 fprintf( outfile
, ".Letext:\n");
2338 /*******************************************************************
2341 * Build the 32-bit callbacks
2343 static int BuildCall32( FILE *outfile
, char * outname
)
2349 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
2350 fprintf( outfile
, "\t.text\n" );
2353 fprintf( outfile
, "\t.file\t\"%s\"\n", outname
);
2354 getcwd(buffer
, sizeof(buffer
));
2357 * The stabs help the internal debugger as they are an indication that it
2358 * is sensible to step into a thunk/trampoline.
2360 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
2361 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", outname
);
2362 fprintf( outfile
, "\t.text\n" );
2363 fprintf( outfile
, "\t.align 4\n" );
2364 fprintf( outfile
, "Code_Start:\n" );
2367 /* Build the 32-bit large stack callback */
2369 BuildCallTo32LargeStack( outfile
);
2371 /* Build the register callback function */
2373 BuildCallFrom32Regs( outfile
);
2376 fprintf( outfile
, "\t.text\n");
2377 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
2378 fprintf( outfile
, ".Letext:\n");
2385 /*******************************************************************
2388 static void usage(void)
2391 "usage: build [-o outfile] -spec SPECNAMES\n"
2392 " build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2393 " build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2394 " build [-o outfile] -call32\n" );
2399 /*******************************************************************
2402 int main(int argc
, char **argv
)
2404 char *outname
= NULL
;
2405 FILE *outfile
= stdout
;
2408 if (argc
< 2) usage();
2410 if (!strcmp( argv
[1], "-o" ))
2415 if (argc
< 2) usage();
2416 if (!(outfile
= fopen( outname
, "w" )))
2418 fprintf( stderr
, "Unable to create output file '%s'\n", outname
);
2423 /* Retrieve the selector values; this assumes that we are building
2424 * the asm files on the platform that will also run them. Probably
2425 * a safe assumption to make.
2427 GET_CS( Code_Selector
);
2428 GET_DS( Data_Selector
);
2430 if (!strcmp( argv
[1], "-spec" ))
2431 res
= BuildSpec( outfile
, argc
, argv
);
2432 else if (!strcmp( argv
[1], "-callfrom16" ))
2433 res
= BuildCallFrom16( outfile
, outname
, argc
, argv
);
2434 else if (!strcmp( argv
[1], "-callto16" ))
2435 res
= BuildCallTo16( outfile
, outname
, argc
, argv
);
2436 else if (!strcmp( argv
[1], "-call32" ))
2437 res
= BuildCall32( outfile
, outname
);