winebuild: Add support for specifying thiscall calling convention.
[wine] / tools / winebuild / build.h
1 /*
2  * Copyright 1993 Robert J. Amstadt
3  * Copyright 1995 Martin von Loewis
4  * Copyright 1995, 1996, 1997 Alexandre Julliard
5  * Copyright 1997 Eric Youngdale
6  * Copyright 1999 Ulrich Weigand
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #ifndef __WINE_BUILD_H
24 #define __WINE_BUILD_H
25
26 #ifndef __WINE_CONFIG_H
27 # error You must include config.h to use this header
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #ifndef max
35 #define max(a,b)   (((a) > (b)) ? (a) : (b))
36 #endif
37 #ifndef min
38 #define min(a,b)   (((a) < (b)) ? (a) : (b))
39 #endif
40
41 typedef enum
42 {
43     TYPE_VARIABLE,     /* variable */
44     TYPE_PASCAL,       /* pascal function (Win16) */
45     TYPE_ABS,          /* absolute value (Win16) */
46     TYPE_STUB,         /* unimplemented stub */
47     TYPE_STDCALL,      /* stdcall function (Win32) */
48     TYPE_CDECL,        /* cdecl function (Win32) */
49     TYPE_VARARGS,      /* varargs function (Win32) */
50     TYPE_THISCALL,     /* thiscall function (Win32 on i386) */
51     TYPE_EXTERN,       /* external symbol (Win32) */
52     TYPE_NBTYPES
53 } ORD_TYPE;
54
55 typedef enum
56 {
57     SPEC_WIN16,
58     SPEC_WIN32
59 } SPEC_TYPE;
60
61 typedef struct
62 {
63     int n_values;
64     int *values;
65 } ORD_VARIABLE;
66
67 typedef struct
68 {
69     char arg_types[21];
70 } ORD_FUNCTION;
71
72 typedef struct
73 {
74     unsigned short value;
75 } ORD_ABS;
76
77 typedef struct
78 {
79     ORD_TYPE    type;
80     int         ordinal;
81     int         lineno;
82     int         flags;
83     char       *name;         /* public name of this function */
84     char       *link_name;    /* name of the C symbol to link to */
85     char       *export_name;  /* name exported under for noname exports */
86     union
87     {
88         ORD_VARIABLE   var;
89         ORD_FUNCTION   func;
90         ORD_ABS        abs;
91     } u;
92 } ORDDEF;
93
94 typedef struct
95 {
96     char            *src_name;           /* file name of the source spec file */
97     char            *file_name;          /* file name of the dll */
98     char            *dll_name;           /* internal name of the dll */
99     char            *init_func;          /* initialization routine */
100     char            *main_module;        /* main Win32 module for Win16 specs */
101     SPEC_TYPE        type;               /* type of dll (Win16/Win32) */
102     int              base;               /* ordinal base */
103     int              limit;              /* ordinal limit */
104     int              stack_size;         /* exe stack size */
105     int              heap_size;          /* exe heap size */
106     int              nb_entry_points;    /* number of used entry points */
107     int              alloc_entry_points; /* number of allocated entry points */
108     int              nb_names;           /* number of entry points with names */
109     unsigned int     nb_resources;       /* number of resources */
110     int              characteristics;    /* characteristics for the PE header */
111     int              dll_characteristics;/* DLL characteristics for the PE header */
112     int              subsystem;          /* subsystem id */
113     int              subsystem_major;    /* subsystem version major number */
114     int              subsystem_minor;    /* subsystem version minor number */
115     ORDDEF          *entry_points;       /* dll entry points */
116     ORDDEF         **names;              /* array of entry point names (points into entry_points) */
117     ORDDEF         **ordinals;           /* array of dll ordinals (points into entry_points) */
118     struct resource *resources;          /* array of dll resources (format differs between Win16/Win32) */
119 } DLLSPEC;
120
121 enum target_cpu
122 {
123     CPU_x86, CPU_x86_64, CPU_SPARC, CPU_ALPHA, CPU_POWERPC, CPU_ARM, CPU_LAST = CPU_ARM
124 };
125
126 enum target_platform
127 {
128     PLATFORM_UNSPECIFIED,
129     PLATFORM_APPLE,
130     PLATFORM_FREEBSD,
131     PLATFORM_SOLARIS,
132     PLATFORM_WINDOWS
133 };
134
135 extern char *target_alias;
136 extern enum target_cpu target_cpu;
137 extern enum target_platform target_platform;
138
139 struct strarray
140 {
141     const char **str;
142     unsigned int count;
143     unsigned int max;
144 };
145
146 /* entry point flags */
147 #define FLAG_NORELAY   0x01  /* don't use relay debugging for this function */
148 #define FLAG_NONAME    0x02  /* don't export function by name */
149 #define FLAG_RET16     0x04  /* function returns a 16-bit value */
150 #define FLAG_RET64     0x08  /* function returns a 64-bit value */
151 #define FLAG_REGISTER  0x10  /* use register calling convention */
152 #define FLAG_PRIVATE   0x20  /* function is private (cannot be imported) */
153 #define FLAG_ORDINAL   0x40  /* function should be imported by ordinal */
154
155 #define FLAG_FORWARD   0x100  /* function is a forwarded name */
156 #define FLAG_EXT_LINK  0x200  /* function links to an external symbol */
157 #define FLAG_EXPORT32  0x400  /* 32-bit export in 16-bit spec file */
158
159 #define FLAG_CPU(cpu)  (0x01000 << (cpu))
160 #define FLAG_CPU_MASK  (FLAG_CPU(CPU_LAST + 1) - FLAG_CPU(0))
161 #define FLAG_CPU_WIN64 (FLAG_CPU(CPU_x86_64))
162 #define FLAG_CPU_WIN32 (FLAG_CPU_MASK & ~FLAG_CPU_WIN64)
163
164 #define MAX_ORDINALS  65535
165
166 /* some Windows constants */
167
168 #define IMAGE_FILE_RELOCS_STRIPPED         0x0001 /* No relocation info */
169 #define IMAGE_FILE_EXECUTABLE_IMAGE        0x0002
170 #define IMAGE_FILE_LINE_NUMS_STRIPPED      0x0004
171 #define IMAGE_FILE_LOCAL_SYMS_STRIPPED     0x0008
172 #define IMAGE_FILE_AGGRESIVE_WS_TRIM       0x0010
173 #define IMAGE_FILE_LARGE_ADDRESS_AWARE     0x0020
174 #define IMAGE_FILE_16BIT_MACHINE           0x0040
175 #define IMAGE_FILE_BYTES_REVERSED_LO       0x0080
176 #define IMAGE_FILE_32BIT_MACHINE           0x0100
177 #define IMAGE_FILE_DEBUG_STRIPPED          0x0200
178 #define IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP 0x0400
179 #define IMAGE_FILE_NET_RUN_FROM_SWAP       0x0800
180 #define IMAGE_FILE_SYSTEM                  0x1000
181 #define IMAGE_FILE_DLL                     0x2000
182 #define IMAGE_FILE_UP_SYSTEM_ONLY          0x4000
183 #define IMAGE_FILE_BYTES_REVERSED_HI       0x8000
184
185 #define IMAGE_DLLCHARACTERISTICS_NX_COMPAT 0x0100
186
187 #define IMAGE_SUBSYSTEM_NATIVE      1
188 #define IMAGE_SUBSYSTEM_WINDOWS_GUI 2
189 #define IMAGE_SUBSYSTEM_WINDOWS_CUI 3
190
191 /* global functions */
192
193 #ifndef __GNUC__
194 #define __attribute__(X)
195 #endif
196
197 #ifndef DECLSPEC_NORETURN
198 # if defined(_MSC_VER) && (_MSC_VER >= 1200) && !defined(MIDL_PASS)
199 #  define DECLSPEC_NORETURN __declspec(noreturn)
200 # else
201 #  define DECLSPEC_NORETURN __attribute__((noreturn))
202 # endif
203 #endif
204
205 extern void *xmalloc (size_t size);
206 extern void *xrealloc (void *ptr, size_t size);
207 extern char *xstrdup( const char *str );
208 extern char *strupper(char *s);
209 extern int strendswith(const char* str, const char* end);
210 extern char *strmake(const char* fmt, ...) __attribute__((__format__ (__printf__, 1, 2 )));
211 extern struct strarray *strarray_init(void);
212 extern void strarray_add( struct strarray *array, ... );
213 extern void strarray_addv( struct strarray *array, char * const *argv );
214 extern void strarray_free( struct strarray *array );
215 extern DECLSPEC_NORETURN void fatal_error( const char *msg, ... )
216    __attribute__ ((__format__ (__printf__, 1, 2)));
217 extern DECLSPEC_NORETURN void fatal_perror( const char *msg, ... )
218    __attribute__ ((__format__ (__printf__, 1, 2)));
219 extern void error( const char *msg, ... )
220    __attribute__ ((__format__ (__printf__, 1, 2)));
221 extern void warning( const char *msg, ... )
222    __attribute__ ((__format__ (__printf__, 1, 2)));
223 extern int output( const char *format, ... )
224    __attribute__ ((__format__ (__printf__, 1, 2)));
225 extern void output_cfi( const char *format, ... )
226    __attribute__ ((__format__ (__printf__, 1, 2)));
227 extern void spawn( struct strarray *array );
228 extern char *find_tool( const char *name, const char * const *names );
229 extern struct strarray *get_as_command(void);
230 extern struct strarray *get_ld_command(void);
231 extern const char *get_nm_command(void);
232 extern char *get_temp_file_name( const char *prefix, const char *suffix );
233 extern void output_standard_file_header(void);
234 extern FILE *open_input_file( const char *srcdir, const char *name );
235 extern void close_input_file( FILE *file );
236 extern void dump_bytes( const void *buffer, unsigned int size );
237 extern int remove_stdcall_decoration( char *name );
238 extern void assemble_file( const char *src_file, const char *obj_file );
239 extern DLLSPEC *alloc_dll_spec(void);
240 extern void free_dll_spec( DLLSPEC *spec );
241 extern const char *make_c_identifier( const char *str );
242 extern const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec );
243 extern enum target_cpu get_cpu_from_name( const char *name );
244 extern unsigned int get_alignment(unsigned int align);
245 extern unsigned int get_page_size(void);
246 extern unsigned int get_ptr_size(void);
247 extern const char *asm_name( const char *func );
248 extern const char *func_declaration( const char *func );
249 extern const char *asm_globl( const char *func );
250 extern const char *get_asm_ptr_keyword(void);
251 extern const char *get_asm_string_keyword(void);
252 extern const char *get_asm_short_keyword(void);
253 extern const char *get_asm_rodata_section(void);
254 extern const char *get_asm_string_section(void);
255 extern void output_function_size( const char *name );
256 extern void output_gnu_stack_note(void);
257
258 extern void add_import_dll( const char *name, const char *filename );
259 extern void add_delayed_import( const char *name );
260 extern void add_extra_ld_symbol( const char *name );
261 extern void read_undef_symbols( DLLSPEC *spec, char **argv );
262 extern void resolve_imports( DLLSPEC *spec );
263 extern int is_undefined( const char *name );
264 extern int has_imports(void);
265 extern int has_relays( DLLSPEC *spec );
266 extern void output_get_pc_thunk(void);
267 extern void output_module( DLLSPEC *spec );
268 extern void output_stubs( DLLSPEC *spec );
269 extern void output_imports( DLLSPEC *spec );
270 extern void output_import_lib( DLLSPEC *spec, char **argv );
271 extern void output_exports( DLLSPEC *spec );
272 extern int load_res32_file( const char *name, DLLSPEC *spec );
273 extern void output_resources( DLLSPEC *spec );
274 extern void output_bin_resources( DLLSPEC *spec, unsigned int start_rva );
275 extern void output_fake_module( DLLSPEC *spec );
276 extern void output_def_file( DLLSPEC *spec, int include_private );
277 extern void load_res16_file( const char *name, DLLSPEC *spec );
278 extern void output_res16_data( DLLSPEC *spec );
279 extern void output_bin_res16_data( DLLSPEC *spec );
280 extern void output_res16_directory( DLLSPEC *spec );
281 extern void output_bin_res16_directory( DLLSPEC *spec, unsigned int data_offset );
282 extern void output_spec16_file( DLLSPEC *spec );
283 extern void output_fake_module16( DLLSPEC *spec16 );
284 extern void output_res_o_file( DLLSPEC *spec );
285 extern void output_asm_relays(void);
286 extern void output_asm_relays16(void);
287
288 extern void BuildSpec32File( DLLSPEC *spec );
289
290 extern void add_16bit_exports( DLLSPEC *spec32, DLLSPEC *spec16 );
291 extern int parse_spec_file( FILE *file, DLLSPEC *spec );
292 extern int parse_def_file( FILE *file, DLLSPEC *spec );
293
294 /* buffer management */
295
296 extern int byte_swapped;
297 extern const char *input_buffer_filename;
298 extern const unsigned char *input_buffer;
299 extern size_t input_buffer_pos;
300 extern size_t input_buffer_size;
301 extern unsigned char *output_buffer;
302 extern size_t output_buffer_pos;
303 extern size_t output_buffer_size;
304
305 extern void init_input_buffer( const char *file );
306 extern void init_output_buffer(void);
307 extern void flush_output_buffer(void);
308 extern unsigned char get_byte(void);
309 extern unsigned short get_word(void);
310 extern unsigned int get_dword(void);
311 extern void put_data( const void *data, size_t size );
312 extern void put_byte( unsigned char val );
313 extern void put_word( unsigned short val );
314 extern void put_dword( unsigned int val );
315 extern void put_qword( unsigned int val );
316 extern void put_pword( unsigned int val );
317 extern void align_output( unsigned int align );
318
319 /* global variables */
320
321 extern int current_line;
322 extern int UsePIC;
323 extern int nb_lib_paths;
324 extern int nb_errors;
325 extern int display_warnings;
326 extern int kill_at;
327 extern int verbose;
328 extern int save_temps;
329 extern int link_ext_symbols;
330 extern int force_pointer_size;
331 extern int unwind_tables;
332
333 extern char *input_file_name;
334 extern char *spec_file_name;
335 extern FILE *output_file;
336 extern const char *output_file_name;
337 extern char **lib_path;
338
339 extern char *as_command;
340 extern char *ld_command;
341 extern char *nm_command;
342
343 #endif  /* __WINE_BUILD_H */