Modularized CallFrom/To16 routines. Adapted dependent routines,
[wine/testsucceed.git] / tools / build.c
blobadd3b18563c9dcce6aa5e6b4f82b33f92aab2121
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_RETURN, /* simple return value function (Win16) */
53 TYPE_REGISTER, /* register function */
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 "return", /* TYPE_RETURN */
73 "register", /* TYPE_REGISTER */
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 debugging = 1;
175 /* Offset of a structure field relative to the start of the struct */
176 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
178 /* Offset of register relative to the start of the CONTEXT struct */
179 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT86,reg)
181 /* Offset of register relative to the start of the STACK16FRAME struct */
182 #define STACK16OFFSET(reg) STRUCTOFFSET(STACK16FRAME,reg)
184 /* Offset of register relative to the start of the STACK32FRAME struct */
185 #define STACK32OFFSET(reg) STRUCTOFFSET(STACK32FRAME,reg)
187 /* Offset of the stack pointer relative to %fs:(0) */
188 #define STACKOFFSET (STRUCTOFFSET(TEB,cur_stack))
191 static void *xmalloc (size_t size)
193 void *res;
195 res = malloc (size ? size : 1);
196 if (res == NULL)
198 fprintf (stderr, "Virtual memory exhausted.\n");
199 exit (1);
201 return res;
205 static void *xrealloc (void *ptr, size_t size)
207 void *res = realloc (ptr, size);
208 if (res == NULL)
210 fprintf (stderr, "Virtual memory exhausted.\n");
211 exit (1);
213 return res;
216 static char *xstrdup( const char *str )
218 char *res = strdup( str );
219 if (!res)
221 fprintf (stderr, "Virtual memory exhausted.\n");
222 exit (1);
224 return res;
227 static int IsNumberString(char *s)
229 while (*s != '\0')
230 if (!isdigit(*s++))
231 return 0;
233 return 1;
236 static char *strupper(char *s)
238 char *p;
240 for(p = s; *p != '\0'; p++)
241 *p = toupper(*p);
243 return s;
246 static char * GetTokenInLine(void)
248 char *p;
249 char *token;
251 if (ParseNext != ParseBuffer)
253 if (ParseSaveChar == '\0')
254 return NULL;
255 *ParseNext = ParseSaveChar;
259 * Remove initial white space.
261 for (p = ParseNext; isspace(*p); p++)
264 if ((*p == '\0') || (*p == '#'))
265 return NULL;
268 * Find end of token.
270 token = p++;
271 if (*token != '(' && *token != ')')
272 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
273 p++;
275 ParseSaveChar = *p;
276 ParseNext = p;
277 *p = '\0';
279 return token;
282 static char * GetToken(void)
284 char *token;
286 if (ParseBuffer == NULL)
288 ParseBuffer = xmalloc(512);
289 ParseNext = ParseBuffer;
290 while (1)
292 Line++;
293 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
294 return NULL;
295 if (ParseBuffer[0] != '#')
296 break;
300 while ((token = GetTokenInLine()) == NULL)
302 ParseNext = ParseBuffer;
303 while (1)
305 Line++;
306 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
307 return NULL;
308 if (ParseBuffer[0] != '#')
309 break;
313 return token;
317 /*******************************************************************
318 * ParseVariable
320 * Parse a variable definition.
322 static int ParseVariable( ORDDEF *odp )
324 char *endptr;
325 int *value_array;
326 int n_values;
327 int value_array_size;
329 char *token = GetToken();
330 if (*token != '(')
332 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
333 SpecName, Line, token);
334 return -1;
337 n_values = 0;
338 value_array_size = 25;
339 value_array = xmalloc(sizeof(*value_array) * value_array_size);
341 while ((token = GetToken()) != NULL)
343 if (*token == ')')
344 break;
346 value_array[n_values++] = strtol(token, &endptr, 0);
347 if (n_values == value_array_size)
349 value_array_size += 25;
350 value_array = xrealloc(value_array,
351 sizeof(*value_array) * value_array_size);
354 if (endptr == NULL || *endptr != '\0')
356 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
357 SpecName, Line, token);
358 return -1;
362 if (token == NULL)
364 fprintf(stderr, "%s:%d: End of file in variable declaration\n",
365 SpecName, Line);
366 return -1;
369 odp->u.var.n_values = n_values;
370 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
372 return 0;
376 /*******************************************************************
377 * ParseExportFunction
379 * Parse a function definition.
381 static int ParseExportFunction( ORDDEF *odp )
383 char *token;
384 int i;
386 switch(SpecType)
388 case SPEC_WIN16:
389 if (odp->type == TYPE_STDCALL)
391 fprintf( stderr, "%s:%d: 'stdcall' not supported for Win16\n",
392 SpecName, Line );
393 return -1;
395 break;
396 case SPEC_WIN32:
397 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
399 fprintf( stderr, "%s:%d: 'pascal' not supported for Win32\n",
400 SpecName, Line );
401 return -1;
403 break;
404 default:
405 break;
408 token = GetToken();
409 if (*token != '(')
411 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
412 SpecName, Line, token);
413 return -1;
416 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
418 token = GetToken();
419 if (*token == ')')
420 break;
422 if (!strcmp(token, "word"))
423 odp->u.func.arg_types[i] = 'w';
424 else if (!strcmp(token, "s_word"))
425 odp->u.func.arg_types[i] = 's';
426 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
427 odp->u.func.arg_types[i] = 'l';
428 else if (!strcmp(token, "ptr"))
429 odp->u.func.arg_types[i] = 'p';
430 else if (!strcmp(token, "str"))
431 odp->u.func.arg_types[i] = 't';
432 else if (!strcmp(token, "wstr"))
433 odp->u.func.arg_types[i] = 'W';
434 else if (!strcmp(token, "segstr"))
435 odp->u.func.arg_types[i] = 'T';
436 else if (!strcmp(token, "double"))
438 odp->u.func.arg_types[i++] = 'l';
439 odp->u.func.arg_types[i] = 'l';
441 else
443 fprintf(stderr, "%s:%d: Unknown variable type '%s'\n",
444 SpecName, Line, token);
445 return -1;
447 if (SpecType == SPEC_WIN32)
449 if (strcmp(token, "long") &&
450 strcmp(token, "ptr") &&
451 strcmp(token, "str") &&
452 strcmp(token, "wstr") &&
453 strcmp(token, "double"))
455 fprintf( stderr, "%s:%d: Type '%s' not supported for Win32\n",
456 SpecName, Line, token );
457 return -1;
461 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
463 fprintf( stderr, "%s:%d: Too many arguments\n", SpecName, Line );
464 return -1;
466 odp->u.func.arg_types[i] = '\0';
467 if ((odp->type == TYPE_STDCALL) && !i)
468 odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
469 strcpy(odp->u.func.link_name, GetToken());
470 return 0;
474 /*******************************************************************
475 * ParseEquate
477 * Parse an 'equate' definition.
479 static int ParseEquate( ORDDEF *odp )
481 char *endptr;
483 char *token = GetToken();
484 int value = strtol(token, &endptr, 0);
485 if (endptr == NULL || *endptr != '\0')
487 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
488 SpecName, Line, token);
489 return -1;
492 if (SpecType == SPEC_WIN32)
494 fprintf( stderr, "%s:%d: 'equate' not supported for Win32\n",
495 SpecName, Line );
496 return -1;
499 odp->u.abs.value = value;
500 return 0;
504 /*******************************************************************
505 * ParseReturn
507 * Parse a 'return' definition.
509 static int ParseReturn( ORDDEF *odp )
511 char *token;
512 char *endptr;
514 token = GetToken();
515 odp->u.ret.arg_size = strtol(token, &endptr, 0);
516 if (endptr == NULL || *endptr != '\0')
518 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
519 SpecName, Line, token);
520 return -1;
523 token = GetToken();
524 odp->u.ret.ret_value = strtol(token, &endptr, 0);
525 if (endptr == NULL || *endptr != '\0')
527 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
528 SpecName, Line, token);
529 return -1;
532 if (SpecType == SPEC_WIN32)
534 fprintf( stderr, "%s:%d: 'return' not supported for Win32\n",
535 SpecName, Line );
536 return -1;
539 return 0;
543 /*******************************************************************
544 * ParseStub
546 * Parse a 'stub' definition.
548 static int ParseStub( ORDDEF *odp )
550 odp->u.func.arg_types[0] = '\0';
551 strcpy( odp->u.func.link_name, STUB_CALLBACK );
552 return 0;
556 /*******************************************************************
557 * ParseVarargs
559 * Parse an 'varargs' definition.
561 static int ParseVarargs( ORDDEF *odp )
563 char *token;
565 if (SpecType == SPEC_WIN16)
567 fprintf( stderr, "%s:%d: 'varargs' not supported for Win16\n",
568 SpecName, Line );
569 return -1;
572 token = GetToken();
573 if (*token != '(')
575 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
576 SpecName, Line, token);
577 return -1;
579 token = GetToken();
580 if (*token != ')')
582 fprintf(stderr, "%s:%d: Expected ')' got '%s'\n",
583 SpecName, Line, token);
584 return -1;
587 strcpy( odp->u.vargs.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_ABS:
686 return ParseEquate( odp );
687 case TYPE_RETURN:
688 return ParseReturn( 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 (!DLLInitFunc[0])
753 fprintf(stderr, "%s:%d: Expected function name after init\n", SpecName, Line);
755 else if (strcmp(token, "import") == 0)
757 if (nb_imports >= MAX_IMPORTS)
759 fprintf( stderr, "%s:%d: Too many imports (limit %d)\n",
760 SpecName, Line, MAX_IMPORTS );
761 return -1;
763 if (SpecType != SPEC_WIN32)
765 fprintf( stderr, "%s:%d: Imports not supported for Win16\n", SpecName, Line );
766 return -1;
768 DLLImports[nb_imports++] = xstrdup(GetToken());
770 else if (IsNumberString(token))
772 int ordinal;
773 int rv;
775 ordinal = atoi(token);
776 if ((rv = ParseOrdinal(ordinal)) < 0)
777 return rv;
779 else
781 fprintf(stderr,
782 "%s:%d: Expected name, id, length or ordinal\n",
783 SpecName, Line);
784 return -1;
788 return 0;
792 /*******************************************************************
793 * StoreVariableCode
795 * Store a list of ints into a byte array.
797 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
799 int i;
801 switch(size)
803 case 1:
804 for (i = 0; i < odp->u.var.n_values; i++)
805 buffer[i] = odp->u.var.values[i];
806 break;
807 case 2:
808 for (i = 0; i < odp->u.var.n_values; i++)
809 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
810 break;
811 case 4:
812 for (i = 0; i < odp->u.var.n_values; i++)
813 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
814 break;
816 return odp->u.var.n_values * size;
820 /*******************************************************************
821 * DumpBytes
823 * Dump a byte stream into the assembly code.
825 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
826 const char *label )
828 int i;
830 fprintf( outfile, "\nstatic BYTE %s[] = \n{", label );
832 for (i = 0; i < len; i++)
834 if (!(i & 0x0f)) fprintf( outfile, "\n " );
835 fprintf( outfile, "%d", *data++ );
836 if (i < len - 1) fprintf( outfile, ", " );
838 fprintf( outfile, "\n};\n" );
842 /*******************************************************************
843 * BuildModule16
845 * Build the in-memory representation of a 16-bit NE module, and dump it
846 * as a byte stream into the assembly code.
848 static int BuildModule16( FILE *outfile, int max_code_offset,
849 int max_data_offset )
851 ORDDEF *odp;
852 int i;
853 char *buffer;
854 NE_MODULE *pModule;
855 SEGTABLEENTRY *pSegment;
856 OFSTRUCT *pFileInfo;
857 BYTE *pstr;
858 WORD *pword;
859 ET_BUNDLE *bundle = 0;
860 ET_ENTRY *entry = 0;
862 /* Module layout:
863 * NE_MODULE Module
864 * OFSTRUCT File information
865 * SEGTABLEENTRY Segment 1 (code)
866 * SEGTABLEENTRY Segment 2 (data)
867 * WORD[2] Resource table (empty)
868 * BYTE[2] Imported names (empty)
869 * BYTE[n] Resident names table
870 * BYTE[n] Entry table
873 buffer = xmalloc( 0x10000 );
875 pModule = (NE_MODULE *)buffer;
876 memset( pModule, 0, sizeof(*pModule) );
877 pModule->magic = IMAGE_OS2_SIGNATURE;
878 pModule->count = 1;
879 pModule->next = 0;
880 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
881 pModule->dgroup = 2;
882 pModule->heap_size = DLLHeapSize;
883 pModule->stack_size = 0;
884 pModule->ip = 0;
885 pModule->cs = 0;
886 pModule->sp = 0;
887 pModule->ss = 0;
888 pModule->seg_count = 2;
889 pModule->modref_count = 0;
890 pModule->nrname_size = 0;
891 pModule->modref_table = 0;
892 pModule->nrname_fpos = 0;
893 pModule->moveable_entries = 0;
894 pModule->alignment = 0;
895 pModule->truetype = 0;
896 pModule->os_flags = NE_OSFLAGS_WINDOWS;
897 pModule->misc_flags = 0;
898 pModule->dlls_to_init = 0;
899 pModule->nrname_handle = 0;
900 pModule->min_swap_area = 0;
901 pModule->expected_version = 0x030a;
902 pModule->module32 = 0;
903 pModule->self = 0;
904 pModule->self_loading_sel = 0;
906 /* File information */
908 pFileInfo = (OFSTRUCT *)(pModule + 1);
909 pModule->fileinfo = (int)pFileInfo - (int)pModule;
910 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
911 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
912 + strlen(DLLFileName);
913 strcpy( pFileInfo->szPathName, DLLFileName );
914 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
916 /* Segment table */
918 pSegment = (SEGTABLEENTRY *)pstr;
919 pModule->seg_table = (int)pSegment - (int)pModule;
920 pSegment->filepos = 0;
921 pSegment->size = max_code_offset;
922 pSegment->flags = 0;
923 pSegment->minsize = max_code_offset;
924 pSegment->hSeg = 0;
925 pSegment++;
927 pModule->dgroup_entry = (int)pSegment - (int)pModule;
928 pSegment->filepos = 0;
929 pSegment->size = max_data_offset;
930 pSegment->flags = NE_SEGFLAGS_DATA;
931 pSegment->minsize = max_data_offset;
932 pSegment->hSeg = 0;
933 pSegment++;
935 /* Resource table */
937 pword = (WORD *)pSegment;
938 pModule->res_table = (int)pword - (int)pModule;
939 *pword++ = 0;
940 *pword++ = 0;
942 /* Imported names table */
944 pstr = (char *)pword;
945 pModule->import_table = (int)pstr - (int)pModule;
946 *pstr++ = 0;
947 *pstr++ = 0;
949 /* Resident names table */
951 pModule->name_table = (int)pstr - (int)pModule;
952 /* First entry is module name */
953 *pstr = strlen(DLLName );
954 strcpy( pstr + 1, DLLName );
955 pstr += *pstr + 1;
956 *(WORD *)pstr = 0;
957 pstr += sizeof(WORD);
958 /* Store all ordinals */
959 odp = OrdinalDefinitions + 1;
960 for (i = 1; i <= Limit; i++, odp++)
962 if (!odp->name[0]) continue;
963 *pstr = strlen( odp->name );
964 strcpy( pstr + 1, odp->name );
965 strupper( pstr + 1 );
966 pstr += *pstr + 1;
967 *(WORD *)pstr = i;
968 pstr += sizeof(WORD);
970 *pstr++ = 0;
972 /* Entry table */
974 pModule->entry_table = (int)pstr - (int)pModule;
975 odp = OrdinalDefinitions + 1;
976 for (i = 1; i <= Limit; i++, odp++)
978 int selector = 0;
980 switch (odp->type)
982 case TYPE_CDECL:
983 case TYPE_PASCAL:
984 case TYPE_PASCAL_16:
985 case TYPE_REGISTER:
986 case TYPE_RETURN:
987 case TYPE_STUB:
988 selector = 1; /* Code selector */
989 break;
991 case TYPE_BYTE:
992 case TYPE_WORD:
993 case TYPE_LONG:
994 selector = 2; /* Data selector */
995 break;
997 case TYPE_ABS:
998 selector = 0xfe; /* Constant selector */
999 break;
1001 default:
1002 selector = 0; /* Invalid selector */
1003 break;
1006 if ( !selector )
1007 continue;
1009 if ( bundle && bundle->last+1 == i )
1010 bundle->last++;
1011 else
1013 if ( bundle )
1014 bundle->next = (char *)pstr - (char *)pModule;
1016 bundle = (ET_BUNDLE *)pstr;
1017 bundle->first = i-1;
1018 bundle->last = i;
1019 bundle->next = 0;
1020 pstr += sizeof(ET_BUNDLE);
1023 /* FIXME: is this really correct ?? */
1024 entry = (ET_ENTRY *)pstr;
1025 entry->type = 0xff; /* movable */
1026 entry->flags = 3; /* exported & public data */
1027 entry->segnum = selector;
1028 entry->offs = odp->offset;
1029 pstr += sizeof(ET_ENTRY);
1031 *pstr++ = 0;
1033 /* Dump the module content */
1035 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
1036 "Module" );
1037 return (int)pstr - (int)pModule;
1041 /*******************************************************************
1042 * BuildSpec32File
1044 * Build a Win32 C file from a spec file.
1046 static int BuildSpec32File( char * specfile, FILE *outfile )
1048 ORDDEF *odp;
1049 int i, nb_names, fwd_size = 0;
1051 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
1052 specfile );
1053 fprintf( outfile, "#include \"builtin32.h\"\n\n" );
1055 /* Output code for all stubs functions */
1057 fprintf( outfile, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
1058 DLLName );
1059 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1061 if (odp->type != TYPE_STUB) continue;
1062 fprintf( outfile, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
1063 i, DLLName, i );
1066 /* Output code for all register functions */
1068 fprintf( outfile, "#ifdef __i386__\n" );
1069 fprintf( outfile, "#ifndef __GNUC__\n" );
1070 fprintf( outfile, "static void __asm__dummy() {\n" );
1071 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
1072 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1074 if (odp->type != TYPE_REGISTER) continue;
1075 fprintf( outfile,
1076 "__asm__(\".align 4\\n\\t\"\n"
1077 " \".globl " PREFIX "%s\\n\\t\"\n"
1078 " \".type " PREFIX "%s,@function\\n\\t\"\n"
1079 " \"" PREFIX "%s:\\n\\t\"\n"
1080 " \"call " PREFIX "CALL32_Regs\\n\\t\"\n"
1081 " \".long " PREFIX "__regs_%s\\n\\t\"\n"
1082 " \".byte %d,%d\");\n",
1083 odp->u.func.link_name, odp->u.func.link_name,
1084 odp->u.func.link_name, odp->u.func.link_name,
1085 4 * strlen(odp->u.func.arg_types),
1086 4 * strlen(odp->u.func.arg_types) );
1088 fprintf( outfile, "#ifndef __GNUC__\n" );
1089 fprintf( outfile, "}\n" );
1090 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
1091 fprintf( outfile, "#endif /* defined(__i386__) */\n" );
1093 /* Output the DLL functions prototypes */
1095 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1097 switch(odp->type)
1099 case TYPE_VARARGS:
1100 fprintf( outfile, "extern void %s();\n", odp->u.vargs.link_name );
1101 break;
1102 case TYPE_EXTERN:
1103 fprintf( outfile, "extern void %s();\n", odp->u.ext.link_name );
1104 break;
1105 case TYPE_REGISTER:
1106 case TYPE_STDCALL:
1107 case TYPE_CDECL:
1108 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1109 break;
1110 case TYPE_FORWARD:
1111 fwd_size += strlen(odp->u.fwd.link_name) + 1;
1112 break;
1113 case TYPE_INVALID:
1114 case TYPE_STUB:
1115 break;
1116 default:
1117 fprintf(stderr,"build: function type %d not available for Win32\n",
1118 odp->type);
1119 return -1;
1123 /* Output LibMain function */
1124 if (DLLInitFunc[0]) fprintf( outfile, "extern void %s();\n", DLLInitFunc );
1127 /* Output the DLL functions table */
1129 fprintf( outfile, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1130 Limit - Base + 1 );
1131 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1133 switch(odp->type)
1135 case TYPE_INVALID:
1136 fprintf( outfile, " 0" );
1137 break;
1138 case TYPE_VARARGS:
1139 fprintf( outfile, " %s", odp->u.vargs.link_name );
1140 break;
1141 case TYPE_EXTERN:
1142 fprintf( outfile, " %s", odp->u.ext.link_name );
1143 break;
1144 case TYPE_REGISTER:
1145 case TYPE_STDCALL:
1146 case TYPE_CDECL:
1147 fprintf( outfile, " %s", odp->u.func.link_name);
1148 break;
1149 case TYPE_STUB:
1150 fprintf( outfile, " __stub_%d", i );
1151 break;
1152 case TYPE_FORWARD:
1153 fprintf( outfile, " (ENTRYPOINT32)\"%s\"", odp->u.fwd.link_name );
1154 break;
1155 default:
1156 return -1;
1158 if (i < Limit) fprintf( outfile, ",\n" );
1160 fprintf( outfile, "\n};\n\n" );
1162 /* Output the DLL names table */
1164 nb_names = 0;
1165 fprintf( outfile, "static const char * const FuncNames[] =\n{\n" );
1166 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1168 if (odp->type == TYPE_INVALID) continue;
1169 if (nb_names++) fprintf( outfile, ",\n" );
1170 fprintf( outfile, " \"%s\"", odp->name );
1172 fprintf( outfile, "\n};\n\n" );
1174 /* Output the DLL argument types */
1176 fprintf( outfile, "static const unsigned int ArgTypes[%d] =\n{\n",
1177 Limit - Base + 1 );
1178 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1180 unsigned int j, mask = 0;
1181 if ((odp->type == TYPE_STDCALL) || (odp->type == TYPE_CDECL) ||
1182 (odp->type == TYPE_REGISTER))
1183 for (j = 0; odp->u.func.arg_types[j]; j++)
1185 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
1186 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
1188 fprintf( outfile, " %d", mask );
1189 if (i < Limit) fprintf( outfile, ",\n" );
1191 fprintf( outfile, "\n};\n\n" );
1193 /* Output the DLL ordinals table */
1195 fprintf( outfile, "static const unsigned short FuncOrdinals[] =\n{\n" );
1196 nb_names = 0;
1197 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1199 if (odp->type == TYPE_INVALID) continue;
1200 if (nb_names++) fprintf( outfile, ",\n" );
1201 fprintf( outfile, " %d", i - Base );
1203 fprintf( outfile, "\n};\n\n" );
1205 /* Output the DLL functions arguments */
1207 fprintf( outfile, "static const unsigned char FuncArgs[%d] =\n{\n",
1208 Limit - Base + 1 );
1209 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1211 unsigned char args;
1212 switch(odp->type)
1214 case TYPE_STDCALL:
1215 args = (unsigned char)strlen(odp->u.func.arg_types);
1216 break;
1217 case TYPE_CDECL:
1218 args = 0x80 | (unsigned char)strlen(odp->u.func.arg_types);
1219 break;
1220 case TYPE_REGISTER:
1221 args = 0x40 | (unsigned char)strlen(odp->u.func.arg_types);
1222 break;
1223 case TYPE_FORWARD:
1224 args = 0xfd;
1225 break;
1226 default:
1227 args = 0xff;
1228 break;
1230 fprintf( outfile, " 0x%02x", args );
1231 if (i < Limit) fprintf( outfile, ",\n" );
1233 fprintf( outfile, "\n};\n\n" );
1235 /* Output the DLL imports */
1237 if (nb_imports)
1239 fprintf( outfile, "static const char * const Imports[%d] =\n{\n", nb_imports );
1240 for (i = 0; i < nb_imports; i++)
1242 fprintf( outfile, " \"%s\"", DLLImports[i] );
1243 if (i < nb_imports-1) fprintf( outfile, ",\n" );
1245 fprintf( outfile, "\n};\n\n" );
1248 /* Output the DLL descriptor */
1250 fprintf( outfile, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1251 DLLName );
1252 fprintf( outfile, " \"%s\",\n", DLLName );
1253 fprintf( outfile, " %d,\n", Base );
1254 fprintf( outfile, " %d,\n", Limit - Base + 1 );
1255 fprintf( outfile, " %d,\n", nb_names );
1256 fprintf( outfile, " %d,\n", nb_imports );
1257 fprintf( outfile, " %d,\n", (fwd_size + 3) & ~3 );
1258 fprintf( outfile,
1259 " Functions,\n"
1260 " FuncNames,\n"
1261 " FuncOrdinals,\n"
1262 " FuncArgs,\n"
1263 " ArgTypes,\n");
1264 fprintf( outfile, " %s,\n", nb_imports ? "Imports" : "0" );
1265 fprintf( outfile, " %s\n", DLLInitFunc[0] ? DLLInitFunc : "0" );
1266 fprintf( outfile, "};\n" );
1267 return 0;
1270 /*******************************************************************
1271 * Spec16TypeCompare
1273 static int Spec16TypeCompare( const void *e1, const void *e2 )
1275 const ORDDEF *odp1 = *(const ORDDEF **)e1;
1276 const ORDDEF *odp2 = *(const ORDDEF **)e2;
1278 int type1 = (odp1->type == TYPE_CDECL) ? 0
1279 : (odp1->type == TYPE_REGISTER) ? 3
1280 : (odp1->type == TYPE_PASCAL_16) ? 1 : 2;
1282 int type2 = (odp2->type == TYPE_CDECL) ? 0
1283 : (odp2->type == TYPE_REGISTER) ? 3
1284 : (odp2->type == TYPE_PASCAL_16) ? 1 : 2;
1286 int retval = type1 - type2;
1287 if ( !retval )
1288 retval = strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
1290 return retval;
1293 /*******************************************************************
1294 * BuildSpec16File
1296 * Build a Win16 assembly file from a spec file.
1298 static int BuildSpec16File( char * specfile, FILE *outfile )
1300 ORDDEF *odp, **typelist;
1301 int i, j, k;
1302 int code_offset, data_offset, module_size;
1303 unsigned char *data;
1305 /* File header */
1307 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
1308 specfile );
1309 fprintf( outfile, "#define __FLATCS__ 0x%04x\n", Code_Selector );
1310 fprintf( outfile, "#include \"builtin16.h\"\n\n" );
1312 data = (unsigned char *)xmalloc( 0x10000 );
1313 memset( data, 0, 16 );
1314 data_offset = 16;
1317 /* Build sorted list of all argument types, without duplicates */
1319 typelist = (ORDDEF **)calloc( Limit+1, sizeof(ORDDEF *) );
1321 odp = OrdinalDefinitions;
1322 for (i = j = 0; i <= Limit; i++, odp++)
1324 switch (odp->type)
1326 case TYPE_REGISTER:
1327 case TYPE_CDECL:
1328 case TYPE_PASCAL:
1329 case TYPE_PASCAL_16:
1330 case TYPE_STUB:
1331 typelist[j++] = odp;
1333 default:
1334 break;
1338 qsort( typelist, j, sizeof(ORDDEF *), Spec16TypeCompare );
1340 i = k = 0;
1341 while ( i < j )
1343 typelist[k++] = typelist[i++];
1344 while ( i < j && Spec16TypeCompare( typelist + i, typelist + k-1 ) == 0 )
1345 i++;
1348 /* Output CallFrom16 profiles needed by this .spec file */
1350 fprintf( outfile, "\n/* ### start build ### */\n" );
1352 for ( i = 0; i < k; i++ )
1353 fprintf( outfile, "extern void %s_CallFrom16_%s_%s_%s();\n",
1354 DLLName,
1355 (typelist[i]->type == TYPE_CDECL) ? "c" : "p",
1356 (typelist[i]->type == TYPE_REGISTER) ? "regs" :
1357 (typelist[i]->type == TYPE_PASCAL_16) ? "word" : "long",
1358 typelist[i]->u.func.arg_types );
1360 fprintf( outfile, "/* ### stop build ### */\n\n" );
1362 /* Output the DLL functions prototypes */
1364 odp = OrdinalDefinitions;
1365 for (i = 0; i <= Limit; i++, odp++)
1367 switch(odp->type)
1369 case TYPE_REGISTER:
1370 case TYPE_CDECL:
1371 case TYPE_PASCAL:
1372 case TYPE_PASCAL_16:
1373 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1374 break;
1375 default:
1376 break;
1380 /* Output code segment */
1382 fprintf( outfile, "\nstatic ENTRYPOINT16 Code_Segment[] = \n{\n" );
1383 code_offset = 0;
1385 odp = OrdinalDefinitions;
1386 for (i = 0; i <= Limit; i++, odp++)
1388 switch (odp->type)
1390 case TYPE_INVALID:
1391 odp->offset = 0xffff;
1392 break;
1394 case TYPE_ABS:
1395 odp->offset = LOWORD(odp->u.abs.value);
1396 break;
1398 case TYPE_BYTE:
1399 odp->offset = data_offset;
1400 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1401 break;
1403 case TYPE_WORD:
1404 odp->offset = data_offset;
1405 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1406 break;
1408 case TYPE_LONG:
1409 odp->offset = data_offset;
1410 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1411 break;
1413 case TYPE_RETURN:
1414 fprintf( outfile, " /* %s.%d */ ", DLLName, i );
1415 fprintf( outfile, "EP_RET( %d, %d ),\n",
1416 odp->u.ret.ret_value, odp->u.ret.arg_size );
1418 odp->offset = code_offset;
1419 code_offset += sizeof(ENTRYPOINT16);
1420 break;
1422 case TYPE_REGISTER:
1423 case TYPE_CDECL:
1424 case TYPE_PASCAL:
1425 case TYPE_PASCAL_16:
1426 case TYPE_STUB:
1427 fprintf( outfile, " /* %s.%d */ ", DLLName, i );
1428 fprintf( outfile, "EP_STD( %s, %s_CallFrom16_%s_%s_%s ),\n",
1429 odp->u.func.link_name,
1430 DLLName,
1431 (odp->type == TYPE_CDECL) ? "c" : "p",
1432 (odp->type == TYPE_REGISTER) ? "regs" :
1433 (odp->type == TYPE_PASCAL_16) ? "word" : "long",
1434 odp->u.func.arg_types );
1436 odp->offset = code_offset;
1437 code_offset += sizeof(ENTRYPOINT16);
1438 break;
1440 default:
1441 fprintf(stderr,"build: function type %d not available for Win16\n",
1442 odp->type);
1443 return -1;
1447 if (!code_offset) /* Make sure the code segment is not empty */
1449 fprintf( outfile, " { },\n" );
1450 code_offset += sizeof(ENTRYPOINT16);
1453 fprintf( outfile, "};\n" );
1455 /* Output data segment */
1457 DumpBytes( outfile, data, data_offset, "Data_Segment" );
1459 /* Build the module */
1461 module_size = BuildModule16( outfile, code_offset, data_offset );
1463 /* Output the DLL descriptor */
1465 fprintf( outfile, "\nWIN16_DESCRIPTOR %s_Descriptor = \n{\n", DLLName );
1466 fprintf( outfile, " \"%s\",\n", DLLName );
1467 fprintf( outfile, " Module,\n" );
1468 fprintf( outfile, " sizeof(Module),\n" );
1469 fprintf( outfile, " (BYTE *)Code_Segment,\n" );
1470 fprintf( outfile, " (BYTE *)Data_Segment\n" );
1471 fprintf( outfile, "};\n" );
1473 return 0;
1477 /*******************************************************************
1478 * BuildSpecFile
1480 * Build an assembly file from a spec file.
1482 static int BuildSpecFile( FILE *outfile, char *specname )
1484 SpecName = specname;
1485 SpecFp = fopen( specname, "r");
1486 if (SpecFp == NULL)
1488 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1489 return -1;
1492 if (ParseTopLevel() < 0) return -1;
1494 switch(SpecType)
1496 case SPEC_WIN16:
1497 return BuildSpec16File( specname, outfile );
1498 case SPEC_WIN32:
1499 return BuildSpec32File( specname, outfile );
1500 default:
1501 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1502 return -1;
1507 /*******************************************************************
1508 * BuildCallFrom16Func
1510 * Build a 16-bit-to-Wine callback glue function. The syntax of the function
1511 * profile is: call_type_xxxxx, where 'call' is the letter 'c' or 'p' for C or
1512 * Pascal calling convention, 'type' is one of 'regs', 'word' or
1513 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1514 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1515 * 'T'=segmented pointer to null-terminated string).
1516 * For register functions, the arguments are ignored, but they are still
1517 * removed from the stack upon return. !! FIXME !!
1519 * This glue function contains only that part of the 16->32 thunk that is
1520 * variable (depending on the type and number of arguments); the glue routine
1521 * is part of a particular DLL and uses a routine provided by the core to
1522 * perform those actions that do not depend on the argument type.
1524 * The 16->32 glue routine consists of a main entry point (which must be part
1525 * of the ELF .data section) and two auxillary routines (in the .text section).
1526 * The main entry point pushes address of the 'Thunk' auxillary routine onto
1527 * the stack and then jumps to the appropriate core CallFrom16... routine.
1528 * Furthermore, at a fixed position relative to the main entry point, the
1529 * function profile string is stored if relay debugging is active; this string
1530 * will be consulted by the debugging routines in the core to correctly
1531 * display the arguments.
1533 * The core will perform the switch to 32-bit, and then call back to the
1534 * 'Thunk' auxillary routine. This routine is expected to perform the
1535 * following tasks:
1536 * - modify the auxillary routine address in the STACK16FRAME to now
1537 * point to the 'ThunkRet' routine
1538 * - convert arguemnts and push them onto the 32-bit stack
1539 * - call the 32-bit target routine
1541 * After the target routine (and then the 'Thunk' routine) have returned,
1542 * the core part will switch back to 16-bit, and finally jump to the
1543 * 'ThunkRet' auxillary routine. This routine is expected to convert the
1544 * return value if necessary (%eax -> %dx:%ax), and perform the appropriate
1545 * return to the 16-bit caller (lret or lret $argsize).
1547 * In the case of a 'register' routine, there is no 'ThunkRet' auxillary
1548 * routine, as the reloading of all registers and return to 16-bit code
1549 * is done by the core routine. The number of arguments to be popped off
1550 * the caller's stack must be returned (in %eax) by the 'Thunk' routine.
1552 * NOTE: The generated routines contain only proper position-independent
1553 * code and may thus be used within shared objects (e.g. libwine.so
1554 * or elfdlls). The exception is the main entry point, which must
1555 * contain absolute relocations but cannot yet use the GOT; thus
1556 * this entry point is made part of the ELF .data section.
1558 static void BuildCallFrom16Func( FILE *outfile, char *profile, char *prefix )
1560 int i, pos, argsize = 0;
1561 int short_ret = 0;
1562 int reg_func = 0;
1563 int usecdecl = 0;
1564 char *args = profile + 7;
1566 /* Parse function type */
1568 if (!strncmp( "c_", profile, 2 )) usecdecl = 1;
1569 else if (strncmp( "p_", profile, 2 ))
1571 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1572 return;
1575 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
1576 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
1577 else if (strncmp( "long_", profile + 2, 5 ))
1579 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1580 return;
1583 for ( i = 0; args[i]; i++ )
1584 switch ( args[i] )
1586 case 'w': /* word */
1587 case 's': /* s_word */
1588 argsize += 2;
1589 break;
1591 case 'l': /* long or segmented pointer */
1592 case 'T': /* segmented pointer to null-terminated string */
1593 case 'p': /* linear pointer */
1594 case 't': /* linear pointer to null-terminated string */
1595 argsize += 4;
1596 break;
1600 * Build main entry point (in .data section)
1602 * NOTE: If you change this, you must also change the retrieval of
1603 * the profile string relative to the main entry point address
1604 * (see BUILTIN_GetEntryPoint16 in if1632/builtin.c).
1606 fprintf( outfile, "\t.data\n" );
1607 fprintf( outfile, "\t.globl " PREFIX "%s_CallFrom16_%s\n", prefix, profile );
1608 fprintf( outfile, PREFIX "%s_CallFrom16_%s:\n", prefix, profile );
1609 fprintf( outfile, "\tpushl $.L%s_Thunk_%s\n", prefix, profile );
1610 fprintf( outfile, "\tjmp " PREFIX "%s\n",
1611 !reg_func? "CallFrom16" : "CallFrom16Register" );
1612 if ( debugging )
1613 fprintf( outfile, "\t" STRING " \"%s\\0\"\n", profile );
1616 * Build *Thunk* routine
1618 * This routine gets called by the main CallFrom16 routine with
1619 * registers set up as follows:
1621 * STACK16FRAME is completely set up on the 16-bit stack
1622 * DS, ES, SS: flat data segment
1623 * FS: current TEB
1624 * ESP: points to 32-bit stack
1625 * EBP: points to ebp member of last STACK32FRAME
1626 * EDX: points to current STACK16FRAME
1627 * ECX: points to ldt_copy
1630 fprintf( outfile, "\t.text\n" );
1631 fprintf( outfile, ".L%s_Thunk_%s:\n", prefix, profile );
1633 if ( reg_func )
1634 fprintf( outfile, "\tleal 4(%%esp), %%eax\n"
1635 "\tpushl %%eax\n" );
1636 else
1637 fprintf( outfile, "\taddl $[.L%s_ThunkRet_%s - .L%s_Thunk_%s], %d(%%edx)\n",
1638 prefix, profile, prefix, profile,
1639 STACK16OFFSET(relay));
1641 if ( !reg_func ) /* FIXME */
1643 /* Copy the arguments */
1644 pos = (usecdecl? argsize : 0) + sizeof( STACK16FRAME );
1645 args = profile + 7;
1646 for ( i = strlen(args)-1; i >= 0; i-- )
1647 switch (args[i])
1649 case 'w': /* word */
1650 if ( usecdecl ) pos -= 2;
1651 fprintf( outfile, "\tmovzwl %d(%%edx),%%eax\n", pos );
1652 fprintf( outfile, "\tpushl %%eax\n" );
1653 if ( !usecdecl ) pos += 2;
1654 break;
1656 case 's': /* s_word */
1657 if ( usecdecl ) pos -= 2;
1658 fprintf( outfile, "\tmovswl %d(%%edx),%%eax\n", pos );
1659 fprintf( outfile, "\tpushl %%eax\n" );
1660 if ( !usecdecl ) pos += 2;
1661 break;
1663 case 'l': /* long or segmented pointer */
1664 case 'T': /* segmented pointer to null-terminated string */
1665 if ( usecdecl ) pos -= 4;
1666 fprintf( outfile, "\tpushl %d(%%edx)\n", pos );
1667 if ( !usecdecl ) pos += 4;
1668 break;
1670 case 'p': /* linear pointer */
1671 case 't': /* linear pointer to null-terminated string */
1672 if ( usecdecl ) pos -= 4;
1673 /* Get the selector */
1674 fprintf( outfile, "\tmovzwl %d(%%edx),%%eax\n", pos + 2 );
1675 /* Get the selector base */
1676 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1677 fprintf( outfile, "\tpushl (%%ecx,%%eax)\n" );
1678 /* Add the offset */
1679 fprintf( outfile, "\tmovzwl %d(%%edx),%%eax\n", pos );
1680 fprintf( outfile, "\taddl %%eax,(%%esp)\n" );
1681 if ( !usecdecl ) pos += 4;
1682 break;
1684 default:
1685 fprintf( stderr, "Unknown arg type '%c'\n", args[i] );
1689 /* Call entry point */
1690 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point) );
1692 if ( reg_func )
1693 fprintf( outfile, "\tmovl $%d, %%eax\n", argsize );
1695 fprintf( outfile, "\tret\n" );
1699 * Build *ThunkRet* routine
1701 * At this point, all registers are set up for return to 16-bit code.
1702 * EAX contains the function return value.
1703 * SS:SP point to the return address to the caller (on 16-bit stack).
1705 if ( !reg_func )
1707 fprintf( outfile, ".L%s_ThunkRet_%s:\n", prefix, profile );
1709 if ( !short_ret )
1710 fprintf( outfile, "\tshldl $16, %%eax, %%edx\n" );
1712 if ( !usecdecl && argsize )
1714 fprintf( outfile, "\t.byte 0x66\n" );
1715 fprintf( outfile, "\tlret $%d\n", argsize );
1717 else
1719 fprintf( outfile, "\t.byte 0x66\n" );
1720 fprintf( outfile, "\tlret\n" );
1723 fprintf( outfile, "\n" );
1726 /*******************************************************************
1727 * BuildCallTo16Func
1729 * Build a Wine-to-16-bit callback glue function. As above, this glue
1730 * routine will only contain the argument-type dependent part of the
1731 * thunk; the rest will be done by a routine provided by the core.
1733 * Prototypes for the CallTo16 functions:
1734 * extern WORD PREFIX_CallTo16_word_xxx( FARPROC16 func, args... );
1735 * extern LONG PREFIX_CallTo16_long_xxx( FARPROC16 func, args... );
1737 * The main entry point of the glue routine simply performs a call to
1738 * the proper core routine depending on the return type (WORD/LONG).
1739 * Note that the core will never perform a return from this call; however,
1740 * it will use the return address on the stack to access the other
1741 * argument-type dependent parts of the thunk. (If relay debugging is
1742 * active, the core routine will access the number of arguments stored
1743 * in the code section immediately precending the main entry point).
1745 * After the core routine has performed the switch to 16-bit code, it
1746 * will call the argument-transfer routine provided by the glue code
1747 * immediately after the main entry point. This routine is expected
1748 * to transfer the arguments to the 16-bit stack, finish loading the
1749 * register for 16-bit code (%ds and %es must be loaded from %ss),
1750 * and call the 16-bit target.
1752 * The target will return to a 16:16 return address provided by the core.
1753 * The core will finalize the switch back to 32-bit and the return to
1754 * the caller without additional support by the glue code. Note that
1755 * the 32-bit arguments will not be popped off the stack (hence the
1756 * CallTo... routine must *not* be declared WINAPI/CALLBACK).
1759 static void BuildCallTo16Func( FILE *outfile, char *profile, char *prefix )
1761 char *args = profile + 5;
1762 int pos, short_ret = 0;
1764 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1765 else if (strncmp( "long_", profile, 5 ))
1767 fprintf( stderr, "Invalid function name '%s'.\n", profile );
1768 exit(1);
1771 if ( debugging )
1773 /* Number of arguments (for relay debugging) */
1774 fprintf( outfile, "\n\t.align 4\n" );
1775 fprintf( outfile, "\t.long %d\n", strlen(args) );
1778 /* Main entry point */
1780 #ifdef USE_STABS
1781 fprintf( outfile, ".stabs \"%s_CallTo16_%s:F1\",36,0,0," PREFIX "%s_CallTo16_%s\n",
1782 prefix, profile, prefix, profile);
1783 #endif
1784 fprintf( outfile, "\t.globl " PREFIX "%s_CallTo16_%s\n", prefix, profile );
1785 fprintf( outfile, PREFIX "%s_CallTo16_%s:\n", prefix, profile );
1787 if ( short_ret )
1788 fprintf( outfile, "\tcall " PREFIX "CallTo16Word\n" );
1789 else
1790 fprintf( outfile, "\tcall " PREFIX "CallTo16Long\n" );
1793 * The core routine will call here with registers set up as follows:
1795 * SS:SP points to the 16-bit stack
1796 * SS:BP points to the bp member of last STACK16FRAME
1797 * EDX points to the current STACK32FRAME
1798 * ECX contains the 16:16 return address
1799 * FS contains the last 16-bit %fs value
1802 /* Transfer the arguments */
1804 pos = STACK32OFFSET(args) + 4; /* first arg is target address */
1805 while (*args)
1807 switch (*args++)
1809 case 'w': /* word */
1810 fprintf( outfile, "\tpushw %d(%%edx)\n", pos );
1811 break;
1812 case 'l': /* long */
1813 fprintf( outfile, "\tpushl %d(%%edx)\n", pos );
1814 break;
1815 default:
1816 fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
1817 args[-1] );
1819 pos += 4;
1822 /* Push the return address */
1824 fprintf( outfile, "\tpushl %%ecx\n" );
1826 /* Push the called routine address */
1828 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK32OFFSET(args) );
1830 /* Set %ds and %es (and %ax just in case) equal to %ss */
1832 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1833 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
1834 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1836 /* Jump to the called routine */
1838 fprintf( outfile, "\t.byte 0x66\n" );
1839 fprintf( outfile, "\tlret\n" );
1844 /*******************************************************************
1845 * BuildCallFrom16Core
1847 * This routine builds the core routines used in 16->32 thunks:
1848 * CallFrom16, CallFrom16Register, and CallFrom16Thunk.
1850 * CallFrom16 and CallFrom16Register are used by the 16->32 glue code
1851 * as described above. CallFrom16Thunk is a special variant used by
1852 * the implementation of the Win95 16->32 thunk functions C16ThkSL and
1853 * C16ThkSL01 and is implemented as follows:
1854 * On entry, the EBX register is set up to contain a flat pointer to the
1855 * 16-bit stack such that EBX+22 points to the first argument.
1856 * Then, the entry point is called, while EBP is set up to point
1857 * to the return address (on the 32-bit stack).
1858 * The called function returns with CX set to the number of bytes
1859 * to be popped of the caller's stack.
1861 * Stack layout upon entry to the core routine (STACK16FRAME):
1862 * ... ...
1863 * (sp+22) word first 16-bit arg
1864 * (sp+20) word cs
1865 * (sp+18) word ip
1866 * (sp+16) word bp
1867 * (sp+12) long 32-bit entry point (reused for Win16 mutex recursion count)
1868 * (sp+8) long cs of 16-bit entry point
1869 * (sp+4) long ip of 16-bit entry point
1870 * (sp) long auxillary relay function address
1872 * Added on the stack:
1873 * (sp-2) word saved gs
1874 * (sp-4) word saved fs
1875 * (sp-6) word saved es
1876 * (sp-8) word saved ds
1877 * (sp-12) long saved ebp
1878 * (sp-16) long saved ecx
1879 * (sp-20) long saved edx
1880 * (sp-24) long saved previous stack
1882 static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk )
1884 char *name = thunk? "Thunk" : reg_func? "Register" : "";
1886 /* Function header */
1887 fprintf( outfile, "\n\t.align 4\n" );
1888 #ifdef USE_STABS
1889 fprintf( outfile, ".stabs \"CallFrom16%s:F1\",36,0,0," PREFIX "CallFrom16%s\n",
1890 name, name);
1891 #endif
1892 fprintf( outfile, "\t.globl " PREFIX "CallFrom16%s\n", name );
1893 fprintf( outfile, PREFIX "CallFrom16%s:\n", name );
1895 /* No relay function for 'thunk' */
1896 if ( thunk )
1897 fprintf( outfile, "\tpushl $0\n" );
1899 /* Create STACK16FRAME (except STACK32FRAME link) */
1900 fprintf( outfile, "\tpushw %%gs\n" );
1901 fprintf( outfile, "\tpushw %%fs\n" );
1902 fprintf( outfile, "\tpushw %%es\n" );
1903 fprintf( outfile, "\tpushw %%ds\n" );
1904 fprintf( outfile, "\tpushl %%ebp\n" );
1905 fprintf( outfile, "\tpushl %%ecx\n" );
1906 fprintf( outfile, "\tpushl %%edx\n" );
1908 #ifdef USE__PIC__
1909 /* Get Global Offset Table into %ecx */
1910 fprintf( outfile, "\tcall .LCallFrom16%s.getgot\n", name );
1911 fprintf( outfile, ".LCallFrom16%s.getgot:\n", name );
1912 fprintf( outfile, "\tpopl %%ecx\n" );
1913 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE+[.-.LCallFrom16%s.getgot], %%ecx\n" );
1914 #endif
1916 /* Load 32-bit segment registers */
1917 fprintf( outfile, "\tmovw $0x%04x, %%dx\n", Data_Selector );
1918 #ifdef __svr4__
1919 fprintf( outfile, "\tdata16\n");
1920 #endif
1921 fprintf( outfile, "\tmovw %%dx, %%ds\n" );
1922 #ifdef __svr4__
1923 fprintf( outfile, "\tdata16\n");
1924 #endif
1925 fprintf( outfile, "\tmovw %%dx, %%es\n" );
1926 #ifdef USE__PIC__
1927 fprintf( outfile, "\tmovl " PREFIX "SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
1928 fprintf( outfile, "\tmovw (%%edx), %%fs\n" );
1929 #else
1930 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb, %%fs\n" );
1931 #endif
1933 /* Get address of ldt_copy array into %ecx */
1934 #ifdef USE__PIC__
1935 fprintf( outfile, "\tmovl " PREFIX "ldt_copy@GOT(%%ecx), %%ecx\n" );
1936 #else
1937 fprintf( outfile, "\tmovl $" PREFIX "ldt_copy, %%ecx\n" );
1938 #endif
1940 /* Translate STACK16FRAME base to flat offset in %edx */
1941 fprintf( outfile, "\tmovw %%ss, %%dx\n" );
1942 fprintf( outfile, "\tandl $0xfff8, %%edx\n" );
1943 fprintf( outfile, "\tmovl (%%ecx,%%edx), %%edx\n" );
1944 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
1945 fprintf( outfile, "\tleal -4(%%ebp,%%edx), %%edx\n" );
1946 /* -4 since STACK16FRAME not yet complete! */
1948 /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
1949 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET );
1950 fprintf( outfile, "\tpushl %%ebp\n" );
1952 /* Switch stacks */
1953 #ifdef __svr4__
1954 fprintf( outfile,"\tdata16\n");
1955 #endif
1956 fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET + 2 );
1957 fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET );
1958 fprintf( outfile, "\tpushl %%ds\n" );
1959 fprintf( outfile, "\tpopl %%ss\n" );
1960 fprintf( outfile, "\tmovl %%ebp, %%esp\n" );
1961 fprintf( outfile, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME, ebp) );
1964 /* At this point:
1965 STACK16FRAME is completely set up
1966 DS, ES, SS: flat data segment
1967 FS: current TEB
1968 ESP: points to last STACK32FRAME
1969 EBP: points to ebp member of last STACK32FRAME
1970 EDX: points to current STACK16FRAME
1971 ECX: points to ldt_copy
1972 all other registers: unchanged */
1974 /* Special case: C16ThkSL stub */
1975 if ( thunk )
1977 /* Set up registers as expected and call thunk */
1978 fprintf( outfile, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME)-22 );
1979 fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
1981 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point) );
1983 /* Switch stack back */
1984 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
1985 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
1986 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
1988 /* Restore registers and return directly to caller */
1989 fprintf( outfile, "\taddl $8, %%esp\n" );
1990 fprintf( outfile, "\tpopl %%ebp\n" );
1991 fprintf( outfile, "\tpopw %%ds\n" );
1992 fprintf( outfile, "\tpopw %%es\n" );
1993 fprintf( outfile, "\tpopw %%fs\n" );
1994 fprintf( outfile, "\tpopw %%gs\n" );
1995 fprintf( outfile, "\taddl $18, %%esp\n" );
1997 fprintf( outfile, "\txorb %%ch, %%ch\n" );
1998 fprintf( outfile, "\tpopl %%ebx\n" );
1999 fprintf( outfile, "\taddw %%cx, %%sp\n" );
2000 fprintf( outfile, "\tpush %%ebx\n" );
2002 fprintf( outfile, "\t.byte 0x66\n" );
2003 fprintf( outfile, "\tlret\n" );
2005 return;
2009 /* Build register CONTEXT */
2010 if ( reg_func )
2012 fprintf( outfile, "\tsubl $%d, %%esp\n", sizeof(CONTEXT) );
2014 fprintf( outfile, "\tpushfl\n" );
2015 fprintf( outfile, "\tpopl %d(%%esp)\n", CONTEXTOFFSET(EFlags) );
2017 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax) );
2018 fprintf( outfile, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx) );
2019 fprintf( outfile, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi) );
2020 fprintf( outfile, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi) );
2022 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp) );
2023 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp) );
2024 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx) );
2025 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx) );
2026 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx) );
2027 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx) );
2029 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds) );
2030 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs) );
2031 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es) );
2032 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs) );
2033 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs) );
2034 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs) );
2035 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs) );
2036 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs) );
2038 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs) );
2039 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs) );
2040 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip) );
2041 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip) );
2043 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET+2 );
2044 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs) );
2045 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET );
2046 fprintf( outfile, "\taddl $%d, %%eax\n", STACK16OFFSET(ip) );
2047 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp) );
2048 #if 0
2049 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2050 #endif
2054 /* Print debug info before call */
2055 if ( debugging )
2057 fprintf( outfile, "\tpushl %%ecx\n" );
2058 fprintf( outfile, "\tpushl %%edx\n" );
2059 if ( reg_func )
2060 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2061 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
2062 else
2063 fprintf( outfile, "\tpushl $0\n" );
2064 #if USE__PIC__
2065 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16@PLT\n ");
2066 #else
2067 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n ");
2068 #endif
2069 fprintf( outfile, "\tpopl %%edx\n" );
2070 fprintf( outfile, "\tpopl %%edx\n" );
2071 fprintf( outfile, "\tpopl %%ecx\n" );
2074 /* Call *Thunk* relay routine (which will call the API entry point) */
2075 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay) );
2077 /* Print debug info after call */
2078 if ( debugging )
2080 fprintf( outfile, "\tpushl %%eax\n" );
2081 if ( reg_func )
2082 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2083 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
2084 else
2085 fprintf( outfile, "\tpushl $0\n" );
2086 #if USE__PIC__
2087 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret@PLT\n ");
2088 #else
2089 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n ");
2090 #endif
2091 fprintf( outfile, "\tpopl %%eax\n" );
2092 fprintf( outfile, "\tpopl %%eax\n" );
2096 if ( reg_func )
2098 fprintf( outfile, "\tmovl %%esp, %%ebx\n" );
2100 /* Switch stack back */
2101 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2102 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
2103 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2105 /* Restore all registers from CONTEXT */
2106 fprintf( outfile, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs) );
2107 fprintf( outfile, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp) );
2108 fprintf( outfile, "\tleal 4(%%esp, %%eax), %%esp\n" );
2110 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
2111 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
2112 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags) );
2113 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
2115 fprintf( outfile, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs) );
2116 fprintf( outfile, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs) );
2117 fprintf( outfile, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs) );
2119 fprintf( outfile, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp) );
2120 fprintf( outfile, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi) );
2121 fprintf( outfile, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi) );
2122 fprintf( outfile, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax) );
2123 fprintf( outfile, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx) );
2124 fprintf( outfile, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx) );
2125 fprintf( outfile, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx) );
2127 fprintf( outfile, "\tpopl %%ds\n" );
2128 fprintf( outfile, "\tpopfl\n" );
2129 fprintf( outfile, "\t.byte 0x66\n" );
2130 fprintf( outfile, "\tlret\n" );
2132 else
2134 /* Switch stack back */
2135 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2136 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
2137 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2139 /* Restore registers and return to *ThunkRet* routine */
2140 fprintf( outfile, "\tpopl %%edx\n" );
2141 fprintf( outfile, "\tpopl %%ecx\n" );
2142 fprintf( outfile, "\tpopl %%ebp\n" );
2143 fprintf( outfile, "\tpopw %%ds\n" );
2144 fprintf( outfile, "\tpopw %%es\n" );
2145 fprintf( outfile, "\tpopw %%fs\n" );
2146 fprintf( outfile, "\tpopw %%gs\n" );
2147 fprintf( outfile, "\tret $14\n" );
2152 /*******************************************************************
2153 * BuildCallTo16Core
2155 * This routine builds the core routines used in 32->16 thunks:
2156 * CallTo16Word, CallTo16Long, CallTo16RegisterShort, and
2157 * CallTo16RegisterLong.
2159 * CallTo16Word and CallTo16Long are used by the 32->16 glue code
2160 * as described above. The register functions can be called directly:
2162 * extern void CallTo16RegisterShort( const CONTEXT86 *context, int nb_args );
2163 * extern void CallTo16RegisterLong ( const CONTEXT86 *context, int nb_args );
2165 * They call to 16-bit code with all registers except SS:SP set up as specified
2166 * by the 'context' structure, and SS:SP set to point to the current 16-bit
2167 * stack, decremented by the value specified in the 'nb_args' argument.
2170 static void BuildCallTo16Core( FILE *outfile, int short_ret, int reg_func )
2172 char *name = reg_func == 2 ? "RegisterLong" :
2173 reg_func == 1 ? "RegisterShort" :
2174 short_ret? "Word" : "Long";
2176 /* Function header */
2177 fprintf( outfile, "\n\t.align 4\n" );
2178 #ifdef USE_STABS
2179 fprintf( outfile, ".stabs \"CallTo16%s:F1\",36,0,0," PREFIX "CallTo16%s\n",
2180 name, name);
2181 #endif
2182 fprintf( outfile, "\t.globl " PREFIX "CallTo16%s\n", name );
2183 fprintf( outfile, PREFIX "CallTo16%s:\n", name );
2185 /* Retrieve relay target address */
2186 if ( !reg_func )
2187 fprintf( outfile, "\tpopl %%eax\n" );
2189 /* Function entry sequence */
2190 fprintf( outfile, "\tpushl %%ebp\n" );
2191 fprintf( outfile, "\tmovl %%esp, %%ebp\n" );
2193 /* Save the 32-bit registers */
2194 fprintf( outfile, "\tpushl %%ebx\n" );
2195 fprintf( outfile, "\tpushl %%ecx\n" );
2196 fprintf( outfile, "\tpushl %%edx\n" );
2197 fprintf( outfile, "\tpushl %%esi\n" );
2198 fprintf( outfile, "\tpushl %%edi\n" );
2200 #ifdef USE__PIC__
2201 /* Get Global Offset Table into %ebx */
2202 fprintf( outfile, "\tcall .LCallTo16%s.getgot\n", name );
2203 fprintf( outfile, ".LCallFrom16%s.getgot:\n", name );
2204 fprintf( outfile, "\tpopl %%ebx\n" );
2205 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE+[.-.LCallFrom16%s.getgot], %%ebx\n" );
2206 #endif
2208 /* Move relay target address to %edi */
2209 if ( !reg_func )
2210 fprintf( outfile, "\tmovl %%eax, %%edi\n" );
2212 /* Enter Win16 Mutex */
2213 #ifdef USE__PIC__
2214 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock@PLT\n" );
2215 #else
2216 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock\n" );
2217 #endif
2219 /* Print debugging info */
2220 if (debugging)
2222 /* Push number of arguments (from relay stub) */
2223 if ( reg_func )
2224 fprintf( outfile, "\tpushl $-1\n" );
2225 else
2226 fprintf( outfile, "\tpushl -9(%%edi)\n" );
2228 /* Push the address of the first argument */
2229 fprintf( outfile, "\tleal 8(%%ebp),%%eax\n" );
2230 fprintf( outfile, "\tpushl %%eax\n" );
2232 #ifdef USE__PIC__
2233 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16@PLT\n" );
2234 #else
2235 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
2236 #endif
2237 fprintf( outfile, "\tpopl %%eax\n" );
2238 fprintf( outfile, "\tpopl %%eax\n" );
2241 /* Get return address */
2242 #ifdef USE__PIC__
2243 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr@GOTOFF(%%ebx), %%ecx\n" );
2244 #else
2245 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr, %%ecx\n" );
2246 #endif
2248 /* Call the actual CallTo16 routine (simulate a lcall) */
2249 fprintf( outfile, "\tpushl %%cs\n" );
2250 fprintf( outfile, "\tcall .LCallTo16%s\n", name );
2252 /* Convert and push return value */
2253 if ( short_ret )
2255 fprintf( outfile, "\tmovzwl %%ax, %%eax\n" );
2256 fprintf( outfile, "\tpushl %%eax\n" );
2258 else if ( reg_func != 2 )
2260 fprintf( outfile, "\tshll $16,%%edx\n" );
2261 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
2262 fprintf( outfile, "\tpushl %%edx\n" );
2264 else
2265 fprintf( outfile, "\tpushl %%eax\n" );
2267 /* Print debugging info */
2268 if (debugging)
2270 #ifdef USE__PIC__
2271 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret@PLT\n" );
2272 #else
2273 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret\n" );
2274 #endif
2277 /* Leave Win16 Mutex */
2278 #ifdef USE__PIC__
2279 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock@PLT\n" );
2280 #else
2281 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock\n" );
2282 #endif
2284 /* Get return value */
2285 fprintf( outfile, "\tpopl %%eax\n" );
2287 /* Restore the 32-bit registers */
2288 fprintf( outfile, "\tpopl %%edi\n" );
2289 fprintf( outfile, "\tpopl %%esi\n" );
2290 fprintf( outfile, "\tpopl %%edx\n" );
2291 fprintf( outfile, "\tpopl %%ecx\n" );
2292 fprintf( outfile, "\tpopl %%ebx\n" );
2294 /* Function exit sequence */
2295 fprintf( outfile, "\tpopl %%ebp\n" );
2296 fprintf( outfile, "\tret\n" );
2299 /* Start of the actual CallTo16 routine */
2301 fprintf( outfile, ".LCallTo16%s:\n", name );
2303 /* Complete STACK32FRAME */
2304 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
2305 fprintf( outfile, "\tmovl %%esp,%%edx\n" );
2307 /* Switch to the 16-bit stack */
2308 #ifdef __svr4__
2309 fprintf( outfile,"\tdata16\n");
2310 #endif
2311 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
2312 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
2313 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
2315 if (reg_func)
2317 /* Add the specified offset to the new sp */
2318 fprintf( outfile, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(args)+4 );
2320 /* Push the return address
2321 * With sreg suffix, we push 16:16 address (normal lret)
2322 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
2324 if (reg_func == 1)
2325 fprintf( outfile, "\tpushl %%ecx\n" );
2326 else
2328 fprintf( outfile, "\tshldl $16, %%ecx, %%eax\n" );
2329 fprintf( outfile, "\tpushw $0\n" );
2330 fprintf( outfile, "\tpushw %%ax\n" );
2331 fprintf( outfile, "\tpushw $0\n" );
2332 fprintf( outfile, "\tpushw %%cx\n" );
2335 /* Push the called routine address */
2336 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(args) );
2337 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs) );
2338 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip) );
2340 /* Get the registers */
2341 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs) );
2342 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs) );
2343 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2344 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs) );
2345 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2346 fprintf( outfile, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp) );
2347 fprintf( outfile, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi) );
2348 fprintf( outfile, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi) );
2349 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax) );
2350 fprintf( outfile, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx) );
2351 fprintf( outfile, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx) );
2352 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx) );
2354 /* Get the 16-bit ds */
2355 fprintf( outfile, "\tpopw %%ds\n" );
2357 /* Jump to the called routine */
2358 fprintf( outfile, "\t.byte 0x66\n" );
2359 fprintf( outfile, "\tlret\n" );
2361 else /* not a register function */
2363 /* Make %bp point to the previous stackframe (built by CallFrom16) */
2364 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
2365 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp) );
2367 /* Set %fs to the value saved by the last CallFrom16 */
2368 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs)-STACK16OFFSET(bp) );
2369 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2371 /* Jump to the relay code */
2372 fprintf( outfile, "\tjmp *%%edi\n" );
2376 /*******************************************************************
2377 * BuildRet16Func
2379 * Build the return code for 16-bit callbacks
2381 static void BuildRet16Func( FILE *outfile )
2384 * Note: This must reside in the .data section to allow
2385 * run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
2388 fprintf( outfile, "\n\t.globl " PREFIX "CallTo16_Ret\n" );
2389 fprintf( outfile, PREFIX "CallTo16_Ret:\n" );
2391 /* Restore 32-bit segment registers */
2393 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
2394 #ifdef __svr4__
2395 fprintf( outfile, "\tdata16\n");
2396 #endif
2397 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
2398 #ifdef __svr4__
2399 fprintf( outfile, "\tdata16\n");
2400 #endif
2401 fprintf( outfile, "\tmovw %%bx,%%es\n" );
2403 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
2405 /* Restore the 32-bit stack */
2407 #ifdef __svr4__
2408 fprintf( outfile, "\tdata16\n");
2409 #endif
2410 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
2411 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
2412 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2414 /* Return to caller */
2416 fprintf( outfile, "\tlret\n" );
2418 /* Declare the return address variable */
2420 fprintf( outfile, "\n\t.globl " PREFIX "CallTo16_RetAddr\n" );
2421 fprintf( outfile, PREFIX "CallTo16_RetAddr:\t.long 0\n" );
2424 /*******************************************************************
2425 * BuildCallTo32CBClient
2427 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
2429 * Since the relay stub is itself 32-bit, this should not be a problem;
2430 * unfortunately, the relay stubs are expected to switch back to a
2431 * 16-bit stack (and 16-bit code) after completion :-(
2433 * This would conflict with our 16- vs. 32-bit stack handling, so
2434 * we simply switch *back* to our 32-bit stack before returning to
2435 * the caller ...
2437 * The CBClient relay stub expects to be called with the following
2438 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
2439 * stack at the designated places:
2441 * ...
2442 * (ebp+14) original arguments to the callback routine
2443 * (ebp+10) far return address to original caller
2444 * (ebp+6) Thunklet target address
2445 * (ebp+2) Thunklet relay ID code
2446 * (ebp) BP (saved by CBClientGlueSL)
2447 * (ebp-2) SI (saved by CBClientGlueSL)
2448 * (ebp-4) DI (saved by CBClientGlueSL)
2449 * (ebp-6) DS (saved by CBClientGlueSL)
2451 * ... buffer space used by the 16-bit side glue for temp copies
2453 * (ebx+4) far return address to 16-bit side glue code
2454 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
2456 * The 32-bit side glue code accesses both the original arguments (via ebp)
2457 * and the temporary copies prepared by the 16-bit side glue (via ebx).
2458 * After completion, the stub will load ss:sp from the buffer at ebx
2459 * and perform a far return to 16-bit code.
2461 * To trick the relay stub into returning to us, we replace the 16-bit
2462 * return address to the glue code by a cs:ip pair pointing to our
2463 * return entry point (the original return address is saved first).
2464 * Our return stub thus called will then reload the 32-bit ss:esp and
2465 * return to 32-bit code (by using and ss:esp value that we have also
2466 * pushed onto the 16-bit stack before and a cs:eip values found at
2467 * that position on the 32-bit stack). The ss:esp to be restored is
2468 * found relative to the 16-bit stack pointer at:
2470 * (ebx-4) ss (flat)
2471 * (ebx-8) sp (32-bit stack pointer)
2473 * The second variant of this routine, CALL32_CBClientEx, which is used
2474 * to implement KERNEL.621, has to cope with yet another problem: Here,
2475 * the 32-bit side directly returns to the caller of the CBClient thunklet,
2476 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
2477 * As we have to return to our 32-bit code first, we have to adapt the
2478 * layout of our temporary area so as to include values for the registers
2479 * that are to be restored, and later (in the implementation of KERNEL.621)
2480 * we *really* restore them. The return stub restores DS, DI, SI, and BP
2481 * from the stack, skips the next 8 bytes (CBClient relay code / target),
2482 * and then performs a lret NN, where NN is the number of arguments to be
2483 * removed. Thus, we prepare our temporary area as follows:
2485 * (ebx+22) 16-bit cs (this segment)
2486 * (ebx+20) 16-bit ip ('16-bit' return entry point)
2487 * (ebx+16) 32-bit ss (flat)
2488 * (ebx+12) 32-bit sp (32-bit stack pointer)
2489 * (ebx+10) 16-bit bp (points to ebx+24)
2490 * (ebx+8) 16-bit si (ignored)
2491 * (ebx+6) 16-bit di (ignored)
2492 * (ebx+4) 16-bit ds (we actually use the flat DS here)
2493 * (ebx+2) 16-bit ss (16-bit stack segment)
2494 * (ebx+0) 16-bit sp (points to ebx+4)
2496 * Note that we ensure that DS is not changed and remains the flat segment,
2497 * and the 32-bit stack pointer our own return stub needs fits just
2498 * perfectly into the 8 bytes that are skipped by the Windows stub.
2499 * One problem is that we have to determine the number of removed arguments,
2500 * as these have to be really removed in KERNEL.621. Thus, the BP value
2501 * that we place in the temporary area to be restored, contains the value
2502 * that SP would have if no arguments were removed. By comparing the actual
2503 * value of SP with this value in our return stub we can compute the number
2504 * of removed arguments. This is then returned to KERNEL.621.
2506 * The stack layout of this function:
2507 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
2508 * (ebp+16) esi pointer to caller's esi value
2509 * (ebp+12) arg ebp value to be set for relay stub
2510 * (ebp+8) func CBClient relay stub address
2511 * (ebp+4) ret addr
2512 * (ebp) ebp
2514 static void BuildCallTo32CBClient( FILE *outfile, BOOL isEx )
2516 char *name = isEx? "CBClientEx" : "CBClient";
2517 int size = isEx? 24 : 12;
2519 /* Function header */
2521 fprintf( outfile, "\n\t.align 4\n" );
2522 #ifdef USE_STABS
2523 fprintf( outfile, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX "CALL32_%s\n",
2524 name, name );
2525 #endif
2526 fprintf( outfile, "\t.globl " PREFIX "CALL32_%s\n", name );
2527 fprintf( outfile, PREFIX "CALL32_%s:\n", name );
2529 /* Entry code */
2531 fprintf( outfile, "\tpushl %%ebp\n" );
2532 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2533 fprintf( outfile, "\tpushl %%edi\n" );
2534 fprintf( outfile, "\tpushl %%esi\n" );
2535 fprintf( outfile, "\tpushl %%ebx\n" );
2537 /* Get the 16-bit stack */
2539 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET);
2541 /* Convert it to a flat address */
2543 fprintf( outfile, "\tshldl $16,%%ebx,%%eax\n" );
2544 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
2545 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%esi\n" );
2546 fprintf( outfile, "\tmovw %%bx,%%ax\n" );
2547 fprintf( outfile, "\taddl %%eax,%%esi\n" );
2549 /* Allocate temporary area (simulate STACK16_PUSH) */
2551 fprintf( outfile, "\tpushf\n" );
2552 fprintf( outfile, "\tcld\n" );
2553 fprintf( outfile, "\tleal -%d(%%esi), %%edi\n", size );
2554 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2555 fprintf( outfile, "\trep\n\tmovsb\n" );
2556 fprintf( outfile, "\tpopf\n" );
2558 fprintf( outfile, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size, STACKOFFSET );
2560 fprintf( outfile, "\tpushl %%edi\n" ); /* remember address */
2562 /* Set up temporary area */
2564 if ( !isEx )
2566 fprintf( outfile, "\tleal 4(%%edi), %%edi\n" );
2568 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2569 fprintf( outfile, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */
2571 fprintf( outfile, "\tmovl %%ss, %%ax\n" );
2572 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2573 fprintf( outfile, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */
2575 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 + 4 );
2576 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */
2578 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
2579 fprintf( outfile, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */
2581 else
2583 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 );
2584 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" );
2586 fprintf( outfile, "\tmovl %%ds, %%ax\n" );
2587 fprintf( outfile, "\tmovw %%ax, 4(%%edi)\n" );
2589 fprintf( outfile, "\taddl $20, %%ebx\n" );
2590 fprintf( outfile, "\tmovw %%bx, 10(%%edi)\n" );
2592 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2593 fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
2595 fprintf( outfile, "\tmovl %%ss, %%ax\n" );
2596 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2597 fprintf( outfile, "\tmovl %%eax, 16(%%edi)\n" );
2599 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
2600 fprintf( outfile, "\tmovl %%eax, 20(%%edi)\n" );
2603 /* Set up registers and call CBClient relay stub (simulating a far call) */
2605 fprintf( outfile, "\tmovl 16(%%ebp), %%esi\n" );
2606 fprintf( outfile, "\tmovl (%%esi), %%esi\n" );
2608 fprintf( outfile, "\tmovl %%edi, %%ebx\n" );
2609 fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
2610 fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
2612 fprintf( outfile, "\tpushl %%cs\n" );
2613 fprintf( outfile, "\tcall *%%eax\n" );
2615 /* Return new esi value to caller */
2617 fprintf( outfile, "\tmovl 32(%%esp), %%edi\n" );
2618 fprintf( outfile, "\tmovl %%esi, (%%edi)\n" );
2620 /* Cleanup temporary area (simulate STACK16_POP) */
2622 fprintf( outfile, "\tpop %%esi\n" );
2624 fprintf( outfile, "\tpushf\n" );
2625 fprintf( outfile, "\tstd\n" );
2626 fprintf( outfile, "\tdec %%esi\n" );
2627 fprintf( outfile, "\tleal %d(%%esi), %%edi\n", size );
2628 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2629 fprintf( outfile, "\trep\n\tmovsb\n" );
2630 fprintf( outfile, "\tpopf\n" );
2632 fprintf( outfile, "\t.byte 0x64\n\taddw $%d,(%d)\n", size, STACKOFFSET );
2634 /* Return argument size to caller */
2635 if ( isEx )
2637 fprintf( outfile, "\tmovl 32(%%esp), %%ebx\n" );
2638 fprintf( outfile, "\tmovl %%ebp, (%%ebx)\n" );
2641 /* Restore registers and return */
2643 fprintf( outfile, "\tpopl %%ebx\n" );
2644 fprintf( outfile, "\tpopl %%esi\n" );
2645 fprintf( outfile, "\tpopl %%edi\n" );
2646 fprintf( outfile, "\tpopl %%ebp\n" );
2647 fprintf( outfile, "\tret\n" );
2650 static void BuildCallTo32CBClientRet( FILE *outfile, BOOL isEx )
2652 char *name = isEx? "CBClientEx" : "CBClient";
2654 /* '16-bit' return stub */
2656 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_Ret\n", name );
2657 fprintf( outfile, PREFIX "CALL32_%s_Ret:\n", name );
2659 if ( !isEx )
2661 fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
2662 fprintf( outfile, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
2664 else
2666 fprintf( outfile, "\tmovzwl %%bp, %%ebx\n" );
2667 fprintf( outfile, "\tsubw %%bp, %%sp\n" );
2668 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
2669 fprintf( outfile, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
2671 fprintf( outfile, "\tlret\n" );
2673 /* Declare the return address variable */
2675 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_RetAddr\n", name );
2676 fprintf( outfile, PREFIX "CALL32_%s_RetAddr:\t.long 0\n", name );
2680 /*******************************************************************
2681 * BuildCallTo32LargeStack
2683 * Build the function used to switch to the original 32-bit stack
2684 * before calling a 32-bit function from 32-bit code. This is used for
2685 * functions that need a large stack, like X bitmaps functions.
2687 * The generated function has the following prototype:
2688 * int xxx( int (*func)(), void *arg );
2690 * The pointer to the function can be retrieved by calling CALL32_Init,
2691 * which also takes care of saving the current 32-bit stack pointer.
2692 * Furthermore, CALL32_Init switches to a new stack and jumps to the
2693 * specified target address.
2695 * NOTE: The CALL32_LargeStack routine may be recursively entered by the
2696 * same thread, but not concurrently entered by several threads.
2698 * Stack layout of CALL32_Init:
2700 * (esp+12) new stack address
2701 * (esp+8) target address
2702 * (esp+4) pointer to variable to receive CALL32_LargeStack address
2703 * (esp) ret addr
2705 * Stack layout of CALL32_LargeStack:
2706 * ... ...
2707 * (ebp+12) arg
2708 * (ebp+8) func
2709 * (ebp+4) ret addr
2710 * (ebp) ebp
2712 static void BuildCallTo32LargeStack( FILE *outfile )
2714 /* Initialization function */
2716 fprintf( outfile, "\n\t.align 4\n" );
2717 #ifdef USE_STABS
2718 fprintf( outfile, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX "CALL32_Init\n");
2719 #endif
2720 fprintf( outfile, "\t.globl " PREFIX "CALL32_Init\n" );
2721 fprintf( outfile, "\t.type " PREFIX "CALL32_Init,@function\n" );
2722 fprintf( outfile, PREFIX "CALL32_Init:\n" );
2723 fprintf( outfile, "\tmovl %%esp,CALL32_Original32_esp\n" );
2724 fprintf( outfile, "\tpopl %%eax\n" );
2725 fprintf( outfile, "\tpopl %%eax\n" );
2726 fprintf( outfile, "\tmovl $CALL32_LargeStack,(%%eax)\n" );
2727 fprintf( outfile, "\tpopl %%eax\n" );
2728 fprintf( outfile, "\tpopl %%esp\n" );
2729 fprintf( outfile, "\tpushl %%eax\n" );
2730 fprintf( outfile, "\tret\n" );
2732 /* Function header */
2734 fprintf( outfile, "\n\t.align 4\n" );
2735 #ifdef USE_STABS
2736 fprintf( outfile, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2737 #endif
2738 fprintf( outfile, "CALL32_LargeStack:\n" );
2740 /* Entry code */
2742 fprintf( outfile, "\tpushl %%ebp\n" );
2743 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2745 /* Switch to the original 32-bit stack pointer */
2747 fprintf( outfile, "\tcmpl $0, CALL32_RecursionCount\n" );
2748 fprintf( outfile, "\tjne CALL32_skip\n" );
2749 fprintf( outfile, "\tmovl CALL32_Original32_esp, %%esp\n" );
2750 fprintf( outfile, "CALL32_skip:\n" );
2752 fprintf( outfile, "\tincl CALL32_RecursionCount\n" );
2754 /* Transfer the argument and call the function */
2756 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2757 fprintf( outfile, "\tcall *8(%%ebp)\n" );
2759 /* Restore registers and return */
2761 fprintf( outfile, "\tdecl CALL32_RecursionCount\n" );
2763 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2764 fprintf( outfile, "\tpopl %%ebp\n" );
2765 fprintf( outfile, "\tret\n" );
2767 /* Data */
2769 fprintf( outfile, "\t.data\n" );
2770 fprintf( outfile, "CALL32_Original32_esp:\t.long 0\n" );
2771 fprintf( outfile, "CALL32_RecursionCount:\t.long 0\n" );
2772 fprintf( outfile, "\t.text\n" );
2776 /*******************************************************************
2777 * BuildCallFrom32Regs
2779 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2780 * 'args' is the number of dword arguments.
2782 * Stack layout:
2783 * ...
2784 * (ebp+12) first arg
2785 * (ebp+8) ret addr to user code
2786 * (ebp+4) ret addr to relay code
2787 * (ebp+0) saved ebp
2788 * (ebp-128) buffer area to allow stack frame manipulation
2789 * (ebp-332) CONTEXT86 struct
2790 * (ebp-336) CONTEXT86 *argument
2791 * .... other arguments copied from (ebp+12)
2793 * The entry point routine is called with a CONTEXT* extra argument,
2794 * following the normal args. In this context structure, EIP_reg
2795 * contains the return address to user code, and ESP_reg the stack
2796 * pointer on return (with the return address and arguments already
2797 * removed).
2799 static void BuildCallFrom32Regs( FILE *outfile )
2801 static const int STACK_SPACE = 128 + sizeof(CONTEXT86);
2803 /* Function header */
2805 fprintf( outfile, "\n\t.align 4\n" );
2806 #ifdef USE_STABS
2807 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
2808 #endif
2809 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
2810 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
2812 /* Allocate some buffer space on the stack */
2814 fprintf( outfile, "\tpushl %%ebp\n" );
2815 fprintf( outfile, "\tmovl %%esp,%%ebp\n ");
2816 fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
2818 /* Build the context structure */
2820 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
2821 fprintf( outfile, "\tpushfl\n" );
2822 fprintf( outfile, "\tpopl %%eax\n" );
2823 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
2824 fprintf( outfile, "\tmovl 0(%%ebp),%%eax\n" );
2825 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
2826 fprintf( outfile, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
2827 fprintf( outfile, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
2828 fprintf( outfile, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
2829 fprintf( outfile, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
2830 fprintf( outfile, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
2832 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2833 fprintf( outfile, "\tmovw %%cs,%%ax\n" );
2834 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs) - STACK_SPACE );
2835 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2836 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
2837 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2838 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
2839 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2840 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
2841 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
2842 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs) - STACK_SPACE );
2843 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2844 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs) - STACK_SPACE );
2845 fprintf( outfile, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
2847 fprintf( outfile, "\tmovl $0x%x,%%eax\n", CONTEXT_FULL );
2848 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags) - STACK_SPACE );
2850 fprintf( outfile, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
2851 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
2853 /* Transfer the arguments */
2855 fprintf( outfile, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
2856 fprintf( outfile, "\tpushl %%esp\n" ); /* push ptr to context struct */
2857 fprintf( outfile, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
2858 fprintf( outfile, "\tjecxz 1f\n" );
2859 fprintf( outfile, "\tsubl %%ecx,%%esp\n" );
2860 fprintf( outfile, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */
2861 fprintf( outfile, "\tmovl %%esp,%%edi\n" );
2862 fprintf( outfile, "\tshrl $2,%%ecx\n" );
2863 fprintf( outfile, "\tcld\n" );
2864 fprintf( outfile, "\trep\n\tmovsl\n" ); /* copy args */
2866 fprintf( outfile, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
2867 fprintf( outfile, "\tleal 12(%%ebp,%%eax),%%eax\n" );
2868 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2870 /* Call the entry point */
2872 fprintf( outfile, "\tcall *0(%%ebx)\n" );
2874 /* Store %eip and %ebp onto the new stack */
2876 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2877 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
2878 fprintf( outfile, "\tmovl %%eax,-4(%%edx)\n" );
2879 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
2880 fprintf( outfile, "\tmovl %%eax,-8(%%edx)\n" );
2882 /* Restore the context structure */
2884 /* Note: we don't bother to restore %cs, %ds and %ss
2885 * changing them in 32-bit code is a recipe for disaster anyway
2887 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
2888 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2889 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
2890 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2891 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
2892 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2894 fprintf( outfile, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
2895 fprintf( outfile, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
2896 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
2897 fprintf( outfile, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
2898 fprintf( outfile, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
2900 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
2901 fprintf( outfile, "\tpushl %%eax\n" );
2902 fprintf( outfile, "\tpopfl\n" );
2903 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
2905 fprintf( outfile, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2906 fprintf( outfile, "\tleal -8(%%ebp),%%esp\n" );
2907 fprintf( outfile, "\tpopl %%ebp\n" );
2908 fprintf( outfile, "\tret\n" );
2912 /*******************************************************************
2913 * BuildSpec
2915 * Build the spec files
2917 static int BuildSpec( FILE *outfile, int argc, char *argv[] )
2919 int i;
2920 for (i = 2; i < argc; i++)
2921 if (BuildSpecFile( outfile, argv[i] ) < 0) return -1;
2922 return 0;
2925 /*******************************************************************
2926 * BuildGlue
2928 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
2930 static int BuildGlue( FILE *outfile, char * outname, int argc, char *argv[] )
2932 char buffer[1024];
2933 FILE *infile;
2935 if (argc > 2)
2937 infile = fopen( argv[2], "r" );
2938 if (!infile)
2940 perror( argv[2] );
2941 exit( 1 );
2944 else infile = stdin;
2946 /* File header */
2948 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2949 fprintf( outfile, "\t.text\n" );
2951 #ifdef USE_STABS
2952 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2953 getcwd(buffer, sizeof(buffer));
2956 * The stabs help the internal debugger as they are an indication that it
2957 * is sensible to step into a thunk/trampoline.
2959 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2960 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2961 fprintf( outfile, "\t.text\n" );
2962 fprintf( outfile, "\t.align 4\n" );
2963 fprintf( outfile, "Code_Start:\n\n" );
2964 #endif
2966 /* Build the callback glue functions */
2968 while (fgets( buffer, sizeof(buffer), infile ))
2970 if (strstr( buffer, "### start build ###" )) break;
2972 while (fgets( buffer, sizeof(buffer), infile ))
2974 char *p;
2975 if ( (p = strstr( buffer, "CallFrom16_" )) != NULL )
2977 char *q, *profile = p + strlen( "CallFrom16_" );
2978 for (q = profile; (*q == '_') || isalpha(*q); q++ )
2980 *q = '\0';
2981 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
2983 if ( ++q < p ) p[-1] = '\0'; else q = "";
2984 BuildCallFrom16Func( outfile, profile, q );
2986 if ( (p = strstr( buffer, "CallTo16_" )) != NULL )
2988 char *q, *profile = p + strlen( "CallTo16_" );
2989 for (q = profile; (*q == '_') || isalpha(*q); q++ )
2991 *q = '\0';
2992 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
2994 if ( ++q < p ) p[-1] = '\0'; else q = "";
2995 BuildCallTo16Func( outfile, profile, q );
2997 if (strstr( buffer, "### stop build ###" )) break;
3001 #ifdef USE_STABS
3002 fprintf( outfile, "\t.text\n");
3003 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
3004 fprintf( outfile, ".Letext:\n");
3005 #endif
3007 fclose( infile );
3008 return 0;
3011 /*******************************************************************
3012 * BuildCall16
3014 * Build the 16-bit callbacks
3016 static int BuildCall16( FILE *outfile, char * outname )
3018 #ifdef USE_STABS
3019 char buffer[1024];
3020 #endif
3022 /* File header */
3024 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
3025 fprintf( outfile, "\t.text\n" );
3027 #ifdef USE_STABS
3028 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
3029 getcwd(buffer, sizeof(buffer));
3032 * The stabs help the internal debugger as they are an indication that it
3033 * is sensible to step into a thunk/trampoline.
3035 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
3036 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
3037 fprintf( outfile, "\t.text\n" );
3038 fprintf( outfile, "\t.align 4\n" );
3039 fprintf( outfile, "Code_Start:\n\n" );
3040 #endif
3041 fprintf( outfile, PREFIX"Call16_Start:\n" );
3042 fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
3043 fprintf( outfile, "\t.byte 0\n\n" );
3046 /* Standard CallFrom16 routine */
3047 BuildCallFrom16Core( outfile, FALSE, FALSE );
3049 /* Register CallFrom16 routine */
3050 BuildCallFrom16Core( outfile, TRUE, FALSE );
3052 /* C16ThkSL CallFrom16 routine */
3053 BuildCallFrom16Core( outfile, FALSE, TRUE );
3055 /* Standard CallTo16 routine (WORD return) */
3056 BuildCallTo16Core( outfile, TRUE, FALSE );
3058 /* Standard CallTo16 routine (DWORD return) */
3059 BuildCallTo16Core( outfile, FALSE, FALSE );
3061 /* Register CallTo16 routine (16:16 retf) */
3062 BuildCallTo16Core( outfile, FALSE, 1 );
3064 /* Register CallTo16 routine (16:32 retf) */
3065 BuildCallTo16Core( outfile, FALSE, 2 );
3067 /* CBClientThunkSL routine */
3068 BuildCallTo32CBClient( outfile, FALSE );
3070 /* CBClientThunkSLEx routine */
3071 BuildCallTo32CBClient( outfile, TRUE );
3073 fprintf( outfile, PREFIX"Call16_End:\n" );
3074 fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
3076 #ifdef USE_STABS
3077 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
3078 fprintf( outfile, ".Letext:\n");
3079 #endif
3081 /* The whole Call16_Ret segment must lie within the .data section */
3082 fprintf( outfile, "\n\t.data\n" );
3083 fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
3084 fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
3086 /* Standard CallTo16 return stub */
3087 BuildRet16Func( outfile );
3089 /* CBClientThunkSL return stub */
3090 BuildCallTo32CBClientRet( outfile, FALSE );
3092 /* CBClientThunkSLEx return stub */
3093 BuildCallTo32CBClientRet( outfile, TRUE );
3095 /* End of Call16_Ret segment */
3096 fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
3097 fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
3099 return 0;
3102 /*******************************************************************
3103 * BuildCall32
3105 * Build the 32-bit callbacks
3107 static int BuildCall32( FILE *outfile, char * outname )
3109 #ifdef USE_STABS
3110 char buffer[1024];
3111 #endif
3113 /* File header */
3115 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
3116 fprintf( outfile, "\t.text\n" );
3118 #ifdef __i386__
3120 #ifdef USE_STABS
3121 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
3122 getcwd(buffer, sizeof(buffer));
3125 * The stabs help the internal debugger as they are an indication that it
3126 * is sensible to step into a thunk/trampoline.
3128 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
3129 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
3130 fprintf( outfile, "\t.text\n" );
3131 fprintf( outfile, "\t.align 4\n" );
3132 fprintf( outfile, "Code_Start:\n" );
3133 #endif
3135 /* Build the 32-bit large stack callback */
3137 BuildCallTo32LargeStack( outfile );
3139 /* Build the register callback function */
3141 BuildCallFrom32Regs( outfile );
3143 #ifdef USE_STABS
3144 fprintf( outfile, "\t.text\n");
3145 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
3146 fprintf( outfile, ".Letext:\n");
3147 #endif
3149 #else /* __i386__ */
3151 /* Just to avoid an empty file */
3152 fprintf( outfile, "\t.long 0\n" );
3154 #endif /* __i386__ */
3155 return 0;
3159 /*******************************************************************
3160 * usage
3162 static void usage(void)
3164 fprintf( stderr,
3165 "usage: build [-o outfile] -spec SPECNAMES\n"
3166 " build [-o outfile] -glue SOURCE_FILE\n"
3167 " build [-o outfile] -call16\n"
3168 " build [-o outfile] -call32\n" );
3169 exit(1);
3173 /*******************************************************************
3174 * main
3176 int main(int argc, char **argv)
3178 char *outname = NULL;
3179 FILE *outfile = stdout;
3180 int res = -1;
3182 if (argc < 2) usage();
3184 if (!strcmp( argv[1], "-o" ))
3186 outname = argv[2];
3187 argv += 2;
3188 argc -= 2;
3189 if (argc < 2) usage();
3190 if (!(outfile = fopen( outname, "w" )))
3192 fprintf( stderr, "Unable to create output file '%s'\n", outname );
3193 exit(1);
3197 /* Retrieve the selector values; this assumes that we are building
3198 * the asm files on the platform that will also run them. Probably
3199 * a safe assumption to make.
3201 GET_CS( Code_Selector );
3202 GET_DS( Data_Selector );
3204 if (!strcmp( argv[1], "-spec" ))
3205 res = BuildSpec( outfile, argc, argv );
3206 else if (!strcmp( argv[1], "-glue" ))
3207 res = BuildGlue( outfile, outname, argc, argv );
3208 else if (!strcmp( argv[1], "-call16" ))
3209 res = BuildCall16( outfile, outname );
3210 else if (!strcmp( argv[1], "-call32" ))
3211 res = BuildCall32( outfile, outname );
3212 else
3214 fclose( outfile );
3215 unlink( outname );
3216 usage();
3219 fclose( outfile );
3220 if (res < 0)
3222 unlink( outname );
3223 return 1;
3225 return 0;