oleaut32 should not import functions from olepro32.
[wine.git] / tools / build.c
blob6228a4231138af48a195d1e99ab04d9ec09f2f49
1 /*
2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Martin von Loewis
4 * Copyright 1995, 1996, 1997 Alexandre Julliard
5 * Copyright 1997 Eric Youngdale
6 * Copyright 1999 Ulrich Weigand
7 */
9 #include "config.h"
11 #include <assert.h>
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <unistd.h>
19 #include "winbase.h"
20 #include "winnt.h"
21 #include "module.h"
22 #include "neexe.h"
23 #include "stackframe.h"
24 #include "builtin16.h"
25 #include "thread.h"
27 #ifdef NEED_UNDERSCORE_PREFIX
28 # define PREFIX "_"
29 #else
30 # define PREFIX
31 #endif
33 #ifdef HAVE_ASM_STRING
34 # define STRING ".string"
35 #else
36 # define STRING ".ascii"
37 #endif
39 #if defined(__GNUC__) && !defined(__svr4__)
40 # define USE_STABS
41 #else
42 # undef USE_STABS
43 #endif
45 #ifdef __i386__
46 extern WORD __get_cs(void);
47 extern WORD __get_ds(void);
48 __ASM_GLOBAL_FUNC( __get_cs, "movl %cs,%eax\n\tret" );
49 __ASM_GLOBAL_FUNC( __get_ds, "movl %ds,%eax\n\tret" );
50 #else
51 static inline WORD __get_cs(void) { return 0; }
52 static inline WORD __get_ds(void) { return 0; }
53 #endif
55 typedef enum
57 TYPE_BYTE, /* byte variable (Win16) */
58 TYPE_WORD, /* word variable (Win16) */
59 TYPE_LONG, /* long variable (Win16) */
60 TYPE_PASCAL_16, /* pascal function with 16-bit return (Win16) */
61 TYPE_PASCAL, /* pascal function with 32-bit return (Win16) */
62 TYPE_ABS, /* absolute value (Win16) */
63 TYPE_REGISTER, /* register function */
64 TYPE_INTERRUPT, /* interrupt handler function (Win16) */
65 TYPE_STUB, /* unimplemented stub */
66 TYPE_STDCALL, /* stdcall function (Win32) */
67 TYPE_CDECL, /* cdecl function (Win32) */
68 TYPE_VARARGS, /* varargs function (Win32) */
69 TYPE_EXTERN, /* external symbol (Win32) */
70 TYPE_FORWARD, /* forwarded function (Win32) */
71 TYPE_NBTYPES
72 } ORD_TYPE;
74 static const char * const TypeNames[TYPE_NBTYPES] =
76 "byte", /* TYPE_BYTE */
77 "word", /* TYPE_WORD */
78 "long", /* TYPE_LONG */
79 "pascal16", /* TYPE_PASCAL_16 */
80 "pascal", /* TYPE_PASCAL */
81 "equate", /* TYPE_ABS */
82 "register", /* TYPE_REGISTER */
83 "interrupt", /* TYPE_INTERRUPT */
84 "stub", /* TYPE_STUB */
85 "stdcall", /* TYPE_STDCALL */
86 "cdecl", /* TYPE_CDECL */
87 "varargs", /* TYPE_VARARGS */
88 "extern", /* TYPE_EXTERN */
89 "forward" /* TYPE_FORWARD */
92 #define MAX_ORDINALS 2048
93 #define MAX_IMPORTS 16
95 /* Callback function used for stub functions */
96 #define STUB_CALLBACK \
97 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
99 typedef enum
101 SPEC_INVALID,
102 SPEC_WIN16,
103 SPEC_WIN32
104 } SPEC_TYPE;
106 typedef enum
108 SPEC_MODE_DLL,
109 SPEC_MODE_GUIEXE,
110 SPEC_MODE_CUIEXE
111 } SPEC_MODE;
113 typedef struct
115 int n_values;
116 int *values;
117 } ORD_VARIABLE;
119 typedef struct
121 int n_args;
122 char arg_types[32];
123 char link_name[80];
124 } ORD_FUNCTION;
126 typedef struct
128 int value;
129 } ORD_ABS;
131 typedef struct
133 char link_name[80];
134 } ORD_EXTERN;
136 typedef struct
138 char link_name[80];
139 } ORD_FORWARD;
141 typedef struct
143 ORD_TYPE type;
144 int ordinal;
145 int offset;
146 int lineno;
147 char name[80];
148 union
150 ORD_VARIABLE var;
151 ORD_FUNCTION func;
152 ORD_ABS abs;
153 ORD_EXTERN ext;
154 ORD_FORWARD fwd;
155 } u;
156 } ORDDEF;
158 static ORDDEF EntryPoints[MAX_ORDINALS];
159 static ORDDEF *Ordinals[MAX_ORDINALS];
160 static ORDDEF *Names[MAX_ORDINALS];
162 static SPEC_TYPE SpecType = SPEC_INVALID;
163 static SPEC_MODE SpecMode = SPEC_MODE_DLL;
164 static char DLLName[80];
165 static char DLLFileName[80];
166 static int Limit = 0;
167 static int Base = MAX_ORDINALS;
168 static int DLLHeapSize = 0;
169 static FILE *SpecFp;
170 static WORD Code_Selector, Data_Selector;
171 static char DLLInitFunc[80];
172 static char *DLLImports[MAX_IMPORTS];
173 static char rsrc_name[80];
174 static int nb_imports;
175 static int nb_entry_points;
176 static int nb_names;
177 static const char *input_file_name;
178 static const char *output_file_name;
180 char *ParseBuffer = NULL;
181 char *ParseNext;
182 char ParseSaveChar;
183 int Line;
185 static int UsePIC = 0;
187 static int debugging = 1;
189 /* Offset of a structure field relative to the start of the struct */
190 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
192 /* Offset of register relative to the start of the CONTEXT struct */
193 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT86,reg)
195 /* Offset of register relative to the start of the STACK16FRAME struct */
196 #define STACK16OFFSET(reg) STRUCTOFFSET(STACK16FRAME,reg)
198 /* Offset of register relative to the start of the STACK32FRAME struct */
199 #define STACK32OFFSET(reg) STRUCTOFFSET(STACK32FRAME,reg)
201 /* Offset of the stack pointer relative to %fs:(0) */
202 #define STACKOFFSET (STRUCTOFFSET(TEB,cur_stack))
204 static void BuildCallFrom16Func( FILE *outfile, char *profile, char *prefix, int local );
206 static void *xmalloc (size_t size)
208 void *res;
210 res = malloc (size ? size : 1);
211 if (res == NULL)
213 fprintf (stderr, "Virtual memory exhausted.\n");
214 if (output_file_name) unlink( output_file_name );
215 exit (1);
217 return res;
221 static void *xrealloc (void *ptr, size_t size)
223 void *res = realloc (ptr, size);
224 if (res == NULL)
226 fprintf (stderr, "Virtual memory exhausted.\n");
227 if (output_file_name) unlink( output_file_name );
228 exit (1);
230 return res;
233 static char *xstrdup( const char *str )
235 char *res = strdup( str );
236 if (!res)
238 fprintf (stderr, "Virtual memory exhausted.\n");
239 if (output_file_name) unlink( output_file_name );
240 exit (1);
242 return res;
245 static void fatal_error( const char *msg, ... )
247 va_list valist;
248 va_start( valist, msg );
249 fprintf( stderr, "%s:%d: ", input_file_name, Line );
250 vfprintf( stderr, msg, valist );
251 va_end( valist );
252 if (output_file_name) unlink( output_file_name );
253 exit(1);
256 static int IsNumberString(char *s)
258 while (*s != '\0')
259 if (!isdigit(*s++))
260 return 0;
262 return 1;
265 static char *strupper(char *s)
267 char *p;
269 for(p = s; *p != '\0'; p++)
270 *p = toupper(*p);
272 return s;
275 static char * GetTokenInLine(void)
277 char *p;
278 char *token;
280 if (ParseNext != ParseBuffer)
282 if (ParseSaveChar == '\0')
283 return NULL;
284 *ParseNext = ParseSaveChar;
288 * Remove initial white space.
290 for (p = ParseNext; isspace(*p); p++)
293 if ((*p == '\0') || (*p == '#'))
294 return NULL;
297 * Find end of token.
299 token = p++;
300 if (*token != '(' && *token != ')')
301 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
302 p++;
304 ParseSaveChar = *p;
305 ParseNext = p;
306 *p = '\0';
308 return token;
311 static char * GetToken(void)
313 char *token;
315 if (ParseBuffer == NULL)
317 ParseBuffer = xmalloc(512);
318 ParseNext = ParseBuffer;
319 while (1)
321 Line++;
322 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
323 return NULL;
324 if (ParseBuffer[0] != '#')
325 break;
329 while ((token = GetTokenInLine()) == NULL)
331 ParseNext = ParseBuffer;
332 while (1)
334 Line++;
335 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
336 return NULL;
337 if (ParseBuffer[0] != '#')
338 break;
342 return token;
346 static int name_compare( const void *name1, const void *name2 )
348 ORDDEF *odp1 = *(ORDDEF **)name1;
349 ORDDEF *odp2 = *(ORDDEF **)name2;
350 return strcmp( odp1->name, odp2->name );
353 /*******************************************************************
354 * AssignOrdinals
356 * Assign ordinals to all entry points.
358 static void AssignOrdinals(void)
360 int i, ordinal;
362 if ( !nb_names ) return;
364 /* sort the list of names */
365 qsort( Names, nb_names, sizeof(Names[0]), name_compare );
367 /* check for duplicate names */
368 for (i = 0; i < nb_names - 1; i++)
370 if (!strcmp( Names[i]->name, Names[i+1]->name ))
372 Line = MAX( Names[i]->lineno, Names[i+1]->lineno );
373 fatal_error( "'%s' redefined (previous definition at line %d)\n",
374 Names[i]->name, MIN( Names[i]->lineno, Names[i+1]->lineno ) );
378 /* start assigning from Base, or from 1 if no ordinal defined yet */
379 if (Base == MAX_ORDINALS) Base = 1;
380 for (i = 0, ordinal = Base; i < nb_names; i++)
382 if (Names[i]->ordinal != -1) continue; /* already has an ordinal */
383 while (Ordinals[ordinal]) ordinal++;
384 if (ordinal >= MAX_ORDINALS)
386 Line = Names[i]->lineno;
387 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
389 Names[i]->ordinal = ordinal;
390 Ordinals[ordinal] = Names[i];
392 if (ordinal > Limit) Limit = ordinal;
396 /*******************************************************************
397 * ParseVariable
399 * Parse a variable definition.
401 static void ParseVariable( ORDDEF *odp )
403 char *endptr;
404 int *value_array;
405 int n_values;
406 int value_array_size;
408 char *token = GetToken();
409 if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
411 n_values = 0;
412 value_array_size = 25;
413 value_array = xmalloc(sizeof(*value_array) * value_array_size);
415 while ((token = GetToken()) != NULL)
417 if (*token == ')')
418 break;
420 value_array[n_values++] = strtol(token, &endptr, 0);
421 if (n_values == value_array_size)
423 value_array_size += 25;
424 value_array = xrealloc(value_array,
425 sizeof(*value_array) * value_array_size);
428 if (endptr == NULL || *endptr != '\0')
429 fatal_error( "Expected number value, got '%s'\n", token );
432 if (token == NULL)
433 fatal_error( "End of file in variable declaration\n" );
435 odp->u.var.n_values = n_values;
436 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
440 /*******************************************************************
441 * ParseExportFunction
443 * Parse a function definition.
445 static void ParseExportFunction( ORDDEF *odp )
447 char *token;
448 int i;
450 switch(SpecType)
452 case SPEC_WIN16:
453 if (odp->type == TYPE_STDCALL)
454 fatal_error( "'stdcall' not supported for Win16\n" );
455 if (odp->type == TYPE_VARARGS)
456 fatal_error( "'varargs' not supported for Win16\n" );
457 break;
458 case SPEC_WIN32:
459 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
460 fatal_error( "'pascal' not supported for Win32\n" );
461 break;
462 default:
463 break;
466 token = GetToken();
467 if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
469 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
471 token = GetToken();
472 if (*token == ')')
473 break;
475 if (!strcmp(token, "word"))
476 odp->u.func.arg_types[i] = 'w';
477 else if (!strcmp(token, "s_word"))
478 odp->u.func.arg_types[i] = 's';
479 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
480 odp->u.func.arg_types[i] = 'l';
481 else if (!strcmp(token, "ptr"))
482 odp->u.func.arg_types[i] = 'p';
483 else if (!strcmp(token, "str"))
484 odp->u.func.arg_types[i] = 't';
485 else if (!strcmp(token, "wstr"))
486 odp->u.func.arg_types[i] = 'W';
487 else if (!strcmp(token, "segstr"))
488 odp->u.func.arg_types[i] = 'T';
489 else if (!strcmp(token, "double"))
491 odp->u.func.arg_types[i++] = 'l';
492 odp->u.func.arg_types[i] = 'l';
494 else fatal_error( "Unknown variable type '%s'\n", token );
496 if (SpecType == SPEC_WIN32)
498 if (strcmp(token, "long") &&
499 strcmp(token, "ptr") &&
500 strcmp(token, "str") &&
501 strcmp(token, "wstr") &&
502 strcmp(token, "double"))
504 fatal_error( "Type '%s' not supported for Win32\n", token );
508 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
509 fatal_error( "Too many arguments\n" );
511 odp->u.func.arg_types[i] = '\0';
512 if ((odp->type == TYPE_STDCALL) && !i)
513 odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
514 strcpy(odp->u.func.link_name, GetToken());
518 /*******************************************************************
519 * ParseEquate
521 * Parse an 'equate' definition.
523 static void ParseEquate( ORDDEF *odp )
525 char *endptr;
527 char *token = GetToken();
528 int value = strtol(token, &endptr, 0);
529 if (endptr == NULL || *endptr != '\0')
530 fatal_error( "Expected number value, got '%s'\n", token );
531 if (SpecType == SPEC_WIN32)
532 fatal_error( "'equate' not supported for Win32\n" );
533 odp->u.abs.value = value;
537 /*******************************************************************
538 * ParseStub
540 * Parse a 'stub' definition.
542 static void ParseStub( ORDDEF *odp )
544 odp->u.func.arg_types[0] = '\0';
545 strcpy( odp->u.func.link_name, STUB_CALLBACK );
549 /*******************************************************************
550 * ParseInterrupt
552 * Parse an 'interrupt' definition.
554 static void ParseInterrupt( ORDDEF *odp )
556 char *token;
558 if (SpecType == SPEC_WIN32)
559 fatal_error( "'interrupt' not supported for Win32\n" );
561 token = GetToken();
562 if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
564 token = GetToken();
565 if (*token != ')') fatal_error( "Expected ')' got '%s'\n", token );
567 odp->u.func.arg_types[0] = '\0';
568 strcpy( odp->u.func.link_name, GetToken() );
572 /*******************************************************************
573 * ParseExtern
575 * Parse an 'extern' definition.
577 static void ParseExtern( ORDDEF *odp )
579 if (SpecType == SPEC_WIN16) fatal_error( "'extern' not supported for Win16\n" );
580 strcpy( odp->u.ext.link_name, GetToken() );
584 /*******************************************************************
585 * ParseForward
587 * Parse a 'forward' definition.
589 static void ParseForward( ORDDEF *odp )
591 if (SpecType == SPEC_WIN16) fatal_error( "'forward' not supported for Win16\n" );
592 strcpy( odp->u.fwd.link_name, GetToken() );
596 /*******************************************************************
597 * ParseOrdinal
599 * Parse an ordinal definition.
601 static void ParseOrdinal(int ordinal)
603 char *token;
605 ORDDEF *odp = &EntryPoints[nb_entry_points++];
607 if (!(token = GetToken())) fatal_error( "Expected type after ordinal\n" );
609 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
610 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
611 break;
613 if (odp->type >= TYPE_NBTYPES)
614 fatal_error( "Expected type after ordinal, found '%s' instead\n", token );
616 if (!(token = GetToken())) fatal_error( "Expected name after type\n" );
618 strcpy( odp->name, token );
619 odp->lineno = Line;
620 odp->ordinal = ordinal;
622 switch(odp->type)
624 case TYPE_BYTE:
625 case TYPE_WORD:
626 case TYPE_LONG:
627 ParseVariable( odp );
628 break;
629 case TYPE_REGISTER:
630 ParseExportFunction( odp );
631 #ifndef __i386__
632 /* ignore Win32 'register' routines on non-Intel archs */
633 if (SpecType == SPEC_WIN32)
635 nb_entry_points--;
636 return;
638 #endif
639 break;
640 case TYPE_PASCAL_16:
641 case TYPE_PASCAL:
642 case TYPE_STDCALL:
643 case TYPE_VARARGS:
644 case TYPE_CDECL:
645 ParseExportFunction( odp );
646 break;
647 case TYPE_INTERRUPT:
648 ParseInterrupt( odp );
649 break;
650 case TYPE_ABS:
651 ParseEquate( odp );
652 break;
653 case TYPE_STUB:
654 ParseStub( odp );
655 break;
656 case TYPE_EXTERN:
657 ParseExtern( odp );
658 break;
659 case TYPE_FORWARD:
660 ParseForward( odp );
661 break;
662 default:
663 assert( 0 );
666 if (ordinal != -1)
668 if (ordinal >= MAX_ORDINALS) fatal_error( "Ordinal number %d too large\n", ordinal );
669 if (ordinal > Limit) Limit = ordinal;
670 if (ordinal < Base) Base = ordinal;
671 odp->ordinal = ordinal;
672 Ordinals[ordinal] = odp;
675 if (!strcmp( odp->name, "@" ))
677 if (ordinal == -1)
678 fatal_error( "Nameless function needs an explicit ordinal number\n" );
679 if (SpecType != SPEC_WIN32)
680 fatal_error( "Nameless functions not supported for Win16\n" );
681 odp->name[0] = 0;
683 else Names[nb_names++] = odp;
687 /*******************************************************************
688 * ParseTopLevel
690 * Parse a spec file.
692 static void ParseTopLevel(void)
694 char *token;
696 while ((token = GetToken()) != NULL)
698 if (strcmp(token, "name") == 0)
700 strcpy(DLLName, GetToken());
701 strupper(DLLName);
703 else if (strcmp(token, "file") == 0)
705 strcpy(DLLFileName, GetToken());
706 strupper(DLLFileName);
708 else if (strcmp(token, "type") == 0)
710 token = GetToken();
711 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
712 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
713 else fatal_error( "Type must be 'win16' or 'win32'\n" );
715 else if (strcmp(token, "mode") == 0)
717 token = GetToken();
718 if (!strcmp(token, "dll" )) SpecMode = SPEC_MODE_DLL;
719 else if (!strcmp(token, "guiexe" )) SpecMode = SPEC_MODE_GUIEXE;
720 else if (!strcmp(token, "cuiexe" )) SpecMode = SPEC_MODE_CUIEXE;
721 else fatal_error( "Mode must be 'dll', 'guiexe' or 'cuiexe'\n" );
723 else if (strcmp(token, "heap") == 0)
725 token = GetToken();
726 if (!IsNumberString(token)) fatal_error( "Expected number after heap\n" );
727 DLLHeapSize = atoi(token);
729 else if (strcmp(token, "init") == 0)
731 strcpy(DLLInitFunc, GetToken());
732 if (SpecType == SPEC_WIN16)
733 fatal_error( "init cannot be used for Win16 spec files\n" );
734 if (!DLLInitFunc[0])
735 fatal_error( "Expected function name after init\n" );
737 else if (strcmp(token, "import") == 0)
739 if (nb_imports >= MAX_IMPORTS)
740 fatal_error( "Too many imports (limit %d)\n", MAX_IMPORTS );
741 if (SpecType != SPEC_WIN32)
742 fatal_error( "Imports not supported for Win16\n" );
743 DLLImports[nb_imports++] = xstrdup(GetToken());
745 else if (strcmp(token, "rsrc") == 0)
747 strcpy( rsrc_name, GetToken() );
748 strcat( rsrc_name, "_ResourceDescriptor" );
750 else if (strcmp(token, "@") == 0)
752 if (SpecType != SPEC_WIN32)
753 fatal_error( "'@' ordinals not supported for Win16\n" );
754 ParseOrdinal( -1 );
756 else if (IsNumberString(token))
758 ParseOrdinal( atoi(token) );
760 else
761 fatal_error( "Expected name, id, length or ordinal\n" );
764 if (!DLLFileName[0])
766 if (SpecMode == SPEC_MODE_DLL)
767 sprintf( DLLFileName, "%s.DLL", DLLName );
768 else
769 sprintf( DLLFileName, "%s.EXE", DLLName );
774 /*******************************************************************
775 * StoreVariableCode
777 * Store a list of ints into a byte array.
779 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
781 int i;
783 switch(size)
785 case 1:
786 for (i = 0; i < odp->u.var.n_values; i++)
787 buffer[i] = odp->u.var.values[i];
788 break;
789 case 2:
790 for (i = 0; i < odp->u.var.n_values; i++)
791 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
792 break;
793 case 4:
794 for (i = 0; i < odp->u.var.n_values; i++)
795 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
796 break;
798 return odp->u.var.n_values * size;
802 /*******************************************************************
803 * DumpBytes
805 * Dump a byte stream into the assembly code.
807 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
808 const char *label )
810 int i;
812 fprintf( outfile, "\nstatic BYTE %s[] = \n{", label );
814 for (i = 0; i < len; i++)
816 if (!(i & 0x0f)) fprintf( outfile, "\n " );
817 fprintf( outfile, "%d", *data++ );
818 if (i < len - 1) fprintf( outfile, ", " );
820 fprintf( outfile, "\n};\n" );
824 /*******************************************************************
825 * BuildModule16
827 * Build the in-memory representation of a 16-bit NE module, and dump it
828 * as a byte stream into the assembly code.
830 static int BuildModule16( FILE *outfile, int max_code_offset,
831 int max_data_offset )
833 int i;
834 char *buffer;
835 NE_MODULE *pModule;
836 SEGTABLEENTRY *pSegment;
837 OFSTRUCT *pFileInfo;
838 BYTE *pstr;
839 WORD *pword;
840 ET_BUNDLE *bundle = 0;
841 ET_ENTRY *entry = 0;
843 /* Module layout:
844 * NE_MODULE Module
845 * OFSTRUCT File information
846 * SEGTABLEENTRY Segment 1 (code)
847 * SEGTABLEENTRY Segment 2 (data)
848 * WORD[2] Resource table (empty)
849 * BYTE[2] Imported names (empty)
850 * BYTE[n] Resident names table
851 * BYTE[n] Entry table
854 buffer = xmalloc( 0x10000 );
856 pModule = (NE_MODULE *)buffer;
857 memset( pModule, 0, sizeof(*pModule) );
858 pModule->magic = IMAGE_OS2_SIGNATURE;
859 pModule->count = 1;
860 pModule->next = 0;
861 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
862 pModule->dgroup = 2;
863 pModule->heap_size = DLLHeapSize;
864 pModule->stack_size = 0;
865 pModule->ip = 0;
866 pModule->cs = 0;
867 pModule->sp = 0;
868 pModule->ss = 0;
869 pModule->seg_count = 2;
870 pModule->modref_count = 0;
871 pModule->nrname_size = 0;
872 pModule->modref_table = 0;
873 pModule->nrname_fpos = 0;
874 pModule->moveable_entries = 0;
875 pModule->alignment = 0;
876 pModule->truetype = 0;
877 pModule->os_flags = NE_OSFLAGS_WINDOWS;
878 pModule->misc_flags = 0;
879 pModule->dlls_to_init = 0;
880 pModule->nrname_handle = 0;
881 pModule->min_swap_area = 0;
882 pModule->expected_version = 0;
883 pModule->module32 = 0;
884 pModule->self = 0;
885 pModule->self_loading_sel = 0;
887 /* File information */
889 pFileInfo = (OFSTRUCT *)(pModule + 1);
890 pModule->fileinfo = (int)pFileInfo - (int)pModule;
891 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
892 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
893 + strlen(DLLFileName);
894 strcpy( pFileInfo->szPathName, DLLFileName );
895 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
897 #ifdef __i386__ /* FIXME: Alignment problems! */
899 /* Segment table */
901 pSegment = (SEGTABLEENTRY *)pstr;
902 pModule->seg_table = (int)pSegment - (int)pModule;
903 pSegment->filepos = 0;
904 pSegment->size = max_code_offset;
905 pSegment->flags = 0;
906 pSegment->minsize = max_code_offset;
907 pSegment->hSeg = 0;
908 pSegment++;
910 pModule->dgroup_entry = (int)pSegment - (int)pModule;
911 pSegment->filepos = 0;
912 pSegment->size = max_data_offset;
913 pSegment->flags = NE_SEGFLAGS_DATA;
914 pSegment->minsize = max_data_offset;
915 pSegment->hSeg = 0;
916 pSegment++;
918 /* Resource table */
920 pword = (WORD *)pSegment;
921 pModule->res_table = (int)pword - (int)pModule;
922 *pword++ = 0;
923 *pword++ = 0;
925 /* Imported names table */
927 pstr = (char *)pword;
928 pModule->import_table = (int)pstr - (int)pModule;
929 *pstr++ = 0;
930 *pstr++ = 0;
932 /* Resident names table */
934 pModule->name_table = (int)pstr - (int)pModule;
935 /* First entry is module name */
936 *pstr = strlen(DLLName );
937 strcpy( pstr + 1, DLLName );
938 pstr += *pstr + 1;
939 *(WORD *)pstr = 0;
940 pstr += sizeof(WORD);
941 /* Store all ordinals */
942 for (i = 1; i <= Limit; i++)
944 ORDDEF *odp = Ordinals[i];
945 if (!odp || !odp->name[0]) continue;
946 *pstr = strlen( odp->name );
947 strcpy( pstr + 1, odp->name );
948 strupper( pstr + 1 );
949 pstr += *pstr + 1;
950 *(WORD *)pstr = i;
951 pstr += sizeof(WORD);
953 *pstr++ = 0;
955 /* Entry table */
957 pModule->entry_table = (int)pstr - (int)pModule;
958 for (i = 1; i <= Limit; i++)
960 int selector = 0;
961 ORDDEF *odp = Ordinals[i];
962 if (!odp) continue;
964 switch (odp->type)
966 case TYPE_CDECL:
967 case TYPE_PASCAL:
968 case TYPE_PASCAL_16:
969 case TYPE_REGISTER:
970 case TYPE_INTERRUPT:
971 case TYPE_STUB:
972 selector = 1; /* Code selector */
973 break;
975 case TYPE_BYTE:
976 case TYPE_WORD:
977 case TYPE_LONG:
978 selector = 2; /* Data selector */
979 break;
981 case TYPE_ABS:
982 selector = 0xfe; /* Constant selector */
983 break;
985 default:
986 selector = 0; /* Invalid selector */
987 break;
990 if ( !selector )
991 continue;
993 if ( bundle && bundle->last+1 == i )
994 bundle->last++;
995 else
997 if ( bundle )
998 bundle->next = (char *)pstr - (char *)pModule;
1000 bundle = (ET_BUNDLE *)pstr;
1001 bundle->first = i-1;
1002 bundle->last = i;
1003 bundle->next = 0;
1004 pstr += sizeof(ET_BUNDLE);
1007 /* FIXME: is this really correct ?? */
1008 entry = (ET_ENTRY *)pstr;
1009 entry->type = 0xff; /* movable */
1010 entry->flags = 3; /* exported & public data */
1011 entry->segnum = selector;
1012 entry->offs = odp->offset;
1013 pstr += sizeof(ET_ENTRY);
1015 *pstr++ = 0;
1016 #endif
1018 /* Dump the module content */
1020 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
1021 "Module" );
1022 return (int)pstr - (int)pModule;
1026 /*******************************************************************
1027 * BuildSpec32File
1029 * Build a Win32 C file from a spec file.
1031 static int BuildSpec32File( FILE *outfile )
1033 ORDDEF *odp;
1034 int i, fwd_size = 0, have_regs = FALSE;
1035 int nr_exports;
1037 AssignOrdinals();
1038 nr_exports = Base <= Limit ? Limit - Base + 1 : 0;
1040 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
1041 input_file_name );
1042 fprintf( outfile, "#include \"builtin32.h\"\n\n" );
1043 fprintf( outfile, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
1044 DLLName );
1046 /* Output the DLL functions prototypes */
1048 for (i = 0, odp = EntryPoints; i < nb_entry_points; i++, odp++)
1050 switch(odp->type)
1052 case TYPE_EXTERN:
1053 fprintf( outfile, "extern void %s();\n", odp->u.ext.link_name );
1054 break;
1055 case TYPE_STDCALL:
1056 case TYPE_VARARGS:
1057 case TYPE_CDECL:
1058 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1059 break;
1060 case TYPE_FORWARD:
1061 fwd_size += strlen(odp->u.fwd.link_name) + 1;
1062 break;
1063 case TYPE_REGISTER:
1064 fprintf( outfile, "extern void __regs_%d();\n", odp->ordinal );
1065 have_regs = TRUE;
1066 break;
1067 case TYPE_STUB:
1068 fprintf( outfile, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
1069 odp->ordinal, DLLName, odp->ordinal );
1070 break;
1071 default:
1072 fprintf(stderr,"build: function type %d not available for Win32\n",
1073 odp->type);
1074 return -1;
1078 /* Output LibMain function */
1079 if (DLLInitFunc[0]) fprintf( outfile, "extern void %s();\n", DLLInitFunc );
1082 /* Output code for all register functions */
1084 if ( have_regs )
1086 fprintf( outfile, "#ifndef __GNUC__\n" );
1087 fprintf( outfile, "static void __asm__dummy() {\n" );
1088 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
1089 for (i = 0, odp = EntryPoints; i < nb_entry_points; i++, odp++)
1091 if (odp->type != TYPE_REGISTER) continue;
1092 fprintf( outfile,
1093 "__asm__(\".align 4\\n\\t\"\n"
1094 " \".type " PREFIX "__regs_%d,@function\\n\\t\"\n"
1095 " \"" PREFIX "__regs_%d:\\n\\t\"\n"
1096 " \"call " PREFIX "CALL32_Regs\\n\\t\"\n"
1097 " \".long " PREFIX "%s\\n\\t\"\n"
1098 " \".byte %d,%d\");\n",
1099 odp->ordinal, odp->ordinal, odp->u.func.link_name,
1100 4 * strlen(odp->u.func.arg_types),
1101 4 * strlen(odp->u.func.arg_types) );
1103 fprintf( outfile, "#ifndef __GNUC__\n" );
1104 fprintf( outfile, "}\n" );
1105 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
1108 /* Output the DLL functions table */
1110 fprintf( outfile, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1111 nr_exports );
1112 for (i = Base; i <= Limit; i++)
1114 ORDDEF *odp = Ordinals[i];
1115 if (!odp) fprintf( outfile, " 0" );
1116 else switch(odp->type)
1118 case TYPE_EXTERN:
1119 fprintf( outfile, " %s", odp->u.ext.link_name );
1120 break;
1121 case TYPE_STDCALL:
1122 case TYPE_VARARGS:
1123 case TYPE_CDECL:
1124 fprintf( outfile, " %s", odp->u.func.link_name);
1125 break;
1126 case TYPE_STUB:
1127 fprintf( outfile, " __stub_%d", i );
1128 break;
1129 case TYPE_REGISTER:
1130 fprintf( outfile, " __regs_%d", i );
1131 break;
1132 case TYPE_FORWARD:
1133 fprintf( outfile, " (ENTRYPOINT32)\"%s\"", odp->u.fwd.link_name );
1134 break;
1135 default:
1136 return -1;
1138 if (i < Limit) fprintf( outfile, ",\n" );
1140 fprintf( outfile, "\n};\n\n" );
1142 /* Output the DLL names table */
1144 fprintf( outfile, "static const char * const FuncNames[%d] =\n{\n", nb_names );
1145 for (i = 0; i < nb_names; i++)
1147 if (i) fprintf( outfile, ",\n" );
1148 fprintf( outfile, " \"%s\"", Names[i]->name );
1150 fprintf( outfile, "\n};\n\n" );
1152 /* Output the DLL ordinals table */
1154 fprintf( outfile, "static const unsigned short FuncOrdinals[%d] =\n{\n", nb_names );
1155 for (i = 0; i < nb_names; i++)
1157 if (i) fprintf( outfile, ",\n" );
1158 fprintf( outfile, " %d", Names[i]->ordinal - Base );
1160 fprintf( outfile, "\n};\n\n" );
1162 /* Output the DLL argument types */
1164 fprintf( outfile, "static const unsigned int ArgTypes[%d] =\n{\n",
1165 nr_exports );
1166 for (i = Base; i <= Limit; i++)
1168 ORDDEF *odp = Ordinals[i];
1169 unsigned int j, mask = 0;
1170 if (odp &&
1171 ((odp->type == TYPE_STDCALL) || (odp->type == TYPE_CDECL) ||
1172 (odp->type == TYPE_REGISTER)))
1173 for (j = 0; odp->u.func.arg_types[j]; j++)
1175 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
1176 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
1178 fprintf( outfile, " %d", mask );
1179 if (i < Limit) fprintf( outfile, ",\n" );
1181 fprintf( outfile, "\n};\n\n" );
1183 /* Output the DLL functions arguments */
1185 fprintf( outfile, "static const unsigned char FuncArgs[%d] =\n{\n",
1186 nr_exports );
1187 for (i = Base; i <= Limit; i++)
1189 unsigned char args = 0xff;
1190 ORDDEF *odp = Ordinals[i];
1191 if (odp) switch(odp->type)
1193 case TYPE_STDCALL:
1194 args = (unsigned char)strlen(odp->u.func.arg_types);
1195 break;
1196 case TYPE_CDECL:
1197 args = 0x80 | (unsigned char)strlen(odp->u.func.arg_types);
1198 break;
1199 case TYPE_REGISTER:
1200 args = 0x40 | (unsigned char)strlen(odp->u.func.arg_types);
1201 break;
1202 case TYPE_FORWARD:
1203 args = 0xfd;
1204 break;
1205 default:
1206 args = 0xff;
1207 break;
1209 fprintf( outfile, " 0x%02x", args );
1210 if (i < Limit) fprintf( outfile, ",\n" );
1212 fprintf( outfile, "\n};\n\n" );
1214 /* Output the DLL imports */
1216 if (nb_imports)
1218 fprintf( outfile, "static const char * const Imports[%d] =\n{\n", nb_imports );
1219 for (i = 0; i < nb_imports; i++)
1221 fprintf( outfile, " \"%s\"", DLLImports[i] );
1222 if (i < nb_imports-1) fprintf( outfile, ",\n" );
1224 fprintf( outfile, "\n};\n\n" );
1227 /* Output the DLL descriptor */
1229 if (rsrc_name[0]) fprintf( outfile, "extern const char %s[];\n\n", rsrc_name );
1231 fprintf( outfile, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1232 DLLName );
1233 fprintf( outfile, " \"%s\",\n", DLLName );
1234 fprintf( outfile, " \"%s\",\n", DLLFileName );
1235 fprintf( outfile, " %d,\n", nr_exports? Base : 0 );
1236 fprintf( outfile, " %d,\n", nr_exports );
1237 fprintf( outfile, " %d,\n", nb_names );
1238 fprintf( outfile, " %d,\n", nb_imports );
1239 fprintf( outfile, " %d,\n", (fwd_size + 3) & ~3 );
1240 fprintf( outfile,
1241 " Functions,\n"
1242 " FuncNames,\n"
1243 " FuncOrdinals,\n"
1244 " FuncArgs,\n"
1245 " ArgTypes,\n");
1246 fprintf( outfile, " %s,\n", nb_imports ? "Imports" : "0" );
1247 fprintf( outfile, " %s,\n", DLLInitFunc[0] ? DLLInitFunc : "0" );
1248 fprintf( outfile, " %d,\n", SpecMode == SPEC_MODE_DLL ? IMAGE_FILE_DLL : 0 );
1249 fprintf( outfile, " %s\n", rsrc_name[0] ? rsrc_name : "0" );
1250 fprintf( outfile, "};\n" );
1252 /* Output the DLL constructor */
1254 fprintf( outfile, "static void dll_init(void) __attribute__((constructor));\n" );
1255 fprintf( outfile, "static void dll_init(void) { BUILTIN32_RegisterDLL( &%s_Descriptor ); }\n",
1256 DLLName );
1257 return 0;
1260 /*******************************************************************
1261 * Spec16TypeCompare
1263 static int Spec16TypeCompare( const void *e1, const void *e2 )
1265 const ORDDEF *odp1 = *(const ORDDEF **)e1;
1266 const ORDDEF *odp2 = *(const ORDDEF **)e2;
1268 int type1 = (odp1->type == TYPE_CDECL) ? 0
1269 : (odp1->type == TYPE_REGISTER) ? 3
1270 : (odp1->type == TYPE_INTERRUPT) ? 4
1271 : (odp1->type == TYPE_PASCAL_16) ? 1 : 2;
1273 int type2 = (odp2->type == TYPE_CDECL) ? 0
1274 : (odp2->type == TYPE_REGISTER) ? 3
1275 : (odp2->type == TYPE_INTERRUPT) ? 4
1276 : (odp2->type == TYPE_PASCAL_16) ? 1 : 2;
1278 int retval = type1 - type2;
1279 if ( !retval )
1280 retval = strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
1282 return retval;
1285 /*******************************************************************
1286 * BuildSpec16File
1288 * Build a Win16 assembly file from a spec file.
1290 static int BuildSpec16File( FILE *outfile )
1292 ORDDEF **type, **typelist;
1293 int i, nFuncs, nTypes;
1294 int code_offset, data_offset, module_size;
1295 unsigned char *data;
1297 /* File header */
1299 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
1300 input_file_name );
1301 fprintf( outfile, "#define __FLATCS__ 0x%04x\n", Code_Selector );
1302 fprintf( outfile, "#include \"builtin16.h\"\n\n" );
1304 data = (unsigned char *)xmalloc( 0x10000 );
1305 memset( data, 0, 16 );
1306 data_offset = 16;
1309 /* Build sorted list of all argument types, without duplicates */
1311 typelist = (ORDDEF **)calloc( Limit+1, sizeof(ORDDEF *) );
1313 for (i = nFuncs = 0; i <= Limit; i++)
1315 ORDDEF *odp = Ordinals[i];
1316 if (!odp) continue;
1317 switch (odp->type)
1319 case TYPE_REGISTER:
1320 case TYPE_INTERRUPT:
1321 case TYPE_CDECL:
1322 case TYPE_PASCAL:
1323 case TYPE_PASCAL_16:
1324 case TYPE_STUB:
1325 typelist[nFuncs++] = odp;
1327 default:
1328 break;
1332 qsort( typelist, nFuncs, sizeof(ORDDEF *), Spec16TypeCompare );
1334 i = nTypes = 0;
1335 while ( i < nFuncs )
1337 typelist[nTypes++] = typelist[i++];
1338 while ( i < nFuncs && Spec16TypeCompare( typelist + i, typelist + nTypes-1 ) == 0 )
1339 i++;
1342 /* Output CallFrom16 routines needed by this .spec file */
1344 for ( i = 0; i < nTypes; i++ )
1346 char profile[101];
1348 sprintf( profile, "%s_%s_%s",
1349 (typelist[i]->type == TYPE_CDECL) ? "c" : "p",
1350 (typelist[i]->type == TYPE_REGISTER) ? "regs" :
1351 (typelist[i]->type == TYPE_INTERRUPT) ? "intr" :
1352 (typelist[i]->type == TYPE_PASCAL_16) ? "word" : "long",
1353 typelist[i]->u.func.arg_types );
1355 BuildCallFrom16Func( outfile, profile, DLLName, TRUE );
1358 /* Output the DLL functions prototypes */
1360 for (i = 0; i <= Limit; i++)
1362 ORDDEF *odp = Ordinals[i];
1363 if (!odp) continue;
1364 switch(odp->type)
1366 case TYPE_REGISTER:
1367 case TYPE_INTERRUPT:
1368 case TYPE_CDECL:
1369 case TYPE_PASCAL:
1370 case TYPE_PASCAL_16:
1371 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1372 break;
1373 default:
1374 break;
1378 /* Output code segment */
1380 fprintf( outfile, "\nstatic struct\n{\n CALLFROM16 call[%d];\n"
1381 " ENTRYPOINT16 entry[%d];\n} Code_Segment = \n{\n {\n",
1382 nTypes, nFuncs );
1383 code_offset = 0;
1385 for ( i = 0; i < nTypes; i++ )
1387 char profile[101], *arg;
1388 int argsize = 0;
1390 sprintf( profile, "%s_%s_%s",
1391 (typelist[i]->type == TYPE_CDECL) ? "c" : "p",
1392 (typelist[i]->type == TYPE_REGISTER) ? "regs" :
1393 (typelist[i]->type == TYPE_INTERRUPT) ? "intr" :
1394 (typelist[i]->type == TYPE_PASCAL_16) ? "word" : "long",
1395 typelist[i]->u.func.arg_types );
1397 if ( typelist[i]->type != TYPE_CDECL )
1398 for ( arg = typelist[i]->u.func.arg_types; *arg; arg++ )
1399 switch ( *arg )
1401 case 'w': /* word */
1402 case 's': /* s_word */
1403 argsize += 2;
1404 break;
1406 case 'l': /* long or segmented pointer */
1407 case 'T': /* segmented pointer to null-terminated string */
1408 case 'p': /* linear pointer */
1409 case 't': /* linear pointer to null-terminated string */
1410 argsize += 4;
1411 break;
1414 if ( typelist[i]->type == TYPE_INTERRUPT )
1415 argsize += 2;
1417 fprintf( outfile, " CF16_%s( %s_CallFrom16_%s, %d, \"%s\" ),\n",
1418 ( typelist[i]->type == TYPE_REGISTER
1419 || typelist[i]->type == TYPE_INTERRUPT)? "REGS":
1420 typelist[i]->type == TYPE_PASCAL_16? "WORD" : "LONG",
1421 DLLName, profile, argsize, profile );
1423 code_offset += sizeof(CALLFROM16);
1425 fprintf( outfile, " },\n {\n" );
1427 for (i = 0; i <= Limit; i++)
1429 ORDDEF *odp = Ordinals[i];
1430 if (!odp) continue;
1431 switch (odp->type)
1433 case TYPE_ABS:
1434 odp->offset = LOWORD(odp->u.abs.value);
1435 break;
1437 case TYPE_BYTE:
1438 odp->offset = data_offset;
1439 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1440 break;
1442 case TYPE_WORD:
1443 odp->offset = data_offset;
1444 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1445 break;
1447 case TYPE_LONG:
1448 odp->offset = data_offset;
1449 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1450 break;
1452 case TYPE_REGISTER:
1453 case TYPE_INTERRUPT:
1454 case TYPE_CDECL:
1455 case TYPE_PASCAL:
1456 case TYPE_PASCAL_16:
1457 case TYPE_STUB:
1458 type = bsearch( &odp, typelist, nTypes, sizeof(ORDDEF *), Spec16TypeCompare );
1459 assert( type );
1461 fprintf( outfile, " /* %s.%d */ ", DLLName, i );
1462 fprintf( outfile, "EP( %s, %d /* %s_%s_%s */ ),\n",
1463 odp->u.func.link_name,
1464 (type-typelist)*sizeof(CALLFROM16) -
1465 (code_offset + sizeof(ENTRYPOINT16)),
1466 (odp->type == TYPE_CDECL) ? "c" : "p",
1467 (odp->type == TYPE_REGISTER) ? "regs" :
1468 (odp->type == TYPE_INTERRUPT) ? "intr" :
1469 (odp->type == TYPE_PASCAL_16) ? "word" : "long",
1470 odp->u.func.arg_types );
1472 odp->offset = code_offset;
1473 code_offset += sizeof(ENTRYPOINT16);
1474 break;
1476 default:
1477 fprintf(stderr,"build: function type %d not available for Win16\n",
1478 odp->type);
1479 return -1;
1483 fprintf( outfile, " }\n};\n" );
1485 /* Output data segment */
1487 DumpBytes( outfile, data, data_offset, "Data_Segment" );
1489 /* Build the module */
1491 module_size = BuildModule16( outfile, code_offset, data_offset );
1493 /* Output the DLL descriptor */
1495 if (rsrc_name[0]) fprintf( outfile, "extern const char %s[];\n\n", rsrc_name );
1497 fprintf( outfile, "\nconst BUILTIN16_DESCRIPTOR %s_Descriptor = \n{\n", DLLName );
1498 fprintf( outfile, " \"%s\",\n", DLLName );
1499 fprintf( outfile, " Module,\n" );
1500 fprintf( outfile, " sizeof(Module),\n" );
1501 fprintf( outfile, " (BYTE *)&Code_Segment,\n" );
1502 fprintf( outfile, " (BYTE *)Data_Segment,\n" );
1503 fprintf( outfile, " %s\n", rsrc_name[0] ? rsrc_name : "0" );
1504 fprintf( outfile, "};\n" );
1506 /* Output the DLL constructor */
1508 fprintf( outfile, "static void dll_init(void) __attribute__((constructor));\n" );
1509 fprintf( outfile, "static void dll_init(void) { BUILTIN_RegisterDLL( &%s_Descriptor ); }\n",
1510 DLLName );
1511 return 0;
1515 /*******************************************************************
1516 * BuildSpecFile
1518 * Build an assembly file from a spec file.
1520 static void BuildSpecFile( FILE *outfile, FILE *infile )
1522 SpecFp = infile;
1523 ParseTopLevel();
1525 switch(SpecType)
1527 case SPEC_WIN16:
1528 BuildSpec16File( outfile );
1529 break;
1530 case SPEC_WIN32:
1531 BuildSpec32File( outfile );
1532 break;
1533 default:
1534 fatal_error( "Missing 'type' declaration\n" );
1539 /*******************************************************************
1540 * BuildCallFrom16Func
1542 * Build a 16-bit-to-Wine callback glue function.
1544 * The generated routines are intended to be used as argument conversion
1545 * routines to be called by the CallFrom16... core. Thus, the prototypes of
1546 * the generated routines are (see also CallFrom16):
1548 * extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
1549 * extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
1550 * extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args,
1551 * CONTEXT86 *context );
1552 * extern void WINAPI PREFIX_CallFrom16_C_intr_xxx( FARPROC func, LPBYTE args,
1553 * CONTEXT86 *context );
1555 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
1556 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
1557 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1558 * 'T'=segmented pointer to null-terminated string).
1560 * The generated routines fetch the arguments from the 16-bit stack (pointed
1561 * to by 'args'); the offsets of the single argument values are computed
1562 * according to the calling convention and the argument types. Then, the
1563 * 32-bit entry point is called with these arguments.
1565 * For register functions, the arguments (if present) are converted just
1566 * the same as for normal functions, but in addition the CONTEXT86 pointer
1567 * filled with the current register values is passed to the 32-bit routine.
1568 * (An 'intr' interrupt handler routine is treated exactly like a register
1569 * routine, except that upon return, the flags word pushed onto the stack
1570 * by the interrupt is removed by the 16-bit call stub.)
1573 static void BuildCallFrom16Func( FILE *outfile, char *profile, char *prefix, int local )
1575 int i, pos, argsize = 0;
1576 int short_ret = 0;
1577 int reg_func = 0;
1578 int usecdecl = 0;
1579 char *args = profile + 7;
1580 char *ret_type;
1582 /* Parse function type */
1584 if (!strncmp( "c_", profile, 2 )) usecdecl = 1;
1585 else if (strncmp( "p_", profile, 2 ))
1587 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1588 return;
1591 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
1592 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
1593 else if (!strncmp( "intr_", profile + 2, 5 )) reg_func = 2;
1594 else if (strncmp( "long_", profile + 2, 5 ))
1596 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1597 return;
1600 for ( i = 0; args[i]; i++ )
1601 switch ( args[i] )
1603 case 'w': /* word */
1604 case 's': /* s_word */
1605 argsize += 2;
1606 break;
1608 case 'l': /* long or segmented pointer */
1609 case 'T': /* segmented pointer to null-terminated string */
1610 case 'p': /* linear pointer */
1611 case 't': /* linear pointer to null-terminated string */
1612 argsize += 4;
1613 break;
1616 ret_type = reg_func? "void" : short_ret? "WORD" : "LONG";
1618 fprintf( outfile, "typedef %s WINAPI (*proc_%s_t)( ",
1619 ret_type, profile );
1620 args = profile + 7;
1621 for ( i = 0; args[i]; i++ )
1623 if ( i ) fprintf( outfile, ", " );
1624 switch (args[i])
1626 case 'w': fprintf( outfile, "WORD" ); break;
1627 case 's': fprintf( outfile, "INT16" ); break;
1628 case 'l': case 'T': fprintf( outfile, "LONG" ); break;
1629 case 'p': case 't': fprintf( outfile, "LPVOID" ); break;
1632 if ( reg_func )
1633 fprintf( outfile, "%sstruct _CONTEXT86 *", i? ", " : "" );
1634 else if ( !i )
1635 fprintf( outfile, "void" );
1636 fprintf( outfile, " );\n" );
1638 fprintf( outfile, "%s%s WINAPI %s_CallFrom16_%s( FARPROC proc, LPBYTE args%s )\n{\n",
1639 local? "static " : "", ret_type, prefix, profile,
1640 reg_func? ", struct _CONTEXT86 *context" : "" );
1642 fprintf( outfile, " %s((proc_%s_t) proc) (\n",
1643 reg_func? "" : "return ", profile );
1644 args = profile + 7;
1645 pos = !usecdecl? argsize : 0;
1646 for ( i = 0; args[i]; i++ )
1648 if ( i ) fprintf( outfile, ",\n" );
1649 fprintf( outfile, " " );
1650 switch (args[i])
1652 case 'w': /* word */
1653 if ( !usecdecl ) pos -= 2;
1654 fprintf( outfile, "*(WORD *)(args+%d)", pos );
1655 if ( usecdecl ) pos += 2;
1656 break;
1658 case 's': /* s_word */
1659 if ( !usecdecl ) pos -= 2;
1660 fprintf( outfile, "*(INT16 *)(args+%d)", pos );
1661 if ( usecdecl ) pos += 2;
1662 break;
1664 case 'l': /* long or segmented pointer */
1665 case 'T': /* segmented pointer to null-terminated string */
1666 if ( !usecdecl ) pos -= 4;
1667 fprintf( outfile, "*(LONG *)(args+%d)", pos );
1668 if ( usecdecl ) pos += 4;
1669 break;
1671 case 'p': /* linear pointer */
1672 case 't': /* linear pointer to null-terminated string */
1673 if ( !usecdecl ) pos -= 4;
1674 fprintf( outfile, "PTR_SEG_TO_LIN( *(SEGPTR *)(args+%d) )", pos );
1675 if ( usecdecl ) pos += 4;
1676 break;
1678 default:
1679 fprintf( stderr, "Unknown arg type '%c'\n", args[i] );
1682 if ( reg_func )
1683 fprintf( outfile, "%s context", i? ",\n" : "" );
1684 fprintf( outfile, " );\n}\n\n" );
1687 /*******************************************************************
1688 * BuildCallTo16Func
1690 * Build a Wine-to-16-bit callback glue function.
1692 * Prototypes for the CallTo16 functions:
1693 * extern WORD CALLBACK PREFIX_CallTo16_word_xxx( FARPROC16 func, args... );
1694 * extern LONG CALLBACK PREFIX_CallTo16_long_xxx( FARPROC16 func, args... );
1696 * These routines are provided solely for convenience; they simply
1697 * write the arguments onto the 16-bit stack, and call the appropriate
1698 * CallTo16... core routine.
1700 * If you have more sophisticated argument conversion requirements than
1701 * are provided by these routines, you might as well call the core
1702 * routines by yourself.
1705 static void BuildCallTo16Func( FILE *outfile, char *profile, char *prefix )
1707 char *args = profile + 5;
1708 int i, argsize = 0, short_ret = 0;
1710 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1711 else if (strncmp( "long_", profile, 5 ))
1713 fprintf( stderr, "Invalid function name '%s'.\n", profile );
1714 exit(1);
1717 fprintf( outfile, "%s %s_CallTo16_%s( FARPROC16 proc",
1718 short_ret? "WORD" : "LONG", prefix, profile );
1719 args = profile + 5;
1720 for ( i = 0; args[i]; i++ )
1722 fprintf( outfile, ", " );
1723 switch (args[i])
1725 case 'w': fprintf( outfile, "WORD" ); argsize += 2; break;
1726 case 'l': fprintf( outfile, "LONG" ); argsize += 4; break;
1728 fprintf( outfile, " arg%d", i+1 );
1730 fprintf( outfile, " )\n{\n" );
1732 if ( argsize > 0 )
1733 fprintf( outfile, " LPBYTE args = (LPBYTE)CURRENT_STACK16;\n" );
1735 args = profile + 5;
1736 for ( i = 0; args[i]; i++ )
1738 switch (args[i])
1740 case 'w': fprintf( outfile, " args -= sizeof(WORD); *(WORD" ); break;
1741 case 'l': fprintf( outfile, " args -= sizeof(LONG); *(LONG" ); break;
1742 default: fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
1743 args[i] );
1745 fprintf( outfile, " *)args = arg%d;\n", i+1 );
1748 fprintf( outfile, " return CallTo16%s( proc, %d );\n}\n\n",
1749 short_ret? "Word" : "Long", argsize );
1754 /*******************************************************************
1755 * BuildCallFrom16Core
1757 * This routine builds the core routines used in 16->32 thunks:
1758 * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
1760 * These routines are intended to be called via a far call (with 32-bit
1761 * operand size) from 16-bit code. The 16-bit code stub must push %bp,
1762 * the 32-bit entry point to be called, and the argument conversion
1763 * routine to be used (see stack layout below).
1765 * The core routine completes the STACK16FRAME on the 16-bit stack and
1766 * switches to the 32-bit stack. Then, the argument conversion routine
1767 * is called; it gets passed the 32-bit entry point and a pointer to the
1768 * 16-bit arguments (on the 16-bit stack) as parameters. (You can either
1769 * use conversion routines automatically generated by BuildCallFrom16,
1770 * or write your own for special purposes.)
1772 * The conversion routine must call the 32-bit entry point, passing it
1773 * the converted arguments, and return its return value to the core.
1774 * After the conversion routine has returned, the core switches back
1775 * to the 16-bit stack, converts the return value to the DX:AX format
1776 * (CallFrom16Long), and returns to the 16-bit call stub. All parameters,
1777 * including %bp, are popped off the stack.
1779 * The 16-bit call stub now returns to the caller, popping the 16-bit
1780 * arguments if necessary (pascal calling convention).
1782 * In the case of a 'register' function, CallFrom16Register fills a
1783 * CONTEXT86 structure with the values all registers had at the point
1784 * the first instruction of the 16-bit call stub was about to be
1785 * executed. A pointer to this CONTEXT86 is passed as third parameter
1786 * to the argument conversion routine, which typically passes it on
1787 * to the called 32-bit entry point.
1789 * CallFrom16Thunk is a special variant used by the implementation of
1790 * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
1791 * implemented as follows:
1792 * On entry, the EBX register is set up to contain a flat pointer to the
1793 * 16-bit stack such that EBX+22 points to the first argument.
1794 * Then, the entry point is called, while EBP is set up to point
1795 * to the return address (on the 32-bit stack).
1796 * The called function returns with CX set to the number of bytes
1797 * to be popped of the caller's stack.
1799 * Stack layout upon entry to the core routine (STACK16FRAME):
1800 * ... ...
1801 * (sp+24) word first 16-bit arg
1802 * (sp+22) word cs
1803 * (sp+20) word ip
1804 * (sp+18) word bp
1805 * (sp+14) long 32-bit entry point (reused for Win16 mutex recursion count)
1806 * (sp+12) word ip of actual entry point (necessary for relay debugging)
1807 * (sp+8) long relay (argument conversion) function entry point
1808 * (sp+4) long cs of 16-bit entry point
1809 * (sp) long ip of 16-bit entry point
1811 * Added on the stack:
1812 * (sp-2) word saved gs
1813 * (sp-4) word saved fs
1814 * (sp-6) word saved es
1815 * (sp-8) word saved ds
1816 * (sp-12) long saved ebp
1817 * (sp-16) long saved ecx
1818 * (sp-20) long saved edx
1819 * (sp-24) long saved previous stack
1821 static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk, int short_ret )
1823 char *name = thunk? "Thunk" : reg_func? "Register" : short_ret? "Word" : "Long";
1825 /* Function header */
1826 fprintf( outfile, "\n\t.align 4\n" );
1827 #ifdef USE_STABS
1828 fprintf( outfile, ".stabs \"CallFrom16%s:F1\",36,0,0," PREFIX "CallFrom16%s\n",
1829 name, name);
1830 #endif
1831 fprintf( outfile, "\t.type " PREFIX "CallFrom16%s,@function\n", name );
1832 fprintf( outfile, "\t.globl " PREFIX "CallFrom16%s\n", name );
1833 fprintf( outfile, PREFIX "CallFrom16%s:\n", name );
1835 /* Create STACK16FRAME (except STACK32FRAME link) */
1836 fprintf( outfile, "\tpushw %%gs\n" );
1837 fprintf( outfile, "\tpushw %%fs\n" );
1838 fprintf( outfile, "\tpushw %%es\n" );
1839 fprintf( outfile, "\tpushw %%ds\n" );
1840 fprintf( outfile, "\tpushl %%ebp\n" );
1841 fprintf( outfile, "\tpushl %%ecx\n" );
1842 fprintf( outfile, "\tpushl %%edx\n" );
1844 /* Save original EFlags register */
1845 fprintf( outfile, "\tpushfl\n" );
1847 if ( UsePIC )
1849 /* Get Global Offset Table into %ecx */
1850 fprintf( outfile, "\tcall .LCallFrom16%s.getgot1\n", name );
1851 fprintf( outfile, ".LCallFrom16%s.getgot1:\n", name );
1852 fprintf( outfile, "\tpopl %%ecx\n" );
1853 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot1], %%ecx\n", name );
1856 /* Load 32-bit segment registers */
1857 fprintf( outfile, "\tmovw $0x%04x, %%dx\n", Data_Selector );
1858 #ifdef __svr4__
1859 fprintf( outfile, "\tdata16\n");
1860 #endif
1861 fprintf( outfile, "\tmovw %%dx, %%ds\n" );
1862 #ifdef __svr4__
1863 fprintf( outfile, "\tdata16\n");
1864 #endif
1865 fprintf( outfile, "\tmovw %%dx, %%es\n" );
1867 if ( UsePIC )
1869 fprintf( outfile, "\tmovl " PREFIX "SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
1870 fprintf( outfile, "\tmovw (%%edx), %%fs\n" );
1872 else
1873 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb, %%fs\n" );
1875 /* Get address of ldt_copy array into %ecx */
1876 if ( UsePIC )
1877 fprintf( outfile, "\tmovl " PREFIX "ldt_copy@GOT(%%ecx), %%ecx\n" );
1878 else
1879 fprintf( outfile, "\tmovl $" PREFIX "ldt_copy, %%ecx\n" );
1881 /* Translate STACK16FRAME base to flat offset in %edx */
1882 fprintf( outfile, "\tmovw %%ss, %%dx\n" );
1883 fprintf( outfile, "\tandl $0xfff8, %%edx\n" );
1884 fprintf( outfile, "\tmovl (%%ecx,%%edx), %%edx\n" );
1885 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
1886 fprintf( outfile, "\tleal (%%ebp,%%edx), %%edx\n" );
1888 /* Get saved flags into %ecx */
1889 fprintf( outfile, "\tpopl %%ecx\n" );
1891 /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
1892 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET );
1893 fprintf( outfile, "\tpushl %%ebp\n" );
1895 /* Switch stacks */
1896 #ifdef __svr4__
1897 fprintf( outfile,"\tdata16\n");
1898 #endif
1899 fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET + 2 );
1900 fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET );
1901 fprintf( outfile, "\tpushl %%ds\n" );
1902 fprintf( outfile, "\tpopl %%ss\n" );
1903 fprintf( outfile, "\tmovl %%ebp, %%esp\n" );
1904 fprintf( outfile, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME, ebp) );
1907 /* At this point:
1908 STACK16FRAME is completely set up
1909 DS, ES, SS: flat data segment
1910 FS: current TEB
1911 ESP: points to last STACK32FRAME
1912 EBP: points to ebp member of last STACK32FRAME
1913 EDX: points to current STACK16FRAME
1914 ECX: contains saved flags
1915 all other registers: unchanged */
1917 /* Special case: C16ThkSL stub */
1918 if ( thunk )
1920 /* Set up registers as expected and call thunk */
1921 fprintf( outfile, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME)-22 );
1922 fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
1924 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point) );
1926 /* Switch stack back */
1927 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
1928 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
1929 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
1931 /* Restore registers and return directly to caller */
1932 fprintf( outfile, "\taddl $8, %%esp\n" );
1933 fprintf( outfile, "\tpopl %%ebp\n" );
1934 fprintf( outfile, "\tpopw %%ds\n" );
1935 fprintf( outfile, "\tpopw %%es\n" );
1936 fprintf( outfile, "\tpopw %%fs\n" );
1937 fprintf( outfile, "\tpopw %%gs\n" );
1938 fprintf( outfile, "\taddl $20, %%esp\n" );
1940 fprintf( outfile, "\txorb %%ch, %%ch\n" );
1941 fprintf( outfile, "\tpopl %%ebx\n" );
1942 fprintf( outfile, "\taddw %%cx, %%sp\n" );
1943 fprintf( outfile, "\tpush %%ebx\n" );
1945 fprintf( outfile, "\t.byte 0x66\n" );
1946 fprintf( outfile, "\tlret\n" );
1948 return;
1952 /* Build register CONTEXT */
1953 if ( reg_func )
1955 fprintf( outfile, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86) );
1957 fprintf( outfile, "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags) );
1959 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax) );
1960 fprintf( outfile, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx) );
1961 fprintf( outfile, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi) );
1962 fprintf( outfile, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi) );
1964 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp) );
1965 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp) );
1966 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx) );
1967 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx) );
1968 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx) );
1969 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx) );
1971 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds) );
1972 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs) );
1973 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es) );
1974 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs) );
1975 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs) );
1976 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs) );
1977 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs) );
1978 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs) );
1980 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs) );
1981 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs) );
1982 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip) );
1983 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip) );
1985 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET+2 );
1986 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs) );
1987 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET );
1988 fprintf( outfile, "\taddl $%d, %%eax\n", STACK16OFFSET(ip) );
1989 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp) );
1990 #if 0
1991 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
1992 #endif
1994 /* Push address of CONTEXT86 structure -- popped by the relay routine */
1995 fprintf( outfile, "\tpushl %%esp\n" );
1999 /* Print debug info before call */
2000 if ( debugging )
2002 if ( UsePIC )
2004 fprintf( outfile, "\tpushl %%ebx\n" );
2006 /* Get Global Offset Table into %ebx (for PLT call) */
2007 fprintf( outfile, "\tcall .LCallFrom16%s.getgot2\n", name );
2008 fprintf( outfile, ".LCallFrom16%s.getgot2:\n", name );
2009 fprintf( outfile, "\tpopl %%ebx\n" );
2010 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot2], %%ebx\n", name );
2013 fprintf( outfile, "\tpushl %%edx\n" );
2014 if ( reg_func )
2015 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2016 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
2017 else
2018 fprintf( outfile, "\tpushl $0\n" );
2020 if ( UsePIC )
2021 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16@PLT\n ");
2022 else
2023 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n ");
2025 fprintf( outfile, "\tpopl %%edx\n" );
2026 fprintf( outfile, "\tpopl %%edx\n" );
2028 if ( UsePIC )
2029 fprintf( outfile, "\tpopl %%ebx\n" );
2032 /* Call relay routine (which will call the API entry point) */
2033 fprintf( outfile, "\tleal %d(%%edx), %%eax\n", sizeof(STACK16FRAME) );
2034 fprintf( outfile, "\tpushl %%eax\n" );
2035 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point) );
2036 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay) );
2038 /* Print debug info after call */
2039 if ( debugging )
2041 if ( UsePIC )
2043 fprintf( outfile, "\tpushl %%ebx\n" );
2045 /* Get Global Offset Table into %ebx (for PLT call) */
2046 fprintf( outfile, "\tcall .LCallFrom16%s.getgot3\n", name );
2047 fprintf( outfile, ".LCallFrom16%s.getgot3:\n", name );
2048 fprintf( outfile, "\tpopl %%ebx\n" );
2049 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot3], %%ebx\n", name );
2052 fprintf( outfile, "\tpushl %%eax\n" );
2053 if ( reg_func )
2054 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2055 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
2056 else
2057 fprintf( outfile, "\tpushl $0\n" );
2059 if ( UsePIC )
2060 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret@PLT\n ");
2061 else
2062 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n ");
2064 fprintf( outfile, "\tpopl %%eax\n" );
2065 fprintf( outfile, "\tpopl %%eax\n" );
2067 if ( UsePIC )
2068 fprintf( outfile, "\tpopl %%ebx\n" );
2072 if ( reg_func )
2074 fprintf( outfile, "\tmovl %%esp, %%ebx\n" );
2076 /* Switch stack back */
2077 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2078 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
2079 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2081 /* Get return address to CallFrom16 stub */
2082 fprintf( outfile, "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip)-4 );
2083 fprintf( outfile, "\tpopl %%eax\n" );
2084 fprintf( outfile, "\tpopl %%edx\n" );
2086 /* Restore all registers from CONTEXT */
2087 fprintf( outfile, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs) );
2088 fprintf( outfile, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp) );
2089 fprintf( outfile, "\taddl $4, %%esp\n" ); /* room for final return address */
2091 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
2092 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
2093 fprintf( outfile, "\tpushl %%edx\n" );
2094 fprintf( outfile, "\tpushl %%eax\n" );
2095 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags) );
2096 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
2098 fprintf( outfile, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs) );
2099 fprintf( outfile, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs) );
2100 fprintf( outfile, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs) );
2102 fprintf( outfile, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp) );
2103 fprintf( outfile, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi) );
2104 fprintf( outfile, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi) );
2105 fprintf( outfile, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax) );
2106 fprintf( outfile, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx) );
2107 fprintf( outfile, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx) );
2108 fprintf( outfile, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx) );
2110 fprintf( outfile, "\tpopl %%ds\n" );
2111 fprintf( outfile, "\tpopfl\n" );
2112 fprintf( outfile, "\tlret\n" );
2114 else
2116 /* Switch stack back */
2117 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2118 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
2119 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2121 /* Restore registers */
2122 fprintf( outfile, "\tpopl %%edx\n" );
2123 fprintf( outfile, "\tpopl %%ecx\n" );
2124 fprintf( outfile, "\tpopl %%ebp\n" );
2125 fprintf( outfile, "\tpopw %%ds\n" );
2126 fprintf( outfile, "\tpopw %%es\n" );
2127 fprintf( outfile, "\tpopw %%fs\n" );
2128 fprintf( outfile, "\tpopw %%gs\n" );
2130 /* Prepare return value and set flags accordingly */
2131 if ( !short_ret )
2132 fprintf( outfile, "\tshldl $16, %%eax, %%edx\n" );
2133 fprintf( outfile, "\torl %%eax, %%eax\n" );
2135 /* Return to return stub which will return to caller */
2136 fprintf( outfile, "\tlret $12\n" );
2141 /*******************************************************************
2142 * BuildCallTo16Core
2144 * This routine builds the core routines used in 32->16 thunks:
2146 * extern void CALLBACK CallTo16Word( SEGPTR target, int nb_args );
2147 * extern void CALLBACK CallTo16Long( SEGPTR target, int nb_args );
2148 * extern void CALLBACK CallTo16RegisterShort( const CONTEXT86 *context, int nb_args );
2149 * extern void CALLBACK CallTo16RegisterLong ( const CONTEXT86 *context, int nb_args );
2151 * These routines can be called directly from 32-bit code.
2153 * All routines expect that the 16-bit stack contents (arguments) were
2154 * already set up by the caller; nb_args must contain the number of bytes
2155 * to be conserved. The 16-bit SS:SP will be set accordinly.
2157 * All other registers are either taken from the CONTEXT86 structure
2158 * or else set to default values. The target routine address is either
2159 * given directly or taken from the CONTEXT86.
2161 * If you want to call a 16-bit routine taking only standard argument types
2162 * (WORD and LONG), you can also have an appropriate argument conversion
2163 * stub automatically generated (see BuildCallTo16); you'd then call this
2164 * stub, which in turn would prepare the 16-bit stack and call the appropiate
2165 * core routine.
2169 static void BuildCallTo16Core( FILE *outfile, int short_ret, int reg_func )
2171 char *name = reg_func == 2 ? "RegisterLong" :
2172 reg_func == 1 ? "RegisterShort" :
2173 short_ret? "Word" : "Long";
2175 /* Function header */
2176 fprintf( outfile, "\n\t.align 4\n" );
2177 #ifdef USE_STABS
2178 fprintf( outfile, ".stabs \"CallTo16%s:F1\",36,0,0," PREFIX "CallTo16%s\n",
2179 name, name);
2180 #endif
2181 fprintf( outfile, "\t.globl " PREFIX "CallTo16%s\n", name );
2182 fprintf( outfile, PREFIX "CallTo16%s:\n", name );
2184 /* Function entry sequence */
2185 fprintf( outfile, "\tpushl %%ebp\n" );
2186 fprintf( outfile, "\tmovl %%esp, %%ebp\n" );
2188 /* Save the 32-bit registers */
2189 fprintf( outfile, "\tpushl %%ebx\n" );
2190 fprintf( outfile, "\tpushl %%ecx\n" );
2191 fprintf( outfile, "\tpushl %%edx\n" );
2192 fprintf( outfile, "\tpushl %%esi\n" );
2193 fprintf( outfile, "\tpushl %%edi\n" );
2195 if ( UsePIC )
2197 /* Get Global Offset Table into %ebx */
2198 fprintf( outfile, "\tcall .LCallTo16%s.getgot1\n", name );
2199 fprintf( outfile, ".LCallTo16%s.getgot1:\n", name );
2200 fprintf( outfile, "\tpopl %%ebx\n" );
2201 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot1], %%ebx\n", name );
2204 /* Enter Win16 Mutex */
2205 if ( UsePIC )
2206 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock@PLT\n" );
2207 else
2208 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock\n" );
2210 /* Print debugging info */
2211 if (debugging)
2213 /* Push flags, number of arguments, and target */
2214 fprintf( outfile, "\tpushl $%d\n", reg_func );
2215 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2216 fprintf( outfile, "\tpushl 8(%%ebp)\n" );
2218 if ( UsePIC )
2219 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16@PLT\n" );
2220 else
2221 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
2223 fprintf( outfile, "\taddl $12, %%esp\n" );
2226 /* Get return address */
2227 if ( UsePIC )
2228 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr@GOTOFF(%%ebx), %%ecx\n" );
2229 else
2230 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr, %%ecx\n" );
2232 /* Call the actual CallTo16 routine (simulate a lcall) */
2233 fprintf( outfile, "\tpushl %%cs\n" );
2234 fprintf( outfile, "\tcall .LCallTo16%s\n", name );
2236 /* Convert and push return value */
2237 if ( short_ret )
2239 fprintf( outfile, "\tmovzwl %%ax, %%eax\n" );
2240 fprintf( outfile, "\tpushl %%eax\n" );
2242 else if ( reg_func != 2 )
2244 fprintf( outfile, "\tshll $16,%%edx\n" );
2245 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
2246 fprintf( outfile, "\tpushl %%edx\n" );
2248 else
2249 fprintf( outfile, "\tpushl %%eax\n" );
2251 if ( UsePIC )
2253 /* Get Global Offset Table into %ebx (might have been overwritten) */
2254 fprintf( outfile, "\tcall .LCallTo16%s.getgot2\n", name );
2255 fprintf( outfile, ".LCallTo16%s.getgot2:\n", name );
2256 fprintf( outfile, "\tpopl %%ebx\n" );
2257 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot2], %%ebx\n", name );
2260 /* Print debugging info */
2261 if (debugging)
2263 if ( UsePIC )
2264 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret@PLT\n" );
2265 else
2266 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret\n" );
2269 /* Leave Win16 Mutex */
2270 if ( UsePIC )
2271 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock@PLT\n" );
2272 else
2273 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock\n" );
2275 /* Get return value */
2276 fprintf( outfile, "\tpopl %%eax\n" );
2278 /* Restore the 32-bit registers */
2279 fprintf( outfile, "\tpopl %%edi\n" );
2280 fprintf( outfile, "\tpopl %%esi\n" );
2281 fprintf( outfile, "\tpopl %%edx\n" );
2282 fprintf( outfile, "\tpopl %%ecx\n" );
2283 fprintf( outfile, "\tpopl %%ebx\n" );
2285 /* Function exit sequence */
2286 fprintf( outfile, "\tpopl %%ebp\n" );
2287 fprintf( outfile, "\tret $8\n" );
2290 /* Start of the actual CallTo16 routine */
2292 fprintf( outfile, ".LCallTo16%s:\n", name );
2294 /* Complete STACK32FRAME */
2295 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
2296 fprintf( outfile, "\tmovl %%esp,%%edx\n" );
2298 /* Switch to the 16-bit stack */
2299 #ifdef __svr4__
2300 fprintf( outfile,"\tdata16\n");
2301 #endif
2302 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
2303 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
2304 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
2306 /* Make %bp point to the previous stackframe (built by CallFrom16) */
2307 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
2308 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp) );
2310 /* Add the specified offset to the new sp */
2311 fprintf( outfile, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args) );
2313 /* Push the return address
2314 * With sreg suffix, we push 16:16 address (normal lret)
2315 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
2317 if (reg_func != 2)
2318 fprintf( outfile, "\tpushl %%ecx\n" );
2319 else
2321 fprintf( outfile, "\tshldl $16, %%ecx, %%eax\n" );
2322 fprintf( outfile, "\tpushw $0\n" );
2323 fprintf( outfile, "\tpushw %%ax\n" );
2324 fprintf( outfile, "\tpushw $0\n" );
2325 fprintf( outfile, "\tpushw %%cx\n" );
2328 if (reg_func)
2330 /* Push the called routine address */
2331 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target) );
2332 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs) );
2333 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip) );
2335 /* Get the registers */
2336 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs) );
2337 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs) );
2338 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2339 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs) );
2340 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2341 fprintf( outfile, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp) );
2342 fprintf( outfile, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi) );
2343 fprintf( outfile, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi) );
2344 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax) );
2345 fprintf( outfile, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx) );
2346 fprintf( outfile, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx) );
2347 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx) );
2349 /* Get the 16-bit ds */
2350 fprintf( outfile, "\tpopw %%ds\n" );
2352 else /* not a register function */
2354 /* Push the called routine address */
2355 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK32OFFSET(target) );
2357 /* Set %fs to the value saved by the last CallFrom16 */
2358 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs)-STACK16OFFSET(bp) );
2359 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2361 /* Set %ds and %es (and %ax just in case) equal to %ss */
2362 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
2363 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2364 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2367 /* Jump to the called routine */
2368 fprintf( outfile, "\t.byte 0x66\n" );
2369 fprintf( outfile, "\tlret\n" );
2372 /*******************************************************************
2373 * BuildRet16Func
2375 * Build the return code for 16-bit callbacks
2377 static void BuildRet16Func( FILE *outfile )
2380 * Note: This must reside in the .data section to allow
2381 * run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
2384 fprintf( outfile, "\n\t.globl " PREFIX "CallTo16_Ret\n" );
2385 fprintf( outfile, PREFIX "CallTo16_Ret:\n" );
2387 /* Restore 32-bit segment registers */
2389 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
2390 #ifdef __svr4__
2391 fprintf( outfile, "\tdata16\n");
2392 #endif
2393 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
2394 #ifdef __svr4__
2395 fprintf( outfile, "\tdata16\n");
2396 #endif
2397 fprintf( outfile, "\tmovw %%bx,%%es\n" );
2399 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
2401 /* Restore the 32-bit stack */
2403 #ifdef __svr4__
2404 fprintf( outfile, "\tdata16\n");
2405 #endif
2406 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
2407 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
2408 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2410 /* Return to caller */
2412 fprintf( outfile, "\tlret\n" );
2414 /* Declare the return address variable */
2416 fprintf( outfile, "\n\t.globl " PREFIX "CallTo16_RetAddr\n" );
2417 fprintf( outfile, PREFIX "CallTo16_RetAddr:\t.long 0\n" );
2420 /*******************************************************************
2421 * BuildCallTo32CBClient
2423 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
2425 * Since the relay stub is itself 32-bit, this should not be a problem;
2426 * unfortunately, the relay stubs are expected to switch back to a
2427 * 16-bit stack (and 16-bit code) after completion :-(
2429 * This would conflict with our 16- vs. 32-bit stack handling, so
2430 * we simply switch *back* to our 32-bit stack before returning to
2431 * the caller ...
2433 * The CBClient relay stub expects to be called with the following
2434 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
2435 * stack at the designated places:
2437 * ...
2438 * (ebp+14) original arguments to the callback routine
2439 * (ebp+10) far return address to original caller
2440 * (ebp+6) Thunklet target address
2441 * (ebp+2) Thunklet relay ID code
2442 * (ebp) BP (saved by CBClientGlueSL)
2443 * (ebp-2) SI (saved by CBClientGlueSL)
2444 * (ebp-4) DI (saved by CBClientGlueSL)
2445 * (ebp-6) DS (saved by CBClientGlueSL)
2447 * ... buffer space used by the 16-bit side glue for temp copies
2449 * (ebx+4) far return address to 16-bit side glue code
2450 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
2452 * The 32-bit side glue code accesses both the original arguments (via ebp)
2453 * and the temporary copies prepared by the 16-bit side glue (via ebx).
2454 * After completion, the stub will load ss:sp from the buffer at ebx
2455 * and perform a far return to 16-bit code.
2457 * To trick the relay stub into returning to us, we replace the 16-bit
2458 * return address to the glue code by a cs:ip pair pointing to our
2459 * return entry point (the original return address is saved first).
2460 * Our return stub thus called will then reload the 32-bit ss:esp and
2461 * return to 32-bit code (by using and ss:esp value that we have also
2462 * pushed onto the 16-bit stack before and a cs:eip values found at
2463 * that position on the 32-bit stack). The ss:esp to be restored is
2464 * found relative to the 16-bit stack pointer at:
2466 * (ebx-4) ss (flat)
2467 * (ebx-8) sp (32-bit stack pointer)
2469 * The second variant of this routine, CALL32_CBClientEx, which is used
2470 * to implement KERNEL.621, has to cope with yet another problem: Here,
2471 * the 32-bit side directly returns to the caller of the CBClient thunklet,
2472 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
2473 * As we have to return to our 32-bit code first, we have to adapt the
2474 * layout of our temporary area so as to include values for the registers
2475 * that are to be restored, and later (in the implementation of KERNEL.621)
2476 * we *really* restore them. The return stub restores DS, DI, SI, and BP
2477 * from the stack, skips the next 8 bytes (CBClient relay code / target),
2478 * and then performs a lret NN, where NN is the number of arguments to be
2479 * removed. Thus, we prepare our temporary area as follows:
2481 * (ebx+22) 16-bit cs (this segment)
2482 * (ebx+20) 16-bit ip ('16-bit' return entry point)
2483 * (ebx+16) 32-bit ss (flat)
2484 * (ebx+12) 32-bit sp (32-bit stack pointer)
2485 * (ebx+10) 16-bit bp (points to ebx+24)
2486 * (ebx+8) 16-bit si (ignored)
2487 * (ebx+6) 16-bit di (ignored)
2488 * (ebx+4) 16-bit ds (we actually use the flat DS here)
2489 * (ebx+2) 16-bit ss (16-bit stack segment)
2490 * (ebx+0) 16-bit sp (points to ebx+4)
2492 * Note that we ensure that DS is not changed and remains the flat segment,
2493 * and the 32-bit stack pointer our own return stub needs fits just
2494 * perfectly into the 8 bytes that are skipped by the Windows stub.
2495 * One problem is that we have to determine the number of removed arguments,
2496 * as these have to be really removed in KERNEL.621. Thus, the BP value
2497 * that we place in the temporary area to be restored, contains the value
2498 * that SP would have if no arguments were removed. By comparing the actual
2499 * value of SP with this value in our return stub we can compute the number
2500 * of removed arguments. This is then returned to KERNEL.621.
2502 * The stack layout of this function:
2503 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
2504 * (ebp+16) esi pointer to caller's esi value
2505 * (ebp+12) arg ebp value to be set for relay stub
2506 * (ebp+8) func CBClient relay stub address
2507 * (ebp+4) ret addr
2508 * (ebp) ebp
2510 static void BuildCallTo32CBClient( FILE *outfile, BOOL isEx )
2512 char *name = isEx? "CBClientEx" : "CBClient";
2513 int size = isEx? 24 : 12;
2515 /* Function header */
2517 fprintf( outfile, "\n\t.align 4\n" );
2518 #ifdef USE_STABS
2519 fprintf( outfile, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX "CALL32_%s\n",
2520 name, name );
2521 #endif
2522 fprintf( outfile, "\t.globl " PREFIX "CALL32_%s\n", name );
2523 fprintf( outfile, PREFIX "CALL32_%s:\n", name );
2525 /* Entry code */
2527 fprintf( outfile, "\tpushl %%ebp\n" );
2528 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2529 fprintf( outfile, "\tpushl %%edi\n" );
2530 fprintf( outfile, "\tpushl %%esi\n" );
2531 fprintf( outfile, "\tpushl %%ebx\n" );
2533 /* Get the 16-bit stack */
2535 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET);
2537 /* Convert it to a flat address */
2539 fprintf( outfile, "\tshldl $16,%%ebx,%%eax\n" );
2540 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
2541 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%esi\n" );
2542 fprintf( outfile, "\tmovw %%bx,%%ax\n" );
2543 fprintf( outfile, "\taddl %%eax,%%esi\n" );
2545 /* Allocate temporary area (simulate STACK16_PUSH) */
2547 fprintf( outfile, "\tpushf\n" );
2548 fprintf( outfile, "\tcld\n" );
2549 fprintf( outfile, "\tleal -%d(%%esi), %%edi\n", size );
2550 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2551 fprintf( outfile, "\trep\n\tmovsb\n" );
2552 fprintf( outfile, "\tpopf\n" );
2554 fprintf( outfile, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size, STACKOFFSET );
2556 fprintf( outfile, "\tpushl %%edi\n" ); /* remember address */
2558 /* Set up temporary area */
2560 if ( !isEx )
2562 fprintf( outfile, "\tleal 4(%%edi), %%edi\n" );
2564 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2565 fprintf( outfile, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */
2567 fprintf( outfile, "\tmovw %%ss, %%ax\n" );
2568 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2569 fprintf( outfile, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */
2571 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 + 4 );
2572 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */
2574 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
2575 fprintf( outfile, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */
2577 else
2579 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 );
2580 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" );
2582 fprintf( outfile, "\tmovw %%ds, %%ax\n" );
2583 fprintf( outfile, "\tmovw %%ax, 4(%%edi)\n" );
2585 fprintf( outfile, "\taddl $20, %%ebx\n" );
2586 fprintf( outfile, "\tmovw %%bx, 10(%%edi)\n" );
2588 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2589 fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
2591 fprintf( outfile, "\tmovw %%ss, %%ax\n" );
2592 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2593 fprintf( outfile, "\tmovl %%eax, 16(%%edi)\n" );
2595 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
2596 fprintf( outfile, "\tmovl %%eax, 20(%%edi)\n" );
2599 /* Set up registers and call CBClient relay stub (simulating a far call) */
2601 fprintf( outfile, "\tmovl 16(%%ebp), %%esi\n" );
2602 fprintf( outfile, "\tmovl (%%esi), %%esi\n" );
2604 fprintf( outfile, "\tmovl %%edi, %%ebx\n" );
2605 fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
2606 fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
2608 fprintf( outfile, "\tpushl %%cs\n" );
2609 fprintf( outfile, "\tcall *%%eax\n" );
2611 /* Return new esi value to caller */
2613 fprintf( outfile, "\tmovl 32(%%esp), %%edi\n" );
2614 fprintf( outfile, "\tmovl %%esi, (%%edi)\n" );
2616 /* Cleanup temporary area (simulate STACK16_POP) */
2618 fprintf( outfile, "\tpop %%esi\n" );
2620 fprintf( outfile, "\tpushf\n" );
2621 fprintf( outfile, "\tstd\n" );
2622 fprintf( outfile, "\tdec %%esi\n" );
2623 fprintf( outfile, "\tleal %d(%%esi), %%edi\n", size );
2624 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2625 fprintf( outfile, "\trep\n\tmovsb\n" );
2626 fprintf( outfile, "\tpopf\n" );
2628 fprintf( outfile, "\t.byte 0x64\n\taddw $%d,(%d)\n", size, STACKOFFSET );
2630 /* Return argument size to caller */
2631 if ( isEx )
2633 fprintf( outfile, "\tmovl 32(%%esp), %%ebx\n" );
2634 fprintf( outfile, "\tmovl %%ebp, (%%ebx)\n" );
2637 /* Restore registers and return */
2639 fprintf( outfile, "\tpopl %%ebx\n" );
2640 fprintf( outfile, "\tpopl %%esi\n" );
2641 fprintf( outfile, "\tpopl %%edi\n" );
2642 fprintf( outfile, "\tpopl %%ebp\n" );
2643 fprintf( outfile, "\tret\n" );
2646 static void BuildCallTo32CBClientRet( FILE *outfile, BOOL isEx )
2648 char *name = isEx? "CBClientEx" : "CBClient";
2650 /* '16-bit' return stub */
2652 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_Ret\n", name );
2653 fprintf( outfile, PREFIX "CALL32_%s_Ret:\n", name );
2655 if ( !isEx )
2657 fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
2658 fprintf( outfile, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
2660 else
2662 fprintf( outfile, "\tmovzwl %%bp, %%ebx\n" );
2663 fprintf( outfile, "\tsubw %%bp, %%sp\n" );
2664 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
2665 fprintf( outfile, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
2667 fprintf( outfile, "\tlret\n" );
2669 /* Declare the return address variable */
2671 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_RetAddr\n", name );
2672 fprintf( outfile, PREFIX "CALL32_%s_RetAddr:\t.long 0\n", name );
2676 /*******************************************************************
2677 * BuildCallTo32LargeStack
2679 * Build the function used to switch to the original 32-bit stack
2680 * before calling a 32-bit function from 32-bit code. This is used for
2681 * functions that need a large stack, like X bitmaps functions.
2683 * The generated function has the following prototype:
2684 * int xxx( int (*func)(), void *arg );
2686 * The pointer to the function can be retrieved by calling CALL32_Init,
2687 * which also takes care of saving the current 32-bit stack pointer.
2688 * Furthermore, CALL32_Init switches to a new stack and jumps to the
2689 * specified target address.
2691 * NOTE: The CALL32_LargeStack routine may be recursively entered by the
2692 * same thread, but not concurrently entered by several threads.
2694 * Stack layout of CALL32_Init:
2696 * (esp+12) new stack address
2697 * (esp+8) target address
2698 * (esp+4) pointer to variable to receive CALL32_LargeStack address
2699 * (esp) ret addr
2701 * Stack layout of CALL32_LargeStack:
2702 * ... ...
2703 * (ebp+12) arg
2704 * (ebp+8) func
2705 * (ebp+4) ret addr
2706 * (ebp) ebp
2708 static void BuildCallTo32LargeStack( FILE *outfile )
2710 /* Initialization function */
2712 fprintf( outfile, "\n\t.align 4\n" );
2713 #ifdef USE_STABS
2714 fprintf( outfile, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX "CALL32_Init\n");
2715 #endif
2716 fprintf( outfile, "\t.globl " PREFIX "CALL32_Init\n" );
2717 fprintf( outfile, "\t.type " PREFIX "CALL32_Init,@function\n" );
2718 fprintf( outfile, PREFIX "CALL32_Init:\n" );
2719 fprintf( outfile, "\tmovl %%esp,CALL32_Original32_esp\n" );
2720 fprintf( outfile, "\tpopl %%eax\n" );
2721 fprintf( outfile, "\tpopl %%eax\n" );
2722 fprintf( outfile, "\tmovl $CALL32_LargeStack,(%%eax)\n" );
2723 fprintf( outfile, "\tpopl %%eax\n" );
2724 fprintf( outfile, "\tpopl %%esp\n" );
2725 fprintf( outfile, "\tpushl %%eax\n" );
2726 fprintf( outfile, "\tret\n" );
2728 /* Function header */
2730 fprintf( outfile, "\n\t.align 4\n" );
2731 #ifdef USE_STABS
2732 fprintf( outfile, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2733 #endif
2734 fprintf( outfile, "CALL32_LargeStack:\n" );
2736 /* Entry code */
2738 fprintf( outfile, "\tpushl %%ebp\n" );
2739 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2741 /* Switch to the original 32-bit stack pointer */
2743 fprintf( outfile, "\tcmpl $0, CALL32_RecursionCount\n" );
2744 fprintf( outfile, "\tjne CALL32_skip\n" );
2745 fprintf( outfile, "\tmovl CALL32_Original32_esp, %%esp\n" );
2746 fprintf( outfile, "CALL32_skip:\n" );
2748 fprintf( outfile, "\tincl CALL32_RecursionCount\n" );
2750 /* Transfer the argument and call the function */
2752 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2753 fprintf( outfile, "\tcall *8(%%ebp)\n" );
2755 /* Restore registers and return */
2757 fprintf( outfile, "\tdecl CALL32_RecursionCount\n" );
2759 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2760 fprintf( outfile, "\tpopl %%ebp\n" );
2761 fprintf( outfile, "\tret\n" );
2763 /* Data */
2765 fprintf( outfile, "\t.data\n" );
2766 fprintf( outfile, "CALL32_Original32_esp:\t.long 0\n" );
2767 fprintf( outfile, "CALL32_RecursionCount:\t.long 0\n" );
2768 fprintf( outfile, "\t.text\n" );
2772 /*******************************************************************
2773 * BuildCallFrom32Regs
2775 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2776 * 'args' is the number of dword arguments.
2778 * Stack layout:
2779 * ...
2780 * (ebp+12) first arg
2781 * (ebp+8) ret addr to user code
2782 * (ebp+4) ret addr to relay code
2783 * (ebp+0) saved ebp
2784 * (ebp-128) buffer area to allow stack frame manipulation
2785 * (ebp-332) CONTEXT86 struct
2786 * (ebp-336) CONTEXT86 *argument
2787 * .... other arguments copied from (ebp+12)
2789 * The entry point routine is called with a CONTEXT* extra argument,
2790 * following the normal args. In this context structure, EIP_reg
2791 * contains the return address to user code, and ESP_reg the stack
2792 * pointer on return (with the return address and arguments already
2793 * removed).
2795 static void BuildCallFrom32Regs( FILE *outfile )
2797 static const int STACK_SPACE = 128 + sizeof(CONTEXT86);
2799 /* Function header */
2801 fprintf( outfile, "\n\t.align 4\n" );
2802 #ifdef USE_STABS
2803 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
2804 #endif
2805 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
2806 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
2808 /* Allocate some buffer space on the stack */
2810 fprintf( outfile, "\tpushl %%ebp\n" );
2811 fprintf( outfile, "\tmovl %%esp,%%ebp\n ");
2812 fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
2814 /* Build the context structure */
2816 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
2817 fprintf( outfile, "\tpushfl\n" );
2818 fprintf( outfile, "\tpopl %%eax\n" );
2819 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
2820 fprintf( outfile, "\tmovl 0(%%ebp),%%eax\n" );
2821 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
2822 fprintf( outfile, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
2823 fprintf( outfile, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
2824 fprintf( outfile, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
2825 fprintf( outfile, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
2826 fprintf( outfile, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
2828 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2829 fprintf( outfile, "\tmovw %%cs,%%ax\n" );
2830 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs) - STACK_SPACE );
2831 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2832 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
2833 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2834 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
2835 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2836 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
2837 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
2838 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs) - STACK_SPACE );
2839 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2840 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs) - STACK_SPACE );
2841 fprintf( outfile, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
2843 fprintf( outfile, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL );
2844 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags) - STACK_SPACE );
2846 fprintf( outfile, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
2847 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
2849 /* Transfer the arguments */
2851 fprintf( outfile, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
2852 fprintf( outfile, "\tpushl %%esp\n" ); /* push ptr to context struct */
2853 fprintf( outfile, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
2854 fprintf( outfile, "\tjecxz 1f\n" );
2855 fprintf( outfile, "\tsubl %%ecx,%%esp\n" );
2856 fprintf( outfile, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */
2857 fprintf( outfile, "\tmovl %%esp,%%edi\n" );
2858 fprintf( outfile, "\tshrl $2,%%ecx\n" );
2859 fprintf( outfile, "\tcld\n" );
2860 fprintf( outfile, "\trep\n\tmovsl\n" ); /* copy args */
2862 fprintf( outfile, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
2863 fprintf( outfile, "\tleal 12(%%ebp,%%eax),%%eax\n" );
2864 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2866 /* Call the entry point */
2868 fprintf( outfile, "\tcall *0(%%ebx)\n" );
2870 /* Store %eip and %ebp onto the new stack */
2872 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2873 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
2874 fprintf( outfile, "\tmovl %%eax,-4(%%edx)\n" );
2875 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
2876 fprintf( outfile, "\tmovl %%eax,-8(%%edx)\n" );
2878 /* Restore the context structure */
2880 /* Note: we don't bother to restore %cs, %ds and %ss
2881 * changing them in 32-bit code is a recipe for disaster anyway
2883 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
2884 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2885 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
2886 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2887 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
2888 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2890 fprintf( outfile, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
2891 fprintf( outfile, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
2892 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
2893 fprintf( outfile, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
2894 fprintf( outfile, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
2896 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
2897 fprintf( outfile, "\tpushl %%eax\n" );
2898 fprintf( outfile, "\tpopfl\n" );
2899 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
2901 fprintf( outfile, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2902 fprintf( outfile, "\tleal -8(%%ebp),%%esp\n" );
2903 fprintf( outfile, "\tpopl %%ebp\n" );
2904 fprintf( outfile, "\tret\n" );
2908 /*******************************************************************
2909 * BuildGlue
2911 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
2913 static void BuildGlue( FILE *outfile, FILE *infile )
2915 char buffer[1024];
2917 /* File header */
2919 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
2920 input_file_name );
2921 fprintf( outfile, "#include \"builtin16.h\"\n" );
2922 fprintf( outfile, "#include \"stackframe.h\"\n\n" );
2924 /* Build the callback glue functions */
2926 while (fgets( buffer, sizeof(buffer), infile ))
2928 if (strstr( buffer, "### start build ###" )) break;
2930 while (fgets( buffer, sizeof(buffer), infile ))
2932 char *p;
2933 if ( (p = strstr( buffer, "CallFrom16_" )) != NULL )
2935 char *q, *profile = p + strlen( "CallFrom16_" );
2936 for (q = profile; (*q == '_') || isalpha(*q); q++ )
2938 *q = '\0';
2939 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
2941 if ( ++q < p ) p[-1] = '\0'; else q = "";
2942 BuildCallFrom16Func( outfile, profile, q, FALSE );
2944 if ( (p = strstr( buffer, "CallTo16_" )) != NULL )
2946 char *q, *profile = p + strlen( "CallTo16_" );
2947 for (q = profile; (*q == '_') || isalpha(*q); q++ )
2949 *q = '\0';
2950 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
2952 if ( ++q < p ) p[-1] = '\0'; else q = "";
2953 BuildCallTo16Func( outfile, profile, q );
2955 if (strstr( buffer, "### stop build ###" )) break;
2958 fclose( infile );
2961 /*******************************************************************
2962 * BuildCall16
2964 * Build the 16-bit callbacks
2966 static void BuildCall16( FILE *outfile )
2968 /* File header */
2970 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2971 fprintf( outfile, "\t.text\n" );
2973 #ifdef __i386__
2975 #ifdef USE_STABS
2976 if (output_file_name)
2978 char buffer[1024];
2979 getcwd(buffer, sizeof(buffer));
2980 fprintf( outfile, "\t.file\t\"%s\"\n", output_file_name );
2983 * The stabs help the internal debugger as they are an indication that it
2984 * is sensible to step into a thunk/trampoline.
2986 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2987 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name );
2988 fprintf( outfile, "Code_Start:\n\n" );
2990 #endif
2991 fprintf( outfile, PREFIX"Call16_Start:\n" );
2992 fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
2993 fprintf( outfile, "\t.byte 0\n\n" );
2996 /* Standard CallFrom16 routine (WORD return) */
2997 BuildCallFrom16Core( outfile, FALSE, FALSE, TRUE );
2999 /* Standard CallFrom16 routine (DWORD return) */
3000 BuildCallFrom16Core( outfile, FALSE, FALSE, FALSE );
3002 /* Register CallFrom16 routine */
3003 BuildCallFrom16Core( outfile, TRUE, FALSE, FALSE );
3005 /* C16ThkSL CallFrom16 routine */
3006 BuildCallFrom16Core( outfile, FALSE, TRUE, FALSE );
3008 /* Standard CallTo16 routine (WORD return) */
3009 BuildCallTo16Core( outfile, TRUE, FALSE );
3011 /* Standard CallTo16 routine (DWORD return) */
3012 BuildCallTo16Core( outfile, FALSE, FALSE );
3014 /* Register CallTo16 routine (16:16 retf) */
3015 BuildCallTo16Core( outfile, FALSE, 1 );
3017 /* Register CallTo16 routine (16:32 retf) */
3018 BuildCallTo16Core( outfile, FALSE, 2 );
3020 /* CBClientThunkSL routine */
3021 BuildCallTo32CBClient( outfile, FALSE );
3023 /* CBClientThunkSLEx routine */
3024 BuildCallTo32CBClient( outfile, TRUE );
3026 fprintf( outfile, PREFIX"Call16_End:\n" );
3027 fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
3029 #ifdef USE_STABS
3030 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
3031 fprintf( outfile, ".Letext:\n");
3032 #endif
3034 /* The whole Call16_Ret segment must lie within the .data section */
3035 fprintf( outfile, "\n\t.data\n" );
3036 fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
3037 fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
3039 /* Standard CallTo16 return stub */
3040 BuildRet16Func( outfile );
3042 /* CBClientThunkSL return stub */
3043 BuildCallTo32CBClientRet( outfile, FALSE );
3045 /* CBClientThunkSLEx return stub */
3046 BuildCallTo32CBClientRet( outfile, TRUE );
3048 /* End of Call16_Ret segment */
3049 fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
3050 fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
3052 #else /* __i386__ */
3054 fprintf( outfile, PREFIX"Call16_Start:\n" );
3055 fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
3056 fprintf( outfile, "\t.byte 0\n\n" );
3057 fprintf( outfile, PREFIX"Call16_End:\n" );
3058 fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
3060 fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
3061 fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
3062 fprintf( outfile, "\t.byte 0\n\n" );
3063 fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
3064 fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
3066 #endif /* __i386__ */
3069 /*******************************************************************
3070 * BuildCall32
3072 * Build the 32-bit callbacks
3074 static void BuildCall32( FILE *outfile )
3076 /* File header */
3078 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
3079 fprintf( outfile, "\t.text\n" );
3081 #ifdef __i386__
3083 #ifdef USE_STABS
3084 if (output_file_name)
3086 char buffer[1024];
3087 getcwd(buffer, sizeof(buffer));
3088 fprintf( outfile, "\t.file\t\"%s\"\n", output_file_name );
3091 * The stabs help the internal debugger as they are an indication that it
3092 * is sensible to step into a thunk/trampoline.
3094 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
3095 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name );
3096 fprintf( outfile, "Code_Start:\n" );
3098 #endif
3100 /* Build the 32-bit large stack callback */
3102 BuildCallTo32LargeStack( outfile );
3104 /* Build the register callback function */
3106 BuildCallFrom32Regs( outfile );
3108 #ifdef USE_STABS
3109 fprintf( outfile, "\t.text\n");
3110 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
3111 fprintf( outfile, ".Letext:\n");
3112 #endif
3114 #else /* __i386__ */
3116 /* Just to avoid an empty file */
3117 fprintf( outfile, "\t.long 0\n" );
3119 #endif /* __i386__ */
3123 /*******************************************************************
3124 * usage
3126 static void usage(void)
3128 fprintf( stderr,
3129 "usage: build [-pic] [-o outfile] -spec SPEC_FILE\n"
3130 " build [-pic] [-o outfile] -glue SOURCE_FILE\n"
3131 " build [-pic] [-o outfile] -call16\n"
3132 " build [-pic] [-o outfile] -call32\n" );
3133 if (output_file_name) unlink( output_file_name );
3134 exit(1);
3138 /*******************************************************************
3139 * open_input
3141 static FILE *open_input( const char *name )
3143 FILE *f;
3145 if (!name)
3147 input_file_name = "<stdin>";
3148 return stdin;
3150 input_file_name = name;
3151 if (!(f = fopen( name, "r" )))
3153 fprintf( stderr, "Cannot open input file '%s'\n", name );
3154 if (output_file_name) unlink( output_file_name );
3155 exit(1);
3157 return f;
3160 /*******************************************************************
3161 * main
3163 int main(int argc, char **argv)
3165 FILE *outfile = stdout;
3167 if (argc < 2) usage();
3169 if (!strcmp( argv[1], "-pic" ))
3171 UsePIC = 1;
3172 argv += 1;
3173 argc -= 1;
3174 if (argc < 2) usage();
3177 if (!strcmp( argv[1], "-o" ))
3179 output_file_name = argv[2];
3180 argv += 2;
3181 argc -= 2;
3182 if (argc < 2) usage();
3183 if (!(outfile = fopen( output_file_name, "w" )))
3185 fprintf( stderr, "Unable to create output file '%s'\n", output_file_name );
3186 exit(1);
3190 /* Retrieve the selector values; this assumes that we are building
3191 * the asm files on the platform that will also run them. Probably
3192 * a safe assumption to make.
3194 Code_Selector = __get_cs();
3195 Data_Selector = __get_ds();
3197 if (!strcmp( argv[1], "-spec" )) BuildSpecFile( outfile, open_input( argv[2] ) );
3198 else if (!strcmp( argv[1], "-glue" )) BuildGlue( outfile, open_input( argv[2] ) );
3199 else if (!strcmp( argv[1], "-call16" )) BuildCall16( outfile );
3200 else if (!strcmp( argv[1], "-call32" )) BuildCall32( outfile );
3201 else
3203 fclose( outfile );
3204 usage();
3207 fclose( outfile );
3208 return 0;