Release 971116
[wine.git] / tools / build.c
blob952bb9f9cd0a834ff199e1dbaca7b3ee6cd1f484
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"
22 #ifdef NEED_UNDERSCORE_PREFIX
23 # define PREFIX "_"
24 #else
25 # define PREFIX
26 #endif
28 #if defined(__GNUC__) && !defined(__svr4__)
29 # define USE_STABS
30 #else
31 # undef USE_STABS
32 #endif
34 typedef enum
36 TYPE_INVALID,
37 TYPE_BYTE, /* byte variable (Win16) */
38 TYPE_WORD, /* word variable (Win16) */
39 TYPE_LONG, /* long variable (Win16) */
40 TYPE_PASCAL_16, /* pascal function with 16-bit return (Win16) */
41 TYPE_PASCAL, /* pascal function with 32-bit return (Win16) */
42 TYPE_ABS, /* absolute value (Win16) */
43 TYPE_RETURN, /* simple return value function (Win16) */
44 TYPE_REGISTER, /* register function */
45 TYPE_STUB, /* unimplemented stub */
46 TYPE_STDCALL, /* stdcall function (Win32) */
47 TYPE_CDECL, /* cdecl function (Win32) */
48 TYPE_VARARGS, /* varargs function (Win32) */
49 TYPE_EXTERN, /* external symbol (Win32) */
50 TYPE_NBTYPES
51 } ORD_TYPE;
53 static const char * const TypeNames[TYPE_NBTYPES] =
55 NULL,
56 "byte", /* TYPE_BYTE */
57 "word", /* TYPE_WORD */
58 "long", /* TYPE_LONG */
59 "pascal16", /* TYPE_PASCAL_16 */
60 "pascal", /* TYPE_PASCAL */
61 "equate", /* TYPE_ABS */
62 "return", /* TYPE_RETURN */
63 "register", /* TYPE_REGISTER */
64 "stub", /* TYPE_STUB */
65 "stdcall", /* TYPE_STDCALL */
66 "cdecl", /* TYPE_CDECL */
67 "varargs", /* TYPE_VARARGS */
68 "extern" /* TYPE_EXTERN */
71 #define MAX_ORDINALS 1299
73 /* Callback function used for stub functions */
74 #define STUB_CALLBACK \
75 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
77 typedef enum
79 SPEC_INVALID,
80 SPEC_WIN16,
81 SPEC_WIN32
82 } SPEC_TYPE;
84 typedef struct
86 int n_values;
87 int *values;
88 } ORD_VARIABLE;
90 typedef struct
92 int n_args;
93 char arg_types[32];
94 char link_name[80];
95 } ORD_FUNCTION;
97 typedef struct
99 int arg_size;
100 int ret_value;
101 } ORD_RETURN;
103 typedef struct
105 int value;
106 } ORD_ABS;
108 typedef struct
110 char link_name[80];
111 } ORD_VARARGS;
113 typedef struct
115 char link_name[80];
116 } ORD_EXTERN;
118 typedef struct
120 ORD_TYPE type;
121 int offset;
122 int lineno;
123 char name[80];
124 union
126 ORD_VARIABLE var;
127 ORD_FUNCTION func;
128 ORD_RETURN ret;
129 ORD_ABS abs;
130 ORD_VARARGS vargs;
131 ORD_EXTERN ext;
132 } u;
133 } ORDDEF;
135 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
137 static SPEC_TYPE SpecType = SPEC_INVALID;
138 static char DLLName[80];
139 static char DLLFileName[80];
140 static int Limit = 0;
141 static int Base = MAX_ORDINALS;
142 static int DLLHeapSize = 0;
143 static char *SpecName;
144 static FILE *SpecFp;
145 static WORD Code_Selector, Data_Selector;
147 char *ParseBuffer = NULL;
148 char *ParseNext;
149 char ParseSaveChar;
150 int Line;
152 static int debugging = 1;
154 /* Offset of a structure field relative to the start of the struct */
155 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
157 /* Offset of register relative to the start of the CONTEXT struct */
158 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT,reg)
160 static void *xmalloc (size_t size)
162 void *res;
164 res = malloc (size ? size : 1);
165 if (res == NULL)
167 fprintf (stderr, "Virtual memory exhausted.\n");
168 exit (1);
170 return res;
174 static void *xrealloc (void *ptr, size_t size)
176 void *res = realloc (ptr, size);
177 if (res == NULL)
179 fprintf (stderr, "Virtual memory exhausted.\n");
180 exit (1);
182 return res;
186 static int IsNumberString(char *s)
188 while (*s != '\0')
189 if (!isdigit(*s++))
190 return 0;
192 return 1;
195 static char *strupper(char *s)
197 char *p;
199 for(p = s; *p != '\0'; p++)
200 *p = toupper(*p);
202 return s;
205 static char * GetTokenInLine(void)
207 char *p;
208 char *token;
210 if (ParseNext != ParseBuffer)
212 if (ParseSaveChar == '\0')
213 return NULL;
214 *ParseNext = ParseSaveChar;
218 * Remove initial white space.
220 for (p = ParseNext; isspace(*p); p++)
223 if ((*p == '\0') || (*p == '#'))
224 return NULL;
227 * Find end of token.
229 token = p++;
230 if (*token != '(' && *token != ')')
231 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
232 p++;
234 ParseSaveChar = *p;
235 ParseNext = p;
236 *p = '\0';
238 return token;
241 static char * GetToken(void)
243 char *token;
245 if (ParseBuffer == NULL)
247 ParseBuffer = xmalloc(512);
248 ParseNext = ParseBuffer;
249 while (1)
251 Line++;
252 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
253 return NULL;
254 if (ParseBuffer[0] != '#')
255 break;
259 while ((token = GetTokenInLine()) == NULL)
261 ParseNext = ParseBuffer;
262 while (1)
264 Line++;
265 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
266 return NULL;
267 if (ParseBuffer[0] != '#')
268 break;
272 return token;
276 /*******************************************************************
277 * ParseVariable
279 * Parse a variable definition.
281 static int ParseVariable( ORDDEF *odp )
283 char *endptr;
284 int *value_array;
285 int n_values;
286 int value_array_size;
288 char *token = GetToken();
289 if (*token != '(')
291 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
292 SpecName, Line, token);
293 return -1;
296 n_values = 0;
297 value_array_size = 25;
298 value_array = xmalloc(sizeof(*value_array) * value_array_size);
300 while ((token = GetToken()) != NULL)
302 if (*token == ')')
303 break;
305 value_array[n_values++] = strtol(token, &endptr, 0);
306 if (n_values == value_array_size)
308 value_array_size += 25;
309 value_array = xrealloc(value_array,
310 sizeof(*value_array) * value_array_size);
313 if (endptr == NULL || *endptr != '\0')
315 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
316 SpecName, Line, token);
317 return -1;
321 if (token == NULL)
323 fprintf(stderr, "%s:%d: End of file in variable declaration\n",
324 SpecName, Line);
325 return -1;
328 odp->u.var.n_values = n_values;
329 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
331 return 0;
335 /*******************************************************************
336 * ParseExportFunction
338 * Parse a function definition.
340 static int ParseExportFunction( ORDDEF *odp )
342 char *token;
343 int i;
345 switch(SpecType)
347 case SPEC_WIN16:
348 if (odp->type == TYPE_STDCALL)
350 fprintf( stderr, "%s:%d: 'stdcall' not supported for Win16\n",
351 SpecName, Line );
352 return -1;
354 if (odp->type == TYPE_CDECL)
356 fprintf( stderr, "%s:%d: 'cdecl' not supported for Win16\n",
357 SpecName, Line );
358 return -1;
360 break;
361 case SPEC_WIN32:
362 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
364 fprintf( stderr, "%s:%d: 'pascal' not supported for Win32\n",
365 SpecName, Line );
366 return -1;
368 break;
369 default:
370 break;
373 token = GetToken();
374 if (*token != '(')
376 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
377 SpecName, Line, token);
378 return -1;
381 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
383 token = GetToken();
384 if (*token == ')')
385 break;
387 if (!strcmp(token, "word"))
388 odp->u.func.arg_types[i] = 'w';
389 else if (!strcmp(token, "s_word"))
390 odp->u.func.arg_types[i] = 's';
391 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
392 odp->u.func.arg_types[i] = 'l';
393 else if (!strcmp(token, "ptr"))
394 odp->u.func.arg_types[i] = 'p';
395 else if (!strcmp(token, "str"))
396 odp->u.func.arg_types[i] = 't';
397 else if (!strcmp(token, "wstr"))
398 odp->u.func.arg_types[i] = 'W';
399 else if (!strcmp(token, "segstr"))
400 odp->u.func.arg_types[i] = 'T';
401 else if (!strcmp(token, "double"))
403 odp->u.func.arg_types[i++] = 'l';
404 odp->u.func.arg_types[i] = 'l';
406 else
408 fprintf(stderr, "%s:%d: Unknown variable type '%s'\n",
409 SpecName, Line, token);
410 return -1;
412 if (SpecType == SPEC_WIN32)
414 if (strcmp(token, "long") &&
415 strcmp(token, "ptr") &&
416 strcmp(token, "str") &&
417 strcmp(token, "wstr") &&
418 strcmp(token, "double"))
420 fprintf( stderr, "%s:%d: Type '%s' not supported for Win32\n",
421 SpecName, Line, token );
422 return -1;
426 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
428 fprintf( stderr, "%s:%d: Too many arguments\n", SpecName, Line );
429 return -1;
431 odp->u.func.arg_types[i] = '\0';
432 if ((odp->type == TYPE_STDCALL) && !i)
433 odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
434 if ((odp->type == TYPE_REGISTER) && (SpecType == SPEC_WIN32) && i)
436 fprintf( stderr, "%s:%d: register functions cannot have arguments in Win32\n",
437 SpecName, Line );
438 return -1;
440 strcpy(odp->u.func.link_name, GetToken());
441 return 0;
445 /*******************************************************************
446 * ParseEquate
448 * Parse an 'equate' definition.
450 static int ParseEquate( ORDDEF *odp )
452 char *endptr;
454 char *token = GetToken();
455 int value = strtol(token, &endptr, 0);
456 if (endptr == NULL || *endptr != '\0')
458 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
459 SpecName, Line, token);
460 return -1;
463 if (SpecType == SPEC_WIN32)
465 fprintf( stderr, "%s:%d: 'equate' not supported for Win32\n",
466 SpecName, Line );
467 return -1;
470 odp->u.abs.value = value;
471 return 0;
475 /*******************************************************************
476 * ParseReturn
478 * Parse a 'return' definition.
480 static int ParseReturn( ORDDEF *odp )
482 char *token;
483 char *endptr;
485 token = GetToken();
486 odp->u.ret.arg_size = strtol(token, &endptr, 0);
487 if (endptr == NULL || *endptr != '\0')
489 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
490 SpecName, Line, token);
491 return -1;
494 token = GetToken();
495 odp->u.ret.ret_value = strtol(token, &endptr, 0);
496 if (endptr == NULL || *endptr != '\0')
498 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
499 SpecName, Line, token);
500 return -1;
503 if (SpecType == SPEC_WIN32)
505 fprintf( stderr, "%s:%d: 'return' not supported for Win32\n",
506 SpecName, Line );
507 return -1;
510 return 0;
514 /*******************************************************************
515 * ParseStub
517 * Parse a 'stub' definition.
519 static int ParseStub( ORDDEF *odp )
521 odp->u.func.arg_types[0] = '\0';
522 strcpy( odp->u.func.link_name, STUB_CALLBACK );
523 return 0;
527 /*******************************************************************
528 * ParseVarargs
530 * Parse an 'varargs' definition.
532 static int ParseVarargs( ORDDEF *odp )
534 char *token;
536 if (SpecType == SPEC_WIN16)
538 fprintf( stderr, "%s:%d: 'varargs' not supported for Win16\n",
539 SpecName, Line );
540 return -1;
543 token = GetToken();
544 if (*token != '(')
546 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
547 SpecName, Line, token);
548 return -1;
550 token = GetToken();
551 if (*token != ')')
553 fprintf(stderr, "%s:%d: Expected ')' got '%s'\n",
554 SpecName, Line, token);
555 return -1;
558 strcpy( odp->u.vargs.link_name, GetToken() );
559 return 0;
563 /*******************************************************************
564 * ParseExtern
566 * Parse an 'extern' definition.
568 static int ParseExtern( ORDDEF *odp )
570 if (SpecType == SPEC_WIN16)
572 fprintf( stderr, "%s:%d: 'extern' not supported for Win16\n",
573 SpecName, Line );
574 return -1;
576 strcpy( odp->u.ext.link_name, GetToken() );
577 return 0;
581 /*******************************************************************
582 * ParseOrdinal
584 * Parse an ordinal definition.
586 static int ParseOrdinal(int ordinal)
588 ORDDEF *odp;
589 char *token;
591 if (ordinal >= MAX_ORDINALS)
593 fprintf(stderr, "%s:%d: Ordinal number too large\n", SpecName, Line );
594 return -1;
596 if (ordinal > Limit) Limit = ordinal;
597 if (ordinal < Base) Base = ordinal;
599 odp = &OrdinalDefinitions[ordinal];
600 if (!(token = GetToken()))
602 fprintf(stderr, "%s:%d: Expected type after ordinal\n", SpecName, Line);
603 return -1;
606 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
607 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
608 break;
610 if (odp->type >= TYPE_NBTYPES)
612 fprintf( stderr,
613 "%s:%d: Expected type after ordinal, found '%s' instead\n",
614 SpecName, Line, token );
615 return -1;
618 if (!(token = GetToken()))
620 fprintf( stderr, "%s:%d: Expected name after type\n", SpecName, Line );
621 return -1;
623 strcpy( odp->name, token );
624 odp->lineno = Line;
626 switch(odp->type)
628 case TYPE_BYTE:
629 case TYPE_WORD:
630 case TYPE_LONG:
631 return ParseVariable( odp );
632 case TYPE_PASCAL_16:
633 case TYPE_PASCAL:
634 case TYPE_REGISTER:
635 case TYPE_STDCALL:
636 case TYPE_CDECL:
637 return ParseExportFunction( odp );
638 case TYPE_ABS:
639 return ParseEquate( odp );
640 case TYPE_RETURN:
641 return ParseReturn( odp );
642 case TYPE_STUB:
643 return ParseStub( odp );
644 case TYPE_VARARGS:
645 return ParseVarargs( odp );
646 case TYPE_EXTERN:
647 return ParseExtern( odp );
648 default:
649 fprintf( stderr, "Should not happen\n" );
650 return -1;
655 /*******************************************************************
656 * ParseTopLevel
658 * Parse a spec file.
660 static int ParseTopLevel(void)
662 char *token;
664 while ((token = GetToken()) != NULL)
666 if (strcmp(token, "name") == 0)
668 strcpy(DLLName, GetToken());
669 strupper(DLLName);
670 if (!DLLFileName[0]) sprintf( DLLFileName, "%s.DLL", DLLName );
672 else if (strcmp(token, "file") == 0)
674 strcpy(DLLFileName, GetToken());
675 strupper(DLLFileName);
677 else if (strcmp(token, "type") == 0)
679 token = GetToken();
680 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
681 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
682 else
684 fprintf(stderr, "%s:%d: Type must be 'win16' or 'win32'\n",
685 SpecName, Line);
686 return -1;
689 else if (strcmp(token, "heap") == 0)
691 token = GetToken();
692 if (!IsNumberString(token))
694 fprintf(stderr, "%s:%d: Expected number after heap\n",
695 SpecName, Line);
696 return -1;
698 DLLHeapSize = atoi(token);
700 else if (IsNumberString(token))
702 int ordinal;
703 int rv;
705 ordinal = atoi(token);
706 if ((rv = ParseOrdinal(ordinal)) < 0)
707 return rv;
709 else
711 fprintf(stderr,
712 "%s:%d: Expected name, id, length or ordinal\n",
713 SpecName, Line);
714 return -1;
718 return 0;
722 /*******************************************************************
723 * StoreVariableCode
725 * Store a list of ints into a byte array.
727 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
729 int i;
731 switch(size)
733 case 1:
734 for (i = 0; i < odp->u.var.n_values; i++)
735 buffer[i] = odp->u.var.values[i];
736 break;
737 case 2:
738 for (i = 0; i < odp->u.var.n_values; i++)
739 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
740 break;
741 case 4:
742 for (i = 0; i < odp->u.var.n_values; i++)
743 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
744 break;
746 return odp->u.var.n_values * size;
750 /*******************************************************************
751 * DumpBytes
753 * Dump a byte stream into the assembly code.
755 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
756 const char *section, const char *label_start )
758 int i;
759 if (section) fprintf( outfile, "\t%s\n", section );
760 if (label_start) fprintf( outfile, "%s:\n", label_start );
761 for (i = 0; i < len; i++)
763 if (!(i & 0x0f)) fprintf( outfile, "\t.byte " );
764 fprintf( outfile, "%d", *data++ );
765 if (i < len - 1)
766 fprintf( outfile, "%c", ((i & 0x0f) != 0x0f) ? ',' : '\n' );
768 fprintf( outfile, "\n" );
772 /*******************************************************************
773 * BuildModule16
775 * Build the in-memory representation of a 16-bit NE module, and dump it
776 * as a byte stream into the assembly code.
778 static int BuildModule16( FILE *outfile, int max_code_offset,
779 int max_data_offset )
781 ORDDEF *odp;
782 int i;
783 char *buffer;
784 NE_MODULE *pModule;
785 SEGTABLEENTRY *pSegment;
786 OFSTRUCT *pFileInfo;
787 BYTE *pstr, *bundle;
788 WORD *pword;
790 /* Module layout:
791 * NE_MODULE Module
792 * OFSTRUCT File information
793 * SEGTABLEENTRY Segment 1 (code)
794 * SEGTABLEENTRY Segment 2 (data)
795 * WORD[2] Resource table (empty)
796 * BYTE[2] Imported names (empty)
797 * BYTE[n] Resident names table
798 * BYTE[n] Entry table
801 buffer = xmalloc( 0x10000 );
803 pModule = (NE_MODULE *)buffer;
804 pModule->magic = IMAGE_OS2_SIGNATURE;
805 pModule->count = 1;
806 pModule->next = 0;
807 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
808 pModule->dgroup = 2;
809 pModule->heap_size = DLLHeapSize;
810 pModule->stack_size = 0;
811 pModule->ip = 0;
812 pModule->cs = 0;
813 pModule->sp = 0;
814 pModule->ss = 0;
815 pModule->seg_count = 2;
816 pModule->modref_count = 0;
817 pModule->nrname_size = 0;
818 pModule->modref_table = 0;
819 pModule->nrname_fpos = 0;
820 pModule->moveable_entries = 0;
821 pModule->alignment = 0;
822 pModule->truetype = 0;
823 pModule->os_flags = NE_OSFLAGS_WINDOWS;
824 pModule->misc_flags = 0;
825 pModule->dlls_to_init = 0;
826 pModule->nrname_handle = 0;
827 pModule->min_swap_area = 0;
828 pModule->expected_version = 0x030a;
829 pModule->pe_module = NULL;
830 pModule->self = 0;
831 pModule->self_loading_sel = 0;
833 /* File information */
835 pFileInfo = (OFSTRUCT *)(pModule + 1);
836 pModule->fileinfo = (int)pFileInfo - (int)pModule;
837 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
838 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
839 + strlen(DLLFileName);
840 strcpy( pFileInfo->szPathName, DLLFileName );
841 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
843 /* Segment table */
845 pSegment = (SEGTABLEENTRY *)pstr;
846 pModule->seg_table = (int)pSegment - (int)pModule;
847 pSegment->filepos = 0;
848 pSegment->size = max_code_offset;
849 pSegment->flags = 0;
850 pSegment->minsize = max_code_offset;
851 pSegment->selector = 0;
852 pSegment++;
854 pModule->dgroup_entry = (int)pSegment - (int)pModule;
855 pSegment->filepos = 0;
856 pSegment->size = max_data_offset;
857 pSegment->flags = NE_SEGFLAGS_DATA;
858 pSegment->minsize = max_data_offset;
859 pSegment->selector = 0;
860 pSegment++;
862 /* Resource table */
864 pword = (WORD *)pSegment;
865 pModule->res_table = (int)pword - (int)pModule;
866 *pword++ = 0;
867 *pword++ = 0;
869 /* Imported names table */
871 pstr = (char *)pword;
872 pModule->import_table = (int)pstr - (int)pModule;
873 *pstr++ = 0;
874 *pstr++ = 0;
876 /* Resident names table */
878 pModule->name_table = (int)pstr - (int)pModule;
879 /* First entry is module name */
880 *pstr = strlen(DLLName );
881 strcpy( pstr + 1, DLLName );
882 pstr += *pstr + 1;
883 *(WORD *)pstr = 0;
884 pstr += sizeof(WORD);
885 /* Store all ordinals */
886 odp = OrdinalDefinitions + 1;
887 for (i = 1; i <= Limit; i++, odp++)
889 if (!odp->name[0]) continue;
890 *pstr = strlen( odp->name );
891 strcpy( pstr + 1, odp->name );
892 strupper( pstr + 1 );
893 pstr += *pstr + 1;
894 *(WORD *)pstr = i;
895 pstr += sizeof(WORD);
897 *pstr++ = 0;
899 /* Entry table */
901 pModule->entry_table = (int)pstr - (int)pModule;
902 bundle = NULL;
903 odp = OrdinalDefinitions + 1;
904 for (i = 1; i <= Limit; i++, odp++)
906 int selector = 0;
908 switch (odp->type)
910 case TYPE_PASCAL:
911 case TYPE_PASCAL_16:
912 case TYPE_REGISTER:
913 case TYPE_RETURN:
914 case TYPE_STUB:
915 selector = 1; /* Code selector */
916 break;
918 case TYPE_BYTE:
919 case TYPE_WORD:
920 case TYPE_LONG:
921 selector = 2; /* Data selector */
922 break;
924 case TYPE_ABS:
925 selector = 0xfe; /* Constant selector */
926 break;
928 default:
929 selector = 0; /* Invalid selector */
930 break;
933 /* create a new bundle if necessary */
934 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
936 bundle = pstr;
937 bundle[0] = 0;
938 bundle[1] = selector;
939 pstr += 2;
942 (*bundle)++;
943 if (selector != 0)
945 *pstr++ = 1;
946 *(WORD *)pstr = odp->offset;
947 pstr += sizeof(WORD);
950 *pstr++ = 0;
952 /* Dump the module content */
954 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
955 ".data", "Module_Start" );
956 return (int)pstr - (int)pModule;
960 /*******************************************************************
961 * BuildSpec32File
963 * Build a Win32 assembly file from a spec file.
965 static int BuildSpec32File( char * specfile, FILE *outfile )
967 ORDDEF *odp;
968 int i, nb_names, nb_stubs;
969 char buffer[1024];
970 unsigned char *args, *p;
972 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
973 fprintf( outfile, "\t.file\t\"%s\"\n", specfile );
974 #ifdef USE_STABS
975 getcwd(buffer, sizeof(buffer));
978 * The stabs help the internal debugger as they are an indication that it
979 * is sensible to step into a thunk/trampoline.
981 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
982 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", specfile);
983 #endif
985 fprintf( outfile, "\t.text\n" );
986 fprintf( outfile, "\t.align 4\n" );
987 fprintf( outfile, "Code_Start:\n" );
989 /* Output code for all register functions */
991 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
993 if (odp->type != TYPE_REGISTER) continue;
994 fprintf( outfile, "\n/* %s.%d (%s) */\n", DLLName, i, odp->name);
995 fprintf( outfile, "\t.align 4\n" );
996 #ifdef USE_STABS
997 fprintf( outfile, ".stabs \"%s_%d:F1\",36,0,%d,%s_%d\n",
998 DLLName, i, odp->lineno, DLLName, i);
999 #endif
1000 fprintf( outfile, "%s_%d:\n", DLLName, i );
1001 #ifdef USE_STABS
1002 fprintf( outfile, ".stabn 68,0,%d,0\n", odp->lineno);
1003 #endif
1004 fprintf( outfile, "\tpushl $" PREFIX "%s\n",odp->u.func.link_name);
1005 fprintf( outfile, "\tjmp " PREFIX "CALL32_Regs\n" );
1008 /* Output code for all stubs functions */
1010 nb_stubs = 0;
1011 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1013 if (odp->type != TYPE_STUB) continue;
1014 fprintf( outfile, "\n/* %s.%d (%s) */\n", DLLName, i, odp->name);
1015 #ifdef USE_STABS
1016 fprintf( outfile, ".stabs \"%s_%d:F1\",36,0,%d,%s_%d\n",
1017 DLLName, i, odp->lineno, DLLName, i);
1018 #endif
1019 fprintf( outfile, "%s_%d:\n", DLLName, i );
1020 #ifdef USE_STABS
1021 fprintf( outfile, ".stabn 68,0,%d,0\n", odp->lineno);
1022 #endif
1023 fprintf( outfile, "\tpushl $Name_%d\n", i );
1024 fprintf( outfile, "\tpushl $%d\n", i );
1025 if (++nb_stubs == 1)
1027 fprintf( outfile, "DoStub:\n" );
1028 fprintf( outfile, "\tpushl $DLLName\n" );
1029 fprintf( outfile, "\tcall " PREFIX "RELAY_Unimplemented32\n" );
1031 else fprintf( outfile, "\tjmp DoStub\n" );
1034 /* Output the DLL functions table */
1036 fprintf( outfile, "\t.text\n" );
1037 fprintf( outfile, "\t.align 4\n" );
1038 fprintf( outfile, "Functions:\n" );
1039 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1041 switch(odp->type)
1043 case TYPE_INVALID:
1044 fprintf( outfile, "\t.long 0\n" );
1045 break;
1046 case TYPE_VARARGS:
1047 fprintf( outfile, "\t.long " PREFIX "%s\n",odp->u.vargs.link_name);
1048 break;
1049 case TYPE_EXTERN:
1050 fprintf( outfile, "\t.long " PREFIX "%s\n", odp->u.ext.link_name );
1051 break;
1052 case TYPE_STDCALL:
1053 case TYPE_CDECL:
1054 fprintf( outfile, "\t.long " PREFIX "%s\n", odp->u.func.link_name);
1055 break;
1056 case TYPE_REGISTER:
1057 case TYPE_STUB:
1058 fprintf( outfile, "\t.long %s_%d\n", DLLName, i );
1059 break;
1060 default:
1061 fprintf(stderr,"build: function type %d not available for Win32\n",
1062 odp->type);
1063 return -1;
1067 /* Output the DLL names table */
1069 fprintf( outfile, "FuncNames:\n" );
1070 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1072 if (odp->type != TYPE_INVALID)
1073 fprintf( outfile, "\t.long Name_%d\n", i );
1076 /* Output the DLL argument types */
1078 fprintf( outfile, "ArgTypes:\n" );
1079 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1081 unsigned int j, mask = 0;
1082 if ((odp->type == TYPE_STDCALL) || (odp->type == TYPE_CDECL))
1083 for (j = 0; odp->u.func.arg_types[j]; j++)
1085 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
1086 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
1088 fprintf( outfile, "\t.long %d\n",mask);
1091 /* Output the DLL ordinals table */
1093 fprintf( outfile, "FuncOrdinals:\n" );
1094 nb_names = 0;
1095 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1097 if (odp->type == TYPE_INVALID) continue;
1098 nb_names++;
1099 /* Some assemblers do not have .word */
1100 fprintf( outfile, "\t.byte %d,%d\n", LOBYTE(i), HIBYTE(i) );
1103 /* Output the DLL names */
1105 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1107 if (odp->type != TYPE_INVALID)
1108 fprintf( outfile, "Name_%d:\t.ascii \"%s\\0\"\n", i, odp->name );
1111 /* Output the DLL functions arguments */
1113 args = p = (unsigned char *)xmalloc( Limit - Base + 1 );
1114 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1116 switch(odp->type)
1118 case TYPE_STDCALL:
1119 *p++ = (unsigned char)strlen(odp->u.func.arg_types);
1120 break;
1121 case TYPE_CDECL:
1122 *p++ = 0x80 | (unsigned char)strlen(odp->u.func.arg_types);
1123 break;
1124 case TYPE_REGISTER:
1125 *p++ = 0xfe;
1126 break;
1127 default:
1128 *p++ = 0xff;
1129 break;
1132 DumpBytes( outfile, args, Limit - Base + 1, NULL, "FuncArgs" );
1134 /* Output the DLL descriptor */
1136 fprintf( outfile, "DLLName:\t.ascii \"%s\\0\"\n", DLLName );
1137 fprintf( outfile, "\t.align 4\n" );
1138 fprintf( outfile, "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1139 fprintf( outfile, PREFIX "%s_Descriptor:\n", DLLName );
1140 fprintf( outfile, "\t.long DLLName\n" ); /* Name */
1141 fprintf( outfile, "\t.long %d\n", Base ); /* Base */
1142 fprintf( outfile, "\t.long %d\n", Limit+1-Base ); /* Number of functions */
1143 fprintf( outfile, "\t.long %d\n", nb_names ); /* Number of names */
1144 fprintf( outfile, "\t.long Functions\n" ); /* Functions */
1145 fprintf( outfile, "\t.long FuncNames\n" ); /* Function names */
1146 fprintf( outfile, "\t.long FuncOrdinals\n" ); /* Function ordinals */
1147 fprintf( outfile, "\t.long FuncArgs\n" ); /* Function arguments */
1148 fprintf( outfile, "\t.long ArgTypes\n" ); /* Function argtypes */
1149 #ifdef USE_STABS
1150 fprintf( outfile, "\t.text\n");
1151 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
1152 fprintf( outfile, ".Letext:\n");
1153 #endif
1154 return 0;
1158 /*******************************************************************
1159 * BuildSpec16File
1161 * Build a Win16 assembly file from a spec file.
1163 static int BuildSpec16File( char * specfile, FILE *outfile )
1165 ORDDEF *odp;
1166 int i;
1167 int code_offset, data_offset, module_size;
1168 unsigned char *data;
1170 data = (unsigned char *)xmalloc( 0x10000 );
1171 memset( data, 0, 16 );
1172 data_offset = 16;
1174 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
1175 fprintf( outfile, "\t.text\n" );
1176 fprintf( outfile, "Code_Start:\n" );
1177 code_offset = 0;
1179 odp = OrdinalDefinitions;
1180 for (i = 0; i <= Limit; i++, odp++)
1182 switch (odp->type)
1184 case TYPE_INVALID:
1185 odp->offset = 0xffff;
1186 break;
1188 case TYPE_ABS:
1189 odp->offset = LOWORD(odp->u.abs.value);
1190 break;
1192 case TYPE_BYTE:
1193 odp->offset = data_offset;
1194 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1195 break;
1197 case TYPE_WORD:
1198 odp->offset = data_offset;
1199 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1200 break;
1202 case TYPE_LONG:
1203 odp->offset = data_offset;
1204 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1205 break;
1207 case TYPE_RETURN:
1208 fprintf( outfile,"/* %s.%d */\n", DLLName, i);
1209 fprintf( outfile,"\tmovw $%d,%%ax\n",LOWORD(odp->u.ret.ret_value));
1210 fprintf( outfile,"\tmovw $%d,%%dx\n",HIWORD(odp->u.ret.ret_value));
1211 fprintf( outfile,"\t.byte 0x66\n");
1212 if (odp->u.ret.arg_size != 0)
1213 fprintf( outfile, "\tlret $%d\n\n", odp->u.ret.arg_size);
1214 else
1216 fprintf( outfile, "\tlret\n");
1217 fprintf( outfile, "\tnop\n");
1218 fprintf( outfile, "\tnop\n\n");
1220 odp->offset = code_offset;
1221 code_offset += 12; /* Assembly code is 12 bytes long */
1222 break;
1224 case TYPE_REGISTER:
1225 case TYPE_PASCAL:
1226 case TYPE_PASCAL_16:
1227 case TYPE_STUB:
1228 fprintf( outfile, "/* %s.%d */\n", DLLName, i);
1229 fprintf( outfile, "\tpushw %%bp\n" );
1230 fprintf( outfile, "\tpushl $" PREFIX "%s\n",odp->u.func.link_name);
1231 /* FreeBSD does not understand lcall, so do it the hard way */
1232 fprintf( outfile, "\t.byte 0x9a\n" );
1233 fprintf( outfile, "\t.long " PREFIX "CallFrom16_%s_%s\n",
1234 (odp->type == TYPE_REGISTER) ? "regs" :
1235 (odp->type == TYPE_PASCAL) ? "long" : "word",
1236 odp->u.func.arg_types );
1237 fprintf( outfile, "\t.long 0x%08lx\n",
1238 MAKELONG( Code_Selector, 0x9090 /* nop ; nop */ ) );
1239 odp->offset = code_offset;
1240 code_offset += 16; /* Assembly code is 16 bytes long */
1241 break;
1243 default:
1244 fprintf(stderr,"build: function type %d not available for Win16\n",
1245 odp->type);
1246 return -1;
1250 if (!code_offset) /* Make sure the code segment is not empty */
1252 fprintf( outfile, "\t.byte 0\n" );
1253 code_offset++;
1256 /* Output data segment */
1258 DumpBytes( outfile, data, data_offset, NULL, "Data_Start" );
1260 /* Build the module */
1262 module_size = BuildModule16( outfile, code_offset, data_offset );
1264 /* Output the DLL descriptor */
1266 fprintf( outfile, "\t.text\n" );
1267 fprintf( outfile, "DLLName:\t.ascii \"%s\\0\"\n", DLLName );
1268 fprintf( outfile, "\t.align 4\n" );
1269 fprintf( outfile, "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1270 fprintf( outfile, PREFIX "%s_Descriptor:\n", DLLName );
1271 fprintf( outfile, "\t.long DLLName\n" ); /* Name */
1272 fprintf( outfile, "\t.long Module_Start\n" ); /* Module start */
1273 fprintf( outfile, "\t.long %d\n", module_size ); /* Module size */
1274 fprintf( outfile, "\t.long Code_Start\n" ); /* Code start */
1275 fprintf( outfile, "\t.long Data_Start\n" ); /* Data start */
1276 return 0;
1280 /*******************************************************************
1281 * BuildSpecFile
1283 * Build an assembly file from a spec file.
1285 static int BuildSpecFile( FILE *outfile, char *specname )
1287 SpecName = specname;
1288 SpecFp = fopen( specname, "r");
1289 if (SpecFp == NULL)
1291 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1292 return -1;
1295 if (ParseTopLevel() < 0) return -1;
1297 switch(SpecType)
1299 case SPEC_WIN16:
1300 return BuildSpec16File( specname, outfile );
1301 case SPEC_WIN32:
1302 return BuildSpec32File( specname, outfile );
1303 default:
1304 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1305 return -1;
1310 /*******************************************************************
1311 * TransferArgs16To32
1313 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1314 * The 16-bit stack layout is:
1315 * ... ...
1316 * (bp+8) arg2
1317 * (bp+6) arg1
1318 * (bp+4) cs
1319 * (bp+2) ip
1320 * (bp) bp
1322 static int TransferArgs16To32( FILE *outfile, char *args )
1324 int i, pos16, pos32;
1326 /* Save ebx first */
1328 fprintf( outfile, "\tpushl %%ebx\n" );
1330 /* Get the 32-bit stack pointer */
1332 fprintf( outfile, "\tmovl " PREFIX "CALLTO16_Saved32_esp,%%ebx\n" );
1334 /* Copy the arguments */
1336 pos16 = 6; /* skip bp and return address */
1337 pos32 = 0;
1339 for (i = strlen(args); i > 0; i--)
1341 pos32 -= 4;
1342 switch(args[i-1])
1344 case 'w': /* word */
1345 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1346 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1347 pos16 += 2;
1348 break;
1350 case 's': /* s_word */
1351 fprintf( outfile, "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1352 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1353 pos16 += 2;
1354 break;
1356 case 'l': /* long or segmented pointer */
1357 case 'T': /* segmented pointer to null-terminated string */
1358 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", pos16 );
1359 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1360 pos16 += 4;
1361 break;
1363 case 'p': /* linear pointer */
1364 case 't': /* linear pointer to null-terminated string */
1365 /* Get the selector */
1366 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1367 /* Get the selector base */
1368 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1369 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1370 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1371 /* Add the offset */
1372 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1373 fprintf( outfile, "\taddl %%eax,%d(%%ebx)\n", pos32 );
1374 pos16 += 4;
1375 break;
1377 default:
1378 fprintf( stderr, "Unknown arg type '%c'\n", args[i-1] );
1382 /* Restore ebx */
1384 fprintf( outfile, "\tpopl %%ebx\n" );
1386 return pos16 - 6; /* Return the size of the 16-bit args */
1390 /*******************************************************************
1391 * BuildContext16
1393 * Build the context structure on the 32-bit stack.
1395 static void BuildContext16( FILE *outfile )
1397 /* Save ebx first */
1399 fprintf( outfile, "\tpushl %%ebx\n" );
1401 /* Get the 32-bit stack pointer */
1403 fprintf( outfile, "\tmovl " PREFIX "CALLTO16_Saved32_esp,%%ebx\n" );
1405 /* Store the registers */
1407 fprintf( outfile, "\tpopl %d(%%ebx)\n", /* Get ebx from stack*/
1408 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1409 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1410 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1411 fprintf( outfile, "\tmovl %%ecx,%d(%%ebx)\n",
1412 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1413 fprintf( outfile, "\tmovl %%edx,%d(%%ebx)\n",
1414 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1415 fprintf( outfile, "\tmovl %%esi,%d(%%ebx)\n",
1416 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1417 fprintf( outfile, "\tmovl %%edi,%d(%%ebx)\n",
1418 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1420 fprintf( outfile, "\tmovzwl -10(%%ebp),%%eax\n" ); /* Get %ds from stack*/
1421 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1422 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1423 fprintf( outfile, "\tmovzwl -6(%%ebp),%%eax\n" ); /* Get %es from stack*/
1424 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1425 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1426 fprintf( outfile, "\tpushfl\n" );
1427 fprintf( outfile, "\tpopl %d(%%ebx)\n",
1428 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1429 fprintf( outfile, "\tmovl -16(%%ebp),%%eax\n" ); /* Get %ebp from stack */
1430 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1431 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1432 fprintf( outfile, "\tmovzwl 2(%%ebp),%%eax\n" ); /* Get %ip from stack */
1433 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1434 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1435 fprintf( outfile, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1436 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1437 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1438 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
1439 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1440 CONTEXTOFFSET(SegFs) - sizeof(CONTEXT) );
1441 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1442 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1443 CONTEXTOFFSET(SegGs) - sizeof(CONTEXT) );
1444 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1445 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1446 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1447 #if 0
1448 fprintf( outfile, "\tfsave %d(%%ebx)\n",
1449 CONTEXTOFFSET(FloatSave) - sizeof(CONTEXT) );
1450 #endif
1454 /*******************************************************************
1455 * RestoreContext16
1457 * Restore the registers from the context structure.
1458 * %edx must point to the 32-bit stack top.
1460 static void RestoreContext16( FILE *outfile )
1462 /* Get the 32-bit stack pointer */
1464 fprintf( outfile, "\tmovl %%edx,%%ebx\n" );
1466 /* Remove everything up to the return address from the 16-bit stack */
1468 fprintf( outfile, "\taddl $22,%%esp\n" );
1470 /* Restore the registers */
1472 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n",
1473 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1474 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n",
1475 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1476 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n",
1477 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1478 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n",
1479 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1480 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n",
1481 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1482 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new cs */
1483 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1484 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new ip */
1485 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1486 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new ds */
1487 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1488 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new es */
1489 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1490 fprintf( outfile, "\tpushl %d(%%ebx)\n",
1491 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1492 fprintf( outfile, "\tpopfl\n" );
1493 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1494 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1495 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n",
1496 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1497 fprintf( outfile, "\tpopw %%es\n" ); /* Set es */
1498 fprintf( outfile, "\tpopw %%ds\n" ); /* Set ds */
1502 /*******************************************************************
1503 * BuildCallFrom16Func
1505 * Build a 16-bit-to-Wine callback function. The syntax of the function
1506 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1507 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1508 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1509 * 'T'=segmented pointer to null-terminated string).
1510 * For register functions, the arguments are ignored, but they are still
1511 * removed from the stack upon return.
1513 * Stack layout upon entry to the callback function:
1514 * ... ...
1515 * (sp+18) word first 16-bit arg
1516 * (sp+16) word cs
1517 * (sp+14) word ip
1518 * (sp+12) word bp
1519 * (sp+8) long 32-bit entry point (used to store edx)
1520 * (sp+6) word high word of cs (always 0, used to store es)
1521 * (sp+4) word low word of cs of 16-bit entry point
1522 * (sp+2) word high word of ip (always 0, used to store ds)
1523 * (sp) word low word of ip of 16-bit entry point
1525 * Added on the stack:
1526 * (sp-4) long ebp
1527 * (sp-6) word saved previous sp
1528 * (sp-8) word saved previous ss
1530 static void BuildCallFrom16Func( FILE *outfile, char *profile )
1532 int argsize = 0;
1533 int short_ret = 0;
1534 int reg_func = 0;
1535 char *args = profile + 5;
1537 /* Parse function type */
1539 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1540 else if (!strncmp( "regs_", profile, 5 )) reg_func = 1;
1541 else if (strncmp( "long_", profile, 5 ))
1543 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1544 return;
1547 /* Function header */
1549 fprintf( outfile, "\n\t.align 4\n" );
1550 #ifdef USE_STABS
1551 fprintf( outfile, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX "CallFrom16_%s\n",
1552 profile, profile);
1553 #endif
1554 fprintf( outfile, "\t.globl " PREFIX "CallFrom16_%s\n", profile );
1555 fprintf( outfile, PREFIX "CallFrom16_%s:\n", profile );
1557 /* Setup bp to point to its copy on the stack */
1559 fprintf( outfile, "\tpushl %%ebp\n" ); /* Save the full 32-bit ebp */
1560 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1561 fprintf( outfile, "\taddw $16,%%bp\n" );
1563 /* Save 16-bit ds and es */
1565 /* Stupid FreeBSD assembler doesn't know these either */
1566 /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1567 fprintf( outfile, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1568 /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1569 fprintf( outfile, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1571 /* Restore 32-bit ds and es */
1573 fprintf( outfile, "\tpushl $0x%04x%04x\n", Data_Selector, Data_Selector );
1574 fprintf( outfile, "\tpopw %%ds\n" );
1575 fprintf( outfile, "\tpopw %%es\n" );
1578 /* Save the 16-bit stack */
1580 fprintf( outfile, "\tpushl " PREFIX "IF1632_Saved16_ss_sp\n" );
1581 #ifdef __svr4__
1582 fprintf( outfile,"\tdata16\n");
1583 #endif
1584 fprintf( outfile, "\tmovw %%ss," PREFIX "IF1632_Saved16_ss_sp+2\n" );
1585 fprintf( outfile, "\tmovw %%sp," PREFIX "IF1632_Saved16_ss_sp\n" );
1587 /* Transfer the arguments */
1589 if (reg_func) BuildContext16( outfile );
1590 else if (*args) argsize = TransferArgs16To32( outfile, args );
1592 /* Get the address of the API function */
1594 fprintf( outfile, "\tmovl -4(%%ebp),%%eax\n" );
1596 /* If necessary, save %edx over the API function address */
1598 if (!reg_func && short_ret)
1599 fprintf( outfile, "\tmovl %%edx,-4(%%ebp)\n" );
1601 /* Switch to the 32-bit stack */
1603 fprintf( outfile, "\tmovl " PREFIX "CALLTO16_Saved32_esp,%%ebp\n" );
1604 fprintf( outfile, "\tpushw %%ds\n" );
1605 fprintf( outfile, "\tpopw %%ss\n" );
1606 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1607 reg_func ? sizeof(CONTEXT) : 4 * strlen(args) );
1608 if (reg_func) /* Push the address of the context struct */
1609 fprintf( outfile, "\tpushl %%esp\n" );
1611 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1613 fprintf( outfile, "\taddl $%d,%%ebp\n", STRUCTOFFSET(STACK32FRAME,ebp) );
1615 /* Print the debug information before the call */
1617 if (debugging)
1619 fprintf( outfile, "\tpushl %%eax\n" );
1620 fprintf( outfile, "\tpushl $Profile_%s\n", profile );
1621 fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1622 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n" );
1623 fprintf( outfile, "\tpopl %%eax\n" );
1624 fprintf( outfile, "\tpopl %%eax\n" );
1625 fprintf( outfile, "\tpopl %%eax\n" );
1628 /* Call the entry point */
1630 fprintf( outfile, "\tcall %%eax\n" );
1632 /* Print the debug information after the call */
1634 if (debugging)
1636 if (reg_func)
1638 /* Push again the address of the context struct in case */
1639 /* it has been removed by an stdcall function */
1640 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1641 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME,ebp) );
1642 fprintf( outfile, "\tpushl %%esp\n" );
1644 fprintf( outfile, "\tpushl %%eax\n" );
1645 fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1646 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n" );
1647 fprintf( outfile, "\tpopl %%eax\n" );
1648 fprintf( outfile, "\tpopl %%eax\n" );
1651 /* Restore the value of the saved 32-bit stack pointer */
1653 fprintf( outfile, "\tleal -%d(%%ebp),%%edx\n",
1654 STRUCTOFFSET(STACK32FRAME,ebp) );
1655 fprintf( outfile, "movl %%edx," PREFIX "CALLTO16_Saved32_esp\n" );
1657 /* Restore the 16-bit stack */
1659 #ifdef __svr4__
1660 fprintf( outfile, "\tdata16\n");
1661 #endif
1662 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp+2,%%ss\n" );
1663 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp,%%sp\n" );
1664 fprintf( outfile, "\tpopl " PREFIX "IF1632_Saved16_ss_sp\n" );
1666 if (reg_func)
1668 /* Calc the arguments size */
1669 while (*args)
1671 switch(*args)
1673 case 'w':
1674 case 's':
1675 argsize += 2;
1676 break;
1677 case 'p':
1678 case 't':
1679 case 'l':
1680 case 'T':
1681 argsize += 4;
1682 break;
1683 default:
1684 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1686 args++;
1689 /* Restore registers from the context structure */
1690 RestoreContext16( outfile );
1692 else
1694 /* Restore high 16 bits of ebp */
1695 fprintf( outfile, "\tpopl %%ebp\n" );
1697 /* Restore ds and es */
1698 fprintf( outfile, "\tincl %%esp\n" ); /* Remove ip */
1699 fprintf( outfile, "\tincl %%esp\n" );
1700 fprintf( outfile, "\tpopl %%edx\n" ); /* Remove cs and ds */
1701 fprintf( outfile, "\tmovw %%dx,%%ds\n" ); /* and restore ds */
1702 fprintf( outfile, "\tpopw %%es\n" ); /* Restore es */
1704 if (short_ret) fprintf( outfile, "\tpopl %%edx\n" ); /* Restore edx */
1705 else
1707 /* Get the return value into dx:ax */
1708 fprintf( outfile, "\tmovl %%eax,%%edx\n" );
1709 fprintf( outfile, "\tshrl $16,%%edx\n" );
1710 /* Remove API entry point */
1711 fprintf( outfile, "\taddl $4,%%esp\n" );
1714 /* Restore low 16 bits of ebp */
1715 fprintf( outfile, "\tpopw %%bp\n" );
1718 /* Remove the arguments and return */
1720 if (argsize)
1722 fprintf( outfile, "\t.byte 0x66\n" );
1723 fprintf( outfile, "\tlret $%d\n", argsize );
1725 else
1727 fprintf( outfile, "\t.byte 0x66\n" );
1728 fprintf( outfile, "\tlret\n" );
1733 /*******************************************************************
1734 * BuildCallTo16Func
1736 * Build a Wine-to-16-bit callback function.
1738 * Stack frame of the callback function:
1739 * ... ...
1740 * (ebp+16) arg2
1741 * (ebp+12) arg1
1742 * (ebp+8) func to call
1743 * (ebp+4) return address
1744 * (ebp) previous ebp
1746 * Prototypes for the CallTo16 functions:
1747 * extern WINAPI WORD CallTo16_word_xxx( FARPROC16 func, args... );
1748 * extern WINAPI LONG CallTo16_long_xxx( FARPROC16 func, args... );
1749 * extern WINAPI void CallTo16_regs_{short,long}( const CONTEXT *context );
1751 static void BuildCallTo16Func( FILE *outfile, char *profile )
1753 int short_ret = 0;
1754 int reg_func = 0;
1755 char *args = profile + 5;
1757 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1758 else if (!strncmp( "regs_", profile, 5 )) reg_func = 1;
1759 else if (strncmp( "long_", profile, 5 ))
1761 fprintf( stderr, "Invalid function name '%s'.\n", profile );
1762 exit(1);
1765 /* Function header */
1767 fprintf( outfile, "\n\t.align 4\n" );
1768 #ifdef USE_STABS
1769 fprintf( outfile, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX "CallTo16_%s\n",
1770 profile, profile);
1771 #endif
1772 fprintf( outfile, "\t.globl " PREFIX "CallTo16_%s\n", profile );
1773 fprintf( outfile, PREFIX "CallTo16_%s:\n", profile );
1775 /* Entry code */
1777 fprintf( outfile, "\tpushl %%ebp\n" );
1778 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
1780 /* Call the actual CallTo16 routine (simulate a lcall) */
1782 fprintf( outfile, "\tpushw $0\n" );
1783 fprintf( outfile, "\tpushw %%cs\n" );
1784 fprintf( outfile, "\tcall do_callto16_%s\n", profile );
1786 /* Exit code */
1788 /* FIXME: this is a hack because of task.c */
1789 if (!strcmp( profile, "word_" ))
1791 fprintf( outfile, ".globl " PREFIX "CALLTO16_Restore\n" );
1792 fprintf( outfile, PREFIX "CALLTO16_Restore:\n" );
1794 fprintf( outfile, "\tpopl %%ebp\n" );
1795 fprintf( outfile, "\tret $%d\n", strlen(args) + 1 );
1797 /* Start of the actual CallTo16 routine */
1799 /* Save the 32-bit registers */
1801 fprintf( outfile, "do_callto16_%s:\n", profile );
1802 fprintf( outfile, "\tpushl %%ebx\n" );
1803 fprintf( outfile, "\tpushl %%ecx\n" );
1804 fprintf( outfile, "\tpushl %%edx\n" );
1805 fprintf( outfile, "\tpushl %%esi\n" );
1806 fprintf( outfile, "\tpushl %%edi\n" );
1808 /* Save the 32-bit stack */
1810 fprintf( outfile, "\tmovl %%esp," PREFIX "CALLTO16_Saved32_esp\n" );
1811 fprintf( outfile, "\tmovl %%ebp,%%ebx\n" );
1813 /* Print debugging info */
1815 if (debugging)
1817 /* Push the address of the first argument */
1818 fprintf( outfile, "\tleal 8(%%ebp),%%eax\n" );
1819 fprintf( outfile, "\tpushl $%d\n", reg_func ? -1 : strlen(args) );
1820 fprintf( outfile, "\tpushl %%eax\n" );
1821 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
1822 fprintf( outfile, "\tpopl %%eax\n" );
1823 fprintf( outfile, "\tpopl %%eax\n" );
1826 if (reg_func)
1828 /* Switch to the 16-bit stack, saving the current %%esp, */
1829 /* and adding the specified offset to the new sp */
1830 fprintf( outfile, "\tmovzwl " PREFIX "IF1632_Saved16_ss_sp,%%edx\n" );
1831 fprintf( outfile, "\tleal -4(%%edx),%%edx\n" );
1832 fprintf( outfile, "\tmovl 12(%%ebx),%%eax\n" ); /* Get the offset */
1833 fprintf( outfile, "\taddl %%edx,%%eax\n" );
1834 #ifdef __svr4__
1835 fprintf( outfile,"\tdata16\n");
1836 #endif
1837 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp+2,%%ss\n" );
1838 fprintf( outfile, "\txchgl %%esp,%%eax\n" );
1839 fprintf( outfile, "\t.byte 0x36\n" /* %ss: */ );
1840 fprintf( outfile, "\tmovl %%eax,0(%%edx)\n" );
1842 /* Get the registers. ebx is handled later on. */
1844 fprintf( outfile, "\tmovl 8(%%ebx),%%ebx\n" );
1845 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs) );
1846 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1847 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp) );
1848 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax) );
1849 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx) );
1850 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx) );
1851 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi) );
1852 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi) );
1854 /* Push the return address
1855 * With _short suffix, we push 16:16 address (normal lret)
1856 * With _long suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
1858 if (!strncmp(profile,"regs_short",10))
1859 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_regs\n" );
1860 else
1862 fprintf( outfile, "\tpushw $0\n" );
1863 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_regs+2\n" );
1864 fprintf( outfile, "\tpushw $0\n" );
1865 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_regs\n" );
1868 /* Push the called routine address */
1870 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
1871 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
1873 /* Get the 16-bit ds */
1875 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
1876 /* Get ebx from the 32-bit stack */
1877 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(Ebx) );
1878 fprintf( outfile, "\tpopw %%ds\n" );
1880 else /* not a register function */
1882 int pos = 12; /* first argument position */
1884 /* Switch to the 16-bit stack, saving the current %%esp */
1885 fprintf( outfile, "\tmovl %%esp,%%eax\n" );
1886 #ifdef __svr4__
1887 fprintf( outfile,"\tdata16\n");
1888 #endif
1889 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp+2,%%ss\n" );
1890 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp,%%sp\n" );
1891 fprintf( outfile, "\tpushl %%eax\n" );
1893 /* Make %bp point to the previous stackframe (built by CallFrom16) */
1894 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1895 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n",
1896 STRUCTOFFSET(STACK16FRAME,bp) + 4 /* for saved %%esp */ );
1898 /* Transfer the arguments */
1900 while (*args)
1902 switch(*args++)
1904 case 'w': /* word */
1905 fprintf( outfile, "\tpushw %d(%%ebx)\n", pos );
1906 break;
1907 case 'l': /* long */
1908 fprintf( outfile, "\tpushl %d(%%ebx)\n", pos );
1909 break;
1910 default:
1911 fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
1912 args[-1] );
1914 pos += 4;
1917 /* Push the return address */
1919 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_%s\n",
1920 short_ret ? "word" : "long" );
1922 /* Push the called routine address */
1924 fprintf( outfile, "\tpushl 8(%%ebx)\n" );
1926 /* Set %ds and %es (and %ax just in case) equal to %ss */
1928 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1929 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
1930 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1933 /* Jump to the called routine */
1935 fprintf( outfile, "\t.byte 0x66\n" );
1936 fprintf( outfile, "\tlret\n" );
1940 /*******************************************************************
1941 * BuildRet16Func
1943 * Build the return code for 16-bit callbacks
1945 static void BuildRet16Func( FILE *outfile )
1947 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_word\n" );
1948 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_long\n" );
1949 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_regs\n" );
1951 fprintf( outfile, PREFIX "CALLTO16_Ret_word:\n" );
1952 fprintf( outfile, "\txorl %%edx,%%edx\n" );
1954 /* Remove the arguments just in case */
1956 fprintf( outfile, PREFIX "CALLTO16_Ret_long:\n" );
1957 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1958 STRUCTOFFSET(STACK16FRAME,bp) + 4 /* for saved %%esp */ );
1960 /* Put return value into %eax */
1962 fprintf( outfile, PREFIX "CALLTO16_Ret_regs:\n" );
1963 fprintf( outfile, "\tshll $16,%%edx\n" );
1964 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
1965 fprintf( outfile, "\tmovl %%edx,%%eax\n" );
1967 /* Restore 32-bit segment registers */
1969 fprintf( outfile, "\tpopl %%ecx\n" ); /* Get the saved %%esp */
1970 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
1971 #ifdef __svr4__
1972 fprintf( outfile, "\tdata16\n");
1973 #endif
1974 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
1975 #ifdef __svr4__
1976 fprintf( outfile, "\tdata16\n");
1977 #endif
1978 fprintf( outfile, "\tmovw %%bx,%%es\n" );
1980 /* Restore the 32-bit stack */
1982 #ifdef __svr4__
1983 fprintf( outfile, "\tdata16\n");
1984 #endif
1985 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
1986 fprintf( outfile, "\tmovl %%ecx,%%esp\n" );
1988 /* Restore the 32-bit registers */
1990 fprintf( outfile, "\tpopl %%edi\n" );
1991 fprintf( outfile, "\tpopl %%esi\n" );
1992 fprintf( outfile, "\tpopl %%edx\n" );
1993 fprintf( outfile, "\tpopl %%ecx\n" );
1994 fprintf( outfile, "\tpopl %%ebx\n" );
1996 /* Return to caller */
1998 fprintf( outfile, "\tlret\n" );
2000 /* Declare the return address variables */
2002 fprintf( outfile, "\t.data\n" );
2003 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_word\n" );
2004 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_long\n" );
2005 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_regs\n" );
2006 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Saved32_esp\n" );
2007 fprintf( outfile, PREFIX "CALLTO16_RetAddr_word:\t.long 0\n" );
2008 fprintf( outfile, PREFIX "CALLTO16_RetAddr_long:\t.long 0\n" );
2009 fprintf( outfile, PREFIX "CALLTO16_RetAddr_regs:\t.long 0\n" );
2010 fprintf( outfile, PREFIX "CALLTO16_Saved32_esp:\t.long 0\n" );
2011 fprintf( outfile, "\t.text\n" );
2015 /*******************************************************************
2016 * BuildCallTo32LargeStack
2018 * Build the function used to switch to the original 32-bit stack
2019 * before calling a 32-bit function from 32-bit code. This is used for
2020 * functions that need a large stack, like X bitmaps functions.
2022 * The generated function has the following prototype:
2023 * int xxx( int (*func)(), void *arg );
2025 * The pointer to the function can be retrieved by calling CALL32_Init,
2026 * which also takes care of saving the current 32-bit stack pointer.
2028 * Stack layout:
2029 * ... ...
2030 * (ebp+12) arg
2031 * (ebp+8) func
2032 * (ebp+4) ret addr
2033 * (ebp) ebp
2035 static void BuildCallTo32LargeStack( FILE *outfile )
2037 /* Initialization function */
2039 fprintf( outfile, "\n\t.align 4\n" );
2040 #ifdef USE_STABS
2041 fprintf( outfile, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX "CALL32_Init\n");
2042 #endif
2043 fprintf( outfile, "\t.globl " PREFIX "CALL32_Init\n" );
2044 fprintf( outfile, PREFIX "CALL32_Init:\n" );
2045 fprintf( outfile, "\tleal -256(%%esp),%%eax\n" );
2046 fprintf( outfile, "\tmovl %%eax,CALL32_Original32_esp\n" );
2047 fprintf( outfile, "\tmovl $CALL32_LargeStack,%%eax\n" );
2048 fprintf( outfile, "\tret\n" );
2050 /* Function header */
2052 fprintf( outfile, "\n\t.align 4\n" );
2053 #ifdef USE_STABS
2054 fprintf( outfile, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2055 #endif
2056 fprintf( outfile, "CALL32_LargeStack:\n" );
2058 /* Entry code */
2060 fprintf( outfile, "\tpushl %%ebp\n" );
2061 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2063 /* Switch to the original 32-bit stack pointer */
2065 fprintf( outfile, "\tmovl CALL32_Original32_esp, %%esp\n" );
2067 /* Transfer the argument and call the function */
2069 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2070 fprintf( outfile, "\tcall 8(%%ebp)\n" );
2072 /* Restore registers and return */
2074 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2075 fprintf( outfile, "\tpopl %%ebp\n" );
2076 fprintf( outfile, "\tret\n" );
2078 /* Data */
2080 fprintf( outfile, "\t.data\n" );
2081 fprintf( outfile, "CALL32_Original32_esp:\t.long 0\n" );
2082 fprintf( outfile, "\t.text\n" );
2086 /*******************************************************************
2087 * BuildCallFrom32Regs
2089 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2090 * 'args' is the number of dword arguments.
2092 * Stack layout:
2093 * ... ...
2094 * (esp+208) ret addr (or relay addr when debugging_relay is on)
2095 * (esp+204) entry point
2096 * (esp+0) CONTEXT struct
2098 static void BuildCallFrom32Regs( FILE *outfile )
2100 /* Function header */
2102 fprintf( outfile, "\n\t.align 4\n" );
2103 #ifdef USE_STABS
2104 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
2105 #endif
2106 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
2107 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
2109 /* Build the context structure */
2111 fprintf( outfile, "\tpushw $0\n" );
2112 fprintf( outfile, "\tpushw %%ss\n" );
2113 fprintf( outfile, "\tpushl %%eax\n" ); /* %esp place holder */
2114 fprintf( outfile, "\tpushfl\n" );
2115 fprintf( outfile, "\tpushw $0\n" );
2116 fprintf( outfile, "\tpushw %%cs\n" );
2117 fprintf( outfile, "\tpushl 20(%%esp)\n" ); /* %eip at time of call */
2118 fprintf( outfile, "\tpushl %%ebp\n" );
2120 fprintf( outfile, "\tpushl %%eax\n" );
2121 fprintf( outfile, "\tpushl %%ecx\n" );
2122 fprintf( outfile, "\tpushl %%edx\n" );
2123 fprintf( outfile, "\tpushl %%ebx\n" );
2124 fprintf( outfile, "\tpushl %%esi\n" );
2125 fprintf( outfile, "\tpushl %%edi\n" );
2127 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2128 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2129 fprintf( outfile, "\tpushl %%eax\n" );
2130 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2131 fprintf( outfile, "\tpushl %%eax\n" );
2132 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2133 fprintf( outfile, "\tpushl %%eax\n" );
2134 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2135 fprintf( outfile, "\tpushl %%eax\n" );
2137 fprintf( outfile, "\tleal -%d(%%esp),%%esp\n",
2138 sizeof(FLOATING_SAVE_AREA) + 6 * sizeof(DWORD) /* DR regs */ );
2139 fprintf( outfile, "\tpushl $0x0001001f\n" ); /* ContextFlags */
2141 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2143 fprintf( outfile, "\tleal %d(%%esp),%%eax\n",
2144 sizeof(CONTEXT) + 4 ); /* %esp at time of call */
2145 fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Esp) );
2147 fprintf( outfile, "\tcall " PREFIX "RELAY_CallFrom32Regs\n" );
2149 /* Restore the context structure */
2151 fprintf( outfile, "\tfrstor %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2153 /* Store %eip value onto the new stack */
2155 fprintf( outfile, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Eip) );
2156 fprintf( outfile, "\tmovl %d(%%esp),%%ebx\n", CONTEXTOFFSET(Esp) );
2157 fprintf( outfile, "\tmovl %%eax,0(%%ebx)\n" );
2159 /* Restore all registers */
2161 fprintf( outfile, "\tleal %d(%%esp),%%esp\n",
2162 sizeof(FLOATING_SAVE_AREA) + 7 * sizeof(DWORD) );
2163 fprintf( outfile, "\tpopl %%eax\n" );
2164 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2165 fprintf( outfile, "\tpopl %%eax\n" );
2166 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2167 fprintf( outfile, "\tpopl %%eax\n" );
2168 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2169 fprintf( outfile, "\tpopl %%eax\n" );
2170 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2172 fprintf( outfile, "\tpopl %%edi\n" );
2173 fprintf( outfile, "\tpopl %%esi\n" );
2174 fprintf( outfile, "\tpopl %%ebx\n" );
2175 fprintf( outfile, "\tpopl %%edx\n" );
2176 fprintf( outfile, "\tpopl %%ecx\n" );
2177 fprintf( outfile, "\tpopl %%eax\n" );
2178 fprintf( outfile, "\tpopl %%ebp\n" );
2179 fprintf( outfile, "\tleal 8(%%esp),%%esp\n" ); /* skip %eip and %cs */
2180 fprintf( outfile, "\tpopfl\n" );
2181 fprintf( outfile, "\tpopl %%esp\n" );
2182 fprintf( outfile, "\tret\n" );
2186 /*******************************************************************
2187 * BuildSpec
2189 * Build the spec files
2191 static int BuildSpec( FILE *outfile, int argc, char *argv[] )
2193 int i;
2194 for (i = 2; i < argc; i++)
2195 if (BuildSpecFile( outfile, argv[i] ) < 0) return -1;
2196 return 0;
2200 /*******************************************************************
2201 * BuildCallFrom16
2203 * Build the 16-bit-to-Wine callbacks
2205 static int BuildCallFrom16( FILE *outfile, char * outname, int argc, char *argv[] )
2207 int i;
2208 char buffer[1024];
2210 /* File header */
2212 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2213 fprintf( outfile, "\t.text\n" );
2215 #ifdef USE_STABS
2216 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2217 getcwd(buffer, sizeof(buffer));
2220 * The stabs help the internal debugger as they are an indication that it
2221 * is sensible to step into a thunk/trampoline.
2223 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2224 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2225 fprintf( outfile, "\t.text\n" );
2226 fprintf( outfile, "\t.align 4\n" );
2227 fprintf( outfile, "Code_Start:\n\n" );
2228 #endif
2230 /* Build the callback functions */
2232 for (i = 2; i < argc; i++) BuildCallFrom16Func( outfile, argv[i] );
2234 /* Output the argument debugging strings */
2236 if (debugging)
2238 fprintf( outfile, "/* Argument strings */\n" );
2239 for (i = 2; i < argc; i++)
2241 fprintf( outfile, "Profile_%s:\n", argv[i] );
2242 fprintf( outfile, "\t.ascii \"%s\\0\"\n", argv[i] + 5 );
2246 #ifdef USE_STABS
2247 fprintf( outfile, "\t.text\n");
2248 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2249 fprintf( outfile, ".Letext:\n");
2250 #endif
2252 return 0;
2256 /*******************************************************************
2257 * BuildCallTo16
2259 * Build the Wine-to-16-bit callbacks
2261 static int BuildCallTo16( FILE *outfile, char * outname, int argc, char *argv[] )
2263 char buffer[1024];
2264 FILE *infile;
2266 if (argc > 2)
2268 infile = fopen( argv[2], "r" );
2269 if (!infile)
2271 perror( argv[2] );
2272 exit( 1 );
2275 else infile = stdin;
2277 /* File header */
2279 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2280 fprintf( outfile, "\t.text\n" );
2282 #ifdef USE_STABS
2283 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2284 getcwd(buffer, sizeof(buffer));
2287 * The stabs help the internal debugger as they are an indication that it
2288 * is sensible to step into a thunk/trampoline.
2290 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2291 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2292 fprintf( outfile, "\t.text\n" );
2293 fprintf( outfile, "\t.align 4\n" );
2294 fprintf( outfile, "Code_Start:\n\n" );
2295 #endif
2297 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Start\n" );
2298 fprintf( outfile, PREFIX "CALLTO16_Start:\n" );
2300 /* Build the callback functions */
2302 while (fgets( buffer, sizeof(buffer), infile ))
2304 if (strstr( buffer, "### start build ###" )) break;
2306 while (fgets( buffer, sizeof(buffer), infile ))
2308 char *p = strstr( buffer, "CallTo16_" );
2309 if (p)
2311 char *profile = p + strlen( "CallTo16_" );
2312 p = profile;
2313 while ((*p == '_') || isalpha(*p)) p++;
2314 *p = '\0';
2315 BuildCallTo16Func( outfile, profile );
2317 if (strstr( buffer, "### stop build ###" )) break;
2320 /* Output the 16-bit return code */
2322 BuildRet16Func( outfile );
2324 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_End\n" );
2325 fprintf( outfile, PREFIX "CALLTO16_End:\n" );
2327 #ifdef USE_STABS
2328 fprintf( outfile, "\t.text\n");
2329 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2330 fprintf( outfile, ".Letext:\n");
2331 #endif
2333 fclose( infile );
2334 return 0;
2338 /*******************************************************************
2339 * BuildCall32
2341 * Build the 32-bit callbacks
2343 static int BuildCall32( FILE *outfile, char * outname )
2345 char buffer[1024];
2347 /* File header */
2349 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2350 fprintf( outfile, "\t.text\n" );
2352 #ifdef USE_STABS
2353 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2354 getcwd(buffer, sizeof(buffer));
2357 * The stabs help the internal debugger as they are an indication that it
2358 * is sensible to step into a thunk/trampoline.
2360 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2361 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2362 fprintf( outfile, "\t.text\n" );
2363 fprintf( outfile, "\t.align 4\n" );
2364 fprintf( outfile, "Code_Start:\n" );
2365 #endif
2367 /* Build the 32-bit large stack callback */
2369 BuildCallTo32LargeStack( outfile );
2371 /* Build the register callback function */
2373 BuildCallFrom32Regs( outfile );
2375 #ifdef USE_STABS
2376 fprintf( outfile, "\t.text\n");
2377 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2378 fprintf( outfile, ".Letext:\n");
2379 #endif
2381 return 0;
2385 /*******************************************************************
2386 * usage
2388 static void usage(void)
2390 fprintf( stderr,
2391 "usage: build [-o outfile] -spec SPECNAMES\n"
2392 " build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2393 " build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2394 " build [-o outfile] -call32\n" );
2395 exit(1);
2399 /*******************************************************************
2400 * main
2402 int main(int argc, char **argv)
2404 char *outname = NULL;
2405 FILE *outfile = stdout;
2406 int res = -1;
2408 if (argc < 2) usage();
2410 if (!strcmp( argv[1], "-o" ))
2412 outname = argv[2];
2413 argv += 2;
2414 argc -= 2;
2415 if (argc < 2) usage();
2416 if (!(outfile = fopen( outname, "w" )))
2418 fprintf( stderr, "Unable to create output file '%s'\n", outname );
2419 exit(1);
2423 /* Retrieve the selector values; this assumes that we are building
2424 * the asm files on the platform that will also run them. Probably
2425 * a safe assumption to make.
2427 GET_CS( Code_Selector );
2428 GET_DS( Data_Selector );
2430 if (!strcmp( argv[1], "-spec" ))
2431 res = BuildSpec( outfile, argc, argv );
2432 else if (!strcmp( argv[1], "-callfrom16" ))
2433 res = BuildCallFrom16( outfile, outname, argc, argv );
2434 else if (!strcmp( argv[1], "-callto16" ))
2435 res = BuildCallTo16( outfile, outname, argc, argv );
2436 else if (!strcmp( argv[1], "-call32" ))
2437 res = BuildCall32( outfile, outname );
2438 else
2440 fclose( outfile );
2441 unlink( outname );
2442 usage();
2445 fclose( outfile );
2446 if (res < 0)
2448 unlink( outname );
2449 return 1;
2451 return 0;