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 */
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 int IsNumberString(char *s
)
96 static char *strlower(char *s
)
100 for(p
= s
; *p
!= '\0'; p
++)
106 static char *strupper(char *s
)
110 for(p
= s
; *p
!= '\0'; p
++)
116 static char * GetTokenInLine(void)
121 if (ParseNext
!= ParseBuffer
)
123 if (ParseSaveChar
== '\0')
125 *ParseNext
= ParseSaveChar
;
129 * Remove initial white space.
131 for (p
= ParseNext
; isspace(*p
); p
++)
134 if ((*p
== '\0') || (*p
== '#'))
141 if (*token
!= '(' && *token
!= ')')
142 while (*p
!= '\0' && *p
!= '(' && *p
!= ')' && !isspace(*p
))
152 static char * GetToken(void)
156 if (ParseBuffer
== NULL
)
158 ParseBuffer
= malloc(512);
159 ParseNext
= ParseBuffer
;
163 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
165 if (ParseBuffer
[0] != '#')
170 while ((token
= GetTokenInLine()) == NULL
)
172 ParseNext
= ParseBuffer
;
176 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
178 if (ParseBuffer
[0] != '#')
186 static int ParseVariable(int ordinal
, int type
)
190 char export_name
[80];
195 int value_array_size
;
197 strcpy(export_name
, GetToken());
202 fprintf(stderr
, "%d: Expected '(' got '%s'\n", Line
, token
);
207 value_array_size
= 25;
208 value_array
= malloc(sizeof(*value_array
) * value_array_size
);
210 while ((token
= GetToken()) != NULL
)
215 value_array
[n_values
++] = strtol(token
, &endptr
, 0);
216 if (n_values
== value_array_size
)
218 value_array_size
+= 25;
219 value_array
= realloc(value_array
,
220 sizeof(*value_array
) * value_array_size
);
223 if (endptr
== NULL
|| *endptr
!= '\0')
225 fprintf(stderr
, "%d: Expected number value, got '%s'\n", Line
,
233 fprintf(stderr
, "%d: End of file in variable declaration\n", Line
);
237 if (ordinal
>= MAX_ORDINALS
)
239 fprintf(stderr
, "%d: Ordinal number too large\n", Line
);
243 odp
= &OrdinalDefinitions
[ordinal
];
245 strcpy(odp
->export_name
, export_name
);
247 vdp
= malloc(sizeof(*vdp
));
248 odp
->additional_data
= vdp
;
250 vdp
->n_values
= n_values
;
251 vdp
->values
= realloc(value_array
, sizeof(*value_array
) * n_values
);
256 static int ParseExportFunction(int ordinal
, int type
)
263 odp
= &OrdinalDefinitions
[ordinal
];
264 strcpy(odp
->export_name
, GetToken());
266 fdp
= malloc(sizeof(*fdp
));
267 odp
->additional_data
= fdp
;
272 fprintf(stderr
, "%d: Expected '(' got '%s'\n", Line
, token
);
276 for (i
= 0; i
< 16; i
++)
282 if (!strcmp(token
, "byte") || !strcmp(token
, "word"))
283 fdp
->arg_types
[i
] = 'w';
284 else if (!strcmp(token
, "s_byte") || !strcmp(token
, "s_word"))
285 fdp
->arg_types
[i
] = 's';
286 else if (!strcmp(token
, "long") || !strcmp(token
, "segptr"))
287 fdp
->arg_types
[i
] = 'l';
288 else if (!strcmp(token
, "ptr"))
289 fdp
->arg_types
[i
] = 'p';
292 fprintf(stderr
, "%d: Unknown variable type '%s'\n", Line
, token
);
296 fdp
->arg_types
[i
] = '\0';
298 strcpy(fdp
->internal_name
, GetToken());
302 static int ParseEquate(int ordinal
)
309 odp
= &OrdinalDefinitions
[ordinal
];
310 strcpy(odp
->export_name
, GetToken());
313 value
= strtol(token
, &endptr
, 0);
314 if (endptr
== NULL
|| *endptr
!= '\0')
316 fprintf(stderr
, "%d: Expected number value, got '%s'\n", Line
,
321 odp
->type
= TYPE_ABS
;
322 odp
->additional_data
= (void *) value
;
327 static int ParseReturn(int ordinal
)
334 rdp
= malloc(sizeof(*rdp
));
336 odp
= &OrdinalDefinitions
[ordinal
];
337 strcpy(odp
->export_name
, GetToken());
338 odp
->type
= TYPE_RETURN
;
339 odp
->additional_data
= rdp
;
342 rdp
->arg_size
= strtol(token
, &endptr
, 0);
343 if (endptr
== NULL
|| *endptr
!= '\0')
345 fprintf(stderr
, "%d: Expected number value, got '%s'\n", Line
,
351 rdp
->ret_value
= strtol(token
, &endptr
, 0);
352 if (endptr
== NULL
|| *endptr
!= '\0')
354 fprintf(stderr
, "%d: Expected number value, got '%s'\n", Line
,
363 static int ParseStub( int ordinal
)
368 odp
= &OrdinalDefinitions
[ordinal
];
369 strcpy( odp
->export_name
, GetToken() );
370 odp
->type
= TYPE_STUB
;
371 fdp
= malloc(sizeof(*fdp
));
372 odp
->additional_data
= fdp
;
373 fdp
->arg_types
[0] = '\0';
374 strcpy( fdp
->internal_name
, STUB_CALLBACK
);
379 static int ParseOrdinal(int ordinal
)
383 if (ordinal
>= MAX_ORDINALS
)
385 fprintf(stderr
, "%d: Ordinal number too large\n", Line
);
388 if (ordinal
> Limit
) Limit
= ordinal
;
393 fprintf(stderr
, "%d: Expected type after ordinal\n", Line
);
397 if (strcmp(token
, "byte") == 0)
398 return ParseVariable(ordinal
, TYPE_BYTE
);
399 else if (strcmp(token
, "word") == 0)
400 return ParseVariable(ordinal
, TYPE_WORD
);
401 else if (strcmp(token
, "long") == 0)
402 return ParseVariable(ordinal
, TYPE_LONG
);
403 else if (strcmp(token
, "p") == 0)
404 return ParseExportFunction(ordinal
, TYPE_PASCAL
);
405 else if (strcmp(token
, "pascal") == 0)
406 return ParseExportFunction(ordinal
, TYPE_PASCAL
);
407 else if (strcmp(token
, "pascal16") == 0)
408 return ParseExportFunction(ordinal
, TYPE_PASCAL_16
);
409 else if (strcmp(token
, "register") == 0)
410 return ParseExportFunction(ordinal
, TYPE_REGISTER
);
411 else if (strcmp(token
, "stdcall") == 0)
412 return ParseExportFunction(ordinal
, TYPE_STDCALL
);
413 else if (strcmp(token
, "equate") == 0)
414 return ParseEquate(ordinal
);
415 else if (strcmp(token
, "return") == 0)
416 return ParseReturn(ordinal
);
417 else if (strcmp(token
, "stub") == 0)
418 return ParseStub(ordinal
);
422 "%d: Expected type after ordinal, found '%s' instead\n",
428 static int ParseTopLevel(void)
432 while ((token
= GetToken()) != NULL
)
434 if (strcmp(token
, "name") == 0)
436 strcpy(LowerDLLName
, GetToken());
437 strlower(LowerDLLName
);
439 strcpy(UpperDLLName
, LowerDLLName
);
440 strupper(UpperDLLName
);
442 else if (strcmp(token
, "id") == 0)
445 if (!IsNumberString(token
))
447 fprintf(stderr
, "%d: Expected number after id\n", Line
);
453 else if (strcmp(token
, "base") == 0)
456 if (!IsNumberString(token
))
458 fprintf(stderr
, "%d: Expected number after base\n", Line
);
464 else if (IsNumberString(token
))
469 ordinal
= atoi(token
);
470 if ((rv
= ParseOrdinal(ordinal
)) < 0)
476 "%d: Expected name, id, length or ordinal\n", Line
);
485 static int OutputVariableCode( char *storage
, ORDDEF
*odp
)
490 vdp
= odp
->additional_data
;
491 printf( "\t.data\n" );
492 for (i
= 0; i
< vdp
->n_values
; i
++)
495 printf( "\t%s\t", storage
);
497 printf( "%d", vdp
->values
[i
]);
499 if ((i
& 7) == 7 || i
== vdp
->n_values
- 1) printf( "\n");
503 printf( "\t.text\n" );
504 return vdp
->n_values
;
508 /*******************************************************************
511 * Build the in-memory representation of the module, and dump it
512 * as a byte stream into the assembly code.
514 static void BuildModule( int max_code_offset
, int max_data_offset
)
520 SEGTABLEENTRY
*pSegment
;
521 LOADEDFILEINFO
*pFileInfo
;
527 * LOADEDFILEINFO File information
528 * SEGTABLEENTRY Segment 1 (code)
529 * SEGTABLEENTRY Segment 2 (data)
530 * WORD[2] Resource table (empty)
531 * BYTE[2] Imported names (empty)
532 * BYTE[n] Resident names table
533 * BYTE[n] Entry table
536 buffer
= malloc( 0x10000 );
538 pModule
= (NE_MODULE
*)buffer
;
539 pModule
->magic
= NE_SIGNATURE
;
542 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_LIBMODULE
;
544 pModule
->heap_size
= 0xffff;
545 pModule
->stack_size
= 0;
550 pModule
->seg_count
= 2;
551 pModule
->modref_count
= 0;
552 pModule
->nrname_size
= 0;
553 pModule
->modref_table
= 0;
554 pModule
->nrname_fpos
= 0;
555 pModule
->moveable_entries
= 0;
556 pModule
->alignment
= 0;
557 pModule
->truetype
= 0;
558 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
559 pModule
->misc_flags
= 0;
560 pModule
->dlls_to_init
= 0;
561 pModule
->nrname_handle
= 0;
562 pModule
->min_swap_area
= 0;
563 pModule
->expected_version
= 0x030a;
565 /* File information */
567 pFileInfo
= (LOADEDFILEINFO
*)(pModule
+ 1);
568 pModule
->fileinfo
= (int)pFileInfo
- (int)pModule
;
569 pFileInfo
->length
= sizeof(LOADEDFILEINFO
) + strlen(UpperDLLName
) + 3;
570 pFileInfo
->fixed_media
= 0;
571 pFileInfo
->error
= 0;
574 sprintf( pFileInfo
->filename
, "%s.DLL", UpperDLLName
);
575 pstr
= (char *)pFileInfo
+ pFileInfo
->length
+ 1;
579 pSegment
= (SEGTABLEENTRY
*)pstr
;
580 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
581 pSegment
->filepos
= 0;
582 pSegment
->size
= max_code_offset
;
584 pSegment
->minsize
= max_code_offset
;
585 pSegment
->selector
= 0;
588 pModule
->dgroup_entry
= (int)pSegment
- (int)pModule
;
589 pSegment
->filepos
= 0;
590 pSegment
->size
= max_data_offset
;
591 pSegment
->flags
= NE_SEGFLAGS_DATA
;
592 pSegment
->minsize
= max_data_offset
;
593 pSegment
->selector
= 0;
598 pword
= (WORD
*)pSegment
;
599 pModule
->res_table
= (int)pword
- (int)pModule
;
603 /* Imported names table */
605 pstr
= (char *)pword
;
606 pModule
->import_table
= (int)pstr
- (int)pModule
;
610 /* Resident names table */
612 pModule
->name_table
= (int)pstr
- (int)pModule
;
613 /* First entry is module name */
614 *pstr
= strlen(UpperDLLName
);
615 strcpy( pstr
+ 1, UpperDLLName
);
618 pstr
+= sizeof(WORD
);
619 /* Store all ordinals */
620 odp
= OrdinalDefinitions
+ 1;
621 for (i
= 1; i
<= Limit
; i
++, odp
++)
623 if (!odp
->export_name
[0]) continue;
624 *pstr
= strlen( odp
->export_name
);
625 strcpy( pstr
+ 1, odp
->export_name
);
626 strupper( pstr
+ 1 );
629 pstr
+= sizeof(WORD
);
635 pModule
->entry_table
= (int)pstr
- (int)pModule
;
637 odp
= OrdinalDefinitions
+ 1;
638 for (i
= 1; i
<= Limit
; i
++, odp
++)
645 selector
= 0; /* Invalid selector */
653 selector
= 1; /* Code selector */
659 selector
= 2; /* Data selector */
663 selector
= 0xfe; /* Constant selector */
667 /* create a new bundle if necessary */
668 if (!bundle
|| (bundle
[0] >= 254) || (bundle
[1] != selector
))
672 bundle
[1] = selector
;
680 *(WORD
*)pstr
= odp
->offset
;
681 pstr
+= sizeof(WORD
);
686 /* Dump the module content */
688 printf( "\t.data\n" );
689 printf( "\t.globl " PREFIX
"%s_Module_Start\n", UpperDLLName
);
690 printf( PREFIX
"%s_Module_Start:\n", UpperDLLName
);
691 size
= (int)pstr
- (int)pModule
;
692 for (i
= 0, pstr
= buffer
; i
< size
; i
++, pstr
++)
694 if (!(i
& 7)) printf( "\t.byte " );
695 printf( "%d%c", *pstr
, ((i
& 7) != 7) ? ',' : '\n' );
697 if (i
& 7) printf( "0\n" );
698 printf( "\t.globl " PREFIX
"%s_Module_End\n", UpperDLLName
);
699 printf( PREFIX
"%s_Module_End:\n", UpperDLLName
);
703 static void BuildSpec32Files( char *specname
)
710 SpecFp
= fopen( specname
, "r");
713 fprintf(stderr
, "Could not open specification file, '%s'\n", specname
);
719 printf( "/* File generated automatically, do not edit! */\n" );
720 printf( "#include <sys/types.h>\n");
721 printf( "#include \"dlls.h\"\n");
722 printf( "#include \"pe_image.h\"\n");
723 printf( "#include \"winerror.h\"\n");
724 printf( "#include \"stddebug.h\"\n");
725 printf( "#include \"debug.h\"\n");
726 printf( "\nextern int RELAY32_Unimplemented();\n\n" );
728 odp
= OrdinalDefinitions
;
729 for (i
= 0; i
<= Limit
; i
++, odp
++)
732 fdp
= odp
->additional_data
;
733 rdp
= odp
->additional_data
;
739 printf( "int %s_%d()\n{\n\t", UpperDLLName
, i
);
740 printf( "RELAY32_Unimplemented(\"%s\",%d);\n", UpperDLLName
, i
);
741 printf( "\t/*NOTREACHED*/\n\treturn 0;\n}\n\n");
744 argc
=strlen(fdp
->arg_types
);
745 printf( "void %s_%d(", UpperDLLName
, i
);
746 for(argno
=0;argno
<argc
;argno
++)
748 switch(fdp
->arg_types
[argno
])
750 case 'p': printf( "void *");break;
751 case 'l': printf( "int ");break;
753 fprintf(stderr
, "Not supported argument type %c\n",
754 fdp
->arg_types
[argno
]);
757 putchar( 'a'+argno
);
758 if (argno
!=argc
-1) putchar( ',' );
761 printf( "\textern int %s();\n", fdp
->internal_name
);
762 printf( "\tdprintf_relay(stddeb,\"Entering %%s.%%s(");
763 for (argno
=0;argno
<argc
;argno
++) printf( "%%x ");
764 printf( ")\\n\", \"%s\", \"%s\"", UpperDLLName
, odp
->export_name
);
765 for(argno
=0;argno
<argc
;argno
++) printf( ",%c", 'a'+argno
);
766 printf( ");\n\t%s(", fdp
->internal_name
);
767 for(argno
=0;argno
<argc
;argno
++)
770 if (argno
!=argc
-1) putchar(',');
772 printf( ");\n\t__asm__ __volatile__(\"movl %%ebp,%%esp;"
773 "popl %%ebp;ret $%d\");\n}\n\n",
777 printf( "void %s_%d()\n{\n\t", UpperDLLName
, i
);
778 printf( "RELAY32_DebugEnter(\"%s\",\"%s\");\n\t",
779 UpperDLLName
, odp
->export_name
);
780 printf( "WIN32_LastError=ERROR_CALL_NOT_IMPLEMENTED;\n");
781 printf( "\t__asm__ __volatile__ (\"movl %d,%%eax\");\n",
783 printf( "\t__asm__ __volatile__ (\"movl %%ebp,%%esp;popl %%ebp;"
784 "ret $%d\");\n}\n\n", rdp
->arg_size
);
787 fprintf(stderr
,"build: function type %d not available for Win32\n",
793 printf( "static WIN32_function functions[%d+1]={\n", Limit
);
795 odp
= OrdinalDefinitions
;
796 for (i
= 0; i
<= Limit
; i
++, odp
++)
798 fdp
= odp
->additional_data
;
799 rdp
= odp
->additional_data
;
804 printf( "{0,%s_%d},\n",UpperDLLName
, i
);
809 printf( "{\"%s\",%s_%d},\n", odp
->export_name
, UpperDLLName
, i
);
812 fprintf(stderr
, "build: implementation error: missing %d\n",
819 printf( "static WIN32_builtin dll={\"%s\",functions,%d,0};\n",
820 UpperDLLName
, Limit
);
822 printf( "void %s_Init(void)\n{\n",UpperDLLName
);
823 printf( "\tdll.next=WIN32_builtin_list;\n");
824 printf( "\tWIN32_builtin_list=&dll;\n}");
828 static void BuildSpec16Files( char *specname
)
834 int code_offset
, data_offset
;
836 SpecFp
= fopen( specname
, "r");
839 fprintf(stderr
, "Could not open specification file, '%s'\n", specname
);
845 printf( "/* File generated automatically; do not edit! */\n" );
846 printf( "\t.data\n" );
847 printf( "\t.globl " PREFIX
"%s_Data_Start\n", UpperDLLName
);
848 printf( PREFIX
"%s_Data_Start:\n", UpperDLLName
);
849 printf( "\t.word 0,0,0,0,0,0,0,0\n" );
851 printf( "\t.text\n" );
852 printf( "\t.globl " PREFIX
"%s_Code_Start\n", UpperDLLName
);
853 printf( PREFIX
"%s_Code_Start:\n", UpperDLLName
);
856 odp
= OrdinalDefinitions
;
857 for (i
= 0; i
<= Limit
; i
++, odp
++)
859 fdp
= odp
->additional_data
;
860 rdp
= odp
->additional_data
;
865 odp
->offset
= 0xffff;
869 odp
->offset
= (int)odp
->additional_data
& 0xffff;
873 printf( "/* %s.%d */\n", UpperDLLName
, i
);
874 odp
->offset
= data_offset
;
875 data_offset
+= OutputVariableCode( ".byte", odp
);
879 printf( "/* %s.%d */\n", UpperDLLName
, i
);
880 odp
->offset
= data_offset
;
881 data_offset
+= 2 * OutputVariableCode( ".word", odp
);
885 printf( "/* %s.%d */\n", UpperDLLName
, i
);
886 odp
->offset
= data_offset
;
887 data_offset
+= 4 * OutputVariableCode( ".long", odp
);
891 printf( "/* %s.%d */\n", UpperDLLName
, i
);
892 printf( "\tmovw $%d,%%ax\n", rdp
->ret_value
& 0xffff );
893 printf( "\tmovw $%d,%%dx\n", (rdp
->ret_value
>> 16) & 0xffff);
894 printf( "\t.byte 0x66\n");
895 if (rdp
->arg_size
!= 0)
896 printf( "\tlret $%d\n", rdp
->arg_size
);
899 odp
->offset
= code_offset
;
900 code_offset
+= 10; /* Assembly code is 10 bytes long */
901 if (rdp
->arg_size
!= 0) code_offset
+= 2;
908 printf( "/* %s.%d */\n", UpperDLLName
, i
);
909 printf( "\tpushw %%bp\n" );
910 printf( "\tpushl $0x%08x\n", (DLLId
<< 16) | i
);
911 printf( "\tpushl $" PREFIX
"%s\n", fdp
->internal_name
);
912 printf( "\tljmp $0x%04x, $" PREFIX
"CallTo32_%s_%s\n\n",
914 (odp
->type
== TYPE_REGISTER
) ? "regs" :
915 (odp
->type
== TYPE_PASCAL
) ? "long" : "word",
922 odp
->offset
= code_offset
;
923 code_offset
+= 24; /* Assembly code is 24 bytes long */
927 fprintf( stderr
, "build: Unknown function type; please report.\n");
932 if (!code_offset
) /* Make sure the code segment is not empty */
934 printf( "\t.byte 0\n" );
938 BuildModule( code_offset
, data_offset
);
942 /*******************************************************************
943 * BuildCall32LargeStack
945 * Build the function used to switch to the original 32-bit stack
946 * before calling a 32-bit function from 32-bit code. This is used for
947 * functions that need a large stack, like X bitmaps functions.
949 * The generated function has the following prototype:
950 * int CallTo32_LargeStack( int (*func)(), int nbargs, ... )
961 static void BuildCall32LargeStack(void)
963 /* Function header */
965 printf( "/**********\n" );
966 printf( " * " PREFIX
"CallTo32_LargeStack\n" );
967 printf( " **********/\n" );
968 printf( "\t.align 4\n" );
969 printf( "\t.globl " PREFIX
"CallTo32_LargeStack\n\n" );
970 printf( PREFIX
"CallTo32_LargeStack:\n" );
974 printf( "\tpushl %%ebp\n" );
975 printf( "\tmovl %%esp,%%ebp\n" );
977 /* Retrieve the original 32-bit stack pointer */
979 printf( "\tmovl " PREFIX
"IF1632_Original32_esp, %%eax\n" );
980 printf( "\torl %%eax,%%eax\n" );
981 printf( "\tje 0f\n" );
985 printf( "\tpushl %%ecx\n" );
986 printf( "\tpushl %%esi\n" );
987 printf( "\tpushl %%edi\n" );
989 /* Switch to the new stack */
991 printf( "\tmovl %%eax,%%esp\n" );
993 /* Transfer the arguments */
995 printf( "\tleal 16(%%ebp),%%esi\n" );
996 printf( "\tmovl 12(%%ebp),%%ecx\n" );
997 printf( "\torl %%ecx,%%ecx\n" );
998 printf( "\tje 1f\n" );
999 printf( "\tshll $2,%%ecx\n" );
1000 printf( "\tsubl %%ecx,%%esp\n" );
1001 printf( "\tmovl %%esp,%%edi\n" );
1002 printf( "\tshrl $2,%%ecx\n" );
1003 printf( "\trep; movsl\n" );
1006 /* Call the function */
1008 printf( "\tcall 8(%%ebp)\n" );
1010 /* Switch back to the normal stack */
1012 printf( "\tleal -12(%%ebp),%%esp\n" );
1014 /* Restore registers and return */
1016 printf( "\tpopl %%edi\n" );
1017 printf( "\tpopl %%esi\n" );
1018 printf( "\tpopl %%ecx\n" );
1020 printf( "\tpopl %%ebp\n" );
1021 printf( "\tret\n" );
1023 /* We get here if IF1632_Original32_esp is 0, i.e. we have not */
1024 /* switched to another 32-bit stack yet. */
1028 /* Move the return address up the stack */
1030 printf( "\tmovl 4(%%ebp),%%eax\n" );
1031 printf( "\tmovl %%eax,12(%%ebp)\n" );
1033 /* Restore ebp and remove old return address */
1035 printf( "\tpopl %%ebp\n" );
1036 printf( "\taddl $4,%%esp\n" );
1038 /* Now jump to the routine, leaving the original return address and */
1039 /* the arguments on the stack. */
1041 printf( "\tret\n" );
1045 /*******************************************************************
1046 * TransferArgs16To32
1048 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1049 * The 16-bit stack layout is:
1057 static int TransferArgs16To32( char *args
)
1059 int i
, pos16
, pos32
;
1061 /* Save ebx first */
1063 printf( "\tpushl %%ebx\n" );
1065 /* Get the 32-bit stack pointer */
1067 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebx\n" );
1069 /* Copy the arguments */
1071 pos16
= 6; /* skip bp and return address */
1074 for (i
= strlen(args
); i
> 0; i
--)
1079 case 'w': /* word */
1080 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1081 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1085 case 's': /* s_word */
1086 printf( "\tmovswl %d(%%ebp),%%eax\n", pos16
);
1087 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1091 case 'l': /* long */
1092 printf( "\tmovl %d(%%ebp),%%eax\n", pos16
);
1093 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1098 /* Get the selector */
1099 printf( "\tmovw %d(%%ebp),%%ax\n", pos16
+ 2 );
1100 /* Get the selector base */
1101 printf( "\tandl $0xfff8,%%eax\n" );
1102 printf( "\tmovl " PREFIX
"ldt_copy(%%eax),%%eax\n" );
1103 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1104 /* Add the offset */
1105 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1106 printf( "\taddl %%eax,%d(%%ebx)\n", pos32
);
1111 fprintf( stderr
, "Unknown arg type '%c'\n", args
[i
-1] );
1117 printf( "\tpopl %%ebx\n" );
1119 return pos16
- 6; /* Return the size of the 16-bit args */
1123 /*******************************************************************
1126 * Build the context structure on the 32-bit stack.
1127 * The only valid registers in the context structure are:
1128 * eax, ebx, ecx, edx, esi, edi, ds, es, (some of the) flags
1130 static void BuildContext(void)
1132 /* Save ebx first */
1134 printf( "\tpushl %%ebx\n" );
1136 /* Get the 32-bit stack pointer */
1138 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebx\n" );
1140 /* Store the registers */
1142 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_ebx
) ); /* Get ebx from stack */
1143 printf( "\tmovl %%eax,%d(%%ebx)\n", CONTEXTOFFSET(sc_eax
) );
1144 printf( "\tmovl %%ecx,%d(%%ebx)\n", CONTEXTOFFSET(sc_ecx
) );
1145 printf( "\tmovl %%edx,%d(%%ebx)\n", CONTEXTOFFSET(sc_edx
) );
1146 printf( "\tmovl %%esi,%d(%%ebx)\n", CONTEXTOFFSET(sc_esi
) );
1147 printf( "\tmovl %%edi,%d(%%ebx)\n", CONTEXTOFFSET(sc_edi
) );
1148 printf( "\tpushw %%es\n" );
1149 printf( "\tpopw %d(%%ebx)\n", CONTEXTOFFSET(sc_es
) );
1150 printf( "\tmovw -10(%%ebp),%%ax\n" ); /* Get saved ds from stack */
1151 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(sc_ds
) );
1152 printf( "\tpushfl\n" );
1154 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_eflags
) );
1156 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_efl
) );
1161 /*******************************************************************
1164 * Restore the registers from the context structure
1166 static void RestoreContext(void)
1168 /* Get the 32-bit stack pointer */
1170 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebx\n" );
1172 /* Restore the registers */
1174 printf( "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(sc_ecx
) );
1175 printf( "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(sc_edx
) );
1176 printf( "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(sc_esi
) );
1177 printf( "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(sc_edi
) );
1178 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(sc_es
) );
1179 printf( "\tpopw %%es\n" );
1180 printf( "\tpopw %%ax\n" ); /* Remove old ds from the stack */
1181 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(sc_ds
) ); /* Push new ds */
1183 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(sc_eflags
) );
1185 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(sc_efl
) );
1187 printf( "\tpopfl\n" );
1188 printf( "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(sc_eax
) );
1189 printf( "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(sc_ebx
) );
1193 /*******************************************************************
1196 * Build a 32-bit callback function. The syntax of the function
1197 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1198 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1199 * 'l'=long, 'p'=pointer).
1200 * For register functions, the arguments are ignored, but they are still
1201 * removed from the stack upon return.
1203 * Stack layout upon entry to the callback function:
1205 * (sp+14) first 16-bit arg
1209 * (sp+4) dll_id+ordinal (long)
1210 * (sp) entrypoint (long)
1213 static void BuildCall32Func( char *profile
)
1218 char *args
= profile
+ 5;
1220 /* Parse function type */
1222 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1223 else if (!strncmp( "regs_", profile
, 5 )) reg_func
= 1;
1224 else if (strncmp( "long_", profile
, 5 ))
1226 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1230 /* Function header */
1232 printf( "/**********\n" );
1233 printf( " * " PREFIX
"CallTo32_%s\n", profile
);
1234 printf( " **********/\n" );
1235 printf( "\t.align 4\n" );
1236 printf( "\t.globl " PREFIX
"CallTo32_%s\n\n", profile
);
1237 printf( PREFIX
"CallTo32_%s:\n", profile
);
1239 /* Setup bp to point to its copy on the stack */
1241 printf( "\tmovzwl %%sp,%%ebp\n" );
1242 printf( "\taddw $8,%%bp\n" );
1244 /* Save 16-bit ds */
1246 printf( "\tpushw %%ds\n" );
1248 /* Restore 32-bit ds */
1250 printf( "\tpushw $0x%04x\n", WINE_DATA_SELECTOR
);
1251 printf( "\tpopw %%ds\n" );
1253 /* Save the 16-bit stack */
1255 printf( "\tpushw " PREFIX
"IF1632_Saved16_sp\n" );
1256 printf( "\tpushw " PREFIX
"IF1632_Saved16_ss\n" );
1257 printf( "\tmovw %%ss," PREFIX
"IF1632_Saved16_ss\n" );
1258 printf( "\tmovw %%sp," PREFIX
"IF1632_Saved16_sp\n" );
1260 /* Transfer the arguments */
1262 if (reg_func
) BuildContext();
1263 else if (*args
) argsize
= TransferArgs16To32( args
);
1265 /* Get the address of the API function */
1267 printf( "\tmovl -8(%%ebp),%%eax\n" );
1269 /* If necessary, save %edx over the API function address */
1271 if (!reg_func
&& short_ret
)
1272 printf( "\tmovl %%edx,-8(%%ebp)\n" );
1276 printf( "\tpushw %%ds\n" );
1277 printf( "\tpopw %%es\n" );
1279 /* Switch to the 32-bit stack */
1281 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebp\n" );
1282 printf( "\tpushw %%ds\n" );
1283 printf( "\tpopw %%ss\n" );
1284 printf( "\tleal -%d(%%ebp),%%esp\n",
1285 reg_func
? sizeof(struct sigcontext_struct
) : 4 * strlen(args
) );
1287 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1289 printf( "\taddl $24,%%ebp\n" );
1291 /* Print the debug information before the call */
1295 printf( "\tpushl %%eax\n" );
1296 printf( "\tpushl $CALL32_Str_%s\n", profile
);
1297 printf( "\tcall " PREFIX
"RELAY_DebugCall32\n" );
1298 printf( "\tpopl %%eax\n" );
1299 printf( "\tpopl %%eax\n" );
1302 /* Call the entry point */
1304 printf( "\tcall %%eax\n" );
1306 /* Print the debug information after the call */
1310 printf( "\tpushl %%eax\n" );
1311 printf( "\tpushl $%d\n", reg_func
? 2 : (short_ret
? 1 : 0) );
1312 printf( "\tcall " PREFIX
"RELAY_DebugReturn\n" );
1313 printf( "\tpopl %%eax\n" );
1314 printf( "\tpopl %%eax\n" );
1317 /* Restore the 16-bit stack */
1319 printf( "\tmovw " PREFIX
"IF1632_Saved16_ss,%%ss\n" );
1320 printf( "\tmovw " PREFIX
"IF1632_Saved16_sp,%%sp\n" );
1321 printf( "\tpopw " PREFIX
"IF1632_Saved16_ss\n" );
1322 printf( "\tpopw " PREFIX
"IF1632_Saved16_sp\n" );
1326 /* Restore registers from the context structure */
1329 /* Calc the arguments size */
1343 fprintf( stderr
, "Unknown arg type '%c'\n", *args
);
1351 printf( "\tpopw %%ds\n" );
1353 /* Get the return value into dx:ax and clean up the stack */
1359 printf( "\tpopl %%edx\n" ); /* Restore %edx */
1360 printf( "\taddl $4,%%esp\n" ); /* Remove DLL id and ordinal */
1364 printf( "\tpushl %%eax\n" );
1365 printf( "\tpopw %%ax\n" );
1366 printf( "\tpopw %%dx\n" );
1367 /* Remove API entry point, DLL id and ordinal from the stack */
1368 printf( "\taddl $8,%%esp\n" );
1373 /* Remove API entry point, DLL id and ordinal from the stack, */
1374 /* but take care not to change the value of the carry flag. */
1376 printf( "\tpopl %%ebp\n" );
1377 printf( "\tpopl %%ebp\n" );
1382 printf( "\tpopw %%bp\n" );
1384 /* Remove the arguments and return */
1388 printf( "\t.byte 0x66\n" );
1389 printf( "\tlret $%d\n", argsize
);
1393 printf( "\t.byte 0x66\n" );
1394 printf( "\tlret\n" );
1399 /*******************************************************************
1402 * Build a 16-bit callback function.
1404 * Stack frame of the callback function:
1408 * (ebp+16) 16-bit ds
1409 * (ebp+12) func to call
1410 * (ebp+8) code selector
1411 * (ebp+4) return address
1412 * (ebp) previous ebp
1414 * Prototypes for the CallTo16 functions:
1415 * extern WORD CallTo16_word_xxx( FARPROC func, WORD ds, args... );
1416 * extern LONG CallTo16_long_xxx( FARPROC func, WORD ds, args... );
1417 * extern void CallTo16_regs_( FARPROC func, WORD ds, WORD es, WORD bp,
1418 * WORD ax, WORD bx, WORD cx, WORD dx,
1419 * WORD si, WORD di );
1421 static void BuildCall16Func( char *profile
)
1425 char *args
= profile
+ 5;
1427 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1428 else if (!strncmp( "regs_", profile
, 5 )) reg_func
= short_ret
= 1;
1429 else if (strncmp( "long_", profile
, 5 ))
1431 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1435 /* Function header */
1437 printf( "/**********\n" );
1438 printf( " * " PREFIX
"CallTo16_%s\n", profile
);
1439 printf( " **********/\n" );
1440 printf( "\t.align 4\n" );
1441 printf( "\t.globl " PREFIX
"CallTo16_%s\n\n", profile
);
1442 printf( PREFIX
"CallTo16_%s:\n", profile
);
1444 /* Push code selector before return address to simulate a lcall */
1446 printf( "\tpopl %%eax\n" );
1447 printf( "\tpushl $0x%04x\n", WINE_CODE_SELECTOR
);
1448 printf( "\tpushl %%eax\n" );
1452 printf( "\tpushl %%ebp\n" );
1453 printf( "\tmovl %%esp,%%ebp\n" );
1455 /* Save the 32-bit registers */
1457 printf( "\tpushl %%ebx\n" );
1458 printf( "\tpushl %%ecx\n" );
1459 printf( "\tpushl %%edx\n" );
1460 printf( "\tpushl %%esi\n" );
1461 printf( "\tpushl %%edi\n" );
1463 /* Save the 32-bit stack */
1465 printf( "\tpushl " PREFIX
"IF1632_Saved32_esp\n" );
1466 printf( "\tmovl %%esp," PREFIX
"IF1632_Saved32_esp\n" );
1467 printf( "\tmovl %%ebp,%%ebx\n" );
1469 /* Print debugging info */
1473 /* Push the address of the first argument */
1474 printf( "\tmovl %%ebx,%%eax\n" );
1475 printf( "\taddl $12,%%eax\n" );
1476 printf( "\tpushl $%d\n", reg_func
? 8 : strlen(args
) );
1477 printf( "\tpushl %%eax\n" );
1478 printf( "\tcall " PREFIX
"RELAY_DebugCall16\n" );
1479 printf( "\tpopl %%eax\n" );
1480 printf( "\tpopl %%eax\n" );
1483 /* Switch to the 16-bit stack */
1485 printf( "\tmovw " PREFIX
"IF1632_Saved16_ss,%%ss\n" );
1486 printf( "\tmovw " PREFIX
"IF1632_Saved16_sp,%%sp\n" );
1488 /* Transfer the arguments */
1492 /* Get the registers. ebx is handled later on. */
1493 printf( "\tpushw 20(%%ebx)\n" );
1494 printf( "\tpopw %%es\n" );
1495 printf( "\tmovl 24(%%ebx),%%ebp\n" );
1496 printf( "\tmovl 28(%%ebx),%%eax\n" );
1497 printf( "\tmovl 36(%%ebx),%%ecx\n" );
1498 printf( "\tmovl 40(%%ebx),%%edx\n" );
1499 printf( "\tmovl 44(%%ebx),%%esi\n" );
1500 printf( "\tmovl 48(%%ebx),%%edi\n" );
1502 else /* not a register function */
1504 int pos
= 20; /* first argument position */
1506 /* Make %bp point to the previous stackframe (built by CallTo32) */
1507 printf( "\tmovw %%sp,%%bp\n" );
1508 printf( "\taddw $16,%%bp\n" );
1514 case 'w': /* word */
1515 printf( "\tpushw %d(%%ebx)\n", pos
);
1517 case 'l': /* long */
1518 printf( "\tpushl %d(%%ebx)\n", pos
);
1525 /* Push the return address */
1527 printf( "\tpushl " PREFIX
"CALL16_RetAddr_%s\n",
1528 short_ret
? "word" : "long" );
1530 /* Push the called routine address */
1532 printf( "\tpushl 12(%%ebx)\n" );
1534 /* Get the 16-bit ds */
1535 /* FIXME: this shouldn't be necessary if function prologs fixup worked. */
1539 printf( "\tpushw 16(%%ebx)\n" );
1540 printf( "\tmovl 32(%%ebx),%%ebx\n" ); /*Get ebx from the 32-bit stack*/
1541 printf( "\tpopw %%ds\n" );
1545 /* Set ax equal to ds for window procedures */
1546 printf( "\tmovw 16(%%ebx),%%ax\n" );
1547 /* printf( "\tmovw %%ax,%%ds\n" ); */
1550 /* Jump to the called routine */
1552 printf( "\t.byte 0x66\n" );
1553 printf( "\tlret\n" );
1557 /*******************************************************************
1560 * Build the return code for 16-bit callbacks
1562 static void BuildRet16Func()
1564 printf( "\t.globl " PREFIX
"CALL16_Ret_word\n" );
1565 printf( "\t.globl " PREFIX
"CALL16_Ret_long\n" );
1567 /* Put return value into eax */
1569 printf( PREFIX
"CALL16_Ret_long:\n" );
1570 printf( "\tpushw %%dx\n" );
1571 printf( "\tpushw %%ax\n" );
1572 printf( "\tpopl %%eax\n" );
1573 printf( PREFIX
"CALL16_Ret_word:\n" );
1575 /* Restore 32-bit segment registers */
1577 printf( "\tmovw $0x%04x,%%bx\n", WINE_DATA_SELECTOR
);
1578 printf( "\tmovw %%bx,%%ds\n" );
1579 printf( "\tmovw %%bx,%%es\n" );
1580 printf( "\tmovw %%bx,%%ss\n" );
1582 /* Restore the 32-bit stack */
1584 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%esp\n" );
1585 printf( "\tpopl " PREFIX
"IF1632_Saved32_esp\n" );
1587 /* Restore the 32-bit registers */
1589 printf( "\tpopl %%edi\n" );
1590 printf( "\tpopl %%esi\n" );
1591 printf( "\tpopl %%edx\n" );
1592 printf( "\tpopl %%ecx\n" );
1593 printf( "\tpopl %%ebx\n" );
1595 /* Return to caller */
1597 printf( "\tpopl %%ebp\n" );
1598 printf( "\tlret\n" );
1600 /* Declare the return address variables */
1602 printf( "\t.data\n" );
1603 printf( "\t.globl " PREFIX
"CALL16_RetAddr_word\n" );
1604 printf( "\t.globl " PREFIX
"CALL16_RetAddr_long\n" );
1605 printf( PREFIX
"CALL16_RetAddr_word:\t.long 0\n" );
1606 printf( PREFIX
"CALL16_RetAddr_long:\t.long 0\n" );
1607 printf( "\t.text\n" );
1611 static void usage(void)
1613 fprintf(stderr
, "usage: build -spec SPECNAMES\n"
1614 " build -call32 FUNCTION_PROFILES\n"
1615 " build -call16 FUNCTION_PROFILES\n" );
1620 int main(int argc
, char **argv
)
1624 if (argc
<= 2) usage();
1626 if (!strcmp( argv
[1], "-spec16" ))
1628 for (i
= 2; i
< argc
; i
++) BuildSpec16Files( argv
[i
] );
1630 else if (!strcmp( argv
[1], "-spec32" ))
1632 for (i
= 2; i
< argc
; i
++) BuildSpec32Files( argv
[i
] );
1634 else if (!strcmp( argv
[1], "-call32" )) /* 32-bit callbacks */
1638 printf( "/* File generated automatically. Do no edit! */\n\n" );
1639 printf( "\t.text\n" );
1641 /* Build the 32-bit large stack callback */
1643 BuildCall32LargeStack();
1645 /* Build the callback functions */
1647 for (i
= 2; i
< argc
; i
++) BuildCall32Func( argv
[i
] );
1649 /* Output the argument debugging strings */
1653 printf( "/* Argument strings */\n" );
1654 for (i
= 2; i
< argc
; i
++)
1656 printf( "CALL32_Str_%s:\n", argv
[i
] );
1657 printf( "\t.ascii \"%s\\0\"\n", argv
[i
] + 5 );
1661 else if (!strcmp( argv
[1], "-call16" )) /* 16-bit callbacks */
1665 printf( "/* File generated automatically. Do no edit! */\n\n" );
1666 printf( "\t.text\n" );
1667 printf( "\t.globl " PREFIX
"CALL16_Start\n" );
1668 printf( PREFIX
"CALL16_Start:\n" );
1670 /* Build the callback functions */
1672 for (i
= 2; i
< argc
; i
++) BuildCall16Func( argv
[i
] );
1674 /* Output the 16-bit return code */
1678 printf( "\t.globl " PREFIX
"CALL16_End\n" );
1679 printf( PREFIX
"CALL16_End:\n" );