Added missing #include "config.h"
[wine/multimedia.git] / tools / build.c
blob24edf164774156493bb55f7d5cb231fd8a6b492f
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);
711 else
712 fprintf(stderr, "InitFunc is %s\n", DLLInitFunc);
714 else if (IsNumberString(token))
716 int ordinal;
717 int rv;
719 ordinal = atoi(token);
720 if ((rv = ParseOrdinal(ordinal)) < 0)
721 return rv;
723 else
725 fprintf(stderr,
726 "%s:%d: Expected name, id, length or ordinal\n",
727 SpecName, Line);
728 return -1;
732 return 0;
736 /*******************************************************************
737 * StoreVariableCode
739 * Store a list of ints into a byte array.
741 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
743 int i;
745 switch(size)
747 case 1:
748 for (i = 0; i < odp->u.var.n_values; i++)
749 buffer[i] = odp->u.var.values[i];
750 break;
751 case 2:
752 for (i = 0; i < odp->u.var.n_values; i++)
753 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
754 break;
755 case 4:
756 for (i = 0; i < odp->u.var.n_values; i++)
757 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
758 break;
760 return odp->u.var.n_values * size;
764 /*******************************************************************
765 * DumpBytes
767 * Dump a byte stream into the assembly code.
769 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
770 const char *section, const char *label_start )
772 int i;
773 if (section) fprintf( outfile, "\t%s\n", section );
774 if (label_start) fprintf( outfile, "%s:\n", label_start );
775 for (i = 0; i < len; i++)
777 if (!(i & 0x0f)) fprintf( outfile, "\t.byte " );
778 fprintf( outfile, "%d", *data++ );
779 if (i < len - 1)
780 fprintf( outfile, "%c", ((i & 0x0f) != 0x0f) ? ',' : '\n' );
782 fprintf( outfile, "\n" );
786 /*******************************************************************
787 * BuildModule16
789 * Build the in-memory representation of a 16-bit NE module, and dump it
790 * as a byte stream into the assembly code.
792 static int BuildModule16( FILE *outfile, int max_code_offset,
793 int max_data_offset )
795 ORDDEF *odp;
796 int i;
797 char *buffer;
798 NE_MODULE *pModule;
799 SEGTABLEENTRY *pSegment;
800 OFSTRUCT *pFileInfo;
801 BYTE *pstr, *bundle;
802 WORD *pword;
804 /* Module layout:
805 * NE_MODULE Module
806 * OFSTRUCT File information
807 * SEGTABLEENTRY Segment 1 (code)
808 * SEGTABLEENTRY Segment 2 (data)
809 * WORD[2] Resource table (empty)
810 * BYTE[2] Imported names (empty)
811 * BYTE[n] Resident names table
812 * BYTE[n] Entry table
815 buffer = xmalloc( 0x10000 );
817 pModule = (NE_MODULE *)buffer;
818 pModule->magic = IMAGE_OS2_SIGNATURE;
819 pModule->count = 1;
820 pModule->next = 0;
821 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
822 pModule->dgroup = 2;
823 pModule->heap_size = DLLHeapSize;
824 pModule->stack_size = 0;
825 pModule->ip = 0;
826 pModule->cs = 0;
827 pModule->sp = 0;
828 pModule->ss = 0;
829 pModule->seg_count = 2;
830 pModule->modref_count = 0;
831 pModule->nrname_size = 0;
832 pModule->modref_table = 0;
833 pModule->nrname_fpos = 0;
834 pModule->moveable_entries = 0;
835 pModule->alignment = 0;
836 pModule->truetype = 0;
837 pModule->os_flags = NE_OSFLAGS_WINDOWS;
838 pModule->misc_flags = 0;
839 pModule->dlls_to_init = 0;
840 pModule->nrname_handle = 0;
841 pModule->min_swap_area = 0;
842 pModule->expected_version = 0x030a;
843 pModule->module32 = 0;
844 pModule->self = 0;
845 pModule->self_loading_sel = 0;
847 /* File information */
849 pFileInfo = (OFSTRUCT *)(pModule + 1);
850 pModule->fileinfo = (int)pFileInfo - (int)pModule;
851 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
852 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
853 + strlen(DLLFileName);
854 strcpy( pFileInfo->szPathName, DLLFileName );
855 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
857 /* Segment table */
859 pSegment = (SEGTABLEENTRY *)pstr;
860 pModule->seg_table = (int)pSegment - (int)pModule;
861 pSegment->filepos = 0;
862 pSegment->size = max_code_offset;
863 pSegment->flags = 0;
864 pSegment->minsize = max_code_offset;
865 pSegment->hSeg = 0;
866 pSegment++;
868 pModule->dgroup_entry = (int)pSegment - (int)pModule;
869 pSegment->filepos = 0;
870 pSegment->size = max_data_offset;
871 pSegment->flags = NE_SEGFLAGS_DATA;
872 pSegment->minsize = max_data_offset;
873 pSegment->hSeg = 0;
874 pSegment++;
876 /* Resource table */
878 pword = (WORD *)pSegment;
879 pModule->res_table = (int)pword - (int)pModule;
880 *pword++ = 0;
881 *pword++ = 0;
883 /* Imported names table */
885 pstr = (char *)pword;
886 pModule->import_table = (int)pstr - (int)pModule;
887 *pstr++ = 0;
888 *pstr++ = 0;
890 /* Resident names table */
892 pModule->name_table = (int)pstr - (int)pModule;
893 /* First entry is module name */
894 *pstr = strlen(DLLName );
895 strcpy( pstr + 1, DLLName );
896 pstr += *pstr + 1;
897 *(WORD *)pstr = 0;
898 pstr += sizeof(WORD);
899 /* Store all ordinals */
900 odp = OrdinalDefinitions + 1;
901 for (i = 1; i <= Limit; i++, odp++)
903 if (!odp->name[0]) continue;
904 *pstr = strlen( odp->name );
905 strcpy( pstr + 1, odp->name );
906 strupper( pstr + 1 );
907 pstr += *pstr + 1;
908 *(WORD *)pstr = i;
909 pstr += sizeof(WORD);
911 *pstr++ = 0;
913 /* Entry table */
915 pModule->entry_table = (int)pstr - (int)pModule;
916 bundle = NULL;
917 odp = OrdinalDefinitions + 1;
918 for (i = 1; i <= Limit; i++, odp++)
920 int selector = 0;
922 switch (odp->type)
924 case TYPE_CDECL:
925 case TYPE_PASCAL:
926 case TYPE_PASCAL_16:
927 case TYPE_REGISTER:
928 case TYPE_RETURN:
929 case TYPE_STUB:
930 selector = 1; /* Code selector */
931 break;
933 case TYPE_BYTE:
934 case TYPE_WORD:
935 case TYPE_LONG:
936 selector = 2; /* Data selector */
937 break;
939 case TYPE_ABS:
940 selector = 0xfe; /* Constant selector */
941 break;
943 default:
944 selector = 0; /* Invalid selector */
945 break;
948 /* create a new bundle if necessary */
949 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
951 bundle = pstr;
952 bundle[0] = 0;
953 bundle[1] = selector;
954 pstr += 2;
957 (*bundle)++;
958 if (selector != 0)
960 *pstr++ = 1;
961 *(WORD *)pstr = odp->offset;
962 pstr += sizeof(WORD);
965 *pstr++ = 0;
967 /* Dump the module content */
969 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
970 ".data", "Module_Start" );
971 return (int)pstr - (int)pModule;
975 /*******************************************************************
976 * BuildSpec32File
978 * Build a Win32 C file from a spec file.
980 static int BuildSpec32File( char * specfile, FILE *outfile )
982 ORDDEF *odp;
983 int i, nb_names;
985 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
986 specfile );
987 fprintf( outfile, "#include \"builtin32.h\"\n\n" );
989 /* Output code for all stubs functions */
991 fprintf( outfile, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
992 DLLName );
993 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
995 if (odp->type != TYPE_STUB) continue;
996 fprintf( outfile, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
997 i, DLLName, i );
1000 /* Output code for all register functions */
1002 fprintf( outfile, "#ifdef __i386__\n" );
1003 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1005 if (odp->type != TYPE_REGISTER) continue;
1006 fprintf( outfile,
1007 "__asm__(\".align 4\\n\\t\"\n"
1008 " \".globl " PREFIX "%s\\n\\t\"\n"
1009 " \".type " PREFIX "%s,@function\\n\\t\"\n"
1010 " \"" PREFIX "%s:\\n\\t\"\n"
1011 " \"pushl $" PREFIX "__regs_%s\\n\\t\"\n"
1012 " \"pushl $" PREFIX "CALL32_Regs\\n\\t\"\n"
1013 " \"ret\");\n",
1014 odp->u.func.link_name, odp->u.func.link_name,
1015 odp->u.func.link_name, odp->u.func.link_name );
1017 fprintf( outfile, "#endif\n" );
1019 /* Output the DLL functions prototypes */
1021 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1023 switch(odp->type)
1025 case TYPE_VARARGS:
1026 fprintf( outfile, "extern void %s();\n", odp->u.vargs.link_name );
1027 break;
1028 case TYPE_EXTERN:
1029 fprintf( outfile, "extern void %s();\n", odp->u.ext.link_name );
1030 break;
1031 case TYPE_REGISTER:
1032 case TYPE_STDCALL:
1033 case TYPE_CDECL:
1034 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1035 break;
1036 case TYPE_INVALID:
1037 case TYPE_STUB:
1038 break;
1039 default:
1040 fprintf(stderr,"build: function type %d not available for Win32\n",
1041 odp->type);
1042 return -1;
1046 /* Output LibMain function */
1047 if (DLLInitFunc[0]) fprintf( outfile, "extern void %s();\n", DLLInitFunc );
1050 /* Output the DLL functions table */
1052 fprintf( outfile, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1053 Limit - Base + 1 );
1054 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1056 switch(odp->type)
1058 case TYPE_INVALID:
1059 fprintf( outfile, " 0" );
1060 break;
1061 case TYPE_VARARGS:
1062 fprintf( outfile, " %s", odp->u.vargs.link_name );
1063 break;
1064 case TYPE_EXTERN:
1065 fprintf( outfile, " %s", odp->u.ext.link_name );
1066 break;
1067 case TYPE_REGISTER:
1068 case TYPE_STDCALL:
1069 case TYPE_CDECL:
1070 fprintf( outfile, " %s", odp->u.func.link_name);
1071 break;
1072 case TYPE_STUB:
1073 fprintf( outfile, " __stub_%d", i );
1074 break;
1075 default:
1076 return -1;
1078 if (i < Limit) fprintf( outfile, ",\n" );
1080 fprintf( outfile, "\n};\n\n" );
1082 /* Output the DLL names table */
1084 nb_names = 0;
1085 fprintf( outfile, "static const char * const FuncNames[] =\n{\n" );
1086 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1088 if (odp->type == TYPE_INVALID) continue;
1089 if (nb_names++) fprintf( outfile, ",\n" );
1090 fprintf( outfile, " \"%s\"", odp->name );
1092 fprintf( outfile, "\n};\n\n" );
1094 /* Output the DLL argument types */
1096 fprintf( outfile, "static const unsigned int ArgTypes[%d] =\n{\n",
1097 Limit - Base + 1 );
1098 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1100 unsigned int j, mask = 0;
1101 if ((odp->type == TYPE_STDCALL) || (odp->type == TYPE_CDECL))
1102 for (j = 0; odp->u.func.arg_types[j]; j++)
1104 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
1105 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
1107 fprintf( outfile, " %d", mask );
1108 if (i < Limit) fprintf( outfile, ",\n" );
1110 fprintf( outfile, "\n};\n\n" );
1112 /* Output the DLL ordinals table */
1114 fprintf( outfile, "static const unsigned short FuncOrdinals[] =\n{\n" );
1115 nb_names = 0;
1116 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1118 if (odp->type == TYPE_INVALID) continue;
1119 if (nb_names++) fprintf( outfile, ",\n" );
1120 fprintf( outfile, " %d", i - Base );
1122 fprintf( outfile, "\n};\n\n" );
1124 /* Output the DLL functions arguments */
1126 fprintf( outfile, "static const unsigned char FuncArgs[%d] =\n{\n",
1127 Limit - Base + 1 );
1128 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1130 unsigned char args;
1131 switch(odp->type)
1133 case TYPE_STDCALL:
1134 args = (unsigned char)strlen(odp->u.func.arg_types);
1135 break;
1136 case TYPE_CDECL:
1137 args = 0x80 | (unsigned char)strlen(odp->u.func.arg_types);
1138 break;
1139 case TYPE_REGISTER:
1140 args = 0xfe;
1141 break;
1142 default:
1143 args = 0xff;
1144 break;
1146 fprintf( outfile, " 0x%02x", args );
1147 if (i < Limit) fprintf( outfile, ",\n" );
1149 fprintf( outfile, "\n};\n\n" );
1151 /* Output the DLL descriptor */
1153 fprintf( outfile, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1154 DLLName );
1155 fprintf( outfile, " \"%s\",\n", DLLName );
1156 fprintf( outfile, " %d,\n", Base );
1157 fprintf( outfile, " %d,\n", Limit - Base + 1 );
1158 fprintf( outfile, " %d,\n", nb_names );
1159 fprintf( outfile,
1160 " Functions,\n"
1161 " FuncNames,\n"
1162 " FuncOrdinals,\n"
1163 " FuncArgs,\n"
1164 " ArgTypes,\n");
1165 fprintf( outfile, " %s\n", DLLInitFunc[0] ? DLLInitFunc : "0" );
1166 fprintf( outfile, "};\n" );
1167 return 0;
1171 /*******************************************************************
1172 * BuildSpec16File
1174 * Build a Win16 assembly file from a spec file.
1176 static int BuildSpec16File( char * specfile, FILE *outfile )
1178 ORDDEF *odp;
1179 int i;
1180 int code_offset, data_offset, module_size;
1181 unsigned char *data;
1183 data = (unsigned char *)xmalloc( 0x10000 );
1184 memset( data, 0, 16 );
1185 data_offset = 16;
1187 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
1188 fprintf( outfile, "\t.text\n" );
1189 fprintf( outfile, "Code_Start:\n" );
1190 code_offset = 0;
1192 odp = OrdinalDefinitions;
1193 for (i = 0; i <= Limit; i++, odp++)
1195 switch (odp->type)
1197 case TYPE_INVALID:
1198 odp->offset = 0xffff;
1199 break;
1201 case TYPE_ABS:
1202 odp->offset = LOWORD(odp->u.abs.value);
1203 break;
1205 case TYPE_BYTE:
1206 odp->offset = data_offset;
1207 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1208 break;
1210 case TYPE_WORD:
1211 odp->offset = data_offset;
1212 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1213 break;
1215 case TYPE_LONG:
1216 odp->offset = data_offset;
1217 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1218 break;
1220 case TYPE_RETURN:
1221 fprintf( outfile,"/* %s.%d */\n", DLLName, i);
1222 fprintf( outfile,"\tmovw $%d,%%ax\n",LOWORD(odp->u.ret.ret_value));
1223 fprintf( outfile,"\tmovw $%d,%%dx\n",HIWORD(odp->u.ret.ret_value));
1224 fprintf( outfile,"\t.byte 0x66\n");
1225 if (odp->u.ret.arg_size != 0)
1226 fprintf( outfile, "\tlret $%d\n\n", odp->u.ret.arg_size);
1227 else
1229 fprintf( outfile, "\tlret\n");
1230 fprintf( outfile, "\tnop\n");
1231 fprintf( outfile, "\tnop\n\n");
1233 odp->offset = code_offset;
1234 code_offset += 12; /* Assembly code is 12 bytes long */
1235 break;
1237 case TYPE_REGISTER:
1238 case TYPE_CDECL:
1239 case TYPE_PASCAL:
1240 case TYPE_PASCAL_16:
1241 case TYPE_STUB:
1242 fprintf( outfile, "/* %s.%d */\n", DLLName, i);
1243 fprintf( outfile, "\tpushw %%bp\n" );
1244 fprintf( outfile, "\tpushl $" PREFIX "%s\n",odp->u.func.link_name);
1245 /* FreeBSD does not understand lcall, so do it the hard way */
1246 fprintf( outfile, "\t.byte 0x9a\n" );
1247 fprintf( outfile, "\t.long " PREFIX "CallFrom16_%s_%s_%s\n",
1248 (odp->type == TYPE_CDECL) ? "c" : "p",
1249 (odp->type == TYPE_REGISTER) ? "regs" :
1250 (odp->type == TYPE_PASCAL_16) ? "word" : "long",
1251 odp->u.func.arg_types );
1252 fprintf( outfile, "\t.long 0x%08lx\n",
1253 MAKELONG( Code_Selector, 0x9090 /* nop ; nop */ ) );
1254 odp->offset = code_offset;
1255 code_offset += 16; /* Assembly code is 16 bytes long */
1256 break;
1258 default:
1259 fprintf(stderr,"build: function type %d not available for Win16\n",
1260 odp->type);
1261 return -1;
1265 if (!code_offset) /* Make sure the code segment is not empty */
1267 fprintf( outfile, "\t.byte 0\n" );
1268 code_offset++;
1271 /* Output data segment */
1273 DumpBytes( outfile, data, data_offset, NULL, "Data_Start" );
1275 /* Build the module */
1277 module_size = BuildModule16( outfile, code_offset, data_offset );
1279 /* Output the DLL descriptor */
1281 fprintf( outfile, "\t.text\n" );
1282 fprintf( outfile, "DLLName:\t" STRING " \"%s\\0\"\n", DLLName );
1283 fprintf( outfile, "\t.align 4\n" );
1284 fprintf( outfile, "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1285 fprintf( outfile, PREFIX "%s_Descriptor:\n", DLLName );
1286 fprintf( outfile, "\t.long DLLName\n" ); /* Name */
1287 fprintf( outfile, "\t.long Module_Start\n" ); /* Module start */
1288 fprintf( outfile, "\t.long %d\n", module_size ); /* Module size */
1289 fprintf( outfile, "\t.long Code_Start\n" ); /* Code start */
1290 fprintf( outfile, "\t.long Data_Start\n" ); /* Data start */
1291 return 0;
1295 /*******************************************************************
1296 * BuildSpecFile
1298 * Build an assembly file from a spec file.
1300 static int BuildSpecFile( FILE *outfile, char *specname )
1302 SpecName = specname;
1303 SpecFp = fopen( specname, "r");
1304 if (SpecFp == NULL)
1306 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1307 return -1;
1310 if (ParseTopLevel() < 0) return -1;
1312 switch(SpecType)
1314 case SPEC_WIN16:
1315 return BuildSpec16File( specname, outfile );
1316 case SPEC_WIN32:
1317 return BuildSpec32File( specname, outfile );
1318 default:
1319 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1320 return -1;
1325 /*******************************************************************
1326 * TransferArgs16To32
1328 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1329 * The 16-bit stack layout is:
1330 * ... ...
1331 * (bp+8) arg2
1332 * (bp+6) arg1
1333 * (bp+4) cs
1334 * (bp+2) ip
1335 * (bp) bp
1337 * For 'cdecl' argn up to arg1 are reversed.
1339 static int TransferArgs16To32( FILE *outfile, char *args, int usecdecl )
1341 int i, pos16, pos32;
1342 char *xargs;
1344 /* Copy the arguments */
1346 pos16 = 6; /* skip bp and return address */
1347 pos32 = usecdecl ? -(strlen(args) * 4) : 0;
1348 xargs = usecdecl ? args:args+strlen(args);
1350 for (i = strlen(args); i > 0; i--)
1352 if (!usecdecl) {
1353 pos32 -= 4;
1354 xargs--;
1356 switch(*xargs)
1358 case 'w': /* word */
1359 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1360 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1361 pos16 += 2;
1362 break;
1364 case 's': /* s_word */
1365 fprintf( outfile, "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1366 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1367 pos16 += 2;
1368 break;
1370 case 'l': /* long or segmented pointer */
1371 case 'T': /* segmented pointer to null-terminated string */
1372 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", pos16 );
1373 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1374 pos16 += 4;
1375 break;
1377 case 'p': /* linear pointer */
1378 case 't': /* linear pointer to null-terminated string */
1379 /* Get the selector */
1380 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1381 /* Get the selector base */
1382 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1383 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1384 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1385 /* Add the offset */
1386 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1387 fprintf( outfile, "\taddl %%eax,%d(%%ebx)\n", pos32 );
1388 pos16 += 4;
1389 break;
1391 default:
1392 fprintf( stderr, "Unknown arg type '%c'\n", *xargs );
1394 if (usecdecl) {
1395 pos32 += 4;
1396 xargs++;
1400 return pos16 - 6; /* Return the size of the 16-bit args */
1404 /*******************************************************************
1405 * BuildContext16
1407 * Build the context structure on the 32-bit stack.
1409 static void BuildContext16( FILE *outfile )
1411 /* Store the registers */
1413 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1414 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1415 fprintf( outfile, "\tmovl %%ecx,%d(%%ebx)\n",
1416 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1417 fprintf( outfile, "\tmovl %%edx,%d(%%ebx)\n",
1418 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1419 fprintf( outfile, "\tmovl %%esi,%d(%%ebx)\n",
1420 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1421 fprintf( outfile, "\tmovl %%edi,%d(%%ebx)\n",
1422 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1424 fprintf( outfile, "\tmovl -24(%%ebp),%%eax\n" ); /* Get %ebx from stack*/
1425 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1426 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1427 fprintf( outfile, "\tmovzwl -10(%%ebp),%%eax\n" ); /* Get %ds from stack*/
1428 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1429 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1430 fprintf( outfile, "\tmovzwl -6(%%ebp),%%eax\n" ); /* Get %es from stack*/
1431 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1432 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1433 fprintf( outfile, "\tpushfl\n" );
1434 fprintf( outfile, "\tpopl %d(%%ebx)\n",
1435 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1436 fprintf( outfile, "\tmovl -20(%%ebp),%%eax\n" ); /* Get %ebp from stack */
1437 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1438 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1439 fprintf( outfile, "\tmovzwl 2(%%ebp),%%eax\n" ); /* Get %ip from stack */
1440 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1441 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1442 fprintf( outfile, "\tleal 2(%%ebp),%%eax\n" ); /* Get initial %sp */
1443 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1444 CONTEXTOFFSET(Esp) - sizeof(CONTEXT) );
1445 fprintf( outfile, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1446 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1447 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1448 fprintf( outfile, "\tmovzwl -14(%%ebp),%%eax\n" ); /* Get %fs from stack */
1449 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1450 CONTEXTOFFSET(SegFs) - sizeof(CONTEXT) );
1451 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1452 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1453 CONTEXTOFFSET(SegGs) - sizeof(CONTEXT) );
1454 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1455 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1456 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1457 #if 0
1458 fprintf( outfile, "\tfsave %d(%%ebx)\n",
1459 CONTEXTOFFSET(FloatSave) - sizeof(CONTEXT) );
1460 #endif
1464 /*******************************************************************
1465 * RestoreContext16
1467 * Restore the registers from the context structure.
1469 static void RestoreContext16( FILE *outfile )
1471 /* Get the 32-bit stack pointer */
1473 fprintf( outfile, "\tleal -%d(%%ebp),%%ebx\n",
1474 STRUCTOFFSET(STACK32FRAME,ebp) );
1476 /* Remove everything up to (including) the return address
1477 * from the 16-bit stack */
1479 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1480 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1481 fprintf( outfile, "\tmovw %%ax,%%ss\n" );
1482 fprintf( outfile, "\tmovl %d(%%ebx),%%esp\n",
1483 CONTEXTOFFSET(Esp) - sizeof(CONTEXT) );
1484 fprintf( outfile, "\taddl $4,%%esp\n" ); /* Remove return address */
1486 /* Restore the registers */
1488 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n",
1489 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1490 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n",
1491 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1492 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n",
1493 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1494 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n",
1495 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1496 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n",
1497 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1498 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new cs */
1499 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1500 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new ip */
1501 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1502 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new ds */
1503 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1504 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new es */
1505 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1506 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new fs */
1507 CONTEXTOFFSET(SegFs) - sizeof(CONTEXT) );
1508 fprintf( outfile, "\tpushl %d(%%ebx)\n",
1509 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1510 fprintf( outfile, "\tpopfl\n" );
1511 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1512 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1513 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n",
1514 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1515 fprintf( outfile, "\tpopl %%fs\n" ); /* Set fs */
1516 fprintf( outfile, "\tpopl %%es\n" ); /* Set es */
1517 fprintf( outfile, "\tpopl %%ds\n" ); /* Set ds */
1521 /*******************************************************************
1522 * BuildCallFrom16Func
1524 * Build a 16-bit-to-Wine callback function. The syntax of the function
1525 * profile is: call_type_xxxxx, where 'call' is the letter 'c' or 'p' for C or
1526 * Pascal calling convention, 'type' is one of 'regs', 'word' or
1527 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1528 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1529 * 'T'=segmented pointer to null-terminated string).
1530 * For register functions, the arguments are ignored, but they are still
1531 * removed from the stack upon return.
1533 * A special variant of the callback function is generated by the function
1534 * profile "t_long_". This is used by the Win95 16->32 thunk
1535 * functions C16ThkSL and C16ThkSL01 and is implemented as follows:
1536 * On entry, the EBX register is set up to contain a flat pointer to the
1537 * 16-bit stack such that EBX+22 points to the first argument.
1538 * Then, the entry point is called, while EBP is set up to point
1539 * to the return address (on the 32-bit stack).
1540 * The called function returns with CX set to the number of bytes
1541 * to be popped of the caller's stack.
1543 * Stack layout upon entry to the callback function:
1544 * ... ...
1545 * (sp+18) word first 16-bit arg
1546 * (sp+16) word cs
1547 * (sp+14) word ip
1548 * (sp+12) word bp
1549 * (sp+8) long 32-bit entry point (used to store edx)
1550 * (sp+6) word high word of cs (always 0, used to store es)
1551 * (sp+4) word low word of cs of 16-bit entry point
1552 * (sp+2) word high word of ip (always 0, used to store ds)
1553 * (sp) word low word of ip of 16-bit entry point
1555 * Added on the stack:
1556 * (sp-2) word saved fs
1557 * (sp-4) word buffer for Win16Mutex recursion count
1558 * (sp-8) long ebp
1559 * (sp-12) long saved previous stack
1561 static void BuildCallFrom16Func( FILE *outfile, char *profile )
1563 int argsize = 0;
1564 int short_ret = 0;
1565 int reg_func = 0;
1566 int cdecl = 0;
1567 int thunk = 0;
1568 char *args = profile + 7;
1570 /* Parse function type */
1572 if (!strncmp( "c_", profile, 2 )) cdecl = 1;
1573 else if (!strncmp( "t_", profile, 2 )) thunk = 1;
1574 else if (strncmp( "p_", profile, 2 ))
1576 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1577 return;
1580 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
1581 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
1582 else if (strncmp( "long_", profile + 2, 5 ))
1584 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1585 return;
1588 /* Function header */
1590 fprintf( outfile, "\n\t.align 4\n" );
1591 #ifdef USE_STABS
1592 fprintf( outfile, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX "CallFrom16_%s\n",
1593 profile, profile);
1594 #endif
1595 fprintf( outfile, "\t.globl " PREFIX "CallFrom16_%s\n", profile );
1596 fprintf( outfile, PREFIX "CallFrom16_%s:\n", profile );
1598 /* Save 16-bit fs and leave room for Win16Mutex recursion count */
1600 fprintf( outfile, "\t.byte 0x66\n\tpushl %%fs\n" );
1601 fprintf( outfile, "\tpushw $0\n" );
1603 /* Setup bp to point to its copy on the stack */
1605 fprintf( outfile, "\tpushl %%ebp\n" ); /* Save the full 32-bit ebp */
1606 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1607 fprintf( outfile, "\taddw $20,%%bp\n" );
1609 /* Save 16-bit ds and es */
1611 /* Stupid FreeBSD assembler doesn't know these either */
1612 /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1613 fprintf( outfile, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1614 /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1615 fprintf( outfile, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1617 /* Save %ebx */
1619 fprintf( outfile, "\tpushl %%ebx\n" );
1621 /* Restore 32-bit segment registers */
1623 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
1624 #ifdef __svr4__
1625 fprintf( outfile, "\tdata16\n");
1626 #endif
1627 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
1628 #ifdef __svr4__
1629 fprintf( outfile, "\tdata16\n");
1630 #endif
1631 fprintf( outfile, "\tmovw %%bx,%%es\n" );
1633 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
1635 /* Get the 32-bit stack pointer from the TEB */
1637 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET );
1639 /* Save the 16-bit stack */
1641 #ifdef __svr4__
1642 fprintf( outfile,"\tdata16\n");
1643 #endif
1644 fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss,(%d)\n", STACKOFFSET + 2 );
1645 fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp,(%d)\n", STACKOFFSET );
1647 /* Transfer the arguments */
1649 if (reg_func) BuildContext16( outfile );
1650 else if (*args) argsize = TransferArgs16To32( outfile, args, cdecl );
1651 else if (thunk)
1653 /* Get the stack selector base */
1654 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1655 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1656 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1657 fprintf( outfile, "\tmovl %%eax,-24(%%ebp)\n" );
1658 /* Add the offset */
1659 fprintf( outfile, "\tleal -16(%%ebp),%%eax\n" );
1660 fprintf( outfile, "\taddl %%eax,-24(%%ebp)\n" );
1663 /* Get the address of the API function */
1665 fprintf( outfile, "\tmovl -4(%%ebp),%%eax\n" );
1667 /* If necessary, save %edx over the API function address */
1669 if (!reg_func && short_ret)
1670 fprintf( outfile, "\tmovl %%edx,-4(%%ebp)\n" );
1672 /* Restore %ebx and store the 32-bit stack pointer instead */
1674 fprintf( outfile, "\tmovl %%ebx,%%ebp\n" );
1675 fprintf( outfile, "\tpopl %%ebx\n" );
1676 fprintf( outfile, "\tpushl %%ebp\n" );
1678 /* Switch to the 32-bit stack */
1680 fprintf( outfile, "\tpushl %%ds\n" );
1681 fprintf( outfile, "\tpopl %%ss\n" );
1682 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1683 reg_func ? sizeof(CONTEXT) : 4 * strlen(args) );
1684 if (reg_func) /* Push the address of the context struct */
1685 fprintf( outfile, "\tpushl %%esp\n" );
1687 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1689 fprintf( outfile, "\taddl $%d,%%ebp\n", STRUCTOFFSET(STACK32FRAME,ebp) );
1691 /* Print the debug information before the call */
1693 if (debugging && !thunk)
1695 int ftype = 0;
1697 if (cdecl) ftype |= 4;
1698 if (reg_func) ftype |= 2;
1699 if (short_ret) ftype |= 1;
1701 fprintf( outfile, "\tpushl %%eax\n" );
1702 fprintf( outfile, "\tpushl $Profile_%s\n", profile );
1703 fprintf( outfile, "\tpushl $%d\n", ftype );
1704 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n" );
1705 fprintf( outfile, "\tpopl %%eax\n" );
1706 fprintf( outfile, "\tpopl %%eax\n" );
1707 fprintf( outfile, "\tpopl %%eax\n" );
1710 /* Call the entry point */
1712 if (thunk)
1714 fprintf( outfile, "\tpushl %%ebp\n" );
1715 fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
1716 fprintf( outfile, "\tcall *%%eax\n" );
1717 fprintf( outfile, "\tpopl %%ebp\n" );
1719 else
1720 fprintf( outfile, "\tcall *%%eax\n" );
1723 /* Print the debug information after the call */
1725 if (debugging && !thunk)
1727 if (reg_func)
1729 /* Push again the address of the context struct in case */
1730 /* it has been removed by an stdcall function */
1731 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1732 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME,ebp) );
1733 fprintf( outfile, "\tpushl %%esp\n" );
1735 fprintf( outfile, "\tpushl %%eax\n" );
1736 fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1737 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n" );
1738 fprintf( outfile, "\tpopl %%eax\n" );
1739 fprintf( outfile, "\tpopl %%eax\n" );
1742 /* Restore the 16-bit stack */
1744 #ifdef __svr4__
1745 fprintf( outfile, "\tdata16\n");
1746 #endif
1747 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2 );
1748 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
1749 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
1751 if (reg_func)
1753 /* Calc the arguments size */
1754 while (*args)
1756 switch(*args)
1758 case 'w':
1759 case 's':
1760 argsize += 2;
1761 break;
1762 case 'p':
1763 case 't':
1764 case 'l':
1765 case 'T':
1766 argsize += 4;
1767 break;
1768 default:
1769 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1771 args++;
1774 /* Restore registers from the context structure */
1775 RestoreContext16( outfile );
1777 else
1779 /* Restore high 16 bits of ebp */
1780 fprintf( outfile, "\tpopl %%ebp\n" );
1782 /* Restore ds and es */
1783 fprintf( outfile, "\tincl %%esp\n" ); /* Remove mutex count */
1784 fprintf( outfile, "\tincl %%esp\n" );
1785 fprintf( outfile, "\tpopl %%edx\n" ); /* Remove ip and fs */
1786 fprintf( outfile, "\tmovw %%dx,%%fs\n" ); /* and restore fs */
1787 fprintf( outfile, "\tpopl %%edx\n" ); /* Remove cs and ds */
1788 fprintf( outfile, "\tmovw %%dx,%%ds\n" ); /* and restore ds */
1789 fprintf( outfile, "\t.byte 0x66\n\tpopl %%es\n" ); /* Restore es */
1791 if (short_ret) fprintf( outfile, "\tpopl %%edx\n" ); /* Restore edx */
1792 else
1794 /* Get the return value into dx:ax */
1795 fprintf( outfile, "\tmovl %%eax,%%edx\n" );
1796 fprintf( outfile, "\tshrl $16,%%edx\n" );
1797 /* Remove API entry point */
1798 fprintf( outfile, "\taddl $4,%%esp\n" );
1801 /* Restore low 16 bits of ebp */
1802 fprintf( outfile, "\tpopw %%bp\n" );
1805 /* Remove the arguments and return */
1807 if (thunk)
1809 fprintf( outfile, "\tpopl %%ebx\n" );
1810 fprintf( outfile, "\txorb %%ch,%%ch\n" );
1811 fprintf( outfile, "\taddw %%cx, %%sp\n" );
1812 fprintf( outfile, "\tpushl %%ebx\n" );
1813 fprintf( outfile, "\t.byte 0x66\n" );
1814 fprintf( outfile, "\tlret\n" );
1816 else if (argsize && !cdecl)
1818 fprintf( outfile, "\t.byte 0x66\n" );
1819 fprintf( outfile, "\tlret $%d\n", argsize );
1821 else
1823 fprintf( outfile, "\t.byte 0x66\n" );
1824 fprintf( outfile, "\tlret\n" );
1829 /*******************************************************************
1830 * BuildCallTo16Func
1832 * Build a Wine-to-16-bit callback function.
1834 * Stack frame of the callback function:
1835 * ... ...
1836 * (ebp+16) arg2
1837 * (ebp+12) arg1
1838 * (ebp+8) func to call
1839 * (ebp+4) return address
1840 * (ebp) previous ebp
1842 * Prototypes for the CallTo16 functions:
1843 * extern WINAPI WORD CallTo16_word_xxx( FARPROC16 func, args... );
1844 * extern WINAPI LONG CallTo16_long_xxx( FARPROC16 func, args... );
1845 * extern WINAPI void CallTo16_sreg_( const CONTEXT *context, int nb_args );
1846 * extern WINAPI void CallTo16_lreg_( const CONTEXT *context, int nb_args );
1848 static void BuildCallTo16Func( FILE *outfile, char *profile )
1850 int short_ret = 0;
1851 int reg_func = 0;
1852 char *args = profile + 5;
1854 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1855 else if (!strncmp( "sreg_", profile, 5 )) reg_func = 1;
1856 else if (!strncmp( "lreg_", profile, 5 )) reg_func = 2;
1857 else if (strncmp( "long_", profile, 5 ))
1859 fprintf( stderr, "Invalid function name '%s'.\n", profile );
1860 exit(1);
1863 /* Function header */
1865 fprintf( outfile, "\n\t.align 4\n" );
1866 #ifdef USE_STABS
1867 fprintf( outfile, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX "CallTo16_%s\n",
1868 profile, profile);
1869 #endif
1870 fprintf( outfile, "\t.globl " PREFIX "CallTo16_%s\n", profile );
1871 fprintf( outfile, PREFIX "CallTo16_%s:\n", profile );
1873 /* Entry code */
1875 fprintf( outfile, "\tpushl %%ebp\n" );
1876 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
1878 /* Save the 32-bit registers */
1880 fprintf( outfile, "\tpushl %%ebx\n" );
1881 fprintf( outfile, "\tpushl %%ecx\n" );
1882 fprintf( outfile, "\tpushl %%edx\n" );
1883 fprintf( outfile, "\tpushl %%esi\n" );
1884 fprintf( outfile, "\tpushl %%edi\n" );
1886 /* Enter Win16 Mutex */
1888 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock\n" );
1890 /* Print debugging info */
1892 if (debugging)
1894 /* Push the address of the first argument */
1895 fprintf( outfile, "\tleal 8(%%ebp),%%eax\n" );
1896 fprintf( outfile, "\tpushl $%d\n", reg_func ? -1 : strlen(args) );
1897 fprintf( outfile, "\tpushl %%eax\n" );
1898 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
1899 fprintf( outfile, "\tpopl %%eax\n" );
1900 fprintf( outfile, "\tpopl %%eax\n" );
1903 /* Call the actual CallTo16 routine (simulate a lcall) */
1905 fprintf( outfile, "\tpushl %%cs\n" );
1906 fprintf( outfile, "\tcall do_callto16_%s\n", profile );
1908 fprintf( outfile, "\tpushl %%eax\n" );
1910 /* Print debugging info */
1912 if (debugging)
1914 fprintf( outfile, "\tpushl %%eax\n" );
1915 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret\n" );
1916 fprintf( outfile, "\tpopl %%eax\n" );
1919 /* Leave Win16 Mutex */
1921 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock\n" );
1923 /* Restore the 32-bit registers */
1925 fprintf( outfile, "\tpopl %%eax\n" );
1926 fprintf( outfile, "\tpopl %%edi\n" );
1927 fprintf( outfile, "\tpopl %%esi\n" );
1928 fprintf( outfile, "\tpopl %%edx\n" );
1929 fprintf( outfile, "\tpopl %%ecx\n" );
1930 fprintf( outfile, "\tpopl %%ebx\n" );
1932 /* Exit code */
1934 #if 0
1935 /* FIXME: this is a hack because of task.c */
1936 if (!strcmp( profile, "word_" ))
1938 fprintf( outfile, ".globl " PREFIX "CALLTO16_Restore\n" );
1939 fprintf( outfile, PREFIX "CALLTO16_Restore:\n" );
1941 #endif
1942 fprintf( outfile, "\tpopl %%ebp\n" );
1943 fprintf( outfile, "\tret $%d\n", 4 * strlen(args) + 4 );
1946 /* Start of the actual CallTo16 routine */
1948 fprintf( outfile, "do_callto16_%s:\n", profile );
1950 /* Save the 32-bit stack */
1952 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
1953 fprintf( outfile, "\tmovl %%ebp,%%ebx\n" );
1954 fprintf( outfile, "\tmovl %%esp,%%edx\n" );
1956 if (reg_func)
1958 /* Switch to the 16-bit stack, saving the current %%esp, */
1959 /* and adding the specified offset to the new sp */
1960 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d),%%eax\n", STACKOFFSET );
1961 fprintf( outfile, "\tsubl 12(%%ebx),%%eax\n" ); /* Get the offset */
1962 #ifdef __svr4__
1963 fprintf( outfile,"\tdata16\n");
1964 #endif
1965 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
1966 fprintf( outfile, "\tmovl %%eax,%%esp\n" );
1967 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
1969 /* Get the registers. ebx is handled later on. */
1971 fprintf( outfile, "\tmovl 8(%%ebx),%%ebx\n" );
1972 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs) );
1973 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1974 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegFs) );
1975 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
1976 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp) );
1977 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax) );
1978 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx) );
1979 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx) );
1980 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi) );
1981 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi) );
1983 /* Push the return address
1984 * With sreg suffix, we push 16:16 address (normal lret)
1985 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
1987 if (reg_func == 1)
1988 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_long\n" );
1989 else
1991 fprintf( outfile, "\tpushw $0\n" );
1992 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_eax+2\n" );
1993 fprintf( outfile, "\tpushw $0\n" );
1994 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_eax\n" );
1997 /* Push the called routine address */
1999 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
2000 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
2002 /* Get the 16-bit ds */
2004 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
2005 /* Get ebx from the 32-bit stack */
2006 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(Ebx) );
2007 fprintf( outfile, "\tpopl %%ds\n" );
2009 else /* not a register function */
2011 int pos = 12; /* first argument position */
2013 /* Switch to the 16-bit stack */
2014 #ifdef __svr4__
2015 fprintf( outfile,"\tdata16\n");
2016 #endif
2017 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
2018 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
2019 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
2021 /* Make %bp point to the previous stackframe (built by CallFrom16) */
2022 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
2023 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n",
2024 STRUCTOFFSET(STACK16FRAME,bp) );
2026 /* Transfer the arguments */
2028 while (*args)
2030 switch(*args++)
2032 case 'w': /* word */
2033 fprintf( outfile, "\tpushw %d(%%ebx)\n", pos );
2034 break;
2035 case 'l': /* long */
2036 fprintf( outfile, "\tpushl %d(%%ebx)\n", pos );
2037 break;
2038 default:
2039 fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
2040 args[-1] );
2042 pos += 4;
2045 /* Push the return address */
2047 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_%s\n",
2048 short_ret ? "word" : "long" );
2050 /* Push the called routine address */
2052 fprintf( outfile, "\tpushl 8(%%ebx)\n" );
2054 /* Set %fs to the value saved by the last CallFrom16 */
2056 fprintf( outfile, "\tmovw -14(%%ebp),%%ax\n" );
2057 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2059 /* Set %ds and %es (and %ax just in case) equal to %ss */
2061 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
2062 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2063 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2066 /* Jump to the called routine */
2068 fprintf( outfile, "\t.byte 0x66\n" );
2069 fprintf( outfile, "\tlret\n" );
2073 /*******************************************************************
2074 * BuildRet16Func
2076 * Build the return code for 16-bit callbacks
2078 static void BuildRet16Func( FILE *outfile )
2080 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_word\n" );
2081 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_long\n" );
2082 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_eax\n" );
2084 fprintf( outfile, PREFIX "CALLTO16_Ret_word:\n" );
2085 fprintf( outfile, "\txorl %%edx,%%edx\n" );
2087 /* Put return value into %eax */
2089 fprintf( outfile, PREFIX "CALLTO16_Ret_long:\n" );
2090 fprintf( outfile, "\tshll $16,%%edx\n" );
2091 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
2092 fprintf( outfile, "\tmovl %%edx,%%eax\n" );
2093 fprintf( outfile, PREFIX "CALLTO16_Ret_eax:\n" );
2095 /* Restore 32-bit segment registers */
2097 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
2098 #ifdef __svr4__
2099 fprintf( outfile, "\tdata16\n");
2100 #endif
2101 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
2102 #ifdef __svr4__
2103 fprintf( outfile, "\tdata16\n");
2104 #endif
2105 fprintf( outfile, "\tmovw %%bx,%%es\n" );
2107 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
2109 /* Restore the 32-bit stack */
2111 #ifdef __svr4__
2112 fprintf( outfile, "\tdata16\n");
2113 #endif
2114 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
2115 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
2116 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2118 /* Return to caller */
2120 fprintf( outfile, "\tlret\n" );
2122 /* Declare the return address variables */
2124 fprintf( outfile, "\t.data\n" );
2125 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_word\n" );
2126 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_long\n" );
2127 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_eax\n" );
2128 fprintf( outfile, PREFIX "CALLTO16_RetAddr_word:\t.long 0\n" );
2129 fprintf( outfile, PREFIX "CALLTO16_RetAddr_long:\t.long 0\n" );
2130 fprintf( outfile, PREFIX "CALLTO16_RetAddr_eax:\t.long 0\n" );
2131 fprintf( outfile, "\t.text\n" );
2134 /*******************************************************************
2135 * BuildCallTo32CBClient
2137 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
2139 * Since the relay stub is itself 32-bit, this should not be a problem;
2140 * unfortunately, the relay stubs are expected to switch back to a
2141 * 16-bit stack (and 16-bit code) after completion :-(
2143 * This would conflict with our 16- vs. 32-bit stack handling, so
2144 * we simply switch *back* to our 32-bit stack before returning to
2145 * the caller ...
2147 * The CBClient relay stub expects to be called with:
2148 * - ebp pointing to the 16-bit stack at ss:bp
2149 * - ebx pointing to a buffer containing the saved 16-bit ss:sp
2151 * After completion, the stub will load ss:sp from the buffer at ebx
2152 * and perform a far return to 16-bit code.
2154 * To trick the relay stub into returning to us, we push a 16-bit
2155 * cs:ip pair pointing to out return entry point onto the 16-bit stack,
2156 * followed by a ss:sp pair pointing to *that* cs:ip pair.
2157 * Our return stub thus called will then reload the 32-bit ss:esp and
2158 * return to 32-bit code (by using and ss:esp value that we have also
2159 * pushed onto the 16-bit stack before and a cs:eip values found at
2160 * that position on the 32-bit stack). The layout of our
2161 * temporary area used on the 16-bit stack is thus as follows:
2163 * (ebx+12) 32-bit ss (flat)
2164 * (ebx+8) 32-bit sp (32-bit stack pointer)
2165 * (ebx+6) 16-bit cs (this segment)
2166 * (ebx+4) 16-bit ip ('16-bit' return entry point)
2167 * (ebx+2) 16-bit ss (16-bit stack segment)
2168 * (ebx+0) 16-bit sp (points to ebx+4)
2170 * The stack layout of this function:
2171 * (ebp+12) arg ebp value to be set for relay stub
2172 * (ebp+8) func CBClient relay stub address
2173 * (ebp+4) ret addr
2174 * (ebp) ebp
2176 static void BuildCallTo32CBClient( FILE *outfile )
2178 /* Function header */
2180 fprintf( outfile, "\n\t.align 4\n" );
2181 #ifdef USE_STABS
2182 fprintf( outfile, ".stabs \"CALL32_CBClient:F1\",36,0,0," PREFIX "CALL32_CBClient\n" );
2183 #endif
2184 fprintf( outfile, "\t.globl " PREFIX "CALL32_CBClient\n" );
2185 fprintf( outfile, PREFIX "CALL32_CBClient:\n" );
2187 /* Entry code */
2189 fprintf( outfile, "\tpushl %%ebp\n" );
2190 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2191 fprintf( outfile, "\tpushl %%edi\n" );
2192 fprintf( outfile, "\tpushl %%esi\n" );
2193 fprintf( outfile, "\tpushl %%ebx\n" );
2195 /* Get the 16-bit stack */
2197 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET);
2199 /* Convert it to a flat address */
2201 fprintf( outfile, "\tshldl $16,%%ebx,%%eax\n" );
2202 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
2203 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%esi\n" );
2204 fprintf( outfile, "\tmovw %%bx,%%ax\n" );
2205 fprintf( outfile, "\taddl %%eax,%%esi\n" );
2207 /* Allocate temporary area (simulate STACK16_PUSH) */
2209 fprintf( outfile, "\tpushf\n" );
2210 fprintf( outfile, "\tcld\n" );
2211 fprintf( outfile, "\tleal -16(%%esi), %%edi\n" );
2212 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2213 fprintf( outfile, "\trep\n\tmovsb\n" );
2214 fprintf( outfile, "\tpopf\n" );
2216 fprintf( outfile, "\t.byte 0x64\n\tsubw $16,(%d)\n", STACKOFFSET );
2218 fprintf( outfile, "\tpushl %%edi\n" ); /* remember address */
2220 /* Set up temporary area */
2222 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-16+4 );
2223 fprintf( outfile, "\tmovl %%ebx, (%%edi)\n" );
2225 fprintf( outfile, "\tmovl " PREFIX "CALL32_CBClient_RetAddr, %%eax\n" );
2226 fprintf( outfile, "\tmovl %%eax, 4(%%edi)\n" );
2228 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2229 fprintf( outfile, "\tmovl %%eax, 8(%%edi)\n" );
2231 fprintf( outfile, "\tmovl %%ss, %%ax\n" );
2232 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2233 fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
2235 /* Setup registers and call CBClient relay stub (simulating a far call) */
2237 fprintf( outfile, "\tmovl %%edi, %%ebx\n" );
2238 fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
2239 fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
2241 fprintf( outfile, "\tpushl %%cs\n" );
2242 fprintf( outfile, "\tcall *%%eax\n" );
2244 /* Cleanup temporary area (simulate STACK16_POP) */
2246 fprintf( outfile, "\tpop %%esi\n" );
2248 fprintf( outfile, "\tpushf\n" );
2249 fprintf( outfile, "\tstd\n" );
2250 fprintf( outfile, "\tdec %%esi\n" );
2251 fprintf( outfile, "\tleal 16(%%esi), %%edi\n" );
2252 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2253 fprintf( outfile, "\trep\n\tmovsb\n" );
2254 fprintf( outfile, "\tpopf\n" );
2256 fprintf( outfile, "\t.byte 0x64\n\taddw $16,(%d)\n", STACKOFFSET );
2258 /* Restore registers and return */
2260 fprintf( outfile, "\tpopl %%ebx\n" );
2261 fprintf( outfile, "\tpopl %%esi\n" );
2262 fprintf( outfile, "\tpopl %%edi\n" );
2263 fprintf( outfile, "\tpopl %%ebp\n" );
2264 fprintf( outfile, "\tret\n" );
2266 /* '16-bit' return stub */
2268 fprintf( outfile, "\t.globl " PREFIX "CALL32_CBClient_Ret\n" );
2269 fprintf( outfile, PREFIX "CALL32_CBClient_Ret:\n" );
2271 fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
2272 fprintf( outfile, "\tlssl %%ss:(%%ebx), %%esp\n" );
2273 fprintf( outfile, "\tlret\n" );
2275 /* Declare the return address variable */
2277 fprintf( outfile, "\t.data\n" );
2278 fprintf( outfile, "\t.globl " PREFIX "CALL32_CBClient_RetAddr\n" );
2279 fprintf( outfile, PREFIX "CALL32_CBClient_RetAddr:\t.long 0\n" );
2280 fprintf( outfile, "\t.text\n" );
2285 /*******************************************************************
2286 * BuildCallTo32LargeStack
2288 * Build the function used to switch to the original 32-bit stack
2289 * before calling a 32-bit function from 32-bit code. This is used for
2290 * functions that need a large stack, like X bitmaps functions.
2292 * The generated function has the following prototype:
2293 * int xxx( int (*func)(), void *arg );
2295 * The pointer to the function can be retrieved by calling CALL32_Init,
2296 * which also takes care of saving the current 32-bit stack pointer.
2298 * Stack layout:
2299 * ... ...
2300 * (ebp+12) arg
2301 * (ebp+8) func
2302 * (ebp+4) ret addr
2303 * (ebp) ebp
2305 static void BuildCallTo32LargeStack( FILE *outfile )
2307 /* Initialization function */
2309 fprintf( outfile, "\n\t.align 4\n" );
2310 #ifdef USE_STABS
2311 fprintf( outfile, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX "CALL32_Init\n");
2312 #endif
2313 fprintf( outfile, "\t.globl " PREFIX "CALL32_Init\n" );
2314 fprintf( outfile, "\t.type " PREFIX "CALL32_Init,@function\n" );
2315 fprintf( outfile, PREFIX "CALL32_Init:\n" );
2316 fprintf( outfile, "\tleal -256(%%esp),%%eax\n" );
2317 fprintf( outfile, "\tmovl %%eax,CALL32_Original32_esp\n" );
2318 fprintf( outfile, "\tmovl $CALL32_LargeStack,%%eax\n" );
2319 fprintf( outfile, "\tret\n" );
2321 /* Function header */
2323 fprintf( outfile, "\n\t.align 4\n" );
2324 #ifdef USE_STABS
2325 fprintf( outfile, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2326 #endif
2327 fprintf( outfile, "CALL32_LargeStack:\n" );
2329 /* Entry code */
2331 fprintf( outfile, "\tpushl %%ebp\n" );
2332 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2334 /* Switch to the original 32-bit stack pointer */
2336 fprintf( outfile, "\tmovl CALL32_Original32_esp, %%esp\n" );
2338 /* Transfer the argument and call the function */
2340 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2341 fprintf( outfile, "\tcall *8(%%ebp)\n" );
2343 /* Restore registers and return */
2345 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2346 fprintf( outfile, "\tpopl %%ebp\n" );
2347 fprintf( outfile, "\tret\n" );
2349 /* Data */
2351 fprintf( outfile, "\t.data\n" );
2352 fprintf( outfile, "CALL32_Original32_esp:\t.long 0\n" );
2353 fprintf( outfile, "\t.text\n" );
2357 /*******************************************************************
2358 * BuildCallFrom32Regs
2360 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2361 * 'args' is the number of dword arguments.
2363 * Stack layout:
2364 * ... ...
2365 * (esp+336) ret addr (or relay addr when debugging(relay) is on)
2366 * (esp+332) entry point
2367 * (esp+204) buffer area to allow stack frame manipulation
2368 * (esp+0) CONTEXT struct
2370 static void BuildCallFrom32Regs( FILE *outfile )
2372 #define STACK_SPACE 128
2374 /* Function header */
2376 fprintf( outfile, "\n\t.align 4\n" );
2377 #ifdef USE_STABS
2378 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
2379 #endif
2380 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
2381 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
2383 /* Allocate some buffer space on the stack */
2385 fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
2387 /* Build the context structure */
2389 fprintf( outfile, "\tpushw $0\n" );
2390 fprintf( outfile, "\t.byte 0x66\n\tpushl %%ss\n" );
2391 fprintf( outfile, "\tpushl %%eax\n" ); /* %esp place holder */
2392 fprintf( outfile, "\tpushfl\n" );
2393 fprintf( outfile, "\tpushw $0\n" );
2394 fprintf( outfile, "\t.byte 0x66\n\tpushl %%cs\n" );
2395 fprintf( outfile, "\tpushl %d(%%esp)\n", 16+STACK_SPACE+4 ); /* %eip at time of call */
2396 fprintf( outfile, "\tpushl %%ebp\n" );
2398 fprintf( outfile, "\tpushl %%eax\n" );
2399 fprintf( outfile, "\tpushl %%ecx\n" );
2400 fprintf( outfile, "\tpushl %%edx\n" );
2401 fprintf( outfile, "\tpushl %%ebx\n" );
2402 fprintf( outfile, "\tpushl %%esi\n" );
2403 fprintf( outfile, "\tpushl %%edi\n" );
2405 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2406 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2407 fprintf( outfile, "\tpushl %%eax\n" );
2408 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2409 fprintf( outfile, "\tpushl %%eax\n" );
2410 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2411 fprintf( outfile, "\tpushl %%eax\n" );
2412 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2413 fprintf( outfile, "\tpushl %%eax\n" );
2415 fprintf( outfile, "\tleal -%d(%%esp),%%esp\n",
2416 sizeof(FLOATING_SAVE_AREA) + 6 * sizeof(DWORD) /* DR regs */ );
2417 fprintf( outfile, "\tpushl $0x0001001f\n" ); /* ContextFlags */
2419 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2421 fprintf( outfile, "\tleal %d(%%esp),%%eax\n",
2422 sizeof(CONTEXT) + STACK_SPACE + 4 ); /* %esp at time of call */
2423 fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Esp) );
2425 fprintf( outfile, "\tcall " PREFIX "RELAY_CallFrom32Regs\n" );
2427 /* Restore the context structure */
2429 fprintf( outfile, "\tfrstor %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2431 /* Store %eip value onto the new stack */
2433 fprintf( outfile, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Eip) );
2434 fprintf( outfile, "\tmovl %d(%%esp),%%ebx\n", CONTEXTOFFSET(Esp) );
2435 fprintf( outfile, "\tmovl %%eax,0(%%ebx)\n" );
2437 /* Restore all registers */
2439 fprintf( outfile, "\tleal %d(%%esp),%%esp\n",
2440 sizeof(FLOATING_SAVE_AREA) + 7 * sizeof(DWORD) );
2441 fprintf( outfile, "\tpopl %%eax\n" );
2442 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2443 fprintf( outfile, "\tpopl %%eax\n" );
2444 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2445 fprintf( outfile, "\tpopl %%eax\n" );
2446 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2447 fprintf( outfile, "\tpopl %%eax\n" );
2448 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2450 fprintf( outfile, "\tpopl %%edi\n" );
2451 fprintf( outfile, "\tpopl %%esi\n" );
2452 fprintf( outfile, "\tpopl %%ebx\n" );
2453 fprintf( outfile, "\tpopl %%edx\n" );
2454 fprintf( outfile, "\tpopl %%ecx\n" );
2455 fprintf( outfile, "\tpopl %%eax\n" );
2456 fprintf( outfile, "\tpopl %%ebp\n" );
2457 fprintf( outfile, "\tleal 8(%%esp),%%esp\n" ); /* skip %eip and %cs */
2458 fprintf( outfile, "\tpopfl\n" );
2459 fprintf( outfile, "\tpopl %%esp\n" );
2460 fprintf( outfile, "\tret\n" );
2462 #undef STACK_SPACE
2466 /*******************************************************************
2467 * BuildSpec
2469 * Build the spec files
2471 static int BuildSpec( FILE *outfile, int argc, char *argv[] )
2473 int i;
2474 for (i = 2; i < argc; i++)
2475 if (BuildSpecFile( outfile, argv[i] ) < 0) return -1;
2476 return 0;
2480 /*******************************************************************
2481 * BuildCallFrom16
2483 * Build the 16-bit-to-Wine callbacks
2485 static int BuildCallFrom16( FILE *outfile, char * outname, int argc, char *argv[] )
2487 int i;
2488 char buffer[1024];
2490 /* File header */
2492 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2493 fprintf( outfile, "\t.text\n" );
2495 #ifdef USE_STABS
2496 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2497 getcwd(buffer, sizeof(buffer));
2500 * The stabs help the internal debugger as they are an indication that it
2501 * is sensible to step into a thunk/trampoline.
2503 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2504 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2505 fprintf( outfile, "\t.text\n" );
2506 fprintf( outfile, "\t.align 4\n" );
2507 fprintf( outfile, "Code_Start:\n\n" );
2508 #endif
2510 /* Build the callback functions */
2512 for (i = 2; i < argc; i++) BuildCallFrom16Func( outfile, argv[i] );
2514 /* Build the thunk callback function */
2516 BuildCallFrom16Func( outfile, "t_long_" );
2518 /* Output the argument debugging strings */
2520 if (debugging)
2522 fprintf( outfile, "/* Argument strings */\n" );
2523 for (i = 2; i < argc; i++)
2525 fprintf( outfile, "Profile_%s:\t", argv[i] );
2526 fprintf( outfile, STRING " \"%s\\0\"\n", argv[i] + 7 );
2530 #ifdef USE_STABS
2531 fprintf( outfile, "\t.text\n");
2532 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2533 fprintf( outfile, ".Letext:\n");
2534 #endif
2536 return 0;
2540 /*******************************************************************
2541 * BuildCallTo16
2543 * Build the Wine-to-16-bit callbacks
2545 static int BuildCallTo16( FILE *outfile, char * outname, int argc, char *argv[] )
2547 char buffer[1024];
2548 FILE *infile;
2550 if (argc > 2)
2552 infile = fopen( argv[2], "r" );
2553 if (!infile)
2555 perror( argv[2] );
2556 exit( 1 );
2559 else infile = stdin;
2561 /* File header */
2563 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2564 fprintf( outfile, "\t.text\n" );
2566 #ifdef USE_STABS
2567 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2568 getcwd(buffer, sizeof(buffer));
2571 * The stabs help the internal debugger as they are an indication that it
2572 * is sensible to step into a thunk/trampoline.
2574 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2575 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2576 fprintf( outfile, "\t.text\n" );
2577 fprintf( outfile, "\t.align 4\n" );
2578 fprintf( outfile, "Code_Start:\n\n" );
2579 #endif
2581 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Start\n" );
2582 fprintf( outfile, PREFIX "CALLTO16_Start:\n" );
2584 /* Build the callback functions */
2586 while (fgets( buffer, sizeof(buffer), infile ))
2588 if (strstr( buffer, "### start build ###" )) break;
2590 while (fgets( buffer, sizeof(buffer), infile ))
2592 char *p = strstr( buffer, "CallTo16_" );
2593 if (p)
2595 char *profile = p + strlen( "CallTo16_" );
2596 p = profile;
2597 while ((*p == '_') || isalpha(*p)) p++;
2598 *p = '\0';
2599 BuildCallTo16Func( outfile, profile );
2601 if (strstr( buffer, "### stop build ###" )) break;
2604 /* Output the 16-bit return code */
2606 BuildRet16Func( outfile );
2608 /* Output the CBClient callback function
2609 * (while this does not really 'call to 16-bit' code, it is placed
2610 * here so that its 16-bit return stub is defined within the CALLTO16
2611 * 16-bit segment)
2613 BuildCallTo32CBClient( outfile );
2616 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_End\n" );
2617 fprintf( outfile, PREFIX "CALLTO16_End:\n" );
2619 #ifdef USE_STABS
2620 fprintf( outfile, "\t.text\n");
2621 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2622 fprintf( outfile, ".Letext:\n");
2623 #endif
2625 fclose( infile );
2626 return 0;
2630 /*******************************************************************
2631 * BuildCall32
2633 * Build the 32-bit callbacks
2635 static int BuildCall32( FILE *outfile, char * outname )
2637 char buffer[1024];
2639 /* File header */
2641 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2642 fprintf( outfile, "\t.text\n" );
2644 #ifdef __i386__
2646 #ifdef USE_STABS
2647 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2648 getcwd(buffer, sizeof(buffer));
2651 * The stabs help the internal debugger as they are an indication that it
2652 * is sensible to step into a thunk/trampoline.
2654 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2655 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2656 fprintf( outfile, "\t.text\n" );
2657 fprintf( outfile, "\t.align 4\n" );
2658 fprintf( outfile, "Code_Start:\n" );
2659 #endif
2661 /* Build the 32-bit large stack callback */
2663 BuildCallTo32LargeStack( outfile );
2665 /* Build the register callback function */
2667 BuildCallFrom32Regs( outfile );
2669 #ifdef USE_STABS
2670 fprintf( outfile, "\t.text\n");
2671 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2672 fprintf( outfile, ".Letext:\n");
2673 #endif
2675 #else /* __i386__ */
2677 /* Just to avoid an empty file */
2678 fprintf( outfile, "\t.long 0\n" );
2680 #endif /* __i386__ */
2681 return 0;
2685 /*******************************************************************
2686 * usage
2688 static void usage(void)
2690 fprintf( stderr,
2691 "usage: build [-o outfile] -spec SPECNAMES\n"
2692 " build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2693 " build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2694 " build [-o outfile] -call32\n" );
2695 exit(1);
2699 /*******************************************************************
2700 * main
2702 int main(int argc, char **argv)
2704 char *outname = NULL;
2705 FILE *outfile = stdout;
2706 int res = -1;
2708 if (argc < 2) usage();
2710 if (!strcmp( argv[1], "-o" ))
2712 outname = argv[2];
2713 argv += 2;
2714 argc -= 2;
2715 if (argc < 2) usage();
2716 if (!(outfile = fopen( outname, "w" )))
2718 fprintf( stderr, "Unable to create output file '%s'\n", outname );
2719 exit(1);
2723 /* Retrieve the selector values; this assumes that we are building
2724 * the asm files on the platform that will also run them. Probably
2725 * a safe assumption to make.
2727 GET_CS( Code_Selector );
2728 GET_DS( Data_Selector );
2730 if (!strcmp( argv[1], "-spec" ))
2731 res = BuildSpec( outfile, argc, argv );
2732 else if (!strcmp( argv[1], "-callfrom16" ))
2733 res = BuildCallFrom16( outfile, outname, argc, argv );
2734 else if (!strcmp( argv[1], "-callto16" ))
2735 res = BuildCallTo16( outfile, outname, argc, argv );
2736 else if (!strcmp( argv[1], "-call32" ))
2737 res = BuildCall32( outfile, outname );
2738 else
2740 fclose( outfile );
2741 unlink( outname );
2742 usage();
2745 fclose( outfile );
2746 if (res < 0)
2748 unlink( outname );
2749 return 1;
2751 return 0;