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"
38 #define MAX_TMP_FILES 8
39 static const char *tmp_files
[MAX_TMP_FILES
];
40 static unsigned int nb_tmp_files
;
42 /* atexit handler to clean tmp files */
43 static void cleanup_tmp_files(void)
46 for (i
= 0; i
< MAX_TMP_FILES
; i
++) if (tmp_files
[i
]) unlink( tmp_files
[i
] );
50 void *xmalloc (size_t size
)
54 res
= malloc (size
? size
: 1);
57 fprintf (stderr
, "Virtual memory exhausted.\n");
63 void *xrealloc (void *ptr
, size_t size
)
65 void *res
= realloc (ptr
, size
);
66 if (size
&& res
== NULL
)
68 fprintf (stderr
, "Virtual memory exhausted.\n");
74 char *xstrdup( const char *str
)
76 char *res
= strdup( str
);
79 fprintf (stderr
, "Virtual memory exhausted.\n");
85 char *strupper(char *s
)
88 for (p
= s
; *p
; p
++) *p
= toupper(*p
);
92 int strendswith(const char* str
, const char* end
)
96 return l
>= m
&& strcmp(str
+ l
- m
, end
) == 0;
99 void fatal_error( const char *msg
, ... )
102 va_start( valist
, msg
);
105 fprintf( stderr
, "%s:", input_file_name
);
107 fprintf( stderr
, "%d:", current_line
);
108 fputc( ' ', stderr
);
110 else fprintf( stderr
, "winebuild: " );
111 vfprintf( stderr
, msg
, valist
);
116 void fatal_perror( const char *msg
, ... )
119 va_start( valist
, msg
);
122 fprintf( stderr
, "%s:", input_file_name
);
124 fprintf( stderr
, "%d:", current_line
);
125 fputc( ' ', stderr
);
127 vfprintf( stderr
, msg
, valist
);
133 void error( const char *msg
, ... )
136 va_start( valist
, msg
);
139 fprintf( stderr
, "%s:", input_file_name
);
141 fprintf( stderr
, "%d:", current_line
);
142 fputc( ' ', stderr
);
144 vfprintf( stderr
, msg
, valist
);
149 void warning( const char *msg
, ... )
153 if (!display_warnings
) return;
154 va_start( valist
, msg
);
157 fprintf( stderr
, "%s:", input_file_name
);
159 fprintf( stderr
, "%d:", current_line
);
160 fputc( ' ', stderr
);
162 fprintf( stderr
, "warning: " );
163 vfprintf( stderr
, msg
, valist
);
167 int output( const char *format
, ... )
172 va_start( valist
, format
);
173 ret
= vfprintf( output_file
, format
, valist
);
175 if (ret
< 0) fatal_perror( "Output error" );
179 /* get a name for a temp file, automatically cleaned up on exit */
180 char *get_temp_file_name( const char *prefix
, const char *suffix
)
186 assert( nb_tmp_files
< MAX_TMP_FILES
);
187 if (!nb_tmp_files
&& !save_temps
) atexit( cleanup_tmp_files
);
189 if (!prefix
|| !prefix
[0]) prefix
= "winebuild";
190 if (!suffix
) suffix
= "";
191 if (!(ext
= strchr( prefix
, '.' ))) ext
= prefix
+ strlen(prefix
);
192 name
= xmalloc( sizeof("/tmp/") + (ext
- prefix
) + sizeof(".XXXXXX") + strlen(suffix
) );
193 strcpy( name
, "/tmp/" );
194 memcpy( name
+ 5, prefix
, ext
- prefix
);
195 strcpy( name
+ 5 + (ext
- prefix
), ".XXXXXX" );
196 strcat( name
, suffix
);
198 /* first try without the /tmp/ prefix */
199 if ((fd
= mkstemps( name
+ 5, strlen(suffix
) )) != -1)
201 else if ((fd
= mkstemps( name
, strlen(suffix
) )) == -1)
202 fatal_error( "could not generate a temp file\n" );
205 tmp_files
[nb_tmp_files
++] = name
;
209 /* output a standard header for generated files */
210 void output_standard_file_header(void)
213 output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name
);
215 output( "/* File generated automatically; do not edit! */\n" );
216 output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
219 /* dump a byte stream into the assembly code */
220 void dump_bytes( const void *buffer
, unsigned int size
)
223 const unsigned char *ptr
= buffer
;
226 output( "\t.byte " );
227 for (i
= 0; i
< size
- 1; i
++, ptr
++)
229 if ((i
% 16) == 15) output( "0x%02x\n\t.byte ", *ptr
);
230 else output( "0x%02x,", *ptr
);
232 output( "0x%02x\n", *ptr
);
236 /*******************************************************************
239 * Open a file in the given srcdir and set the input_file_name global variable.
241 FILE *open_input_file( const char *srcdir
, const char *name
)
244 FILE *file
= fopen( name
, "r" );
248 fullname
= xmalloc( strlen(srcdir
) + strlen(name
) + 2 );
249 strcpy( fullname
, srcdir
);
250 strcat( fullname
, "/" );
251 strcat( fullname
, name
);
252 file
= fopen( fullname
, "r" );
254 else fullname
= xstrdup( name
);
256 if (!file
) fatal_error( "Cannot open file '%s'\n", fullname
);
257 input_file_name
= fullname
;
263 /*******************************************************************
266 * Close the current input file (must have been opened with open_input_file).
268 void close_input_file( FILE *file
)
271 free( input_file_name
);
272 input_file_name
= NULL
;
277 /*******************************************************************
278 * remove_stdcall_decoration
280 * Remove a possible @xx suffix from a function name.
281 * Return the numerical value of the suffix, or -1 if none.
283 int remove_stdcall_decoration( char *name
)
285 char *p
, *end
= strrchr( name
, '@' );
286 if (!end
|| !end
[1] || end
== name
) return -1;
287 /* make sure all the rest is digits */
288 for (p
= end
+ 1; *p
; p
++) if (!isdigit(*p
)) return -1;
290 return atoi( end
+ 1 );
294 /*******************************************************************
297 * Run a file through the assembler.
299 void assemble_file( const char *src_file
, const char *obj_file
)
304 if (!as_command
) as_command
= xstrdup("as");
305 cmd
= xmalloc( strlen(as_command
) + strlen(obj_file
) + strlen(src_file
) + 6 );
306 sprintf( cmd
, "%s -o %s %s", as_command
, obj_file
, src_file
);
307 if (verbose
) fprintf( stderr
, "%s\n", cmd
);
309 if (err
) fatal_error( "%s failed with status %d\n", as_command
, err
);
314 /*******************************************************************
317 * Create a new dll spec file descriptor
319 DLLSPEC
*alloc_dll_spec(void)
323 spec
= xmalloc( sizeof(*spec
) );
324 spec
->file_name
= NULL
;
325 spec
->dll_name
= NULL
;
326 spec
->init_func
= NULL
;
327 spec
->type
= SPEC_WIN32
;
328 spec
->base
= MAX_ORDINALS
;
330 spec
->stack_size
= 0;
332 spec
->nb_entry_points
= 0;
333 spec
->alloc_entry_points
= 0;
335 spec
->nb_resources
= 0;
336 spec
->characteristics
= IMAGE_FILE_EXECUTABLE_IMAGE
;
337 if (get_ptr_size() > 4)
338 spec
->characteristics
|= IMAGE_FILE_LARGE_ADDRESS_AWARE
;
339 spec
->dll_characteristics
= IMAGE_DLLCHARACTERISTICS_NX_COMPAT
;
341 spec
->subsystem_major
= 4;
342 spec
->subsystem_minor
= 0;
343 spec
->entry_points
= NULL
;
345 spec
->ordinals
= NULL
;
346 spec
->resources
= NULL
;
351 /*******************************************************************
354 * Free dll spec file descriptor
356 void free_dll_spec( DLLSPEC
*spec
)
360 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
362 ORDDEF
*odp
= &spec
->entry_points
[i
];
364 free( odp
->export_name
);
365 free( odp
->link_name
);
367 free( spec
->file_name
);
368 free( spec
->dll_name
);
369 free( spec
->init_func
);
370 free( spec
->entry_points
);
372 free( spec
->ordinals
);
373 free( spec
->resources
);
378 /*******************************************************************
381 * Map a string to a valid C identifier.
383 const char *make_c_identifier( const char *str
)
385 static char buffer
[256];
388 for (p
= buffer
; *str
&& p
< buffer
+sizeof(buffer
)-1; p
++, str
++)
390 if (isalnum(*str
)) *p
= *str
;
398 /*******************************************************************
401 * Generate an internal name for a stub entry point.
403 const char *get_stub_name( const ORDDEF
*odp
, const DLLSPEC
*spec
)
405 static char buffer
[256];
406 if (odp
->name
|| odp
->export_name
)
409 sprintf( buffer
, "__wine_stub_%s", odp
->name
? odp
->name
: odp
->export_name
);
410 /* make sure name is a legal C identifier */
411 for (p
= buffer
; *p
; p
++) if (!isalnum(*p
) && *p
!= '_') break;
412 if (!*p
) return buffer
;
414 sprintf( buffer
, "__wine_stub_%s_%d", make_c_identifier(spec
->file_name
), odp
->ordinal
);
419 /*****************************************************************
420 * Function: get_alignment
423 * According to the info page for gas, the .align directive behaves
424 * differently on different systems. On some architectures, the
425 * argument of a .align directive is the number of bytes to pad to, so
426 * to align on an 8-byte boundary you'd say
428 * On other systems, the argument is "the number of low-order zero bits
429 * that the location counter must have after advancement." So to
430 * align on an 8-byte boundary you'd say
433 * The reason gas is written this way is that it's trying to mimick
434 * native assemblers for the various architectures it runs on. gas
435 * provides other directives that work consistently across
436 * architectures, but of course we want to work on all arches with or
437 * without gas. Hence this function.
441 * align -- the number of bytes to align to. Must be a power of 2.
443 unsigned int get_alignment(unsigned int align
)
447 assert( !(align
& (align
- 1)) );
454 if (target_platform
!= PLATFORM_APPLE
) return align
;
459 while ((1u << n
) != align
) n
++;
467 /* return the page size for the target CPU */
468 unsigned int get_page_size(void)
472 case CPU_x86
: return 4096;
473 case CPU_x86_64
: return 4096;
474 case CPU_POWERPC
: return 4096;
475 case CPU_SPARC
: return 8192;
476 case CPU_ALPHA
: return 8192;
483 /* return the size of a pointer on the target CPU */
484 unsigned int get_ptr_size(void)
501 /* return the assembly name for a C symbol */
502 const char *asm_name( const char *sym
)
504 static char buffer
[256];
506 switch (target_platform
)
509 case PLATFORM_WINDOWS
:
511 strcpy( buffer
+ 1, sym
);
518 /* return an assembly function declaration for a C function name */
519 const char *func_declaration( const char *func
)
521 static char buffer
[256];
523 switch (target_platform
)
527 case PLATFORM_WINDOWS
:
528 sprintf( buffer
, ".def _%s; .scl 2; .type 32; .endef", func
);
531 sprintf( buffer
, ".type %s,@function", func
);
537 /* output a size declaration for an assembly function */
538 void output_function_size( const char *name
)
540 switch (target_platform
)
543 case PLATFORM_WINDOWS
:
546 output( "\t.size %s, .-%s\n", name
, name
);
551 /* output the GNU note for non-exec stack */
552 void output_gnu_stack_note(void)
554 switch (target_platform
)
556 case PLATFORM_WINDOWS
:
560 output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
565 /* return a global symbol declaration for an assembly symbol */
566 const char *asm_globl( const char *func
)
568 static char buffer
[256];
570 switch (target_platform
)
573 sprintf( buffer
, "\t.globl _%s\n\t.private_extern _%s\n_%s:", func
, func
, func
);
575 case PLATFORM_WINDOWS
:
576 sprintf( buffer
, "\t.globl _%s\n_%s:", func
, func
);
579 sprintf( buffer
, "\t.globl %s\n\t.hidden %s\n%s:", func
, func
, func
);
584 const char *get_asm_ptr_keyword(void)
586 switch(get_ptr_size())
588 case 4: return ".long";
589 case 8: return ".quad";
595 const char *get_asm_string_keyword(void)
597 switch (target_platform
)
606 const char *get_asm_short_keyword(void)
608 switch (target_platform
)
610 default: return ".short";
614 const char *get_asm_rodata_section(void)
616 switch (target_platform
)
618 case PLATFORM_APPLE
: return ".const";
619 default: return ".section .rodata";
623 const char *get_asm_string_section(void)
625 switch (target_platform
)
627 case PLATFORM_APPLE
: return ".cstring";
628 default: return ".section .rodata";