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
;
51 { "x86_64", CPU_x86_64
},
52 { "sparc", CPU_SPARC
},
53 { "alpha", CPU_ALPHA
},
54 { "powerpc", CPU_POWERPC
}
57 /* atexit handler to clean tmp files */
58 static void cleanup_tmp_files(void)
61 for (i
= 0; i
< MAX_TMP_FILES
; i
++) if (tmp_files
[i
]) unlink( tmp_files
[i
] );
65 void *xmalloc (size_t size
)
69 res
= malloc (size
? size
: 1);
72 fprintf (stderr
, "Virtual memory exhausted.\n");
78 void *xrealloc (void *ptr
, size_t size
)
80 void *res
= realloc (ptr
, size
);
81 if (size
&& res
== NULL
)
83 fprintf (stderr
, "Virtual memory exhausted.\n");
89 char *xstrdup( const char *str
)
91 char *res
= strdup( str
);
94 fprintf (stderr
, "Virtual memory exhausted.\n");
100 char *strupper(char *s
)
103 for (p
= s
; *p
; p
++) *p
= toupper(*p
);
107 int strendswith(const char* str
, const char* end
)
111 return l
>= m
&& strcmp(str
+ l
- m
, end
) == 0;
114 void fatal_error( 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 else fprintf( stderr
, "winebuild: " );
126 vfprintf( stderr
, msg
, valist
);
131 void fatal_perror( 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
);
148 void error( const char *msg
, ... )
151 va_start( valist
, msg
);
154 fprintf( stderr
, "%s:", input_file_name
);
156 fprintf( stderr
, "%d:", current_line
);
157 fputc( ' ', stderr
);
159 vfprintf( stderr
, msg
, valist
);
164 void warning( const char *msg
, ... )
168 if (!display_warnings
) return;
169 va_start( valist
, msg
);
172 fprintf( stderr
, "%s:", input_file_name
);
174 fprintf( stderr
, "%d:", current_line
);
175 fputc( ' ', stderr
);
177 fprintf( stderr
, "warning: " );
178 vfprintf( stderr
, msg
, valist
);
182 int output( const char *format
, ... )
187 va_start( valist
, format
);
188 ret
= vfprintf( output_file
, format
, valist
);
190 if (ret
< 0) fatal_perror( "Output error" );
194 /* find a build tool in the path, trying the various names */
195 char *find_tool( const char * const *names
)
198 static unsigned int count
, maxlen
;
208 /* split the path in directories */
210 if (!getenv( "PATH" )) return NULL
;
211 path
= xstrdup( getenv( "PATH" ));
212 for (p
= path
, count
= 2; *p
; p
++) if (*p
== ':') count
++;
213 dirs
= xmalloc( count
* sizeof(*dirs
) );
215 dirs
[count
++] = p
= path
;
218 while (*p
&& *p
!= ':') p
++;
223 for (i
= 0; i
< count
; i
++) maxlen
= max( maxlen
, strlen(dirs
[i
])+2 );
228 len
= strlen(*names
) + 1;
229 file
= xmalloc( maxlen
+ len
);
231 for (i
= 0; i
< count
; i
++)
233 strcpy( file
, dirs
[i
] );
234 p
= file
+ strlen(file
);
235 if (p
== file
) *p
++ = '.';
236 if (p
[-1] != '/') *p
++ = '/';
239 if (!stat( file
, &st
) && S_ISREG(st
.st_mode
) && (st
.st_mode
& 0111)) return file
;
247 const char *get_as_command(void)
253 as_command
= xmalloc( strlen(target_alias
) + sizeof("-as") );
254 strcpy( as_command
, target_alias
);
255 strcat( as_command
, "-as" );
259 static const char * const commands
[] = { "gas", "as", NULL
};
260 if (!(as_command
= find_tool( commands
))) as_command
= xstrdup("as");
263 if (force_pointer_size
)
265 const char *args
= (target_platform
== PLATFORM_APPLE
) ?
266 ((force_pointer_size
== 8) ? " -arch x86_64" : " -arch i386") :
267 ((force_pointer_size
== 8) ? " --64" : " --32");
268 as_command
= xrealloc( as_command
, strlen(as_command
) + strlen(args
) + 1 );
269 strcat( as_command
, args
);
275 const char *get_ld_command(void)
281 ld_command
= xmalloc( strlen(target_alias
) + sizeof("-ld") );
282 strcpy( ld_command
, target_alias
);
283 strcat( ld_command
, "-ld" );
287 static const char * const commands
[] = { "ld", "gld", NULL
};
288 if (!(ld_command
= find_tool( commands
))) ld_command
= xstrdup("ld");
291 if (force_pointer_size
)
293 const char *args
= (target_platform
== PLATFORM_APPLE
) ?
294 ((force_pointer_size
== 8) ? " -arch x86_64" : " -arch i386") :
295 ((force_pointer_size
== 8) ? " -m elf_x86_64" : " -m elf_i386");
296 ld_command
= xrealloc( ld_command
, strlen(ld_command
) + strlen(args
) + 1 );
297 strcat( ld_command
, args
);
303 const char *get_nm_command(void)
309 nm_command
= xmalloc( strlen(target_alias
) + sizeof("-nm") );
310 strcpy( nm_command
, target_alias
);
311 strcat( nm_command
, "-nm" );
315 static const char * const commands
[] = { "nm", "gnm", NULL
};
316 if (!(nm_command
= find_tool( commands
))) nm_command
= xstrdup("nm");
322 /* get a name for a temp file, automatically cleaned up on exit */
323 char *get_temp_file_name( const char *prefix
, const char *suffix
)
329 assert( nb_tmp_files
< MAX_TMP_FILES
);
330 if (!nb_tmp_files
&& !save_temps
) atexit( cleanup_tmp_files
);
332 if (!prefix
|| !prefix
[0]) prefix
= "winebuild";
333 if (!suffix
) suffix
= "";
334 if (!(ext
= strchr( prefix
, '.' ))) ext
= prefix
+ strlen(prefix
);
335 name
= xmalloc( sizeof("/tmp/") + (ext
- prefix
) + sizeof(".XXXXXX") + strlen(suffix
) );
336 strcpy( name
, "/tmp/" );
337 memcpy( name
+ 5, prefix
, ext
- prefix
);
338 strcpy( name
+ 5 + (ext
- prefix
), ".XXXXXX" );
339 strcat( name
, suffix
);
341 /* first try without the /tmp/ prefix */
342 if ((fd
= mkstemps( name
+ 5, strlen(suffix
) )) != -1)
344 else if ((fd
= mkstemps( name
, strlen(suffix
) )) == -1)
345 fatal_error( "could not generate a temp file\n" );
348 tmp_files
[nb_tmp_files
++] = name
;
352 /* output a standard header for generated files */
353 void output_standard_file_header(void)
356 output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name
);
358 output( "/* File generated automatically; do not edit! */\n" );
359 output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
362 /* dump a byte stream into the assembly code */
363 void dump_bytes( const void *buffer
, unsigned int size
)
366 const unsigned char *ptr
= buffer
;
369 output( "\t.byte " );
370 for (i
= 0; i
< size
- 1; i
++, ptr
++)
372 if ((i
% 16) == 15) output( "0x%02x\n\t.byte ", *ptr
);
373 else output( "0x%02x,", *ptr
);
375 output( "0x%02x\n", *ptr
);
379 /*******************************************************************
382 * Open a file in the given srcdir and set the input_file_name global variable.
384 FILE *open_input_file( const char *srcdir
, const char *name
)
387 FILE *file
= fopen( name
, "r" );
391 fullname
= xmalloc( strlen(srcdir
) + strlen(name
) + 2 );
392 strcpy( fullname
, srcdir
);
393 strcat( fullname
, "/" );
394 strcat( fullname
, name
);
395 file
= fopen( fullname
, "r" );
397 else fullname
= xstrdup( name
);
399 if (!file
) fatal_error( "Cannot open file '%s'\n", fullname
);
400 input_file_name
= fullname
;
406 /*******************************************************************
409 * Close the current input file (must have been opened with open_input_file).
411 void close_input_file( FILE *file
)
414 free( input_file_name
);
415 input_file_name
= NULL
;
420 /*******************************************************************
421 * remove_stdcall_decoration
423 * Remove a possible @xx suffix from a function name.
424 * Return the numerical value of the suffix, or -1 if none.
426 int remove_stdcall_decoration( char *name
)
428 char *p
, *end
= strrchr( name
, '@' );
429 if (!end
|| !end
[1] || end
== name
) return -1;
430 /* make sure all the rest is digits */
431 for (p
= end
+ 1; *p
; p
++) if (!isdigit(*p
)) return -1;
433 return atoi( end
+ 1 );
437 /*******************************************************************
440 * Run a file through the assembler.
442 void assemble_file( const char *src_file
, const char *obj_file
)
444 const char *prog
= get_as_command();
448 cmd
= xmalloc( strlen(prog
) + strlen(obj_file
) + strlen(src_file
) + 6 );
449 sprintf( cmd
, "%s -o %s %s", prog
, obj_file
, src_file
);
450 if (verbose
) fprintf( stderr
, "%s\n", cmd
);
452 if (err
) fatal_error( "%s failed with status %d\n", prog
, err
);
457 /*******************************************************************
460 * Create a new dll spec file descriptor
462 DLLSPEC
*alloc_dll_spec(void)
466 spec
= xmalloc( sizeof(*spec
) );
467 spec
->file_name
= NULL
;
468 spec
->dll_name
= NULL
;
469 spec
->init_func
= NULL
;
470 spec
->main_module
= NULL
;
471 spec
->type
= SPEC_WIN32
;
472 spec
->base
= MAX_ORDINALS
;
474 spec
->stack_size
= 0;
476 spec
->nb_entry_points
= 0;
477 spec
->alloc_entry_points
= 0;
479 spec
->nb_resources
= 0;
480 spec
->characteristics
= IMAGE_FILE_EXECUTABLE_IMAGE
;
481 if (get_ptr_size() > 4)
482 spec
->characteristics
|= IMAGE_FILE_LARGE_ADDRESS_AWARE
;
484 spec
->characteristics
|= IMAGE_FILE_32BIT_MACHINE
;
485 spec
->dll_characteristics
= IMAGE_DLLCHARACTERISTICS_NX_COMPAT
;
487 spec
->subsystem_major
= 4;
488 spec
->subsystem_minor
= 0;
489 spec
->entry_points
= NULL
;
491 spec
->ordinals
= NULL
;
492 spec
->resources
= NULL
;
497 /*******************************************************************
500 * Free dll spec file descriptor
502 void free_dll_spec( DLLSPEC
*spec
)
506 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
508 ORDDEF
*odp
= &spec
->entry_points
[i
];
510 free( odp
->export_name
);
511 free( odp
->link_name
);
513 free( spec
->file_name
);
514 free( spec
->dll_name
);
515 free( spec
->init_func
);
516 free( spec
->entry_points
);
518 free( spec
->ordinals
);
519 free( spec
->resources
);
524 /*******************************************************************
527 * Map a string to a valid C identifier.
529 const char *make_c_identifier( const char *str
)
531 static char buffer
[256];
534 for (p
= buffer
; *str
&& p
< buffer
+sizeof(buffer
)-1; p
++, str
++)
536 if (isalnum(*str
)) *p
= *str
;
544 /*******************************************************************
547 * Generate an internal name for a stub entry point.
549 const char *get_stub_name( const ORDDEF
*odp
, const DLLSPEC
*spec
)
551 static char buffer
[256];
552 if (odp
->name
|| odp
->export_name
)
555 sprintf( buffer
, "__wine_stub_%s", odp
->name
? odp
->name
: odp
->export_name
);
556 /* make sure name is a legal C identifier */
557 for (p
= buffer
; *p
; p
++) if (!isalnum(*p
) && *p
!= '_') break;
558 if (!*p
) return buffer
;
560 sprintf( buffer
, "__wine_stub_%s_%d", make_c_identifier(spec
->file_name
), odp
->ordinal
);
564 /* parse a cpu name and return the corresponding value */
565 enum target_cpu
get_cpu_from_name( const char *name
)
569 for (i
= 0; i
< sizeof(cpu_names
)/sizeof(cpu_names
[0]); i
++)
570 if (!strcmp( cpu_names
[i
].name
, name
)) return cpu_names
[i
].cpu
;
574 /*****************************************************************
575 * Function: get_alignment
578 * According to the info page for gas, the .align directive behaves
579 * differently on different systems. On some architectures, the
580 * argument of a .align directive is the number of bytes to pad to, so
581 * to align on an 8-byte boundary you'd say
583 * On other systems, the argument is "the number of low-order zero bits
584 * that the location counter must have after advancement." So to
585 * align on an 8-byte boundary you'd say
588 * The reason gas is written this way is that it's trying to mimick
589 * native assemblers for the various architectures it runs on. gas
590 * provides other directives that work consistently across
591 * architectures, but of course we want to work on all arches with or
592 * without gas. Hence this function.
596 * align -- the number of bytes to align to. Must be a power of 2.
598 unsigned int get_alignment(unsigned int align
)
602 assert( !(align
& (align
- 1)) );
609 if (target_platform
!= PLATFORM_APPLE
) return align
;
614 while ((1u << n
) != align
) n
++;
622 /* return the page size for the target CPU */
623 unsigned int get_page_size(void)
627 case CPU_x86
: return 4096;
628 case CPU_x86_64
: return 4096;
629 case CPU_POWERPC
: return 4096;
630 case CPU_SPARC
: return 8192;
631 case CPU_ALPHA
: return 8192;
638 /* return the size of a pointer on the target CPU */
639 unsigned int get_ptr_size(void)
656 /* return the assembly name for a C symbol */
657 const char *asm_name( const char *sym
)
659 static char buffer
[256];
661 switch (target_platform
)
664 case PLATFORM_WINDOWS
:
665 if (sym
[0] == '.' && sym
[1] == 'L') return sym
;
667 strcpy( buffer
+ 1, sym
);
674 /* return an assembly function declaration for a C function name */
675 const char *func_declaration( const char *func
)
677 static char buffer
[256];
679 switch (target_platform
)
683 case PLATFORM_WINDOWS
:
684 sprintf( buffer
, ".def _%s; .scl 2; .type 32; .endef", func
);
687 sprintf( buffer
, ".type %s,@function", func
);
693 /* output a size declaration for an assembly function */
694 void output_function_size( const char *name
)
696 switch (target_platform
)
699 case PLATFORM_WINDOWS
:
702 output( "\t.size %s, .-%s\n", name
, name
);
707 /* output the GNU note for non-exec stack */
708 void output_gnu_stack_note(void)
710 switch (target_platform
)
712 case PLATFORM_WINDOWS
:
716 output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
721 /* return a global symbol declaration for an assembly symbol */
722 const char *asm_globl( const char *func
)
724 static char buffer
[256];
726 switch (target_platform
)
729 sprintf( buffer
, "\t.globl _%s\n\t.private_extern _%s\n_%s:", func
, func
, func
);
731 case PLATFORM_WINDOWS
:
732 sprintf( buffer
, "\t.globl _%s\n_%s:", func
, func
);
735 sprintf( buffer
, "\t.globl %s\n\t.hidden %s\n%s:", func
, func
, func
);
740 const char *get_asm_ptr_keyword(void)
742 switch(get_ptr_size())
744 case 4: return ".long";
745 case 8: return ".quad";
751 const char *get_asm_string_keyword(void)
753 switch (target_platform
)
762 const char *get_asm_short_keyword(void)
764 switch (target_platform
)
766 default: return ".short";
770 const char *get_asm_rodata_section(void)
772 switch (target_platform
)
774 case PLATFORM_APPLE
: return ".const";
775 default: return ".section .rodata";
779 const char *get_asm_string_section(void)
781 switch (target_platform
)
783 case PLATFORM_APPLE
: return ".cstring";
784 default: return ".section .rodata";