dinput: Move SetEventNotification and associated event into base class.
[wine] / tools / winebuild / spec32.c
1 /*
2  * 32-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  * 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.
14  *
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.
19  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <assert.h>
29 #include <ctype.h>
30 #include <stdarg.h>
31 #include <string.h>
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wine/exception.h"
36 #include "build.h"
37
38
39 /* check if entry point needs a relay thunk */
40 static inline int needs_relay( const ORDDEF *odp )
41 {
42     /* skip nonexistent entry points */
43     if (!odp) return 0;
44     /* skip non-functions */
45     if ((odp->type != TYPE_STDCALL) && (odp->type != TYPE_CDECL)) return 0;
46     /* skip norelay and forward entry points */
47     if (odp->flags & (FLAG_NORELAY|FLAG_FORWARD)) return 0;
48     return 1;
49 }
50
51 /* check if dll will output relay thunks */
52 int has_relays( DLLSPEC *spec )
53 {
54     unsigned int i;
55
56     if (target_cpu != CPU_x86) return 0;
57
58     for (i = spec->base; i <= spec->limit; i++)
59     {
60         ORDDEF *odp = spec->ordinals[i];
61         if (needs_relay( odp )) return 1;
62     }
63     return 0;
64 }
65
66 /*******************************************************************
67  *         output_relay_debug
68  *
69  * Output entry points for relay debugging
70  */
71 static void output_relay_debug( DLLSPEC *spec )
72 {
73     unsigned int i, j, args, flags;
74
75     /* first the table of entry point offsets */
76
77     output( "\t%s\n", get_asm_rodata_section() );
78     output( "\t.align %d\n", get_alignment(4) );
79     output( ".L__wine_spec_relay_entry_point_offsets:\n" );
80
81     for (i = spec->base; i <= spec->limit; i++)
82     {
83         ORDDEF *odp = spec->ordinals[i];
84
85         if (needs_relay( odp ))
86             output( "\t.long .L__wine_spec_relay_entry_point_%d-__wine_spec_relay_entry_points\n", i );
87         else
88             output( "\t.long 0\n" );
89     }
90
91     /* then the table of argument types */
92
93     output( "\t.align %d\n", get_alignment(4) );
94     output( ".L__wine_spec_relay_arg_types:\n" );
95
96     for (i = spec->base; i <= spec->limit; i++)
97     {
98         ORDDEF *odp = spec->ordinals[i];
99         unsigned int mask = 0;
100
101         if (needs_relay( odp ))
102         {
103             for (j = 0; j < 16 && odp->u.func.arg_types[j]; j++)
104             {
105                 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
106                 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
107             }
108         }
109         output( "\t.long 0x%08x\n", mask );
110     }
111
112     /* then the relay thunks */
113
114     output( "\t.text\n" );
115     output( "__wine_spec_relay_entry_points:\n" );
116     output( "\tnop\n" );  /* to avoid 0 offset */
117
118     for (i = spec->base; i <= spec->limit; i++)
119     {
120         ORDDEF *odp = spec->ordinals[i];
121
122         if (!needs_relay( odp )) continue;
123
124         output( "\t.align %d\n", get_alignment(4) );
125         output( ".L__wine_spec_relay_entry_point_%d:\n", i );
126
127         if (odp->flags & FLAG_REGISTER)
128             output( "\tpushl %%eax\n" );
129         else
130             output( "\tpushl %%esp\n" );
131
132         args = strlen(odp->u.func.arg_types);
133         flags = 0;
134         if (odp->flags & FLAG_RET64) flags |= 1;
135         if (odp->type == TYPE_STDCALL) flags |= 2;
136         output( "\tpushl $%u\n", (flags << 24) | (args << 16) | (i - spec->base) );
137
138         if (UsePIC)
139         {
140             output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
141             output( "1:\tleal .L__wine_spec_relay_descr-1b(%%eax),%%eax\n" );
142         }
143         else output( "\tmovl $.L__wine_spec_relay_descr,%%eax\n" );
144         output( "\tpushl %%eax\n" );
145
146         if (odp->flags & FLAG_REGISTER)
147         {
148             output( "\tcall *8(%%eax)\n" );
149         }
150         else
151         {
152             output( "\tcall *4(%%eax)\n" );
153             if (odp->type == TYPE_STDCALL)
154                 output( "\tret $%u\n", args * get_ptr_size() );
155             else
156                 output( "\tret\n" );
157         }
158     }
159 }
160
161 /*******************************************************************
162  *         output_exports
163  *
164  * Output the export table for a Win32 module.
165  */
166 static void output_exports( DLLSPEC *spec )
167 {
168     int i, fwd_size = 0;
169     int nr_exports = spec->base <= spec->limit ? spec->limit - spec->base + 1 : 0;
170
171     if (!nr_exports) return;
172
173     output( "\n/* export table */\n\n" );
174     output( "\t.data\n" );
175     output( "\t.align %d\n", get_alignment(4) );
176     output( ".L__wine_spec_exports:\n" );
177
178     /* export directory header */
179
180     output( "\t.long 0\n" );                       /* Characteristics */
181     output( "\t.long 0\n" );                       /* TimeDateStamp */
182     output( "\t.long 0\n" );                       /* MajorVersion/MinorVersion */
183     output( "\t.long .L__wine_spec_exp_names-.L__wine_spec_rva_base\n" ); /* Name */
184     output( "\t.long %u\n", spec->base );          /* Base */
185     output( "\t.long %u\n", nr_exports );          /* NumberOfFunctions */
186     output( "\t.long %u\n", spec->nb_names );      /* NumberOfNames */
187     output( "\t.long .L__wine_spec_exports_funcs-.L__wine_spec_rva_base\n" ); /* AddressOfFunctions */
188     if (spec->nb_names)
189     {
190         output( "\t.long .L__wine_spec_exp_name_ptrs-.L__wine_spec_rva_base\n" ); /* AddressOfNames */
191         output( "\t.long .L__wine_spec_exp_ordinals-.L__wine_spec_rva_base\n" );  /* AddressOfNameOrdinals */
192     }
193     else
194     {
195         output( "\t.long 0\n" );  /* AddressOfNames */
196         output( "\t.long 0\n" );  /* AddressOfNameOrdinals */
197     }
198
199     /* output the function pointers */
200
201     output( "\n.L__wine_spec_exports_funcs:\n" );
202     for (i = spec->base; i <= spec->limit; i++)
203     {
204         ORDDEF *odp = spec->ordinals[i];
205         if (!odp) output( "\t%s 0\n", get_asm_ptr_keyword() );
206         else switch(odp->type)
207         {
208         case TYPE_EXTERN:
209         case TYPE_STDCALL:
210         case TYPE_VARARGS:
211         case TYPE_CDECL:
212             if (odp->flags & FLAG_FORWARD)
213             {
214                 output( "\t%s .L__wine_spec_forwards+%u\n", get_asm_ptr_keyword(), fwd_size );
215                 fwd_size += strlen(odp->link_name) + 1;
216             }
217             else if (odp->flags & FLAG_EXT_LINK)
218             {
219                 output( "\t%s %s_%s\n",
220                          get_asm_ptr_keyword(), asm_name("__wine_spec_ext_link"), odp->link_name );
221             }
222             else
223             {
224                 output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(odp->link_name) );
225             }
226             break;
227         case TYPE_STUB:
228             output( "\t%s %s\n", get_asm_ptr_keyword(),
229                      asm_name( get_stub_name( odp, spec )) );
230             break;
231         default:
232             assert(0);
233         }
234     }
235
236     if (spec->nb_names)
237     {
238         /* output the function name pointers */
239
240         int namepos = strlen(spec->file_name) + 1;
241
242         output( "\n.L__wine_spec_exp_name_ptrs:\n" );
243         for (i = 0; i < spec->nb_names; i++)
244         {
245             output( "\t.long .L__wine_spec_exp_names+%u-.L__wine_spec_rva_base\n", namepos );
246             namepos += strlen(spec->names[i]->name) + 1;
247         }
248
249         /* output the function ordinals */
250
251         output( "\n.L__wine_spec_exp_ordinals:\n" );
252         for (i = 0; i < spec->nb_names; i++)
253         {
254             output( "\t%s %d\n",
255                      get_asm_short_keyword(), spec->names[i]->ordinal - spec->base );
256         }
257         if (spec->nb_names % 2)
258         {
259             output( "\t%s 0\n", get_asm_short_keyword() );
260         }
261     }
262
263     /* output the export name strings */
264
265     output( "\n.L__wine_spec_exp_names:\n" );
266     output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
267     for (i = 0; i < spec->nb_names; i++)
268         output( "\t%s \"%s\"\n",
269                  get_asm_string_keyword(), spec->names[i]->name );
270
271     /* output forward strings */
272
273     if (fwd_size)
274     {
275         output( "\n.L__wine_spec_forwards:\n" );
276         for (i = spec->base; i <= spec->limit; i++)
277         {
278             ORDDEF *odp = spec->ordinals[i];
279             if (odp && (odp->flags & FLAG_FORWARD))
280                 output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->link_name );
281         }
282     }
283     output( "\t.align %d\n", get_alignment(get_ptr_size()) );
284     output( ".L__wine_spec_exports_end:\n" );
285
286     /* output relays */
287
288     /* we only support relay debugging on i386 */
289     if (target_cpu != CPU_x86)
290     {
291         output( "\t%s 0\n", get_asm_ptr_keyword() );
292         return;
293     }
294
295     output( ".L__wine_spec_relay_descr:\n" );
296     output( "\t%s 0xdeb90001\n", get_asm_ptr_keyword() );  /* magic */
297     output( "\t%s 0,0\n", get_asm_ptr_keyword() );         /* relay funcs */
298     output( "\t%s 0\n", get_asm_ptr_keyword() );           /* private data */
299     output( "\t%s __wine_spec_relay_entry_points\n", get_asm_ptr_keyword() );
300     output( "\t%s .L__wine_spec_relay_entry_point_offsets\n", get_asm_ptr_keyword() );
301     output( "\t%s .L__wine_spec_relay_arg_types\n", get_asm_ptr_keyword() );
302
303     output_relay_debug( spec );
304 }
305
306
307 /*******************************************************************
308  *         output_asm_constructor
309  *
310  * Output code for calling a dll constructor.
311  */
312 static void output_asm_constructor( const char *constructor )
313 {
314     if (target_platform == PLATFORM_APPLE)
315     {
316         /* Mach-O doesn't have an init section */
317         output( "\n\t.mod_init_func\n" );
318         output( "\t.align %d\n", get_alignment(4) );
319         output( "\t.long %s\n", asm_name(constructor) );
320     }
321     else
322     {
323         output( "\n\t.section \".init\",\"ax\"\n" );
324         switch(target_cpu)
325         {
326         case CPU_x86:
327         case CPU_x86_64:
328             output( "\tcall %s\n", asm_name(constructor) );
329             break;
330         case CPU_SPARC:
331             output( "\tcall %s\n", asm_name(constructor) );
332             output( "\tnop\n" );
333             break;
334         case CPU_ALPHA:
335             output( "\tjsr $26,%s\n", asm_name(constructor) );
336             break;
337         case CPU_POWERPC:
338             output( "\tbl %s\n", asm_name(constructor) );
339             break;
340         }
341     }
342 }
343
344
345 /*******************************************************************
346  *         BuildSpec32File
347  *
348  * Build a Win32 C file from a spec file.
349  */
350 void BuildSpec32File( DLLSPEC *spec )
351 {
352     int machine = 0;
353     unsigned int page_size = get_page_size();
354
355     resolve_imports( spec );
356     output_standard_file_header();
357
358     /* Reserve some space for the PE header */
359
360     output( "\t.text\n" );
361     output( "\t.align %d\n", get_alignment(page_size) );
362     output( "__wine_spec_pe_header:\n" );
363     if (target_platform == PLATFORM_APPLE)
364         output( "\t.space 65536\n" );
365     else
366         output( "\t.skip 65536\n" );
367
368     /* Output the NT header */
369
370     output( "\n\t.data\n" );
371     output( "\t.align %d\n", get_alignment(get_ptr_size()) );
372     output( "%s\n", asm_globl("__wine_spec_nt_header") );
373     output( ".L__wine_spec_rva_base:\n" );
374
375     output( "\t.long 0x%04x\n", IMAGE_NT_SIGNATURE );    /* Signature */
376     switch(target_cpu)
377     {
378     case CPU_x86:     machine = IMAGE_FILE_MACHINE_I386; break;
379     case CPU_x86_64:  machine = IMAGE_FILE_MACHINE_AMD64; break;
380     case CPU_POWERPC: machine = IMAGE_FILE_MACHINE_POWERPC; break;
381     case CPU_ALPHA:   machine = IMAGE_FILE_MACHINE_ALPHA; break;
382     case CPU_SPARC:   machine = IMAGE_FILE_MACHINE_UNKNOWN; break;
383     }
384     output( "\t%s 0x%04x\n",              /* Machine */
385              get_asm_short_keyword(), machine );
386     output( "\t%s 0\n",                   /* NumberOfSections */
387              get_asm_short_keyword() );
388     output( "\t.long 0\n" );              /* TimeDateStamp */
389     output( "\t.long 0\n" );              /* PointerToSymbolTable */
390     output( "\t.long 0\n" );              /* NumberOfSymbols */
391     output( "\t%s %d\n",                  /* SizeOfOptionalHeader */
392              get_asm_short_keyword(),
393              get_ptr_size() == 8 ? IMAGE_SIZEOF_NT_OPTIONAL64_HEADER : IMAGE_SIZEOF_NT_OPTIONAL32_HEADER );
394     output( "\t%s 0x%04x\n",              /* Characteristics */
395              get_asm_short_keyword(), spec->characteristics );
396     output( "\t%s 0x%04x\n",              /* Magic */
397              get_asm_short_keyword(),
398              get_ptr_size() == 8 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC : IMAGE_NT_OPTIONAL_HDR32_MAGIC );
399     output( "\t.byte 0\n" );              /* MajorLinkerVersion */
400     output( "\t.byte 0\n" );              /* MinorLinkerVersion */
401     output( "\t.long 0\n" );              /* SizeOfCode */
402     output( "\t.long 0\n" );              /* SizeOfInitializedData */
403     output( "\t.long 0\n" );              /* SizeOfUninitializedData */
404     /* note: we expand the AddressOfEntryPoint field on 64-bit by overwriting the BaseOfCode field */
405     output( "\t%s %s\n",                  /* AddressOfEntryPoint */
406              get_asm_ptr_keyword(), asm_name(spec->init_func) );
407     if (get_ptr_size() == 4)
408     {
409         output( "\t.long 0\n" );          /* BaseOfCode */
410         output( "\t.long 0\n" );          /* BaseOfData */
411     }
412     output( "\t%s __wine_spec_pe_header\n",         /* ImageBase */
413              get_asm_ptr_keyword() );
414     output( "\t.long %u\n", page_size );  /* SectionAlignment */
415     output( "\t.long %u\n", page_size );  /* FileAlignment */
416     output( "\t%s 1,0\n",                 /* Major/MinorOperatingSystemVersion */
417              get_asm_short_keyword() );
418     output( "\t%s 0,0\n",                 /* Major/MinorImageVersion */
419              get_asm_short_keyword() );
420     output( "\t%s %u,%u\n",               /* Major/MinorSubsystemVersion */
421              get_asm_short_keyword(), spec->subsystem_major, spec->subsystem_minor );
422     output( "\t.long 0\n" );                          /* Win32VersionValue */
423     output( "\t.long %s-.L__wine_spec_rva_base\n",    /* SizeOfImage */
424              asm_name("_end") );
425     output( "\t.long %u\n", page_size );  /* SizeOfHeaders */
426     output( "\t.long 0\n" );              /* CheckSum */
427     output( "\t%s 0x%04x\n",              /* Subsystem */
428              get_asm_short_keyword(), spec->subsystem );
429     output( "\t%s 0\n",                   /* DllCharacteristics */
430              get_asm_short_keyword() );
431     output( "\t%s %u,%u\n",               /* SizeOfStackReserve/Commit */
432              get_asm_ptr_keyword(), (spec->stack_size ? spec->stack_size : 1024) * 1024, page_size );
433     output( "\t%s %u,%u\n",               /* SizeOfHeapReserve/Commit */
434              get_asm_ptr_keyword(), (spec->heap_size ? spec->heap_size : 1024) * 1024, page_size );
435     output( "\t.long 0\n" );              /* LoaderFlags */
436     output( "\t.long 16\n" );             /* NumberOfRvaAndSizes */
437
438     if (spec->base <= spec->limit)   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
439         output( "\t.long .L__wine_spec_exports-.L__wine_spec_rva_base,"
440                  ".L__wine_spec_exports_end-.L__wine_spec_exports\n" );
441     else
442         output( "\t.long 0,0\n" );
443
444     if (has_imports())   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
445         output( "\t.long .L__wine_spec_imports-.L__wine_spec_rva_base,"
446                  ".L__wine_spec_imports_end-.L__wine_spec_imports\n" );
447     else
448         output( "\t.long 0,0\n" );
449
450     if (spec->nb_resources)   /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
451         output( "\t.long .L__wine_spec_resources-.L__wine_spec_rva_base,"
452                  ".L__wine_spec_resources_end-.L__wine_spec_resources\n" );
453     else
454         output( "\t.long 0,0\n" );
455
456     output( "\t.long 0,0\n" );  /* DataDirectory[3] */
457     output( "\t.long 0,0\n" );  /* DataDirectory[4] */
458     output( "\t.long 0,0\n" );  /* DataDirectory[5] */
459     output( "\t.long 0,0\n" );  /* DataDirectory[6] */
460     output( "\t.long 0,0\n" );  /* DataDirectory[7] */
461     output( "\t.long 0,0\n" );  /* DataDirectory[8] */
462     output( "\t.long 0,0\n" );  /* DataDirectory[9] */
463     output( "\t.long 0,0\n" );  /* DataDirectory[10] */
464     output( "\t.long 0,0\n" );  /* DataDirectory[11] */
465     output( "\t.long 0,0\n" );  /* DataDirectory[12] */
466     output( "\t.long 0,0\n" );  /* DataDirectory[13] */
467     output( "\t.long 0,0\n" );  /* DataDirectory[14] */
468     output( "\t.long 0,0\n" );  /* DataDirectory[15] */
469
470     output( "\n\t%s\n", get_asm_string_section() );
471     output( "%s\n", asm_globl("__wine_spec_file_name") );
472     output( ".L__wine_spec_file_name:\n" );
473     output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
474     if (target_platform == PLATFORM_APPLE)
475         output( "\t.lcomm %s,4\n", asm_name("_end") );
476
477     output_stubs( spec );
478     output_exports( spec );
479     output_imports( spec );
480     output_resources( spec );
481     output_asm_constructor( "__wine_spec_init_ctor" );
482     output_gnu_stack_note();
483 }
484
485
486 /*******************************************************************
487  *         BuildDef32File
488  *
489  * Build a Win32 def file from a spec file.
490  */
491 void BuildDef32File( DLLSPEC *spec )
492 {
493     const char *name;
494     int i, total;
495
496     if (spec_file_name)
497         output( "; File generated automatically from %s; do not edit!\n\n",
498                  spec_file_name );
499     else
500         output( "; File generated automatically; do not edit!\n\n" );
501
502     output( "LIBRARY %s\n\n", spec->file_name);
503     output( "EXPORTS\n");
504
505     /* Output the exports and relay entry points */
506
507     for (i = total = 0; i < spec->nb_entry_points; i++)
508     {
509         const ORDDEF *odp = &spec->entry_points[i];
510         int is_data = 0;
511
512         if (!odp) continue;
513
514         if (odp->name) name = odp->name;
515         else if (odp->export_name) name = odp->export_name;
516         else continue;
517
518         if (!(odp->flags & FLAG_PRIVATE)) total++;
519
520         if (odp->type == TYPE_STUB) continue;
521
522         output( "  %s", name );
523
524         switch(odp->type)
525         {
526         case TYPE_EXTERN:
527             is_data = 1;
528             /* fall through */
529         case TYPE_VARARGS:
530         case TYPE_CDECL:
531             /* try to reduce output */
532             if(strcmp(name, odp->link_name) || (odp->flags & FLAG_FORWARD))
533                 output( "=%s", odp->link_name );
534             break;
535         case TYPE_STDCALL:
536         {
537             int at_param = strlen(odp->u.func.arg_types) * get_ptr_size();
538             if (!kill_at) output( "@%d", at_param );
539             if  (odp->flags & FLAG_FORWARD)
540             {
541                 output( "=%s", odp->link_name );
542             }
543             else if (strcmp(name, odp->link_name)) /* try to reduce output */
544             {
545                 output( "=%s", odp->link_name );
546                 if (!kill_at) output( "@%d", at_param );
547             }
548             break;
549         }
550         default:
551             assert(0);
552         }
553         output( " @%d", odp->ordinal );
554         if (!odp->name) output( " NONAME" );
555         if (is_data) output( " DATA" );
556         if (odp->flags & FLAG_PRIVATE) output( " PRIVATE" );
557         output( "\n" );
558     }
559     if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
560 }