2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
4 * Copyright 1995 Martin von Loewis
15 /* ELF symbols do not have an underscore in front */
16 #if defined (__ELF__) || defined (__svr4__)
22 #define TYPE_INVALID 0
26 #define TYPE_PASCAL_16 4
28 #define TYPE_REGISTER 6
32 #define TYPE_STDCALL 10
34 #define MAX_ORDINALS 1299
36 /* Callback function used for stub functions */
37 #define STUB_CALLBACK "RELAY_Unimplemented"
39 typedef struct ordinal_definition_s
44 void *additional_data;
47 typedef struct ordinal_variable_definition_s
53 typedef struct ordinal_function_definition_s
57 char internal_name[80];
60 typedef struct ordinal_return_definition_s
66 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
68 char LowerDLLName[80];
69 char UpperDLLName[80];
75 char *ParseBuffer = NULL;
80 static int debugging = 1;
82 /* Offset of register relative to the end of the context struct */
83 #define CONTEXTOFFSET(reg) \
84 ((int)&(((struct sigcontext_struct *)1)->reg) - 1 \
85 - sizeof(struct sigcontext_struct))
87 static void *xmalloc (size_t size)
91 res = malloc (size ? size : 1);
94 fprintf (stderr, "Virtual memory exhausted.\n");
101 static void *xrealloc (void *ptr, size_t size)
103 void *res = realloc (ptr, size);
106 fprintf (stderr, "Virtual memory exhausted.\n");
113 static int IsNumberString(char *s)
122 static char *strlower(char *s)
126 for(p = s; *p != '\0'; p++)
132 static char *strupper(char *s)
136 for(p = s; *p != '\0'; p++)
142 static char * GetTokenInLine(void)
147 if (ParseNext != ParseBuffer)
149 if (ParseSaveChar == '\0')
151 *ParseNext = ParseSaveChar;
155 * Remove initial white space.
157 for (p = ParseNext; isspace(*p); p++)
160 if ((*p == '\0') || (*p == '#'))
167 if (*token != '(' && *token != ')')
168 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
178 static char * GetToken(void)
182 if (ParseBuffer == NULL)
184 ParseBuffer = xmalloc(512);
185 ParseNext = ParseBuffer;
189 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
191 if (ParseBuffer[0] != '#')
196 while ((token = GetTokenInLine()) == NULL)
198 ParseNext = ParseBuffer;
202 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
204 if (ParseBuffer[0] != '#')
212 static int ParseVariable(int ordinal, int type)
216 char export_name[80];
221 int value_array_size;
223 strcpy(export_name, GetToken());
228 fprintf(stderr, "%d: Expected '(' got '%s'\n", Line, token);
233 value_array_size = 25;
234 value_array = xmalloc(sizeof(*value_array) * value_array_size);
236 while ((token = GetToken()) != NULL)
241 value_array[n_values++] = strtol(token, &endptr, 0);
242 if (n_values == value_array_size)
244 value_array_size += 25;
245 value_array = xrealloc(value_array,
246 sizeof(*value_array) * value_array_size);
249 if (endptr == NULL || *endptr != '\0')
251 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
259 fprintf(stderr, "%d: End of file in variable declaration\n", Line);
263 if (ordinal >= MAX_ORDINALS)
265 fprintf(stderr, "%d: Ordinal number too large\n", Line);
269 odp = &OrdinalDefinitions[ordinal];
271 strcpy(odp->export_name, export_name);
273 vdp = xmalloc(sizeof(*vdp));
274 odp->additional_data = vdp;
276 vdp->n_values = n_values;
277 vdp->values = xrealloc(value_array, sizeof(*value_array) * n_values);
282 static int ParseExportFunction(int ordinal, int type)
289 odp = &OrdinalDefinitions[ordinal];
290 strcpy(odp->export_name, GetToken());
292 fdp = xmalloc(sizeof(*fdp));
293 odp->additional_data = fdp;
298 fprintf(stderr, "%d: Expected '(' got '%s'\n", Line, token);
302 for (i = 0; i < 16; i++)
308 if (!strcmp(token, "byte") || !strcmp(token, "word"))
309 fdp->arg_types[i] = 'w';
310 else if (!strcmp(token, "s_byte") || !strcmp(token, "s_word"))
311 fdp->arg_types[i] = 's';
312 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
313 fdp->arg_types[i] = 'l';
314 else if (!strcmp(token, "ptr"))
315 fdp->arg_types[i] = 'p';
318 fprintf(stderr, "%d: Unknown variable type '%s'\n", Line, token);
322 fdp->arg_types[i] = '\0';
324 strcpy(fdp->internal_name, GetToken());
328 static int ParseEquate(int ordinal)
335 odp = &OrdinalDefinitions[ordinal];
336 strcpy(odp->export_name, GetToken());
339 value = strtol(token, &endptr, 0);
340 if (endptr == NULL || *endptr != '\0')
342 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
347 odp->type = TYPE_ABS;
348 odp->additional_data = (void *) value;
353 static int ParseReturn(int ordinal)
360 rdp = xmalloc(sizeof(*rdp));
362 odp = &OrdinalDefinitions[ordinal];
363 strcpy(odp->export_name, GetToken());
364 odp->type = TYPE_RETURN;
365 odp->additional_data = rdp;
368 rdp->arg_size = strtol(token, &endptr, 0);
369 if (endptr == NULL || *endptr != '\0')
371 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
377 rdp->ret_value = strtol(token, &endptr, 0);
378 if (endptr == NULL || *endptr != '\0')
380 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
389 static int ParseStub( int ordinal )
394 odp = &OrdinalDefinitions[ordinal];
395 strcpy( odp->export_name, GetToken() );
396 odp->type = TYPE_STUB;
397 fdp = xmalloc(sizeof(*fdp));
398 odp->additional_data = fdp;
399 fdp->arg_types[0] = '\0';
400 strcpy( fdp->internal_name, STUB_CALLBACK );
405 static int ParseOrdinal(int ordinal)
409 if (ordinal >= MAX_ORDINALS)
411 fprintf(stderr, "%d: Ordinal number too large\n", Line);
414 if (ordinal > Limit) Limit = ordinal;
419 fprintf(stderr, "%d: Expected type after ordinal\n", Line);
423 if (strcmp(token, "byte") == 0)
424 return ParseVariable(ordinal, TYPE_BYTE);
425 else if (strcmp(token, "word") == 0)
426 return ParseVariable(ordinal, TYPE_WORD);
427 else if (strcmp(token, "long") == 0)
428 return ParseVariable(ordinal, TYPE_LONG);
429 else if (strcmp(token, "p") == 0)
430 return ParseExportFunction(ordinal, TYPE_PASCAL);
431 else if (strcmp(token, "pascal") == 0)
432 return ParseExportFunction(ordinal, TYPE_PASCAL);
433 else if (strcmp(token, "pascal16") == 0)
434 return ParseExportFunction(ordinal, TYPE_PASCAL_16);
435 else if (strcmp(token, "register") == 0)
436 return ParseExportFunction(ordinal, TYPE_REGISTER);
437 else if (strcmp(token, "stdcall") == 0)
438 return ParseExportFunction(ordinal, TYPE_STDCALL);
439 else if (strcmp(token, "equate") == 0)
440 return ParseEquate(ordinal);
441 else if (strcmp(token, "return") == 0)
442 return ParseReturn(ordinal);
443 else if (strcmp(token, "stub") == 0)
444 return ParseStub(ordinal);
448 "%d: Expected type after ordinal, found '%s' instead\n",
454 static int ParseTopLevel(void)
458 while ((token = GetToken()) != NULL)
460 if (strcmp(token, "name") == 0)
462 strcpy(LowerDLLName, GetToken());
463 strlower(LowerDLLName);
465 strcpy(UpperDLLName, LowerDLLName);
466 strupper(UpperDLLName);
468 else if (strcmp(token, "id") == 0)
471 if (!IsNumberString(token))
473 fprintf(stderr, "%d: Expected number after id\n", Line);
479 else if (strcmp(token, "base") == 0)
482 if (!IsNumberString(token))
484 fprintf(stderr, "%d: Expected number after base\n", Line);
490 else if (IsNumberString(token))
495 ordinal = atoi(token);
496 if ((rv = ParseOrdinal(ordinal)) < 0)
502 "%d: Expected name, id, length or ordinal\n", Line);
511 static int OutputVariableCode( char *storage, ORDDEF *odp )
516 vdp = odp->additional_data;
517 printf( "\t.data\n" );
518 for (i = 0; i < vdp->n_values; i++)
521 printf( "\t%s\t", storage);
523 printf( "%d", vdp->values[i]);
525 if ((i & 7) == 7 || i == vdp->n_values - 1) printf( "\n");
529 printf( "\t.text\n" );
530 return vdp->n_values;
534 /*******************************************************************
537 * Build the in-memory representation of the module, and dump it
538 * as a byte stream into the assembly code.
540 static void BuildModule( int max_code_offset, int max_data_offset )
546 SEGTABLEENTRY *pSegment;
547 LOADEDFILEINFO *pFileInfo;
553 * LOADEDFILEINFO File information
554 * SEGTABLEENTRY Segment 1 (code)
555 * SEGTABLEENTRY Segment 2 (data)
556 * WORD[2] Resource table (empty)
557 * BYTE[2] Imported names (empty)
558 * BYTE[n] Resident names table
559 * BYTE[n] Entry table
562 buffer = xmalloc( 0x10000 );
564 pModule = (NE_MODULE *)buffer;
565 pModule->magic = NE_SIGNATURE;
568 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
570 pModule->heap_size = 0xffff;
571 pModule->stack_size = 0;
576 pModule->seg_count = 2;
577 pModule->modref_count = 0;
578 pModule->nrname_size = 0;
579 pModule->modref_table = 0;
580 pModule->nrname_fpos = 0;
581 pModule->moveable_entries = 0;
582 pModule->alignment = 0;
583 pModule->truetype = 0;
584 pModule->os_flags = NE_OSFLAGS_WINDOWS;
585 pModule->misc_flags = 0;
586 pModule->dlls_to_init = 0;
587 pModule->nrname_handle = 0;
588 pModule->min_swap_area = 0;
589 pModule->expected_version = 0x030a;
591 /* File information */
593 pFileInfo = (LOADEDFILEINFO *)(pModule + 1);
594 pModule->fileinfo = (int)pFileInfo - (int)pModule;
595 pFileInfo->length = sizeof(LOADEDFILEINFO) + strlen(UpperDLLName) + 3;
596 pFileInfo->fixed_media = 0;
597 pFileInfo->error = 0;
600 sprintf( pFileInfo->filename, "%s.DLL", UpperDLLName );
601 pstr = (char *)pFileInfo + pFileInfo->length + 1;
605 pSegment = (SEGTABLEENTRY *)pstr;
606 pModule->seg_table = (int)pSegment - (int)pModule;
607 pSegment->filepos = 0;
608 pSegment->size = max_code_offset;
610 pSegment->minsize = max_code_offset;
611 pSegment->selector = 0;
614 pModule->dgroup_entry = (int)pSegment - (int)pModule;
615 pSegment->filepos = 0;
616 pSegment->size = max_data_offset;
617 pSegment->flags = NE_SEGFLAGS_DATA;
618 pSegment->minsize = max_data_offset;
619 pSegment->selector = 0;
624 pword = (WORD *)pSegment;
625 pModule->res_table = (int)pword - (int)pModule;
629 /* Imported names table */
631 pstr = (char *)pword;
632 pModule->import_table = (int)pstr - (int)pModule;
636 /* Resident names table */
638 pModule->name_table = (int)pstr - (int)pModule;
639 /* First entry is module name */
640 *pstr = strlen(UpperDLLName );
641 strcpy( pstr + 1, UpperDLLName );
644 pstr += sizeof(WORD);
645 /* Store all ordinals */
646 odp = OrdinalDefinitions + 1;
647 for (i = 1; i <= Limit; i++, odp++)
649 if (!odp->export_name[0]) continue;
650 *pstr = strlen( odp->export_name );
651 strcpy( pstr + 1, odp->export_name );
652 strupper( pstr + 1 );
655 pstr += sizeof(WORD);
661 pModule->entry_table = (int)pstr - (int)pModule;
663 odp = OrdinalDefinitions + 1;
664 for (i = 1; i <= Limit; i++, odp++)
671 selector = 0; /* Invalid selector */
679 selector = 1; /* Code selector */
685 selector = 2; /* Data selector */
689 selector = 0xfe; /* Constant selector */
693 /* create a new bundle if necessary */
694 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
698 bundle[1] = selector;
706 *(WORD *)pstr = odp->offset;
707 pstr += sizeof(WORD);
712 /* Dump the module content */
714 printf( "\t.data\n" );
715 printf( "\t.globl " PREFIX "%s_Module_Start\n", UpperDLLName );
716 printf( PREFIX "%s_Module_Start:\n", UpperDLLName );
717 size = (int)pstr - (int)pModule;
718 for (i = 0, pstr = buffer; i < size; i++, pstr++)
720 if (!(i & 7)) printf( "\t.byte " );
721 printf( "%d%c", *pstr, ((i & 7) != 7) ? ',' : '\n' );
723 if (i & 7) printf( "0\n" );
724 printf( "\t.globl " PREFIX "%s_Module_End\n", UpperDLLName );
725 printf( PREFIX "%s_Module_End:\n", UpperDLLName );
729 static void BuildSpec32Files( char *specname )
736 SpecFp = fopen( specname, "r");
739 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
745 printf( "/* File generated automatically, do not edit! */\n" );
746 printf( "#include <sys/types.h>\n");
747 printf( "#include \"windows.h\"\n");
748 printf( "#include \"dlls.h\"\n");
749 printf( "#include \"pe_image.h\"\n");
750 printf( "#include \"winerror.h\"\n");
751 printf( "#include \"relay32.h\"\n");
752 printf( "#include \"stddebug.h\"\n");
753 printf( "#include \"debug.h\"\n");
755 odp = OrdinalDefinitions;
756 for (i = 0; i <= Limit; i++, odp++)
759 fdp = odp->additional_data;
760 rdp = odp->additional_data;
766 printf( "int %s_%d()\n{\n\t", UpperDLLName, i);
767 printf( "RELAY32_Unimplemented(\"%s\",%d);\n", UpperDLLName, i);
768 printf( "\t/*NOTREACHED*/\n\treturn 0;\n}\n\n");
771 argc=strlen(fdp->arg_types);
772 printf( "void %s_%d(", UpperDLLName, i);
773 for(argno=0;argno<argc;argno++)
775 switch(fdp->arg_types[argno])
777 case 'p': printf( "void *");break;
778 case 'l': printf( "int ");break;
780 fprintf(stderr, "Not supported argument type %c\n",
781 fdp->arg_types[argno]);
784 putchar( 'a'+argno );
785 if (argno!=argc-1) putchar( ',' );
788 printf( "\tdprintf_relay(stddeb,\"Call %%s.%%s(");
789 for (argno=0;argno<argc;argno++)
792 putchar( (fdp->arg_types[argno] == 'p') ? 'p' : 'x' );
793 if (argno < argc-1) putchar( ',' );
795 printf( ")\\n\", \"%s\", \"%s\"", UpperDLLName, odp->export_name);
796 for(argno=0;argno<argc;argno++) printf( ",%c", 'a'+argno);
797 printf( ");\n\t%s(", fdp->internal_name );
798 for(argno=0;argno<argc;argno++)
801 if (argno!=argc-1) putchar(',');
803 printf( ");\n\t__asm__ __volatile__(\"movl %%ebp,%%esp;"
804 "popl %%ebp;ret $%d\");\n}\n\n",
808 printf( "void %s_%d()\n{\n\t", UpperDLLName, i);
809 printf( "RELAY32_DebugEnter(\"%s\",\"%s\");\n\t",
810 UpperDLLName, odp->export_name);
811 printf( "WIN32_LastError=ERROR_CALL_NOT_IMPLEMENTED;\n");
812 printf( "\t__asm__ __volatile__ (\"movl %d,%%eax\");\n",
814 printf( "\t__asm__ __volatile__ (\"movl %%ebp,%%esp;popl %%ebp;"
815 "ret $%d\");\n}\n\n", rdp->arg_size);
818 fprintf(stderr,"build: function type %d not available for Win32\n",
824 printf( "static WIN32_function functions[%d+1]={\n", Limit);
826 odp = OrdinalDefinitions;
827 for (i = 0; i <= Limit; i++, odp++)
829 fdp = odp->additional_data;
830 rdp = odp->additional_data;
835 printf( "{0,%s_%d},\n",UpperDLLName, i);
840 printf( "{\"%s\",%s_%d},\n", odp->export_name, UpperDLLName, i);
843 fprintf(stderr, "build: implementation error: missing %d\n",
850 printf( "static WIN32_builtin dll={\"%s\",functions,%d,0};\n",
851 UpperDLLName, Limit+1);
853 printf( "void %s_Init(void)\n{\n",UpperDLLName);
854 printf( "\tdll.next=WIN32_builtin_list;\n");
855 printf( "\tWIN32_builtin_list=&dll;\n}");
859 static void BuildSpec16Files( char *specname )
865 int code_offset, data_offset;
867 SpecFp = fopen( specname, "r");
870 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
876 printf( "/* File generated automatically; do not edit! */\n" );
877 printf( "\t.data\n" );
878 printf( "\t.globl " PREFIX "%s_Data_Start\n", UpperDLLName );
879 printf( PREFIX "%s_Data_Start:\n", UpperDLLName );
881 printf( "\t.4byte 0,0,0,0,0,0,0,0\n" );
883 printf( "\t.word 0,0,0,0,0,0,0,0\n" );
886 printf( "\t.text\n" );
887 printf( "\t.globl " PREFIX "%s_Code_Start\n", UpperDLLName );
888 printf( PREFIX "%s_Code_Start:\n", UpperDLLName );
891 odp = OrdinalDefinitions;
892 for (i = 0; i <= Limit; i++, odp++)
894 fdp = odp->additional_data;
895 rdp = odp->additional_data;
900 odp->offset = 0xffff;
904 odp->offset = (int)odp->additional_data & 0xffff;
908 printf( "/* %s.%d */\n", UpperDLLName, i);
909 odp->offset = data_offset;
910 data_offset += OutputVariableCode( ".byte", odp);
914 printf( "/* %s.%d */\n", UpperDLLName, i);
915 odp->offset = data_offset;
917 data_offset += 2 * OutputVariableCode( ".4byte", odp);
919 data_offset += 2 * OutputVariableCode( ".word", odp);
924 printf( "/* %s.%d */\n", UpperDLLName, i);
925 odp->offset = data_offset;
926 data_offset += 4 * OutputVariableCode( ".long", odp);
930 printf( "/* %s.%d */\n", UpperDLLName, i);
931 printf( "\tmovw $%d,%%ax\n", rdp->ret_value & 0xffff );
932 printf( "\tmovw $%d,%%dx\n", (rdp->ret_value >> 16) & 0xffff);
933 printf( "\t.byte 0x66\n");
934 if (rdp->arg_size != 0)
935 printf( "\tlret $%d\n", rdp->arg_size);
938 odp->offset = code_offset;
939 code_offset += 10; /* Assembly code is 10 bytes long */
940 if (rdp->arg_size != 0) code_offset += 2;
947 printf( "/* %s.%d */\n", UpperDLLName, i);
948 printf( "\tpushw %%bp\n" );
949 printf( "\tpushl $0x%08x\n", (DLLId << 16) | i);
950 printf( "\tpushl $" PREFIX "%s\n", fdp->internal_name );
951 printf( "\tljmp $0x%04x, $" PREFIX "CallTo32_%s_%s\n\n",
953 (odp->type == TYPE_REGISTER) ? "regs" :
954 (odp->type == TYPE_PASCAL) ? "long" : "word",
961 odp->offset = code_offset;
962 code_offset += 24; /* Assembly code is 24 bytes long */
966 fprintf( stderr, "build: Unknown function type; please report.\n");
971 if (!code_offset) /* Make sure the code segment is not empty */
973 printf( "\t.byte 0\n" );
977 BuildModule( code_offset, data_offset );
981 /*******************************************************************
982 * BuildCall32LargeStack
984 * Build the function used to switch to the original 32-bit stack
985 * before calling a 32-bit function from 32-bit code. This is used for
986 * functions that need a large stack, like X bitmaps functions.
988 * The generated function has the following prototype:
989 * int CallTo32_LargeStack( int (*func)(), int nbargs, ... )
1000 static void BuildCall32LargeStack(void)
1002 /* Function header */
1004 printf( "/**********\n" );
1005 printf( " * " PREFIX "CallTo32_LargeStack\n" );
1006 printf( " **********/\n" );
1007 printf( "\t.align 4\n" );
1008 printf( "\t.globl " PREFIX "CallTo32_LargeStack\n\n" );
1009 printf( PREFIX "CallTo32_LargeStack:\n" );
1013 printf( "\tpushl %%ebp\n" );
1014 printf( "\tmovl %%esp,%%ebp\n" );
1016 /* Save registers */
1018 printf( "\tpushl %%ecx\n" );
1019 printf( "\tpushl %%esi\n" );
1020 printf( "\tpushl %%edi\n" );
1022 /* Retrieve the original 32-bit stack pointer and switch to it if any */
1024 printf( "\tmovl " PREFIX "IF1632_Original32_esp, %%eax\n" );
1025 printf( "\torl %%eax,%%eax\n" );
1026 printf( "\tje 0f\n" );
1027 printf( "\tmovl %%eax,%%esp\n" );
1030 /* Transfer the arguments */
1032 printf( "\tmovl 12(%%ebp),%%ecx\n" );
1033 printf( "\torl %%ecx,%%ecx\n" );
1034 printf( "\tje 1f\n" );
1035 printf( "\tleal 16(%%ebp),%%esi\n" );
1036 printf( "\tshll $2,%%ecx\n" );
1037 printf( "\tsubl %%ecx,%%esp\n" );
1038 printf( "\tmovl %%esp,%%edi\n" );
1039 printf( "\tshrl $2,%%ecx\n" );
1040 printf( "\tcld\n" );
1041 printf( "\trep; movsl\n" );
1044 /* Call the function */
1046 printf( "\tcall 8(%%ebp)\n" );
1048 /* Switch back to the normal stack */
1050 printf( "\tleal -12(%%ebp),%%esp\n" );
1052 /* Restore registers and return */
1054 printf( "\tpopl %%edi\n" );
1055 printf( "\tpopl %%esi\n" );
1056 printf( "\tpopl %%ecx\n" );
1057 printf( "\tpopl %%ebp\n" );
1058 printf( "\tret\n" );
1062 /*******************************************************************
1063 * TransferArgs16To32
1065 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1066 * The 16-bit stack layout is:
1074 static int TransferArgs16To32( char *args )
1076 int i, pos16, pos32;
1078 /* Save ebx first */
1080 printf( "\tpushl %%ebx\n" );
1082 /* Get the 32-bit stack pointer */
1084 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1086 /* Copy the arguments */
1088 pos16 = 6; /* skip bp and return address */
1091 for (i = strlen(args); i > 0; i--)
1096 case 'w': /* word */
1097 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1098 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1102 case 's': /* s_word */
1103 printf( "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1104 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1108 case 'l': /* long */
1109 printf( "\tmovl %d(%%ebp),%%eax\n", pos16 );
1110 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1115 /* Get the selector */
1116 printf( "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1117 /* Get the selector base */
1118 printf( "\tandl $0xfff8,%%eax\n" );
1119 printf( "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1120 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1121 /* Add the offset */
1122 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1123 printf( "\taddl %%eax,%d(%%ebx)\n", pos32 );
1128 fprintf( stderr, "Unknown arg type '%c'\n", args[i-1] );
1134 printf( "\tpopl %%ebx\n" );
1136 return pos16 - 6; /* Return the size of the 16-bit args */
1140 /*******************************************************************
1143 * Build the context structure on the 32-bit stack.
1144 * The only valid registers in the context structure are:
1145 * eax, ebx, ecx, edx, esi, edi, ds, es, (some of the) flags
1147 static void BuildContext(void)
1149 /* Save ebx first */
1151 printf( "\tpushl %%ebx\n" );
1153 /* Get the 32-bit stack pointer */
1155 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1157 /* Store the registers */
1159 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_ebx) ); /* Get ebx from stack */
1160 printf( "\tmovl %%eax,%d(%%ebx)\n", CONTEXTOFFSET(sc_eax) );
1161 printf( "\tmovl %%ecx,%d(%%ebx)\n", CONTEXTOFFSET(sc_ecx) );
1162 printf( "\tmovl %%edx,%d(%%ebx)\n", CONTEXTOFFSET(sc_edx) );
1163 printf( "\tmovl %%esi,%d(%%ebx)\n", CONTEXTOFFSET(sc_esi) );
1164 printf( "\tmovl %%edi,%d(%%ebx)\n", CONTEXTOFFSET(sc_edi) );
1165 printf( "\tmovw -10(%%ebp),%%ax\n" ); /* Get saved ds from stack */
1166 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(sc_ds) );
1167 printf( "\tmovw -12(%%ebp),%%ax\n" ); /* Get saved es from stack */
1168 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(sc_es) );
1169 printf( "\tpushfl\n" );
1171 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_eflags) );
1173 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_efl) );
1178 /*******************************************************************
1181 * Restore the registers from the context structure
1183 static void RestoreContext(void)
1185 /* Get the 32-bit stack pointer */
1187 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1189 /* Restore the registers */
1191 printf( "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(sc_ecx) );
1192 printf( "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(sc_edx) );
1193 printf( "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(sc_esi) );
1194 printf( "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(sc_edi) );
1195 printf( "\tpopl %%eax\n" ); /* Remove old ds and es from stack */
1196 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(sc_ds) ); /* Push new ds */
1197 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(sc_es) ); /* Push new es */
1199 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(sc_eflags) );
1201 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(sc_efl) );
1203 printf( "\tpopfl\n" );
1204 printf( "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(sc_eax) );
1205 printf( "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(sc_ebx) );
1209 /*******************************************************************
1212 * Build a 32-bit callback function. The syntax of the function
1213 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1214 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1215 * 'l'=long, 'p'=pointer).
1216 * For register functions, the arguments are ignored, but they are still
1217 * removed from the stack upon return.
1219 * Stack layout upon entry to the callback function:
1221 * (sp+14) first 16-bit arg
1225 * (sp+4) dll_id+ordinal (long)
1226 * (sp) entrypoint (long)
1229 static void BuildCall32Func( char *profile )
1234 char *args = profile + 5;
1236 /* Parse function type */
1238 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1239 else if (!strncmp( "regs_", profile, 5 )) reg_func = 1;
1240 else if (strncmp( "long_", profile, 5 ))
1242 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1246 /* Function header */
1248 printf( "/**********\n" );
1249 printf( " * " PREFIX "CallTo32_%s\n", profile );
1250 printf( " **********/\n" );
1251 printf( "\t.align 4\n" );
1252 printf( "\t.globl " PREFIX "CallTo32_%s\n\n", profile );
1253 printf( PREFIX "CallTo32_%s:\n", profile );
1255 /* Setup bp to point to its copy on the stack */
1257 printf( "\tmovzwl %%sp,%%ebp\n" );
1258 printf( "\taddw $8,%%bp\n" );
1260 /* Save 16-bit ds and es */
1262 printf( "\tpushw %%ds\n" );
1263 printf( "\tpushw %%es\n" );
1265 /* Restore 32-bit ds and es */
1267 printf( "\tpushl $0x%04x%04x\n", WINE_DATA_SELECTOR, WINE_DATA_SELECTOR );
1268 printf( "\tpopw %%ds\n" );
1269 printf( "\tpopw %%es\n" );
1272 /* Save the 16-bit stack */
1274 printf( "\tpushw " PREFIX "IF1632_Saved16_sp\n" );
1275 printf( "\tpushw " PREFIX "IF1632_Saved16_ss\n" );
1276 printf( "\tmovw %%ss," PREFIX "IF1632_Saved16_ss\n" );
1277 printf( "\tmovw %%sp," PREFIX "IF1632_Saved16_sp\n" );
1279 /* Transfer the arguments */
1281 if (reg_func) BuildContext();
1282 else if (*args) argsize = TransferArgs16To32( args );
1284 /* Get the address of the API function */
1286 printf( "\tmovl -8(%%ebp),%%eax\n" );
1288 /* If necessary, save %edx over the API function address */
1290 if (!reg_func && short_ret)
1291 printf( "\tmovl %%edx,-8(%%ebp)\n" );
1293 /* Switch to the 32-bit stack */
1295 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebp\n" );
1296 printf( "\tpushw %%ds\n" );
1297 printf( "\tpopw %%ss\n" );
1298 printf( "\tleal -%d(%%ebp),%%esp\n",
1299 reg_func ? sizeof(struct sigcontext_struct) : 4 * strlen(args) );
1301 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1303 printf( "\taddl $24,%%ebp\n" );
1305 /* Print the debug information before the call */
1309 printf( "\tpushl %%eax\n" );
1310 printf( "\tpushl $CALL32_Str_%s\n", profile );
1311 printf( "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0) );
1312 printf( "\tcall " PREFIX "RELAY_DebugCall32\n" );
1313 printf( "\tpopl %%eax\n" );
1314 printf( "\tpopl %%eax\n" );
1315 printf( "\tpopl %%eax\n" );
1318 /* Call the entry point */
1320 printf( "\tcall %%eax\n" );
1322 /* Print the debug information after the call */
1326 printf( "\tpushl %%eax\n" );
1327 printf( "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0) );
1328 printf( "\tcall " PREFIX "RELAY_DebugReturn\n" );
1329 printf( "\tpopl %%eax\n" );
1330 printf( "\tpopl %%eax\n" );
1333 /* Restore the 16-bit stack */
1335 printf( "\tmovw " PREFIX "IF1632_Saved16_ss,%%ss\n" );
1336 printf( "\tmovw " PREFIX "IF1632_Saved16_sp,%%sp\n" );
1337 printf( "\tpopw " PREFIX "IF1632_Saved16_ss\n" );
1338 printf( "\tpopw " PREFIX "IF1632_Saved16_sp\n" );
1342 /* Restore registers from the context structure */
1345 /* Calc the arguments size */
1359 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1365 /* Restore ds and es */
1367 printf( "\tpopw %%es\n" );
1368 printf( "\tpopw %%ds\n" );
1370 /* Get the return value into dx:ax and clean up the stack */
1376 printf( "\tpopl %%edx\n" ); /* Restore %edx */
1377 printf( "\taddl $4,%%esp\n" ); /* Remove DLL id and ordinal */
1381 printf( "\tpushl %%eax\n" );
1382 printf( "\tpopw %%ax\n" );
1383 printf( "\tpopw %%dx\n" );
1384 /* Remove API entry point, DLL id and ordinal from the stack */
1385 printf( "\taddl $8,%%esp\n" );
1390 /* Remove API entry point, DLL id and ordinal from the stack, */
1391 /* but take care not to change the value of the carry flag. */
1393 printf( "\tpopl %%ebp\n" );
1394 printf( "\tpopl %%ebp\n" );
1399 printf( "\tpopw %%bp\n" );
1401 /* Remove the arguments and return */
1405 printf( "\t.byte 0x66\n" );
1406 printf( "\tlret $%d\n", argsize );
1410 printf( "\t.byte 0x66\n" );
1411 printf( "\tlret\n" );
1416 /*******************************************************************
1419 * Build a 16-bit callback function.
1421 * Stack frame of the callback function:
1425 * (ebp+16) 16-bit ds
1426 * (ebp+12) func to call
1427 * (ebp+8) code selector
1428 * (ebp+4) return address
1429 * (ebp) previous ebp
1431 * Prototypes for the CallTo16 functions:
1432 * extern WORD CallTo16_word_xxx( FARPROC func, WORD ds, args... );
1433 * extern LONG CallTo16_long_xxx( FARPROC func, WORD ds, args... );
1434 * extern void CallTo16_regs_( FARPROC func, WORD ds, WORD es, WORD bp,
1435 * WORD ax, WORD bx, WORD cx, WORD dx,
1436 * WORD si, WORD di );
1438 static void BuildCall16Func( char *profile )
1442 char *args = profile + 5;
1444 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1445 else if (!strncmp( "regs_", profile, 5 )) reg_func = short_ret = 1;
1446 else if (strncmp( "long_", profile, 5 ))
1448 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1452 /* Function header */
1454 printf( "/**********\n" );
1455 printf( " * " PREFIX "CallTo16_%s\n", profile );
1456 printf( " **********/\n" );
1457 printf( "\t.align 4\n" );
1458 printf( "\t.globl " PREFIX "CallTo16_%s\n\n", profile );
1459 printf( PREFIX "CallTo16_%s:\n", profile );
1461 /* Push code selector before return address to simulate a lcall */
1463 printf( "\tpopl %%eax\n" );
1464 printf( "\tpushl $0x%04x\n", WINE_CODE_SELECTOR );
1465 printf( "\tpushl %%eax\n" );
1469 printf( "\tpushl %%ebp\n" );
1470 printf( "\tmovl %%esp,%%ebp\n" );
1472 /* Save the 32-bit registers */
1474 printf( "\tpushl %%ebx\n" );
1475 printf( "\tpushl %%ecx\n" );
1476 printf( "\tpushl %%edx\n" );
1477 printf( "\tpushl %%esi\n" );
1478 printf( "\tpushl %%edi\n" );
1480 /* Save the 32-bit stack */
1482 printf( "\tpushl " PREFIX "IF1632_Saved32_esp\n" );
1483 printf( "\tmovl %%esp," PREFIX "IF1632_Saved32_esp\n" );
1484 printf( "\tmovl %%ebp,%%ebx\n" );
1486 /* Print debugging info */
1490 /* Push the address of the first argument */
1491 printf( "\tmovl %%ebx,%%eax\n" );
1492 printf( "\taddl $12,%%eax\n" );
1493 printf( "\tpushl $%d\n", reg_func ? 8 : strlen(args) );
1494 printf( "\tpushl %%eax\n" );
1495 printf( "\tcall " PREFIX "RELAY_DebugCall16\n" );
1496 printf( "\tpopl %%eax\n" );
1497 printf( "\tpopl %%eax\n" );
1500 /* Switch to the 16-bit stack */
1502 printf( "\tmovw " PREFIX "IF1632_Saved16_ss,%%ss\n" );
1503 printf( "\tmovw " PREFIX "IF1632_Saved16_sp,%%sp\n" );
1505 /* Transfer the arguments */
1509 /* Get the registers. ebx is handled later on. */
1510 printf( "\tpushw 20(%%ebx)\n" );
1511 printf( "\tpopw %%es\n" );
1512 printf( "\tmovl 24(%%ebx),%%ebp\n" );
1513 printf( "\tmovl 28(%%ebx),%%eax\n" );
1514 printf( "\tmovl 36(%%ebx),%%ecx\n" );
1515 printf( "\tmovl 40(%%ebx),%%edx\n" );
1516 printf( "\tmovl 44(%%ebx),%%esi\n" );
1517 printf( "\tmovl 48(%%ebx),%%edi\n" );
1519 else /* not a register function */
1521 int pos = 20; /* first argument position */
1523 /* Make %bp point to the previous stackframe (built by CallTo32) */
1524 printf( "\tmovw %%sp,%%bp\n" );
1525 printf( "\taddw $16,%%bp\n" );
1531 case 'w': /* word */
1532 printf( "\tpushw %d(%%ebx)\n", pos );
1534 case 'l': /* long */
1535 printf( "\tpushl %d(%%ebx)\n", pos );
1542 /* Push the return address */
1544 printf( "\tpushl " PREFIX "CALL16_RetAddr_%s\n",
1545 short_ret ? "word" : "long" );
1547 /* Push the called routine address */
1549 printf( "\tpushl 12(%%ebx)\n" );
1551 /* Get the 16-bit ds */
1555 printf( "\tpushw 16(%%ebx)\n" );
1556 printf( "\tmovl 32(%%ebx),%%ebx\n" ); /*Get ebx from the 32-bit stack*/
1557 printf( "\tpopw %%ds\n" );
1561 /* Set ax equal to ds for window procedures */
1562 printf( "\tmovw 16(%%ebx),%%ax\n" );
1563 printf( "\tmovw %%ax,%%ds\n" );
1566 /* Jump to the called routine */
1568 printf( "\t.byte 0x66\n" );
1569 printf( "\tlret\n" );
1573 /*******************************************************************
1576 * Build the return code for 16-bit callbacks
1578 static void BuildRet16Func()
1580 printf( "\t.globl " PREFIX "CALL16_Ret_word\n" );
1581 printf( "\t.globl " PREFIX "CALL16_Ret_long\n" );
1583 /* Put return value into eax */
1585 printf( PREFIX "CALL16_Ret_long:\n" );
1586 printf( "\tpushw %%dx\n" );
1587 printf( "\tpushw %%ax\n" );
1588 printf( "\tpopl %%eax\n" );
1589 printf( PREFIX "CALL16_Ret_word:\n" );
1591 /* Restore 32-bit segment registers */
1593 printf( "\tmovw $0x%04x,%%bx\n", WINE_DATA_SELECTOR );
1594 printf( "\tmovw %%bx,%%ds\n" );
1595 printf( "\tmovw %%bx,%%es\n" );
1596 printf( "\tmovw %%bx,%%ss\n" );
1598 /* Restore the 32-bit stack */
1600 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%esp\n" );
1601 printf( "\tpopl " PREFIX "IF1632_Saved32_esp\n" );
1603 /* Restore the 32-bit registers */
1605 printf( "\tpopl %%edi\n" );
1606 printf( "\tpopl %%esi\n" );
1607 printf( "\tpopl %%edx\n" );
1608 printf( "\tpopl %%ecx\n" );
1609 printf( "\tpopl %%ebx\n" );
1611 /* Return to caller */
1613 printf( "\tpopl %%ebp\n" );
1614 printf( "\tlret\n" );
1616 /* Declare the return address variables */
1618 printf( "\t.data\n" );
1619 printf( "\t.globl " PREFIX "CALL16_RetAddr_word\n" );
1620 printf( "\t.globl " PREFIX "CALL16_RetAddr_long\n" );
1621 printf( PREFIX "CALL16_RetAddr_word:\t.long 0\n" );
1622 printf( PREFIX "CALL16_RetAddr_long:\t.long 0\n" );
1623 printf( "\t.text\n" );
1627 static void usage(void)
1629 fprintf(stderr, "usage: build -spec SPECNAMES\n"
1630 " build -call32 FUNCTION_PROFILES\n"
1631 " build -call16 FUNCTION_PROFILES\n" );
1636 int main(int argc, char **argv)
1640 if (argc <= 2) usage();
1642 if (!strcmp( argv[1], "-spec16" ))
1644 for (i = 2; i < argc; i++) BuildSpec16Files( argv[i] );
1646 else if (!strcmp( argv[1], "-spec32" ))
1648 for (i = 2; i < argc; i++) BuildSpec32Files( argv[i] );
1650 else if (!strcmp( argv[1], "-call32" )) /* 32-bit callbacks */
1654 printf( "/* File generated automatically. Do not edit! */\n\n" );
1655 printf( "\t.text\n" );
1657 /* Build the 32-bit large stack callback */
1659 BuildCall32LargeStack();
1661 /* Build the callback functions */
1663 for (i = 2; i < argc; i++) BuildCall32Func( argv[i] );
1665 /* Output the argument debugging strings */
1669 printf( "/* Argument strings */\n" );
1670 for (i = 2; i < argc; i++)
1672 printf( "CALL32_Str_%s:\n", argv[i] );
1673 printf( "\t.ascii \"%s\\0\"\n", argv[i] + 5 );
1677 else if (!strcmp( argv[1], "-call16" )) /* 16-bit callbacks */
1681 printf( "/* File generated automatically. Do not edit! */\n\n" );
1682 printf( "\t.text\n" );
1683 printf( "\t.globl " PREFIX "CALL16_Start\n" );
1684 printf( PREFIX "CALL16_Start:\n" );
1686 /* Build the callback functions */
1688 for (i = 2; i < argc; i++) BuildCall16Func( argv[i] );
1690 /* Output the 16-bit return code */
1694 printf( "\t.globl " PREFIX "CALL16_End\n" );
1695 printf( PREFIX "CALL16_End:\n" );