Make stack check work if stack is a small value.
[wine/wine64.git] / tools / winebuild / utils.c
blob10b92bb2e2f419e8d7985f5d77bd92ab251150b8
1 /*
2 * Small utility functions for winebuild
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include "build.h"
33 void *xmalloc (size_t size)
35 void *res;
37 res = malloc (size ? size : 1);
38 if (res == NULL)
40 fprintf (stderr, "Virtual memory exhausted.\n");
41 exit (1);
43 return res;
46 void *xrealloc (void *ptr, size_t size)
48 void *res = realloc (ptr, size);
49 if (size && res == NULL)
51 fprintf (stderr, "Virtual memory exhausted.\n");
52 exit (1);
54 return res;
57 char *xstrdup( const char *str )
59 char *res = strdup( str );
60 if (!res)
62 fprintf (stderr, "Virtual memory exhausted.\n");
63 exit (1);
65 return res;
68 char *strupper(char *s)
70 char *p;
71 for (p = s; *p; p++) *p = toupper(*p);
72 return s;
75 int strendswith(const char* str, const char* end)
77 int l = strlen(str);
78 int m = strlen(end);
79 return l >= m && strcmp(str + l - m, end) == 0;
82 void fatal_error( const char *msg, ... )
84 va_list valist;
85 va_start( valist, msg );
86 if (input_file_name)
88 fprintf( stderr, "%s:", input_file_name );
89 if (current_line)
90 fprintf( stderr, "%d:", current_line );
91 fputc( ' ', stderr );
93 else fprintf( stderr, "winebuild: " );
94 vfprintf( stderr, msg, valist );
95 va_end( valist );
96 exit(1);
99 void fatal_perror( const char *msg, ... )
101 va_list valist;
102 va_start( valist, msg );
103 if (input_file_name)
105 fprintf( stderr, "%s:", input_file_name );
106 if (current_line)
107 fprintf( stderr, "%d:", current_line );
108 fputc( ' ', stderr );
110 vfprintf( stderr, msg, valist );
111 perror( " " );
112 va_end( valist );
113 exit(1);
116 void error( const char *msg, ... )
118 va_list valist;
119 va_start( valist, msg );
120 if (input_file_name)
122 fprintf( stderr, "%s:", input_file_name );
123 if (current_line)
124 fprintf( stderr, "%d:", current_line );
125 fputc( ' ', stderr );
127 vfprintf( stderr, msg, valist );
128 va_end( valist );
129 nb_errors++;
132 void warning( const char *msg, ... )
134 va_list valist;
136 if (!display_warnings) return;
137 va_start( valist, msg );
138 if (input_file_name)
140 fprintf( stderr, "%s:", input_file_name );
141 if (current_line)
142 fprintf( stderr, "%d:", current_line );
143 fputc( ' ', stderr );
145 fprintf( stderr, "warning: " );
146 vfprintf( stderr, msg, valist );
147 va_end( valist );
150 /* output a standard header for generated files */
151 void output_standard_file_header( FILE *outfile )
153 if (spec_file_name)
154 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n",
155 spec_file_name );
156 else
157 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
158 fprintf( outfile,
159 "/* This file can be copied, modified and distributed without restriction. */\n\n" );
162 /* dump a byte stream into the assembly code */
163 void dump_bytes( FILE *outfile, const unsigned char *data, int len,
164 const char *label, int constant )
166 int i;
168 fprintf( outfile, "\nstatic %sunsigned char %s[%d] = {",
169 constant ? "const " : "", label, len );
170 for (i = 0; i < len; i++)
172 if (!(i & 7)) fprintf( outfile, "\n " );
173 fprintf( outfile, "0x%02x", *data++ );
174 if (i < len - 1) fprintf( outfile, "," );
176 fprintf( outfile, "\n};\n" );
180 /*******************************************************************
181 * open_input_file
183 * Open a file in the given srcdir and set the input_file_name global variable.
185 FILE *open_input_file( const char *srcdir, const char *name )
187 char *fullname;
188 FILE *file = fopen( name, "r" );
190 if (!file && srcdir)
192 fullname = xmalloc( strlen(srcdir) + strlen(name) + 2 );
193 strcpy( fullname, srcdir );
194 strcat( fullname, "/" );
195 strcat( fullname, name );
196 file = fopen( fullname, "r" );
198 else fullname = xstrdup( name );
200 if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
201 input_file_name = fullname;
202 current_line = 1;
203 return file;
207 /*******************************************************************
208 * close_input_file
210 * Close the current input file (must have been opened with open_input_file).
212 void close_input_file( FILE *file )
214 fclose( file );
215 free( input_file_name );
216 input_file_name = NULL;
217 current_line = 0;
221 /*******************************************************************
222 * remove_stdcall_decoration
224 * Remove a possible @xx suffix from a function name.
225 * Return the numerical value of the suffix, or -1 if none.
227 int remove_stdcall_decoration( char *name )
229 char *p, *end = strrchr( name, '@' );
230 if (!end || !end[1] || end == name) return -1;
231 /* make sure all the rest is digits */
232 for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
233 *end = 0;
234 return atoi( end + 1 );
238 /*******************************************************************
239 * alloc_dll_spec
241 * Create a new dll spec file descriptor
243 DLLSPEC *alloc_dll_spec(void)
245 DLLSPEC *spec;
247 spec = xmalloc( sizeof(*spec) );
248 spec->file_name = NULL;
249 spec->dll_name = NULL;
250 spec->owner_name = NULL;
251 spec->init_func = NULL;
252 spec->type = SPEC_WIN32;
253 spec->base = MAX_ORDINALS;
254 spec->limit = 0;
255 spec->stack_size = 0;
256 spec->heap_size = 0;
257 spec->nb_entry_points = 0;
258 spec->alloc_entry_points = 0;
259 spec->nb_names = 0;
260 spec->nb_resources = 0;
261 spec->characteristics = 0;
262 spec->subsystem = 0;
263 spec->subsystem_major = 4;
264 spec->subsystem_minor = 0;
265 spec->entry_points = NULL;
266 spec->names = NULL;
267 spec->ordinals = NULL;
268 spec->resources = NULL;
269 return spec;
273 /*******************************************************************
274 * free_dll_spec
276 * Free dll spec file descriptor
278 void free_dll_spec( DLLSPEC *spec )
280 int i;
282 for (i = 0; i < spec->nb_entry_points; i++)
284 ORDDEF *odp = &spec->entry_points[i];
285 free( odp->name );
286 free( odp->export_name );
287 free( odp->link_name );
289 free( spec->file_name );
290 free( spec->dll_name );
291 free( spec->owner_name );
292 free( spec->init_func );
293 free( spec->entry_points );
294 free( spec->names );
295 free( spec->ordinals );
296 free( spec->resources );
297 free( spec );
301 /*******************************************************************
302 * make_c_identifier
304 * Map a string to a valid C identifier.
306 const char *make_c_identifier( const char *str )
308 static char buffer[256];
309 char *p;
311 for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
313 if (isalnum(*str)) *p = *str;
314 else *p = '_';
316 *p = 0;
317 return buffer;
321 /*****************************************************************
322 * Function: get_alignment
324 * Description:
325 * According to the info page for gas, the .align directive behaves
326 * differently on different systems. On some architectures, the
327 * argument of a .align directive is the number of bytes to pad to, so
328 * to align on an 8-byte boundary you'd say
329 * .align 8
330 * On other systems, the argument is "the number of low-order zero bits
331 * that the location counter must have after advancement." So to
332 * align on an 8-byte boundary you'd say
333 * .align 3
335 * The reason gas is written this way is that it's trying to mimick
336 * native assemblers for the various architectures it runs on. gas
337 * provides other directives that work consistantly across
338 * architectures, but of course we want to work on all arches with or
339 * without gas. Hence this function.
342 * Parameters:
343 * align -- the number of bytes to align to. Must be a power of 2.
345 unsigned int get_alignment(unsigned int align)
347 unsigned int n;
349 assert( !(align & (align - 1)) );
351 switch(target_cpu)
353 case CPU_x86:
354 case CPU_SPARC:
355 if (target_platform != PLATFORM_APPLE) return align;
356 /* fall through */
357 case CPU_POWERPC:
358 case CPU_ALPHA:
359 n = 0;
360 while ((1 << n) != align) n++;
361 return n;
363 /* unreached */
364 assert(0);
365 return 0;
368 /* return the page size for the target CPU */
369 unsigned int get_page_size(void)
371 switch(target_cpu)
373 case CPU_x86: return 4096;
374 case CPU_POWERPC: return 4096;
375 case CPU_SPARC: return 8192;
376 case CPU_ALPHA: return 8192;
378 /* unreached */
379 assert(0);
380 return 0;
383 /* return the assembly name for a C symbol */
384 const char *asm_name( const char *sym )
386 static char buffer[256];
388 switch (target_platform)
390 case PLATFORM_APPLE:
391 case PLATFORM_WINDOWS:
392 buffer[0] = '_';
393 strcpy( buffer + 1, sym );
394 return buffer;
395 default:
396 return sym;
400 /* return an assembly function declaration for a C function name */
401 const char *func_declaration( const char *func )
403 static char buffer[256];
405 switch (target_platform)
407 case PLATFORM_APPLE:
408 return "";
409 case PLATFORM_WINDOWS:
410 sprintf( buffer, ".def _%s; .scl 2; .type 32; .endef", func );
411 break;
412 case PLATFORM_SVR4:
413 sprintf( buffer, ".type %s,2", func );
414 break;
415 default:
416 sprintf( buffer, ".type %s,@function", func );
417 break;
419 return buffer;
422 /* return a size declaration for an assembly function */
423 const char *func_size( const char *func )
425 static char buffer[256];
427 switch (target_platform)
429 case PLATFORM_APPLE:
430 case PLATFORM_WINDOWS:
431 return "";
432 default:
433 sprintf( buffer, ".size %s, .-%s", func, func );
434 return buffer;
438 const char *get_asm_string_keyword(void)
440 switch (target_platform)
442 case PLATFORM_APPLE:
443 case PLATFORM_SVR4:
444 return ".asciz";
445 default:
446 return ".string";
450 const char *get_asm_short_keyword(void)
452 switch (target_platform)
454 case PLATFORM_SVR4: return ".half";
455 default: return ".short";