dd887414cf845c8068acb63f859616956c09bb5b
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
39 #define MAX_TMP_FILES 8
40 static const char *tmp_files
[MAX_TMP_FILES
];
41 static unsigned int nb_tmp_files
;
43 /* atexit handler to clean tmp files */
44 static void cleanup_tmp_files(void)
47 for (i
= 0; i
< MAX_TMP_FILES
; i
++) if (tmp_files
[i
]) unlink( tmp_files
[i
] );
51 void *xmalloc (size_t size
)
55 res
= malloc (size
? size
: 1);
58 fprintf (stderr
, "Virtual memory exhausted.\n");
64 void *xrealloc (void *ptr
, size_t size
)
66 void *res
= realloc (ptr
, size
);
67 if (size
&& res
== NULL
)
69 fprintf (stderr
, "Virtual memory exhausted.\n");
75 char *xstrdup( const char *str
)
77 char *res
= strdup( str
);
80 fprintf (stderr
, "Virtual memory exhausted.\n");
86 char *strupper(char *s
)
89 for (p
= s
; *p
; p
++) *p
= toupper(*p
);
93 int strendswith(const char* str
, const char* end
)
97 return l
>= m
&& strcmp(str
+ l
- m
, end
) == 0;
100 void fatal_error( const char *msg
, ... )
103 va_start( valist
, msg
);
106 fprintf( stderr
, "%s:", input_file_name
);
108 fprintf( stderr
, "%d:", current_line
);
109 fputc( ' ', stderr
);
111 else fprintf( stderr
, "winebuild: " );
112 vfprintf( stderr
, msg
, valist
);
117 void fatal_perror( const char *msg
, ... )
120 va_start( valist
, msg
);
123 fprintf( stderr
, "%s:", input_file_name
);
125 fprintf( stderr
, "%d:", current_line
);
126 fputc( ' ', stderr
);
128 vfprintf( stderr
, msg
, valist
);
134 void error( const char *msg
, ... )
137 va_start( valist
, msg
);
140 fprintf( stderr
, "%s:", input_file_name
);
142 fprintf( stderr
, "%d:", current_line
);
143 fputc( ' ', stderr
);
145 vfprintf( stderr
, msg
, valist
);
150 void warning( const char *msg
, ... )
154 if (!display_warnings
) return;
155 va_start( valist
, msg
);
158 fprintf( stderr
, "%s:", input_file_name
);
160 fprintf( stderr
, "%d:", current_line
);
161 fputc( ' ', stderr
);
163 fprintf( stderr
, "warning: " );
164 vfprintf( stderr
, msg
, valist
);
168 /* get a name for a temp file, automatically cleaned up on exit */
169 char *get_temp_file_name( const char *prefix
, const char *suffix
)
175 assert( nb_tmp_files
< MAX_TMP_FILES
);
176 if (!nb_tmp_files
&& !save_temps
) atexit( cleanup_tmp_files
);
178 if (!prefix
|| !prefix
[0]) prefix
= "winebuild";
179 if (!suffix
) suffix
= "";
180 if (!(ext
= strchr( prefix
, '.' ))) ext
= prefix
+ strlen(prefix
);
181 name
= xmalloc( sizeof("/tmp/") + (ext
- prefix
) + sizeof(".XXXXXX") + strlen(suffix
) );
182 strcpy( name
, "/tmp/" );
183 memcpy( name
+ 5, prefix
, ext
- prefix
);
184 strcpy( name
+ 5 + (ext
- prefix
), ".XXXXXX" );
185 strcat( name
, suffix
);
187 /* first try without the /tmp/ prefix */
188 if ((fd
= mkstemps( name
+ 5, strlen(suffix
) )) != -1)
190 else if ((fd
= mkstemps( name
, strlen(suffix
) )) == -1)
191 fatal_error( "could not generate a temp file\n" );
194 tmp_files
[nb_tmp_files
++] = name
;
198 /* output a standard header for generated files */
199 void output_standard_file_header( FILE *outfile
)
202 fprintf( outfile
, "/* File generated automatically from %s; do not edit! */\n",
205 fprintf( outfile
, "/* File generated automatically; do not edit! */\n" );
207 "/* This file can be copied, modified and distributed without restriction. */\n\n" );
210 /* dump a byte stream into the assembly code */
211 void dump_bytes( FILE *outfile
, const void *buffer
, unsigned int size
)
214 const unsigned char *ptr
= buffer
;
217 fprintf( outfile
, "\t.byte " );
218 for (i
= 0; i
< size
- 1; i
++, ptr
++)
220 if ((i
% 16) == 15) fprintf( outfile
, "0x%02x\n\t.byte ", *ptr
);
221 else fprintf( outfile
, "0x%02x,", *ptr
);
223 fprintf( outfile
, "0x%02x\n", *ptr
);
227 /*******************************************************************
230 * Open a file in the given srcdir and set the input_file_name global variable.
232 FILE *open_input_file( const char *srcdir
, const char *name
)
235 FILE *file
= fopen( name
, "r" );
239 fullname
= xmalloc( strlen(srcdir
) + strlen(name
) + 2 );
240 strcpy( fullname
, srcdir
);
241 strcat( fullname
, "/" );
242 strcat( fullname
, name
);
243 file
= fopen( fullname
, "r" );
245 else fullname
= xstrdup( name
);
247 if (!file
) fatal_error( "Cannot open file '%s'\n", fullname
);
248 input_file_name
= fullname
;
254 /*******************************************************************
257 * Close the current input file (must have been opened with open_input_file).
259 void close_input_file( FILE *file
)
262 free( input_file_name
);
263 input_file_name
= NULL
;
268 /*******************************************************************
269 * remove_stdcall_decoration
271 * Remove a possible @xx suffix from a function name.
272 * Return the numerical value of the suffix, or -1 if none.
274 int remove_stdcall_decoration( char *name
)
276 char *p
, *end
= strrchr( name
, '@' );
277 if (!end
|| !end
[1] || end
== name
) return -1;
278 /* make sure all the rest is digits */
279 for (p
= end
+ 1; *p
; p
++) if (!isdigit(*p
)) return -1;
281 return atoi( end
+ 1 );
285 /*******************************************************************
288 * Run a file through the assembler.
290 void assemble_file( const char *src_file
, const char *obj_file
)
295 if (!as_command
) as_command
= xstrdup("as");
296 cmd
= xmalloc( strlen(as_command
) + strlen(obj_file
) + strlen(src_file
) + 6 );
297 sprintf( cmd
, "%s -o %s %s", as_command
, obj_file
, src_file
);
298 if (verbose
) fprintf( stderr
, "%s\n", cmd
);
300 if (err
) fatal_error( "%s failed with status %d\n", as_command
, err
);
305 /*******************************************************************
308 * Create a new dll spec file descriptor
310 DLLSPEC
*alloc_dll_spec(void)
314 spec
= xmalloc( sizeof(*spec
) );
315 spec
->file_name
= NULL
;
316 spec
->dll_name
= NULL
;
317 spec
->init_func
= NULL
;
318 spec
->type
= SPEC_WIN32
;
319 spec
->base
= MAX_ORDINALS
;
321 spec
->stack_size
= 0;
323 spec
->nb_entry_points
= 0;
324 spec
->alloc_entry_points
= 0;
326 spec
->nb_resources
= 0;
327 spec
->characteristics
= 0;
329 spec
->subsystem_major
= 4;
330 spec
->subsystem_minor
= 0;
331 spec
->entry_points
= NULL
;
333 spec
->ordinals
= NULL
;
334 spec
->resources
= NULL
;
339 /*******************************************************************
342 * Free dll spec file descriptor
344 void free_dll_spec( DLLSPEC
*spec
)
348 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
350 ORDDEF
*odp
= &spec
->entry_points
[i
];
352 free( odp
->export_name
);
353 free( odp
->link_name
);
355 free( spec
->file_name
);
356 free( spec
->dll_name
);
357 free( spec
->init_func
);
358 free( spec
->entry_points
);
360 free( spec
->ordinals
);
361 free( spec
->resources
);
366 /*******************************************************************
369 * Map a string to a valid C identifier.
371 const char *make_c_identifier( const char *str
)
373 static char buffer
[256];
376 for (p
= buffer
; *str
&& p
< buffer
+sizeof(buffer
)-1; p
++, str
++)
378 if (isalnum(*str
)) *p
= *str
;
386 /*******************************************************************
389 * Generate an internal name for a stub entry point.
391 const char *get_stub_name( const ORDDEF
*odp
, const DLLSPEC
*spec
)
393 static char buffer
[256];
394 if (odp
->name
|| odp
->export_name
)
397 sprintf( buffer
, "__wine_stub_%s", odp
->name
? odp
->name
: odp
->export_name
);
398 /* make sure name is a legal C identifier */
399 for (p
= buffer
; *p
; p
++) if (!isalnum(*p
) && *p
!= '_') break;
400 if (!*p
) return buffer
;
402 sprintf( buffer
, "__wine_stub_%s_%d", make_c_identifier(spec
->file_name
), odp
->ordinal
);
407 /*****************************************************************
408 * Function: get_alignment
411 * According to the info page for gas, the .align directive behaves
412 * differently on different systems. On some architectures, the
413 * argument of a .align directive is the number of bytes to pad to, so
414 * to align on an 8-byte boundary you'd say
416 * On other systems, the argument is "the number of low-order zero bits
417 * that the location counter must have after advancement." So to
418 * align on an 8-byte boundary you'd say
421 * The reason gas is written this way is that it's trying to mimick
422 * native assemblers for the various architectures it runs on. gas
423 * provides other directives that work consistantly across
424 * architectures, but of course we want to work on all arches with or
425 * without gas. Hence this function.
429 * align -- the number of bytes to align to. Must be a power of 2.
431 unsigned int get_alignment(unsigned int align
)
435 assert( !(align
& (align
- 1)) );
442 if (target_platform
!= PLATFORM_APPLE
) return align
;
447 while ((1 << n
) != align
) n
++;
455 /* return the page size for the target CPU */
456 unsigned int get_page_size(void)
460 case CPU_x86
: return 4096;
461 case CPU_x86_64
: return 4096;
462 case CPU_POWERPC
: return 4096;
463 case CPU_SPARC
: return 8192;
464 case CPU_ALPHA
: return 8192;
471 /* return the size of a pointer on the target CPU */
472 unsigned int get_ptr_size(void)
489 /* return the assembly name for a C symbol */
490 const char *asm_name( const char *sym
)
492 static char buffer
[256];
494 switch (target_platform
)
497 case PLATFORM_WINDOWS
:
499 strcpy( buffer
+ 1, sym
);
506 /* return an assembly function declaration for a C function name */
507 const char *func_declaration( const char *func
)
509 static char buffer
[256];
511 switch (target_platform
)
515 case PLATFORM_WINDOWS
:
516 sprintf( buffer
, ".def _%s; .scl 2; .type 32; .endef", func
);
519 sprintf( buffer
, ".type %s,@function", func
);
525 /* output a size declaration for an assembly function */
526 void output_function_size( FILE *outfile
, const char *name
)
528 switch (target_platform
)
531 case PLATFORM_WINDOWS
:
534 fprintf( outfile
, "\t.size %s, .-%s\n", name
, name
);
539 /* return a global symbol declaration for an assembly symbol */
540 const char *asm_globl( const char *func
)
542 static char buffer
[256];
544 switch (target_platform
)
547 sprintf( buffer
, "\t.globl _%s\n\t.private_extern _%s\n_%s:", func
, func
, func
);
549 case PLATFORM_WINDOWS
:
550 sprintf( buffer
, "\t.globl _%s\n_%s:", func
, func
);
553 sprintf( buffer
, "\t.globl %s\n\t.hidden %s\n%s:", func
, func
, func
);
558 const char *get_asm_ptr_keyword(void)
560 switch(get_ptr_size())
562 case 4: return ".long";
563 case 8: return ".quad";
569 const char *get_asm_string_keyword(void)
571 switch (target_platform
)
580 const char *get_asm_short_keyword(void)
582 switch (target_platform
)
584 default: return ".short";
588 const char *get_asm_string_section(void)
590 switch (target_platform
)
592 case PLATFORM_APPLE
: return ".cstring";
593 default: return ".section .rodata";