Add prototype for CRTDLL_wcstol.
[wine/testsucceed.git] / tools / build.c
blob835ad88224ce625f13df5d9a4073004f25614847
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 "selectors.h"
24 #include "stackframe.h"
25 #include "builtin16.h"
26 #include "thread.h"
28 #ifdef NEED_UNDERSCORE_PREFIX
29 # define PREFIX "_"
30 #else
31 # define PREFIX
32 #endif
34 #ifdef HAVE_ASM_STRING
35 # define STRING ".string"
36 #else
37 # define STRING ".ascii"
38 #endif
40 #if defined(__GNUC__) && !defined(__svr4__)
41 # define USE_STABS
42 #else
43 # undef USE_STABS
44 #endif
46 typedef enum
48 TYPE_BYTE, /* byte variable (Win16) */
49 TYPE_WORD, /* word variable (Win16) */
50 TYPE_LONG, /* long variable (Win16) */
51 TYPE_PASCAL_16, /* pascal function with 16-bit return (Win16) */
52 TYPE_PASCAL, /* pascal function with 32-bit return (Win16) */
53 TYPE_ABS, /* absolute value (Win16) */
54 TYPE_REGISTER, /* register function */
55 TYPE_INTERRUPT, /* interrupt handler function (Win16) */
56 TYPE_STUB, /* unimplemented stub */
57 TYPE_STDCALL, /* stdcall function (Win32) */
58 TYPE_CDECL, /* cdecl function (Win32) */
59 TYPE_VARARGS, /* varargs function (Win32) */
60 TYPE_EXTERN, /* external symbol (Win32) */
61 TYPE_FORWARD, /* forwarded function (Win32) */
62 TYPE_NBTYPES
63 } ORD_TYPE;
65 static const char * const TypeNames[TYPE_NBTYPES] =
67 "byte", /* TYPE_BYTE */
68 "word", /* TYPE_WORD */
69 "long", /* TYPE_LONG */
70 "pascal16", /* TYPE_PASCAL_16 */
71 "pascal", /* TYPE_PASCAL */
72 "equate", /* TYPE_ABS */
73 "register", /* TYPE_REGISTER */
74 "interrupt", /* TYPE_INTERRUPT */
75 "stub", /* TYPE_STUB */
76 "stdcall", /* TYPE_STDCALL */
77 "cdecl", /* TYPE_CDECL */
78 "varargs", /* TYPE_VARARGS */
79 "extern", /* TYPE_EXTERN */
80 "forward" /* TYPE_FORWARD */
83 #define MAX_ORDINALS 2048
84 #define MAX_IMPORTS 16
86 /* Callback function used for stub functions */
87 #define STUB_CALLBACK \
88 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
90 typedef enum
92 SPEC_INVALID,
93 SPEC_WIN16,
94 SPEC_WIN32
95 } SPEC_TYPE;
97 typedef enum
99 SPEC_MODE_DLL,
100 SPEC_MODE_GUIEXE,
101 SPEC_MODE_CUIEXE
102 } SPEC_MODE;
104 typedef struct
106 int n_values;
107 int *values;
108 } ORD_VARIABLE;
110 typedef struct
112 int n_args;
113 char arg_types[32];
114 char link_name[80];
115 } ORD_FUNCTION;
117 typedef struct
119 int value;
120 } ORD_ABS;
122 typedef struct
124 char link_name[80];
125 } ORD_EXTERN;
127 typedef struct
129 char link_name[80];
130 } ORD_FORWARD;
132 typedef struct
134 ORD_TYPE type;
135 int ordinal;
136 int offset;
137 int lineno;
138 char name[80];
139 union
141 ORD_VARIABLE var;
142 ORD_FUNCTION func;
143 ORD_ABS abs;
144 ORD_EXTERN ext;
145 ORD_FORWARD fwd;
146 } u;
147 } ORDDEF;
149 static ORDDEF EntryPoints[MAX_ORDINALS];
150 static ORDDEF *Ordinals[MAX_ORDINALS];
151 static ORDDEF *Names[MAX_ORDINALS];
153 static SPEC_TYPE SpecType = SPEC_INVALID;
154 static SPEC_MODE SpecMode = SPEC_MODE_DLL;
155 static char DLLName[80];
156 static char DLLFileName[80];
157 static int Limit = 0;
158 static int Base = MAX_ORDINALS;
159 static int DLLHeapSize = 0;
160 static FILE *SpecFp;
161 static WORD Code_Selector, Data_Selector;
162 static char DLLInitFunc[80];
163 static char *DLLImports[MAX_IMPORTS];
164 static char rsrc_name[80];
165 static int nb_imports;
166 static int nb_entry_points;
167 static int nb_names;
168 static const char *input_file_name;
169 static const char *output_file_name;
171 char *ParseBuffer = NULL;
172 char *ParseNext;
173 char ParseSaveChar;
174 int Line;
176 static int UsePIC = 0;
178 static int debugging = 1;
180 /* Offset of a structure field relative to the start of the struct */
181 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
183 /* Offset of register relative to the start of the CONTEXT struct */
184 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT86,reg)
186 /* Offset of register relative to the start of the STACK16FRAME struct */
187 #define STACK16OFFSET(reg) STRUCTOFFSET(STACK16FRAME,reg)
189 /* Offset of register relative to the start of the STACK32FRAME struct */
190 #define STACK32OFFSET(reg) STRUCTOFFSET(STACK32FRAME,reg)
192 /* Offset of the stack pointer relative to %fs:(0) */
193 #define STACKOFFSET (STRUCTOFFSET(TEB,cur_stack))
195 static void BuildCallFrom16Func( FILE *outfile, char *profile, char *prefix, int local );
197 static void *xmalloc (size_t size)
199 void *res;
201 res = malloc (size ? size : 1);
202 if (res == NULL)
204 fprintf (stderr, "Virtual memory exhausted.\n");
205 if (output_file_name) unlink( output_file_name );
206 exit (1);
208 return res;
212 static void *xrealloc (void *ptr, size_t size)
214 void *res = realloc (ptr, size);
215 if (res == NULL)
217 fprintf (stderr, "Virtual memory exhausted.\n");
218 if (output_file_name) unlink( output_file_name );
219 exit (1);
221 return res;
224 static char *xstrdup( const char *str )
226 char *res = strdup( str );
227 if (!res)
229 fprintf (stderr, "Virtual memory exhausted.\n");
230 if (output_file_name) unlink( output_file_name );
231 exit (1);
233 return res;
236 static void fatal_error( const char *msg, ... )
238 va_list valist;
239 va_start( valist, msg );
240 fprintf( stderr, "%s:%d: ", input_file_name, Line );
241 vfprintf( stderr, msg, valist );
242 va_end( valist );
243 if (output_file_name) unlink( output_file_name );
244 exit(1);
247 static int IsNumberString(char *s)
249 while (*s != '\0')
250 if (!isdigit(*s++))
251 return 0;
253 return 1;
256 static char *strupper(char *s)
258 char *p;
260 for(p = s; *p != '\0'; p++)
261 *p = toupper(*p);
263 return s;
266 static char * GetTokenInLine(void)
268 char *p;
269 char *token;
271 if (ParseNext != ParseBuffer)
273 if (ParseSaveChar == '\0')
274 return NULL;
275 *ParseNext = ParseSaveChar;
279 * Remove initial white space.
281 for (p = ParseNext; isspace(*p); p++)
284 if ((*p == '\0') || (*p == '#'))
285 return NULL;
288 * Find end of token.
290 token = p++;
291 if (*token != '(' && *token != ')')
292 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
293 p++;
295 ParseSaveChar = *p;
296 ParseNext = p;
297 *p = '\0';
299 return token;
302 static char * GetToken(void)
304 char *token;
306 if (ParseBuffer == NULL)
308 ParseBuffer = xmalloc(512);
309 ParseNext = ParseBuffer;
310 while (1)
312 Line++;
313 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
314 return NULL;
315 if (ParseBuffer[0] != '#')
316 break;
320 while ((token = GetTokenInLine()) == NULL)
322 ParseNext = ParseBuffer;
323 while (1)
325 Line++;
326 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
327 return NULL;
328 if (ParseBuffer[0] != '#')
329 break;
333 return token;
337 static int name_compare( const void *name1, const void *name2 )
339 ORDDEF *odp1 = *(ORDDEF **)name1;
340 ORDDEF *odp2 = *(ORDDEF **)name2;
341 return strcmp( odp1->name, odp2->name );
344 /*******************************************************************
345 * AssignOrdinals
347 * Assign ordinals to all entry points.
349 static void AssignOrdinals(void)
351 int i, ordinal;
353 if ( !nb_names ) return;
355 /* sort the list of names */
356 qsort( Names, nb_names, sizeof(Names[0]), name_compare );
358 /* check for duplicate names */
359 for (i = 0; i < nb_names - 1; i++)
361 if (!strcmp( Names[i]->name, Names[i+1]->name ))
363 Line = MAX( Names[i]->lineno, Names[i+1]->lineno );
364 fatal_error( "'%s' redefined (previous definition at line %d)\n",
365 Names[i]->name, MIN( Names[i]->lineno, Names[i+1]->lineno ) );
369 /* start assigning from Base, or from 1 if no ordinal defined yet */
370 if (Base == MAX_ORDINALS) Base = 1;
371 for (i = 0, ordinal = Base; i < nb_names; i++)
373 if (Names[i]->ordinal != -1) continue; /* already has an ordinal */
374 while (Ordinals[ordinal]) ordinal++;
375 if (ordinal >= MAX_ORDINALS)
377 Line = Names[i]->lineno;
378 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
380 Names[i]->ordinal = ordinal;
381 Ordinals[ordinal] = Names[i];
383 if (ordinal > Limit) Limit = ordinal;
387 /*******************************************************************
388 * ParseVariable
390 * Parse a variable definition.
392 static void ParseVariable( ORDDEF *odp )
394 char *endptr;
395 int *value_array;
396 int n_values;
397 int value_array_size;
399 char *token = GetToken();
400 if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
402 n_values = 0;
403 value_array_size = 25;
404 value_array = xmalloc(sizeof(*value_array) * value_array_size);
406 while ((token = GetToken()) != NULL)
408 if (*token == ')')
409 break;
411 value_array[n_values++] = strtol(token, &endptr, 0);
412 if (n_values == value_array_size)
414 value_array_size += 25;
415 value_array = xrealloc(value_array,
416 sizeof(*value_array) * value_array_size);
419 if (endptr == NULL || *endptr != '\0')
420 fatal_error( "Expected number value, got '%s'\n", token );
423 if (token == NULL)
424 fatal_error( "End of file in variable declaration\n" );
426 odp->u.var.n_values = n_values;
427 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
431 /*******************************************************************
432 * ParseExportFunction
434 * Parse a function definition.
436 static void ParseExportFunction( ORDDEF *odp )
438 char *token;
439 int i;
441 switch(SpecType)
443 case SPEC_WIN16:
444 if (odp->type == TYPE_STDCALL)
445 fatal_error( "'stdcall' not supported for Win16\n" );
446 if (odp->type == TYPE_VARARGS)
447 fatal_error( "'varargs' not supported for Win16\n" );
448 break;
449 case SPEC_WIN32:
450 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
451 fatal_error( "'pascal' not supported for Win32\n" );
452 break;
453 default:
454 break;
457 token = GetToken();
458 if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
460 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
462 token = GetToken();
463 if (*token == ')')
464 break;
466 if (!strcmp(token, "word"))
467 odp->u.func.arg_types[i] = 'w';
468 else if (!strcmp(token, "s_word"))
469 odp->u.func.arg_types[i] = 's';
470 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
471 odp->u.func.arg_types[i] = 'l';
472 else if (!strcmp(token, "ptr"))
473 odp->u.func.arg_types[i] = 'p';
474 else if (!strcmp(token, "str"))
475 odp->u.func.arg_types[i] = 't';
476 else if (!strcmp(token, "wstr"))
477 odp->u.func.arg_types[i] = 'W';
478 else if (!strcmp(token, "segstr"))
479 odp->u.func.arg_types[i] = 'T';
480 else if (!strcmp(token, "double"))
482 odp->u.func.arg_types[i++] = 'l';
483 odp->u.func.arg_types[i] = 'l';
485 else fatal_error( "Unknown variable type '%s'\n", token );
487 if (SpecType == SPEC_WIN32)
489 if (strcmp(token, "long") &&
490 strcmp(token, "ptr") &&
491 strcmp(token, "str") &&
492 strcmp(token, "wstr") &&
493 strcmp(token, "double"))
495 fatal_error( "Type '%s' not supported for Win32\n", token );
499 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
500 fatal_error( "Too many arguments\n" );
502 odp->u.func.arg_types[i] = '\0';
503 if ((odp->type == TYPE_STDCALL) && !i)
504 odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
505 strcpy(odp->u.func.link_name, GetToken());
509 /*******************************************************************
510 * ParseEquate
512 * Parse an 'equate' definition.
514 static void ParseEquate( ORDDEF *odp )
516 char *endptr;
518 char *token = GetToken();
519 int value = strtol(token, &endptr, 0);
520 if (endptr == NULL || *endptr != '\0')
521 fatal_error( "Expected number value, got '%s'\n", token );
522 if (SpecType == SPEC_WIN32)
523 fatal_error( "'equate' not supported for Win32\n" );
524 odp->u.abs.value = value;
528 /*******************************************************************
529 * ParseStub
531 * Parse a 'stub' definition.
533 static void ParseStub( ORDDEF *odp )
535 odp->u.func.arg_types[0] = '\0';
536 strcpy( odp->u.func.link_name, STUB_CALLBACK );
540 /*******************************************************************
541 * ParseInterrupt
543 * Parse an 'interrupt' definition.
545 static void ParseInterrupt( ORDDEF *odp )
547 char *token;
549 if (SpecType == SPEC_WIN32)
550 fatal_error( "'interrupt' not supported for Win32\n" );
552 token = GetToken();
553 if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
555 token = GetToken();
556 if (*token != ')') fatal_error( "Expected ')' got '%s'\n", token );
558 odp->u.func.arg_types[0] = '\0';
559 strcpy( odp->u.func.link_name, GetToken() );
563 /*******************************************************************
564 * ParseExtern
566 * Parse an 'extern' definition.
568 static void ParseExtern( ORDDEF *odp )
570 if (SpecType == SPEC_WIN16) fatal_error( "'extern' not supported for Win16\n" );
571 strcpy( odp->u.ext.link_name, GetToken() );
575 /*******************************************************************
576 * ParseForward
578 * Parse a 'forward' definition.
580 static void ParseForward( ORDDEF *odp )
582 if (SpecType == SPEC_WIN16) fatal_error( "'forward' not supported for Win16\n" );
583 strcpy( odp->u.fwd.link_name, GetToken() );
587 /*******************************************************************
588 * ParseOrdinal
590 * Parse an ordinal definition.
592 static void ParseOrdinal(int ordinal)
594 char *token;
596 ORDDEF *odp = &EntryPoints[nb_entry_points++];
598 if (!(token = GetToken())) fatal_error( "Expected type after ordinal\n" );
600 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
601 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
602 break;
604 if (odp->type >= TYPE_NBTYPES)
605 fatal_error( "Expected type after ordinal, found '%s' instead\n", token );
607 if (!(token = GetToken())) fatal_error( "Expected name after type\n" );
609 strcpy( odp->name, token );
610 odp->lineno = Line;
611 odp->ordinal = ordinal;
613 switch(odp->type)
615 case TYPE_BYTE:
616 case TYPE_WORD:
617 case TYPE_LONG:
618 ParseVariable( odp );
619 break;
620 case TYPE_REGISTER:
621 ParseExportFunction( odp );
622 #ifndef __i386__
623 /* ignore Win32 'register' routines on non-Intel archs */
624 if (SpecType == SPEC_WIN32)
626 nb_entry_points--;
627 return;
629 #endif
630 break;
631 case TYPE_PASCAL_16:
632 case TYPE_PASCAL:
633 case TYPE_STDCALL:
634 case TYPE_VARARGS:
635 case TYPE_CDECL:
636 ParseExportFunction( odp );
637 break;
638 case TYPE_INTERRUPT:
639 ParseInterrupt( odp );
640 break;
641 case TYPE_ABS:
642 ParseEquate( odp );
643 break;
644 case TYPE_STUB:
645 ParseStub( odp );
646 break;
647 case TYPE_EXTERN:
648 ParseExtern( odp );
649 break;
650 case TYPE_FORWARD:
651 ParseForward( odp );
652 break;
653 default:
654 assert( 0 );
657 if (ordinal != -1)
659 if (ordinal >= MAX_ORDINALS) fatal_error( "Ordinal number %d too large\n", ordinal );
660 if (ordinal > Limit) Limit = ordinal;
661 if (ordinal < Base) Base = ordinal;
662 odp->ordinal = ordinal;
663 Ordinals[ordinal] = odp;
666 if (!strcmp( odp->name, "@" ))
668 if (ordinal == -1)
669 fatal_error( "Nameless function needs an explicit ordinal number\n" );
670 if (SpecType != SPEC_WIN32)
671 fatal_error( "Nameless functions not supported for Win16\n" );
672 odp->name[0] = 0;
674 else Names[nb_names++] = odp;
678 /*******************************************************************
679 * ParseTopLevel
681 * Parse a spec file.
683 static void ParseTopLevel(void)
685 char *token;
687 while ((token = GetToken()) != NULL)
689 if (strcmp(token, "name") == 0)
691 strcpy(DLLName, GetToken());
692 strupper(DLLName);
694 else if (strcmp(token, "file") == 0)
696 strcpy(DLLFileName, GetToken());
697 strupper(DLLFileName);
699 else if (strcmp(token, "type") == 0)
701 token = GetToken();
702 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
703 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
704 else fatal_error( "Type must be 'win16' or 'win32'\n" );
706 else if (strcmp(token, "mode") == 0)
708 token = GetToken();
709 if (!strcmp(token, "dll" )) SpecMode = SPEC_MODE_DLL;
710 else if (!strcmp(token, "guiexe" )) SpecMode = SPEC_MODE_GUIEXE;
711 else if (!strcmp(token, "cuiexe" )) SpecMode = SPEC_MODE_CUIEXE;
712 else fatal_error( "Mode must be 'dll', 'guiexe' or 'cuiexe'\n" );
714 else if (strcmp(token, "heap") == 0)
716 token = GetToken();
717 if (!IsNumberString(token)) fatal_error( "Expected number after heap\n" );
718 DLLHeapSize = atoi(token);
720 else if (strcmp(token, "init") == 0)
722 strcpy(DLLInitFunc, GetToken());
723 if (SpecType == SPEC_WIN16)
724 fatal_error( "init cannot be used for Win16 spec files\n" );
725 if (!DLLInitFunc[0])
726 fatal_error( "Expected function name after init\n" );
728 else if (strcmp(token, "import") == 0)
730 if (nb_imports >= MAX_IMPORTS)
731 fatal_error( "Too many imports (limit %d)\n", MAX_IMPORTS );
732 if (SpecType != SPEC_WIN32)
733 fatal_error( "Imports not supported for Win16\n" );
734 DLLImports[nb_imports++] = xstrdup(GetToken());
736 else if (strcmp(token, "rsrc") == 0)
738 strcpy( rsrc_name, GetToken() );
739 strcat( rsrc_name, "_ResourceDescriptor" );
741 else if (strcmp(token, "@") == 0)
743 if (SpecType != SPEC_WIN32)
744 fatal_error( "'@' ordinals not supported for Win16\n" );
745 ParseOrdinal( -1 );
747 else if (IsNumberString(token))
749 ParseOrdinal( atoi(token) );
751 else
752 fatal_error( "Expected name, id, length or ordinal\n" );
755 if (!DLLFileName[0])
757 if (SpecMode == SPEC_MODE_DLL)
758 sprintf( DLLFileName, "%s.DLL", DLLName );
759 else
760 sprintf( DLLFileName, "%s.EXE", DLLName );
765 /*******************************************************************
766 * StoreVariableCode
768 * Store a list of ints into a byte array.
770 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
772 int i;
774 switch(size)
776 case 1:
777 for (i = 0; i < odp->u.var.n_values; i++)
778 buffer[i] = odp->u.var.values[i];
779 break;
780 case 2:
781 for (i = 0; i < odp->u.var.n_values; i++)
782 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
783 break;
784 case 4:
785 for (i = 0; i < odp->u.var.n_values; i++)
786 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
787 break;
789 return odp->u.var.n_values * size;
793 /*******************************************************************
794 * DumpBytes
796 * Dump a byte stream into the assembly code.
798 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
799 const char *label )
801 int i;
803 fprintf( outfile, "\nstatic BYTE %s[] = \n{", label );
805 for (i = 0; i < len; i++)
807 if (!(i & 0x0f)) fprintf( outfile, "\n " );
808 fprintf( outfile, "%d", *data++ );
809 if (i < len - 1) fprintf( outfile, ", " );
811 fprintf( outfile, "\n};\n" );
815 /*******************************************************************
816 * BuildModule16
818 * Build the in-memory representation of a 16-bit NE module, and dump it
819 * as a byte stream into the assembly code.
821 static int BuildModule16( FILE *outfile, int max_code_offset,
822 int max_data_offset )
824 int i;
825 char *buffer;
826 NE_MODULE *pModule;
827 SEGTABLEENTRY *pSegment;
828 OFSTRUCT *pFileInfo;
829 BYTE *pstr;
830 WORD *pword;
831 ET_BUNDLE *bundle = 0;
832 ET_ENTRY *entry = 0;
834 /* Module layout:
835 * NE_MODULE Module
836 * OFSTRUCT File information
837 * SEGTABLEENTRY Segment 1 (code)
838 * SEGTABLEENTRY Segment 2 (data)
839 * WORD[2] Resource table (empty)
840 * BYTE[2] Imported names (empty)
841 * BYTE[n] Resident names table
842 * BYTE[n] Entry table
845 buffer = xmalloc( 0x10000 );
847 pModule = (NE_MODULE *)buffer;
848 memset( pModule, 0, sizeof(*pModule) );
849 pModule->magic = IMAGE_OS2_SIGNATURE;
850 pModule->count = 1;
851 pModule->next = 0;
852 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
853 pModule->dgroup = 2;
854 pModule->heap_size = DLLHeapSize;
855 pModule->stack_size = 0;
856 pModule->ip = 0;
857 pModule->cs = 0;
858 pModule->sp = 0;
859 pModule->ss = 0;
860 pModule->seg_count = 2;
861 pModule->modref_count = 0;
862 pModule->nrname_size = 0;
863 pModule->modref_table = 0;
864 pModule->nrname_fpos = 0;
865 pModule->moveable_entries = 0;
866 pModule->alignment = 0;
867 pModule->truetype = 0;
868 pModule->os_flags = NE_OSFLAGS_WINDOWS;
869 pModule->misc_flags = 0;
870 pModule->dlls_to_init = 0;
871 pModule->nrname_handle = 0;
872 pModule->min_swap_area = 0;
873 pModule->expected_version = 0;
874 pModule->module32 = 0;
875 pModule->self = 0;
876 pModule->self_loading_sel = 0;
878 /* File information */
880 pFileInfo = (OFSTRUCT *)(pModule + 1);
881 pModule->fileinfo = (int)pFileInfo - (int)pModule;
882 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
883 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
884 + strlen(DLLFileName);
885 strcpy( pFileInfo->szPathName, DLLFileName );
886 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
888 #ifdef __i386__ /* FIXME: Alignment problems! */
890 /* Segment table */
892 pSegment = (SEGTABLEENTRY *)pstr;
893 pModule->seg_table = (int)pSegment - (int)pModule;
894 pSegment->filepos = 0;
895 pSegment->size = max_code_offset;
896 pSegment->flags = 0;
897 pSegment->minsize = max_code_offset;
898 pSegment->hSeg = 0;
899 pSegment++;
901 pModule->dgroup_entry = (int)pSegment - (int)pModule;
902 pSegment->filepos = 0;
903 pSegment->size = max_data_offset;
904 pSegment->flags = NE_SEGFLAGS_DATA;
905 pSegment->minsize = max_data_offset;
906 pSegment->hSeg = 0;
907 pSegment++;
909 /* Resource table */
911 pword = (WORD *)pSegment;
912 pModule->res_table = (int)pword - (int)pModule;
913 *pword++ = 0;
914 *pword++ = 0;
916 /* Imported names table */
918 pstr = (char *)pword;
919 pModule->import_table = (int)pstr - (int)pModule;
920 *pstr++ = 0;
921 *pstr++ = 0;
923 /* Resident names table */
925 pModule->name_table = (int)pstr - (int)pModule;
926 /* First entry is module name */
927 *pstr = strlen(DLLName );
928 strcpy( pstr + 1, DLLName );
929 pstr += *pstr + 1;
930 *(WORD *)pstr = 0;
931 pstr += sizeof(WORD);
932 /* Store all ordinals */
933 for (i = 1; i <= Limit; i++)
935 ORDDEF *odp = Ordinals[i];
936 if (!odp || !odp->name[0]) continue;
937 *pstr = strlen( odp->name );
938 strcpy( pstr + 1, odp->name );
939 strupper( pstr + 1 );
940 pstr += *pstr + 1;
941 *(WORD *)pstr = i;
942 pstr += sizeof(WORD);
944 *pstr++ = 0;
946 /* Entry table */
948 pModule->entry_table = (int)pstr - (int)pModule;
949 for (i = 1; i <= Limit; i++)
951 int selector = 0;
952 ORDDEF *odp = Ordinals[i];
953 if (!odp) continue;
955 switch (odp->type)
957 case TYPE_CDECL:
958 case TYPE_PASCAL:
959 case TYPE_PASCAL_16:
960 case TYPE_REGISTER:
961 case TYPE_INTERRUPT:
962 case TYPE_STUB:
963 selector = 1; /* Code selector */
964 break;
966 case TYPE_BYTE:
967 case TYPE_WORD:
968 case TYPE_LONG:
969 selector = 2; /* Data selector */
970 break;
972 case TYPE_ABS:
973 selector = 0xfe; /* Constant selector */
974 break;
976 default:
977 selector = 0; /* Invalid selector */
978 break;
981 if ( !selector )
982 continue;
984 if ( bundle && bundle->last+1 == i )
985 bundle->last++;
986 else
988 if ( bundle )
989 bundle->next = (char *)pstr - (char *)pModule;
991 bundle = (ET_BUNDLE *)pstr;
992 bundle->first = i-1;
993 bundle->last = i;
994 bundle->next = 0;
995 pstr += sizeof(ET_BUNDLE);
998 /* FIXME: is this really correct ?? */
999 entry = (ET_ENTRY *)pstr;
1000 entry->type = 0xff; /* movable */
1001 entry->flags = 3; /* exported & public data */
1002 entry->segnum = selector;
1003 entry->offs = odp->offset;
1004 pstr += sizeof(ET_ENTRY);
1006 *pstr++ = 0;
1007 #endif
1009 /* Dump the module content */
1011 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
1012 "Module" );
1013 return (int)pstr - (int)pModule;
1017 /*******************************************************************
1018 * BuildSpec32File
1020 * Build a Win32 C file from a spec file.
1022 static int BuildSpec32File( FILE *outfile )
1024 ORDDEF *odp;
1025 int i, fwd_size = 0, have_regs = FALSE;
1026 int nr_exports;
1028 AssignOrdinals();
1029 nr_exports = Base <= Limit ? Limit - Base + 1 : 0;
1031 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
1032 input_file_name );
1033 fprintf( outfile, "#include \"builtin32.h\"\n\n" );
1034 fprintf( outfile, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
1035 DLLName );
1037 /* Output the DLL functions prototypes */
1039 for (i = 0, odp = EntryPoints; i < nb_entry_points; i++, odp++)
1041 switch(odp->type)
1043 case TYPE_EXTERN:
1044 fprintf( outfile, "extern void %s();\n", odp->u.ext.link_name );
1045 break;
1046 case TYPE_STDCALL:
1047 case TYPE_VARARGS:
1048 case TYPE_CDECL:
1049 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1050 break;
1051 case TYPE_FORWARD:
1052 fwd_size += strlen(odp->u.fwd.link_name) + 1;
1053 break;
1054 case TYPE_REGISTER:
1055 fprintf( outfile, "extern void __regs_%d();\n", odp->ordinal );
1056 have_regs = TRUE;
1057 break;
1058 case TYPE_STUB:
1059 fprintf( outfile, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
1060 odp->ordinal, DLLName, odp->ordinal );
1061 break;
1062 default:
1063 fprintf(stderr,"build: function type %d not available for Win32\n",
1064 odp->type);
1065 return -1;
1069 /* Output LibMain function */
1070 if (DLLInitFunc[0]) fprintf( outfile, "extern void %s();\n", DLLInitFunc );
1073 /* Output code for all register functions */
1075 if ( have_regs )
1077 fprintf( outfile, "#ifndef __GNUC__\n" );
1078 fprintf( outfile, "static void __asm__dummy() {\n" );
1079 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
1080 for (i = 0, odp = EntryPoints; i < nb_entry_points; i++, odp++)
1082 if (odp->type != TYPE_REGISTER) continue;
1083 fprintf( outfile,
1084 "__asm__(\".align 4\\n\\t\"\n"
1085 " \".type " PREFIX "__regs_%d,@function\\n\\t\"\n"
1086 " \"" PREFIX "__regs_%d:\\n\\t\"\n"
1087 " \"call " PREFIX "CALL32_Regs\\n\\t\"\n"
1088 " \".long " PREFIX "%s\\n\\t\"\n"
1089 " \".byte %d,%d\");\n",
1090 odp->ordinal, odp->ordinal, odp->u.func.link_name,
1091 4 * strlen(odp->u.func.arg_types),
1092 4 * strlen(odp->u.func.arg_types) );
1094 fprintf( outfile, "#ifndef __GNUC__\n" );
1095 fprintf( outfile, "}\n" );
1096 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
1099 /* Output the DLL functions table */
1101 fprintf( outfile, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1102 nr_exports );
1103 for (i = Base; i <= Limit; i++)
1105 ORDDEF *odp = Ordinals[i];
1106 if (!odp) fprintf( outfile, " 0" );
1107 else switch(odp->type)
1109 case TYPE_EXTERN:
1110 fprintf( outfile, " %s", odp->u.ext.link_name );
1111 break;
1112 case TYPE_STDCALL:
1113 case TYPE_VARARGS:
1114 case TYPE_CDECL:
1115 fprintf( outfile, " %s", odp->u.func.link_name);
1116 break;
1117 case TYPE_STUB:
1118 fprintf( outfile, " __stub_%d", i );
1119 break;
1120 case TYPE_REGISTER:
1121 fprintf( outfile, " __regs_%d", i );
1122 break;
1123 case TYPE_FORWARD:
1124 fprintf( outfile, " (ENTRYPOINT32)\"%s\"", odp->u.fwd.link_name );
1125 break;
1126 default:
1127 return -1;
1129 if (i < Limit) fprintf( outfile, ",\n" );
1131 fprintf( outfile, "\n};\n\n" );
1133 /* Output the DLL names table */
1135 fprintf( outfile, "static const char * const FuncNames[%d] =\n{\n", nb_names );
1136 for (i = 0; i < nb_names; i++)
1138 if (i) fprintf( outfile, ",\n" );
1139 fprintf( outfile, " \"%s\"", Names[i]->name );
1141 fprintf( outfile, "\n};\n\n" );
1143 /* Output the DLL ordinals table */
1145 fprintf( outfile, "static const unsigned short FuncOrdinals[%d] =\n{\n", nb_names );
1146 for (i = 0; i < nb_names; i++)
1148 if (i) fprintf( outfile, ",\n" );
1149 fprintf( outfile, " %d", Names[i]->ordinal - Base );
1151 fprintf( outfile, "\n};\n\n" );
1153 /* Output the DLL argument types */
1155 fprintf( outfile, "static const unsigned int ArgTypes[%d] =\n{\n",
1156 nr_exports );
1157 for (i = Base; i <= Limit; i++)
1159 ORDDEF *odp = Ordinals[i];
1160 unsigned int j, mask = 0;
1161 if (odp &&
1162 ((odp->type == TYPE_STDCALL) || (odp->type == TYPE_CDECL) ||
1163 (odp->type == TYPE_REGISTER)))
1164 for (j = 0; odp->u.func.arg_types[j]; j++)
1166 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
1167 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
1169 fprintf( outfile, " %d", mask );
1170 if (i < Limit) fprintf( outfile, ",\n" );
1172 fprintf( outfile, "\n};\n\n" );
1174 /* Output the DLL functions arguments */
1176 fprintf( outfile, "static const unsigned char FuncArgs[%d] =\n{\n",
1177 nr_exports );
1178 for (i = Base; i <= Limit; i++)
1180 unsigned char args = 0xff;
1181 ORDDEF *odp = Ordinals[i];
1182 if (odp) switch(odp->type)
1184 case TYPE_STDCALL:
1185 args = (unsigned char)strlen(odp->u.func.arg_types);
1186 break;
1187 case TYPE_CDECL:
1188 args = 0x80 | (unsigned char)strlen(odp->u.func.arg_types);
1189 break;
1190 case TYPE_REGISTER:
1191 args = 0x40 | (unsigned char)strlen(odp->u.func.arg_types);
1192 break;
1193 case TYPE_FORWARD:
1194 args = 0xfd;
1195 break;
1196 default:
1197 args = 0xff;
1198 break;
1200 fprintf( outfile, " 0x%02x", args );
1201 if (i < Limit) fprintf( outfile, ",\n" );
1203 fprintf( outfile, "\n};\n\n" );
1205 /* Output the DLL imports */
1207 if (nb_imports)
1209 fprintf( outfile, "static const char * const Imports[%d] =\n{\n", nb_imports );
1210 for (i = 0; i < nb_imports; i++)
1212 fprintf( outfile, " \"%s\"", DLLImports[i] );
1213 if (i < nb_imports-1) fprintf( outfile, ",\n" );
1215 fprintf( outfile, "\n};\n\n" );
1218 /* Output the DLL descriptor */
1220 if (rsrc_name[0]) fprintf( outfile, "extern const char %s[];\n\n", rsrc_name );
1222 fprintf( outfile, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1223 DLLName );
1224 fprintf( outfile, " \"%s\",\n", DLLName );
1225 fprintf( outfile, " \"%s\",\n", DLLFileName );
1226 fprintf( outfile, " %d,\n", nr_exports? Base : 0 );
1227 fprintf( outfile, " %d,\n", nr_exports );
1228 fprintf( outfile, " %d,\n", nb_names );
1229 fprintf( outfile, " %d,\n", nb_imports );
1230 fprintf( outfile, " %d,\n", (fwd_size + 3) & ~3 );
1231 fprintf( outfile,
1232 " Functions,\n"
1233 " FuncNames,\n"
1234 " FuncOrdinals,\n"
1235 " FuncArgs,\n"
1236 " ArgTypes,\n");
1237 fprintf( outfile, " %s,\n", nb_imports ? "Imports" : "0" );
1238 fprintf( outfile, " %s,\n", DLLInitFunc[0] ? DLLInitFunc : "0" );
1239 fprintf( outfile, " %d,\n", SpecMode == SPEC_MODE_DLL ? IMAGE_FILE_DLL : 0 );
1240 fprintf( outfile, " %s\n", rsrc_name[0] ? rsrc_name : "0" );
1241 fprintf( outfile, "};\n" );
1243 /* Output the DLL constructor */
1245 fprintf( outfile, "static void dll_init(void) __attribute__((constructor));\n" );
1246 fprintf( outfile, "static void dll_init(void) { BUILTIN32_RegisterDLL( &%s_Descriptor ); }\n",
1247 DLLName );
1248 return 0;
1251 /*******************************************************************
1252 * Spec16TypeCompare
1254 static int Spec16TypeCompare( const void *e1, const void *e2 )
1256 const ORDDEF *odp1 = *(const ORDDEF **)e1;
1257 const ORDDEF *odp2 = *(const ORDDEF **)e2;
1259 int type1 = (odp1->type == TYPE_CDECL) ? 0
1260 : (odp1->type == TYPE_REGISTER) ? 3
1261 : (odp1->type == TYPE_INTERRUPT) ? 4
1262 : (odp1->type == TYPE_PASCAL_16) ? 1 : 2;
1264 int type2 = (odp2->type == TYPE_CDECL) ? 0
1265 : (odp2->type == TYPE_REGISTER) ? 3
1266 : (odp2->type == TYPE_INTERRUPT) ? 4
1267 : (odp2->type == TYPE_PASCAL_16) ? 1 : 2;
1269 int retval = type1 - type2;
1270 if ( !retval )
1271 retval = strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
1273 return retval;
1276 /*******************************************************************
1277 * BuildSpec16File
1279 * Build a Win16 assembly file from a spec file.
1281 static int BuildSpec16File( FILE *outfile )
1283 ORDDEF **type, **typelist;
1284 int i, nFuncs, nTypes;
1285 int code_offset, data_offset, module_size;
1286 unsigned char *data;
1288 /* File header */
1290 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
1291 input_file_name );
1292 fprintf( outfile, "#define __FLATCS__ 0x%04x\n", Code_Selector );
1293 fprintf( outfile, "#include \"builtin16.h\"\n\n" );
1295 data = (unsigned char *)xmalloc( 0x10000 );
1296 memset( data, 0, 16 );
1297 data_offset = 16;
1300 /* Build sorted list of all argument types, without duplicates */
1302 typelist = (ORDDEF **)calloc( Limit+1, sizeof(ORDDEF *) );
1304 for (i = nFuncs = 0; i <= Limit; i++)
1306 ORDDEF *odp = Ordinals[i];
1307 if (!odp) continue;
1308 switch (odp->type)
1310 case TYPE_REGISTER:
1311 case TYPE_INTERRUPT:
1312 case TYPE_CDECL:
1313 case TYPE_PASCAL:
1314 case TYPE_PASCAL_16:
1315 case TYPE_STUB:
1316 typelist[nFuncs++] = odp;
1318 default:
1319 break;
1323 qsort( typelist, nFuncs, sizeof(ORDDEF *), Spec16TypeCompare );
1325 i = nTypes = 0;
1326 while ( i < nFuncs )
1328 typelist[nTypes++] = typelist[i++];
1329 while ( i < nFuncs && Spec16TypeCompare( typelist + i, typelist + nTypes-1 ) == 0 )
1330 i++;
1333 /* Output CallFrom16 routines needed by this .spec file */
1335 for ( i = 0; i < nTypes; i++ )
1337 char profile[101];
1339 sprintf( profile, "%s_%s_%s",
1340 (typelist[i]->type == TYPE_CDECL) ? "c" : "p",
1341 (typelist[i]->type == TYPE_REGISTER) ? "regs" :
1342 (typelist[i]->type == TYPE_INTERRUPT) ? "intr" :
1343 (typelist[i]->type == TYPE_PASCAL_16) ? "word" : "long",
1344 typelist[i]->u.func.arg_types );
1346 BuildCallFrom16Func( outfile, profile, DLLName, TRUE );
1349 /* Output the DLL functions prototypes */
1351 for (i = 0; i <= Limit; i++)
1353 ORDDEF *odp = Ordinals[i];
1354 if (!odp) continue;
1355 switch(odp->type)
1357 case TYPE_REGISTER:
1358 case TYPE_INTERRUPT:
1359 case TYPE_CDECL:
1360 case TYPE_PASCAL:
1361 case TYPE_PASCAL_16:
1362 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1363 break;
1364 default:
1365 break;
1369 /* Output code segment */
1371 fprintf( outfile, "\nstatic struct\n{\n CALLFROM16 call[%d];\n"
1372 " ENTRYPOINT16 entry[%d];\n} Code_Segment = \n{\n {\n",
1373 nTypes, nFuncs );
1374 code_offset = 0;
1376 for ( i = 0; i < nTypes; i++ )
1378 char profile[101], *arg;
1379 int argsize = 0;
1381 sprintf( profile, "%s_%s_%s",
1382 (typelist[i]->type == TYPE_CDECL) ? "c" : "p",
1383 (typelist[i]->type == TYPE_REGISTER) ? "regs" :
1384 (typelist[i]->type == TYPE_INTERRUPT) ? "intr" :
1385 (typelist[i]->type == TYPE_PASCAL_16) ? "word" : "long",
1386 typelist[i]->u.func.arg_types );
1388 if ( typelist[i]->type != TYPE_CDECL )
1389 for ( arg = typelist[i]->u.func.arg_types; *arg; arg++ )
1390 switch ( *arg )
1392 case 'w': /* word */
1393 case 's': /* s_word */
1394 argsize += 2;
1395 break;
1397 case 'l': /* long or segmented pointer */
1398 case 'T': /* segmented pointer to null-terminated string */
1399 case 'p': /* linear pointer */
1400 case 't': /* linear pointer to null-terminated string */
1401 argsize += 4;
1402 break;
1405 if ( typelist[i]->type == TYPE_INTERRUPT )
1406 argsize += 2;
1408 fprintf( outfile, " CF16_%s( %s_CallFrom16_%s, %d, \"%s\" ),\n",
1409 ( typelist[i]->type == TYPE_REGISTER
1410 || typelist[i]->type == TYPE_INTERRUPT)? "REGS":
1411 typelist[i]->type == TYPE_PASCAL_16? "WORD" : "LONG",
1412 DLLName, profile, argsize, profile );
1414 code_offset += sizeof(CALLFROM16);
1416 fprintf( outfile, " },\n {\n" );
1418 for (i = 0; i <= Limit; i++)
1420 ORDDEF *odp = Ordinals[i];
1421 if (!odp) continue;
1422 switch (odp->type)
1424 case TYPE_ABS:
1425 odp->offset = LOWORD(odp->u.abs.value);
1426 break;
1428 case TYPE_BYTE:
1429 odp->offset = data_offset;
1430 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1431 break;
1433 case TYPE_WORD:
1434 odp->offset = data_offset;
1435 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1436 break;
1438 case TYPE_LONG:
1439 odp->offset = data_offset;
1440 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1441 break;
1443 case TYPE_REGISTER:
1444 case TYPE_INTERRUPT:
1445 case TYPE_CDECL:
1446 case TYPE_PASCAL:
1447 case TYPE_PASCAL_16:
1448 case TYPE_STUB:
1449 type = bsearch( &odp, typelist, nTypes, sizeof(ORDDEF *), Spec16TypeCompare );
1450 assert( type );
1452 fprintf( outfile, " /* %s.%d */ ", DLLName, i );
1453 fprintf( outfile, "EP( %s, %d /* %s_%s_%s */ ),\n",
1454 odp->u.func.link_name,
1455 (type-typelist)*sizeof(CALLFROM16) -
1456 (code_offset + sizeof(ENTRYPOINT16)),
1457 (odp->type == TYPE_CDECL) ? "c" : "p",
1458 (odp->type == TYPE_REGISTER) ? "regs" :
1459 (odp->type == TYPE_INTERRUPT) ? "intr" :
1460 (odp->type == TYPE_PASCAL_16) ? "word" : "long",
1461 odp->u.func.arg_types );
1463 odp->offset = code_offset;
1464 code_offset += sizeof(ENTRYPOINT16);
1465 break;
1467 default:
1468 fprintf(stderr,"build: function type %d not available for Win16\n",
1469 odp->type);
1470 return -1;
1474 fprintf( outfile, " }\n};\n" );
1476 /* Output data segment */
1478 DumpBytes( outfile, data, data_offset, "Data_Segment" );
1480 /* Build the module */
1482 module_size = BuildModule16( outfile, code_offset, data_offset );
1484 /* Output the DLL descriptor */
1486 if (rsrc_name[0]) fprintf( outfile, "extern const char %s[];\n\n", rsrc_name );
1488 fprintf( outfile, "\nconst BUILTIN16_DESCRIPTOR %s_Descriptor = \n{\n", DLLName );
1489 fprintf( outfile, " \"%s\",\n", DLLName );
1490 fprintf( outfile, " Module,\n" );
1491 fprintf( outfile, " sizeof(Module),\n" );
1492 fprintf( outfile, " (BYTE *)&Code_Segment,\n" );
1493 fprintf( outfile, " (BYTE *)Data_Segment,\n" );
1494 fprintf( outfile, " %s\n", rsrc_name[0] ? rsrc_name : "0" );
1495 fprintf( outfile, "};\n" );
1497 /* Output the DLL constructor */
1499 fprintf( outfile, "static void dll_init(void) __attribute__((constructor));\n" );
1500 fprintf( outfile, "static void dll_init(void) { BUILTIN_RegisterDLL( &%s_Descriptor ); }\n",
1501 DLLName );
1502 return 0;
1506 /*******************************************************************
1507 * BuildSpecFile
1509 * Build an assembly file from a spec file.
1511 static void BuildSpecFile( FILE *outfile, FILE *infile )
1513 SpecFp = infile;
1514 ParseTopLevel();
1516 switch(SpecType)
1518 case SPEC_WIN16:
1519 BuildSpec16File( outfile );
1520 break;
1521 case SPEC_WIN32:
1522 BuildSpec32File( outfile );
1523 break;
1524 default:
1525 fatal_error( "Missing 'type' declaration\n" );
1530 /*******************************************************************
1531 * BuildCallFrom16Func
1533 * Build a 16-bit-to-Wine callback glue function.
1535 * The generated routines are intended to be used as argument conversion
1536 * routines to be called by the CallFrom16... core. Thus, the prototypes of
1537 * the generated routines are (see also CallFrom16):
1539 * extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
1540 * extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
1541 * extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args,
1542 * CONTEXT86 *context );
1543 * extern void WINAPI PREFIX_CallFrom16_C_intr_xxx( FARPROC func, LPBYTE args,
1544 * CONTEXT86 *context );
1546 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
1547 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
1548 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1549 * 'T'=segmented pointer to null-terminated string).
1551 * The generated routines fetch the arguments from the 16-bit stack (pointed
1552 * to by 'args'); the offsets of the single argument values are computed
1553 * according to the calling convention and the argument types. Then, the
1554 * 32-bit entry point is called with these arguments.
1556 * For register functions, the arguments (if present) are converted just
1557 * the same as for normal functions, but in addition the CONTEXT86 pointer
1558 * filled with the current register values is passed to the 32-bit routine.
1559 * (An 'intr' interrupt handler routine is treated exactly like a register
1560 * routine, except that upon return, the flags word pushed onto the stack
1561 * by the interrupt is removed by the 16-bit call stub.)
1564 static void BuildCallFrom16Func( FILE *outfile, char *profile, char *prefix, int local )
1566 int i, pos, argsize = 0;
1567 int short_ret = 0;
1568 int reg_func = 0;
1569 int usecdecl = 0;
1570 char *args = profile + 7;
1571 char *ret_type;
1573 /* Parse function type */
1575 if (!strncmp( "c_", profile, 2 )) usecdecl = 1;
1576 else if (strncmp( "p_", profile, 2 ))
1578 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1579 return;
1582 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
1583 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
1584 else if (!strncmp( "intr_", profile + 2, 5 )) reg_func = 2;
1585 else if (strncmp( "long_", profile + 2, 5 ))
1587 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1588 return;
1591 for ( i = 0; args[i]; i++ )
1592 switch ( args[i] )
1594 case 'w': /* word */
1595 case 's': /* s_word */
1596 argsize += 2;
1597 break;
1599 case 'l': /* long or segmented pointer */
1600 case 'T': /* segmented pointer to null-terminated string */
1601 case 'p': /* linear pointer */
1602 case 't': /* linear pointer to null-terminated string */
1603 argsize += 4;
1604 break;
1607 ret_type = reg_func? "void" : short_ret? "WORD" : "LONG";
1609 fprintf( outfile, "typedef %s WINAPI (*proc_%s_t)( ",
1610 ret_type, profile );
1611 args = profile + 7;
1612 for ( i = 0; args[i]; i++ )
1614 if ( i ) fprintf( outfile, ", " );
1615 switch (args[i])
1617 case 'w': fprintf( outfile, "WORD" ); break;
1618 case 's': fprintf( outfile, "INT16" ); break;
1619 case 'l': case 'T': fprintf( outfile, "LONG" ); break;
1620 case 'p': case 't': fprintf( outfile, "LPVOID" ); break;
1623 if ( reg_func )
1624 fprintf( outfile, "%sstruct _CONTEXT86 *", i? ", " : "" );
1625 else if ( !i )
1626 fprintf( outfile, "void" );
1627 fprintf( outfile, " );\n" );
1629 fprintf( outfile, "%s%s WINAPI %s_CallFrom16_%s( FARPROC proc, LPBYTE args%s )\n{\n",
1630 local? "static " : "", ret_type, prefix, profile,
1631 reg_func? ", struct _CONTEXT86 *context" : "" );
1633 fprintf( outfile, " %s((proc_%s_t) proc) (\n",
1634 reg_func? "" : "return ", profile );
1635 args = profile + 7;
1636 pos = !usecdecl? argsize : 0;
1637 for ( i = 0; args[i]; i++ )
1639 if ( i ) fprintf( outfile, ",\n" );
1640 fprintf( outfile, " " );
1641 switch (args[i])
1643 case 'w': /* word */
1644 if ( !usecdecl ) pos -= 2;
1645 fprintf( outfile, "*(WORD *)(args+%d)", pos );
1646 if ( usecdecl ) pos += 2;
1647 break;
1649 case 's': /* s_word */
1650 if ( !usecdecl ) pos -= 2;
1651 fprintf( outfile, "*(INT16 *)(args+%d)", pos );
1652 if ( usecdecl ) pos += 2;
1653 break;
1655 case 'l': /* long or segmented pointer */
1656 case 'T': /* segmented pointer to null-terminated string */
1657 if ( !usecdecl ) pos -= 4;
1658 fprintf( outfile, "*(LONG *)(args+%d)", pos );
1659 if ( usecdecl ) pos += 4;
1660 break;
1662 case 'p': /* linear pointer */
1663 case 't': /* linear pointer to null-terminated string */
1664 if ( !usecdecl ) pos -= 4;
1665 fprintf( outfile, "PTR_SEG_TO_LIN( *(SEGPTR *)(args+%d) )", pos );
1666 if ( usecdecl ) pos += 4;
1667 break;
1669 default:
1670 fprintf( stderr, "Unknown arg type '%c'\n", args[i] );
1673 if ( reg_func )
1674 fprintf( outfile, "%s context", i? ",\n" : "" );
1675 fprintf( outfile, " );\n}\n\n" );
1678 /*******************************************************************
1679 * BuildCallTo16Func
1681 * Build a Wine-to-16-bit callback glue function.
1683 * Prototypes for the CallTo16 functions:
1684 * extern WORD CALLBACK PREFIX_CallTo16_word_xxx( FARPROC16 func, args... );
1685 * extern LONG CALLBACK PREFIX_CallTo16_long_xxx( FARPROC16 func, args... );
1687 * These routines are provided solely for convenience; they simply
1688 * write the arguments onto the 16-bit stack, and call the appropriate
1689 * CallTo16... core routine.
1691 * If you have more sophisticated argument conversion requirements than
1692 * are provided by these routines, you might as well call the core
1693 * routines by yourself.
1696 static void BuildCallTo16Func( FILE *outfile, char *profile, char *prefix )
1698 char *args = profile + 5;
1699 int i, argsize = 0, short_ret = 0;
1701 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1702 else if (strncmp( "long_", profile, 5 ))
1704 fprintf( stderr, "Invalid function name '%s'.\n", profile );
1705 exit(1);
1708 fprintf( outfile, "%s %s_CallTo16_%s( FARPROC16 proc",
1709 short_ret? "WORD" : "LONG", prefix, profile );
1710 args = profile + 5;
1711 for ( i = 0; args[i]; i++ )
1713 fprintf( outfile, ", " );
1714 switch (args[i])
1716 case 'w': fprintf( outfile, "WORD" ); argsize += 2; break;
1717 case 'l': fprintf( outfile, "LONG" ); argsize += 4; break;
1719 fprintf( outfile, " arg%d", i+1 );
1721 fprintf( outfile, " )\n{\n" );
1723 if ( argsize > 0 )
1724 fprintf( outfile, " LPBYTE args = (LPBYTE)CURRENT_STACK16;\n" );
1726 args = profile + 5;
1727 for ( i = 0; args[i]; i++ )
1729 switch (args[i])
1731 case 'w': fprintf( outfile, " args -= sizeof(WORD); *(WORD" ); break;
1732 case 'l': fprintf( outfile, " args -= sizeof(LONG); *(LONG" ); break;
1733 default: fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
1734 args[i] );
1736 fprintf( outfile, " *)args = arg%d;\n", i+1 );
1739 fprintf( outfile, " return CallTo16%s( proc, %d );\n}\n\n",
1740 short_ret? "Word" : "Long", argsize );
1745 /*******************************************************************
1746 * BuildCallFrom16Core
1748 * This routine builds the core routines used in 16->32 thunks:
1749 * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
1751 * These routines are intended to be called via a far call (with 32-bit
1752 * operand size) from 16-bit code. The 16-bit code stub must push %bp,
1753 * the 32-bit entry point to be called, and the argument conversion
1754 * routine to be used (see stack layout below).
1756 * The core routine completes the STACK16FRAME on the 16-bit stack and
1757 * switches to the 32-bit stack. Then, the argument conversion routine
1758 * is called; it gets passed the 32-bit entry point and a pointer to the
1759 * 16-bit arguments (on the 16-bit stack) as parameters. (You can either
1760 * use conversion routines automatically generated by BuildCallFrom16,
1761 * or write your own for special purposes.)
1763 * The conversion routine must call the 32-bit entry point, passing it
1764 * the converted arguments, and return its return value to the core.
1765 * After the conversion routine has returned, the core switches back
1766 * to the 16-bit stack, converts the return value to the DX:AX format
1767 * (CallFrom16Long), and returns to the 16-bit call stub. All parameters,
1768 * including %bp, are popped off the stack.
1770 * The 16-bit call stub now returns to the caller, popping the 16-bit
1771 * arguments if necessary (pascal calling convention).
1773 * In the case of a 'register' function, CallFrom16Register fills a
1774 * CONTEXT86 structure with the values all registers had at the point
1775 * the first instruction of the 16-bit call stub was about to be
1776 * executed. A pointer to this CONTEXT86 is passed as third parameter
1777 * to the argument conversion routine, which typically passes it on
1778 * to the called 32-bit entry point.
1780 * CallFrom16Thunk is a special variant used by the implementation of
1781 * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
1782 * implemented as follows:
1783 * On entry, the EBX register is set up to contain a flat pointer to the
1784 * 16-bit stack such that EBX+22 points to the first argument.
1785 * Then, the entry point is called, while EBP is set up to point
1786 * to the return address (on the 32-bit stack).
1787 * The called function returns with CX set to the number of bytes
1788 * to be popped of the caller's stack.
1790 * Stack layout upon entry to the core routine (STACK16FRAME):
1791 * ... ...
1792 * (sp+24) word first 16-bit arg
1793 * (sp+22) word cs
1794 * (sp+20) word ip
1795 * (sp+18) word bp
1796 * (sp+14) long 32-bit entry point (reused for Win16 mutex recursion count)
1797 * (sp+12) word ip of actual entry point (necessary for relay debugging)
1798 * (sp+8) long relay (argument conversion) function entry point
1799 * (sp+4) long cs of 16-bit entry point
1800 * (sp) long ip of 16-bit entry point
1802 * Added on the stack:
1803 * (sp-2) word saved gs
1804 * (sp-4) word saved fs
1805 * (sp-6) word saved es
1806 * (sp-8) word saved ds
1807 * (sp-12) long saved ebp
1808 * (sp-16) long saved ecx
1809 * (sp-20) long saved edx
1810 * (sp-24) long saved previous stack
1812 static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk, int short_ret )
1814 char *name = thunk? "Thunk" : reg_func? "Register" : short_ret? "Word" : "Long";
1816 /* Function header */
1817 fprintf( outfile, "\n\t.align 4\n" );
1818 #ifdef USE_STABS
1819 fprintf( outfile, ".stabs \"CallFrom16%s:F1\",36,0,0," PREFIX "CallFrom16%s\n",
1820 name, name);
1821 #endif
1822 fprintf( outfile, "\t.type " PREFIX "CallFrom16%s,@function\n", name );
1823 fprintf( outfile, "\t.globl " PREFIX "CallFrom16%s\n", name );
1824 fprintf( outfile, PREFIX "CallFrom16%s:\n", name );
1826 /* Create STACK16FRAME (except STACK32FRAME link) */
1827 fprintf( outfile, "\tpushw %%gs\n" );
1828 fprintf( outfile, "\tpushw %%fs\n" );
1829 fprintf( outfile, "\tpushw %%es\n" );
1830 fprintf( outfile, "\tpushw %%ds\n" );
1831 fprintf( outfile, "\tpushl %%ebp\n" );
1832 fprintf( outfile, "\tpushl %%ecx\n" );
1833 fprintf( outfile, "\tpushl %%edx\n" );
1835 /* Save original EFlags register */
1836 fprintf( outfile, "\tpushfl\n" );
1838 if ( UsePIC )
1840 /* Get Global Offset Table into %ecx */
1841 fprintf( outfile, "\tcall .LCallFrom16%s.getgot1\n", name );
1842 fprintf( outfile, ".LCallFrom16%s.getgot1:\n", name );
1843 fprintf( outfile, "\tpopl %%ecx\n" );
1844 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot1], %%ecx\n", name );
1847 /* Load 32-bit segment registers */
1848 fprintf( outfile, "\tmovw $0x%04x, %%dx\n", Data_Selector );
1849 #ifdef __svr4__
1850 fprintf( outfile, "\tdata16\n");
1851 #endif
1852 fprintf( outfile, "\tmovw %%dx, %%ds\n" );
1853 #ifdef __svr4__
1854 fprintf( outfile, "\tdata16\n");
1855 #endif
1856 fprintf( outfile, "\tmovw %%dx, %%es\n" );
1858 if ( UsePIC )
1860 fprintf( outfile, "\tmovl " PREFIX "SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
1861 fprintf( outfile, "\tmovw (%%edx), %%fs\n" );
1863 else
1864 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb, %%fs\n" );
1866 /* Get address of ldt_copy array into %ecx */
1867 if ( UsePIC )
1868 fprintf( outfile, "\tmovl " PREFIX "ldt_copy@GOT(%%ecx), %%ecx\n" );
1869 else
1870 fprintf( outfile, "\tmovl $" PREFIX "ldt_copy, %%ecx\n" );
1872 /* Translate STACK16FRAME base to flat offset in %edx */
1873 fprintf( outfile, "\tmovw %%ss, %%dx\n" );
1874 fprintf( outfile, "\tandl $0xfff8, %%edx\n" );
1875 fprintf( outfile, "\tmovl (%%ecx,%%edx), %%edx\n" );
1876 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
1877 fprintf( outfile, "\tleal (%%ebp,%%edx), %%edx\n" );
1879 /* Get saved flags into %ecx */
1880 fprintf( outfile, "\tpopl %%ecx\n" );
1882 /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
1883 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET );
1884 fprintf( outfile, "\tpushl %%ebp\n" );
1886 /* Switch stacks */
1887 #ifdef __svr4__
1888 fprintf( outfile,"\tdata16\n");
1889 #endif
1890 fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET + 2 );
1891 fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET );
1892 fprintf( outfile, "\tpushl %%ds\n" );
1893 fprintf( outfile, "\tpopl %%ss\n" );
1894 fprintf( outfile, "\tmovl %%ebp, %%esp\n" );
1895 fprintf( outfile, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME, ebp) );
1898 /* At this point:
1899 STACK16FRAME is completely set up
1900 DS, ES, SS: flat data segment
1901 FS: current TEB
1902 ESP: points to last STACK32FRAME
1903 EBP: points to ebp member of last STACK32FRAME
1904 EDX: points to current STACK16FRAME
1905 ECX: contains saved flags
1906 all other registers: unchanged */
1908 /* Special case: C16ThkSL stub */
1909 if ( thunk )
1911 /* Set up registers as expected and call thunk */
1912 fprintf( outfile, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME)-22 );
1913 fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
1915 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point) );
1917 /* Switch stack back */
1918 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
1919 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
1920 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
1922 /* Restore registers and return directly to caller */
1923 fprintf( outfile, "\taddl $8, %%esp\n" );
1924 fprintf( outfile, "\tpopl %%ebp\n" );
1925 fprintf( outfile, "\tpopw %%ds\n" );
1926 fprintf( outfile, "\tpopw %%es\n" );
1927 fprintf( outfile, "\tpopw %%fs\n" );
1928 fprintf( outfile, "\tpopw %%gs\n" );
1929 fprintf( outfile, "\taddl $20, %%esp\n" );
1931 fprintf( outfile, "\txorb %%ch, %%ch\n" );
1932 fprintf( outfile, "\tpopl %%ebx\n" );
1933 fprintf( outfile, "\taddw %%cx, %%sp\n" );
1934 fprintf( outfile, "\tpush %%ebx\n" );
1936 fprintf( outfile, "\t.byte 0x66\n" );
1937 fprintf( outfile, "\tlret\n" );
1939 return;
1943 /* Build register CONTEXT */
1944 if ( reg_func )
1946 fprintf( outfile, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86) );
1948 fprintf( outfile, "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags) );
1950 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax) );
1951 fprintf( outfile, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx) );
1952 fprintf( outfile, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi) );
1953 fprintf( outfile, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi) );
1955 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp) );
1956 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp) );
1957 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx) );
1958 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx) );
1959 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx) );
1960 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx) );
1962 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds) );
1963 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs) );
1964 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es) );
1965 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs) );
1966 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs) );
1967 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs) );
1968 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs) );
1969 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs) );
1971 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs) );
1972 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs) );
1973 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip) );
1974 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip) );
1976 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET+2 );
1977 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs) );
1978 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET );
1979 fprintf( outfile, "\taddl $%d, %%eax\n", STACK16OFFSET(ip) );
1980 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp) );
1981 #if 0
1982 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
1983 #endif
1985 /* Push address of CONTEXT86 structure -- popped by the relay routine */
1986 fprintf( outfile, "\tpushl %%esp\n" );
1990 /* Print debug info before call */
1991 if ( debugging )
1993 if ( UsePIC )
1995 fprintf( outfile, "\tpushl %%ebx\n" );
1997 /* Get Global Offset Table into %ebx (for PLT call) */
1998 fprintf( outfile, "\tcall .LCallFrom16%s.getgot2\n", name );
1999 fprintf( outfile, ".LCallFrom16%s.getgot2:\n", name );
2000 fprintf( outfile, "\tpopl %%ebx\n" );
2001 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot2], %%ebx\n", name );
2004 fprintf( outfile, "\tpushl %%edx\n" );
2005 if ( reg_func )
2006 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2007 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
2008 else
2009 fprintf( outfile, "\tpushl $0\n" );
2011 if ( UsePIC )
2012 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16@PLT\n ");
2013 else
2014 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n ");
2016 fprintf( outfile, "\tpopl %%edx\n" );
2017 fprintf( outfile, "\tpopl %%edx\n" );
2019 if ( UsePIC )
2020 fprintf( outfile, "\tpopl %%ebx\n" );
2023 /* Call relay routine (which will call the API entry point) */
2024 fprintf( outfile, "\tleal %d(%%edx), %%eax\n", sizeof(STACK16FRAME) );
2025 fprintf( outfile, "\tpushl %%eax\n" );
2026 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point) );
2027 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay) );
2029 /* Print debug info after call */
2030 if ( debugging )
2032 if ( UsePIC )
2034 fprintf( outfile, "\tpushl %%ebx\n" );
2036 /* Get Global Offset Table into %ebx (for PLT call) */
2037 fprintf( outfile, "\tcall .LCallFrom16%s.getgot3\n", name );
2038 fprintf( outfile, ".LCallFrom16%s.getgot3:\n", name );
2039 fprintf( outfile, "\tpopl %%ebx\n" );
2040 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot3], %%ebx\n", name );
2043 fprintf( outfile, "\tpushl %%eax\n" );
2044 if ( reg_func )
2045 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2046 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
2047 else
2048 fprintf( outfile, "\tpushl $0\n" );
2050 if ( UsePIC )
2051 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret@PLT\n ");
2052 else
2053 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n ");
2055 fprintf( outfile, "\tpopl %%eax\n" );
2056 fprintf( outfile, "\tpopl %%eax\n" );
2058 if ( UsePIC )
2059 fprintf( outfile, "\tpopl %%ebx\n" );
2063 if ( reg_func )
2065 fprintf( outfile, "\tmovl %%esp, %%ebx\n" );
2067 /* Switch stack back */
2068 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2069 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
2070 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2072 /* Get return address to CallFrom16 stub */
2073 fprintf( outfile, "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip)-4 );
2074 fprintf( outfile, "\tpopl %%eax\n" );
2075 fprintf( outfile, "\tpopl %%edx\n" );
2077 /* Restore all registers from CONTEXT */
2078 fprintf( outfile, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs) );
2079 fprintf( outfile, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp) );
2080 fprintf( outfile, "\taddl $4, %%esp\n" ); /* room for final return address */
2082 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
2083 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
2084 fprintf( outfile, "\tpushl %%edx\n" );
2085 fprintf( outfile, "\tpushl %%eax\n" );
2086 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags) );
2087 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
2089 fprintf( outfile, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs) );
2090 fprintf( outfile, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs) );
2091 fprintf( outfile, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs) );
2093 fprintf( outfile, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp) );
2094 fprintf( outfile, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi) );
2095 fprintf( outfile, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi) );
2096 fprintf( outfile, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax) );
2097 fprintf( outfile, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx) );
2098 fprintf( outfile, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx) );
2099 fprintf( outfile, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx) );
2101 fprintf( outfile, "\tpopl %%ds\n" );
2102 fprintf( outfile, "\tpopfl\n" );
2103 fprintf( outfile, "\tlret\n" );
2105 else
2107 /* Switch stack back */
2108 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2109 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
2110 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2112 /* Restore registers */
2113 fprintf( outfile, "\tpopl %%edx\n" );
2114 fprintf( outfile, "\tpopl %%ecx\n" );
2115 fprintf( outfile, "\tpopl %%ebp\n" );
2116 fprintf( outfile, "\tpopw %%ds\n" );
2117 fprintf( outfile, "\tpopw %%es\n" );
2118 fprintf( outfile, "\tpopw %%fs\n" );
2119 fprintf( outfile, "\tpopw %%gs\n" );
2121 /* Prepare return value and set flags accordingly */
2122 if ( !short_ret )
2123 fprintf( outfile, "\tshldl $16, %%eax, %%edx\n" );
2124 fprintf( outfile, "\torl %%eax, %%eax\n" );
2126 /* Return to return stub which will return to caller */
2127 fprintf( outfile, "\tlret $12\n" );
2132 /*******************************************************************
2133 * BuildCallTo16Core
2135 * This routine builds the core routines used in 32->16 thunks:
2137 * extern void CALLBACK CallTo16Word( SEGPTR target, int nb_args );
2138 * extern void CALLBACK CallTo16Long( SEGPTR target, int nb_args );
2139 * extern void CALLBACK CallTo16RegisterShort( const CONTEXT86 *context, int nb_args );
2140 * extern void CALLBACK CallTo16RegisterLong ( const CONTEXT86 *context, int nb_args );
2142 * These routines can be called directly from 32-bit code.
2144 * All routines expect that the 16-bit stack contents (arguments) were
2145 * already set up by the caller; nb_args must contain the number of bytes
2146 * to be conserved. The 16-bit SS:SP will be set accordinly.
2148 * All other registers are either taken from the CONTEXT86 structure
2149 * or else set to default values. The target routine address is either
2150 * given directly or taken from the CONTEXT86.
2152 * If you want to call a 16-bit routine taking only standard argument types
2153 * (WORD and LONG), you can also have an appropriate argument conversion
2154 * stub automatically generated (see BuildCallTo16); you'd then call this
2155 * stub, which in turn would prepare the 16-bit stack and call the appropiate
2156 * core routine.
2160 static void BuildCallTo16Core( FILE *outfile, int short_ret, int reg_func )
2162 char *name = reg_func == 2 ? "RegisterLong" :
2163 reg_func == 1 ? "RegisterShort" :
2164 short_ret? "Word" : "Long";
2166 /* Function header */
2167 fprintf( outfile, "\n\t.align 4\n" );
2168 #ifdef USE_STABS
2169 fprintf( outfile, ".stabs \"CallTo16%s:F1\",36,0,0," PREFIX "CallTo16%s\n",
2170 name, name);
2171 #endif
2172 fprintf( outfile, "\t.globl " PREFIX "CallTo16%s\n", name );
2173 fprintf( outfile, PREFIX "CallTo16%s:\n", name );
2175 /* Function entry sequence */
2176 fprintf( outfile, "\tpushl %%ebp\n" );
2177 fprintf( outfile, "\tmovl %%esp, %%ebp\n" );
2179 /* Save the 32-bit registers */
2180 fprintf( outfile, "\tpushl %%ebx\n" );
2181 fprintf( outfile, "\tpushl %%ecx\n" );
2182 fprintf( outfile, "\tpushl %%edx\n" );
2183 fprintf( outfile, "\tpushl %%esi\n" );
2184 fprintf( outfile, "\tpushl %%edi\n" );
2186 if ( UsePIC )
2188 /* Get Global Offset Table into %ebx */
2189 fprintf( outfile, "\tcall .LCallTo16%s.getgot1\n", name );
2190 fprintf( outfile, ".LCallTo16%s.getgot1:\n", name );
2191 fprintf( outfile, "\tpopl %%ebx\n" );
2192 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot1], %%ebx\n", name );
2195 /* Enter Win16 Mutex */
2196 if ( UsePIC )
2197 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock@PLT\n" );
2198 else
2199 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock\n" );
2201 /* Print debugging info */
2202 if (debugging)
2204 /* Push flags, number of arguments, and target */
2205 fprintf( outfile, "\tpushl $%d\n", reg_func );
2206 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2207 fprintf( outfile, "\tpushl 8(%%ebp)\n" );
2209 if ( UsePIC )
2210 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16@PLT\n" );
2211 else
2212 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
2214 fprintf( outfile, "\taddl $12, %%esp\n" );
2217 /* Get return address */
2218 if ( UsePIC )
2219 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr@GOTOFF(%%ebx), %%ecx\n" );
2220 else
2221 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr, %%ecx\n" );
2223 /* Call the actual CallTo16 routine (simulate a lcall) */
2224 fprintf( outfile, "\tpushl %%cs\n" );
2225 fprintf( outfile, "\tcall .LCallTo16%s\n", name );
2227 /* Convert and push return value */
2228 if ( short_ret )
2230 fprintf( outfile, "\tmovzwl %%ax, %%eax\n" );
2231 fprintf( outfile, "\tpushl %%eax\n" );
2233 else if ( reg_func != 2 )
2235 fprintf( outfile, "\tshll $16,%%edx\n" );
2236 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
2237 fprintf( outfile, "\tpushl %%edx\n" );
2239 else
2240 fprintf( outfile, "\tpushl %%eax\n" );
2242 if ( UsePIC )
2244 /* Get Global Offset Table into %ebx (might have been overwritten) */
2245 fprintf( outfile, "\tcall .LCallTo16%s.getgot2\n", name );
2246 fprintf( outfile, ".LCallTo16%s.getgot2:\n", name );
2247 fprintf( outfile, "\tpopl %%ebx\n" );
2248 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot2], %%ebx\n", name );
2251 /* Print debugging info */
2252 if (debugging)
2254 if ( UsePIC )
2255 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret@PLT\n" );
2256 else
2257 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret\n" );
2260 /* Leave Win16 Mutex */
2261 if ( UsePIC )
2262 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock@PLT\n" );
2263 else
2264 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock\n" );
2266 /* Get return value */
2267 fprintf( outfile, "\tpopl %%eax\n" );
2269 /* Restore the 32-bit registers */
2270 fprintf( outfile, "\tpopl %%edi\n" );
2271 fprintf( outfile, "\tpopl %%esi\n" );
2272 fprintf( outfile, "\tpopl %%edx\n" );
2273 fprintf( outfile, "\tpopl %%ecx\n" );
2274 fprintf( outfile, "\tpopl %%ebx\n" );
2276 /* Function exit sequence */
2277 fprintf( outfile, "\tpopl %%ebp\n" );
2278 fprintf( outfile, "\tret $8\n" );
2281 /* Start of the actual CallTo16 routine */
2283 fprintf( outfile, ".LCallTo16%s:\n", name );
2285 /* Complete STACK32FRAME */
2286 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
2287 fprintf( outfile, "\tmovl %%esp,%%edx\n" );
2289 /* Switch to the 16-bit stack */
2290 #ifdef __svr4__
2291 fprintf( outfile,"\tdata16\n");
2292 #endif
2293 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
2294 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
2295 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
2297 /* Make %bp point to the previous stackframe (built by CallFrom16) */
2298 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
2299 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp) );
2301 /* Add the specified offset to the new sp */
2302 fprintf( outfile, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args) );
2304 /* Push the return address
2305 * With sreg suffix, we push 16:16 address (normal lret)
2306 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
2308 if (reg_func != 2)
2309 fprintf( outfile, "\tpushl %%ecx\n" );
2310 else
2312 fprintf( outfile, "\tshldl $16, %%ecx, %%eax\n" );
2313 fprintf( outfile, "\tpushw $0\n" );
2314 fprintf( outfile, "\tpushw %%ax\n" );
2315 fprintf( outfile, "\tpushw $0\n" );
2316 fprintf( outfile, "\tpushw %%cx\n" );
2319 if (reg_func)
2321 /* Push the called routine address */
2322 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target) );
2323 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs) );
2324 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip) );
2326 /* Get the registers */
2327 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs) );
2328 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs) );
2329 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2330 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs) );
2331 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2332 fprintf( outfile, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp) );
2333 fprintf( outfile, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi) );
2334 fprintf( outfile, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi) );
2335 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax) );
2336 fprintf( outfile, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx) );
2337 fprintf( outfile, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx) );
2338 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx) );
2340 /* Get the 16-bit ds */
2341 fprintf( outfile, "\tpopw %%ds\n" );
2343 else /* not a register function */
2345 /* Push the called routine address */
2346 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK32OFFSET(target) );
2348 /* Set %fs to the value saved by the last CallFrom16 */
2349 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs)-STACK16OFFSET(bp) );
2350 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2352 /* Set %ds and %es (and %ax just in case) equal to %ss */
2353 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
2354 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2355 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2358 /* Jump to the called routine */
2359 fprintf( outfile, "\t.byte 0x66\n" );
2360 fprintf( outfile, "\tlret\n" );
2363 /*******************************************************************
2364 * BuildRet16Func
2366 * Build the return code for 16-bit callbacks
2368 static void BuildRet16Func( FILE *outfile )
2371 * Note: This must reside in the .data section to allow
2372 * run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
2375 fprintf( outfile, "\n\t.globl " PREFIX "CallTo16_Ret\n" );
2376 fprintf( outfile, PREFIX "CallTo16_Ret:\n" );
2378 /* Restore 32-bit segment registers */
2380 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
2381 #ifdef __svr4__
2382 fprintf( outfile, "\tdata16\n");
2383 #endif
2384 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
2385 #ifdef __svr4__
2386 fprintf( outfile, "\tdata16\n");
2387 #endif
2388 fprintf( outfile, "\tmovw %%bx,%%es\n" );
2390 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
2392 /* Restore the 32-bit stack */
2394 #ifdef __svr4__
2395 fprintf( outfile, "\tdata16\n");
2396 #endif
2397 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
2398 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
2399 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2401 /* Return to caller */
2403 fprintf( outfile, "\tlret\n" );
2405 /* Declare the return address variable */
2407 fprintf( outfile, "\n\t.globl " PREFIX "CallTo16_RetAddr\n" );
2408 fprintf( outfile, PREFIX "CallTo16_RetAddr:\t.long 0\n" );
2411 /*******************************************************************
2412 * BuildCallTo32CBClient
2414 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
2416 * Since the relay stub is itself 32-bit, this should not be a problem;
2417 * unfortunately, the relay stubs are expected to switch back to a
2418 * 16-bit stack (and 16-bit code) after completion :-(
2420 * This would conflict with our 16- vs. 32-bit stack handling, so
2421 * we simply switch *back* to our 32-bit stack before returning to
2422 * the caller ...
2424 * The CBClient relay stub expects to be called with the following
2425 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
2426 * stack at the designated places:
2428 * ...
2429 * (ebp+14) original arguments to the callback routine
2430 * (ebp+10) far return address to original caller
2431 * (ebp+6) Thunklet target address
2432 * (ebp+2) Thunklet relay ID code
2433 * (ebp) BP (saved by CBClientGlueSL)
2434 * (ebp-2) SI (saved by CBClientGlueSL)
2435 * (ebp-4) DI (saved by CBClientGlueSL)
2436 * (ebp-6) DS (saved by CBClientGlueSL)
2438 * ... buffer space used by the 16-bit side glue for temp copies
2440 * (ebx+4) far return address to 16-bit side glue code
2441 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
2443 * The 32-bit side glue code accesses both the original arguments (via ebp)
2444 * and the temporary copies prepared by the 16-bit side glue (via ebx).
2445 * After completion, the stub will load ss:sp from the buffer at ebx
2446 * and perform a far return to 16-bit code.
2448 * To trick the relay stub into returning to us, we replace the 16-bit
2449 * return address to the glue code by a cs:ip pair pointing to our
2450 * return entry point (the original return address is saved first).
2451 * Our return stub thus called will then reload the 32-bit ss:esp and
2452 * return to 32-bit code (by using and ss:esp value that we have also
2453 * pushed onto the 16-bit stack before and a cs:eip values found at
2454 * that position on the 32-bit stack). The ss:esp to be restored is
2455 * found relative to the 16-bit stack pointer at:
2457 * (ebx-4) ss (flat)
2458 * (ebx-8) sp (32-bit stack pointer)
2460 * The second variant of this routine, CALL32_CBClientEx, which is used
2461 * to implement KERNEL.621, has to cope with yet another problem: Here,
2462 * the 32-bit side directly returns to the caller of the CBClient thunklet,
2463 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
2464 * As we have to return to our 32-bit code first, we have to adapt the
2465 * layout of our temporary area so as to include values for the registers
2466 * that are to be restored, and later (in the implementation of KERNEL.621)
2467 * we *really* restore them. The return stub restores DS, DI, SI, and BP
2468 * from the stack, skips the next 8 bytes (CBClient relay code / target),
2469 * and then performs a lret NN, where NN is the number of arguments to be
2470 * removed. Thus, we prepare our temporary area as follows:
2472 * (ebx+22) 16-bit cs (this segment)
2473 * (ebx+20) 16-bit ip ('16-bit' return entry point)
2474 * (ebx+16) 32-bit ss (flat)
2475 * (ebx+12) 32-bit sp (32-bit stack pointer)
2476 * (ebx+10) 16-bit bp (points to ebx+24)
2477 * (ebx+8) 16-bit si (ignored)
2478 * (ebx+6) 16-bit di (ignored)
2479 * (ebx+4) 16-bit ds (we actually use the flat DS here)
2480 * (ebx+2) 16-bit ss (16-bit stack segment)
2481 * (ebx+0) 16-bit sp (points to ebx+4)
2483 * Note that we ensure that DS is not changed and remains the flat segment,
2484 * and the 32-bit stack pointer our own return stub needs fits just
2485 * perfectly into the 8 bytes that are skipped by the Windows stub.
2486 * One problem is that we have to determine the number of removed arguments,
2487 * as these have to be really removed in KERNEL.621. Thus, the BP value
2488 * that we place in the temporary area to be restored, contains the value
2489 * that SP would have if no arguments were removed. By comparing the actual
2490 * value of SP with this value in our return stub we can compute the number
2491 * of removed arguments. This is then returned to KERNEL.621.
2493 * The stack layout of this function:
2494 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
2495 * (ebp+16) esi pointer to caller's esi value
2496 * (ebp+12) arg ebp value to be set for relay stub
2497 * (ebp+8) func CBClient relay stub address
2498 * (ebp+4) ret addr
2499 * (ebp) ebp
2501 static void BuildCallTo32CBClient( FILE *outfile, BOOL isEx )
2503 char *name = isEx? "CBClientEx" : "CBClient";
2504 int size = isEx? 24 : 12;
2506 /* Function header */
2508 fprintf( outfile, "\n\t.align 4\n" );
2509 #ifdef USE_STABS
2510 fprintf( outfile, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX "CALL32_%s\n",
2511 name, name );
2512 #endif
2513 fprintf( outfile, "\t.globl " PREFIX "CALL32_%s\n", name );
2514 fprintf( outfile, PREFIX "CALL32_%s:\n", name );
2516 /* Entry code */
2518 fprintf( outfile, "\tpushl %%ebp\n" );
2519 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2520 fprintf( outfile, "\tpushl %%edi\n" );
2521 fprintf( outfile, "\tpushl %%esi\n" );
2522 fprintf( outfile, "\tpushl %%ebx\n" );
2524 /* Get the 16-bit stack */
2526 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET);
2528 /* Convert it to a flat address */
2530 fprintf( outfile, "\tshldl $16,%%ebx,%%eax\n" );
2531 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
2532 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%esi\n" );
2533 fprintf( outfile, "\tmovw %%bx,%%ax\n" );
2534 fprintf( outfile, "\taddl %%eax,%%esi\n" );
2536 /* Allocate temporary area (simulate STACK16_PUSH) */
2538 fprintf( outfile, "\tpushf\n" );
2539 fprintf( outfile, "\tcld\n" );
2540 fprintf( outfile, "\tleal -%d(%%esi), %%edi\n", size );
2541 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2542 fprintf( outfile, "\trep\n\tmovsb\n" );
2543 fprintf( outfile, "\tpopf\n" );
2545 fprintf( outfile, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size, STACKOFFSET );
2547 fprintf( outfile, "\tpushl %%edi\n" ); /* remember address */
2549 /* Set up temporary area */
2551 if ( !isEx )
2553 fprintf( outfile, "\tleal 4(%%edi), %%edi\n" );
2555 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2556 fprintf( outfile, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */
2558 fprintf( outfile, "\tmovw %%ss, %%ax\n" );
2559 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2560 fprintf( outfile, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */
2562 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 + 4 );
2563 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */
2565 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
2566 fprintf( outfile, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */
2568 else
2570 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 );
2571 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" );
2573 fprintf( outfile, "\tmovw %%ds, %%ax\n" );
2574 fprintf( outfile, "\tmovw %%ax, 4(%%edi)\n" );
2576 fprintf( outfile, "\taddl $20, %%ebx\n" );
2577 fprintf( outfile, "\tmovw %%bx, 10(%%edi)\n" );
2579 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2580 fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
2582 fprintf( outfile, "\tmovw %%ss, %%ax\n" );
2583 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2584 fprintf( outfile, "\tmovl %%eax, 16(%%edi)\n" );
2586 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
2587 fprintf( outfile, "\tmovl %%eax, 20(%%edi)\n" );
2590 /* Set up registers and call CBClient relay stub (simulating a far call) */
2592 fprintf( outfile, "\tmovl 16(%%ebp), %%esi\n" );
2593 fprintf( outfile, "\tmovl (%%esi), %%esi\n" );
2595 fprintf( outfile, "\tmovl %%edi, %%ebx\n" );
2596 fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
2597 fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
2599 fprintf( outfile, "\tpushl %%cs\n" );
2600 fprintf( outfile, "\tcall *%%eax\n" );
2602 /* Return new esi value to caller */
2604 fprintf( outfile, "\tmovl 32(%%esp), %%edi\n" );
2605 fprintf( outfile, "\tmovl %%esi, (%%edi)\n" );
2607 /* Cleanup temporary area (simulate STACK16_POP) */
2609 fprintf( outfile, "\tpop %%esi\n" );
2611 fprintf( outfile, "\tpushf\n" );
2612 fprintf( outfile, "\tstd\n" );
2613 fprintf( outfile, "\tdec %%esi\n" );
2614 fprintf( outfile, "\tleal %d(%%esi), %%edi\n", size );
2615 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2616 fprintf( outfile, "\trep\n\tmovsb\n" );
2617 fprintf( outfile, "\tpopf\n" );
2619 fprintf( outfile, "\t.byte 0x64\n\taddw $%d,(%d)\n", size, STACKOFFSET );
2621 /* Return argument size to caller */
2622 if ( isEx )
2624 fprintf( outfile, "\tmovl 32(%%esp), %%ebx\n" );
2625 fprintf( outfile, "\tmovl %%ebp, (%%ebx)\n" );
2628 /* Restore registers and return */
2630 fprintf( outfile, "\tpopl %%ebx\n" );
2631 fprintf( outfile, "\tpopl %%esi\n" );
2632 fprintf( outfile, "\tpopl %%edi\n" );
2633 fprintf( outfile, "\tpopl %%ebp\n" );
2634 fprintf( outfile, "\tret\n" );
2637 static void BuildCallTo32CBClientRet( FILE *outfile, BOOL isEx )
2639 char *name = isEx? "CBClientEx" : "CBClient";
2641 /* '16-bit' return stub */
2643 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_Ret\n", name );
2644 fprintf( outfile, PREFIX "CALL32_%s_Ret:\n", name );
2646 if ( !isEx )
2648 fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
2649 fprintf( outfile, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
2651 else
2653 fprintf( outfile, "\tmovzwl %%bp, %%ebx\n" );
2654 fprintf( outfile, "\tsubw %%bp, %%sp\n" );
2655 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
2656 fprintf( outfile, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
2658 fprintf( outfile, "\tlret\n" );
2660 /* Declare the return address variable */
2662 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_RetAddr\n", name );
2663 fprintf( outfile, PREFIX "CALL32_%s_RetAddr:\t.long 0\n", name );
2667 /*******************************************************************
2668 * BuildCallTo32LargeStack
2670 * Build the function used to switch to the original 32-bit stack
2671 * before calling a 32-bit function from 32-bit code. This is used for
2672 * functions that need a large stack, like X bitmaps functions.
2674 * The generated function has the following prototype:
2675 * int xxx( int (*func)(), void *arg );
2677 * The pointer to the function can be retrieved by calling CALL32_Init,
2678 * which also takes care of saving the current 32-bit stack pointer.
2679 * Furthermore, CALL32_Init switches to a new stack and jumps to the
2680 * specified target address.
2682 * NOTE: The CALL32_LargeStack routine may be recursively entered by the
2683 * same thread, but not concurrently entered by several threads.
2685 * Stack layout of CALL32_Init:
2687 * (esp+12) new stack address
2688 * (esp+8) target address
2689 * (esp+4) pointer to variable to receive CALL32_LargeStack address
2690 * (esp) ret addr
2692 * Stack layout of CALL32_LargeStack:
2693 * ... ...
2694 * (ebp+12) arg
2695 * (ebp+8) func
2696 * (ebp+4) ret addr
2697 * (ebp) ebp
2699 static void BuildCallTo32LargeStack( FILE *outfile )
2701 /* Initialization function */
2703 fprintf( outfile, "\n\t.align 4\n" );
2704 #ifdef USE_STABS
2705 fprintf( outfile, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX "CALL32_Init\n");
2706 #endif
2707 fprintf( outfile, "\t.globl " PREFIX "CALL32_Init\n" );
2708 fprintf( outfile, "\t.type " PREFIX "CALL32_Init,@function\n" );
2709 fprintf( outfile, PREFIX "CALL32_Init:\n" );
2710 fprintf( outfile, "\tmovl %%esp,CALL32_Original32_esp\n" );
2711 fprintf( outfile, "\tpopl %%eax\n" );
2712 fprintf( outfile, "\tpopl %%eax\n" );
2713 fprintf( outfile, "\tmovl $CALL32_LargeStack,(%%eax)\n" );
2714 fprintf( outfile, "\tpopl %%eax\n" );
2715 fprintf( outfile, "\tpopl %%esp\n" );
2716 fprintf( outfile, "\tpushl %%eax\n" );
2717 fprintf( outfile, "\tret\n" );
2719 /* Function header */
2721 fprintf( outfile, "\n\t.align 4\n" );
2722 #ifdef USE_STABS
2723 fprintf( outfile, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2724 #endif
2725 fprintf( outfile, "CALL32_LargeStack:\n" );
2727 /* Entry code */
2729 fprintf( outfile, "\tpushl %%ebp\n" );
2730 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2732 /* Switch to the original 32-bit stack pointer */
2734 fprintf( outfile, "\tcmpl $0, CALL32_RecursionCount\n" );
2735 fprintf( outfile, "\tjne CALL32_skip\n" );
2736 fprintf( outfile, "\tmovl CALL32_Original32_esp, %%esp\n" );
2737 fprintf( outfile, "CALL32_skip:\n" );
2739 fprintf( outfile, "\tincl CALL32_RecursionCount\n" );
2741 /* Transfer the argument and call the function */
2743 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2744 fprintf( outfile, "\tcall *8(%%ebp)\n" );
2746 /* Restore registers and return */
2748 fprintf( outfile, "\tdecl CALL32_RecursionCount\n" );
2750 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2751 fprintf( outfile, "\tpopl %%ebp\n" );
2752 fprintf( outfile, "\tret\n" );
2754 /* Data */
2756 fprintf( outfile, "\t.data\n" );
2757 fprintf( outfile, "CALL32_Original32_esp:\t.long 0\n" );
2758 fprintf( outfile, "CALL32_RecursionCount:\t.long 0\n" );
2759 fprintf( outfile, "\t.text\n" );
2763 /*******************************************************************
2764 * BuildCallFrom32Regs
2766 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2767 * 'args' is the number of dword arguments.
2769 * Stack layout:
2770 * ...
2771 * (ebp+12) first arg
2772 * (ebp+8) ret addr to user code
2773 * (ebp+4) ret addr to relay code
2774 * (ebp+0) saved ebp
2775 * (ebp-128) buffer area to allow stack frame manipulation
2776 * (ebp-332) CONTEXT86 struct
2777 * (ebp-336) CONTEXT86 *argument
2778 * .... other arguments copied from (ebp+12)
2780 * The entry point routine is called with a CONTEXT* extra argument,
2781 * following the normal args. In this context structure, EIP_reg
2782 * contains the return address to user code, and ESP_reg the stack
2783 * pointer on return (with the return address and arguments already
2784 * removed).
2786 static void BuildCallFrom32Regs( FILE *outfile )
2788 static const int STACK_SPACE = 128 + sizeof(CONTEXT86);
2790 /* Function header */
2792 fprintf( outfile, "\n\t.align 4\n" );
2793 #ifdef USE_STABS
2794 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
2795 #endif
2796 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
2797 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
2799 /* Allocate some buffer space on the stack */
2801 fprintf( outfile, "\tpushl %%ebp\n" );
2802 fprintf( outfile, "\tmovl %%esp,%%ebp\n ");
2803 fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
2805 /* Build the context structure */
2807 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
2808 fprintf( outfile, "\tpushfl\n" );
2809 fprintf( outfile, "\tpopl %%eax\n" );
2810 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
2811 fprintf( outfile, "\tmovl 0(%%ebp),%%eax\n" );
2812 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
2813 fprintf( outfile, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
2814 fprintf( outfile, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
2815 fprintf( outfile, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
2816 fprintf( outfile, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
2817 fprintf( outfile, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
2819 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2820 fprintf( outfile, "\tmovw %%cs,%%ax\n" );
2821 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs) - STACK_SPACE );
2822 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2823 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
2824 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2825 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
2826 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2827 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
2828 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
2829 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs) - STACK_SPACE );
2830 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2831 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs) - STACK_SPACE );
2832 fprintf( outfile, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
2834 fprintf( outfile, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL );
2835 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags) - STACK_SPACE );
2837 fprintf( outfile, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
2838 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
2840 /* Transfer the arguments */
2842 fprintf( outfile, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
2843 fprintf( outfile, "\tpushl %%esp\n" ); /* push ptr to context struct */
2844 fprintf( outfile, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
2845 fprintf( outfile, "\tjecxz 1f\n" );
2846 fprintf( outfile, "\tsubl %%ecx,%%esp\n" );
2847 fprintf( outfile, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */
2848 fprintf( outfile, "\tmovl %%esp,%%edi\n" );
2849 fprintf( outfile, "\tshrl $2,%%ecx\n" );
2850 fprintf( outfile, "\tcld\n" );
2851 fprintf( outfile, "\trep\n\tmovsl\n" ); /* copy args */
2853 fprintf( outfile, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
2854 fprintf( outfile, "\tleal 12(%%ebp,%%eax),%%eax\n" );
2855 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2857 /* Call the entry point */
2859 fprintf( outfile, "\tcall *0(%%ebx)\n" );
2861 /* Store %eip and %ebp onto the new stack */
2863 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2864 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
2865 fprintf( outfile, "\tmovl %%eax,-4(%%edx)\n" );
2866 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
2867 fprintf( outfile, "\tmovl %%eax,-8(%%edx)\n" );
2869 /* Restore the context structure */
2871 /* Note: we don't bother to restore %cs, %ds and %ss
2872 * changing them in 32-bit code is a recipe for disaster anyway
2874 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
2875 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2876 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
2877 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2878 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
2879 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2881 fprintf( outfile, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
2882 fprintf( outfile, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
2883 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
2884 fprintf( outfile, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
2885 fprintf( outfile, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
2887 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
2888 fprintf( outfile, "\tpushl %%eax\n" );
2889 fprintf( outfile, "\tpopfl\n" );
2890 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
2892 fprintf( outfile, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2893 fprintf( outfile, "\tleal -8(%%ebp),%%esp\n" );
2894 fprintf( outfile, "\tpopl %%ebp\n" );
2895 fprintf( outfile, "\tret\n" );
2899 /*******************************************************************
2900 * BuildGlue
2902 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
2904 static void BuildGlue( FILE *outfile, FILE *infile )
2906 char buffer[1024];
2908 /* File header */
2910 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
2911 input_file_name );
2912 fprintf( outfile, "#include \"builtin16.h\"\n" );
2913 fprintf( outfile, "#include \"stackframe.h\"\n\n" );
2915 /* Build the callback glue functions */
2917 while (fgets( buffer, sizeof(buffer), infile ))
2919 if (strstr( buffer, "### start build ###" )) break;
2921 while (fgets( buffer, sizeof(buffer), infile ))
2923 char *p;
2924 if ( (p = strstr( buffer, "CallFrom16_" )) != NULL )
2926 char *q, *profile = p + strlen( "CallFrom16_" );
2927 for (q = profile; (*q == '_') || isalpha(*q); q++ )
2929 *q = '\0';
2930 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
2932 if ( ++q < p ) p[-1] = '\0'; else q = "";
2933 BuildCallFrom16Func( outfile, profile, q, FALSE );
2935 if ( (p = strstr( buffer, "CallTo16_" )) != NULL )
2937 char *q, *profile = p + strlen( "CallTo16_" );
2938 for (q = profile; (*q == '_') || isalpha(*q); q++ )
2940 *q = '\0';
2941 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
2943 if ( ++q < p ) p[-1] = '\0'; else q = "";
2944 BuildCallTo16Func( outfile, profile, q );
2946 if (strstr( buffer, "### stop build ###" )) break;
2949 fclose( infile );
2952 /*******************************************************************
2953 * BuildCall16
2955 * Build the 16-bit callbacks
2957 static void BuildCall16( FILE *outfile )
2959 /* File header */
2961 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2962 fprintf( outfile, "\t.text\n" );
2964 #ifdef __i386__
2966 #ifdef USE_STABS
2967 if (output_file_name)
2969 char buffer[1024];
2970 getcwd(buffer, sizeof(buffer));
2971 fprintf( outfile, "\t.file\t\"%s\"\n", output_file_name );
2974 * The stabs help the internal debugger as they are an indication that it
2975 * is sensible to step into a thunk/trampoline.
2977 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2978 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name );
2979 fprintf( outfile, "Code_Start:\n\n" );
2981 #endif
2982 fprintf( outfile, PREFIX"Call16_Start:\n" );
2983 fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
2984 fprintf( outfile, "\t.byte 0\n\n" );
2987 /* Standard CallFrom16 routine (WORD return) */
2988 BuildCallFrom16Core( outfile, FALSE, FALSE, TRUE );
2990 /* Standard CallFrom16 routine (DWORD return) */
2991 BuildCallFrom16Core( outfile, FALSE, FALSE, FALSE );
2993 /* Register CallFrom16 routine */
2994 BuildCallFrom16Core( outfile, TRUE, FALSE, FALSE );
2996 /* C16ThkSL CallFrom16 routine */
2997 BuildCallFrom16Core( outfile, FALSE, TRUE, FALSE );
2999 /* Standard CallTo16 routine (WORD return) */
3000 BuildCallTo16Core( outfile, TRUE, FALSE );
3002 /* Standard CallTo16 routine (DWORD return) */
3003 BuildCallTo16Core( outfile, FALSE, FALSE );
3005 /* Register CallTo16 routine (16:16 retf) */
3006 BuildCallTo16Core( outfile, FALSE, 1 );
3008 /* Register CallTo16 routine (16:32 retf) */
3009 BuildCallTo16Core( outfile, FALSE, 2 );
3011 /* CBClientThunkSL routine */
3012 BuildCallTo32CBClient( outfile, FALSE );
3014 /* CBClientThunkSLEx routine */
3015 BuildCallTo32CBClient( outfile, TRUE );
3017 fprintf( outfile, PREFIX"Call16_End:\n" );
3018 fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
3020 #ifdef USE_STABS
3021 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
3022 fprintf( outfile, ".Letext:\n");
3023 #endif
3025 /* The whole Call16_Ret segment must lie within the .data section */
3026 fprintf( outfile, "\n\t.data\n" );
3027 fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
3028 fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
3030 /* Standard CallTo16 return stub */
3031 BuildRet16Func( outfile );
3033 /* CBClientThunkSL return stub */
3034 BuildCallTo32CBClientRet( outfile, FALSE );
3036 /* CBClientThunkSLEx return stub */
3037 BuildCallTo32CBClientRet( outfile, TRUE );
3039 /* End of Call16_Ret segment */
3040 fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
3041 fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
3043 #else /* __i386__ */
3045 fprintf( outfile, PREFIX"Call16_Start:\n" );
3046 fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
3047 fprintf( outfile, "\t.byte 0\n\n" );
3048 fprintf( outfile, PREFIX"Call16_End:\n" );
3049 fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
3051 fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
3052 fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
3053 fprintf( outfile, "\t.byte 0\n\n" );
3054 fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
3055 fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
3057 #endif /* __i386__ */
3060 /*******************************************************************
3061 * BuildCall32
3063 * Build the 32-bit callbacks
3065 static void BuildCall32( FILE *outfile )
3067 /* File header */
3069 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
3070 fprintf( outfile, "\t.text\n" );
3072 #ifdef __i386__
3074 #ifdef USE_STABS
3075 if (output_file_name)
3077 char buffer[1024];
3078 getcwd(buffer, sizeof(buffer));
3079 fprintf( outfile, "\t.file\t\"%s\"\n", output_file_name );
3082 * The stabs help the internal debugger as they are an indication that it
3083 * is sensible to step into a thunk/trampoline.
3085 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
3086 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name );
3087 fprintf( outfile, "Code_Start:\n" );
3089 #endif
3091 /* Build the 32-bit large stack callback */
3093 BuildCallTo32LargeStack( outfile );
3095 /* Build the register callback function */
3097 BuildCallFrom32Regs( outfile );
3099 #ifdef USE_STABS
3100 fprintf( outfile, "\t.text\n");
3101 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
3102 fprintf( outfile, ".Letext:\n");
3103 #endif
3105 #else /* __i386__ */
3107 /* Just to avoid an empty file */
3108 fprintf( outfile, "\t.long 0\n" );
3110 #endif /* __i386__ */
3114 /*******************************************************************
3115 * usage
3117 static void usage(void)
3119 fprintf( stderr,
3120 "usage: build [-pic] [-o outfile] -spec SPEC_FILE\n"
3121 " build [-pic] [-o outfile] -glue SOURCE_FILE\n"
3122 " build [-pic] [-o outfile] -call16\n"
3123 " build [-pic] [-o outfile] -call32\n" );
3124 if (output_file_name) unlink( output_file_name );
3125 exit(1);
3129 /*******************************************************************
3130 * open_input
3132 static FILE *open_input( const char *name )
3134 FILE *f;
3136 if (!name)
3138 input_file_name = "<stdin>";
3139 return stdin;
3141 input_file_name = name;
3142 if (!(f = fopen( name, "r" )))
3144 fprintf( stderr, "Cannot open input file '%s'\n", name );
3145 if (output_file_name) unlink( output_file_name );
3146 exit(1);
3148 return f;
3151 /*******************************************************************
3152 * main
3154 int main(int argc, char **argv)
3156 FILE *outfile = stdout;
3158 if (argc < 2) usage();
3160 if (!strcmp( argv[1], "-pic" ))
3162 UsePIC = 1;
3163 argv += 1;
3164 argc -= 1;
3165 if (argc < 2) usage();
3168 if (!strcmp( argv[1], "-o" ))
3170 output_file_name = argv[2];
3171 argv += 2;
3172 argc -= 2;
3173 if (argc < 2) usage();
3174 if (!(outfile = fopen( output_file_name, "w" )))
3176 fprintf( stderr, "Unable to create output file '%s'\n", output_file_name );
3177 exit(1);
3181 /* Retrieve the selector values; this assumes that we are building
3182 * the asm files on the platform that will also run them. Probably
3183 * a safe assumption to make.
3185 GET_CS( Code_Selector );
3186 GET_DS( Data_Selector );
3188 if (!strcmp( argv[1], "-spec" )) BuildSpecFile( outfile, open_input( argv[2] ) );
3189 else if (!strcmp( argv[1], "-glue" )) BuildGlue( outfile, open_input( argv[2] ) );
3190 else if (!strcmp( argv[1], "-call16" )) BuildCall16( outfile );
3191 else if (!strcmp( argv[1], "-call32" )) BuildCall32( outfile );
3192 else
3194 fclose( outfile );
3195 usage();
3198 fclose( outfile );
3199 return 0;