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
12 #include "wine/port.h"
17 #include "wine/exception.h"
18 #include "builtin16.h"
20 #include "stackframe.h"
25 extern unsigned short __get_cs(void);
26 __ASM_GLOBAL_FUNC( __get_cs
, "movw %cs,%ax\n\tret" );
30 /*******************************************************************
33 * Output a file header with the common declarations we need.
35 static void output_file_header( FILE *outfile
)
37 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n\n",
39 fprintf( outfile
, "extern struct\n{\n" );
40 fprintf( outfile
, " void *base[8192];\n" );
41 fprintf( outfile
, " unsigned long limit[8192];\n" );
42 fprintf( outfile
, " unsigned char flags[8192];\n" );
43 fprintf( outfile
, "} wine_ldt_copy;\n\n" );
45 fprintf( outfile
, "#define __stdcall __attribute__((__stdcall__))\n\n" );
47 fprintf( outfile
, "#define __stdcall\n\n" );
52 /*******************************************************************
55 * Store a list of ints into a byte array.
57 static int StoreVariableCode( unsigned char *buffer
, int size
, ORDDEF
*odp
)
64 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
65 buffer
[i
] = odp
->u
.var
.values
[i
];
68 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
69 ((unsigned short *)buffer
)[i
] = odp
->u
.var
.values
[i
];
72 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
73 ((unsigned int *)buffer
)[i
] = odp
->u
.var
.values
[i
];
76 return odp
->u
.var
.n_values
* size
;
80 /*******************************************************************
83 * Build the in-memory representation of a 16-bit NE module, and dump it
84 * as a byte stream into the assembly code.
86 static int BuildModule16( FILE *outfile
, int max_code_offset
,
92 SEGTABLEENTRY
*pSegment
;
95 ET_BUNDLE
*bundle
= 0;
100 * OFSTRUCT File information
101 * SEGTABLEENTRY Segment 1 (code)
102 * SEGTABLEENTRY Segment 2 (data)
103 * WORD[2] Resource table (empty)
104 * BYTE[2] Imported names (empty)
105 * BYTE[n] Resident names table
106 * BYTE[n] Entry table
109 buffer
= xmalloc( 0x10000 );
110 memset( buffer
, 0, 0x10000 );
112 pModule
= (NE_MODULE
*)buffer
;
113 pModule
->magic
= IMAGE_OS2_SIGNATURE
;
116 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_BUILTIN
| NE_FFLAGS_LIBMODULE
;
118 pModule
->heap_size
= DLLHeapSize
;
119 pModule
->stack_size
= 0;
124 pModule
->seg_count
= 2;
125 pModule
->modref_count
= 0;
126 pModule
->nrname_size
= 0;
127 pModule
->modref_table
= 0;
128 pModule
->nrname_fpos
= 0;
129 pModule
->moveable_entries
= 0;
130 pModule
->alignment
= 0;
131 pModule
->truetype
= 0;
132 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
133 pModule
->misc_flags
= 0;
134 pModule
->dlls_to_init
= 0;
135 pModule
->nrname_handle
= 0;
136 pModule
->min_swap_area
= 0;
137 pModule
->expected_version
= 0;
138 pModule
->module32
= 0;
140 pModule
->self_loading_sel
= 0;
142 /* File information */
144 pFileInfo
= (OFSTRUCT
*)(pModule
+ 1);
145 pModule
->fileinfo
= (int)pFileInfo
- (int)pModule
;
146 memset( pFileInfo
, 0, sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
) );
147 pFileInfo
->cBytes
= sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
)
148 + strlen(DLLFileName
);
149 strcpy( pFileInfo
->szPathName
, DLLFileName
);
150 pstr
= (char *)pFileInfo
+ pFileInfo
->cBytes
+ 1;
154 pstr
= (char *)(((long)pstr
+ 3) & ~3);
155 pSegment
= (SEGTABLEENTRY
*)pstr
;
156 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
157 pSegment
->filepos
= 0;
158 pSegment
->size
= max_code_offset
;
160 pSegment
->minsize
= max_code_offset
;
164 pModule
->dgroup_entry
= (int)pSegment
- (int)pModule
;
165 pSegment
->filepos
= 0;
166 pSegment
->size
= max_data_offset
;
167 pSegment
->flags
= NE_SEGFLAGS_DATA
;
168 pSegment
->minsize
= max_data_offset
;
174 pstr
= (char *)pSegment
;
175 pstr
= (char *)(((long)pstr
+ 3) & ~3);
176 pModule
->res_table
= (int)pstr
- (int)pModule
;
177 pstr
+= output_res16_directory( pstr
);
179 /* Imported names table */
181 pstr
= (char *)(((long)pstr
+ 3) & ~3);
182 pModule
->import_table
= (int)pstr
- (int)pModule
;
186 /* Resident names table */
188 pstr
= (char *)(((long)pstr
+ 3) & ~3);
189 pModule
->name_table
= (int)pstr
- (int)pModule
;
190 /* First entry is module name */
191 *pstr
= strlen( DLLName
);
192 strcpy( pstr
+ 1, DLLName
);
194 PUT_UA_WORD( pstr
, 0 );
195 pstr
+= sizeof(WORD
);
196 /* Store all ordinals */
197 for (i
= 1; i
<= Limit
; i
++)
199 ORDDEF
*odp
= Ordinals
[i
];
200 if (!odp
|| !odp
->name
[0]) continue;
201 *pstr
= strlen( odp
->name
);
202 strcpy( pstr
+ 1, odp
->name
);
203 strupper( pstr
+ 1 );
205 PUT_UA_WORD( pstr
, i
);
206 pstr
+= sizeof(WORD
);
212 pstr
= (char *)(((long)pstr
+ 3) & ~3);
213 pModule
->entry_table
= (int)pstr
- (int)pModule
;
214 for (i
= 1; i
<= Limit
; i
++)
217 ORDDEF
*odp
= Ordinals
[i
];
226 selector
= 1; /* Code selector */
230 selector
= 2; /* Data selector */
234 selector
= 0xfe; /* Constant selector */
238 selector
= 0; /* Invalid selector */
245 if ( bundle
&& bundle
->last
+1 == i
)
249 pstr
= (char *)(((long)pstr
+ 1) & ~1);
251 bundle
->next
= (char *)pstr
- (char *)pModule
;
253 bundle
= (ET_BUNDLE
*)pstr
;
257 pstr
+= sizeof(ET_BUNDLE
);
260 /* FIXME: is this really correct ?? */
261 entry
.type
= 0xff; /* movable */
262 entry
.flags
= 3; /* exported & public data */
263 entry
.segnum
= selector
;
264 entry
.offs
= odp
->offset
;
265 memcpy( pstr
, &entry
, sizeof(ET_ENTRY
) );
266 pstr
+= sizeof(ET_ENTRY
);
270 /* Dump the module content */
272 pstr
= (char *)(((long)pstr
+ 3) & ~3);
273 dump_bytes( outfile
, (char *)pModule
, (int)pstr
- (int)pModule
, "Module", 0 );
274 return (int)pstr
- (int)pModule
;
278 /*******************************************************************
279 * BuildCallFrom16Func
281 * Build a 16-bit-to-Wine callback glue function.
283 * The generated routines are intended to be used as argument conversion
284 * routines to be called by the CallFrom16... core. Thus, the prototypes of
285 * the generated routines are (see also CallFrom16):
287 * extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
288 * extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
289 * extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args,
290 * CONTEXT86 *context );
291 * extern void WINAPI PREFIX_CallFrom16_C_intr_xxx( FARPROC func, LPBYTE args,
292 * CONTEXT86 *context );
294 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
295 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
296 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
297 * 'T'=segmented pointer to null-terminated string).
299 * The generated routines fetch the arguments from the 16-bit stack (pointed
300 * to by 'args'); the offsets of the single argument values are computed
301 * according to the calling convention and the argument types. Then, the
302 * 32-bit entry point is called with these arguments.
304 * For register functions, the arguments (if present) are converted just
305 * the same as for normal functions, but in addition the CONTEXT86 pointer
306 * filled with the current register values is passed to the 32-bit routine.
307 * (An 'intr' interrupt handler routine is treated exactly like a register
308 * routine, except that upon return, the flags word pushed onto the stack
309 * by the interrupt is removed by the 16-bit call stub.)
312 static void BuildCallFrom16Func( FILE *outfile
, char *profile
, char *prefix
, int local
)
314 int i
, pos
, argsize
= 0;
318 char *args
= profile
+ 7;
321 /* Parse function type */
323 if (!strncmp( "c_", profile
, 2 )) usecdecl
= 1;
324 else if (strncmp( "p_", profile
, 2 ))
326 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
330 if (!strncmp( "word_", profile
+ 2, 5 )) short_ret
= 1;
331 else if (!strncmp( "regs_", profile
+ 2, 5 )) reg_func
= 1;
332 else if (!strncmp( "intr_", profile
+ 2, 5 )) reg_func
= 2;
333 else if (strncmp( "long_", profile
+ 2, 5 ))
335 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
339 for ( i
= 0; args
[i
]; i
++ )
343 case 's': /* s_word */
346 case 'l': /* long or segmented pointer */
347 case 'T': /* segmented pointer to null-terminated string */
348 case 'p': /* linear pointer */
349 case 't': /* linear pointer to null-terminated string */
354 ret_type
= reg_func
? "void" : short_ret
? "unsigned short" : "unsigned int";
356 fprintf( outfile
, "typedef %s __stdcall (*proc_%s_t)( ", ret_type
, profile
);
358 for ( i
= 0; args
[i
]; i
++ )
360 if ( i
) fprintf( outfile
, ", " );
363 case 'w': fprintf( outfile
, "unsigned short" ); break;
364 case 's': fprintf( outfile
, "short" ); break;
365 case 'l': case 'T': fprintf( outfile
, "unsigned int" ); break;
366 case 'p': case 't': fprintf( outfile
, "void *" ); break;
370 fprintf( outfile
, "%svoid *", i
? ", " : "" );
372 fprintf( outfile
, "void" );
373 fprintf( outfile
, " );\n" );
375 fprintf( outfile
, "%s%s __stdcall %s_CallFrom16_%s( proc_%s_t proc, unsigned char *args%s )\n",
376 local
? "static " : "", ret_type
, prefix
, profile
, profile
,
377 reg_func
? ", void *context" : "" );
379 fprintf( outfile
, "{\n %sproc(\n", reg_func
? "" : "return " );
381 pos
= !usecdecl
? argsize
: 0;
382 for ( i
= 0; args
[i
]; i
++ )
384 if ( i
) fprintf( outfile
, ",\n" );
385 fprintf( outfile
, " " );
389 if ( !usecdecl
) pos
-= 2;
390 fprintf( outfile
, "*(unsigned short *)(args+%d)", pos
);
391 if ( usecdecl
) pos
+= 2;
394 case 's': /* s_word */
395 if ( !usecdecl
) pos
-= 2;
396 fprintf( outfile
, "*(short *)(args+%d)", pos
);
397 if ( usecdecl
) pos
+= 2;
400 case 'l': /* long or segmented pointer */
401 case 'T': /* segmented pointer to null-terminated string */
402 if ( !usecdecl
) pos
-= 4;
403 fprintf( outfile
, "*(unsigned int *)(args+%d)", pos
);
404 if ( usecdecl
) pos
+= 4;
407 case 'p': /* linear pointer */
408 case 't': /* linear pointer to null-terminated string */
409 if ( !usecdecl
) pos
-= 4;
410 fprintf( outfile
, "((char*)wine_ldt_copy.base[*(unsigned short*)(args+%d) >> 3] + *(unsigned short*)(args+%d))",
412 if ( usecdecl
) pos
+= 4;
416 fprintf( stderr
, "Unknown arg type '%c'\n", args
[i
] );
420 fprintf( outfile
, "%s context", i
? ",\n" : "" );
421 fprintf( outfile
, " );\n}\n\n" );
425 /*******************************************************************
428 * Build a Wine-to-16-bit callback glue function.
430 * Prototypes for the CallTo16 functions:
431 * extern WORD CALLBACK PREFIX_CallTo16_word_xxx( FARPROC16 func, args... );
432 * extern LONG CALLBACK PREFIX_CallTo16_long_xxx( FARPROC16 func, args... );
434 * These routines are provided solely for convenience; they simply
435 * write the arguments onto the 16-bit stack, and call the appropriate
436 * wine_call_to_16... core routine.
438 * If you have more sophisticated argument conversion requirements than
439 * are provided by these routines, you might as well call the core
440 * routines by yourself.
443 static void BuildCallTo16Func( FILE *outfile
, char *profile
, char *prefix
)
445 char *args
= profile
+ 5;
446 int i
, argsize
= 0, short_ret
= 0;
448 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
449 else if (strncmp( "long_", profile
, 5 ))
451 fprintf( stderr
, "Invalid function name '%s'.\n", profile
);
455 fprintf( outfile
, "unsigned %s __stdcall %s_CallTo16_%s( void (*proc)()",
456 short_ret
? "short" : "int", prefix
, profile
);
458 for ( i
= 0; args
[i
]; i
++ )
460 fprintf( outfile
, ", " );
463 case 'w': fprintf( outfile
, "unsigned short" ); argsize
+= 2; break;
464 case 'l': fprintf( outfile
, "unsigned int" ); argsize
+= 4; break;
466 fprintf( outfile
, " arg%d", i
+1 );
468 fprintf( outfile
, " )\n{\n" );
473 fprintf( outfile
, " char *args;\n" );
474 fprintf( outfile
, " unsigned int cur_stack;\n\n" );
475 fprintf( outfile
, "#ifdef __GNUC__\n" );
476 fprintf( outfile
, " __asm__(\".byte 0x64\\n\\tmovl (0x%x),%%0\" : \"=r\" (cur_stack));\n",
478 fprintf( outfile
, "#else\n" );
479 fprintf( outfile
, " extern char *NtCurrentTeb(void);\n" );
480 fprintf( outfile
, " cur_stack = *(unsigned int *)(NtCurrentTeb() + 0x%x);\n",
482 fprintf( outfile
, "#endif\n" );
483 fprintf( outfile
, " args = (char *)wine_ldt_copy.base[cur_stack >> 19] + (cur_stack & 0xffff);\n" );
487 for ( i
= 0; args
[i
]; i
++ )
491 case 'w': fprintf( outfile
, " args -= sizeof(unsigned short); *(unsigned short" ); break;
492 case 'l': fprintf( outfile
, " args -= sizeof(unsigned int); *(unsigned int" ); break;
493 default: fprintf( stderr
, "Unexpected case '%c' in BuildCallTo16Func\n",
496 fprintf( outfile
, " *)args = arg%d;\n", i
+1 );
499 fprintf( outfile
, " return wine_call_to_16_%s( proc, %d );\n}\n\n",
500 short_ret
? "word" : "long", argsize
);
502 fprintf( outfile
, " assert(0);\n}\n\n" );
503 #endif /* __i386__ */
507 /*******************************************************************
510 static const char *get_function_name( const ORDDEF
*odp
)
512 static char buffer
[80];
514 sprintf( buffer
, "%s_%s_%s",
515 (odp
->type
== TYPE_CDECL
) ? "c" : "p",
516 (odp
->flags
& FLAG_REGISTER
) ? "regs" :
517 (odp
->flags
& FLAG_INTERRUPT
) ? "intr" :
518 (odp
->type
== TYPE_PASCAL_16
) ? "word" : "long",
519 odp
->u
.func
.arg_types
);
524 /*******************************************************************
527 static int Spec16TypeCompare( const void *e1
, const void *e2
)
529 const ORDDEF
*odp1
= *(const ORDDEF
**)e1
;
530 const ORDDEF
*odp2
= *(const ORDDEF
**)e2
;
533 int type1
= (odp1
->type
== TYPE_CDECL
) ? 0
534 : (odp1
->type
== TYPE_PASCAL_16
) ? 1 : 2;
536 int type2
= (odp2
->type
== TYPE_CDECL
) ? 0
537 : (odp2
->type
== TYPE_PASCAL_16
) ? 1 : 2;
539 if (odp1
->flags
& FLAG_REGISTER
) type1
+= 4;
540 if (odp1
->flags
& FLAG_INTERRUPT
) type1
+= 8;
541 if (odp2
->flags
& FLAG_REGISTER
) type2
+= 4;
542 if (odp2
->flags
& FLAG_INTERRUPT
) type2
+= 8;
544 retval
= type1
- type2
;
546 retval
= strcmp( odp1
->u
.func
.arg_types
, odp2
->u
.func
.arg_types
);
552 /*******************************************************************
555 * Output the functions for stub entry points
557 static void output_stub_funcs( FILE *outfile
)
562 for (i
= 0; i
<= Limit
; i
++)
564 ORDDEF
*odp
= Ordinals
[i
];
565 if (!odp
|| odp
->type
!= TYPE_STUB
) continue;
566 fprintf( outfile
, "#ifdef __GNUC__\n" );
567 fprintf( outfile
, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
568 fprintf( outfile
, "#endif\n" );
569 fprintf( outfile
, "static void __wine_unimplemented( const char *func )\n{\n" );
570 fprintf( outfile
, " struct exc_record {\n" );
571 fprintf( outfile
, " unsigned int code, flags;\n" );
572 fprintf( outfile
, " void *rec, *addr;\n" );
573 fprintf( outfile
, " unsigned int params;\n" );
574 fprintf( outfile
, " const void *info[15];\n" );
575 fprintf( outfile
, " } rec;\n\n" );
576 fprintf( outfile
, " extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
577 fprintf( outfile
, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB
);
578 fprintf( outfile
, " rec.flags = %d;\n", EH_NONCONTINUABLE
);
579 fprintf( outfile
, " rec.rec = 0;\n" );
580 fprintf( outfile
, " rec.params = 2;\n" );
581 fprintf( outfile
, " rec.info[0] = dllname;\n" );
582 fprintf( outfile
, " rec.info[1] = func;\n" );
583 fprintf( outfile
, "#ifdef __GNUC__\n" );
584 fprintf( outfile
, " rec.addr = __builtin_return_address(1);\n" );
585 fprintf( outfile
, "#else\n" );
586 fprintf( outfile
, " rec.addr = 0;\n" );
587 fprintf( outfile
, "#endif\n" );
588 fprintf( outfile
, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
591 for (i
= 0; i
<= Limit
; i
++)
593 ORDDEF
*odp
= Ordinals
[i
];
594 if (!odp
|| odp
->type
!= TYPE_STUB
) continue;
595 odp
->link_name
= xrealloc( odp
->link_name
, strlen(odp
->name
) + 13 );
596 strcpy( odp
->link_name
, "__wine_stub_" );
597 strcat( odp
->link_name
, odp
->name
);
598 for (p
= odp
->link_name
; *p
; p
++) if (!isalnum(*p
)) *p
= '_';
599 fprintf( outfile
, "static void %s(void) { __wine_unimplemented(\"%s\"); }\n",
600 odp
->link_name
, odp
->name
);
605 /*******************************************************************
608 * Build a Win16 assembly file from a spec file.
610 void BuildSpec16File( FILE *outfile
)
612 ORDDEF
**type
, **typelist
;
613 int i
, nFuncs
, nTypes
;
614 int code_offset
, data_offset
, module_size
, res_size
;
617 unsigned short code_selector
= __get_cs();
622 output_file_header( outfile
);
623 fprintf( outfile
, "extern unsigned short __wine_call_from_16_word();\n" );
624 fprintf( outfile
, "extern unsigned int __wine_call_from_16_long();\n" );
625 fprintf( outfile
, "extern void __wine_call_from_16_regs();\n" );
626 fprintf( outfile
, "extern void __wine_call_from_16_thunk();\n" );
628 data
= (unsigned char *)xmalloc( 0x10000 );
629 memset( data
, 0, 16 );
633 fprintf( outfile
, "static const char dllname[] = \"%s\";\n\n", DLLName
);
635 output_stub_funcs( outfile
);
637 /* Build sorted list of all argument types, without duplicates */
639 typelist
= (ORDDEF
**)calloc( Limit
+1, sizeof(ORDDEF
*) );
641 for (i
= nFuncs
= 0; i
<= Limit
; i
++)
643 ORDDEF
*odp
= Ordinals
[i
];
651 typelist
[nFuncs
++] = odp
;
658 qsort( typelist
, nFuncs
, sizeof(ORDDEF
*), Spec16TypeCompare
);
663 typelist
[nTypes
++] = typelist
[i
++];
664 while ( i
< nFuncs
&& Spec16TypeCompare( typelist
+ i
, typelist
+ nTypes
-1 ) == 0 )
668 /* Output CallFrom16 routines needed by this .spec file */
670 for ( i
= 0; i
< nTypes
; i
++ )
674 strcpy( profile
, get_function_name( typelist
[i
] ));
675 BuildCallFrom16Func( outfile
, profile
, DLLName
, TRUE
);
679 /* Output the DLL functions prototypes */
681 for (i
= 0; i
<= Limit
; i
++)
683 ORDDEF
*odp
= Ordinals
[i
];
690 fprintf( outfile
, "extern void %s();\n", odp
->link_name
);
697 /* Output code segment */
699 fprintf( outfile
, "\n#include \"pshpack1.h\"\n" );
700 fprintf( outfile
, "\nstatic struct code_segment\n{\n" );
701 fprintf( outfile
, " struct {\n" );
703 fprintf( outfile
, " unsigned char pushl;\n" ); /* pushl $relay */
704 fprintf( outfile
, " void *relay;\n" );
705 fprintf( outfile
, " unsigned char lcall;\n" ); /* lcall __FLATCS__:glue */
706 fprintf( outfile
, " void *glue;\n" );
707 fprintf( outfile
, " unsigned short flatcs;\n" );
709 fprintf( outfile
, " unsigned short lret;\n" ); /* lret $args */
710 fprintf( outfile
, " unsigned short args;\n" );
711 fprintf( outfile
, " unsigned int arg_types[2];\n" );
712 fprintf( outfile
, " } call[%d];\n", nTypes
);
713 fprintf( outfile
, " struct {\n" );
715 fprintf( outfile
, " unsigned short pushw_bp;\n" ); /* pushw %bp */
716 fprintf( outfile
, " unsigned char pushl;\n" ); /* pushl $target */
718 fprintf( outfile
, " void (*target)();\n" );
719 fprintf( outfile
, " unsigned short call;\n" ); /* call CALLFROM16 */
720 fprintf( outfile
, " short callfrom16;\n" );
721 fprintf( outfile
, " } entry[%d];\n", nFuncs
);
722 fprintf( outfile
, "} code_segment =\n{\n {\n" );
726 for ( i
= 0; i
< nTypes
; i
++ )
728 char profile
[101], *arg
;
729 unsigned int arg_types
[2];
732 strcpy( profile
, get_function_name( typelist
[i
] ));
733 if ( typelist
[i
]->type
!= TYPE_CDECL
)
734 for ( arg
= typelist
[i
]->u
.func
.arg_types
; *arg
; arg
++ )
738 case 's': /* s_word */
741 case 'l': /* long or segmented pointer */
742 case 'T': /* segmented pointer to null-terminated string */
743 case 'p': /* linear pointer */
744 case 't': /* linear pointer to null-terminated string */
749 if (typelist
[i
]->flags
& FLAG_INTERRUPT
) argsize
+= 2;
751 /* build the arg types bit fields */
752 arg_types
[0] = arg_types
[1] = 0;
753 for (j
= 0; typelist
[i
]->u
.func
.arg_types
[j
]; j
++)
756 switch(typelist
[i
]->u
.func
.arg_types
[j
])
758 case 'w': type
= ARG_WORD
; break;
759 case 's': type
= ARG_SWORD
; break;
760 case 'l': type
= ARG_LONG
; break;
761 case 'p': type
= ARG_PTR
; break;
762 case 't': type
= ARG_STR
; break;
763 case 'T': type
= ARG_SEGSTR
; break;
765 arg_types
[j
/ 10] |= type
<< (3 * (j
% 10));
767 if (typelist
[i
]->flags
& (FLAG_REGISTER
|FLAG_INTERRUPT
)) arg_types
[0] |= ARG_REGISTER
;
768 if (typelist
[i
]->type
== TYPE_PASCAL_16
) arg_types
[0] |= ARG_RET16
;
771 fprintf( outfile
, " { 0x68, %s_CallFrom16_%s, 0x9a, __wine_call_from_16_%s,\n",
773 (typelist
[i
]->flags
& (FLAG_REGISTER
|FLAG_INTERRUPT
)) ? "regs":
774 typelist
[i
]->type
== TYPE_PASCAL_16
? "word" : "long" );
776 fprintf( outfile
, " 0x%04x, 0xca66, %d, { 0x%08x, 0x%08x } },\n",
777 code_selector
, argsize
, arg_types
[0], arg_types
[1] );
779 fprintf( outfile
, " 0x%04x, 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
780 code_selector
, arg_types
[0], arg_types
[1] );
783 fprintf( outfile
, " { 0xca66, %d, { 0x%08x, 0x%08x } },\n",
784 argsize
, arg_types
[0], arg_types
[1] );
786 fprintf( outfile
, " { 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
787 arg_types
[0], arg_types
[1] );
789 code_offset
+= sizeof(CALLFROM16
);
791 fprintf( outfile
, " },\n {\n" );
793 for (i
= 0; i
<= Limit
; i
++)
795 ORDDEF
*odp
= Ordinals
[i
];
800 odp
->offset
= LOWORD(odp
->u
.abs
.value
);
804 odp
->offset
= data_offset
;
805 data_offset
+= StoreVariableCode( data
+ data_offset
, 4, odp
);
812 type
= bsearch( &odp
, typelist
, nTypes
, sizeof(ORDDEF
*), Spec16TypeCompare
);
815 fprintf( outfile
, " /* %s.%d */ ", DLLName
, i
);
817 fprintf( outfile
, "{ 0x5566, 0x68, %s, 0xe866, %d /* %s */ },\n",
819 fprintf( outfile
, "{ %s, 0xe866, %d, /* %s */ },\n",
822 (type
-typelist
)*sizeof(CALLFROM16
) -
823 (code_offset
+ sizeof(ENTRYPOINT16
)),
824 get_function_name( odp
) );
826 odp
->offset
= code_offset
;
827 code_offset
+= sizeof(ENTRYPOINT16
);
831 fprintf(stderr
,"build: function type %d not available for Win16\n",
837 fprintf( outfile
, " }\n};\n" );
839 /* Output data segment */
841 dump_bytes( outfile
, data
, data_offset
, "Data_Segment", 0 );
843 /* Build the module */
845 module_size
= BuildModule16( outfile
, code_offset
, data_offset
);
846 res_size
= output_res16_data( outfile
);
848 /* Output the DLL descriptor */
850 fprintf( outfile
, "#include \"poppack.h\"\n\n" );
852 fprintf( outfile
, "static const struct dll_descriptor\n{\n" );
853 fprintf( outfile
, " unsigned char *module_start;\n" );
854 fprintf( outfile
, " int module_size;\n" );
855 fprintf( outfile
, " struct code_segment *code_start;\n" );
856 fprintf( outfile
, " unsigned char *data_start;\n" );
857 fprintf( outfile
, " const char *owner;\n" );
858 fprintf( outfile
, " const unsigned char *rsrc;\n" );
859 fprintf( outfile
, "} descriptor =\n{\n" );
860 fprintf( outfile
, " Module,\n" );
861 fprintf( outfile
, " sizeof(Module),\n" );
862 fprintf( outfile
, " &code_segment,\n" );
863 fprintf( outfile
, " Data_Segment,\n" );
864 fprintf( outfile
, " \"%s\",\n", owner_name
);
865 fprintf( outfile
, " %s\n", res_size
? "resource_data" : "0" );
866 fprintf( outfile
, "};\n" );
868 /* Output the DLL constructor */
870 fprintf( outfile
, "#ifndef __GNUC__\n" );
871 fprintf( outfile
, "static void __asm__dummy_dll_init(void) {\n" );
872 fprintf( outfile
, "#endif /* defined(__GNUC__) */\n" );
874 #if defined(__i386__)
875 fprintf( outfile
, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
876 fprintf( outfile
, " \"\\tcall " PREFIX
"__wine_spec_%s_init\\n\"\n", DLLName
);
877 fprintf( outfile
, " \"\\t.previous\\n\");\n" );
878 #elif defined(__sparc__)
879 fprintf( outfile
, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
880 fprintf( outfile
, " \"\\tcall " PREFIX
"__wine_spec_%s_init\\n\"\n", DLLName
);
881 fprintf( outfile
, " \"\\tnop\\n\"\n" );
882 fprintf( outfile
, " \"\\t.previous\\n\");\n" );
883 #elif defined(__PPC__)
884 fprintf( outfile
, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
885 fprintf( outfile
, " \"\\tbl " PREFIX
"__wine_spec_%s_init\\n\"\n", DLLName
);
886 fprintf( outfile
, " \"\\t.previous\\n\");\n" );
888 #error You need to define the DLL constructor for your architecture
891 fprintf( outfile
, "#ifndef __GNUC__\n" );
892 fprintf( outfile
, "}\n" );
893 fprintf( outfile
, "#endif /* defined(__GNUC__) */\n\n" );
896 "void __wine_spec_%s_init(void)\n"
898 " extern void __wine_register_dll_16( const struct dll_descriptor *descr );\n"
899 " __wine_register_dll_16( &descriptor );\n"
904 /*******************************************************************
907 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
909 void BuildGlue( FILE *outfile
, FILE *infile
)
915 output_file_header( outfile
);
918 fprintf( outfile
, "extern unsigned short __stdcall wine_call_to_16_word( void (*target)(), int args );\n" );
919 fprintf( outfile
, "extern unsigned int __stdcall wine_call_to_16_long( void (*target)(), int args );\n\n" );
921 fprintf( outfile
, "#include <assert.h>\n\n" );
924 /* Build the callback glue functions */
926 while (fgets( buffer
, sizeof(buffer
), infile
))
928 if (strstr( buffer
, "### start build ###" )) break;
930 while (fgets( buffer
, sizeof(buffer
), infile
))
933 if ( (p
= strstr( buffer
, "CallTo16_" )) != NULL
)
935 char *q
, *profile
= p
+ strlen( "CallTo16_" );
936 for (q
= profile
; (*q
== '_') || isalpha(*q
); q
++ )
939 for (q
= p
-1; q
> buffer
&& ((*q
== '_') || isalnum(*q
)); q
-- )
941 if ( ++q
< p
) p
[-1] = '\0'; else q
= "";
942 BuildCallTo16Func( outfile
, profile
, q
);
944 if (strstr( buffer
, "### stop build ###" )) break;