Better DropTarget.
[wine.git] / tools / build.c
blobd082097e3f733668ab8b34946e90232a1da864ad
1 /*
2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Martin von Loewis
4 * Copyright 1995, 1996, 1997 Alexandre Julliard
5 * Copyright 1997 Eric Youngdale
6 * Copyright 1999 Ulrich Weigand
7 */
9 #include <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <unistd.h>
16 #include "winbase.h"
17 #include "winnt.h"
18 #include "module.h"
19 #include "neexe.h"
20 #include "selectors.h"
21 #include "stackframe.h"
22 #include "builtin16.h"
23 #include "thread.h"
25 #ifdef NEED_UNDERSCORE_PREFIX
26 # define PREFIX "_"
27 #else
28 # define PREFIX
29 #endif
31 #ifdef HAVE_ASM_STRING
32 # define STRING ".string"
33 #else
34 # define STRING ".ascii"
35 #endif
37 #if defined(__GNUC__) && !defined(__svr4__)
38 # define USE_STABS
39 #else
40 # undef USE_STABS
41 #endif
43 typedef enum
45 TYPE_INVALID,
46 TYPE_BYTE, /* byte variable (Win16) */
47 TYPE_WORD, /* word variable (Win16) */
48 TYPE_LONG, /* long variable (Win16) */
49 TYPE_PASCAL_16, /* pascal function with 16-bit return (Win16) */
50 TYPE_PASCAL, /* pascal function with 32-bit return (Win16) */
51 TYPE_ABS, /* absolute value (Win16) */
52 TYPE_REGISTER, /* register function */
53 TYPE_INTERRUPT, /* interrupt handler function (Win16) */
54 TYPE_STUB, /* unimplemented stub */
55 TYPE_STDCALL, /* stdcall function (Win32) */
56 TYPE_CDECL, /* cdecl function (Win32) */
57 TYPE_VARARGS, /* varargs function (Win32) */
58 TYPE_EXTERN, /* external symbol (Win32) */
59 TYPE_FORWARD, /* forwarded function (Win32) */
60 TYPE_NBTYPES
61 } ORD_TYPE;
63 static const char * const TypeNames[TYPE_NBTYPES] =
65 NULL,
66 "byte", /* TYPE_BYTE */
67 "word", /* TYPE_WORD */
68 "long", /* TYPE_LONG */
69 "pascal16", /* TYPE_PASCAL_16 */
70 "pascal", /* TYPE_PASCAL */
71 "equate", /* TYPE_ABS */
72 "register", /* TYPE_REGISTER */
73 "interrupt", /* TYPE_INTERRUPT */
74 "stub", /* TYPE_STUB */
75 "stdcall", /* TYPE_STDCALL */
76 "cdecl", /* TYPE_CDECL */
77 "varargs", /* TYPE_VARARGS */
78 "extern", /* TYPE_EXTERN */
79 "forward" /* TYPE_FORWARD */
82 #define MAX_ORDINALS 2048
83 #define MAX_IMPORTS 16
85 /* Callback function used for stub functions */
86 #define STUB_CALLBACK \
87 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
89 typedef enum
91 SPEC_INVALID,
92 SPEC_WIN16,
93 SPEC_WIN32
94 } SPEC_TYPE;
96 typedef struct
98 int n_values;
99 int *values;
100 } ORD_VARIABLE;
102 typedef struct
104 int n_args;
105 char arg_types[32];
106 char link_name[80];
107 } ORD_FUNCTION;
109 typedef struct
111 int arg_size;
112 int ret_value;
113 } ORD_RETURN;
115 typedef struct
117 int value;
118 } ORD_ABS;
120 typedef struct
122 char link_name[80];
123 } ORD_VARARGS;
125 typedef struct
127 char link_name[80];
128 } ORD_EXTERN;
130 typedef struct
132 char link_name[80];
133 } ORD_FORWARD;
135 typedef struct
137 ORD_TYPE type;
138 int offset;
139 int lineno;
140 char name[80];
141 union
143 ORD_VARIABLE var;
144 ORD_FUNCTION func;
145 ORD_RETURN ret;
146 ORD_ABS abs;
147 ORD_VARARGS vargs;
148 ORD_EXTERN ext;
149 ORD_FORWARD fwd;
150 } u;
151 } ORDDEF;
153 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
155 static SPEC_TYPE SpecType = SPEC_INVALID;
156 static char DLLName[80];
157 static char DLLFileName[80];
158 static int Limit = 0;
159 static int Base = MAX_ORDINALS;
160 static int DLLHeapSize = 0;
161 static char *SpecName;
162 static FILE *SpecFp;
163 static WORD Code_Selector, Data_Selector;
164 static char DLLInitFunc[80];
165 static char *DLLImports[MAX_IMPORTS];
166 static int nb_imports = 0;
168 char *ParseBuffer = NULL;
169 char *ParseNext;
170 char ParseSaveChar;
171 int Line;
173 static int UsePIC = 0;
175 static int debugging = 1;
177 /* Offset of a structure field relative to the start of the struct */
178 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
180 /* Offset of register relative to the start of the CONTEXT struct */
181 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT86,reg)
183 /* Offset of register relative to the start of the STACK16FRAME struct */
184 #define STACK16OFFSET(reg) STRUCTOFFSET(STACK16FRAME,reg)
186 /* Offset of register relative to the start of the STACK32FRAME struct */
187 #define STACK32OFFSET(reg) STRUCTOFFSET(STACK32FRAME,reg)
189 /* Offset of the stack pointer relative to %fs:(0) */
190 #define STACKOFFSET (STRUCTOFFSET(TEB,cur_stack))
192 static void BuildCallFrom16Func( FILE *outfile, char *profile, char *prefix, int local );
194 static void *xmalloc (size_t size)
196 void *res;
198 res = malloc (size ? size : 1);
199 if (res == NULL)
201 fprintf (stderr, "Virtual memory exhausted.\n");
202 exit (1);
204 return res;
208 static void *xrealloc (void *ptr, size_t size)
210 void *res = realloc (ptr, size);
211 if (res == NULL)
213 fprintf (stderr, "Virtual memory exhausted.\n");
214 exit (1);
216 return res;
219 static char *xstrdup( const char *str )
221 char *res = strdup( str );
222 if (!res)
224 fprintf (stderr, "Virtual memory exhausted.\n");
225 exit (1);
227 return res;
230 static int IsNumberString(char *s)
232 while (*s != '\0')
233 if (!isdigit(*s++))
234 return 0;
236 return 1;
239 static char *strupper(char *s)
241 char *p;
243 for(p = s; *p != '\0'; p++)
244 *p = toupper(*p);
246 return s;
249 static char * GetTokenInLine(void)
251 char *p;
252 char *token;
254 if (ParseNext != ParseBuffer)
256 if (ParseSaveChar == '\0')
257 return NULL;
258 *ParseNext = ParseSaveChar;
262 * Remove initial white space.
264 for (p = ParseNext; isspace(*p); p++)
267 if ((*p == '\0') || (*p == '#'))
268 return NULL;
271 * Find end of token.
273 token = p++;
274 if (*token != '(' && *token != ')')
275 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
276 p++;
278 ParseSaveChar = *p;
279 ParseNext = p;
280 *p = '\0';
282 return token;
285 static char * GetToken(void)
287 char *token;
289 if (ParseBuffer == NULL)
291 ParseBuffer = xmalloc(512);
292 ParseNext = ParseBuffer;
293 while (1)
295 Line++;
296 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
297 return NULL;
298 if (ParseBuffer[0] != '#')
299 break;
303 while ((token = GetTokenInLine()) == NULL)
305 ParseNext = ParseBuffer;
306 while (1)
308 Line++;
309 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
310 return NULL;
311 if (ParseBuffer[0] != '#')
312 break;
316 return token;
320 /*******************************************************************
321 * ParseVariable
323 * Parse a variable definition.
325 static int ParseVariable( ORDDEF *odp )
327 char *endptr;
328 int *value_array;
329 int n_values;
330 int value_array_size;
332 char *token = GetToken();
333 if (*token != '(')
335 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
336 SpecName, Line, token);
337 return -1;
340 n_values = 0;
341 value_array_size = 25;
342 value_array = xmalloc(sizeof(*value_array) * value_array_size);
344 while ((token = GetToken()) != NULL)
346 if (*token == ')')
347 break;
349 value_array[n_values++] = strtol(token, &endptr, 0);
350 if (n_values == value_array_size)
352 value_array_size += 25;
353 value_array = xrealloc(value_array,
354 sizeof(*value_array) * value_array_size);
357 if (endptr == NULL || *endptr != '\0')
359 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
360 SpecName, Line, token);
361 return -1;
365 if (token == NULL)
367 fprintf(stderr, "%s:%d: End of file in variable declaration\n",
368 SpecName, Line);
369 return -1;
372 odp->u.var.n_values = n_values;
373 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
375 return 0;
379 /*******************************************************************
380 * ParseExportFunction
382 * Parse a function definition.
384 static int ParseExportFunction( ORDDEF *odp )
386 char *token;
387 int i;
389 switch(SpecType)
391 case SPEC_WIN16:
392 if (odp->type == TYPE_STDCALL)
394 fprintf( stderr, "%s:%d: 'stdcall' not supported for Win16\n",
395 SpecName, Line );
396 return -1;
398 break;
399 case SPEC_WIN32:
400 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
402 fprintf( stderr, "%s:%d: 'pascal' not supported for Win32\n",
403 SpecName, Line );
404 return -1;
406 break;
407 default:
408 break;
411 token = GetToken();
412 if (*token != '(')
414 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
415 SpecName, Line, token);
416 return -1;
419 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
421 token = GetToken();
422 if (*token == ')')
423 break;
425 if (!strcmp(token, "word"))
426 odp->u.func.arg_types[i] = 'w';
427 else if (!strcmp(token, "s_word"))
428 odp->u.func.arg_types[i] = 's';
429 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
430 odp->u.func.arg_types[i] = 'l';
431 else if (!strcmp(token, "ptr"))
432 odp->u.func.arg_types[i] = 'p';
433 else if (!strcmp(token, "str"))
434 odp->u.func.arg_types[i] = 't';
435 else if (!strcmp(token, "wstr"))
436 odp->u.func.arg_types[i] = 'W';
437 else if (!strcmp(token, "segstr"))
438 odp->u.func.arg_types[i] = 'T';
439 else if (!strcmp(token, "double"))
441 odp->u.func.arg_types[i++] = 'l';
442 odp->u.func.arg_types[i] = 'l';
444 else
446 fprintf(stderr, "%s:%d: Unknown variable type '%s'\n",
447 SpecName, Line, token);
448 return -1;
450 if (SpecType == SPEC_WIN32)
452 if (strcmp(token, "long") &&
453 strcmp(token, "ptr") &&
454 strcmp(token, "str") &&
455 strcmp(token, "wstr") &&
456 strcmp(token, "double"))
458 fprintf( stderr, "%s:%d: Type '%s' not supported for Win32\n",
459 SpecName, Line, token );
460 return -1;
464 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
466 fprintf( stderr, "%s:%d: Too many arguments\n", SpecName, Line );
467 return -1;
469 odp->u.func.arg_types[i] = '\0';
470 if ((odp->type == TYPE_STDCALL) && !i)
471 odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
472 strcpy(odp->u.func.link_name, GetToken());
473 return 0;
477 /*******************************************************************
478 * ParseEquate
480 * Parse an 'equate' definition.
482 static int ParseEquate( ORDDEF *odp )
484 char *endptr;
486 char *token = GetToken();
487 int value = strtol(token, &endptr, 0);
488 if (endptr == NULL || *endptr != '\0')
490 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
491 SpecName, Line, token);
492 return -1;
495 if (SpecType == SPEC_WIN32)
497 fprintf( stderr, "%s:%d: 'equate' not supported for Win32\n",
498 SpecName, Line );
499 return -1;
502 odp->u.abs.value = value;
503 return 0;
507 /*******************************************************************
508 * ParseStub
510 * Parse a 'stub' definition.
512 static int ParseStub( ORDDEF *odp )
514 odp->u.func.arg_types[0] = '\0';
515 strcpy( odp->u.func.link_name, STUB_CALLBACK );
516 return 0;
520 /*******************************************************************
521 * ParseVarargs
523 * Parse an 'varargs' definition.
525 static int ParseVarargs( ORDDEF *odp )
527 char *token;
529 if (SpecType == SPEC_WIN16)
531 fprintf( stderr, "%s:%d: 'varargs' not supported for Win16\n",
532 SpecName, Line );
533 return -1;
536 token = GetToken();
537 if (*token != '(')
539 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
540 SpecName, Line, token);
541 return -1;
543 token = GetToken();
544 if (*token != ')')
546 fprintf(stderr, "%s:%d: Expected ')' got '%s'\n",
547 SpecName, Line, token);
548 return -1;
551 strcpy( odp->u.vargs.link_name, GetToken() );
552 return 0;
555 /*******************************************************************
556 * ParseInterrupt
558 * Parse an 'interrupt' definition.
560 static int ParseInterrupt( ORDDEF *odp )
562 char *token;
564 if (SpecType == SPEC_WIN32)
566 fprintf( stderr, "%s:%d: 'interrupt' not supported for Win32\n",
567 SpecName, Line );
568 return -1;
571 token = GetToken();
572 if (*token != '(')
574 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
575 SpecName, Line, token);
576 return -1;
578 token = GetToken();
579 if (*token != ')')
581 fprintf(stderr, "%s:%d: Expected ')' got '%s'\n",
582 SpecName, Line, token);
583 return -1;
586 odp->u.func.arg_types[0] = '\0';
587 strcpy( odp->u.func.link_name, GetToken() );
588 return 0;
592 /*******************************************************************
593 * ParseExtern
595 * Parse an 'extern' definition.
597 static int ParseExtern( ORDDEF *odp )
599 if (SpecType == SPEC_WIN16)
601 fprintf( stderr, "%s:%d: 'extern' not supported for Win16\n",
602 SpecName, Line );
603 return -1;
605 strcpy( odp->u.ext.link_name, GetToken() );
606 return 0;
610 /*******************************************************************
611 * ParseForward
613 * Parse a 'forward' definition.
615 static int ParseForward( ORDDEF *odp )
617 if (SpecType == SPEC_WIN16)
619 fprintf( stderr, "%s:%d: 'forward' not supported for Win16\n",
620 SpecName, Line );
621 return -1;
623 strcpy( odp->u.fwd.link_name, GetToken() );
624 return 0;
628 /*******************************************************************
629 * ParseOrdinal
631 * Parse an ordinal definition.
633 static int ParseOrdinal(int ordinal)
635 ORDDEF *odp;
636 char *token;
638 if (ordinal >= MAX_ORDINALS)
640 fprintf(stderr, "%s:%d: Ordinal number too large\n", SpecName, Line );
641 return -1;
643 if (ordinal > Limit) Limit = ordinal;
644 if (ordinal < Base) Base = ordinal;
646 odp = &OrdinalDefinitions[ordinal];
647 if (!(token = GetToken()))
649 fprintf(stderr, "%s:%d: Expected type after ordinal\n", SpecName, Line);
650 return -1;
653 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
654 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
655 break;
657 if (odp->type >= TYPE_NBTYPES)
659 fprintf( stderr,
660 "%s:%d: Expected type after ordinal, found '%s' instead\n",
661 SpecName, Line, token );
662 return -1;
665 if (!(token = GetToken()))
667 fprintf( stderr, "%s:%d: Expected name after type\n", SpecName, Line );
668 return -1;
670 strcpy( odp->name, token );
671 odp->lineno = Line;
673 switch(odp->type)
675 case TYPE_BYTE:
676 case TYPE_WORD:
677 case TYPE_LONG:
678 return ParseVariable( odp );
679 case TYPE_PASCAL_16:
680 case TYPE_PASCAL:
681 case TYPE_REGISTER:
682 case TYPE_STDCALL:
683 case TYPE_CDECL:
684 return ParseExportFunction( odp );
685 case TYPE_INTERRUPT:
686 return ParseInterrupt( odp );
687 case TYPE_ABS:
688 return ParseEquate( odp );
689 case TYPE_STUB:
690 return ParseStub( odp );
691 case TYPE_VARARGS:
692 return ParseVarargs( odp );
693 case TYPE_EXTERN:
694 return ParseExtern( odp );
695 case TYPE_FORWARD:
696 return ParseForward( odp );
697 default:
698 fprintf( stderr, "Should not happen\n" );
699 return -1;
704 /*******************************************************************
705 * ParseTopLevel
707 * Parse a spec file.
709 static int ParseTopLevel(void)
711 char *token;
713 while ((token = GetToken()) != NULL)
715 if (strcmp(token, "name") == 0)
717 strcpy(DLLName, GetToken());
718 strupper(DLLName);
719 if (!DLLFileName[0]) sprintf( DLLFileName, "%s.DLL", DLLName );
721 else if (strcmp(token, "file") == 0)
723 strcpy(DLLFileName, GetToken());
724 strupper(DLLFileName);
726 else if (strcmp(token, "type") == 0)
728 token = GetToken();
729 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
730 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
731 else
733 fprintf(stderr, "%s:%d: Type must be 'win16' or 'win32'\n",
734 SpecName, Line);
735 return -1;
738 else if (strcmp(token, "heap") == 0)
740 token = GetToken();
741 if (!IsNumberString(token))
743 fprintf(stderr, "%s:%d: Expected number after heap\n",
744 SpecName, Line);
745 return -1;
747 DLLHeapSize = atoi(token);
749 else if (strcmp(token, "init") == 0)
751 strcpy(DLLInitFunc, GetToken());
752 if (SpecType == SPEC_WIN16)
754 fprintf(stderr, "%s:%d: init cannot be used for Win16 spec files\n",
755 SpecName, Line);
756 return -1;
758 if (!DLLInitFunc[0])
760 fprintf(stderr, "%s:%d: Expected function name after init\n", SpecName, Line);
761 return -1;
764 else if (strcmp(token, "import") == 0)
766 if (nb_imports >= MAX_IMPORTS)
768 fprintf( stderr, "%s:%d: Too many imports (limit %d)\n",
769 SpecName, Line, MAX_IMPORTS );
770 return -1;
772 if (SpecType != SPEC_WIN32)
774 fprintf( stderr, "%s:%d: Imports not supported for Win16\n", SpecName, Line );
775 return -1;
777 DLLImports[nb_imports++] = xstrdup(GetToken());
779 else if (IsNumberString(token))
781 int ordinal;
782 int rv;
784 ordinal = atoi(token);
785 if ((rv = ParseOrdinal(ordinal)) < 0)
786 return rv;
788 else
790 fprintf(stderr,
791 "%s:%d: Expected name, id, length or ordinal\n",
792 SpecName, Line);
793 return -1;
797 return 0;
801 /*******************************************************************
802 * StoreVariableCode
804 * Store a list of ints into a byte array.
806 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
808 int i;
810 switch(size)
812 case 1:
813 for (i = 0; i < odp->u.var.n_values; i++)
814 buffer[i] = odp->u.var.values[i];
815 break;
816 case 2:
817 for (i = 0; i < odp->u.var.n_values; i++)
818 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
819 break;
820 case 4:
821 for (i = 0; i < odp->u.var.n_values; i++)
822 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
823 break;
825 return odp->u.var.n_values * size;
829 /*******************************************************************
830 * DumpBytes
832 * Dump a byte stream into the assembly code.
834 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
835 const char *label )
837 int i;
839 fprintf( outfile, "\nstatic BYTE %s[] = \n{", label );
841 for (i = 0; i < len; i++)
843 if (!(i & 0x0f)) fprintf( outfile, "\n " );
844 fprintf( outfile, "%d", *data++ );
845 if (i < len - 1) fprintf( outfile, ", " );
847 fprintf( outfile, "\n};\n" );
851 /*******************************************************************
852 * BuildModule16
854 * Build the in-memory representation of a 16-bit NE module, and dump it
855 * as a byte stream into the assembly code.
857 static int BuildModule16( FILE *outfile, int max_code_offset,
858 int max_data_offset )
860 ORDDEF *odp;
861 int i;
862 char *buffer;
863 NE_MODULE *pModule;
864 SEGTABLEENTRY *pSegment;
865 OFSTRUCT *pFileInfo;
866 BYTE *pstr;
867 WORD *pword;
868 ET_BUNDLE *bundle = 0;
869 ET_ENTRY *entry = 0;
871 /* Module layout:
872 * NE_MODULE Module
873 * OFSTRUCT File information
874 * SEGTABLEENTRY Segment 1 (code)
875 * SEGTABLEENTRY Segment 2 (data)
876 * WORD[2] Resource table (empty)
877 * BYTE[2] Imported names (empty)
878 * BYTE[n] Resident names table
879 * BYTE[n] Entry table
882 buffer = xmalloc( 0x10000 );
884 pModule = (NE_MODULE *)buffer;
885 memset( pModule, 0, sizeof(*pModule) );
886 pModule->magic = IMAGE_OS2_SIGNATURE;
887 pModule->count = 1;
888 pModule->next = 0;
889 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
890 pModule->dgroup = 2;
891 pModule->heap_size = DLLHeapSize;
892 pModule->stack_size = 0;
893 pModule->ip = 0;
894 pModule->cs = 0;
895 pModule->sp = 0;
896 pModule->ss = 0;
897 pModule->seg_count = 2;
898 pModule->modref_count = 0;
899 pModule->nrname_size = 0;
900 pModule->modref_table = 0;
901 pModule->nrname_fpos = 0;
902 pModule->moveable_entries = 0;
903 pModule->alignment = 0;
904 pModule->truetype = 0;
905 pModule->os_flags = NE_OSFLAGS_WINDOWS;
906 pModule->misc_flags = 0;
907 pModule->dlls_to_init = 0;
908 pModule->nrname_handle = 0;
909 pModule->min_swap_area = 0;
910 pModule->expected_version = 0x030a;
911 pModule->module32 = 0;
912 pModule->self = 0;
913 pModule->self_loading_sel = 0;
915 /* File information */
917 pFileInfo = (OFSTRUCT *)(pModule + 1);
918 pModule->fileinfo = (int)pFileInfo - (int)pModule;
919 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
920 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
921 + strlen(DLLFileName);
922 strcpy( pFileInfo->szPathName, DLLFileName );
923 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
925 #ifdef __i386__ /* FIXME: Alignment problems! */
927 /* Segment table */
929 pSegment = (SEGTABLEENTRY *)pstr;
930 pModule->seg_table = (int)pSegment - (int)pModule;
931 pSegment->filepos = 0;
932 pSegment->size = max_code_offset;
933 pSegment->flags = 0;
934 pSegment->minsize = max_code_offset;
935 pSegment->hSeg = 0;
936 pSegment++;
938 pModule->dgroup_entry = (int)pSegment - (int)pModule;
939 pSegment->filepos = 0;
940 pSegment->size = max_data_offset;
941 pSegment->flags = NE_SEGFLAGS_DATA;
942 pSegment->minsize = max_data_offset;
943 pSegment->hSeg = 0;
944 pSegment++;
946 /* Resource table */
948 pword = (WORD *)pSegment;
949 pModule->res_table = (int)pword - (int)pModule;
950 *pword++ = 0;
951 *pword++ = 0;
953 /* Imported names table */
955 pstr = (char *)pword;
956 pModule->import_table = (int)pstr - (int)pModule;
957 *pstr++ = 0;
958 *pstr++ = 0;
960 /* Resident names table */
962 pModule->name_table = (int)pstr - (int)pModule;
963 /* First entry is module name */
964 *pstr = strlen(DLLName );
965 strcpy( pstr + 1, DLLName );
966 pstr += *pstr + 1;
967 *(WORD *)pstr = 0;
968 pstr += sizeof(WORD);
969 /* Store all ordinals */
970 odp = OrdinalDefinitions + 1;
971 for (i = 1; i <= Limit; i++, odp++)
973 if (!odp->name[0]) continue;
974 *pstr = strlen( odp->name );
975 strcpy( pstr + 1, odp->name );
976 strupper( pstr + 1 );
977 pstr += *pstr + 1;
978 *(WORD *)pstr = i;
979 pstr += sizeof(WORD);
981 *pstr++ = 0;
983 /* Entry table */
985 pModule->entry_table = (int)pstr - (int)pModule;
986 odp = OrdinalDefinitions + 1;
987 for (i = 1; i <= Limit; i++, odp++)
989 int selector = 0;
991 switch (odp->type)
993 case TYPE_CDECL:
994 case TYPE_PASCAL:
995 case TYPE_PASCAL_16:
996 case TYPE_REGISTER:
997 case TYPE_INTERRUPT:
998 case TYPE_STUB:
999 selector = 1; /* Code selector */
1000 break;
1002 case TYPE_BYTE:
1003 case TYPE_WORD:
1004 case TYPE_LONG:
1005 selector = 2; /* Data selector */
1006 break;
1008 case TYPE_ABS:
1009 selector = 0xfe; /* Constant selector */
1010 break;
1012 default:
1013 selector = 0; /* Invalid selector */
1014 break;
1017 if ( !selector )
1018 continue;
1020 if ( bundle && bundle->last+1 == i )
1021 bundle->last++;
1022 else
1024 if ( bundle )
1025 bundle->next = (char *)pstr - (char *)pModule;
1027 bundle = (ET_BUNDLE *)pstr;
1028 bundle->first = i-1;
1029 bundle->last = i;
1030 bundle->next = 0;
1031 pstr += sizeof(ET_BUNDLE);
1034 /* FIXME: is this really correct ?? */
1035 entry = (ET_ENTRY *)pstr;
1036 entry->type = 0xff; /* movable */
1037 entry->flags = 3; /* exported & public data */
1038 entry->segnum = selector;
1039 entry->offs = odp->offset;
1040 pstr += sizeof(ET_ENTRY);
1042 *pstr++ = 0;
1043 #endif
1045 /* Dump the module content */
1047 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
1048 "Module" );
1049 return (int)pstr - (int)pModule;
1053 /*******************************************************************
1054 * BuildSpec32File
1056 * Build a Win32 C file from a spec file.
1058 static int BuildSpec32File( char * specfile, FILE *outfile )
1060 ORDDEF *odp;
1061 int i, nb_names, fwd_size = 0;
1063 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
1064 specfile );
1065 fprintf( outfile, "#include \"builtin32.h\"\n\n" );
1067 /* Output code for all stubs functions */
1069 fprintf( outfile, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
1070 DLLName );
1071 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1073 if (odp->type != TYPE_STUB) continue;
1074 fprintf( outfile, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
1075 i, DLLName, i );
1078 /* Output code for all register functions */
1080 fprintf( outfile, "#ifdef __i386__\n" );
1081 fprintf( outfile, "#ifndef __GNUC__\n" );
1082 fprintf( outfile, "static void __asm__dummy() {\n" );
1083 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
1084 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1086 if (odp->type != TYPE_REGISTER) continue;
1087 fprintf( outfile,
1088 "__asm__(\".align 4\\n\\t\"\n"
1089 " \".globl " PREFIX "%s\\n\\t\"\n"
1090 " \".type " PREFIX "%s,@function\\n\\t\"\n"
1091 " \"" PREFIX "%s:\\n\\t\"\n"
1092 " \"call " PREFIX "CALL32_Regs\\n\\t\"\n"
1093 " \".long " PREFIX "__regs_%s\\n\\t\"\n"
1094 " \".byte %d,%d\");\n",
1095 odp->u.func.link_name, odp->u.func.link_name,
1096 odp->u.func.link_name, odp->u.func.link_name,
1097 4 * strlen(odp->u.func.arg_types),
1098 4 * strlen(odp->u.func.arg_types) );
1100 fprintf( outfile, "#ifndef __GNUC__\n" );
1101 fprintf( outfile, "}\n" );
1102 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
1103 fprintf( outfile, "#endif /* defined(__i386__) */\n" );
1105 /* Output the DLL functions prototypes */
1107 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1109 switch(odp->type)
1111 case TYPE_VARARGS:
1112 fprintf( outfile, "extern void %s();\n", odp->u.vargs.link_name );
1113 break;
1114 case TYPE_EXTERN:
1115 fprintf( outfile, "extern void %s();\n", odp->u.ext.link_name );
1116 break;
1117 case TYPE_REGISTER:
1118 case TYPE_STDCALL:
1119 case TYPE_CDECL:
1120 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1121 break;
1122 case TYPE_FORWARD:
1123 fwd_size += strlen(odp->u.fwd.link_name) + 1;
1124 break;
1125 case TYPE_INVALID:
1126 case TYPE_STUB:
1127 break;
1128 default:
1129 fprintf(stderr,"build: function type %d not available for Win32\n",
1130 odp->type);
1131 return -1;
1135 /* Output LibMain function */
1136 if (DLLInitFunc[0]) fprintf( outfile, "extern void %s();\n", DLLInitFunc );
1139 /* Output the DLL functions table */
1141 fprintf( outfile, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1142 Limit - Base + 1 );
1143 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1145 switch(odp->type)
1147 case TYPE_INVALID:
1148 fprintf( outfile, " 0" );
1149 break;
1150 case TYPE_VARARGS:
1151 fprintf( outfile, " %s", odp->u.vargs.link_name );
1152 break;
1153 case TYPE_EXTERN:
1154 fprintf( outfile, " %s", odp->u.ext.link_name );
1155 break;
1156 case TYPE_REGISTER:
1157 case TYPE_STDCALL:
1158 case TYPE_CDECL:
1159 fprintf( outfile, " %s", odp->u.func.link_name);
1160 break;
1161 case TYPE_STUB:
1162 fprintf( outfile, " __stub_%d", i );
1163 break;
1164 case TYPE_FORWARD:
1165 fprintf( outfile, " (ENTRYPOINT32)\"%s\"", odp->u.fwd.link_name );
1166 break;
1167 default:
1168 return -1;
1170 if (i < Limit) fprintf( outfile, ",\n" );
1172 fprintf( outfile, "\n};\n\n" );
1174 /* Output the DLL names table */
1176 nb_names = 0;
1177 fprintf( outfile, "static const char * const FuncNames[] =\n{\n" );
1178 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1180 if (odp->type == TYPE_INVALID) continue;
1181 if (nb_names++) fprintf( outfile, ",\n" );
1182 fprintf( outfile, " \"%s\"", odp->name );
1184 fprintf( outfile, "\n};\n\n" );
1186 /* Output the DLL argument types */
1188 fprintf( outfile, "static const unsigned int ArgTypes[%d] =\n{\n",
1189 Limit - Base + 1 );
1190 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1192 unsigned int j, mask = 0;
1193 if ((odp->type == TYPE_STDCALL) || (odp->type == TYPE_CDECL) ||
1194 (odp->type == TYPE_REGISTER))
1195 for (j = 0; odp->u.func.arg_types[j]; j++)
1197 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
1198 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
1200 fprintf( outfile, " %d", mask );
1201 if (i < Limit) fprintf( outfile, ",\n" );
1203 fprintf( outfile, "\n};\n\n" );
1205 /* Output the DLL ordinals table */
1207 fprintf( outfile, "static const unsigned short FuncOrdinals[] =\n{\n" );
1208 nb_names = 0;
1209 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1211 if (odp->type == TYPE_INVALID) continue;
1212 if (nb_names++) fprintf( outfile, ",\n" );
1213 fprintf( outfile, " %d", i - Base );
1215 fprintf( outfile, "\n};\n\n" );
1217 /* Output the DLL functions arguments */
1219 fprintf( outfile, "static const unsigned char FuncArgs[%d] =\n{\n",
1220 Limit - Base + 1 );
1221 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1223 unsigned char args;
1224 switch(odp->type)
1226 case TYPE_STDCALL:
1227 args = (unsigned char)strlen(odp->u.func.arg_types);
1228 break;
1229 case TYPE_CDECL:
1230 args = 0x80 | (unsigned char)strlen(odp->u.func.arg_types);
1231 break;
1232 case TYPE_REGISTER:
1233 args = 0x40 | (unsigned char)strlen(odp->u.func.arg_types);
1234 break;
1235 case TYPE_FORWARD:
1236 args = 0xfd;
1237 break;
1238 default:
1239 args = 0xff;
1240 break;
1242 fprintf( outfile, " 0x%02x", args );
1243 if (i < Limit) fprintf( outfile, ",\n" );
1245 fprintf( outfile, "\n};\n\n" );
1247 /* Output the DLL imports */
1249 if (nb_imports)
1251 fprintf( outfile, "static const char * const Imports[%d] =\n{\n", nb_imports );
1252 for (i = 0; i < nb_imports; i++)
1254 fprintf( outfile, " \"%s\"", DLLImports[i] );
1255 if (i < nb_imports-1) fprintf( outfile, ",\n" );
1257 fprintf( outfile, "\n};\n\n" );
1260 /* Output the DLL descriptor */
1262 fprintf( outfile, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1263 DLLName );
1264 fprintf( outfile, " \"%s\",\n", DLLName );
1265 fprintf( outfile, " %d,\n", Base );
1266 fprintf( outfile, " %d,\n", Limit - Base + 1 );
1267 fprintf( outfile, " %d,\n", nb_names );
1268 fprintf( outfile, " %d,\n", nb_imports );
1269 fprintf( outfile, " %d,\n", (fwd_size + 3) & ~3 );
1270 fprintf( outfile,
1271 " Functions,\n"
1272 " FuncNames,\n"
1273 " FuncOrdinals,\n"
1274 " FuncArgs,\n"
1275 " ArgTypes,\n");
1276 fprintf( outfile, " %s,\n", nb_imports ? "Imports" : "0" );
1277 fprintf( outfile, " %s\n", DLLInitFunc[0] ? DLLInitFunc : "0" );
1278 fprintf( outfile, "};\n" );
1279 return 0;
1282 /*******************************************************************
1283 * Spec16TypeCompare
1285 static int Spec16TypeCompare( const void *e1, const void *e2 )
1287 const ORDDEF *odp1 = *(const ORDDEF **)e1;
1288 const ORDDEF *odp2 = *(const ORDDEF **)e2;
1290 int type1 = (odp1->type == TYPE_CDECL) ? 0
1291 : (odp1->type == TYPE_REGISTER) ? 3
1292 : (odp1->type == TYPE_INTERRUPT) ? 4
1293 : (odp1->type == TYPE_PASCAL_16) ? 1 : 2;
1295 int type2 = (odp2->type == TYPE_CDECL) ? 0
1296 : (odp2->type == TYPE_REGISTER) ? 3
1297 : (odp2->type == TYPE_INTERRUPT) ? 4
1298 : (odp2->type == TYPE_PASCAL_16) ? 1 : 2;
1300 int retval = type1 - type2;
1301 if ( !retval )
1302 retval = strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
1304 return retval;
1307 /*******************************************************************
1308 * BuildSpec16File
1310 * Build a Win16 assembly file from a spec file.
1312 static int BuildSpec16File( char * specfile, FILE *outfile )
1314 ORDDEF *odp, **type, **typelist;
1315 int i, nFuncs, nTypes;
1316 int code_offset, data_offset, module_size;
1317 unsigned char *data;
1319 /* File header */
1321 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
1322 specfile );
1323 fprintf( outfile, "#define __FLATCS__ 0x%04x\n", Code_Selector );
1324 fprintf( outfile, "#include \"builtin16.h\"\n\n" );
1326 data = (unsigned char *)xmalloc( 0x10000 );
1327 memset( data, 0, 16 );
1328 data_offset = 16;
1331 /* Build sorted list of all argument types, without duplicates */
1333 typelist = (ORDDEF **)calloc( Limit+1, sizeof(ORDDEF *) );
1335 odp = OrdinalDefinitions;
1336 for (i = nFuncs = 0; i <= Limit; i++, odp++)
1338 switch (odp->type)
1340 case TYPE_REGISTER:
1341 case TYPE_INTERRUPT:
1342 case TYPE_CDECL:
1343 case TYPE_PASCAL:
1344 case TYPE_PASCAL_16:
1345 case TYPE_STUB:
1346 typelist[nFuncs++] = odp;
1348 default:
1349 break;
1353 qsort( typelist, nFuncs, sizeof(ORDDEF *), Spec16TypeCompare );
1355 i = nTypes = 0;
1356 while ( i < nFuncs )
1358 typelist[nTypes++] = typelist[i++];
1359 while ( i < nFuncs && Spec16TypeCompare( typelist + i, typelist + nTypes-1 ) == 0 )
1360 i++;
1363 /* Output CallFrom16 routines needed by this .spec file */
1365 for ( i = 0; i < nTypes; i++ )
1367 char profile[101];
1369 sprintf( profile, "%s_%s_%s",
1370 (typelist[i]->type == TYPE_CDECL) ? "c" : "p",
1371 (typelist[i]->type == TYPE_REGISTER) ? "regs" :
1372 (typelist[i]->type == TYPE_INTERRUPT) ? "intr" :
1373 (typelist[i]->type == TYPE_PASCAL_16) ? "word" : "long",
1374 typelist[i]->u.func.arg_types );
1376 BuildCallFrom16Func( outfile, profile, DLLName, TRUE );
1379 /* Output the DLL functions prototypes */
1381 odp = OrdinalDefinitions;
1382 for (i = 0; i <= Limit; i++, odp++)
1384 switch(odp->type)
1386 case TYPE_REGISTER:
1387 case TYPE_INTERRUPT:
1388 case TYPE_CDECL:
1389 case TYPE_PASCAL:
1390 case TYPE_PASCAL_16:
1391 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1392 break;
1393 default:
1394 break;
1398 /* Output code segment */
1400 fprintf( outfile, "\nstatic struct\n{\n CALLFROM16 call[%d];\n"
1401 " ENTRYPOINT16 entry[%d];\n} Code_Segment = \n{\n {\n",
1402 nTypes, nFuncs );
1403 code_offset = 0;
1405 for ( i = 0; i < nTypes; i++ )
1407 char profile[101], *arg;
1408 int argsize = 0;
1410 sprintf( profile, "%s_%s_%s",
1411 (typelist[i]->type == TYPE_CDECL) ? "c" : "p",
1412 (typelist[i]->type == TYPE_REGISTER) ? "regs" :
1413 (typelist[i]->type == TYPE_INTERRUPT) ? "intr" :
1414 (typelist[i]->type == TYPE_PASCAL_16) ? "word" : "long",
1415 typelist[i]->u.func.arg_types );
1417 if ( typelist[i]->type != TYPE_CDECL )
1418 for ( arg = typelist[i]->u.func.arg_types; *arg; arg++ )
1419 switch ( *arg )
1421 case 'w': /* word */
1422 case 's': /* s_word */
1423 argsize += 2;
1424 break;
1426 case 'l': /* long or segmented pointer */
1427 case 'T': /* segmented pointer to null-terminated string */
1428 case 'p': /* linear pointer */
1429 case 't': /* linear pointer to null-terminated string */
1430 argsize += 4;
1431 break;
1434 if ( typelist[i]->type == TYPE_INTERRUPT )
1435 argsize += 2;
1437 fprintf( outfile, " CF16_%s( %s_CallFrom16_%s, %d, \"%s\" ),\n",
1438 ( typelist[i]->type == TYPE_REGISTER
1439 || typelist[i]->type == TYPE_INTERRUPT)? "REGS":
1440 typelist[i]->type == TYPE_PASCAL_16? "WORD" : "LONG",
1441 DLLName, profile, argsize, profile );
1443 code_offset += sizeof(CALLFROM16);
1445 fprintf( outfile, " },\n {\n" );
1447 odp = OrdinalDefinitions;
1448 for (i = 0; i <= Limit; i++, odp++)
1450 switch (odp->type)
1452 case TYPE_INVALID:
1453 odp->offset = 0xffff;
1454 break;
1456 case TYPE_ABS:
1457 odp->offset = LOWORD(odp->u.abs.value);
1458 break;
1460 case TYPE_BYTE:
1461 odp->offset = data_offset;
1462 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1463 break;
1465 case TYPE_WORD:
1466 odp->offset = data_offset;
1467 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1468 break;
1470 case TYPE_LONG:
1471 odp->offset = data_offset;
1472 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1473 break;
1475 case TYPE_REGISTER:
1476 case TYPE_INTERRUPT:
1477 case TYPE_CDECL:
1478 case TYPE_PASCAL:
1479 case TYPE_PASCAL_16:
1480 case TYPE_STUB:
1481 type = bsearch( &odp, typelist, nTypes, sizeof(ORDDEF *), Spec16TypeCompare );
1482 assert( type );
1484 fprintf( outfile, " /* %s.%d */ ", DLLName, i );
1485 fprintf( outfile, "EP( %s, %d /* %s_%s_%s */ ),\n",
1486 odp->u.func.link_name,
1487 (type-typelist)*sizeof(CALLFROM16) -
1488 (code_offset + sizeof(ENTRYPOINT16)),
1489 (odp->type == TYPE_CDECL) ? "c" : "p",
1490 (odp->type == TYPE_REGISTER) ? "regs" :
1491 (odp->type == TYPE_INTERRUPT) ? "intr" :
1492 (odp->type == TYPE_PASCAL_16) ? "word" : "long",
1493 odp->u.func.arg_types );
1495 odp->offset = code_offset;
1496 code_offset += sizeof(ENTRYPOINT16);
1497 break;
1499 default:
1500 fprintf(stderr,"build: function type %d not available for Win16\n",
1501 odp->type);
1502 return -1;
1506 fprintf( outfile, " }\n};\n" );
1508 /* Output data segment */
1510 DumpBytes( outfile, data, data_offset, "Data_Segment" );
1512 /* Build the module */
1514 module_size = BuildModule16( outfile, code_offset, data_offset );
1516 /* Output the DLL descriptor */
1518 fprintf( outfile, "\nWIN16_DESCRIPTOR %s_Descriptor = \n{\n", DLLName );
1519 fprintf( outfile, " \"%s\",\n", DLLName );
1520 fprintf( outfile, " Module,\n" );
1521 fprintf( outfile, " sizeof(Module),\n" );
1522 fprintf( outfile, " (BYTE *)&Code_Segment,\n" );
1523 fprintf( outfile, " (BYTE *)Data_Segment\n" );
1524 fprintf( outfile, "};\n" );
1526 return 0;
1530 /*******************************************************************
1531 * BuildSpecFile
1533 * Build an assembly file from a spec file.
1535 static int BuildSpecFile( FILE *outfile, char *specname )
1537 SpecName = specname;
1538 SpecFp = fopen( specname, "r");
1539 if (SpecFp == NULL)
1541 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1542 return -1;
1545 if (ParseTopLevel() < 0) return -1;
1547 switch(SpecType)
1549 case SPEC_WIN16:
1550 return BuildSpec16File( specname, outfile );
1551 case SPEC_WIN32:
1552 return BuildSpec32File( specname, outfile );
1553 default:
1554 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1555 return -1;
1560 /*******************************************************************
1561 * BuildCallFrom16Func
1563 * Build a 16-bit-to-Wine callback glue function.
1565 * The generated routines are intended to be used as argument conversion
1566 * routines to be called by the CallFrom16... core. Thus, the prototypes of
1567 * the generated routines are (see also CallFrom16):
1569 * extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
1570 * extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
1571 * extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args,
1572 * CONTEXT86 *context );
1573 * extern void WINAPI PREFIX_CallFrom16_C_intr_xxx( FARPROC func, LPBYTE args,
1574 * CONTEXT86 *context );
1576 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
1577 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
1578 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1579 * 'T'=segmented pointer to null-terminated string).
1581 * The generated routines fetch the arguments from the 16-bit stack (pointed
1582 * to by 'args'); the offsets of the single argument values are computed
1583 * according to the calling convention and the argument types. Then, the
1584 * 32-bit entry point is called with these arguments.
1586 * For register functions, the arguments (if present) are converted just
1587 * the same as for normal functions, but in addition the CONTEXT86 pointer
1588 * filled with the current register values is passed to the 32-bit routine.
1589 * (An 'intr' interrupt handler routine is treated exactly like a register
1590 * routine, except that upon return, the flags word pushed onto the stack
1591 * by the interrupt is removed by the 16-bit call stub.)
1594 static void BuildCallFrom16Func( FILE *outfile, char *profile, char *prefix, int local )
1596 int i, pos, argsize = 0;
1597 int short_ret = 0;
1598 int reg_func = 0;
1599 int usecdecl = 0;
1600 char *args = profile + 7;
1601 char *ret_type;
1603 /* Parse function type */
1605 if (!strncmp( "c_", profile, 2 )) usecdecl = 1;
1606 else if (strncmp( "p_", profile, 2 ))
1608 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1609 return;
1612 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
1613 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
1614 else if (!strncmp( "intr_", profile + 2, 5 )) reg_func = 2;
1615 else if (strncmp( "long_", profile + 2, 5 ))
1617 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1618 return;
1621 for ( i = 0; args[i]; i++ )
1622 switch ( args[i] )
1624 case 'w': /* word */
1625 case 's': /* s_word */
1626 argsize += 2;
1627 break;
1629 case 'l': /* long or segmented pointer */
1630 case 'T': /* segmented pointer to null-terminated string */
1631 case 'p': /* linear pointer */
1632 case 't': /* linear pointer to null-terminated string */
1633 argsize += 4;
1634 break;
1637 ret_type = reg_func? "void" : short_ret? "WORD" : "LONG";
1639 fprintf( outfile, "%s%s WINAPI %s_CallFrom16_%s( FARPROC proc, LPBYTE args%s )\n{\n",
1640 local? "static " : "", ret_type, prefix, profile,
1641 reg_func? ", struct _CONTEXT86 *context" : "" );
1643 fprintf( outfile, " %s((%s WINAPI (*)( ",
1644 reg_func? "" : "return ", ret_type );
1645 args = profile + 7;
1646 for ( i = 0; args[i]; i++ )
1648 if ( i ) fprintf( outfile, ", " );
1649 switch (args[i])
1651 case 'w': fprintf( outfile, "WORD" ); break;
1652 case 's': fprintf( outfile, "INT16" ); break;
1653 case 'l': case 'T': fprintf( outfile, "LONG" ); break;
1654 case 'p': case 't': fprintf( outfile, "LPVOID" ); break;
1657 if ( reg_func )
1658 fprintf( outfile, "%sstruct _CONTEXT86 *", i? ", " : "" );
1659 else if ( !i )
1660 fprintf( outfile, "void" );
1661 fprintf( outfile, " )) proc) (\n" );
1662 args = profile + 7;
1663 pos = !usecdecl? argsize : 0;
1664 for ( i = 0; args[i]; i++ )
1666 if ( i ) fprintf( outfile, ",\n" );
1667 fprintf( outfile, " " );
1668 switch (args[i])
1670 case 'w': /* word */
1671 if ( !usecdecl ) pos -= 2;
1672 fprintf( outfile, "*(WORD *)(args+%d)", pos );
1673 if ( usecdecl ) pos += 2;
1674 break;
1676 case 's': /* s_word */
1677 if ( !usecdecl ) pos -= 2;
1678 fprintf( outfile, "*(INT16 *)(args+%d)", pos );
1679 if ( usecdecl ) pos += 2;
1680 break;
1682 case 'l': /* long or segmented pointer */
1683 case 'T': /* segmented pointer to null-terminated string */
1684 if ( !usecdecl ) pos -= 4;
1685 fprintf( outfile, "*(LONG *)(args+%d)", pos );
1686 if ( usecdecl ) pos += 4;
1687 break;
1689 case 'p': /* linear pointer */
1690 case 't': /* linear pointer to null-terminated string */
1691 if ( !usecdecl ) pos -= 4;
1692 fprintf( outfile, "PTR_SEG_TO_LIN( *(SEGPTR *)(args+%d) )", pos );
1693 if ( usecdecl ) pos += 4;
1694 break;
1696 default:
1697 fprintf( stderr, "Unknown arg type '%c'\n", args[i] );
1700 if ( reg_func )
1701 fprintf( outfile, "%s context", i? ",\n" : "" );
1702 fprintf( outfile, " );\n}\n\n" );
1705 /*******************************************************************
1706 * BuildCallTo16Func
1708 * Build a Wine-to-16-bit callback glue function.
1710 * Prototypes for the CallTo16 functions:
1711 * extern WORD CALLBACK PREFIX_CallTo16_word_xxx( FARPROC16 func, args... );
1712 * extern LONG CALLBACK PREFIX_CallTo16_long_xxx( FARPROC16 func, args... );
1714 * These routines are provided solely for convenience; they simply
1715 * write the arguments onto the 16-bit stack, and call the appropriate
1716 * CallTo16... core routine.
1718 * If you have more sophisticated argument conversion requirements than
1719 * are provided by these routines, you might as well call the core
1720 * routines by yourself.
1723 static void BuildCallTo16Func( FILE *outfile, char *profile, char *prefix )
1725 char *args = profile + 5;
1726 int i, argsize = 0, short_ret = 0;
1728 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1729 else if (strncmp( "long_", profile, 5 ))
1731 fprintf( stderr, "Invalid function name '%s'.\n", profile );
1732 exit(1);
1735 fprintf( outfile, "%s %s_CallTo16_%s( FARPROC16 proc",
1736 short_ret? "WORD" : "LONG", prefix, profile );
1737 args = profile + 5;
1738 for ( i = 0; args[i]; i++ )
1740 fprintf( outfile, ", " );
1741 switch (args[i])
1743 case 'w': fprintf( outfile, "WORD" ); argsize += 2; break;
1744 case 'l': fprintf( outfile, "LONG" ); argsize += 4; break;
1746 fprintf( outfile, " arg%d", i+1 );
1748 fprintf( outfile, " )\n{\n" );
1750 if ( argsize > 0 )
1751 fprintf( outfile, " LPBYTE args = (LPBYTE)CURRENT_STACK16;\n" );
1753 args = profile + 5;
1754 for ( i = 0; args[i]; i++ )
1756 fprintf( outfile, " *--(" );
1757 switch (args[i])
1759 case 'w': fprintf( outfile, "WORD" ); break;
1760 case 'l': fprintf( outfile, "LONG" ); break;
1761 default: fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
1762 args[i] );
1764 fprintf( outfile, " *)args = arg%d;\n", i+1 );
1767 fprintf( outfile, " return CallTo16%s( proc, %d );\n}\n\n",
1768 short_ret? "Word" : "Long", argsize );
1773 /*******************************************************************
1774 * BuildCallFrom16Core
1776 * This routine builds the core routines used in 16->32 thunks:
1777 * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
1779 * These routines are intended to be called via a far call (with 32-bit
1780 * operand size) from 16-bit code. The 16-bit code stub must push %bp,
1781 * the 32-bit entry point to be called, and the argument conversion
1782 * routine to be used (see stack layout below).
1784 * The core routine completes the STACK16FRAME on the 16-bit stack and
1785 * switches to the 32-bit stack. Then, the argument conversion routine
1786 * is called; it gets passed the 32-bit entry point and a pointer to the
1787 * 16-bit arguments (on the 16-bit stack) as parameters. (You can either
1788 * use conversion routines automatically generated by BuildCallFrom16,
1789 * or write your own for special purposes.)
1791 * The conversion routine must call the 32-bit entry point, passing it
1792 * the converted arguments, and return its return value to the core.
1793 * After the conversion routine has returned, the core switches back
1794 * to the 16-bit stack, converts the return value to the DX:AX format
1795 * (CallFrom16Long), and returns to the 16-bit call stub. All parameters,
1796 * including %bp, are popped off the stack.
1798 * The 16-bit call stub now returns to the caller, popping the 16-bit
1799 * arguments if necessary (pascal calling convention).
1801 * In the case of a 'register' function, CallFrom16Register fills a
1802 * CONTEXT86 structure with the values all registers had at the point
1803 * the first instruction of the 16-bit call stub was about to be
1804 * executed. A pointer to this CONTEXT86 is passed as third parameter
1805 * to the argument conversion routine, which typically passes it on
1806 * to the called 32-bit entry point.
1808 * CallFrom16Thunk is a special variant used by the implementation of
1809 * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
1810 * implemented as follows:
1811 * On entry, the EBX register is set up to contain a flat pointer to the
1812 * 16-bit stack such that EBX+22 points to the first argument.
1813 * Then, the entry point is called, while EBP is set up to point
1814 * to the return address (on the 32-bit stack).
1815 * The called function returns with CX set to the number of bytes
1816 * to be popped of the caller's stack.
1818 * Stack layout upon entry to the core routine (STACK16FRAME):
1819 * ... ...
1820 * (sp+24) word first 16-bit arg
1821 * (sp+22) word cs
1822 * (sp+20) word ip
1823 * (sp+18) word bp
1824 * (sp+14) long 32-bit entry point (reused for Win16 mutex recursion count)
1825 * (sp+12) word ip of actual entry point (necessary for relay debugging)
1826 * (sp+8) long relay (argument conversion) function entry point
1827 * (sp+4) long cs of 16-bit entry point
1828 * (sp) long ip of 16-bit entry point
1830 * Added on the stack:
1831 * (sp-2) word saved gs
1832 * (sp-4) word saved fs
1833 * (sp-6) word saved es
1834 * (sp-8) word saved ds
1835 * (sp-12) long saved ebp
1836 * (sp-16) long saved ecx
1837 * (sp-20) long saved edx
1838 * (sp-24) long saved previous stack
1840 static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk, int short_ret )
1842 char *name = thunk? "Thunk" : reg_func? "Register" : short_ret? "Word" : "Long";
1844 /* Function header */
1845 fprintf( outfile, "\n\t.align 4\n" );
1846 #ifdef USE_STABS
1847 fprintf( outfile, ".stabs \"CallFrom16%s:F1\",36,0,0," PREFIX "CallFrom16%s\n",
1848 name, name);
1849 #endif
1850 fprintf( outfile, "\t.globl " PREFIX "CallFrom16%s\n", name );
1851 fprintf( outfile, PREFIX "CallFrom16%s:\n", name );
1853 /* Create STACK16FRAME (except STACK32FRAME link) */
1854 fprintf( outfile, "\tpushw %%gs\n" );
1855 fprintf( outfile, "\tpushw %%fs\n" );
1856 fprintf( outfile, "\tpushw %%es\n" );
1857 fprintf( outfile, "\tpushw %%ds\n" );
1858 fprintf( outfile, "\tpushl %%ebp\n" );
1859 fprintf( outfile, "\tpushl %%ecx\n" );
1860 fprintf( outfile, "\tpushl %%edx\n" );
1862 if ( UsePIC )
1864 /* Get Global Offset Table into %ecx */
1865 fprintf( outfile, "\tcall .LCallFrom16%s.getgot1\n", name );
1866 fprintf( outfile, ".LCallFrom16%s.getgot1:\n", name );
1867 fprintf( outfile, "\tpopl %%ecx\n" );
1868 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot1], %%ecx\n", name );
1871 /* Load 32-bit segment registers */
1872 fprintf( outfile, "\tmovw $0x%04x, %%dx\n", Data_Selector );
1873 #ifdef __svr4__
1874 fprintf( outfile, "\tdata16\n");
1875 #endif
1876 fprintf( outfile, "\tmovw %%dx, %%ds\n" );
1877 #ifdef __svr4__
1878 fprintf( outfile, "\tdata16\n");
1879 #endif
1880 fprintf( outfile, "\tmovw %%dx, %%es\n" );
1882 if ( UsePIC )
1884 fprintf( outfile, "\tmovl " PREFIX "SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
1885 fprintf( outfile, "\tmovw (%%edx), %%fs\n" );
1887 else
1888 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb, %%fs\n" );
1890 /* Get address of ldt_copy array into %ecx */
1891 if ( UsePIC )
1892 fprintf( outfile, "\tmovl " PREFIX "ldt_copy@GOT(%%ecx), %%ecx\n" );
1893 else
1894 fprintf( outfile, "\tmovl $" PREFIX "ldt_copy, %%ecx\n" );
1896 /* Translate STACK16FRAME base to flat offset in %edx */
1897 fprintf( outfile, "\tmovw %%ss, %%dx\n" );
1898 fprintf( outfile, "\tandl $0xfff8, %%edx\n" );
1899 fprintf( outfile, "\tmovl (%%ecx,%%edx), %%edx\n" );
1900 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
1901 fprintf( outfile, "\tleal -4(%%ebp,%%edx), %%edx\n" );
1902 /* -4 since STACK16FRAME not yet complete! */
1904 /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
1905 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET );
1906 fprintf( outfile, "\tpushl %%ebp\n" );
1908 /* Switch stacks */
1909 #ifdef __svr4__
1910 fprintf( outfile,"\tdata16\n");
1911 #endif
1912 fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET + 2 );
1913 fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET );
1914 fprintf( outfile, "\tpushl %%ds\n" );
1915 fprintf( outfile, "\tpopl %%ss\n" );
1916 fprintf( outfile, "\tmovl %%ebp, %%esp\n" );
1917 fprintf( outfile, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME, ebp) );
1920 /* At this point:
1921 STACK16FRAME is completely set up
1922 DS, ES, SS: flat data segment
1923 FS: current TEB
1924 ESP: points to last STACK32FRAME
1925 EBP: points to ebp member of last STACK32FRAME
1926 EDX: points to current STACK16FRAME
1927 ECX: points to ldt_copy
1928 all other registers: unchanged */
1930 /* Special case: C16ThkSL stub */
1931 if ( thunk )
1933 /* Set up registers as expected and call thunk */
1934 fprintf( outfile, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME)-22 );
1935 fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
1937 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point) );
1939 /* Switch stack back */
1940 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
1941 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
1942 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
1944 /* Restore registers and return directly to caller */
1945 fprintf( outfile, "\taddl $8, %%esp\n" );
1946 fprintf( outfile, "\tpopl %%ebp\n" );
1947 fprintf( outfile, "\tpopw %%ds\n" );
1948 fprintf( outfile, "\tpopw %%es\n" );
1949 fprintf( outfile, "\tpopw %%fs\n" );
1950 fprintf( outfile, "\tpopw %%gs\n" );
1951 fprintf( outfile, "\taddl $20, %%esp\n" );
1953 fprintf( outfile, "\txorb %%ch, %%ch\n" );
1954 fprintf( outfile, "\tpopl %%ebx\n" );
1955 fprintf( outfile, "\taddw %%cx, %%sp\n" );
1956 fprintf( outfile, "\tpush %%ebx\n" );
1958 fprintf( outfile, "\t.byte 0x66\n" );
1959 fprintf( outfile, "\tlret\n" );
1961 return;
1965 /* Build register CONTEXT */
1966 if ( reg_func )
1968 fprintf( outfile, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86) );
1970 fprintf( outfile, "\tpushfl\n" );
1971 fprintf( outfile, "\tpopl %d(%%esp)\n", CONTEXTOFFSET(EFlags) );
1973 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax) );
1974 fprintf( outfile, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx) );
1975 fprintf( outfile, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi) );
1976 fprintf( outfile, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi) );
1978 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp) );
1979 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp) );
1980 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx) );
1981 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx) );
1982 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx) );
1983 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx) );
1985 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds) );
1986 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs) );
1987 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es) );
1988 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs) );
1989 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs) );
1990 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs) );
1991 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs) );
1992 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs) );
1994 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs) );
1995 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs) );
1996 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip) );
1997 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip) );
1999 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET+2 );
2000 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs) );
2001 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET );
2002 fprintf( outfile, "\taddl $%d, %%eax\n", STACK16OFFSET(ip) );
2003 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp) );
2004 #if 0
2005 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2006 #endif
2008 /* Push address of CONTEXT86 structure -- popped by the relay routine */
2009 fprintf( outfile, "\tpushl %%esp\n" );
2013 /* Print debug info before call */
2014 if ( debugging )
2016 if ( UsePIC )
2018 fprintf( outfile, "\tpushl %%ebx\n" );
2020 /* Get Global Offset Table into %ebx (for PLT call) */
2021 fprintf( outfile, "\tcall .LCallFrom16%s.getgot2\n", name );
2022 fprintf( outfile, ".LCallFrom16%s.getgot2:\n", name );
2023 fprintf( outfile, "\tpopl %%ebx\n" );
2024 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot2], %%ebx\n", name );
2027 fprintf( outfile, "\tpushl %%edx\n" );
2028 if ( reg_func )
2029 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2030 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
2031 else
2032 fprintf( outfile, "\tpushl $0\n" );
2034 if ( UsePIC )
2035 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16@PLT\n ");
2036 else
2037 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n ");
2039 fprintf( outfile, "\tpopl %%edx\n" );
2040 fprintf( outfile, "\tpopl %%edx\n" );
2042 if ( UsePIC )
2043 fprintf( outfile, "\tpopl %%ebx\n" );
2046 /* Call relay routine (which will call the API entry point) */
2047 fprintf( outfile, "\tleal %d(%%edx), %%eax\n", sizeof(STACK16FRAME) );
2048 fprintf( outfile, "\tpushl %%eax\n" );
2049 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point) );
2050 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay) );
2052 /* Print debug info after call */
2053 if ( debugging )
2055 if ( UsePIC )
2057 fprintf( outfile, "\tpushl %%ebx\n" );
2059 /* Get Global Offset Table into %ebx (for PLT call) */
2060 fprintf( outfile, "\tcall .LCallFrom16%s.getgot3\n", name );
2061 fprintf( outfile, ".LCallFrom16%s.getgot3:\n", name );
2062 fprintf( outfile, "\tpopl %%ebx\n" );
2063 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot3], %%ebx\n", name );
2066 fprintf( outfile, "\tpushl %%eax\n" );
2067 if ( reg_func )
2068 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2069 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
2070 else
2071 fprintf( outfile, "\tpushl $0\n" );
2073 if ( UsePIC )
2074 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret@PLT\n ");
2075 else
2076 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n ");
2078 fprintf( outfile, "\tpopl %%eax\n" );
2079 fprintf( outfile, "\tpopl %%eax\n" );
2081 if ( UsePIC )
2082 fprintf( outfile, "\tpopl %%ebx\n" );
2086 if ( reg_func )
2088 fprintf( outfile, "\tmovl %%esp, %%ebx\n" );
2090 /* Switch stack back */
2091 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2092 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
2093 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2095 /* Get return address to CallFrom16 stub */
2096 fprintf( outfile, "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip)-4 );
2097 fprintf( outfile, "\tpopl %%eax\n" );
2098 fprintf( outfile, "\tpopl %%edx\n" );
2100 /* Restore all registers from CONTEXT */
2101 fprintf( outfile, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs) );
2102 fprintf( outfile, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp) );
2103 fprintf( outfile, "\taddl $4, %%esp\n" ); /* room for final return address */
2105 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
2106 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
2107 fprintf( outfile, "\tpushl %%edx\n" );
2108 fprintf( outfile, "\tpushl %%eax\n" );
2109 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags) );
2110 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
2112 fprintf( outfile, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs) );
2113 fprintf( outfile, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs) );
2114 fprintf( outfile, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs) );
2116 fprintf( outfile, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp) );
2117 fprintf( outfile, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi) );
2118 fprintf( outfile, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi) );
2119 fprintf( outfile, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax) );
2120 fprintf( outfile, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx) );
2121 fprintf( outfile, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx) );
2122 fprintf( outfile, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx) );
2124 fprintf( outfile, "\tpopl %%ds\n" );
2125 fprintf( outfile, "\tpopfl\n" );
2126 fprintf( outfile, "\tlret\n" );
2128 else
2130 /* Switch stack back */
2131 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2132 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
2133 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2135 /* Restore registers */
2136 fprintf( outfile, "\tpopl %%edx\n" );
2137 fprintf( outfile, "\tpopl %%ecx\n" );
2138 fprintf( outfile, "\tpopl %%ebp\n" );
2139 fprintf( outfile, "\tpopw %%ds\n" );
2140 fprintf( outfile, "\tpopw %%es\n" );
2141 fprintf( outfile, "\tpopw %%fs\n" );
2142 fprintf( outfile, "\tpopw %%gs\n" );
2144 /* Prepare return value and set flags accordingly */
2145 if ( !short_ret )
2146 fprintf( outfile, "\tshldl $16, %%eax, %%edx\n" );
2147 fprintf( outfile, "\torl %%eax, %%eax\n" );
2149 /* Return to return stub which will return to caller */
2150 fprintf( outfile, "\tlret $12\n" );
2155 /*******************************************************************
2156 * BuildCallTo16Core
2158 * This routine builds the core routines used in 32->16 thunks:
2160 * extern void CALLBACK CallTo16Word( SEGPTR target, int nb_args );
2161 * extern void CALLBACK CallTo16Long( SEGPTR target, int nb_args );
2162 * extern void CALLBACK CallTo16RegisterShort( const CONTEXT86 *context, int nb_args );
2163 * extern void CALLBACK CallTo16RegisterLong ( const CONTEXT86 *context, int nb_args );
2165 * These routines can be called directly from 32-bit code.
2167 * All routines expect that the 16-bit stack contents (arguments) were
2168 * already set up by the caller; nb_args must contain the number of bytes
2169 * to be conserved. The 16-bit SS:SP will be set accordinly.
2171 * All other registers are either taken from the CONTEXT86 structure
2172 * or else set to default values. The target routine address is either
2173 * given directly or taken from the CONTEXT86.
2175 * If you want to call a 16-bit routine taking only standard argument types
2176 * (WORD and LONG), you can also have an appropriate argument conversion
2177 * stub automatically generated (see BuildCallTo16); you'd then call this
2178 * stub, which in turn would prepare the 16-bit stack and call the appropiate
2179 * core routine.
2183 static void BuildCallTo16Core( FILE *outfile, int short_ret, int reg_func )
2185 char *name = reg_func == 2 ? "RegisterLong" :
2186 reg_func == 1 ? "RegisterShort" :
2187 short_ret? "Word" : "Long";
2189 /* Function header */
2190 fprintf( outfile, "\n\t.align 4\n" );
2191 #ifdef USE_STABS
2192 fprintf( outfile, ".stabs \"CallTo16%s:F1\",36,0,0," PREFIX "CallTo16%s\n",
2193 name, name);
2194 #endif
2195 fprintf( outfile, "\t.globl " PREFIX "CallTo16%s\n", name );
2196 fprintf( outfile, PREFIX "CallTo16%s:\n", name );
2198 /* Function entry sequence */
2199 fprintf( outfile, "\tpushl %%ebp\n" );
2200 fprintf( outfile, "\tmovl %%esp, %%ebp\n" );
2202 /* Save the 32-bit registers */
2203 fprintf( outfile, "\tpushl %%ebx\n" );
2204 fprintf( outfile, "\tpushl %%ecx\n" );
2205 fprintf( outfile, "\tpushl %%edx\n" );
2206 fprintf( outfile, "\tpushl %%esi\n" );
2207 fprintf( outfile, "\tpushl %%edi\n" );
2209 if ( UsePIC )
2211 /* Get Global Offset Table into %ebx */
2212 fprintf( outfile, "\tcall .LCallTo16%s.getgot1\n", name );
2213 fprintf( outfile, ".LCallTo16%s.getgot1:\n", name );
2214 fprintf( outfile, "\tpopl %%ebx\n" );
2215 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot1], %%ebx\n", name );
2218 /* Enter Win16 Mutex */
2219 if ( UsePIC )
2220 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock@PLT\n" );
2221 else
2222 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock\n" );
2224 /* Print debugging info */
2225 if (debugging)
2227 /* Push flags, number of arguments, and target */
2228 fprintf( outfile, "\tpushl $%d\n", reg_func );
2229 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2230 fprintf( outfile, "\tpushl 8(%%ebp)\n" );
2232 if ( UsePIC )
2233 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16@PLT\n" );
2234 else
2235 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
2237 fprintf( outfile, "\taddl $12, %%esp\n" );
2240 /* Get return address */
2241 if ( UsePIC )
2242 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr@GOTOFF(%%ebx), %%ecx\n" );
2243 else
2244 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr, %%ecx\n" );
2246 /* Call the actual CallTo16 routine (simulate a lcall) */
2247 fprintf( outfile, "\tpushl %%cs\n" );
2248 fprintf( outfile, "\tcall .LCallTo16%s\n", name );
2250 /* Convert and push return value */
2251 if ( short_ret )
2253 fprintf( outfile, "\tmovzwl %%ax, %%eax\n" );
2254 fprintf( outfile, "\tpushl %%eax\n" );
2256 else if ( reg_func != 2 )
2258 fprintf( outfile, "\tshll $16,%%edx\n" );
2259 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
2260 fprintf( outfile, "\tpushl %%edx\n" );
2262 else
2263 fprintf( outfile, "\tpushl %%eax\n" );
2265 if ( UsePIC )
2267 /* Get Global Offset Table into %ebx (might have been overwritten) */
2268 fprintf( outfile, "\tcall .LCallTo16%s.getgot2\n", name );
2269 fprintf( outfile, ".LCallTo16%s.getgot2:\n", name );
2270 fprintf( outfile, "\tpopl %%ebx\n" );
2271 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot2], %%ebx\n", name );
2274 /* Print debugging info */
2275 if (debugging)
2277 if ( UsePIC )
2278 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret@PLT\n" );
2279 else
2280 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret\n" );
2283 /* Leave Win16 Mutex */
2284 if ( UsePIC )
2285 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock@PLT\n" );
2286 else
2287 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock\n" );
2289 /* Get return value */
2290 fprintf( outfile, "\tpopl %%eax\n" );
2292 /* Restore the 32-bit registers */
2293 fprintf( outfile, "\tpopl %%edi\n" );
2294 fprintf( outfile, "\tpopl %%esi\n" );
2295 fprintf( outfile, "\tpopl %%edx\n" );
2296 fprintf( outfile, "\tpopl %%ecx\n" );
2297 fprintf( outfile, "\tpopl %%ebx\n" );
2299 /* Function exit sequence */
2300 fprintf( outfile, "\tpopl %%ebp\n" );
2301 fprintf( outfile, "\tret $8\n" );
2304 /* Start of the actual CallTo16 routine */
2306 fprintf( outfile, ".LCallTo16%s:\n", name );
2308 /* Complete STACK32FRAME */
2309 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
2310 fprintf( outfile, "\tmovl %%esp,%%edx\n" );
2312 /* Switch to the 16-bit stack */
2313 #ifdef __svr4__
2314 fprintf( outfile,"\tdata16\n");
2315 #endif
2316 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
2317 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
2318 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
2320 /* Make %bp point to the previous stackframe (built by CallFrom16) */
2321 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
2322 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp) );
2324 /* Add the specified offset to the new sp */
2325 fprintf( outfile, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args) );
2327 /* Push the return address
2328 * With sreg suffix, we push 16:16 address (normal lret)
2329 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
2331 if (reg_func != 2)
2332 fprintf( outfile, "\tpushl %%ecx\n" );
2333 else
2335 fprintf( outfile, "\tshldl $16, %%ecx, %%eax\n" );
2336 fprintf( outfile, "\tpushw $0\n" );
2337 fprintf( outfile, "\tpushw %%ax\n" );
2338 fprintf( outfile, "\tpushw $0\n" );
2339 fprintf( outfile, "\tpushw %%cx\n" );
2342 if (reg_func)
2344 /* Push the called routine address */
2345 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target) );
2346 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs) );
2347 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip) );
2349 /* Get the registers */
2350 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs) );
2351 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs) );
2352 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2353 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs) );
2354 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2355 fprintf( outfile, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp) );
2356 fprintf( outfile, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi) );
2357 fprintf( outfile, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi) );
2358 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax) );
2359 fprintf( outfile, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx) );
2360 fprintf( outfile, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx) );
2361 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx) );
2363 /* Get the 16-bit ds */
2364 fprintf( outfile, "\tpopw %%ds\n" );
2366 else /* not a register function */
2368 /* Push the called routine address */
2369 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK32OFFSET(target) );
2371 /* Set %fs to the value saved by the last CallFrom16 */
2372 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs)-STACK16OFFSET(bp) );
2373 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2375 /* Set %ds and %es (and %ax just in case) equal to %ss */
2376 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
2377 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2378 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2381 /* Jump to the called routine */
2382 fprintf( outfile, "\t.byte 0x66\n" );
2383 fprintf( outfile, "\tlret\n" );
2386 /*******************************************************************
2387 * BuildRet16Func
2389 * Build the return code for 16-bit callbacks
2391 static void BuildRet16Func( FILE *outfile )
2394 * Note: This must reside in the .data section to allow
2395 * run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
2398 fprintf( outfile, "\n\t.globl " PREFIX "CallTo16_Ret\n" );
2399 fprintf( outfile, PREFIX "CallTo16_Ret:\n" );
2401 /* Restore 32-bit segment registers */
2403 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
2404 #ifdef __svr4__
2405 fprintf( outfile, "\tdata16\n");
2406 #endif
2407 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
2408 #ifdef __svr4__
2409 fprintf( outfile, "\tdata16\n");
2410 #endif
2411 fprintf( outfile, "\tmovw %%bx,%%es\n" );
2413 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
2415 /* Restore the 32-bit stack */
2417 #ifdef __svr4__
2418 fprintf( outfile, "\tdata16\n");
2419 #endif
2420 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
2421 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
2422 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2424 /* Return to caller */
2426 fprintf( outfile, "\tlret\n" );
2428 /* Declare the return address variable */
2430 fprintf( outfile, "\n\t.globl " PREFIX "CallTo16_RetAddr\n" );
2431 fprintf( outfile, PREFIX "CallTo16_RetAddr:\t.long 0\n" );
2434 /*******************************************************************
2435 * BuildCallTo32CBClient
2437 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
2439 * Since the relay stub is itself 32-bit, this should not be a problem;
2440 * unfortunately, the relay stubs are expected to switch back to a
2441 * 16-bit stack (and 16-bit code) after completion :-(
2443 * This would conflict with our 16- vs. 32-bit stack handling, so
2444 * we simply switch *back* to our 32-bit stack before returning to
2445 * the caller ...
2447 * The CBClient relay stub expects to be called with the following
2448 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
2449 * stack at the designated places:
2451 * ...
2452 * (ebp+14) original arguments to the callback routine
2453 * (ebp+10) far return address to original caller
2454 * (ebp+6) Thunklet target address
2455 * (ebp+2) Thunklet relay ID code
2456 * (ebp) BP (saved by CBClientGlueSL)
2457 * (ebp-2) SI (saved by CBClientGlueSL)
2458 * (ebp-4) DI (saved by CBClientGlueSL)
2459 * (ebp-6) DS (saved by CBClientGlueSL)
2461 * ... buffer space used by the 16-bit side glue for temp copies
2463 * (ebx+4) far return address to 16-bit side glue code
2464 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
2466 * The 32-bit side glue code accesses both the original arguments (via ebp)
2467 * and the temporary copies prepared by the 16-bit side glue (via ebx).
2468 * After completion, the stub will load ss:sp from the buffer at ebx
2469 * and perform a far return to 16-bit code.
2471 * To trick the relay stub into returning to us, we replace the 16-bit
2472 * return address to the glue code by a cs:ip pair pointing to our
2473 * return entry point (the original return address is saved first).
2474 * Our return stub thus called will then reload the 32-bit ss:esp and
2475 * return to 32-bit code (by using and ss:esp value that we have also
2476 * pushed onto the 16-bit stack before and a cs:eip values found at
2477 * that position on the 32-bit stack). The ss:esp to be restored is
2478 * found relative to the 16-bit stack pointer at:
2480 * (ebx-4) ss (flat)
2481 * (ebx-8) sp (32-bit stack pointer)
2483 * The second variant of this routine, CALL32_CBClientEx, which is used
2484 * to implement KERNEL.621, has to cope with yet another problem: Here,
2485 * the 32-bit side directly returns to the caller of the CBClient thunklet,
2486 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
2487 * As we have to return to our 32-bit code first, we have to adapt the
2488 * layout of our temporary area so as to include values for the registers
2489 * that are to be restored, and later (in the implementation of KERNEL.621)
2490 * we *really* restore them. The return stub restores DS, DI, SI, and BP
2491 * from the stack, skips the next 8 bytes (CBClient relay code / target),
2492 * and then performs a lret NN, where NN is the number of arguments to be
2493 * removed. Thus, we prepare our temporary area as follows:
2495 * (ebx+22) 16-bit cs (this segment)
2496 * (ebx+20) 16-bit ip ('16-bit' return entry point)
2497 * (ebx+16) 32-bit ss (flat)
2498 * (ebx+12) 32-bit sp (32-bit stack pointer)
2499 * (ebx+10) 16-bit bp (points to ebx+24)
2500 * (ebx+8) 16-bit si (ignored)
2501 * (ebx+6) 16-bit di (ignored)
2502 * (ebx+4) 16-bit ds (we actually use the flat DS here)
2503 * (ebx+2) 16-bit ss (16-bit stack segment)
2504 * (ebx+0) 16-bit sp (points to ebx+4)
2506 * Note that we ensure that DS is not changed and remains the flat segment,
2507 * and the 32-bit stack pointer our own return stub needs fits just
2508 * perfectly into the 8 bytes that are skipped by the Windows stub.
2509 * One problem is that we have to determine the number of removed arguments,
2510 * as these have to be really removed in KERNEL.621. Thus, the BP value
2511 * that we place in the temporary area to be restored, contains the value
2512 * that SP would have if no arguments were removed. By comparing the actual
2513 * value of SP with this value in our return stub we can compute the number
2514 * of removed arguments. This is then returned to KERNEL.621.
2516 * The stack layout of this function:
2517 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
2518 * (ebp+16) esi pointer to caller's esi value
2519 * (ebp+12) arg ebp value to be set for relay stub
2520 * (ebp+8) func CBClient relay stub address
2521 * (ebp+4) ret addr
2522 * (ebp) ebp
2524 static void BuildCallTo32CBClient( FILE *outfile, BOOL isEx )
2526 char *name = isEx? "CBClientEx" : "CBClient";
2527 int size = isEx? 24 : 12;
2529 /* Function header */
2531 fprintf( outfile, "\n\t.align 4\n" );
2532 #ifdef USE_STABS
2533 fprintf( outfile, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX "CALL32_%s\n",
2534 name, name );
2535 #endif
2536 fprintf( outfile, "\t.globl " PREFIX "CALL32_%s\n", name );
2537 fprintf( outfile, PREFIX "CALL32_%s:\n", name );
2539 /* Entry code */
2541 fprintf( outfile, "\tpushl %%ebp\n" );
2542 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2543 fprintf( outfile, "\tpushl %%edi\n" );
2544 fprintf( outfile, "\tpushl %%esi\n" );
2545 fprintf( outfile, "\tpushl %%ebx\n" );
2547 /* Get the 16-bit stack */
2549 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET);
2551 /* Convert it to a flat address */
2553 fprintf( outfile, "\tshldl $16,%%ebx,%%eax\n" );
2554 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
2555 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%esi\n" );
2556 fprintf( outfile, "\tmovw %%bx,%%ax\n" );
2557 fprintf( outfile, "\taddl %%eax,%%esi\n" );
2559 /* Allocate temporary area (simulate STACK16_PUSH) */
2561 fprintf( outfile, "\tpushf\n" );
2562 fprintf( outfile, "\tcld\n" );
2563 fprintf( outfile, "\tleal -%d(%%esi), %%edi\n", size );
2564 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2565 fprintf( outfile, "\trep\n\tmovsb\n" );
2566 fprintf( outfile, "\tpopf\n" );
2568 fprintf( outfile, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size, STACKOFFSET );
2570 fprintf( outfile, "\tpushl %%edi\n" ); /* remember address */
2572 /* Set up temporary area */
2574 if ( !isEx )
2576 fprintf( outfile, "\tleal 4(%%edi), %%edi\n" );
2578 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2579 fprintf( outfile, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */
2581 fprintf( outfile, "\tmovl %%ss, %%ax\n" );
2582 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2583 fprintf( outfile, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */
2585 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 + 4 );
2586 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */
2588 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
2589 fprintf( outfile, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */
2591 else
2593 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 );
2594 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" );
2596 fprintf( outfile, "\tmovl %%ds, %%ax\n" );
2597 fprintf( outfile, "\tmovw %%ax, 4(%%edi)\n" );
2599 fprintf( outfile, "\taddl $20, %%ebx\n" );
2600 fprintf( outfile, "\tmovw %%bx, 10(%%edi)\n" );
2602 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2603 fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
2605 fprintf( outfile, "\tmovl %%ss, %%ax\n" );
2606 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2607 fprintf( outfile, "\tmovl %%eax, 16(%%edi)\n" );
2609 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
2610 fprintf( outfile, "\tmovl %%eax, 20(%%edi)\n" );
2613 /* Set up registers and call CBClient relay stub (simulating a far call) */
2615 fprintf( outfile, "\tmovl 16(%%ebp), %%esi\n" );
2616 fprintf( outfile, "\tmovl (%%esi), %%esi\n" );
2618 fprintf( outfile, "\tmovl %%edi, %%ebx\n" );
2619 fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
2620 fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
2622 fprintf( outfile, "\tpushl %%cs\n" );
2623 fprintf( outfile, "\tcall *%%eax\n" );
2625 /* Return new esi value to caller */
2627 fprintf( outfile, "\tmovl 32(%%esp), %%edi\n" );
2628 fprintf( outfile, "\tmovl %%esi, (%%edi)\n" );
2630 /* Cleanup temporary area (simulate STACK16_POP) */
2632 fprintf( outfile, "\tpop %%esi\n" );
2634 fprintf( outfile, "\tpushf\n" );
2635 fprintf( outfile, "\tstd\n" );
2636 fprintf( outfile, "\tdec %%esi\n" );
2637 fprintf( outfile, "\tleal %d(%%esi), %%edi\n", size );
2638 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2639 fprintf( outfile, "\trep\n\tmovsb\n" );
2640 fprintf( outfile, "\tpopf\n" );
2642 fprintf( outfile, "\t.byte 0x64\n\taddw $%d,(%d)\n", size, STACKOFFSET );
2644 /* Return argument size to caller */
2645 if ( isEx )
2647 fprintf( outfile, "\tmovl 32(%%esp), %%ebx\n" );
2648 fprintf( outfile, "\tmovl %%ebp, (%%ebx)\n" );
2651 /* Restore registers and return */
2653 fprintf( outfile, "\tpopl %%ebx\n" );
2654 fprintf( outfile, "\tpopl %%esi\n" );
2655 fprintf( outfile, "\tpopl %%edi\n" );
2656 fprintf( outfile, "\tpopl %%ebp\n" );
2657 fprintf( outfile, "\tret\n" );
2660 static void BuildCallTo32CBClientRet( FILE *outfile, BOOL isEx )
2662 char *name = isEx? "CBClientEx" : "CBClient";
2664 /* '16-bit' return stub */
2666 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_Ret\n", name );
2667 fprintf( outfile, PREFIX "CALL32_%s_Ret:\n", name );
2669 if ( !isEx )
2671 fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
2672 fprintf( outfile, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
2674 else
2676 fprintf( outfile, "\tmovzwl %%bp, %%ebx\n" );
2677 fprintf( outfile, "\tsubw %%bp, %%sp\n" );
2678 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
2679 fprintf( outfile, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
2681 fprintf( outfile, "\tlret\n" );
2683 /* Declare the return address variable */
2685 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_RetAddr\n", name );
2686 fprintf( outfile, PREFIX "CALL32_%s_RetAddr:\t.long 0\n", name );
2690 /*******************************************************************
2691 * BuildCallTo32LargeStack
2693 * Build the function used to switch to the original 32-bit stack
2694 * before calling a 32-bit function from 32-bit code. This is used for
2695 * functions that need a large stack, like X bitmaps functions.
2697 * The generated function has the following prototype:
2698 * int xxx( int (*func)(), void *arg );
2700 * The pointer to the function can be retrieved by calling CALL32_Init,
2701 * which also takes care of saving the current 32-bit stack pointer.
2702 * Furthermore, CALL32_Init switches to a new stack and jumps to the
2703 * specified target address.
2705 * NOTE: The CALL32_LargeStack routine may be recursively entered by the
2706 * same thread, but not concurrently entered by several threads.
2708 * Stack layout of CALL32_Init:
2710 * (esp+12) new stack address
2711 * (esp+8) target address
2712 * (esp+4) pointer to variable to receive CALL32_LargeStack address
2713 * (esp) ret addr
2715 * Stack layout of CALL32_LargeStack:
2716 * ... ...
2717 * (ebp+12) arg
2718 * (ebp+8) func
2719 * (ebp+4) ret addr
2720 * (ebp) ebp
2722 static void BuildCallTo32LargeStack( FILE *outfile )
2724 /* Initialization function */
2726 fprintf( outfile, "\n\t.align 4\n" );
2727 #ifdef USE_STABS
2728 fprintf( outfile, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX "CALL32_Init\n");
2729 #endif
2730 fprintf( outfile, "\t.globl " PREFIX "CALL32_Init\n" );
2731 fprintf( outfile, "\t.type " PREFIX "CALL32_Init,@function\n" );
2732 fprintf( outfile, PREFIX "CALL32_Init:\n" );
2733 fprintf( outfile, "\tmovl %%esp,CALL32_Original32_esp\n" );
2734 fprintf( outfile, "\tpopl %%eax\n" );
2735 fprintf( outfile, "\tpopl %%eax\n" );
2736 fprintf( outfile, "\tmovl $CALL32_LargeStack,(%%eax)\n" );
2737 fprintf( outfile, "\tpopl %%eax\n" );
2738 fprintf( outfile, "\tpopl %%esp\n" );
2739 fprintf( outfile, "\tpushl %%eax\n" );
2740 fprintf( outfile, "\tret\n" );
2742 /* Function header */
2744 fprintf( outfile, "\n\t.align 4\n" );
2745 #ifdef USE_STABS
2746 fprintf( outfile, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2747 #endif
2748 fprintf( outfile, "CALL32_LargeStack:\n" );
2750 /* Entry code */
2752 fprintf( outfile, "\tpushl %%ebp\n" );
2753 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2755 /* Switch to the original 32-bit stack pointer */
2757 fprintf( outfile, "\tcmpl $0, CALL32_RecursionCount\n" );
2758 fprintf( outfile, "\tjne CALL32_skip\n" );
2759 fprintf( outfile, "\tmovl CALL32_Original32_esp, %%esp\n" );
2760 fprintf( outfile, "CALL32_skip:\n" );
2762 fprintf( outfile, "\tincl CALL32_RecursionCount\n" );
2764 /* Transfer the argument and call the function */
2766 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2767 fprintf( outfile, "\tcall *8(%%ebp)\n" );
2769 /* Restore registers and return */
2771 fprintf( outfile, "\tdecl CALL32_RecursionCount\n" );
2773 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2774 fprintf( outfile, "\tpopl %%ebp\n" );
2775 fprintf( outfile, "\tret\n" );
2777 /* Data */
2779 fprintf( outfile, "\t.data\n" );
2780 fprintf( outfile, "CALL32_Original32_esp:\t.long 0\n" );
2781 fprintf( outfile, "CALL32_RecursionCount:\t.long 0\n" );
2782 fprintf( outfile, "\t.text\n" );
2786 /*******************************************************************
2787 * BuildCallFrom32Regs
2789 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2790 * 'args' is the number of dword arguments.
2792 * Stack layout:
2793 * ...
2794 * (ebp+12) first arg
2795 * (ebp+8) ret addr to user code
2796 * (ebp+4) ret addr to relay code
2797 * (ebp+0) saved ebp
2798 * (ebp-128) buffer area to allow stack frame manipulation
2799 * (ebp-332) CONTEXT86 struct
2800 * (ebp-336) CONTEXT86 *argument
2801 * .... other arguments copied from (ebp+12)
2803 * The entry point routine is called with a CONTEXT* extra argument,
2804 * following the normal args. In this context structure, EIP_reg
2805 * contains the return address to user code, and ESP_reg the stack
2806 * pointer on return (with the return address and arguments already
2807 * removed).
2809 static void BuildCallFrom32Regs( FILE *outfile )
2811 static const int STACK_SPACE = 128 + sizeof(CONTEXT86);
2813 /* Function header */
2815 fprintf( outfile, "\n\t.align 4\n" );
2816 #ifdef USE_STABS
2817 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
2818 #endif
2819 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
2820 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
2822 /* Allocate some buffer space on the stack */
2824 fprintf( outfile, "\tpushl %%ebp\n" );
2825 fprintf( outfile, "\tmovl %%esp,%%ebp\n ");
2826 fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
2828 /* Build the context structure */
2830 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
2831 fprintf( outfile, "\tpushfl\n" );
2832 fprintf( outfile, "\tpopl %%eax\n" );
2833 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
2834 fprintf( outfile, "\tmovl 0(%%ebp),%%eax\n" );
2835 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
2836 fprintf( outfile, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
2837 fprintf( outfile, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
2838 fprintf( outfile, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
2839 fprintf( outfile, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
2840 fprintf( outfile, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
2842 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2843 fprintf( outfile, "\tmovw %%cs,%%ax\n" );
2844 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs) - STACK_SPACE );
2845 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2846 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
2847 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2848 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
2849 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2850 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
2851 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
2852 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs) - STACK_SPACE );
2853 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2854 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs) - STACK_SPACE );
2855 fprintf( outfile, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
2857 fprintf( outfile, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL );
2858 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags) - STACK_SPACE );
2860 fprintf( outfile, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
2861 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
2863 /* Transfer the arguments */
2865 fprintf( outfile, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
2866 fprintf( outfile, "\tpushl %%esp\n" ); /* push ptr to context struct */
2867 fprintf( outfile, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
2868 fprintf( outfile, "\tjecxz 1f\n" );
2869 fprintf( outfile, "\tsubl %%ecx,%%esp\n" );
2870 fprintf( outfile, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */
2871 fprintf( outfile, "\tmovl %%esp,%%edi\n" );
2872 fprintf( outfile, "\tshrl $2,%%ecx\n" );
2873 fprintf( outfile, "\tcld\n" );
2874 fprintf( outfile, "\trep\n\tmovsl\n" ); /* copy args */
2876 fprintf( outfile, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
2877 fprintf( outfile, "\tleal 12(%%ebp,%%eax),%%eax\n" );
2878 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2880 /* Call the entry point */
2882 fprintf( outfile, "\tcall *0(%%ebx)\n" );
2884 /* Store %eip and %ebp onto the new stack */
2886 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2887 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
2888 fprintf( outfile, "\tmovl %%eax,-4(%%edx)\n" );
2889 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
2890 fprintf( outfile, "\tmovl %%eax,-8(%%edx)\n" );
2892 /* Restore the context structure */
2894 /* Note: we don't bother to restore %cs, %ds and %ss
2895 * changing them in 32-bit code is a recipe for disaster anyway
2897 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
2898 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2899 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
2900 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2901 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
2902 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2904 fprintf( outfile, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
2905 fprintf( outfile, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
2906 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
2907 fprintf( outfile, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
2908 fprintf( outfile, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
2910 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
2911 fprintf( outfile, "\tpushl %%eax\n" );
2912 fprintf( outfile, "\tpopfl\n" );
2913 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
2915 fprintf( outfile, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2916 fprintf( outfile, "\tleal -8(%%ebp),%%esp\n" );
2917 fprintf( outfile, "\tpopl %%ebp\n" );
2918 fprintf( outfile, "\tret\n" );
2922 /*******************************************************************
2923 * BuildSpec
2925 * Build the spec files
2927 static int BuildSpec( FILE *outfile, int argc, char *argv[] )
2929 int i;
2930 for (i = 2; i < argc; i++)
2931 if (BuildSpecFile( outfile, argv[i] ) < 0) return -1;
2932 return 0;
2935 /*******************************************************************
2936 * BuildGlue
2938 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
2940 static int BuildGlue( FILE *outfile, char * outname, int argc, char *argv[] )
2942 char buffer[1024];
2943 FILE *infile;
2945 if (argc > 2)
2947 infile = fopen( argv[2], "r" );
2948 if (!infile)
2950 perror( argv[2] );
2951 exit( 1 );
2954 else infile = stdin;
2956 /* File header */
2958 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
2959 argc > 2? argv[2] : "<stdin>" );
2960 fprintf( outfile, "#include \"builtin16.h\"\n" );
2961 fprintf( outfile, "#include \"stackframe.h\"\n\n" );
2963 /* Build the callback glue functions */
2965 while (fgets( buffer, sizeof(buffer), infile ))
2967 if (strstr( buffer, "### start build ###" )) break;
2969 while (fgets( buffer, sizeof(buffer), infile ))
2971 char *p;
2972 if ( (p = strstr( buffer, "CallFrom16_" )) != NULL )
2974 char *q, *profile = p + strlen( "CallFrom16_" );
2975 for (q = profile; (*q == '_') || isalpha(*q); q++ )
2977 *q = '\0';
2978 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
2980 if ( ++q < p ) p[-1] = '\0'; else q = "";
2981 BuildCallFrom16Func( outfile, profile, q, FALSE );
2983 if ( (p = strstr( buffer, "CallTo16_" )) != NULL )
2985 char *q, *profile = p + strlen( "CallTo16_" );
2986 for (q = profile; (*q == '_') || isalpha(*q); q++ )
2988 *q = '\0';
2989 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
2991 if ( ++q < p ) p[-1] = '\0'; else q = "";
2992 BuildCallTo16Func( outfile, profile, q );
2994 if (strstr( buffer, "### stop build ###" )) break;
2997 fclose( infile );
2998 return 0;
3001 /*******************************************************************
3002 * BuildCall16
3004 * Build the 16-bit callbacks
3006 static int BuildCall16( FILE *outfile, char * outname )
3008 #ifdef USE_STABS
3009 char buffer[1024];
3010 #endif
3012 /* File header */
3014 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
3015 fprintf( outfile, "\t.text\n" );
3017 #ifdef __i386__
3019 #ifdef USE_STABS
3020 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
3021 getcwd(buffer, sizeof(buffer));
3024 * The stabs help the internal debugger as they are an indication that it
3025 * is sensible to step into a thunk/trampoline.
3027 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
3028 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
3029 fprintf( outfile, "\t.text\n" );
3030 fprintf( outfile, "\t.align 4\n" );
3031 fprintf( outfile, "Code_Start:\n\n" );
3032 #endif
3033 fprintf( outfile, PREFIX"Call16_Start:\n" );
3034 fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
3035 fprintf( outfile, "\t.byte 0\n\n" );
3038 /* Standard CallFrom16 routine (WORD return) */
3039 BuildCallFrom16Core( outfile, FALSE, FALSE, TRUE );
3041 /* Standard CallFrom16 routine (DWORD return) */
3042 BuildCallFrom16Core( outfile, FALSE, FALSE, FALSE );
3044 /* Register CallFrom16 routine */
3045 BuildCallFrom16Core( outfile, TRUE, FALSE, FALSE );
3047 /* C16ThkSL CallFrom16 routine */
3048 BuildCallFrom16Core( outfile, FALSE, TRUE, FALSE );
3050 /* Standard CallTo16 routine (WORD return) */
3051 BuildCallTo16Core( outfile, TRUE, FALSE );
3053 /* Standard CallTo16 routine (DWORD return) */
3054 BuildCallTo16Core( outfile, FALSE, FALSE );
3056 /* Register CallTo16 routine (16:16 retf) */
3057 BuildCallTo16Core( outfile, FALSE, 1 );
3059 /* Register CallTo16 routine (16:32 retf) */
3060 BuildCallTo16Core( outfile, FALSE, 2 );
3062 /* CBClientThunkSL routine */
3063 BuildCallTo32CBClient( outfile, FALSE );
3065 /* CBClientThunkSLEx routine */
3066 BuildCallTo32CBClient( outfile, TRUE );
3068 fprintf( outfile, PREFIX"Call16_End:\n" );
3069 fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
3071 #ifdef USE_STABS
3072 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
3073 fprintf( outfile, ".Letext:\n");
3074 #endif
3076 /* The whole Call16_Ret segment must lie within the .data section */
3077 fprintf( outfile, "\n\t.data\n" );
3078 fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
3079 fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
3081 /* Standard CallTo16 return stub */
3082 BuildRet16Func( outfile );
3084 /* CBClientThunkSL return stub */
3085 BuildCallTo32CBClientRet( outfile, FALSE );
3087 /* CBClientThunkSLEx return stub */
3088 BuildCallTo32CBClientRet( outfile, TRUE );
3090 /* End of Call16_Ret segment */
3091 fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
3092 fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
3094 #else /* __i386__ */
3096 fprintf( outfile, PREFIX"Call16_Start:\n" );
3097 fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
3098 fprintf( outfile, "\t.byte 0\n\n" );
3099 fprintf( outfile, PREFIX"Call16_End:\n" );
3100 fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
3102 fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
3103 fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
3104 fprintf( outfile, "\t.byte 0\n\n" );
3105 fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
3106 fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
3108 #endif /* __i386__ */
3110 return 0;
3113 /*******************************************************************
3114 * BuildCall32
3116 * Build the 32-bit callbacks
3118 static int BuildCall32( FILE *outfile, char * outname )
3120 #ifdef USE_STABS
3121 char buffer[1024];
3122 #endif
3124 /* File header */
3126 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
3127 fprintf( outfile, "\t.text\n" );
3129 #ifdef __i386__
3131 #ifdef USE_STABS
3132 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
3133 getcwd(buffer, sizeof(buffer));
3136 * The stabs help the internal debugger as they are an indication that it
3137 * is sensible to step into a thunk/trampoline.
3139 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
3140 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
3141 fprintf( outfile, "\t.text\n" );
3142 fprintf( outfile, "\t.align 4\n" );
3143 fprintf( outfile, "Code_Start:\n" );
3144 #endif
3146 /* Build the 32-bit large stack callback */
3148 BuildCallTo32LargeStack( outfile );
3150 /* Build the register callback function */
3152 BuildCallFrom32Regs( outfile );
3154 #ifdef USE_STABS
3155 fprintf( outfile, "\t.text\n");
3156 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
3157 fprintf( outfile, ".Letext:\n");
3158 #endif
3160 #else /* __i386__ */
3162 /* Just to avoid an empty file */
3163 fprintf( outfile, "\t.long 0\n" );
3165 #endif /* __i386__ */
3166 return 0;
3170 /*******************************************************************
3171 * usage
3173 static void usage(void)
3175 fprintf( stderr,
3176 "usage: build [-pic] [-o outfile] -spec SPECNAMES\n"
3177 " build [-pic] [-o outfile] -glue SOURCE_FILE\n"
3178 " build [-pic] [-o outfile] -call16\n"
3179 " build [-pic] [-o outfile] -call32\n" );
3180 exit(1);
3184 /*******************************************************************
3185 * main
3187 int main(int argc, char **argv)
3189 char *outname = NULL;
3190 FILE *outfile = stdout;
3191 int res = -1;
3193 if (argc < 2) usage();
3195 if (!strcmp( argv[1], "-pic" ))
3197 UsePIC = 1;
3198 argv += 1;
3199 argc -= 1;
3200 if (argc < 2) usage();
3203 if (!strcmp( argv[1], "-o" ))
3205 outname = argv[2];
3206 argv += 2;
3207 argc -= 2;
3208 if (argc < 2) usage();
3209 if (!(outfile = fopen( outname, "w" )))
3211 fprintf( stderr, "Unable to create output file '%s'\n", outname );
3212 exit(1);
3216 /* Retrieve the selector values; this assumes that we are building
3217 * the asm files on the platform that will also run them. Probably
3218 * a safe assumption to make.
3220 GET_CS( Code_Selector );
3221 GET_DS( Data_Selector );
3223 if (!strcmp( argv[1], "-spec" ))
3224 res = BuildSpec( outfile, argc, argv );
3225 else if (!strcmp( argv[1], "-glue" ))
3226 res = BuildGlue( outfile, outname, argc, argv );
3227 else if (!strcmp( argv[1], "-call16" ))
3228 res = BuildCall16( outfile, outname );
3229 else if (!strcmp( argv[1], "-call32" ))
3230 res = BuildCall32( outfile, outname );
3231 else
3233 fclose( outfile );
3234 unlink( outname );
3235 usage();
3238 fclose( outfile );
3239 if (res < 0)
3241 unlink( outname );
3242 return 1;
3244 return 0;