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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
36 #define MAX_TMP_FILES 8
37 static const char *tmp_files[MAX_TMP_FILES];
38 static unsigned int nb_tmp_files;
40 /* atexit handler to clean tmp files */
41 static void cleanup_tmp_files(void)
44 for (i = 0; i < MAX_TMP_FILES; i++) if (tmp_files[i]) unlink( tmp_files[i] );
48 void *xmalloc (size_t size)
52 res = malloc (size ? size : 1);
55 fprintf (stderr, "Virtual memory exhausted.\n");
61 void *xrealloc (void *ptr, size_t size)
63 void *res = realloc (ptr, size);
64 if (size && res == NULL)
66 fprintf (stderr, "Virtual memory exhausted.\n");
72 char *xstrdup( const char *str )
74 char *res = strdup( str );
77 fprintf (stderr, "Virtual memory exhausted.\n");
83 char *strupper(char *s)
86 for (p = s; *p; p++) *p = toupper(*p);
90 int strendswith(const char* str, const char* end)
94 return l >= m && strcmp(str + l - m, end) == 0;
97 void fatal_error( const char *msg, ... )
100 va_start( valist, msg );
103 fprintf( stderr, "%s:", input_file_name );
105 fprintf( stderr, "%d:", current_line );
106 fputc( ' ', stderr );
108 else fprintf( stderr, "winebuild: " );
109 vfprintf( stderr, msg, valist );
114 void fatal_perror( const char *msg, ... )
117 va_start( valist, msg );
120 fprintf( stderr, "%s:", input_file_name );
122 fprintf( stderr, "%d:", current_line );
123 fputc( ' ', stderr );
125 vfprintf( stderr, msg, valist );
131 void error( const char *msg, ... )
134 va_start( valist, msg );
137 fprintf( stderr, "%s:", input_file_name );
139 fprintf( stderr, "%d:", current_line );
140 fputc( ' ', stderr );
142 vfprintf( stderr, msg, valist );
147 void warning( const char *msg, ... )
151 if (!display_warnings) return;
152 va_start( valist, msg );
155 fprintf( stderr, "%s:", input_file_name );
157 fprintf( stderr, "%d:", current_line );
158 fputc( ' ', stderr );
160 fprintf( stderr, "warning: " );
161 vfprintf( stderr, msg, valist );
165 int output( const char *format, ... )
170 va_start( valist, format );
171 ret = vfprintf( output_file, format, valist );
173 if (ret < 0) fatal_perror( "Output error" );
177 /* get a name for a temp file, automatically cleaned up on exit */
178 char *get_temp_file_name( const char *prefix, const char *suffix )
184 assert( nb_tmp_files < MAX_TMP_FILES );
185 if (!nb_tmp_files && !save_temps) atexit( cleanup_tmp_files );
187 if (!prefix || !prefix[0]) prefix = "winebuild";
188 if (!suffix) suffix = "";
189 if (!(ext = strchr( prefix, '.' ))) ext = prefix + strlen(prefix);
190 name = xmalloc( sizeof("/tmp/") + (ext - prefix) + sizeof(".XXXXXX") + strlen(suffix) );
191 strcpy( name, "/tmp/" );
192 memcpy( name + 5, prefix, ext - prefix );
193 strcpy( name + 5 + (ext - prefix), ".XXXXXX" );
194 strcat( name, suffix );
196 /* first try without the /tmp/ prefix */
197 if ((fd = mkstemps( name + 5, strlen(suffix) )) != -1)
199 else if ((fd = mkstemps( name, strlen(suffix) )) == -1)
200 fatal_error( "could not generate a temp file\n" );
203 tmp_files[nb_tmp_files++] = name;
207 /* output a standard header for generated files */
208 void output_standard_file_header(void)
211 output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name );
213 output( "/* File generated automatically; do not edit! */\n" );
214 output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
217 /* dump a byte stream into the assembly code */
218 void dump_bytes( const void *buffer, unsigned int size )
221 const unsigned char *ptr = buffer;
224 output( "\t.byte " );
225 for (i = 0; i < size - 1; i++, ptr++)
227 if ((i % 16) == 15) output( "0x%02x\n\t.byte ", *ptr );
228 else output( "0x%02x,", *ptr );
230 output( "0x%02x\n", *ptr );
234 /*******************************************************************
237 * Open a file in the given srcdir and set the input_file_name global variable.
239 FILE *open_input_file( const char *srcdir, const char *name )
242 FILE *file = fopen( name, "r" );
246 fullname = xmalloc( strlen(srcdir) + strlen(name) + 2 );
247 strcpy( fullname, srcdir );
248 strcat( fullname, "/" );
249 strcat( fullname, name );
250 file = fopen( fullname, "r" );
252 else fullname = xstrdup( name );
254 if (!file) fatal_error( "Cannot open file '%s'\n", fullname );
255 input_file_name = fullname;
261 /*******************************************************************
264 * Close the current input file (must have been opened with open_input_file).
266 void close_input_file( FILE *file )
269 free( input_file_name );
270 input_file_name = NULL;
275 /*******************************************************************
276 * remove_stdcall_decoration
278 * Remove a possible @xx suffix from a function name.
279 * Return the numerical value of the suffix, or -1 if none.
281 int remove_stdcall_decoration( char *name )
283 char *p, *end = strrchr( name, '@' );
284 if (!end || !end[1] || end == name) return -1;
285 /* make sure all the rest is digits */
286 for (p = end + 1; *p; p++) if (!isdigit(*p)) return -1;
288 return atoi( end + 1 );
292 /*******************************************************************
295 * Run a file through the assembler.
297 void assemble_file( const char *src_file, const char *obj_file )
302 if (!as_command) as_command = xstrdup("as");
303 cmd = xmalloc( strlen(as_command) + strlen(obj_file) + strlen(src_file) + 6 );
304 sprintf( cmd, "%s -o %s %s", as_command, obj_file, src_file );
305 if (verbose) fprintf( stderr, "%s\n", cmd );
307 if (err) fatal_error( "%s failed with status %d\n", as_command, err );
312 /*******************************************************************
315 * Create a new dll spec file descriptor
317 DLLSPEC *alloc_dll_spec(void)
321 spec = xmalloc( sizeof(*spec) );
322 spec->file_name = NULL;
323 spec->dll_name = NULL;
324 spec->init_func = NULL;
325 spec->type = SPEC_WIN32;
326 spec->base = MAX_ORDINALS;
328 spec->stack_size = 0;
330 spec->nb_entry_points = 0;
331 spec->alloc_entry_points = 0;
333 spec->nb_resources = 0;
334 spec->characteristics = 0;
336 spec->subsystem_major = 4;
337 spec->subsystem_minor = 0;
338 spec->entry_points = NULL;
340 spec->ordinals = NULL;
341 spec->resources = NULL;
346 /*******************************************************************
349 * Free dll spec file descriptor
351 void free_dll_spec( DLLSPEC *spec )
355 for (i = 0; i < spec->nb_entry_points; i++)
357 ORDDEF *odp = &spec->entry_points[i];
359 free( odp->export_name );
360 free( odp->link_name );
362 free( spec->file_name );
363 free( spec->dll_name );
364 free( spec->init_func );
365 free( spec->entry_points );
367 free( spec->ordinals );
368 free( spec->resources );
373 /*******************************************************************
376 * Map a string to a valid C identifier.
378 const char *make_c_identifier( const char *str )
380 static char buffer[256];
383 for (p = buffer; *str && p < buffer+sizeof(buffer)-1; p++, str++)
385 if (isalnum(*str)) *p = *str;
393 /*******************************************************************
396 * Generate an internal name for a stub entry point.
398 const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec )
400 static char buffer[256];
401 if (odp->name || odp->export_name)
404 sprintf( buffer, "__wine_stub_%s", odp->name ? odp->name : odp->export_name );
405 /* make sure name is a legal C identifier */
406 for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break;
407 if (!*p) return buffer;
409 sprintf( buffer, "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal );
414 /*****************************************************************
415 * Function: get_alignment
418 * According to the info page for gas, the .align directive behaves
419 * differently on different systems. On some architectures, the
420 * argument of a .align directive is the number of bytes to pad to, so
421 * to align on an 8-byte boundary you'd say
423 * On other systems, the argument is "the number of low-order zero bits
424 * that the location counter must have after advancement." So to
425 * align on an 8-byte boundary you'd say
428 * The reason gas is written this way is that it's trying to mimick
429 * native assemblers for the various architectures it runs on. gas
430 * provides other directives that work consistently across
431 * architectures, but of course we want to work on all arches with or
432 * without gas. Hence this function.
436 * align -- the number of bytes to align to. Must be a power of 2.
438 unsigned int get_alignment(unsigned int align)
442 assert( !(align & (align - 1)) );
449 if (target_platform != PLATFORM_APPLE) return align;
454 while ((1 << n) != align) n++;
462 /* return the page size for the target CPU */
463 unsigned int get_page_size(void)
467 case CPU_x86: return 4096;
468 case CPU_x86_64: return 4096;
469 case CPU_POWERPC: return 4096;
470 case CPU_SPARC: return 8192;
471 case CPU_ALPHA: return 8192;
478 /* return the size of a pointer on the target CPU */
479 unsigned int get_ptr_size(void)
496 /* return the assembly name for a C symbol */
497 const char *asm_name( const char *sym )
499 static char buffer[256];
501 switch (target_platform)
504 case PLATFORM_WINDOWS:
506 strcpy( buffer + 1, sym );
513 /* return an assembly function declaration for a C function name */
514 const char *func_declaration( const char *func )
516 static char buffer[256];
518 switch (target_platform)
522 case PLATFORM_WINDOWS:
523 sprintf( buffer, ".def _%s; .scl 2; .type 32; .endef", func );
526 sprintf( buffer, ".type %s,@function", func );
532 /* output a size declaration for an assembly function */
533 void output_function_size( const char *name )
535 switch (target_platform)
538 case PLATFORM_WINDOWS:
541 output( "\t.size %s, .-%s\n", name, name );
546 /* output the GNU note for non-exec stack */
547 void output_gnu_stack_note(void)
549 switch (target_platform)
551 case PLATFORM_WINDOWS:
555 output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
560 /* return a global symbol declaration for an assembly symbol */
561 const char *asm_globl( const char *func )
563 static char buffer[256];
565 switch (target_platform)
568 sprintf( buffer, "\t.globl _%s\n\t.private_extern _%s\n_%s:", func, func, func );
570 case PLATFORM_WINDOWS:
571 sprintf( buffer, "\t.globl _%s\n_%s:", func, func );
574 sprintf( buffer, "\t.globl %s\n\t.hidden %s\n%s:", func, func, func );
579 const char *get_asm_ptr_keyword(void)
581 switch(get_ptr_size())
583 case 4: return ".long";
584 case 8: return ".quad";
590 const char *get_asm_string_keyword(void)
592 switch (target_platform)
601 const char *get_asm_short_keyword(void)
603 switch (target_platform)
605 default: return ".short";
609 const char *get_asm_rodata_section(void)
611 switch (target_platform)
613 case PLATFORM_APPLE: return ".const";
614 default: return ".section .rodata";
618 const char *get_asm_string_section(void)
620 switch (target_platform)
622 case PLATFORM_APPLE: return ".cstring";
623 default: return ".section .rodata";