Generate 16-bit resources in the proper format inside the module data,
[wine/dcerpc.git] / tools / winebuild / spec16.c
blob6266327452fecc136b3653296dcf9bcf2e91b478
1 /*
2 * 16-bit spec files
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
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <ctype.h>
31 #include "wine/exception.h"
32 #include "wine/winbase16.h"
34 #include "build.h"
37 /*******************************************************************
38 * get_cs
40 #ifdef __i386__
41 static inline unsigned short get_cs(void)
43 unsigned short res;
44 #ifdef __GNUC__
45 __asm__("movw %%cs,%w0" : "=r"(res));
46 #elif defined(_MSC_VER)
47 __asm { mov res, cs }
48 #else
49 res = 0;
50 #endif
51 return res;
53 #endif /* __i386__ */
56 /*******************************************************************
57 * output_file_header
59 * Output a file header with the common declarations we need.
61 static void output_file_header( FILE *outfile )
63 output_standard_file_header( outfile );
64 fprintf( outfile, "extern struct\n{\n" );
65 fprintf( outfile, " void *base[8192];\n" );
66 fprintf( outfile, " unsigned long limit[8192];\n" );
67 fprintf( outfile, " unsigned char flags[8192];\n" );
68 fprintf( outfile, "} wine_ldt_copy;\n\n" );
69 #ifdef __i386__
70 fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
71 #else
72 fprintf( outfile, "#define __stdcall\n\n" );
73 #endif
77 /*******************************************************************
78 * StoreVariableCode
80 * Store a list of ints into a byte array.
82 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
84 int i;
86 switch(size)
88 case 1:
89 for (i = 0; i < odp->u.var.n_values; i++)
90 buffer[i] = odp->u.var.values[i];
91 break;
92 case 2:
93 for (i = 0; i < odp->u.var.n_values; i++)
94 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
95 break;
96 case 4:
97 for (i = 0; i < odp->u.var.n_values; i++)
98 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
99 break;
101 return odp->u.var.n_values * size;
105 /*******************************************************************
106 * output_entry_table
108 static int output_entry_table( unsigned char **ret_buff, DLLSPEC *spec )
110 int i, prev = 0, prev_sel = -1;
111 unsigned char *pstr, *buffer;
112 unsigned char *bundle = NULL;
114 buffer = xmalloc( spec->limit * 5 ); /* we use at most 5 bytes per entry-point */
115 pstr = buffer;
117 for (i = 1; i <= spec->limit; i++)
119 int selector = 0;
120 WORD offset;
121 ORDDEF *odp = spec->ordinals[i];
122 if (!odp) continue;
124 switch (odp->type)
126 case TYPE_CDECL:
127 case TYPE_PASCAL:
128 case TYPE_VARARGS:
129 case TYPE_STUB:
130 selector = 1; /* Code selector */
131 break;
132 case TYPE_VARIABLE:
133 selector = 2; /* Data selector */
134 break;
135 case TYPE_ABS:
136 selector = 0xfe; /* Constant selector */
137 break;
138 default:
139 continue;
142 if (!bundle || prev + 1 != i || prev_sel != selector || *bundle == 255)
144 /* need to start a new bundle */
146 if (prev + 1 != i)
148 int skip = i - (prev + 1);
149 while (skip > 255)
151 *pstr++ = 255;
152 *pstr++ = 0;
153 skip -= 255;
155 *pstr++ = skip;
156 *pstr++ = 0;
159 bundle = pstr;
160 *pstr++ = 0;
161 *pstr++ = selector;
162 prev_sel = selector;
164 /* output the entry */
165 *pstr++ = 3; /* flags: exported & public data */
166 offset = odp->offset;
167 memcpy( pstr, &offset, sizeof(WORD) );
168 pstr += sizeof(WORD);
169 (*bundle)++; /* increment bundle entry count */
170 prev = i;
172 *pstr++ = 0;
173 if ((pstr - buffer) & 1) *pstr++ = 0;
174 *ret_buff = xrealloc( buffer, pstr - buffer );
175 return pstr - buffer;
179 /*******************************************************************
180 * output_bytes
182 static void output_bytes( FILE *outfile, const void *buffer, unsigned int size )
184 unsigned int i;
185 const unsigned char *ptr = buffer;
187 fprintf( outfile, " {" );
188 for (i = 0; i < size; i++)
190 if (!(i & 7)) fprintf( outfile, "\n " );
191 fprintf( outfile, " 0x%02x", *ptr++ );
192 if (i < size - 1) fprintf( outfile, "," );
194 fprintf( outfile, "\n },\n" );
198 /*******************************************************************
199 * output_module_data
201 * Output the 16-bit NE module structure.
203 static void output_module_data( FILE *outfile, int max_code_offset, const void *data_segment,
204 unsigned int data_size, DLLSPEC *spec )
206 unsigned char *resdir_buffer, *resdata_buffer, *et_buffer;
207 unsigned char string[256];
208 int i;
209 unsigned int ne_offset, segtable_offset, impnames_offset, data_offset;
210 unsigned int resnames_offset, resnames_size;
211 unsigned int resdir_size, resdir_offset;
212 unsigned int resdata_size, resdata_offset, resdata_align;
213 unsigned int et_size, et_offset;
215 /* DOS header */
217 fprintf( outfile, "static const struct module_data\n{\n" );
218 fprintf( outfile, " struct\n {\n" );
219 fprintf( outfile, " unsigned short e_magic;\n" );
220 fprintf( outfile, " unsigned short e_cblp;\n" );
221 fprintf( outfile, " unsigned short e_cp;\n" );
222 fprintf( outfile, " unsigned short e_crlc;\n" );
223 fprintf( outfile, " unsigned short e_cparhdr;\n" );
224 fprintf( outfile, " unsigned short e_minalloc;\n" );
225 fprintf( outfile, " unsigned short e_maxalloc;\n" );
226 fprintf( outfile, " unsigned short e_ss;\n" );
227 fprintf( outfile, " unsigned short e_sp;\n" );
228 fprintf( outfile, " unsigned short e_csum;\n" );
229 fprintf( outfile, " unsigned short e_ip;\n" );
230 fprintf( outfile, " unsigned short e_cs;\n" );
231 fprintf( outfile, " unsigned short e_lfarlc;\n" );
232 fprintf( outfile, " unsigned short e_ovno;\n" );
233 fprintf( outfile, " unsigned short e_res[4];\n" );
234 fprintf( outfile, " unsigned short e_oemid;\n" );
235 fprintf( outfile, " unsigned short e_oeminfo;\n" );
236 fprintf( outfile, " unsigned short e_res2[10];\n" );
237 fprintf( outfile, " unsigned int e_lfanew;\n" );
238 fprintf( outfile, " } dos_header;\n" );
240 /* NE header */
242 ne_offset = 64;
243 fprintf( outfile, " struct\n {\n" );
244 fprintf( outfile, " unsigned short ne_magic;\n" );
245 fprintf( outfile, " unsigned char ne_ver;\n" );
246 fprintf( outfile, " unsigned char ne_rev;\n" );
247 fprintf( outfile, " unsigned short ne_enttab;\n" );
248 fprintf( outfile, " unsigned short ne_cbenttab;\n" );
249 fprintf( outfile, " int ne_crc;\n" );
250 fprintf( outfile, " unsigned short ne_flags;\n" );
251 fprintf( outfile, " unsigned short ne_autodata;\n" );
252 fprintf( outfile, " unsigned short ne_heap;\n" );
253 fprintf( outfile, " unsigned short ne_stack;\n" );
254 fprintf( outfile, " unsigned int ne_csip;\n" );
255 fprintf( outfile, " unsigned int ne_sssp;\n" );
256 fprintf( outfile, " unsigned short ne_cseg;\n" );
257 fprintf( outfile, " unsigned short ne_cmod;\n" );
258 fprintf( outfile, " unsigned short ne_cbnrestab;\n" );
259 fprintf( outfile, " unsigned short ne_segtab;\n" );
260 fprintf( outfile, " unsigned short ne_rsrctab;\n" );
261 fprintf( outfile, " unsigned short ne_restab;\n" );
262 fprintf( outfile, " unsigned short ne_modtab;\n" );
263 fprintf( outfile, " unsigned short ne_imptab;\n" );
264 fprintf( outfile, " unsigned int ne_nrestab;\n" );
265 fprintf( outfile, " unsigned short ne_cmovent;\n" );
266 fprintf( outfile, " unsigned short ne_align;\n" );
267 fprintf( outfile, " unsigned short ne_cres;\n" );
268 fprintf( outfile, " unsigned char ne_exetyp;\n" );
269 fprintf( outfile, " unsigned char ne_flagsothers;\n" );
270 fprintf( outfile, " unsigned short ne_pretthunks;\n" );
271 fprintf( outfile, " unsigned short ne_psegrefbytes;\n" );
272 fprintf( outfile, " unsigned short ne_swaparea;\n" );
273 fprintf( outfile, " unsigned short ne_expver;\n" );
274 fprintf( outfile, " } os2_header;\n" );
276 /* segment table */
278 segtable_offset = 64;
279 fprintf( outfile, " struct\n {\n" );
280 fprintf( outfile, " unsigned short filepos;\n" );
281 fprintf( outfile, " unsigned short size;\n" );
282 fprintf( outfile, " unsigned short flags;\n" );
283 fprintf( outfile, " unsigned short minsize;\n" );
284 fprintf( outfile, " } segtable[2];\n" );
286 /* resource directory */
288 resdir_offset = segtable_offset + 2 * 8;
289 resdir_size = get_res16_directory_size( spec );
290 fprintf( outfile, " unsigned char resdir[%d];\n", resdir_size );
292 /* resident names table */
294 resnames_offset = resdir_offset + resdir_size;
295 fprintf( outfile, " struct\n {\n" );
296 fprintf( outfile, " struct { unsigned char len; char name[%d]; unsigned short ord; } name_0;\n",
297 strlen( spec->dll_name ) );
298 resnames_size = 3 + strlen( spec->dll_name );
299 for (i = 1; i <= spec->limit; i++)
301 ORDDEF *odp = spec->ordinals[i];
302 if (!odp || !odp->name[0]) continue;
303 fprintf( outfile, " struct { unsigned char len; char name[%d]; unsigned short ord; } name_%d;\n",
304 strlen(odp->name), i );
305 resnames_size += 3 + strlen( odp->name );
307 fprintf( outfile, " unsigned char name_last[%d];\n", 2 - (resnames_size & 1) );
308 resnames_size = (resnames_size + 2) & ~1;
309 fprintf( outfile, " } resnames;\n" );
311 /* imported names table */
313 impnames_offset = resnames_offset + resnames_size;
314 fprintf( outfile, " unsigned char impnames[2];\n" );
316 /* entry table */
318 et_offset = impnames_offset + 2;
319 et_size = output_entry_table( &et_buffer, spec );
320 fprintf( outfile, " unsigned char entry_table[%d];\n", et_size );
322 /* data segment */
324 data_offset = et_offset + et_size;
325 fprintf( outfile, " unsigned char data_segment[%d];\n", data_size );
326 if (data_offset + data_size >= 0x10000)
327 fatal_error( "Not supported yet: 16-bit module data larger than 64K\n" );
329 /* resource data */
331 resdata_offset = ne_offset + data_offset + data_size;
332 for (resdata_align = 0; resdata_align < 16; resdata_align++)
334 unsigned int size = get_res16_data_size( spec, resdata_offset, resdata_align );
335 if ((resdata_offset + size) >> resdata_align <= 0xffff) break;
337 output_res16_directory( &resdir_buffer, spec, resdata_offset, resdata_align );
338 resdata_size = output_res16_data( &resdata_buffer, spec, resdata_offset, resdata_align );
339 if (resdata_size) fprintf( outfile, " unsigned char resources[%d];\n", resdata_size );
341 /* DOS header */
343 fprintf( outfile, "} module =\n{\n {\n" );
344 fprintf( outfile, " 0x%04x,\n", IMAGE_DOS_SIGNATURE ); /* e_magic */
345 fprintf( outfile, " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n" );
346 fprintf( outfile, " { 0, 0, 0, 0, }, 0, 0,\n" );
347 fprintf( outfile, " { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },\n" );
348 fprintf( outfile, " sizeof(module.dos_header)\n" ); /* e_lfanew */
350 /* NE header */
352 fprintf( outfile, " },\n {\n" );
353 fprintf( outfile, " 0x%04x,\n", IMAGE_OS2_SIGNATURE ); /* ne_magic */
354 fprintf( outfile, " 0, 0,\n" );
355 fprintf( outfile, " %d,\n", et_offset ); /* ne_enttab */
356 fprintf( outfile, " sizeof(module.entry_table),\n" ); /* ne_cbenttab */
357 fprintf( outfile, " 0,\n" ); /* ne_crc */
358 fprintf( outfile, " 0x%04x,\n", /* ne_flags */
359 NE_FFLAGS_SINGLEDATA | NE_FFLAGS_LIBMODULE );
360 fprintf( outfile, " 2,\n" ); /* ne_autodata */
361 fprintf( outfile, " %d,\n", spec->heap_size ); /* ne_heap */
362 fprintf( outfile, " 0, 0, 0,\n" );
363 fprintf( outfile, " 2,\n" ); /* ne_cseg */
364 fprintf( outfile, " 0,\n" ); /* ne_cmod */
365 fprintf( outfile, " 0,\n" ); /* ne_cbnrestab */
366 fprintf( outfile, " %d,\n", segtable_offset ); /* ne_segtab */
367 fprintf( outfile, " %d,\n", resdir_offset ); /* ne_rsrctab */
368 fprintf( outfile, " %d,\n", resnames_offset ); /* ne_restab */
369 fprintf( outfile, " %d,\n", impnames_offset ); /* ne_modtab */
370 fprintf( outfile, " %d,\n", impnames_offset ); /* ne_imptab */
371 fprintf( outfile, " 0,\n" ); /* ne_nrestab */
372 fprintf( outfile, " 0,\n" ); /* ne_cmovent */
373 fprintf( outfile, " 0,\n" ); /* ne_align */
374 fprintf( outfile, " 0,\n" ); /* ne_cres */
375 fprintf( outfile, " 0x%04x,\n", NE_OSFLAGS_WINDOWS ); /* ne_exetyp */
376 fprintf( outfile, " 0x%04x,\n", NE_AFLAGS_FASTLOAD ); /* ne_flagsothers */
377 fprintf( outfile, " 0,\n" ); /* ne_pretthunks */
378 fprintf( outfile, " sizeof(module),\n" ); /* ne_psegrefbytes */
379 fprintf( outfile, " 0,\n" ); /* ne_swaparea */
380 fprintf( outfile, " 0\n" ); /* ne_expver */
381 fprintf( outfile, " },\n" );
383 /* segment table */
385 fprintf( outfile, " {\n" );
386 fprintf( outfile, " { 0, %d, 0x%04x, %d },\n",
387 max_code_offset, NE_SEGFLAGS_32BIT, max_code_offset );
388 fprintf( outfile, " { %d, %d, 0x%04x, %d },\n",
389 data_offset, data_size, NE_SEGFLAGS_DATA, data_size );
390 fprintf( outfile, " },\n" );
392 /* resource directory */
394 output_bytes( outfile, resdir_buffer, resdir_size );
395 free( resdir_buffer );
397 /* resident names table */
399 fprintf( outfile, " {\n" );
400 strcpy( string, spec->dll_name );
401 fprintf( outfile, " { %d, \"%s\", 0 },\n", strlen(string), strupper(string) );
402 for (i = 1; i <= spec->limit; i++)
404 ORDDEF *odp = spec->ordinals[i];
405 if (!odp || !odp->name[0]) continue;
406 strcpy( string, odp->name );
407 fprintf( outfile, " { %d, \"%s\", %d },\n", strlen(string), strupper(string), i );
409 fprintf( outfile, " { 0 }\n },\n" );
411 /* imported names table */
413 fprintf( outfile, " { 0, 0 },\n" );
415 /* entry table */
417 output_bytes( outfile, et_buffer, et_size );
418 free( et_buffer );
420 /* data_segment */
422 output_bytes( outfile, data_segment, data_size );
424 /* resource data */
426 if (resdata_size)
428 output_bytes( outfile, resdata_buffer, resdata_size );
429 free( resdata_buffer );
432 fprintf( outfile, "};\n" );
436 #ifdef __i386__
437 /*******************************************************************
438 * BuildCallFrom16Func
440 * Build a 16-bit-to-Wine callback glue function.
442 * The generated routines are intended to be used as argument conversion
443 * routines to be called by the CallFrom16... core. Thus, the prototypes of
444 * the generated routines are (see also CallFrom16):
446 * extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
447 * extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
448 * extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args,
449 * CONTEXT86 *context );
451 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
452 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
453 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
454 * 'T'=segmented pointer to null-terminated string).
456 * The generated routines fetch the arguments from the 16-bit stack (pointed
457 * to by 'args'); the offsets of the single argument values are computed
458 * according to the calling convention and the argument types. Then, the
459 * 32-bit entry point is called with these arguments.
461 * For register functions, the arguments (if present) are converted just
462 * the same as for normal functions, but in addition the CONTEXT86 pointer
463 * filled with the current register values is passed to the 32-bit routine.
465 static void BuildCallFrom16Func( FILE *outfile, const char *profile, const char *prefix )
467 int i, pos, argsize = 0;
468 int short_ret = 0;
469 int reg_func = 0;
470 int usecdecl = 0;
471 int varargs = 0;
472 const char *args = profile + 7;
473 const char *ret_type;
475 /* Parse function type */
477 if (!strncmp( "c_", profile, 2 )) usecdecl = 1;
478 else if (!strncmp( "v_", profile, 2 )) varargs = usecdecl = 1;
479 else if (strncmp( "p_", profile, 2 ))
481 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
482 return;
485 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
486 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
487 else if (strncmp( "long_", profile + 2, 5 ))
489 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
490 return;
493 for ( i = 0; args[i]; i++ )
494 switch ( args[i] )
496 case 'w': /* word */
497 case 's': /* s_word */
498 argsize += 2;
499 break;
500 case 'l': /* long or segmented pointer */
501 case 'T': /* segmented pointer to null-terminated string */
502 case 'p': /* linear pointer */
503 case 't': /* linear pointer to null-terminated string */
504 argsize += 4;
505 break;
508 ret_type = reg_func? "void" : short_ret ? "unsigned short" : "unsigned int";
510 fprintf( outfile, "typedef %s (%s*proc_%s_t)( ",
511 ret_type, usecdecl ? "" : "__stdcall ", profile );
512 args = profile + 7;
513 for ( i = 0; args[i]; i++ )
515 if ( i ) fprintf( outfile, ", " );
516 switch (args[i])
518 case 'w': fprintf( outfile, "unsigned short" ); break;
519 case 's': fprintf( outfile, "short" ); break;
520 case 'l': case 'T': fprintf( outfile, "unsigned int" ); break;
521 case 'p': case 't': fprintf( outfile, "void *" ); break;
524 if (reg_func || varargs)
525 fprintf( outfile, "%svoid *", i? ", " : "" );
526 else if ( !i )
527 fprintf( outfile, "void" );
528 fprintf( outfile, " );\n" );
530 fprintf( outfile, "static %s __stdcall __wine_%s_CallFrom16_%s( proc_%s_t proc, unsigned char *args%s )\n",
531 ret_type, make_c_identifier(prefix), profile, profile,
532 reg_func? ", void *context" : "" );
534 fprintf( outfile, "{\n %sproc(\n", reg_func ? "" : "return " );
535 args = profile + 7;
536 pos = !usecdecl? argsize : 0;
537 for ( i = 0; args[i]; i++ )
539 if ( i ) fprintf( outfile, ",\n" );
540 fprintf( outfile, " " );
541 switch (args[i])
543 case 'w': /* word */
544 if ( !usecdecl ) pos -= 2;
545 fprintf( outfile, "*(unsigned short *)(args+%d)", pos );
546 if ( usecdecl ) pos += 2;
547 break;
549 case 's': /* s_word */
550 if ( !usecdecl ) pos -= 2;
551 fprintf( outfile, "*(short *)(args+%d)", pos );
552 if ( usecdecl ) pos += 2;
553 break;
555 case 'l': /* long or segmented pointer */
556 case 'T': /* segmented pointer to null-terminated string */
557 if ( !usecdecl ) pos -= 4;
558 fprintf( outfile, "*(unsigned int *)(args+%d)", pos );
559 if ( usecdecl ) pos += 4;
560 break;
562 case 'p': /* linear pointer */
563 case 't': /* linear pointer to null-terminated string */
564 if ( !usecdecl ) pos -= 4;
565 fprintf( outfile, "((char*)wine_ldt_copy.base[*(unsigned short*)(args+%d) >> 3] + *(unsigned short*)(args+%d))",
566 pos + 2, pos );
567 if ( usecdecl ) pos += 4;
568 break;
570 default:
571 fprintf( stderr, "Unknown arg type '%c'\n", args[i] );
574 if ( reg_func )
575 fprintf( outfile, "%s context", i? ",\n" : "" );
576 else if (varargs)
577 fprintf( outfile, "%s args + %d", i? ",\n" : "", argsize );
578 fprintf( outfile, " );\n}\n\n" );
580 #endif
583 /*******************************************************************
584 * get_function_name
586 static const char *get_function_name( const ORDDEF *odp )
588 static char buffer[80];
590 sprintf( buffer, "%s_%s_%s",
591 (odp->type == TYPE_PASCAL) ? "p" :
592 (odp->type == TYPE_VARARGS) ? "v" : "c",
593 (odp->flags & FLAG_REGISTER) ? "regs" :
594 (odp->flags & FLAG_RET16) ? "word" : "long",
595 odp->u.func.arg_types );
596 return buffer;
600 /*******************************************************************
601 * Spec16TypeCompare
603 static int Spec16TypeCompare( const void *e1, const void *e2 )
605 const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
606 const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
607 int retval;
608 int type1 = odp1->type;
609 int type2 = odp2->type;
611 if (type1 == TYPE_STUB) type1 = TYPE_CDECL;
612 if (type2 == TYPE_STUB) type2 = TYPE_CDECL;
614 if ((retval = type1 - type2) != 0) return retval;
616 type1 = odp1->flags & (FLAG_RET16|FLAG_REGISTER);
617 type2 = odp2->flags & (FLAG_RET16|FLAG_REGISTER);
619 if ((retval = type1 - type2) != 0) return retval;
621 return strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
625 /*******************************************************************
626 * output_stub_funcs
628 * Output the functions for stub entry points
630 static void output_stub_funcs( FILE *outfile, const DLLSPEC *spec )
632 int i;
633 char *p;
635 for (i = 0; i <= spec->limit; i++)
637 ORDDEF *odp = spec->ordinals[i];
638 if (!odp || odp->type != TYPE_STUB) continue;
639 fprintf( outfile, "#ifdef __GNUC__\n" );
640 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
641 fprintf( outfile, "#endif\n" );
642 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
643 fprintf( outfile, " extern void __stdcall RaiseException( unsigned int, unsigned int, unsigned int, const void ** );\n" );
644 fprintf( outfile, " const void *args[2];\n" );
645 fprintf( outfile, " args[0] = \"%s\";\n", spec->file_name );
646 fprintf( outfile, " args[1] = func;\n" );
647 fprintf( outfile, " for (;;) RaiseException( 0x%08x, %d, 2, args );\n}\n\n",
648 EXCEPTION_WINE_STUB, EH_NONCONTINUABLE );
649 break;
651 for (i = 0; i <= spec->limit; i++)
653 ORDDEF *odp = spec->ordinals[i];
654 if (!odp || odp->type != TYPE_STUB) continue;
655 odp->link_name = xrealloc( odp->link_name, strlen(odp->name) + 13 );
656 strcpy( odp->link_name, "__wine_stub_" );
657 strcat( odp->link_name, odp->name );
658 for (p = odp->link_name; *p; p++) if (!isalnum(*p)) *p = '_';
659 fprintf( outfile, "static void %s(void) { __wine_unimplemented(\"%s\"); }\n",
660 odp->link_name, odp->name );
665 /*******************************************************************
666 * BuildSpec16File
668 * Build a Win16 assembly file from a spec file.
670 void BuildSpec16File( FILE *outfile, DLLSPEC *spec )
672 ORDDEF **type, **typelist;
673 int i, nFuncs, nTypes;
674 int code_offset, data_offset;
675 unsigned char *data;
676 char constructor[100], destructor[100];
677 #ifdef __i386__
678 unsigned short code_selector = get_cs();
679 #endif
681 /* File header */
683 output_file_header( outfile );
684 fprintf( outfile, "extern unsigned short __wine_call_from_16_word();\n" );
685 fprintf( outfile, "extern unsigned int __wine_call_from_16_long();\n" );
686 fprintf( outfile, "extern void __wine_call_from_16_regs();\n" );
687 fprintf( outfile, "extern void __wine_call_from_16_thunk();\n" );
689 data = (unsigned char *)xmalloc( 0x10000 );
690 memset( data, 0, 16 );
691 data_offset = 16;
693 if (!spec->dll_name) /* set default name from file name */
695 char *p;
696 spec->dll_name = xstrdup( spec->file_name );
697 if ((p = strrchr( spec->dll_name, '.' ))) *p = 0;
700 output_stub_funcs( outfile, spec );
702 /* Build sorted list of all argument types, without duplicates */
704 typelist = (ORDDEF **)calloc( spec->limit+1, sizeof(ORDDEF *) );
706 for (i = nFuncs = 0; i <= spec->limit; i++)
708 ORDDEF *odp = spec->ordinals[i];
709 if (!odp) continue;
710 switch (odp->type)
712 case TYPE_CDECL:
713 case TYPE_PASCAL:
714 case TYPE_VARARGS:
715 case TYPE_STUB:
716 typelist[nFuncs++] = odp;
718 default:
719 break;
723 qsort( typelist, nFuncs, sizeof(ORDDEF *), Spec16TypeCompare );
725 i = nTypes = 0;
726 while ( i < nFuncs )
728 typelist[nTypes++] = typelist[i++];
729 while ( i < nFuncs && Spec16TypeCompare( typelist + i, typelist + nTypes-1 ) == 0 )
730 i++;
733 /* Output CallFrom16 routines needed by this .spec file */
734 #ifdef __i386__
735 for ( i = 0; i < nTypes; i++ )
737 char profile[101];
739 strcpy( profile, get_function_name( typelist[i] ));
740 BuildCallFrom16Func( outfile, profile, spec->file_name );
742 #endif
744 /* Output the DLL functions prototypes */
746 for (i = 0; i <= spec->limit; i++)
748 ORDDEF *odp = spec->ordinals[i];
749 if (!odp) continue;
750 switch(odp->type)
752 case TYPE_CDECL:
753 case TYPE_PASCAL:
754 case TYPE_VARARGS:
755 fprintf( outfile, "extern void %s();\n", odp->link_name );
756 break;
757 default:
758 break;
762 /* Output code segment */
764 fprintf( outfile, "\n#include \"pshpack1.h\"\n" );
765 fprintf( outfile, "\nstatic struct code_segment\n{\n" );
766 fprintf( outfile, " struct {\n" );
767 #ifdef __i386__
768 fprintf( outfile, " unsigned char pushl;\n" ); /* pushl $relay */
769 fprintf( outfile, " void *relay;\n" );
770 fprintf( outfile, " unsigned char lcall;\n" ); /* lcall __FLATCS__:glue */
771 fprintf( outfile, " void *glue;\n" );
772 fprintf( outfile, " unsigned short flatcs;\n" );
773 #endif
774 fprintf( outfile, " unsigned short lret;\n" ); /* lret $args */
775 fprintf( outfile, " unsigned short args;\n" );
776 fprintf( outfile, " unsigned int arg_types[2];\n" );
777 fprintf( outfile, " } call[%d];\n", nTypes );
778 fprintf( outfile, " struct {\n" );
779 #ifdef __i386__
780 fprintf( outfile, " unsigned short pushw_bp;\n" ); /* pushw %bp */
781 fprintf( outfile, " unsigned char pushl;\n" ); /* pushl $target */
782 #endif
783 fprintf( outfile, " void (*target)();\n" );
784 fprintf( outfile, " unsigned short call;\n" ); /* call CALLFROM16 */
785 fprintf( outfile, " short callfrom16;\n" );
786 fprintf( outfile, " } entry[%d];\n", nFuncs );
787 fprintf( outfile, "} code_segment =\n{\n {\n" );
789 code_offset = 0;
791 for ( i = 0; i < nTypes; i++ )
793 char profile[101], *arg;
794 unsigned int arg_types[2];
795 int j, argsize = 0;
797 strcpy( profile, get_function_name( typelist[i] ));
798 if ( typelist[i]->type == TYPE_PASCAL )
799 for ( arg = typelist[i]->u.func.arg_types; *arg; arg++ )
800 switch ( *arg )
802 case 'w': /* word */
803 case 's': /* s_word */
804 argsize += 2;
805 break;
806 case 'l': /* long or segmented pointer */
807 case 'T': /* segmented pointer to null-terminated string */
808 case 'p': /* linear pointer */
809 case 't': /* linear pointer to null-terminated string */
810 argsize += 4;
811 break;
814 /* build the arg types bit fields */
815 arg_types[0] = arg_types[1] = 0;
816 for (j = 0; typelist[i]->u.func.arg_types[j]; j++)
818 int type = 0;
819 switch(typelist[i]->u.func.arg_types[j])
821 case 'w': type = ARG_WORD; break;
822 case 's': type = ARG_SWORD; break;
823 case 'l': type = ARG_LONG; break;
824 case 'p': type = ARG_PTR; break;
825 case 't': type = ARG_STR; break;
826 case 'T': type = ARG_SEGSTR; break;
828 arg_types[j / 10] |= type << (3 * (j % 10));
830 if (typelist[i]->flags & FLAG_REGISTER) arg_types[0] |= ARG_REGISTER;
831 if (typelist[i]->flags & FLAG_RET16) arg_types[0] |= ARG_RET16;
833 #ifdef __i386__
834 fprintf( outfile, " { 0x68, __wine_%s_CallFrom16_%s, 0x9a, __wine_call_from_16_%s,\n",
835 make_c_identifier(spec->file_name), profile,
836 (typelist[i]->flags & FLAG_REGISTER) ? "regs":
837 (typelist[i]->flags & FLAG_RET16) ? "word" : "long" );
838 if (argsize)
839 fprintf( outfile, " 0x%04x, 0xca66, %d, { 0x%08x, 0x%08x } },\n",
840 code_selector, argsize, arg_types[0], arg_types[1] );
841 else
842 fprintf( outfile, " 0x%04x, 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
843 code_selector, arg_types[0], arg_types[1] );
844 #else
845 if (argsize)
846 fprintf( outfile, " { 0xca66, %d, { 0x%08x, 0x%08x } },\n",
847 argsize, arg_types[0], arg_types[1] );
848 else
849 fprintf( outfile, " { 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
850 arg_types[0], arg_types[1] );
851 #endif
852 code_offset += sizeof(CALLFROM16);
854 fprintf( outfile, " },\n {\n" );
856 for (i = 0; i <= spec->limit; i++)
858 ORDDEF *odp = spec->ordinals[i];
859 if (!odp) continue;
860 switch (odp->type)
862 case TYPE_ABS:
863 odp->offset = LOWORD(odp->u.abs.value);
864 break;
866 case TYPE_VARIABLE:
867 odp->offset = data_offset;
868 data_offset += StoreVariableCode( data + data_offset, 4, odp);
869 break;
871 case TYPE_CDECL:
872 case TYPE_PASCAL:
873 case TYPE_VARARGS:
874 case TYPE_STUB:
875 type = bsearch( &odp, typelist, nTypes, sizeof(ORDDEF *), Spec16TypeCompare );
876 assert( type );
878 fprintf( outfile, " /* %s.%d */ ", spec->dll_name, i );
879 #ifdef __i386__
880 fprintf( outfile, "{ 0x5566, 0x68, %s, 0xe866, %d /* %s */ },\n",
881 #else
882 fprintf( outfile, "{ %s, 0xe866, %d, /* %s */ },\n",
883 #endif
884 odp->link_name,
885 (type-typelist)*sizeof(CALLFROM16) -
886 (code_offset + sizeof(ENTRYPOINT16)),
887 get_function_name( odp ) );
889 odp->offset = code_offset;
890 code_offset += sizeof(ENTRYPOINT16);
891 break;
893 default:
894 fprintf(stderr,"build: function type %d not available for Win16\n",
895 odp->type);
896 exit(1);
900 fprintf( outfile, " }\n};\n" );
902 /* Build the module */
904 output_module_data( outfile, code_offset, data, data_offset, spec );
906 /* Output the DLL descriptor */
908 fprintf( outfile, "#include \"poppack.h\"\n\n" );
910 fprintf( outfile, "static const struct dll_descriptor\n{\n" );
911 fprintf( outfile, " const struct module_data *module;\n" );
912 fprintf( outfile, " struct code_segment *code_start;\n" );
913 fprintf( outfile, " const unsigned char *rsrc;\n" );
914 fprintf( outfile, "} descriptor =\n{\n" );
915 fprintf( outfile, " &module,\n" );
916 fprintf( outfile, " &code_segment\n" );
917 fprintf( outfile, "};\n" );
919 /* Output the DLL constructor */
921 sprintf( constructor, "__wine_spec_%s_init", make_c_identifier(spec->file_name) );
922 sprintf( destructor, "__wine_spec_%s_fini", make_c_identifier(spec->file_name) );
923 output_dll_init( outfile, constructor, destructor );
925 fprintf( outfile,
926 "void %s(void)\n"
927 "{\n"
928 " extern void __wine_dll_register_16( const struct dll_descriptor *descr, const char *file_name );\n"
929 " __wine_dll_register_16( &descriptor, \"%s\" );\n"
930 "}\n", constructor, spec->file_name );
931 fprintf( outfile,
932 "void %s(void)\n"
933 "{\n"
934 " extern void __wine_dll_unregister_16( const struct dll_descriptor *descr );\n"
935 " __wine_dll_unregister_16( &descriptor );\n"
936 "}\n", destructor );