Release 980628
[wine] / tools / build.c
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  */
7
8 #include <assert.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <unistd.h>
14
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"
22
23 #ifdef NEED_UNDERSCORE_PREFIX
24 # define PREFIX "_"
25 #else
26 # define PREFIX
27 #endif
28
29 #ifdef HAVE_ASM_STRING
30 # define STRING ".string"
31 #else
32 # define STRING ".ascii"
33 #endif
34
35 #if defined(__GNUC__) && !defined(__svr4__)
36 # define USE_STABS
37 #else
38 # undef USE_STABS
39 #endif
40
41 typedef enum
42 {
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;
59
60 static const char * const TypeNames[TYPE_NBTYPES] =
61 {
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 */
76 };
77
78 #define MAX_ORDINALS    2048
79
80   /* Callback function used for stub functions */
81 #define STUB_CALLBACK \
82   ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
83
84 typedef enum
85 {
86     SPEC_INVALID,
87     SPEC_WIN16,
88     SPEC_WIN32
89 } SPEC_TYPE;
90
91 typedef struct
92 {
93     int n_values;
94     int *values;
95 } ORD_VARIABLE;
96
97 typedef struct
98 {
99     int  n_args;
100     char arg_types[32];
101     char link_name[80];
102 } ORD_FUNCTION;
103
104 typedef struct
105 {
106     int arg_size;
107     int ret_value;
108 } ORD_RETURN;
109
110 typedef struct
111 {
112     int value;
113 } ORD_ABS;
114
115 typedef struct
116 {
117     char link_name[80];
118 } ORD_VARARGS;
119
120 typedef struct
121 {
122     char link_name[80];
123 } ORD_EXTERN;
124
125 typedef struct
126 {
127     ORD_TYPE    type;
128     int         offset;
129     int         lineno;
130     char        name[80];
131     union
132     {
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;
141
142 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
143
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
154 char *ParseBuffer = NULL;
155 char *ParseNext;
156 char ParseSaveChar;
157 int Line;
158
159 static int debugging = 1;
160
161   /* Offset of a structure field relative to the start of the struct */
162 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
163
164   /* Offset of register relative to the start of the CONTEXT struct */
165 #define CONTEXTOFFSET(reg)  STRUCTOFFSET(CONTEXT,reg)
166
167   /* Offset of the stack pointer relative to %fs:(0) */
168 #define STACKOFFSET (STRUCTOFFSET(THDB,cur_stack) - STRUCTOFFSET(THDB,teb))
169
170   /* Offset of the saved fs relative to %fs:(0) */
171 #define FSOFFSET (STRUCTOFFSET(THDB,saved_fs) - STRUCTOFFSET(THDB,teb))
172
173
174 static void *xmalloc (size_t size)
175 {
176     void *res;
177
178     res = malloc (size ? size : 1);
179     if (res == NULL)
180     {
181         fprintf (stderr, "Virtual memory exhausted.\n");
182         exit (1);
183     }
184     return res;
185 }
186
187
188 static void *xrealloc (void *ptr, size_t size)
189 {
190     void *res = realloc (ptr, size);
191     if (res == NULL)
192     {
193         fprintf (stderr, "Virtual memory exhausted.\n");
194         exit (1);
195     }
196     return res;
197 }
198
199
200 static int IsNumberString(char *s)
201 {
202     while (*s != '\0')
203         if (!isdigit(*s++))
204             return 0;
205
206     return 1;
207 }
208
209 static char *strupper(char *s)
210 {
211     char *p;
212     
213     for(p = s; *p != '\0'; p++)
214         *p = toupper(*p);
215
216     return s;
217 }
218
219 static char * GetTokenInLine(void)
220 {
221     char *p;
222     char *token;
223
224     if (ParseNext != ParseBuffer)
225     {
226         if (ParseSaveChar == '\0')
227             return NULL;
228         *ParseNext = ParseSaveChar;
229     }
230     
231     /*
232      * Remove initial white space.
233      */
234     for (p = ParseNext; isspace(*p); p++)
235         ;
236     
237     if ((*p == '\0') || (*p == '#'))
238         return NULL;
239     
240     /*
241      * Find end of token.
242      */
243     token = p++;
244     if (*token != '(' && *token != ')')
245         while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
246             p++;
247     
248     ParseSaveChar = *p;
249     ParseNext = p;
250     *p = '\0';
251
252     return token;
253 }
254
255 static char * GetToken(void)
256 {
257     char *token;
258
259     if (ParseBuffer == NULL)
260     {
261         ParseBuffer = xmalloc(512);
262         ParseNext = ParseBuffer;
263         while (1)
264         {
265             Line++;
266             if (fgets(ParseBuffer, 511, SpecFp) == NULL)
267                 return NULL;
268             if (ParseBuffer[0] != '#')
269                 break;
270         }
271     }
272
273     while ((token = GetTokenInLine()) == NULL)
274     {
275         ParseNext = ParseBuffer;
276         while (1)
277         {
278             Line++;
279             if (fgets(ParseBuffer, 511, SpecFp) == NULL)
280                 return NULL;
281             if (ParseBuffer[0] != '#')
282                 break;
283         }
284     }
285
286     return token;
287 }
288
289
290 /*******************************************************************
291  *         ParseVariable
292  *
293  * Parse a variable definition.
294  */
295 static int ParseVariable( ORDDEF *odp )
296 {
297     char *endptr;
298     int *value_array;
299     int n_values;
300     int value_array_size;
301     
302     char *token = GetToken();
303     if (*token != '(')
304     {
305         fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
306                 SpecName, Line, token);
307         return -1;
308     }
309
310     n_values = 0;
311     value_array_size = 25;
312     value_array = xmalloc(sizeof(*value_array) * value_array_size);
313     
314     while ((token = GetToken()) != NULL)
315     {
316         if (*token == ')')
317             break;
318
319         value_array[n_values++] = strtol(token, &endptr, 0);
320         if (n_values == value_array_size)
321         {
322             value_array_size += 25;
323             value_array = xrealloc(value_array, 
324                                    sizeof(*value_array) * value_array_size);
325         }
326         
327         if (endptr == NULL || *endptr != '\0')
328         {
329             fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
330                     SpecName, Line, token);
331             return -1;
332         }
333     }
334     
335     if (token == NULL)
336     {
337         fprintf(stderr, "%s:%d: End of file in variable declaration\n",
338                 SpecName, Line);
339         return -1;
340     }
341
342     odp->u.var.n_values = n_values;
343     odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
344
345     return 0;
346 }
347
348
349 /*******************************************************************
350  *         ParseExportFunction
351  *
352  * Parse a function definition.
353  */
354 static int ParseExportFunction( ORDDEF *odp )
355 {
356     char *token;
357     int i;
358
359     switch(SpecType)
360     {
361     case SPEC_WIN16:
362         if (odp->type == TYPE_STDCALL)
363         {
364             fprintf( stderr, "%s:%d: 'stdcall' not supported for Win16\n",
365                      SpecName, Line );
366             return -1;
367         }
368         break;
369     case SPEC_WIN32:
370         if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
371         {
372             fprintf( stderr, "%s:%d: 'pascal' not supported for Win32\n",
373                      SpecName, Line );
374             return -1;
375         }
376         break;
377     default:
378         break;
379     }
380
381     token = GetToken();
382     if (*token != '(')
383     {
384         fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
385                 SpecName, Line, token);
386         return -1;
387     }
388
389     for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
390     {
391         token = GetToken();
392         if (*token == ')')
393             break;
394
395         if (!strcmp(token, "word"))
396             odp->u.func.arg_types[i] = 'w';
397         else if (!strcmp(token, "s_word"))
398             odp->u.func.arg_types[i] = 's';
399         else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
400             odp->u.func.arg_types[i] = 'l';
401         else if (!strcmp(token, "ptr"))
402             odp->u.func.arg_types[i] = 'p';
403         else if (!strcmp(token, "str"))
404             odp->u.func.arg_types[i] = 't';
405         else if (!strcmp(token, "wstr"))
406             odp->u.func.arg_types[i] = 'W';
407         else if (!strcmp(token, "segstr"))
408             odp->u.func.arg_types[i] = 'T';
409         else if (!strcmp(token, "double"))
410         {
411             odp->u.func.arg_types[i++] = 'l';
412             odp->u.func.arg_types[i] = 'l';
413         }
414         else
415         {
416             fprintf(stderr, "%s:%d: Unknown variable type '%s'\n",
417                     SpecName, Line, token);
418             return -1;
419         }
420         if (SpecType == SPEC_WIN32)
421         {
422             if (strcmp(token, "long") &&
423                 strcmp(token, "ptr") &&
424                 strcmp(token, "str") &&
425                 strcmp(token, "wstr") &&
426                 strcmp(token, "double"))
427             {
428                 fprintf( stderr, "%s:%d: Type '%s' not supported for Win32\n",
429                          SpecName, Line, token );
430                 return -1;
431             }
432         }
433     }
434     if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
435     {
436         fprintf( stderr, "%s:%d: Too many arguments\n", SpecName, Line );
437         return -1;
438     }
439     odp->u.func.arg_types[i] = '\0';
440     if ((odp->type == TYPE_STDCALL) && !i)
441         odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
442     if ((odp->type == TYPE_REGISTER) && (SpecType == SPEC_WIN32) && i)
443     {
444         fprintf( stderr, "%s:%d: register functions cannot have arguments in Win32\n",
445                  SpecName, Line );
446         return -1;
447     }
448     strcpy(odp->u.func.link_name, GetToken());
449     return 0;
450 }
451
452
453 /*******************************************************************
454  *         ParseEquate
455  *
456  * Parse an 'equate' definition.
457  */
458 static int ParseEquate( ORDDEF *odp )
459 {
460     char *endptr;
461     
462     char *token = GetToken();
463     int value = strtol(token, &endptr, 0);
464     if (endptr == NULL || *endptr != '\0')
465     {
466         fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
467                 SpecName, Line, token);
468         return -1;
469     }
470
471     if (SpecType == SPEC_WIN32)
472     {
473         fprintf( stderr, "%s:%d: 'equate' not supported for Win32\n",
474                  SpecName, Line );
475         return -1;
476     }
477
478     odp->u.abs.value = value;
479     return 0;
480 }
481
482
483 /*******************************************************************
484  *         ParseReturn
485  *
486  * Parse a 'return' definition.
487  */
488 static int ParseReturn( ORDDEF *odp )
489 {
490     char *token;
491     char *endptr;
492     
493     token = GetToken();
494     odp->u.ret.arg_size = strtol(token, &endptr, 0);
495     if (endptr == NULL || *endptr != '\0')
496     {
497         fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
498                 SpecName, Line, token);
499         return -1;
500     }
501
502     token = GetToken();
503     odp->u.ret.ret_value = strtol(token, &endptr, 0);
504     if (endptr == NULL || *endptr != '\0')
505     {
506         fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
507                 SpecName, Line, token);
508         return -1;
509     }
510
511     if (SpecType == SPEC_WIN32)
512     {
513         fprintf( stderr, "%s:%d: 'return' not supported for Win32\n",
514                  SpecName, Line );
515         return -1;
516     }
517
518     return 0;
519 }
520
521
522 /*******************************************************************
523  *         ParseStub
524  *
525  * Parse a 'stub' definition.
526  */
527 static int ParseStub( ORDDEF *odp )
528 {
529     odp->u.func.arg_types[0] = '\0';
530     strcpy( odp->u.func.link_name, STUB_CALLBACK );
531     return 0;
532 }
533
534
535 /*******************************************************************
536  *         ParseVarargs
537  *
538  * Parse an 'varargs' definition.
539  */
540 static int ParseVarargs( ORDDEF *odp )
541 {
542     char *token;
543
544     if (SpecType == SPEC_WIN16)
545     {
546         fprintf( stderr, "%s:%d: 'varargs' not supported for Win16\n",
547                  SpecName, Line );
548         return -1;
549     }
550
551     token = GetToken();
552     if (*token != '(')
553     {
554         fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
555                 SpecName, Line, token);
556         return -1;
557     }
558     token = GetToken();
559     if (*token != ')')
560     {
561         fprintf(stderr, "%s:%d: Expected ')' got '%s'\n",
562                 SpecName, Line, token);
563         return -1;
564     }
565
566     strcpy( odp->u.vargs.link_name, GetToken() );
567     return 0;
568 }
569
570
571 /*******************************************************************
572  *         ParseExtern
573  *
574  * Parse an 'extern' definition.
575  */
576 static int ParseExtern( ORDDEF *odp )
577 {
578     if (SpecType == SPEC_WIN16)
579     {
580         fprintf( stderr, "%s:%d: 'extern' not supported for Win16\n",
581                  SpecName, Line );
582         return -1;
583     }
584     strcpy( odp->u.ext.link_name, GetToken() );
585     return 0;
586 }
587
588
589 /*******************************************************************
590  *         ParseOrdinal
591  *
592  * Parse an ordinal definition.
593  */
594 static int ParseOrdinal(int ordinal)
595 {
596     ORDDEF *odp;
597     char *token;
598
599     if (ordinal >= MAX_ORDINALS)
600     {
601         fprintf(stderr, "%s:%d: Ordinal number too large\n", SpecName, Line );
602         return -1;
603     }
604     if (ordinal > Limit) Limit = ordinal;
605     if (ordinal < Base) Base = ordinal;
606
607     odp = &OrdinalDefinitions[ordinal];
608     if (!(token = GetToken()))
609     {
610         fprintf(stderr, "%s:%d: Expected type after ordinal\n", SpecName, Line);
611         return -1;
612     }
613
614     for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
615         if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
616             break;
617
618     if (odp->type >= TYPE_NBTYPES)
619     {
620         fprintf( stderr,
621                  "%s:%d: Expected type after ordinal, found '%s' instead\n",
622                  SpecName, Line, token );
623         return -1;
624     }
625
626     if (!(token = GetToken()))
627     {
628         fprintf( stderr, "%s:%d: Expected name after type\n", SpecName, Line );
629         return -1;
630     }
631     strcpy( odp->name, token );
632     odp->lineno = Line;
633
634     switch(odp->type)
635     {
636     case TYPE_BYTE:
637     case TYPE_WORD:
638     case TYPE_LONG:
639         return ParseVariable( odp );
640     case TYPE_PASCAL_16:
641     case TYPE_PASCAL:
642     case TYPE_REGISTER:
643     case TYPE_STDCALL:
644     case TYPE_CDECL:
645         return ParseExportFunction( odp );
646     case TYPE_ABS:
647         return ParseEquate( odp );
648     case TYPE_RETURN:
649         return ParseReturn( odp );
650     case TYPE_STUB:
651         return ParseStub( odp );
652     case TYPE_VARARGS:
653         return ParseVarargs( odp );
654     case TYPE_EXTERN:
655         return ParseExtern( odp );
656     default:
657         fprintf( stderr, "Should not happen\n" );
658         return -1;
659     }
660 }
661
662
663 /*******************************************************************
664  *         ParseTopLevel
665  *
666  * Parse a spec file.
667  */
668 static int ParseTopLevel(void)
669 {
670     char *token;
671     
672     while ((token = GetToken()) != NULL)
673     {
674         if (strcmp(token, "name") == 0)
675         {
676             strcpy(DLLName, GetToken());
677             strupper(DLLName);
678             if (!DLLFileName[0]) sprintf( DLLFileName, "%s.DLL", DLLName );
679         }
680         else if (strcmp(token, "file") == 0)
681         {
682             strcpy(DLLFileName, GetToken());
683             strupper(DLLFileName);
684         }
685         else if (strcmp(token, "type") == 0)
686         {
687             token = GetToken();
688             if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
689             else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
690             else
691             {
692                 fprintf(stderr, "%s:%d: Type must be 'win16' or 'win32'\n",
693                         SpecName, Line);
694                 return -1;
695             }
696         }
697         else if (strcmp(token, "heap") == 0)
698         {
699             token = GetToken();
700             if (!IsNumberString(token))
701             {
702                 fprintf(stderr, "%s:%d: Expected number after heap\n",
703                         SpecName, Line);
704                 return -1;
705             }
706             DLLHeapSize = atoi(token);
707         }
708         else if (IsNumberString(token))
709         {
710             int ordinal;
711             int rv;
712             
713             ordinal = atoi(token);
714             if ((rv = ParseOrdinal(ordinal)) < 0)
715                 return rv;
716         }
717         else
718         {
719             fprintf(stderr, 
720                     "%s:%d: Expected name, id, length or ordinal\n",
721                     SpecName, Line);
722             return -1;
723         }
724     }
725
726     return 0;
727 }
728
729
730 /*******************************************************************
731  *         StoreVariableCode
732  *
733  * Store a list of ints into a byte array.
734  */
735 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
736 {
737     int i;
738
739     switch(size)
740     {
741     case 1:
742         for (i = 0; i < odp->u.var.n_values; i++)
743             buffer[i] = odp->u.var.values[i];
744         break;
745     case 2:
746         for (i = 0; i < odp->u.var.n_values; i++)
747             ((unsigned short *)buffer)[i] = odp->u.var.values[i];
748         break;
749     case 4:
750         for (i = 0; i < odp->u.var.n_values; i++)
751             ((unsigned int *)buffer)[i] = odp->u.var.values[i];
752         break;
753     }
754     return odp->u.var.n_values * size;
755 }
756
757
758 /*******************************************************************
759  *         DumpBytes
760  *
761  * Dump a byte stream into the assembly code.
762  */
763 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
764                        const char *section, const char *label_start )
765 {
766     int i;
767     if (section) fprintf( outfile, "\t%s\n", section );
768     if (label_start) fprintf( outfile, "%s:\n", label_start );
769     for (i = 0; i < len; i++)
770     {
771         if (!(i & 0x0f)) fprintf( outfile, "\t.byte " );
772         fprintf( outfile, "%d", *data++ );
773         if (i < len - 1)
774             fprintf( outfile, "%c", ((i & 0x0f) != 0x0f) ? ',' : '\n' );
775     }
776     fprintf( outfile, "\n" );
777 }
778
779
780 /*******************************************************************
781  *         BuildModule16
782  *
783  * Build the in-memory representation of a 16-bit NE module, and dump it
784  * as a byte stream into the assembly code.
785  */
786 static int BuildModule16( FILE *outfile, int max_code_offset,
787                           int max_data_offset )
788 {
789     ORDDEF *odp;
790     int i;
791     char *buffer;
792     NE_MODULE *pModule;
793     SEGTABLEENTRY *pSegment;
794     OFSTRUCT *pFileInfo;
795     BYTE *pstr, *bundle;
796     WORD *pword;
797
798     /*   Module layout:
799      * NE_MODULE       Module
800      * OFSTRUCT        File information
801      * SEGTABLEENTRY   Segment 1 (code)
802      * SEGTABLEENTRY   Segment 2 (data)
803      * WORD[2]         Resource table (empty)
804      * BYTE[2]         Imported names (empty)
805      * BYTE[n]         Resident names table
806      * BYTE[n]         Entry table
807      */
808
809     buffer = xmalloc( 0x10000 );
810
811     pModule = (NE_MODULE *)buffer;
812     pModule->magic = IMAGE_OS2_SIGNATURE;
813     pModule->count = 1;
814     pModule->next = 0;
815     pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
816     pModule->dgroup = 2;
817     pModule->heap_size = DLLHeapSize;
818     pModule->stack_size = 0;
819     pModule->ip = 0;
820     pModule->cs = 0;
821     pModule->sp = 0;
822     pModule->ss = 0;
823     pModule->seg_count = 2;
824     pModule->modref_count = 0;
825     pModule->nrname_size = 0;
826     pModule->modref_table = 0;
827     pModule->nrname_fpos = 0;
828     pModule->moveable_entries = 0;
829     pModule->alignment = 0;
830     pModule->truetype = 0;
831     pModule->os_flags = NE_OSFLAGS_WINDOWS;
832     pModule->misc_flags = 0;
833     pModule->dlls_to_init  = 0;
834     pModule->nrname_handle = 0;
835     pModule->min_swap_area = 0;
836     pModule->expected_version = 0x030a;
837     pModule->module32 = 0;
838     pModule->self = 0;
839     pModule->self_loading_sel = 0;
840
841       /* File information */
842
843     pFileInfo = (OFSTRUCT *)(pModule + 1);
844     pModule->fileinfo = (int)pFileInfo - (int)pModule;
845     memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
846     pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
847                         + strlen(DLLFileName);
848     strcpy( pFileInfo->szPathName, DLLFileName );
849     pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
850         
851       /* Segment table */
852
853     pSegment = (SEGTABLEENTRY *)pstr;
854     pModule->seg_table = (int)pSegment - (int)pModule;
855     pSegment->filepos = 0;
856     pSegment->size = max_code_offset;
857     pSegment->flags = 0;
858     pSegment->minsize = max_code_offset;
859     pSegment->selector = 0;
860     pSegment++;
861
862     pModule->dgroup_entry = (int)pSegment - (int)pModule;
863     pSegment->filepos = 0;
864     pSegment->size = max_data_offset;
865     pSegment->flags = NE_SEGFLAGS_DATA;
866     pSegment->minsize = max_data_offset;
867     pSegment->selector = 0;
868     pSegment++;
869
870       /* Resource table */
871
872     pword = (WORD *)pSegment;
873     pModule->res_table = (int)pword - (int)pModule;
874     *pword++ = 0;
875     *pword++ = 0;
876
877       /* Imported names table */
878
879     pstr = (char *)pword;
880     pModule->import_table = (int)pstr - (int)pModule;
881     *pstr++ = 0;
882     *pstr++ = 0;
883
884       /* Resident names table */
885
886     pModule->name_table = (int)pstr - (int)pModule;
887     /* First entry is module name */
888     *pstr = strlen(DLLName );
889     strcpy( pstr + 1, DLLName );
890     pstr += *pstr + 1;
891     *(WORD *)pstr = 0;
892     pstr += sizeof(WORD);
893     /* Store all ordinals */
894     odp = OrdinalDefinitions + 1;
895     for (i = 1; i <= Limit; i++, odp++)
896     {
897         if (!odp->name[0]) continue;
898         *pstr = strlen( odp->name );
899         strcpy( pstr + 1, odp->name );
900         strupper( pstr + 1 );
901         pstr += *pstr + 1;
902         *(WORD *)pstr = i;
903         pstr += sizeof(WORD);
904     }
905     *pstr++ = 0;
906
907       /* Entry table */
908
909     pModule->entry_table = (int)pstr - (int)pModule;
910     bundle = NULL;
911     odp = OrdinalDefinitions + 1;
912     for (i = 1; i <= Limit; i++, odp++)
913     {
914         int selector = 0;
915
916         switch (odp->type)
917         {
918         case TYPE_CDECL:
919         case TYPE_PASCAL:
920         case TYPE_PASCAL_16:
921         case TYPE_REGISTER:
922         case TYPE_RETURN:
923         case TYPE_STUB:
924             selector = 1;  /* Code selector */
925             break;
926
927         case TYPE_BYTE:
928         case TYPE_WORD:
929         case TYPE_LONG:
930             selector = 2;  /* Data selector */
931             break;
932
933         case TYPE_ABS:
934             selector = 0xfe;  /* Constant selector */
935             break;
936
937         default:
938             selector = 0;  /* Invalid selector */
939             break;
940         }
941
942           /* create a new bundle if necessary */
943         if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
944         {
945             bundle = pstr;
946             bundle[0] = 0;
947             bundle[1] = selector;
948             pstr += 2;
949         }
950
951         (*bundle)++;
952         if (selector != 0)
953         {
954             *pstr++ = 1;
955             *(WORD *)pstr = odp->offset;
956             pstr += sizeof(WORD);
957         }
958     }
959     *pstr++ = 0;
960
961       /* Dump the module content */
962
963     DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
964                ".data", "Module_Start" );
965     return (int)pstr - (int)pModule;
966 }
967
968
969 /*******************************************************************
970  *         BuildSpec32File
971  *
972  * Build a Win32 C file from a spec file.
973  */
974 static int BuildSpec32File( char * specfile, FILE *outfile )
975 {
976     ORDDEF *odp;
977     int i, nb_names;
978
979     fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
980              specfile );
981     fprintf( outfile, "#include \"builtin32.h\"\n\n" );
982
983     /* Output code for all stubs functions */
984
985     fprintf( outfile, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
986              DLLName );
987     for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
988     {
989         if (odp->type != TYPE_STUB) continue;
990         fprintf( outfile, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
991                  i, DLLName, i );
992     }
993
994     /* Output code for all register functions */
995
996     fprintf( outfile, "#ifdef __i386__\n" );
997     for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
998     {
999         if (odp->type != TYPE_REGISTER) continue;
1000         fprintf( outfile,
1001                  "__asm__(\".align 4\\n\\t\"\n"
1002                  "        \".globl " PREFIX "%s\\n\\t\"\n"
1003                  "        \".type " PREFIX "%s,@function\\n\\t\"\n"
1004                  "        \"" PREFIX "%s:\\n\\t\"\n"
1005                  "        \"pushl $" PREFIX "__regs_%s\\n\\t\"\n"
1006                  "        \"pushl $" PREFIX "CALL32_Regs\\n\\t\"\n"
1007                  "        \"ret\");\n",
1008                  odp->u.func.link_name, odp->u.func.link_name,
1009                  odp->u.func.link_name, odp->u.func.link_name );
1010     }
1011     fprintf( outfile, "#endif\n" );
1012
1013     /* Output the DLL functions prototypes */
1014
1015     for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1016     {
1017         switch(odp->type)
1018         {
1019         case TYPE_VARARGS:
1020             fprintf( outfile, "extern void %s();\n", odp->u.vargs.link_name );
1021             break;
1022         case TYPE_EXTERN:
1023             fprintf( outfile, "extern void %s();\n", odp->u.ext.link_name );
1024             break;
1025         case TYPE_REGISTER:
1026         case TYPE_STDCALL:
1027         case TYPE_CDECL:
1028             fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1029             break;
1030         case TYPE_INVALID:
1031         case TYPE_STUB:
1032             break;
1033         default:
1034             fprintf(stderr,"build: function type %d not available for Win32\n",
1035                     odp->type);
1036             return -1;
1037         }
1038     }
1039
1040     /* Output the DLL functions table */
1041
1042     fprintf( outfile, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1043              Limit - Base + 1 );
1044     for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1045     {
1046         switch(odp->type)
1047         {
1048         case TYPE_INVALID:
1049             fprintf( outfile, "    0" );
1050             break;
1051         case TYPE_VARARGS:
1052             fprintf( outfile, "    %s", odp->u.vargs.link_name );
1053             break;
1054         case TYPE_EXTERN:
1055             fprintf( outfile, "    %s", odp->u.ext.link_name );
1056             break;
1057         case TYPE_REGISTER:
1058         case TYPE_STDCALL:
1059         case TYPE_CDECL:
1060             fprintf( outfile, "    %s", odp->u.func.link_name);
1061             break;
1062         case TYPE_STUB:
1063             fprintf( outfile, "    __stub_%d", i );
1064             break;
1065         default:
1066             return -1;
1067         }
1068         if (i < Limit) fprintf( outfile, ",\n" );
1069     }
1070     fprintf( outfile, "\n};\n\n" );
1071
1072     /* Output the DLL names table */
1073
1074     nb_names = 0;
1075     fprintf( outfile, "static const char * const FuncNames[] =\n{\n" );
1076     for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1077     {
1078         if (odp->type == TYPE_INVALID) continue;
1079         if (nb_names++) fprintf( outfile, ",\n" );
1080         fprintf( outfile, "    \"%s\"", odp->name );
1081     }
1082     fprintf( outfile, "\n};\n\n" );
1083
1084     /* Output the DLL argument types */
1085
1086     fprintf( outfile, "static const unsigned int ArgTypes[%d] =\n{\n",
1087              Limit - Base + 1 );
1088     for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1089     {
1090         unsigned int j, mask = 0;
1091         if ((odp->type == TYPE_STDCALL) || (odp->type == TYPE_CDECL))
1092             for (j = 0; odp->u.func.arg_types[j]; j++)
1093             {
1094                 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
1095                 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
1096             }
1097         fprintf( outfile, "    %d", mask );
1098         if (i < Limit) fprintf( outfile, ",\n" );
1099     }
1100     fprintf( outfile, "\n};\n\n" );
1101
1102     /* Output the DLL ordinals table */
1103
1104     fprintf( outfile, "static const unsigned short FuncOrdinals[] =\n{\n" );
1105     nb_names = 0;
1106     for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1107     {
1108         if (odp->type == TYPE_INVALID) continue;
1109         if (nb_names++) fprintf( outfile, ",\n" );
1110         fprintf( outfile, "    %d", i - Base );
1111     }
1112     fprintf( outfile, "\n};\n\n" );
1113
1114     /* Output the DLL functions arguments */
1115
1116     fprintf( outfile, "static const unsigned char FuncArgs[%d] =\n{\n",
1117              Limit - Base + 1 );
1118     for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1119     {
1120         unsigned char args;
1121         switch(odp->type)
1122         {
1123         case TYPE_STDCALL:
1124             args = (unsigned char)strlen(odp->u.func.arg_types);
1125             break;
1126         case TYPE_CDECL:
1127             args = 0x80 | (unsigned char)strlen(odp->u.func.arg_types);
1128             break;
1129         case TYPE_REGISTER:
1130             args = 0xfe;
1131             break;
1132         default:
1133             args = 0xff;
1134             break;
1135         }
1136         fprintf( outfile, "    0x%02x", args );
1137         if (i < Limit) fprintf( outfile, ",\n" );
1138     }
1139     fprintf( outfile, "\n};\n\n" );
1140
1141     /* Output the DLL descriptor */
1142
1143     fprintf( outfile, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1144              DLLName );
1145     fprintf( outfile, "    \"%s\",\n", DLLName );
1146     fprintf( outfile, "    %d,\n", Base );
1147     fprintf( outfile, "    %d,\n", Limit - Base + 1 );
1148     fprintf( outfile, "    %d,\n", nb_names );
1149     fprintf( outfile,
1150              "    Functions,\n"
1151              "    FuncNames,\n"
1152              "    FuncOrdinals,\n"
1153              "    FuncArgs,\n"
1154              "    ArgTypes\n"
1155              "};\n" );
1156     return 0;
1157 }
1158
1159
1160 /*******************************************************************
1161  *         BuildSpec16File
1162  *
1163  * Build a Win16 assembly file from a spec file.
1164  */
1165 static int BuildSpec16File( char * specfile, FILE *outfile )
1166 {
1167     ORDDEF *odp;
1168     int i;
1169     int code_offset, data_offset, module_size;
1170     unsigned char *data;
1171
1172     data = (unsigned char *)xmalloc( 0x10000 );
1173     memset( data, 0, 16 );
1174     data_offset = 16;
1175
1176     fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
1177     fprintf( outfile, "\t.text\n" );
1178     fprintf( outfile, "Code_Start:\n" );
1179     code_offset = 0;
1180
1181     odp = OrdinalDefinitions;
1182     for (i = 0; i <= Limit; i++, odp++)
1183     {
1184         switch (odp->type)
1185         {
1186           case TYPE_INVALID:
1187             odp->offset = 0xffff;
1188             break;
1189
1190           case TYPE_ABS:
1191             odp->offset = LOWORD(odp->u.abs.value);
1192             break;
1193
1194           case TYPE_BYTE:
1195             odp->offset = data_offset;
1196             data_offset += StoreVariableCode( data + data_offset, 1, odp);
1197             break;
1198
1199           case TYPE_WORD:
1200             odp->offset = data_offset;
1201             data_offset += StoreVariableCode( data + data_offset, 2, odp);
1202             break;
1203
1204           case TYPE_LONG:
1205             odp->offset = data_offset;
1206             data_offset += StoreVariableCode( data + data_offset, 4, odp);
1207             break;
1208
1209           case TYPE_RETURN:
1210             fprintf( outfile,"/* %s.%d */\n", DLLName, i);
1211             fprintf( outfile,"\tmovw $%d,%%ax\n",LOWORD(odp->u.ret.ret_value));
1212             fprintf( outfile,"\tmovw $%d,%%dx\n",HIWORD(odp->u.ret.ret_value));
1213             fprintf( outfile,"\t.byte 0x66\n");
1214             if (odp->u.ret.arg_size != 0)
1215                 fprintf( outfile, "\tlret $%d\n\n", odp->u.ret.arg_size);
1216             else
1217             {
1218                 fprintf( outfile, "\tlret\n");
1219                 fprintf( outfile, "\tnop\n");
1220                 fprintf( outfile, "\tnop\n\n");
1221             }
1222             odp->offset = code_offset;
1223             code_offset += 12;  /* Assembly code is 12 bytes long */
1224             break;
1225
1226           case TYPE_REGISTER:
1227           case TYPE_CDECL:
1228           case TYPE_PASCAL:
1229           case TYPE_PASCAL_16:
1230           case TYPE_STUB:
1231             fprintf( outfile, "/* %s.%d */\n", DLLName, i);
1232             fprintf( outfile, "\tpushw %%bp\n" );
1233             fprintf( outfile, "\tpushl $" PREFIX "%s\n",odp->u.func.link_name);
1234             /* FreeBSD does not understand lcall, so do it the hard way */
1235             fprintf( outfile, "\t.byte 0x9a\n" );
1236             fprintf( outfile, "\t.long " PREFIX "CallFrom16_%s_%s_%s\n",
1237                      (odp->type == TYPE_CDECL) ? "c" : "p",
1238                      (odp->type == TYPE_REGISTER) ? "regs" :
1239                      (odp->type == TYPE_PASCAL_16) ? "word" : "long",
1240                      odp->u.func.arg_types );
1241             fprintf( outfile, "\t.long 0x%08lx\n",
1242                      MAKELONG( Code_Selector, 0x9090 /* nop ; nop */ ) );
1243             odp->offset = code_offset;
1244             code_offset += 16;  /* Assembly code is 16 bytes long */
1245             break;
1246                 
1247           default:
1248             fprintf(stderr,"build: function type %d not available for Win16\n",
1249                     odp->type);
1250             return -1;
1251         }
1252     }
1253
1254     if (!code_offset)  /* Make sure the code segment is not empty */
1255     {
1256         fprintf( outfile, "\t.byte 0\n" );
1257         code_offset++;
1258     }
1259
1260     /* Output data segment */
1261
1262     DumpBytes( outfile, data, data_offset, NULL, "Data_Start" );
1263
1264     /* Build the module */
1265
1266     module_size = BuildModule16( outfile, code_offset, data_offset );
1267
1268     /* Output the DLL descriptor */
1269
1270     fprintf( outfile, "\t.text\n" );
1271     fprintf( outfile, "DLLName:\t" STRING " \"%s\\0\"\n", DLLName );
1272     fprintf( outfile, "\t.align 4\n" );
1273     fprintf( outfile, "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1274     fprintf( outfile, PREFIX "%s_Descriptor:\n", DLLName );
1275     fprintf( outfile, "\t.long DLLName\n" );          /* Name */
1276     fprintf( outfile, "\t.long Module_Start\n" );     /* Module start */
1277     fprintf( outfile, "\t.long %d\n", module_size );  /* Module size */
1278     fprintf( outfile, "\t.long Code_Start\n" );       /* Code start */
1279     fprintf( outfile, "\t.long Data_Start\n" );       /* Data start */
1280     return 0;
1281 }
1282
1283
1284 /*******************************************************************
1285  *         BuildSpecFile
1286  *
1287  * Build an assembly file from a spec file.
1288  */
1289 static int BuildSpecFile( FILE *outfile, char *specname )
1290 {
1291     SpecName = specname;
1292     SpecFp = fopen( specname, "r");
1293     if (SpecFp == NULL)
1294     {
1295         fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1296         return -1;
1297     }
1298
1299     if (ParseTopLevel() < 0) return -1;
1300
1301     switch(SpecType)
1302     {
1303     case SPEC_WIN16:
1304         return BuildSpec16File( specname, outfile );
1305     case SPEC_WIN32:
1306         return BuildSpec32File( specname, outfile );
1307     default:
1308         fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1309         return -1;
1310     }
1311 }
1312
1313
1314 /*******************************************************************
1315  *         TransferArgs16To32
1316  *
1317  * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1318  * The 16-bit stack layout is:
1319  *   ...     ...
1320  *  (bp+8)    arg2
1321  *  (bp+6)    arg1
1322  *  (bp+4)    cs
1323  *  (bp+2)    ip
1324  *  (bp)      bp
1325  *
1326  * For 'cdecl' argn up to arg1 are reversed.
1327  */
1328 static int TransferArgs16To32( FILE *outfile, char *args, int usecdecl )
1329 {
1330     int i, pos16, pos32;
1331     char *xargs;
1332
1333     /* Copy the arguments */
1334
1335     pos16 = 6;  /* skip bp and return address */
1336     pos32 = usecdecl ? -(strlen(args) * 4) : 0;
1337     xargs = usecdecl ? args:args+strlen(args);
1338
1339     for (i = strlen(args); i > 0; i--)
1340     {
1341         if (!usecdecl) {
1342             pos32 -= 4;
1343             xargs--;
1344         }
1345         switch(*xargs)
1346         {
1347         case 'w':  /* word */
1348             fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1349             fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1350             pos16 += 2;
1351             break;
1352
1353         case 's':  /* s_word */
1354             fprintf( outfile, "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1355             fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1356             pos16 += 2;
1357             break;
1358
1359         case 'l':  /* long or segmented pointer */
1360         case 'T':  /* segmented pointer to null-terminated string */
1361             fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", pos16 );
1362             fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1363             pos16 += 4;
1364             break;
1365
1366         case 'p':  /* linear pointer */
1367         case 't':  /* linear pointer to null-terminated string */
1368             /* Get the selector */
1369             fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1370             /* Get the selector base */
1371             fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1372             fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1373             fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1374             /* Add the offset */
1375             fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1376             fprintf( outfile, "\taddl %%eax,%d(%%ebx)\n", pos32 );
1377             pos16 += 4;
1378             break;
1379
1380         default:
1381             fprintf( stderr, "Unknown arg type '%c'\n", *xargs );
1382         }
1383         if (usecdecl) {
1384             pos32 += 4;
1385             xargs++;
1386         }
1387     }
1388
1389     return pos16 - 6;  /* Return the size of the 16-bit args */
1390 }
1391
1392
1393 /*******************************************************************
1394  *         BuildContext16
1395  *
1396  * Build the context structure on the 32-bit stack.
1397  */
1398 static void BuildContext16( FILE *outfile )
1399 {
1400     /* Store the registers */
1401
1402     fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1403              CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1404     fprintf( outfile, "\tmovl %%ecx,%d(%%ebx)\n",
1405              CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1406     fprintf( outfile, "\tmovl %%edx,%d(%%ebx)\n",
1407              CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1408     fprintf( outfile, "\tmovl %%esi,%d(%%ebx)\n",
1409              CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1410     fprintf( outfile, "\tmovl %%edi,%d(%%ebx)\n",
1411              CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1412
1413     fprintf( outfile, "\tmovl -20(%%ebp),%%eax\n" ); /* Get %ebx from stack*/
1414     fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1415              CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1416     fprintf( outfile, "\tmovzwl -10(%%ebp),%%eax\n" ); /* Get %ds from stack*/
1417     fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1418              CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1419     fprintf( outfile, "\tmovzwl -6(%%ebp),%%eax\n" ); /* Get %es from stack*/
1420     fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1421              CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1422     fprintf( outfile, "\tpushfl\n" );
1423     fprintf( outfile, "\tpopl %d(%%ebx)\n",
1424              CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1425     fprintf( outfile, "\tmovl -16(%%ebp),%%eax\n" ); /* Get %ebp from stack */
1426     fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1427              CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1428     fprintf( outfile, "\tmovzwl 2(%%ebp),%%eax\n" ); /* Get %ip from stack */
1429     fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1430              CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1431     fprintf( outfile, "\tleal 2(%%ebp),%%eax\n" );  /* Get initial %sp */
1432     fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1433              CONTEXTOFFSET(Esp) - sizeof(CONTEXT) );
1434     fprintf( outfile, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1435     fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1436              CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1437     fprintf( outfile, "\tmovw %%fs,%%ax\n" );
1438     fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1439              CONTEXTOFFSET(SegFs) - sizeof(CONTEXT) );
1440     fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1441     fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1442              CONTEXTOFFSET(SegGs) - sizeof(CONTEXT) );
1443     fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1444     fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1445              CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1446 #if 0
1447     fprintf( outfile, "\tfsave %d(%%ebx)\n",
1448              CONTEXTOFFSET(FloatSave) - sizeof(CONTEXT) );
1449 #endif
1450 }
1451
1452
1453 /*******************************************************************
1454  *         RestoreContext16
1455  *
1456  * Restore the registers from the context structure.
1457  */
1458 static void RestoreContext16( FILE *outfile )
1459 {
1460     /* Get the 32-bit stack pointer */
1461
1462     fprintf( outfile, "\tleal -%d(%%ebp),%%ebx\n",
1463              STRUCTOFFSET(STACK32FRAME,ebp) );
1464
1465     /* Remove everything up to (including) the return address
1466      * from the 16-bit stack */
1467
1468     fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1469              CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1470     fprintf( outfile, "\tmovw %%ax,%%ss\n" );
1471     fprintf( outfile, "\tmovl %d(%%ebx),%%esp\n",
1472              CONTEXTOFFSET(Esp) - sizeof(CONTEXT) );
1473     fprintf( outfile, "\taddl $4,%%esp\n" );  /* Remove return address */
1474
1475     /* Restore the registers */
1476
1477     fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n",
1478              CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1479     fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n",
1480              CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1481     fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n",
1482              CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1483     fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n",
1484              CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1485     fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n",
1486              CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1487     fprintf( outfile, "\tpushw %d(%%ebx)\n",  /* Push new cs */
1488              CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1489     fprintf( outfile, "\tpushw %d(%%ebx)\n",  /* Push new ip */
1490              CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1491     fprintf( outfile, "\tpushl %d(%%ebx)\n",  /* Push new ds */
1492              CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1493     fprintf( outfile, "\tpushl %d(%%ebx)\n",  /* Push new es */
1494              CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1495     fprintf( outfile, "\tpushl %d(%%ebx)\n",
1496              CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1497     fprintf( outfile, "\tpopfl\n" );
1498     fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1499              CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1500     fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n",
1501              CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1502     fprintf( outfile, "\tpopl %%es\n" );  /* Set es */
1503     fprintf( outfile, "\tpopl %%ds\n" );  /* Set ds */
1504 }
1505
1506
1507 /*******************************************************************
1508  *         BuildCallFrom16Func
1509  *
1510  * Build a 16-bit-to-Wine callback function. The syntax of the function
1511  * profile is: call_type_xxxxx, where 'call' is the letter 'c' or 'p' for C or
1512  * Pascal calling convention, 'type' is one of 'regs', 'word' or
1513  * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1514  * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1515  * 'T'=segmented pointer to null-terminated string).
1516  * For register functions, the arguments are ignored, but they are still
1517  * removed from the stack upon return.
1518  *
1519  * A special variant of the callback function is generated by the function 
1520  * profile "t_long_". This is used by the Win95 16->32 thunk
1521  * functions C16ThkSL and C16ThkSL01 and is implemented as follows:
1522  * On entry, the EBX register is set up to contain a flat pointer to the
1523  * 16-bit stack such that EBX+22 points to the first argument.
1524  * Then, the entry point is called, while EBP is set up to point
1525  * to the return address (on the 32-bit stack).
1526  * The called function returns with CX set to the number of bytes
1527  * to be popped of the caller's stack.
1528  *
1529  * Stack layout upon entry to the callback function:
1530  *  ...           ...
1531  * (sp+18) word   first 16-bit arg
1532  * (sp+16) word   cs
1533  * (sp+14) word   ip
1534  * (sp+12) word   bp
1535  * (sp+8)  long   32-bit entry point (used to store edx)
1536  * (sp+6)  word   high word of cs (always 0, used to store es)
1537  * (sp+4)  word   low word of cs of 16-bit entry point
1538  * (sp+2)  word   high word of ip (always 0, used to store ds)
1539  * (sp)    word   low word of ip of 16-bit entry point
1540  *
1541  * Added on the stack:
1542  * (sp-4)  long   ebp
1543  * (sp-8)  long   saved previous stack
1544  */
1545 static void BuildCallFrom16Func( FILE *outfile, char *profile )
1546 {
1547     int argsize = 0;
1548     int short_ret = 0;
1549     int reg_func = 0;
1550     int cdecl = 0;
1551     int thunk = 0;
1552     char *args = profile + 7;
1553
1554     /* Parse function type */
1555
1556     if (!strncmp( "c_", profile, 2 )) cdecl = 1;
1557     else if (!strncmp( "t_", profile, 2 )) thunk = 1;
1558     else if (strncmp( "p_", profile, 2 ))
1559     {
1560         fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1561         return;
1562     }
1563
1564     if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
1565     else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
1566     else if (strncmp( "long_", profile + 2, 5 ))
1567     {
1568         fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1569         return;
1570     }
1571
1572     /* Function header */
1573
1574     fprintf( outfile, "\n\t.align 4\n" );
1575 #ifdef USE_STABS
1576     fprintf( outfile, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX "CallFrom16_%s\n", 
1577              profile, profile);
1578 #endif
1579     fprintf( outfile, "\t.globl " PREFIX "CallFrom16_%s\n", profile );
1580     fprintf( outfile, PREFIX "CallFrom16_%s:\n", profile );
1581
1582     /* Setup bp to point to its copy on the stack */
1583
1584     fprintf( outfile, "\tpushl %%ebp\n" );  /* Save the full 32-bit ebp */
1585     fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1586     fprintf( outfile, "\taddw $16,%%bp\n" );
1587
1588     /* Save 16-bit ds and es */
1589
1590     /* Stupid FreeBSD assembler doesn't know these either */
1591     /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1592     fprintf( outfile, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1593     /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1594     fprintf( outfile, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1595
1596     /* Save %ebx */
1597
1598     fprintf( outfile, "\tpushl %%ebx\n" );
1599
1600     /* Restore 32-bit segment registers */
1601
1602     fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
1603 #ifdef __svr4__
1604     fprintf( outfile, "\tdata16\n");
1605 #endif
1606     fprintf( outfile, "\tmovw %%bx,%%ds\n" );
1607 #ifdef __svr4__
1608     fprintf( outfile, "\tdata16\n");
1609 #endif
1610     fprintf( outfile, "\tmovw %%bx,%%es\n" );
1611
1612     fprintf( outfile, "\tmovw %%fs, %%bx\n" );
1613     fprintf( outfile, "\tmovw " PREFIX "CALLTO16_Current_fs,%%fs\n" );
1614     fprintf( outfile, "\t.byte 0x64\n\tmovw %%bx,(%d)\n", FSOFFSET );
1615
1616     /* Get the 32-bit stack pointer from the TEB */
1617
1618     fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET );
1619
1620     /* Save the 16-bit stack */
1621
1622 #ifdef __svr4__
1623     fprintf( outfile,"\tdata16\n");
1624 #endif
1625     fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss,(%d)\n", STACKOFFSET + 2 );
1626     fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp,(%d)\n", STACKOFFSET );
1627
1628     /* Transfer the arguments */
1629
1630     if (reg_func) BuildContext16( outfile );
1631     else if (*args) argsize = TransferArgs16To32( outfile, args, cdecl );
1632     else if (thunk)
1633     {
1634         /* Get the stack selector base */
1635         fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1636         fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1637         fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1638         fprintf( outfile, "\tmovl %%eax,-20(%%ebp)\n" );
1639         /* Add the offset */
1640         fprintf( outfile, "\tleal -16(%%ebp),%%eax\n" );
1641         fprintf( outfile, "\taddl %%eax,-20(%%ebp)\n" );
1642     }
1643
1644     /* Get the address of the API function */
1645
1646     fprintf( outfile, "\tmovl -4(%%ebp),%%eax\n" );
1647
1648     /* If necessary, save %edx over the API function address */
1649
1650     if (!reg_func && short_ret)
1651         fprintf( outfile, "\tmovl %%edx,-4(%%ebp)\n" );
1652
1653     /* Restore %ebx and store the 32-bit stack pointer instead */
1654
1655     fprintf( outfile, "\tmovl %%ebx,%%ebp\n" );
1656     fprintf( outfile, "\tpopl %%ebx\n" );
1657     fprintf( outfile, "\tpushl %%ebp\n" );
1658
1659     /* Switch to the 32-bit stack */
1660
1661     fprintf( outfile, "\tpushl %%ds\n" );
1662     fprintf( outfile, "\tpopl %%ss\n" );
1663     fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1664              reg_func ? sizeof(CONTEXT) : 4 * strlen(args) );
1665     if (reg_func)  /* Push the address of the context struct */
1666         fprintf( outfile, "\tpushl %%esp\n" );
1667
1668     /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1669
1670     fprintf( outfile, "\taddl $%d,%%ebp\n", STRUCTOFFSET(STACK32FRAME,ebp) );
1671
1672     /* Print the debug information before the call */
1673
1674     if (debugging && !thunk)
1675     {
1676         int ftype = 0;
1677
1678         if (cdecl) ftype |= 4;
1679         if (reg_func) ftype |= 2;
1680         if (short_ret) ftype |= 1;
1681
1682         fprintf( outfile, "\tpushl %%eax\n" );
1683         fprintf( outfile, "\tpushl $Profile_%s\n", profile );
1684         fprintf( outfile, "\tpushl $%d\n", ftype );
1685         fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n" );
1686         fprintf( outfile, "\tpopl %%eax\n" );
1687         fprintf( outfile, "\tpopl %%eax\n" );
1688         fprintf( outfile, "\tpopl %%eax\n" );
1689     }
1690
1691     /* Call the entry point */
1692
1693     if (thunk)
1694     {
1695         fprintf( outfile, "\tpushl %%ebp\n" );
1696         fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
1697         fprintf( outfile, "\tcall *%%eax\n" );
1698         fprintf( outfile, "\tpopl %%ebp\n" );
1699     }
1700     else
1701         fprintf( outfile, "\tcall *%%eax\n" );
1702
1703
1704     /* Print the debug information after the call */
1705
1706     if (debugging && !thunk)
1707     {
1708         if (reg_func)
1709         {
1710             /* Push again the address of the context struct in case */
1711             /* it has been removed by an stdcall function */
1712             fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1713                      sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME,ebp) );
1714             fprintf( outfile, "\tpushl %%esp\n" );
1715         }
1716         fprintf( outfile, "\tpushl %%eax\n" );
1717         fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1718         fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n" );
1719         fprintf( outfile, "\tpopl %%eax\n" );
1720         fprintf( outfile, "\tpopl %%eax\n" );
1721     }
1722
1723     /* Restore the 16-bit stack */
1724
1725 #ifdef __svr4__
1726     fprintf( outfile, "\tdata16\n");
1727 #endif
1728     fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2 );
1729     fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
1730     fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
1731     fprintf( outfile, "\tmovw %%fs," PREFIX "CALLTO16_Current_fs\n" );
1732
1733     fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%bx\n", FSOFFSET );
1734     fprintf( outfile, "\tmovw %%bx, %%fs\n" );
1735
1736     if (reg_func)
1737     {
1738         /* Calc the arguments size */
1739         while (*args)
1740         {
1741             switch(*args)
1742             {
1743             case 'w':
1744             case 's':
1745                 argsize += 2;
1746                 break;
1747             case 'p':
1748             case 't':
1749             case 'l':
1750             case 'T':
1751                 argsize += 4;
1752                 break;
1753             default:
1754                 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1755             }
1756             args++;
1757         }
1758
1759         /* Restore registers from the context structure */
1760         RestoreContext16( outfile );
1761     }
1762     else
1763     {
1764         /* Restore high 16 bits of ebp */
1765         fprintf( outfile, "\tpopl %%ebp\n" );
1766
1767         /* Restore ds and es */
1768         fprintf( outfile, "\tincl %%esp\n" );      /* Remove ip */
1769         fprintf( outfile, "\tincl %%esp\n" );
1770         fprintf( outfile, "\tpopl %%edx\n" );      /* Remove cs and ds */
1771         fprintf( outfile, "\tmovw %%dx,%%ds\n" );  /* and restore ds */
1772         fprintf( outfile, "\t.byte 0x66\n\tpopl %%es\n" );    /* Restore es */
1773
1774         if (short_ret) fprintf( outfile, "\tpopl %%edx\n" );  /* Restore edx */
1775         else
1776         {
1777             /* Get the return value into dx:ax */
1778             fprintf( outfile, "\tmovl %%eax,%%edx\n" );
1779             fprintf( outfile, "\tshrl $16,%%edx\n" );
1780             /* Remove API entry point */
1781             fprintf( outfile, "\taddl $4,%%esp\n" );
1782         }
1783
1784         /* Restore low 16 bits of ebp */
1785         fprintf( outfile, "\tpopw %%bp\n" );
1786     }
1787
1788     /* Remove the arguments and return */
1789     
1790     if (thunk)
1791     {
1792         fprintf( outfile, "\tpopl %%ebx\n" );
1793         fprintf( outfile, "\txorb %%ch,%%ch\n" );
1794         fprintf( outfile, "\taddw %%cx, %%sp\n" );
1795         fprintf( outfile, "\tpushl %%ebx\n" );
1796         fprintf( outfile, "\t.byte 0x66\n" );
1797         fprintf( outfile, "\tlret\n" );
1798     }
1799     else if (argsize && !cdecl)
1800     {
1801         fprintf( outfile, "\t.byte 0x66\n" );
1802         fprintf( outfile, "\tlret $%d\n", argsize );
1803     }
1804     else
1805     {
1806         fprintf( outfile, "\t.byte 0x66\n" );
1807         fprintf( outfile, "\tlret\n" );
1808     }
1809 }
1810
1811
1812 /*******************************************************************
1813  *         BuildCallTo16Func
1814  *
1815  * Build a Wine-to-16-bit callback function.
1816  *
1817  * Stack frame of the callback function:
1818  *  ...      ...
1819  * (ebp+16) arg2
1820  * (ebp+12) arg1
1821  * (ebp+8)  func to call
1822  * (ebp+4)  return address
1823  * (ebp)    previous ebp
1824  *
1825  * Prototypes for the CallTo16 functions:
1826  *   extern WINAPI WORD CallTo16_word_xxx( FARPROC16 func, args... );
1827  *   extern WINAPI LONG CallTo16_long_xxx( FARPROC16 func, args... );
1828  *   extern WINAPI void CallTo16_sreg_( const CONTEXT *context, int nb_args );
1829  *   extern WINAPI void CallTo16_lreg_( const CONTEXT *context, int nb_args );
1830  */
1831 static void BuildCallTo16Func( FILE *outfile, char *profile )
1832 {
1833     int short_ret = 0;
1834     int reg_func = 0;
1835     char *args = profile + 5;
1836
1837     if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1838     else if (!strncmp( "sreg_", profile, 5 )) reg_func = 1;
1839     else if (!strncmp( "lreg_", profile, 5 )) reg_func = 2;
1840     else if (strncmp( "long_", profile, 5 ))
1841     {
1842         fprintf( stderr, "Invalid function name '%s'.\n", profile );
1843         exit(1);
1844     }
1845
1846     /* Function header */
1847
1848     fprintf( outfile, "\n\t.align 4\n" );
1849 #ifdef USE_STABS
1850     fprintf( outfile, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX "CallTo16_%s\n", 
1851              profile, profile);
1852 #endif
1853     fprintf( outfile, "\t.globl " PREFIX "CallTo16_%s\n", profile );
1854     fprintf( outfile, PREFIX "CallTo16_%s:\n", profile );
1855
1856     /* Entry code */
1857
1858     fprintf( outfile, "\tpushl %%ebp\n" );
1859     fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
1860
1861     /* Call the actual CallTo16 routine (simulate a lcall) */
1862
1863     fprintf( outfile, "\tpushl %%cs\n" );
1864     fprintf( outfile, "\tcall do_callto16_%s\n", profile );
1865
1866     /* Exit code */
1867
1868     /* FIXME: this is a hack because of task.c */
1869     if (!strcmp( profile, "word_" ))
1870     {
1871         fprintf( outfile, ".globl " PREFIX "CALLTO16_Restore\n" );
1872         fprintf( outfile, PREFIX "CALLTO16_Restore:\n" );
1873     }
1874     fprintf( outfile, "\tpopl %%ebp\n" );
1875     fprintf( outfile, "\tret $%d\n", 4 * strlen(args) + 4 );
1876
1877     /* Start of the actual CallTo16 routine */
1878
1879     /* Save the 32-bit registers */
1880
1881     fprintf( outfile, "do_callto16_%s:\n", profile );
1882     fprintf( outfile, "\tpushl %%ebx\n" );
1883     fprintf( outfile, "\tpushl %%ecx\n" );
1884     fprintf( outfile, "\tpushl %%edx\n" );
1885     fprintf( outfile, "\tpushl %%esi\n" );
1886     fprintf( outfile, "\tpushl %%edi\n" );
1887
1888     /* Print debugging info */
1889
1890     if (debugging)
1891     {
1892         /* Push the address of the first argument */
1893         fprintf( outfile, "\tleal 8(%%ebp),%%eax\n" );
1894         fprintf( outfile, "\tpushl $%d\n", reg_func ? -1 : strlen(args) );
1895         fprintf( outfile, "\tpushl %%eax\n" );
1896         fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
1897         fprintf( outfile, "\tpopl %%eax\n" );
1898         fprintf( outfile, "\tpopl %%eax\n" );
1899     }
1900
1901     /* Save the 32-bit stack and %fs */
1902
1903     fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
1904     fprintf( outfile, "\tmovl %%ebp,%%ebx\n" );
1905     fprintf( outfile, "\tmovl %%esp,%%edx\n" );
1906     fprintf( outfile, "\tmovw %%fs," PREFIX "CALLTO16_Current_fs\n" );
1907
1908     if (reg_func)
1909     {
1910         /* Switch to the 16-bit stack, saving the current %%esp, */
1911         /* and adding the specified offset to the new sp */
1912         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d),%%eax\n", STACKOFFSET );
1913         fprintf( outfile, "\tsubl 12(%%ebx),%%eax\n" ); /* Get the offset */
1914 #ifdef __svr4__
1915         fprintf( outfile,"\tdata16\n");
1916 #endif
1917         fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
1918         fprintf( outfile, "\tmovl %%eax,%%esp\n" );
1919         fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
1920
1921     fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%cx\n", FSOFFSET );
1922     fprintf( outfile, "\tmovw %%cx, %%fs\n" );
1923
1924         /* Get the registers. ebx is handled later on. */
1925
1926         fprintf( outfile, "\tmovl 8(%%ebx),%%ebx\n" );
1927         fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs) );
1928         fprintf( outfile, "\tmovw %%ax,%%es\n" );
1929         fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp) );
1930         fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax) );
1931         fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx) );
1932         fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx) );
1933         fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi) );
1934         fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi) );
1935
1936         /* Push the return address 
1937          * With sreg suffix, we push 16:16 address (normal lret)
1938          * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
1939          */
1940         if (reg_func == 1)
1941             fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_long\n" );
1942         else 
1943         {
1944             fprintf( outfile, "\tpushw $0\n" );
1945             fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_eax+2\n" );
1946             fprintf( outfile, "\tpushw $0\n" );
1947             fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_eax\n" );
1948         }
1949
1950         /* Push the called routine address */
1951
1952         fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
1953         fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
1954
1955         /* Get the 16-bit ds */
1956
1957         fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
1958         /* Get ebx from the 32-bit stack */
1959         fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(Ebx) );
1960         fprintf( outfile, "\tpopl %%ds\n" );
1961     }
1962     else  /* not a register function */
1963     {
1964         int pos = 12;  /* first argument position */
1965
1966         /* Switch to the 16-bit stack */
1967 #ifdef __svr4__
1968         fprintf( outfile,"\tdata16\n");
1969 #endif
1970         fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
1971         fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
1972         fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
1973
1974     fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%cx\n", FSOFFSET );
1975     fprintf( outfile, "\tmovw %%cx, %%fs\n" );
1976
1977         /* Make %bp point to the previous stackframe (built by CallFrom16) */
1978         fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1979         fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n",
1980                  STRUCTOFFSET(STACK16FRAME,bp) );
1981
1982         /* Transfer the arguments */
1983
1984         while (*args)
1985         {
1986             switch(*args++)
1987             {
1988             case 'w': /* word */
1989                 fprintf( outfile, "\tpushw %d(%%ebx)\n", pos );
1990                 break;
1991             case 'l': /* long */
1992                 fprintf( outfile, "\tpushl %d(%%ebx)\n", pos );
1993                 break;
1994             default:
1995                 fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
1996                         args[-1] );
1997             }
1998             pos += 4;
1999         }
2000
2001         /* Push the return address */
2002
2003         fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_%s\n",
2004                  short_ret ? "word" : "long" );
2005
2006         /* Push the called routine address */
2007
2008         fprintf( outfile, "\tpushl 8(%%ebx)\n" );
2009
2010         /* Set %ds and %es (and %ax just in case) equal to %ss */
2011
2012         fprintf( outfile, "\tmovw %%ss,%%ax\n" );
2013         fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2014         fprintf( outfile, "\tmovw %%ax,%%es\n" );
2015     }
2016
2017     /* Jump to the called routine */
2018
2019     fprintf( outfile, "\t.byte 0x66\n" );
2020     fprintf( outfile, "\tlret\n" );
2021 }
2022
2023
2024 /*******************************************************************
2025  *         BuildRet16Func
2026  *
2027  * Build the return code for 16-bit callbacks
2028  */
2029 static void BuildRet16Func( FILE *outfile )
2030 {
2031     fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_word\n" );
2032     fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_long\n" );
2033     fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_eax\n" );
2034
2035     fprintf( outfile, PREFIX "CALLTO16_Ret_word:\n" );
2036     fprintf( outfile, "\txorl %%edx,%%edx\n" );
2037
2038     /* Put return value into %eax */
2039
2040     fprintf( outfile, PREFIX "CALLTO16_Ret_long:\n" );
2041     fprintf( outfile, "\tshll $16,%%edx\n" );
2042     fprintf( outfile, "\tmovw %%ax,%%dx\n" );
2043     fprintf( outfile, "\tmovl %%edx,%%eax\n" );
2044     fprintf( outfile, PREFIX "CALLTO16_Ret_eax:\n" );
2045
2046     /* Restore 32-bit segment registers */
2047
2048     fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
2049 #ifdef __svr4__
2050     fprintf( outfile, "\tdata16\n");
2051 #endif
2052     fprintf( outfile, "\tmovw %%bx,%%ds\n" );
2053 #ifdef __svr4__
2054     fprintf( outfile, "\tdata16\n");
2055 #endif
2056     fprintf( outfile, "\tmovw %%bx,%%es\n" );
2057
2058     fprintf( outfile, "\tmovw %%fs, %%cx\n" );
2059     fprintf( outfile, "\tmovw " PREFIX "CALLTO16_Current_fs,%%fs\n" );
2060     fprintf( outfile, "\t.byte 0x64\n\tmovw %%cx,(%d)\n", FSOFFSET );
2061
2062     /* Restore the 32-bit stack */
2063
2064 #ifdef __svr4__
2065     fprintf( outfile, "\tdata16\n");
2066 #endif
2067     fprintf( outfile, "\tmovw %%bx,%%ss\n" );
2068     fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
2069     fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2070
2071     /* Restore the 32-bit registers */
2072
2073     fprintf( outfile, "\tpopl %%edi\n" );
2074     fprintf( outfile, "\tpopl %%esi\n" );
2075     fprintf( outfile, "\tpopl %%edx\n" );
2076     fprintf( outfile, "\tpopl %%ecx\n" );
2077     fprintf( outfile, "\tpopl %%ebx\n" );
2078
2079     /* Return to caller */
2080
2081     fprintf( outfile, "\tlret\n" );
2082
2083     /* Declare the return address variables */
2084
2085     fprintf( outfile, "\t.data\n" );
2086     fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_word\n" );
2087     fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_long\n" );
2088     fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_eax\n" );
2089     fprintf( outfile, PREFIX "CALLTO16_RetAddr_word:\t.long 0\n" );
2090     fprintf( outfile, PREFIX "CALLTO16_RetAddr_long:\t.long 0\n" );
2091     fprintf( outfile, PREFIX "CALLTO16_RetAddr_eax:\t.long 0\n" );
2092     fprintf( outfile, "\t.text\n" );
2093 }
2094
2095
2096 /*******************************************************************
2097  *         BuildCallTo32LargeStack
2098  *
2099  * Build the function used to switch to the original 32-bit stack
2100  * before calling a 32-bit function from 32-bit code. This is used for
2101  * functions that need a large stack, like X bitmaps functions.
2102  *
2103  * The generated function has the following prototype:
2104  *   int xxx( int (*func)(), void *arg );
2105  *
2106  * The pointer to the function can be retrieved by calling CALL32_Init,
2107  * which also takes care of saving the current 32-bit stack pointer.
2108  *
2109  * Stack layout:
2110  *   ...     ...
2111  * (ebp+12)  arg
2112  * (ebp+8)   func
2113  * (ebp+4)   ret addr
2114  * (ebp)     ebp
2115  */
2116 static void BuildCallTo32LargeStack( FILE *outfile )
2117 {
2118     /* Initialization function */
2119
2120     fprintf( outfile, "\n\t.align 4\n" );
2121 #ifdef USE_STABS
2122     fprintf( outfile, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX "CALL32_Init\n");
2123 #endif
2124     fprintf( outfile, "\t.globl " PREFIX "CALL32_Init\n" );
2125     fprintf( outfile, "\t.type " PREFIX "CALL32_Init,@function\n" );
2126     fprintf( outfile, PREFIX "CALL32_Init:\n" );
2127     fprintf( outfile, "\tleal -256(%%esp),%%eax\n" );
2128     fprintf( outfile, "\tmovl %%eax,CALL32_Original32_esp\n" );
2129     fprintf( outfile, "\tmovl $CALL32_LargeStack,%%eax\n" );
2130     fprintf( outfile, "\tret\n" );
2131
2132     /* Function header */
2133
2134     fprintf( outfile, "\n\t.align 4\n" );
2135 #ifdef USE_STABS
2136     fprintf( outfile, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2137 #endif
2138     fprintf( outfile, "CALL32_LargeStack:\n" );
2139     
2140     /* Entry code */
2141
2142     fprintf( outfile, "\tpushl %%ebp\n" );
2143     fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2144
2145     /* Switch to the original 32-bit stack pointer */
2146
2147     fprintf( outfile, "\tmovl CALL32_Original32_esp, %%esp\n" );
2148
2149     /* Transfer the argument and call the function */
2150
2151     fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2152     fprintf( outfile, "\tcall *8(%%ebp)\n" );
2153
2154     /* Restore registers and return */
2155
2156     fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2157     fprintf( outfile, "\tpopl %%ebp\n" );
2158     fprintf( outfile, "\tret\n" );
2159
2160     /* Data */
2161
2162     fprintf( outfile, "\t.data\n" );
2163     fprintf( outfile, "CALL32_Original32_esp:\t.long 0\n" );
2164     fprintf( outfile, "\t.text\n" );
2165 }
2166
2167
2168 /*******************************************************************
2169  *         BuildCallFrom32Regs
2170  *
2171  * Build a 32-bit-to-Wine call-back function for a 'register' function.
2172  * 'args' is the number of dword arguments.
2173  *
2174  * Stack layout:
2175  *   ...     ...
2176  * (esp+336) ret addr (or relay addr when debugging(relay) is on)
2177  * (esp+332) entry point
2178  * (esp+204) buffer area to allow stack frame manipulation
2179  * (esp+0)   CONTEXT struct
2180  */
2181 static void BuildCallFrom32Regs( FILE *outfile )
2182 {
2183 #define STACK_SPACE 128
2184
2185     /* Function header */
2186
2187     fprintf( outfile, "\n\t.align 4\n" );
2188 #ifdef USE_STABS
2189     fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
2190 #endif
2191     fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
2192     fprintf( outfile, PREFIX "CALL32_Regs:\n" );
2193
2194     /* Allocate some buffer space on the stack */
2195    
2196     fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
2197     
2198     /* Build the context structure */
2199
2200     fprintf( outfile, "\tpushw $0\n" );
2201     fprintf( outfile, "\t.byte 0x66\n\tpushl %%ss\n" );
2202     fprintf( outfile, "\tpushl %%eax\n" );  /* %esp place holder */
2203     fprintf( outfile, "\tpushfl\n" );
2204     fprintf( outfile, "\tpushw $0\n" );
2205     fprintf( outfile, "\t.byte 0x66\n\tpushl %%cs\n" );
2206     fprintf( outfile, "\tpushl %d(%%esp)\n", 16+STACK_SPACE+4 );  /* %eip at time of call */
2207     fprintf( outfile, "\tpushl %%ebp\n" );
2208
2209     fprintf( outfile, "\tpushl %%eax\n" );
2210     fprintf( outfile, "\tpushl %%ecx\n" );
2211     fprintf( outfile, "\tpushl %%edx\n" );
2212     fprintf( outfile, "\tpushl %%ebx\n" );
2213     fprintf( outfile, "\tpushl %%esi\n" );
2214     fprintf( outfile, "\tpushl %%edi\n" );
2215
2216     fprintf( outfile, "\txorl %%eax,%%eax\n" );
2217     fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2218     fprintf( outfile, "\tpushl %%eax\n" );
2219     fprintf( outfile, "\tmovw %%es,%%ax\n" );
2220     fprintf( outfile, "\tpushl %%eax\n" );
2221     fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2222     fprintf( outfile, "\tpushl %%eax\n" );
2223     fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2224     fprintf( outfile, "\tpushl %%eax\n" );
2225
2226     fprintf( outfile, "\tleal -%d(%%esp),%%esp\n",
2227              sizeof(FLOATING_SAVE_AREA) + 6 * sizeof(DWORD) /* DR regs */ );
2228     fprintf( outfile, "\tpushl $0x0001001f\n" );  /* ContextFlags */
2229
2230     fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2231
2232     fprintf( outfile, "\tleal %d(%%esp),%%eax\n",
2233              sizeof(CONTEXT) + STACK_SPACE + 4 ); /* %esp at time of call */
2234     fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Esp) );
2235
2236     fprintf( outfile, "\tcall " PREFIX "RELAY_CallFrom32Regs\n" );
2237
2238     /* Restore the context structure */
2239
2240     fprintf( outfile, "\tfrstor %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2241
2242     /* Store %eip value onto the new stack */
2243
2244     fprintf( outfile, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Eip) );
2245     fprintf( outfile, "\tmovl %d(%%esp),%%ebx\n", CONTEXTOFFSET(Esp) );
2246     fprintf( outfile, "\tmovl %%eax,0(%%ebx)\n" );
2247
2248     /* Restore all registers */
2249
2250     fprintf( outfile, "\tleal %d(%%esp),%%esp\n",
2251              sizeof(FLOATING_SAVE_AREA) + 7 * sizeof(DWORD) );
2252     fprintf( outfile, "\tpopl %%eax\n" );
2253     fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2254     fprintf( outfile, "\tpopl %%eax\n" );
2255     fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2256     fprintf( outfile, "\tpopl %%eax\n" );
2257     fprintf( outfile, "\tmovw %%ax,%%es\n" );
2258     fprintf( outfile, "\tpopl %%eax\n" );
2259     fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2260
2261     fprintf( outfile, "\tpopl %%edi\n" );
2262     fprintf( outfile, "\tpopl %%esi\n" );
2263     fprintf( outfile, "\tpopl %%ebx\n" );
2264     fprintf( outfile, "\tpopl %%edx\n" );
2265     fprintf( outfile, "\tpopl %%ecx\n" );
2266     fprintf( outfile, "\tpopl %%eax\n" );
2267     fprintf( outfile, "\tpopl %%ebp\n" );
2268     fprintf( outfile, "\tleal 8(%%esp),%%esp\n" );  /* skip %eip and %cs */
2269     fprintf( outfile, "\tpopfl\n" );
2270     fprintf( outfile, "\tpopl %%esp\n" );
2271     fprintf( outfile, "\tret\n" );
2272
2273 #undef STACK_SPACE
2274 }
2275
2276
2277 /*******************************************************************
2278  *         BuildSpec
2279  *
2280  * Build the spec files
2281  */
2282 static int BuildSpec( FILE *outfile, int argc, char *argv[] )
2283 {
2284     int i;
2285     for (i = 2; i < argc; i++)
2286         if (BuildSpecFile( outfile, argv[i] ) < 0) return -1;
2287     return 0;
2288 }
2289
2290
2291 /*******************************************************************
2292  *         BuildCallFrom16
2293  *
2294  * Build the 16-bit-to-Wine callbacks
2295  */
2296 static int BuildCallFrom16( FILE *outfile, char * outname, int argc, char *argv[] )
2297 {
2298     int i;
2299     char buffer[1024];
2300
2301     /* File header */
2302
2303     fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2304     fprintf( outfile, "\t.text\n" );
2305
2306 #ifdef USE_STABS
2307     fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2308     getcwd(buffer, sizeof(buffer));
2309
2310     /*
2311      * The stabs help the internal debugger as they are an indication that it
2312      * is sensible to step into a thunk/trampoline.
2313      */
2314     fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2315     fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2316     fprintf( outfile, "\t.text\n" );
2317     fprintf( outfile, "\t.align 4\n" );
2318     fprintf( outfile, "Code_Start:\n\n" );
2319 #endif
2320
2321     /* Build the callback functions */
2322
2323     for (i = 2; i < argc; i++) BuildCallFrom16Func( outfile, argv[i] );
2324
2325     /* Build the thunk callback function */
2326
2327     BuildCallFrom16Func( outfile, "t_long_" );
2328
2329     /* Output the argument debugging strings */
2330
2331     if (debugging)
2332     {
2333         fprintf( outfile, "/* Argument strings */\n" );
2334         for (i = 2; i < argc; i++)
2335         {
2336             fprintf( outfile, "Profile_%s:\t", argv[i] );
2337             fprintf( outfile, STRING " \"%s\\0\"\n", argv[i] + 7 );
2338         }
2339     }
2340
2341 #ifdef USE_STABS
2342     fprintf( outfile, "\t.text\n");
2343     fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2344     fprintf( outfile, ".Letext:\n");
2345 #endif
2346
2347     return 0;
2348 }
2349
2350
2351 /*******************************************************************
2352  *         BuildCallTo16
2353  *
2354  * Build the Wine-to-16-bit callbacks
2355  */
2356 static int BuildCallTo16( FILE *outfile, char * outname, int argc, char *argv[] )
2357 {
2358     char buffer[1024];
2359     FILE *infile;
2360
2361     if (argc > 2)
2362     {
2363         infile = fopen( argv[2], "r" );
2364         if (!infile)
2365         {
2366             perror( argv[2] );
2367             exit( 1 );
2368         }
2369     }
2370     else infile = stdin;
2371
2372     /* File header */
2373
2374     fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2375     fprintf( outfile, "\t.text\n" );
2376
2377 #ifdef USE_STABS
2378     fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2379     getcwd(buffer, sizeof(buffer));
2380
2381     /*
2382      * The stabs help the internal debugger as they are an indication that it
2383      * is sensible to step into a thunk/trampoline.
2384      */
2385     fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2386     fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2387     fprintf( outfile, "\t.text\n" );
2388     fprintf( outfile, "\t.align 4\n" );
2389     fprintf( outfile, "Code_Start:\n\n" );
2390 #endif
2391
2392     fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Start\n" );
2393     fprintf( outfile, PREFIX "CALLTO16_Start:\n" );
2394
2395     /* Build the callback functions */
2396
2397     while (fgets( buffer, sizeof(buffer), infile ))
2398     {
2399         if (strstr( buffer, "### start build ###" )) break;
2400     }
2401     while (fgets( buffer, sizeof(buffer), infile ))
2402     {
2403         char *p = strstr( buffer, "CallTo16_" );
2404         if (p)
2405         {
2406             char *profile = p + strlen( "CallTo16_" );
2407             p = profile;
2408             while ((*p == '_') || isalpha(*p)) p++;
2409             *p = '\0';
2410             BuildCallTo16Func( outfile, profile );
2411         }
2412         if (strstr( buffer, "### stop build ###" )) break;
2413     }
2414
2415     /* Output the 16-bit return code */
2416
2417     BuildRet16Func( outfile );
2418
2419     fprintf( outfile, "\t.globl " PREFIX "CALLTO16_End\n" );
2420     fprintf( outfile, PREFIX "CALLTO16_End:\n" );
2421
2422 #ifdef USE_STABS
2423     fprintf( outfile, "\t.text\n");
2424     fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2425     fprintf( outfile, ".Letext:\n");
2426 #endif
2427
2428     fclose( infile );
2429     return 0;
2430 }
2431
2432
2433 /*******************************************************************
2434  *         BuildCall32
2435  *
2436  * Build the 32-bit callbacks
2437  */
2438 static int BuildCall32( FILE *outfile, char * outname )
2439 {
2440     char buffer[1024];
2441
2442     /* File header */
2443
2444     fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2445     fprintf( outfile, "\t.text\n" );
2446
2447 #ifdef __i386__
2448
2449 #ifdef USE_STABS
2450     fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2451     getcwd(buffer, sizeof(buffer));
2452
2453     /*
2454      * The stabs help the internal debugger as they are an indication that it
2455      * is sensible to step into a thunk/trampoline.
2456      */
2457     fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2458     fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2459     fprintf( outfile, "\t.text\n" );
2460     fprintf( outfile, "\t.align 4\n" );
2461     fprintf( outfile, "Code_Start:\n" );
2462 #endif
2463
2464     /* Build the 32-bit large stack callback */
2465
2466     BuildCallTo32LargeStack( outfile );
2467
2468     /* Build the register callback function */
2469
2470     BuildCallFrom32Regs( outfile );
2471
2472 #ifdef USE_STABS
2473     fprintf( outfile, "\t.text\n");
2474     fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2475     fprintf( outfile, ".Letext:\n");
2476 #endif
2477
2478 #else  /* __i386__ */
2479
2480     /* Just to avoid an empty file */
2481     fprintf( outfile, "\t.long 0\n" );
2482
2483 #endif  /* __i386__ */
2484     return 0;
2485 }
2486
2487
2488 /*******************************************************************
2489  *         usage
2490  */
2491 static void usage(void)
2492 {
2493     fprintf( stderr,
2494              "usage: build [-o outfile] -spec SPECNAMES\n"
2495              "       build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2496              "       build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2497              "       build [-o outfile] -call32\n" );
2498     exit(1);
2499 }
2500
2501
2502 /*******************************************************************
2503  *         main
2504  */
2505 int main(int argc, char **argv)
2506 {
2507     char *outname = NULL;
2508     FILE *outfile = stdout;
2509     int res = -1;
2510
2511     if (argc < 2) usage();
2512
2513     if (!strcmp( argv[1], "-o" ))
2514     {
2515         outname = argv[2];
2516         argv += 2;
2517         argc -= 2;
2518         if (argc < 2) usage();
2519         if (!(outfile = fopen( outname, "w" )))
2520         {
2521             fprintf( stderr, "Unable to create output file '%s'\n", outname );
2522             exit(1);
2523         }
2524     }
2525
2526     /* Retrieve the selector values; this assumes that we are building
2527      * the asm files on the platform that will also run them. Probably
2528      * a safe assumption to make.
2529      */
2530     GET_CS( Code_Selector );
2531     GET_DS( Data_Selector );
2532
2533     if (!strcmp( argv[1], "-spec" ))
2534         res = BuildSpec( outfile, argc, argv );
2535     else if (!strcmp( argv[1], "-callfrom16" ))
2536         res = BuildCallFrom16( outfile, outname, argc, argv );
2537     else if (!strcmp( argv[1], "-callto16" ))
2538         res = BuildCallTo16( outfile, outname, argc, argv );
2539     else if (!strcmp( argv[1], "-call32" ))
2540         res = BuildCall32( outfile, outname );
2541     else
2542     {
2543         fclose( outfile );
2544         unlink( outname );
2545         usage();
2546     }
2547
2548     fclose( outfile );
2549     if (res < 0)
2550     {
2551         unlink( outname );
2552         return 1;
2553     }
2554     return 0;
2555 }