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"
33 #ifdef HAVE_SYS_STAT_H
34 # include <sys/stat.h>
36 #ifdef HAVE_SYS_MMAN_H
42 #define MAX_TMP_FILES 8
43 static const char *tmp_files
[MAX_TMP_FILES
];
44 static unsigned int nb_tmp_files
;
57 { "amd64", CPU_x86_64
},
58 { "x86_64", CPU_x86_64
},
59 { "sparc", CPU_SPARC
},
60 { "alpha", CPU_ALPHA
},
61 { "powerpc", CPU_POWERPC
},
65 /* atexit handler to clean tmp files */
66 static void cleanup_tmp_files(void)
69 for (i
= 0; i
< MAX_TMP_FILES
; i
++) if (tmp_files
[i
]) unlink( tmp_files
[i
] );
73 void *xmalloc (size_t size
)
77 res
= malloc (size
? size
: 1);
80 fprintf (stderr
, "Virtual memory exhausted.\n");
86 void *xrealloc (void *ptr
, size_t size
)
88 void *res
= realloc (ptr
, size
);
89 if (size
&& res
== NULL
)
91 fprintf (stderr
, "Virtual memory exhausted.\n");
97 char *xstrdup( const char *str
)
99 char *res
= strdup( str
);
102 fprintf (stderr
, "Virtual memory exhausted.\n");
108 char *strupper(char *s
)
111 for (p
= s
; *p
; p
++) *p
= toupper(*p
);
115 int strendswith(const char* str
, const char* end
)
119 return l
>= m
&& strcmp(str
+ l
- m
, end
) == 0;
122 void fatal_error( const char *msg
, ... )
125 va_start( valist
, msg
);
128 fprintf( stderr
, "%s:", input_file_name
);
130 fprintf( stderr
, "%d:", current_line
);
131 fputc( ' ', stderr
);
133 else fprintf( stderr
, "winebuild: " );
134 vfprintf( stderr
, msg
, valist
);
139 void fatal_perror( const char *msg
, ... )
142 va_start( valist
, msg
);
145 fprintf( stderr
, "%s:", input_file_name
);
147 fprintf( stderr
, "%d:", current_line
);
148 fputc( ' ', stderr
);
150 vfprintf( stderr
, msg
, valist
);
156 void error( const char *msg
, ... )
159 va_start( valist
, msg
);
162 fprintf( stderr
, "%s:", input_file_name
);
164 fprintf( stderr
, "%d:", current_line
);
165 fputc( ' ', stderr
);
167 vfprintf( stderr
, msg
, valist
);
172 void warning( const char *msg
, ... )
176 if (!display_warnings
) return;
177 va_start( valist
, msg
);
180 fprintf( stderr
, "%s:", input_file_name
);
182 fprintf( stderr
, "%d:", current_line
);
183 fputc( ' ', stderr
);
185 fprintf( stderr
, "warning: " );
186 vfprintf( stderr
, msg
, valist
);
190 int output( const char *format
, ... )
195 va_start( valist
, format
);
196 ret
= vfprintf( output_file
, format
, valist
);
198 if (ret
< 0) fatal_perror( "Output error" );
202 /* find a build tool in the path, trying the various names */
203 char *find_tool( const char *name
, const char * const *names
)
206 static unsigned int count
, maxlen
;
209 const char *alt_names
[2];
215 file
= xmalloc( strlen(target_alias
) + strlen(name
) + 2 );
216 strcpy( file
, target_alias
);
218 strcat( file
, name
);
226 /* split the path in directories */
228 if (!getenv( "PATH" )) return NULL
;
229 path
= xstrdup( getenv( "PATH" ));
230 for (p
= path
, count
= 2; *p
; p
++) if (*p
== ':') count
++;
231 dirs
= xmalloc( count
* sizeof(*dirs
) );
233 dirs
[count
++] = p
= path
;
236 while (*p
&& *p
!= ':') p
++;
241 for (i
= 0; i
< count
; i
++) maxlen
= max( maxlen
, strlen(dirs
[i
])+2 );
253 len
= strlen(*names
) + sizeof(EXEEXT
) + 1;
254 file
= xmalloc( maxlen
+ len
);
256 for (i
= 0; i
< count
; i
++)
258 strcpy( file
, dirs
[i
] );
259 p
= file
+ strlen(file
);
260 if (p
== file
) *p
++ = '.';
261 if (p
[-1] != '/') *p
++ = '/';
265 if (!stat( file
, &st
) && S_ISREG(st
.st_mode
) && (st
.st_mode
& 0111)) return file
;
270 return xstrdup( name
);
273 const char *get_as_command(void)
277 static const char * const commands
[] = { "gas", "as", NULL
};
278 as_command
= find_tool( "as", commands
);
280 if (force_pointer_size
)
282 const char *args
= (target_platform
== PLATFORM_APPLE
) ?
283 ((force_pointer_size
== 8) ? " -arch x86_64" : " -arch i386") :
284 ((force_pointer_size
== 8) ? " --64" : " --32");
285 as_command
= xrealloc( as_command
, strlen(as_command
) + strlen(args
) + 1 );
286 strcat( as_command
, args
);
292 const char *get_ld_command(void)
296 static const char * const commands
[] = { "ld", "gld", NULL
};
297 ld_command
= find_tool( "ld", commands
);
299 if (force_pointer_size
)
303 switch (target_platform
)
306 args
= (force_pointer_size
== 8) ? " -arch x86_64" : " -arch i386";
308 case PLATFORM_FREEBSD
:
309 args
= (force_pointer_size
== 8) ? " -m elf_x86_64_fbsd" : " -m elf_i386_fbsd";
312 args
= (force_pointer_size
== 8) ? " -m elf_x86_64" : " -m elf_i386";
315 ld_command
= xrealloc( ld_command
, strlen(ld_command
) + strlen(args
) + 1 );
316 strcat( ld_command
, args
);
322 const char *get_nm_command(void)
326 static const char * const commands
[] = { "nm", "gnm", NULL
};
327 nm_command
= find_tool( "nm", commands
);
332 /* get a name for a temp file, automatically cleaned up on exit */
333 char *get_temp_file_name( const char *prefix
, const char *suffix
)
339 assert( nb_tmp_files
< MAX_TMP_FILES
);
340 if (!nb_tmp_files
&& !save_temps
) atexit( cleanup_tmp_files
);
342 if (!prefix
|| !prefix
[0]) prefix
= "winebuild";
343 if (!suffix
) suffix
= "";
344 if (!(ext
= strchr( prefix
, '.' ))) ext
= prefix
+ strlen(prefix
);
345 name
= xmalloc( sizeof("/tmp/") + (ext
- prefix
) + sizeof(".XXXXXX") + strlen(suffix
) );
346 strcpy( name
, "/tmp/" );
347 memcpy( name
+ 5, prefix
, ext
- prefix
);
348 strcpy( name
+ 5 + (ext
- prefix
), ".XXXXXX" );
349 strcat( name
, suffix
);
351 /* first try without the /tmp/ prefix */
352 if ((fd
= mkstemps( name
+ 5, strlen(suffix
) )) != -1)
354 else if ((fd
= mkstemps( name
, strlen(suffix
) )) == -1)
355 fatal_error( "could not generate a temp file\n" );
358 tmp_files
[nb_tmp_files
++] = name
;
362 /*******************************************************************
365 * Function for reading from/writing to a memory buffer.
368 int byte_swapped
= 0;
369 const char *input_buffer_filename
;
370 const unsigned char *input_buffer
;
371 size_t input_buffer_pos
;
372 size_t input_buffer_size
;
373 unsigned char *output_buffer
;
374 size_t output_buffer_pos
;
375 size_t output_buffer_size
;
377 static void check_output_buffer_space( size_t size
)
379 if (output_buffer_pos
+ size
>= output_buffer_size
)
381 output_buffer_size
= max( output_buffer_size
* 2, output_buffer_pos
+ size
);
382 output_buffer
= xrealloc( output_buffer
, output_buffer_size
);
386 void init_input_buffer( const char *file
)
391 if ((fd
= open( file
, O_RDONLY
| O_BINARY
)) == -1) fatal_perror( "Cannot open %s", file
);
392 if ((fstat( fd
, &st
) == -1)) fatal_perror( "Cannot stat %s", file
);
393 if (!st
.st_size
) fatal_error( "%s is an empty file\n", file
);
395 if ((input_buffer
= mmap( NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0 )) == (void*)-1)
398 unsigned char *buffer
= xmalloc( st
.st_size
);
399 if (read( fd
, buffer
, st
.st_size
) != st
.st_size
) fatal_error( "Cannot read %s\n", file
);
400 input_buffer
= buffer
;
403 input_buffer_filename
= xstrdup( file
);
404 input_buffer_size
= st
.st_size
;
405 input_buffer_pos
= 0;
409 void init_output_buffer(void)
411 output_buffer_size
= 1024;
412 output_buffer_pos
= 0;
413 output_buffer
= xmalloc( output_buffer_size
);
416 void flush_output_buffer(void)
418 if (fwrite( output_buffer
, 1, output_buffer_pos
, output_file
) != output_buffer_pos
)
419 fatal_error( "Error writing to %s\n", output_file_name
);
420 free( output_buffer
);
423 unsigned char get_byte(void)
425 if (input_buffer_pos
>= input_buffer_size
)
426 fatal_error( "%s is a truncated file\n", input_buffer_filename
);
427 return input_buffer
[input_buffer_pos
++];
430 unsigned short get_word(void)
434 if (input_buffer_pos
+ sizeof(ret
) > input_buffer_size
)
435 fatal_error( "%s is a truncated file\n", input_buffer_filename
);
436 memcpy( &ret
, input_buffer
+ input_buffer_pos
, sizeof(ret
) );
437 if (byte_swapped
) ret
= (ret
<< 8) | (ret
>> 8);
438 input_buffer_pos
+= sizeof(ret
);
442 unsigned int get_dword(void)
446 if (input_buffer_pos
+ sizeof(ret
) > input_buffer_size
)
447 fatal_error( "%s is a truncated file\n", input_buffer_filename
);
448 memcpy( &ret
, input_buffer
+ input_buffer_pos
, sizeof(ret
) );
450 ret
= ((ret
<< 24) | ((ret
<< 8) & 0x00ff0000) | ((ret
>> 8) & 0x0000ff00) | (ret
>> 24));
451 input_buffer_pos
+= sizeof(ret
);
455 void put_data( const void *data
, size_t size
)
457 check_output_buffer_space( size
);
458 memcpy( output_buffer
+ output_buffer_pos
, data
, size
);
459 output_buffer_pos
+= size
;
462 void put_byte( unsigned char val
)
464 check_output_buffer_space( 1 );
465 output_buffer
[output_buffer_pos
++] = val
;
468 void put_word( unsigned short val
)
470 if (byte_swapped
) val
= (val
<< 8) | (val
>> 8);
471 put_data( &val
, sizeof(val
) );
474 void put_dword( unsigned int val
)
477 val
= ((val
<< 24) | ((val
<< 8) & 0x00ff0000) | ((val
>> 8) & 0x0000ff00) | (val
>> 24));
478 put_data( &val
, sizeof(val
) );
481 void put_qword( unsigned int val
)
495 /* pointer-sized word */
496 void put_pword( unsigned int val
)
498 if (get_ptr_size() == 8) put_qword( val
);
499 else put_dword( val
);
502 void align_output( unsigned int align
)
504 size_t size
= align
- (output_buffer_pos
% align
);
506 if (size
== align
) return;
507 check_output_buffer_space( size
);
508 memset( output_buffer
+ output_buffer_pos
, 0, size
);
509 output_buffer_pos
+= size
;
512 /* output a standard header for generated files */
513 void output_standard_file_header(void)
516 output( "/* File generated automatically from %s; do not edit! */\n", spec_file_name
);
518 output( "/* File generated automatically; do not edit! */\n" );
519 output( "/* This file can be copied, modified and distributed without restriction. */\n\n" );
522 /* dump a byte stream into the assembly code */
523 void dump_bytes( const void *buffer
, unsigned int size
)
526 const unsigned char *ptr
= buffer
;
529 output( "\t.byte " );
530 for (i
= 0; i
< size
- 1; i
++, ptr
++)
532 if ((i
% 16) == 15) output( "0x%02x\n\t.byte ", *ptr
);
533 else output( "0x%02x,", *ptr
);
535 output( "0x%02x\n", *ptr
);
539 /*******************************************************************
542 * Open a file in the given srcdir and set the input_file_name global variable.
544 FILE *open_input_file( const char *srcdir
, const char *name
)
547 FILE *file
= fopen( name
, "r" );
551 fullname
= xmalloc( strlen(srcdir
) + strlen(name
) + 2 );
552 strcpy( fullname
, srcdir
);
553 strcat( fullname
, "/" );
554 strcat( fullname
, name
);
555 file
= fopen( fullname
, "r" );
557 else fullname
= xstrdup( name
);
559 if (!file
) fatal_error( "Cannot open file '%s'\n", fullname
);
560 input_file_name
= fullname
;
566 /*******************************************************************
569 * Close the current input file (must have been opened with open_input_file).
571 void close_input_file( FILE *file
)
574 free( input_file_name
);
575 input_file_name
= NULL
;
580 /*******************************************************************
581 * remove_stdcall_decoration
583 * Remove a possible @xx suffix from a function name.
584 * Return the numerical value of the suffix, or -1 if none.
586 int remove_stdcall_decoration( char *name
)
588 char *p
, *end
= strrchr( name
, '@' );
589 if (!end
|| !end
[1] || end
== name
) return -1;
590 /* make sure all the rest is digits */
591 for (p
= end
+ 1; *p
; p
++) if (!isdigit(*p
)) return -1;
593 return atoi( end
+ 1 );
597 /*******************************************************************
600 * Run a file through the assembler.
602 void assemble_file( const char *src_file
, const char *obj_file
)
604 const char *prog
= get_as_command();
608 cmd
= xmalloc( strlen(prog
) + strlen(obj_file
) + strlen(src_file
) + 6 );
609 sprintf( cmd
, "%s -o %s %s", prog
, obj_file
, src_file
);
610 if (verbose
) fprintf( stderr
, "%s\n", cmd
);
612 if (err
) fatal_error( "%s failed with status %d\n", prog
, err
);
617 /*******************************************************************
620 * Create a new dll spec file descriptor
622 DLLSPEC
*alloc_dll_spec(void)
626 spec
= xmalloc( sizeof(*spec
) );
627 spec
->file_name
= NULL
;
628 spec
->dll_name
= NULL
;
629 spec
->init_func
= NULL
;
630 spec
->main_module
= NULL
;
631 spec
->type
= SPEC_WIN32
;
632 spec
->base
= MAX_ORDINALS
;
634 spec
->stack_size
= 0;
636 spec
->nb_entry_points
= 0;
637 spec
->alloc_entry_points
= 0;
639 spec
->nb_resources
= 0;
640 spec
->characteristics
= IMAGE_FILE_EXECUTABLE_IMAGE
;
641 if (get_ptr_size() > 4)
642 spec
->characteristics
|= IMAGE_FILE_LARGE_ADDRESS_AWARE
;
644 spec
->characteristics
|= IMAGE_FILE_32BIT_MACHINE
;
645 spec
->dll_characteristics
= IMAGE_DLLCHARACTERISTICS_NX_COMPAT
;
647 spec
->subsystem_major
= 4;
648 spec
->subsystem_minor
= 0;
649 spec
->entry_points
= NULL
;
651 spec
->ordinals
= NULL
;
652 spec
->resources
= NULL
;
657 /*******************************************************************
660 * Free dll spec file descriptor
662 void free_dll_spec( DLLSPEC
*spec
)
666 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
668 ORDDEF
*odp
= &spec
->entry_points
[i
];
670 free( odp
->export_name
);
671 free( odp
->link_name
);
673 free( spec
->file_name
);
674 free( spec
->dll_name
);
675 free( spec
->init_func
);
676 free( spec
->entry_points
);
678 free( spec
->ordinals
);
679 free( spec
->resources
);
684 /*******************************************************************
687 * Map a string to a valid C identifier.
689 const char *make_c_identifier( const char *str
)
691 static char buffer
[256];
694 for (p
= buffer
; *str
&& p
< buffer
+sizeof(buffer
)-1; p
++, str
++)
696 if (isalnum(*str
)) *p
= *str
;
704 /*******************************************************************
707 * Generate an internal name for a stub entry point.
709 const char *get_stub_name( const ORDDEF
*odp
, const DLLSPEC
*spec
)
711 static char buffer
[256];
712 if (odp
->name
|| odp
->export_name
)
715 sprintf( buffer
, "__wine_stub_%s", odp
->name
? odp
->name
: odp
->export_name
);
716 /* make sure name is a legal C identifier */
717 for (p
= buffer
; *p
; p
++) if (!isalnum(*p
) && *p
!= '_') break;
718 if (!*p
) return buffer
;
720 sprintf( buffer
, "__wine_stub_%s_%d", make_c_identifier(spec
->file_name
), odp
->ordinal
);
724 /* parse a cpu name and return the corresponding value */
725 enum target_cpu
get_cpu_from_name( const char *name
)
729 for (i
= 0; i
< sizeof(cpu_names
)/sizeof(cpu_names
[0]); i
++)
730 if (!strcmp( cpu_names
[i
].name
, name
)) return cpu_names
[i
].cpu
;
734 /*****************************************************************
735 * Function: get_alignment
738 * According to the info page for gas, the .align directive behaves
739 * differently on different systems. On some architectures, the
740 * argument of a .align directive is the number of bytes to pad to, so
741 * to align on an 8-byte boundary you'd say
743 * On other systems, the argument is "the number of low-order zero bits
744 * that the location counter must have after advancement." So to
745 * align on an 8-byte boundary you'd say
748 * The reason gas is written this way is that it's trying to mimick
749 * native assemblers for the various architectures it runs on. gas
750 * provides other directives that work consistently across
751 * architectures, but of course we want to work on all arches with or
752 * without gas. Hence this function.
756 * align -- the number of bytes to align to. Must be a power of 2.
758 unsigned int get_alignment(unsigned int align
)
762 assert( !(align
& (align
- 1)) );
770 if (target_platform
!= PLATFORM_APPLE
) return align
;
775 while ((1u << n
) != align
) n
++;
783 /* return the page size for the target CPU */
784 unsigned int get_page_size(void)
788 case CPU_x86
: return 4096;
789 case CPU_x86_64
: return 4096;
790 case CPU_POWERPC
: return 4096;
791 case CPU_ARM
: return 4096;
792 case CPU_SPARC
: return 8192;
793 case CPU_ALPHA
: return 8192;
800 /* return the size of a pointer on the target CPU */
801 unsigned int get_ptr_size(void)
819 /* return the assembly name for a C symbol */
820 const char *asm_name( const char *sym
)
822 static char buffer
[256];
824 switch (target_platform
)
827 case PLATFORM_WINDOWS
:
828 if (sym
[0] == '.' && sym
[1] == 'L') return sym
;
830 strcpy( buffer
+ 1, sym
);
837 /* return an assembly function declaration for a C function name */
838 const char *func_declaration( const char *func
)
840 static char buffer
[256];
842 switch (target_platform
)
846 case PLATFORM_WINDOWS
:
847 sprintf( buffer
, ".def _%s; .scl 2; .type 32; .endef", func
);
853 sprintf( buffer
, ".type %s,%%function", func
);
856 sprintf( buffer
, ".type %s,@function", func
);
864 /* output a size declaration for an assembly function */
865 void output_function_size( const char *name
)
867 switch (target_platform
)
870 case PLATFORM_WINDOWS
:
873 output( "\t.size %s, .-%s\n", name
, name
);
878 /* output a .cfi directive */
879 void output_cfi( const char *format
, ... )
883 if (!unwind_tables
) return;
884 va_start( valist
, format
);
885 fputc( '\t', output_file
);
886 vfprintf( output_file
, format
, valist
);
887 fputc( '\n', output_file
);
891 /* output the GNU note for non-exec stack */
892 void output_gnu_stack_note(void)
894 switch (target_platform
)
896 case PLATFORM_WINDOWS
:
903 output( "\t.section .note.GNU-stack,\"\",%%progbits\n" );
906 output( "\t.section .note.GNU-stack,\"\",@progbits\n" );
913 /* return a global symbol declaration for an assembly symbol */
914 const char *asm_globl( const char *func
)
916 static char buffer
[256];
918 switch (target_platform
)
921 sprintf( buffer
, "\t.globl _%s\n\t.private_extern _%s\n_%s:", func
, func
, func
);
923 case PLATFORM_WINDOWS
:
924 sprintf( buffer
, "\t.globl _%s\n_%s:", func
, func
);
927 sprintf( buffer
, "\t.globl %s\n\t.hidden %s\n%s:", func
, func
, func
);
932 const char *get_asm_ptr_keyword(void)
934 switch(get_ptr_size())
936 case 4: return ".long";
937 case 8: return ".quad";
943 const char *get_asm_string_keyword(void)
945 switch (target_platform
)
954 const char *get_asm_short_keyword(void)
956 switch (target_platform
)
958 default: return ".short";
962 const char *get_asm_rodata_section(void)
964 switch (target_platform
)
966 case PLATFORM_APPLE
: return ".const";
967 default: return ".section .rodata";
971 const char *get_asm_string_section(void)
973 switch (target_platform
)
975 case PLATFORM_APPLE
: return ".cstring";
976 default: return ".section .rodata";