2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Martin von Loewis
4 * Copyright 1995, 1996 Alexandre Julliard
13 #include "registers.h"
14 #include "winerror.h" /* for ERROR_CALL_NOT_IMPLEMENTED */
19 /* ELF symbols do not have an underscore in front */
20 #if defined (__ELF__) || defined (__svr4__) || defined(_SCO_DS)
26 #define TYPE_INVALID 0
30 #define TYPE_PASCAL_16 4
32 #define TYPE_REGISTER 6
36 #define TYPE_STDCALL 10
38 #define MAX_ORDINALS 1299
40 /* Callback function used for stub functions */
41 #define STUB_CALLBACK \
42 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
51 typedef struct ordinal_definition_s
56 void *additional_data
;
59 typedef struct ordinal_variable_definition_s
65 typedef struct ordinal_function_definition_s
69 char internal_name
[80];
72 typedef struct ordinal_return_definition_s
78 static ORDDEF OrdinalDefinitions
[MAX_ORDINALS
];
80 static enum SPEC_TYPE SpecType
= SPEC_INVALID
;
87 char *ParseBuffer
= NULL
;
92 static int debugging
= 1;
94 /* Offset of register relative to the end of the context struct */
95 #define CONTEXTOFFSET(reg) \
96 ((int)®##_reg((struct sigcontext_struct *)0) \
97 - sizeof(struct sigcontext_struct))
99 static void *xmalloc (size_t size
)
103 res
= malloc (size
? size
: 1);
106 fprintf (stderr
, "Virtual memory exhausted.\n");
113 static void *xrealloc (void *ptr
, size_t size
)
115 void *res
= realloc (ptr
, size
);
118 fprintf (stderr
, "Virtual memory exhausted.\n");
125 static int IsNumberString(char *s
)
134 static char *strupper(char *s
)
138 for(p
= s
; *p
!= '\0'; p
++)
144 static char * GetTokenInLine(void)
149 if (ParseNext
!= ParseBuffer
)
151 if (ParseSaveChar
== '\0')
153 *ParseNext
= ParseSaveChar
;
157 * Remove initial white space.
159 for (p
= ParseNext
; isspace(*p
); p
++)
162 if ((*p
== '\0') || (*p
== '#'))
169 if (*token
!= '(' && *token
!= ')')
170 while (*p
!= '\0' && *p
!= '(' && *p
!= ')' && !isspace(*p
))
180 static char * GetToken(void)
184 if (ParseBuffer
== NULL
)
186 ParseBuffer
= xmalloc(512);
187 ParseNext
= ParseBuffer
;
191 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
193 if (ParseBuffer
[0] != '#')
198 while ((token
= GetTokenInLine()) == NULL
)
200 ParseNext
= ParseBuffer
;
204 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
206 if (ParseBuffer
[0] != '#')
214 static int ParseVariable(int ordinal
, int type
)
218 char export_name
[80];
223 int value_array_size
;
225 strcpy(export_name
, GetToken());
230 fprintf(stderr
, "%d: Expected '(' got '%s'\n", Line
, token
);
235 value_array_size
= 25;
236 value_array
= xmalloc(sizeof(*value_array
) * value_array_size
);
238 while ((token
= GetToken()) != NULL
)
243 value_array
[n_values
++] = strtol(token
, &endptr
, 0);
244 if (n_values
== value_array_size
)
246 value_array_size
+= 25;
247 value_array
= xrealloc(value_array
,
248 sizeof(*value_array
) * value_array_size
);
251 if (endptr
== NULL
|| *endptr
!= '\0')
253 fprintf(stderr
, "%d: Expected number value, got '%s'\n", Line
,
261 fprintf(stderr
, "%d: End of file in variable declaration\n", Line
);
265 if (ordinal
>= MAX_ORDINALS
)
267 fprintf(stderr
, "%d: Ordinal number too large\n", Line
);
271 odp
= &OrdinalDefinitions
[ordinal
];
273 strcpy(odp
->export_name
, export_name
);
275 vdp
= xmalloc(sizeof(*vdp
));
276 odp
->additional_data
= vdp
;
278 vdp
->n_values
= n_values
;
279 vdp
->values
= xrealloc(value_array
, sizeof(*value_array
) * n_values
);
284 static int ParseExportFunction(int ordinal
, int type
)
291 odp
= &OrdinalDefinitions
[ordinal
];
292 strcpy(odp
->export_name
, GetToken());
294 fdp
= xmalloc(sizeof(*fdp
));
295 odp
->additional_data
= fdp
;
300 fprintf(stderr
, "%d: Expected '(' got '%s'\n", Line
, token
);
304 for (i
= 0; i
< 16; i
++)
310 if (!strcmp(token
, "byte") || !strcmp(token
, "word"))
311 fdp
->arg_types
[i
] = 'w';
312 else if (!strcmp(token
, "s_byte") || !strcmp(token
, "s_word"))
313 fdp
->arg_types
[i
] = 's';
314 else if (!strcmp(token
, "long") || !strcmp(token
, "segptr"))
315 fdp
->arg_types
[i
] = 'l';
316 else if (!strcmp(token
, "ptr"))
317 fdp
->arg_types
[i
] = 'p';
320 fprintf(stderr
, "%d: Unknown variable type '%s'\n", Line
, token
);
324 fdp
->arg_types
[i
] = '\0';
326 strcpy(fdp
->internal_name
, GetToken());
330 static int ParseEquate(int ordinal
)
337 odp
= &OrdinalDefinitions
[ordinal
];
338 strcpy(odp
->export_name
, GetToken());
341 value
= strtol(token
, &endptr
, 0);
342 if (endptr
== NULL
|| *endptr
!= '\0')
344 fprintf(stderr
, "%d: Expected number value, got '%s'\n", Line
,
349 odp
->type
= TYPE_ABS
;
350 odp
->additional_data
= (void *) value
;
355 static int ParseReturn(int ordinal
)
362 rdp
= xmalloc(sizeof(*rdp
));
364 odp
= &OrdinalDefinitions
[ordinal
];
365 strcpy(odp
->export_name
, GetToken());
366 odp
->type
= TYPE_RETURN
;
367 odp
->additional_data
= rdp
;
370 rdp
->arg_size
= strtol(token
, &endptr
, 0);
371 if (endptr
== NULL
|| *endptr
!= '\0')
373 fprintf(stderr
, "%d: Expected number value, got '%s'\n", Line
,
379 rdp
->ret_value
= strtol(token
, &endptr
, 0);
380 if (endptr
== NULL
|| *endptr
!= '\0')
382 fprintf(stderr
, "%d: Expected number value, got '%s'\n", Line
,
391 static int ParseStub( int ordinal
)
396 odp
= &OrdinalDefinitions
[ordinal
];
397 strcpy( odp
->export_name
, GetToken() );
398 odp
->type
= TYPE_STUB
;
399 fdp
= xmalloc(sizeof(*fdp
));
400 odp
->additional_data
= fdp
;
401 fdp
->arg_types
[0] = '\0';
402 strcpy( fdp
->internal_name
, STUB_CALLBACK
);
407 static int ParseOrdinal(int ordinal
)
411 if (ordinal
>= MAX_ORDINALS
)
413 fprintf(stderr
, "%d: Ordinal number too large\n", Line
);
416 if (ordinal
> Limit
) Limit
= ordinal
;
421 fprintf(stderr
, "%d: Expected type after ordinal\n", Line
);
425 if (strcmp(token
, "byte") == 0)
426 return ParseVariable(ordinal
, TYPE_BYTE
);
427 else if (strcmp(token
, "word") == 0)
428 return ParseVariable(ordinal
, TYPE_WORD
);
429 else if (strcmp(token
, "long") == 0)
430 return ParseVariable(ordinal
, TYPE_LONG
);
431 else if (strcmp(token
, "p") == 0)
432 return ParseExportFunction(ordinal
, TYPE_PASCAL
);
433 else if (strcmp(token
, "pascal") == 0)
434 return ParseExportFunction(ordinal
, TYPE_PASCAL
);
435 else if (strcmp(token
, "pascal16") == 0)
436 return ParseExportFunction(ordinal
, TYPE_PASCAL_16
);
437 else if (strcmp(token
, "register") == 0)
438 return ParseExportFunction(ordinal
, TYPE_REGISTER
);
439 else if (strcmp(token
, "stdcall") == 0)
440 return ParseExportFunction(ordinal
, TYPE_STDCALL
);
441 else if (strcmp(token
, "equate") == 0)
442 return ParseEquate(ordinal
);
443 else if (strcmp(token
, "return") == 0)
444 return ParseReturn(ordinal
);
445 else if (strcmp(token
, "stub") == 0)
446 return ParseStub(ordinal
);
450 "%d: Expected type after ordinal, found '%s' instead\n",
456 static int ParseTopLevel(void)
460 while ((token
= GetToken()) != NULL
)
462 if (strcmp(token
, "name") == 0)
464 strcpy(DLLName
, GetToken());
467 else if (strcmp(token
, "type") == 0)
470 if (!strcmp(token
, "win16" )) SpecType
= SPEC_WIN16
;
471 else if (!strcmp(token
, "win32" )) SpecType
= SPEC_WIN32
;
474 fprintf(stderr
, "%d: Type must be 'win16' or 'win32'\n", Line
);
478 else if (strcmp(token
, "base") == 0)
481 if (!IsNumberString(token
))
483 fprintf(stderr
, "%d: Expected number after base\n", Line
);
488 else if (strcmp(token
, "heap") == 0)
491 if (!IsNumberString(token
))
493 fprintf(stderr
, "%d: Expected number after heap\n", Line
);
496 HeapSize
= atoi(token
);
498 else if (IsNumberString(token
))
503 ordinal
= atoi(token
);
504 if ((rv
= ParseOrdinal(ordinal
)) < 0)
510 "%d: Expected name, id, length or ordinal\n", Line
);
519 /*******************************************************************
522 * Store a list of ints into a byte array.
524 static int StoreVariableCode( unsigned char *buffer
, int size
, ORDDEF
*odp
)
529 vdp
= odp
->additional_data
;
533 for (i
= 0; i
< vdp
->n_values
; i
++)
534 buffer
[i
] = vdp
->values
[i
];
537 for (i
= 0; i
< vdp
->n_values
; i
++)
538 ((unsigned short *)buffer
)[i
] = vdp
->values
[i
];
541 for (i
= 0; i
< vdp
->n_values
; i
++)
542 ((unsigned int *)buffer
)[i
] = vdp
->values
[i
];
545 return vdp
->n_values
* size
;
549 /*******************************************************************
552 * Dump a byte stream into the assembly code.
554 static void DumpBytes( const unsigned char *data
, int len
,
555 const char *section
, const char *label_start
)
558 if (section
) printf( "\t%s\n", section
);
559 if (label_start
) printf( "%s:\n", label_start
);
560 for (i
= 0; i
< len
; i
++)
562 if (!(i
& 0x0f)) printf( "\t.byte " );
563 printf( "%d", *data
++ );
564 if (i
< len
- 1) printf( "%c", ((i
& 0x0f) != 0x0f) ? ',' : '\n' );
570 /*******************************************************************
573 * Build the in-memory representation of a 16-bit NE module, and dump it
574 * as a byte stream into the assembly code.
576 static int BuildModule16( int max_code_offset
, int max_data_offset
)
582 SEGTABLEENTRY
*pSegment
;
589 * OFSTRUCT File information
590 * SEGTABLEENTRY Segment 1 (code)
591 * SEGTABLEENTRY Segment 2 (data)
592 * WORD[2] Resource table (empty)
593 * BYTE[2] Imported names (empty)
594 * BYTE[n] Resident names table
595 * BYTE[n] Entry table
598 buffer
= xmalloc( 0x10000 );
600 pModule
= (NE_MODULE
*)buffer
;
601 pModule
->magic
= NE_SIGNATURE
;
604 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_BUILTIN
| NE_FFLAGS_LIBMODULE
;
606 pModule
->heap_size
= HeapSize
;
607 pModule
->stack_size
= 0;
612 pModule
->seg_count
= 2;
613 pModule
->modref_count
= 0;
614 pModule
->nrname_size
= 0;
615 pModule
->modref_table
= 0;
616 pModule
->nrname_fpos
= 0;
617 pModule
->moveable_entries
= 0;
618 pModule
->alignment
= 0;
619 pModule
->truetype
= 0;
620 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
621 pModule
->misc_flags
= 0;
622 pModule
->dlls_to_init
= 0;
623 pModule
->nrname_handle
= 0;
624 pModule
->min_swap_area
= 0;
625 pModule
->expected_version
= 0x030a;
626 pModule
->pe_module
= NULL
;
628 pModule
->self_loading_sel
= 0;
630 /* File information */
632 pFileInfo
= (OFSTRUCT
*)(pModule
+ 1);
633 pModule
->fileinfo
= (int)pFileInfo
- (int)pModule
;
634 memset( pFileInfo
, 0, sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
) );
635 pFileInfo
->cBytes
= sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
)
636 + strlen(DLLName
) + 4;
637 sprintf( pFileInfo
->szPathName
, "%s.DLL", DLLName
);
638 pstr
= (char *)pFileInfo
+ pFileInfo
->cBytes
+ 1;
642 pSegment
= (SEGTABLEENTRY
*)pstr
;
643 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
644 pSegment
->filepos
= 0;
645 pSegment
->size
= max_code_offset
;
647 pSegment
->minsize
= max_code_offset
;
648 pSegment
->selector
= 0;
651 pModule
->dgroup_entry
= (int)pSegment
- (int)pModule
;
652 pSegment
->filepos
= 0;
653 pSegment
->size
= max_data_offset
;
654 pSegment
->flags
= NE_SEGFLAGS_DATA
;
655 pSegment
->minsize
= max_data_offset
;
656 pSegment
->selector
= 0;
661 pword
= (WORD
*)pSegment
;
662 pModule
->res_table
= (int)pword
- (int)pModule
;
666 /* Imported names table */
668 pstr
= (char *)pword
;
669 pModule
->import_table
= (int)pstr
- (int)pModule
;
673 /* Resident names table */
675 pModule
->name_table
= (int)pstr
- (int)pModule
;
676 /* First entry is module name */
677 *pstr
= strlen(DLLName
);
678 strcpy( pstr
+ 1, DLLName
);
681 pstr
+= sizeof(WORD
);
682 /* Store all ordinals */
683 odp
= OrdinalDefinitions
+ 1;
684 for (i
= 1; i
<= Limit
; i
++, odp
++)
686 if (!odp
->export_name
[0]) continue;
687 *pstr
= strlen( odp
->export_name
);
688 strcpy( pstr
+ 1, odp
->export_name
);
689 strupper( pstr
+ 1 );
692 pstr
+= sizeof(WORD
);
698 pModule
->entry_table
= (int)pstr
- (int)pModule
;
700 odp
= OrdinalDefinitions
+ 1;
701 for (i
= 1; i
<= Limit
; i
++, odp
++)
708 selector
= 0; /* Invalid selector */
716 selector
= 1; /* Code selector */
722 selector
= 2; /* Data selector */
726 selector
= 0xfe; /* Constant selector */
730 /* create a new bundle if necessary */
731 if (!bundle
|| (bundle
[0] >= 254) || (bundle
[1] != selector
))
735 bundle
[1] = selector
;
743 *(WORD
*)pstr
= odp
->offset
;
744 pstr
+= sizeof(WORD
);
749 /* Dump the module content */
751 DumpBytes( (char *)pModule
, (int)pstr
- (int)pModule
,
752 ".data", "Module_Start" );
753 return (int)pstr
- (int)pModule
;
757 /*******************************************************************
760 * Build the in-memory representation of a 32-bit pseudo-NE module, and dump it
761 * as a byte stream into the assembly code.
763 static int BuildModule32(void)
773 * OFSTRUCT File information
774 * SEGTABLEENTRY Segment table (empty)
775 * WORD[2] Resource table (empty)
776 * BYTE[2] Imported names (empty)
777 * BYTE[n] Resident names table (1 entry)
778 * BYTE[n] Entry table (empty)
781 buffer
= xmalloc( 0x10000 );
783 pModule
= (NE_MODULE
*)buffer
;
784 pModule
->magic
= NE_SIGNATURE
;
787 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_BUILTIN
|
788 NE_FFLAGS_LIBMODULE
| NE_FFLAGS_WIN32
;
790 pModule
->heap_size
= HeapSize
;
791 pModule
->stack_size
= 0;
796 pModule
->seg_count
= 0;
797 pModule
->modref_count
= 0;
798 pModule
->nrname_size
= 0;
799 pModule
->modref_table
= 0;
800 pModule
->nrname_fpos
= 0;
801 pModule
->moveable_entries
= 0;
802 pModule
->alignment
= 0;
803 pModule
->truetype
= 0;
804 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
805 pModule
->misc_flags
= 0;
806 pModule
->dlls_to_init
= 0;
807 pModule
->nrname_handle
= 0;
808 pModule
->min_swap_area
= 0;
809 pModule
->expected_version
= 0x030a;
810 pModule
->pe_module
= NULL
;
812 pModule
->self_loading_sel
= 0;
814 /* File information */
816 pFileInfo
= (OFSTRUCT
*)(pModule
+ 1);
817 pModule
->fileinfo
= (int)pFileInfo
- (int)pModule
;
818 memset( pFileInfo
, 0, sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
) );
819 pFileInfo
->cBytes
= sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
)
820 + strlen(DLLName
) + 4;
821 sprintf( pFileInfo
->szPathName
, "%s.DLL", DLLName
);
822 pstr
= (char *)pFileInfo
+ pFileInfo
->cBytes
+ 1;
826 pModule
->seg_table
= (int)pstr
- (int)pModule
;
830 pword
= (WORD
*)pstr
;
831 pModule
->res_table
= (int)pword
- (int)pModule
;
835 /* Imported names table */
837 pstr
= (char *)pword
;
838 pModule
->import_table
= (int)pstr
- (int)pModule
;
842 /* Resident names table */
844 pModule
->name_table
= (int)pstr
- (int)pModule
;
845 /* First entry is module name */
846 *pstr
= strlen(DLLName
);
847 strcpy( pstr
+ 1, DLLName
);
850 pstr
+= sizeof(WORD
);
855 pModule
->entry_table
= (int)pstr
- (int)pModule
;
858 /* Dump the module content */
860 DumpBytes( (char *)pModule
, (int)pstr
- (int)pModule
,
861 ".data", "Module_Start" );
862 return (int)pstr
- (int)pModule
;
866 /*******************************************************************
869 * Build a Win32 assembly file from a spec file.
871 static void BuildSpec32Files(void)
878 printf( "/* File generated automatically; do not edit! */\n" );
879 printf( "\t.text\n" );
880 printf( "\t.align 4\n" );
881 printf( "Code_Start:\n\n" );
883 odp
= OrdinalDefinitions
;
884 for (i
= 0; i
<= Limit
; i
++, odp
++)
886 fdp
= odp
->additional_data
;
887 rdp
= odp
->additional_data
;
892 printf( "/* %s.%d */\n", DLLName
, i
);
893 printf( "\t.align 4\n" );
894 printf( "%s_%d:\n", DLLName
, i
);
895 printf( "\tpushl %%ebp\n" );
896 printf( "\tpushl $Name_%d\n", i
);
897 printf( "\tpushl $" PREFIX
"%s\n", STUB_CALLBACK
);
898 printf( "\tjmp " PREFIX
"CallFrom32_0\n" );
903 printf( "/* %s.%d (%s) */\n",
904 DLLName
, i
, odp
->export_name
);
905 printf( "\t.align 4\n" );
906 printf( "%s_%d:\n", DLLName
, i
);
907 printf( "\tpushl %%ebp\n" );
908 printf( "\tpushl $Name_%d\n", i
);
909 printf( "\tpushl $" PREFIX
"%s\n", fdp
->internal_name
);
910 printf( "\tjmp " PREFIX
"CallFrom32_%d\n", strlen(fdp
->arg_types
));
914 printf( "/* %s.%d (%s) */\n",
915 DLLName
, i
, odp
->export_name
);
916 printf( "\t.align 4\n" );
917 printf( "%s_%d:\n", DLLName
, i
);
918 printf( "\tmovl $%d,%%eax\n", ERROR_CALL_NOT_IMPLEMENTED
);
919 printf( "\tmovl %%eax," PREFIX
"WIN32_LastError\n" );
920 printf( "\tmovl $%d,%%eax\n", rdp
->ret_value
);
921 if (rdp
->arg_size
) printf( "\tret $%d\n", rdp
->arg_size
);
922 else printf( "\tret\n" );
926 fprintf(stderr
,"build: function type %d not available for Win32\n",
932 module_size
= BuildModule32();
934 /* Output the DLL functions table */
936 printf( "\t.text\n" );
937 printf( "\t.align 4\n" );
938 printf( "Functions:\n" );
939 odp
= OrdinalDefinitions
;
940 for (i
= 0; i
<= Limit
; i
++, odp
++) printf("\t.long %s_%d\n", DLLName
, i
);
942 /* Output the DLL names table */
944 printf( "FuncNames:\n" );
945 odp
= OrdinalDefinitions
;
946 for (i
= 0; i
<= Limit
; i
++, odp
++)
948 if (odp
->type
== TYPE_INVALID
) printf( "\t.long 0\n" );
949 else printf( "\t.long Name_%d\n", i
);
952 /* Output the DLL names */
954 for (i
= 0, odp
= OrdinalDefinitions
; i
<= Limit
; i
++, odp
++)
956 printf( "Name_%d:\t", i
);
957 if (odp
->type
== TYPE_INVALID
)
958 printf( ".ascii \"%s.%d\\0\"\n", DLLName
, i
);
960 printf( ".ascii \"%s\\0\"\n", odp
->export_name
);
963 /* Output the DLL descriptor */
965 printf( "DLLName:\t.ascii \"%s\\0\"\n", DLLName
);
966 printf( "\t.align 4\n" );
967 printf( "\t.globl " PREFIX
"%s_Descriptor\n", DLLName
);
968 printf( PREFIX
"%s_Descriptor:\n", DLLName
);
969 printf( "\t.long DLLName\n" ); /* Name */
970 printf( "\t.long Module_Start\n" ); /* Module start */
971 printf( "\t.long %d\n", module_size
); /* Module size */
972 printf( "\t.long %d\n", Base
); /* Base */
973 printf( "\t.long %d\n", Limit
); /* Limit */
974 printf( "\t.long Functions\n" ); /* Functions */
975 printf( "\t.long FuncNames\n" ); /* Function names */
979 /*******************************************************************
982 * Build a Win16 assembly file from a spec file.
984 static void BuildSpec16Files(void)
990 int code_offset
, data_offset
, module_size
;
993 data
= (unsigned char *)xmalloc( 0x10000 );
994 memset( data
, 0, 16 );
997 printf( "/* File generated automatically; do not edit! */\n" );
998 printf( "\t.text\n" );
999 printf( "Code_Start:\n" );
1002 odp
= OrdinalDefinitions
;
1003 for (i
= 0; i
<= Limit
; i
++, odp
++)
1005 fdp
= odp
->additional_data
;
1006 rdp
= odp
->additional_data
;
1011 odp
->offset
= 0xffff;
1015 odp
->offset
= (int)odp
->additional_data
& 0xffff;
1019 printf( "/* %s.%d */\n", DLLName
, i
);
1020 odp
->offset
= data_offset
;
1021 data_offset
+= StoreVariableCode( data
, 1, odp
);
1025 printf( "/* %s.%d */\n", DLLName
, i
);
1026 odp
->offset
= data_offset
;
1027 data_offset
+= StoreVariableCode( data
, 2, odp
);
1031 printf( "/* %s.%d */\n", DLLName
, i
);
1032 odp
->offset
= data_offset
;
1033 data_offset
+= StoreVariableCode( data
, 4, odp
);
1037 printf( "/* %s.%d */\n", DLLName
, i
);
1038 printf( "\tmovw $%d,%%ax\n", rdp
->ret_value
& 0xffff );
1039 printf( "\tmovw $%d,%%dx\n", (rdp
->ret_value
>> 16) & 0xffff);
1040 printf( "\t.byte 0x66\n");
1041 if (rdp
->arg_size
!= 0)
1042 printf( "\tlret $%d\n\n", rdp
->arg_size
);
1045 printf( "\tlret\n");
1047 printf( "\tnop\n\n");
1049 odp
->offset
= code_offset
;
1050 code_offset
+= 12; /* Assembly code is 12 bytes long */
1055 case TYPE_PASCAL_16
:
1057 printf( "/* %s.%d */\n", DLLName
, i
);
1058 printf( "\tpushw %%bp\n" );
1059 printf( "\tpushl $" PREFIX
"%s\n", fdp
->internal_name
);
1060 /* FreeBSD does not understand lcall, so do it the hard way */
1061 printf( "\t.byte 0x9a /*lcall*/\n" );
1062 printf( "\t.long " PREFIX
"CallFrom16_%s_%s\n",
1063 (odp
->type
== TYPE_REGISTER
) ? "regs" :
1064 (odp
->type
== TYPE_PASCAL
) ? "long" : "word",
1066 printf( "\t.word 0x%04x\n", WINE_CODE_SELECTOR
);
1067 printf( "\tnop\n" );
1068 printf( "\tnop\n\n" );
1069 odp
->offset
= code_offset
;
1070 code_offset
+= 16; /* Assembly code is 16 bytes long */
1074 fprintf(stderr
,"build: function type %d not available for Win16\n",
1080 if (!code_offset
) /* Make sure the code segment is not empty */
1082 printf( "\t.byte 0\n" );
1086 /* Output data segment */
1088 DumpBytes( data
, data_offset
, NULL
, "Data_Start" );
1090 /* Build the module */
1092 module_size
= BuildModule16( code_offset
, data_offset
);
1094 /* Output the DLL descriptor */
1096 printf( "\t.text\n" );
1097 printf( "DLLName:\t.ascii \"%s\\0\"\n", DLLName
);
1098 printf( "\t.align 4\n" );
1099 printf( "\t.globl " PREFIX
"%s_Descriptor\n", DLLName
);
1100 printf( PREFIX
"%s_Descriptor:\n", DLLName
);
1101 printf( "\t.long DLLName\n" ); /* Name */
1102 printf( "\t.long Module_Start\n" ); /* Module start */
1103 printf( "\t.long %d\n", module_size
); /* Module size */
1104 printf( "\t.long Code_Start\n" ); /* Code start */
1105 printf( "\t.long Data_Start\n" ); /* Data start */
1109 /*******************************************************************
1112 * Build an assembly file from a spec file.
1114 static void BuildSpecFiles( char *specname
)
1116 SpecFp
= fopen( specname
, "r");
1119 fprintf(stderr
, "Could not open specification file, '%s'\n", specname
);
1127 fprintf( stderr
, "%s: Missing 'type' declaration\n", specname
);
1139 /*******************************************************************
1140 * BuildCall32LargeStack
1142 * Build the function used to switch to the original 32-bit stack
1143 * before calling a 32-bit function from 32-bit code. This is used for
1144 * functions that need a large stack, like X bitmaps functions.
1146 * The generated function has the following prototype:
1147 * int CallTo32_LargeStack( int (*func)(), int nbargs, ... )
1158 static void BuildCall32LargeStack(void)
1160 /* Function header */
1162 printf( "/**********\n" );
1163 printf( " * " PREFIX
"CallTo32_LargeStack\n" );
1164 printf( " **********/\n" );
1165 printf( "\t.align 4\n" );
1166 printf( "\t.globl " PREFIX
"CallTo32_LargeStack\n\n" );
1167 printf( PREFIX
"CallTo32_LargeStack:\n" );
1171 printf( "\tpushl %%ebp\n" );
1172 printf( "\tmovl %%esp,%%ebp\n" );
1174 /* Save registers */
1176 printf( "\tpushl %%ecx\n" );
1177 printf( "\tpushl %%esi\n" );
1178 printf( "\tpushl %%edi\n" );
1180 /* Retrieve the original 32-bit stack pointer and switch to it if any */
1182 printf( "\tmovl " PREFIX
"IF1632_Original32_esp, %%eax\n" );
1183 printf( "\torl %%eax,%%eax\n" );
1184 printf( "\tje no_orig_esp\n" );
1185 printf( "\tmovl %%eax,%%esp\n" );
1186 printf( "no_orig_esp:\n" );
1188 /* Transfer the arguments */
1190 printf( "\tmovl 12(%%ebp),%%ecx\n" );
1191 printf( "\torl %%ecx,%%ecx\n" );
1192 printf( "\tje no_args\n" );
1193 printf( "\tleal 16(%%ebp),%%esi\n" );
1194 printf( "\tshll $2,%%ecx\n" );
1195 printf( "\tsubl %%ecx,%%esp\n" );
1196 printf( "\tmovl %%esp,%%edi\n" );
1197 printf( "\tshrl $2,%%ecx\n" );
1198 printf( "\tcld\n" );
1199 printf( "\trep; movsl\n" );
1200 printf( "no_args:\n" );
1202 /* Call the function */
1204 printf( "\tcall 8(%%ebp)\n" );
1206 /* Switch back to the normal stack */
1208 printf( "\tleal -12(%%ebp),%%esp\n" );
1210 /* Restore registers and return */
1212 printf( "\tpopl %%edi\n" );
1213 printf( "\tpopl %%esi\n" );
1214 printf( "\tpopl %%ecx\n" );
1215 printf( "\tpopl %%ebp\n" );
1216 printf( "\tret\n" );
1220 /*******************************************************************
1221 * TransferArgs16To32
1223 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1224 * The 16-bit stack layout is:
1232 static int TransferArgs16To32( char *args
)
1234 int i
, pos16
, pos32
;
1236 /* Save ebx first */
1238 printf( "\tpushl %%ebx\n" );
1240 /* Get the 32-bit stack pointer */
1242 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebx\n" );
1244 /* Copy the arguments */
1246 pos16
= 6; /* skip bp and return address */
1249 for (i
= strlen(args
); i
> 0; i
--)
1254 case 'w': /* word */
1255 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1256 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1260 case 's': /* s_word */
1261 printf( "\tmovswl %d(%%ebp),%%eax\n", pos16
);
1262 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1266 case 'l': /* long */
1267 printf( "\tmovl %d(%%ebp),%%eax\n", pos16
);
1268 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1273 /* Get the selector */
1274 printf( "\tmovw %d(%%ebp),%%ax\n", pos16
+ 2 );
1275 /* Get the selector base */
1276 printf( "\tandl $0xfff8,%%eax\n" );
1277 printf( "\tmovl " PREFIX
"ldt_copy(%%eax),%%eax\n" );
1278 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1279 /* Add the offset */
1280 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1281 printf( "\taddl %%eax,%d(%%ebx)\n", pos32
);
1286 fprintf( stderr
, "Unknown arg type '%c'\n", args
[i
-1] );
1292 printf( "\tpopl %%ebx\n" );
1294 return pos16
- 6; /* Return the size of the 16-bit args */
1298 /*******************************************************************
1301 * Build the context structure on the 32-bit stack.
1302 * The only valid registers in the context structure are:
1303 * eax, ebx, ecx, edx, esi, edi, ds, es, (some of the) flags
1305 static void BuildContext(void)
1307 /* Save ebx first */
1309 printf( "\tpushl %%ebx\n" );
1311 /* Get the 32-bit stack pointer */
1313 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebx\n" );
1315 /* Store the registers */
1317 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(EBX
) ); /* Get ebx from stack*/
1318 printf( "\tmovl %%eax,%d(%%ebx)\n", CONTEXTOFFSET(EAX
) );
1319 printf( "\tmovl %%ecx,%d(%%ebx)\n", CONTEXTOFFSET(ECX
) );
1320 printf( "\tmovl %%edx,%d(%%ebx)\n", CONTEXTOFFSET(EDX
) );
1321 printf( "\tmovl %%esi,%d(%%ebx)\n", CONTEXTOFFSET(ESI
) );
1322 printf( "\tmovl %%edi,%d(%%ebx)\n", CONTEXTOFFSET(EDI
) );
1323 printf( "\tmovw -10(%%ebp),%%ax\n" ); /* Get saved ds from stack */
1324 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(DS
) );
1325 printf( "\tmovw -6(%%ebp),%%ax\n" ); /* Get saved es from stack */
1326 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(ES
) );
1327 printf( "\tpushfl\n" );
1328 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(EFL
) );
1332 /*******************************************************************
1335 * Restore the registers from the context structure
1337 static void RestoreContext(void)
1339 /* Get the 32-bit stack pointer */
1341 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebx\n" );
1343 /* Restore the registers */
1345 printf( "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(ECX
) );
1346 printf( "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(EDX
) );
1347 printf( "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(ESI
) );
1348 printf( "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(EDI
) );
1349 printf( "\tpopl %%eax\n" ); /* Remove old ds and ip from stack */
1350 printf( "\tpopl %%eax\n" ); /* Remove old cs and es from stack */
1351 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(DS
) ); /* Push new ds */
1352 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(ES
) ); /* Push new es */
1353 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFL
) );
1354 printf( "\tpopfl\n" );
1355 printf( "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(EAX
) );
1356 printf( "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(EBX
) );
1360 /*******************************************************************
1361 * BuildCallFrom16Func
1363 * Build a 16-bit-to-Wine callback function. The syntax of the function
1364 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1365 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1366 * 'l'=long, 'p'=pointer).
1367 * For register functions, the arguments are ignored, but they are still
1368 * removed from the stack upon return.
1370 * Stack layout upon entry to the callback function:
1372 * (sp+18) word first 16-bit arg
1376 * (sp+8) long 32-bit entry point
1377 * (sp+6) word high word of cs (always 0, used to store es)
1378 * (sp+4) word low word of cs of 16-bit entry point
1379 * (sp+2) word high word of ip (always 0, used to store ds)
1380 * (sp) word low word of ip of 16-bit entry point
1383 static void BuildCallFrom16Func( char *profile
)
1388 char *args
= profile
+ 5;
1390 /* Parse function type */
1392 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1393 else if (!strncmp( "regs_", profile
, 5 )) reg_func
= 1;
1394 else if (strncmp( "long_", profile
, 5 ))
1396 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1400 /* Function header */
1402 printf( "/**********\n" );
1403 printf( " * " PREFIX
"CallFrom16_%s\n", profile
);
1404 printf( " **********/\n" );
1405 printf( "\t.align 4\n" );
1406 printf( "\t.globl " PREFIX
"CallFrom16_%s\n\n", profile
);
1407 printf( PREFIX
"CallFrom16_%s:\n", profile
);
1409 /* Setup bp to point to its copy on the stack */
1411 printf( "\tmovzwl %%sp,%%ebp\n" );
1412 printf( "\taddw $12,%%bp\n" );
1414 /* Save 16-bit ds and es */
1416 /* Stupid FreeBSD assembler doesn't know these either */
1417 /* printf( "\tmovw %%ds,-10(%%ebp)\n" ); */
1418 printf( "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1419 /* printf( "\tmovw %%es,-6(%%ebp)\n" ); */
1420 printf( "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1422 /* Restore 32-bit ds and es */
1424 printf( "\tpushl $0x%04x%04x\n", WINE_DATA_SELECTOR
, WINE_DATA_SELECTOR
);
1425 printf( "\tpopw %%ds\n" );
1426 printf( "\tpopw %%es\n" );
1429 /* Save the 16-bit stack */
1431 printf( "\tpushw " PREFIX
"IF1632_Saved16_sp\n" );
1432 printf( "\tpushw " PREFIX
"IF1632_Saved16_ss\n" );
1434 printf("\tdata16\n");
1436 printf( "\tmovw %%ss," PREFIX
"IF1632_Saved16_ss\n" );
1437 printf( "\tmovw %%sp," PREFIX
"IF1632_Saved16_sp\n" );
1439 /* Transfer the arguments */
1441 if (reg_func
) BuildContext();
1442 else if (*args
) argsize
= TransferArgs16To32( args
);
1444 /* Get the address of the API function */
1446 printf( "\tmovl -4(%%ebp),%%eax\n" );
1448 /* If necessary, save %edx over the API function address */
1450 if (!reg_func
&& short_ret
)
1451 printf( "\tmovl %%edx,-4(%%ebp)\n" );
1453 /* Switch to the 32-bit stack */
1455 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebp\n" );
1456 printf( "\tpushw %%ds\n" );
1457 printf( "\tpopw %%ss\n" );
1458 printf( "\tleal -%d(%%ebp),%%esp\n",
1459 reg_func
? sizeof(struct sigcontext_struct
) : 4 * strlen(args
) );
1461 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1463 printf( "\taddl $24,%%ebp\n" );
1465 /* Print the debug information before the call */
1469 printf( "\tpushl %%eax\n" );
1470 printf( "\tpushl $Profile_%s\n", profile
);
1471 printf( "\tpushl $%d\n", reg_func
? 2 : (short_ret
? 1 : 0) );
1472 printf( "\tcall " PREFIX
"RELAY_DebugCallFrom16\n" );
1473 printf( "\tpopl %%eax\n" );
1474 printf( "\tpopl %%eax\n" );
1475 printf( "\tpopl %%eax\n" );
1478 /* Call the entry point */
1480 printf( "\tcall %%eax\n" );
1482 /* Print the debug information after the call */
1486 printf( "\tpushl %%eax\n" );
1487 printf( "\tpushl $%d\n", reg_func
? 2 : (short_ret
? 1 : 0) );
1488 printf( "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret\n" );
1489 printf( "\tpopl %%eax\n" );
1490 printf( "\tpopl %%eax\n" );
1493 /* Restore the 16-bit stack */
1496 printf( "\tdata16\n");
1498 printf( "\tmovw " PREFIX
"IF1632_Saved16_ss,%%ss\n" );
1499 printf( "\tmovw " PREFIX
"IF1632_Saved16_sp,%%sp\n" );
1501 printf( "\tdata16\n");
1503 printf( "\tpopw " PREFIX
"IF1632_Saved16_ss\n" );
1505 printf( "\tdata16\n");
1507 printf( "\tpopw " PREFIX
"IF1632_Saved16_sp\n" );
1511 /* Restore registers from the context structure */
1514 /* Calc the arguments size */
1528 fprintf( stderr
, "Unknown arg type '%c'\n", *args
);
1533 /* Restore ds and es */
1534 printf( "\tpopw %%es\n" );
1535 printf( "\tpopw %%ds\n" );
1537 /* Remove the entry point from the stack */
1538 /* (we don't use add to avoid modifying the carry flag) */
1539 printf( "\tpopl %%ebp\n" );
1543 /* Restore ds and es */
1544 printf( "\tpopw %%bp\n" ); /* Remove ip */
1545 printf( "\tpopl %%ebp\n" ); /* Remove ds and cs */
1546 printf( "\tmovw %%bp,%%ds\n" ); /* Restore ds */
1547 printf( "\tpopw %%es\n" ); /* Restore es */
1549 if (short_ret
) printf( "\tpopl %%edx\n" ); /* Restore edx */
1552 /* Get the return value into dx:ax */
1553 printf( "\tpushl %%eax\n" );
1554 printf( "\tpopw %%ax\n" );
1555 printf( "\tpopw %%dx\n" );
1556 /* Remove API entry point */
1557 printf( "\taddl $4,%%esp\n" );
1563 printf( "\tpopw %%bp\n" );
1565 /* Remove the arguments and return */
1569 printf( "\t.byte 0x66\n" );
1570 printf( "\tlret $%d\n", argsize
);
1574 printf( "\t.byte 0x66\n" );
1575 printf( "\tlret\n" );
1580 /*******************************************************************
1583 * Build a Wine-to-16-bit callback function.
1585 * Stack frame of the callback function:
1589 * (ebp+16) 16-bit ds
1590 * (ebp+12) func to call
1591 * (ebp+8) code selector
1592 * (ebp+4) return address
1593 * (ebp) previous ebp
1595 * Prototypes for the CallTo16 functions:
1596 * extern WORD CallTo16_word_xxx( FARPROC func, WORD ds, args... );
1597 * extern LONG CallTo16_long_xxx( FARPROC func, WORD ds, args... );
1598 * extern void CallTo16_regs_( FARPROC func, WORD ds, WORD es, WORD bp,
1599 * WORD ax, WORD bx, WORD cx, WORD dx,
1600 * WORD si, WORD di );
1602 static void BuildCallTo16Func( char *profile
)
1606 char *args
= profile
+ 5;
1608 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1609 else if (!strncmp( "regs_", profile
, 5 )) reg_func
= short_ret
= 1;
1610 else if (strncmp( "long_", profile
, 5 ))
1612 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1616 /* Function header */
1618 printf( "/**********\n" );
1619 printf( " * " PREFIX
"CallTo16_%s\n", profile
);
1620 printf( " **********/\n" );
1621 printf( "\t.align 4\n" );
1622 printf( "\t.globl " PREFIX
"CallTo16_%s\n\n", profile
);
1623 printf( PREFIX
"CallTo16_%s:\n", profile
);
1625 /* Push code selector before return address to simulate a lcall */
1627 printf( "\tpopl %%eax\n" );
1628 printf( "\tpushl $0x%04x\n", WINE_CODE_SELECTOR
);
1629 printf( "\tpushl %%eax\n" );
1633 printf( "\tpushl %%ebp\n" );
1634 printf( "\tmovl %%esp,%%ebp\n" );
1636 /* Save the 32-bit registers */
1638 printf( "\tpushl %%ebx\n" );
1639 printf( "\tpushl %%ecx\n" );
1640 printf( "\tpushl %%edx\n" );
1641 printf( "\tpushl %%esi\n" );
1642 printf( "\tpushl %%edi\n" );
1644 /* Save the 32-bit stack */
1646 printf( "\tpushl " PREFIX
"IF1632_Saved32_esp\n" );
1647 printf( "\tmovl %%esp," PREFIX
"IF1632_Saved32_esp\n" );
1648 printf( "\tmovl %%ebp,%%ebx\n" );
1650 /* Print debugging info */
1654 /* Push the address of the first argument */
1655 printf( "\tmovl %%ebx,%%eax\n" );
1656 printf( "\taddl $12,%%eax\n" );
1657 printf( "\tpushl $%d\n", reg_func
? 8 : strlen(args
) );
1658 printf( "\tpushl %%eax\n" );
1659 printf( "\tcall " PREFIX
"RELAY_DebugCallTo16\n" );
1660 printf( "\tpopl %%eax\n" );
1661 printf( "\tpopl %%eax\n" );
1664 /* Switch to the 16-bit stack */
1667 printf("\tdata16\n");
1669 printf( "\tmovw " PREFIX
"IF1632_Saved16_ss,%%ss\n" );
1670 printf( "\tmovw " PREFIX
"IF1632_Saved16_sp,%%sp\n" );
1672 /* Transfer the arguments */
1676 /* Get the registers. ebx is handled later on. */
1677 printf( "\tpushw 20(%%ebx)\n" );
1678 printf( "\tpopw %%es\n" );
1679 printf( "\tmovl 24(%%ebx),%%ebp\n" );
1680 printf( "\tmovl 28(%%ebx),%%eax\n" );
1681 printf( "\tmovl 36(%%ebx),%%ecx\n" );
1682 printf( "\tmovl 40(%%ebx),%%edx\n" );
1683 printf( "\tmovl 44(%%ebx),%%esi\n" );
1684 printf( "\tmovl 48(%%ebx),%%edi\n" );
1686 else /* not a register function */
1688 int pos
= 20; /* first argument position */
1690 /* Make %bp point to the previous stackframe (built by CallFrom16) */
1691 printf( "\tmovw %%sp,%%bp\n" );
1692 printf( "\taddw $16,%%bp\n" );
1698 case 'w': /* word */
1699 printf( "\tpushw %d(%%ebx)\n", pos
);
1701 case 'l': /* long */
1702 printf( "\tpushl %d(%%ebx)\n", pos
);
1709 /* Push the return address */
1711 printf( "\tpushl " PREFIX
"CALLTO16_RetAddr_%s\n",
1712 short_ret
? "word" : "long" );
1714 /* Push the called routine address */
1716 printf( "\tpushl 12(%%ebx)\n" );
1718 /* Get the 16-bit ds */
1722 printf( "\tpushw 16(%%ebx)\n" );
1723 printf( "\tmovl 32(%%ebx),%%ebx\n" ); /*Get ebx from the 32-bit stack*/
1724 printf( "\tpopw %%ds\n" );
1728 /* Set ax equal to ds for window procedures */
1729 printf( "\tmovw 16(%%ebx),%%ax\n" );
1731 printf( "\tdata16\n");
1733 printf( "\tmovw %%ax,%%ds\n" );
1736 /* Jump to the called routine */
1738 printf( "\t.byte 0x66\n" );
1739 printf( "\tlret\n" );
1743 /*******************************************************************
1746 * Build the return code for 16-bit callbacks
1748 static void BuildRet16Func()
1750 printf( "\t.globl " PREFIX
"CALLTO16_Ret_word\n" );
1751 printf( "\t.globl " PREFIX
"CALLTO16_Ret_long\n" );
1753 /* Put return value into eax */
1755 printf( PREFIX
"CALLTO16_Ret_long:\n" );
1756 printf( "\tpushw %%dx\n" );
1757 printf( "\tpushw %%ax\n" );
1758 printf( "\tpopl %%eax\n" );
1759 printf( PREFIX
"CALLTO16_Ret_word:\n" );
1761 /* Restore 32-bit segment registers */
1763 printf( "\tmovw $0x%04x,%%bx\n", WINE_DATA_SELECTOR
);
1765 printf( "\tdata16\n");
1767 printf( "\tmovw %%bx,%%ds\n" );
1769 printf( "\tdata16\n");
1771 printf( "\tmovw %%bx,%%es\n" );
1773 printf( "\tdata16\n");
1775 printf( "\tmovw %%bx,%%ss\n" );
1777 /* Restore the 32-bit stack */
1779 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%esp\n" );
1780 printf( "\tpopl " PREFIX
"IF1632_Saved32_esp\n" );
1782 /* Restore the 32-bit registers */
1784 printf( "\tpopl %%edi\n" );
1785 printf( "\tpopl %%esi\n" );
1786 printf( "\tpopl %%edx\n" );
1787 printf( "\tpopl %%ecx\n" );
1788 printf( "\tpopl %%ebx\n" );
1790 /* Return to caller */
1792 printf( "\tpopl %%ebp\n" );
1793 printf( "\tlret\n" );
1795 /* Declare the return address variables */
1797 printf( "\t.data\n" );
1798 printf( "\t.globl " PREFIX
"CALLTO16_RetAddr_word\n" );
1799 printf( "\t.globl " PREFIX
"CALLTO16_RetAddr_long\n" );
1800 printf( PREFIX
"CALLTO16_RetAddr_word:\t.long 0\n" );
1801 printf( PREFIX
"CALLTO16_RetAddr_long:\t.long 0\n" );
1802 printf( "\t.text\n" );
1806 /*******************************************************************
1807 * BuildCallFrom32Func
1809 * Build a 32-bit-to-Wine call-back function.
1810 * 'args' is the number of dword arguments.
1819 * (ebp-8) entry point
1821 static void BuildCallFrom32Func( int args
)
1823 /* Function header */
1825 printf( "/**********\n" );
1826 printf( " * " PREFIX
"CallFrom32_%d\n", args
);
1827 printf( " **********/\n" );
1828 printf( "\t.align 4\n" );
1829 printf( "\t.globl " PREFIX
"CallFrom32_%d\n\n", args
);
1830 printf( PREFIX
"CallFrom32_%d:\n", args
);
1834 printf( "\tleal 8(%%esp),%%ebp\n" );
1836 /* Print the debugging info */
1840 printf( "\tpushl $%d\n", args
);
1841 printf( "\tcall " PREFIX
"RELAY_DebugCallFrom32\n" );
1842 printf( "\tadd $4, %%esp\n" );
1845 /* Transfer the arguments */
1850 for (i
= args
; i
> 0; i
--) printf( "\tpushl %d(%%ebp)\n", 4 * i
+ 4 );
1854 /* Push the address of the arguments. The called function will */
1855 /* ignore this if it really takes no arguments. */
1856 printf( "\tleal 8(%%ebp),%%eax\n" );
1857 printf( "\tpushl %%eax\n" );
1860 /* Call the function */
1862 printf( "\tcall -8(%%ebp)\n" );
1864 /* Print the debugging info */
1868 printf( "\tadd $%d,%%esp\n", args
? (args
* 4) : 4 );
1869 printf( "\tpushl %%eax\n" );
1870 printf( "\tcall " PREFIX
"RELAY_DebugCallFrom32Ret\n" );
1871 printf( "\tpopl %%eax\n" );
1874 printf( "\tmovl %%ebp,%%esp\n" );
1875 printf( "\tpopl %%ebp\n" );
1877 /* Return, removing arguments */
1879 if (args
) printf( "\tret $%d\n", args
* 4 );
1880 else printf( "\tret\n" );
1884 /*******************************************************************
1887 * Build a Wine-to-32-bit callback function.
1889 * Stack frame of the callback function:
1893 * (ebp+8) func to call
1894 * (ebp+4) return address
1895 * (ebp) previous ebp
1897 * Prototype for the CallTo32 functions:
1898 * extern LONG CallTo32_nn( FARPROC func, args... );
1900 static void BuildCallTo32Func( int args
)
1902 /* Function header */
1904 printf( "/**********\n" );
1905 printf( " * " PREFIX
"CallTo32_%d\n", args
);
1906 printf( " **********/\n" );
1907 printf( "\t.align 4\n" );
1908 printf( "\t.globl " PREFIX
"CallTo32_%d\n\n", args
);
1909 printf( PREFIX
"CallTo32_%d:\n", args
);
1913 printf( "\tpushl %%ebp\n" );
1914 printf( "\tmovl %%esp,%%ebp\n" );
1916 /* Transfer arguments */
1921 for (i
= args
; i
> 0; i
--) printf( "\tpushl %d(%%ebp)\n", 4 * i
+ 8 );
1924 /* Print the debugging output */
1928 printf( "\tpushl $%d\n", args
);
1929 printf( "\tpushl 8(%%ebp)\n" );
1930 printf( "\tcall " PREFIX
"RELAY_DebugCallTo32\n" );
1931 printf( "\taddl $8,%%esp\n" );
1934 /* Call the function */
1936 printf( "\tcall 8(%%ebp)\n" );
1938 /* Return to Wine */
1940 printf( "\tmovl %%ebp,%%esp\n" );
1941 printf( "\tpopl %%ebp\n" );
1942 printf( "\tret\n" );
1946 static void usage(void)
1948 fprintf(stderr
, "usage: build -spec SPECNAMES\n"
1949 " build -callfrom16 FUNCTION_PROFILES\n"
1950 " build -callto16 FUNCTION_PROFILES\n"
1951 " build -callfrom32 FUNCTION_PROFILES\n"
1952 " build -callto32 FUNCTION_PROFILES\n" );
1957 int main(int argc
, char **argv
)
1961 if (argc
<= 2) usage();
1963 if (!strcmp( argv
[1], "-spec" ))
1965 for (i
= 2; i
< argc
; i
++) BuildSpecFiles( argv
[i
] );
1967 else if (!strcmp( argv
[1], "-callfrom16" )) /* 16-bit-to-Wine callbacks */
1971 printf( "/* File generated automatically. Do not edit! */\n\n" );
1972 printf( "\t.text\n" );
1974 /* Build the 32-bit large stack callback */
1976 BuildCall32LargeStack();
1978 /* Build the callback functions */
1980 for (i
= 2; i
< argc
; i
++) BuildCallFrom16Func( argv
[i
] );
1982 /* Output the argument debugging strings */
1986 printf( "/* Argument strings */\n" );
1987 for (i
= 2; i
< argc
; i
++)
1989 printf( "Profile_%s:\n", argv
[i
] );
1990 printf( "\t.ascii \"%s\\0\"\n", argv
[i
] + 5 );
1994 else if (!strcmp( argv
[1], "-callto16" )) /* Wine-to-16-bit callbacks */
1998 printf( "/* File generated automatically. Do not edit! */\n\n" );
1999 printf( "\t.text\n" );
2000 printf( "\t.globl " PREFIX
"CALLTO16_Start\n" );
2001 printf( PREFIX
"CALLTO16_Start:\n" );
2003 /* Build the callback functions */
2005 for (i
= 2; i
< argc
; i
++) BuildCallTo16Func( argv
[i
] );
2007 /* Output the 16-bit return code */
2011 printf( "\t.globl " PREFIX
"CALLTO16_End\n" );
2012 printf( PREFIX
"CALLTO16_End:\n" );
2014 else if (!strcmp( argv
[1], "-callfrom32" )) /* 32-bit-to-Wine callbacks */
2018 printf( "/* File generated automatically. Do not edit! */\n\n" );
2019 printf( "\t.text\n" );
2021 /* Build the callback functions */
2023 for (i
= 2; i
< argc
; i
++) BuildCallFrom32Func( atoi(argv
[i
]) );
2025 else if (!strcmp( argv
[1], "-callto32" )) /* Wine-to-32-bit callbacks */
2029 printf( "/* File generated automatically. Do not edit! */\n\n" );
2030 printf( "\t.text\n" );
2032 /* Build the callback functions */
2034 for (i
= 2; i
< argc
; i
++) BuildCallTo32Func( atoi(argv
[i
]) );
2041 #endif /* WINELIB */