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 fprintf( outfile, "extern void RELAY_Unimplemented16(void);\n\n" );
513 data = (unsigned char *)xmalloc( 0x10000 );
514 memset( data, 0, 16 );
518 /* Build sorted list of all argument types, without duplicates */
520 typelist = (ORDDEF **)calloc( Limit+1, sizeof(ORDDEF *) );
522 for (i = nFuncs = 0; i <= Limit; i++)
524 ORDDEF *odp = Ordinals[i];
534 typelist[nFuncs++] = odp;
541 qsort( typelist, nFuncs, sizeof(ORDDEF *), Spec16TypeCompare );
546 typelist[nTypes++] = typelist[i++];
547 while ( i < nFuncs && Spec16TypeCompare( typelist + i, typelist + nTypes-1 ) == 0 )
551 /* Output CallFrom16 routines needed by this .spec file */
553 for ( i = 0; i < nTypes; i++ )
557 sprintf( profile, "%s_%s_%s",
558 (typelist[i]->type == TYPE_CDECL) ? "c" : "p",
559 (typelist[i]->type == TYPE_REGISTER) ? "regs" :
560 (typelist[i]->type == TYPE_INTERRUPT) ? "intr" :
561 (typelist[i]->type == TYPE_PASCAL_16) ? "word" : "long",
562 typelist[i]->u.func.arg_types );
564 BuildCallFrom16Func( outfile, profile, DLLName, TRUE );
567 /* Output the DLL functions prototypes */
569 for (i = 0; i <= Limit; i++)
571 ORDDEF *odp = Ordinals[i];
580 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
587 /* Output code segment */
589 fprintf( outfile, "\nstatic struct\n{\n CALLFROM16 call[%d];\n"
590 " ENTRYPOINT16 entry[%d];\n} Code_Segment = \n{\n {\n",
594 for ( i = 0; i < nTypes; i++ )
596 char profile[101], *arg;
599 sprintf( profile, "%s_%s_%s",
600 (typelist[i]->type == TYPE_CDECL) ? "c" : "p",
601 (typelist[i]->type == TYPE_REGISTER) ? "regs" :
602 (typelist[i]->type == TYPE_INTERRUPT) ? "intr" :
603 (typelist[i]->type == TYPE_PASCAL_16) ? "word" : "long",
604 typelist[i]->u.func.arg_types );
606 if ( typelist[i]->type != TYPE_CDECL )
607 for ( arg = typelist[i]->u.func.arg_types; *arg; arg++ )
611 case 's': /* s_word */
615 case 'l': /* long or segmented pointer */
616 case 'T': /* segmented pointer to null-terminated string */
617 case 'p': /* linear pointer */
618 case 't': /* linear pointer to null-terminated string */
623 if ( typelist[i]->type == TYPE_INTERRUPT )
626 fprintf( outfile, " { 0x68, %s_CallFrom16_%s, 0x9a, CallFrom16%s,\n",
628 (typelist[i]->type == TYPE_REGISTER
629 || typelist[i]->type == TYPE_INTERRUPT)? "Register":
630 typelist[i]->type == TYPE_PASCAL_16? "Word" : "Long" );
632 fprintf( outfile, " 0x%04x, 0x66, 0xca, %d, \"%s\" },\n",
633 code_selector, argsize, profile );
635 fprintf( outfile, " 0x%04x, 0x66, 0xcb, 0x9090, \"%s\" },\n",
636 code_selector, profile );
638 code_offset += sizeof(CALLFROM16);
640 fprintf( outfile, " },\n {\n" );
642 for (i = 0; i <= Limit; i++)
644 ORDDEF *odp = Ordinals[i];
649 odp->offset = LOWORD(odp->u.abs.value);
653 odp->offset = data_offset;
654 data_offset += StoreVariableCode( data + data_offset, 1, odp);
658 odp->offset = data_offset;
659 data_offset += StoreVariableCode( data + data_offset, 2, odp);
663 odp->offset = data_offset;
664 data_offset += StoreVariableCode( data + data_offset, 4, odp);
673 type = bsearch( &odp, typelist, nTypes, sizeof(ORDDEF *), Spec16TypeCompare );
676 fprintf( outfile, " /* %s.%d */ ", DLLName, i );
677 fprintf( outfile, "{ 0x5566, 0x68, %s, 0xe866, %d /* %s_%s_%s */ },\n",
678 odp->u.func.link_name,
679 (type-typelist)*sizeof(CALLFROM16) -
680 (code_offset + sizeof(ENTRYPOINT16)),
681 (odp->type == TYPE_CDECL) ? "c" : "p",
682 (odp->type == TYPE_REGISTER) ? "regs" :
683 (odp->type == TYPE_INTERRUPT) ? "intr" :
684 (odp->type == TYPE_PASCAL_16) ? "word" : "long",
685 odp->u.func.arg_types );
687 odp->offset = code_offset;
688 code_offset += sizeof(ENTRYPOINT16);
692 fprintf(stderr,"build: function type %d not available for Win16\n",
698 fprintf( outfile, " }\n};\n" );
700 /* Output data segment */
702 dump_bytes( outfile, data, data_offset, "Data_Segment" );
704 /* Build the module */
706 module_size = BuildModule16( outfile, code_offset, data_offset );
708 /* Output the DLL descriptor */
710 if (rsrc_name[0]) fprintf( outfile, "extern const char %s[];\n\n", rsrc_name );
712 fprintf( outfile, "\nstatic const BUILTIN16_DESCRIPTOR descriptor = \n{\n" );
713 fprintf( outfile, " \"%s\",\n", DLLName );
714 fprintf( outfile, " Module,\n" );
715 fprintf( outfile, " sizeof(Module),\n" );
716 fprintf( outfile, " (BYTE *)&Code_Segment,\n" );
717 fprintf( outfile, " (BYTE *)Data_Segment,\n" );
718 fprintf( outfile, " \"%s\",\n", owner_name );
719 fprintf( outfile, " %s\n", rsrc_name[0] ? rsrc_name : "0" );
720 fprintf( outfile, "};\n" );
722 /* Output the DLL constructor */
724 fprintf( outfile, "#ifdef __GNUC__\n" );
725 fprintf( outfile, "static void %s_init(void) __attribute__((constructor));\n", DLLName );
726 fprintf( outfile, "#else /* defined(__GNUC__) */\n" );
727 fprintf( outfile, "static void __asm__dummy_dll_init(void) {\n" );
728 fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
729 fprintf( outfile, " \"\\tcall %s_init\\n\"\n", DLLName );
730 fprintf( outfile, " \"\\t.previous\\n\");\n" );
731 fprintf( outfile, "}\n" );
732 fprintf( outfile, "#endif /* defined(__GNUC__) */\n" );
733 fprintf( outfile, "static void %s_init(void) { BUILTIN_RegisterDLL( &descriptor ); }\n",
738 /*******************************************************************
741 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
743 void BuildGlue( FILE *outfile, FILE *infile )
749 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
751 fprintf( outfile, "#include \"builtin16.h\"\n" );
752 fprintf( outfile, "#include \"stackframe.h\"\n\n" );
754 fprintf( outfile, "extern WORD CALLBACK CallTo16Word( FARPROC16 target, INT nArgs );\n" );
755 fprintf( outfile, "extern LONG CALLBACK CallTo16Long( FARPROC16 target, INT nArgs );\n" );
757 /* Build the callback glue functions */
759 while (fgets( buffer, sizeof(buffer), infile ))
761 if (strstr( buffer, "### start build ###" )) break;
763 while (fgets( buffer, sizeof(buffer), infile ))
766 if ( (p = strstr( buffer, "CallFrom16_" )) != NULL )
768 char *q, *profile = p + strlen( "CallFrom16_" );
769 for (q = profile; (*q == '_') || isalpha(*q); q++ )
772 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
774 if ( ++q < p ) p[-1] = '\0'; else q = "";
775 BuildCallFrom16Func( outfile, profile, q, FALSE );
777 if ( (p = strstr( buffer, "CallTo16_" )) != NULL )
779 char *q, *profile = p + strlen( "CallTo16_" );
780 for (q = profile; (*q == '_') || isalpha(*q); q++ )
783 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
785 if ( ++q < p ) p[-1] = '\0'; else q = "";
786 BuildCallTo16Func( outfile, profile, q );
788 if (strstr( buffer, "### stop build ###" )) break;