2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
14 /* ELF symbols do not have an underscore in front */
21 #define TYPE_INVALID 0
25 #define TYPE_PASCAL_16 4
27 #define TYPE_REGISTER 6
32 #define MAX_ORDINALS 1299
34 /* Callback function used for stub functions */
35 #define STUB_CALLBACK "RELAY_Unimplemented"
37 typedef struct ordinal_definition_s
42 void *additional_data;
45 typedef struct ordinal_variable_definition_s
51 typedef struct ordinal_function_definition_s
55 char internal_name[80];
58 typedef struct ordinal_return_definition_s
64 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
66 char LowerDLLName[80];
67 char UpperDLLName[80];
72 char *ParseBuffer = NULL;
77 static int debugging = 1;
79 /* Offset of register relative to the end of the context struct */
80 #define CONTEXTOFFSET(reg) \
81 ((int)&(((struct sigcontext_struct *)1)->reg) - 1 \
82 - sizeof(struct sigcontext_struct))
84 static int IsNumberString(char *s)
93 static char *strlower(char *s)
97 for(p = s; *p != '\0'; p++)
103 static char *strupper(char *s)
107 for(p = s; *p != '\0'; p++)
113 static char * GetTokenInLine(void)
118 if (ParseNext != ParseBuffer)
120 if (ParseSaveChar == '\0')
122 *ParseNext = ParseSaveChar;
126 * Remove initial white space.
128 for (p = ParseNext; isspace(*p); p++)
131 if ((*p == '\0') || (*p == '#'))
138 if (*token != '(' && *token != ')')
139 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
149 static char * GetToken(void)
153 if (ParseBuffer == NULL)
155 ParseBuffer = malloc(512);
156 ParseNext = ParseBuffer;
160 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
162 if (ParseBuffer[0] != '#')
167 while ((token = GetTokenInLine()) == NULL)
169 ParseNext = ParseBuffer;
173 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
175 if (ParseBuffer[0] != '#')
183 static int ParseVariable(int ordinal, int type)
187 char export_name[80];
192 int value_array_size;
194 strcpy(export_name, GetToken());
199 fprintf(stderr, "%d: Expected '(' got '%s'\n", Line, token);
204 value_array_size = 25;
205 value_array = malloc(sizeof(*value_array) * value_array_size);
207 while ((token = GetToken()) != NULL)
212 value_array[n_values++] = strtol(token, &endptr, 0);
213 if (n_values == value_array_size)
215 value_array_size += 25;
216 value_array = realloc(value_array,
217 sizeof(*value_array) * value_array_size);
220 if (endptr == NULL || *endptr != '\0')
222 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
230 fprintf(stderr, "%d: End of file in variable declaration\n", Line);
234 if (ordinal >= MAX_ORDINALS)
236 fprintf(stderr, "%d: Ordinal number too large\n", Line);
240 odp = &OrdinalDefinitions[ordinal];
242 strcpy(odp->export_name, export_name);
244 vdp = malloc(sizeof(*vdp));
245 odp->additional_data = vdp;
247 vdp->n_values = n_values;
248 vdp->values = realloc(value_array, sizeof(*value_array) * n_values);
253 static int ParseExportFunction(int ordinal, int type)
260 odp = &OrdinalDefinitions[ordinal];
261 strcpy(odp->export_name, GetToken());
263 fdp = malloc(sizeof(*fdp));
264 odp->additional_data = fdp;
269 fprintf(stderr, "%d: Expected '(' got '%s'\n", Line, token);
273 for (i = 0; i < 16; i++)
279 if (!strcmp(token, "byte") || !strcmp(token, "word"))
280 fdp->arg_types[i] = 'w';
281 else if (!strcmp(token, "s_byte") || !strcmp(token, "s_word"))
282 fdp->arg_types[i] = 's';
283 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
284 fdp->arg_types[i] = 'l';
285 else if (!strcmp(token, "ptr"))
286 fdp->arg_types[i] = 'p';
289 fprintf(stderr, "%d: Unknown variable type '%s'\n", Line, token);
293 fdp->arg_types[i] = '\0';
295 if ((type == TYPE_REGISTER) && (i > 0))
297 fprintf( stderr, "%d: Register function can't have arguments\n", Line);
301 strcpy(fdp->internal_name, GetToken());
305 static int ParseEquate(int ordinal)
312 odp = &OrdinalDefinitions[ordinal];
313 strcpy(odp->export_name, GetToken());
316 value = strtol(token, &endptr, 0);
317 if (endptr == NULL || *endptr != '\0')
319 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
324 odp->type = TYPE_ABS;
325 odp->additional_data = (void *) value;
330 static int ParseReturn(int ordinal)
337 rdp = malloc(sizeof(*rdp));
339 odp = &OrdinalDefinitions[ordinal];
340 strcpy(odp->export_name, GetToken());
341 odp->type = TYPE_RETURN;
342 odp->additional_data = rdp;
345 rdp->arg_size = strtol(token, &endptr, 0);
346 if (endptr == NULL || *endptr != '\0')
348 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
354 rdp->ret_value = strtol(token, &endptr, 0);
355 if (endptr == NULL || *endptr != '\0')
357 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
366 static int ParseStub( int ordinal )
371 odp = &OrdinalDefinitions[ordinal];
372 strcpy( odp->export_name, GetToken() );
373 odp->type = TYPE_STUB;
374 fdp = malloc(sizeof(*fdp));
375 odp->additional_data = fdp;
376 fdp->arg_types[0] = '\0';
377 strcpy( fdp->internal_name, STUB_CALLBACK );
382 static int ParseOrdinal(int ordinal)
386 if (ordinal >= MAX_ORDINALS)
388 fprintf(stderr, "%d: Ordinal number too large\n", Line);
391 if (ordinal > Limit) Limit = ordinal;
396 fprintf(stderr, "%d: Expected type after ordinal\n", Line);
400 if (strcmp(token, "byte") == 0)
401 return ParseVariable(ordinal, TYPE_BYTE);
402 else if (strcmp(token, "word") == 0)
403 return ParseVariable(ordinal, TYPE_WORD);
404 else if (strcmp(token, "long") == 0)
405 return ParseVariable(ordinal, TYPE_LONG);
406 else if (strcmp(token, "p") == 0)
407 return ParseExportFunction(ordinal, TYPE_PASCAL);
408 else if (strcmp(token, "pascal") == 0)
409 return ParseExportFunction(ordinal, TYPE_PASCAL);
410 else if (strcmp(token, "pascal16") == 0)
411 return ParseExportFunction(ordinal, TYPE_PASCAL_16);
412 else if (strcmp(token, "register") == 0)
413 return ParseExportFunction(ordinal, TYPE_REGISTER);
414 else if (strcmp(token, "equate") == 0)
415 return ParseEquate(ordinal);
416 else if (strcmp(token, "return") == 0)
417 return ParseReturn(ordinal);
418 else if (strcmp(token, "stub") == 0)
419 return ParseStub(ordinal);
423 "%d: Expected type after ordinal, found '%s' instead\n",
429 static int ParseTopLevel(void)
433 while ((token = GetToken()) != NULL)
435 if (strcmp(token, "name") == 0)
437 strcpy(LowerDLLName, GetToken());
438 strlower(LowerDLLName);
440 strcpy(UpperDLLName, LowerDLLName);
441 strupper(UpperDLLName);
443 else if (strcmp(token, "id") == 0)
446 if (!IsNumberString(token))
448 fprintf(stderr, "%d: Expected number after id\n", Line);
454 else if (IsNumberString(token))
459 ordinal = atoi(token);
460 if ((rv = ParseOrdinal(ordinal)) < 0)
466 "%d: Expected name, id, length or ordinal\n", Line);
475 static int OutputVariableCode(FILE *fp, char *storage, ORDDEF *odp)
480 vdp = odp->additional_data;
481 fprintf( fp, "\t.data\n" );
482 for (i = 0; i < vdp->n_values; i++)
485 fprintf(fp, "\t%s\t", storage);
487 fprintf(fp, "%d", vdp->values[i]);
489 if ((i & 7) == 7 || i == vdp->n_values - 1)
495 fprintf( fp, "\t.text\n" );
496 return vdp->n_values;
500 /*******************************************************************
503 * Build the in-memory representation of the module, and dump it
504 * as a byte stream into the assembly code.
506 static void BuildModule( FILE *fp, int max_code_offset, int max_data_offset )
512 SEGTABLEENTRY *pSegment;
513 LOADEDFILEINFO *pFileInfo;
519 * LOADEDFILEINFO File information
520 * SEGTABLEENTRY Segment 1 (code)
521 * SEGTABLEENTRY Segment 2 (data)
522 * WORD[2] Resource table (empty)
523 * BYTE[2] Imported names (empty)
524 * BYTE[n] Resident names table
525 * BYTE[n] Entry table
528 buffer = malloc( 0x10000 );
530 pModule = (NE_MODULE *)buffer;
531 pModule->magic = NE_SIGNATURE;
534 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_LIBMODULE;
536 pModule->heap_size = 0xffff;
537 pModule->stack_size = 0;
542 pModule->seg_count = 2;
543 pModule->modref_count = 0;
544 pModule->nrname_size = 0;
545 pModule->modref_table = 0;
546 pModule->nrname_fpos = 0;
547 pModule->moveable_entries = 0;
548 pModule->alignment = 0;
549 pModule->truetype = 0;
550 pModule->os_flags = NE_OSFLAGS_WINDOWS;
551 pModule->misc_flags = 0;
552 pModule->reserved = 0;
553 pModule->nrname_handle = 0;
554 pModule->min_swap_area = 0;
555 pModule->expected_version = 0x030a;
557 /* File information */
559 pFileInfo = (LOADEDFILEINFO *)(pModule + 1);
560 pModule->fileinfo = (int)pFileInfo - (int)pModule;
561 pFileInfo->length = sizeof(LOADEDFILEINFO) + strlen(UpperDLLName) + 3;
562 pFileInfo->fixed_media = 0;
563 pFileInfo->error = 0;
566 sprintf( pFileInfo->filename, "%s.DLL", UpperDLLName );
567 pstr = (char *)pFileInfo + pFileInfo->length + 1;
571 pSegment = (SEGTABLEENTRY *)pstr;
572 pModule->seg_table = (int)pSegment - (int)pModule;
573 pSegment->filepos = 0;
574 pSegment->size = max_code_offset;
576 pSegment->minsize = max_code_offset;
577 pSegment->selector = 0;
580 pModule->dgroup_entry = (int)pSegment - (int)pModule;
581 pSegment->filepos = 0;
582 pSegment->size = max_data_offset;
583 pSegment->flags = NE_SEGFLAGS_DATA;
584 pSegment->minsize = max_data_offset;
585 pSegment->selector = 0;
590 pword = (WORD *)pSegment;
591 pModule->res_table = (int)pword - (int)pModule;
595 /* Imported names table */
597 pstr = (char *)pword;
598 pModule->import_table = (int)pstr - (int)pModule;
602 /* Resident names table */
604 pModule->name_table = (int)pstr - (int)pModule;
605 /* First entry is module name */
606 *pstr = strlen(UpperDLLName );
607 strcpy( pstr + 1, UpperDLLName );
610 pstr += sizeof(WORD);
611 /* Store all ordinals */
612 odp = OrdinalDefinitions + 1;
613 for (i = 1; i <= Limit; i++, odp++)
615 if (!odp->export_name[0]) continue;
616 *pstr = strlen( odp->export_name );
617 strcpy( pstr + 1, odp->export_name );
618 strupper( pstr + 1 );
621 pstr += sizeof(WORD);
627 pModule->entry_table = (int)pstr - (int)pModule;
629 odp = OrdinalDefinitions + 1;
630 for (i = 1; i <= Limit; i++, odp++)
637 selector = 0; /* Invalid selector */
645 selector = 1; /* Code selector */
651 selector = 2; /* Data selector */
655 selector = 0xfe; /* Constant selector */
659 /* create a new bundle if necessary */
660 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
664 bundle[1] = selector;
672 *(WORD *)pstr = odp->offset;
673 pstr += sizeof(WORD);
678 /* Dump the module content */
680 fprintf( fp, "\t.data\n" );
681 fprintf( fp, "\t.globl " PREFIX "%s_Module_Start\n", UpperDLLName );
682 fprintf( fp, PREFIX "%s_Module_Start:\n", UpperDLLName );
683 size = (int)pstr - (int)pModule;
684 for (i = 0, pstr = buffer; i < size; i++, pstr++)
686 if (!(i & 7)) fprintf( fp, "\t.byte " );
687 fprintf( fp, "%d%c", *pstr, ((i & 7) != 7) ? ',' : '\n' );
689 if (i & 7) fprintf( fp, "0\n" );
690 fprintf( fp, "\t.globl " PREFIX "%s_Module_End\n", UpperDLLName );
691 fprintf( fp, PREFIX "%s_Module_End:\n", UpperDLLName );
695 static void BuildSpecFiles( char *specname)
703 int code_offset, data_offset;
705 SpecFp = fopen( specname, "r");
708 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
714 sprintf(filename, "dll_%s.S", LowerDLLName);
715 fp = fopen(filename, "w");
716 fprintf( fp, "/* File generated automatically; do not edit! */\n" );
717 fprintf( fp, "\t.data\n" );
718 fprintf( fp, "\t.globl " PREFIX "%s_Data_Start\n", UpperDLLName );
719 fprintf( fp, PREFIX "%s_Data_Start:\n", UpperDLLName );
720 fprintf( fp, "\t.word 0,0,0,0,0,0,0,0\n" );
722 fprintf( fp, "\t.text\n" );
723 fprintf( fp, "\t.globl " PREFIX "%s_Code_Start\n", UpperDLLName );
724 fprintf( fp, PREFIX "%s_Code_Start:\n", UpperDLLName );
727 odp = OrdinalDefinitions;
728 for (i = 0; i <= Limit; i++, odp++)
730 fdp = odp->additional_data;
731 rdp = odp->additional_data;
736 odp->offset = 0xffff;
740 odp->offset = (int)odp->additional_data & 0xffff;
744 fprintf( fp, "/* %s.%d */\n", UpperDLLName, i);
745 odp->offset = data_offset;
746 data_offset += OutputVariableCode(fp, ".byte", odp);
750 fprintf( fp, "/* %s.%d */\n", UpperDLLName, i);
751 odp->offset = data_offset;
752 data_offset += 2 * OutputVariableCode(fp, ".word", odp);
756 fprintf( fp, "/* %s.%d */\n", UpperDLLName, i);
757 odp->offset = data_offset;
758 data_offset += 4 * OutputVariableCode(fp, ".long", odp);
762 fprintf( fp, "/* %s.%d */\n", UpperDLLName, i);
763 fprintf( fp, "\tmovw $%d,%%ax\n", rdp->ret_value & 0xffff );
764 fprintf( fp, "\tmovw $%d,%%dx\n", (rdp->ret_value >> 16) & 0xffff);
765 fprintf(fp, "\t.byte 0x66\n");
766 if (rdp->arg_size != 0)
767 fprintf(fp, "\tlret $%d\n", rdp->arg_size);
769 fprintf(fp, "\tlret\n");
770 odp->offset = code_offset;
771 code_offset += 10; /* Assembly code is 10 bytes long */
772 if (rdp->arg_size != 0) code_offset += 2;
779 fprintf( fp, "/* %s.%d */\n", UpperDLLName, i);
780 fprintf(fp, "\tpushw %%bp\n" );
781 fprintf(fp, "\tpushl $0x%08x\n", (DLLId << 16) | i);
782 fprintf(fp, "\tpushl $" PREFIX "%s\n", fdp->internal_name );
783 fprintf(fp, "\tljmp $0x%04x, $" PREFIX "CallTo32_%s_%s\n\n",
785 (odp->type == TYPE_REGISTER) ? "regs" :
786 (odp->type == TYPE_PASCAL) ? "long" : "word",
788 odp->offset = code_offset;
789 code_offset += 19; /* Assembly code is 19 bytes long */
793 fprintf( stderr, "build: Unknown function type; please report.\n");
798 if (!code_offset) /* Make sure the code segment is not empty */
800 fprintf( fp, "\t.byte 0\n" );
804 BuildModule( fp, code_offset, data_offset );
808 sprintf(filename, "tab_%s.c", LowerDLLName);
809 fp = fopen(filename, "w");
811 fprintf( fp, "/* File generated automatically; do not edit! */\n\n" );
812 fprintf( fp, "#include \"dlls.h\"\n\n" );
813 fprintf( fp, "static struct dll_table_entry_s %s_table_entries[%d] =\n{\n",
814 UpperDLLName, Limit + 1);
815 odp = OrdinalDefinitions;
816 for (i = 0; i <= Limit; i++, odp++)
823 selector = 0; /* Invalid selector */
831 selector = 1; /* Code selector */
837 selector = 2; /* Data selector */
841 selector = 0xff; /* Constant selector */
845 /* fprintf(fp, " { %d, %d, ", selector, odp->offset ); */
846 fprintf( fp, " { " );
847 fprintf(fp, "\"%s\" ", odp->export_name);
851 fprintf(fp, "}, \n");
854 fprintf(fp, "};\n\n");
856 fprintf( fp, "extern BYTE %s_Code_Start[];\n", UpperDLLName );
857 fprintf( fp, "extern BYTE %s_Data_Start[];\n", UpperDLLName );
858 fprintf( fp, "extern BYTE %s_Module_Start[];\n", UpperDLLName );
859 fprintf( fp, "extern BYTE %s_Module_End[];\n\n", UpperDLLName );
860 fprintf( fp, "struct dll_table_s %s_table =\n{\n", UpperDLLName );
861 fprintf( fp, " %s_table_entries, %d, %d,\n",
862 UpperDLLName, Limit + 1, DLLId );
863 fprintf( fp, " %s_Code_Start, %s_Data_Start,\n",
864 UpperDLLName, UpperDLLName );
865 fprintf( fp, " %s_Module_Start, %s_Module_End\n};\n",
866 UpperDLLName, UpperDLLName );
871 /*******************************************************************
874 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
875 * The 16-bit stack layout is:
883 static int TransferArgs16To32( char *args )
889 printf( "\tpushl %%ebx\n" );
891 /* Get the 32-bit stack pointer */
893 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
895 /* Copy the arguments */
897 pos16 = 6; /* skip bp and return address */
900 for (i = strlen(args); i > 0; i--)
906 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
907 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
911 case 's': /* s_word */
912 printf( "\tmovswl %d(%%ebp),%%eax\n", pos16 );
913 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
918 printf( "\tmovl %d(%%ebp),%%eax\n", pos16 );
919 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
924 /* Get the selector */
925 printf( "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
926 /* Get the selector base */
927 printf( "\tandl $0xfff8,%%eax\n" );
928 printf( "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
929 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
931 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
932 printf( "\taddl %%eax,%d(%%ebx)\n", pos32 );
937 fprintf( stderr, "Unknown arg type '%c'\n", args[i-1] );
943 printf( "\tpopl %%ebx\n" );
945 return pos16 - 6; /* Return the size of the 16-bit args */
949 /*******************************************************************
952 * Build the context structure on the 32-bit stack.
953 * The only valid registers in the context structure are:
954 * eax, ebx, ecx, edx, esi, edi, ds, es, (some of the) flags
956 static void BuildContext(void)
960 printf( "\tpushl %%ebx\n" );
962 /* Get the 32-bit stack pointer */
964 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
966 /* Store the registers */
968 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_ebx) ); /* Get ebx from stack */
969 printf( "\tmovl %%eax,%d(%%ebx)\n", CONTEXTOFFSET(sc_eax) );
970 printf( "\tmovl %%ecx,%d(%%ebx)\n", CONTEXTOFFSET(sc_ecx) );
971 printf( "\tmovl %%edx,%d(%%ebx)\n", CONTEXTOFFSET(sc_edx) );
972 printf( "\tmovl %%esi,%d(%%ebx)\n", CONTEXTOFFSET(sc_esi) );
973 printf( "\tmovl %%edi,%d(%%ebx)\n", CONTEXTOFFSET(sc_edi) );
974 printf( "\tpushw %%es\n" );
975 printf( "\tpopw %d(%%ebx)\n", CONTEXTOFFSET(sc_es) );
976 printf( "\tmovw -10(%%ebp),%%ax\n" ); /* Get saved ds from stack */
977 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(sc_ds) );
978 printf( "\tpushfl\n" );
980 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_eflags) );
982 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_efl) );
987 /*******************************************************************
990 * Restore the registers from the context structure
992 static void RestoreContext(void)
994 /* Get the 32-bit stack pointer */
996 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
998 /* Restore the registers */
1000 printf( "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(sc_ecx) );
1001 printf( "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(sc_edx) );
1002 printf( "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(sc_esi) );
1003 printf( "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(sc_edi) );
1004 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(sc_es) );
1005 printf( "\tpopw %%es\n" );
1006 printf( "\tpopw %%ax\n" ); /* Remove old ds from the stack */
1007 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(sc_ds) ); /* Push new ds */
1009 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(sc_eflags) );
1011 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(sc_efl) );
1013 printf( "\tpopfl\n" );
1014 printf( "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(sc_eax) );
1015 printf( "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(sc_ebx) );
1019 /*******************************************************************
1022 * Build a 32-bit callback function. The syntax of the function
1023 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1024 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1025 * 'l'=long, 'p'=pointer).
1027 * Stack layout upon entry to the callback function:
1029 * (sp+14) first 16-bit arg
1033 * (sp+4) dll_id+ordinal (long)
1034 * (sp) entrypoint (long)
1037 static void BuildCall32Func( char *profile )
1042 char *args = profile + 5;
1044 /* Parse function type */
1046 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1047 else if (!strncmp( "regs_", profile, 5 )) reg_func = 1;
1048 else if (strncmp( "long_", profile, 5 ))
1050 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1054 /* Function header */
1056 printf( "/**********\n" );
1057 printf( " * " PREFIX "CallTo32_%s\n", profile );
1058 printf( " **********/\n" );
1059 printf( "\t.align 4\n" );
1060 printf( "\t.globl " PREFIX "CallTo32_%s\n\n", profile );
1061 printf( PREFIX "CallTo32_%s:\n", profile );
1063 /* Setup bp to point to its copy on the stack */
1065 printf( "\tmovzwl %%sp,%%ebp\n" );
1066 printf( "\taddw $8,%%bp\n" );
1068 /* Save 16-bit ds */
1070 printf( "\tpushw %%ds\n" );
1072 /* Restore 32-bit ds */
1074 printf( "\tpushw $0x%04x\n", WINE_DATA_SELECTOR );
1075 printf( "\tpopw %%ds\n" );
1077 /* Save the 16-bit stack */
1079 printf( "\tpushw " PREFIX "IF1632_Saved16_sp\n" );
1080 printf( "\tpushw " PREFIX "IF1632_Saved16_ss\n" );
1081 printf( "\tmovw %%ss," PREFIX "IF1632_Saved16_ss\n" );
1082 printf( "\tmovw %%sp," PREFIX "IF1632_Saved16_sp\n" );
1084 /* Transfer the arguments */
1086 if (reg_func) BuildContext();
1087 else if (*args) argsize = TransferArgs16To32( args );
1089 /* Get the address of the API function */
1091 printf( "\tmovl -8(%%ebp),%%eax\n" );
1095 printf( "\tpushw %%ds\n" );
1096 printf( "\tpopw %%es\n" );
1098 /* Switch to the 32-bit stack */
1100 printf( "\tpushw %%ds\n" );
1101 printf( "\tpopw %%ss\n" );
1102 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%esp\n" );
1104 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1106 printf( "\tmovl %%esp,%%ebp\n" );
1107 printf( "\taddl $24,%%ebp\n" );
1110 printf( "\tsubl $%d,%%esp\n", sizeof(struct sigcontext_struct) );
1112 printf( "\tsubl $%d,%%esp\n", 4 * strlen(args) );
1114 /* Call the entry point */
1118 printf( "\tpushl %%eax\n" );
1119 printf( "\tpushl $CALL32_Str_%s\n", profile );
1120 printf( "\tcall " PREFIX "RELAY_DebugCall32\n" );
1121 printf( "\tpopl %%eax\n" );
1122 printf( "\tpopl %%eax\n" );
1125 printf( "\tcall %%eax\n" );
1129 printf( "\tpushl %%eax\n" );
1130 printf( "\tpushl $%d\n", short_ret );
1131 printf( "\tcall " PREFIX "RELAY_DebugReturn\n" );
1132 printf( "\tpopl %%eax\n" );
1133 printf( "\tpopl %%eax\n" );
1137 printf( "\taddl $%d,%%esp\n", sizeof(struct sigcontext_struct) );
1139 printf( "\taddl $%d,%%esp\n", 4 * strlen(args) );
1141 /* Restore the 16-bit stack */
1143 printf( "\tmovw " PREFIX "IF1632_Saved16_ss,%%ss\n" );
1144 printf( "\tmovw " PREFIX "IF1632_Saved16_sp,%%sp\n" );
1145 printf( "\tpopw " PREFIX "IF1632_Saved16_ss\n" );
1146 printf( "\tpopw " PREFIX "IF1632_Saved16_sp\n" );
1150 /* Restore registers from the context structure */
1153 else /* Store the return value in dx:ax if needed */
1157 printf( "\tpushl %%eax\n" );
1158 printf( "\tpopw %%dx\n" );
1159 printf( "\tpopw %%dx\n" );
1163 /* Restore ds and bp */
1165 printf( "\tpopw %%ds\n" );
1166 printf( "\tpopl %%ebp\n" ); /* Remove entry point address */
1167 printf( "\tpopl %%ebp\n" ); /* Remove DLL id and ordinal */
1168 printf( "\tpopw %%bp\n" );
1170 /* Remove the arguments and return */
1174 printf( "\t.byte 0x66\n" );
1175 printf( "\tlret $%d\n", argsize );
1179 printf( "\t.byte 0x66\n" );
1180 printf( "\tlret\n" );
1185 /*******************************************************************
1188 * Build a 16-bit callback function.
1190 * Stack frame of the callback function:
1194 * (ebp+16) 16-bit ds
1195 * (ebp+12) func to call
1196 * (ebp+8) code selector
1197 * (ebp+4) return address
1198 * (ebp) previous ebp
1200 * Prototypes for the CallTo16 functions:
1201 * extern WORD CallTo16_word_xxx( FARPROC func, WORD ds, args... );
1202 * extern LONG CallTo16_long_xxx( FARPROC func, WORD ds, args... );
1203 * extern void CallTo16_regs_( FARPROC func, WORD ds, WORD es, WORD bp,
1204 * WORD ax, WORD bx, WORD cx, WORD dx,
1205 * WORD si, WORD di );
1207 static void BuildCall16Func( char *profile )
1211 char *args = profile + 5;
1213 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1214 else if (!strncmp( "regs_", profile, 5 )) reg_func = short_ret = 1;
1215 else if (strncmp( "long_", profile, 5 ))
1217 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1221 /* Function header */
1223 printf( "/**********\n" );
1224 printf( " * " PREFIX "CallTo16_%s\n", profile );
1225 printf( " **********/\n" );
1226 printf( "\t.align 4\n" );
1227 printf( "\t.globl " PREFIX "CallTo16_%s\n\n", profile );
1228 printf( PREFIX "CallTo16_%s:\n", profile );
1230 /* Push code selector before return address to simulate a lcall */
1232 printf( "\tpopl %%eax\n" );
1233 printf( "\tpushl $0x%04x\n", WINE_CODE_SELECTOR );
1234 printf( "\tpushl %%eax\n" );
1238 printf( "\tpushl %%ebp\n" );
1239 printf( "\tmovl %%esp,%%ebp\n" );
1241 /* Save the 32-bit registers */
1243 printf( "\tpushl %%ebx\n" );
1244 printf( "\tpushl %%ecx\n" );
1245 printf( "\tpushl %%edx\n" );
1246 printf( "\tpushl %%esi\n" );
1247 printf( "\tpushl %%edi\n" );
1249 /* Save the 32-bit stack */
1251 printf( "\tpushl " PREFIX "IF1632_Saved32_esp\n" );
1252 printf( "\tmovl %%esp," PREFIX "IF1632_Saved32_esp\n" );
1253 printf( "\tmovl %%ebp,%%ebx\n" );
1255 /* Print debugging info */
1259 /* Push the address of the first argument */
1260 printf( "\tmovl %%ebx,%%eax\n" );
1261 printf( "\taddl $12,%%eax\n" );
1262 printf( "\tpushl $%d\n", reg_func ? 7 : strlen(args) );
1263 printf( "\tpushl %%eax\n" );
1264 printf( "\tcall " PREFIX "RELAY_DebugCall16\n" );
1265 printf( "\tpopl %%eax\n" );
1266 printf( "\tpopl %%eax\n" );
1269 /* Switch to the 16-bit stack */
1271 printf( "\tmovw " PREFIX "IF1632_Saved16_ss,%%ss\n" );
1272 printf( "\tmovw " PREFIX "IF1632_Saved16_sp,%%sp\n" );
1274 /* Transfer the arguments */
1278 /* Get the registers. ebx is handled later on. */
1279 printf( "\tpushw 20(%%ebx)\n" );
1280 printf( "\tpopw %%es\n" );
1281 printf( "\tmovl 24(%%ebx),%%ebp\n" );
1282 printf( "\tmovl 28(%%ebx),%%eax\n" );
1283 printf( "\tmovl 36(%%ebx),%%ecx\n" );
1284 printf( "\tmovl 40(%%ebx),%%edx\n" );
1285 printf( "\tmovl 44(%%ebx),%%esi\n" );
1286 printf( "\tmovl 48(%%ebx),%%edi\n" );
1288 else /* not a register function */
1290 int pos = 20; /* first argument position */
1292 /* Make %bp point to the previous stackframe (built by CallTo32) */
1293 printf( "\tmovw %%sp,%%bp\n" );
1294 printf( "\taddw $16,%%bp\n" );
1300 case 'w': /* word */
1301 printf( "\tpushw %d(%%ebx)\n", pos );
1303 case 'l': /* long */
1304 printf( "\tpushl %d(%%ebx)\n", pos );
1311 /* Push the return address */
1313 printf( "\tpushl " PREFIX "CALL16_RetAddr_%s\n",
1314 short_ret ? "word" : "long" );
1316 /* Push the called routine address */
1318 printf( "\tpushl 12(%%ebx)\n" );
1320 /* Get the 16-bit ds */
1321 /* FIXME: this shouldn't be necessary if function prologs fixup worked. */
1323 printf( "\tpushw 16(%%ebx)\n" );
1324 printf( "\tpopw %%ds\n" );
1328 /* Retrieve ebx from the 32-bit stack */
1329 printf( "\tmovl %%fs:28(%%ebx),%%ebx\n" );
1333 /* Set ax equal to ds for window procedures */
1334 printf( "\tmovw %%ds,%%ax\n" );
1337 /* Jump to the called routine */
1339 printf( "\t.byte 0x66\n" );
1340 printf( "\tlret\n" );
1344 /*******************************************************************
1347 * Build the return code for 16-bit callbacks
1349 static void BuildRet16Func()
1351 printf( "\t.globl " PREFIX "CALL16_Ret_word\n" );
1352 printf( "\t.globl " PREFIX "CALL16_Ret_long\n" );
1354 /* Put return value into eax */
1356 printf( PREFIX "CALL16_Ret_long:\n" );
1357 printf( "\tpushw %%dx\n" );
1358 printf( "\tpushw %%ax\n" );
1359 printf( "\tpopl %%eax\n" );
1360 printf( PREFIX "CALL16_Ret_word:\n" );
1362 /* Restore 32-bit segment registers */
1364 printf( "\tmovw $0x%04x,%%bx\n", WINE_DATA_SELECTOR );
1365 printf( "\tmovw %%bx,%%ds\n" );
1366 printf( "\tmovw %%bx,%%es\n" );
1367 printf( "\tmovw %%bx,%%ss\n" );
1369 /* Restore the 32-bit stack */
1371 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%esp\n" );
1372 printf( "\tpopl " PREFIX "IF1632_Saved32_esp\n" );
1374 /* Restore the 32-bit registers */
1376 printf( "\tpopl %%edi\n" );
1377 printf( "\tpopl %%esi\n" );
1378 printf( "\tpopl %%edx\n" );
1379 printf( "\tpopl %%ecx\n" );
1380 printf( "\tpopl %%ebx\n" );
1382 /* Return to caller */
1384 printf( "\tpopl %%ebp\n" );
1385 printf( "\tlret\n" );
1387 /* Declare the return address variables */
1389 printf( "\t.data\n" );
1390 printf( "\t.globl " PREFIX "CALL16_RetAddr_word\n" );
1391 printf( "\t.globl " PREFIX "CALL16_RetAddr_long\n" );
1392 printf( PREFIX "CALL16_RetAddr_word:\t.long 0\n" );
1393 printf( PREFIX "CALL16_RetAddr_long:\t.long 0\n" );
1394 printf( "\t.text\n" );
1398 static void usage(void)
1400 fprintf(stderr, "usage: build -spec SPECNAMES\n"
1401 " build -call32 FUNCTION_PROFILES\n"
1402 " build -call16 FUNCTION_PROFILES\n" );
1407 int main(int argc, char **argv)
1411 if (argc <= 2) usage();
1413 if (!strcmp( argv[1], "-spec" ))
1415 for (i = 2; i < argc; i++) BuildSpecFiles( argv[i] );
1417 else if (!strcmp( argv[1], "-call32" )) /* 32-bit callbacks */
1421 printf( "/* File generated automatically. Do no edit! */\n\n" );
1422 printf( "\t.text\n" );
1424 /* Build the callback functions */
1426 for (i = 2; i < argc; i++) BuildCall32Func( argv[i] );
1428 /* Output the argument debugging strings */
1432 printf( "/* Argument strings */\n" );
1433 for (i = 2; i < argc; i++)
1435 printf( "CALL32_Str_%s:\n", argv[i] );
1436 printf( "\t.ascii \"%s\\0\"\n", argv[i] + 5 );
1440 else if (!strcmp( argv[1], "-call16" )) /* 16-bit callbacks */
1444 printf( "/* File generated automatically. Do no edit! */\n\n" );
1445 printf( "\t.text\n" );
1446 printf( "\t.globl " PREFIX "CALL16_Start\n" );
1447 printf( PREFIX "CALL16_Start:\n" );
1449 /* Build the callback functions */
1451 for (i = 2; i < argc; i++) BuildCall16Func( argv[i] );
1453 /* Output the 16-bit return code */
1457 printf( "\t.globl " PREFIX "CALL16_End\n" );
1458 printf( PREFIX "CALL16_End:\n" );