Removed extra output.
[wine/multimedia.git] / tools / build.c
blob1d474f5e14811fff33064e3c8d753eb8e38dafca
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 */
8 #include <assert.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <unistd.h>
15 #include "windows.h"
16 #include "winnt.h"
17 #include "module.h"
18 #include "neexe.h"
19 #include "selectors.h"
20 #include "stackframe.h"
21 #include "thread.h"
23 #ifdef NEED_UNDERSCORE_PREFIX
24 # define PREFIX "_"
25 #else
26 # define PREFIX
27 #endif
29 #ifdef HAVE_ASM_STRING
30 # define STRING ".string"
31 #else
32 # define STRING ".ascii"
33 #endif
35 #if defined(__GNUC__) && !defined(__svr4__)
36 # define USE_STABS
37 #else
38 # undef USE_STABS
39 #endif
41 typedef enum
43 TYPE_INVALID,
44 TYPE_BYTE, /* byte variable (Win16) */
45 TYPE_WORD, /* word variable (Win16) */
46 TYPE_LONG, /* long variable (Win16) */
47 TYPE_PASCAL_16, /* pascal function with 16-bit return (Win16) */
48 TYPE_PASCAL, /* pascal function with 32-bit return (Win16) */
49 TYPE_ABS, /* absolute value (Win16) */
50 TYPE_RETURN, /* simple return value function (Win16) */
51 TYPE_REGISTER, /* register function */
52 TYPE_STUB, /* unimplemented stub */
53 TYPE_STDCALL, /* stdcall function (Win32) */
54 TYPE_CDECL, /* cdecl function (Win32) */
55 TYPE_VARARGS, /* varargs function (Win32) */
56 TYPE_EXTERN, /* external symbol (Win32) */
57 TYPE_NBTYPES
58 } ORD_TYPE;
60 static const char * const TypeNames[TYPE_NBTYPES] =
62 NULL,
63 "byte", /* TYPE_BYTE */
64 "word", /* TYPE_WORD */
65 "long", /* TYPE_LONG */
66 "pascal16", /* TYPE_PASCAL_16 */
67 "pascal", /* TYPE_PASCAL */
68 "equate", /* TYPE_ABS */
69 "return", /* TYPE_RETURN */
70 "register", /* TYPE_REGISTER */
71 "stub", /* TYPE_STUB */
72 "stdcall", /* TYPE_STDCALL */
73 "cdecl", /* TYPE_CDECL */
74 "varargs", /* TYPE_VARARGS */
75 "extern" /* TYPE_EXTERN */
78 #define MAX_ORDINALS 2048
80 /* Callback function used for stub functions */
81 #define STUB_CALLBACK \
82 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
84 typedef enum
86 SPEC_INVALID,
87 SPEC_WIN16,
88 SPEC_WIN32
89 } SPEC_TYPE;
91 typedef struct
93 int n_values;
94 int *values;
95 } ORD_VARIABLE;
97 typedef struct
99 int n_args;
100 char arg_types[32];
101 char link_name[80];
102 } ORD_FUNCTION;
104 typedef struct
106 int arg_size;
107 int ret_value;
108 } ORD_RETURN;
110 typedef struct
112 int value;
113 } ORD_ABS;
115 typedef struct
117 char link_name[80];
118 } ORD_VARARGS;
120 typedef struct
122 char link_name[80];
123 } ORD_EXTERN;
125 typedef struct
127 ORD_TYPE type;
128 int offset;
129 int lineno;
130 char name[80];
131 union
133 ORD_VARIABLE var;
134 ORD_FUNCTION func;
135 ORD_RETURN ret;
136 ORD_ABS abs;
137 ORD_VARARGS vargs;
138 ORD_EXTERN ext;
139 } u;
140 } ORDDEF;
142 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
144 static SPEC_TYPE SpecType = SPEC_INVALID;
145 static char DLLName[80];
146 static char DLLFileName[80];
147 static int Limit = 0;
148 static int Base = MAX_ORDINALS;
149 static int DLLHeapSize = 0;
150 static char *SpecName;
151 static FILE *SpecFp;
152 static WORD Code_Selector, Data_Selector;
153 static char DLLInitFunc[80];
155 char *ParseBuffer = NULL;
156 char *ParseNext;
157 char ParseSaveChar;
158 int Line;
160 static int debugging = 1;
162 /* Offset of a structure field relative to the start of the struct */
163 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
165 /* Offset of register relative to the start of the CONTEXT struct */
166 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT,reg)
168 /* Offset of the stack pointer relative to %fs:(0) */
169 #define STACKOFFSET (STRUCTOFFSET(THDB,cur_stack) - STRUCTOFFSET(THDB,teb))
172 static void *xmalloc (size_t size)
174 void *res;
176 res = malloc (size ? size : 1);
177 if (res == NULL)
179 fprintf (stderr, "Virtual memory exhausted.\n");
180 exit (1);
182 return res;
186 static void *xrealloc (void *ptr, size_t size)
188 void *res = realloc (ptr, size);
189 if (res == NULL)
191 fprintf (stderr, "Virtual memory exhausted.\n");
192 exit (1);
194 return res;
198 static int IsNumberString(char *s)
200 while (*s != '\0')
201 if (!isdigit(*s++))
202 return 0;
204 return 1;
207 static char *strupper(char *s)
209 char *p;
211 for(p = s; *p != '\0'; p++)
212 *p = toupper(*p);
214 return s;
217 static char * GetTokenInLine(void)
219 char *p;
220 char *token;
222 if (ParseNext != ParseBuffer)
224 if (ParseSaveChar == '\0')
225 return NULL;
226 *ParseNext = ParseSaveChar;
230 * Remove initial white space.
232 for (p = ParseNext; isspace(*p); p++)
235 if ((*p == '\0') || (*p == '#'))
236 return NULL;
239 * Find end of token.
241 token = p++;
242 if (*token != '(' && *token != ')')
243 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
244 p++;
246 ParseSaveChar = *p;
247 ParseNext = p;
248 *p = '\0';
250 return token;
253 static char * GetToken(void)
255 char *token;
257 if (ParseBuffer == NULL)
259 ParseBuffer = xmalloc(512);
260 ParseNext = ParseBuffer;
261 while (1)
263 Line++;
264 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
265 return NULL;
266 if (ParseBuffer[0] != '#')
267 break;
271 while ((token = GetTokenInLine()) == NULL)
273 ParseNext = ParseBuffer;
274 while (1)
276 Line++;
277 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
278 return NULL;
279 if (ParseBuffer[0] != '#')
280 break;
284 return token;
288 /*******************************************************************
289 * ParseVariable
291 * Parse a variable definition.
293 static int ParseVariable( ORDDEF *odp )
295 char *endptr;
296 int *value_array;
297 int n_values;
298 int value_array_size;
300 char *token = GetToken();
301 if (*token != '(')
303 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
304 SpecName, Line, token);
305 return -1;
308 n_values = 0;
309 value_array_size = 25;
310 value_array = xmalloc(sizeof(*value_array) * value_array_size);
312 while ((token = GetToken()) != NULL)
314 if (*token == ')')
315 break;
317 value_array[n_values++] = strtol(token, &endptr, 0);
318 if (n_values == value_array_size)
320 value_array_size += 25;
321 value_array = xrealloc(value_array,
322 sizeof(*value_array) * value_array_size);
325 if (endptr == NULL || *endptr != '\0')
327 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
328 SpecName, Line, token);
329 return -1;
333 if (token == NULL)
335 fprintf(stderr, "%s:%d: End of file in variable declaration\n",
336 SpecName, Line);
337 return -1;
340 odp->u.var.n_values = n_values;
341 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
343 return 0;
347 /*******************************************************************
348 * ParseExportFunction
350 * Parse a function definition.
352 static int ParseExportFunction( ORDDEF *odp )
354 char *token;
355 int i;
357 switch(SpecType)
359 case SPEC_WIN16:
360 if (odp->type == TYPE_STDCALL)
362 fprintf( stderr, "%s:%d: 'stdcall' not supported for Win16\n",
363 SpecName, Line );
364 return -1;
366 break;
367 case SPEC_WIN32:
368 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
370 fprintf( stderr, "%s:%d: 'pascal' not supported for Win32\n",
371 SpecName, Line );
372 return -1;
374 break;
375 default:
376 break;
379 token = GetToken();
380 if (*token != '(')
382 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
383 SpecName, Line, token);
384 return -1;
387 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
389 token = GetToken();
390 if (*token == ')')
391 break;
393 if (!strcmp(token, "word"))
394 odp->u.func.arg_types[i] = 'w';
395 else if (!strcmp(token, "s_word"))
396 odp->u.func.arg_types[i] = 's';
397 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
398 odp->u.func.arg_types[i] = 'l';
399 else if (!strcmp(token, "ptr"))
400 odp->u.func.arg_types[i] = 'p';
401 else if (!strcmp(token, "str"))
402 odp->u.func.arg_types[i] = 't';
403 else if (!strcmp(token, "wstr"))
404 odp->u.func.arg_types[i] = 'W';
405 else if (!strcmp(token, "segstr"))
406 odp->u.func.arg_types[i] = 'T';
407 else if (!strcmp(token, "double"))
409 odp->u.func.arg_types[i++] = 'l';
410 odp->u.func.arg_types[i] = 'l';
412 else
414 fprintf(stderr, "%s:%d: Unknown variable type '%s'\n",
415 SpecName, Line, token);
416 return -1;
418 if (SpecType == SPEC_WIN32)
420 if (strcmp(token, "long") &&
421 strcmp(token, "ptr") &&
422 strcmp(token, "str") &&
423 strcmp(token, "wstr") &&
424 strcmp(token, "double"))
426 fprintf( stderr, "%s:%d: Type '%s' not supported for Win32\n",
427 SpecName, Line, token );
428 return -1;
432 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
434 fprintf( stderr, "%s:%d: Too many arguments\n", SpecName, Line );
435 return -1;
437 odp->u.func.arg_types[i] = '\0';
438 if ((odp->type == TYPE_STDCALL) && !i)
439 odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
440 if ((odp->type == TYPE_REGISTER) && (SpecType == SPEC_WIN32) && i)
442 fprintf( stderr, "%s:%d: register functions cannot have arguments in Win32\n",
443 SpecName, Line );
444 return -1;
446 strcpy(odp->u.func.link_name, GetToken());
447 return 0;
451 /*******************************************************************
452 * ParseEquate
454 * Parse an 'equate' definition.
456 static int ParseEquate( ORDDEF *odp )
458 char *endptr;
460 char *token = GetToken();
461 int value = strtol(token, &endptr, 0);
462 if (endptr == NULL || *endptr != '\0')
464 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
465 SpecName, Line, token);
466 return -1;
469 if (SpecType == SPEC_WIN32)
471 fprintf( stderr, "%s:%d: 'equate' not supported for Win32\n",
472 SpecName, Line );
473 return -1;
476 odp->u.abs.value = value;
477 return 0;
481 /*******************************************************************
482 * ParseReturn
484 * Parse a 'return' definition.
486 static int ParseReturn( ORDDEF *odp )
488 char *token;
489 char *endptr;
491 token = GetToken();
492 odp->u.ret.arg_size = strtol(token, &endptr, 0);
493 if (endptr == NULL || *endptr != '\0')
495 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
496 SpecName, Line, token);
497 return -1;
500 token = GetToken();
501 odp->u.ret.ret_value = strtol(token, &endptr, 0);
502 if (endptr == NULL || *endptr != '\0')
504 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
505 SpecName, Line, token);
506 return -1;
509 if (SpecType == SPEC_WIN32)
511 fprintf( stderr, "%s:%d: 'return' not supported for Win32\n",
512 SpecName, Line );
513 return -1;
516 return 0;
520 /*******************************************************************
521 * ParseStub
523 * Parse a 'stub' definition.
525 static int ParseStub( ORDDEF *odp )
527 odp->u.func.arg_types[0] = '\0';
528 strcpy( odp->u.func.link_name, STUB_CALLBACK );
529 return 0;
533 /*******************************************************************
534 * ParseVarargs
536 * Parse an 'varargs' definition.
538 static int ParseVarargs( ORDDEF *odp )
540 char *token;
542 if (SpecType == SPEC_WIN16)
544 fprintf( stderr, "%s:%d: 'varargs' not supported for Win16\n",
545 SpecName, Line );
546 return -1;
549 token = GetToken();
550 if (*token != '(')
552 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
553 SpecName, Line, token);
554 return -1;
556 token = GetToken();
557 if (*token != ')')
559 fprintf(stderr, "%s:%d: Expected ')' got '%s'\n",
560 SpecName, Line, token);
561 return -1;
564 strcpy( odp->u.vargs.link_name, GetToken() );
565 return 0;
569 /*******************************************************************
570 * ParseExtern
572 * Parse an 'extern' definition.
574 static int ParseExtern( ORDDEF *odp )
576 if (SpecType == SPEC_WIN16)
578 fprintf( stderr, "%s:%d: 'extern' not supported for Win16\n",
579 SpecName, Line );
580 return -1;
582 strcpy( odp->u.ext.link_name, GetToken() );
583 return 0;
587 /*******************************************************************
588 * ParseOrdinal
590 * Parse an ordinal definition.
592 static int ParseOrdinal(int ordinal)
594 ORDDEF *odp;
595 char *token;
597 if (ordinal >= MAX_ORDINALS)
599 fprintf(stderr, "%s:%d: Ordinal number too large\n", SpecName, Line );
600 return -1;
602 if (ordinal > Limit) Limit = ordinal;
603 if (ordinal < Base) Base = ordinal;
605 odp = &OrdinalDefinitions[ordinal];
606 if (!(token = GetToken()))
608 fprintf(stderr, "%s:%d: Expected type after ordinal\n", SpecName, Line);
609 return -1;
612 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
613 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
614 break;
616 if (odp->type >= TYPE_NBTYPES)
618 fprintf( stderr,
619 "%s:%d: Expected type after ordinal, found '%s' instead\n",
620 SpecName, Line, token );
621 return -1;
624 if (!(token = GetToken()))
626 fprintf( stderr, "%s:%d: Expected name after type\n", SpecName, Line );
627 return -1;
629 strcpy( odp->name, token );
630 odp->lineno = Line;
632 switch(odp->type)
634 case TYPE_BYTE:
635 case TYPE_WORD:
636 case TYPE_LONG:
637 return ParseVariable( odp );
638 case TYPE_PASCAL_16:
639 case TYPE_PASCAL:
640 case TYPE_REGISTER:
641 case TYPE_STDCALL:
642 case TYPE_CDECL:
643 return ParseExportFunction( odp );
644 case TYPE_ABS:
645 return ParseEquate( odp );
646 case TYPE_RETURN:
647 return ParseReturn( odp );
648 case TYPE_STUB:
649 return ParseStub( odp );
650 case TYPE_VARARGS:
651 return ParseVarargs( odp );
652 case TYPE_EXTERN:
653 return ParseExtern( odp );
654 default:
655 fprintf( stderr, "Should not happen\n" );
656 return -1;
661 /*******************************************************************
662 * ParseTopLevel
664 * Parse a spec file.
666 static int ParseTopLevel(void)
668 char *token;
670 while ((token = GetToken()) != NULL)
672 if (strcmp(token, "name") == 0)
674 strcpy(DLLName, GetToken());
675 strupper(DLLName);
676 if (!DLLFileName[0]) sprintf( DLLFileName, "%s.DLL", DLLName );
678 else if (strcmp(token, "file") == 0)
680 strcpy(DLLFileName, GetToken());
681 strupper(DLLFileName);
683 else if (strcmp(token, "type") == 0)
685 token = GetToken();
686 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
687 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
688 else
690 fprintf(stderr, "%s:%d: Type must be 'win16' or 'win32'\n",
691 SpecName, Line);
692 return -1;
695 else if (strcmp(token, "heap") == 0)
697 token = GetToken();
698 if (!IsNumberString(token))
700 fprintf(stderr, "%s:%d: Expected number after heap\n",
701 SpecName, Line);
702 return -1;
704 DLLHeapSize = atoi(token);
706 else if (strcmp(token, "init") == 0)
708 strcpy(DLLInitFunc, GetToken());
709 if (!DLLInitFunc[0])
710 fprintf(stderr, "%s:%d: Expected function name after init\n", SpecName, Line);
712 else if (IsNumberString(token))
714 int ordinal;
715 int rv;
717 ordinal = atoi(token);
718 if ((rv = ParseOrdinal(ordinal)) < 0)
719 return rv;
721 else
723 fprintf(stderr,
724 "%s:%d: Expected name, id, length or ordinal\n",
725 SpecName, Line);
726 return -1;
730 return 0;
734 /*******************************************************************
735 * StoreVariableCode
737 * Store a list of ints into a byte array.
739 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
741 int i;
743 switch(size)
745 case 1:
746 for (i = 0; i < odp->u.var.n_values; i++)
747 buffer[i] = odp->u.var.values[i];
748 break;
749 case 2:
750 for (i = 0; i < odp->u.var.n_values; i++)
751 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
752 break;
753 case 4:
754 for (i = 0; i < odp->u.var.n_values; i++)
755 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
756 break;
758 return odp->u.var.n_values * size;
762 /*******************************************************************
763 * DumpBytes
765 * Dump a byte stream into the assembly code.
767 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
768 const char *section, const char *label_start )
770 int i;
771 if (section) fprintf( outfile, "\t%s\n", section );
772 if (label_start) fprintf( outfile, "%s:\n", label_start );
773 for (i = 0; i < len; i++)
775 if (!(i & 0x0f)) fprintf( outfile, "\t.byte " );
776 fprintf( outfile, "%d", *data++ );
777 if (i < len - 1)
778 fprintf( outfile, "%c", ((i & 0x0f) != 0x0f) ? ',' : '\n' );
780 fprintf( outfile, "\n" );
784 /*******************************************************************
785 * BuildModule16
787 * Build the in-memory representation of a 16-bit NE module, and dump it
788 * as a byte stream into the assembly code.
790 static int BuildModule16( FILE *outfile, int max_code_offset,
791 int max_data_offset )
793 ORDDEF *odp;
794 int i;
795 char *buffer;
796 NE_MODULE *pModule;
797 SEGTABLEENTRY *pSegment;
798 OFSTRUCT *pFileInfo;
799 BYTE *pstr, *bundle;
800 WORD *pword;
802 /* Module layout:
803 * NE_MODULE Module
804 * OFSTRUCT File information
805 * SEGTABLEENTRY Segment 1 (code)
806 * SEGTABLEENTRY Segment 2 (data)
807 * WORD[2] Resource table (empty)
808 * BYTE[2] Imported names (empty)
809 * BYTE[n] Resident names table
810 * BYTE[n] Entry table
813 buffer = xmalloc( 0x10000 );
815 pModule = (NE_MODULE *)buffer;
816 pModule->magic = IMAGE_OS2_SIGNATURE;
817 pModule->count = 1;
818 pModule->next = 0;
819 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
820 pModule->dgroup = 2;
821 pModule->heap_size = DLLHeapSize;
822 pModule->stack_size = 0;
823 pModule->ip = 0;
824 pModule->cs = 0;
825 pModule->sp = 0;
826 pModule->ss = 0;
827 pModule->seg_count = 2;
828 pModule->modref_count = 0;
829 pModule->nrname_size = 0;
830 pModule->modref_table = 0;
831 pModule->nrname_fpos = 0;
832 pModule->moveable_entries = 0;
833 pModule->alignment = 0;
834 pModule->truetype = 0;
835 pModule->os_flags = NE_OSFLAGS_WINDOWS;
836 pModule->misc_flags = 0;
837 pModule->dlls_to_init = 0;
838 pModule->nrname_handle = 0;
839 pModule->min_swap_area = 0;
840 pModule->expected_version = 0x030a;
841 pModule->module32 = 0;
842 pModule->self = 0;
843 pModule->self_loading_sel = 0;
845 /* File information */
847 pFileInfo = (OFSTRUCT *)(pModule + 1);
848 pModule->fileinfo = (int)pFileInfo - (int)pModule;
849 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
850 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
851 + strlen(DLLFileName);
852 strcpy( pFileInfo->szPathName, DLLFileName );
853 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
855 /* Segment table */
857 pSegment = (SEGTABLEENTRY *)pstr;
858 pModule->seg_table = (int)pSegment - (int)pModule;
859 pSegment->filepos = 0;
860 pSegment->size = max_code_offset;
861 pSegment->flags = 0;
862 pSegment->minsize = max_code_offset;
863 pSegment->hSeg = 0;
864 pSegment++;
866 pModule->dgroup_entry = (int)pSegment - (int)pModule;
867 pSegment->filepos = 0;
868 pSegment->size = max_data_offset;
869 pSegment->flags = NE_SEGFLAGS_DATA;
870 pSegment->minsize = max_data_offset;
871 pSegment->hSeg = 0;
872 pSegment++;
874 /* Resource table */
876 pword = (WORD *)pSegment;
877 pModule->res_table = (int)pword - (int)pModule;
878 *pword++ = 0;
879 *pword++ = 0;
881 /* Imported names table */
883 pstr = (char *)pword;
884 pModule->import_table = (int)pstr - (int)pModule;
885 *pstr++ = 0;
886 *pstr++ = 0;
888 /* Resident names table */
890 pModule->name_table = (int)pstr - (int)pModule;
891 /* First entry is module name */
892 *pstr = strlen(DLLName );
893 strcpy( pstr + 1, DLLName );
894 pstr += *pstr + 1;
895 *(WORD *)pstr = 0;
896 pstr += sizeof(WORD);
897 /* Store all ordinals */
898 odp = OrdinalDefinitions + 1;
899 for (i = 1; i <= Limit; i++, odp++)
901 if (!odp->name[0]) continue;
902 *pstr = strlen( odp->name );
903 strcpy( pstr + 1, odp->name );
904 strupper( pstr + 1 );
905 pstr += *pstr + 1;
906 *(WORD *)pstr = i;
907 pstr += sizeof(WORD);
909 *pstr++ = 0;
911 /* Entry table */
913 pModule->entry_table = (int)pstr - (int)pModule;
914 bundle = NULL;
915 odp = OrdinalDefinitions + 1;
916 for (i = 1; i <= Limit; i++, odp++)
918 int selector = 0;
920 switch (odp->type)
922 case TYPE_CDECL:
923 case TYPE_PASCAL:
924 case TYPE_PASCAL_16:
925 case TYPE_REGISTER:
926 case TYPE_RETURN:
927 case TYPE_STUB:
928 selector = 1; /* Code selector */
929 break;
931 case TYPE_BYTE:
932 case TYPE_WORD:
933 case TYPE_LONG:
934 selector = 2; /* Data selector */
935 break;
937 case TYPE_ABS:
938 selector = 0xfe; /* Constant selector */
939 break;
941 default:
942 selector = 0; /* Invalid selector */
943 break;
946 /* create a new bundle if necessary */
947 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
949 bundle = pstr;
950 bundle[0] = 0;
951 bundle[1] = selector;
952 pstr += 2;
955 (*bundle)++;
956 if (selector != 0)
958 *pstr++ = 1;
959 *(WORD *)pstr = odp->offset;
960 pstr += sizeof(WORD);
963 *pstr++ = 0;
965 /* Dump the module content */
967 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
968 ".data", "Module_Start" );
969 return (int)pstr - (int)pModule;
973 /*******************************************************************
974 * BuildSpec32File
976 * Build a Win32 C file from a spec file.
978 static int BuildSpec32File( char * specfile, FILE *outfile )
980 ORDDEF *odp;
981 int i, nb_names;
983 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
984 specfile );
985 fprintf( outfile, "#include \"builtin32.h\"\n\n" );
987 /* Output code for all stubs functions */
989 fprintf( outfile, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
990 DLLName );
991 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
993 if (odp->type != TYPE_STUB) continue;
994 fprintf( outfile, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
995 i, DLLName, i );
998 /* Output code for all register functions */
1000 fprintf( outfile, "#ifdef __i386__\n" );
1001 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1003 if (odp->type != TYPE_REGISTER) continue;
1004 fprintf( outfile,
1005 "__asm__(\".align 4\\n\\t\"\n"
1006 " \".globl " PREFIX "%s\\n\\t\"\n"
1007 " \".type " PREFIX "%s,@function\\n\\t\"\n"
1008 " \"" PREFIX "%s:\\n\\t\"\n"
1009 " \"pushl $" PREFIX "__regs_%s\\n\\t\"\n"
1010 " \"pushl $" PREFIX "CALL32_Regs\\n\\t\"\n"
1011 " \"ret\");\n",
1012 odp->u.func.link_name, odp->u.func.link_name,
1013 odp->u.func.link_name, odp->u.func.link_name );
1015 fprintf( outfile, "#endif\n" );
1017 /* Output the DLL functions prototypes */
1019 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1021 switch(odp->type)
1023 case TYPE_VARARGS:
1024 fprintf( outfile, "extern void %s();\n", odp->u.vargs.link_name );
1025 break;
1026 case TYPE_EXTERN:
1027 fprintf( outfile, "extern void %s();\n", odp->u.ext.link_name );
1028 break;
1029 case TYPE_REGISTER:
1030 case TYPE_STDCALL:
1031 case TYPE_CDECL:
1032 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1033 break;
1034 case TYPE_INVALID:
1035 case TYPE_STUB:
1036 break;
1037 default:
1038 fprintf(stderr,"build: function type %d not available for Win32\n",
1039 odp->type);
1040 return -1;
1044 /* Output LibMain function */
1045 if (DLLInitFunc[0]) fprintf( outfile, "extern void %s();\n", DLLInitFunc );
1048 /* Output the DLL functions table */
1050 fprintf( outfile, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1051 Limit - Base + 1 );
1052 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1054 switch(odp->type)
1056 case TYPE_INVALID:
1057 fprintf( outfile, " 0" );
1058 break;
1059 case TYPE_VARARGS:
1060 fprintf( outfile, " %s", odp->u.vargs.link_name );
1061 break;
1062 case TYPE_EXTERN:
1063 fprintf( outfile, " %s", odp->u.ext.link_name );
1064 break;
1065 case TYPE_REGISTER:
1066 case TYPE_STDCALL:
1067 case TYPE_CDECL:
1068 fprintf( outfile, " %s", odp->u.func.link_name);
1069 break;
1070 case TYPE_STUB:
1071 fprintf( outfile, " __stub_%d", i );
1072 break;
1073 default:
1074 return -1;
1076 if (i < Limit) fprintf( outfile, ",\n" );
1078 fprintf( outfile, "\n};\n\n" );
1080 /* Output the DLL names table */
1082 nb_names = 0;
1083 fprintf( outfile, "static const char * const FuncNames[] =\n{\n" );
1084 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1086 if (odp->type == TYPE_INVALID) continue;
1087 if (nb_names++) fprintf( outfile, ",\n" );
1088 fprintf( outfile, " \"%s\"", odp->name );
1090 fprintf( outfile, "\n};\n\n" );
1092 /* Output the DLL argument types */
1094 fprintf( outfile, "static const unsigned int ArgTypes[%d] =\n{\n",
1095 Limit - Base + 1 );
1096 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1098 unsigned int j, mask = 0;
1099 if ((odp->type == TYPE_STDCALL) || (odp->type == TYPE_CDECL))
1100 for (j = 0; odp->u.func.arg_types[j]; j++)
1102 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
1103 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
1105 fprintf( outfile, " %d", mask );
1106 if (i < Limit) fprintf( outfile, ",\n" );
1108 fprintf( outfile, "\n};\n\n" );
1110 /* Output the DLL ordinals table */
1112 fprintf( outfile, "static const unsigned short FuncOrdinals[] =\n{\n" );
1113 nb_names = 0;
1114 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1116 if (odp->type == TYPE_INVALID) continue;
1117 if (nb_names++) fprintf( outfile, ",\n" );
1118 fprintf( outfile, " %d", i - Base );
1120 fprintf( outfile, "\n};\n\n" );
1122 /* Output the DLL functions arguments */
1124 fprintf( outfile, "static const unsigned char FuncArgs[%d] =\n{\n",
1125 Limit - Base + 1 );
1126 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1128 unsigned char args;
1129 switch(odp->type)
1131 case TYPE_STDCALL:
1132 args = (unsigned char)strlen(odp->u.func.arg_types);
1133 break;
1134 case TYPE_CDECL:
1135 args = 0x80 | (unsigned char)strlen(odp->u.func.arg_types);
1136 break;
1137 case TYPE_REGISTER:
1138 args = 0xfe;
1139 break;
1140 default:
1141 args = 0xff;
1142 break;
1144 fprintf( outfile, " 0x%02x", args );
1145 if (i < Limit) fprintf( outfile, ",\n" );
1147 fprintf( outfile, "\n};\n\n" );
1149 /* Output the DLL descriptor */
1151 fprintf( outfile, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1152 DLLName );
1153 fprintf( outfile, " \"%s\",\n", DLLName );
1154 fprintf( outfile, " %d,\n", Base );
1155 fprintf( outfile, " %d,\n", Limit - Base + 1 );
1156 fprintf( outfile, " %d,\n", nb_names );
1157 fprintf( outfile,
1158 " Functions,\n"
1159 " FuncNames,\n"
1160 " FuncOrdinals,\n"
1161 " FuncArgs,\n"
1162 " ArgTypes,\n");
1163 fprintf( outfile, " %s\n", DLLInitFunc[0] ? DLLInitFunc : "0" );
1164 fprintf( outfile, "};\n" );
1165 return 0;
1169 /*******************************************************************
1170 * BuildSpec16File
1172 * Build a Win16 assembly file from a spec file.
1174 static int BuildSpec16File( char * specfile, FILE *outfile )
1176 ORDDEF *odp;
1177 int i;
1178 int code_offset, data_offset, module_size;
1179 unsigned char *data;
1181 data = (unsigned char *)xmalloc( 0x10000 );
1182 memset( data, 0, 16 );
1183 data_offset = 16;
1185 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
1186 fprintf( outfile, "\t.text\n" );
1187 fprintf( outfile, "Code_Start:\n" );
1188 code_offset = 0;
1190 odp = OrdinalDefinitions;
1191 for (i = 0; i <= Limit; i++, odp++)
1193 switch (odp->type)
1195 case TYPE_INVALID:
1196 odp->offset = 0xffff;
1197 break;
1199 case TYPE_ABS:
1200 odp->offset = LOWORD(odp->u.abs.value);
1201 break;
1203 case TYPE_BYTE:
1204 odp->offset = data_offset;
1205 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1206 break;
1208 case TYPE_WORD:
1209 odp->offset = data_offset;
1210 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1211 break;
1213 case TYPE_LONG:
1214 odp->offset = data_offset;
1215 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1216 break;
1218 case TYPE_RETURN:
1219 fprintf( outfile,"/* %s.%d */\n", DLLName, i);
1220 fprintf( outfile,"\tmovw $%d,%%ax\n",LOWORD(odp->u.ret.ret_value));
1221 fprintf( outfile,"\tmovw $%d,%%dx\n",HIWORD(odp->u.ret.ret_value));
1222 fprintf( outfile,"\t.byte 0x66\n");
1223 if (odp->u.ret.arg_size != 0)
1224 fprintf( outfile, "\tlret $%d\n\n", odp->u.ret.arg_size);
1225 else
1227 fprintf( outfile, "\tlret\n");
1228 fprintf( outfile, "\tnop\n");
1229 fprintf( outfile, "\tnop\n\n");
1231 odp->offset = code_offset;
1232 code_offset += 12; /* Assembly code is 12 bytes long */
1233 break;
1235 case TYPE_REGISTER:
1236 case TYPE_CDECL:
1237 case TYPE_PASCAL:
1238 case TYPE_PASCAL_16:
1239 case TYPE_STUB:
1240 fprintf( outfile, "/* %s.%d */\n", DLLName, i);
1241 fprintf( outfile, "\tpushw %%bp\n" );
1242 fprintf( outfile, "\tpushl $" PREFIX "%s\n",odp->u.func.link_name);
1243 /* FreeBSD does not understand lcall, so do it the hard way */
1244 fprintf( outfile, "\t.byte 0x9a\n" );
1245 fprintf( outfile, "\t.long " PREFIX "CallFrom16_%s_%s_%s\n",
1246 (odp->type == TYPE_CDECL) ? "c" : "p",
1247 (odp->type == TYPE_REGISTER) ? "regs" :
1248 (odp->type == TYPE_PASCAL_16) ? "word" : "long",
1249 odp->u.func.arg_types );
1250 fprintf( outfile, "\t.long 0x%08lx\n",
1251 MAKELONG( Code_Selector, 0x9090 /* nop ; nop */ ) );
1252 odp->offset = code_offset;
1253 code_offset += 16; /* Assembly code is 16 bytes long */
1254 break;
1256 default:
1257 fprintf(stderr,"build: function type %d not available for Win16\n",
1258 odp->type);
1259 return -1;
1263 if (!code_offset) /* Make sure the code segment is not empty */
1265 fprintf( outfile, "\t.byte 0\n" );
1266 code_offset++;
1269 /* Output data segment */
1271 DumpBytes( outfile, data, data_offset, NULL, "Data_Start" );
1273 /* Build the module */
1275 module_size = BuildModule16( outfile, code_offset, data_offset );
1277 /* Output the DLL descriptor */
1279 fprintf( outfile, "\t.text\n" );
1280 fprintf( outfile, "DLLName:\t" STRING " \"%s\\0\"\n", DLLName );
1281 fprintf( outfile, "\t.align 4\n" );
1282 fprintf( outfile, "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1283 fprintf( outfile, PREFIX "%s_Descriptor:\n", DLLName );
1284 fprintf( outfile, "\t.long DLLName\n" ); /* Name */
1285 fprintf( outfile, "\t.long Module_Start\n" ); /* Module start */
1286 fprintf( outfile, "\t.long %d\n", module_size ); /* Module size */
1287 fprintf( outfile, "\t.long Code_Start\n" ); /* Code start */
1288 fprintf( outfile, "\t.long Data_Start\n" ); /* Data start */
1289 return 0;
1293 /*******************************************************************
1294 * BuildSpecFile
1296 * Build an assembly file from a spec file.
1298 static int BuildSpecFile( FILE *outfile, char *specname )
1300 SpecName = specname;
1301 SpecFp = fopen( specname, "r");
1302 if (SpecFp == NULL)
1304 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1305 return -1;
1308 if (ParseTopLevel() < 0) return -1;
1310 switch(SpecType)
1312 case SPEC_WIN16:
1313 return BuildSpec16File( specname, outfile );
1314 case SPEC_WIN32:
1315 return BuildSpec32File( specname, outfile );
1316 default:
1317 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1318 return -1;
1323 /*******************************************************************
1324 * TransferArgs16To32
1326 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1327 * The 16-bit stack layout is:
1328 * ... ...
1329 * (bp+8) arg2
1330 * (bp+6) arg1
1331 * (bp+4) cs
1332 * (bp+2) ip
1333 * (bp) bp
1335 * For 'cdecl' argn up to arg1 are reversed.
1337 static int TransferArgs16To32( FILE *outfile, char *args, int usecdecl )
1339 int i, pos16, pos32;
1340 char *xargs;
1342 /* Copy the arguments */
1344 pos16 = 6; /* skip bp and return address */
1345 pos32 = usecdecl ? -(strlen(args) * 4) : 0;
1346 xargs = usecdecl ? args:args+strlen(args);
1348 for (i = strlen(args); i > 0; i--)
1350 if (!usecdecl) {
1351 pos32 -= 4;
1352 xargs--;
1354 switch(*xargs)
1356 case 'w': /* word */
1357 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1358 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1359 pos16 += 2;
1360 break;
1362 case 's': /* s_word */
1363 fprintf( outfile, "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1364 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1365 pos16 += 2;
1366 break;
1368 case 'l': /* long or segmented pointer */
1369 case 'T': /* segmented pointer to null-terminated string */
1370 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", pos16 );
1371 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1372 pos16 += 4;
1373 break;
1375 case 'p': /* linear pointer */
1376 case 't': /* linear pointer to null-terminated string */
1377 /* Get the selector */
1378 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1379 /* Get the selector base */
1380 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1381 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1382 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1383 /* Add the offset */
1384 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1385 fprintf( outfile, "\taddl %%eax,%d(%%ebx)\n", pos32 );
1386 pos16 += 4;
1387 break;
1389 default:
1390 fprintf( stderr, "Unknown arg type '%c'\n", *xargs );
1392 if (usecdecl) {
1393 pos32 += 4;
1394 xargs++;
1398 return pos16 - 6; /* Return the size of the 16-bit args */
1402 /*******************************************************************
1403 * BuildContext16
1405 * Build the context structure on the 32-bit stack.
1407 static void BuildContext16( FILE *outfile )
1409 /* Store the registers */
1411 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1412 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1413 fprintf( outfile, "\tmovl %%ecx,%d(%%ebx)\n",
1414 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1415 fprintf( outfile, "\tmovl %%edx,%d(%%ebx)\n",
1416 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1417 fprintf( outfile, "\tmovl %%esi,%d(%%ebx)\n",
1418 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1419 fprintf( outfile, "\tmovl %%edi,%d(%%ebx)\n",
1420 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1422 fprintf( outfile, "\tmovl -24(%%ebp),%%eax\n" ); /* Get %ebx from stack*/
1423 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1424 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1425 fprintf( outfile, "\tmovzwl -10(%%ebp),%%eax\n" ); /* Get %ds from stack*/
1426 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1427 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1428 fprintf( outfile, "\tmovzwl -6(%%ebp),%%eax\n" ); /* Get %es from stack*/
1429 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1430 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1431 fprintf( outfile, "\tpushfl\n" );
1432 fprintf( outfile, "\tpopl %d(%%ebx)\n",
1433 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1434 fprintf( outfile, "\tmovl -20(%%ebp),%%eax\n" ); /* Get %ebp from stack */
1435 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1436 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1437 fprintf( outfile, "\tmovzwl 2(%%ebp),%%eax\n" ); /* Get %ip from stack */
1438 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1439 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1440 fprintf( outfile, "\tleal 2(%%ebp),%%eax\n" ); /* Get initial %sp */
1441 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1442 CONTEXTOFFSET(Esp) - sizeof(CONTEXT) );
1443 fprintf( outfile, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1444 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1445 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1446 fprintf( outfile, "\tmovzwl -14(%%ebp),%%eax\n" ); /* Get %fs from stack */
1447 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1448 CONTEXTOFFSET(SegFs) - sizeof(CONTEXT) );
1449 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1450 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1451 CONTEXTOFFSET(SegGs) - sizeof(CONTEXT) );
1452 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1453 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1454 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1455 #if 0
1456 fprintf( outfile, "\tfsave %d(%%ebx)\n",
1457 CONTEXTOFFSET(FloatSave) - sizeof(CONTEXT) );
1458 #endif
1462 /*******************************************************************
1463 * RestoreContext16
1465 * Restore the registers from the context structure.
1467 static void RestoreContext16( FILE *outfile )
1469 /* Get the 32-bit stack pointer */
1471 fprintf( outfile, "\tleal -%d(%%ebp),%%ebx\n",
1472 STRUCTOFFSET(STACK32FRAME,ebp) );
1474 /* Remove everything up to (including) the return address
1475 * from the 16-bit stack */
1477 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1478 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1479 fprintf( outfile, "\tmovw %%ax,%%ss\n" );
1480 fprintf( outfile, "\tmovl %d(%%ebx),%%esp\n",
1481 CONTEXTOFFSET(Esp) - sizeof(CONTEXT) );
1482 fprintf( outfile, "\taddl $4,%%esp\n" ); /* Remove return address */
1484 /* Restore the registers */
1486 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n",
1487 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1488 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n",
1489 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1490 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n",
1491 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1492 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n",
1493 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1494 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n",
1495 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1496 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new cs */
1497 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1498 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new ip */
1499 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1500 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new ds */
1501 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1502 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new es */
1503 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1504 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new fs */
1505 CONTEXTOFFSET(SegFs) - sizeof(CONTEXT) );
1506 fprintf( outfile, "\tpushl %d(%%ebx)\n",
1507 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1508 fprintf( outfile, "\tpopfl\n" );
1509 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1510 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1511 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n",
1512 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1513 fprintf( outfile, "\tpopl %%fs\n" ); /* Set fs */
1514 fprintf( outfile, "\tpopl %%es\n" ); /* Set es */
1515 fprintf( outfile, "\tpopl %%ds\n" ); /* Set ds */
1519 /*******************************************************************
1520 * BuildCallFrom16Func
1522 * Build a 16-bit-to-Wine callback function. The syntax of the function
1523 * profile is: call_type_xxxxx, where 'call' is the letter 'c' or 'p' for C or
1524 * Pascal calling convention, 'type' is one of 'regs', 'word' or
1525 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1526 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1527 * 'T'=segmented pointer to null-terminated string).
1528 * For register functions, the arguments are ignored, but they are still
1529 * removed from the stack upon return.
1531 * A special variant of the callback function is generated by the function
1532 * profile "t_long_". This is used by the Win95 16->32 thunk
1533 * functions C16ThkSL and C16ThkSL01 and is implemented as follows:
1534 * On entry, the EBX register is set up to contain a flat pointer to the
1535 * 16-bit stack such that EBX+22 points to the first argument.
1536 * Then, the entry point is called, while EBP is set up to point
1537 * to the return address (on the 32-bit stack).
1538 * The called function returns with CX set to the number of bytes
1539 * to be popped of the caller's stack.
1541 * Stack layout upon entry to the callback function:
1542 * ... ...
1543 * (sp+18) word first 16-bit arg
1544 * (sp+16) word cs
1545 * (sp+14) word ip
1546 * (sp+12) word bp
1547 * (sp+8) long 32-bit entry point (used to store edx)
1548 * (sp+6) word high word of cs (always 0, used to store es)
1549 * (sp+4) word low word of cs of 16-bit entry point
1550 * (sp+2) word high word of ip (always 0, used to store ds)
1551 * (sp) word low word of ip of 16-bit entry point
1553 * Added on the stack:
1554 * (sp-2) word saved fs
1555 * (sp-4) word buffer for Win16Mutex recursion count
1556 * (sp-8) long ebp
1557 * (sp-12) long saved previous stack
1559 static void BuildCallFrom16Func( FILE *outfile, char *profile )
1561 int argsize = 0;
1562 int short_ret = 0;
1563 int reg_func = 0;
1564 int cdecl = 0;
1565 int thunk = 0;
1566 char *args = profile + 7;
1568 /* Parse function type */
1570 if (!strncmp( "c_", profile, 2 )) cdecl = 1;
1571 else if (!strncmp( "t_", profile, 2 )) thunk = 1;
1572 else if (strncmp( "p_", profile, 2 ))
1574 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1575 return;
1578 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
1579 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
1580 else if (strncmp( "long_", profile + 2, 5 ))
1582 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1583 return;
1586 /* Function header */
1588 fprintf( outfile, "\n\t.align 4\n" );
1589 #ifdef USE_STABS
1590 fprintf( outfile, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX "CallFrom16_%s\n",
1591 profile, profile);
1592 #endif
1593 fprintf( outfile, "\t.globl " PREFIX "CallFrom16_%s\n", profile );
1594 fprintf( outfile, PREFIX "CallFrom16_%s:\n", profile );
1596 /* Save 16-bit fs and leave room for Win16Mutex recursion count */
1598 fprintf( outfile, "\t.byte 0x66\n\tpushl %%fs\n" );
1599 fprintf( outfile, "\tpushw $0\n" );
1601 /* Setup bp to point to its copy on the stack */
1603 fprintf( outfile, "\tpushl %%ebp\n" ); /* Save the full 32-bit ebp */
1604 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1605 fprintf( outfile, "\taddw $20,%%bp\n" );
1607 /* Save 16-bit ds and es */
1609 /* Stupid FreeBSD assembler doesn't know these either */
1610 /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1611 fprintf( outfile, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1612 /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1613 fprintf( outfile, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1615 /* Save %ebx */
1617 fprintf( outfile, "\tpushl %%ebx\n" );
1619 /* Restore 32-bit segment registers */
1621 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
1622 #ifdef __svr4__
1623 fprintf( outfile, "\tdata16\n");
1624 #endif
1625 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
1626 #ifdef __svr4__
1627 fprintf( outfile, "\tdata16\n");
1628 #endif
1629 fprintf( outfile, "\tmovw %%bx,%%es\n" );
1631 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
1633 /* Get the 32-bit stack pointer from the TEB */
1635 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET );
1637 /* Save the 16-bit stack */
1639 #ifdef __svr4__
1640 fprintf( outfile,"\tdata16\n");
1641 #endif
1642 fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss,(%d)\n", STACKOFFSET + 2 );
1643 fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp,(%d)\n", STACKOFFSET );
1645 /* Transfer the arguments */
1647 if (reg_func) BuildContext16( outfile );
1648 else if (*args) argsize = TransferArgs16To32( outfile, args, cdecl );
1649 else if (thunk)
1651 /* Get the stack selector base */
1652 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1653 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1654 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1655 fprintf( outfile, "\tmovl %%eax,-24(%%ebp)\n" );
1656 /* Add the offset */
1657 fprintf( outfile, "\tleal -16(%%ebp),%%eax\n" );
1658 fprintf( outfile, "\taddl %%eax,-24(%%ebp)\n" );
1661 /* Get the address of the API function */
1663 fprintf( outfile, "\tmovl -4(%%ebp),%%eax\n" );
1665 /* If necessary, save %edx over the API function address */
1667 if (!reg_func && short_ret)
1668 fprintf( outfile, "\tmovl %%edx,-4(%%ebp)\n" );
1670 /* Restore %ebx and store the 32-bit stack pointer instead */
1672 fprintf( outfile, "\tmovl %%ebx,%%ebp\n" );
1673 fprintf( outfile, "\tpopl %%ebx\n" );
1674 fprintf( outfile, "\tpushl %%ebp\n" );
1676 /* Switch to the 32-bit stack */
1678 fprintf( outfile, "\tpushl %%ds\n" );
1679 fprintf( outfile, "\tpopl %%ss\n" );
1680 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1681 reg_func ? sizeof(CONTEXT) : 4 * strlen(args) );
1682 if (reg_func) /* Push the address of the context struct */
1683 fprintf( outfile, "\tpushl %%esp\n" );
1685 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1687 fprintf( outfile, "\taddl $%d,%%ebp\n", STRUCTOFFSET(STACK32FRAME,ebp) );
1689 /* Print the debug information before the call */
1691 if (debugging && !thunk)
1693 int ftype = 0;
1695 if (cdecl) ftype |= 4;
1696 if (reg_func) ftype |= 2;
1697 if (short_ret) ftype |= 1;
1699 fprintf( outfile, "\tpushl %%eax\n" );
1700 fprintf( outfile, "\tpushl $Profile_%s\n", profile );
1701 fprintf( outfile, "\tpushl $%d\n", ftype );
1702 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n" );
1703 fprintf( outfile, "\tpopl %%eax\n" );
1704 fprintf( outfile, "\tpopl %%eax\n" );
1705 fprintf( outfile, "\tpopl %%eax\n" );
1708 /* Call the entry point */
1710 if (thunk)
1712 fprintf( outfile, "\tpushl %%ebp\n" );
1713 fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
1714 fprintf( outfile, "\tcall *%%eax\n" );
1715 fprintf( outfile, "\tpopl %%ebp\n" );
1717 else
1718 fprintf( outfile, "\tcall *%%eax\n" );
1721 /* Print the debug information after the call */
1723 if (debugging && !thunk)
1725 if (reg_func)
1727 /* Push again the address of the context struct in case */
1728 /* it has been removed by an stdcall function */
1729 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1730 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME,ebp) );
1731 fprintf( outfile, "\tpushl %%esp\n" );
1733 fprintf( outfile, "\tpushl %%eax\n" );
1734 fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1735 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n" );
1736 fprintf( outfile, "\tpopl %%eax\n" );
1737 fprintf( outfile, "\tpopl %%eax\n" );
1740 /* Restore the 16-bit stack */
1742 #ifdef __svr4__
1743 fprintf( outfile, "\tdata16\n");
1744 #endif
1745 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2 );
1746 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
1747 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
1749 if (reg_func)
1751 /* Calc the arguments size */
1752 while (*args)
1754 switch(*args)
1756 case 'w':
1757 case 's':
1758 argsize += 2;
1759 break;
1760 case 'p':
1761 case 't':
1762 case 'l':
1763 case 'T':
1764 argsize += 4;
1765 break;
1766 default:
1767 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1769 args++;
1772 /* Restore registers from the context structure */
1773 RestoreContext16( outfile );
1775 else
1777 /* Restore high 16 bits of ebp */
1778 fprintf( outfile, "\tpopl %%ebp\n" );
1780 /* Restore ds and es */
1781 fprintf( outfile, "\tincl %%esp\n" ); /* Remove mutex count */
1782 fprintf( outfile, "\tincl %%esp\n" );
1783 fprintf( outfile, "\tpopl %%edx\n" ); /* Remove ip and fs */
1784 fprintf( outfile, "\tmovw %%dx,%%fs\n" ); /* and restore fs */
1785 fprintf( outfile, "\tpopl %%edx\n" ); /* Remove cs and ds */
1786 fprintf( outfile, "\tmovw %%dx,%%ds\n" ); /* and restore ds */
1787 fprintf( outfile, "\t.byte 0x66\n\tpopl %%es\n" ); /* Restore es */
1789 if (short_ret) fprintf( outfile, "\tpopl %%edx\n" ); /* Restore edx */
1790 else
1792 /* Get the return value into dx:ax */
1793 fprintf( outfile, "\tmovl %%eax,%%edx\n" );
1794 fprintf( outfile, "\tshrl $16,%%edx\n" );
1795 /* Remove API entry point */
1796 fprintf( outfile, "\taddl $4,%%esp\n" );
1799 /* Restore low 16 bits of ebp */
1800 fprintf( outfile, "\tpopw %%bp\n" );
1803 /* Remove the arguments and return */
1805 if (thunk)
1807 fprintf( outfile, "\tpopl %%ebx\n" );
1808 fprintf( outfile, "\txorb %%ch,%%ch\n" );
1809 fprintf( outfile, "\taddw %%cx, %%sp\n" );
1810 fprintf( outfile, "\tpushl %%ebx\n" );
1811 fprintf( outfile, "\t.byte 0x66\n" );
1812 fprintf( outfile, "\tlret\n" );
1814 else if (argsize && !cdecl)
1816 fprintf( outfile, "\t.byte 0x66\n" );
1817 fprintf( outfile, "\tlret $%d\n", argsize );
1819 else
1821 fprintf( outfile, "\t.byte 0x66\n" );
1822 fprintf( outfile, "\tlret\n" );
1827 /*******************************************************************
1828 * BuildCallTo16Func
1830 * Build a Wine-to-16-bit callback function.
1832 * Stack frame of the callback function:
1833 * ... ...
1834 * (ebp+16) arg2
1835 * (ebp+12) arg1
1836 * (ebp+8) func to call
1837 * (ebp+4) return address
1838 * (ebp) previous ebp
1840 * Prototypes for the CallTo16 functions:
1841 * extern WINAPI WORD CallTo16_word_xxx( FARPROC16 func, args... );
1842 * extern WINAPI LONG CallTo16_long_xxx( FARPROC16 func, args... );
1843 * extern WINAPI void CallTo16_sreg_( const CONTEXT *context, int nb_args );
1844 * extern WINAPI void CallTo16_lreg_( const CONTEXT *context, int nb_args );
1846 static void BuildCallTo16Func( FILE *outfile, char *profile )
1848 int short_ret = 0;
1849 int reg_func = 0;
1850 char *args = profile + 5;
1852 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1853 else if (!strncmp( "sreg_", profile, 5 )) reg_func = 1;
1854 else if (!strncmp( "lreg_", profile, 5 )) reg_func = 2;
1855 else if (strncmp( "long_", profile, 5 ))
1857 fprintf( stderr, "Invalid function name '%s'.\n", profile );
1858 exit(1);
1861 /* Function header */
1863 fprintf( outfile, "\n\t.align 4\n" );
1864 #ifdef USE_STABS
1865 fprintf( outfile, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX "CallTo16_%s\n",
1866 profile, profile);
1867 #endif
1868 fprintf( outfile, "\t.globl " PREFIX "CallTo16_%s\n", profile );
1869 fprintf( outfile, PREFIX "CallTo16_%s:\n", profile );
1871 /* Entry code */
1873 fprintf( outfile, "\tpushl %%ebp\n" );
1874 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
1876 /* Save the 32-bit registers */
1878 fprintf( outfile, "\tpushl %%ebx\n" );
1879 fprintf( outfile, "\tpushl %%ecx\n" );
1880 fprintf( outfile, "\tpushl %%edx\n" );
1881 fprintf( outfile, "\tpushl %%esi\n" );
1882 fprintf( outfile, "\tpushl %%edi\n" );
1884 /* Enter Win16 Mutex */
1886 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock\n" );
1888 /* Print debugging info */
1890 if (debugging)
1892 /* Push the address of the first argument */
1893 fprintf( outfile, "\tleal 8(%%ebp),%%eax\n" );
1894 fprintf( outfile, "\tpushl $%d\n", reg_func ? -1 : strlen(args) );
1895 fprintf( outfile, "\tpushl %%eax\n" );
1896 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
1897 fprintf( outfile, "\tpopl %%eax\n" );
1898 fprintf( outfile, "\tpopl %%eax\n" );
1901 /* Call the actual CallTo16 routine (simulate a lcall) */
1903 fprintf( outfile, "\tpushl %%cs\n" );
1904 fprintf( outfile, "\tcall do_callto16_%s\n", profile );
1906 fprintf( outfile, "\tpushl %%eax\n" );
1908 /* Print debugging info */
1910 if (debugging)
1912 fprintf( outfile, "\tpushl %%eax\n" );
1913 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret\n" );
1914 fprintf( outfile, "\tpopl %%eax\n" );
1917 /* Leave Win16 Mutex */
1919 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock\n" );
1921 /* Restore the 32-bit registers */
1923 fprintf( outfile, "\tpopl %%eax\n" );
1924 fprintf( outfile, "\tpopl %%edi\n" );
1925 fprintf( outfile, "\tpopl %%esi\n" );
1926 fprintf( outfile, "\tpopl %%edx\n" );
1927 fprintf( outfile, "\tpopl %%ecx\n" );
1928 fprintf( outfile, "\tpopl %%ebx\n" );
1930 /* Exit code */
1932 #if 0
1933 /* FIXME: this is a hack because of task.c */
1934 if (!strcmp( profile, "word_" ))
1936 fprintf( outfile, ".globl " PREFIX "CALLTO16_Restore\n" );
1937 fprintf( outfile, PREFIX "CALLTO16_Restore:\n" );
1939 #endif
1940 fprintf( outfile, "\tpopl %%ebp\n" );
1941 fprintf( outfile, "\tret $%d\n", 4 * strlen(args) + 4 );
1944 /* Start of the actual CallTo16 routine */
1946 fprintf( outfile, "do_callto16_%s:\n", profile );
1948 /* Save the 32-bit stack */
1950 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
1951 fprintf( outfile, "\tmovl %%ebp,%%ebx\n" );
1952 fprintf( outfile, "\tmovl %%esp,%%edx\n" );
1954 if (reg_func)
1956 /* Switch to the 16-bit stack, saving the current %%esp, */
1957 /* and adding the specified offset to the new sp */
1958 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d),%%eax\n", STACKOFFSET );
1959 fprintf( outfile, "\tsubl 12(%%ebx),%%eax\n" ); /* Get the offset */
1960 #ifdef __svr4__
1961 fprintf( outfile,"\tdata16\n");
1962 #endif
1963 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
1964 fprintf( outfile, "\tmovl %%eax,%%esp\n" );
1965 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
1967 /* Get the registers. ebx is handled later on. */
1969 fprintf( outfile, "\tmovl 8(%%ebx),%%ebx\n" );
1970 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs) );
1971 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1972 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegFs) );
1973 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
1974 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp) );
1975 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax) );
1976 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx) );
1977 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx) );
1978 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi) );
1979 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi) );
1981 /* Push the return address
1982 * With sreg suffix, we push 16:16 address (normal lret)
1983 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
1985 if (reg_func == 1)
1986 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_long\n" );
1987 else
1989 fprintf( outfile, "\tpushw $0\n" );
1990 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_eax+2\n" );
1991 fprintf( outfile, "\tpushw $0\n" );
1992 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_eax\n" );
1995 /* Push the called routine address */
1997 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
1998 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
2000 /* Get the 16-bit ds */
2002 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
2003 /* Get ebx from the 32-bit stack */
2004 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(Ebx) );
2005 fprintf( outfile, "\tpopl %%ds\n" );
2007 else /* not a register function */
2009 int pos = 12; /* first argument position */
2011 /* Switch to the 16-bit stack */
2012 #ifdef __svr4__
2013 fprintf( outfile,"\tdata16\n");
2014 #endif
2015 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
2016 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
2017 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
2019 /* Make %bp point to the previous stackframe (built by CallFrom16) */
2020 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
2021 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n",
2022 STRUCTOFFSET(STACK16FRAME,bp) );
2024 /* Transfer the arguments */
2026 while (*args)
2028 switch(*args++)
2030 case 'w': /* word */
2031 fprintf( outfile, "\tpushw %d(%%ebx)\n", pos );
2032 break;
2033 case 'l': /* long */
2034 fprintf( outfile, "\tpushl %d(%%ebx)\n", pos );
2035 break;
2036 default:
2037 fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
2038 args[-1] );
2040 pos += 4;
2043 /* Push the return address */
2045 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_%s\n",
2046 short_ret ? "word" : "long" );
2048 /* Push the called routine address */
2050 fprintf( outfile, "\tpushl 8(%%ebx)\n" );
2052 /* Set %fs to the value saved by the last CallFrom16 */
2054 fprintf( outfile, "\tmovw -14(%%ebp),%%ax\n" );
2055 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2057 /* Set %ds and %es (and %ax just in case) equal to %ss */
2059 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
2060 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2061 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2064 /* Jump to the called routine */
2066 fprintf( outfile, "\t.byte 0x66\n" );
2067 fprintf( outfile, "\tlret\n" );
2071 /*******************************************************************
2072 * BuildRet16Func
2074 * Build the return code for 16-bit callbacks
2076 static void BuildRet16Func( FILE *outfile )
2078 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_word\n" );
2079 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_long\n" );
2080 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_eax\n" );
2082 fprintf( outfile, PREFIX "CALLTO16_Ret_word:\n" );
2083 fprintf( outfile, "\txorl %%edx,%%edx\n" );
2085 /* Put return value into %eax */
2087 fprintf( outfile, PREFIX "CALLTO16_Ret_long:\n" );
2088 fprintf( outfile, "\tshll $16,%%edx\n" );
2089 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
2090 fprintf( outfile, "\tmovl %%edx,%%eax\n" );
2091 fprintf( outfile, PREFIX "CALLTO16_Ret_eax:\n" );
2093 /* Restore 32-bit segment registers */
2095 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
2096 #ifdef __svr4__
2097 fprintf( outfile, "\tdata16\n");
2098 #endif
2099 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
2100 #ifdef __svr4__
2101 fprintf( outfile, "\tdata16\n");
2102 #endif
2103 fprintf( outfile, "\tmovw %%bx,%%es\n" );
2105 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
2107 /* Restore the 32-bit stack */
2109 #ifdef __svr4__
2110 fprintf( outfile, "\tdata16\n");
2111 #endif
2112 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
2113 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
2114 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2116 /* Return to caller */
2118 fprintf( outfile, "\tlret\n" );
2120 /* Declare the return address variables */
2122 fprintf( outfile, "\t.data\n" );
2123 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_word\n" );
2124 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_long\n" );
2125 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_eax\n" );
2126 fprintf( outfile, PREFIX "CALLTO16_RetAddr_word:\t.long 0\n" );
2127 fprintf( outfile, PREFIX "CALLTO16_RetAddr_long:\t.long 0\n" );
2128 fprintf( outfile, PREFIX "CALLTO16_RetAddr_eax:\t.long 0\n" );
2129 fprintf( outfile, "\t.text\n" );
2132 /*******************************************************************
2133 * BuildCallTo32CBClient
2135 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
2137 * Since the relay stub is itself 32-bit, this should not be a problem;
2138 * unfortunately, the relay stubs are expected to switch back to a
2139 * 16-bit stack (and 16-bit code) after completion :-(
2141 * This would conflict with our 16- vs. 32-bit stack handling, so
2142 * we simply switch *back* to our 32-bit stack before returning to
2143 * the caller ...
2145 * The CBClient relay stub expects to be called with:
2146 * - ebp pointing to the 16-bit stack at ss:bp
2147 * - ebx pointing to a buffer containing the saved 16-bit ss:sp
2149 * After completion, the stub will load ss:sp from the buffer at ebx
2150 * and perform a far return to 16-bit code.
2152 * To trick the relay stub into returning to us, we push a 16-bit
2153 * cs:ip pair pointing to out return entry point onto the 16-bit stack,
2154 * followed by a ss:sp pair pointing to *that* cs:ip pair.
2155 * Our return stub thus called will then reload the 32-bit ss:esp and
2156 * return to 32-bit code (by using and ss:esp value that we have also
2157 * pushed onto the 16-bit stack before and a cs:eip values found at
2158 * that position on the 32-bit stack). The layout of our
2159 * temporary area used on the 16-bit stack is thus as follows:
2161 * (ebx+12) 32-bit ss (flat)
2162 * (ebx+8) 32-bit sp (32-bit stack pointer)
2163 * (ebx+6) 16-bit cs (this segment)
2164 * (ebx+4) 16-bit ip ('16-bit' return entry point)
2165 * (ebx+2) 16-bit ss (16-bit stack segment)
2166 * (ebx+0) 16-bit sp (points to ebx+4)
2168 * The stack layout of this function:
2169 * (ebp+12) arg ebp value to be set for relay stub
2170 * (ebp+8) func CBClient relay stub address
2171 * (ebp+4) ret addr
2172 * (ebp) ebp
2174 static void BuildCallTo32CBClient( FILE *outfile )
2176 /* Function header */
2178 fprintf( outfile, "\n\t.align 4\n" );
2179 #ifdef USE_STABS
2180 fprintf( outfile, ".stabs \"CALL32_CBClient:F1\",36,0,0," PREFIX "CALL32_CBClient\n" );
2181 #endif
2182 fprintf( outfile, "\t.globl " PREFIX "CALL32_CBClient\n" );
2183 fprintf( outfile, PREFIX "CALL32_CBClient:\n" );
2185 /* Entry code */
2187 fprintf( outfile, "\tpushl %%ebp\n" );
2188 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2189 fprintf( outfile, "\tpushl %%edi\n" );
2190 fprintf( outfile, "\tpushl %%esi\n" );
2191 fprintf( outfile, "\tpushl %%ebx\n" );
2193 /* Get the 16-bit stack */
2195 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET);
2197 /* Convert it to a flat address */
2199 fprintf( outfile, "\tshldl $16,%%ebx,%%eax\n" );
2200 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
2201 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%esi\n" );
2202 fprintf( outfile, "\tmovw %%bx,%%ax\n" );
2203 fprintf( outfile, "\taddl %%eax,%%esi\n" );
2205 /* Allocate temporary area (simulate STACK16_PUSH) */
2207 fprintf( outfile, "\tpushf\n" );
2208 fprintf( outfile, "\tcld\n" );
2209 fprintf( outfile, "\tleal -16(%%esi), %%edi\n" );
2210 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2211 fprintf( outfile, "\trep\n\tmovsb\n" );
2212 fprintf( outfile, "\tpopf\n" );
2214 fprintf( outfile, "\t.byte 0x64\n\tsubw $16,(%d)\n", STACKOFFSET );
2216 fprintf( outfile, "\tpushl %%edi\n" ); /* remember address */
2218 /* Set up temporary area */
2220 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-16+4 );
2221 fprintf( outfile, "\tmovl %%ebx, (%%edi)\n" );
2223 fprintf( outfile, "\tmovl " PREFIX "CALL32_CBClient_RetAddr, %%eax\n" );
2224 fprintf( outfile, "\tmovl %%eax, 4(%%edi)\n" );
2226 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2227 fprintf( outfile, "\tmovl %%eax, 8(%%edi)\n" );
2229 fprintf( outfile, "\tmovl %%ss, %%ax\n" );
2230 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2231 fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
2233 /* Setup registers and call CBClient relay stub (simulating a far call) */
2235 fprintf( outfile, "\tmovl %%edi, %%ebx\n" );
2236 fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
2237 fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
2239 fprintf( outfile, "\tpushl %%cs\n" );
2240 fprintf( outfile, "\tcall *%%eax\n" );
2242 /* Cleanup temporary area (simulate STACK16_POP) */
2244 fprintf( outfile, "\tpop %%esi\n" );
2246 fprintf( outfile, "\tpushf\n" );
2247 fprintf( outfile, "\tstd\n" );
2248 fprintf( outfile, "\tdec %%esi\n" );
2249 fprintf( outfile, "\tleal 16(%%esi), %%edi\n" );
2250 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2251 fprintf( outfile, "\trep\n\tmovsb\n" );
2252 fprintf( outfile, "\tpopf\n" );
2254 fprintf( outfile, "\t.byte 0x64\n\taddw $16,(%d)\n", STACKOFFSET );
2256 /* Restore registers and return */
2258 fprintf( outfile, "\tpopl %%ebx\n" );
2259 fprintf( outfile, "\tpopl %%esi\n" );
2260 fprintf( outfile, "\tpopl %%edi\n" );
2261 fprintf( outfile, "\tpopl %%ebp\n" );
2262 fprintf( outfile, "\tret\n" );
2264 /* '16-bit' return stub */
2266 fprintf( outfile, "\t.globl " PREFIX "CALL32_CBClient_Ret\n" );
2267 fprintf( outfile, PREFIX "CALL32_CBClient_Ret:\n" );
2269 fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
2270 fprintf( outfile, "\tlssl %%ss:(%%ebx), %%esp\n" );
2271 fprintf( outfile, "\tlret\n" );
2273 /* Declare the return address variable */
2275 fprintf( outfile, "\t.data\n" );
2276 fprintf( outfile, "\t.globl " PREFIX "CALL32_CBClient_RetAddr\n" );
2277 fprintf( outfile, PREFIX "CALL32_CBClient_RetAddr:\t.long 0\n" );
2278 fprintf( outfile, "\t.text\n" );
2283 /*******************************************************************
2284 * BuildCallTo32LargeStack
2286 * Build the function used to switch to the original 32-bit stack
2287 * before calling a 32-bit function from 32-bit code. This is used for
2288 * functions that need a large stack, like X bitmaps functions.
2290 * The generated function has the following prototype:
2291 * int xxx( int (*func)(), void *arg );
2293 * The pointer to the function can be retrieved by calling CALL32_Init,
2294 * which also takes care of saving the current 32-bit stack pointer.
2296 * NOTE: The CALL32_LargeStack routine may be recursively entered by the
2297 * same thread, but not concurrently entered by several threads.
2299 * Stack layout:
2300 * ... ...
2301 * (ebp+12) arg
2302 * (ebp+8) func
2303 * (ebp+4) ret addr
2304 * (ebp) ebp
2306 static void BuildCallTo32LargeStack( FILE *outfile )
2308 /* Initialization function */
2310 fprintf( outfile, "\n\t.align 4\n" );
2311 #ifdef USE_STABS
2312 fprintf( outfile, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX "CALL32_Init\n");
2313 #endif
2314 fprintf( outfile, "\t.globl " PREFIX "CALL32_Init\n" );
2315 fprintf( outfile, "\t.type " PREFIX "CALL32_Init,@function\n" );
2316 fprintf( outfile, PREFIX "CALL32_Init:\n" );
2317 fprintf( outfile, "\tleal -256(%%esp),%%eax\n" );
2318 fprintf( outfile, "\tmovl %%eax,CALL32_Original32_esp\n" );
2319 fprintf( outfile, "\tmovl $CALL32_LargeStack,%%eax\n" );
2320 fprintf( outfile, "\tret\n" );
2322 /* Function header */
2324 fprintf( outfile, "\n\t.align 4\n" );
2325 #ifdef USE_STABS
2326 fprintf( outfile, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2327 #endif
2328 fprintf( outfile, "CALL32_LargeStack:\n" );
2330 /* Entry code */
2332 fprintf( outfile, "\tpushl %%ebp\n" );
2333 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2335 /* Switch to the original 32-bit stack pointer */
2337 fprintf( outfile, "\tcmpl $0, CALL32_RecursionCount\n" );
2338 fprintf( outfile, "\tjne CALL32_skip\n" );
2339 fprintf( outfile, "\tmovl CALL32_Original32_esp, %%esp\n" );
2340 fprintf( outfile, "CALL32_skip:\n" );
2342 fprintf( outfile, "\tincl CALL32_RecursionCount\n" );
2344 /* Transfer the argument and call the function */
2346 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2347 fprintf( outfile, "\tcall *8(%%ebp)\n" );
2349 /* Restore registers and return */
2351 fprintf( outfile, "\tdecl CALL32_RecursionCount\n" );
2353 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2354 fprintf( outfile, "\tpopl %%ebp\n" );
2355 fprintf( outfile, "\tret\n" );
2357 /* Data */
2359 fprintf( outfile, "\t.data\n" );
2360 fprintf( outfile, "CALL32_Original32_esp:\t.long 0\n" );
2361 fprintf( outfile, "CALL32_RecursionCount:\t.long 0\n" );
2362 fprintf( outfile, "\t.text\n" );
2366 /*******************************************************************
2367 * BuildCallFrom32Regs
2369 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2370 * 'args' is the number of dword arguments.
2372 * Stack layout:
2373 * ... ...
2374 * (esp+336) ret addr (or relay addr when debugging(relay) is on)
2375 * (esp+332) entry point
2376 * (esp+204) buffer area to allow stack frame manipulation
2377 * (esp+0) CONTEXT struct
2379 static void BuildCallFrom32Regs( FILE *outfile )
2381 #define STACK_SPACE 128
2383 /* Function header */
2385 fprintf( outfile, "\n\t.align 4\n" );
2386 #ifdef USE_STABS
2387 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
2388 #endif
2389 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
2390 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
2392 /* Allocate some buffer space on the stack */
2394 fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
2396 /* Build the context structure */
2398 fprintf( outfile, "\tpushw $0\n" );
2399 fprintf( outfile, "\t.byte 0x66\n\tpushl %%ss\n" );
2400 fprintf( outfile, "\tpushl %%eax\n" ); /* %esp place holder */
2401 fprintf( outfile, "\tpushfl\n" );
2402 fprintf( outfile, "\tpushw $0\n" );
2403 fprintf( outfile, "\t.byte 0x66\n\tpushl %%cs\n" );
2404 fprintf( outfile, "\tpushl %d(%%esp)\n", 16+STACK_SPACE+4 ); /* %eip at time of call */
2405 fprintf( outfile, "\tpushl %%ebp\n" );
2407 fprintf( outfile, "\tpushl %%eax\n" );
2408 fprintf( outfile, "\tpushl %%ecx\n" );
2409 fprintf( outfile, "\tpushl %%edx\n" );
2410 fprintf( outfile, "\tpushl %%ebx\n" );
2411 fprintf( outfile, "\tpushl %%esi\n" );
2412 fprintf( outfile, "\tpushl %%edi\n" );
2414 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2415 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2416 fprintf( outfile, "\tpushl %%eax\n" );
2417 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2418 fprintf( outfile, "\tpushl %%eax\n" );
2419 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2420 fprintf( outfile, "\tpushl %%eax\n" );
2421 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2422 fprintf( outfile, "\tpushl %%eax\n" );
2424 fprintf( outfile, "\tleal -%d(%%esp),%%esp\n",
2425 sizeof(FLOATING_SAVE_AREA) + 6 * sizeof(DWORD) /* DR regs */ );
2426 fprintf( outfile, "\tpushl $0x0001001f\n" ); /* ContextFlags */
2428 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2430 fprintf( outfile, "\tleal %d(%%esp),%%eax\n",
2431 sizeof(CONTEXT) + STACK_SPACE + 4 ); /* %esp at time of call */
2432 fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Esp) );
2434 fprintf( outfile, "\tcall " PREFIX "RELAY_CallFrom32Regs\n" );
2436 /* Restore the context structure */
2438 fprintf( outfile, "\tfrstor %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2440 /* Store %eip value onto the new stack */
2442 fprintf( outfile, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Eip) );
2443 fprintf( outfile, "\tmovl %d(%%esp),%%ebx\n", CONTEXTOFFSET(Esp) );
2444 fprintf( outfile, "\tmovl %%eax,0(%%ebx)\n" );
2446 /* Restore all registers */
2448 fprintf( outfile, "\tleal %d(%%esp),%%esp\n",
2449 sizeof(FLOATING_SAVE_AREA) + 7 * sizeof(DWORD) );
2450 fprintf( outfile, "\tpopl %%eax\n" );
2451 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2452 fprintf( outfile, "\tpopl %%eax\n" );
2453 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2454 fprintf( outfile, "\tpopl %%eax\n" );
2455 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2456 fprintf( outfile, "\tpopl %%eax\n" );
2457 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2459 fprintf( outfile, "\tpopl %%edi\n" );
2460 fprintf( outfile, "\tpopl %%esi\n" );
2461 fprintf( outfile, "\tpopl %%ebx\n" );
2462 fprintf( outfile, "\tpopl %%edx\n" );
2463 fprintf( outfile, "\tpopl %%ecx\n" );
2464 fprintf( outfile, "\tpopl %%eax\n" );
2465 fprintf( outfile, "\tpopl %%ebp\n" );
2466 fprintf( outfile, "\tleal 8(%%esp),%%esp\n" ); /* skip %eip and %cs */
2467 fprintf( outfile, "\tpopfl\n" );
2468 fprintf( outfile, "\tpopl %%esp\n" );
2469 fprintf( outfile, "\tret\n" );
2471 #undef STACK_SPACE
2475 /*******************************************************************
2476 * BuildSpec
2478 * Build the spec files
2480 static int BuildSpec( FILE *outfile, int argc, char *argv[] )
2482 int i;
2483 for (i = 2; i < argc; i++)
2484 if (BuildSpecFile( outfile, argv[i] ) < 0) return -1;
2485 return 0;
2489 /*******************************************************************
2490 * BuildCallFrom16
2492 * Build the 16-bit-to-Wine callbacks
2494 static int BuildCallFrom16( FILE *outfile, char * outname, int argc, char *argv[] )
2496 int i;
2497 char buffer[1024];
2499 /* File header */
2501 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2502 fprintf( outfile, "\t.text\n" );
2504 #ifdef USE_STABS
2505 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2506 getcwd(buffer, sizeof(buffer));
2509 * The stabs help the internal debugger as they are an indication that it
2510 * is sensible to step into a thunk/trampoline.
2512 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2513 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2514 fprintf( outfile, "\t.text\n" );
2515 fprintf( outfile, "\t.align 4\n" );
2516 fprintf( outfile, "Code_Start:\n\n" );
2517 #endif
2518 fprintf( outfile, PREFIX"CallFrom16_Start:\n" );
2519 fprintf( outfile, "\t.globl "PREFIX"CallFrom16_Start\n" );
2521 /* Build the callback functions */
2523 for (i = 2; i < argc; i++) BuildCallFrom16Func( outfile, argv[i] );
2525 /* Build the thunk callback function */
2527 BuildCallFrom16Func( outfile, "t_long_" );
2529 /* Output the argument debugging strings */
2531 if (debugging)
2533 fprintf( outfile, "/* Argument strings */\n" );
2534 for (i = 2; i < argc; i++)
2536 fprintf( outfile, "Profile_%s:\t", argv[i] );
2537 fprintf( outfile, STRING " \"%s\\0\"\n", argv[i] + 7 );
2540 fprintf( outfile, PREFIX"CallFrom16_End:\n" );
2541 fprintf( outfile, "\t.globl "PREFIX"CallFrom16_End\n" );
2543 #ifdef USE_STABS
2544 fprintf( outfile, "\t.text\n");
2545 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2546 fprintf( outfile, ".Letext:\n");
2547 #endif
2549 return 0;
2553 /*******************************************************************
2554 * BuildCallTo16
2556 * Build the Wine-to-16-bit callbacks
2558 static int BuildCallTo16( FILE *outfile, char * outname, int argc, char *argv[] )
2560 char buffer[1024];
2561 FILE *infile;
2563 if (argc > 2)
2565 infile = fopen( argv[2], "r" );
2566 if (!infile)
2568 perror( argv[2] );
2569 exit( 1 );
2572 else infile = stdin;
2574 /* File header */
2576 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2577 fprintf( outfile, "\t.text\n" );
2579 #ifdef USE_STABS
2580 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2581 getcwd(buffer, sizeof(buffer));
2584 * The stabs help the internal debugger as they are an indication that it
2585 * is sensible to step into a thunk/trampoline.
2587 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2588 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2589 fprintf( outfile, "\t.text\n" );
2590 fprintf( outfile, "\t.align 4\n" );
2591 fprintf( outfile, "Code_Start:\n\n" );
2592 #endif
2594 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Start\n" );
2595 fprintf( outfile, PREFIX "CALLTO16_Start:\n" );
2597 /* Build the callback functions */
2599 while (fgets( buffer, sizeof(buffer), infile ))
2601 if (strstr( buffer, "### start build ###" )) break;
2603 while (fgets( buffer, sizeof(buffer), infile ))
2605 char *p = strstr( buffer, "CallTo16_" );
2606 if (p)
2608 char *profile = p + strlen( "CallTo16_" );
2609 p = profile;
2610 while ((*p == '_') || isalpha(*p)) p++;
2611 *p = '\0';
2612 BuildCallTo16Func( outfile, profile );
2614 if (strstr( buffer, "### stop build ###" )) break;
2617 /* Output the 16-bit return code */
2619 BuildRet16Func( outfile );
2621 /* Output the CBClient callback function
2622 * (while this does not really 'call to 16-bit' code, it is placed
2623 * here so that its 16-bit return stub is defined within the CALLTO16
2624 * 16-bit segment)
2626 BuildCallTo32CBClient( outfile );
2629 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_End\n" );
2630 fprintf( outfile, PREFIX "CALLTO16_End:\n" );
2632 #ifdef USE_STABS
2633 fprintf( outfile, "\t.text\n");
2634 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2635 fprintf( outfile, ".Letext:\n");
2636 #endif
2638 fclose( infile );
2639 return 0;
2643 /*******************************************************************
2644 * BuildCall32
2646 * Build the 32-bit callbacks
2648 static int BuildCall32( FILE *outfile, char * outname )
2650 char buffer[1024];
2652 /* File header */
2654 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2655 fprintf( outfile, "\t.text\n" );
2657 #ifdef __i386__
2659 #ifdef USE_STABS
2660 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2661 getcwd(buffer, sizeof(buffer));
2664 * The stabs help the internal debugger as they are an indication that it
2665 * is sensible to step into a thunk/trampoline.
2667 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2668 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2669 fprintf( outfile, "\t.text\n" );
2670 fprintf( outfile, "\t.align 4\n" );
2671 fprintf( outfile, "Code_Start:\n" );
2672 #endif
2674 /* Build the 32-bit large stack callback */
2676 BuildCallTo32LargeStack( outfile );
2678 /* Build the register callback function */
2680 BuildCallFrom32Regs( outfile );
2682 #ifdef USE_STABS
2683 fprintf( outfile, "\t.text\n");
2684 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2685 fprintf( outfile, ".Letext:\n");
2686 #endif
2688 #else /* __i386__ */
2690 /* Just to avoid an empty file */
2691 fprintf( outfile, "\t.long 0\n" );
2693 #endif /* __i386__ */
2694 return 0;
2698 /*******************************************************************
2699 * usage
2701 static void usage(void)
2703 fprintf( stderr,
2704 "usage: build [-o outfile] -spec SPECNAMES\n"
2705 " build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2706 " build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2707 " build [-o outfile] -call32\n" );
2708 exit(1);
2712 /*******************************************************************
2713 * main
2715 int main(int argc, char **argv)
2717 char *outname = NULL;
2718 FILE *outfile = stdout;
2719 int res = -1;
2721 if (argc < 2) usage();
2723 if (!strcmp( argv[1], "-o" ))
2725 outname = argv[2];
2726 argv += 2;
2727 argc -= 2;
2728 if (argc < 2) usage();
2729 if (!(outfile = fopen( outname, "w" )))
2731 fprintf( stderr, "Unable to create output file '%s'\n", outname );
2732 exit(1);
2736 /* Retrieve the selector values; this assumes that we are building
2737 * the asm files on the platform that will also run them. Probably
2738 * a safe assumption to make.
2740 GET_CS( Code_Selector );
2741 GET_DS( Data_Selector );
2743 if (!strcmp( argv[1], "-spec" ))
2744 res = BuildSpec( outfile, argc, argv );
2745 else if (!strcmp( argv[1], "-callfrom16" ))
2746 res = BuildCallFrom16( outfile, outname, argc, argv );
2747 else if (!strcmp( argv[1], "-callto16" ))
2748 res = BuildCallTo16( outfile, outname, argc, argv );
2749 else if (!strcmp( argv[1], "-call32" ))
2750 res = BuildCall32( outfile, outname );
2751 else
2753 fclose( outfile );
2754 unlink( outname );
2755 usage();
2758 fclose( outfile );
2759 if (res < 0)
2761 unlink( outname );
2762 return 1;
2764 return 0;