Changed the Win32 dll descriptor to be in IMAGE_NT_HEADERS format.
[wine] / tools / winebuild / spec16.c
1 /*
2  * 16-bit spec files
3  *
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
9  */
10
11 #include <assert.h>
12 #include <ctype.h>
13
14 #include "builtin16.h"
15 #include "module.h"
16 #include "neexe.h"
17 #include "stackframe.h"
18
19 #include "build.h"
20
21
22 /*******************************************************************
23  *         StoreVariableCode
24  *
25  * Store a list of ints into a byte array.
26  */
27 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
28 {
29     int i;
30
31     switch(size)
32     {
33     case 1:
34         for (i = 0; i < odp->u.var.n_values; i++)
35             buffer[i] = odp->u.var.values[i];
36         break;
37     case 2:
38         for (i = 0; i < odp->u.var.n_values; i++)
39             ((unsigned short *)buffer)[i] = odp->u.var.values[i];
40         break;
41     case 4:
42         for (i = 0; i < odp->u.var.n_values; i++)
43             ((unsigned int *)buffer)[i] = odp->u.var.values[i];
44         break;
45     }
46     return odp->u.var.n_values * size;
47 }
48
49
50 /*******************************************************************
51  *         BuildModule16
52  *
53  * Build the in-memory representation of a 16-bit NE module, and dump it
54  * as a byte stream into the assembly code.
55  */
56 static int BuildModule16( FILE *outfile, int max_code_offset,
57                           int max_data_offset )
58 {
59     int i;
60     char *buffer;
61     NE_MODULE *pModule;
62     SEGTABLEENTRY *pSegment;
63     OFSTRUCT *pFileInfo;
64     BYTE *pstr;
65     WORD *pword;
66     ET_BUNDLE *bundle = 0;
67     ET_ENTRY *entry = 0;
68
69     /*   Module layout:
70      * NE_MODULE       Module
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
77      * BYTE[n]         Entry table
78      */
79
80     buffer = xmalloc( 0x10000 );
81
82     pModule = (NE_MODULE *)buffer;
83     memset( pModule, 0, sizeof(*pModule) );
84     pModule->magic = IMAGE_OS2_SIGNATURE;
85     pModule->count = 1;
86     pModule->next = 0;
87     pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
88     pModule->dgroup = 2;
89     pModule->heap_size = DLLHeapSize;
90     pModule->stack_size = 0;
91     pModule->ip = 0;
92     pModule->cs = 0;
93     pModule->sp = 0;
94     pModule->ss = 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;
110     pModule->self = 0;
111     pModule->self_loading_sel = 0;
112
113       /* File information */
114
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;
122         
123 #ifdef __i386__  /* FIXME: Alignment problems! */
124
125       /* Segment table */
126
127     pSegment = (SEGTABLEENTRY *)pstr;
128     pModule->seg_table = (int)pSegment - (int)pModule;
129     pSegment->filepos = 0;
130     pSegment->size = max_code_offset;
131     pSegment->flags = 0;
132     pSegment->minsize = max_code_offset;
133     pSegment->hSeg = 0;
134     pSegment++;
135
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;
141     pSegment->hSeg = 0;
142     pSegment++;
143
144       /* Resource table */
145
146     pword = (WORD *)pSegment;
147     pModule->res_table = (int)pword - (int)pModule;
148     *pword++ = 0;
149     *pword++ = 0;
150
151       /* Imported names table */
152
153     pstr = (char *)pword;
154     pModule->import_table = (int)pstr - (int)pModule;
155     *pstr++ = 0;
156     *pstr++ = 0;
157
158       /* Resident names table */
159
160     pModule->name_table = (int)pstr - (int)pModule;
161     /* First entry is module name */
162     *pstr = strlen(DLLName );
163     strcpy( pstr + 1, DLLName );
164     pstr += *pstr + 1;
165     *(WORD *)pstr = 0;
166     pstr += sizeof(WORD);
167     /* Store all ordinals */
168     for (i = 1; i <= Limit; i++)
169     {
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 );
175         pstr += *pstr + 1;
176         *(WORD *)pstr = i;
177         pstr += sizeof(WORD);
178     }
179     *pstr++ = 0;
180
181       /* Entry table */
182
183     pModule->entry_table = (int)pstr - (int)pModule;
184     for (i = 1; i <= Limit; i++)
185     {
186         int selector = 0;
187         ORDDEF *odp = Ordinals[i];
188         if (!odp) continue;
189
190         switch (odp->type)
191         {
192         case TYPE_CDECL:
193         case TYPE_PASCAL:
194         case TYPE_PASCAL_16:
195         case TYPE_REGISTER:
196         case TYPE_INTERRUPT:
197         case TYPE_STUB:
198             selector = 1;  /* Code selector */
199             break;
200
201         case TYPE_BYTE:
202         case TYPE_WORD:
203         case TYPE_LONG:
204             selector = 2;  /* Data selector */
205             break;
206
207         case TYPE_ABS:
208             selector = 0xfe;  /* Constant selector */
209             break;
210
211         default:
212             selector = 0;  /* Invalid selector */
213             break;
214         }
215
216         if ( !selector )
217            continue;
218
219         if ( bundle && bundle->last+1 == i )
220             bundle->last++;
221         else
222         {
223             if ( bundle )
224                 bundle->next = (char *)pstr - (char *)pModule;
225
226             bundle = (ET_BUNDLE *)pstr;
227             bundle->first = i-1;
228             bundle->last = i;
229             bundle->next = 0;
230             pstr += sizeof(ET_BUNDLE);
231         }
232
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);
240     }
241     *pstr++ = 0;
242 #endif
243
244       /* Dump the module content */
245
246     dump_bytes( outfile, (char *)pModule, (int)pstr - (int)pModule, "Module" );
247     return (int)pstr - (int)pModule;
248 }
249
250
251 /*******************************************************************
252  *         BuildCallFrom16Func
253  *
254  * Build a 16-bit-to-Wine callback glue function. 
255  *
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):
259  *
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 );
266  *
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).
271  *
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.
276  * 
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.)
283  *
284  */
285 static void BuildCallFrom16Func( FILE *outfile, char *profile, char *prefix, int local )
286 {
287     int i, pos, argsize = 0;
288     int short_ret = 0;
289     int reg_func = 0;
290     int usecdecl = 0;
291     char *args = profile + 7;
292     char *ret_type;
293
294     /* Parse function type */
295
296     if (!strncmp( "c_", profile, 2 )) usecdecl = 1;
297     else if (strncmp( "p_", profile, 2 ))
298     {
299         fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
300         return;
301     }
302
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 ))
307     {
308         fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
309         return;
310     }
311
312     for ( i = 0; args[i]; i++ )
313         switch ( args[i] )
314         {
315         case 'w':  /* word */
316         case 's':  /* s_word */
317             argsize += 2;
318             break;
319         
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 */
324             argsize += 4;
325             break;
326         }
327
328     ret_type = reg_func? "void" : short_ret? "WORD" : "LONG";
329
330     fprintf( outfile, "typedef %s WINAPI (*proc_%s_t)( ", 
331                       ret_type, profile );
332     args = profile + 7;
333     for ( i = 0; args[i]; i++ )
334     {
335         if ( i ) fprintf( outfile, ", " );
336         switch (args[i])
337         {
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;
342         }
343     }
344     if ( reg_func )
345         fprintf( outfile, "%sstruct _CONTEXT86 *", i? ", " : "" );
346     else if ( !i )
347         fprintf( outfile, "void" );
348     fprintf( outfile, " );\n" );
349     
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" : "" );
353
354     fprintf( outfile, "    %s((proc_%s_t) proc) (\n",
355              reg_func? "" : "return ", profile );
356     args = profile + 7;
357     pos = !usecdecl? argsize : 0;
358     for ( i = 0; args[i]; i++ )
359     {
360         if ( i ) fprintf( outfile, ",\n" );
361         fprintf( outfile, "        " );
362         switch (args[i])
363         {
364         case 'w':  /* word */
365             if ( !usecdecl ) pos -= 2;
366             fprintf( outfile, "*(WORD *)(args+%d)", pos );
367             if (  usecdecl ) pos += 2;
368             break;
369
370         case 's':  /* s_word */
371             if ( !usecdecl ) pos -= 2;
372             fprintf( outfile, "*(INT16 *)(args+%d)", pos );
373             if (  usecdecl ) pos += 2;
374             break;
375
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;
381             break;
382
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;
388             break;
389
390         default:
391             fprintf( stderr, "Unknown arg type '%c'\n", args[i] );
392         }
393     }
394     if ( reg_func )
395         fprintf( outfile, "%s        context", i? ",\n" : "" );
396     fprintf( outfile, " );\n}\n\n" );
397 }
398
399
400 /*******************************************************************
401  *         BuildCallTo16Func
402  *
403  * Build a Wine-to-16-bit callback glue function. 
404  *
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... );
408  * 
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.
412  *
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.
416  *
417  */
418 static void BuildCallTo16Func( FILE *outfile, char *profile, char *prefix )
419 {
420     char *args = profile + 5;
421     int i, argsize = 0, short_ret = 0;
422
423     if (!strncmp( "word_", profile, 5 )) short_ret = 1;
424     else if (strncmp( "long_", profile, 5 ))
425     {
426         fprintf( stderr, "Invalid function name '%s'.\n", profile );
427         exit(1);
428     }
429
430     fprintf( outfile, "%s %s_CallTo16_%s( FARPROC16 proc",
431              short_ret? "WORD" : "LONG", prefix, profile );
432     args = profile + 5;
433     for ( i = 0; args[i]; i++ )
434     {
435         fprintf( outfile, ", " );
436         switch (args[i])
437         {
438         case 'w': fprintf( outfile, "WORD" ); argsize += 2; break;
439         case 'l': fprintf( outfile, "LONG" ); argsize += 4; break;
440         }
441         fprintf( outfile, " arg%d", i+1 );
442     }
443     fprintf( outfile, " )\n{\n" );
444
445     if ( argsize > 0 )
446         fprintf( outfile, "    LPBYTE args = (LPBYTE)CURRENT_STACK16;\n" );
447
448     args = profile + 5;
449     for ( i = 0; args[i]; i++ )
450     {
451         switch (args[i])
452         {
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",
456                                    args[i] );
457         }
458         fprintf( outfile, " *)args = arg%d;\n", i+1 );
459     }
460
461     fprintf( outfile, "    return CallTo16%s( proc, %d );\n}\n\n",
462              short_ret? "Word" : "Long", argsize );
463 }
464
465
466 /*******************************************************************
467  *         Spec16TypeCompare
468  */
469 static int Spec16TypeCompare( const void *e1, const void *e2 )
470 {
471     const ORDDEF *odp1 = *(const ORDDEF **)e1;
472     const ORDDEF *odp2 = *(const ORDDEF **)e2;
473
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;
478
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;
483
484     int retval = type1 - type2;
485     if ( !retval )
486         retval = strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
487
488     return retval;
489 }
490
491
492 /*******************************************************************
493  *         BuildSpec16File
494  *
495  * Build a Win16 assembly file from a spec file.
496  */
497 void BuildSpec16File( FILE *outfile )
498 {
499     ORDDEF **type, **typelist;
500     int i, nFuncs, nTypes;
501     int code_offset, data_offset, module_size;
502     unsigned char *data;
503
504     /* File header */
505
506     fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
507              input_file_name );
508     fprintf( outfile, "#define __FLATCS__ 0x%04x\n", code_selector );
509     fprintf( outfile, "#include \"builtin16.h\"\n\n" );
510
511     fprintf( outfile, "extern void RELAY_Unimplemented16(void);\n\n" );
512
513     data = (unsigned char *)xmalloc( 0x10000 );
514     memset( data, 0, 16 );
515     data_offset = 16;
516     strupper( DLLName );
517
518     /* Build sorted list of all argument types, without duplicates */
519
520     typelist = (ORDDEF **)calloc( Limit+1, sizeof(ORDDEF *) );
521
522     for (i = nFuncs = 0; i <= Limit; i++)
523     {
524         ORDDEF *odp = Ordinals[i];
525         if (!odp) continue;
526         switch (odp->type)
527         {
528           case TYPE_REGISTER:
529           case TYPE_INTERRUPT:
530           case TYPE_CDECL:
531           case TYPE_PASCAL:
532           case TYPE_PASCAL_16:
533           case TYPE_STUB:
534             typelist[nFuncs++] = odp;
535
536           default:
537             break;
538         }
539     }
540
541     qsort( typelist, nFuncs, sizeof(ORDDEF *), Spec16TypeCompare );
542
543     i = nTypes = 0;
544     while ( i < nFuncs )
545     {
546         typelist[nTypes++] = typelist[i++];
547         while ( i < nFuncs && Spec16TypeCompare( typelist + i, typelist + nTypes-1 ) == 0 )
548             i++;
549     }
550
551     /* Output CallFrom16 routines needed by this .spec file */
552
553     for ( i = 0; i < nTypes; i++ )
554     {
555         char profile[101];
556
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 );
563
564         BuildCallFrom16Func( outfile, profile, DLLName, TRUE );
565     }
566
567     /* Output the DLL functions prototypes */
568
569     for (i = 0; i <= Limit; i++)
570     {
571         ORDDEF *odp = Ordinals[i];
572         if (!odp) continue;
573         switch(odp->type)
574         {
575         case TYPE_REGISTER:
576         case TYPE_INTERRUPT:
577         case TYPE_CDECL:
578         case TYPE_PASCAL:
579         case TYPE_PASCAL_16:
580             fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
581             break;
582         default:
583             break;
584         }
585     }
586
587     /* Output code segment */
588
589     fprintf( outfile, "\nstatic struct\n{\n    CALLFROM16   call[%d];\n"
590                       "    ENTRYPOINT16 entry[%d];\n} Code_Segment = \n{\n  {\n",
591                       nTypes, nFuncs );
592     code_offset = 0;
593
594     for ( i = 0; i < nTypes; i++ )
595     {
596         char profile[101], *arg;
597         int argsize = 0;
598
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 );
605
606         if ( typelist[i]->type != TYPE_CDECL )
607             for ( arg = typelist[i]->u.func.arg_types; *arg; arg++ )
608                 switch ( *arg )
609                 {
610                 case 'w':  /* word */
611                 case 's':  /* s_word */
612                     argsize += 2;
613                     break;
614         
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 */
619                     argsize += 4;
620                     break;
621                 }
622
623         if ( typelist[i]->type == TYPE_INTERRUPT )
624             argsize += 2;
625
626         fprintf( outfile, "    { 0x68, %s_CallFrom16_%s, 0x9a, CallFrom16%s,\n",
627                  DLLName, profile, 
628                  (typelist[i]->type == TYPE_REGISTER 
629                   || typelist[i]->type == TYPE_INTERRUPT)? "Register":
630                  typelist[i]->type == TYPE_PASCAL_16? "Word" : "Long" );
631         if (argsize)
632             fprintf( outfile, "        0x%04x, 0x66, 0xca, %d, \"%s\" },\n",
633                      code_selector, argsize, profile );
634         else
635             fprintf( outfile, "        0x%04x, 0x66, 0xcb, 0x9090, \"%s\" },\n",
636                      code_selector, profile );
637
638         code_offset += sizeof(CALLFROM16);
639     }
640     fprintf( outfile, "  },\n  {\n" );
641
642     for (i = 0; i <= Limit; i++)
643     {
644         ORDDEF *odp = Ordinals[i];
645         if (!odp) continue;
646         switch (odp->type)
647         {
648           case TYPE_ABS:
649             odp->offset = LOWORD(odp->u.abs.value);
650             break;
651
652           case TYPE_BYTE:
653             odp->offset = data_offset;
654             data_offset += StoreVariableCode( data + data_offset, 1, odp);
655             break;
656
657           case TYPE_WORD:
658             odp->offset = data_offset;
659             data_offset += StoreVariableCode( data + data_offset, 2, odp);
660             break;
661
662           case TYPE_LONG:
663             odp->offset = data_offset;
664             data_offset += StoreVariableCode( data + data_offset, 4, odp);
665             break;
666
667           case TYPE_REGISTER:
668           case TYPE_INTERRUPT:
669           case TYPE_CDECL:
670           case TYPE_PASCAL:
671           case TYPE_PASCAL_16:
672           case TYPE_STUB:
673             type = bsearch( &odp, typelist, nTypes, sizeof(ORDDEF *), Spec16TypeCompare );
674             assert( type );
675
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 );
686                                  
687             odp->offset = code_offset;
688             code_offset += sizeof(ENTRYPOINT16);
689             break;
690                 
691           default:
692             fprintf(stderr,"build: function type %d not available for Win16\n",
693                     odp->type);
694             exit(1);
695         }
696     }
697
698     fprintf( outfile, "    }\n};\n" );
699
700     /* Output data segment */
701
702     dump_bytes( outfile, data, data_offset, "Data_Segment" );
703
704     /* Build the module */
705
706     module_size = BuildModule16( outfile, code_offset, data_offset );
707
708     /* Output the DLL descriptor */
709
710     if (rsrc_name[0]) fprintf( outfile, "extern const char %s[];\n\n", rsrc_name );
711
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" );
721     
722     /* Output the DLL constructor */
723
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",
734              DLLName );
735 }
736
737
738 /*******************************************************************
739  *         BuildGlue
740  *
741  * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
742  */
743 void BuildGlue( FILE *outfile, FILE *infile )
744 {
745     char buffer[1024];
746
747     /* File header */
748
749     fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
750              input_file_name );
751     fprintf( outfile, "#include \"builtin16.h\"\n" );
752     fprintf( outfile, "#include \"stackframe.h\"\n\n" );
753
754     fprintf( outfile, "extern WORD CALLBACK CallTo16Word( FARPROC16 target, INT nArgs );\n" );
755     fprintf( outfile, "extern LONG CALLBACK CallTo16Long( FARPROC16 target, INT nArgs );\n" );
756
757     /* Build the callback glue functions */
758
759     while (fgets( buffer, sizeof(buffer), infile ))
760     {
761         if (strstr( buffer, "### start build ###" )) break;
762     }
763     while (fgets( buffer, sizeof(buffer), infile ))
764     {
765         char *p; 
766         if ( (p = strstr( buffer, "CallFrom16_" )) != NULL )
767         {
768             char *q, *profile = p + strlen( "CallFrom16_" );
769             for (q = profile; (*q == '_') || isalpha(*q); q++ )
770                 ;
771             *q = '\0';
772             for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
773                 ;
774             if ( ++q < p ) p[-1] = '\0'; else q = "";
775             BuildCallFrom16Func( outfile, profile, q, FALSE );
776         }
777         if ( (p = strstr( buffer, "CallTo16_" )) != NULL )
778         {
779             char *q, *profile = p + strlen( "CallTo16_" );
780             for (q = profile; (*q == '_') || isalpha(*q); q++ )
781                 ;
782             *q = '\0';
783             for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
784                 ;
785             if ( ++q < p ) p[-1] = '\0'; else q = "";
786             BuildCallTo16Func( outfile, profile, q );
787         }
788         if (strstr( buffer, "### stop build ###" )) break;
789     }
790
791     fclose( infile );
792 }