4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Martin von Loewis
6 * Copyright 1995, 1996, 1997 Alexandre Julliard
7 * Copyright 1997 Eric Youngdale
8 * Copyright 1999 Ulrich Weigand
14 #include "builtin16.h"
17 #include "stackframe.h"
22 /*******************************************************************
25 * Store a list of ints into a byte array.
27 static int StoreVariableCode( unsigned char *buffer
, int size
, ORDDEF
*odp
)
34 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
35 buffer
[i
] = odp
->u
.var
.values
[i
];
38 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
39 ((unsigned short *)buffer
)[i
] = odp
->u
.var
.values
[i
];
42 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
43 ((unsigned int *)buffer
)[i
] = odp
->u
.var
.values
[i
];
46 return odp
->u
.var
.n_values
* size
;
50 /*******************************************************************
53 * Build the in-memory representation of a 16-bit NE module, and dump it
54 * as a byte stream into the assembly code.
56 static int BuildModule16( FILE *outfile
, int max_code_offset
,
62 SEGTABLEENTRY
*pSegment
;
66 ET_BUNDLE
*bundle
= 0;
71 * OFSTRUCT File information
72 * SEGTABLEENTRY Segment 1 (code)
73 * SEGTABLEENTRY Segment 2 (data)
74 * WORD[2] Resource table (empty)
75 * BYTE[2] Imported names (empty)
76 * BYTE[n] Resident names table
80 buffer
= xmalloc( 0x10000 );
82 pModule
= (NE_MODULE
*)buffer
;
83 memset( pModule
, 0, sizeof(*pModule
) );
84 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
87 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_BUILTIN
| NE_FFLAGS_LIBMODULE
;
89 pModule
->heap_size
= DLLHeapSize
;
90 pModule
->stack_size
= 0;
95 pModule
->seg_count
= 2;
96 pModule
->modref_count
= 0;
97 pModule
->nrname_size
= 0;
98 pModule
->modref_table
= 0;
99 pModule
->nrname_fpos
= 0;
100 pModule
->moveable_entries
= 0;
101 pModule
->alignment
= 0;
102 pModule
->truetype
= 0;
103 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
104 pModule
->misc_flags
= 0;
105 pModule
->dlls_to_init
= 0;
106 pModule
->nrname_handle
= 0;
107 pModule
->min_swap_area
= 0;
108 pModule
->expected_version
= 0;
109 pModule
->module32
= 0;
111 pModule
->self_loading_sel
= 0;
113 /* File information */
115 pFileInfo
= (OFSTRUCT
*)(pModule
+ 1);
116 pModule
->fileinfo
= (int)pFileInfo
- (int)pModule
;
117 memset( pFileInfo
, 0, sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
) );
118 pFileInfo
->cBytes
= sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
)
119 + strlen(DLLFileName
);
120 strcpy( pFileInfo
->szPathName
, DLLFileName
);
121 pstr
= (char *)pFileInfo
+ pFileInfo
->cBytes
+ 1;
123 #ifdef __i386__ /* FIXME: Alignment problems! */
127 pSegment
= (SEGTABLEENTRY
*)pstr
;
128 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
129 pSegment
->filepos
= 0;
130 pSegment
->size
= max_code_offset
;
132 pSegment
->minsize
= max_code_offset
;
136 pModule
->dgroup_entry
= (int)pSegment
- (int)pModule
;
137 pSegment
->filepos
= 0;
138 pSegment
->size
= max_data_offset
;
139 pSegment
->flags
= NE_SEGFLAGS_DATA
;
140 pSegment
->minsize
= max_data_offset
;
146 pword
= (WORD
*)pSegment
;
147 pModule
->res_table
= (int)pword
- (int)pModule
;
151 /* Imported names table */
153 pstr
= (char *)pword
;
154 pModule
->import_table
= (int)pstr
- (int)pModule
;
158 /* Resident names table */
160 pModule
->name_table
= (int)pstr
- (int)pModule
;
161 /* First entry is module name */
162 *pstr
= strlen(DLLName
);
163 strcpy( pstr
+ 1, DLLName
);
166 pstr
+= sizeof(WORD
);
167 /* Store all ordinals */
168 for (i
= 1; i
<= Limit
; i
++)
170 ORDDEF
*odp
= Ordinals
[i
];
171 if (!odp
|| !odp
->name
[0]) continue;
172 *pstr
= strlen( odp
->name
);
173 strcpy( pstr
+ 1, odp
->name
);
174 strupper( pstr
+ 1 );
177 pstr
+= sizeof(WORD
);
183 pModule
->entry_table
= (int)pstr
- (int)pModule
;
184 for (i
= 1; i
<= Limit
; i
++)
187 ORDDEF
*odp
= Ordinals
[i
];
198 selector
= 1; /* Code selector */
204 selector
= 2; /* Data selector */
208 selector
= 0xfe; /* Constant selector */
212 selector
= 0; /* Invalid selector */
219 if ( bundle
&& bundle
->last
+1 == i
)
224 bundle
->next
= (char *)pstr
- (char *)pModule
;
226 bundle
= (ET_BUNDLE
*)pstr
;
230 pstr
+= sizeof(ET_BUNDLE
);
233 /* FIXME: is this really correct ?? */
234 entry
= (ET_ENTRY
*)pstr
;
235 entry
->type
= 0xff; /* movable */
236 entry
->flags
= 3; /* exported & public data */
237 entry
->segnum
= selector
;
238 entry
->offs
= odp
->offset
;
239 pstr
+= sizeof(ET_ENTRY
);
244 /* Dump the module content */
246 dump_bytes( outfile
, (char *)pModule
, (int)pstr
- (int)pModule
, "Module" );
247 return (int)pstr
- (int)pModule
;
251 /*******************************************************************
252 * BuildCallFrom16Func
254 * Build a 16-bit-to-Wine callback glue function.
256 * The generated routines are intended to be used as argument conversion
257 * routines to be called by the CallFrom16... core. Thus, the prototypes of
258 * the generated routines are (see also CallFrom16):
260 * extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
261 * extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
262 * extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args,
263 * CONTEXT86 *context );
264 * extern void WINAPI PREFIX_CallFrom16_C_intr_xxx( FARPROC func, LPBYTE args,
265 * CONTEXT86 *context );
267 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
268 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
269 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
270 * 'T'=segmented pointer to null-terminated string).
272 * The generated routines fetch the arguments from the 16-bit stack (pointed
273 * to by 'args'); the offsets of the single argument values are computed
274 * according to the calling convention and the argument types. Then, the
275 * 32-bit entry point is called with these arguments.
277 * For register functions, the arguments (if present) are converted just
278 * the same as for normal functions, but in addition the CONTEXT86 pointer
279 * filled with the current register values is passed to the 32-bit routine.
280 * (An 'intr' interrupt handler routine is treated exactly like a register
281 * routine, except that upon return, the flags word pushed onto the stack
282 * by the interrupt is removed by the 16-bit call stub.)
285 static void BuildCallFrom16Func( FILE *outfile
, char *profile
, char *prefix
, int local
)
287 int i
, pos
, argsize
= 0;
291 char *args
= profile
+ 7;
294 /* Parse function type */
296 if (!strncmp( "c_", profile
, 2 )) usecdecl
= 1;
297 else if (strncmp( "p_", profile
, 2 ))
299 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
303 if (!strncmp( "word_", profile
+ 2, 5 )) short_ret
= 1;
304 else if (!strncmp( "regs_", profile
+ 2, 5 )) reg_func
= 1;
305 else if (!strncmp( "intr_", profile
+ 2, 5 )) reg_func
= 2;
306 else if (strncmp( "long_", profile
+ 2, 5 ))
308 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
312 for ( i
= 0; args
[i
]; i
++ )
316 case 's': /* s_word */
320 case 'l': /* long or segmented pointer */
321 case 'T': /* segmented pointer to null-terminated string */
322 case 'p': /* linear pointer */
323 case 't': /* linear pointer to null-terminated string */
328 ret_type
= reg_func
? "void" : short_ret
? "WORD" : "LONG";
330 fprintf( outfile
, "typedef %s WINAPI (*proc_%s_t)( ",
333 for ( i
= 0; args
[i
]; i
++ )
335 if ( i
) fprintf( outfile
, ", " );
338 case 'w': fprintf( outfile
, "WORD" ); break;
339 case 's': fprintf( outfile
, "INT16" ); break;
340 case 'l': case 'T': fprintf( outfile
, "LONG" ); break;
341 case 'p': case 't': fprintf( outfile
, "LPVOID" ); break;
345 fprintf( outfile
, "%sstruct _CONTEXT86 *", i
? ", " : "" );
347 fprintf( outfile
, "void" );
348 fprintf( outfile
, " );\n" );
350 fprintf( outfile
, "%s%s WINAPI %s_CallFrom16_%s( FARPROC proc, LPBYTE args%s )\n{\n",
351 local
? "static " : "", ret_type
, prefix
, profile
,
352 reg_func
? ", struct _CONTEXT86 *context" : "" );
354 fprintf( outfile
, " %s((proc_%s_t) proc) (\n",
355 reg_func
? "" : "return ", profile
);
357 pos
= !usecdecl
? argsize
: 0;
358 for ( i
= 0; args
[i
]; i
++ )
360 if ( i
) fprintf( outfile
, ",\n" );
361 fprintf( outfile
, " " );
365 if ( !usecdecl
) pos
-= 2;
366 fprintf( outfile
, "*(WORD *)(args+%d)", pos
);
367 if ( usecdecl
) pos
+= 2;
370 case 's': /* s_word */
371 if ( !usecdecl
) pos
-= 2;
372 fprintf( outfile
, "*(INT16 *)(args+%d)", pos
);
373 if ( usecdecl
) pos
+= 2;
376 case 'l': /* long or segmented pointer */
377 case 'T': /* segmented pointer to null-terminated string */
378 if ( !usecdecl
) pos
-= 4;
379 fprintf( outfile
, "*(LONG *)(args+%d)", pos
);
380 if ( usecdecl
) pos
+= 4;
383 case 'p': /* linear pointer */
384 case 't': /* linear pointer to null-terminated string */
385 if ( !usecdecl
) pos
-= 4;
386 fprintf( outfile
, "PTR_SEG_TO_LIN( *(SEGPTR *)(args+%d) )", pos
);
387 if ( usecdecl
) pos
+= 4;
391 fprintf( stderr
, "Unknown arg type '%c'\n", args
[i
] );
395 fprintf( outfile
, "%s context", i
? ",\n" : "" );
396 fprintf( outfile
, " );\n}\n\n" );
400 /*******************************************************************
403 * Build a Wine-to-16-bit callback glue function.
405 * Prototypes for the CallTo16 functions:
406 * extern WORD CALLBACK PREFIX_CallTo16_word_xxx( FARPROC16 func, args... );
407 * extern LONG CALLBACK PREFIX_CallTo16_long_xxx( FARPROC16 func, args... );
409 * These routines are provided solely for convenience; they simply
410 * write the arguments onto the 16-bit stack, and call the appropriate
411 * CallTo16... core routine.
413 * If you have more sophisticated argument conversion requirements than
414 * are provided by these routines, you might as well call the core
415 * routines by yourself.
418 static void BuildCallTo16Func( FILE *outfile
, char *profile
, char *prefix
)
420 char *args
= profile
+ 5;
421 int i
, argsize
= 0, short_ret
= 0;
423 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
424 else if (strncmp( "long_", profile
, 5 ))
426 fprintf( stderr
, "Invalid function name '%s'.\n", profile
);
430 fprintf( outfile
, "%s %s_CallTo16_%s( FARPROC16 proc",
431 short_ret
? "WORD" : "LONG", prefix
, profile
);
433 for ( i
= 0; args
[i
]; i
++ )
435 fprintf( outfile
, ", " );
438 case 'w': fprintf( outfile
, "WORD" ); argsize
+= 2; break;
439 case 'l': fprintf( outfile
, "LONG" ); argsize
+= 4; break;
441 fprintf( outfile
, " arg%d", i
+1 );
443 fprintf( outfile
, " )\n{\n" );
446 fprintf( outfile
, " LPBYTE args = (LPBYTE)CURRENT_STACK16;\n" );
449 for ( i
= 0; args
[i
]; i
++ )
453 case 'w': fprintf( outfile
, " args -= sizeof(WORD); *(WORD" ); break;
454 case 'l': fprintf( outfile
, " args -= sizeof(LONG); *(LONG" ); break;
455 default: fprintf( stderr
, "Unexpected case '%c' in BuildCallTo16Func\n",
458 fprintf( outfile
, " *)args = arg%d;\n", i
+1 );
461 fprintf( outfile
, " return CallTo16%s( proc, %d );\n}\n\n",
462 short_ret
? "Word" : "Long", argsize
);
466 /*******************************************************************
469 static int Spec16TypeCompare( const void *e1
, const void *e2
)
471 const ORDDEF
*odp1
= *(const ORDDEF
**)e1
;
472 const ORDDEF
*odp2
= *(const ORDDEF
**)e2
;
474 int type1
= (odp1
->type
== TYPE_CDECL
) ? 0
475 : (odp1
->type
== TYPE_REGISTER
) ? 3
476 : (odp1
->type
== TYPE_INTERRUPT
) ? 4
477 : (odp1
->type
== TYPE_PASCAL_16
) ? 1 : 2;
479 int type2
= (odp2
->type
== TYPE_CDECL
) ? 0
480 : (odp2
->type
== TYPE_REGISTER
) ? 3
481 : (odp2
->type
== TYPE_INTERRUPT
) ? 4
482 : (odp2
->type
== TYPE_PASCAL_16
) ? 1 : 2;
484 int retval
= type1
- type2
;
486 retval
= strcmp( odp1
->u
.func
.arg_types
, odp2
->u
.func
.arg_types
);
492 /*******************************************************************
495 * Build a Win16 assembly file from a spec file.
497 void BuildSpec16File( FILE *outfile
)
499 ORDDEF
**type
, **typelist
;
500 int i
, nFuncs
, nTypes
;
501 int code_offset
, data_offset
, module_size
;
506 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
508 fprintf( outfile
, "#define __FLATCS__ 0x%04x\n", code_selector
);
509 fprintf( outfile
, "#include \"builtin16.h\"\n\n" );
511 data
= (unsigned char *)xmalloc( 0x10000 );
512 memset( data
, 0, 16 );
516 /* Build sorted list of all argument types, without duplicates */
518 typelist
= (ORDDEF
**)calloc( Limit
+1, sizeof(ORDDEF
*) );
520 for (i
= nFuncs
= 0; i
<= Limit
; i
++)
522 ORDDEF
*odp
= Ordinals
[i
];
532 typelist
[nFuncs
++] = odp
;
539 qsort( typelist
, nFuncs
, sizeof(ORDDEF
*), Spec16TypeCompare
);
544 typelist
[nTypes
++] = typelist
[i
++];
545 while ( i
< nFuncs
&& Spec16TypeCompare( typelist
+ i
, typelist
+ nTypes
-1 ) == 0 )
549 /* Output CallFrom16 routines needed by this .spec file */
551 for ( i
= 0; i
< nTypes
; i
++ )
555 sprintf( profile
, "%s_%s_%s",
556 (typelist
[i
]->type
== TYPE_CDECL
) ? "c" : "p",
557 (typelist
[i
]->type
== TYPE_REGISTER
) ? "regs" :
558 (typelist
[i
]->type
== TYPE_INTERRUPT
) ? "intr" :
559 (typelist
[i
]->type
== TYPE_PASCAL_16
) ? "word" : "long",
560 typelist
[i
]->u
.func
.arg_types
);
562 BuildCallFrom16Func( outfile
, profile
, DLLName
, TRUE
);
565 /* Output the DLL functions prototypes */
567 for (i
= 0; i
<= Limit
; i
++)
569 ORDDEF
*odp
= Ordinals
[i
];
578 fprintf( outfile
, "extern void %s();\n", odp
->u
.func
.link_name
);
585 /* Output code segment */
587 fprintf( outfile
, "\nstatic struct\n{\n CALLFROM16 call[%d];\n"
588 " ENTRYPOINT16 entry[%d];\n} Code_Segment = \n{\n {\n",
592 for ( i
= 0; i
< nTypes
; i
++ )
594 char profile
[101], *arg
;
597 sprintf( profile
, "%s_%s_%s",
598 (typelist
[i
]->type
== TYPE_CDECL
) ? "c" : "p",
599 (typelist
[i
]->type
== TYPE_REGISTER
) ? "regs" :
600 (typelist
[i
]->type
== TYPE_INTERRUPT
) ? "intr" :
601 (typelist
[i
]->type
== TYPE_PASCAL_16
) ? "word" : "long",
602 typelist
[i
]->u
.func
.arg_types
);
604 if ( typelist
[i
]->type
!= TYPE_CDECL
)
605 for ( arg
= typelist
[i
]->u
.func
.arg_types
; *arg
; arg
++ )
609 case 's': /* s_word */
613 case 'l': /* long or segmented pointer */
614 case 'T': /* segmented pointer to null-terminated string */
615 case 'p': /* linear pointer */
616 case 't': /* linear pointer to null-terminated string */
621 if ( typelist
[i
]->type
== TYPE_INTERRUPT
)
624 fprintf( outfile
, " CF16_%s( %s_CallFrom16_%s, %d, \"%s\" ),\n",
625 ( typelist
[i
]->type
== TYPE_REGISTER
626 || typelist
[i
]->type
== TYPE_INTERRUPT
)? "REGS":
627 typelist
[i
]->type
== TYPE_PASCAL_16
? "WORD" : "LONG",
628 DLLName
, profile
, argsize
, profile
);
630 code_offset
+= sizeof(CALLFROM16
);
632 fprintf( outfile
, " },\n {\n" );
634 for (i
= 0; i
<= Limit
; i
++)
636 ORDDEF
*odp
= Ordinals
[i
];
641 odp
->offset
= LOWORD(odp
->u
.abs
.value
);
645 odp
->offset
= data_offset
;
646 data_offset
+= StoreVariableCode( data
+ data_offset
, 1, odp
);
650 odp
->offset
= data_offset
;
651 data_offset
+= StoreVariableCode( data
+ data_offset
, 2, odp
);
655 odp
->offset
= data_offset
;
656 data_offset
+= StoreVariableCode( data
+ data_offset
, 4, odp
);
665 type
= bsearch( &odp
, typelist
, nTypes
, sizeof(ORDDEF
*), Spec16TypeCompare
);
668 fprintf( outfile
, " /* %s.%d */ ", DLLName
, i
);
669 fprintf( outfile
, "EP( %s, %d /* %s_%s_%s */ ),\n",
670 odp
->u
.func
.link_name
,
671 (type
-typelist
)*sizeof(CALLFROM16
) -
672 (code_offset
+ sizeof(ENTRYPOINT16
)),
673 (odp
->type
== TYPE_CDECL
) ? "c" : "p",
674 (odp
->type
== TYPE_REGISTER
) ? "regs" :
675 (odp
->type
== TYPE_INTERRUPT
) ? "intr" :
676 (odp
->type
== TYPE_PASCAL_16
) ? "word" : "long",
677 odp
->u
.func
.arg_types
);
679 odp
->offset
= code_offset
;
680 code_offset
+= sizeof(ENTRYPOINT16
);
684 fprintf(stderr
,"build: function type %d not available for Win16\n",
690 fprintf( outfile
, " }\n};\n" );
692 /* Output data segment */
694 dump_bytes( outfile
, data
, data_offset
, "Data_Segment" );
696 /* Build the module */
698 module_size
= BuildModule16( outfile
, code_offset
, data_offset
);
700 /* Output the DLL descriptor */
702 if (rsrc_name
[0]) fprintf( outfile
, "extern const char %s[];\n\n", rsrc_name
);
704 fprintf( outfile
, "\nstatic const BUILTIN16_DESCRIPTOR descriptor = \n{\n" );
705 fprintf( outfile
, " \"%s\",\n", DLLName
);
706 fprintf( outfile
, " Module,\n" );
707 fprintf( outfile
, " sizeof(Module),\n" );
708 fprintf( outfile
, " (BYTE *)&Code_Segment,\n" );
709 fprintf( outfile
, " (BYTE *)Data_Segment,\n" );
710 fprintf( outfile
, " %s\n", rsrc_name
[0] ? rsrc_name
: "0" );
711 fprintf( outfile
, "};\n" );
713 /* Output the DLL constructor */
715 fprintf( outfile
, "#ifdef __GNUC__\n" );
716 fprintf( outfile
, "static void %s_init(void) __attribute__((constructor));\n", DLLName
);
717 fprintf( outfile
, "#else /* defined(__GNUC__) */\n" );
718 fprintf( outfile
, "static void __asm__dummy_dll_init(void) {\n" );
719 fprintf( outfile
, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
720 fprintf( outfile
, " \"\\tcall %s_init\\n\"\n", DLLName
);
721 fprintf( outfile
, " \"\\t.previous\\n\");\n" );
722 fprintf( outfile
, "}\n" );
723 fprintf( outfile
, "#endif /* defined(__GNUC__) */\n" );
724 fprintf( outfile
, "static void %s_init(void) { BUILTIN_RegisterDLL( &descriptor ); }\n",
729 /*******************************************************************
732 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
734 void BuildGlue( FILE *outfile
, FILE *infile
)
740 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
742 fprintf( outfile
, "#include \"builtin16.h\"\n" );
743 fprintf( outfile
, "#include \"stackframe.h\"\n\n" );
745 /* Build the callback glue functions */
747 while (fgets( buffer
, sizeof(buffer
), infile
))
749 if (strstr( buffer
, "### start build ###" )) break;
751 while (fgets( buffer
, sizeof(buffer
), infile
))
754 if ( (p
= strstr( buffer
, "CallFrom16_" )) != NULL
)
756 char *q
, *profile
= p
+ strlen( "CallFrom16_" );
757 for (q
= profile
; (*q
== '_') || isalpha(*q
); q
++ )
760 for (q
= p
-1; q
> buffer
&& ((*q
== '_') || isalnum(*q
)); q
-- )
762 if ( ++q
< p
) p
[-1] = '\0'; else q
= "";
763 BuildCallFrom16Func( outfile
, profile
, q
, FALSE
);
765 if ( (p
= strstr( buffer
, "CallTo16_" )) != NULL
)
767 char *q
, *profile
= p
+ strlen( "CallTo16_" );
768 for (q
= profile
; (*q
== '_') || isalpha(*q
); q
++ )
771 for (q
= p
-1; q
> buffer
&& ((*q
== '_') || isalnum(*q
)); q
-- )
773 if ( ++q
< p
) p
[-1] = '\0'; else q
= "";
774 BuildCallTo16Func( outfile
, profile
, q
);
776 if (strstr( buffer
, "### stop build ###" )) break;